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 /* ---- SCO connections ---- */
45 struct hci_conn
*hcon
;
53 #define sco_conn_lock(c) spin_lock(&c->lock);
54 #define sco_conn_unlock(c) spin_unlock(&c->lock);
56 static void sco_sock_close(struct sock
*sk
);
57 static void sco_sock_kill(struct sock
*sk
);
59 /* ----- SCO socket info ----- */
60 #define sco_pi(sk) ((struct sco_pinfo *) sk)
68 struct sco_conn
*conn
;
71 /* ---- SCO timers ---- */
72 #define SCO_CONN_TIMEOUT (HZ * 40)
73 #define SCO_DISCONN_TIMEOUT (HZ * 2)
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
);
82 sk
->sk_err
= ETIMEDOUT
;
83 sk
->sk_state_change(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 /* ---- SCO connections ---- */
103 static struct sco_conn
*sco_conn_add(struct hci_conn
*hcon
)
105 struct hci_dev
*hdev
= hcon
->hdev
;
106 struct sco_conn
*conn
= hcon
->sco_data
;
111 conn
= kzalloc(sizeof(struct sco_conn
), GFP_KERNEL
);
115 spin_lock_init(&conn
->lock
);
117 hcon
->sco_data
= conn
;
120 if (hdev
->sco_mtu
> 0)
121 conn
->mtu
= hdev
->sco_mtu
;
125 BT_DBG("hcon %p conn %p", hcon
, conn
);
131 * Must be called on the locked socket. */
132 static void sco_chan_del(struct sock
*sk
, int err
)
134 struct sco_conn
*conn
;
136 conn
= sco_pi(sk
)->conn
;
138 BT_DBG("sk %p, conn %p, err %d", sk
, conn
, err
);
143 sco_pi(sk
)->conn
= NULL
;
144 sco_conn_unlock(conn
);
147 hci_conn_drop(conn
->hcon
);
150 sk
->sk_state
= BT_CLOSED
;
152 sk
->sk_state_change(sk
);
154 sock_set_flag(sk
, SOCK_ZAPPED
);
157 static void sco_conn_del(struct hci_conn
*hcon
, int err
)
159 struct sco_conn
*conn
= hcon
->sco_data
;
165 BT_DBG("hcon %p conn %p, err %d", hcon
, conn
, err
);
170 sco_conn_unlock(conn
);
175 sco_sock_clear_timer(sk
);
176 sco_chan_del(sk
, err
);
182 hcon
->sco_data
= NULL
;
186 static void __sco_chan_add(struct sco_conn
*conn
, struct sock
*sk
,
189 BT_DBG("conn %p", conn
);
191 sco_pi(sk
)->conn
= conn
;
195 bt_accept_enqueue(parent
, sk
);
198 static int sco_chan_add(struct sco_conn
*conn
, struct sock
*sk
,
207 __sco_chan_add(conn
, sk
, parent
);
209 sco_conn_unlock(conn
);
213 static int sco_connect(struct sock
*sk
)
215 struct sco_conn
*conn
;
216 struct hci_conn
*hcon
;
217 struct hci_dev
*hdev
;
220 BT_DBG("%pMR -> %pMR", &sco_pi(sk
)->src
, &sco_pi(sk
)->dst
);
222 hdev
= hci_get_route(&sco_pi(sk
)->dst
, &sco_pi(sk
)->src
);
224 return -EHOSTUNREACH
;
228 if (lmp_esco_capable(hdev
) && !disable_esco
)
233 if (sco_pi(sk
)->setting
== BT_VOICE_TRANSPARENT
&&
234 (!lmp_transp_capable(hdev
) || !lmp_esco_capable(hdev
))) {
239 hcon
= hci_connect_sco(hdev
, type
, &sco_pi(sk
)->dst
,
240 sco_pi(sk
)->setting
);
246 conn
= sco_conn_add(hcon
);
253 /* Update source addr of the socket */
254 bacpy(&sco_pi(sk
)->src
, &hcon
->src
);
256 err
= sco_chan_add(conn
, sk
, NULL
);
260 if (hcon
->state
== BT_CONNECTED
) {
261 sco_sock_clear_timer(sk
);
262 sk
->sk_state
= BT_CONNECTED
;
264 sk
->sk_state
= BT_CONNECT
;
265 sco_sock_set_timer(sk
, sk
->sk_sndtimeo
);
269 hci_dev_unlock(hdev
);
274 static int sco_send_frame(struct sock
*sk
, struct msghdr
*msg
, int len
)
276 struct sco_conn
*conn
= sco_pi(sk
)->conn
;
280 /* Check outgoing MTU */
284 BT_DBG("sk %p len %d", sk
, len
);
286 skb
= bt_skb_send_alloc(sk
, len
, msg
->msg_flags
& MSG_DONTWAIT
, &err
);
290 if (memcpy_from_msg(skb_put(skb
, len
), msg
, len
)) {
295 hci_send_sco(conn
->hcon
, skb
);
300 static void sco_recv_frame(struct sco_conn
*conn
, struct sk_buff
*skb
)
306 sco_conn_unlock(conn
);
311 BT_DBG("sk %p len %d", sk
, skb
->len
);
313 if (sk
->sk_state
!= BT_CONNECTED
)
316 if (!sock_queue_rcv_skb(sk
, skb
))
323 /* -------- Socket interface ---------- */
324 static struct sock
*__sco_get_sock_listen_by_addr(bdaddr_t
*ba
)
328 sk_for_each(sk
, &sco_sk_list
.head
) {
329 if (sk
->sk_state
!= BT_LISTEN
)
332 if (!bacmp(&sco_pi(sk
)->src
, ba
))
339 /* Find socket listening on source bdaddr.
340 * Returns closest match.
342 static struct sock
*sco_get_sock_listen(bdaddr_t
*src
)
344 struct sock
*sk
= NULL
, *sk1
= NULL
;
346 read_lock(&sco_sk_list
.lock
);
348 sk_for_each(sk
, &sco_sk_list
.head
) {
349 if (sk
->sk_state
!= BT_LISTEN
)
353 if (!bacmp(&sco_pi(sk
)->src
, src
))
357 if (!bacmp(&sco_pi(sk
)->src
, BDADDR_ANY
))
361 read_unlock(&sco_sk_list
.lock
);
363 return sk
? sk
: sk1
;
366 static void sco_sock_destruct(struct sock
*sk
)
370 skb_queue_purge(&sk
->sk_receive_queue
);
371 skb_queue_purge(&sk
->sk_write_queue
);
374 static void sco_sock_cleanup_listen(struct sock
*parent
)
378 BT_DBG("parent %p", parent
);
380 /* Close not yet accepted channels */
381 while ((sk
= bt_accept_dequeue(parent
, NULL
))) {
386 parent
->sk_state
= BT_CLOSED
;
387 sock_set_flag(parent
, SOCK_ZAPPED
);
390 /* Kill socket (only if zapped and orphan)
391 * Must be called on unlocked socket.
393 static void sco_sock_kill(struct sock
*sk
)
395 if (!sock_flag(sk
, SOCK_ZAPPED
) || sk
->sk_socket
)
398 BT_DBG("sk %p state %d", sk
, sk
->sk_state
);
400 /* Kill poor orphan */
401 bt_sock_unlink(&sco_sk_list
, sk
);
402 sock_set_flag(sk
, SOCK_DEAD
);
406 static void __sco_sock_close(struct sock
*sk
)
408 BT_DBG("sk %p state %d socket %p", sk
, sk
->sk_state
, sk
->sk_socket
);
410 switch (sk
->sk_state
) {
412 sco_sock_cleanup_listen(sk
);
417 if (sco_pi(sk
)->conn
->hcon
) {
418 sk
->sk_state
= BT_DISCONN
;
419 sco_sock_set_timer(sk
, SCO_DISCONN_TIMEOUT
);
420 sco_conn_lock(sco_pi(sk
)->conn
);
421 hci_conn_drop(sco_pi(sk
)->conn
->hcon
);
422 sco_pi(sk
)->conn
->hcon
= NULL
;
423 sco_conn_unlock(sco_pi(sk
)->conn
);
425 sco_chan_del(sk
, ECONNRESET
);
431 sco_chan_del(sk
, ECONNRESET
);
435 sock_set_flag(sk
, SOCK_ZAPPED
);
440 /* Must be called on unlocked socket. */
441 static void sco_sock_close(struct sock
*sk
)
443 sco_sock_clear_timer(sk
);
445 __sco_sock_close(sk
);
450 static void sco_sock_init(struct sock
*sk
, struct sock
*parent
)
455 sk
->sk_type
= parent
->sk_type
;
456 bt_sk(sk
)->flags
= bt_sk(parent
)->flags
;
457 security_sk_clone(parent
, sk
);
461 static struct proto sco_proto
= {
463 .owner
= THIS_MODULE
,
464 .obj_size
= sizeof(struct sco_pinfo
)
467 static struct sock
*sco_sock_alloc(struct net
*net
, struct socket
*sock
,
468 int proto
, gfp_t prio
, int kern
)
472 sk
= sk_alloc(net
, PF_BLUETOOTH
, prio
, &sco_proto
, kern
);
476 sock_init_data(sock
, sk
);
477 INIT_LIST_HEAD(&bt_sk(sk
)->accept_q
);
479 sk
->sk_destruct
= sco_sock_destruct
;
480 sk
->sk_sndtimeo
= SCO_CONN_TIMEOUT
;
482 sock_reset_flag(sk
, SOCK_ZAPPED
);
484 sk
->sk_protocol
= proto
;
485 sk
->sk_state
= BT_OPEN
;
487 sco_pi(sk
)->setting
= BT_VOICE_CVSD_16BIT
;
489 setup_timer(&sk
->sk_timer
, sco_sock_timeout
, (unsigned long)sk
);
491 bt_sock_link(&sco_sk_list
, sk
);
495 static int sco_sock_create(struct net
*net
, struct socket
*sock
, int protocol
,
500 BT_DBG("sock %p", sock
);
502 sock
->state
= SS_UNCONNECTED
;
504 if (sock
->type
!= SOCK_SEQPACKET
)
505 return -ESOCKTNOSUPPORT
;
507 sock
->ops
= &sco_sock_ops
;
509 sk
= sco_sock_alloc(net
, sock
, protocol
, GFP_ATOMIC
, kern
);
513 sco_sock_init(sk
, NULL
);
517 static int sco_sock_bind(struct socket
*sock
, struct sockaddr
*addr
,
520 struct sockaddr_sco
*sa
= (struct sockaddr_sco
*) addr
;
521 struct sock
*sk
= sock
->sk
;
524 BT_DBG("sk %p %pMR", sk
, &sa
->sco_bdaddr
);
526 if (!addr
|| addr
->sa_family
!= AF_BLUETOOTH
)
529 if (addr_len
< sizeof(struct sockaddr_sco
))
534 if (sk
->sk_state
!= BT_OPEN
) {
539 if (sk
->sk_type
!= SOCK_SEQPACKET
) {
544 bacpy(&sco_pi(sk
)->src
, &sa
->sco_bdaddr
);
546 sk
->sk_state
= BT_BOUND
;
553 static int sco_sock_connect(struct socket
*sock
, struct sockaddr
*addr
, int alen
, int flags
)
555 struct sockaddr_sco
*sa
= (struct sockaddr_sco
*) addr
;
556 struct sock
*sk
= sock
->sk
;
561 if (alen
< sizeof(struct sockaddr_sco
) ||
562 addr
->sa_family
!= AF_BLUETOOTH
)
565 if (sk
->sk_state
!= BT_OPEN
&& sk
->sk_state
!= BT_BOUND
)
568 if (sk
->sk_type
!= SOCK_SEQPACKET
)
573 /* Set destination address and psm */
574 bacpy(&sco_pi(sk
)->dst
, &sa
->sco_bdaddr
);
576 err
= sco_connect(sk
);
580 err
= bt_sock_wait_state(sk
, BT_CONNECTED
,
581 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
588 static int sco_sock_listen(struct socket
*sock
, int backlog
)
590 struct sock
*sk
= sock
->sk
;
591 bdaddr_t
*src
= &sco_pi(sk
)->src
;
594 BT_DBG("sk %p backlog %d", sk
, backlog
);
598 if (sk
->sk_state
!= BT_BOUND
) {
603 if (sk
->sk_type
!= SOCK_SEQPACKET
) {
608 write_lock(&sco_sk_list
.lock
);
610 if (__sco_get_sock_listen_by_addr(src
)) {
615 sk
->sk_max_ack_backlog
= backlog
;
616 sk
->sk_ack_backlog
= 0;
618 sk
->sk_state
= BT_LISTEN
;
621 write_unlock(&sco_sk_list
.lock
);
628 static int sco_sock_accept(struct socket
*sock
, struct socket
*newsock
,
631 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
632 struct sock
*sk
= sock
->sk
, *ch
;
638 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
640 BT_DBG("sk %p timeo %ld", sk
, timeo
);
642 /* Wait for an incoming connection. (wake-one). */
643 add_wait_queue_exclusive(sk_sleep(sk
), &wait
);
645 if (sk
->sk_state
!= BT_LISTEN
) {
650 ch
= bt_accept_dequeue(sk
, newsock
);
659 if (signal_pending(current
)) {
660 err
= sock_intr_errno(timeo
);
666 timeo
= wait_woken(&wait
, TASK_INTERRUPTIBLE
, timeo
);
669 remove_wait_queue(sk_sleep(sk
), &wait
);
674 newsock
->state
= SS_CONNECTED
;
676 BT_DBG("new socket %p", ch
);
683 static int sco_sock_getname(struct socket
*sock
, struct sockaddr
*addr
,
686 struct sockaddr_sco
*sa
= (struct sockaddr_sco
*) addr
;
687 struct sock
*sk
= sock
->sk
;
689 BT_DBG("sock %p, sk %p", sock
, sk
);
691 addr
->sa_family
= AF_BLUETOOTH
;
692 *len
= sizeof(struct sockaddr_sco
);
695 bacpy(&sa
->sco_bdaddr
, &sco_pi(sk
)->dst
);
697 bacpy(&sa
->sco_bdaddr
, &sco_pi(sk
)->src
);
702 static int sco_sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
705 struct sock
*sk
= sock
->sk
;
708 BT_DBG("sock %p, sk %p", sock
, sk
);
710 err
= sock_error(sk
);
714 if (msg
->msg_flags
& MSG_OOB
)
719 if (sk
->sk_state
== BT_CONNECTED
)
720 err
= sco_send_frame(sk
, msg
, len
);
728 static void sco_conn_defer_accept(struct hci_conn
*conn
, u16 setting
)
730 struct hci_dev
*hdev
= conn
->hdev
;
732 BT_DBG("conn %p", conn
);
734 conn
->state
= BT_CONFIG
;
736 if (!lmp_esco_capable(hdev
)) {
737 struct hci_cp_accept_conn_req cp
;
739 bacpy(&cp
.bdaddr
, &conn
->dst
);
740 cp
.role
= 0x00; /* Ignored */
742 hci_send_cmd(hdev
, HCI_OP_ACCEPT_CONN_REQ
, sizeof(cp
), &cp
);
744 struct hci_cp_accept_sync_conn_req cp
;
746 bacpy(&cp
.bdaddr
, &conn
->dst
);
747 cp
.pkt_type
= cpu_to_le16(conn
->pkt_type
);
749 cp
.tx_bandwidth
= cpu_to_le32(0x00001f40);
750 cp
.rx_bandwidth
= cpu_to_le32(0x00001f40);
751 cp
.content_format
= cpu_to_le16(setting
);
753 switch (setting
& SCO_AIRMODE_MASK
) {
754 case SCO_AIRMODE_TRANSP
:
755 if (conn
->pkt_type
& ESCO_2EV3
)
756 cp
.max_latency
= cpu_to_le16(0x0008);
758 cp
.max_latency
= cpu_to_le16(0x000D);
759 cp
.retrans_effort
= 0x02;
761 case SCO_AIRMODE_CVSD
:
762 cp
.max_latency
= cpu_to_le16(0xffff);
763 cp
.retrans_effort
= 0xff;
767 hci_send_cmd(hdev
, HCI_OP_ACCEPT_SYNC_CONN_REQ
,
772 static int sco_sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
773 size_t len
, int flags
)
775 struct sock
*sk
= sock
->sk
;
776 struct sco_pinfo
*pi
= sco_pi(sk
);
780 if (sk
->sk_state
== BT_CONNECT2
&&
781 test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
)) {
782 sco_conn_defer_accept(pi
->conn
->hcon
, pi
->setting
);
783 sk
->sk_state
= BT_CONFIG
;
791 return bt_sock_recvmsg(sock
, msg
, len
, flags
);
794 static int sco_sock_setsockopt(struct socket
*sock
, int level
, int optname
,
795 char __user
*optval
, unsigned int optlen
)
797 struct sock
*sk
= sock
->sk
;
799 struct bt_voice voice
;
809 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
814 if (get_user(opt
, (u32 __user
*) optval
)) {
820 set_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
);
822 clear_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
);
826 if (sk
->sk_state
!= BT_OPEN
&& sk
->sk_state
!= BT_BOUND
&&
827 sk
->sk_state
!= BT_CONNECT2
) {
832 voice
.setting
= sco_pi(sk
)->setting
;
834 len
= min_t(unsigned int, sizeof(voice
), optlen
);
835 if (copy_from_user((char *)&voice
, optval
, len
)) {
840 /* Explicitly check for these values */
841 if (voice
.setting
!= BT_VOICE_TRANSPARENT
&&
842 voice
.setting
!= BT_VOICE_CVSD_16BIT
) {
847 sco_pi(sk
)->setting
= voice
.setting
;
859 static int sco_sock_getsockopt_old(struct socket
*sock
, int optname
,
860 char __user
*optval
, int __user
*optlen
)
862 struct sock
*sk
= sock
->sk
;
863 struct sco_options opts
;
864 struct sco_conninfo cinfo
;
869 if (get_user(len
, optlen
))
876 if (sk
->sk_state
!= BT_CONNECTED
&&
877 !(sk
->sk_state
== BT_CONNECT2
&&
878 test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
))) {
883 opts
.mtu
= sco_pi(sk
)->conn
->mtu
;
885 BT_DBG("mtu %d", opts
.mtu
);
887 len
= min_t(unsigned int, len
, sizeof(opts
));
888 if (copy_to_user(optval
, (char *)&opts
, len
))
894 if (sk
->sk_state
!= BT_CONNECTED
&&
895 !(sk
->sk_state
== BT_CONNECT2
&&
896 test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
))) {
901 memset(&cinfo
, 0, sizeof(cinfo
));
902 cinfo
.hci_handle
= sco_pi(sk
)->conn
->hcon
->handle
;
903 memcpy(cinfo
.dev_class
, sco_pi(sk
)->conn
->hcon
->dev_class
, 3);
905 len
= min_t(unsigned int, len
, sizeof(cinfo
));
906 if (copy_to_user(optval
, (char *)&cinfo
, len
))
920 static int sco_sock_getsockopt(struct socket
*sock
, int level
, int optname
,
921 char __user
*optval
, int __user
*optlen
)
923 struct sock
*sk
= sock
->sk
;
925 struct bt_voice voice
;
929 if (level
== SOL_SCO
)
930 return sco_sock_getsockopt_old(sock
, optname
, optval
, optlen
);
932 if (get_user(len
, optlen
))
940 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
945 if (put_user(test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
),
946 (u32 __user
*)optval
))
952 voice
.setting
= sco_pi(sk
)->setting
;
954 len
= min_t(unsigned int, len
, sizeof(voice
));
955 if (copy_to_user(optval
, (char *)&voice
, len
))
969 static int sco_sock_shutdown(struct socket
*sock
, int how
)
971 struct sock
*sk
= sock
->sk
;
974 BT_DBG("sock %p, sk %p", sock
, sk
);
982 if (!sk
->sk_shutdown
) {
983 sk
->sk_shutdown
= SHUTDOWN_MASK
;
984 sco_sock_clear_timer(sk
);
985 __sco_sock_close(sk
);
987 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
&&
988 !(current
->flags
& PF_EXITING
))
989 err
= bt_sock_wait_state(sk
, BT_CLOSED
,
999 static int sco_sock_release(struct socket
*sock
)
1001 struct sock
*sk
= sock
->sk
;
1004 BT_DBG("sock %p, sk %p", sock
, sk
);
1011 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
&&
1012 !(current
->flags
& PF_EXITING
)) {
1014 err
= bt_sock_wait_state(sk
, BT_CLOSED
, sk
->sk_lingertime
);
1023 static void sco_conn_ready(struct sco_conn
*conn
)
1025 struct sock
*parent
;
1026 struct sock
*sk
= conn
->sk
;
1028 BT_DBG("conn %p", conn
);
1031 sco_sock_clear_timer(sk
);
1033 sk
->sk_state
= BT_CONNECTED
;
1034 sk
->sk_state_change(sk
);
1037 sco_conn_lock(conn
);
1040 sco_conn_unlock(conn
);
1044 parent
= sco_get_sock_listen(&conn
->hcon
->src
);
1046 sco_conn_unlock(conn
);
1050 bh_lock_sock(parent
);
1052 sk
= sco_sock_alloc(sock_net(parent
), NULL
,
1053 BTPROTO_SCO
, GFP_ATOMIC
, 0);
1055 bh_unlock_sock(parent
);
1056 sco_conn_unlock(conn
);
1060 sco_sock_init(sk
, parent
);
1062 bacpy(&sco_pi(sk
)->src
, &conn
->hcon
->src
);
1063 bacpy(&sco_pi(sk
)->dst
, &conn
->hcon
->dst
);
1065 hci_conn_hold(conn
->hcon
);
1066 __sco_chan_add(conn
, sk
, parent
);
1068 if (test_bit(BT_SK_DEFER_SETUP
, &bt_sk(parent
)->flags
))
1069 sk
->sk_state
= BT_CONNECT2
;
1071 sk
->sk_state
= BT_CONNECTED
;
1073 /* Wake up parent */
1074 parent
->sk_data_ready(parent
);
1076 bh_unlock_sock(parent
);
1078 sco_conn_unlock(conn
);
1082 /* ----- SCO interface with lower layer (HCI) ----- */
1083 int sco_connect_ind(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, __u8
*flags
)
1088 BT_DBG("hdev %s, bdaddr %pMR", hdev
->name
, bdaddr
);
1090 /* Find listening sockets */
1091 read_lock(&sco_sk_list
.lock
);
1092 sk_for_each(sk
, &sco_sk_list
.head
) {
1093 if (sk
->sk_state
!= BT_LISTEN
)
1096 if (!bacmp(&sco_pi(sk
)->src
, &hdev
->bdaddr
) ||
1097 !bacmp(&sco_pi(sk
)->src
, BDADDR_ANY
)) {
1098 lm
|= HCI_LM_ACCEPT
;
1100 if (test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
))
1101 *flags
|= HCI_PROTO_DEFER
;
1105 read_unlock(&sco_sk_list
.lock
);
1110 static void sco_connect_cfm(struct hci_conn
*hcon
, __u8 status
)
1112 if (hcon
->type
!= SCO_LINK
&& hcon
->type
!= ESCO_LINK
)
1115 BT_DBG("hcon %p bdaddr %pMR status %d", hcon
, &hcon
->dst
, status
);
1118 struct sco_conn
*conn
;
1120 conn
= sco_conn_add(hcon
);
1122 sco_conn_ready(conn
);
1124 sco_conn_del(hcon
, bt_to_errno(status
));
1127 static void sco_disconn_cfm(struct hci_conn
*hcon
, __u8 reason
)
1129 if (hcon
->type
!= SCO_LINK
&& hcon
->type
!= ESCO_LINK
)
1132 BT_DBG("hcon %p reason %d", hcon
, reason
);
1134 sco_conn_del(hcon
, bt_to_errno(reason
));
1137 void sco_recv_scodata(struct hci_conn
*hcon
, struct sk_buff
*skb
)
1139 struct sco_conn
*conn
= hcon
->sco_data
;
1144 BT_DBG("conn %p len %d", conn
, skb
->len
);
1147 sco_recv_frame(conn
, skb
);
1155 static struct hci_cb sco_cb
= {
1157 .connect_cfm
= sco_connect_cfm
,
1158 .disconn_cfm
= sco_disconn_cfm
,
1161 static int sco_debugfs_show(struct seq_file
*f
, void *p
)
1165 read_lock(&sco_sk_list
.lock
);
1167 sk_for_each(sk
, &sco_sk_list
.head
) {
1168 seq_printf(f
, "%pMR %pMR %d\n", &sco_pi(sk
)->src
,
1169 &sco_pi(sk
)->dst
, sk
->sk_state
);
1172 read_unlock(&sco_sk_list
.lock
);
1177 static int sco_debugfs_open(struct inode
*inode
, struct file
*file
)
1179 return single_open(file
, sco_debugfs_show
, inode
->i_private
);
1182 static const struct file_operations sco_debugfs_fops
= {
1183 .open
= sco_debugfs_open
,
1185 .llseek
= seq_lseek
,
1186 .release
= single_release
,
1189 static struct dentry
*sco_debugfs
;
1191 static const struct proto_ops sco_sock_ops
= {
1192 .family
= PF_BLUETOOTH
,
1193 .owner
= THIS_MODULE
,
1194 .release
= sco_sock_release
,
1195 .bind
= sco_sock_bind
,
1196 .connect
= sco_sock_connect
,
1197 .listen
= sco_sock_listen
,
1198 .accept
= sco_sock_accept
,
1199 .getname
= sco_sock_getname
,
1200 .sendmsg
= sco_sock_sendmsg
,
1201 .recvmsg
= sco_sock_recvmsg
,
1202 .poll
= bt_sock_poll
,
1203 .ioctl
= bt_sock_ioctl
,
1204 .mmap
= sock_no_mmap
,
1205 .socketpair
= sock_no_socketpair
,
1206 .shutdown
= sco_sock_shutdown
,
1207 .setsockopt
= sco_sock_setsockopt
,
1208 .getsockopt
= sco_sock_getsockopt
1211 static const struct net_proto_family sco_sock_family_ops
= {
1212 .family
= PF_BLUETOOTH
,
1213 .owner
= THIS_MODULE
,
1214 .create
= sco_sock_create
,
1217 int __init
sco_init(void)
1221 BUILD_BUG_ON(sizeof(struct sockaddr_sco
) > sizeof(struct sockaddr
));
1223 err
= proto_register(&sco_proto
, 0);
1227 err
= bt_sock_register(BTPROTO_SCO
, &sco_sock_family_ops
);
1229 BT_ERR("SCO socket registration failed");
1233 err
= bt_procfs_init(&init_net
, "sco", &sco_sk_list
, NULL
);
1235 BT_ERR("Failed to create SCO proc file");
1236 bt_sock_unregister(BTPROTO_SCO
);
1240 BT_INFO("SCO socket layer initialized");
1242 hci_register_cb(&sco_cb
);
1244 if (IS_ERR_OR_NULL(bt_debugfs
))
1247 sco_debugfs
= debugfs_create_file("sco", 0444, bt_debugfs
,
1248 NULL
, &sco_debugfs_fops
);
1253 proto_unregister(&sco_proto
);
1259 bt_procfs_cleanup(&init_net
, "sco");
1261 debugfs_remove(sco_debugfs
);
1263 hci_unregister_cb(&sco_cb
);
1265 bt_sock_unregister(BTPROTO_SCO
);
1267 proto_unregister(&sco_proto
);
1270 module_param(disable_esco
, bool, 0644);
1271 MODULE_PARM_DESC(disable_esco
, "Disable eSCO connection creation");