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