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 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;
131 conn = kzalloc(sizeof(struct sco_conn), GFP_KERNEL);
135 spin_lock_init(&conn->lock);
137 hcon->sco_data = conn;
140 if (hdev->sco_mtu > 0)
141 conn->mtu = hdev->sco_mtu;
145 BT_DBG("hcon %p conn %p", hcon, conn);
151 * Must be called on the locked socket. */
152 static void sco_chan_del(struct sock *sk, int err)
154 struct sco_conn *conn;
156 conn = sco_pi(sk)->conn;
158 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
163 sco_pi(sk)->conn = NULL;
164 sco_conn_unlock(conn);
167 hci_conn_drop(conn->hcon);
170 sk->sk_state = BT_CLOSED;
172 sk->sk_state_change(sk);
174 sock_set_flag(sk, SOCK_ZAPPED);
177 static void sco_conn_del(struct hci_conn *hcon, int err)
179 struct sco_conn *conn = hcon->sco_data;
185 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
190 sco_conn_unlock(conn);
195 sco_sock_clear_timer(sk);
196 sco_chan_del(sk, err);
200 /* Ensure no more work items will run before freeing conn. */
201 cancel_delayed_work_sync(&conn->timeout_work);
204 hcon->sco_data = NULL;
208 static void __sco_chan_add(struct sco_conn *conn, struct sock *sk,
211 BT_DBG("conn %p", conn);
213 sco_pi(sk)->conn = conn;
216 INIT_DELAYED_WORK(&conn->timeout_work, sco_sock_timeout);
219 bt_accept_enqueue(parent, sk, true);
222 static int sco_chan_add(struct sco_conn *conn, struct sock *sk,
231 __sco_chan_add(conn, sk, parent);
233 sco_conn_unlock(conn);
237 static int sco_connect(struct hci_dev *hdev, struct sock *sk)
239 struct sco_conn *conn;
240 struct hci_conn *hcon;
243 BT_DBG("%pMR -> %pMR", &sco_pi(sk)->src, &sco_pi(sk)->dst);
245 if (lmp_esco_capable(hdev) && !disable_esco)
250 if (sco_pi(sk)->setting == BT_VOICE_TRANSPARENT &&
251 (!lmp_transp_capable(hdev) || !lmp_esco_capable(hdev)))
254 hcon = hci_connect_sco(hdev, type, &sco_pi(sk)->dst,
255 sco_pi(sk)->setting);
257 return PTR_ERR(hcon);
259 conn = sco_conn_add(hcon);
265 /* Update source addr of the socket */
266 bacpy(&sco_pi(sk)->src, &hcon->src);
268 err = sco_chan_add(conn, sk, NULL);
272 if (hcon->state == BT_CONNECTED) {
273 sco_sock_clear_timer(sk);
274 sk->sk_state = BT_CONNECTED;
276 sk->sk_state = BT_CONNECT;
277 sco_sock_set_timer(sk, sk->sk_sndtimeo);
283 static int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
285 struct sco_conn *conn = sco_pi(sk)->conn;
289 /* Check outgoing MTU */
293 BT_DBG("sk %p len %d", sk, len);
295 skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
299 if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
304 hci_send_sco(conn->hcon, skb);
309 static void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
315 sco_conn_unlock(conn);
320 BT_DBG("sk %p len %u", sk, skb->len);
322 if (sk->sk_state != BT_CONNECTED)
325 if (!sock_queue_rcv_skb(sk, skb))
332 /* -------- Socket interface ---------- */
333 static struct sock *__sco_get_sock_listen_by_addr(bdaddr_t *ba)
337 sk_for_each(sk, &sco_sk_list.head) {
338 if (sk->sk_state != BT_LISTEN)
341 if (!bacmp(&sco_pi(sk)->src, ba))
348 /* Find socket listening on source bdaddr.
349 * Returns closest match.
351 static struct sock *sco_get_sock_listen(bdaddr_t *src)
353 struct sock *sk = NULL, *sk1 = NULL;
355 read_lock(&sco_sk_list.lock);
357 sk_for_each(sk, &sco_sk_list.head) {
358 if (sk->sk_state != BT_LISTEN)
362 if (!bacmp(&sco_pi(sk)->src, src))
366 if (!bacmp(&sco_pi(sk)->src, BDADDR_ANY))
370 read_unlock(&sco_sk_list.lock);
372 return sk ? sk : sk1;
375 static void sco_sock_destruct(struct sock *sk)
379 skb_queue_purge(&sk->sk_receive_queue);
380 skb_queue_purge(&sk->sk_write_queue);
383 static void sco_sock_cleanup_listen(struct sock *parent)
387 BT_DBG("parent %p", parent);
389 /* Close not yet accepted channels */
390 while ((sk = bt_accept_dequeue(parent, NULL))) {
395 parent->sk_state = BT_CLOSED;
396 sock_set_flag(parent, SOCK_ZAPPED);
399 /* Kill socket (only if zapped and orphan)
400 * Must be called on unlocked socket.
402 static void sco_sock_kill(struct sock *sk)
404 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket)
407 BT_DBG("sk %p state %d", sk, sk->sk_state);
409 /* Kill poor orphan */
410 bt_sock_unlink(&sco_sk_list, sk);
411 sock_set_flag(sk, SOCK_DEAD);
415 static void __sco_sock_close(struct sock *sk)
417 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
419 switch (sk->sk_state) {
421 sco_sock_cleanup_listen(sk);
426 if (sco_pi(sk)->conn->hcon) {
427 sk->sk_state = BT_DISCONN;
428 sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT);
429 sco_conn_lock(sco_pi(sk)->conn);
430 hci_conn_drop(sco_pi(sk)->conn->hcon);
431 sco_pi(sk)->conn->hcon = NULL;
432 sco_conn_unlock(sco_pi(sk)->conn);
434 sco_chan_del(sk, ECONNRESET);
440 sco_chan_del(sk, ECONNRESET);
444 sock_set_flag(sk, SOCK_ZAPPED);
449 /* Must be called on unlocked socket. */
450 static void sco_sock_close(struct sock *sk)
453 sco_sock_clear_timer(sk);
454 __sco_sock_close(sk);
458 static void sco_skb_put_cmsg(struct sk_buff *skb, struct msghdr *msg,
461 if (sco_pi(sk)->cmsg_mask & SCO_CMSG_PKT_STATUS)
462 put_cmsg(msg, SOL_BLUETOOTH, BT_SCM_PKT_STATUS,
463 sizeof(bt_cb(skb)->sco.pkt_status),
464 &bt_cb(skb)->sco.pkt_status);
467 static void sco_sock_init(struct sock *sk, struct sock *parent)
472 sk->sk_type = parent->sk_type;
473 bt_sk(sk)->flags = bt_sk(parent)->flags;
474 security_sk_clone(parent, sk);
476 bt_sk(sk)->skb_put_cmsg = sco_skb_put_cmsg;
480 static struct proto sco_proto = {
482 .owner = THIS_MODULE,
483 .obj_size = sizeof(struct sco_pinfo)
486 static struct sock *sco_sock_alloc(struct net *net, struct socket *sock,
487 int proto, gfp_t prio, int kern)
491 sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto, kern);
495 sock_init_data(sock, sk);
496 INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
498 sk->sk_destruct = sco_sock_destruct;
499 sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
501 sock_reset_flag(sk, SOCK_ZAPPED);
503 sk->sk_protocol = proto;
504 sk->sk_state = BT_OPEN;
506 sco_pi(sk)->setting = BT_VOICE_CVSD_16BIT;
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;
572 struct hci_dev *hdev;
577 if (alen < sizeof(struct sockaddr_sco) ||
578 addr->sa_family != AF_BLUETOOTH)
581 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
584 if (sk->sk_type != SOCK_SEQPACKET)
587 hdev = hci_get_route(&sa->sco_bdaddr, &sco_pi(sk)->src, BDADDR_BREDR);
589 return -EHOSTUNREACH;
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;
730 BT_DBG("sock %p, sk %p", sock, sk);
732 err = sock_error(sk);
736 if (msg->msg_flags & MSG_OOB)
741 if (sk->sk_state == BT_CONNECTED)
742 err = sco_send_frame(sk, msg, len);
750 static void sco_conn_defer_accept(struct hci_conn *conn, u16 setting)
752 struct hci_dev *hdev = conn->hdev;
754 BT_DBG("conn %p", conn);
756 conn->state = BT_CONFIG;
758 if (!lmp_esco_capable(hdev)) {
759 struct hci_cp_accept_conn_req cp;
761 bacpy(&cp.bdaddr, &conn->dst);
762 cp.role = 0x00; /* Ignored */
764 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
766 struct hci_cp_accept_sync_conn_req cp;
768 bacpy(&cp.bdaddr, &conn->dst);
769 cp.pkt_type = cpu_to_le16(conn->pkt_type);
771 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
772 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
773 cp.content_format = cpu_to_le16(setting);
775 switch (setting & SCO_AIRMODE_MASK) {
776 case SCO_AIRMODE_TRANSP:
777 if (conn->pkt_type & ESCO_2EV3)
778 cp.max_latency = cpu_to_le16(0x0008);
780 cp.max_latency = cpu_to_le16(0x000D);
781 cp.retrans_effort = 0x02;
783 case SCO_AIRMODE_CVSD:
784 cp.max_latency = cpu_to_le16(0xffff);
785 cp.retrans_effort = 0xff;
788 /* use CVSD settings as fallback */
789 cp.max_latency = cpu_to_le16(0xffff);
790 cp.retrans_effort = 0xff;
794 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
799 static int sco_sock_recvmsg(struct socket *sock, struct msghdr *msg,
800 size_t len, int flags)
802 struct sock *sk = sock->sk;
803 struct sco_pinfo *pi = sco_pi(sk);
807 if (sk->sk_state == BT_CONNECT2 &&
808 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
809 sco_conn_defer_accept(pi->conn->hcon, pi->setting);
810 sk->sk_state = BT_CONFIG;
818 return bt_sock_recvmsg(sock, msg, len, flags);
821 static int sco_sock_setsockopt(struct socket *sock, int level, int optname,
822 sockptr_t optval, unsigned int optlen)
824 struct sock *sk = sock->sk;
826 struct bt_voice voice;
836 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
841 if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
847 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
849 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
853 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
854 sk->sk_state != BT_CONNECT2) {
859 voice.setting = sco_pi(sk)->setting;
861 len = min_t(unsigned int, sizeof(voice), optlen);
862 if (copy_from_sockptr(&voice, optval, len)) {
867 /* Explicitly check for these values */
868 if (voice.setting != BT_VOICE_TRANSPARENT &&
869 voice.setting != BT_VOICE_CVSD_16BIT) {
874 sco_pi(sk)->setting = voice.setting;
878 if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
884 sco_pi(sk)->cmsg_mask |= SCO_CMSG_PKT_STATUS;
886 sco_pi(sk)->cmsg_mask &= SCO_CMSG_PKT_STATUS;
898 static int sco_sock_getsockopt_old(struct socket *sock, int optname,
899 char __user *optval, int __user *optlen)
901 struct sock *sk = sock->sk;
902 struct sco_options opts;
903 struct sco_conninfo cinfo;
908 if (get_user(len, optlen))
915 if (sk->sk_state != BT_CONNECTED &&
916 !(sk->sk_state == BT_CONNECT2 &&
917 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
922 opts.mtu = sco_pi(sk)->conn->mtu;
924 BT_DBG("mtu %u", opts.mtu);
926 len = min_t(unsigned int, len, sizeof(opts));
927 if (copy_to_user(optval, (char *)&opts, len))
933 if (sk->sk_state != BT_CONNECTED &&
934 !(sk->sk_state == BT_CONNECT2 &&
935 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
940 memset(&cinfo, 0, sizeof(cinfo));
941 cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
942 memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
944 len = min_t(unsigned int, len, sizeof(cinfo));
945 if (copy_to_user(optval, (char *)&cinfo, len))
959 static int sco_sock_getsockopt(struct socket *sock, int level, int optname,
960 char __user *optval, int __user *optlen)
962 struct sock *sk = sock->sk;
964 struct bt_voice voice;
970 if (level == SOL_SCO)
971 return sco_sock_getsockopt_old(sock, optname, optval, optlen);
973 if (get_user(len, optlen))
981 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
986 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
987 (u32 __user *)optval))
993 voice.setting = sco_pi(sk)->setting;
995 len = min_t(unsigned int, len, sizeof(voice));
996 if (copy_to_user(optval, (char *)&voice, len))
1002 if (sk->sk_state != BT_CONNECTED) {
1007 phys = hci_conn_get_phy(sco_pi(sk)->conn->hcon);
1009 if (put_user(phys, (u32 __user *) optval))
1014 pkt_status = (sco_pi(sk)->cmsg_mask & SCO_CMSG_PKT_STATUS);
1016 if (put_user(pkt_status, (int __user *)optval))
1022 if (sk->sk_state != BT_CONNECTED) {
1027 if (put_user(sco_pi(sk)->conn->mtu, (u32 __user *)optval))
1040 static int sco_sock_shutdown(struct socket *sock, int how)
1042 struct sock *sk = sock->sk;
1045 BT_DBG("sock %p, sk %p", sock, sk);
1053 if (!sk->sk_shutdown) {
1054 sk->sk_shutdown = SHUTDOWN_MASK;
1055 sco_sock_clear_timer(sk);
1056 __sco_sock_close(sk);
1058 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1059 !(current->flags & PF_EXITING))
1060 err = bt_sock_wait_state(sk, BT_CLOSED,
1070 static int sco_sock_release(struct socket *sock)
1072 struct sock *sk = sock->sk;
1075 BT_DBG("sock %p, sk %p", sock, sk);
1082 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1083 !(current->flags & PF_EXITING)) {
1085 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1094 static void sco_conn_ready(struct sco_conn *conn)
1096 struct sock *parent;
1097 struct sock *sk = conn->sk;
1099 BT_DBG("conn %p", conn);
1103 sco_sock_clear_timer(sk);
1104 sk->sk_state = BT_CONNECTED;
1105 sk->sk_state_change(sk);
1108 sco_conn_lock(conn);
1111 sco_conn_unlock(conn);
1115 parent = sco_get_sock_listen(&conn->hcon->src);
1117 sco_conn_unlock(conn);
1123 sk = sco_sock_alloc(sock_net(parent), NULL,
1124 BTPROTO_SCO, GFP_ATOMIC, 0);
1126 release_sock(parent);
1127 sco_conn_unlock(conn);
1131 sco_sock_init(sk, parent);
1133 bacpy(&sco_pi(sk)->src, &conn->hcon->src);
1134 bacpy(&sco_pi(sk)->dst, &conn->hcon->dst);
1136 hci_conn_hold(conn->hcon);
1137 __sco_chan_add(conn, sk, parent);
1139 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
1140 sk->sk_state = BT_CONNECT2;
1142 sk->sk_state = BT_CONNECTED;
1144 /* Wake up parent */
1145 parent->sk_data_ready(parent);
1147 release_sock(parent);
1149 sco_conn_unlock(conn);
1153 /* ----- SCO interface with lower layer (HCI) ----- */
1154 int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
1159 BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
1161 /* Find listening sockets */
1162 read_lock(&sco_sk_list.lock);
1163 sk_for_each(sk, &sco_sk_list.head) {
1164 if (sk->sk_state != BT_LISTEN)
1167 if (!bacmp(&sco_pi(sk)->src, &hdev->bdaddr) ||
1168 !bacmp(&sco_pi(sk)->src, BDADDR_ANY)) {
1169 lm |= HCI_LM_ACCEPT;
1171 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
1172 *flags |= HCI_PROTO_DEFER;
1176 read_unlock(&sco_sk_list.lock);
1181 static void sco_connect_cfm(struct hci_conn *hcon, __u8 status)
1183 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
1186 BT_DBG("hcon %p bdaddr %pMR status %u", hcon, &hcon->dst, status);
1189 struct sco_conn *conn;
1191 conn = sco_conn_add(hcon);
1193 sco_conn_ready(conn);
1195 sco_conn_del(hcon, bt_to_errno(status));
1198 static void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
1200 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
1203 BT_DBG("hcon %p reason %d", hcon, reason);
1205 sco_conn_del(hcon, bt_to_errno(reason));
1208 void sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
1210 struct sco_conn *conn = hcon->sco_data;
1215 BT_DBG("conn %p len %u", conn, skb->len);
1218 sco_recv_frame(conn, skb);
1226 static struct hci_cb sco_cb = {
1228 .connect_cfm = sco_connect_cfm,
1229 .disconn_cfm = sco_disconn_cfm,
1232 static int sco_debugfs_show(struct seq_file *f, void *p)
1236 read_lock(&sco_sk_list.lock);
1238 sk_for_each(sk, &sco_sk_list.head) {
1239 seq_printf(f, "%pMR %pMR %d\n", &sco_pi(sk)->src,
1240 &sco_pi(sk)->dst, sk->sk_state);
1243 read_unlock(&sco_sk_list.lock);
1248 DEFINE_SHOW_ATTRIBUTE(sco_debugfs);
1250 static struct dentry *sco_debugfs;
1252 static const struct proto_ops sco_sock_ops = {
1253 .family = PF_BLUETOOTH,
1254 .owner = THIS_MODULE,
1255 .release = sco_sock_release,
1256 .bind = sco_sock_bind,
1257 .connect = sco_sock_connect,
1258 .listen = sco_sock_listen,
1259 .accept = sco_sock_accept,
1260 .getname = sco_sock_getname,
1261 .sendmsg = sco_sock_sendmsg,
1262 .recvmsg = sco_sock_recvmsg,
1263 .poll = bt_sock_poll,
1264 .ioctl = bt_sock_ioctl,
1265 .gettstamp = sock_gettstamp,
1266 .mmap = sock_no_mmap,
1267 .socketpair = sock_no_socketpair,
1268 .shutdown = sco_sock_shutdown,
1269 .setsockopt = sco_sock_setsockopt,
1270 .getsockopt = sco_sock_getsockopt
1273 static const struct net_proto_family sco_sock_family_ops = {
1274 .family = PF_BLUETOOTH,
1275 .owner = THIS_MODULE,
1276 .create = sco_sock_create,
1279 int __init sco_init(void)
1283 BUILD_BUG_ON(sizeof(struct sockaddr_sco) > sizeof(struct sockaddr));
1285 err = proto_register(&sco_proto, 0);
1289 err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
1291 BT_ERR("SCO socket registration failed");
1295 err = bt_procfs_init(&init_net, "sco", &sco_sk_list, NULL);
1297 BT_ERR("Failed to create SCO proc file");
1298 bt_sock_unregister(BTPROTO_SCO);
1302 BT_INFO("SCO socket layer initialized");
1304 hci_register_cb(&sco_cb);
1306 if (IS_ERR_OR_NULL(bt_debugfs))
1309 sco_debugfs = debugfs_create_file("sco", 0444, bt_debugfs,
1310 NULL, &sco_debugfs_fops);
1315 proto_unregister(&sco_proto);
1321 bt_procfs_cleanup(&init_net, "sco");
1323 debugfs_remove(sco_debugfs);
1325 hci_unregister_cb(&sco_cb);
1327 bt_sock_unregister(BTPROTO_SCO);
1329 proto_unregister(&sco_proto);
1332 module_param(disable_esco, bool, 0644);
1333 MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");