[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / net / bluetooth / sco.c
blob746c11fc017e9b8e73320ac126e68a607d02e76a
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/sched.h>
34 #include <linux/slab.h>
35 #include <linux/poll.h>
36 #include <linux/fcntl.h>
37 #include <linux/init.h>
38 #include <linux/interrupt.h>
39 #include <linux/socket.h>
40 #include <linux/skbuff.h>
41 #include <linux/proc_fs.h>
42 #include <linux/seq_file.h>
43 #include <linux/list.h>
44 #include <net/sock.h>
46 #include <asm/system.h>
47 #include <asm/uaccess.h>
49 #include <net/bluetooth/bluetooth.h>
50 #include <net/bluetooth/hci_core.h>
51 #include <net/bluetooth/sco.h>
53 #ifndef CONFIG_BT_SCO_DEBUG
54 #undef BT_DBG
55 #define BT_DBG(D...)
56 #endif
58 #define VERSION "0.4"
60 static struct proto_ops sco_sock_ops;
62 static struct bt_sock_list sco_sk_list = {
63 .lock = RW_LOCK_UNLOCKED
66 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent);
67 static void sco_chan_del(struct sock *sk, int err);
69 static int sco_conn_del(struct hci_conn *conn, int err);
71 static void sco_sock_close(struct sock *sk);
72 static void sco_sock_kill(struct sock *sk);
74 /* ---- SCO timers ---- */
75 static void sco_sock_timeout(unsigned long arg)
77 struct sock *sk = (struct sock *) arg;
79 BT_DBG("sock %p state %d", sk, sk->sk_state);
81 bh_lock_sock(sk);
82 sk->sk_err = ETIMEDOUT;
83 sk->sk_state_change(sk);
84 bh_unlock_sock(sk);
86 sco_sock_kill(sk);
87 sock_put(sk);
90 static void sco_sock_set_timer(struct sock *sk, long timeout)
92 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
93 sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
96 static void sco_sock_clear_timer(struct sock *sk)
98 BT_DBG("sock %p state %d", sk, sk->sk_state);
99 sk_stop_timer(sk, &sk->sk_timer);
102 static void sco_sock_init_timer(struct sock *sk)
104 init_timer(&sk->sk_timer);
105 sk->sk_timer.function = sco_sock_timeout;
106 sk->sk_timer.data = (unsigned long)sk;
109 /* ---- SCO connections ---- */
110 static struct sco_conn *sco_conn_add(struct hci_conn *hcon, __u8 status)
112 struct hci_dev *hdev = hcon->hdev;
113 struct sco_conn *conn;
115 if ((conn = hcon->sco_data))
116 return conn;
118 if (status)
119 return conn;
121 if (!(conn = kmalloc(sizeof(struct sco_conn), GFP_ATOMIC)))
122 return NULL;
123 memset(conn, 0, sizeof(struct sco_conn));
125 spin_lock_init(&conn->lock);
127 hcon->sco_data = conn;
128 conn->hcon = hcon;
130 conn->src = &hdev->bdaddr;
131 conn->dst = &hcon->dst;
133 if (hdev->sco_mtu > 0)
134 conn->mtu = hdev->sco_mtu;
135 else
136 conn->mtu = 60;
138 BT_DBG("hcon %p conn %p", hcon, conn);
139 return conn;
142 static inline struct sock *sco_chan_get(struct sco_conn *conn)
144 struct sock *sk = NULL;
145 sco_conn_lock(conn);
146 sk = conn->sk;
147 sco_conn_unlock(conn);
148 return sk;
151 static int sco_conn_del(struct hci_conn *hcon, int err)
153 struct sco_conn *conn;
154 struct sock *sk;
156 if (!(conn = hcon->sco_data))
157 return 0;
159 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
161 /* Kill socket */
162 if ((sk = sco_chan_get(conn))) {
163 bh_lock_sock(sk);
164 sco_sock_clear_timer(sk);
165 sco_chan_del(sk, err);
166 bh_unlock_sock(sk);
167 sco_sock_kill(sk);
170 hcon->sco_data = NULL;
171 kfree(conn);
172 return 0;
175 static inline int sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
177 int err = 0;
179 sco_conn_lock(conn);
180 if (conn->sk) {
181 err = -EBUSY;
182 } else {
183 __sco_chan_add(conn, sk, parent);
185 sco_conn_unlock(conn);
186 return err;
189 static int sco_connect(struct sock *sk)
191 bdaddr_t *src = &bt_sk(sk)->src;
192 bdaddr_t *dst = &bt_sk(sk)->dst;
193 struct sco_conn *conn;
194 struct hci_conn *hcon;
195 struct hci_dev *hdev;
196 int err = 0;
198 BT_DBG("%s -> %s", batostr(src), batostr(dst));
200 if (!(hdev = hci_get_route(dst, src)))
201 return -EHOSTUNREACH;
203 hci_dev_lock_bh(hdev);
205 err = -ENOMEM;
207 hcon = hci_connect(hdev, SCO_LINK, dst);
208 if (!hcon)
209 goto done;
211 conn = sco_conn_add(hcon, 0);
212 if (!conn) {
213 hci_conn_put(hcon);
214 goto done;
217 /* Update source addr of the socket */
218 bacpy(src, conn->src);
220 err = sco_chan_add(conn, sk, NULL);
221 if (err)
222 goto done;
224 if (hcon->state == BT_CONNECTED) {
225 sco_sock_clear_timer(sk);
226 sk->sk_state = BT_CONNECTED;
227 } else {
228 sk->sk_state = BT_CONNECT;
229 sco_sock_set_timer(sk, sk->sk_sndtimeo);
231 done:
232 hci_dev_unlock_bh(hdev);
233 hci_dev_put(hdev);
234 return err;
237 static inline int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
239 struct sco_conn *conn = sco_pi(sk)->conn;
240 struct sk_buff *skb;
241 int err, count;
243 /* Check outgoing MTU */
244 if (len > conn->mtu)
245 return -EINVAL;
247 BT_DBG("sk %p len %d", sk, len);
249 count = min_t(unsigned int, conn->mtu, len);
250 if (!(skb = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err)))
251 return err;
253 if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) {
254 err = -EFAULT;
255 goto fail;
258 if ((err = hci_send_sco(conn->hcon, skb)) < 0)
259 goto fail;
261 return count;
263 fail:
264 kfree_skb(skb);
265 return err;
268 static inline void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
270 struct sock *sk = sco_chan_get(conn);
272 if (!sk)
273 goto drop;
275 BT_DBG("sk %p len %d", sk, skb->len);
277 if (sk->sk_state != BT_CONNECTED)
278 goto drop;
280 if (!sock_queue_rcv_skb(sk, skb))
281 return;
283 drop:
284 kfree_skb(skb);
285 return;
288 /* -------- Socket interface ---------- */
289 static struct sock *__sco_get_sock_by_addr(bdaddr_t *ba)
291 struct sock *sk;
292 struct hlist_node *node;
294 sk_for_each(sk, node, &sco_sk_list.head)
295 if (!bacmp(&bt_sk(sk)->src, ba))
296 goto found;
297 sk = NULL;
298 found:
299 return sk;
302 /* Find socket listening on source bdaddr.
303 * Returns closest match.
305 static struct sock *sco_get_sock_listen(bdaddr_t *src)
307 struct sock *sk = NULL, *sk1 = NULL;
308 struct hlist_node *node;
310 read_lock(&sco_sk_list.lock);
312 sk_for_each(sk, node, &sco_sk_list.head) {
313 if (sk->sk_state != BT_LISTEN)
314 continue;
316 /* Exact match. */
317 if (!bacmp(&bt_sk(sk)->src, src))
318 break;
320 /* Closest match */
321 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
322 sk1 = sk;
325 read_unlock(&sco_sk_list.lock);
327 return node ? sk : sk1;
330 static void sco_sock_destruct(struct sock *sk)
332 BT_DBG("sk %p", sk);
334 skb_queue_purge(&sk->sk_receive_queue);
335 skb_queue_purge(&sk->sk_write_queue);
338 static void sco_sock_cleanup_listen(struct sock *parent)
340 struct sock *sk;
342 BT_DBG("parent %p", parent);
344 /* Close not yet accepted channels */
345 while ((sk = bt_accept_dequeue(parent, NULL))) {
346 sco_sock_close(sk);
347 sco_sock_kill(sk);
350 parent->sk_state = BT_CLOSED;
351 sock_set_flag(parent, SOCK_ZAPPED);
354 /* Kill socket (only if zapped and orphan)
355 * Must be called on unlocked socket.
357 static void sco_sock_kill(struct sock *sk)
359 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
360 return;
362 BT_DBG("sk %p state %d", sk, sk->sk_state);
364 /* Kill poor orphan */
365 bt_sock_unlink(&sco_sk_list, sk);
366 sock_set_flag(sk, SOCK_DEAD);
367 sock_put(sk);
370 /* Close socket.
371 * Must be called on unlocked socket.
373 static void sco_sock_close(struct sock *sk)
375 struct sco_conn *conn;
377 sco_sock_clear_timer(sk);
379 lock_sock(sk);
381 conn = sco_pi(sk)->conn;
383 BT_DBG("sk %p state %d conn %p socket %p", sk, sk->sk_state, conn, sk->sk_socket);
385 switch (sk->sk_state) {
386 case BT_LISTEN:
387 sco_sock_cleanup_listen(sk);
388 break;
390 case BT_CONNECTED:
391 case BT_CONFIG:
392 case BT_CONNECT:
393 case BT_DISCONN:
394 sco_chan_del(sk, ECONNRESET);
395 break;
397 default:
398 sock_set_flag(sk, SOCK_ZAPPED);
399 break;
402 release_sock(sk);
404 sco_sock_kill(sk);
407 static void sco_sock_init(struct sock *sk, struct sock *parent)
409 BT_DBG("sk %p", sk);
411 if (parent)
412 sk->sk_type = parent->sk_type;
415 static struct proto sco_proto = {
416 .name = "SCO",
417 .owner = THIS_MODULE,
418 .obj_size = sizeof(struct sco_pinfo)
421 static struct sock *sco_sock_alloc(struct socket *sock, int proto, int prio)
423 struct sock *sk;
425 sk = sk_alloc(PF_BLUETOOTH, prio, &sco_proto, 1);
426 if (!sk)
427 return NULL;
429 sock_init_data(sock, sk);
430 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
432 sk->sk_destruct = sco_sock_destruct;
433 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
435 sock_reset_flag(sk, SOCK_ZAPPED);
437 sk->sk_protocol = proto;
438 sk->sk_state = BT_OPEN;
440 sco_sock_init_timer(sk);
442 bt_sock_link(&sco_sk_list, sk);
443 return sk;
446 static int sco_sock_create(struct socket *sock, int protocol)
448 struct sock *sk;
450 BT_DBG("sock %p", sock);
452 sock->state = SS_UNCONNECTED;
454 if (sock->type != SOCK_SEQPACKET)
455 return -ESOCKTNOSUPPORT;
457 sock->ops = &sco_sock_ops;
459 if (!(sk = sco_sock_alloc(sock, protocol, GFP_KERNEL)))
460 return -ENOMEM;
462 sco_sock_init(sk, NULL);
463 return 0;
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;
471 int err = 0;
473 BT_DBG("sk %p %s", sk, batostr(&sa->sco_bdaddr));
475 if (!addr || addr->sa_family != AF_BLUETOOTH)
476 return -EINVAL;
478 lock_sock(sk);
480 if (sk->sk_state != BT_OPEN) {
481 err = -EBADFD;
482 goto done;
485 write_lock_bh(&sco_sk_list.lock);
487 if (bacmp(src, BDADDR_ANY) && __sco_get_sock_by_addr(src)) {
488 err = -EADDRINUSE;
489 } else {
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);
497 done:
498 release_sock(sk);
499 return err;
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;
506 int err = 0;
509 BT_DBG("sk %p", sk);
511 if (addr->sa_family != AF_BLUETOOTH || alen < sizeof(struct sockaddr_sco))
512 return -EINVAL;
514 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
515 return -EBADFD;
517 if (sk->sk_type != SOCK_SEQPACKET)
518 return -EINVAL;
520 lock_sock(sk);
522 /* Set destination address and psm */
523 bacpy(&bt_sk(sk)->dst, &sa->sco_bdaddr);
525 if ((err = sco_connect(sk)))
526 goto done;
528 err = bt_sock_wait_state(sk, BT_CONNECTED,
529 sock_sndtimeo(sk, flags & O_NONBLOCK));
531 done:
532 release_sock(sk);
533 return err;
536 static int sco_sock_listen(struct socket *sock, int backlog)
538 struct sock *sk = sock->sk;
539 int err = 0;
541 BT_DBG("sk %p backlog %d", sk, backlog);
543 lock_sock(sk);
545 if (sk->sk_state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
546 err = -EBADFD;
547 goto done;
550 sk->sk_max_ack_backlog = backlog;
551 sk->sk_ack_backlog = 0;
552 sk->sk_state = BT_LISTEN;
554 done:
555 release_sock(sk);
556 return err;
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;
563 long timeo;
564 int err = 0;
566 lock_sock(sk);
568 if (sk->sk_state != BT_LISTEN) {
569 err = -EBADFD;
570 goto done;
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);
581 if (!timeo) {
582 err = -EAGAIN;
583 break;
586 release_sock(sk);
587 timeo = schedule_timeout(timeo);
588 lock_sock(sk);
590 if (sk->sk_state != BT_LISTEN) {
591 err = -EBADFD;
592 break;
595 if (signal_pending(current)) {
596 err = sock_intr_errno(timeo);
597 break;
600 set_current_state(TASK_RUNNING);
601 remove_wait_queue(sk->sk_sleep, &wait);
603 if (err)
604 goto done;
606 newsock->state = SS_CONNECTED;
608 BT_DBG("new socket %p", ch);
610 done:
611 release_sock(sk);
612 return err;
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);
625 if (peer)
626 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst);
627 else
628 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src);
630 return 0;
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;
637 int err = 0;
639 BT_DBG("sock %p, sk %p", sock, sk);
641 if (sk->sk_err)
642 return sock_error(sk);
644 if (msg->msg_flags & MSG_OOB)
645 return -EOPNOTSUPP;
647 lock_sock(sk);
649 if (sk->sk_state == BT_CONNECTED)
650 err = sco_send_frame(sk, msg, len);
651 else
652 err = -ENOTCONN;
654 release_sock(sk);
655 return err;
658 static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen)
660 struct sock *sk = sock->sk;
661 int err = 0;
663 BT_DBG("sk %p", sk);
665 lock_sock(sk);
667 switch (optname) {
668 default:
669 err = -ENOPROTOOPT;
670 break;
673 release_sock(sk);
674 return err;
677 static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
679 struct sock *sk = sock->sk;
680 struct sco_options opts;
681 struct sco_conninfo cinfo;
682 int len, err = 0;
684 BT_DBG("sk %p", sk);
686 if (get_user(len, optlen))
687 return -EFAULT;
689 lock_sock(sk);
691 switch (optname) {
692 case SCO_OPTIONS:
693 if (sk->sk_state != BT_CONNECTED) {
694 err = -ENOTCONN;
695 break;
698 opts.mtu = sco_pi(sk)->conn->mtu;
700 BT_DBG("mtu %d", opts.mtu);
702 len = min_t(unsigned int, len, sizeof(opts));
703 if (copy_to_user(optval, (char *)&opts, len))
704 err = -EFAULT;
706 break;
708 case SCO_CONNINFO:
709 if (sk->sk_state != BT_CONNECTED) {
710 err = -ENOTCONN;
711 break;
714 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
715 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
717 len = min_t(unsigned int, len, sizeof(cinfo));
718 if (copy_to_user(optval, (char *)&cinfo, len))
719 err = -EFAULT;
721 break;
723 default:
724 err = -ENOPROTOOPT;
725 break;
728 release_sock(sk);
729 return err;
732 static int sco_sock_release(struct socket *sock)
734 struct sock *sk = sock->sk;
735 int err = 0;
737 BT_DBG("sock %p, sk %p", sock, sk);
739 if (!sk)
740 return 0;
742 sco_sock_close(sk);
744 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) {
745 lock_sock(sk);
746 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
747 release_sock(sk);
750 sock_orphan(sk);
751 sco_sock_kill(sk);
752 return err;
755 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
757 BT_DBG("conn %p", conn);
759 sco_pi(sk)->conn = conn;
760 conn->sk = sk;
762 if (parent)
763 bt_accept_enqueue(parent, sk);
766 /* Delete channel.
767 * Must be called on the locked socket. */
768 static void sco_chan_del(struct sock *sk, int err)
770 struct sco_conn *conn;
772 conn = sco_pi(sk)->conn;
774 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
776 if (conn) {
777 sco_conn_lock(conn);
778 conn->sk = NULL;
779 sco_pi(sk)->conn = NULL;
780 sco_conn_unlock(conn);
781 hci_conn_put(conn->hcon);
784 sk->sk_state = BT_CLOSED;
785 sk->sk_err = err;
786 sk->sk_state_change(sk);
788 sock_set_flag(sk, SOCK_ZAPPED);
791 static void sco_conn_ready(struct sco_conn *conn)
793 struct sock *parent, *sk;
795 BT_DBG("conn %p", conn);
797 sco_conn_lock(conn);
799 if ((sk = conn->sk)) {
800 sco_sock_clear_timer(sk);
801 bh_lock_sock(sk);
802 sk->sk_state = BT_CONNECTED;
803 sk->sk_state_change(sk);
804 bh_unlock_sock(sk);
805 } else {
806 parent = sco_get_sock_listen(conn->src);
807 if (!parent)
808 goto done;
810 bh_lock_sock(parent);
812 sk = sco_sock_alloc(NULL, BTPROTO_SCO, GFP_ATOMIC);
813 if (!sk) {
814 bh_unlock_sock(parent);
815 goto done;
818 sco_sock_init(sk, parent);
820 bacpy(&bt_sk(sk)->src, conn->src);
821 bacpy(&bt_sk(sk)->dst, conn->dst);
823 hci_conn_hold(conn->hcon);
824 __sco_chan_add(conn, sk, parent);
826 sk->sk_state = BT_CONNECTED;
828 /* Wake up parent */
829 parent->sk_data_ready(parent, 1);
831 bh_unlock_sock(parent);
834 done:
835 sco_conn_unlock(conn);
838 /* ----- SCO interface with lower layer (HCI) ----- */
839 static int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
841 BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
843 /* Always accept connection */
844 return HCI_LM_ACCEPT;
847 static int sco_connect_cfm(struct hci_conn *hcon, __u8 status)
849 BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
851 if (hcon->type != SCO_LINK)
852 return 0;
854 if (!status) {
855 struct sco_conn *conn;
857 conn = sco_conn_add(hcon, status);
858 if (conn)
859 sco_conn_ready(conn);
860 } else
861 sco_conn_del(hcon, bt_err(status));
863 return 0;
866 static int sco_disconn_ind(struct hci_conn *hcon, __u8 reason)
868 BT_DBG("hcon %p reason %d", hcon, reason);
870 if (hcon->type != SCO_LINK)
871 return 0;
873 sco_conn_del(hcon, bt_err(reason));
874 return 0;
877 static int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
879 struct sco_conn *conn = hcon->sco_data;
881 if (!conn)
882 goto drop;
884 BT_DBG("conn %p len %d", conn, skb->len);
886 if (skb->len) {
887 sco_recv_frame(conn, skb);
888 return 0;
891 drop:
892 kfree_skb(skb);
893 return 0;
896 /* ---- Proc fs support ---- */
897 #ifdef CONFIG_PROC_FS
898 static void *sco_seq_start(struct seq_file *seq, loff_t *pos)
900 struct sock *sk;
901 struct hlist_node *node;
902 loff_t l = *pos;
904 read_lock_bh(&sco_sk_list.lock);
906 sk_for_each(sk, node, &sco_sk_list.head)
907 if (!l--)
908 goto found;
909 sk = NULL;
910 found:
911 return sk;
914 static void *sco_seq_next(struct seq_file *seq, void *e, loff_t *pos)
916 struct sock *sk = e;
917 (*pos)++;
918 return sk_next(sk);
921 static void sco_seq_stop(struct seq_file *seq, void *e)
923 read_unlock_bh(&sco_sk_list.lock);
926 static int sco_seq_show(struct seq_file *seq, void *e)
928 struct sock *sk = e;
929 seq_printf(seq, "%s %s %d\n",
930 batostr(&bt_sk(sk)->src), batostr(&bt_sk(sk)->dst), sk->sk_state);
931 return 0;
934 static struct seq_operations sco_seq_ops = {
935 .start = sco_seq_start,
936 .next = sco_seq_next,
937 .stop = sco_seq_stop,
938 .show = sco_seq_show
941 static int sco_seq_open(struct inode *inode, struct file *file)
943 return seq_open(file, &sco_seq_ops);
946 static struct file_operations sco_seq_fops = {
947 .owner = THIS_MODULE,
948 .open = sco_seq_open,
949 .read = seq_read,
950 .llseek = seq_lseek,
951 .release = seq_release,
954 static int __init sco_proc_init(void)
956 struct proc_dir_entry *p = create_proc_entry("sco", S_IRUGO, proc_bt);
957 if (!p)
958 return -ENOMEM;
959 p->owner = THIS_MODULE;
960 p->proc_fops = &sco_seq_fops;
961 return 0;
964 static void __exit sco_proc_cleanup(void)
966 remove_proc_entry("sco", proc_bt);
969 #else /* CONFIG_PROC_FS */
971 static int __init sco_proc_init(void)
973 return 0;
976 static void __exit sco_proc_cleanup(void)
978 return;
980 #endif /* CONFIG_PROC_FS */
982 static struct proto_ops sco_sock_ops = {
983 .family = PF_BLUETOOTH,
984 .owner = THIS_MODULE,
985 .release = sco_sock_release,
986 .bind = sco_sock_bind,
987 .connect = sco_sock_connect,
988 .listen = sco_sock_listen,
989 .accept = sco_sock_accept,
990 .getname = sco_sock_getname,
991 .sendmsg = sco_sock_sendmsg,
992 .recvmsg = bt_sock_recvmsg,
993 .poll = bt_sock_poll,
994 .ioctl = sock_no_ioctl,
995 .mmap = sock_no_mmap,
996 .socketpair = sock_no_socketpair,
997 .shutdown = sock_no_shutdown,
998 .setsockopt = sco_sock_setsockopt,
999 .getsockopt = sco_sock_getsockopt
1002 static struct net_proto_family sco_sock_family_ops = {
1003 .family = PF_BLUETOOTH,
1004 .owner = THIS_MODULE,
1005 .create = sco_sock_create,
1008 static struct hci_proto sco_hci_proto = {
1009 .name = "SCO",
1010 .id = HCI_PROTO_SCO,
1011 .connect_ind = sco_connect_ind,
1012 .connect_cfm = sco_connect_cfm,
1013 .disconn_ind = sco_disconn_ind,
1014 .recv_scodata = sco_recv_scodata
1017 static int __init sco_init(void)
1019 int err;
1021 err = proto_register(&sco_proto, 0);
1022 if (err < 0)
1023 return err;
1025 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1026 if (err < 0) {
1027 BT_ERR("SCO socket registration failed");
1028 goto error;
1031 err = hci_register_proto(&sco_hci_proto);
1032 if (err < 0) {
1033 BT_ERR("SCO protocol registration failed");
1034 bt_sock_unregister(BTPROTO_SCO);
1035 goto error;
1038 sco_proc_init();
1040 BT_INFO("SCO (Voice Link) ver %s", VERSION);
1041 BT_INFO("SCO socket layer initialized");
1043 return 0;
1045 error:
1046 proto_unregister(&sco_proto);
1047 return err;
1050 static void __exit sco_exit(void)
1052 sco_proc_cleanup();
1054 if (bt_sock_unregister(BTPROTO_SCO) < 0)
1055 BT_ERR("SCO socket unregistration failed");
1057 if (hci_unregister_proto(&sco_hci_proto) < 0)
1058 BT_ERR("SCO protocol unregistration failed");
1060 proto_unregister(&sco_proto);
1063 module_init(sco_init);
1064 module_exit(sco_exit);
1066 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>");
1067 MODULE_DESCRIPTION("Bluetooth SCO ver " VERSION);
1068 MODULE_VERSION(VERSION);
1069 MODULE_LICENSE("GPL");
1070 MODULE_ALIAS("bt-proto-2");