Linux 2.6.17.7
[linux/fpc-iii.git] / net / bluetooth / rfcomm / tty.c
blob74368f79ee5d084e0645c6af221062567f475e47
1 /*
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.
25 * RFCOMM TTY.
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
45 #undef BT_DBG
46 #define BT_DBG(D...)
47 #endif
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;
56 struct rfcomm_dev {
57 struct list_head list;
58 atomic_t refcnt;
60 char name[12];
61 int id;
62 unsigned long flags;
63 int opened;
64 int err;
66 bdaddr_t src;
67 bdaddr_t dst;
68 u8 channel;
70 uint modem_status;
72 struct rfcomm_dlc *dlc;
73 struct tty_struct *tty;
74 wait_queue_head_t wait;
75 struct tasklet_struct wakeup_task;
77 atomic_t wmem_alloc;
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);
96 rfcomm_dlc_lock(dlc);
97 /* Detach DLC if it's owned by this dev */
98 if (dlc->owner == dev)
99 dlc->owner = NULL;
100 rfcomm_dlc_unlock(dlc);
102 rfcomm_dlc_put(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
108 refcounting bugs. */
109 BUG_ON(!list_empty(&dev->list));
111 kfree(dev);
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()
131 anyway. */
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;
139 struct list_head *p;
141 list_for_each(p, &rfcomm_dev_list) {
142 dev = list_entry(p, struct rfcomm_dev, list);
143 if (dev->id == id)
144 return dev;
147 return NULL;
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);
157 if (dev)
158 rfcomm_dev_hold(dev);
160 read_unlock(&rfcomm_dev_lock);
162 return dev;
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;
169 int err = 0;
171 BT_DBG("id %d channel %d", req->dev_id, req->channel);
173 dev = kmalloc(sizeof(struct rfcomm_dev), GFP_KERNEL);
174 if (!dev)
175 return -ENOMEM;
176 memset(dev, 0, sizeof(struct rfcomm_dev));
178 write_lock_bh(&rfcomm_dev_lock);
180 if (req->dev_id < 0) {
181 dev->id = 0;
183 list_for_each(p, &rfcomm_dev_list) {
184 if (list_entry(p, struct rfcomm_dev, list)->id != dev->id)
185 break;
187 dev->id++;
188 head = p;
190 } else {
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) {
197 err = -EADDRINUSE;
198 goto out;
201 if (entry->id > dev->id - 1)
202 break;
204 head = p;
208 if ((dev->id < 0) || (dev->id > RFCOMM_MAX_DEV - 1)) {
209 err = -ENFILE;
210 goto out;
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;
233 dlc->owner = dev;
234 dev->dlc = dlc;
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);
241 out:
242 write_unlock_bh(&rfcomm_dev_lock);
244 if (err) {
245 kfree(dev);
246 return err;
249 tty_register_device(rfcomm_tty_driver, dev->id, NULL);
251 return dev->id;
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);
262 rfcomm_dev_put(dev);
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);
279 rfcomm_dev_put(dev);
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);
294 if (skb) {
295 rfcomm_set_owner_w(skb, dev);
296 return skb;
299 return NULL;
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;
310 int id;
312 if (copy_from_user(&req, arg, sizeof(req)))
313 return -EFAULT;
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))
318 return -EPERM;
320 if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
321 /* Socket must be connected */
322 if (sk->sk_state != BT_CONNECTED)
323 return -EBADFD;
325 dlc = rfcomm_pi(sk)->dlc;
326 rfcomm_dlc_hold(dlc);
327 } else {
328 dlc = rfcomm_dlc_alloc(GFP_KERNEL);
329 if (!dlc)
330 return -ENOMEM;
333 id = rfcomm_dev_add(&req, dlc);
334 if (id < 0) {
335 rfcomm_dlc_put(dlc);
336 return id;
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;
345 return id;
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)))
354 return -EFAULT;
356 BT_DBG("dev_id %id flags 0x%x", req.dev_id, req.flags);
358 if (!(dev = rfcomm_dev_get(req.dev_id)))
359 return -ENODEV;
361 if (dev->flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN)) {
362 rfcomm_dev_put(dev);
363 return -EPERM;
366 if (req.flags & (1 << RFCOMM_HANGUP_NOW))
367 rfcomm_dlc_close(dev->dlc, 0);
369 rfcomm_dev_del(dev);
370 rfcomm_dev_put(dev);
371 return 0;
374 static int rfcomm_get_dev_list(void __user *arg)
376 struct rfcomm_dev_list_req *dl;
377 struct rfcomm_dev_info *di;
378 struct list_head *p;
379 int n = 0, size, err;
380 u16 dev_num;
382 BT_DBG("");
384 if (get_user(dev_num, (u16 __user *) arg))
385 return -EFAULT;
387 if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di))
388 return -EINVAL;
390 size = sizeof(*dl) + dev_num * sizeof(*di);
392 if (!(dl = kmalloc(size, GFP_KERNEL)))
393 return -ENOMEM;
395 di = dl->dev_info;
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);
407 if (++n >= dev_num)
408 break;
411 read_unlock_bh(&rfcomm_dev_lock);
413 dl->dev_num = n;
414 size = sizeof(*dl) + n * sizeof(*di);
416 err = copy_to_user(arg, dl, size);
417 kfree(dl);
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;
426 int err = 0;
428 BT_DBG("");
430 if (copy_from_user(&di, arg, sizeof(di)))
431 return -EFAULT;
433 if (!(dev = rfcomm_dev_get(di.id)))
434 return -ENODEV;
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)))
443 err = -EFAULT;
445 rfcomm_dev_put(dev);
446 return err;
449 int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
451 BT_DBG("cmd %d arg %p", cmd, arg);
453 switch (cmd) {
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);
467 return -EINVAL;
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)) {
477 kfree_skb(skb);
478 return;
481 BT_DBG("dlc %p tty %p len %d", dlc, tty, skb->len);
483 if (test_bit(TTY_DONT_FLIP, &tty->flags)) {
484 tty_buffer_request_room(tty, skb->len);
485 tty_insert_flip_string(tty, skb->data, skb->len);
486 tty_flip_buffer_push(tty);
487 } else
488 tty->ldisc.receive_buf(tty, skb->data, NULL, skb->len);
490 kfree_skb(skb);
493 static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
495 struct rfcomm_dev *dev = dlc->owner;
496 if (!dev)
497 return;
499 BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
501 dev->err = err;
502 wake_up_interruptible(&dev->wait);
504 if (dlc->state == BT_CLOSED) {
505 if (!dev->tty) {
506 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
507 rfcomm_dev_hold(dev);
508 rfcomm_dev_del(dev);
510 /* We have to drop DLC lock here, otherwise
511 rfcomm_dev_put() will dead lock if it's
512 the last reference. */
513 rfcomm_dlc_unlock(dlc);
514 rfcomm_dev_put(dev);
515 rfcomm_dlc_lock(dlc);
517 } else
518 tty_hangup(dev->tty);
522 static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig)
524 struct rfcomm_dev *dev = dlc->owner;
525 if (!dev)
526 return;
528 BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc, dev, v24_sig);
530 if ((dev->modem_status & TIOCM_CD) && !(v24_sig & RFCOMM_V24_DV)) {
531 if (dev->tty && !C_CLOCAL(dev->tty))
532 tty_hangup(dev->tty);
535 dev->modem_status =
536 ((v24_sig & RFCOMM_V24_RTC) ? (TIOCM_DSR | TIOCM_DTR) : 0) |
537 ((v24_sig & RFCOMM_V24_RTR) ? (TIOCM_RTS | TIOCM_CTS) : 0) |
538 ((v24_sig & RFCOMM_V24_IC) ? TIOCM_RI : 0) |
539 ((v24_sig & RFCOMM_V24_DV) ? TIOCM_CD : 0);
542 /* ---- TTY functions ---- */
543 static void rfcomm_tty_wakeup(unsigned long arg)
545 struct rfcomm_dev *dev = (void *) arg;
546 struct tty_struct *tty = dev->tty;
547 if (!tty)
548 return;
550 BT_DBG("dev %p tty %p", dev, tty);
552 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && tty->ldisc.write_wakeup)
553 (tty->ldisc.write_wakeup)(tty);
555 wake_up_interruptible(&tty->write_wait);
556 #ifdef SERIAL_HAVE_POLL_WAIT
557 wake_up_interruptible(&tty->poll_wait);
558 #endif
561 static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
563 DECLARE_WAITQUEUE(wait, current);
564 struct rfcomm_dev *dev;
565 struct rfcomm_dlc *dlc;
566 int err, id;
568 id = tty->index;
570 BT_DBG("tty %p id %d", tty, id);
572 /* We don't leak this refcount. For reasons which are not entirely
573 clear, the TTY layer will call our ->close() method even if the
574 open fails. We decrease the refcount there, and decreasing it
575 here too would cause breakage. */
576 dev = rfcomm_dev_get(id);
577 if (!dev)
578 return -ENODEV;
580 BT_DBG("dev %p dst %s channel %d opened %d", dev, batostr(&dev->dst), dev->channel, dev->opened);
582 if (dev->opened++ != 0)
583 return 0;
585 dlc = dev->dlc;
587 /* Attach TTY and open DLC */
589 rfcomm_dlc_lock(dlc);
590 tty->driver_data = dev;
591 dev->tty = tty;
592 rfcomm_dlc_unlock(dlc);
593 set_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
595 err = rfcomm_dlc_open(dlc, &dev->src, &dev->dst, dev->channel);
596 if (err < 0)
597 return err;
599 /* Wait for DLC to connect */
600 add_wait_queue(&dev->wait, &wait);
601 while (1) {
602 set_current_state(TASK_INTERRUPTIBLE);
604 if (dlc->state == BT_CLOSED) {
605 err = -dev->err;
606 break;
609 if (dlc->state == BT_CONNECTED)
610 break;
612 if (signal_pending(current)) {
613 err = -EINTR;
614 break;
617 schedule();
619 set_current_state(TASK_RUNNING);
620 remove_wait_queue(&dev->wait, &wait);
622 return err;
625 static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
627 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
628 if (!dev)
629 return;
631 BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc, dev->opened);
633 if (--dev->opened == 0) {
634 /* Close DLC and dettach TTY */
635 rfcomm_dlc_close(dev->dlc, 0);
637 clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
638 tasklet_kill(&dev->wakeup_task);
640 rfcomm_dlc_lock(dev->dlc);
641 tty->driver_data = NULL;
642 dev->tty = NULL;
643 rfcomm_dlc_unlock(dev->dlc);
646 rfcomm_dev_put(dev);
649 static int rfcomm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
651 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
652 struct rfcomm_dlc *dlc = dev->dlc;
653 struct sk_buff *skb;
654 int err = 0, sent = 0, size;
656 BT_DBG("tty %p count %d", tty, count);
658 while (count) {
659 size = min_t(uint, count, dlc->mtu);
661 skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, GFP_ATOMIC);
663 if (!skb)
664 break;
666 skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
668 memcpy(skb_put(skb, size), buf + sent, size);
670 if ((err = rfcomm_dlc_send(dlc, skb)) < 0) {
671 kfree_skb(skb);
672 break;
675 sent += size;
676 count -= size;
679 return sent ? sent : err;
682 static int rfcomm_tty_write_room(struct tty_struct *tty)
684 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
685 int room;
687 BT_DBG("tty %p", tty);
689 room = rfcomm_room(dev->dlc) - atomic_read(&dev->wmem_alloc);
690 if (room < 0)
691 room = 0;
692 return room;
695 static int rfcomm_tty_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd, unsigned long arg)
697 BT_DBG("tty %p cmd 0x%02x", tty, cmd);
699 switch (cmd) {
700 case TCGETS:
701 BT_DBG("TCGETS is not supported");
702 return -ENOIOCTLCMD;
704 case TCSETS:
705 BT_DBG("TCSETS is not supported");
706 return -ENOIOCTLCMD;
708 case TIOCMIWAIT:
709 BT_DBG("TIOCMIWAIT");
710 break;
712 case TIOCGICOUNT:
713 BT_DBG("TIOCGICOUNT");
714 break;
716 case TIOCGSERIAL:
717 BT_ERR("TIOCGSERIAL is not supported");
718 return -ENOIOCTLCMD;
720 case TIOCSSERIAL:
721 BT_ERR("TIOCSSERIAL is not supported");
722 return -ENOIOCTLCMD;
724 case TIOCSERGSTRUCT:
725 BT_ERR("TIOCSERGSTRUCT is not supported");
726 return -ENOIOCTLCMD;
728 case TIOCSERGETLSR:
729 BT_ERR("TIOCSERGETLSR is not supported");
730 return -ENOIOCTLCMD;
732 case TIOCSERCONFIG:
733 BT_ERR("TIOCSERCONFIG is not supported");
734 return -ENOIOCTLCMD;
736 default:
737 return -ENOIOCTLCMD; /* ioctls which we must ignore */
741 return -ENOIOCTLCMD;
744 static void rfcomm_tty_set_termios(struct tty_struct *tty, struct termios *old)
746 struct termios *new = (struct termios *) tty->termios;
747 int old_baud_rate = tty_termios_baud_rate(old);
748 int new_baud_rate = tty_termios_baud_rate(new);
750 u8 baud, data_bits, stop_bits, parity, x_on, x_off;
751 u16 changes = 0;
753 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
755 BT_DBG("tty %p termios %p", tty, old);
757 /* Handle turning off CRTSCTS */
758 if ((old->c_cflag & CRTSCTS) && !(new->c_cflag & CRTSCTS))
759 BT_DBG("Turning off CRTSCTS unsupported");
761 /* Parity on/off and when on, odd/even */
762 if (((old->c_cflag & PARENB) != (new->c_cflag & PARENB)) ||
763 ((old->c_cflag & PARODD) != (new->c_cflag & PARODD)) ) {
764 changes |= RFCOMM_RPN_PM_PARITY;
765 BT_DBG("Parity change detected.");
768 /* Mark and space parity are not supported! */
769 if (new->c_cflag & PARENB) {
770 if (new->c_cflag & PARODD) {
771 BT_DBG("Parity is ODD");
772 parity = RFCOMM_RPN_PARITY_ODD;
773 } else {
774 BT_DBG("Parity is EVEN");
775 parity = RFCOMM_RPN_PARITY_EVEN;
777 } else {
778 BT_DBG("Parity is OFF");
779 parity = RFCOMM_RPN_PARITY_NONE;
782 /* Setting the x_on / x_off characters */
783 if (old->c_cc[VSTOP] != new->c_cc[VSTOP]) {
784 BT_DBG("XOFF custom");
785 x_on = new->c_cc[VSTOP];
786 changes |= RFCOMM_RPN_PM_XON;
787 } else {
788 BT_DBG("XOFF default");
789 x_on = RFCOMM_RPN_XON_CHAR;
792 if (old->c_cc[VSTART] != new->c_cc[VSTART]) {
793 BT_DBG("XON custom");
794 x_off = new->c_cc[VSTART];
795 changes |= RFCOMM_RPN_PM_XOFF;
796 } else {
797 BT_DBG("XON default");
798 x_off = RFCOMM_RPN_XOFF_CHAR;
801 /* Handle setting of stop bits */
802 if ((old->c_cflag & CSTOPB) != (new->c_cflag & CSTOPB))
803 changes |= RFCOMM_RPN_PM_STOP;
805 /* POSIX does not support 1.5 stop bits and RFCOMM does not
806 * support 2 stop bits. So a request for 2 stop bits gets
807 * translated to 1.5 stop bits */
808 if (new->c_cflag & CSTOPB) {
809 stop_bits = RFCOMM_RPN_STOP_15;
810 } else {
811 stop_bits = RFCOMM_RPN_STOP_1;
814 /* Handle number of data bits [5-8] */
815 if ((old->c_cflag & CSIZE) != (new->c_cflag & CSIZE))
816 changes |= RFCOMM_RPN_PM_DATA;
818 switch (new->c_cflag & CSIZE) {
819 case CS5:
820 data_bits = RFCOMM_RPN_DATA_5;
821 break;
822 case CS6:
823 data_bits = RFCOMM_RPN_DATA_6;
824 break;
825 case CS7:
826 data_bits = RFCOMM_RPN_DATA_7;
827 break;
828 case CS8:
829 data_bits = RFCOMM_RPN_DATA_8;
830 break;
831 default:
832 data_bits = RFCOMM_RPN_DATA_8;
833 break;
836 /* Handle baudrate settings */
837 if (old_baud_rate != new_baud_rate)
838 changes |= RFCOMM_RPN_PM_BITRATE;
840 switch (new_baud_rate) {
841 case 2400:
842 baud = RFCOMM_RPN_BR_2400;
843 break;
844 case 4800:
845 baud = RFCOMM_RPN_BR_4800;
846 break;
847 case 7200:
848 baud = RFCOMM_RPN_BR_7200;
849 break;
850 case 9600:
851 baud = RFCOMM_RPN_BR_9600;
852 break;
853 case 19200:
854 baud = RFCOMM_RPN_BR_19200;
855 break;
856 case 38400:
857 baud = RFCOMM_RPN_BR_38400;
858 break;
859 case 57600:
860 baud = RFCOMM_RPN_BR_57600;
861 break;
862 case 115200:
863 baud = RFCOMM_RPN_BR_115200;
864 break;
865 case 230400:
866 baud = RFCOMM_RPN_BR_230400;
867 break;
868 default:
869 /* 9600 is standard accordinag to the RFCOMM specification */
870 baud = RFCOMM_RPN_BR_9600;
871 break;
875 if (changes)
876 rfcomm_send_rpn(dev->dlc->session, 1, dev->dlc->dlci, baud,
877 data_bits, stop_bits, parity,
878 RFCOMM_RPN_FLOW_NONE, x_on, x_off, changes);
880 return;
883 static void rfcomm_tty_throttle(struct tty_struct *tty)
885 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
887 BT_DBG("tty %p dev %p", tty, dev);
889 rfcomm_dlc_throttle(dev->dlc);
892 static void rfcomm_tty_unthrottle(struct tty_struct *tty)
894 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
896 BT_DBG("tty %p dev %p", tty, dev);
898 rfcomm_dlc_unthrottle(dev->dlc);
901 static int rfcomm_tty_chars_in_buffer(struct tty_struct *tty)
903 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
904 struct rfcomm_dlc *dlc = dev->dlc;
906 BT_DBG("tty %p dev %p", tty, dev);
908 if (!skb_queue_empty(&dlc->tx_queue))
909 return dlc->mtu;
911 return 0;
914 static void rfcomm_tty_flush_buffer(struct tty_struct *tty)
916 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
917 if (!dev)
918 return;
920 BT_DBG("tty %p dev %p", tty, dev);
922 skb_queue_purge(&dev->dlc->tx_queue);
924 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && tty->ldisc.write_wakeup)
925 tty->ldisc.write_wakeup(tty);
928 static void rfcomm_tty_send_xchar(struct tty_struct *tty, char ch)
930 BT_DBG("tty %p ch %c", tty, ch);
933 static void rfcomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
935 BT_DBG("tty %p timeout %d", tty, timeout);
938 static void rfcomm_tty_hangup(struct tty_struct *tty)
940 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
941 if (!dev)
942 return;
944 BT_DBG("tty %p dev %p", tty, dev);
946 rfcomm_tty_flush_buffer(tty);
948 if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags))
949 rfcomm_dev_del(dev);
952 static int rfcomm_tty_read_proc(char *buf, char **start, off_t offset, int len, int *eof, void *unused)
954 return 0;
957 static int rfcomm_tty_tiocmget(struct tty_struct *tty, struct file *filp)
959 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
961 BT_DBG("tty %p dev %p", tty, dev);
963 return dev->modem_status;
966 static int rfcomm_tty_tiocmset(struct tty_struct *tty, struct file *filp, unsigned int set, unsigned int clear)
968 struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
969 struct rfcomm_dlc *dlc = dev->dlc;
970 u8 v24_sig;
972 BT_DBG("tty %p dev %p set 0x%02x clear 0x%02x", tty, dev, set, clear);
974 rfcomm_dlc_get_modem_status(dlc, &v24_sig);
976 if (set & TIOCM_DSR || set & TIOCM_DTR)
977 v24_sig |= RFCOMM_V24_RTC;
978 if (set & TIOCM_RTS || set & TIOCM_CTS)
979 v24_sig |= RFCOMM_V24_RTR;
980 if (set & TIOCM_RI)
981 v24_sig |= RFCOMM_V24_IC;
982 if (set & TIOCM_CD)
983 v24_sig |= RFCOMM_V24_DV;
985 if (clear & TIOCM_DSR || clear & TIOCM_DTR)
986 v24_sig &= ~RFCOMM_V24_RTC;
987 if (clear & TIOCM_RTS || clear & TIOCM_CTS)
988 v24_sig &= ~RFCOMM_V24_RTR;
989 if (clear & TIOCM_RI)
990 v24_sig &= ~RFCOMM_V24_IC;
991 if (clear & TIOCM_CD)
992 v24_sig &= ~RFCOMM_V24_DV;
994 rfcomm_dlc_set_modem_status(dlc, v24_sig);
996 return 0;
999 /* ---- TTY structure ---- */
1001 static struct tty_operations rfcomm_ops = {
1002 .open = rfcomm_tty_open,
1003 .close = rfcomm_tty_close,
1004 .write = rfcomm_tty_write,
1005 .write_room = rfcomm_tty_write_room,
1006 .chars_in_buffer = rfcomm_tty_chars_in_buffer,
1007 .flush_buffer = rfcomm_tty_flush_buffer,
1008 .ioctl = rfcomm_tty_ioctl,
1009 .throttle = rfcomm_tty_throttle,
1010 .unthrottle = rfcomm_tty_unthrottle,
1011 .set_termios = rfcomm_tty_set_termios,
1012 .send_xchar = rfcomm_tty_send_xchar,
1013 .hangup = rfcomm_tty_hangup,
1014 .wait_until_sent = rfcomm_tty_wait_until_sent,
1015 .read_proc = rfcomm_tty_read_proc,
1016 .tiocmget = rfcomm_tty_tiocmget,
1017 .tiocmset = rfcomm_tty_tiocmset,
1020 int rfcomm_init_ttys(void)
1022 rfcomm_tty_driver = alloc_tty_driver(RFCOMM_TTY_PORTS);
1023 if (!rfcomm_tty_driver)
1024 return -1;
1026 rfcomm_tty_driver->owner = THIS_MODULE;
1027 rfcomm_tty_driver->driver_name = "rfcomm";
1028 rfcomm_tty_driver->devfs_name = "bluetooth/rfcomm/";
1029 rfcomm_tty_driver->name = "rfcomm";
1030 rfcomm_tty_driver->major = RFCOMM_TTY_MAJOR;
1031 rfcomm_tty_driver->minor_start = RFCOMM_TTY_MINOR;
1032 rfcomm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1033 rfcomm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1034 rfcomm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1035 rfcomm_tty_driver->init_termios = tty_std_termios;
1036 rfcomm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1037 tty_set_operations(rfcomm_tty_driver, &rfcomm_ops);
1039 if (tty_register_driver(rfcomm_tty_driver)) {
1040 BT_ERR("Can't register RFCOMM TTY driver");
1041 put_tty_driver(rfcomm_tty_driver);
1042 return -1;
1045 BT_INFO("RFCOMM TTY layer initialized");
1047 return 0;
1050 void rfcomm_cleanup_ttys(void)
1052 tty_unregister_driver(rfcomm_tty_driver);
1053 put_tty_driver(rfcomm_tty_driver);