proc tty: remove struct tty_operations::read_proc
[safe/jmp/linux-2.6] / net / bluetooth / rfcomm / tty.c
index bbc3a44..cab71ea 100644 (file)
@@ -1,4 +1,4 @@
-/* 
+/*
    RFCOMM implementation for Linux Bluetooth stack (BlueZ).
    Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
    Copyright (C) 2002 Marcel Holtmann <marcel@holtmann.org>
    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
-   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
-   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
-   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
+   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
+   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
-   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
-   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
+   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
+   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
    SOFTWARE IS DISCLAIMED.
 */
 
 /*
  * RFCOMM TTY.
- *
- * $Id: tty.c,v 1.24 2002/10/03 01:54:38 holtmann Exp $
  */
 
-#include <linux/config.h>
 #include <linux/module.h>
 
 #include <linux/tty.h>
 #include <linux/tty_driver.h>
 #include <linux/tty_flip.h>
 
+#include <linux/capability.h>
 #include <linux/slab.h>
 #include <linux/skbuff.h>
 
 #include <net/bluetooth/bluetooth.h>
+#include <net/bluetooth/hci_core.h>
 #include <net/bluetooth/rfcomm.h>
 
-#ifndef CONFIG_BT_RFCOMM_DEBUG
-#undef  BT_DBG
-#define BT_DBG(D...)
-#endif
-
 #define RFCOMM_TTY_MAGIC 0x6d02                /* magic number for rfcomm struct */
 #define RFCOMM_TTY_PORTS RFCOMM_MAX_DEV        /* whole lotta rfcomm devices */
 #define RFCOMM_TTY_MAJOR 216           /* device node major id of the usb/bluetooth.c driver */
@@ -59,7 +53,7 @@ struct rfcomm_dev {
        char                    name[12];
        int                     id;
        unsigned long           flags;
-       int                     opened;
+       atomic_t                opened;
        int                     err;
 
        bdaddr_t                src;
@@ -73,7 +67,11 @@ struct rfcomm_dev {
        wait_queue_head_t       wait;
        struct tasklet_struct   wakeup_task;
 
+       struct device           *tty_dev;
+
        atomic_t                wmem_alloc;
+
+       struct sk_buff_head     pending;
 };
 
 static LIST_HEAD(rfcomm_dev_list);
@@ -92,6 +90,11 @@ static void rfcomm_dev_destruct(struct rfcomm_dev *dev)
 
        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));
+
        rfcomm_dlc_lock(dlc);
        /* Detach DLC if it's owned by this dev */
        if (dlc->owner == dev)
@@ -102,14 +105,9 @@ static void rfcomm_dev_destruct(struct rfcomm_dev *dev)
 
        tty_unregister_device(rfcomm_tty_driver, dev->id);
 
-       /* 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));
-
        kfree(dev);
 
-       /* It's safe to call module_put() here because socket still 
+       /* It's safe to call module_put() here because socket still
           holds reference to this module. */
        module_put(THIS_MODULE);
 }
@@ -153,14 +151,52 @@ static inline struct rfcomm_dev *rfcomm_dev_get(int id)
        read_lock(&rfcomm_dev_lock);
 
        dev = __rfcomm_dev_get(id);
-       if (dev)
-               rfcomm_dev_hold(dev);
+
+       if (dev) {
+               if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
+                       dev = NULL;
+               else
+                       rfcomm_dev_hold(dev);
+       }
 
        read_unlock(&rfcomm_dev_lock);
 
        return dev;
 }
 
