blob: ad6dd4a1de6cb1687acf3ea72aefb39519217619 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/kernel.h>
2#include <linux/errno.h>
3#include <linux/init.h>
4#include <linux/slab.h>
5#include <linux/mm.h>
6#include <linux/module.h>
7#include <linux/moduleparam.h>
David Hardeman378f0582005-09-17 17:55:31 +10008#include <linux/scatterlist.h>
Matthias Kaehlcke1cfab022007-12-13 16:15:33 -08009#include <linux/mutex.h>
Alan Stern32b36ee2014-06-03 11:11:34 -040010#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/usb.h>
12
Roger Quadrose5e47462013-12-18 15:40:10 +053013#define SIMPLE_IO_TIMEOUT 10000 /* in milliseconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15/*-------------------------------------------------------------------------*/
16
Alan Sternd0b46522013-01-30 16:38:11 -050017static int override_alt = -1;
18module_param_named(alt, override_alt, int, 0644);
19MODULE_PARM_DESC(alt, ">= 0 to override altsetting selection");
20
21/*-------------------------------------------------------------------------*/
22
Martin Fuzzeyfabbf212010-10-01 00:20:42 +020023/* FIXME make these public somewhere; usbdevfs.h? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024struct usbtest_param {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +020025 /* inputs */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 unsigned test_num; /* 0..(TEST_CASES-1) */
27 unsigned iterations;
28 unsigned length;
29 unsigned vary;
30 unsigned sglen;
31
Martin Fuzzeyfabbf212010-10-01 00:20:42 +020032 /* outputs */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 struct timeval duration;
34};
35#define USBTEST_REQUEST _IOWR('U', 100, struct usbtest_param)
36
37/*-------------------------------------------------------------------------*/
38
39#define GENERIC /* let probe() bind using module params */
40
41/* Some devices that can be used for testing will have "real" drivers.
42 * Entries for those need to be enabled here by hand, after disabling
43 * that "real" driver.
44 */
45//#define IBOT2 /* grab iBOT2 webcams */
46//#define KEYSPAN_19Qi /* grab un-renumerated serial adapter */
47
48/*-------------------------------------------------------------------------*/
49
50struct usbtest_info {
51 const char *name;
52 u8 ep_in; /* bulk/intr source */
53 u8 ep_out; /* bulk/intr sink */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +020054 unsigned autoconf:1;
55 unsigned ctrl_out:1;
56 unsigned iso:1; /* try iso in/out */
Amit Virdi457a0952014-08-22 14:36:37 +053057 unsigned intr:1; /* try interrupt in/out */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 int alt;
59};
60
61/* this is accessed only through usbfs ioctl calls.
62 * one ioctl to issue a test ... one lock per device.
63 * tests create other threads if they need them.
64 * urbs and buffers are allocated dynamically,
65 * and data generated deterministically.
66 */
67struct usbtest_dev {
68 struct usb_interface *intf;
69 struct usbtest_info *info;
70 int in_pipe;
71 int out_pipe;
72 int in_iso_pipe;
73 int out_iso_pipe;
Amit Virdi457a0952014-08-22 14:36:37 +053074 int in_int_pipe;
75 int out_int_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct usb_endpoint_descriptor *iso_in, *iso_out;
Amit Virdi457a0952014-08-22 14:36:37 +053077 struct usb_endpoint_descriptor *int_in, *int_out;
Matthias Kaehlcke1cfab022007-12-13 16:15:33 -080078 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80#define TBUF_SIZE 256
81 u8 *buf;
82};
83
Martin Fuzzeyfabbf212010-10-01 00:20:42 +020084static struct usb_device *testdev_to_usbdev(struct usbtest_dev *test)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Martin Fuzzeyfabbf212010-10-01 00:20:42 +020086 return interface_to_usbdev(test->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89/* set up all urbs so they can be used with either bulk or interrupt */
90#define INTERRUPT_RATE 1 /* msec/transfer */
91
David Brownell28ffd792008-04-25 18:51:10 -070092#define ERROR(tdev, fmt, args...) \
93 dev_err(&(tdev)->intf->dev , fmt , ## args)
Arjan van de Venb6c63932008-07-25 01:45:52 -070094#define WARNING(tdev, fmt, args...) \
David Brownell28ffd792008-04-25 18:51:10 -070095 dev_warn(&(tdev)->intf->dev , fmt , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Martin Fuzzey084fb202011-01-16 19:17:11 +010097#define GUARD_BYTE 0xA5
Peter Chen41d3c0b2015-09-01 09:47:58 +080098#define MAX_SGLEN 128
Martin Fuzzey084fb202011-01-16 19:17:11 +010099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*-------------------------------------------------------------------------*/
101
102static int
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200103get_endpoints(struct usbtest_dev *dev, struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 int tmp;
106 struct usb_host_interface *alt;
107 struct usb_host_endpoint *in, *out;
108 struct usb_host_endpoint *iso_in, *iso_out;
Amit Virdi457a0952014-08-22 14:36:37 +0530109 struct usb_host_endpoint *int_in, *int_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 struct usb_device *udev;
111
112 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
113 unsigned ep;
114
115 in = out = NULL;
116 iso_in = iso_out = NULL;
Amit Virdi457a0952014-08-22 14:36:37 +0530117 int_in = int_out = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 alt = intf->altsetting + tmp;
119
Alan Sternd0b46522013-01-30 16:38:11 -0500120 if (override_alt >= 0 &&
121 override_alt != alt->desc.bAlternateSetting)
122 continue;
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 /* take the first altsetting with in-bulk + out-bulk;
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200125 * ignore other endpoints and altsettings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 */
127 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
128 struct usb_host_endpoint *e;
129
130 e = alt->endpoint + ep;
Huang Rui9a37a502013-09-24 00:03:43 +0800131 switch (usb_endpoint_type(&e->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 case USB_ENDPOINT_XFER_BULK:
133 break;
Amit Virdi457a0952014-08-22 14:36:37 +0530134 case USB_ENDPOINT_XFER_INT:
135 if (dev->info->intr)
136 goto try_intr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 case USB_ENDPOINT_XFER_ISOC:
138 if (dev->info->iso)
139 goto try_iso;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200140 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 default:
142 continue;
143 }
Luiz Fernando N. Capitulino4d823dd2006-10-26 13:03:02 -0300144 if (usb_endpoint_dir_in(&e->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (!in)
146 in = e;
147 } else {
148 if (!out)
149 out = e;
150 }
151 continue;
Amit Virdi457a0952014-08-22 14:36:37 +0530152try_intr:
153 if (usb_endpoint_dir_in(&e->desc)) {
154 if (!int_in)
155 int_in = e;
156 } else {
157 if (!int_out)
158 int_out = e;
159 }
160 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161try_iso:
Luiz Fernando N. Capitulino4d823dd2006-10-26 13:03:02 -0300162 if (usb_endpoint_dir_in(&e->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (!iso_in)
164 iso_in = e;
165 } else {
166 if (!iso_out)
167 iso_out = e;
168 }
169 }
Amit Virdi457a0952014-08-22 14:36:37 +0530170 if ((in && out) || iso_in || iso_out || int_in || int_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 goto found;
172 }
173 return -EINVAL;
174
175found:
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200176 udev = testdev_to_usbdev(dev);
Alan Sternd0b46522013-01-30 16:38:11 -0500177 dev->info->alt = alt->desc.bAlternateSetting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (alt->desc.bAlternateSetting != 0) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200179 tmp = usb_set_interface(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 alt->desc.bInterfaceNumber,
181 alt->desc.bAlternateSetting);
182 if (tmp < 0)
183 return tmp;
184 }
185
186 if (in) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200187 dev->in_pipe = usb_rcvbulkpipe(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200189 dev->out_pipe = usb_sndbulkpipe(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
191 }
192 if (iso_in) {
193 dev->iso_in = &iso_in->desc;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200194 dev->in_iso_pipe = usb_rcvisocpipe(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 iso_in->desc.bEndpointAddress
196 & USB_ENDPOINT_NUMBER_MASK);
Ming Lei951fd8e2010-08-02 22:09:17 +0800197 }
198
199 if (iso_out) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 dev->iso_out = &iso_out->desc;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200201 dev->out_iso_pipe = usb_sndisocpipe(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 iso_out->desc.bEndpointAddress
203 & USB_ENDPOINT_NUMBER_MASK);
204 }
Amit Virdi457a0952014-08-22 14:36:37 +0530205
206 if (int_in) {
207 dev->int_in = &int_in->desc;
208 dev->in_int_pipe = usb_rcvintpipe(udev,
209 int_in->desc.bEndpointAddress
210 & USB_ENDPOINT_NUMBER_MASK);
211 }
212
213 if (int_out) {
214 dev->int_out = &int_out->desc;
215 dev->out_int_pipe = usb_sndintpipe(udev,
216 int_out->desc.bEndpointAddress
217 & USB_ENDPOINT_NUMBER_MASK);
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return 0;
220}
221
222/*-------------------------------------------------------------------------*/
223
224/* Support for testing basic non-queued I/O streams.
225 *
226 * These just package urbs as requests that can be easily canceled.
227 * Each urb's data buffer is dynamically allocated; callers can fill
228 * them with non-zero test data (or test for it) when appropriate.
229 */
230
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200231static void simple_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Ming Leicdc97792008-02-24 18:41:47 +0800233 complete(urb->context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Martin Fuzzey084fb202011-01-16 19:17:11 +0100236static struct urb *usbtest_alloc_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 struct usb_device *udev,
238 int pipe,
Martin Fuzzey084fb202011-01-16 19:17:11 +0100239 unsigned long bytes,
240 unsigned transfer_flags,
Amit Virdi457a0952014-08-22 14:36:37 +0530241 unsigned offset,
242 u8 bInterval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 struct urb *urb;
245
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200246 urb = usb_alloc_urb(0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (!urb)
248 return urb;
Amit Virdi457a0952014-08-22 14:36:37 +0530249
250 if (bInterval)
251 usb_fill_int_urb(urb, udev, pipe, NULL, bytes, simple_callback,
252 NULL, bInterval);
253 else
254 usb_fill_bulk_urb(urb, udev, pipe, NULL, bytes, simple_callback,
255 NULL);
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 urb->interval = (udev->speed == USB_SPEED_HIGH)
258 ? (INTERRUPT_RATE << 3)
259 : INTERRUPT_RATE;
Martin Fuzzey084fb202011-01-16 19:17:11 +0100260 urb->transfer_flags = transfer_flags;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200261 if (usb_pipein(pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 urb->transfer_flags |= URB_SHORT_NOT_OK;
Martin Fuzzey084fb202011-01-16 19:17:11 +0100263
264 if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
265 urb->transfer_buffer = usb_alloc_coherent(udev, bytes + offset,
266 GFP_KERNEL, &urb->transfer_dma);
267 else
268 urb->transfer_buffer = kmalloc(bytes + offset, GFP_KERNEL);
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (!urb->transfer_buffer) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200271 usb_free_urb(urb);
Martin Fuzzey084fb202011-01-16 19:17:11 +0100272 return NULL;
273 }
274
275 /* To test unaligned transfers add an offset and fill the
276 unused memory with a guard value */
277 if (offset) {
278 memset(urb->transfer_buffer, GUARD_BYTE, offset);
279 urb->transfer_buffer += offset;
280 if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
281 urb->transfer_dma += offset;
282 }
283
284 /* For inbound transfers use guard byte so that test fails if
285 data not correctly copied */
286 memset(urb->transfer_buffer,
287 usb_pipein(urb->pipe) ? GUARD_BYTE : 0,
288 bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return urb;
290}
291
Martin Fuzzey084fb202011-01-16 19:17:11 +0100292static struct urb *simple_alloc_urb(
293 struct usb_device *udev,
294 int pipe,
Amit Virdi457a0952014-08-22 14:36:37 +0530295 unsigned long bytes,
296 u8 bInterval)
Martin Fuzzey084fb202011-01-16 19:17:11 +0100297{
Amit Virdi457a0952014-08-22 14:36:37 +0530298 return usbtest_alloc_urb(udev, pipe, bytes, URB_NO_TRANSFER_DMA_MAP, 0,
299 bInterval);
Martin Fuzzey084fb202011-01-16 19:17:11 +0100300}
301
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200302static unsigned pattern;
Vikram Pandita7f4e9852009-11-09 21:24:32 -0600303static unsigned mod_pattern;
304module_param_named(pattern, mod_pattern, uint, S_IRUGO | S_IWUSR);
305MODULE_PARM_DESC(mod_pattern, "i/o pattern (0 == zeroes)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Alan Sternb9a6e8e2015-09-01 09:48:01 +0800307static unsigned get_maxpacket(struct usb_device *udev, int pipe)
308{
309 struct usb_host_endpoint *ep;
310
311 ep = usb_pipe_endpoint(udev, pipe);
312 return le16_to_cpup(&ep->desc.wMaxPacketSize);
313}
314
315static void simple_fill_buf(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 unsigned i;
318 u8 *buf = urb->transfer_buffer;
319 unsigned len = urb->transfer_buffer_length;
Alan Sternb9a6e8e2015-09-01 09:48:01 +0800320 unsigned maxpacket;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 switch (pattern) {
323 default:
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200324 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 case 0:
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200326 memset(buf, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 break;
328 case 1: /* mod63 */
Alan Sternb9a6e8e2015-09-01 09:48:01 +0800329 maxpacket = get_maxpacket(urb->dev, urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 for (i = 0; i < len; i++)
Alan Sternb9a6e8e2015-09-01 09:48:01 +0800331 *buf++ = (u8) ((i % maxpacket) % 63);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 break;
333 }
334}
335
Greg Dietsche304b0b52011-05-08 22:51:43 -0500336static inline unsigned long buffer_offset(void *buf)
Martin Fuzzey084fb202011-01-16 19:17:11 +0100337{
Greg Dietsche304b0b52011-05-08 22:51:43 -0500338 return (unsigned long)buf & (ARCH_KMALLOC_MINALIGN - 1);
Martin Fuzzey084fb202011-01-16 19:17:11 +0100339}
340
341static int check_guard_bytes(struct usbtest_dev *tdev, struct urb *urb)
342{
343 u8 *buf = urb->transfer_buffer;
344 u8 *guard = buf - buffer_offset(buf);
345 unsigned i;
346
347 for (i = 0; guard < buf; i++, guard++) {
348 if (*guard != GUARD_BYTE) {
349 ERROR(tdev, "guard byte[%d] %d (not %d)\n",
350 i, *guard, GUARD_BYTE);
351 return -EINVAL;
352 }
353 }
354 return 0;
355}
356
357static int simple_check_buf(struct usbtest_dev *tdev, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 unsigned i;
360 u8 expected;
361 u8 *buf = urb->transfer_buffer;
362 unsigned len = urb->actual_length;
Alan Sternb9a6e8e2015-09-01 09:48:01 +0800363 unsigned maxpacket = get_maxpacket(urb->dev, urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Martin Fuzzey084fb202011-01-16 19:17:11 +0100365 int ret = check_guard_bytes(tdev, urb);
366 if (ret)
367 return ret;
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 for (i = 0; i < len; i++, buf++) {
370 switch (pattern) {
371 /* all-zeroes has no synchronization issues */
372 case 0:
373 expected = 0;
374 break;
375 /* mod63 stays in sync with short-terminated transfers,
376 * or otherwise when host and gadget agree on how large
377 * each usb transfer request should be. resync is done
378 * with set_interface or set_config.
379 */
380 case 1: /* mod63 */
Alan Sternb9a6e8e2015-09-01 09:48:01 +0800381 expected = (i % maxpacket) % 63;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 break;
383 /* always fail unsupported patterns */
384 default:
385 expected = !*buf;
386 break;
387 }
388 if (*buf == expected)
389 continue;
David Brownell28ffd792008-04-25 18:51:10 -0700390 ERROR(tdev, "buf[%d] = %d (not %d)\n", i, *buf, expected);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return -EINVAL;
392 }
393 return 0;
394}
395
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200396static void simple_free_urb(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Greg Dietsche304b0b52011-05-08 22:51:43 -0500398 unsigned long offset = buffer_offset(urb->transfer_buffer);
Martin Fuzzey084fb202011-01-16 19:17:11 +0100399
400 if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
401 usb_free_coherent(
402 urb->dev,
403 urb->transfer_buffer_length + offset,
404 urb->transfer_buffer - offset,
405 urb->transfer_dma - offset);
406 else
407 kfree(urb->transfer_buffer - offset);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200408 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200411static int simple_io(
David Brownell28ffd792008-04-25 18:51:10 -0700412 struct usbtest_dev *tdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 struct urb *urb,
414 int iterations,
415 int vary,
416 int expected,
417 const char *label
418)
419{
420 struct usb_device *udev = urb->dev;
421 int max = urb->transfer_buffer_length;
422 struct completion completion;
423 int retval = 0;
Roger Quadrose5e47462013-12-18 15:40:10 +0530424 unsigned long expire;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 urb->context = &completion;
427 while (retval == 0 && iterations-- > 0) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200428 init_completion(&completion);
Sebastian Andrzej Siewior7c79d092011-08-23 10:44:54 +0200429 if (usb_pipeout(urb->pipe)) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200430 simple_fill_buf(urb);
Sebastian Andrzej Siewior7c79d092011-08-23 10:44:54 +0200431 urb->transfer_flags |= URB_ZERO_PACKET;
432 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200433 retval = usb_submit_urb(urb, GFP_KERNEL);
434 if (retval != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 break;
436
Roger Quadrose5e47462013-12-18 15:40:10 +0530437 expire = msecs_to_jiffies(SIMPLE_IO_TIMEOUT);
438 if (!wait_for_completion_timeout(&completion, expire)) {
439 usb_kill_urb(urb);
440 retval = (urb->status == -ENOENT ?
441 -ETIMEDOUT : urb->status);
442 } else {
443 retval = urb->status;
444 }
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 urb->dev = udev;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200447 if (retval == 0 && usb_pipein(urb->pipe))
David Brownell28ffd792008-04-25 18:51:10 -0700448 retval = simple_check_buf(tdev, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 if (vary) {
451 int len = urb->transfer_buffer_length;
452
453 len += vary;
454 len %= max;
455 if (len == 0)
456 len = (vary < max) ? vary : max;
457 urb->transfer_buffer_length = len;
458 }
459
460 /* FIXME if endpoint halted, clear halt (and log) */
461 }
462 urb->transfer_buffer_length = max;
463
464 if (expected != retval)
David Brownell28ffd792008-04-25 18:51:10 -0700465 dev_err(&udev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 "%s failed, iterations left %d, status %d (not %d)\n",
467 label, iterations, retval, expected);
468 return retval;
469}
470
471
472/*-------------------------------------------------------------------------*/
473
474/* We use scatterlist primitives to test queued I/O.
475 * Yes, this also tests the scatterlist primitives.
476 */
477
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200478static void free_sglist(struct scatterlist *sg, int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 unsigned i;
David Brownell28ffd792008-04-25 18:51:10 -0700481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (!sg)
483 return;
484 for (i = 0; i < nents; i++) {
Jens Axboe45711f12007-10-22 21:19:53 +0200485 if (!sg_page(&sg[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 continue;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200487 kfree(sg_virt(&sg[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200489 kfree(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492static struct scatterlist *
Alan Sternb9a6e8e2015-09-01 09:48:01 +0800493alloc_sglist(int nents, int max, int vary, struct usbtest_dev *dev, int pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct scatterlist *sg;
496 unsigned i;
497 unsigned size = max;
Alan Sternb9a6e8e2015-09-01 09:48:01 +0800498 unsigned maxpacket =
499 get_maxpacket(interface_to_usbdev(dev->intf), pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Dan Carpenter564e6982012-11-17 18:06:11 +0300501 if (max == 0)
502 return NULL;
503
Huang Ruif55055b2013-10-21 23:15:30 +0800504 sg = kmalloc_array(nents, sizeof(*sg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (!sg)
506 return NULL;
Alan Stern4756feb2008-03-27 10:15:22 -0400507 sg_init_table(sg, nents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 for (i = 0; i < nents; i++) {
510 char *buf;
David Brownell8b524902006-04-02 10:20:15 -0800511 unsigned j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200513 buf = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (!buf) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200515 free_sglist(sg, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return NULL;
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 /* kmalloc pages are always physically contiguous! */
Alan Stern4756feb2008-03-27 10:15:22 -0400520 sg_set_buf(&sg[i], buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
David Brownell8b524902006-04-02 10:20:15 -0800522 switch (pattern) {
523 case 0:
524 /* already zeroed */
525 break;
526 case 1:
527 for (j = 0; j < size; j++)
Alan Sternb9a6e8e2015-09-01 09:48:01 +0800528 *buf++ = (u8) ((j % maxpacket) % 63);
David Brownell8b524902006-04-02 10:20:15 -0800529 break;
530 }
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (vary) {
533 size += vary;
534 size %= max;
535 if (size == 0)
536 size = (vary < max) ? vary : max;
537 }
538 }
539
540 return sg;
541}
542
Alan Stern32b36ee2014-06-03 11:11:34 -0400543static void sg_timeout(unsigned long _req)
544{
545 struct usb_sg_request *req = (struct usb_sg_request *) _req;
546
547 req->status = -ETIMEDOUT;
548 usb_sg_cancel(req);
549}
550
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200551static int perform_sglist(
David Brownell28ffd792008-04-25 18:51:10 -0700552 struct usbtest_dev *tdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 unsigned iterations,
554 int pipe,
555 struct usb_sg_request *req,
556 struct scatterlist *sg,
557 int nents
558)
559{
David Brownell28ffd792008-04-25 18:51:10 -0700560 struct usb_device *udev = testdev_to_usbdev(tdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 int retval = 0;
Alan Stern32b36ee2014-06-03 11:11:34 -0400562 struct timer_list sg_timer;
563
564 setup_timer_on_stack(&sg_timer, sg_timeout, (unsigned long) req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 while (retval == 0 && iterations-- > 0) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200567 retval = usb_sg_init(req, udev, pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 (udev->speed == USB_SPEED_HIGH)
569 ? (INTERRUPT_RATE << 3)
570 : INTERRUPT_RATE,
Christoph Lametere94b1762006-12-06 20:33:17 -0800571 sg, nents, 0, GFP_KERNEL);
David Brownell28ffd792008-04-25 18:51:10 -0700572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (retval)
574 break;
Alan Stern32b36ee2014-06-03 11:11:34 -0400575 mod_timer(&sg_timer, jiffies +
576 msecs_to_jiffies(SIMPLE_IO_TIMEOUT));
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200577 usb_sg_wait(req);
Alan Stern32b36ee2014-06-03 11:11:34 -0400578 del_timer_sync(&sg_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 retval = req->status;
580
David Brownell8b524902006-04-02 10:20:15 -0800581 /* FIXME check resulting data pattern */
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /* FIXME if endpoint halted, clear halt (and log) */
584 }
585
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200586 /* FIXME for unlink or fault handling tests, don't report
587 * failure if retval is as we expected ...
588 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (retval)
David Brownell28ffd792008-04-25 18:51:10 -0700590 ERROR(tdev, "perform_sglist failed, "
591 "iterations left %d, status %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 iterations, retval);
593 return retval;
594}
595
596
597/*-------------------------------------------------------------------------*/
598
599/* unqueued control message testing
600 *
601 * there's a nice set of device functional requirements in chapter 9 of the
602 * usb 2.0 spec, which we can apply to ANY device, even ones that don't use
603 * special test firmware.
604 *
605 * we know the device is configured (or suspended) by the time it's visible
606 * through usbfs. we can't change that, so we won't test enumeration (which
607 * worked 'well enough' to get here, this time), power management (ditto),
608 * or remote wakeup (which needs human interaction).
609 */
610
611static unsigned realworld = 1;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200612module_param(realworld, uint, 0);
613MODULE_PARM_DESC(realworld, "clear to demand stricter spec compliance");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200615static int get_altsetting(struct usbtest_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 struct usb_interface *iface = dev->intf;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200618 struct usb_device *udev = interface_to_usbdev(iface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 int retval;
620
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200621 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 USB_REQ_GET_INTERFACE, USB_DIR_IN|USB_RECIP_INTERFACE,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200623 0, iface->altsetting[0].desc.bInterfaceNumber,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 dev->buf, 1, USB_CTRL_GET_TIMEOUT);
625 switch (retval) {
626 case 1:
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200627 return dev->buf[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 case 0:
629 retval = -ERANGE;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200630 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 default:
632 return retval;
633 }
634}
635
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200636static int set_altsetting(struct usbtest_dev *dev, int alternate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 struct usb_interface *iface = dev->intf;
639 struct usb_device *udev;
640
641 if (alternate < 0 || alternate >= 256)
642 return -EINVAL;
643
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200644 udev = interface_to_usbdev(iface);
645 return usb_set_interface(udev,
646 iface->altsetting[0].desc.bInterfaceNumber,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 alternate);
648}
649
David Brownell28ffd792008-04-25 18:51:10 -0700650static int is_good_config(struct usbtest_dev *tdev, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 struct usb_config_descriptor *config;
David Brownell28ffd792008-04-25 18:51:10 -0700653
Huang Ruif55055b2013-10-21 23:15:30 +0800654 if (len < sizeof(*config))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return 0;
David Brownell28ffd792008-04-25 18:51:10 -0700656 config = (struct usb_config_descriptor *) tdev->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 switch (config->bDescriptorType) {
659 case USB_DT_CONFIG:
660 case USB_DT_OTHER_SPEED_CONFIG:
661 if (config->bLength != 9) {
David Brownell28ffd792008-04-25 18:51:10 -0700662 ERROR(tdev, "bogus config descriptor length\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return 0;
664 }
665 /* this bit 'must be 1' but often isn't */
666 if (!realworld && !(config->bmAttributes & 0x80)) {
David Brownell28ffd792008-04-25 18:51:10 -0700667 ERROR(tdev, "high bit of config attributes not set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return 0;
669 }
670 if (config->bmAttributes & 0x1f) { /* reserved == 0 */
David Brownell28ffd792008-04-25 18:51:10 -0700671 ERROR(tdev, "reserved config bits set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return 0;
673 }
674 break;
675 default:
676 return 0;
677 }
678
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200679 if (le16_to_cpu(config->wTotalLength) == len) /* read it all */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return 1;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200681 if (le16_to_cpu(config->wTotalLength) >= TBUF_SIZE) /* max partial read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return 1;
David Brownell28ffd792008-04-25 18:51:10 -0700683 ERROR(tdev, "bogus config descriptor read size\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return 0;
685}
686
Huang Rui82f92672013-10-30 11:27:38 +0800687static int is_good_ext(struct usbtest_dev *tdev, u8 *buf)
688{
689 struct usb_ext_cap_descriptor *ext;
690 u32 attr;
691
692 ext = (struct usb_ext_cap_descriptor *) buf;
693
694 if (ext->bLength != USB_DT_USB_EXT_CAP_SIZE) {
695 ERROR(tdev, "bogus usb 2.0 extension descriptor length\n");
696 return 0;
697 }
698
699 attr = le32_to_cpu(ext->bmAttributes);
Huang Rui875bc232013-11-13 22:35:14 +0800700 /* bits[1:15] is used and others are reserved */
701 if (attr & ~0xfffe) { /* reserved == 0 */
Huang Rui82f92672013-10-30 11:27:38 +0800702 ERROR(tdev, "reserved bits set\n");
703 return 0;
704 }
705
706 return 1;
707}
708
Huang Ruib8fef792013-10-30 11:27:39 +0800709static int is_good_ss_cap(struct usbtest_dev *tdev, u8 *buf)
710{
711 struct usb_ss_cap_descriptor *ss;
712
713 ss = (struct usb_ss_cap_descriptor *) buf;
714
715 if (ss->bLength != USB_DT_USB_SS_CAP_SIZE) {
716 ERROR(tdev, "bogus superspeed device capability descriptor length\n");
717 return 0;
718 }
719
720 /*
721 * only bit[1] of bmAttributes is used for LTM and others are
722 * reserved
723 */
724 if (ss->bmAttributes & ~0x02) { /* reserved == 0 */
725 ERROR(tdev, "reserved bits set in bmAttributes\n");
726 return 0;
727 }
728
729 /* bits[0:3] of wSpeedSupported is used and others are reserved */
730 if (le16_to_cpu(ss->wSpeedSupported) & ~0x0f) { /* reserved == 0 */
731 ERROR(tdev, "reserved bits set in wSpeedSupported\n");
732 return 0;
733 }
734
735 return 1;
736}
737
Huang Rui8e292172013-10-30 11:27:40 +0800738static int is_good_con_id(struct usbtest_dev *tdev, u8 *buf)
739{
740 struct usb_ss_container_id_descriptor *con_id;
741
742 con_id = (struct usb_ss_container_id_descriptor *) buf;
743
744 if (con_id->bLength != USB_DT_USB_SS_CONTN_ID_SIZE) {
745 ERROR(tdev, "bogus container id descriptor length\n");
746 return 0;
747 }
748
749 if (con_id->bReserved) { /* reserved == 0 */
750 ERROR(tdev, "reserved bits set\n");
751 return 0;
752 }
753
754 return 1;
755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757/* sanity test for standard requests working with usb_control_mesg() and some
758 * of the utility functions which use it.
759 *
760 * this doesn't test how endpoint halts behave or data toggles get set, since
761 * we won't do I/O to bulk/interrupt endpoints here (which is how to change
762 * halt or toggle). toggle testing is impractical without support from hcds.
763 *
764 * this avoids failing devices linux would normally work with, by not testing
765 * config/altsetting operations for devices that only support their defaults.
766 * such devices rarely support those needless operations.
767 *
768 * NOTE that since this is a sanity test, it's not examining boundary cases
769 * to see if usbcore, hcd, and device all behave right. such testing would
770 * involve varied read sizes and other operation sequences.
771 */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200772static int ch9_postconfig(struct usbtest_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 struct usb_interface *iface = dev->intf;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200775 struct usb_device *udev = interface_to_usbdev(iface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 int i, alt, retval;
777
778 /* [9.2.3] if there's more than one altsetting, we need to be able to
779 * set and get each one. mostly trusts the descriptors from usbcore.
780 */
781 for (i = 0; i < iface->num_altsetting; i++) {
782
783 /* 9.2.3 constrains the range here */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200784 alt = iface->altsetting[i].desc.bAlternateSetting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (alt < 0 || alt >= iface->num_altsetting) {
David Brownell28ffd792008-04-25 18:51:10 -0700786 dev_err(&iface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 "invalid alt [%d].bAltSetting = %d\n",
788 i, alt);
789 }
790
791 /* [real world] get/set unimplemented if there's only one */
792 if (realworld && iface->num_altsetting == 1)
793 continue;
794
795 /* [9.4.10] set_interface */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200796 retval = set_altsetting(dev, alt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (retval) {
David Brownell28ffd792008-04-25 18:51:10 -0700798 dev_err(&iface->dev, "can't set_interface = %d, %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 alt, retval);
800 return retval;
801 }
802
803 /* [9.4.4] get_interface always works */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200804 retval = get_altsetting(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (retval != alt) {
David Brownell28ffd792008-04-25 18:51:10 -0700806 dev_err(&iface->dev, "get alt should be %d, was %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 alt, retval);
808 return (retval < 0) ? retval : -EDOM;
809 }
810
811 }
812
813 /* [real world] get_config unimplemented if there's only one */
814 if (!realworld || udev->descriptor.bNumConfigurations != 1) {
815 int expected = udev->actconfig->desc.bConfigurationValue;
816
817 /* [9.4.2] get_configuration always works
818 * ... although some cheap devices (like one TI Hub I've got)
819 * won't return config descriptors except before set_config.
820 */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200821 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 USB_REQ_GET_CONFIGURATION,
823 USB_DIR_IN | USB_RECIP_DEVICE,
824 0, 0, dev->buf, 1, USB_CTRL_GET_TIMEOUT);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200825 if (retval != 1 || dev->buf[0] != expected) {
David Brownell28ffd792008-04-25 18:51:10 -0700826 dev_err(&iface->dev, "get config --> %d %d (1 %d)\n",
David Brownellff7c79e2005-04-22 13:17:00 -0700827 retval, dev->buf[0], expected);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return (retval < 0) ? retval : -EDOM;
829 }
830 }
831
832 /* there's always [9.4.3] a device descriptor [9.6.1] */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200833 retval = usb_get_descriptor(udev, USB_DT_DEVICE, 0,
Huang Ruif55055b2013-10-21 23:15:30 +0800834 dev->buf, sizeof(udev->descriptor));
835 if (retval != sizeof(udev->descriptor)) {
David Brownell28ffd792008-04-25 18:51:10 -0700836 dev_err(&iface->dev, "dev descriptor --> %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return (retval < 0) ? retval : -EDOM;
838 }
839
Huang Rui9d3bd762013-10-28 23:31:32 +0800840 /*
841 * there's always [9.4.3] a bos device descriptor [9.6.2] in USB
842 * 3.0 spec
843 */
Huang Ruif6250992013-11-13 22:35:13 +0800844 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0210) {
Huang Rui82f92672013-10-30 11:27:38 +0800845 struct usb_bos_descriptor *bos = NULL;
846 struct usb_dev_cap_header *header = NULL;
847 unsigned total, num, length;
848 u8 *buf;
849
Huang Rui9d3bd762013-10-28 23:31:32 +0800850 retval = usb_get_descriptor(udev, USB_DT_BOS, 0, dev->buf,
851 sizeof(*udev->bos->desc));
852 if (retval != sizeof(*udev->bos->desc)) {
853 dev_err(&iface->dev, "bos descriptor --> %d\n", retval);
854 return (retval < 0) ? retval : -EDOM;
855 }
Huang Rui82f92672013-10-30 11:27:38 +0800856
857 bos = (struct usb_bos_descriptor *)dev->buf;
858 total = le16_to_cpu(bos->wTotalLength);
859 num = bos->bNumDeviceCaps;
860
861 if (total > TBUF_SIZE)
862 total = TBUF_SIZE;
863
864 /*
865 * get generic device-level capability descriptors [9.6.2]
866 * in USB 3.0 spec
867 */
868 retval = usb_get_descriptor(udev, USB_DT_BOS, 0, dev->buf,
869 total);
870 if (retval != total) {
871 dev_err(&iface->dev, "bos descriptor set --> %d\n",
872 retval);
873 return (retval < 0) ? retval : -EDOM;
874 }
875
876 length = sizeof(*udev->bos->desc);
877 buf = dev->buf;
878 for (i = 0; i < num; i++) {
879 buf += length;
880 if (buf + sizeof(struct usb_dev_cap_header) >
881 dev->buf + total)
882 break;
883
884 header = (struct usb_dev_cap_header *)buf;
885 length = header->bLength;
886
887 if (header->bDescriptorType !=
888 USB_DT_DEVICE_CAPABILITY) {
889 dev_warn(&udev->dev, "not device capability descriptor, skip\n");
890 continue;
891 }
892
893 switch (header->bDevCapabilityType) {
894 case USB_CAP_TYPE_EXT:
895 if (buf + USB_DT_USB_EXT_CAP_SIZE >
896 dev->buf + total ||
897 !is_good_ext(dev, buf)) {
898 dev_err(&iface->dev, "bogus usb 2.0 extension descriptor\n");
899 return -EDOM;
900 }
901 break;
Huang Ruib8fef792013-10-30 11:27:39 +0800902 case USB_SS_CAP_TYPE:
903 if (buf + USB_DT_USB_SS_CAP_SIZE >
904 dev->buf + total ||
905 !is_good_ss_cap(dev, buf)) {
906 dev_err(&iface->dev, "bogus superspeed device capability descriptor\n");
907 return -EDOM;
908 }
909 break;
Huang Rui8e292172013-10-30 11:27:40 +0800910 case CONTAINER_ID_TYPE:
911 if (buf + USB_DT_USB_SS_CONTN_ID_SIZE >
912 dev->buf + total ||
913 !is_good_con_id(dev, buf)) {
914 dev_err(&iface->dev, "bogus container id descriptor\n");
915 return -EDOM;
916 }
917 break;
Huang Rui82f92672013-10-30 11:27:38 +0800918 default:
919 break;
920 }
921 }
Huang Rui9d3bd762013-10-28 23:31:32 +0800922 }
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 /* there's always [9.4.3] at least one config descriptor [9.6.3] */
925 for (i = 0; i < udev->descriptor.bNumConfigurations; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200926 retval = usb_get_descriptor(udev, USB_DT_CONFIG, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 dev->buf, TBUF_SIZE);
David Brownell28ffd792008-04-25 18:51:10 -0700928 if (!is_good_config(dev, retval)) {
929 dev_err(&iface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 "config [%d] descriptor --> %d\n",
931 i, retval);
932 return (retval < 0) ? retval : -EDOM;
933 }
934
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200935 /* FIXME cross-checking udev->config[i] to make sure usbcore
936 * parsed it right (etc) would be good testing paranoia
937 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939
940 /* and sometimes [9.2.6.6] speed dependent descriptors */
941 if (le16_to_cpu(udev->descriptor.bcdUSB) == 0x0200) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200942 struct usb_qualifier_descriptor *d = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 /* device qualifier [9.6.2] */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200945 retval = usb_get_descriptor(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 USB_DT_DEVICE_QUALIFIER, 0, dev->buf,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200947 sizeof(struct usb_qualifier_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (retval == -EPIPE) {
949 if (udev->speed == USB_SPEED_HIGH) {
David Brownell28ffd792008-04-25 18:51:10 -0700950 dev_err(&iface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 "hs dev qualifier --> %d\n",
952 retval);
953 return (retval < 0) ? retval : -EDOM;
954 }
955 /* usb2.0 but not high-speed capable; fine */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200956 } else if (retval != sizeof(struct usb_qualifier_descriptor)) {
David Brownell28ffd792008-04-25 18:51:10 -0700957 dev_err(&iface->dev, "dev qualifier --> %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return (retval < 0) ? retval : -EDOM;
959 } else
960 d = (struct usb_qualifier_descriptor *) dev->buf;
961
962 /* might not have [9.6.2] any other-speed configs [9.6.4] */
963 if (d) {
964 unsigned max = d->bNumConfigurations;
965 for (i = 0; i < max; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200966 retval = usb_get_descriptor(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 USB_DT_OTHER_SPEED_CONFIG, i,
968 dev->buf, TBUF_SIZE);
David Brownell28ffd792008-04-25 18:51:10 -0700969 if (!is_good_config(dev, retval)) {
970 dev_err(&iface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 "other speed config --> %d\n",
972 retval);
973 return (retval < 0) ? retval : -EDOM;
974 }
975 }
976 }
977 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200978 /* FIXME fetch strings from at least the device descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 /* [9.4.5] get_status always works */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200981 retval = usb_get_status(udev, USB_RECIP_DEVICE, 0, dev->buf);
Alan Stern15b73362013-07-30 15:35:40 -0400982 if (retval) {
David Brownell28ffd792008-04-25 18:51:10 -0700983 dev_err(&iface->dev, "get dev status --> %d\n", retval);
Alan Stern15b73362013-07-30 15:35:40 -0400984 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200987 /* FIXME configuration.bmAttributes says if we could try to set/clear
988 * the device's remote wakeup feature ... if we can, test that here
989 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200991 retval = usb_get_status(udev, USB_RECIP_INTERFACE,
992 iface->altsetting[0].desc.bInterfaceNumber, dev->buf);
Alan Stern15b73362013-07-30 15:35:40 -0400993 if (retval) {
David Brownell28ffd792008-04-25 18:51:10 -0700994 dev_err(&iface->dev, "get interface status --> %d\n", retval);
Alan Stern15b73362013-07-30 15:35:40 -0400995 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200997 /* FIXME get status for each endpoint in the interface */
David Brownell28ffd792008-04-25 18:51:10 -0700998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return 0;
1000}
1001
1002/*-------------------------------------------------------------------------*/
1003
1004/* use ch9 requests to test whether:
1005 * (a) queues work for control, keeping N subtests queued and
1006 * active (auto-resubmit) for M loops through the queue.
1007 * (b) protocol stalls (control-only) will autorecover.
1008 * it's not like bulk/intr; no halt clearing.
1009 * (c) short control reads are reported and handled.
1010 * (d) queues are always processed in-order
1011 */
1012
1013struct ctrl_ctx {
1014 spinlock_t lock;
1015 struct usbtest_dev *dev;
1016 struct completion complete;
1017 unsigned count;
1018 unsigned pending;
1019 int status;
1020 struct urb **urb;
1021 struct usbtest_param *param;
1022 int last;
1023};
1024
Huang Ruic952a8b2013-11-04 21:11:53 +08001025#define NUM_SUBCASES 16 /* how many test subcases here? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027struct subcase {
1028 struct usb_ctrlrequest setup;
1029 int number;
1030 int expected;
1031};
1032
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001033static void ctrl_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
1035 struct ctrl_ctx *ctx = urb->context;
1036 struct usb_ctrlrequest *reqp;
1037 struct subcase *subcase;
1038 int status = urb->status;
1039
1040 reqp = (struct usb_ctrlrequest *)urb->setup_packet;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001041 subcase = container_of(reqp, struct subcase, setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001043 spin_lock(&ctx->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 ctx->count--;
1045 ctx->pending--;
1046
1047 /* queue must transfer and complete in fifo order, unless
1048 * usb_unlink_urb() is used to unlink something not at the
1049 * physical queue head (not tested).
1050 */
1051 if (subcase->number > 0) {
1052 if ((subcase->number - ctx->last) != 1) {
David Brownell28ffd792008-04-25 18:51:10 -07001053 ERROR(ctx->dev,
1054 "subcase %d completed out of order, last %d\n",
1055 subcase->number, ctx->last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 status = -EDOM;
1057 ctx->last = subcase->number;
1058 goto error;
1059 }
1060 }
1061 ctx->last = subcase->number;
1062
1063 /* succeed or fault in only one way? */
1064 if (status == subcase->expected)
1065 status = 0;
1066
1067 /* async unlink for cleanup? */
1068 else if (status != -ECONNRESET) {
1069
1070 /* some faults are allowed, not required */
1071 if (subcase->expected > 0 && (
Greg Kroah-Hartman59d99782007-07-18 10:58:02 -07001072 ((status == -subcase->expected /* happened */
1073 || status == 0)))) /* didn't */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 status = 0;
1075 /* sometimes more than one fault is allowed */
1076 else if (subcase->number == 12 && status == -EPIPE)
1077 status = 0;
1078 else
David Brownell28ffd792008-04-25 18:51:10 -07001079 ERROR(ctx->dev, "subtest %d error, status %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 subcase->number, status);
1081 }
1082
1083 /* unexpected status codes mean errors; ideally, in hardware */
1084 if (status) {
1085error:
1086 if (ctx->status == 0) {
1087 int i;
1088
1089 ctx->status = status;
David Brownell28ffd792008-04-25 18:51:10 -07001090 ERROR(ctx->dev, "control queue %02x.%02x, err %d, "
1091 "%d left, subcase %d, len %d/%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 reqp->bRequestType, reqp->bRequest,
David Brownell28ffd792008-04-25 18:51:10 -07001093 status, ctx->count, subcase->number,
1094 urb->actual_length,
1095 urb->transfer_buffer_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 /* FIXME this "unlink everything" exit route should
1098 * be a separate test case.
1099 */
1100
1101 /* unlink whatever's still pending */
1102 for (i = 1; i < ctx->param->sglen; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001103 struct urb *u = ctx->urb[
1104 (i + subcase->number)
1105 % ctx->param->sglen];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 if (u == urb || !u->dev)
1108 continue;
Franck Bui-Huucaa2a122006-05-15 19:23:53 +02001109 spin_unlock(&ctx->lock);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001110 status = usb_unlink_urb(u);
Franck Bui-Huucaa2a122006-05-15 19:23:53 +02001111 spin_lock(&ctx->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 switch (status) {
1113 case -EINPROGRESS:
1114 case -EBUSY:
1115 case -EIDRM:
1116 continue;
1117 default:
David Brownell28ffd792008-04-25 18:51:10 -07001118 ERROR(ctx->dev, "urb unlink --> %d\n",
1119 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121 }
1122 status = ctx->status;
1123 }
1124 }
1125
1126 /* resubmit if we need to, else mark this as done */
1127 if ((status == 0) && (ctx->pending < ctx->count)) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001128 status = usb_submit_urb(urb, GFP_ATOMIC);
1129 if (status != 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001130 ERROR(ctx->dev,
1131 "can't resubmit ctrl %02x.%02x, err %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 reqp->bRequestType, reqp->bRequest, status);
1133 urb->dev = NULL;
1134 } else
1135 ctx->pending++;
1136 } else
1137 urb->dev = NULL;
David Brownell28ffd792008-04-25 18:51:10 -07001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 /* signal completion when nothing's queued */
1140 if (ctx->pending == 0)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001141 complete(&ctx->complete);
1142 spin_unlock(&ctx->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
1145static int
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001146test_ctrl_queue(struct usbtest_dev *dev, struct usbtest_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001148 struct usb_device *udev = testdev_to_usbdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 struct urb **urb;
1150 struct ctrl_ctx context;
1151 int i;
1152
Xi Wange65cdfa2012-04-09 15:48:55 -04001153 if (param->sglen == 0 || param->iterations > UINT_MAX / param->sglen)
1154 return -EOPNOTSUPP;
1155
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001156 spin_lock_init(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 context.dev = dev;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001158 init_completion(&context.complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 context.count = param->sglen * param->iterations;
1160 context.pending = 0;
1161 context.status = -ENOMEM;
1162 context.param = param;
1163 context.last = -1;
1164
1165 /* allocate and init the urbs we'll queue.
1166 * as with bulk/intr sglists, sglen is the queue depth; it also
1167 * controls which subtests run (more tests than sglen) or rerun.
1168 */
Christoph Lametere94b1762006-12-06 20:33:17 -08001169 urb = kcalloc(param->sglen, sizeof(struct urb *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (!urb)
1171 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 for (i = 0; i < param->sglen; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001173 int pipe = usb_rcvctrlpipe(udev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 unsigned len;
1175 struct urb *u;
1176 struct usb_ctrlrequest req;
1177 struct subcase *reqp;
Marcin Slusarz6def7552008-05-12 20:17:25 +02001178
1179 /* sign of this variable means:
1180 * -: tested code must return this (negative) error code
1181 * +: tested code may return this (negative too) error code
1182 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 int expected = 0;
1184
1185 /* requests here are mostly expected to succeed on any
1186 * device, but some are chosen to trigger protocol stalls
1187 * or short reads.
1188 */
Huang Ruif55055b2013-10-21 23:15:30 +08001189 memset(&req, 0, sizeof(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 req.bRequest = USB_REQ_GET_DESCRIPTOR;
1191 req.bRequestType = USB_DIR_IN|USB_RECIP_DEVICE;
1192
1193 switch (i % NUM_SUBCASES) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001194 case 0: /* get device descriptor */
1195 req.wValue = cpu_to_le16(USB_DT_DEVICE << 8);
1196 len = sizeof(struct usb_device_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001198 case 1: /* get first config descriptor (only) */
1199 req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
1200 len = sizeof(struct usb_config_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001202 case 2: /* get altsetting (OFTEN STALLS) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 req.bRequest = USB_REQ_GET_INTERFACE;
1204 req.bRequestType = USB_DIR_IN|USB_RECIP_INTERFACE;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001205 /* index = 0 means first interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 len = 1;
1207 expected = EPIPE;
1208 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001209 case 3: /* get interface status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 req.bRequest = USB_REQ_GET_STATUS;
1211 req.bRequestType = USB_DIR_IN|USB_RECIP_INTERFACE;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001212 /* interface 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 len = 2;
1214 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001215 case 4: /* get device status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 req.bRequest = USB_REQ_GET_STATUS;
1217 req.bRequestType = USB_DIR_IN|USB_RECIP_DEVICE;
1218 len = 2;
1219 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001220 case 5: /* get device qualifier (MAY STALL) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 req.wValue = cpu_to_le16 (USB_DT_DEVICE_QUALIFIER << 8);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001222 len = sizeof(struct usb_qualifier_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (udev->speed != USB_SPEED_HIGH)
1224 expected = EPIPE;
1225 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001226 case 6: /* get first config descriptor, plus interface */
1227 req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
1228 len = sizeof(struct usb_config_descriptor);
1229 len += sizeof(struct usb_interface_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001231 case 7: /* get interface descriptor (ALWAYS STALLS) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 req.wValue = cpu_to_le16 (USB_DT_INTERFACE << 8);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001233 /* interface == 0 */
1234 len = sizeof(struct usb_interface_descriptor);
David Brownell28ffd792008-04-25 18:51:10 -07001235 expected = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001237 /* NOTE: two consecutive stalls in the queue here.
1238 * that tests fault recovery a bit more aggressively. */
1239 case 8: /* clear endpoint halt (MAY STALL) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 req.bRequest = USB_REQ_CLEAR_FEATURE;
1241 req.bRequestType = USB_RECIP_ENDPOINT;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001242 /* wValue 0 == ep halt */
1243 /* wIndex 0 == ep0 (shouldn't halt!) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 len = 0;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001245 pipe = usb_sndctrlpipe(udev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 expected = EPIPE;
1247 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001248 case 9: /* get endpoint status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 req.bRequest = USB_REQ_GET_STATUS;
1250 req.bRequestType = USB_DIR_IN|USB_RECIP_ENDPOINT;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001251 /* endpoint 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 len = 2;
1253 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001254 case 10: /* trigger short read (EREMOTEIO) */
1255 req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 len = 1024;
1257 expected = -EREMOTEIO;
1258 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001259 /* NOTE: two consecutive _different_ faults in the queue. */
1260 case 11: /* get endpoint descriptor (ALWAYS STALLS) */
1261 req.wValue = cpu_to_le16(USB_DT_ENDPOINT << 8);
1262 /* endpoint == 0 */
1263 len = sizeof(struct usb_interface_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 expected = EPIPE;
1265 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001266 /* NOTE: sometimes even a third fault in the queue! */
1267 case 12: /* get string 0 descriptor (MAY STALL) */
1268 req.wValue = cpu_to_le16(USB_DT_STRING << 8);
1269 /* string == 0, for language IDs */
1270 len = sizeof(struct usb_interface_descriptor);
1271 /* may succeed when > 4 languages */
1272 expected = EREMOTEIO; /* or EPIPE, if no strings */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001274 case 13: /* short read, resembling case 10 */
1275 req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
1276 /* last data packet "should" be DATA1, not DATA0 */
Paul Zimmerman6a23ccd2012-04-16 14:19:07 -07001277 if (udev->speed == USB_SPEED_SUPER)
1278 len = 1024 - 512;
1279 else
1280 len = 1024 - udev->descriptor.bMaxPacketSize0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 expected = -EREMOTEIO;
1282 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001283 case 14: /* short read; try to fill the last packet */
1284 req.wValue = cpu_to_le16((USB_DT_DEVICE << 8) | 0);
David Brownell28ffd792008-04-25 18:51:10 -07001285 /* device descriptor size == 18 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 len = udev->descriptor.bMaxPacketSize0;
Sebastian Andrzej Siewior67e7d642011-04-14 16:17:21 +02001287 if (udev->speed == USB_SPEED_SUPER)
1288 len = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 switch (len) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001290 case 8:
1291 len = 24;
1292 break;
1293 case 16:
1294 len = 32;
1295 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
1297 expected = -EREMOTEIO;
1298 break;
Huang Ruic952a8b2013-11-04 21:11:53 +08001299 case 15:
1300 req.wValue = cpu_to_le16(USB_DT_BOS << 8);
1301 if (udev->bos)
1302 len = le16_to_cpu(udev->bos->desc->wTotalLength);
1303 else
1304 len = sizeof(struct usb_bos_descriptor);
Sarah Sharp8cf43282013-12-13 13:44:17 -08001305 if (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0201)
Huang Ruic952a8b2013-11-04 21:11:53 +08001306 expected = -EPIPE;
1307 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 default:
David Brownell28ffd792008-04-25 18:51:10 -07001309 ERROR(dev, "bogus number of ctrl queue testcases!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 context.status = -EINVAL;
1311 goto cleanup;
1312 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001313 req.wLength = cpu_to_le16(len);
Amit Virdi457a0952014-08-22 14:36:37 +05301314 urb[i] = u = simple_alloc_urb(udev, pipe, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (!u)
1316 goto cleanup;
1317
Huang Ruif55055b2013-10-21 23:15:30 +08001318 reqp = kmalloc(sizeof(*reqp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (!reqp)
1320 goto cleanup;
1321 reqp->setup = req;
1322 reqp->number = i % NUM_SUBCASES;
1323 reqp->expected = expected;
1324 u->setup_packet = (char *) &reqp->setup;
1325
1326 u->context = &context;
1327 u->complete = ctrl_complete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
1329
1330 /* queue the urbs */
1331 context.urb = urb;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001332 spin_lock_irq(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 for (i = 0; i < param->sglen; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001334 context.status = usb_submit_urb(urb[i], GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (context.status != 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001336 ERROR(dev, "can't submit urb[%d], status %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 i, context.status);
1338 context.count = context.pending;
1339 break;
1340 }
1341 context.pending++;
1342 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001343 spin_unlock_irq(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 /* FIXME set timer and time out; provide a disconnect hook */
1346
1347 /* wait for the last one to complete */
1348 if (context.pending > 0)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001349 wait_for_completion(&context.complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351cleanup:
1352 for (i = 0; i < param->sglen; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001353 if (!urb[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 continue;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001355 urb[i]->dev = udev;
Alan Stern0ede76f2010-03-05 15:10:17 -05001356 kfree(urb[i]->setup_packet);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001357 simple_free_urb(urb[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001359 kfree(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return context.status;
1361}
1362#undef NUM_SUBCASES
1363
1364
1365/*-------------------------------------------------------------------------*/
1366
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001367static void unlink1_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 int status = urb->status;
1370
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001371 /* we "know" -EPIPE (stall) never happens */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (!status)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001373 status = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (status) {
1375 urb->status = status;
Ming Leicdc97792008-02-24 18:41:47 +08001376 complete(urb->context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378}
1379
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001380static int unlink1(struct usbtest_dev *dev, int pipe, int size, int async)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 struct urb *urb;
1383 struct completion completion;
1384 int retval = 0;
1385
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001386 init_completion(&completion);
Amit Virdi457a0952014-08-22 14:36:37 +05301387 urb = simple_alloc_urb(testdev_to_usbdev(dev), pipe, size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (!urb)
1389 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 urb->context = &completion;
1391 urb->complete = unlink1_callback;
1392
Huang Ruie4d58f52014-05-26 10:55:36 +08001393 if (usb_pipeout(urb->pipe)) {
1394 simple_fill_buf(urb);
1395 urb->transfer_flags |= URB_ZERO_PACKET;
1396 }
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 /* keep the endpoint busy. there are lots of hc/hcd-internal
1399 * states, and testing should get to all of them over time.
1400 *
1401 * FIXME want additional tests for when endpoint is STALLing
1402 * due to errors, or is just NAKing requests.
1403 */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001404 retval = usb_submit_urb(urb, GFP_KERNEL);
1405 if (retval != 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001406 dev_err(&dev->intf->dev, "submit fail %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return retval;
1408 }
1409
1410 /* unlinking that should always work. variable delay tests more
1411 * hcd states and code paths, even with little other system load.
1412 */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001413 msleep(jiffies % (2 * INTERRUPT_RATE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (async) {
Martin Fuzzey3b6c0232009-06-04 23:20:38 +02001415 while (!completion_done(&completion)) {
1416 retval = usb_unlink_urb(urb);
1417
Huang Ruia7683eb2014-05-22 18:06:14 +08001418 if (retval == 0 && usb_pipein(urb->pipe))
1419 retval = simple_check_buf(dev, urb);
1420
Martin Fuzzey3b6c0232009-06-04 23:20:38 +02001421 switch (retval) {
1422 case -EBUSY:
1423 case -EIDRM:
1424 /* we can't unlink urbs while they're completing
1425 * or if they've completed, and we haven't
1426 * resubmitted. "normal" drivers would prevent
1427 * resubmission, but since we're testing unlink
1428 * paths, we can't.
1429 */
1430 ERROR(dev, "unlink retry\n");
1431 continue;
1432 case 0:
1433 case -EINPROGRESS:
1434 break;
1435
1436 default:
1437 dev_err(&dev->intf->dev,
1438 "unlink fail %d\n", retval);
1439 return retval;
1440 }
1441
1442 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444 } else
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001445 usb_kill_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001447 wait_for_completion(&completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 retval = urb->status;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001449 simple_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 if (async)
1452 return (retval == -ECONNRESET) ? 0 : retval - 1000;
1453 else
1454 return (retval == -ENOENT || retval == -EPERM) ?
1455 0 : retval - 2000;
1456}
1457
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001458static int unlink_simple(struct usbtest_dev *dev, int pipe, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
1460 int retval = 0;
1461
1462 /* test sync and async paths */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001463 retval = unlink1(dev, pipe, len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (!retval)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001465 retval = unlink1(dev, pipe, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return retval;
1467}
1468
1469/*-------------------------------------------------------------------------*/
1470
Alan Stern869410f2011-04-14 11:21:04 -04001471struct queued_ctx {
1472 struct completion complete;
1473 atomic_t pending;
1474 unsigned num;
1475 int status;
1476 struct urb **urbs;
1477};
1478
1479static void unlink_queued_callback(struct urb *urb)
1480{
1481 int status = urb->status;
1482 struct queued_ctx *ctx = urb->context;
1483
1484 if (ctx->status)
1485 goto done;
1486 if (urb == ctx->urbs[ctx->num - 4] || urb == ctx->urbs[ctx->num - 2]) {
1487 if (status == -ECONNRESET)
1488 goto done;
1489 /* What error should we report if the URB completed normally? */
1490 }
1491 if (status != 0)
1492 ctx->status = status;
1493
1494 done:
1495 if (atomic_dec_and_test(&ctx->pending))
1496 complete(&ctx->complete);
1497}
1498
1499static int unlink_queued(struct usbtest_dev *dev, int pipe, unsigned num,
1500 unsigned size)
1501{
1502 struct queued_ctx ctx;
1503 struct usb_device *udev = testdev_to_usbdev(dev);
1504 void *buf;
1505 dma_addr_t buf_dma;
1506 int i;
1507 int retval = -ENOMEM;
1508
1509 init_completion(&ctx.complete);
1510 atomic_set(&ctx.pending, 1); /* One more than the actual value */
1511 ctx.num = num;
1512 ctx.status = 0;
1513
1514 buf = usb_alloc_coherent(udev, size, GFP_KERNEL, &buf_dma);
1515 if (!buf)
1516 return retval;
1517 memset(buf, 0, size);
1518
1519 /* Allocate and init the urbs we'll queue */
1520 ctx.urbs = kcalloc(num, sizeof(struct urb *), GFP_KERNEL);
1521 if (!ctx.urbs)
1522 goto free_buf;
1523 for (i = 0; i < num; i++) {
1524 ctx.urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1525 if (!ctx.urbs[i])
1526 goto free_urbs;
1527 usb_fill_bulk_urb(ctx.urbs[i], udev, pipe, buf, size,
1528 unlink_queued_callback, &ctx);
1529 ctx.urbs[i]->transfer_dma = buf_dma;
1530 ctx.urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Huang Ruie4d58f52014-05-26 10:55:36 +08001531
1532 if (usb_pipeout(ctx.urbs[i]->pipe)) {
1533 simple_fill_buf(ctx.urbs[i]);
1534 ctx.urbs[i]->transfer_flags |= URB_ZERO_PACKET;
1535 }
Alan Stern869410f2011-04-14 11:21:04 -04001536 }
1537
1538 /* Submit all the URBs and then unlink URBs num - 4 and num - 2. */
1539 for (i = 0; i < num; i++) {
1540 atomic_inc(&ctx.pending);
1541 retval = usb_submit_urb(ctx.urbs[i], GFP_KERNEL);
1542 if (retval != 0) {
1543 dev_err(&dev->intf->dev, "submit urbs[%d] fail %d\n",
1544 i, retval);
1545 atomic_dec(&ctx.pending);
1546 ctx.status = retval;
1547 break;
1548 }
1549 }
1550 if (i == num) {
1551 usb_unlink_urb(ctx.urbs[num - 4]);
1552 usb_unlink_urb(ctx.urbs[num - 2]);
1553 } else {
1554 while (--i >= 0)
1555 usb_unlink_urb(ctx.urbs[i]);
1556 }
1557
1558 if (atomic_dec_and_test(&ctx.pending)) /* The extra count */
1559 complete(&ctx.complete);
1560 wait_for_completion(&ctx.complete);
1561 retval = ctx.status;
1562
1563 free_urbs:
1564 for (i = 0; i < num; i++)
1565 usb_free_urb(ctx.urbs[i]);
1566 kfree(ctx.urbs);
1567 free_buf:
1568 usb_free_coherent(udev, size, buf, buf_dma);
1569 return retval;
1570}
1571
1572/*-------------------------------------------------------------------------*/
1573
David Brownell28ffd792008-04-25 18:51:10 -07001574static int verify_not_halted(struct usbtest_dev *tdev, int ep, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
1576 int retval;
1577 u16 status;
1578
1579 /* shouldn't look or act halted */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001580 retval = usb_get_status(urb->dev, USB_RECIP_ENDPOINT, ep, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (retval < 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001582 ERROR(tdev, "ep %02x couldn't get no-halt status, %d\n",
1583 ep, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 return retval;
1585 }
1586 if (status != 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001587 ERROR(tdev, "ep %02x bogus status: %04x != 0\n", ep, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return -EINVAL;
1589 }
David Brownell28ffd792008-04-25 18:51:10 -07001590 retval = simple_io(tdev, urb, 1, 0, 0, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (retval != 0)
1592 return -EINVAL;
1593 return 0;
1594}
1595
David Brownell28ffd792008-04-25 18:51:10 -07001596static int verify_halted(struct usbtest_dev *tdev, int ep, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
1598 int retval;
1599 u16 status;
1600
1601 /* should look and act halted */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001602 retval = usb_get_status(urb->dev, USB_RECIP_ENDPOINT, ep, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (retval < 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001604 ERROR(tdev, "ep %02x couldn't get halt status, %d\n",
1605 ep, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return retval;
1607 }
1608 if (status != 1) {
David Brownell28ffd792008-04-25 18:51:10 -07001609 ERROR(tdev, "ep %02x bogus status: %04x != 1\n", ep, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return -EINVAL;
1611 }
David Brownell28ffd792008-04-25 18:51:10 -07001612 retval = simple_io(tdev, urb, 1, 0, -EPIPE, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (retval != -EPIPE)
1614 return -EINVAL;
David Brownell28ffd792008-04-25 18:51:10 -07001615 retval = simple_io(tdev, urb, 1, 0, -EPIPE, "verify_still_halted");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 if (retval != -EPIPE)
1617 return -EINVAL;
1618 return 0;
1619}
1620
David Brownell28ffd792008-04-25 18:51:10 -07001621static int test_halt(struct usbtest_dev *tdev, int ep, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 int retval;
1624
1625 /* shouldn't look or act halted now */
David Brownell28ffd792008-04-25 18:51:10 -07001626 retval = verify_not_halted(tdev, ep, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (retval < 0)
1628 return retval;
1629
1630 /* set halt (protocol test only), verify it worked */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001631 retval = usb_control_msg(urb->dev, usb_sndctrlpipe(urb->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 USB_REQ_SET_FEATURE, USB_RECIP_ENDPOINT,
1633 USB_ENDPOINT_HALT, ep,
1634 NULL, 0, USB_CTRL_SET_TIMEOUT);
1635 if (retval < 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001636 ERROR(tdev, "ep %02x couldn't set halt, %d\n", ep, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return retval;
1638 }
David Brownell28ffd792008-04-25 18:51:10 -07001639 retval = verify_halted(tdev, ep, urb);
Roger Quadros824d7522013-12-18 15:40:11 +05301640 if (retval < 0) {
1641 int ret;
1642
1643 /* clear halt anyways, else further tests will fail */
1644 ret = usb_clear_halt(urb->dev, urb->pipe);
1645 if (ret)
1646 ERROR(tdev, "ep %02x couldn't clear halt, %d\n",
1647 ep, ret);
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return retval;
Roger Quadros824d7522013-12-18 15:40:11 +05301650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 /* clear halt (tests API + protocol), verify it worked */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001653 retval = usb_clear_halt(urb->dev, urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (retval < 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001655 ERROR(tdev, "ep %02x couldn't clear halt, %d\n", ep, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 return retval;
1657 }
David Brownell28ffd792008-04-25 18:51:10 -07001658 retval = verify_not_halted(tdev, ep, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (retval < 0)
1660 return retval;
1661
1662 /* NOTE: could also verify SET_INTERFACE clear halts ... */
1663
1664 return 0;
1665}
1666
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001667static int halt_simple(struct usbtest_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
Paul Zimmerman6a23ccd2012-04-16 14:19:07 -07001669 int ep;
1670 int retval = 0;
1671 struct urb *urb;
1672 struct usb_device *udev = testdev_to_usbdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Paul Zimmerman6a23ccd2012-04-16 14:19:07 -07001674 if (udev->speed == USB_SPEED_SUPER)
Amit Virdi457a0952014-08-22 14:36:37 +05301675 urb = simple_alloc_urb(udev, 0, 1024, 0);
Paul Zimmerman6a23ccd2012-04-16 14:19:07 -07001676 else
Amit Virdi457a0952014-08-22 14:36:37 +05301677 urb = simple_alloc_urb(udev, 0, 512, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (urb == NULL)
1679 return -ENOMEM;
1680
1681 if (dev->in_pipe) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001682 ep = usb_pipeendpoint(dev->in_pipe) | USB_DIR_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 urb->pipe = dev->in_pipe;
David Brownell28ffd792008-04-25 18:51:10 -07001684 retval = test_halt(dev, ep, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 if (retval < 0)
1686 goto done;
1687 }
1688
1689 if (dev->out_pipe) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001690 ep = usb_pipeendpoint(dev->out_pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 urb->pipe = dev->out_pipe;
David Brownell28ffd792008-04-25 18:51:10 -07001692 retval = test_halt(dev, ep, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
1694done:
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001695 simple_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 return retval;
1697}
1698
1699/*-------------------------------------------------------------------------*/
1700
1701/* Control OUT tests use the vendor control requests from Intel's
1702 * USB 2.0 compliance test device: write a buffer, read it back.
1703 *
1704 * Intel's spec only _requires_ that it work for one packet, which
1705 * is pretty weak. Some HCDs place limits here; most devices will
1706 * need to be able to handle more than one OUT data packet. We'll
1707 * try whatever we're told to try.
1708 */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001709static int ctrl_out(struct usbtest_dev *dev,
Martin Fuzzey084fb202011-01-16 19:17:11 +01001710 unsigned count, unsigned length, unsigned vary, unsigned offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Orjan Fribergf54fa842006-08-08 23:31:40 -07001712 unsigned i, j, len;
1713 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 u8 *buf;
1715 char *what = "?";
1716 struct usb_device *udev;
Orjan Fribergf54fa842006-08-08 23:31:40 -07001717
David Brownellff7c79e2005-04-22 13:17:00 -07001718 if (length < 1 || length > 0xffff || vary >= length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return -EINVAL;
1720
Martin Fuzzey084fb202011-01-16 19:17:11 +01001721 buf = kmalloc(length + offset, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 if (!buf)
1723 return -ENOMEM;
1724
Martin Fuzzey084fb202011-01-16 19:17:11 +01001725 buf += offset;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001726 udev = testdev_to_usbdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 len = length;
1728 retval = 0;
1729
1730 /* NOTE: hardware might well act differently if we pushed it
1731 * with lots back-to-back queued requests.
1732 */
1733 for (i = 0; i < count; i++) {
1734 /* write patterned data */
1735 for (j = 0; j < len; j++)
Peter Chen169900f2015-09-01 09:48:00 +08001736 buf[j] = (u8)(i + j);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001737 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 0x5b, USB_DIR_OUT|USB_TYPE_VENDOR,
1739 0, 0, buf, len, USB_CTRL_SET_TIMEOUT);
1740 if (retval != len) {
1741 what = "write";
David Brownellff7c79e2005-04-22 13:17:00 -07001742 if (retval >= 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001743 ERROR(dev, "ctrl_out, wlen %d (expected %d)\n",
David Brownellff7c79e2005-04-22 13:17:00 -07001744 retval, len);
1745 retval = -EBADMSG;
1746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 break;
1748 }
1749
1750 /* read it back -- assuming nothing intervened!! */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001751 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 0x5c, USB_DIR_IN|USB_TYPE_VENDOR,
1753 0, 0, buf, len, USB_CTRL_GET_TIMEOUT);
1754 if (retval != len) {
1755 what = "read";
David Brownellff7c79e2005-04-22 13:17:00 -07001756 if (retval >= 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001757 ERROR(dev, "ctrl_out, rlen %d (expected %d)\n",
David Brownellff7c79e2005-04-22 13:17:00 -07001758 retval, len);
1759 retval = -EBADMSG;
1760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 break;
1762 }
1763
1764 /* fail if we can't verify */
1765 for (j = 0; j < len; j++) {
Peter Chen169900f2015-09-01 09:48:00 +08001766 if (buf[j] != (u8)(i + j)) {
David Brownell28ffd792008-04-25 18:51:10 -07001767 ERROR(dev, "ctrl_out, byte %d is %d not %d\n",
Peter Chen169900f2015-09-01 09:48:00 +08001768 j, buf[j], (u8)(i + j));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 retval = -EBADMSG;
1770 break;
1771 }
1772 }
1773 if (retval < 0) {
1774 what = "verify";
1775 break;
1776 }
1777
1778 len += vary;
David Brownellff7c79e2005-04-22 13:17:00 -07001779
1780 /* [real world] the "zero bytes IN" case isn't really used.
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001781 * hardware can easily trip up in this weird case, since its
David Brownellff7c79e2005-04-22 13:17:00 -07001782 * status stage is IN, not OUT like other ep0in transfers.
1783 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if (len > length)
David Brownellff7c79e2005-04-22 13:17:00 -07001785 len = realworld ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
1787
1788 if (retval < 0)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001789 ERROR(dev, "ctrl_out %s failed, code %d, count %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 what, retval, i);
1791
Martin Fuzzey084fb202011-01-16 19:17:11 +01001792 kfree(buf - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return retval;
1794}
1795
1796/*-------------------------------------------------------------------------*/
1797
1798/* ISO tests ... mimics common usage
1799 * - buffer length is split into N packets (mostly maxpacket sized)
1800 * - multi-buffers according to sglen
1801 */
1802
1803struct iso_context {
1804 unsigned count;
1805 unsigned pending;
1806 spinlock_t lock;
1807 struct completion done;
Alan Stern9da21502006-05-22 16:47:13 -04001808 int submit_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 unsigned long errors;
Alan Stern9da21502006-05-22 16:47:13 -04001810 unsigned long packet_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 struct usbtest_dev *dev;
1812};
1813
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001814static void iso_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
1816 struct iso_context *ctx = urb->context;
1817
1818 spin_lock(&ctx->lock);
1819 ctx->count--;
1820
Alan Stern9da21502006-05-22 16:47:13 -04001821 ctx->packet_count += urb->number_of_packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (urb->error_count > 0)
1823 ctx->errors += urb->error_count;
Alan Stern9da21502006-05-22 16:47:13 -04001824 else if (urb->status != 0)
1825 ctx->errors += urb->number_of_packets;
Martin Fuzzey40aed522010-10-01 00:20:48 +02001826 else if (urb->actual_length != urb->transfer_buffer_length)
1827 ctx->errors++;
Martin Fuzzey084fb202011-01-16 19:17:11 +01001828 else if (check_guard_bytes(ctx->dev, urb) != 0)
1829 ctx->errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Alan Stern9da21502006-05-22 16:47:13 -04001831 if (urb->status == 0 && ctx->count > (ctx->pending - 1)
1832 && !ctx->submit_error) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001833 int status = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 switch (status) {
1835 case 0:
1836 goto done;
1837 default:
David Brownell28ffd792008-04-25 18:51:10 -07001838 dev_err(&ctx->dev->intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 "iso resubmit err %d\n",
1840 status);
1841 /* FALLTHROUGH */
1842 case -ENODEV: /* disconnected */
Alan Stern9da21502006-05-22 16:47:13 -04001843 case -ESHUTDOWN: /* endpoint disabled */
1844 ctx->submit_error = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 break;
1846 }
1847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 ctx->pending--;
1850 if (ctx->pending == 0) {
1851 if (ctx->errors)
David Brownell28ffd792008-04-25 18:51:10 -07001852 dev_err(&ctx->dev->intf->dev,
Alan Stern9da21502006-05-22 16:47:13 -04001853 "iso test, %lu errors out of %lu\n",
1854 ctx->errors, ctx->packet_count);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001855 complete(&ctx->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
1857done:
1858 spin_unlock(&ctx->lock);
1859}
1860
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001861static struct urb *iso_alloc_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 struct usb_device *udev,
1863 int pipe,
1864 struct usb_endpoint_descriptor *desc,
Martin Fuzzey084fb202011-01-16 19:17:11 +01001865 long bytes,
1866 unsigned offset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867)
1868{
1869 struct urb *urb;
1870 unsigned i, maxp, packets;
1871
1872 if (bytes < 0 || !desc)
1873 return NULL;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001874 maxp = 0x7ff & usb_endpoint_maxp(desc);
1875 maxp *= 1 + (0x3 & (usb_endpoint_maxp(desc) >> 11));
Julia Lawalldfa5ec72008-03-04 15:25:11 -08001876 packets = DIV_ROUND_UP(bytes, maxp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001878 urb = usb_alloc_urb(packets, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (!urb)
1880 return urb;
1881 urb->dev = udev;
1882 urb->pipe = pipe;
1883
1884 urb->number_of_packets = packets;
1885 urb->transfer_buffer_length = bytes;
Martin Fuzzey084fb202011-01-16 19:17:11 +01001886 urb->transfer_buffer = usb_alloc_coherent(udev, bytes + offset,
1887 GFP_KERNEL,
1888 &urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 if (!urb->transfer_buffer) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001890 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return NULL;
1892 }
Martin Fuzzey084fb202011-01-16 19:17:11 +01001893 if (offset) {
1894 memset(urb->transfer_buffer, GUARD_BYTE, offset);
1895 urb->transfer_buffer += offset;
1896 urb->transfer_dma += offset;
1897 }
1898 /* For inbound transfers use guard byte so that test fails if
1899 data not correctly copied */
1900 memset(urb->transfer_buffer,
1901 usb_pipein(urb->pipe) ? GUARD_BYTE : 0,
1902 bytes);
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 for (i = 0; i < packets; i++) {
1905 /* here, only the last packet will be short */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001906 urb->iso_frame_desc[i].length = min((unsigned) bytes, maxp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 bytes -= urb->iso_frame_desc[i].length;
1908
1909 urb->iso_frame_desc[i].offset = maxp * i;
1910 }
1911
1912 urb->complete = iso_callback;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001913 /* urb->context = SET BY CALLER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 urb->interval = 1 << (desc->bInterval - 1);
1915 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
1916 return urb;
1917}
1918
1919static int
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001920test_iso_queue(struct usbtest_dev *dev, struct usbtest_param *param,
Martin Fuzzey084fb202011-01-16 19:17:11 +01001921 int pipe, struct usb_endpoint_descriptor *desc, unsigned offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
1923 struct iso_context context;
1924 struct usb_device *udev;
1925 unsigned i;
1926 unsigned long packets = 0;
Alan Stern9da21502006-05-22 16:47:13 -04001927 int status = 0;
Peter Chen41d3c0b2015-09-01 09:47:58 +08001928 struct urb *urbs[param->sglen];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Huang Ruif55055b2013-10-21 23:15:30 +08001930 memset(&context, 0, sizeof(context));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 context.count = param->iterations * param->sglen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 context.dev = dev;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001933 init_completion(&context.done);
1934 spin_lock_init(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001936 udev = testdev_to_usbdev(dev);
David Brownell28ffd792008-04-25 18:51:10 -07001937 dev_info(&dev->intf->dev,
Peter Chena0c9d0d2015-08-17 16:36:53 +08001938 "iso period %d %sframes, wMaxPacket %d, transactions: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 1 << (desc->bInterval - 1),
1940 (udev->speed == USB_SPEED_HIGH) ? "micro" : "",
Peter Chena0c9d0d2015-08-17 16:36:53 +08001941 usb_endpoint_maxp(desc) & 0x7ff,
1942 1 + (0x3 & (usb_endpoint_maxp(desc) >> 11)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 for (i = 0; i < param->sglen; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001945 urbs[i] = iso_alloc_urb(udev, pipe, desc,
Martin Fuzzey084fb202011-01-16 19:17:11 +01001946 param->length, offset);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001947 if (!urbs[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 status = -ENOMEM;
1949 goto fail;
1950 }
1951 packets += urbs[i]->number_of_packets;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001952 urbs[i]->context = &context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
1954 packets *= param->iterations;
David Brownell28ffd792008-04-25 18:51:10 -07001955 dev_info(&dev->intf->dev,
Peter Chena0c9d0d2015-08-17 16:36:53 +08001956 "total %lu msec (%lu packets)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 (packets * (1 << (desc->bInterval - 1)))
1958 / ((udev->speed == USB_SPEED_HIGH) ? 8 : 1),
1959 packets);
1960
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001961 spin_lock_irq(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 for (i = 0; i < param->sglen; i++) {
Alan Stern9da21502006-05-22 16:47:13 -04001963 ++context.pending;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001964 status = usb_submit_urb(urbs[i], GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 if (status < 0) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001966 ERROR(dev, "submit iso[%d], error %d\n", i, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (i == 0) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001968 spin_unlock_irq(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 goto fail;
1970 }
1971
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001972 simple_free_urb(urbs[i]);
Ming Leie10e1be2010-08-02 22:09:01 +08001973 urbs[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 context.pending--;
Alan Stern9da21502006-05-22 16:47:13 -04001975 context.submit_error = 1;
1976 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
1978 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001979 spin_unlock_irq(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001981 wait_for_completion(&context.done);
Alan Stern9da21502006-05-22 16:47:13 -04001982
Ming Leie10e1be2010-08-02 22:09:01 +08001983 for (i = 0; i < param->sglen; i++) {
1984 if (urbs[i])
1985 simple_free_urb(urbs[i]);
1986 }
Alan Stern9da21502006-05-22 16:47:13 -04001987 /*
1988 * Isochronous transfers are expected to fail sometimes. As an
1989 * arbitrary limit, we will report an error if any submissions
1990 * fail or if the transfer failure rate is > 10%.
1991 */
1992 if (status != 0)
1993 ;
1994 else if (context.submit_error)
1995 status = -EACCES;
1996 else if (context.errors > context.packet_count / 10)
1997 status = -EIO;
1998 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000fail:
2001 for (i = 0; i < param->sglen; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002002 if (urbs[i])
2003 simple_free_urb(urbs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
2005 return status;
2006}
2007
Martin Fuzzey084fb202011-01-16 19:17:11 +01002008static int test_unaligned_bulk(
2009 struct usbtest_dev *tdev,
2010 int pipe,
2011 unsigned length,
2012 int iterations,
2013 unsigned transfer_flags,
2014 const char *label)
2015{
2016 int retval;
2017 struct urb *urb = usbtest_alloc_urb(
Amit Virdi457a0952014-08-22 14:36:37 +05302018 testdev_to_usbdev(tdev), pipe, length, transfer_flags, 1, 0);
Martin Fuzzey084fb202011-01-16 19:17:11 +01002019
2020 if (!urb)
2021 return -ENOMEM;
2022
2023 retval = simple_io(tdev, urb, iterations, 0, 0, label);
2024 simple_free_urb(urb);
2025 return retval;
2026}
2027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028/*-------------------------------------------------------------------------*/
2029
2030/* We only have this one interface to user space, through usbfs.
2031 * User mode code can scan usbfs to find N different devices (maybe on
2032 * different busses) to use when testing, and allocate one thread per
2033 * test. So discovery is simplified, and we have no device naming issues.
2034 *
2035 * Don't use these only as stress/load tests. Use them along with with
2036 * other USB bus activity: plugging, unplugging, mousing, mp3 playback,
2037 * video capture, and so on. Run different tests at different times, in
2038 * different sequences. Nothing here should interact with other devices,
2039 * except indirectly by consuming USB bandwidth and CPU resources for test
2040 * threads and request completion. But the only way to know that for sure
2041 * is to test when HC queues are in use by many devices.
David Brownell28ffd792008-04-25 18:51:10 -07002042 *
2043 * WARNING: Because usbfs grabs udev->dev.sem before calling this ioctl(),
2044 * it locks out usbcore in certain code paths. Notably, if you disconnect
Petr Mladek37ebb542014-09-19 17:32:23 +02002045 * the device-under-test, hub_wq will wait block forever waiting for the
David Brownell28ffd792008-04-25 18:51:10 -07002046 * ioctl to complete ... so that usb_disconnect() can abort the pending
2047 * urbs and then call usbtest_disconnect(). To abort a test, you're best
2048 * off just killing the userspace task and waiting for it to exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 */
2050
2051static int
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002052usbtest_ioctl(struct usb_interface *intf, unsigned int code, void *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002054 struct usbtest_dev *dev = usb_get_intfdata(intf);
2055 struct usb_device *udev = testdev_to_usbdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 struct usbtest_param *param = buf;
2057 int retval = -EOPNOTSUPP;
2058 struct urb *urb;
2059 struct scatterlist *sg;
2060 struct usb_sg_request req;
2061 struct timeval start;
2062 unsigned i;
2063
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002064 /* FIXME USBDEVFS_CONNECTINFO doesn't say how fast the device is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Vikram Pandita7f4e9852009-11-09 21:24:32 -06002066 pattern = mod_pattern;
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (code != USBTEST_REQUEST)
2069 return -EOPNOTSUPP;
2070
roel kluin8aafdf62008-10-21 00:36:44 -04002071 if (param->iterations <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return -EINVAL;
2073
Peter Chen41d3c0b2015-09-01 09:47:58 +08002074 if (param->sglen > MAX_SGLEN)
2075 return -EINVAL;
2076
Matthias Kaehlcke1cfab022007-12-13 16:15:33 -08002077 if (mutex_lock_interruptible(&dev->lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return -ERESTARTSYS;
2079
Alan Stern70a1c9e2008-03-06 17:00:58 -05002080 /* FIXME: What if a system sleep starts while a test is running? */
David Brownellff7c79e2005-04-22 13:17:00 -07002081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 /* some devices, like ez-usb default devices, need a non-default
2083 * altsetting to have any active endpoints. some tests change
2084 * altsettings; force a default so most tests don't need to check.
2085 */
2086 if (dev->info->alt >= 0) {
David Brownell28ffd792008-04-25 18:51:10 -07002087 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 if (intf->altsetting->desc.bInterfaceNumber) {
Matthias Kaehlcke1cfab022007-12-13 16:15:33 -08002090 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 return -ENODEV;
2092 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002093 res = set_altsetting(dev, dev->info->alt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if (res) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002095 dev_err(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 "set altsetting to %d failed, %d\n",
2097 dev->info->alt, res);
Matthias Kaehlcke1cfab022007-12-13 16:15:33 -08002098 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 return res;
2100 }
2101 }
2102
2103 /*
2104 * Just a bunch of test cases that every HCD is expected to handle.
2105 *
2106 * Some may need specific firmware, though it'd be good to have
2107 * one firmware image to handle all the test cases.
2108 *
2109 * FIXME add more tests! cancel requests, verify the data, control
2110 * queueing, concurrent read+write threads, and so on.
2111 */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002112 do_gettimeofday(&start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 switch (param->test_num) {
2114
2115 case 0:
David Brownell28ffd792008-04-25 18:51:10 -07002116 dev_info(&intf->dev, "TEST 0: NOP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 retval = 0;
2118 break;
2119
2120 /* Simple non-queued bulk I/O tests */
2121 case 1:
2122 if (dev->out_pipe == 0)
2123 break;
David Brownell28ffd792008-04-25 18:51:10 -07002124 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 "TEST 1: write %d bytes %u times\n",
2126 param->length, param->iterations);
Amit Virdi457a0952014-08-22 14:36:37 +05302127 urb = simple_alloc_urb(udev, dev->out_pipe, param->length, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 if (!urb) {
2129 retval = -ENOMEM;
2130 break;
2131 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002132 /* FIRMWARE: bulk sink (maybe accepts short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07002133 retval = simple_io(dev, urb, param->iterations, 0, 0, "test1");
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002134 simple_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 break;
2136 case 2:
2137 if (dev->in_pipe == 0)
2138 break;
David Brownell28ffd792008-04-25 18:51:10 -07002139 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 "TEST 2: read %d bytes %u times\n",
2141 param->length, param->iterations);
Amit Virdi457a0952014-08-22 14:36:37 +05302142 urb = simple_alloc_urb(udev, dev->in_pipe, param->length, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (!urb) {
2144 retval = -ENOMEM;
2145 break;
2146 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002147 /* FIRMWARE: bulk source (maybe generates short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07002148 retval = simple_io(dev, urb, param->iterations, 0, 0, "test2");
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002149 simple_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 break;
2151 case 3:
2152 if (dev->out_pipe == 0 || param->vary == 0)
2153 break;
David Brownell28ffd792008-04-25 18:51:10 -07002154 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 "TEST 3: write/%d 0..%d bytes %u times\n",
2156 param->vary, param->length, param->iterations);
Amit Virdi457a0952014-08-22 14:36:37 +05302157 urb = simple_alloc_urb(udev, dev->out_pipe, param->length, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 if (!urb) {
2159 retval = -ENOMEM;
2160 break;
2161 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002162 /* FIRMWARE: bulk sink (maybe accepts short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07002163 retval = simple_io(dev, urb, param->iterations, param->vary,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 0, "test3");
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002165 simple_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 break;
2167 case 4:
2168 if (dev->in_pipe == 0 || param->vary == 0)
2169 break;
David Brownell28ffd792008-04-25 18:51:10 -07002170 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 "TEST 4: read/%d 0..%d bytes %u times\n",
2172 param->vary, param->length, param->iterations);
Amit Virdi457a0952014-08-22 14:36:37 +05302173 urb = simple_alloc_urb(udev, dev->in_pipe, param->length, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (!urb) {
2175 retval = -ENOMEM;
2176 break;
2177 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002178 /* FIRMWARE: bulk source (maybe generates short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07002179 retval = simple_io(dev, urb, param->iterations, param->vary,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 0, "test4");
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002181 simple_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 break;
2183
2184 /* Queued bulk I/O tests */
2185 case 5:
2186 if (dev->out_pipe == 0 || param->sglen == 0)
2187 break;
David Brownell28ffd792008-04-25 18:51:10 -07002188 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 "TEST 5: write %d sglists %d entries of %d bytes\n",
2190 param->iterations,
2191 param->sglen, param->length);
Alan Sternb9a6e8e2015-09-01 09:48:01 +08002192 sg = alloc_sglist(param->sglen, param->length,
2193 0, dev, dev->out_pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (!sg) {
2195 retval = -ENOMEM;
2196 break;
2197 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002198 /* FIRMWARE: bulk sink (maybe accepts short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07002199 retval = perform_sglist(dev, param->iterations, dev->out_pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 &req, sg, param->sglen);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002201 free_sglist(sg, param->sglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 break;
2203
2204 case 6:
2205 if (dev->in_pipe == 0 || param->sglen == 0)
2206 break;
David Brownell28ffd792008-04-25 18:51:10 -07002207 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 "TEST 6: read %d sglists %d entries of %d bytes\n",
2209 param->iterations,
2210 param->sglen, param->length);
Alan Sternb9a6e8e2015-09-01 09:48:01 +08002211 sg = alloc_sglist(param->sglen, param->length,
2212 0, dev, dev->in_pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (!sg) {
2214 retval = -ENOMEM;
2215 break;
2216 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002217 /* FIRMWARE: bulk source (maybe generates short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07002218 retval = perform_sglist(dev, param->iterations, dev->in_pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 &req, sg, param->sglen);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002220 free_sglist(sg, param->sglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 break;
2222 case 7:
2223 if (dev->out_pipe == 0 || param->sglen == 0 || param->vary == 0)
2224 break;
David Brownell28ffd792008-04-25 18:51:10 -07002225 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 "TEST 7: write/%d %d sglists %d entries 0..%d bytes\n",
2227 param->vary, param->iterations,
2228 param->sglen, param->length);
Alan Sternb9a6e8e2015-09-01 09:48:01 +08002229 sg = alloc_sglist(param->sglen, param->length,
2230 param->vary, dev, dev->out_pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 if (!sg) {
2232 retval = -ENOMEM;
2233 break;
2234 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002235 /* FIRMWARE: bulk sink (maybe accepts short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07002236 retval = perform_sglist(dev, param->iterations, dev->out_pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 &req, sg, param->sglen);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002238 free_sglist(sg, param->sglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 break;
2240 case 8:
2241 if (dev->in_pipe == 0 || param->sglen == 0 || param->vary == 0)
2242 break;
David Brownell28ffd792008-04-25 18:51:10 -07002243 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 "TEST 8: read/%d %d sglists %d entries 0..%d bytes\n",
2245 param->vary, param->iterations,
2246 param->sglen, param->length);
Alan Sternb9a6e8e2015-09-01 09:48:01 +08002247 sg = alloc_sglist(param->sglen, param->length,
2248 param->vary, dev, dev->in_pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 if (!sg) {
2250 retval = -ENOMEM;
2251 break;
2252 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002253 /* FIRMWARE: bulk source (maybe generates short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07002254 retval = perform_sglist(dev, param->iterations, dev->in_pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 &req, sg, param->sglen);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002256 free_sglist(sg, param->sglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 break;
2258
2259 /* non-queued sanity tests for control (chapter 9 subset) */
2260 case 9:
2261 retval = 0;
David Brownell28ffd792008-04-25 18:51:10 -07002262 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 "TEST 9: ch9 (subset) control tests, %d times\n",
2264 param->iterations);
2265 for (i = param->iterations; retval == 0 && i--; /* NOP */)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002266 retval = ch9_postconfig(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (retval)
David Brownell28ffd792008-04-25 18:51:10 -07002268 dev_err(&intf->dev, "ch9 subset failed, "
2269 "iterations left %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 break;
2271
2272 /* queued control messaging */
2273 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 retval = 0;
David Brownell28ffd792008-04-25 18:51:10 -07002275 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 "TEST 10: queue %d control calls, %d times\n",
2277 param->sglen,
2278 param->iterations);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002279 retval = test_ctrl_queue(dev, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 break;
2281
2282 /* simple non-queued unlinks (ring with one urb) */
2283 case 11:
2284 if (dev->in_pipe == 0 || !param->length)
2285 break;
2286 retval = 0;
David Brownell28ffd792008-04-25 18:51:10 -07002287 dev_info(&intf->dev, "TEST 11: unlink %d reads of %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 param->iterations, param->length);
2289 for (i = param->iterations; retval == 0 && i--; /* NOP */)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002290 retval = unlink_simple(dev, dev->in_pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 param->length);
2292 if (retval)
David Brownell28ffd792008-04-25 18:51:10 -07002293 dev_err(&intf->dev, "unlink reads failed %d, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 "iterations left %d\n", retval, i);
2295 break;
2296 case 12:
2297 if (dev->out_pipe == 0 || !param->length)
2298 break;
2299 retval = 0;
David Brownell28ffd792008-04-25 18:51:10 -07002300 dev_info(&intf->dev, "TEST 12: unlink %d writes of %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 param->iterations, param->length);
2302 for (i = param->iterations; retval == 0 && i--; /* NOP */)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002303 retval = unlink_simple(dev, dev->out_pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 param->length);
2305 if (retval)
David Brownell28ffd792008-04-25 18:51:10 -07002306 dev_err(&intf->dev, "unlink writes failed %d, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 "iterations left %d\n", retval, i);
2308 break;
2309
2310 /* ep halt tests */
2311 case 13:
2312 if (dev->out_pipe == 0 && dev->in_pipe == 0)
2313 break;
2314 retval = 0;
David Brownell28ffd792008-04-25 18:51:10 -07002315 dev_info(&intf->dev, "TEST 13: set/clear %d halts\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 param->iterations);
2317 for (i = param->iterations; retval == 0 && i--; /* NOP */)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002318 retval = halt_simple(dev);
David Brownell28ffd792008-04-25 18:51:10 -07002319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (retval)
David Brownell28ffd792008-04-25 18:51:10 -07002321 ERROR(dev, "halts failed, iterations left %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 break;
2323
2324 /* control write tests */
2325 case 14:
2326 if (!dev->info->ctrl_out)
2327 break;
David Brownell28ffd792008-04-25 18:51:10 -07002328 dev_info(&intf->dev, "TEST 14: %d ep0out, %d..%d vary %d\n",
David Brownellff7c79e2005-04-22 13:17:00 -07002329 param->iterations,
2330 realworld ? 1 : 0, param->length,
2331 param->vary);
David Brownell28ffd792008-04-25 18:51:10 -07002332 retval = ctrl_out(dev, param->iterations,
Martin Fuzzey084fb202011-01-16 19:17:11 +01002333 param->length, param->vary, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 break;
2335
2336 /* iso write tests */
2337 case 15:
2338 if (dev->out_iso_pipe == 0 || param->sglen == 0)
2339 break;
David Brownell28ffd792008-04-25 18:51:10 -07002340 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 "TEST 15: write %d iso, %d entries of %d bytes\n",
2342 param->iterations,
2343 param->sglen, param->length);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002344 /* FIRMWARE: iso sink */
2345 retval = test_iso_queue(dev, param,
Martin Fuzzey084fb202011-01-16 19:17:11 +01002346 dev->out_iso_pipe, dev->iso_out, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 break;
2348
2349 /* iso read tests */
2350 case 16:
2351 if (dev->in_iso_pipe == 0 || param->sglen == 0)
2352 break;
David Brownell28ffd792008-04-25 18:51:10 -07002353 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 "TEST 16: read %d iso, %d entries of %d bytes\n",
2355 param->iterations,
2356 param->sglen, param->length);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002357 /* FIRMWARE: iso source */
2358 retval = test_iso_queue(dev, param,
Martin Fuzzey084fb202011-01-16 19:17:11 +01002359 dev->in_iso_pipe, dev->iso_in, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 break;
2361
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002362 /* FIXME scatterlist cancel (needs helper thread) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Martin Fuzzey084fb202011-01-16 19:17:11 +01002364 /* Tests for bulk I/O using DMA mapping by core and odd address */
2365 case 17:
2366 if (dev->out_pipe == 0)
2367 break;
2368 dev_info(&intf->dev,
2369 "TEST 17: write odd addr %d bytes %u times core map\n",
2370 param->length, param->iterations);
2371
2372 retval = test_unaligned_bulk(
2373 dev, dev->out_pipe,
2374 param->length, param->iterations,
2375 0, "test17");
2376 break;
2377
2378 case 18:
2379 if (dev->in_pipe == 0)
2380 break;
2381 dev_info(&intf->dev,
2382 "TEST 18: read odd addr %d bytes %u times core map\n",
2383 param->length, param->iterations);
2384
2385 retval = test_unaligned_bulk(
2386 dev, dev->in_pipe,
2387 param->length, param->iterations,
2388 0, "test18");
2389 break;
2390
2391 /* Tests for bulk I/O using premapped coherent buffer and odd address */
2392 case 19:
2393 if (dev->out_pipe == 0)
2394 break;
2395 dev_info(&intf->dev,
2396 "TEST 19: write odd addr %d bytes %u times premapped\n",
2397 param->length, param->iterations);
2398
2399 retval = test_unaligned_bulk(
2400 dev, dev->out_pipe,
2401 param->length, param->iterations,
2402 URB_NO_TRANSFER_DMA_MAP, "test19");
2403 break;
2404
2405 case 20:
2406 if (dev->in_pipe == 0)
2407 break;
2408 dev_info(&intf->dev,
2409 "TEST 20: read odd addr %d bytes %u times premapped\n",
2410 param->length, param->iterations);
2411
2412 retval = test_unaligned_bulk(
2413 dev, dev->in_pipe,
2414 param->length, param->iterations,
2415 URB_NO_TRANSFER_DMA_MAP, "test20");
2416 break;
2417
2418 /* control write tests with unaligned buffer */
2419 case 21:
2420 if (!dev->info->ctrl_out)
2421 break;
2422 dev_info(&intf->dev,
2423 "TEST 21: %d ep0out odd addr, %d..%d vary %d\n",
2424 param->iterations,
2425 realworld ? 1 : 0, param->length,
2426 param->vary);
2427 retval = ctrl_out(dev, param->iterations,
2428 param->length, param->vary, 1);
2429 break;
2430
2431 /* unaligned iso tests */
2432 case 22:
2433 if (dev->out_iso_pipe == 0 || param->sglen == 0)
2434 break;
2435 dev_info(&intf->dev,
2436 "TEST 22: write %d iso odd, %d entries of %d bytes\n",
2437 param->iterations,
2438 param->sglen, param->length);
2439 retval = test_iso_queue(dev, param,
2440 dev->out_iso_pipe, dev->iso_out, 1);
2441 break;
2442
2443 case 23:
2444 if (dev->in_iso_pipe == 0 || param->sglen == 0)
2445 break;
2446 dev_info(&intf->dev,
2447 "TEST 23: read %d iso odd, %d entries of %d bytes\n",
2448 param->iterations,
2449 param->sglen, param->length);
2450 retval = test_iso_queue(dev, param,
2451 dev->in_iso_pipe, dev->iso_in, 1);
2452 break;
2453
Alan Stern869410f2011-04-14 11:21:04 -04002454 /* unlink URBs from a bulk-OUT queue */
2455 case 24:
2456 if (dev->out_pipe == 0 || !param->length || param->sglen < 4)
2457 break;
2458 retval = 0;
Alan Stern2cb50002013-01-02 13:58:18 -05002459 dev_info(&intf->dev, "TEST 24: unlink from %d queues of "
Alan Stern869410f2011-04-14 11:21:04 -04002460 "%d %d-byte writes\n",
2461 param->iterations, param->sglen, param->length);
2462 for (i = param->iterations; retval == 0 && i > 0; --i) {
2463 retval = unlink_queued(dev, dev->out_pipe,
2464 param->sglen, param->length);
2465 if (retval) {
2466 dev_err(&intf->dev,
2467 "unlink queued writes failed %d, "
2468 "iterations left %d\n", retval, i);
2469 break;
2470 }
2471 }
2472 break;
2473
Amit Virdi457a0952014-08-22 14:36:37 +05302474 /* Simple non-queued interrupt I/O tests */
2475 case 25:
2476 if (dev->out_int_pipe == 0)
2477 break;
2478 dev_info(&intf->dev,
2479 "TEST 25: write %d bytes %u times\n",
2480 param->length, param->iterations);
2481 urb = simple_alloc_urb(udev, dev->out_int_pipe, param->length,
2482 dev->int_out->bInterval);
2483 if (!urb) {
2484 retval = -ENOMEM;
2485 break;
2486 }
2487 /* FIRMWARE: interrupt sink (maybe accepts short writes) */
2488 retval = simple_io(dev, urb, param->iterations, 0, 0, "test25");
2489 simple_free_urb(urb);
2490 break;
2491 case 26:
2492 if (dev->in_int_pipe == 0)
2493 break;
2494 dev_info(&intf->dev,
2495 "TEST 26: read %d bytes %u times\n",
2496 param->length, param->iterations);
2497 urb = simple_alloc_urb(udev, dev->in_int_pipe, param->length,
2498 dev->int_in->bInterval);
2499 if (!urb) {
2500 retval = -ENOMEM;
2501 break;
2502 }
2503 /* FIRMWARE: interrupt source (maybe generates short writes) */
2504 retval = simple_io(dev, urb, param->iterations, 0, 0, "test26");
2505 simple_free_urb(urb);
2506 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002508 do_gettimeofday(&param->duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 param->duration.tv_sec -= start.tv_sec;
2510 param->duration.tv_usec -= start.tv_usec;
2511 if (param->duration.tv_usec < 0) {
2512 param->duration.tv_usec += 1000 * 1000;
2513 param->duration.tv_sec -= 1;
2514 }
Matthias Kaehlcke1cfab022007-12-13 16:15:33 -08002515 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return retval;
2517}
2518
2519/*-------------------------------------------------------------------------*/
2520
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002521static unsigned force_interrupt;
2522module_param(force_interrupt, uint, 0);
2523MODULE_PARM_DESC(force_interrupt, "0 = test default; else interrupt");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
2525#ifdef GENERIC
2526static unsigned short vendor;
2527module_param(vendor, ushort, 0);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002528MODULE_PARM_DESC(vendor, "vendor code (from usb-if)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
2530static unsigned short product;
2531module_param(product, ushort, 0);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002532MODULE_PARM_DESC(product, "product code (from vendor)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533#endif
2534
2535static int
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002536usbtest_probe(struct usb_interface *intf, const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
2538 struct usb_device *udev;
2539 struct usbtest_dev *dev;
2540 struct usbtest_info *info;
2541 char *rtest, *wtest;
2542 char *irtest, *iwtest;
Amit Virdi457a0952014-08-22 14:36:37 +05302543 char *intrtest, *intwtest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002545 udev = interface_to_usbdev(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
2547#ifdef GENERIC
2548 /* specify devices by module parameters? */
2549 if (id->match_flags == 0) {
2550 /* vendor match required, product match optional */
2551 if (!vendor || le16_to_cpu(udev->descriptor.idVendor) != (u16)vendor)
2552 return -ENODEV;
2553 if (product && le16_to_cpu(udev->descriptor.idProduct) != (u16)product)
2554 return -ENODEV;
David Brownell28ffd792008-04-25 18:51:10 -07002555 dev_info(&intf->dev, "matched module params, "
2556 "vend=0x%04x prod=0x%04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 le16_to_cpu(udev->descriptor.idVendor),
2558 le16_to_cpu(udev->descriptor.idProduct));
2559 }
2560#endif
2561
Christoph Lametere94b1762006-12-06 20:33:17 -08002562 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if (!dev)
2564 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 info = (struct usbtest_info *) id->driver_info;
2566 dev->info = info;
Matthias Kaehlcke1cfab022007-12-13 16:15:33 -08002567 mutex_init(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
2569 dev->intf = intf;
2570
2571 /* cacheline-aligned scratch for i/o */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002572 dev->buf = kmalloc(TBUF_SIZE, GFP_KERNEL);
2573 if (dev->buf == NULL) {
2574 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 return -ENOMEM;
2576 }
2577
2578 /* NOTE this doesn't yet test the handful of difference that are
2579 * visible with high speed interrupts: bigger maxpacket (1K) and
2580 * "high bandwidth" modes (up to 3 packets/uframe).
2581 */
2582 rtest = wtest = "";
2583 irtest = iwtest = "";
Amit Virdi457a0952014-08-22 14:36:37 +05302584 intrtest = intwtest = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if (force_interrupt || udev->speed == USB_SPEED_LOW) {
2586 if (info->ep_in) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002587 dev->in_pipe = usb_rcvintpipe(udev, info->ep_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 rtest = " intr-in";
2589 }
2590 if (info->ep_out) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002591 dev->out_pipe = usb_sndintpipe(udev, info->ep_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 wtest = " intr-out";
2593 }
2594 } else {
Alan Sternd0b46522013-01-30 16:38:11 -05002595 if (override_alt >= 0 || info->autoconf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 int status;
2597
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002598 status = get_endpoints(dev, intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 if (status < 0) {
Arjan van de Venb6c63932008-07-25 01:45:52 -07002600 WARNING(dev, "couldn't get endpoints, %d\n",
David Brownell28ffd792008-04-25 18:51:10 -07002601 status);
Julia Lawallf4a728d2012-03-25 21:08:32 +02002602 kfree(dev->buf);
2603 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 return status;
2605 }
2606 /* may find bulk or ISO pipes */
2607 } else {
2608 if (info->ep_in)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002609 dev->in_pipe = usb_rcvbulkpipe(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 info->ep_in);
2611 if (info->ep_out)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002612 dev->out_pipe = usb_sndbulkpipe(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 info->ep_out);
2614 }
2615 if (dev->in_pipe)
2616 rtest = " bulk-in";
2617 if (dev->out_pipe)
2618 wtest = " bulk-out";
2619 if (dev->in_iso_pipe)
2620 irtest = " iso-in";
2621 if (dev->out_iso_pipe)
2622 iwtest = " iso-out";
Amit Virdi457a0952014-08-22 14:36:37 +05302623 if (dev->in_int_pipe)
2624 intrtest = " int-in";
2625 if (dev->out_int_pipe)
2626 intwtest = " int-out";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 }
2628
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002629 usb_set_intfdata(intf, dev);
2630 dev_info(&intf->dev, "%s\n", info->name);
Amit Virdi457a0952014-08-22 14:36:37 +05302631 dev_info(&intf->dev, "%s {control%s%s%s%s%s%s%s} tests%s\n",
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02002632 usb_speed_string(udev->speed),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 info->ctrl_out ? " in/out" : "",
2634 rtest, wtest,
2635 irtest, iwtest,
Amit Virdi457a0952014-08-22 14:36:37 +05302636 intrtest, intwtest,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 info->alt >= 0 ? " (+alt)" : "");
2638 return 0;
2639}
2640
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002641static int usbtest_suspend(struct usb_interface *intf, pm_message_t message)
David Brownellff7c79e2005-04-22 13:17:00 -07002642{
David Brownellff7c79e2005-04-22 13:17:00 -07002643 return 0;
2644}
2645
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002646static int usbtest_resume(struct usb_interface *intf)
David Brownellff7c79e2005-04-22 13:17:00 -07002647{
David Brownellff7c79e2005-04-22 13:17:00 -07002648 return 0;
2649}
2650
2651
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002652static void usbtest_disconnect(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002654 struct usbtest_dev *dev = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002656 usb_set_intfdata(intf, NULL);
2657 dev_dbg(&intf->dev, "disconnect\n");
2658 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659}
2660
2661/* Basic testing only needs a device that can source or sink bulk traffic.
2662 * Any device can test control transfers (default with GENERIC binding).
2663 *
2664 * Several entries work with the default EP0 implementation that's built
2665 * into EZ-USB chips. There's a default vendor ID which can be overridden
2666 * by (very) small config EEPROMS, but otherwise all these devices act
2667 * identically until firmware is loaded: only EP0 works. It turns out
2668 * to be easy to make other endpoints work, without modifying that EP0
2669 * behavior. For now, we expect that kind of firmware.
2670 */
2671
2672/* an21xx or fx versions of ez-usb */
2673static struct usbtest_info ez1_info = {
2674 .name = "EZ-USB device",
2675 .ep_in = 2,
2676 .ep_out = 2,
2677 .alt = 1,
2678};
2679
2680/* fx2 version of ez-usb */
2681static struct usbtest_info ez2_info = {
2682 .name = "FX2 device",
2683 .ep_in = 6,
2684 .ep_out = 2,
2685 .alt = 1,
2686};
2687
2688/* ezusb family device with dedicated usb test firmware,
2689 */
2690static struct usbtest_info fw_info = {
2691 .name = "usb test device",
2692 .ep_in = 2,
2693 .ep_out = 2,
2694 .alt = 1,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002695 .autoconf = 1, /* iso and ctrl_out need autoconf */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 .ctrl_out = 1,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002697 .iso = 1, /* iso_ep's are #8 in/out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698};
2699
2700/* peripheral running Linux and 'zero.c' test firmware, or
2701 * its user-mode cousin. different versions of this use
2702 * different hardware with the same vendor/product codes.
2703 * host side MUST rely on the endpoint descriptors.
2704 */
2705static struct usbtest_info gz_info = {
2706 .name = "Linux gadget zero",
2707 .autoconf = 1,
2708 .ctrl_out = 1,
Boyan Nedeltchev4b85c622012-11-12 13:06:06 +02002709 .iso = 1,
Amit Virdi457a0952014-08-22 14:36:37 +05302710 .intr = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 .alt = 0,
2712};
2713
2714static struct usbtest_info um_info = {
2715 .name = "Linux user mode test driver",
2716 .autoconf = 1,
2717 .alt = -1,
2718};
2719
2720static struct usbtest_info um2_info = {
2721 .name = "Linux user mode ISO test driver",
2722 .autoconf = 1,
2723 .iso = 1,
2724 .alt = -1,
2725};
2726
2727#ifdef IBOT2
2728/* this is a nice source of high speed bulk data;
2729 * uses an FX2, with firmware provided in the device
2730 */
2731static struct usbtest_info ibot2_info = {
2732 .name = "iBOT2 webcam",
2733 .ep_in = 2,
2734 .alt = -1,
2735};
2736#endif
2737
2738#ifdef GENERIC
2739/* we can use any device to test control traffic */
2740static struct usbtest_info generic_info = {
2741 .name = "Generic USB device",
2742 .alt = -1,
2743};
2744#endif
2745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Németh Márton33b9e162010-01-10 15:34:45 +01002747static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 /*-------------------------------------------------------------*/
2750
2751 /* EZ-USB devices which download firmware to replace (or in our
2752 * case augment) the default device implementation.
2753 */
2754
2755 /* generic EZ-USB FX controller */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002756 { USB_DEVICE(0x0547, 0x2235),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 .driver_info = (unsigned long) &ez1_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002758 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
2760 /* CY3671 development board with EZ-USB FX */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002761 { USB_DEVICE(0x0547, 0x0080),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 .driver_info = (unsigned long) &ez1_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002763 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 /* generic EZ-USB FX2 controller (or development board) */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002766 { USB_DEVICE(0x04b4, 0x8613),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 .driver_info = (unsigned long) &ez2_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002768 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
2770 /* re-enumerated usb test device firmware */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002771 { USB_DEVICE(0xfff0, 0xfff0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 .driver_info = (unsigned long) &fw_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002773 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
2775 /* "Gadget Zero" firmware runs under Linux */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002776 { USB_DEVICE(0x0525, 0xa4a0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 .driver_info = (unsigned long) &gz_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002778 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
2780 /* so does a user-mode variant */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002781 { USB_DEVICE(0x0525, 0xa4a4),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 .driver_info = (unsigned long) &um_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002783 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
2785 /* ... and a user-mode variant that talks iso */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002786 { USB_DEVICE(0x0525, 0xa4a3),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 .driver_info = (unsigned long) &um2_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002788 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
2790#ifdef KEYSPAN_19Qi
2791 /* Keyspan 19qi uses an21xx (original EZ-USB) */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002792 /* this does not coexist with the real Keyspan 19qi driver! */
2793 { USB_DEVICE(0x06cd, 0x010b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 .driver_info = (unsigned long) &ez1_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002795 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796#endif
2797
2798 /*-------------------------------------------------------------*/
2799
2800#ifdef IBOT2
2801 /* iBOT2 makes a nice source of high speed bulk-in data */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002802 /* this does not coexist with a real iBOT2 driver! */
2803 { USB_DEVICE(0x0b62, 0x0059),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 .driver_info = (unsigned long) &ibot2_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002805 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806#endif
2807
2808 /*-------------------------------------------------------------*/
2809
2810#ifdef GENERIC
2811 /* module params can specify devices to use for control tests */
2812 { .driver_info = (unsigned long) &generic_info, },
2813#endif
2814
2815 /*-------------------------------------------------------------*/
2816
2817 { }
2818};
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002819MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
2821static struct usb_driver usbtest_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 .name = "usbtest",
2823 .id_table = id_table,
2824 .probe = usbtest_probe,
Andi Kleenc532b292010-06-01 23:04:41 +02002825 .unlocked_ioctl = usbtest_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 .disconnect = usbtest_disconnect,
David Brownellff7c79e2005-04-22 13:17:00 -07002827 .suspend = usbtest_suspend,
2828 .resume = usbtest_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829};
2830
2831/*-------------------------------------------------------------------------*/
2832
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002833static int __init usbtest_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
2835#ifdef GENERIC
2836 if (vendor)
David Brownell28ffd792008-04-25 18:51:10 -07002837 pr_debug("params: vend=0x%04x prod=0x%04x\n", vendor, product);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838#endif
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002839 return usb_register(&usbtest_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840}
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002841module_init(usbtest_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002843static void __exit usbtest_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002845 usb_deregister(&usbtest_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846}
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002847module_exit(usbtest_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002849MODULE_DESCRIPTION("USB Core/HCD Testing Driver");
2850MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851