Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / net / bluetooth / sco.c
bloba324b009e34b7bc1b66a91ce9e3a5188169ec04c
1 /*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
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;
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
15 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
18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22 SOFTWARE IS DISCLAIMED.
25 /* Bluetooth SCO sockets. */
27 #include <linux/module.h>
29 #include <linux/types.h>
30 #include <linux/errno.h>
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/poll.h>
35 #include <linux/fcntl.h>
36 #include <linux/init.h>
37 #include <linux/interrupt.h>
38 #include <linux/socket.h>
39 #include <linux/skbuff.h>
40 #include <linux/device.h>
41 #include <linux/debugfs.h>
42 #include <linux/seq_file.h>
43 #include <linux/list.h>
44 #include <linux/security.h>
45 #include <net/sock.h>
47 #include <asm/system.h>
48 #include <linux/uaccess.h>
50 #include <net/bluetooth/bluetooth.h>
51 #include <net/bluetooth/hci_core.h>
52 #include <net/bluetooth/sco.h>
54 static int disable_esco;
56 static const struct proto_ops sco_sock_ops;
58 static struct bt_sock_list sco_sk_list = {
59 .lock = __RW_LOCK_UNLOCKED(sco_sk_list.lock)
62 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent);
63 static void sco_chan_del(struct sock *sk, int err);
65 static int sco_conn_del(struct hci_conn *conn, int err);
67 static void sco_sock_close(struct sock *sk);
68 static void sco_sock_kill(struct sock *sk);
70 /* ---- SCO timers ---- */
71 static void sco_sock_timeout(unsigned long arg)
73 struct sock *sk = (struct sock *) arg;
75 BT_DBG("sock %p state %d", sk, sk->sk_state);
77 bh_lock_sock(sk);
78 sk->sk_err = ETIMEDOUT;
79 sk->sk_state_change(sk);
80 bh_unlock_sock(sk);
82 sco_sock_kill(sk);
83 sock_put(sk);
86 static void sco_sock_set_timer(struct sock *sk, long timeout)
88 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
89 sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
92 static void sco_sock_clear_timer(struct sock *sk)
94 BT_DBG("sock %p state %d", sk, sk->sk_state);
95 sk_stop_timer(sk, &sk->sk_timer);
98 /* ---- SCO connections ---- */
99 static struct sco_conn *sco_conn_add(struct hci_conn *hcon, __u8 status)
101 struct hci_dev *hdev = hcon->hdev;
102 struct sco_conn *conn = hcon->sco_data;
104 if (conn || status)
105 return conn;
107 conn = kzalloc(sizeof(struct sco_conn), GFP_ATOMIC);
108 if (!conn)
109 return NULL;
111 spin_lock_init(&conn->lock);
113 hcon->sco_data = conn;
114 conn->hcon = hcon;
116 conn->src = &hdev->bdaddr;
117 conn->dst = &hcon->dst;
119 if (hdev->sco_mtu > 0)
120 conn->mtu = hdev->sco_mtu;
121 else
122 conn->mtu = 60;
124 BT_DBG("hcon %p conn %p", hcon, conn);
126 return conn;
129 static inline struct sock *sco_chan_get(struct sco_conn *conn)
131 struct sock *sk = NULL;
132 sco_conn_lock(conn);
133 sk = conn->sk;
134 sco_conn_unlock(conn);
135 return sk;
138 static int sco_conn_del(struct hci_conn *hcon, int err)
140 struct sco_conn *conn = hcon->sco_data;
141 struct sock *sk;
143 if (!conn)
144 return 0;
146 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
148 /* Kill socket */
149 sk = sco_chan_get(conn);
150 if (sk) {
151 bh_lock_sock(sk);
152 sco_sock_clear_timer(sk);
153 sco_chan_del(sk, err);
154 bh_unlock_sock(sk);
155 sco_sock_kill(sk);
158 hcon->sco_data = NULL;
159 kfree(conn);
160 return 0;
163 static inline int sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
165 int err = 0;
167 sco_conn_lock(conn);
168 if (conn->sk)
169 err = -EBUSY;
170 else
171 __sco_chan_add(conn, sk, parent);
173 sco_conn_unlock(conn);
174 return err;
177 static int sco_connect(struct sock *sk)
179 bdaddr_t *src = &bt_sk(sk)->src;
180 bdaddr_t *dst = &bt_sk(sk)->dst;
181 struct sco_conn *conn;
182 struct hci_conn *hcon;
183 struct hci_dev *hdev;
184 int err, type;
186 BT_DBG("%s -> %s", batostr(src), batostr(dst));
188 hdev = hci_get_route(dst, src);
189 if (!hdev)
190 return -EHOSTUNREACH;
192 hci_dev_lock_bh(hdev);
194 if (lmp_esco_capable(hdev) && !disable_esco)
195 type = ESCO_LINK;
196 else
197 type = SCO_LINK;
199 hcon = hci_connect(hdev, type, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING);
200 if (IS_ERR(hcon)) {
201 err = PTR_ERR(hcon);
202 goto done;
205 conn = sco_conn_add(hcon, 0);
206 if (!conn) {
207 hci_conn_put(hcon);
208 err = -ENOMEM;
209 goto done;
212 /* Update source addr of the socket */
213 bacpy(src, conn->src);
215 err = sco_chan_add(conn, sk, NULL);
216 if (err)
217 goto done;
219 if (hcon->state == BT_CONNECTED) {
220 sco_sock_clear_timer(sk);
221 sk->sk_state = BT_CONNECTED;
222 } else {
223 sk->sk_state = BT_CONNECT;
224 sco_sock_set_timer(sk, sk->sk_sndtimeo);
227 done:
228 hci_dev_unlock_bh(hdev);
229 hci_dev_put(hdev);
230 return err;
233 static inline int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
235 struct sco_conn *conn = sco_pi(sk)->conn;
236 struct sk_buff *skb;
237 int err, count;
239 /* Check outgoing MTU */
240 if (len > conn->mtu)
241 return -EINVAL;
243 BT_DBG("sk %p len %d", sk, len);
245 count = min_t(unsigned int, conn->mtu, len);
246 skb = bt_skb_send_alloc(sk, count,
247 msg->msg_flags & MSG_DONTWAIT, &err);
248 if (!skb)
249 return err;
251 if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) {
252 kfree_skb(skb);
253 return -EFAULT;
256 hci_send_sco(conn->hcon, skb);
258 return count;
261 static inline void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
263 struct sock *sk = sco_chan_get(conn);
265 if (!sk)
266 goto drop;
268 BT_DBG("sk %p len %d", sk, skb->len);
270 if (sk->sk_state != BT_CONNECTED)
271 goto drop;
273 if (!sock_queue_rcv_skb(sk, skb))
274 return;
276 drop:
277 kfree_skb(skb);
280 /* -------- Socket interface ---------- */
281 static struct sock *__sco_get_sock_by_addr(bdaddr_t *ba)
283 struct sock *sk;
284 struct hlist_node *node;
286 sk_for_each(sk, node, &sco_sk_list.head)
287 if (!bacmp(&bt_sk(sk)->src, ba))
288 goto found;
289 sk = NULL;
290 found:
291 return sk;
294 /* Find socket listening on source bdaddr.
295 * Returns closest match.
297 static struct sock *sco_get_sock_listen(bdaddr_t *src)
299 struct sock *sk = NULL, *sk1 = NULL;
300 struct hlist_node *node;
302 read_lock(&sco_sk_list.lock);
304 sk_for_each(sk, node, &sco_sk_list.head) {
305 if (sk->sk_state != BT_LISTEN)
306 continue;
308 /* Exact match. */
309 if (!bacmp(&bt_sk(sk)->src, src))
310 break;
312 /* Closest match */
313 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
314 sk1 = sk;
317 read_unlock(&sco_sk_list.lock);
319 return node ? sk : sk1;
322 static void sco_sock_destruct(struct sock *sk)
324 BT_DBG("sk %p", sk);
326 skb_queue_purge(&sk->sk_receive_queue);
327 skb_queue_purge(&sk->sk_write_queue);
330 static void sco_sock_cleanup_listen(struct sock *parent)
332 struct sock *sk;
334 BT_DBG("parent %p", parent);
336 /* Close not yet accepted channels */
337 while ((sk = bt_accept_dequeue(parent, NULL))) {
338 sco_sock_close(sk);
339 sco_sock_kill(sk);
342 parent->sk_state = BT_CLOSED;
343 sock_set_flag(parent, SOCK_ZAPPED);
346 /* Kill socket (only if zapped and orphan)
347 * Must be called on unlocked socket.
349 static void sco_sock_kill(struct sock *sk)
351 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
352 return;
354 BT_DBG("sk %p state %d", sk, sk->sk_state);
356 /* Kill poor orphan */
357 bt_sock_unlink(&sco_sk_list, sk);
358 sock_set_flag(sk, SOCK_DEAD);
359 sock_put(sk);
362 static void __sco_sock_close(struct sock *sk)
364 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
366 switch (sk->sk_state) {
367 case BT_LISTEN:
368 sco_sock_cleanup_listen(sk);
369 break;
371 case BT_CONNECTED:
372 case BT_CONFIG:
373 if (sco_pi(sk)->conn) {
374 sk->sk_state = BT_DISCONN;
375 sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT);
376 hci_conn_put(sco_pi(sk)->conn->hcon);
377 sco_pi(sk)->conn->hcon = NULL;
378 } else
379 sco_chan_del(sk, ECONNRESET);
380 break;
382 case BT_CONNECT:
383 case BT_DISCONN:
384 sco_chan_del(sk, ECONNRESET);
385 break;
387 default:
388 sock_set_flag(sk, SOCK_ZAPPED);
389 break;
393 /* Must be called on unlocked socket. */
394 static void sco_sock_close(struct sock *sk)
396 sco_sock_clear_timer(sk);
397 lock_sock(sk);
398 __sco_sock_close(sk);
399 release_sock(sk);
400 sco_sock_kill(sk);
403 static void sco_sock_init(struct sock *sk, struct sock *parent)
405 BT_DBG("sk %p", sk);
407 if (parent) {
408 sk->sk_type = parent->sk_type;
409 security_sk_clone(parent, sk);
413 static struct proto sco_proto = {
414 .name = "SCO",
415 .owner = THIS_MODULE,
416 .obj_size = sizeof(struct sco_pinfo)
419 static struct sock *sco_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
421 struct sock *sk;
423 sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto);
424 if (!sk)
425 return NULL;
427 sock_init_data(sock, sk);
428 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
430 sk->sk_destruct = sco_sock_destruct;
431 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
433 sock_reset_flag(sk, SOCK_ZAPPED);
435 sk->sk_protocol = proto;
436 sk->sk_state = BT_OPEN;
438 setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk);
440 bt_sock_link(&sco_sk_list, sk);
441 return sk;
444 static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
445 int kern)
447 struct sock *sk;
449 BT_DBG("sock %p", sock);
451 sock->state = SS_UNCONNECTED;
453 if (sock->type != SOCK_SEQPACKET)
454 return -ESOCKTNOSUPPORT;
456 sock->ops = &sco_sock_ops;
458 sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC);
459 if (!sk)
460 return -ENOMEM;
462 sco_sock_init(sk, NULL);
463 return 0;
466 static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
468 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
469 struct sock *sk = sock->sk;
470 bdaddr_t *src = &sa->sco_bdaddr;
471 int err = 0;
473 BT_DBG("sk %p %s", sk, batostr(&sa->sco_bdaddr));
475 if (!addr || addr->sa_family != AF_BLUETOOTH)
476 return -EINVAL;
478 lock_sock(sk);
480 if (sk->sk_state != BT_OPEN) {
481 err = -EBADFD;
482 goto done;
485 write_lock_bh(&sco_sk_list.lock);
487 if (bacmp(src, BDADDR_ANY) && __sco_get_sock_by_addr(src)) {
488 err = -EADDRINUSE;
489 } else {
490 /* Save source address */
491 bacpy(&bt_sk(sk)->src, &sa->sco_bdaddr);
492 sk->sk_state = BT_BOUND;
495 write_unlock_bh(&sco_sk_list.lock);
497 done:
498 release_sock(sk);
499 return err;
502 static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
504 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
505 struct sock *sk = sock->sk;
506 int err = 0;
509 BT_DBG("sk %p", sk);
511 if (alen < sizeof(struct sockaddr_sco) ||
512 addr->sa_family != AF_BLUETOOTH)
513 return -EINVAL;
515 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
516 return -EBADFD;
518 if (sk->sk_type != SOCK_SEQPACKET)
519 return -EINVAL;
521 lock_sock(sk);
523 /* Set destination address and psm */
524 bacpy(&bt_sk(sk)->dst, &sa->sco_bdaddr);
526 err = sco_connect(sk);
527 if (err)
528 goto done;
530 err = bt_sock_wait_state(sk, BT_CONNECTED,
531 sock_sndtimeo(sk, flags & O_NONBLOCK));
533 done:
534 release_sock(sk);
535 return err;
538 static int sco_sock_listen(struct socket *sock, int backlog)
540 struct sock *sk = sock->sk;
541 int err = 0;
543 BT_DBG("sk %p backlog %d", sk, backlog);
545 lock_sock(sk);
547 if (sk->sk_state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
548 err = -EBADFD;
549 goto done;
552 sk->sk_max_ack_backlog = backlog;
553 sk->sk_ack_backlog = 0;
554 sk->sk_state = BT_LISTEN;
556 done:
557 release_sock(sk);
558 return err;
561 static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
563 DECLARE_WAITQUEUE(wait, current);
564 struct sock *sk = sock->sk, *ch;
565 long timeo;
566 int err = 0;
568 lock_sock(sk);
570 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
572 BT_DBG("sk %p timeo %ld", sk, timeo);
574 /* Wait for an incoming connection. (wake-one). */
575 add_wait_queue_exclusive(sk_sleep(sk), &wait);
576 while (1) {
577 set_current_state(TASK_INTERRUPTIBLE);
579 if (sk->sk_state != BT_LISTEN) {
580 err = -EBADFD;
581 break;
584 ch = bt_accept_dequeue(sk, newsock);
585 if (ch)
586 break;
588 if (!timeo) {
589 err = -EAGAIN;
590 break;
593 if (signal_pending(current)) {
594 err = sock_intr_errno(timeo);
595 break;
598 release_sock(sk);
599 timeo = schedule_timeout(timeo);
600 lock_sock(sk);
602 __set_current_state(TASK_RUNNING);
603 remove_wait_queue(sk_sleep(sk), &wait);
605 if (err)
606 goto done;
608 newsock->state = SS_CONNECTED;
610 BT_DBG("new socket %p", ch);
612 done:
613 release_sock(sk);
614 return err;
617 static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
619 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
620 struct sock *sk = sock->sk;
622 BT_DBG("sock %p, sk %p", sock, sk);
624 addr->sa_family = AF_BLUETOOTH;
625 *len = sizeof(struct sockaddr_sco);
627 if (peer)
628 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst);
629 else
630 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src);
632 return 0;
635 static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
636 struct msghdr *msg, size_t len)
638 struct sock *sk = sock->sk;
639 int err;
641 BT_DBG("sock %p, sk %p", sock, sk);
643 err = sock_error(sk);
644 if (err)
645 return err;
647 if (msg->msg_flags & MSG_OOB)
648 return -EOPNOTSUPP;
650 lock_sock(sk);
652 if (sk->sk_state == BT_CONNECTED)
653 err = sco_send_frame(sk, msg, len);
654 else
655 err = -ENOTCONN;
657 release_sock(sk);
658 return err;
661 static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
663 struct sock *sk = sock->sk;
664 int err = 0;
666 BT_DBG("sk %p", sk);
668 lock_sock(sk);
670 switch (optname) {
671 default:
672 err = -ENOPROTOOPT;
673 break;
676 release_sock(sk);
677 return err;
680 static int sco_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
682 struct sock *sk = sock->sk;
683 struct sco_options opts;
684 struct sco_conninfo cinfo;
685 int len, err = 0;
687 BT_DBG("sk %p", sk);
689 if (get_user(len, optlen))
690 return -EFAULT;
692 lock_sock(sk);
694 switch (optname) {
695 case SCO_OPTIONS:
696 if (sk->sk_state != BT_CONNECTED) {
697 err = -ENOTCONN;
698 break;
701 opts.mtu = sco_pi(sk)->conn->mtu;
703 BT_DBG("mtu %d", opts.mtu);
705 len = min_t(unsigned int, len, sizeof(opts));
706 if (copy_to_user(optval, (char *)&opts, len))
707 err = -EFAULT;
709 break;
711 case SCO_CONNINFO:
712 if (sk->sk_state != BT_CONNECTED) {
713 err = -ENOTCONN;
714 break;
717 memset(&cinfo, 0, sizeof(cinfo));
718 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
719 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
721 len = min_t(unsigned int, len, sizeof(cinfo));
722 if (copy_to_user(optval, (char *)&cinfo, len))
723 err = -EFAULT;
725 break;
727 default:
728 err = -ENOPROTOOPT;
729 break;
732 release_sock(sk);
733 return err;
736 static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
738 struct sock *sk = sock->sk;
739 int len, err = 0;
741 BT_DBG("sk %p", sk);
743 if (level == SOL_SCO)
744 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
746 if (get_user(len, optlen))
747 return -EFAULT;
749 lock_sock(sk);
751 switch (optname) {
752 default:
753 err = -ENOPROTOOPT;
754 break;
757 release_sock(sk);
758 return err;
761 static int sco_sock_shutdown(struct socket *sock, int how)
763 struct sock *sk = sock->sk;
764 int err = 0;
766 BT_DBG("sock %p, sk %p", sock, sk);
768 if (!sk)
769 return 0;
771 lock_sock(sk);
772 if (!sk->sk_shutdown) {
773 sk->sk_shutdown = SHUTDOWN_MASK;
774 sco_sock_clear_timer(sk);
775 __sco_sock_close(sk);
777 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
778 err = bt_sock_wait_state(sk, BT_CLOSED,
779 sk->sk_lingertime);
781 release_sock(sk);
782 return err;
785 static int sco_sock_release(struct socket *sock)
787 struct sock *sk = sock->sk;
788 int err = 0;
790 BT_DBG("sock %p, sk %p", sock, sk);
792 if (!sk)
793 return 0;
795 sco_sock_close(sk);
797 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) {
798 lock_sock(sk);
799 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
800 release_sock(sk);
803 sock_orphan(sk);
804 sco_sock_kill(sk);
805 return err;
808 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
810 BT_DBG("conn %p", conn);
812 sco_pi(sk)->conn = conn;
813 conn->sk = sk;
815 if (parent)
816 bt_accept_enqueue(parent, sk);
819 /* Delete channel.
820 * Must be called on the locked socket. */
821 static void sco_chan_del(struct sock *sk, int err)
823 struct sco_conn *conn;
825 conn = sco_pi(sk)->conn;
827 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
829 if (conn) {
830 sco_conn_lock(conn);
831 conn->sk = NULL;
832 sco_pi(sk)->conn = NULL;
833 sco_conn_unlock(conn);
835 if (conn->hcon)
836 hci_conn_put(conn->hcon);
839 sk->sk_state = BT_CLOSED;
840 sk->sk_err = err;
841 sk->sk_state_change(sk);
843 sock_set_flag(sk, SOCK_ZAPPED);
846 static void sco_conn_ready(struct sco_conn *conn)
848 struct sock *parent;
849 struct sock *sk = conn->sk;
851 BT_DBG("conn %p", conn);
853 sco_conn_lock(conn);
855 if (sk) {
856 sco_sock_clear_timer(sk);
857 bh_lock_sock(sk);
858 sk->sk_state = BT_CONNECTED;
859 sk->sk_state_change(sk);
860 bh_unlock_sock(sk);
861 } else {
862 parent = sco_get_sock_listen(conn->src);
863 if (!parent)
864 goto done;
866 bh_lock_sock(parent);
868 sk = sco_sock_alloc(sock_net(parent), NULL,
869 BTPROTO_SCO, GFP_ATOMIC);
870 if (!sk) {
871 bh_unlock_sock(parent);
872 goto done;
875 sco_sock_init(sk, parent);
877 bacpy(&bt_sk(sk)->src, conn->src);
878 bacpy(&bt_sk(sk)->dst, conn->dst);
880 hci_conn_hold(conn->hcon);
881 __sco_chan_add(conn, sk, parent);
883 sk->sk_state = BT_CONNECTED;
885 /* Wake up parent */
886 parent->sk_data_ready(parent, 1);
888 bh_unlock_sock(parent);
891 done:
892 sco_conn_unlock(conn);
895 /* ----- SCO interface with lower layer (HCI) ----- */
896 static int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
898 register struct sock *sk;
899 struct hlist_node *node;
900 int lm = 0;
902 if (type != SCO_LINK && type != ESCO_LINK)
903 return -EINVAL;
905 BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
907 /* Find listening sockets */
908 read_lock(&sco_sk_list.lock);
909 sk_for_each(sk, node, &sco_sk_list.head) {
910 if (sk->sk_state != BT_LISTEN)
911 continue;
913 if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr) ||
914 !bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
915 lm |= HCI_LM_ACCEPT;
916 break;
919 read_unlock(&sco_sk_list.lock);
921 return lm;
924 static int sco_connect_cfm(struct hci_conn *hcon, __u8 status)
926 BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
928 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
929 return -EINVAL;
931 if (!status) {
932 struct sco_conn *conn;
934 conn = sco_conn_add(hcon, status);
935 if (conn)
936 sco_conn_ready(conn);
937 } else
938 sco_conn_del(hcon, bt_to_errno(status));
940 return 0;
943 static int sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
945 BT_DBG("hcon %p reason %d", hcon, reason);
947 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
948 return -EINVAL;
950 sco_conn_del(hcon, bt_to_errno(reason));
952 return 0;
955 static int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
957 struct sco_conn *conn = hcon->sco_data;
959 if (!conn)
960 goto drop;
962 BT_DBG("conn %p len %d", conn, skb->len);
964 if (skb->len) {
965 sco_recv_frame(conn, skb);
966 return 0;
969 drop:
970 kfree_skb(skb);
971 return 0;
974 static int sco_debugfs_show(struct seq_file *f, void *p)
976 struct sock *sk;
977 struct hlist_node *node;
979 read_lock_bh(&sco_sk_list.lock);
981 sk_for_each(sk, node, &sco_sk_list.head) {
982 seq_printf(f, "%s %s %d\n", batostr(&bt_sk(sk)->src),
983 batostr(&bt_sk(sk)->dst), sk->sk_state);
986 read_unlock_bh(&sco_sk_list.lock);
988 return 0;
991 static int sco_debugfs_open(struct inode *inode, struct file *file)
993 return single_open(file, sco_debugfs_show, inode->i_private);
996 static const struct file_operations sco_debugfs_fops = {
997 .open = sco_debugfs_open,
998 .read = seq_read,
999 .llseek = seq_lseek,
1000 .release = single_release,
1003 static struct dentry *sco_debugfs;
1005 static const struct proto_ops sco_sock_ops = {
1006 .family = PF_BLUETOOTH,
1007 .owner = THIS_MODULE,
1008 .release = sco_sock_release,
1009 .bind = sco_sock_bind,
1010 .connect = sco_sock_connect,
1011 .listen = sco_sock_listen,
1012 .accept = sco_sock_accept,
1013 .getname = sco_sock_getname,
1014 .sendmsg = sco_sock_sendmsg,
1015 .recvmsg = bt_sock_recvmsg,
1016 .poll = bt_sock_poll,
1017 .ioctl = bt_sock_ioctl,
1018 .mmap = sock_no_mmap,
1019 .socketpair = sock_no_socketpair,
1020 .shutdown = sco_sock_shutdown,
1021 .setsockopt = sco_sock_setsockopt,
1022 .getsockopt = sco_sock_getsockopt
1025 static const struct net_proto_family sco_sock_family_ops = {
1026 .family = PF_BLUETOOTH,
1027 .owner = THIS_MODULE,
1028 .create = sco_sock_create,
1031 static struct hci_proto sco_hci_proto = {
1032 .name = "SCO",
1033 .id = HCI_PROTO_SCO,
1034 .connect_ind = sco_connect_ind,
1035 .connect_cfm = sco_connect_cfm,
1036 .disconn_cfm = sco_disconn_cfm,
1037 .recv_scodata = sco_recv_scodata
1040 int __init sco_init(void)
1042 int err;
1044 err = proto_register(&sco_proto, 0);
1045 if (err < 0)
1046 return err;
1048 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1049 if (err < 0) {
1050 BT_ERR("SCO socket registration failed");
1051 goto error;
1054 err = hci_register_proto(&sco_hci_proto);
1055 if (err < 0) {
1056 BT_ERR("SCO protocol registration failed");
1057 bt_sock_unregister(BTPROTO_SCO);
1058 goto error;
1061 if (bt_debugfs) {
1062 sco_debugfs = debugfs_create_file("sco", 0444,
1063 bt_debugfs, NULL, &sco_debugfs_fops);
1064 if (!sco_debugfs)
1065 BT_ERR("Failed to create SCO debug file");
1068 BT_INFO("SCO socket layer initialized");
1070 return 0;
1072 error:
1073 proto_unregister(&sco_proto);
1074 return err;
1077 void __exit sco_exit(void)
1079 debugfs_remove(sco_debugfs);
1081 if (bt_sock_unregister(BTPROTO_SCO) < 0)
1082 BT_ERR("SCO socket unregistration failed");
1084 if (hci_unregister_proto(&sco_hci_proto) < 0)
1085 BT_ERR("SCO protocol unregistration failed");
1087 proto_unregister(&sco_proto);
1090 module_param(disable_esco, bool, 0644);
1091 MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");