1 #include <linux/config.h>
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
4 #include <linux/init.h>
5 #include <linux/slab.h>
7 #include <linux/module.h>
8 #include <asm/scatterlist.h>
10 #if !defined (DEBUG) && defined (CONFIG_USB_DEBUG)
13 #include <linux/usb.h>
16 /*-------------------------------------------------------------------------*/
18 // FIXME make these public somewhere; usbdevfs.h?
20 struct usbtest_param
{
22 unsigned test_num
; /* 0..(TEST_CASES-1) */
29 struct timeval duration
;
31 #define USBTEST_REQUEST _IOWR('U', 100, struct usbtest_param)
33 /*-------------------------------------------------------------------------*/
35 #define GENERIC /* let probe() bind using module params */
37 /* Some devices that can be used for testing will have "real" drivers.
38 * Entries for those need to be enabled here by hand, after disabling
41 //#define IBOT2 /* grab iBOT2 webcams */
42 //#define KEYSPAN_19Qi /* grab un-renumerated serial adapter */
44 /*-------------------------------------------------------------------------*/
48 u8 ep_in
; /* bulk/intr source */
49 u8 ep_out
; /* bulk/intr sink */
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.
60 struct usb_interface
*intf
;
61 struct usbtest_info
*info
;
71 static struct usb_device
*testdev_to_usbdev (struct usbtest_dev
*test
)
73 return interface_to_usbdev (test
->intf
);
76 /* set up all urbs so they can be used with either bulk or interrupt */
77 #define INTERRUPT_RATE 1 /* msec/transfer */
79 /*-------------------------------------------------------------------------*/
81 /* Support for testing basic non-queued I/O streams.
83 * These just package urbs as requests that can be easily canceled.
84 * Each urb's data buffer is dynamically allocated; callers can fill
85 * them with non-zero test data (or test for it) when appropriate.
88 static void simple_callback (struct urb
*urb
, struct pt_regs
*regs
)
90 complete ((struct completion
*) urb
->context
);
93 static struct urb
*simple_alloc_urb (
94 struct usb_device
*udev
,
103 urb
= usb_alloc_urb (0, SLAB_KERNEL
);
106 usb_fill_bulk_urb (urb
, udev
, pipe
, 0, bytes
, simple_callback
, 0);
107 urb
->interval
= (udev
->speed
== USB_SPEED_HIGH
)
108 ? (INTERRUPT_RATE
<< 3)
110 urb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
111 if (usb_pipein (pipe
))
112 urb
->transfer_flags
|= URB_SHORT_NOT_OK
;
113 urb
->transfer_buffer
= usb_buffer_alloc (udev
, bytes
, SLAB_KERNEL
,
115 if (!urb
->transfer_buffer
) {
119 memset (urb
->transfer_buffer
, 0, bytes
);
123 static void simple_free_urb (struct urb
*urb
)
125 usb_buffer_free (urb
->dev
, urb
->transfer_buffer_length
,
126 urb
->transfer_buffer
, urb
->transfer_dma
);
130 static int simple_io (
136 struct usb_device
*udev
= urb
->dev
;
137 int max
= urb
->transfer_buffer_length
;
138 struct completion completion
;
141 urb
->context
= &completion
;
142 while (retval
== 0 && iterations
-- > 0) {
143 init_completion (&completion
);
144 if ((retval
= usb_submit_urb (urb
, SLAB_KERNEL
)) != 0)
147 /* NOTE: no timeouts; can't be broken out of by interrupt */
148 wait_for_completion (&completion
);
149 retval
= urb
->status
;
153 int len
= urb
->transfer_buffer_length
;
158 len
= (vary
< max
) ? vary
: max
;
159 urb
->transfer_buffer_length
= len
;
162 /* FIXME if endpoint halted, clear halt (and log) */
164 urb
->transfer_buffer_length
= max
;
166 // FIXME for unlink or fault handling tests, don't report
167 // failure if retval is as we expected ...
169 dbg ("simple_io failed, iterations left %d, status %d",
174 /*-------------------------------------------------------------------------*/
176 /* We use scatterlist primitives to test queued I/O.
177 * Yes, this also tests the scatterlist primitives.
180 static void free_sglist (struct scatterlist
*sg
, int nents
)
186 for (i
= 0; i
< nents
; i
++) {
189 kfree (page_address (sg
[i
].page
) + sg
[i
].offset
);
194 static struct scatterlist
*
195 alloc_sglist (int nents
, int max
, int vary
)
197 struct scatterlist
*sg
;
201 sg
= kmalloc (nents
* sizeof *sg
, SLAB_KERNEL
);
204 memset (sg
, 0, nents
* sizeof *sg
);
206 for (i
= 0; i
< nents
; i
++) {
209 buf
= kmalloc (size
, SLAB_KERNEL
);
214 memset (buf
, 0, size
);
216 /* kmalloc pages are always physically contiguous! */
217 sg
[i
].page
= virt_to_page (buf
);
218 sg
[i
].offset
= ((unsigned) buf
) & ~PAGE_MASK
;
219 sg
[i
].length
= size
;
225 size
= (vary
< max
) ? vary
: max
;
232 static int perform_sglist (
233 struct usb_device
*udev
,
236 struct usb_sg_request
*req
,
237 struct scatterlist
*sg
,
243 while (retval
== 0 && iterations
-- > 0) {
244 retval
= usb_sg_init (req
, udev
, pipe
,
245 (udev
->speed
== USB_SPEED_HIGH
)
246 ? (INTERRUPT_RATE
<< 3)
248 sg
, nents
, 0, SLAB_KERNEL
);
253 retval
= req
->status
;
255 /* FIXME if endpoint halted, clear halt (and log) */
258 // FIXME for unlink or fault handling tests, don't report
259 // failure if retval is as we expected ...
262 dbg ("perform_sglist failed, iterations left %d, status %d",
268 /*-------------------------------------------------------------------------*/
270 /* unqueued control message testing
272 * there's a nice set of device functional requirements in chapter 9 of the
273 * usb 2.0 spec, which we can apply to ANY device, even ones that don't use
274 * special test firmware.
276 * we know the device is configured (or suspended) by the time it's visible
277 * through usbfs. we can't change that, so we won't test enumeration (which
278 * worked 'well enough' to get here, this time), power management (ditto),
279 * or remote wakeup (which needs human interaction).
282 static int realworld
= 1;
283 MODULE_PARM (realworld
, "i");
284 MODULE_PARM_DESC (realworld
, "clear to demand stricter ch9 compliance");
286 static int get_altsetting (struct usbtest_dev
*dev
)
288 struct usb_interface
*iface
= dev
->intf
;
289 struct usb_device
*udev
= interface_to_usbdev (iface
);
292 retval
= usb_control_msg (udev
, usb_rcvctrlpipe (udev
, 0),
293 USB_REQ_GET_INTERFACE
, USB_DIR_IN
|USB_RECIP_INTERFACE
,
294 0, iface
->altsetting
[0].desc
.bInterfaceNumber
,
295 dev
->buf
, 1, HZ
* USB_CTRL_GET_TIMEOUT
);
307 /* this is usb_set_interface(), with no 'only one altsetting' case */
308 static int set_altsetting (struct usbtest_dev
*dev
, int alternate
)
310 struct usb_interface
*iface
= dev
->intf
;
311 struct usb_device
*udev
;
312 struct usb_host_interface
*iface_as
;
315 if (alternate
< 0 || alternate
>= iface
->num_altsetting
)
318 udev
= interface_to_usbdev (iface
);
319 if ((ret
= usb_control_msg (udev
, usb_sndctrlpipe (udev
, 0),
320 USB_REQ_SET_INTERFACE
, USB_RECIP_INTERFACE
,
322 iface
->altsetting
->desc
.bInterfaceNumber
,
323 NULL
, 0, HZ
* USB_CTRL_SET_TIMEOUT
)) < 0)
326 // FIXME usbcore should be more like this:
327 // - remove that special casing in usbcore.
328 // - fix usbcore signature to take interface
330 /* prevent requests using previous endpoint settings */
331 iface_as
= iface
->altsetting
+ iface
->act_altsetting
;
332 for (i
= 0; i
< iface_as
->desc
.bNumEndpoints
; i
++) {
333 u8 ep
= iface_as
->endpoint
[i
].desc
.bEndpointAddress
;
334 int out
= !(ep
& USB_DIR_IN
);
336 ep
&= USB_ENDPOINT_NUMBER_MASK
;
337 (out
? udev
->epmaxpacketout
: udev
->epmaxpacketin
) [ep
] = 0;
338 // FIXME want hcd hook here, "forget this endpoint"
340 iface
->act_altsetting
= alternate
;
342 /* reset toggles and maxpacket for all endpoints affected */
343 iface_as
= iface
->altsetting
+ iface
->act_altsetting
;
344 for (i
= 0; i
< iface_as
->desc
.bNumEndpoints
; i
++) {
345 u8 ep
= iface_as
->endpoint
[i
].desc
.bEndpointAddress
;
346 int out
= !(ep
& USB_DIR_IN
);
348 ep
&= USB_ENDPOINT_NUMBER_MASK
;
349 usb_settoggle (udev
, ep
, out
, 0);
350 (out
? udev
->epmaxpacketout
: udev
->epmaxpacketin
) [ep
]
351 = iface_as
->endpoint
[i
].desc
.wMaxPacketSize
;
357 static int is_good_config (char *buf
, int len
)
359 struct usb_config_descriptor
*config
;
361 if (len
< sizeof *config
)
363 config
= (struct usb_config_descriptor
*) buf
;
365 switch (config
->bDescriptorType
) {
367 case USB_DT_OTHER_SPEED_CONFIG
:
368 if (config
->bLength
!= 9)
370 /* this bit 'must be 1' but often isn't */
371 if (!realworld
&& !(config
->bmAttributes
& 0x80)) {
372 dbg ("high bit of config attributes not set");
375 if (config
->bmAttributes
& 0x1f) /* reserved == 0 */
382 le32_to_cpus (&config
->wTotalLength
);
383 if (config
->wTotalLength
== len
) /* read it all */
385 return config
->wTotalLength
>= TBUF_SIZE
; /* max partial read */
388 /* sanity test for standard requests working with usb_control_mesg() and some
389 * of the utility functions which use it.
391 * this doesn't test how endpoint halts behave or data toggles get set, since
392 * we won't do I/O to bulk/interrupt endpoints here (which is how to change
393 * halt or toggle). toggle testing is impractical without support from hcds.
395 * this avoids failing devices linux would normally work with, by not testing
396 * config/altsetting operations for devices that only support their defaults.
397 * such devices rarely support those needless operations.
399 * NOTE that since this is a sanity test, it's not examining boundary cases
400 * to see if usbcore, hcd, and device all behave right. such testing would
401 * involve varied read sizes and other operation sequences.
403 static int ch9_postconfig (struct usbtest_dev
*dev
)
405 struct usb_interface
*iface
= dev
->intf
;
406 struct usb_device
*udev
= interface_to_usbdev (iface
);
409 /* [9.2.3] if there's more than one altsetting, we need to be able to
410 * set and get each one. mostly trusts the descriptors from usbcore.
412 for (i
= 0; i
< iface
->num_altsetting
; i
++) {
414 /* 9.2.3 constrains the range here, and Linux ensures
415 * they're ordered meaningfully in this array
417 if (iface
->altsetting
[i
].desc
.bAlternateSetting
!= i
) {
418 dbg ("%s, illegal alt [%d].bAltSetting = %d",
420 iface
->altsetting
[i
].desc
425 /* [real world] get/set unimplemented if there's only one */
426 if (realworld
&& iface
->num_altsetting
== 1)
429 /* [9.4.10] set_interface */
430 retval
= set_altsetting (dev
, i
);
432 dbg ("%s can't set_interface = %d, %d",
437 /* [9.4.4] get_interface always works */
438 retval
= get_altsetting (dev
);
440 dbg ("%s get alt should be %d, was %d",
442 return (retval
< 0) ? retval
: -EDOM
;
447 /* [real world] get_config unimplemented if there's only one */
448 if (!realworld
|| udev
->descriptor
.bNumConfigurations
!= 1) {
449 int expected
= udev
->actconfig
->desc
.bConfigurationValue
;
451 /* [9.4.2] get_configuration always works
452 * ... although some cheap devices (like one TI Hub I've got)
453 * won't return config descriptors except before set_config.
455 retval
= usb_control_msg (udev
, usb_rcvctrlpipe (udev
, 0),
456 USB_REQ_GET_CONFIGURATION
,
457 USB_DIR_IN
| USB_RECIP_DEVICE
,
458 0, 0, dev
->buf
, 1, HZ
* USB_CTRL_GET_TIMEOUT
);
459 if (retval
!= 1 || dev
->buf
[0] != expected
) {
460 dbg ("%s get config --> %d (%d)", dev
->id
, retval
,
462 return (retval
< 0) ? retval
: -EDOM
;
466 /* there's always [9.4.3] a device descriptor [9.6.1] */
467 retval
= usb_get_descriptor (udev
, USB_DT_DEVICE
, 0,
468 dev
->buf
, sizeof udev
->descriptor
);
469 if (retval
!= sizeof udev
->descriptor
) {
470 dbg ("%s dev descriptor --> %d", dev
->id
, retval
);
471 return (retval
< 0) ? retval
: -EDOM
;
474 /* there's always [9.4.3] at least one config descriptor [9.6.3] */
475 for (i
= 0; i
< udev
->descriptor
.bNumConfigurations
; i
++) {
476 retval
= usb_get_descriptor (udev
, USB_DT_CONFIG
, i
,
477 dev
->buf
, TBUF_SIZE
);
478 if (!is_good_config (dev
->buf
, retval
)) {
479 dbg ("%s config [%d] descriptor --> %d",
481 return (retval
< 0) ? retval
: -EDOM
;
484 // FIXME cross-checking udev->config[i] to make sure usbcore
485 // parsed it right (etc) would be good testing paranoia
488 /* and sometimes [9.2.6.6] speed dependent descriptors */
489 if (udev
->descriptor
.bcdUSB
== 0x0200) { /* pre-swapped */
490 struct usb_qualifier_descriptor
*d
= 0;
492 /* device qualifier [9.6.2] */
493 retval
= usb_get_descriptor (udev
,
494 USB_DT_DEVICE_QUALIFIER
, 0, dev
->buf
,
495 sizeof (struct usb_qualifier_descriptor
));
496 if (retval
== -EPIPE
) {
497 if (udev
->speed
== USB_SPEED_HIGH
) {
498 dbg ("%s hs dev qualifier --> %d",
500 return (retval
< 0) ? retval
: -EDOM
;
502 /* usb2.0 but not high-speed capable; fine */
503 } else if (retval
!= sizeof (struct usb_qualifier_descriptor
)) {
504 dbg ("%s dev qualifier --> %d", dev
->id
, retval
);
505 return (retval
< 0) ? retval
: -EDOM
;
507 d
= (struct usb_qualifier_descriptor
*) dev
->buf
;
509 /* might not have [9.6.2] any other-speed configs [9.6.4] */
511 unsigned max
= d
->bNumConfigurations
;
512 for (i
= 0; i
< max
; i
++) {
513 retval
= usb_get_descriptor (udev
,
514 USB_DT_OTHER_SPEED_CONFIG
, i
,
515 dev
->buf
, TBUF_SIZE
);
516 if (!is_good_config (dev
->buf
, retval
)) {
517 dbg ("%s other speed config --> %d",
519 return (retval
< 0) ? retval
: -EDOM
;
524 // FIXME fetch strings from at least the device descriptor
526 /* [9.4.5] get_status always works */
527 retval
= usb_get_status (udev
, USB_RECIP_DEVICE
, 0, dev
->buf
);
529 dbg ("%s get dev status --> %d", dev
->id
, retval
);
530 return (retval
< 0) ? retval
: -EDOM
;
533 // FIXME configuration.bmAttributes says if we could try to set/clear
534 // the device's remote wakeup feature ... if we can, test that here
536 retval
= usb_get_status (udev
, USB_RECIP_INTERFACE
,
537 iface
->altsetting
[0].desc
.bInterfaceNumber
, dev
->buf
);
539 dbg ("%s get interface status --> %d", dev
->id
, retval
);
540 return (retval
< 0) ? retval
: -EDOM
;
542 // FIXME get status for each endpoint in the interface
547 /*-------------------------------------------------------------------------*/
549 /* use ch9 requests to test whether:
550 * (a) queues work for control, keeping N subtests queued and
551 * active (auto-resubmit) for M loops through the queue.
552 * (b) protocol stalls (control-only) will autorecover.
553 * it's quite not like bulk/intr; no halt clearing.
554 * (c) short control reads are reported and handled.
555 * (d) queues are always processed in-order
560 struct usbtest_dev
*dev
;
561 struct completion complete
;
566 struct usbtest_param
*param
;
570 #define NUM_SUBCASES 13 /* how many test subcases here? */
573 struct usb_ctrlrequest setup
;
578 static void ctrl_complete (struct urb
*urb
, struct pt_regs
*regs
)
580 struct ctrl_ctx
*ctx
= urb
->context
;
581 struct usb_ctrlrequest
*reqp
;
582 struct subcase
*subcase
;
583 int status
= urb
->status
;
585 reqp
= (struct usb_ctrlrequest
*)urb
->setup_packet
;
586 subcase
= container_of (reqp
, struct subcase
, setup
);
588 spin_lock (&ctx
->lock
);
592 /* queue must transfer and complete in fifo order, unless
593 * usb_unlink_urb() is used to unlink something not at the
594 * physical queue head (not tested).
596 if (subcase
->number
> 0) {
597 if ((subcase
->number
- ctx
->last
) != 1) {
598 dbg ("subcase %d completed out of order, last %d",
599 subcase
->number
, ctx
->last
);
604 ctx
->last
= subcase
->number
;
606 /* succeed or fault in only one way? */
607 if (status
== subcase
->expected
)
610 /* async unlink for cleanup? */
611 else if (status
!= -ECONNRESET
) {
613 /* some faults are allowed, not required */
614 if (subcase
->expected
> 0 && (
615 ((urb
->status
== -subcase
->expected
/* happened */
616 || urb
->status
== 0)))) /* didn't */
618 /* sometimes more than one fault is allowed */
619 else if (subcase
->number
== 12 && status
== -EPIPE
)
622 dbg ("subtest %d error, status %d",
623 subcase
->number
, status
);
626 /* unexpected status codes mean errors; ideally, in hardware */
629 if (ctx
->status
== 0) {
632 ctx
->status
= status
;
633 info ("control queue %02x.%02x, err %d, %d left",
634 reqp
->bRequestType
, reqp
->bRequest
,
637 /* FIXME this "unlink everything" exit route should
638 * be a separate test case.
641 /* unlink whatever's still pending */
642 for (i
= 0; i
< ctx
->param
->sglen
; i
++) {
643 struct urb
*u
= ctx
->urb
[i
];
645 if (u
== urb
|| !u
->dev
)
647 status
= usb_unlink_urb (u
);
653 dbg ("urb unlink --> %d", status
);
656 status
= ctx
->status
;
660 /* resubmit if we need to, else mark this as done */
661 if ((status
== 0) && (ctx
->pending
< ctx
->count
)) {
662 if ((status
= usb_submit_urb (urb
, SLAB_ATOMIC
)) != 0) {
663 dbg ("can't resubmit ctrl %02x.%02x, err %d",
664 reqp
->bRequestType
, reqp
->bRequest
, status
);
671 /* signal completion when nothing's queued */
672 if (ctx
->pending
== 0)
673 complete (&ctx
->complete
);
674 spin_unlock (&ctx
->lock
);
678 test_ctrl_queue (struct usbtest_dev
*dev
, struct usbtest_param
*param
)
680 struct usb_device
*udev
= testdev_to_usbdev (dev
);
682 struct ctrl_ctx context
;
685 spin_lock_init (&context
.lock
);
687 init_completion (&context
.complete
);
688 context
.count
= param
->sglen
* param
->iterations
;
690 context
.status
= -ENOMEM
;
691 context
.param
= param
;
694 /* allocate and init the urbs we'll queue.
695 * as with bulk/intr sglists, sglen is the queue depth; it also
696 * controls which subtests run (more tests than sglen) or rerun.
698 urb
= kmalloc (param
->sglen
* sizeof (struct urb
*), SLAB_KERNEL
);
701 memset (urb
, 0, param
->sglen
* sizeof (struct urb
*));
702 for (i
= 0; i
< param
->sglen
; i
++) {
703 int pipe
= usb_rcvctrlpipe (udev
, 0);
706 struct usb_ctrlrequest req
;
707 struct subcase
*reqp
;
710 /* requests here are mostly expected to succeed on any
711 * device, but some are chosen to trigger protocol stalls
714 memset (&req
, 0, sizeof req
);
715 req
.bRequest
= USB_REQ_GET_DESCRIPTOR
;
716 req
.bRequestType
= USB_DIR_IN
|USB_RECIP_DEVICE
;
718 switch (i
% NUM_SUBCASES
) {
719 case 0: // get device descriptor
720 req
.wValue
= cpu_to_le16 (USB_DT_DEVICE
<< 8);
721 len
= sizeof (struct usb_device_descriptor
);
723 case 1: // get first config descriptor (only)
724 req
.wValue
= cpu_to_le16 ((USB_DT_CONFIG
<< 8) | 0);
725 len
= sizeof (struct usb_config_descriptor
);
727 case 2: // get altsetting (OFTEN STALLS)
728 req
.bRequest
= USB_REQ_GET_INTERFACE
;
729 req
.bRequestType
= USB_DIR_IN
|USB_RECIP_INTERFACE
;
730 // index = 0 means first interface
734 case 3: // get interface status
735 req
.bRequest
= USB_REQ_GET_STATUS
;
736 req
.bRequestType
= USB_DIR_IN
|USB_RECIP_INTERFACE
;
740 case 4: // get device status
741 req
.bRequest
= USB_REQ_GET_STATUS
;
742 req
.bRequestType
= USB_DIR_IN
|USB_RECIP_DEVICE
;
745 case 5: // get device qualifier (MAY STALL)
746 req
.wValue
= cpu_to_le16 (USB_DT_DEVICE_QUALIFIER
<< 8);
747 len
= sizeof (struct usb_qualifier_descriptor
);
748 if (udev
->speed
!= USB_SPEED_HIGH
)
751 case 6: // get first config descriptor, plus interface
752 req
.wValue
= cpu_to_le16 ((USB_DT_CONFIG
<< 8) | 0);
753 len
= sizeof (struct usb_config_descriptor
);
754 len
+= sizeof (struct usb_interface_descriptor
);
756 case 7: // get interface descriptor (ALWAYS STALLS)
757 req
.wValue
= cpu_to_le16 (USB_DT_INTERFACE
<< 8);
759 len
= sizeof (struct usb_interface_descriptor
);
762 // NOTE: two consecutive stalls in the queue here.
763 // that tests fault recovery a bit more aggressively.
764 case 8: // clear endpoint halt (USUALLY STALLS)
765 req
.bRequest
= USB_REQ_CLEAR_FEATURE
;
766 req
.bRequestType
= USB_RECIP_ENDPOINT
;
767 // wValue 0 == ep halt
768 // wIndex 0 == ep0 (shouldn't halt!)
770 pipe
= usb_sndctrlpipe (udev
, 0);
773 case 9: // get endpoint status
774 req
.bRequest
= USB_REQ_GET_STATUS
;
775 req
.bRequestType
= USB_DIR_IN
|USB_RECIP_ENDPOINT
;
779 case 10: // trigger short read (EREMOTEIO)
780 req
.wValue
= cpu_to_le16 ((USB_DT_CONFIG
<< 8) | 0);
782 expected
= -EREMOTEIO
;
784 // NOTE: two consecutive _different_ faults in the queue.
785 case 11: // get endpoint descriptor (ALWAYS STALLS)
786 req
.wValue
= cpu_to_le16 (USB_DT_ENDPOINT
<< 8);
788 len
= sizeof (struct usb_interface_descriptor
);
791 // NOTE: sometimes even a third fault in the queue!
792 case 12: // get string 0 descriptor (MAY STALL)
793 req
.wValue
= cpu_to_le16 (USB_DT_STRING
<< 8);
794 // string == 0, for language IDs
795 len
= sizeof (struct usb_interface_descriptor
);
796 expected
= EREMOTEIO
; // or EPIPE, if no strings
799 err ("bogus number of ctrl queue testcases!");
800 context
.status
= -EINVAL
;
803 req
.wLength
= cpu_to_le16 (len
);
804 urb
[i
] = u
= simple_alloc_urb (udev
, pipe
, len
);
808 reqp
= usb_buffer_alloc (udev
, sizeof *reqp
, SLAB_KERNEL
,
813 reqp
->number
= i
% NUM_SUBCASES
;
814 reqp
->expected
= expected
;
815 u
->setup_packet
= (char *) &reqp
->setup
;
817 u
->context
= &context
;
818 u
->complete
= ctrl_complete
;
819 u
->transfer_flags
|= URB_ASYNC_UNLINK
;
824 spin_lock_irq (&context
.lock
);
825 for (i
= 0; i
< param
->sglen
; i
++) {
826 context
.status
= usb_submit_urb (urb
[i
], SLAB_ATOMIC
);
827 if (context
.status
!= 0) {
828 dbg ("can't submit urb[%d], status %d",
830 context
.count
= context
.pending
;
835 spin_unlock_irq (&context
.lock
);
837 /* FIXME set timer and time out; provide a disconnect hook */
839 /* wait for the last one to complete */
840 wait_for_completion (&context
.complete
);
843 for (i
= 0; i
< param
->sglen
; i
++) {
847 if (urb
[i
]->setup_packet
)
848 usb_buffer_free (udev
, sizeof (struct usb_ctrlrequest
),
849 urb
[i
]->setup_packet
,
851 simple_free_urb (urb
[i
]);
854 return context
.status
;
859 /*-------------------------------------------------------------------------*/
861 static void unlink1_callback (struct urb
*urb
, struct pt_regs
*regs
)
863 int status
= urb
->status
;
865 // we "know" -EPIPE (stall) never happens
867 status
= usb_submit_urb (urb
, SLAB_ATOMIC
);
869 if (status
== -ECONNRESET
|| status
== -ENOENT
)
871 urb
->status
= status
;
872 complete ((struct completion
*) urb
->context
);
876 static int unlink1 (struct usbtest_dev
*dev
, int pipe
, int size
, int async
)
879 struct completion completion
;
882 init_completion (&completion
);
883 urb
= simple_alloc_urb (testdev_to_usbdev (dev
), pipe
, size
);
887 urb
->transfer_flags
|= URB_ASYNC_UNLINK
;
888 urb
->context
= &completion
;
889 urb
->complete
= unlink1_callback
;
891 /* keep the endpoint busy. there are lots of hc/hcd-internal
892 * states, and testing should get to all of them over time.
894 * FIXME want additional tests for when endpoint is STALLing
895 * due to errors, or is just NAKing requests.
897 if ((retval
= usb_submit_urb (urb
, SLAB_KERNEL
)) != 0) {
898 dbg ("submit/unlink fail %d", retval
);
902 /* unlinking that should always work. variable delay tests more
903 * hcd states and code paths, even with little other system load.
905 wait_ms (jiffies
% (2 * INTERRUPT_RATE
));
907 retval
= usb_unlink_urb (urb
);
908 if (retval
== -EBUSY
) {
909 /* we can't unlink urbs while they're completing.
910 * "normal" drivers would prevent resubmission, but
911 * since we're testing unlink paths, we can't.
913 dbg ("unlink retry");
916 if (!(retval
== 0 || retval
== -EINPROGRESS
)) {
917 dbg ("submit/unlink fail %d", retval
);
921 wait_for_completion (&completion
);
922 retval
= urb
->status
;
923 simple_free_urb (urb
);
927 static int unlink_simple (struct usbtest_dev
*dev
, int pipe
, int len
)
931 /* test sync and async paths */
932 retval
= unlink1 (dev
, pipe
, len
, 1);
934 retval
= unlink1 (dev
, pipe
, len
, 0);
938 /*-------------------------------------------------------------------------*/
940 /* We only have this one interface to user space, through usbfs.
941 * User mode code can scan usbfs to find N different devices (maybe on
942 * different busses) to use when testing, and allocate one thread per
943 * test. So discovery is simplified, and we have no device naming issues.
945 * Don't use these only as stress/load tests. Use them along with with
946 * other USB bus activity: plugging, unplugging, mousing, mp3 playback,
947 * video capture, and so on. Run different tests at different times, in
948 * different sequences. Nothing here should interact with other devices,
949 * except indirectly by consuming USB bandwidth and CPU resources for test
950 * threads and request completion. But the only way to know that for sure
951 * is to test when HC queues are in use by many devices.
955 usbtest_ioctl (struct usb_interface
*intf
, unsigned int code
, void *buf
)
957 struct usbtest_dev
*dev
= usb_get_intfdata (intf
);
958 struct usb_device
*udev
= testdev_to_usbdev (dev
);
959 struct usbtest_param
*param
= buf
;
960 int retval
= -EOPNOTSUPP
;
962 struct scatterlist
*sg
;
963 struct usb_sg_request req
;
964 struct timeval start
;
967 // FIXME USBDEVFS_CONNECTINFO doesn't say how fast the device is.
969 if (code
!= USBTEST_REQUEST
)
972 if (param
->iterations
<= 0 || param
->length
< 0
973 || param
->sglen
< 0 || param
->vary
< 0)
976 if (down_interruptible (&dev
->sem
))
979 /* some devices, like ez-usb default devices, need a non-default
980 * altsetting to have any active endpoints. some tests change
981 * altsettings; force a default so most tests don't need to check.
983 if (dev
->info
->alt
>= 0) {
986 if (intf
->altsetting
->desc
.bInterfaceNumber
) {
990 res
= set_altsetting (dev
, dev
->info
->alt
);
992 err ("%s: set altsetting to %d failed, %d",
993 dev
->id
, dev
->info
->alt
, res
);
1000 * Just a bunch of test cases that every HCD is expected to handle.
1002 * Some may need specific firmware, though it'd be good to have
1003 * one firmware image to handle all the test cases.
1005 * FIXME add more tests! cancel requests, verify the data, control
1006 * queueing, concurrent read+write threads, and so on.
1008 do_gettimeofday (&start
);
1009 switch (param
->test_num
) {
1012 dbg ("%s TEST 0: NOP", dev
->id
);
1016 /* Simple non-queued bulk I/O tests */
1018 if (dev
->out_pipe
== 0)
1020 dbg ("%s TEST 1: write %d bytes %u times", dev
->id
,
1021 param
->length
, param
->iterations
);
1022 urb
= simple_alloc_urb (udev
, dev
->out_pipe
, param
->length
);
1027 // FIRMWARE: bulk sink (maybe accepts short writes)
1028 retval
= simple_io (urb
, param
->iterations
, 0);
1029 simple_free_urb (urb
);
1032 if (dev
->in_pipe
== 0)
1034 dbg ("%s TEST 2: read %d bytes %u times", dev
->id
,
1035 param
->length
, param
->iterations
);
1036 urb
= simple_alloc_urb (udev
, dev
->in_pipe
, param
->length
);
1041 // FIRMWARE: bulk source (maybe generates short writes)
1042 retval
= simple_io (urb
, param
->iterations
, 0);
1043 simple_free_urb (urb
);
1046 if (dev
->out_pipe
== 0 || param
->vary
== 0)
1048 dbg ("%s TEST 3: write/%d 0..%d bytes %u times", dev
->id
,
1049 param
->vary
, param
->length
, param
->iterations
);
1050 urb
= simple_alloc_urb (udev
, dev
->out_pipe
, param
->length
);
1055 // FIRMWARE: bulk sink (maybe accepts short writes)
1056 retval
= simple_io (urb
, param
->iterations
, param
->vary
);
1057 simple_free_urb (urb
);
1060 if (dev
->in_pipe
== 0 || param
->vary
== 0)
1062 dbg ("%s TEST 4: read/%d 0..%d bytes %u times", dev
->id
,
1063 param
->vary
, param
->length
, param
->iterations
);
1064 urb
= simple_alloc_urb (udev
, dev
->in_pipe
, param
->length
);
1069 // FIRMWARE: bulk source (maybe generates short writes)
1070 retval
= simple_io (urb
, param
->iterations
, param
->vary
);
1071 simple_free_urb (urb
);
1074 /* Queued bulk I/O tests */
1076 if (dev
->out_pipe
== 0 || param
->sglen
== 0)
1078 dbg ("%s TEST 5: write %d sglists, %d entries of %d bytes",
1079 dev
->id
, param
->iterations
,
1080 param
->sglen
, param
->length
);
1081 sg
= alloc_sglist (param
->sglen
, param
->length
, 0);
1086 // FIRMWARE: bulk sink (maybe accepts short writes)
1087 retval
= perform_sglist (udev
, param
->iterations
, dev
->out_pipe
,
1088 &req
, sg
, param
->sglen
);
1089 free_sglist (sg
, param
->sglen
);
1093 if (dev
->in_pipe
== 0 || param
->sglen
== 0)
1095 dbg ("%s TEST 6: read %d sglists, %d entries of %d bytes",
1096 dev
->id
, param
->iterations
,
1097 param
->sglen
, param
->length
);
1098 sg
= alloc_sglist (param
->sglen
, param
->length
, 0);
1103 // FIRMWARE: bulk source (maybe generates short writes)
1104 retval
= perform_sglist (udev
, param
->iterations
, dev
->in_pipe
,
1105 &req
, sg
, param
->sglen
);
1106 free_sglist (sg
, param
->sglen
);
1109 if (dev
->out_pipe
== 0 || param
->sglen
== 0 || param
->vary
== 0)
1111 dbg ("%s TEST 7: write/%d %d sglists, %d entries 0..%d bytes",
1112 dev
->id
, param
->vary
, param
->iterations
,
1113 param
->sglen
, param
->length
);
1114 sg
= alloc_sglist (param
->sglen
, param
->length
, param
->vary
);
1119 // FIRMWARE: bulk sink (maybe accepts short writes)
1120 retval
= perform_sglist (udev
, param
->iterations
, dev
->out_pipe
,
1121 &req
, sg
, param
->sglen
);
1122 free_sglist (sg
, param
->sglen
);
1125 if (dev
->in_pipe
== 0 || param
->sglen
== 0 || param
->vary
== 0)
1127 dbg ("%s TEST 8: read/%d %d sglists, %d entries 0..%d bytes",
1128 dev
->id
, param
->vary
, param
->iterations
,
1129 param
->sglen
, param
->length
);
1130 sg
= alloc_sglist (param
->sglen
, param
->length
, param
->vary
);
1135 // FIRMWARE: bulk source (maybe generates short writes)
1136 retval
= perform_sglist (udev
, param
->iterations
, dev
->in_pipe
,
1137 &req
, sg
, param
->sglen
);
1138 free_sglist (sg
, param
->sglen
);
1141 /* non-queued sanity tests for control (chapter 9 subset) */
1144 dbg ("%s TEST 9: ch9 (subset) control tests, %d times",
1145 dev
->id
, param
->iterations
);
1146 for (i
= param
->iterations
; retval
== 0 && i
--; /* NOP */)
1147 retval
= ch9_postconfig (dev
);
1149 dbg ("ch9 subset failed, iterations left %d", i
);
1152 /* queued control messaging */
1154 if (param
->sglen
== 0)
1157 dbg ("%s TEST 10: queue %d control calls, %d times",
1158 dev
->id
, param
->sglen
,
1160 retval
= test_ctrl_queue (dev
, param
);
1163 /* simple non-queued unlinks (ring with one urb) */
1165 if (dev
->in_pipe
== 0 || !param
->length
)
1168 dbg ("%s TEST 11: unlink %d reads of %d",
1169 dev
->id
, param
->iterations
, param
->length
);
1170 for (i
= param
->iterations
; retval
== 0 && i
--; /* NOP */)
1171 retval
= unlink_simple (dev
, dev
->in_pipe
, param
->length
);
1173 dbg ("unlink reads failed, iterations left %d", i
);
1176 if (dev
->out_pipe
== 0 || !param
->length
)
1179 dbg ("%s TEST 12: unlink %d writes of %d",
1180 dev
->id
, param
->iterations
, param
->length
);
1181 for (i
= param
->iterations
; retval
== 0 && i
--; /* NOP */)
1182 retval
= unlink_simple (dev
, dev
->out_pipe
, param
->length
);
1184 dbg ("unlink writes failed, iterations left %d", i
);
1187 // FIXME unlink from queue (ring with N urbs)
1189 // FIXME scatterlist cancel (needs helper thread)
1192 do_gettimeofday (¶m
->duration
);
1193 param
->duration
.tv_sec
-= start
.tv_sec
;
1194 param
->duration
.tv_usec
-= start
.tv_usec
;
1195 if (param
->duration
.tv_usec
< 0) {
1196 param
->duration
.tv_usec
+= 1000 * 1000;
1197 param
->duration
.tv_sec
-= 1;
1203 /*-------------------------------------------------------------------------*/
1205 static int force_interrupt
= 0;
1206 MODULE_PARM (force_interrupt
, "i");
1207 MODULE_PARM_DESC (force_interrupt
, "0 = test bulk (default), else interrupt");
1211 MODULE_PARM (vendor
, "h");
1212 MODULE_PARM_DESC (vendor
, "vendor code (from usb-if)");
1215 MODULE_PARM (product
, "h");
1216 MODULE_PARM_DESC (product
, "product code (from vendor)");
1220 usbtest_probe (struct usb_interface
*intf
, const struct usb_device_id
*id
)
1222 struct usb_device
*udev
;
1223 struct usbtest_dev
*dev
;
1224 struct usbtest_info
*info
;
1225 char *rtest
, *wtest
;
1227 udev
= interface_to_usbdev (intf
);
1230 /* specify devices by module parameters? */
1231 if (id
->match_flags
== 0) {
1232 /* vendor match required, product match optional */
1233 if (!vendor
|| udev
->descriptor
.idVendor
!= (u16
)vendor
)
1235 if (product
&& udev
->descriptor
.idProduct
!= (u16
)product
)
1237 dbg ("matched module params, vend=0x%04x prod=0x%04x",
1238 udev
->descriptor
.idVendor
,
1239 udev
->descriptor
.idProduct
);
1243 dev
= kmalloc (sizeof *dev
, SLAB_KERNEL
);
1246 memset (dev
, 0, sizeof *dev
);
1247 info
= (struct usbtest_info
*) id
->driver_info
;
1249 init_MUTEX (&dev
->sem
);
1251 /* use the same kind of id the hid driver shows */
1252 snprintf (dev
->id
, sizeof dev
->id
, "%s-%s:%d",
1253 udev
->bus
->bus_name
, udev
->devpath
,
1254 intf
->altsetting
[0].desc
.bInterfaceNumber
);
1257 /* cacheline-aligned scratch for i/o */
1258 if ((dev
->buf
= kmalloc (TBUF_SIZE
, SLAB_KERNEL
)) == 0) {
1263 /* NOTE this doesn't yet test the handful of difference that are
1264 * visible with high speed interrupts: bigger maxpacket (1K) and
1265 * "high bandwidth" modes (up to 3 packets/uframe).
1268 if (force_interrupt
|| udev
->speed
== USB_SPEED_LOW
) {
1270 dev
->in_pipe
= usb_rcvintpipe (udev
, info
->ep_in
);
1274 dev
->out_pipe
= usb_sndintpipe (udev
, info
->ep_out
);
1275 wtest
= " intr-out";
1279 dev
->in_pipe
= usb_rcvbulkpipe (udev
, info
->ep_in
);
1283 dev
->out_pipe
= usb_sndbulkpipe (udev
, info
->ep_out
);
1284 wtest
= " bulk-out";
1288 usb_set_intfdata (intf
, dev
);
1289 info ("%s at %s ... %s speed {control%s%s} tests",
1290 info
->name
, dev
->id
,
1292 switch (udev
->speed
) {
1293 case USB_SPEED_LOW
: tmp
= "low"; break;
1294 case USB_SPEED_FULL
: tmp
= "full"; break;
1295 case USB_SPEED_HIGH
: tmp
= "high"; break;
1296 default: tmp
= "unknown"; break;
1297 }; tmp
; }), rtest
, wtest
);
1301 static void usbtest_disconnect (struct usb_interface
*intf
)
1303 struct usbtest_dev
*dev
= usb_get_intfdata (intf
);
1307 usb_set_intfdata (intf
, NULL
);
1308 info ("unbound %s", dev
->id
);
1311 /* Basic testing only needs a device that can source or sink bulk traffic.
1312 * Any device can test control transfers (default with GENERIC binding).
1314 * Several entries work with the default EP0 implementation that's built
1315 * into EZ-USB chips. There's a default vendor ID which can be overridden
1316 * by (very) small config EEPROMS, but otherwise all these devices act
1317 * identically until firmware is loaded: only EP0 works. It turns out
1318 * to be easy to make other endpoints work, without modifying that EP0
1319 * behavior. For now, we expect that kind of firmware.
1322 /* an21xx or fx versions of ez-usb */
1323 static struct usbtest_info ez1_info
= {
1324 .name
= "EZ-USB device",
1330 /* fx2 version of ez-usb */
1331 static struct usbtest_info ez2_info
= {
1332 .name
= "FX2 device",
1338 /* ezusb family device with dedicated usb test firmware,
1339 * or a peripheral running Linux and 'zero.c' test firmware.
1341 * FIXME usbtest should read the descriptors, since compatible
1342 * test firmware might run on hardware (pxa250 for one) that
1343 * can't configure an ep2in-bulk.
1345 static struct usbtest_info fw_info
= {
1346 .name
= "usb test device",
1352 static struct usbtest_info um_info
= {
1353 .name
= "user mode test driver",
1360 /* this is a nice source of high speed bulk data;
1361 * uses an FX2, with firmware provided in the device
1363 static struct usbtest_info ibot2_info
= {
1364 .name
= "iBOT2 webcam",
1371 /* we can use any device to test control traffic */
1372 static struct usbtest_info generic_info
= {
1373 .name
= "Generic USB device",
1378 // FIXME remove this
1379 static struct usbtest_info hact_info
= {
1387 static struct usb_device_id id_table
[] = {
1389 { USB_DEVICE (0x0547, 0x1002),
1390 .driver_info
= (unsigned long) &hact_info
,
1393 /*-------------------------------------------------------------*/
1395 /* EZ-USB devices which download firmware to replace (or in our
1396 * case augment) the default device implementation.
1399 /* generic EZ-USB FX controller */
1400 { USB_DEVICE (0x0547, 0x2235),
1401 .driver_info
= (unsigned long) &ez1_info
,
1404 /* CY3671 development board with EZ-USB FX */
1405 { USB_DEVICE (0x0547, 0x0080),
1406 .driver_info
= (unsigned long) &ez1_info
,
1409 /* generic EZ-USB FX2 controller (or development board) */
1410 { USB_DEVICE (0x04b4, 0x8613),
1411 .driver_info
= (unsigned long) &ez2_info
,
1414 /* re-enumerated usb test device firmware */
1415 { USB_DEVICE (0xfff0, 0xfff0),
1416 .driver_info
= (unsigned long) &fw_info
,
1419 /* "Gadget Zero" firmware runs under Linux */
1420 { USB_DEVICE (0x0525, 0xa4a0),
1421 .driver_info
= (unsigned long) &fw_info
,
1424 /* so does a user-mode variant */
1425 { USB_DEVICE (0x0525, 0xa4a4),
1426 .driver_info
= (unsigned long) &um_info
,
1430 /* Keyspan 19qi uses an21xx (original EZ-USB) */
1431 // this does not coexist with the real Keyspan 19qi driver!
1432 { USB_DEVICE (0x06cd, 0x010b),
1433 .driver_info
= (unsigned long) &ez1_info
,
1437 /*-------------------------------------------------------------*/
1440 /* iBOT2 makes a nice source of high speed bulk-in data */
1441 // this does not coexist with a real iBOT2 driver!
1442 { USB_DEVICE (0x0b62, 0x0059),
1443 .driver_info
= (unsigned long) &ibot2_info
,
1447 /*-------------------------------------------------------------*/
1450 /* module params can specify devices to use for control tests */
1451 { .driver_info
= (unsigned long) &generic_info
, },
1454 /*-------------------------------------------------------------*/
1458 MODULE_DEVICE_TABLE (usb
, id_table
);
1460 static struct usb_driver usbtest_driver
= {
1461 .owner
= THIS_MODULE
,
1463 .id_table
= id_table
,
1464 .probe
= usbtest_probe
,
1465 .ioctl
= usbtest_ioctl
,
1466 .disconnect
= usbtest_disconnect
,
1469 /*-------------------------------------------------------------------------*/
1471 static int __init
usbtest_init (void)
1475 dbg ("params: vend=0x%04x prod=0x%04x", vendor
, product
);
1477 return usb_register (&usbtest_driver
);
1479 module_init (usbtest_init
);
1481 static void __exit
usbtest_exit (void)
1483 usb_deregister (&usbtest_driver
);
1485 module_exit (usbtest_exit
);
1487 MODULE_DESCRIPTION ("USB Core/HCD Testing Driver");
1488 MODULE_LICENSE ("GPL");