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
;
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 struct device
*rfcomm_get_device(struct rfcomm_dev
*dev
)
167 struct hci_dev
*hdev
;
168 struct hci_conn
*conn
;
170 hdev
= hci_get_route(&dev
->dst
, &dev
->src
);
174 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &dev
->dst
);
178 return conn
? &conn
->dev
: NULL
;
181 static int rfcomm_dev_add(struct rfcomm_dev_req
*req
, struct rfcomm_dlc
*dlc
)
183 struct rfcomm_dev
*dev
;
184 struct list_head
*head
= &rfcomm_dev_list
, *p
;
187 BT_DBG("id %d channel %d", req
->dev_id
, req
->channel
);
189 dev
= kzalloc(sizeof(struct rfcomm_dev
), GFP_KERNEL
);
193 write_lock_bh(&rfcomm_dev_lock
);
195 if (req
->dev_id
< 0) {
198 list_for_each(p
, &rfcomm_dev_list
) {
199 if (list_entry(p
, struct rfcomm_dev
, list
)->id
!= dev
->id
)
206 dev
->id
= req
->dev_id
;
208 list_for_each(p
, &rfcomm_dev_list
) {
209 struct rfcomm_dev
*entry
= list_entry(p
, struct rfcomm_dev
, list
);
211 if (entry
->id
== dev
->id
) {
216 if (entry
->id
> dev
->id
- 1)
223 if ((dev
->id
< 0) || (dev
->id
> RFCOMM_MAX_DEV
- 1)) {
228 sprintf(dev
->name
, "rfcomm%d", dev
->id
);
230 list_add(&dev
->list
, head
);
231 atomic_set(&dev
->refcnt
, 1);
233 bacpy(&dev
->src
, &req
->src
);
234 bacpy(&dev
->dst
, &req
->dst
);
235 dev
->channel
= req
->channel
;
237 dev
->flags
= req
->flags
&
238 ((1 << RFCOMM_RELEASE_ONHUP
) | (1 << RFCOMM_REUSE_DLC
));
240 init_waitqueue_head(&dev
->wait
);
241 tasklet_init(&dev
->wakeup_task
, rfcomm_tty_wakeup
, (unsigned long) dev
);
243 rfcomm_dlc_lock(dlc
);
244 dlc
->data_ready
= rfcomm_dev_data_ready
;
245 dlc
->state_change
= rfcomm_dev_state_change
;
246 dlc
->modem_status
= rfcomm_dev_modem_status
;
250 rfcomm_dlc_unlock(dlc
);
252 /* It's safe to call __module_get() here because socket already
253 holds reference to this module. */
254 __module_get(THIS_MODULE
);
257 write_unlock_bh(&rfcomm_dev_lock
);
264 tty_register_device(rfcomm_tty_driver
, dev
->id
, rfcomm_get_device(dev
));
269 static void rfcomm_dev_del(struct rfcomm_dev
*dev
)
271 BT_DBG("dev %p", dev
);
273 write_lock_bh(&rfcomm_dev_lock
);
274 list_del_init(&dev
->list
);
275 write_unlock_bh(&rfcomm_dev_lock
);
280 /* ---- Send buffer ---- */
281 static inline unsigned int rfcomm_room(struct rfcomm_dlc
*dlc
)
283 /* We can't let it be zero, because we don't get a callback
284 when tx_credits becomes nonzero, hence we'd never wake up */
285 return dlc
->mtu
* (dlc
->tx_credits
?:1);
288 static void rfcomm_wfree(struct sk_buff
*skb
)
290 struct rfcomm_dev
*dev
= (void *) skb
->sk
;
291 atomic_sub(skb
->truesize
, &dev
->wmem_alloc
);
292 if (test_bit(RFCOMM_TTY_ATTACHED
, &dev
->flags
))
293 tasklet_schedule(&dev
->wakeup_task
);
297 static inline void rfcomm_set_owner_w(struct sk_buff
*skb
, struct rfcomm_dev
*dev
)
299 rfcomm_dev_hold(dev
);
300 atomic_add(skb
->truesize
, &dev
->wmem_alloc
);
301 skb
->sk
= (void *) dev
;
302 skb
->destructor
= rfcomm_wfree
;
305 static struct sk_buff
*rfcomm_wmalloc(struct rfcomm_dev
*dev
, unsigned long size
, gfp_t priority
)
307 if (atomic_read(&dev
->wmem_alloc
) < rfcomm_room(dev
->dlc
)) {
308 struct sk_buff
*skb
= alloc_skb(size
, priority
);
310 rfcomm_set_owner_w(skb
, dev
);
317 /* ---- Device IOCTLs ---- */
319 #define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP))
321 static int rfcomm_create_dev(struct sock
*sk
, void __user
*arg
)
323 struct rfcomm_dev_req req
;
324 struct rfcomm_dlc
*dlc
;
327 if (copy_from_user(&req
, arg
, sizeof(req
)))
330 BT_DBG("sk %p dev_id %id flags 0x%x", sk
, req
.dev_id
, req
.flags
);
332 if (req
.flags
!= NOCAP_FLAGS
&& !capable(CAP_NET_ADMIN
))
335 if (req
.flags
& (1 << RFCOMM_REUSE_DLC
)) {
336 /* Socket must be connected */
337 if (sk
->sk_state
!= BT_CONNECTED
)
340 dlc
= rfcomm_pi(sk
)->dlc
;
341 rfcomm_dlc_hold(dlc
);
343 dlc
= rfcomm_dlc_alloc(GFP_KERNEL
);
348 id
= rfcomm_dev_add(&req
, dlc
);
354 if (req
.flags
& (1 << RFCOMM_REUSE_DLC
)) {
355 /* DLC is now used by device.
356 * Socket must be disconnected */
357 sk
->sk_state
= BT_CLOSED
;
363 static int rfcomm_release_dev(void __user
*arg
)
365 struct rfcomm_dev_req req
;
366 struct rfcomm_dev
*dev
;
368 if (copy_from_user(&req
, arg
, sizeof(req
)))
371 BT_DBG("dev_id %id flags 0x%x", req
.dev_id
, req
.flags
);
373 if (!(dev
= rfcomm_dev_get(req
.dev_id
)))
376 if (dev
->flags
!= NOCAP_FLAGS
&& !capable(CAP_NET_ADMIN
)) {
381 if (req
.flags
& (1 << RFCOMM_HANGUP_NOW
))
382 rfcomm_dlc_close(dev
->dlc
, 0);
389 static int rfcomm_get_dev_list(void __user
*arg
)
391 struct rfcomm_dev_list_req
*dl
;
392 struct rfcomm_dev_info
*di
;
394 int n
= 0, size
, err
;
399 if (get_user(dev_num
, (u16 __user
*) arg
))
402 if (!dev_num
|| dev_num
> (PAGE_SIZE
* 4) / sizeof(*di
))
405 size
= sizeof(*dl
) + dev_num
* sizeof(*di
);
407 if (!(dl
= kmalloc(size
, GFP_KERNEL
)))
412 read_lock_bh(&rfcomm_dev_lock
);
414 list_for_each(p
, &rfcomm_dev_list
) {
415 struct rfcomm_dev
*dev
= list_entry(p
, struct rfcomm_dev
, list
);
416 (di
+ n
)->id
= dev
->id
;
417 (di
+ n
)->flags
= dev
->flags
;
418 (di
+ n
)->state
= dev
->dlc
->state
;
419 (di
+ n
)->channel
= dev
->channel
;
420 bacpy(&(di
+ n
)->src
, &dev
->src
);
421 bacpy(&(di
+ n
)->dst
, &dev
->dst
);
426 read_unlock_bh(&rfcomm_dev_lock
);
429 size
= sizeof(*dl
) + n
* sizeof(*di
);
431 err
= copy_to_user(arg
, dl
, size
);
434 return err
? -EFAULT
: 0;
437 static int rfcomm_get_dev_info(void __user
*arg
)
439 struct rfcomm_dev
*dev
;
440 struct rfcomm_dev_info di
;
445 if (copy_from_user(&di
, arg
, sizeof(di
)))
448 if (!(dev
= rfcomm_dev_get(di
.id
)))
451 di
.flags
= dev
->flags
;
452 di
.channel
= dev
->channel
;
453 di
.state
= dev
->dlc
->state
;
454 bacpy(&di
.src
, &dev
->src
);
455 bacpy(&di
.dst
, &dev
->dst
);
457 if (copy_to_user(arg
, &di
, sizeof(di
)))
464 int rfcomm_dev_ioctl(struct sock
*sk
, unsigned int cmd
, void __user
*arg
)
466 BT_DBG("cmd %d arg %p", cmd
, arg
);
469 case RFCOMMCREATEDEV
:
470 return rfcomm_create_dev(sk
, arg
);
472 case RFCOMMRELEASEDEV
:
473 return rfcomm_release_dev(arg
);
475 case RFCOMMGETDEVLIST
:
476 return rfcomm_get_dev_list(arg
);
478 case RFCOMMGETDEVINFO
:
479 return rfcomm_get_dev_info(arg
);
485 /* ---- DLC callbacks ---- */
486 static void rfcomm_dev_data_ready(struct rfcomm_dlc
*dlc
, struct sk_buff
*skb
)
488 struct rfcomm_dev
*dev
= dlc
->owner
;
489 struct tty_struct
*tty
;
491 if (!dev
|| !(tty
= dev
->tty
)) {
496 BT_DBG("dlc %p tty %p len %d", dlc
, tty
, skb
->len
);
498 tty_insert_flip_string(tty
, skb
->data
, skb
->len
);
499 tty_flip_buffer_push(tty
);
504 static void rfcomm_dev_state_change(struct rfcomm_dlc
*dlc
, int err
)
506 struct rfcomm_dev
*dev
= dlc
->owner
;
510 BT_DBG("dlc %p dev %p err %d", dlc
, dev
, err
);
513 wake_up_interruptible(&dev
->wait
);
515 if (dlc
->state
== BT_CLOSED
) {
517 if (test_bit(RFCOMM_RELEASE_ONHUP
, &dev
->flags
)) {
518 rfcomm_dev_hold(dev
);
521 /* We have to drop DLC lock here, otherwise
522 rfcomm_dev_put() will dead lock if it's
523 the last reference. */
524 rfcomm_dlc_unlock(dlc
);
526 rfcomm_dlc_lock(dlc
);
529 tty_hangup(dev
->tty
);
533 static void rfcomm_dev_modem_status(struct rfcomm_dlc
*dlc
, u8 v24_sig
)
535 struct rfcomm_dev
*dev
= dlc
->owner
;
539 BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc
, dev
, v24_sig
);
541 if ((dev
->modem_status
& TIOCM_CD
) && !(v24_sig
& RFCOMM_V24_DV
)) {
542 if (dev
->tty
&& !C_CLOCAL(dev
->tty
))
543 tty_hangup(dev
->tty
);
547 ((v24_sig
& RFCOMM_V24_RTC
) ? (TIOCM_DSR
| TIOCM_DTR
) : 0) |
548 ((v24_sig
& RFCOMM_V24_RTR
) ? (TIOCM_RTS
| TIOCM_CTS
) : 0) |
549 ((v24_sig
& RFCOMM_V24_IC
) ? TIOCM_RI
: 0) |
550 ((v24_sig
& RFCOMM_V24_DV
) ? TIOCM_CD
: 0);
553 /* ---- TTY functions ---- */
554 static void rfcomm_tty_wakeup(unsigned long arg
)
556 struct rfcomm_dev
*dev
= (void *) arg
;
557 struct tty_struct
*tty
= dev
->tty
;
561 BT_DBG("dev %p tty %p", dev
, tty
);
563 if (test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
) && tty
->ldisc
.write_wakeup
)
564 (tty
->ldisc
.write_wakeup
)(tty
);
566 wake_up_interruptible(&tty
->write_wait
);
567 #ifdef SERIAL_HAVE_POLL_WAIT
568 wake_up_interruptible(&tty
->poll_wait
);
572 static int rfcomm_tty_open(struct tty_struct
*tty
, struct file
*filp
)
574 DECLARE_WAITQUEUE(wait
, current
);
575 struct rfcomm_dev
*dev
;
576 struct rfcomm_dlc
*dlc
;
581 BT_DBG("tty %p id %d", tty
, id
);
583 /* We don't leak this refcount. For reasons which are not entirely
584 clear, the TTY layer will call our ->close() method even if the
585 open fails. We decrease the refcount there, and decreasing it
586 here too would cause breakage. */
587 dev
= rfcomm_dev_get(id
);
591 BT_DBG("dev %p dst %s channel %d opened %d", dev
, batostr(&dev
->dst
), dev
->channel
, dev
->opened
);
593 if (dev
->opened
++ != 0)
598 /* Attach TTY and open DLC */
600 rfcomm_dlc_lock(dlc
);
601 tty
->driver_data
= dev
;
603 rfcomm_dlc_unlock(dlc
);
604 set_bit(RFCOMM_TTY_ATTACHED
, &dev
->flags
);
606 err
= rfcomm_dlc_open(dlc
, &dev
->src
, &dev
->dst
, dev
->channel
);
610 /* Wait for DLC to connect */
611 add_wait_queue(&dev
->wait
, &wait
);
613 set_current_state(TASK_INTERRUPTIBLE
);
615 if (dlc
->state
== BT_CLOSED
) {
620 if (dlc
->state
== BT_CONNECTED
)
623 if (signal_pending(current
)) {
630 set_current_state(TASK_RUNNING
);
631 remove_wait_queue(&dev
->wait
, &wait
);
636 static void rfcomm_tty_close(struct tty_struct
*tty
, struct file
*filp
)
638 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
642 BT_DBG("tty %p dev %p dlc %p opened %d", tty
, dev
, dev
->dlc
, dev
->opened
);
644 if (--dev
->opened
== 0) {
645 /* Close DLC and dettach TTY */
646 rfcomm_dlc_close(dev
->dlc
, 0);
648 clear_bit(RFCOMM_TTY_ATTACHED
, &dev
->flags
);
649 tasklet_kill(&dev
->wakeup_task
);
651 rfcomm_dlc_lock(dev
->dlc
);
652 tty
->driver_data
= NULL
;
654 rfcomm_dlc_unlock(dev
->dlc
);
660 static int rfcomm_tty_write(struct tty_struct
*tty
, const unsigned char *buf
, int count
)
662 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
663 struct rfcomm_dlc
*dlc
= dev
->dlc
;
665 int err
= 0, sent
= 0, size
;
667 BT_DBG("tty %p count %d", tty
, count
);
670 size
= min_t(uint
, count
, dlc
->mtu
);
672 skb
= rfcomm_wmalloc(dev
, size
+ RFCOMM_SKB_RESERVE
, GFP_ATOMIC
);
677 skb_reserve(skb
, RFCOMM_SKB_HEAD_RESERVE
);
679 memcpy(skb_put(skb
, size
), buf
+ sent
, size
);
681 if ((err
= rfcomm_dlc_send(dlc
, skb
)) < 0) {
690 return sent
? sent
: err
;
693 static int rfcomm_tty_write_room(struct tty_struct
*tty
)
695 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
698 BT_DBG("tty %p", tty
);
700 room
= rfcomm_room(dev
->dlc
) - atomic_read(&dev
->wmem_alloc
);
706 static int rfcomm_tty_ioctl(struct tty_struct
*tty
, struct file
*filp
, unsigned int cmd
, unsigned long arg
)
708 BT_DBG("tty %p cmd 0x%02x", tty
, cmd
);
712 BT_DBG("TCGETS is not supported");
716 BT_DBG("TCSETS is not supported");
720 BT_DBG("TIOCMIWAIT");
724 BT_DBG("TIOCGICOUNT");
728 BT_ERR("TIOCGSERIAL is not supported");
732 BT_ERR("TIOCSSERIAL is not supported");
736 BT_ERR("TIOCSERGSTRUCT is not supported");
740 BT_ERR("TIOCSERGETLSR is not supported");
744 BT_ERR("TIOCSERCONFIG is not supported");
748 return -ENOIOCTLCMD
; /* ioctls which we must ignore */
755 static void rfcomm_tty_set_termios(struct tty_struct
*tty
, struct termios
*old
)
757 struct termios
*new = (struct termios
*) tty
->termios
;
758 int old_baud_rate
= tty_termios_baud_rate(old
);
759 int new_baud_rate
= tty_termios_baud_rate(new);
761 u8 baud
, data_bits
, stop_bits
, parity
, x_on
, x_off
;
764 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
766 BT_DBG("tty %p termios %p", tty
, old
);
771 /* Handle turning off CRTSCTS */
772 if ((old
->c_cflag
& CRTSCTS
) && !(new->c_cflag
& CRTSCTS
))
773 BT_DBG("Turning off CRTSCTS unsupported");
775 /* Parity on/off and when on, odd/even */
776 if (((old
->c_cflag
& PARENB
) != (new->c_cflag
& PARENB
)) ||
777 ((old
->c_cflag
& PARODD
) != (new->c_cflag
& PARODD
)) ) {
778 changes
|= RFCOMM_RPN_PM_PARITY
;
779 BT_DBG("Parity change detected.");
782 /* Mark and space parity are not supported! */
783 if (new->c_cflag
& PARENB
) {
784 if (new->c_cflag
& PARODD
) {
785 BT_DBG("Parity is ODD");
786 parity
= RFCOMM_RPN_PARITY_ODD
;
788 BT_DBG("Parity is EVEN");
789 parity
= RFCOMM_RPN_PARITY_EVEN
;
792 BT_DBG("Parity is OFF");
793 parity
= RFCOMM_RPN_PARITY_NONE
;
796 /* Setting the x_on / x_off characters */
797 if (old
->c_cc
[VSTOP
] != new->c_cc
[VSTOP
]) {
798 BT_DBG("XOFF custom");
799 x_on
= new->c_cc
[VSTOP
];
800 changes
|= RFCOMM_RPN_PM_XON
;
802 BT_DBG("XOFF default");
803 x_on
= RFCOMM_RPN_XON_CHAR
;
806 if (old
->c_cc
[VSTART
] != new->c_cc
[VSTART
]) {
807 BT_DBG("XON custom");
808 x_off
= new->c_cc
[VSTART
];
809 changes
|= RFCOMM_RPN_PM_XOFF
;
811 BT_DBG("XON default");
812 x_off
= RFCOMM_RPN_XOFF_CHAR
;
815 /* Handle setting of stop bits */
816 if ((old
->c_cflag
& CSTOPB
) != (new->c_cflag
& CSTOPB
))
817 changes
|= RFCOMM_RPN_PM_STOP
;
819 /* POSIX does not support 1.5 stop bits and RFCOMM does not
820 * support 2 stop bits. So a request for 2 stop bits gets
821 * translated to 1.5 stop bits */
822 if (new->c_cflag
& CSTOPB
) {
823 stop_bits
= RFCOMM_RPN_STOP_15
;
825 stop_bits
= RFCOMM_RPN_STOP_1
;
828 /* Handle number of data bits [5-8] */
829 if ((old
->c_cflag
& CSIZE
) != (new->c_cflag
& CSIZE
))
830 changes
|= RFCOMM_RPN_PM_DATA
;
832 switch (new->c_cflag
& CSIZE
) {
834 data_bits
= RFCOMM_RPN_DATA_5
;
837 data_bits
= RFCOMM_RPN_DATA_6
;
840 data_bits
= RFCOMM_RPN_DATA_7
;
843 data_bits
= RFCOMM_RPN_DATA_8
;
846 data_bits
= RFCOMM_RPN_DATA_8
;
850 /* Handle baudrate settings */
851 if (old_baud_rate
!= new_baud_rate
)
852 changes
|= RFCOMM_RPN_PM_BITRATE
;
854 switch (new_baud_rate
) {
856 baud
= RFCOMM_RPN_BR_2400
;
859 baud
= RFCOMM_RPN_BR_4800
;
862 baud
= RFCOMM_RPN_BR_7200
;
865 baud
= RFCOMM_RPN_BR_9600
;
868 baud
= RFCOMM_RPN_BR_19200
;
871 baud
= RFCOMM_RPN_BR_38400
;
874 baud
= RFCOMM_RPN_BR_57600
;
877 baud
= RFCOMM_RPN_BR_115200
;
880 baud
= RFCOMM_RPN_BR_230400
;
883 /* 9600 is standard accordinag to the RFCOMM specification */
884 baud
= RFCOMM_RPN_BR_9600
;
890 rfcomm_send_rpn(dev
->dlc
->session
, 1, dev
->dlc
->dlci
, baud
,
891 data_bits
, stop_bits
, parity
,
892 RFCOMM_RPN_FLOW_NONE
, x_on
, x_off
, changes
);
897 static void rfcomm_tty_throttle(struct tty_struct
*tty
)
899 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
901 BT_DBG("tty %p dev %p", tty
, dev
);
903 rfcomm_dlc_throttle(dev
->dlc
);
906 static void rfcomm_tty_unthrottle(struct tty_struct
*tty
)
908 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
910 BT_DBG("tty %p dev %p", tty
, dev
);
912 rfcomm_dlc_unthrottle(dev
->dlc
);
915 static int rfcomm_tty_chars_in_buffer(struct tty_struct
*tty
)
917 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
918 struct rfcomm_dlc
*dlc
= dev
->dlc
;
920 BT_DBG("tty %p dev %p", tty
, dev
);
922 if (!skb_queue_empty(&dlc
->tx_queue
))
928 static void rfcomm_tty_flush_buffer(struct tty_struct
*tty
)
930 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
934 BT_DBG("tty %p dev %p", tty
, dev
);
936 skb_queue_purge(&dev
->dlc
->tx_queue
);
938 if (test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
) && tty
->ldisc
.write_wakeup
)
939 tty
->ldisc
.write_wakeup(tty
);
942 static void rfcomm_tty_send_xchar(struct tty_struct
*tty
, char ch
)
944 BT_DBG("tty %p ch %c", tty
, ch
);
947 static void rfcomm_tty_wait_until_sent(struct tty_struct
*tty
, int timeout
)
949 BT_DBG("tty %p timeout %d", tty
, timeout
);
952 static void rfcomm_tty_hangup(struct tty_struct
*tty
)
954 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
958 BT_DBG("tty %p dev %p", tty
, dev
);
960 rfcomm_tty_flush_buffer(tty
);
962 if (test_bit(RFCOMM_RELEASE_ONHUP
, &dev
->flags
))
966 static int rfcomm_tty_read_proc(char *buf
, char **start
, off_t offset
, int len
, int *eof
, void *unused
)
971 static int rfcomm_tty_tiocmget(struct tty_struct
*tty
, struct file
*filp
)
973 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
975 BT_DBG("tty %p dev %p", tty
, dev
);
977 return dev
->modem_status
;
980 static int rfcomm_tty_tiocmset(struct tty_struct
*tty
, struct file
*filp
, unsigned int set
, unsigned int clear
)
982 struct rfcomm_dev
*dev
= (struct rfcomm_dev
*) tty
->driver_data
;
983 struct rfcomm_dlc
*dlc
= dev
->dlc
;
986 BT_DBG("tty %p dev %p set 0x%02x clear 0x%02x", tty
, dev
, set
, clear
);
988 rfcomm_dlc_get_modem_status(dlc
, &v24_sig
);
990 if (set
& TIOCM_DSR
|| set
& TIOCM_DTR
)
991 v24_sig
|= RFCOMM_V24_RTC
;
992 if (set
& TIOCM_RTS
|| set
& TIOCM_CTS
)
993 v24_sig
|= RFCOMM_V24_RTR
;
995 v24_sig
|= RFCOMM_V24_IC
;
997 v24_sig
|= RFCOMM_V24_DV
;
999 if (clear
& TIOCM_DSR
|| clear
& TIOCM_DTR
)
1000 v24_sig
&= ~RFCOMM_V24_RTC
;
1001 if (clear
& TIOCM_RTS
|| clear
& TIOCM_CTS
)
1002 v24_sig
&= ~RFCOMM_V24_RTR
;
1003 if (clear
& TIOCM_RI
)
1004 v24_sig
&= ~RFCOMM_V24_IC
;
1005 if (clear
& TIOCM_CD
)
1006 v24_sig
&= ~RFCOMM_V24_DV
;
1008 rfcomm_dlc_set_modem_status(dlc
, v24_sig
);
1013 /* ---- TTY structure ---- */
1015 static const struct tty_operations rfcomm_ops
= {
1016 .open
= rfcomm_tty_open
,
1017 .close
= rfcomm_tty_close
,
1018 .write
= rfcomm_tty_write
,
1019 .write_room
= rfcomm_tty_write_room
,
1020 .chars_in_buffer
= rfcomm_tty_chars_in_buffer
,
1021 .flush_buffer
= rfcomm_tty_flush_buffer
,
1022 .ioctl
= rfcomm_tty_ioctl
,
1023 .throttle
= rfcomm_tty_throttle
,
1024 .unthrottle
= rfcomm_tty_unthrottle
,
1025 .set_termios
= rfcomm_tty_set_termios
,
1026 .send_xchar
= rfcomm_tty_send_xchar
,
1027 .hangup
= rfcomm_tty_hangup
,
1028 .wait_until_sent
= rfcomm_tty_wait_until_sent
,
1029 .read_proc
= rfcomm_tty_read_proc
,
1030 .tiocmget
= rfcomm_tty_tiocmget
,
1031 .tiocmset
= rfcomm_tty_tiocmset
,
1034 int rfcomm_init_ttys(void)
1036 rfcomm_tty_driver
= alloc_tty_driver(RFCOMM_TTY_PORTS
);
1037 if (!rfcomm_tty_driver
)
1040 rfcomm_tty_driver
->owner
= THIS_MODULE
;
1041 rfcomm_tty_driver
->driver_name
= "rfcomm";
1042 rfcomm_tty_driver
->name
= "rfcomm";
1043 rfcomm_tty_driver
->major
= RFCOMM_TTY_MAJOR
;
1044 rfcomm_tty_driver
->minor_start
= RFCOMM_TTY_MINOR
;
1045 rfcomm_tty_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1046 rfcomm_tty_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1047 rfcomm_tty_driver
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
1048 rfcomm_tty_driver
->init_termios
= tty_std_termios
;
1049 rfcomm_tty_driver
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1050 tty_set_operations(rfcomm_tty_driver
, &rfcomm_ops
);
1052 if (tty_register_driver(rfcomm_tty_driver
)) {
1053 BT_ERR("Can't register RFCOMM TTY driver");
1054 put_tty_driver(rfcomm_tty_driver
);
1058 BT_INFO("RFCOMM TTY layer initialized");
1063 void rfcomm_cleanup_ttys(void)
1065 tty_unregister_driver(rfcomm_tty_driver
);
1066 put_tty_driver(rfcomm_tty_driver
);