sata_mv: fix oops
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / bluetooth / sco.c
blob7714a2ec3854d032ed0038ebcc8aea5e39494de7
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/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>
42 #include <net/sock.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
52 #undef BT_DBG
53 #define BT_DBG(D...)
54 #endif
56 #define VERSION "0.5"
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);
79 bh_lock_sock(sk);
80 sk->sk_err = ETIMEDOUT;
81 sk->sk_state_change(sk);
82 bh_unlock_sock(sk);
84 sco_sock_kill(sk);
85 sock_put(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;
113 if (conn || status)
114 return conn;
116 conn = kzalloc(sizeof(struct sco_conn), GFP_ATOMIC);
117 if (!conn)
118 return NULL;
120 spin_lock_init(&conn->lock);
122 hcon->sco_data = conn;
123 conn->hcon = hcon;
125 conn->src = &hdev->bdaddr;
126 conn->dst = &hcon->dst;
128 if (hdev->sco_mtu > 0)
129 conn->mtu = hdev->sco_mtu;
130 else
131 conn->mtu = 60;
133 BT_DBG("hcon %p conn %p", hcon, conn);
135 return conn;
138 static inline struct sock *sco_chan_get(struct sco_conn *conn)
140 struct sock *sk = NULL;
141 sco_conn_lock(conn);
142 sk = conn->sk;
143 sco_conn_unlock(conn);
144 return sk;
147 static int sco_conn_del(struct hci_conn *hcon, int err)
149 struct sco_conn *conn;
150 struct sock *sk;
152 if (!(conn = hcon->sco_data))
153 return 0;
155 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
157 /* Kill socket */
158 if ((sk = sco_chan_get(conn))) {
159 bh_lock_sock(sk);
160 sco_sock_clear_timer(sk);
161 sco_chan_del(sk, err);
162 bh_unlock_sock(sk);
163 sco_sock_kill(sk);
166 hcon->sco_data = NULL;
167 kfree(conn);
168 return 0;
171 static inline int sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
173 int err = 0;
175 sco_conn_lock(conn);
176 if (conn->sk) {
177 err = -EBUSY;
178 } else {
179 __sco_chan_add(conn, sk, parent);
181 sco_conn_unlock(conn);
182 return err;
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;
192 int err = 0;
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);
201 err = -ENOMEM;
203 hcon = hci_connect(hdev, SCO_LINK, dst);
204 if (!hcon)
205 goto done;
207 conn = sco_conn_add(hcon, 0);
208 if (!conn) {
209 hci_conn_put(hcon);
210 goto done;
213 /* Update source addr of the socket */
214 bacpy(src, conn->src);
216 err = sco_chan_add(conn, sk, NULL);
217 if (err)
218 goto done;
220 if (hcon->state == BT_CONNECTED) {
221 sco_sock_clear_timer(sk);
222 sk->sk_state = BT_CONNECTED;
223 } else {
224 sk->sk_state = BT_CONNECT;
225 sco_sock_set_timer(sk, sk->sk_sndtimeo);
227 done:
228 hci_dev_unlock_bh(hdev);
229 hci_dev_put(hdev);
230 return err;
233 static inline int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
235 struct sco_conn *conn = sco_pi(sk)->conn;
236 struct sk_buff *skb;
237 int err, count;
239 /* Check outgoing MTU */
240 if (len > conn->mtu)
241 return -EINVAL;
243 BT_DBG("sk %p len %d", sk, len);
245 count = min_t(unsigned int, conn->mtu, len);
246 if (!(skb = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err)))
247 return err;
249 if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) {
250 err = -EFAULT;
251 goto fail;
254 if ((err = hci_send_sco(conn->hcon, skb)) < 0)
255 return err;
257 return count;
259 fail:
260 kfree_skb(skb);
261 return err;
264 static inline void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
266 struct sock *sk = sco_chan_get(conn);
268 if (!sk)
269 goto drop;
271 BT_DBG("sk %p len %d", sk, skb->len);
273 if (sk->sk_state != BT_CONNECTED)
274 goto drop;
276 if (!sock_queue_rcv_skb(sk, skb))
277 return;
279 drop:
280 kfree_skb(skb);
281 return;
284 /* -------- Socket interface ---------- */
285 static struct sock *__sco_get_sock_by_addr(bdaddr_t *ba)
287 struct sock *sk;
288 struct hlist_node *node;
290 sk_for_each(sk, node, &sco_sk_list.head)
291 if (!bacmp(&bt_sk(sk)->src, ba))
292 goto found;
293 sk = NULL;
294 found:
295 return sk;
298 /* Find socket listening on source bdaddr.
299 * Returns closest match.
301 static struct sock *sco_get_sock_listen(bdaddr_t *src)
303 struct sock *sk = NULL, *sk1 = NULL;
304 struct hlist_node *node;
306 read_lock(&sco_sk_list.lock);
308 sk_for_each(sk, node, &sco_sk_list.head) {
309 if (sk->sk_state != BT_LISTEN)
310 continue;
312 /* Exact match. */
313 if (!bacmp(&bt_sk(sk)->src, src))
314 break;
316 /* Closest match */
317 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
318 sk1 = sk;
321 read_unlock(&sco_sk_list.lock);
323 return node ? sk : sk1;
326 static void sco_sock_destruct(struct sock *sk)
328 BT_DBG("sk %p", sk);
330 skb_queue_purge(&sk->sk_receive_queue);
331 skb_queue_purge(&sk->sk_write_queue);
334 static void sco_sock_cleanup_listen(struct sock *parent)
336 struct sock *sk;
338 BT_DBG("parent %p", parent);
340 /* Close not yet accepted channels */
341 while ((sk = bt_accept_dequeue(parent, NULL))) {
342 sco_sock_close(sk);
343 sco_sock_kill(sk);
346 parent->sk_state = BT_CLOSED;
347 sock_set_flag(parent, SOCK_ZAPPED);
350 /* Kill socket (only if zapped and orphan)
351 * Must be called on unlocked socket.
353 static void sco_sock_kill(struct sock *sk)
355 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
356 return;
358 BT_DBG("sk %p state %d", sk, sk->sk_state);
360 /* Kill poor orphan */
361 bt_sock_unlink(&sco_sk_list, sk);
362 sock_set_flag(sk, SOCK_DEAD);
363 sock_put(sk);
366 /* Close socket.
367 * Must be called on unlocked socket.
369 static void sco_sock_close(struct sock *sk)
371 struct sco_conn *conn;
373 sco_sock_clear_timer(sk);
375 lock_sock(sk);
377 conn = sco_pi(sk)->conn;
379 BT_DBG("sk %p state %d conn %p socket %p", sk, sk->sk_state, conn, sk->sk_socket);
381 switch (sk->sk_state) {
382 case BT_LISTEN:
383 sco_sock_cleanup_listen(sk);
384 break;
386 case BT_CONNECTED:
387 case BT_CONFIG:
388 case BT_CONNECT:
389 case BT_DISCONN:
390 sco_chan_del(sk, ECONNRESET);
391 break;
393 default:
394 sock_set_flag(sk, SOCK_ZAPPED);
395 break;
398 release_sock(sk);
400 sco_sock_kill(sk);
403 static void sco_sock_init(struct sock *sk, struct sock *parent)
405 BT_DBG("sk %p", sk);
407 if (parent)
408 sk->sk_type = parent->sk_type;
411 static struct proto sco_proto = {
412 .name = "SCO",
413 .owner = THIS_MODULE,
414 .obj_size = sizeof(struct sco_pinfo)
417 static struct sock *sco_sock_alloc(struct socket *sock, int proto, gfp_t prio)
419 struct sock *sk;
421 sk = sk_alloc(PF_BLUETOOTH, prio, &sco_proto, 1);
422 if (!sk)
423 return NULL;
425 sock_init_data(sock, sk);
426 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
428 sk->sk_destruct = sco_sock_destruct;
429 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
431 sock_reset_flag(sk, SOCK_ZAPPED);
433 sk->sk_protocol = proto;
434 sk->sk_state = BT_OPEN;
436 sco_sock_init_timer(sk);
438 bt_sock_link(&sco_sk_list, sk);
439 return sk;
442 static int sco_sock_create(struct socket *sock, int protocol)
444 struct sock *sk;
446 BT_DBG("sock %p", sock);
448 sock->state = SS_UNCONNECTED;
450 if (sock->type != SOCK_SEQPACKET)
451 return -ESOCKTNOSUPPORT;
453 sock->ops = &sco_sock_ops;
455 if (!(sk = sco_sock_alloc(sock, protocol, GFP_KERNEL)))
456 return -ENOMEM;
458 sco_sock_init(sk, NULL);
459 return 0;
462 static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
464 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
465 struct sock *sk = sock->sk;
466 bdaddr_t *src = &sa->sco_bdaddr;
467 int err = 0;
469 BT_DBG("sk %p %s", sk, batostr(&sa->sco_bdaddr));
471 if (!addr || addr->sa_family != AF_BLUETOOTH)
472 return -EINVAL;
474 lock_sock(sk);
476 if (sk->sk_state != BT_OPEN) {
477 err = -EBADFD;
478 goto done;
481 write_lock_bh(&sco_sk_list.lock);
483 if (bacmp(src, BDADDR_ANY) && __sco_get_sock_by_addr(src)) {
484 err = -EADDRINUSE;
485 } else {
486 /* Save source address */
487 bacpy(&bt_sk(sk)->src, &sa->sco_bdaddr);
488 sk->sk_state = BT_BOUND;
491 write_unlock_bh(&sco_sk_list.lock);
493 done:
494 release_sock(sk);
495 return err;
498 static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
500 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
501 struct sock *sk = sock->sk;
502 int err = 0;
505 BT_DBG("sk %p", sk);
507 if (addr->sa_family != AF_BLUETOOTH || alen < sizeof(struct sockaddr_sco))
508 return -EINVAL;
510 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
511 return -EBADFD;
513 if (sk->sk_type != SOCK_SEQPACKET)
514 return -EINVAL;
516 lock_sock(sk);
518 /* Set destination address and psm */
519 bacpy(&bt_sk(sk)->dst, &sa->sco_bdaddr);
521 if ((err = sco_connect(sk)))
522 goto done;
524 err = bt_sock_wait_state(sk, BT_CONNECTED,
525 sock_sndtimeo(sk, flags & O_NONBLOCK));
527 done:
528 release_sock(sk);
529 return err;
532 static int sco_sock_listen(struct socket *sock, int backlog)
534 struct sock *sk = sock->sk;
535 int err = 0;
537 BT_DBG("sk %p backlog %d", sk, backlog);
539 lock_sock(sk);
541 if (sk->sk_state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
542 err = -EBADFD;
543 goto done;
546 sk->sk_max_ack_backlog = backlog;
547 sk->sk_ack_backlog = 0;
548 sk->sk_state = BT_LISTEN;
550 done:
551 release_sock(sk);
552 return err;
555 static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
557 DECLARE_WAITQUEUE(wait, current);
558 struct sock *sk = sock->sk, *ch;
559 long timeo;
560 int err = 0;
562 lock_sock(sk);
564 if (sk->sk_state != BT_LISTEN) {
565 err = -EBADFD;
566 goto done;
569 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
571 BT_DBG("sk %p timeo %ld", sk, timeo);
573 /* Wait for an incoming connection. (wake-one). */
574 add_wait_queue_exclusive(sk->sk_sleep, &wait);
575 while (!(ch = bt_accept_dequeue(sk, newsock))) {
576 set_current_state(TASK_INTERRUPTIBLE);
577 if (!timeo) {
578 err = -EAGAIN;
579 break;
582 release_sock(sk);
583 timeo = schedule_timeout(timeo);
584 lock_sock(sk);
586 if (sk->sk_state != BT_LISTEN) {
587 err = -EBADFD;
588 break;
591 if (signal_pending(current)) {
592 err = sock_intr_errno(timeo);
593 break;
596 set_current_state(TASK_RUNNING);
597 remove_wait_queue(sk->sk_sleep, &wait);
599 if (err)
600 goto done;
602 newsock->state = SS_CONNECTED;
604 BT_DBG("new socket %p", ch);
606 done:
607 release_sock(sk);
608 return err;
611 static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
613 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
614 struct sock *sk = sock->sk;
616 BT_DBG("sock %p, sk %p", sock, sk);
618 addr->sa_family = AF_BLUETOOTH;
619 *len = sizeof(struct sockaddr_sco);
621 if (peer)
622 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst);
623 else
624 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src);
626 return 0;
629 static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
630 struct msghdr *msg, size_t len)
632 struct sock *sk = sock->sk;
633 int err = 0;
635 BT_DBG("sock %p, sk %p", sock, sk);
637 err = sock_error(sk);
638 if (err)
639 return err;
641 if (msg->msg_flags & MSG_OOB)
642 return -EOPNOTSUPP;
644 lock_sock(sk);
646 if (sk->sk_state == BT_CONNECTED)
647 err = sco_send_frame(sk, msg, len);
648 else
649 err = -ENOTCONN;
651 release_sock(sk);
652 return err;
655 static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen)
657 struct sock *sk = sock->sk;
658 int err = 0;
660 BT_DBG("sk %p", sk);
662 lock_sock(sk);
664 switch (optname) {
665 default:
666 err = -ENOPROTOOPT;
667 break;
670 release_sock(sk);
671 return err;
674 static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
676 struct sock *sk = sock->sk;
677 struct sco_options opts;
678 struct sco_conninfo cinfo;
679 int len, err = 0;
681 BT_DBG("sk %p", sk);
683 if (get_user(len, optlen))
684 return -EFAULT;
686 lock_sock(sk);
688 switch (optname) {
689 case SCO_OPTIONS:
690 if (sk->sk_state != BT_CONNECTED) {
691 err = -ENOTCONN;
692 break;
695 opts.mtu = sco_pi(sk)->conn->mtu;
697 BT_DBG("mtu %d", opts.mtu);
699 len = min_t(unsigned int, len, sizeof(opts));
700 if (copy_to_user(optval, (char *)&opts, len))
701 err = -EFAULT;
703 break;
705 case SCO_CONNINFO:
706 if (sk->sk_state != BT_CONNECTED) {
707 err = -ENOTCONN;
708 break;
711 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
712 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
714 len = min_t(unsigned int, len, sizeof(cinfo));
715 if (copy_to_user(optval, (char *)&cinfo, len))
716 err = -EFAULT;
718 break;
720 default:
721 err = -ENOPROTOOPT;
722 break;
725 release_sock(sk);
726 return err;
729 static int sco_sock_release(struct socket *sock)
731 struct sock *sk = sock->sk;
732 int err = 0;
734 BT_DBG("sock %p, sk %p", sock, sk);
736 if (!sk)
737 return 0;
739 sco_sock_close(sk);
741 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) {
742 lock_sock(sk);
743 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
744 release_sock(sk);
747 sock_orphan(sk);
748 sco_sock_kill(sk);
749 return err;
752 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
754 BT_DBG("conn %p", conn);
756 sco_pi(sk)->conn = conn;
757 conn->sk = sk;
759 if (parent)
760 bt_accept_enqueue(parent, sk);
763 /* Delete channel.
764 * Must be called on the locked socket. */
765 static void sco_chan_del(struct sock *sk, int err)
767 struct sco_conn *conn;
769 conn = sco_pi(sk)->conn;
771 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
773 if (conn) {
774 sco_conn_lock(conn);
775 conn->sk = NULL;
776 sco_pi(sk)->conn = NULL;
777 sco_conn_unlock(conn);
778 hci_conn_put(conn->hcon);
781 sk->sk_state = BT_CLOSED;
782 sk->sk_err = err;
783 sk->sk_state_change(sk);
785 sock_set_flag(sk, SOCK_ZAPPED);
788 static void sco_conn_ready(struct sco_conn *conn)
790 struct sock *parent, *sk;
792 BT_DBG("conn %p", conn);
794 sco_conn_lock(conn);
796 if ((sk = conn->sk)) {
797 sco_sock_clear_timer(sk);
798 bh_lock_sock(sk);
799 sk->sk_state = BT_CONNECTED;
800 sk->sk_state_change(sk);
801 bh_unlock_sock(sk);
802 } else {
803 parent = sco_get_sock_listen(conn->src);
804 if (!parent)
805 goto done;
807 bh_lock_sock(parent);
809 sk = sco_sock_alloc(NULL, BTPROTO_SCO, GFP_ATOMIC);
810 if (!sk) {
811 bh_unlock_sock(parent);
812 goto done;
815 sco_sock_init(sk, parent);
817 bacpy(&bt_sk(sk)->src, conn->src);
818 bacpy(&bt_sk(sk)->dst, conn->dst);
820 hci_conn_hold(conn->hcon);
821 __sco_chan_add(conn, sk, parent);
823 sk->sk_state = BT_CONNECTED;
825 /* Wake up parent */
826 parent->sk_data_ready(parent, 1);
828 bh_unlock_sock(parent);
831 done:
832 sco_conn_unlock(conn);
835 /* ----- SCO interface with lower layer (HCI) ----- */
836 static int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
838 BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
840 /* Always accept connection */
841 return HCI_LM_ACCEPT;
844 static int sco_connect_cfm(struct hci_conn *hcon, __u8 status)
846 BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
848 if (hcon->type != SCO_LINK)
849 return 0;
851 if (!status) {
852 struct sco_conn *conn;
854 conn = sco_conn_add(hcon, status);
855 if (conn)
856 sco_conn_ready(conn);
857 } else
858 sco_conn_del(hcon, bt_err(status));
860 return 0;
863 static int sco_disconn_ind(struct hci_conn *hcon, __u8 reason)
865 BT_DBG("hcon %p reason %d", hcon, reason);
867 if (hcon->type != SCO_LINK)
868 return 0;
870 sco_conn_del(hcon, bt_err(reason));
871 return 0;
874 static int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
876 struct sco_conn *conn = hcon->sco_data;
878 if (!conn)
879 goto drop;
881 BT_DBG("conn %p len %d", conn, skb->len);
883 if (skb->len) {
884 sco_recv_frame(conn, skb);
885 return 0;
888 drop:
889 kfree_skb(skb);
890 return 0;
893 static ssize_t sco_sysfs_show(struct class *dev, char *buf)
895 struct sock *sk;
896 struct hlist_node *node;
897 char *str = buf;
899 read_lock_bh(&sco_sk_list.lock);
901 sk_for_each(sk, node, &sco_sk_list.head) {
902 str += sprintf(str, "%s %s %d\n",
903 batostr(&bt_sk(sk)->src), batostr(&bt_sk(sk)->dst),
904 sk->sk_state);
907 read_unlock_bh(&sco_sk_list.lock);
909 return (str - buf);
912 static CLASS_ATTR(sco, S_IRUGO, sco_sysfs_show, NULL);
914 static const struct proto_ops sco_sock_ops = {
915 .family = PF_BLUETOOTH,
916 .owner = THIS_MODULE,
917 .release = sco_sock_release,
918 .bind = sco_sock_bind,
919 .connect = sco_sock_connect,
920 .listen = sco_sock_listen,
921 .accept = sco_sock_accept,
922 .getname = sco_sock_getname,
923 .sendmsg = sco_sock_sendmsg,
924 .recvmsg = bt_sock_recvmsg,
925 .poll = bt_sock_poll,
926 .ioctl = sock_no_ioctl,
927 .mmap = sock_no_mmap,
928 .socketpair = sock_no_socketpair,
929 .shutdown = sock_no_shutdown,
930 .setsockopt = sco_sock_setsockopt,
931 .getsockopt = sco_sock_getsockopt
934 static struct net_proto_family sco_sock_family_ops = {
935 .family = PF_BLUETOOTH,
936 .owner = THIS_MODULE,
937 .create = sco_sock_create,
940 static struct hci_proto sco_hci_proto = {
941 .name = "SCO",
942 .id = HCI_PROTO_SCO,
943 .connect_ind = sco_connect_ind,
944 .connect_cfm = sco_connect_cfm,
945 .disconn_ind = sco_disconn_ind,
946 .recv_scodata = sco_recv_scodata
949 static int __init sco_init(void)
951 int err;
953 err = proto_register(&sco_proto, 0);
954 if (err < 0)
955 return err;
957 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
958 if (err < 0) {
959 BT_ERR("SCO socket registration failed");
960 goto error;
963 err = hci_register_proto(&sco_hci_proto);
964 if (err < 0) {
965 BT_ERR("SCO protocol registration failed");
966 bt_sock_unregister(BTPROTO_SCO);
967 goto error;
970 class_create_file(bt_class, &class_attr_sco);
972 BT_INFO("SCO (Voice Link) ver %s", VERSION);
973 BT_INFO("SCO socket layer initialized");
975 return 0;
977 error:
978 proto_unregister(&sco_proto);
979 return err;
982 static void __exit sco_exit(void)
984 class_remove_file(bt_class, &class_attr_sco);
986 if (bt_sock_unregister(BTPROTO_SCO) < 0)
987 BT_ERR("SCO socket unregistration failed");
989 if (hci_unregister_proto(&sco_hci_proto) < 0)
990 BT_ERR("SCO protocol unregistration failed");
992 proto_unregister(&sco_proto);
995 module_init(sco_init);
996 module_exit(sco_exit);
998 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>");
999 MODULE_DESCRIPTION("Bluetooth SCO ver " VERSION);
1000 MODULE_VERSION(VERSION);
1001 MODULE_LICENSE("GPL");
1002 MODULE_ALIAS("bt-proto-2");