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/config.h>
28 #include <linux/module.h>
30 #include <linux/types.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/major.h>
34 #include <linux/sched.h>
35 #include <linux/slab.h>
36 #include <linux/poll.h>
37 #include <linux/fcntl.h>
38 #include <linux/init.h>
39 #include <linux/interrupt.h>
40 #include <linux/socket.h>
41 #include <linux/skbuff.h>
42 #include <linux/proc_fs.h>
43 #include <linux/seq_file.h>
44 #include <linux/list.h>
47 #include <asm/system.h>
48 #include <asm/uaccess.h>
50 #include <net/bluetooth/bluetooth.h>
51 #include <net/bluetooth/hci_core.h>
52 #include <net/bluetooth/sco.h>
54 #ifndef CONFIG_BT_SCO_DEBUG
61 static struct proto_ops sco_sock_ops;
63 static struct bt_sock_list sco_sk_list = {
64 .lock = RW_LOCK_UNLOCKED
67 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent);
68 static void sco_chan_del(struct sock *sk, int err);
70 static int sco_conn_del(struct hci_conn *conn, int err);
72 static void sco_sock_close(struct sock *sk);
73 static void sco_sock_kill(struct sock *sk);
75 /* ---- SCO timers ---- */
76 static void sco_sock_timeout(unsigned long arg)
78 struct sock *sk = (struct sock *) arg;
80 BT_DBG("sock %p state %d", sk, sk->sk_state);
83 sk->sk_err = ETIMEDOUT;
84 sk->sk_state_change(sk);
91 static void sco_sock_set_timer(struct sock *sk, long timeout)
93 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
94 sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
97 static void sco_sock_clear_timer(struct sock *sk)
99 BT_DBG("sock %p state %d", sk, sk->sk_state);
100 sk_stop_timer(sk, &sk->sk_timer);
103 static void sco_sock_init_timer(struct sock *sk)
105 init_timer(&sk->sk_timer);
106 sk->sk_timer.function = sco_sock_timeout;
107 sk->sk_timer.data = (unsigned long)sk;
110 /* ---- SCO connections ---- */
111 static struct sco_conn *sco_conn_add(struct hci_conn *hcon, __u8 status)
113 struct hci_dev *hdev = hcon->hdev;
114 struct sco_conn *conn;
116 if ((conn = hcon->sco_data))
122 if (!(conn = kmalloc(sizeof(struct sco_conn), GFP_ATOMIC)))
124 memset(conn, 0, sizeof(struct sco_conn));
126 spin_lock_init(&conn->lock);
128 hcon->sco_data = conn;
131 conn->src = &hdev->bdaddr;
132 conn->dst = &hcon->dst;
134 if (hdev->sco_mtu > 0)
135 conn->mtu = hdev->sco_mtu;
139 BT_DBG("hcon %p conn %p", hcon, conn);
143 static inline struct sock *sco_chan_get(struct sco_conn *conn)
145 struct sock *sk = NULL;
148 sco_conn_unlock(conn);
152 static int sco_conn_del(struct hci_conn *hcon, int err)
154 struct sco_conn *conn;
157 if (!(conn = hcon->sco_data))
160 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
163 if ((sk = sco_chan_get(conn))) {
165 sco_sock_clear_timer(sk);
166 sco_chan_del(sk, err);
171 hcon->sco_data = NULL;
176 static inline int sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
184 __sco_chan_add(conn, sk, parent);
186 sco_conn_unlock(conn);
190 static int sco_connect(struct sock *sk)
192 bdaddr_t *src = &bt_sk(sk)->src;
193 bdaddr_t *dst = &bt_sk(sk)->dst;
194 struct sco_conn *conn;
195 struct hci_conn *hcon;
196 struct hci_dev *hdev;
199 BT_DBG("%s -> %s", batostr(src), batostr(dst));
201 if (!(hdev = hci_get_route(dst, src)))
202 return -EHOSTUNREACH;
204 hci_dev_lock_bh(hdev);
208 hcon = hci_connect(hdev, SCO_LINK, dst);
212 conn = sco_conn_add(hcon, 0);
218 /* Update source addr of the socket */
219 bacpy(src, conn->src);
221 err = sco_chan_add(conn, sk, NULL);
225 if (hcon->state == BT_CONNECTED) {
226 sco_sock_clear_timer(sk);
227 sk->sk_state = BT_CONNECTED;
229 sk->sk_state = BT_CONNECT;
230 sco_sock_set_timer(sk, sk->sk_sndtimeo);
233 hci_dev_unlock_bh(hdev);
238 static inline int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
240 struct sco_conn *conn = sco_pi(sk)->conn;
244 /* Check outgoing MTU */
248 BT_DBG("sk %p len %d", sk, len);
250 count = min_t(unsigned int, conn->mtu, len);
251 if (!(skb = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err)))
254 if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) {
259 if ((err = hci_send_sco(conn->hcon, skb)) < 0)
269 static inline void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
271 struct sock *sk = sco_chan_get(conn);
276 BT_DBG("sk %p len %d", sk, skb->len);
278 if (sk->sk_state != BT_CONNECTED)
281 if (!sock_queue_rcv_skb(sk, skb))
289 /* -------- Socket interface ---------- */
290 static struct sock *__sco_get_sock_by_addr(bdaddr_t *ba)
293 struct hlist_node *node;
295 sk_for_each(sk, node, &sco_sk_list.head)
296 if (!bacmp(&bt_sk(sk)->src, ba))
303 /* Find socket listening on source bdaddr.
304 * Returns closest match.
306 static struct sock *sco_get_sock_listen(bdaddr_t *src)
308 struct sock *sk = NULL, *sk1 = NULL;
309 struct hlist_node *node;
311 read_lock(&sco_sk_list.lock);
313 sk_for_each(sk, node, &sco_sk_list.head) {
314 if (sk->sk_state != BT_LISTEN)
318 if (!bacmp(&bt_sk(sk)->src, src))
322 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
326 read_unlock(&sco_sk_list.lock);
328 return node ? sk : sk1;
331 static void sco_sock_destruct(struct sock *sk)
335 skb_queue_purge(&sk->sk_receive_queue);
336 skb_queue_purge(&sk->sk_write_queue);
339 static void sco_sock_cleanup_listen(struct sock *parent)
343 BT_DBG("parent %p", parent);
345 /* Close not yet accepted channels */
346 while ((sk = bt_accept_dequeue(parent, NULL))) {
351 parent->sk_state = BT_CLOSED;
352 sock_set_flag(parent, SOCK_ZAPPED);
355 /* Kill socket (only if zapped and orphan)
356 * Must be called on unlocked socket.
358 static void sco_sock_kill(struct sock *sk)
360 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
363 BT_DBG("sk %p state %d", sk, sk->sk_state);
365 /* Kill poor orphan */
366 bt_sock_unlink(&sco_sk_list, sk);
367 sock_set_flag(sk, SOCK_DEAD);
372 * Must be called on unlocked socket.
374 static void sco_sock_close(struct sock *sk)
376 struct sco_conn *conn;
378 sco_sock_clear_timer(sk);
382 conn = sco_pi(sk)->conn;
384 BT_DBG("sk %p state %d conn %p socket %p", sk, sk->sk_state, conn, sk->sk_socket);
386 switch (sk->sk_state) {
388 sco_sock_cleanup_listen(sk);
395 sco_chan_del(sk, ECONNRESET);
399 sock_set_flag(sk, SOCK_ZAPPED);
408 static void sco_sock_init(struct sock *sk, struct sock *parent)
413 sk->sk_type = parent->sk_type;
416 static struct proto sco_proto = {
418 .owner = THIS_MODULE,
419 .obj_size = sizeof(struct sco_pinfo)
422 static struct sock *sco_sock_alloc(struct socket *sock, int proto, int prio)
426 sk = sk_alloc(PF_BLUETOOTH, prio, &sco_proto, 1);
430 sock_init_data(sock, sk);
431 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
433 sk->sk_destruct = sco_sock_destruct;
434 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
436 sock_reset_flag(sk, SOCK_ZAPPED);
438 sk->sk_protocol = proto;
439 sk->sk_state = BT_OPEN;
441 sco_sock_init_timer(sk);
443 bt_sock_link(&sco_sk_list, sk);
447 static int sco_sock_create(struct socket *sock, int protocol)
451 BT_DBG("sock %p", sock);
453 sock->state = SS_UNCONNECTED;
455 if (sock->type != SOCK_SEQPACKET)
456 return -ESOCKTNOSUPPORT;
458 sock->ops = &sco_sock_ops;
460 if (!(sk = sco_sock_alloc(sock, protocol, GFP_KERNEL)))
463 sco_sock_init(sk, NULL);
467 static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
469 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
470 struct sock *sk = sock->sk;
471 bdaddr_t *src = &sa->sco_bdaddr;
474 BT_DBG("sk %p %s", sk, batostr(&sa->sco_bdaddr));
476 if (!addr || addr->sa_family != AF_BLUETOOTH)
481 if (sk->sk_state != BT_OPEN) {
486 write_lock_bh(&sco_sk_list.lock);
488 if (bacmp(src, BDADDR_ANY) && __sco_get_sock_by_addr(src)) {
491 /* Save source address */
492 bacpy(&bt_sk(sk)->src, &sa->sco_bdaddr);
493 sk->sk_state = BT_BOUND;
496 write_unlock_bh(&sco_sk_list.lock);
503 static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
505 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
506 struct sock *sk = sock->sk;
512 if (addr->sa_family != AF_BLUETOOTH || alen < sizeof(struct sockaddr_sco))
515 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
518 if (sk->sk_type != SOCK_SEQPACKET)
523 /* Set destination address and psm */
524 bacpy(&bt_sk(sk)->dst, &sa->sco_bdaddr);
526 if ((err = sco_connect(sk)))
529 err = bt_sock_wait_state(sk, BT_CONNECTED,
530 sock_sndtimeo(sk, flags & O_NONBLOCK));
537 static int sco_sock_listen(struct socket *sock, int backlog)
539 struct sock *sk = sock->sk;
542 BT_DBG("sk %p backlog %d", sk, backlog);
546 if (sk->sk_state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
551 sk->sk_max_ack_backlog = backlog;
552 sk->sk_ack_backlog = 0;
553 sk->sk_state = BT_LISTEN;
560 static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
562 DECLARE_WAITQUEUE(wait, current);
563 struct sock *sk = sock->sk, *ch;
569 if (sk->sk_state != BT_LISTEN) {
574 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
576 BT_DBG("sk %p timeo %ld", sk, timeo);
578 /* Wait for an incoming connection. (wake-one). */
579 add_wait_queue_exclusive(sk->sk_sleep, &wait);
580 while (!(ch = bt_accept_dequeue(sk, newsock))) {
581 set_current_state(TASK_INTERRUPTIBLE);
588 timeo = schedule_timeout(timeo);
591 if (sk->sk_state != BT_LISTEN) {
596 if (signal_pending(current)) {
597 err = sock_intr_errno(timeo);
601 set_current_state(TASK_RUNNING);
602 remove_wait_queue(sk->sk_sleep, &wait);
607 newsock->state = SS_CONNECTED;
609 BT_DBG("new socket %p", ch);
616 static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
618 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
619 struct sock *sk = sock->sk;
621 BT_DBG("sock %p, sk %p", sock, sk);
623 addr->sa_family = AF_BLUETOOTH;
624 *len = sizeof(struct sockaddr_sco);
627 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst);
629 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src);
634 static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
635 struct msghdr *msg, size_t len)
637 struct sock *sk = sock->sk;
640 BT_DBG("sock %p, sk %p", sock, sk);
643 return 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(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)
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)
874 sco_conn_del(hcon, bt_err(reason));
878 static int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
880 struct sco_conn *conn = hcon->sco_data;
885 BT_DBG("conn %p len %d", conn, skb->len);
888 sco_recv_frame(conn, skb);
897 /* ---- Proc fs support ---- */
898 #ifdef CONFIG_PROC_FS
899 static void *sco_seq_start(struct seq_file *seq, loff_t *pos)
902 struct hlist_node *node;
905 read_lock_bh(&sco_sk_list.lock);
907 sk_for_each(sk, node, &sco_sk_list.head)
915 static void *sco_seq_next(struct seq_file *seq, void *e, loff_t *pos)
922 static void sco_seq_stop(struct seq_file *seq, void *e)
924 read_unlock_bh(&sco_sk_list.lock);
927 static int sco_seq_show(struct seq_file *seq, void *e)
930 seq_printf(seq, "%s %s %d\n",
931 batostr(&bt_sk(sk)->src), batostr(&bt_sk(sk)->dst), sk->sk_state);
935 static struct seq_operations sco_seq_ops = {
936 .start = sco_seq_start,
937 .next = sco_seq_next,
938 .stop = sco_seq_stop,
942 static int sco_seq_open(struct inode *inode, struct file *file)
944 return seq_open(file, &sco_seq_ops);
947 static struct file_operations sco_seq_fops = {
948 .owner = THIS_MODULE,
949 .open = sco_seq_open,
952 .release = seq_release,
955 static int __init sco_proc_init(void)
957 struct proc_dir_entry *p = create_proc_entry("sco", S_IRUGO, proc_bt);
960 p->owner = THIS_MODULE;
961 p->proc_fops = &sco_seq_fops;
965 static void __exit sco_proc_cleanup(void)
967 remove_proc_entry("sco", proc_bt);
970 #else /* CONFIG_PROC_FS */
972 static int __init sco_proc_init(void)
977 static void __exit sco_proc_cleanup(void)
981 #endif /* CONFIG_PROC_FS */
983 static struct proto_ops sco_sock_ops = {
984 .family = PF_BLUETOOTH,
985 .owner = THIS_MODULE,
986 .release = sco_sock_release,
987 .bind = sco_sock_bind,
988 .connect = sco_sock_connect,
989 .listen = sco_sock_listen,
990 .accept = sco_sock_accept,
991 .getname = sco_sock_getname,
992 .sendmsg = sco_sock_sendmsg,
993 .recvmsg = bt_sock_recvmsg,
994 .poll = bt_sock_poll,
995 .ioctl = sock_no_ioctl,
996 .mmap = sock_no_mmap,
997 .socketpair = sock_no_socketpair,
998 .shutdown = sock_no_shutdown,
999 .setsockopt = sco_sock_setsockopt,
1000 .getsockopt = sco_sock_getsockopt
1003 static struct net_proto_family sco_sock_family_ops = {
1004 .family = PF_BLUETOOTH,
1005 .owner = THIS_MODULE,
1006 .create = sco_sock_create,
1009 static struct hci_proto sco_hci_proto = {
1011 .id = HCI_PROTO_SCO,
1012 .connect_ind = sco_connect_ind,
1013 .connect_cfm = sco_connect_cfm,
1014 .disconn_ind = sco_disconn_ind,
1015 .recv_scodata = sco_recv_scodata
1018 static int __init sco_init(void)
1022 err = proto_register(&sco_proto, 0);
1026 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1028 BT_ERR("SCO socket registration failed");
1032 err = hci_register_proto(&sco_hci_proto);
1034 BT_ERR("SCO protocol registration failed");
1035 bt_sock_unregister(BTPROTO_SCO);
1041 BT_INFO("SCO (Voice Link) ver %s", VERSION);
1042 BT_INFO("SCO socket layer initialized");
1047 proto_unregister(&sco_proto);
1051 static void __exit sco_exit(void)
1055 if (bt_sock_unregister(BTPROTO_SCO) < 0)
1056 BT_ERR("SCO socket unregistration failed");
1058 if (hci_unregister_proto(&sco_hci_proto) < 0)
1059 BT_ERR("SCO protocol unregistration failed");
1061 proto_unregister(&sco_proto);
1064 module_init(sco_init);
1065 module_exit(sco_exit);
1067 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>");
1068 MODULE_DESCRIPTION("Bluetooth SCO ver " VERSION);
1069 MODULE_VERSION(VERSION);
1070 MODULE_LICENSE("GPL");
1071 MODULE_ALIAS("bt-proto-2");