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 int 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
);
174 sco_sock_clear_timer(sk
);
175 sco_chan_del(sk
, err
);
180 hcon
->sco_data
= NULL
;
185 static void __sco_chan_add(struct sco_conn
*conn
, struct sock
*sk
, struct sock
*parent
)
187 BT_DBG("conn %p", conn
);
189 sco_pi(sk
)->conn
= conn
;
193 bt_accept_enqueue(parent
, sk
);
196 static int sco_chan_add(struct sco_conn
*conn
, struct sock
*sk
,
205 __sco_chan_add(conn
, sk
, parent
);
207 sco_conn_unlock(conn
);
211 static int sco_connect(struct sock
*sk
)
213 struct sco_conn
*conn
;
214 struct hci_conn
*hcon
;
215 struct hci_dev
*hdev
;
218 BT_DBG("%pMR -> %pMR", &sco_pi(sk
)->src
, &sco_pi(sk
)->dst
);
220 hdev
= hci_get_route(&sco_pi(sk
)->dst
, &sco_pi(sk
)->src
);
222 return -EHOSTUNREACH
;
226 if (lmp_esco_capable(hdev
) && !disable_esco
)
231 if (sco_pi(sk
)->setting
== BT_VOICE_TRANSPARENT
&&
232 (!lmp_transp_capable(hdev
) || !lmp_esco_capable(hdev
))) {
237 hcon
= hci_connect_sco(hdev
, type
, &sco_pi(sk
)->dst
,
238 sco_pi(sk
)->setting
);
244 conn
= sco_conn_add(hcon
);
251 /* Update source addr of the socket */
252 bacpy(&sco_pi(sk
)->src
, &hcon
->src
);
254 err
= sco_chan_add(conn
, sk
, NULL
);
258 if (hcon
->state
== BT_CONNECTED
) {
259 sco_sock_clear_timer(sk
);
260 sk
->sk_state
= BT_CONNECTED
;
262 sk
->sk_state
= BT_CONNECT
;
263 sco_sock_set_timer(sk
, sk
->sk_sndtimeo
);
267 hci_dev_unlock(hdev
);
272 static int sco_send_frame(struct sock
*sk
, struct msghdr
*msg
, int len
)
274 struct sco_conn
*conn
= sco_pi(sk
)->conn
;
278 /* Check outgoing MTU */
282 BT_DBG("sk %p len %d", sk
, len
);
284 skb
= bt_skb_send_alloc(sk
, len
, msg
->msg_flags
& MSG_DONTWAIT
, &err
);
288 if (memcpy_from_msg(skb_put(skb
, len
), msg
, len
)) {
293 hci_send_sco(conn
->hcon
, skb
);
298 static void sco_recv_frame(struct sco_conn
*conn
, struct sk_buff
*skb
)
304 sco_conn_unlock(conn
);
309 BT_DBG("sk %p len %d", sk
, skb
->len
);
311 if (sk
->sk_state
!= BT_CONNECTED
)
314 if (!sock_queue_rcv_skb(sk
, skb
))
321 /* -------- Socket interface ---------- */
322 static struct sock
*__sco_get_sock_listen_by_addr(bdaddr_t
*ba
)
326 sk_for_each(sk
, &sco_sk_list
.head
) {
327 if (sk
->sk_state
!= BT_LISTEN
)
330 if (!bacmp(&sco_pi(sk
)->src
, ba
))
337 /* Find socket listening on source bdaddr.
338 * Returns closest match.
340 static struct sock
*sco_get_sock_listen(bdaddr_t
*src
)
342 struct sock
*sk
= NULL
, *sk1
= NULL
;
344 read_lock(&sco_sk_list
.lock
);
346 sk_for_each(sk
, &sco_sk_list
.head
) {
347 if (sk
->sk_state
!= BT_LISTEN
)
351 if (!bacmp(&sco_pi(sk
)->src
, src
))
355 if (!bacmp(&sco_pi(sk
)->src
, BDADDR_ANY
))
359 read_unlock(&sco_sk_list
.lock
);
361 return sk
? sk
: sk1
;
364 static void sco_sock_destruct(struct sock
*sk
)
368 skb_queue_purge(&sk
->sk_receive_queue
);
369 skb_queue_purge(&sk
->sk_write_queue
);
372 static void sco_sock_cleanup_listen(struct sock
*parent
)
376 BT_DBG("parent %p", parent
);
378 /* Close not yet accepted channels */
379 while ((sk
= bt_accept_dequeue(parent
, NULL
))) {
384 parent
->sk_state
= BT_CLOSED
;
385 sock_set_flag(parent
, SOCK_ZAPPED
);
388 /* Kill socket (only if zapped and orphan)
389 * Must be called on unlocked socket.
391 static void sco_sock_kill(struct sock
*sk
)
393 if (!sock_flag(sk
, SOCK_ZAPPED
) || sk
->sk_socket
)
396 BT_DBG("sk %p state %d", sk
, sk
->sk_state
);
398 /* Kill poor orphan */
399 bt_sock_unlink(&sco_sk_list
, sk
);
400 sock_set_flag(sk
, SOCK_DEAD
);
404 static void __sco_sock_close(struct sock
*sk
)
406 BT_DBG("sk %p state %d socket %p", sk
, sk
->sk_state
, sk
->sk_socket
);
408 switch (sk
->sk_state
) {
410 sco_sock_cleanup_listen(sk
);
415 if (sco_pi(sk
)->conn
->hcon
) {
416 sk
->sk_state
= BT_DISCONN
;
417 sco_sock_set_timer(sk
, SCO_DISCONN_TIMEOUT
);
418 hci_conn_drop(sco_pi(sk
)->conn
->hcon
);
419 sco_pi(sk
)->conn
->hcon
= NULL
;
421 sco_chan_del(sk
, ECONNRESET
);
427 sco_chan_del(sk
, ECONNRESET
);
431 sock_set_flag(sk
, SOCK_ZAPPED
);
436 /* Must be called on unlocked socket. */
437 static void sco_sock_close(struct sock
*sk
)
439 sco_sock_clear_timer(sk
);
441 __sco_sock_close(sk
);
446 static void sco_sock_init(struct sock
*sk
, struct sock
*parent
)
451 sk
->sk_type
= parent
->sk_type
;
452 bt_sk(sk
)->flags
= bt_sk(parent
)->flags
;
453 security_sk_clone(parent
, sk
);
457 static struct proto sco_proto
= {
459 .owner
= THIS_MODULE
,
460 .obj_size
= sizeof(struct sco_pinfo
)
463 static struct sock
*sco_sock_alloc(struct net
*net
, struct socket
*sock
, int proto
, gfp_t prio
)
467 sk
= sk_alloc(net
, PF_BLUETOOTH
, prio
, &sco_proto
);
471 sock_init_data(sock
, sk
);
472 INIT_LIST_HEAD(&bt_sk(sk
)->accept_q
);
474 sk
->sk_destruct
= sco_sock_destruct
;
475 sk
->sk_sndtimeo
= SCO_CONN_TIMEOUT
;
477 sock_reset_flag(sk
, SOCK_ZAPPED
);
479 sk
->sk_protocol
= proto
;
480 sk
->sk_state
= BT_OPEN
;
482 sco_pi(sk
)->setting
= BT_VOICE_CVSD_16BIT
;
484 setup_timer(&sk
->sk_timer
, sco_sock_timeout
, (unsigned long)sk
);
486 bt_sock_link(&sco_sk_list
, sk
);
490 static int sco_sock_create(struct net
*net
, struct socket
*sock
, int protocol
,
495 BT_DBG("sock %p", sock
);
497 sock
->state
= SS_UNCONNECTED
;
499 if (sock
->type
!= SOCK_SEQPACKET
)
500 return -ESOCKTNOSUPPORT
;
502 sock
->ops
= &sco_sock_ops
;
504 sk
= sco_sock_alloc(net
, sock
, protocol
, GFP_ATOMIC
);
508 sco_sock_init(sk
, NULL
);
512 static int sco_sock_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
514 struct sockaddr_sco
*sa
= (struct sockaddr_sco
*) addr
;
515 struct sock
*sk
= sock
->sk
;
518 BT_DBG("sk %p %pMR", sk
, &sa
->sco_bdaddr
);
520 if (!addr
|| addr
->sa_family
!= AF_BLUETOOTH
)
525 if (sk
->sk_state
!= BT_OPEN
) {
530 if (sk
->sk_type
!= SOCK_SEQPACKET
) {
535 bacpy(&sco_pi(sk
)->src
, &sa
->sco_bdaddr
);
537 sk
->sk_state
= BT_BOUND
;
544 static int sco_sock_connect(struct socket
*sock
, struct sockaddr
*addr
, int alen
, int flags
)
546 struct sockaddr_sco
*sa
= (struct sockaddr_sco
*) addr
;
547 struct sock
*sk
= sock
->sk
;
552 if (alen
< sizeof(struct sockaddr_sco
) ||
553 addr
->sa_family
!= AF_BLUETOOTH
)
556 if (sk
->sk_state
!= BT_OPEN
&& sk
->sk_state
!= BT_BOUND
)
559 if (sk
->sk_type
!= SOCK_SEQPACKET
)
564 /* Set destination address and psm */
565 bacpy(&sco_pi(sk
)->dst
, &sa
->sco_bdaddr
);
567 err
= sco_connect(sk
);
571 err
= bt_sock_wait_state(sk
, BT_CONNECTED
,
572 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
579 static int sco_sock_listen(struct socket
*sock
, int backlog
)
581 struct sock
*sk
= sock
->sk
;
582 bdaddr_t
*src
= &sco_pi(sk
)->src
;
585 BT_DBG("sk %p backlog %d", sk
, backlog
);
589 if (sk
->sk_state
!= BT_BOUND
) {
594 if (sk
->sk_type
!= SOCK_SEQPACKET
) {
599 write_lock(&sco_sk_list
.lock
);
601 if (__sco_get_sock_listen_by_addr(src
)) {
606 sk
->sk_max_ack_backlog
= backlog
;
607 sk
->sk_ack_backlog
= 0;
609 sk
->sk_state
= BT_LISTEN
;
612 write_unlock(&sco_sk_list
.lock
);
619 static int sco_sock_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
621 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
622 struct sock
*sk
= sock
->sk
, *ch
;
628 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
630 BT_DBG("sk %p timeo %ld", sk
, timeo
);
632 /* Wait for an incoming connection. (wake-one). */
633 add_wait_queue_exclusive(sk_sleep(sk
), &wait
);
635 if (sk
->sk_state
!= BT_LISTEN
) {
640 ch
= bt_accept_dequeue(sk
, newsock
);
649 if (signal_pending(current
)) {
650 err
= sock_intr_errno(timeo
);
656 timeo
= wait_woken(&wait
, TASK_INTERRUPTIBLE
, timeo
);
659 remove_wait_queue(sk_sleep(sk
), &wait
);
664 newsock
->state
= SS_CONNECTED
;
666 BT_DBG("new socket %p", ch
);
673 static int sco_sock_getname(struct socket
*sock
, struct sockaddr
*addr
, int *len
, int peer
)
675 struct sockaddr_sco
*sa
= (struct sockaddr_sco
*) addr
;
676 struct sock
*sk
= sock
->sk
;
678 BT_DBG("sock %p, sk %p", sock
, sk
);
680 addr
->sa_family
= AF_BLUETOOTH
;
681 *len
= sizeof(struct sockaddr_sco
);
684 bacpy(&sa
->sco_bdaddr
, &sco_pi(sk
)->dst
);
686 bacpy(&sa
->sco_bdaddr
, &sco_pi(sk
)->src
);
691 static int sco_sock_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
692 struct msghdr
*msg
, size_t len
)
694 struct sock
*sk
= sock
->sk
;
697 BT_DBG("sock %p, sk %p", sock
, sk
);
699 err
= sock_error(sk
);
703 if (msg
->msg_flags
& MSG_OOB
)
708 if (sk
->sk_state
== BT_CONNECTED
)
709 err
= sco_send_frame(sk
, msg
, len
);
717 static void sco_conn_defer_accept(struct hci_conn
*conn
, u16 setting
)
719 struct hci_dev
*hdev
= conn
->hdev
;
721 BT_DBG("conn %p", conn
);
723 conn
->state
= BT_CONFIG
;
725 if (!lmp_esco_capable(hdev
)) {
726 struct hci_cp_accept_conn_req cp
;
728 bacpy(&cp
.bdaddr
, &conn
->dst
);
729 cp
.role
= 0x00; /* Ignored */
731 hci_send_cmd(hdev
, HCI_OP_ACCEPT_CONN_REQ
, sizeof(cp
), &cp
);
733 struct hci_cp_accept_sync_conn_req cp
;
735 bacpy(&cp
.bdaddr
, &conn
->dst
);
736 cp
.pkt_type
= cpu_to_le16(conn
->pkt_type
);
738 cp
.tx_bandwidth
= cpu_to_le32(0x00001f40);
739 cp
.rx_bandwidth
= cpu_to_le32(0x00001f40);
740 cp
.content_format
= cpu_to_le16(setting
);
742 switch (setting
& SCO_AIRMODE_MASK
) {
743 case SCO_AIRMODE_TRANSP
:
744 if (conn
->pkt_type
& ESCO_2EV3
)
745 cp
.max_latency
= cpu_to_le16(0x0008);
747 cp
.max_latency
= cpu_to_le16(0x000D);
748 cp
.retrans_effort
= 0x02;
750 case SCO_AIRMODE_CVSD
:
751 cp
.max_latency
= cpu_to_le16(0xffff);
752 cp
.retrans_effort
= 0xff;
756 hci_send_cmd(hdev
, HCI_OP_ACCEPT_SYNC_CONN_REQ
,
761 static int sco_sock_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
762 struct msghdr
*msg
, size_t len
, int flags
)
764 struct sock
*sk
= sock
->sk
;
765 struct sco_pinfo
*pi
= sco_pi(sk
);
769 if (sk
->sk_state
== BT_CONNECT2
&&
770 test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
)) {
771 sco_conn_defer_accept(pi
->conn
->hcon
, pi
->setting
);
772 sk
->sk_state
= BT_CONFIG
;
780 return bt_sock_recvmsg(iocb
, sock
, msg
, len
, flags
);
783 static int sco_sock_setsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, unsigned int optlen
)
785 struct sock
*sk
= sock
->sk
;
787 struct bt_voice voice
;
797 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
802 if (get_user(opt
, (u32 __user
*) optval
)) {
808 set_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
);
810 clear_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
);
814 if (sk
->sk_state
!= BT_OPEN
&& sk
->sk_state
!= BT_BOUND
&&
815 sk
->sk_state
!= BT_CONNECT2
) {
820 voice
.setting
= sco_pi(sk
)->setting
;
822 len
= min_t(unsigned int, sizeof(voice
), optlen
);
823 if (copy_from_user((char *) &voice
, optval
, len
)) {
828 /* Explicitly check for these values */
829 if (voice
.setting
!= BT_VOICE_TRANSPARENT
&&
830 voice
.setting
!= BT_VOICE_CVSD_16BIT
) {
835 sco_pi(sk
)->setting
= voice
.setting
;
847 static int sco_sock_getsockopt_old(struct socket
*sock
, int optname
, char __user
*optval
, int __user
*optlen
)
849 struct sock
*sk
= sock
->sk
;
850 struct sco_options opts
;
851 struct sco_conninfo cinfo
;
856 if (get_user(len
, optlen
))
863 if (sk
->sk_state
!= BT_CONNECTED
&&
864 !(sk
->sk_state
== BT_CONNECT2
&&
865 test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
))) {
870 opts
.mtu
= sco_pi(sk
)->conn
->mtu
;
872 BT_DBG("mtu %d", opts
.mtu
);
874 len
= min_t(unsigned int, len
, sizeof(opts
));
875 if (copy_to_user(optval
, (char *)&opts
, len
))
881 if (sk
->sk_state
!= BT_CONNECTED
&&
882 !(sk
->sk_state
== BT_CONNECT2
&&
883 test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
))) {
888 memset(&cinfo
, 0, sizeof(cinfo
));
889 cinfo
.hci_handle
= sco_pi(sk
)->conn
->hcon
->handle
;
890 memcpy(cinfo
.dev_class
, sco_pi(sk
)->conn
->hcon
->dev_class
, 3);
892 len
= min_t(unsigned int, len
, sizeof(cinfo
));
893 if (copy_to_user(optval
, (char *)&cinfo
, len
))
907 static int sco_sock_getsockopt(struct socket
*sock
, int level
, int optname
, char __user
*optval
, int __user
*optlen
)
909 struct sock
*sk
= sock
->sk
;
911 struct bt_voice voice
;
915 if (level
== SOL_SCO
)
916 return sco_sock_getsockopt_old(sock
, optname
, optval
, optlen
);
918 if (get_user(len
, optlen
))
926 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
931 if (put_user(test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
),
932 (u32 __user
*) optval
))
938 voice
.setting
= sco_pi(sk
)->setting
;
940 len
= min_t(unsigned int, len
, sizeof(voice
));
941 if (copy_to_user(optval
, (char *)&voice
, len
))
955 static int sco_sock_shutdown(struct socket
*sock
, int how
)
957 struct sock
*sk
= sock
->sk
;
960 BT_DBG("sock %p, sk %p", sock
, sk
);
966 if (!sk
->sk_shutdown
) {
967 sk
->sk_shutdown
= SHUTDOWN_MASK
;
968 sco_sock_clear_timer(sk
);
969 __sco_sock_close(sk
);
971 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
&&
972 !(current
->flags
& PF_EXITING
))
973 err
= bt_sock_wait_state(sk
, BT_CLOSED
,
980 static int sco_sock_release(struct socket
*sock
)
982 struct sock
*sk
= sock
->sk
;
985 BT_DBG("sock %p, sk %p", sock
, sk
);
992 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
&&
993 !(current
->flags
& PF_EXITING
)) {
995 err
= bt_sock_wait_state(sk
, BT_CLOSED
, sk
->sk_lingertime
);
1004 static void sco_conn_ready(struct sco_conn
*conn
)
1006 struct sock
*parent
;
1007 struct sock
*sk
= conn
->sk
;
1009 BT_DBG("conn %p", conn
);
1012 sco_sock_clear_timer(sk
);
1014 sk
->sk_state
= BT_CONNECTED
;
1015 sk
->sk_state_change(sk
);
1018 sco_conn_lock(conn
);
1020 parent
= sco_get_sock_listen(&conn
->hcon
->src
);
1022 sco_conn_unlock(conn
);
1026 bh_lock_sock(parent
);
1028 sk
= sco_sock_alloc(sock_net(parent
), NULL
,
1029 BTPROTO_SCO
, GFP_ATOMIC
);
1031 bh_unlock_sock(parent
);
1032 sco_conn_unlock(conn
);
1036 sco_sock_init(sk
, parent
);
1038 bacpy(&sco_pi(sk
)->src
, &conn
->hcon
->src
);
1039 bacpy(&sco_pi(sk
)->dst
, &conn
->hcon
->dst
);
1041 hci_conn_hold(conn
->hcon
);
1042 __sco_chan_add(conn
, sk
, parent
);
1044 if (test_bit(BT_SK_DEFER_SETUP
, &bt_sk(parent
)->flags
))
1045 sk
->sk_state
= BT_CONNECT2
;
1047 sk
->sk_state
= BT_CONNECTED
;
1049 /* Wake up parent */
1050 parent
->sk_data_ready(parent
);
1052 bh_unlock_sock(parent
);
1054 sco_conn_unlock(conn
);
1058 /* ----- SCO interface with lower layer (HCI) ----- */
1059 int sco_connect_ind(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, __u8
*flags
)
1064 BT_DBG("hdev %s, bdaddr %pMR", hdev
->name
, bdaddr
);
1066 /* Find listening sockets */
1067 read_lock(&sco_sk_list
.lock
);
1068 sk_for_each(sk
, &sco_sk_list
.head
) {
1069 if (sk
->sk_state
!= BT_LISTEN
)
1072 if (!bacmp(&sco_pi(sk
)->src
, &hdev
->bdaddr
) ||
1073 !bacmp(&sco_pi(sk
)->src
, BDADDR_ANY
)) {
1074 lm
|= HCI_LM_ACCEPT
;
1076 if (test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
))
1077 *flags
|= HCI_PROTO_DEFER
;
1081 read_unlock(&sco_sk_list
.lock
);
1086 void sco_connect_cfm(struct hci_conn
*hcon
, __u8 status
)
1088 BT_DBG("hcon %p bdaddr %pMR status %d", hcon
, &hcon
->dst
, status
);
1090 struct sco_conn
*conn
;
1092 conn
= sco_conn_add(hcon
);
1094 sco_conn_ready(conn
);
1096 sco_conn_del(hcon
, bt_to_errno(status
));
1099 void sco_disconn_cfm(struct hci_conn
*hcon
, __u8 reason
)
1101 BT_DBG("hcon %p reason %d", hcon
, reason
);
1103 sco_conn_del(hcon
, bt_to_errno(reason
));
1106 int sco_recv_scodata(struct hci_conn
*hcon
, struct sk_buff
*skb
)
1108 struct sco_conn
*conn
= hcon
->sco_data
;
1113 BT_DBG("conn %p len %d", conn
, skb
->len
);
1116 sco_recv_frame(conn
, skb
);
1125 static int sco_debugfs_show(struct seq_file
*f
, void *p
)
1129 read_lock(&sco_sk_list
.lock
);
1131 sk_for_each(sk
, &sco_sk_list
.head
) {
1132 seq_printf(f
, "%pMR %pMR %d\n", &sco_pi(sk
)->src
,
1133 &sco_pi(sk
)->dst
, sk
->sk_state
);
1136 read_unlock(&sco_sk_list
.lock
);
1141 static int sco_debugfs_open(struct inode
*inode
, struct file
*file
)
1143 return single_open(file
, sco_debugfs_show
, inode
->i_private
);
1146 static const struct file_operations sco_debugfs_fops
= {
1147 .open
= sco_debugfs_open
,
1149 .llseek
= seq_lseek
,
1150 .release
= single_release
,
1153 static struct dentry
*sco_debugfs
;
1155 static const struct proto_ops sco_sock_ops
= {
1156 .family
= PF_BLUETOOTH
,
1157 .owner
= THIS_MODULE
,
1158 .release
= sco_sock_release
,
1159 .bind
= sco_sock_bind
,
1160 .connect
= sco_sock_connect
,
1161 .listen
= sco_sock_listen
,
1162 .accept
= sco_sock_accept
,
1163 .getname
= sco_sock_getname
,
1164 .sendmsg
= sco_sock_sendmsg
,
1165 .recvmsg
= sco_sock_recvmsg
,
1166 .poll
= bt_sock_poll
,
1167 .ioctl
= bt_sock_ioctl
,
1168 .mmap
= sock_no_mmap
,
1169 .socketpair
= sock_no_socketpair
,
1170 .shutdown
= sco_sock_shutdown
,
1171 .setsockopt
= sco_sock_setsockopt
,
1172 .getsockopt
= sco_sock_getsockopt
1175 static const struct net_proto_family sco_sock_family_ops
= {
1176 .family
= PF_BLUETOOTH
,
1177 .owner
= THIS_MODULE
,
1178 .create
= sco_sock_create
,
1181 int __init
sco_init(void)
1185 BUILD_BUG_ON(sizeof(struct sockaddr_sco
) > sizeof(struct sockaddr
));
1187 err
= proto_register(&sco_proto
, 0);
1191 err
= bt_sock_register(BTPROTO_SCO
, &sco_sock_family_ops
);
1193 BT_ERR("SCO socket registration failed");
1197 err
= bt_procfs_init(&init_net
, "sco", &sco_sk_list
, NULL
);
1199 BT_ERR("Failed to create SCO proc file");
1200 bt_sock_unregister(BTPROTO_SCO
);
1204 BT_INFO("SCO socket layer initialized");
1206 if (IS_ERR_OR_NULL(bt_debugfs
))
1209 sco_debugfs
= debugfs_create_file("sco", 0444, bt_debugfs
,
1210 NULL
, &sco_debugfs_fops
);
1215 proto_unregister(&sco_proto
);
1219 void __exit
sco_exit(void)
1221 bt_procfs_cleanup(&init_net
, "sco");
1223 debugfs_remove(sco_debugfs
);
1225 bt_sock_unregister(BTPROTO_SCO
);
1227 proto_unregister(&sco_proto
);
1230 module_param(disable_esco
, bool, 0644);
1231 MODULE_PARM_DESC(disable_esco
, "Disable eSCO connection creation");