28 #include <linux/module.h>
30 #include <linux/tty.h>
38 #define RFCOMM_TTY_MAGIC 0x6d02
39 #define RFCOMM_TTY_PORTS RFCOMM_MAX_DEV
40 #define RFCOMM_TTY_MAJOR 216
41 #define RFCOMM_TTY_MINOR 0
74 static void rfcomm_dev_state_change(
struct rfcomm_dlc *dlc,
int err);
75 static void rfcomm_dev_modem_status(
struct rfcomm_dlc *dlc,
u8 v24_sig);
91 BT_DBG(
"dev %p dlc %p", dev, dlc);
100 if (dlc->
owner == dev)
116 .destruct = rfcomm_dev_destruct,
119 static struct rfcomm_dev *__rfcomm_dev_get(
int id)
134 spin_lock(&rfcomm_dev_lock);
136 dev = __rfcomm_dev_get(
id);
142 tty_port_get(&dev->
port);
145 spin_unlock(&rfcomm_dev_lock);
159 conn = hci_conn_hash_lookup_ba(hdev,
ACL_LINK, &dev->
dst);
163 return conn ? &conn->
dev :
NULL;
193 spin_lock(&rfcomm_dev_lock);
199 if (entry->
id != dev->
id)
209 if (entry->
id == dev->
id) {
214 if (entry->
id > dev->
id - 1)
228 list_add(&dev->
list, head);
230 bacpy(&dev->
src, &req->
src);
231 bacpy(&dev->
dst, &req->
dst);
238 dev->
port.ops = &rfcomm_port_ops;
241 skb_queue_head_init(&dev->
pending);
251 rfcomm_dlc_throttle(dlc);
276 spin_unlock(&rfcomm_dev_lock);
292 BT_ERR(
"Failed to create address attribute");
295 BT_ERR(
"Failed to create channel attribute");
304 static void rfcomm_dev_del(
struct rfcomm_dev *dev)
312 if (dev->
port.count > 0) {
313 spin_unlock_irqrestore(&dev->
port.lock, flags);
316 spin_unlock_irqrestore(&dev->
port.lock, flags);
318 spin_lock(&rfcomm_dev_lock);
319 list_del_init(&dev->
list);
320 spin_unlock(&rfcomm_dev_lock);
326 static inline unsigned int rfcomm_room(
struct rfcomm_dlc *dlc)
333 static void rfcomm_wfree(
struct sk_buff *skb)
345 tty_port_get(&dev->
port);
347 skb->
sk = (
void *) dev;
354 struct sk_buff *skb = alloc_skb(size, priority);
356 rfcomm_set_owner_w(skb, dev);
365 #define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP))
367 static int rfcomm_create_dev(
struct sock *sk,
void __user *
arg)
387 rfcomm_dlc_hold(dlc);
394 id = rfcomm_dev_add(&req, dlc);
409 static int rfcomm_release_dev(
void __user *
arg)
419 dev = rfcomm_dev_get(req.
dev_id);
441 static int rfcomm_get_dev_list(
void __user *arg)
454 if (!dev_num || dev_num > (
PAGE_SIZE * 4) /
sizeof(*di))
457 size =
sizeof(*dl) + dev_num *
sizeof(*di);
465 spin_lock(&rfcomm_dev_lock);
470 (di +
n)->
id = dev->
id;
471 (di + n)->flags = dev->
flags;
473 (di + n)->channel = dev->
channel;
474 bacpy(&(di + n)->
src, &dev->
src);
475 bacpy(&(di + n)->
dst, &dev->
dst);
480 spin_unlock(&rfcomm_dev_lock);
483 size =
sizeof(*dl) + n *
sizeof(*di);
491 static int rfcomm_get_dev_info(
void __user *arg)
502 dev = rfcomm_dev_get(di.id);
506 di.flags = dev->
flags;
508 di.state = dev->
dlc->state;
509 bacpy(&di.src, &dev->
src);
510 bacpy(&di.dst, &dev->
dst);
521 BT_DBG(
"cmd %d arg %p", cmd, arg);
525 return rfcomm_create_dev(sk, arg);
528 return rfcomm_release_dev(arg);
531 return rfcomm_get_dev_list(arg);
534 return rfcomm_get_dev_info(arg);
552 if (!tty || !skb_queue_empty(&dev->
pending)) {
557 BT_DBG(
"dlc %p tty %p len %d", dlc, tty, skb->
len);
559 tty_insert_flip_string(tty, skb->
data, skb->
len);
565 static void rfcomm_dev_state_change(
struct rfcomm_dlc *dlc,
int err)
571 BT_DBG(
"dlc %p dev %p err %d", dlc, dev, err);
577 if (!dev->
port.tty) {
587 if (rfcomm_dev_get(dev->
id) == NULL) {
601 static void rfcomm_dev_modem_status(
struct rfcomm_dlc *dlc,
u8 v24_sig)
607 BT_DBG(
"dlc %p dev %p v24_sig 0x%02x", dlc, dev, v24_sig);
622 static void rfcomm_tty_copy_pending(
struct rfcomm_dev *dev)
631 BT_DBG(
"dev %p tty %p", dev, tty);
636 inserted += tty_insert_flip_string(tty, skb->
data, skb->
len);
646 static int rfcomm_tty_open(
struct tty_struct *tty,
struct file *filp)
656 BT_DBG(
"tty %p id %d", tty,
id);
662 dev = rfcomm_dev_get(
id);
670 if (++dev->
port.count > 1) {
671 spin_unlock_irqrestore(&dev->
port.lock, flags);
674 spin_unlock_irqrestore(&dev->
port.lock, flags);
719 rfcomm_tty_copy_pending(dev);
721 rfcomm_dlc_unthrottle(dev->
dlc);
726 static void rfcomm_tty_close(
struct tty_struct *tty,
struct file *filp)
734 BT_DBG(
"tty %p dev %p dlc %p opened %d", tty, dev, dev->
dlc,
738 if (!--dev->
port.count) {
739 spin_unlock_irqrestore(&dev->
port.lock, flags);
754 spin_lock(&rfcomm_dev_lock);
755 list_del_init(&dev->
list);
756 spin_unlock(&rfcomm_dev_lock);
761 spin_unlock_irqrestore(&dev->
port.lock, flags);
766 static int rfcomm_tty_write(
struct tty_struct *tty,
const unsigned char *buf,
int count)
771 int err = 0, sent = 0, size;
773 BT_DBG(
"tty %p count %d", tty, count);
797 return sent ? sent :
err;
800 static int rfcomm_tty_write_room(
struct tty_struct *tty)
807 if (!dev || !dev->
dlc)
817 static int rfcomm_tty_ioctl(
struct tty_struct *tty,
unsigned int cmd,
unsigned long arg)
819 BT_DBG(
"tty %p cmd 0x%02x", tty, cmd);
823 BT_DBG(
"TCGETS is not supported");
827 BT_DBG(
"TCSETS is not supported");
835 BT_ERR(
"TIOCGSERIAL is not supported");
839 BT_ERR(
"TIOCSSERIAL is not supported");
843 BT_ERR(
"TIOCSERGSTRUCT is not supported");
847 BT_ERR(
"TIOCSERGETLSR is not supported");
851 BT_ERR(
"TIOCSERCONFIG is not supported");
864 struct ktermios *
new = &tty->termios;
873 BT_DBG(
"tty %p termios %p", tty, old);
875 if (!dev || !dev->
dlc || !dev->
dlc->session)
880 BT_DBG(
"Turning off CRTSCTS unsupported");
886 BT_DBG(
"Parity change detected.");
890 if (new->c_cflag &
PARENB) {
891 if (new->c_cflag &
PARODD) {
906 x_on =
new->c_cc[
VSTOP];
915 x_off =
new->c_cc[
VSTART];
929 if (new->c_cflag &
CSTOPB)
938 switch (new->c_cflag &
CSIZE) {
957 if (old_baud_rate != new_baud_rate)
960 switch (new_baud_rate) {
997 data_bits, stop_bits, parity,
1001 static void rfcomm_tty_throttle(
struct tty_struct *tty)
1005 BT_DBG(
"tty %p dev %p", tty, dev);
1007 rfcomm_dlc_throttle(dev->
dlc);
1010 static void rfcomm_tty_unthrottle(
struct tty_struct *tty)
1014 BT_DBG(
"tty %p dev %p", tty, dev);
1016 rfcomm_dlc_unthrottle(dev->
dlc);
1019 static int rfcomm_tty_chars_in_buffer(
struct tty_struct *tty)
1023 BT_DBG(
"tty %p dev %p", tty, dev);
1025 if (!dev || !dev->
dlc)
1028 if (!skb_queue_empty(&dev->
dlc->tx_queue))
1029 return dev->
dlc->mtu;
1034 static void rfcomm_tty_flush_buffer(
struct tty_struct *tty)
1038 BT_DBG(
"tty %p dev %p", tty, dev);
1040 if (!dev || !dev->
dlc)
1047 static void rfcomm_tty_send_xchar(
struct tty_struct *tty,
char ch)
1049 BT_DBG(
"tty %p ch %c", tty, ch);
1052 static void rfcomm_tty_wait_until_sent(
struct tty_struct *tty,
int timeout)
1054 BT_DBG(
"tty %p timeout %d", tty, timeout);
1057 static void rfcomm_tty_hangup(
struct tty_struct *tty)
1061 BT_DBG(
"tty %p dev %p", tty, dev);
1066 rfcomm_tty_flush_buffer(tty);
1069 if (rfcomm_dev_get(dev->
id) == NULL)
1071 rfcomm_dev_del(dev);
1076 static int rfcomm_tty_tiocmget(
struct tty_struct *tty)
1080 BT_DBG(
"tty %p dev %p", tty, dev);
1085 static int rfcomm_tty_tiocmset(
struct tty_struct *tty,
unsigned int set,
unsigned int clear)
1091 BT_DBG(
"tty %p dev %p set 0x%02x clear 0x%02x", tty, dev,
set, clear);
1111 v24_sig &= ~RFCOMM_V24_DV;
1121 .open = rfcomm_tty_open,
1122 .close = rfcomm_tty_close,
1123 .write = rfcomm_tty_write,
1124 .write_room = rfcomm_tty_write_room,
1125 .chars_in_buffer = rfcomm_tty_chars_in_buffer,
1126 .flush_buffer = rfcomm_tty_flush_buffer,
1127 .ioctl = rfcomm_tty_ioctl,
1128 .throttle = rfcomm_tty_throttle,
1129 .unthrottle = rfcomm_tty_unthrottle,
1130 .set_termios = rfcomm_tty_set_termios,
1131 .send_xchar = rfcomm_tty_send_xchar,
1132 .hangup = rfcomm_tty_hangup,
1133 .wait_until_sent = rfcomm_tty_wait_until_sent,
1134 .tiocmget = rfcomm_tty_tiocmget,
1135 .tiocmset = rfcomm_tty_tiocmset,
1143 if (!rfcomm_tty_driver)
1147 rfcomm_tty_driver->
name =
"rfcomm";
1160 BT_ERR(
"Can't register RFCOMM TTY driver");
1165 BT_INFO(
"RFCOMM TTY layer initialized");