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>
37 #include <net/bluetooth/mgmt.h>
38 #include <net/bluetooth/mgmt_tizen.h>
41 static bool disable_esco;
43 static const struct proto_ops sco_sock_ops;
45 static struct bt_sock_list sco_sk_list = {
46 .lock = __RW_LOCK_UNLOCKED(sco_sk_list.lock)
49 /* ---- SCO connections ---- */
51 struct hci_conn *hcon;
56 struct delayed_work timeout_work;
61 #define sco_conn_lock(c) spin_lock(&c->lock)
62 #define sco_conn_unlock(c) spin_unlock(&c->lock)
64 static void sco_sock_close(struct sock *sk);
65 static void sco_sock_kill(struct sock *sk);
67 /* ----- SCO socket info ----- */
68 #define sco_pi(sk) ((struct sco_pinfo *) sk)
77 struct sco_conn *conn;
80 /* ---- SCO timers ---- */
81 #define SCO_CONN_TIMEOUT (HZ * 40)
82 #define SCO_DISCONN_TIMEOUT (HZ * 2)
84 static void sco_sock_timeout(struct work_struct *work)
86 struct sco_conn *conn = container_of(work, struct sco_conn,
94 sco_conn_unlock(conn);
99 BT_DBG("sock %p state %d", sk, sk->sk_state);
102 sk->sk_err = ETIMEDOUT;
103 sk->sk_state_change(sk);
108 static void sco_sock_set_timer(struct sock *sk, long timeout)
110 if (!sco_pi(sk)->conn)
113 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
114 cancel_delayed_work(&sco_pi(sk)->conn->timeout_work);
115 schedule_delayed_work(&sco_pi(sk)->conn->timeout_work, timeout);
118 static void sco_sock_clear_timer(struct sock *sk)
120 if (!sco_pi(sk)->conn)
123 BT_DBG("sock %p state %d", sk, sk->sk_state);
124 cancel_delayed_work(&sco_pi(sk)->conn->timeout_work);
127 /* ---- SCO connections ---- */
128 static struct sco_conn *sco_conn_add(struct hci_conn *hcon)
130 struct hci_dev *hdev = hcon->hdev;
131 struct sco_conn *conn = hcon->sco_data;
136 conn = kzalloc(sizeof(struct sco_conn), GFP_KERNEL);
140 spin_lock_init(&conn->lock);
141 INIT_DELAYED_WORK(&conn->timeout_work, sco_sock_timeout);
143 hcon->sco_data = conn;
146 if (hdev->sco_mtu > 0)
147 conn->mtu = hdev->sco_mtu;
151 BT_DBG("hcon %p conn %p", hcon, conn);
157 * Must be called on the locked socket. */
158 static void sco_chan_del(struct sock *sk, int err)
160 struct sco_conn *conn;
162 conn = sco_pi(sk)->conn;
164 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
169 sco_pi(sk)->conn = NULL;
170 sco_conn_unlock(conn);
173 hci_conn_drop(conn->hcon);
176 sk->sk_state = BT_CLOSED;
178 sk->sk_state_change(sk);
180 sock_set_flag(sk, SOCK_ZAPPED);
183 static void sco_conn_del(struct hci_conn *hcon, int err)
185 struct sco_conn *conn = hcon->sco_data;
191 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
198 sco_conn_unlock(conn);
202 sco_sock_clear_timer(sk);
203 sco_chan_del(sk, err);
208 /* Ensure no more work items will run before freeing conn. */
209 cancel_delayed_work_sync(&conn->timeout_work);
211 hcon->sco_data = NULL;
215 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk,
218 BT_DBG("conn %p", conn);
220 sco_pi(sk)->conn = conn;
224 bt_accept_enqueue(parent, sk, true);
227 static int sco_chan_add(struct sco_conn *conn, struct sock *sk,
236 __sco_chan_add(conn, sk, parent);
238 sco_conn_unlock(conn);
242 static int sco_connect(struct hci_dev *hdev, struct sock *sk)
244 struct sco_conn *conn;
245 struct hci_conn *hcon;
248 BT_DBG("%pMR -> %pMR", &sco_pi(sk)->src, &sco_pi(sk)->dst);
250 if (lmp_esco_capable(hdev) && !disable_esco)
255 if (sco_pi(sk)->setting == BT_VOICE_TRANSPARENT &&
256 (!lmp_transp_capable(hdev) || !lmp_esco_capable(hdev)))
259 hcon = hci_connect_sco(hdev, type, &sco_pi(sk)->dst,
260 sco_pi(sk)->setting);
262 return PTR_ERR(hcon);
264 conn = sco_conn_add(hcon);
270 /* Update source addr of the socket */
271 bacpy(&sco_pi(sk)->src, &hcon->src);
273 err = sco_chan_add(conn, sk, NULL);
277 if (hcon->state == BT_CONNECTED) {
278 sco_sock_clear_timer(sk);
279 sk->sk_state = BT_CONNECTED;
281 sk->sk_state = BT_CONNECT;
282 sco_sock_set_timer(sk, sk->sk_sndtimeo);
288 static int sco_send_frame(struct sock *sk, struct sk_buff *skb)
290 struct sco_conn *conn = sco_pi(sk)->conn;
293 /* Check outgoing MTU */
297 BT_DBG("sk %p len %d", sk, len);
299 hci_send_sco(conn->hcon, skb);
304 static void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
310 sco_conn_unlock(conn);
315 BT_DBG("sk %p len %u", sk, skb->len);
317 if (sk->sk_state != BT_CONNECTED)
320 if (!sock_queue_rcv_skb(sk, skb))
327 /* -------- Socket interface ---------- */
328 static struct sock *__sco_get_sock_listen_by_addr(bdaddr_t *ba)
332 sk_for_each(sk, &sco_sk_list.head) {
333 if (sk->sk_state != BT_LISTEN)
336 if (!bacmp(&sco_pi(sk)->src, ba))
343 /* Find socket listening on source bdaddr.
344 * Returns closest match.
346 static struct sock *sco_get_sock_listen(bdaddr_t *src)
348 struct sock *sk = NULL, *sk1 = NULL;
350 read_lock(&sco_sk_list.lock);
352 sk_for_each(sk, &sco_sk_list.head) {
353 if (sk->sk_state != BT_LISTEN)
357 if (!bacmp(&sco_pi(sk)->src, src))
361 if (!bacmp(&sco_pi(sk)->src, BDADDR_ANY))
365 read_unlock(&sco_sk_list.lock);
367 return sk ? sk : sk1;
370 static void sco_sock_destruct(struct sock *sk)
374 skb_queue_purge(&sk->sk_receive_queue);
375 skb_queue_purge(&sk->sk_write_queue);
378 static void sco_sock_cleanup_listen(struct sock *parent)
382 BT_DBG("parent %p", parent);
384 /* Close not yet accepted channels */
385 while ((sk = bt_accept_dequeue(parent, NULL))) {
390 parent->sk_state = BT_CLOSED;
391 sock_set_flag(parent, SOCK_ZAPPED);
394 /* Kill socket (only if zapped and orphan)
395 * Must be called on unlocked socket.
397 static void sco_sock_kill(struct sock *sk)
399 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
402 BT_DBG("sk %p state %d", sk, sk->sk_state);
404 /* Kill poor orphan */
405 bt_sock_unlink(&sco_sk_list, sk);
406 sock_set_flag(sk, SOCK_DEAD);
410 static void __sco_sock_close(struct sock *sk)
412 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
414 switch (sk->sk_state) {
416 sco_sock_cleanup_listen(sk);
421 if (sco_pi(sk)->conn->hcon) {
422 sk->sk_state = BT_DISCONN;
423 sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT);
424 sco_conn_lock(sco_pi(sk)->conn);
425 hci_conn_drop(sco_pi(sk)->conn->hcon);
426 sco_pi(sk)->conn->hcon = NULL;
427 sco_conn_unlock(sco_pi(sk)->conn);
429 sco_chan_del(sk, ECONNRESET);
435 sco_chan_del(sk, ECONNRESET);
439 sock_set_flag(sk, SOCK_ZAPPED);
444 /* Must be called on unlocked socket. */
445 static void sco_sock_close(struct sock *sk)
448 sco_sock_clear_timer(sk);
449 __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 bt_sock_link(&sco_sk_list, sk);
507 static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
512 BT_DBG("sock %p", sock);
514 sock->state = SS_UNCONNECTED;
516 if (sock->type != SOCK_SEQPACKET)
517 return -ESOCKTNOSUPPORT;
519 sock->ops = &sco_sock_ops;
521 sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern);
525 sco_sock_init(sk, NULL);
529 static int sco_sock_bind(struct socket *sock, struct sockaddr *addr,
532 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
533 struct sock *sk = sock->sk;
536 if (!addr || addr_len < sizeof(struct sockaddr_sco) ||
537 addr->sa_family != AF_BLUETOOTH)
540 BT_DBG("sk %p %pMR", sk, &sa->sco_bdaddr);
544 if (sk->sk_state != BT_OPEN) {
549 if (sk->sk_type != SOCK_SEQPACKET) {
554 bacpy(&sco_pi(sk)->src, &sa->sco_bdaddr);
556 sk->sk_state = BT_BOUND;
563 static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
565 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
566 struct sock *sk = sock->sk;
567 struct hci_dev *hdev;
572 if (alen < sizeof(struct sockaddr_sco) ||
573 addr->sa_family != AF_BLUETOOTH)
577 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) {
582 if (sk->sk_type != SOCK_SEQPACKET) {
587 hdev = hci_get_route(&sa->sco_bdaddr, &sco_pi(sk)->src, BDADDR_BREDR);
594 /* Set destination address and psm */
595 bacpy(&sco_pi(sk)->dst, &sa->sco_bdaddr);
597 err = sco_connect(hdev, sk);
598 hci_dev_unlock(hdev);
603 err = bt_sock_wait_state(sk, BT_CONNECTED,
604 sock_sndtimeo(sk, flags & O_NONBLOCK));
611 static int sco_sock_listen(struct socket *sock, int backlog)
613 struct sock *sk = sock->sk;
614 bdaddr_t *src = &sco_pi(sk)->src;
617 BT_DBG("sk %p backlog %d", sk, backlog);
621 if (sk->sk_state != BT_BOUND) {
626 if (sk->sk_type != SOCK_SEQPACKET) {
631 write_lock(&sco_sk_list.lock);
633 if (__sco_get_sock_listen_by_addr(src)) {
638 sk->sk_max_ack_backlog = backlog;
639 sk->sk_ack_backlog = 0;
641 sk->sk_state = BT_LISTEN;
644 write_unlock(&sco_sk_list.lock);
651 static int sco_sock_accept(struct socket *sock, struct socket *newsock,
652 int flags, bool kern)
654 DEFINE_WAIT_FUNC(wait, woken_wake_function);
655 struct sock *sk = sock->sk, *ch;
661 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
663 BT_DBG("sk %p timeo %ld", sk, timeo);
665 /* Wait for an incoming connection. (wake-one). */
666 add_wait_queue_exclusive(sk_sleep(sk), &wait);
668 if (sk->sk_state != BT_LISTEN) {
673 ch = bt_accept_dequeue(sk, newsock);
682 if (signal_pending(current)) {
683 err = sock_intr_errno(timeo);
689 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
692 remove_wait_queue(sk_sleep(sk), &wait);
697 newsock->state = SS_CONNECTED;
699 BT_DBG("new socket %p", ch);
706 static int sco_sock_getname(struct socket *sock, struct sockaddr *addr,
709 struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
710 struct sock *sk = sock->sk;
712 BT_DBG("sock %p, sk %p", sock, sk);
714 addr->sa_family = AF_BLUETOOTH;
717 bacpy(&sa->sco_bdaddr, &sco_pi(sk)->dst);
719 bacpy(&sa->sco_bdaddr, &sco_pi(sk)->src);
721 return sizeof(struct sockaddr_sco);
724 static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg,
727 struct sock *sk = sock->sk;
731 BT_DBG("sock %p, sk %p", sock, sk);
733 err = sock_error(sk);
737 if (msg->msg_flags & MSG_OOB)
740 skb = bt_skb_sendmsg(sk, msg, len, len, 0, 0);
746 if (sk->sk_state == BT_CONNECTED)
747 err = sco_send_frame(sk, skb);
758 static void sco_conn_defer_accept(struct hci_conn *conn, u16 setting)
760 struct hci_dev *hdev = conn->hdev;
762 BT_DBG("conn %p", conn);
764 conn->state = BT_CONFIG;
766 if (!lmp_esco_capable(hdev)) {
767 struct hci_cp_accept_conn_req cp;
769 bacpy(&cp.bdaddr, &conn->dst);
770 cp.role = 0x00; /* Ignored */
772 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
774 struct hci_cp_accept_sync_conn_req cp;
776 bacpy(&cp.bdaddr, &conn->dst);
777 cp.pkt_type = cpu_to_le16(conn->pkt_type);
779 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
780 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
781 cp.content_format = cpu_to_le16(setting);
783 switch (setting & SCO_AIRMODE_MASK) {
784 case SCO_AIRMODE_TRANSP:
785 if (conn->pkt_type & ESCO_2EV3)
786 cp.max_latency = cpu_to_le16(0x0008);
788 cp.max_latency = cpu_to_le16(0x000D);
789 cp.retrans_effort = 0x02;
791 case SCO_AIRMODE_CVSD:
792 cp.max_latency = cpu_to_le16(0xffff);
793 cp.retrans_effort = 0xff;
796 /* use CVSD settings as fallback */
797 cp.max_latency = cpu_to_le16(0xffff);
798 cp.retrans_effort = 0xff;
802 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
807 static int sco_sock_recvmsg(struct socket *sock, struct msghdr *msg,
808 size_t len, int flags)
810 struct sock *sk = sock->sk;
811 struct sco_pinfo *pi = sco_pi(sk);
815 if (sk->sk_state == BT_CONNECT2 &&
816 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
817 sco_conn_defer_accept(pi->conn->hcon, pi->setting);
818 sk->sk_state = BT_CONFIG;
826 return bt_sock_recvmsg(sock, msg, len, flags);
829 static int sco_sock_setsockopt(struct socket *sock, int level, int optname,
830 sockptr_t optval, unsigned int optlen)
832 struct sock *sk = sock->sk;
834 struct bt_voice voice;
844 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
849 if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
855 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
857 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
861 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
862 sk->sk_state != BT_CONNECT2) {
867 voice.setting = sco_pi(sk)->setting;
869 len = min_t(unsigned int, sizeof(voice), optlen);
870 if (copy_from_sockptr(&voice, optval, len)) {
875 /* Explicitly check for these values */
877 /* Codec defer accept */
878 if (voice.setting != (BT_VOICE_TRANSPARENT |
879 BT_VOICE_CVSD_16BIT) &&
880 voice.setting != BT_VOICE_CVSD_16BIT) {
882 if (voice.setting != BT_VOICE_TRANSPARENT &&
883 voice.setting != BT_VOICE_CVSD_16BIT) {
889 sco_pi(sk)->setting = voice.setting;
893 if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
899 sco_pi(sk)->cmsg_mask |= SCO_CMSG_PKT_STATUS;
901 sco_pi(sk)->cmsg_mask &= SCO_CMSG_PKT_STATUS;
913 static int sco_sock_getsockopt_old(struct socket *sock, int optname,
914 char __user *optval, int __user *optlen)
916 struct sock *sk = sock->sk;
917 struct sco_options opts;
918 struct sco_conninfo cinfo;
923 if (get_user(len, optlen))
930 if (sk->sk_state != BT_CONNECTED &&
931 !(sk->sk_state == BT_CONNECT2 &&
932 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
937 opts.mtu = sco_pi(sk)->conn->mtu;
939 BT_DBG("mtu %u", opts.mtu);
941 len = min_t(unsigned int, len, sizeof(opts));
942 if (copy_to_user(optval, (char *)&opts, len))
948 if (sk->sk_state != BT_CONNECTED &&
949 !(sk->sk_state == BT_CONNECT2 &&
950 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
955 memset(&cinfo, 0, sizeof(cinfo));
956 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
957 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
959 len = min_t(unsigned int, len, sizeof(cinfo));
960 if (copy_to_user(optval, (char *)&cinfo, len))
974 static int sco_sock_getsockopt(struct socket *sock, int level, int optname,
975 char __user *optval, int __user *optlen)
977 struct sock *sk = sock->sk;
979 struct bt_voice voice;
985 if (level == SOL_SCO)
986 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
988 if (get_user(len, optlen))
996 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
1001 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
1002 (u32 __user *)optval))
1008 voice.setting = sco_pi(sk)->setting;
1010 len = min_t(unsigned int, len, sizeof(voice));
1011 if (copy_to_user(optval, (char *)&voice, len))
1017 if (sk->sk_state != BT_CONNECTED) {
1022 phys = hci_conn_get_phy(sco_pi(sk)->conn->hcon);
1024 if (put_user(phys, (u32 __user *) optval))
1029 pkt_status = (sco_pi(sk)->cmsg_mask & SCO_CMSG_PKT_STATUS);
1031 if (put_user(pkt_status, (int __user *)optval))
1037 if (sk->sk_state != BT_CONNECTED) {
1042 if (put_user(sco_pi(sk)->conn->mtu, (u32 __user *)optval))
1055 static int sco_sock_shutdown(struct socket *sock, int how)
1057 struct sock *sk = sock->sk;
1060 BT_DBG("sock %p, sk %p", sock, sk);
1068 if (!sk->sk_shutdown) {
1069 sk->sk_shutdown = SHUTDOWN_MASK;
1070 sco_sock_clear_timer(sk);
1071 __sco_sock_close(sk);
1073 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1074 !(current->flags & PF_EXITING))
1075 err = bt_sock_wait_state(sk, BT_CLOSED,
1085 static int sco_sock_release(struct socket *sock)
1087 struct sock *sk = sock->sk;
1090 BT_DBG("sock %p, sk %p", sock, sk);
1097 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1098 !(current->flags & PF_EXITING)) {
1100 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1105 /* SCO kernel panic fix */
1106 bt_sock_unlink(&sco_sk_list, sk);
1113 static void sco_conn_ready(struct sco_conn *conn)
1115 struct sock *parent;
1116 struct sock *sk = conn->sk;
1118 BT_DBG("conn %p", conn);
1122 sco_sock_clear_timer(sk);
1123 sk->sk_state = BT_CONNECTED;
1124 sk->sk_state_change(sk);
1127 sco_conn_lock(conn);
1130 sco_conn_unlock(conn);
1135 /* Multiple SCO accept feature */
1136 parent = sco_get_sock_listen(&conn->hcon->dst);
1138 parent = sco_get_sock_listen(&conn->hcon->src);
1141 sco_conn_unlock(conn);
1147 sk = sco_sock_alloc(sock_net(parent), NULL,
1148 BTPROTO_SCO, GFP_ATOMIC, 0);
1150 release_sock(parent);
1151 sco_conn_unlock(conn);
1155 sco_sock_init(sk, parent);
1157 bacpy(&sco_pi(sk)->src, &conn->hcon->src);
1158 bacpy(&sco_pi(sk)->dst, &conn->hcon->dst);
1160 hci_conn_hold(conn->hcon);
1161 __sco_chan_add(conn, sk, parent);
1163 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
1164 sk->sk_state = BT_CONNECT2;
1166 sk->sk_state = BT_CONNECTED;
1168 /* Wake up parent */
1169 parent->sk_data_ready(parent);
1171 release_sock(parent);
1173 sco_conn_unlock(conn);
1179 static int hci_write_acl_link_policy(struct hci_conn *hcon, uint16_t policy)
1181 struct hci_conn *hcon_acl;
1182 struct hci_cp_write_link_policy cp;
1184 BT_DBG("Write link Policy %d", policy);
1186 hcon_acl = hci_conn_hash_lookup_ba(hcon->hdev, ACL_LINK, &hcon->dst);
1188 BT_ERR("ACL does not alive");
1192 memset(&cp, 0, sizeof(cp));
1193 cp.handle = __cpu_to_le16(hcon_acl->handle);
1194 cp.policy = __cpu_to_le16(policy);
1196 hci_send_cmd(hcon_acl->hdev, HCI_OP_WRITE_LINK_POLICY, sizeof(cp), &cp);
1201 /* WBC/NBC feature */
1202 void sco_connect_set_gw_nbc(struct hci_dev *hdev)
1204 struct hci_cp_write_voice_setting cp1;
1205 struct hci_cp_bcm_wbs_req cp2;
1206 struct hci_cp_i2c_pcm_req cp3;
1207 struct hci_cp_sco_pcm_req cp4;
1209 BT_DBG("Setting the NBC params, hdev = %p", hdev);
1211 cp1.voice_setting = cpu_to_le16(0x0060);
1212 hci_send_cmd(hdev, HCI_OP_WRITE_VOICE_SETTING, sizeof(cp1), &cp1);
1213 hdev->voice_setting = cpu_to_le16(0x0060);
1215 cp2.role = 0x00; /* WbDisable */
1216 cp2.pkt_type = cpu_to_le16(0x0002);
1217 hci_send_cmd(hdev, HCI_BCM_ENABLE_WBS_REQ, sizeof(cp2), &cp2);
1219 cp3.i2c_enable = 0x01;
1220 cp3.is_master = 0x00;
1221 cp3.pcm_rate = 0x00;
1222 cp3.clock_rate = 0x01;
1223 hci_send_cmd(hdev, HCI_BCM_I2C_PCM_REQ, sizeof(cp3), &cp3);
1225 cp4.sco_routing = 0x00;
1226 cp4.pcm_rate = 0x01;
1227 cp4.frame_type = 0x00;
1228 cp4.sync_mode = 0x00;
1229 cp4.clock_mode = 0x00;
1230 hci_send_cmd(hdev, HCI_BCM_SCO_PCM_REQ, sizeof(cp4), &cp4);
1233 void sco_connect_set_gw_wbc(struct hci_dev *hdev)
1235 struct hci_cp_write_voice_setting cp1;
1236 struct hci_cp_bcm_wbs_req cp2;
1237 struct hci_cp_i2c_pcm_req cp3;
1238 struct hci_cp_sco_pcm_req cp4;
1240 BT_DBG("Setting the WBC params, hdev = %p", hdev);
1241 cp1.voice_setting = cpu_to_le16(0x0003 | 0x0060);
1242 hci_send_cmd(hdev, HCI_OP_WRITE_VOICE_SETTING, sizeof(cp1), &cp1);
1243 hdev->voice_setting = cpu_to_le16(0x0003 | 0x0060);
1245 cp2.role = 0x01; /* Enable */
1246 cp2.pkt_type = cpu_to_le16(0x0002);
1247 hci_send_cmd(hdev, HCI_BCM_ENABLE_WBS_REQ, sizeof(cp2), &cp2);
1249 cp3.i2c_enable = 0x00;
1250 cp3.is_master = 0x00;
1251 cp3.pcm_rate = 0x01;
1252 cp3.clock_rate = 0x02;
1253 hci_send_cmd(hdev, HCI_BCM_I2C_PCM_REQ, sizeof(cp3), &cp3);
1255 cp4.sco_routing = 0x00;
1256 cp4.pcm_rate = 0x00;
1257 cp4.frame_type = 0x00;
1258 cp4.sync_mode = 0x00;
1259 cp4.clock_mode = 0x00;
1260 hci_send_cmd(hdev, HCI_BCM_SCO_PCM_REQ, sizeof(cp4), &cp4);
1263 void sco_connect_set_nbc(struct hci_dev *hdev)
1265 struct hci_cp_write_voice_setting cp1;
1266 struct hci_cp_bcm_wbs_req cp2;
1267 struct hci_cp_i2c_pcm_req cp3;
1268 struct hci_cp_sco_pcm_req cp4;
1270 BT_DBG("Setting the NBC params, hdev = %p", hdev);
1272 cp1.voice_setting = cpu_to_le16(0x0060);
1273 hci_send_cmd(hdev, HCI_OP_WRITE_VOICE_SETTING, sizeof(cp1), &cp1);
1274 hdev->voice_setting = cpu_to_le16(0x0060);
1276 cp2.role = 0x00; /* WbDisable */
1277 cp2.pkt_type = cpu_to_le16(0x0002);
1278 hci_send_cmd(hdev, HCI_BCM_ENABLE_WBS_REQ, sizeof(cp2), &cp2);
1280 cp3.i2c_enable = 0x00;
1281 cp3.is_master = 0x00;
1282 cp3.pcm_rate = 0x00;
1283 cp3.clock_rate = 0x04;
1284 hci_send_cmd(hdev, HCI_BCM_I2C_PCM_REQ, sizeof(cp3), &cp3);
1286 cp4.sco_routing = 0x00;
1287 cp4.pcm_rate = 0x04;
1288 cp4.frame_type = 0x00;
1289 cp4.sync_mode = 0x00;
1290 cp4.clock_mode = 0x00;
1291 hci_send_cmd(hdev, HCI_BCM_SCO_PCM_REQ, sizeof(cp4), &cp4);
1294 void sco_connect_set_wbc(struct hci_dev *hdev)
1296 struct hci_cp_write_voice_setting cp1;
1297 struct hci_cp_bcm_wbs_req cp2;
1298 struct hci_cp_i2c_pcm_req cp3;
1299 struct hci_cp_sco_pcm_req cp4;
1301 BT_DBG("Setting the WBC params, hdev = %p", hdev);
1302 cp1.voice_setting = cpu_to_le16(0x0003 | 0x0060);
1303 hci_send_cmd(hdev, HCI_OP_WRITE_VOICE_SETTING, sizeof(cp1), &cp1);
1304 hdev->voice_setting = cpu_to_le16(0x0003 | 0x0060);
1306 cp2.role = 0x01; /* Enable */
1307 cp2.pkt_type = cpu_to_le16(0x0002);
1308 hci_send_cmd(hdev, HCI_BCM_ENABLE_WBS_REQ, sizeof(cp2), &cp2);
1310 cp3.i2c_enable = 0x00;
1311 cp3.is_master = 0x00;
1312 cp3.pcm_rate = 0x01;
1313 cp3.clock_rate = 0x04;
1314 hci_send_cmd(hdev, HCI_BCM_I2C_PCM_REQ, sizeof(cp3), &cp3);
1316 cp4.sco_routing = 0x00;
1317 cp4.pcm_rate = 0x04;
1318 cp4.frame_type = 0x00;
1319 cp4.sync_mode = 0x00;
1320 cp4.clock_mode = 0x00;
1321 hci_send_cmd(hdev, HCI_BCM_SCO_PCM_REQ, sizeof(cp4), &cp4);
1325 /* ----- SCO interface with lower layer (HCI) ----- */
1326 int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
1331 BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
1333 /* Find listening sockets */
1334 read_lock(&sco_sk_list.lock);
1335 sk_for_each(sk, &sco_sk_list.head) {
1336 if (sk->sk_state != BT_LISTEN)
1340 /* Multiple SCO accept feature */
1341 if (!bacmp(&sco_pi(sk)->src, bdaddr) ||
1342 !bacmp(&sco_pi(sk)->src, BDADDR_ANY)) {
1344 if (!bacmp(&sco_pi(sk)->src, &hdev->bdaddr) ||
1345 !bacmp(&sco_pi(sk)->src, BDADDR_ANY)) {
1347 lm |= HCI_LM_ACCEPT;
1349 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
1350 *flags |= HCI_PROTO_DEFER;
1354 read_unlock(&sco_sk_list.lock);
1357 /* WBC/NBC feature */
1358 if ((lm & HCI_LM_ACCEPT) && !hci_conn_hash_lookup_sco(hdev)) {
1359 struct hci_conn *hcon_acl;
1361 hcon_acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, bdaddr);
1363 BT_ERR("ACL doesn't alive. Use 0x%X",
1364 hdev->voice_setting);
1366 if (hdev->voice_setting == 0x0063)
1367 sco_connect_set_wbc(hdev);
1369 sco_connect_set_nbc(hdev);
1370 } else if (hcon_acl->sco_role == MGMT_SCO_ROLE_HANDSFREE) {
1371 BT_DBG("Handsfree SCO 0x%X", hcon_acl->voice_setting);
1372 if (hcon_acl->voice_setting == 0x0063)
1373 sco_connect_set_wbc(hdev);
1375 sco_connect_set_nbc(hdev);
1377 BT_DBG("Gateway SCO 0x%X", hcon_acl->voice_setting);
1378 if (hcon_acl->voice_setting == 0x0063)
1379 sco_connect_set_gw_wbc(hdev);
1381 sco_connect_set_gw_nbc(hdev);
1389 static void sco_connect_cfm(struct hci_conn *hcon, __u8 status)
1391 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
1394 BT_DBG("hcon %p bdaddr %pMR status %u", hcon, &hcon->dst, status);
1397 struct sco_conn *conn;
1399 conn = sco_conn_add(hcon);
1401 sco_conn_ready(conn);
1405 hci_write_acl_link_policy(hcon, HCI_LP_RSWITCH);
1408 sco_conn_del(hcon, bt_to_errno(status));
1411 static void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
1413 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
1416 BT_DBG("hcon %p reason %d", hcon, reason);
1420 hci_write_acl_link_policy(hcon, HCI_LP_SNIFF | HCI_LP_RSWITCH);
1423 sco_conn_del(hcon, bt_to_errno(reason));
1426 void sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
1428 struct sco_conn *conn = hcon->sco_data;
1433 BT_DBG("conn %p len %u", conn, skb->len);
1436 sco_recv_frame(conn, skb);
1444 static struct hci_cb sco_cb = {
1446 .connect_cfm = sco_connect_cfm,
1447 .disconn_cfm = sco_disconn_cfm,
1450 static int sco_debugfs_show(struct seq_file *f, void *p)
1454 read_lock(&sco_sk_list.lock);
1456 sk_for_each(sk, &sco_sk_list.head) {
1457 seq_printf(f, "%pMR %pMR %d\n", &sco_pi(sk)->src,
1458 &sco_pi(sk)->dst, sk->sk_state);
1461 read_unlock(&sco_sk_list.lock);
1466 DEFINE_SHOW_ATTRIBUTE(sco_debugfs);
1468 static struct dentry *sco_debugfs;
1470 static const struct proto_ops sco_sock_ops = {
1471 .family = PF_BLUETOOTH,
1472 .owner = THIS_MODULE,
1473 .release = sco_sock_release,
1474 .bind = sco_sock_bind,
1475 .connect = sco_sock_connect,
1476 .listen = sco_sock_listen,
1477 .accept = sco_sock_accept,
1478 .getname = sco_sock_getname,
1479 .sendmsg = sco_sock_sendmsg,
1480 .recvmsg = sco_sock_recvmsg,
1481 .poll = bt_sock_poll,
1482 .ioctl = bt_sock_ioctl,
1483 .gettstamp = sock_gettstamp,
1484 .mmap = sock_no_mmap,
1485 .socketpair = sock_no_socketpair,
1486 .shutdown = sco_sock_shutdown,
1487 .setsockopt = sco_sock_setsockopt,
1488 .getsockopt = sco_sock_getsockopt
1491 static const struct net_proto_family sco_sock_family_ops = {
1492 .family = PF_BLUETOOTH,
1493 .owner = THIS_MODULE,
1494 .create = sco_sock_create,
1497 int __init sco_init(void)
1501 BUILD_BUG_ON(sizeof(struct sockaddr_sco) > sizeof(struct sockaddr));
1503 err = proto_register(&sco_proto, 0);
1507 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1509 BT_ERR("SCO socket registration failed");
1513 err = bt_procfs_init(&init_net, "sco", &sco_sk_list, NULL);
1515 BT_ERR("Failed to create SCO proc file");
1516 bt_sock_unregister(BTPROTO_SCO);
1520 BT_INFO("SCO socket layer initialized");
1522 hci_register_cb(&sco_cb);
1524 if (IS_ERR_OR_NULL(bt_debugfs))
1527 sco_debugfs = debugfs_create_file("sco", 0444, bt_debugfs,
1528 NULL, &sco_debugfs_fops);
1533 proto_unregister(&sco_proto);
1539 bt_procfs_cleanup(&init_net, "sco");
1541 debugfs_remove(sco_debugfs);
1543 hci_unregister_cb(&sco_cb);
1545 bt_sock_unregister(BTPROTO_SCO);
1547 proto_unregister(&sco_proto);
1550 module_param(disable_esco, bool, 0644);
1551 MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");