Staging: hv: hv_mouse.c: clean up struct synthhid_msg_hdr
[wandboard.git] / net / bluetooth / sco.c
blob960c6d1637da0d3ee23cc998e5890b84123f76c9
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 <net/sock.h>
46 #include <asm/system.h>
47 #include <linux/uaccess.h>
49 #include <net/bluetooth/bluetooth.h>
50 #include <net/bluetooth/hci_core.h>
51 #include <net/bluetooth/sco.h>
53 #define VERSION "0.6"
55 static int disable_esco;
57 static const struct proto_ops sco_sock_ops;
59 static struct bt_sock_list sco_sk_list = {
60 .lock = __RW_LOCK_UNLOCKED(sco_sk_list.lock)
63 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent);
64 static void sco_chan_del(struct sock *sk, int err);
66 static int sco_conn_del(struct hci_conn *conn, int err);
68 static void sco_sock_close(struct sock *sk);
69 static void sco_sock_kill(struct sock *sk);
71 /* ---- SCO timers ---- */
72 static void sco_sock_timeout(unsigned long arg)
74 struct sock *sk = (struct sock *) arg;
76 BT_DBG("sock %p state %d", sk, sk->sk_state);
78 bh_lock_sock(sk);
79 sk->sk_err = ETIMEDOUT;
80 sk->sk_state_change(sk);
81 bh_unlock_sock(sk);
83 sco_sock_kill(sk);
84 sock_put(sk);
87 static void sco_sock_set_timer(struct sock *sk, long timeout)
89 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
90 sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
93 static void sco_sock_clear_timer(struct sock *sk)
95 BT_DBG("sock %p state %d", sk, sk->sk_state);
96 sk_stop_timer(sk, &sk->sk_timer);
99 /* ---- SCO connections ---- */
100 static struct sco_conn *sco_conn_add(struct hci_conn *hcon, __u8 status)
102 struct hci_dev *hdev = hcon->hdev;
103 struct sco_conn *conn = hcon->sco_data;
105 if (conn || status)
106 return conn;
108 conn = kzalloc(sizeof(struct sco_conn), GFP_ATOMIC);
109 if (!conn)
110 return NULL;
112 spin_lock_init(&conn->lock);
114 hcon->sco_data = conn;
115 conn->hcon = hcon;
117 conn->src = &hdev->bdaddr;
118 conn->dst = &hcon->dst;
120 if (hdev->sco_mtu > 0)
121 conn->mtu = hdev->sco_mtu;
122 else
123 conn->mtu = 60;
125 BT_DBG("hcon %p conn %p", hcon, conn);
127 return conn;
130 static inline struct sock *sco_chan_get(struct sco_conn *conn)
132 struct sock *sk = NULL;
133 sco_conn_lock(conn);
134 sk = conn->sk;
135 sco_conn_unlock(conn);
136 return sk;
139 static int sco_conn_del(struct hci_conn *hcon, int err)
141 struct sco_conn *conn = hcon->sco_data;
142 struct sock *sk;
144 if (!conn)
145 return 0;
147 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
149 /* Kill socket */
150 sk = sco_chan_get(conn);
151 if (sk) {
152 bh_lock_sock(sk);
153 sco_sock_clear_timer(sk);
154 sco_chan_del(sk, err);
155 bh_unlock_sock(sk);
156 sco_sock_kill(sk);
159 hcon->sco_data = NULL;
160 kfree(conn);
161 return 0;
164 static inline int sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
166 int err = 0;
168 sco_conn_lock(conn);
169 if (conn->sk)
170 err = -EBUSY;
171 else
172 __sco_chan_add(conn, sk, parent);
174 sco_conn_unlock(conn);
175 return err;
178 static int sco_connect(struct sock *sk)
180 bdaddr_t *src = &bt_sk(sk)->src;
181 bdaddr_t *dst = &bt_sk(sk)->dst;
182 struct sco_conn *conn;
183 struct hci_conn *hcon;
184 struct hci_dev *hdev;
185 int err, type;
187 BT_DBG("%s -> %s", batostr(src), batostr(dst));
189 hdev = hci_get_route(dst, src);
190 if (!hdev)
191 return -EHOSTUNREACH;
193 hci_dev_lock_bh(hdev);
195 err = -ENOMEM;
197 if (lmp_esco_capable(hdev) && !disable_esco)
198 type = ESCO_LINK;
199 else
200 type = SCO_LINK;
202 hcon = hci_connect(hdev, type, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING);
203 if (!hcon)
204 goto done;
206 conn = sco_conn_add(hcon, 0);
207 if (!conn) {
208 hci_conn_put(hcon);
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 case BT_CONNECT:
374 case BT_DISCONN:
375 sco_chan_del(sk, ECONNRESET);
376 break;
378 default:
379 sock_set_flag(sk, SOCK_ZAPPED);
380 break;
384 /* Must be called on unlocked socket. */
385 static void sco_sock_close(struct sock *sk)
387 sco_sock_clear_timer(sk);
388 lock_sock(sk);
389 __sco_sock_close(sk);
390 release_sock(sk);
391 sco_sock_kill(sk);
394 static void sco_sock_init(struct sock *sk, struct sock *parent)
396 BT_DBG("sk %p", sk);
398 if (parent)
399 sk->sk_type = parent->sk_type;
402 static struct proto sco_proto = {
403 .name = "SCO",
404 .owner = THIS_MODULE,
405 .obj_size = sizeof(struct sco_pinfo)
408 static struct sock *sco_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
410 struct sock *sk;
412 sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto);
413 if (!sk)
414 return NULL;
416 sock_init_data(sock, sk);
417 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
419 sk->sk_destruct = sco_sock_destruct;
420 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
422 sock_reset_flag(sk, SOCK_ZAPPED);
424 sk->sk_protocol = proto;
425 sk->sk_state = BT_OPEN;
427 setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk);
429 bt_sock_link(&sco_sk_list, sk);
430 return sk;
433 static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
434 int kern)
436 struct sock *sk;
438 BT_DBG("sock %p", sock);
440 sock->state = SS_UNCONNECTED;
442 if (sock->type != SOCK_SEQPACKET)
443 return -ESOCKTNOSUPPORT;
445 sock->ops = &sco_sock_ops;
447 sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC);
448 if (!sk)
449 return -ENOMEM;
451 sco_sock_init(sk, NULL);
452 return 0;
455 static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
457 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
458 struct sock *sk = sock->sk;
459 bdaddr_t *src = &sa->sco_bdaddr;
460 int err = 0;
462 BT_DBG("sk %p %s", sk, batostr(&sa->sco_bdaddr));
464 if (!addr || addr->sa_family != AF_BLUETOOTH)
465 return -EINVAL;
467 lock_sock(sk);
469 if (sk->sk_state != BT_OPEN) {
470 err = -EBADFD;
471 goto done;
474 write_lock_bh(&sco_sk_list.lock);
476 if (bacmp(src, BDADDR_ANY) && __sco_get_sock_by_addr(src)) {
477 err = -EADDRINUSE;
478 } else {
479 /* Save source address */
480 bacpy(&bt_sk(sk)->src, &sa->sco_bdaddr);
481 sk->sk_state = BT_BOUND;
484 write_unlock_bh(&sco_sk_list.lock);
486 done:
487 release_sock(sk);
488 return err;
491 static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
493 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
494 struct sock *sk = sock->sk;
495 int err = 0;
498 BT_DBG("sk %p", sk);
500 if (alen < sizeof(struct sockaddr_sco) ||
501 addr->sa_family != AF_BLUETOOTH)
502 return -EINVAL;
504 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
505 return -EBADFD;
507 if (sk->sk_type != SOCK_SEQPACKET)
508 return -EINVAL;
510 lock_sock(sk);
512 /* Set destination address and psm */
513 bacpy(&bt_sk(sk)->dst, &sa->sco_bdaddr);
515 err = sco_connect(sk);
516 if (err)
517 goto done;
519 err = bt_sock_wait_state(sk, BT_CONNECTED,
520 sock_sndtimeo(sk, flags & O_NONBLOCK));
522 done:
523 release_sock(sk);
524 return err;
527 static int sco_sock_listen(struct socket *sock, int backlog)
529 struct sock *sk = sock->sk;
530 int err = 0;
532 BT_DBG("sk %p backlog %d", sk, backlog);
534 lock_sock(sk);
536 if (sk->sk_state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
537 err = -EBADFD;
538 goto done;
541 sk->sk_max_ack_backlog = backlog;
542 sk->sk_ack_backlog = 0;
543 sk->sk_state = BT_LISTEN;
545 done:
546 release_sock(sk);
547 return err;
550 static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
552 DECLARE_WAITQUEUE(wait, current);
553 struct sock *sk = sock->sk, *ch;
554 long timeo;
555 int err = 0;
557 lock_sock(sk);
559 if (sk->sk_state != BT_LISTEN) {
560 err = -EBADFD;
561 goto done;
564 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
566 BT_DBG("sk %p timeo %ld", sk, timeo);
568 /* Wait for an incoming connection. (wake-one). */
569 add_wait_queue_exclusive(sk_sleep(sk), &wait);
570 while (!(ch = bt_accept_dequeue(sk, newsock))) {
571 set_current_state(TASK_INTERRUPTIBLE);
572 if (!timeo) {
573 err = -EAGAIN;
574 break;
577 release_sock(sk);
578 timeo = schedule_timeout(timeo);
579 lock_sock(sk);
581 if (sk->sk_state != BT_LISTEN) {
582 err = -EBADFD;
583 break;
586 if (signal_pending(current)) {
587 err = sock_intr_errno(timeo);
588 break;
591 set_current_state(TASK_RUNNING);
592 remove_wait_queue(sk_sleep(sk), &wait);
594 if (err)
595 goto done;
597 newsock->state = SS_CONNECTED;
599 BT_DBG("new socket %p", ch);
601 done:
602 release_sock(sk);
603 return err;
606 static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
608 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
609 struct sock *sk = sock->sk;
611 BT_DBG("sock %p, sk %p", sock, sk);
613 addr->sa_family = AF_BLUETOOTH;
614 *len = sizeof(struct sockaddr_sco);
616 if (peer)
617 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst);
618 else
619 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src);
621 return 0;
624 static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
625 struct msghdr *msg, size_t len)
627 struct sock *sk = sock->sk;
628 int err;
630 BT_DBG("sock %p, sk %p", sock, sk);
632 err = sock_error(sk);
633 if (err)
634 return err;
636 if (msg->msg_flags & MSG_OOB)
637 return -EOPNOTSUPP;
639 lock_sock(sk);
641 if (sk->sk_state == BT_CONNECTED)
642 err = sco_send_frame(sk, msg, len);
643 else
644 err = -ENOTCONN;
646 release_sock(sk);
647 return err;
650 static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
652 struct sock *sk = sock->sk;
653 int err = 0;
655 BT_DBG("sk %p", sk);
657 lock_sock(sk);
659 switch (optname) {
660 default:
661 err = -ENOPROTOOPT;
662 break;
665 release_sock(sk);
666 return err;
669 static int sco_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
671 struct sock *sk = sock->sk;
672 struct sco_options opts;
673 struct sco_conninfo cinfo;
674 int len, err = 0;
676 BT_DBG("sk %p", sk);
678 if (get_user(len, optlen))
679 return -EFAULT;
681 lock_sock(sk);
683 switch (optname) {
684 case SCO_OPTIONS:
685 if (sk->sk_state != BT_CONNECTED) {
686 err = -ENOTCONN;
687 break;
690 opts.mtu = sco_pi(sk)->conn->mtu;
692 BT_DBG("mtu %d", opts.mtu);
694 len = min_t(unsigned int, len, sizeof(opts));
695 if (copy_to_user(optval, (char *)&opts, len))
696 err = -EFAULT;
698 break;
700 case SCO_CONNINFO:
701 if (sk->sk_state != BT_CONNECTED) {
702 err = -ENOTCONN;
703 break;
706 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
707 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
709 len = min_t(unsigned int, len, sizeof(cinfo));
710 if (copy_to_user(optval, (char *)&cinfo, len))
711 err = -EFAULT;
713 break;
715 default:
716 err = -ENOPROTOOPT;
717 break;
720 release_sock(sk);
721 return err;
724 static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
726 struct sock *sk = sock->sk;
727 int len, err = 0;
729 BT_DBG("sk %p", sk);
731 if (level == SOL_SCO)
732 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
734 if (get_user(len, optlen))
735 return -EFAULT;
737 lock_sock(sk);
739 switch (optname) {
740 default:
741 err = -ENOPROTOOPT;
742 break;
745 release_sock(sk);
746 return err;
749 static int sco_sock_shutdown(struct socket *sock, int how)
751 struct sock *sk = sock->sk;
752 int err = 0;
754 BT_DBG("sock %p, sk %p", sock, sk);
756 if (!sk)
757 return 0;
759 lock_sock(sk);
760 if (!sk->sk_shutdown) {
761 sk->sk_shutdown = SHUTDOWN_MASK;
762 sco_sock_clear_timer(sk);
763 __sco_sock_close(sk);
765 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
766 err = bt_sock_wait_state(sk, BT_CLOSED,
767 sk->sk_lingertime);
769 release_sock(sk);
770 return err;
773 static int sco_sock_release(struct socket *sock)
775 struct sock *sk = sock->sk;
776 int err = 0;
778 BT_DBG("sock %p, sk %p", sock, sk);
780 if (!sk)
781 return 0;
783 sco_sock_close(sk);
785 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) {
786 lock_sock(sk);
787 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
788 release_sock(sk);
791 sock_orphan(sk);
792 sco_sock_kill(sk);
793 return err;
796 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
798 BT_DBG("conn %p", conn);
800 sco_pi(sk)->conn = conn;
801 conn->sk = sk;
803 if (parent)
804 bt_accept_enqueue(parent, sk);
807 /* Delete channel.
808 * Must be called on the locked socket. */
809 static void sco_chan_del(struct sock *sk, int err)
811 struct sco_conn *conn;
813 conn = sco_pi(sk)->conn;
815 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
817 if (conn) {
818 sco_conn_lock(conn);
819 conn->sk = NULL;
820 sco_pi(sk)->conn = NULL;
821 sco_conn_unlock(conn);
822 hci_conn_put(conn->hcon);
825 sk->sk_state = BT_CLOSED;
826 sk->sk_err = err;
827 sk->sk_state_change(sk);
829 sock_set_flag(sk, SOCK_ZAPPED);
832 static void sco_conn_ready(struct sco_conn *conn)
834 struct sock *parent;
835 struct sock *sk = conn->sk;
837 BT_DBG("conn %p", conn);
839 sco_conn_lock(conn);
841 if (sk) {
842 sco_sock_clear_timer(sk);
843 bh_lock_sock(sk);
844 sk->sk_state = BT_CONNECTED;
845 sk->sk_state_change(sk);
846 bh_unlock_sock(sk);
847 } else {
848 parent = sco_get_sock_listen(conn->src);
849 if (!parent)
850 goto done;
852 bh_lock_sock(parent);
854 sk = sco_sock_alloc(sock_net(parent), NULL,
855 BTPROTO_SCO, GFP_ATOMIC);
856 if (!sk) {
857 bh_unlock_sock(parent);
858 goto done;
861 sco_sock_init(sk, parent);
863 bacpy(&bt_sk(sk)->src, conn->src);
864 bacpy(&bt_sk(sk)->dst, conn->dst);
866 hci_conn_hold(conn->hcon);
867 __sco_chan_add(conn, sk, parent);
869 sk->sk_state = BT_CONNECTED;
871 /* Wake up parent */
872 parent->sk_data_ready(parent, 1);
874 bh_unlock_sock(parent);
877 done:
878 sco_conn_unlock(conn);
881 /* ----- SCO interface with lower layer (HCI) ----- */
882 static int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
884 register struct sock *sk;
885 struct hlist_node *node;
886 int lm = 0;
888 if (type != SCO_LINK && type != ESCO_LINK)
889 return -EINVAL;
891 BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
893 /* Find listening sockets */
894 read_lock(&sco_sk_list.lock);
895 sk_for_each(sk, node, &sco_sk_list.head) {
896 if (sk->sk_state != BT_LISTEN)
897 continue;
899 if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr) ||
900 !bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
901 lm |= HCI_LM_ACCEPT;
902 break;
905 read_unlock(&sco_sk_list.lock);
907 return lm;
910 static int sco_connect_cfm(struct hci_conn *hcon, __u8 status)
912 BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
914 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
915 return -EINVAL;
917 if (!status) {
918 struct sco_conn *conn;
920 conn = sco_conn_add(hcon, status);
921 if (conn)
922 sco_conn_ready(conn);
923 } else
924 sco_conn_del(hcon, bt_err(status));
926 return 0;
929 static int sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
931 BT_DBG("hcon %p reason %d", hcon, reason);
933 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
934 return -EINVAL;
936 sco_conn_del(hcon, bt_err(reason));
938 return 0;
941 static int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
943 struct sco_conn *conn = hcon->sco_data;
945 if (!conn)
946 goto drop;
948 BT_DBG("conn %p len %d", conn, skb->len);
950 if (skb->len) {
951 sco_recv_frame(conn, skb);
952 return 0;
955 drop:
956 kfree_skb(skb);
957 return 0;
960 static int sco_debugfs_show(struct seq_file *f, void *p)
962 struct sock *sk;
963 struct hlist_node *node;
965 read_lock_bh(&sco_sk_list.lock);
967 sk_for_each(sk, node, &sco_sk_list.head) {
968 seq_printf(f, "%s %s %d\n", batostr(&bt_sk(sk)->src),
969 batostr(&bt_sk(sk)->dst), sk->sk_state);
972 read_unlock_bh(&sco_sk_list.lock);
974 return 0;
977 static int sco_debugfs_open(struct inode *inode, struct file *file)
979 return single_open(file, sco_debugfs_show, inode->i_private);
982 static const struct file_operations sco_debugfs_fops = {
983 .open = sco_debugfs_open,
984 .read = seq_read,
985 .llseek = seq_lseek,
986 .release = single_release,
989 static struct dentry *sco_debugfs;
991 static const struct proto_ops sco_sock_ops = {
992 .family = PF_BLUETOOTH,
993 .owner = THIS_MODULE,
994 .release = sco_sock_release,
995 .bind = sco_sock_bind,
996 .connect = sco_sock_connect,
997 .listen = sco_sock_listen,
998 .accept = sco_sock_accept,
999 .getname = sco_sock_getname,
1000 .sendmsg = sco_sock_sendmsg,
1001 .recvmsg = bt_sock_recvmsg,
1002 .poll = bt_sock_poll,
1003 .ioctl = bt_sock_ioctl,
1004 .mmap = sock_no_mmap,
1005 .socketpair = sock_no_socketpair,
1006 .shutdown = sco_sock_shutdown,
1007 .setsockopt = sco_sock_setsockopt,
1008 .getsockopt = sco_sock_getsockopt
1011 static const struct net_proto_family sco_sock_family_ops = {
1012 .family = PF_BLUETOOTH,
1013 .owner = THIS_MODULE,
1014 .create = sco_sock_create,
1017 static struct hci_proto sco_hci_proto = {
1018 .name = "SCO",
1019 .id = HCI_PROTO_SCO,
1020 .connect_ind = sco_connect_ind,
1021 .connect_cfm = sco_connect_cfm,
1022 .disconn_cfm = sco_disconn_cfm,
1023 .recv_scodata = sco_recv_scodata
1026 static int __init sco_init(void)
1028 int err;
1030 err = proto_register(&sco_proto, 0);
1031 if (err < 0)
1032 return err;
1034 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1035 if (err < 0) {
1036 BT_ERR("SCO socket registration failed");
1037 goto error;
1040 err = hci_register_proto(&sco_hci_proto);
1041 if (err < 0) {
1042 BT_ERR("SCO protocol registration failed");
1043 bt_sock_unregister(BTPROTO_SCO);
1044 goto error;
1047 if (bt_debugfs) {
1048 sco_debugfs = debugfs_create_file("sco", 0444,
1049 bt_debugfs, NULL, &sco_debugfs_fops);
1050 if (!sco_debugfs)
1051 BT_ERR("Failed to create SCO debug file");
1054 BT_INFO("SCO (Voice Link) ver %s", VERSION);
1055 BT_INFO("SCO socket layer initialized");
1057 return 0;
1059 error:
1060 proto_unregister(&sco_proto);
1061 return err;
1064 static void __exit sco_exit(void)
1066 debugfs_remove(sco_debugfs);
1068 if (bt_sock_unregister(BTPROTO_SCO) < 0)
1069 BT_ERR("SCO socket unregistration failed");
1071 if (hci_unregister_proto(&sco_hci_proto) < 0)
1072 BT_ERR("SCO protocol unregistration failed");
1074 proto_unregister(&sco_proto);
1077 module_init(sco_init);
1078 module_exit(sco_exit);
1080 module_param(disable_esco, bool, 0644);
1081 MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");
1083 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1084 MODULE_DESCRIPTION("Bluetooth SCO ver " VERSION);
1085 MODULE_VERSION(VERSION);
1086 MODULE_LICENSE("GPL");
1087 MODULE_ALIAS("bt-proto-2");