Merge branches 'x86/acpi', 'x86/asm', 'x86/cpudetect', 'x86/crashdump', 'x86/debug...
[safe/jmp/linux-2.6] / drivers / usb / class / cdc-acm.c
index a9dd28f..326dd7f 100644 (file)
@@ -51,6 +51,7 @@
  */
 
 #undef DEBUG
+#undef VERBOSE_DEBUG
 
 #include <linux/kernel.h>
 #include <linux/errno.h>
@@ -70,6 +71,9 @@
 
 #include "cdc-acm.h"
 
+
+#define ACM_CLOSE_TIMEOUT      15      /* seconds to let writes drain */
+
 /*
  * Version Information
  */
@@ -85,6 +89,12 @@ static DEFINE_MUTEX(open_mutex);
 
 #define ACM_READY(acm) (acm && acm->dev && acm->used)
 
+#ifdef VERBOSE_DEBUG
+#define verbose        1
+#else
+#define verbose        0
+#endif
+
 /*
  * Functions for ACM control messages.
  */
@@ -136,25 +146,24 @@ static int acm_wb_alloc(struct acm *acm)
 static int acm_wb_is_avail(struct acm *acm)
 {
        int i, n;
+       unsigned long flags;
 
        n = ACM_NW;
+       spin_lock_irqsave(&acm->write_lock, flags);
        for (i = 0; i < ACM_NW; i++) {
                n -= acm->wb[i].use;
        }
+       spin_unlock_irqrestore(&acm->write_lock, flags);
        return n;
 }
 
 /*
- * Finish write.
+ * Finish write. Caller must hold acm->write_lock
  */
 static void acm_write_done(struct acm *acm, struct acm_wb *wb)
 {
-       unsigned long flags;
-
-       spin_lock_irqsave(&acm->write_lock, flags);
        wb->use = 0;
        acm->transmitting--;
-       spin_unlock_irqrestore(&acm->write_lock, flags);
 }
 
 /*
@@ -313,8 +322,8 @@ exit:
        usb_mark_last_busy(acm->dev);
        retval = usb_submit_urb (urb, GFP_ATOMIC);
        if (retval)
-               err ("%s - usb_submit_urb failed with result %d",
-                    __func__, retval);
+               dev_err(&urb->dev->dev, "%s - usb_submit_urb failed with "
+                       "result %d", __func__, retval);
 }
 
 /* data interface returns incoming bytes, or we got unthrottled */