+static struct device *rfcomm_get_device(struct rfcomm_dev *dev)
+{
+       struct hci_dev *hdev;
+       struct hci_conn *conn;
+
+       hdev = hci_get_route(&dev->dst, &dev->src);
+       if (!hdev)
+               return NULL;
+
+       conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &dev->dst);
+
+       hci_dev_put(hdev);
+
+       return conn ? &conn->dev : NULL;
+}
+
+static ssize_t show_address(struct device *tty_dev, struct device_attribute *attr, char *buf)
+{
+       struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
+       bdaddr_t bdaddr;
+       baswap(&bdaddr, &dev->dst);
+       return sprintf(buf, "%s\n", batostr(&bdaddr));
+}
+
+static ssize_t show_channel(struct device *tty_dev, struct device_attribute *attr, char *buf)
+{
+       struct rfcomm_dev *dev = dev_get_drvdata(tty_dev);
+       return sprintf(buf, "%d\n", dev->channel);
+}
+
+static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
+static DEVICE_ATTR(channel, S_IRUGO, show_channel, NULL);
+
 static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
 {
        struct rfcomm_dev *dev;
@@ -168,11 +204,10 @@ static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
        int err = 0;
 
        BT_DBG("id %d channel %d", req->dev_id, req->channel);
-       
-       dev = kmalloc(sizeof(struct rfcomm_dev), GFP_KERNEL);
+
+       dev = kzalloc(sizeof(struct rfcomm_dev), GFP_KERNEL);
        if (!dev)
                return -ENOMEM;
-       memset(dev, 0, sizeof(struct rfcomm_dev));
 
        write_lock_bh(&rfcomm_dev_lock);
 
@@ -218,42 +253,86 @@ static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
        bacpy(&dev->dst, &req->dst);
        dev->channel = req->channel;
 
-       dev->flags = req->flags & 
+       dev->flags = req->flags &
                ((1 << RFCOMM_RELEASE_ONHUP) | (1 << RFCOMM_REUSE_DLC));
 
+       atomic_set(&dev->opened, 0);
+
        init_waitqueue_head(&dev->wait);
        tasklet_init(&dev->wakeup_task, rfcomm_tty_wakeup, (unsigned long) dev);
 
+       skb_queue_head_init(&dev->pending);
+
        rfcomm_dlc_lock(dlc);
+
+       if (req->flags & (1 << RFCOMM_REUSE_DLC)) {
+               struct sock *sk = dlc->owner;
+               struct sk_buff *skb;
+
+               BUG_ON(!sk);
+
+               rfcomm_dlc_throttle(dlc);
+
+               while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
+                       skb_orphan(skb);
+                       skb_queue_tail(&dev->pending, skb);
+                       atomic_sub(skb->len, &sk->sk_rmem_alloc);
+               }
+       }
+
        dlc->data_ready   = rfcomm_dev_data_ready;
        dlc->state_change = rfcomm_dev_state_change;
        dlc->modem_status = rfcomm_dev_modem_status;
 
        dlc->owner = dev;
        dev->dlc   = dlc;
+
+       rfcomm_dev_modem_status(dlc, dlc->remote_v24_sig);
+
        rfcomm_dlc_unlock(dlc);
 
-       /* It's safe to call __module_get() here because socket already 
+       /* It's safe to call __module_get() here because socket already
           holds reference to this module. */
        __module_get(THIS_MODULE);
 
 out:
        write_unlock_bh(&rfcomm_dev_lock);
 
