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>
30 #include <linux/sched/signal.h>
32 #include <net/bluetooth/bluetooth.h>
33 #include <net/bluetooth/hci_core.h>
34 #include <net/bluetooth/sco.h>
36 static bool disable_esco
;
38 static const struct proto_ops sco_sock_ops
;
40 static struct bt_sock_list sco_sk_list
= {
41 .lock
= __RW_LOCK_UNLOCKED(sco_sk_list
.lock
)
44 /* ---- SCO connections ---- */
46 struct hci_conn
*hcon
;
54 #define sco_conn_lock(c) spin_lock(&c->lock);
55 #define sco_conn_unlock(c) spin_unlock(&c->lock);
57 static void sco_sock_close(struct sock
*sk
);
58 static void sco_sock_kill(struct sock
*sk
);
60 /* ----- SCO socket info ----- */
61 #define sco_pi(sk) ((struct sco_pinfo *) sk)
70 struct sco_conn
*conn
;
73 /* ---- SCO timers ---- */
74 #define SCO_CONN_TIMEOUT (HZ * 40)
75 #define SCO_DISCONN_TIMEOUT (HZ * 2)
77 static void sco_sock_timeout(struct timer_list
*t
)
79 struct sock
*sk
= from_timer(sk
, t
, sk_timer
);
81 BT_DBG("sock %p state %d", sk
, sk
->sk_state
);
84 sk
->sk_err
= ETIMEDOUT
;
85 sk
->sk_state_change(sk
);
92 static void sco_sock_set_timer(struct sock
*sk
, long timeout
)
94 BT_DBG("sock %p state %d timeout %ld", sk
, sk
->sk_state
, timeout
);
95 sk_reset_timer(sk
, &sk
->sk_timer
, jiffies
+ timeout
);
98 static void sco_sock_clear_timer(struct sock
*sk
)
100 BT_DBG("sock %p state %d", sk
, sk
->sk_state
);
101 sk_stop_timer(sk
, &sk
->sk_timer
);
104 /* ---- SCO connections ---- */
105 static struct sco_conn
*sco_conn_add(struct hci_conn
*hcon
)
107 struct hci_dev
*hdev
= hcon
->hdev
;
108 struct sco_conn
*conn
= hcon
->sco_data
;
113 conn
= kzalloc(sizeof(struct sco_conn
), GFP_KERNEL
);
117 spin_lock_init(&conn
->lock
);
119 hcon
->sco_data
= conn
;
122 if (hdev
->sco_mtu
> 0)
123 conn
->mtu
= hdev
->sco_mtu
;
127 BT_DBG("hcon %p conn %p", hcon
, conn
);
133 * Must be called on the locked socket. */
134 static void sco_chan_del(struct sock
*sk
, int err
)
136 struct sco_conn
*conn
;
138 conn
= sco_pi(sk
)->conn
;
140 BT_DBG("sk %p, conn %p, err %d", sk
, conn
, err
);
145 sco_pi(sk
)->conn
= NULL
;
146 sco_conn_unlock(conn
);
149 hci_conn_drop(conn
->hcon
);
152 sk
->sk_state
= BT_CLOSED
;
154 sk
->sk_state_change(sk
);
156 sock_set_flag(sk
, SOCK_ZAPPED
);
159 static void sco_conn_del(struct hci_conn
*hcon
, int err
)
161 struct sco_conn
*conn
= hcon
->sco_data
;
167 BT_DBG("hcon %p conn %p, err %d", hcon
, conn
, err
);
172 sco_conn_unlock(conn
);
177 sco_sock_clear_timer(sk
);
178 sco_chan_del(sk
, err
);
184 hcon
->sco_data
= NULL
;
188 static void __sco_chan_add(struct sco_conn
*conn
, struct sock
*sk
,
191 BT_DBG("conn %p", conn
);
193 sco_pi(sk
)->conn
= conn
;
197 bt_accept_enqueue(parent
, sk
, true);
200 static int sco_chan_add(struct sco_conn
*conn
, struct sock
*sk
,
209 __sco_chan_add(conn
, sk
, parent
);
211 sco_conn_unlock(conn
);
215 static int sco_connect(struct sock
*sk
)
217 struct sco_conn
*conn
;
218 struct hci_conn
*hcon
;
219 struct hci_dev
*hdev
;
222 BT_DBG("%pMR -> %pMR", &sco_pi(sk
)->src
, &sco_pi(sk
)->dst
);
224 hdev
= hci_get_route(&sco_pi(sk
)->dst
, &sco_pi(sk
)->src
, BDADDR_BREDR
);
226 return -EHOSTUNREACH
;
230 if (lmp_esco_capable(hdev
) && !disable_esco
)
235 if (sco_pi(sk
)->setting
== BT_VOICE_TRANSPARENT
&&
236 (!lmp_transp_capable(hdev
) || !lmp_esco_capable(hdev
))) {
241 hcon
= hci_connect_sco(hdev
, type
, &sco_pi(sk
)->dst
,
242 sco_pi(sk
)->setting
);
248 conn
= sco_conn_add(hcon
);
255 /* Update source addr of the socket */
256 bacpy(&sco_pi(sk
)->src
, &hcon
->src
);
258 err
= sco_chan_add(conn
, sk
, NULL
);
262 if (hcon
->state
== BT_CONNECTED
) {
263 sco_sock_clear_timer(sk
);
264 sk
->sk_state
= BT_CONNECTED
;
266 sk
->sk_state
= BT_CONNECT
;
267 sco_sock_set_timer(sk
, sk
->sk_sndtimeo
);
271 hci_dev_unlock(hdev
);
276 static int sco_send_frame(struct sock
*sk
, struct msghdr
*msg
, int len
)
278 struct sco_conn
*conn
= sco_pi(sk
)->conn
;
282 /* Check outgoing MTU */
286 BT_DBG("sk %p len %d", sk
, len
);
288 skb
= bt_skb_send_alloc(sk
, len
, msg
->msg_flags
& MSG_DONTWAIT
, &err
);
292 if (memcpy_from_msg(skb_put(skb
, len
), msg
, len
)) {
297 hci_send_sco(conn
->hcon
, skb
);
302 static void sco_recv_frame(struct sco_conn
*conn
, struct sk_buff
*skb
)
308 sco_conn_unlock(conn
);
313 BT_DBG("sk %p len %d", sk
, skb
->len
);
315 if (sk
->sk_state
!= BT_CONNECTED
)
318 if (!sock_queue_rcv_skb(sk
, skb
))
325 /* -------- Socket interface ---------- */
326 static struct sock
*__sco_get_sock_listen_by_addr(bdaddr_t
*ba
)
330 sk_for_each(sk
, &sco_sk_list
.head
) {
331 if (sk
->sk_state
!= BT_LISTEN
)
334 if (!bacmp(&sco_pi(sk
)->src
, ba
))
341 /* Find socket listening on source bdaddr.
342 * Returns closest match.
344 static struct sock
*sco_get_sock_listen(bdaddr_t
*src
)
346 struct sock
*sk
= NULL
, *sk1
= NULL
;
348 read_lock(&sco_sk_list
.lock
);
350 sk_for_each(sk
, &sco_sk_list
.head
) {
351 if (sk
->sk_state
!= BT_LISTEN
)
355 if (!bacmp(&sco_pi(sk
)->src
, src
))
359 if (!bacmp(&sco_pi(sk
)->src
, BDADDR_ANY
))
363 read_unlock(&sco_sk_list
.lock
);
365 return sk
? sk
: sk1
;
368 static void sco_sock_destruct(struct sock
*sk
)
372 skb_queue_purge(&sk
->sk_receive_queue
);
373 skb_queue_purge(&sk
->sk_write_queue
);
376 static void sco_sock_cleanup_listen(struct sock
*parent
)
380 BT_DBG("parent %p", parent
);
382 /* Close not yet accepted channels */
383 while ((sk
= bt_accept_dequeue(parent
, NULL
))) {
388 parent
->sk_state
= BT_CLOSED
;
389 sock_set_flag(parent
, SOCK_ZAPPED
);
392 /* Kill socket (only if zapped and orphan)
393 * Must be called on unlocked socket.
395 static void sco_sock_kill(struct sock
*sk
)
397 if (!sock_flag(sk
, SOCK_ZAPPED
) || sk
->sk_socket
||
398 sock_flag(sk
, SOCK_DEAD
))
401 BT_DBG("sk %p state %d", sk
, sk
->sk_state
);
403 /* Kill poor orphan */
404 bt_sock_unlink(&sco_sk_list
, sk
);
405 sock_set_flag(sk
, SOCK_DEAD
);
409 static void __sco_sock_close(struct sock
*sk
)
411 BT_DBG("sk %p state %d socket %p", sk
, sk
->sk_state
, sk
->sk_socket
);
413 switch (sk
->sk_state
) {
415 sco_sock_cleanup_listen(sk
);
420 if (sco_pi(sk
)->conn
->hcon
) {
421 sk
->sk_state
= BT_DISCONN
;
422 sco_sock_set_timer(sk
, SCO_DISCONN_TIMEOUT
);
423 sco_conn_lock(sco_pi(sk
)->conn
);
424 hci_conn_drop(sco_pi(sk
)->conn
->hcon
);
425 sco_pi(sk
)->conn
->hcon
= NULL
;
426 sco_conn_unlock(sco_pi(sk
)->conn
);
428 sco_chan_del(sk
, ECONNRESET
);
434 sco_chan_del(sk
, ECONNRESET
);
438 sock_set_flag(sk
, SOCK_ZAPPED
);
443 /* Must be called on unlocked socket. */
444 static void sco_sock_close(struct sock
*sk
)
446 sco_sock_clear_timer(sk
);
448 __sco_sock_close(sk
);
453 static void sco_skb_put_cmsg(struct sk_buff
*skb
, struct msghdr
*msg
,
456 if (sco_pi(sk
)->cmsg_mask
& SCO_CMSG_PKT_STATUS
)
457 put_cmsg(msg
, SOL_BLUETOOTH
, BT_SCM_PKT_STATUS
,
458 sizeof(bt_cb(skb
)->sco
.pkt_status
),
459 &bt_cb(skb
)->sco
.pkt_status
);
462 static void sco_sock_init(struct sock
*sk
, struct sock
*parent
)
467 sk
->sk_type
= parent
->sk_type
;
468 bt_sk(sk
)->flags
= bt_sk(parent
)->flags
;
469 security_sk_clone(parent
, sk
);
471 bt_sk(sk
)->skb_put_cmsg
= sco_skb_put_cmsg
;
475 static struct proto sco_proto
= {
477 .owner
= THIS_MODULE
,
478 .obj_size
= sizeof(struct sco_pinfo
)
481 static struct sock
*sco_sock_alloc(struct net
*net
, struct socket
*sock
,
482 int proto
, gfp_t prio
, int kern
)
486 sk
= sk_alloc(net
, PF_BLUETOOTH
, prio
, &sco_proto
, kern
);
490 sock_init_data(sock
, sk
);
491 INIT_LIST_HEAD(&bt_sk(sk
)->accept_q
);
493 sk
->sk_destruct
= sco_sock_destruct
;
494 sk
->sk_sndtimeo
= SCO_CONN_TIMEOUT
;
496 sock_reset_flag(sk
, SOCK_ZAPPED
);
498 sk
->sk_protocol
= proto
;
499 sk
->sk_state
= BT_OPEN
;
501 sco_pi(sk
)->setting
= BT_VOICE_CVSD_16BIT
;
503 timer_setup(&sk
->sk_timer
, sco_sock_timeout
, 0);
505 bt_sock_link(&sco_sk_list
, sk
);
509 static int sco_sock_create(struct net
*net
, struct socket
*sock
, int protocol
,
514 BT_DBG("sock %p", sock
);
516 sock
->state
= SS_UNCONNECTED
;
518 if (sock
->type
!= SOCK_SEQPACKET
)
519 return -ESOCKTNOSUPPORT
;
521 sock
->ops
= &sco_sock_ops
;
523 sk
= sco_sock_alloc(net
, sock
, protocol
, GFP_ATOMIC
, kern
);
527 sco_sock_init(sk
, NULL
);
531 static int sco_sock_bind(struct socket
*sock
, struct sockaddr
*addr
,
534 struct sockaddr_sco
*sa
= (struct sockaddr_sco
*) addr
;
535 struct sock
*sk
= sock
->sk
;
538 if (!addr
|| addr_len
< sizeof(struct sockaddr_sco
) ||
539 addr
->sa_family
!= AF_BLUETOOTH
)
542 BT_DBG("sk %p %pMR", sk
, &sa
->sco_bdaddr
);
546 if (sk
->sk_state
!= BT_OPEN
) {
551 if (sk
->sk_type
!= SOCK_SEQPACKET
) {
556 bacpy(&sco_pi(sk
)->src
, &sa
->sco_bdaddr
);
558 sk
->sk_state
= BT_BOUND
;
565 static int sco_sock_connect(struct socket
*sock
, struct sockaddr
*addr
, int alen
, int flags
)
567 struct sockaddr_sco
*sa
= (struct sockaddr_sco
*) addr
;
568 struct sock
*sk
= sock
->sk
;
573 if (alen
< sizeof(struct sockaddr_sco
) ||
574 addr
->sa_family
!= AF_BLUETOOTH
)
577 if (sk
->sk_state
!= BT_OPEN
&& sk
->sk_state
!= BT_BOUND
)
580 if (sk
->sk_type
!= SOCK_SEQPACKET
)
585 /* Set destination address and psm */
586 bacpy(&sco_pi(sk
)->dst
, &sa
->sco_bdaddr
);
588 err
= sco_connect(sk
);
592 err
= bt_sock_wait_state(sk
, BT_CONNECTED
,
593 sock_sndtimeo(sk
, flags
& O_NONBLOCK
));
600 static int sco_sock_listen(struct socket
*sock
, int backlog
)
602 struct sock
*sk
= sock
->sk
;
603 bdaddr_t
*src
= &sco_pi(sk
)->src
;
606 BT_DBG("sk %p backlog %d", sk
, backlog
);
610 if (sk
->sk_state
!= BT_BOUND
) {
615 if (sk
->sk_type
!= SOCK_SEQPACKET
) {
620 write_lock(&sco_sk_list
.lock
);
622 if (__sco_get_sock_listen_by_addr(src
)) {
627 sk
->sk_max_ack_backlog
= backlog
;
628 sk
->sk_ack_backlog
= 0;
630 sk
->sk_state
= BT_LISTEN
;
633 write_unlock(&sco_sk_list
.lock
);
640 static int sco_sock_accept(struct socket
*sock
, struct socket
*newsock
,
641 int flags
, bool kern
)
643 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
644 struct sock
*sk
= sock
->sk
, *ch
;
650 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
652 BT_DBG("sk %p timeo %ld", sk
, timeo
);
654 /* Wait for an incoming connection. (wake-one). */
655 add_wait_queue_exclusive(sk_sleep(sk
), &wait
);
657 if (sk
->sk_state
!= BT_LISTEN
) {
662 ch
= bt_accept_dequeue(sk
, newsock
);
671 if (signal_pending(current
)) {
672 err
= sock_intr_errno(timeo
);
678 timeo
= wait_woken(&wait
, TASK_INTERRUPTIBLE
, timeo
);
681 remove_wait_queue(sk_sleep(sk
), &wait
);
686 newsock
->state
= SS_CONNECTED
;
688 BT_DBG("new socket %p", ch
);
695 static int sco_sock_getname(struct socket
*sock
, struct sockaddr
*addr
,
698 struct sockaddr_sco
*sa
= (struct sockaddr_sco
*) addr
;
699 struct sock
*sk
= sock
->sk
;
701 BT_DBG("sock %p, sk %p", sock
, sk
);
703 addr
->sa_family
= AF_BLUETOOTH
;
706 bacpy(&sa
->sco_bdaddr
, &sco_pi(sk
)->dst
);
708 bacpy(&sa
->sco_bdaddr
, &sco_pi(sk
)->src
);
710 return sizeof(struct sockaddr_sco
);
713 static int sco_sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
716 struct sock
*sk
= sock
->sk
;
719 BT_DBG("sock %p, sk %p", sock
, sk
);
721 err
= sock_error(sk
);
725 if (msg
->msg_flags
& MSG_OOB
)
730 if (sk
->sk_state
== BT_CONNECTED
)
731 err
= sco_send_frame(sk
, msg
, len
);
739 static void sco_conn_defer_accept(struct hci_conn
*conn
, u16 setting
)
741 struct hci_dev
*hdev
= conn
->hdev
;
743 BT_DBG("conn %p", conn
);
745 conn
->state
= BT_CONFIG
;
747 if (!lmp_esco_capable(hdev
)) {
748 struct hci_cp_accept_conn_req cp
;
750 bacpy(&cp
.bdaddr
, &conn
->dst
);
751 cp
.role
= 0x00; /* Ignored */
753 hci_send_cmd(hdev
, HCI_OP_ACCEPT_CONN_REQ
, sizeof(cp
), &cp
);
755 struct hci_cp_accept_sync_conn_req cp
;
757 bacpy(&cp
.bdaddr
, &conn
->dst
);
758 cp
.pkt_type
= cpu_to_le16(conn
->pkt_type
);
760 cp
.tx_bandwidth
= cpu_to_le32(0x00001f40);
761 cp
.rx_bandwidth
= cpu_to_le32(0x00001f40);
762 cp
.content_format
= cpu_to_le16(setting
);
764 switch (setting
& SCO_AIRMODE_MASK
) {
765 case SCO_AIRMODE_TRANSP
:
766 if (conn
->pkt_type
& ESCO_2EV3
)
767 cp
.max_latency
= cpu_to_le16(0x0008);
769 cp
.max_latency
= cpu_to_le16(0x000D);
770 cp
.retrans_effort
= 0x02;
772 case SCO_AIRMODE_CVSD
:
773 cp
.max_latency
= cpu_to_le16(0xffff);
774 cp
.retrans_effort
= 0xff;
778 hci_send_cmd(hdev
, HCI_OP_ACCEPT_SYNC_CONN_REQ
,
783 static int sco_sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
784 size_t len
, int flags
)
786 struct sock
*sk
= sock
->sk
;
787 struct sco_pinfo
*pi
= sco_pi(sk
);
791 if (sk
->sk_state
== BT_CONNECT2
&&
792 test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
)) {
793 sco_conn_defer_accept(pi
->conn
->hcon
, pi
->setting
);
794 sk
->sk_state
= BT_CONFIG
;
802 return bt_sock_recvmsg(sock
, msg
, len
, flags
);
805 static int sco_sock_setsockopt(struct socket
*sock
, int level
, int optname
,
806 sockptr_t optval
, unsigned int optlen
)
808 struct sock
*sk
= sock
->sk
;
810 struct bt_voice voice
;
820 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
825 if (copy_from_sockptr(&opt
, optval
, sizeof(u32
))) {
831 set_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
);
833 clear_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
);
837 if (sk
->sk_state
!= BT_OPEN
&& sk
->sk_state
!= BT_BOUND
&&
838 sk
->sk_state
!= BT_CONNECT2
) {
843 voice
.setting
= sco_pi(sk
)->setting
;
845 len
= min_t(unsigned int, sizeof(voice
), optlen
);
846 if (copy_from_sockptr(&voice
, optval
, len
)) {
851 /* Explicitly check for these values */
852 if (voice
.setting
!= BT_VOICE_TRANSPARENT
&&
853 voice
.setting
!= BT_VOICE_CVSD_16BIT
) {
858 sco_pi(sk
)->setting
= voice
.setting
;
862 if (copy_from_sockptr(&opt
, optval
, sizeof(u32
))) {
868 sco_pi(sk
)->cmsg_mask
|= SCO_CMSG_PKT_STATUS
;
870 sco_pi(sk
)->cmsg_mask
&= SCO_CMSG_PKT_STATUS
;
882 static int sco_sock_getsockopt_old(struct socket
*sock
, int optname
,
883 char __user
*optval
, int __user
*optlen
)
885 struct sock
*sk
= sock
->sk
;
886 struct sco_options opts
;
887 struct sco_conninfo cinfo
;
892 if (get_user(len
, optlen
))
899 if (sk
->sk_state
!= BT_CONNECTED
&&
900 !(sk
->sk_state
== BT_CONNECT2
&&
901 test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
))) {
906 opts
.mtu
= sco_pi(sk
)->conn
->mtu
;
908 BT_DBG("mtu %d", opts
.mtu
);
910 len
= min_t(unsigned int, len
, sizeof(opts
));
911 if (copy_to_user(optval
, (char *)&opts
, len
))
917 if (sk
->sk_state
!= BT_CONNECTED
&&
918 !(sk
->sk_state
== BT_CONNECT2
&&
919 test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
))) {
924 memset(&cinfo
, 0, sizeof(cinfo
));
925 cinfo
.hci_handle
= sco_pi(sk
)->conn
->hcon
->handle
;
926 memcpy(cinfo
.dev_class
, sco_pi(sk
)->conn
->hcon
->dev_class
, 3);
928 len
= min_t(unsigned int, len
, sizeof(cinfo
));
929 if (copy_to_user(optval
, (char *)&cinfo
, len
))
943 static int sco_sock_getsockopt(struct socket
*sock
, int level
, int optname
,
944 char __user
*optval
, int __user
*optlen
)
946 struct sock
*sk
= sock
->sk
;
948 struct bt_voice voice
;
954 if (level
== SOL_SCO
)
955 return sco_sock_getsockopt_old(sock
, optname
, optval
, optlen
);
957 if (get_user(len
, optlen
))
965 if (sk
->sk_state
!= BT_BOUND
&& sk
->sk_state
!= BT_LISTEN
) {
970 if (put_user(test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
),
971 (u32 __user
*)optval
))
977 voice
.setting
= sco_pi(sk
)->setting
;
979 len
= min_t(unsigned int, len
, sizeof(voice
));
980 if (copy_to_user(optval
, (char *)&voice
, len
))
986 if (sk
->sk_state
!= BT_CONNECTED
) {
991 phys
= hci_conn_get_phy(sco_pi(sk
)->conn
->hcon
);
993 if (put_user(phys
, (u32 __user
*) optval
))
998 pkt_status
= (sco_pi(sk
)->cmsg_mask
& SCO_CMSG_PKT_STATUS
);
1000 if (put_user(pkt_status
, (int __user
*)optval
))
1006 if (sk
->sk_state
!= BT_CONNECTED
) {
1011 if (put_user(sco_pi(sk
)->conn
->mtu
, (u32 __user
*)optval
))
1024 static int sco_sock_shutdown(struct socket
*sock
, int how
)
1026 struct sock
*sk
= sock
->sk
;
1029 BT_DBG("sock %p, sk %p", sock
, sk
);
1037 if (!sk
->sk_shutdown
) {
1038 sk
->sk_shutdown
= SHUTDOWN_MASK
;
1039 sco_sock_clear_timer(sk
);
1040 __sco_sock_close(sk
);
1042 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
&&
1043 !(current
->flags
& PF_EXITING
))
1044 err
= bt_sock_wait_state(sk
, BT_CLOSED
,
1054 static int sco_sock_release(struct socket
*sock
)
1056 struct sock
*sk
= sock
->sk
;
1059 BT_DBG("sock %p, sk %p", sock
, sk
);
1066 if (sock_flag(sk
, SOCK_LINGER
) && sk
->sk_lingertime
&&
1067 !(current
->flags
& PF_EXITING
)) {
1069 err
= bt_sock_wait_state(sk
, BT_CLOSED
, sk
->sk_lingertime
);
1078 static void sco_conn_ready(struct sco_conn
*conn
)
1080 struct sock
*parent
;
1081 struct sock
*sk
= conn
->sk
;
1083 BT_DBG("conn %p", conn
);
1086 sco_sock_clear_timer(sk
);
1088 sk
->sk_state
= BT_CONNECTED
;
1089 sk
->sk_state_change(sk
);
1092 sco_conn_lock(conn
);
1095 sco_conn_unlock(conn
);
1099 parent
= sco_get_sock_listen(&conn
->hcon
->src
);
1101 sco_conn_unlock(conn
);
1105 bh_lock_sock(parent
);
1107 sk
= sco_sock_alloc(sock_net(parent
), NULL
,
1108 BTPROTO_SCO
, GFP_ATOMIC
, 0);
1110 bh_unlock_sock(parent
);
1111 sco_conn_unlock(conn
);
1115 sco_sock_init(sk
, parent
);
1117 bacpy(&sco_pi(sk
)->src
, &conn
->hcon
->src
);
1118 bacpy(&sco_pi(sk
)->dst
, &conn
->hcon
->dst
);
1120 hci_conn_hold(conn
->hcon
);
1121 __sco_chan_add(conn
, sk
, parent
);
1123 if (test_bit(BT_SK_DEFER_SETUP
, &bt_sk(parent
)->flags
))
1124 sk
->sk_state
= BT_CONNECT2
;
1126 sk
->sk_state
= BT_CONNECTED
;
1128 /* Wake up parent */
1129 parent
->sk_data_ready(parent
);
1131 bh_unlock_sock(parent
);
1133 sco_conn_unlock(conn
);
1137 /* ----- SCO interface with lower layer (HCI) ----- */
1138 int sco_connect_ind(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, __u8
*flags
)
1143 BT_DBG("hdev %s, bdaddr %pMR", hdev
->name
, bdaddr
);
1145 /* Find listening sockets */
1146 read_lock(&sco_sk_list
.lock
);
1147 sk_for_each(sk
, &sco_sk_list
.head
) {
1148 if (sk
->sk_state
!= BT_LISTEN
)
1151 if (!bacmp(&sco_pi(sk
)->src
, &hdev
->bdaddr
) ||
1152 !bacmp(&sco_pi(sk
)->src
, BDADDR_ANY
)) {
1153 lm
|= HCI_LM_ACCEPT
;
1155 if (test_bit(BT_SK_DEFER_SETUP
, &bt_sk(sk
)->flags
))
1156 *flags
|= HCI_PROTO_DEFER
;
1160 read_unlock(&sco_sk_list
.lock
);
1165 static void sco_connect_cfm(struct hci_conn
*hcon
, __u8 status
)
1167 if (hcon
->type
!= SCO_LINK
&& hcon
->type
!= ESCO_LINK
)
1170 BT_DBG("hcon %p bdaddr %pMR status %d", hcon
, &hcon
->dst
, status
);
1173 struct sco_conn
*conn
;
1175 conn
= sco_conn_add(hcon
);
1177 sco_conn_ready(conn
);
1179 sco_conn_del(hcon
, bt_to_errno(status
));
1182 static void sco_disconn_cfm(struct hci_conn
*hcon
, __u8 reason
)
1184 if (hcon
->type
!= SCO_LINK
&& hcon
->type
!= ESCO_LINK
)
1187 BT_DBG("hcon %p reason %d", hcon
, reason
);
1189 sco_conn_del(hcon
, bt_to_errno(reason
));
1192 void sco_recv_scodata(struct hci_conn
*hcon
, struct sk_buff
*skb
)
1194 struct sco_conn
*conn
= hcon
->sco_data
;
1199 BT_DBG("conn %p len %d", conn
, skb
->len
);
1202 sco_recv_frame(conn
, skb
);
1210 static struct hci_cb sco_cb
= {
1212 .connect_cfm
= sco_connect_cfm
,
1213 .disconn_cfm
= sco_disconn_cfm
,
1216 static int sco_debugfs_show(struct seq_file
*f
, void *p
)
1220 read_lock(&sco_sk_list
.lock
);
1222 sk_for_each(sk
, &sco_sk_list
.head
) {
1223 seq_printf(f
, "%pMR %pMR %d\n", &sco_pi(sk
)->src
,
1224 &sco_pi(sk
)->dst
, sk
->sk_state
);
1227 read_unlock(&sco_sk_list
.lock
);
1232 DEFINE_SHOW_ATTRIBUTE(sco_debugfs
);
1234 static struct dentry
*sco_debugfs
;
1236 static const struct proto_ops sco_sock_ops
= {
1237 .family
= PF_BLUETOOTH
,
1238 .owner
= THIS_MODULE
,
1239 .release
= sco_sock_release
,
1240 .bind
= sco_sock_bind
,
1241 .connect
= sco_sock_connect
,
1242 .listen
= sco_sock_listen
,
1243 .accept
= sco_sock_accept
,
1244 .getname
= sco_sock_getname
,
1245 .sendmsg
= sco_sock_sendmsg
,
1246 .recvmsg
= sco_sock_recvmsg
,
1247 .poll
= bt_sock_poll
,
1248 .ioctl
= bt_sock_ioctl
,
1249 .gettstamp
= sock_gettstamp
,
1250 .mmap
= sock_no_mmap
,
1251 .socketpair
= sock_no_socketpair
,
1252 .shutdown
= sco_sock_shutdown
,
1253 .setsockopt
= sco_sock_setsockopt
,
1254 .getsockopt
= sco_sock_getsockopt
1257 static const struct net_proto_family sco_sock_family_ops
= {
1258 .family
= PF_BLUETOOTH
,
1259 .owner
= THIS_MODULE
,
1260 .create
= sco_sock_create
,
1263 int __init
sco_init(void)
1267 BUILD_BUG_ON(sizeof(struct sockaddr_sco
) > sizeof(struct sockaddr
));
1269 err
= proto_register(&sco_proto
, 0);
1273 err
= bt_sock_register(BTPROTO_SCO
, &sco_sock_family_ops
);
1275 BT_ERR("SCO socket registration failed");
1279 err
= bt_procfs_init(&init_net
, "sco", &sco_sk_list
, NULL
);
1281 BT_ERR("Failed to create SCO proc file");
1282 bt_sock_unregister(BTPROTO_SCO
);
1286 BT_INFO("SCO socket layer initialized");
1288 hci_register_cb(&sco_cb
);
1290 if (IS_ERR_OR_NULL(bt_debugfs
))
1293 sco_debugfs
= debugfs_create_file("sco", 0444, bt_debugfs
,
1294 NULL
, &sco_debugfs_fops
);
1299 proto_unregister(&sco_proto
);
1305 bt_procfs_cleanup(&init_net
, "sco");
1307 debugfs_remove(sco_debugfs
);
1309 hci_unregister_cb(&sco_cb
);
1311 bt_sock_unregister(BTPROTO_SCO
);
1313 proto_unregister(&sco_proto
);
1316 module_param(disable_esco
, bool, 0644);
1317 MODULE_PARM_DESC(disable_esco
, "Disable eSCO connection creation");