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>
28 #include <linux/debugfs.h>
29 #include <linux/seq_file.h>
31 #include <net/bluetooth/bluetooth.h>
32 #include <net/bluetooth/hci_core.h>
33 #include <net/bluetooth/sco.h>
35 static bool disable_esco
;
37 static const struct proto_ops sco_sock_ops
;
39 static struct bt_sock_list sco_sk_list
= {
40 .lock
= __RW_LOCK_UNLOCKED(sco_sk_list
.lock
)
43 static void __sco_chan_add(struct sco_conn
*conn
, struct sock
*sk
, struct sock
*parent
);
44 static void sco_chan_del(struct sock
*sk
, int err
);
46 static void sco_sock_close(struct sock
*sk
);
47 static void sco_sock_kill(struct sock
*sk
);
49 /* ---- SCO timers ---- */
50 static void sco_sock_timeout(unsigned long arg
)
52 struct sock
*sk
= (struct sock
*) arg
;
54 BT_DBG("sock %p state %d", sk
, sk
->sk_state
);
57 sk
->sk_err
= ETIMEDOUT
;
58 sk
->sk_state_change(sk
);
65 static void sco_sock_set_timer(struct sock
*sk
, long timeout
)
67 BT_DBG("sock %p state %d timeout %ld", sk
, sk
->sk_state
, timeout
);
68 sk_reset_timer(sk
, &sk
->sk_timer
, jiffies
+ timeout
);
71 static void sco_sock_clear_timer(struct sock
*sk
)
73 BT_DBG("sock %p state %d", sk
, sk
->sk_state
);
74 sk_stop_timer(sk
, &sk
->sk_timer
);
77 /* ---- SCO connections ---- */
78 static struct sco_conn
*sco_conn_add(struct hci_conn
*hcon
)
80 struct hci_dev
*hdev
= hcon
->hdev
;
81 struct sco_conn
*conn
= hcon
->sco_data
;
86 conn
= kzalloc(sizeof(struct sco_conn
), GFP_KERNEL
);
90 spin_lock_init(&conn
->lock
);
92 hcon
->sco_data
= conn
;
95 conn
->src
= &hdev
->bdaddr
;
96 conn
->dst
= &hcon
->dst
;
98 if (hdev
->sco_mtu
> 0)
99 conn
->mtu
= hdev
->sco_mtu
;
103 BT_DBG("hcon %p conn %p", hcon
, conn
);
108 static struct sock
*sco_chan_get(struct sco_conn
*conn
)
110 struct sock
*sk
= NULL
;
113 sco_conn_unlock(conn
);
117 static int sco_conn_del(struct hci_conn
*hcon
, int err
)
119 struct sco_conn
*conn
= hcon
->sco_data
;
125 BT_DBG("hcon %p conn %p, err %d", hcon
, conn
, err
);
128 sk
= sco_chan_get(conn
);
131 sco_sock_clear_timer(sk
);
132 sco_chan_del(sk
, err
);
137 hcon
->sco_data
= NULL
;
142 static int sco_chan_add(struct sco_conn
*conn
, struct sock
*sk
,
151 __sco_chan_add(conn
, sk
, parent
);
153 sco_conn_unlock(conn
);
157 static int sco_connect(struct sock
*sk
)
159 bdaddr_t
*src
= &bt_sk(sk
)->src
;
160 bdaddr_t
*dst
= &bt_sk(sk
)->dst
;
161 struct sco_conn
*conn
;
162 struct hci_conn
*hcon
;
163 struct hci_dev
*hdev
;
166 BT_DBG("%pMR -> %pMR", src
, dst
);
168 hdev
= hci_get_route(dst
, src
);
170 return -EHOSTUNREACH
;
174 if (lmp_esco_capable(hdev
) && !disable_esco
)
179 if (sco_pi(sk
)->setting
== BT_VOICE_TRANSPARENT
&&
180 (!lmp_transp_capable(hdev
) || !lmp_esco_capable(hdev
))) {
185 hcon
= hci_connect_sco(hdev
, type
, dst
, sco_pi(sk
)->setting
);
191 conn
= sco_conn_add(hcon
);
198 /* Update source addr of the socket */
199 bacpy(src
, conn
->src
);
201 err
= sco_chan_add(conn
, sk
, NULL
);
205 if (hcon
->state
== BT_CONNECTED
) {
206 sco_sock_clear_timer(sk
);
207 sk
->sk_state
= BT_CONNECTED
;
209 sk
->sk_state
= BT_CONNECT
;
210 sco_sock_set_timer(sk
, sk
->sk_sndtimeo
);
214 hci_dev_unlock(hdev
);
219 static int sco_send_frame(struct sock
*sk
, struct msghdr
*msg
, int len
)
221 struct sco_conn
*conn
= sco_pi(sk
)->conn
;
225 /* Check outgoing MTU */
229 BT_DBG("sk %p len %d", sk
, len
);
231 skb
= bt_skb_send_alloc(sk
, len
, msg
->msg_flags
& MSG_DONTWAIT
, &err
);
235 if (memcpy_fromiovec(skb_put(skb
, len
), msg
->msg_iov
, len
)) {
240 hci_send_sco(conn
->hcon
, skb
);
245 static void sco_recv_frame(struct sco_conn
*conn
, struct sk_buff
*skb
)
247 struct sock
*sk
= sco_chan_get(conn
);
252 BT_DBG("sk %p len %d", sk
, skb
->len
);
254 if (sk
->sk_state
!= BT_CONNECTED
)
257 if (!sock_queue_rcv_skb(sk
, skb
))
264 /* -------- Socket interface ---------- */
265 static struct sock
*__sco_get_sock_listen_by_addr(bdaddr_t
*ba
)
269 sk_for_each(sk
, &sco_sk_list
.head
) {
270 if (sk
->sk_state
!= BT_LISTEN
)
273 if (!bacmp(&bt_sk(sk
)->src
, ba
))
280 /* Find socket listening on source bdaddr.
281 * Returns closest match.
283 static struct sock
*sco_get_sock_listen(bdaddr_t
*src
)
285 struct sock
*sk
= NULL
, *sk1
= NULL
;
287 read_lock(&sco_sk_list
.lock
);
289 sk_for_each(sk
, &sco_sk_list
.head
) {
290 if (sk
->sk_state
!= BT_LISTEN
)
294 if (!bacmp(&bt_sk(sk
)->src
, src
))
298 if (!bacmp(&bt_sk(sk
)->src
, BDADDR_ANY
))
302 read_unlock(&sco_sk_list
.lock
);
304 return sk
? sk
: sk1
;
307 static void sco_sock_destruct(struct sock
*sk
)
311 skb_queue_purge(&sk
->sk_receive_queue
);
312 skb_queue_purge(&sk
->sk_write_queue
);
315 static void sco_sock_cleanup_listen(struct sock
*parent
)
319 BT_DBG("parent %p", parent
);
321 /* Close not yet accepted channels */
322 while ((sk
= bt_accept_dequeue(parent
, NULL
))) {
327 parent
->sk_state
= BT_CLOSED
;
328 sock_set_flag(parent
, SOCK_ZAPPED
);
331 /* Kill socket (only if zapped and orphan)
332 * Must be called on unlocked socket.
334 static void sco_sock_kill(struct sock
*sk
)
336 if (!sock_flag(sk
, SOCK_ZAPPED
) || sk
->sk_socket
)
339 BT_DBG("sk %p state %d", sk
, sk
->sk_state
);
341 /* Kill poor orphan */
342 bt_sock_unlink(&sco_sk_list
, sk
);
343 sock_set_flag(sk
, SOCK_DEAD
);
347 static void __sco_sock_close(struct sock
*sk
)
349 BT_DBG("sk %p state %d socket %p", sk
, sk
->sk_state
, sk
->sk_socket
);
351 switch (sk
->sk_state
) {
353 sco_sock_cleanup_listen(sk
);
358 if (sco_pi(sk
)->conn
->hcon
) {
359 sk
->sk_state
= BT_DISCONN
;
360 sco_sock_set_timer(sk
, SCO_DISCONN_TIMEOUT
);
361 hci_conn_drop(sco_pi(sk
)->conn
->hcon
);
362 sco_pi(sk
)->conn
->hcon
= NULL
;
364 sco_chan_del(sk
, ECONNRESET
);
370 sco_chan_del(sk
, ECONNRESET
);
374 sock_set_flag(sk
, SOCK_ZAPPED
);
379 /* Must be called on unlocked socket. */
380 static void sco_sock_close(struct sock
*sk
)
382 sco_sock_clear_timer(sk
);
384 __sco_sock_close(sk
);
389 static void sco_sock_init(struct sock
*sk
, struct sock
*parent
)
394 sk
->sk_type
= parent
->sk_type
;
395 bt_sk(sk
)->flags
= bt_sk(parent
)->flags
;
396 security_sk_clone(parent
, sk
);
400 static struct proto sco_proto
= {
402 .owner
= THIS_MODULE
,
403 .obj_size
= sizeof(struct sco_pinfo
)
406 static struct sock
*sco_sock_alloc(struct net
*net
, struct socket
*sock
, int proto
, gfp_t prio
)
410 sk
= sk_alloc(net
, PF_BLUETOOTH
, prio
, &sco_proto
);
414 sock_init_data(sock
, sk
);
415 INIT_LIST_HEAD(&bt_sk(sk
)->accept_q
);
417 sk
->sk_destruct
= sco_sock_destruct
;
418 sk
->sk_sndtimeo
= SCO_CONN_TIMEOUT
;
420 sock_reset_flag(sk
, SOCK_ZAPPED
);
422 sk
->sk_protocol
= proto
;
423 sk
->sk_state
= BT_OPEN
;
425 sco_pi(sk
)->setting
= BT_VOICE_CVSD_16BIT
;
427 setup_timer(&sk
->sk_timer
, sco_sock_timeout
, (unsigned long)sk
);
429 bt_sock_link(&sco_sk_list
, sk
);
433 static int sco_sock_create(struct net
*net
, struct socket
*sock
, int protocol
,
438 BT_DBG("sock %p", sock
);
440 sock
->state
= SS_UNCONNECTED
;
442 if (sock
->type
!= SOCK_SEQPACKET
)
443 return -ESOCKTNOSUPPORT
;
445 sock
->ops
= &sco_sock_ops
;
447 sk
= sco_sock_alloc(net
, sock
, protocol
, GFP_ATOMIC
);
451 sco_sock_init(sk
, NULL
);
455 static int sco_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
457 struct sockaddr_sco
*sa
= (struct sockaddr_sco
*) addr
;
458 struct sock
*sk
= sock
->sk
;
461 BT_DBG("sk %p %pMR", sk
, &sa
->sco_bdaddr
);
463 if (!addr
|| addr
->sa_family
!= AF_BLUETOOTH
)
468 if (sk
->sk_state
!= BT_OPEN
) {
473 if (sk
->sk_type
!= SOCK_SEQPACKET
) {
478 bacpy(&bt_sk(sk
)->src
, &sa
->sco_bdaddr
);
480 sk
->sk_state
= BT_BOUND
;
487 static int sco_sock_connect(struct socket
*sock
, struct sockaddr
*addr
, int alen
, int flags
)
489 struct sockaddr_sco
*sa
= (struct sockaddr_sco
*) addr
;
490 struct sock
*sk
= sock
->sk
;
495 if (alen
< sizeof(struct sockaddr_sco
) ||
496 addr
->sa_family
!= AF_BLUETOOTH
)
499 if (sk
->sk_state
!= BT_OPEN
&& sk
->sk_state
!= BT_BOUND
)
502 if (sk
->sk_type
!= SOCK_SEQPACKET
)
507 /* Set destination address and psm */
508 bacpy(&bt_sk(sk
)->dst
, &sa
->sco_bdaddr
);
510 err
= sco_connect(sk
);
514 err
= bt_sock_wait_state(sk
, BT_CONNECTED
,
515 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
522 static int sco_sock_listen(struct socket
*sock
, int backlog
)
524 struct sock
*sk
= sock
->sk
;
525 bdaddr_t
*src
= &bt_sk(sk
)->src
;
528 BT_DBG("sk %p backlog %d", sk
, backlog
);
532 if (sk
->sk_state
!= BT_BOUND
) {
537 if (sk
->sk_type
!= SOCK_SEQPACKET
) {
542 write_lock(&sco_sk_list
.lock
);
544 if (__sco_get_sock_listen_by_addr(src
)) {
549 sk
->sk_max_ack_backlog
= backlog
;
550 sk
->sk_ack_backlog
= 0;
552 sk
->sk_state
= BT_LISTEN
;
555 write_unlock(&sco_sk_list
.lock
);
562 static int sco_sock_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
564 DECLARE_WAITQUEUE(wait
, current
);
565 struct sock
*sk
= sock
->sk
, *ch
;
571 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
573 BT_DBG("sk %p timeo %ld", sk
, timeo
);
575 /* Wait for an incoming connection. (wake-one). */
576 add_wait_queue_exclusive(sk_sleep(sk
), &wait
);
578 set_current_state(TASK_INTERRUPTIBLE
);
580 if (sk
->sk_state
!= BT_LISTEN
) {
585 ch
= bt_accept_dequeue(sk
, newsock
);
594 if (signal_pending(current
)) {
595 err
= sock_intr_errno(timeo
);
600 timeo
= schedule_timeout(timeo
);
603 __set_current_state(TASK_RUNNING
);
604 remove_wait_queue(sk_sleep(sk
), &wait
);
609 newsock
->state
= SS_CONNECTED
;
611 BT_DBG("new socket %p", ch
);
618 static int sco_sock_getname(struct socket
*sock
, struct sockaddr
*addr
, int *len
, int peer
)
620 struct sockaddr_sco
*sa
= (struct sockaddr_sco
*) addr
;
621 struct sock
*sk
= sock
->sk
;
623 BT_DBG("sock %p, sk %p", sock
, sk
);
625 addr
->sa_family
= AF_BLUETOOTH
;
626 *len
= sizeof(struct sockaddr_sco
);
629 bacpy(&sa
->sco_bdaddr
, &bt_sk(sk
)->dst
);
631 bacpy(&sa
->sco_bdaddr
, &bt_sk(sk
)->src
);
636 static int sco_sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
637 struct msghdr
*msg
, size_t len
)
639 struct sock
*sk
= sock
->sk
;
642 BT_DBG("sock %p, sk %p", sock
, sk
);
644 err
= sock_error(sk
);
648 if (msg
->msg_flags
& MSG_OOB
)
653 if (sk
->sk_state
== BT_CONNECTED
)
654 err
= sco_send_frame(sk
, msg
, len
);
662 static void sco_conn_defer_accept(struct hci_conn
*conn
, u16 setting
)
664 struct hci_dev
*hdev
= conn
->hdev
;
666 BT_DBG("conn %p", conn
);
668 conn
->state
= BT_CONFIG
;
670 if (!lmp_esco_capable(hdev
)) {
671 struct hci_cp_accept_conn_req cp
;
673 bacpy(&cp
.bdaddr
, &conn
->dst
);
674 cp
.role
= 0x00; /* Ignored */
676 hci_send_cmd(hdev
, HCI_OP_ACCEPT_CONN_REQ
, sizeof(cp
), &cp
);
678 struct hci_cp_accept_sync_conn_req cp
;
680 bacpy(&cp
.bdaddr
, &conn
->dst
);
681 cp
.pkt_type
= cpu_to_le16(conn
->pkt_type
);
683 cp
.tx_bandwidth
= __constant_cpu_to_le32(0x00001f40);
684 cp
.rx_bandwidth
= __constant_cpu_to_le32(0x00001f40);
685 cp
.content_format
= cpu_to_le16(setting
);
687 switch (setting
& SCO_AIRMODE_MASK
) {
688 case SCO_AIRMODE_TRANSP
:
689 if (conn
->pkt_type
& ESCO_2EV3
)
690 cp
.max_latency
= __constant_cpu_to_le16(0x0008);
692 cp
.max_latency
= __constant_cpu_to_le16(0x000D);
693 cp
.retrans_effort
= 0x02;
695 case SCO_AIRMODE_CVSD
:
696 cp
.max_latency
= __constant_cpu_to_le16(0xffff);
697 cp
.retrans_effort
= 0xff;
701 hci_send_cmd(hdev
, HCI_OP_ACCEPT_SYNC_CONN_REQ
,
706 static int sco_sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
707 struct msghdr
*msg
, size_t len
, int flags
)
709 struct sock
*sk
= sock
->sk
;
710 struct sco_pinfo
*pi
= sco_pi(sk
);
714 if (sk
->sk_state
== BT_CONNECT2
&&
715 test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
)) {
716 sco_conn_defer_accept(pi
->conn
->hcon
, pi
->setting
);
717 sk
->sk_state
= BT_CONFIG
;
725 return bt_sock_recvmsg(iocb
, sock
, msg
, len
, flags
);
728 static int sco_sock_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, unsigned int optlen
)
730 struct sock
*sk
= sock
->sk
;
732 struct bt_voice voice
;
742 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
747 if (get_user(opt
, (u32 __user
*) optval
)) {
753 set_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
);
755 clear_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
);
759 if (sk
->sk_state
!= BT_OPEN
&& sk
->sk_state
!= BT_BOUND
&&
760 sk
->sk_state
!= BT_CONNECT2
) {
765 voice
.setting
= sco_pi(sk
)->setting
;
767 len
= min_t(unsigned int, sizeof(voice
), optlen
);
768 if (copy_from_user((char *) &voice
, optval
, len
)) {
773 /* Explicitly check for these values */
774 if (voice
.setting
!= BT_VOICE_TRANSPARENT
&&
775 voice
.setting
!= BT_VOICE_CVSD_16BIT
) {
780 sco_pi(sk
)->setting
= voice
.setting
;
792 static int sco_sock_getsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, int __user
*optlen
)
794 struct sock
*sk
= sock
->sk
;
795 struct sco_options opts
;
796 struct sco_conninfo cinfo
;
801 if (get_user(len
, optlen
))
808 if (sk
->sk_state
!= BT_CONNECTED
&&
809 !(sk
->sk_state
== BT_CONNECT2
&&
810 test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
))) {
815 opts
.mtu
= sco_pi(sk
)->conn
->mtu
;
817 BT_DBG("mtu %d", opts
.mtu
);
819 len
= min_t(unsigned int, len
, sizeof(opts
));
820 if (copy_to_user(optval
, (char *)&opts
, len
))
826 if (sk
->sk_state
!= BT_CONNECTED
&&
827 !(sk
->sk_state
== BT_CONNECT2
&&
828 test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
))) {
833 memset(&cinfo
, 0, sizeof(cinfo
));
834 cinfo
.hci_handle
= sco_pi(sk
)->conn
->hcon
->handle
;
835 memcpy(cinfo
.dev_class
, sco_pi(sk
)->conn
->hcon
->dev_class
, 3);
837 len
= min_t(unsigned int, len
, sizeof(cinfo
));
838 if (copy_to_user(optval
, (char *)&cinfo
, len
))
852 static int sco_sock_getsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int __user
*optlen
)
854 struct sock
*sk
= sock
->sk
;
856 struct bt_voice voice
;
860 if (level
== SOL_SCO
)
861 return sco_sock_getsockopt_old(sock
, optname
, optval
, optlen
);
863 if (get_user(len
, optlen
))
871 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
876 if (put_user(test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
),
877 (u32 __user
*) optval
))
883 voice
.setting
= sco_pi(sk
)->setting
;
885 len
= min_t(unsigned int, len
, sizeof(voice
));
886 if (copy_to_user(optval
, (char *)&voice
, len
))
900 static int sco_sock_shutdown(struct socket
*sock
, int how
)
902 struct sock
*sk
= sock
->sk
;
905 BT_DBG("sock %p, sk %p", sock
, sk
);
911 if (!sk
->sk_shutdown
) {
912 sk
->sk_shutdown
= SHUTDOWN_MASK
;
913 sco_sock_clear_timer(sk
);
914 __sco_sock_close(sk
);
916 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
)
917 err
= bt_sock_wait_state(sk
, BT_CLOSED
,
924 static int sco_sock_release(struct socket
*sock
)
926 struct sock
*sk
= sock
->sk
;
929 BT_DBG("sock %p, sk %p", sock
, sk
);
936 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
) {
938 err
= bt_sock_wait_state(sk
, BT_CLOSED
, sk
->sk_lingertime
);
947 static void __sco_chan_add(struct sco_conn
*conn
, struct sock
*sk
, struct sock
*parent
)
949 BT_DBG("conn %p", conn
);
951 sco_pi(sk
)->conn
= conn
;
955 bt_accept_enqueue(parent
, sk
);
959 * Must be called on the locked socket. */
960 static void sco_chan_del(struct sock
*sk
, int err
)
962 struct sco_conn
*conn
;
964 conn
= sco_pi(sk
)->conn
;
966 BT_DBG("sk %p, conn %p, err %d", sk
, conn
, err
);
971 sco_pi(sk
)->conn
= NULL
;
972 sco_conn_unlock(conn
);
975 hci_conn_drop(conn
->hcon
);
978 sk
->sk_state
= BT_CLOSED
;
980 sk
->sk_state_change(sk
);
982 sock_set_flag(sk
, SOCK_ZAPPED
);
985 static void sco_conn_ready(struct sco_conn
*conn
)
988 struct sock
*sk
= conn
->sk
;
990 BT_DBG("conn %p", conn
);
993 sco_sock_clear_timer(sk
);
995 sk
->sk_state
= BT_CONNECTED
;
996 sk
->sk_state_change(sk
);
1001 parent
= sco_get_sock_listen(conn
->src
);
1003 sco_conn_unlock(conn
);
1007 bh_lock_sock(parent
);
1009 sk
= sco_sock_alloc(sock_net(parent
), NULL
,
1010 BTPROTO_SCO
, GFP_ATOMIC
);
1012 bh_unlock_sock(parent
);
1013 sco_conn_unlock(conn
);
1017 sco_sock_init(sk
, parent
);
1019 bacpy(&bt_sk(sk
)->src
, conn
->src
);
1020 bacpy(&bt_sk(sk
)->dst
, conn
->dst
);
1022 hci_conn_hold(conn
->hcon
);
1023 __sco_chan_add(conn
, sk
, parent
);
1025 if (test_bit(BT_SK_DEFER_SETUP
, &bt_sk(parent
)->flags
))
1026 sk
->sk_state
= BT_CONNECT2
;
1028 sk
->sk_state
= BT_CONNECTED
;
1030 /* Wake up parent */
1031 parent
->sk_data_ready(parent
, 1);
1033 bh_unlock_sock(parent
);
1035 sco_conn_unlock(conn
);
1039 /* ----- SCO interface with lower layer (HCI) ----- */
1040 int sco_connect_ind(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, __u8
*flags
)
1045 BT_DBG("hdev %s, bdaddr %pMR", hdev
->name
, bdaddr
);
1047 /* Find listening sockets */
1048 read_lock(&sco_sk_list
.lock
);
1049 sk_for_each(sk
, &sco_sk_list
.head
) {
1050 if (sk
->sk_state
!= BT_LISTEN
)
1053 if (!bacmp(&bt_sk(sk
)->src
, &hdev
->bdaddr
) ||
1054 !bacmp(&bt_sk(sk
)->src
, BDADDR_ANY
)) {
1055 lm
|= HCI_LM_ACCEPT
;
1057 if (test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
))
1058 *flags
|= HCI_PROTO_DEFER
;
1062 read_unlock(&sco_sk_list
.lock
);
1067 void sco_connect_cfm(struct hci_conn
*hcon
, __u8 status
)
1069 BT_DBG("hcon %p bdaddr %pMR status %d", hcon
, &hcon
->dst
, status
);
1071 struct sco_conn
*conn
;
1073 conn
= sco_conn_add(hcon
);
1075 sco_conn_ready(conn
);
1077 sco_conn_del(hcon
, bt_to_errno(status
));
1080 void sco_disconn_cfm(struct hci_conn
*hcon
, __u8 reason
)
1082 BT_DBG("hcon %p reason %d", hcon
, reason
);
1084 sco_conn_del(hcon
, bt_to_errno(reason
));
1087 int sco_recv_scodata(struct hci_conn
*hcon
, struct sk_buff
*skb
)
1089 struct sco_conn
*conn
= hcon
->sco_data
;
1094 BT_DBG("conn %p len %d", conn
, skb
->len
);
1097 sco_recv_frame(conn
, skb
);
1106 static int sco_debugfs_show(struct seq_file
*f
, void *p
)
1110 read_lock(&sco_sk_list
.lock
);
1112 sk_for_each(sk
, &sco_sk_list
.head
) {
1113 seq_printf(f
, "%pMR %pMR %d\n", &bt_sk(sk
)->src
,
1114 &bt_sk(sk
)->dst
, sk
->sk_state
);
1117 read_unlock(&sco_sk_list
.lock
);
1122 static int sco_debugfs_open(struct inode
*inode
, struct file
*file
)
1124 return single_open(file
, sco_debugfs_show
, inode
->i_private
);
1127 static const struct file_operations sco_debugfs_fops
= {
1128 .open
= sco_debugfs_open
,
1130 .llseek
= seq_lseek
,
1131 .release
= single_release
,
1134 static struct dentry
*sco_debugfs
;
1136 static const struct proto_ops sco_sock_ops
= {
1137 .family
= PF_BLUETOOTH
,
1138 .owner
= THIS_MODULE
,
1139 .release
= sco_sock_release
,
1140 .bind
= sco_sock_bind
,
1141 .connect
= sco_sock_connect
,
1142 .listen
= sco_sock_listen
,
1143 .accept
= sco_sock_accept
,
1144 .getname
= sco_sock_getname
,
1145 .sendmsg
= sco_sock_sendmsg
,
1146 .recvmsg
= sco_sock_recvmsg
,
1147 .poll
= bt_sock_poll
,
1148 .ioctl
= bt_sock_ioctl
,
1149 .mmap
= sock_no_mmap
,
1150 .socketpair
= sock_no_socketpair
,
1151 .shutdown
= sco_sock_shutdown
,
1152 .setsockopt
= sco_sock_setsockopt
,
1153 .getsockopt
= sco_sock_getsockopt
1156 static const struct net_proto_family sco_sock_family_ops
= {
1157 .family
= PF_BLUETOOTH
,
1158 .owner
= THIS_MODULE
,
1159 .create
= sco_sock_create
,
1162 int __init
sco_init(void)
1166 err
= proto_register(&sco_proto
, 0);
1170 err
= bt_sock_register(BTPROTO_SCO
, &sco_sock_family_ops
);
1172 BT_ERR("SCO socket registration failed");
1176 err
= bt_procfs_init(&init_net
, "sco", &sco_sk_list
, NULL
);
1178 BT_ERR("Failed to create SCO proc file");
1179 bt_sock_unregister(BTPROTO_SCO
);
1184 sco_debugfs
= debugfs_create_file("sco", 0444, bt_debugfs
,
1185 NULL
, &sco_debugfs_fops
);
1187 BT_ERR("Failed to create SCO debug file");
1190 BT_INFO("SCO socket layer initialized");
1195 proto_unregister(&sco_proto
);
1199 void __exit
sco_exit(void)
1201 bt_procfs_cleanup(&init_net
, "sco");
1203 debugfs_remove(sco_debugfs
);
1205 bt_sock_unregister(BTPROTO_SCO
);
1207 proto_unregister(&sco_proto
);
1210 module_param(disable_esco
, bool, 0644);
1211 MODULE_PARM_DESC(disable_esco
, "Disable eSCO connection creation");