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;
61 wait_queue_head_t wait;
63 struct device *tty_dev;
67 struct sk_buff_head pending;
70 static LIST_HEAD(rfcomm_dev_list);
71 static DEFINE_SPINLOCK(rfcomm_dev_lock);
73 static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb);
74 static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err);
75 static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig);
77 /* ---- Device functions ---- */
80 * The reason this isn't actually a race, as you no doubt have a little voice
81 * screaming at you in your head, is that the refcount should never actually
82 * reach zero unless the device has already been taken off the list, in
83 * rfcomm_dev_del(). And if that's not true, we'll hit the BUG() in
84 * rfcomm_dev_destruct() anyway.
86 static void rfcomm_dev_destruct(struct tty_port *port)
88 struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
89 struct rfcomm_dlc *dlc = dev->dlc;
91 BT_DBG("dev %p dlc %p", dev, dlc);
93 /* Refcount should only hit zero when called from rfcomm_dev_del()
94 which will have taken us off the list. Everything else are
96 BUG_ON(!list_empty(&dev->list));
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);
110 /* It's safe to call module_put() here because socket still
111 holds reference to this module. */
112 module_put(THIS_MODULE);
115 static const struct tty_port_operations rfcomm_port_ops = {
116 .destruct = rfcomm_dev_destruct,
119 static struct rfcomm_dev *__rfcomm_dev_get(int id)
121 struct rfcomm_dev *dev;
123 list_for_each_entry(dev, &rfcomm_dev_list, list)
130 static struct rfcomm_dev *rfcomm_dev_get(int id)
132 struct rfcomm_dev *dev;
134 spin_lock(&rfcomm_dev_lock);
136 dev = __rfcomm_dev_get(id);
139 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
142 tty_port_get(&dev->port);
145 spin_unlock(&rfcomm_dev_lock);
150 static struct device *rfcomm_get_device(struct rfcomm_dev *dev)
152 struct hci_dev *hdev;
153 struct hci_conn *conn;
155 hdev = hci_get_route(&dev->dst, &dev->src);
159 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &dev->dst);
163 return conn ? &conn->dev : NULL;
166 static ssize_t show_address(struct device *tty_dev, struct device_attribute *attr, char *buf)
168 struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
169 return sprintf(buf, "%pMR\n", &dev->dst);
172 static ssize_t show_channel(struct device *tty_dev, struct device_attribute *attr, char *buf)
174 struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
175 return sprintf(buf, "%d\n", dev->channel);
178 static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
179 static DEVICE_ATTR(channel, S_IRUGO, show_channel, NULL);
181 static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
183 struct rfcomm_dev *dev, *entry;
184 struct list_head *head = &rfcomm_dev_list;
187 BT_DBG("id %d channel %d", req->dev_id, req->channel);
189 dev = kzalloc(sizeof(struct rfcomm_dev), GFP_KERNEL);
193 spin_lock(&rfcomm_dev_lock);
195 if (req->dev_id < 0) {
198 list_for_each_entry(entry, &rfcomm_dev_list, list) {
199 if (entry->id != dev->id)
206 dev->id = req->dev_id;
208 list_for_each_entry(entry, &rfcomm_dev_list, list) {
209 if (entry->id == dev->id) {
214 if (entry->id > dev->id - 1)
221 if ((dev->id < 0) || (dev->id > RFCOMM_MAX_DEV - 1)) {
226 sprintf(dev->name, "rfcomm%d", dev->id);
228 list_add(&dev->list, head);
230 bacpy(&dev->src, &req->src);
231 bacpy(&dev->dst, &req->dst);
232 dev->channel = req->channel;
234 dev->flags = req->flags &
235 ((1 << RFCOMM_RELEASE_ONHUP) | (1 << RFCOMM_REUSE_DLC));
237 tty_port_init(&dev->port);
238 dev->port.ops = &rfcomm_port_ops;
239 init_waitqueue_head(&dev->wait);
241 skb_queue_head_init(&dev->pending);
243 rfcomm_dlc_lock(dlc);
245 if (req->flags & (1 << RFCOMM_REUSE_DLC)) {
246 struct sock *sk = dlc->owner;
251 rfcomm_dlc_throttle(dlc);
253 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
255 skb_queue_tail(&dev->pending, skb);
256 atomic_sub(skb->len, &sk->sk_rmem_alloc);
260 dlc->data_ready = rfcomm_dev_data_ready;
261 dlc->state_change = rfcomm_dev_state_change;
262 dlc->modem_status = rfcomm_dev_modem_status;
267 rfcomm_dev_modem_status(dlc, dlc->remote_v24_sig);
269 rfcomm_dlc_unlock(dlc);
271 /* It's safe to call __module_get() here because socket already
272 holds reference to this module. */
273 __module_get(THIS_MODULE);
276 spin_unlock(&rfcomm_dev_lock);
281 dev->tty_dev = tty_port_register_device(&dev->port, rfcomm_tty_driver,
283 if (IS_ERR(dev->tty_dev)) {
284 err = PTR_ERR(dev->tty_dev);
285 list_del(&dev->list);
289 dev_set_drvdata(dev->tty_dev, dev);
291 if (device_create_file(dev->tty_dev, &dev_attr_address) < 0)
292 BT_ERR("Failed to create address attribute");
294 if (device_create_file(dev->tty_dev, &dev_attr_channel) < 0)
295 BT_ERR("Failed to create channel attribute");
304 static void rfcomm_dev_del(struct rfcomm_dev *dev)
307 BT_DBG("dev %p", dev);
309 BUG_ON(test_and_set_bit(RFCOMM_TTY_RELEASED, &dev->flags));
311 spin_lock_irqsave(&dev->port.lock, flags);
312 if (dev->port.count > 0) {
313 spin_unlock_irqrestore(&dev->port.lock, flags);
316 spin_unlock_irqrestore(&dev->port.lock, flags);
318 spin_lock(&rfcomm_dev_lock);
319 list_del_init(&dev->list);
320 spin_unlock(&rfcomm_dev_lock);
322 tty_port_put(&dev->port);
325 /* ---- Send buffer ---- */
326 static inline unsigned int rfcomm_room(struct rfcomm_dlc *dlc)
328 /* We can't let it be zero, because we don't get a callback
329 when tx_credits becomes nonzero, hence we'd never wake up */
330 return dlc->mtu * (dlc->tx_credits?:1);
333 static void rfcomm_wfree(struct sk_buff *skb)
335 struct rfcomm_dev *dev = (void *) skb->sk;
336 struct tty_struct *tty = dev->port.tty;
337 atomic_sub(skb->truesize, &dev->wmem_alloc);
338 if (test_bit(RFCOMM_TTY_ATTACHED, &dev->flags) && tty)
340 tty_port_put(&dev->port);
343 static void rfcomm_set_owner_w(struct sk_buff *skb, struct rfcomm_dev *dev)
345 tty_port_get(&dev->port);
346 atomic_add(skb->truesize, &dev->wmem_alloc);
347 skb->sk = (void *) dev;
348 skb->destructor = rfcomm_wfree;
351 static struct sk_buff *rfcomm_wmalloc(struct rfcomm_dev *dev, unsigned long size, gfp_t priority)
353 if (atomic_read(&dev->wmem_alloc) < rfcomm_room(dev->dlc)) {
354 struct sk_buff *skb = alloc_skb(size, priority);
356 rfcomm_set_owner_w(skb, dev);
363 /* ---- Device IOCTLs ---- */
365 #define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP))
367 static int rfcomm_create_dev(struct sock *sk, void __user *arg)
369 struct rfcomm_dev_req req;
370 struct rfcomm_dlc *dlc;
373 if (copy_from_user(&req, arg, sizeof(req)))
376 BT_DBG("sk %p dev_id %d flags 0x%x", sk, req.dev_id, req.flags);
378 if (req.flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN))
381 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
382 /* Socket must be connected */
383 if (sk->sk_state != BT_CONNECTED)
386 dlc = rfcomm_pi(sk)->dlc;
387 rfcomm_dlc_hold(dlc);
389 dlc = rfcomm_dlc_alloc(GFP_KERNEL);
394 id = rfcomm_dev_add(&req, dlc);
400 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
401 /* DLC is now used by device.
402 * Socket must be disconnected */
403 sk->sk_state = BT_CLOSED;
409 static int rfcomm_release_dev(void __user *arg)
411 struct rfcomm_dev_req req;
412 struct rfcomm_dev *dev;
414 if (copy_from_user(&req, arg, sizeof(req)))
417 BT_DBG("dev_id %d flags 0x%x", req.dev_id, req.flags);
419 dev = rfcomm_dev_get(req.dev_id);
423 if (dev->flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN)) {
424 tty_port_put(&dev->port);
428 if (req.flags & (1 << RFCOMM_HANGUP_NOW))
429 rfcomm_dlc_close(dev->dlc, 0);
431 /* Shut down TTY synchronously before freeing rfcomm_dev */
433 tty_vhangup(dev->port.tty);
435 if (!test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags))
437 tty_port_put(&dev->port);
441 static int rfcomm_get_dev_list(void __user *arg)
443 struct rfcomm_dev *dev;
444 struct rfcomm_dev_list_req *dl;
445 struct rfcomm_dev_info *di;
446 int n = 0, size, err;
451 if (get_user(dev_num, (u16 __user *) arg))
454 if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di))
457 size = sizeof(*dl) + dev_num * sizeof(*di);
459 dl = kzalloc(size, GFP_KERNEL);
465 spin_lock(&rfcomm_dev_lock);
467 list_for_each_entry(dev, &rfcomm_dev_list, list) {
468 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
470 (di + n)->id = dev->id;
471 (di + n)->flags = dev->flags;
472 (di + n)->state = dev->dlc->state;
473 (di + n)->channel = dev->channel;
474 bacpy(&(di + n)->src, &dev->src);
475 bacpy(&(di + n)->dst, &dev->dst);
480 spin_unlock(&rfcomm_dev_lock);
483 size = sizeof(*dl) + n * sizeof(*di);
485 err = copy_to_user(arg, dl, size);
488 return err ? -EFAULT : 0;
491 static int rfcomm_get_dev_info(void __user *arg)
493 struct rfcomm_dev *dev;
494 struct rfcomm_dev_info di;
499 if (copy_from_user(&di, arg, sizeof(di)))
502 dev = rfcomm_dev_get(di.id);
506 di.flags = dev->flags;
507 di.channel = dev->channel;
508 di.state = dev->dlc->state;
509 bacpy(&di.src, &dev->src);
510 bacpy(&di.dst, &dev->dst);
512 if (copy_to_user(arg, &di, sizeof(di)))
515 tty_port_put(&dev->port);
519 int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
521 BT_DBG("cmd %d arg %p", cmd, arg);
524 case RFCOMMCREATEDEV:
525 return rfcomm_create_dev(sk, arg);
527 case RFCOMMRELEASEDEV:
528 return rfcomm_release_dev(arg);
530 case RFCOMMGETDEVLIST:
531 return rfcomm_get_dev_list(arg);
533 case RFCOMMGETDEVINFO:
534 return rfcomm_get_dev_info(arg);
540 /* ---- DLC callbacks ---- */
541 static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb)
543 struct rfcomm_dev *dev = dlc->owner;
544 struct tty_struct *tty;
552 if (!tty || !skb_queue_empty(&dev->pending)) {
553 skb_queue_tail(&dev->pending, skb);
557 BT_DBG("dlc %p tty %p len %d", dlc, tty, skb->len);
559 tty_insert_flip_string(&dev->port, skb->data, skb->len);
560 tty_flip_buffer_push(tty);
565 static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
567 struct rfcomm_dev *dev = dlc->owner;
571 BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
574 wake_up_interruptible(&dev->wait);
576 if (dlc->state == BT_CLOSED) {
577 if (!dev->port.tty) {
578 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
579 /* Drop DLC lock here to avoid deadlock
580 * 1. rfcomm_dev_get will take rfcomm_dev_lock
581 * but in rfcomm_dev_add there's lock order:
582 * rfcomm_dev_lock -> dlc lock
583 * 2. tty_port_put will deadlock if it's
586 rfcomm_dlc_unlock(dlc);
587 if (rfcomm_dev_get(dev->id) == NULL) {
588 rfcomm_dlc_lock(dlc);
593 tty_port_put(&dev->port);
594 rfcomm_dlc_lock(dlc);
597 tty_hangup(dev->port.tty);
601 static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig)
603 struct rfcomm_dev *dev = dlc->owner;
607 BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc, dev, v24_sig);
609 if ((dev->modem_status & TIOCM_CD) && !(v24_sig & RFCOMM_V24_DV)) {
610 if (dev->port.tty && !C_CLOCAL(dev->port.tty))
611 tty_hangup(dev->port.tty);
615 ((v24_sig & RFCOMM_V24_RTC) ? (TIOCM_DSR | TIOCM_DTR) : 0) |
616 ((v24_sig & RFCOMM_V24_RTR) ? (TIOCM_RTS | TIOCM_CTS) : 0) |
617 ((v24_sig & RFCOMM_V24_IC) ? TIOCM_RI : 0) |
618 ((v24_sig & RFCOMM_V24_DV) ? TIOCM_CD : 0);
621 /* ---- TTY functions ---- */
622 static void rfcomm_tty_copy_pending(struct rfcomm_dev *dev)
624 struct tty_struct *tty = dev->port.tty;
631 BT_DBG("dev %p tty %p", dev, tty);
633 rfcomm_dlc_lock(dev->dlc);
635 while ((skb = skb_dequeue(&dev->pending))) {
636 inserted += tty_insert_flip_string(&dev->port, skb->data,
641 rfcomm_dlc_unlock(dev->dlc);
644 tty_flip_buffer_push(tty);
647 static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
649 DECLARE_WAITQUEUE(wait, current);
650 struct rfcomm_dev *dev;
651 struct rfcomm_dlc *dlc;
657 BT_DBG("tty %p id %d", tty, id);
659 /* We don't leak this refcount. For reasons which are not entirely
660 clear, the TTY layer will call our ->close() method even if the
661 open fails. We decrease the refcount there, and decreasing it
662 here too would cause breakage. */
663 dev = rfcomm_dev_get(id);
667 BT_DBG("dev %p dst %pMR channel %d opened %d", dev, &dev->dst,
668 dev->channel, dev->port.count);
670 spin_lock_irqsave(&dev->port.lock, flags);
671 if (++dev->port.count > 1) {
672 spin_unlock_irqrestore(&dev->port.lock, flags);
675 spin_unlock_irqrestore(&dev->port.lock, flags);
679 /* Attach TTY and open DLC */
681 rfcomm_dlc_lock(dlc);
682 tty->driver_data = dev;
684 rfcomm_dlc_unlock(dlc);
685 set_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
687 err = rfcomm_dlc_open(dlc, &dev->src, &dev->dst, dev->channel);
691 /* Wait for DLC to connect */
692 add_wait_queue(&dev->wait, &wait);
694 set_current_state(TASK_INTERRUPTIBLE);
696 if (dlc->state == BT_CLOSED) {
701 if (dlc->state == BT_CONNECTED)
704 if (signal_pending(current)) {
713 set_current_state(TASK_RUNNING);
714 remove_wait_queue(&dev->wait, &wait);
717 device_move(dev->tty_dev, rfcomm_get_device(dev),
718 DPM_ORDER_DEV_AFTER_PARENT);
720 rfcomm_tty_copy_pending(dev);
722 rfcomm_dlc_unthrottle(dev->dlc);
727 static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
729 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
735 BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc,
738 spin_lock_irqsave(&dev->port.lock, flags);
739 if (!--dev->port.count) {
740 spin_unlock_irqrestore(&dev->port.lock, flags);
741 if (dev->tty_dev->parent)
742 device_move(dev->tty_dev, NULL, DPM_ORDER_DEV_LAST);
744 /* Close DLC and dettach TTY */
745 rfcomm_dlc_close(dev->dlc, 0);
747 clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
749 rfcomm_dlc_lock(dev->dlc);
750 tty->driver_data = NULL;
751 dev->port.tty = NULL;
752 rfcomm_dlc_unlock(dev->dlc);
754 if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags)) {
755 spin_lock(&rfcomm_dev_lock);
756 list_del_init(&dev->list);
757 spin_unlock(&rfcomm_dev_lock);
759 tty_port_put(&dev->port);
762 spin_unlock_irqrestore(&dev->port.lock, flags);
764 tty_port_put(&dev->port);
767 static int rfcomm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
769 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
770 struct rfcomm_dlc *dlc = dev->dlc;
772 int err = 0, sent = 0, size;
774 BT_DBG("tty %p count %d", tty, count);
777 size = min_t(uint, count, dlc->mtu);
779 skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, GFP_ATOMIC);
784 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
786 memcpy(skb_put(skb, size), buf + sent, size);
788 err = rfcomm_dlc_send(dlc, skb);
798 return sent ? sent : err;
801 static int rfcomm_tty_write_room(struct tty_struct *tty)
803 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
806 BT_DBG("tty %p", tty);
808 if (!dev || !dev->dlc)
811 room = rfcomm_room(dev->dlc) - atomic_read(&dev->wmem_alloc);
818 static int rfcomm_tty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
820 BT_DBG("tty %p cmd 0x%02x", tty, cmd);
824 BT_DBG("TCGETS is not supported");
828 BT_DBG("TCSETS is not supported");
832 BT_DBG("TIOCMIWAIT");
836 BT_ERR("TIOCGSERIAL is not supported");
840 BT_ERR("TIOCSSERIAL is not supported");
844 BT_ERR("TIOCSERGSTRUCT is not supported");
848 BT_ERR("TIOCSERGETLSR is not supported");
852 BT_ERR("TIOCSERCONFIG is not supported");
856 return -ENOIOCTLCMD; /* ioctls which we must ignore */
863 static void rfcomm_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
865 struct ktermios *new = &tty->termios;
866 int old_baud_rate = tty_termios_baud_rate(old);
867 int new_baud_rate = tty_termios_baud_rate(new);
869 u8 baud, data_bits, stop_bits, parity, x_on, x_off;
872 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
874 BT_DBG("tty %p termios %p", tty, old);
876 if (!dev || !dev->dlc || !dev->dlc->session)
879 /* Handle turning off CRTSCTS */
880 if ((old->c_cflag & CRTSCTS) && !(new->c_cflag & CRTSCTS))
881 BT_DBG("Turning off CRTSCTS unsupported");
883 /* Parity on/off and when on, odd/even */
884 if (((old->c_cflag & PARENB) != (new->c_cflag & PARENB)) ||
885 ((old->c_cflag & PARODD) != (new->c_cflag & PARODD))) {
886 changes |= RFCOMM_RPN_PM_PARITY;
887 BT_DBG("Parity change detected.");
890 /* Mark and space parity are not supported! */
891 if (new->c_cflag & PARENB) {
892 if (new->c_cflag & PARODD) {
893 BT_DBG("Parity is ODD");
894 parity = RFCOMM_RPN_PARITY_ODD;
896 BT_DBG("Parity is EVEN");
897 parity = RFCOMM_RPN_PARITY_EVEN;
900 BT_DBG("Parity is OFF");
901 parity = RFCOMM_RPN_PARITY_NONE;
904 /* Setting the x_on / x_off characters */
905 if (old->c_cc[VSTOP] != new->c_cc[VSTOP]) {
906 BT_DBG("XOFF custom");
907 x_on = new->c_cc[VSTOP];
908 changes |= RFCOMM_RPN_PM_XON;
910 BT_DBG("XOFF default");
911 x_on = RFCOMM_RPN_XON_CHAR;
914 if (old->c_cc[VSTART] != new->c_cc[VSTART]) {
915 BT_DBG("XON custom");
916 x_off = new->c_cc[VSTART];
917 changes |= RFCOMM_RPN_PM_XOFF;
919 BT_DBG("XON default");
920 x_off = RFCOMM_RPN_XOFF_CHAR;
923 /* Handle setting of stop bits */
924 if ((old->c_cflag & CSTOPB) != (new->c_cflag & CSTOPB))
925 changes |= RFCOMM_RPN_PM_STOP;
927 /* POSIX does not support 1.5 stop bits and RFCOMM does not
928 * support 2 stop bits. So a request for 2 stop bits gets
929 * translated to 1.5 stop bits */
930 if (new->c_cflag & CSTOPB)
931 stop_bits = RFCOMM_RPN_STOP_15;
933 stop_bits = RFCOMM_RPN_STOP_1;
935 /* Handle number of data bits [5-8] */
936 if ((old->c_cflag & CSIZE) != (new->c_cflag & CSIZE))
937 changes |= RFCOMM_RPN_PM_DATA;
939 switch (new->c_cflag & CSIZE) {
941 data_bits = RFCOMM_RPN_DATA_5;
944 data_bits = RFCOMM_RPN_DATA_6;
947 data_bits = RFCOMM_RPN_DATA_7;
950 data_bits = RFCOMM_RPN_DATA_8;
953 data_bits = RFCOMM_RPN_DATA_8;
957 /* Handle baudrate settings */
958 if (old_baud_rate != new_baud_rate)
959 changes |= RFCOMM_RPN_PM_BITRATE;
961 switch (new_baud_rate) {
963 baud = RFCOMM_RPN_BR_2400;
966 baud = RFCOMM_RPN_BR_4800;
969 baud = RFCOMM_RPN_BR_7200;
972 baud = RFCOMM_RPN_BR_9600;
975 baud = RFCOMM_RPN_BR_19200;
978 baud = RFCOMM_RPN_BR_38400;
981 baud = RFCOMM_RPN_BR_57600;
984 baud = RFCOMM_RPN_BR_115200;
987 baud = RFCOMM_RPN_BR_230400;
990 /* 9600 is standard accordinag to the RFCOMM specification */
991 baud = RFCOMM_RPN_BR_9600;
997 rfcomm_send_rpn(dev->dlc->session, 1, dev->dlc->dlci, baud,
998 data_bits, stop_bits, parity,
999 RFCOMM_RPN_FLOW_NONE, x_on, x_off, changes);
1002 static void rfcomm_tty_throttle(struct tty_struct *tty)
1004 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1006 BT_DBG("tty %p dev %p", tty, dev);
1008 rfcomm_dlc_throttle(dev->dlc);
1011 static void rfcomm_tty_unthrottle(struct tty_struct *tty)
1013 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1015 BT_DBG("tty %p dev %p", tty, dev);
1017 rfcomm_dlc_unthrottle(dev->dlc);
1020 static int rfcomm_tty_chars_in_buffer(struct tty_struct *tty)
1022 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1024 BT_DBG("tty %p dev %p", tty, dev);
1026 if (!dev || !dev->dlc)
1029 if (!skb_queue_empty(&dev->dlc->tx_queue))
1030 return dev->dlc->mtu;
1035 static void rfcomm_tty_flush_buffer(struct tty_struct *tty)
1037 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1039 BT_DBG("tty %p dev %p", tty, dev);
1041 if (!dev || !dev->dlc)
1044 skb_queue_purge(&dev->dlc->tx_queue);
1048 static void rfcomm_tty_send_xchar(struct tty_struct *tty, char ch)
1050 BT_DBG("tty %p ch %c", tty, ch);
1053 static void rfcomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
1055 BT_DBG("tty %p timeout %d", tty, timeout);
1058 static void rfcomm_tty_hangup(struct tty_struct *tty)
1060 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1062 BT_DBG("tty %p dev %p", tty, dev);
1067 rfcomm_tty_flush_buffer(tty);
1069 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
1070 if (rfcomm_dev_get(dev->id) == NULL)
1072 rfcomm_dev_del(dev);
1073 tty_port_put(&dev->port);
1077 static int rfcomm_tty_tiocmget(struct tty_struct *tty)
1079 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1081 BT_DBG("tty %p dev %p", tty, dev);
1083 return dev->modem_status;
1086 static int rfcomm_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
1088 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1089 struct rfcomm_dlc *dlc = dev->dlc;
1092 BT_DBG("tty %p dev %p set 0x%02x clear 0x%02x", tty, dev, set, clear);
1094 rfcomm_dlc_get_modem_status(dlc, &v24_sig);
1096 if (set & TIOCM_DSR || set & TIOCM_DTR)
1097 v24_sig |= RFCOMM_V24_RTC;
1098 if (set & TIOCM_RTS || set & TIOCM_CTS)
1099 v24_sig |= RFCOMM_V24_RTR;
1101 v24_sig |= RFCOMM_V24_IC;
1103 v24_sig |= RFCOMM_V24_DV;
1105 if (clear & TIOCM_DSR || clear & TIOCM_DTR)
1106 v24_sig &= ~RFCOMM_V24_RTC;
1107 if (clear & TIOCM_RTS || clear & TIOCM_CTS)
1108 v24_sig &= ~RFCOMM_V24_RTR;
1109 if (clear & TIOCM_RI)
1110 v24_sig &= ~RFCOMM_V24_IC;
1111 if (clear & TIOCM_CD)
1112 v24_sig &= ~RFCOMM_V24_DV;
1114 rfcomm_dlc_set_modem_status(dlc, v24_sig);
1119 /* ---- TTY structure ---- */
1121 static const struct tty_operations rfcomm_ops = {
1122 .open = rfcomm_tty_open,
1123 .close = rfcomm_tty_close,
1124 .write = rfcomm_tty_write,
1125 .write_room = rfcomm_tty_write_room,
1126 .chars_in_buffer = rfcomm_tty_chars_in_buffer,
1127 .flush_buffer = rfcomm_tty_flush_buffer,
1128 .ioctl = rfcomm_tty_ioctl,
1129 .throttle = rfcomm_tty_throttle,
1130 .unthrottle = rfcomm_tty_unthrottle,
1131 .set_termios = rfcomm_tty_set_termios,
1132 .send_xchar = rfcomm_tty_send_xchar,
1133 .hangup = rfcomm_tty_hangup,
1134 .wait_until_sent = rfcomm_tty_wait_until_sent,
1135 .tiocmget = rfcomm_tty_tiocmget,
1136 .tiocmset = rfcomm_tty_tiocmset,
1139 int __init rfcomm_init_ttys(void)
1143 rfcomm_tty_driver = alloc_tty_driver(RFCOMM_TTY_PORTS);
1144 if (!rfcomm_tty_driver)
1147 rfcomm_tty_driver->driver_name = "rfcomm";
1148 rfcomm_tty_driver->name = "rfcomm";
1149 rfcomm_tty_driver->major = RFCOMM_TTY_MAJOR;
1150 rfcomm_tty_driver->minor_start = RFCOMM_TTY_MINOR;
1151 rfcomm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1152 rfcomm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1153 rfcomm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1154 rfcomm_tty_driver->init_termios = tty_std_termios;
1155 rfcomm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1156 rfcomm_tty_driver->init_termios.c_lflag &= ~ICANON;
1157 tty_set_operations(rfcomm_tty_driver, &rfcomm_ops);
1159 error = tty_register_driver(rfcomm_tty_driver);
1161 BT_ERR("Can't register RFCOMM TTY driver");
1162 put_tty_driver(rfcomm_tty_driver);
1166 BT_INFO("RFCOMM TTY layer initialized");
1171 void rfcomm_cleanup_ttys(void)
1173 tty_unregister_driver(rfcomm_tty_driver);
1174 put_tty_driver(rfcomm_tty_driver);