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_get(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_get(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 dev_set_drvdata(dev->tty_dev, dev);
321 if (device_create_file(dev->tty_dev, &dev_attr_address) < 0)
322 BT_ERR("Failed to create address attribute");
324 if (device_create_file(dev->tty_dev, &dev_attr_channel) < 0)
325 BT_ERR("Failed to create channel attribute");
334 /* ---- Send buffer ---- */
335 static inline unsigned int rfcomm_room(struct rfcomm_dlc *dlc)
337 /* We can't let it be zero, because we don't get a callback
338 when tx_credits becomes nonzero, hence we'd never wake up */
339 return dlc->mtu * (dlc->tx_credits?:1);
342 static void rfcomm_wfree(struct sk_buff *skb)
344 struct rfcomm_dev *dev = (void *) skb->sk;
345 atomic_sub(skb->truesize, &dev->wmem_alloc);
346 if (test_bit(RFCOMM_TTY_ATTACHED, &dev->flags))
347 tty_port_tty_wakeup(&dev->port);
348 tty_port_put(&dev->port);
351 static void rfcomm_set_owner_w(struct sk_buff *skb, struct rfcomm_dev *dev)
353 tty_port_get(&dev->port);
354 atomic_add(skb->truesize, &dev->wmem_alloc);
355 skb->sk = (void *) dev;
356 skb->destructor = rfcomm_wfree;
359 static struct sk_buff *rfcomm_wmalloc(struct rfcomm_dev *dev, unsigned long size, gfp_t priority)
361 if (atomic_read(&dev->wmem_alloc) < rfcomm_room(dev->dlc)) {
362 struct sk_buff *skb = alloc_skb(size, priority);
364 rfcomm_set_owner_w(skb, dev);
371 /* ---- Device IOCTLs ---- */
373 #define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP))
375 static int rfcomm_create_dev(struct sock *sk, void __user *arg)
377 struct rfcomm_dev_req req;
378 struct rfcomm_dlc *dlc;
381 if (copy_from_user(&req, arg, sizeof(req)))
384 BT_DBG("sk %p dev_id %d flags 0x%x", sk, req.dev_id, req.flags);
386 if (req.flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN))
389 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
390 /* Socket must be connected */
391 if (sk->sk_state != BT_CONNECTED)
394 dlc = rfcomm_pi(sk)->dlc;
395 rfcomm_dlc_hold(dlc);
397 /* Validate the channel is unused */
398 dlc = rfcomm_dlc_exists(&req.src, &req.dst, req.channel);
405 dlc = rfcomm_dlc_alloc(GFP_KERNEL);
410 id = rfcomm_dev_add(&req, dlc);
416 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
417 /* DLC is now used by device.
418 * Socket must be disconnected */
419 sk->sk_state = BT_CLOSED;
425 static int rfcomm_release_dev(void __user *arg)
427 struct rfcomm_dev_req req;
428 struct rfcomm_dev *dev;
429 struct tty_struct *tty;
431 if (copy_from_user(&req, arg, sizeof(req)))
434 BT_DBG("dev_id %d flags 0x%x", req.dev_id, req.flags);
436 dev = rfcomm_dev_get(req.dev_id);
440 if (dev->flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN)) {
441 tty_port_put(&dev->port);
445 /* only release once */
446 if (test_and_set_bit(RFCOMM_DEV_RELEASED, &dev->status)) {
447 tty_port_put(&dev->port);
451 if (req.flags & (1 << RFCOMM_HANGUP_NOW))
452 rfcomm_dlc_close(dev->dlc, 0);
454 /* Shut down TTY synchronously before freeing rfcomm_dev */
455 tty = tty_port_tty_get(&dev->port);
461 if (!test_bit(RFCOMM_TTY_OWNED, &dev->status))
462 tty_port_put(&dev->port);
464 tty_port_put(&dev->port);
468 static int rfcomm_get_dev_list(void __user *arg)
470 struct rfcomm_dev *dev;
471 struct rfcomm_dev_list_req *dl;
472 struct rfcomm_dev_info *di;
473 int n = 0, size, err;
478 if (get_user(dev_num, (u16 __user *) arg))
481 if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di))
484 size = sizeof(*dl) + dev_num * sizeof(*di);
486 dl = kzalloc(size, GFP_KERNEL);
492 spin_lock(&rfcomm_dev_lock);
494 list_for_each_entry(dev, &rfcomm_dev_list, list) {
495 if (!tty_port_get(&dev->port))
497 (di + n)->id = dev->id;
498 (di + n)->flags = dev->flags;
499 (di + n)->state = dev->dlc->state;
500 (di + n)->channel = dev->channel;
501 bacpy(&(di + n)->src, &dev->src);
502 bacpy(&(di + n)->dst, &dev->dst);
503 tty_port_put(&dev->port);
508 spin_unlock(&rfcomm_dev_lock);
511 size = sizeof(*dl) + n * sizeof(*di);
513 err = copy_to_user(arg, dl, size);
516 return err ? -EFAULT : 0;
519 static int rfcomm_get_dev_info(void __user *arg)
521 struct rfcomm_dev *dev;
522 struct rfcomm_dev_info di;
527 if (copy_from_user(&di, arg, sizeof(di)))
530 dev = rfcomm_dev_get(di.id);
534 di.flags = dev->flags;
535 di.channel = dev->channel;
536 di.state = dev->dlc->state;
537 bacpy(&di.src, &dev->src);
538 bacpy(&di.dst, &dev->dst);
540 if (copy_to_user(arg, &di, sizeof(di)))
543 tty_port_put(&dev->port);
547 int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
549 BT_DBG("cmd %d arg %p", cmd, arg);
552 case RFCOMMCREATEDEV:
553 return rfcomm_create_dev(sk, arg);
555 case RFCOMMRELEASEDEV:
556 return rfcomm_release_dev(arg);
558 case RFCOMMGETDEVLIST:
559 return rfcomm_get_dev_list(arg);
561 case RFCOMMGETDEVINFO:
562 return rfcomm_get_dev_info(arg);
568 /* ---- DLC callbacks ---- */
569 static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb)
571 struct rfcomm_dev *dev = dlc->owner;
578 if (!skb_queue_empty(&dev->pending)) {
579 skb_queue_tail(&dev->pending, skb);
583 BT_DBG("dlc %p len %d", dlc, skb->len);
585 tty_insert_flip_string(&dev->port, skb->data, skb->len);
586 tty_flip_buffer_push(&dev->port);
591 static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
593 struct rfcomm_dev *dev = dlc->owner;
597 BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
600 if (dlc->state == BT_CONNECTED) {
601 rfcomm_reparent_device(dev);
603 wake_up_interruptible(&dev->port.open_wait);
604 } else if (dlc->state == BT_CLOSED)
605 tty_port_tty_hangup(&dev->port, false);
608 static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig)
610 struct rfcomm_dev *dev = dlc->owner;
614 BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc, dev, v24_sig);
616 if ((dev->modem_status & TIOCM_CD) && !(v24_sig & RFCOMM_V24_DV))
617 tty_port_tty_hangup(&dev->port, true);
620 ((v24_sig & RFCOMM_V24_RTC) ? (TIOCM_DSR | TIOCM_DTR) : 0) |
621 ((v24_sig & RFCOMM_V24_RTR) ? (TIOCM_RTS | TIOCM_CTS) : 0) |
622 ((v24_sig & RFCOMM_V24_IC) ? TIOCM_RI : 0) |
623 ((v24_sig & RFCOMM_V24_DV) ? TIOCM_CD : 0);
626 /* ---- TTY functions ---- */
627 static void rfcomm_tty_copy_pending(struct rfcomm_dev *dev)
632 BT_DBG("dev %p", dev);
634 rfcomm_dlc_lock(dev->dlc);
636 while ((skb = skb_dequeue(&dev->pending))) {
637 inserted += tty_insert_flip_string(&dev->port, skb->data,
642 rfcomm_dlc_unlock(dev->dlc);
645 tty_flip_buffer_push(&dev->port);
648 /* do the reverse of install, clearing the tty fields and releasing the
649 * reference to tty_port
651 static void rfcomm_tty_cleanup(struct tty_struct *tty)
653 struct rfcomm_dev *dev = tty->driver_data;
655 clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
657 rfcomm_dlc_lock(dev->dlc);
658 tty->driver_data = NULL;
659 rfcomm_dlc_unlock(dev->dlc);
662 * purge the dlc->tx_queue to avoid circular dependencies
663 * between dev and dlc
665 skb_queue_purge(&dev->dlc->tx_queue);
667 tty_port_put(&dev->port);
670 /* we acquire the tty_port reference since it's here the tty is first used
671 * by setting the termios. We also populate the driver_data field and install
674 static int rfcomm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
676 struct rfcomm_dev *dev;
677 struct rfcomm_dlc *dlc;
680 dev = rfcomm_dev_get(tty->index);
686 /* Attach TTY and open DLC */
687 rfcomm_dlc_lock(dlc);
688 tty->driver_data = dev;
689 rfcomm_dlc_unlock(dlc);
690 set_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
692 /* install the tty_port */
693 err = tty_port_install(&dev->port, driver, tty);
695 rfcomm_tty_cleanup(tty);
699 /* take over the tty_port reference if the port was created with the
700 * flag RFCOMM_RELEASE_ONHUP. This will force the release of the port
701 * when the last process closes the tty. The behaviour is expected by
704 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
705 set_bit(RFCOMM_TTY_OWNED, &dev->status);
706 tty_port_put(&dev->port);
712 static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
714 struct rfcomm_dev *dev = tty->driver_data;
717 BT_DBG("tty %p id %d", tty, tty->index);
719 BT_DBG("dev %p dst %pMR channel %d opened %d", dev, &dev->dst,
720 dev->channel, dev->port.count);
722 err = tty_port_open(&dev->port, tty, filp);
727 * FIXME: rfcomm should use proper flow control for
728 * received data. This hack will be unnecessary and can
729 * be removed when that's implemented
731 rfcomm_tty_copy_pending(dev);
733 rfcomm_dlc_unthrottle(dev->dlc);
738 static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
740 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
742 BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc,
745 tty_port_close(&dev->port, tty, filp);
748 static int rfcomm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
750 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
751 struct rfcomm_dlc *dlc = dev->dlc;
753 int err = 0, sent = 0, size;
755 BT_DBG("tty %p count %d", tty, count);
758 size = min_t(uint, count, dlc->mtu);
760 skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, GFP_ATOMIC);
765 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
767 memcpy(skb_put(skb, size), buf + sent, size);
769 err = rfcomm_dlc_send(dlc, skb);
779 return sent ? sent : err;
782 static int rfcomm_tty_write_room(struct tty_struct *tty)
784 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
787 BT_DBG("tty %p", tty);
789 if (!dev || !dev->dlc)
792 room = rfcomm_room(dev->dlc) - atomic_read(&dev->wmem_alloc);
799 static int rfcomm_tty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
801 BT_DBG("tty %p cmd 0x%02x", tty, cmd);
805 BT_DBG("TCGETS is not supported");
809 BT_DBG("TCSETS is not supported");
813 BT_DBG("TIOCMIWAIT");
817 BT_ERR("TIOCGSERIAL is not supported");
821 BT_ERR("TIOCSSERIAL is not supported");
825 BT_ERR("TIOCSERGSTRUCT is not supported");
829 BT_ERR("TIOCSERGETLSR is not supported");
833 BT_ERR("TIOCSERCONFIG is not supported");
837 return -ENOIOCTLCMD; /* ioctls which we must ignore */
844 static void rfcomm_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
846 struct ktermios *new = &tty->termios;
847 int old_baud_rate = tty_termios_baud_rate(old);
848 int new_baud_rate = tty_termios_baud_rate(new);
850 u8 baud, data_bits, stop_bits, parity, x_on, x_off;
853 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
855 BT_DBG("tty %p termios %p", tty, old);
857 if (!dev || !dev->dlc || !dev->dlc->session)
860 /* Handle turning off CRTSCTS */
861 if ((old->c_cflag & CRTSCTS) && !(new->c_cflag & CRTSCTS))
862 BT_DBG("Turning off CRTSCTS unsupported");
864 /* Parity on/off and when on, odd/even */
865 if (((old->c_cflag & PARENB) != (new->c_cflag & PARENB)) ||
866 ((old->c_cflag & PARODD) != (new->c_cflag & PARODD))) {
867 changes |= RFCOMM_RPN_PM_PARITY;
868 BT_DBG("Parity change detected.");
871 /* Mark and space parity are not supported! */
872 if (new->c_cflag & PARENB) {
873 if (new->c_cflag & PARODD) {
874 BT_DBG("Parity is ODD");
875 parity = RFCOMM_RPN_PARITY_ODD;
877 BT_DBG("Parity is EVEN");
878 parity = RFCOMM_RPN_PARITY_EVEN;
881 BT_DBG("Parity is OFF");
882 parity = RFCOMM_RPN_PARITY_NONE;
885 /* Setting the x_on / x_off characters */
886 if (old->c_cc[VSTOP] != new->c_cc[VSTOP]) {
887 BT_DBG("XOFF custom");
888 x_on = new->c_cc[VSTOP];
889 changes |= RFCOMM_RPN_PM_XON;
891 BT_DBG("XOFF default");
892 x_on = RFCOMM_RPN_XON_CHAR;
895 if (old->c_cc[VSTART] != new->c_cc[VSTART]) {
896 BT_DBG("XON custom");
897 x_off = new->c_cc[VSTART];
898 changes |= RFCOMM_RPN_PM_XOFF;
900 BT_DBG("XON default");
901 x_off = RFCOMM_RPN_XOFF_CHAR;
904 /* Handle setting of stop bits */
905 if ((old->c_cflag & CSTOPB) != (new->c_cflag & CSTOPB))
906 changes |= RFCOMM_RPN_PM_STOP;
908 /* POSIX does not support 1.5 stop bits and RFCOMM does not
909 * support 2 stop bits. So a request for 2 stop bits gets
910 * translated to 1.5 stop bits */
911 if (new->c_cflag & CSTOPB)
912 stop_bits = RFCOMM_RPN_STOP_15;
914 stop_bits = RFCOMM_RPN_STOP_1;
916 /* Handle number of data bits [5-8] */
917 if ((old->c_cflag & CSIZE) != (new->c_cflag & CSIZE))
918 changes |= RFCOMM_RPN_PM_DATA;
920 switch (new->c_cflag & CSIZE) {
922 data_bits = RFCOMM_RPN_DATA_5;
925 data_bits = RFCOMM_RPN_DATA_6;
928 data_bits = RFCOMM_RPN_DATA_7;
931 data_bits = RFCOMM_RPN_DATA_8;
934 data_bits = RFCOMM_RPN_DATA_8;
938 /* Handle baudrate settings */
939 if (old_baud_rate != new_baud_rate)
940 changes |= RFCOMM_RPN_PM_BITRATE;
942 switch (new_baud_rate) {
944 baud = RFCOMM_RPN_BR_2400;
947 baud = RFCOMM_RPN_BR_4800;
950 baud = RFCOMM_RPN_BR_7200;
953 baud = RFCOMM_RPN_BR_9600;
956 baud = RFCOMM_RPN_BR_19200;
959 baud = RFCOMM_RPN_BR_38400;
962 baud = RFCOMM_RPN_BR_57600;
965 baud = RFCOMM_RPN_BR_115200;
968 baud = RFCOMM_RPN_BR_230400;
971 /* 9600 is standard accordinag to the RFCOMM specification */
972 baud = RFCOMM_RPN_BR_9600;
978 rfcomm_send_rpn(dev->dlc->session, 1, dev->dlc->dlci, baud,
979 data_bits, stop_bits, parity,
980 RFCOMM_RPN_FLOW_NONE, x_on, x_off, changes);
983 static void rfcomm_tty_throttle(struct tty_struct *tty)
985 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
987 BT_DBG("tty %p dev %p", tty, dev);
989 rfcomm_dlc_throttle(dev->dlc);
992 static void rfcomm_tty_unthrottle(struct tty_struct *tty)
994 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
996 BT_DBG("tty %p dev %p", tty, dev);
998 rfcomm_dlc_unthrottle(dev->dlc);
1001 static int rfcomm_tty_chars_in_buffer(struct tty_struct *tty)
1003 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1005 BT_DBG("tty %p dev %p", tty, dev);
1007 if (!dev || !dev->dlc)
1010 if (!skb_queue_empty(&dev->dlc->tx_queue))
1011 return dev->dlc->mtu;
1016 static void rfcomm_tty_flush_buffer(struct tty_struct *tty)
1018 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1020 BT_DBG("tty %p dev %p", tty, dev);
1022 if (!dev || !dev->dlc)
1025 skb_queue_purge(&dev->dlc->tx_queue);
1029 static void rfcomm_tty_send_xchar(struct tty_struct *tty, char ch)
1031 BT_DBG("tty %p ch %c", tty, ch);
1034 static void rfcomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
1036 BT_DBG("tty %p timeout %d", tty, timeout);
1039 static void rfcomm_tty_hangup(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 tty_port_hangup(&dev->port);
1048 static int rfcomm_tty_tiocmget(struct tty_struct *tty)
1050 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1052 BT_DBG("tty %p dev %p", tty, dev);
1054 return dev->modem_status;
1057 static int rfcomm_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
1059 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
1060 struct rfcomm_dlc *dlc = dev->dlc;
1063 BT_DBG("tty %p dev %p set 0x%02x clear 0x%02x", tty, dev, set, clear);
1065 rfcomm_dlc_get_modem_status(dlc, &v24_sig);
1067 if (set & TIOCM_DSR || set & TIOCM_DTR)
1068 v24_sig |= RFCOMM_V24_RTC;
1069 if (set & TIOCM_RTS || set & TIOCM_CTS)
1070 v24_sig |= RFCOMM_V24_RTR;
1072 v24_sig |= RFCOMM_V24_IC;
1074 v24_sig |= RFCOMM_V24_DV;
1076 if (clear & TIOCM_DSR || clear & TIOCM_DTR)
1077 v24_sig &= ~RFCOMM_V24_RTC;
1078 if (clear & TIOCM_RTS || clear & TIOCM_CTS)
1079 v24_sig &= ~RFCOMM_V24_RTR;
1080 if (clear & TIOCM_RI)
1081 v24_sig &= ~RFCOMM_V24_IC;
1082 if (clear & TIOCM_CD)
1083 v24_sig &= ~RFCOMM_V24_DV;
1085 rfcomm_dlc_set_modem_status(dlc, v24_sig);
1090 /* ---- TTY structure ---- */
1092 static const struct tty_operations rfcomm_ops = {
1093 .open = rfcomm_tty_open,
1094 .close = rfcomm_tty_close,
1095 .write = rfcomm_tty_write,
1096 .write_room = rfcomm_tty_write_room,
1097 .chars_in_buffer = rfcomm_tty_chars_in_buffer,
1098 .flush_buffer = rfcomm_tty_flush_buffer,
1099 .ioctl = rfcomm_tty_ioctl,
1100 .throttle = rfcomm_tty_throttle,
1101 .unthrottle = rfcomm_tty_unthrottle,
1102 .set_termios = rfcomm_tty_set_termios,
1103 .send_xchar = rfcomm_tty_send_xchar,
1104 .hangup = rfcomm_tty_hangup,
1105 .wait_until_sent = rfcomm_tty_wait_until_sent,
1106 .tiocmget = rfcomm_tty_tiocmget,
1107 .tiocmset = rfcomm_tty_tiocmset,
1108 .install = rfcomm_tty_install,
1109 .cleanup = rfcomm_tty_cleanup,
1112 int __init rfcomm_init_ttys(void)
1116 rfcomm_tty_driver = alloc_tty_driver(RFCOMM_TTY_PORTS);
1117 if (!rfcomm_tty_driver)
1120 rfcomm_tty_driver->driver_name = "rfcomm";
1121 rfcomm_tty_driver->name = "rfcomm";
1122 rfcomm_tty_driver->major = RFCOMM_TTY_MAJOR;
1123 rfcomm_tty_driver->minor_start = RFCOMM_TTY_MINOR;
1124 rfcomm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1125 rfcomm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1126 rfcomm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1127 rfcomm_tty_driver->init_termios = tty_std_termios;
1128 rfcomm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
1129 rfcomm_tty_driver->init_termios.c_lflag &= ~ICANON;
1130 tty_set_operations(rfcomm_tty_driver, &rfcomm_ops);
1132 error = tty_register_driver(rfcomm_tty_driver);
1134 BT_ERR("Can't register RFCOMM TTY driver");
1135 put_tty_driver(rfcomm_tty_driver);
1139 BT_INFO("RFCOMM TTY layer initialized");
1144 void rfcomm_cleanup_ttys(void)
1146 tty_unregister_driver(rfcomm_tty_driver);
1147 put_tty_driver(rfcomm_tty_driver);