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/config.h>
31 #include <linux/module.h>
33 #include <linux/tty.h>
34 #include <linux/tty_driver.h>
35 #include <linux/tty_flip.h>
37 #include <linux/capability.h>
38 #include <linux/slab.h>
39 #include <linux/skbuff.h>
41 #include <net/bluetooth/bluetooth.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;
80 static LIST_HEAD(rfcomm_dev_list);
81 static DEFINE_RWLOCK(rfcomm_dev_lock);
83 static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb);
84 static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err);
85 static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig);
87 static void rfcomm_tty_wakeup(unsigned long arg);
89 /* ---- Device functions ---- */
90 static void rfcomm_dev_destruct(struct rfcomm_dev *dev)
92 struct rfcomm_dlc *dlc = dev->dlc;
94 BT_DBG("dev %p dlc %p", dev, dlc);
97 /* Detach DLC if it's owned by this dev */
98 if (dlc->owner == dev)
100 rfcomm_dlc_unlock(dlc);
104 tty_unregister_device(rfcomm_tty_driver, dev->id);
106 /* Refcount should only hit zero when called from rfcomm_dev_del()
107 which will have taken us off the list. Everything else are
109 BUG_ON(!list_empty(&dev->list));
113 /* It's safe to call module_put() here because socket still
114 holds reference to this module. */
115 module_put(THIS_MODULE);
118 static inline void rfcomm_dev_hold(struct rfcomm_dev *dev)
120 atomic_inc(&dev->refcnt);
123 static inline void rfcomm_dev_put(struct rfcomm_dev *dev)
125 /* The reason this isn't actually a race, as you no
126 doubt have a little voice screaming at you in your
127 head, is that the refcount should never actually
128 reach zero unless the device has already been taken
129 off the list, in rfcomm_dev_del(). And if that's not
130 true, we'll hit the BUG() in rfcomm_dev_destruct()
132 if (atomic_dec_and_test(&dev->refcnt))
133 rfcomm_dev_destruct(dev);
136 static struct rfcomm_dev *__rfcomm_dev_get(int id)
138 struct rfcomm_dev *dev;
141 list_for_each(p, &rfcomm_dev_list) {
142 dev = list_entry(p, struct rfcomm_dev, list);
150 static inline struct rfcomm_dev *rfcomm_dev_get(int id)
152 struct rfcomm_dev *dev;
154 read_lock(&rfcomm_dev_lock);
156 dev = __rfcomm_dev_get(id);
158 rfcomm_dev_hold(dev);
160 read_unlock(&rfcomm_dev_lock);
165 static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
167 struct rfcomm_dev *dev;
168 struct list_head *head = &rfcomm_dev_list, *p;
171 BT_DBG("id %d channel %d", req->dev_id, req->channel);
173 dev = kmalloc(sizeof(struct rfcomm_dev), GFP_KERNEL);
176 memset(dev, 0, sizeof(struct rfcomm_dev));
178 write_lock_bh(&rfcomm_dev_lock);
180 if (req->dev_id < 0) {
183 list_for_each(p, &rfcomm_dev_list) {
184 if (list_entry(p, struct rfcomm_dev, list)->id != dev->id)
191 dev->id = req->dev_id;
193 list_for_each(p, &rfcomm_dev_list) {
194 struct rfcomm_dev *entry = list_entry(p, struct rfcomm_dev, list);
196 if (entry->id == dev->id) {
201 if (entry->id > dev->id - 1)
208 if ((dev->id < 0) || (dev->id > RFCOMM_MAX_DEV - 1)) {
213 sprintf(dev->name, "rfcomm%d", dev->id);
215 list_add(&dev->list, head);
216 atomic_set(&dev->refcnt, 1);
218 bacpy(&dev->src, &req->src);
219 bacpy(&dev->dst, &req->dst);
220 dev->channel = req->channel;
222 dev->flags = req->flags &
223 ((1 << RFCOMM_RELEASE_ONHUP) | (1 << RFCOMM_REUSE_DLC));
225 init_waitqueue_head(&dev->wait);
226 tasklet_init(&dev->wakeup_task, rfcomm_tty_wakeup, (unsigned long) dev);
228 rfcomm_dlc_lock(dlc);
229 dlc->data_ready = rfcomm_dev_data_ready;
230 dlc->state_change = rfcomm_dev_state_change;
231 dlc->modem_status = rfcomm_dev_modem_status;
235 rfcomm_dlc_unlock(dlc);
237 /* It's safe to call __module_get() here because socket already
238 holds reference to this module. */
239 __module_get(THIS_MODULE);
242 write_unlock_bh(&rfcomm_dev_lock);
249 tty_register_device(rfcomm_tty_driver, dev->id, NULL);
254 static void rfcomm_dev_del(struct rfcomm_dev *dev)
256 BT_DBG("dev %p", dev);
258 write_lock_bh(&rfcomm_dev_lock);
259 list_del_init(&dev->list);
260 write_unlock_bh(&rfcomm_dev_lock);
265 /* ---- Send buffer ---- */
266 static inline unsigned int rfcomm_room(struct rfcomm_dlc *dlc)
268 /* We can't let it be zero, because we don't get a callback
269 when tx_credits becomes nonzero, hence we'd never wake up */
270 return dlc->mtu * (dlc->tx_credits?:1);
273 static void rfcomm_wfree(struct sk_buff *skb)
275 struct rfcomm_dev *dev = (void *) skb->sk;
276 atomic_sub(skb->truesize, &dev->wmem_alloc);
277 if (test_bit(RFCOMM_TTY_ATTACHED, &dev->flags))
278 tasklet_schedule(&dev->wakeup_task);
282 static inline void rfcomm_set_owner_w(struct sk_buff *skb, struct rfcomm_dev *dev)
284 rfcomm_dev_hold(dev);
285 atomic_add(skb->truesize, &dev->wmem_alloc);
286 skb->sk = (void *) dev;
287 skb->destructor = rfcomm_wfree;
290 static struct sk_buff *rfcomm_wmalloc(struct rfcomm_dev *dev, unsigned long size, gfp_t priority)
292 if (atomic_read(&dev->wmem_alloc) < rfcomm_room(dev->dlc)) {
293 struct sk_buff *skb = alloc_skb(size, priority);
295 rfcomm_set_owner_w(skb, dev);
302 /* ---- Device IOCTLs ---- */
304 #define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP))
306 static int rfcomm_create_dev(struct sock *sk, void __user *arg)
308 struct rfcomm_dev_req req;
309 struct rfcomm_dlc *dlc;
312 if (copy_from_user(&req, arg, sizeof(req)))
315 BT_DBG("sk %p dev_id %id flags 0x%x", sk, req.dev_id, req.flags);
317 if (req.flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN))
320 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
321 /* Socket must be connected */
322 if (sk->sk_state != BT_CONNECTED)
325 dlc = rfcomm_pi(sk)->dlc;
326 rfcomm_dlc_hold(dlc);
328 dlc = rfcomm_dlc_alloc(GFP_KERNEL);
333 id = rfcomm_dev_add(&req, dlc);
339 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
340 /* DLC is now used by device.
341 * Socket must be disconnected */
342 sk->sk_state = BT_CLOSED;
348 static int rfcomm_release_dev(void __user *arg)
350 struct rfcomm_dev_req req;
351 struct rfcomm_dev *dev;
353 if (copy_from_user(&req, arg, sizeof(req)))
356 BT_DBG("dev_id %id flags 0x%x", req.dev_id, req.flags);
358 if (!(dev = rfcomm_dev_get(req.dev_id)))
361 if (dev->flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN)) {
366 if (req.flags & (1 << RFCOMM_HANGUP_NOW))
367 rfcomm_dlc_close(dev->dlc, 0);
374 static int rfcomm_get_dev_list(void __user *arg)
376 struct rfcomm_dev_list_req *dl;
377 struct rfcomm_dev_info *di;
379 int n = 0, size, err;
384 if (get_user(dev_num, (u16 __user *) arg))
387 if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di))
390 size = sizeof(*dl) + dev_num * sizeof(*di);
392 if (!(dl = kmalloc(size, GFP_KERNEL)))
397 read_lock_bh(&rfcomm_dev_lock);
399 list_for_each(p, &rfcomm_dev_list) {
400 struct rfcomm_dev *dev = list_entry(p, struct rfcomm_dev, list);
401 (di + n)->id = dev->id;
402 (di + n)->flags = dev->flags;
403 (di + n)->state = dev->dlc->state;
404 (di + n)->channel = dev->channel;
405 bacpy(&(di + n)->src, &dev->src);
406 bacpy(&(di + n)->dst, &dev->dst);
411 read_unlock_bh(&rfcomm_dev_lock);
414 size = sizeof(*dl) + n * sizeof(*di);
416 err = copy_to_user(arg, dl, size);
419 return err ? -EFAULT : 0;
422 static int rfcomm_get_dev_info(void __user *arg)
424 struct rfcomm_dev *dev;
425 struct rfcomm_dev_info di;
430 if (copy_from_user(&di, arg, sizeof(di)))
433 if (!(dev = rfcomm_dev_get(di.id)))
436 di.flags = dev->flags;
437 di.channel = dev->channel;
438 di.state = dev->dlc->state;
439 bacpy(&di.src, &dev->src);
440 bacpy(&di.dst, &dev->dst);
442 if (copy_to_user(arg, &di, sizeof(di)))
449 int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
451 BT_DBG("cmd %d arg %p", cmd, arg);
454 case RFCOMMCREATEDEV:
455 return rfcomm_create_dev(sk, arg);
457 case RFCOMMRELEASEDEV:
458 return rfcomm_release_dev(arg);
460 case RFCOMMGETDEVLIST:
461 return rfcomm_get_dev_list(arg);
463 case RFCOMMGETDEVINFO:
464 return rfcomm_get_dev_info(arg);
470 /* ---- DLC callbacks ---- */
471 static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb)
473 struct rfcomm_dev *dev = dlc->owner;
474 struct tty_struct *tty;
476 if (!dev || !(tty = dev->tty)) {
481 BT_DBG("dlc %p tty %p len %d", dlc, tty, skb->len);
483 tty_insert_flip_string(tty, skb->data, skb->len);
484 tty_flip_buffer_push(tty);
489 static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
491 struct rfcomm_dev *dev = dlc->owner;
495 BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
498 wake_up_interruptible(&dev->wait);
500 if (dlc->state == BT_CLOSED) {
502 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
503 rfcomm_dev_hold(dev);
506 /* We have to drop DLC lock here, otherwise
507 rfcomm_dev_put() will dead lock if it's
508 the last reference. */
509 rfcomm_dlc_unlock(dlc);
511 rfcomm_dlc_lock(dlc);
514 tty_hangup(dev->tty);
518 static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig)
520 struct rfcomm_dev *dev = dlc->owner;
524 BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc, dev, v24_sig);
526 if ((dev->modem_status & TIOCM_CD) && !(v24_sig & RFCOMM_V24_DV)) {
527 if (dev->tty && !C_CLOCAL(dev->tty))
528 tty_hangup(dev->tty);
532 ((v24_sig & RFCOMM_V24_RTC) ? (TIOCM_DSR | TIOCM_DTR) : 0) |
533 ((v24_sig & RFCOMM_V24_RTR) ? (TIOCM_RTS | TIOCM_CTS) : 0) |
534 ((v24_sig & RFCOMM_V24_IC) ? TIOCM_RI : 0) |
535 ((v24_sig & RFCOMM_V24_DV) ? TIOCM_CD : 0);
538 /* ---- TTY functions ---- */
539 static void rfcomm_tty_wakeup(unsigned long arg)
541 struct rfcomm_dev *dev = (void *) arg;
542 struct tty_struct *tty = dev->tty;
546 BT_DBG("dev %p tty %p", dev, tty);
548 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && tty->ldisc.write_wakeup)
549 (tty->ldisc.write_wakeup)(tty);
551 wake_up_interruptible(&tty->write_wait);
552 #ifdef SERIAL_HAVE_POLL_WAIT
553 wake_up_interruptible(&tty->poll_wait);
557 static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
559 DECLARE_WAITQUEUE(wait, current);
560 struct rfcomm_dev *dev;
561 struct rfcomm_dlc *dlc;
566 BT_DBG("tty %p id %d", tty, id);
568 /* We don't leak this refcount. For reasons which are not entirely
569 clear, the TTY layer will call our ->close() method even if the
570 open fails. We decrease the refcount there, and decreasing it
571 here too would cause breakage. */
572 dev = rfcomm_dev_get(id);
576 BT_DBG("dev %p dst %s channel %d opened %d", dev, batostr(&dev->dst), dev->channel, dev->opened);
578 if (dev->opened++ != 0)
583 /* Attach TTY and open DLC */
585 rfcomm_dlc_lock(dlc);
586 tty->driver_data = dev;
588 rfcomm_dlc_unlock(dlc);
589 set_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
591 err = rfcomm_dlc_open(dlc, &dev->src, &dev->dst, dev->channel);
595 /* Wait for DLC to connect */
596 add_wait_queue(&dev->wait, &wait);
598 set_current_state(TASK_INTERRUPTIBLE);
600 if (dlc->state == BT_CLOSED) {
605 if (dlc->state == BT_CONNECTED)
608 if (signal_pending(current)) {
615 set_current_state(TASK_RUNNING);
616 remove_wait_queue(&dev->wait, &wait);
621 static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
623 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
627 BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc, dev->opened);
629 if (--dev->opened == 0) {
630 /* Close DLC and dettach TTY */
631 rfcomm_dlc_close(dev->dlc, 0);
633 clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
634 tasklet_kill(&dev->wakeup_task);
636 rfcomm_dlc_lock(dev->dlc);
637 tty->driver_data = NULL;
639 rfcomm_dlc_unlock(dev->dlc);
645 static int rfcomm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
647 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
648 struct rfcomm_dlc *dlc = dev->dlc;
650 int err = 0, sent = 0, size;
652 BT_DBG("tty %p count %d", tty, count);
655 size = min_t(uint, count, dlc->mtu);
657 skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, GFP_ATOMIC);
662 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
664 memcpy(skb_put(skb, size), buf + sent, size);
666 if ((err = rfcomm_dlc_send(dlc, skb)) < 0) {
675 return sent ? sent : err;
678 static int rfcomm_tty_write_room(struct tty_struct *tty)
680 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
683 BT_DBG("tty %p", tty);
685 room = rfcomm_room(dev->dlc) - atomic_read(&dev->wmem_alloc);
691 static int rfcomm_tty_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd, unsigned long arg)
693 BT_DBG("tty %p cmd 0x%02x", tty, cmd);
697 BT_DBG("TCGETS is not supported");
701 BT_DBG("TCSETS is not supported");
705 BT_DBG("TIOCMIWAIT");
709 BT_DBG("TIOCGICOUNT");
713 BT_ERR("TIOCGSERIAL is not supported");
717 BT_ERR("TIOCSSERIAL is not supported");
721 BT_ERR("TIOCSERGSTRUCT is not supported");
725 BT_ERR("TIOCSERGETLSR is not supported");
729 BT_ERR("TIOCSERCONFIG is not supported");
733 return -ENOIOCTLCMD; /* ioctls which we must ignore */
740 static void rfcomm_tty_set_termios(struct tty_struct *tty, struct termios *old)
742 struct termios *new = (struct termios *) tty->termios;
743 int old_baud_rate = tty_termios_baud_rate(old);
744 int new_baud_rate = tty_termios_baud_rate(new);
746 u8 baud, data_bits, stop_bits, parity, x_on, x_off;
749 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
751 BT_DBG("tty %p termios %p", tty, old);
753 /* Handle turning off CRTSCTS */
754 if ((old->c_cflag & CRTSCTS) && !(new->c_cflag & CRTSCTS))
755 BT_DBG("Turning off CRTSCTS unsupported");
757 /* Parity on/off and when on, odd/even */
758 if (((old->c_cflag & PARENB) != (new->c_cflag & PARENB)) ||
759 ((old->c_cflag & PARODD) != (new->c_cflag & PARODD)) ) {
760 changes |= RFCOMM_RPN_PM_PARITY;
761 BT_DBG("Parity change detected.");
764 /* Mark and space parity are not supported! */
765 if (new->c_cflag & PARENB) {
766 if (new->c_cflag & PARODD) {
767 BT_DBG("Parity is ODD");
768 parity = RFCOMM_RPN_PARITY_ODD;
770 BT_DBG("Parity is EVEN");
771 parity = RFCOMM_RPN_PARITY_EVEN;
774 BT_DBG("Parity is OFF");
775 parity = RFCOMM_RPN_PARITY_NONE;
778 /* Setting the x_on / x_off characters */
779 if (old->c_cc[VSTOP] != new->c_cc[VSTOP]) {
780 BT_DBG("XOFF custom");
781 x_on = new->c_cc[VSTOP];
782 changes |= RFCOMM_RPN_PM_XON;
784 BT_DBG("XOFF default");
785 x_on = RFCOMM_RPN_XON_CHAR;
788 if (old->c_cc[VSTART] != new->c_cc[VSTART]) {
789 BT_DBG("XON custom");
790 x_off = new->c_cc[VSTART];
791 changes |= RFCOMM_RPN_PM_XOFF;
793 BT_DBG("XON default");
794 x_off = RFCOMM_RPN_XOFF_CHAR;
797 /* Handle setting of stop bits */
798 if ((old->c_cflag & CSTOPB) != (new->c_cflag & CSTOPB))
799 changes |= RFCOMM_RPN_PM_STOP;
801 /* POSIX does not support 1.5 stop bits and RFCOMM does not
802 * support 2 stop bits. So a request for 2 stop bits gets
803 * translated to 1.5 stop bits */
804 if (new->c_cflag & CSTOPB) {
805 stop_bits = RFCOMM_RPN_STOP_15;
807 stop_bits = RFCOMM_RPN_STOP_1;
810 /* Handle number of data bits [5-8] */
811 if ((old->c_cflag & CSIZE) != (new->c_cflag & CSIZE))
812 changes |= RFCOMM_RPN_PM_DATA;
814 switch (new->c_cflag & CSIZE) {
816 data_bits = RFCOMM_RPN_DATA_5;
819 data_bits = RFCOMM_RPN_DATA_6;
822 data_bits = RFCOMM_RPN_DATA_7;
825 data_bits = RFCOMM_RPN_DATA_8;
828 data_bits = RFCOMM_RPN_DATA_8;
832 /* Handle baudrate settings */
833 if (old_baud_rate != new_baud_rate)
834 changes |= RFCOMM_RPN_PM_BITRATE;
836 switch (new_baud_rate) {
838 baud = RFCOMM_RPN_BR_2400;
841 baud = RFCOMM_RPN_BR_4800;
844 baud = RFCOMM_RPN_BR_7200;
847 baud = RFCOMM_RPN_BR_9600;
850 baud = RFCOMM_RPN_BR_19200;
853 baud = RFCOMM_RPN_BR_38400;
856 baud = RFCOMM_RPN_BR_57600;
859 baud = RFCOMM_RPN_BR_115200;
862 baud = RFCOMM_RPN_BR_230400;
865 /* 9600 is standard accordinag to the RFCOMM specification */
866 baud = RFCOMM_RPN_BR_9600;
872 rfcomm_send_rpn(dev->dlc->session, 1, dev->dlc->dlci, baud,
873 data_bits, stop_bits, parity,
874 RFCOMM_RPN_FLOW_NONE, x_on, x_off, changes);
879 static void rfcomm_tty_throttle(struct tty_struct *tty)
881 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
883 BT_DBG("tty %p dev %p", tty, dev);
885 rfcomm_dlc_throttle(dev->dlc);
888 static void rfcomm_tty_unthrottle(struct tty_struct *tty)
890 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
892 BT_DBG("tty %p dev %p", tty, dev);
894 rfcomm_dlc_unthrottle(dev->dlc);
897 static int rfcomm_tty_chars_in_buffer(struct tty_struct *tty)
899 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
900 struct rfcomm_dlc *dlc = dev->dlc;
902 BT_DBG("tty %p dev %p", tty, dev);
904 if (!skb_queue_empty(&dlc->tx_queue))
910 static void rfcomm_tty_flush_buffer(struct tty_struct *tty)
912 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
916 BT_DBG("tty %p dev %p", tty, dev);
918 skb_queue_purge(&dev->dlc->tx_queue);
920 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && tty->ldisc.write_wakeup)
921 tty->ldisc.write_wakeup(tty);
924 static void rfcomm_tty_send_xchar(struct tty_struct *tty, char ch)
926 BT_DBG("tty %p ch %c", tty, ch);
929 static void rfcomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
931 BT_DBG("tty %p timeout %d", tty, timeout);
934 static void rfcomm_tty_hangup(struct tty_struct *tty)
936 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
940 BT_DBG("tty %p dev %p", tty, dev);
942 rfcomm_tty_flush_buffer(tty);
944 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags))
948 static int rfcomm_tty_read_proc(char *buf, char **start, off_t offset, int len, int *eof, void *unused)
953 static int rfcomm_tty_tiocmget(struct tty_struct *tty, struct file *filp)
955 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
957 BT_DBG("tty %p dev %p", tty, dev);
959 return dev->modem_status;
962 static int rfcomm_tty_tiocmset(struct tty_struct *tty, struct file *filp, unsigned int set, unsigned int clear)
964 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
965 struct rfcomm_dlc *dlc = dev->dlc;
968 BT_DBG("tty %p dev %p set 0x%02x clear 0x%02x", tty, dev, set, clear);
970 rfcomm_dlc_get_modem_status(dlc, &v24_sig);
972 if (set & TIOCM_DSR || set & TIOCM_DTR)
973 v24_sig |= RFCOMM_V24_RTC;
974 if (set & TIOCM_RTS || set & TIOCM_CTS)
975 v24_sig |= RFCOMM_V24_RTR;
977 v24_sig |= RFCOMM_V24_IC;
979 v24_sig |= RFCOMM_V24_DV;
981 if (clear & TIOCM_DSR || clear & TIOCM_DTR)
982 v24_sig &= ~RFCOMM_V24_RTC;
983 if (clear & TIOCM_RTS || clear & TIOCM_CTS)
984 v24_sig &= ~RFCOMM_V24_RTR;
985 if (clear & TIOCM_RI)
986 v24_sig &= ~RFCOMM_V24_IC;
987 if (clear & TIOCM_CD)
988 v24_sig &= ~RFCOMM_V24_DV;
990 rfcomm_dlc_set_modem_status(dlc, v24_sig);
995 /* ---- TTY structure ---- */
997 static struct tty_operations rfcomm_ops = {
998 .open = rfcomm_tty_open,
999 .close = rfcomm_tty_close,
1000 .write = rfcomm_tty_write,
1001 .write_room = rfcomm_tty_write_room,
1002 .chars_in_buffer = rfcomm_tty_chars_in_buffer,
1003 .flush_buffer = rfcomm_tty_flush_buffer,
1004 .ioctl = rfcomm_tty_ioctl,
1005 .throttle = rfcomm_tty_throttle,
1006 .unthrottle = rfcomm_tty_unthrottle,
1007 .set_termios = rfcomm_tty_set_termios,
1008 .send_xchar = rfcomm_tty_send_xchar,
1009 .hangup = rfcomm_tty_hangup,
1010 .wait_until_sent = rfcomm_tty_wait_until_sent,
1011 .read_proc = rfcomm_tty_read_proc,
1012 .tiocmget = rfcomm_tty_tiocmget,
1013 .tiocmset = rfcomm_tty_tiocmset,
1016 int rfcomm_init_ttys(void)
1018 rfcomm_tty_driver = alloc_tty_driver(RFCOMM_TTY_PORTS);
1019 if (!rfcomm_tty_driver)
1022 rfcomm_tty_driver->owner = THIS_MODULE;
1023 rfcomm_tty_driver->driver_name = "rfcomm";
1024 rfcomm_tty_driver->devfs_name = "bluetooth/rfcomm/";
1025 rfcomm_tty_driver->name = "rfcomm";
1026 rfcomm_tty_driver->major = RFCOMM_TTY_MAJOR;
1027 rfcomm_tty_driver->minor_start = RFCOMM_TTY_MINOR;
1028 rfcomm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1029 rfcomm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1030 rfcomm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1031 rfcomm_tty_driver->init_termios = tty_std_termios;
1032 rfcomm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1033 tty_set_operations(rfcomm_tty_driver, &rfcomm_ops);
1035 if (tty_register_driver(rfcomm_tty_driver)) {
1036 BT_ERR("Can't register RFCOMM TTY driver");
1037 put_tty_driver(rfcomm_tty_driver);
1041 BT_INFO("RFCOMM TTY layer initialized");
1046 void rfcomm_cleanup_ttys(void)
1048 tty_unregister_driver(rfcomm_tty_driver);
1049 put_tty_driver(rfcomm_tty_driver);