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);
99 /* Detach DLC if it's owned by this dev */
100 if (dlc->owner == dev)
102 rfcomm_dlc_unlock(dlc);
106 tty_unregister_device(rfcomm_tty_driver, dev->id);
108 /* Refcount should only hit zero when called from rfcomm_dev_del()
109 which will have taken us off the list. Everything else are
111 BUG_ON(!list_empty(&dev->list));
115 /* It's safe to call module_put() here because socket still
116 holds reference to this module. */
117 module_put(THIS_MODULE);
120 static inline void rfcomm_dev_hold(struct rfcomm_dev *dev)
122 atomic_inc(&dev->refcnt);
125 static inline void rfcomm_dev_put(struct rfcomm_dev *dev)
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()
134 if (atomic_dec_and_test(&dev->refcnt))
135 rfcomm_dev_destruct(dev);
138 static struct rfcomm_dev *__rfcomm_dev_get(int id)
140 struct rfcomm_dev *dev;
143 list_for_each(p, &rfcomm_dev_list) {
144 dev = list_entry(p, struct rfcomm_dev, list);
152 static inline struct rfcomm_dev *rfcomm_dev_get(int id)
154 struct rfcomm_dev *dev;
156 read_lock(&rfcomm_dev_lock);
158 dev = __rfcomm_dev_get(id);
160 rfcomm_dev_hold(dev);
162 read_unlock(&rfcomm_dev_lock);
167 static struct device *rfcomm_get_device(struct rfcomm_dev *dev)
169 struct hci_dev *hdev;
170 struct hci_conn *conn;
172 hdev = hci_get_route(&dev->dst, &dev->src);
176 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &dev->dst);
180 return conn ? &conn->dev : NULL;
183 static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
185 struct rfcomm_dev *dev;
186 struct list_head *head = &rfcomm_dev_list, *p;
189 BT_DBG("id %d channel %d", req->dev_id, req->channel);
191 dev = kzalloc(sizeof(struct rfcomm_dev), GFP_KERNEL);
195 write_lock_bh(&rfcomm_dev_lock);
197 if (req->dev_id < 0) {
200 list_for_each(p, &rfcomm_dev_list) {
201 if (list_entry(p, struct rfcomm_dev, list)->id != dev->id)
208 dev->id = req->dev_id;
210 list_for_each(p, &rfcomm_dev_list) {
211 struct rfcomm_dev *entry = list_entry(p, struct rfcomm_dev, list);
213 if (entry->id == dev->id) {
218 if (entry->id > dev->id - 1)
225 if ((dev->id < 0) || (dev->id > RFCOMM_MAX_DEV - 1)) {
230 sprintf(dev->name, "rfcomm%d", dev->id);
232 list_add(&dev->list, head);
233 atomic_set(&dev->refcnt, 1);
235 bacpy(&dev->src, &req->src);
236 bacpy(&dev->dst, &req->dst);
237 dev->channel = req->channel;
239 dev->flags = req->flags &
240 ((1 << RFCOMM_RELEASE_ONHUP) | (1 << RFCOMM_REUSE_DLC));
242 init_waitqueue_head(&dev->wait);
243 tasklet_init(&dev->wakeup_task, rfcomm_tty_wakeup, (unsigned long) dev);
245 rfcomm_dlc_lock(dlc);
246 dlc->data_ready = rfcomm_dev_data_ready;
247 dlc->state_change = rfcomm_dev_state_change;
248 dlc->modem_status = rfcomm_dev_modem_status;
252 rfcomm_dlc_unlock(dlc);
254 /* It's safe to call __module_get() here because socket already
255 holds reference to this module. */
256 __module_get(THIS_MODULE);
259 write_unlock_bh(&rfcomm_dev_lock);
266 dev->tty_dev = tty_register_device(rfcomm_tty_driver, dev->id, NULL);
271 static void rfcomm_dev_del(struct rfcomm_dev *dev)
273 BT_DBG("dev %p", dev);
275 write_lock_bh(&rfcomm_dev_lock);
276 list_del_init(&dev->list);
277 write_unlock_bh(&rfcomm_dev_lock);
282 /* ---- Send buffer ---- */
283 static inline unsigned int rfcomm_room(struct rfcomm_dlc *dlc)
285 /* We can't let it be zero, because we don't get a callback
286 when tx_credits becomes nonzero, hence we'd never wake up */
287 return dlc->mtu * (dlc->tx_credits?:1);
290 static void rfcomm_wfree(struct sk_buff *skb)
292 struct rfcomm_dev *dev = (void *) skb->sk;
293 atomic_sub(skb->truesize, &dev->wmem_alloc);
294 if (test_bit(RFCOMM_TTY_ATTACHED, &dev->flags))
295 tasklet_schedule(&dev->wakeup_task);
299 static inline void rfcomm_set_owner_w(struct sk_buff *skb, struct rfcomm_dev *dev)
301 rfcomm_dev_hold(dev);
302 atomic_add(skb->truesize, &dev->wmem_alloc);
303 skb->sk = (void *) dev;
304 skb->destructor = rfcomm_wfree;
307 static struct sk_buff *rfcomm_wmalloc(struct rfcomm_dev *dev, unsigned long size, gfp_t priority)
309 if (atomic_read(&dev->wmem_alloc) < rfcomm_room(dev->dlc)) {
310 struct sk_buff *skb = alloc_skb(size, priority);
312 rfcomm_set_owner_w(skb, dev);
319 /* ---- Device IOCTLs ---- */
321 #define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP))
323 static int rfcomm_create_dev(struct sock *sk, void __user *arg)
325 struct rfcomm_dev_req req;
326 struct rfcomm_dlc *dlc;
329 if (copy_from_user(&req, arg, sizeof(req)))
332 BT_DBG("sk %p dev_id %id flags 0x%x", sk, req.dev_id, req.flags);
334 if (req.flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN))
337 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
338 /* Socket must be connected */
339 if (sk->sk_state != BT_CONNECTED)
342 dlc = rfcomm_pi(sk)->dlc;
343 rfcomm_dlc_hold(dlc);
345 dlc = rfcomm_dlc_alloc(GFP_KERNEL);
350 id = rfcomm_dev_add(&req, dlc);
356 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
357 /* DLC is now used by device.
358 * Socket must be disconnected */
359 sk->sk_state = BT_CLOSED;
365 static int rfcomm_release_dev(void __user *arg)
367 struct rfcomm_dev_req req;
368 struct rfcomm_dev *dev;
370 if (copy_from_user(&req, arg, sizeof(req)))
373 BT_DBG("dev_id %id flags 0x%x", req.dev_id, req.flags);
375 if (!(dev = rfcomm_dev_get(req.dev_id)))
378 if (dev->flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN)) {
383 if (req.flags & (1 << RFCOMM_HANGUP_NOW))
384 rfcomm_dlc_close(dev->dlc, 0);
386 /* Shut down TTY synchronously before freeing rfcomm_dev */
388 tty_vhangup(dev->tty);
395 static int rfcomm_get_dev_list(void __user *arg)
397 struct rfcomm_dev_list_req *dl;
398 struct rfcomm_dev_info *di;
400 int n = 0, size, err;
405 if (get_user(dev_num, (u16 __user *) arg))
408 if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di))
411 size = sizeof(*dl) + dev_num * sizeof(*di);
413 if (!(dl = kmalloc(size, GFP_KERNEL)))
418 read_lock_bh(&rfcomm_dev_lock);
420 list_for_each(p, &rfcomm_dev_list) {
421 struct rfcomm_dev *dev = list_entry(p, struct rfcomm_dev, list);
422 (di + n)->id = dev->id;
423 (di + n)->flags = dev->flags;
424 (di + n)->state = dev->dlc->state;
425 (di + n)->channel = dev->channel;
426 bacpy(&(di + n)->src, &dev->src);
427 bacpy(&(di + n)->dst, &dev->dst);
432 read_unlock_bh(&rfcomm_dev_lock);
435 size = sizeof(*dl) + n * sizeof(*di);
437 err = copy_to_user(arg, dl, size);
440 return err ? -EFAULT : 0;
443 static int rfcomm_get_dev_info(void __user *arg)
445 struct rfcomm_dev *dev;
446 struct rfcomm_dev_info di;
451 if (copy_from_user(&di, arg, sizeof(di)))
454 if (!(dev = rfcomm_dev_get(di.id)))
457 di.flags = dev->flags;
458 di.channel = dev->channel;
459 di.state = dev->dlc->state;
460 bacpy(&di.src, &dev->src);
461 bacpy(&di.dst, &dev->dst);
463 if (copy_to_user(arg, &di, sizeof(di)))
470 int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
472 BT_DBG("cmd %d arg %p", cmd, arg);
475 case RFCOMMCREATEDEV:
476 return rfcomm_create_dev(sk, arg);
478 case RFCOMMRELEASEDEV:
479 return rfcomm_release_dev(arg);
481 case RFCOMMGETDEVLIST:
482 return rfcomm_get_dev_list(arg);
484 case RFCOMMGETDEVINFO:
485 return rfcomm_get_dev_info(arg);
491 /* ---- DLC callbacks ---- */
492 static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb)
494 struct rfcomm_dev *dev = dlc->owner;
495 struct tty_struct *tty;
497 if (!dev || !(tty = dev->tty)) {
502 BT_DBG("dlc %p tty %p len %d", dlc, tty, skb->len);
504 tty_insert_flip_string(tty, skb->data, skb->len);
505 tty_flip_buffer_push(tty);
510 static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
512 struct rfcomm_dev *dev = dlc->owner;
516 BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
519 wake_up_interruptible(&dev->wait);
521 if (dlc->state == BT_CLOSED) {
523 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
524 if (rfcomm_dev_get(dev->id) == NULL)
528 /* We have to drop DLC lock here, otherwise
529 rfcomm_dev_put() will dead lock if it's
530 the last reference. */
531 rfcomm_dlc_unlock(dlc);
533 rfcomm_dlc_lock(dlc);
536 tty_hangup(dev->tty);
540 static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig)
542 struct rfcomm_dev *dev = dlc->owner;
546 BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc, dev, v24_sig);
548 if ((dev->modem_status & TIOCM_CD) && !(v24_sig & RFCOMM_V24_DV)) {
549 if (dev->tty && !C_CLOCAL(dev->tty))
550 tty_hangup(dev->tty);
554 ((v24_sig & RFCOMM_V24_RTC) ? (TIOCM_DSR | TIOCM_DTR) : 0) |
555 ((v24_sig & RFCOMM_V24_RTR) ? (TIOCM_RTS | TIOCM_CTS) : 0) |
556 ((v24_sig & RFCOMM_V24_IC) ? TIOCM_RI : 0) |
557 ((v24_sig & RFCOMM_V24_DV) ? TIOCM_CD : 0);
560 /* ---- TTY functions ---- */
561 static void rfcomm_tty_wakeup(unsigned long arg)
563 struct rfcomm_dev *dev = (void *) arg;
564 struct tty_struct *tty = dev->tty;
568 BT_DBG("dev %p tty %p", dev, tty);
570 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && tty->ldisc.write_wakeup)
571 (tty->ldisc.write_wakeup)(tty);
573 wake_up_interruptible(&tty->write_wait);
574 #ifdef SERIAL_HAVE_POLL_WAIT
575 wake_up_interruptible(&tty->poll_wait);
579 static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
581 DECLARE_WAITQUEUE(wait, current);
582 struct rfcomm_dev *dev;
583 struct rfcomm_dlc *dlc;
588 BT_DBG("tty %p id %d", tty, id);
590 /* We don't leak this refcount. For reasons which are not entirely
591 clear, the TTY layer will call our ->close() method even if the
592 open fails. We decrease the refcount there, and decreasing it
593 here too would cause breakage. */
594 dev = rfcomm_dev_get(id);
598 BT_DBG("dev %p dst %s channel %d opened %d", dev, batostr(&dev->dst), dev->channel, dev->opened);
600 if (dev->opened++ != 0)
605 /* Attach TTY and open DLC */
607 rfcomm_dlc_lock(dlc);
608 tty->driver_data = dev;
610 rfcomm_dlc_unlock(dlc);
611 set_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
613 err = rfcomm_dlc_open(dlc, &dev->src, &dev->dst, dev->channel);
617 /* Wait for DLC to connect */
618 add_wait_queue(&dev->wait, &wait);
620 set_current_state(TASK_INTERRUPTIBLE);
622 if (dlc->state == BT_CLOSED) {
627 if (dlc->state == BT_CONNECTED)
630 if (signal_pending(current)) {
637 set_current_state(TASK_RUNNING);
638 remove_wait_queue(&dev->wait, &wait);
641 device_move(dev->tty_dev, rfcomm_get_device(dev));
646 static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
648 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
652 BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc, dev->opened);
654 if (--dev->opened == 0) {
655 device_move(dev->tty_dev, NULL);
657 /* Close DLC and dettach TTY */
658 rfcomm_dlc_close(dev->dlc, 0);
660 clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
661 tasklet_kill(&dev->wakeup_task);
663 rfcomm_dlc_lock(dev->dlc);
664 tty->driver_data = NULL;
666 rfcomm_dlc_unlock(dev->dlc);
672 static int rfcomm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
674 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
675 struct rfcomm_dlc *dlc = dev->dlc;
677 int err = 0, sent = 0, size;
679 BT_DBG("tty %p count %d", tty, count);
682 size = min_t(uint, count, dlc->mtu);
684 skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, GFP_ATOMIC);
689 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
691 memcpy(skb_put(skb, size), buf + sent, size);
693 if ((err = rfcomm_dlc_send(dlc, skb)) < 0) {
702 return sent ? sent : err;
705 static int rfcomm_tty_write_room(struct tty_struct *tty)
707 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
710 BT_DBG("tty %p", tty);
712 if (!dev || !dev->dlc)
715 room = rfcomm_room(dev->dlc) - atomic_read(&dev->wmem_alloc);
722 static int rfcomm_tty_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd, unsigned long arg)
724 BT_DBG("tty %p cmd 0x%02x", tty, cmd);
728 BT_DBG("TCGETS is not supported");
732 BT_DBG("TCSETS is not supported");
736 BT_DBG("TIOCMIWAIT");
740 BT_DBG("TIOCGICOUNT");
744 BT_ERR("TIOCGSERIAL is not supported");
748 BT_ERR("TIOCSSERIAL is not supported");
752 BT_ERR("TIOCSERGSTRUCT is not supported");
756 BT_ERR("TIOCSERGETLSR is not supported");
760 BT_ERR("TIOCSERCONFIG is not supported");
764 return -ENOIOCTLCMD; /* ioctls which we must ignore */
771 static void rfcomm_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
773 struct ktermios *new = tty->termios;
774 int old_baud_rate = tty_termios_baud_rate(old);
775 int new_baud_rate = tty_termios_baud_rate(new);
777 u8 baud, data_bits, stop_bits, parity, x_on, x_off;
780 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
782 BT_DBG("tty %p termios %p", tty, old);
784 if (!dev || !dev->dlc || !dev->dlc->session)
787 /* Handle turning off CRTSCTS */
788 if ((old->c_cflag & CRTSCTS) && !(new->c_cflag & CRTSCTS))
789 BT_DBG("Turning off CRTSCTS unsupported");
791 /* Parity on/off and when on, odd/even */
792 if (((old->c_cflag & PARENB) != (new->c_cflag & PARENB)) ||
793 ((old->c_cflag & PARODD) != (new->c_cflag & PARODD)) ) {
794 changes |= RFCOMM_RPN_PM_PARITY;
795 BT_DBG("Parity change detected.");
798 /* Mark and space parity are not supported! */
799 if (new->c_cflag & PARENB) {
800 if (new->c_cflag & PARODD) {
801 BT_DBG("Parity is ODD");
802 parity = RFCOMM_RPN_PARITY_ODD;
804 BT_DBG("Parity is EVEN");
805 parity = RFCOMM_RPN_PARITY_EVEN;
808 BT_DBG("Parity is OFF");
809 parity = RFCOMM_RPN_PARITY_NONE;
812 /* Setting the x_on / x_off characters */
813 if (old->c_cc[VSTOP] != new->c_cc[VSTOP]) {
814 BT_DBG("XOFF custom");
815 x_on = new->c_cc[VSTOP];
816 changes |= RFCOMM_RPN_PM_XON;
818 BT_DBG("XOFF default");
819 x_on = RFCOMM_RPN_XON_CHAR;
822 if (old->c_cc[VSTART] != new->c_cc[VSTART]) {
823 BT_DBG("XON custom");
824 x_off = new->c_cc[VSTART];
825 changes |= RFCOMM_RPN_PM_XOFF;
827 BT_DBG("XON default");
828 x_off = RFCOMM_RPN_XOFF_CHAR;
831 /* Handle setting of stop bits */
832 if ((old->c_cflag & CSTOPB) != (new->c_cflag & CSTOPB))
833 changes |= RFCOMM_RPN_PM_STOP;
835 /* POSIX does not support 1.5 stop bits and RFCOMM does not
836 * support 2 stop bits. So a request for 2 stop bits gets
837 * translated to 1.5 stop bits */
838 if (new->c_cflag & CSTOPB) {
839 stop_bits = RFCOMM_RPN_STOP_15;
841 stop_bits = RFCOMM_RPN_STOP_1;
844 /* Handle number of data bits [5-8] */
845 if ((old->c_cflag & CSIZE) != (new->c_cflag & CSIZE))
846 changes |= RFCOMM_RPN_PM_DATA;
848 switch (new->c_cflag & CSIZE) {
850 data_bits = RFCOMM_RPN_DATA_5;
853 data_bits = RFCOMM_RPN_DATA_6;
856 data_bits = RFCOMM_RPN_DATA_7;
859 data_bits = RFCOMM_RPN_DATA_8;
862 data_bits = RFCOMM_RPN_DATA_8;
866 /* Handle baudrate settings */
867 if (old_baud_rate != new_baud_rate)
868 changes |= RFCOMM_RPN_PM_BITRATE;
870 switch (new_baud_rate) {
872 baud = RFCOMM_RPN_BR_2400;
875 baud = RFCOMM_RPN_BR_4800;
878 baud = RFCOMM_RPN_BR_7200;
881 baud = RFCOMM_RPN_BR_9600;
884 baud = RFCOMM_RPN_BR_19200;
887 baud = RFCOMM_RPN_BR_38400;
890 baud = RFCOMM_RPN_BR_57600;
893 baud = RFCOMM_RPN_BR_115200;
896 baud = RFCOMM_RPN_BR_230400;
899 /* 9600 is standard accordinag to the RFCOMM specification */
900 baud = RFCOMM_RPN_BR_9600;
906 rfcomm_send_rpn(dev->dlc->session, 1, dev->dlc->dlci, baud,
907 data_bits, stop_bits, parity,
908 RFCOMM_RPN_FLOW_NONE, x_on, x_off, changes);
913 static void rfcomm_tty_throttle(struct tty_struct *tty)
915 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
917 BT_DBG("tty %p dev %p", tty, dev);
919 rfcomm_dlc_throttle(dev->dlc);
922 static void rfcomm_tty_unthrottle(struct tty_struct *tty)
924 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
926 BT_DBG("tty %p dev %p", tty, dev);
928 rfcomm_dlc_unthrottle(dev->dlc);
931 static int rfcomm_tty_chars_in_buffer(struct tty_struct *tty)
933 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
935 BT_DBG("tty %p dev %p", tty, dev);
937 if (!dev || !dev->dlc)
940 if (!skb_queue_empty(&dev->dlc->tx_queue))
941 return dev->dlc->mtu;
946 static void rfcomm_tty_flush_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 skb_queue_purge(&dev->dlc->tx_queue);
957 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && tty->ldisc.write_wakeup)
958 tty->ldisc.write_wakeup(tty);
961 static void rfcomm_tty_send_xchar(struct tty_struct *tty, char ch)
963 BT_DBG("tty %p ch %c", tty, ch);
966 static void rfcomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
968 BT_DBG("tty %p timeout %d", tty, timeout);
971 static void rfcomm_tty_hangup(struct tty_struct *tty)
973 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
975 BT_DBG("tty %p dev %p", tty, dev);
980 rfcomm_tty_flush_buffer(tty);
982 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
983 if (rfcomm_dev_get(dev->id) == NULL)
990 static int rfcomm_tty_read_proc(char *buf, char **start, off_t offset, int len, int *eof, void *unused)
995 static int rfcomm_tty_tiocmget(struct tty_struct *tty, struct file *filp)
997 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
999 BT_DBG("tty %p dev %p", tty, dev);
1001 return dev->modem_status;
1004 static int rfcomm_tty_tiocmset(struct tty_struct *tty, struct file *filp, unsigned int set, unsigned int clear)
1006 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1007 struct rfcomm_dlc *dlc = dev->dlc;
1010 BT_DBG("tty %p dev %p set 0x%02x clear 0x%02x", tty, dev, set, clear);
1012 rfcomm_dlc_get_modem_status(dlc, &v24_sig);
1014 if (set & TIOCM_DSR || set & TIOCM_DTR)
1015 v24_sig |= RFCOMM_V24_RTC;
1016 if (set & TIOCM_RTS || set & TIOCM_CTS)
1017 v24_sig |= RFCOMM_V24_RTR;
1019 v24_sig |= RFCOMM_V24_IC;
1021 v24_sig |= RFCOMM_V24_DV;
1023 if (clear & TIOCM_DSR || clear & TIOCM_DTR)
1024 v24_sig &= ~RFCOMM_V24_RTC;
1025 if (clear & TIOCM_RTS || clear & TIOCM_CTS)
1026 v24_sig &= ~RFCOMM_V24_RTR;
1027 if (clear & TIOCM_RI)
1028 v24_sig &= ~RFCOMM_V24_IC;
1029 if (clear & TIOCM_CD)
1030 v24_sig &= ~RFCOMM_V24_DV;
1032 rfcomm_dlc_set_modem_status(dlc, v24_sig);
1037 /* ---- TTY structure ---- */
1039 static const struct tty_operations rfcomm_ops = {
1040 .open = rfcomm_tty_open,
1041 .close = rfcomm_tty_close,
1042 .write = rfcomm_tty_write,
1043 .write_room = rfcomm_tty_write_room,
1044 .chars_in_buffer = rfcomm_tty_chars_in_buffer,
1045 .flush_buffer = rfcomm_tty_flush_buffer,
1046 .ioctl = rfcomm_tty_ioctl,
1047 .throttle = rfcomm_tty_throttle,
1048 .unthrottle = rfcomm_tty_unthrottle,
1049 .set_termios = rfcomm_tty_set_termios,
1050 .send_xchar = rfcomm_tty_send_xchar,
1051 .hangup = rfcomm_tty_hangup,
1052 .wait_until_sent = rfcomm_tty_wait_until_sent,
1053 .read_proc = rfcomm_tty_read_proc,
1054 .tiocmget = rfcomm_tty_tiocmget,
1055 .tiocmset = rfcomm_tty_tiocmset,
1058 int rfcomm_init_ttys(void)
1060 rfcomm_tty_driver = alloc_tty_driver(RFCOMM_TTY_PORTS);
1061 if (!rfcomm_tty_driver)
1064 rfcomm_tty_driver->owner = THIS_MODULE;
1065 rfcomm_tty_driver->driver_name = "rfcomm";
1066 rfcomm_tty_driver->name = "rfcomm";
1067 rfcomm_tty_driver->major = RFCOMM_TTY_MAJOR;
1068 rfcomm_tty_driver->minor_start = RFCOMM_TTY_MINOR;
1069 rfcomm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1070 rfcomm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1071 rfcomm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1072 rfcomm_tty_driver->init_termios = tty_std_termios;
1073 rfcomm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1074 tty_set_operations(rfcomm_tty_driver, &rfcomm_ops);
1076 if (tty_register_driver(rfcomm_tty_driver)) {
1077 BT_ERR("Can't register RFCOMM TTY driver");
1078 put_tty_driver(rfcomm_tty_driver);
1082 BT_INFO("RFCOMM TTY layer initialized");
1087 void rfcomm_cleanup_ttys(void)
1089 tty_unregister_driver(rfcomm_tty_driver);
1090 put_tty_driver(rfcomm_tty_driver);