11da177e4SLinus Torvalds #include <linux/kernel.h> 21da177e4SLinus Torvalds #include <linux/errno.h> 31da177e4SLinus Torvalds #include <linux/init.h> 41da177e4SLinus Torvalds #include <linux/slab.h> 51da177e4SLinus Torvalds #include <linux/mm.h> 61da177e4SLinus Torvalds #include <linux/module.h> 71da177e4SLinus Torvalds #include <linux/moduleparam.h> 8378f058cSDavid Hardeman #include <linux/scatterlist.h> 91da177e4SLinus Torvalds 101da177e4SLinus Torvalds #include <linux/usb.h> 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds // FIXME make these public somewhere; usbdevfs.h? 161da177e4SLinus Torvalds // 171da177e4SLinus Torvalds struct usbtest_param { 181da177e4SLinus Torvalds // inputs 191da177e4SLinus Torvalds unsigned test_num; /* 0..(TEST_CASES-1) */ 201da177e4SLinus Torvalds unsigned iterations; 211da177e4SLinus Torvalds unsigned length; 221da177e4SLinus Torvalds unsigned vary; 231da177e4SLinus Torvalds unsigned sglen; 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds // outputs 261da177e4SLinus Torvalds struct timeval duration; 271da177e4SLinus Torvalds }; 281da177e4SLinus Torvalds #define USBTEST_REQUEST _IOWR('U', 100, struct usbtest_param) 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds #define GENERIC /* let probe() bind using module params */ 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds /* Some devices that can be used for testing will have "real" drivers. 351da177e4SLinus Torvalds * Entries for those need to be enabled here by hand, after disabling 361da177e4SLinus Torvalds * that "real" driver. 371da177e4SLinus Torvalds */ 381da177e4SLinus Torvalds //#define IBOT2 /* grab iBOT2 webcams */ 391da177e4SLinus Torvalds //#define KEYSPAN_19Qi /* grab un-renumerated serial adapter */ 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds struct usbtest_info { 441da177e4SLinus Torvalds const char *name; 451da177e4SLinus Torvalds u8 ep_in; /* bulk/intr source */ 461da177e4SLinus Torvalds u8 ep_out; /* bulk/intr sink */ 471da177e4SLinus Torvalds unsigned autoconf : 1; 481da177e4SLinus Torvalds unsigned ctrl_out : 1; 491da177e4SLinus Torvalds unsigned iso : 1; /* try iso in/out */ 501da177e4SLinus Torvalds int alt; 511da177e4SLinus Torvalds }; 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds /* this is accessed only through usbfs ioctl calls. 541da177e4SLinus Torvalds * one ioctl to issue a test ... one lock per device. 551da177e4SLinus Torvalds * tests create other threads if they need them. 561da177e4SLinus Torvalds * urbs and buffers are allocated dynamically, 571da177e4SLinus Torvalds * and data generated deterministically. 581da177e4SLinus Torvalds */ 591da177e4SLinus Torvalds struct usbtest_dev { 601da177e4SLinus Torvalds struct usb_interface *intf; 611da177e4SLinus Torvalds struct usbtest_info *info; 621da177e4SLinus Torvalds int in_pipe; 631da177e4SLinus Torvalds int out_pipe; 641da177e4SLinus Torvalds int in_iso_pipe; 651da177e4SLinus Torvalds int out_iso_pipe; 661da177e4SLinus Torvalds struct usb_endpoint_descriptor *iso_in, *iso_out; 671da177e4SLinus Torvalds struct semaphore sem; 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds #define TBUF_SIZE 256 701da177e4SLinus Torvalds u8 *buf; 711da177e4SLinus Torvalds }; 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds static struct usb_device *testdev_to_usbdev (struct usbtest_dev *test) 741da177e4SLinus Torvalds { 751da177e4SLinus Torvalds return interface_to_usbdev (test->intf); 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds /* set up all urbs so they can be used with either bulk or interrupt */ 791da177e4SLinus Torvalds #define INTERRUPT_RATE 1 /* msec/transfer */ 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds #define xprintk(tdev,level,fmt,args...) \ 821da177e4SLinus Torvalds dev_printk(level , &(tdev)->intf->dev , fmt , ## args) 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds #ifdef DEBUG 851da177e4SLinus Torvalds #define DBG(dev,fmt,args...) \ 861da177e4SLinus Torvalds xprintk(dev , KERN_DEBUG , fmt , ## args) 871da177e4SLinus Torvalds #else 881da177e4SLinus Torvalds #define DBG(dev,fmt,args...) \ 891da177e4SLinus Torvalds do { } while (0) 901da177e4SLinus Torvalds #endif /* DEBUG */ 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds #ifdef VERBOSE 931da177e4SLinus Torvalds #define VDBG DBG 941da177e4SLinus Torvalds #else 951da177e4SLinus Torvalds #define VDBG(dev,fmt,args...) \ 961da177e4SLinus Torvalds do { } while (0) 971da177e4SLinus Torvalds #endif /* VERBOSE */ 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds #define ERROR(dev,fmt,args...) \ 1001da177e4SLinus Torvalds xprintk(dev , KERN_ERR , fmt , ## args) 1011da177e4SLinus Torvalds #define WARN(dev,fmt,args...) \ 1021da177e4SLinus Torvalds xprintk(dev , KERN_WARNING , fmt , ## args) 1031da177e4SLinus Torvalds #define INFO(dev,fmt,args...) \ 1041da177e4SLinus Torvalds xprintk(dev , KERN_INFO , fmt , ## args) 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds static int 1091da177e4SLinus Torvalds get_endpoints (struct usbtest_dev *dev, struct usb_interface *intf) 1101da177e4SLinus Torvalds { 1111da177e4SLinus Torvalds int tmp; 1121da177e4SLinus Torvalds struct usb_host_interface *alt; 1131da177e4SLinus Torvalds struct usb_host_endpoint *in, *out; 1141da177e4SLinus Torvalds struct usb_host_endpoint *iso_in, *iso_out; 1151da177e4SLinus Torvalds struct usb_device *udev; 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds for (tmp = 0; tmp < intf->num_altsetting; tmp++) { 1181da177e4SLinus Torvalds unsigned ep; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds in = out = NULL; 1211da177e4SLinus Torvalds iso_in = iso_out = NULL; 1221da177e4SLinus Torvalds alt = intf->altsetting + tmp; 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds /* take the first altsetting with in-bulk + out-bulk; 1251da177e4SLinus Torvalds * ignore other endpoints and altsetttings. 1261da177e4SLinus Torvalds */ 1271da177e4SLinus Torvalds for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) { 1281da177e4SLinus Torvalds struct usb_host_endpoint *e; 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds e = alt->endpoint + ep; 1311da177e4SLinus Torvalds switch (e->desc.bmAttributes) { 1321da177e4SLinus Torvalds case USB_ENDPOINT_XFER_BULK: 1331da177e4SLinus Torvalds break; 1341da177e4SLinus Torvalds case USB_ENDPOINT_XFER_ISOC: 1351da177e4SLinus Torvalds if (dev->info->iso) 1361da177e4SLinus Torvalds goto try_iso; 1371da177e4SLinus Torvalds // FALLTHROUGH 1381da177e4SLinus Torvalds default: 1391da177e4SLinus Torvalds continue; 1401da177e4SLinus Torvalds } 1414d823dd2SLuiz Fernando N. Capitulino if (usb_endpoint_dir_in(&e->desc)) { 1421da177e4SLinus Torvalds if (!in) 1431da177e4SLinus Torvalds in = e; 1441da177e4SLinus Torvalds } else { 1451da177e4SLinus Torvalds if (!out) 1461da177e4SLinus Torvalds out = e; 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds continue; 1491da177e4SLinus Torvalds try_iso: 1504d823dd2SLuiz Fernando N. Capitulino if (usb_endpoint_dir_in(&e->desc)) { 1511da177e4SLinus Torvalds if (!iso_in) 1521da177e4SLinus Torvalds iso_in = e; 1531da177e4SLinus Torvalds } else { 1541da177e4SLinus Torvalds if (!iso_out) 1551da177e4SLinus Torvalds iso_out = e; 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds if ((in && out) || (iso_in && iso_out)) 1591da177e4SLinus Torvalds goto found; 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds return -EINVAL; 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds found: 1641da177e4SLinus Torvalds udev = testdev_to_usbdev (dev); 1651da177e4SLinus Torvalds if (alt->desc.bAlternateSetting != 0) { 1661da177e4SLinus Torvalds tmp = usb_set_interface (udev, 1671da177e4SLinus Torvalds alt->desc.bInterfaceNumber, 1681da177e4SLinus Torvalds alt->desc.bAlternateSetting); 1691da177e4SLinus Torvalds if (tmp < 0) 1701da177e4SLinus Torvalds return tmp; 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds if (in) { 1741da177e4SLinus Torvalds dev->in_pipe = usb_rcvbulkpipe (udev, 1751da177e4SLinus Torvalds in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); 1761da177e4SLinus Torvalds dev->out_pipe = usb_sndbulkpipe (udev, 1771da177e4SLinus Torvalds out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds if (iso_in) { 1801da177e4SLinus Torvalds dev->iso_in = &iso_in->desc; 1811da177e4SLinus Torvalds dev->in_iso_pipe = usb_rcvisocpipe (udev, 1821da177e4SLinus Torvalds iso_in->desc.bEndpointAddress 1831da177e4SLinus Torvalds & USB_ENDPOINT_NUMBER_MASK); 1841da177e4SLinus Torvalds dev->iso_out = &iso_out->desc; 1851da177e4SLinus Torvalds dev->out_iso_pipe = usb_sndisocpipe (udev, 1861da177e4SLinus Torvalds iso_out->desc.bEndpointAddress 1871da177e4SLinus Torvalds & USB_ENDPOINT_NUMBER_MASK); 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds return 0; 1901da177e4SLinus Torvalds } 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds /* Support for testing basic non-queued I/O streams. 1951da177e4SLinus Torvalds * 1961da177e4SLinus Torvalds * These just package urbs as requests that can be easily canceled. 1971da177e4SLinus Torvalds * Each urb's data buffer is dynamically allocated; callers can fill 1981da177e4SLinus Torvalds * them with non-zero test data (or test for it) when appropriate. 1991da177e4SLinus Torvalds */ 2001da177e4SLinus Torvalds 2017d12e780SDavid Howells static void simple_callback (struct urb *urb) 2021da177e4SLinus Torvalds { 2031da177e4SLinus Torvalds complete ((struct completion *) urb->context); 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds static struct urb *simple_alloc_urb ( 2071da177e4SLinus Torvalds struct usb_device *udev, 2081da177e4SLinus Torvalds int pipe, 2091da177e4SLinus Torvalds unsigned long bytes 2101da177e4SLinus Torvalds ) 2111da177e4SLinus Torvalds { 2121da177e4SLinus Torvalds struct urb *urb; 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds if (bytes < 0) 2151da177e4SLinus Torvalds return NULL; 2161da177e4SLinus Torvalds urb = usb_alloc_urb (0, SLAB_KERNEL); 2171da177e4SLinus Torvalds if (!urb) 2181da177e4SLinus Torvalds return urb; 2191da177e4SLinus Torvalds usb_fill_bulk_urb (urb, udev, pipe, NULL, bytes, simple_callback, NULL); 2201da177e4SLinus Torvalds urb->interval = (udev->speed == USB_SPEED_HIGH) 2211da177e4SLinus Torvalds ? (INTERRUPT_RATE << 3) 2221da177e4SLinus Torvalds : INTERRUPT_RATE; 2231da177e4SLinus Torvalds urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; 2241da177e4SLinus Torvalds if (usb_pipein (pipe)) 2251da177e4SLinus Torvalds urb->transfer_flags |= URB_SHORT_NOT_OK; 2261da177e4SLinus Torvalds urb->transfer_buffer = usb_buffer_alloc (udev, bytes, SLAB_KERNEL, 2271da177e4SLinus Torvalds &urb->transfer_dma); 2281da177e4SLinus Torvalds if (!urb->transfer_buffer) { 2291da177e4SLinus Torvalds usb_free_urb (urb); 2301da177e4SLinus Torvalds urb = NULL; 2311da177e4SLinus Torvalds } else 2321da177e4SLinus Torvalds memset (urb->transfer_buffer, 0, bytes); 2331da177e4SLinus Torvalds return urb; 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds static unsigned pattern = 0; 2371da177e4SLinus Torvalds module_param (pattern, uint, S_IRUGO); 2381da177e4SLinus Torvalds // MODULE_PARM_DESC (pattern, "i/o pattern (0 == zeroes)"); 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds static inline void simple_fill_buf (struct urb *urb) 2411da177e4SLinus Torvalds { 2421da177e4SLinus Torvalds unsigned i; 2431da177e4SLinus Torvalds u8 *buf = urb->transfer_buffer; 2441da177e4SLinus Torvalds unsigned len = urb->transfer_buffer_length; 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds switch (pattern) { 2471da177e4SLinus Torvalds default: 2481da177e4SLinus Torvalds // FALLTHROUGH 2491da177e4SLinus Torvalds case 0: 2501da177e4SLinus Torvalds memset (buf, 0, len); 2511da177e4SLinus Torvalds break; 2521da177e4SLinus Torvalds case 1: /* mod63 */ 2531da177e4SLinus Torvalds for (i = 0; i < len; i++) 2541da177e4SLinus Torvalds *buf++ = (u8) (i % 63); 2551da177e4SLinus Torvalds break; 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds static inline int simple_check_buf (struct urb *urb) 2601da177e4SLinus Torvalds { 2611da177e4SLinus Torvalds unsigned i; 2621da177e4SLinus Torvalds u8 expected; 2631da177e4SLinus Torvalds u8 *buf = urb->transfer_buffer; 2641da177e4SLinus Torvalds unsigned len = urb->actual_length; 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds for (i = 0; i < len; i++, buf++) { 2671da177e4SLinus Torvalds switch (pattern) { 2681da177e4SLinus Torvalds /* all-zeroes has no synchronization issues */ 2691da177e4SLinus Torvalds case 0: 2701da177e4SLinus Torvalds expected = 0; 2711da177e4SLinus Torvalds break; 2721da177e4SLinus Torvalds /* mod63 stays in sync with short-terminated transfers, 2731da177e4SLinus Torvalds * or otherwise when host and gadget agree on how large 2741da177e4SLinus Torvalds * each usb transfer request should be. resync is done 2751da177e4SLinus Torvalds * with set_interface or set_config. 2761da177e4SLinus Torvalds */ 2771da177e4SLinus Torvalds case 1: /* mod63 */ 2781da177e4SLinus Torvalds expected = i % 63; 2791da177e4SLinus Torvalds break; 2801da177e4SLinus Torvalds /* always fail unsupported patterns */ 2811da177e4SLinus Torvalds default: 2821da177e4SLinus Torvalds expected = !*buf; 2831da177e4SLinus Torvalds break; 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds if (*buf == expected) 2861da177e4SLinus Torvalds continue; 2871da177e4SLinus Torvalds dbg ("buf[%d] = %d (not %d)", i, *buf, expected); 2881da177e4SLinus Torvalds return -EINVAL; 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds return 0; 2911da177e4SLinus Torvalds } 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds static void simple_free_urb (struct urb *urb) 2941da177e4SLinus Torvalds { 2951da177e4SLinus Torvalds usb_buffer_free (urb->dev, urb->transfer_buffer_length, 2961da177e4SLinus Torvalds urb->transfer_buffer, urb->transfer_dma); 2971da177e4SLinus Torvalds usb_free_urb (urb); 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds static int simple_io ( 3011da177e4SLinus Torvalds struct urb *urb, 3021da177e4SLinus Torvalds int iterations, 3031da177e4SLinus Torvalds int vary, 3041da177e4SLinus Torvalds int expected, 3051da177e4SLinus Torvalds const char *label 3061da177e4SLinus Torvalds ) 3071da177e4SLinus Torvalds { 3081da177e4SLinus Torvalds struct usb_device *udev = urb->dev; 3091da177e4SLinus Torvalds int max = urb->transfer_buffer_length; 3101da177e4SLinus Torvalds struct completion completion; 3111da177e4SLinus Torvalds int retval = 0; 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds urb->context = &completion; 3141da177e4SLinus Torvalds while (retval == 0 && iterations-- > 0) { 3151da177e4SLinus Torvalds init_completion (&completion); 3161da177e4SLinus Torvalds if (usb_pipeout (urb->pipe)) 3171da177e4SLinus Torvalds simple_fill_buf (urb); 3181da177e4SLinus Torvalds if ((retval = usb_submit_urb (urb, SLAB_KERNEL)) != 0) 3191da177e4SLinus Torvalds break; 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds /* NOTE: no timeouts; can't be broken out of by interrupt */ 3221da177e4SLinus Torvalds wait_for_completion (&completion); 3231da177e4SLinus Torvalds retval = urb->status; 3241da177e4SLinus Torvalds urb->dev = udev; 3251da177e4SLinus Torvalds if (retval == 0 && usb_pipein (urb->pipe)) 3261da177e4SLinus Torvalds retval = simple_check_buf (urb); 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds if (vary) { 3291da177e4SLinus Torvalds int len = urb->transfer_buffer_length; 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds len += vary; 3321da177e4SLinus Torvalds len %= max; 3331da177e4SLinus Torvalds if (len == 0) 3341da177e4SLinus Torvalds len = (vary < max) ? vary : max; 3351da177e4SLinus Torvalds urb->transfer_buffer_length = len; 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds /* FIXME if endpoint halted, clear halt (and log) */ 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds urb->transfer_buffer_length = max; 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds if (expected != retval) 3431da177e4SLinus Torvalds dev_dbg (&udev->dev, 3441da177e4SLinus Torvalds "%s failed, iterations left %d, status %d (not %d)\n", 3451da177e4SLinus Torvalds label, iterations, retval, expected); 3461da177e4SLinus Torvalds return retval; 3471da177e4SLinus Torvalds } 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds /* We use scatterlist primitives to test queued I/O. 3531da177e4SLinus Torvalds * Yes, this also tests the scatterlist primitives. 3541da177e4SLinus Torvalds */ 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds static void free_sglist (struct scatterlist *sg, int nents) 3571da177e4SLinus Torvalds { 3581da177e4SLinus Torvalds unsigned i; 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds if (!sg) 3611da177e4SLinus Torvalds return; 3621da177e4SLinus Torvalds for (i = 0; i < nents; i++) { 3631da177e4SLinus Torvalds if (!sg [i].page) 3641da177e4SLinus Torvalds continue; 3651da177e4SLinus Torvalds kfree (page_address (sg [i].page) + sg [i].offset); 3661da177e4SLinus Torvalds } 3671da177e4SLinus Torvalds kfree (sg); 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds 3701da177e4SLinus Torvalds static struct scatterlist * 3711da177e4SLinus Torvalds alloc_sglist (int nents, int max, int vary) 3721da177e4SLinus Torvalds { 3731da177e4SLinus Torvalds struct scatterlist *sg; 3741da177e4SLinus Torvalds unsigned i; 3751da177e4SLinus Torvalds unsigned size = max; 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds sg = kmalloc (nents * sizeof *sg, SLAB_KERNEL); 3781da177e4SLinus Torvalds if (!sg) 3791da177e4SLinus Torvalds return NULL; 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds for (i = 0; i < nents; i++) { 3821da177e4SLinus Torvalds char *buf; 3838b524901SDavid Brownell unsigned j; 3841da177e4SLinus Torvalds 38580b6ca48SEric Sesterhenn buf = kzalloc (size, SLAB_KERNEL); 3861da177e4SLinus Torvalds if (!buf) { 3871da177e4SLinus Torvalds free_sglist (sg, i); 3881da177e4SLinus Torvalds return NULL; 3891da177e4SLinus Torvalds } 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds /* kmalloc pages are always physically contiguous! */ 392378f058cSDavid Hardeman sg_init_one(&sg[i], buf, size); 3931da177e4SLinus Torvalds 3948b524901SDavid Brownell switch (pattern) { 3958b524901SDavid Brownell case 0: 3968b524901SDavid Brownell /* already zeroed */ 3978b524901SDavid Brownell break; 3988b524901SDavid Brownell case 1: 3998b524901SDavid Brownell for (j = 0; j < size; j++) 4008b524901SDavid Brownell *buf++ = (u8) (j % 63); 4018b524901SDavid Brownell break; 4028b524901SDavid Brownell } 4038b524901SDavid Brownell 4041da177e4SLinus Torvalds if (vary) { 4051da177e4SLinus Torvalds size += vary; 4061da177e4SLinus Torvalds size %= max; 4071da177e4SLinus Torvalds if (size == 0) 4081da177e4SLinus Torvalds size = (vary < max) ? vary : max; 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds return sg; 4131da177e4SLinus Torvalds } 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds static int perform_sglist ( 4161da177e4SLinus Torvalds struct usb_device *udev, 4171da177e4SLinus Torvalds unsigned iterations, 4181da177e4SLinus Torvalds int pipe, 4191da177e4SLinus Torvalds struct usb_sg_request *req, 4201da177e4SLinus Torvalds struct scatterlist *sg, 4211da177e4SLinus Torvalds int nents 4221da177e4SLinus Torvalds ) 4231da177e4SLinus Torvalds { 4241da177e4SLinus Torvalds int retval = 0; 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds while (retval == 0 && iterations-- > 0) { 4271da177e4SLinus Torvalds retval = usb_sg_init (req, udev, pipe, 4281da177e4SLinus Torvalds (udev->speed == USB_SPEED_HIGH) 4291da177e4SLinus Torvalds ? (INTERRUPT_RATE << 3) 4301da177e4SLinus Torvalds : INTERRUPT_RATE, 4311da177e4SLinus Torvalds sg, nents, 0, SLAB_KERNEL); 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds if (retval) 4341da177e4SLinus Torvalds break; 4351da177e4SLinus Torvalds usb_sg_wait (req); 4361da177e4SLinus Torvalds retval = req->status; 4371da177e4SLinus Torvalds 4388b524901SDavid Brownell /* FIXME check resulting data pattern */ 4398b524901SDavid Brownell 4401da177e4SLinus Torvalds /* FIXME if endpoint halted, clear halt (and log) */ 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds // FIXME for unlink or fault handling tests, don't report 4441da177e4SLinus Torvalds // failure if retval is as we expected ... 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds if (retval) 4471da177e4SLinus Torvalds dbg ("perform_sglist failed, iterations left %d, status %d", 4481da177e4SLinus Torvalds iterations, retval); 4491da177e4SLinus Torvalds return retval; 4501da177e4SLinus Torvalds } 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds /* unqueued control message testing 4561da177e4SLinus Torvalds * 4571da177e4SLinus Torvalds * there's a nice set of device functional requirements in chapter 9 of the 4581da177e4SLinus Torvalds * usb 2.0 spec, which we can apply to ANY device, even ones that don't use 4591da177e4SLinus Torvalds * special test firmware. 4601da177e4SLinus Torvalds * 4611da177e4SLinus Torvalds * we know the device is configured (or suspended) by the time it's visible 4621da177e4SLinus Torvalds * through usbfs. we can't change that, so we won't test enumeration (which 4631da177e4SLinus Torvalds * worked 'well enough' to get here, this time), power management (ditto), 4641da177e4SLinus Torvalds * or remote wakeup (which needs human interaction). 4651da177e4SLinus Torvalds */ 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds static unsigned realworld = 1; 4681da177e4SLinus Torvalds module_param (realworld, uint, 0); 469ff7c79e4SDavid Brownell MODULE_PARM_DESC (realworld, "clear to demand stricter spec compliance"); 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds static int get_altsetting (struct usbtest_dev *dev) 4721da177e4SLinus Torvalds { 4731da177e4SLinus Torvalds struct usb_interface *iface = dev->intf; 4741da177e4SLinus Torvalds struct usb_device *udev = interface_to_usbdev (iface); 4751da177e4SLinus Torvalds int retval; 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds retval = usb_control_msg (udev, usb_rcvctrlpipe (udev, 0), 4781da177e4SLinus Torvalds USB_REQ_GET_INTERFACE, USB_DIR_IN|USB_RECIP_INTERFACE, 4791da177e4SLinus Torvalds 0, iface->altsetting [0].desc.bInterfaceNumber, 4801da177e4SLinus Torvalds dev->buf, 1, USB_CTRL_GET_TIMEOUT); 4811da177e4SLinus Torvalds switch (retval) { 4821da177e4SLinus Torvalds case 1: 4831da177e4SLinus Torvalds return dev->buf [0]; 4841da177e4SLinus Torvalds case 0: 4851da177e4SLinus Torvalds retval = -ERANGE; 4861da177e4SLinus Torvalds // FALLTHROUGH 4871da177e4SLinus Torvalds default: 4881da177e4SLinus Torvalds return retval; 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds static int set_altsetting (struct usbtest_dev *dev, int alternate) 4931da177e4SLinus Torvalds { 4941da177e4SLinus Torvalds struct usb_interface *iface = dev->intf; 4951da177e4SLinus Torvalds struct usb_device *udev; 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds if (alternate < 0 || alternate >= 256) 4981da177e4SLinus Torvalds return -EINVAL; 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds udev = interface_to_usbdev (iface); 5011da177e4SLinus Torvalds return usb_set_interface (udev, 5021da177e4SLinus Torvalds iface->altsetting [0].desc.bInterfaceNumber, 5031da177e4SLinus Torvalds alternate); 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds static int is_good_config (char *buf, int len) 5071da177e4SLinus Torvalds { 5081da177e4SLinus Torvalds struct usb_config_descriptor *config; 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds if (len < sizeof *config) 5111da177e4SLinus Torvalds return 0; 5121da177e4SLinus Torvalds config = (struct usb_config_descriptor *) buf; 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds switch (config->bDescriptorType) { 5151da177e4SLinus Torvalds case USB_DT_CONFIG: 5161da177e4SLinus Torvalds case USB_DT_OTHER_SPEED_CONFIG: 5171da177e4SLinus Torvalds if (config->bLength != 9) { 5181da177e4SLinus Torvalds dbg ("bogus config descriptor length"); 5191da177e4SLinus Torvalds return 0; 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds /* this bit 'must be 1' but often isn't */ 5221da177e4SLinus Torvalds if (!realworld && !(config->bmAttributes & 0x80)) { 5231da177e4SLinus Torvalds dbg ("high bit of config attributes not set"); 5241da177e4SLinus Torvalds return 0; 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds if (config->bmAttributes & 0x1f) { /* reserved == 0 */ 5271da177e4SLinus Torvalds dbg ("reserved config bits set"); 5281da177e4SLinus Torvalds return 0; 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds break; 5311da177e4SLinus Torvalds default: 5321da177e4SLinus Torvalds return 0; 5331da177e4SLinus Torvalds } 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds if (le16_to_cpu(config->wTotalLength) == len) /* read it all */ 5361da177e4SLinus Torvalds return 1; 5371da177e4SLinus Torvalds if (le16_to_cpu(config->wTotalLength) >= TBUF_SIZE) /* max partial read */ 5381da177e4SLinus Torvalds return 1; 5391da177e4SLinus Torvalds dbg ("bogus config descriptor read size"); 5401da177e4SLinus Torvalds return 0; 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds /* sanity test for standard requests working with usb_control_mesg() and some 5441da177e4SLinus Torvalds * of the utility functions which use it. 5451da177e4SLinus Torvalds * 5461da177e4SLinus Torvalds * this doesn't test how endpoint halts behave or data toggles get set, since 5471da177e4SLinus Torvalds * we won't do I/O to bulk/interrupt endpoints here (which is how to change 5481da177e4SLinus Torvalds * halt or toggle). toggle testing is impractical without support from hcds. 5491da177e4SLinus Torvalds * 5501da177e4SLinus Torvalds * this avoids failing devices linux would normally work with, by not testing 5511da177e4SLinus Torvalds * config/altsetting operations for devices that only support their defaults. 5521da177e4SLinus Torvalds * such devices rarely support those needless operations. 5531da177e4SLinus Torvalds * 5541da177e4SLinus Torvalds * NOTE that since this is a sanity test, it's not examining boundary cases 5551da177e4SLinus Torvalds * to see if usbcore, hcd, and device all behave right. such testing would 5561da177e4SLinus Torvalds * involve varied read sizes and other operation sequences. 5571da177e4SLinus Torvalds */ 5581da177e4SLinus Torvalds static int ch9_postconfig (struct usbtest_dev *dev) 5591da177e4SLinus Torvalds { 5601da177e4SLinus Torvalds struct usb_interface *iface = dev->intf; 5611da177e4SLinus Torvalds struct usb_device *udev = interface_to_usbdev (iface); 5621da177e4SLinus Torvalds int i, alt, retval; 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds /* [9.2.3] if there's more than one altsetting, we need to be able to 5651da177e4SLinus Torvalds * set and get each one. mostly trusts the descriptors from usbcore. 5661da177e4SLinus Torvalds */ 5671da177e4SLinus Torvalds for (i = 0; i < iface->num_altsetting; i++) { 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds /* 9.2.3 constrains the range here */ 5701da177e4SLinus Torvalds alt = iface->altsetting [i].desc.bAlternateSetting; 5711da177e4SLinus Torvalds if (alt < 0 || alt >= iface->num_altsetting) { 5721da177e4SLinus Torvalds dev_dbg (&iface->dev, 5731da177e4SLinus Torvalds "invalid alt [%d].bAltSetting = %d\n", 5741da177e4SLinus Torvalds i, alt); 5751da177e4SLinus Torvalds } 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds /* [real world] get/set unimplemented if there's only one */ 5781da177e4SLinus Torvalds if (realworld && iface->num_altsetting == 1) 5791da177e4SLinus Torvalds continue; 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds /* [9.4.10] set_interface */ 5821da177e4SLinus Torvalds retval = set_altsetting (dev, alt); 5831da177e4SLinus Torvalds if (retval) { 5841da177e4SLinus Torvalds dev_dbg (&iface->dev, "can't set_interface = %d, %d\n", 5851da177e4SLinus Torvalds alt, retval); 5861da177e4SLinus Torvalds return retval; 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds /* [9.4.4] get_interface always works */ 5901da177e4SLinus Torvalds retval = get_altsetting (dev); 5911da177e4SLinus Torvalds if (retval != alt) { 5921da177e4SLinus Torvalds dev_dbg (&iface->dev, "get alt should be %d, was %d\n", 5931da177e4SLinus Torvalds alt, retval); 5941da177e4SLinus Torvalds return (retval < 0) ? retval : -EDOM; 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds } 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds /* [real world] get_config unimplemented if there's only one */ 6001da177e4SLinus Torvalds if (!realworld || udev->descriptor.bNumConfigurations != 1) { 6011da177e4SLinus Torvalds int expected = udev->actconfig->desc.bConfigurationValue; 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds /* [9.4.2] get_configuration always works 6041da177e4SLinus Torvalds * ... although some cheap devices (like one TI Hub I've got) 6051da177e4SLinus Torvalds * won't return config descriptors except before set_config. 6061da177e4SLinus Torvalds */ 6071da177e4SLinus Torvalds retval = usb_control_msg (udev, usb_rcvctrlpipe (udev, 0), 6081da177e4SLinus Torvalds USB_REQ_GET_CONFIGURATION, 6091da177e4SLinus Torvalds USB_DIR_IN | USB_RECIP_DEVICE, 6101da177e4SLinus Torvalds 0, 0, dev->buf, 1, USB_CTRL_GET_TIMEOUT); 6111da177e4SLinus Torvalds if (retval != 1 || dev->buf [0] != expected) { 612ff7c79e4SDavid Brownell dev_dbg (&iface->dev, "get config --> %d %d (1 %d)\n", 613ff7c79e4SDavid Brownell retval, dev->buf[0], expected); 6141da177e4SLinus Torvalds return (retval < 0) ? retval : -EDOM; 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds } 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds /* there's always [9.4.3] a device descriptor [9.6.1] */ 6191da177e4SLinus Torvalds retval = usb_get_descriptor (udev, USB_DT_DEVICE, 0, 6201da177e4SLinus Torvalds dev->buf, sizeof udev->descriptor); 6211da177e4SLinus Torvalds if (retval != sizeof udev->descriptor) { 6221da177e4SLinus Torvalds dev_dbg (&iface->dev, "dev descriptor --> %d\n", retval); 6231da177e4SLinus Torvalds return (retval < 0) ? retval : -EDOM; 6241da177e4SLinus Torvalds } 6251da177e4SLinus Torvalds 6261da177e4SLinus Torvalds /* there's always [9.4.3] at least one config descriptor [9.6.3] */ 6271da177e4SLinus Torvalds for (i = 0; i < udev->descriptor.bNumConfigurations; i++) { 6281da177e4SLinus Torvalds retval = usb_get_descriptor (udev, USB_DT_CONFIG, i, 6291da177e4SLinus Torvalds dev->buf, TBUF_SIZE); 6301da177e4SLinus Torvalds if (!is_good_config (dev->buf, retval)) { 6311da177e4SLinus Torvalds dev_dbg (&iface->dev, 6321da177e4SLinus Torvalds "config [%d] descriptor --> %d\n", 6331da177e4SLinus Torvalds i, retval); 6341da177e4SLinus Torvalds return (retval < 0) ? retval : -EDOM; 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds 6371da177e4SLinus Torvalds // FIXME cross-checking udev->config[i] to make sure usbcore 6381da177e4SLinus Torvalds // parsed it right (etc) would be good testing paranoia 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds /* and sometimes [9.2.6.6] speed dependent descriptors */ 6421da177e4SLinus Torvalds if (le16_to_cpu(udev->descriptor.bcdUSB) == 0x0200) { 6431da177e4SLinus Torvalds struct usb_qualifier_descriptor *d = NULL; 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds /* device qualifier [9.6.2] */ 6461da177e4SLinus Torvalds retval = usb_get_descriptor (udev, 6471da177e4SLinus Torvalds USB_DT_DEVICE_QUALIFIER, 0, dev->buf, 6481da177e4SLinus Torvalds sizeof (struct usb_qualifier_descriptor)); 6491da177e4SLinus Torvalds if (retval == -EPIPE) { 6501da177e4SLinus Torvalds if (udev->speed == USB_SPEED_HIGH) { 6511da177e4SLinus Torvalds dev_dbg (&iface->dev, 6521da177e4SLinus Torvalds "hs dev qualifier --> %d\n", 6531da177e4SLinus Torvalds retval); 6541da177e4SLinus Torvalds return (retval < 0) ? retval : -EDOM; 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds /* usb2.0 but not high-speed capable; fine */ 6571da177e4SLinus Torvalds } else if (retval != sizeof (struct usb_qualifier_descriptor)) { 6581da177e4SLinus Torvalds dev_dbg (&iface->dev, "dev qualifier --> %d\n", retval); 6591da177e4SLinus Torvalds return (retval < 0) ? retval : -EDOM; 6601da177e4SLinus Torvalds } else 6611da177e4SLinus Torvalds d = (struct usb_qualifier_descriptor *) dev->buf; 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds /* might not have [9.6.2] any other-speed configs [9.6.4] */ 6641da177e4SLinus Torvalds if (d) { 6651da177e4SLinus Torvalds unsigned max = d->bNumConfigurations; 6661da177e4SLinus Torvalds for (i = 0; i < max; i++) { 6671da177e4SLinus Torvalds retval = usb_get_descriptor (udev, 6681da177e4SLinus Torvalds USB_DT_OTHER_SPEED_CONFIG, i, 6691da177e4SLinus Torvalds dev->buf, TBUF_SIZE); 6701da177e4SLinus Torvalds if (!is_good_config (dev->buf, retval)) { 6711da177e4SLinus Torvalds dev_dbg (&iface->dev, 6721da177e4SLinus Torvalds "other speed config --> %d\n", 6731da177e4SLinus Torvalds retval); 6741da177e4SLinus Torvalds return (retval < 0) ? retval : -EDOM; 6751da177e4SLinus Torvalds } 6761da177e4SLinus Torvalds } 6771da177e4SLinus Torvalds } 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds // FIXME fetch strings from at least the device descriptor 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds /* [9.4.5] get_status always works */ 6821da177e4SLinus Torvalds retval = usb_get_status (udev, USB_RECIP_DEVICE, 0, dev->buf); 6831da177e4SLinus Torvalds if (retval != 2) { 6841da177e4SLinus Torvalds dev_dbg (&iface->dev, "get dev status --> %d\n", retval); 6851da177e4SLinus Torvalds return (retval < 0) ? retval : -EDOM; 6861da177e4SLinus Torvalds } 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds // FIXME configuration.bmAttributes says if we could try to set/clear 6891da177e4SLinus Torvalds // the device's remote wakeup feature ... if we can, test that here 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds retval = usb_get_status (udev, USB_RECIP_INTERFACE, 6921da177e4SLinus Torvalds iface->altsetting [0].desc.bInterfaceNumber, dev->buf); 6931da177e4SLinus Torvalds if (retval != 2) { 6941da177e4SLinus Torvalds dev_dbg (&iface->dev, "get interface status --> %d\n", retval); 6951da177e4SLinus Torvalds return (retval < 0) ? retval : -EDOM; 6961da177e4SLinus Torvalds } 6971da177e4SLinus Torvalds // FIXME get status for each endpoint in the interface 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds return 0; 7001da177e4SLinus Torvalds } 7011da177e4SLinus Torvalds 7021da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds /* use ch9 requests to test whether: 7051da177e4SLinus Torvalds * (a) queues work for control, keeping N subtests queued and 7061da177e4SLinus Torvalds * active (auto-resubmit) for M loops through the queue. 7071da177e4SLinus Torvalds * (b) protocol stalls (control-only) will autorecover. 7081da177e4SLinus Torvalds * it's not like bulk/intr; no halt clearing. 7091da177e4SLinus Torvalds * (c) short control reads are reported and handled. 7101da177e4SLinus Torvalds * (d) queues are always processed in-order 7111da177e4SLinus Torvalds */ 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds struct ctrl_ctx { 7141da177e4SLinus Torvalds spinlock_t lock; 7151da177e4SLinus Torvalds struct usbtest_dev *dev; 7161da177e4SLinus Torvalds struct completion complete; 7171da177e4SLinus Torvalds unsigned count; 7181da177e4SLinus Torvalds unsigned pending; 7191da177e4SLinus Torvalds int status; 7201da177e4SLinus Torvalds struct urb **urb; 7211da177e4SLinus Torvalds struct usbtest_param *param; 7221da177e4SLinus Torvalds int last; 7231da177e4SLinus Torvalds }; 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds #define NUM_SUBCASES 15 /* how many test subcases here? */ 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds struct subcase { 7281da177e4SLinus Torvalds struct usb_ctrlrequest setup; 7291da177e4SLinus Torvalds int number; 7301da177e4SLinus Torvalds int expected; 7311da177e4SLinus Torvalds }; 7321da177e4SLinus Torvalds 7337d12e780SDavid Howells static void ctrl_complete (struct urb *urb) 7341da177e4SLinus Torvalds { 7351da177e4SLinus Torvalds struct ctrl_ctx *ctx = urb->context; 7361da177e4SLinus Torvalds struct usb_ctrlrequest *reqp; 7371da177e4SLinus Torvalds struct subcase *subcase; 7381da177e4SLinus Torvalds int status = urb->status; 7391da177e4SLinus Torvalds 7401da177e4SLinus Torvalds reqp = (struct usb_ctrlrequest *)urb->setup_packet; 7411da177e4SLinus Torvalds subcase = container_of (reqp, struct subcase, setup); 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds spin_lock (&ctx->lock); 7441da177e4SLinus Torvalds ctx->count--; 7451da177e4SLinus Torvalds ctx->pending--; 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds /* queue must transfer and complete in fifo order, unless 7481da177e4SLinus Torvalds * usb_unlink_urb() is used to unlink something not at the 7491da177e4SLinus Torvalds * physical queue head (not tested). 7501da177e4SLinus Torvalds */ 7511da177e4SLinus Torvalds if (subcase->number > 0) { 7521da177e4SLinus Torvalds if ((subcase->number - ctx->last) != 1) { 7531da177e4SLinus Torvalds dbg ("subcase %d completed out of order, last %d", 7541da177e4SLinus Torvalds subcase->number, ctx->last); 7551da177e4SLinus Torvalds status = -EDOM; 7561da177e4SLinus Torvalds ctx->last = subcase->number; 7571da177e4SLinus Torvalds goto error; 7581da177e4SLinus Torvalds } 7591da177e4SLinus Torvalds } 7601da177e4SLinus Torvalds ctx->last = subcase->number; 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds /* succeed or fault in only one way? */ 7631da177e4SLinus Torvalds if (status == subcase->expected) 7641da177e4SLinus Torvalds status = 0; 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds /* async unlink for cleanup? */ 7671da177e4SLinus Torvalds else if (status != -ECONNRESET) { 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds /* some faults are allowed, not required */ 7701da177e4SLinus Torvalds if (subcase->expected > 0 && ( 7711da177e4SLinus Torvalds ((urb->status == -subcase->expected /* happened */ 7721da177e4SLinus Torvalds || urb->status == 0)))) /* didn't */ 7731da177e4SLinus Torvalds status = 0; 7741da177e4SLinus Torvalds /* sometimes more than one fault is allowed */ 7751da177e4SLinus Torvalds else if (subcase->number == 12 && status == -EPIPE) 7761da177e4SLinus Torvalds status = 0; 7771da177e4SLinus Torvalds else 7781da177e4SLinus Torvalds dbg ("subtest %d error, status %d", 7791da177e4SLinus Torvalds subcase->number, status); 7801da177e4SLinus Torvalds } 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds /* unexpected status codes mean errors; ideally, in hardware */ 7831da177e4SLinus Torvalds if (status) { 7841da177e4SLinus Torvalds error: 7851da177e4SLinus Torvalds if (ctx->status == 0) { 7861da177e4SLinus Torvalds int i; 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds ctx->status = status; 7891da177e4SLinus Torvalds info ("control queue %02x.%02x, err %d, %d left", 7901da177e4SLinus Torvalds reqp->bRequestType, reqp->bRequest, 7911da177e4SLinus Torvalds status, ctx->count); 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds /* FIXME this "unlink everything" exit route should 7941da177e4SLinus Torvalds * be a separate test case. 7951da177e4SLinus Torvalds */ 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds /* unlink whatever's still pending */ 7981da177e4SLinus Torvalds for (i = 1; i < ctx->param->sglen; i++) { 7991da177e4SLinus Torvalds struct urb *u = ctx->urb [ 8001da177e4SLinus Torvalds (i + subcase->number) % ctx->param->sglen]; 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds if (u == urb || !u->dev) 8031da177e4SLinus Torvalds continue; 804caa2a122SFranck Bui-Huu spin_unlock(&ctx->lock); 8051da177e4SLinus Torvalds status = usb_unlink_urb (u); 806caa2a122SFranck Bui-Huu spin_lock(&ctx->lock); 8071da177e4SLinus Torvalds switch (status) { 8081da177e4SLinus Torvalds case -EINPROGRESS: 8091da177e4SLinus Torvalds case -EBUSY: 8101da177e4SLinus Torvalds case -EIDRM: 8111da177e4SLinus Torvalds continue; 8121da177e4SLinus Torvalds default: 8131da177e4SLinus Torvalds dbg ("urb unlink --> %d", status); 8141da177e4SLinus Torvalds } 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds status = ctx->status; 8171da177e4SLinus Torvalds } 8181da177e4SLinus Torvalds } 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds /* resubmit if we need to, else mark this as done */ 8211da177e4SLinus Torvalds if ((status == 0) && (ctx->pending < ctx->count)) { 822*54e6ecb2SChristoph Lameter if ((status = usb_submit_urb (urb, GFP_ATOMIC)) != 0) { 8231da177e4SLinus Torvalds dbg ("can't resubmit ctrl %02x.%02x, err %d", 8241da177e4SLinus Torvalds reqp->bRequestType, reqp->bRequest, status); 8251da177e4SLinus Torvalds urb->dev = NULL; 8261da177e4SLinus Torvalds } else 8271da177e4SLinus Torvalds ctx->pending++; 8281da177e4SLinus Torvalds } else 8291da177e4SLinus Torvalds urb->dev = NULL; 8301da177e4SLinus Torvalds 8311da177e4SLinus Torvalds /* signal completion when nothing's queued */ 8321da177e4SLinus Torvalds if (ctx->pending == 0) 8331da177e4SLinus Torvalds complete (&ctx->complete); 8341da177e4SLinus Torvalds spin_unlock (&ctx->lock); 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds static int 8381da177e4SLinus Torvalds test_ctrl_queue (struct usbtest_dev *dev, struct usbtest_param *param) 8391da177e4SLinus Torvalds { 8401da177e4SLinus Torvalds struct usb_device *udev = testdev_to_usbdev (dev); 8411da177e4SLinus Torvalds struct urb **urb; 8421da177e4SLinus Torvalds struct ctrl_ctx context; 8431da177e4SLinus Torvalds int i; 8441da177e4SLinus Torvalds 8451da177e4SLinus Torvalds spin_lock_init (&context.lock); 8461da177e4SLinus Torvalds context.dev = dev; 8471da177e4SLinus Torvalds init_completion (&context.complete); 8481da177e4SLinus Torvalds context.count = param->sglen * param->iterations; 8491da177e4SLinus Torvalds context.pending = 0; 8501da177e4SLinus Torvalds context.status = -ENOMEM; 8511da177e4SLinus Torvalds context.param = param; 8521da177e4SLinus Torvalds context.last = -1; 8531da177e4SLinus Torvalds 8541da177e4SLinus Torvalds /* allocate and init the urbs we'll queue. 8551da177e4SLinus Torvalds * as with bulk/intr sglists, sglen is the queue depth; it also 8561da177e4SLinus Torvalds * controls which subtests run (more tests than sglen) or rerun. 8571da177e4SLinus Torvalds */ 85880b6ca48SEric Sesterhenn urb = kcalloc(param->sglen, sizeof(struct urb *), SLAB_KERNEL); 8591da177e4SLinus Torvalds if (!urb) 8601da177e4SLinus Torvalds return -ENOMEM; 8611da177e4SLinus Torvalds for (i = 0; i < param->sglen; i++) { 8621da177e4SLinus Torvalds int pipe = usb_rcvctrlpipe (udev, 0); 8631da177e4SLinus Torvalds unsigned len; 8641da177e4SLinus Torvalds struct urb *u; 8651da177e4SLinus Torvalds struct usb_ctrlrequest req; 8661da177e4SLinus Torvalds struct subcase *reqp; 8671da177e4SLinus Torvalds int expected = 0; 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds /* requests here are mostly expected to succeed on any 8701da177e4SLinus Torvalds * device, but some are chosen to trigger protocol stalls 8711da177e4SLinus Torvalds * or short reads. 8721da177e4SLinus Torvalds */ 8731da177e4SLinus Torvalds memset (&req, 0, sizeof req); 8741da177e4SLinus Torvalds req.bRequest = USB_REQ_GET_DESCRIPTOR; 8751da177e4SLinus Torvalds req.bRequestType = USB_DIR_IN|USB_RECIP_DEVICE; 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds switch (i % NUM_SUBCASES) { 8781da177e4SLinus Torvalds case 0: // get device descriptor 8791da177e4SLinus Torvalds req.wValue = cpu_to_le16 (USB_DT_DEVICE << 8); 8801da177e4SLinus Torvalds len = sizeof (struct usb_device_descriptor); 8811da177e4SLinus Torvalds break; 8821da177e4SLinus Torvalds case 1: // get first config descriptor (only) 8831da177e4SLinus Torvalds req.wValue = cpu_to_le16 ((USB_DT_CONFIG << 8) | 0); 8841da177e4SLinus Torvalds len = sizeof (struct usb_config_descriptor); 8851da177e4SLinus Torvalds break; 8861da177e4SLinus Torvalds case 2: // get altsetting (OFTEN STALLS) 8871da177e4SLinus Torvalds req.bRequest = USB_REQ_GET_INTERFACE; 8881da177e4SLinus Torvalds req.bRequestType = USB_DIR_IN|USB_RECIP_INTERFACE; 8891da177e4SLinus Torvalds // index = 0 means first interface 8901da177e4SLinus Torvalds len = 1; 8911da177e4SLinus Torvalds expected = EPIPE; 8921da177e4SLinus Torvalds break; 8931da177e4SLinus Torvalds case 3: // get interface status 8941da177e4SLinus Torvalds req.bRequest = USB_REQ_GET_STATUS; 8951da177e4SLinus Torvalds req.bRequestType = USB_DIR_IN|USB_RECIP_INTERFACE; 8961da177e4SLinus Torvalds // interface 0 8971da177e4SLinus Torvalds len = 2; 8981da177e4SLinus Torvalds break; 8991da177e4SLinus Torvalds case 4: // get device status 9001da177e4SLinus Torvalds req.bRequest = USB_REQ_GET_STATUS; 9011da177e4SLinus Torvalds req.bRequestType = USB_DIR_IN|USB_RECIP_DEVICE; 9021da177e4SLinus Torvalds len = 2; 9031da177e4SLinus Torvalds break; 9041da177e4SLinus Torvalds case 5: // get device qualifier (MAY STALL) 9051da177e4SLinus Torvalds req.wValue = cpu_to_le16 (USB_DT_DEVICE_QUALIFIER << 8); 9061da177e4SLinus Torvalds len = sizeof (struct usb_qualifier_descriptor); 9071da177e4SLinus Torvalds if (udev->speed != USB_SPEED_HIGH) 9081da177e4SLinus Torvalds expected = EPIPE; 9091da177e4SLinus Torvalds break; 9101da177e4SLinus Torvalds case 6: // get first config descriptor, plus interface 9111da177e4SLinus Torvalds req.wValue = cpu_to_le16 ((USB_DT_CONFIG << 8) | 0); 9121da177e4SLinus Torvalds len = sizeof (struct usb_config_descriptor); 9131da177e4SLinus Torvalds len += sizeof (struct usb_interface_descriptor); 9141da177e4SLinus Torvalds break; 9151da177e4SLinus Torvalds case 7: // get interface descriptor (ALWAYS STALLS) 9161da177e4SLinus Torvalds req.wValue = cpu_to_le16 (USB_DT_INTERFACE << 8); 9171da177e4SLinus Torvalds // interface == 0 9181da177e4SLinus Torvalds len = sizeof (struct usb_interface_descriptor); 9191da177e4SLinus Torvalds expected = EPIPE; 9201da177e4SLinus Torvalds break; 9211da177e4SLinus Torvalds // NOTE: two consecutive stalls in the queue here. 9221da177e4SLinus Torvalds // that tests fault recovery a bit more aggressively. 9231da177e4SLinus Torvalds case 8: // clear endpoint halt (USUALLY STALLS) 9241da177e4SLinus Torvalds req.bRequest = USB_REQ_CLEAR_FEATURE; 9251da177e4SLinus Torvalds req.bRequestType = USB_RECIP_ENDPOINT; 9261da177e4SLinus Torvalds // wValue 0 == ep halt 9271da177e4SLinus Torvalds // wIndex 0 == ep0 (shouldn't halt!) 9281da177e4SLinus Torvalds len = 0; 9291da177e4SLinus Torvalds pipe = usb_sndctrlpipe (udev, 0); 9301da177e4SLinus Torvalds expected = EPIPE; 9311da177e4SLinus Torvalds break; 9321da177e4SLinus Torvalds case 9: // get endpoint status 9331da177e4SLinus Torvalds req.bRequest = USB_REQ_GET_STATUS; 9341da177e4SLinus Torvalds req.bRequestType = USB_DIR_IN|USB_RECIP_ENDPOINT; 9351da177e4SLinus Torvalds // endpoint 0 9361da177e4SLinus Torvalds len = 2; 9371da177e4SLinus Torvalds break; 9381da177e4SLinus Torvalds case 10: // trigger short read (EREMOTEIO) 9391da177e4SLinus Torvalds req.wValue = cpu_to_le16 ((USB_DT_CONFIG << 8) | 0); 9401da177e4SLinus Torvalds len = 1024; 9411da177e4SLinus Torvalds expected = -EREMOTEIO; 9421da177e4SLinus Torvalds break; 9431da177e4SLinus Torvalds // NOTE: two consecutive _different_ faults in the queue. 9441da177e4SLinus Torvalds case 11: // get endpoint descriptor (ALWAYS STALLS) 9451da177e4SLinus Torvalds req.wValue = cpu_to_le16 (USB_DT_ENDPOINT << 8); 9461da177e4SLinus Torvalds // endpoint == 0 9471da177e4SLinus Torvalds len = sizeof (struct usb_interface_descriptor); 9481da177e4SLinus Torvalds expected = EPIPE; 9491da177e4SLinus Torvalds break; 9501da177e4SLinus Torvalds // NOTE: sometimes even a third fault in the queue! 9511da177e4SLinus Torvalds case 12: // get string 0 descriptor (MAY STALL) 9521da177e4SLinus Torvalds req.wValue = cpu_to_le16 (USB_DT_STRING << 8); 9531da177e4SLinus Torvalds // string == 0, for language IDs 9541da177e4SLinus Torvalds len = sizeof (struct usb_interface_descriptor); 9551da177e4SLinus Torvalds // may succeed when > 4 languages 9561da177e4SLinus Torvalds expected = EREMOTEIO; // or EPIPE, if no strings 9571da177e4SLinus Torvalds break; 9581da177e4SLinus Torvalds case 13: // short read, resembling case 10 9591da177e4SLinus Torvalds req.wValue = cpu_to_le16 ((USB_DT_CONFIG << 8) | 0); 9601da177e4SLinus Torvalds // last data packet "should" be DATA1, not DATA0 9611da177e4SLinus Torvalds len = 1024 - udev->descriptor.bMaxPacketSize0; 9621da177e4SLinus Torvalds expected = -EREMOTEIO; 9631da177e4SLinus Torvalds break; 9641da177e4SLinus Torvalds case 14: // short read; try to fill the last packet 9651da177e4SLinus Torvalds req.wValue = cpu_to_le16 ((USB_DT_DEVICE << 8) | 0); 9661da177e4SLinus Torvalds // device descriptor size == 18 bytes 9671da177e4SLinus Torvalds len = udev->descriptor.bMaxPacketSize0; 9681da177e4SLinus Torvalds switch (len) { 9691da177e4SLinus Torvalds case 8: len = 24; break; 9701da177e4SLinus Torvalds case 16: len = 32; break; 9711da177e4SLinus Torvalds } 9721da177e4SLinus Torvalds expected = -EREMOTEIO; 9731da177e4SLinus Torvalds break; 9741da177e4SLinus Torvalds default: 9751da177e4SLinus Torvalds err ("bogus number of ctrl queue testcases!"); 9761da177e4SLinus Torvalds context.status = -EINVAL; 9771da177e4SLinus Torvalds goto cleanup; 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds req.wLength = cpu_to_le16 (len); 9801da177e4SLinus Torvalds urb [i] = u = simple_alloc_urb (udev, pipe, len); 9811da177e4SLinus Torvalds if (!u) 9821da177e4SLinus Torvalds goto cleanup; 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds reqp = usb_buffer_alloc (udev, sizeof *reqp, SLAB_KERNEL, 9851da177e4SLinus Torvalds &u->setup_dma); 9861da177e4SLinus Torvalds if (!reqp) 9871da177e4SLinus Torvalds goto cleanup; 9881da177e4SLinus Torvalds reqp->setup = req; 9891da177e4SLinus Torvalds reqp->number = i % NUM_SUBCASES; 9901da177e4SLinus Torvalds reqp->expected = expected; 9911da177e4SLinus Torvalds u->setup_packet = (char *) &reqp->setup; 9923e8a556aSAlan Stern u->transfer_flags |= URB_NO_SETUP_DMA_MAP; 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds u->context = &context; 9951da177e4SLinus Torvalds u->complete = ctrl_complete; 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds /* queue the urbs */ 9991da177e4SLinus Torvalds context.urb = urb; 10001da177e4SLinus Torvalds spin_lock_irq (&context.lock); 10011da177e4SLinus Torvalds for (i = 0; i < param->sglen; i++) { 1002*54e6ecb2SChristoph Lameter context.status = usb_submit_urb (urb [i], GFP_ATOMIC); 10031da177e4SLinus Torvalds if (context.status != 0) { 10041da177e4SLinus Torvalds dbg ("can't submit urb[%d], status %d", 10051da177e4SLinus Torvalds i, context.status); 10061da177e4SLinus Torvalds context.count = context.pending; 10071da177e4SLinus Torvalds break; 10081da177e4SLinus Torvalds } 10091da177e4SLinus Torvalds context.pending++; 10101da177e4SLinus Torvalds } 10111da177e4SLinus Torvalds spin_unlock_irq (&context.lock); 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds /* FIXME set timer and time out; provide a disconnect hook */ 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds /* wait for the last one to complete */ 10161da177e4SLinus Torvalds if (context.pending > 0) 10171da177e4SLinus Torvalds wait_for_completion (&context.complete); 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds cleanup: 10201da177e4SLinus Torvalds for (i = 0; i < param->sglen; i++) { 10211da177e4SLinus Torvalds if (!urb [i]) 10221da177e4SLinus Torvalds continue; 10231da177e4SLinus Torvalds urb [i]->dev = udev; 10241da177e4SLinus Torvalds if (urb [i]->setup_packet) 10251da177e4SLinus Torvalds usb_buffer_free (udev, sizeof (struct usb_ctrlrequest), 10261da177e4SLinus Torvalds urb [i]->setup_packet, 10271da177e4SLinus Torvalds urb [i]->setup_dma); 10281da177e4SLinus Torvalds simple_free_urb (urb [i]); 10291da177e4SLinus Torvalds } 10301da177e4SLinus Torvalds kfree (urb); 10311da177e4SLinus Torvalds return context.status; 10321da177e4SLinus Torvalds } 10331da177e4SLinus Torvalds #undef NUM_SUBCASES 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 10371da177e4SLinus Torvalds 10387d12e780SDavid Howells static void unlink1_callback (struct urb *urb) 10391da177e4SLinus Torvalds { 10401da177e4SLinus Torvalds int status = urb->status; 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds // we "know" -EPIPE (stall) never happens 10431da177e4SLinus Torvalds if (!status) 1044*54e6ecb2SChristoph Lameter status = usb_submit_urb (urb, GFP_ATOMIC); 10451da177e4SLinus Torvalds if (status) { 10461da177e4SLinus Torvalds urb->status = status; 10471da177e4SLinus Torvalds complete ((struct completion *) urb->context); 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds } 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds static int unlink1 (struct usbtest_dev *dev, int pipe, int size, int async) 10521da177e4SLinus Torvalds { 10531da177e4SLinus Torvalds struct urb *urb; 10541da177e4SLinus Torvalds struct completion completion; 10551da177e4SLinus Torvalds int retval = 0; 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds init_completion (&completion); 10581da177e4SLinus Torvalds urb = simple_alloc_urb (testdev_to_usbdev (dev), pipe, size); 10591da177e4SLinus Torvalds if (!urb) 10601da177e4SLinus Torvalds return -ENOMEM; 10611da177e4SLinus Torvalds urb->context = &completion; 10621da177e4SLinus Torvalds urb->complete = unlink1_callback; 10631da177e4SLinus Torvalds 10641da177e4SLinus Torvalds /* keep the endpoint busy. there are lots of hc/hcd-internal 10651da177e4SLinus Torvalds * states, and testing should get to all of them over time. 10661da177e4SLinus Torvalds * 10671da177e4SLinus Torvalds * FIXME want additional tests for when endpoint is STALLing 10681da177e4SLinus Torvalds * due to errors, or is just NAKing requests. 10691da177e4SLinus Torvalds */ 10701da177e4SLinus Torvalds if ((retval = usb_submit_urb (urb, SLAB_KERNEL)) != 0) { 10711da177e4SLinus Torvalds dev_dbg (&dev->intf->dev, "submit fail %d\n", retval); 10721da177e4SLinus Torvalds return retval; 10731da177e4SLinus Torvalds } 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds /* unlinking that should always work. variable delay tests more 10761da177e4SLinus Torvalds * hcd states and code paths, even with little other system load. 10771da177e4SLinus Torvalds */ 10781da177e4SLinus Torvalds msleep (jiffies % (2 * INTERRUPT_RATE)); 10791da177e4SLinus Torvalds if (async) { 10801da177e4SLinus Torvalds retry: 10811da177e4SLinus Torvalds retval = usb_unlink_urb (urb); 10821da177e4SLinus Torvalds if (retval == -EBUSY || retval == -EIDRM) { 10831da177e4SLinus Torvalds /* we can't unlink urbs while they're completing. 10841da177e4SLinus Torvalds * or if they've completed, and we haven't resubmitted. 10851da177e4SLinus Torvalds * "normal" drivers would prevent resubmission, but 10861da177e4SLinus Torvalds * since we're testing unlink paths, we can't. 10871da177e4SLinus Torvalds */ 10881da177e4SLinus Torvalds dev_dbg (&dev->intf->dev, "unlink retry\n"); 10891da177e4SLinus Torvalds goto retry; 10901da177e4SLinus Torvalds } 10911da177e4SLinus Torvalds } else 10921da177e4SLinus Torvalds usb_kill_urb (urb); 10931da177e4SLinus Torvalds if (!(retval == 0 || retval == -EINPROGRESS)) { 10941da177e4SLinus Torvalds dev_dbg (&dev->intf->dev, "unlink fail %d\n", retval); 10951da177e4SLinus Torvalds return retval; 10961da177e4SLinus Torvalds } 10971da177e4SLinus Torvalds 10981da177e4SLinus Torvalds wait_for_completion (&completion); 10991da177e4SLinus Torvalds retval = urb->status; 11001da177e4SLinus Torvalds simple_free_urb (urb); 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds if (async) 11031da177e4SLinus Torvalds return (retval == -ECONNRESET) ? 0 : retval - 1000; 11041da177e4SLinus Torvalds else 11051da177e4SLinus Torvalds return (retval == -ENOENT || retval == -EPERM) ? 11061da177e4SLinus Torvalds 0 : retval - 2000; 11071da177e4SLinus Torvalds } 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds static int unlink_simple (struct usbtest_dev *dev, int pipe, int len) 11101da177e4SLinus Torvalds { 11111da177e4SLinus Torvalds int retval = 0; 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds /* test sync and async paths */ 11141da177e4SLinus Torvalds retval = unlink1 (dev, pipe, len, 1); 11151da177e4SLinus Torvalds if (!retval) 11161da177e4SLinus Torvalds retval = unlink1 (dev, pipe, len, 0); 11171da177e4SLinus Torvalds return retval; 11181da177e4SLinus Torvalds } 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds static int verify_not_halted (int ep, struct urb *urb) 11231da177e4SLinus Torvalds { 11241da177e4SLinus Torvalds int retval; 11251da177e4SLinus Torvalds u16 status; 11261da177e4SLinus Torvalds 11271da177e4SLinus Torvalds /* shouldn't look or act halted */ 11281da177e4SLinus Torvalds retval = usb_get_status (urb->dev, USB_RECIP_ENDPOINT, ep, &status); 11291da177e4SLinus Torvalds if (retval < 0) { 11301da177e4SLinus Torvalds dbg ("ep %02x couldn't get no-halt status, %d", ep, retval); 11311da177e4SLinus Torvalds return retval; 11321da177e4SLinus Torvalds } 11331da177e4SLinus Torvalds if (status != 0) { 11341da177e4SLinus Torvalds dbg ("ep %02x bogus status: %04x != 0", ep, status); 11351da177e4SLinus Torvalds return -EINVAL; 11361da177e4SLinus Torvalds } 11371da177e4SLinus Torvalds retval = simple_io (urb, 1, 0, 0, __FUNCTION__); 11381da177e4SLinus Torvalds if (retval != 0) 11391da177e4SLinus Torvalds return -EINVAL; 11401da177e4SLinus Torvalds return 0; 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds static int verify_halted (int ep, struct urb *urb) 11441da177e4SLinus Torvalds { 11451da177e4SLinus Torvalds int retval; 11461da177e4SLinus Torvalds u16 status; 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds /* should look and act halted */ 11491da177e4SLinus Torvalds retval = usb_get_status (urb->dev, USB_RECIP_ENDPOINT, ep, &status); 11501da177e4SLinus Torvalds if (retval < 0) { 11511da177e4SLinus Torvalds dbg ("ep %02x couldn't get halt status, %d", ep, retval); 11521da177e4SLinus Torvalds return retval; 11531da177e4SLinus Torvalds } 11541da177e4SLinus Torvalds if (status != 1) { 11551da177e4SLinus Torvalds dbg ("ep %02x bogus status: %04x != 1", ep, status); 11561da177e4SLinus Torvalds return -EINVAL; 11571da177e4SLinus Torvalds } 11581da177e4SLinus Torvalds retval = simple_io (urb, 1, 0, -EPIPE, __FUNCTION__); 11591da177e4SLinus Torvalds if (retval != -EPIPE) 11601da177e4SLinus Torvalds return -EINVAL; 11611da177e4SLinus Torvalds retval = simple_io (urb, 1, 0, -EPIPE, "verify_still_halted"); 11621da177e4SLinus Torvalds if (retval != -EPIPE) 11631da177e4SLinus Torvalds return -EINVAL; 11641da177e4SLinus Torvalds return 0; 11651da177e4SLinus Torvalds } 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds static int test_halt (int ep, struct urb *urb) 11681da177e4SLinus Torvalds { 11691da177e4SLinus Torvalds int retval; 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds /* shouldn't look or act halted now */ 11721da177e4SLinus Torvalds retval = verify_not_halted (ep, urb); 11731da177e4SLinus Torvalds if (retval < 0) 11741da177e4SLinus Torvalds return retval; 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds /* set halt (protocol test only), verify it worked */ 11771da177e4SLinus Torvalds retval = usb_control_msg (urb->dev, usb_sndctrlpipe (urb->dev, 0), 11781da177e4SLinus Torvalds USB_REQ_SET_FEATURE, USB_RECIP_ENDPOINT, 11791da177e4SLinus Torvalds USB_ENDPOINT_HALT, ep, 11801da177e4SLinus Torvalds NULL, 0, USB_CTRL_SET_TIMEOUT); 11811da177e4SLinus Torvalds if (retval < 0) { 11821da177e4SLinus Torvalds dbg ("ep %02x couldn't set halt, %d", ep, retval); 11831da177e4SLinus Torvalds return retval; 11841da177e4SLinus Torvalds } 11851da177e4SLinus Torvalds retval = verify_halted (ep, urb); 11861da177e4SLinus Torvalds if (retval < 0) 11871da177e4SLinus Torvalds return retval; 11881da177e4SLinus Torvalds 11891da177e4SLinus Torvalds /* clear halt (tests API + protocol), verify it worked */ 11901da177e4SLinus Torvalds retval = usb_clear_halt (urb->dev, urb->pipe); 11911da177e4SLinus Torvalds if (retval < 0) { 11921da177e4SLinus Torvalds dbg ("ep %02x couldn't clear halt, %d", ep, retval); 11931da177e4SLinus Torvalds return retval; 11941da177e4SLinus Torvalds } 11951da177e4SLinus Torvalds retval = verify_not_halted (ep, urb); 11961da177e4SLinus Torvalds if (retval < 0) 11971da177e4SLinus Torvalds return retval; 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds /* NOTE: could also verify SET_INTERFACE clear halts ... */ 12001da177e4SLinus Torvalds 12011da177e4SLinus Torvalds return 0; 12021da177e4SLinus Torvalds } 12031da177e4SLinus Torvalds 12041da177e4SLinus Torvalds static int halt_simple (struct usbtest_dev *dev) 12051da177e4SLinus Torvalds { 12061da177e4SLinus Torvalds int ep; 12071da177e4SLinus Torvalds int retval = 0; 12081da177e4SLinus Torvalds struct urb *urb; 12091da177e4SLinus Torvalds 12101da177e4SLinus Torvalds urb = simple_alloc_urb (testdev_to_usbdev (dev), 0, 512); 12111da177e4SLinus Torvalds if (urb == NULL) 12121da177e4SLinus Torvalds return -ENOMEM; 12131da177e4SLinus Torvalds 12141da177e4SLinus Torvalds if (dev->in_pipe) { 12151da177e4SLinus Torvalds ep = usb_pipeendpoint (dev->in_pipe) | USB_DIR_IN; 12161da177e4SLinus Torvalds urb->pipe = dev->in_pipe; 12171da177e4SLinus Torvalds retval = test_halt (ep, urb); 12181da177e4SLinus Torvalds if (retval < 0) 12191da177e4SLinus Torvalds goto done; 12201da177e4SLinus Torvalds } 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds if (dev->out_pipe) { 12231da177e4SLinus Torvalds ep = usb_pipeendpoint (dev->out_pipe); 12241da177e4SLinus Torvalds urb->pipe = dev->out_pipe; 12251da177e4SLinus Torvalds retval = test_halt (ep, urb); 12261da177e4SLinus Torvalds } 12271da177e4SLinus Torvalds done: 12281da177e4SLinus Torvalds simple_free_urb (urb); 12291da177e4SLinus Torvalds return retval; 12301da177e4SLinus Torvalds } 12311da177e4SLinus Torvalds 12321da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 12331da177e4SLinus Torvalds 12341da177e4SLinus Torvalds /* Control OUT tests use the vendor control requests from Intel's 12351da177e4SLinus Torvalds * USB 2.0 compliance test device: write a buffer, read it back. 12361da177e4SLinus Torvalds * 12371da177e4SLinus Torvalds * Intel's spec only _requires_ that it work for one packet, which 12381da177e4SLinus Torvalds * is pretty weak. Some HCDs place limits here; most devices will 12391da177e4SLinus Torvalds * need to be able to handle more than one OUT data packet. We'll 12401da177e4SLinus Torvalds * try whatever we're told to try. 12411da177e4SLinus Torvalds */ 12421da177e4SLinus Torvalds static int ctrl_out (struct usbtest_dev *dev, 12431da177e4SLinus Torvalds unsigned count, unsigned length, unsigned vary) 12441da177e4SLinus Torvalds { 1245f54fa84dSOrjan Friberg unsigned i, j, len; 1246f54fa84dSOrjan Friberg int retval; 12471da177e4SLinus Torvalds u8 *buf; 12481da177e4SLinus Torvalds char *what = "?"; 12491da177e4SLinus Torvalds struct usb_device *udev; 12501da177e4SLinus Torvalds 1251ff7c79e4SDavid Brownell if (length < 1 || length > 0xffff || vary >= length) 12521da177e4SLinus Torvalds return -EINVAL; 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds buf = kmalloc(length, SLAB_KERNEL); 12551da177e4SLinus Torvalds if (!buf) 12561da177e4SLinus Torvalds return -ENOMEM; 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds udev = testdev_to_usbdev (dev); 12591da177e4SLinus Torvalds len = length; 12601da177e4SLinus Torvalds retval = 0; 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds /* NOTE: hardware might well act differently if we pushed it 12631da177e4SLinus Torvalds * with lots back-to-back queued requests. 12641da177e4SLinus Torvalds */ 12651da177e4SLinus Torvalds for (i = 0; i < count; i++) { 12661da177e4SLinus Torvalds /* write patterned data */ 12671da177e4SLinus Torvalds for (j = 0; j < len; j++) 12681da177e4SLinus Torvalds buf [j] = i + j; 12691da177e4SLinus Torvalds retval = usb_control_msg (udev, usb_sndctrlpipe (udev,0), 12701da177e4SLinus Torvalds 0x5b, USB_DIR_OUT|USB_TYPE_VENDOR, 12711da177e4SLinus Torvalds 0, 0, buf, len, USB_CTRL_SET_TIMEOUT); 12721da177e4SLinus Torvalds if (retval != len) { 12731da177e4SLinus Torvalds what = "write"; 1274ff7c79e4SDavid Brownell if (retval >= 0) { 1275ff7c79e4SDavid Brownell INFO(dev, "ctrl_out, wlen %d (expected %d)\n", 1276ff7c79e4SDavid Brownell retval, len); 1277ff7c79e4SDavid Brownell retval = -EBADMSG; 1278ff7c79e4SDavid Brownell } 12791da177e4SLinus Torvalds break; 12801da177e4SLinus Torvalds } 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds /* read it back -- assuming nothing intervened!! */ 12831da177e4SLinus Torvalds retval = usb_control_msg (udev, usb_rcvctrlpipe (udev,0), 12841da177e4SLinus Torvalds 0x5c, USB_DIR_IN|USB_TYPE_VENDOR, 12851da177e4SLinus Torvalds 0, 0, buf, len, USB_CTRL_GET_TIMEOUT); 12861da177e4SLinus Torvalds if (retval != len) { 12871da177e4SLinus Torvalds what = "read"; 1288ff7c79e4SDavid Brownell if (retval >= 0) { 1289ff7c79e4SDavid Brownell INFO(dev, "ctrl_out, rlen %d (expected %d)\n", 1290ff7c79e4SDavid Brownell retval, len); 1291ff7c79e4SDavid Brownell retval = -EBADMSG; 1292ff7c79e4SDavid Brownell } 12931da177e4SLinus Torvalds break; 12941da177e4SLinus Torvalds } 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds /* fail if we can't verify */ 12971da177e4SLinus Torvalds for (j = 0; j < len; j++) { 12981da177e4SLinus Torvalds if (buf [j] != (u8) (i + j)) { 12991da177e4SLinus Torvalds INFO (dev, "ctrl_out, byte %d is %d not %d\n", 13001da177e4SLinus Torvalds j, buf [j], (u8) i + j); 13011da177e4SLinus Torvalds retval = -EBADMSG; 13021da177e4SLinus Torvalds break; 13031da177e4SLinus Torvalds } 13041da177e4SLinus Torvalds } 13051da177e4SLinus Torvalds if (retval < 0) { 13061da177e4SLinus Torvalds what = "verify"; 13071da177e4SLinus Torvalds break; 13081da177e4SLinus Torvalds } 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds len += vary; 1311ff7c79e4SDavid Brownell 1312ff7c79e4SDavid Brownell /* [real world] the "zero bytes IN" case isn't really used. 1313ff7c79e4SDavid Brownell * hardware can easily trip up in this wierd case, since its 1314ff7c79e4SDavid Brownell * status stage is IN, not OUT like other ep0in transfers. 1315ff7c79e4SDavid Brownell */ 13161da177e4SLinus Torvalds if (len > length) 1317ff7c79e4SDavid Brownell len = realworld ? 1 : 0; 13181da177e4SLinus Torvalds } 13191da177e4SLinus Torvalds 13201da177e4SLinus Torvalds if (retval < 0) 13211da177e4SLinus Torvalds INFO (dev, "ctrl_out %s failed, code %d, count %d\n", 13221da177e4SLinus Torvalds what, retval, i); 13231da177e4SLinus Torvalds 13241da177e4SLinus Torvalds kfree (buf); 13251da177e4SLinus Torvalds return retval; 13261da177e4SLinus Torvalds } 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds /* ISO tests ... mimics common usage 13311da177e4SLinus Torvalds * - buffer length is split into N packets (mostly maxpacket sized) 13321da177e4SLinus Torvalds * - multi-buffers according to sglen 13331da177e4SLinus Torvalds */ 13341da177e4SLinus Torvalds 13351da177e4SLinus Torvalds struct iso_context { 13361da177e4SLinus Torvalds unsigned count; 13371da177e4SLinus Torvalds unsigned pending; 13381da177e4SLinus Torvalds spinlock_t lock; 13391da177e4SLinus Torvalds struct completion done; 13409da2150fSAlan Stern int submit_error; 13411da177e4SLinus Torvalds unsigned long errors; 13429da2150fSAlan Stern unsigned long packet_count; 13431da177e4SLinus Torvalds struct usbtest_dev *dev; 13441da177e4SLinus Torvalds }; 13451da177e4SLinus Torvalds 13467d12e780SDavid Howells static void iso_callback (struct urb *urb) 13471da177e4SLinus Torvalds { 13481da177e4SLinus Torvalds struct iso_context *ctx = urb->context; 13491da177e4SLinus Torvalds 13501da177e4SLinus Torvalds spin_lock(&ctx->lock); 13511da177e4SLinus Torvalds ctx->count--; 13521da177e4SLinus Torvalds 13539da2150fSAlan Stern ctx->packet_count += urb->number_of_packets; 13541da177e4SLinus Torvalds if (urb->error_count > 0) 13551da177e4SLinus Torvalds ctx->errors += urb->error_count; 13569da2150fSAlan Stern else if (urb->status != 0) 13579da2150fSAlan Stern ctx->errors += urb->number_of_packets; 13581da177e4SLinus Torvalds 13599da2150fSAlan Stern if (urb->status == 0 && ctx->count > (ctx->pending - 1) 13609da2150fSAlan Stern && !ctx->submit_error) { 13611da177e4SLinus Torvalds int status = usb_submit_urb (urb, GFP_ATOMIC); 13621da177e4SLinus Torvalds switch (status) { 13631da177e4SLinus Torvalds case 0: 13641da177e4SLinus Torvalds goto done; 13651da177e4SLinus Torvalds default: 13661da177e4SLinus Torvalds dev_dbg (&ctx->dev->intf->dev, 13671da177e4SLinus Torvalds "iso resubmit err %d\n", 13681da177e4SLinus Torvalds status); 13691da177e4SLinus Torvalds /* FALLTHROUGH */ 13701da177e4SLinus Torvalds case -ENODEV: /* disconnected */ 13719da2150fSAlan Stern case -ESHUTDOWN: /* endpoint disabled */ 13729da2150fSAlan Stern ctx->submit_error = 1; 13731da177e4SLinus Torvalds break; 13741da177e4SLinus Torvalds } 13751da177e4SLinus Torvalds } 13761da177e4SLinus Torvalds simple_free_urb (urb); 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds ctx->pending--; 13791da177e4SLinus Torvalds if (ctx->pending == 0) { 13801da177e4SLinus Torvalds if (ctx->errors) 13811da177e4SLinus Torvalds dev_dbg (&ctx->dev->intf->dev, 13829da2150fSAlan Stern "iso test, %lu errors out of %lu\n", 13839da2150fSAlan Stern ctx->errors, ctx->packet_count); 13841da177e4SLinus Torvalds complete (&ctx->done); 13851da177e4SLinus Torvalds } 13861da177e4SLinus Torvalds done: 13871da177e4SLinus Torvalds spin_unlock(&ctx->lock); 13881da177e4SLinus Torvalds } 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds static struct urb *iso_alloc_urb ( 13911da177e4SLinus Torvalds struct usb_device *udev, 13921da177e4SLinus Torvalds int pipe, 13931da177e4SLinus Torvalds struct usb_endpoint_descriptor *desc, 13941da177e4SLinus Torvalds long bytes 13951da177e4SLinus Torvalds ) 13961da177e4SLinus Torvalds { 13971da177e4SLinus Torvalds struct urb *urb; 13981da177e4SLinus Torvalds unsigned i, maxp, packets; 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds if (bytes < 0 || !desc) 14011da177e4SLinus Torvalds return NULL; 14021da177e4SLinus Torvalds maxp = 0x7ff & le16_to_cpu(desc->wMaxPacketSize); 14031da177e4SLinus Torvalds maxp *= 1 + (0x3 & (le16_to_cpu(desc->wMaxPacketSize) >> 11)); 14041da177e4SLinus Torvalds packets = (bytes + maxp - 1) / maxp; 14051da177e4SLinus Torvalds 14061da177e4SLinus Torvalds urb = usb_alloc_urb (packets, SLAB_KERNEL); 14071da177e4SLinus Torvalds if (!urb) 14081da177e4SLinus Torvalds return urb; 14091da177e4SLinus Torvalds urb->dev = udev; 14101da177e4SLinus Torvalds urb->pipe = pipe; 14111da177e4SLinus Torvalds 14121da177e4SLinus Torvalds urb->number_of_packets = packets; 14131da177e4SLinus Torvalds urb->transfer_buffer_length = bytes; 14141da177e4SLinus Torvalds urb->transfer_buffer = usb_buffer_alloc (udev, bytes, SLAB_KERNEL, 14151da177e4SLinus Torvalds &urb->transfer_dma); 14161da177e4SLinus Torvalds if (!urb->transfer_buffer) { 14171da177e4SLinus Torvalds usb_free_urb (urb); 14181da177e4SLinus Torvalds return NULL; 14191da177e4SLinus Torvalds } 14201da177e4SLinus Torvalds memset (urb->transfer_buffer, 0, bytes); 14211da177e4SLinus Torvalds for (i = 0; i < packets; i++) { 14221da177e4SLinus Torvalds /* here, only the last packet will be short */ 14231da177e4SLinus Torvalds urb->iso_frame_desc[i].length = min ((unsigned) bytes, maxp); 14241da177e4SLinus Torvalds bytes -= urb->iso_frame_desc[i].length; 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds urb->iso_frame_desc[i].offset = maxp * i; 14271da177e4SLinus Torvalds } 14281da177e4SLinus Torvalds 14291da177e4SLinus Torvalds urb->complete = iso_callback; 14301da177e4SLinus Torvalds // urb->context = SET BY CALLER 14311da177e4SLinus Torvalds urb->interval = 1 << (desc->bInterval - 1); 14321da177e4SLinus Torvalds urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; 14331da177e4SLinus Torvalds return urb; 14341da177e4SLinus Torvalds } 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds static int 14371da177e4SLinus Torvalds test_iso_queue (struct usbtest_dev *dev, struct usbtest_param *param, 14381da177e4SLinus Torvalds int pipe, struct usb_endpoint_descriptor *desc) 14391da177e4SLinus Torvalds { 14401da177e4SLinus Torvalds struct iso_context context; 14411da177e4SLinus Torvalds struct usb_device *udev; 14421da177e4SLinus Torvalds unsigned i; 14431da177e4SLinus Torvalds unsigned long packets = 0; 14449da2150fSAlan Stern int status = 0; 14451da177e4SLinus Torvalds struct urb *urbs[10]; /* FIXME no limit */ 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds if (param->sglen > 10) 14481da177e4SLinus Torvalds return -EDOM; 14491da177e4SLinus Torvalds 14509da2150fSAlan Stern memset(&context, 0, sizeof context); 14511da177e4SLinus Torvalds context.count = param->iterations * param->sglen; 14521da177e4SLinus Torvalds context.dev = dev; 14531da177e4SLinus Torvalds init_completion (&context.done); 14541da177e4SLinus Torvalds spin_lock_init (&context.lock); 14551da177e4SLinus Torvalds 14561da177e4SLinus Torvalds memset (urbs, 0, sizeof urbs); 14571da177e4SLinus Torvalds udev = testdev_to_usbdev (dev); 14581da177e4SLinus Torvalds dev_dbg (&dev->intf->dev, 14591da177e4SLinus Torvalds "... iso period %d %sframes, wMaxPacket %04x\n", 14601da177e4SLinus Torvalds 1 << (desc->bInterval - 1), 14611da177e4SLinus Torvalds (udev->speed == USB_SPEED_HIGH) ? "micro" : "", 14621da177e4SLinus Torvalds le16_to_cpu(desc->wMaxPacketSize)); 14631da177e4SLinus Torvalds 14641da177e4SLinus Torvalds for (i = 0; i < param->sglen; i++) { 14651da177e4SLinus Torvalds urbs [i] = iso_alloc_urb (udev, pipe, desc, 14661da177e4SLinus Torvalds param->length); 14671da177e4SLinus Torvalds if (!urbs [i]) { 14681da177e4SLinus Torvalds status = -ENOMEM; 14691da177e4SLinus Torvalds goto fail; 14701da177e4SLinus Torvalds } 14711da177e4SLinus Torvalds packets += urbs[i]->number_of_packets; 14721da177e4SLinus Torvalds urbs [i]->context = &context; 14731da177e4SLinus Torvalds } 14741da177e4SLinus Torvalds packets *= param->iterations; 14751da177e4SLinus Torvalds dev_dbg (&dev->intf->dev, 14761da177e4SLinus Torvalds "... total %lu msec (%lu packets)\n", 14771da177e4SLinus Torvalds (packets * (1 << (desc->bInterval - 1))) 14781da177e4SLinus Torvalds / ((udev->speed == USB_SPEED_HIGH) ? 8 : 1), 14791da177e4SLinus Torvalds packets); 14801da177e4SLinus Torvalds 14811da177e4SLinus Torvalds spin_lock_irq (&context.lock); 14821da177e4SLinus Torvalds for (i = 0; i < param->sglen; i++) { 14839da2150fSAlan Stern ++context.pending; 1484*54e6ecb2SChristoph Lameter status = usb_submit_urb (urbs [i], GFP_ATOMIC); 14851da177e4SLinus Torvalds if (status < 0) { 14861da177e4SLinus Torvalds ERROR (dev, "submit iso[%d], error %d\n", i, status); 14871da177e4SLinus Torvalds if (i == 0) { 14881da177e4SLinus Torvalds spin_unlock_irq (&context.lock); 14891da177e4SLinus Torvalds goto fail; 14901da177e4SLinus Torvalds } 14911da177e4SLinus Torvalds 14921da177e4SLinus Torvalds simple_free_urb (urbs [i]); 14931da177e4SLinus Torvalds context.pending--; 14949da2150fSAlan Stern context.submit_error = 1; 14959da2150fSAlan Stern break; 14961da177e4SLinus Torvalds } 14971da177e4SLinus Torvalds } 14981da177e4SLinus Torvalds spin_unlock_irq (&context.lock); 14991da177e4SLinus Torvalds 15001da177e4SLinus Torvalds wait_for_completion (&context.done); 15019da2150fSAlan Stern 15029da2150fSAlan Stern /* 15039da2150fSAlan Stern * Isochronous transfers are expected to fail sometimes. As an 15049da2150fSAlan Stern * arbitrary limit, we will report an error if any submissions 15059da2150fSAlan Stern * fail or if the transfer failure rate is > 10%. 15069da2150fSAlan Stern */ 15079da2150fSAlan Stern if (status != 0) 15089da2150fSAlan Stern ; 15099da2150fSAlan Stern else if (context.submit_error) 15109da2150fSAlan Stern status = -EACCES; 15119da2150fSAlan Stern else if (context.errors > context.packet_count / 10) 15129da2150fSAlan Stern status = -EIO; 15139da2150fSAlan Stern return status; 15141da177e4SLinus Torvalds 15151da177e4SLinus Torvalds fail: 15161da177e4SLinus Torvalds for (i = 0; i < param->sglen; i++) { 15171da177e4SLinus Torvalds if (urbs [i]) 15181da177e4SLinus Torvalds simple_free_urb (urbs [i]); 15191da177e4SLinus Torvalds } 15201da177e4SLinus Torvalds return status; 15211da177e4SLinus Torvalds } 15221da177e4SLinus Torvalds 15231da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 15241da177e4SLinus Torvalds 15251da177e4SLinus Torvalds /* We only have this one interface to user space, through usbfs. 15261da177e4SLinus Torvalds * User mode code can scan usbfs to find N different devices (maybe on 15271da177e4SLinus Torvalds * different busses) to use when testing, and allocate one thread per 15281da177e4SLinus Torvalds * test. So discovery is simplified, and we have no device naming issues. 15291da177e4SLinus Torvalds * 15301da177e4SLinus Torvalds * Don't use these only as stress/load tests. Use them along with with 15311da177e4SLinus Torvalds * other USB bus activity: plugging, unplugging, mousing, mp3 playback, 15321da177e4SLinus Torvalds * video capture, and so on. Run different tests at different times, in 15331da177e4SLinus Torvalds * different sequences. Nothing here should interact with other devices, 15341da177e4SLinus Torvalds * except indirectly by consuming USB bandwidth and CPU resources for test 15351da177e4SLinus Torvalds * threads and request completion. But the only way to know that for sure 15361da177e4SLinus Torvalds * is to test when HC queues are in use by many devices. 15371da177e4SLinus Torvalds */ 15381da177e4SLinus Torvalds 15391da177e4SLinus Torvalds static int 15401da177e4SLinus Torvalds usbtest_ioctl (struct usb_interface *intf, unsigned int code, void *buf) 15411da177e4SLinus Torvalds { 15421da177e4SLinus Torvalds struct usbtest_dev *dev = usb_get_intfdata (intf); 15431da177e4SLinus Torvalds struct usb_device *udev = testdev_to_usbdev (dev); 15441da177e4SLinus Torvalds struct usbtest_param *param = buf; 15451da177e4SLinus Torvalds int retval = -EOPNOTSUPP; 15461da177e4SLinus Torvalds struct urb *urb; 15471da177e4SLinus Torvalds struct scatterlist *sg; 15481da177e4SLinus Torvalds struct usb_sg_request req; 15491da177e4SLinus Torvalds struct timeval start; 15501da177e4SLinus Torvalds unsigned i; 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds // FIXME USBDEVFS_CONNECTINFO doesn't say how fast the device is. 15531da177e4SLinus Torvalds 15541da177e4SLinus Torvalds if (code != USBTEST_REQUEST) 15551da177e4SLinus Torvalds return -EOPNOTSUPP; 15561da177e4SLinus Torvalds 15571da177e4SLinus Torvalds if (param->iterations <= 0 || param->length < 0 15581da177e4SLinus Torvalds || param->sglen < 0 || param->vary < 0) 15591da177e4SLinus Torvalds return -EINVAL; 15601da177e4SLinus Torvalds 15611da177e4SLinus Torvalds if (down_interruptible (&dev->sem)) 15621da177e4SLinus Torvalds return -ERESTARTSYS; 15631da177e4SLinus Torvalds 1564ca078baeSPavel Machek if (intf->dev.power.power_state.event != PM_EVENT_ON) { 1565ff7c79e4SDavid Brownell up (&dev->sem); 1566ff7c79e4SDavid Brownell return -EHOSTUNREACH; 1567ff7c79e4SDavid Brownell } 1568ff7c79e4SDavid Brownell 15691da177e4SLinus Torvalds /* some devices, like ez-usb default devices, need a non-default 15701da177e4SLinus Torvalds * altsetting to have any active endpoints. some tests change 15711da177e4SLinus Torvalds * altsettings; force a default so most tests don't need to check. 15721da177e4SLinus Torvalds */ 15731da177e4SLinus Torvalds if (dev->info->alt >= 0) { 15741da177e4SLinus Torvalds int res; 15751da177e4SLinus Torvalds 15761da177e4SLinus Torvalds if (intf->altsetting->desc.bInterfaceNumber) { 15771da177e4SLinus Torvalds up (&dev->sem); 15781da177e4SLinus Torvalds return -ENODEV; 15791da177e4SLinus Torvalds } 15801da177e4SLinus Torvalds res = set_altsetting (dev, dev->info->alt); 15811da177e4SLinus Torvalds if (res) { 15821da177e4SLinus Torvalds dev_err (&intf->dev, 15831da177e4SLinus Torvalds "set altsetting to %d failed, %d\n", 15841da177e4SLinus Torvalds dev->info->alt, res); 15851da177e4SLinus Torvalds up (&dev->sem); 15861da177e4SLinus Torvalds return res; 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds } 15891da177e4SLinus Torvalds 15901da177e4SLinus Torvalds /* 15911da177e4SLinus Torvalds * Just a bunch of test cases that every HCD is expected to handle. 15921da177e4SLinus Torvalds * 15931da177e4SLinus Torvalds * Some may need specific firmware, though it'd be good to have 15941da177e4SLinus Torvalds * one firmware image to handle all the test cases. 15951da177e4SLinus Torvalds * 15961da177e4SLinus Torvalds * FIXME add more tests! cancel requests, verify the data, control 15971da177e4SLinus Torvalds * queueing, concurrent read+write threads, and so on. 15981da177e4SLinus Torvalds */ 15991da177e4SLinus Torvalds do_gettimeofday (&start); 16001da177e4SLinus Torvalds switch (param->test_num) { 16011da177e4SLinus Torvalds 16021da177e4SLinus Torvalds case 0: 16031da177e4SLinus Torvalds dev_dbg (&intf->dev, "TEST 0: NOP\n"); 16041da177e4SLinus Torvalds retval = 0; 16051da177e4SLinus Torvalds break; 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds /* Simple non-queued bulk I/O tests */ 16081da177e4SLinus Torvalds case 1: 16091da177e4SLinus Torvalds if (dev->out_pipe == 0) 16101da177e4SLinus Torvalds break; 16111da177e4SLinus Torvalds dev_dbg (&intf->dev, 16121da177e4SLinus Torvalds "TEST 1: write %d bytes %u times\n", 16131da177e4SLinus Torvalds param->length, param->iterations); 16141da177e4SLinus Torvalds urb = simple_alloc_urb (udev, dev->out_pipe, param->length); 16151da177e4SLinus Torvalds if (!urb) { 16161da177e4SLinus Torvalds retval = -ENOMEM; 16171da177e4SLinus Torvalds break; 16181da177e4SLinus Torvalds } 16191da177e4SLinus Torvalds // FIRMWARE: bulk sink (maybe accepts short writes) 16201da177e4SLinus Torvalds retval = simple_io (urb, param->iterations, 0, 0, "test1"); 16211da177e4SLinus Torvalds simple_free_urb (urb); 16221da177e4SLinus Torvalds break; 16231da177e4SLinus Torvalds case 2: 16241da177e4SLinus Torvalds if (dev->in_pipe == 0) 16251da177e4SLinus Torvalds break; 16261da177e4SLinus Torvalds dev_dbg (&intf->dev, 16271da177e4SLinus Torvalds "TEST 2: read %d bytes %u times\n", 16281da177e4SLinus Torvalds param->length, param->iterations); 16291da177e4SLinus Torvalds urb = simple_alloc_urb (udev, dev->in_pipe, param->length); 16301da177e4SLinus Torvalds if (!urb) { 16311da177e4SLinus Torvalds retval = -ENOMEM; 16321da177e4SLinus Torvalds break; 16331da177e4SLinus Torvalds } 16341da177e4SLinus Torvalds // FIRMWARE: bulk source (maybe generates short writes) 16351da177e4SLinus Torvalds retval = simple_io (urb, param->iterations, 0, 0, "test2"); 16361da177e4SLinus Torvalds simple_free_urb (urb); 16371da177e4SLinus Torvalds break; 16381da177e4SLinus Torvalds case 3: 16391da177e4SLinus Torvalds if (dev->out_pipe == 0 || param->vary == 0) 16401da177e4SLinus Torvalds break; 16411da177e4SLinus Torvalds dev_dbg (&intf->dev, 16421da177e4SLinus Torvalds "TEST 3: write/%d 0..%d bytes %u times\n", 16431da177e4SLinus Torvalds param->vary, param->length, param->iterations); 16441da177e4SLinus Torvalds urb = simple_alloc_urb (udev, dev->out_pipe, param->length); 16451da177e4SLinus Torvalds if (!urb) { 16461da177e4SLinus Torvalds retval = -ENOMEM; 16471da177e4SLinus Torvalds break; 16481da177e4SLinus Torvalds } 16491da177e4SLinus Torvalds // FIRMWARE: bulk sink (maybe accepts short writes) 16501da177e4SLinus Torvalds retval = simple_io (urb, param->iterations, param->vary, 16511da177e4SLinus Torvalds 0, "test3"); 16521da177e4SLinus Torvalds simple_free_urb (urb); 16531da177e4SLinus Torvalds break; 16541da177e4SLinus Torvalds case 4: 16551da177e4SLinus Torvalds if (dev->in_pipe == 0 || param->vary == 0) 16561da177e4SLinus Torvalds break; 16571da177e4SLinus Torvalds dev_dbg (&intf->dev, 16581da177e4SLinus Torvalds "TEST 4: read/%d 0..%d bytes %u times\n", 16591da177e4SLinus Torvalds param->vary, param->length, param->iterations); 16601da177e4SLinus Torvalds urb = simple_alloc_urb (udev, dev->in_pipe, param->length); 16611da177e4SLinus Torvalds if (!urb) { 16621da177e4SLinus Torvalds retval = -ENOMEM; 16631da177e4SLinus Torvalds break; 16641da177e4SLinus Torvalds } 16651da177e4SLinus Torvalds // FIRMWARE: bulk source (maybe generates short writes) 16661da177e4SLinus Torvalds retval = simple_io (urb, param->iterations, param->vary, 16671da177e4SLinus Torvalds 0, "test4"); 16681da177e4SLinus Torvalds simple_free_urb (urb); 16691da177e4SLinus Torvalds break; 16701da177e4SLinus Torvalds 16711da177e4SLinus Torvalds /* Queued bulk I/O tests */ 16721da177e4SLinus Torvalds case 5: 16731da177e4SLinus Torvalds if (dev->out_pipe == 0 || param->sglen == 0) 16741da177e4SLinus Torvalds break; 16751da177e4SLinus Torvalds dev_dbg (&intf->dev, 16761da177e4SLinus Torvalds "TEST 5: write %d sglists %d entries of %d bytes\n", 16771da177e4SLinus Torvalds param->iterations, 16781da177e4SLinus Torvalds param->sglen, param->length); 16791da177e4SLinus Torvalds sg = alloc_sglist (param->sglen, param->length, 0); 16801da177e4SLinus Torvalds if (!sg) { 16811da177e4SLinus Torvalds retval = -ENOMEM; 16821da177e4SLinus Torvalds break; 16831da177e4SLinus Torvalds } 16841da177e4SLinus Torvalds // FIRMWARE: bulk sink (maybe accepts short writes) 16851da177e4SLinus Torvalds retval = perform_sglist (udev, param->iterations, dev->out_pipe, 16861da177e4SLinus Torvalds &req, sg, param->sglen); 16871da177e4SLinus Torvalds free_sglist (sg, param->sglen); 16881da177e4SLinus Torvalds break; 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds case 6: 16911da177e4SLinus Torvalds if (dev->in_pipe == 0 || param->sglen == 0) 16921da177e4SLinus Torvalds break; 16931da177e4SLinus Torvalds dev_dbg (&intf->dev, 16941da177e4SLinus Torvalds "TEST 6: read %d sglists %d entries of %d bytes\n", 16951da177e4SLinus Torvalds param->iterations, 16961da177e4SLinus Torvalds param->sglen, param->length); 16971da177e4SLinus Torvalds sg = alloc_sglist (param->sglen, param->length, 0); 16981da177e4SLinus Torvalds if (!sg) { 16991da177e4SLinus Torvalds retval = -ENOMEM; 17001da177e4SLinus Torvalds break; 17011da177e4SLinus Torvalds } 17021da177e4SLinus Torvalds // FIRMWARE: bulk source (maybe generates short writes) 17031da177e4SLinus Torvalds retval = perform_sglist (udev, param->iterations, dev->in_pipe, 17041da177e4SLinus Torvalds &req, sg, param->sglen); 17051da177e4SLinus Torvalds free_sglist (sg, param->sglen); 17061da177e4SLinus Torvalds break; 17071da177e4SLinus Torvalds case 7: 17081da177e4SLinus Torvalds if (dev->out_pipe == 0 || param->sglen == 0 || param->vary == 0) 17091da177e4SLinus Torvalds break; 17101da177e4SLinus Torvalds dev_dbg (&intf->dev, 17111da177e4SLinus Torvalds "TEST 7: write/%d %d sglists %d entries 0..%d bytes\n", 17121da177e4SLinus Torvalds param->vary, param->iterations, 17131da177e4SLinus Torvalds param->sglen, param->length); 17141da177e4SLinus Torvalds sg = alloc_sglist (param->sglen, param->length, param->vary); 17151da177e4SLinus Torvalds if (!sg) { 17161da177e4SLinus Torvalds retval = -ENOMEM; 17171da177e4SLinus Torvalds break; 17181da177e4SLinus Torvalds } 17191da177e4SLinus Torvalds // FIRMWARE: bulk sink (maybe accepts short writes) 17201da177e4SLinus Torvalds retval = perform_sglist (udev, param->iterations, dev->out_pipe, 17211da177e4SLinus Torvalds &req, sg, param->sglen); 17221da177e4SLinus Torvalds free_sglist (sg, param->sglen); 17231da177e4SLinus Torvalds break; 17241da177e4SLinus Torvalds case 8: 17251da177e4SLinus Torvalds if (dev->in_pipe == 0 || param->sglen == 0 || param->vary == 0) 17261da177e4SLinus Torvalds break; 17271da177e4SLinus Torvalds dev_dbg (&intf->dev, 17281da177e4SLinus Torvalds "TEST 8: read/%d %d sglists %d entries 0..%d bytes\n", 17291da177e4SLinus Torvalds param->vary, param->iterations, 17301da177e4SLinus Torvalds param->sglen, param->length); 17311da177e4SLinus Torvalds sg = alloc_sglist (param->sglen, param->length, param->vary); 17321da177e4SLinus Torvalds if (!sg) { 17331da177e4SLinus Torvalds retval = -ENOMEM; 17341da177e4SLinus Torvalds break; 17351da177e4SLinus Torvalds } 17361da177e4SLinus Torvalds // FIRMWARE: bulk source (maybe generates short writes) 17371da177e4SLinus Torvalds retval = perform_sglist (udev, param->iterations, dev->in_pipe, 17381da177e4SLinus Torvalds &req, sg, param->sglen); 17391da177e4SLinus Torvalds free_sglist (sg, param->sglen); 17401da177e4SLinus Torvalds break; 17411da177e4SLinus Torvalds 17421da177e4SLinus Torvalds /* non-queued sanity tests for control (chapter 9 subset) */ 17431da177e4SLinus Torvalds case 9: 17441da177e4SLinus Torvalds retval = 0; 17451da177e4SLinus Torvalds dev_dbg (&intf->dev, 17461da177e4SLinus Torvalds "TEST 9: ch9 (subset) control tests, %d times\n", 17471da177e4SLinus Torvalds param->iterations); 17481da177e4SLinus Torvalds for (i = param->iterations; retval == 0 && i--; /* NOP */) 17491da177e4SLinus Torvalds retval = ch9_postconfig (dev); 17501da177e4SLinus Torvalds if (retval) 17511da177e4SLinus Torvalds dbg ("ch9 subset failed, iterations left %d", i); 17521da177e4SLinus Torvalds break; 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds /* queued control messaging */ 17551da177e4SLinus Torvalds case 10: 17561da177e4SLinus Torvalds if (param->sglen == 0) 17571da177e4SLinus Torvalds break; 17581da177e4SLinus Torvalds retval = 0; 17591da177e4SLinus Torvalds dev_dbg (&intf->dev, 17601da177e4SLinus Torvalds "TEST 10: queue %d control calls, %d times\n", 17611da177e4SLinus Torvalds param->sglen, 17621da177e4SLinus Torvalds param->iterations); 17631da177e4SLinus Torvalds retval = test_ctrl_queue (dev, param); 17641da177e4SLinus Torvalds break; 17651da177e4SLinus Torvalds 17661da177e4SLinus Torvalds /* simple non-queued unlinks (ring with one urb) */ 17671da177e4SLinus Torvalds case 11: 17681da177e4SLinus Torvalds if (dev->in_pipe == 0 || !param->length) 17691da177e4SLinus Torvalds break; 17701da177e4SLinus Torvalds retval = 0; 17711da177e4SLinus Torvalds dev_dbg (&intf->dev, "TEST 11: unlink %d reads of %d\n", 17721da177e4SLinus Torvalds param->iterations, param->length); 17731da177e4SLinus Torvalds for (i = param->iterations; retval == 0 && i--; /* NOP */) 17741da177e4SLinus Torvalds retval = unlink_simple (dev, dev->in_pipe, 17751da177e4SLinus Torvalds param->length); 17761da177e4SLinus Torvalds if (retval) 17771da177e4SLinus Torvalds dev_dbg (&intf->dev, "unlink reads failed %d, " 17781da177e4SLinus Torvalds "iterations left %d\n", retval, i); 17791da177e4SLinus Torvalds break; 17801da177e4SLinus Torvalds case 12: 17811da177e4SLinus Torvalds if (dev->out_pipe == 0 || !param->length) 17821da177e4SLinus Torvalds break; 17831da177e4SLinus Torvalds retval = 0; 17841da177e4SLinus Torvalds dev_dbg (&intf->dev, "TEST 12: unlink %d writes of %d\n", 17851da177e4SLinus Torvalds param->iterations, param->length); 17861da177e4SLinus Torvalds for (i = param->iterations; retval == 0 && i--; /* NOP */) 17871da177e4SLinus Torvalds retval = unlink_simple (dev, dev->out_pipe, 17881da177e4SLinus Torvalds param->length); 17891da177e4SLinus Torvalds if (retval) 17901da177e4SLinus Torvalds dev_dbg (&intf->dev, "unlink writes failed %d, " 17911da177e4SLinus Torvalds "iterations left %d\n", retval, i); 17921da177e4SLinus Torvalds break; 17931da177e4SLinus Torvalds 17941da177e4SLinus Torvalds /* ep halt tests */ 17951da177e4SLinus Torvalds case 13: 17961da177e4SLinus Torvalds if (dev->out_pipe == 0 && dev->in_pipe == 0) 17971da177e4SLinus Torvalds break; 17981da177e4SLinus Torvalds retval = 0; 17991da177e4SLinus Torvalds dev_dbg (&intf->dev, "TEST 13: set/clear %d halts\n", 18001da177e4SLinus Torvalds param->iterations); 18011da177e4SLinus Torvalds for (i = param->iterations; retval == 0 && i--; /* NOP */) 18021da177e4SLinus Torvalds retval = halt_simple (dev); 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds if (retval) 18051da177e4SLinus Torvalds DBG (dev, "halts failed, iterations left %d\n", i); 18061da177e4SLinus Torvalds break; 18071da177e4SLinus Torvalds 18081da177e4SLinus Torvalds /* control write tests */ 18091da177e4SLinus Torvalds case 14: 18101da177e4SLinus Torvalds if (!dev->info->ctrl_out) 18111da177e4SLinus Torvalds break; 1812ff7c79e4SDavid Brownell dev_dbg (&intf->dev, "TEST 14: %d ep0out, %d..%d vary %d\n", 1813ff7c79e4SDavid Brownell param->iterations, 1814ff7c79e4SDavid Brownell realworld ? 1 : 0, param->length, 1815ff7c79e4SDavid Brownell param->vary); 18161da177e4SLinus Torvalds retval = ctrl_out (dev, param->iterations, 18171da177e4SLinus Torvalds param->length, param->vary); 18181da177e4SLinus Torvalds break; 18191da177e4SLinus Torvalds 18201da177e4SLinus Torvalds /* iso write tests */ 18211da177e4SLinus Torvalds case 15: 18221da177e4SLinus Torvalds if (dev->out_iso_pipe == 0 || param->sglen == 0) 18231da177e4SLinus Torvalds break; 18241da177e4SLinus Torvalds dev_dbg (&intf->dev, 18251da177e4SLinus Torvalds "TEST 15: write %d iso, %d entries of %d bytes\n", 18261da177e4SLinus Torvalds param->iterations, 18271da177e4SLinus Torvalds param->sglen, param->length); 18281da177e4SLinus Torvalds // FIRMWARE: iso sink 18291da177e4SLinus Torvalds retval = test_iso_queue (dev, param, 18301da177e4SLinus Torvalds dev->out_iso_pipe, dev->iso_out); 18311da177e4SLinus Torvalds break; 18321da177e4SLinus Torvalds 18331da177e4SLinus Torvalds /* iso read tests */ 18341da177e4SLinus Torvalds case 16: 18351da177e4SLinus Torvalds if (dev->in_iso_pipe == 0 || param->sglen == 0) 18361da177e4SLinus Torvalds break; 18371da177e4SLinus Torvalds dev_dbg (&intf->dev, 18381da177e4SLinus Torvalds "TEST 16: read %d iso, %d entries of %d bytes\n", 18391da177e4SLinus Torvalds param->iterations, 18401da177e4SLinus Torvalds param->sglen, param->length); 18411da177e4SLinus Torvalds // FIRMWARE: iso source 18421da177e4SLinus Torvalds retval = test_iso_queue (dev, param, 18431da177e4SLinus Torvalds dev->in_iso_pipe, dev->iso_in); 18441da177e4SLinus Torvalds break; 18451da177e4SLinus Torvalds 18461da177e4SLinus Torvalds // FIXME unlink from queue (ring with N urbs) 18471da177e4SLinus Torvalds 18481da177e4SLinus Torvalds // FIXME scatterlist cancel (needs helper thread) 18491da177e4SLinus Torvalds 18501da177e4SLinus Torvalds } 18511da177e4SLinus Torvalds do_gettimeofday (¶m->duration); 18521da177e4SLinus Torvalds param->duration.tv_sec -= start.tv_sec; 18531da177e4SLinus Torvalds param->duration.tv_usec -= start.tv_usec; 18541da177e4SLinus Torvalds if (param->duration.tv_usec < 0) { 18551da177e4SLinus Torvalds param->duration.tv_usec += 1000 * 1000; 18561da177e4SLinus Torvalds param->duration.tv_sec -= 1; 18571da177e4SLinus Torvalds } 18581da177e4SLinus Torvalds up (&dev->sem); 18591da177e4SLinus Torvalds return retval; 18601da177e4SLinus Torvalds } 18611da177e4SLinus Torvalds 18621da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 18631da177e4SLinus Torvalds 18641da177e4SLinus Torvalds static unsigned force_interrupt = 0; 18651da177e4SLinus Torvalds module_param (force_interrupt, uint, 0); 18661da177e4SLinus Torvalds MODULE_PARM_DESC (force_interrupt, "0 = test default; else interrupt"); 18671da177e4SLinus Torvalds 18681da177e4SLinus Torvalds #ifdef GENERIC 18691da177e4SLinus Torvalds static unsigned short vendor; 18701da177e4SLinus Torvalds module_param(vendor, ushort, 0); 18711da177e4SLinus Torvalds MODULE_PARM_DESC (vendor, "vendor code (from usb-if)"); 18721da177e4SLinus Torvalds 18731da177e4SLinus Torvalds static unsigned short product; 18741da177e4SLinus Torvalds module_param(product, ushort, 0); 18751da177e4SLinus Torvalds MODULE_PARM_DESC (product, "product code (from vendor)"); 18761da177e4SLinus Torvalds #endif 18771da177e4SLinus Torvalds 18781da177e4SLinus Torvalds static int 18791da177e4SLinus Torvalds usbtest_probe (struct usb_interface *intf, const struct usb_device_id *id) 18801da177e4SLinus Torvalds { 18811da177e4SLinus Torvalds struct usb_device *udev; 18821da177e4SLinus Torvalds struct usbtest_dev *dev; 18831da177e4SLinus Torvalds struct usbtest_info *info; 18841da177e4SLinus Torvalds char *rtest, *wtest; 18851da177e4SLinus Torvalds char *irtest, *iwtest; 18861da177e4SLinus Torvalds 18871da177e4SLinus Torvalds udev = interface_to_usbdev (intf); 18881da177e4SLinus Torvalds 18891da177e4SLinus Torvalds #ifdef GENERIC 18901da177e4SLinus Torvalds /* specify devices by module parameters? */ 18911da177e4SLinus Torvalds if (id->match_flags == 0) { 18921da177e4SLinus Torvalds /* vendor match required, product match optional */ 18931da177e4SLinus Torvalds if (!vendor || le16_to_cpu(udev->descriptor.idVendor) != (u16)vendor) 18941da177e4SLinus Torvalds return -ENODEV; 18951da177e4SLinus Torvalds if (product && le16_to_cpu(udev->descriptor.idProduct) != (u16)product) 18961da177e4SLinus Torvalds return -ENODEV; 18971da177e4SLinus Torvalds dbg ("matched module params, vend=0x%04x prod=0x%04x", 18981da177e4SLinus Torvalds le16_to_cpu(udev->descriptor.idVendor), 18991da177e4SLinus Torvalds le16_to_cpu(udev->descriptor.idProduct)); 19001da177e4SLinus Torvalds } 19011da177e4SLinus Torvalds #endif 19021da177e4SLinus Torvalds 190380b6ca48SEric Sesterhenn dev = kzalloc(sizeof(*dev), SLAB_KERNEL); 19041da177e4SLinus Torvalds if (!dev) 19051da177e4SLinus Torvalds return -ENOMEM; 19061da177e4SLinus Torvalds info = (struct usbtest_info *) id->driver_info; 19071da177e4SLinus Torvalds dev->info = info; 19081da177e4SLinus Torvalds init_MUTEX (&dev->sem); 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds dev->intf = intf; 19111da177e4SLinus Torvalds 19121da177e4SLinus Torvalds /* cacheline-aligned scratch for i/o */ 19131da177e4SLinus Torvalds if ((dev->buf = kmalloc (TBUF_SIZE, SLAB_KERNEL)) == NULL) { 19141da177e4SLinus Torvalds kfree (dev); 19151da177e4SLinus Torvalds return -ENOMEM; 19161da177e4SLinus Torvalds } 19171da177e4SLinus Torvalds 19181da177e4SLinus Torvalds /* NOTE this doesn't yet test the handful of difference that are 19191da177e4SLinus Torvalds * visible with high speed interrupts: bigger maxpacket (1K) and 19201da177e4SLinus Torvalds * "high bandwidth" modes (up to 3 packets/uframe). 19211da177e4SLinus Torvalds */ 19221da177e4SLinus Torvalds rtest = wtest = ""; 19231da177e4SLinus Torvalds irtest = iwtest = ""; 19241da177e4SLinus Torvalds if (force_interrupt || udev->speed == USB_SPEED_LOW) { 19251da177e4SLinus Torvalds if (info->ep_in) { 19261da177e4SLinus Torvalds dev->in_pipe = usb_rcvintpipe (udev, info->ep_in); 19271da177e4SLinus Torvalds rtest = " intr-in"; 19281da177e4SLinus Torvalds } 19291da177e4SLinus Torvalds if (info->ep_out) { 19301da177e4SLinus Torvalds dev->out_pipe = usb_sndintpipe (udev, info->ep_out); 19311da177e4SLinus Torvalds wtest = " intr-out"; 19321da177e4SLinus Torvalds } 19331da177e4SLinus Torvalds } else { 19341da177e4SLinus Torvalds if (info->autoconf) { 19351da177e4SLinus Torvalds int status; 19361da177e4SLinus Torvalds 19371da177e4SLinus Torvalds status = get_endpoints (dev, intf); 19381da177e4SLinus Torvalds if (status < 0) { 19391da177e4SLinus Torvalds dbg ("couldn't get endpoints, %d\n", status); 19401da177e4SLinus Torvalds return status; 19411da177e4SLinus Torvalds } 19421da177e4SLinus Torvalds /* may find bulk or ISO pipes */ 19431da177e4SLinus Torvalds } else { 19441da177e4SLinus Torvalds if (info->ep_in) 19451da177e4SLinus Torvalds dev->in_pipe = usb_rcvbulkpipe (udev, 19461da177e4SLinus Torvalds info->ep_in); 19471da177e4SLinus Torvalds if (info->ep_out) 19481da177e4SLinus Torvalds dev->out_pipe = usb_sndbulkpipe (udev, 19491da177e4SLinus Torvalds info->ep_out); 19501da177e4SLinus Torvalds } 19511da177e4SLinus Torvalds if (dev->in_pipe) 19521da177e4SLinus Torvalds rtest = " bulk-in"; 19531da177e4SLinus Torvalds if (dev->out_pipe) 19541da177e4SLinus Torvalds wtest = " bulk-out"; 19551da177e4SLinus Torvalds if (dev->in_iso_pipe) 19561da177e4SLinus Torvalds irtest = " iso-in"; 19571da177e4SLinus Torvalds if (dev->out_iso_pipe) 19581da177e4SLinus Torvalds iwtest = " iso-out"; 19591da177e4SLinus Torvalds } 19601da177e4SLinus Torvalds 19611da177e4SLinus Torvalds usb_set_intfdata (intf, dev); 19621da177e4SLinus Torvalds dev_info (&intf->dev, "%s\n", info->name); 19631da177e4SLinus Torvalds dev_info (&intf->dev, "%s speed {control%s%s%s%s%s} tests%s\n", 19641da177e4SLinus Torvalds ({ char *tmp; 19651da177e4SLinus Torvalds switch (udev->speed) { 19661da177e4SLinus Torvalds case USB_SPEED_LOW: tmp = "low"; break; 19671da177e4SLinus Torvalds case USB_SPEED_FULL: tmp = "full"; break; 19681da177e4SLinus Torvalds case USB_SPEED_HIGH: tmp = "high"; break; 19691da177e4SLinus Torvalds default: tmp = "unknown"; break; 19701da177e4SLinus Torvalds }; tmp; }), 19711da177e4SLinus Torvalds info->ctrl_out ? " in/out" : "", 19721da177e4SLinus Torvalds rtest, wtest, 19731da177e4SLinus Torvalds irtest, iwtest, 19741da177e4SLinus Torvalds info->alt >= 0 ? " (+alt)" : ""); 19751da177e4SLinus Torvalds return 0; 19761da177e4SLinus Torvalds } 19771da177e4SLinus Torvalds 1978ff7c79e4SDavid Brownell static int usbtest_suspend (struct usb_interface *intf, pm_message_t message) 1979ff7c79e4SDavid Brownell { 1980ff7c79e4SDavid Brownell return 0; 1981ff7c79e4SDavid Brownell } 1982ff7c79e4SDavid Brownell 1983ff7c79e4SDavid Brownell static int usbtest_resume (struct usb_interface *intf) 1984ff7c79e4SDavid Brownell { 1985ff7c79e4SDavid Brownell return 0; 1986ff7c79e4SDavid Brownell } 1987ff7c79e4SDavid Brownell 1988ff7c79e4SDavid Brownell 19891da177e4SLinus Torvalds static void usbtest_disconnect (struct usb_interface *intf) 19901da177e4SLinus Torvalds { 19911da177e4SLinus Torvalds struct usbtest_dev *dev = usb_get_intfdata (intf); 19921da177e4SLinus Torvalds 19931da177e4SLinus Torvalds down (&dev->sem); 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds usb_set_intfdata (intf, NULL); 19961da177e4SLinus Torvalds dev_dbg (&intf->dev, "disconnect\n"); 19971da177e4SLinus Torvalds kfree (dev); 19981da177e4SLinus Torvalds } 19991da177e4SLinus Torvalds 20001da177e4SLinus Torvalds /* Basic testing only needs a device that can source or sink bulk traffic. 20011da177e4SLinus Torvalds * Any device can test control transfers (default with GENERIC binding). 20021da177e4SLinus Torvalds * 20031da177e4SLinus Torvalds * Several entries work with the default EP0 implementation that's built 20041da177e4SLinus Torvalds * into EZ-USB chips. There's a default vendor ID which can be overridden 20051da177e4SLinus Torvalds * by (very) small config EEPROMS, but otherwise all these devices act 20061da177e4SLinus Torvalds * identically until firmware is loaded: only EP0 works. It turns out 20071da177e4SLinus Torvalds * to be easy to make other endpoints work, without modifying that EP0 20081da177e4SLinus Torvalds * behavior. For now, we expect that kind of firmware. 20091da177e4SLinus Torvalds */ 20101da177e4SLinus Torvalds 20111da177e4SLinus Torvalds /* an21xx or fx versions of ez-usb */ 20121da177e4SLinus Torvalds static struct usbtest_info ez1_info = { 20131da177e4SLinus Torvalds .name = "EZ-USB device", 20141da177e4SLinus Torvalds .ep_in = 2, 20151da177e4SLinus Torvalds .ep_out = 2, 20161da177e4SLinus Torvalds .alt = 1, 20171da177e4SLinus Torvalds }; 20181da177e4SLinus Torvalds 20191da177e4SLinus Torvalds /* fx2 version of ez-usb */ 20201da177e4SLinus Torvalds static struct usbtest_info ez2_info = { 20211da177e4SLinus Torvalds .name = "FX2 device", 20221da177e4SLinus Torvalds .ep_in = 6, 20231da177e4SLinus Torvalds .ep_out = 2, 20241da177e4SLinus Torvalds .alt = 1, 20251da177e4SLinus Torvalds }; 20261da177e4SLinus Torvalds 20271da177e4SLinus Torvalds /* ezusb family device with dedicated usb test firmware, 20281da177e4SLinus Torvalds */ 20291da177e4SLinus Torvalds static struct usbtest_info fw_info = { 20301da177e4SLinus Torvalds .name = "usb test device", 20311da177e4SLinus Torvalds .ep_in = 2, 20321da177e4SLinus Torvalds .ep_out = 2, 20331da177e4SLinus Torvalds .alt = 1, 20341da177e4SLinus Torvalds .autoconf = 1, // iso and ctrl_out need autoconf 20351da177e4SLinus Torvalds .ctrl_out = 1, 20361da177e4SLinus Torvalds .iso = 1, // iso_ep's are #8 in/out 20371da177e4SLinus Torvalds }; 20381da177e4SLinus Torvalds 20391da177e4SLinus Torvalds /* peripheral running Linux and 'zero.c' test firmware, or 20401da177e4SLinus Torvalds * its user-mode cousin. different versions of this use 20411da177e4SLinus Torvalds * different hardware with the same vendor/product codes. 20421da177e4SLinus Torvalds * host side MUST rely on the endpoint descriptors. 20431da177e4SLinus Torvalds */ 20441da177e4SLinus Torvalds static struct usbtest_info gz_info = { 20451da177e4SLinus Torvalds .name = "Linux gadget zero", 20461da177e4SLinus Torvalds .autoconf = 1, 20471da177e4SLinus Torvalds .ctrl_out = 1, 20481da177e4SLinus Torvalds .alt = 0, 20491da177e4SLinus Torvalds }; 20501da177e4SLinus Torvalds 20511da177e4SLinus Torvalds static struct usbtest_info um_info = { 20521da177e4SLinus Torvalds .name = "Linux user mode test driver", 20531da177e4SLinus Torvalds .autoconf = 1, 20541da177e4SLinus Torvalds .alt = -1, 20551da177e4SLinus Torvalds }; 20561da177e4SLinus Torvalds 20571da177e4SLinus Torvalds static struct usbtest_info um2_info = { 20581da177e4SLinus Torvalds .name = "Linux user mode ISO test driver", 20591da177e4SLinus Torvalds .autoconf = 1, 20601da177e4SLinus Torvalds .iso = 1, 20611da177e4SLinus Torvalds .alt = -1, 20621da177e4SLinus Torvalds }; 20631da177e4SLinus Torvalds 20641da177e4SLinus Torvalds #ifdef IBOT2 20651da177e4SLinus Torvalds /* this is a nice source of high speed bulk data; 20661da177e4SLinus Torvalds * uses an FX2, with firmware provided in the device 20671da177e4SLinus Torvalds */ 20681da177e4SLinus Torvalds static struct usbtest_info ibot2_info = { 20691da177e4SLinus Torvalds .name = "iBOT2 webcam", 20701da177e4SLinus Torvalds .ep_in = 2, 20711da177e4SLinus Torvalds .alt = -1, 20721da177e4SLinus Torvalds }; 20731da177e4SLinus Torvalds #endif 20741da177e4SLinus Torvalds 20751da177e4SLinus Torvalds #ifdef GENERIC 20761da177e4SLinus Torvalds /* we can use any device to test control traffic */ 20771da177e4SLinus Torvalds static struct usbtest_info generic_info = { 20781da177e4SLinus Torvalds .name = "Generic USB device", 20791da177e4SLinus Torvalds .alt = -1, 20801da177e4SLinus Torvalds }; 20811da177e4SLinus Torvalds #endif 20821da177e4SLinus Torvalds 20831da177e4SLinus Torvalds // FIXME remove this 20841da177e4SLinus Torvalds static struct usbtest_info hact_info = { 20851da177e4SLinus Torvalds .name = "FX2/hact", 20861da177e4SLinus Torvalds //.ep_in = 6, 20871da177e4SLinus Torvalds .ep_out = 2, 20881da177e4SLinus Torvalds .alt = -1, 20891da177e4SLinus Torvalds }; 20901da177e4SLinus Torvalds 20911da177e4SLinus Torvalds 20921da177e4SLinus Torvalds static struct usb_device_id id_table [] = { 20931da177e4SLinus Torvalds 20941da177e4SLinus Torvalds { USB_DEVICE (0x0547, 0x1002), 20951da177e4SLinus Torvalds .driver_info = (unsigned long) &hact_info, 20961da177e4SLinus Torvalds }, 20971da177e4SLinus Torvalds 20981da177e4SLinus Torvalds /*-------------------------------------------------------------*/ 20991da177e4SLinus Torvalds 21001da177e4SLinus Torvalds /* EZ-USB devices which download firmware to replace (or in our 21011da177e4SLinus Torvalds * case augment) the default device implementation. 21021da177e4SLinus Torvalds */ 21031da177e4SLinus Torvalds 21041da177e4SLinus Torvalds /* generic EZ-USB FX controller */ 21051da177e4SLinus Torvalds { USB_DEVICE (0x0547, 0x2235), 21061da177e4SLinus Torvalds .driver_info = (unsigned long) &ez1_info, 21071da177e4SLinus Torvalds }, 21081da177e4SLinus Torvalds 21091da177e4SLinus Torvalds /* CY3671 development board with EZ-USB FX */ 21101da177e4SLinus Torvalds { USB_DEVICE (0x0547, 0x0080), 21111da177e4SLinus Torvalds .driver_info = (unsigned long) &ez1_info, 21121da177e4SLinus Torvalds }, 21131da177e4SLinus Torvalds 21141da177e4SLinus Torvalds /* generic EZ-USB FX2 controller (or development board) */ 21151da177e4SLinus Torvalds { USB_DEVICE (0x04b4, 0x8613), 21161da177e4SLinus Torvalds .driver_info = (unsigned long) &ez2_info, 21171da177e4SLinus Torvalds }, 21181da177e4SLinus Torvalds 21191da177e4SLinus Torvalds /* re-enumerated usb test device firmware */ 21201da177e4SLinus Torvalds { USB_DEVICE (0xfff0, 0xfff0), 21211da177e4SLinus Torvalds .driver_info = (unsigned long) &fw_info, 21221da177e4SLinus Torvalds }, 21231da177e4SLinus Torvalds 21241da177e4SLinus Torvalds /* "Gadget Zero" firmware runs under Linux */ 21251da177e4SLinus Torvalds { USB_DEVICE (0x0525, 0xa4a0), 21261da177e4SLinus Torvalds .driver_info = (unsigned long) &gz_info, 21271da177e4SLinus Torvalds }, 21281da177e4SLinus Torvalds 21291da177e4SLinus Torvalds /* so does a user-mode variant */ 21301da177e4SLinus Torvalds { USB_DEVICE (0x0525, 0xa4a4), 21311da177e4SLinus Torvalds .driver_info = (unsigned long) &um_info, 21321da177e4SLinus Torvalds }, 21331da177e4SLinus Torvalds 21341da177e4SLinus Torvalds /* ... and a user-mode variant that talks iso */ 21351da177e4SLinus Torvalds { USB_DEVICE (0x0525, 0xa4a3), 21361da177e4SLinus Torvalds .driver_info = (unsigned long) &um2_info, 21371da177e4SLinus Torvalds }, 21381da177e4SLinus Torvalds 21391da177e4SLinus Torvalds #ifdef KEYSPAN_19Qi 21401da177e4SLinus Torvalds /* Keyspan 19qi uses an21xx (original EZ-USB) */ 21411da177e4SLinus Torvalds // this does not coexist with the real Keyspan 19qi driver! 21421da177e4SLinus Torvalds { USB_DEVICE (0x06cd, 0x010b), 21431da177e4SLinus Torvalds .driver_info = (unsigned long) &ez1_info, 21441da177e4SLinus Torvalds }, 21451da177e4SLinus Torvalds #endif 21461da177e4SLinus Torvalds 21471da177e4SLinus Torvalds /*-------------------------------------------------------------*/ 21481da177e4SLinus Torvalds 21491da177e4SLinus Torvalds #ifdef IBOT2 21501da177e4SLinus Torvalds /* iBOT2 makes a nice source of high speed bulk-in data */ 21511da177e4SLinus Torvalds // this does not coexist with a real iBOT2 driver! 21521da177e4SLinus Torvalds { USB_DEVICE (0x0b62, 0x0059), 21531da177e4SLinus Torvalds .driver_info = (unsigned long) &ibot2_info, 21541da177e4SLinus Torvalds }, 21551da177e4SLinus Torvalds #endif 21561da177e4SLinus Torvalds 21571da177e4SLinus Torvalds /*-------------------------------------------------------------*/ 21581da177e4SLinus Torvalds 21591da177e4SLinus Torvalds #ifdef GENERIC 21601da177e4SLinus Torvalds /* module params can specify devices to use for control tests */ 21611da177e4SLinus Torvalds { .driver_info = (unsigned long) &generic_info, }, 21621da177e4SLinus Torvalds #endif 21631da177e4SLinus Torvalds 21641da177e4SLinus Torvalds /*-------------------------------------------------------------*/ 21651da177e4SLinus Torvalds 21661da177e4SLinus Torvalds { } 21671da177e4SLinus Torvalds }; 21681da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, id_table); 21691da177e4SLinus Torvalds 21701da177e4SLinus Torvalds static struct usb_driver usbtest_driver = { 21711da177e4SLinus Torvalds .name = "usbtest", 21721da177e4SLinus Torvalds .id_table = id_table, 21731da177e4SLinus Torvalds .probe = usbtest_probe, 21741da177e4SLinus Torvalds .ioctl = usbtest_ioctl, 21751da177e4SLinus Torvalds .disconnect = usbtest_disconnect, 2176ff7c79e4SDavid Brownell .suspend = usbtest_suspend, 2177ff7c79e4SDavid Brownell .resume = usbtest_resume, 21781da177e4SLinus Torvalds }; 21791da177e4SLinus Torvalds 21801da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/ 21811da177e4SLinus Torvalds 21821da177e4SLinus Torvalds static int __init usbtest_init (void) 21831da177e4SLinus Torvalds { 21841da177e4SLinus Torvalds #ifdef GENERIC 21851da177e4SLinus Torvalds if (vendor) 21861da177e4SLinus Torvalds dbg ("params: vend=0x%04x prod=0x%04x", vendor, product); 21871da177e4SLinus Torvalds #endif 21881da177e4SLinus Torvalds return usb_register (&usbtest_driver); 21891da177e4SLinus Torvalds } 21901da177e4SLinus Torvalds module_init (usbtest_init); 21911da177e4SLinus Torvalds 21921da177e4SLinus Torvalds static void __exit usbtest_exit (void) 21931da177e4SLinus Torvalds { 21941da177e4SLinus Torvalds usb_deregister (&usbtest_driver); 21951da177e4SLinus Torvalds } 21961da177e4SLinus Torvalds module_exit (usbtest_exit); 21971da177e4SLinus Torvalds 21981da177e4SLinus Torvalds MODULE_DESCRIPTION ("USB Core/HCD Testing Driver"); 21991da177e4SLinus Torvalds MODULE_LICENSE ("GPL"); 22001da177e4SLinus Torvalds 2201