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.
28 #include <linux/module.h>
30 #include <linux/tty.h>
31 #include <linux/tty_driver.h>
32 #include <linux/tty_flip.h>
34 #include <net/bluetooth/bluetooth.h>
35 #include <net/bluetooth/hci_core.h>
36 #include <net/bluetooth/rfcomm.h>
38 #define RFCOMM_TTY_MAGIC 0x6d02 /* magic number for rfcomm struct */
39 #define RFCOMM_TTY_PORTS RFCOMM_MAX_DEV /* whole lotta rfcomm devices */
40 #define RFCOMM_TTY_MAJOR 216 /* device node major id of the usb/bluetooth.c driver */
41 #define RFCOMM_TTY_MINOR 0
43 static struct tty_driver *rfcomm_tty_driver;
47 struct list_head list;
60 struct rfcomm_dlc *dlc;
62 struct device *tty_dev;
66 struct sk_buff_head pending;
69 static LIST_HEAD(rfcomm_dev_list);
70 static DEFINE_SPINLOCK(rfcomm_dev_lock);
72 static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb);
73 static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err);
74 static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig);
76 /* ---- Device functions ---- */
78 static void rfcomm_dev_destruct(struct tty_port *port)
80 struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
81 struct rfcomm_dlc *dlc = dev->dlc;
83 BT_DBG("dev %p dlc %p", dev, dlc);
85 spin_lock(&rfcomm_dev_lock);
87 spin_unlock(&rfcomm_dev_lock);
90 /* Detach DLC if it's owned by this dev */
91 if (dlc->owner == dev)
93 rfcomm_dlc_unlock(dlc);
97 tty_unregister_device(rfcomm_tty_driver, dev->id);
101 /* It's safe to call module_put() here because socket still
102 holds reference to this module. */
103 module_put(THIS_MODULE);
106 /* device-specific initialization: open the dlc */
107 static int rfcomm_dev_activate(struct tty_port *port, struct tty_struct *tty)
109 struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
111 return rfcomm_dlc_open(dev->dlc, &dev->src, &dev->dst, dev->channel);
114 /* we block the open until the dlc->state becomes BT_CONNECTED */
115 static int rfcomm_dev_carrier_raised(struct tty_port *port)
117 struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
119 return (dev->dlc->state == BT_CONNECTED);
122 /* device-specific cleanup: close the dlc */
123 static void rfcomm_dev_shutdown(struct tty_port *port)
125 struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
127 if (dev->tty_dev->parent)
128 device_move(dev->tty_dev, NULL, DPM_ORDER_DEV_LAST);
131 rfcomm_dlc_close(dev->dlc, 0);
134 static const struct tty_port_operations rfcomm_port_ops = {
135 .destruct = rfcomm_dev_destruct,
136 .activate = rfcomm_dev_activate,
137 .shutdown = rfcomm_dev_shutdown,
138 .carrier_raised = rfcomm_dev_carrier_raised,
141 static struct rfcomm_dev *__rfcomm_dev_get(int id)
143 struct rfcomm_dev *dev;
145 list_for_each_entry(dev, &rfcomm_dev_list, list)
152 static struct rfcomm_dev *rfcomm_dev_get(int id)
154 struct rfcomm_dev *dev;
156 spin_lock(&rfcomm_dev_lock);
158 dev = __rfcomm_dev_get(id);
161 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
164 tty_port_get(&dev->port);
167 spin_unlock(&rfcomm_dev_lock);
172 static struct device *rfcomm_get_device(struct rfcomm_dev *dev)
174 struct hci_dev *hdev;
175 struct hci_conn *conn;
177 hdev = hci_get_route(&dev->dst, &dev->src);
181 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &dev->dst);
185 return conn ? &conn->dev : NULL;
188 static ssize_t show_address(struct device *tty_dev, struct device_attribute *attr, char *buf)
190 struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
191 return sprintf(buf, "%pMR\n", &dev->dst);
194 static ssize_t show_channel(struct device *tty_dev, struct device_attribute *attr, char *buf)
196 struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
197 return sprintf(buf, "%d\n", dev->channel);
200 static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
201 static DEVICE_ATTR(channel, S_IRUGO, show_channel, NULL);
203 static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
205 struct rfcomm_dev *dev, *entry;
206 struct list_head *head = &rfcomm_dev_list;
209 BT_DBG("id %d channel %d", req->dev_id, req->channel);
211 dev = kzalloc(sizeof(struct rfcomm_dev), GFP_KERNEL);
215 spin_lock(&rfcomm_dev_lock);
217 if (req->dev_id < 0) {
220 list_for_each_entry(entry, &rfcomm_dev_list, list) {
221 if (entry->id != dev->id)
228 dev->id = req->dev_id;
230 list_for_each_entry(entry, &rfcomm_dev_list, list) {
231 if (entry->id == dev->id) {
236 if (entry->id > dev->id - 1)
243 if ((dev->id < 0) || (dev->id > RFCOMM_MAX_DEV - 1)) {
248 sprintf(dev->name, "rfcomm%d", dev->id);
250 list_add(&dev->list, head);
252 bacpy(&dev->src, &req->src);
253 bacpy(&dev->dst, &req->dst);
254 dev->channel = req->channel;
256 dev->flags = req->flags &
257 ((1 << RFCOMM_RELEASE_ONHUP) | (1 << RFCOMM_REUSE_DLC));
259 tty_port_init(&dev->port);
260 dev->port.ops = &rfcomm_port_ops;
262 skb_queue_head_init(&dev->pending);
264 rfcomm_dlc_lock(dlc);
266 if (req->flags & (1 << RFCOMM_REUSE_DLC)) {
267 struct sock *sk = dlc->owner;
272 rfcomm_dlc_throttle(dlc);
274 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
276 skb_queue_tail(&dev->pending, skb);
277 atomic_sub(skb->len, &sk->sk_rmem_alloc);
281 dlc->data_ready = rfcomm_dev_data_ready;
282 dlc->state_change = rfcomm_dev_state_change;
283 dlc->modem_status = rfcomm_dev_modem_status;
288 rfcomm_dev_modem_status(dlc, dlc->remote_v24_sig);
290 rfcomm_dlc_unlock(dlc);
292 /* It's safe to call __module_get() here because socket already
293 holds reference to this module. */
294 __module_get(THIS_MODULE);
297 spin_unlock(&rfcomm_dev_lock);
302 dev->tty_dev = tty_port_register_device(&dev->port, rfcomm_tty_driver,
304 if (IS_ERR(dev->tty_dev)) {
305 err = PTR_ERR(dev->tty_dev);
306 spin_lock(&rfcomm_dev_lock);
307 list_del(&dev->list);
308 spin_unlock(&rfcomm_dev_lock);
312 dev_set_drvdata(dev->tty_dev, dev);
314 if (device_create_file(dev->tty_dev, &dev_attr_address) < 0)
315 BT_ERR("Failed to create address attribute");
317 if (device_create_file(dev->tty_dev, &dev_attr_channel) < 0)
318 BT_ERR("Failed to create channel attribute");
327 /* ---- Send buffer ---- */
328 static inline unsigned int rfcomm_room(struct rfcomm_dlc *dlc)
330 /* We can't let it be zero, because we don't get a callback
331 when tx_credits becomes nonzero, hence we'd never wake up */
332 return dlc->mtu * (dlc->tx_credits?:1);
335 static void rfcomm_wfree(struct sk_buff *skb)
337 struct rfcomm_dev *dev = (void *) skb->sk;
338 atomic_sub(skb->truesize, &dev->wmem_alloc);
339 if (test_bit(RFCOMM_TTY_ATTACHED, &dev->flags))
340 tty_port_tty_wakeup(&dev->port);
341 tty_port_put(&dev->port);
344 static void rfcomm_set_owner_w(struct sk_buff *skb, struct rfcomm_dev *dev)
346 tty_port_get(&dev->port);
347 atomic_add(skb->truesize, &dev->wmem_alloc);
348 skb->sk = (void *) dev;
349 skb->destructor = rfcomm_wfree;
352 static struct sk_buff *rfcomm_wmalloc(struct rfcomm_dev *dev, unsigned long size, gfp_t priority)
354 if (atomic_read(&dev->wmem_alloc) < rfcomm_room(dev->dlc)) {
355 struct sk_buff *skb = alloc_skb(size, priority);
357 rfcomm_set_owner_w(skb, dev);
364 /* ---- Device IOCTLs ---- */
366 #define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP))
368 static int rfcomm_create_dev(struct sock *sk, void __user *arg)
370 struct rfcomm_dev_req req;
371 struct rfcomm_dlc *dlc;
374 if (copy_from_user(&req, arg, sizeof(req)))
377 BT_DBG("sk %p dev_id %d flags 0x%x", sk, req.dev_id, req.flags);
379 if (req.flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN))
382 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
383 /* Socket must be connected */
384 if (sk->sk_state != BT_CONNECTED)
387 dlc = rfcomm_pi(sk)->dlc;
388 rfcomm_dlc_hold(dlc);
390 dlc = rfcomm_dlc_alloc(GFP_KERNEL);
395 id = rfcomm_dev_add(&req, dlc);
401 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
402 /* DLC is now used by device.
403 * Socket must be disconnected */
404 sk->sk_state = BT_CLOSED;
410 static int rfcomm_release_dev(void __user *arg)
412 struct rfcomm_dev_req req;
413 struct rfcomm_dev *dev;
414 struct tty_struct *tty;
416 if (copy_from_user(&req, arg, sizeof(req)))
419 BT_DBG("dev_id %d flags 0x%x", req.dev_id, req.flags);
421 dev = rfcomm_dev_get(req.dev_id);
425 if (dev->flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN)) {
426 tty_port_put(&dev->port);
430 if (req.flags & (1 << RFCOMM_HANGUP_NOW))
431 rfcomm_dlc_close(dev->dlc, 0);
433 /* Shut down TTY synchronously before freeing rfcomm_dev */
434 tty = tty_port_tty_get(&dev->port);
440 if (!test_and_set_bit(RFCOMM_TTY_RELEASED, &dev->flags))
441 tty_port_put(&dev->port);
443 tty_port_put(&dev->port);
447 static int rfcomm_get_dev_list(void __user *arg)
449 struct rfcomm_dev *dev;
450 struct rfcomm_dev_list_req *dl;
451 struct rfcomm_dev_info *di;
452 int n = 0, size, err;
457 if (get_user(dev_num, (u16 __user *) arg))
460 if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di))
463 size = sizeof(*dl) + dev_num * sizeof(*di);
465 dl = kzalloc(size, GFP_KERNEL);
471 spin_lock(&rfcomm_dev_lock);
473 list_for_each_entry(dev, &rfcomm_dev_list, list) {
474 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
476 (di + n)->id = dev->id;
477 (di + n)->flags = dev->flags;
478 (di + n)->state = dev->dlc->state;
479 (di + n)->channel = dev->channel;
480 bacpy(&(di + n)->src, &dev->src);
481 bacpy(&(di + n)->dst, &dev->dst);
486 spin_unlock(&rfcomm_dev_lock);
489 size = sizeof(*dl) + n * sizeof(*di);
491 err = copy_to_user(arg, dl, size);
494 return err ? -EFAULT : 0;
497 static int rfcomm_get_dev_info(void __user *arg)
499 struct rfcomm_dev *dev;
500 struct rfcomm_dev_info di;
505 if (copy_from_user(&di, arg, sizeof(di)))
508 dev = rfcomm_dev_get(di.id);
512 di.flags = dev->flags;
513 di.channel = dev->channel;
514 di.state = dev->dlc->state;
515 bacpy(&di.src, &dev->src);
516 bacpy(&di.dst, &dev->dst);
518 if (copy_to_user(arg, &di, sizeof(di)))
521 tty_port_put(&dev->port);
525 int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
527 BT_DBG("cmd %d arg %p", cmd, arg);
530 case RFCOMMCREATEDEV:
531 return rfcomm_create_dev(sk, arg);
533 case RFCOMMRELEASEDEV:
534 return rfcomm_release_dev(arg);
536 case RFCOMMGETDEVLIST:
537 return rfcomm_get_dev_list(arg);
539 case RFCOMMGETDEVINFO:
540 return rfcomm_get_dev_info(arg);
546 /* ---- DLC callbacks ---- */
547 static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb)
549 struct rfcomm_dev *dev = dlc->owner;
556 if (!skb_queue_empty(&dev->pending)) {
557 skb_queue_tail(&dev->pending, skb);
561 BT_DBG("dlc %p len %d", dlc, skb->len);
563 tty_insert_flip_string(&dev->port, skb->data, skb->len);
564 tty_flip_buffer_push(&dev->port);
569 static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
571 struct rfcomm_dev *dev = dlc->owner;
572 struct tty_struct *tty;
576 BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
579 if (dlc->state == BT_CONNECTED) {
580 device_move(dev->tty_dev, rfcomm_get_device(dev),
581 DPM_ORDER_DEV_AFTER_PARENT);
583 wake_up_interruptible(&dev->port.open_wait);
584 } else if (dlc->state == BT_CLOSED) {
585 tty = tty_port_tty_get(&dev->port);
587 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
588 /* Drop DLC lock here to avoid deadlock
589 * 1. rfcomm_dev_get will take rfcomm_dev_lock
590 * but in rfcomm_dev_add there's lock order:
591 * rfcomm_dev_lock -> dlc lock
592 * 2. tty_port_put will deadlock if it's
595 * FIXME: when we release the lock anything
596 * could happen to dev, even its destruction
598 rfcomm_dlc_unlock(dlc);
599 if (rfcomm_dev_get(dev->id) == NULL) {
600 rfcomm_dlc_lock(dlc);
604 if (!test_and_set_bit(RFCOMM_TTY_RELEASED,
606 tty_port_put(&dev->port);
608 tty_port_put(&dev->port);
609 rfcomm_dlc_lock(dlc);
618 static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig)
620 struct rfcomm_dev *dev = dlc->owner;
624 BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc, dev, v24_sig);
626 if ((dev->modem_status & TIOCM_CD) && !(v24_sig & RFCOMM_V24_DV))
627 tty_port_tty_hangup(&dev->port, true);
630 ((v24_sig & RFCOMM_V24_RTC) ? (TIOCM_DSR | TIOCM_DTR) : 0) |
631 ((v24_sig & RFCOMM_V24_RTR) ? (TIOCM_RTS | TIOCM_CTS) : 0) |
632 ((v24_sig & RFCOMM_V24_IC) ? TIOCM_RI : 0) |
633 ((v24_sig & RFCOMM_V24_DV) ? TIOCM_CD : 0);
636 /* ---- TTY functions ---- */
637 static void rfcomm_tty_copy_pending(struct rfcomm_dev *dev)
642 BT_DBG("dev %p", dev);
644 rfcomm_dlc_lock(dev->dlc);
646 while ((skb = skb_dequeue(&dev->pending))) {
647 inserted += tty_insert_flip_string(&dev->port, skb->data,
652 rfcomm_dlc_unlock(dev->dlc);
655 tty_flip_buffer_push(&dev->port);
658 /* do the reverse of install, clearing the tty fields and releasing the
659 * reference to tty_port
661 static void rfcomm_tty_cleanup(struct tty_struct *tty)
663 struct rfcomm_dev *dev = tty->driver_data;
665 clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
667 rfcomm_dlc_lock(dev->dlc);
668 tty->driver_data = NULL;
669 rfcomm_dlc_unlock(dev->dlc);
672 * purge the dlc->tx_queue to avoid circular dependencies
673 * between dev and dlc
675 skb_queue_purge(&dev->dlc->tx_queue);
677 tty_port_put(&dev->port);
680 /* we acquire the tty_port reference since it's here the tty is first used
681 * by setting the termios. We also populate the driver_data field and install
684 static int rfcomm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
686 struct rfcomm_dev *dev;
687 struct rfcomm_dlc *dlc;
690 dev = rfcomm_dev_get(tty->index);
696 /* Attach TTY and open DLC */
697 rfcomm_dlc_lock(dlc);
698 tty->driver_data = dev;
699 rfcomm_dlc_unlock(dlc);
700 set_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
702 /* install the tty_port */
703 err = tty_port_install(&dev->port, driver, tty);
705 rfcomm_tty_cleanup(tty);
710 static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
712 struct rfcomm_dev *dev = tty->driver_data;
715 BT_DBG("tty %p id %d", tty, tty->index);
717 BT_DBG("dev %p dst %pMR channel %d opened %d", dev, &dev->dst,
718 dev->channel, dev->port.count);
720 err = tty_port_open(&dev->port, tty, filp);
725 * FIXME: rfcomm should use proper flow control for
726 * received data. This hack will be unnecessary and can
727 * be removed when that's implemented
729 rfcomm_tty_copy_pending(dev);
731 rfcomm_dlc_unthrottle(dev->dlc);
736 static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
738 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
740 BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc,
743 tty_port_close(&dev->port, tty, filp);
746 static int rfcomm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
748 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
749 struct rfcomm_dlc *dlc = dev->dlc;
751 int err = 0, sent = 0, size;
753 BT_DBG("tty %p count %d", tty, count);
756 size = min_t(uint, count, dlc->mtu);
758 skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, GFP_ATOMIC);
763 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
765 memcpy(skb_put(skb, size), buf + sent, size);
767 err = rfcomm_dlc_send(dlc, skb);
777 return sent ? sent : err;
780 static int rfcomm_tty_write_room(struct tty_struct *tty)
782 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
785 BT_DBG("tty %p", tty);
787 if (!dev || !dev->dlc)
790 room = rfcomm_room(dev->dlc) - atomic_read(&dev->wmem_alloc);
797 static int rfcomm_tty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
799 BT_DBG("tty %p cmd 0x%02x", tty, cmd);
803 BT_DBG("TCGETS is not supported");
807 BT_DBG("TCSETS is not supported");
811 BT_DBG("TIOCMIWAIT");
815 BT_ERR("TIOCGSERIAL is not supported");
819 BT_ERR("TIOCSSERIAL is not supported");
823 BT_ERR("TIOCSERGSTRUCT is not supported");
827 BT_ERR("TIOCSERGETLSR is not supported");
831 BT_ERR("TIOCSERCONFIG is not supported");
835 return -ENOIOCTLCMD; /* ioctls which we must ignore */
842 static void rfcomm_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
844 struct ktermios *new = &tty->termios;
845 int old_baud_rate = tty_termios_baud_rate(old);
846 int new_baud_rate = tty_termios_baud_rate(new);
848 u8 baud, data_bits, stop_bits, parity, x_on, x_off;
851 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
853 BT_DBG("tty %p termios %p", tty, old);
855 if (!dev || !dev->dlc || !dev->dlc->session)
858 /* Handle turning off CRTSCTS */
859 if ((old->c_cflag & CRTSCTS) && !(new->c_cflag & CRTSCTS))
860 BT_DBG("Turning off CRTSCTS unsupported");
862 /* Parity on/off and when on, odd/even */
863 if (((old->c_cflag & PARENB) != (new->c_cflag & PARENB)) ||
864 ((old->c_cflag & PARODD) != (new->c_cflag & PARODD))) {
865 changes |= RFCOMM_RPN_PM_PARITY;
866 BT_DBG("Parity change detected.");
869 /* Mark and space parity are not supported! */
870 if (new->c_cflag & PARENB) {
871 if (new->c_cflag & PARODD) {
872 BT_DBG("Parity is ODD");
873 parity = RFCOMM_RPN_PARITY_ODD;
875 BT_DBG("Parity is EVEN");
876 parity = RFCOMM_RPN_PARITY_EVEN;
879 BT_DBG("Parity is OFF");
880 parity = RFCOMM_RPN_PARITY_NONE;
883 /* Setting the x_on / x_off characters */
884 if (old->c_cc[VSTOP] != new->c_cc[VSTOP]) {
885 BT_DBG("XOFF custom");
886 x_on = new->c_cc[VSTOP];
887 changes |= RFCOMM_RPN_PM_XON;
889 BT_DBG("XOFF default");
890 x_on = RFCOMM_RPN_XON_CHAR;
893 if (old->c_cc[VSTART] != new->c_cc[VSTART]) {
894 BT_DBG("XON custom");
895 x_off = new->c_cc[VSTART];
896 changes |= RFCOMM_RPN_PM_XOFF;
898 BT_DBG("XON default");
899 x_off = RFCOMM_RPN_XOFF_CHAR;
902 /* Handle setting of stop bits */
903 if ((old->c_cflag & CSTOPB) != (new->c_cflag & CSTOPB))
904 changes |= RFCOMM_RPN_PM_STOP;
906 /* POSIX does not support 1.5 stop bits and RFCOMM does not
907 * support 2 stop bits. So a request for 2 stop bits gets
908 * translated to 1.5 stop bits */
909 if (new->c_cflag & CSTOPB)
910 stop_bits = RFCOMM_RPN_STOP_15;
912 stop_bits = RFCOMM_RPN_STOP_1;
914 /* Handle number of data bits [5-8] */
915 if ((old->c_cflag & CSIZE) != (new->c_cflag & CSIZE))
916 changes |= RFCOMM_RPN_PM_DATA;
918 switch (new->c_cflag & CSIZE) {
920 data_bits = RFCOMM_RPN_DATA_5;
923 data_bits = RFCOMM_RPN_DATA_6;
926 data_bits = RFCOMM_RPN_DATA_7;
929 data_bits = RFCOMM_RPN_DATA_8;
932 data_bits = RFCOMM_RPN_DATA_8;
936 /* Handle baudrate settings */
937 if (old_baud_rate != new_baud_rate)
938 changes |= RFCOMM_RPN_PM_BITRATE;
940 switch (new_baud_rate) {
942 baud = RFCOMM_RPN_BR_2400;
945 baud = RFCOMM_RPN_BR_4800;
948 baud = RFCOMM_RPN_BR_7200;
951 baud = RFCOMM_RPN_BR_9600;
954 baud = RFCOMM_RPN_BR_19200;
957 baud = RFCOMM_RPN_BR_38400;
960 baud = RFCOMM_RPN_BR_57600;
963 baud = RFCOMM_RPN_BR_115200;
966 baud = RFCOMM_RPN_BR_230400;
969 /* 9600 is standard accordinag to the RFCOMM specification */
970 baud = RFCOMM_RPN_BR_9600;
976 rfcomm_send_rpn(dev->dlc->session, 1, dev->dlc->dlci, baud,
977 data_bits, stop_bits, parity,
978 RFCOMM_RPN_FLOW_NONE, x_on, x_off, changes);
981 static void rfcomm_tty_throttle(struct tty_struct *tty)
983 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
985 BT_DBG("tty %p dev %p", tty, dev);
987 rfcomm_dlc_throttle(dev->dlc);
990 static void rfcomm_tty_unthrottle(struct tty_struct *tty)
992 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
994 BT_DBG("tty %p dev %p", tty, dev);
996 rfcomm_dlc_unthrottle(dev->dlc);
999 static int rfcomm_tty_chars_in_buffer(struct tty_struct *tty)
1001 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1003 BT_DBG("tty %p dev %p", tty, dev);
1005 if (!dev || !dev->dlc)
1008 if (!skb_queue_empty(&dev->dlc->tx_queue))
1009 return dev->dlc->mtu;
1014 static void rfcomm_tty_flush_buffer(struct tty_struct *tty)
1016 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1018 BT_DBG("tty %p dev %p", tty, dev);
1020 if (!dev || !dev->dlc)
1023 skb_queue_purge(&dev->dlc->tx_queue);
1027 static void rfcomm_tty_send_xchar(struct tty_struct *tty, char ch)
1029 BT_DBG("tty %p ch %c", tty, ch);
1032 static void rfcomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
1034 BT_DBG("tty %p timeout %d", tty, timeout);
1037 static void rfcomm_tty_hangup(struct tty_struct *tty)
1039 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1041 BT_DBG("tty %p dev %p", tty, dev);
1043 tty_port_hangup(&dev->port);
1045 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags) &&
1046 !test_and_set_bit(RFCOMM_TTY_RELEASED, &dev->flags))
1047 tty_port_put(&dev->port);
1050 static int rfcomm_tty_tiocmget(struct tty_struct *tty)
1052 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1054 BT_DBG("tty %p dev %p", tty, dev);
1056 return dev->modem_status;
1059 static int rfcomm_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
1061 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1062 struct rfcomm_dlc *dlc = dev->dlc;
1065 BT_DBG("tty %p dev %p set 0x%02x clear 0x%02x", tty, dev, set, clear);
1067 rfcomm_dlc_get_modem_status(dlc, &v24_sig);
1069 if (set & TIOCM_DSR || set & TIOCM_DTR)
1070 v24_sig |= RFCOMM_V24_RTC;
1071 if (set & TIOCM_RTS || set & TIOCM_CTS)
1072 v24_sig |= RFCOMM_V24_RTR;
1074 v24_sig |= RFCOMM_V24_IC;
1076 v24_sig |= RFCOMM_V24_DV;
1078 if (clear & TIOCM_DSR || clear & TIOCM_DTR)
1079 v24_sig &= ~RFCOMM_V24_RTC;
1080 if (clear & TIOCM_RTS || clear & TIOCM_CTS)
1081 v24_sig &= ~RFCOMM_V24_RTR;
1082 if (clear & TIOCM_RI)
1083 v24_sig &= ~RFCOMM_V24_IC;
1084 if (clear & TIOCM_CD)
1085 v24_sig &= ~RFCOMM_V24_DV;
1087 rfcomm_dlc_set_modem_status(dlc, v24_sig);
1092 /* ---- TTY structure ---- */
1094 static const struct tty_operations rfcomm_ops = {
1095 .open = rfcomm_tty_open,
1096 .close = rfcomm_tty_close,
1097 .write = rfcomm_tty_write,
1098 .write_room = rfcomm_tty_write_room,
1099 .chars_in_buffer = rfcomm_tty_chars_in_buffer,
1100 .flush_buffer = rfcomm_tty_flush_buffer,
1101 .ioctl = rfcomm_tty_ioctl,
1102 .throttle = rfcomm_tty_throttle,
1103 .unthrottle = rfcomm_tty_unthrottle,
1104 .set_termios = rfcomm_tty_set_termios,
1105 .send_xchar = rfcomm_tty_send_xchar,
1106 .hangup = rfcomm_tty_hangup,
1107 .wait_until_sent = rfcomm_tty_wait_until_sent,
1108 .tiocmget = rfcomm_tty_tiocmget,
1109 .tiocmset = rfcomm_tty_tiocmset,
1110 .install = rfcomm_tty_install,
1111 .cleanup = rfcomm_tty_cleanup,
1114 int __init rfcomm_init_ttys(void)
1118 rfcomm_tty_driver = alloc_tty_driver(RFCOMM_TTY_PORTS);
1119 if (!rfcomm_tty_driver)
1122 rfcomm_tty_driver->driver_name = "rfcomm";
1123 rfcomm_tty_driver->name = "rfcomm";
1124 rfcomm_tty_driver->major = RFCOMM_TTY_MAJOR;
1125 rfcomm_tty_driver->minor_start = RFCOMM_TTY_MINOR;
1126 rfcomm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1127 rfcomm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1128 rfcomm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1129 rfcomm_tty_driver->init_termios = tty_std_termios;
1130 rfcomm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
1131 rfcomm_tty_driver->init_termios.c_lflag &= ~ICANON;
1132 tty_set_operations(rfcomm_tty_driver, &rfcomm_ops);
1134 error = tty_register_driver(rfcomm_tty_driver);
1136 BT_ERR("Can't register RFCOMM TTY driver");
1137 put_tty_driver(rfcomm_tty_driver);
1141 BT_INFO("RFCOMM TTY layer initialized");
1146 void rfcomm_cleanup_ttys(void)
1148 tty_unregister_driver(rfcomm_tty_driver);
1149 put_tty_driver(rfcomm_tty_driver);