blob: c7f82310e73ec3f933b6cff1e62dc37d367abe3f [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/kernel.h>
3#include <linux/errno.h>
4#include <linux/init.h>
5#include <linux/slab.h>
6#include <linux/mm.h>
7#include <linux/module.h>
8#include <linux/moduleparam.h>
David Hardeman378f0582005-09-17 17:55:31 +10009#include <linux/scatterlist.h>
Matthias Kaehlcke1cfab022007-12-13 16:15:33 -080010#include <linux/mutex.h>
Alan Stern32b36ee2014-06-03 11:11:34 -040011#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/usb.h>
13
Roger Quadrose5e47462013-12-18 15:40:10 +053014#define SIMPLE_IO_TIMEOUT 10000 /* in milliseconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16/*-------------------------------------------------------------------------*/
17
Alan Sternd0b46522013-01-30 16:38:11 -050018static int override_alt = -1;
19module_param_named(alt, override_alt, int, 0644);
20MODULE_PARM_DESC(alt, ">= 0 to override altsetting selection");
Peter Chen145f48c2015-10-13 15:18:21 +080021static void complicated_callback(struct urb *urb);
Alan Sternd0b46522013-01-30 16:38:11 -050022
23/*-------------------------------------------------------------------------*/
24
Martin Fuzzeyfabbf212010-10-01 00:20:42 +020025/* FIXME make these public somewhere; usbdevfs.h? */
Deepa Dinamani18fc4eb2015-11-04 15:29:11 -080026
27/* Parameter for usbtest driver. */
28struct usbtest_param_32 {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +020029 /* inputs */
Deepa Dinamani18fc4eb2015-11-04 15:29:11 -080030 __u32 test_num; /* 0..(TEST_CASES-1) */
31 __u32 iterations;
32 __u32 length;
33 __u32 vary;
34 __u32 sglen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Martin Fuzzeyfabbf212010-10-01 00:20:42 +020036 /* outputs */
Deepa Dinamani18fc4eb2015-11-04 15:29:11 -080037 __s32 duration_sec;
38 __s32 duration_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
Deepa Dinamani18fc4eb2015-11-04 15:29:11 -080040
41/*
42 * Compat parameter to the usbtest driver.
43 * This supports older user space binaries compiled with 64 bit compiler.
44 */
45struct usbtest_param_64 {
46 /* inputs */
47 __u32 test_num; /* 0..(TEST_CASES-1) */
48 __u32 iterations;
49 __u32 length;
50 __u32 vary;
51 __u32 sglen;
52
53 /* outputs */
54 __s64 duration_sec;
55 __s64 duration_usec;
56};
57
58/* IOCTL interface to the driver. */
59#define USBTEST_REQUEST_32 _IOWR('U', 100, struct usbtest_param_32)
60/* COMPAT IOCTL interface to the driver. */
61#define USBTEST_REQUEST_64 _IOWR('U', 100, struct usbtest_param_64)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/*-------------------------------------------------------------------------*/
64
65#define GENERIC /* let probe() bind using module params */
66
67/* Some devices that can be used for testing will have "real" drivers.
68 * Entries for those need to be enabled here by hand, after disabling
69 * that "real" driver.
70 */
71//#define IBOT2 /* grab iBOT2 webcams */
72//#define KEYSPAN_19Qi /* grab un-renumerated serial adapter */
73
74/*-------------------------------------------------------------------------*/
75
76struct usbtest_info {
77 const char *name;
78 u8 ep_in; /* bulk/intr source */
79 u8 ep_out; /* bulk/intr sink */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +020080 unsigned autoconf:1;
81 unsigned ctrl_out:1;
82 unsigned iso:1; /* try iso in/out */
Amit Virdi457a0952014-08-22 14:36:37 +053083 unsigned intr:1; /* try interrupt in/out */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 int alt;
85};
86
87/* this is accessed only through usbfs ioctl calls.
88 * one ioctl to issue a test ... one lock per device.
89 * tests create other threads if they need them.
90 * urbs and buffers are allocated dynamically,
91 * and data generated deterministically.
92 */
93struct usbtest_dev {
94 struct usb_interface *intf;
95 struct usbtest_info *info;
96 int in_pipe;
97 int out_pipe;
98 int in_iso_pipe;
99 int out_iso_pipe;
Amit Virdi457a0952014-08-22 14:36:37 +0530100 int in_int_pipe;
101 int out_int_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 struct usb_endpoint_descriptor *iso_in, *iso_out;
Amit Virdi457a0952014-08-22 14:36:37 +0530103 struct usb_endpoint_descriptor *int_in, *int_out;
Matthias Kaehlcke1cfab022007-12-13 16:15:33 -0800104 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106#define TBUF_SIZE 256
107 u8 *buf;
108};
109
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200110static struct usb_device *testdev_to_usbdev(struct usbtest_dev *test)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200112 return interface_to_usbdev(test->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115/* set up all urbs so they can be used with either bulk or interrupt */
116#define INTERRUPT_RATE 1 /* msec/transfer */
117
David Brownell28ffd792008-04-25 18:51:10 -0700118#define ERROR(tdev, fmt, args...) \
119 dev_err(&(tdev)->intf->dev , fmt , ## args)
Arjan van de Venb6c63932008-07-25 01:45:52 -0700120#define WARNING(tdev, fmt, args...) \
David Brownell28ffd792008-04-25 18:51:10 -0700121 dev_warn(&(tdev)->intf->dev , fmt , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Martin Fuzzey084fb202011-01-16 19:17:11 +0100123#define GUARD_BYTE 0xA5
Peter Chen41d3c0b2015-09-01 09:47:58 +0800124#define MAX_SGLEN 128
Martin Fuzzey084fb202011-01-16 19:17:11 +0100125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/*-------------------------------------------------------------------------*/
127
Gustavo A. R. Silva65c78432017-04-03 22:51:54 -0500128static inline void endpoint_update(int edi,
129 struct usb_host_endpoint **in,
130 struct usb_host_endpoint **out,
131 struct usb_host_endpoint *e)
132{
133 if (edi) {
134 if (!*in)
135 *in = e;
136 } else {
137 if (!*out)
138 *out = e;
139 }
140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static int
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200143get_endpoints(struct usbtest_dev *dev, struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 int tmp;
146 struct usb_host_interface *alt;
147 struct usb_host_endpoint *in, *out;
148 struct usb_host_endpoint *iso_in, *iso_out;
Amit Virdi457a0952014-08-22 14:36:37 +0530149 struct usb_host_endpoint *int_in, *int_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct usb_device *udev;
151
152 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
153 unsigned ep;
154
155 in = out = NULL;
156 iso_in = iso_out = NULL;
Amit Virdi457a0952014-08-22 14:36:37 +0530157 int_in = int_out = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 alt = intf->altsetting + tmp;
159
Alan Sternd0b46522013-01-30 16:38:11 -0500160 if (override_alt >= 0 &&
161 override_alt != alt->desc.bAlternateSetting)
162 continue;
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /* take the first altsetting with in-bulk + out-bulk;
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200165 * ignore other endpoints and altsettings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 */
167 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
168 struct usb_host_endpoint *e;
Gustavo A. R. Silva65c78432017-04-03 22:51:54 -0500169 int edi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 e = alt->endpoint + ep;
Gustavo A. R. Silva65c78432017-04-03 22:51:54 -0500172 edi = usb_endpoint_dir_in(&e->desc);
173
Huang Rui9a37a502013-09-24 00:03:43 +0800174 switch (usb_endpoint_type(&e->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 case USB_ENDPOINT_XFER_BULK:
Gustavo A. R. Silva65c78432017-04-03 22:51:54 -0500176 endpoint_update(edi, &in, &out, e);
177 continue;
Amit Virdi457a0952014-08-22 14:36:37 +0530178 case USB_ENDPOINT_XFER_INT:
179 if (dev->info->intr)
Gustavo A. R. Silva65c78432017-04-03 22:51:54 -0500180 endpoint_update(edi, &int_in, &int_out, e);
Gustavo A. R. Silva2c930e32017-04-03 22:48:40 -0500181 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 case USB_ENDPOINT_XFER_ISOC:
183 if (dev->info->iso)
Gustavo A. R. Silva65c78432017-04-03 22:51:54 -0500184 endpoint_update(edi, &iso_in, &iso_out, e);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200185 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 default:
187 continue;
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
Amit Virdi457a0952014-08-22 14:36:37 +0530190 if ((in && out) || iso_in || iso_out || int_in || int_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 goto found;
192 }
193 return -EINVAL;
194
195found:
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200196 udev = testdev_to_usbdev(dev);
Alan Sternd0b46522013-01-30 16:38:11 -0500197 dev->info->alt = alt->desc.bAlternateSetting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (alt->desc.bAlternateSetting != 0) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200199 tmp = usb_set_interface(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 alt->desc.bInterfaceNumber,
201 alt->desc.bAlternateSetting);
202 if (tmp < 0)
203 return tmp;
204 }
205
Alan Stern7c80f9e2017-09-29 10:54:24 -0400206 if (in)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200207 dev->in_pipe = usb_rcvbulkpipe(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Alan Stern7c80f9e2017-09-29 10:54:24 -0400209 if (out)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200210 dev->out_pipe = usb_sndbulkpipe(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Alan Stern7c80f9e2017-09-29 10:54:24 -0400212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (iso_in) {
214 dev->iso_in = &iso_in->desc;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200215 dev->in_iso_pipe = usb_rcvisocpipe(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 iso_in->desc.bEndpointAddress
217 & USB_ENDPOINT_NUMBER_MASK);
Ming Lei951fd8e2010-08-02 22:09:17 +0800218 }
219
220 if (iso_out) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 dev->iso_out = &iso_out->desc;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200222 dev->out_iso_pipe = usb_sndisocpipe(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 iso_out->desc.bEndpointAddress
224 & USB_ENDPOINT_NUMBER_MASK);
225 }
Amit Virdi457a0952014-08-22 14:36:37 +0530226
227 if (int_in) {
228 dev->int_in = &int_in->desc;
229 dev->in_int_pipe = usb_rcvintpipe(udev,
230 int_in->desc.bEndpointAddress
231 & USB_ENDPOINT_NUMBER_MASK);
232 }
233
234 if (int_out) {
235 dev->int_out = &int_out->desc;
236 dev->out_int_pipe = usb_sndintpipe(udev,
237 int_out->desc.bEndpointAddress
238 & USB_ENDPOINT_NUMBER_MASK);
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return 0;
241}
242
243/*-------------------------------------------------------------------------*/
244
245/* Support for testing basic non-queued I/O streams.
246 *
247 * These just package urbs as requests that can be easily canceled.
248 * Each urb's data buffer is dynamically allocated; callers can fill
249 * them with non-zero test data (or test for it) when appropriate.
250 */
251
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200252static void simple_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Ming Leicdc97792008-02-24 18:41:47 +0800254 complete(urb->context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Martin Fuzzey084fb202011-01-16 19:17:11 +0100257static struct urb *usbtest_alloc_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 struct usb_device *udev,
259 int pipe,
Martin Fuzzey084fb202011-01-16 19:17:11 +0100260 unsigned long bytes,
261 unsigned transfer_flags,
Amit Virdi457a0952014-08-22 14:36:37 +0530262 unsigned offset,
Peter Chen145f48c2015-10-13 15:18:21 +0800263 u8 bInterval,
264 usb_complete_t complete_fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 struct urb *urb;
267
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200268 urb = usb_alloc_urb(0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (!urb)
270 return urb;
Amit Virdi457a0952014-08-22 14:36:37 +0530271
272 if (bInterval)
Peter Chen145f48c2015-10-13 15:18:21 +0800273 usb_fill_int_urb(urb, udev, pipe, NULL, bytes, complete_fn,
Amit Virdi457a0952014-08-22 14:36:37 +0530274 NULL, bInterval);
275 else
Peter Chen145f48c2015-10-13 15:18:21 +0800276 usb_fill_bulk_urb(urb, udev, pipe, NULL, bytes, complete_fn,
Amit Virdi457a0952014-08-22 14:36:37 +0530277 NULL);
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 urb->interval = (udev->speed == USB_SPEED_HIGH)
280 ? (INTERRUPT_RATE << 3)
281 : INTERRUPT_RATE;
Martin Fuzzey084fb202011-01-16 19:17:11 +0100282 urb->transfer_flags = transfer_flags;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200283 if (usb_pipein(pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 urb->transfer_flags |= URB_SHORT_NOT_OK;
Martin Fuzzey084fb202011-01-16 19:17:11 +0100285
Chunfeng Yun26186e52016-04-28 11:42:21 +0800286 if ((bytes + offset) == 0)
287 return urb;
288
Martin Fuzzey084fb202011-01-16 19:17:11 +0100289 if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
290 urb->transfer_buffer = usb_alloc_coherent(udev, bytes + offset,
291 GFP_KERNEL, &urb->transfer_dma);
292 else
293 urb->transfer_buffer = kmalloc(bytes + offset, GFP_KERNEL);
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (!urb->transfer_buffer) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200296 usb_free_urb(urb);
Martin Fuzzey084fb202011-01-16 19:17:11 +0100297 return NULL;
298 }
299
300 /* To test unaligned transfers add an offset and fill the
301 unused memory with a guard value */
302 if (offset) {
303 memset(urb->transfer_buffer, GUARD_BYTE, offset);
304 urb->transfer_buffer += offset;
305 if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
306 urb->transfer_dma += offset;
307 }
308
309 /* For inbound transfers use guard byte so that test fails if
310 data not correctly copied */
311 memset(urb->transfer_buffer,
312 usb_pipein(urb->pipe) ? GUARD_BYTE : 0,
313 bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return urb;
315}
316
Martin Fuzzey084fb202011-01-16 19:17:11 +0100317static struct urb *simple_alloc_urb(
318 struct usb_device *udev,
319 int pipe,
Amit Virdi457a0952014-08-22 14:36:37 +0530320 unsigned long bytes,
321 u8 bInterval)
Martin Fuzzey084fb202011-01-16 19:17:11 +0100322{
Amit Virdi457a0952014-08-22 14:36:37 +0530323 return usbtest_alloc_urb(udev, pipe, bytes, URB_NO_TRANSFER_DMA_MAP, 0,
Peter Chen145f48c2015-10-13 15:18:21 +0800324 bInterval, simple_callback);
325}
326
327static struct urb *complicated_alloc_urb(
328 struct usb_device *udev,
329 int pipe,
330 unsigned long bytes,
331 u8 bInterval)
332{
333 return usbtest_alloc_urb(udev, pipe, bytes, URB_NO_TRANSFER_DMA_MAP, 0,
334 bInterval, complicated_callback);
Martin Fuzzey084fb202011-01-16 19:17:11 +0100335}
336
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200337static unsigned pattern;
Vikram Pandita7f4e9852009-11-09 21:24:32 -0600338static unsigned mod_pattern;
339module_param_named(pattern, mod_pattern, uint, S_IRUGO | S_IWUSR);
340MODULE_PARM_DESC(mod_pattern, "i/o pattern (0 == zeroes)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Alan Sternb9a6e8e2015-09-01 09:48:01 +0800342static unsigned get_maxpacket(struct usb_device *udev, int pipe)
343{
344 struct usb_host_endpoint *ep;
345
346 ep = usb_pipe_endpoint(udev, pipe);
347 return le16_to_cpup(&ep->desc.wMaxPacketSize);
348}
349
350static void simple_fill_buf(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 unsigned i;
353 u8 *buf = urb->transfer_buffer;
354 unsigned len = urb->transfer_buffer_length;
Alan Sternb9a6e8e2015-09-01 09:48:01 +0800355 unsigned maxpacket;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 switch (pattern) {
358 default:
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200359 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 case 0:
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200361 memset(buf, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 break;
363 case 1: /* mod63 */
Alan Sternb9a6e8e2015-09-01 09:48:01 +0800364 maxpacket = get_maxpacket(urb->dev, urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 for (i = 0; i < len; i++)
Alan Sternb9a6e8e2015-09-01 09:48:01 +0800366 *buf++ = (u8) ((i % maxpacket) % 63);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 break;
368 }
369}
370
Greg Dietsche304b0b52011-05-08 22:51:43 -0500371static inline unsigned long buffer_offset(void *buf)
Martin Fuzzey084fb202011-01-16 19:17:11 +0100372{
Greg Dietsche304b0b52011-05-08 22:51:43 -0500373 return (unsigned long)buf & (ARCH_KMALLOC_MINALIGN - 1);
Martin Fuzzey084fb202011-01-16 19:17:11 +0100374}
375
376static int check_guard_bytes(struct usbtest_dev *tdev, struct urb *urb)
377{
378 u8 *buf = urb->transfer_buffer;
379 u8 *guard = buf - buffer_offset(buf);
380 unsigned i;
381
382 for (i = 0; guard < buf; i++, guard++) {
383 if (*guard != GUARD_BYTE) {
384 ERROR(tdev, "guard byte[%d] %d (not %d)\n",
385 i, *guard, GUARD_BYTE);
386 return -EINVAL;
387 }
388 }
389 return 0;
390}
391
392static int simple_check_buf(struct usbtest_dev *tdev, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 unsigned i;
395 u8 expected;
396 u8 *buf = urb->transfer_buffer;
397 unsigned len = urb->actual_length;
Alan Sternb9a6e8e2015-09-01 09:48:01 +0800398 unsigned maxpacket = get_maxpacket(urb->dev, urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Martin Fuzzey084fb202011-01-16 19:17:11 +0100400 int ret = check_guard_bytes(tdev, urb);
401 if (ret)
402 return ret;
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 for (i = 0; i < len; i++, buf++) {
405 switch (pattern) {
406 /* all-zeroes has no synchronization issues */
407 case 0:
408 expected = 0;
409 break;
410 /* mod63 stays in sync with short-terminated transfers,
411 * or otherwise when host and gadget agree on how large
412 * each usb transfer request should be. resync is done
413 * with set_interface or set_config.
414 */
415 case 1: /* mod63 */
Alan Sternb9a6e8e2015-09-01 09:48:01 +0800416 expected = (i % maxpacket) % 63;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 break;
418 /* always fail unsupported patterns */
419 default:
420 expected = !*buf;
421 break;
422 }
423 if (*buf == expected)
424 continue;
David Brownell28ffd792008-04-25 18:51:10 -0700425 ERROR(tdev, "buf[%d] = %d (not %d)\n", i, *buf, expected);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return -EINVAL;
427 }
428 return 0;
429}
430
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200431static void simple_free_urb(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Greg Dietsche304b0b52011-05-08 22:51:43 -0500433 unsigned long offset = buffer_offset(urb->transfer_buffer);
Martin Fuzzey084fb202011-01-16 19:17:11 +0100434
435 if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
436 usb_free_coherent(
437 urb->dev,
438 urb->transfer_buffer_length + offset,
439 urb->transfer_buffer - offset,
440 urb->transfer_dma - offset);
441 else
442 kfree(urb->transfer_buffer - offset);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200443 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200446static int simple_io(
David Brownell28ffd792008-04-25 18:51:10 -0700447 struct usbtest_dev *tdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 struct urb *urb,
449 int iterations,
450 int vary,
451 int expected,
452 const char *label
453)
454{
455 struct usb_device *udev = urb->dev;
456 int max = urb->transfer_buffer_length;
457 struct completion completion;
458 int retval = 0;
Roger Quadrose5e47462013-12-18 15:40:10 +0530459 unsigned long expire;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 urb->context = &completion;
462 while (retval == 0 && iterations-- > 0) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200463 init_completion(&completion);
Sebastian Andrzej Siewior7c79d092011-08-23 10:44:54 +0200464 if (usb_pipeout(urb->pipe)) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200465 simple_fill_buf(urb);
Sebastian Andrzej Siewior7c79d092011-08-23 10:44:54 +0200466 urb->transfer_flags |= URB_ZERO_PACKET;
467 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200468 retval = usb_submit_urb(urb, GFP_KERNEL);
469 if (retval != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 break;
471
Roger Quadrose5e47462013-12-18 15:40:10 +0530472 expire = msecs_to_jiffies(SIMPLE_IO_TIMEOUT);
473 if (!wait_for_completion_timeout(&completion, expire)) {
474 usb_kill_urb(urb);
475 retval = (urb->status == -ENOENT ?
476 -ETIMEDOUT : urb->status);
477 } else {
478 retval = urb->status;
479 }
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 urb->dev = udev;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200482 if (retval == 0 && usb_pipein(urb->pipe))
David Brownell28ffd792008-04-25 18:51:10 -0700483 retval = simple_check_buf(tdev, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 if (vary) {
486 int len = urb->transfer_buffer_length;
487
488 len += vary;
489 len %= max;
490 if (len == 0)
491 len = (vary < max) ? vary : max;
492 urb->transfer_buffer_length = len;
493 }
494
495 /* FIXME if endpoint halted, clear halt (and log) */
496 }
497 urb->transfer_buffer_length = max;
498
499 if (expected != retval)
David Brownell28ffd792008-04-25 18:51:10 -0700500 dev_err(&udev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 "%s failed, iterations left %d, status %d (not %d)\n",
502 label, iterations, retval, expected);
503 return retval;
504}
505
506
507/*-------------------------------------------------------------------------*/
508
509/* We use scatterlist primitives to test queued I/O.
510 * Yes, this also tests the scatterlist primitives.
511 */
512
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200513static void free_sglist(struct scatterlist *sg, int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 unsigned i;
David Brownell28ffd792008-04-25 18:51:10 -0700516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (!sg)
518 return;
519 for (i = 0; i < nents; i++) {
Jens Axboe45711f12007-10-22 21:19:53 +0200520 if (!sg_page(&sg[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 continue;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200522 kfree(sg_virt(&sg[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200524 kfree(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527static struct scatterlist *
Alan Sternb9a6e8e2015-09-01 09:48:01 +0800528alloc_sglist(int nents, int max, int vary, struct usbtest_dev *dev, int pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 struct scatterlist *sg;
Mathias Nymancdc77c82016-05-02 11:39:03 +0300531 unsigned int n_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 unsigned i;
533 unsigned size = max;
Alan Sternb9a6e8e2015-09-01 09:48:01 +0800534 unsigned maxpacket =
535 get_maxpacket(interface_to_usbdev(dev->intf), pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Dan Carpenter564e6982012-11-17 18:06:11 +0300537 if (max == 0)
538 return NULL;
539
Huang Ruif55055b2013-10-21 23:15:30 +0800540 sg = kmalloc_array(nents, sizeof(*sg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (!sg)
542 return NULL;
Alan Stern4756feb2008-03-27 10:15:22 -0400543 sg_init_table(sg, nents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 for (i = 0; i < nents; i++) {
546 char *buf;
David Brownell8b524902006-04-02 10:20:15 -0800547 unsigned j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200549 buf = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (!buf) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200551 free_sglist(sg, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return NULL;
553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 /* kmalloc pages are always physically contiguous! */
Alan Stern4756feb2008-03-27 10:15:22 -0400556 sg_set_buf(&sg[i], buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
David Brownell8b524902006-04-02 10:20:15 -0800558 switch (pattern) {
559 case 0:
560 /* already zeroed */
561 break;
562 case 1:
563 for (j = 0; j < size; j++)
Mathias Nymancdc77c82016-05-02 11:39:03 +0300564 *buf++ = (u8) (((j + n_size) % maxpacket) % 63);
565 n_size += size;
David Brownell8b524902006-04-02 10:20:15 -0800566 break;
567 }
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (vary) {
570 size += vary;
571 size %= max;
572 if (size == 0)
573 size = (vary < max) ? vary : max;
574 }
575 }
576
577 return sg;
578}
579
Kees Cook7d221852017-10-22 17:57:22 -0700580struct sg_timeout {
581 struct timer_list timer;
582 struct usb_sg_request *req;
583};
Alan Stern32b36ee2014-06-03 11:11:34 -0400584
Kees Cook7d221852017-10-22 17:57:22 -0700585static void sg_timeout(struct timer_list *t)
586{
587 struct sg_timeout *timeout = from_timer(timeout, t, timer);
588
589 usb_sg_cancel(timeout->req);
Alan Stern32b36ee2014-06-03 11:11:34 -0400590}
591
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200592static int perform_sglist(
David Brownell28ffd792008-04-25 18:51:10 -0700593 struct usbtest_dev *tdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 unsigned iterations,
595 int pipe,
596 struct usb_sg_request *req,
597 struct scatterlist *sg,
598 int nents
599)
600{
David Brownell28ffd792008-04-25 18:51:10 -0700601 struct usb_device *udev = testdev_to_usbdev(tdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 int retval = 0;
Kees Cook7d221852017-10-22 17:57:22 -0700603 struct sg_timeout timeout = {
604 .req = req,
605 };
Alan Stern32b36ee2014-06-03 11:11:34 -0400606
Kees Cook7d221852017-10-22 17:57:22 -0700607 timer_setup_on_stack(&timeout.timer, sg_timeout, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 while (retval == 0 && iterations-- > 0) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200610 retval = usb_sg_init(req, udev, pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 (udev->speed == USB_SPEED_HIGH)
612 ? (INTERRUPT_RATE << 3)
613 : INTERRUPT_RATE,
Christoph Lametere94b1762006-12-06 20:33:17 -0800614 sg, nents, 0, GFP_KERNEL);
David Brownell28ffd792008-04-25 18:51:10 -0700615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (retval)
617 break;
Kees Cook7d221852017-10-22 17:57:22 -0700618 mod_timer(&timeout.timer, jiffies +
Alan Stern32b36ee2014-06-03 11:11:34 -0400619 msecs_to_jiffies(SIMPLE_IO_TIMEOUT));
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200620 usb_sg_wait(req);
Kees Cook7d221852017-10-22 17:57:22 -0700621 if (!del_timer_sync(&timeout.timer))
Lu Baolu53958752016-08-11 10:31:14 +0800622 retval = -ETIMEDOUT;
623 else
624 retval = req->status;
Kees Cook7d221852017-10-22 17:57:22 -0700625 destroy_timer_on_stack(&timeout.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
David Brownell8b524902006-04-02 10:20:15 -0800627 /* FIXME check resulting data pattern */
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* FIXME if endpoint halted, clear halt (and log) */
630 }
631
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200632 /* FIXME for unlink or fault handling tests, don't report
633 * failure if retval is as we expected ...
634 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (retval)
David Brownell28ffd792008-04-25 18:51:10 -0700636 ERROR(tdev, "perform_sglist failed, "
637 "iterations left %d, status %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 iterations, retval);
639 return retval;
640}
641
642
643/*-------------------------------------------------------------------------*/
644
645/* unqueued control message testing
646 *
647 * there's a nice set of device functional requirements in chapter 9 of the
648 * usb 2.0 spec, which we can apply to ANY device, even ones that don't use
649 * special test firmware.
650 *
651 * we know the device is configured (or suspended) by the time it's visible
652 * through usbfs. we can't change that, so we won't test enumeration (which
653 * worked 'well enough' to get here, this time), power management (ditto),
654 * or remote wakeup (which needs human interaction).
655 */
656
657static unsigned realworld = 1;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200658module_param(realworld, uint, 0);
659MODULE_PARM_DESC(realworld, "clear to demand stricter spec compliance");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200661static int get_altsetting(struct usbtest_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 struct usb_interface *iface = dev->intf;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200664 struct usb_device *udev = interface_to_usbdev(iface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 int retval;
666
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200667 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 USB_REQ_GET_INTERFACE, USB_DIR_IN|USB_RECIP_INTERFACE,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200669 0, iface->altsetting[0].desc.bInterfaceNumber,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 dev->buf, 1, USB_CTRL_GET_TIMEOUT);
671 switch (retval) {
672 case 1:
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200673 return dev->buf[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 case 0:
675 retval = -ERANGE;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200676 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 default:
678 return retval;
679 }
680}
681
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200682static int set_altsetting(struct usbtest_dev *dev, int alternate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 struct usb_interface *iface = dev->intf;
685 struct usb_device *udev;
686
687 if (alternate < 0 || alternate >= 256)
688 return -EINVAL;
689
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200690 udev = interface_to_usbdev(iface);
691 return usb_set_interface(udev,
692 iface->altsetting[0].desc.bInterfaceNumber,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 alternate);
694}
695
David Brownell28ffd792008-04-25 18:51:10 -0700696static int is_good_config(struct usbtest_dev *tdev, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 struct usb_config_descriptor *config;
David Brownell28ffd792008-04-25 18:51:10 -0700699
Huang Ruif55055b2013-10-21 23:15:30 +0800700 if (len < sizeof(*config))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return 0;
David Brownell28ffd792008-04-25 18:51:10 -0700702 config = (struct usb_config_descriptor *) tdev->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 switch (config->bDescriptorType) {
705 case USB_DT_CONFIG:
706 case USB_DT_OTHER_SPEED_CONFIG:
707 if (config->bLength != 9) {
David Brownell28ffd792008-04-25 18:51:10 -0700708 ERROR(tdev, "bogus config descriptor length\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return 0;
710 }
711 /* this bit 'must be 1' but often isn't */
712 if (!realworld && !(config->bmAttributes & 0x80)) {
David Brownell28ffd792008-04-25 18:51:10 -0700713 ERROR(tdev, "high bit of config attributes not set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return 0;
715 }
716 if (config->bmAttributes & 0x1f) { /* reserved == 0 */
David Brownell28ffd792008-04-25 18:51:10 -0700717 ERROR(tdev, "reserved config bits set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return 0;
719 }
720 break;
721 default:
722 return 0;
723 }
724
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200725 if (le16_to_cpu(config->wTotalLength) == len) /* read it all */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return 1;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200727 if (le16_to_cpu(config->wTotalLength) >= TBUF_SIZE) /* max partial read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return 1;
David Brownell28ffd792008-04-25 18:51:10 -0700729 ERROR(tdev, "bogus config descriptor read size\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return 0;
731}
732
Huang Rui82f92672013-10-30 11:27:38 +0800733static int is_good_ext(struct usbtest_dev *tdev, u8 *buf)
734{
735 struct usb_ext_cap_descriptor *ext;
736 u32 attr;
737
738 ext = (struct usb_ext_cap_descriptor *) buf;
739
740 if (ext->bLength != USB_DT_USB_EXT_CAP_SIZE) {
741 ERROR(tdev, "bogus usb 2.0 extension descriptor length\n");
742 return 0;
743 }
744
745 attr = le32_to_cpu(ext->bmAttributes);
Huang Rui875bc232013-11-13 22:35:14 +0800746 /* bits[1:15] is used and others are reserved */
747 if (attr & ~0xfffe) { /* reserved == 0 */
Huang Rui82f92672013-10-30 11:27:38 +0800748 ERROR(tdev, "reserved bits set\n");
749 return 0;
750 }
751
752 return 1;
753}
754
Huang Ruib8fef792013-10-30 11:27:39 +0800755static int is_good_ss_cap(struct usbtest_dev *tdev, u8 *buf)
756{
757 struct usb_ss_cap_descriptor *ss;
758
759 ss = (struct usb_ss_cap_descriptor *) buf;
760
761 if (ss->bLength != USB_DT_USB_SS_CAP_SIZE) {
762 ERROR(tdev, "bogus superspeed device capability descriptor length\n");
763 return 0;
764 }
765
766 /*
767 * only bit[1] of bmAttributes is used for LTM and others are
768 * reserved
769 */
770 if (ss->bmAttributes & ~0x02) { /* reserved == 0 */
771 ERROR(tdev, "reserved bits set in bmAttributes\n");
772 return 0;
773 }
774
775 /* bits[0:3] of wSpeedSupported is used and others are reserved */
776 if (le16_to_cpu(ss->wSpeedSupported) & ~0x0f) { /* reserved == 0 */
777 ERROR(tdev, "reserved bits set in wSpeedSupported\n");
778 return 0;
779 }
780
781 return 1;
782}
783
Huang Rui8e292172013-10-30 11:27:40 +0800784static int is_good_con_id(struct usbtest_dev *tdev, u8 *buf)
785{
786 struct usb_ss_container_id_descriptor *con_id;
787
788 con_id = (struct usb_ss_container_id_descriptor *) buf;
789
790 if (con_id->bLength != USB_DT_USB_SS_CONTN_ID_SIZE) {
791 ERROR(tdev, "bogus container id descriptor length\n");
792 return 0;
793 }
794
795 if (con_id->bReserved) { /* reserved == 0 */
796 ERROR(tdev, "reserved bits set\n");
797 return 0;
798 }
799
800 return 1;
801}
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803/* sanity test for standard requests working with usb_control_mesg() and some
804 * of the utility functions which use it.
805 *
806 * this doesn't test how endpoint halts behave or data toggles get set, since
807 * we won't do I/O to bulk/interrupt endpoints here (which is how to change
808 * halt or toggle). toggle testing is impractical without support from hcds.
809 *
810 * this avoids failing devices linux would normally work with, by not testing
811 * config/altsetting operations for devices that only support their defaults.
812 * such devices rarely support those needless operations.
813 *
814 * NOTE that since this is a sanity test, it's not examining boundary cases
815 * to see if usbcore, hcd, and device all behave right. such testing would
816 * involve varied read sizes and other operation sequences.
817 */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200818static int ch9_postconfig(struct usbtest_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
820 struct usb_interface *iface = dev->intf;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200821 struct usb_device *udev = interface_to_usbdev(iface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 int i, alt, retval;
823
824 /* [9.2.3] if there's more than one altsetting, we need to be able to
825 * set and get each one. mostly trusts the descriptors from usbcore.
826 */
827 for (i = 0; i < iface->num_altsetting; i++) {
828
829 /* 9.2.3 constrains the range here */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200830 alt = iface->altsetting[i].desc.bAlternateSetting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (alt < 0 || alt >= iface->num_altsetting) {
David Brownell28ffd792008-04-25 18:51:10 -0700832 dev_err(&iface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 "invalid alt [%d].bAltSetting = %d\n",
834 i, alt);
835 }
836
837 /* [real world] get/set unimplemented if there's only one */
838 if (realworld && iface->num_altsetting == 1)
839 continue;
840
841 /* [9.4.10] set_interface */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200842 retval = set_altsetting(dev, alt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (retval) {
David Brownell28ffd792008-04-25 18:51:10 -0700844 dev_err(&iface->dev, "can't set_interface = %d, %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 alt, retval);
846 return retval;
847 }
848
849 /* [9.4.4] get_interface always works */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200850 retval = get_altsetting(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (retval != alt) {
David Brownell28ffd792008-04-25 18:51:10 -0700852 dev_err(&iface->dev, "get alt should be %d, was %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 alt, retval);
854 return (retval < 0) ? retval : -EDOM;
855 }
856
857 }
858
859 /* [real world] get_config unimplemented if there's only one */
860 if (!realworld || udev->descriptor.bNumConfigurations != 1) {
861 int expected = udev->actconfig->desc.bConfigurationValue;
862
863 /* [9.4.2] get_configuration always works
864 * ... although some cheap devices (like one TI Hub I've got)
865 * won't return config descriptors except before set_config.
866 */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200867 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 USB_REQ_GET_CONFIGURATION,
869 USB_DIR_IN | USB_RECIP_DEVICE,
870 0, 0, dev->buf, 1, USB_CTRL_GET_TIMEOUT);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200871 if (retval != 1 || dev->buf[0] != expected) {
David Brownell28ffd792008-04-25 18:51:10 -0700872 dev_err(&iface->dev, "get config --> %d %d (1 %d)\n",
David Brownellff7c79e2005-04-22 13:17:00 -0700873 retval, dev->buf[0], expected);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return (retval < 0) ? retval : -EDOM;
875 }
876 }
877
878 /* there's always [9.4.3] a device descriptor [9.6.1] */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200879 retval = usb_get_descriptor(udev, USB_DT_DEVICE, 0,
Huang Ruif55055b2013-10-21 23:15:30 +0800880 dev->buf, sizeof(udev->descriptor));
881 if (retval != sizeof(udev->descriptor)) {
David Brownell28ffd792008-04-25 18:51:10 -0700882 dev_err(&iface->dev, "dev descriptor --> %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return (retval < 0) ? retval : -EDOM;
884 }
885
Huang Rui9d3bd762013-10-28 23:31:32 +0800886 /*
887 * there's always [9.4.3] a bos device descriptor [9.6.2] in USB
888 * 3.0 spec
889 */
Huang Ruif6250992013-11-13 22:35:13 +0800890 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0210) {
Huang Rui82f92672013-10-30 11:27:38 +0800891 struct usb_bos_descriptor *bos = NULL;
892 struct usb_dev_cap_header *header = NULL;
893 unsigned total, num, length;
894 u8 *buf;
895
Huang Rui9d3bd762013-10-28 23:31:32 +0800896 retval = usb_get_descriptor(udev, USB_DT_BOS, 0, dev->buf,
897 sizeof(*udev->bos->desc));
898 if (retval != sizeof(*udev->bos->desc)) {
899 dev_err(&iface->dev, "bos descriptor --> %d\n", retval);
900 return (retval < 0) ? retval : -EDOM;
901 }
Huang Rui82f92672013-10-30 11:27:38 +0800902
903 bos = (struct usb_bos_descriptor *)dev->buf;
904 total = le16_to_cpu(bos->wTotalLength);
905 num = bos->bNumDeviceCaps;
906
907 if (total > TBUF_SIZE)
908 total = TBUF_SIZE;
909
910 /*
911 * get generic device-level capability descriptors [9.6.2]
912 * in USB 3.0 spec
913 */
914 retval = usb_get_descriptor(udev, USB_DT_BOS, 0, dev->buf,
915 total);
916 if (retval != total) {
917 dev_err(&iface->dev, "bos descriptor set --> %d\n",
918 retval);
919 return (retval < 0) ? retval : -EDOM;
920 }
921
922 length = sizeof(*udev->bos->desc);
923 buf = dev->buf;
924 for (i = 0; i < num; i++) {
925 buf += length;
926 if (buf + sizeof(struct usb_dev_cap_header) >
927 dev->buf + total)
928 break;
929
930 header = (struct usb_dev_cap_header *)buf;
931 length = header->bLength;
932
933 if (header->bDescriptorType !=
934 USB_DT_DEVICE_CAPABILITY) {
935 dev_warn(&udev->dev, "not device capability descriptor, skip\n");
936 continue;
937 }
938
939 switch (header->bDevCapabilityType) {
940 case USB_CAP_TYPE_EXT:
941 if (buf + USB_DT_USB_EXT_CAP_SIZE >
942 dev->buf + total ||
943 !is_good_ext(dev, buf)) {
944 dev_err(&iface->dev, "bogus usb 2.0 extension descriptor\n");
945 return -EDOM;
946 }
947 break;
Huang Ruib8fef792013-10-30 11:27:39 +0800948 case USB_SS_CAP_TYPE:
949 if (buf + USB_DT_USB_SS_CAP_SIZE >
950 dev->buf + total ||
951 !is_good_ss_cap(dev, buf)) {
952 dev_err(&iface->dev, "bogus superspeed device capability descriptor\n");
953 return -EDOM;
954 }
955 break;
Huang Rui8e292172013-10-30 11:27:40 +0800956 case CONTAINER_ID_TYPE:
957 if (buf + USB_DT_USB_SS_CONTN_ID_SIZE >
958 dev->buf + total ||
959 !is_good_con_id(dev, buf)) {
960 dev_err(&iface->dev, "bogus container id descriptor\n");
961 return -EDOM;
962 }
963 break;
Huang Rui82f92672013-10-30 11:27:38 +0800964 default:
965 break;
966 }
967 }
Huang Rui9d3bd762013-10-28 23:31:32 +0800968 }
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* there's always [9.4.3] at least one config descriptor [9.6.3] */
971 for (i = 0; i < udev->descriptor.bNumConfigurations; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200972 retval = usb_get_descriptor(udev, USB_DT_CONFIG, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 dev->buf, TBUF_SIZE);
David Brownell28ffd792008-04-25 18:51:10 -0700974 if (!is_good_config(dev, retval)) {
975 dev_err(&iface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 "config [%d] descriptor --> %d\n",
977 i, retval);
978 return (retval < 0) ? retval : -EDOM;
979 }
980
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200981 /* FIXME cross-checking udev->config[i] to make sure usbcore
982 * parsed it right (etc) would be good testing paranoia
983 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985
986 /* and sometimes [9.2.6.6] speed dependent descriptors */
987 if (le16_to_cpu(udev->descriptor.bcdUSB) == 0x0200) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200988 struct usb_qualifier_descriptor *d = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 /* device qualifier [9.6.2] */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200991 retval = usb_get_descriptor(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 USB_DT_DEVICE_QUALIFIER, 0, dev->buf,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200993 sizeof(struct usb_qualifier_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (retval == -EPIPE) {
995 if (udev->speed == USB_SPEED_HIGH) {
David Brownell28ffd792008-04-25 18:51:10 -0700996 dev_err(&iface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 "hs dev qualifier --> %d\n",
998 retval);
Colin Ian Kingef5ec7f2017-02-12 18:35:18 +0000999 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001 /* usb2.0 but not high-speed capable; fine */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001002 } else if (retval != sizeof(struct usb_qualifier_descriptor)) {
David Brownell28ffd792008-04-25 18:51:10 -07001003 dev_err(&iface->dev, "dev qualifier --> %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return (retval < 0) ? retval : -EDOM;
1005 } else
1006 d = (struct usb_qualifier_descriptor *) dev->buf;
1007
1008 /* might not have [9.6.2] any other-speed configs [9.6.4] */
1009 if (d) {
1010 unsigned max = d->bNumConfigurations;
1011 for (i = 0; i < max; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001012 retval = usb_get_descriptor(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 USB_DT_OTHER_SPEED_CONFIG, i,
1014 dev->buf, TBUF_SIZE);
David Brownell28ffd792008-04-25 18:51:10 -07001015 if (!is_good_config(dev, retval)) {
1016 dev_err(&iface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 "other speed config --> %d\n",
1018 retval);
1019 return (retval < 0) ? retval : -EDOM;
1020 }
1021 }
1022 }
1023 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001024 /* FIXME fetch strings from at least the device descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 /* [9.4.5] get_status always works */
Felipe Balbid9e1e142017-11-02 10:57:40 +02001027 retval = usb_get_std_status(udev, USB_RECIP_DEVICE, 0, dev->buf);
Alan Stern15b73362013-07-30 15:35:40 -04001028 if (retval) {
David Brownell28ffd792008-04-25 18:51:10 -07001029 dev_err(&iface->dev, "get dev status --> %d\n", retval);
Alan Stern15b73362013-07-30 15:35:40 -04001030 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001033 /* FIXME configuration.bmAttributes says if we could try to set/clear
1034 * the device's remote wakeup feature ... if we can, test that here
1035 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Felipe Balbid9e1e142017-11-02 10:57:40 +02001037 retval = usb_get_std_status(udev, USB_RECIP_INTERFACE,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001038 iface->altsetting[0].desc.bInterfaceNumber, dev->buf);
Alan Stern15b73362013-07-30 15:35:40 -04001039 if (retval) {
David Brownell28ffd792008-04-25 18:51:10 -07001040 dev_err(&iface->dev, "get interface status --> %d\n", retval);
Alan Stern15b73362013-07-30 15:35:40 -04001041 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001043 /* FIXME get status for each endpoint in the interface */
David Brownell28ffd792008-04-25 18:51:10 -07001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return 0;
1046}
1047
1048/*-------------------------------------------------------------------------*/
1049
1050/* use ch9 requests to test whether:
1051 * (a) queues work for control, keeping N subtests queued and
1052 * active (auto-resubmit) for M loops through the queue.
1053 * (b) protocol stalls (control-only) will autorecover.
1054 * it's not like bulk/intr; no halt clearing.
1055 * (c) short control reads are reported and handled.
1056 * (d) queues are always processed in-order
1057 */
1058
1059struct ctrl_ctx {
1060 spinlock_t lock;
1061 struct usbtest_dev *dev;
1062 struct completion complete;
1063 unsigned count;
1064 unsigned pending;
1065 int status;
1066 struct urb **urb;
Deepa Dinamani18fc4eb2015-11-04 15:29:11 -08001067 struct usbtest_param_32 *param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 int last;
1069};
1070
Huang Ruic952a8b2013-11-04 21:11:53 +08001071#define NUM_SUBCASES 16 /* how many test subcases here? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073struct subcase {
1074 struct usb_ctrlrequest setup;
1075 int number;
1076 int expected;
1077};
1078
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001079static void ctrl_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 struct ctrl_ctx *ctx = urb->context;
1082 struct usb_ctrlrequest *reqp;
1083 struct subcase *subcase;
1084 int status = urb->status;
Sebastian Andrzej Siewior6f3fde62018-07-01 17:35:44 +02001085 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 reqp = (struct usb_ctrlrequest *)urb->setup_packet;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001088 subcase = container_of(reqp, struct subcase, setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Sebastian Andrzej Siewior6f3fde62018-07-01 17:35:44 +02001090 spin_lock_irqsave(&ctx->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 ctx->count--;
1092 ctx->pending--;
1093
1094 /* queue must transfer and complete in fifo order, unless
1095 * usb_unlink_urb() is used to unlink something not at the
1096 * physical queue head (not tested).
1097 */
1098 if (subcase->number > 0) {
1099 if ((subcase->number - ctx->last) != 1) {
David Brownell28ffd792008-04-25 18:51:10 -07001100 ERROR(ctx->dev,
1101 "subcase %d completed out of order, last %d\n",
1102 subcase->number, ctx->last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 status = -EDOM;
1104 ctx->last = subcase->number;
1105 goto error;
1106 }
1107 }
1108 ctx->last = subcase->number;
1109
1110 /* succeed or fault in only one way? */
1111 if (status == subcase->expected)
1112 status = 0;
1113
1114 /* async unlink for cleanup? */
1115 else if (status != -ECONNRESET) {
1116
1117 /* some faults are allowed, not required */
1118 if (subcase->expected > 0 && (
Greg Kroah-Hartman59d99782007-07-18 10:58:02 -07001119 ((status == -subcase->expected /* happened */
1120 || status == 0)))) /* didn't */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 status = 0;
1122 /* sometimes more than one fault is allowed */
1123 else if (subcase->number == 12 && status == -EPIPE)
1124 status = 0;
1125 else
David Brownell28ffd792008-04-25 18:51:10 -07001126 ERROR(ctx->dev, "subtest %d error, status %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 subcase->number, status);
1128 }
1129
1130 /* unexpected status codes mean errors; ideally, in hardware */
1131 if (status) {
1132error:
1133 if (ctx->status == 0) {
1134 int i;
1135
1136 ctx->status = status;
David Brownell28ffd792008-04-25 18:51:10 -07001137 ERROR(ctx->dev, "control queue %02x.%02x, err %d, "
1138 "%d left, subcase %d, len %d/%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 reqp->bRequestType, reqp->bRequest,
David Brownell28ffd792008-04-25 18:51:10 -07001140 status, ctx->count, subcase->number,
1141 urb->actual_length,
1142 urb->transfer_buffer_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 /* FIXME this "unlink everything" exit route should
1145 * be a separate test case.
1146 */
1147
1148 /* unlink whatever's still pending */
1149 for (i = 1; i < ctx->param->sglen; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001150 struct urb *u = ctx->urb[
1151 (i + subcase->number)
1152 % ctx->param->sglen];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 if (u == urb || !u->dev)
1155 continue;
Franck Bui-Huucaa2a122006-05-15 19:23:53 +02001156 spin_unlock(&ctx->lock);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001157 status = usb_unlink_urb(u);
Franck Bui-Huucaa2a122006-05-15 19:23:53 +02001158 spin_lock(&ctx->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 switch (status) {
1160 case -EINPROGRESS:
1161 case -EBUSY:
1162 case -EIDRM:
1163 continue;
1164 default:
David Brownell28ffd792008-04-25 18:51:10 -07001165 ERROR(ctx->dev, "urb unlink --> %d\n",
1166 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168 }
1169 status = ctx->status;
1170 }
1171 }
1172
1173 /* resubmit if we need to, else mark this as done */
1174 if ((status == 0) && (ctx->pending < ctx->count)) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001175 status = usb_submit_urb(urb, GFP_ATOMIC);
1176 if (status != 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001177 ERROR(ctx->dev,
1178 "can't resubmit ctrl %02x.%02x, err %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 reqp->bRequestType, reqp->bRequest, status);
1180 urb->dev = NULL;
1181 } else
1182 ctx->pending++;
1183 } else
1184 urb->dev = NULL;
David Brownell28ffd792008-04-25 18:51:10 -07001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 /* signal completion when nothing's queued */
1187 if (ctx->pending == 0)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001188 complete(&ctx->complete);
Sebastian Andrzej Siewior6f3fde62018-07-01 17:35:44 +02001189 spin_unlock_irqrestore(&ctx->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
1192static int
Deepa Dinamani18fc4eb2015-11-04 15:29:11 -08001193test_ctrl_queue(struct usbtest_dev *dev, struct usbtest_param_32 *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001195 struct usb_device *udev = testdev_to_usbdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 struct urb **urb;
1197 struct ctrl_ctx context;
1198 int i;
1199
Xi Wange65cdfa2012-04-09 15:48:55 -04001200 if (param->sglen == 0 || param->iterations > UINT_MAX / param->sglen)
1201 return -EOPNOTSUPP;
1202
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001203 spin_lock_init(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 context.dev = dev;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001205 init_completion(&context.complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 context.count = param->sglen * param->iterations;
1207 context.pending = 0;
1208 context.status = -ENOMEM;
1209 context.param = param;
1210 context.last = -1;
1211
1212 /* allocate and init the urbs we'll queue.
1213 * as with bulk/intr sglists, sglen is the queue depth; it also
1214 * controls which subtests run (more tests than sglen) or rerun.
1215 */
Christoph Lametere94b1762006-12-06 20:33:17 -08001216 urb = kcalloc(param->sglen, sizeof(struct urb *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (!urb)
1218 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 for (i = 0; i < param->sglen; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001220 int pipe = usb_rcvctrlpipe(udev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 unsigned len;
1222 struct urb *u;
1223 struct usb_ctrlrequest req;
1224 struct subcase *reqp;
Marcin Slusarz6def7552008-05-12 20:17:25 +02001225
1226 /* sign of this variable means:
1227 * -: tested code must return this (negative) error code
1228 * +: tested code may return this (negative too) error code
1229 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int expected = 0;
1231
1232 /* requests here are mostly expected to succeed on any
1233 * device, but some are chosen to trigger protocol stalls
1234 * or short reads.
1235 */
Huang Ruif55055b2013-10-21 23:15:30 +08001236 memset(&req, 0, sizeof(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 req.bRequest = USB_REQ_GET_DESCRIPTOR;
1238 req.bRequestType = USB_DIR_IN|USB_RECIP_DEVICE;
1239
1240 switch (i % NUM_SUBCASES) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001241 case 0: /* get device descriptor */
1242 req.wValue = cpu_to_le16(USB_DT_DEVICE << 8);
1243 len = sizeof(struct usb_device_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001245 case 1: /* get first config descriptor (only) */
1246 req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
1247 len = sizeof(struct usb_config_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001249 case 2: /* get altsetting (OFTEN STALLS) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 req.bRequest = USB_REQ_GET_INTERFACE;
1251 req.bRequestType = USB_DIR_IN|USB_RECIP_INTERFACE;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001252 /* index = 0 means first interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 len = 1;
1254 expected = EPIPE;
1255 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001256 case 3: /* get interface status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 req.bRequest = USB_REQ_GET_STATUS;
1258 req.bRequestType = USB_DIR_IN|USB_RECIP_INTERFACE;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001259 /* interface 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 len = 2;
1261 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001262 case 4: /* get device status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 req.bRequest = USB_REQ_GET_STATUS;
1264 req.bRequestType = USB_DIR_IN|USB_RECIP_DEVICE;
1265 len = 2;
1266 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001267 case 5: /* get device qualifier (MAY STALL) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 req.wValue = cpu_to_le16 (USB_DT_DEVICE_QUALIFIER << 8);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001269 len = sizeof(struct usb_qualifier_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (udev->speed != USB_SPEED_HIGH)
1271 expected = EPIPE;
1272 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001273 case 6: /* get first config descriptor, plus interface */
1274 req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
1275 len = sizeof(struct usb_config_descriptor);
1276 len += sizeof(struct usb_interface_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001278 case 7: /* get interface descriptor (ALWAYS STALLS) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 req.wValue = cpu_to_le16 (USB_DT_INTERFACE << 8);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001280 /* interface == 0 */
1281 len = sizeof(struct usb_interface_descriptor);
David Brownell28ffd792008-04-25 18:51:10 -07001282 expected = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001284 /* NOTE: two consecutive stalls in the queue here.
1285 * that tests fault recovery a bit more aggressively. */
1286 case 8: /* clear endpoint halt (MAY STALL) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 req.bRequest = USB_REQ_CLEAR_FEATURE;
1288 req.bRequestType = USB_RECIP_ENDPOINT;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001289 /* wValue 0 == ep halt */
1290 /* wIndex 0 == ep0 (shouldn't halt!) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 len = 0;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001292 pipe = usb_sndctrlpipe(udev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 expected = EPIPE;
1294 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001295 case 9: /* get endpoint status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 req.bRequest = USB_REQ_GET_STATUS;
1297 req.bRequestType = USB_DIR_IN|USB_RECIP_ENDPOINT;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001298 /* endpoint 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 len = 2;
1300 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001301 case 10: /* trigger short read (EREMOTEIO) */
1302 req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 len = 1024;
1304 expected = -EREMOTEIO;
1305 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001306 /* NOTE: two consecutive _different_ faults in the queue. */
1307 case 11: /* get endpoint descriptor (ALWAYS STALLS) */
1308 req.wValue = cpu_to_le16(USB_DT_ENDPOINT << 8);
1309 /* endpoint == 0 */
1310 len = sizeof(struct usb_interface_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 expected = EPIPE;
1312 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001313 /* NOTE: sometimes even a third fault in the queue! */
1314 case 12: /* get string 0 descriptor (MAY STALL) */
1315 req.wValue = cpu_to_le16(USB_DT_STRING << 8);
1316 /* string == 0, for language IDs */
1317 len = sizeof(struct usb_interface_descriptor);
1318 /* may succeed when > 4 languages */
1319 expected = EREMOTEIO; /* or EPIPE, if no strings */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001321 case 13: /* short read, resembling case 10 */
1322 req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
1323 /* last data packet "should" be DATA1, not DATA0 */
Paul Zimmerman6a23ccd2012-04-16 14:19:07 -07001324 if (udev->speed == USB_SPEED_SUPER)
1325 len = 1024 - 512;
1326 else
1327 len = 1024 - udev->descriptor.bMaxPacketSize0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 expected = -EREMOTEIO;
1329 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001330 case 14: /* short read; try to fill the last packet */
1331 req.wValue = cpu_to_le16((USB_DT_DEVICE << 8) | 0);
David Brownell28ffd792008-04-25 18:51:10 -07001332 /* device descriptor size == 18 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 len = udev->descriptor.bMaxPacketSize0;
Sebastian Andrzej Siewior67e7d642011-04-14 16:17:21 +02001334 if (udev->speed == USB_SPEED_SUPER)
1335 len = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 switch (len) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001337 case 8:
1338 len = 24;
1339 break;
1340 case 16:
1341 len = 32;
1342 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344 expected = -EREMOTEIO;
1345 break;
Huang Ruic952a8b2013-11-04 21:11:53 +08001346 case 15:
1347 req.wValue = cpu_to_le16(USB_DT_BOS << 8);
1348 if (udev->bos)
1349 len = le16_to_cpu(udev->bos->desc->wTotalLength);
1350 else
1351 len = sizeof(struct usb_bos_descriptor);
Sarah Sharp8cf43282013-12-13 13:44:17 -08001352 if (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0201)
Huang Ruic952a8b2013-11-04 21:11:53 +08001353 expected = -EPIPE;
1354 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 default:
David Brownell28ffd792008-04-25 18:51:10 -07001356 ERROR(dev, "bogus number of ctrl queue testcases!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 context.status = -EINVAL;
1358 goto cleanup;
1359 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001360 req.wLength = cpu_to_le16(len);
Amit Virdi457a0952014-08-22 14:36:37 +05301361 urb[i] = u = simple_alloc_urb(udev, pipe, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (!u)
1363 goto cleanup;
1364
Huang Ruif55055b2013-10-21 23:15:30 +08001365 reqp = kmalloc(sizeof(*reqp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (!reqp)
1367 goto cleanup;
1368 reqp->setup = req;
1369 reqp->number = i % NUM_SUBCASES;
1370 reqp->expected = expected;
1371 u->setup_packet = (char *) &reqp->setup;
1372
1373 u->context = &context;
1374 u->complete = ctrl_complete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376
1377 /* queue the urbs */
1378 context.urb = urb;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001379 spin_lock_irq(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 for (i = 0; i < param->sglen; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001381 context.status = usb_submit_urb(urb[i], GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (context.status != 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001383 ERROR(dev, "can't submit urb[%d], status %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 i, context.status);
1385 context.count = context.pending;
1386 break;
1387 }
1388 context.pending++;
1389 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001390 spin_unlock_irq(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /* FIXME set timer and time out; provide a disconnect hook */
1393
1394 /* wait for the last one to complete */
1395 if (context.pending > 0)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001396 wait_for_completion(&context.complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398cleanup:
1399 for (i = 0; i < param->sglen; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001400 if (!urb[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 continue;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001402 urb[i]->dev = udev;
Alan Stern0ede76f2010-03-05 15:10:17 -05001403 kfree(urb[i]->setup_packet);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001404 simple_free_urb(urb[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001406 kfree(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return context.status;
1408}
1409#undef NUM_SUBCASES
1410
1411
1412/*-------------------------------------------------------------------------*/
1413
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001414static void unlink1_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 int status = urb->status;
1417
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001418 /* we "know" -EPIPE (stall) never happens */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (!status)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001420 status = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (status) {
1422 urb->status = status;
Ming Leicdc97792008-02-24 18:41:47 +08001423 complete(urb->context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425}
1426
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001427static int unlink1(struct usbtest_dev *dev, int pipe, int size, int async)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
1429 struct urb *urb;
1430 struct completion completion;
1431 int retval = 0;
1432
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001433 init_completion(&completion);
Amit Virdi457a0952014-08-22 14:36:37 +05301434 urb = simple_alloc_urb(testdev_to_usbdev(dev), pipe, size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (!urb)
1436 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 urb->context = &completion;
1438 urb->complete = unlink1_callback;
1439
Huang Ruie4d58f52014-05-26 10:55:36 +08001440 if (usb_pipeout(urb->pipe)) {
1441 simple_fill_buf(urb);
1442 urb->transfer_flags |= URB_ZERO_PACKET;
1443 }
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 /* keep the endpoint busy. there are lots of hc/hcd-internal
1446 * states, and testing should get to all of them over time.
1447 *
1448 * FIXME want additional tests for when endpoint is STALLing
1449 * due to errors, or is just NAKing requests.
1450 */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001451 retval = usb_submit_urb(urb, GFP_KERNEL);
1452 if (retval != 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001453 dev_err(&dev->intf->dev, "submit fail %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return retval;
1455 }
1456
1457 /* unlinking that should always work. variable delay tests more
1458 * hcd states and code paths, even with little other system load.
1459 */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001460 msleep(jiffies % (2 * INTERRUPT_RATE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (async) {
Martin Fuzzey3b6c0232009-06-04 23:20:38 +02001462 while (!completion_done(&completion)) {
1463 retval = usb_unlink_urb(urb);
1464
Huang Ruia7683eb2014-05-22 18:06:14 +08001465 if (retval == 0 && usb_pipein(urb->pipe))
1466 retval = simple_check_buf(dev, urb);
1467
Martin Fuzzey3b6c0232009-06-04 23:20:38 +02001468 switch (retval) {
1469 case -EBUSY:
1470 case -EIDRM:
1471 /* we can't unlink urbs while they're completing
1472 * or if they've completed, and we haven't
1473 * resubmitted. "normal" drivers would prevent
1474 * resubmission, but since we're testing unlink
1475 * paths, we can't.
1476 */
1477 ERROR(dev, "unlink retry\n");
1478 continue;
1479 case 0:
1480 case -EINPROGRESS:
1481 break;
1482
1483 default:
1484 dev_err(&dev->intf->dev,
1485 "unlink fail %d\n", retval);
1486 return retval;
1487 }
1488
1489 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
1491 } else
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001492 usb_kill_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001494 wait_for_completion(&completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 retval = urb->status;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001496 simple_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 if (async)
1499 return (retval == -ECONNRESET) ? 0 : retval - 1000;
1500 else
1501 return (retval == -ENOENT || retval == -EPERM) ?
1502 0 : retval - 2000;
1503}
1504
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001505static int unlink_simple(struct usbtest_dev *dev, int pipe, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 int retval = 0;
1508
1509 /* test sync and async paths */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001510 retval = unlink1(dev, pipe, len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (!retval)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001512 retval = unlink1(dev, pipe, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return retval;
1514}
1515
1516/*-------------------------------------------------------------------------*/
1517
Alan Stern869410f2011-04-14 11:21:04 -04001518struct queued_ctx {
1519 struct completion complete;
1520 atomic_t pending;
1521 unsigned num;
1522 int status;
1523 struct urb **urbs;
1524};
1525
1526static void unlink_queued_callback(struct urb *urb)
1527{
1528 int status = urb->status;
1529 struct queued_ctx *ctx = urb->context;
1530
1531 if (ctx->status)
1532 goto done;
1533 if (urb == ctx->urbs[ctx->num - 4] || urb == ctx->urbs[ctx->num - 2]) {
1534 if (status == -ECONNRESET)
1535 goto done;
1536 /* What error should we report if the URB completed normally? */
1537 }
1538 if (status != 0)
1539 ctx->status = status;
1540
1541 done:
1542 if (atomic_dec_and_test(&ctx->pending))
1543 complete(&ctx->complete);
1544}
1545
1546static int unlink_queued(struct usbtest_dev *dev, int pipe, unsigned num,
1547 unsigned size)
1548{
1549 struct queued_ctx ctx;
1550 struct usb_device *udev = testdev_to_usbdev(dev);
1551 void *buf;
1552 dma_addr_t buf_dma;
1553 int i;
1554 int retval = -ENOMEM;
1555
1556 init_completion(&ctx.complete);
1557 atomic_set(&ctx.pending, 1); /* One more than the actual value */
1558 ctx.num = num;
1559 ctx.status = 0;
1560
1561 buf = usb_alloc_coherent(udev, size, GFP_KERNEL, &buf_dma);
1562 if (!buf)
1563 return retval;
1564 memset(buf, 0, size);
1565
1566 /* Allocate and init the urbs we'll queue */
1567 ctx.urbs = kcalloc(num, sizeof(struct urb *), GFP_KERNEL);
1568 if (!ctx.urbs)
1569 goto free_buf;
1570 for (i = 0; i < num; i++) {
1571 ctx.urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1572 if (!ctx.urbs[i])
1573 goto free_urbs;
1574 usb_fill_bulk_urb(ctx.urbs[i], udev, pipe, buf, size,
1575 unlink_queued_callback, &ctx);
1576 ctx.urbs[i]->transfer_dma = buf_dma;
1577 ctx.urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Huang Ruie4d58f52014-05-26 10:55:36 +08001578
1579 if (usb_pipeout(ctx.urbs[i]->pipe)) {
1580 simple_fill_buf(ctx.urbs[i]);
1581 ctx.urbs[i]->transfer_flags |= URB_ZERO_PACKET;
1582 }
Alan Stern869410f2011-04-14 11:21:04 -04001583 }
1584
1585 /* Submit all the URBs and then unlink URBs num - 4 and num - 2. */
1586 for (i = 0; i < num; i++) {
1587 atomic_inc(&ctx.pending);
1588 retval = usb_submit_urb(ctx.urbs[i], GFP_KERNEL);
1589 if (retval != 0) {
1590 dev_err(&dev->intf->dev, "submit urbs[%d] fail %d\n",
1591 i, retval);
1592 atomic_dec(&ctx.pending);
1593 ctx.status = retval;
1594 break;
1595 }
1596 }
1597 if (i == num) {
1598 usb_unlink_urb(ctx.urbs[num - 4]);
1599 usb_unlink_urb(ctx.urbs[num - 2]);
1600 } else {
1601 while (--i >= 0)
1602 usb_unlink_urb(ctx.urbs[i]);
1603 }
1604
1605 if (atomic_dec_and_test(&ctx.pending)) /* The extra count */
1606 complete(&ctx.complete);
1607 wait_for_completion(&ctx.complete);
1608 retval = ctx.status;
1609
1610 free_urbs:
1611 for (i = 0; i < num; i++)
1612 usb_free_urb(ctx.urbs[i]);
1613 kfree(ctx.urbs);
1614 free_buf:
1615 usb_free_coherent(udev, size, buf, buf_dma);
1616 return retval;
1617}
1618
1619/*-------------------------------------------------------------------------*/
1620
David Brownell28ffd792008-04-25 18:51:10 -07001621static int verify_not_halted(struct usbtest_dev *tdev, int ep, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 int retval;
1624 u16 status;
1625
1626 /* shouldn't look or act halted */
Felipe Balbid9e1e142017-11-02 10:57:40 +02001627 retval = usb_get_std_status(urb->dev, USB_RECIP_ENDPOINT, ep, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (retval < 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001629 ERROR(tdev, "ep %02x couldn't get no-halt status, %d\n",
1630 ep, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return retval;
1632 }
1633 if (status != 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001634 ERROR(tdev, "ep %02x bogus status: %04x != 0\n", ep, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return -EINVAL;
1636 }
David Brownell28ffd792008-04-25 18:51:10 -07001637 retval = simple_io(tdev, urb, 1, 0, 0, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (retval != 0)
1639 return -EINVAL;
1640 return 0;
1641}
1642
David Brownell28ffd792008-04-25 18:51:10 -07001643static int verify_halted(struct usbtest_dev *tdev, int ep, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645 int retval;
1646 u16 status;
1647
1648 /* should look and act halted */
Felipe Balbid9e1e142017-11-02 10:57:40 +02001649 retval = usb_get_std_status(urb->dev, USB_RECIP_ENDPOINT, ep, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (retval < 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001651 ERROR(tdev, "ep %02x couldn't get halt status, %d\n",
1652 ep, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 return retval;
1654 }
1655 if (status != 1) {
David Brownell28ffd792008-04-25 18:51:10 -07001656 ERROR(tdev, "ep %02x bogus status: %04x != 1\n", ep, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return -EINVAL;
1658 }
David Brownell28ffd792008-04-25 18:51:10 -07001659 retval = simple_io(tdev, urb, 1, 0, -EPIPE, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (retval != -EPIPE)
1661 return -EINVAL;
David Brownell28ffd792008-04-25 18:51:10 -07001662 retval = simple_io(tdev, urb, 1, 0, -EPIPE, "verify_still_halted");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 if (retval != -EPIPE)
1664 return -EINVAL;
1665 return 0;
1666}
1667
David Brownell28ffd792008-04-25 18:51:10 -07001668static int test_halt(struct usbtest_dev *tdev, int ep, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
1670 int retval;
1671
1672 /* shouldn't look or act halted now */
David Brownell28ffd792008-04-25 18:51:10 -07001673 retval = verify_not_halted(tdev, ep, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (retval < 0)
1675 return retval;
1676
1677 /* set halt (protocol test only), verify it worked */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001678 retval = usb_control_msg(urb->dev, usb_sndctrlpipe(urb->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 USB_REQ_SET_FEATURE, USB_RECIP_ENDPOINT,
1680 USB_ENDPOINT_HALT, ep,
1681 NULL, 0, USB_CTRL_SET_TIMEOUT);
1682 if (retval < 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001683 ERROR(tdev, "ep %02x couldn't set halt, %d\n", ep, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 return retval;
1685 }
David Brownell28ffd792008-04-25 18:51:10 -07001686 retval = verify_halted(tdev, ep, urb);
Roger Quadros824d7522013-12-18 15:40:11 +05301687 if (retval < 0) {
1688 int ret;
1689
1690 /* clear halt anyways, else further tests will fail */
1691 ret = usb_clear_halt(urb->dev, urb->pipe);
1692 if (ret)
1693 ERROR(tdev, "ep %02x couldn't clear halt, %d\n",
1694 ep, ret);
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 return retval;
Roger Quadros824d7522013-12-18 15:40:11 +05301697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 /* clear halt (tests API + protocol), verify it worked */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001700 retval = usb_clear_halt(urb->dev, urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (retval < 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001702 ERROR(tdev, "ep %02x couldn't clear halt, %d\n", ep, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return retval;
1704 }
David Brownell28ffd792008-04-25 18:51:10 -07001705 retval = verify_not_halted(tdev, ep, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (retval < 0)
1707 return retval;
1708
1709 /* NOTE: could also verify SET_INTERFACE clear halts ... */
1710
1711 return 0;
1712}
1713
Mathias Nymancc2e60d2017-12-15 15:11:38 +02001714static int test_toggle_sync(struct usbtest_dev *tdev, int ep, struct urb *urb)
1715{
1716 int retval;
1717
1718 /* clear initial data toggle to DATA0 */
1719 retval = usb_clear_halt(urb->dev, urb->pipe);
1720 if (retval < 0) {
1721 ERROR(tdev, "ep %02x couldn't clear halt, %d\n", ep, retval);
1722 return retval;
1723 }
1724
1725 /* transfer 3 data packets, should be DATA0, DATA1, DATA0 */
1726 retval = simple_io(tdev, urb, 1, 0, 0, __func__);
1727 if (retval != 0)
1728 return -EINVAL;
1729
1730 /* clear halt resets device side data toggle, host should react to it */
1731 retval = usb_clear_halt(urb->dev, urb->pipe);
1732 if (retval < 0) {
1733 ERROR(tdev, "ep %02x couldn't clear halt, %d\n", ep, retval);
1734 return retval;
1735 }
1736
1737 /* host should use DATA0 again after clear halt */
1738 retval = simple_io(tdev, urb, 1, 0, 0, __func__);
1739
1740 return retval;
1741}
1742
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001743static int halt_simple(struct usbtest_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Paul Zimmerman6a23ccd2012-04-16 14:19:07 -07001745 int ep;
1746 int retval = 0;
1747 struct urb *urb;
1748 struct usb_device *udev = testdev_to_usbdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Paul Zimmerman6a23ccd2012-04-16 14:19:07 -07001750 if (udev->speed == USB_SPEED_SUPER)
Amit Virdi457a0952014-08-22 14:36:37 +05301751 urb = simple_alloc_urb(udev, 0, 1024, 0);
Paul Zimmerman6a23ccd2012-04-16 14:19:07 -07001752 else
Amit Virdi457a0952014-08-22 14:36:37 +05301753 urb = simple_alloc_urb(udev, 0, 512, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (urb == NULL)
1755 return -ENOMEM;
1756
1757 if (dev->in_pipe) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001758 ep = usb_pipeendpoint(dev->in_pipe) | USB_DIR_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 urb->pipe = dev->in_pipe;
David Brownell28ffd792008-04-25 18:51:10 -07001760 retval = test_halt(dev, ep, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (retval < 0)
1762 goto done;
1763 }
1764
1765 if (dev->out_pipe) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001766 ep = usb_pipeendpoint(dev->out_pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 urb->pipe = dev->out_pipe;
David Brownell28ffd792008-04-25 18:51:10 -07001768 retval = test_halt(dev, ep, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
1770done:
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001771 simple_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return retval;
1773}
1774
Mathias Nymancc2e60d2017-12-15 15:11:38 +02001775static int toggle_sync_simple(struct usbtest_dev *dev)
1776{
1777 int ep;
1778 int retval = 0;
1779 struct urb *urb;
1780 struct usb_device *udev = testdev_to_usbdev(dev);
1781 unsigned maxp = get_maxpacket(udev, dev->out_pipe);
1782
1783 /*
1784 * Create a URB that causes a transfer of uneven amount of data packets
1785 * This way the clear toggle has an impact on the data toggle sequence.
1786 * Use 2 maxpacket length packets and one zero packet.
1787 */
1788 urb = simple_alloc_urb(udev, 0, 2 * maxp, 0);
1789 if (urb == NULL)
1790 return -ENOMEM;
1791
1792 urb->transfer_flags |= URB_ZERO_PACKET;
1793
1794 ep = usb_pipeendpoint(dev->out_pipe);
1795 urb->pipe = dev->out_pipe;
1796 retval = test_toggle_sync(dev, ep, urb);
1797
1798 simple_free_urb(urb);
1799 return retval;
1800}
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802/*-------------------------------------------------------------------------*/
1803
1804/* Control OUT tests use the vendor control requests from Intel's
1805 * USB 2.0 compliance test device: write a buffer, read it back.
1806 *
1807 * Intel's spec only _requires_ that it work for one packet, which
1808 * is pretty weak. Some HCDs place limits here; most devices will
1809 * need to be able to handle more than one OUT data packet. We'll
1810 * try whatever we're told to try.
1811 */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001812static int ctrl_out(struct usbtest_dev *dev,
Martin Fuzzey084fb202011-01-16 19:17:11 +01001813 unsigned count, unsigned length, unsigned vary, unsigned offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Orjan Fribergf54fa842006-08-08 23:31:40 -07001815 unsigned i, j, len;
1816 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 u8 *buf;
1818 char *what = "?";
1819 struct usb_device *udev;
Orjan Fribergf54fa842006-08-08 23:31:40 -07001820
David Brownellff7c79e2005-04-22 13:17:00 -07001821 if (length < 1 || length > 0xffff || vary >= length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return -EINVAL;
1823
Martin Fuzzey084fb202011-01-16 19:17:11 +01001824 buf = kmalloc(length + offset, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (!buf)
1826 return -ENOMEM;
1827
Martin Fuzzey084fb202011-01-16 19:17:11 +01001828 buf += offset;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001829 udev = testdev_to_usbdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 len = length;
1831 retval = 0;
1832
1833 /* NOTE: hardware might well act differently if we pushed it
1834 * with lots back-to-back queued requests.
1835 */
1836 for (i = 0; i < count; i++) {
1837 /* write patterned data */
1838 for (j = 0; j < len; j++)
Peter Chen169900f2015-09-01 09:48:00 +08001839 buf[j] = (u8)(i + j);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001840 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 0x5b, USB_DIR_OUT|USB_TYPE_VENDOR,
1842 0, 0, buf, len, USB_CTRL_SET_TIMEOUT);
1843 if (retval != len) {
1844 what = "write";
David Brownellff7c79e2005-04-22 13:17:00 -07001845 if (retval >= 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001846 ERROR(dev, "ctrl_out, wlen %d (expected %d)\n",
David Brownellff7c79e2005-04-22 13:17:00 -07001847 retval, len);
1848 retval = -EBADMSG;
1849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 break;
1851 }
1852
1853 /* read it back -- assuming nothing intervened!! */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001854 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 0x5c, USB_DIR_IN|USB_TYPE_VENDOR,
1856 0, 0, buf, len, USB_CTRL_GET_TIMEOUT);
1857 if (retval != len) {
1858 what = "read";
David Brownellff7c79e2005-04-22 13:17:00 -07001859 if (retval >= 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001860 ERROR(dev, "ctrl_out, rlen %d (expected %d)\n",
David Brownellff7c79e2005-04-22 13:17:00 -07001861 retval, len);
1862 retval = -EBADMSG;
1863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 break;
1865 }
1866
1867 /* fail if we can't verify */
1868 for (j = 0; j < len; j++) {
Peter Chen169900f2015-09-01 09:48:00 +08001869 if (buf[j] != (u8)(i + j)) {
David Brownell28ffd792008-04-25 18:51:10 -07001870 ERROR(dev, "ctrl_out, byte %d is %d not %d\n",
Peter Chen169900f2015-09-01 09:48:00 +08001871 j, buf[j], (u8)(i + j));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 retval = -EBADMSG;
1873 break;
1874 }
1875 }
1876 if (retval < 0) {
1877 what = "verify";
1878 break;
1879 }
1880
1881 len += vary;
David Brownellff7c79e2005-04-22 13:17:00 -07001882
1883 /* [real world] the "zero bytes IN" case isn't really used.
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001884 * hardware can easily trip up in this weird case, since its
David Brownellff7c79e2005-04-22 13:17:00 -07001885 * status stage is IN, not OUT like other ep0in transfers.
1886 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 if (len > length)
David Brownellff7c79e2005-04-22 13:17:00 -07001888 len = realworld ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 }
1890
1891 if (retval < 0)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001892 ERROR(dev, "ctrl_out %s failed, code %d, count %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 what, retval, i);
1894
Martin Fuzzey084fb202011-01-16 19:17:11 +01001895 kfree(buf - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return retval;
1897}
1898
1899/*-------------------------------------------------------------------------*/
1900
Peter Chen145f48c2015-10-13 15:18:21 +08001901/* ISO/BULK tests ... mimics common usage
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 * - buffer length is split into N packets (mostly maxpacket sized)
1903 * - multi-buffers according to sglen
1904 */
1905
Peter Chen145f48c2015-10-13 15:18:21 +08001906struct transfer_context {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 unsigned count;
1908 unsigned pending;
1909 spinlock_t lock;
1910 struct completion done;
Alan Stern9da21502006-05-22 16:47:13 -04001911 int submit_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 unsigned long errors;
Alan Stern9da21502006-05-22 16:47:13 -04001913 unsigned long packet_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 struct usbtest_dev *dev;
Peter Chen145f48c2015-10-13 15:18:21 +08001915 bool is_iso;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916};
1917
Peter Chen145f48c2015-10-13 15:18:21 +08001918static void complicated_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
Peter Chen145f48c2015-10-13 15:18:21 +08001920 struct transfer_context *ctx = urb->context;
Sebastian Andrzej Siewior6f3fde62018-07-01 17:35:44 +02001921 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Sebastian Andrzej Siewior6f3fde62018-07-01 17:35:44 +02001923 spin_lock_irqsave(&ctx->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 ctx->count--;
1925
Alan Stern9da21502006-05-22 16:47:13 -04001926 ctx->packet_count += urb->number_of_packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if (urb->error_count > 0)
1928 ctx->errors += urb->error_count;
Alan Stern9da21502006-05-22 16:47:13 -04001929 else if (urb->status != 0)
Peter Chen145f48c2015-10-13 15:18:21 +08001930 ctx->errors += (ctx->is_iso ? urb->number_of_packets : 1);
Martin Fuzzey40aed522010-10-01 00:20:48 +02001931 else if (urb->actual_length != urb->transfer_buffer_length)
1932 ctx->errors++;
Martin Fuzzey084fb202011-01-16 19:17:11 +01001933 else if (check_guard_bytes(ctx->dev, urb) != 0)
1934 ctx->errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Alan Stern9da21502006-05-22 16:47:13 -04001936 if (urb->status == 0 && ctx->count > (ctx->pending - 1)
1937 && !ctx->submit_error) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001938 int status = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 switch (status) {
1940 case 0:
1941 goto done;
1942 default:
David Brownell28ffd792008-04-25 18:51:10 -07001943 dev_err(&ctx->dev->intf->dev,
Peter Chenc7c78062015-11-18 17:40:23 +08001944 "resubmit err %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 status);
1946 /* FALLTHROUGH */
1947 case -ENODEV: /* disconnected */
Alan Stern9da21502006-05-22 16:47:13 -04001948 case -ESHUTDOWN: /* endpoint disabled */
1949 ctx->submit_error = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 break;
1951 }
1952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954 ctx->pending--;
1955 if (ctx->pending == 0) {
1956 if (ctx->errors)
David Brownell28ffd792008-04-25 18:51:10 -07001957 dev_err(&ctx->dev->intf->dev,
Peter Chenc7c78062015-11-18 17:40:23 +08001958 "during the test, %lu errors out of %lu\n",
Alan Stern9da21502006-05-22 16:47:13 -04001959 ctx->errors, ctx->packet_count);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001960 complete(&ctx->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
1962done:
Sebastian Andrzej Siewior6f3fde62018-07-01 17:35:44 +02001963 spin_unlock_irqrestore(&ctx->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001966static struct urb *iso_alloc_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 struct usb_device *udev,
1968 int pipe,
1969 struct usb_endpoint_descriptor *desc,
Martin Fuzzey084fb202011-01-16 19:17:11 +01001970 long bytes,
1971 unsigned offset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972)
1973{
1974 struct urb *urb;
1975 unsigned i, maxp, packets;
1976
1977 if (bytes < 0 || !desc)
1978 return NULL;
Jaejoong Kim9f8e32d2017-10-20 16:29:15 +09001979 maxp = usb_endpoint_maxp(desc);
Felipe Balbi131e19b2016-09-28 13:40:03 +03001980 maxp *= usb_endpoint_maxp_mult(desc);
Julia Lawalldfa5ec72008-03-04 15:25:11 -08001981 packets = DIV_ROUND_UP(bytes, maxp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001983 urb = usb_alloc_urb(packets, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (!urb)
1985 return urb;
1986 urb->dev = udev;
1987 urb->pipe = pipe;
1988
1989 urb->number_of_packets = packets;
1990 urb->transfer_buffer_length = bytes;
Martin Fuzzey084fb202011-01-16 19:17:11 +01001991 urb->transfer_buffer = usb_alloc_coherent(udev, bytes + offset,
1992 GFP_KERNEL,
1993 &urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 if (!urb->transfer_buffer) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001995 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return NULL;
1997 }
Martin Fuzzey084fb202011-01-16 19:17:11 +01001998 if (offset) {
1999 memset(urb->transfer_buffer, GUARD_BYTE, offset);
2000 urb->transfer_buffer += offset;
2001 urb->transfer_dma += offset;
2002 }
2003 /* For inbound transfers use guard byte so that test fails if
2004 data not correctly copied */
2005 memset(urb->transfer_buffer,
2006 usb_pipein(urb->pipe) ? GUARD_BYTE : 0,
2007 bytes);
2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 for (i = 0; i < packets; i++) {
2010 /* here, only the last packet will be short */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002011 urb->iso_frame_desc[i].length = min((unsigned) bytes, maxp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 bytes -= urb->iso_frame_desc[i].length;
2013
2014 urb->iso_frame_desc[i].offset = maxp * i;
2015 }
2016
Peter Chen145f48c2015-10-13 15:18:21 +08002017 urb->complete = complicated_callback;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002018 /* urb->context = SET BY CALLER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 urb->interval = 1 << (desc->bInterval - 1);
2020 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
2021 return urb;
2022}
2023
2024static int
Deepa Dinamani18fc4eb2015-11-04 15:29:11 -08002025test_queue(struct usbtest_dev *dev, struct usbtest_param_32 *param,
Martin Fuzzey084fb202011-01-16 19:17:11 +01002026 int pipe, struct usb_endpoint_descriptor *desc, unsigned offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
Peter Chen145f48c2015-10-13 15:18:21 +08002028 struct transfer_context context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 struct usb_device *udev;
2030 unsigned i;
2031 unsigned long packets = 0;
Alan Stern9da21502006-05-22 16:47:13 -04002032 int status = 0;
Tobin C. Hardingc53439f2018-03-09 17:11:14 +11002033 struct urb *urbs[MAX_SGLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Dan Carpentercb84f562017-09-30 11:15:29 +03002035 if (!param->sglen || param->iterations > UINT_MAX / param->sglen)
2036 return -EINVAL;
2037
Tobin C. Hardingc53439f2018-03-09 17:11:14 +11002038 if (param->sglen > MAX_SGLEN)
2039 return -EINVAL;
2040
Huang Ruif55055b2013-10-21 23:15:30 +08002041 memset(&context, 0, sizeof(context));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 context.count = param->iterations * param->sglen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 context.dev = dev;
Peter Chen145f48c2015-10-13 15:18:21 +08002044 context.is_iso = !!desc;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002045 init_completion(&context.done);
2046 spin_lock_init(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002048 udev = testdev_to_usbdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
2050 for (i = 0; i < param->sglen; i++) {
Peter Chen145f48c2015-10-13 15:18:21 +08002051 if (context.is_iso)
2052 urbs[i] = iso_alloc_urb(udev, pipe, desc,
Martin Fuzzey084fb202011-01-16 19:17:11 +01002053 param->length, offset);
Peter Chen145f48c2015-10-13 15:18:21 +08002054 else
2055 urbs[i] = complicated_alloc_urb(udev, pipe,
2056 param->length, 0);
2057
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002058 if (!urbs[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 status = -ENOMEM;
2060 goto fail;
2061 }
2062 packets += urbs[i]->number_of_packets;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002063 urbs[i]->context = &context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
2065 packets *= param->iterations;
Peter Chen145f48c2015-10-13 15:18:21 +08002066
2067 if (context.is_iso) {
2068 dev_info(&dev->intf->dev,
2069 "iso period %d %sframes, wMaxPacket %d, transactions: %d\n",
2070 1 << (desc->bInterval - 1),
2071 (udev->speed == USB_SPEED_HIGH) ? "micro" : "",
Felipe Balbiefdd17e2016-09-28 14:17:38 +03002072 usb_endpoint_maxp(desc),
Felipe Balbi131e19b2016-09-28 13:40:03 +03002073 usb_endpoint_maxp_mult(desc));
Peter Chen145f48c2015-10-13 15:18:21 +08002074
2075 dev_info(&dev->intf->dev,
2076 "total %lu msec (%lu packets)\n",
2077 (packets * (1 << (desc->bInterval - 1)))
2078 / ((udev->speed == USB_SPEED_HIGH) ? 8 : 1),
2079 packets);
2080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002082 spin_lock_irq(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 for (i = 0; i < param->sglen; i++) {
Alan Stern9da21502006-05-22 16:47:13 -04002084 ++context.pending;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002085 status = usb_submit_urb(urbs[i], GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (status < 0) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002087 ERROR(dev, "submit iso[%d], error %d\n", i, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 if (i == 0) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002089 spin_unlock_irq(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 goto fail;
2091 }
2092
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002093 simple_free_urb(urbs[i]);
Ming Leie10e1be2010-08-02 22:09:01 +08002094 urbs[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 context.pending--;
Alan Stern9da21502006-05-22 16:47:13 -04002096 context.submit_error = 1;
2097 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
2099 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002100 spin_unlock_irq(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002102 wait_for_completion(&context.done);
Alan Stern9da21502006-05-22 16:47:13 -04002103
Ming Leie10e1be2010-08-02 22:09:01 +08002104 for (i = 0; i < param->sglen; i++) {
2105 if (urbs[i])
2106 simple_free_urb(urbs[i]);
2107 }
Alan Stern9da21502006-05-22 16:47:13 -04002108 /*
2109 * Isochronous transfers are expected to fail sometimes. As an
2110 * arbitrary limit, we will report an error if any submissions
2111 * fail or if the transfer failure rate is > 10%.
2112 */
2113 if (status != 0)
2114 ;
2115 else if (context.submit_error)
2116 status = -EACCES;
Peter Chen145f48c2015-10-13 15:18:21 +08002117 else if (context.errors >
2118 (context.is_iso ? context.packet_count / 10 : 0))
Alan Stern9da21502006-05-22 16:47:13 -04002119 status = -EIO;
2120 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122fail:
2123 for (i = 0; i < param->sglen; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002124 if (urbs[i])
2125 simple_free_urb(urbs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 }
2127 return status;
2128}
2129
Martin Fuzzey084fb202011-01-16 19:17:11 +01002130static int test_unaligned_bulk(
2131 struct usbtest_dev *tdev,
2132 int pipe,
2133 unsigned length,
2134 int iterations,
2135 unsigned transfer_flags,
2136 const char *label)
2137{
2138 int retval;
Peter Chen145f48c2015-10-13 15:18:21 +08002139 struct urb *urb = usbtest_alloc_urb(testdev_to_usbdev(tdev),
2140 pipe, length, transfer_flags, 1, 0, simple_callback);
Martin Fuzzey084fb202011-01-16 19:17:11 +01002141
2142 if (!urb)
2143 return -ENOMEM;
2144
2145 retval = simple_io(tdev, urb, iterations, 0, 0, label);
2146 simple_free_urb(urb);
2147 return retval;
2148}
2149
Deepa Dinamani18fc4eb2015-11-04 15:29:11 -08002150/* Run tests. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151static int
Deepa Dinamani18fc4eb2015-11-04 15:29:11 -08002152usbtest_do_ioctl(struct usb_interface *intf, struct usbtest_param_32 *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002154 struct usbtest_dev *dev = usb_get_intfdata(intf);
2155 struct usb_device *udev = testdev_to_usbdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 struct urb *urb;
2157 struct scatterlist *sg;
2158 struct usb_sg_request req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 unsigned i;
Deepa Dinamani18fc4eb2015-11-04 15:29:11 -08002160 int retval = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
roel kluin8aafdf62008-10-21 00:36:44 -04002162 if (param->iterations <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 return -EINVAL;
Dan Carpentercb84f562017-09-30 11:15:29 +03002164 if (param->sglen > MAX_SGLEN)
2165 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 /*
2167 * Just a bunch of test cases that every HCD is expected to handle.
2168 *
2169 * Some may need specific firmware, though it'd be good to have
2170 * one firmware image to handle all the test cases.
2171 *
2172 * FIXME add more tests! cancel requests, verify the data, control
2173 * queueing, concurrent read+write threads, and so on.
2174 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 switch (param->test_num) {
2176
2177 case 0:
David Brownell28ffd792008-04-25 18:51:10 -07002178 dev_info(&intf->dev, "TEST 0: NOP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 retval = 0;
2180 break;
2181
2182 /* Simple non-queued bulk I/O tests */
2183 case 1:
2184 if (dev->out_pipe == 0)
2185 break;
David Brownell28ffd792008-04-25 18:51:10 -07002186 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 "TEST 1: write %d bytes %u times\n",
2188 param->length, param->iterations);
Amit Virdi457a0952014-08-22 14:36:37 +05302189 urb = simple_alloc_urb(udev, dev->out_pipe, param->length, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 if (!urb) {
2191 retval = -ENOMEM;
2192 break;
2193 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002194 /* FIRMWARE: bulk sink (maybe accepts short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07002195 retval = simple_io(dev, urb, param->iterations, 0, 0, "test1");
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002196 simple_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 break;
2198 case 2:
2199 if (dev->in_pipe == 0)
2200 break;
David Brownell28ffd792008-04-25 18:51:10 -07002201 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 "TEST 2: read %d bytes %u times\n",
2203 param->length, param->iterations);
Amit Virdi457a0952014-08-22 14:36:37 +05302204 urb = simple_alloc_urb(udev, dev->in_pipe, param->length, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 if (!urb) {
2206 retval = -ENOMEM;
2207 break;
2208 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002209 /* FIRMWARE: bulk source (maybe generates short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07002210 retval = simple_io(dev, urb, param->iterations, 0, 0, "test2");
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002211 simple_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 break;
2213 case 3:
2214 if (dev->out_pipe == 0 || param->vary == 0)
2215 break;
David Brownell28ffd792008-04-25 18:51:10 -07002216 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 "TEST 3: write/%d 0..%d bytes %u times\n",
2218 param->vary, param->length, param->iterations);
Amit Virdi457a0952014-08-22 14:36:37 +05302219 urb = simple_alloc_urb(udev, dev->out_pipe, param->length, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (!urb) {
2221 retval = -ENOMEM;
2222 break;
2223 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002224 /* FIRMWARE: bulk sink (maybe accepts short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07002225 retval = simple_io(dev, urb, param->iterations, param->vary,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 0, "test3");
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002227 simple_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 break;
2229 case 4:
2230 if (dev->in_pipe == 0 || param->vary == 0)
2231 break;
David Brownell28ffd792008-04-25 18:51:10 -07002232 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 "TEST 4: read/%d 0..%d bytes %u times\n",
2234 param->vary, param->length, param->iterations);
Amit Virdi457a0952014-08-22 14:36:37 +05302235 urb = simple_alloc_urb(udev, dev->in_pipe, param->length, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 if (!urb) {
2237 retval = -ENOMEM;
2238 break;
2239 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002240 /* FIRMWARE: bulk source (maybe generates short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07002241 retval = simple_io(dev, urb, param->iterations, param->vary,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 0, "test4");
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002243 simple_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 break;
2245
2246 /* Queued bulk I/O tests */
2247 case 5:
2248 if (dev->out_pipe == 0 || param->sglen == 0)
2249 break;
David Brownell28ffd792008-04-25 18:51:10 -07002250 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 "TEST 5: write %d sglists %d entries of %d bytes\n",
2252 param->iterations,
2253 param->sglen, param->length);
Alan Sternb9a6e8e2015-09-01 09:48:01 +08002254 sg = alloc_sglist(param->sglen, param->length,
2255 0, dev, dev->out_pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (!sg) {
2257 retval = -ENOMEM;
2258 break;
2259 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002260 /* FIRMWARE: bulk sink (maybe accepts short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07002261 retval = perform_sglist(dev, param->iterations, dev->out_pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 &req, sg, param->sglen);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002263 free_sglist(sg, param->sglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 break;
2265
2266 case 6:
2267 if (dev->in_pipe == 0 || param->sglen == 0)
2268 break;
David Brownell28ffd792008-04-25 18:51:10 -07002269 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 "TEST 6: read %d sglists %d entries of %d bytes\n",
2271 param->iterations,
2272 param->sglen, param->length);
Alan Sternb9a6e8e2015-09-01 09:48:01 +08002273 sg = alloc_sglist(param->sglen, param->length,
2274 0, dev, dev->in_pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 if (!sg) {
2276 retval = -ENOMEM;
2277 break;
2278 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002279 /* FIRMWARE: bulk source (maybe generates short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07002280 retval = perform_sglist(dev, param->iterations, dev->in_pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 &req, sg, param->sglen);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002282 free_sglist(sg, param->sglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 break;
2284 case 7:
2285 if (dev->out_pipe == 0 || param->sglen == 0 || param->vary == 0)
2286 break;
David Brownell28ffd792008-04-25 18:51:10 -07002287 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 "TEST 7: write/%d %d sglists %d entries 0..%d bytes\n",
2289 param->vary, param->iterations,
2290 param->sglen, param->length);
Alan Sternb9a6e8e2015-09-01 09:48:01 +08002291 sg = alloc_sglist(param->sglen, param->length,
2292 param->vary, dev, dev->out_pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 if (!sg) {
2294 retval = -ENOMEM;
2295 break;
2296 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002297 /* FIRMWARE: bulk sink (maybe accepts short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07002298 retval = perform_sglist(dev, param->iterations, dev->out_pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 &req, sg, param->sglen);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002300 free_sglist(sg, param->sglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 break;
2302 case 8:
2303 if (dev->in_pipe == 0 || param->sglen == 0 || param->vary == 0)
2304 break;
David Brownell28ffd792008-04-25 18:51:10 -07002305 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 "TEST 8: read/%d %d sglists %d entries 0..%d bytes\n",
2307 param->vary, param->iterations,
2308 param->sglen, param->length);
Alan Sternb9a6e8e2015-09-01 09:48:01 +08002309 sg = alloc_sglist(param->sglen, param->length,
2310 param->vary, dev, dev->in_pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 if (!sg) {
2312 retval = -ENOMEM;
2313 break;
2314 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002315 /* FIRMWARE: bulk source (maybe generates short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07002316 retval = perform_sglist(dev, param->iterations, dev->in_pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 &req, sg, param->sglen);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002318 free_sglist(sg, param->sglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 break;
2320
2321 /* non-queued sanity tests for control (chapter 9 subset) */
2322 case 9:
2323 retval = 0;
David Brownell28ffd792008-04-25 18:51:10 -07002324 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 "TEST 9: ch9 (subset) control tests, %d times\n",
2326 param->iterations);
2327 for (i = param->iterations; retval == 0 && i--; /* NOP */)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002328 retval = ch9_postconfig(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 if (retval)
David Brownell28ffd792008-04-25 18:51:10 -07002330 dev_err(&intf->dev, "ch9 subset failed, "
2331 "iterations left %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 break;
2333
2334 /* queued control messaging */
2335 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 retval = 0;
David Brownell28ffd792008-04-25 18:51:10 -07002337 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 "TEST 10: queue %d control calls, %d times\n",
2339 param->sglen,
2340 param->iterations);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002341 retval = test_ctrl_queue(dev, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 break;
2343
2344 /* simple non-queued unlinks (ring with one urb) */
2345 case 11:
2346 if (dev->in_pipe == 0 || !param->length)
2347 break;
2348 retval = 0;
David Brownell28ffd792008-04-25 18:51:10 -07002349 dev_info(&intf->dev, "TEST 11: unlink %d reads of %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 param->iterations, param->length);
2351 for (i = param->iterations; retval == 0 && i--; /* NOP */)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002352 retval = unlink_simple(dev, dev->in_pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 param->length);
2354 if (retval)
David Brownell28ffd792008-04-25 18:51:10 -07002355 dev_err(&intf->dev, "unlink reads failed %d, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 "iterations left %d\n", retval, i);
2357 break;
2358 case 12:
2359 if (dev->out_pipe == 0 || !param->length)
2360 break;
2361 retval = 0;
David Brownell28ffd792008-04-25 18:51:10 -07002362 dev_info(&intf->dev, "TEST 12: unlink %d writes of %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 param->iterations, param->length);
2364 for (i = param->iterations; retval == 0 && i--; /* NOP */)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002365 retval = unlink_simple(dev, dev->out_pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 param->length);
2367 if (retval)
David Brownell28ffd792008-04-25 18:51:10 -07002368 dev_err(&intf->dev, "unlink writes failed %d, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 "iterations left %d\n", retval, i);
2370 break;
2371
2372 /* ep halt tests */
2373 case 13:
2374 if (dev->out_pipe == 0 && dev->in_pipe == 0)
2375 break;
2376 retval = 0;
David Brownell28ffd792008-04-25 18:51:10 -07002377 dev_info(&intf->dev, "TEST 13: set/clear %d halts\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 param->iterations);
2379 for (i = param->iterations; retval == 0 && i--; /* NOP */)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002380 retval = halt_simple(dev);
David Brownell28ffd792008-04-25 18:51:10 -07002381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (retval)
David Brownell28ffd792008-04-25 18:51:10 -07002383 ERROR(dev, "halts failed, iterations left %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 break;
2385
2386 /* control write tests */
2387 case 14:
2388 if (!dev->info->ctrl_out)
2389 break;
David Brownell28ffd792008-04-25 18:51:10 -07002390 dev_info(&intf->dev, "TEST 14: %d ep0out, %d..%d vary %d\n",
David Brownellff7c79e2005-04-22 13:17:00 -07002391 param->iterations,
2392 realworld ? 1 : 0, param->length,
2393 param->vary);
David Brownell28ffd792008-04-25 18:51:10 -07002394 retval = ctrl_out(dev, param->iterations,
Martin Fuzzey084fb202011-01-16 19:17:11 +01002395 param->length, param->vary, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 break;
2397
2398 /* iso write tests */
2399 case 15:
2400 if (dev->out_iso_pipe == 0 || param->sglen == 0)
2401 break;
David Brownell28ffd792008-04-25 18:51:10 -07002402 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 "TEST 15: write %d iso, %d entries of %d bytes\n",
2404 param->iterations,
2405 param->sglen, param->length);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002406 /* FIRMWARE: iso sink */
Peter Chen145f48c2015-10-13 15:18:21 +08002407 retval = test_queue(dev, param,
Martin Fuzzey084fb202011-01-16 19:17:11 +01002408 dev->out_iso_pipe, dev->iso_out, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 break;
2410
2411 /* iso read tests */
2412 case 16:
2413 if (dev->in_iso_pipe == 0 || param->sglen == 0)
2414 break;
David Brownell28ffd792008-04-25 18:51:10 -07002415 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 "TEST 16: read %d iso, %d entries of %d bytes\n",
2417 param->iterations,
2418 param->sglen, param->length);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002419 /* FIRMWARE: iso source */
Peter Chen145f48c2015-10-13 15:18:21 +08002420 retval = test_queue(dev, param,
Martin Fuzzey084fb202011-01-16 19:17:11 +01002421 dev->in_iso_pipe, dev->iso_in, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 break;
2423
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002424 /* FIXME scatterlist cancel (needs helper thread) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Martin Fuzzey084fb202011-01-16 19:17:11 +01002426 /* Tests for bulk I/O using DMA mapping by core and odd address */
2427 case 17:
2428 if (dev->out_pipe == 0)
2429 break;
2430 dev_info(&intf->dev,
2431 "TEST 17: write odd addr %d bytes %u times core map\n",
2432 param->length, param->iterations);
2433
2434 retval = test_unaligned_bulk(
2435 dev, dev->out_pipe,
2436 param->length, param->iterations,
2437 0, "test17");
2438 break;
2439
2440 case 18:
2441 if (dev->in_pipe == 0)
2442 break;
2443 dev_info(&intf->dev,
2444 "TEST 18: read odd addr %d bytes %u times core map\n",
2445 param->length, param->iterations);
2446
2447 retval = test_unaligned_bulk(
2448 dev, dev->in_pipe,
2449 param->length, param->iterations,
2450 0, "test18");
2451 break;
2452
2453 /* Tests for bulk I/O using premapped coherent buffer and odd address */
2454 case 19:
2455 if (dev->out_pipe == 0)
2456 break;
2457 dev_info(&intf->dev,
2458 "TEST 19: write odd addr %d bytes %u times premapped\n",
2459 param->length, param->iterations);
2460
2461 retval = test_unaligned_bulk(
2462 dev, dev->out_pipe,
2463 param->length, param->iterations,
2464 URB_NO_TRANSFER_DMA_MAP, "test19");
2465 break;
2466
2467 case 20:
2468 if (dev->in_pipe == 0)
2469 break;
2470 dev_info(&intf->dev,
2471 "TEST 20: read odd addr %d bytes %u times premapped\n",
2472 param->length, param->iterations);
2473
2474 retval = test_unaligned_bulk(
2475 dev, dev->in_pipe,
2476 param->length, param->iterations,
2477 URB_NO_TRANSFER_DMA_MAP, "test20");
2478 break;
2479
2480 /* control write tests with unaligned buffer */
2481 case 21:
2482 if (!dev->info->ctrl_out)
2483 break;
2484 dev_info(&intf->dev,
2485 "TEST 21: %d ep0out odd addr, %d..%d vary %d\n",
2486 param->iterations,
2487 realworld ? 1 : 0, param->length,
2488 param->vary);
2489 retval = ctrl_out(dev, param->iterations,
2490 param->length, param->vary, 1);
2491 break;
2492
2493 /* unaligned iso tests */
2494 case 22:
2495 if (dev->out_iso_pipe == 0 || param->sglen == 0)
2496 break;
2497 dev_info(&intf->dev,
2498 "TEST 22: write %d iso odd, %d entries of %d bytes\n",
2499 param->iterations,
2500 param->sglen, param->length);
Peter Chen145f48c2015-10-13 15:18:21 +08002501 retval = test_queue(dev, param,
Martin Fuzzey084fb202011-01-16 19:17:11 +01002502 dev->out_iso_pipe, dev->iso_out, 1);
2503 break;
2504
2505 case 23:
2506 if (dev->in_iso_pipe == 0 || param->sglen == 0)
2507 break;
2508 dev_info(&intf->dev,
2509 "TEST 23: read %d iso odd, %d entries of %d bytes\n",
2510 param->iterations,
2511 param->sglen, param->length);
Peter Chen145f48c2015-10-13 15:18:21 +08002512 retval = test_queue(dev, param,
Martin Fuzzey084fb202011-01-16 19:17:11 +01002513 dev->in_iso_pipe, dev->iso_in, 1);
2514 break;
2515
Alan Stern869410f2011-04-14 11:21:04 -04002516 /* unlink URBs from a bulk-OUT queue */
2517 case 24:
2518 if (dev->out_pipe == 0 || !param->length || param->sglen < 4)
2519 break;
2520 retval = 0;
Alan Stern2cb50002013-01-02 13:58:18 -05002521 dev_info(&intf->dev, "TEST 24: unlink from %d queues of "
Alan Stern869410f2011-04-14 11:21:04 -04002522 "%d %d-byte writes\n",
2523 param->iterations, param->sglen, param->length);
2524 for (i = param->iterations; retval == 0 && i > 0; --i) {
2525 retval = unlink_queued(dev, dev->out_pipe,
2526 param->sglen, param->length);
2527 if (retval) {
2528 dev_err(&intf->dev,
2529 "unlink queued writes failed %d, "
2530 "iterations left %d\n", retval, i);
2531 break;
2532 }
2533 }
2534 break;
2535
Amit Virdi457a0952014-08-22 14:36:37 +05302536 /* Simple non-queued interrupt I/O tests */
2537 case 25:
2538 if (dev->out_int_pipe == 0)
2539 break;
2540 dev_info(&intf->dev,
2541 "TEST 25: write %d bytes %u times\n",
2542 param->length, param->iterations);
2543 urb = simple_alloc_urb(udev, dev->out_int_pipe, param->length,
2544 dev->int_out->bInterval);
2545 if (!urb) {
2546 retval = -ENOMEM;
2547 break;
2548 }
2549 /* FIRMWARE: interrupt sink (maybe accepts short writes) */
2550 retval = simple_io(dev, urb, param->iterations, 0, 0, "test25");
2551 simple_free_urb(urb);
2552 break;
2553 case 26:
2554 if (dev->in_int_pipe == 0)
2555 break;
2556 dev_info(&intf->dev,
2557 "TEST 26: read %d bytes %u times\n",
2558 param->length, param->iterations);
2559 urb = simple_alloc_urb(udev, dev->in_int_pipe, param->length,
2560 dev->int_in->bInterval);
2561 if (!urb) {
2562 retval = -ENOMEM;
2563 break;
2564 }
2565 /* FIRMWARE: interrupt source (maybe generates short writes) */
2566 retval = simple_io(dev, urb, param->iterations, 0, 0, "test26");
2567 simple_free_urb(urb);
2568 break;
Peter Chen145f48c2015-10-13 15:18:21 +08002569 case 27:
2570 /* We do performance test, so ignore data compare */
2571 if (dev->out_pipe == 0 || param->sglen == 0 || pattern != 0)
2572 break;
2573 dev_info(&intf->dev,
2574 "TEST 27: bulk write %dMbytes\n", (param->iterations *
2575 param->sglen * param->length) / (1024 * 1024));
2576 retval = test_queue(dev, param,
2577 dev->out_pipe, NULL, 0);
2578 break;
2579 case 28:
2580 if (dev->in_pipe == 0 || param->sglen == 0 || pattern != 0)
2581 break;
2582 dev_info(&intf->dev,
2583 "TEST 28: bulk read %dMbytes\n", (param->iterations *
2584 param->sglen * param->length) / (1024 * 1024));
2585 retval = test_queue(dev, param,
2586 dev->in_pipe, NULL, 0);
2587 break;
Mathias Nymancc2e60d2017-12-15 15:11:38 +02002588 /* Test data Toggle/seq_nr clear between bulk out transfers */
2589 case 29:
2590 if (dev->out_pipe == 0)
2591 break;
2592 retval = 0;
2593 dev_info(&intf->dev, "TEST 29: Clear toggle between bulk writes %d times\n",
2594 param->iterations);
2595 for (i = param->iterations; retval == 0 && i > 0; --i)
2596 retval = toggle_sync_simple(dev);
2597
2598 if (retval)
2599 ERROR(dev, "toggle sync failed, iterations left %d\n",
2600 i);
2601 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 }
Deepa Dinamani18fc4eb2015-11-04 15:29:11 -08002603 return retval;
2604}
2605
2606/*-------------------------------------------------------------------------*/
2607
2608/* We only have this one interface to user space, through usbfs.
2609 * User mode code can scan usbfs to find N different devices (maybe on
2610 * different busses) to use when testing, and allocate one thread per
2611 * test. So discovery is simplified, and we have no device naming issues.
2612 *
2613 * Don't use these only as stress/load tests. Use them along with with
2614 * other USB bus activity: plugging, unplugging, mousing, mp3 playback,
2615 * video capture, and so on. Run different tests at different times, in
2616 * different sequences. Nothing here should interact with other devices,
2617 * except indirectly by consuming USB bandwidth and CPU resources for test
2618 * threads and request completion. But the only way to know that for sure
2619 * is to test when HC queues are in use by many devices.
2620 *
2621 * WARNING: Because usbfs grabs udev->dev.sem before calling this ioctl(),
2622 * it locks out usbcore in certain code paths. Notably, if you disconnect
2623 * the device-under-test, hub_wq will wait block forever waiting for the
2624 * ioctl to complete ... so that usb_disconnect() can abort the pending
2625 * urbs and then call usbtest_disconnect(). To abort a test, you're best
2626 * off just killing the userspace task and waiting for it to exit.
2627 */
2628
2629static int
2630usbtest_ioctl(struct usb_interface *intf, unsigned int code, void *buf)
2631{
2632
2633 struct usbtest_dev *dev = usb_get_intfdata(intf);
2634 struct usbtest_param_64 *param_64 = buf;
2635 struct usbtest_param_32 temp;
2636 struct usbtest_param_32 *param_32 = buf;
2637 struct timespec64 start;
2638 struct timespec64 end;
2639 struct timespec64 duration;
2640 int retval = -EOPNOTSUPP;
2641
2642 /* FIXME USBDEVFS_CONNECTINFO doesn't say how fast the device is. */
2643
2644 pattern = mod_pattern;
2645
2646 if (mutex_lock_interruptible(&dev->lock))
2647 return -ERESTARTSYS;
2648
2649 /* FIXME: What if a system sleep starts while a test is running? */
2650
2651 /* some devices, like ez-usb default devices, need a non-default
2652 * altsetting to have any active endpoints. some tests change
2653 * altsettings; force a default so most tests don't need to check.
2654 */
2655 if (dev->info->alt >= 0) {
2656 if (intf->altsetting->desc.bInterfaceNumber) {
2657 retval = -ENODEV;
2658 goto free_mutex;
2659 }
2660 retval = set_altsetting(dev, dev->info->alt);
2661 if (retval) {
2662 dev_err(&intf->dev,
2663 "set altsetting to %d failed, %d\n",
2664 dev->info->alt, retval);
2665 goto free_mutex;
2666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 }
Deepa Dinamani18fc4eb2015-11-04 15:29:11 -08002668
2669 switch (code) {
2670 case USBTEST_REQUEST_64:
2671 temp.test_num = param_64->test_num;
2672 temp.iterations = param_64->iterations;
2673 temp.length = param_64->length;
2674 temp.sglen = param_64->sglen;
2675 temp.vary = param_64->vary;
2676 param_32 = &temp;
2677 break;
2678
2679 case USBTEST_REQUEST_32:
2680 break;
2681
2682 default:
2683 retval = -EOPNOTSUPP;
2684 goto free_mutex;
2685 }
2686
2687 ktime_get_ts64(&start);
2688
2689 retval = usbtest_do_ioctl(intf, param_32);
Peter Chen28324932016-07-26 16:01:30 +08002690 if (retval < 0)
Deepa Dinamani18fc4eb2015-11-04 15:29:11 -08002691 goto free_mutex;
2692
2693 ktime_get_ts64(&end);
2694
2695 duration = timespec64_sub(end, start);
2696
2697 temp.duration_sec = duration.tv_sec;
2698 temp.duration_usec = duration.tv_nsec/NSEC_PER_USEC;
2699
2700 switch (code) {
2701 case USBTEST_REQUEST_32:
2702 param_32->duration_sec = temp.duration_sec;
2703 param_32->duration_usec = temp.duration_usec;
2704 break;
2705
2706 case USBTEST_REQUEST_64:
2707 param_64->duration_sec = temp.duration_sec;
2708 param_64->duration_usec = temp.duration_usec;
2709 break;
2710 }
2711
2712free_mutex:
Matthias Kaehlcke1cfab022007-12-13 16:15:33 -08002713 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 return retval;
2715}
2716
2717/*-------------------------------------------------------------------------*/
2718
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002719static unsigned force_interrupt;
2720module_param(force_interrupt, uint, 0);
2721MODULE_PARM_DESC(force_interrupt, "0 = test default; else interrupt");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
2723#ifdef GENERIC
2724static unsigned short vendor;
2725module_param(vendor, ushort, 0);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002726MODULE_PARM_DESC(vendor, "vendor code (from usb-if)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
2728static unsigned short product;
2729module_param(product, ushort, 0);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002730MODULE_PARM_DESC(product, "product code (from vendor)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731#endif
2732
2733static int
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002734usbtest_probe(struct usb_interface *intf, const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
2736 struct usb_device *udev;
2737 struct usbtest_dev *dev;
2738 struct usbtest_info *info;
2739 char *rtest, *wtest;
2740 char *irtest, *iwtest;
Amit Virdi457a0952014-08-22 14:36:37 +05302741 char *intrtest, *intwtest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002743 udev = interface_to_usbdev(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
2745#ifdef GENERIC
2746 /* specify devices by module parameters? */
2747 if (id->match_flags == 0) {
2748 /* vendor match required, product match optional */
2749 if (!vendor || le16_to_cpu(udev->descriptor.idVendor) != (u16)vendor)
2750 return -ENODEV;
2751 if (product && le16_to_cpu(udev->descriptor.idProduct) != (u16)product)
2752 return -ENODEV;
David Brownell28ffd792008-04-25 18:51:10 -07002753 dev_info(&intf->dev, "matched module params, "
2754 "vend=0x%04x prod=0x%04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 le16_to_cpu(udev->descriptor.idVendor),
2756 le16_to_cpu(udev->descriptor.idProduct));
2757 }
2758#endif
2759
Christoph Lametere94b1762006-12-06 20:33:17 -08002760 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 if (!dev)
2762 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 info = (struct usbtest_info *) id->driver_info;
2764 dev->info = info;
Matthias Kaehlcke1cfab022007-12-13 16:15:33 -08002765 mutex_init(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767 dev->intf = intf;
2768
2769 /* cacheline-aligned scratch for i/o */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002770 dev->buf = kmalloc(TBUF_SIZE, GFP_KERNEL);
2771 if (dev->buf == NULL) {
2772 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 return -ENOMEM;
2774 }
2775
2776 /* NOTE this doesn't yet test the handful of difference that are
2777 * visible with high speed interrupts: bigger maxpacket (1K) and
2778 * "high bandwidth" modes (up to 3 packets/uframe).
2779 */
2780 rtest = wtest = "";
2781 irtest = iwtest = "";
Amit Virdi457a0952014-08-22 14:36:37 +05302782 intrtest = intwtest = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 if (force_interrupt || udev->speed == USB_SPEED_LOW) {
2784 if (info->ep_in) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002785 dev->in_pipe = usb_rcvintpipe(udev, info->ep_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 rtest = " intr-in";
2787 }
2788 if (info->ep_out) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002789 dev->out_pipe = usb_sndintpipe(udev, info->ep_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 wtest = " intr-out";
2791 }
2792 } else {
Alan Sternd0b46522013-01-30 16:38:11 -05002793 if (override_alt >= 0 || info->autoconf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 int status;
2795
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002796 status = get_endpoints(dev, intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 if (status < 0) {
Arjan van de Venb6c63932008-07-25 01:45:52 -07002798 WARNING(dev, "couldn't get endpoints, %d\n",
David Brownell28ffd792008-04-25 18:51:10 -07002799 status);
Julia Lawallf4a728d2012-03-25 21:08:32 +02002800 kfree(dev->buf);
2801 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 return status;
2803 }
2804 /* may find bulk or ISO pipes */
2805 } else {
2806 if (info->ep_in)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002807 dev->in_pipe = usb_rcvbulkpipe(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 info->ep_in);
2809 if (info->ep_out)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002810 dev->out_pipe = usb_sndbulkpipe(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 info->ep_out);
2812 }
2813 if (dev->in_pipe)
2814 rtest = " bulk-in";
2815 if (dev->out_pipe)
2816 wtest = " bulk-out";
2817 if (dev->in_iso_pipe)
2818 irtest = " iso-in";
2819 if (dev->out_iso_pipe)
2820 iwtest = " iso-out";
Amit Virdi457a0952014-08-22 14:36:37 +05302821 if (dev->in_int_pipe)
2822 intrtest = " int-in";
2823 if (dev->out_int_pipe)
2824 intwtest = " int-out";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 }
2826
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002827 usb_set_intfdata(intf, dev);
2828 dev_info(&intf->dev, "%s\n", info->name);
Amit Virdi457a0952014-08-22 14:36:37 +05302829 dev_info(&intf->dev, "%s {control%s%s%s%s%s%s%s} tests%s\n",
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02002830 usb_speed_string(udev->speed),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 info->ctrl_out ? " in/out" : "",
2832 rtest, wtest,
2833 irtest, iwtest,
Amit Virdi457a0952014-08-22 14:36:37 +05302834 intrtest, intwtest,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 info->alt >= 0 ? " (+alt)" : "");
2836 return 0;
2837}
2838
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002839static int usbtest_suspend(struct usb_interface *intf, pm_message_t message)
David Brownellff7c79e2005-04-22 13:17:00 -07002840{
David Brownellff7c79e2005-04-22 13:17:00 -07002841 return 0;
2842}
2843
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002844static int usbtest_resume(struct usb_interface *intf)
David Brownellff7c79e2005-04-22 13:17:00 -07002845{
David Brownellff7c79e2005-04-22 13:17:00 -07002846 return 0;
2847}
2848
2849
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002850static void usbtest_disconnect(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851{
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002852 struct usbtest_dev *dev = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002854 usb_set_intfdata(intf, NULL);
2855 dev_dbg(&intf->dev, "disconnect\n");
2856 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857}
2858
2859/* Basic testing only needs a device that can source or sink bulk traffic.
2860 * Any device can test control transfers (default with GENERIC binding).
2861 *
2862 * Several entries work with the default EP0 implementation that's built
2863 * into EZ-USB chips. There's a default vendor ID which can be overridden
2864 * by (very) small config EEPROMS, but otherwise all these devices act
2865 * identically until firmware is loaded: only EP0 works. It turns out
2866 * to be easy to make other endpoints work, without modifying that EP0
2867 * behavior. For now, we expect that kind of firmware.
2868 */
2869
2870/* an21xx or fx versions of ez-usb */
2871static struct usbtest_info ez1_info = {
2872 .name = "EZ-USB device",
2873 .ep_in = 2,
2874 .ep_out = 2,
2875 .alt = 1,
2876};
2877
2878/* fx2 version of ez-usb */
2879static struct usbtest_info ez2_info = {
2880 .name = "FX2 device",
2881 .ep_in = 6,
2882 .ep_out = 2,
2883 .alt = 1,
2884};
2885
2886/* ezusb family device with dedicated usb test firmware,
2887 */
2888static struct usbtest_info fw_info = {
2889 .name = "usb test device",
2890 .ep_in = 2,
2891 .ep_out = 2,
2892 .alt = 1,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002893 .autoconf = 1, /* iso and ctrl_out need autoconf */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 .ctrl_out = 1,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002895 .iso = 1, /* iso_ep's are #8 in/out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896};
2897
2898/* peripheral running Linux and 'zero.c' test firmware, or
2899 * its user-mode cousin. different versions of this use
2900 * different hardware with the same vendor/product codes.
2901 * host side MUST rely on the endpoint descriptors.
2902 */
2903static struct usbtest_info gz_info = {
2904 .name = "Linux gadget zero",
2905 .autoconf = 1,
2906 .ctrl_out = 1,
Boyan Nedeltchev4b85c622012-11-12 13:06:06 +02002907 .iso = 1,
Amit Virdi457a0952014-08-22 14:36:37 +05302908 .intr = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 .alt = 0,
2910};
2911
2912static struct usbtest_info um_info = {
2913 .name = "Linux user mode test driver",
2914 .autoconf = 1,
2915 .alt = -1,
2916};
2917
2918static struct usbtest_info um2_info = {
2919 .name = "Linux user mode ISO test driver",
2920 .autoconf = 1,
2921 .iso = 1,
2922 .alt = -1,
2923};
2924
2925#ifdef IBOT2
2926/* this is a nice source of high speed bulk data;
2927 * uses an FX2, with firmware provided in the device
2928 */
2929static struct usbtest_info ibot2_info = {
2930 .name = "iBOT2 webcam",
2931 .ep_in = 2,
2932 .alt = -1,
2933};
2934#endif
2935
2936#ifdef GENERIC
2937/* we can use any device to test control traffic */
2938static struct usbtest_info generic_info = {
2939 .name = "Generic USB device",
2940 .alt = -1,
2941};
2942#endif
2943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Németh Márton33b9e162010-01-10 15:34:45 +01002945static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 /*-------------------------------------------------------------*/
2948
2949 /* EZ-USB devices which download firmware to replace (or in our
2950 * case augment) the default device implementation.
2951 */
2952
2953 /* generic EZ-USB FX controller */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002954 { USB_DEVICE(0x0547, 0x2235),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 .driver_info = (unsigned long) &ez1_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002956 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
2958 /* CY3671 development board with EZ-USB FX */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002959 { USB_DEVICE(0x0547, 0x0080),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 .driver_info = (unsigned long) &ez1_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002961 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
2963 /* generic EZ-USB FX2 controller (or development board) */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002964 { USB_DEVICE(0x04b4, 0x8613),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 .driver_info = (unsigned long) &ez2_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002966 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
2968 /* re-enumerated usb test device firmware */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002969 { USB_DEVICE(0xfff0, 0xfff0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 .driver_info = (unsigned long) &fw_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002971 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973 /* "Gadget Zero" firmware runs under Linux */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002974 { USB_DEVICE(0x0525, 0xa4a0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 .driver_info = (unsigned long) &gz_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002976 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
2978 /* so does a user-mode variant */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002979 { USB_DEVICE(0x0525, 0xa4a4),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 .driver_info = (unsigned long) &um_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002981 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
2983 /* ... and a user-mode variant that talks iso */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002984 { USB_DEVICE(0x0525, 0xa4a3),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 .driver_info = (unsigned long) &um2_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002986 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
2988#ifdef KEYSPAN_19Qi
2989 /* Keyspan 19qi uses an21xx (original EZ-USB) */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002990 /* this does not coexist with the real Keyspan 19qi driver! */
2991 { USB_DEVICE(0x06cd, 0x010b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 .driver_info = (unsigned long) &ez1_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002993 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994#endif
2995
2996 /*-------------------------------------------------------------*/
2997
2998#ifdef IBOT2
2999 /* iBOT2 makes a nice source of high speed bulk-in data */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02003000 /* this does not coexist with a real iBOT2 driver! */
3001 { USB_DEVICE(0x0b62, 0x0059),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 .driver_info = (unsigned long) &ibot2_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02003003 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004#endif
3005
3006 /*-------------------------------------------------------------*/
3007
3008#ifdef GENERIC
3009 /* module params can specify devices to use for control tests */
3010 { .driver_info = (unsigned long) &generic_info, },
3011#endif
3012
3013 /*-------------------------------------------------------------*/
3014
3015 { }
3016};
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02003017MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
3019static struct usb_driver usbtest_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 .name = "usbtest",
3021 .id_table = id_table,
3022 .probe = usbtest_probe,
Andi Kleenc532b292010-06-01 23:04:41 +02003023 .unlocked_ioctl = usbtest_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 .disconnect = usbtest_disconnect,
David Brownellff7c79e2005-04-22 13:17:00 -07003025 .suspend = usbtest_suspend,
3026 .resume = usbtest_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027};
3028
3029/*-------------------------------------------------------------------------*/
3030
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02003031static int __init usbtest_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
3033#ifdef GENERIC
3034 if (vendor)
David Brownell28ffd792008-04-25 18:51:10 -07003035 pr_debug("params: vend=0x%04x prod=0x%04x\n", vendor, product);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036#endif
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02003037 return usb_register(&usbtest_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038}
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02003039module_init(usbtest_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02003041static void __exit usbtest_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042{
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02003043 usb_deregister(&usbtest_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044}
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02003045module_exit(usbtest_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02003047MODULE_DESCRIPTION("USB Core/HCD Testing Driver");
3048MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049