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> |
| 28 | |
| 29 | #include <linux/types.h> |
| 30 | #include <linux/errno.h> |
| 31 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/sched.h> |
| 33 | #include <linux/slab.h> |
| 34 | #include <linux/poll.h> |
| 35 | #include <linux/fcntl.h> |
| 36 | #include <linux/init.h> |
| 37 | #include <linux/interrupt.h> |
| 38 | #include <linux/socket.h> |
| 39 | #include <linux/skbuff.h> |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 40 | #include <linux/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/list.h> |
| 42 | #include <net/sock.h> |
| 43 | |
| 44 | #include <asm/system.h> |
| 45 | #include <asm/uaccess.h> |
| 46 | |
| 47 | #include <net/bluetooth/bluetooth.h> |
| 48 | #include <net/bluetooth/hci_core.h> |
| 49 | #include <net/bluetooth/sco.h> |
| 50 | |
Marcel Holtmann | 7cb127d | 2008-07-14 20:13:53 +0200 | [diff] [blame] | 51 | #define VERSION "0.6" |
| 52 | |
| 53 | static int disable_esco = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 55 | static const struct proto_ops sco_sock_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | static struct bt_sock_list sco_sk_list = { |
Robert P. J. Day | d5fb296 | 2008-03-28 16:17:38 -0700 | [diff] [blame] | 58 | .lock = __RW_LOCK_UNLOCKED(sco_sk_list.lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent); |
| 62 | static void sco_chan_del(struct sock *sk, int err); |
| 63 | |
| 64 | static int sco_conn_del(struct hci_conn *conn, int err); |
| 65 | |
| 66 | static void sco_sock_close(struct sock *sk); |
| 67 | static void sco_sock_kill(struct sock *sk); |
| 68 | |
| 69 | /* ---- SCO timers ---- */ |
| 70 | static void sco_sock_timeout(unsigned long arg) |
| 71 | { |
| 72 | struct sock *sk = (struct sock *) arg; |
| 73 | |
| 74 | BT_DBG("sock %p state %d", sk, sk->sk_state); |
| 75 | |
| 76 | bh_lock_sock(sk); |
| 77 | sk->sk_err = ETIMEDOUT; |
| 78 | sk->sk_state_change(sk); |
| 79 | bh_unlock_sock(sk); |
| 80 | |
| 81 | sco_sock_kill(sk); |
| 82 | sock_put(sk); |
| 83 | } |
| 84 | |
| 85 | static void sco_sock_set_timer(struct sock *sk, long timeout) |
| 86 | { |
| 87 | BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout); |
| 88 | sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout); |
| 89 | } |
| 90 | |
| 91 | static void sco_sock_clear_timer(struct sock *sk) |
| 92 | { |
| 93 | BT_DBG("sock %p state %d", sk, sk->sk_state); |
| 94 | sk_stop_timer(sk, &sk->sk_timer); |
| 95 | } |
| 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | /* ---- SCO connections ---- */ |
| 98 | static struct sco_conn *sco_conn_add(struct hci_conn *hcon, __u8 status) |
| 99 | { |
| 100 | struct hci_dev *hdev = hcon->hdev; |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 101 | struct sco_conn *conn = hcon->sco_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 103 | if (conn || status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | return conn; |
| 105 | |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 106 | conn = kzalloc(sizeof(struct sco_conn), GFP_ATOMIC); |
| 107 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | spin_lock_init(&conn->lock); |
| 111 | |
| 112 | hcon->sco_data = conn; |
| 113 | conn->hcon = hcon; |
| 114 | |
| 115 | conn->src = &hdev->bdaddr; |
| 116 | conn->dst = &hcon->dst; |
| 117 | |
| 118 | if (hdev->sco_mtu > 0) |
| 119 | conn->mtu = hdev->sco_mtu; |
| 120 | else |
| 121 | conn->mtu = 60; |
| 122 | |
| 123 | BT_DBG("hcon %p conn %p", hcon, conn); |
Marcel Holtmann | 25ea6db | 2006-07-06 15:40:09 +0200 | [diff] [blame] | 124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | return conn; |
| 126 | } |
| 127 | |
| 128 | static inline struct sock *sco_chan_get(struct sco_conn *conn) |
| 129 | { |
| 130 | struct sock *sk = NULL; |
| 131 | sco_conn_lock(conn); |
| 132 | sk = conn->sk; |
| 133 | sco_conn_unlock(conn); |
| 134 | return sk; |
| 135 | } |
| 136 | |
| 137 | static int sco_conn_del(struct hci_conn *hcon, int err) |
| 138 | { |
| 139 | struct sco_conn *conn; |
| 140 | struct sock *sk; |
| 141 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 142 | if (!(conn = hcon->sco_data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | return 0; |
| 144 | |
| 145 | BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); |
| 146 | |
| 147 | /* Kill socket */ |
| 148 | if ((sk = sco_chan_get(conn))) { |
| 149 | bh_lock_sock(sk); |
| 150 | sco_sock_clear_timer(sk); |
| 151 | sco_chan_del(sk, err); |
| 152 | bh_unlock_sock(sk); |
| 153 | sco_sock_kill(sk); |
| 154 | } |
| 155 | |
| 156 | hcon->sco_data = NULL; |
| 157 | kfree(conn); |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static inline int sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent) |
| 162 | { |
| 163 | int err = 0; |
| 164 | |
| 165 | sco_conn_lock(conn); |
| 166 | if (conn->sk) { |
| 167 | err = -EBUSY; |
| 168 | } else { |
| 169 | __sco_chan_add(conn, sk, parent); |
| 170 | } |
| 171 | sco_conn_unlock(conn); |
| 172 | return err; |
| 173 | } |
| 174 | |
| 175 | static int sco_connect(struct sock *sk) |
| 176 | { |
| 177 | bdaddr_t *src = &bt_sk(sk)->src; |
| 178 | bdaddr_t *dst = &bt_sk(sk)->dst; |
| 179 | struct sco_conn *conn; |
| 180 | struct hci_conn *hcon; |
| 181 | struct hci_dev *hdev; |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 182 | int err, type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
| 184 | BT_DBG("%s -> %s", batostr(src), batostr(dst)); |
| 185 | |
| 186 | if (!(hdev = hci_get_route(dst, src))) |
| 187 | return -EHOSTUNREACH; |
| 188 | |
| 189 | hci_dev_lock_bh(hdev); |
| 190 | |
| 191 | err = -ENOMEM; |
| 192 | |
Marcel Holtmann | 7cb127d | 2008-07-14 20:13:53 +0200 | [diff] [blame] | 193 | if (lmp_esco_capable(hdev) && !disable_esco) |
| 194 | type = ESCO_LINK; |
| 195 | else |
| 196 | type = SCO_LINK; |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 197 | |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 198 | hcon = hci_connect(hdev, type, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | if (!hcon) |
| 200 | goto done; |
| 201 | |
| 202 | conn = sco_conn_add(hcon, 0); |
| 203 | if (!conn) { |
| 204 | hci_conn_put(hcon); |
| 205 | goto done; |
| 206 | } |
| 207 | |
| 208 | /* Update source addr of the socket */ |
| 209 | bacpy(src, conn->src); |
| 210 | |
| 211 | err = sco_chan_add(conn, sk, NULL); |
| 212 | if (err) |
| 213 | goto done; |
| 214 | |
| 215 | if (hcon->state == BT_CONNECTED) { |
| 216 | sco_sock_clear_timer(sk); |
| 217 | sk->sk_state = BT_CONNECTED; |
| 218 | } else { |
| 219 | sk->sk_state = BT_CONNECT; |
| 220 | sco_sock_set_timer(sk, sk->sk_sndtimeo); |
| 221 | } |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | done: |
| 224 | hci_dev_unlock_bh(hdev); |
| 225 | hci_dev_put(hdev); |
| 226 | return err; |
| 227 | } |
| 228 | |
| 229 | static inline int sco_send_frame(struct sock *sk, struct msghdr *msg, int len) |
| 230 | { |
| 231 | struct sco_conn *conn = sco_pi(sk)->conn; |
| 232 | struct sk_buff *skb; |
| 233 | int err, count; |
| 234 | |
| 235 | /* Check outgoing MTU */ |
| 236 | if (len > conn->mtu) |
| 237 | return -EINVAL; |
| 238 | |
| 239 | BT_DBG("sk %p len %d", sk, len); |
| 240 | |
| 241 | count = min_t(unsigned int, conn->mtu, len); |
| 242 | if (!(skb = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err))) |
| 243 | return err; |
| 244 | |
| 245 | if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) { |
| 246 | err = -EFAULT; |
| 247 | goto fail; |
| 248 | } |
| 249 | |
| 250 | if ((err = hci_send_sco(conn->hcon, skb)) < 0) |
Eric Sesterhenn | cdee575 | 2006-04-05 22:28:14 -0700 | [diff] [blame] | 251 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
| 253 | return count; |
| 254 | |
| 255 | fail: |
| 256 | kfree_skb(skb); |
| 257 | return err; |
| 258 | } |
| 259 | |
| 260 | static inline void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb) |
| 261 | { |
| 262 | struct sock *sk = sco_chan_get(conn); |
| 263 | |
| 264 | if (!sk) |
| 265 | goto drop; |
| 266 | |
| 267 | BT_DBG("sk %p len %d", sk, skb->len); |
| 268 | |
| 269 | if (sk->sk_state != BT_CONNECTED) |
| 270 | goto drop; |
| 271 | |
| 272 | if (!sock_queue_rcv_skb(sk, skb)) |
| 273 | return; |
| 274 | |
| 275 | drop: |
| 276 | kfree_skb(skb); |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | /* -------- Socket interface ---------- */ |
| 281 | static struct sock *__sco_get_sock_by_addr(bdaddr_t *ba) |
| 282 | { |
| 283 | struct sock *sk; |
| 284 | struct hlist_node *node; |
| 285 | |
| 286 | sk_for_each(sk, node, &sco_sk_list.head) |
| 287 | if (!bacmp(&bt_sk(sk)->src, ba)) |
| 288 | goto found; |
| 289 | sk = NULL; |
| 290 | found: |
| 291 | return sk; |
| 292 | } |
| 293 | |
| 294 | /* Find socket listening on source bdaddr. |
| 295 | * Returns closest match. |
| 296 | */ |
| 297 | static struct sock *sco_get_sock_listen(bdaddr_t *src) |
| 298 | { |
| 299 | struct sock *sk = NULL, *sk1 = NULL; |
| 300 | struct hlist_node *node; |
| 301 | |
| 302 | read_lock(&sco_sk_list.lock); |
| 303 | |
| 304 | sk_for_each(sk, node, &sco_sk_list.head) { |
| 305 | if (sk->sk_state != BT_LISTEN) |
| 306 | continue; |
| 307 | |
| 308 | /* Exact match. */ |
| 309 | if (!bacmp(&bt_sk(sk)->src, src)) |
| 310 | break; |
| 311 | |
| 312 | /* Closest match */ |
| 313 | if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) |
| 314 | sk1 = sk; |
| 315 | } |
| 316 | |
| 317 | read_unlock(&sco_sk_list.lock); |
| 318 | |
| 319 | return node ? sk : sk1; |
| 320 | } |
| 321 | |
| 322 | static void sco_sock_destruct(struct sock *sk) |
| 323 | { |
| 324 | BT_DBG("sk %p", sk); |
| 325 | |
| 326 | skb_queue_purge(&sk->sk_receive_queue); |
| 327 | skb_queue_purge(&sk->sk_write_queue); |
| 328 | } |
| 329 | |
| 330 | static void sco_sock_cleanup_listen(struct sock *parent) |
| 331 | { |
| 332 | struct sock *sk; |
| 333 | |
| 334 | BT_DBG("parent %p", parent); |
| 335 | |
| 336 | /* Close not yet accepted channels */ |
| 337 | while ((sk = bt_accept_dequeue(parent, NULL))) { |
| 338 | sco_sock_close(sk); |
| 339 | sco_sock_kill(sk); |
| 340 | } |
| 341 | |
| 342 | parent->sk_state = BT_CLOSED; |
| 343 | sock_set_flag(parent, SOCK_ZAPPED); |
| 344 | } |
| 345 | |
| 346 | /* Kill socket (only if zapped and orphan) |
| 347 | * Must be called on unlocked socket. |
| 348 | */ |
| 349 | static void sco_sock_kill(struct sock *sk) |
| 350 | { |
| 351 | if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket) |
| 352 | return; |
| 353 | |
| 354 | BT_DBG("sk %p state %d", sk, sk->sk_state); |
| 355 | |
| 356 | /* Kill poor orphan */ |
| 357 | bt_sock_unlink(&sco_sk_list, sk); |
| 358 | sock_set_flag(sk, SOCK_DEAD); |
| 359 | sock_put(sk); |
| 360 | } |
| 361 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame^] | 362 | static void __sco_sock_close(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame^] | 364 | 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] | 365 | |
| 366 | switch (sk->sk_state) { |
| 367 | case BT_LISTEN: |
| 368 | sco_sock_cleanup_listen(sk); |
| 369 | break; |
| 370 | |
| 371 | case BT_CONNECTED: |
| 372 | case BT_CONFIG: |
| 373 | case BT_CONNECT: |
| 374 | case BT_DISCONN: |
| 375 | sco_chan_del(sk, ECONNRESET); |
| 376 | break; |
| 377 | |
| 378 | default: |
| 379 | sock_set_flag(sk, SOCK_ZAPPED); |
| 380 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 381 | } |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame^] | 382 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame^] | 384 | /* Must be called on unlocked socket. */ |
| 385 | static void sco_sock_close(struct sock *sk) |
| 386 | { |
| 387 | sco_sock_clear_timer(sk); |
| 388 | lock_sock(sk); |
| 389 | __sco_sock_close(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | sco_sock_kill(sk); |
| 392 | } |
| 393 | |
| 394 | static void sco_sock_init(struct sock *sk, struct sock *parent) |
| 395 | { |
| 396 | BT_DBG("sk %p", sk); |
| 397 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 398 | if (parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | sk->sk_type = parent->sk_type; |
| 400 | } |
| 401 | |
| 402 | static struct proto sco_proto = { |
| 403 | .name = "SCO", |
| 404 | .owner = THIS_MODULE, |
| 405 | .obj_size = sizeof(struct sco_pinfo) |
| 406 | }; |
| 407 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 408 | 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] | 409 | { |
| 410 | struct sock *sk; |
| 411 | |
Pavel Emelyanov | 6257ff2 | 2007-11-01 00:39:31 -0700 | [diff] [blame] | 412 | sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | if (!sk) |
| 414 | return NULL; |
| 415 | |
| 416 | sock_init_data(sock, sk); |
| 417 | INIT_LIST_HEAD(&bt_sk(sk)->accept_q); |
| 418 | |
| 419 | sk->sk_destruct = sco_sock_destruct; |
| 420 | sk->sk_sndtimeo = SCO_CONN_TIMEOUT; |
| 421 | |
| 422 | sock_reset_flag(sk, SOCK_ZAPPED); |
| 423 | |
| 424 | sk->sk_protocol = proto; |
| 425 | sk->sk_state = BT_OPEN; |
| 426 | |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 427 | setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
| 429 | bt_sock_link(&sco_sk_list, sk); |
| 430 | return sk; |
| 431 | } |
| 432 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 433 | static int sco_sock_create(struct net *net, struct socket *sock, int protocol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | { |
| 435 | struct sock *sk; |
| 436 | |
| 437 | BT_DBG("sock %p", sock); |
| 438 | |
| 439 | sock->state = SS_UNCONNECTED; |
| 440 | |
| 441 | if (sock->type != SOCK_SEQPACKET) |
| 442 | return -ESOCKTNOSUPPORT; |
| 443 | |
| 444 | sock->ops = &sco_sock_ops; |
| 445 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 446 | sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC); |
Marcel Holtmann | 74da626 | 2006-10-15 17:31:14 +0200 | [diff] [blame] | 447 | if (!sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | return -ENOMEM; |
| 449 | |
| 450 | sco_sock_init(sk, NULL); |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len) |
| 455 | { |
| 456 | struct sockaddr_sco *sa = (struct sockaddr_sco *) addr; |
| 457 | struct sock *sk = sock->sk; |
| 458 | bdaddr_t *src = &sa->sco_bdaddr; |
| 459 | int err = 0; |
| 460 | |
| 461 | BT_DBG("sk %p %s", sk, batostr(&sa->sco_bdaddr)); |
| 462 | |
| 463 | if (!addr || addr->sa_family != AF_BLUETOOTH) |
| 464 | return -EINVAL; |
| 465 | |
| 466 | lock_sock(sk); |
| 467 | |
| 468 | if (sk->sk_state != BT_OPEN) { |
| 469 | err = -EBADFD; |
| 470 | goto done; |
| 471 | } |
| 472 | |
| 473 | write_lock_bh(&sco_sk_list.lock); |
| 474 | |
| 475 | if (bacmp(src, BDADDR_ANY) && __sco_get_sock_by_addr(src)) { |
| 476 | err = -EADDRINUSE; |
| 477 | } else { |
| 478 | /* Save source address */ |
| 479 | bacpy(&bt_sk(sk)->src, &sa->sco_bdaddr); |
| 480 | sk->sk_state = BT_BOUND; |
| 481 | } |
| 482 | |
| 483 | write_unlock_bh(&sco_sk_list.lock); |
| 484 | |
| 485 | done: |
| 486 | release_sock(sk); |
| 487 | return err; |
| 488 | } |
| 489 | |
| 490 | static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags) |
| 491 | { |
| 492 | struct sockaddr_sco *sa = (struct sockaddr_sco *) addr; |
| 493 | struct sock *sk = sock->sk; |
| 494 | int err = 0; |
| 495 | |
| 496 | |
| 497 | BT_DBG("sk %p", sk); |
| 498 | |
| 499 | if (addr->sa_family != AF_BLUETOOTH || alen < sizeof(struct sockaddr_sco)) |
| 500 | return -EINVAL; |
| 501 | |
| 502 | if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) |
| 503 | return -EBADFD; |
| 504 | |
| 505 | if (sk->sk_type != SOCK_SEQPACKET) |
| 506 | return -EINVAL; |
| 507 | |
| 508 | lock_sock(sk); |
| 509 | |
| 510 | /* Set destination address and psm */ |
| 511 | bacpy(&bt_sk(sk)->dst, &sa->sco_bdaddr); |
| 512 | |
| 513 | if ((err = sco_connect(sk))) |
| 514 | goto done; |
| 515 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 516 | err = bt_sock_wait_state(sk, BT_CONNECTED, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | sock_sndtimeo(sk, flags & O_NONBLOCK)); |
| 518 | |
| 519 | done: |
| 520 | release_sock(sk); |
| 521 | return err; |
| 522 | } |
| 523 | |
| 524 | static int sco_sock_listen(struct socket *sock, int backlog) |
| 525 | { |
| 526 | struct sock *sk = sock->sk; |
| 527 | int err = 0; |
| 528 | |
| 529 | BT_DBG("sk %p backlog %d", sk, backlog); |
| 530 | |
| 531 | lock_sock(sk); |
| 532 | |
| 533 | if (sk->sk_state != BT_BOUND || sock->type != SOCK_SEQPACKET) { |
| 534 | err = -EBADFD; |
| 535 | goto done; |
| 536 | } |
| 537 | |
| 538 | sk->sk_max_ack_backlog = backlog; |
| 539 | sk->sk_ack_backlog = 0; |
| 540 | sk->sk_state = BT_LISTEN; |
| 541 | |
| 542 | done: |
| 543 | release_sock(sk); |
| 544 | return err; |
| 545 | } |
| 546 | |
| 547 | static int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags) |
| 548 | { |
| 549 | DECLARE_WAITQUEUE(wait, current); |
| 550 | struct sock *sk = sock->sk, *ch; |
| 551 | long timeo; |
| 552 | int err = 0; |
| 553 | |
| 554 | lock_sock(sk); |
| 555 | |
| 556 | if (sk->sk_state != BT_LISTEN) { |
| 557 | err = -EBADFD; |
| 558 | goto done; |
| 559 | } |
| 560 | |
| 561 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 562 | |
| 563 | BT_DBG("sk %p timeo %ld", sk, timeo); |
| 564 | |
| 565 | /* Wait for an incoming connection. (wake-one). */ |
| 566 | add_wait_queue_exclusive(sk->sk_sleep, &wait); |
| 567 | while (!(ch = bt_accept_dequeue(sk, newsock))) { |
| 568 | set_current_state(TASK_INTERRUPTIBLE); |
| 569 | if (!timeo) { |
| 570 | err = -EAGAIN; |
| 571 | break; |
| 572 | } |
| 573 | |
| 574 | release_sock(sk); |
| 575 | timeo = schedule_timeout(timeo); |
| 576 | lock_sock(sk); |
| 577 | |
| 578 | if (sk->sk_state != BT_LISTEN) { |
| 579 | err = -EBADFD; |
| 580 | break; |
| 581 | } |
| 582 | |
| 583 | if (signal_pending(current)) { |
| 584 | err = sock_intr_errno(timeo); |
| 585 | break; |
| 586 | } |
| 587 | } |
| 588 | set_current_state(TASK_RUNNING); |
| 589 | remove_wait_queue(sk->sk_sleep, &wait); |
| 590 | |
| 591 | if (err) |
| 592 | goto done; |
| 593 | |
| 594 | newsock->state = SS_CONNECTED; |
| 595 | |
| 596 | BT_DBG("new socket %p", ch); |
| 597 | |
| 598 | done: |
| 599 | release_sock(sk); |
| 600 | return err; |
| 601 | } |
| 602 | |
| 603 | static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer) |
| 604 | { |
| 605 | struct sockaddr_sco *sa = (struct sockaddr_sco *) addr; |
| 606 | struct sock *sk = sock->sk; |
| 607 | |
| 608 | BT_DBG("sock %p, sk %p", sock, sk); |
| 609 | |
| 610 | addr->sa_family = AF_BLUETOOTH; |
| 611 | *len = sizeof(struct sockaddr_sco); |
| 612 | |
| 613 | if (peer) |
| 614 | bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst); |
| 615 | else |
| 616 | bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src); |
| 617 | |
| 618 | return 0; |
| 619 | } |
| 620 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 621 | static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | struct msghdr *msg, size_t len) |
| 623 | { |
| 624 | struct sock *sk = sock->sk; |
| 625 | int err = 0; |
| 626 | |
| 627 | BT_DBG("sock %p, sk %p", sock, sk); |
| 628 | |
Benjamin LaHaise | c1cbe4b | 2005-12-13 23:22:19 -0800 | [diff] [blame] | 629 | err = sock_error(sk); |
| 630 | if (err) |
| 631 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
| 633 | if (msg->msg_flags & MSG_OOB) |
| 634 | return -EOPNOTSUPP; |
| 635 | |
| 636 | lock_sock(sk); |
| 637 | |
| 638 | if (sk->sk_state == BT_CONNECTED) |
| 639 | err = sco_send_frame(sk, msg, len); |
| 640 | else |
| 641 | err = -ENOTCONN; |
| 642 | |
| 643 | release_sock(sk); |
| 644 | return err; |
| 645 | } |
| 646 | |
| 647 | static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen) |
| 648 | { |
| 649 | struct sock *sk = sock->sk; |
| 650 | int err = 0; |
| 651 | |
| 652 | BT_DBG("sk %p", sk); |
| 653 | |
| 654 | lock_sock(sk); |
| 655 | |
| 656 | switch (optname) { |
| 657 | default: |
| 658 | err = -ENOPROTOOPT; |
| 659 | break; |
| 660 | } |
| 661 | |
| 662 | release_sock(sk); |
| 663 | return err; |
| 664 | } |
| 665 | |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 666 | 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] | 667 | { |
| 668 | struct sock *sk = sock->sk; |
| 669 | struct sco_options opts; |
| 670 | struct sco_conninfo cinfo; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 671 | int len, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | |
| 673 | BT_DBG("sk %p", sk); |
| 674 | |
| 675 | if (get_user(len, optlen)) |
| 676 | return -EFAULT; |
| 677 | |
| 678 | lock_sock(sk); |
| 679 | |
| 680 | switch (optname) { |
| 681 | case SCO_OPTIONS: |
| 682 | if (sk->sk_state != BT_CONNECTED) { |
| 683 | err = -ENOTCONN; |
| 684 | break; |
| 685 | } |
| 686 | |
| 687 | opts.mtu = sco_pi(sk)->conn->mtu; |
| 688 | |
| 689 | BT_DBG("mtu %d", opts.mtu); |
| 690 | |
| 691 | len = min_t(unsigned int, len, sizeof(opts)); |
| 692 | if (copy_to_user(optval, (char *)&opts, len)) |
| 693 | err = -EFAULT; |
| 694 | |
| 695 | break; |
| 696 | |
| 697 | case SCO_CONNINFO: |
| 698 | if (sk->sk_state != BT_CONNECTED) { |
| 699 | err = -ENOTCONN; |
| 700 | break; |
| 701 | } |
| 702 | |
| 703 | cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle; |
| 704 | memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3); |
| 705 | |
| 706 | len = min_t(unsigned int, len, sizeof(cinfo)); |
| 707 | if (copy_to_user(optval, (char *)&cinfo, len)) |
| 708 | err = -EFAULT; |
| 709 | |
| 710 | break; |
| 711 | |
| 712 | default: |
| 713 | err = -ENOPROTOOPT; |
| 714 | break; |
| 715 | } |
| 716 | |
| 717 | release_sock(sk); |
| 718 | return err; |
| 719 | } |
| 720 | |
Marcel Holtmann | d58daf4 | 2009-01-15 21:52:14 +0100 | [diff] [blame] | 721 | static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen) |
| 722 | { |
| 723 | struct sock *sk = sock->sk; |
| 724 | int len, err = 0; |
| 725 | |
| 726 | BT_DBG("sk %p", sk); |
| 727 | |
| 728 | if (level == SOL_SCO) |
| 729 | return sco_sock_getsockopt_old(sock, optname, optval, optlen); |
| 730 | |
| 731 | if (get_user(len, optlen)) |
| 732 | return -EFAULT; |
| 733 | |
| 734 | lock_sock(sk); |
| 735 | |
| 736 | switch (optname) { |
| 737 | default: |
| 738 | err = -ENOPROTOOPT; |
| 739 | break; |
| 740 | } |
| 741 | |
| 742 | release_sock(sk); |
| 743 | return err; |
| 744 | } |
| 745 | |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame^] | 746 | static int sco_sock_shutdown(struct socket *sock, int how) |
| 747 | { |
| 748 | struct sock *sk = sock->sk; |
| 749 | int err = 0; |
| 750 | |
| 751 | BT_DBG("sock %p, sk %p", sock, sk); |
| 752 | |
| 753 | if (!sk) |
| 754 | return 0; |
| 755 | |
| 756 | lock_sock(sk); |
| 757 | if (!sk->sk_shutdown) { |
| 758 | sk->sk_shutdown = SHUTDOWN_MASK; |
| 759 | sco_sock_clear_timer(sk); |
| 760 | __sco_sock_close(sk); |
| 761 | |
| 762 | if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) |
| 763 | err = bt_sock_wait_state(sk, BT_CLOSED, |
| 764 | sk->sk_lingertime); |
| 765 | } |
| 766 | release_sock(sk); |
| 767 | return err; |
| 768 | } |
| 769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | static int sco_sock_release(struct socket *sock) |
| 771 | { |
| 772 | struct sock *sk = sock->sk; |
| 773 | int err = 0; |
| 774 | |
| 775 | BT_DBG("sock %p, sk %p", sock, sk); |
| 776 | |
| 777 | if (!sk) |
| 778 | return 0; |
| 779 | |
| 780 | sco_sock_close(sk); |
| 781 | |
| 782 | if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime) { |
| 783 | lock_sock(sk); |
| 784 | err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime); |
| 785 | release_sock(sk); |
| 786 | } |
| 787 | |
| 788 | sock_orphan(sk); |
| 789 | sco_sock_kill(sk); |
| 790 | return err; |
| 791 | } |
| 792 | |
| 793 | static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent) |
| 794 | { |
| 795 | BT_DBG("conn %p", conn); |
| 796 | |
| 797 | sco_pi(sk)->conn = conn; |
| 798 | conn->sk = sk; |
| 799 | |
| 800 | if (parent) |
| 801 | bt_accept_enqueue(parent, sk); |
| 802 | } |
| 803 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 804 | /* Delete channel. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | * Must be called on the locked socket. */ |
| 806 | static void sco_chan_del(struct sock *sk, int err) |
| 807 | { |
| 808 | struct sco_conn *conn; |
| 809 | |
| 810 | conn = sco_pi(sk)->conn; |
| 811 | |
| 812 | BT_DBG("sk %p, conn %p, err %d", sk, conn, err); |
| 813 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 814 | if (conn) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | sco_conn_lock(conn); |
| 816 | conn->sk = NULL; |
| 817 | sco_pi(sk)->conn = NULL; |
| 818 | sco_conn_unlock(conn); |
| 819 | hci_conn_put(conn->hcon); |
| 820 | } |
| 821 | |
| 822 | sk->sk_state = BT_CLOSED; |
| 823 | sk->sk_err = err; |
| 824 | sk->sk_state_change(sk); |
| 825 | |
| 826 | sock_set_flag(sk, SOCK_ZAPPED); |
| 827 | } |
| 828 | |
| 829 | static void sco_conn_ready(struct sco_conn *conn) |
| 830 | { |
| 831 | struct sock *parent, *sk; |
| 832 | |
| 833 | BT_DBG("conn %p", conn); |
| 834 | |
| 835 | sco_conn_lock(conn); |
| 836 | |
| 837 | if ((sk = conn->sk)) { |
| 838 | sco_sock_clear_timer(sk); |
| 839 | bh_lock_sock(sk); |
| 840 | sk->sk_state = BT_CONNECTED; |
| 841 | sk->sk_state_change(sk); |
| 842 | bh_unlock_sock(sk); |
| 843 | } else { |
| 844 | parent = sco_get_sock_listen(conn->src); |
| 845 | if (!parent) |
| 846 | goto done; |
| 847 | |
| 848 | bh_lock_sock(parent); |
| 849 | |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 850 | sk = sco_sock_alloc(sock_net(parent), NULL, BTPROTO_SCO, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | if (!sk) { |
| 852 | bh_unlock_sock(parent); |
| 853 | goto done; |
| 854 | } |
| 855 | |
| 856 | sco_sock_init(sk, parent); |
| 857 | |
| 858 | bacpy(&bt_sk(sk)->src, conn->src); |
| 859 | bacpy(&bt_sk(sk)->dst, conn->dst); |
| 860 | |
| 861 | hci_conn_hold(conn->hcon); |
| 862 | __sco_chan_add(conn, sk, parent); |
| 863 | |
| 864 | sk->sk_state = BT_CONNECTED; |
| 865 | |
| 866 | /* Wake up parent */ |
| 867 | parent->sk_data_ready(parent, 1); |
| 868 | |
| 869 | bh_unlock_sock(parent); |
| 870 | } |
| 871 | |
| 872 | done: |
| 873 | sco_conn_unlock(conn); |
| 874 | } |
| 875 | |
| 876 | /* ----- SCO interface with lower layer (HCI) ----- */ |
| 877 | static int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type) |
| 878 | { |
Marcel Holtmann | 71aeeaa | 2009-01-15 21:57:02 +0100 | [diff] [blame] | 879 | register struct sock *sk; |
| 880 | struct hlist_node *node; |
| 881 | int lm = 0; |
| 882 | |
| 883 | if (type != SCO_LINK && type != ESCO_LINK) |
| 884 | return 0; |
| 885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr)); |
| 887 | |
Marcel Holtmann | 71aeeaa | 2009-01-15 21:57:02 +0100 | [diff] [blame] | 888 | /* Find listening sockets */ |
| 889 | read_lock(&sco_sk_list.lock); |
| 890 | sk_for_each(sk, node, &sco_sk_list.head) { |
| 891 | if (sk->sk_state != BT_LISTEN) |
| 892 | continue; |
| 893 | |
| 894 | if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr) || |
| 895 | !bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { |
| 896 | lm |= HCI_LM_ACCEPT; |
| 897 | break; |
| 898 | } |
| 899 | } |
| 900 | read_unlock(&sco_sk_list.lock); |
| 901 | |
| 902 | return lm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | static int sco_connect_cfm(struct hci_conn *hcon, __u8 status) |
| 906 | { |
| 907 | BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status); |
| 908 | |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 909 | if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | return 0; |
| 911 | |
| 912 | if (!status) { |
| 913 | struct sco_conn *conn; |
| 914 | |
| 915 | conn = sco_conn_add(hcon, status); |
| 916 | if (conn) |
| 917 | sco_conn_ready(conn); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 918 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | sco_conn_del(hcon, bt_err(status)); |
| 920 | |
| 921 | return 0; |
| 922 | } |
| 923 | |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 924 | static int sco_disconn_cfm(struct hci_conn *hcon, __u8 reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | { |
| 926 | BT_DBG("hcon %p reason %d", hcon, reason); |
| 927 | |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 928 | if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | return 0; |
| 930 | |
| 931 | sco_conn_del(hcon, bt_err(reason)); |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 932 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | return 0; |
| 934 | } |
| 935 | |
| 936 | static int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb) |
| 937 | { |
| 938 | struct sco_conn *conn = hcon->sco_data; |
| 939 | |
| 940 | if (!conn) |
| 941 | goto drop; |
| 942 | |
| 943 | BT_DBG("conn %p len %d", conn, skb->len); |
| 944 | |
| 945 | if (skb->len) { |
| 946 | sco_recv_frame(conn, skb); |
| 947 | return 0; |
| 948 | } |
| 949 | |
| 950 | drop: |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 951 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | return 0; |
| 953 | } |
| 954 | |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 955 | static ssize_t sco_sysfs_show(struct class *dev, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | { |
| 957 | struct sock *sk; |
| 958 | struct hlist_node *node; |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 959 | char *str = buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | |
| 961 | read_lock_bh(&sco_sk_list.lock); |
| 962 | |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 963 | sk_for_each(sk, node, &sco_sk_list.head) { |
| 964 | str += sprintf(str, "%s %s %d\n", |
| 965 | batostr(&bt_sk(sk)->src), batostr(&bt_sk(sk)->dst), |
| 966 | sk->sk_state); |
| 967 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | read_unlock_bh(&sco_sk_list.lock); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 970 | |
| 971 | return (str - buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 974 | static CLASS_ATTR(sco, S_IRUGO, sco_sysfs_show, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 976 | static const struct proto_ops sco_sock_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | .family = PF_BLUETOOTH, |
| 978 | .owner = THIS_MODULE, |
| 979 | .release = sco_sock_release, |
| 980 | .bind = sco_sock_bind, |
| 981 | .connect = sco_sock_connect, |
| 982 | .listen = sco_sock_listen, |
| 983 | .accept = sco_sock_accept, |
| 984 | .getname = sco_sock_getname, |
| 985 | .sendmsg = sco_sock_sendmsg, |
| 986 | .recvmsg = bt_sock_recvmsg, |
| 987 | .poll = bt_sock_poll, |
Marcel Holtmann | 3241ad8 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 988 | .ioctl = bt_sock_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | .mmap = sock_no_mmap, |
| 990 | .socketpair = sock_no_socketpair, |
Marcel Holtmann | fd0b3ff | 2009-06-16 00:01:49 +0200 | [diff] [blame^] | 991 | .shutdown = sco_sock_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | .setsockopt = sco_sock_setsockopt, |
| 993 | .getsockopt = sco_sock_getsockopt |
| 994 | }; |
| 995 | |
| 996 | static struct net_proto_family sco_sock_family_ops = { |
| 997 | .family = PF_BLUETOOTH, |
| 998 | .owner = THIS_MODULE, |
| 999 | .create = sco_sock_create, |
| 1000 | }; |
| 1001 | |
| 1002 | static struct hci_proto sco_hci_proto = { |
| 1003 | .name = "SCO", |
| 1004 | .id = HCI_PROTO_SCO, |
| 1005 | .connect_ind = sco_connect_ind, |
| 1006 | .connect_cfm = sco_connect_cfm, |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 1007 | .disconn_cfm = sco_disconn_cfm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | .recv_scodata = sco_recv_scodata |
| 1009 | }; |
| 1010 | |
| 1011 | static int __init sco_init(void) |
| 1012 | { |
| 1013 | int err; |
| 1014 | |
| 1015 | err = proto_register(&sco_proto, 0); |
| 1016 | if (err < 0) |
| 1017 | return err; |
| 1018 | |
| 1019 | err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops); |
| 1020 | if (err < 0) { |
| 1021 | BT_ERR("SCO socket registration failed"); |
| 1022 | goto error; |
| 1023 | } |
| 1024 | |
| 1025 | err = hci_register_proto(&sco_hci_proto); |
| 1026 | if (err < 0) { |
| 1027 | BT_ERR("SCO protocol registration failed"); |
| 1028 | bt_sock_unregister(BTPROTO_SCO); |
| 1029 | goto error; |
| 1030 | } |
| 1031 | |
Marcel Holtmann | df5c37e | 2006-10-15 17:30:45 +0200 | [diff] [blame] | 1032 | if (class_create_file(bt_class, &class_attr_sco) < 0) |
| 1033 | BT_ERR("Failed to create SCO info file"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | |
| 1035 | BT_INFO("SCO (Voice Link) ver %s", VERSION); |
| 1036 | BT_INFO("SCO socket layer initialized"); |
| 1037 | |
| 1038 | return 0; |
| 1039 | |
| 1040 | error: |
| 1041 | proto_unregister(&sco_proto); |
| 1042 | return err; |
| 1043 | } |
| 1044 | |
| 1045 | static void __exit sco_exit(void) |
| 1046 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 1047 | class_remove_file(bt_class, &class_attr_sco); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | |
| 1049 | if (bt_sock_unregister(BTPROTO_SCO) < 0) |
| 1050 | BT_ERR("SCO socket unregistration failed"); |
| 1051 | |
| 1052 | if (hci_unregister_proto(&sco_hci_proto) < 0) |
| 1053 | BT_ERR("SCO protocol unregistration failed"); |
| 1054 | |
| 1055 | proto_unregister(&sco_proto); |
| 1056 | } |
| 1057 | |
| 1058 | module_init(sco_init); |
| 1059 | module_exit(sco_exit); |
| 1060 | |
Marcel Holtmann | 7cb127d | 2008-07-14 20:13:53 +0200 | [diff] [blame] | 1061 | module_param(disable_esco, bool, 0644); |
| 1062 | MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation"); |
| 1063 | |
Marcel Holtmann | 63fbd24 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 1064 | MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | MODULE_DESCRIPTION("Bluetooth SCO ver " VERSION); |
| 1066 | MODULE_VERSION(VERSION); |
| 1067 | MODULE_LICENSE("GPL"); |
| 1068 | MODULE_ALIAS("bt-proto-2"); |