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>
51 #ifndef CONFIG_BT_SCO_DEBUG
58 static const struct proto_ops sco_sock_ops;
60 static struct bt_sock_list sco_sk_list = {
61 .lock = RW_LOCK_UNLOCKED
64 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent);
65 static void sco_chan_del(struct sock *sk, int err);
67 static int sco_conn_del(struct hci_conn *conn, int err);
69 static void sco_sock_close(struct sock *sk);
70 static void sco_sock_kill(struct sock *sk);
72 /* ---- SCO timers ---- */
73 static void sco_sock_timeout(unsigned long arg)
75 struct sock *sk = (struct sock *) arg;
77 BT_DBG("sock %p state %d", sk, sk->sk_state);
80 sk->sk_err = ETIMEDOUT;
81 sk->sk_state_change(sk);
88 static void sco_sock_set_timer(struct sock *sk, long timeout)
90 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
91 sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
94 static void sco_sock_clear_timer(struct sock *sk)
96 BT_DBG("sock %p state %d", sk, sk->sk_state);
97 sk_stop_timer(sk, &sk->sk_timer);
100 static void sco_sock_init_timer(struct sock *sk)
102 init_timer(&sk->sk_timer);
103 sk->sk_timer.function = sco_sock_timeout;
104 sk->sk_timer.data = (unsigned long)sk;
107 /* ---- SCO connections ---- */
108 static struct sco_conn *sco_conn_add(struct hci_conn *hcon, __u8 status)
110 struct hci_dev *hdev = hcon->hdev;
111 struct sco_conn *conn = hcon->sco_data;
116 conn = kzalloc(sizeof(struct sco_conn), GFP_ATOMIC);
120 spin_lock_init(&conn->lock);
122 hcon->sco_data = conn;
125 conn->src = &hdev->bdaddr;
126 conn->dst = &hcon->dst;
128 if (hdev->sco_mtu > 0)
129 conn->mtu = hdev->sco_mtu;
133 BT_DBG("hcon %p conn %p", hcon, conn);
138 static inline struct sock *sco_chan_get(struct sco_conn *conn)
140 struct sock *sk = NULL;
143 sco_conn_unlock(conn);
147 static int sco_conn_del(struct hci_conn *hcon, int err)
149 struct sco_conn *conn;
152 if (!(conn = hcon->sco_data))
155 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
158 if ((sk = sco_chan_get(conn))) {
160 sco_sock_clear_timer(sk);
161 sco_chan_del(sk, err);
166 hcon->sco_data = NULL;
171 static inline int sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
179 __sco_chan_add(conn, sk, parent);
181 sco_conn_unlock(conn);
185 static int sco_connect(struct sock *sk)
187 bdaddr_t *src = &bt_sk(sk)->src;
188 bdaddr_t *dst = &bt_sk(sk)->dst;
189 struct sco_conn *conn;
190 struct hci_conn *hcon;
191 struct hci_dev *hdev;
194 BT_DBG("%s -> %s", batostr(src), batostr(dst));
196 if (!(hdev = hci_get_route(dst, src)))
197 return -EHOSTUNREACH;
199 hci_dev_lock_bh(hdev);
203 type = lmp_esco_capable(hdev) ? ESCO_LINK : SCO_LINK;
205 hcon = hci_connect(hdev, type, dst);
209 conn = sco_conn_add(hcon, 0);
215 /* Update source addr of the socket */
216 bacpy(src, conn->src);
218 err = sco_chan_add(conn, sk, NULL);
222 if (hcon->state == BT_CONNECTED) {
223 sco_sock_clear_timer(sk);
224 sk->sk_state = BT_CONNECTED;
226 sk->sk_state = BT_CONNECT;
227 sco_sock_set_timer(sk, sk->sk_sndtimeo);
231 hci_dev_unlock_bh(hdev);
236 static inline int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
238 struct sco_conn *conn = sco_pi(sk)->conn;
242 /* Check outgoing MTU */
246 BT_DBG("sk %p len %d", sk, len);
248 count = min_t(unsigned int, conn->mtu, len);
249 if (!(skb = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err)))
252 if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) {
257 if ((err = hci_send_sco(conn->hcon, skb)) < 0)
267 static inline void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
269 struct sock *sk = sco_chan_get(conn);
274 BT_DBG("sk %p len %d", sk, skb->len);
276 if (sk->sk_state != BT_CONNECTED)
279 if (!sock_queue_rcv_skb(sk, skb))
287 /* -------- Socket interface ---------- */
288 static struct sock *__sco_get_sock_by_addr(bdaddr_t *ba)
291 struct hlist_node *node;
293 sk_for_each(sk, node, &sco_sk_list.head)
294 if (!bacmp(&bt_sk(sk)->src, ba))
301 /* Find socket listening on source bdaddr.
302 * Returns closest match.
304 static struct sock *sco_get_sock_listen(bdaddr_t *src)
306 struct sock *sk = NULL, *sk1 = NULL;
307 struct hlist_node *node;
309 read_lock(&sco_sk_list.lock);
311 sk_for_each(sk, node, &sco_sk_list.head) {
312 if (sk->sk_state != BT_LISTEN)
316 if (!bacmp(&bt_sk(sk)->src, src))
320 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
324 read_unlock(&sco_sk_list.lock);
326 return node ? sk : sk1;
329 static void sco_sock_destruct(struct sock *sk)
333 skb_queue_purge(&sk->sk_receive_queue);
334 skb_queue_purge(&sk->sk_write_queue);
337 static void sco_sock_cleanup_listen(struct sock *parent)
341 BT_DBG("parent %p", parent);
343 /* Close not yet accepted channels */
344 while ((sk = bt_accept_dequeue(parent, NULL))) {
349 parent->sk_state = BT_CLOSED;
350 sock_set_flag(parent, SOCK_ZAPPED);
353 /* Kill socket (only if zapped and orphan)
354 * Must be called on unlocked socket.
356 static void sco_sock_kill(struct sock *sk)
358 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
361 BT_DBG("sk %p state %d", sk, sk->sk_state);
363 /* Kill poor orphan */
364 bt_sock_unlink(&sco_sk_list, sk);
365 sock_set_flag(sk, SOCK_DEAD);
370 * Must be called on unlocked socket.
372 static void sco_sock_close(struct sock *sk)
374 struct sco_conn *conn;
376 sco_sock_clear_timer(sk);
380 conn = sco_pi(sk)->conn;
382 BT_DBG("sk %p state %d conn %p socket %p", sk, sk->sk_state, conn, sk->sk_socket);
384 switch (sk->sk_state) {
386 sco_sock_cleanup_listen(sk);
393 sco_chan_del(sk, ECONNRESET);
397 sock_set_flag(sk, SOCK_ZAPPED);
406 static void sco_sock_init(struct sock *sk, struct sock *parent)
411 sk->sk_type = parent->sk_type;
414 static struct proto sco_proto = {
416 .owner = THIS_MODULE,
417 .obj_size = sizeof(struct sco_pinfo)
420 static struct sock *sco_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio)
424 sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto, 1);
428 sock_init_data(sock, sk);
429 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
431 sk->sk_destruct = sco_sock_destruct;
432 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
434 sock_reset_flag(sk, SOCK_ZAPPED);
436 sk->sk_protocol = proto;
437 sk->sk_state = BT_OPEN;
439 sco_sock_init_timer(sk);
441 bt_sock_link(&sco_sk_list, sk);
445 static int sco_sock_create(struct net *net, struct socket *sock, int protocol)
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);
462 sco_sock_init(sk, NULL);
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;
473 BT_DBG("sk %p %s", sk, batostr(&sa->sco_bdaddr));
475 if (!addr || addr->sa_family != AF_BLUETOOTH)
480 if (sk->sk_state != BT_OPEN) {
485 write_lock_bh(&sco_sk_list.lock);
487 if (bacmp(src, BDADDR_ANY) && __sco_get_sock_by_addr(src)) {
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);
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;
511 if (addr->sa_family != AF_BLUETOOTH || alen < sizeof(struct sockaddr_sco))
514 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
517 if (sk->sk_type != SOCK_SEQPACKET)
522 /* Set destination address and psm */
523 bacpy(&bt_sk(sk)->dst, &sa->sco_bdaddr);
525 if ((err = sco_connect(sk)))
528 err = bt_sock_wait_state(sk, BT_CONNECTED,
529 sock_sndtimeo(sk, flags & O_NONBLOCK));
536 static int sco_sock_listen(struct socket *sock, int backlog)
538 struct sock *sk = sock->sk;
541 BT_DBG("sk %p backlog %d", sk, backlog);
545 if (sk->sk_state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
550 sk->sk_max_ack_backlog = backlog;
551 sk->sk_ack_backlog = 0;
552 sk->sk_state = BT_LISTEN;
559 static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
561 DECLARE_WAITQUEUE(wait, current);
562 struct sock *sk = sock->sk, *ch;
568 if (sk->sk_state != BT_LISTEN) {
573 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
575 BT_DBG("sk %p timeo %ld", sk, timeo);
577 /* Wait for an incoming connection. (wake-one). */
578 add_wait_queue_exclusive(sk->sk_sleep, &wait);
579 while (!(ch = bt_accept_dequeue(sk, newsock))) {
580 set_current_state(TASK_INTERRUPTIBLE);
587 timeo = schedule_timeout(timeo);
590 if (sk->sk_state != BT_LISTEN) {
595 if (signal_pending(current)) {
596 err = sock_intr_errno(timeo);
600 set_current_state(TASK_RUNNING);
601 remove_wait_queue(sk->sk_sleep, &wait);
606 newsock->state = SS_CONNECTED;
608 BT_DBG("new socket %p", ch);
615 static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
617 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
618 struct sock *sk = sock->sk;
620 BT_DBG("sock %p, sk %p", sock, sk);
622 addr->sa_family = AF_BLUETOOTH;
623 *len = sizeof(struct sockaddr_sco);
626 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst);
628 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src);
633 static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
634 struct msghdr *msg, size_t len)
636 struct sock *sk = sock->sk;
639 BT_DBG("sock %p, sk %p", sock, sk);
641 err = sock_error(sk);
645 if (msg->msg_flags & MSG_OOB)
650 if (sk->sk_state == BT_CONNECTED)
651 err = sco_send_frame(sk, msg, len);
659 static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen)
661 struct sock *sk = sock->sk;
678 static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
680 struct sock *sk = sock->sk;
681 struct sco_options opts;
682 struct sco_conninfo cinfo;
687 if (get_user(len, optlen))
694 if (sk->sk_state != BT_CONNECTED) {
699 opts.mtu = sco_pi(sk)->conn->mtu;
701 BT_DBG("mtu %d", opts.mtu);
703 len = min_t(unsigned int, len, sizeof(opts));
704 if (copy_to_user(optval, (char *)&opts, len))
710 if (sk->sk_state != BT_CONNECTED) {
715 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
716 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
718 len = min_t(unsigned int, len, sizeof(cinfo));
719 if (copy_to_user(optval, (char *)&cinfo, len))
733 static int sco_sock_release(struct socket *sock)
735 struct sock *sk = sock->sk;
738 BT_DBG("sock %p, sk %p", sock, sk);
745 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) {
747 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
756 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
758 BT_DBG("conn %p", conn);
760 sco_pi(sk)->conn = conn;
764 bt_accept_enqueue(parent, sk);
768 * Must be called on the locked socket. */
769 static void sco_chan_del(struct sock *sk, int err)
771 struct sco_conn *conn;
773 conn = sco_pi(sk)->conn;
775 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
780 sco_pi(sk)->conn = NULL;
781 sco_conn_unlock(conn);
782 hci_conn_put(conn->hcon);
785 sk->sk_state = BT_CLOSED;
787 sk->sk_state_change(sk);
789 sock_set_flag(sk, SOCK_ZAPPED);
792 static void sco_conn_ready(struct sco_conn *conn)
794 struct sock *parent, *sk;
796 BT_DBG("conn %p", conn);
800 if ((sk = conn->sk)) {
801 sco_sock_clear_timer(sk);
803 sk->sk_state = BT_CONNECTED;
804 sk->sk_state_change(sk);
807 parent = sco_get_sock_listen(conn->src);
811 bh_lock_sock(parent);
813 sk = sco_sock_alloc(parent->sk_net, NULL, BTPROTO_SCO, GFP_ATOMIC);
815 bh_unlock_sock(parent);
819 sco_sock_init(sk, parent);
821 bacpy(&bt_sk(sk)->src, conn->src);
822 bacpy(&bt_sk(sk)->dst, conn->dst);
824 hci_conn_hold(conn->hcon);
825 __sco_chan_add(conn, sk, parent);
827 sk->sk_state = BT_CONNECTED;
830 parent->sk_data_ready(parent, 1);
832 bh_unlock_sock(parent);
836 sco_conn_unlock(conn);
839 /* ----- SCO interface with lower layer (HCI) ----- */
840 static int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
842 BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
844 /* Always accept connection */
845 return HCI_LM_ACCEPT;
848 static int sco_connect_cfm(struct hci_conn *hcon, __u8 status)
850 BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
852 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
856 struct sco_conn *conn;
858 conn = sco_conn_add(hcon, status);
860 sco_conn_ready(conn);
862 sco_conn_del(hcon, bt_err(status));
867 static int sco_disconn_ind(struct hci_conn *hcon, __u8 reason)
869 BT_DBG("hcon %p reason %d", hcon, reason);
871 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
874 sco_conn_del(hcon, bt_err(reason));
879 static int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
881 struct sco_conn *conn = hcon->sco_data;
886 BT_DBG("conn %p len %d", conn, skb->len);
889 sco_recv_frame(conn, skb);
898 static ssize_t sco_sysfs_show(struct class *dev, char *buf)
901 struct hlist_node *node;
904 read_lock_bh(&sco_sk_list.lock);
906 sk_for_each(sk, node, &sco_sk_list.head) {
907 str += sprintf(str, "%s %s %d\n",
908 batostr(&bt_sk(sk)->src), batostr(&bt_sk(sk)->dst),
912 read_unlock_bh(&sco_sk_list.lock);
917 static CLASS_ATTR(sco, S_IRUGO, sco_sysfs_show, NULL);
919 static const struct proto_ops sco_sock_ops = {
920 .family = PF_BLUETOOTH,
921 .owner = THIS_MODULE,
922 .release = sco_sock_release,
923 .bind = sco_sock_bind,
924 .connect = sco_sock_connect,
925 .listen = sco_sock_listen,
926 .accept = sco_sock_accept,
927 .getname = sco_sock_getname,
928 .sendmsg = sco_sock_sendmsg,
929 .recvmsg = bt_sock_recvmsg,
930 .poll = bt_sock_poll,
931 .ioctl = sock_no_ioctl,
932 .mmap = sock_no_mmap,
933 .socketpair = sock_no_socketpair,
934 .shutdown = sock_no_shutdown,
935 .setsockopt = sco_sock_setsockopt,
936 .getsockopt = sco_sock_getsockopt
939 static struct net_proto_family sco_sock_family_ops = {
940 .family = PF_BLUETOOTH,
941 .owner = THIS_MODULE,
942 .create = sco_sock_create,
945 static struct hci_proto sco_hci_proto = {
948 .connect_ind = sco_connect_ind,
949 .connect_cfm = sco_connect_cfm,
950 .disconn_ind = sco_disconn_ind,
951 .recv_scodata = sco_recv_scodata
954 static int __init sco_init(void)
958 err = proto_register(&sco_proto, 0);
962 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
964 BT_ERR("SCO socket registration failed");
968 err = hci_register_proto(&sco_hci_proto);
970 BT_ERR("SCO protocol registration failed");
971 bt_sock_unregister(BTPROTO_SCO);
975 if (class_create_file(bt_class, &class_attr_sco) < 0)
976 BT_ERR("Failed to create SCO info file");
978 BT_INFO("SCO (Voice Link) ver %s", VERSION);
979 BT_INFO("SCO socket layer initialized");
984 proto_unregister(&sco_proto);
988 static void __exit sco_exit(void)
990 class_remove_file(bt_class, &class_attr_sco);
992 if (bt_sock_unregister(BTPROTO_SCO) < 0)
993 BT_ERR("SCO socket unregistration failed");
995 if (hci_unregister_proto(&sco_hci_proto) < 0)
996 BT_ERR("SCO protocol unregistration failed");
998 proto_unregister(&sco_proto);
1001 module_init(sco_init);
1002 module_exit(sco_exit);
1004 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>");
1005 MODULE_DESCRIPTION("Bluetooth SCO ver " VERSION);
1006 MODULE_VERSION(VERSION);
1007 MODULE_LICENSE("GPL");
1008 MODULE_ALIAS("bt-proto-2");