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/list.h>
44 #include <asm/system.h>
45 #include <asm/uaccess.h>
47 #include <net/bluetooth/bluetooth.h>
48 #include <net/bluetooth/hci_core.h>
49 #include <net/bluetooth/sco.h>
53 static int disable_esco = 0;
55 static const struct proto_ops sco_sock_ops;
57 static struct bt_sock_list sco_sk_list = {
58 .lock = __RW_LOCK_UNLOCKED(sco_sk_list.lock)
61 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent);
62 static void sco_chan_del(struct sock *sk, int err);
64 static int sco_conn_del(struct hci_conn *conn, int err);
66 static void sco_sock_close(struct sock *sk);
67 static void sco_sock_kill(struct sock *sk);
69 /* ---- SCO timers ---- */
70 static void sco_sock_timeout(unsigned long arg)
72 struct sock *sk = (struct sock *) arg;
74 BT_DBG("sock %p state %d", sk, sk->sk_state);
77 sk->sk_err = ETIMEDOUT;
78 sk->sk_state_change(sk);
85 static void sco_sock_set_timer(struct sock *sk, long timeout)
87 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
88 sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
91 static void sco_sock_clear_timer(struct sock *sk)
93 BT_DBG("sock %p state %d", sk, sk->sk_state);
94 sk_stop_timer(sk, &sk->sk_timer);
97 /* ---- SCO connections ---- */
98 static struct sco_conn *sco_conn_add(struct hci_conn *hcon, __u8 status)
100 struct hci_dev *hdev = hcon->hdev;
101 struct sco_conn *conn = hcon->sco_data;
106 conn = kzalloc(sizeof(struct sco_conn), GFP_ATOMIC);
110 spin_lock_init(&conn->lock);
112 hcon->sco_data = conn;
115 conn->src = &hdev->bdaddr;
116 conn->dst = &hcon->dst;
118 if (hdev->sco_mtu > 0)
119 conn->mtu = hdev->sco_mtu;
123 BT_DBG("hcon %p conn %p", hcon, conn);
128 static inline struct sock *sco_chan_get(struct sco_conn *conn)
130 struct sock *sk = NULL;
133 sco_conn_unlock(conn);
137 static int sco_conn_del(struct hci_conn *hcon, int err)
139 struct sco_conn *conn;
142 if (!(conn = hcon->sco_data))
145 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
148 if ((sk = sco_chan_get(conn))) {
150 sco_sock_clear_timer(sk);
151 sco_chan_del(sk, err);
156 hcon->sco_data = NULL;
161 static inline int sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
169 __sco_chan_add(conn, sk, parent);
171 sco_conn_unlock(conn);
175 static int sco_connect(struct sock *sk)
177 bdaddr_t *src = &bt_sk(sk)->src;
178 bdaddr_t *dst = &bt_sk(sk)->dst;
179 struct sco_conn *conn;
180 struct hci_conn *hcon;
181 struct hci_dev *hdev;
184 BT_DBG("%s -> %s", batostr(src), batostr(dst));
186 if (!(hdev = hci_get_route(dst, src)))
187 return -EHOSTUNREACH;
189 hci_dev_lock_bh(hdev);
193 if (lmp_esco_capable(hdev) && !disable_esco)
198 hcon = hci_connect(hdev, type, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING);
202 conn = sco_conn_add(hcon, 0);
208 /* Update source addr of the socket */
209 bacpy(src, conn->src);
211 err = sco_chan_add(conn, sk, NULL);
215 if (hcon->state == BT_CONNECTED) {
216 sco_sock_clear_timer(sk);
217 sk->sk_state = BT_CONNECTED;
219 sk->sk_state = BT_CONNECT;
220 sco_sock_set_timer(sk, sk->sk_sndtimeo);
224 hci_dev_unlock_bh(hdev);
229 static inline int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
231 struct sco_conn *conn = sco_pi(sk)->conn;
235 /* Check outgoing MTU */
239 BT_DBG("sk %p len %d", sk, len);
241 count = min_t(unsigned int, conn->mtu, len);
242 if (!(skb = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err)))
245 if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) {
250 if ((err = hci_send_sco(conn->hcon, skb)) < 0)
260 static inline void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
262 struct sock *sk = sco_chan_get(conn);
267 BT_DBG("sk %p len %d", sk, skb->len);
269 if (sk->sk_state != BT_CONNECTED)
272 if (!sock_queue_rcv_skb(sk, skb))
280 /* -------- Socket interface ---------- */
281 static struct sock *__sco_get_sock_by_addr(bdaddr_t *ba)
284 struct hlist_node *node;
286 sk_for_each(sk, node, &sco_sk_list.head)
287 if (!bacmp(&bt_sk(sk)->src, ba))
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)
309 if (!bacmp(&bt_sk(sk)->src, src))
313 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
317 read_unlock(&sco_sk_list.lock);
319 return node ? sk : sk1;
322 static void sco_sock_destruct(struct sock *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)
334 BT_DBG("parent %p", parent);
336 /* Close not yet accepted channels */
337 while ((sk = bt_accept_dequeue(parent, NULL))) {
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)
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);
363 * Must be called on unlocked socket.
365 static void sco_sock_close(struct sock *sk)
367 struct sco_conn *conn;
369 sco_sock_clear_timer(sk);
373 conn = sco_pi(sk)->conn;
375 BT_DBG("sk %p state %d conn %p socket %p", sk, sk->sk_state, conn, sk->sk_socket);
377 switch (sk->sk_state) {
379 sco_sock_cleanup_listen(sk);
386 sco_chan_del(sk, ECONNRESET);
390 sock_set_flag(sk, SOCK_ZAPPED);
399 static void sco_sock_init(struct sock *sk, struct sock *parent)
404 sk->sk_type = parent->sk_type;
407 static struct proto sco_proto = {
409 .owner = THIS_MODULE,
410 .obj_size = sizeof(struct sco_pinfo)
413 static struct sock *sco_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
417 sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto);
421 sock_init_data(sock, sk);
422 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
424 sk->sk_destruct = sco_sock_destruct;
425 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
427 sock_reset_flag(sk, SOCK_ZAPPED);
429 sk->sk_protocol = proto;
430 sk->sk_state = BT_OPEN;
432 setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk);
434 bt_sock_link(&sco_sk_list, sk);
438 static int sco_sock_create(struct net *net, struct socket *sock, int protocol)
442 BT_DBG("sock %p", sock);
444 sock->state = SS_UNCONNECTED;
446 if (sock->type != SOCK_SEQPACKET)
447 return -ESOCKTNOSUPPORT;
449 sock->ops = &sco_sock_ops;
451 sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC);
455 sco_sock_init(sk, NULL);
459 static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
461 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
462 struct sock *sk = sock->sk;
463 bdaddr_t *src = &sa->sco_bdaddr;
466 BT_DBG("sk %p %s", sk, batostr(&sa->sco_bdaddr));
468 if (!addr || addr->sa_family != AF_BLUETOOTH)
473 if (sk->sk_state != BT_OPEN) {
478 write_lock_bh(&sco_sk_list.lock);
480 if (bacmp(src, BDADDR_ANY) && __sco_get_sock_by_addr(src)) {
483 /* Save source address */
484 bacpy(&bt_sk(sk)->src, &sa->sco_bdaddr);
485 sk->sk_state = BT_BOUND;
488 write_unlock_bh(&sco_sk_list.lock);
495 static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
497 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
498 struct sock *sk = sock->sk;
504 if (addr->sa_family != AF_BLUETOOTH || alen < sizeof(struct sockaddr_sco))
507 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
510 if (sk->sk_type != SOCK_SEQPACKET)
515 /* Set destination address and psm */
516 bacpy(&bt_sk(sk)->dst, &sa->sco_bdaddr);
518 if ((err = sco_connect(sk)))
521 err = bt_sock_wait_state(sk, BT_CONNECTED,
522 sock_sndtimeo(sk, flags & O_NONBLOCK));
529 static int sco_sock_listen(struct socket *sock, int backlog)
531 struct sock *sk = sock->sk;
534 BT_DBG("sk %p backlog %d", sk, backlog);
538 if (sk->sk_state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
543 sk->sk_max_ack_backlog = backlog;
544 sk->sk_ack_backlog = 0;
545 sk->sk_state = BT_LISTEN;
552 static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
554 DECLARE_WAITQUEUE(wait, current);
555 struct sock *sk = sock->sk, *ch;
561 if (sk->sk_state != BT_LISTEN) {
566 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
568 BT_DBG("sk %p timeo %ld", sk, timeo);
570 /* Wait for an incoming connection. (wake-one). */
571 add_wait_queue_exclusive(sk->sk_sleep, &wait);
572 while (!(ch = bt_accept_dequeue(sk, newsock))) {
573 set_current_state(TASK_INTERRUPTIBLE);
580 timeo = schedule_timeout(timeo);
583 if (sk->sk_state != BT_LISTEN) {
588 if (signal_pending(current)) {
589 err = sock_intr_errno(timeo);
593 set_current_state(TASK_RUNNING);
594 remove_wait_queue(sk->sk_sleep, &wait);
599 newsock->state = SS_CONNECTED;
601 BT_DBG("new socket %p", ch);
608 static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
610 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
611 struct sock *sk = sock->sk;
613 BT_DBG("sock %p, sk %p", sock, sk);
615 addr->sa_family = AF_BLUETOOTH;
616 *len = sizeof(struct sockaddr_sco);
619 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst);
621 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src);
626 static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
627 struct msghdr *msg, size_t len)
629 struct sock *sk = sock->sk;
632 BT_DBG("sock %p, sk %p", sock, sk);
634 err = sock_error(sk);
638 if (msg->msg_flags & MSG_OOB)
643 if (sk->sk_state == BT_CONNECTED)
644 err = sco_send_frame(sk, msg, len);
652 static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen)
654 struct sock *sk = sock->sk;
671 static int sco_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
673 struct sock *sk = sock->sk;
674 struct sco_options opts;
675 struct sco_conninfo cinfo;
680 if (get_user(len, optlen))
687 if (sk->sk_state != BT_CONNECTED) {
692 opts.mtu = sco_pi(sk)->conn->mtu;
694 BT_DBG("mtu %d", opts.mtu);
696 len = min_t(unsigned int, len, sizeof(opts));
697 if (copy_to_user(optval, (char *)&opts, len))
703 if (sk->sk_state != BT_CONNECTED) {
708 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
709 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
711 len = min_t(unsigned int, len, sizeof(cinfo));
712 if (copy_to_user(optval, (char *)&cinfo, len))
726 static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
728 struct sock *sk = sock->sk;
733 if (level == SOL_SCO)
734 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
736 if (get_user(len, optlen))
751 static int sco_sock_release(struct socket *sock)
753 struct sock *sk = sock->sk;
756 BT_DBG("sock %p, sk %p", sock, sk);
763 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) {
765 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
774 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
776 BT_DBG("conn %p", conn);
778 sco_pi(sk)->conn = conn;
782 bt_accept_enqueue(parent, sk);
786 * Must be called on the locked socket. */
787 static void sco_chan_del(struct sock *sk, int err)
789 struct sco_conn *conn;
791 conn = sco_pi(sk)->conn;
793 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
798 sco_pi(sk)->conn = NULL;
799 sco_conn_unlock(conn);
800 hci_conn_put(conn->hcon);
803 sk->sk_state = BT_CLOSED;
805 sk->sk_state_change(sk);
807 sock_set_flag(sk, SOCK_ZAPPED);
810 static void sco_conn_ready(struct sco_conn *conn)
812 struct sock *parent, *sk;
814 BT_DBG("conn %p", conn);
818 if ((sk = conn->sk)) {
819 sco_sock_clear_timer(sk);
821 sk->sk_state = BT_CONNECTED;
822 sk->sk_state_change(sk);
825 parent = sco_get_sock_listen(conn->src);
829 bh_lock_sock(parent);
831 sk = sco_sock_alloc(sock_net(parent), NULL, BTPROTO_SCO, GFP_ATOMIC);
833 bh_unlock_sock(parent);
837 sco_sock_init(sk, parent);
839 bacpy(&bt_sk(sk)->src, conn->src);
840 bacpy(&bt_sk(sk)->dst, conn->dst);
842 hci_conn_hold(conn->hcon);
843 __sco_chan_add(conn, sk, parent);
845 sk->sk_state = BT_CONNECTED;
848 parent->sk_data_ready(parent, 1);
850 bh_unlock_sock(parent);
854 sco_conn_unlock(conn);
857 /* ----- SCO interface with lower layer (HCI) ----- */
858 static int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
860 register struct sock *sk;
861 struct hlist_node *node;
864 if (type != SCO_LINK && type != ESCO_LINK)
867 BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
869 /* Find listening sockets */
870 read_lock(&sco_sk_list.lock);
871 sk_for_each(sk, node, &sco_sk_list.head) {
872 if (sk->sk_state != BT_LISTEN)
875 if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr) ||
876 !bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
881 read_unlock(&sco_sk_list.lock);
886 static int sco_connect_cfm(struct hci_conn *hcon, __u8 status)
888 BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
890 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
894 struct sco_conn *conn;
896 conn = sco_conn_add(hcon, status);
898 sco_conn_ready(conn);
900 sco_conn_del(hcon, bt_err(status));
905 static int sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
907 BT_DBG("hcon %p reason %d", hcon, reason);
909 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
912 sco_conn_del(hcon, bt_err(reason));
917 static int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
919 struct sco_conn *conn = hcon->sco_data;
924 BT_DBG("conn %p len %d", conn, skb->len);
927 sco_recv_frame(conn, skb);
936 static ssize_t sco_sysfs_show(struct class *dev, char *buf)
939 struct hlist_node *node;
942 read_lock_bh(&sco_sk_list.lock);
944 sk_for_each(sk, node, &sco_sk_list.head) {
945 str += sprintf(str, "%s %s %d\n",
946 batostr(&bt_sk(sk)->src), batostr(&bt_sk(sk)->dst),
950 read_unlock_bh(&sco_sk_list.lock);
955 static CLASS_ATTR(sco, S_IRUGO, sco_sysfs_show, NULL);
957 static const struct proto_ops sco_sock_ops = {
958 .family = PF_BLUETOOTH,
959 .owner = THIS_MODULE,
960 .release = sco_sock_release,
961 .bind = sco_sock_bind,
962 .connect = sco_sock_connect,
963 .listen = sco_sock_listen,
964 .accept = sco_sock_accept,
965 .getname = sco_sock_getname,
966 .sendmsg = sco_sock_sendmsg,
967 .recvmsg = bt_sock_recvmsg,
968 .poll = bt_sock_poll,
969 .ioctl = bt_sock_ioctl,
970 .mmap = sock_no_mmap,
971 .socketpair = sock_no_socketpair,
972 .shutdown = sock_no_shutdown,
973 .setsockopt = sco_sock_setsockopt,
974 .getsockopt = sco_sock_getsockopt
977 static struct net_proto_family sco_sock_family_ops = {
978 .family = PF_BLUETOOTH,
979 .owner = THIS_MODULE,
980 .create = sco_sock_create,
983 static struct hci_proto sco_hci_proto = {
986 .connect_ind = sco_connect_ind,
987 .connect_cfm = sco_connect_cfm,
988 .disconn_cfm = sco_disconn_cfm,
989 .recv_scodata = sco_recv_scodata
992 static int __init sco_init(void)
996 err = proto_register(&sco_proto, 0);
1000 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1002 BT_ERR("SCO socket registration failed");
1006 err = hci_register_proto(&sco_hci_proto);
1008 BT_ERR("SCO protocol registration failed");
1009 bt_sock_unregister(BTPROTO_SCO);
1013 if (class_create_file(bt_class, &class_attr_sco) < 0)
1014 BT_ERR("Failed to create SCO info file");
1016 BT_INFO("SCO (Voice Link) ver %s", VERSION);
1017 BT_INFO("SCO socket layer initialized");
1022 proto_unregister(&sco_proto);
1026 static void __exit sco_exit(void)
1028 class_remove_file(bt_class, &class_attr_sco);
1030 if (bt_sock_unregister(BTPROTO_SCO) < 0)
1031 BT_ERR("SCO socket unregistration failed");
1033 if (hci_unregister_proto(&sco_hci_proto) < 0)
1034 BT_ERR("SCO protocol unregistration failed");
1036 proto_unregister(&sco_proto);
1039 module_init(sco_init);
1040 module_exit(sco_exit);
1042 module_param(disable_esco, bool, 0644);
1043 MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");
1045 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1046 MODULE_DESCRIPTION("Bluetooth SCO ver " VERSION);
1047 MODULE_VERSION(VERSION);
1048 MODULE_LICENSE("GPL");
1049 MODULE_ALIAS("bt-proto-2");