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.
28 * $Id: sco.c,v 1.1.1.1 2005/04/11 02:51:12 jack Exp $
32 #include <linux/config.h>
33 #include <linux/module.h>
35 #include <linux/types.h>
36 #include <linux/errno.h>
37 #include <linux/kernel.h>
38 #include <linux/major.h>
39 #include <linux/sched.h>
40 #include <linux/slab.h>
41 #include <linux/poll.h>
42 #include <linux/fcntl.h>
43 #include <linux/init.h>
44 #include <linux/skbuff.h>
45 #include <linux/interrupt.h>
46 #include <linux/socket.h>
47 #include <linux/skbuff.h>
48 #include <linux/proc_fs.h>
49 #include <linux/list.h>
52 #include <asm/system.h>
53 #include <asm/uaccess.h>
55 #include <net/bluetooth/bluetooth.h>
56 #include <net/bluetooth/hci_core.h>
57 #include <net/bluetooth/sco.h>
61 #define BT_DBG( A... )
64 static struct proto_ops sco_sock_ops;
66 static struct bluez_sock_list sco_sk_list = {
67 lock: RW_LOCK_UNLOCKED
70 static inline int sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent);
71 static void sco_chan_del(struct sock *sk, int err);
72 static inline struct sock * sco_chan_get(struct sco_conn *conn);
74 static int sco_conn_del(struct hci_conn *conn, int err);
76 static void sco_sock_close(struct sock *sk);
77 static void sco_sock_kill(struct sock *sk);
79 /* ----- SCO timers ------ */
80 static void sco_sock_timeout(unsigned long arg)
82 struct sock *sk = (struct sock *) arg;
84 BT_DBG("sock %p state %d", sk, sk->state);
95 static void sco_sock_set_timer(struct sock *sk, long timeout)
97 BT_DBG("sock %p state %d timeout %ld", sk, sk->state, timeout);
99 if (!mod_timer(&sk->timer, jiffies + timeout))
103 static void sco_sock_clear_timer(struct sock *sk)
105 BT_DBG("sock %p state %d", sk, sk->state);
107 if (timer_pending(&sk->timer) && del_timer(&sk->timer))
111 static void sco_sock_init_timer(struct sock *sk)
113 init_timer(&sk->timer);
114 sk->timer.function = sco_sock_timeout;
115 sk->timer.data = (unsigned long)sk;
118 /* -------- SCO connections --------- */
119 static struct sco_conn *sco_conn_add(struct hci_conn *hcon, __u8 status)
121 struct hci_dev *hdev = hcon->hdev;
122 struct sco_conn *conn;
124 if ((conn = hcon->sco_data))
130 if (!(conn = kmalloc(sizeof(struct sco_conn), GFP_ATOMIC)))
132 memset(conn, 0, sizeof(struct sco_conn));
134 spin_lock_init(&conn->lock);
136 hcon->sco_data = conn;
139 conn->src = &hdev->bdaddr;
140 conn->dst = &hcon->dst;
142 if (hdev->sco_mtu > 0)
143 conn->mtu = hdev->sco_mtu;
147 BT_DBG("hcon %p conn %p", hcon, conn);
153 static int sco_conn_del(struct hci_conn *hcon, int err)
155 struct sco_conn *conn;
158 if (!(conn = hcon->sco_data))
161 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
164 if ((sk = sco_chan_get(conn))) {
166 sco_sock_clear_timer(sk);
167 sco_chan_del(sk, err);
172 hcon->sco_data = NULL;
179 int sco_connect(struct sock *sk)
181 bdaddr_t *src = &bluez_pi(sk)->src;
182 bdaddr_t *dst = &bluez_pi(sk)->dst;
183 struct sco_conn *conn;
184 struct hci_conn *hcon;
185 struct hci_dev *hdev;
188 BT_DBG("%s -> %s", batostr(src), batostr(dst));
190 if (!(hdev = hci_get_route(dst, src)))
191 return -EHOSTUNREACH;
193 hci_dev_lock_bh(hdev);
197 hcon = hci_connect(hdev, SCO_LINK, dst);
201 conn = sco_conn_add(hcon, 0);
207 /* Update source addr of the socket */
208 bacpy(src, conn->src);
210 err = sco_chan_add(conn, sk, NULL);
214 if (hcon->state == BT_CONNECTED) {
215 sco_sock_clear_timer(sk);
216 sk->state = BT_CONNECTED;
218 sk->state = BT_CONNECT;
219 sco_sock_set_timer(sk, sk->sndtimeo);
222 hci_dev_unlock_bh(hdev);
227 static inline int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
229 struct sco_conn *conn = sco_pi(sk)->conn;
233 /* Check outgoing MTU */
237 BT_DBG("sk %p len %d", sk, len);
239 count = MIN(conn->mtu, len);
240 if (!(skb = bluez_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err)))
243 if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) {
248 if ((err = hci_send_sco(conn->hcon, skb)) < 0)
258 static inline void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
260 struct sock *sk = sco_chan_get(conn);
265 BT_DBG("sk %p len %d", sk, skb->len);
267 if (sk->state != BT_CONNECTED)
270 if (!sock_queue_rcv_skb(sk, skb))
278 /* -------- Socket interface ---------- */
279 static struct sock *__sco_get_sock_by_addr(bdaddr_t *ba)
283 for (sk = sco_sk_list.head; sk; sk = sk->next) {
284 if (!bacmp(&bluez_pi(sk)->src, ba))
291 /* Find socket listening on source bdaddr.
292 * Returns closest match.
294 static struct sock *sco_get_sock_listen(bdaddr_t *src)
296 struct sock *sk, *sk1 = NULL;
298 read_lock(&sco_sk_list.lock);
300 for (sk = sco_sk_list.head; sk; sk = sk->next) {
301 if (sk->state != BT_LISTEN)
305 if (!bacmp(&bluez_pi(sk)->src, src))
309 if (!bacmp(&bluez_pi(sk)->src, BDADDR_ANY))
313 read_unlock(&sco_sk_list.lock);
315 return sk ? sk : sk1;
318 static void sco_sock_destruct(struct sock *sk)
322 skb_queue_purge(&sk->receive_queue);
323 skb_queue_purge(&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 = bluez_accept_dequeue(parent, NULL)))
338 parent->state = BT_CLOSED;
342 /* Kill socket (only if zapped and orphan)
343 * Must be called on unlocked socket.
345 static void sco_sock_kill(struct sock *sk)
347 if (!sk->zapped || sk->socket)
350 BT_DBG("sk %p state %d", sk, sk->state);
352 /* Kill poor orphan */
353 bluez_sock_unlink(&sco_sk_list, sk);
359 * Must be called on unlocked socket.
361 static void sco_sock_close(struct sock *sk)
363 struct sco_conn *conn;
365 sco_sock_clear_timer(sk);
369 conn = sco_pi(sk)->conn;
371 BT_DBG("sk %p state %d conn %p socket %p", sk, sk->state, conn, sk->socket);
375 sco_sock_cleanup_listen(sk);
382 sco_chan_del(sk, ECONNRESET);
395 static void sco_sock_init(struct sock *sk, struct sock *parent)
400 sk->type = parent->type;
403 static struct sock *sco_sock_alloc(struct socket *sock, int proto, int prio)
407 if (!(sk = sk_alloc(PF_BLUETOOTH, prio, 1)))
410 bluez_sock_init(sock, sk);
414 sk->destruct = sco_sock_destruct;
415 sk->sndtimeo = SCO_CONN_TIMEOUT;
417 sk->protocol = proto;
420 sco_sock_init_timer(sk);
422 bluez_sock_link(&sco_sk_list, sk);
428 static int sco_sock_create(struct socket *sock, int protocol)
432 BT_DBG("sock %p", sock);
434 sock->state = SS_UNCONNECTED;
436 if (sock->type != SOCK_SEQPACKET)
437 return -ESOCKTNOSUPPORT;
439 sock->ops = &sco_sock_ops;
441 if (!(sk = sco_sock_alloc(sock, protocol, GFP_KERNEL)))
444 sco_sock_init(sk, NULL);
448 static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
450 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
451 struct sock *sk = sock->sk;
452 bdaddr_t *src = &sa->sco_bdaddr;
455 BT_DBG("sk %p %s", sk, batostr(&sa->sco_bdaddr));
457 if (!addr || addr->sa_family != AF_BLUETOOTH)
462 if (sk->state != BT_OPEN) {
467 write_lock_bh(&sco_sk_list.lock);
469 if (bacmp(src, BDADDR_ANY) && __sco_get_sock_by_addr(src)) {
472 /* Save source address */
473 bacpy(&bluez_pi(sk)->src, &sa->sco_bdaddr);
474 sk->state = BT_BOUND;
477 write_unlock_bh(&sco_sk_list.lock);
485 static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
487 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
488 struct sock *sk = sock->sk;
494 if (addr->sa_family != AF_BLUETOOTH || alen < sizeof(struct sockaddr_sco))
497 if (sk->state != BT_OPEN && sk->state != BT_BOUND)
500 if (sk->type != SOCK_SEQPACKET)
505 /* Set destination address and psm */
506 bacpy(&bluez_pi(sk)->dst, &sa->sco_bdaddr);
508 if ((err = sco_connect(sk)))
511 err = bluez_sock_w4_connect(sk, flags);
518 int sco_sock_listen(struct socket *sock, int backlog)
520 struct sock *sk = sock->sk;
523 BT_DBG("sk %p backlog %d", sk, backlog);
527 if (sk->state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
532 sk->max_ack_backlog = backlog;
534 sk->state = BT_LISTEN;
541 int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
543 DECLARE_WAITQUEUE(wait, current);
544 struct sock *sk = sock->sk, *ch;
550 if (sk->state != BT_LISTEN) {
555 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
557 BT_DBG("sk %p timeo %ld", sk, timeo);
559 /* Wait for an incoming connection. (wake-one). */
560 add_wait_queue_exclusive(sk->sleep, &wait);
561 while (!(ch = bluez_accept_dequeue(sk, newsock))) {
562 set_current_state(TASK_INTERRUPTIBLE);
569 timeo = schedule_timeout(timeo);
572 if (sk->state != BT_LISTEN) {
577 if (signal_pending(current)) {
578 err = sock_intr_errno(timeo);
582 set_current_state(TASK_RUNNING);
583 remove_wait_queue(sk->sleep, &wait);
588 newsock->state = SS_CONNECTED;
590 BT_DBG("new socket %p", ch);
597 static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
599 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
600 struct sock *sk = sock->sk;
602 BT_DBG("sock %p, sk %p", sock, sk);
604 addr->sa_family = AF_BLUETOOTH;
605 *len = sizeof(struct sockaddr_sco);
608 bacpy(&sa->sco_bdaddr, &bluez_pi(sk)->dst);
610 bacpy(&sa->sco_bdaddr, &bluez_pi(sk)->src);
615 static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg, int len, struct scm_cookie *scm)
617 struct sock *sk = sock->sk;
620 BT_DBG("sock %p, sk %p", sock, sk);
623 return sock_error(sk);
625 if (msg->msg_flags & MSG_OOB)
630 if (sk->state == BT_CONNECTED)
631 err = sco_send_frame(sk, msg, len);
639 int sco_sock_setsockopt(struct socket *sock, int level, int optname, char *optval, int optlen)
641 struct sock *sk = sock->sk;
658 int sco_sock_getsockopt(struct socket *sock, int level, int optname, char *optval, int *optlen)
660 struct sock *sk = sock->sk;
661 struct sco_options opts;
662 struct sco_conninfo cinfo;
667 if (get_user(len, optlen))
674 if (sk->state != BT_CONNECTED) {
679 opts.mtu = sco_pi(sk)->conn->mtu;
681 BT_DBG("mtu %d", opts.mtu);
683 len = MIN(len, sizeof(opts));
684 if (copy_to_user(optval, (char *)&opts, len))
690 if (sk->state != BT_CONNECTED) {
695 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
697 len = MIN(len, sizeof(cinfo));
698 if (copy_to_user(optval, (char *)&cinfo, len))
712 static int sco_sock_release(struct socket *sock)
714 struct sock *sk = sock->sk;
716 BT_DBG("sock %p, sk %p", sock, sk);
727 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
729 BT_DBG("conn %p", conn);
731 sco_pi(sk)->conn = conn;
735 bluez_accept_enqueue(parent, sk);
738 static inline int sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
746 __sco_chan_add(conn, sk, parent);
748 sco_conn_unlock(conn);
752 static inline struct sock * sco_chan_get(struct sco_conn *conn)
754 struct sock *sk = NULL;
757 sco_conn_unlock(conn);
762 * Must be called on the locked socket. */
763 static void sco_chan_del(struct sock *sk, int err)
765 struct sco_conn *conn;
767 conn = sco_pi(sk)->conn;
769 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
774 sco_pi(sk)->conn = NULL;
775 sco_conn_unlock(conn);
776 hci_conn_put(conn->hcon);
779 sk->state = BT_CLOSED;
781 sk->state_change(sk);
786 static void sco_conn_ready(struct sco_conn *conn)
788 struct sock *parent, *sk;
790 BT_DBG("conn %p", conn);
794 if ((sk = conn->sk)) {
795 sco_sock_clear_timer(sk);
797 sk->state = BT_CONNECTED;
798 sk->state_change(sk);
801 parent = sco_get_sock_listen(conn->src);
805 bh_lock_sock(parent);
807 sk = sco_sock_alloc(NULL, BTPROTO_SCO, GFP_ATOMIC);
809 bh_unlock_sock(parent);
813 sco_sock_init(sk, parent);
815 bacpy(&bluez_pi(sk)->src, conn->src);
816 bacpy(&bluez_pi(sk)->dst, conn->dst);
818 hci_conn_hold(conn->hcon);
819 __sco_chan_add(conn, sk, parent);
821 sk->state = BT_CONNECTED;
824 parent->data_ready(parent, 1);
826 bh_unlock_sock(parent);
830 sco_conn_unlock(conn);
833 /* ----- SCO interface with lower layer (HCI) ----- */
834 int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
836 BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
838 /* Always accept connection */
839 return HCI_LM_ACCEPT;
842 int sco_connect_cfm(struct hci_conn *hcon, __u8 status)
844 BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
846 if (hcon->type != SCO_LINK)
850 struct sco_conn *conn;
852 conn = sco_conn_add(hcon, status);
854 sco_conn_ready(conn);
856 sco_conn_del(hcon, bterr(status));
861 int sco_disconn_ind(struct hci_conn *hcon, __u8 reason)
863 BT_DBG("hcon %p reason %d", hcon, reason);
865 if (hcon->type != SCO_LINK)
868 sco_conn_del(hcon, bterr(reason));
872 int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
874 struct sco_conn *conn = hcon->sco_data;
879 BT_DBG("conn %p len %d", conn, skb->len);
882 sco_recv_frame(conn, skb);
891 /* ----- Proc fs support ------ */
892 static int sco_sock_dump(char *buf, struct bluez_sock_list *list)
894 struct sco_pinfo *pi;
898 write_lock_bh(&list->lock);
900 for (sk = list->head; sk; sk = sk->next) {
902 ptr += sprintf(ptr, "%s %s %d\n",
903 batostr(&bluez_pi(sk)->src), batostr(&bluez_pi(sk)->dst),
907 write_unlock_bh(&list->lock);
909 ptr += sprintf(ptr, "\n");
914 static int sco_read_proc(char *buf, char **start, off_t offset, int count, int *eof, void *priv)
919 BT_DBG("count %d, offset %ld", count, offset);
921 ptr += sco_sock_dump(ptr, &sco_sk_list);
924 if (len <= count + offset)
927 *start = buf + offset;
938 static struct proto_ops sco_sock_ops = {
939 family: PF_BLUETOOTH,
940 release: sco_sock_release,
942 connect: sco_sock_connect,
943 listen: sco_sock_listen,
944 accept: sco_sock_accept,
945 getname: sco_sock_getname,
946 sendmsg: sco_sock_sendmsg,
947 recvmsg: bluez_sock_recvmsg,
948 poll: bluez_sock_poll,
949 socketpair: sock_no_socketpair,
950 ioctl: sock_no_ioctl,
951 shutdown: sock_no_shutdown,
952 setsockopt: sco_sock_setsockopt,
953 getsockopt: sco_sock_getsockopt,
957 static struct net_proto_family sco_sock_family_ops = {
958 family: PF_BLUETOOTH,
959 create: sco_sock_create
962 static struct hci_proto sco_hci_proto = {
965 connect_ind: sco_connect_ind,
966 connect_cfm: sco_connect_cfm,
967 disconn_ind: sco_disconn_ind,
968 recv_scodata: sco_recv_scodata,
971 int __init sco_init(void)
975 if ((err = bluez_sock_register(BTPROTO_SCO, &sco_sock_family_ops))) {
976 BT_ERR("Can't register SCO socket layer");
980 if ((err = hci_register_proto(&sco_hci_proto))) {
981 BT_ERR("Can't register SCO protocol");
985 create_proc_read_entry("bluetooth/sco", 0, 0, sco_read_proc, NULL);
987 BT_INFO("BlueZ SCO ver %s Copyright (C) 2000,2001 Qualcomm Inc", VERSION);
988 BT_INFO("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
992 void sco_cleanup(void)
996 remove_proc_entry("bluetooth/sco", NULL);
998 /* Unregister socket, protocol and notifier */
999 if ((err = bluez_sock_unregister(BTPROTO_SCO)))
1000 BT_ERR("Can't unregister SCO socket layer %d", err);
1002 if ((err = hci_unregister_proto(&sco_hci_proto)))
1003 BT_ERR("Can't unregister SCO protocol %d", err);
1006 module_init(sco_init);
1007 module_exit(sco_cleanup);
1009 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
1010 MODULE_DESCRIPTION("BlueZ SCO ver " VERSION);
1011 MODULE_LICENSE("GPL");