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;
54 unsigned long status; /* don't export to userspace */
62 struct rfcomm_dlc *dlc;
64 struct device *tty_dev;
68 struct sk_buff_head pending;
71 static LIST_HEAD(rfcomm_dev_list);
72 static DEFINE_SPINLOCK(rfcomm_dev_lock);
74 static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb);
75 static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err);
76 static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig);
78 /* ---- Device functions ---- */
80 static void rfcomm_dev_destruct(struct tty_port *port)
82 struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
83 struct rfcomm_dlc *dlc = dev->dlc;
85 BT_DBG("dev %p dlc %p", dev, dlc);
88 /* Detach DLC if it's owned by this dev */
89 if (dlc->owner == dev)
91 rfcomm_dlc_unlock(dlc);
95 tty_unregister_device(rfcomm_tty_driver, dev->id);
97 spin_lock(&rfcomm_dev_lock);
99 spin_unlock(&rfcomm_dev_lock);
103 /* It's safe to call module_put() here because socket still
104 holds reference to this module. */
105 module_put(THIS_MODULE);
108 /* device-specific initialization: open the dlc */
109 static int rfcomm_dev_activate(struct tty_port *port, struct tty_struct *tty)
111 struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
113 return rfcomm_dlc_open(dev->dlc, &dev->src, &dev->dst, dev->channel);
116 /* we block the open until the dlc->state becomes BT_CONNECTED */
117 static int rfcomm_dev_carrier_raised(struct tty_port *port)
119 struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
121 return (dev->dlc->state == BT_CONNECTED);
124 /* device-specific cleanup: close the dlc */
125 static void rfcomm_dev_shutdown(struct tty_port *port)
127 struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
129 if (dev->tty_dev->parent)
130 device_move(dev->tty_dev, NULL, DPM_ORDER_DEV_LAST);
133 rfcomm_dlc_close(dev->dlc, 0);
136 static const struct tty_port_operations rfcomm_port_ops = {
137 .destruct = rfcomm_dev_destruct,
138 .activate = rfcomm_dev_activate,
139 .shutdown = rfcomm_dev_shutdown,
140 .carrier_raised = rfcomm_dev_carrier_raised,
143 static struct rfcomm_dev *__rfcomm_dev_lookup(int id)
145 struct rfcomm_dev *dev;
147 list_for_each_entry(dev, &rfcomm_dev_list, list)
154 static struct rfcomm_dev *rfcomm_dev_get(int id)
156 struct rfcomm_dev *dev;
158 spin_lock(&rfcomm_dev_lock);
160 dev = __rfcomm_dev_lookup(id);
162 if (dev && !tty_port_get(&dev->port))
165 spin_unlock(&rfcomm_dev_lock);
170 static void rfcomm_reparent_device(struct rfcomm_dev *dev)
172 struct hci_dev *hdev;
173 struct hci_conn *conn;
175 hdev = hci_get_route(&dev->dst, &dev->src);
179 /* The lookup results are unsafe to access without the
180 * hci device lock (FIXME: why is this not documented?)
183 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &dev->dst);
185 /* Just because the acl link is in the hash table is no
186 * guarantee the sysfs device has been added ...
188 if (conn && device_is_registered(&conn->dev))
189 device_move(dev->tty_dev, &conn->dev, DPM_ORDER_DEV_AFTER_PARENT);
191 hci_dev_unlock(hdev);
195 static ssize_t show_address(struct device *tty_dev, struct device_attribute *attr, char *buf)
197 struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
198 return sprintf(buf, "%pMR\n", &dev->dst);
201 static ssize_t show_channel(struct device *tty_dev, struct device_attribute *attr, char *buf)
203 struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
204 return sprintf(buf, "%d\n", dev->channel);
207 static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
208 static DEVICE_ATTR(channel, S_IRUGO, show_channel, NULL);
210 static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
212 struct rfcomm_dev *dev, *entry;
213 struct list_head *head = &rfcomm_dev_list;
216 BT_DBG("id %d channel %d", req->dev_id, req->channel);
218 dev = kzalloc(sizeof(struct rfcomm_dev), GFP_KERNEL);
222 spin_lock(&rfcomm_dev_lock);
224 if (req->dev_id < 0) {
227 list_for_each_entry(entry, &rfcomm_dev_list, list) {
228 if (entry->id != dev->id)
235 dev->id = req->dev_id;
237 list_for_each_entry(entry, &rfcomm_dev_list, list) {
238 if (entry->id == dev->id) {
243 if (entry->id > dev->id - 1)
250 if ((dev->id < 0) || (dev->id > RFCOMM_MAX_DEV - 1)) {
255 sprintf(dev->name, "rfcomm%d", dev->id);
257 list_add(&dev->list, head);
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 tty_port_init(&dev->port);
267 dev->port.ops = &rfcomm_port_ops;
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 spin_unlock(&rfcomm_dev_lock);
309 dev->tty_dev = tty_port_register_device(&dev->port, rfcomm_tty_driver,
311 if (IS_ERR(dev->tty_dev)) {
312 err = PTR_ERR(dev->tty_dev);
313 spin_lock(&rfcomm_dev_lock);
314 list_del(&dev->list);
315 spin_unlock(&rfcomm_dev_lock);
319 rfcomm_reparent_device(dev);
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");
335 /* ---- Send buffer ---- */
336 static inline unsigned int rfcomm_room(struct rfcomm_dlc *dlc)
338 /* We can't let it be zero, because we don't get a callback
339 when tx_credits becomes nonzero, hence we'd never wake up */
340 return dlc->mtu * (dlc->tx_credits?:1);
343 static void rfcomm_wfree(struct sk_buff *skb)
345 struct rfcomm_dev *dev = (void *) skb->sk;
346 atomic_sub(skb->truesize, &dev->wmem_alloc);
347 if (test_bit(RFCOMM_TTY_ATTACHED, &dev->flags))
348 tty_port_tty_wakeup(&dev->port);
349 tty_port_put(&dev->port);
352 static void rfcomm_set_owner_w(struct sk_buff *skb, struct rfcomm_dev *dev)
354 tty_port_get(&dev->port);
355 atomic_add(skb->truesize, &dev->wmem_alloc);
356 skb->sk = (void *) dev;
357 skb->destructor = rfcomm_wfree;
360 static struct sk_buff *rfcomm_wmalloc(struct rfcomm_dev *dev, unsigned long size, gfp_t priority)
362 if (atomic_read(&dev->wmem_alloc) < rfcomm_room(dev->dlc)) {
363 struct sk_buff *skb = alloc_skb(size, priority);
365 rfcomm_set_owner_w(skb, dev);
372 /* ---- Device IOCTLs ---- */
374 #define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP))
376 static int rfcomm_create_dev(struct sock *sk, void __user *arg)
378 struct rfcomm_dev_req req;
379 struct rfcomm_dlc *dlc;
382 if (copy_from_user(&req, arg, sizeof(req)))
385 BT_DBG("sk %p dev_id %d flags 0x%x", sk, req.dev_id, req.flags);
387 if (req.flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN))
390 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
391 /* Socket must be connected */
392 if (sk->sk_state != BT_CONNECTED)
395 dlc = rfcomm_pi(sk)->dlc;
396 rfcomm_dlc_hold(dlc);
398 /* Validate the channel is unused */
399 dlc = rfcomm_dlc_exists(&req.src, &req.dst, req.channel);
406 dlc = rfcomm_dlc_alloc(GFP_KERNEL);
411 id = rfcomm_dev_add(&req, dlc);
417 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
418 /* DLC is now used by device.
419 * Socket must be disconnected */
420 sk->sk_state = BT_CLOSED;
426 static int rfcomm_release_dev(void __user *arg)
428 struct rfcomm_dev_req req;
429 struct rfcomm_dev *dev;
430 struct tty_struct *tty;
432 if (copy_from_user(&req, arg, sizeof(req)))
435 BT_DBG("dev_id %d flags 0x%x", req.dev_id, req.flags);
437 dev = rfcomm_dev_get(req.dev_id);
441 if (dev->flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN)) {
442 tty_port_put(&dev->port);
446 /* only release once */
447 if (test_and_set_bit(RFCOMM_DEV_RELEASED, &dev->status)) {
448 tty_port_put(&dev->port);
452 if (req.flags & (1 << RFCOMM_HANGUP_NOW))
453 rfcomm_dlc_close(dev->dlc, 0);
455 /* Shut down TTY synchronously before freeing rfcomm_dev */
456 tty = tty_port_tty_get(&dev->port);
462 if (!test_bit(RFCOMM_TTY_OWNED, &dev->status))
463 tty_port_put(&dev->port);
465 tty_port_put(&dev->port);
469 static int rfcomm_get_dev_list(void __user *arg)
471 struct rfcomm_dev *dev;
472 struct rfcomm_dev_list_req *dl;
473 struct rfcomm_dev_info *di;
474 int n = 0, size, err;
479 if (get_user(dev_num, (u16 __user *) arg))
482 if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di))
485 size = sizeof(*dl) + dev_num * sizeof(*di);
487 dl = kzalloc(size, GFP_KERNEL);
493 spin_lock(&rfcomm_dev_lock);
495 list_for_each_entry(dev, &rfcomm_dev_list, list) {
496 if (!tty_port_get(&dev->port))
498 (di + n)->id = dev->id;
499 (di + n)->flags = dev->flags;
500 (di + n)->state = dev->dlc->state;
501 (di + n)->channel = dev->channel;
502 bacpy(&(di + n)->src, &dev->src);
503 bacpy(&(di + n)->dst, &dev->dst);
504 tty_port_put(&dev->port);
509 spin_unlock(&rfcomm_dev_lock);
512 size = sizeof(*dl) + n * sizeof(*di);
514 err = copy_to_user(arg, dl, size);
517 return err ? -EFAULT : 0;
520 static int rfcomm_get_dev_info(void __user *arg)
522 struct rfcomm_dev *dev;
523 struct rfcomm_dev_info di;
528 if (copy_from_user(&di, arg, sizeof(di)))
531 dev = rfcomm_dev_get(di.id);
535 di.flags = dev->flags;
536 di.channel = dev->channel;
537 di.state = dev->dlc->state;
538 bacpy(&di.src, &dev->src);
539 bacpy(&di.dst, &dev->dst);
541 if (copy_to_user(arg, &di, sizeof(di)))
544 tty_port_put(&dev->port);
548 int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
550 BT_DBG("cmd %d arg %p", cmd, arg);
553 case RFCOMMCREATEDEV:
554 return rfcomm_create_dev(sk, arg);
556 case RFCOMMRELEASEDEV:
557 return rfcomm_release_dev(arg);
559 case RFCOMMGETDEVLIST:
560 return rfcomm_get_dev_list(arg);
562 case RFCOMMGETDEVINFO:
563 return rfcomm_get_dev_info(arg);
569 /* ---- DLC callbacks ---- */
570 static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb)
572 struct rfcomm_dev *dev = dlc->owner;
579 if (!skb_queue_empty(&dev->pending)) {
580 skb_queue_tail(&dev->pending, skb);
584 BT_DBG("dlc %p len %d", dlc, skb->len);
586 tty_insert_flip_string(&dev->port, skb->data, skb->len);
587 tty_flip_buffer_push(&dev->port);
592 static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
594 struct rfcomm_dev *dev = dlc->owner;
598 BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
601 if (dlc->state == BT_CONNECTED) {
602 rfcomm_reparent_device(dev);
604 wake_up_interruptible(&dev->port.open_wait);
605 } else if (dlc->state == BT_CLOSED)
606 tty_port_tty_hangup(&dev->port, false);
609 static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig)
611 struct rfcomm_dev *dev = dlc->owner;
615 BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc, dev, v24_sig);
617 if ((dev->modem_status & TIOCM_CD) && !(v24_sig & RFCOMM_V24_DV))
618 tty_port_tty_hangup(&dev->port, true);
621 ((v24_sig & RFCOMM_V24_RTC) ? (TIOCM_DSR | TIOCM_DTR) : 0) |
622 ((v24_sig & RFCOMM_V24_RTR) ? (TIOCM_RTS | TIOCM_CTS) : 0) |
623 ((v24_sig & RFCOMM_V24_IC) ? TIOCM_RI : 0) |
624 ((v24_sig & RFCOMM_V24_DV) ? TIOCM_CD : 0);
627 /* ---- TTY functions ---- */
628 static void rfcomm_tty_copy_pending(struct rfcomm_dev *dev)
633 BT_DBG("dev %p", dev);
635 rfcomm_dlc_lock(dev->dlc);
637 while ((skb = skb_dequeue(&dev->pending))) {
638 inserted += tty_insert_flip_string(&dev->port, skb->data,
643 rfcomm_dlc_unlock(dev->dlc);
646 tty_flip_buffer_push(&dev->port);
649 /* do the reverse of install, clearing the tty fields and releasing the
650 * reference to tty_port
652 static void rfcomm_tty_cleanup(struct tty_struct *tty)
654 struct rfcomm_dev *dev = tty->driver_data;
656 clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
658 rfcomm_dlc_lock(dev->dlc);
659 tty->driver_data = NULL;
660 rfcomm_dlc_unlock(dev->dlc);
663 * purge the dlc->tx_queue to avoid circular dependencies
664 * between dev and dlc
666 skb_queue_purge(&dev->dlc->tx_queue);
668 tty_port_put(&dev->port);
671 /* we acquire the tty_port reference since it's here the tty is first used
672 * by setting the termios. We also populate the driver_data field and install
675 static int rfcomm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
677 struct rfcomm_dev *dev;
678 struct rfcomm_dlc *dlc;
681 dev = rfcomm_dev_get(tty->index);
687 /* Attach TTY and open DLC */
688 rfcomm_dlc_lock(dlc);
689 tty->driver_data = dev;
690 rfcomm_dlc_unlock(dlc);
691 set_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
693 /* install the tty_port */
694 err = tty_port_install(&dev->port, driver, tty);
696 rfcomm_tty_cleanup(tty);
700 /* take over the tty_port reference if the port was created with the
701 * flag RFCOMM_RELEASE_ONHUP. This will force the release of the port
702 * when the last process closes the tty. The behaviour is expected by
705 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
706 set_bit(RFCOMM_TTY_OWNED, &dev->status);
707 tty_port_put(&dev->port);
713 static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
715 struct rfcomm_dev *dev = tty->driver_data;
718 BT_DBG("tty %p id %d", tty, tty->index);
720 BT_DBG("dev %p dst %pMR channel %d opened %d", dev, &dev->dst,
721 dev->channel, dev->port.count);
723 err = tty_port_open(&dev->port, tty, filp);
728 * FIXME: rfcomm should use proper flow control for
729 * received data. This hack will be unnecessary and can
730 * be removed when that's implemented
732 rfcomm_tty_copy_pending(dev);
734 rfcomm_dlc_unthrottle(dev->dlc);
739 static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
741 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
743 BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc,
746 tty_port_close(&dev->port, tty, filp);
749 static int rfcomm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
751 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
752 struct rfcomm_dlc *dlc = dev->dlc;
754 int err = 0, sent = 0, size;
756 BT_DBG("tty %p count %d", tty, count);
759 size = min_t(uint, count, dlc->mtu);
761 skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, GFP_ATOMIC);
766 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
768 memcpy(skb_put(skb, size), buf + sent, size);
770 err = rfcomm_dlc_send(dlc, skb);
780 return sent ? sent : err;
783 static int rfcomm_tty_write_room(struct tty_struct *tty)
785 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
788 BT_DBG("tty %p", tty);
790 if (!dev || !dev->dlc)
793 room = rfcomm_room(dev->dlc) - atomic_read(&dev->wmem_alloc);
800 static int rfcomm_tty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
802 BT_DBG("tty %p cmd 0x%02x", tty, cmd);
806 BT_DBG("TCGETS is not supported");
810 BT_DBG("TCSETS is not supported");
814 BT_DBG("TIOCMIWAIT");
818 BT_ERR("TIOCGSERIAL is not supported");
822 BT_ERR("TIOCSSERIAL is not supported");
826 BT_ERR("TIOCSERGSTRUCT is not supported");
830 BT_ERR("TIOCSERGETLSR is not supported");
834 BT_ERR("TIOCSERCONFIG is not supported");
838 return -ENOIOCTLCMD; /* ioctls which we must ignore */
845 static void rfcomm_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
847 struct ktermios *new = &tty->termios;
848 int old_baud_rate = tty_termios_baud_rate(old);
849 int new_baud_rate = tty_termios_baud_rate(new);
851 u8 baud, data_bits, stop_bits, parity, x_on, x_off;
854 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
856 BT_DBG("tty %p termios %p", tty, old);
858 if (!dev || !dev->dlc || !dev->dlc->session)
861 /* Handle turning off CRTSCTS */
862 if ((old->c_cflag & CRTSCTS) && !(new->c_cflag & CRTSCTS))
863 BT_DBG("Turning off CRTSCTS unsupported");
865 /* Parity on/off and when on, odd/even */
866 if (((old->c_cflag & PARENB) != (new->c_cflag & PARENB)) ||
867 ((old->c_cflag & PARODD) != (new->c_cflag & PARODD))) {
868 changes |= RFCOMM_RPN_PM_PARITY;
869 BT_DBG("Parity change detected.");
872 /* Mark and space parity are not supported! */
873 if (new->c_cflag & PARENB) {
874 if (new->c_cflag & PARODD) {
875 BT_DBG("Parity is ODD");
876 parity = RFCOMM_RPN_PARITY_ODD;
878 BT_DBG("Parity is EVEN");
879 parity = RFCOMM_RPN_PARITY_EVEN;
882 BT_DBG("Parity is OFF");
883 parity = RFCOMM_RPN_PARITY_NONE;
886 /* Setting the x_on / x_off characters */
887 if (old->c_cc[VSTOP] != new->c_cc[VSTOP]) {
888 BT_DBG("XOFF custom");
889 x_on = new->c_cc[VSTOP];
890 changes |= RFCOMM_RPN_PM_XON;
892 BT_DBG("XOFF default");
893 x_on = RFCOMM_RPN_XON_CHAR;
896 if (old->c_cc[VSTART] != new->c_cc[VSTART]) {
897 BT_DBG("XON custom");
898 x_off = new->c_cc[VSTART];
899 changes |= RFCOMM_RPN_PM_XOFF;
901 BT_DBG("XON default");
902 x_off = RFCOMM_RPN_XOFF_CHAR;
905 /* Handle setting of stop bits */
906 if ((old->c_cflag & CSTOPB) != (new->c_cflag & CSTOPB))
907 changes |= RFCOMM_RPN_PM_STOP;
909 /* POSIX does not support 1.5 stop bits and RFCOMM does not
910 * support 2 stop bits. So a request for 2 stop bits gets
911 * translated to 1.5 stop bits */
912 if (new->c_cflag & CSTOPB)
913 stop_bits = RFCOMM_RPN_STOP_15;
915 stop_bits = RFCOMM_RPN_STOP_1;
917 /* Handle number of data bits [5-8] */
918 if ((old->c_cflag & CSIZE) != (new->c_cflag & CSIZE))
919 changes |= RFCOMM_RPN_PM_DATA;
921 switch (new->c_cflag & CSIZE) {
923 data_bits = RFCOMM_RPN_DATA_5;
926 data_bits = RFCOMM_RPN_DATA_6;
929 data_bits = RFCOMM_RPN_DATA_7;
932 data_bits = RFCOMM_RPN_DATA_8;
935 data_bits = RFCOMM_RPN_DATA_8;
939 /* Handle baudrate settings */
940 if (old_baud_rate != new_baud_rate)
941 changes |= RFCOMM_RPN_PM_BITRATE;
943 switch (new_baud_rate) {
945 baud = RFCOMM_RPN_BR_2400;
948 baud = RFCOMM_RPN_BR_4800;
951 baud = RFCOMM_RPN_BR_7200;
954 baud = RFCOMM_RPN_BR_9600;
957 baud = RFCOMM_RPN_BR_19200;
960 baud = RFCOMM_RPN_BR_38400;
963 baud = RFCOMM_RPN_BR_57600;
966 baud = RFCOMM_RPN_BR_115200;
969 baud = RFCOMM_RPN_BR_230400;
972 /* 9600 is standard accordinag to the RFCOMM specification */
973 baud = RFCOMM_RPN_BR_9600;
979 rfcomm_send_rpn(dev->dlc->session, 1, dev->dlc->dlci, baud,
980 data_bits, stop_bits, parity,
981 RFCOMM_RPN_FLOW_NONE, x_on, x_off, changes);
984 static void rfcomm_tty_throttle(struct tty_struct *tty)
986 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
988 BT_DBG("tty %p dev %p", tty, dev);
990 rfcomm_dlc_throttle(dev->dlc);
993 static void rfcomm_tty_unthrottle(struct tty_struct *tty)
995 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
997 BT_DBG("tty %p dev %p", tty, dev);
999 rfcomm_dlc_unthrottle(dev->dlc);
1002 static int rfcomm_tty_chars_in_buffer(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 if (!dev || !dev->dlc)
1011 if (!skb_queue_empty(&dev->dlc->tx_queue))
1012 return dev->dlc->mtu;
1017 static void rfcomm_tty_flush_buffer(struct tty_struct *tty)
1019 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1021 BT_DBG("tty %p dev %p", tty, dev);
1023 if (!dev || !dev->dlc)
1026 skb_queue_purge(&dev->dlc->tx_queue);
1030 static void rfcomm_tty_send_xchar(struct tty_struct *tty, char ch)
1032 BT_DBG("tty %p ch %c", tty, ch);
1035 static void rfcomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
1037 BT_DBG("tty %p timeout %d", tty, timeout);
1040 static void rfcomm_tty_hangup(struct tty_struct *tty)
1042 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1044 BT_DBG("tty %p dev %p", tty, dev);
1046 tty_port_hangup(&dev->port);
1049 static int rfcomm_tty_tiocmget(struct tty_struct *tty)
1051 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1053 BT_DBG("tty %p dev %p", tty, dev);
1055 return dev->modem_status;
1058 static int rfcomm_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
1060 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1061 struct rfcomm_dlc *dlc = dev->dlc;
1064 BT_DBG("tty %p dev %p set 0x%02x clear 0x%02x", tty, dev, set, clear);
1066 rfcomm_dlc_get_modem_status(dlc, &v24_sig);
1068 if (set & TIOCM_DSR || set & TIOCM_DTR)
1069 v24_sig |= RFCOMM_V24_RTC;
1070 if (set & TIOCM_RTS || set & TIOCM_CTS)
1071 v24_sig |= RFCOMM_V24_RTR;
1073 v24_sig |= RFCOMM_V24_IC;
1075 v24_sig |= RFCOMM_V24_DV;
1077 if (clear & TIOCM_DSR || clear & TIOCM_DTR)
1078 v24_sig &= ~RFCOMM_V24_RTC;
1079 if (clear & TIOCM_RTS || clear & TIOCM_CTS)
1080 v24_sig &= ~RFCOMM_V24_RTR;
1081 if (clear & TIOCM_RI)
1082 v24_sig &= ~RFCOMM_V24_IC;
1083 if (clear & TIOCM_CD)
1084 v24_sig &= ~RFCOMM_V24_DV;
1086 rfcomm_dlc_set_modem_status(dlc, v24_sig);
1091 /* ---- TTY structure ---- */
1093 static const struct tty_operations rfcomm_ops = {
1094 .open = rfcomm_tty_open,
1095 .close = rfcomm_tty_close,
1096 .write = rfcomm_tty_write,
1097 .write_room = rfcomm_tty_write_room,
1098 .chars_in_buffer = rfcomm_tty_chars_in_buffer,
1099 .flush_buffer = rfcomm_tty_flush_buffer,
1100 .ioctl = rfcomm_tty_ioctl,
1101 .throttle = rfcomm_tty_throttle,
1102 .unthrottle = rfcomm_tty_unthrottle,
1103 .set_termios = rfcomm_tty_set_termios,
1104 .send_xchar = rfcomm_tty_send_xchar,
1105 .hangup = rfcomm_tty_hangup,
1106 .wait_until_sent = rfcomm_tty_wait_until_sent,
1107 .tiocmget = rfcomm_tty_tiocmget,
1108 .tiocmset = rfcomm_tty_tiocmset,
1109 .install = rfcomm_tty_install,
1110 .cleanup = rfcomm_tty_cleanup,
1113 int __init rfcomm_init_ttys(void)
1117 rfcomm_tty_driver = alloc_tty_driver(RFCOMM_TTY_PORTS);
1118 if (!rfcomm_tty_driver)
1121 rfcomm_tty_driver->driver_name = "rfcomm";
1122 rfcomm_tty_driver->name = "rfcomm";
1123 rfcomm_tty_driver->major = RFCOMM_TTY_MAJOR;
1124 rfcomm_tty_driver->minor_start = RFCOMM_TTY_MINOR;
1125 rfcomm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1126 rfcomm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1127 rfcomm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1128 rfcomm_tty_driver->init_termios = tty_std_termios;
1129 rfcomm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
1130 rfcomm_tty_driver->init_termios.c_lflag &= ~ICANON;
1131 tty_set_operations(rfcomm_tty_driver, &rfcomm_ops);
1133 error = tty_register_driver(rfcomm_tty_driver);
1135 BT_ERR("Can't register RFCOMM TTY driver");
1136 put_tty_driver(rfcomm_tty_driver);
1140 BT_INFO("RFCOMM TTY layer initialized");
1145 void rfcomm_cleanup_ttys(void)
1147 tty_unregister_driver(rfcomm_tty_driver);
1148 put_tty_driver(rfcomm_tty_driver);