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/module.h>
32 #include <linux/tty.h>
33 #include <linux/tty_driver.h>
34 #include <linux/tty_flip.h>
36 #include <linux/capability.h>
37 #include <linux/slab.h>
38 #include <linux/skbuff.h>
40 #include <net/bluetooth/bluetooth.h>
41 #include <net/bluetooth/hci_core.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
;
77 struct device
*tty_dev
;
82 static LIST_HEAD(rfcomm_dev_list
);
83 static DEFINE_RWLOCK(rfcomm_dev_lock
);
85 static void rfcomm_dev_data_ready(struct rfcomm_dlc
*dlc
, struct sk_buff
*skb
);
86 static void rfcomm_dev_state_change(struct rfcomm_dlc
*dlc
, int err
);
87 static void rfcomm_dev_modem_status(struct rfcomm_dlc
*dlc
, u8 v24_sig
);
89 static void rfcomm_tty_wakeup(unsigned long arg
);
91 /* ---- Device functions ---- */
92 static void rfcomm_dev_destruct(struct rfcomm_dev
*dev
)
94 struct rfcomm_dlc
*dlc
= dev
->dlc
;
96 BT_DBG("dev %p dlc %p", dev
, dlc
);
98 write_lock_bh(&rfcomm_dev_lock
);
99 list_del_init(&dev
->list
);
100 write_unlock_bh(&rfcomm_dev_lock
);
102 rfcomm_dlc_lock(dlc
);
103 /* Detach DLC if it's owned by this dev */
104 if (dlc
->owner
== dev
)
106 rfcomm_dlc_unlock(dlc
);
110 tty_unregister_device(rfcomm_tty_driver
, dev
->id
);
112 /* Refcount should only hit zero when called from rfcomm_dev_del()
113 which will have taken us off the list. Everything else are
115 BUG_ON(!list_empty(&dev
->list
));
119 /* It's safe to call module_put() here because socket still
120 holds reference to this module. */
121 module_put(THIS_MODULE
);
124 static inline void rfcomm_dev_hold(struct rfcomm_dev
*dev
)
126 atomic_inc(&dev
->refcnt
);
129 static inline void rfcomm_dev_put(struct rfcomm_dev
*dev
)
131 /* The reason this isn't actually a race, as you no
132 doubt have a little voice screaming at you in your
133 head, is that the refcount should never actually
134 reach zero unless the device has already been taken
135 off the list, in rfcomm_dev_del(). And if that's not
136 true, we'll hit the BUG() in rfcomm_dev_destruct()
138 if (atomic_dec_and_test(&dev
->refcnt
))
139 rfcomm_dev_destruct(dev
);
142 static struct rfcomm_dev
*__rfcomm_dev_get(int id
)
144 struct rfcomm_dev
*dev
;
147 list_for_each(p
, &rfcomm_dev_list
) {
148 dev
= list_entry(p
, struct rfcomm_dev
, list
);
156 static inline struct rfcomm_dev
*rfcomm_dev_get(int id
)
158 struct rfcomm_dev
*dev
;
160 read_lock(&rfcomm_dev_lock
);
162 dev
= __rfcomm_dev_get(id
);
165 if (test_bit(RFCOMM_TTY_RELEASED
, &dev
->flags
))
168 rfcomm_dev_hold(dev
);
171 read_unlock(&rfcomm_dev_lock
);
176 static struct device
*rfcomm_get_device(struct rfcomm_dev
*dev
)
178 struct hci_dev
*hdev
;
179 struct hci_conn
*conn
;
181 hdev
= hci_get_route(&dev
->dst
, &dev
->src
);
185 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &dev
->dst
);
189 return conn
? &conn
->dev
: NULL
;
192 static ssize_t
show_address(struct device
*tty_dev
, struct device_attribute
*attr
, char *buf
)
194 struct rfcomm_dev
*dev
= dev_get_drvdata(tty_dev
);
196 baswap(&bdaddr
, &dev
->dst
);
197 return sprintf(buf
, "%s\n", batostr(&bdaddr
));
200 static ssize_t
show_channel(struct device
*tty_dev
, struct device_attribute
*attr
, char *buf
)
202 struct rfcomm_dev
*dev
= dev_get_drvdata(tty_dev
);
203 return sprintf(buf
, "%d\n", dev
->channel
);
206 static DEVICE_ATTR(address
, S_IRUGO
, show_address
, NULL
);
207 static DEVICE_ATTR(channel
, S_IRUGO
, show_channel
, NULL
);
209 static int rfcomm_dev_add(struct rfcomm_dev_req
*req
, struct rfcomm_dlc
*dlc
)
211 struct rfcomm_dev
*dev
;
212 struct list_head
*head
= &rfcomm_dev_list
, *p
;
215 BT_DBG("id %d channel %d", req
->dev_id
, req
->channel
);
217 dev
= kzalloc(sizeof(struct rfcomm_dev
), GFP_KERNEL
);
221 write_lock_bh(&rfcomm_dev_lock
);
223 if (req
->dev_id
< 0) {
226 list_for_each(p
, &rfcomm_dev_list
) {
227 if (list_entry(p
, struct rfcomm_dev
, list
)->id
!= dev
->id
)
234 dev
->id
= req
->dev_id
;
236 list_for_each(p
, &rfcomm_dev_list
) {
237 struct rfcomm_dev
*entry
= list_entry(p
, struct rfcomm_dev
, list
);
239 if (entry
->id
== dev
->id
) {
244 if (entry
->id
> dev
->id
- 1)
251 if ((dev
->id
< 0) || (dev
->id
> RFCOMM_MAX_DEV
- 1)) {
256 sprintf(dev
->name
, "rfcomm%d", dev
->id
);
258 list_add(&dev
->list
, head
);
259 atomic_set(&dev
->refcnt
, 1);
261 bacpy(&dev
->src
, &req
->src
);
262 bacpy(&dev
->dst
, &req
->dst
);
263 dev
->channel
= req
->channel
;
265 dev
->flags
= req
->flags
&
266 ((1 << RFCOMM_RELEASE_ONHUP
) | (1 << RFCOMM_REUSE_DLC
));
268 init_waitqueue_head(&dev
->wait
);
269 tasklet_init(&dev
->wakeup_task
, rfcomm_tty_wakeup
, (unsigned long) dev
);
271 rfcomm_dlc_lock(dlc
);
272 dlc
->data_ready
= rfcomm_dev_data_ready
;
273 dlc
->state_change
= rfcomm_dev_state_change
;
274 dlc
->modem_status
= rfcomm_dev_modem_status
;
278 rfcomm_dlc_unlock(dlc
);
280 /* It's safe to call __module_get() here because socket already
281 holds reference to this module. */
282 __module_get(THIS_MODULE
);
285 write_unlock_bh(&rfcomm_dev_lock
);
292 dev
->tty_dev
= tty_register_device(rfcomm_tty_driver
, dev
->id
, NULL
);
294 if (IS_ERR(dev
->tty_dev
)) {
295 err
= PTR_ERR(dev
->tty_dev
);
296 list_del(&dev
->list
);
301 dev_set_drvdata(dev
->tty_dev
, dev
);
303 if (device_create_file(dev
->tty_dev
, &dev_attr_address
) < 0)
304 BT_ERR("Failed to create address attribute");
306 if (device_create_file(dev
->tty_dev
, &dev_attr_channel
) < 0)
307 BT_ERR("Failed to create channel attribute");
312 static void rfcomm_dev_del(struct rfcomm_dev
*dev
)
314 BT_DBG("dev %p", dev
);
316 set_bit(RFCOMM_TTY_RELEASED
, &dev
->flags
);
320 /* ---- Send buffer ---- */
321 static inline unsigned int rfcomm_room(struct rfcomm_dlc
*dlc
)
323 /* We can't let it be zero, because we don't get a callback
324 when tx_credits becomes nonzero, hence we'd never wake up */
325 return dlc
->mtu
* (dlc
->tx_credits
?:1);
328 static void rfcomm_wfree(struct sk_buff
*skb
)
330 struct rfcomm_dev
*dev
= (void *) skb
->sk
;
331 atomic_sub(skb
->truesize
, &dev
->wmem_alloc
);
332 if (test_bit(RFCOMM_TTY_ATTACHED
, &dev
->flags
))
333 tasklet_schedule(&dev
->wakeup_task
);
337 static inline void rfcomm_set_owner_w(struct sk_buff
*skb
, struct rfcomm_dev
*dev
)
339 rfcomm_dev_hold(dev
);
340 atomic_add(skb
->truesize
, &dev
->wmem_alloc
);
341 skb
->sk
= (void *) dev
;
342 skb
->destructor
= rfcomm_wfree
;
345 static struct sk_buff
*rfcomm_wmalloc(struct rfcomm_dev
*dev
, unsigned long size
, gfp_t priority
)
347 if (atomic_read(&dev
->wmem_alloc
) < rfcomm_room(dev
->dlc
)) {
348 struct sk_buff
*skb
= alloc_skb(size
, priority
);
350 rfcomm_set_owner_w(skb
, dev
);
357 /* ---- Device IOCTLs ---- */
359 #define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP))
361 static int rfcomm_create_dev(struct sock
*sk
, void __user
*arg
)
363 struct rfcomm_dev_req req
;
364 struct rfcomm_dlc
*dlc
;
367 if (copy_from_user(&req
, arg
, sizeof(req
)))
370 BT_DBG("sk %p dev_id %d flags 0x%x", sk
, req
.dev_id
, req
.flags
);
372 if (req
.flags
!= NOCAP_FLAGS
&& !capable(CAP_NET_ADMIN
))
375 if (req
.flags
& (1 << RFCOMM_REUSE_DLC
)) {
376 /* Socket must be connected */
377 if (sk
->sk_state
!= BT_CONNECTED
)
380 dlc
= rfcomm_pi(sk
)->dlc
;
381 rfcomm_dlc_hold(dlc
);
383 dlc
= rfcomm_dlc_alloc(GFP_KERNEL
);
388 id
= rfcomm_dev_add(&req
, dlc
);
394 if (req
.flags
& (1 << RFCOMM_REUSE_DLC
)) {
395 /* DLC is now used by device.
396 * Socket must be disconnected */
397 sk
->sk_state
= BT_CLOSED
;
403 static int rfcomm_release_dev(void __user
*arg
)
405 struct rfcomm_dev_req req
;
406 struct rfcomm_dev
*dev
;
408 if (copy_from_user(&req
, arg
, sizeof(req
)))
411 BT_DBG("dev_id %d flags 0x%x", req
.dev_id
, req
.flags
);
413 if (!(dev
= rfcomm_dev_get(req
.dev_id
)))
416 if (dev
->flags
!= NOCAP_FLAGS
&& !capable(CAP_NET_ADMIN
)) {
421 if (req
.flags
& (1 << RFCOMM_HANGUP_NOW
))
422 rfcomm_dlc_close(dev
->dlc
, 0);
424 /* Shut down TTY synchronously before freeing rfcomm_dev */
426 tty_vhangup(dev
->tty
);
433 static int rfcomm_get_dev_list(void __user
*arg
)
435 struct rfcomm_dev_list_req
*dl
;
436 struct rfcomm_dev_info
*di
;
438 int n
= 0, size
, err
;
443 if (get_user(dev_num
, (u16 __user
*) arg
))
446 if (!dev_num
|| dev_num
> (PAGE_SIZE
* 4) / sizeof(*di
))
449 size
= sizeof(*dl
) + dev_num
* sizeof(*di
);
451 if (!(dl
= kmalloc(size
, GFP_KERNEL
)))
456 read_lock_bh(&rfcomm_dev_lock
);
458 list_for_each(p
, &rfcomm_dev_list
) {
459 struct rfcomm_dev
*dev
= list_entry(p
, struct rfcomm_dev
, list
);
460 if (test_bit(RFCOMM_TTY_RELEASED
, &dev
->flags
))
462 (di
+ n
)->id
= dev
->id
;
463 (di
+ n
)->flags
= dev
->flags
;
464 (di
+ n
)->state
= dev
->dlc
->state
;
465 (di
+ n
)->channel
= dev
->channel
;
466 bacpy(&(di
+ n
)->src
, &dev
->src
);
467 bacpy(&(di
+ n
)->dst
, &dev
->dst
);
472 read_unlock_bh(&rfcomm_dev_lock
);
475 size
= sizeof(*dl
) + n
* sizeof(*di
);
477 err
= copy_to_user(arg
, dl
, size
);
480 return err
? -EFAULT
: 0;
483 static int rfcomm_get_dev_info(void __user
*arg
)
485 struct rfcomm_dev
*dev
;
486 struct rfcomm_dev_info di
;
491 if (copy_from_user(&di
, arg
, sizeof(di
)))
494 if (!(dev
= rfcomm_dev_get(di
.id
)))
497 di
.flags
= dev
->flags
;
498 di
.channel
= dev
->channel
;
499 di
.state
= dev
->dlc
->state
;
500 bacpy(&di
.src
, &dev
->src
);
501 bacpy(&di
.dst
, &dev
->dst
);
503 if (copy_to_user(arg
, &di
, sizeof(di
)))
510 int rfcomm_dev_ioctl(struct sock
*sk
, unsigned int cmd
, void __user
*arg
)
512 BT_DBG("cmd %d arg %p", cmd
, arg
);
515 case RFCOMMCREATEDEV
:
516 return rfcomm_create_dev(sk
, arg
);
518 case RFCOMMRELEASEDEV
:
519 return rfcomm_release_dev(arg
);
521 case RFCOMMGETDEVLIST
:
522 return rfcomm_get_dev_list(arg
);
524 case RFCOMMGETDEVINFO
:
525 return rfcomm_get_dev_info(arg
);
531 /* ---- DLC callbacks ---- */
532 static void rfcomm_dev_data_ready(struct rfcomm_dlc
*dlc
, struct sk_buff
*skb
)
534 struct rfcomm_dev
*dev
= dlc
->owner
;
535 struct tty_struct
*tty
;
537 if (!dev
|| !(tty
= dev
->tty
)) {
542 BT_DBG("dlc %p tty %p len %d", dlc
, tty
, skb
->len
);
544 tty_insert_flip_string(tty
, skb
->data
, skb
->len
);
545 tty_flip_buffer_push(tty
);
550 static void rfcomm_dev_state_change(struct rfcomm_dlc
*dlc
, int err
)
552 struct rfcomm_dev
*dev
= dlc
->owner
;
556 BT_DBG("dlc %p dev %p err %d", dlc
, dev
, err
);
559 wake_up_interruptible(&dev
->wait
);
561 if (dlc
->state
== BT_CLOSED
) {
563 if (test_bit(RFCOMM_RELEASE_ONHUP
, &dev
->flags
)) {
564 if (rfcomm_dev_get(dev
->id
) == NULL
)
568 /* We have to drop DLC lock here, otherwise
569 rfcomm_dev_put() will dead lock if it's
570 the last reference. */
571 rfcomm_dlc_unlock(dlc
);
573 rfcomm_dlc_lock(dlc
);
576 tty_hangup(dev
->tty
);
580 static void rfcomm_dev_modem_status(struct rfcomm_dlc
*dlc
, u8 v24_sig
)
582 struct rfcomm_dev
*dev
= dlc
->owner
;
586 BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc
, dev
, v24_sig
);
588 if ((dev
->modem_status
& TIOCM_CD
) && !(v24_sig
& RFCOMM_V24_DV
)) {
589 if (dev
->tty
&& !C_CLOCAL(dev
->tty
))
590 tty_hangup(dev
->tty
);
594 ((v24_sig
& RFCOMM_V24_RTC
) ? (TIOCM_DSR
| TIOCM_DTR
) : 0) |
595 ((v24_sig
& RFCOMM_V24_RTR
) ? (TIOCM_RTS
| TIOCM_CTS
) : 0) |
596 ((v24_sig
& RFCOMM_V24_IC
) ? TIOCM_RI
: 0) |
597 ((v24_sig
& RFCOMM_V24_DV
) ? TIOCM_CD
: 0);
600 /* ---- TTY functions ---- */
601 static void rfcomm_tty_wakeup(unsigned long arg
)
603 struct rfcomm_dev
*dev
= (void *) arg
;
604 struct tty_struct
*tty
= dev
->tty
;
608 BT_DBG("dev %p tty %p", dev
, tty
);
610 if (test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
) && tty
->ldisc
.write_wakeup
)
611 (tty
->ldisc
.write_wakeup
)(tty
);
613 wake_up_interruptible(&tty
->write_wait
);
614 #ifdef SERIAL_HAVE_POLL_WAIT
615 wake_up_interruptible(&tty
->poll_wait
);
619 static int rfcomm_tty_open(struct tty_struct
*tty
, struct file
*filp
)
621 DECLARE_WAITQUEUE(wait
, current
);
622 struct rfcomm_dev
*dev
;
623 struct rfcomm_dlc
*dlc
;
628 BT_DBG("tty %p id %d", tty
, id
);
630 /* We don't leak this refcount. For reasons which are not entirely
631 clear, the TTY layer will call our ->close() method even if the
632 open fails. We decrease the refcount there, and decreasing it
633 here too would cause breakage. */
634 dev
= rfcomm_dev_get(id
);
638 BT_DBG("dev %p dst %s channel %d opened %d", dev
, batostr(&dev
->dst
), dev
->channel
, dev
->opened
);
640 if (dev
->opened
++ != 0)
645 /* Attach TTY and open DLC */
647 rfcomm_dlc_lock(dlc
);
648 tty
->driver_data
= dev
;
650 rfcomm_dlc_unlock(dlc
);
651 set_bit(RFCOMM_TTY_ATTACHED
, &dev
->flags
);
653 err
= rfcomm_dlc_open(dlc
, &dev
->src
, &dev
->dst
, dev
->channel
);
657 /* Wait for DLC to connect */
658 add_wait_queue(&dev
->wait
, &wait
);
660 set_current_state(TASK_INTERRUPTIBLE
);
662 if (dlc
->state
== BT_CLOSED
) {
667 if (dlc
->state
== BT_CONNECTED
)
670 if (signal_pending(current
)) {
677 set_current_state(TASK_RUNNING
);
678 remove_wait_queue(&dev
->wait
, &wait
);
681 device_move(dev
->tty_dev
, rfcomm_get_device(dev
));
686 static void rfcomm_tty_close(struct tty_struct
*tty
, struct file
*filp
)
688 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
692 BT_DBG("tty %p dev %p dlc %p opened %d", tty
, dev
, dev
->dlc
, dev
->opened
);
694 if (--dev
->opened
== 0) {
695 device_move(dev
->tty_dev
, NULL
);
697 /* Close DLC and dettach TTY */
698 rfcomm_dlc_close(dev
->dlc
, 0);
700 clear_bit(RFCOMM_TTY_ATTACHED
, &dev
->flags
);
701 tasklet_kill(&dev
->wakeup_task
);
703 rfcomm_dlc_lock(dev
->dlc
);
704 tty
->driver_data
= NULL
;
706 rfcomm_dlc_unlock(dev
->dlc
);
712 static int rfcomm_tty_write(struct tty_struct
*tty
, const unsigned char *buf
, int count
)
714 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
715 struct rfcomm_dlc
*dlc
= dev
->dlc
;
717 int err
= 0, sent
= 0, size
;
719 BT_DBG("tty %p count %d", tty
, count
);
722 size
= min_t(uint
, count
, dlc
->mtu
);
724 skb
= rfcomm_wmalloc(dev
, size
+ RFCOMM_SKB_RESERVE
, GFP_ATOMIC
);
729 skb_reserve(skb
, RFCOMM_SKB_HEAD_RESERVE
);
731 memcpy(skb_put(skb
, size
), buf
+ sent
, size
);
733 if ((err
= rfcomm_dlc_send(dlc
, skb
)) < 0) {
742 return sent
? sent
: err
;
745 static int rfcomm_tty_write_room(struct tty_struct
*tty
)
747 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
750 BT_DBG("tty %p", tty
);
752 if (!dev
|| !dev
->dlc
)
755 room
= rfcomm_room(dev
->dlc
) - atomic_read(&dev
->wmem_alloc
);
762 static int rfcomm_tty_ioctl(struct tty_struct
*tty
, struct file
*filp
, unsigned int cmd
, unsigned long arg
)
764 BT_DBG("tty %p cmd 0x%02x", tty
, cmd
);
768 BT_DBG("TCGETS is not supported");
772 BT_DBG("TCSETS is not supported");
776 BT_DBG("TIOCMIWAIT");
780 BT_DBG("TIOCGICOUNT");
784 BT_ERR("TIOCGSERIAL is not supported");
788 BT_ERR("TIOCSSERIAL is not supported");
792 BT_ERR("TIOCSERGSTRUCT is not supported");
796 BT_ERR("TIOCSERGETLSR is not supported");
800 BT_ERR("TIOCSERCONFIG is not supported");
804 return -ENOIOCTLCMD
; /* ioctls which we must ignore */
811 static void rfcomm_tty_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
813 struct ktermios
*new = tty
->termios
;
814 int old_baud_rate
= tty_termios_baud_rate(old
);
815 int new_baud_rate
= tty_termios_baud_rate(new);
817 u8 baud
, data_bits
, stop_bits
, parity
, x_on
, x_off
;
820 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
822 BT_DBG("tty %p termios %p", tty
, old
);
824 if (!dev
|| !dev
->dlc
|| !dev
->dlc
->session
)
827 /* Handle turning off CRTSCTS */
828 if ((old
->c_cflag
& CRTSCTS
) && !(new->c_cflag
& CRTSCTS
))
829 BT_DBG("Turning off CRTSCTS unsupported");
831 /* Parity on/off and when on, odd/even */
832 if (((old
->c_cflag
& PARENB
) != (new->c_cflag
& PARENB
)) ||
833 ((old
->c_cflag
& PARODD
) != (new->c_cflag
& PARODD
)) ) {
834 changes
|= RFCOMM_RPN_PM_PARITY
;
835 BT_DBG("Parity change detected.");
838 /* Mark and space parity are not supported! */
839 if (new->c_cflag
& PARENB
) {
840 if (new->c_cflag
& PARODD
) {
841 BT_DBG("Parity is ODD");
842 parity
= RFCOMM_RPN_PARITY_ODD
;
844 BT_DBG("Parity is EVEN");
845 parity
= RFCOMM_RPN_PARITY_EVEN
;
848 BT_DBG("Parity is OFF");
849 parity
= RFCOMM_RPN_PARITY_NONE
;
852 /* Setting the x_on / x_off characters */
853 if (old
->c_cc
[VSTOP
] != new->c_cc
[VSTOP
]) {
854 BT_DBG("XOFF custom");
855 x_on
= new->c_cc
[VSTOP
];
856 changes
|= RFCOMM_RPN_PM_XON
;
858 BT_DBG("XOFF default");
859 x_on
= RFCOMM_RPN_XON_CHAR
;
862 if (old
->c_cc
[VSTART
] != new->c_cc
[VSTART
]) {
863 BT_DBG("XON custom");
864 x_off
= new->c_cc
[VSTART
];
865 changes
|= RFCOMM_RPN_PM_XOFF
;
867 BT_DBG("XON default");
868 x_off
= RFCOMM_RPN_XOFF_CHAR
;
871 /* Handle setting of stop bits */
872 if ((old
->c_cflag
& CSTOPB
) != (new->c_cflag
& CSTOPB
))
873 changes
|= RFCOMM_RPN_PM_STOP
;
875 /* POSIX does not support 1.5 stop bits and RFCOMM does not
876 * support 2 stop bits. So a request for 2 stop bits gets
877 * translated to 1.5 stop bits */
878 if (new->c_cflag
& CSTOPB
) {
879 stop_bits
= RFCOMM_RPN_STOP_15
;
881 stop_bits
= RFCOMM_RPN_STOP_1
;
884 /* Handle number of data bits [5-8] */
885 if ((old
->c_cflag
& CSIZE
) != (new->c_cflag
& CSIZE
))
886 changes
|= RFCOMM_RPN_PM_DATA
;
888 switch (new->c_cflag
& CSIZE
) {
890 data_bits
= RFCOMM_RPN_DATA_5
;
893 data_bits
= RFCOMM_RPN_DATA_6
;
896 data_bits
= RFCOMM_RPN_DATA_7
;
899 data_bits
= RFCOMM_RPN_DATA_8
;
902 data_bits
= RFCOMM_RPN_DATA_8
;
906 /* Handle baudrate settings */
907 if (old_baud_rate
!= new_baud_rate
)
908 changes
|= RFCOMM_RPN_PM_BITRATE
;
910 switch (new_baud_rate
) {
912 baud
= RFCOMM_RPN_BR_2400
;
915 baud
= RFCOMM_RPN_BR_4800
;
918 baud
= RFCOMM_RPN_BR_7200
;
921 baud
= RFCOMM_RPN_BR_9600
;
924 baud
= RFCOMM_RPN_BR_19200
;
927 baud
= RFCOMM_RPN_BR_38400
;
930 baud
= RFCOMM_RPN_BR_57600
;
933 baud
= RFCOMM_RPN_BR_115200
;
936 baud
= RFCOMM_RPN_BR_230400
;
939 /* 9600 is standard accordinag to the RFCOMM specification */
940 baud
= RFCOMM_RPN_BR_9600
;
946 rfcomm_send_rpn(dev
->dlc
->session
, 1, dev
->dlc
->dlci
, baud
,
947 data_bits
, stop_bits
, parity
,
948 RFCOMM_RPN_FLOW_NONE
, x_on
, x_off
, changes
);
953 static void rfcomm_tty_throttle(struct tty_struct
*tty
)
955 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
957 BT_DBG("tty %p dev %p", tty
, dev
);
959 rfcomm_dlc_throttle(dev
->dlc
);
962 static void rfcomm_tty_unthrottle(struct tty_struct
*tty
)
964 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
966 BT_DBG("tty %p dev %p", tty
, dev
);
968 rfcomm_dlc_unthrottle(dev
->dlc
);
971 static int rfcomm_tty_chars_in_buffer(struct tty_struct
*tty
)
973 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
975 BT_DBG("tty %p dev %p", tty
, dev
);
977 if (!dev
|| !dev
->dlc
)
980 if (!skb_queue_empty(&dev
->dlc
->tx_queue
))
981 return dev
->dlc
->mtu
;
986 static void rfcomm_tty_flush_buffer(struct tty_struct
*tty
)
988 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
990 BT_DBG("tty %p dev %p", tty
, dev
);
992 if (!dev
|| !dev
->dlc
)
995 skb_queue_purge(&dev
->dlc
->tx_queue
);
997 if (test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
) && tty
->ldisc
.write_wakeup
)
998 tty
->ldisc
.write_wakeup(tty
);
1001 static void rfcomm_tty_send_xchar(struct tty_struct
*tty
, char ch
)
1003 BT_DBG("tty %p ch %c", tty
, ch
);
1006 static void rfcomm_tty_wait_until_sent(struct tty_struct
*tty
, int timeout
)
1008 BT_DBG("tty %p timeout %d", tty
, timeout
);
1011 static void rfcomm_tty_hangup(struct tty_struct
*tty
)
1013 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
1015 BT_DBG("tty %p dev %p", tty
, dev
);
1020 rfcomm_tty_flush_buffer(tty
);
1022 if (test_bit(RFCOMM_RELEASE_ONHUP
, &dev
->flags
)) {
1023 if (rfcomm_dev_get(dev
->id
) == NULL
)
1025 rfcomm_dev_del(dev
);
1026 rfcomm_dev_put(dev
);
1030 static int rfcomm_tty_read_proc(char *buf
, char **start
, off_t offset
, int len
, int *eof
, void *unused
)
1035 static int rfcomm_tty_tiocmget(struct tty_struct
*tty
, struct file
*filp
)
1037 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
1039 BT_DBG("tty %p dev %p", tty
, dev
);
1041 return dev
->modem_status
;
1044 static int rfcomm_tty_tiocmset(struct tty_struct
*tty
, struct file
*filp
, unsigned int set
, unsigned int clear
)
1046 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
1047 struct rfcomm_dlc
*dlc
= dev
->dlc
;
1050 BT_DBG("tty %p dev %p set 0x%02x clear 0x%02x", tty
, dev
, set
, clear
);
1052 rfcomm_dlc_get_modem_status(dlc
, &v24_sig
);
1054 if (set
& TIOCM_DSR
|| set
& TIOCM_DTR
)
1055 v24_sig
|= RFCOMM_V24_RTC
;
1056 if (set
& TIOCM_RTS
|| set
& TIOCM_CTS
)
1057 v24_sig
|= RFCOMM_V24_RTR
;
1059 v24_sig
|= RFCOMM_V24_IC
;
1061 v24_sig
|= RFCOMM_V24_DV
;
1063 if (clear
& TIOCM_DSR
|| clear
& TIOCM_DTR
)
1064 v24_sig
&= ~RFCOMM_V24_RTC
;
1065 if (clear
& TIOCM_RTS
|| clear
& TIOCM_CTS
)
1066 v24_sig
&= ~RFCOMM_V24_RTR
;
1067 if (clear
& TIOCM_RI
)
1068 v24_sig
&= ~RFCOMM_V24_IC
;
1069 if (clear
& TIOCM_CD
)
1070 v24_sig
&= ~RFCOMM_V24_DV
;
1072 rfcomm_dlc_set_modem_status(dlc
, v24_sig
);
1077 /* ---- TTY structure ---- */
1079 static const struct tty_operations rfcomm_ops
= {
1080 .open
= rfcomm_tty_open
,
1081 .close
= rfcomm_tty_close
,
1082 .write
= rfcomm_tty_write
,
1083 .write_room
= rfcomm_tty_write_room
,
1084 .chars_in_buffer
= rfcomm_tty_chars_in_buffer
,
1085 .flush_buffer
= rfcomm_tty_flush_buffer
,
1086 .ioctl
= rfcomm_tty_ioctl
,
1087 .throttle
= rfcomm_tty_throttle
,
1088 .unthrottle
= rfcomm_tty_unthrottle
,
1089 .set_termios
= rfcomm_tty_set_termios
,
1090 .send_xchar
= rfcomm_tty_send_xchar
,
1091 .hangup
= rfcomm_tty_hangup
,
1092 .wait_until_sent
= rfcomm_tty_wait_until_sent
,
1093 .read_proc
= rfcomm_tty_read_proc
,
1094 .tiocmget
= rfcomm_tty_tiocmget
,
1095 .tiocmset
= rfcomm_tty_tiocmset
,
1098 int rfcomm_init_ttys(void)
1100 rfcomm_tty_driver
= alloc_tty_driver(RFCOMM_TTY_PORTS
);
1101 if (!rfcomm_tty_driver
)
1104 rfcomm_tty_driver
->owner
= THIS_MODULE
;
1105 rfcomm_tty_driver
->driver_name
= "rfcomm";
1106 rfcomm_tty_driver
->name
= "rfcomm";
1107 rfcomm_tty_driver
->major
= RFCOMM_TTY_MAJOR
;
1108 rfcomm_tty_driver
->minor_start
= RFCOMM_TTY_MINOR
;
1109 rfcomm_tty_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1110 rfcomm_tty_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1111 rfcomm_tty_driver
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
1112 rfcomm_tty_driver
->init_termios
= tty_std_termios
;
1113 rfcomm_tty_driver
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1114 tty_set_operations(rfcomm_tty_driver
, &rfcomm_ops
);
1116 if (tty_register_driver(rfcomm_tty_driver
)) {
1117 BT_ERR("Can't register RFCOMM TTY driver");
1118 put_tty_driver(rfcomm_tty_driver
);
1122 BT_INFO("RFCOMM TTY layer initialized");
1127 void rfcomm_cleanup_ttys(void)
1129 tty_unregister_driver(rfcomm_tty_driver
);
1130 put_tty_driver(rfcomm_tty_driver
);