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 err = PTR_ERR(dev->tty_dev);
279 list_del(&dev->list);
287 static void rfcomm_dev_del(struct rfcomm_dev *dev)
289 BT_DBG("dev %p", dev);
291 set_bit(RFCOMM_TTY_RELEASED, &dev->flags);
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);
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);
325 rfcomm_set_owner_w(skb, dev);
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;
342 if (copy_from_user(&req, arg, sizeof(req)))
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))
350 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
351 /* Socket must be connected */
352 if (sk->sk_state != BT_CONNECTED)
355 dlc = rfcomm_pi(sk)->dlc;
356 rfcomm_dlc_hold(dlc);
358 dlc = rfcomm_dlc_alloc(GFP_KERNEL);
363 id = rfcomm_dev_add(&req, dlc);
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;
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)))
386 BT_DBG("dev_id %d flags 0x%x", req.dev_id, req.flags);
388 if (!(dev = rfcomm_dev_get(req.dev_id)))
391 if (dev->flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN)) {
396 if (req.flags & (1 << RFCOMM_HANGUP_NOW))
397 rfcomm_dlc_close(dev->dlc, 0);
399 /* Shut down TTY synchronously before freeing rfcomm_dev */
401 tty_vhangup(dev->tty);
408 static int rfcomm_get_dev_list(void __user *arg)
410 struct rfcomm_dev_list_req *dl;
411 struct rfcomm_dev_info *di;
413 int n = 0, size, err;
418 if (get_user(dev_num, (u16 __user *) arg))
421 if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di))
424 size = sizeof(*dl) + dev_num * sizeof(*di);
426 if (!(dl = kmalloc(size, GFP_KERNEL)))
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))
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);
447 read_unlock_bh(&rfcomm_dev_lock);
450 size = sizeof(*dl) + n * sizeof(*di);
452 err = copy_to_user(arg, dl, size);
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;
466 if (copy_from_user(&di, arg, sizeof(di)))
469 if (!(dev = rfcomm_dev_get(di.id)))
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)))
485 int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
487 BT_DBG("cmd %d arg %p", cmd, arg);
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);
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)) {
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);
525 static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
527 struct rfcomm_dev *dev = dlc->owner;
531 BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
534 wake_up_interruptible(&dev->wait);
536 if (dlc->state == BT_CLOSED) {
538 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
539 if (rfcomm_dev_get(dev->id) == NULL)
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);
548 rfcomm_dlc_lock(dlc);
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;
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);
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;
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);
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;
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);
613 BT_DBG("dev %p dst %s channel %d opened %d", dev, batostr(&dev->dst), dev->channel, dev->opened);
615 if (dev->opened++ != 0)
620 /* Attach TTY and open DLC */
622 rfcomm_dlc_lock(dlc);
623 tty->driver_data = dev;
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);
632 /* Wait for DLC to connect */
633 add_wait_queue(&dev->wait, &wait);
635 set_current_state(TASK_INTERRUPTIBLE);
637 if (dlc->state == BT_CLOSED) {
642 if (dlc->state == BT_CONNECTED)
645 if (signal_pending(current)) {
652 set_current_state(TASK_RUNNING);
653 remove_wait_queue(&dev->wait, &wait);
656 device_move(dev->tty_dev, rfcomm_get_device(dev));
661 static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
663 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
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;
681 rfcomm_dlc_unlock(dev->dlc);
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;
692 int err = 0, sent = 0, size;
694 BT_DBG("tty %p count %d", tty, count);
697 size = min_t(uint, count, dlc->mtu);
699 skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, GFP_ATOMIC);
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) {
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;
725 BT_DBG("tty %p", tty);
727 if (!dev || !dev->dlc)
730 room = rfcomm_room(dev->dlc) - atomic_read(&dev->wmem_alloc);
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);
743 BT_DBG("TCGETS is not supported");
747 BT_DBG("TCSETS is not supported");
751 BT_DBG("TIOCMIWAIT");
755 BT_DBG("TIOCGICOUNT");
759 BT_ERR("TIOCGSERIAL is not supported");
763 BT_ERR("TIOCSSERIAL is not supported");
767 BT_ERR("TIOCSERGSTRUCT is not supported");
771 BT_ERR("TIOCSERGETLSR is not supported");
775 BT_ERR("TIOCSERCONFIG is not supported");
779 return -ENOIOCTLCMD; /* ioctls which we must ignore */
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;
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)
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;
819 BT_DBG("Parity is EVEN");
820 parity = RFCOMM_RPN_PARITY_EVEN;
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;
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;
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;
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) {
865 data_bits = RFCOMM_RPN_DATA_5;
868 data_bits = RFCOMM_RPN_DATA_6;
871 data_bits = RFCOMM_RPN_DATA_7;
874 data_bits = RFCOMM_RPN_DATA_8;
877 data_bits = RFCOMM_RPN_DATA_8;
881 /* Handle baudrate settings */
882 if (old_baud_rate != new_baud_rate)
883 changes |= RFCOMM_RPN_PM_BITRATE;
885 switch (new_baud_rate) {
887 baud = RFCOMM_RPN_BR_2400;
890 baud = RFCOMM_RPN_BR_4800;
893 baud = RFCOMM_RPN_BR_7200;
896 baud = RFCOMM_RPN_BR_9600;
899 baud = RFCOMM_RPN_BR_19200;
902 baud = RFCOMM_RPN_BR_38400;
905 baud = RFCOMM_RPN_BR_57600;
908 baud = RFCOMM_RPN_BR_115200;
911 baud = RFCOMM_RPN_BR_230400;
914 /* 9600 is standard accordinag to the RFCOMM specification */
915 baud = RFCOMM_RPN_BR_9600;
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);
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)
955 if (!skb_queue_empty(&dev->dlc->tx_queue))
956 return dev->dlc->mtu;
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)
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);
995 rfcomm_tty_flush_buffer(tty);
997 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
998 if (rfcomm_dev_get(dev->id) == NULL)
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)
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;
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;
1034 v24_sig |= RFCOMM_V24_IC;
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);
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)
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);
1097 BT_INFO("RFCOMM TTY layer initialized");
1102 void rfcomm_cleanup_ttys(void)
1104 tty_unregister_driver(rfcomm_tty_driver);
1105 put_tty_driver(rfcomm_tty_driver);