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.
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
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;
57 struct list_head list;
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;
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)
106 rfcomm_dlc_unlock(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
115 BUG_ON(!list_empty(&dev->list));
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()
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;
147 list_for_each(p, &rfcomm_dev_list) {
148 dev = list_entry(p, struct rfcomm_dev, list);
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);
165 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
168 rfcomm_dev_hold(dev);
171 read_unlock(&rfcomm_dev_lock);
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);
185 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &dev->dst);
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;
198 BT_DBG("id %d channel %d", req->dev_id, req->channel);
200 dev = kzalloc(sizeof(struct rfcomm_dev), GFP_KERNEL);
204 write_lock_bh(&rfcomm_dev_lock);
206 if (req->dev_id < 0) {
209 list_for_each(p, &rfcomm_dev_list) {
210 if (list_entry(p, struct rfcomm_dev, list)->id != dev->id)
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) {
227 if (entry->id > dev->id - 1)
234 if ((dev->id < 0) || (dev->id > RFCOMM_MAX_DEV - 1)) {
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;
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);
268 write_unlock_bh(&rfcomm_dev_lock);
275 dev->tty_dev = tty_register_device(rfcomm_tty_driver, dev->id, NULL);
277 if (IS_ERR(dev->tty_dev)) {
278 list_del(&dev->list);
280 return PTR_ERR(dev->tty_dev);
286 static void rfcomm_dev_del(struct rfcomm_dev *dev)
288 BT_DBG("dev %p", dev);
290 set_bit(RFCOMM_TTY_RELEASED, &dev->flags);
294 /* ---- Send buffer ---- */
295 static inline unsigned int rfcomm_room(struct rfcomm_dlc *dlc)
297 /* We can't let it be zero, because we don't get a callback
298 when tx_credits becomes nonzero, hence we'd never wake up */
299 return dlc->mtu * (dlc->tx_credits?:1);
302 static void rfcomm_wfree(struct sk_buff *skb)
304 struct rfcomm_dev *dev = (void *) skb->sk;
305 atomic_sub(skb->truesize, &dev->wmem_alloc);
306 if (test_bit(RFCOMM_TTY_ATTACHED, &dev->flags))
307 tasklet_schedule(&dev->wakeup_task);
311 static inline void rfcomm_set_owner_w(struct sk_buff *skb, struct rfcomm_dev *dev)
313 rfcomm_dev_hold(dev);
314 atomic_add(skb->truesize, &dev->wmem_alloc);
315 skb->sk = (void *) dev;
316 skb->destructor = rfcomm_wfree;
319 static struct sk_buff *rfcomm_wmalloc(struct rfcomm_dev *dev, unsigned long size, gfp_t priority)
321 if (atomic_read(&dev->wmem_alloc) < rfcomm_room(dev->dlc)) {
322 struct sk_buff *skb = alloc_skb(size, priority);
324 rfcomm_set_owner_w(skb, dev);
331 /* ---- Device IOCTLs ---- */
333 #define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP))
335 static int rfcomm_create_dev(struct sock *sk, void __user *arg)
337 struct rfcomm_dev_req req;
338 struct rfcomm_dlc *dlc;
341 if (copy_from_user(&req, arg, sizeof(req)))
344 BT_DBG("sk %p dev_id %d flags 0x%x", sk, req.dev_id, req.flags);
346 if (req.flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN))
349 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
350 /* Socket must be connected */
351 if (sk->sk_state != BT_CONNECTED)
354 dlc = rfcomm_pi(sk)->dlc;
355 rfcomm_dlc_hold(dlc);
357 dlc = rfcomm_dlc_alloc(GFP_KERNEL);
362 id = rfcomm_dev_add(&req, dlc);
368 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
369 /* DLC is now used by device.
370 * Socket must be disconnected */
371 sk->sk_state = BT_CLOSED;
377 static int rfcomm_release_dev(void __user *arg)
379 struct rfcomm_dev_req req;
380 struct rfcomm_dev *dev;
382 if (copy_from_user(&req, arg, sizeof(req)))
385 BT_DBG("dev_id %d flags 0x%x", req.dev_id, req.flags);
387 if (!(dev = rfcomm_dev_get(req.dev_id)))
390 if (dev->flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN)) {
395 if (req.flags & (1 << RFCOMM_HANGUP_NOW))
396 rfcomm_dlc_close(dev->dlc, 0);
398 /* Shut down TTY synchronously before freeing rfcomm_dev */
400 tty_vhangup(dev->tty);
407 static int rfcomm_get_dev_list(void __user *arg)
409 struct rfcomm_dev_list_req *dl;
410 struct rfcomm_dev_info *di;
412 int n = 0, size, err;
417 if (get_user(dev_num, (u16 __user *) arg))
420 if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di))
423 size = sizeof(*dl) + dev_num * sizeof(*di);
425 if (!(dl = kmalloc(size, GFP_KERNEL)))
430 read_lock_bh(&rfcomm_dev_lock);
432 list_for_each(p, &rfcomm_dev_list) {
433 struct rfcomm_dev *dev = list_entry(p, struct rfcomm_dev, list);
434 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
436 (di + n)->id = dev->id;
437 (di + n)->flags = dev->flags;
438 (di + n)->state = dev->dlc->state;
439 (di + n)->channel = dev->channel;
440 bacpy(&(di + n)->src, &dev->src);
441 bacpy(&(di + n)->dst, &dev->dst);
446 read_unlock_bh(&rfcomm_dev_lock);
449 size = sizeof(*dl) + n * sizeof(*di);
451 err = copy_to_user(arg, dl, size);
454 return err ? -EFAULT : 0;
457 static int rfcomm_get_dev_info(void __user *arg)
459 struct rfcomm_dev *dev;
460 struct rfcomm_dev_info di;
465 if (copy_from_user(&di, arg, sizeof(di)))
468 if (!(dev = rfcomm_dev_get(di.id)))
471 di.flags = dev->flags;
472 di.channel = dev->channel;
473 di.state = dev->dlc->state;
474 bacpy(&di.src, &dev->src);
475 bacpy(&di.dst, &dev->dst);
477 if (copy_to_user(arg, &di, sizeof(di)))
484 int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
486 BT_DBG("cmd %d arg %p", cmd, arg);
489 case RFCOMMCREATEDEV:
490 return rfcomm_create_dev(sk, arg);
492 case RFCOMMRELEASEDEV:
493 return rfcomm_release_dev(arg);
495 case RFCOMMGETDEVLIST:
496 return rfcomm_get_dev_list(arg);
498 case RFCOMMGETDEVINFO:
499 return rfcomm_get_dev_info(arg);
505 /* ---- DLC callbacks ---- */
506 static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb)
508 struct rfcomm_dev *dev = dlc->owner;
509 struct tty_struct *tty;
511 if (!dev || !(tty = dev->tty)) {
516 BT_DBG("dlc %p tty %p len %d", dlc, tty, skb->len);
518 tty_insert_flip_string(tty, skb->data, skb->len);
519 tty_flip_buffer_push(tty);
524 static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
526 struct rfcomm_dev *dev = dlc->owner;
530 BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
533 wake_up_interruptible(&dev->wait);
535 if (dlc->state == BT_CLOSED) {
537 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
538 if (rfcomm_dev_get(dev->id) == NULL)
542 /* We have to drop DLC lock here, otherwise
543 rfcomm_dev_put() will dead lock if it's
544 the last reference. */
545 rfcomm_dlc_unlock(dlc);
547 rfcomm_dlc_lock(dlc);
550 tty_hangup(dev->tty);
554 static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig)
556 struct rfcomm_dev *dev = dlc->owner;
560 BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc, dev, v24_sig);
562 if ((dev->modem_status & TIOCM_CD) && !(v24_sig & RFCOMM_V24_DV)) {
563 if (dev->tty && !C_CLOCAL(dev->tty))
564 tty_hangup(dev->tty);
568 ((v24_sig & RFCOMM_V24_RTC) ? (TIOCM_DSR | TIOCM_DTR) : 0) |
569 ((v24_sig & RFCOMM_V24_RTR) ? (TIOCM_RTS | TIOCM_CTS) : 0) |
570 ((v24_sig & RFCOMM_V24_IC) ? TIOCM_RI : 0) |
571 ((v24_sig & RFCOMM_V24_DV) ? TIOCM_CD : 0);
574 /* ---- TTY functions ---- */
575 static void rfcomm_tty_wakeup(unsigned long arg)
577 struct rfcomm_dev *dev = (void *) arg;
578 struct tty_struct *tty = dev->tty;
582 BT_DBG("dev %p tty %p", dev, tty);
584 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && tty->ldisc.write_wakeup)
585 (tty->ldisc.write_wakeup)(tty);
587 wake_up_interruptible(&tty->write_wait);
588 #ifdef SERIAL_HAVE_POLL_WAIT
589 wake_up_interruptible(&tty->poll_wait);
593 static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
595 DECLARE_WAITQUEUE(wait, current);
596 struct rfcomm_dev *dev;
597 struct rfcomm_dlc *dlc;
602 BT_DBG("tty %p id %d", tty, id);
604 /* We don't leak this refcount. For reasons which are not entirely
605 clear, the TTY layer will call our ->close() method even if the
606 open fails. We decrease the refcount there, and decreasing it
607 here too would cause breakage. */
608 dev = rfcomm_dev_get(id);
612 BT_DBG("dev %p dst %s channel %d opened %d", dev, batostr(&dev->dst), dev->channel, dev->opened);
614 if (dev->opened++ != 0)
619 /* Attach TTY and open DLC */
621 rfcomm_dlc_lock(dlc);
622 tty->driver_data = dev;
624 rfcomm_dlc_unlock(dlc);
625 set_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
627 err = rfcomm_dlc_open(dlc, &dev->src, &dev->dst, dev->channel);
631 /* Wait for DLC to connect */
632 add_wait_queue(&dev->wait, &wait);
634 set_current_state(TASK_INTERRUPTIBLE);
636 if (dlc->state == BT_CLOSED) {
641 if (dlc->state == BT_CONNECTED)
644 if (signal_pending(current)) {
651 set_current_state(TASK_RUNNING);
652 remove_wait_queue(&dev->wait, &wait);
655 device_move(dev->tty_dev, rfcomm_get_device(dev));
660 static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
662 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
666 BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc, dev->opened);
668 if (--dev->opened == 0) {
669 device_move(dev->tty_dev, NULL);
671 /* Close DLC and dettach TTY */
672 rfcomm_dlc_close(dev->dlc, 0);
674 clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
675 tasklet_kill(&dev->wakeup_task);
677 rfcomm_dlc_lock(dev->dlc);
678 tty->driver_data = NULL;
680 rfcomm_dlc_unlock(dev->dlc);
686 static int rfcomm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
688 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
689 struct rfcomm_dlc *dlc = dev->dlc;
691 int err = 0, sent = 0, size;
693 BT_DBG("tty %p count %d", tty, count);
696 size = min_t(uint, count, dlc->mtu);
698 skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, GFP_ATOMIC);
703 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
705 memcpy(skb_put(skb, size), buf + sent, size);
707 if ((err = rfcomm_dlc_send(dlc, skb)) < 0) {
716 return sent ? sent : err;
719 static int rfcomm_tty_write_room(struct tty_struct *tty)
721 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
724 BT_DBG("tty %p", tty);
726 if (!dev || !dev->dlc)
729 room = rfcomm_room(dev->dlc) - atomic_read(&dev->wmem_alloc);
736 static int rfcomm_tty_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd, unsigned long arg)
738 BT_DBG("tty %p cmd 0x%02x", tty, cmd);
742 BT_DBG("TCGETS is not supported");
746 BT_DBG("TCSETS is not supported");
750 BT_DBG("TIOCMIWAIT");
754 BT_DBG("TIOCGICOUNT");
758 BT_ERR("TIOCGSERIAL is not supported");
762 BT_ERR("TIOCSSERIAL is not supported");
766 BT_ERR("TIOCSERGSTRUCT is not supported");
770 BT_ERR("TIOCSERGETLSR is not supported");
774 BT_ERR("TIOCSERCONFIG is not supported");
778 return -ENOIOCTLCMD; /* ioctls which we must ignore */
785 static void rfcomm_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
787 struct ktermios *new = tty->termios;
788 int old_baud_rate = tty_termios_baud_rate(old);
789 int new_baud_rate = tty_termios_baud_rate(new);
791 u8 baud, data_bits, stop_bits, parity, x_on, x_off;
794 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
796 BT_DBG("tty %p termios %p", tty, old);
798 if (!dev || !dev->dlc || !dev->dlc->session)
801 /* Handle turning off CRTSCTS */
802 if ((old->c_cflag & CRTSCTS) && !(new->c_cflag & CRTSCTS))
803 BT_DBG("Turning off CRTSCTS unsupported");
805 /* Parity on/off and when on, odd/even */
806 if (((old->c_cflag & PARENB) != (new->c_cflag & PARENB)) ||
807 ((old->c_cflag & PARODD) != (new->c_cflag & PARODD)) ) {
808 changes |= RFCOMM_RPN_PM_PARITY;
809 BT_DBG("Parity change detected.");
812 /* Mark and space parity are not supported! */
813 if (new->c_cflag & PARENB) {
814 if (new->c_cflag & PARODD) {
815 BT_DBG("Parity is ODD");
816 parity = RFCOMM_RPN_PARITY_ODD;
818 BT_DBG("Parity is EVEN");
819 parity = RFCOMM_RPN_PARITY_EVEN;
822 BT_DBG("Parity is OFF");
823 parity = RFCOMM_RPN_PARITY_NONE;
826 /* Setting the x_on / x_off characters */
827 if (old->c_cc[VSTOP] != new->c_cc[VSTOP]) {
828 BT_DBG("XOFF custom");
829 x_on = new->c_cc[VSTOP];
830 changes |= RFCOMM_RPN_PM_XON;
832 BT_DBG("XOFF default");
833 x_on = RFCOMM_RPN_XON_CHAR;
836 if (old->c_cc[VSTART] != new->c_cc[VSTART]) {
837 BT_DBG("XON custom");
838 x_off = new->c_cc[VSTART];
839 changes |= RFCOMM_RPN_PM_XOFF;
841 BT_DBG("XON default");
842 x_off = RFCOMM_RPN_XOFF_CHAR;
845 /* Handle setting of stop bits */
846 if ((old->c_cflag & CSTOPB) != (new->c_cflag & CSTOPB))
847 changes |= RFCOMM_RPN_PM_STOP;
849 /* POSIX does not support 1.5 stop bits and RFCOMM does not
850 * support 2 stop bits. So a request for 2 stop bits gets
851 * translated to 1.5 stop bits */
852 if (new->c_cflag & CSTOPB) {
853 stop_bits = RFCOMM_RPN_STOP_15;
855 stop_bits = RFCOMM_RPN_STOP_1;
858 /* Handle number of data bits [5-8] */
859 if ((old->c_cflag & CSIZE) != (new->c_cflag & CSIZE))
860 changes |= RFCOMM_RPN_PM_DATA;
862 switch (new->c_cflag & CSIZE) {
864 data_bits = RFCOMM_RPN_DATA_5;
867 data_bits = RFCOMM_RPN_DATA_6;
870 data_bits = RFCOMM_RPN_DATA_7;
873 data_bits = RFCOMM_RPN_DATA_8;
876 data_bits = RFCOMM_RPN_DATA_8;
880 /* Handle baudrate settings */
881 if (old_baud_rate != new_baud_rate)
882 changes |= RFCOMM_RPN_PM_BITRATE;
884 switch (new_baud_rate) {
886 baud = RFCOMM_RPN_BR_2400;
889 baud = RFCOMM_RPN_BR_4800;
892 baud = RFCOMM_RPN_BR_7200;
895 baud = RFCOMM_RPN_BR_9600;
898 baud = RFCOMM_RPN_BR_19200;
901 baud = RFCOMM_RPN_BR_38400;
904 baud = RFCOMM_RPN_BR_57600;
907 baud = RFCOMM_RPN_BR_115200;
910 baud = RFCOMM_RPN_BR_230400;
913 /* 9600 is standard accordinag to the RFCOMM specification */
914 baud = RFCOMM_RPN_BR_9600;
920 rfcomm_send_rpn(dev->dlc->session, 1, dev->dlc->dlci, baud,
921 data_bits, stop_bits, parity,
922 RFCOMM_RPN_FLOW_NONE, x_on, x_off, changes);
927 static void rfcomm_tty_throttle(struct tty_struct *tty)
929 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
931 BT_DBG("tty %p dev %p", tty, dev);
933 rfcomm_dlc_throttle(dev->dlc);
936 static void rfcomm_tty_unthrottle(struct tty_struct *tty)
938 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
940 BT_DBG("tty %p dev %p", tty, dev);
942 rfcomm_dlc_unthrottle(dev->dlc);
945 static int rfcomm_tty_chars_in_buffer(struct tty_struct *tty)
947 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
949 BT_DBG("tty %p dev %p", tty, dev);
951 if (!dev || !dev->dlc)
954 if (!skb_queue_empty(&dev->dlc->tx_queue))
955 return dev->dlc->mtu;
960 static void rfcomm_tty_flush_buffer(struct tty_struct *tty)
962 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
964 BT_DBG("tty %p dev %p", tty, dev);
966 if (!dev || !dev->dlc)
969 skb_queue_purge(&dev->dlc->tx_queue);
971 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && tty->ldisc.write_wakeup)
972 tty->ldisc.write_wakeup(tty);
975 static void rfcomm_tty_send_xchar(struct tty_struct *tty, char ch)
977 BT_DBG("tty %p ch %c", tty, ch);
980 static void rfcomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
982 BT_DBG("tty %p timeout %d", tty, timeout);
985 static void rfcomm_tty_hangup(struct tty_struct *tty)
987 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
989 BT_DBG("tty %p dev %p", tty, dev);
994 rfcomm_tty_flush_buffer(tty);
996 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
997 if (rfcomm_dev_get(dev->id) == NULL)
1000 rfcomm_dev_put(dev);
1004 static int rfcomm_tty_read_proc(char *buf, char **start, off_t offset, int len, int *eof, void *unused)
1009 static int rfcomm_tty_tiocmget(struct tty_struct *tty, struct file *filp)
1011 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1013 BT_DBG("tty %p dev %p", tty, dev);
1015 return dev->modem_status;
1018 static int rfcomm_tty_tiocmset(struct tty_struct *tty, struct file *filp, unsigned int set, unsigned int clear)
1020 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1021 struct rfcomm_dlc *dlc = dev->dlc;
1024 BT_DBG("tty %p dev %p set 0x%02x clear 0x%02x", tty, dev, set, clear);
1026 rfcomm_dlc_get_modem_status(dlc, &v24_sig);
1028 if (set & TIOCM_DSR || set & TIOCM_DTR)
1029 v24_sig |= RFCOMM_V24_RTC;
1030 if (set & TIOCM_RTS || set & TIOCM_CTS)
1031 v24_sig |= RFCOMM_V24_RTR;
1033 v24_sig |= RFCOMM_V24_IC;
1035 v24_sig |= RFCOMM_V24_DV;
1037 if (clear & TIOCM_DSR || clear & TIOCM_DTR)
1038 v24_sig &= ~RFCOMM_V24_RTC;
1039 if (clear & TIOCM_RTS || clear & TIOCM_CTS)
1040 v24_sig &= ~RFCOMM_V24_RTR;
1041 if (clear & TIOCM_RI)
1042 v24_sig &= ~RFCOMM_V24_IC;
1043 if (clear & TIOCM_CD)
1044 v24_sig &= ~RFCOMM_V24_DV;
1046 rfcomm_dlc_set_modem_status(dlc, v24_sig);
1051 /* ---- TTY structure ---- */
1053 static const struct tty_operations rfcomm_ops = {
1054 .open = rfcomm_tty_open,
1055 .close = rfcomm_tty_close,
1056 .write = rfcomm_tty_write,
1057 .write_room = rfcomm_tty_write_room,
1058 .chars_in_buffer = rfcomm_tty_chars_in_buffer,
1059 .flush_buffer = rfcomm_tty_flush_buffer,
1060 .ioctl = rfcomm_tty_ioctl,
1061 .throttle = rfcomm_tty_throttle,
1062 .unthrottle = rfcomm_tty_unthrottle,
1063 .set_termios = rfcomm_tty_set_termios,
1064 .send_xchar = rfcomm_tty_send_xchar,
1065 .hangup = rfcomm_tty_hangup,
1066 .wait_until_sent = rfcomm_tty_wait_until_sent,
1067 .read_proc = rfcomm_tty_read_proc,
1068 .tiocmget = rfcomm_tty_tiocmget,
1069 .tiocmset = rfcomm_tty_tiocmset,
1072 int rfcomm_init_ttys(void)
1074 rfcomm_tty_driver = alloc_tty_driver(RFCOMM_TTY_PORTS);
1075 if (!rfcomm_tty_driver)
1078 rfcomm_tty_driver->owner = THIS_MODULE;
1079 rfcomm_tty_driver->driver_name = "rfcomm";
1080 rfcomm_tty_driver->name = "rfcomm";
1081 rfcomm_tty_driver->major = RFCOMM_TTY_MAJOR;
1082 rfcomm_tty_driver->minor_start = RFCOMM_TTY_MINOR;
1083 rfcomm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1084 rfcomm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1085 rfcomm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1086 rfcomm_tty_driver->init_termios = tty_std_termios;
1087 rfcomm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1088 tty_set_operations(rfcomm_tty_driver, &rfcomm_ops);
1090 if (tty_register_driver(rfcomm_tty_driver)) {
1091 BT_ERR("Can't register RFCOMM TTY driver");
1092 put_tty_driver(rfcomm_tty_driver);
1096 BT_INFO("RFCOMM TTY layer initialized");
1101 void rfcomm_cleanup_ttys(void)
1103 tty_unregister_driver(rfcomm_tty_driver);
1104 put_tty_driver(rfcomm_tty_driver);