-       if (err) {
-               kfree(dev);
-               return err;
+       if (err < 0)
+               goto free;
+
+       dev->tty_dev = tty_register_device(rfcomm_tty_driver, dev->id, NULL);
+
+       if (IS_ERR(dev->tty_dev)) {
+               err = PTR_ERR(dev->tty_dev);
+               list_del(&dev->list);
+               goto free;
        }
 
-       tty_register_device(rfcomm_tty_driver, dev->id, NULL);
+       dev_set_drvdata(dev->tty_dev, dev);
+
+       if (device_create_file(dev->tty_dev, &dev_attr_address) < 0)
+               BT_ERR("Failed to create address attribute");
+
+       if (device_create_file(dev->tty_dev, &dev_attr_channel) < 0)
+               BT_ERR("Failed to create channel attribute");
 
        return dev->id;
+
+free:
+       kfree(dev);
+       return err;
 }
 
 static void rfcomm_dev_del(struct rfcomm_dev *dev)
 {
        BT_DBG("dev %p", dev);
 
+       BUG_ON(test_and_set_bit(RFCOMM_TTY_RELEASED, &dev->flags));
+
+       if (atomic_read(&dev->opened) > 0)
+               return;
+
        write_lock_bh(&rfcomm_dev_lock);
        list_del_init(&dev->list);
        write_unlock_bh(&rfcomm_dev_lock);
@@ -286,7 +365,7 @@ static inline void rfcomm_set_owner_w(struct sk_buff *skb, struct rfcomm_dev *de
        skb->destructor = rfcomm_wfree;
 }
 
-static struct sk_buff *rfcomm_wmalloc(struct rfcomm_dev *dev, unsigned long size, int priority)
+static struct sk_buff *rfcomm_wmalloc(struct rfcomm_dev *dev, unsigned long size, gfp_t priority)
 {
        if (atomic_read(&dev->wmem_alloc) < rfcomm_room(dev->dlc)) {
                struct sk_buff *skb = alloc_skb(size, priority);
@@ -311,7 +390,7 @@ static int rfcomm_create_dev(struct sock *sk, void __user *arg)
        if (copy_from_user(&req, arg, sizeof(req)))
                return -EFAULT;
 
-       BT_DBG("sk %p dev_id %id flags 0x%x", sk, req.dev_id, req.flags);
+       BT_DBG("sk %p dev_id %d flags 0x%x", sk, req.dev_id, req.flags);
 
        if (req.flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN))
                return -EPERM;
@@ -352,7 +431,7 @@ static int rfcomm_release_dev(void __user *arg)
        if (copy_from_user(&req, arg, sizeof(req)))
                return -EFAULT;
 
-       BT_DBG("dev_id %id flags 0x%x", req.dev_id, req.flags);
+       BT_DBG("dev_id %d flags 0x%x", req.dev_id, req.flags);
 
        if (!(dev = rfcomm_dev_get(req.dev_id)))
                return -ENODEV;
@@ -365,7 +444,12 @@ static int rfcomm_release_dev(void __user *arg)
        if (req.flags & (1 << RFCOMM_HANGUP_NOW))
                rfcomm_dlc_close(dev->dlc, 0);
 
-       rfcomm_dev_del(dev);
+       /* Shut down TTY synchronously before freeing rfcomm_dev */
+       if (dev->tty)
+               tty_vhangup(dev->tty);
+
+       if (!test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags))
+               rfcomm_dev_del(dev);
        rfcomm_dev_put(dev);
        return 0;
 }
@@ -397,6 +481,8 @@ static int rfcomm_get_dev_list(void __user *arg)
 
        list_for_each(p, &rfcomm_dev_list) {
                struct rfcomm_dev *dev = list_entry(p, struct rfcomm_dev, list);
+               if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags))
+                       continue;
                (di + n)->id      = dev->id;
                (di + n)->flags   = dev->flags;
                (di + n)->state   = dev->dlc->state;
@@ -471,25 +557,21 @@ static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb)
 {
        struct rfcomm_dev *dev = dlc->owner;
        struct tty_struct *tty;
-       
-       if (!dev || !(tty = dev->tty)) {
+
+       if (!dev) {
                kfree_skb(skb);
                return;
        }
 
-       BT_DBG("dlc %p tty %p len %d", dlc, tty, skb->len);
+       if (!(tty = dev->tty) || !skb_queue_empty(&dev->pending)) {
+               skb_queue_tail(&dev->pending, skb);
+               return;
+       }
 
-       if (test_bit(TTY_DONT_FLIP, &tty->flags)) {
-               register int i;
-               for (i = 0; i < skb->len; i++) {
-                       if (tty->flip.count >= TTY_FLIPBUF_SIZE)
-                               tty_flip_buffer_push(tty);
+       BT_DBG("dlc %p tty %p len %d", dlc, tty, skb->len);
 
-                       tty_insert_flip_char(tty, skb->data[i], 0);
-               }
-               tty_flip_buffer_push(tty);
-       } else
-               tty->ldisc.receive_buf(tty, skb->data, NULL, skb->len);
+       tty_insert_flip_string(tty, skb->data, skb->len);
+       tty_flip_buffer_push(tty);
 
        kfree_skb(skb);
 }
@@ -499,7 +581,7 @@ static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
        struct rfcomm_dev *dev = dlc->owner;
        if (!dev)
                return;
-       
+
        BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
 
        dev->err = err;
@@ -508,17 +590,24 @@ static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
        if (dlc->state == BT_CLOSED) {
                if (!dev->tty) {
                        if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
-                               rfcomm_dev_hold(dev);
-                               rfcomm_dev_del(dev);
-
-                               /* We have to drop DLC lock here, otherwise
-                                  rfcomm_dev_put() will dead lock if it's
-                                  the last reference. */
+                               /* Drop DLC lock here to avoid deadlock
+                                * 1. rfcomm_dev_get will take rfcomm_dev_lock
+                                *    but in rfcomm_dev_add there's lock order:
+                                *    rfcomm_dev_lock -> dlc lock
+                                * 2. rfcomm_dev_put will deadlock if it's
+                                *    the last reference
+                                */
                                rfcomm_dlc_unlock(dlc);
+                               if (rfcomm_dev_get(dev->id) == NULL) {
+                                       rfcomm_dlc_lock(dlc);
+                                       return;
+                               }
+
+                               rfcomm_dev_del(dev);
                                rfcomm_dev_put(dev);
                                rfcomm_dlc_lock(dlc);
                        }
-               } else 
+               } else
                        tty_hangup(dev->tty);
        }
 }
