GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / usb / misc / usbtest.c
blob874a48cff53513f14e746d9dbc8123fe82f84052
1 #include <linux/kernel.h>
2 #include <linux/errno.h>
3 #include <linux/init.h>
4 #include <linux/slab.h>
5 #include <linux/mm.h>
6 #include <linux/module.h>
7 #include <linux/moduleparam.h>
8 #include <linux/scatterlist.h>
9 #include <linux/mutex.h>
11 #include <linux/usb.h>
14 /*-------------------------------------------------------------------------*/
17 struct usbtest_param {
18 // inputs
19 unsigned test_num; /* 0..(TEST_CASES-1) */
20 unsigned iterations;
21 unsigned length;
22 unsigned vary;
23 unsigned sglen;
25 // outputs
26 struct timeval duration;
28 #define USBTEST_REQUEST _IOWR('U', 100, struct usbtest_param)
30 /*-------------------------------------------------------------------------*/
32 #define GENERIC /* let probe() bind using module params */
34 /* Some devices that can be used for testing will have "real" drivers.
35 * Entries for those need to be enabled here by hand, after disabling
36 * that "real" driver.
38 //#define IBOT2 /* grab iBOT2 webcams */
39 //#define KEYSPAN_19Qi /* grab un-renumerated serial adapter */
41 /*-------------------------------------------------------------------------*/
43 struct usbtest_info {
44 const char *name;
45 u8 ep_in; /* bulk/intr source */
46 u8 ep_out; /* bulk/intr sink */
47 unsigned autoconf : 1;
48 unsigned ctrl_out : 1;
49 unsigned iso : 1; /* try iso in/out */
50 int alt;
53 /* this is accessed only through usbfs ioctl calls.
54 * one ioctl to issue a test ... one lock per device.
55 * tests create other threads if they need them.
56 * urbs and buffers are allocated dynamically,
57 * and data generated deterministically.
59 struct usbtest_dev {
60 struct usb_interface *intf;
61 struct usbtest_info *info;
62 int in_pipe;
63 int out_pipe;
64 int in_iso_pipe;
65 int out_iso_pipe;
66 struct usb_endpoint_descriptor *iso_in, *iso_out;
67 struct mutex lock;
69 #define TBUF_SIZE 256
70 u8 *buf;
73 static struct usb_device *testdev_to_usbdev (struct usbtest_dev *test)
75 return interface_to_usbdev (test->intf);
78 /* set up all urbs so they can be used with either bulk or interrupt */
79 #define INTERRUPT_RATE 1 /* msec/transfer */
81 #define ERROR(tdev, fmt, args...) \
82 dev_err(&(tdev)->intf->dev , fmt , ## args)
83 #define WARNING(tdev, fmt, args...) \
84 dev_warn(&(tdev)->intf->dev , fmt , ## args)
86 /*-------------------------------------------------------------------------*/
88 static int
89 get_endpoints (struct usbtest_dev *dev, struct usb_interface *intf)
91 int tmp;
92 struct usb_host_interface *alt;
93 struct usb_host_endpoint *in, *out;
94 struct usb_host_endpoint *iso_in, *iso_out;
95 struct usb_device *udev;
97 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
98 unsigned ep;
100 in = out = NULL;
101 iso_in = iso_out = NULL;
102 alt = intf->altsetting + tmp;
104 /* take the first altsetting with in-bulk + out-bulk;
105 * ignore other endpoints and altsetttings.
107 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
108 struct usb_host_endpoint *e;
110 e = alt->endpoint + ep;
111 switch (e->desc.bmAttributes) {
112 case USB_ENDPOINT_XFER_BULK:
113 break;
114 case USB_ENDPOINT_XFER_ISOC:
115 if (dev->info->iso)
116 goto try_iso;
117 // FALLTHROUGH
118 default:
119 continue;
121 if (usb_endpoint_dir_in(&e->desc)) {
122 if (!in)
123 in = e;
124 } else {
125 if (!out)
126 out = e;
128 continue;
129 try_iso:
130 if (usb_endpoint_dir_in(&e->desc)) {
131 if (!iso_in)
132 iso_in = e;
133 } else {
134 if (!iso_out)
135 iso_out = e;
138 if ((in && out) || iso_in || iso_out)
139 goto found;
141 return -EINVAL;
143 found:
144 udev = testdev_to_usbdev (dev);
145 if (alt->desc.bAlternateSetting != 0) {
146 tmp = usb_set_interface (udev,
147 alt->desc.bInterfaceNumber,
148 alt->desc.bAlternateSetting);
149 if (tmp < 0)
150 return tmp;
153 if (in) {
154 dev->in_pipe = usb_rcvbulkpipe (udev,
155 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
156 dev->out_pipe = usb_sndbulkpipe (udev,
157 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
159 if (iso_in) {
160 dev->iso_in = &iso_in->desc;
161 dev->in_iso_pipe = usb_rcvisocpipe (udev,
162 iso_in->desc.bEndpointAddress
163 & USB_ENDPOINT_NUMBER_MASK);
166 if (iso_out) {
167 dev->iso_out = &iso_out->desc;
168 dev->out_iso_pipe = usb_sndisocpipe (udev,
169 iso_out->desc.bEndpointAddress
170 & USB_ENDPOINT_NUMBER_MASK);
172 return 0;
175 /*-------------------------------------------------------------------------*/
177 /* Support for testing basic non-queued I/O streams.
179 * These just package urbs as requests that can be easily canceled.
180 * Each urb's data buffer is dynamically allocated; callers can fill
181 * them with non-zero test data (or test for it) when appropriate.
184 static void simple_callback (struct urb *urb)
186 complete(urb->context);
189 static struct urb *simple_alloc_urb (
190 struct usb_device *udev,
191 int pipe,
192 unsigned long bytes
195 struct urb *urb;
197 urb = usb_alloc_urb (0, GFP_KERNEL);
198 if (!urb)
199 return urb;
200 usb_fill_bulk_urb (urb, udev, pipe, NULL, bytes, simple_callback, NULL);
201 urb->interval = (udev->speed == USB_SPEED_HIGH)
202 ? (INTERRUPT_RATE << 3)
203 : INTERRUPT_RATE;
204 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
205 if (usb_pipein (pipe))
206 urb->transfer_flags |= URB_SHORT_NOT_OK;
207 urb->transfer_buffer = usb_alloc_coherent (udev, bytes, GFP_KERNEL,
208 &urb->transfer_dma);
209 if (!urb->transfer_buffer) {
210 usb_free_urb (urb);
211 urb = NULL;
212 } else
213 memset (urb->transfer_buffer, 0, bytes);
214 return urb;
217 static unsigned pattern = 0;
218 static unsigned mod_pattern;
219 module_param_named(pattern, mod_pattern, uint, S_IRUGO | S_IWUSR);
220 MODULE_PARM_DESC(mod_pattern, "i/o pattern (0 == zeroes)");
222 static inline void simple_fill_buf (struct urb *urb)
224 unsigned i;
225 u8 *buf = urb->transfer_buffer;
226 unsigned len = urb->transfer_buffer_length;
228 switch (pattern) {
229 default:
230 // FALLTHROUGH
231 case 0:
232 memset (buf, 0, len);
233 break;
234 case 1: /* mod63 */
235 for (i = 0; i < len; i++)
236 *buf++ = (u8) (i % 63);
237 break;
241 static inline int simple_check_buf(struct usbtest_dev *tdev, struct urb *urb)
243 unsigned i;
244 u8 expected;
245 u8 *buf = urb->transfer_buffer;
246 unsigned len = urb->actual_length;
248 for (i = 0; i < len; i++, buf++) {
249 switch (pattern) {
250 /* all-zeroes has no synchronization issues */
251 case 0:
252 expected = 0;
253 break;
254 /* mod63 stays in sync with short-terminated transfers,
255 * or otherwise when host and gadget agree on how large
256 * each usb transfer request should be. resync is done
257 * with set_interface or set_config.
259 case 1: /* mod63 */
260 expected = i % 63;
261 break;
262 /* always fail unsupported patterns */
263 default:
264 expected = !*buf;
265 break;
267 if (*buf == expected)
268 continue;
269 ERROR(tdev, "buf[%d] = %d (not %d)\n", i, *buf, expected);
270 return -EINVAL;
272 return 0;
275 static void simple_free_urb (struct urb *urb)
277 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
278 urb->transfer_buffer, urb->transfer_dma);
279 usb_free_urb (urb);
282 static int simple_io (
283 struct usbtest_dev *tdev,
284 struct urb *urb,
285 int iterations,
286 int vary,
287 int expected,
288 const char *label
291 struct usb_device *udev = urb->dev;
292 int max = urb->transfer_buffer_length;
293 struct completion completion;
294 int retval = 0;
296 urb->context = &completion;
297 while (retval == 0 && iterations-- > 0) {
298 init_completion (&completion);
299 if (usb_pipeout (urb->pipe))
300 simple_fill_buf (urb);
301 if ((retval = usb_submit_urb (urb, GFP_KERNEL)) != 0)
302 break;
304 /* NOTE: no timeouts; can't be broken out of by interrupt */
305 wait_for_completion (&completion);
306 retval = urb->status;
307 urb->dev = udev;
308 if (retval == 0 && usb_pipein (urb->pipe))
309 retval = simple_check_buf(tdev, urb);
311 if (vary) {
312 int len = urb->transfer_buffer_length;
314 len += vary;
315 len %= max;
316 if (len == 0)
317 len = (vary < max) ? vary : max;
318 urb->transfer_buffer_length = len;
322 urb->transfer_buffer_length = max;
324 if (expected != retval)
325 dev_err(&udev->dev,
326 "%s failed, iterations left %d, status %d (not %d)\n",
327 label, iterations, retval, expected);
328 return retval;
332 /*-------------------------------------------------------------------------*/
334 /* We use scatterlist primitives to test queued I/O.
335 * Yes, this also tests the scatterlist primitives.
338 static void free_sglist (struct scatterlist *sg, int nents)
340 unsigned i;
342 if (!sg)
343 return;
344 for (i = 0; i < nents; i++) {
345 if (!sg_page(&sg[i]))
346 continue;
347 kfree (sg_virt(&sg[i]));
349 kfree (sg);
352 static struct scatterlist *
353 alloc_sglist (int nents, int max, int vary)
355 struct scatterlist *sg;
356 unsigned i;
357 unsigned size = max;
359 sg = kmalloc (nents * sizeof *sg, GFP_KERNEL);
360 if (!sg)
361 return NULL;
362 sg_init_table(sg, nents);
364 for (i = 0; i < nents; i++) {
365 char *buf;
366 unsigned j;
368 buf = kzalloc (size, GFP_KERNEL);
369 if (!buf) {
370 free_sglist (sg, i);
371 return NULL;
374 /* kmalloc pages are always physically contiguous! */
375 sg_set_buf(&sg[i], buf, size);
377 switch (pattern) {
378 case 0:
379 /* already zeroed */
380 break;
381 case 1:
382 for (j = 0; j < size; j++)
383 *buf++ = (u8) (j % 63);
384 break;
387 if (vary) {
388 size += vary;
389 size %= max;
390 if (size == 0)
391 size = (vary < max) ? vary : max;
395 return sg;
398 static int perform_sglist (
399 struct usbtest_dev *tdev,
400 unsigned iterations,
401 int pipe,
402 struct usb_sg_request *req,
403 struct scatterlist *sg,
404 int nents
407 struct usb_device *udev = testdev_to_usbdev(tdev);
408 int retval = 0;
410 while (retval == 0 && iterations-- > 0) {
411 retval = usb_sg_init (req, udev, pipe,
412 (udev->speed == USB_SPEED_HIGH)
413 ? (INTERRUPT_RATE << 3)
414 : INTERRUPT_RATE,
415 sg, nents, 0, GFP_KERNEL);
417 if (retval)
418 break;
419 usb_sg_wait (req);
420 retval = req->status;
425 // failure if retval is as we expected ...
427 if (retval)
428 ERROR(tdev, "perform_sglist failed, "
429 "iterations left %d, status %d\n",
430 iterations, retval);
431 return retval;
435 /*-------------------------------------------------------------------------*/
437 /* unqueued control message testing
439 * there's a nice set of device functional requirements in chapter 9 of the
440 * usb 2.0 spec, which we can apply to ANY device, even ones that don't use
441 * special test firmware.
443 * we know the device is configured (or suspended) by the time it's visible
444 * through usbfs. we can't change that, so we won't test enumeration (which
445 * worked 'well enough' to get here, this time), power management (ditto),
446 * or remote wakeup (which needs human interaction).
449 static unsigned realworld = 1;
450 module_param (realworld, uint, 0);
451 MODULE_PARM_DESC (realworld, "clear to demand stricter spec compliance");
453 static int get_altsetting (struct usbtest_dev *dev)
455 struct usb_interface *iface = dev->intf;
456 struct usb_device *udev = interface_to_usbdev (iface);
457 int retval;
459 retval = usb_control_msg (udev, usb_rcvctrlpipe (udev, 0),
460 USB_REQ_GET_INTERFACE, USB_DIR_IN|USB_RECIP_INTERFACE,
461 0, iface->altsetting [0].desc.bInterfaceNumber,
462 dev->buf, 1, USB_CTRL_GET_TIMEOUT);
463 switch (retval) {
464 case 1:
465 return dev->buf [0];
466 case 0:
467 retval = -ERANGE;
468 // FALLTHROUGH
469 default:
470 return retval;
474 static int set_altsetting (struct usbtest_dev *dev, int alternate)
476 struct usb_interface *iface = dev->intf;
477 struct usb_device *udev;
479 if (alternate < 0 || alternate >= 256)
480 return -EINVAL;
482 udev = interface_to_usbdev (iface);
483 return usb_set_interface (udev,
484 iface->altsetting [0].desc.bInterfaceNumber,
485 alternate);
488 static int is_good_config(struct usbtest_dev *tdev, int len)
490 struct usb_config_descriptor *config;
492 if (len < sizeof *config)
493 return 0;
494 config = (struct usb_config_descriptor *) tdev->buf;
496 switch (config->bDescriptorType) {
497 case USB_DT_CONFIG:
498 case USB_DT_OTHER_SPEED_CONFIG:
499 if (config->bLength != 9) {
500 ERROR(tdev, "bogus config descriptor length\n");
501 return 0;
503 /* this bit 'must be 1' but often isn't */
504 if (!realworld && !(config->bmAttributes & 0x80)) {
505 ERROR(tdev, "high bit of config attributes not set\n");
506 return 0;
508 if (config->bmAttributes & 0x1f) { /* reserved == 0 */
509 ERROR(tdev, "reserved config bits set\n");
510 return 0;
512 break;
513 default:
514 return 0;
517 if (le16_to_cpu(config->wTotalLength) == len) /* read it all */
518 return 1;
519 if (le16_to_cpu(config->wTotalLength) >= TBUF_SIZE) /* max partial read */
520 return 1;
521 ERROR(tdev, "bogus config descriptor read size\n");
522 return 0;
525 /* sanity test for standard requests working with usb_control_mesg() and some
526 * of the utility functions which use it.
528 * this doesn't test how endpoint halts behave or data toggles get set, since
529 * we won't do I/O to bulk/interrupt endpoints here (which is how to change
530 * halt or toggle). toggle testing is impractical without support from hcds.
532 * this avoids failing devices linux would normally work with, by not testing
533 * config/altsetting operations for devices that only support their defaults.
534 * such devices rarely support those needless operations.
536 * NOTE that since this is a sanity test, it's not examining boundary cases
537 * to see if usbcore, hcd, and device all behave right. such testing would
538 * involve varied read sizes and other operation sequences.
540 static int ch9_postconfig (struct usbtest_dev *dev)
542 struct usb_interface *iface = dev->intf;
543 struct usb_device *udev = interface_to_usbdev (iface);
544 int i, alt, retval;
546 /* [9.2.3] if there's more than one altsetting, we need to be able to
547 * set and get each one. mostly trusts the descriptors from usbcore.
549 for (i = 0; i < iface->num_altsetting; i++) {
551 /* 9.2.3 constrains the range here */
552 alt = iface->altsetting [i].desc.bAlternateSetting;
553 if (alt < 0 || alt >= iface->num_altsetting) {
554 dev_err(&iface->dev,
555 "invalid alt [%d].bAltSetting = %d\n",
556 i, alt);
559 /* [real world] get/set unimplemented if there's only one */
560 if (realworld && iface->num_altsetting == 1)
561 continue;
563 /* [9.4.10] set_interface */
564 retval = set_altsetting (dev, alt);
565 if (retval) {
566 dev_err(&iface->dev, "can't set_interface = %d, %d\n",
567 alt, retval);
568 return retval;
571 /* [9.4.4] get_interface always works */
572 retval = get_altsetting (dev);
573 if (retval != alt) {
574 dev_err(&iface->dev, "get alt should be %d, was %d\n",
575 alt, retval);
576 return (retval < 0) ? retval : -EDOM;
581 /* [real world] get_config unimplemented if there's only one */
582 if (!realworld || udev->descriptor.bNumConfigurations != 1) {
583 int expected = udev->actconfig->desc.bConfigurationValue;
585 /* [9.4.2] get_configuration always works
586 * ... although some cheap devices (like one TI Hub I've got)
587 * won't return config descriptors except before set_config.
589 retval = usb_control_msg (udev, usb_rcvctrlpipe (udev, 0),
590 USB_REQ_GET_CONFIGURATION,
591 USB_DIR_IN | USB_RECIP_DEVICE,
592 0, 0, dev->buf, 1, USB_CTRL_GET_TIMEOUT);
593 if (retval != 1 || dev->buf [0] != expected) {
594 dev_err(&iface->dev, "get config --> %d %d (1 %d)\n",
595 retval, dev->buf[0], expected);
596 return (retval < 0) ? retval : -EDOM;
600 /* there's always [9.4.3] a device descriptor [9.6.1] */
601 retval = usb_get_descriptor (udev, USB_DT_DEVICE, 0,
602 dev->buf, sizeof udev->descriptor);
603 if (retval != sizeof udev->descriptor) {
604 dev_err(&iface->dev, "dev descriptor --> %d\n", retval);
605 return (retval < 0) ? retval : -EDOM;
608 /* there's always [9.4.3] at least one config descriptor [9.6.3] */
609 for (i = 0; i < udev->descriptor.bNumConfigurations; i++) {
610 retval = usb_get_descriptor (udev, USB_DT_CONFIG, i,
611 dev->buf, TBUF_SIZE);
612 if (!is_good_config(dev, retval)) {
613 dev_err(&iface->dev,
614 "config [%d] descriptor --> %d\n",
615 i, retval);
616 return (retval < 0) ? retval : -EDOM;
619 // parsed it right (etc) would be good testing paranoia
622 /* and sometimes [9.2.6.6] speed dependent descriptors */
623 if (le16_to_cpu(udev->descriptor.bcdUSB) == 0x0200) {
624 struct usb_qualifier_descriptor *d = NULL;
626 /* device qualifier [9.6.2] */
627 retval = usb_get_descriptor (udev,
628 USB_DT_DEVICE_QUALIFIER, 0, dev->buf,
629 sizeof (struct usb_qualifier_descriptor));
630 if (retval == -EPIPE) {
631 if (udev->speed == USB_SPEED_HIGH) {
632 dev_err(&iface->dev,
633 "hs dev qualifier --> %d\n",
634 retval);
635 return (retval < 0) ? retval : -EDOM;
637 /* usb2.0 but not high-speed capable; fine */
638 } else if (retval != sizeof (struct usb_qualifier_descriptor)) {
639 dev_err(&iface->dev, "dev qualifier --> %d\n", retval);
640 return (retval < 0) ? retval : -EDOM;
641 } else
642 d = (struct usb_qualifier_descriptor *) dev->buf;
644 /* might not have [9.6.2] any other-speed configs [9.6.4] */
645 if (d) {
646 unsigned max = d->bNumConfigurations;
647 for (i = 0; i < max; i++) {
648 retval = usb_get_descriptor (udev,
649 USB_DT_OTHER_SPEED_CONFIG, i,
650 dev->buf, TBUF_SIZE);
651 if (!is_good_config(dev, retval)) {
652 dev_err(&iface->dev,
653 "other speed config --> %d\n",
654 retval);
655 return (retval < 0) ? retval : -EDOM;
661 /* [9.4.5] get_status always works */
662 retval = usb_get_status (udev, USB_RECIP_DEVICE, 0, dev->buf);
663 if (retval != 2) {
664 dev_err(&iface->dev, "get dev status --> %d\n", retval);
665 return (retval < 0) ? retval : -EDOM;
668 // the device's remote wakeup feature ... if we can, test that here
670 retval = usb_get_status (udev, USB_RECIP_INTERFACE,
671 iface->altsetting [0].desc.bInterfaceNumber, dev->buf);
672 if (retval != 2) {
673 dev_err(&iface->dev, "get interface status --> %d\n", retval);
674 return (retval < 0) ? retval : -EDOM;
677 return 0;
680 /*-------------------------------------------------------------------------*/
682 /* use ch9 requests to test whether:
683 * (a) queues work for control, keeping N subtests queued and
684 * active (auto-resubmit) for M loops through the queue.
685 * (b) protocol stalls (control-only) will autorecover.
686 * it's not like bulk/intr; no halt clearing.
687 * (c) short control reads are reported and handled.
688 * (d) queues are always processed in-order
691 struct ctrl_ctx {
692 spinlock_t lock;
693 struct usbtest_dev *dev;
694 struct completion complete;
695 unsigned count;
696 unsigned pending;
697 int status;
698 struct urb **urb;
699 struct usbtest_param *param;
700 int last;
703 #define NUM_SUBCASES 15 /* how many test subcases here? */
705 struct subcase {
706 struct usb_ctrlrequest setup;
707 int number;
708 int expected;
711 static void ctrl_complete (struct urb *urb)
713 struct ctrl_ctx *ctx = urb->context;
714 struct usb_ctrlrequest *reqp;
715 struct subcase *subcase;
716 int status = urb->status;
718 reqp = (struct usb_ctrlrequest *)urb->setup_packet;
719 subcase = container_of (reqp, struct subcase, setup);
721 spin_lock (&ctx->lock);
722 ctx->count--;
723 ctx->pending--;
725 /* queue must transfer and complete in fifo order, unless
726 * usb_unlink_urb() is used to unlink something not at the
727 * physical queue head (not tested).
729 if (subcase->number > 0) {
730 if ((subcase->number - ctx->last) != 1) {
731 ERROR(ctx->dev,
732 "subcase %d completed out of order, last %d\n",
733 subcase->number, ctx->last);
734 status = -EDOM;
735 ctx->last = subcase->number;
736 goto error;
739 ctx->last = subcase->number;
741 /* succeed or fault in only one way? */
742 if (status == subcase->expected)
743 status = 0;
745 /* async unlink for cleanup? */
746 else if (status != -ECONNRESET) {
748 /* some faults are allowed, not required */
749 if (subcase->expected > 0 && (
750 ((status == -subcase->expected /* happened */
751 || status == 0)))) /* didn't */
752 status = 0;
753 /* sometimes more than one fault is allowed */
754 else if (subcase->number == 12 && status == -EPIPE)
755 status = 0;
756 else
757 ERROR(ctx->dev, "subtest %d error, status %d\n",
758 subcase->number, status);
761 /* unexpected status codes mean errors; ideally, in hardware */
762 if (status) {
763 error:
764 if (ctx->status == 0) {
765 int i;
767 ctx->status = status;
768 ERROR(ctx->dev, "control queue %02x.%02x, err %d, "
769 "%d left, subcase %d, len %d/%d\n",
770 reqp->bRequestType, reqp->bRequest,
771 status, ctx->count, subcase->number,
772 urb->actual_length,
773 urb->transfer_buffer_length);
776 /* unlink whatever's still pending */
777 for (i = 1; i < ctx->param->sglen; i++) {
778 struct urb *u = ctx->urb [
779 (i + subcase->number)
780 % ctx->param->sglen];
782 if (u == urb || !u->dev)
783 continue;
784 spin_unlock(&ctx->lock);
785 status = usb_unlink_urb (u);
786 spin_lock(&ctx->lock);
787 switch (status) {
788 case -EINPROGRESS:
789 case -EBUSY:
790 case -EIDRM:
791 continue;
792 default:
793 ERROR(ctx->dev, "urb unlink --> %d\n",
794 status);
797 status = ctx->status;
801 /* resubmit if we need to, else mark this as done */
802 if ((status == 0) && (ctx->pending < ctx->count)) {
803 if ((status = usb_submit_urb (urb, GFP_ATOMIC)) != 0) {
804 ERROR(ctx->dev,
805 "can't resubmit ctrl %02x.%02x, err %d\n",
806 reqp->bRequestType, reqp->bRequest, status);
807 urb->dev = NULL;
808 } else
809 ctx->pending++;
810 } else
811 urb->dev = NULL;
813 /* signal completion when nothing's queued */
814 if (ctx->pending == 0)
815 complete (&ctx->complete);
816 spin_unlock (&ctx->lock);
819 static int
820 test_ctrl_queue (struct usbtest_dev *dev, struct usbtest_param *param)
822 struct usb_device *udev = testdev_to_usbdev (dev);
823 struct urb **urb;
824 struct ctrl_ctx context;
825 int i;
827 spin_lock_init (&context.lock);
828 context.dev = dev;
829 init_completion (&context.complete);
830 context.count = param->sglen * param->iterations;
831 context.pending = 0;
832 context.status = -ENOMEM;
833 context.param = param;
834 context.last = -1;
836 /* allocate and init the urbs we'll queue.
837 * as with bulk/intr sglists, sglen is the queue depth; it also
838 * controls which subtests run (more tests than sglen) or rerun.
840 urb = kcalloc(param->sglen, sizeof(struct urb *), GFP_KERNEL);
841 if (!urb)
842 return -ENOMEM;
843 for (i = 0; i < param->sglen; i++) {
844 int pipe = usb_rcvctrlpipe (udev, 0);
845 unsigned len;
846 struct urb *u;
847 struct usb_ctrlrequest req;
848 struct subcase *reqp;
850 /* sign of this variable means:
851 * -: tested code must return this (negative) error code
852 * +: tested code may return this (negative too) error code
854 int expected = 0;
856 /* requests here are mostly expected to succeed on any
857 * device, but some are chosen to trigger protocol stalls
858 * or short reads.
860 memset (&req, 0, sizeof req);
861 req.bRequest = USB_REQ_GET_DESCRIPTOR;
862 req.bRequestType = USB_DIR_IN|USB_RECIP_DEVICE;
864 switch (i % NUM_SUBCASES) {
865 case 0: // get device descriptor
866 req.wValue = cpu_to_le16 (USB_DT_DEVICE << 8);
867 len = sizeof (struct usb_device_descriptor);
868 break;
869 case 1: // get first config descriptor (only)
870 req.wValue = cpu_to_le16 ((USB_DT_CONFIG << 8) | 0);
871 len = sizeof (struct usb_config_descriptor);
872 break;
873 case 2: // get altsetting (OFTEN STALLS)
874 req.bRequest = USB_REQ_GET_INTERFACE;
875 req.bRequestType = USB_DIR_IN|USB_RECIP_INTERFACE;
876 // index = 0 means first interface
877 len = 1;
878 expected = EPIPE;
879 break;
880 case 3: // get interface status
881 req.bRequest = USB_REQ_GET_STATUS;
882 req.bRequestType = USB_DIR_IN|USB_RECIP_INTERFACE;
883 // interface 0
884 len = 2;
885 break;
886 case 4: // get device status
887 req.bRequest = USB_REQ_GET_STATUS;
888 req.bRequestType = USB_DIR_IN|USB_RECIP_DEVICE;
889 len = 2;
890 break;
891 case 5: // get device qualifier (MAY STALL)
892 req.wValue = cpu_to_le16 (USB_DT_DEVICE_QUALIFIER << 8);
893 len = sizeof (struct usb_qualifier_descriptor);
894 if (udev->speed != USB_SPEED_HIGH)
895 expected = EPIPE;
896 break;
897 case 6: // get first config descriptor, plus interface
898 req.wValue = cpu_to_le16 ((USB_DT_CONFIG << 8) | 0);
899 len = sizeof (struct usb_config_descriptor);
900 len += sizeof (struct usb_interface_descriptor);
901 break;
902 case 7: // get interface descriptor (ALWAYS STALLS)
903 req.wValue = cpu_to_le16 (USB_DT_INTERFACE << 8);
904 // interface == 0
905 len = sizeof (struct usb_interface_descriptor);
906 expected = -EPIPE;
907 break;
908 // NOTE: two consecutive stalls in the queue here.
909 // that tests fault recovery a bit more aggressively.
910 case 8: // clear endpoint halt (MAY STALL)
911 req.bRequest = USB_REQ_CLEAR_FEATURE;
912 req.bRequestType = USB_RECIP_ENDPOINT;
913 // wValue 0 == ep halt
914 // wIndex 0 == ep0 (shouldn't halt!)
915 len = 0;
916 pipe = usb_sndctrlpipe (udev, 0);
917 expected = EPIPE;
918 break;
919 case 9: // get endpoint status
920 req.bRequest = USB_REQ_GET_STATUS;
921 req.bRequestType = USB_DIR_IN|USB_RECIP_ENDPOINT;
922 // endpoint 0
923 len = 2;
924 break;
925 case 10: // trigger short read (EREMOTEIO)
926 req.wValue = cpu_to_le16 ((USB_DT_CONFIG << 8) | 0);
927 len = 1024;
928 expected = -EREMOTEIO;
929 break;
930 // NOTE: two consecutive _different_ faults in the queue.
931 case 11: // get endpoint descriptor (ALWAYS STALLS)
932 req.wValue = cpu_to_le16 (USB_DT_ENDPOINT << 8);
933 // endpoint == 0
934 len = sizeof (struct usb_interface_descriptor);
935 expected = EPIPE;
936 break;
937 // NOTE: sometimes even a third fault in the queue!
938 case 12: // get string 0 descriptor (MAY STALL)
939 req.wValue = cpu_to_le16 (USB_DT_STRING << 8);
940 // string == 0, for language IDs
941 len = sizeof (struct usb_interface_descriptor);
942 // may succeed when > 4 languages
943 expected = EREMOTEIO; // or EPIPE, if no strings
944 break;
945 case 13: // short read, resembling case 10
946 req.wValue = cpu_to_le16 ((USB_DT_CONFIG << 8) | 0);
947 // last data packet "should" be DATA1, not DATA0
948 len = 1024 - udev->descriptor.bMaxPacketSize0;
949 expected = -EREMOTEIO;
950 break;
951 case 14: // short read; try to fill the last packet
952 req.wValue = cpu_to_le16 ((USB_DT_DEVICE << 8) | 0);
953 /* device descriptor size == 18 bytes */
954 len = udev->descriptor.bMaxPacketSize0;
955 switch (len) {
956 case 8: len = 24; break;
957 case 16: len = 32; break;
959 expected = -EREMOTEIO;
960 break;
961 default:
962 ERROR(dev, "bogus number of ctrl queue testcases!\n");
963 context.status = -EINVAL;
964 goto cleanup;
966 req.wLength = cpu_to_le16 (len);
967 urb [i] = u = simple_alloc_urb (udev, pipe, len);
968 if (!u)
969 goto cleanup;
971 reqp = kmalloc(sizeof *reqp, GFP_KERNEL);
972 if (!reqp)
973 goto cleanup;
974 reqp->setup = req;
975 reqp->number = i % NUM_SUBCASES;
976 reqp->expected = expected;
977 u->setup_packet = (char *) &reqp->setup;
979 u->context = &context;
980 u->complete = ctrl_complete;
983 /* queue the urbs */
984 context.urb = urb;
985 spin_lock_irq (&context.lock);
986 for (i = 0; i < param->sglen; i++) {
987 context.status = usb_submit_urb (urb [i], GFP_ATOMIC);
988 if (context.status != 0) {
989 ERROR(dev, "can't submit urb[%d], status %d\n",
990 i, context.status);
991 context.count = context.pending;
992 break;
994 context.pending++;
996 spin_unlock_irq (&context.lock);
999 /* wait for the last one to complete */
1000 if (context.pending > 0)
1001 wait_for_completion (&context.complete);
1003 cleanup:
1004 for (i = 0; i < param->sglen; i++) {
1005 if (!urb [i])
1006 continue;
1007 urb [i]->dev = udev;
1008 kfree(urb[i]->setup_packet);
1009 simple_free_urb (urb [i]);
1011 kfree (urb);
1012 return context.status;
1014 #undef NUM_SUBCASES
1017 /*-------------------------------------------------------------------------*/
1019 static void unlink1_callback (struct urb *urb)
1021 int status = urb->status;
1023 // we "know" -EPIPE (stall) never happens
1024 if (!status)
1025 status = usb_submit_urb (urb, GFP_ATOMIC);
1026 if (status) {
1027 urb->status = status;
1028 complete(urb->context);
1032 static int unlink1 (struct usbtest_dev *dev, int pipe, int size, int async)
1034 struct urb *urb;
1035 struct completion completion;
1036 int retval = 0;
1038 init_completion (&completion);
1039 urb = simple_alloc_urb (testdev_to_usbdev (dev), pipe, size);
1040 if (!urb)
1041 return -ENOMEM;
1042 urb->context = &completion;
1043 urb->complete = unlink1_callback;
1045 if ((retval = usb_submit_urb (urb, GFP_KERNEL)) != 0) {
1046 dev_err(&dev->intf->dev, "submit fail %d\n", retval);
1047 return retval;
1050 /* unlinking that should always work. variable delay tests more
1051 * hcd states and code paths, even with little other system load.
1053 msleep (jiffies % (2 * INTERRUPT_RATE));
1054 if (async) {
1055 while (!completion_done(&completion)) {
1056 retval = usb_unlink_urb(urb);
1058 switch (retval) {
1059 case -EBUSY:
1060 case -EIDRM:
1061 /* we can't unlink urbs while they're completing
1062 * or if they've completed, and we haven't
1063 * resubmitted. "normal" drivers would prevent
1064 * resubmission, but since we're testing unlink
1065 * paths, we can't.
1067 ERROR(dev, "unlink retry\n");
1068 continue;
1069 case 0:
1070 case -EINPROGRESS:
1071 break;
1073 default:
1074 dev_err(&dev->intf->dev,
1075 "unlink fail %d\n", retval);
1076 return retval;
1079 break;
1081 } else
1082 usb_kill_urb (urb);
1084 wait_for_completion (&completion);
1085 retval = urb->status;
1086 simple_free_urb (urb);
1088 if (async)
1089 return (retval == -ECONNRESET) ? 0 : retval - 1000;
1090 else
1091 return (retval == -ENOENT || retval == -EPERM) ?
1092 0 : retval - 2000;
1095 static int unlink_simple (struct usbtest_dev *dev, int pipe, int len)
1097 int retval = 0;
1099 /* test sync and async paths */
1100 retval = unlink1 (dev, pipe, len, 1);
1101 if (!retval)
1102 retval = unlink1 (dev, pipe, len, 0);
1103 return retval;
1106 /*-------------------------------------------------------------------------*/
1108 static int verify_not_halted(struct usbtest_dev *tdev, int ep, struct urb *urb)
1110 int retval;
1111 u16 status;
1113 /* shouldn't look or act halted */
1114 retval = usb_get_status (urb->dev, USB_RECIP_ENDPOINT, ep, &status);
1115 if (retval < 0) {
1116 ERROR(tdev, "ep %02x couldn't get no-halt status, %d\n",
1117 ep, retval);
1118 return retval;
1120 if (status != 0) {
1121 ERROR(tdev, "ep %02x bogus status: %04x != 0\n", ep, status);
1122 return -EINVAL;
1124 retval = simple_io(tdev, urb, 1, 0, 0, __func__);
1125 if (retval != 0)
1126 return -EINVAL;
1127 return 0;
1130 static int verify_halted(struct usbtest_dev *tdev, int ep, struct urb *urb)
1132 int retval;
1133 u16 status;
1135 /* should look and act halted */
1136 retval = usb_get_status (urb->dev, USB_RECIP_ENDPOINT, ep, &status);
1137 if (retval < 0) {
1138 ERROR(tdev, "ep %02x couldn't get halt status, %d\n",
1139 ep, retval);
1140 return retval;
1142 le16_to_cpus(&status);
1143 if (status != 1) {
1144 ERROR(tdev, "ep %02x bogus status: %04x != 1\n", ep, status);
1145 return -EINVAL;
1147 retval = simple_io(tdev, urb, 1, 0, -EPIPE, __func__);
1148 if (retval != -EPIPE)
1149 return -EINVAL;
1150 retval = simple_io(tdev, urb, 1, 0, -EPIPE, "verify_still_halted");
1151 if (retval != -EPIPE)
1152 return -EINVAL;
1153 return 0;
1156 static int test_halt(struct usbtest_dev *tdev, int ep, struct urb *urb)
1158 int retval;
1160 /* shouldn't look or act halted now */
1161 retval = verify_not_halted(tdev, ep, urb);
1162 if (retval < 0)
1163 return retval;
1165 /* set halt (protocol test only), verify it worked */
1166 retval = usb_control_msg (urb->dev, usb_sndctrlpipe (urb->dev, 0),
1167 USB_REQ_SET_FEATURE, USB_RECIP_ENDPOINT,
1168 USB_ENDPOINT_HALT, ep,
1169 NULL, 0, USB_CTRL_SET_TIMEOUT);
1170 if (retval < 0) {
1171 ERROR(tdev, "ep %02x couldn't set halt, %d\n", ep, retval);
1172 return retval;
1174 retval = verify_halted(tdev, ep, urb);
1175 if (retval < 0)
1176 return retval;
1178 /* clear halt (tests API + protocol), verify it worked */
1179 retval = usb_clear_halt (urb->dev, urb->pipe);
1180 if (retval < 0) {
1181 ERROR(tdev, "ep %02x couldn't clear halt, %d\n", ep, retval);
1182 return retval;
1184 retval = verify_not_halted(tdev, ep, urb);
1185 if (retval < 0)
1186 return retval;
1188 /* NOTE: could also verify SET_INTERFACE clear halts ... */
1190 return 0;
1193 static int halt_simple (struct usbtest_dev *dev)
1195 int ep;
1196 int retval = 0;
1197 struct urb *urb;
1199 urb = simple_alloc_urb (testdev_to_usbdev (dev), 0, 512);
1200 if (urb == NULL)
1201 return -ENOMEM;
1203 if (dev->in_pipe) {
1204 ep = usb_pipeendpoint (dev->in_pipe) | USB_DIR_IN;
1205 urb->pipe = dev->in_pipe;
1206 retval = test_halt(dev, ep, urb);
1207 if (retval < 0)
1208 goto done;
1211 if (dev->out_pipe) {
1212 ep = usb_pipeendpoint (dev->out_pipe);
1213 urb->pipe = dev->out_pipe;
1214 retval = test_halt(dev, ep, urb);
1216 done:
1217 simple_free_urb (urb);
1218 return retval;
1221 /*-------------------------------------------------------------------------*/
1223 /* Control OUT tests use the vendor control requests from Intel's
1224 * USB 2.0 compliance test device: write a buffer, read it back.
1226 * Intel's spec only _requires_ that it work for one packet, which
1227 * is pretty weak. Some HCDs place limits here; most devices will
1228 * need to be able to handle more than one OUT data packet. We'll
1229 * try whatever we're told to try.
1231 static int ctrl_out (struct usbtest_dev *dev,
1232 unsigned count, unsigned length, unsigned vary)
1234 unsigned i, j, len;
1235 int retval;
1236 u8 *buf;
1237 char *what = "?";
1238 struct usb_device *udev;
1240 if (length < 1 || length > 0xffff || vary >= length)
1241 return -EINVAL;
1243 buf = kmalloc(length, GFP_KERNEL);
1244 if (!buf)
1245 return -ENOMEM;
1247 udev = testdev_to_usbdev (dev);
1248 len = length;
1249 retval = 0;
1251 /* NOTE: hardware might well act differently if we pushed it
1252 * with lots back-to-back queued requests.
1254 for (i = 0; i < count; i++) {
1255 /* write patterned data */
1256 for (j = 0; j < len; j++)
1257 buf [j] = i + j;
1258 retval = usb_control_msg (udev, usb_sndctrlpipe (udev,0),
1259 0x5b, USB_DIR_OUT|USB_TYPE_VENDOR,
1260 0, 0, buf, len, USB_CTRL_SET_TIMEOUT);
1261 if (retval != len) {
1262 what = "write";
1263 if (retval >= 0) {
1264 ERROR(dev, "ctrl_out, wlen %d (expected %d)\n",
1265 retval, len);
1266 retval = -EBADMSG;
1268 break;
1271 /* read it back -- assuming nothing intervened!! */
1272 retval = usb_control_msg (udev, usb_rcvctrlpipe (udev,0),
1273 0x5c, USB_DIR_IN|USB_TYPE_VENDOR,
1274 0, 0, buf, len, USB_CTRL_GET_TIMEOUT);
1275 if (retval != len) {
1276 what = "read";
1277 if (retval >= 0) {
1278 ERROR(dev, "ctrl_out, rlen %d (expected %d)\n",
1279 retval, len);
1280 retval = -EBADMSG;
1282 break;
1285 /* fail if we can't verify */
1286 for (j = 0; j < len; j++) {
1287 if (buf [j] != (u8) (i + j)) {
1288 ERROR(dev, "ctrl_out, byte %d is %d not %d\n",
1289 j, buf [j], (u8) i + j);
1290 retval = -EBADMSG;
1291 break;
1294 if (retval < 0) {
1295 what = "verify";
1296 break;
1299 len += vary;
1301 /* [real world] the "zero bytes IN" case isn't really used.
1302 * hardware can easily trip up in this weird case, since its
1303 * status stage is IN, not OUT like other ep0in transfers.
1305 if (len > length)
1306 len = realworld ? 1 : 0;
1309 if (retval < 0)
1310 ERROR (dev, "ctrl_out %s failed, code %d, count %d\n",
1311 what, retval, i);
1313 kfree (buf);
1314 return retval;
1317 /*-------------------------------------------------------------------------*/
1319 /* ISO tests ... mimics common usage
1320 * - buffer length is split into N packets (mostly maxpacket sized)
1321 * - multi-buffers according to sglen
1324 struct iso_context {
1325 unsigned count;
1326 unsigned pending;
1327 spinlock_t lock;
1328 struct completion done;
1329 int submit_error;
1330 unsigned long errors;
1331 unsigned long packet_count;
1332 struct usbtest_dev *dev;
1335 static void iso_callback (struct urb *urb)
1337 struct iso_context *ctx = urb->context;
1339 spin_lock(&ctx->lock);
1340 ctx->count--;
1342 ctx->packet_count += urb->number_of_packets;
1343 if (urb->error_count > 0)
1344 ctx->errors += urb->error_count;
1345 else if (urb->status != 0)
1346 ctx->errors += urb->number_of_packets;
1348 if (urb->status == 0 && ctx->count > (ctx->pending - 1)
1349 && !ctx->submit_error) {
1350 int status = usb_submit_urb (urb, GFP_ATOMIC);
1351 switch (status) {
1352 case 0:
1353 goto done;
1354 default:
1355 dev_err(&ctx->dev->intf->dev,
1356 "iso resubmit err %d\n",
1357 status);
1358 /* FALLTHROUGH */
1359 case -ENODEV: /* disconnected */
1360 case -ESHUTDOWN: /* endpoint disabled */
1361 ctx->submit_error = 1;
1362 break;
1366 ctx->pending--;
1367 if (ctx->pending == 0) {
1368 if (ctx->errors)
1369 dev_err(&ctx->dev->intf->dev,
1370 "iso test, %lu errors out of %lu\n",
1371 ctx->errors, ctx->packet_count);
1372 complete (&ctx->done);
1374 done:
1375 spin_unlock(&ctx->lock);
1378 static struct urb *iso_alloc_urb (
1379 struct usb_device *udev,
1380 int pipe,
1381 struct usb_endpoint_descriptor *desc,
1382 long bytes
1385 struct urb *urb;
1386 unsigned i, maxp, packets;
1388 if (bytes < 0 || !desc)
1389 return NULL;
1390 maxp = 0x7ff & le16_to_cpu(desc->wMaxPacketSize);
1391 maxp *= 1 + (0x3 & (le16_to_cpu(desc->wMaxPacketSize) >> 11));
1392 packets = DIV_ROUND_UP(bytes, maxp);
1394 urb = usb_alloc_urb (packets, GFP_KERNEL);
1395 if (!urb)
1396 return urb;
1397 urb->dev = udev;
1398 urb->pipe = pipe;
1400 urb->number_of_packets = packets;
1401 urb->transfer_buffer_length = bytes;
1402 urb->transfer_buffer = usb_alloc_coherent (udev, bytes, GFP_KERNEL,
1403 &urb->transfer_dma);
1404 if (!urb->transfer_buffer) {
1405 usb_free_urb (urb);
1406 return NULL;
1408 memset (urb->transfer_buffer, 0, bytes);
1409 for (i = 0; i < packets; i++) {
1410 /* here, only the last packet will be short */
1411 urb->iso_frame_desc[i].length = min ((unsigned) bytes, maxp);
1412 bytes -= urb->iso_frame_desc[i].length;
1414 urb->iso_frame_desc[i].offset = maxp * i;
1417 urb->complete = iso_callback;
1418 // urb->context = SET BY CALLER
1419 urb->interval = 1 << (desc->bInterval - 1);
1420 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
1421 return urb;
1424 static int
1425 test_iso_queue (struct usbtest_dev *dev, struct usbtest_param *param,
1426 int pipe, struct usb_endpoint_descriptor *desc)
1428 struct iso_context context;
1429 struct usb_device *udev;
1430 unsigned i;
1431 unsigned long packets = 0;
1432 int status = 0;
1433 struct urb *urbs[10];
1435 if (param->sglen > 10)
1436 return -EDOM;
1438 memset(&context, 0, sizeof context);
1439 context.count = param->iterations * param->sglen;
1440 context.dev = dev;
1441 init_completion (&context.done);
1442 spin_lock_init (&context.lock);
1444 memset (urbs, 0, sizeof urbs);
1445 udev = testdev_to_usbdev (dev);
1446 dev_info(&dev->intf->dev,
1447 "... iso period %d %sframes, wMaxPacket %04x\n",
1448 1 << (desc->bInterval - 1),
1449 (udev->speed == USB_SPEED_HIGH) ? "micro" : "",
1450 le16_to_cpu(desc->wMaxPacketSize));
1452 for (i = 0; i < param->sglen; i++) {
1453 urbs [i] = iso_alloc_urb (udev, pipe, desc,
1454 param->length);
1455 if (!urbs [i]) {
1456 status = -ENOMEM;
1457 goto fail;
1459 packets += urbs[i]->number_of_packets;
1460 urbs [i]->context = &context;
1462 packets *= param->iterations;
1463 dev_info(&dev->intf->dev,
1464 "... total %lu msec (%lu packets)\n",
1465 (packets * (1 << (desc->bInterval - 1)))
1466 / ((udev->speed == USB_SPEED_HIGH) ? 8 : 1),
1467 packets);
1469 spin_lock_irq (&context.lock);
1470 for (i = 0; i < param->sglen; i++) {
1471 ++context.pending;
1472 status = usb_submit_urb (urbs [i], GFP_ATOMIC);
1473 if (status < 0) {
1474 ERROR (dev, "submit iso[%d], error %d\n", i, status);
1475 if (i == 0) {
1476 spin_unlock_irq (&context.lock);
1477 goto fail;
1480 simple_free_urb (urbs [i]);
1481 urbs[i] = NULL;
1482 context.pending--;
1483 context.submit_error = 1;
1484 break;
1487 spin_unlock_irq (&context.lock);
1489 wait_for_completion (&context.done);
1491 for (i = 0; i < param->sglen; i++) {
1492 if (urbs[i])
1493 simple_free_urb(urbs[i]);
1496 * Isochronous transfers are expected to fail sometimes. As an
1497 * arbitrary limit, we will report an error if any submissions
1498 * fail or if the transfer failure rate is > 10%.
1500 if (status != 0)
1502 else if (context.submit_error)
1503 status = -EACCES;
1504 else if (context.errors > context.packet_count / 10)
1505 status = -EIO;
1506 return status;
1508 fail:
1509 for (i = 0; i < param->sglen; i++) {
1510 if (urbs [i])
1511 simple_free_urb (urbs [i]);
1513 return status;
1516 /*-------------------------------------------------------------------------*/
1518 /* We only have this one interface to user space, through usbfs.
1519 * User mode code can scan usbfs to find N different devices (maybe on
1520 * different busses) to use when testing, and allocate one thread per
1521 * test. So discovery is simplified, and we have no device naming issues.
1523 * Don't use these only as stress/load tests. Use them along with with
1524 * other USB bus activity: plugging, unplugging, mousing, mp3 playback,
1525 * video capture, and so on. Run different tests at different times, in
1526 * different sequences. Nothing here should interact with other devices,
1527 * except indirectly by consuming USB bandwidth and CPU resources for test
1528 * threads and request completion. But the only way to know that for sure
1529 * is to test when HC queues are in use by many devices.
1531 * WARNING: Because usbfs grabs udev->dev.sem before calling this ioctl(),
1532 * it locks out usbcore in certain code paths. Notably, if you disconnect
1533 * the device-under-test, khubd will wait block forever waiting for the
1534 * ioctl to complete ... so that usb_disconnect() can abort the pending
1535 * urbs and then call usbtest_disconnect(). To abort a test, you're best
1536 * off just killing the userspace task and waiting for it to exit.
1539 /* No BKL needed */
1540 static int
1541 usbtest_ioctl (struct usb_interface *intf, unsigned int code, void *buf)
1543 struct usbtest_dev *dev = usb_get_intfdata (intf);
1544 struct usb_device *udev = testdev_to_usbdev (dev);
1545 struct usbtest_param *param = buf;
1546 int retval = -EOPNOTSUPP;
1547 struct urb *urb;
1548 struct scatterlist *sg;
1549 struct usb_sg_request req;
1550 struct timeval start;
1551 unsigned i;
1554 pattern = mod_pattern;
1556 if (code != USBTEST_REQUEST)
1557 return -EOPNOTSUPP;
1559 if (param->iterations <= 0)
1560 return -EINVAL;
1562 if (mutex_lock_interruptible(&dev->lock))
1563 return -ERESTARTSYS;
1566 /* some devices, like ez-usb default devices, need a non-default
1567 * altsetting to have any active endpoints. some tests change
1568 * altsettings; force a default so most tests don't need to check.
1570 if (dev->info->alt >= 0) {
1571 int res;
1573 if (intf->altsetting->desc.bInterfaceNumber) {
1574 mutex_unlock(&dev->lock);
1575 return -ENODEV;
1577 res = set_altsetting (dev, dev->info->alt);
1578 if (res) {
1579 dev_err (&intf->dev,
1580 "set altsetting to %d failed, %d\n",
1581 dev->info->alt, res);
1582 mutex_unlock(&dev->lock);
1583 return res;
1587 do_gettimeofday (&start);
1588 switch (param->test_num) {
1590 case 0:
1591 dev_info(&intf->dev, "TEST 0: NOP\n");
1592 retval = 0;
1593 break;
1595 /* Simple non-queued bulk I/O tests */
1596 case 1:
1597 if (dev->out_pipe == 0)
1598 break;
1599 dev_info(&intf->dev,
1600 "TEST 1: write %d bytes %u times\n",
1601 param->length, param->iterations);
1602 urb = simple_alloc_urb (udev, dev->out_pipe, param->length);
1603 if (!urb) {
1604 retval = -ENOMEM;
1605 break;
1607 // FIRMWARE: bulk sink (maybe accepts short writes)
1608 retval = simple_io(dev, urb, param->iterations, 0, 0, "test1");
1609 simple_free_urb (urb);
1610 break;
1611 case 2:
1612 if (dev->in_pipe == 0)
1613 break;
1614 dev_info(&intf->dev,
1615 "TEST 2: read %d bytes %u times\n",
1616 param->length, param->iterations);
1617 urb = simple_alloc_urb (udev, dev->in_pipe, param->length);
1618 if (!urb) {
1619 retval = -ENOMEM;
1620 break;
1622 // FIRMWARE: bulk source (maybe generates short writes)
1623 retval = simple_io(dev, urb, param->iterations, 0, 0, "test2");
1624 simple_free_urb (urb);
1625 break;
1626 case 3:
1627 if (dev->out_pipe == 0 || param->vary == 0)
1628 break;
1629 dev_info(&intf->dev,
1630 "TEST 3: write/%d 0..%d bytes %u times\n",
1631 param->vary, param->length, param->iterations);
1632 urb = simple_alloc_urb (udev, dev->out_pipe, param->length);
1633 if (!urb) {
1634 retval = -ENOMEM;
1635 break;
1637 // FIRMWARE: bulk sink (maybe accepts short writes)
1638 retval = simple_io(dev, urb, param->iterations, param->vary,
1639 0, "test3");
1640 simple_free_urb (urb);
1641 break;
1642 case 4:
1643 if (dev->in_pipe == 0 || param->vary == 0)
1644 break;
1645 dev_info(&intf->dev,
1646 "TEST 4: read/%d 0..%d bytes %u times\n",
1647 param->vary, param->length, param->iterations);
1648 urb = simple_alloc_urb (udev, dev->in_pipe, param->length);
1649 if (!urb) {
1650 retval = -ENOMEM;
1651 break;
1653 // FIRMWARE: bulk source (maybe generates short writes)
1654 retval = simple_io(dev, urb, param->iterations, param->vary,
1655 0, "test4");
1656 simple_free_urb (urb);
1657 break;
1659 /* Queued bulk I/O tests */
1660 case 5:
1661 if (dev->out_pipe == 0 || param->sglen == 0)
1662 break;
1663 dev_info(&intf->dev,
1664 "TEST 5: write %d sglists %d entries of %d bytes\n",
1665 param->iterations,
1666 param->sglen, param->length);
1667 sg = alloc_sglist (param->sglen, param->length, 0);
1668 if (!sg) {
1669 retval = -ENOMEM;
1670 break;
1672 // FIRMWARE: bulk sink (maybe accepts short writes)
1673 retval = perform_sglist(dev, param->iterations, dev->out_pipe,
1674 &req, sg, param->sglen);
1675 free_sglist (sg, param->sglen);
1676 break;
1678 case 6:
1679 if (dev->in_pipe == 0 || param->sglen == 0)
1680 break;
1681 dev_info(&intf->dev,
1682 "TEST 6: read %d sglists %d entries of %d bytes\n",
1683 param->iterations,
1684 param->sglen, param->length);
1685 sg = alloc_sglist (param->sglen, param->length, 0);
1686 if (!sg) {
1687 retval = -ENOMEM;
1688 break;
1690 // FIRMWARE: bulk source (maybe generates short writes)
1691 retval = perform_sglist(dev, param->iterations, dev->in_pipe,
1692 &req, sg, param->sglen);
1693 free_sglist (sg, param->sglen);
1694 break;
1695 case 7:
1696 if (dev->out_pipe == 0 || param->sglen == 0 || param->vary == 0)
1697 break;
1698 dev_info(&intf->dev,
1699 "TEST 7: write/%d %d sglists %d entries 0..%d bytes\n",
1700 param->vary, param->iterations,
1701 param->sglen, param->length);
1702 sg = alloc_sglist (param->sglen, param->length, param->vary);
1703 if (!sg) {
1704 retval = -ENOMEM;
1705 break;
1707 // FIRMWARE: bulk sink (maybe accepts short writes)
1708 retval = perform_sglist(dev, param->iterations, dev->out_pipe,
1709 &req, sg, param->sglen);
1710 free_sglist (sg, param->sglen);
1711 break;
1712 case 8:
1713 if (dev->in_pipe == 0 || param->sglen == 0 || param->vary == 0)
1714 break;
1715 dev_info(&intf->dev,
1716 "TEST 8: read/%d %d sglists %d entries 0..%d bytes\n",
1717 param->vary, param->iterations,
1718 param->sglen, param->length);
1719 sg = alloc_sglist (param->sglen, param->length, param->vary);
1720 if (!sg) {
1721 retval = -ENOMEM;
1722 break;
1724 // FIRMWARE: bulk source (maybe generates short writes)
1725 retval = perform_sglist(dev, param->iterations, dev->in_pipe,
1726 &req, sg, param->sglen);
1727 free_sglist (sg, param->sglen);
1728 break;
1730 /* non-queued sanity tests for control (chapter 9 subset) */
1731 case 9:
1732 retval = 0;
1733 dev_info(&intf->dev,
1734 "TEST 9: ch9 (subset) control tests, %d times\n",
1735 param->iterations);
1736 for (i = param->iterations; retval == 0 && i--; /* NOP */)
1737 retval = ch9_postconfig (dev);
1738 if (retval)
1739 dev_err(&intf->dev, "ch9 subset failed, "
1740 "iterations left %d\n", i);
1741 break;
1743 /* queued control messaging */
1744 case 10:
1745 if (param->sglen == 0)
1746 break;
1747 retval = 0;
1748 dev_info(&intf->dev,
1749 "TEST 10: queue %d control calls, %d times\n",
1750 param->sglen,
1751 param->iterations);
1752 retval = test_ctrl_queue (dev, param);
1753 break;
1755 /* simple non-queued unlinks (ring with one urb) */
1756 case 11:
1757 if (dev->in_pipe == 0 || !param->length)
1758 break;
1759 retval = 0;
1760 dev_info(&intf->dev, "TEST 11: unlink %d reads of %d\n",
1761 param->iterations, param->length);
1762 for (i = param->iterations; retval == 0 && i--; /* NOP */)
1763 retval = unlink_simple (dev, dev->in_pipe,
1764 param->length);
1765 if (retval)
1766 dev_err(&intf->dev, "unlink reads failed %d, "
1767 "iterations left %d\n", retval, i);
1768 break;
1769 case 12:
1770 if (dev->out_pipe == 0 || !param->length)
1771 break;
1772 retval = 0;
1773 dev_info(&intf->dev, "TEST 12: unlink %d writes of %d\n",
1774 param->iterations, param->length);
1775 for (i = param->iterations; retval == 0 && i--; /* NOP */)
1776 retval = unlink_simple (dev, dev->out_pipe,
1777 param->length);
1778 if (retval)
1779 dev_err(&intf->dev, "unlink writes failed %d, "
1780 "iterations left %d\n", retval, i);
1781 break;
1783 /* ep halt tests */
1784 case 13:
1785 if (dev->out_pipe == 0 && dev->in_pipe == 0)
1786 break;
1787 retval = 0;
1788 dev_info(&intf->dev, "TEST 13: set/clear %d halts\n",
1789 param->iterations);
1790 for (i = param->iterations; retval == 0 && i--; /* NOP */)
1791 retval = halt_simple (dev);
1793 if (retval)
1794 ERROR(dev, "halts failed, iterations left %d\n", i);
1795 break;
1797 /* control write tests */
1798 case 14:
1799 if (!dev->info->ctrl_out)
1800 break;
1801 dev_info(&intf->dev, "TEST 14: %d ep0out, %d..%d vary %d\n",
1802 param->iterations,
1803 realworld ? 1 : 0, param->length,
1804 param->vary);
1805 retval = ctrl_out(dev, param->iterations,
1806 param->length, param->vary);
1807 break;
1809 /* iso write tests */
1810 case 15:
1811 if (dev->out_iso_pipe == 0 || param->sglen == 0)
1812 break;
1813 dev_info(&intf->dev,
1814 "TEST 15: write %d iso, %d entries of %d bytes\n",
1815 param->iterations,
1816 param->sglen, param->length);
1817 // FIRMWARE: iso sink
1818 retval = test_iso_queue (dev, param,
1819 dev->out_iso_pipe, dev->iso_out);
1820 break;
1822 /* iso read tests */
1823 case 16:
1824 if (dev->in_iso_pipe == 0 || param->sglen == 0)
1825 break;
1826 dev_info(&intf->dev,
1827 "TEST 16: read %d iso, %d entries of %d bytes\n",
1828 param->iterations,
1829 param->sglen, param->length);
1830 // FIRMWARE: iso source
1831 retval = test_iso_queue (dev, param,
1832 dev->in_iso_pipe, dev->iso_in);
1833 break;
1838 do_gettimeofday (&param->duration);
1839 param->duration.tv_sec -= start.tv_sec;
1840 param->duration.tv_usec -= start.tv_usec;
1841 if (param->duration.tv_usec < 0) {
1842 param->duration.tv_usec += 1000 * 1000;
1843 param->duration.tv_sec -= 1;
1845 mutex_unlock(&dev->lock);
1846 return retval;
1849 /*-------------------------------------------------------------------------*/
1851 static unsigned force_interrupt = 0;
1852 module_param (force_interrupt, uint, 0);
1853 MODULE_PARM_DESC (force_interrupt, "0 = test default; else interrupt");
1855 #ifdef GENERIC
1856 static unsigned short vendor;
1857 module_param(vendor, ushort, 0);
1858 MODULE_PARM_DESC (vendor, "vendor code (from usb-if)");
1860 static unsigned short product;
1861 module_param(product, ushort, 0);
1862 MODULE_PARM_DESC (product, "product code (from vendor)");
1863 #endif
1865 static int
1866 usbtest_probe (struct usb_interface *intf, const struct usb_device_id *id)
1868 struct usb_device *udev;
1869 struct usbtest_dev *dev;
1870 struct usbtest_info *info;
1871 char *rtest, *wtest;
1872 char *irtest, *iwtest;
1874 udev = interface_to_usbdev (intf);
1876 #ifdef GENERIC
1877 /* specify devices by module parameters? */
1878 if (id->match_flags == 0) {
1879 /* vendor match required, product match optional */
1880 if (!vendor || le16_to_cpu(udev->descriptor.idVendor) != (u16)vendor)
1881 return -ENODEV;
1882 if (product && le16_to_cpu(udev->descriptor.idProduct) != (u16)product)
1883 return -ENODEV;
1884 dev_info(&intf->dev, "matched module params, "
1885 "vend=0x%04x prod=0x%04x\n",
1886 le16_to_cpu(udev->descriptor.idVendor),
1887 le16_to_cpu(udev->descriptor.idProduct));
1889 #endif
1891 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1892 if (!dev)
1893 return -ENOMEM;
1894 info = (struct usbtest_info *) id->driver_info;
1895 dev->info = info;
1896 mutex_init(&dev->lock);
1898 dev->intf = intf;
1900 /* cacheline-aligned scratch for i/o */
1901 if ((dev->buf = kmalloc (TBUF_SIZE, GFP_KERNEL)) == NULL) {
1902 kfree (dev);
1903 return -ENOMEM;
1906 /* NOTE this doesn't yet test the handful of difference that are
1907 * visible with high speed interrupts: bigger maxpacket (1K) and
1908 * "high bandwidth" modes (up to 3 packets/uframe).
1910 rtest = wtest = "";
1911 irtest = iwtest = "";
1912 if (force_interrupt || udev->speed == USB_SPEED_LOW) {
1913 if (info->ep_in) {
1914 dev->in_pipe = usb_rcvintpipe (udev, info->ep_in);
1915 rtest = " intr-in";
1917 if (info->ep_out) {
1918 dev->out_pipe = usb_sndintpipe (udev, info->ep_out);
1919 wtest = " intr-out";
1921 } else {
1922 if (info->autoconf) {
1923 int status;
1925 status = get_endpoints (dev, intf);
1926 if (status < 0) {
1927 WARNING(dev, "couldn't get endpoints, %d\n",
1928 status);
1929 return status;
1931 /* may find bulk or ISO pipes */
1932 } else {
1933 if (info->ep_in)
1934 dev->in_pipe = usb_rcvbulkpipe (udev,
1935 info->ep_in);
1936 if (info->ep_out)
1937 dev->out_pipe = usb_sndbulkpipe (udev,
1938 info->ep_out);
1940 if (dev->in_pipe)
1941 rtest = " bulk-in";
1942 if (dev->out_pipe)
1943 wtest = " bulk-out";
1944 if (dev->in_iso_pipe)
1945 irtest = " iso-in";
1946 if (dev->out_iso_pipe)
1947 iwtest = " iso-out";
1950 usb_set_intfdata (intf, dev);
1951 dev_info (&intf->dev, "%s\n", info->name);
1952 dev_info (&intf->dev, "%s speed {control%s%s%s%s%s} tests%s\n",
1953 ({ char *tmp;
1954 switch (udev->speed) {
1955 case USB_SPEED_LOW: tmp = "low"; break;
1956 case USB_SPEED_FULL: tmp = "full"; break;
1957 case USB_SPEED_HIGH: tmp = "high"; break;
1958 default: tmp = "unknown"; break;
1959 }; tmp; }),
1960 info->ctrl_out ? " in/out" : "",
1961 rtest, wtest,
1962 irtest, iwtest,
1963 info->alt >= 0 ? " (+alt)" : "");
1964 return 0;
1967 static int usbtest_suspend (struct usb_interface *intf, pm_message_t message)
1969 return 0;
1972 static int usbtest_resume (struct usb_interface *intf)
1974 return 0;
1978 static void usbtest_disconnect (struct usb_interface *intf)
1980 struct usbtest_dev *dev = usb_get_intfdata (intf);
1982 usb_set_intfdata (intf, NULL);
1983 dev_dbg (&intf->dev, "disconnect\n");
1984 kfree (dev);
1987 /* Basic testing only needs a device that can source or sink bulk traffic.
1988 * Any device can test control transfers (default with GENERIC binding).
1990 * Several entries work with the default EP0 implementation that's built
1991 * into EZ-USB chips. There's a default vendor ID which can be overridden
1992 * by (very) small config EEPROMS, but otherwise all these devices act
1993 * identically until firmware is loaded: only EP0 works. It turns out
1994 * to be easy to make other endpoints work, without modifying that EP0
1995 * behavior. For now, we expect that kind of firmware.
1998 /* an21xx or fx versions of ez-usb */
1999 static struct usbtest_info ez1_info = {
2000 .name = "EZ-USB device",
2001 .ep_in = 2,
2002 .ep_out = 2,
2003 .alt = 1,
2006 /* fx2 version of ez-usb */
2007 static struct usbtest_info ez2_info = {
2008 .name = "FX2 device",
2009 .ep_in = 6,
2010 .ep_out = 2,
2011 .alt = 1,
2014 /* ezusb family device with dedicated usb test firmware,
2016 static struct usbtest_info fw_info = {
2017 .name = "usb test device",
2018 .ep_in = 2,
2019 .ep_out = 2,
2020 .alt = 1,
2021 .autoconf = 1, // iso and ctrl_out need autoconf
2022 .ctrl_out = 1,
2023 .iso = 1, // iso_ep's are #8 in/out
2026 /* peripheral running Linux and 'zero.c' test firmware, or
2027 * its user-mode cousin. different versions of this use
2028 * different hardware with the same vendor/product codes.
2029 * host side MUST rely on the endpoint descriptors.
2031 static struct usbtest_info gz_info = {
2032 .name = "Linux gadget zero",
2033 .autoconf = 1,
2034 .ctrl_out = 1,
2035 .alt = 0,
2038 static struct usbtest_info um_info = {
2039 .name = "Linux user mode test driver",
2040 .autoconf = 1,
2041 .alt = -1,
2044 static struct usbtest_info um2_info = {
2045 .name = "Linux user mode ISO test driver",
2046 .autoconf = 1,
2047 .iso = 1,
2048 .alt = -1,
2051 #ifdef IBOT2
2052 /* this is a nice source of high speed bulk data;
2053 * uses an FX2, with firmware provided in the device
2055 static struct usbtest_info ibot2_info = {
2056 .name = "iBOT2 webcam",
2057 .ep_in = 2,
2058 .alt = -1,
2060 #endif
2062 #ifdef GENERIC
2063 /* we can use any device to test control traffic */
2064 static struct usbtest_info generic_info = {
2065 .name = "Generic USB device",
2066 .alt = -1,
2068 #endif
2071 static const struct usb_device_id id_table[] = {
2073 /*-------------------------------------------------------------*/
2075 /* EZ-USB devices which download firmware to replace (or in our
2076 * case augment) the default device implementation.
2079 /* generic EZ-USB FX controller */
2080 { USB_DEVICE (0x0547, 0x2235),
2081 .driver_info = (unsigned long) &ez1_info,
2084 /* CY3671 development board with EZ-USB FX */
2085 { USB_DEVICE (0x0547, 0x0080),
2086 .driver_info = (unsigned long) &ez1_info,
2089 /* generic EZ-USB FX2 controller (or development board) */
2090 { USB_DEVICE (0x04b4, 0x8613),
2091 .driver_info = (unsigned long) &ez2_info,
2094 /* re-enumerated usb test device firmware */
2095 { USB_DEVICE (0xfff0, 0xfff0),
2096 .driver_info = (unsigned long) &fw_info,
2099 /* "Gadget Zero" firmware runs under Linux */
2100 { USB_DEVICE (0x0525, 0xa4a0),
2101 .driver_info = (unsigned long) &gz_info,
2104 /* so does a user-mode variant */
2105 { USB_DEVICE (0x0525, 0xa4a4),
2106 .driver_info = (unsigned long) &um_info,
2109 /* ... and a user-mode variant that talks iso */
2110 { USB_DEVICE (0x0525, 0xa4a3),
2111 .driver_info = (unsigned long) &um2_info,
2114 #ifdef KEYSPAN_19Qi
2115 /* Keyspan 19qi uses an21xx (original EZ-USB) */
2116 // this does not coexist with the real Keyspan 19qi driver!
2117 { USB_DEVICE (0x06cd, 0x010b),
2118 .driver_info = (unsigned long) &ez1_info,
2120 #endif
2122 /*-------------------------------------------------------------*/
2124 #ifdef IBOT2
2125 /* iBOT2 makes a nice source of high speed bulk-in data */
2126 // this does not coexist with a real iBOT2 driver!
2127 { USB_DEVICE (0x0b62, 0x0059),
2128 .driver_info = (unsigned long) &ibot2_info,
2130 #endif
2132 /*-------------------------------------------------------------*/
2134 #ifdef GENERIC
2135 /* module params can specify devices to use for control tests */
2136 { .driver_info = (unsigned long) &generic_info, },
2137 #endif
2139 /*-------------------------------------------------------------*/
2143 MODULE_DEVICE_TABLE (usb, id_table);
2145 static struct usb_driver usbtest_driver = {
2146 .name = "usbtest",
2147 .id_table = id_table,
2148 .probe = usbtest_probe,
2149 .unlocked_ioctl = usbtest_ioctl,
2150 .disconnect = usbtest_disconnect,
2151 .suspend = usbtest_suspend,
2152 .resume = usbtest_resume,
2155 /*-------------------------------------------------------------------------*/
2157 static int __init usbtest_init (void)
2159 #ifdef GENERIC
2160 if (vendor)
2161 pr_debug("params: vend=0x%04x prod=0x%04x\n", vendor, product);
2162 #endif
2163 return usb_register (&usbtest_driver);
2165 module_init (usbtest_init);
2167 static void __exit usbtest_exit (void)
2169 usb_deregister (&usbtest_driver);
2171 module_exit (usbtest_exit);
2173 MODULE_DESCRIPTION ("USB Core/HCD Testing Driver");
2174 MODULE_LICENSE ("GPL");