blob: 9e62102443dc79a408feed50d9db48576422fd5c [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
4
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
10
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090015 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090020 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth SCO sockets. */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
Marcel Holtmannaef7d972010-03-21 05:27:45 +010028#include <linux/debugfs.h>
29#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <net/bluetooth/bluetooth.h>
32#include <net/bluetooth/hci_core.h>
33#include <net/bluetooth/sco.h>
34
Rusty Russelleb939922011-12-19 14:08:01 +000035static bool disable_esco;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080037static const struct proto_ops sco_sock_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39static struct bt_sock_list sco_sk_list = {
Robert P. J. Dayd5fb2962008-03-28 16:17:38 -070040 .lock = __RW_LOCK_UNLOCKED(sco_sk_list.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041};
42
43static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent);
44static void sco_chan_del(struct sock *sk, int err);
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static void sco_sock_close(struct sock *sk);
47static void sco_sock_kill(struct sock *sk);
48
49/* ---- SCO timers ---- */
50static void sco_sock_timeout(unsigned long arg)
51{
52 struct sock *sk = (struct sock *) arg;
53
54 BT_DBG("sock %p state %d", sk, sk->sk_state);
55
56 bh_lock_sock(sk);
57 sk->sk_err = ETIMEDOUT;
58 sk->sk_state_change(sk);
59 bh_unlock_sock(sk);
60
61 sco_sock_kill(sk);
62 sock_put(sk);
63}
64
65static void sco_sock_set_timer(struct sock *sk, long timeout)
66{
67 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
68 sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
69}
70
71static void sco_sock_clear_timer(struct sock *sk)
72{
73 BT_DBG("sock %p state %d", sk, sk->sk_state);
74 sk_stop_timer(sk, &sk->sk_timer);
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* ---- SCO connections ---- */
Lukasz Rymanowski519e42b2012-04-19 16:12:28 +020078static struct sco_conn *sco_conn_add(struct hci_conn *hcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 struct hci_dev *hdev = hcon->hdev;
Marcel Holtmann25ea6db2006-07-06 15:40:09 +020081 struct sco_conn *conn = hcon->sco_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Lukasz Rymanowski519e42b2012-04-19 16:12:28 +020083 if (conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return conn;
85
Claudio Takahasic10cc5a2013-04-11 11:35:45 -030086 conn = kzalloc(sizeof(struct sco_conn), GFP_KERNEL);
Marcel Holtmann25ea6db2006-07-06 15:40:09 +020087 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 spin_lock_init(&conn->lock);
91
92 hcon->sco_data = conn;
93 conn->hcon = hcon;
94
95 conn->src = &hdev->bdaddr;
96 conn->dst = &hcon->dst;
97
98 if (hdev->sco_mtu > 0)
99 conn->mtu = hdev->sco_mtu;
100 else
101 conn->mtu = 60;
102
103 BT_DBG("hcon %p conn %p", hcon, conn);
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return conn;
106}
107
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300108static struct sock *sco_chan_get(struct sco_conn *conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 struct sock *sk = NULL;
111 sco_conn_lock(conn);
112 sk = conn->sk;
113 sco_conn_unlock(conn);
114 return sk;
115}
116
117static int sco_conn_del(struct hci_conn *hcon, int err)
118{
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200119 struct sco_conn *conn = hcon->sco_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 struct sock *sk;
121
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200122 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return 0;
124
125 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
126
127 /* Kill socket */
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200128 sk = sco_chan_get(conn);
129 if (sk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 bh_lock_sock(sk);
131 sco_sock_clear_timer(sk);
132 sco_chan_del(sk, err);
133 bh_unlock_sock(sk);
134 sco_sock_kill(sk);
135 }
136
137 hcon->sco_data = NULL;
138 kfree(conn);
139 return 0;
140}
141
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300142static int sco_chan_add(struct sco_conn *conn, struct sock *sk,
143 struct sock *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 int err = 0;
146
147 sco_conn_lock(conn);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300148 if (conn->sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 err = -EBUSY;
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300150 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 __sco_chan_add(conn, sk, parent);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 sco_conn_unlock(conn);
154 return err;
155}
156
157static int sco_connect(struct sock *sk)
158{
159 bdaddr_t *src = &bt_sk(sk)->src;
160 bdaddr_t *dst = &bt_sk(sk)->dst;
161 struct sco_conn *conn;
162 struct hci_conn *hcon;
163 struct hci_dev *hdev;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200164 int err, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +0300166 BT_DBG("%pMR -> %pMR", src, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200168 hdev = hci_get_route(dst, src);
169 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return -EHOSTUNREACH;
171
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300172 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Marcel Holtmann7cb127d2008-07-14 20:13:53 +0200174 if (lmp_esco_capable(hdev) && !disable_esco)
175 type = ESCO_LINK;
176 else
177 type = SCO_LINK;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200178
Andre Guedesb12f62c2012-04-24 21:02:54 -0300179 hcon = hci_connect(hdev, type, dst, BDADDR_BREDR, BT_SECURITY_LOW,
180 HCI_AT_NO_BONDING);
Ville Tervo30e76272011-02-22 16:10:53 -0300181 if (IS_ERR(hcon)) {
182 err = PTR_ERR(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 goto done;
Ville Tervo30e76272011-02-22 16:10:53 -0300184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Lukasz Rymanowski519e42b2012-04-19 16:12:28 +0200186 conn = sco_conn_add(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (!conn) {
David Herrmann76a68ba2013-04-06 20:28:37 +0200188 hci_conn_drop(hcon);
Ville Tervo30e76272011-02-22 16:10:53 -0300189 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 goto done;
191 }
192
193 /* Update source addr of the socket */
194 bacpy(src, conn->src);
195
196 err = sco_chan_add(conn, sk, NULL);
197 if (err)
198 goto done;
199
200 if (hcon->state == BT_CONNECTED) {
201 sco_sock_clear_timer(sk);
202 sk->sk_state = BT_CONNECTED;
203 } else {
204 sk->sk_state = BT_CONNECT;
205 sco_sock_set_timer(sk, sk->sk_sndtimeo);
206 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208done:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300209 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 hci_dev_put(hdev);
211 return err;
212}
213
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300214static int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 struct sco_conn *conn = sco_pi(sk)->conn;
217 struct sk_buff *skb;
Mikel Astiz088ce082012-04-11 08:48:48 +0200218 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 /* Check outgoing MTU */
221 if (len > conn->mtu)
222 return -EINVAL;
223
224 BT_DBG("sk %p len %d", sk, len);
225
Mikel Astiz088ce082012-04-11 08:48:48 +0200226 skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300227 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return err;
229
Mikel Astiz088ce082012-04-11 08:48:48 +0200230 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300231 kfree_skb(skb);
232 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234
Gustavo F. Padovan0d861d82010-05-01 16:15:35 -0300235 hci_send_sco(conn->hcon, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Mikel Astiz088ce082012-04-11 08:48:48 +0200237 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Gustavo Padovan6039aa732012-05-23 04:04:18 -0300240static void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct sock *sk = sco_chan_get(conn);
243
244 if (!sk)
245 goto drop;
246
247 BT_DBG("sk %p len %d", sk, skb->len);
248
249 if (sk->sk_state != BT_CONNECTED)
250 goto drop;
251
252 if (!sock_queue_rcv_skb(sk, skb))
253 return;
254
255drop:
256 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
259/* -------- Socket interface ---------- */
Marcel Holtmannfb334052012-04-19 14:37:58 +0200260static struct sock *__sco_get_sock_listen_by_addr(bdaddr_t *ba)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Marcel Holtmannfb334052012-04-19 14:37:58 +0200262 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Sasha Levinb67bfe02013-02-27 17:06:00 -0800264 sk_for_each(sk, &sco_sk_list.head) {
Marcel Holtmannfb334052012-04-19 14:37:58 +0200265 if (sk->sk_state != BT_LISTEN)
266 continue;
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (!bacmp(&bt_sk(sk)->src, ba))
Marcel Holtmannfb334052012-04-19 14:37:58 +0200269 return sk;
270 }
271
272 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275/* Find socket listening on source bdaddr.
276 * Returns closest match.
277 */
278static struct sock *sco_get_sock_listen(bdaddr_t *src)
279{
280 struct sock *sk = NULL, *sk1 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 read_lock(&sco_sk_list.lock);
283
Sasha Levinb67bfe02013-02-27 17:06:00 -0800284 sk_for_each(sk, &sco_sk_list.head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (sk->sk_state != BT_LISTEN)
286 continue;
287
288 /* Exact match. */
289 if (!bacmp(&bt_sk(sk)->src, src))
290 break;
291
292 /* Closest match */
293 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
294 sk1 = sk;
295 }
296
297 read_unlock(&sco_sk_list.lock);
298
Sasha Levinb67bfe02013-02-27 17:06:00 -0800299 return sk ? sk : sk1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302static void sco_sock_destruct(struct sock *sk)
303{
304 BT_DBG("sk %p", sk);
305
306 skb_queue_purge(&sk->sk_receive_queue);
307 skb_queue_purge(&sk->sk_write_queue);
308}
309
310static void sco_sock_cleanup_listen(struct sock *parent)
311{
312 struct sock *sk;
313
314 BT_DBG("parent %p", parent);
315
316 /* Close not yet accepted channels */
317 while ((sk = bt_accept_dequeue(parent, NULL))) {
318 sco_sock_close(sk);
319 sco_sock_kill(sk);
320 }
321
322 parent->sk_state = BT_CLOSED;
323 sock_set_flag(parent, SOCK_ZAPPED);
324}
325
326/* Kill socket (only if zapped and orphan)
327 * Must be called on unlocked socket.
328 */
329static void sco_sock_kill(struct sock *sk)
330{
331 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
332 return;
333
334 BT_DBG("sk %p state %d", sk, sk->sk_state);
335
336 /* Kill poor orphan */
337 bt_sock_unlink(&sco_sk_list, sk);
338 sock_set_flag(sk, SOCK_DEAD);
339 sock_put(sk);
340}
341
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200342static void __sco_sock_close(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200344 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 switch (sk->sk_state) {
347 case BT_LISTEN:
348 sco_sock_cleanup_listen(sk);
349 break;
350
351 case BT_CONNECTED:
352 case BT_CONFIG:
Gustavo Padovanb7e98b52013-01-03 19:59:28 -0200353 if (sco_pi(sk)->conn->hcon) {
Luiz Augusto von Dentz4a777082011-05-12 11:13:15 +0300354 sk->sk_state = BT_DISCONN;
355 sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT);
David Herrmann76a68ba2013-04-06 20:28:37 +0200356 hci_conn_drop(sco_pi(sk)->conn->hcon);
Luiz Augusto von Dentz4a777082011-05-12 11:13:15 +0300357 sco_pi(sk)->conn->hcon = NULL;
358 } else
359 sco_chan_del(sk, ECONNRESET);
360 break;
361
Vinicius Costa Gomeseb20ff92013-03-13 19:46:20 -0300362 case BT_CONNECT2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 case BT_CONNECT:
364 case BT_DISCONN:
365 sco_chan_del(sk, ECONNRESET);
366 break;
367
368 default:
369 sock_set_flag(sk, SOCK_ZAPPED);
370 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700371 }
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200372}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200374/* Must be called on unlocked socket. */
375static void sco_sock_close(struct sock *sk)
376{
377 sco_sock_clear_timer(sk);
378 lock_sock(sk);
379 __sco_sock_close(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 sco_sock_kill(sk);
382}
383
384static void sco_sock_init(struct sock *sk, struct sock *parent)
385{
386 BT_DBG("sk %p", sk);
387
Paul Moore6230c9b2011-10-07 09:40:59 +0000388 if (parent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 sk->sk_type = parent->sk_type;
Frédéric Dalleau20714bf2012-11-21 10:51:12 +0100390 bt_sk(sk)->flags = bt_sk(parent)->flags;
Paul Moore6230c9b2011-10-07 09:40:59 +0000391 security_sk_clone(parent, sk);
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395static struct proto sco_proto = {
396 .name = "SCO",
397 .owner = THIS_MODULE,
398 .obj_size = sizeof(struct sco_pinfo)
399};
400
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700401static struct sock *sco_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 struct sock *sk;
404
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700405 sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (!sk)
407 return NULL;
408
409 sock_init_data(sock, sk);
410 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
411
412 sk->sk_destruct = sco_sock_destruct;
413 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
414
415 sock_reset_flag(sk, SOCK_ZAPPED);
416
417 sk->sk_protocol = proto;
418 sk->sk_state = BT_OPEN;
419
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800420 setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 bt_sock_link(&sco_sk_list, sk);
423 return sk;
424}
425
Eric Paris3f378b62009-11-05 22:18:14 -0800426static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
427 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 struct sock *sk;
430
431 BT_DBG("sock %p", sock);
432
433 sock->state = SS_UNCONNECTED;
434
435 if (sock->type != SOCK_SEQPACKET)
436 return -ESOCKTNOSUPPORT;
437
438 sock->ops = &sco_sock_ops;
439
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700440 sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC);
Marcel Holtmann74da6262006-10-15 17:31:14 +0200441 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return -ENOMEM;
443
444 sco_sock_init(sk, NULL);
445 return 0;
446}
447
448static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
449{
450 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
451 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 int err = 0;
453
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +0300454 BT_DBG("sk %p %pMR", sk, &sa->sco_bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 if (!addr || addr->sa_family != AF_BLUETOOTH)
457 return -EINVAL;
458
459 lock_sock(sk);
460
461 if (sk->sk_state != BT_OPEN) {
462 err = -EBADFD;
463 goto done;
464 }
465
Marcel Holtmann8ed21f72012-04-19 13:43:53 +0200466 if (sk->sk_type != SOCK_SEQPACKET) {
467 err = -EINVAL;
468 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470
Marcel Holtmann8ed21f72012-04-19 13:43:53 +0200471 bacpy(&bt_sk(sk)->src, &sa->sco_bdaddr);
472
473 sk->sk_state = BT_BOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475done:
476 release_sock(sk);
477 return err;
478}
479
480static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
481{
482 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
483 struct sock *sk = sock->sk;
Claudio Takahasi92f185c2013-04-11 11:35:46 -0300484 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 BT_DBG("sk %p", sk);
487
Changli Gao6503d962010-03-31 22:58:26 +0000488 if (alen < sizeof(struct sockaddr_sco) ||
489 addr->sa_family != AF_BLUETOOTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return -EINVAL;
491
492 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
493 return -EBADFD;
494
495 if (sk->sk_type != SOCK_SEQPACKET)
496 return -EINVAL;
497
498 lock_sock(sk);
499
500 /* Set destination address and psm */
501 bacpy(&bt_sk(sk)->dst, &sa->sco_bdaddr);
502
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200503 err = sco_connect(sk);
504 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 goto done;
506
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900507 err = bt_sock_wait_state(sk, BT_CONNECTED,
Gustavo Padovanbe7c2b92012-05-17 00:36:21 -0300508 sock_sndtimeo(sk, flags & O_NONBLOCK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510done:
511 release_sock(sk);
512 return err;
513}
514
515static int sco_sock_listen(struct socket *sock, int backlog)
516{
517 struct sock *sk = sock->sk;
Marcel Holtmannfb334052012-04-19 14:37:58 +0200518 bdaddr_t *src = &bt_sk(sk)->src;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 int err = 0;
520
521 BT_DBG("sk %p backlog %d", sk, backlog);
522
523 lock_sock(sk);
524
Marcel Holtmann7d5d7752012-04-19 13:43:52 +0200525 if (sk->sk_state != BT_BOUND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 err = -EBADFD;
527 goto done;
528 }
529
Marcel Holtmann7d5d7752012-04-19 13:43:52 +0200530 if (sk->sk_type != SOCK_SEQPACKET) {
531 err = -EINVAL;
532 goto done;
533 }
534
Marcel Holtmannfb334052012-04-19 14:37:58 +0200535 write_lock(&sco_sk_list.lock);
536
537 if (__sco_get_sock_listen_by_addr(src)) {
538 err = -EADDRINUSE;
539 goto unlock;
540 }
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 sk->sk_max_ack_backlog = backlog;
543 sk->sk_ack_backlog = 0;
Marcel Holtmannfb334052012-04-19 14:37:58 +0200544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 sk->sk_state = BT_LISTEN;
546
Marcel Holtmannfb334052012-04-19 14:37:58 +0200547unlock:
548 write_unlock(&sco_sk_list.lock);
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550done:
551 release_sock(sk);
552 return err;
553}
554
555static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
556{
557 DECLARE_WAITQUEUE(wait, current);
558 struct sock *sk = sock->sk, *ch;
559 long timeo;
560 int err = 0;
561
562 lock_sock(sk);
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
565
566 BT_DBG("sk %p timeo %ld", sk, timeo);
567
568 /* Wait for an incoming connection. (wake-one). */
Eric Dumazetaa395142010-04-20 13:03:51 +0000569 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Peter Hurley552b0d32011-07-24 00:11:01 -0400570 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 if (sk->sk_state != BT_LISTEN) {
574 err = -EBADFD;
575 break;
576 }
577
Peter Hurley552b0d32011-07-24 00:11:01 -0400578 ch = bt_accept_dequeue(sk, newsock);
579 if (ch)
580 break;
581
582 if (!timeo) {
583 err = -EAGAIN;
584 break;
585 }
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (signal_pending(current)) {
588 err = sock_intr_errno(timeo);
589 break;
590 }
Peter Hurley552b0d32011-07-24 00:11:01 -0400591
592 release_sock(sk);
593 timeo = schedule_timeout(timeo);
594 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Peter Hurley552b0d32011-07-24 00:11:01 -0400596 __set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000597 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 if (err)
600 goto done;
601
602 newsock->state = SS_CONNECTED;
603
604 BT_DBG("new socket %p", ch);
605
606done:
607 release_sock(sk);
608 return err;
609}
610
611static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
612{
613 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
614 struct sock *sk = sock->sk;
615
616 BT_DBG("sock %p, sk %p", sock, sk);
617
618 addr->sa_family = AF_BLUETOOTH;
619 *len = sizeof(struct sockaddr_sco);
620
621 if (peer)
622 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst);
623 else
624 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src);
625
626 return 0;
627}
628
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900629static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct msghdr *msg, size_t len)
631{
632 struct sock *sk = sock->sk;
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300633 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 BT_DBG("sock %p, sk %p", sock, sk);
636
Benjamin LaHaisec1cbe4b2005-12-13 23:22:19 -0800637 err = sock_error(sk);
638 if (err)
639 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 if (msg->msg_flags & MSG_OOB)
642 return -EOPNOTSUPP;
643
644 lock_sock(sk);
645
646 if (sk->sk_state == BT_CONNECTED)
647 err = sco_send_frame(sk, msg, len);
648 else
649 err = -ENOTCONN;
650
651 release_sock(sk);
652 return err;
653}
654
Frédéric Dalleaufa5513b2013-04-16 17:28:58 +0200655static void sco_conn_defer_accept(struct hci_conn *conn, int mask)
656{
657 struct hci_dev *hdev = conn->hdev;
658
659 BT_DBG("conn %p", conn);
660
661 conn->state = BT_CONFIG;
662
663 if (!lmp_esco_capable(hdev)) {
664 struct hci_cp_accept_conn_req cp;
665
666 bacpy(&cp.bdaddr, &conn->dst);
667
668 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
669 cp.role = 0x00; /* Become master */
670 else
671 cp.role = 0x01; /* Remain slave */
672
673 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
674 } else {
675 struct hci_cp_accept_sync_conn_req cp;
676
677 bacpy(&cp.bdaddr, &conn->dst);
678 cp.pkt_type = cpu_to_le16(conn->pkt_type);
679
680 cp.tx_bandwidth = __constant_cpu_to_le32(0x00001f40);
681 cp.rx_bandwidth = __constant_cpu_to_le32(0x00001f40);
682 cp.max_latency = __constant_cpu_to_le16(0xffff);
683 cp.content_format = cpu_to_le16(hdev->voice_setting);
684 cp.retrans_effort = 0xff;
685
686 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
687 sizeof(cp), &cp);
688 }
689}
690
Frédéric Dalleau20714bf2012-11-21 10:51:12 +0100691static int sco_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
692 struct msghdr *msg, size_t len, int flags)
693{
694 struct sock *sk = sock->sk;
695 struct sco_pinfo *pi = sco_pi(sk);
696
697 lock_sock(sk);
698
699 if (sk->sk_state == BT_CONNECT2 &&
700 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
Frédéric Dalleaufa5513b2013-04-16 17:28:58 +0200701 sco_conn_defer_accept(pi->conn->hcon, 0);
Frédéric Dalleau20714bf2012-11-21 10:51:12 +0100702 sk->sk_state = BT_CONFIG;
703
704 release_sock(sk);
705 return 0;
706 }
707
708 release_sock(sk);
709
710 return bt_sock_recvmsg(iocb, sock, msg, len, flags);
711}
712
David S. Millerb7058842009-09-30 16:12:20 -0700713static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 struct sock *sk = sock->sk;
716 int err = 0;
Frédéric Dalleaub96e9c62012-11-21 10:51:11 +0100717 u32 opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 BT_DBG("sk %p", sk);
720
721 lock_sock(sk);
722
723 switch (optname) {
Frédéric Dalleaub96e9c62012-11-21 10:51:11 +0100724
725 case BT_DEFER_SETUP:
726 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
727 err = -EINVAL;
728 break;
729 }
730
731 if (get_user(opt, (u32 __user *) optval)) {
732 err = -EFAULT;
733 break;
734 }
735
736 if (opt)
737 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
738 else
739 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
740 break;
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 default:
743 err = -ENOPROTOOPT;
744 break;
745 }
746
747 release_sock(sk);
748 return err;
749}
750
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100751static int sco_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 struct sock *sk = sock->sk;
754 struct sco_options opts;
755 struct sco_conninfo cinfo;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900756 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 BT_DBG("sk %p", sk);
759
760 if (get_user(len, optlen))
761 return -EFAULT;
762
763 lock_sock(sk);
764
765 switch (optname) {
766 case SCO_OPTIONS:
767 if (sk->sk_state != BT_CONNECTED) {
768 err = -ENOTCONN;
769 break;
770 }
771
772 opts.mtu = sco_pi(sk)->conn->mtu;
773
774 BT_DBG("mtu %d", opts.mtu);
775
776 len = min_t(unsigned int, len, sizeof(opts));
777 if (copy_to_user(optval, (char *)&opts, len))
778 err = -EFAULT;
779
780 break;
781
782 case SCO_CONNINFO:
783 if (sk->sk_state != BT_CONNECTED) {
784 err = -ENOTCONN;
785 break;
786 }
787
Vasiliy Kulikovc4c896e2011-02-14 13:54:26 +0300788 memset(&cinfo, 0, sizeof(cinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
790 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
791
792 len = min_t(unsigned int, len, sizeof(cinfo));
793 if (copy_to_user(optval, (char *)&cinfo, len))
794 err = -EFAULT;
795
796 break;
797
798 default:
799 err = -ENOPROTOOPT;
800 break;
801 }
802
803 release_sock(sk);
804 return err;
805}
806
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100807static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
808{
809 struct sock *sk = sock->sk;
810 int len, err = 0;
811
812 BT_DBG("sk %p", sk);
813
814 if (level == SOL_SCO)
815 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
816
817 if (get_user(len, optlen))
818 return -EFAULT;
819
820 lock_sock(sk);
821
822 switch (optname) {
Frédéric Dalleaub96e9c62012-11-21 10:51:11 +0100823
824 case BT_DEFER_SETUP:
825 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
826 err = -EINVAL;
827 break;
828 }
829
830 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
831 (u32 __user *) optval))
832 err = -EFAULT;
833
834 break;
835
Marcel Holtmannd58daf42009-01-15 21:52:14 +0100836 default:
837 err = -ENOPROTOOPT;
838 break;
839 }
840
841 release_sock(sk);
842 return err;
843}
844
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200845static int sco_sock_shutdown(struct socket *sock, int how)
846{
847 struct sock *sk = sock->sk;
848 int err = 0;
849
850 BT_DBG("sock %p, sk %p", sock, sk);
851
852 if (!sk)
853 return 0;
854
855 lock_sock(sk);
856 if (!sk->sk_shutdown) {
857 sk->sk_shutdown = SHUTDOWN_MASK;
858 sco_sock_clear_timer(sk);
859 __sco_sock_close(sk);
860
861 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
862 err = bt_sock_wait_state(sk, BT_CLOSED,
Gustavo Padovanbe7c2b92012-05-17 00:36:21 -0300863 sk->sk_lingertime);
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +0200864 }
865 release_sock(sk);
866 return err;
867}
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869static int sco_sock_release(struct socket *sock)
870{
871 struct sock *sk = sock->sk;
872 int err = 0;
873
874 BT_DBG("sock %p, sk %p", sock, sk);
875
876 if (!sk)
877 return 0;
878
879 sco_sock_close(sk);
880
881 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) {
882 lock_sock(sk);
883 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
884 release_sock(sk);
885 }
886
887 sock_orphan(sk);
888 sco_sock_kill(sk);
889 return err;
890}
891
892static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
893{
894 BT_DBG("conn %p", conn);
895
896 sco_pi(sk)->conn = conn;
897 conn->sk = sk;
898
899 if (parent)
900 bt_accept_enqueue(parent, sk);
901}
902
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900903/* Delete channel.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 * Must be called on the locked socket. */
905static void sco_chan_del(struct sock *sk, int err)
906{
907 struct sco_conn *conn;
908
909 conn = sco_pi(sk)->conn;
910
911 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
912
Gustavo Padovan0b27a4b2012-12-03 15:36:51 -0200913 if (conn) {
914 sco_conn_lock(conn);
915 conn->sk = NULL;
916 sco_pi(sk)->conn = NULL;
917 sco_conn_unlock(conn);
918
919 if (conn->hcon)
David Herrmann76a68ba2013-04-06 20:28:37 +0200920 hci_conn_drop(conn->hcon);
Gustavo Padovan0b27a4b2012-12-03 15:36:51 -0200921 }
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 sk->sk_state = BT_CLOSED;
924 sk->sk_err = err;
925 sk->sk_state_change(sk);
926
927 sock_set_flag(sk, SOCK_ZAPPED);
928}
929
930static void sco_conn_ready(struct sco_conn *conn)
931{
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200932 struct sock *parent;
933 struct sock *sk = conn->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 BT_DBG("conn %p", conn);
936
Andrei Emeltchenko735cbc42010-12-01 16:58:22 +0200937 if (sk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 sco_sock_clear_timer(sk);
939 bh_lock_sock(sk);
940 sk->sk_state = BT_CONNECTED;
941 sk->sk_state_change(sk);
942 bh_unlock_sock(sk);
943 } else {
Andre Guedes40528082013-01-29 19:59:56 -0300944 sco_conn_lock(conn);
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 parent = sco_get_sock_listen(conn->src);
Andre Guedes40528082013-01-29 19:59:56 -0300947 if (!parent) {
948 sco_conn_unlock(conn);
949 return;
950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 bh_lock_sock(parent);
953
Gustavo F. Padovanb9dbdbc2010-05-01 16:15:35 -0300954 sk = sco_sock_alloc(sock_net(parent), NULL,
Gustavo Padovanbe7c2b92012-05-17 00:36:21 -0300955 BTPROTO_SCO, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (!sk) {
957 bh_unlock_sock(parent);
Andre Guedes40528082013-01-29 19:59:56 -0300958 sco_conn_unlock(conn);
959 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961
962 sco_sock_init(sk, parent);
963
964 bacpy(&bt_sk(sk)->src, conn->src);
965 bacpy(&bt_sk(sk)->dst, conn->dst);
966
967 hci_conn_hold(conn->hcon);
968 __sco_chan_add(conn, sk, parent);
969
Frédéric Dalleau20714bf2012-11-21 10:51:12 +0100970 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
971 sk->sk_state = BT_CONNECT2;
972 else
973 sk->sk_state = BT_CONNECTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 /* Wake up parent */
976 parent->sk_data_ready(parent, 1);
977
978 bh_unlock_sock(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Andre Guedes40528082013-01-29 19:59:56 -0300980 sco_conn_unlock(conn);
981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
984/* ----- SCO interface with lower layer (HCI) ----- */
Frédéric Dalleau20714bf2012-11-21 10:51:12 +0100985int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Gustavo Padovanfc5fef62012-05-23 04:04:19 -0300987 struct sock *sk;
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +0100988 int lm = 0;
989
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +0300990 BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +0100992 /* Find listening sockets */
993 read_lock(&sco_sk_list.lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800994 sk_for_each(sk, &sco_sk_list.head) {
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +0100995 if (sk->sk_state != BT_LISTEN)
996 continue;
997
998 if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr) ||
Gustavo Padovanbe7c2b92012-05-17 00:36:21 -0300999 !bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001000 lm |= HCI_LM_ACCEPT;
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01001001
1002 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
1003 *flags |= HCI_PROTO_DEFER;
Marcel Holtmann71aeeaa2009-01-15 21:57:02 +01001004 break;
1005 }
1006 }
1007 read_unlock(&sco_sk_list.lock);
1008
1009 return lm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
Andrei Emeltchenko9e664632012-07-24 16:06:15 +03001012void sco_connect_cfm(struct hci_conn *hcon, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001014 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (!status) {
1016 struct sco_conn *conn;
1017
Lukasz Rymanowski519e42b2012-04-19 16:12:28 +02001018 conn = sco_conn_add(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (conn)
1020 sco_conn_ready(conn);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001021 } else
Joe Perchese1750722011-06-29 18:18:29 -07001022 sco_conn_del(hcon, bt_to_errno(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
Andrei Emeltchenko9e664632012-07-24 16:06:15 +03001025void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 BT_DBG("hcon %p reason %d", hcon, reason);
1028
Joe Perchese1750722011-06-29 18:18:29 -07001029 sco_conn_del(hcon, bt_to_errno(reason));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
Ulisses Furquim686ebf22011-12-21 10:11:33 -02001032int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
1034 struct sco_conn *conn = hcon->sco_data;
1035
1036 if (!conn)
1037 goto drop;
1038
1039 BT_DBG("conn %p len %d", conn, skb->len);
1040
1041 if (skb->len) {
1042 sco_recv_frame(conn, skb);
1043 return 0;
1044 }
1045
1046drop:
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001047 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return 0;
1049}
1050
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001051static int sco_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
1053 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Gustavo F. Padovanee65d192011-12-27 15:28:46 -02001055 read_lock(&sco_sk_list.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Sasha Levinb67bfe02013-02-27 17:06:00 -08001057 sk_for_each(sk, &sco_sk_list.head) {
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +03001058 seq_printf(f, "%pMR %pMR %d\n", &bt_sk(sk)->src,
1059 &bt_sk(sk)->dst, sk->sk_state);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08001060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Gustavo F. Padovanee65d192011-12-27 15:28:46 -02001062 read_unlock(&sco_sk_list.lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08001063
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001064 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001067static int sco_debugfs_open(struct inode *inode, struct file *file)
1068{
1069 return single_open(file, sco_debugfs_show, inode->i_private);
1070}
1071
1072static const struct file_operations sco_debugfs_fops = {
1073 .open = sco_debugfs_open,
1074 .read = seq_read,
1075 .llseek = seq_lseek,
1076 .release = single_release,
1077};
1078
1079static struct dentry *sco_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001081static const struct proto_ops sco_sock_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 .family = PF_BLUETOOTH,
1083 .owner = THIS_MODULE,
1084 .release = sco_sock_release,
1085 .bind = sco_sock_bind,
1086 .connect = sco_sock_connect,
1087 .listen = sco_sock_listen,
1088 .accept = sco_sock_accept,
1089 .getname = sco_sock_getname,
1090 .sendmsg = sco_sock_sendmsg,
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01001091 .recvmsg = sco_sock_recvmsg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 .poll = bt_sock_poll,
Marcel Holtmann3241ad82008-07-14 20:13:50 +02001093 .ioctl = bt_sock_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 .mmap = sock_no_mmap,
1095 .socketpair = sock_no_socketpair,
Marcel Holtmannfd0b3ff2009-06-16 00:01:49 +02001096 .shutdown = sco_sock_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 .setsockopt = sco_sock_setsockopt,
1098 .getsockopt = sco_sock_getsockopt
1099};
1100
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001101static const struct net_proto_family sco_sock_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 .family = PF_BLUETOOTH,
1103 .owner = THIS_MODULE,
1104 .create = sco_sock_create,
1105};
1106
Gustavo F. Padovan64274512011-02-07 20:08:52 -02001107int __init sco_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
1109 int err;
1110
1111 err = proto_register(&sco_proto, 0);
1112 if (err < 0)
1113 return err;
1114
1115 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1116 if (err < 0) {
1117 BT_ERR("SCO socket registration failed");
1118 goto error;
1119 }
1120
Masatake YAMATOde9b9212012-07-26 01:30:12 +09001121 err = bt_procfs_init(THIS_MODULE, &init_net, "sco", &sco_sk_list, NULL);
1122 if (err < 0) {
1123 BT_ERR("Failed to create SCO proc file");
1124 bt_sock_unregister(BTPROTO_SCO);
1125 goto error;
1126 }
1127
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001128 if (bt_debugfs) {
Gustavo Padovanbe7c2b92012-05-17 00:36:21 -03001129 sco_debugfs = debugfs_create_file("sco", 0444, bt_debugfs,
1130 NULL, &sco_debugfs_fops);
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001131 if (!sco_debugfs)
1132 BT_ERR("Failed to create SCO debug file");
1133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 BT_INFO("SCO socket layer initialized");
1136
1137 return 0;
1138
1139error:
1140 proto_unregister(&sco_proto);
1141 return err;
1142}
1143
Gustavo F. Padovan64274512011-02-07 20:08:52 -02001144void __exit sco_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Masatake YAMATOde9b9212012-07-26 01:30:12 +09001146 bt_procfs_cleanup(&init_net, "sco");
1147
Marcel Holtmannaef7d972010-03-21 05:27:45 +01001148 debugfs_remove(sco_debugfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
David Herrmann5e9d7f82013-02-24 19:36:51 +01001150 bt_sock_unregister(BTPROTO_SCO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 proto_unregister(&sco_proto);
1153}
1154
Marcel Holtmann7cb127d2008-07-14 20:13:53 +02001155module_param(disable_esco, bool, 0644);
1156MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");