@@ -467,22 +476,31 @@ urbs:
 /* data interface wrote those outgoing bytes */
 static void acm_write_bulk(struct urb *urb)
 {
-       struct acm *acm;
        struct acm_wb *wb = urb->context;
+       struct acm *acm = wb->instance;
+       unsigned long flags;
 
-       dbg("Entering acm_write_bulk with status %d", urb->status);
+       if (verbose || urb->status
+                       || (urb->actual_length != urb->transfer_buffer_length))
+               dev_dbg(&acm->data->dev, "tx %d/%d bytes -- > %d\n",
+                       urb->actual_length,
+                       urb->transfer_buffer_length,
+                       urb->status);
 
-       acm = wb->instance;
+       spin_lock_irqsave(&acm->write_lock, flags);
        acm_write_done(acm, wb);
+       spin_unlock_irqrestore(&acm->write_lock, flags);
        if (ACM_READY(acm))
                schedule_work(&acm->work);
+       else
+               wake_up_interruptible(&acm->drain_wait);
 }
 
 static void acm_softint(struct work_struct *work)
 {
        struct acm *acm = container_of(work, struct acm, work);
-       dbg("Entering acm_softint.");
-       
+
+       dev_vdbg(&acm->data->dev, "tx work\n");
        if (!ACM_READY(acm))
                return;
        tty_wakeup(acm->tty);
@@ -495,7 +513,7 @@ static void acm_waker(struct work_struct *waker)
 
        rv = usb_autopm_get_interface(acm->control);
        if (rv < 0) {
-               err("Autopm failure in %s", __func__);
+               dev_err(&acm->dev->dev, "Autopm failure in %s\n", __func__);
                return;
        }
        if (acm->delayed_wb) {
@@ -570,8 +588,8 @@ static int acm_tty_open(struct tty_struct *tty, struct file *filp)
        tasklet_schedule(&acm->urb_task);
 
 done:
-err_out:
        mutex_unlock(&acm->mutex);
+err_out:
        mutex_unlock(&open_mutex);
        return rv;
 
@@ -603,6 +621,8 @@ static void acm_tty_unregister(struct acm *acm)
        kfree(acm);
 }
 
+static int acm_tty_chars_in_buffer(struct tty_struct *tty);
+
 static void acm_tty_close(struct tty_struct *tty, struct file *filp)
 {
        struct acm *acm = tty->driver_data;
@@ -617,6 +637,13 @@ static void acm_tty_close(struct tty_struct *tty, struct file *filp)
                if (acm->dev) {
                        usb_autopm_get_interface(acm->control);
                        acm_set_control(acm, acm->ctrlout = 0);
+
+                       /* try letting the last writes drain naturally */
+                       wait_event_interruptible_timeout(acm->drain_wait,
+                                       (ACM_NW == acm_wb_is_avail(acm))
+                                               || !acm->dev,
+                                       ACM_CLOSE_TIMEOUT * HZ);
+
                        usb_kill_urb(acm->ctrlurb);
                        for (i = 0; i < ACM_NW; i++)
                                usb_kill_urb(acm->wb[i].urb);
@@ -821,9 +848,10 @@ static void acm_write_buffers_free(struct acm *acm)
 {
        int i;
        struct acm_wb *wb;
+       struct usb_device *usb_dev = interface_to_usbdev(acm->control);
 
        for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
-               usb_buffer_free(acm->dev, acm->writesize, wb->buf, wb->dmah);
+               usb_buffer_free(usb_dev, acm->writesize, wb->buf, wb->dmah);
        }
 }
 
@@ -896,7 +924,7 @@ static int acm_probe (struct usb_interface *intf,
        
        /* normal probing*/
        if (!buffer) {
-               err("Weird descriptor references\n");
+               dev_err(&intf->dev, "Weird descriptor references\n");
                return -EINVAL;
        }
 
@@ -906,21 +934,24 @@ static int acm_probe (struct usb_interface *intf,
                        buflen = intf->cur_altsetting->endpoint->extralen;
                        buffer = intf->cur_altsetting->endpoint->extra;
                } else {
-                       err("Zero length descriptor references\n");
+                       dev_err(&intf->dev,
+                               "Zero length descriptor references\n");
                        return -EINVAL;
                }
        }
 
        while (buflen > 0) {
                if (buffer [1] != USB_DT_CS_INTERFACE) {
-                       err("skipping garbage\n");
+                       dev_err(&intf->dev, "skipping garbage\n");
                        goto next_desc;
                }
 
                switch (buffer [2]) {
                        case USB_CDC_UNION_TYPE: /* we've found it */
                                if (union_header) {
-                                       err("More than one union descriptor, skipping ...");
+                                       dev_err(&intf->dev, "More than one "
+                                               "union descriptor, "
+                                               "skipping ...\n");
                                        goto next_desc;
                                }
                                union_header = (struct usb_cdc_union_desc *)
@@ -938,7 +969,9 @@ static int acm_probe (struct usb_interface *intf,
                                call_management_function = buffer[3];
                                call_interface_num = buffer[4];
                                if ((call_management_function & 3) != 3)
-                                       err("This device cannot do calls on its own. It is no modem.");
+                                       dev_err(&intf->dev, "This device "
+                                               "cannot do calls on its own. "
+                                               "It is no modem.\n");
                                break;
                        default:
                                /* there are LOTS more CDC descriptors that
@@ -1023,7 +1056,7 @@ skip_normal_probe:
        for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
 
        if (minor == ACM_TTY_MINORS) {
-               err("no more free acm devices");
+               dev_err(&intf->dev, "no more free acm devices\n");
                return -ENODEV;
        }
 
@@ -1047,6 +1080,7 @@ skip_normal_probe:
        acm->urb_task.data = (unsigned long) acm;
        INIT_WORK(&acm->work, acm_softint);
        INIT_WORK(&acm->waker, acm_waker);
+       init_waitqueue_head(&acm->drain_wait);
        spin_lock_init(&acm->throttle_lock);
        spin_lock_init(&acm->write_lock);
        spin_lock_init(&acm->read_lock);
@@ -1241,7 +1275,7 @@ static int acm_suspend(struct usb_interface *intf, pm_message_t message)
        struct acm *acm = usb_get_intfdata(intf);
        int cnt;
 
-       if (acm->dev->auto_pm) {
+       if (message.event & PM_EVENT_AUTO) {
                int b;
 
                spin_lock_irq(&acm->read_lock);
@@ -1315,6 +1349,9 @@ static struct usb_device_id acm_ids[] = {
        { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
        .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
        },
+       { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */
+       .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
+       },
        { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
        .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
        },
@@ -1333,6 +1370,12 @@ static struct usb_device_id acm_ids[] = {
        { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
        .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
        },
+       { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
+       .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
+       },
+       { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */
+       .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
+       },
 
        /* control interfaces with various AT-command sets */
        { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
@@ -1422,7 +1465,8 @@ static int __init acm_init(void)
                return retval;
        }
 
-       info(DRIVER_VERSION ":" DRIVER_DESC);
+       printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
+              DRIVER_DESC "\n");
 
        return 0;
 }