Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / usb / misc / usbtest.c
index 3104f28..983e104 100644 (file)
@@ -1,7 +1,3 @@
-#include <linux/config.h>
-#if !defined (DEBUG) && defined (CONFIG_USB_DEBUG)
-#   define DEBUG
-#endif
 #include <linux/kernel.h>
 #include <linux/errno.h>
 #include <linux/init.h>
@@ -9,7 +5,7 @@
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/moduleparam.h>
-#include <asm/scatterlist.h>
+#include <linux/scatterlist.h>
 
 #include <linux/usb.h>
 
@@ -381,22 +377,29 @@ alloc_sglist (int nents, int max, int vary)
        sg = kmalloc (nents * sizeof *sg, SLAB_KERNEL);
        if (!sg)
                return NULL;
-       memset (sg, 0, nents * sizeof *sg);
 
        for (i = 0; i < nents; i++) {
                char            *buf;
+               unsigned        j;
 
-               buf = kmalloc (size, SLAB_KERNEL);
+               buf = kzalloc (size, SLAB_KERNEL);
                if (!buf) {
                        free_sglist (sg, i);
                        return NULL;
                }
-               memset (buf, 0, size);
 
                /* kmalloc pages are always physically contiguous! */
-               sg [i].page = virt_to_page (buf);
-               sg [i].offset = offset_in_page (buf);
-               sg [i].length = size;
+               sg_init_one(&sg[i], buf, size);
+
+               switch (pattern) {
+               case 0:
+                       /* already zeroed */
+                       break;
+               case 1:
+                       for (j = 0; j < size; j++)
+                               *buf++ = (u8) (j % 63);
+                       break;
+               }
 
                if (vary) {
                        size += vary;
@@ -432,6 +435,8 @@ static int perform_sglist (
                usb_sg_wait (req);
                retval = req->status;
 
+               /* FIXME check resulting data pattern */
+
                /* FIXME if endpoint halted, clear halt (and log) */
        }
 
@@ -461,7 +466,7 @@ static int perform_sglist (
 
 static unsigned realworld = 1;
 module_param (realworld, uint, 0);
-MODULE_PARM_DESC (realworld, "clear to demand stricter ch9 compliance");
+MODULE_PARM_DESC (realworld, "clear to demand stricter spec compliance");
 
 static int get_altsetting (struct usbtest_dev *dev)
 {
@@ -604,9 +609,8 @@ static int ch9_postconfig (struct usbtest_dev *dev)
                                USB_DIR_IN | USB_RECIP_DEVICE,
                                0, 0, dev->buf, 1, USB_CTRL_GET_TIMEOUT);
                if (retval != 1 || dev->buf [0] != expected) {
-                       dev_dbg (&iface->dev,
-                               "get config --> %d (%d)\n", retval,
-                               expected);
+                       dev_dbg (&iface->dev, "get config --> %d %d (1 %d)\n",
+                               retval, dev->buf[0], expected);
                        return (retval < 0) ? retval : -EDOM;
                }
        }
@@ -797,7 +801,9 @@ error:
 
                                if (u == urb || !u->dev)
                                        continue;
+                               spin_unlock(&ctx->lock);
                                status = usb_unlink_urb (u);
+                               spin_lock(&ctx->lock);
                                switch (status) {
                                case -EINPROGRESS:
                                case -EBUSY:
@@ -849,10 +855,9 @@ test_ctrl_queue (struct usbtest_dev *dev, struct usbtest_param *param)
         * as with bulk/intr sglists, sglen is the queue depth; it also
         * controls which subtests run (more tests than sglen) or rerun.
         */
-       urb = kmalloc (param->sglen * sizeof (struct urb *), SLAB_KERNEL);
+       urb = kcalloc(param->sglen, sizeof(struct urb *), SLAB_KERNEL);
        if (!urb)
                return -ENOMEM;
-       memset (urb, 0, param->sglen * sizeof (struct urb *));
        for (i = 0; i < param->sglen; i++) {
                int                     pipe = usb_rcvctrlpipe (udev, 0);
                unsigned                len;
@@ -984,10 +989,10 @@ test_ctrl_queue (struct usbtest_dev *dev, struct usbtest_param *param)
                reqp->number = i % NUM_SUBCASES;
                reqp->expected = expected;
                u->setup_packet = (char *) &reqp->setup;
+               u->transfer_flags |= URB_NO_SETUP_DMA_MAP;
 
                u->context = &context;
                u->complete = ctrl_complete;
-               u->transfer_flags |= URB_ASYNC_UNLINK;
        }
 
        /* queue the urbs */
@@ -1053,7 +1058,6 @@ static int unlink1 (struct usbtest_dev *dev, int pipe, int size, int async)
        urb = simple_alloc_urb (testdev_to_usbdev (dev), pipe, size);
        if (!urb)
                return -ENOMEM;
-       urb->transfer_flags |= URB_ASYNC_UNLINK;
        urb->context = &completion;
        urb->complete = unlink1_callback;
 
@@ -1238,12 +1242,13 @@ done:
 static int ctrl_out (struct usbtest_dev *dev,
                unsigned count, unsigned length, unsigned vary)
 {
-       unsigned                i, j, len, retval;
+       unsigned                i, j, len;
+       int                     retval;
        u8                      *buf;
        char                    *what = "?";
        struct usb_device       *udev;
-       
-       if (length > 0xffff || vary >= length)
+
+       if (length < 1 || length > 0xffff || vary >= length)
                return -EINVAL;
 
        buf = kmalloc(length, SLAB_KERNEL);
@@ -1266,6 +1271,11 @@ static int ctrl_out (struct usbtest_dev *dev,
                                0, 0, buf, len, USB_CTRL_SET_TIMEOUT);
                if (retval != len) {
                        what = "write";
+                       if (retval >= 0) {
+                               INFO(dev, "ctrl_out, wlen %d (expected %d)\n",
+                                               retval, len);
+                               retval = -EBADMSG;
+                       }
                        break;
                }
 
@@ -1275,6 +1285,11 @@ static int ctrl_out (struct usbtest_dev *dev,
                                0, 0, buf, len, USB_CTRL_GET_TIMEOUT);
                if (retval != len) {
                        what = "read";
+                       if (retval >= 0) {
+                               INFO(dev, "ctrl_out, rlen %d (expected %d)\n",
+                                               retval, len);
+                               retval = -EBADMSG;
+                       }
                        break;
                }
 
@@ -1293,8 +1308,13 @@ static int ctrl_out (struct usbtest_dev *dev,
                }
 
                len += vary;
+
+               /* [real world] the "zero bytes IN" case isn't really used.
+                * hardware can easily trip up in this wierd case, since its
+                * status stage is IN, not OUT like other ep0in transfers.
+                */
                if (len > length)
-                       len = 0;
+                       len = realworld ? 1 : 0;
        }
 
        if (retval < 0)
@@ -1317,7 +1337,9 @@ struct iso_context {
        unsigned                pending;
        spinlock_t              lock;
        struct completion       done;
+       int                     submit_error;
        unsigned long           errors;
+       unsigned long           packet_count;
        struct usbtest_dev      *dev;
 };
 
@@ -1328,10 +1350,14 @@ static void iso_callback (struct urb *urb, struct pt_regs *regs)
        spin_lock(&ctx->lock);
        ctx->count--;
 
+       ctx->packet_count += urb->number_of_packets;
        if (urb->error_count > 0)
                ctx->errors += urb->error_count;
+       else if (urb->status != 0)
+               ctx->errors += urb->number_of_packets;
 
-       if (urb->status == 0 && ctx->count > (ctx->pending - 1)) {
+       if (urb->status == 0 && ctx->count > (ctx->pending - 1)
+                       && !ctx->submit_error) {
                int status = usb_submit_urb (urb, GFP_ATOMIC);
                switch (status) {
                case 0:
@@ -1342,6 +1368,8 @@ static void iso_callback (struct urb *urb, struct pt_regs *regs)
                                        status);
                        /* FALLTHROUGH */
                case -ENODEV:                   /* disconnected */
+               case -ESHUTDOWN:                /* endpoint disabled */
+                       ctx->submit_error = 1;
                        break;
                }
        }
@@ -1351,8 +1379,8 @@ static void iso_callback (struct urb *urb, struct pt_regs *regs)
        if (ctx->pending == 0) {
                if (ctx->errors)
                        dev_dbg (&ctx->dev->intf->dev,
-                               "iso test, %lu errors\n",
-                               ctx->errors);
+                               "iso test, %lu errors out of %lu\n",
+                               ctx->errors, ctx->packet_count);
                complete (&ctx->done);
        }
 done:
@@ -1413,15 +1441,14 @@ test_iso_queue (struct usbtest_dev *dev, struct usbtest_param *param,
        struct usb_device       *udev;
        unsigned                i;
        unsigned long           packets = 0;
-       int                     status;
+       int                     status = 0;
        struct urb              *urbs[10];      /* FIXME no limit */
 
        if (param->sglen > 10)
                return -EDOM;
 
+       memset(&context, 0, sizeof context);
        context.count = param->iterations * param->sglen;
-       context.pending = param->sglen;
-       context.errors = 0;
        context.dev = dev;
        init_completion (&context.done);
        spin_lock_init (&context.lock);
@@ -1453,6 +1480,7 @@ test_iso_queue (struct usbtest_dev *dev, struct usbtest_param *param,
 
        spin_lock_irq (&context.lock);
        for (i = 0; i < param->sglen; i++) {
+               ++context.pending;
                status = usb_submit_urb (urbs [i], SLAB_ATOMIC);
                if (status < 0) {
                        ERROR (dev, "submit iso[%d], error %d\n", i, status);
@@ -1463,12 +1491,26 @@ test_iso_queue (struct usbtest_dev *dev, struct usbtest_param *param,
 
                        simple_free_urb (urbs [i]);
                        context.pending--;
+                       context.submit_error = 1;
+                       break;
                }
        }
        spin_unlock_irq (&context.lock);
 
        wait_for_completion (&context.done);
-       return 0;
+
+       /*
+        * Isochronous transfers are expected to fail sometimes.  As an
+        * arbitrary limit, we will report an error if any submissions
+        * fail or if the transfer failure rate is > 10%.
+        */
+       if (status != 0)
+               ;
+       else if (context.submit_error)
+               status = -EACCES;
+       else if (context.errors > context.packet_count / 10)
+               status = -EIO;
+       return status;
 
 fail:
        for (i = 0; i < param->sglen; i++) {
@@ -1519,6 +1561,11 @@ usbtest_ioctl (struct usb_interface *intf, unsigned int code, void *buf)
        if (down_interruptible (&dev->sem))
                return -ERESTARTSYS;
 
+       if (intf->dev.power.power_state.event != PM_EVENT_ON) {
+               up (&dev->sem);
+               return -EHOSTUNREACH;
+       }
+
        /* some devices, like ez-usb default devices, need a non-default
         * altsetting to have any active endpoints.  some tests change
         * altsettings; force a default so most tests don't need to check.
@@ -1762,8 +1809,10 @@ usbtest_ioctl (struct usb_interface *intf, unsigned int code, void *buf)
        case 14:
                if (!dev->info->ctrl_out)
                        break;
-               dev_dbg (&intf->dev, "TEST 14:  %d ep0out, 0..%d vary %d\n",
-                               param->iterations, param->length, param->vary);
+               dev_dbg (&intf->dev, "TEST 14:  %d ep0out, %d..%d vary %d\n",
+                               param->iterations,
+                               realworld ? 1 : 0, param->length,
+                               param->vary);
                retval = ctrl_out (dev, param->iterations, 
                                param->length, param->vary);
                break;
@@ -1851,10 +1900,9 @@ usbtest_probe (struct usb_interface *intf, const struct usb_device_id *id)
        }
 #endif
 
-       dev = kmalloc (sizeof *dev, SLAB_KERNEL);
+       dev = kzalloc(sizeof(*dev), SLAB_KERNEL);
        if (!dev)
                return -ENOMEM;
-       memset (dev, 0, sizeof *dev);
        info = (struct usbtest_info *) id->driver_info;
        dev->info = info;
        init_MUTEX (&dev->sem);
@@ -1927,6 +1975,17 @@ usbtest_probe (struct usb_interface *intf, const struct usb_device_id *id)
        return 0;
 }
 
+static int usbtest_suspend (struct usb_interface *intf, pm_message_t message)
+{
+       return 0;
+}
+
+static int usbtest_resume (struct usb_interface *intf)
+{
+       return 0;
+}
+
+
 static void usbtest_disconnect (struct usb_interface *intf)
 {
        struct usbtest_dev      *dev = usb_get_intfdata (intf);
@@ -2109,12 +2168,13 @@ static struct usb_device_id id_table [] = {
 MODULE_DEVICE_TABLE (usb, id_table);
 
 static struct usb_driver usbtest_driver = {
-       .owner =        THIS_MODULE,
        .name =         "usbtest",
        .id_table =     id_table,
        .probe =        usbtest_probe,
        .ioctl =        usbtest_ioctl,
        .disconnect =   usbtest_disconnect,
+       .suspend =      usbtest_suspend,
+       .resume =       usbtest_resume,
 };
 
 /*-------------------------------------------------------------------------*/