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 /* Refcount should only hit zero when called from rfcomm_dev_del()
99 which will have taken us off the list. Everything else are
101 BUG_ON(!list_empty(&dev
->list
));
103 rfcomm_dlc_lock(dlc
);
104 /* Detach DLC if it's owned by this dev */
105 if (dlc
->owner
== dev
)
107 rfcomm_dlc_unlock(dlc
);
111 tty_unregister_device(rfcomm_tty_driver
, dev
->id
);
115 /* It's safe to call module_put() here because socket still
116 holds reference to this module. */
117 module_put(THIS_MODULE
);
120 static inline void rfcomm_dev_hold(struct rfcomm_dev
*dev
)
122 atomic_inc(&dev
->refcnt
);
125 static inline void rfcomm_dev_put(struct rfcomm_dev
*dev
)
127 /* The reason this isn't actually a race, as you no
128 doubt have a little voice screaming at you in your
129 head, is that the refcount should never actually
130 reach zero unless the device has already been taken
131 off the list, in rfcomm_dev_del(). And if that's not
132 true, we'll hit the BUG() in rfcomm_dev_destruct()
134 if (atomic_dec_and_test(&dev
->refcnt
))
135 rfcomm_dev_destruct(dev
);
138 static struct rfcomm_dev
*__rfcomm_dev_get(int id
)
140 struct rfcomm_dev
*dev
;
143 list_for_each(p
, &rfcomm_dev_list
) {
144 dev
= list_entry(p
, struct rfcomm_dev
, list
);
152 static inline struct rfcomm_dev
*rfcomm_dev_get(int id
)
154 struct rfcomm_dev
*dev
;
156 read_lock(&rfcomm_dev_lock
);
158 dev
= __rfcomm_dev_get(id
);
161 if (test_bit(RFCOMM_TTY_RELEASED
, &dev
->flags
))
164 rfcomm_dev_hold(dev
);
167 read_unlock(&rfcomm_dev_lock
);
172 static struct device
*rfcomm_get_device(struct rfcomm_dev
*dev
)
174 struct hci_dev
*hdev
;
175 struct hci_conn
*conn
;
177 hdev
= hci_get_route(&dev
->dst
, &dev
->src
);
181 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &dev
->dst
);
185 return conn
? &conn
->dev
: NULL
;
188 static ssize_t
show_address(struct device
*tty_dev
, struct device_attribute
*attr
, char *buf
)
190 struct rfcomm_dev
*dev
= dev_get_drvdata(tty_dev
);
192 baswap(&bdaddr
, &dev
->dst
);
193 return sprintf(buf
, "%s\n", batostr(&bdaddr
));
196 static ssize_t
show_channel(struct device
*tty_dev
, struct device_attribute
*attr
, char *buf
)
198 struct rfcomm_dev
*dev
= dev_get_drvdata(tty_dev
);
199 return sprintf(buf
, "%d\n", dev
->channel
);
202 static DEVICE_ATTR(address
, S_IRUGO
, show_address
, NULL
);
203 static DEVICE_ATTR(channel
, S_IRUGO
, show_channel
, NULL
);
205 static int rfcomm_dev_add(struct rfcomm_dev_req
*req
, struct rfcomm_dlc
*dlc
)
207 struct rfcomm_dev
*dev
;
208 struct list_head
*head
= &rfcomm_dev_list
, *p
;
211 BT_DBG("id %d channel %d", req
->dev_id
, req
->channel
);
213 dev
= kzalloc(sizeof(struct rfcomm_dev
), GFP_KERNEL
);
217 write_lock_bh(&rfcomm_dev_lock
);
219 if (req
->dev_id
< 0) {
222 list_for_each(p
, &rfcomm_dev_list
) {
223 if (list_entry(p
, struct rfcomm_dev
, list
)->id
!= dev
->id
)
230 dev
->id
= req
->dev_id
;
232 list_for_each(p
, &rfcomm_dev_list
) {
233 struct rfcomm_dev
*entry
= list_entry(p
, struct rfcomm_dev
, list
);
235 if (entry
->id
== dev
->id
) {
240 if (entry
->id
> dev
->id
- 1)
247 if ((dev
->id
< 0) || (dev
->id
> RFCOMM_MAX_DEV
- 1)) {
252 sprintf(dev
->name
, "rfcomm%d", dev
->id
);
254 list_add(&dev
->list
, head
);
255 atomic_set(&dev
->refcnt
, 1);
257 bacpy(&dev
->src
, &req
->src
);
258 bacpy(&dev
->dst
, &req
->dst
);
259 dev
->channel
= req
->channel
;
261 dev
->flags
= req
->flags
&
262 ((1 << RFCOMM_RELEASE_ONHUP
) | (1 << RFCOMM_REUSE_DLC
));
264 init_waitqueue_head(&dev
->wait
);
265 tasklet_init(&dev
->wakeup_task
, rfcomm_tty_wakeup
, (unsigned long) dev
);
267 rfcomm_dlc_lock(dlc
);
268 dlc
->data_ready
= rfcomm_dev_data_ready
;
269 dlc
->state_change
= rfcomm_dev_state_change
;
270 dlc
->modem_status
= rfcomm_dev_modem_status
;
274 rfcomm_dlc_unlock(dlc
);
276 /* It's safe to call __module_get() here because socket already
277 holds reference to this module. */
278 __module_get(THIS_MODULE
);
281 write_unlock_bh(&rfcomm_dev_lock
);
288 dev
->tty_dev
= tty_register_device(rfcomm_tty_driver
, dev
->id
, NULL
);
290 if (IS_ERR(dev
->tty_dev
)) {
291 err
= PTR_ERR(dev
->tty_dev
);
292 list_del(&dev
->list
);
297 dev_set_drvdata(dev
->tty_dev
, dev
);
299 if (device_create_file(dev
->tty_dev
, &dev_attr_address
) < 0)
300 BT_ERR("Failed to create address attribute");
302 if (device_create_file(dev
->tty_dev
, &dev_attr_channel
) < 0)
303 BT_ERR("Failed to create channel attribute");
308 static void rfcomm_dev_del(struct rfcomm_dev
*dev
)
310 BT_DBG("dev %p", dev
);
312 if (test_bit(RFCOMM_TTY_RELEASED
, &dev
->flags
))
315 set_bit(RFCOMM_TTY_RELEASED
, &dev
->flags
);
317 write_lock_bh(&rfcomm_dev_lock
);
318 list_del_init(&dev
->list
);
319 write_unlock_bh(&rfcomm_dev_lock
);
324 /* ---- Send buffer ---- */
325 static inline unsigned int rfcomm_room(struct rfcomm_dlc
*dlc
)
327 /* We can't let it be zero, because we don't get a callback
328 when tx_credits becomes nonzero, hence we'd never wake up */
329 return dlc
->mtu
* (dlc
->tx_credits
?:1);
332 static void rfcomm_wfree(struct sk_buff
*skb
)
334 struct rfcomm_dev
*dev
= (void *) skb
->sk
;
335 atomic_sub(skb
->truesize
, &dev
->wmem_alloc
);
336 if (test_bit(RFCOMM_TTY_ATTACHED
, &dev
->flags
))
337 tasklet_schedule(&dev
->wakeup_task
);
341 static inline void rfcomm_set_owner_w(struct sk_buff
*skb
, struct rfcomm_dev
*dev
)
343 rfcomm_dev_hold(dev
);
344 atomic_add(skb
->truesize
, &dev
->wmem_alloc
);
345 skb
->sk
= (void *) dev
;
346 skb
->destructor
= rfcomm_wfree
;
349 static struct sk_buff
*rfcomm_wmalloc(struct rfcomm_dev
*dev
, unsigned long size
, gfp_t priority
)
351 if (atomic_read(&dev
->wmem_alloc
) < rfcomm_room(dev
->dlc
)) {
352 struct sk_buff
*skb
= alloc_skb(size
, priority
);
354 rfcomm_set_owner_w(skb
, dev
);
361 /* ---- Device IOCTLs ---- */
363 #define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP))
365 static int rfcomm_create_dev(struct sock
*sk
, void __user
*arg
)
367 struct rfcomm_dev_req req
;
368 struct rfcomm_dlc
*dlc
;
371 if (copy_from_user(&req
, arg
, sizeof(req
)))
374 BT_DBG("sk %p dev_id %d flags 0x%x", sk
, req
.dev_id
, req
.flags
);
376 if (req
.flags
!= NOCAP_FLAGS
&& !capable(CAP_NET_ADMIN
))
379 if (req
.flags
& (1 << RFCOMM_REUSE_DLC
)) {
380 /* Socket must be connected */
381 if (sk
->sk_state
!= BT_CONNECTED
)
384 dlc
= rfcomm_pi(sk
)->dlc
;
385 rfcomm_dlc_hold(dlc
);
387 dlc
= rfcomm_dlc_alloc(GFP_KERNEL
);
392 id
= rfcomm_dev_add(&req
, dlc
);
398 if (req
.flags
& (1 << RFCOMM_REUSE_DLC
)) {
399 /* DLC is now used by device.
400 * Socket must be disconnected */
401 sk
->sk_state
= BT_CLOSED
;
407 static int rfcomm_release_dev(void __user
*arg
)
409 struct rfcomm_dev_req req
;
410 struct rfcomm_dev
*dev
;
412 if (copy_from_user(&req
, arg
, sizeof(req
)))
415 BT_DBG("dev_id %d flags 0x%x", req
.dev_id
, req
.flags
);
417 if (!(dev
= rfcomm_dev_get(req
.dev_id
)))
420 if (dev
->flags
!= NOCAP_FLAGS
&& !capable(CAP_NET_ADMIN
)) {
425 if (req
.flags
& (1 << RFCOMM_HANGUP_NOW
))
426 rfcomm_dlc_close(dev
->dlc
, 0);
428 /* Shut down TTY synchronously before freeing rfcomm_dev */
430 tty_vhangup(dev
->tty
);
432 if (!test_bit(RFCOMM_RELEASE_ONHUP
, &dev
->flags
))
438 static int rfcomm_get_dev_list(void __user
*arg
)
440 struct rfcomm_dev_list_req
*dl
;
441 struct rfcomm_dev_info
*di
;
443 int n
= 0, size
, err
;
448 if (get_user(dev_num
, (u16 __user
*) arg
))
451 if (!dev_num
|| dev_num
> (PAGE_SIZE
* 4) / sizeof(*di
))
454 size
= sizeof(*dl
) + dev_num
* sizeof(*di
);
456 if (!(dl
= kmalloc(size
, GFP_KERNEL
)))
461 read_lock_bh(&rfcomm_dev_lock
);
463 list_for_each(p
, &rfcomm_dev_list
) {
464 struct rfcomm_dev
*dev
= list_entry(p
, struct rfcomm_dev
, list
);
465 if (test_bit(RFCOMM_TTY_RELEASED
, &dev
->flags
))
467 (di
+ n
)->id
= dev
->id
;
468 (di
+ n
)->flags
= dev
->flags
;
469 (di
+ n
)->state
= dev
->dlc
->state
;
470 (di
+ n
)->channel
= dev
->channel
;
471 bacpy(&(di
+ n
)->src
, &dev
->src
);
472 bacpy(&(di
+ n
)->dst
, &dev
->dst
);
477 read_unlock_bh(&rfcomm_dev_lock
);
480 size
= sizeof(*dl
) + n
* sizeof(*di
);
482 err
= copy_to_user(arg
, dl
, size
);
485 return err
? -EFAULT
: 0;
488 static int rfcomm_get_dev_info(void __user
*arg
)
490 struct rfcomm_dev
*dev
;
491 struct rfcomm_dev_info di
;
496 if (copy_from_user(&di
, arg
, sizeof(di
)))
499 if (!(dev
= rfcomm_dev_get(di
.id
)))
502 di
.flags
= dev
->flags
;
503 di
.channel
= dev
->channel
;
504 di
.state
= dev
->dlc
->state
;
505 bacpy(&di
.src
, &dev
->src
);
506 bacpy(&di
.dst
, &dev
->dst
);
508 if (copy_to_user(arg
, &di
, sizeof(di
)))
515 int rfcomm_dev_ioctl(struct sock
*sk
, unsigned int cmd
, void __user
*arg
)
517 BT_DBG("cmd %d arg %p", cmd
, arg
);
520 case RFCOMMCREATEDEV
:
521 return rfcomm_create_dev(sk
, arg
);
523 case RFCOMMRELEASEDEV
:
524 return rfcomm_release_dev(arg
);
526 case RFCOMMGETDEVLIST
:
527 return rfcomm_get_dev_list(arg
);
529 case RFCOMMGETDEVINFO
:
530 return rfcomm_get_dev_info(arg
);
536 /* ---- DLC callbacks ---- */
537 static void rfcomm_dev_data_ready(struct rfcomm_dlc
*dlc
, struct sk_buff
*skb
)
539 struct rfcomm_dev
*dev
= dlc
->owner
;
540 struct tty_struct
*tty
;
542 if (!dev
|| !(tty
= dev
->tty
)) {
547 BT_DBG("dlc %p tty %p len %d", dlc
, tty
, skb
->len
);
549 tty_insert_flip_string(tty
, skb
->data
, skb
->len
);
550 tty_flip_buffer_push(tty
);
555 static void rfcomm_dev_state_change(struct rfcomm_dlc
*dlc
, int err
)
557 struct rfcomm_dev
*dev
= dlc
->owner
;
561 BT_DBG("dlc %p dev %p err %d", dlc
, dev
, err
);
564 wake_up_interruptible(&dev
->wait
);
566 if (dlc
->state
== BT_CLOSED
) {
568 if (test_bit(RFCOMM_RELEASE_ONHUP
, &dev
->flags
)) {
569 if (rfcomm_dev_get(dev
->id
) == NULL
)
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 if (dev
->tty_dev
->parent
)
696 device_move(dev
->tty_dev
, NULL
);
698 /* Close DLC and dettach TTY */
699 rfcomm_dlc_close(dev
->dlc
, 0);
701 clear_bit(RFCOMM_TTY_ATTACHED
, &dev
->flags
);
702 tasklet_kill(&dev
->wakeup_task
);
704 rfcomm_dlc_lock(dev
->dlc
);
705 tty
->driver_data
= NULL
;
707 rfcomm_dlc_unlock(dev
->dlc
);
713 static int rfcomm_tty_write(struct tty_struct
*tty
, const unsigned char *buf
, int count
)
715 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
716 struct rfcomm_dlc
*dlc
= dev
->dlc
;
718 int err
= 0, sent
= 0, size
;
720 BT_DBG("tty %p count %d", tty
, count
);
723 size
= min_t(uint
, count
, dlc
->mtu
);
725 skb
= rfcomm_wmalloc(dev
, size
+ RFCOMM_SKB_RESERVE
, GFP_ATOMIC
);
730 skb_reserve(skb
, RFCOMM_SKB_HEAD_RESERVE
);
732 memcpy(skb_put(skb
, size
), buf
+ sent
, size
);
734 if ((err
= rfcomm_dlc_send(dlc
, skb
)) < 0) {
743 return sent
? sent
: err
;
746 static int rfcomm_tty_write_room(struct tty_struct
*tty
)
748 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
751 BT_DBG("tty %p", tty
);
753 if (!dev
|| !dev
->dlc
)
756 room
= rfcomm_room(dev
->dlc
) - atomic_read(&dev
->wmem_alloc
);
763 static int rfcomm_tty_ioctl(struct tty_struct
*tty
, struct file
*filp
, unsigned int cmd
, unsigned long arg
)
765 BT_DBG("tty %p cmd 0x%02x", tty
, cmd
);
769 BT_DBG("TCGETS is not supported");
773 BT_DBG("TCSETS is not supported");
777 BT_DBG("TIOCMIWAIT");
781 BT_DBG("TIOCGICOUNT");
785 BT_ERR("TIOCGSERIAL is not supported");
789 BT_ERR("TIOCSSERIAL is not supported");
793 BT_ERR("TIOCSERGSTRUCT is not supported");
797 BT_ERR("TIOCSERGETLSR is not supported");
801 BT_ERR("TIOCSERCONFIG is not supported");
805 return -ENOIOCTLCMD
; /* ioctls which we must ignore */
812 static void rfcomm_tty_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
814 struct ktermios
*new = tty
->termios
;
815 int old_baud_rate
= tty_termios_baud_rate(old
);
816 int new_baud_rate
= tty_termios_baud_rate(new);
818 u8 baud
, data_bits
, stop_bits
, parity
, x_on
, x_off
;
821 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
823 BT_DBG("tty %p termios %p", tty
, old
);
825 if (!dev
|| !dev
->dlc
|| !dev
->dlc
->session
)
828 /* Handle turning off CRTSCTS */
829 if ((old
->c_cflag
& CRTSCTS
) && !(new->c_cflag
& CRTSCTS
))
830 BT_DBG("Turning off CRTSCTS unsupported");
832 /* Parity on/off and when on, odd/even */
833 if (((old
->c_cflag
& PARENB
) != (new->c_cflag
& PARENB
)) ||
834 ((old
->c_cflag
& PARODD
) != (new->c_cflag
& PARODD
)) ) {
835 changes
|= RFCOMM_RPN_PM_PARITY
;
836 BT_DBG("Parity change detected.");
839 /* Mark and space parity are not supported! */
840 if (new->c_cflag
& PARENB
) {
841 if (new->c_cflag
& PARODD
) {
842 BT_DBG("Parity is ODD");
843 parity
= RFCOMM_RPN_PARITY_ODD
;
845 BT_DBG("Parity is EVEN");
846 parity
= RFCOMM_RPN_PARITY_EVEN
;
849 BT_DBG("Parity is OFF");
850 parity
= RFCOMM_RPN_PARITY_NONE
;
853 /* Setting the x_on / x_off characters */
854 if (old
->c_cc
[VSTOP
] != new->c_cc
[VSTOP
]) {
855 BT_DBG("XOFF custom");
856 x_on
= new->c_cc
[VSTOP
];
857 changes
|= RFCOMM_RPN_PM_XON
;
859 BT_DBG("XOFF default");
860 x_on
= RFCOMM_RPN_XON_CHAR
;
863 if (old
->c_cc
[VSTART
] != new->c_cc
[VSTART
]) {
864 BT_DBG("XON custom");
865 x_off
= new->c_cc
[VSTART
];
866 changes
|= RFCOMM_RPN_PM_XOFF
;
868 BT_DBG("XON default");
869 x_off
= RFCOMM_RPN_XOFF_CHAR
;
872 /* Handle setting of stop bits */
873 if ((old
->c_cflag
& CSTOPB
) != (new->c_cflag
& CSTOPB
))
874 changes
|= RFCOMM_RPN_PM_STOP
;
876 /* POSIX does not support 1.5 stop bits and RFCOMM does not
877 * support 2 stop bits. So a request for 2 stop bits gets
878 * translated to 1.5 stop bits */
879 if (new->c_cflag
& CSTOPB
) {
880 stop_bits
= RFCOMM_RPN_STOP_15
;
882 stop_bits
= RFCOMM_RPN_STOP_1
;
885 /* Handle number of data bits [5-8] */
886 if ((old
->c_cflag
& CSIZE
) != (new->c_cflag
& CSIZE
))
887 changes
|= RFCOMM_RPN_PM_DATA
;
889 switch (new->c_cflag
& CSIZE
) {
891 data_bits
= RFCOMM_RPN_DATA_5
;
894 data_bits
= RFCOMM_RPN_DATA_6
;
897 data_bits
= RFCOMM_RPN_DATA_7
;
900 data_bits
= RFCOMM_RPN_DATA_8
;
903 data_bits
= RFCOMM_RPN_DATA_8
;
907 /* Handle baudrate settings */
908 if (old_baud_rate
!= new_baud_rate
)
909 changes
|= RFCOMM_RPN_PM_BITRATE
;
911 switch (new_baud_rate
) {
913 baud
= RFCOMM_RPN_BR_2400
;
916 baud
= RFCOMM_RPN_BR_4800
;
919 baud
= RFCOMM_RPN_BR_7200
;
922 baud
= RFCOMM_RPN_BR_9600
;
925 baud
= RFCOMM_RPN_BR_19200
;
928 baud
= RFCOMM_RPN_BR_38400
;
931 baud
= RFCOMM_RPN_BR_57600
;
934 baud
= RFCOMM_RPN_BR_115200
;
937 baud
= RFCOMM_RPN_BR_230400
;
940 /* 9600 is standard accordinag to the RFCOMM specification */
941 baud
= RFCOMM_RPN_BR_9600
;
947 rfcomm_send_rpn(dev
->dlc
->session
, 1, dev
->dlc
->dlci
, baud
,
948 data_bits
, stop_bits
, parity
,
949 RFCOMM_RPN_FLOW_NONE
, x_on
, x_off
, changes
);
954 static void rfcomm_tty_throttle(struct tty_struct
*tty
)
956 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
958 BT_DBG("tty %p dev %p", tty
, dev
);
960 rfcomm_dlc_throttle(dev
->dlc
);
963 static void rfcomm_tty_unthrottle(struct tty_struct
*tty
)
965 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
967 BT_DBG("tty %p dev %p", tty
, dev
);
969 rfcomm_dlc_unthrottle(dev
->dlc
);
972 static int rfcomm_tty_chars_in_buffer(struct tty_struct
*tty
)
974 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
976 BT_DBG("tty %p dev %p", tty
, dev
);
978 if (!dev
|| !dev
->dlc
)
981 if (!skb_queue_empty(&dev
->dlc
->tx_queue
))
982 return dev
->dlc
->mtu
;
987 static void rfcomm_tty_flush_buffer(struct tty_struct
*tty
)
989 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
991 BT_DBG("tty %p dev %p", tty
, dev
);
993 if (!dev
|| !dev
->dlc
)
996 skb_queue_purge(&dev
->dlc
->tx_queue
);
998 if (test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
) && tty
->ldisc
.write_wakeup
)
999 tty
->ldisc
.write_wakeup(tty
);
1002 static void rfcomm_tty_send_xchar(struct tty_struct
*tty
, char ch
)
1004 BT_DBG("tty %p ch %c", tty
, ch
);
1007 static void rfcomm_tty_wait_until_sent(struct tty_struct
*tty
, int timeout
)
1009 BT_DBG("tty %p timeout %d", tty
, timeout
);
1012 static void rfcomm_tty_hangup(struct tty_struct
*tty
)
1014 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
1016 BT_DBG("tty %p dev %p", tty
, dev
);
1021 rfcomm_tty_flush_buffer(tty
);
1023 if (test_bit(RFCOMM_RELEASE_ONHUP
, &dev
->flags
)) {
1024 if (rfcomm_dev_get(dev
->id
) == NULL
)
1026 rfcomm_dev_del(dev
);
1027 rfcomm_dev_put(dev
);
1031 static int rfcomm_tty_read_proc(char *buf
, char **start
, off_t offset
, int len
, int *eof
, void *unused
)
1036 static int rfcomm_tty_tiocmget(struct tty_struct
*tty
, struct file
*filp
)
1038 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
1040 BT_DBG("tty %p dev %p", tty
, dev
);
1042 return dev
->modem_status
;
1045 static int rfcomm_tty_tiocmset(struct tty_struct
*tty
, struct file
*filp
, unsigned int set
, unsigned int clear
)
1047 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
1048 struct rfcomm_dlc
*dlc
= dev
->dlc
;
1051 BT_DBG("tty %p dev %p set 0x%02x clear 0x%02x", tty
, dev
, set
, clear
);
1053 rfcomm_dlc_get_modem_status(dlc
, &v24_sig
);
1055 if (set
& TIOCM_DSR
|| set
& TIOCM_DTR
)
1056 v24_sig
|= RFCOMM_V24_RTC
;
1057 if (set
& TIOCM_RTS
|| set
& TIOCM_CTS
)
1058 v24_sig
|= RFCOMM_V24_RTR
;
1060 v24_sig
|= RFCOMM_V24_IC
;
1062 v24_sig
|= RFCOMM_V24_DV
;
1064 if (clear
& TIOCM_DSR
|| clear
& TIOCM_DTR
)
1065 v24_sig
&= ~RFCOMM_V24_RTC
;
1066 if (clear
& TIOCM_RTS
|| clear
& TIOCM_CTS
)
1067 v24_sig
&= ~RFCOMM_V24_RTR
;
1068 if (clear
& TIOCM_RI
)
1069 v24_sig
&= ~RFCOMM_V24_IC
;
1070 if (clear
& TIOCM_CD
)
1071 v24_sig
&= ~RFCOMM_V24_DV
;
1073 rfcomm_dlc_set_modem_status(dlc
, v24_sig
);
1078 /* ---- TTY structure ---- */
1080 static const struct tty_operations rfcomm_ops
= {
1081 .open
= rfcomm_tty_open
,
1082 .close
= rfcomm_tty_close
,
1083 .write
= rfcomm_tty_write
,
1084 .write_room
= rfcomm_tty_write_room
,
1085 .chars_in_buffer
= rfcomm_tty_chars_in_buffer
,
1086 .flush_buffer
= rfcomm_tty_flush_buffer
,
1087 .ioctl
= rfcomm_tty_ioctl
,
1088 .throttle
= rfcomm_tty_throttle
,
1089 .unthrottle
= rfcomm_tty_unthrottle
,
1090 .set_termios
= rfcomm_tty_set_termios
,
1091 .send_xchar
= rfcomm_tty_send_xchar
,
1092 .hangup
= rfcomm_tty_hangup
,
1093 .wait_until_sent
= rfcomm_tty_wait_until_sent
,
1094 .read_proc
= rfcomm_tty_read_proc
,
1095 .tiocmget
= rfcomm_tty_tiocmget
,
1096 .tiocmset
= rfcomm_tty_tiocmset
,
1099 int rfcomm_init_ttys(void)
1101 rfcomm_tty_driver
= alloc_tty_driver(RFCOMM_TTY_PORTS
);
1102 if (!rfcomm_tty_driver
)
1105 rfcomm_tty_driver
->owner
= THIS_MODULE
;
1106 rfcomm_tty_driver
->driver_name
= "rfcomm";
1107 rfcomm_tty_driver
->name
= "rfcomm";
1108 rfcomm_tty_driver
->major
= RFCOMM_TTY_MAJOR
;
1109 rfcomm_tty_driver
->minor_start
= RFCOMM_TTY_MINOR
;
1110 rfcomm_tty_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1111 rfcomm_tty_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1112 rfcomm_tty_driver
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
1113 rfcomm_tty_driver
->init_termios
= tty_std_termios
;
1114 rfcomm_tty_driver
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1115 tty_set_operations(rfcomm_tty_driver
, &rfcomm_ops
);
1117 if (tty_register_driver(rfcomm_tty_driver
)) {
1118 BT_ERR("Can't register RFCOMM TTY driver");
1119 put_tty_driver(rfcomm_tty_driver
);
1123 BT_INFO("RFCOMM TTY layer initialized");
1128 void rfcomm_cleanup_ttys(void)
1130 tty_unregister_driver(rfcomm_tty_driver
);
1131 put_tty_driver(rfcomm_tty_driver
);