@@ -536,7 +625,7 @@ static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig)
                        tty_hangup(dev->tty);
        }
 
-       dev->modem_status = 
+       dev->modem_status =
                ((v24_sig & RFCOMM_V24_RTC) ? (TIOCM_DSR | TIOCM_DTR) : 0) |
                ((v24_sig & RFCOMM_V24_RTR) ? (TIOCM_RTS | TIOCM_CTS) : 0) |
                ((v24_sig & RFCOMM_V24_IC)  ? TIOCM_RI : 0) |
@@ -552,14 +641,31 @@ static void rfcomm_tty_wakeup(unsigned long arg)
                return;
 
        BT_DBG("dev %p tty %p", dev, tty);
+       tty_wakeup(tty);
+}
+
+static void rfcomm_tty_copy_pending(struct rfcomm_dev *dev)
+{
+       struct tty_struct *tty = dev->tty;
+       struct sk_buff *skb;
+       int inserted = 0;
+
+       if (!tty)
+               return;
+
+       BT_DBG("dev %p tty %p", dev, tty);
+
+       rfcomm_dlc_lock(dev->dlc);
+
+       while ((skb = skb_dequeue(&dev->pending))) {
+               inserted += tty_insert_flip_string(tty, skb->data, skb->len);
+               kfree_skb(skb);
+       }
 
-       if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && tty->ldisc.write_wakeup)
-                (tty->ldisc.write_wakeup)(tty);
+       rfcomm_dlc_unlock(dev->dlc);
 
-       wake_up_interruptible(&tty->write_wait);
-#ifdef SERIAL_HAVE_POLL_WAIT
-       wake_up_interruptible(&tty->poll_wait);
-#endif
+       if (inserted > 0)
+               tty_flip_buffer_push(tty);
 }
 
 static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
@@ -569,7 +675,7 @@ static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
        struct rfcomm_dlc *dlc;
        int err, id;
 
-        id = tty->index;
+       id = tty->index;
 
        BT_DBG("tty %p id %d", tty, id);
 
@@ -581,9 +687,10 @@ static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
        if (!dev)
                return -ENODEV;
 
-       BT_DBG("dev %p dst %s channel %d opened %d", dev, batostr(&dev->dst), dev->channel, dev->opened);
+       BT_DBG("dev %p dst %s channel %d opened %d", dev, batostr(&dev->dst),
+                               dev->channel, atomic_read(&dev->opened));
 
-       if (dev->opened++ != 0)
+       if (atomic_inc_return(&dev->opened) > 1)
                return 0;
 
        dlc = dev->dlc;
@@ -623,6 +730,14 @@ static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
        set_current_state(TASK_RUNNING);
        remove_wait_queue(&dev->wait, &wait);
 
+       if (err == 0)
+               device_move(dev->tty_dev, rfcomm_get_device(dev),
+                           DPM_ORDER_DEV_AFTER_PARENT);
+
+       rfcomm_tty_copy_pending(dev);
+
+       rfcomm_dlc_unthrottle(dev->dlc);
+
        return err;
 }
 
@@ -632,9 +747,13 @@ static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
        if (!dev)
                return;
 
