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;
81 struct sk_buff_head pending;
84 static LIST_HEAD(rfcomm_dev_list);
85 static DEFINE_RWLOCK(rfcomm_dev_lock);
87 static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb);
88 static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err);
89 static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig);
91 static void rfcomm_tty_wakeup(unsigned long arg);
93 /* ---- Device functions ---- */
94 static void rfcomm_dev_destruct(struct rfcomm_dev *dev)
96 struct rfcomm_dlc *dlc = dev->dlc;
98 BT_DBG("dev %p dlc %p", dev, dlc);
100 /* Refcount should only hit zero when called from rfcomm_dev_del()
101 which will have taken us off the list. Everything else are
103 BUG_ON(!list_empty(&dev->list));
105 rfcomm_dlc_lock(dlc);
106 /* Detach DLC if it's owned by this dev */
107 if (dlc->owner == dev)
109 rfcomm_dlc_unlock(dlc);
113 tty_unregister_device(rfcomm_tty_driver, dev->id);
117 /* It's safe to call module_put() here because socket still
118 holds reference to this module. */
119 module_put(THIS_MODULE);
122 static inline void rfcomm_dev_hold(struct rfcomm_dev *dev)
124 atomic_inc(&dev->refcnt);
127 static inline void rfcomm_dev_put(struct rfcomm_dev *dev)
129 /* The reason this isn't actually a race, as you no
130 doubt have a little voice screaming at you in your
131 head, is that the refcount should never actually
132 reach zero unless the device has already been taken
133 off the list, in rfcomm_dev_del(). And if that's not
134 true, we'll hit the BUG() in rfcomm_dev_destruct()
136 if (atomic_dec_and_test(&dev->refcnt))
137 rfcomm_dev_destruct(dev);
140 static struct rfcomm_dev *__rfcomm_dev_get(int id)
142 struct rfcomm_dev *dev;
145 list_for_each(p, &rfcomm_dev_list) {
146 dev = list_entry(p, struct rfcomm_dev, list);
154 static inline struct rfcomm_dev *rfcomm_dev_get(int id)
156 struct rfcomm_dev *dev;
158 read_lock(&rfcomm_dev_lock);
160 dev = __rfcomm_dev_get(id);
163 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
166 rfcomm_dev_hold(dev);
169 read_unlock(&rfcomm_dev_lock);
174 static struct device *rfcomm_get_device(struct rfcomm_dev *dev)
176 struct hci_dev *hdev;
177 struct hci_conn *conn;
179 hdev = hci_get_route(&dev->dst, &dev->src);
183 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &dev->dst);
187 return conn ? &conn->dev : NULL;
190 static ssize_t show_address(struct device *tty_dev, struct device_attribute *attr, char *buf)
192 struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
194 baswap(&bdaddr, &dev->dst);
195 return sprintf(buf, "%s\n", batostr(&bdaddr));
198 static ssize_t show_channel(struct device *tty_dev, struct device_attribute *attr, char *buf)
200 struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
201 return sprintf(buf, "%d\n", dev->channel);
204 static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
205 static DEVICE_ATTR(channel, S_IRUGO, show_channel, NULL);
207 static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
209 struct rfcomm_dev *dev;
210 struct list_head *head = &rfcomm_dev_list, *p;
213 BT_DBG("id %d channel %d", req->dev_id, req->channel);
215 dev = kzalloc(sizeof(struct rfcomm_dev), GFP_KERNEL);
219 write_lock_bh(&rfcomm_dev_lock);
221 if (req->dev_id < 0) {
224 list_for_each(p, &rfcomm_dev_list) {
225 if (list_entry(p, struct rfcomm_dev, list)->id != dev->id)
232 dev->id = req->dev_id;
234 list_for_each(p, &rfcomm_dev_list) {
235 struct rfcomm_dev *entry = list_entry(p, struct rfcomm_dev, list);
237 if (entry->id == dev->id) {
242 if (entry->id > dev->id - 1)
249 if ((dev->id < 0) || (dev->id > RFCOMM_MAX_DEV - 1)) {
254 sprintf(dev->name, "rfcomm%d", dev->id);
256 list_add(&dev->list, head);
257 atomic_set(&dev->refcnt, 1);
259 bacpy(&dev->src, &req->src);
260 bacpy(&dev->dst, &req->dst);
261 dev->channel = req->channel;
263 dev->flags = req->flags &
264 ((1 << RFCOMM_RELEASE_ONHUP) | (1 << RFCOMM_REUSE_DLC));
266 init_waitqueue_head(&dev->wait);
267 tasklet_init(&dev->wakeup_task, rfcomm_tty_wakeup, (unsigned long) dev);
269 skb_queue_head_init(&dev->pending);
271 rfcomm_dlc_lock(dlc);
273 if (req->flags & (1 << RFCOMM_REUSE_DLC)) {
274 struct sock *sk = dlc->owner;
279 rfcomm_dlc_throttle(dlc);
281 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
283 skb_queue_tail(&dev->pending, skb);
284 atomic_sub(skb->len, &sk->sk_rmem_alloc);
288 dlc->data_ready = rfcomm_dev_data_ready;
289 dlc->state_change = rfcomm_dev_state_change;
290 dlc->modem_status = rfcomm_dev_modem_status;
295 rfcomm_dev_modem_status(dlc, dlc->remote_v24_sig);
297 rfcomm_dlc_unlock(dlc);
299 /* It's safe to call __module_get() here because socket already
300 holds reference to this module. */
301 __module_get(THIS_MODULE);
304 write_unlock_bh(&rfcomm_dev_lock);
311 dev->tty_dev = tty_register_device(rfcomm_tty_driver, dev->id, NULL);
313 if (IS_ERR(dev->tty_dev)) {
314 err = PTR_ERR(dev->tty_dev);
315 list_del(&dev->list);
320 dev_set_drvdata(dev->tty_dev, dev);
322 if (device_create_file(dev->tty_dev, &dev_attr_address) < 0)
323 BT_ERR("Failed to create address attribute");
325 if (device_create_file(dev->tty_dev, &dev_attr_channel) < 0)
326 BT_ERR("Failed to create channel attribute");
331 static void rfcomm_dev_del(struct rfcomm_dev *dev)
333 BT_DBG("dev %p", dev);
335 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
338 set_bit(RFCOMM_TTY_RELEASED, &dev->flags);
340 write_lock_bh(&rfcomm_dev_lock);
341 list_del_init(&dev->list);
342 write_unlock_bh(&rfcomm_dev_lock);
347 /* ---- Send buffer ---- */
348 static inline unsigned int rfcomm_room(struct rfcomm_dlc *dlc)
350 /* We can't let it be zero, because we don't get a callback
351 when tx_credits becomes nonzero, hence we'd never wake up */
352 return dlc->mtu * (dlc->tx_credits?:1);
355 static void rfcomm_wfree(struct sk_buff *skb)
357 struct rfcomm_dev *dev = (void *) skb->sk;
358 atomic_sub(skb->truesize, &dev->wmem_alloc);
359 if (test_bit(RFCOMM_TTY_ATTACHED, &dev->flags))
360 tasklet_schedule(&dev->wakeup_task);
364 static inline void rfcomm_set_owner_w(struct sk_buff *skb, struct rfcomm_dev *dev)
366 rfcomm_dev_hold(dev);
367 atomic_add(skb->truesize, &dev->wmem_alloc);
368 skb->sk = (void *) dev;
369 skb->destructor = rfcomm_wfree;
372 static struct sk_buff *rfcomm_wmalloc(struct rfcomm_dev *dev, unsigned long size, gfp_t priority)
374 if (atomic_read(&dev->wmem_alloc) < rfcomm_room(dev->dlc)) {
375 struct sk_buff *skb = alloc_skb(size, priority);
377 rfcomm_set_owner_w(skb, dev);
384 /* ---- Device IOCTLs ---- */
386 #define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP))
388 static int rfcomm_create_dev(struct sock *sk, void __user *arg)
390 struct rfcomm_dev_req req;
391 struct rfcomm_dlc *dlc;
394 if (copy_from_user(&req, arg, sizeof(req)))
397 BT_DBG("sk %p dev_id %d flags 0x%x", sk, req.dev_id, req.flags);
399 if (req.flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN))
402 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
403 /* Socket must be connected */
404 if (sk->sk_state != BT_CONNECTED)
407 dlc = rfcomm_pi(sk)->dlc;
408 rfcomm_dlc_hold(dlc);
410 dlc = rfcomm_dlc_alloc(GFP_KERNEL);
415 id = rfcomm_dev_add(&req, dlc);
421 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
422 /* DLC is now used by device.
423 * Socket must be disconnected */
424 sk->sk_state = BT_CLOSED;
430 static int rfcomm_release_dev(void __user *arg)
432 struct rfcomm_dev_req req;
433 struct rfcomm_dev *dev;
435 if (copy_from_user(&req, arg, sizeof(req)))
438 BT_DBG("dev_id %d flags 0x%x", req.dev_id, req.flags);
440 if (!(dev = rfcomm_dev_get(req.dev_id)))
443 if (dev->flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN)) {
448 if (req.flags & (1 << RFCOMM_HANGUP_NOW))
449 rfcomm_dlc_close(dev->dlc, 0);
451 /* Shut down TTY synchronously before freeing rfcomm_dev */
453 tty_vhangup(dev->tty);
455 if (!test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags))
461 static int rfcomm_get_dev_list(void __user *arg)
463 struct rfcomm_dev_list_req *dl;
464 struct rfcomm_dev_info *di;
466 int n = 0, size, err;
471 if (get_user(dev_num, (u16 __user *) arg))
474 if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di))
477 size = sizeof(*dl) + dev_num * sizeof(*di);
479 if (!(dl = kmalloc(size, GFP_KERNEL)))
484 read_lock_bh(&rfcomm_dev_lock);
486 list_for_each(p, &rfcomm_dev_list) {
487 struct rfcomm_dev *dev = list_entry(p, struct rfcomm_dev, list);
488 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
490 (di + n)->id = dev->id;
491 (di + n)->flags = dev->flags;
492 (di + n)->state = dev->dlc->state;
493 (di + n)->channel = dev->channel;
494 bacpy(&(di + n)->src, &dev->src);
495 bacpy(&(di + n)->dst, &dev->dst);
500 read_unlock_bh(&rfcomm_dev_lock);
503 size = sizeof(*dl) + n * sizeof(*di);
505 err = copy_to_user(arg, dl, size);
508 return err ? -EFAULT : 0;
511 static int rfcomm_get_dev_info(void __user *arg)
513 struct rfcomm_dev *dev;
514 struct rfcomm_dev_info di;
519 if (copy_from_user(&di, arg, sizeof(di)))
522 if (!(dev = rfcomm_dev_get(di.id)))
525 di.flags = dev->flags;
526 di.channel = dev->channel;
527 di.state = dev->dlc->state;
528 bacpy(&di.src, &dev->src);
529 bacpy(&di.dst, &dev->dst);
531 if (copy_to_user(arg, &di, sizeof(di)))
538 int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
540 BT_DBG("cmd %d arg %p", cmd, arg);
543 case RFCOMMCREATEDEV:
544 return rfcomm_create_dev(sk, arg);
546 case RFCOMMRELEASEDEV:
547 return rfcomm_release_dev(arg);
549 case RFCOMMGETDEVLIST:
550 return rfcomm_get_dev_list(arg);
552 case RFCOMMGETDEVINFO:
553 return rfcomm_get_dev_info(arg);
559 /* ---- DLC callbacks ---- */
560 static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb)
562 struct rfcomm_dev *dev = dlc->owner;
563 struct tty_struct *tty;
570 if (!(tty = dev->tty) || !skb_queue_empty(&dev->pending)) {
571 skb_queue_tail(&dev->pending, skb);
575 BT_DBG("dlc %p tty %p len %d", dlc, tty, skb->len);
577 tty_insert_flip_string(tty, skb->data, skb->len);
578 tty_flip_buffer_push(tty);
583 static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
585 struct rfcomm_dev *dev = dlc->owner;
589 BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
592 wake_up_interruptible(&dev->wait);
594 if (dlc->state == BT_CLOSED) {
596 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
597 /* Drop DLC lock here to avoid deadlock
598 * 1. rfcomm_dev_get will take rfcomm_dev_lock
599 * but in rfcomm_dev_add there's lock order:
600 * rfcomm_dev_lock -> dlc lock
601 * 2. rfcomm_dev_put will deadlock if it's
604 rfcomm_dlc_unlock(dlc);
605 if (rfcomm_dev_get(dev->id) == NULL) {
606 rfcomm_dlc_lock(dlc);
612 rfcomm_dlc_lock(dlc);
615 tty_hangup(dev->tty);
619 static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig)
621 struct rfcomm_dev *dev = dlc->owner;
625 BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc, dev, v24_sig);
627 if ((dev->modem_status & TIOCM_CD) && !(v24_sig & RFCOMM_V24_DV)) {
628 if (dev->tty && !C_CLOCAL(dev->tty))
629 tty_hangup(dev->tty);
633 ((v24_sig & RFCOMM_V24_RTC) ? (TIOCM_DSR | TIOCM_DTR) : 0) |
634 ((v24_sig & RFCOMM_V24_RTR) ? (TIOCM_RTS | TIOCM_CTS) : 0) |
635 ((v24_sig & RFCOMM_V24_IC) ? TIOCM_RI : 0) |
636 ((v24_sig & RFCOMM_V24_DV) ? TIOCM_CD : 0);
639 /* ---- TTY functions ---- */
640 static void rfcomm_tty_wakeup(unsigned long arg)
642 struct rfcomm_dev *dev = (void *) arg;
643 struct tty_struct *tty = dev->tty;
647 BT_DBG("dev %p tty %p", dev, tty);
649 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && tty->ldisc.write_wakeup)
650 (tty->ldisc.write_wakeup)(tty);
652 wake_up_interruptible(&tty->write_wait);
653 #ifdef SERIAL_HAVE_POLL_WAIT
654 wake_up_interruptible(&tty->poll_wait);
658 static void rfcomm_tty_copy_pending(struct rfcomm_dev *dev)
660 struct tty_struct *tty = dev->tty;
667 BT_DBG("dev %p tty %p", dev, tty);
669 rfcomm_dlc_lock(dev->dlc);
671 while ((skb = skb_dequeue(&dev->pending))) {
672 inserted += tty_insert_flip_string(tty, skb->data, skb->len);
676 rfcomm_dlc_unlock(dev->dlc);
679 tty_flip_buffer_push(tty);
682 static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
684 DECLARE_WAITQUEUE(wait, current);
685 struct rfcomm_dev *dev;
686 struct rfcomm_dlc *dlc;
691 BT_DBG("tty %p id %d", tty, id);
693 /* We don't leak this refcount. For reasons which are not entirely
694 clear, the TTY layer will call our ->close() method even if the
695 open fails. We decrease the refcount there, and decreasing it
696 here too would cause breakage. */
697 dev = rfcomm_dev_get(id);
701 BT_DBG("dev %p dst %s channel %d opened %d", dev, batostr(&dev->dst), dev->channel, dev->opened);
703 if (dev->opened++ != 0)
708 /* Attach TTY and open DLC */
710 rfcomm_dlc_lock(dlc);
711 tty->driver_data = dev;
713 rfcomm_dlc_unlock(dlc);
714 set_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
716 err = rfcomm_dlc_open(dlc, &dev->src, &dev->dst, dev->channel);
720 /* Wait for DLC to connect */
721 add_wait_queue(&dev->wait, &wait);
723 set_current_state(TASK_INTERRUPTIBLE);
725 if (dlc->state == BT_CLOSED) {
730 if (dlc->state == BT_CONNECTED)
733 if (signal_pending(current)) {
740 set_current_state(TASK_RUNNING);
741 remove_wait_queue(&dev->wait, &wait);
744 device_move(dev->tty_dev, rfcomm_get_device(dev));
746 rfcomm_tty_copy_pending(dev);
748 rfcomm_dlc_unthrottle(dev->dlc);
753 static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
755 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
759 BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc, dev->opened);
761 if (--dev->opened == 0) {
762 if (dev->tty_dev->parent)
763 device_move(dev->tty_dev, NULL);
765 /* Close DLC and dettach TTY */
766 rfcomm_dlc_close(dev->dlc, 0);
768 clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
769 tasklet_kill(&dev->wakeup_task);
771 rfcomm_dlc_lock(dev->dlc);
772 tty->driver_data = NULL;
774 rfcomm_dlc_unlock(dev->dlc);
780 static int rfcomm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
782 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
783 struct rfcomm_dlc *dlc = dev->dlc;
785 int err = 0, sent = 0, size;
787 BT_DBG("tty %p count %d", tty, count);
790 size = min_t(uint, count, dlc->mtu);
792 skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, GFP_ATOMIC);
797 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
799 memcpy(skb_put(skb, size), buf + sent, size);
801 if ((err = rfcomm_dlc_send(dlc, skb)) < 0) {
810 return sent ? sent : err;
813 static int rfcomm_tty_write_room(struct tty_struct *tty)
815 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
818 BT_DBG("tty %p", tty);
820 if (!dev || !dev->dlc)
823 room = rfcomm_room(dev->dlc) - atomic_read(&dev->wmem_alloc);
830 static int rfcomm_tty_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd, unsigned long arg)
832 BT_DBG("tty %p cmd 0x%02x", tty, cmd);
836 BT_DBG("TCGETS is not supported");
840 BT_DBG("TCSETS is not supported");
844 BT_DBG("TIOCMIWAIT");
848 BT_DBG("TIOCGICOUNT");
852 BT_ERR("TIOCGSERIAL is not supported");
856 BT_ERR("TIOCSSERIAL is not supported");
860 BT_ERR("TIOCSERGSTRUCT is not supported");
864 BT_ERR("TIOCSERGETLSR is not supported");
868 BT_ERR("TIOCSERCONFIG is not supported");
872 return -ENOIOCTLCMD; /* ioctls which we must ignore */
879 static void rfcomm_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
881 struct ktermios *new = tty->termios;
882 int old_baud_rate = tty_termios_baud_rate(old);
883 int new_baud_rate = tty_termios_baud_rate(new);
885 u8 baud, data_bits, stop_bits, parity, x_on, x_off;
888 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
890 BT_DBG("tty %p termios %p", tty, old);
892 if (!dev || !dev->dlc || !dev->dlc->session)
895 /* Handle turning off CRTSCTS */
896 if ((old->c_cflag & CRTSCTS) && !(new->c_cflag & CRTSCTS))
897 BT_DBG("Turning off CRTSCTS unsupported");
899 /* Parity on/off and when on, odd/even */
900 if (((old->c_cflag & PARENB) != (new->c_cflag & PARENB)) ||
901 ((old->c_cflag & PARODD) != (new->c_cflag & PARODD)) ) {
902 changes |= RFCOMM_RPN_PM_PARITY;
903 BT_DBG("Parity change detected.");
906 /* Mark and space parity are not supported! */
907 if (new->c_cflag & PARENB) {
908 if (new->c_cflag & PARODD) {
909 BT_DBG("Parity is ODD");
910 parity = RFCOMM_RPN_PARITY_ODD;
912 BT_DBG("Parity is EVEN");
913 parity = RFCOMM_RPN_PARITY_EVEN;
916 BT_DBG("Parity is OFF");
917 parity = RFCOMM_RPN_PARITY_NONE;
920 /* Setting the x_on / x_off characters */
921 if (old->c_cc[VSTOP] != new->c_cc[VSTOP]) {
922 BT_DBG("XOFF custom");
923 x_on = new->c_cc[VSTOP];
924 changes |= RFCOMM_RPN_PM_XON;
926 BT_DBG("XOFF default");
927 x_on = RFCOMM_RPN_XON_CHAR;
930 if (old->c_cc[VSTART] != new->c_cc[VSTART]) {
931 BT_DBG("XON custom");
932 x_off = new->c_cc[VSTART];
933 changes |= RFCOMM_RPN_PM_XOFF;
935 BT_DBG("XON default");
936 x_off = RFCOMM_RPN_XOFF_CHAR;
939 /* Handle setting of stop bits */
940 if ((old->c_cflag & CSTOPB) != (new->c_cflag & CSTOPB))
941 changes |= RFCOMM_RPN_PM_STOP;
943 /* POSIX does not support 1.5 stop bits and RFCOMM does not
944 * support 2 stop bits. So a request for 2 stop bits gets
945 * translated to 1.5 stop bits */
946 if (new->c_cflag & CSTOPB) {
947 stop_bits = RFCOMM_RPN_STOP_15;
949 stop_bits = RFCOMM_RPN_STOP_1;
952 /* Handle number of data bits [5-8] */
953 if ((old->c_cflag & CSIZE) != (new->c_cflag & CSIZE))
954 changes |= RFCOMM_RPN_PM_DATA;
956 switch (new->c_cflag & CSIZE) {
958 data_bits = RFCOMM_RPN_DATA_5;
961 data_bits = RFCOMM_RPN_DATA_6;
964 data_bits = RFCOMM_RPN_DATA_7;
967 data_bits = RFCOMM_RPN_DATA_8;
970 data_bits = RFCOMM_RPN_DATA_8;
974 /* Handle baudrate settings */
975 if (old_baud_rate != new_baud_rate)
976 changes |= RFCOMM_RPN_PM_BITRATE;
978 switch (new_baud_rate) {
980 baud = RFCOMM_RPN_BR_2400;
983 baud = RFCOMM_RPN_BR_4800;
986 baud = RFCOMM_RPN_BR_7200;
989 baud = RFCOMM_RPN_BR_9600;
992 baud = RFCOMM_RPN_BR_19200;
995 baud = RFCOMM_RPN_BR_38400;
998 baud = RFCOMM_RPN_BR_57600;
1001 baud = RFCOMM_RPN_BR_115200;
1004 baud = RFCOMM_RPN_BR_230400;
1007 /* 9600 is standard accordinag to the RFCOMM specification */
1008 baud = RFCOMM_RPN_BR_9600;
1014 rfcomm_send_rpn(dev->dlc->session, 1, dev->dlc->dlci, baud,
1015 data_bits, stop_bits, parity,
1016 RFCOMM_RPN_FLOW_NONE, x_on, x_off, changes);
1021 static void rfcomm_tty_throttle(struct tty_struct *tty)
1023 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1025 BT_DBG("tty %p dev %p", tty, dev);
1027 rfcomm_dlc_throttle(dev->dlc);
1030 static void rfcomm_tty_unthrottle(struct tty_struct *tty)
1032 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1034 BT_DBG("tty %p dev %p", tty, dev);
1036 rfcomm_dlc_unthrottle(dev->dlc);
1039 static int rfcomm_tty_chars_in_buffer(struct tty_struct *tty)
1041 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1043 BT_DBG("tty %p dev %p", tty, dev);
1045 if (!dev || !dev->dlc)
1048 if (!skb_queue_empty(&dev->dlc->tx_queue))
1049 return dev->dlc->mtu;
1054 static void rfcomm_tty_flush_buffer(struct tty_struct *tty)
1056 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1058 BT_DBG("tty %p dev %p", tty, dev);
1060 if (!dev || !dev->dlc)
1063 skb_queue_purge(&dev->dlc->tx_queue);
1065 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && tty->ldisc.write_wakeup)
1066 tty->ldisc.write_wakeup(tty);
1069 static void rfcomm_tty_send_xchar(struct tty_struct *tty, char ch)
1071 BT_DBG("tty %p ch %c", tty, ch);
1074 static void rfcomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
1076 BT_DBG("tty %p timeout %d", tty, timeout);
1079 static void rfcomm_tty_hangup(struct tty_struct *tty)
1081 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1083 BT_DBG("tty %p dev %p", tty, dev);
1088 rfcomm_tty_flush_buffer(tty);
1090 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
1091 if (rfcomm_dev_get(dev->id) == NULL)
1093 rfcomm_dev_del(dev);
1094 rfcomm_dev_put(dev);
1098 static int rfcomm_tty_read_proc(char *buf, char **start, off_t offset, int len, int *eof, void *unused)
1103 static int rfcomm_tty_tiocmget(struct tty_struct *tty, struct file *filp)
1105 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1107 BT_DBG("tty %p dev %p", tty, dev);
1109 return dev->modem_status;
1112 static int rfcomm_tty_tiocmset(struct tty_struct *tty, struct file *filp, unsigned int set, unsigned int clear)
1114 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1115 struct rfcomm_dlc *dlc = dev->dlc;
1118 BT_DBG("tty %p dev %p set 0x%02x clear 0x%02x", tty, dev, set, clear);
1120 rfcomm_dlc_get_modem_status(dlc, &v24_sig);
1122 if (set & TIOCM_DSR || set & TIOCM_DTR)
1123 v24_sig |= RFCOMM_V24_RTC;
1124 if (set & TIOCM_RTS || set & TIOCM_CTS)
1125 v24_sig |= RFCOMM_V24_RTR;
1127 v24_sig |= RFCOMM_V24_IC;
1129 v24_sig |= RFCOMM_V24_DV;
1131 if (clear & TIOCM_DSR || clear & TIOCM_DTR)
1132 v24_sig &= ~RFCOMM_V24_RTC;
1133 if (clear & TIOCM_RTS || clear & TIOCM_CTS)
1134 v24_sig &= ~RFCOMM_V24_RTR;
1135 if (clear & TIOCM_RI)
1136 v24_sig &= ~RFCOMM_V24_IC;
1137 if (clear & TIOCM_CD)
1138 v24_sig &= ~RFCOMM_V24_DV;
1140 rfcomm_dlc_set_modem_status(dlc, v24_sig);
1145 /* ---- TTY structure ---- */
1147 static const struct tty_operations rfcomm_ops = {
1148 .open = rfcomm_tty_open,
1149 .close = rfcomm_tty_close,
1150 .write = rfcomm_tty_write,
1151 .write_room = rfcomm_tty_write_room,
1152 .chars_in_buffer = rfcomm_tty_chars_in_buffer,
1153 .flush_buffer = rfcomm_tty_flush_buffer,
1154 .ioctl = rfcomm_tty_ioctl,
1155 .throttle = rfcomm_tty_throttle,
1156 .unthrottle = rfcomm_tty_unthrottle,
1157 .set_termios = rfcomm_tty_set_termios,
1158 .send_xchar = rfcomm_tty_send_xchar,
1159 .hangup = rfcomm_tty_hangup,
1160 .wait_until_sent = rfcomm_tty_wait_until_sent,
1161 .read_proc = rfcomm_tty_read_proc,
1162 .tiocmget = rfcomm_tty_tiocmget,
1163 .tiocmset = rfcomm_tty_tiocmset,
1166 int rfcomm_init_ttys(void)
1168 rfcomm_tty_driver = alloc_tty_driver(RFCOMM_TTY_PORTS);
1169 if (!rfcomm_tty_driver)
1172 rfcomm_tty_driver->owner = THIS_MODULE;
1173 rfcomm_tty_driver->driver_name = "rfcomm";
1174 rfcomm_tty_driver->name = "rfcomm";
1175 rfcomm_tty_driver->major = RFCOMM_TTY_MAJOR;
1176 rfcomm_tty_driver->minor_start = RFCOMM_TTY_MINOR;
1177 rfcomm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1178 rfcomm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1179 rfcomm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1180 rfcomm_tty_driver->init_termios = tty_std_termios;
1181 rfcomm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1182 rfcomm_tty_driver->init_termios.c_lflag &= ~ICANON;
1183 tty_set_operations(rfcomm_tty_driver, &rfcomm_ops);
1185 if (tty_register_driver(rfcomm_tty_driver)) {
1186 BT_ERR("Can't register RFCOMM TTY driver");
1187 put_tty_driver(rfcomm_tty_driver);
1191 BT_INFO("RFCOMM TTY layer initialized");
1196 void rfcomm_cleanup_ttys(void)
1198 tty_unregister_driver(rfcomm_tty_driver);
1199 put_tty_driver(rfcomm_tty_driver);