YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | Copyright (C) 2000-2001 Qualcomm Incorporated |
| 4 | |
| 5 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> |
| 6 | |
| 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; |
| 10 | |
| 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 |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | SOFTWARE IS DISCLAIMED. |
| 23 | */ |
| 24 | |
| 25 | /* Bluetooth SCO sockets. */ |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/module.h> |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 28 | #include <linux/debugfs.h> |
| 29 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | #include <net/bluetooth/bluetooth.h> |
| 32 | #include <net/bluetooth/hci_core.h> |
| 33 | #include <net/bluetooth/sco.h> |
| 34 | |
Rusty Russell | eb93992 | 2011-12-19 14:08:01 +0000 | [diff] [blame] | 35 | static bool disable_esco; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 37 | static const struct proto_ops sco_sock_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | static struct bt_sock_list sco_sk_list = { |
Robert P. J. Day | d5fb296 | 2008-03-28 16:17:38 -0700 | [diff] [blame] | 40 | .lock = __RW_LOCK_UNLOCKED(sco_sk_list.lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent); |
| 44 | static void sco_chan_del(struct sock *sk, int err); |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | static void sco_sock_close(struct sock *sk); |
| 47 | static void sco_sock_kill(struct sock *sk); |
| 48 | |
| 49 | /* ---- SCO timers ---- */ |
| 50 | static void sco_sock_timeout(unsigned long arg) |
| 51 | { |
| 52 | struct sock *sk = (struct sock *) arg; |
| 53 | |
| 54 | BT_DBG("sock %p state %d", sk, sk->sk_state); |
| 55 | |
| 56 | bh_lock_sock(sk); |
| 57 | sk->sk_err = ETIMEDOUT; |
| 58 | sk->sk_state_change(sk); |
| 59 | bh_unlock_sock(sk); |
| 60 | |
| 61 | sco_sock_kill(sk); |
| 62 | sock_put(sk); |
| 63 | } |
| 64 | |
| 65 | static void sco_sock_set_timer(struct sock *sk, long timeout) |
| 66 | { |
| 67 | BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout); |
| 68 | sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout); |
| 69 | } |
| 70 | |
| 71 | static void sco_sock_clear_timer(struct sock *sk) |
| 72 | { |
| 73 | BT_DBG("sock %p state %d", sk, sk->sk_state); |
| 74 | sk_stop_timer(sk, &sk->sk_timer); |
| 75 | } |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /* ---- SCO connections ---- */ |
Lukasz Rymanowski | 519e42b | 2012-04-19 16:12:28 +0200 | [diff] [blame] | 78 | static struct sco_conn *sco_conn_add(struct hci_conn *hcon) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
| 80 | struct hci_dev *hdev = hcon->hdev; |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 81 | struct sco_conn *conn = hcon->sco_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Lukasz Rymanowski | 519e42b | 2012-04-19 16:12:28 +0200 | [diff] [blame] | 83 | if (conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return conn; |
| 85 | |
Claudio Takahasi | c10cc5a | 2013-04-11 11:35:45 -0300 | [diff] [blame] | 86 | conn = kzalloc(sizeof(struct sco_conn), GFP_KERNEL); |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 87 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | spin_lock_init(&conn->lock); |
| 91 | |
| 92 | hcon->sco_data = conn; |
| 93 | conn->hcon = hcon; |
| 94 | |
| 95 | conn->src = &hdev->bdaddr; |
| 96 | conn->dst = &hcon->dst; |
| 97 | |
| 98 | if (hdev->sco_mtu > 0) |
| 99 | conn->mtu = hdev->sco_mtu; |
| 100 | else |
| 101 | conn->mtu = 60; |
| 102 | |
| 103 | BT_DBG("hcon %p conn %p", hcon, conn); |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | return conn; |
| 106 | } |
| 107 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 108 | static struct sock *sco_chan_get(struct sco_conn *conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
| 110 | struct sock *sk = NULL; |
| 111 | sco_conn_lock(conn); |
| 112 | sk = conn->sk; |
| 113 | sco_conn_unlock(conn); |
| 114 | return sk; |
| 115 | } |
| 116 | |
| 117 | static int sco_conn_del(struct hci_conn *hcon, int err) |
| 118 | { |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 119 | struct sco_conn *conn = hcon->sco_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | struct sock *sk; |
| 121 | |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 122 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | return 0; |
| 124 | |
| 125 | BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); |
| 126 | |
| 127 | /* Kill socket */ |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 128 | sk = sco_chan_get(conn); |
| 129 | if (sk) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | bh_lock_sock(sk); |
| 131 | sco_sock_clear_timer(sk); |
| 132 | sco_chan_del(sk, err); |
| 133 | bh_unlock_sock(sk); |
| 134 | sco_sock_kill(sk); |
| 135 | } |
| 136 | |
| 137 | hcon->sco_data = NULL; |
| 138 | kfree(conn); |
| 139 | return 0; |
| 140 | } |
| 141 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 142 | static int sco_chan_add(struct sco_conn *conn, struct sock *sk, |
| 143 | struct sock *parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
| 145 | int err = 0; |
| 146 | |
| 147 | sco_conn_lock(conn); |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 148 | if (conn->sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | err = -EBUSY; |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 150 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | __sco_chan_add(conn, sk, parent); |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | sco_conn_unlock(conn); |
| 154 | return err; |
| 155 | } |
| 156 | |
| 157 | static int sco_connect(struct sock *sk) |
| 158 | { |
| 159 | bdaddr_t *src = &bt_sk(sk)->src; |
| 160 | bdaddr_t *dst = &bt_sk(sk)->dst; |
| 161 | struct sco_conn *conn; |
| 162 | struct hci_conn *hcon; |
| 163 | struct hci_dev *hdev; |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 164 | int err, type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 166 | BT_DBG("%pMR -> %pMR", src, dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 168 | hdev = hci_get_route(dst, src); |
| 169 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | return -EHOSTUNREACH; |
| 171 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 172 | hci_dev_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Marcel Holtmann | 7cb127d | 2008-07-14 20:13:53 +0200 | [diff] [blame] | 174 | if (lmp_esco_capable(hdev) && !disable_esco) |
| 175 | type = ESCO_LINK; |
| 176 | else |
| 177 | type = SCO_LINK; |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 178 | |
Andre Guedes | b12f62c | 2012-04-24 21:02:54 -0300 | [diff] [blame] | 179 | hcon = hci_connect(hdev, type, dst, BDADDR_BREDR, BT_SECURITY_LOW, |
| 180 | HCI_AT_NO_BONDING); |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 181 | if (IS_ERR(hcon)) { |
| 182 | err = PTR_ERR(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | goto done; |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 184 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Lukasz Rymanowski | 519e42b | 2012-04-19 16:12:28 +0200 | [diff] [blame] | 186 | conn = sco_conn_add(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | if (!conn) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 188 | hci_conn_drop(hcon); |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 189 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | goto done; |
| 191 | } |
| 192 | |
| 193 | /* Update source addr of the socket */ |
| 194 | bacpy(src, conn->src); |
| 195 | |
| 196 | err = sco_chan_add(conn, sk, NULL); |
| 197 | if (err) |
| 198 | goto done; |
| 199 | |
| 200 | if (hcon->state == BT_CONNECTED) { |
| 201 | sco_sock_clear_timer(sk); |
| 202 | sk->sk_state = BT_CONNECTED; |
| 203 | } else { |
| 204 | sk->sk_state = BT_CONNECT; |
| 205 | sco_sock_set_timer(sk, sk->sk_sndtimeo); |
| 206 | } |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 209 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | hci_dev_put(hdev); |
| 211 | return err; |
| 212 | } |
| 213 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 214 | static int sco_send_frame(struct sock *sk, struct msghdr *msg, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { |
| 216 | struct sco_conn *conn = sco_pi(sk)->conn; |
| 217 | struct sk_buff *skb; |
Mikel Astiz | 088ce08 | 2012-04-11 08:48:48 +0200 | [diff] [blame] | 218 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
| 220 | /* Check outgoing MTU */ |
| 221 | if (len > conn->mtu) |
| 222 | return -EINVAL; |
| 223 | |
| 224 | BT_DBG("sk %p len %d", sk, len); |
| 225 | |
Mikel Astiz | 088ce08 | 2012-04-11 08:48:48 +0200 | [diff] [blame] | 226 | skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err); |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 227 | if (!skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | return err; |
| 229 | |
Mikel Astiz | 088ce08 | 2012-04-11 08:48:48 +0200 | [diff] [blame] | 230 | if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) { |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 231 | kfree_skb(skb); |
| 232 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Gustavo F. Padovan | 0d861d8 | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 235 | hci_send_sco(conn->hcon, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
Mikel Astiz | 088ce08 | 2012-04-11 08:48:48 +0200 | [diff] [blame] | 237 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 240 | static void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
| 242 | struct sock *sk = sco_chan_get(conn); |
| 243 | |
| 244 | if (!sk) |
| 245 | goto drop; |
| 246 | |
| 247 | BT_DBG("sk %p len %d", sk, skb->len); |
| 248 | |
| 249 | if (sk->sk_state != BT_CONNECTED) |
| 250 | goto drop; |
| 251 | |
| 252 | if (!sock_queue_rcv_skb(sk, skb)) |
| 253 | return; |
| 254 | |
| 255 | drop: |
| 256 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* -------- Socket interface ---------- */ |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 260 | static struct sock *__sco_get_sock_listen_by_addr(bdaddr_t *ba) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | { |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 262 | struct sock *sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 264 | sk_for_each(sk, &sco_sk_list.head) { |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 265 | if (sk->sk_state != BT_LISTEN) |
| 266 | continue; |
| 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | if (!bacmp(&bt_sk(sk)->src, ba)) |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 269 | return sk; |
| 270 | } |
| 271 | |
| 272 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* Find socket listening on source bdaddr. |
| 276 | * Returns closest match. |
| 277 | */ |
| 278 | static struct sock *sco_get_sock_listen(bdaddr_t *src) |
| 279 | { |
| 280 | struct sock *sk = NULL, *sk1 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
| 282 | read_lock(&sco_sk_list.lock); |
| 283 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 284 | sk_for_each(sk, &sco_sk_list.head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | if (sk->sk_state != BT_LISTEN) |
| 286 | continue; |
| 287 | |
| 288 | /* Exact match. */ |
| 289 | if (!bacmp(&bt_sk(sk)->src, src)) |
| 290 | break; |
| 291 | |
| 292 | /* Closest match */ |
| 293 | if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) |
| 294 | sk1 = sk; |
| 295 | } |
| 296 | |
| 297 | read_unlock(&sco_sk_list.lock); |
| 298 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 299 | return sk ? sk : sk1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static void sco_sock_destruct(struct sock *sk) |
| 303 | { |
| 304 | BT_DBG("sk %p", sk); |
| 305 | |
| 306 | skb_queue_purge(&sk->sk_receive_queue); |
| 307 | skb_queue_purge(&sk->sk_write_queue); |
| 308 | } |
| 309 | |
| 310 | static void sco_sock_cleanup_listen(struct sock *parent) |
| 311 | { |
| 312 | struct sock *sk; |
| 313 | |
| 314 | BT_DBG("parent %p", parent); |
| 315 | |
| 316 | /* Close not yet accepted channels */ |
| 317 | while ((sk = bt_accept_dequeue(parent, NULL))) { |
| 318 | sco_sock_close(sk); |
| 319 | sco_sock_kill(sk); |
| 320 | } |
| 321 | |
| 322 | parent->sk_state = BT_CLOSED; |
| 323 | sock_set_flag(parent, SOCK_ZAPPED); |
| 324 | } |
| 325 | |
| 326 | /* Kill socket (only if zapped and orphan) |
| 327 | * Must be called on unlocked socket. |
| 328 | */ |
| 329 | static void sco_sock_kill(struct sock *sk) |
| 330 | { |
| 331 | if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket) |
| 332 | return; |
| 333 | |
| 334 | BT_DBG("sk %p state %d", sk, sk->sk_state); |
| 335 | |
| 336 | /* Kill poor orphan */ |
| 337 | bt_sock_unlink(&sco_sk_list, sk); |
| 338 | sock_set_flag(sk, SOCK_DEAD); |
| 339 | sock_put(sk); |
| 340 | } |
| 341 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 342 | static void __sco_sock_close(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | { |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 344 | BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
| 346 | switch (sk->sk_state) { |
| 347 | case BT_LISTEN: |
| 348 | sco_sock_cleanup_listen(sk); |
| 349 | break; |
| 350 | |
| 351 | case BT_CONNECTED: |
| 352 | case BT_CONFIG: |
Gustavo Padovan | b7e98b5 | 2013-01-03 19:59:28 -0200 | [diff] [blame] | 353 | if (sco_pi(sk)->conn->hcon) { |
Luiz Augusto von Dentz | 4a77708 | 2011-05-12 11:13:15 +0300 | [diff] [blame] | 354 | sk->sk_state = BT_DISCONN; |
| 355 | sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT); |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 356 | hci_conn_drop(sco_pi(sk)->conn->hcon); |
Luiz Augusto von Dentz | 4a77708 | 2011-05-12 11:13:15 +0300 | [diff] [blame] | 357 | sco_pi(sk)->conn->hcon = NULL; |
| 358 | } else |
| 359 | sco_chan_del(sk, ECONNRESET); |
| 360 | break; |
| 361 | |
Vinicius Costa Gomes | eb20ff9 | 2013-03-13 19:46:20 -0300 | [diff] [blame] | 362 | case BT_CONNECT2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | case BT_CONNECT: |
| 364 | case BT_DISCONN: |
| 365 | sco_chan_del(sk, ECONNRESET); |
| 366 | break; |
| 367 | |
| 368 | default: |
| 369 | sock_set_flag(sk, SOCK_ZAPPED); |
| 370 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 371 | } |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 374 | /* Must be called on unlocked socket. */ |
| 375 | static void sco_sock_close(struct sock *sk) |
| 376 | { |
| 377 | sco_sock_clear_timer(sk); |
| 378 | lock_sock(sk); |
| 379 | __sco_sock_close(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | sco_sock_kill(sk); |
| 382 | } |
| 383 | |
| 384 | static void sco_sock_init(struct sock *sk, struct sock *parent) |
| 385 | { |
| 386 | BT_DBG("sk %p", sk); |
| 387 | |
Paul Moore | 6230c9b | 2011-10-07 09:40:59 +0000 | [diff] [blame] | 388 | if (parent) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | sk->sk_type = parent->sk_type; |
Frédéric Dalleau | 20714bf | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 390 | bt_sk(sk)->flags = bt_sk(parent)->flags; |
Paul Moore | 6230c9b | 2011-10-07 09:40:59 +0000 | [diff] [blame] | 391 | security_sk_clone(parent, sk); |
| 392 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static struct proto sco_proto = { |
| 396 | .name = "SCO", |
| 397 | .owner = THIS_MODULE, |
| 398 | .obj_size = sizeof(struct sco_pinfo) |
| 399 | }; |
| 400 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 401 | static struct sock *sco_sock_alloc(struct net *net, struct socket *sock, int proto, gfp_t prio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | { |
| 403 | struct sock *sk; |
| 404 | |
Pavel Emelyanov | 6257ff2 | 2007-11-01 00:39:31 -0700 | [diff] [blame] | 405 | sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | if (!sk) |
| 407 | return NULL; |
| 408 | |
| 409 | sock_init_data(sock, sk); |
| 410 | INIT_LIST_HEAD(&bt_sk(sk)->accept_q); |
| 411 | |
| 412 | sk->sk_destruct = sco_sock_destruct; |
| 413 | sk->sk_sndtimeo = SCO_CONN_TIMEOUT; |
| 414 | |
| 415 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 416 | |
| 417 | sk->sk_protocol = proto; |
| 418 | sk->sk_state = BT_OPEN; |
| 419 | |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 420 | setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
| 422 | bt_sock_link(&sco_sk_list, sk); |
| 423 | return sk; |
| 424 | } |
| 425 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 426 | static int sco_sock_create(struct net *net, struct socket *sock, int protocol, |
| 427 | int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | { |
| 429 | struct sock *sk; |
| 430 | |
| 431 | BT_DBG("sock %p", sock); |
| 432 | |
| 433 | sock->state = SS_UNCONNECTED; |
| 434 | |
| 435 | if (sock->type != SOCK_SEQPACKET) |
| 436 | return -ESOCKTNOSUPPORT; |
| 437 | |
| 438 | sock->ops = &sco_sock_ops; |
| 439 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 440 | sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC); |
Marcel Holtmann | 74da626 | 2006-10-15 17:31:14 +0200 | [diff] [blame] | 441 | if (!sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | return -ENOMEM; |
| 443 | |
| 444 | sco_sock_init(sk, NULL); |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len) |
| 449 | { |
| 450 | struct sockaddr_sco *sa = (struct sockaddr_sco *) addr; |
| 451 | struct sock *sk = sock->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | int err = 0; |
| 453 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 454 | BT_DBG("sk %p %pMR", sk, &sa->sco_bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
| 456 | if (!addr || addr->sa_family != AF_BLUETOOTH) |
| 457 | return -EINVAL; |
| 458 | |
| 459 | lock_sock(sk); |
| 460 | |
| 461 | if (sk->sk_state != BT_OPEN) { |
| 462 | err = -EBADFD; |
| 463 | goto done; |
| 464 | } |
| 465 | |
Marcel Holtmann | 8ed21f7 | 2012-04-19 13:43:53 +0200 | [diff] [blame] | 466 | if (sk->sk_type != SOCK_SEQPACKET) { |
| 467 | err = -EINVAL; |
| 468 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Marcel Holtmann | 8ed21f7 | 2012-04-19 13:43:53 +0200 | [diff] [blame] | 471 | bacpy(&bt_sk(sk)->src, &sa->sco_bdaddr); |
| 472 | |
| 473 | sk->sk_state = BT_BOUND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
| 475 | done: |
| 476 | release_sock(sk); |
| 477 | return err; |
| 478 | } |
| 479 | |
| 480 | static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags) |
| 481 | { |
| 482 | struct sockaddr_sco *sa = (struct sockaddr_sco *) addr; |
| 483 | struct sock *sk = sock->sk; |
Claudio Takahasi | 92f185c | 2013-04-11 11:35:46 -0300 | [diff] [blame] | 484 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
| 486 | BT_DBG("sk %p", sk); |
| 487 | |
Changli Gao | 6503d96 | 2010-03-31 22:58:26 +0000 | [diff] [blame] | 488 | if (alen < sizeof(struct sockaddr_sco) || |
| 489 | addr->sa_family != AF_BLUETOOTH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | return -EINVAL; |
| 491 | |
| 492 | if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) |
| 493 | return -EBADFD; |
| 494 | |
| 495 | if (sk->sk_type != SOCK_SEQPACKET) |
| 496 | return -EINVAL; |
| 497 | |
| 498 | lock_sock(sk); |
| 499 | |
| 500 | /* Set destination address and psm */ |
| 501 | bacpy(&bt_sk(sk)->dst, &sa->sco_bdaddr); |
| 502 | |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 503 | err = sco_connect(sk); |
| 504 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | goto done; |
| 506 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 507 | err = bt_sock_wait_state(sk, BT_CONNECTED, |
Gustavo Padovan | be7c2b9 | 2012-05-17 00:36:21 -0300 | [diff] [blame] | 508 | sock_sndtimeo(sk, flags & O_NONBLOCK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
| 510 | done: |
| 511 | release_sock(sk); |
| 512 | return err; |
| 513 | } |
| 514 | |
| 515 | static int sco_sock_listen(struct socket *sock, int backlog) |
| 516 | { |
| 517 | struct sock *sk = sock->sk; |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 518 | bdaddr_t *src = &bt_sk(sk)->src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | int err = 0; |
| 520 | |
| 521 | BT_DBG("sk %p backlog %d", sk, backlog); |
| 522 | |
| 523 | lock_sock(sk); |
| 524 | |
Marcel Holtmann | 7d5d775 | 2012-04-19 13:43:52 +0200 | [diff] [blame] | 525 | if (sk->sk_state != BT_BOUND) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | err = -EBADFD; |
| 527 | goto done; |
| 528 | } |
| 529 | |
Marcel Holtmann | 7d5d775 | 2012-04-19 13:43:52 +0200 | [diff] [blame] | 530 | if (sk->sk_type != SOCK_SEQPACKET) { |
| 531 | err = -EINVAL; |
| 532 | goto done; |
| 533 | } |
| 534 | |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 535 | write_lock(&sco_sk_list.lock); |
| 536 | |
| 537 | if (__sco_get_sock_listen_by_addr(src)) { |
| 538 | err = -EADDRINUSE; |
| 539 | goto unlock; |
| 540 | } |
| 541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | sk->sk_max_ack_backlog = backlog; |
| 543 | sk->sk_ack_backlog = 0; |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | sk->sk_state = BT_LISTEN; |
| 546 | |
Marcel Holtmann | fb33405 | 2012-04-19 14:37:58 +0200 | [diff] [blame] | 547 | unlock: |
| 548 | write_unlock(&sco_sk_list.lock); |
| 549 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | done: |
| 551 | release_sock(sk); |
| 552 | return err; |
| 553 | } |
| 554 | |
| 555 | static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags) |
| 556 | { |
| 557 | DECLARE_WAITQUEUE(wait, current); |
| 558 | struct sock *sk = sock->sk, *ch; |
| 559 | long timeo; |
| 560 | int err = 0; |
| 561 | |
| 562 | lock_sock(sk); |
| 563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 565 | |
| 566 | BT_DBG("sk %p timeo %ld", sk, timeo); |
| 567 | |
| 568 | /* Wait for an incoming connection. (wake-one). */ |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 569 | add_wait_queue_exclusive(sk_sleep(sk), &wait); |
Peter Hurley | 552b0d3 | 2011-07-24 00:11:01 -0400 | [diff] [blame] | 570 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | set_current_state(TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
| 573 | if (sk->sk_state != BT_LISTEN) { |
| 574 | err = -EBADFD; |
| 575 | break; |
| 576 | } |
| 577 | |
Peter Hurley | 552b0d3 | 2011-07-24 00:11:01 -0400 | [diff] [blame] | 578 | ch = bt_accept_dequeue(sk, newsock); |
| 579 | if (ch) |
| 580 | break; |
| 581 | |
| 582 | if (!timeo) { |
| 583 | err = -EAGAIN; |
| 584 | break; |
| 585 | } |
| 586 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | if (signal_pending(current)) { |
| 588 | err = sock_intr_errno(timeo); |
| 589 | break; |
| 590 | } |
Peter Hurley | 552b0d3 | 2011-07-24 00:11:01 -0400 | [diff] [blame] | 591 | |
| 592 | release_sock(sk); |
| 593 | timeo = schedule_timeout(timeo); |
| 594 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
Peter Hurley | 552b0d3 | 2011-07-24 00:11:01 -0400 | [diff] [blame] | 596 | __set_current_state(TASK_RUNNING); |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 597 | remove_wait_queue(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | |
| 599 | if (err) |
| 600 | goto done; |
| 601 | |
| 602 | newsock->state = SS_CONNECTED; |
| 603 | |
| 604 | BT_DBG("new socket %p", ch); |
| 605 | |
| 606 | done: |
| 607 | release_sock(sk); |
| 608 | return err; |
| 609 | } |
| 610 | |
| 611 | static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer) |
| 612 | { |
| 613 | struct sockaddr_sco *sa = (struct sockaddr_sco *) addr; |
| 614 | struct sock *sk = sock->sk; |
| 615 | |
| 616 | BT_DBG("sock %p, sk %p", sock, sk); |
| 617 | |
| 618 | addr->sa_family = AF_BLUETOOTH; |
| 619 | *len = sizeof(struct sockaddr_sco); |
| 620 | |
| 621 | if (peer) |
| 622 | bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst); |
| 623 | else |
| 624 | bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src); |
| 625 | |
| 626 | return 0; |
| 627 | } |
| 628 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 629 | static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | struct msghdr *msg, size_t len) |
| 631 | { |
| 632 | struct sock *sk = sock->sk; |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 633 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
| 635 | BT_DBG("sock %p, sk %p", sock, sk); |
| 636 | |
Benjamin LaHaise | c1cbe4b | 2005-12-13 23:22:19 -0800 | [diff] [blame] | 637 | err = sock_error(sk); |
| 638 | if (err) |
| 639 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | |
| 641 | if (msg->msg_flags & MSG_OOB) |
| 642 | return -EOPNOTSUPP; |
| 643 | |
| 644 | lock_sock(sk); |
| 645 | |
| 646 | if (sk->sk_state == BT_CONNECTED) |
| 647 | err = sco_send_frame(sk, msg, len); |
| 648 | else |
| 649 | err = -ENOTCONN; |
| 650 | |
| 651 | release_sock(sk); |
| 652 | return err; |
| 653 | } |
| 654 | |
Frédéric Dalleau | fa5513b | 2013-04-16 17:28:58 +0200 | [diff] [blame] | 655 | static void sco_conn_defer_accept(struct hci_conn *conn, int mask) |
| 656 | { |
| 657 | struct hci_dev *hdev = conn->hdev; |
| 658 | |
| 659 | BT_DBG("conn %p", conn); |
| 660 | |
| 661 | conn->state = BT_CONFIG; |
| 662 | |
| 663 | if (!lmp_esco_capable(hdev)) { |
| 664 | struct hci_cp_accept_conn_req cp; |
| 665 | |
| 666 | bacpy(&cp.bdaddr, &conn->dst); |
| 667 | |
| 668 | if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER)) |
| 669 | cp.role = 0x00; /* Become master */ |
| 670 | else |
| 671 | cp.role = 0x01; /* Remain slave */ |
| 672 | |
| 673 | hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp); |
| 674 | } else { |
| 675 | struct hci_cp_accept_sync_conn_req cp; |
| 676 | |
| 677 | bacpy(&cp.bdaddr, &conn->dst); |
| 678 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
| 679 | |
| 680 | cp.tx_bandwidth = __constant_cpu_to_le32(0x00001f40); |
| 681 | cp.rx_bandwidth = __constant_cpu_to_le32(0x00001f40); |
| 682 | cp.max_latency = __constant_cpu_to_le16(0xffff); |
| 683 | cp.content_format = cpu_to_le16(hdev->voice_setting); |
| 684 | cp.retrans_effort = 0xff; |
| 685 | |
| 686 | hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ, |
| 687 | sizeof(cp), &cp); |
| 688 | } |
| 689 | } |
| 690 | |
Frédéric Dalleau | 20714bf | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 691 | static int sco_sock_recvmsg(struct kiocb *iocb, struct socket *sock, |
| 692 | struct msghdr *msg, size_t len, int flags) |
| 693 | { |
| 694 | struct sock *sk = sock->sk; |
| 695 | struct sco_pinfo *pi = sco_pi(sk); |
| 696 | |
| 697 | lock_sock(sk); |
| 698 | |
| 699 | if (sk->sk_state == BT_CONNECT2 && |
| 700 | test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) { |
Frédéric Dalleau | fa5513b | 2013-04-16 17:28:58 +0200 | [diff] [blame] | 701 | sco_conn_defer_accept(pi->conn->hcon, 0); |
Frédéric Dalleau | 20714bf | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 702 | sk->sk_state = BT_CONFIG; |
Mathias Krause | c8c4991 | 2013-04-07 01:51:51 +0000 | [diff] [blame] | 703 | msg->msg_namelen = 0; |
Frédéric Dalleau | 20714bf | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 704 | |
| 705 | release_sock(sk); |
| 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | release_sock(sk); |
| 710 | |
| 711 | return bt_sock_recvmsg(iocb, sock, msg, len, flags); |
| 712 | } |
| 713 | |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 714 | static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | { |
| 716 | struct sock *sk = sock->sk; |
| 717 | int err = 0; |
Frédéric Dalleau | b96e9c6 | 2012-11-21 10:51:11 +0100 | [diff] [blame] | 718 | u32 opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | |
| 720 | BT_DBG("sk %p", sk); |
| 721 | |
| 722 | lock_sock(sk); |
| 723 | |
| 724 | switch (optname) { |
Frédéric Dalleau | b96e9c6 | 2012-11-21 10:51:11 +0100 | [diff] [blame] | 725 | |
| 726 | case BT_DEFER_SETUP: |
| 727 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { |
| 728 | err = -EINVAL; |
| 729 | break; |
| 730 | } |
| 731 | |
| 732 | if (get_user(opt, (u32 __user *) optval)) { |
| 733 | err = -EFAULT; |
| 734 | break; |
| 735 | } |
| 736 | |
| 737 | if (opt) |
| 738 | set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags); |
| 739 | else |
| 740 | clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags); |
| 741 | break; |
| 742 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | default: |
| 744 | err = -ENOPROTOOPT; |
| 745 | break; |
| 746 | } |
| 747 | |
| 748 | release_sock(sk); |
| 749 | return err; |
| 750 | } |
| 751 | |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 752 | static int sco_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | { |
| 754 | struct sock *sk = sock->sk; |
| 755 | struct sco_options opts; |
| 756 | struct sco_conninfo cinfo; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 757 | int len, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | |
| 759 | BT_DBG("sk %p", sk); |
| 760 | |
| 761 | if (get_user(len, optlen)) |
| 762 | return -EFAULT; |
| 763 | |
| 764 | lock_sock(sk); |
| 765 | |
| 766 | switch (optname) { |
| 767 | case SCO_OPTIONS: |
Johan Hedberg | 9d225d2 | 2013-08-08 14:53:56 +0300 | [diff] [blame^] | 768 | if (sk->sk_state != BT_CONNECTED && |
| 769 | !(sk->sk_state == BT_CONNECT2 && |
| 770 | test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | err = -ENOTCONN; |
| 772 | break; |
| 773 | } |
| 774 | |
| 775 | opts.mtu = sco_pi(sk)->conn->mtu; |
| 776 | |
| 777 | BT_DBG("mtu %d", opts.mtu); |
| 778 | |
| 779 | len = min_t(unsigned int, len, sizeof(opts)); |
| 780 | if (copy_to_user(optval, (char *)&opts, len)) |
| 781 | err = -EFAULT; |
| 782 | |
| 783 | break; |
| 784 | |
| 785 | case SCO_CONNINFO: |
Johan Hedberg | 9d225d2 | 2013-08-08 14:53:56 +0300 | [diff] [blame^] | 786 | if (sk->sk_state != BT_CONNECTED && |
| 787 | !(sk->sk_state == BT_CONNECT2 && |
| 788 | test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | err = -ENOTCONN; |
| 790 | break; |
| 791 | } |
| 792 | |
Vasiliy Kulikov | c4c896e | 2011-02-14 13:54:26 +0300 | [diff] [blame] | 793 | memset(&cinfo, 0, sizeof(cinfo)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle; |
| 795 | memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3); |
| 796 | |
| 797 | len = min_t(unsigned int, len, sizeof(cinfo)); |
| 798 | if (copy_to_user(optval, (char *)&cinfo, len)) |
| 799 | err = -EFAULT; |
| 800 | |
| 801 | break; |
| 802 | |
| 803 | default: |
| 804 | err = -ENOPROTOOPT; |
| 805 | break; |
| 806 | } |
| 807 | |
| 808 | release_sock(sk); |
| 809 | return err; |
| 810 | } |
| 811 | |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 812 | static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen) |
| 813 | { |
| 814 | struct sock *sk = sock->sk; |
| 815 | int len, err = 0; |
| 816 | |
| 817 | BT_DBG("sk %p", sk); |
| 818 | |
| 819 | if (level == SOL_SCO) |
| 820 | return sco_sock_getsockopt_old(sock, optname, optval, optlen); |
| 821 | |
| 822 | if (get_user(len, optlen)) |
| 823 | return -EFAULT; |
| 824 | |
| 825 | lock_sock(sk); |
| 826 | |
| 827 | switch (optname) { |
Frédéric Dalleau | b96e9c6 | 2012-11-21 10:51:11 +0100 | [diff] [blame] | 828 | |
| 829 | case BT_DEFER_SETUP: |
| 830 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) { |
| 831 | err = -EINVAL; |
| 832 | break; |
| 833 | } |
| 834 | |
| 835 | if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags), |
| 836 | (u32 __user *) optval)) |
| 837 | err = -EFAULT; |
| 838 | |
| 839 | break; |
| 840 | |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 841 | default: |
| 842 | err = -ENOPROTOOPT; |
| 843 | break; |
| 844 | } |
| 845 | |
| 846 | release_sock(sk); |
| 847 | return err; |
| 848 | } |
| 849 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 850 | static int sco_sock_shutdown(struct socket *sock, int how) |
| 851 | { |
| 852 | struct sock *sk = sock->sk; |
| 853 | int err = 0; |
| 854 | |
| 855 | BT_DBG("sock %p, sk %p", sock, sk); |
| 856 | |
| 857 | if (!sk) |
| 858 | return 0; |
| 859 | |
| 860 | lock_sock(sk); |
| 861 | if (!sk->sk_shutdown) { |
| 862 | sk->sk_shutdown = SHUTDOWN_MASK; |
| 863 | sco_sock_clear_timer(sk); |
| 864 | __sco_sock_close(sk); |
| 865 | |
| 866 | if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) |
| 867 | err = bt_sock_wait_state(sk, BT_CLOSED, |
Gustavo Padovan | be7c2b9 | 2012-05-17 00:36:21 -0300 | [diff] [blame] | 868 | sk->sk_lingertime); |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 869 | } |
| 870 | release_sock(sk); |
| 871 | return err; |
| 872 | } |
| 873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | static int sco_sock_release(struct socket *sock) |
| 875 | { |
| 876 | struct sock *sk = sock->sk; |
| 877 | int err = 0; |
| 878 | |
| 879 | BT_DBG("sock %p, sk %p", sock, sk); |
| 880 | |
| 881 | if (!sk) |
| 882 | return 0; |
| 883 | |
| 884 | sco_sock_close(sk); |
| 885 | |
| 886 | if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) { |
| 887 | lock_sock(sk); |
| 888 | err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime); |
| 889 | release_sock(sk); |
| 890 | } |
| 891 | |
| 892 | sock_orphan(sk); |
| 893 | sco_sock_kill(sk); |
| 894 | return err; |
| 895 | } |
| 896 | |
| 897 | static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent) |
| 898 | { |
| 899 | BT_DBG("conn %p", conn); |
| 900 | |
| 901 | sco_pi(sk)->conn = conn; |
| 902 | conn->sk = sk; |
| 903 | |
| 904 | if (parent) |
| 905 | bt_accept_enqueue(parent, sk); |
| 906 | } |
| 907 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 908 | /* Delete channel. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | * Must be called on the locked socket. */ |
| 910 | static void sco_chan_del(struct sock *sk, int err) |
| 911 | { |
| 912 | struct sco_conn *conn; |
| 913 | |
| 914 | conn = sco_pi(sk)->conn; |
| 915 | |
| 916 | BT_DBG("sk %p, conn %p, err %d", sk, conn, err); |
| 917 | |
Gustavo Padovan | 0b27a4b | 2012-12-03 15:36:51 -0200 | [diff] [blame] | 918 | if (conn) { |
| 919 | sco_conn_lock(conn); |
| 920 | conn->sk = NULL; |
| 921 | sco_pi(sk)->conn = NULL; |
| 922 | sco_conn_unlock(conn); |
| 923 | |
| 924 | if (conn->hcon) |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 925 | hci_conn_drop(conn->hcon); |
Gustavo Padovan | 0b27a4b | 2012-12-03 15:36:51 -0200 | [diff] [blame] | 926 | } |
| 927 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | sk->sk_state = BT_CLOSED; |
| 929 | sk->sk_err = err; |
| 930 | sk->sk_state_change(sk); |
| 931 | |
| 932 | sock_set_flag(sk, SOCK_ZAPPED); |
| 933 | } |
| 934 | |
| 935 | static void sco_conn_ready(struct sco_conn *conn) |
| 936 | { |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 937 | struct sock *parent; |
| 938 | struct sock *sk = conn->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | |
| 940 | BT_DBG("conn %p", conn); |
| 941 | |
Andrei Emeltchenko | 735cbc4 | 2010-12-01 16:58:22 +0200 | [diff] [blame] | 942 | if (sk) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | sco_sock_clear_timer(sk); |
| 944 | bh_lock_sock(sk); |
| 945 | sk->sk_state = BT_CONNECTED; |
| 946 | sk->sk_state_change(sk); |
| 947 | bh_unlock_sock(sk); |
| 948 | } else { |
Andre Guedes | 4052808 | 2013-01-29 19:59:56 -0300 | [diff] [blame] | 949 | sco_conn_lock(conn); |
| 950 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | parent = sco_get_sock_listen(conn->src); |
Andre Guedes | 4052808 | 2013-01-29 19:59:56 -0300 | [diff] [blame] | 952 | if (!parent) { |
| 953 | sco_conn_unlock(conn); |
| 954 | return; |
| 955 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | |
| 957 | bh_lock_sock(parent); |
| 958 | |
Gustavo F. Padovan | b9dbdbc | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 959 | sk = sco_sock_alloc(sock_net(parent), NULL, |
Gustavo Padovan | be7c2b9 | 2012-05-17 00:36:21 -0300 | [diff] [blame] | 960 | BTPROTO_SCO, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | if (!sk) { |
| 962 | bh_unlock_sock(parent); |
Andre Guedes | 4052808 | 2013-01-29 19:59:56 -0300 | [diff] [blame] | 963 | sco_conn_unlock(conn); |
| 964 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | sco_sock_init(sk, parent); |
| 968 | |
| 969 | bacpy(&bt_sk(sk)->src, conn->src); |
| 970 | bacpy(&bt_sk(sk)->dst, conn->dst); |
| 971 | |
| 972 | hci_conn_hold(conn->hcon); |
| 973 | __sco_chan_add(conn, sk, parent); |
| 974 | |
Frédéric Dalleau | 20714bf | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 975 | if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags)) |
| 976 | sk->sk_state = BT_CONNECT2; |
| 977 | else |
| 978 | sk->sk_state = BT_CONNECTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | |
| 980 | /* Wake up parent */ |
| 981 | parent->sk_data_ready(parent, 1); |
| 982 | |
| 983 | bh_unlock_sock(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | |
Andre Guedes | 4052808 | 2013-01-29 19:59:56 -0300 | [diff] [blame] | 985 | sco_conn_unlock(conn); |
| 986 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | /* ----- SCO interface with lower layer (HCI) ----- */ |
Frédéric Dalleau | 20714bf | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 990 | int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | { |
Gustavo Padovan | fc5fef6 | 2012-05-23 04:04:19 -0300 | [diff] [blame] | 992 | struct sock *sk; |
Marcel Holtmann | 71aeeaa | 2009-01-15 21:57:02 +0100 | [diff] [blame] | 993 | int lm = 0; |
| 994 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 995 | BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | |
Marcel Holtmann | 71aeeaa | 2009-01-15 21:57:02 +0100 | [diff] [blame] | 997 | /* Find listening sockets */ |
| 998 | read_lock(&sco_sk_list.lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 999 | sk_for_each(sk, &sco_sk_list.head) { |
Marcel Holtmann | 71aeeaa | 2009-01-15 21:57:02 +0100 | [diff] [blame] | 1000 | if (sk->sk_state != BT_LISTEN) |
| 1001 | continue; |
| 1002 | |
| 1003 | if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr) || |
Gustavo Padovan | be7c2b9 | 2012-05-17 00:36:21 -0300 | [diff] [blame] | 1004 | !bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { |
Marcel Holtmann | 71aeeaa | 2009-01-15 21:57:02 +0100 | [diff] [blame] | 1005 | lm |= HCI_LM_ACCEPT; |
Frédéric Dalleau | 20714bf | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 1006 | |
| 1007 | if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) |
| 1008 | *flags |= HCI_PROTO_DEFER; |
Marcel Holtmann | 71aeeaa | 2009-01-15 21:57:02 +0100 | [diff] [blame] | 1009 | break; |
| 1010 | } |
| 1011 | } |
| 1012 | read_unlock(&sco_sk_list.lock); |
| 1013 | |
| 1014 | return lm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
Andrei Emeltchenko | 9e66463 | 2012-07-24 16:06:15 +0300 | [diff] [blame] | 1017 | void sco_connect_cfm(struct hci_conn *hcon, __u8 status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | { |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1019 | BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | if (!status) { |
| 1021 | struct sco_conn *conn; |
| 1022 | |
Lukasz Rymanowski | 519e42b | 2012-04-19 16:12:28 +0200 | [diff] [blame] | 1023 | conn = sco_conn_add(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | if (conn) |
| 1025 | sco_conn_ready(conn); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1026 | } else |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 1027 | sco_conn_del(hcon, bt_to_errno(status)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
Andrei Emeltchenko | 9e66463 | 2012-07-24 16:06:15 +0300 | [diff] [blame] | 1030 | void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | { |
| 1032 | BT_DBG("hcon %p reason %d", hcon, reason); |
| 1033 | |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 1034 | sco_conn_del(hcon, bt_to_errno(reason)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 1037 | int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | { |
| 1039 | struct sco_conn *conn = hcon->sco_data; |
| 1040 | |
| 1041 | if (!conn) |
| 1042 | goto drop; |
| 1043 | |
| 1044 | BT_DBG("conn %p len %d", conn, skb->len); |
| 1045 | |
| 1046 | if (skb->len) { |
| 1047 | sco_recv_frame(conn, skb); |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
| 1051 | drop: |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1052 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | return 0; |
| 1054 | } |
| 1055 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1056 | static int sco_debugfs_show(struct seq_file *f, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | { |
| 1058 | struct sock *sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | |
Gustavo F. Padovan | ee65d19 | 2011-12-27 15:28:46 -0200 | [diff] [blame] | 1060 | read_lock(&sco_sk_list.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1062 | sk_for_each(sk, &sco_sk_list.head) { |
Andrei Emeltchenko | fcb7333 | 2012-09-25 12:49:44 +0300 | [diff] [blame] | 1063 | seq_printf(f, "%pMR %pMR %d\n", &bt_sk(sk)->src, |
| 1064 | &bt_sk(sk)->dst, sk->sk_state); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 1065 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
Gustavo F. Padovan | ee65d19 | 2011-12-27 15:28:46 -0200 | [diff] [blame] | 1067 | read_unlock(&sco_sk_list.lock); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 1068 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1069 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1072 | static int sco_debugfs_open(struct inode *inode, struct file *file) |
| 1073 | { |
| 1074 | return single_open(file, sco_debugfs_show, inode->i_private); |
| 1075 | } |
| 1076 | |
| 1077 | static const struct file_operations sco_debugfs_fops = { |
| 1078 | .open = sco_debugfs_open, |
| 1079 | .read = seq_read, |
| 1080 | .llseek = seq_lseek, |
| 1081 | .release = single_release, |
| 1082 | }; |
| 1083 | |
| 1084 | static struct dentry *sco_debugfs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 1086 | static const struct proto_ops sco_sock_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | .family = PF_BLUETOOTH, |
| 1088 | .owner = THIS_MODULE, |
| 1089 | .release = sco_sock_release, |
| 1090 | .bind = sco_sock_bind, |
| 1091 | .connect = sco_sock_connect, |
| 1092 | .listen = sco_sock_listen, |
| 1093 | .accept = sco_sock_accept, |
| 1094 | .getname = sco_sock_getname, |
| 1095 | .sendmsg = sco_sock_sendmsg, |
Frédéric Dalleau | 20714bf | 2012-11-21 10:51:12 +0100 | [diff] [blame] | 1096 | .recvmsg = sco_sock_recvmsg, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | .poll = bt_sock_poll, |
Marcel Holtmann | 3241ad8 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 1098 | .ioctl = bt_sock_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | .mmap = sock_no_mmap, |
| 1100 | .socketpair = sock_no_socketpair, |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame] | 1101 | .shutdown = sco_sock_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | .setsockopt = sco_sock_setsockopt, |
| 1103 | .getsockopt = sco_sock_getsockopt |
| 1104 | }; |
| 1105 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 1106 | static const struct net_proto_family sco_sock_family_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | .family = PF_BLUETOOTH, |
| 1108 | .owner = THIS_MODULE, |
| 1109 | .create = sco_sock_create, |
| 1110 | }; |
| 1111 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 1112 | int __init sco_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | { |
| 1114 | int err; |
| 1115 | |
| 1116 | err = proto_register(&sco_proto, 0); |
| 1117 | if (err < 0) |
| 1118 | return err; |
| 1119 | |
| 1120 | err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops); |
| 1121 | if (err < 0) { |
| 1122 | BT_ERR("SCO socket registration failed"); |
| 1123 | goto error; |
| 1124 | } |
| 1125 | |
Al Viro | b031661 | 2013-04-04 19:14:33 -0400 | [diff] [blame] | 1126 | err = bt_procfs_init(&init_net, "sco", &sco_sk_list, NULL); |
Masatake YAMATO | de9b921 | 2012-07-26 01:30:12 +0900 | [diff] [blame] | 1127 | if (err < 0) { |
| 1128 | BT_ERR("Failed to create SCO proc file"); |
| 1129 | bt_sock_unregister(BTPROTO_SCO); |
| 1130 | goto error; |
| 1131 | } |
| 1132 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1133 | if (bt_debugfs) { |
Gustavo Padovan | be7c2b9 | 2012-05-17 00:36:21 -0300 | [diff] [blame] | 1134 | sco_debugfs = debugfs_create_file("sco", 0444, bt_debugfs, |
| 1135 | NULL, &sco_debugfs_fops); |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1136 | if (!sco_debugfs) |
| 1137 | BT_ERR("Failed to create SCO debug file"); |
| 1138 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | BT_INFO("SCO socket layer initialized"); |
| 1141 | |
| 1142 | return 0; |
| 1143 | |
| 1144 | error: |
| 1145 | proto_unregister(&sco_proto); |
| 1146 | return err; |
| 1147 | } |
| 1148 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 1149 | void __exit sco_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | { |
Masatake YAMATO | de9b921 | 2012-07-26 01:30:12 +0900 | [diff] [blame] | 1151 | bt_procfs_cleanup(&init_net, "sco"); |
| 1152 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 1153 | debugfs_remove(sco_debugfs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | |
David Herrmann | 5e9d7f8 | 2013-02-24 19:36:51 +0100 | [diff] [blame] | 1155 | bt_sock_unregister(BTPROTO_SCO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | proto_unregister(&sco_proto); |
| 1158 | } |
| 1159 | |
Marcel Holtmann | 7cb127d | 2008-07-14 20:13:53 +0200 | [diff] [blame] | 1160 | module_param(disable_esco, bool, 0644); |
| 1161 | MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation"); |