-       BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc, dev->opened);
+       BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc,
+                                               atomic_read(&dev->opened));
+
+       if (atomic_dec_and_test(&dev->opened)) {
+               if (dev->tty_dev->parent)
+                       device_move(dev->tty_dev, NULL, DPM_ORDER_DEV_LAST);
 
-       if (--dev->opened == 0) {
                /* Close DLC and dettach TTY */
                rfcomm_dlc_close(dev->dlc, 0);
 
@@ -645,6 +764,14 @@ static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
                tty->driver_data = NULL;
                dev->tty = NULL;
                rfcomm_dlc_unlock(dev->dlc);
+
+               if (test_bit(RFCOMM_TTY_RELEASED, &dev->flags)) {
+                       write_lock_bh(&rfcomm_dev_lock);
+                       list_del_init(&dev->list);
+                       write_unlock_bh(&rfcomm_dev_lock);
+
+                       rfcomm_dev_put(dev);
+               }
        }
 
        rfcomm_dev_put(dev);
@@ -663,7 +790,7 @@ static int rfcomm_tty_write(struct tty_struct *tty, const unsigned char *buf, in
                size = min_t(uint, count, dlc->mtu);
 
                skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, GFP_ATOMIC);
-               
+
                if (!skb)
                        break;
 
@@ -690,9 +817,13 @@ static int rfcomm_tty_write_room(struct tty_struct *tty)
 
        BT_DBG("tty %p", tty);
 
+       if (!dev || !dev->dlc)
+               return 0;
+
        room = rfcomm_room(dev->dlc) - atomic_read(&dev->wmem_alloc);
        if (room < 0)
                room = 0;
+
        return room;
 }
 
@@ -745,9 +876,9 @@ static int rfcomm_tty_ioctl(struct tty_struct *tty, struct file *filp, unsigned
        return -ENOIOCTLCMD;
 }
 
-static void rfcomm_tty_set_termios(struct tty_struct *tty, struct termios *old)
+static void rfcomm_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
 {
-       struct termios *new = (struct termios *) tty->termios;
+       struct ktermios *new = tty->termios;
        int old_baud_rate = tty_termios_baud_rate(old);
        int new_baud_rate = tty_termios_baud_rate(new);
 
@@ -758,8 +889,11 @@ static void rfcomm_tty_set_termios(struct tty_struct *tty, struct termios *old)
 
        BT_DBG("tty %p termios %p", tty, old);
 
+       if (!dev || !dev->dlc || !dev->dlc->session)
+               return;
+
        /* Handle turning off CRTSCTS */
-       if ((old->c_cflag & CRTSCTS) && !(new->c_cflag & CRTSCTS)) 
+       if ((old->c_cflag & CRTSCTS) && !(new->c_cflag & CRTSCTS))
                BT_DBG("Turning off CRTSCTS unsupported");
 
        /* Parity on/off and when on, odd/even */
@@ -816,7 +950,7 @@ static void rfcomm_tty_set_termios(struct tty_struct *tty, struct termios *old)
        }
 
        /* Handle number of data bits [5-8] */
-       if ((old->c_cflag & CSIZE) != (new->c_cflag & CSIZE)) 
+       if ((old->c_cflag & CSIZE) != (new->c_cflag & CSIZE))
                changes |= RFCOMM_RPN_PM_DATA;
 
        switch (new->c_cflag & CSIZE) {
@@ -854,7 +988,7 @@ static void rfcomm_tty_set_termios(struct tty_struct *tty, struct termios *old)
        case 9600:
                baud = RFCOMM_RPN_BR_9600;
                break;
-       case 19200: 
+       case 19200:
                baud = RFCOMM_RPN_BR_19200;
                break;
        case 38400:
@@ -873,7 +1007,7 @@ static void rfcomm_tty_set_termios(struct tty_struct *tty, struct termios *old)
                /* 9600 is standard accordinag to the RFCOMM specification */
                baud = RFCOMM_RPN_BR_9600;
                break;
-       
+
        }
 
        if (changes)
