|
@@ -58,7 +58,6 @@ struct rfcomm_dev {
|
|
|
uint modem_status;
|
|
|
|
|
|
struct rfcomm_dlc *dlc;
|
|
|
- wait_queue_head_t wait;
|
|
|
|
|
|
struct device *tty_dev;
|
|
|
|
|
@@ -76,13 +75,6 @@ static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig);
|
|
|
|
|
|
/* ---- Device functions ---- */
|
|
|
|
|
|
-/*
|
|
|
- * The reason this isn't actually a race, as you no doubt have a little voice
|
|
|
- * screaming at you in your head, is that the refcount should never actually
|
|
|
- * reach zero unless the device has already been taken off the list, in
|
|
|
- * rfcomm_dev_del(). And if that's not true, we'll hit the BUG() in
|
|
|
- * rfcomm_dev_destruct() anyway.
|
|
|
- */
|
|
|
static void rfcomm_dev_destruct(struct tty_port *port)
|
|
|
{
|
|
|
struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
|
|
@@ -90,10 +82,9 @@ static void rfcomm_dev_destruct(struct tty_port *port)
|
|
|
|
|
|
BT_DBG("dev %p dlc %p", dev, dlc);
|
|
|
|
|
|
- /* Refcount should only hit zero when called from rfcomm_dev_del()
|
|
|
- which will have taken us off the list. Everything else are
|
|
|
- refcounting bugs. */
|
|
|
- BUG_ON(!list_empty(&dev->list));
|
|
|
+ spin_lock(&rfcomm_dev_lock);
|
|
|
+ list_del(&dev->list);
|
|
|
+ spin_unlock(&rfcomm_dev_lock);
|
|
|
|
|
|
rfcomm_dlc_lock(dlc);
|
|
|
/* Detach DLC if it's owned by this dev */
|
|
@@ -112,8 +103,39 @@ static void rfcomm_dev_destruct(struct tty_port *port)
|
|
|
module_put(THIS_MODULE);
|
|
|
}
|
|
|
|
|
|
+/* device-specific initialization: open the dlc */
|
|
|
+static int rfcomm_dev_activate(struct tty_port *port, struct tty_struct *tty)
|
|
|
+{
|
|
|
+ struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
|
|
|
+
|
|
|
+ return rfcomm_dlc_open(dev->dlc, &dev->src, &dev->dst, dev->channel);
|
|
|
+}
|
|
|
+
|
|
|
+/* we block the open until the dlc->state becomes BT_CONNECTED */
|
|
|
+static int rfcomm_dev_carrier_raised(struct tty_port *port)
|
|
|
+{
|
|
|
+ struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
|
|
|
+
|
|
|
+ return (dev->dlc->state == BT_CONNECTED);
|
|
|
+}
|
|
|
+
|
|
|
+/* device-specific cleanup: close the dlc */
|
|
|
+static void rfcomm_dev_shutdown(struct tty_port *port)
|
|
|
+{
|
|
|
+ struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
|
|
|
+
|
|
|
+ if (dev->tty_dev->parent)
|
|
|
+ device_move(dev->tty_dev, NULL, DPM_ORDER_DEV_LAST);
|
|
|
+
|
|
|
+ /* close the dlc */
|
|
|
+ rfcomm_dlc_close(dev->dlc, 0);
|
|
|
+}
|
|
|
+
|
|
|
static const struct tty_port_operations rfcomm_port_ops = {
|
|
|
.destruct = rfcomm_dev_destruct,
|
|
|
+ .activate = rfcomm_dev_activate,
|
|
|
+ .shutdown = rfcomm_dev_shutdown,
|
|
|
+ .carrier_raised = rfcomm_dev_carrier_raised,
|
|
|
};
|
|
|
|
|
|
static struct rfcomm_dev *__rfcomm_dev_get(int id)
|
|
@@ -236,7 +258,6 @@ static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
|
|
|
|
|
|
tty_port_init(&dev->port);
|
|
|
dev->port.ops = &rfcomm_port_ops;
|
|
|
- init_waitqueue_head(&dev->wait);
|
|
|
|
|
|
skb_queue_head_init(&dev->pending);
|
|
|
|
|
@@ -282,7 +303,9 @@ out:
|
|
|
dev->id, NULL);
|
|
|
if (IS_ERR(dev->tty_dev)) {
|
|
|
err = PTR_ERR(dev->tty_dev);
|
|
|
+ spin_lock(&rfcomm_dev_lock);
|
|
|
list_del(&dev->list);
|
|
|
+ spin_unlock(&rfcomm_dev_lock);
|
|
|
goto free;
|
|
|
}
|
|
|
|
|
@@ -301,27 +324,6 @@ free:
|
|
|
return err;
|
|
|
}
|
|
|
|
|
|
-static void rfcomm_dev_del(struct rfcomm_dev *dev)
|
|
|
-{
|
|
|
- unsigned long flags;
|
|
|
- BT_DBG("dev %p", dev);
|
|
|
-
|
|
|
- BUG_ON(test_and_set_bit(RFCOMM_TTY_RELEASED, &dev->flags));
|
|
|
-
|
|
|
- spin_lock_irqsave(&dev->port.lock, flags);
|
|
|
- if (dev->port.count > 0) {
|
|
|
- spin_unlock_irqrestore(&dev->port.lock, flags);
|
|
|
- return;
|
|
|
- }
|
|
|
- spin_unlock_irqrestore(&dev->port.lock, flags);
|
|
|
-
|
|
|
- spin_lock(&rfcomm_dev_lock);
|
|
|
- list_del_init(&dev->list);
|
|
|
- spin_unlock(&rfcomm_dev_lock);
|
|
|
-
|
|
|
- tty_port_put(&dev->port);
|
|
|
-}
|
|
|
-
|
|
|
/* ---- Send buffer ---- */
|
|
|
static inline unsigned int rfcomm_room(struct rfcomm_dlc *dlc)
|
|
|
{
|
|
@@ -333,10 +335,9 @@ static inline unsigned int rfcomm_room(struct rfcomm_dlc *dlc)
|
|
|
static void rfcomm_wfree(struct sk_buff *skb)
|
|
|
{
|
|
|
struct rfcomm_dev *dev = (void *) skb->sk;
|
|
|
- struct tty_struct *tty = dev->port.tty;
|
|
|
atomic_sub(skb->truesize, &dev->wmem_alloc);
|
|
|
- if (test_bit(RFCOMM_TTY_ATTACHED, &dev->flags) && tty)
|
|
|
- tty_wakeup(tty);
|
|
|
+ if (test_bit(RFCOMM_TTY_ATTACHED, &dev->flags))
|
|
|
+ tty_port_tty_wakeup(&dev->port);
|
|
|
tty_port_put(&dev->port);
|
|
|
}
|
|
|
|
|
@@ -410,6 +411,7 @@ static int rfcomm_release_dev(void __user *arg)
|
|
|
{
|
|
|
struct rfcomm_dev_req req;
|
|
|
struct rfcomm_dev *dev;
|
|
|
+ struct tty_struct *tty;
|
|
|
|
|
|
if (copy_from_user(&req, arg, sizeof(req)))
|
|
|
return -EFAULT;
|
|
@@ -429,11 +431,15 @@ static int rfcomm_release_dev(void __user *arg)
|
|
|
rfcomm_dlc_close(dev->dlc, 0);
|
|
|
|
|
|
/* Shut down TTY synchronously before freeing rfcomm_dev */
|
|
|
- if (dev->port.tty)
|
|
|
- tty_vhangup(dev->port.tty);
|
|
|
+ tty = tty_port_tty_get(&dev->port);
|
|
|
+ if (tty) {
|
|
|
+ tty_vhangup(tty);
|
|
|
+ tty_kref_put(tty);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!test_and_set_bit(RFCOMM_TTY_RELEASED, &dev->flags))
|
|
|
+ tty_port_put(&dev->port);
|
|
|
|
|
|
- if (!test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags))
|
|
|
- rfcomm_dev_del(dev);
|
|
|
tty_port_put(&dev->port);
|
|
|
return 0;
|
|
|
}
|
|
@@ -563,16 +569,21 @@ static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb)
|
|
|
static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
|
|
|
{
|
|
|
struct rfcomm_dev *dev = dlc->owner;
|
|
|
+ struct tty_struct *tty;
|
|
|
if (!dev)
|
|
|
return;
|
|
|
|
|
|
BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
|
|
|
|
|
|
dev->err = err;
|
|
|
- wake_up_interruptible(&dev->wait);
|
|
|
+ if (dlc->state == BT_CONNECTED) {
|
|
|
+ device_move(dev->tty_dev, rfcomm_get_device(dev),
|
|
|
+ DPM_ORDER_DEV_AFTER_PARENT);
|
|
|
|
|
|
- if (dlc->state == BT_CLOSED) {
|
|
|
- if (!dev->port.tty) {
|
|
|
+ wake_up_interruptible(&dev->port.open_wait);
|
|
|
+ } else if (dlc->state == BT_CLOSED) {
|
|
|
+ tty = tty_port_tty_get(&dev->port);
|
|
|
+ if (!tty) {
|
|
|
if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
|
|
|
/* Drop DLC lock here to avoid deadlock
|
|
|
* 1. rfcomm_dev_get will take rfcomm_dev_lock
|
|
@@ -580,6 +591,9 @@ static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
|
|
|
* rfcomm_dev_lock -> dlc lock
|
|
|
* 2. tty_port_put will deadlock if it's
|
|
|
* the last reference
|
|
|
+ *
|
|
|
+ * FIXME: when we release the lock anything
|
|
|
+ * could happen to dev, even its destruction
|
|
|
*/
|
|
|
rfcomm_dlc_unlock(dlc);
|
|
|
if (rfcomm_dev_get(dev->id) == NULL) {
|
|
@@ -587,12 +601,17 @@ static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- rfcomm_dev_del(dev);
|
|
|
+ if (!test_and_set_bit(RFCOMM_TTY_RELEASED,
|
|
|
+ &dev->flags))
|
|
|
+ tty_port_put(&dev->port);
|
|
|
+
|
|
|
tty_port_put(&dev->port);
|
|
|
rfcomm_dlc_lock(dlc);
|
|
|
}
|
|
|
- } else
|
|
|
- tty_hangup(dev->port.tty);
|
|
|
+ } else {
|
|
|
+ tty_hangup(tty);
|
|
|
+ tty_kref_put(tty);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -604,10 +623,8 @@ static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig)
|
|
|
|
|
|
BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc, dev, v24_sig);
|
|
|
|
|
|
- if ((dev->modem_status & TIOCM_CD) && !(v24_sig & RFCOMM_V24_DV)) {
|
|
|
- if (dev->port.tty && !C_CLOCAL(dev->port.tty))
|
|
|
- tty_hangup(dev->port.tty);
|
|
|
- }
|
|
|
+ if ((dev->modem_status & TIOCM_CD) && !(v24_sig & RFCOMM_V24_DV))
|
|
|
+ tty_port_tty_hangup(&dev->port, true);
|
|
|
|
|
|
dev->modem_status =
|
|
|
((v24_sig & RFCOMM_V24_RTC) ? (TIOCM_DSR | TIOCM_DTR) : 0) |
|
|
@@ -638,124 +655,92 @@ static void rfcomm_tty_copy_pending(struct rfcomm_dev *dev)
|
|
|
tty_flip_buffer_push(&dev->port);
|
|
|
}
|
|
|
|
|
|
-static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
|
|
|
+/* do the reverse of install, clearing the tty fields and releasing the
|
|
|
+ * reference to tty_port
|
|
|
+ */
|
|
|
+static void rfcomm_tty_cleanup(struct tty_struct *tty)
|
|
|
{
|
|
|
- DECLARE_WAITQUEUE(wait, current);
|
|
|
- struct rfcomm_dev *dev;
|
|
|
- struct rfcomm_dlc *dlc;
|
|
|
- unsigned long flags;
|
|
|
- int err, id;
|
|
|
+ struct rfcomm_dev *dev = tty->driver_data;
|
|
|
|
|
|
- id = tty->index;
|
|
|
+ clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
|
|
|
|
|
|
- BT_DBG("tty %p id %d", tty, id);
|
|
|
+ rfcomm_dlc_lock(dev->dlc);
|
|
|
+ tty->driver_data = NULL;
|
|
|
+ rfcomm_dlc_unlock(dev->dlc);
|
|
|
|
|
|
- /* We don't leak this refcount. For reasons which are not entirely
|
|
|
- clear, the TTY layer will call our ->close() method even if the
|
|
|
- open fails. We decrease the refcount there, and decreasing it
|
|
|
- here too would cause breakage. */
|
|
|
- dev = rfcomm_dev_get(id);
|
|
|
- if (!dev)
|
|
|
- return -ENODEV;
|
|
|
+ /*
|
|
|
+ * purge the dlc->tx_queue to avoid circular dependencies
|
|
|
+ * between dev and dlc
|
|
|
+ */
|
|
|
+ skb_queue_purge(&dev->dlc->tx_queue);
|
|
|
|
|
|
- BT_DBG("dev %p dst %pMR channel %d opened %d", dev, &dev->dst,
|
|
|
- dev->channel, dev->port.count);
|
|
|
+ tty_port_put(&dev->port);
|
|
|
+}
|
|
|
|
|
|
- spin_lock_irqsave(&dev->port.lock, flags);
|
|
|
- if (++dev->port.count > 1) {
|
|
|
- spin_unlock_irqrestore(&dev->port.lock, flags);
|
|
|
- return 0;
|
|
|
- }
|
|
|
- spin_unlock_irqrestore(&dev->port.lock, flags);
|
|
|
+/* we acquire the tty_port reference since it's here the tty is first used
|
|
|
+ * by setting the termios. We also populate the driver_data field and install
|
|
|
+ * the tty port
|
|
|
+ */
|
|
|
+static int rfcomm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
|
|
|
+{
|
|
|
+ struct rfcomm_dev *dev;
|
|
|
+ struct rfcomm_dlc *dlc;
|
|
|
+ int err;
|
|
|
+
|
|
|
+ dev = rfcomm_dev_get(tty->index);
|
|
|
+ if (!dev)
|
|
|
+ return -ENODEV;
|
|
|
|
|
|
dlc = dev->dlc;
|
|
|
|
|
|
/* Attach TTY and open DLC */
|
|
|
-
|
|
|
rfcomm_dlc_lock(dlc);
|
|
|
tty->driver_data = dev;
|
|
|
- dev->port.tty = tty;
|
|
|
rfcomm_dlc_unlock(dlc);
|
|
|
set_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
|
|
|
|
|
|
- err = rfcomm_dlc_open(dlc, &dev->src, &dev->dst, dev->channel);
|
|
|
- if (err < 0)
|
|
|
- return err;
|
|
|
-
|
|
|
- /* Wait for DLC to connect */
|
|
|
- add_wait_queue(&dev->wait, &wait);
|
|
|
- while (1) {
|
|
|
- set_current_state(TASK_INTERRUPTIBLE);
|
|
|
+ /* install the tty_port */
|
|
|
+ err = tty_port_install(&dev->port, driver, tty);
|
|
|
+ if (err)
|
|
|
+ rfcomm_tty_cleanup(tty);
|
|
|
|
|
|
- if (dlc->state == BT_CLOSED) {
|
|
|
- err = -dev->err;
|
|
|
- break;
|
|
|
- }
|
|
|
+ return err;
|
|
|
+}
|
|
|
|
|
|
- if (dlc->state == BT_CONNECTED)
|
|
|
- break;
|
|
|
+static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
|
|
|
+{
|
|
|
+ struct rfcomm_dev *dev = tty->driver_data;
|
|
|
+ int err;
|
|
|
|
|
|
- if (signal_pending(current)) {
|
|
|
- err = -EINTR;
|
|
|
- break;
|
|
|
- }
|
|
|
+ BT_DBG("tty %p id %d", tty, tty->index);
|
|
|
|
|
|
- tty_unlock(tty);
|
|
|
- schedule();
|
|
|
- tty_lock(tty);
|
|
|
- }
|
|
|
- set_current_state(TASK_RUNNING);
|
|
|
- remove_wait_queue(&dev->wait, &wait);
|
|
|
+ BT_DBG("dev %p dst %pMR channel %d opened %d", dev, &dev->dst,
|
|
|
+ dev->channel, dev->port.count);
|
|
|
|
|
|
- if (err == 0)
|
|
|
- device_move(dev->tty_dev, rfcomm_get_device(dev),
|
|
|
- DPM_ORDER_DEV_AFTER_PARENT);
|
|
|
+ err = tty_port_open(&dev->port, tty, filp);
|
|
|
+ if (err)
|
|
|
+ return err;
|
|
|
|
|
|
+ /*
|
|
|
+ * FIXME: rfcomm should use proper flow control for
|
|
|
+ * received data. This hack will be unnecessary and can
|
|
|
+ * be removed when that's implemented
|
|
|
+ */
|
|
|
rfcomm_tty_copy_pending(dev);
|
|
|
|
|
|
rfcomm_dlc_unthrottle(dev->dlc);
|
|
|
|
|
|
- return err;
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
|
|
|
{
|
|
|
struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
|
|
|
- unsigned long flags;
|
|
|
-
|
|
|
- if (!dev)
|
|
|
- return;
|
|
|
|
|
|
BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc,
|
|
|
dev->port.count);
|
|
|
|
|
|
- spin_lock_irqsave(&dev->port.lock, flags);
|
|
|
- if (!--dev->port.count) {
|
|
|
- spin_unlock_irqrestore(&dev->port.lock, flags);
|
|
|
- if (dev->tty_dev->parent)
|
|
|
- device_move(dev->tty_dev, NULL, DPM_ORDER_DEV_LAST);
|
|
|
-
|
|
|
- /* Close DLC and dettach TTY */
|
|
|
- rfcomm_dlc_close(dev->dlc, 0);
|
|
|
-
|
|
|
- clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
|
|
|
-
|
|
|
- rfcomm_dlc_lock(dev->dlc);
|
|
|
- tty->driver_data = NULL;
|
|
|
- dev->port.tty = NULL;
|
|
|
- rfcomm_dlc_unlock(dev->dlc);
|
|
|
-
|
|
|
- if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags)) {
|
|
|
- spin_lock(&rfcomm_dev_lock);
|
|
|
- list_del_init(&dev->list);
|
|
|
- spin_unlock(&rfcomm_dev_lock);
|
|
|
-
|
|
|
- tty_port_put(&dev->port);
|
|
|
- }
|
|
|
- } else
|
|
|
- spin_unlock_irqrestore(&dev->port.lock, flags);
|
|
|
-
|
|
|
- tty_port_put(&dev->port);
|
|
|
+ tty_port_close(&dev->port, tty, filp);
|
|
|
}
|
|
|
|
|
|
static int rfcomm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
|
|
@@ -1055,17 +1040,11 @@ static void rfcomm_tty_hangup(struct tty_struct *tty)
|
|
|
|
|
|
BT_DBG("tty %p dev %p", tty, dev);
|
|
|
|
|
|
- if (!dev)
|
|
|
- return;
|
|
|
-
|
|
|
- rfcomm_tty_flush_buffer(tty);
|
|
|
+ tty_port_hangup(&dev->port);
|
|
|
|
|
|
- if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
|
|
|
- if (rfcomm_dev_get(dev->id) == NULL)
|
|
|
- return;
|
|
|
- rfcomm_dev_del(dev);
|
|
|
+ if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags) &&
|
|
|
+ !test_and_set_bit(RFCOMM_TTY_RELEASED, &dev->flags))
|
|
|
tty_port_put(&dev->port);
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
static int rfcomm_tty_tiocmget(struct tty_struct *tty)
|
|
@@ -1128,6 +1107,8 @@ static const struct tty_operations rfcomm_ops = {
|
|
|
.wait_until_sent = rfcomm_tty_wait_until_sent,
|
|
|
.tiocmget = rfcomm_tty_tiocmget,
|
|
|
.tiocmset = rfcomm_tty_tiocmset,
|
|
|
+ .install = rfcomm_tty_install,
|
|
|
+ .cleanup = rfcomm_tty_cleanup,
|
|
|
};
|
|
|
|
|
|
int __init rfcomm_init_ttys(void)
|
|
@@ -1146,7 +1127,7 @@ int __init rfcomm_init_ttys(void)
|
|
|
rfcomm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
|
|
|
rfcomm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
|
|
|
rfcomm_tty_driver->init_termios = tty_std_termios;
|
|
|
- rfcomm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
|
|
|
+ rfcomm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
|
|
|
rfcomm_tty_driver->init_termios.c_lflag &= ~ICANON;
|
|
|
tty_set_operations(rfcomm_tty_driver, &rfcomm_ops);
|
|
|
|