Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-2.6-block.git] / net / bluetooth / rfcomm / tty.c
CommitLineData
8e87d142 1/*
1da177e4
LT
2 RFCOMM implementation for Linux Bluetooth stack (BlueZ).
3 Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
4 Copyright (C) 2002 Marcel Holtmann <marcel@holtmann.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation;
9
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
11 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
13 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
8e87d142
YH
14 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
15 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1da177e4
LT
17 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
8e87d142
YH
19 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
20 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
1da177e4
LT
21 SOFTWARE IS DISCLAIMED.
22*/
23
24/*
25 * RFCOMM TTY.
26 *
27 * $Id: tty.c,v 1.24 2002/10/03 01:54:38 holtmann Exp $
28 */
29
1da177e4
LT
30#include <linux/module.h>
31
32#include <linux/tty.h>
33#include <linux/tty_driver.h>
34#include <linux/tty_flip.h>
35
4fc268d2 36#include <linux/capability.h>
1da177e4
LT
37#include <linux/slab.h>
38#include <linux/skbuff.h>
39
40#include <net/bluetooth/bluetooth.h>
0a85b964 41#include <net/bluetooth/hci_core.h>
1da177e4
LT
42#include <net/bluetooth/rfcomm.h>
43
44#ifndef CONFIG_BT_RFCOMM_DEBUG
45#undef BT_DBG
46#define BT_DBG(D...)
47#endif
48
49#define RFCOMM_TTY_MAGIC 0x6d02 /* magic number for rfcomm struct */
50#define RFCOMM_TTY_PORTS RFCOMM_MAX_DEV /* whole lotta rfcomm devices */
51#define RFCOMM_TTY_MAJOR 216 /* device node major id of the usb/bluetooth.c driver */
52#define RFCOMM_TTY_MINOR 0
53
54static struct tty_driver *rfcomm_tty_driver;
55
56struct rfcomm_dev {
57 struct list_head list;
58 atomic_t refcnt;
59
60 char name[12];
61 int id;
62 unsigned long flags;
63 int opened;
64 int err;
65
66 bdaddr_t src;
67 bdaddr_t dst;
68 u8 channel;
69
70 uint modem_status;
71
72 struct rfcomm_dlc *dlc;
73 struct tty_struct *tty;
74 wait_queue_head_t wait;
75 struct tasklet_struct wakeup_task;
76
c1a33136
MH
77 struct device *tty_dev;
78
1da177e4
LT
79 atomic_t wmem_alloc;
80};
81
82static LIST_HEAD(rfcomm_dev_list);
83static DEFINE_RWLOCK(rfcomm_dev_lock);
84
85static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb);
86static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err);
87static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig);
88
89static void rfcomm_tty_wakeup(unsigned long arg);
90
91/* ---- Device functions ---- */
92static void rfcomm_dev_destruct(struct rfcomm_dev *dev)
93{
94 struct rfcomm_dlc *dlc = dev->dlc;
95
96 BT_DBG("dev %p dlc %p", dev, dlc);
97
f951375d
DY
98 /* Refcount should only hit zero when called from rfcomm_dev_del()
99 which will have taken us off the list. Everything else are
100 refcounting bugs. */
101 BUG_ON(!list_empty(&dev->list));
8de0a154 102
1da177e4
LT
103 rfcomm_dlc_lock(dlc);
104 /* Detach DLC if it's owned by this dev */
105 if (dlc->owner == dev)
106 dlc->owner = NULL;
107 rfcomm_dlc_unlock(dlc);
108
109 rfcomm_dlc_put(dlc);
110
111 tty_unregister_device(rfcomm_tty_driver, dev->id);
112
1da177e4
LT
113 kfree(dev);
114
8e87d142 115 /* It's safe to call module_put() here because socket still
1da177e4
LT
116 holds reference to this module. */
117 module_put(THIS_MODULE);
118}
119
120static inline void rfcomm_dev_hold(struct rfcomm_dev *dev)
121{
122 atomic_inc(&dev->refcnt);
123}
124
125static inline void rfcomm_dev_put(struct rfcomm_dev *dev)
126{
127 /* The reason this isn't actually a race, as you no
128 doubt have a little voice screaming at you in your
129 head, is that the refcount should never actually
130 reach zero unless the device has already been taken
131 off the list, in rfcomm_dev_del(). And if that's not
132 true, we'll hit the BUG() in rfcomm_dev_destruct()
133 anyway. */
134 if (atomic_dec_and_test(&dev->refcnt))
135 rfcomm_dev_destruct(dev);
136}
137
138static struct rfcomm_dev *__rfcomm_dev_get(int id)
139{
140 struct rfcomm_dev *dev;
141 struct list_head *p;
142
143 list_for_each(p, &rfcomm_dev_list) {
144 dev = list_entry(p, struct rfcomm_dev, list);
145 if (dev->id == id)
146 return dev;
147 }
148
149 return NULL;
150}
151
152static inline struct rfcomm_dev *rfcomm_dev_get(int id)
153{
154 struct rfcomm_dev *dev;
155
156 read_lock(&rfcomm_dev_lock);
157
158 dev = __rfcomm_dev_get(id);
8de0a154
VT
159
160 if (dev) {
161 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
162 dev = NULL;
163 else
164 rfcomm_dev_hold(dev);
165 }
1da177e4
LT
166
167 read_unlock(&rfcomm_dev_lock);
168
169 return dev;
170}
171
0a85b964
MH
172static struct device *rfcomm_get_device(struct rfcomm_dev *dev)
173{
174 struct hci_dev *hdev;
175 struct hci_conn *conn;
176
177 hdev = hci_get_route(&dev->dst, &dev->src);
178 if (!hdev)
179 return NULL;
180
181 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &dev->dst);
0a85b964
MH
182
183 hci_dev_put(hdev);
184
b2cfcd75 185 return conn ? &conn->dev : NULL;
0a85b964
MH
186}
187
dae6a0f6
MH
188static ssize_t show_address(struct device *tty_dev, struct device_attribute *attr, char *buf)
189{
190 struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
191 bdaddr_t bdaddr;
192 baswap(&bdaddr, &dev->dst);
193 return sprintf(buf, "%s\n", batostr(&bdaddr));
194}
195
196static ssize_t show_channel(struct device *tty_dev, struct device_attribute *attr, char *buf)
197{
198 struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
199 return sprintf(buf, "%d\n", dev->channel);
200}
201
202static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
203static DEVICE_ATTR(channel, S_IRUGO, show_channel, NULL);
204
1da177e4
LT
205static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
206{
207 struct rfcomm_dev *dev;
208 struct list_head *head = &rfcomm_dev_list, *p;
209 int err = 0;
210
211 BT_DBG("id %d channel %d", req->dev_id, req->channel);
8e87d142 212
25ea6db0 213 dev = kzalloc(sizeof(struct rfcomm_dev), GFP_KERNEL);
1da177e4
LT
214 if (!dev)
215 return -ENOMEM;
1da177e4
LT
216
217 write_lock_bh(&rfcomm_dev_lock);
218
219 if (req->dev_id < 0) {
220 dev->id = 0;
221
222 list_for_each(p, &rfcomm_dev_list) {
223 if (list_entry(p, struct rfcomm_dev, list)->id != dev->id)
224 break;
225
226 dev->id++;
227 head = p;
228 }
229 } else {
230 dev->id = req->dev_id;
231
232 list_for_each(p, &rfcomm_dev_list) {
233 struct rfcomm_dev *entry = list_entry(p, struct rfcomm_dev, list);
234
235 if (entry->id == dev->id) {
236 err = -EADDRINUSE;
237 goto out;
238 }
239
240 if (entry->id > dev->id - 1)
241 break;
242
243 head = p;
244 }
245 }
246
247 if ((dev->id < 0) || (dev->id > RFCOMM_MAX_DEV - 1)) {
248 err = -ENFILE;
249 goto out;
250 }
251
252 sprintf(dev->name, "rfcomm%d", dev->id);
253
254 list_add(&dev->list, head);
255 atomic_set(&dev->refcnt, 1);
256
257 bacpy(&dev->src, &req->src);
258 bacpy(&dev->dst, &req->dst);
259 dev->channel = req->channel;
260
8e87d142 261 dev->flags = req->flags &
1da177e4
LT
262 ((1 << RFCOMM_RELEASE_ONHUP) | (1 << RFCOMM_REUSE_DLC));
263
264 init_waitqueue_head(&dev->wait);
265 tasklet_init(&dev->wakeup_task, rfcomm_tty_wakeup, (unsigned long) dev);
266
267 rfcomm_dlc_lock(dlc);
268 dlc->data_ready = rfcomm_dev_data_ready;
269 dlc->state_change = rfcomm_dev_state_change;
270 dlc->modem_status = rfcomm_dev_modem_status;
271
272 dlc->owner = dev;
273 dev->dlc = dlc;
274 rfcomm_dlc_unlock(dlc);
275
8e87d142 276 /* It's safe to call __module_get() here because socket already
1da177e4
LT
277 holds reference to this module. */
278 __module_get(THIS_MODULE);
279
280out:
281 write_unlock_bh(&rfcomm_dev_lock);
282
09c7d829 283 if (err < 0) {
1da177e4
LT
284 kfree(dev);
285 return err;
286 }
287
c1a33136 288 dev->tty_dev = tty_register_device(rfcomm_tty_driver, dev->id, NULL);
1da177e4 289
8de0a154 290 if (IS_ERR(dev->tty_dev)) {
09c7d829 291 err = PTR_ERR(dev->tty_dev);
8de0a154
VT
292 list_del(&dev->list);
293 kfree(dev);
09c7d829 294 return err;
8de0a154
VT
295 }
296
dae6a0f6
MH
297 dev_set_drvdata(dev->tty_dev, dev);
298
299 if (device_create_file(dev->tty_dev, &dev_attr_address) < 0)
300 BT_ERR("Failed to create address attribute");
301
302 if (device_create_file(dev->tty_dev, &dev_attr_channel) < 0)
303 BT_ERR("Failed to create channel attribute");
304
1da177e4
LT
305 return dev->id;
306}
307
308static void rfcomm_dev_del(struct rfcomm_dev *dev)
309{
310 BT_DBG("dev %p", dev);
311
f951375d
DY
312 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
313 BUG_ON(1);
314 else
315 set_bit(RFCOMM_TTY_RELEASED, &dev->flags);
316
317 write_lock_bh(&rfcomm_dev_lock);
318 list_del_init(&dev->list);
319 write_unlock_bh(&rfcomm_dev_lock);
320
1da177e4
LT
321 rfcomm_dev_put(dev);
322}
323
324/* ---- Send buffer ---- */
325static inline unsigned int rfcomm_room(struct rfcomm_dlc *dlc)
326{
327 /* We can't let it be zero, because we don't get a callback
328 when tx_credits becomes nonzero, hence we'd never wake up */
329 return dlc->mtu * (dlc->tx_credits?:1);
330}
331
332static void rfcomm_wfree(struct sk_buff *skb)
333{
334 struct rfcomm_dev *dev = (void *) skb->sk;
335 atomic_sub(skb->truesize, &dev->wmem_alloc);
336 if (test_bit(RFCOMM_TTY_ATTACHED, &dev->flags))
337 tasklet_schedule(&dev->wakeup_task);
338 rfcomm_dev_put(dev);
339}
340
341static inline void rfcomm_set_owner_w(struct sk_buff *skb, struct rfcomm_dev *dev)
342{
343 rfcomm_dev_hold(dev);
344 atomic_add(skb->truesize, &dev->wmem_alloc);
345 skb->sk = (void *) dev;
346 skb->destructor = rfcomm_wfree;
347}
348
dd0fc66f 349static struct sk_buff *rfcomm_wmalloc(struct rfcomm_dev *dev, unsigned long size, gfp_t priority)
1da177e4
LT
350{
351 if (atomic_read(&dev->wmem_alloc) < rfcomm_room(dev->dlc)) {
352 struct sk_buff *skb = alloc_skb(size, priority);
353 if (skb) {
354 rfcomm_set_owner_w(skb, dev);
355 return skb;
356 }
357 }
358 return NULL;
359}
360
361/* ---- Device IOCTLs ---- */
362
363#define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP))
364
365static int rfcomm_create_dev(struct sock *sk, void __user *arg)
366{
367 struct rfcomm_dev_req req;
368 struct rfcomm_dlc *dlc;
369 int id;
370
371 if (copy_from_user(&req, arg, sizeof(req)))
372 return -EFAULT;
373
8de0a154 374 BT_DBG("sk %p dev_id %d flags 0x%x", sk, req.dev_id, req.flags);
1da177e4
LT
375
376 if (req.flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN))
377 return -EPERM;
378
379 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
380 /* Socket must be connected */
381 if (sk->sk_state != BT_CONNECTED)
382 return -EBADFD;
383
384 dlc = rfcomm_pi(sk)->dlc;
385 rfcomm_dlc_hold(dlc);
386 } else {
387 dlc = rfcomm_dlc_alloc(GFP_KERNEL);
388 if (!dlc)
389 return -ENOMEM;
390 }
391
392 id = rfcomm_dev_add(&req, dlc);
393 if (id < 0) {
394 rfcomm_dlc_put(dlc);
395 return id;
396 }
397
398 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
399 /* DLC is now used by device.
400 * Socket must be disconnected */
401 sk->sk_state = BT_CLOSED;
402 }
403
404 return id;
405}
406
407static int rfcomm_release_dev(void __user *arg)
408{
409 struct rfcomm_dev_req req;
410 struct rfcomm_dev *dev;
411
412 if (copy_from_user(&req, arg, sizeof(req)))
413 return -EFAULT;
414
8de0a154 415 BT_DBG("dev_id %d flags 0x%x", req.dev_id, req.flags);
1da177e4
LT
416
417 if (!(dev = rfcomm_dev_get(req.dev_id)))
418 return -ENODEV;
419
420 if (dev->flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN)) {
421 rfcomm_dev_put(dev);
422 return -EPERM;
423 }
424
425 if (req.flags & (1 << RFCOMM_HANGUP_NOW))
426 rfcomm_dlc_close(dev->dlc, 0);
427
84950cf0
MR
428 /* Shut down TTY synchronously before freeing rfcomm_dev */
429 if (dev->tty)
430 tty_vhangup(dev->tty);
431
1da177e4
LT
432 rfcomm_dev_del(dev);
433 rfcomm_dev_put(dev);
434 return 0;
435}
436
437static int rfcomm_get_dev_list(void __user *arg)
438{
439 struct rfcomm_dev_list_req *dl;
440 struct rfcomm_dev_info *di;
441 struct list_head *p;
442 int n = 0, size, err;
443 u16 dev_num;
444
445 BT_DBG("");
446
447 if (get_user(dev_num, (u16 __user *) arg))
448 return -EFAULT;
449
450 if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di))
451 return -EINVAL;
452
453 size = sizeof(*dl) + dev_num * sizeof(*di);
454
455 if (!(dl = kmalloc(size, GFP_KERNEL)))
456 return -ENOMEM;
457
458 di = dl->dev_info;
459
460 read_lock_bh(&rfcomm_dev_lock);
461
462 list_for_each(p, &rfcomm_dev_list) {
463 struct rfcomm_dev *dev = list_entry(p, struct rfcomm_dev, list);
8de0a154
VT
464 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
465 continue;
1da177e4
LT
466 (di + n)->id = dev->id;
467 (di + n)->flags = dev->flags;
468 (di + n)->state = dev->dlc->state;
469 (di + n)->channel = dev->channel;
470 bacpy(&(di + n)->src, &dev->src);
471 bacpy(&(di + n)->dst, &dev->dst);
472 if (++n >= dev_num)
473 break;
474 }
475
476 read_unlock_bh(&rfcomm_dev_lock);
477
478 dl->dev_num = n;
479 size = sizeof(*dl) + n * sizeof(*di);
480
481 err = copy_to_user(arg, dl, size);
482 kfree(dl);
483
484 return err ? -EFAULT : 0;
485}
486
487static int rfcomm_get_dev_info(void __user *arg)
488{
489 struct rfcomm_dev *dev;
490 struct rfcomm_dev_info di;
491 int err = 0;
492
493 BT_DBG("");
494
495 if (copy_from_user(&di, arg, sizeof(di)))
496 return -EFAULT;
497
498 if (!(dev = rfcomm_dev_get(di.id)))
499 return -ENODEV;
500
501 di.flags = dev->flags;
502 di.channel = dev->channel;
503 di.state = dev->dlc->state;
504 bacpy(&di.src, &dev->src);
505 bacpy(&di.dst, &dev->dst);
506
507 if (copy_to_user(arg, &di, sizeof(di)))
508 err = -EFAULT;
509
510 rfcomm_dev_put(dev);
511 return err;
512}
513
514int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
515{
516 BT_DBG("cmd %d arg %p", cmd, arg);
517
518 switch (cmd) {
519 case RFCOMMCREATEDEV:
520 return rfcomm_create_dev(sk, arg);
521
522 case RFCOMMRELEASEDEV:
523 return rfcomm_release_dev(arg);
524
525 case RFCOMMGETDEVLIST:
526 return rfcomm_get_dev_list(arg);
527
528 case RFCOMMGETDEVINFO:
529 return rfcomm_get_dev_info(arg);
530 }
531
532 return -EINVAL;
533}
534
535/* ---- DLC callbacks ---- */
536static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb)
537{
538 struct rfcomm_dev *dev = dlc->owner;
539 struct tty_struct *tty;
8e87d142 540
1da177e4
LT
541 if (!dev || !(tty = dev->tty)) {
542 kfree_skb(skb);
543 return;
544 }
545
546 BT_DBG("dlc %p tty %p len %d", dlc, tty, skb->len);
547
817d6d3b
PF
548 tty_insert_flip_string(tty, skb->data, skb->len);
549 tty_flip_buffer_push(tty);
1da177e4
LT
550
551 kfree_skb(skb);
552}
553
554static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
555{
556 struct rfcomm_dev *dev = dlc->owner;
557 if (!dev)
558 return;
8e87d142 559
1da177e4
LT
560 BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
561
562 dev->err = err;
563 wake_up_interruptible(&dev->wait);
564
565 if (dlc->state == BT_CLOSED) {
566 if (!dev->tty) {
567 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
77f2a45f
MH
568 if (rfcomm_dev_get(dev->id) == NULL)
569 return;
1da177e4 570
77f2a45f 571 rfcomm_dev_del(dev);
1da177e4
LT
572 /* We have to drop DLC lock here, otherwise
573 rfcomm_dev_put() will dead lock if it's
574 the last reference. */
575 rfcomm_dlc_unlock(dlc);
576 rfcomm_dev_put(dev);
577 rfcomm_dlc_lock(dlc);
578 }
8e87d142 579 } else
1da177e4
LT
580 tty_hangup(dev->tty);
581 }
582}
583
584static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig)
585{
586 struct rfcomm_dev *dev = dlc->owner;
587 if (!dev)
588 return;