@@ -905,12 +1039,14 @@ static void rfcomm_tty_unthrottle(struct tty_struct *tty)
 static int rfcomm_tty_chars_in_buffer(struct tty_struct *tty)
 {
        struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
-       struct rfcomm_dlc *dlc = dev->dlc;
 
        BT_DBG("tty %p dev %p", tty, dev);
 
-       if (!skb_queue_empty(&dlc->tx_queue))
-               return dlc->mtu;
+       if (!dev || !dev->dlc)
+               return 0;
+
+       if (!skb_queue_empty(&dev->dlc->tx_queue))
+               return dev->dlc->mtu;
 
        return 0;
 }
@@ -918,15 +1054,14 @@ static int rfcomm_tty_chars_in_buffer(struct tty_struct *tty)
 static void rfcomm_tty_flush_buffer(struct tty_struct *tty)
 {
        struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
-       if (!dev)
-               return;
 
        BT_DBG("tty %p dev %p", tty, dev);
 
-       skb_queue_purge(&dev->dlc->tx_queue);
+       if (!dev || !dev->dlc)
+               return;
 
-       if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && tty->ldisc.write_wakeup)
-               tty->ldisc.write_wakeup(tty);
+       skb_queue_purge(&dev->dlc->tx_queue);
+       tty_wakeup(tty);
 }
 
 static void rfcomm_tty_send_xchar(struct tty_struct *tty, char ch)
@@ -942,29 +1077,29 @@ static void rfcomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
 static void rfcomm_tty_hangup(struct tty_struct *tty)
 {
        struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
-       if (!dev)
-               return;
 
        BT_DBG("tty %p dev %p", tty, dev);
 
+       if (!dev)
+               return;
+
        rfcomm_tty_flush_buffer(tty);
 
-       if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags))
+       if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
+               if (rfcomm_dev_get(dev->id) == NULL)
+                       return;
                rfcomm_dev_del(dev);
-}
-
-static int rfcomm_tty_read_proc(char *buf, char **start, off_t offset, int len, int *eof, void *unused)
-{
-       return 0;
+               rfcomm_dev_put(dev);
+       }
 }
 
 static int rfcomm_tty_tiocmget(struct tty_struct *tty, struct file *filp)
 {
-       struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
+       struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
 
        BT_DBG("tty %p dev %p", tty, dev);
 
-       return dev->modem_status;
+       return dev->modem_status;
 }
 
 static int rfcomm_tty_tiocmset(struct tty_struct *tty, struct file *filp, unsigned int set, unsigned int clear)
@@ -1002,7 +1137,7 @@ static int rfcomm_tty_tiocmset(struct tty_struct *tty, struct file *filp, unsign
 
 /* ---- TTY structure ---- */
 
-static struct tty_operations rfcomm_ops = {
+static const struct tty_operations rfcomm_ops = {
        .open                   = rfcomm_tty_open,
        .close                  = rfcomm_tty_close,
        .write                  = rfcomm_tty_write,
@@ -1016,7 +1151,6 @@ static struct tty_operations rfcomm_ops = {
        .send_xchar             = rfcomm_tty_send_xchar,
        .hangup                 = rfcomm_tty_hangup,
        .wait_until_sent        = rfcomm_tty_wait_until_sent,
-       .read_proc              = rfcomm_tty_read_proc,
        .tiocmget               = rfcomm_tty_tiocmget,
        .tiocmset               = rfcomm_tty_tiocmset,
 };
@@ -1029,15 +1163,15 @@ int rfcomm_init_ttys(void)
 
        rfcomm_tty_driver->owner        = THIS_MODULE;
        rfcomm_tty_driver->driver_name  = "rfcomm";
-       rfcomm_tty_driver->devfs_name   = "bluetooth/rfcomm/";
        rfcomm_tty_driver->name         = "rfcomm";
        rfcomm_tty_driver->major        = RFCOMM_TTY_MAJOR;
        rfcomm_tty_driver->minor_start  = RFCOMM_TTY_MINOR;
        rfcomm_tty_driver->type         = TTY_DRIVER_TYPE_SERIAL;
        rfcomm_tty_driver->subtype      = SERIAL_TYPE_NORMAL;
-       rfcomm_tty_driver->flags        = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
+       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_lflag &= ~ICANON;
        tty_set_operations(rfcomm_tty_driver, &rfcomm_ops);
 
        if (tty_register_driver(rfcomm_tty_driver)) {