mxser: fix compiler warning when building without CONFIG_PCI
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / bluetooth / rfcomm / tty.c
blob22a832098d44057e120ae76cecda07ae2d48ca00
1 /*
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>
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;
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
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
17 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
20 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
21 SOFTWARE IS DISCLAIMED.
25 * RFCOMM TTY.
27 * $Id: tty.c,v 1.24 2002/10/03 01:54:38 holtmann Exp $
30 #include <linux/module.h>
32 #include <linux/tty.h>
33 #include <linux/tty_driver.h>
34 #include <linux/tty_flip.h>
36 #include <linux/capability.h>
37 #include <linux/slab.h>
38 #include <linux/skbuff.h>
40 #include <net/bluetooth/bluetooth.h>
41 #include <net/bluetooth/hci_core.h>
42 #include <net/bluetooth/rfcomm.h>
44 #ifndef CONFIG_BT_RFCOMM_DEBUG
45 #undef BT_DBG
46 #define BT_DBG(D...)
47 #endif
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
54 static struct tty_driver *rfcomm_tty_driver;
56 struct rfcomm_dev {
57 struct list_head list;
58 atomic_t refcnt;
60 char name[12];
61 int id;
62 unsigned long flags;
63 int opened;
64 int err;
66 bdaddr_t src;
67 bdaddr_t dst;
68 u8 channel;
70 uint modem_status;
72 struct rfcomm_dlc *dlc;
73 struct tty_struct *tty;
74 wait_queue_head_t wait;
75 struct tasklet_struct wakeup_task;
77 struct device *tty_dev;
79 atomic_t wmem_alloc;
82 static LIST_HEAD(rfcomm_dev_list);
83 static DEFINE_RWLOCK(rfcomm_dev_lock);
85 static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb);
86 static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err);
87 static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig);
89 static void rfcomm_tty_wakeup(unsigned long arg);
91 /* ---- Device functions ---- */
92 static void rfcomm_dev_destruct(struct rfcomm_dev *dev)
94 struct rfcomm_dlc *dlc = dev->dlc;
96 BT_DBG("dev %p dlc %p", dev, dlc);
98 write_lock_bh(&rfcomm_dev_lock);
99 list_del_init(&dev->list);
100 write_unlock_bh(&rfcomm_dev_lock);
102 rfcomm_dlc_lock(dlc);
103 /* Detach DLC if it's owned by this dev */
104 if (dlc->owner == dev)
105 dlc->owner = NULL;
106 rfcomm_dlc_unlock(dlc);
108 rfcomm_dlc_put(dlc);
110 tty_unregister_device(rfcomm_tty_driver, dev->id);
112 /* Refcount should only hit zero when called from rfcomm_dev_del()
113 which will have taken us off the list. Everything else are
114 refcounting bugs. */
115 BUG_ON(!list_empty(&dev->list));
117 kfree(dev);
119 /* It's safe to call module_put() here because socket still
120 holds reference to this module. */
121 module_put(THIS_MODULE);
124 static inline void rfcomm_dev_hold(struct rfcomm_dev *dev)
126 atomic_inc(&dev->refcnt);
129 static inline void rfcomm_dev_put(struct rfcomm_dev *dev)
131 /* The reason this isn't actually a race, as you no
132 doubt have a little voice screaming at you in your
133 head, is that the refcount should never actually
134 reach zero unless the device has already been taken
135 off the list, in rfcomm_dev_del(). And if that's not
136 true, we'll hit the BUG() in rfcomm_dev_destruct()
137 anyway. */
138 if (atomic_dec_and_test(&dev->refcnt))
139 rfcomm_dev_destruct(dev);
142 static struct rfcomm_dev *__rfcomm_dev_get(int id)
144 struct rfcomm_dev *dev;
145 struct list_head *p;
147 list_for_each(p, &rfcomm_dev_list) {
148 dev = list_entry(p, struct rfcomm_dev, list);
149 if (dev->id == id)
150 return dev;
153 return NULL;
156 static inline struct rfcomm_dev *rfcomm_dev_get(int id)
158 struct rfcomm_dev *dev;
160 read_lock(&rfcomm_dev_lock);
162 dev = __rfcomm_dev_get(id);
164 if (dev) {
165 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
166 dev = NULL;
167 else
168 rfcomm_dev_hold(dev);
171 read_unlock(&rfcomm_dev_lock);
173 return dev;
176 static struct device *rfcomm_get_device(struct rfcomm_dev *dev)
178 struct hci_dev *hdev;
179 struct hci_conn *conn;
181 hdev = hci_get_route(&dev->dst, &dev->src);
182 if (!hdev)
183 return NULL;
185 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &dev->dst);
187 hci_dev_put(hdev);
189 return conn ? &conn->dev : NULL;
192 static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
194 struct rfcomm_dev *dev;
195 struct list_head *head = &rfcomm_dev_list, *p;
196 int err = 0;
198 BT_DBG("id %d channel %d", req->dev_id, req->channel);
200 dev = kzalloc(sizeof(struct rfcomm_dev), GFP_KERNEL);
201 if (!dev)
202 return -ENOMEM;
204 write_lock_bh(&rfcomm_dev_lock);
206 if (req->dev_id < 0) {
207 dev->id = 0;
209 list_for_each(p, &rfcomm_dev_list) {
210 if (list_entry(p, struct rfcomm_dev, list)->id != dev->id)
211 break;
213 dev->id++;
214 head = p;
216 } else {
217 dev->id = req->dev_id;
219 list_for_each(p, &rfcomm_dev_list) {
220 struct rfcomm_dev *entry = list_entry(p, struct rfcomm_dev, list);
222 if (entry->id == dev->id) {
223 err = -EADDRINUSE;
224 goto out;
227 if (entry->id > dev->id - 1)
228 break;
230 head = p;
234 if ((dev->id < 0) || (dev->id > RFCOMM_MAX_DEV - 1)) {
235 err = -ENFILE;
236 goto out;
239 sprintf(dev->name, "rfcomm%d", dev->id);
241 list_add(&dev->list, head);
242 atomic_set(&dev->refcnt, 1);
244 bacpy(&dev->src, &req->src);
245 bacpy(&dev->dst, &req->dst);
246 dev->channel = req->channel;
248 dev->flags = req->flags &
249 ((1 << RFCOMM_RELEASE_ONHUP) | (1 << RFCOMM_REUSE_DLC));
251 init_waitqueue_head(&dev->wait);
252 tasklet_init(&dev->wakeup_task, rfcomm_tty_wakeup, (unsigned long) dev);
254 rfcomm_dlc_lock(dlc);
255 dlc->data_ready = rfcomm_dev_data_ready;
256 dlc->state_change = rfcomm_dev_state_change;
257 dlc->modem_status = rfcomm_dev_modem_status;
259 dlc->owner = dev;
260 dev->dlc = dlc;
261 rfcomm_dlc_unlock(dlc);
263 /* It's safe to call __module_get() here because socket already
264 holds reference to this module. */
265 __module_get(THIS_MODULE);
267 out:
268 write_unlock_bh(&rfcomm_dev_lock);
270 if (err < 0) {
271 kfree(dev);
272 return err;
275 dev->tty_dev = tty_register_device(rfcomm_tty_driver, dev->id, NULL);
277 if (IS_ERR(dev->tty_dev)) {
278 err = PTR_ERR(dev->tty_dev);
279 list_del(&dev->list);
280 kfree(dev);
281 return err;
284 return dev->id;
287 static void rfcomm_dev_del(struct rfcomm_dev *dev)
289 BT_DBG("dev %p", dev);
291 set_bit(RFCOMM_TTY_RELEASED, &dev->flags);
292 rfcomm_dev_put(dev);
295 /* ---- Send buffer ---- */
296 static inline unsigned int rfcomm_room(struct rfcomm_dlc *dlc)
298 /* We can't let it be zero, because we don't get a callback
299 when tx_credits becomes nonzero, hence we'd never wake up */
300 return dlc->mtu * (dlc->tx_credits?:1);
303 static void rfcomm_wfree(struct sk_buff *skb)
305 struct rfcomm_dev *dev = (void *) skb->sk;
306 atomic_sub(skb->truesize, &dev->wmem_alloc);
307 if (test_bit(RFCOMM_TTY_ATTACHED, &dev->flags))
308 tasklet_schedule(&dev->wakeup_task);
309 rfcomm_dev_put(dev);
312 static inline void rfcomm_set_owner_w(struct sk_buff *skb, struct rfcomm_dev *dev)
314 rfcomm_dev_hold(dev);
315 atomic_add(skb->truesize, &dev->wmem_alloc);
316 skb->sk = (void *) dev;
317 skb->destructor = rfcomm_wfree;
320 static struct sk_buff *rfcomm_wmalloc(struct rfcomm_dev *dev, unsigned long size, gfp_t priority)
322 if (atomic_read(&dev->wmem_alloc) < rfcomm_room(dev->dlc)) {
323 struct sk_buff *skb = alloc_skb(size, priority);
324 if (skb) {
325 rfcomm_set_owner_w(skb, dev);
326 return skb;
329 return NULL;
332 /* ---- Device IOCTLs ---- */
334 #define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP))
336 static int rfcomm_create_dev(struct sock *sk, void __user *arg)
338 struct rfcomm_dev_req req;
339 struct rfcomm_dlc *dlc;
340 int id;
342 if (copy_from_user(&req, arg, sizeof(req)))
343 return -EFAULT;
345 BT_DBG("sk %p dev_id %d flags 0x%x", sk, req.dev_id, req.flags);
347 if (req.flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN))
348 return -EPERM;
350 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
351 /* Socket must be connected */
352 if (sk->sk_state != BT_CONNECTED)
353 return -EBADFD;
355 dlc = rfcomm_pi(sk)->dlc;
356 rfcomm_dlc_hold(dlc);
357 } else {
358 dlc = rfcomm_dlc_alloc(GFP_KERNEL);
359 if (!dlc)
360 return -ENOMEM;
363 id = rfcomm_dev_add(&req, dlc);
364 if (id < 0) {
365 rfcomm_dlc_put(dlc);
366 return id;
369 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
370 /* DLC is now used by device.
371 * Socket must be disconnected */
372 sk->sk_state = BT_CLOSED;
375 return id;
378 static int rfcomm_release_dev(void __user *arg)
380 struct rfcomm_dev_req req;
381 struct rfcomm_dev *dev;
383 if (copy_from_user(&req, arg, sizeof(req)))
384 return -EFAULT;
386 BT_DBG("dev_id %d flags 0x%x", req.dev_id, req.flags);
388 if (!(dev = rfcomm_dev_get(req.dev_id)))
389 return -ENODEV;
391 if (dev->flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN)) {
392 rfcomm_dev_put(dev);
393 return -EPERM;
396 if (req.flags & (1 << RFCOMM_HANGUP_NOW))
397 rfcomm_dlc_close(dev->dlc, 0);
399 /* Shut down TTY synchronously before freeing rfcomm_dev */
400 if (dev->tty)
401 tty_vhangup(dev->tty);
403 rfcomm_dev_del(dev);
404 rfcomm_dev_put(dev);
405 return 0;
408 static int rfcomm_get_dev_list(void __user *arg)
410 struct rfcomm_dev_list_req *dl;
411 struct rfcomm_dev_info *di;
412 struct list_head *p;
413 int n = 0, size, err;
414 u16 dev_num;
416 BT_DBG("");
418 if (get_user(dev_num, (u16 __user *) arg))
419 return -EFAULT;
421 if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di))
422 return -EINVAL;
424 size = sizeof(*dl) + dev_num * sizeof(*di);
426 if (!(dl = kmalloc(size, GFP_KERNEL)))
427 return -ENOMEM;
429 di = dl->dev_info;
431 read_lock_bh(&rfcomm_dev_lock);
433 list_for_each(p, &rfcomm_dev_list) {
434 struct rfcomm_dev *dev = list_entry(p, struct rfcomm_dev, list);
435 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
436 continue;
437 (di + n)->id = dev->id;
438 (di + n)->flags = dev->flags;
439 (di + n)->state = dev->dlc->state;
440 (di + n)->channel = dev->channel;
441 bacpy(&(di + n)->src, &dev->src);
442 bacpy(&(di + n)->dst, &dev->dst);
443 if (++n >= dev_num)
444 break;
447 read_unlock_bh(&rfcomm_dev_lock);
449 dl->dev_num = n;
450 size = sizeof(*dl) + n * sizeof(*di);
452 err = copy_to_user(arg, dl, size);
453 kfree(dl);
455 return err ? -EFAULT : 0;
458 static int rfcomm_get_dev_info(void __user *arg)
460 struct rfcomm_dev *dev;
461 struct rfcomm_dev_info di;
462 int err = 0;
464 BT_DBG("");
466 if (copy_from_user(&di, arg, sizeof(di)))
467 return -EFAULT;
469 if (!(dev = rfcomm_dev_get(di.id)))
470 return -ENODEV;
472 di.flags = dev->flags;
473 di.channel = dev->channel;
474 di.state = dev->dlc->state;
475 bacpy(&di.src, &dev->src);
476 bacpy(&di.dst, &dev->dst);
478 if (copy_to_user(arg, &di, sizeof(di)))
479 err = -EFAULT;
481 rfcomm_dev_put(dev);
482 return err;
485 int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
487 BT_DBG("cmd %d arg %p", cmd, arg);
489 switch (cmd) {
490 case RFCOMMCREATEDEV:
491 return rfcomm_create_dev(sk, arg);
493 case RFCOMMRELEASEDEV:
494 return rfcomm_release_dev(arg);
496 case RFCOMMGETDEVLIST:
497 return rfcomm_get_dev_list(arg);
499 case RFCOMMGETDEVINFO:
500 return rfcomm_get_dev_info(arg);
503 return -EINVAL;
506 /* ---- DLC callbacks ---- */
507 static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb)
509 struct rfcomm_dev *dev = dlc->owner;
510 struct tty_struct *tty;
512 if (!dev || !(tty = dev->tty)) {
513 kfree_skb(skb);
514 return;
517 BT_DBG("dlc %p tty %p len %d", dlc, tty, skb->len);
519 tty_insert_flip_string(tty, skb->data, skb->len);
520 tty_flip_buffer_push(tty);
522 kfree_skb(skb);
525 static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
527 struct rfcomm_dev *dev = dlc->owner;
528 if (!dev)
529 return;
531 BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
533 dev->err = err;
534 wake_up_interruptible(&dev->wait);
536 if (dlc->state == BT_CLOSED) {
537 if (!dev->tty) {
538 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
539 if (rfcomm_dev_get(dev->id) == NULL)
540 return;
542 rfcomm_dev_del(dev);
543 /* We have to drop DLC lock here, otherwise
544 rfcomm_dev_put() will dead lock if it's
545 the last reference. */
546 rfcomm_dlc_unlock(dlc);
547 rfcomm_dev_put(dev);
548 rfcomm_dlc_lock(dlc);
550 } else
551 tty_hangup(dev->tty);
555 static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig)
557 struct rfcomm_dev *dev = dlc->owner;
558 if (!dev)
559 return;
561 BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc, dev, v24_sig);
563 if ((dev->modem_status & TIOCM_CD) && !(v24_sig & RFCOMM_V24_DV)) {
564 if (dev->tty && !C_CLOCAL(dev->tty))
565 tty_hangup(dev->tty);
568 dev->modem_status =
569 ((v24_sig & RFCOMM_V24_RTC) ? (TIOCM_DSR | TIOCM_DTR) : 0) |
570 ((v24_sig & RFCOMM_V24_RTR) ? (TIOCM_RTS | TIOCM_CTS) : 0) |
571 ((v24_sig & RFCOMM_V24_IC) ? TIOCM_RI : 0) |
572 ((v24_sig & RFCOMM_V24_DV) ? TIOCM_CD : 0);
575 /* ---- TTY functions ---- */
576 static void rfcomm_tty_wakeup(unsigned long arg)
578 struct rfcomm_dev *dev = (void *) arg;
579 struct tty_struct *tty = dev->tty;
580 if (!tty)
581 return;
583 BT_DBG("dev %p tty %p", dev, tty);
585 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && tty->ldisc.write_wakeup)
586 (tty->ldisc.write_wakeup)(tty);
588 wake_up_interruptible(&tty->write_wait);
589 #ifdef SERIAL_HAVE_POLL_WAIT
590 wake_up_interruptible(&tty->poll_wait);
591 #endif
594 static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
596 DECLARE_WAITQUEUE(wait, current);
597 struct rfcomm_dev *dev;
598 struct rfcomm_dlc *dlc;
599 int err, id;
601 id = tty->index;
603 BT_DBG("tty %p id %d", tty, id);
605 /* We don't leak this refcount. For reasons which are not entirely
606 clear, the TTY layer will call our ->close() method even if the
607 open fails. We decrease the refcount there, and decreasing it
608 here too would cause breakage. */
609 dev = rfcomm_dev_get(id);
610 if (!dev)
611 return -ENODEV;
613 BT_DBG("dev %p dst %s channel %d opened %d", dev, batostr(&dev->dst), dev->channel, dev->opened);
615 if (dev->opened++ != 0)
616 return 0;
618 dlc = dev->dlc;
620 /* Attach TTY and open DLC */
622 rfcomm_dlc_lock(dlc);
623 tty->driver_data = dev;
624 dev->tty = tty;
625 rfcomm_dlc_unlock(dlc);
626 set_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
628 err = rfcomm_dlc_open(dlc, &dev->src, &dev->dst, dev->channel);
629 if (err < 0)
630 return err;
632 /* Wait for DLC to connect */
633 add_wait_queue(&dev->wait, &wait);
634 while (1) {
635 set_current_state(TASK_INTERRUPTIBLE);
637 if (dlc->state == BT_CLOSED) {
638 err = -dev->err;
639 break;
642 if (dlc->state == BT_CONNECTED)
643 break;
645 if (signal_pending(current)) {
646 err = -EINTR;
647 break;
650 schedule();
652 set_current_state(TASK_RUNNING);
653 remove_wait_queue(&dev->wait, &wait);
655 if (err == 0)
656 device_move(dev->tty_dev, rfcomm_get_device(dev));
658 return err;
661 static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
663 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
664 if (!dev)
665 return;
667 BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc, dev->opened);
669 if (--dev->opened == 0) {
670 device_move(dev->tty_dev, NULL);
672 /* Close DLC and dettach TTY */
673 rfcomm_dlc_close(dev->dlc, 0);
675 clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
676 tasklet_kill(&dev->wakeup_task);
678 rfcomm_dlc_lock(dev->dlc);
679 tty->driver_data = NULL;
680 dev->tty = NULL;
681 rfcomm_dlc_unlock(dev->dlc);
684 rfcomm_dev_put(dev);
687 static int rfcomm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
689 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
690 struct rfcomm_dlc *dlc = dev->dlc;
691 struct sk_buff *skb;
692 int err = 0, sent = 0, size;
694 BT_DBG("tty %p count %d", tty, count);
696 while (count) {
697 size = min_t(uint, count, dlc->mtu);
699 skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, GFP_ATOMIC);
701 if (!skb)
702 break;
704 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
706 memcpy(skb_put(skb, size), buf + sent, size);
708 if ((err = rfcomm_dlc_send(dlc, skb)) < 0) {
709 kfree_skb(skb);
710 break;
713 sent += size;
714 count -= size;
717 return sent ? sent : err;
720 static int rfcomm_tty_write_room(struct tty_struct *tty)
722 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
723 int room;
725 BT_DBG("tty %p", tty);
727 if (!dev || !dev->dlc)
728 return 0;
730 room = rfcomm_room(dev->dlc) - atomic_read(&dev->wmem_alloc);
731 if (room < 0)
732 room = 0;
734 return room;
737 static int rfcomm_tty_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd, unsigned long arg)
739 BT_DBG("tty %p cmd 0x%02x", tty, cmd);
741 switch (cmd) {
742 case TCGETS:
743 BT_DBG("TCGETS is not supported");
744 return -ENOIOCTLCMD;
746 case TCSETS:
747 BT_DBG("TCSETS is not supported");
748 return -ENOIOCTLCMD;
750 case TIOCMIWAIT:
751 BT_DBG("TIOCMIWAIT");
752 break;
754 case TIOCGICOUNT:
755 BT_DBG("TIOCGICOUNT");
756 break;
758 case TIOCGSERIAL:
759 BT_ERR("TIOCGSERIAL is not supported");
760 return -ENOIOCTLCMD;
762 case TIOCSSERIAL:
763 BT_ERR("TIOCSSERIAL is not supported");
764 return -ENOIOCTLCMD;
766 case TIOCSERGSTRUCT:
767 BT_ERR("TIOCSERGSTRUCT is not supported");
768 return -ENOIOCTLCMD;
770 case TIOCSERGETLSR:
771 BT_ERR("TIOCSERGETLSR is not supported");
772 return -ENOIOCTLCMD;
774 case TIOCSERCONFIG:
775 BT_ERR("TIOCSERCONFIG is not supported");
776 return -ENOIOCTLCMD;
778 default:
779 return -ENOIOCTLCMD; /* ioctls which we must ignore */
783 return -ENOIOCTLCMD;
786 static void rfcomm_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
788 struct ktermios *new = tty->termios;
789 int old_baud_rate = tty_termios_baud_rate(old);
790 int new_baud_rate = tty_termios_baud_rate(new);
792 u8 baud, data_bits, stop_bits, parity, x_on, x_off;
793 u16 changes = 0;
795 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
797 BT_DBG("tty %p termios %p", tty, old);
799 if (!dev || !dev->dlc || !dev->dlc->session)
800 return;
802 /* Handle turning off CRTSCTS */
803 if ((old->c_cflag & CRTSCTS) && !(new->c_cflag & CRTSCTS))
804 BT_DBG("Turning off CRTSCTS unsupported");
806 /* Parity on/off and when on, odd/even */
807 if (((old->c_cflag & PARENB) != (new->c_cflag & PARENB)) ||
808 ((old->c_cflag & PARODD) != (new->c_cflag & PARODD)) ) {
809 changes |= RFCOMM_RPN_PM_PARITY;
810 BT_DBG("Parity change detected.");
813 /* Mark and space parity are not supported! */
814 if (new->c_cflag & PARENB) {
815 if (new->c_cflag & PARODD) {
816 BT_DBG("Parity is ODD");
817 parity = RFCOMM_RPN_PARITY_ODD;
818 } else {
819 BT_DBG("Parity is EVEN");
820 parity = RFCOMM_RPN_PARITY_EVEN;
822 } else {
823 BT_DBG("Parity is OFF");
824 parity = RFCOMM_RPN_PARITY_NONE;
827 /* Setting the x_on / x_off characters */
828 if (old->c_cc[VSTOP] != new->c_cc[VSTOP]) {
829 BT_DBG("XOFF custom");
830 x_on = new->c_cc[VSTOP];
831 changes |= RFCOMM_RPN_PM_XON;
832 } else {
833 BT_DBG("XOFF default");
834 x_on = RFCOMM_RPN_XON_CHAR;
837 if (old->c_cc[VSTART] != new->c_cc[VSTART]) {
838 BT_DBG("XON custom");
839 x_off = new->c_cc[VSTART];
840 changes |= RFCOMM_RPN_PM_XOFF;
841 } else {
842 BT_DBG("XON default");
843 x_off = RFCOMM_RPN_XOFF_CHAR;
846 /* Handle setting of stop bits */
847 if ((old->c_cflag & CSTOPB) != (new->c_cflag & CSTOPB))
848 changes |= RFCOMM_RPN_PM_STOP;
850 /* POSIX does not support 1.5 stop bits and RFCOMM does not
851 * support 2 stop bits. So a request for 2 stop bits gets
852 * translated to 1.5 stop bits */
853 if (new->c_cflag & CSTOPB) {
854 stop_bits = RFCOMM_RPN_STOP_15;
855 } else {
856 stop_bits = RFCOMM_RPN_STOP_1;
859 /* Handle number of data bits [5-8] */
860 if ((old->c_cflag & CSIZE) != (new->c_cflag & CSIZE))
861 changes |= RFCOMM_RPN_PM_DATA;
863 switch (new->c_cflag & CSIZE) {
864 case CS5:
865 data_bits = RFCOMM_RPN_DATA_5;
866 break;
867 case CS6:
868 data_bits = RFCOMM_RPN_DATA_6;
869 break;
870 case CS7:
871 data_bits = RFCOMM_RPN_DATA_7;
872 break;
873 case CS8:
874 data_bits = RFCOMM_RPN_DATA_8;
875 break;
876 default:
877 data_bits = RFCOMM_RPN_DATA_8;
878 break;
881 /* Handle baudrate settings */
882 if (old_baud_rate != new_baud_rate)
883 changes |= RFCOMM_RPN_PM_BITRATE;
885 switch (new_baud_rate) {
886 case 2400:
887 baud = RFCOMM_RPN_BR_2400;
888 break;
889 case 4800:
890 baud = RFCOMM_RPN_BR_4800;
891 break;
892 case 7200:
893 baud = RFCOMM_RPN_BR_7200;
894 break;
895 case 9600:
896 baud = RFCOMM_RPN_BR_9600;
897 break;
898 case 19200:
899 baud = RFCOMM_RPN_BR_19200;
900 break;
901 case 38400:
902 baud = RFCOMM_RPN_BR_38400;
903 break;
904 case 57600:
905 baud = RFCOMM_RPN_BR_57600;
906 break;
907 case 115200:
908 baud = RFCOMM_RPN_BR_115200;
909 break;
910 case 230400:
911 baud = RFCOMM_RPN_BR_230400;
912 break;
913 default:
914 /* 9600 is standard accordinag to the RFCOMM specification */
915 baud = RFCOMM_RPN_BR_9600;
916 break;
920 if (changes)
921 rfcomm_send_rpn(dev->dlc->session, 1, dev->dlc->dlci, baud,
922 data_bits, stop_bits, parity,
923 RFCOMM_RPN_FLOW_NONE, x_on, x_off, changes);
925 return;
928 static void rfcomm_tty_throttle(struct tty_struct *tty)
930 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
932 BT_DBG("tty %p dev %p", tty, dev);
934 rfcomm_dlc_throttle(dev->dlc);
937 static void rfcomm_tty_unthrottle(struct tty_struct *tty)
939 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
941 BT_DBG("tty %p dev %p", tty, dev);
943 rfcomm_dlc_unthrottle(dev->dlc);
946 static int rfcomm_tty_chars_in_buffer(struct tty_struct *tty)
948 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
950 BT_DBG("tty %p dev %p", tty, dev);
952 if (!dev || !dev->dlc)
953 return 0;
955 if (!skb_queue_empty(&dev->dlc->tx_queue))
956 return dev->dlc->mtu;
958 return 0;
961 static void rfcomm_tty_flush_buffer(struct tty_struct *tty)
963 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
965 BT_DBG("tty %p dev %p", tty, dev);
967 if (!dev || !dev->dlc)
968 return;
970 skb_queue_purge(&dev->dlc->tx_queue);
972 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && tty->ldisc.write_wakeup)
973 tty->ldisc.write_wakeup(tty);
976 static void rfcomm_tty_send_xchar(struct tty_struct *tty, char ch)
978 BT_DBG("tty %p ch %c", tty, ch);
981 static void rfcomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
983 BT_DBG("tty %p timeout %d", tty, timeout);
986 static void rfcomm_tty_hangup(struct tty_struct *tty)
988 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
990 BT_DBG("tty %p dev %p", tty, dev);
992 if (!dev)
993 return;
995 rfcomm_tty_flush_buffer(tty);
997 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
998 if (rfcomm_dev_get(dev->id) == NULL)
999 return;
1000 rfcomm_dev_del(dev);
1001 rfcomm_dev_put(dev);
1005 static int rfcomm_tty_read_proc(char *buf, char **start, off_t offset, int len, int *eof, void *unused)
1007 return 0;
1010 static int rfcomm_tty_tiocmget(struct tty_struct *tty, struct file *filp)
1012 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1014 BT_DBG("tty %p dev %p", tty, dev);
1016 return dev->modem_status;
1019 static int rfcomm_tty_tiocmset(struct tty_struct *tty, struct file *filp, unsigned int set, unsigned int clear)
1021 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1022 struct rfcomm_dlc *dlc = dev->dlc;
1023 u8 v24_sig;
1025 BT_DBG("tty %p dev %p set 0x%02x clear 0x%02x", tty, dev, set, clear);
1027 rfcomm_dlc_get_modem_status(dlc, &v24_sig);
1029 if (set & TIOCM_DSR || set & TIOCM_DTR)
1030 v24_sig |= RFCOMM_V24_RTC;
1031 if (set & TIOCM_RTS || set & TIOCM_CTS)
1032 v24_sig |= RFCOMM_V24_RTR;
1033 if (set & TIOCM_RI)
1034 v24_sig |= RFCOMM_V24_IC;
1035 if (set & TIOCM_CD)
1036 v24_sig |= RFCOMM_V24_DV;
1038 if (clear & TIOCM_DSR || clear & TIOCM_DTR)
1039 v24_sig &= ~RFCOMM_V24_RTC;
1040 if (clear & TIOCM_RTS || clear & TIOCM_CTS)
1041 v24_sig &= ~RFCOMM_V24_RTR;
1042 if (clear & TIOCM_RI)
1043 v24_sig &= ~RFCOMM_V24_IC;
1044 if (clear & TIOCM_CD)
1045 v24_sig &= ~RFCOMM_V24_DV;
1047 rfcomm_dlc_set_modem_status(dlc, v24_sig);
1049 return 0;
1052 /* ---- TTY structure ---- */
1054 static const struct tty_operations rfcomm_ops = {
1055 .open = rfcomm_tty_open,
1056 .close = rfcomm_tty_close,
1057 .write = rfcomm_tty_write,
1058 .write_room = rfcomm_tty_write_room,
1059 .chars_in_buffer = rfcomm_tty_chars_in_buffer,
1060 .flush_buffer = rfcomm_tty_flush_buffer,
1061 .ioctl = rfcomm_tty_ioctl,
1062 .throttle = rfcomm_tty_throttle,
1063 .unthrottle = rfcomm_tty_unthrottle,
1064 .set_termios = rfcomm_tty_set_termios,
1065 .send_xchar = rfcomm_tty_send_xchar,
1066 .hangup = rfcomm_tty_hangup,
1067 .wait_until_sent = rfcomm_tty_wait_until_sent,
1068 .read_proc = rfcomm_tty_read_proc,
1069 .tiocmget = rfcomm_tty_tiocmget,
1070 .tiocmset = rfcomm_tty_tiocmset,
1073 int rfcomm_init_ttys(void)
1075 rfcomm_tty_driver = alloc_tty_driver(RFCOMM_TTY_PORTS);
1076 if (!rfcomm_tty_driver)
1077 return -1;
1079 rfcomm_tty_driver->owner = THIS_MODULE;
1080 rfcomm_tty_driver->driver_name = "rfcomm";
1081 rfcomm_tty_driver->name = "rfcomm";
1082 rfcomm_tty_driver->major = RFCOMM_TTY_MAJOR;
1083 rfcomm_tty_driver->minor_start = RFCOMM_TTY_MINOR;
1084 rfcomm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1085 rfcomm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1086 rfcomm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1087 rfcomm_tty_driver->init_termios = tty_std_termios;
1088 rfcomm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1089 tty_set_operations(rfcomm_tty_driver, &rfcomm_ops);
1091 if (tty_register_driver(rfcomm_tty_driver)) {
1092 BT_ERR("Can't register RFCOMM TTY driver");
1093 put_tty_driver(rfcomm_tty_driver);
1094 return -1;
1097 BT_INFO("RFCOMM TTY layer initialized");
1099 return 0;
1102 void rfcomm_cleanup_ttys(void)
1104 tty_unregister_driver(rfcomm_tty_driver);
1105 put_tty_driver(rfcomm_tty_driver);