eCryptfs: Allow 2 scatterlist entries for encrypted filenames
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / bluetooth / sco.c
blob926ed39912ea7e2fd9d601063e0993c85220e226
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 memset(&cinfo, 0, sizeof(cinfo));
707 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
708 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
710 len = min_t(unsigned int, len, sizeof(cinfo));
711 if (copy_to_user(optval, (char *)&cinfo, len))
712 err = -EFAULT;
714 break;
716 default:
717 err = -ENOPROTOOPT;
718 break;
721 release_sock(sk);
722 return err;
725 static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
727 struct sock *sk = sock->sk;
728 int len, err = 0;
730 BT_DBG("sk %p", sk);
732 if (level == SOL_SCO)
733 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
735 if (get_user(len, optlen))
736 return -EFAULT;
738 lock_sock(sk);
740 switch (optname) {
741 default:
742 err = -ENOPROTOOPT;
743 break;
746 release_sock(sk);
747 return err;
750 static int sco_sock_shutdown(struct socket *sock, int how)
752 struct sock *sk = sock->sk;
753 int err = 0;
755 BT_DBG("sock %p, sk %p", sock, sk);
757 if (!sk)
758 return 0;
760 lock_sock(sk);
761 if (!sk->sk_shutdown) {
762 sk->sk_shutdown = SHUTDOWN_MASK;
763 sco_sock_clear_timer(sk);
764 __sco_sock_close(sk);
766 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
767 err = bt_sock_wait_state(sk, BT_CLOSED,
768 sk->sk_lingertime);
770 release_sock(sk);
771 return err;
774 static int sco_sock_release(struct socket *sock)
776 struct sock *sk = sock->sk;
777 int err = 0;
779 BT_DBG("sock %p, sk %p", sock, sk);
781 if (!sk)
782 return 0;
784 sco_sock_close(sk);
786 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) {
787 lock_sock(sk);
788 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
789 release_sock(sk);
792 sock_orphan(sk);
793 sco_sock_kill(sk);
794 return err;
797 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
799 BT_DBG("conn %p", conn);
801 sco_pi(sk)->conn = conn;
802 conn->sk = sk;
804 if (parent)
805 bt_accept_enqueue(parent, sk);
808 /* Delete channel.
809 * Must be called on the locked socket. */
810 static void sco_chan_del(struct sock *sk, int err)
812 struct sco_conn *conn;
814 conn = sco_pi(sk)->conn;
816 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
818 if (conn) {
819 sco_conn_lock(conn);
820 conn->sk = NULL;
821 sco_pi(sk)->conn = NULL;
822 sco_conn_unlock(conn);
823 hci_conn_put(conn->hcon);
826 sk->sk_state = BT_CLOSED;
827 sk->sk_err = err;
828 sk->sk_state_change(sk);
830 sock_set_flag(sk, SOCK_ZAPPED);
833 static void sco_conn_ready(struct sco_conn *conn)
835 struct sock *parent;
836 struct sock *sk = conn->sk;
838 BT_DBG("conn %p", conn);
840 sco_conn_lock(conn);
842 if (sk) {
843 sco_sock_clear_timer(sk);
844 bh_lock_sock(sk);
845 sk->sk_state = BT_CONNECTED;
846 sk->sk_state_change(sk);
847 bh_unlock_sock(sk);
848 } else {
849 parent = sco_get_sock_listen(conn->src);
850 if (!parent)
851 goto done;
853 bh_lock_sock(parent);
855 sk = sco_sock_alloc(sock_net(parent), NULL,
856 BTPROTO_SCO, GFP_ATOMIC);
857 if (!sk) {
858 bh_unlock_sock(parent);
859 goto done;
862 sco_sock_init(sk, parent);
864 bacpy(&bt_sk(sk)->src, conn->src);
865 bacpy(&bt_sk(sk)->dst, conn->dst);
867 hci_conn_hold(conn->hcon);
868 __sco_chan_add(conn, sk, parent);
870 sk->sk_state = BT_CONNECTED;
872 /* Wake up parent */
873 parent->sk_data_ready(parent, 1);
875 bh_unlock_sock(parent);
878 done:
879 sco_conn_unlock(conn);
882 /* ----- SCO interface with lower layer (HCI) ----- */
883 static int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
885 register struct sock *sk;
886 struct hlist_node *node;
887 int lm = 0;
889 if (type != SCO_LINK && type != ESCO_LINK)
890 return -EINVAL;
892 BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
894 /* Find listening sockets */
895 read_lock(&sco_sk_list.lock);
896 sk_for_each(sk, node, &sco_sk_list.head) {
897 if (sk->sk_state != BT_LISTEN)
898 continue;
900 if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr) ||
901 !bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
902 lm |= HCI_LM_ACCEPT;
903 break;
906 read_unlock(&sco_sk_list.lock);
908 return lm;
911 static int sco_connect_cfm(struct hci_conn *hcon, __u8 status)
913 BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
915 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
916 return -EINVAL;
918 if (!status) {
919 struct sco_conn *conn;
921 conn = sco_conn_add(hcon, status);
922 if (conn)
923 sco_conn_ready(conn);
924 } else
925 sco_conn_del(hcon, bt_err(status));
927 return 0;
930 static int sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
932 BT_DBG("hcon %p reason %d", hcon, reason);
934 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
935 return -EINVAL;
937 sco_conn_del(hcon, bt_err(reason));
939 return 0;
942 static int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
944 struct sco_conn *conn = hcon->sco_data;
946 if (!conn)
947 goto drop;
949 BT_DBG("conn %p len %d", conn, skb->len);
951 if (skb->len) {
952 sco_recv_frame(conn, skb);
953 return 0;
956 drop:
957 kfree_skb(skb);
958 return 0;
961 static int sco_debugfs_show(struct seq_file *f, void *p)
963 struct sock *sk;
964 struct hlist_node *node;
966 read_lock_bh(&sco_sk_list.lock);
968 sk_for_each(sk, node, &sco_sk_list.head) {
969 seq_printf(f, "%s %s %d\n", batostr(&bt_sk(sk)->src),
970 batostr(&bt_sk(sk)->dst), sk->sk_state);
973 read_unlock_bh(&sco_sk_list.lock);
975 return 0;
978 static int sco_debugfs_open(struct inode *inode, struct file *file)
980 return single_open(file, sco_debugfs_show, inode->i_private);
983 static const struct file_operations sco_debugfs_fops = {
984 .open = sco_debugfs_open,
985 .read = seq_read,
986 .llseek = seq_lseek,
987 .release = single_release,
990 static struct dentry *sco_debugfs;
992 static const struct proto_ops sco_sock_ops = {
993 .family = PF_BLUETOOTH,
994 .owner = THIS_MODULE,
995 .release = sco_sock_release,
996 .bind = sco_sock_bind,
997 .connect = sco_sock_connect,
998 .listen = sco_sock_listen,
999 .accept = sco_sock_accept,
1000 .getname = sco_sock_getname,
1001 .sendmsg = sco_sock_sendmsg,
1002 .recvmsg = bt_sock_recvmsg,
1003 .poll = bt_sock_poll,
1004 .ioctl = bt_sock_ioctl,
1005 .mmap = sock_no_mmap,
1006 .socketpair = sock_no_socketpair,
1007 .shutdown = sco_sock_shutdown,
1008 .setsockopt = sco_sock_setsockopt,
1009 .getsockopt = sco_sock_getsockopt
1012 static const struct net_proto_family sco_sock_family_ops = {
1013 .family = PF_BLUETOOTH,
1014 .owner = THIS_MODULE,
1015 .create = sco_sock_create,
1018 static struct hci_proto sco_hci_proto = {
1019 .name = "SCO",
1020 .id = HCI_PROTO_SCO,
1021 .connect_ind = sco_connect_ind,
1022 .connect_cfm = sco_connect_cfm,
1023 .disconn_cfm = sco_disconn_cfm,
1024 .recv_scodata = sco_recv_scodata
1027 static int __init sco_init(void)
1029 int err;
1031 err = proto_register(&sco_proto, 0);
1032 if (err < 0)
1033 return err;
1035 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1036 if (err < 0) {
1037 BT_ERR("SCO socket registration failed");
1038 goto error;
1041 err = hci_register_proto(&sco_hci_proto);
1042 if (err < 0) {
1043 BT_ERR("SCO protocol registration failed");
1044 bt_sock_unregister(BTPROTO_SCO);
1045 goto error;
1048 if (bt_debugfs) {
1049 sco_debugfs = debugfs_create_file("sco", 0444,
1050 bt_debugfs, NULL, &sco_debugfs_fops);
1051 if (!sco_debugfs)
1052 BT_ERR("Failed to create SCO debug file");
1055 BT_INFO("SCO (Voice Link) ver %s", VERSION);
1056 BT_INFO("SCO socket layer initialized");
1058 return 0;
1060 error:
1061 proto_unregister(&sco_proto);
1062 return err;
1065 static void __exit sco_exit(void)
1067 debugfs_remove(sco_debugfs);
1069 if (bt_sock_unregister(BTPROTO_SCO) < 0)
1070 BT_ERR("SCO socket unregistration failed");
1072 if (hci_unregister_proto(&sco_hci_proto) < 0)
1073 BT_ERR("SCO protocol unregistration failed");
1075 proto_unregister(&sco_proto);
1078 module_init(sco_init);
1079 module_exit(sco_exit);
1081 module_param(disable_esco, bool, 0644);
1082 MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");
1084 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1085 MODULE_DESCRIPTION("Bluetooth SCO ver " VERSION);
1086 MODULE_VERSION(VERSION);
1087 MODULE_LICENSE("GPL");
1088 MODULE_ALIAS("bt-proto-2");