xref: /openbmc/linux/net/bluetooth/iso.c (revision b4443423)
1ccf74f23SLuiz Augusto von Dentz // SPDX-License-Identifier: GPL-2.0
2ccf74f23SLuiz Augusto von Dentz /*
3ccf74f23SLuiz Augusto von Dentz  * BlueZ - Bluetooth protocol stack for Linux
4ccf74f23SLuiz Augusto von Dentz  *
5ccf74f23SLuiz Augusto von Dentz  * Copyright (C) 2022 Intel Corporation
6ccf74f23SLuiz Augusto von Dentz  */
7ccf74f23SLuiz Augusto von Dentz 
8ccf74f23SLuiz Augusto von Dentz #include <linux/module.h>
9ccf74f23SLuiz Augusto von Dentz #include <linux/debugfs.h>
10ccf74f23SLuiz Augusto von Dentz #include <linux/seq_file.h>
11ccf74f23SLuiz Augusto von Dentz #include <linux/sched/signal.h>
12ccf74f23SLuiz Augusto von Dentz 
13ccf74f23SLuiz Augusto von Dentz #include <net/bluetooth/bluetooth.h>
14ccf74f23SLuiz Augusto von Dentz #include <net/bluetooth/hci_core.h>
15ccf74f23SLuiz Augusto von Dentz #include <net/bluetooth/iso.h>
16ccf74f23SLuiz Augusto von Dentz 
17ccf74f23SLuiz Augusto von Dentz static const struct proto_ops iso_sock_ops;
18ccf74f23SLuiz Augusto von Dentz 
19ccf74f23SLuiz Augusto von Dentz static struct bt_sock_list iso_sk_list = {
20ccf74f23SLuiz Augusto von Dentz 	.lock = __RW_LOCK_UNLOCKED(iso_sk_list.lock)
21ccf74f23SLuiz Augusto von Dentz };
22ccf74f23SLuiz Augusto von Dentz 
23ccf74f23SLuiz Augusto von Dentz /* ---- ISO connections ---- */
24ccf74f23SLuiz Augusto von Dentz struct iso_conn {
25ccf74f23SLuiz Augusto von Dentz 	struct hci_conn	*hcon;
26ccf74f23SLuiz Augusto von Dentz 
27ccf74f23SLuiz Augusto von Dentz 	/* @lock: spinlock protecting changes to iso_conn fields */
28ccf74f23SLuiz Augusto von Dentz 	spinlock_t	lock;
29ccf74f23SLuiz Augusto von Dentz 	struct sock	*sk;
30ccf74f23SLuiz Augusto von Dentz 
31ccf74f23SLuiz Augusto von Dentz 	struct delayed_work	timeout_work;
32ccf74f23SLuiz Augusto von Dentz 
33ccf74f23SLuiz Augusto von Dentz 	struct sk_buff	*rx_skb;
34ccf74f23SLuiz Augusto von Dentz 	__u32		rx_len;
35ccf74f23SLuiz Augusto von Dentz 	__u16		tx_sn;
36ccf74f23SLuiz Augusto von Dentz };
37ccf74f23SLuiz Augusto von Dentz 
38ccf74f23SLuiz Augusto von Dentz #define iso_conn_lock(c)	spin_lock(&(c)->lock)
39ccf74f23SLuiz Augusto von Dentz #define iso_conn_unlock(c)	spin_unlock(&(c)->lock)
40ccf74f23SLuiz Augusto von Dentz 
41ccf74f23SLuiz Augusto von Dentz static void iso_sock_close(struct sock *sk);
42ccf74f23SLuiz Augusto von Dentz static void iso_sock_kill(struct sock *sk);
43ccf74f23SLuiz Augusto von Dentz 
44ccf74f23SLuiz Augusto von Dentz /* ----- ISO socket info ----- */
45ccf74f23SLuiz Augusto von Dentz #define iso_pi(sk) ((struct iso_pinfo *)sk)
46ccf74f23SLuiz Augusto von Dentz 
47*b4443423SLuiz Augusto von Dentz #define EIR_SERVICE_DATA_LENGTH 4
48*b4443423SLuiz Augusto von Dentz #define BASE_MAX_LENGTH (HCI_MAX_PER_AD_LENGTH - EIR_SERVICE_DATA_LENGTH)
49*b4443423SLuiz Augusto von Dentz 
50ccf74f23SLuiz Augusto von Dentz struct iso_pinfo {
51ccf74f23SLuiz Augusto von Dentz 	struct bt_sock		bt;
52ccf74f23SLuiz Augusto von Dentz 	bdaddr_t		src;
53ccf74f23SLuiz Augusto von Dentz 	__u8			src_type;
54ccf74f23SLuiz Augusto von Dentz 	bdaddr_t		dst;
55ccf74f23SLuiz Augusto von Dentz 	__u8			dst_type;
56f764a6c2SLuiz Augusto von Dentz 	__u8			bc_sid;
57f764a6c2SLuiz Augusto von Dentz 	__u8			bc_num_bis;
58f764a6c2SLuiz Augusto von Dentz 	__u8			bc_bis[ISO_MAX_NUM_BIS];
59f764a6c2SLuiz Augusto von Dentz 	__u16			sync_handle;
60ccf74f23SLuiz Augusto von Dentz 	__u32			flags;
61ccf74f23SLuiz Augusto von Dentz 	struct bt_iso_qos	qos;
62f764a6c2SLuiz Augusto von Dentz 	__u8			base_len;
63*b4443423SLuiz Augusto von Dentz 	__u8			base[BASE_MAX_LENGTH];
64ccf74f23SLuiz Augusto von Dentz 	struct iso_conn		*conn;
65ccf74f23SLuiz Augusto von Dentz };
66ccf74f23SLuiz Augusto von Dentz 
67ccf74f23SLuiz Augusto von Dentz /* ---- ISO timers ---- */
68ccf74f23SLuiz Augusto von Dentz #define ISO_CONN_TIMEOUT	(HZ * 40)
69ccf74f23SLuiz Augusto von Dentz #define ISO_DISCONN_TIMEOUT	(HZ * 2)
70ccf74f23SLuiz Augusto von Dentz 
71ccf74f23SLuiz Augusto von Dentz static void iso_sock_timeout(struct work_struct *work)
72ccf74f23SLuiz Augusto von Dentz {
73ccf74f23SLuiz Augusto von Dentz 	struct iso_conn *conn = container_of(work, struct iso_conn,
74ccf74f23SLuiz Augusto von Dentz 					     timeout_work.work);
75ccf74f23SLuiz Augusto von Dentz 	struct sock *sk;
76ccf74f23SLuiz Augusto von Dentz 
77ccf74f23SLuiz Augusto von Dentz 	iso_conn_lock(conn);
78ccf74f23SLuiz Augusto von Dentz 	sk = conn->sk;
79ccf74f23SLuiz Augusto von Dentz 	if (sk)
80ccf74f23SLuiz Augusto von Dentz 		sock_hold(sk);
81ccf74f23SLuiz Augusto von Dentz 	iso_conn_unlock(conn);
82ccf74f23SLuiz Augusto von Dentz 
83ccf74f23SLuiz Augusto von Dentz 	if (!sk)
84ccf74f23SLuiz Augusto von Dentz 		return;
85ccf74f23SLuiz Augusto von Dentz 
86ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sock %p state %d", sk, sk->sk_state);
87ccf74f23SLuiz Augusto von Dentz 
88ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
89ccf74f23SLuiz Augusto von Dentz 	sk->sk_err = ETIMEDOUT;
90ccf74f23SLuiz Augusto von Dentz 	sk->sk_state_change(sk);
91ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
92ccf74f23SLuiz Augusto von Dentz 	sock_put(sk);
93ccf74f23SLuiz Augusto von Dentz }
94ccf74f23SLuiz Augusto von Dentz 
95ccf74f23SLuiz Augusto von Dentz static void iso_sock_set_timer(struct sock *sk, long timeout)
96ccf74f23SLuiz Augusto von Dentz {
97ccf74f23SLuiz Augusto von Dentz 	if (!iso_pi(sk)->conn)
98ccf74f23SLuiz Augusto von Dentz 		return;
99ccf74f23SLuiz Augusto von Dentz 
100ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
101ccf74f23SLuiz Augusto von Dentz 	cancel_delayed_work(&iso_pi(sk)->conn->timeout_work);
102ccf74f23SLuiz Augusto von Dentz 	schedule_delayed_work(&iso_pi(sk)->conn->timeout_work, timeout);
103ccf74f23SLuiz Augusto von Dentz }
104ccf74f23SLuiz Augusto von Dentz 
105ccf74f23SLuiz Augusto von Dentz static void iso_sock_clear_timer(struct sock *sk)
106ccf74f23SLuiz Augusto von Dentz {
107ccf74f23SLuiz Augusto von Dentz 	if (!iso_pi(sk)->conn)
108ccf74f23SLuiz Augusto von Dentz 		return;
109ccf74f23SLuiz Augusto von Dentz 
110ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sock %p state %d", sk, sk->sk_state);
111ccf74f23SLuiz Augusto von Dentz 	cancel_delayed_work(&iso_pi(sk)->conn->timeout_work);
112ccf74f23SLuiz Augusto von Dentz }
113ccf74f23SLuiz Augusto von Dentz 
114ccf74f23SLuiz Augusto von Dentz /* ---- ISO connections ---- */
115ccf74f23SLuiz Augusto von Dentz static struct iso_conn *iso_conn_add(struct hci_conn *hcon)
116ccf74f23SLuiz Augusto von Dentz {
117ccf74f23SLuiz Augusto von Dentz 	struct iso_conn *conn = hcon->iso_data;
118ccf74f23SLuiz Augusto von Dentz 
119ccf74f23SLuiz Augusto von Dentz 	if (conn)
120ccf74f23SLuiz Augusto von Dentz 		return conn;
121ccf74f23SLuiz Augusto von Dentz 
122ccf74f23SLuiz Augusto von Dentz 	conn = kzalloc(sizeof(*conn), GFP_KERNEL);
123ccf74f23SLuiz Augusto von Dentz 	if (!conn)
124ccf74f23SLuiz Augusto von Dentz 		return NULL;
125ccf74f23SLuiz Augusto von Dentz 
126ccf74f23SLuiz Augusto von Dentz 	spin_lock_init(&conn->lock);
127ccf74f23SLuiz Augusto von Dentz 	INIT_DELAYED_WORK(&conn->timeout_work, iso_sock_timeout);
128ccf74f23SLuiz Augusto von Dentz 
129ccf74f23SLuiz Augusto von Dentz 	hcon->iso_data = conn;
130ccf74f23SLuiz Augusto von Dentz 	conn->hcon = hcon;
131ccf74f23SLuiz Augusto von Dentz 	conn->tx_sn = 0;
132ccf74f23SLuiz Augusto von Dentz 
133ccf74f23SLuiz Augusto von Dentz 	BT_DBG("hcon %p conn %p", hcon, conn);
134ccf74f23SLuiz Augusto von Dentz 
135ccf74f23SLuiz Augusto von Dentz 	return conn;
136ccf74f23SLuiz Augusto von Dentz }
137ccf74f23SLuiz Augusto von Dentz 
138ccf74f23SLuiz Augusto von Dentz /* Delete channel. Must be called on the locked socket. */
139ccf74f23SLuiz Augusto von Dentz static void iso_chan_del(struct sock *sk, int err)
140ccf74f23SLuiz Augusto von Dentz {
141ccf74f23SLuiz Augusto von Dentz 	struct iso_conn *conn;
142f764a6c2SLuiz Augusto von Dentz 	struct sock *parent;
143ccf74f23SLuiz Augusto von Dentz 
144ccf74f23SLuiz Augusto von Dentz 	conn = iso_pi(sk)->conn;
145ccf74f23SLuiz Augusto von Dentz 
146ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
147ccf74f23SLuiz Augusto von Dentz 
148ccf74f23SLuiz Augusto von Dentz 	if (conn) {
149ccf74f23SLuiz Augusto von Dentz 		iso_conn_lock(conn);
150ccf74f23SLuiz Augusto von Dentz 		conn->sk = NULL;
151ccf74f23SLuiz Augusto von Dentz 		iso_pi(sk)->conn = NULL;
152ccf74f23SLuiz Augusto von Dentz 		iso_conn_unlock(conn);
153ccf74f23SLuiz Augusto von Dentz 
154ccf74f23SLuiz Augusto von Dentz 		if (conn->hcon)
155ccf74f23SLuiz Augusto von Dentz 			hci_conn_drop(conn->hcon);
156ccf74f23SLuiz Augusto von Dentz 	}
157ccf74f23SLuiz Augusto von Dentz 
158ccf74f23SLuiz Augusto von Dentz 	sk->sk_state = BT_CLOSED;
159ccf74f23SLuiz Augusto von Dentz 	sk->sk_err   = err;
160f764a6c2SLuiz Augusto von Dentz 
161f764a6c2SLuiz Augusto von Dentz 	parent = bt_sk(sk)->parent;
162f764a6c2SLuiz Augusto von Dentz 	if (parent) {
163f764a6c2SLuiz Augusto von Dentz 		bt_accept_unlink(sk);
164f764a6c2SLuiz Augusto von Dentz 		parent->sk_data_ready(parent);
165f764a6c2SLuiz Augusto von Dentz 	} else {
166ccf74f23SLuiz Augusto von Dentz 		sk->sk_state_change(sk);
167f764a6c2SLuiz Augusto von Dentz 	}
168ccf74f23SLuiz Augusto von Dentz 
169ccf74f23SLuiz Augusto von Dentz 	sock_set_flag(sk, SOCK_ZAPPED);
170ccf74f23SLuiz Augusto von Dentz }
171ccf74f23SLuiz Augusto von Dentz 
172ccf74f23SLuiz Augusto von Dentz static void iso_conn_del(struct hci_conn *hcon, int err)
173ccf74f23SLuiz Augusto von Dentz {
174ccf74f23SLuiz Augusto von Dentz 	struct iso_conn *conn = hcon->iso_data;
175ccf74f23SLuiz Augusto von Dentz 	struct sock *sk;
176ccf74f23SLuiz Augusto von Dentz 
177ccf74f23SLuiz Augusto von Dentz 	if (!conn)
178ccf74f23SLuiz Augusto von Dentz 		return;
179ccf74f23SLuiz Augusto von Dentz 
180ccf74f23SLuiz Augusto von Dentz 	BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
181ccf74f23SLuiz Augusto von Dentz 
182ccf74f23SLuiz Augusto von Dentz 	/* Kill socket */
183ccf74f23SLuiz Augusto von Dentz 	iso_conn_lock(conn);
184ccf74f23SLuiz Augusto von Dentz 	sk = conn->sk;
185ccf74f23SLuiz Augusto von Dentz 	if (sk)
186ccf74f23SLuiz Augusto von Dentz 		sock_hold(sk);
187ccf74f23SLuiz Augusto von Dentz 	iso_conn_unlock(conn);
188ccf74f23SLuiz Augusto von Dentz 
189ccf74f23SLuiz Augusto von Dentz 	if (sk) {
190ccf74f23SLuiz Augusto von Dentz 		lock_sock(sk);
191ccf74f23SLuiz Augusto von Dentz 		iso_sock_clear_timer(sk);
192ccf74f23SLuiz Augusto von Dentz 		iso_chan_del(sk, err);
193ccf74f23SLuiz Augusto von Dentz 		release_sock(sk);
194ccf74f23SLuiz Augusto von Dentz 		sock_put(sk);
195ccf74f23SLuiz Augusto von Dentz 	}
196ccf74f23SLuiz Augusto von Dentz 
197ccf74f23SLuiz Augusto von Dentz 	/* Ensure no more work items will run before freeing conn. */
198ccf74f23SLuiz Augusto von Dentz 	cancel_delayed_work_sync(&conn->timeout_work);
199ccf74f23SLuiz Augusto von Dentz 
200ccf74f23SLuiz Augusto von Dentz 	hcon->iso_data = NULL;
201ccf74f23SLuiz Augusto von Dentz 	kfree(conn);
202ccf74f23SLuiz Augusto von Dentz }
203ccf74f23SLuiz Augusto von Dentz 
204ccf74f23SLuiz Augusto von Dentz static int __iso_chan_add(struct iso_conn *conn, struct sock *sk,
205ccf74f23SLuiz Augusto von Dentz 			  struct sock *parent)
206ccf74f23SLuiz Augusto von Dentz {
207ccf74f23SLuiz Augusto von Dentz 	BT_DBG("conn %p", conn);
208ccf74f23SLuiz Augusto von Dentz 
209ccf74f23SLuiz Augusto von Dentz 	if (iso_pi(sk)->conn == conn && conn->sk == sk)
210ccf74f23SLuiz Augusto von Dentz 		return 0;
211ccf74f23SLuiz Augusto von Dentz 
212ccf74f23SLuiz Augusto von Dentz 	if (conn->sk) {
213ccf74f23SLuiz Augusto von Dentz 		BT_ERR("conn->sk already set");
214ccf74f23SLuiz Augusto von Dentz 		return -EBUSY;
215ccf74f23SLuiz Augusto von Dentz 	}
216ccf74f23SLuiz Augusto von Dentz 
217ccf74f23SLuiz Augusto von Dentz 	iso_pi(sk)->conn = conn;
218ccf74f23SLuiz Augusto von Dentz 	conn->sk = sk;
219ccf74f23SLuiz Augusto von Dentz 
220ccf74f23SLuiz Augusto von Dentz 	if (parent)
221ccf74f23SLuiz Augusto von Dentz 		bt_accept_enqueue(parent, sk, true);
222ccf74f23SLuiz Augusto von Dentz 
223ccf74f23SLuiz Augusto von Dentz 	return 0;
224ccf74f23SLuiz Augusto von Dentz }
225ccf74f23SLuiz Augusto von Dentz 
226ccf74f23SLuiz Augusto von Dentz static int iso_chan_add(struct iso_conn *conn, struct sock *sk,
227ccf74f23SLuiz Augusto von Dentz 			struct sock *parent)
228ccf74f23SLuiz Augusto von Dentz {
229ccf74f23SLuiz Augusto von Dentz 	int err;
230ccf74f23SLuiz Augusto von Dentz 
231ccf74f23SLuiz Augusto von Dentz 	iso_conn_lock(conn);
232ccf74f23SLuiz Augusto von Dentz 	err = __iso_chan_add(conn, sk, parent);
233ccf74f23SLuiz Augusto von Dentz 	iso_conn_unlock(conn);
234ccf74f23SLuiz Augusto von Dentz 
235ccf74f23SLuiz Augusto von Dentz 	return err;
236ccf74f23SLuiz Augusto von Dentz }
237ccf74f23SLuiz Augusto von Dentz 
238f764a6c2SLuiz Augusto von Dentz static int iso_connect_bis(struct sock *sk)
239f764a6c2SLuiz Augusto von Dentz {
240f764a6c2SLuiz Augusto von Dentz 	struct iso_conn *conn;
241f764a6c2SLuiz Augusto von Dentz 	struct hci_conn *hcon;
242f764a6c2SLuiz Augusto von Dentz 	struct hci_dev  *hdev;
243f764a6c2SLuiz Augusto von Dentz 	int err;
244f764a6c2SLuiz Augusto von Dentz 
245f764a6c2SLuiz Augusto von Dentz 	BT_DBG("%pMR", &iso_pi(sk)->src);
246f764a6c2SLuiz Augusto von Dentz 
247f764a6c2SLuiz Augusto von Dentz 	hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
248f764a6c2SLuiz Augusto von Dentz 			     iso_pi(sk)->src_type);
249f764a6c2SLuiz Augusto von Dentz 	if (!hdev)
250f764a6c2SLuiz Augusto von Dentz 		return -EHOSTUNREACH;
251f764a6c2SLuiz Augusto von Dentz 
252f764a6c2SLuiz Augusto von Dentz 	hci_dev_lock(hdev);
253f764a6c2SLuiz Augusto von Dentz 
254f764a6c2SLuiz Augusto von Dentz 	if (!bis_capable(hdev)) {
255f764a6c2SLuiz Augusto von Dentz 		err = -EOPNOTSUPP;
256f764a6c2SLuiz Augusto von Dentz 		goto done;
257f764a6c2SLuiz Augusto von Dentz 	}
258f764a6c2SLuiz Augusto von Dentz 
259f764a6c2SLuiz Augusto von Dentz 	/* Fail if out PHYs are marked as disabled */
260f764a6c2SLuiz Augusto von Dentz 	if (!iso_pi(sk)->qos.out.phy) {
261f764a6c2SLuiz Augusto von Dentz 		err = -EINVAL;
262f764a6c2SLuiz Augusto von Dentz 		goto done;
263f764a6c2SLuiz Augusto von Dentz 	}
264f764a6c2SLuiz Augusto von Dentz 
265f764a6c2SLuiz Augusto von Dentz 	hcon = hci_connect_bis(hdev, &iso_pi(sk)->dst, iso_pi(sk)->dst_type,
266f764a6c2SLuiz Augusto von Dentz 			       &iso_pi(sk)->qos, iso_pi(sk)->base_len,
267f764a6c2SLuiz Augusto von Dentz 			       iso_pi(sk)->base);
268f764a6c2SLuiz Augusto von Dentz 	if (IS_ERR(hcon)) {
269f764a6c2SLuiz Augusto von Dentz 		err = PTR_ERR(hcon);
270f764a6c2SLuiz Augusto von Dentz 		goto done;
271f764a6c2SLuiz Augusto von Dentz 	}
272f764a6c2SLuiz Augusto von Dentz 
273f764a6c2SLuiz Augusto von Dentz 	conn = iso_conn_add(hcon);
274f764a6c2SLuiz Augusto von Dentz 	if (!conn) {
275f764a6c2SLuiz Augusto von Dentz 		hci_conn_drop(hcon);
276f764a6c2SLuiz Augusto von Dentz 		err = -ENOMEM;
277f764a6c2SLuiz Augusto von Dentz 		goto done;
278f764a6c2SLuiz Augusto von Dentz 	}
279f764a6c2SLuiz Augusto von Dentz 
280f764a6c2SLuiz Augusto von Dentz 	/* Update source addr of the socket */
281f764a6c2SLuiz Augusto von Dentz 	bacpy(&iso_pi(sk)->src, &hcon->src);
282f764a6c2SLuiz Augusto von Dentz 
283f764a6c2SLuiz Augusto von Dentz 	err = iso_chan_add(conn, sk, NULL);
284f764a6c2SLuiz Augusto von Dentz 	if (err)
285f764a6c2SLuiz Augusto von Dentz 		goto done;
286f764a6c2SLuiz Augusto von Dentz 
287f764a6c2SLuiz Augusto von Dentz 	if (hcon->state == BT_CONNECTED) {
288f764a6c2SLuiz Augusto von Dentz 		iso_sock_clear_timer(sk);
289f764a6c2SLuiz Augusto von Dentz 		sk->sk_state = BT_CONNECTED;
290f764a6c2SLuiz Augusto von Dentz 	} else {
291f764a6c2SLuiz Augusto von Dentz 		sk->sk_state = BT_CONNECT;
292f764a6c2SLuiz Augusto von Dentz 		iso_sock_set_timer(sk, sk->sk_sndtimeo);
293f764a6c2SLuiz Augusto von Dentz 	}
294f764a6c2SLuiz Augusto von Dentz 
295f764a6c2SLuiz Augusto von Dentz done:
296f764a6c2SLuiz Augusto von Dentz 	hci_dev_unlock(hdev);
297f764a6c2SLuiz Augusto von Dentz 	hci_dev_put(hdev);
298f764a6c2SLuiz Augusto von Dentz 	return err;
299f764a6c2SLuiz Augusto von Dentz }
300f764a6c2SLuiz Augusto von Dentz 
301f764a6c2SLuiz Augusto von Dentz static int iso_connect_cis(struct sock *sk)
302ccf74f23SLuiz Augusto von Dentz {
303ccf74f23SLuiz Augusto von Dentz 	struct iso_conn *conn;
304ccf74f23SLuiz Augusto von Dentz 	struct hci_conn *hcon;
305ccf74f23SLuiz Augusto von Dentz 	struct hci_dev  *hdev;
306ccf74f23SLuiz Augusto von Dentz 	int err;
307ccf74f23SLuiz Augusto von Dentz 
308ccf74f23SLuiz Augusto von Dentz 	BT_DBG("%pMR -> %pMR", &iso_pi(sk)->src, &iso_pi(sk)->dst);
309ccf74f23SLuiz Augusto von Dentz 
310ccf74f23SLuiz Augusto von Dentz 	hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
311ccf74f23SLuiz Augusto von Dentz 			     iso_pi(sk)->src_type);
312ccf74f23SLuiz Augusto von Dentz 	if (!hdev)
313ccf74f23SLuiz Augusto von Dentz 		return -EHOSTUNREACH;
314ccf74f23SLuiz Augusto von Dentz 
315ccf74f23SLuiz Augusto von Dentz 	hci_dev_lock(hdev);
316ccf74f23SLuiz Augusto von Dentz 
317ccf74f23SLuiz Augusto von Dentz 	if (!cis_central_capable(hdev)) {
318ccf74f23SLuiz Augusto von Dentz 		err = -EOPNOTSUPP;
319ccf74f23SLuiz Augusto von Dentz 		goto done;
320ccf74f23SLuiz Augusto von Dentz 	}
321ccf74f23SLuiz Augusto von Dentz 
322ccf74f23SLuiz Augusto von Dentz 	/* Fail if either PHYs are marked as disabled */
323ccf74f23SLuiz Augusto von Dentz 	if (!iso_pi(sk)->qos.in.phy && !iso_pi(sk)->qos.out.phy) {
324ccf74f23SLuiz Augusto von Dentz 		err = -EINVAL;
325ccf74f23SLuiz Augusto von Dentz 		goto done;
326ccf74f23SLuiz Augusto von Dentz 	}
327ccf74f23SLuiz Augusto von Dentz 
328ccf74f23SLuiz Augusto von Dentz 	/* Just bind if DEFER_SETUP has been set */
329ccf74f23SLuiz Augusto von Dentz 	if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
330ccf74f23SLuiz Augusto von Dentz 		hcon = hci_bind_cis(hdev, &iso_pi(sk)->dst,
331ccf74f23SLuiz Augusto von Dentz 				    iso_pi(sk)->dst_type, &iso_pi(sk)->qos);
332ccf74f23SLuiz Augusto von Dentz 		if (IS_ERR(hcon)) {
333ccf74f23SLuiz Augusto von Dentz 			err = PTR_ERR(hcon);
334ccf74f23SLuiz Augusto von Dentz 			goto done;
335ccf74f23SLuiz Augusto von Dentz 		}
336ccf74f23SLuiz Augusto von Dentz 	} else {
337ccf74f23SLuiz Augusto von Dentz 		hcon = hci_connect_cis(hdev, &iso_pi(sk)->dst,
338ccf74f23SLuiz Augusto von Dentz 				       iso_pi(sk)->dst_type, &iso_pi(sk)->qos);
339ccf74f23SLuiz Augusto von Dentz 		if (IS_ERR(hcon)) {
340ccf74f23SLuiz Augusto von Dentz 			err = PTR_ERR(hcon);
341ccf74f23SLuiz Augusto von Dentz 			goto done;
342ccf74f23SLuiz Augusto von Dentz 		}
343ccf74f23SLuiz Augusto von Dentz 	}
344ccf74f23SLuiz Augusto von Dentz 
345ccf74f23SLuiz Augusto von Dentz 	conn = iso_conn_add(hcon);
346ccf74f23SLuiz Augusto von Dentz 	if (!conn) {
347ccf74f23SLuiz Augusto von Dentz 		hci_conn_drop(hcon);
348ccf74f23SLuiz Augusto von Dentz 		err = -ENOMEM;
349ccf74f23SLuiz Augusto von Dentz 		goto done;
350ccf74f23SLuiz Augusto von Dentz 	}
351ccf74f23SLuiz Augusto von Dentz 
352ccf74f23SLuiz Augusto von Dentz 	/* Update source addr of the socket */
353ccf74f23SLuiz Augusto von Dentz 	bacpy(&iso_pi(sk)->src, &hcon->src);
354ccf74f23SLuiz Augusto von Dentz 
355ccf74f23SLuiz Augusto von Dentz 	err = iso_chan_add(conn, sk, NULL);
356ccf74f23SLuiz Augusto von Dentz 	if (err)
357ccf74f23SLuiz Augusto von Dentz 		goto done;
358ccf74f23SLuiz Augusto von Dentz 
359ccf74f23SLuiz Augusto von Dentz 	if (hcon->state == BT_CONNECTED) {
360ccf74f23SLuiz Augusto von Dentz 		iso_sock_clear_timer(sk);
361ccf74f23SLuiz Augusto von Dentz 		sk->sk_state = BT_CONNECTED;
362ccf74f23SLuiz Augusto von Dentz 	} else if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
363ccf74f23SLuiz Augusto von Dentz 		iso_sock_clear_timer(sk);
364ccf74f23SLuiz Augusto von Dentz 		sk->sk_state = BT_CONNECT;
365ccf74f23SLuiz Augusto von Dentz 	} else {
366ccf74f23SLuiz Augusto von Dentz 		sk->sk_state = BT_CONNECT;
367ccf74f23SLuiz Augusto von Dentz 		iso_sock_set_timer(sk, sk->sk_sndtimeo);
368ccf74f23SLuiz Augusto von Dentz 	}
369ccf74f23SLuiz Augusto von Dentz 
370ccf74f23SLuiz Augusto von Dentz done:
371ccf74f23SLuiz Augusto von Dentz 	hci_dev_unlock(hdev);
372ccf74f23SLuiz Augusto von Dentz 	hci_dev_put(hdev);
373ccf74f23SLuiz Augusto von Dentz 	return err;
374ccf74f23SLuiz Augusto von Dentz }
375ccf74f23SLuiz Augusto von Dentz 
376ccf74f23SLuiz Augusto von Dentz static int iso_send_frame(struct sock *sk, struct sk_buff *skb)
377ccf74f23SLuiz Augusto von Dentz {
378ccf74f23SLuiz Augusto von Dentz 	struct iso_conn *conn = iso_pi(sk)->conn;
379ccf74f23SLuiz Augusto von Dentz 	struct hci_iso_data_hdr *hdr;
380ccf74f23SLuiz Augusto von Dentz 	int len = 0;
381ccf74f23SLuiz Augusto von Dentz 
382ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p len %d", sk, skb->len);
383ccf74f23SLuiz Augusto von Dentz 
384ccf74f23SLuiz Augusto von Dentz 	if (skb->len > iso_pi(sk)->qos.out.sdu)
385ccf74f23SLuiz Augusto von Dentz 		return -EMSGSIZE;
386ccf74f23SLuiz Augusto von Dentz 
387ccf74f23SLuiz Augusto von Dentz 	len = skb->len;
388ccf74f23SLuiz Augusto von Dentz 
389ccf74f23SLuiz Augusto von Dentz 	/* Push ISO data header */
390ccf74f23SLuiz Augusto von Dentz 	hdr = skb_push(skb, HCI_ISO_DATA_HDR_SIZE);
391ccf74f23SLuiz Augusto von Dentz 	hdr->sn = cpu_to_le16(conn->tx_sn++);
392ccf74f23SLuiz Augusto von Dentz 	hdr->slen = cpu_to_le16(hci_iso_data_len_pack(len,
393ccf74f23SLuiz Augusto von Dentz 						      HCI_ISO_STATUS_VALID));
394ccf74f23SLuiz Augusto von Dentz 
395ccf74f23SLuiz Augusto von Dentz 	if (sk->sk_state == BT_CONNECTED)
396ccf74f23SLuiz Augusto von Dentz 		hci_send_iso(conn->hcon, skb);
397ccf74f23SLuiz Augusto von Dentz 	else
398ccf74f23SLuiz Augusto von Dentz 		len = -ENOTCONN;
399ccf74f23SLuiz Augusto von Dentz 
400ccf74f23SLuiz Augusto von Dentz 	return len;
401ccf74f23SLuiz Augusto von Dentz }
402ccf74f23SLuiz Augusto von Dentz 
403ccf74f23SLuiz Augusto von Dentz static void iso_recv_frame(struct iso_conn *conn, struct sk_buff *skb)
404ccf74f23SLuiz Augusto von Dentz {
405ccf74f23SLuiz Augusto von Dentz 	struct sock *sk;
406ccf74f23SLuiz Augusto von Dentz 
407ccf74f23SLuiz Augusto von Dentz 	iso_conn_lock(conn);
408ccf74f23SLuiz Augusto von Dentz 	sk = conn->sk;
409ccf74f23SLuiz Augusto von Dentz 	iso_conn_unlock(conn);
410ccf74f23SLuiz Augusto von Dentz 
411ccf74f23SLuiz Augusto von Dentz 	if (!sk)
412ccf74f23SLuiz Augusto von Dentz 		goto drop;
413ccf74f23SLuiz Augusto von Dentz 
414ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p len %d", sk, skb->len);
415ccf74f23SLuiz Augusto von Dentz 
416ccf74f23SLuiz Augusto von Dentz 	if (sk->sk_state != BT_CONNECTED)
417ccf74f23SLuiz Augusto von Dentz 		goto drop;
418ccf74f23SLuiz Augusto von Dentz 
419ccf74f23SLuiz Augusto von Dentz 	if (!sock_queue_rcv_skb(sk, skb))
420ccf74f23SLuiz Augusto von Dentz 		return;
421ccf74f23SLuiz Augusto von Dentz 
422ccf74f23SLuiz Augusto von Dentz drop:
423ccf74f23SLuiz Augusto von Dentz 	kfree_skb(skb);
424ccf74f23SLuiz Augusto von Dentz }
425ccf74f23SLuiz Augusto von Dentz 
426ccf74f23SLuiz Augusto von Dentz /* -------- Socket interface ---------- */
427ccf74f23SLuiz Augusto von Dentz static struct sock *__iso_get_sock_listen_by_addr(bdaddr_t *ba)
428ccf74f23SLuiz Augusto von Dentz {
429ccf74f23SLuiz Augusto von Dentz 	struct sock *sk;
430ccf74f23SLuiz Augusto von Dentz 
431ccf74f23SLuiz Augusto von Dentz 	sk_for_each(sk, &iso_sk_list.head) {
432ccf74f23SLuiz Augusto von Dentz 		if (sk->sk_state != BT_LISTEN)
433ccf74f23SLuiz Augusto von Dentz 			continue;
434ccf74f23SLuiz Augusto von Dentz 
435ccf74f23SLuiz Augusto von Dentz 		if (!bacmp(&iso_pi(sk)->src, ba))
436ccf74f23SLuiz Augusto von Dentz 			return sk;
437ccf74f23SLuiz Augusto von Dentz 	}
438ccf74f23SLuiz Augusto von Dentz 
439ccf74f23SLuiz Augusto von Dentz 	return NULL;
440ccf74f23SLuiz Augusto von Dentz }
441ccf74f23SLuiz Augusto von Dentz 
442f764a6c2SLuiz Augusto von Dentz static struct sock *__iso_get_sock_listen_by_sid(bdaddr_t *ba, bdaddr_t *bc,
443f764a6c2SLuiz Augusto von Dentz 						 __u8 sid)
444f764a6c2SLuiz Augusto von Dentz {
445f764a6c2SLuiz Augusto von Dentz 	struct sock *sk;
446f764a6c2SLuiz Augusto von Dentz 
447f764a6c2SLuiz Augusto von Dentz 	sk_for_each(sk, &iso_sk_list.head) {
448f764a6c2SLuiz Augusto von Dentz 		if (sk->sk_state != BT_LISTEN)
449f764a6c2SLuiz Augusto von Dentz 			continue;
450f764a6c2SLuiz Augusto von Dentz 
451f764a6c2SLuiz Augusto von Dentz 		if (bacmp(&iso_pi(sk)->src, ba))
452f764a6c2SLuiz Augusto von Dentz 			continue;
453f764a6c2SLuiz Augusto von Dentz 
454f764a6c2SLuiz Augusto von Dentz 		if (bacmp(&iso_pi(sk)->dst, bc))
455f764a6c2SLuiz Augusto von Dentz 			continue;
456f764a6c2SLuiz Augusto von Dentz 
457f764a6c2SLuiz Augusto von Dentz 		if (iso_pi(sk)->bc_sid == sid)
458f764a6c2SLuiz Augusto von Dentz 			return sk;
459f764a6c2SLuiz Augusto von Dentz 	}
460f764a6c2SLuiz Augusto von Dentz 
461f764a6c2SLuiz Augusto von Dentz 	return NULL;
462f764a6c2SLuiz Augusto von Dentz }
463f764a6c2SLuiz Augusto von Dentz 
464f764a6c2SLuiz Augusto von Dentz typedef bool (*iso_sock_match_t)(struct sock *sk, void *data);
465f764a6c2SLuiz Augusto von Dentz 
466f764a6c2SLuiz Augusto von Dentz /* Find socket listening:
467f764a6c2SLuiz Augusto von Dentz  * source bdaddr (Unicast)
468f764a6c2SLuiz Augusto von Dentz  * destination bdaddr (Broadcast only)
469f764a6c2SLuiz Augusto von Dentz  * match func - pass NULL to ignore
470f764a6c2SLuiz Augusto von Dentz  * match func data - pass -1 to ignore
471ccf74f23SLuiz Augusto von Dentz  * Returns closest match.
472ccf74f23SLuiz Augusto von Dentz  */
473f764a6c2SLuiz Augusto von Dentz static struct sock *iso_get_sock_listen(bdaddr_t *src, bdaddr_t *dst,
474f764a6c2SLuiz Augusto von Dentz 					iso_sock_match_t match, void *data)
475ccf74f23SLuiz Augusto von Dentz {
476ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = NULL, *sk1 = NULL;
477ccf74f23SLuiz Augusto von Dentz 
478ccf74f23SLuiz Augusto von Dentz 	read_lock(&iso_sk_list.lock);
479ccf74f23SLuiz Augusto von Dentz 
480ccf74f23SLuiz Augusto von Dentz 	sk_for_each(sk, &iso_sk_list.head) {
481ccf74f23SLuiz Augusto von Dentz 		if (sk->sk_state != BT_LISTEN)
482ccf74f23SLuiz Augusto von Dentz 			continue;
483ccf74f23SLuiz Augusto von Dentz 
484f764a6c2SLuiz Augusto von Dentz 		/* Match Broadcast destination */
485f764a6c2SLuiz Augusto von Dentz 		if (bacmp(dst, BDADDR_ANY) && bacmp(&iso_pi(sk)->dst, dst))
486f764a6c2SLuiz Augusto von Dentz 			continue;
487f764a6c2SLuiz Augusto von Dentz 
488f764a6c2SLuiz Augusto von Dentz 		/* Use Match function if provided */
489f764a6c2SLuiz Augusto von Dentz 		if (match && !match(sk, data))
490f764a6c2SLuiz Augusto von Dentz 			continue;
491f764a6c2SLuiz Augusto von Dentz 
492ccf74f23SLuiz Augusto von Dentz 		/* Exact match. */
493ccf74f23SLuiz Augusto von Dentz 		if (!bacmp(&iso_pi(sk)->src, src))
494ccf74f23SLuiz Augusto von Dentz 			break;
495ccf74f23SLuiz Augusto von Dentz 
496ccf74f23SLuiz Augusto von Dentz 		/* Closest match */
497ccf74f23SLuiz Augusto von Dentz 		if (!bacmp(&iso_pi(sk)->src, BDADDR_ANY))
498ccf74f23SLuiz Augusto von Dentz 			sk1 = sk;
499ccf74f23SLuiz Augusto von Dentz 	}
500ccf74f23SLuiz Augusto von Dentz 
501ccf74f23SLuiz Augusto von Dentz 	read_unlock(&iso_sk_list.lock);
502ccf74f23SLuiz Augusto von Dentz 
503ccf74f23SLuiz Augusto von Dentz 	return sk ? sk : sk1;
504ccf74f23SLuiz Augusto von Dentz }
505ccf74f23SLuiz Augusto von Dentz 
506ccf74f23SLuiz Augusto von Dentz static void iso_sock_destruct(struct sock *sk)
507ccf74f23SLuiz Augusto von Dentz {
508ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p", sk);
509ccf74f23SLuiz Augusto von Dentz 
510ccf74f23SLuiz Augusto von Dentz 	skb_queue_purge(&sk->sk_receive_queue);
511ccf74f23SLuiz Augusto von Dentz 	skb_queue_purge(&sk->sk_write_queue);
512ccf74f23SLuiz Augusto von Dentz }
513ccf74f23SLuiz Augusto von Dentz 
514ccf74f23SLuiz Augusto von Dentz static void iso_sock_cleanup_listen(struct sock *parent)
515ccf74f23SLuiz Augusto von Dentz {
516ccf74f23SLuiz Augusto von Dentz 	struct sock *sk;
517ccf74f23SLuiz Augusto von Dentz 
518ccf74f23SLuiz Augusto von Dentz 	BT_DBG("parent %p", parent);
519ccf74f23SLuiz Augusto von Dentz 
520ccf74f23SLuiz Augusto von Dentz 	/* Close not yet accepted channels */
521ccf74f23SLuiz Augusto von Dentz 	while ((sk = bt_accept_dequeue(parent, NULL))) {
522ccf74f23SLuiz Augusto von Dentz 		iso_sock_close(sk);
523ccf74f23SLuiz Augusto von Dentz 		iso_sock_kill(sk);
524ccf74f23SLuiz Augusto von Dentz 	}
525ccf74f23SLuiz Augusto von Dentz 
526ccf74f23SLuiz Augusto von Dentz 	parent->sk_state  = BT_CLOSED;
527ccf74f23SLuiz Augusto von Dentz 	sock_set_flag(parent, SOCK_ZAPPED);
528ccf74f23SLuiz Augusto von Dentz }
529ccf74f23SLuiz Augusto von Dentz 
530ccf74f23SLuiz Augusto von Dentz /* Kill socket (only if zapped and orphan)
531ccf74f23SLuiz Augusto von Dentz  * Must be called on unlocked socket.
532ccf74f23SLuiz Augusto von Dentz  */
533ccf74f23SLuiz Augusto von Dentz static void iso_sock_kill(struct sock *sk)
534ccf74f23SLuiz Augusto von Dentz {
535ccf74f23SLuiz Augusto von Dentz 	if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket ||
536ccf74f23SLuiz Augusto von Dentz 	    sock_flag(sk, SOCK_DEAD))
537ccf74f23SLuiz Augusto von Dentz 		return;
538ccf74f23SLuiz Augusto von Dentz 
539ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p state %d", sk, sk->sk_state);
540ccf74f23SLuiz Augusto von Dentz 
541ccf74f23SLuiz Augusto von Dentz 	/* Kill poor orphan */
542ccf74f23SLuiz Augusto von Dentz 	bt_sock_unlink(&iso_sk_list, sk);
543ccf74f23SLuiz Augusto von Dentz 	sock_set_flag(sk, SOCK_DEAD);
544ccf74f23SLuiz Augusto von Dentz 	sock_put(sk);
545ccf74f23SLuiz Augusto von Dentz }
546ccf74f23SLuiz Augusto von Dentz 
547ccf74f23SLuiz Augusto von Dentz static void iso_conn_defer_reject(struct hci_conn *conn)
548ccf74f23SLuiz Augusto von Dentz {
549ccf74f23SLuiz Augusto von Dentz 	struct hci_cp_le_reject_cis cp;
550ccf74f23SLuiz Augusto von Dentz 
551ccf74f23SLuiz Augusto von Dentz 	BT_DBG("conn %p", conn);
552ccf74f23SLuiz Augusto von Dentz 
553ccf74f23SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
554ccf74f23SLuiz Augusto von Dentz 	cp.handle = cpu_to_le16(conn->handle);
555ccf74f23SLuiz Augusto von Dentz 	cp.reason = HCI_ERROR_REJ_BAD_ADDR;
556ccf74f23SLuiz Augusto von Dentz 	hci_send_cmd(conn->hdev, HCI_OP_LE_REJECT_CIS, sizeof(cp), &cp);
557ccf74f23SLuiz Augusto von Dentz }
558ccf74f23SLuiz Augusto von Dentz 
559ccf74f23SLuiz Augusto von Dentz static void __iso_sock_close(struct sock *sk)
560ccf74f23SLuiz Augusto von Dentz {
561ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
562ccf74f23SLuiz Augusto von Dentz 
563ccf74f23SLuiz Augusto von Dentz 	switch (sk->sk_state) {
564ccf74f23SLuiz Augusto von Dentz 	case BT_LISTEN:
565ccf74f23SLuiz Augusto von Dentz 		iso_sock_cleanup_listen(sk);
566ccf74f23SLuiz Augusto von Dentz 		break;
567ccf74f23SLuiz Augusto von Dentz 
568ccf74f23SLuiz Augusto von Dentz 	case BT_CONNECTED:
569ccf74f23SLuiz Augusto von Dentz 	case BT_CONFIG:
570ccf74f23SLuiz Augusto von Dentz 		if (iso_pi(sk)->conn->hcon) {
571ccf74f23SLuiz Augusto von Dentz 			sk->sk_state = BT_DISCONN;
572ccf74f23SLuiz Augusto von Dentz 			iso_sock_set_timer(sk, ISO_DISCONN_TIMEOUT);
573ccf74f23SLuiz Augusto von Dentz 			iso_conn_lock(iso_pi(sk)->conn);
574ccf74f23SLuiz Augusto von Dentz 			hci_conn_drop(iso_pi(sk)->conn->hcon);
575ccf74f23SLuiz Augusto von Dentz 			iso_pi(sk)->conn->hcon = NULL;
576ccf74f23SLuiz Augusto von Dentz 			iso_conn_unlock(iso_pi(sk)->conn);
577ccf74f23SLuiz Augusto von Dentz 		} else {
578ccf74f23SLuiz Augusto von Dentz 			iso_chan_del(sk, ECONNRESET);
579ccf74f23SLuiz Augusto von Dentz 		}
580ccf74f23SLuiz Augusto von Dentz 		break;
581ccf74f23SLuiz Augusto von Dentz 
582ccf74f23SLuiz Augusto von Dentz 	case BT_CONNECT2:
583ccf74f23SLuiz Augusto von Dentz 		if (iso_pi(sk)->conn->hcon)
584ccf74f23SLuiz Augusto von Dentz 			iso_conn_defer_reject(iso_pi(sk)->conn->hcon);
585ccf74f23SLuiz Augusto von Dentz 		iso_chan_del(sk, ECONNRESET);
586ccf74f23SLuiz Augusto von Dentz 		break;
587ccf74f23SLuiz Augusto von Dentz 	case BT_CONNECT:
588ccf74f23SLuiz Augusto von Dentz 		/* In case of DEFER_SETUP the hcon would be bound to CIG which
589ccf74f23SLuiz Augusto von Dentz 		 * needs to be removed so just call hci_conn_del so the cleanup
590ccf74f23SLuiz Augusto von Dentz 		 * callback do what is needed.
591ccf74f23SLuiz Augusto von Dentz 		 */
592ccf74f23SLuiz Augusto von Dentz 		if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags) &&
593ccf74f23SLuiz Augusto von Dentz 		    iso_pi(sk)->conn->hcon) {
594ccf74f23SLuiz Augusto von Dentz 			hci_conn_del(iso_pi(sk)->conn->hcon);
595ccf74f23SLuiz Augusto von Dentz 			iso_pi(sk)->conn->hcon = NULL;
596ccf74f23SLuiz Augusto von Dentz 		}
597ccf74f23SLuiz Augusto von Dentz 
598ccf74f23SLuiz Augusto von Dentz 		iso_chan_del(sk, ECONNRESET);
599ccf74f23SLuiz Augusto von Dentz 		break;
600ccf74f23SLuiz Augusto von Dentz 	case BT_DISCONN:
601ccf74f23SLuiz Augusto von Dentz 		iso_chan_del(sk, ECONNRESET);
602ccf74f23SLuiz Augusto von Dentz 		break;
603ccf74f23SLuiz Augusto von Dentz 
604ccf74f23SLuiz Augusto von Dentz 	default:
605ccf74f23SLuiz Augusto von Dentz 		sock_set_flag(sk, SOCK_ZAPPED);
606ccf74f23SLuiz Augusto von Dentz 		break;
607ccf74f23SLuiz Augusto von Dentz 	}
608ccf74f23SLuiz Augusto von Dentz }
609ccf74f23SLuiz Augusto von Dentz 
610ccf74f23SLuiz Augusto von Dentz /* Must be called on unlocked socket. */
611ccf74f23SLuiz Augusto von Dentz static void iso_sock_close(struct sock *sk)
612ccf74f23SLuiz Augusto von Dentz {
613ccf74f23SLuiz Augusto von Dentz 	iso_sock_clear_timer(sk);
614ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
615ccf74f23SLuiz Augusto von Dentz 	__iso_sock_close(sk);
616ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
617ccf74f23SLuiz Augusto von Dentz 	iso_sock_kill(sk);
618ccf74f23SLuiz Augusto von Dentz }
619ccf74f23SLuiz Augusto von Dentz 
620ccf74f23SLuiz Augusto von Dentz static void iso_sock_init(struct sock *sk, struct sock *parent)
621ccf74f23SLuiz Augusto von Dentz {
622ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p", sk);
623ccf74f23SLuiz Augusto von Dentz 
624ccf74f23SLuiz Augusto von Dentz 	if (parent) {
625ccf74f23SLuiz Augusto von Dentz 		sk->sk_type = parent->sk_type;
626ccf74f23SLuiz Augusto von Dentz 		bt_sk(sk)->flags = bt_sk(parent)->flags;
627ccf74f23SLuiz Augusto von Dentz 		security_sk_clone(parent, sk);
628ccf74f23SLuiz Augusto von Dentz 	}
629ccf74f23SLuiz Augusto von Dentz }
630ccf74f23SLuiz Augusto von Dentz 
631ccf74f23SLuiz Augusto von Dentz static struct proto iso_proto = {
632ccf74f23SLuiz Augusto von Dentz 	.name		= "ISO",
633ccf74f23SLuiz Augusto von Dentz 	.owner		= THIS_MODULE,
634ccf74f23SLuiz Augusto von Dentz 	.obj_size	= sizeof(struct iso_pinfo)
635ccf74f23SLuiz Augusto von Dentz };
636ccf74f23SLuiz Augusto von Dentz 
637ccf74f23SLuiz Augusto von Dentz #define DEFAULT_IO_QOS \
638ccf74f23SLuiz Augusto von Dentz { \
639ccf74f23SLuiz Augusto von Dentz 	.interval	= 10000u, \
640ccf74f23SLuiz Augusto von Dentz 	.latency	= 10u, \
641ccf74f23SLuiz Augusto von Dentz 	.sdu		= 40u, \
642ccf74f23SLuiz Augusto von Dentz 	.phy		= BT_ISO_PHY_2M, \
643ccf74f23SLuiz Augusto von Dentz 	.rtn		= 2u, \
644ccf74f23SLuiz Augusto von Dentz }
645ccf74f23SLuiz Augusto von Dentz 
646ccf74f23SLuiz Augusto von Dentz static struct bt_iso_qos default_qos = {
647ccf74f23SLuiz Augusto von Dentz 	.cig		= BT_ISO_QOS_CIG_UNSET,
648ccf74f23SLuiz Augusto von Dentz 	.cis		= BT_ISO_QOS_CIS_UNSET,
649ccf74f23SLuiz Augusto von Dentz 	.sca		= 0x00,
650ccf74f23SLuiz Augusto von Dentz 	.packing	= 0x00,
651ccf74f23SLuiz Augusto von Dentz 	.framing	= 0x00,
652ccf74f23SLuiz Augusto von Dentz 	.in		= DEFAULT_IO_QOS,
653ccf74f23SLuiz Augusto von Dentz 	.out		= DEFAULT_IO_QOS,
654ccf74f23SLuiz Augusto von Dentz };
655ccf74f23SLuiz Augusto von Dentz 
656ccf74f23SLuiz Augusto von Dentz static struct sock *iso_sock_alloc(struct net *net, struct socket *sock,
657ccf74f23SLuiz Augusto von Dentz 				   int proto, gfp_t prio, int kern)
658ccf74f23SLuiz Augusto von Dentz {
659ccf74f23SLuiz Augusto von Dentz 	struct sock *sk;
660ccf74f23SLuiz Augusto von Dentz 
661ccf74f23SLuiz Augusto von Dentz 	sk = sk_alloc(net, PF_BLUETOOTH, prio, &iso_proto, kern);
662ccf74f23SLuiz Augusto von Dentz 	if (!sk)
663ccf74f23SLuiz Augusto von Dentz 		return NULL;
664ccf74f23SLuiz Augusto von Dentz 
665ccf74f23SLuiz Augusto von Dentz 	sock_init_data(sock, sk);
666ccf74f23SLuiz Augusto von Dentz 	INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
667ccf74f23SLuiz Augusto von Dentz 
668ccf74f23SLuiz Augusto von Dentz 	sk->sk_destruct = iso_sock_destruct;
669ccf74f23SLuiz Augusto von Dentz 	sk->sk_sndtimeo = ISO_CONN_TIMEOUT;
670ccf74f23SLuiz Augusto von Dentz 
671ccf74f23SLuiz Augusto von Dentz 	sock_reset_flag(sk, SOCK_ZAPPED);
672ccf74f23SLuiz Augusto von Dentz 
673ccf74f23SLuiz Augusto von Dentz 	sk->sk_protocol = proto;
674ccf74f23SLuiz Augusto von Dentz 	sk->sk_state    = BT_OPEN;
675ccf74f23SLuiz Augusto von Dentz 
676ccf74f23SLuiz Augusto von Dentz 	/* Set address type as public as default src address is BDADDR_ANY */
677ccf74f23SLuiz Augusto von Dentz 	iso_pi(sk)->src_type = BDADDR_LE_PUBLIC;
678ccf74f23SLuiz Augusto von Dentz 
679ccf74f23SLuiz Augusto von Dentz 	iso_pi(sk)->qos = default_qos;
680ccf74f23SLuiz Augusto von Dentz 
681ccf74f23SLuiz Augusto von Dentz 	bt_sock_link(&iso_sk_list, sk);
682ccf74f23SLuiz Augusto von Dentz 	return sk;
683ccf74f23SLuiz Augusto von Dentz }
684ccf74f23SLuiz Augusto von Dentz 
685ccf74f23SLuiz Augusto von Dentz static int iso_sock_create(struct net *net, struct socket *sock, int protocol,
686ccf74f23SLuiz Augusto von Dentz 			   int kern)
687ccf74f23SLuiz Augusto von Dentz {
688ccf74f23SLuiz Augusto von Dentz 	struct sock *sk;
689ccf74f23SLuiz Augusto von Dentz 
690ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sock %p", sock);
691ccf74f23SLuiz Augusto von Dentz 
692ccf74f23SLuiz Augusto von Dentz 	sock->state = SS_UNCONNECTED;
693ccf74f23SLuiz Augusto von Dentz 
694ccf74f23SLuiz Augusto von Dentz 	if (sock->type != SOCK_SEQPACKET)
695ccf74f23SLuiz Augusto von Dentz 		return -ESOCKTNOSUPPORT;
696ccf74f23SLuiz Augusto von Dentz 
697ccf74f23SLuiz Augusto von Dentz 	sock->ops = &iso_sock_ops;
698ccf74f23SLuiz Augusto von Dentz 
699ccf74f23SLuiz Augusto von Dentz 	sk = iso_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern);
700ccf74f23SLuiz Augusto von Dentz 	if (!sk)
701ccf74f23SLuiz Augusto von Dentz 		return -ENOMEM;
702ccf74f23SLuiz Augusto von Dentz 
703ccf74f23SLuiz Augusto von Dentz 	iso_sock_init(sk, NULL);
704ccf74f23SLuiz Augusto von Dentz 	return 0;
705ccf74f23SLuiz Augusto von Dentz }
706ccf74f23SLuiz Augusto von Dentz 
707f764a6c2SLuiz Augusto von Dentz static int iso_sock_bind_bc(struct socket *sock, struct sockaddr *addr,
708f764a6c2SLuiz Augusto von Dentz 			    int addr_len)
709f764a6c2SLuiz Augusto von Dentz {
710f764a6c2SLuiz Augusto von Dentz 	struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
711f764a6c2SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
712f764a6c2SLuiz Augusto von Dentz 	int i;
713f764a6c2SLuiz Augusto von Dentz 
714f764a6c2SLuiz Augusto von Dentz 	BT_DBG("sk %p bc_sid %u bc_num_bis %u", sk, sa->iso_bc->bc_sid,
715f764a6c2SLuiz Augusto von Dentz 	       sa->iso_bc->bc_num_bis);
716f764a6c2SLuiz Augusto von Dentz 
717f764a6c2SLuiz Augusto von Dentz 	if (addr_len > sizeof(*sa) + sizeof(*sa->iso_bc) ||
718f764a6c2SLuiz Augusto von Dentz 	    sa->iso_bc->bc_num_bis < 0x01 || sa->iso_bc->bc_num_bis > 0x1f)
719f764a6c2SLuiz Augusto von Dentz 		return -EINVAL;
720f764a6c2SLuiz Augusto von Dentz 
721f764a6c2SLuiz Augusto von Dentz 	bacpy(&iso_pi(sk)->dst, &sa->iso_bc->bc_bdaddr);
722f764a6c2SLuiz Augusto von Dentz 	iso_pi(sk)->dst_type = sa->iso_bc->bc_bdaddr_type;
723f764a6c2SLuiz Augusto von Dentz 	iso_pi(sk)->sync_handle = -1;
724f764a6c2SLuiz Augusto von Dentz 	iso_pi(sk)->bc_sid = sa->iso_bc->bc_sid;
725f764a6c2SLuiz Augusto von Dentz 	iso_pi(sk)->bc_num_bis = sa->iso_bc->bc_num_bis;
726f764a6c2SLuiz Augusto von Dentz 
727f764a6c2SLuiz Augusto von Dentz 	for (i = 0; i < iso_pi(sk)->bc_num_bis; i++) {
728f764a6c2SLuiz Augusto von Dentz 		if (sa->iso_bc->bc_bis[i] < 0x01 ||
729f764a6c2SLuiz Augusto von Dentz 		    sa->iso_bc->bc_bis[i] > 0x1f)
730f764a6c2SLuiz Augusto von Dentz 			return -EINVAL;
731f764a6c2SLuiz Augusto von Dentz 
732f764a6c2SLuiz Augusto von Dentz 		memcpy(iso_pi(sk)->bc_bis, sa->iso_bc->bc_bis,
733f764a6c2SLuiz Augusto von Dentz 		       iso_pi(sk)->bc_num_bis);
734f764a6c2SLuiz Augusto von Dentz 	}
735f764a6c2SLuiz Augusto von Dentz 
736f764a6c2SLuiz Augusto von Dentz 	return 0;
737f764a6c2SLuiz Augusto von Dentz }
738f764a6c2SLuiz Augusto von Dentz 
739ccf74f23SLuiz Augusto von Dentz static int iso_sock_bind(struct socket *sock, struct sockaddr *addr,
740ccf74f23SLuiz Augusto von Dentz 			 int addr_len)
741ccf74f23SLuiz Augusto von Dentz {
742ccf74f23SLuiz Augusto von Dentz 	struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
743ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
744ccf74f23SLuiz Augusto von Dentz 	int err = 0;
745ccf74f23SLuiz Augusto von Dentz 
746ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p %pMR type %u", sk, &sa->iso_bdaddr, sa->iso_bdaddr_type);
747ccf74f23SLuiz Augusto von Dentz 
748ccf74f23SLuiz Augusto von Dentz 	if (!addr || addr_len < sizeof(struct sockaddr_iso) ||
749ccf74f23SLuiz Augusto von Dentz 	    addr->sa_family != AF_BLUETOOTH)
750ccf74f23SLuiz Augusto von Dentz 		return -EINVAL;
751ccf74f23SLuiz Augusto von Dentz 
752ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
753ccf74f23SLuiz Augusto von Dentz 
754ccf74f23SLuiz Augusto von Dentz 	if (sk->sk_state != BT_OPEN) {
755ccf74f23SLuiz Augusto von Dentz 		err = -EBADFD;
756ccf74f23SLuiz Augusto von Dentz 		goto done;
757ccf74f23SLuiz Augusto von Dentz 	}
758ccf74f23SLuiz Augusto von Dentz 
759ccf74f23SLuiz Augusto von Dentz 	if (sk->sk_type != SOCK_SEQPACKET) {
760ccf74f23SLuiz Augusto von Dentz 		err = -EINVAL;
761ccf74f23SLuiz Augusto von Dentz 		goto done;
762ccf74f23SLuiz Augusto von Dentz 	}
763ccf74f23SLuiz Augusto von Dentz 
764ccf74f23SLuiz Augusto von Dentz 	/* Check if the address type is of LE type */
765ccf74f23SLuiz Augusto von Dentz 	if (!bdaddr_type_is_le(sa->iso_bdaddr_type)) {
766ccf74f23SLuiz Augusto von Dentz 		err = -EINVAL;
767ccf74f23SLuiz Augusto von Dentz 		goto done;
768ccf74f23SLuiz Augusto von Dentz 	}
769ccf74f23SLuiz Augusto von Dentz 
770ccf74f23SLuiz Augusto von Dentz 	bacpy(&iso_pi(sk)->src, &sa->iso_bdaddr);
771ccf74f23SLuiz Augusto von Dentz 	iso_pi(sk)->src_type = sa->iso_bdaddr_type;
772ccf74f23SLuiz Augusto von Dentz 
773f764a6c2SLuiz Augusto von Dentz 	/* Check for Broadcast address */
774f764a6c2SLuiz Augusto von Dentz 	if (addr_len > sizeof(*sa)) {
775f764a6c2SLuiz Augusto von Dentz 		err = iso_sock_bind_bc(sock, addr, addr_len);
776f764a6c2SLuiz Augusto von Dentz 		if (err)
777f764a6c2SLuiz Augusto von Dentz 			goto done;
778f764a6c2SLuiz Augusto von Dentz 	}
779f764a6c2SLuiz Augusto von Dentz 
780ccf74f23SLuiz Augusto von Dentz 	sk->sk_state = BT_BOUND;
781ccf74f23SLuiz Augusto von Dentz 
782ccf74f23SLuiz Augusto von Dentz done:
783ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
784ccf74f23SLuiz Augusto von Dentz 	return err;
785ccf74f23SLuiz Augusto von Dentz }
786ccf74f23SLuiz Augusto von Dentz 
787ccf74f23SLuiz Augusto von Dentz static int iso_sock_connect(struct socket *sock, struct sockaddr *addr,
788ccf74f23SLuiz Augusto von Dentz 			    int alen, int flags)
789ccf74f23SLuiz Augusto von Dentz {
790ccf74f23SLuiz Augusto von Dentz 	struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
791ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
792ccf74f23SLuiz Augusto von Dentz 	int err;
793ccf74f23SLuiz Augusto von Dentz 
794ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p", sk);
795ccf74f23SLuiz Augusto von Dentz 
796ccf74f23SLuiz Augusto von Dentz 	if (alen < sizeof(struct sockaddr_iso) ||
797ccf74f23SLuiz Augusto von Dentz 	    addr->sa_family != AF_BLUETOOTH)
798ccf74f23SLuiz Augusto von Dentz 		return -EINVAL;
799ccf74f23SLuiz Augusto von Dentz 
800ccf74f23SLuiz Augusto von Dentz 	if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
801ccf74f23SLuiz Augusto von Dentz 		return -EBADFD;
802ccf74f23SLuiz Augusto von Dentz 
803ccf74f23SLuiz Augusto von Dentz 	if (sk->sk_type != SOCK_SEQPACKET)
804ccf74f23SLuiz Augusto von Dentz 		return -EINVAL;
805ccf74f23SLuiz Augusto von Dentz 
806ccf74f23SLuiz Augusto von Dentz 	/* Check if the address type is of LE type */
807ccf74f23SLuiz Augusto von Dentz 	if (!bdaddr_type_is_le(sa->iso_bdaddr_type))
808ccf74f23SLuiz Augusto von Dentz 		return -EINVAL;
809ccf74f23SLuiz Augusto von Dentz 
810ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
811ccf74f23SLuiz Augusto von Dentz 
812ccf74f23SLuiz Augusto von Dentz 	bacpy(&iso_pi(sk)->dst, &sa->iso_bdaddr);
813ccf74f23SLuiz Augusto von Dentz 	iso_pi(sk)->dst_type = sa->iso_bdaddr_type;
814ccf74f23SLuiz Augusto von Dentz 
815f764a6c2SLuiz Augusto von Dentz 	if (bacmp(&iso_pi(sk)->dst, BDADDR_ANY))
816f764a6c2SLuiz Augusto von Dentz 		err = iso_connect_cis(sk);
817f764a6c2SLuiz Augusto von Dentz 	else
818f764a6c2SLuiz Augusto von Dentz 		err = iso_connect_bis(sk);
819f764a6c2SLuiz Augusto von Dentz 
820ccf74f23SLuiz Augusto von Dentz 	if (err)
821ccf74f23SLuiz Augusto von Dentz 		goto done;
822ccf74f23SLuiz Augusto von Dentz 
823ccf74f23SLuiz Augusto von Dentz 	if (!test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
824ccf74f23SLuiz Augusto von Dentz 		err = bt_sock_wait_state(sk, BT_CONNECTED,
825ccf74f23SLuiz Augusto von Dentz 					 sock_sndtimeo(sk, flags & O_NONBLOCK));
826ccf74f23SLuiz Augusto von Dentz 	}
827ccf74f23SLuiz Augusto von Dentz 
828ccf74f23SLuiz Augusto von Dentz done:
829ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
830ccf74f23SLuiz Augusto von Dentz 	return err;
831ccf74f23SLuiz Augusto von Dentz }
832ccf74f23SLuiz Augusto von Dentz 
833f764a6c2SLuiz Augusto von Dentz static int iso_listen_bis(struct sock *sk)
834f764a6c2SLuiz Augusto von Dentz {
835f764a6c2SLuiz Augusto von Dentz 	struct hci_dev *hdev;
836f764a6c2SLuiz Augusto von Dentz 	int err = 0;
837f764a6c2SLuiz Augusto von Dentz 
838f764a6c2SLuiz Augusto von Dentz 	BT_DBG("%pMR -> %pMR (SID 0x%2.2x)", &iso_pi(sk)->src,
839f764a6c2SLuiz Augusto von Dentz 	       &iso_pi(sk)->dst, iso_pi(sk)->bc_sid);
840f764a6c2SLuiz Augusto von Dentz 
841f764a6c2SLuiz Augusto von Dentz 	write_lock(&iso_sk_list.lock);
842f764a6c2SLuiz Augusto von Dentz 
843f764a6c2SLuiz Augusto von Dentz 	if (__iso_get_sock_listen_by_sid(&iso_pi(sk)->src, &iso_pi(sk)->dst,
844f764a6c2SLuiz Augusto von Dentz 					 iso_pi(sk)->bc_sid))
845f764a6c2SLuiz Augusto von Dentz 		err = -EADDRINUSE;
846f764a6c2SLuiz Augusto von Dentz 
847f764a6c2SLuiz Augusto von Dentz 	write_unlock(&iso_sk_list.lock);
848f764a6c2SLuiz Augusto von Dentz 
849f764a6c2SLuiz Augusto von Dentz 	if (err)
850f764a6c2SLuiz Augusto von Dentz 		return err;
851f764a6c2SLuiz Augusto von Dentz 
852f764a6c2SLuiz Augusto von Dentz 	hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
853f764a6c2SLuiz Augusto von Dentz 			     iso_pi(sk)->src_type);
854f764a6c2SLuiz Augusto von Dentz 	if (!hdev)
855f764a6c2SLuiz Augusto von Dentz 		return -EHOSTUNREACH;
856f764a6c2SLuiz Augusto von Dentz 
857f764a6c2SLuiz Augusto von Dentz 	hci_dev_lock(hdev);
858f764a6c2SLuiz Augusto von Dentz 
859f764a6c2SLuiz Augusto von Dentz 	err = hci_pa_create_sync(hdev, &iso_pi(sk)->dst, iso_pi(sk)->dst_type,
860f764a6c2SLuiz Augusto von Dentz 				 iso_pi(sk)->bc_sid);
861f764a6c2SLuiz Augusto von Dentz 
862f764a6c2SLuiz Augusto von Dentz 	hci_dev_unlock(hdev);
863f764a6c2SLuiz Augusto von Dentz 
864f764a6c2SLuiz Augusto von Dentz 	return err;
865f764a6c2SLuiz Augusto von Dentz }
866f764a6c2SLuiz Augusto von Dentz 
867f764a6c2SLuiz Augusto von Dentz static int iso_listen_cis(struct sock *sk)
868f764a6c2SLuiz Augusto von Dentz {
869f764a6c2SLuiz Augusto von Dentz 	int err = 0;
870f764a6c2SLuiz Augusto von Dentz 
871f764a6c2SLuiz Augusto von Dentz 	BT_DBG("%pMR", &iso_pi(sk)->src);
872f764a6c2SLuiz Augusto von Dentz 
873f764a6c2SLuiz Augusto von Dentz 	write_lock(&iso_sk_list.lock);
874f764a6c2SLuiz Augusto von Dentz 
875f764a6c2SLuiz Augusto von Dentz 	if (__iso_get_sock_listen_by_addr(&iso_pi(sk)->src))
876f764a6c2SLuiz Augusto von Dentz 		err = -EADDRINUSE;
877f764a6c2SLuiz Augusto von Dentz 
878f764a6c2SLuiz Augusto von Dentz 	write_unlock(&iso_sk_list.lock);
879f764a6c2SLuiz Augusto von Dentz 
880f764a6c2SLuiz Augusto von Dentz 	return err;
881f764a6c2SLuiz Augusto von Dentz }
882f764a6c2SLuiz Augusto von Dentz 
883ccf74f23SLuiz Augusto von Dentz static int iso_sock_listen(struct socket *sock, int backlog)
884ccf74f23SLuiz Augusto von Dentz {
885ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
886ccf74f23SLuiz Augusto von Dentz 	int err = 0;
887ccf74f23SLuiz Augusto von Dentz 
888ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p backlog %d", sk, backlog);
889ccf74f23SLuiz Augusto von Dentz 
890ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
891ccf74f23SLuiz Augusto von Dentz 
892ccf74f23SLuiz Augusto von Dentz 	if (sk->sk_state != BT_BOUND) {
893ccf74f23SLuiz Augusto von Dentz 		err = -EBADFD;
894ccf74f23SLuiz Augusto von Dentz 		goto done;
895ccf74f23SLuiz Augusto von Dentz 	}
896ccf74f23SLuiz Augusto von Dentz 
897ccf74f23SLuiz Augusto von Dentz 	if (sk->sk_type != SOCK_SEQPACKET) {
898ccf74f23SLuiz Augusto von Dentz 		err = -EINVAL;
899ccf74f23SLuiz Augusto von Dentz 		goto done;
900ccf74f23SLuiz Augusto von Dentz 	}
901ccf74f23SLuiz Augusto von Dentz 
902f764a6c2SLuiz Augusto von Dentz 	if (!bacmp(&iso_pi(sk)->dst, BDADDR_ANY))
903f764a6c2SLuiz Augusto von Dentz 		err = iso_listen_cis(sk);
904f764a6c2SLuiz Augusto von Dentz 	else
905f764a6c2SLuiz Augusto von Dentz 		err = iso_listen_bis(sk);
906ccf74f23SLuiz Augusto von Dentz 
907f764a6c2SLuiz Augusto von Dentz 	if (err)
908f764a6c2SLuiz Augusto von Dentz 		goto done;
909ccf74f23SLuiz Augusto von Dentz 
910ccf74f23SLuiz Augusto von Dentz 	sk->sk_max_ack_backlog = backlog;
911ccf74f23SLuiz Augusto von Dentz 	sk->sk_ack_backlog = 0;
912ccf74f23SLuiz Augusto von Dentz 
913ccf74f23SLuiz Augusto von Dentz 	sk->sk_state = BT_LISTEN;
914ccf74f23SLuiz Augusto von Dentz 
915ccf74f23SLuiz Augusto von Dentz done:
916ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
917ccf74f23SLuiz Augusto von Dentz 	return err;
918ccf74f23SLuiz Augusto von Dentz }
919ccf74f23SLuiz Augusto von Dentz 
920ccf74f23SLuiz Augusto von Dentz static int iso_sock_accept(struct socket *sock, struct socket *newsock,
921ccf74f23SLuiz Augusto von Dentz 			   int flags, bool kern)
922ccf74f23SLuiz Augusto von Dentz {
923ccf74f23SLuiz Augusto von Dentz 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
924ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk, *ch;
925ccf74f23SLuiz Augusto von Dentz 	long timeo;
926ccf74f23SLuiz Augusto von Dentz 	int err = 0;
927ccf74f23SLuiz Augusto von Dentz 
928ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
929ccf74f23SLuiz Augusto von Dentz 
930ccf74f23SLuiz Augusto von Dentz 	timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
931ccf74f23SLuiz Augusto von Dentz 
932ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p timeo %ld", sk, timeo);
933ccf74f23SLuiz Augusto von Dentz 
934ccf74f23SLuiz Augusto von Dentz 	/* Wait for an incoming connection. (wake-one). */
935ccf74f23SLuiz Augusto von Dentz 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
936ccf74f23SLuiz Augusto von Dentz 	while (1) {
937ccf74f23SLuiz Augusto von Dentz 		if (sk->sk_state != BT_LISTEN) {
938ccf74f23SLuiz Augusto von Dentz 			err = -EBADFD;
939ccf74f23SLuiz Augusto von Dentz 			break;
940ccf74f23SLuiz Augusto von Dentz 		}
941ccf74f23SLuiz Augusto von Dentz 
942ccf74f23SLuiz Augusto von Dentz 		ch = bt_accept_dequeue(sk, newsock);
943ccf74f23SLuiz Augusto von Dentz 		if (ch)
944ccf74f23SLuiz Augusto von Dentz 			break;
945ccf74f23SLuiz Augusto von Dentz 
946ccf74f23SLuiz Augusto von Dentz 		if (!timeo) {
947ccf74f23SLuiz Augusto von Dentz 			err = -EAGAIN;
948ccf74f23SLuiz Augusto von Dentz 			break;
949ccf74f23SLuiz Augusto von Dentz 		}
950ccf74f23SLuiz Augusto von Dentz 
951ccf74f23SLuiz Augusto von Dentz 		if (signal_pending(current)) {
952ccf74f23SLuiz Augusto von Dentz 			err = sock_intr_errno(timeo);
953ccf74f23SLuiz Augusto von Dentz 			break;
954ccf74f23SLuiz Augusto von Dentz 		}
955ccf74f23SLuiz Augusto von Dentz 
956ccf74f23SLuiz Augusto von Dentz 		release_sock(sk);
957ccf74f23SLuiz Augusto von Dentz 
958ccf74f23SLuiz Augusto von Dentz 		timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
959ccf74f23SLuiz Augusto von Dentz 		lock_sock(sk);
960ccf74f23SLuiz Augusto von Dentz 	}
961ccf74f23SLuiz Augusto von Dentz 	remove_wait_queue(sk_sleep(sk), &wait);
962ccf74f23SLuiz Augusto von Dentz 
963ccf74f23SLuiz Augusto von Dentz 	if (err)
964ccf74f23SLuiz Augusto von Dentz 		goto done;
965ccf74f23SLuiz Augusto von Dentz 
966ccf74f23SLuiz Augusto von Dentz 	newsock->state = SS_CONNECTED;
967ccf74f23SLuiz Augusto von Dentz 
968ccf74f23SLuiz Augusto von Dentz 	BT_DBG("new socket %p", ch);
969ccf74f23SLuiz Augusto von Dentz 
970ccf74f23SLuiz Augusto von Dentz done:
971ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
972ccf74f23SLuiz Augusto von Dentz 	return err;
973ccf74f23SLuiz Augusto von Dentz }
974ccf74f23SLuiz Augusto von Dentz 
975ccf74f23SLuiz Augusto von Dentz static int iso_sock_getname(struct socket *sock, struct sockaddr *addr,
976ccf74f23SLuiz Augusto von Dentz 			    int peer)
977ccf74f23SLuiz Augusto von Dentz {
978ccf74f23SLuiz Augusto von Dentz 	struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
979ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
980ccf74f23SLuiz Augusto von Dentz 
981ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sock %p, sk %p", sock, sk);
982ccf74f23SLuiz Augusto von Dentz 
983ccf74f23SLuiz Augusto von Dentz 	addr->sa_family = AF_BLUETOOTH;
984ccf74f23SLuiz Augusto von Dentz 
985ccf74f23SLuiz Augusto von Dentz 	if (peer) {
986ccf74f23SLuiz Augusto von Dentz 		bacpy(&sa->iso_bdaddr, &iso_pi(sk)->dst);
987ccf74f23SLuiz Augusto von Dentz 		sa->iso_bdaddr_type = iso_pi(sk)->dst_type;
988ccf74f23SLuiz Augusto von Dentz 	} else {
989ccf74f23SLuiz Augusto von Dentz 		bacpy(&sa->iso_bdaddr, &iso_pi(sk)->src);
990ccf74f23SLuiz Augusto von Dentz 		sa->iso_bdaddr_type = iso_pi(sk)->src_type;
991ccf74f23SLuiz Augusto von Dentz 	}
992ccf74f23SLuiz Augusto von Dentz 
993ccf74f23SLuiz Augusto von Dentz 	return sizeof(struct sockaddr_iso);
994ccf74f23SLuiz Augusto von Dentz }
995ccf74f23SLuiz Augusto von Dentz 
996ccf74f23SLuiz Augusto von Dentz static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg,
997ccf74f23SLuiz Augusto von Dentz 			    size_t len)
998ccf74f23SLuiz Augusto von Dentz {
999ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
1000ccf74f23SLuiz Augusto von Dentz 	struct iso_conn *conn = iso_pi(sk)->conn;
1001ccf74f23SLuiz Augusto von Dentz 	struct sk_buff *skb, **frag;
1002ccf74f23SLuiz Augusto von Dentz 	int err;
1003ccf74f23SLuiz Augusto von Dentz 
1004ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sock %p, sk %p", sock, sk);
1005ccf74f23SLuiz Augusto von Dentz 
1006ccf74f23SLuiz Augusto von Dentz 	err = sock_error(sk);
1007ccf74f23SLuiz Augusto von Dentz 	if (err)
1008ccf74f23SLuiz Augusto von Dentz 		return err;
1009ccf74f23SLuiz Augusto von Dentz 
1010ccf74f23SLuiz Augusto von Dentz 	if (msg->msg_flags & MSG_OOB)
1011ccf74f23SLuiz Augusto von Dentz 		return -EOPNOTSUPP;
1012ccf74f23SLuiz Augusto von Dentz 
1013ccf74f23SLuiz Augusto von Dentz 	if (sk->sk_state != BT_CONNECTED)
1014ccf74f23SLuiz Augusto von Dentz 		return -ENOTCONN;
1015ccf74f23SLuiz Augusto von Dentz 
1016ccf74f23SLuiz Augusto von Dentz 	skb = bt_skb_sendmsg(sk, msg, len, conn->hcon->hdev->iso_mtu,
1017ccf74f23SLuiz Augusto von Dentz 			     HCI_ISO_DATA_HDR_SIZE, 0);
1018ccf74f23SLuiz Augusto von Dentz 	if (IS_ERR(skb))
1019ccf74f23SLuiz Augusto von Dentz 		return PTR_ERR(skb);
1020ccf74f23SLuiz Augusto von Dentz 
1021ccf74f23SLuiz Augusto von Dentz 	len -= skb->len;
1022ccf74f23SLuiz Augusto von Dentz 
1023ccf74f23SLuiz Augusto von Dentz 	BT_DBG("skb %p len %d", sk, skb->len);
1024ccf74f23SLuiz Augusto von Dentz 
1025ccf74f23SLuiz Augusto von Dentz 	/* Continuation fragments */
1026ccf74f23SLuiz Augusto von Dentz 	frag = &skb_shinfo(skb)->frag_list;
1027ccf74f23SLuiz Augusto von Dentz 	while (len) {
1028ccf74f23SLuiz Augusto von Dentz 		struct sk_buff *tmp;
1029ccf74f23SLuiz Augusto von Dentz 
1030ccf74f23SLuiz Augusto von Dentz 		tmp = bt_skb_sendmsg(sk, msg, len, conn->hcon->hdev->iso_mtu,
1031ccf74f23SLuiz Augusto von Dentz 				     0, 0);
1032ccf74f23SLuiz Augusto von Dentz 		if (IS_ERR(tmp)) {
1033ccf74f23SLuiz Augusto von Dentz 			kfree_skb(skb);
1034ccf74f23SLuiz Augusto von Dentz 			return PTR_ERR(tmp);
1035ccf74f23SLuiz Augusto von Dentz 		}
1036ccf74f23SLuiz Augusto von Dentz 
1037ccf74f23SLuiz Augusto von Dentz 		*frag = tmp;
1038ccf74f23SLuiz Augusto von Dentz 
1039ccf74f23SLuiz Augusto von Dentz 		len  -= tmp->len;
1040ccf74f23SLuiz Augusto von Dentz 
1041ccf74f23SLuiz Augusto von Dentz 		skb->len += tmp->len;
1042ccf74f23SLuiz Augusto von Dentz 		skb->data_len += tmp->len;
1043ccf74f23SLuiz Augusto von Dentz 
1044ccf74f23SLuiz Augusto von Dentz 		BT_DBG("frag %p len %d", *frag, tmp->len);
1045ccf74f23SLuiz Augusto von Dentz 
1046ccf74f23SLuiz Augusto von Dentz 		frag = &(*frag)->next;
1047ccf74f23SLuiz Augusto von Dentz 	}
1048ccf74f23SLuiz Augusto von Dentz 
1049ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
1050ccf74f23SLuiz Augusto von Dentz 
1051ccf74f23SLuiz Augusto von Dentz 	if (sk->sk_state == BT_CONNECTED)
1052ccf74f23SLuiz Augusto von Dentz 		err = iso_send_frame(sk, skb);
1053ccf74f23SLuiz Augusto von Dentz 	else
1054ccf74f23SLuiz Augusto von Dentz 		err = -ENOTCONN;
1055ccf74f23SLuiz Augusto von Dentz 
1056ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
1057ccf74f23SLuiz Augusto von Dentz 
1058ccf74f23SLuiz Augusto von Dentz 	if (err < 0)
1059ccf74f23SLuiz Augusto von Dentz 		kfree_skb(skb);
1060ccf74f23SLuiz Augusto von Dentz 	return err;
1061ccf74f23SLuiz Augusto von Dentz }
1062ccf74f23SLuiz Augusto von Dentz 
1063ccf74f23SLuiz Augusto von Dentz static void iso_conn_defer_accept(struct hci_conn *conn)
1064ccf74f23SLuiz Augusto von Dentz {
1065ccf74f23SLuiz Augusto von Dentz 	struct hci_cp_le_accept_cis cp;
1066ccf74f23SLuiz Augusto von Dentz 	struct hci_dev *hdev = conn->hdev;
1067ccf74f23SLuiz Augusto von Dentz 
1068ccf74f23SLuiz Augusto von Dentz 	BT_DBG("conn %p", conn);
1069ccf74f23SLuiz Augusto von Dentz 
1070ccf74f23SLuiz Augusto von Dentz 	conn->state = BT_CONFIG;
1071ccf74f23SLuiz Augusto von Dentz 
1072ccf74f23SLuiz Augusto von Dentz 	cp.handle = cpu_to_le16(conn->handle);
1073ccf74f23SLuiz Augusto von Dentz 
1074ccf74f23SLuiz Augusto von Dentz 	hci_send_cmd(hdev, HCI_OP_LE_ACCEPT_CIS, sizeof(cp), &cp);
1075ccf74f23SLuiz Augusto von Dentz }
1076ccf74f23SLuiz Augusto von Dentz 
1077ccf74f23SLuiz Augusto von Dentz static int iso_sock_recvmsg(struct socket *sock, struct msghdr *msg,
1078ccf74f23SLuiz Augusto von Dentz 			    size_t len, int flags)
1079ccf74f23SLuiz Augusto von Dentz {
1080ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
1081ccf74f23SLuiz Augusto von Dentz 	struct iso_pinfo *pi = iso_pi(sk);
1082ccf74f23SLuiz Augusto von Dentz 	int err;
1083ccf74f23SLuiz Augusto von Dentz 
1084ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p", sk);
1085ccf74f23SLuiz Augusto von Dentz 
1086ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
1087ccf74f23SLuiz Augusto von Dentz 
1088ccf74f23SLuiz Augusto von Dentz 	if (test_and_clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
1089ccf74f23SLuiz Augusto von Dentz 		switch (sk->sk_state) {
1090ccf74f23SLuiz Augusto von Dentz 		case BT_CONNECT2:
1091ccf74f23SLuiz Augusto von Dentz 			iso_conn_defer_accept(pi->conn->hcon);
1092ccf74f23SLuiz Augusto von Dentz 			sk->sk_state = BT_CONFIG;
1093ccf74f23SLuiz Augusto von Dentz 			release_sock(sk);
1094ccf74f23SLuiz Augusto von Dentz 			return 0;
1095ccf74f23SLuiz Augusto von Dentz 		case BT_CONNECT:
1096f764a6c2SLuiz Augusto von Dentz 			err = iso_connect_cis(sk);
1097ccf74f23SLuiz Augusto von Dentz 			release_sock(sk);
1098ccf74f23SLuiz Augusto von Dentz 			return err;
1099ccf74f23SLuiz Augusto von Dentz 		}
1100ccf74f23SLuiz Augusto von Dentz 	}
1101ccf74f23SLuiz Augusto von Dentz 
1102ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
1103ccf74f23SLuiz Augusto von Dentz 
1104ccf74f23SLuiz Augusto von Dentz 	return bt_sock_recvmsg(sock, msg, len, flags);
1105ccf74f23SLuiz Augusto von Dentz }
1106ccf74f23SLuiz Augusto von Dentz 
1107ccf74f23SLuiz Augusto von Dentz static bool check_io_qos(struct bt_iso_io_qos *qos)
1108ccf74f23SLuiz Augusto von Dentz {
1109ccf74f23SLuiz Augusto von Dentz 	/* If no PHY is enable SDU must be 0 */
1110ccf74f23SLuiz Augusto von Dentz 	if (!qos->phy && qos->sdu)
1111ccf74f23SLuiz Augusto von Dentz 		return false;
1112ccf74f23SLuiz Augusto von Dentz 
1113ccf74f23SLuiz Augusto von Dentz 	if (qos->interval && (qos->interval < 0xff || qos->interval > 0xfffff))
1114ccf74f23SLuiz Augusto von Dentz 		return false;
1115ccf74f23SLuiz Augusto von Dentz 
1116ccf74f23SLuiz Augusto von Dentz 	if (qos->latency && (qos->latency < 0x05 || qos->latency > 0xfa0))
1117ccf74f23SLuiz Augusto von Dentz 		return false;
1118ccf74f23SLuiz Augusto von Dentz 
1119ccf74f23SLuiz Augusto von Dentz 	if (qos->phy > BT_ISO_PHY_ANY)
1120ccf74f23SLuiz Augusto von Dentz 		return false;
1121ccf74f23SLuiz Augusto von Dentz 
1122ccf74f23SLuiz Augusto von Dentz 	return true;
1123ccf74f23SLuiz Augusto von Dentz }
1124ccf74f23SLuiz Augusto von Dentz 
1125ccf74f23SLuiz Augusto von Dentz static bool check_qos(struct bt_iso_qos *qos)
1126ccf74f23SLuiz Augusto von Dentz {
1127ccf74f23SLuiz Augusto von Dentz 	if (qos->sca > 0x07)
1128ccf74f23SLuiz Augusto von Dentz 		return false;
1129ccf74f23SLuiz Augusto von Dentz 
1130ccf74f23SLuiz Augusto von Dentz 	if (qos->packing > 0x01)
1131ccf74f23SLuiz Augusto von Dentz 		return false;
1132ccf74f23SLuiz Augusto von Dentz 
1133ccf74f23SLuiz Augusto von Dentz 	if (qos->framing > 0x01)
1134ccf74f23SLuiz Augusto von Dentz 		return false;
1135ccf74f23SLuiz Augusto von Dentz 
1136ccf74f23SLuiz Augusto von Dentz 	if (!check_io_qos(&qos->in))
1137ccf74f23SLuiz Augusto von Dentz 		return false;
1138ccf74f23SLuiz Augusto von Dentz 
1139ccf74f23SLuiz Augusto von Dentz 	if (!check_io_qos(&qos->out))
1140ccf74f23SLuiz Augusto von Dentz 		return false;
1141ccf74f23SLuiz Augusto von Dentz 
1142ccf74f23SLuiz Augusto von Dentz 	return true;
1143ccf74f23SLuiz Augusto von Dentz }
1144ccf74f23SLuiz Augusto von Dentz 
1145ccf74f23SLuiz Augusto von Dentz static int iso_sock_setsockopt(struct socket *sock, int level, int optname,
1146ccf74f23SLuiz Augusto von Dentz 			       sockptr_t optval, unsigned int optlen)
1147ccf74f23SLuiz Augusto von Dentz {
1148ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
1149ccf74f23SLuiz Augusto von Dentz 	int len, err = 0;
1150ccf74f23SLuiz Augusto von Dentz 	struct bt_iso_qos qos;
1151ccf74f23SLuiz Augusto von Dentz 	u32 opt;
1152ccf74f23SLuiz Augusto von Dentz 
1153ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p", sk);
1154ccf74f23SLuiz Augusto von Dentz 
1155ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
1156ccf74f23SLuiz Augusto von Dentz 
1157ccf74f23SLuiz Augusto von Dentz 	switch (optname) {
1158ccf74f23SLuiz Augusto von Dentz 	case BT_DEFER_SETUP:
1159ccf74f23SLuiz Augusto von Dentz 		if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
1160ccf74f23SLuiz Augusto von Dentz 			err = -EINVAL;
1161ccf74f23SLuiz Augusto von Dentz 			break;
1162ccf74f23SLuiz Augusto von Dentz 		}
1163ccf74f23SLuiz Augusto von Dentz 
1164ccf74f23SLuiz Augusto von Dentz 		if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
1165ccf74f23SLuiz Augusto von Dentz 			err = -EFAULT;
1166ccf74f23SLuiz Augusto von Dentz 			break;
1167ccf74f23SLuiz Augusto von Dentz 		}
1168ccf74f23SLuiz Augusto von Dentz 
1169ccf74f23SLuiz Augusto von Dentz 		if (opt)
1170ccf74f23SLuiz Augusto von Dentz 			set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
1171ccf74f23SLuiz Augusto von Dentz 		else
1172ccf74f23SLuiz Augusto von Dentz 			clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
1173ccf74f23SLuiz Augusto von Dentz 		break;
1174ccf74f23SLuiz Augusto von Dentz 
1175ccf74f23SLuiz Augusto von Dentz 	case BT_ISO_QOS:
1176ccf74f23SLuiz Augusto von Dentz 		if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
1177ccf74f23SLuiz Augusto von Dentz 		    sk->sk_state != BT_CONNECT2) {
1178ccf74f23SLuiz Augusto von Dentz 			err = -EINVAL;
1179ccf74f23SLuiz Augusto von Dentz 			break;
1180ccf74f23SLuiz Augusto von Dentz 		}
1181ccf74f23SLuiz Augusto von Dentz 
1182ccf74f23SLuiz Augusto von Dentz 		len = min_t(unsigned int, sizeof(qos), optlen);
1183164dac97SDan Carpenter 		if (len != sizeof(qos)) {
1184164dac97SDan Carpenter 			err = -EINVAL;
1185164dac97SDan Carpenter 			break;
1186164dac97SDan Carpenter 		}
1187ccf74f23SLuiz Augusto von Dentz 
1188ccf74f23SLuiz Augusto von Dentz 		memset(&qos, 0, sizeof(qos));
1189ccf74f23SLuiz Augusto von Dentz 
1190ccf74f23SLuiz Augusto von Dentz 		if (copy_from_sockptr(&qos, optval, len)) {
1191ccf74f23SLuiz Augusto von Dentz 			err = -EFAULT;
1192ccf74f23SLuiz Augusto von Dentz 			break;
1193ccf74f23SLuiz Augusto von Dentz 		}
1194ccf74f23SLuiz Augusto von Dentz 
1195ccf74f23SLuiz Augusto von Dentz 		if (!check_qos(&qos)) {
1196ccf74f23SLuiz Augusto von Dentz 			err = -EINVAL;
1197ccf74f23SLuiz Augusto von Dentz 			break;
1198ccf74f23SLuiz Augusto von Dentz 		}
1199ccf74f23SLuiz Augusto von Dentz 
1200ccf74f23SLuiz Augusto von Dentz 		iso_pi(sk)->qos = qos;
1201ccf74f23SLuiz Augusto von Dentz 
1202ccf74f23SLuiz Augusto von Dentz 		break;
1203ccf74f23SLuiz Augusto von Dentz 
1204f764a6c2SLuiz Augusto von Dentz 	case BT_ISO_BASE:
1205f764a6c2SLuiz Augusto von Dentz 		if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
1206f764a6c2SLuiz Augusto von Dentz 		    sk->sk_state != BT_CONNECT2) {
1207f764a6c2SLuiz Augusto von Dentz 			err = -EINVAL;
1208f764a6c2SLuiz Augusto von Dentz 			break;
1209f764a6c2SLuiz Augusto von Dentz 		}
1210f764a6c2SLuiz Augusto von Dentz 
1211f764a6c2SLuiz Augusto von Dentz 		if (optlen > sizeof(iso_pi(sk)->base)) {
1212f764a6c2SLuiz Augusto von Dentz 			err = -EOVERFLOW;
1213f764a6c2SLuiz Augusto von Dentz 			break;
1214f764a6c2SLuiz Augusto von Dentz 		}
1215f764a6c2SLuiz Augusto von Dentz 
1216f764a6c2SLuiz Augusto von Dentz 		len = min_t(unsigned int, sizeof(iso_pi(sk)->base), optlen);
1217f764a6c2SLuiz Augusto von Dentz 
1218f764a6c2SLuiz Augusto von Dentz 		if (copy_from_sockptr(iso_pi(sk)->base, optval, len)) {
1219f764a6c2SLuiz Augusto von Dentz 			err = -EFAULT;
1220f764a6c2SLuiz Augusto von Dentz 			break;
1221f764a6c2SLuiz Augusto von Dentz 		}
1222f764a6c2SLuiz Augusto von Dentz 
1223f764a6c2SLuiz Augusto von Dentz 		iso_pi(sk)->base_len = len;
1224f764a6c2SLuiz Augusto von Dentz 
1225f764a6c2SLuiz Augusto von Dentz 		break;
1226f764a6c2SLuiz Augusto von Dentz 
1227ccf74f23SLuiz Augusto von Dentz 	default:
1228ccf74f23SLuiz Augusto von Dentz 		err = -ENOPROTOOPT;
1229ccf74f23SLuiz Augusto von Dentz 		break;
1230ccf74f23SLuiz Augusto von Dentz 	}
1231ccf74f23SLuiz Augusto von Dentz 
1232ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
1233ccf74f23SLuiz Augusto von Dentz 	return err;
1234ccf74f23SLuiz Augusto von Dentz }
1235ccf74f23SLuiz Augusto von Dentz 
1236ccf74f23SLuiz Augusto von Dentz static int iso_sock_getsockopt(struct socket *sock, int level, int optname,
1237ccf74f23SLuiz Augusto von Dentz 			       char __user *optval, int __user *optlen)
1238ccf74f23SLuiz Augusto von Dentz {
1239ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
1240ccf74f23SLuiz Augusto von Dentz 	int len, err = 0;
12410eee4995SLuiz Augusto von Dentz 	struct bt_iso_qos *qos;
1242f764a6c2SLuiz Augusto von Dentz 	u8 base_len;
1243f764a6c2SLuiz Augusto von Dentz 	u8 *base;
1244ccf74f23SLuiz Augusto von Dentz 
1245ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p", sk);
1246ccf74f23SLuiz Augusto von Dentz 
1247ccf74f23SLuiz Augusto von Dentz 	if (get_user(len, optlen))
1248ccf74f23SLuiz Augusto von Dentz 		return -EFAULT;
1249ccf74f23SLuiz Augusto von Dentz 
1250ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
1251ccf74f23SLuiz Augusto von Dentz 
1252ccf74f23SLuiz Augusto von Dentz 	switch (optname) {
1253ccf74f23SLuiz Augusto von Dentz 	case BT_DEFER_SETUP:
1254ccf74f23SLuiz Augusto von Dentz 		if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
1255ccf74f23SLuiz Augusto von Dentz 			err = -EINVAL;
1256ccf74f23SLuiz Augusto von Dentz 			break;
1257ccf74f23SLuiz Augusto von Dentz 		}
1258ccf74f23SLuiz Augusto von Dentz 
1259ccf74f23SLuiz Augusto von Dentz 		if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
1260ccf74f23SLuiz Augusto von Dentz 			     (u32 __user *)optval))
1261ccf74f23SLuiz Augusto von Dentz 			err = -EFAULT;
1262ccf74f23SLuiz Augusto von Dentz 
1263ccf74f23SLuiz Augusto von Dentz 		break;
1264ccf74f23SLuiz Augusto von Dentz 
1265ccf74f23SLuiz Augusto von Dentz 	case BT_ISO_QOS:
1266ccf74f23SLuiz Augusto von Dentz 		if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONNECT2)
12670eee4995SLuiz Augusto von Dentz 			qos = &iso_pi(sk)->conn->hcon->iso_qos;
1268ccf74f23SLuiz Augusto von Dentz 		else
12690eee4995SLuiz Augusto von Dentz 			qos = &iso_pi(sk)->qos;
1270ccf74f23SLuiz Augusto von Dentz 
12710eee4995SLuiz Augusto von Dentz 		len = min_t(unsigned int, len, sizeof(*qos));
12720eee4995SLuiz Augusto von Dentz 		if (copy_to_user(optval, qos, len))
1273ccf74f23SLuiz Augusto von Dentz 			err = -EFAULT;
1274ccf74f23SLuiz Augusto von Dentz 
1275ccf74f23SLuiz Augusto von Dentz 		break;
1276ccf74f23SLuiz Augusto von Dentz 
1277f764a6c2SLuiz Augusto von Dentz 	case BT_ISO_BASE:
1278f764a6c2SLuiz Augusto von Dentz 		if (sk->sk_state == BT_CONNECTED) {
1279f764a6c2SLuiz Augusto von Dentz 			base_len = iso_pi(sk)->conn->hcon->le_per_adv_data_len;
1280f764a6c2SLuiz Augusto von Dentz 			base = iso_pi(sk)->conn->hcon->le_per_adv_data;
1281f764a6c2SLuiz Augusto von Dentz 		} else {
1282f764a6c2SLuiz Augusto von Dentz 			base_len = iso_pi(sk)->base_len;
1283f764a6c2SLuiz Augusto von Dentz 			base = iso_pi(sk)->base;
1284f764a6c2SLuiz Augusto von Dentz 		}
1285f764a6c2SLuiz Augusto von Dentz 
1286f764a6c2SLuiz Augusto von Dentz 		len = min_t(unsigned int, len, base_len);
1287f764a6c2SLuiz Augusto von Dentz 		if (copy_to_user(optval, base, len))
1288f764a6c2SLuiz Augusto von Dentz 			err = -EFAULT;
1289f764a6c2SLuiz Augusto von Dentz 
1290f764a6c2SLuiz Augusto von Dentz 		break;
1291f764a6c2SLuiz Augusto von Dentz 
1292ccf74f23SLuiz Augusto von Dentz 	default:
1293ccf74f23SLuiz Augusto von Dentz 		err = -ENOPROTOOPT;
1294ccf74f23SLuiz Augusto von Dentz 		break;
1295ccf74f23SLuiz Augusto von Dentz 	}
1296ccf74f23SLuiz Augusto von Dentz 
1297ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
1298ccf74f23SLuiz Augusto von Dentz 	return err;
1299ccf74f23SLuiz Augusto von Dentz }
1300ccf74f23SLuiz Augusto von Dentz 
1301ccf74f23SLuiz Augusto von Dentz static int iso_sock_shutdown(struct socket *sock, int how)
1302ccf74f23SLuiz Augusto von Dentz {
1303ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
1304ccf74f23SLuiz Augusto von Dentz 	int err = 0;
1305ccf74f23SLuiz Augusto von Dentz 
1306ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sock %p, sk %p", sock, sk);
1307ccf74f23SLuiz Augusto von Dentz 
1308ccf74f23SLuiz Augusto von Dentz 	if (!sk)
1309ccf74f23SLuiz Augusto von Dentz 		return 0;
1310ccf74f23SLuiz Augusto von Dentz 
1311ccf74f23SLuiz Augusto von Dentz 	sock_hold(sk);
1312ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
1313ccf74f23SLuiz Augusto von Dentz 
1314ccf74f23SLuiz Augusto von Dentz 	if (!sk->sk_shutdown) {
1315ccf74f23SLuiz Augusto von Dentz 		sk->sk_shutdown = SHUTDOWN_MASK;
1316ccf74f23SLuiz Augusto von Dentz 		iso_sock_clear_timer(sk);
1317ccf74f23SLuiz Augusto von Dentz 		__iso_sock_close(sk);
1318ccf74f23SLuiz Augusto von Dentz 
1319ccf74f23SLuiz Augusto von Dentz 		if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1320ccf74f23SLuiz Augusto von Dentz 		    !(current->flags & PF_EXITING))
1321ccf74f23SLuiz Augusto von Dentz 			err = bt_sock_wait_state(sk, BT_CLOSED,
1322ccf74f23SLuiz Augusto von Dentz 						 sk->sk_lingertime);
1323ccf74f23SLuiz Augusto von Dentz 	}
1324ccf74f23SLuiz Augusto von Dentz 
1325ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
1326ccf74f23SLuiz Augusto von Dentz 	sock_put(sk);
1327ccf74f23SLuiz Augusto von Dentz 
1328ccf74f23SLuiz Augusto von Dentz 	return err;
1329ccf74f23SLuiz Augusto von Dentz }
1330ccf74f23SLuiz Augusto von Dentz 
1331ccf74f23SLuiz Augusto von Dentz static int iso_sock_release(struct socket *sock)
1332ccf74f23SLuiz Augusto von Dentz {
1333ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
1334ccf74f23SLuiz Augusto von Dentz 	int err = 0;
1335ccf74f23SLuiz Augusto von Dentz 
1336ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sock %p, sk %p", sock, sk);
1337ccf74f23SLuiz Augusto von Dentz 
1338ccf74f23SLuiz Augusto von Dentz 	if (!sk)
1339ccf74f23SLuiz Augusto von Dentz 		return 0;
1340ccf74f23SLuiz Augusto von Dentz 
1341ccf74f23SLuiz Augusto von Dentz 	iso_sock_close(sk);
1342ccf74f23SLuiz Augusto von Dentz 
1343ccf74f23SLuiz Augusto von Dentz 	if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1344ccf74f23SLuiz Augusto von Dentz 	    !(current->flags & PF_EXITING)) {
1345ccf74f23SLuiz Augusto von Dentz 		lock_sock(sk);
1346ccf74f23SLuiz Augusto von Dentz 		err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1347ccf74f23SLuiz Augusto von Dentz 		release_sock(sk);
1348ccf74f23SLuiz Augusto von Dentz 	}
1349ccf74f23SLuiz Augusto von Dentz 
1350ccf74f23SLuiz Augusto von Dentz 	sock_orphan(sk);
1351ccf74f23SLuiz Augusto von Dentz 	iso_sock_kill(sk);
1352ccf74f23SLuiz Augusto von Dentz 	return err;
1353ccf74f23SLuiz Augusto von Dentz }
1354ccf74f23SLuiz Augusto von Dentz 
1355ccf74f23SLuiz Augusto von Dentz static void iso_sock_ready(struct sock *sk)
1356ccf74f23SLuiz Augusto von Dentz {
1357ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p", sk);
1358ccf74f23SLuiz Augusto von Dentz 
1359ccf74f23SLuiz Augusto von Dentz 	if (!sk)
1360ccf74f23SLuiz Augusto von Dentz 		return;
1361ccf74f23SLuiz Augusto von Dentz 
1362ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
1363ccf74f23SLuiz Augusto von Dentz 	iso_sock_clear_timer(sk);
1364ccf74f23SLuiz Augusto von Dentz 	sk->sk_state = BT_CONNECTED;
1365ccf74f23SLuiz Augusto von Dentz 	sk->sk_state_change(sk);
1366ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
1367ccf74f23SLuiz Augusto von Dentz }
1368ccf74f23SLuiz Augusto von Dentz 
1369ccf74f23SLuiz Augusto von Dentz struct iso_list_data {
1370ccf74f23SLuiz Augusto von Dentz 	struct hci_conn *hcon;
1371ccf74f23SLuiz Augusto von Dentz 	int count;
1372ccf74f23SLuiz Augusto von Dentz };
1373ccf74f23SLuiz Augusto von Dentz 
1374f764a6c2SLuiz Augusto von Dentz static bool iso_match_big(struct sock *sk, void *data)
1375f764a6c2SLuiz Augusto von Dentz {
1376f764a6c2SLuiz Augusto von Dentz 	struct hci_evt_le_big_sync_estabilished *ev = data;
1377f764a6c2SLuiz Augusto von Dentz 
1378f764a6c2SLuiz Augusto von Dentz 	return ev->handle == iso_pi(sk)->qos.big;
1379f764a6c2SLuiz Augusto von Dentz }
1380f764a6c2SLuiz Augusto von Dentz 
1381ccf74f23SLuiz Augusto von Dentz static void iso_conn_ready(struct iso_conn *conn)
1382ccf74f23SLuiz Augusto von Dentz {
1383ccf74f23SLuiz Augusto von Dentz 	struct sock *parent;
1384ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = conn->sk;
1385f764a6c2SLuiz Augusto von Dentz 	struct hci_ev_le_big_sync_estabilished *ev;
1386ccf74f23SLuiz Augusto von Dentz 
1387ccf74f23SLuiz Augusto von Dentz 	BT_DBG("conn %p", conn);
1388ccf74f23SLuiz Augusto von Dentz 
1389ccf74f23SLuiz Augusto von Dentz 	if (sk) {
1390ccf74f23SLuiz Augusto von Dentz 		iso_sock_ready(conn->sk);
1391ccf74f23SLuiz Augusto von Dentz 	} else {
1392ccf74f23SLuiz Augusto von Dentz 		iso_conn_lock(conn);
1393ccf74f23SLuiz Augusto von Dentz 
1394ccf74f23SLuiz Augusto von Dentz 		if (!conn->hcon) {
1395ccf74f23SLuiz Augusto von Dentz 			iso_conn_unlock(conn);
1396ccf74f23SLuiz Augusto von Dentz 			return;
1397ccf74f23SLuiz Augusto von Dentz 		}
1398ccf74f23SLuiz Augusto von Dentz 
1399f764a6c2SLuiz Augusto von Dentz 		ev = hci_recv_event_data(conn->hcon->hdev,
1400f764a6c2SLuiz Augusto von Dentz 					 HCI_EVT_LE_BIG_SYNC_ESTABILISHED);
1401f764a6c2SLuiz Augusto von Dentz 		if (ev)
1402f764a6c2SLuiz Augusto von Dentz 			parent = iso_get_sock_listen(&conn->hcon->src,
1403f764a6c2SLuiz Augusto von Dentz 						     &conn->hcon->dst,
1404f764a6c2SLuiz Augusto von Dentz 						     iso_match_big, ev);
1405f764a6c2SLuiz Augusto von Dentz 		else
1406f764a6c2SLuiz Augusto von Dentz 			parent = iso_get_sock_listen(&conn->hcon->src,
1407f764a6c2SLuiz Augusto von Dentz 						     BDADDR_ANY, NULL, NULL);
1408f764a6c2SLuiz Augusto von Dentz 
1409ccf74f23SLuiz Augusto von Dentz 		if (!parent) {
1410ccf74f23SLuiz Augusto von Dentz 			iso_conn_unlock(conn);
1411ccf74f23SLuiz Augusto von Dentz 			return;
1412ccf74f23SLuiz Augusto von Dentz 		}
1413ccf74f23SLuiz Augusto von Dentz 
1414ccf74f23SLuiz Augusto von Dentz 		lock_sock(parent);
1415ccf74f23SLuiz Augusto von Dentz 
1416ccf74f23SLuiz Augusto von Dentz 		sk = iso_sock_alloc(sock_net(parent), NULL,
1417ccf74f23SLuiz Augusto von Dentz 				    BTPROTO_ISO, GFP_ATOMIC, 0);
1418ccf74f23SLuiz Augusto von Dentz 		if (!sk) {
1419ccf74f23SLuiz Augusto von Dentz 			release_sock(parent);
1420ccf74f23SLuiz Augusto von Dentz 			iso_conn_unlock(conn);
1421ccf74f23SLuiz Augusto von Dentz 			return;
1422ccf74f23SLuiz Augusto von Dentz 		}
1423ccf74f23SLuiz Augusto von Dentz 
1424ccf74f23SLuiz Augusto von Dentz 		iso_sock_init(sk, parent);
1425ccf74f23SLuiz Augusto von Dentz 
1426ccf74f23SLuiz Augusto von Dentz 		bacpy(&iso_pi(sk)->src, &conn->hcon->src);
1427ccf74f23SLuiz Augusto von Dentz 		iso_pi(sk)->src_type = conn->hcon->src_type;
1428f764a6c2SLuiz Augusto von Dentz 
1429f764a6c2SLuiz Augusto von Dentz 		/* If hcon has no destination address (BDADDR_ANY) it means it
1430f764a6c2SLuiz Augusto von Dentz 		 * was created by HCI_EV_LE_BIG_SYNC_ESTABILISHED so we need to
1431f764a6c2SLuiz Augusto von Dentz 		 * initialize using the parent socket destination address.
1432f764a6c2SLuiz Augusto von Dentz 		 */
1433f764a6c2SLuiz Augusto von Dentz 		if (!bacmp(&conn->hcon->dst, BDADDR_ANY)) {
1434f764a6c2SLuiz Augusto von Dentz 			bacpy(&conn->hcon->dst, &iso_pi(parent)->dst);
1435f764a6c2SLuiz Augusto von Dentz 			conn->hcon->dst_type = iso_pi(parent)->dst_type;
1436f764a6c2SLuiz Augusto von Dentz 			conn->hcon->sync_handle = iso_pi(parent)->sync_handle;
1437f764a6c2SLuiz Augusto von Dentz 		}
1438f764a6c2SLuiz Augusto von Dentz 
1439ccf74f23SLuiz Augusto von Dentz 		bacpy(&iso_pi(sk)->dst, &conn->hcon->dst);
1440ccf74f23SLuiz Augusto von Dentz 		iso_pi(sk)->dst_type = conn->hcon->dst_type;
1441ccf74f23SLuiz Augusto von Dentz 
1442ccf74f23SLuiz Augusto von Dentz 		hci_conn_hold(conn->hcon);
1443ccf74f23SLuiz Augusto von Dentz 		__iso_chan_add(conn, sk, parent);
1444ccf74f23SLuiz Augusto von Dentz 
1445ccf74f23SLuiz Augusto von Dentz 		if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
1446ccf74f23SLuiz Augusto von Dentz 			sk->sk_state = BT_CONNECT2;
1447ccf74f23SLuiz Augusto von Dentz 		else
1448ccf74f23SLuiz Augusto von Dentz 			sk->sk_state = BT_CONNECTED;
1449ccf74f23SLuiz Augusto von Dentz 
1450ccf74f23SLuiz Augusto von Dentz 		/* Wake up parent */
1451ccf74f23SLuiz Augusto von Dentz 		parent->sk_data_ready(parent);
1452ccf74f23SLuiz Augusto von Dentz 
1453ccf74f23SLuiz Augusto von Dentz 		release_sock(parent);
1454ccf74f23SLuiz Augusto von Dentz 
1455ccf74f23SLuiz Augusto von Dentz 		iso_conn_unlock(conn);
1456ccf74f23SLuiz Augusto von Dentz 	}
1457ccf74f23SLuiz Augusto von Dentz }
1458ccf74f23SLuiz Augusto von Dentz 
1459f764a6c2SLuiz Augusto von Dentz static bool iso_match_sid(struct sock *sk, void *data)
1460f764a6c2SLuiz Augusto von Dentz {
1461f764a6c2SLuiz Augusto von Dentz 	struct hci_ev_le_pa_sync_established *ev = data;
1462f764a6c2SLuiz Augusto von Dentz 
1463f764a6c2SLuiz Augusto von Dentz 	return ev->sid == iso_pi(sk)->bc_sid;
1464f764a6c2SLuiz Augusto von Dentz }
1465f764a6c2SLuiz Augusto von Dentz 
1466f764a6c2SLuiz Augusto von Dentz static bool iso_match_sync_handle(struct sock *sk, void *data)
1467f764a6c2SLuiz Augusto von Dentz {
1468f764a6c2SLuiz Augusto von Dentz 	struct hci_evt_le_big_info_adv_report *ev = data;
1469f764a6c2SLuiz Augusto von Dentz 
1470f764a6c2SLuiz Augusto von Dentz 	return le16_to_cpu(ev->sync_handle) == iso_pi(sk)->sync_handle;
1471f764a6c2SLuiz Augusto von Dentz }
1472f764a6c2SLuiz Augusto von Dentz 
1473ccf74f23SLuiz Augusto von Dentz /* ----- ISO interface with lower layer (HCI) ----- */
1474f764a6c2SLuiz Augusto von Dentz 
1475ccf74f23SLuiz Augusto von Dentz int iso_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
1476ccf74f23SLuiz Augusto von Dentz {
1477f764a6c2SLuiz Augusto von Dentz 	struct hci_ev_le_pa_sync_established *ev1;
1478f764a6c2SLuiz Augusto von Dentz 	struct hci_evt_le_big_info_adv_report *ev2;
1479ccf74f23SLuiz Augusto von Dentz 	struct sock *sk;
1480ccf74f23SLuiz Augusto von Dentz 	int lm = 0;
1481ccf74f23SLuiz Augusto von Dentz 
1482f764a6c2SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
1483ccf74f23SLuiz Augusto von Dentz 
1484f764a6c2SLuiz Augusto von Dentz 	/* Broadcast receiver requires handling of some events before it can
1485f764a6c2SLuiz Augusto von Dentz 	 * proceed to establishing a BIG sync:
1486f764a6c2SLuiz Augusto von Dentz 	 *
1487f764a6c2SLuiz Augusto von Dentz 	 * 1. HCI_EV_LE_PA_SYNC_ESTABLISHED: The socket may specify a specific
1488f764a6c2SLuiz Augusto von Dentz 	 * SID to listen to and once sync is estabilished its handle needs to
1489f764a6c2SLuiz Augusto von Dentz 	 * be stored in iso_pi(sk)->sync_handle so it can be matched once
1490f764a6c2SLuiz Augusto von Dentz 	 * receiving the BIG Info.
1491f764a6c2SLuiz Augusto von Dentz 	 * 2. HCI_EVT_LE_BIG_INFO_ADV_REPORT: When connect_ind is triggered by a
1492f764a6c2SLuiz Augusto von Dentz 	 * a BIG Info it attempts to check if there any listening socket with
1493f764a6c2SLuiz Augusto von Dentz 	 * the same sync_handle and if it does then attempt to create a sync.
1494f764a6c2SLuiz Augusto von Dentz 	 */
1495f764a6c2SLuiz Augusto von Dentz 	ev1 = hci_recv_event_data(hdev, HCI_EV_LE_PA_SYNC_ESTABLISHED);
1496f764a6c2SLuiz Augusto von Dentz 	if (ev1) {
1497f764a6c2SLuiz Augusto von Dentz 		sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr, iso_match_sid,
1498f764a6c2SLuiz Augusto von Dentz 					 ev1);
1499f764a6c2SLuiz Augusto von Dentz 		if (sk)
1500f764a6c2SLuiz Augusto von Dentz 			iso_pi(sk)->sync_handle = le16_to_cpu(ev1->handle);
1501ccf74f23SLuiz Augusto von Dentz 
1502f764a6c2SLuiz Augusto von Dentz 		goto done;
1503f764a6c2SLuiz Augusto von Dentz 	}
1504f764a6c2SLuiz Augusto von Dentz 
1505f764a6c2SLuiz Augusto von Dentz 	ev2 = hci_recv_event_data(hdev, HCI_EVT_LE_BIG_INFO_ADV_REPORT);
1506f764a6c2SLuiz Augusto von Dentz 	if (ev2) {
1507f764a6c2SLuiz Augusto von Dentz 		sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr,
1508f764a6c2SLuiz Augusto von Dentz 					 iso_match_sync_handle, ev2);
1509f764a6c2SLuiz Augusto von Dentz 		if (sk) {
1510f764a6c2SLuiz Augusto von Dentz 			int err;
1511f764a6c2SLuiz Augusto von Dentz 
1512f764a6c2SLuiz Augusto von Dentz 			if (ev2->num_bis < iso_pi(sk)->bc_num_bis)
1513f764a6c2SLuiz Augusto von Dentz 				iso_pi(sk)->bc_num_bis = ev2->num_bis;
1514f764a6c2SLuiz Augusto von Dentz 
1515f764a6c2SLuiz Augusto von Dentz 			err = hci_le_big_create_sync(hdev,
1516f764a6c2SLuiz Augusto von Dentz 						     &iso_pi(sk)->qos,
1517f764a6c2SLuiz Augusto von Dentz 						     iso_pi(sk)->sync_handle,
1518f764a6c2SLuiz Augusto von Dentz 						     iso_pi(sk)->bc_num_bis,
1519f764a6c2SLuiz Augusto von Dentz 						     iso_pi(sk)->bc_bis);
1520f764a6c2SLuiz Augusto von Dentz 			if (err) {
1521f764a6c2SLuiz Augusto von Dentz 				bt_dev_err(hdev, "hci_le_big_create_sync: %d",
1522f764a6c2SLuiz Augusto von Dentz 					   err);
1523f764a6c2SLuiz Augusto von Dentz 				sk = NULL;
1524f764a6c2SLuiz Augusto von Dentz 			}
1525f764a6c2SLuiz Augusto von Dentz 		}
1526f764a6c2SLuiz Augusto von Dentz 	} else {
1527f764a6c2SLuiz Augusto von Dentz 		sk = iso_get_sock_listen(&hdev->bdaddr, BDADDR_ANY, NULL, NULL);
1528f764a6c2SLuiz Augusto von Dentz 	}
1529f764a6c2SLuiz Augusto von Dentz 
1530f764a6c2SLuiz Augusto von Dentz done:
1531f764a6c2SLuiz Augusto von Dentz 	if (!sk)
1532f764a6c2SLuiz Augusto von Dentz 		return lm;
1533f764a6c2SLuiz Augusto von Dentz 
1534ccf74f23SLuiz Augusto von Dentz 	lm |= HCI_LM_ACCEPT;
1535ccf74f23SLuiz Augusto von Dentz 
1536ccf74f23SLuiz Augusto von Dentz 	if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
1537ccf74f23SLuiz Augusto von Dentz 		*flags |= HCI_PROTO_DEFER;
1538ccf74f23SLuiz Augusto von Dentz 
1539ccf74f23SLuiz Augusto von Dentz 	return lm;
1540ccf74f23SLuiz Augusto von Dentz }
1541ccf74f23SLuiz Augusto von Dentz 
1542ccf74f23SLuiz Augusto von Dentz static void iso_connect_cfm(struct hci_conn *hcon, __u8 status)
1543ccf74f23SLuiz Augusto von Dentz {
1544ccf74f23SLuiz Augusto von Dentz 	if (hcon->type != ISO_LINK) {
1545ccf74f23SLuiz Augusto von Dentz 		if (hcon->type != LE_LINK)
1546ccf74f23SLuiz Augusto von Dentz 			return;
1547ccf74f23SLuiz Augusto von Dentz 
1548ccf74f23SLuiz Augusto von Dentz 		/* Check if LE link has failed */
1549ccf74f23SLuiz Augusto von Dentz 		if (status) {
1550ccf74f23SLuiz Augusto von Dentz 			if (hcon->link)
1551ccf74f23SLuiz Augusto von Dentz 				iso_conn_del(hcon->link, bt_to_errno(status));
1552ccf74f23SLuiz Augusto von Dentz 			return;
1553ccf74f23SLuiz Augusto von Dentz 		}
1554ccf74f23SLuiz Augusto von Dentz 
1555ccf74f23SLuiz Augusto von Dentz 		/* Create CIS if pending */
1556ccf74f23SLuiz Augusto von Dentz 		hci_le_create_cis(hcon);
1557ccf74f23SLuiz Augusto von Dentz 		return;
1558ccf74f23SLuiz Augusto von Dentz 	}
1559ccf74f23SLuiz Augusto von Dentz 
1560ccf74f23SLuiz Augusto von Dentz 	BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
1561ccf74f23SLuiz Augusto von Dentz 
1562ccf74f23SLuiz Augusto von Dentz 	if (!status) {
1563ccf74f23SLuiz Augusto von Dentz 		struct iso_conn *conn;
1564ccf74f23SLuiz Augusto von Dentz 
1565ccf74f23SLuiz Augusto von Dentz 		conn = iso_conn_add(hcon);
1566ccf74f23SLuiz Augusto von Dentz 		if (conn)
1567ccf74f23SLuiz Augusto von Dentz 			iso_conn_ready(conn);
1568ccf74f23SLuiz Augusto von Dentz 	} else {
1569ccf74f23SLuiz Augusto von Dentz 		iso_conn_del(hcon, bt_to_errno(status));
1570ccf74f23SLuiz Augusto von Dentz 	}
1571ccf74f23SLuiz Augusto von Dentz }
1572ccf74f23SLuiz Augusto von Dentz 
1573ccf74f23SLuiz Augusto von Dentz static void iso_disconn_cfm(struct hci_conn *hcon, __u8 reason)
1574ccf74f23SLuiz Augusto von Dentz {
1575ccf74f23SLuiz Augusto von Dentz 	if (hcon->type != ISO_LINK)
1576ccf74f23SLuiz Augusto von Dentz 		return;
1577ccf74f23SLuiz Augusto von Dentz 
1578ccf74f23SLuiz Augusto von Dentz 	BT_DBG("hcon %p reason %d", hcon, reason);
1579ccf74f23SLuiz Augusto von Dentz 
1580ccf74f23SLuiz Augusto von Dentz 	iso_conn_del(hcon, bt_to_errno(reason));
1581ccf74f23SLuiz Augusto von Dentz }
1582ccf74f23SLuiz Augusto von Dentz 
1583ccf74f23SLuiz Augusto von Dentz void iso_recv(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
1584ccf74f23SLuiz Augusto von Dentz {
1585ccf74f23SLuiz Augusto von Dentz 	struct iso_conn *conn = hcon->iso_data;
1586ccf74f23SLuiz Augusto von Dentz 	struct hci_iso_data_hdr *hdr;
1587ccf74f23SLuiz Augusto von Dentz 	__u16 pb, ts, len;
1588ccf74f23SLuiz Augusto von Dentz 
1589ccf74f23SLuiz Augusto von Dentz 	if (!conn)
1590ccf74f23SLuiz Augusto von Dentz 		goto drop;
1591ccf74f23SLuiz Augusto von Dentz 
1592ccf74f23SLuiz Augusto von Dentz 	pb     = hci_iso_flags_pb(flags);
1593ccf74f23SLuiz Augusto von Dentz 	ts     = hci_iso_flags_ts(flags);
1594ccf74f23SLuiz Augusto von Dentz 
1595ccf74f23SLuiz Augusto von Dentz 	BT_DBG("conn %p len %d pb 0x%x ts 0x%x", conn, skb->len, pb, ts);
1596ccf74f23SLuiz Augusto von Dentz 
1597ccf74f23SLuiz Augusto von Dentz 	switch (pb) {
1598ccf74f23SLuiz Augusto von Dentz 	case ISO_START:
1599ccf74f23SLuiz Augusto von Dentz 	case ISO_SINGLE:
1600ccf74f23SLuiz Augusto von Dentz 		if (conn->rx_len) {
1601ccf74f23SLuiz Augusto von Dentz 			BT_ERR("Unexpected start frame (len %d)", skb->len);
1602ccf74f23SLuiz Augusto von Dentz 			kfree_skb(conn->rx_skb);
1603ccf74f23SLuiz Augusto von Dentz 			conn->rx_skb = NULL;
1604ccf74f23SLuiz Augusto von Dentz 			conn->rx_len = 0;
1605ccf74f23SLuiz Augusto von Dentz 		}
1606ccf74f23SLuiz Augusto von Dentz 
1607ccf74f23SLuiz Augusto von Dentz 		if (ts) {
1608ccf74f23SLuiz Augusto von Dentz 			/* TODO: add timestamp to the packet? */
1609ccf74f23SLuiz Augusto von Dentz 			hdr = skb_pull_data(skb, HCI_ISO_TS_DATA_HDR_SIZE);
1610ccf74f23SLuiz Augusto von Dentz 			if (!hdr) {
1611ccf74f23SLuiz Augusto von Dentz 				BT_ERR("Frame is too short (len %d)", skb->len);
1612ccf74f23SLuiz Augusto von Dentz 				goto drop;
1613ccf74f23SLuiz Augusto von Dentz 			}
1614ccf74f23SLuiz Augusto von Dentz 
1615ccf74f23SLuiz Augusto von Dentz 		} else {
1616ccf74f23SLuiz Augusto von Dentz 			hdr = skb_pull_data(skb, HCI_ISO_DATA_HDR_SIZE);
1617ccf74f23SLuiz Augusto von Dentz 			if (!hdr) {
1618ccf74f23SLuiz Augusto von Dentz 				BT_ERR("Frame is too short (len %d)", skb->len);
1619ccf74f23SLuiz Augusto von Dentz 				goto drop;
1620ccf74f23SLuiz Augusto von Dentz 			}
1621ccf74f23SLuiz Augusto von Dentz 		}
1622ccf74f23SLuiz Augusto von Dentz 
1623ccf74f23SLuiz Augusto von Dentz 		len    = __le16_to_cpu(hdr->slen);
1624ccf74f23SLuiz Augusto von Dentz 		flags  = hci_iso_data_flags(len);
1625ccf74f23SLuiz Augusto von Dentz 		len    = hci_iso_data_len(len);
1626ccf74f23SLuiz Augusto von Dentz 
1627ccf74f23SLuiz Augusto von Dentz 		BT_DBG("Start: total len %d, frag len %d flags 0x%4.4x", len,
1628ccf74f23SLuiz Augusto von Dentz 		       skb->len, flags);
1629ccf74f23SLuiz Augusto von Dentz 
1630ccf74f23SLuiz Augusto von Dentz 		if (len == skb->len) {
1631ccf74f23SLuiz Augusto von Dentz 			/* Complete frame received */
1632ccf74f23SLuiz Augusto von Dentz 			iso_recv_frame(conn, skb);
1633ccf74f23SLuiz Augusto von Dentz 			return;
1634ccf74f23SLuiz Augusto von Dentz 		}
1635ccf74f23SLuiz Augusto von Dentz 
1636ccf74f23SLuiz Augusto von Dentz 		if (pb == ISO_SINGLE) {
1637ccf74f23SLuiz Augusto von Dentz 			BT_ERR("Frame malformed (len %d, expected len %d)",
1638ccf74f23SLuiz Augusto von Dentz 			       skb->len, len);
1639ccf74f23SLuiz Augusto von Dentz 			goto drop;
1640ccf74f23SLuiz Augusto von Dentz 		}
1641ccf74f23SLuiz Augusto von Dentz 
1642ccf74f23SLuiz Augusto von Dentz 		if (skb->len > len) {
1643ccf74f23SLuiz Augusto von Dentz 			BT_ERR("Frame is too long (len %d, expected len %d)",
1644ccf74f23SLuiz Augusto von Dentz 			       skb->len, len);
1645ccf74f23SLuiz Augusto von Dentz 			goto drop;
1646ccf74f23SLuiz Augusto von Dentz 		}
1647ccf74f23SLuiz Augusto von Dentz 
1648ccf74f23SLuiz Augusto von Dentz 		/* Allocate skb for the complete frame (with header) */
1649ccf74f23SLuiz Augusto von Dentz 		conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL);
1650ccf74f23SLuiz Augusto von Dentz 		if (!conn->rx_skb)
1651ccf74f23SLuiz Augusto von Dentz 			goto drop;
1652ccf74f23SLuiz Augusto von Dentz 
1653ccf74f23SLuiz Augusto von Dentz 		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
1654ccf74f23SLuiz Augusto von Dentz 					  skb->len);
1655ccf74f23SLuiz Augusto von Dentz 		conn->rx_len = len - skb->len;
1656ccf74f23SLuiz Augusto von Dentz 		break;
1657ccf74f23SLuiz Augusto von Dentz 
1658ccf74f23SLuiz Augusto von Dentz 	case ISO_CONT:
1659ccf74f23SLuiz Augusto von Dentz 		BT_DBG("Cont: frag len %d (expecting %d)", skb->len,
1660ccf74f23SLuiz Augusto von Dentz 		       conn->rx_len);
1661ccf74f23SLuiz Augusto von Dentz 
1662ccf74f23SLuiz Augusto von Dentz 		if (!conn->rx_len) {
1663ccf74f23SLuiz Augusto von Dentz 			BT_ERR("Unexpected continuation frame (len %d)",
1664ccf74f23SLuiz Augusto von Dentz 			       skb->len);
1665ccf74f23SLuiz Augusto von Dentz 			goto drop;
1666ccf74f23SLuiz Augusto von Dentz 		}
1667ccf74f23SLuiz Augusto von Dentz 
1668ccf74f23SLuiz Augusto von Dentz 		if (skb->len > conn->rx_len) {
1669ccf74f23SLuiz Augusto von Dentz 			BT_ERR("Fragment is too long (len %d, expected %d)",
1670ccf74f23SLuiz Augusto von Dentz 			       skb->len, conn->rx_len);
1671ccf74f23SLuiz Augusto von Dentz 			kfree_skb(conn->rx_skb);
1672ccf74f23SLuiz Augusto von Dentz 			conn->rx_skb = NULL;
1673ccf74f23SLuiz Augusto von Dentz 			conn->rx_len = 0;
1674ccf74f23SLuiz Augusto von Dentz 			goto drop;
1675ccf74f23SLuiz Augusto von Dentz 		}
1676ccf74f23SLuiz Augusto von Dentz 
1677ccf74f23SLuiz Augusto von Dentz 		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
1678ccf74f23SLuiz Augusto von Dentz 					  skb->len);
1679ccf74f23SLuiz Augusto von Dentz 		conn->rx_len -= skb->len;
1680ccf74f23SLuiz Augusto von Dentz 		return;
1681ccf74f23SLuiz Augusto von Dentz 
1682ccf74f23SLuiz Augusto von Dentz 	case ISO_END:
1683ccf74f23SLuiz Augusto von Dentz 		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
1684ccf74f23SLuiz Augusto von Dentz 					  skb->len);
1685ccf74f23SLuiz Augusto von Dentz 		conn->rx_len -= skb->len;
1686ccf74f23SLuiz Augusto von Dentz 
1687ccf74f23SLuiz Augusto von Dentz 		if (!conn->rx_len) {
1688ccf74f23SLuiz Augusto von Dentz 			struct sk_buff *rx_skb = conn->rx_skb;
1689ccf74f23SLuiz Augusto von Dentz 
1690ccf74f23SLuiz Augusto von Dentz 			/* Complete frame received. iso_recv_frame
1691ccf74f23SLuiz Augusto von Dentz 			 * takes ownership of the skb so set the global
1692ccf74f23SLuiz Augusto von Dentz 			 * rx_skb pointer to NULL first.
1693ccf74f23SLuiz Augusto von Dentz 			 */
1694ccf74f23SLuiz Augusto von Dentz 			conn->rx_skb = NULL;
1695ccf74f23SLuiz Augusto von Dentz 			iso_recv_frame(conn, rx_skb);
1696ccf74f23SLuiz Augusto von Dentz 		}
1697ccf74f23SLuiz Augusto von Dentz 		break;
1698ccf74f23SLuiz Augusto von Dentz 	}
1699ccf74f23SLuiz Augusto von Dentz 
1700ccf74f23SLuiz Augusto von Dentz drop:
1701ccf74f23SLuiz Augusto von Dentz 	kfree_skb(skb);
1702ccf74f23SLuiz Augusto von Dentz }
1703ccf74f23SLuiz Augusto von Dentz 
1704ccf74f23SLuiz Augusto von Dentz static struct hci_cb iso_cb = {
1705ccf74f23SLuiz Augusto von Dentz 	.name		= "ISO",
1706ccf74f23SLuiz Augusto von Dentz 	.connect_cfm	= iso_connect_cfm,
1707ccf74f23SLuiz Augusto von Dentz 	.disconn_cfm	= iso_disconn_cfm,
1708ccf74f23SLuiz Augusto von Dentz };
1709ccf74f23SLuiz Augusto von Dentz 
1710ccf74f23SLuiz Augusto von Dentz static int iso_debugfs_show(struct seq_file *f, void *p)
1711ccf74f23SLuiz Augusto von Dentz {
1712ccf74f23SLuiz Augusto von Dentz 	struct sock *sk;
1713ccf74f23SLuiz Augusto von Dentz 
1714ccf74f23SLuiz Augusto von Dentz 	read_lock(&iso_sk_list.lock);
1715ccf74f23SLuiz Augusto von Dentz 
1716ccf74f23SLuiz Augusto von Dentz 	sk_for_each(sk, &iso_sk_list.head) {
1717ccf74f23SLuiz Augusto von Dentz 		seq_printf(f, "%pMR %pMR %d\n", &iso_pi(sk)->src,
1718ccf74f23SLuiz Augusto von Dentz 			   &iso_pi(sk)->dst, sk->sk_state);
1719ccf74f23SLuiz Augusto von Dentz 	}
1720ccf74f23SLuiz Augusto von Dentz 
1721ccf74f23SLuiz Augusto von Dentz 	read_unlock(&iso_sk_list.lock);
1722ccf74f23SLuiz Augusto von Dentz 
1723ccf74f23SLuiz Augusto von Dentz 	return 0;
1724ccf74f23SLuiz Augusto von Dentz }
1725ccf74f23SLuiz Augusto von Dentz 
1726ccf74f23SLuiz Augusto von Dentz DEFINE_SHOW_ATTRIBUTE(iso_debugfs);
1727ccf74f23SLuiz Augusto von Dentz 
1728ccf74f23SLuiz Augusto von Dentz static struct dentry *iso_debugfs;
1729ccf74f23SLuiz Augusto von Dentz 
1730ccf74f23SLuiz Augusto von Dentz static const struct proto_ops iso_sock_ops = {
1731ccf74f23SLuiz Augusto von Dentz 	.family		= PF_BLUETOOTH,
1732ccf74f23SLuiz Augusto von Dentz 	.owner		= THIS_MODULE,
1733ccf74f23SLuiz Augusto von Dentz 	.release	= iso_sock_release,
1734ccf74f23SLuiz Augusto von Dentz 	.bind		= iso_sock_bind,
1735ccf74f23SLuiz Augusto von Dentz 	.connect	= iso_sock_connect,
1736ccf74f23SLuiz Augusto von Dentz 	.listen		= iso_sock_listen,
1737ccf74f23SLuiz Augusto von Dentz 	.accept		= iso_sock_accept,
1738ccf74f23SLuiz Augusto von Dentz 	.getname	= iso_sock_getname,
1739ccf74f23SLuiz Augusto von Dentz 	.sendmsg	= iso_sock_sendmsg,
1740ccf74f23SLuiz Augusto von Dentz 	.recvmsg	= iso_sock_recvmsg,
1741ccf74f23SLuiz Augusto von Dentz 	.poll		= bt_sock_poll,
1742ccf74f23SLuiz Augusto von Dentz 	.ioctl		= bt_sock_ioctl,
1743ccf74f23SLuiz Augusto von Dentz 	.mmap		= sock_no_mmap,
1744ccf74f23SLuiz Augusto von Dentz 	.socketpair	= sock_no_socketpair,
1745ccf74f23SLuiz Augusto von Dentz 	.shutdown	= iso_sock_shutdown,
1746ccf74f23SLuiz Augusto von Dentz 	.setsockopt	= iso_sock_setsockopt,
1747ccf74f23SLuiz Augusto von Dentz 	.getsockopt	= iso_sock_getsockopt
1748ccf74f23SLuiz Augusto von Dentz };
1749ccf74f23SLuiz Augusto von Dentz 
1750ccf74f23SLuiz Augusto von Dentz static const struct net_proto_family iso_sock_family_ops = {
1751ccf74f23SLuiz Augusto von Dentz 	.family	= PF_BLUETOOTH,
1752ccf74f23SLuiz Augusto von Dentz 	.owner	= THIS_MODULE,
1753ccf74f23SLuiz Augusto von Dentz 	.create	= iso_sock_create,
1754ccf74f23SLuiz Augusto von Dentz };
1755ccf74f23SLuiz Augusto von Dentz 
1756ccf74f23SLuiz Augusto von Dentz static bool iso_inited;
1757ccf74f23SLuiz Augusto von Dentz 
1758ccf74f23SLuiz Augusto von Dentz bool iso_enabled(void)
1759ccf74f23SLuiz Augusto von Dentz {
1760ccf74f23SLuiz Augusto von Dentz 	return iso_inited;
1761ccf74f23SLuiz Augusto von Dentz }
1762ccf74f23SLuiz Augusto von Dentz 
1763ccf74f23SLuiz Augusto von Dentz int iso_init(void)
1764ccf74f23SLuiz Augusto von Dentz {
1765ccf74f23SLuiz Augusto von Dentz 	int err;
1766ccf74f23SLuiz Augusto von Dentz 
1767ccf74f23SLuiz Augusto von Dentz 	BUILD_BUG_ON(sizeof(struct sockaddr_iso) > sizeof(struct sockaddr));
1768ccf74f23SLuiz Augusto von Dentz 
1769ccf74f23SLuiz Augusto von Dentz 	if (iso_inited)
1770ccf74f23SLuiz Augusto von Dentz 		return -EALREADY;
1771ccf74f23SLuiz Augusto von Dentz 
1772ccf74f23SLuiz Augusto von Dentz 	err = proto_register(&iso_proto, 0);
1773ccf74f23SLuiz Augusto von Dentz 	if (err < 0)
1774ccf74f23SLuiz Augusto von Dentz 		return err;
1775ccf74f23SLuiz Augusto von Dentz 
1776ccf74f23SLuiz Augusto von Dentz 	err = bt_sock_register(BTPROTO_ISO, &iso_sock_family_ops);
1777ccf74f23SLuiz Augusto von Dentz 	if (err < 0) {
1778ccf74f23SLuiz Augusto von Dentz 		BT_ERR("ISO socket registration failed");
1779ccf74f23SLuiz Augusto von Dentz 		goto error;
1780ccf74f23SLuiz Augusto von Dentz 	}
1781ccf74f23SLuiz Augusto von Dentz 
1782ccf74f23SLuiz Augusto von Dentz 	err = bt_procfs_init(&init_net, "iso", &iso_sk_list, NULL);
1783ccf74f23SLuiz Augusto von Dentz 	if (err < 0) {
1784ccf74f23SLuiz Augusto von Dentz 		BT_ERR("Failed to create ISO proc file");
1785ccf74f23SLuiz Augusto von Dentz 		bt_sock_unregister(BTPROTO_ISO);
1786ccf74f23SLuiz Augusto von Dentz 		goto error;
1787ccf74f23SLuiz Augusto von Dentz 	}
1788ccf74f23SLuiz Augusto von Dentz 
1789ccf74f23SLuiz Augusto von Dentz 	BT_INFO("ISO socket layer initialized");
1790ccf74f23SLuiz Augusto von Dentz 
1791ccf74f23SLuiz Augusto von Dentz 	hci_register_cb(&iso_cb);
1792ccf74f23SLuiz Augusto von Dentz 
1793ccf74f23SLuiz Augusto von Dentz 	if (IS_ERR_OR_NULL(bt_debugfs))
1794ccf74f23SLuiz Augusto von Dentz 		return 0;
1795ccf74f23SLuiz Augusto von Dentz 
1796ccf74f23SLuiz Augusto von Dentz 	if (!iso_debugfs) {
1797ccf74f23SLuiz Augusto von Dentz 		iso_debugfs = debugfs_create_file("iso", 0444, bt_debugfs,
1798ccf74f23SLuiz Augusto von Dentz 						  NULL, &iso_debugfs_fops);
1799ccf74f23SLuiz Augusto von Dentz 	}
1800ccf74f23SLuiz Augusto von Dentz 
1801ccf74f23SLuiz Augusto von Dentz 	iso_inited = true;
1802ccf74f23SLuiz Augusto von Dentz 
1803ccf74f23SLuiz Augusto von Dentz 	return 0;
1804ccf74f23SLuiz Augusto von Dentz 
1805ccf74f23SLuiz Augusto von Dentz error:
1806ccf74f23SLuiz Augusto von Dentz 	proto_unregister(&iso_proto);
1807ccf74f23SLuiz Augusto von Dentz 	return err;
1808ccf74f23SLuiz Augusto von Dentz }
1809ccf74f23SLuiz Augusto von Dentz 
1810ccf74f23SLuiz Augusto von Dentz int iso_exit(void)
1811ccf74f23SLuiz Augusto von Dentz {
1812ccf74f23SLuiz Augusto von Dentz 	if (!iso_inited)
1813ccf74f23SLuiz Augusto von Dentz 		return -EALREADY;
1814ccf74f23SLuiz Augusto von Dentz 
1815ccf74f23SLuiz Augusto von Dentz 	bt_procfs_cleanup(&init_net, "iso");
1816ccf74f23SLuiz Augusto von Dentz 
1817ccf74f23SLuiz Augusto von Dentz 	debugfs_remove(iso_debugfs);
1818ccf74f23SLuiz Augusto von Dentz 	iso_debugfs = NULL;
1819ccf74f23SLuiz Augusto von Dentz 
1820ccf74f23SLuiz Augusto von Dentz 	hci_unregister_cb(&iso_cb);
1821ccf74f23SLuiz Augusto von Dentz 
1822ccf74f23SLuiz Augusto von Dentz 	bt_sock_unregister(BTPROTO_ISO);
1823ccf74f23SLuiz Augusto von Dentz 
1824ccf74f23SLuiz Augusto von Dentz 	proto_unregister(&iso_proto);
1825ccf74f23SLuiz Augusto von Dentz 
1826ccf74f23SLuiz Augusto von Dentz 	iso_inited = false;
1827ccf74f23SLuiz Augusto von Dentz 
1828ccf74f23SLuiz Augusto von Dentz 	return 0;
1829ccf74f23SLuiz Augusto von Dentz }
1830