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)
71 struct bt_codec codec;
72 struct sco_conn *conn;
75 /* ---- SCO timers ---- */
76 #define SCO_CONN_TIMEOUT (HZ * 40)
77 #define SCO_DISCONN_TIMEOUT (HZ * 2)
79 static void sco_sock_timeout(struct work_struct *work)
81 struct sco_conn *conn = container_of(work, struct sco_conn,
89 sco_conn_unlock(conn);
94 BT_DBG("sock %p state %d", sk, sk->sk_state);
97 sk->sk_err = ETIMEDOUT;
98 sk->sk_state_change(sk);
103 static void sco_sock_set_timer(struct sock *sk, long timeout)
105 if (!sco_pi(sk)->conn)
108 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
109 cancel_delayed_work(&sco_pi(sk)->conn->timeout_work);
110 schedule_delayed_work(&sco_pi(sk)->conn->timeout_work, timeout);
113 static void sco_sock_clear_timer(struct sock *sk)
115 if (!sco_pi(sk)->conn)
118 BT_DBG("sock %p state %d", sk, sk->sk_state);
119 cancel_delayed_work(&sco_pi(sk)->conn->timeout_work);
122 /* ---- SCO connections ---- */
123 static struct sco_conn *sco_conn_add(struct hci_conn *hcon)
125 struct hci_dev *hdev = hcon->hdev;
126 struct sco_conn *conn = hcon->sco_data;
134 conn = kzalloc(sizeof(struct sco_conn), GFP_KERNEL);
138 spin_lock_init(&conn->lock);
139 INIT_DELAYED_WORK(&conn->timeout_work, sco_sock_timeout);
141 hcon->sco_data = conn;
144 if (hdev->sco_mtu > 0)
145 conn->mtu = hdev->sco_mtu;
149 BT_DBG("hcon %p conn %p", hcon, conn);
155 * Must be called on the locked socket. */
156 static void sco_chan_del(struct sock *sk, int err)
158 struct sco_conn *conn;
160 conn = sco_pi(sk)->conn;
162 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
167 sco_pi(sk)->conn = NULL;
168 sco_conn_unlock(conn);
171 hci_conn_drop(conn->hcon);
174 sk->sk_state = BT_CLOSED;
176 sk->sk_state_change(sk);
178 sock_set_flag(sk, SOCK_ZAPPED);
181 static void sco_conn_del(struct hci_conn *hcon, int err)
183 struct sco_conn *conn = hcon->sco_data;
189 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
196 sco_conn_unlock(conn);
200 sco_sock_clear_timer(sk);
201 sco_chan_del(sk, err);
206 /* Ensure no more work items will run before freeing conn. */
207 cancel_delayed_work_sync(&conn->timeout_work);
209 hcon->sco_data = NULL;
213 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk,
216 BT_DBG("conn %p", conn);
218 sco_pi(sk)->conn = conn;
222 bt_accept_enqueue(parent, sk, true);
225 static int sco_chan_add(struct sco_conn *conn, struct sock *sk,
234 __sco_chan_add(conn, sk, parent);
236 sco_conn_unlock(conn);
240 static int sco_connect(struct sock *sk)
242 struct sco_conn *conn;
243 struct hci_conn *hcon;
244 struct hci_dev *hdev;
247 BT_DBG("%pMR -> %pMR", &sco_pi(sk)->src, &sco_pi(sk)->dst);
249 hdev = hci_get_route(&sco_pi(sk)->dst, &sco_pi(sk)->src, BDADDR_BREDR);
251 return -EHOSTUNREACH;
255 if (lmp_esco_capable(hdev) && !disable_esco)
260 if (sco_pi(sk)->setting == BT_VOICE_TRANSPARENT &&
261 (!lmp_transp_capable(hdev) || !lmp_esco_capable(hdev))) {
266 hcon = hci_connect_sco(hdev, type, &sco_pi(sk)->dst,
267 sco_pi(sk)->setting, &sco_pi(sk)->codec);
273 conn = sco_conn_add(hcon);
282 err = sco_chan_add(conn, sk, NULL);
288 /* Update source addr of the socket */
289 bacpy(&sco_pi(sk)->src, &hcon->src);
291 if (hcon->state == BT_CONNECTED) {
292 sco_sock_clear_timer(sk);
293 sk->sk_state = BT_CONNECTED;
295 sk->sk_state = BT_CONNECT;
296 sco_sock_set_timer(sk, sk->sk_sndtimeo);
302 hci_dev_unlock(hdev);
307 static int sco_send_frame(struct sock *sk, struct sk_buff *skb)
309 struct sco_conn *conn = sco_pi(sk)->conn;
312 /* Check outgoing MTU */
316 BT_DBG("sk %p len %d", sk, len);
318 hci_send_sco(conn->hcon, skb);
323 static void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
329 sco_conn_unlock(conn);
334 BT_DBG("sk %p len %u", sk, skb->len);
336 if (sk->sk_state != BT_CONNECTED)
339 if (!sock_queue_rcv_skb(sk, skb))
346 /* -------- Socket interface ---------- */
347 static struct sock *__sco_get_sock_listen_by_addr(bdaddr_t *ba)
351 sk_for_each(sk, &sco_sk_list.head) {
352 if (sk->sk_state != BT_LISTEN)
355 if (!bacmp(&sco_pi(sk)->src, ba))
362 /* Find socket listening on source bdaddr.
363 * Returns closest match.
365 static struct sock *sco_get_sock_listen(bdaddr_t *src)
367 struct sock *sk = NULL, *sk1 = NULL;
369 read_lock(&sco_sk_list.lock);
371 sk_for_each(sk, &sco_sk_list.head) {
372 if (sk->sk_state != BT_LISTEN)
376 if (!bacmp(&sco_pi(sk)->src, src))
380 if (!bacmp(&sco_pi(sk)->src, BDADDR_ANY))
384 read_unlock(&sco_sk_list.lock);
386 return sk ? sk : sk1;
389 static void sco_sock_destruct(struct sock *sk)
393 skb_queue_purge(&sk->sk_receive_queue);
394 skb_queue_purge(&sk->sk_write_queue);
397 static void sco_sock_cleanup_listen(struct sock *parent)
401 BT_DBG("parent %p", parent);
403 /* Close not yet accepted channels */
404 while ((sk = bt_accept_dequeue(parent, NULL))) {
409 parent->sk_state = BT_CLOSED;
410 sock_set_flag(parent, SOCK_ZAPPED);
413 /* Kill socket (only if zapped and orphan)
414 * Must be called on unlocked socket.
416 static void sco_sock_kill(struct sock *sk)
418 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
421 BT_DBG("sk %p state %d", sk, sk->sk_state);
423 /* Kill poor orphan */
424 bt_sock_unlink(&sco_sk_list, sk);
425 sock_set_flag(sk, SOCK_DEAD);
429 static void __sco_sock_close(struct sock *sk)
431 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
433 switch (sk->sk_state) {
435 sco_sock_cleanup_listen(sk);
440 if (sco_pi(sk)->conn->hcon) {
441 sk->sk_state = BT_DISCONN;
442 sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT);
443 sco_conn_lock(sco_pi(sk)->conn);
444 hci_conn_drop(sco_pi(sk)->conn->hcon);
445 sco_pi(sk)->conn->hcon = NULL;
446 sco_conn_unlock(sco_pi(sk)->conn);
448 sco_chan_del(sk, ECONNRESET);
454 sco_chan_del(sk, ECONNRESET);
458 sock_set_flag(sk, SOCK_ZAPPED);
464 /* Must be called on unlocked socket. */
465 static void sco_sock_close(struct sock *sk)
468 sco_sock_clear_timer(sk);
469 __sco_sock_close(sk);
473 static void sco_sock_init(struct sock *sk, struct sock *parent)
478 sk->sk_type = parent->sk_type;
479 bt_sk(sk)->flags = bt_sk(parent)->flags;
480 security_sk_clone(parent, sk);
484 static struct proto sco_proto = {
486 .owner = THIS_MODULE,
487 .obj_size = sizeof(struct sco_pinfo)
490 static struct sock *sco_sock_alloc(struct net *net, struct socket *sock,
491 int proto, gfp_t prio, int kern)
495 sk = bt_sock_alloc(net, sock, &sco_proto, proto, prio, kern);
499 sk->sk_destruct = sco_sock_destruct;
500 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
502 sco_pi(sk)->setting = BT_VOICE_CVSD_16BIT;
503 sco_pi(sk)->codec.id = BT_CODEC_CVSD;
504 sco_pi(sk)->codec.cid = 0xffff;
505 sco_pi(sk)->codec.vid = 0xffff;
506 sco_pi(sk)->codec.data_path = 0x00;
508 bt_sock_link(&sco_sk_list, sk);
512 static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
517 BT_DBG("sock %p", sock);
519 sock->state = SS_UNCONNECTED;
521 if (sock->type != SOCK_SEQPACKET)
522 return -ESOCKTNOSUPPORT;
524 sock->ops = &sco_sock_ops;
526 sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern);
530 sco_sock_init(sk, NULL);
534 static int sco_sock_bind(struct socket *sock, struct sockaddr *addr,
537 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
538 struct sock *sk = sock->sk;
541 if (!addr || addr_len < sizeof(struct sockaddr_sco) ||
542 addr->sa_family != AF_BLUETOOTH)
545 BT_DBG("sk %p %pMR", sk, &sa->sco_bdaddr);
549 if (sk->sk_state != BT_OPEN) {
554 if (sk->sk_type != SOCK_SEQPACKET) {
559 bacpy(&sco_pi(sk)->src, &sa->sco_bdaddr);
561 sk->sk_state = BT_BOUND;
568 static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
570 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
571 struct sock *sk = sock->sk;
576 if (alen < sizeof(struct sockaddr_sco) ||
577 addr->sa_family != AF_BLUETOOTH)
580 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
583 if (sk->sk_type != SOCK_SEQPACKET)
587 /* Set destination address and psm */
588 bacpy(&sco_pi(sk)->dst, &sa->sco_bdaddr);
591 err = sco_connect(sk);
597 err = bt_sock_wait_state(sk, BT_CONNECTED,
598 sock_sndtimeo(sk, flags & O_NONBLOCK));
604 static int sco_sock_listen(struct socket *sock, int backlog)
606 struct sock *sk = sock->sk;
607 bdaddr_t *src = &sco_pi(sk)->src;
610 BT_DBG("sk %p backlog %d", sk, backlog);
614 if (sk->sk_state != BT_BOUND) {
619 if (sk->sk_type != SOCK_SEQPACKET) {
624 write_lock(&sco_sk_list.lock);
626 if (__sco_get_sock_listen_by_addr(src)) {
631 sk->sk_max_ack_backlog = backlog;
632 sk->sk_ack_backlog = 0;
634 sk->sk_state = BT_LISTEN;
637 write_unlock(&sco_sk_list.lock);
644 static int sco_sock_accept(struct socket *sock, struct socket *newsock,
645 int flags, bool kern)
647 DEFINE_WAIT_FUNC(wait, woken_wake_function);
648 struct sock *sk = sock->sk, *ch;
654 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
656 BT_DBG("sk %p timeo %ld", sk, timeo);
658 /* Wait for an incoming connection. (wake-one). */
659 add_wait_queue_exclusive(sk_sleep(sk), &wait);
661 if (sk->sk_state != BT_LISTEN) {
666 ch = bt_accept_dequeue(sk, newsock);
675 if (signal_pending(current)) {
676 err = sock_intr_errno(timeo);
682 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
685 remove_wait_queue(sk_sleep(sk), &wait);
690 newsock->state = SS_CONNECTED;
692 BT_DBG("new socket %p", ch);
699 static int sco_sock_getname(struct socket *sock, struct sockaddr *addr,
702 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
703 struct sock *sk = sock->sk;
705 BT_DBG("sock %p, sk %p", sock, sk);
707 addr->sa_family = AF_BLUETOOTH;
710 bacpy(&sa->sco_bdaddr, &sco_pi(sk)->dst);
712 bacpy(&sa->sco_bdaddr, &sco_pi(sk)->src);
714 return sizeof(struct sockaddr_sco);
717 static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg,
720 struct sock *sk = sock->sk;
724 BT_DBG("sock %p, sk %p", sock, sk);
726 err = sock_error(sk);
730 if (msg->msg_flags & MSG_OOB)
733 skb = bt_skb_sendmsg(sk, msg, len, len, 0, 0);
739 if (sk->sk_state == BT_CONNECTED)
740 err = sco_send_frame(sk, skb);
751 static void sco_conn_defer_accept(struct hci_conn *conn, u16 setting)
753 struct hci_dev *hdev = conn->hdev;
755 BT_DBG("conn %p", conn);
757 conn->state = BT_CONFIG;
759 if (!lmp_esco_capable(hdev)) {
760 struct hci_cp_accept_conn_req cp;
762 bacpy(&cp.bdaddr, &conn->dst);
763 cp.role = 0x00; /* Ignored */
765 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
767 struct hci_cp_accept_sync_conn_req cp;
769 bacpy(&cp.bdaddr, &conn->dst);
770 cp.pkt_type = cpu_to_le16(conn->pkt_type);
772 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
773 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
774 cp.content_format = cpu_to_le16(setting);
776 switch (setting & SCO_AIRMODE_MASK) {
777 case SCO_AIRMODE_TRANSP:
778 if (conn->pkt_type & ESCO_2EV3)
779 cp.max_latency = cpu_to_le16(0x0008);
781 cp.max_latency = cpu_to_le16(0x000D);
782 cp.retrans_effort = 0x02;
784 case SCO_AIRMODE_CVSD:
785 cp.max_latency = cpu_to_le16(0xffff);
786 cp.retrans_effort = 0xff;
789 /* use CVSD settings as fallback */
790 cp.max_latency = cpu_to_le16(0xffff);
791 cp.retrans_effort = 0xff;
795 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
800 static int sco_sock_recvmsg(struct socket *sock, struct msghdr *msg,
801 size_t len, int flags)
803 struct sock *sk = sock->sk;
804 struct sco_pinfo *pi = sco_pi(sk);
808 if (sk->sk_state == BT_CONNECT2 &&
809 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
810 sco_conn_defer_accept(pi->conn->hcon, pi->setting);
811 sk->sk_state = BT_CONFIG;
819 return bt_sock_recvmsg(sock, msg, len, flags);
822 static int sco_sock_setsockopt(struct socket *sock, int level, int optname,
823 sockptr_t optval, unsigned int optlen)
825 struct sock *sk = sock->sk;
827 struct bt_voice voice;
829 struct bt_codecs *codecs;
830 struct hci_dev *hdev;
840 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
845 if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
851 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
853 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
857 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
858 sk->sk_state != BT_CONNECT2) {
863 voice.setting = sco_pi(sk)->setting;
865 len = min_t(unsigned int, sizeof(voice), optlen);
866 if (copy_from_sockptr(&voice, optval, len)) {
871 /* Explicitly check for these values */
872 if (voice.setting != BT_VOICE_TRANSPARENT &&
873 voice.setting != BT_VOICE_CVSD_16BIT) {
878 sco_pi(sk)->setting = voice.setting;
879 hdev = hci_get_route(&sco_pi(sk)->dst, &sco_pi(sk)->src,
885 if (enhanced_sync_conn_capable(hdev) &&
886 voice.setting == BT_VOICE_TRANSPARENT)
887 sco_pi(sk)->codec.id = BT_CODEC_TRANSPARENT;
892 if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
898 set_bit(BT_SK_PKT_STATUS, &bt_sk(sk)->flags);
900 clear_bit(BT_SK_PKT_STATUS, &bt_sk(sk)->flags);
904 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
905 sk->sk_state != BT_CONNECT2) {
910 hdev = hci_get_route(&sco_pi(sk)->dst, &sco_pi(sk)->src,
917 if (!hci_dev_test_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED)) {
923 if (!hdev->get_data_path_id) {
929 if (optlen < sizeof(struct bt_codecs) ||
930 optlen > sizeof(buffer)) {
936 if (copy_from_sockptr(buffer, optval, optlen)) {
942 codecs = (void *)buffer;
944 if (codecs->num_codecs > 1) {
950 sco_pi(sk)->codec = codecs->codecs[0];
963 static int sco_sock_getsockopt_old(struct socket *sock, int optname,
964 char __user *optval, int __user *optlen)
966 struct sock *sk = sock->sk;
967 struct sco_options opts;
968 struct sco_conninfo cinfo;
973 if (get_user(len, optlen))
980 if (sk->sk_state != BT_CONNECTED &&
981 !(sk->sk_state == BT_CONNECT2 &&
982 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
987 opts.mtu = sco_pi(sk)->conn->mtu;
989 BT_DBG("mtu %u", opts.mtu);
991 len = min_t(unsigned int, len, sizeof(opts));
992 if (copy_to_user(optval, (char *)&opts, len))
998 if (sk->sk_state != BT_CONNECTED &&
999 !(sk->sk_state == BT_CONNECT2 &&
1000 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
1005 memset(&cinfo, 0, sizeof(cinfo));
1006 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
1007 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
1009 len = min_t(unsigned int, len, sizeof(cinfo));
1010 if (copy_to_user(optval, (char *)&cinfo, len))
1024 static int sco_sock_getsockopt(struct socket *sock, int level, int optname,
1025 char __user *optval, int __user *optlen)
1027 struct sock *sk = sock->sk;
1029 struct bt_voice voice;
1032 struct codec_list *c;
1033 u8 num_codecs, i, __user *ptr;
1034 struct hci_dev *hdev;
1035 struct hci_codec_caps *caps;
1036 struct bt_codec codec;
1038 BT_DBG("sk %p", sk);
1040 if (level == SOL_SCO)
1041 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
1043 if (get_user(len, optlen))
1050 case BT_DEFER_SETUP:
1051 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
1056 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
1057 (u32 __user *)optval))
1063 voice.setting = sco_pi(sk)->setting;
1065 len = min_t(unsigned int, len, sizeof(voice));
1066 if (copy_to_user(optval, (char *)&voice, len))
1072 if (sk->sk_state != BT_CONNECTED) {
1077 phys = hci_conn_get_phy(sco_pi(sk)->conn->hcon);
1079 if (put_user(phys, (u32 __user *) optval))
1084 if (put_user(test_bit(BT_SK_PKT_STATUS, &bt_sk(sk)->flags),
1085 (int __user *)optval))
1091 if (sk->sk_state != BT_CONNECTED) {
1096 if (put_user(sco_pi(sk)->conn->mtu, (u32 __user *)optval))
1104 hdev = hci_get_route(&sco_pi(sk)->dst, &sco_pi(sk)->src, BDADDR_BREDR);
1110 if (!hci_dev_test_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED)) {
1116 if (!hdev->get_data_path_id) {
1124 /* find total buffer size required to copy codec + caps */
1126 list_for_each_entry(c, &hdev->local_codecs, list) {
1127 if (c->transport != HCI_TRANSPORT_SCO_ESCO)
1130 for (i = 0, caps = c->caps; i < c->num_caps; i++) {
1131 buf_len += 1 + caps->len;
1132 caps = (void *)&caps->data[caps->len];
1134 buf_len += sizeof(struct bt_codec);
1136 hci_dev_unlock(hdev);
1138 buf_len += sizeof(struct bt_codecs);
1139 if (buf_len > len) {
1145 if (put_user(num_codecs, ptr)) {
1149 ptr += sizeof(num_codecs);
1151 /* Iterate all the codecs supported over SCO and populate
1155 list_for_each_entry(c, &hdev->local_codecs, list) {
1156 if (c->transport != HCI_TRANSPORT_SCO_ESCO)
1162 err = hdev->get_data_path_id(hdev, &codec.data_path);
1165 codec.num_caps = c->num_caps;
1166 if (copy_to_user(ptr, &codec, sizeof(codec))) {
1170 ptr += sizeof(codec);
1172 /* find codec capabilities data length */
1174 for (i = 0, caps = c->caps; i < c->num_caps; i++) {
1175 len += 1 + caps->len;
1176 caps = (void *)&caps->data[caps->len];
1179 /* copy codec capabilities data */
1180 if (len && copy_to_user(ptr, c->caps, len)) {
1187 hci_dev_unlock(hdev);
1192 if (!err && put_user(buf_len, optlen))
1206 static int sco_sock_shutdown(struct socket *sock, int how)
1208 struct sock *sk = sock->sk;
1211 BT_DBG("sock %p, sk %p", sock, sk);
1219 if (!sk->sk_shutdown) {
1220 sk->sk_shutdown = SHUTDOWN_MASK;
1221 sco_sock_clear_timer(sk);
1222 __sco_sock_close(sk);
1224 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1225 !(current->flags & PF_EXITING))
1226 err = bt_sock_wait_state(sk, BT_CLOSED,
1236 static int sco_sock_release(struct socket *sock)
1238 struct sock *sk = sock->sk;
1241 BT_DBG("sock %p, sk %p", sock, sk);
1248 if (sock_flag(sk, SOCK_LINGER) && READ_ONCE(sk->sk_lingertime) &&
1249 !(current->flags & PF_EXITING)) {
1251 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1260 static void sco_conn_ready(struct sco_conn *conn)
1262 struct sock *parent;
1263 struct sock *sk = conn->sk;
1265 BT_DBG("conn %p", conn);
1269 sco_sock_clear_timer(sk);
1270 sk->sk_state = BT_CONNECTED;
1271 sk->sk_state_change(sk);
1274 sco_conn_lock(conn);
1277 sco_conn_unlock(conn);
1281 parent = sco_get_sock_listen(&conn->hcon->src);
1283 sco_conn_unlock(conn);
1289 sk = sco_sock_alloc(sock_net(parent), NULL,
1290 BTPROTO_SCO, GFP_ATOMIC, 0);
1292 release_sock(parent);
1293 sco_conn_unlock(conn);
1297 sco_sock_init(sk, parent);
1299 bacpy(&sco_pi(sk)->src, &conn->hcon->src);
1300 bacpy(&sco_pi(sk)->dst, &conn->hcon->dst);
1302 hci_conn_hold(conn->hcon);
1303 __sco_chan_add(conn, sk, parent);
1305 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
1306 sk->sk_state = BT_CONNECT2;
1308 sk->sk_state = BT_CONNECTED;
1310 /* Wake up parent */
1311 parent->sk_data_ready(parent);
1313 release_sock(parent);
1315 sco_conn_unlock(conn);
1319 /* ----- SCO interface with lower layer (HCI) ----- */
1320 int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
1325 BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
1327 /* Find listening sockets */
1328 read_lock(&sco_sk_list.lock);
1329 sk_for_each(sk, &sco_sk_list.head) {
1330 if (sk->sk_state != BT_LISTEN)
1333 if (!bacmp(&sco_pi(sk)->src, &hdev->bdaddr) ||
1334 !bacmp(&sco_pi(sk)->src, BDADDR_ANY)) {
1335 lm |= HCI_LM_ACCEPT;
1337 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
1338 *flags |= HCI_PROTO_DEFER;
1342 read_unlock(&sco_sk_list.lock);
1347 static void sco_connect_cfm(struct hci_conn *hcon, __u8 status)
1349 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
1352 BT_DBG("hcon %p bdaddr %pMR status %u", hcon, &hcon->dst, status);
1355 struct sco_conn *conn;
1357 conn = sco_conn_add(hcon);
1359 sco_conn_ready(conn);
1361 sco_conn_del(hcon, bt_to_errno(status));
1364 static void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
1366 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
1369 BT_DBG("hcon %p reason %d", hcon, reason);
1371 sco_conn_del(hcon, bt_to_errno(reason));
1374 void sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
1376 struct sco_conn *conn = hcon->sco_data;
1381 BT_DBG("conn %p len %u", conn, skb->len);
1384 sco_recv_frame(conn, skb);
1392 static struct hci_cb sco_cb = {
1394 .connect_cfm = sco_connect_cfm,
1395 .disconn_cfm = sco_disconn_cfm,
1398 static int sco_debugfs_show(struct seq_file *f, void *p)
1402 read_lock(&sco_sk_list.lock);
1404 sk_for_each(sk, &sco_sk_list.head) {
1405 seq_printf(f, "%pMR %pMR %d\n", &sco_pi(sk)->src,
1406 &sco_pi(sk)->dst, sk->sk_state);
1409 read_unlock(&sco_sk_list.lock);
1414 DEFINE_SHOW_ATTRIBUTE(sco_debugfs);
1416 static struct dentry *sco_debugfs;
1418 static const struct proto_ops sco_sock_ops = {
1419 .family = PF_BLUETOOTH,
1420 .owner = THIS_MODULE,
1421 .release = sco_sock_release,
1422 .bind = sco_sock_bind,
1423 .connect = sco_sock_connect,
1424 .listen = sco_sock_listen,
1425 .accept = sco_sock_accept,
1426 .getname = sco_sock_getname,
1427 .sendmsg = sco_sock_sendmsg,
1428 .recvmsg = sco_sock_recvmsg,
1429 .poll = bt_sock_poll,
1430 .ioctl = bt_sock_ioctl,
1431 .gettstamp = sock_gettstamp,
1432 .mmap = sock_no_mmap,
1433 .socketpair = sock_no_socketpair,
1434 .shutdown = sco_sock_shutdown,
1435 .setsockopt = sco_sock_setsockopt,
1436 .getsockopt = sco_sock_getsockopt
1439 static const struct net_proto_family sco_sock_family_ops = {
1440 .family = PF_BLUETOOTH,
1441 .owner = THIS_MODULE,
1442 .create = sco_sock_create,
1445 int __init sco_init(void)
1449 BUILD_BUG_ON(sizeof(struct sockaddr_sco) > sizeof(struct sockaddr));
1451 err = proto_register(&sco_proto, 0);
1455 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1457 BT_ERR("SCO socket registration failed");
1461 err = bt_procfs_init(&init_net, "sco", &sco_sk_list, NULL);
1463 BT_ERR("Failed to create SCO proc file");
1464 bt_sock_unregister(BTPROTO_SCO);
1468 BT_INFO("SCO socket layer initialized");
1470 hci_register_cb(&sco_cb);
1472 if (IS_ERR_OR_NULL(bt_debugfs))
1475 sco_debugfs = debugfs_create_file("sco", 0444, bt_debugfs,
1476 NULL, &sco_debugfs_fops);
1481 proto_unregister(&sco_proto);
1487 bt_procfs_cleanup(&init_net, "sco");
1489 debugfs_remove(sco_debugfs);
1491 hci_unregister_cb(&sco_cb);
1493 bt_sock_unregister(BTPROTO_SCO);
1495 proto_unregister(&sco_proto);
1498 module_param(disable_esco, bool, 0644);
1499 MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");