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>
46 #include <asm/system.h>
47 #include <asm/uaccess.h>
49 #include <net/bluetooth/bluetooth.h>
50 #include <net/bluetooth/hci_core.h>
51 #include <net/bluetooth/sco.h>
55 static int disable_esco = 0;
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);
79 sk->sk_err = ETIMEDOUT;
80 sk->sk_state_change(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;
108 conn = kzalloc(sizeof(struct sco_conn), GFP_ATOMIC);
112 spin_lock_init(&conn->lock);
114 hcon->sco_data = conn;
117 conn->src = &hdev->bdaddr;
118 conn->dst = &hcon->dst;
120 if (hdev->sco_mtu > 0)
121 conn->mtu = hdev->sco_mtu;
125 BT_DBG("hcon %p conn %p", hcon, conn);
130 static inline struct sock *sco_chan_get(struct sco_conn *conn)
132 struct sock *sk = NULL;
135 sco_conn_unlock(conn);
139 static int sco_conn_del(struct hci_conn *hcon, int err)
141 struct sco_conn *conn;
144 if (!(conn = hcon->sco_data))
147 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
150 if ((sk = sco_chan_get(conn))) {
152 sco_sock_clear_timer(sk);
153 sco_chan_del(sk, err);
158 hcon->sco_data = NULL;
163 static inline int sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
171 __sco_chan_add(conn, sk, parent);
173 sco_conn_unlock(conn);
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;
186 BT_DBG("%s -> %s", batostr(src), batostr(dst));
188 if (!(hdev = hci_get_route(dst, src)))
189 return -EHOSTUNREACH;
191 hci_dev_lock_bh(hdev);
195 if (lmp_esco_capable(hdev) && !disable_esco)
200 hcon = hci_connect(hdev, type, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING);
204 conn = sco_conn_add(hcon, 0);
210 /* Update source addr of the socket */
211 bacpy(src, conn->src);
213 err = sco_chan_add(conn, sk, NULL);
217 if (hcon->state == BT_CONNECTED) {
218 sco_sock_clear_timer(sk);
219 sk->sk_state = BT_CONNECTED;
221 sk->sk_state = BT_CONNECT;
222 sco_sock_set_timer(sk, sk->sk_sndtimeo);
226 hci_dev_unlock_bh(hdev);
231 static inline int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
233 struct sco_conn *conn = sco_pi(sk)->conn;
237 /* Check outgoing MTU */
241 BT_DBG("sk %p len %d", sk, len);
243 count = min_t(unsigned int, conn->mtu, len);
244 skb = bt_skb_send_alloc(sk, count,
245 msg->msg_flags & MSG_DONTWAIT, &err);
249 if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) {
254 hci_send_sco(conn->hcon, skb);
259 static inline void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
261 struct sock *sk = sco_chan_get(conn);
266 BT_DBG("sk %p len %d", sk, skb->len);
268 if (sk->sk_state != BT_CONNECTED)
271 if (!sock_queue_rcv_skb(sk, skb))
278 /* -------- Socket interface ---------- */
279 static struct sock *__sco_get_sock_by_addr(bdaddr_t *ba)
282 struct hlist_node *node;
284 sk_for_each(sk, node, &sco_sk_list.head)
285 if (!bacmp(&bt_sk(sk)->src, ba))
292 /* Find socket listening on source bdaddr.
293 * Returns closest match.
295 static struct sock *sco_get_sock_listen(bdaddr_t *src)
297 struct sock *sk = NULL, *sk1 = NULL;
298 struct hlist_node *node;
300 read_lock(&sco_sk_list.lock);
302 sk_for_each(sk, node, &sco_sk_list.head) {
303 if (sk->sk_state != BT_LISTEN)
307 if (!bacmp(&bt_sk(sk)->src, src))
311 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
315 read_unlock(&sco_sk_list.lock);
317 return node ? sk : sk1;
320 static void sco_sock_destruct(struct sock *sk)
324 skb_queue_purge(&sk->sk_receive_queue);
325 skb_queue_purge(&sk->sk_write_queue);
328 static void sco_sock_cleanup_listen(struct sock *parent)
332 BT_DBG("parent %p", parent);
334 /* Close not yet accepted channels */
335 while ((sk = bt_accept_dequeue(parent, NULL))) {
340 parent->sk_state = BT_CLOSED;
341 sock_set_flag(parent, SOCK_ZAPPED);
344 /* Kill socket (only if zapped and orphan)
345 * Must be called on unlocked socket.
347 static void sco_sock_kill(struct sock *sk)
349 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
352 BT_DBG("sk %p state %d", sk, sk->sk_state);
354 /* Kill poor orphan */
355 bt_sock_unlink(&sco_sk_list, sk);
356 sock_set_flag(sk, SOCK_DEAD);
360 static void __sco_sock_close(struct sock *sk)
362 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
364 switch (sk->sk_state) {
366 sco_sock_cleanup_listen(sk);
373 sco_chan_del(sk, ECONNRESET);
377 sock_set_flag(sk, SOCK_ZAPPED);
382 /* Must be called on unlocked socket. */
383 static void sco_sock_close(struct sock *sk)
385 sco_sock_clear_timer(sk);
387 __sco_sock_close(sk);
392 static void sco_sock_init(struct sock *sk, struct sock *parent)
397 sk->sk_type = parent->sk_type;
400 static struct proto sco_proto = {
402 .owner = THIS_MODULE,
403 .obj_size = sizeof(struct sco_pinfo)
406 static struct sock *sco_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
410 sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto);
414 sock_init_data(sock, sk);
415 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
417 sk->sk_destruct = sco_sock_destruct;
418 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
420 sock_reset_flag(sk, SOCK_ZAPPED);
422 sk->sk_protocol = proto;
423 sk->sk_state = BT_OPEN;
425 setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk);
427 bt_sock_link(&sco_sk_list, sk);
431 static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
436 BT_DBG("sock %p", sock);
438 sock->state = SS_UNCONNECTED;
440 if (sock->type != SOCK_SEQPACKET)
441 return -ESOCKTNOSUPPORT;
443 sock->ops = &sco_sock_ops;
445 sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC);
449 sco_sock_init(sk, NULL);
453 static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
455 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
456 struct sock *sk = sock->sk;
457 bdaddr_t *src = &sa->sco_bdaddr;
460 BT_DBG("sk %p %s", sk, batostr(&sa->sco_bdaddr));
462 if (!addr || addr->sa_family != AF_BLUETOOTH)
467 if (sk->sk_state != BT_OPEN) {
472 write_lock_bh(&sco_sk_list.lock);
474 if (bacmp(src, BDADDR_ANY) && __sco_get_sock_by_addr(src)) {
477 /* Save source address */
478 bacpy(&bt_sk(sk)->src, &sa->sco_bdaddr);
479 sk->sk_state = BT_BOUND;
482 write_unlock_bh(&sco_sk_list.lock);
489 static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
491 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
492 struct sock *sk = sock->sk;
498 if (alen < sizeof(struct sockaddr_sco) ||
499 addr->sa_family != AF_BLUETOOTH)
502 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
505 if (sk->sk_type != SOCK_SEQPACKET)
510 /* Set destination address and psm */
511 bacpy(&bt_sk(sk)->dst, &sa->sco_bdaddr);
513 if ((err = sco_connect(sk)))
516 err = bt_sock_wait_state(sk, BT_CONNECTED,
517 sock_sndtimeo(sk, flags & O_NONBLOCK));
524 static int sco_sock_listen(struct socket *sock, int backlog)
526 struct sock *sk = sock->sk;
529 BT_DBG("sk %p backlog %d", sk, backlog);
533 if (sk->sk_state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
538 sk->sk_max_ack_backlog = backlog;
539 sk->sk_ack_backlog = 0;
540 sk->sk_state = BT_LISTEN;
547 static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
549 DECLARE_WAITQUEUE(wait, current);
550 struct sock *sk = sock->sk, *ch;
556 if (sk->sk_state != BT_LISTEN) {
561 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
563 BT_DBG("sk %p timeo %ld", sk, timeo);
565 /* Wait for an incoming connection. (wake-one). */
566 add_wait_queue_exclusive(sk_sleep(sk), &wait);
567 while (!(ch = bt_accept_dequeue(sk, newsock))) {
568 set_current_state(TASK_INTERRUPTIBLE);
575 timeo = schedule_timeout(timeo);
578 if (sk->sk_state != BT_LISTEN) {
583 if (signal_pending(current)) {
584 err = sock_intr_errno(timeo);
588 set_current_state(TASK_RUNNING);
589 remove_wait_queue(sk_sleep(sk), &wait);
594 newsock->state = SS_CONNECTED;
596 BT_DBG("new socket %p", ch);
603 static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
605 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
606 struct sock *sk = sock->sk;
608 BT_DBG("sock %p, sk %p", sock, sk);
610 addr->sa_family = AF_BLUETOOTH;
611 *len = sizeof(struct sockaddr_sco);
614 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst);
616 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src);
621 static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
622 struct msghdr *msg, size_t len)
624 struct sock *sk = sock->sk;
627 BT_DBG("sock %p, sk %p", sock, sk);
629 err = sock_error(sk);
633 if (msg->msg_flags & MSG_OOB)
638 if (sk->sk_state == BT_CONNECTED)
639 err = sco_send_frame(sk, msg, len);
647 static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
649 struct sock *sk = sock->sk;
666 static int sco_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
668 struct sock *sk = sock->sk;
669 struct sco_options opts;
670 struct sco_conninfo cinfo;
675 if (get_user(len, optlen))
682 if (sk->sk_state != BT_CONNECTED) {
687 opts.mtu = sco_pi(sk)->conn->mtu;
689 BT_DBG("mtu %d", opts.mtu);
691 len = min_t(unsigned int, len, sizeof(opts));
692 if (copy_to_user(optval, (char *)&opts, len))
698 if (sk->sk_state != BT_CONNECTED) {
703 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
704 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
706 len = min_t(unsigned int, len, sizeof(cinfo));
707 if (copy_to_user(optval, (char *)&cinfo, len))
721 static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
723 struct sock *sk = sock->sk;
728 if (level == SOL_SCO)
729 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
731 if (get_user(len, optlen))
746 static int sco_sock_shutdown(struct socket *sock, int how)
748 struct sock *sk = sock->sk;
751 BT_DBG("sock %p, sk %p", sock, sk);
757 if (!sk->sk_shutdown) {
758 sk->sk_shutdown = SHUTDOWN_MASK;
759 sco_sock_clear_timer(sk);
760 __sco_sock_close(sk);
762 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime)
763 err = bt_sock_wait_state(sk, BT_CLOSED,
770 static int sco_sock_release(struct socket *sock)
772 struct sock *sk = sock->sk;
775 BT_DBG("sock %p, sk %p", sock, sk);
782 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) {
784 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
793 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
795 BT_DBG("conn %p", conn);
797 sco_pi(sk)->conn = conn;
801 bt_accept_enqueue(parent, sk);
805 * Must be called on the locked socket. */
806 static void sco_chan_del(struct sock *sk, int err)
808 struct sco_conn *conn;
810 conn = sco_pi(sk)->conn;
812 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
817 sco_pi(sk)->conn = NULL;
818 sco_conn_unlock(conn);
819 hci_conn_put(conn->hcon);
822 sk->sk_state = BT_CLOSED;
824 sk->sk_state_change(sk);
826 sock_set_flag(sk, SOCK_ZAPPED);
829 static void sco_conn_ready(struct sco_conn *conn)
831 struct sock *parent, *sk;
833 BT_DBG("conn %p", conn);
837 if ((sk = conn->sk)) {
838 sco_sock_clear_timer(sk);
840 sk->sk_state = BT_CONNECTED;
841 sk->sk_state_change(sk);
844 parent = sco_get_sock_listen(conn->src);
848 bh_lock_sock(parent);
850 sk = sco_sock_alloc(sock_net(parent), NULL,
851 BTPROTO_SCO, GFP_ATOMIC);
853 bh_unlock_sock(parent);
857 sco_sock_init(sk, parent);
859 bacpy(&bt_sk(sk)->src, conn->src);
860 bacpy(&bt_sk(sk)->dst, conn->dst);
862 hci_conn_hold(conn->hcon);
863 __sco_chan_add(conn, sk, parent);
865 sk->sk_state = BT_CONNECTED;
868 parent->sk_data_ready(parent, 1);
870 bh_unlock_sock(parent);
874 sco_conn_unlock(conn);
877 /* ----- SCO interface with lower layer (HCI) ----- */
878 static int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
880 register struct sock *sk;
881 struct hlist_node *node;
884 if (type != SCO_LINK && type != ESCO_LINK)
887 BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
889 /* Find listening sockets */
890 read_lock(&sco_sk_list.lock);
891 sk_for_each(sk, node, &sco_sk_list.head) {
892 if (sk->sk_state != BT_LISTEN)
895 if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr) ||
896 !bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
901 read_unlock(&sco_sk_list.lock);
906 static int sco_connect_cfm(struct hci_conn *hcon, __u8 status)
908 BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
910 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
914 struct sco_conn *conn;
916 conn = sco_conn_add(hcon, status);
918 sco_conn_ready(conn);
920 sco_conn_del(hcon, bt_err(status));
925 static int sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
927 BT_DBG("hcon %p reason %d", hcon, reason);
929 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
932 sco_conn_del(hcon, bt_err(reason));
937 static int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
939 struct sco_conn *conn = hcon->sco_data;
944 BT_DBG("conn %p len %d", conn, skb->len);
947 sco_recv_frame(conn, skb);
956 static int sco_debugfs_show(struct seq_file *f, void *p)
959 struct hlist_node *node;
961 read_lock_bh(&sco_sk_list.lock);
963 sk_for_each(sk, node, &sco_sk_list.head) {
964 seq_printf(f, "%s %s %d\n", batostr(&bt_sk(sk)->src),
965 batostr(&bt_sk(sk)->dst), sk->sk_state);
968 read_unlock_bh(&sco_sk_list.lock);
973 static int sco_debugfs_open(struct inode *inode, struct file *file)
975 return single_open(file, sco_debugfs_show, inode->i_private);
978 static const struct file_operations sco_debugfs_fops = {
979 .open = sco_debugfs_open,
982 .release = single_release,
985 static struct dentry *sco_debugfs;
987 static const struct proto_ops sco_sock_ops = {
988 .family = PF_BLUETOOTH,
989 .owner = THIS_MODULE,
990 .release = sco_sock_release,
991 .bind = sco_sock_bind,
992 .connect = sco_sock_connect,
993 .listen = sco_sock_listen,
994 .accept = sco_sock_accept,
995 .getname = sco_sock_getname,
996 .sendmsg = sco_sock_sendmsg,
997 .recvmsg = bt_sock_recvmsg,
998 .poll = bt_sock_poll,
999 .ioctl = bt_sock_ioctl,
1000 .mmap = sock_no_mmap,
1001 .socketpair = sock_no_socketpair,
1002 .shutdown = sco_sock_shutdown,
1003 .setsockopt = sco_sock_setsockopt,
1004 .getsockopt = sco_sock_getsockopt
1007 static const struct net_proto_family sco_sock_family_ops = {
1008 .family = PF_BLUETOOTH,
1009 .owner = THIS_MODULE,
1010 .create = sco_sock_create,
1013 static struct hci_proto sco_hci_proto = {
1015 .id = HCI_PROTO_SCO,
1016 .connect_ind = sco_connect_ind,
1017 .connect_cfm = sco_connect_cfm,
1018 .disconn_cfm = sco_disconn_cfm,
1019 .recv_scodata = sco_recv_scodata
1022 static int __init sco_init(void)
1026 err = proto_register(&sco_proto, 0);
1030 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1032 BT_ERR("SCO socket registration failed");
1036 err = hci_register_proto(&sco_hci_proto);
1038 BT_ERR("SCO protocol registration failed");
1039 bt_sock_unregister(BTPROTO_SCO);
1044 sco_debugfs = debugfs_create_file("sco", 0444,
1045 bt_debugfs, NULL, &sco_debugfs_fops);
1047 BT_ERR("Failed to create SCO debug file");
1050 BT_INFO("SCO (Voice Link) ver %s", VERSION);
1051 BT_INFO("SCO socket layer initialized");
1056 proto_unregister(&sco_proto);
1060 static void __exit sco_exit(void)
1062 debugfs_remove(sco_debugfs);
1064 if (bt_sock_unregister(BTPROTO_SCO) < 0)
1065 BT_ERR("SCO socket unregistration failed");
1067 if (hci_unregister_proto(&sco_hci_proto) < 0)
1068 BT_ERR("SCO protocol unregistration failed");
1070 proto_unregister(&sco_proto);
1073 module_init(sco_init);
1074 module_exit(sco_exit);
1076 module_param(disable_esco, bool, 0644);
1077 MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");
1079 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1080 MODULE_DESCRIPTION("Bluetooth SCO ver " VERSION);
1081 MODULE_VERSION(VERSION);
1082 MODULE_LICENSE("GPL");
1083 MODULE_ALIAS("bt-proto-2");