MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / net / bluetooth / sco.c
blob55a26670578863fb4298b4021a60f9691e85f7d7
1 /*
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>
45 #include <net/sock.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
55 #undef BT_DBG
56 #define BT_DBG(D...)
57 #endif
59 #define VERSION "0.3"
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);
82 bh_lock_sock(sk);
83 sk->sk_err = ETIMEDOUT;
84 sk->sk_state_change(sk);
85 bh_unlock_sock(sk);
87 sco_sock_kill(sk);
88 sock_put(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))
117 return conn;
119 if (status)
120 return conn;
122 if (!(conn = kmalloc(sizeof(struct sco_conn), GFP_ATOMIC)))
123 return NULL;
124 memset(conn, 0, sizeof(struct sco_conn));
126 spin_lock_init(&conn->lock);
128 hcon->sco_data = conn;
129 conn->hcon = hcon;
131 conn->src = &hdev->bdaddr;
132 conn->dst = &hcon->dst;
134 if (hdev->sco_mtu > 0)
135 conn->mtu = hdev->sco_mtu;
136 else
137 conn->mtu = 60;
139 BT_DBG("hcon %p conn %p", hcon, conn);
140 return conn;
143 static inline struct sock *sco_chan_get(struct sco_conn *conn)
145 struct sock *sk = NULL;
146 sco_conn_lock(conn);
147 sk = conn->sk;
148 sco_conn_unlock(conn);
149 return sk;
152 static int sco_conn_del(struct hci_conn *hcon, int err)
154 struct sco_conn *conn;
155 struct sock *sk;
157 if (!(conn = hcon->sco_data))
158 return 0;
160 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
162 /* Kill socket */
163 if ((sk = sco_chan_get(conn))) {
164 bh_lock_sock(sk);
165 sco_sock_clear_timer(sk);
166 sco_chan_del(sk, err);
167 bh_unlock_sock(sk);
168 sco_sock_kill(sk);
171 hcon->sco_data = NULL;
172 kfree(conn);
173 return 0;
176 static inline int sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
178 int err = 0;
180 sco_conn_lock(conn);
181 if (conn->sk) {
182 err = -EBUSY;
183 } else {
184 __sco_chan_add(conn, sk, parent);
186 sco_conn_unlock(conn);
187 return err;
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;
197 int err = 0;
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);
206 err = -ENOMEM;
208 hcon = hci_connect(hdev, SCO_LINK, dst);
209 if (!hcon)
210 goto done;
212 conn = sco_conn_add(hcon, 0);
213 if (!conn) {
214 hci_conn_put(hcon);
215 goto done;
218 /* Update source addr of the socket */
219 bacpy(src, conn->src);
221 err = sco_chan_add(conn, sk, NULL);
222 if (err)
223 goto done;
225 if (hcon->state == BT_CONNECTED) {
226 sco_sock_clear_timer(sk);
227 sk->sk_state = BT_CONNECTED;
228 } else {
229 sk->sk_state = BT_CONNECT;
230 sco_sock_set_timer(sk, sk->sk_sndtimeo);
232 done:
233 hci_dev_unlock_bh(hdev);
234 hci_dev_put(hdev);
235 return err;
238 static inline int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
240 struct sco_conn *conn = sco_pi(sk)->conn;
241 struct sk_buff *skb;
242 int err, count;
244 /* Check outgoing MTU */
245 if (len > conn->mtu)
246 return -EINVAL;
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)))
252 return err;
254 if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) {
255 err = -EFAULT;
256 goto fail;
259 if ((err = hci_send_sco(conn->hcon, skb)) < 0)
260 goto fail;
262 return count;
264 fail:
265 kfree_skb(skb);
266 return err;
269 static inline void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
271 struct sock *sk = sco_chan_get(conn);
273 if (!sk)
274 goto drop;
276 BT_DBG("sk %p len %d", sk, skb->len);
278 if (sk->sk_state != BT_CONNECTED)
279 goto drop;
281 if (!sock_queue_rcv_skb(sk, skb))
282 return;
284 drop:
285 kfree_skb(skb);
286 return;
289 /* -------- Socket interface ---------- */
290 static struct sock *__sco_get_sock_by_addr(bdaddr_t *ba)
292 struct sock *sk;
293 struct hlist_node *node;
295 sk_for_each(sk, node, &sco_sk_list.head)
296 if (!bacmp(&bt_sk(sk)->src, ba))
297 goto found;
298 sk = NULL;
299 found:
300 return sk;
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)
315 continue;
317 /* Exact match. */
318 if (!bacmp(&bt_sk(sk)->src, src))
319 break;
321 /* Closest match */
322 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
323 sk1 = sk;
326 read_unlock(&sco_sk_list.lock);
328 return node ? sk : sk1;
331 static void sco_sock_destruct(struct sock *sk)
333 BT_DBG("sk %p", sk);
335 skb_queue_purge(&sk->sk_receive_queue);
336 skb_queue_purge(&sk->sk_write_queue);
338 if (sk->sk_protinfo)
339 kfree(sk->sk_protinfo);
342 static void sco_sock_cleanup_listen(struct sock *parent)
344 struct sock *sk;
346 BT_DBG("parent %p", parent);
348 /* Close not yet accepted channels */
349 while ((sk = bt_accept_dequeue(parent, NULL))) {
350 sco_sock_close(sk);
351 sco_sock_kill(sk);
354 parent->sk_state = BT_CLOSED;
355 parent->sk_zapped = 1;
358 /* Kill socket (only if zapped and orphan)
359 * Must be called on unlocked socket.
361 static void sco_sock_kill(struct sock *sk)
363 if (!sk->sk_zapped || sk->sk_socket)
364 return;
366 BT_DBG("sk %p state %d", sk, sk->sk_state);
368 /* Kill poor orphan */
369 bt_sock_unlink(&sco_sk_list, sk);
370 sock_set_flag(sk, SOCK_DEAD);
371 sock_put(sk);
374 /* Close socket.
375 * Must be called on unlocked socket.
377 static void sco_sock_close(struct sock *sk)
379 struct sco_conn *conn;
381 sco_sock_clear_timer(sk);
383 lock_sock(sk);
385 conn = sco_pi(sk)->conn;
387 BT_DBG("sk %p state %d conn %p socket %p", sk, sk->sk_state, conn, sk->sk_socket);
389 switch (sk->sk_state) {
390 case BT_LISTEN:
391 sco_sock_cleanup_listen(sk);
392 break;
394 case BT_CONNECTED:
395 case BT_CONFIG:
396 case BT_CONNECT:
397 case BT_DISCONN:
398 sco_chan_del(sk, ECONNRESET);
399 break;
401 default:
402 sk->sk_zapped = 1;
403 break;
406 release_sock(sk);
408 sco_sock_kill(sk);
411 static void sco_sock_init(struct sock *sk, struct sock *parent)
413 BT_DBG("sk %p", sk);
415 if (parent)
416 sk->sk_type = parent->sk_type;
419 static struct sock *sco_sock_alloc(struct socket *sock, int proto, int prio)
421 struct sock *sk;
423 sk = bt_sock_alloc(sock, proto, sizeof(struct sco_pinfo), prio);
424 if (!sk)
425 return NULL;
427 sk_set_owner(sk, THIS_MODULE);
429 sk->sk_destruct = sco_sock_destruct;
430 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
431 sk->sk_state = BT_OPEN;
433 sco_sock_init_timer(sk);
435 bt_sock_link(&sco_sk_list, sk);
436 return sk;
439 static int sco_sock_create(struct socket *sock, int protocol)
441 struct sock *sk;
443 BT_DBG("sock %p", sock);
445 sock->state = SS_UNCONNECTED;
447 if (sock->type != SOCK_SEQPACKET)
448 return -ESOCKTNOSUPPORT;
450 sock->ops = &sco_sock_ops;
452 if (!(sk = sco_sock_alloc(sock, protocol, GFP_KERNEL)))
453 return -ENOMEM;
455 sco_sock_init(sk, NULL);
456 return 0;
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;
464 int err = 0;
466 BT_DBG("sk %p %s", sk, batostr(&sa->sco_bdaddr));
468 if (!addr || addr->sa_family != AF_BLUETOOTH)
469 return -EINVAL;
471 lock_sock(sk);
473 if (sk->sk_state != BT_OPEN) {
474 err = -EBADFD;
475 goto done;
478 write_lock_bh(&sco_sk_list.lock);
480 if (bacmp(src, BDADDR_ANY) && __sco_get_sock_by_addr(src)) {
481 err = -EADDRINUSE;
482 } else {
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);
490 done:
491 release_sock(sk);
492 return err;
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;
499 int err = 0;
502 BT_DBG("sk %p", sk);
504 if (addr->sa_family != AF_BLUETOOTH || alen < sizeof(struct sockaddr_sco))
505 return -EINVAL;
507 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
508 return -EBADFD;
510 if (sk->sk_type != SOCK_SEQPACKET)
511 return -EINVAL;
513 lock_sock(sk);
515 /* Set destination address and psm */
516 bacpy(&bt_sk(sk)->dst, &sa->sco_bdaddr);
518 if ((err = sco_connect(sk)))
519 goto done;
521 err = bt_sock_wait_state(sk, BT_CONNECTED,
522 sock_sndtimeo(sk, flags & O_NONBLOCK));
524 done:
525 release_sock(sk);
526 return err;
529 static int sco_sock_listen(struct socket *sock, int backlog)
531 struct sock *sk = sock->sk;
532 int err = 0;
534 BT_DBG("sk %p backlog %d", sk, backlog);
536 lock_sock(sk);
538 if (sk->sk_state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
539 err = -EBADFD;
540 goto done;
543 sk->sk_max_ack_backlog = backlog;
544 sk->sk_ack_backlog = 0;
545 sk->sk_state = BT_LISTEN;
547 done:
548 release_sock(sk);
549 return err;
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;
556 long timeo;
557 int err = 0;
559 lock_sock(sk);
561 if (sk->sk_state != BT_LISTEN) {
562 err = -EBADFD;
563 goto done;
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);
574 if (!timeo) {
575 err = -EAGAIN;
576 break;
579 release_sock(sk);
580 timeo = schedule_timeout(timeo);
581 lock_sock(sk);
583 if (sk->sk_state != BT_LISTEN) {
584 err = -EBADFD;
585 break;
588 if (signal_pending(current)) {
589 err = sock_intr_errno(timeo);
590 break;
593 set_current_state(TASK_RUNNING);
594 remove_wait_queue(sk->sk_sleep, &wait);
596 if (err)
597 goto done;
599 newsock->state = SS_CONNECTED;
601 BT_DBG("new socket %p", ch);
603 done:
604 release_sock(sk);
605 return err;
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);
618 if (peer)
619 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst);
620 else
621 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src);
623 return 0;
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;
630 int err = 0;
632 BT_DBG("sock %p, sk %p", sock, sk);
634 if (sk->sk_err)
635 return sock_error(sk);
637 if (msg->msg_flags & MSG_OOB)
638 return -EOPNOTSUPP;
640 lock_sock(sk);
642 if (sk->sk_state == BT_CONNECTED)
643 err = sco_send_frame(sk, msg, len);
644 else
645 err = -ENOTCONN;
647 release_sock(sk);
648 return err;
651 static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen)
653 struct sock *sk = sock->sk;
654 int err = 0;
656 BT_DBG("sk %p", sk);
658 lock_sock(sk);
660 switch (optname) {
661 default:
662 err = -ENOPROTOOPT;
663 break;
666 release_sock(sk);
667 return err;
670 static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
672 struct sock *sk = sock->sk;
673 struct sco_options opts;
674 struct sco_conninfo cinfo;
675 int len, err = 0;
677 BT_DBG("sk %p", sk);
679 if (get_user(len, optlen))
680 return -EFAULT;
682 lock_sock(sk);
684 switch (optname) {
685 case SCO_OPTIONS:
686 if (sk->sk_state != BT_CONNECTED) {
687 err = -ENOTCONN;
688 break;
691 opts.mtu = sco_pi(sk)->conn->mtu;
693 BT_DBG("mtu %d", opts.mtu);
695 len = min_t(unsigned int, len, sizeof(opts));
696 if (copy_to_user(optval, (char *)&opts, len))
697 err = -EFAULT;
699 break;
701 case SCO_CONNINFO:
702 if (sk->sk_state != BT_CONNECTED) {
703 err = -ENOTCONN;
704 break;
707 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
709 len = min_t(unsigned int, len, sizeof(cinfo));
710 if (copy_to_user(optval, (char *)&cinfo, len))
711 err = -EFAULT;
713 break;
715 default:
716 err = -ENOPROTOOPT;
717 break;
720 release_sock(sk);
721 return err;
724 static int sco_sock_release(struct socket *sock)
726 struct sock *sk = sock->sk;
727 int err = 0;
729 BT_DBG("sock %p, sk %p", sock, sk);
731 if (!sk)
732 return 0;
734 sco_sock_close(sk);
736 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) {
737 lock_sock(sk);
738 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
739 release_sock(sk);
742 sock_orphan(sk);
743 sco_sock_kill(sk);
744 return err;
747 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
749 BT_DBG("conn %p", conn);
751 sco_pi(sk)->conn = conn;
752 conn->sk = sk;
754 if (parent)
755 bt_accept_enqueue(parent, sk);
758 /* Delete channel.
759 * Must be called on the locked socket. */
760 static void sco_chan_del(struct sock *sk, int err)
762 struct sco_conn *conn;
764 conn = sco_pi(sk)->conn;
766 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
768 if (conn) {
769 sco_conn_lock(conn);
770 conn->sk = NULL;
771 sco_pi(sk)->conn = NULL;
772 sco_conn_unlock(conn);
773 hci_conn_put(conn->hcon);
776 sk->sk_state = BT_CLOSED;
777 sk->sk_err = err;
778 sk->sk_state_change(sk);
780 sk->sk_zapped = 1;
783 static void sco_conn_ready(struct sco_conn *conn)
785 struct sock *parent, *sk;
787 BT_DBG("conn %p", conn);
789 sco_conn_lock(conn);
791 if ((sk = conn->sk)) {
792 sco_sock_clear_timer(sk);
793 bh_lock_sock(sk);
794 sk->sk_state = BT_CONNECTED;
795 sk->sk_state_change(sk);
796 bh_unlock_sock(sk);
797 } else {
798 parent = sco_get_sock_listen(conn->src);
799 if (!parent)
800 goto done;
802 bh_lock_sock(parent);
804 sk = sco_sock_alloc(NULL, BTPROTO_SCO, GFP_ATOMIC);
805 if (!sk) {
806 bh_unlock_sock(parent);
807 goto done;
810 sco_sock_init(sk, parent);
812 bacpy(&bt_sk(sk)->src, conn->src);
813 bacpy(&bt_sk(sk)->dst, conn->dst);
815 hci_conn_hold(conn->hcon);
816 __sco_chan_add(conn, sk, parent);
818 sk->sk_state = BT_CONNECTED;
820 /* Wake up parent */
821 parent->sk_data_ready(parent, 1);
823 bh_unlock_sock(parent);
826 done:
827 sco_conn_unlock(conn);
830 /* ----- SCO interface with lower layer (HCI) ----- */
831 static int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
833 BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
835 /* Always accept connection */
836 return HCI_LM_ACCEPT;
839 static int sco_connect_cfm(struct hci_conn *hcon, __u8 status)
841 BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
843 if (hcon->type != SCO_LINK)
844 return 0;
846 if (!status) {
847 struct sco_conn *conn;
849 conn = sco_conn_add(hcon, status);
850 if (conn)
851 sco_conn_ready(conn);
852 } else
853 sco_conn_del(hcon, bt_err(status));
855 return 0;
858 static int sco_disconn_ind(struct hci_conn *hcon, __u8 reason)
860 BT_DBG("hcon %p reason %d", hcon, reason);
862 if (hcon->type != SCO_LINK)
863 return 0;
865 sco_conn_del(hcon, bt_err(reason));
866 return 0;
869 static int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
871 struct sco_conn *conn = hcon->sco_data;
873 if (!conn)
874 goto drop;
876 BT_DBG("conn %p len %d", conn, skb->len);
878 if (skb->len) {
879 sco_recv_frame(conn, skb);
880 return 0;
883 drop:
884 kfree_skb(skb);
885 return 0;
888 /* ---- Proc fs support ---- */
889 #ifdef CONFIG_PROC_FS
890 static void *sco_seq_start(struct seq_file *seq, loff_t *pos)
892 struct sock *sk;
893 struct hlist_node *node;
894 loff_t l = *pos;
896 read_lock_bh(&sco_sk_list.lock);
898 sk_for_each(sk, node, &sco_sk_list.head)
899 if (!l--)
900 goto found;
901 sk = NULL;
902 found:
903 return sk;
906 static void *sco_seq_next(struct seq_file *seq, void *e, loff_t *pos)
908 struct sock *sk = e;
909 (*pos)++;
910 return sk_next(sk);
913 static void sco_seq_stop(struct seq_file *seq, void *e)
915 read_unlock_bh(&sco_sk_list.lock);
918 static int sco_seq_show(struct seq_file *seq, void *e)
920 struct sock *sk = e;
921 seq_printf(seq, "%s %s %d\n",
922 batostr(&bt_sk(sk)->src), batostr(&bt_sk(sk)->dst), sk->sk_state);
923 return 0;
926 static struct seq_operations sco_seq_ops = {
927 .start = sco_seq_start,
928 .next = sco_seq_next,
929 .stop = sco_seq_stop,
930 .show = sco_seq_show
933 static int sco_seq_open(struct inode *inode, struct file *file)
935 return seq_open(file, &sco_seq_ops);
938 static struct file_operations sco_seq_fops = {
939 .owner = THIS_MODULE,
940 .open = sco_seq_open,
941 .read = seq_read,
942 .llseek = seq_lseek,
943 .release = seq_release,
946 static int __init sco_proc_init(void)
948 struct proc_dir_entry *p = create_proc_entry("sco", S_IRUGO, proc_bt);
949 if (!p)
950 return -ENOMEM;
951 p->owner = THIS_MODULE;
952 p->proc_fops = &sco_seq_fops;
953 return 0;
956 static void __exit sco_proc_cleanup(void)
958 remove_proc_entry("sco", proc_bt);
961 #else /* CONFIG_PROC_FS */
963 static int __init sco_proc_init(void)
965 return 0;
968 static void __exit sco_proc_cleanup(void)
970 return;
972 #endif /* CONFIG_PROC_FS */
974 static struct proto_ops sco_sock_ops = {
975 .family = PF_BLUETOOTH,
976 .owner = THIS_MODULE,
977 .release = sco_sock_release,
978 .bind = sco_sock_bind,
979 .connect = sco_sock_connect,
980 .listen = sco_sock_listen,
981 .accept = sco_sock_accept,
982 .getname = sco_sock_getname,
983 .sendmsg = sco_sock_sendmsg,
984 .recvmsg = bt_sock_recvmsg,
985 .poll = bt_sock_poll,
986 .ioctl = sock_no_ioctl,
987 .mmap = sock_no_mmap,
988 .socketpair = sock_no_socketpair,
989 .shutdown = sock_no_shutdown,
990 .setsockopt = sco_sock_setsockopt,
991 .getsockopt = sco_sock_getsockopt
994 static struct net_proto_family sco_sock_family_ops = {
995 .family = PF_BLUETOOTH,
996 .owner = THIS_MODULE,
997 .create = sco_sock_create,
1000 static struct hci_proto sco_hci_proto = {
1001 .name = "SCO",
1002 .id = HCI_PROTO_SCO,
1003 .connect_ind = sco_connect_ind,
1004 .connect_cfm = sco_connect_cfm,
1005 .disconn_ind = sco_disconn_ind,
1006 .recv_scodata = sco_recv_scodata
1009 static int __init sco_init(void)
1011 int err;
1013 if ((err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops))) {
1014 BT_ERR("SCO socket registration failed");
1015 return err;
1018 if ((err = hci_register_proto(&sco_hci_proto))) {
1019 BT_ERR("SCO protocol registration failed");
1020 return err;
1023 sco_proc_init();
1025 BT_INFO("SCO (Voice Link) ver %s", VERSION);
1026 BT_INFO("SCO socket layer initialized");
1028 return 0;
1031 static void __exit sco_exit(void)
1033 int err;
1035 sco_proc_cleanup();
1037 /* Unregister socket, protocol and notifier */
1038 if ((err = bt_sock_unregister(BTPROTO_SCO)))
1039 BT_ERR("SCO socket unregistration failed. %d", err);
1041 if ((err = hci_unregister_proto(&sco_hci_proto)))
1042 BT_ERR("SCO protocol unregistration failed. %d", err);
1045 module_init(sco_init);
1046 module_exit(sco_exit);
1048 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
1049 MODULE_DESCRIPTION("Bluetooth SCO ver " VERSION);
1050 MODULE_VERSION(VERSION);
1051 MODULE_LICENSE("GPL");
1052 MODULE_ALIAS("bt-proto-2");