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;
51 struct delayed_work timeout_work;
56 #define sco_conn_lock(c) spin_lock(&c->lock)
57 #define sco_conn_unlock(c) spin_unlock(&c->lock)
59 static void sco_sock_close(struct sock *sk);
60 static void sco_sock_kill(struct sock *sk);
62 /* ----- SCO socket info ----- */
63 #define sco_pi(sk) ((struct sco_pinfo *) sk)
72 struct bt_codec codec;
73 struct sco_conn *conn;
76 /* ---- SCO timers ---- */
77 #define SCO_CONN_TIMEOUT (HZ * 40)
78 #define SCO_DISCONN_TIMEOUT (HZ * 2)
80 static void sco_sock_timeout(struct work_struct *work)
82 struct sco_conn *conn = container_of(work, struct sco_conn,
90 sco_conn_unlock(conn);
95 BT_DBG("sock %p state %d", sk, sk->sk_state);
98 sk->sk_err = ETIMEDOUT;
99 sk->sk_state_change(sk);
104 static void sco_sock_set_timer(struct sock *sk, long timeout)
106 if (!sco_pi(sk)->conn)
109 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
110 cancel_delayed_work(&sco_pi(sk)->conn->timeout_work);
111 schedule_delayed_work(&sco_pi(sk)->conn->timeout_work, timeout);
114 static void sco_sock_clear_timer(struct sock *sk)
116 if (!sco_pi(sk)->conn)
119 BT_DBG("sock %p state %d", sk, sk->sk_state);
120 cancel_delayed_work(&sco_pi(sk)->conn->timeout_work);
123 /* ---- SCO connections ---- */
124 static struct sco_conn *sco_conn_add(struct hci_conn *hcon)
126 struct hci_dev *hdev = hcon->hdev;
127 struct sco_conn *conn = hcon->sco_data;
132 conn = kzalloc(sizeof(struct sco_conn), GFP_KERNEL);
136 spin_lock_init(&conn->lock);
137 INIT_DELAYED_WORK(&conn->timeout_work, sco_sock_timeout);
139 hcon->sco_data = conn;
142 if (hdev->sco_mtu > 0)
143 conn->mtu = hdev->sco_mtu;
147 BT_DBG("hcon %p conn %p", hcon, conn);
153 * Must be called on the locked socket. */
154 static void sco_chan_del(struct sock *sk, int err)
156 struct sco_conn *conn;
158 conn = sco_pi(sk)->conn;
160 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
165 sco_pi(sk)->conn = NULL;
166 sco_conn_unlock(conn);
169 hci_conn_drop(conn->hcon);
172 sk->sk_state = BT_CLOSED;
174 sk->sk_state_change(sk);
176 sock_set_flag(sk, SOCK_ZAPPED);
179 static void sco_conn_del(struct hci_conn *hcon, int err)
181 struct sco_conn *conn = hcon->sco_data;
187 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
194 sco_conn_unlock(conn);
198 sco_sock_clear_timer(sk);
199 sco_chan_del(sk, err);
204 /* Ensure no more work items will run before freeing conn. */
205 cancel_delayed_work_sync(&conn->timeout_work);
207 hcon->sco_data = NULL;
211 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk,
214 BT_DBG("conn %p", conn);
216 sco_pi(sk)->conn = conn;
220 bt_accept_enqueue(parent, sk, true);
223 static int sco_chan_add(struct sco_conn *conn, struct sock *sk,
232 __sco_chan_add(conn, sk, parent);
234 sco_conn_unlock(conn);
238 static int sco_connect(struct hci_dev *hdev, struct sock *sk)
240 struct sco_conn *conn;
241 struct hci_conn *hcon;
244 BT_DBG("%pMR -> %pMR", &sco_pi(sk)->src, &sco_pi(sk)->dst);
246 if (lmp_esco_capable(hdev) && !disable_esco)
251 if (sco_pi(sk)->setting == BT_VOICE_TRANSPARENT &&
252 (!lmp_transp_capable(hdev) || !lmp_esco_capable(hdev)))
255 hcon = hci_connect_sco(hdev, type, &sco_pi(sk)->dst,
256 sco_pi(sk)->setting, &sco_pi(sk)->codec);
258 return PTR_ERR(hcon);
260 conn = sco_conn_add(hcon);
266 /* Update source addr of the socket */
267 bacpy(&sco_pi(sk)->src, &hcon->src);
269 err = sco_chan_add(conn, sk, NULL);
273 if (hcon->state == BT_CONNECTED) {
274 sco_sock_clear_timer(sk);
275 sk->sk_state = BT_CONNECTED;
277 sk->sk_state = BT_CONNECT;
278 sco_sock_set_timer(sk, sk->sk_sndtimeo);
284 static int sco_send_frame(struct sock *sk, struct sk_buff *skb)
286 struct sco_conn *conn = sco_pi(sk)->conn;
289 /* Check outgoing MTU */
293 BT_DBG("sk %p len %d", sk, 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 %u", 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);
441 /* Must be called on unlocked socket. */
442 static void sco_sock_close(struct sock *sk)
445 sco_sock_clear_timer(sk);
446 __sco_sock_close(sk);
450 static void sco_skb_put_cmsg(struct sk_buff *skb, struct msghdr *msg,
453 if (sco_pi(sk)->cmsg_mask & SCO_CMSG_PKT_STATUS)
454 put_cmsg(msg, SOL_BLUETOOTH, BT_SCM_PKT_STATUS,
455 sizeof(bt_cb(skb)->sco.pkt_status),
456 &bt_cb(skb)->sco.pkt_status);
459 static void sco_sock_init(struct sock *sk, struct sock *parent)
464 sk->sk_type = parent->sk_type;
465 bt_sk(sk)->flags = bt_sk(parent)->flags;
466 security_sk_clone(parent, sk);
468 bt_sk(sk)->skb_put_cmsg = sco_skb_put_cmsg;
472 static struct proto sco_proto = {
474 .owner = THIS_MODULE,
475 .obj_size = sizeof(struct sco_pinfo)
478 static struct sock *sco_sock_alloc(struct net *net, struct socket *sock,
479 int proto, gfp_t prio, int kern)
483 sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto, kern);
487 sock_init_data(sock, sk);
488 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
490 sk->sk_destruct = sco_sock_destruct;
491 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
493 sock_reset_flag(sk, SOCK_ZAPPED);
495 sk->sk_protocol = proto;
496 sk->sk_state = BT_OPEN;
498 sco_pi(sk)->setting = BT_VOICE_CVSD_16BIT;
499 sco_pi(sk)->codec.id = BT_CODEC_CVSD;
500 sco_pi(sk)->codec.cid = 0xffff;
501 sco_pi(sk)->codec.vid = 0xffff;
502 sco_pi(sk)->codec.data_path = 0x00;
504 bt_sock_link(&sco_sk_list, sk);
508 static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
513 BT_DBG("sock %p", sock);
515 sock->state = SS_UNCONNECTED;
517 if (sock->type != SOCK_SEQPACKET)
518 return -ESOCKTNOSUPPORT;
520 sock->ops = &sco_sock_ops;
522 sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern);
526 sco_sock_init(sk, NULL);
530 static int sco_sock_bind(struct socket *sock, struct sockaddr *addr,
533 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
534 struct sock *sk = sock->sk;
537 if (!addr || addr_len < sizeof(struct sockaddr_sco) ||
538 addr->sa_family != AF_BLUETOOTH)
541 BT_DBG("sk %p %pMR", sk, &sa->sco_bdaddr);
545 if (sk->sk_state != BT_OPEN) {
550 if (sk->sk_type != SOCK_SEQPACKET) {
555 bacpy(&sco_pi(sk)->src, &sa->sco_bdaddr);
557 sk->sk_state = BT_BOUND;
564 static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
566 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
567 struct sock *sk = sock->sk;
568 struct hci_dev *hdev;
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)
583 hdev = hci_get_route(&sa->sco_bdaddr, &sco_pi(sk)->src, BDADDR_BREDR);
585 return -EHOSTUNREACH;
590 /* Set destination address and psm */
591 bacpy(&sco_pi(sk)->dst, &sa->sco_bdaddr);
593 err = sco_connect(hdev, sk);
594 hci_dev_unlock(hdev);
599 err = bt_sock_wait_state(sk, BT_CONNECTED,
600 sock_sndtimeo(sk, flags & O_NONBLOCK));
607 static int sco_sock_listen(struct socket *sock, int backlog)
609 struct sock *sk = sock->sk;
610 bdaddr_t *src = &sco_pi(sk)->src;
613 BT_DBG("sk %p backlog %d", sk, backlog);
617 if (sk->sk_state != BT_BOUND) {
622 if (sk->sk_type != SOCK_SEQPACKET) {
627 write_lock(&sco_sk_list.lock);
629 if (__sco_get_sock_listen_by_addr(src)) {
634 sk->sk_max_ack_backlog = backlog;
635 sk->sk_ack_backlog = 0;
637 sk->sk_state = BT_LISTEN;
640 write_unlock(&sco_sk_list.lock);
647 static int sco_sock_accept(struct socket *sock, struct socket *newsock,
648 int flags, bool kern)
650 DEFINE_WAIT_FUNC(wait, woken_wake_function);
651 struct sock *sk = sock->sk, *ch;
657 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
659 BT_DBG("sk %p timeo %ld", sk, timeo);
661 /* Wait for an incoming connection. (wake-one). */
662 add_wait_queue_exclusive(sk_sleep(sk), &wait);
664 if (sk->sk_state != BT_LISTEN) {
669 ch = bt_accept_dequeue(sk, newsock);
678 if (signal_pending(current)) {
679 err = sock_intr_errno(timeo);
685 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
688 remove_wait_queue(sk_sleep(sk), &wait);
693 newsock->state = SS_CONNECTED;
695 BT_DBG("new socket %p", ch);
702 static int sco_sock_getname(struct socket *sock, struct sockaddr *addr,
705 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
706 struct sock *sk = sock->sk;
708 BT_DBG("sock %p, sk %p", sock, sk);
710 addr->sa_family = AF_BLUETOOTH;
713 bacpy(&sa->sco_bdaddr, &sco_pi(sk)->dst);
715 bacpy(&sa->sco_bdaddr, &sco_pi(sk)->src);
717 return sizeof(struct sockaddr_sco);
720 static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg,
723 struct sock *sk = sock->sk;
727 BT_DBG("sock %p, sk %p", sock, sk);
729 err = sock_error(sk);
733 if (msg->msg_flags & MSG_OOB)
736 skb = bt_skb_sendmsg(sk, msg, len, len, 0, 0);
742 if (sk->sk_state == BT_CONNECTED)
743 err = sco_send_frame(sk, skb);
754 static void sco_conn_defer_accept(struct hci_conn *conn, u16 setting)
756 struct hci_dev *hdev = conn->hdev;
758 BT_DBG("conn %p", conn);
760 conn->state = BT_CONFIG;
762 if (!lmp_esco_capable(hdev)) {
763 struct hci_cp_accept_conn_req cp;
765 bacpy(&cp.bdaddr, &conn->dst);
766 cp.role = 0x00; /* Ignored */
768 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
770 struct hci_cp_accept_sync_conn_req cp;
772 bacpy(&cp.bdaddr, &conn->dst);
773 cp.pkt_type = cpu_to_le16(conn->pkt_type);
775 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
776 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
777 cp.content_format = cpu_to_le16(setting);
779 switch (setting & SCO_AIRMODE_MASK) {
780 case SCO_AIRMODE_TRANSP:
781 if (conn->pkt_type & ESCO_2EV3)
782 cp.max_latency = cpu_to_le16(0x0008);
784 cp.max_latency = cpu_to_le16(0x000D);
785 cp.retrans_effort = 0x02;
787 case SCO_AIRMODE_CVSD:
788 cp.max_latency = cpu_to_le16(0xffff);
789 cp.retrans_effort = 0xff;
792 /* use CVSD settings as fallback */
793 cp.max_latency = cpu_to_le16(0xffff);
794 cp.retrans_effort = 0xff;
798 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
803 static int sco_sock_recvmsg(struct socket *sock, struct msghdr *msg,
804 size_t len, int flags)
806 struct sock *sk = sock->sk;
807 struct sco_pinfo *pi = sco_pi(sk);
811 if (sk->sk_state == BT_CONNECT2 &&
812 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
813 sco_conn_defer_accept(pi->conn->hcon, pi->setting);
814 sk->sk_state = BT_CONFIG;
822 return bt_sock_recvmsg(sock, msg, len, flags);
825 static int sco_sock_setsockopt(struct socket *sock, int level, int optname,
826 sockptr_t optval, unsigned int optlen)
828 struct sock *sk = sock->sk;
830 struct bt_voice voice;
832 struct bt_codecs *codecs;
833 struct hci_dev *hdev;
843 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
848 if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
854 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
856 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
860 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
861 sk->sk_state != BT_CONNECT2) {
866 voice.setting = sco_pi(sk)->setting;
868 len = min_t(unsigned int, sizeof(voice), optlen);
869 if (copy_from_sockptr(&voice, optval, len)) {
874 /* Explicitly check for these values */
875 if (voice.setting != BT_VOICE_TRANSPARENT &&
876 voice.setting != BT_VOICE_CVSD_16BIT) {
881 sco_pi(sk)->setting = voice.setting;
882 hdev = hci_get_route(&sco_pi(sk)->dst, &sco_pi(sk)->src,
888 if (enhanced_sco_capable(hdev) &&
889 voice.setting == BT_VOICE_TRANSPARENT)
890 sco_pi(sk)->codec.id = BT_CODEC_TRANSPARENT;
895 if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
901 sco_pi(sk)->cmsg_mask |= SCO_CMSG_PKT_STATUS;
903 sco_pi(sk)->cmsg_mask &= SCO_CMSG_PKT_STATUS;
907 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
908 sk->sk_state != BT_CONNECT2) {
913 hdev = hci_get_route(&sco_pi(sk)->dst, &sco_pi(sk)->src,
920 if (!hci_dev_test_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED)) {
926 if (!hdev->get_data_path_id) {
932 if (optlen < sizeof(struct bt_codecs) ||
933 optlen > sizeof(buffer)) {
939 if (copy_from_sockptr(buffer, optval, optlen)) {
945 codecs = (void *)buffer;
947 if (codecs->num_codecs > 1) {
953 sco_pi(sk)->codec = codecs->codecs[0];
966 static int sco_sock_getsockopt_old(struct socket *sock, int optname,
967 char __user *optval, int __user *optlen)
969 struct sock *sk = sock->sk;
970 struct sco_options opts;
971 struct sco_conninfo cinfo;
976 if (get_user(len, optlen))
983 if (sk->sk_state != BT_CONNECTED &&
984 !(sk->sk_state == BT_CONNECT2 &&
985 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
990 opts.mtu = sco_pi(sk)->conn->mtu;
992 BT_DBG("mtu %u", opts.mtu);
994 len = min_t(unsigned int, len, sizeof(opts));
995 if (copy_to_user(optval, (char *)&opts, len))
1001 if (sk->sk_state != BT_CONNECTED &&
1002 !(sk->sk_state == BT_CONNECT2 &&
1003 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
1008 memset(&cinfo, 0, sizeof(cinfo));
1009 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
1010 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
1012 len = min_t(unsigned int, len, sizeof(cinfo));
1013 if (copy_to_user(optval, (char *)&cinfo, len))
1027 static int sco_sock_getsockopt(struct socket *sock, int level, int optname,
1028 char __user *optval, int __user *optlen)
1030 struct sock *sk = sock->sk;
1032 struct bt_voice voice;
1036 struct codec_list *c;
1037 u8 num_codecs, i, __user *ptr;
1038 struct hci_dev *hdev;
1039 struct hci_codec_caps *caps;
1040 struct bt_codec codec;
1042 BT_DBG("sk %p", sk);
1044 if (level == SOL_SCO)
1045 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
1047 if (get_user(len, optlen))
1054 case BT_DEFER_SETUP:
1055 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
1060 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
1061 (u32 __user *)optval))
1067 voice.setting = sco_pi(sk)->setting;
1069 len = min_t(unsigned int, len, sizeof(voice));
1070 if (copy_to_user(optval, (char *)&voice, len))
1076 if (sk->sk_state != BT_CONNECTED) {
1081 phys = hci_conn_get_phy(sco_pi(sk)->conn->hcon);
1083 if (put_user(phys, (u32 __user *) optval))
1088 pkt_status = (sco_pi(sk)->cmsg_mask & SCO_CMSG_PKT_STATUS);
1090 if (put_user(pkt_status, (int __user *)optval))
1096 if (sk->sk_state != BT_CONNECTED) {
1101 if (put_user(sco_pi(sk)->conn->mtu, (u32 __user *)optval))
1109 hdev = hci_get_route(&sco_pi(sk)->dst, &sco_pi(sk)->src, BDADDR_BREDR);
1115 if (!hci_dev_test_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED)) {
1121 if (!hdev->get_data_path_id) {
1127 /* find total buffer size required to copy codec + caps */
1129 list_for_each_entry(c, &hdev->local_codecs, list) {
1130 if (c->transport != HCI_TRANSPORT_SCO_ESCO)
1133 for (i = 0, caps = c->caps; i < c->num_caps; i++) {
1134 buf_len += 1 + caps->len;
1135 caps = (void *)&caps->data[caps->len];
1137 buf_len += sizeof(struct bt_codec);
1139 hci_dev_unlock(hdev);
1141 buf_len += sizeof(struct bt_codecs);
1142 if (buf_len > len) {
1149 if (put_user(num_codecs, ptr)) {
1154 ptr += sizeof(num_codecs);
1156 /* Iterate all the codecs supported over SCO and populate
1160 list_for_each_entry(c, &hdev->local_codecs, list) {
1161 if (c->transport != HCI_TRANSPORT_SCO_ESCO)
1167 err = hdev->get_data_path_id(hdev, &codec.data_path);
1170 codec.num_caps = c->num_caps;
1171 if (copy_to_user(ptr, &codec, sizeof(codec))) {
1175 ptr += sizeof(codec);
1177 /* find codec capabilities data length */
1179 for (i = 0, caps = c->caps; i < c->num_caps; i++) {
1180 len += 1 + caps->len;
1181 caps = (void *)&caps->data[caps->len];
1184 /* copy codec capabilities data */
1185 if (len && copy_to_user(ptr, c->caps, len)) {
1192 if (!err && put_user(buf_len, optlen))
1195 hci_dev_unlock(hdev);
1209 static int sco_sock_shutdown(struct socket *sock, int how)
1211 struct sock *sk = sock->sk;
1214 BT_DBG("sock %p, sk %p", sock, sk);
1222 if (!sk->sk_shutdown) {
1223 sk->sk_shutdown = SHUTDOWN_MASK;
1224 sco_sock_clear_timer(sk);
1225 __sco_sock_close(sk);
1227 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1228 !(current->flags & PF_EXITING))
1229 err = bt_sock_wait_state(sk, BT_CLOSED,
1239 static int sco_sock_release(struct socket *sock)
1241 struct sock *sk = sock->sk;
1244 BT_DBG("sock %p, sk %p", sock, sk);
1251 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1252 !(current->flags & PF_EXITING)) {
1254 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1263 static void sco_conn_ready(struct sco_conn *conn)
1265 struct sock *parent;
1266 struct sock *sk = conn->sk;
1268 BT_DBG("conn %p", conn);
1272 sco_sock_clear_timer(sk);
1273 sk->sk_state = BT_CONNECTED;
1274 sk->sk_state_change(sk);
1277 sco_conn_lock(conn);
1280 sco_conn_unlock(conn);
1284 parent = sco_get_sock_listen(&conn->hcon->src);
1286 sco_conn_unlock(conn);
1292 sk = sco_sock_alloc(sock_net(parent), NULL,
1293 BTPROTO_SCO, GFP_ATOMIC, 0);
1295 release_sock(parent);
1296 sco_conn_unlock(conn);
1300 sco_sock_init(sk, parent);
1302 bacpy(&sco_pi(sk)->src, &conn->hcon->src);
1303 bacpy(&sco_pi(sk)->dst, &conn->hcon->dst);
1305 hci_conn_hold(conn->hcon);
1306 __sco_chan_add(conn, sk, parent);
1308 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
1309 sk->sk_state = BT_CONNECT2;
1311 sk->sk_state = BT_CONNECTED;
1313 /* Wake up parent */
1314 parent->sk_data_ready(parent);
1316 release_sock(parent);
1318 sco_conn_unlock(conn);
1322 /* ----- SCO interface with lower layer (HCI) ----- */
1323 int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
1328 BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
1330 /* Find listening sockets */
1331 read_lock(&sco_sk_list.lock);
1332 sk_for_each(sk, &sco_sk_list.head) {
1333 if (sk->sk_state != BT_LISTEN)
1336 if (!bacmp(&sco_pi(sk)->src, &hdev->bdaddr) ||
1337 !bacmp(&sco_pi(sk)->src, BDADDR_ANY)) {
1338 lm |= HCI_LM_ACCEPT;
1340 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
1341 *flags |= HCI_PROTO_DEFER;
1345 read_unlock(&sco_sk_list.lock);
1350 static void sco_connect_cfm(struct hci_conn *hcon, __u8 status)
1352 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
1355 BT_DBG("hcon %p bdaddr %pMR status %u", hcon, &hcon->dst, status);
1358 struct sco_conn *conn;
1360 conn = sco_conn_add(hcon);
1362 sco_conn_ready(conn);
1364 sco_conn_del(hcon, bt_to_errno(status));
1367 static void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
1369 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
1372 BT_DBG("hcon %p reason %d", hcon, reason);
1374 sco_conn_del(hcon, bt_to_errno(reason));
1377 void sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
1379 struct sco_conn *conn = hcon->sco_data;
1384 BT_DBG("conn %p len %u", conn, skb->len);
1387 sco_recv_frame(conn, skb);
1395 static struct hci_cb sco_cb = {
1397 .connect_cfm = sco_connect_cfm,
1398 .disconn_cfm = sco_disconn_cfm,
1401 static int sco_debugfs_show(struct seq_file *f, void *p)
1405 read_lock(&sco_sk_list.lock);
1407 sk_for_each(sk, &sco_sk_list.head) {
1408 seq_printf(f, "%pMR %pMR %d\n", &sco_pi(sk)->src,
1409 &sco_pi(sk)->dst, sk->sk_state);
1412 read_unlock(&sco_sk_list.lock);
1417 DEFINE_SHOW_ATTRIBUTE(sco_debugfs);
1419 static struct dentry *sco_debugfs;
1421 static const struct proto_ops sco_sock_ops = {
1422 .family = PF_BLUETOOTH,
1423 .owner = THIS_MODULE,
1424 .release = sco_sock_release,
1425 .bind = sco_sock_bind,
1426 .connect = sco_sock_connect,
1427 .listen = sco_sock_listen,
1428 .accept = sco_sock_accept,
1429 .getname = sco_sock_getname,
1430 .sendmsg = sco_sock_sendmsg,
1431 .recvmsg = sco_sock_recvmsg,
1432 .poll = bt_sock_poll,
1433 .ioctl = bt_sock_ioctl,
1434 .gettstamp = sock_gettstamp,
1435 .mmap = sock_no_mmap,
1436 .socketpair = sock_no_socketpair,
1437 .shutdown = sco_sock_shutdown,
1438 .setsockopt = sco_sock_setsockopt,
1439 .getsockopt = sco_sock_getsockopt
1442 static const struct net_proto_family sco_sock_family_ops = {
1443 .family = PF_BLUETOOTH,
1444 .owner = THIS_MODULE,
1445 .create = sco_sock_create,
1448 int __init sco_init(void)
1452 BUILD_BUG_ON(sizeof(struct sockaddr_sco) > sizeof(struct sockaddr));
1454 err = proto_register(&sco_proto, 0);
1458 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1460 BT_ERR("SCO socket registration failed");
1464 err = bt_procfs_init(&init_net, "sco", &sco_sk_list, NULL);
1466 BT_ERR("Failed to create SCO proc file");
1467 bt_sock_unregister(BTPROTO_SCO);
1471 BT_INFO("SCO socket layer initialized");
1473 hci_register_cb(&sco_cb);
1475 if (IS_ERR_OR_NULL(bt_debugfs))
1478 sco_debugfs = debugfs_create_file("sco", 0444, bt_debugfs,
1479 NULL, &sco_debugfs_fops);
1484 proto_unregister(&sco_proto);
1490 bt_procfs_cleanup(&init_net, "sco");
1492 debugfs_remove(sco_debugfs);
1494 hci_unregister_cb(&sco_cb);
1496 bt_sock_unregister(BTPROTO_SCO);
1498 proto_unregister(&sco_proto);
1501 module_param(disable_esco, bool, 0644);
1502 MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");