usb: renesas_usbhs: fix signed-unsigned return
[linux-2.6-block.git] / drivers / usb / misc / usbtest.c
CommitLineData
1da177e4
LT
1#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>
378f058c 8#include <linux/scatterlist.h>
1cfab028 9#include <linux/mutex.h>
32b36eea 10#include <linux/timer.h>
1da177e4
LT
11#include <linux/usb.h>
12
e5e47465 13#define SIMPLE_IO_TIMEOUT 10000 /* in milliseconds */
1da177e4 14
d0b4652f
AS
15/*-------------------------------------------------------------------------*/
16
17static int override_alt = -1;
18module_param_named(alt, override_alt, int, 0644);
19MODULE_PARM_DESC(alt, ">= 0 to override altsetting selection");
145f48c5 20static void complicated_callback(struct urb *urb);
d0b4652f 21
1da177e4
LT
22/*-------------------------------------------------------------------------*/
23
fabbf219 24/* FIXME make these public somewhere; usbdevfs.h? */
18fc4ebd
DD
25
26/* Parameter for usbtest driver. */
27struct usbtest_param_32 {
fabbf219 28 /* inputs */
18fc4ebd
DD
29 __u32 test_num; /* 0..(TEST_CASES-1) */
30 __u32 iterations;
31 __u32 length;
32 __u32 vary;
33 __u32 sglen;
1da177e4 34
fabbf219 35 /* outputs */
18fc4ebd
DD
36 __s32 duration_sec;
37 __s32 duration_usec;
1da177e4 38};
18fc4ebd
DD
39
40/*
41 * Compat parameter to the usbtest driver.
42 * This supports older user space binaries compiled with 64 bit compiler.
43 */
44struct usbtest_param_64 {
45 /* inputs */
46 __u32 test_num; /* 0..(TEST_CASES-1) */
47 __u32 iterations;
48 __u32 length;
49 __u32 vary;
50 __u32 sglen;
51
52 /* outputs */
53 __s64 duration_sec;
54 __s64 duration_usec;
55};
56
57/* IOCTL interface to the driver. */
58#define USBTEST_REQUEST_32 _IOWR('U', 100, struct usbtest_param_32)
59/* COMPAT IOCTL interface to the driver. */
60#define USBTEST_REQUEST_64 _IOWR('U', 100, struct usbtest_param_64)
1da177e4
LT
61
62/*-------------------------------------------------------------------------*/
63
64#define GENERIC /* let probe() bind using module params */
65
66/* Some devices that can be used for testing will have "real" drivers.
67 * Entries for those need to be enabled here by hand, after disabling
68 * that "real" driver.
69 */
70//#define IBOT2 /* grab iBOT2 webcams */
71//#define KEYSPAN_19Qi /* grab un-renumerated serial adapter */
72
73/*-------------------------------------------------------------------------*/
74
75struct usbtest_info {
76 const char *name;
77 u8 ep_in; /* bulk/intr source */
78 u8 ep_out; /* bulk/intr sink */
fabbf219
MF
79 unsigned autoconf:1;
80 unsigned ctrl_out:1;
81 unsigned iso:1; /* try iso in/out */
457a0955 82 unsigned intr:1; /* try interrupt in/out */
1da177e4
LT
83 int alt;
84};
85
86/* this is accessed only through usbfs ioctl calls.
87 * one ioctl to issue a test ... one lock per device.
88 * tests create other threads if they need them.
89 * urbs and buffers are allocated dynamically,
90 * and data generated deterministically.
91 */
92struct usbtest_dev {
93 struct usb_interface *intf;
94 struct usbtest_info *info;
95 int in_pipe;
96 int out_pipe;
97 int in_iso_pipe;
98 int out_iso_pipe;
457a0955
AV
99 int in_int_pipe;
100 int out_int_pipe;
1da177e4 101 struct usb_endpoint_descriptor *iso_in, *iso_out;
457a0955 102 struct usb_endpoint_descriptor *int_in, *int_out;
1cfab028 103 struct mutex lock;
1da177e4
LT
104
105#define TBUF_SIZE 256
106 u8 *buf;
107};
108
fabbf219 109static struct usb_device *testdev_to_usbdev(struct usbtest_dev *test)
1da177e4 110{
fabbf219 111 return interface_to_usbdev(test->intf);
1da177e4
LT
112}
113
114/* set up all urbs so they can be used with either bulk or interrupt */
115#define INTERRUPT_RATE 1 /* msec/transfer */
116
28ffd79c
DB
117#define ERROR(tdev, fmt, args...) \
118 dev_err(&(tdev)->intf->dev , fmt , ## args)
b6c63937 119#define WARNING(tdev, fmt, args...) \
28ffd79c 120 dev_warn(&(tdev)->intf->dev , fmt , ## args)
1da177e4 121
084fb206 122#define GUARD_BYTE 0xA5
41d3c0b8 123#define MAX_SGLEN 128
084fb206 124
1da177e4
LT
125/*-------------------------------------------------------------------------*/
126
127static int
fabbf219 128get_endpoints(struct usbtest_dev *dev, struct usb_interface *intf)
1da177e4
LT
129{
130 int tmp;
131 struct usb_host_interface *alt;
132 struct usb_host_endpoint *in, *out;
133 struct usb_host_endpoint *iso_in, *iso_out;
457a0955 134 struct usb_host_endpoint *int_in, *int_out;
1da177e4
LT
135 struct usb_device *udev;
136
137 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
138 unsigned ep;
139
140 in = out = NULL;
141 iso_in = iso_out = NULL;
457a0955 142 int_in = int_out = NULL;
1da177e4
LT
143 alt = intf->altsetting + tmp;
144
d0b4652f
AS
145 if (override_alt >= 0 &&
146 override_alt != alt->desc.bAlternateSetting)
147 continue;
148
1da177e4 149 /* take the first altsetting with in-bulk + out-bulk;
70f23fd6 150 * ignore other endpoints and altsettings.
1da177e4
LT
151 */
152 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
153 struct usb_host_endpoint *e;
154
155 e = alt->endpoint + ep;
9a37a503 156 switch (usb_endpoint_type(&e->desc)) {
1da177e4
LT
157 case USB_ENDPOINT_XFER_BULK:
158 break;
457a0955
AV
159 case USB_ENDPOINT_XFER_INT:
160 if (dev->info->intr)
161 goto try_intr;
1da177e4
LT
162 case USB_ENDPOINT_XFER_ISOC:
163 if (dev->info->iso)
164 goto try_iso;
fabbf219 165 /* FALLTHROUGH */
1da177e4
LT
166 default:
167 continue;
168 }
4d823dd2 169 if (usb_endpoint_dir_in(&e->desc)) {
1da177e4
LT
170 if (!in)
171 in = e;
172 } else {
173 if (!out)
174 out = e;
175 }
176 continue;
457a0955
AV
177try_intr:
178 if (usb_endpoint_dir_in(&e->desc)) {
179 if (!int_in)
180 int_in = e;
181 } else {
182 if (!int_out)
183 int_out = e;
184 }
185 continue;
1da177e4 186try_iso:
4d823dd2 187 if (usb_endpoint_dir_in(&e->desc)) {
1da177e4
LT
188 if (!iso_in)
189 iso_in = e;
190 } else {
191 if (!iso_out)
192 iso_out = e;
193 }
194 }
457a0955 195 if ((in && out) || iso_in || iso_out || int_in || int_out)
1da177e4
LT
196 goto found;
197 }
198 return -EINVAL;
199
200found:
fabbf219 201 udev = testdev_to_usbdev(dev);
d0b4652f 202 dev->info->alt = alt->desc.bAlternateSetting;
1da177e4 203 if (alt->desc.bAlternateSetting != 0) {
fabbf219 204 tmp = usb_set_interface(udev,
1da177e4
LT
205 alt->desc.bInterfaceNumber,
206 alt->desc.bAlternateSetting);
207 if (tmp < 0)
208 return tmp;
209 }
210
211 if (in) {
fabbf219 212 dev->in_pipe = usb_rcvbulkpipe(udev,
1da177e4 213 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
fabbf219 214 dev->out_pipe = usb_sndbulkpipe(udev,
1da177e4
LT
215 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
216 }
217 if (iso_in) {
218 dev->iso_in = &iso_in->desc;
fabbf219 219 dev->in_iso_pipe = usb_rcvisocpipe(udev,
1da177e4
LT
220 iso_in->desc.bEndpointAddress
221 & USB_ENDPOINT_NUMBER_MASK);
951fd8ee
ML
222 }
223
224 if (iso_out) {
1da177e4 225 dev->iso_out = &iso_out->desc;
fabbf219 226 dev->out_iso_pipe = usb_sndisocpipe(udev,
1da177e4
LT
227 iso_out->desc.bEndpointAddress
228 & USB_ENDPOINT_NUMBER_MASK);
229 }
457a0955
AV
230
231 if (int_in) {
232 dev->int_in = &int_in->desc;
233 dev->in_int_pipe = usb_rcvintpipe(udev,
234 int_in->desc.bEndpointAddress
235 & USB_ENDPOINT_NUMBER_MASK);
236 }
237
238 if (int_out) {
239 dev->int_out = &int_out->desc;
240 dev->out_int_pipe = usb_sndintpipe(udev,
241 int_out->desc.bEndpointAddress
242 & USB_ENDPOINT_NUMBER_MASK);
243 }
1da177e4
LT
244 return 0;
245}
246
247/*-------------------------------------------------------------------------*/
248
249/* Support for testing basic non-queued I/O streams.
250 *
251 * These just package urbs as requests that can be easily canceled.
252 * Each urb's data buffer is dynamically allocated; callers can fill
253 * them with non-zero test data (or test for it) when appropriate.
254 */
255
fabbf219 256static void simple_callback(struct urb *urb)
1da177e4 257{
cdc97792 258 complete(urb->context);
1da177e4
LT
259}
260
084fb206 261static struct urb *usbtest_alloc_urb(
1da177e4
LT
262 struct usb_device *udev,
263 int pipe,
084fb206
MF
264 unsigned long bytes,
265 unsigned transfer_flags,
457a0955 266 unsigned offset,
145f48c5
PC
267 u8 bInterval,
268 usb_complete_t complete_fn)
1da177e4
LT
269{
270 struct urb *urb;
271
fabbf219 272 urb = usb_alloc_urb(0, GFP_KERNEL);
1da177e4
LT
273 if (!urb)
274 return urb;
457a0955
AV
275
276 if (bInterval)
145f48c5 277 usb_fill_int_urb(urb, udev, pipe, NULL, bytes, complete_fn,
457a0955
AV
278 NULL, bInterval);
279 else
145f48c5 280 usb_fill_bulk_urb(urb, udev, pipe, NULL, bytes, complete_fn,
457a0955
AV
281 NULL);
282
1da177e4
LT
283 urb->interval = (udev->speed == USB_SPEED_HIGH)
284 ? (INTERRUPT_RATE << 3)
285 : INTERRUPT_RATE;
084fb206 286 urb->transfer_flags = transfer_flags;
fabbf219 287 if (usb_pipein(pipe))
1da177e4 288 urb->transfer_flags |= URB_SHORT_NOT_OK;
084fb206 289
26186e5f
CY
290 if ((bytes + offset) == 0)
291 return urb;
292
084fb206
MF
293 if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
294 urb->transfer_buffer = usb_alloc_coherent(udev, bytes + offset,
295 GFP_KERNEL, &urb->transfer_dma);
296 else
297 urb->transfer_buffer = kmalloc(bytes + offset, GFP_KERNEL);
298
1da177e4 299 if (!urb->transfer_buffer) {
fabbf219 300 usb_free_urb(urb);
084fb206
MF
301 return NULL;
302 }
303
304 /* To test unaligned transfers add an offset and fill the
305 unused memory with a guard value */
306 if (offset) {
307 memset(urb->transfer_buffer, GUARD_BYTE, offset);
308 urb->transfer_buffer += offset;
309 if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
310 urb->transfer_dma += offset;
311 }
312
313 /* For inbound transfers use guard byte so that test fails if
314 data not correctly copied */
315 memset(urb->transfer_buffer,
316 usb_pipein(urb->pipe) ? GUARD_BYTE : 0,
317 bytes);
1da177e4
LT
318 return urb;
319}
320
084fb206
MF
321static struct urb *simple_alloc_urb(
322 struct usb_device *udev,
323 int pipe,
457a0955
AV
324 unsigned long bytes,
325 u8 bInterval)
084fb206 326{
457a0955 327 return usbtest_alloc_urb(udev, pipe, bytes, URB_NO_TRANSFER_DMA_MAP, 0,
145f48c5
PC
328 bInterval, simple_callback);
329}
330
331static struct urb *complicated_alloc_urb(
332 struct usb_device *udev,
333 int pipe,
334 unsigned long bytes,
335 u8 bInterval)
336{
337 return usbtest_alloc_urb(udev, pipe, bytes, URB_NO_TRANSFER_DMA_MAP, 0,
338 bInterval, complicated_callback);
084fb206
MF
339}
340
fabbf219 341static unsigned pattern;
7f4e9854
VP
342static unsigned mod_pattern;
343module_param_named(pattern, mod_pattern, uint, S_IRUGO | S_IWUSR);
344MODULE_PARM_DESC(mod_pattern, "i/o pattern (0 == zeroes)");
1da177e4 345
b9a6e8e1
AS
346static unsigned get_maxpacket(struct usb_device *udev, int pipe)
347{
348 struct usb_host_endpoint *ep;
349
350 ep = usb_pipe_endpoint(udev, pipe);
351 return le16_to_cpup(&ep->desc.wMaxPacketSize);
352}
353
354static void simple_fill_buf(struct urb *urb)
1da177e4
LT
355{
356 unsigned i;
357 u8 *buf = urb->transfer_buffer;
358 unsigned len = urb->transfer_buffer_length;
b9a6e8e1 359 unsigned maxpacket;
1da177e4
LT
360
361 switch (pattern) {
362 default:
fabbf219 363 /* FALLTHROUGH */
1da177e4 364 case 0:
fabbf219 365 memset(buf, 0, len);
1da177e4
LT
366 break;
367 case 1: /* mod63 */
b9a6e8e1 368 maxpacket = get_maxpacket(urb->dev, urb->pipe);
1da177e4 369 for (i = 0; i < len; i++)
b9a6e8e1 370 *buf++ = (u8) ((i % maxpacket) % 63);
1da177e4
LT
371 break;
372 }
373}
374
304b0b57 375static inline unsigned long buffer_offset(void *buf)
084fb206 376{
304b0b57 377 return (unsigned long)buf & (ARCH_KMALLOC_MINALIGN - 1);
084fb206
MF
378}
379
380static int check_guard_bytes(struct usbtest_dev *tdev, struct urb *urb)
381{
382 u8 *buf = urb->transfer_buffer;
383 u8 *guard = buf - buffer_offset(buf);
384 unsigned i;
385
386 for (i = 0; guard < buf; i++, guard++) {
387 if (*guard != GUARD_BYTE) {
388 ERROR(tdev, "guard byte[%d] %d (not %d)\n",
389 i, *guard, GUARD_BYTE);
390 return -EINVAL;
391 }
392 }
393 return 0;
394}
395
396static int simple_check_buf(struct usbtest_dev *tdev, struct urb *urb)
1da177e4
LT
397{
398 unsigned i;
399 u8 expected;
400 u8 *buf = urb->transfer_buffer;
401 unsigned len = urb->actual_length;
b9a6e8e1 402 unsigned maxpacket = get_maxpacket(urb->dev, urb->pipe);
1da177e4 403
084fb206
MF
404 int ret = check_guard_bytes(tdev, urb);
405 if (ret)
406 return ret;
407
1da177e4
LT
408 for (i = 0; i < len; i++, buf++) {
409 switch (pattern) {
410 /* all-zeroes has no synchronization issues */
411 case 0:
412 expected = 0;
413 break;
414 /* mod63 stays in sync with short-terminated transfers,
415 * or otherwise when host and gadget agree on how large
416 * each usb transfer request should be. resync is done
417 * with set_interface or set_config.
418 */
419 case 1: /* mod63 */
b9a6e8e1 420 expected = (i % maxpacket) % 63;
1da177e4
LT
421 break;
422 /* always fail unsupported patterns */
423 default:
424 expected = !*buf;
425 break;
426 }
427 if (*buf == expected)
428 continue;
28ffd79c 429 ERROR(tdev, "buf[%d] = %d (not %d)\n", i, *buf, expected);
1da177e4
LT
430 return -EINVAL;
431 }
432 return 0;
433}
434
fabbf219 435static void simple_free_urb(struct urb *urb)
1da177e4 436{
304b0b57 437 unsigned long offset = buffer_offset(urb->transfer_buffer);
084fb206
MF
438
439 if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
440 usb_free_coherent(
441 urb->dev,
442 urb->transfer_buffer_length + offset,
443 urb->transfer_buffer - offset,
444 urb->transfer_dma - offset);
445 else
446 kfree(urb->transfer_buffer - offset);
fabbf219 447 usb_free_urb(urb);
1da177e4
LT
448}
449
fabbf219 450static int simple_io(
28ffd79c 451 struct usbtest_dev *tdev,
1da177e4
LT
452 struct urb *urb,
453 int iterations,
454 int vary,
455 int expected,
456 const char *label
457)
458{
459 struct usb_device *udev = urb->dev;
460 int max = urb->transfer_buffer_length;
461 struct completion completion;
462 int retval = 0;
e5e47465 463 unsigned long expire;
1da177e4
LT
464
465 urb->context = &completion;
466 while (retval == 0 && iterations-- > 0) {
fabbf219 467 init_completion(&completion);
7c79d094 468 if (usb_pipeout(urb->pipe)) {
fabbf219 469 simple_fill_buf(urb);
7c79d094
SAS
470 urb->transfer_flags |= URB_ZERO_PACKET;
471 }
fabbf219
MF
472 retval = usb_submit_urb(urb, GFP_KERNEL);
473 if (retval != 0)
1da177e4
LT
474 break;
475
e5e47465
RQ
476 expire = msecs_to_jiffies(SIMPLE_IO_TIMEOUT);
477 if (!wait_for_completion_timeout(&completion, expire)) {
478 usb_kill_urb(urb);
479 retval = (urb->status == -ENOENT ?
480 -ETIMEDOUT : urb->status);
481 } else {
482 retval = urb->status;
483 }
484
1da177e4 485 urb->dev = udev;
fabbf219 486 if (retval == 0 && usb_pipein(urb->pipe))
28ffd79c 487 retval = simple_check_buf(tdev, urb);
1da177e4
LT
488
489 if (vary) {
490 int len = urb->transfer_buffer_length;
491
492 len += vary;
493 len %= max;
494 if (len == 0)
495 len = (vary < max) ? vary : max;
496 urb->transfer_buffer_length = len;
497 }
498
499 /* FIXME if endpoint halted, clear halt (and log) */
500 }
501 urb->transfer_buffer_length = max;
502
503 if (expected != retval)
28ffd79c 504 dev_err(&udev->dev,
1da177e4
LT
505 "%s failed, iterations left %d, status %d (not %d)\n",
506 label, iterations, retval, expected);
507 return retval;
508}
509
510
511/*-------------------------------------------------------------------------*/
512
513/* We use scatterlist primitives to test queued I/O.
514 * Yes, this also tests the scatterlist primitives.
515 */
516
fabbf219 517static void free_sglist(struct scatterlist *sg, int nents)
1da177e4
LT
518{
519 unsigned i;
28ffd79c 520
1da177e4
LT
521 if (!sg)
522 return;
523 for (i = 0; i < nents; i++) {
45711f1a 524 if (!sg_page(&sg[i]))
1da177e4 525 continue;
fabbf219 526 kfree(sg_virt(&sg[i]));
1da177e4 527 }
fabbf219 528 kfree(sg);
1da177e4
LT
529}
530
531static struct scatterlist *
b9a6e8e1 532alloc_sglist(int nents, int max, int vary, struct usbtest_dev *dev, int pipe)
1da177e4
LT
533{
534 struct scatterlist *sg;
535 unsigned i;
536 unsigned size = max;
b9a6e8e1
AS
537 unsigned maxpacket =
538 get_maxpacket(interface_to_usbdev(dev->intf), pipe);
1da177e4 539
564e6989
DC
540 if (max == 0)
541 return NULL;
542
f55055b4 543 sg = kmalloc_array(nents, sizeof(*sg), GFP_KERNEL);
1da177e4
LT
544 if (!sg)
545 return NULL;
4756febb 546 sg_init_table(sg, nents);
1da177e4
LT
547
548 for (i = 0; i < nents; i++) {
549 char *buf;
8b524901 550 unsigned j;
1da177e4 551
fabbf219 552 buf = kzalloc(size, GFP_KERNEL);
1da177e4 553 if (!buf) {
fabbf219 554 free_sglist(sg, i);
1da177e4
LT
555 return NULL;
556 }
1da177e4
LT
557
558 /* kmalloc pages are always physically contiguous! */
4756febb 559 sg_set_buf(&sg[i], buf, size);
1da177e4 560
8b524901
DB
561 switch (pattern) {
562 case 0:
563 /* already zeroed */
564 break;
565 case 1:
566 for (j = 0; j < size; j++)
b9a6e8e1 567 *buf++ = (u8) ((j % maxpacket) % 63);
8b524901
DB
568 break;
569 }
570
1da177e4
LT
571 if (vary) {
572 size += vary;
573 size %= max;
574 if (size == 0)
575 size = (vary < max) ? vary : max;
576 }
577 }
578
579 return sg;
580}
581
32b36eea
AS
582static void sg_timeout(unsigned long _req)
583{
584 struct usb_sg_request *req = (struct usb_sg_request *) _req;
585
586 req->status = -ETIMEDOUT;
587 usb_sg_cancel(req);
588}
589
fabbf219 590static int perform_sglist(
28ffd79c 591 struct usbtest_dev *tdev,
1da177e4
LT
592 unsigned iterations,
593 int pipe,
594 struct usb_sg_request *req,
595 struct scatterlist *sg,
596 int nents
597)
598{
28ffd79c 599 struct usb_device *udev = testdev_to_usbdev(tdev);
1da177e4 600 int retval = 0;
32b36eea
AS
601 struct timer_list sg_timer;
602
603 setup_timer_on_stack(&sg_timer, sg_timeout, (unsigned long) req);
1da177e4
LT
604
605 while (retval == 0 && iterations-- > 0) {
fabbf219 606 retval = usb_sg_init(req, udev, pipe,
1da177e4
LT
607 (udev->speed == USB_SPEED_HIGH)
608 ? (INTERRUPT_RATE << 3)
609 : INTERRUPT_RATE,
e94b1766 610 sg, nents, 0, GFP_KERNEL);
28ffd79c 611
1da177e4
LT
612 if (retval)
613 break;
32b36eea
AS
614 mod_timer(&sg_timer, jiffies +
615 msecs_to_jiffies(SIMPLE_IO_TIMEOUT));
fabbf219 616 usb_sg_wait(req);
32b36eea 617 del_timer_sync(&sg_timer);
1da177e4
LT
618 retval = req->status;
619
8b524901
DB
620 /* FIXME check resulting data pattern */
621
1da177e4
LT
622 /* FIXME if endpoint halted, clear halt (and log) */
623 }
624
fabbf219
MF
625 /* FIXME for unlink or fault handling tests, don't report
626 * failure if retval is as we expected ...
627 */
1da177e4 628 if (retval)
28ffd79c
DB
629 ERROR(tdev, "perform_sglist failed, "
630 "iterations left %d, status %d\n",
1da177e4
LT
631 iterations, retval);
632 return retval;
633}
634
635
636/*-------------------------------------------------------------------------*/
637
638/* unqueued control message testing
639 *
640 * there's a nice set of device functional requirements in chapter 9 of the
641 * usb 2.0 spec, which we can apply to ANY device, even ones that don't use
642 * special test firmware.
643 *
644 * we know the device is configured (or suspended) by the time it's visible
645 * through usbfs. we can't change that, so we won't test enumeration (which
646 * worked 'well enough' to get here, this time), power management (ditto),
647 * or remote wakeup (which needs human interaction).
648 */
649
650static unsigned realworld = 1;
fabbf219
MF
651module_param(realworld, uint, 0);
652MODULE_PARM_DESC(realworld, "clear to demand stricter spec compliance");
1da177e4 653
fabbf219 654static int get_altsetting(struct usbtest_dev *dev)
1da177e4
LT
655{
656 struct usb_interface *iface = dev->intf;
fabbf219 657 struct usb_device *udev = interface_to_usbdev(iface);
1da177e4
LT
658 int retval;
659
fabbf219 660 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1da177e4 661 USB_REQ_GET_INTERFACE, USB_DIR_IN|USB_RECIP_INTERFACE,
fabbf219 662 0, iface->altsetting[0].desc.bInterfaceNumber,
1da177e4
LT
663 dev->buf, 1, USB_CTRL_GET_TIMEOUT);
664 switch (retval) {
665 case 1:
fabbf219 666 return dev->buf[0];
1da177e4
LT
667 case 0:
668 retval = -ERANGE;
fabbf219 669 /* FALLTHROUGH */
1da177e4
LT
670 default:
671 return retval;
672 }
673}
674
fabbf219 675static int set_altsetting(struct usbtest_dev *dev, int alternate)
1da177e4
LT
676{
677 struct usb_interface *iface = dev->intf;
678 struct usb_device *udev;
679
680 if (alternate < 0 || alternate >= 256)
681 return -EINVAL;
682
fabbf219
MF
683 udev = interface_to_usbdev(iface);
684 return usb_set_interface(udev,
685 iface->altsetting[0].desc.bInterfaceNumber,
1da177e4
LT
686 alternate);
687}
688
28ffd79c 689static int is_good_config(struct usbtest_dev *tdev, int len)
1da177e4
LT
690{
691 struct usb_config_descriptor *config;
28ffd79c 692
f55055b4 693 if (len < sizeof(*config))
1da177e4 694 return 0;
28ffd79c 695 config = (struct usb_config_descriptor *) tdev->buf;
1da177e4
LT
696
697 switch (config->bDescriptorType) {
698 case USB_DT_CONFIG:
699 case USB_DT_OTHER_SPEED_CONFIG:
700 if (config->bLength != 9) {
28ffd79c 701 ERROR(tdev, "bogus config descriptor length\n");
1da177e4
LT
702 return 0;
703 }
704 /* this bit 'must be 1' but often isn't */
705 if (!realworld && !(config->bmAttributes & 0x80)) {
28ffd79c 706 ERROR(tdev, "high bit of config attributes not set\n");
1da177e4
LT
707 return 0;
708 }
709 if (config->bmAttributes & 0x1f) { /* reserved == 0 */
28ffd79c 710 ERROR(tdev, "reserved config bits set\n");
1da177e4
LT
711 return 0;
712 }
713 break;
714 default:
715 return 0;
716 }
717
fabbf219 718 if (le16_to_cpu(config->wTotalLength) == len) /* read it all */
1da177e4 719 return 1;
fabbf219 720 if (le16_to_cpu(config->wTotalLength) >= TBUF_SIZE) /* max partial read */
1da177e4 721 return 1;
28ffd79c 722 ERROR(tdev, "bogus config descriptor read size\n");
1da177e4
LT
723 return 0;
724}
725
82f92672
HR
726static int is_good_ext(struct usbtest_dev *tdev, u8 *buf)
727{
728 struct usb_ext_cap_descriptor *ext;
729 u32 attr;
730
731 ext = (struct usb_ext_cap_descriptor *) buf;
732
733 if (ext->bLength != USB_DT_USB_EXT_CAP_SIZE) {
734 ERROR(tdev, "bogus usb 2.0 extension descriptor length\n");
735 return 0;
736 }
737
738 attr = le32_to_cpu(ext->bmAttributes);
875bc23a
HR
739 /* bits[1:15] is used and others are reserved */
740 if (attr & ~0xfffe) { /* reserved == 0 */
82f92672
HR
741 ERROR(tdev, "reserved bits set\n");
742 return 0;
743 }
744
745 return 1;
746}
747
b8fef795
HR
748static int is_good_ss_cap(struct usbtest_dev *tdev, u8 *buf)
749{
750 struct usb_ss_cap_descriptor *ss;
751
752 ss = (struct usb_ss_cap_descriptor *) buf;
753
754 if (ss->bLength != USB_DT_USB_SS_CAP_SIZE) {
755 ERROR(tdev, "bogus superspeed device capability descriptor length\n");
756 return 0;
757 }
758
759 /*
760 * only bit[1] of bmAttributes is used for LTM and others are
761 * reserved
762 */
763 if (ss->bmAttributes & ~0x02) { /* reserved == 0 */
764 ERROR(tdev, "reserved bits set in bmAttributes\n");
765 return 0;
766 }
767
768 /* bits[0:3] of wSpeedSupported is used and others are reserved */
769 if (le16_to_cpu(ss->wSpeedSupported) & ~0x0f) { /* reserved == 0 */
770 ERROR(tdev, "reserved bits set in wSpeedSupported\n");
771 return 0;
772 }
773
774 return 1;
775}
776
8e29217f
HR
777static int is_good_con_id(struct usbtest_dev *tdev, u8 *buf)
778{
779 struct usb_ss_container_id_descriptor *con_id;
780
781 con_id = (struct usb_ss_container_id_descriptor *) buf;
782
783 if (con_id->bLength != USB_DT_USB_SS_CONTN_ID_SIZE) {
784 ERROR(tdev, "bogus container id descriptor length\n");
785 return 0;
786 }
787
788 if (con_id->bReserved) { /* reserved == 0 */
789 ERROR(tdev, "reserved bits set\n");
790 return 0;
791 }
792
793 return 1;
794}
795
1da177e4
LT
796/* sanity test for standard requests working with usb_control_mesg() and some
797 * of the utility functions which use it.
798 *
799 * this doesn't test how endpoint halts behave or data toggles get set, since
800 * we won't do I/O to bulk/interrupt endpoints here (which is how to change
801 * halt or toggle). toggle testing is impractical without support from hcds.
802 *
803 * this avoids failing devices linux would normally work with, by not testing
804 * config/altsetting operations for devices that only support their defaults.
805 * such devices rarely support those needless operations.
806 *
807 * NOTE that since this is a sanity test, it's not examining boundary cases
808 * to see if usbcore, hcd, and device all behave right. such testing would
809 * involve varied read sizes and other operation sequences.
810 */
fabbf219 811static int ch9_postconfig(struct usbtest_dev *dev)
1da177e4
LT
812{
813 struct usb_interface *iface = dev->intf;
fabbf219 814 struct usb_device *udev = interface_to_usbdev(iface);
1da177e4
LT
815 int i, alt, retval;
816
817 /* [9.2.3] if there's more than one altsetting, we need to be able to
818 * set and get each one. mostly trusts the descriptors from usbcore.
819 */
820 for (i = 0; i < iface->num_altsetting; i++) {
821
822 /* 9.2.3 constrains the range here */
fabbf219 823 alt = iface->altsetting[i].desc.bAlternateSetting;
1da177e4 824 if (alt < 0 || alt >= iface->num_altsetting) {
28ffd79c 825 dev_err(&iface->dev,
1da177e4
LT
826 "invalid alt [%d].bAltSetting = %d\n",
827 i, alt);
828 }
829
830 /* [real world] get/set unimplemented if there's only one */
831 if (realworld && iface->num_altsetting == 1)
832 continue;
833
834 /* [9.4.10] set_interface */
fabbf219 835 retval = set_altsetting(dev, alt);
1da177e4 836 if (retval) {
28ffd79c 837 dev_err(&iface->dev, "can't set_interface = %d, %d\n",
1da177e4
LT
838 alt, retval);
839 return retval;
840 }
841
842 /* [9.4.4] get_interface always works */
fabbf219 843 retval = get_altsetting(dev);
1da177e4 844 if (retval != alt) {
28ffd79c 845 dev_err(&iface->dev, "get alt should be %d, was %d\n",
1da177e4
LT
846 alt, retval);
847 return (retval < 0) ? retval : -EDOM;
848 }
849
850 }
851
852 /* [real world] get_config unimplemented if there's only one */
853 if (!realworld || udev->descriptor.bNumConfigurations != 1) {
854 int expected = udev->actconfig->desc.bConfigurationValue;
855
856 /* [9.4.2] get_configuration always works
857 * ... although some cheap devices (like one TI Hub I've got)
858 * won't return config descriptors except before set_config.
859 */
fabbf219 860 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1da177e4
LT
861 USB_REQ_GET_CONFIGURATION,
862 USB_DIR_IN | USB_RECIP_DEVICE,
863 0, 0, dev->buf, 1, USB_CTRL_GET_TIMEOUT);
fabbf219 864 if (retval != 1 || dev->buf[0] != expected) {
28ffd79c 865 dev_err(&iface->dev, "get config --> %d %d (1 %d)\n",
ff7c79e4 866 retval, dev->buf[0], expected);
1da177e4
LT
867 return (retval < 0) ? retval : -EDOM;
868 }
869 }
870
871 /* there's always [9.4.3] a device descriptor [9.6.1] */
fabbf219 872 retval = usb_get_descriptor(udev, USB_DT_DEVICE, 0,
f55055b4
HR
873 dev->buf, sizeof(udev->descriptor));
874 if (retval != sizeof(udev->descriptor)) {
28ffd79c 875 dev_err(&iface->dev, "dev descriptor --> %d\n", retval);
1da177e4
LT
876 return (retval < 0) ? retval : -EDOM;
877 }
878
9d3bd768
HR
879 /*
880 * there's always [9.4.3] a bos device descriptor [9.6.2] in USB
881 * 3.0 spec
882 */
f625099f 883 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0210) {
82f92672
HR
884 struct usb_bos_descriptor *bos = NULL;
885 struct usb_dev_cap_header *header = NULL;
886 unsigned total, num, length;
887 u8 *buf;
888
9d3bd768
HR
889 retval = usb_get_descriptor(udev, USB_DT_BOS, 0, dev->buf,
890 sizeof(*udev->bos->desc));
891 if (retval != sizeof(*udev->bos->desc)) {
892 dev_err(&iface->dev, "bos descriptor --> %d\n", retval);
893 return (retval < 0) ? retval : -EDOM;
894 }
82f92672
HR
895
896 bos = (struct usb_bos_descriptor *)dev->buf;
897 total = le16_to_cpu(bos->wTotalLength);
898 num = bos->bNumDeviceCaps;
899
900 if (total > TBUF_SIZE)
901 total = TBUF_SIZE;
902
903 /*
904 * get generic device-level capability descriptors [9.6.2]
905 * in USB 3.0 spec
906 */
907 retval = usb_get_descriptor(udev, USB_DT_BOS, 0, dev->buf,
908 total);
909 if (retval != total) {
910 dev_err(&iface->dev, "bos descriptor set --> %d\n",
911 retval);
912 return (retval < 0) ? retval : -EDOM;
913 }
914
915 length = sizeof(*udev->bos->desc);
916 buf = dev->buf;
917 for (i = 0; i < num; i++) {
918 buf += length;
919 if (buf + sizeof(struct usb_dev_cap_header) >
920 dev->buf + total)
921 break;
922
923 header = (struct usb_dev_cap_header *)buf;
924 length = header->bLength;
925
926 if (header->bDescriptorType !=
927 USB_DT_DEVICE_CAPABILITY) {
928 dev_warn(&udev->dev, "not device capability descriptor, skip\n");
929 continue;
930 }
931
932 switch (header->bDevCapabilityType) {
933 case USB_CAP_TYPE_EXT:
934 if (buf + USB_DT_USB_EXT_CAP_SIZE >
935 dev->buf + total ||
936 !is_good_ext(dev, buf)) {
937 dev_err(&iface->dev, "bogus usb 2.0 extension descriptor\n");
938 return -EDOM;
939 }
940 break;
b8fef795
HR
941 case USB_SS_CAP_TYPE:
942 if (buf + USB_DT_USB_SS_CAP_SIZE >
943 dev->buf + total ||
944 !is_good_ss_cap(dev, buf)) {
945 dev_err(&iface->dev, "bogus superspeed device capability descriptor\n");
946 return -EDOM;
947 }
948 break;
8e29217f
HR
949 case CONTAINER_ID_TYPE:
950 if (buf + USB_DT_USB_SS_CONTN_ID_SIZE >
951 dev->buf + total ||
952 !is_good_con_id(dev, buf)) {
953 dev_err(&iface->dev, "bogus container id descriptor\n");
954 return -EDOM;
955 }
956 break;
82f92672
HR
957 default:
958 break;
959 }
960 }
9d3bd768
HR
961 }
962
1da177e4
LT
963 /* there's always [9.4.3] at least one config descriptor [9.6.3] */
964 for (i = 0; i < udev->descriptor.bNumConfigurations; i++) {
fabbf219 965 retval = usb_get_descriptor(udev, USB_DT_CONFIG, i,
1da177e4 966 dev->buf, TBUF_SIZE);
28ffd79c
DB
967 if (!is_good_config(dev, retval)) {
968 dev_err(&iface->dev,
1da177e4
LT
969 "config [%d] descriptor --> %d\n",
970 i, retval);
971 return (retval < 0) ? retval : -EDOM;
972 }
973
fabbf219
MF
974 /* FIXME cross-checking udev->config[i] to make sure usbcore
975 * parsed it right (etc) would be good testing paranoia
976 */
1da177e4
LT
977 }
978
979 /* and sometimes [9.2.6.6] speed dependent descriptors */
980 if (le16_to_cpu(udev->descriptor.bcdUSB) == 0x0200) {
fabbf219 981 struct usb_qualifier_descriptor *d = NULL;
1da177e4
LT
982
983 /* device qualifier [9.6.2] */
fabbf219 984 retval = usb_get_descriptor(udev,
1da177e4 985 USB_DT_DEVICE_QUALIFIER, 0, dev->buf,
fabbf219 986 sizeof(struct usb_qualifier_descriptor));
1da177e4
LT
987 if (retval == -EPIPE) {
988 if (udev->speed == USB_SPEED_HIGH) {
28ffd79c 989 dev_err(&iface->dev,
1da177e4
LT
990 "hs dev qualifier --> %d\n",
991 retval);
992 return (retval < 0) ? retval : -EDOM;
993 }
994 /* usb2.0 but not high-speed capable; fine */
fabbf219 995 } else if (retval != sizeof(struct usb_qualifier_descriptor)) {
28ffd79c 996 dev_err(&iface->dev, "dev qualifier --> %d\n", retval);
1da177e4
LT
997 return (retval < 0) ? retval : -EDOM;
998 } else
999 d = (struct usb_qualifier_descriptor *) dev->buf;
1000
1001 /* might not have [9.6.2] any other-speed configs [9.6.4] */
1002 if (d) {
1003 unsigned max = d->bNumConfigurations;
1004 for (i = 0; i < max; i++) {
fabbf219 1005 retval = usb_get_descriptor(udev,
1da177e4
LT
1006 USB_DT_OTHER_SPEED_CONFIG, i,
1007 dev->buf, TBUF_SIZE);
28ffd79c
DB
1008 if (!is_good_config(dev, retval)) {
1009 dev_err(&iface->dev,
1da177e4
LT
1010 "other speed config --> %d\n",
1011 retval);
1012 return (retval < 0) ? retval : -EDOM;
1013 }
1014 }
1015 }
1016 }
fabbf219 1017 /* FIXME fetch strings from at least the device descriptor */
1da177e4
LT
1018
1019 /* [9.4.5] get_status always works */
fabbf219 1020 retval = usb_get_status(udev, USB_RECIP_DEVICE, 0, dev->buf);
15b7336e 1021 if (retval) {
28ffd79c 1022 dev_err(&iface->dev, "get dev status --> %d\n", retval);
15b7336e 1023 return retval;
1da177e4
LT
1024 }
1025
fabbf219
MF
1026 /* FIXME configuration.bmAttributes says if we could try to set/clear
1027 * the device's remote wakeup feature ... if we can, test that here
1028 */
1da177e4 1029
fabbf219
MF
1030 retval = usb_get_status(udev, USB_RECIP_INTERFACE,
1031 iface->altsetting[0].desc.bInterfaceNumber, dev->buf);
15b7336e 1032 if (retval) {
28ffd79c 1033 dev_err(&iface->dev, "get interface status --> %d\n", retval);
15b7336e 1034 return retval;
1da177e4 1035 }
fabbf219 1036 /* FIXME get status for each endpoint in the interface */
28ffd79c 1037
1da177e4
LT
1038 return 0;
1039}
1040
1041/*-------------------------------------------------------------------------*/
1042
1043/* use ch9 requests to test whether:
1044 * (a) queues work for control, keeping N subtests queued and
1045 * active (auto-resubmit) for M loops through the queue.
1046 * (b) protocol stalls (control-only) will autorecover.
1047 * it's not like bulk/intr; no halt clearing.
1048 * (c) short control reads are reported and handled.
1049 * (d) queues are always processed in-order
1050 */
1051
1052struct ctrl_ctx {
1053 spinlock_t lock;
1054 struct usbtest_dev *dev;
1055 struct completion complete;
1056 unsigned count;
1057 unsigned pending;
1058 int status;
1059 struct urb **urb;
18fc4ebd 1060 struct usbtest_param_32 *param;
1da177e4
LT
1061 int last;
1062};
1063
c952a8ba 1064#define NUM_SUBCASES 16 /* how many test subcases here? */
1da177e4
LT
1065
1066struct subcase {
1067 struct usb_ctrlrequest setup;
1068 int number;
1069 int expected;
1070};
1071
fabbf219 1072static void ctrl_complete(struct urb *urb)
1da177e4
LT
1073{
1074 struct ctrl_ctx *ctx = urb->context;
1075 struct usb_ctrlrequest *reqp;
1076 struct subcase *subcase;
1077 int status = urb->status;
1078
1079 reqp = (struct usb_ctrlrequest *)urb->setup_packet;
fabbf219 1080 subcase = container_of(reqp, struct subcase, setup);
1da177e4 1081
fabbf219 1082 spin_lock(&ctx->lock);
1da177e4
LT
1083 ctx->count--;
1084 ctx->pending--;
1085
1086 /* queue must transfer and complete in fifo order, unless
1087 * usb_unlink_urb() is used to unlink something not at the
1088 * physical queue head (not tested).
1089 */
1090 if (subcase->number > 0) {
1091 if ((subcase->number - ctx->last) != 1) {
28ffd79c
DB
1092 ERROR(ctx->dev,
1093 "subcase %d completed out of order, last %d\n",
1094 subcase->number, ctx->last);
1da177e4
LT
1095 status = -EDOM;
1096 ctx->last = subcase->number;
1097 goto error;
1098 }
1099 }
1100 ctx->last = subcase->number;
1101
1102 /* succeed or fault in only one way? */
1103 if (status == subcase->expected)
1104 status = 0;
1105
1106 /* async unlink for cleanup? */
1107 else if (status != -ECONNRESET) {
1108
1109 /* some faults are allowed, not required */
1110 if (subcase->expected > 0 && (
59d99785
GKH
1111 ((status == -subcase->expected /* happened */
1112 || status == 0)))) /* didn't */
1da177e4
LT
1113 status = 0;
1114 /* sometimes more than one fault is allowed */
1115 else if (subcase->number == 12 && status == -EPIPE)
1116 status = 0;
1117 else
28ffd79c 1118 ERROR(ctx->dev, "subtest %d error, status %d\n",
1da177e4
LT
1119 subcase->number, status);
1120 }
1121
1122 /* unexpected status codes mean errors; ideally, in hardware */
1123 if (status) {
1124error:
1125 if (ctx->status == 0) {
1126 int i;
1127
1128 ctx->status = status;
28ffd79c
DB
1129 ERROR(ctx->dev, "control queue %02x.%02x, err %d, "
1130 "%d left, subcase %d, len %d/%d\n",
1da177e4 1131 reqp->bRequestType, reqp->bRequest,
28ffd79c
DB
1132 status, ctx->count, subcase->number,
1133 urb->actual_length,
1134 urb->transfer_buffer_length);
1da177e4
LT
1135
1136 /* FIXME this "unlink everything" exit route should
1137 * be a separate test case.
1138 */
1139
1140 /* unlink whatever's still pending */
1141 for (i = 1; i < ctx->param->sglen; i++) {
fabbf219
MF
1142 struct urb *u = ctx->urb[
1143 (i + subcase->number)
1144 % ctx->param->sglen];
1da177e4
LT
1145
1146 if (u == urb || !u->dev)
1147 continue;
caa2a122 1148 spin_unlock(&ctx->lock);
fabbf219 1149 status = usb_unlink_urb(u);
caa2a122 1150 spin_lock(&ctx->lock);
1da177e4
LT
1151 switch (status) {
1152 case -EINPROGRESS:
1153 case -EBUSY:
1154 case -EIDRM:
1155 continue;
1156 default:
28ffd79c
DB
1157 ERROR(ctx->dev, "urb unlink --> %d\n",
1158 status);
1da177e4
LT
1159 }
1160 }
1161 status = ctx->status;
1162 }
1163 }
1164
1165 /* resubmit if we need to, else mark this as done */
1166 if ((status == 0) && (ctx->pending < ctx->count)) {
fabbf219
MF
1167 status = usb_submit_urb(urb, GFP_ATOMIC);
1168 if (status != 0) {
28ffd79c
DB
1169 ERROR(ctx->dev,
1170 "can't resubmit ctrl %02x.%02x, err %d\n",
1da177e4
LT
1171 reqp->bRequestType, reqp->bRequest, status);
1172 urb->dev = NULL;
1173 } else
1174 ctx->pending++;
1175 } else
1176 urb->dev = NULL;
28ffd79c 1177
1da177e4
LT
1178 /* signal completion when nothing's queued */
1179 if (ctx->pending == 0)
fabbf219
MF
1180 complete(&ctx->complete);
1181 spin_unlock(&ctx->lock);
1da177e4
LT
1182}
1183
1184static int
18fc4ebd 1185test_ctrl_queue(struct usbtest_dev *dev, struct usbtest_param_32 *param)
1da177e4 1186{
fabbf219 1187 struct usb_device *udev = testdev_to_usbdev(dev);
1da177e4
LT
1188 struct urb **urb;
1189 struct ctrl_ctx context;
1190 int i;
1191
e65cdfae
XW
1192 if (param->sglen == 0 || param->iterations > UINT_MAX / param->sglen)
1193 return -EOPNOTSUPP;
1194
fabbf219 1195 spin_lock_init(&context.lock);
1da177e4 1196 context.dev = dev;
fabbf219 1197 init_completion(&context.complete);
1da177e4
LT
1198 context.count = param->sglen * param->iterations;
1199 context.pending = 0;
1200 context.status = -ENOMEM;
1201 context.param = param;
1202 context.last = -1;
1203
1204 /* allocate and init the urbs we'll queue.
1205 * as with bulk/intr sglists, sglen is the queue depth; it also
1206 * controls which subtests run (more tests than sglen) or rerun.
1207 */
e94b1766 1208 urb = kcalloc(param->sglen, sizeof(struct urb *), GFP_KERNEL);
1da177e4
LT
1209 if (!urb)
1210 return -ENOMEM;
1da177e4 1211 for (i = 0; i < param->sglen; i++) {
fabbf219 1212 int pipe = usb_rcvctrlpipe(udev, 0);
1da177e4
LT
1213 unsigned len;
1214 struct urb *u;
1215 struct usb_ctrlrequest req;
1216 struct subcase *reqp;
6def7553
MS
1217
1218 /* sign of this variable means:
1219 * -: tested code must return this (negative) error code
1220 * +: tested code may return this (negative too) error code
1221 */
1da177e4
LT
1222 int expected = 0;
1223
1224 /* requests here are mostly expected to succeed on any
1225 * device, but some are chosen to trigger protocol stalls
1226 * or short reads.
1227 */
f55055b4 1228 memset(&req, 0, sizeof(req));
1da177e4
LT
1229 req.bRequest = USB_REQ_GET_DESCRIPTOR;
1230 req.bRequestType = USB_DIR_IN|USB_RECIP_DEVICE;
1231
1232 switch (i % NUM_SUBCASES) {
fabbf219
MF
1233 case 0: /* get device descriptor */
1234 req.wValue = cpu_to_le16(USB_DT_DEVICE << 8);
1235 len = sizeof(struct usb_device_descriptor);
1da177e4 1236 break;
fabbf219
MF
1237 case 1: /* get first config descriptor (only) */
1238 req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
1239 len = sizeof(struct usb_config_descriptor);
1da177e4 1240 break;
fabbf219 1241 case 2: /* get altsetting (OFTEN STALLS) */
1da177e4
LT
1242 req.bRequest = USB_REQ_GET_INTERFACE;
1243 req.bRequestType = USB_DIR_IN|USB_RECIP_INTERFACE;
fabbf219 1244 /* index = 0 means first interface */
1da177e4
LT
1245 len = 1;
1246 expected = EPIPE;
1247 break;
fabbf219 1248 case 3: /* get interface status */
1da177e4
LT
1249 req.bRequest = USB_REQ_GET_STATUS;
1250 req.bRequestType = USB_DIR_IN|USB_RECIP_INTERFACE;
fabbf219 1251 /* interface 0 */
1da177e4
LT
1252 len = 2;
1253 break;
fabbf219 1254 case 4: /* get device status */
1da177e4
LT
1255 req.bRequest = USB_REQ_GET_STATUS;
1256 req.bRequestType = USB_DIR_IN|USB_RECIP_DEVICE;
1257 len = 2;
1258 break;
fabbf219 1259 case 5: /* get device qualifier (MAY STALL) */
1da177e4 1260 req.wValue = cpu_to_le16 (USB_DT_DEVICE_QUALIFIER << 8);
fabbf219 1261 len = sizeof(struct usb_qualifier_descriptor);
1da177e4
LT
1262 if (udev->speed != USB_SPEED_HIGH)
1263 expected = EPIPE;
1264 break;
fabbf219
MF
1265 case 6: /* get first config descriptor, plus interface */
1266 req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
1267 len = sizeof(struct usb_config_descriptor);
1268 len += sizeof(struct usb_interface_descriptor);
1da177e4 1269 break;
fabbf219 1270 case 7: /* get interface descriptor (ALWAYS STALLS) */
1da177e4 1271 req.wValue = cpu_to_le16 (USB_DT_INTERFACE << 8);
fabbf219
MF
1272 /* interface == 0 */
1273 len = sizeof(struct usb_interface_descriptor);
28ffd79c 1274 expected = -EPIPE;
1da177e4 1275 break;
fabbf219
MF
1276 /* NOTE: two consecutive stalls in the queue here.
1277 * that tests fault recovery a bit more aggressively. */
1278 case 8: /* clear endpoint halt (MAY STALL) */
1da177e4
LT
1279 req.bRequest = USB_REQ_CLEAR_FEATURE;
1280 req.bRequestType = USB_RECIP_ENDPOINT;
fabbf219
MF
1281 /* wValue 0 == ep halt */
1282 /* wIndex 0 == ep0 (shouldn't halt!) */
1da177e4 1283 len = 0;
fabbf219 1284 pipe = usb_sndctrlpipe(udev, 0);
1da177e4
LT
1285 expected = EPIPE;
1286 break;
fabbf219 1287 case 9: /* get endpoint status */
1da177e4
LT
1288 req.bRequest = USB_REQ_GET_STATUS;
1289 req.bRequestType = USB_DIR_IN|USB_RECIP_ENDPOINT;
fabbf219 1290 /* endpoint 0 */
1da177e4
LT
1291 len = 2;
1292 break;
fabbf219
MF
1293 case 10: /* trigger short read (EREMOTEIO) */
1294 req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
1da177e4
LT
1295 len = 1024;
1296 expected = -EREMOTEIO;
1297 break;
fabbf219
MF
1298 /* NOTE: two consecutive _different_ faults in the queue. */
1299 case 11: /* get endpoint descriptor (ALWAYS STALLS) */
1300 req.wValue = cpu_to_le16(USB_DT_ENDPOINT << 8);
1301 /* endpoint == 0 */
1302 len = sizeof(struct usb_interface_descriptor);
1da177e4
LT
1303 expected = EPIPE;
1304 break;
fabbf219
MF
1305 /* NOTE: sometimes even a third fault in the queue! */
1306 case 12: /* get string 0 descriptor (MAY STALL) */
1307 req.wValue = cpu_to_le16(USB_DT_STRING << 8);
1308 /* string == 0, for language IDs */
1309 len = sizeof(struct usb_interface_descriptor);
1310 /* may succeed when > 4 languages */
1311 expected = EREMOTEIO; /* or EPIPE, if no strings */
1da177e4 1312 break;
fabbf219
MF
1313 case 13: /* short read, resembling case 10 */
1314 req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
1315 /* last data packet "should" be DATA1, not DATA0 */
6a23ccd2
PZ
1316 if (udev->speed == USB_SPEED_SUPER)
1317 len = 1024 - 512;
1318 else
1319 len = 1024 - udev->descriptor.bMaxPacketSize0;
1da177e4
LT
1320 expected = -EREMOTEIO;
1321 break;
fabbf219
MF
1322 case 14: /* short read; try to fill the last packet */
1323 req.wValue = cpu_to_le16((USB_DT_DEVICE << 8) | 0);
28ffd79c 1324 /* device descriptor size == 18 bytes */
1da177e4 1325 len = udev->descriptor.bMaxPacketSize0;
67e7d64b
SAS
1326 if (udev->speed == USB_SPEED_SUPER)
1327 len = 512;
1da177e4 1328 switch (len) {
fabbf219
MF
1329 case 8:
1330 len = 24;
1331 break;
1332 case 16:
1333 len = 32;
1334 break;
1da177e4
LT
1335 }
1336 expected = -EREMOTEIO;
1337 break;
c952a8ba
HR
1338 case 15:
1339 req.wValue = cpu_to_le16(USB_DT_BOS << 8);
1340 if (udev->bos)
1341 len = le16_to_cpu(udev->bos->desc->wTotalLength);
1342 else
1343 len = sizeof(struct usb_bos_descriptor);
8cf43285 1344 if (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0201)
c952a8ba
HR
1345 expected = -EPIPE;
1346 break;
1da177e4 1347 default:
28ffd79c 1348 ERROR(dev, "bogus number of ctrl queue testcases!\n");
1da177e4
LT
1349 context.status = -EINVAL;
1350 goto cleanup;
1351 }
fabbf219 1352 req.wLength = cpu_to_le16(len);
457a0955 1353 urb[i] = u = simple_alloc_urb(udev, pipe, len, 0);
1da177e4
LT
1354 if (!u)
1355 goto cleanup;
1356
f55055b4 1357 reqp = kmalloc(sizeof(*reqp), GFP_KERNEL);
1da177e4
LT
1358 if (!reqp)
1359 goto cleanup;
1360 reqp->setup = req;
1361 reqp->number = i % NUM_SUBCASES;
1362 reqp->expected = expected;
1363 u->setup_packet = (char *) &reqp->setup;
1364
1365 u->context = &context;
1366 u->complete = ctrl_complete;
1da177e4
LT
1367 }
1368
1369 /* queue the urbs */
1370 context.urb = urb;
fabbf219 1371 spin_lock_irq(&context.lock);
1da177e4 1372 for (i = 0; i < param->sglen; i++) {
fabbf219 1373 context.status = usb_submit_urb(urb[i], GFP_ATOMIC);
1da177e4 1374 if (context.status != 0) {
28ffd79c 1375 ERROR(dev, "can't submit urb[%d], status %d\n",
1da177e4
LT
1376 i, context.status);
1377 context.count = context.pending;
1378 break;
1379 }
1380 context.pending++;
1381 }
fabbf219 1382 spin_unlock_irq(&context.lock);
1da177e4
LT
1383
1384 /* FIXME set timer and time out; provide a disconnect hook */
1385
1386 /* wait for the last one to complete */
1387 if (context.pending > 0)
fabbf219 1388 wait_for_completion(&context.complete);
1da177e4
LT
1389
1390cleanup:
1391 for (i = 0; i < param->sglen; i++) {
fabbf219 1392 if (!urb[i])
1da177e4 1393 continue;
fabbf219 1394 urb[i]->dev = udev;
0ede76fc 1395 kfree(urb[i]->setup_packet);
fabbf219 1396 simple_free_urb(urb[i]);
1da177e4 1397 }
fabbf219 1398 kfree(urb);
1da177e4
LT
1399 return context.status;
1400}
1401#undef NUM_SUBCASES
1402
1403
1404/*-------------------------------------------------------------------------*/
1405
fabbf219 1406static void unlink1_callback(struct urb *urb)
1da177e4
LT
1407{
1408 int status = urb->status;
1409
fabbf219 1410 /* we "know" -EPIPE (stall) never happens */
1da177e4 1411 if (!status)
fabbf219 1412 status = usb_submit_urb(urb, GFP_ATOMIC);
1da177e4
LT
1413 if (status) {
1414 urb->status = status;
cdc97792 1415 complete(urb->context);
1da177e4
LT
1416 }
1417}
1418
fabbf219 1419static int unlink1(struct usbtest_dev *dev, int pipe, int size, int async)
1da177e4
LT
1420{
1421 struct urb *urb;
1422 struct completion completion;
1423 int retval = 0;
1424
fabbf219 1425 init_completion(&completion);
457a0955 1426 urb = simple_alloc_urb(testdev_to_usbdev(dev), pipe, size, 0);
1da177e4
LT
1427 if (!urb)
1428 return -ENOMEM;
1da177e4
LT
1429 urb->context = &completion;
1430 urb->complete = unlink1_callback;
1431
e4d58f5d
HR
1432 if (usb_pipeout(urb->pipe)) {
1433 simple_fill_buf(urb);
1434 urb->transfer_flags |= URB_ZERO_PACKET;
1435 }
1436
1da177e4
LT
1437 /* keep the endpoint busy. there are lots of hc/hcd-internal
1438 * states, and testing should get to all of them over time.
1439 *
1440 * FIXME want additional tests for when endpoint is STALLing
1441 * due to errors, or is just NAKing requests.
1442 */
fabbf219
MF
1443 retval = usb_submit_urb(urb, GFP_KERNEL);
1444 if (retval != 0) {
28ffd79c 1445 dev_err(&dev->intf->dev, "submit fail %d\n", retval);
1da177e4
LT
1446 return retval;
1447 }
1448
1449 /* unlinking that should always work. variable delay tests more
1450 * hcd states and code paths, even with little other system load.
1451 */
fabbf219 1452 msleep(jiffies % (2 * INTERRUPT_RATE));
1da177e4 1453 if (async) {
3b6c023f
MF
1454 while (!completion_done(&completion)) {
1455 retval = usb_unlink_urb(urb);
1456
a7683eb3
HR
1457 if (retval == 0 && usb_pipein(urb->pipe))
1458 retval = simple_check_buf(dev, urb);
1459
3b6c023f
MF
1460 switch (retval) {
1461 case -EBUSY:
1462 case -EIDRM:
1463 /* we can't unlink urbs while they're completing
1464 * or if they've completed, and we haven't
1465 * resubmitted. "normal" drivers would prevent
1466 * resubmission, but since we're testing unlink
1467 * paths, we can't.
1468 */
1469 ERROR(dev, "unlink retry\n");
1470 continue;
1471 case 0:
1472 case -EINPROGRESS:
1473 break;
1474
1475 default:
1476 dev_err(&dev->intf->dev,
1477 "unlink fail %d\n", retval);
1478 return retval;
1479 }
1480
1481 break;
1da177e4
LT
1482 }
1483 } else
fabbf219 1484 usb_kill_urb(urb);
1da177e4 1485
fabbf219 1486 wait_for_completion(&completion);
1da177e4 1487 retval = urb->status;
fabbf219 1488 simple_free_urb(urb);
1da177e4
LT
1489
1490 if (async)
1491 return (retval == -ECONNRESET) ? 0 : retval - 1000;
1492 else
1493 return (retval == -ENOENT || retval == -EPERM) ?
1494 0 : retval - 2000;
1495}
1496
fabbf219 1497static int unlink_simple(struct usbtest_dev *dev, int pipe, int len)
1da177e4
LT
1498{
1499 int retval = 0;
1500
1501 /* test sync and async paths */
fabbf219 1502 retval = unlink1(dev, pipe, len, 1);
1da177e4 1503 if (!retval)
fabbf219 1504 retval = unlink1(dev, pipe, len, 0);
1da177e4
LT
1505 return retval;
1506}
1507
1508/*-------------------------------------------------------------------------*/
1509
869410f8
AS
1510struct queued_ctx {
1511 struct completion complete;
1512 atomic_t pending;
1513 unsigned num;
1514 int status;
1515 struct urb **urbs;
1516};
1517
1518static void unlink_queued_callback(struct urb *urb)
1519{
1520 int status = urb->status;
1521 struct queued_ctx *ctx = urb->context;
1522
1523 if (ctx->status)
1524 goto done;
1525 if (urb == ctx->urbs[ctx->num - 4] || urb == ctx->urbs[ctx->num - 2]) {
1526 if (status == -ECONNRESET)
1527 goto done;
1528 /* What error should we report if the URB completed normally? */
1529 }
1530 if (status != 0)
1531 ctx->status = status;
1532
1533 done:
1534 if (atomic_dec_and_test(&ctx->pending))
1535 complete(&ctx->complete);
1536}
1537
1538static int unlink_queued(struct usbtest_dev *dev, int pipe, unsigned num,
1539 unsigned size)
1540{
1541 struct queued_ctx ctx;
1542 struct usb_device *udev = testdev_to_usbdev(dev);
1543 void *buf;
1544 dma_addr_t buf_dma;
1545 int i;
1546 int retval = -ENOMEM;
1547
1548 init_completion(&ctx.complete);
1549 atomic_set(&ctx.pending, 1); /* One more than the actual value */
1550 ctx.num = num;
1551 ctx.status = 0;
1552
1553 buf = usb_alloc_coherent(udev, size, GFP_KERNEL, &buf_dma);
1554 if (!buf)
1555 return retval;
1556 memset(buf, 0, size);
1557
1558 /* Allocate and init the urbs we'll queue */
1559 ctx.urbs = kcalloc(num, sizeof(struct urb *), GFP_KERNEL);
1560 if (!ctx.urbs)
1561 goto free_buf;
1562 for (i = 0; i < num; i++) {
1563 ctx.urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1564 if (!ctx.urbs[i])
1565 goto free_urbs;
1566 usb_fill_bulk_urb(ctx.urbs[i], udev, pipe, buf, size,
1567 unlink_queued_callback, &ctx);
1568 ctx.urbs[i]->transfer_dma = buf_dma;
1569 ctx.urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
e4d58f5d
HR
1570
1571 if (usb_pipeout(ctx.urbs[i]->pipe)) {
1572 simple_fill_buf(ctx.urbs[i]);
1573 ctx.urbs[i]->transfer_flags |= URB_ZERO_PACKET;
1574 }
869410f8
AS
1575 }
1576
1577 /* Submit all the URBs and then unlink URBs num - 4 and num - 2. */
1578 for (i = 0; i < num; i++) {
1579 atomic_inc(&ctx.pending);
1580 retval = usb_submit_urb(ctx.urbs[i], GFP_KERNEL);
1581 if (retval != 0) {
1582 dev_err(&dev->intf->dev, "submit urbs[%d] fail %d\n",
1583 i, retval);
1584 atomic_dec(&ctx.pending);
1585 ctx.status = retval;
1586 break;
1587 }
1588 }
1589 if (i == num) {
1590 usb_unlink_urb(ctx.urbs[num - 4]);
1591 usb_unlink_urb(ctx.urbs[num - 2]);
1592 } else {
1593 while (--i >= 0)
1594 usb_unlink_urb(ctx.urbs[i]);
1595 }
1596
1597 if (atomic_dec_and_test(&ctx.pending)) /* The extra count */
1598 complete(&ctx.complete);
1599 wait_for_completion(&ctx.complete);
1600 retval = ctx.status;
1601
1602 free_urbs:
1603 for (i = 0; i < num; i++)
1604 usb_free_urb(ctx.urbs[i]);
1605 kfree(ctx.urbs);
1606 free_buf:
1607 usb_free_coherent(udev, size, buf, buf_dma);
1608 return retval;
1609}
1610
1611/*-------------------------------------------------------------------------*/
1612
28ffd79c 1613static int verify_not_halted(struct usbtest_dev *tdev, int ep, struct urb *urb)
1da177e4
LT
1614{
1615 int retval;
1616 u16 status;
1617
1618 /* shouldn't look or act halted */
fabbf219 1619 retval = usb_get_status(urb->dev, USB_RECIP_ENDPOINT, ep, &status);
1da177e4 1620 if (retval < 0) {
28ffd79c
DB
1621 ERROR(tdev, "ep %02x couldn't get no-halt status, %d\n",
1622 ep, retval);
1da177e4
LT
1623 return retval;
1624 }
1625 if (status != 0) {
28ffd79c 1626 ERROR(tdev, "ep %02x bogus status: %04x != 0\n", ep, status);
1da177e4
LT
1627 return -EINVAL;
1628 }
28ffd79c 1629 retval = simple_io(tdev, urb, 1, 0, 0, __func__);
1da177e4
LT
1630 if (retval != 0)
1631 return -EINVAL;
1632 return 0;
1633}
1634
28ffd79c 1635static int verify_halted(struct usbtest_dev *tdev, int ep, struct urb *urb)
1da177e4
LT
1636{
1637 int retval;
1638 u16 status;
1639
1640 /* should look and act halted */
fabbf219 1641 retval = usb_get_status(urb->dev, USB_RECIP_ENDPOINT, ep, &status);
1da177e4 1642 if (retval < 0) {
28ffd79c
DB
1643 ERROR(tdev, "ep %02x couldn't get halt status, %d\n",
1644 ep, retval);
1da177e4
LT
1645 return retval;
1646 }
1647 if (status != 1) {
28ffd79c 1648 ERROR(tdev, "ep %02x bogus status: %04x != 1\n", ep, status);
1da177e4
LT
1649 return -EINVAL;
1650 }
28ffd79c 1651 retval = simple_io(tdev, urb, 1, 0, -EPIPE, __func__);
1da177e4
LT
1652 if (retval != -EPIPE)
1653 return -EINVAL;
28ffd79c 1654 retval = simple_io(tdev, urb, 1, 0, -EPIPE, "verify_still_halted");
1da177e4
LT
1655 if (retval != -EPIPE)
1656 return -EINVAL;
1657 return 0;
1658}
1659
28ffd79c 1660static int test_halt(struct usbtest_dev *tdev, int ep, struct urb *urb)
1da177e4
LT
1661{
1662 int retval;
1663
1664 /* shouldn't look or act halted now */
28ffd79c 1665 retval = verify_not_halted(tdev, ep, urb);
1da177e4
LT
1666 if (retval < 0)
1667 return retval;
1668
1669 /* set halt (protocol test only), verify it worked */
fabbf219 1670 retval = usb_control_msg(urb->dev, usb_sndctrlpipe(urb->dev, 0),
1da177e4
LT
1671 USB_REQ_SET_FEATURE, USB_RECIP_ENDPOINT,
1672 USB_ENDPOINT_HALT, ep,
1673 NULL, 0, USB_CTRL_SET_TIMEOUT);
1674 if (retval < 0) {
28ffd79c 1675 ERROR(tdev, "ep %02x couldn't set halt, %d\n", ep, retval);
1da177e4
LT
1676 return retval;
1677 }
28ffd79c 1678 retval = verify_halted(tdev, ep, urb);
824d752b
RQ
1679 if (retval < 0) {
1680 int ret;
1681
1682 /* clear halt anyways, else further tests will fail */
1683 ret = usb_clear_halt(urb->dev, urb->pipe);
1684 if (ret)
1685 ERROR(tdev, "ep %02x couldn't clear halt, %d\n",
1686 ep, ret);
1687
1da177e4 1688 return retval;
824d752b 1689 }
1da177e4
LT
1690
1691 /* clear halt (tests API + protocol), verify it worked */
fabbf219 1692 retval = usb_clear_halt(urb->dev, urb->pipe);
1da177e4 1693 if (retval < 0) {
28ffd79c 1694 ERROR(tdev, "ep %02x couldn't clear halt, %d\n", ep, retval);
1da177e4
LT
1695 return retval;
1696 }
28ffd79c 1697 retval = verify_not_halted(tdev, ep, urb);
1da177e4
LT
1698 if (retval < 0)
1699 return retval;
1700
1701 /* NOTE: could also verify SET_INTERFACE clear halts ... */
1702
1703 return 0;
1704}
1705
fabbf219 1706static int halt_simple(struct usbtest_dev *dev)
1da177e4 1707{
6a23ccd2
PZ
1708 int ep;
1709 int retval = 0;
1710 struct urb *urb;
1711 struct usb_device *udev = testdev_to_usbdev(dev);
1da177e4 1712
6a23ccd2 1713 if (udev->speed == USB_SPEED_SUPER)
457a0955 1714 urb = simple_alloc_urb(udev, 0, 1024, 0);
6a23ccd2 1715 else
457a0955 1716 urb = simple_alloc_urb(udev, 0, 512, 0);
1da177e4
LT
1717 if (urb == NULL)
1718 return -ENOMEM;
1719
1720 if (dev->in_pipe) {
fabbf219 1721 ep = usb_pipeendpoint(dev->in_pipe) | USB_DIR_IN;
1da177e4 1722 urb->pipe = dev->in_pipe;
28ffd79c 1723 retval = test_halt(dev, ep, urb);
1da177e4
LT
1724 if (retval < 0)
1725 goto done;
1726 }
1727
1728 if (dev->out_pipe) {
fabbf219 1729 ep = usb_pipeendpoint(dev->out_pipe);
1da177e4 1730 urb->pipe = dev->out_pipe;
28ffd79c 1731 retval = test_halt(dev, ep, urb);
1da177e4
LT
1732 }
1733done:
fabbf219 1734 simple_free_urb(urb);
1da177e4
LT
1735 return retval;
1736}
1737
1738/*-------------------------------------------------------------------------*/
1739
1740/* Control OUT tests use the vendor control requests from Intel's
1741 * USB 2.0 compliance test device: write a buffer, read it back.
1742 *
1743 * Intel's spec only _requires_ that it work for one packet, which
1744 * is pretty weak. Some HCDs place limits here; most devices will
1745 * need to be able to handle more than one OUT data packet. We'll
1746 * try whatever we're told to try.
1747 */
fabbf219 1748static int ctrl_out(struct usbtest_dev *dev,
084fb206 1749 unsigned count, unsigned length, unsigned vary, unsigned offset)
1da177e4 1750{
f54fa84d
OF
1751 unsigned i, j, len;
1752 int retval;
1da177e4
LT
1753 u8 *buf;
1754 char *what = "?";
1755 struct usb_device *udev;
f54fa84d 1756
ff7c79e4 1757 if (length < 1 || length > 0xffff || vary >= length)
1da177e4
LT
1758 return -EINVAL;
1759
084fb206 1760 buf = kmalloc(length + offset, GFP_KERNEL);
1da177e4
LT
1761 if (!buf)
1762 return -ENOMEM;
1763
084fb206 1764 buf += offset;
fabbf219 1765 udev = testdev_to_usbdev(dev);
1da177e4
LT
1766 len = length;
1767 retval = 0;
1768
1769 /* NOTE: hardware might well act differently if we pushed it
1770 * with lots back-to-back queued requests.
1771 */
1772 for (i = 0; i < count; i++) {
1773 /* write patterned data */
1774 for (j = 0; j < len; j++)
169900f9 1775 buf[j] = (u8)(i + j);
fabbf219 1776 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1da177e4
LT
1777 0x5b, USB_DIR_OUT|USB_TYPE_VENDOR,
1778 0, 0, buf, len, USB_CTRL_SET_TIMEOUT);
1779 if (retval != len) {
1780 what = "write";
ff7c79e4 1781 if (retval >= 0) {
28ffd79c 1782 ERROR(dev, "ctrl_out, wlen %d (expected %d)\n",
ff7c79e4
DB
1783 retval, len);
1784 retval = -EBADMSG;
1785 }
1da177e4
LT
1786 break;
1787 }
1788
1789 /* read it back -- assuming nothing intervened!! */
fabbf219 1790 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
1da177e4
LT
1791 0x5c, USB_DIR_IN|USB_TYPE_VENDOR,
1792 0, 0, buf, len, USB_CTRL_GET_TIMEOUT);
1793 if (retval != len) {
1794 what = "read";
ff7c79e4 1795 if (retval >= 0) {
28ffd79c 1796 ERROR(dev, "ctrl_out, rlen %d (expected %d)\n",
ff7c79e4
DB
1797 retval, len);
1798 retval = -EBADMSG;
1799 }
1da177e4
LT
1800 break;
1801 }
1802
1803 /* fail if we can't verify */
1804 for (j = 0; j < len; j++) {
169900f9 1805 if (buf[j] != (u8)(i + j)) {
28ffd79c 1806 ERROR(dev, "ctrl_out, byte %d is %d not %d\n",
169900f9 1807 j, buf[j], (u8)(i + j));
1da177e4
LT
1808 retval = -EBADMSG;
1809 break;
1810 }
1811 }
1812 if (retval < 0) {
1813 what = "verify";
1814 break;
1815 }
1816
1817 len += vary;
ff7c79e4
DB
1818
1819 /* [real world] the "zero bytes IN" case isn't really used.
dc0d5c1e 1820 * hardware can easily trip up in this weird case, since its
ff7c79e4
DB
1821 * status stage is IN, not OUT like other ep0in transfers.
1822 */
1da177e4 1823 if (len > length)
ff7c79e4 1824 len = realworld ? 1 : 0;
1da177e4
LT
1825 }
1826
1827 if (retval < 0)
fabbf219 1828 ERROR(dev, "ctrl_out %s failed, code %d, count %d\n",
1da177e4
LT
1829 what, retval, i);
1830
084fb206 1831 kfree(buf - offset);
1da177e4
LT
1832 return retval;
1833}
1834
1835/*-------------------------------------------------------------------------*/
1836
145f48c5 1837/* ISO/BULK tests ... mimics common usage
1da177e4
LT
1838 * - buffer length is split into N packets (mostly maxpacket sized)
1839 * - multi-buffers according to sglen
1840 */
1841
145f48c5 1842struct transfer_context {
1da177e4
LT
1843 unsigned count;
1844 unsigned pending;
1845 spinlock_t lock;
1846 struct completion done;
9da2150f 1847 int submit_error;
1da177e4 1848 unsigned long errors;
9da2150f 1849 unsigned long packet_count;
1da177e4 1850 struct usbtest_dev *dev;
145f48c5 1851 bool is_iso;
1da177e4
LT
1852};
1853
145f48c5 1854static void complicated_callback(struct urb *urb)
1da177e4 1855{
145f48c5 1856 struct transfer_context *ctx = urb->context;
1da177e4
LT
1857
1858 spin_lock(&ctx->lock);
1859 ctx->count--;
1860
9da2150f 1861 ctx->packet_count += urb->number_of_packets;
1da177e4
LT
1862 if (urb->error_count > 0)
1863 ctx->errors += urb->error_count;
9da2150f 1864 else if (urb->status != 0)
145f48c5 1865 ctx->errors += (ctx->is_iso ? urb->number_of_packets : 1);
40aed524
MF
1866 else if (urb->actual_length != urb->transfer_buffer_length)
1867 ctx->errors++;
084fb206
MF
1868 else if (check_guard_bytes(ctx->dev, urb) != 0)
1869 ctx->errors++;
1da177e4 1870
9da2150f
AS
1871 if (urb->status == 0 && ctx->count > (ctx->pending - 1)
1872 && !ctx->submit_error) {
fabbf219 1873 int status = usb_submit_urb(urb, GFP_ATOMIC);
1da177e4
LT
1874 switch (status) {
1875 case 0:
1876 goto done;
1877 default:
28ffd79c 1878 dev_err(&ctx->dev->intf->dev,
c7c78067 1879 "resubmit err %d\n",
1da177e4
LT
1880 status);
1881 /* FALLTHROUGH */
1882 case -ENODEV: /* disconnected */
9da2150f
AS
1883 case -ESHUTDOWN: /* endpoint disabled */
1884 ctx->submit_error = 1;
1da177e4
LT
1885 break;
1886 }
1887 }
1da177e4
LT
1888
1889 ctx->pending--;
1890 if (ctx->pending == 0) {
1891 if (ctx->errors)
28ffd79c 1892 dev_err(&ctx->dev->intf->dev,
c7c78067 1893 "during the test, %lu errors out of %lu\n",
9da2150f 1894 ctx->errors, ctx->packet_count);
fabbf219 1895 complete(&ctx->done);
1da177e4
LT
1896 }
1897done:
1898 spin_unlock(&ctx->lock);
1899}
1900
fabbf219 1901static struct urb *iso_alloc_urb(
1da177e4
LT
1902 struct usb_device *udev,
1903 int pipe,
1904 struct usb_endpoint_descriptor *desc,
084fb206
MF
1905 long bytes,
1906 unsigned offset
1da177e4
LT
1907)
1908{
1909 struct urb *urb;
1910 unsigned i, maxp, packets;
1911
1912 if (bytes < 0 || !desc)
1913 return NULL;
29cc8897
KM
1914 maxp = 0x7ff & usb_endpoint_maxp(desc);
1915 maxp *= 1 + (0x3 & (usb_endpoint_maxp(desc) >> 11));
dfa5ec79 1916 packets = DIV_ROUND_UP(bytes, maxp);
1da177e4 1917
fabbf219 1918 urb = usb_alloc_urb(packets, GFP_KERNEL);
1da177e4
LT
1919 if (!urb)
1920 return urb;
1921 urb->dev = udev;
1922 urb->pipe = pipe;
1923
1924 urb->number_of_packets = packets;
1925 urb->transfer_buffer_length = bytes;
084fb206
MF
1926 urb->transfer_buffer = usb_alloc_coherent(udev, bytes + offset,
1927 GFP_KERNEL,
1928 &urb->transfer_dma);
1da177e4 1929 if (!urb->transfer_buffer) {
fabbf219 1930 usb_free_urb(urb);
1da177e4
LT
1931 return NULL;
1932 }
084fb206
MF
1933 if (offset) {
1934 memset(urb->transfer_buffer, GUARD_BYTE, offset);
1935 urb->transfer_buffer += offset;
1936 urb->transfer_dma += offset;
1937 }
1938 /* For inbound transfers use guard byte so that test fails if
1939 data not correctly copied */
1940 memset(urb->transfer_buffer,
1941 usb_pipein(urb->pipe) ? GUARD_BYTE : 0,
1942 bytes);
1943
1da177e4
LT
1944 for (i = 0; i < packets; i++) {
1945 /* here, only the last packet will be short */
fabbf219 1946 urb->iso_frame_desc[i].length = min((unsigned) bytes, maxp);
1da177e4
LT
1947 bytes -= urb->iso_frame_desc[i].length;
1948
1949 urb->iso_frame_desc[i].offset = maxp * i;
1950 }
1951
145f48c5 1952 urb->complete = complicated_callback;
fabbf219 1953 /* urb->context = SET BY CALLER */
1da177e4
LT
1954 urb->interval = 1 << (desc->bInterval - 1);
1955 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
1956 return urb;
1957}
1958
1959static int
18fc4ebd 1960test_queue(struct usbtest_dev *dev, struct usbtest_param_32 *param,
084fb206 1961 int pipe, struct usb_endpoint_descriptor *desc, unsigned offset)
1da177e4 1962{
145f48c5 1963 struct transfer_context context;
1da177e4
LT
1964 struct usb_device *udev;
1965 unsigned i;
1966 unsigned long packets = 0;
9da2150f 1967 int status = 0;
41d3c0b8 1968 struct urb *urbs[param->sglen];
1da177e4 1969
f55055b4 1970 memset(&context, 0, sizeof(context));
1da177e4 1971 context.count = param->iterations * param->sglen;
1da177e4 1972 context.dev = dev;
145f48c5 1973 context.is_iso = !!desc;
fabbf219
MF
1974 init_completion(&context.done);
1975 spin_lock_init(&context.lock);
1da177e4 1976
fabbf219 1977 udev = testdev_to_usbdev(dev);
1da177e4
LT
1978
1979 for (i = 0; i < param->sglen; i++) {
145f48c5
PC
1980 if (context.is_iso)
1981 urbs[i] = iso_alloc_urb(udev, pipe, desc,
084fb206 1982 param->length, offset);
145f48c5
PC
1983 else
1984 urbs[i] = complicated_alloc_urb(udev, pipe,
1985 param->length, 0);
1986
fabbf219 1987 if (!urbs[i]) {
1da177e4
LT
1988 status = -ENOMEM;
1989 goto fail;
1990 }
1991 packets += urbs[i]->number_of_packets;
fabbf219 1992 urbs[i]->context = &context;
1da177e4
LT
1993 }
1994 packets *= param->iterations;
145f48c5
PC
1995
1996 if (context.is_iso) {
1997 dev_info(&dev->intf->dev,
1998 "iso period %d %sframes, wMaxPacket %d, transactions: %d\n",
1999 1 << (desc->bInterval - 1),
2000 (udev->speed == USB_SPEED_HIGH) ? "micro" : "",
2001 usb_endpoint_maxp(desc) & 0x7ff,
2002 1 + (0x3 & (usb_endpoint_maxp(desc) >> 11)));
2003
2004 dev_info(&dev->intf->dev,
2005 "total %lu msec (%lu packets)\n",
2006 (packets * (1 << (desc->bInterval - 1)))
2007 / ((udev->speed == USB_SPEED_HIGH) ? 8 : 1),
2008 packets);
2009 }
1da177e4 2010
fabbf219 2011 spin_lock_irq(&context.lock);
1da177e4 2012 for (i = 0; i < param->sglen; i++) {
9da2150f 2013 ++context.pending;
fabbf219 2014 status = usb_submit_urb(urbs[i], GFP_ATOMIC);
1da177e4 2015 if (status < 0) {
fabbf219 2016 ERROR(dev, "submit iso[%d], error %d\n", i, status);
1da177e4 2017 if (i == 0) {
fabbf219 2018 spin_unlock_irq(&context.lock);
1da177e4
LT
2019 goto fail;
2020 }
2021
fabbf219 2022 simple_free_urb(urbs[i]);
e10e1bec 2023 urbs[i] = NULL;
1da177e4 2024 context.pending--;
9da2150f
AS
2025 context.submit_error = 1;
2026 break;
1da177e4
LT
2027 }
2028 }
fabbf219 2029 spin_unlock_irq(&context.lock);
1da177e4 2030
fabbf219 2031 wait_for_completion(&context.done);
9da2150f 2032
e10e1bec
ML
2033 for (i = 0; i < param->sglen; i++) {
2034 if (urbs[i])
2035 simple_free_urb(urbs[i]);
2036 }
9da2150f
AS
2037 /*
2038 * Isochronous transfers are expected to fail sometimes. As an
2039 * arbitrary limit, we will report an error if any submissions
2040 * fail or if the transfer failure rate is > 10%.
2041 */
2042 if (status != 0)
2043 ;
2044 else if (context.submit_error)
2045 status = -EACCES;
145f48c5
PC
2046 else if (context.errors >
2047 (context.is_iso ? context.packet_count / 10 : 0))
9da2150f
AS
2048 status = -EIO;
2049 return status;
1da177e4
LT
2050
2051fail:
2052 for (i = 0; i < param->sglen; i++) {
fabbf219
MF
2053 if (urbs[i])
2054 simple_free_urb(urbs[i]);
1da177e4
LT
2055 }
2056 return status;
2057}
2058
084fb206
MF
2059static int test_unaligned_bulk(
2060 struct usbtest_dev *tdev,
2061 int pipe,
2062 unsigned length,
2063 int iterations,
2064 unsigned transfer_flags,
2065 const char *label)
2066{
2067 int retval;
145f48c5
PC
2068 struct urb *urb = usbtest_alloc_urb(testdev_to_usbdev(tdev),
2069 pipe, length, transfer_flags, 1, 0, simple_callback);
084fb206
MF
2070
2071 if (!urb)
2072 return -ENOMEM;
2073
2074 retval = simple_io(tdev, urb, iterations, 0, 0, label);
2075 simple_free_urb(urb);
2076 return retval;
2077}
2078
18fc4ebd 2079/* Run tests. */
1da177e4 2080static int
18fc4ebd 2081usbtest_do_ioctl(struct usb_interface *intf, struct usbtest_param_32 *param)
1da177e4 2082{
fabbf219
MF
2083 struct usbtest_dev *dev = usb_get_intfdata(intf);
2084 struct usb_device *udev = testdev_to_usbdev(dev);
1da177e4
LT
2085 struct urb *urb;
2086 struct scatterlist *sg;
2087 struct usb_sg_request req;
1da177e4 2088 unsigned i;
18fc4ebd 2089 int retval = -EOPNOTSUPP;
1da177e4 2090
8aafdf6a 2091 if (param->iterations <= 0)
1da177e4 2092 return -EINVAL;
1da177e4
LT
2093 /*
2094 * Just a bunch of test cases that every HCD is expected to handle.
2095 *
2096 * Some may need specific firmware, though it'd be good to have
2097 * one firmware image to handle all the test cases.
2098 *
2099 * FIXME add more tests! cancel requests, verify the data, control
2100 * queueing, concurrent read+write threads, and so on.
2101 */
1da177e4
LT
2102 switch (param->test_num) {
2103
2104 case 0:
28ffd79c 2105 dev_info(&intf->dev, "TEST 0: NOP\n");
1da177e4
LT
2106 retval = 0;
2107 break;
2108
2109 /* Simple non-queued bulk I/O tests */
2110 case 1:
2111 if (dev->out_pipe == 0)
2112 break;
28ffd79c 2113 dev_info(&intf->dev,
1da177e4
LT
2114 "TEST 1: write %d bytes %u times\n",
2115 param->length, param->iterations);
457a0955 2116 urb = simple_alloc_urb(udev, dev->out_pipe, param->length, 0);
1da177e4
LT
2117 if (!urb) {
2118 retval = -ENOMEM;
2119 break;
2120 }
fabbf219 2121 /* FIRMWARE: bulk sink (maybe accepts short writes) */
28ffd79c 2122 retval = simple_io(dev, urb, param->iterations, 0, 0, "test1");
fabbf219 2123 simple_free_urb(urb);
1da177e4
LT
2124 break;
2125 case 2:
2126 if (dev->in_pipe == 0)
2127 break;
28ffd79c 2128 dev_info(&intf->dev,
1da177e4
LT
2129 "TEST 2: read %d bytes %u times\n",
2130 param->length, param->iterations);
457a0955 2131 urb = simple_alloc_urb(udev, dev->in_pipe, param->length, 0);
1da177e4
LT
2132 if (!urb) {
2133 retval = -ENOMEM;
2134 break;
2135 }
fabbf219 2136 /* FIRMWARE: bulk source (maybe generates short writes) */
28ffd79c 2137 retval = simple_io(dev, urb, param->iterations, 0, 0, "test2");
fabbf219 2138 simple_free_urb(urb);
1da177e4
LT
2139 break;
2140 case 3:
2141 if (dev->out_pipe == 0 || param->vary == 0)
2142 break;
28ffd79c 2143 dev_info(&intf->dev,
1da177e4
LT
2144 "TEST 3: write/%d 0..%d bytes %u times\n",
2145 param->vary, param->length, param->iterations);
457a0955 2146 urb = simple_alloc_urb(udev, dev->out_pipe, param->length, 0);
1da177e4
LT
2147 if (!urb) {
2148 retval = -ENOMEM;
2149 break;
2150 }
fabbf219 2151 /* FIRMWARE: bulk sink (maybe accepts short writes) */
28ffd79c 2152 retval = simple_io(dev, urb, param->iterations, param->vary,
1da177e4 2153 0, "test3");
fabbf219 2154 simple_free_urb(urb);
1da177e4
LT
2155 break;
2156 case 4:
2157 if (dev->in_pipe == 0 || param->vary == 0)
2158 break;
28ffd79c 2159 dev_info(&intf->dev,
1da177e4
LT
2160 "TEST 4: read/%d 0..%d bytes %u times\n",
2161 param->vary, param->length, param->iterations);
457a0955 2162 urb = simple_alloc_urb(udev, dev->in_pipe, param->length, 0);
1da177e4
LT
2163 if (!urb) {
2164 retval = -ENOMEM;
2165 break;
2166 }
fabbf219 2167 /* FIRMWARE: bulk source (maybe generates short writes) */
28ffd79c 2168 retval = simple_io(dev, urb, param->iterations, param->vary,
1da177e4 2169 0, "test4");
fabbf219 2170 simple_free_urb(urb);
1da177e4
LT
2171 break;
2172
2173 /* Queued bulk I/O tests */
2174 case 5:
2175 if (dev->out_pipe == 0 || param->sglen == 0)
2176 break;
28ffd79c 2177 dev_info(&intf->dev,
1da177e4
LT
2178 "TEST 5: write %d sglists %d entries of %d bytes\n",
2179 param->iterations,
2180 param->sglen, param->length);
b9a6e8e1
AS
2181 sg = alloc_sglist(param->sglen, param->length,
2182 0, dev, dev->out_pipe);
1da177e4
LT
2183 if (!sg) {
2184 retval = -ENOMEM;
2185 break;
2186 }
fabbf219 2187 /* FIRMWARE: bulk sink (maybe accepts short writes) */
28ffd79c 2188 retval = perform_sglist(dev, param->iterations, dev->out_pipe,
1da177e4 2189 &req, sg, param->sglen);
fabbf219 2190 free_sglist(sg, param->sglen);
1da177e4
LT
2191 break;
2192
2193 case 6:
2194 if (dev->in_pipe == 0 || param->sglen == 0)
2195 break;
28ffd79c 2196 dev_info(&intf->dev,
1da177e4
LT
2197 "TEST 6: read %d sglists %d entries of %d bytes\n",
2198 param->iterations,
2199 param->sglen, param->length);
b9a6e8e1
AS
2200 sg = alloc_sglist(param->sglen, param->length,
2201 0, dev, dev->in_pipe);
1da177e4
LT
2202 if (!sg) {
2203 retval = -ENOMEM;
2204 break;
2205 }
fabbf219 2206 /* FIRMWARE: bulk source (maybe generates short writes) */
28ffd79c 2207 retval = perform_sglist(dev, param->iterations, dev->in_pipe,
1da177e4 2208 &req, sg, param->sglen);
fabbf219 2209 free_sglist(sg, param->sglen);
1da177e4
LT
2210 break;
2211 case 7:
2212 if (dev->out_pipe == 0 || param->sglen == 0 || param->vary == 0)
2213 break;
28ffd79c 2214 dev_info(&intf->dev,
1da177e4
LT
2215 "TEST 7: write/%d %d sglists %d entries 0..%d bytes\n",
2216 param->vary, param->iterations,
2217 param->sglen, param->length);
b9a6e8e1
AS
2218 sg = alloc_sglist(param->sglen, param->length,
2219 param->vary, dev, dev->out_pipe);
1da177e4
LT
2220 if (!sg) {
2221 retval = -ENOMEM;
2222 break;
2223 }
fabbf219 2224 /* FIRMWARE: bulk sink (maybe accepts short writes) */
28ffd79c 2225 retval = perform_sglist(dev, param->iterations, dev->out_pipe,
1da177e4 2226 &req, sg, param->sglen);
fabbf219 2227 free_sglist(sg, param->sglen);
1da177e4
LT
2228 break;
2229 case 8:
2230 if (dev->in_pipe == 0 || param->sglen == 0 || param->vary == 0)
2231 break;
28ffd79c 2232 dev_info(&intf->dev,
1da177e4
LT
2233 "TEST 8: read/%d %d sglists %d entries 0..%d bytes\n",
2234 param->vary, param->iterations,
2235 param->sglen, param->length);
b9a6e8e1
AS
2236 sg = alloc_sglist(param->sglen, param->length,
2237 param->vary, dev, dev->in_pipe);
1da177e4
LT
2238 if (!sg) {
2239 retval = -ENOMEM;
2240 break;
2241 }
fabbf219 2242 /* FIRMWARE: bulk source (maybe generates short writes) */
28ffd79c 2243 retval = perform_sglist(dev, param->iterations, dev->in_pipe,
1da177e4 2244 &req, sg, param->sglen);
fabbf219 2245 free_sglist(sg, param->sglen);
1da177e4
LT
2246 break;
2247
2248 /* non-queued sanity tests for control (chapter 9 subset) */
2249 case 9:
2250 retval = 0;
28ffd79c 2251 dev_info(&intf->dev,
1da177e4
LT
2252 "TEST 9: ch9 (subset) control tests, %d times\n",
2253 param->iterations);
2254 for (i = param->iterations; retval == 0 && i--; /* NOP */)
fabbf219 2255 retval = ch9_postconfig(dev);
1da177e4 2256 if (retval)
28ffd79c
DB
2257 dev_err(&intf->dev, "ch9 subset failed, "
2258 "iterations left %d\n", i);
1da177e4
LT
2259 break;
2260
2261 /* queued control messaging */
2262 case 10:
1da177e4 2263 retval = 0;
28ffd79c 2264 dev_info(&intf->dev,
1da177e4
LT
2265 "TEST 10: queue %d control calls, %d times\n",
2266 param->sglen,
2267 param->iterations);
fabbf219 2268 retval = test_ctrl_queue(dev, param);
1da177e4
LT
2269 break;
2270
2271 /* simple non-queued unlinks (ring with one urb) */
2272 case 11:
2273 if (dev->in_pipe == 0 || !param->length)
2274 break;
2275 retval = 0;
28ffd79c 2276 dev_info(&intf->dev, "TEST 11: unlink %d reads of %d\n",
1da177e4
LT
2277 param->iterations, param->length);
2278 for (i = param->iterations; retval == 0 && i--; /* NOP */)
fabbf219 2279 retval = unlink_simple(dev, dev->in_pipe,
1da177e4
LT
2280 param->length);
2281 if (retval)
28ffd79c 2282 dev_err(&intf->dev, "unlink reads failed %d, "
1da177e4
LT
2283 "iterations left %d\n", retval, i);
2284 break;
2285 case 12:
2286 if (dev->out_pipe == 0 || !param->length)
2287 break;
2288 retval = 0;
28ffd79c 2289 dev_info(&intf->dev, "TEST 12: unlink %d writes of %d\n",
1da177e4
LT
2290 param->iterations, param->length);
2291 for (i = param->iterations; retval == 0 && i--; /* NOP */)
fabbf219 2292 retval = unlink_simple(dev, dev->out_pipe,
1da177e4
LT
2293 param->length);
2294 if (retval)
28ffd79c 2295 dev_err(&intf->dev, "unlink writes failed %d, "
1da177e4
LT
2296 "iterations left %d\n", retval, i);
2297 break;
2298
2299 /* ep halt tests */
2300 case 13:
2301 if (dev->out_pipe == 0 && dev->in_pipe == 0)
2302 break;
2303 retval = 0;
28ffd79c 2304 dev_info(&intf->dev, "TEST 13: set/clear %d halts\n",
1da177e4
LT
2305 param->iterations);
2306 for (i = param->iterations; retval == 0 && i--; /* NOP */)
fabbf219 2307 retval = halt_simple(dev);
28ffd79c 2308
1da177e4 2309 if (retval)
28ffd79c 2310 ERROR(dev, "halts failed, iterations left %d\n", i);
1da177e4
LT
2311 break;
2312
2313 /* control write tests */
2314 case 14:
2315 if (!dev->info->ctrl_out)
2316 break;
28ffd79c 2317 dev_info(&intf->dev, "TEST 14: %d ep0out, %d..%d vary %d\n",
ff7c79e4
DB
2318 param->iterations,
2319 realworld ? 1 : 0, param->length,
2320 param->vary);
28ffd79c 2321 retval = ctrl_out(dev, param->iterations,
084fb206 2322 param->length, param->vary, 0);
1da177e4
LT
2323 break;
2324
2325 /* iso write tests */
2326 case 15:
2327 if (dev->out_iso_pipe == 0 || param->sglen == 0)
2328 break;
28ffd79c 2329 dev_info(&intf->dev,
1da177e4
LT
2330 "TEST 15: write %d iso, %d entries of %d bytes\n",
2331 param->iterations,
2332 param->sglen, param->length);
fabbf219 2333 /* FIRMWARE: iso sink */
145f48c5 2334 retval = test_queue(dev, param,
084fb206 2335 dev->out_iso_pipe, dev->iso_out, 0);
1da177e4
LT
2336 break;
2337
2338 /* iso read tests */
2339 case 16:
2340 if (dev->in_iso_pipe == 0 || param->sglen == 0)
2341 break;
28ffd79c 2342 dev_info(&intf->dev,
1da177e4
LT
2343 "TEST 16: read %d iso, %d entries of %d bytes\n",
2344 param->iterations,
2345 param->sglen, param->length);
fabbf219 2346 /* FIRMWARE: iso source */
145f48c5 2347 retval = test_queue(dev, param,
084fb206 2348 dev->in_iso_pipe, dev->iso_in, 0);
1da177e4
LT
2349 break;
2350
fabbf219 2351 /* FIXME scatterlist cancel (needs helper thread) */
1da177e4 2352
084fb206
MF
2353 /* Tests for bulk I/O using DMA mapping by core and odd address */
2354 case 17:
2355 if (dev->out_pipe == 0)
2356 break;
2357 dev_info(&intf->dev,
2358 "TEST 17: write odd addr %d bytes %u times core map\n",
2359 param->length, param->iterations);
2360
2361 retval = test_unaligned_bulk(
2362 dev, dev->out_pipe,
2363 param->length, param->iterations,
2364 0, "test17");
2365 break;
2366
2367 case 18:
2368 if (dev->in_pipe == 0)
2369 break;
2370 dev_info(&intf->dev,
2371 "TEST 18: read odd addr %d bytes %u times core map\n",
2372 param->length, param->iterations);
2373
2374 retval = test_unaligned_bulk(
2375 dev, dev->in_pipe,
2376 param->length, param->iterations,
2377 0, "test18");
2378 break;
2379
2380 /* Tests for bulk I/O using premapped coherent buffer and odd address */
2381 case 19:
2382 if (dev->out_pipe == 0)
2383 break;
2384 dev_info(&intf->dev,
2385 "TEST 19: write odd addr %d bytes %u times premapped\n",
2386 param->length, param->iterations);
2387
2388 retval = test_unaligned_bulk(
2389 dev, dev->out_pipe,
2390 param->length, param->iterations,
2391 URB_NO_TRANSFER_DMA_MAP, "test19");
2392 break;
2393
2394 case 20:
2395 if (dev->in_pipe == 0)
2396 break;
2397 dev_info(&intf->dev,
2398 "TEST 20: read odd addr %d bytes %u times premapped\n",
2399 param->length, param->iterations);
2400
2401 retval = test_unaligned_bulk(
2402 dev, dev->in_pipe,
2403 param->length, param->iterations,
2404 URB_NO_TRANSFER_DMA_MAP, "test20");
2405 break;
2406
2407 /* control write tests with unaligned buffer */
2408 case 21:
2409 if (!dev->info->ctrl_out)
2410 break;
2411 dev_info(&intf->dev,
2412 "TEST 21: %d ep0out odd addr, %d..%d vary %d\n",
2413 param->iterations,
2414 realworld ? 1 : 0, param->length,
2415 param->vary);
2416 retval = ctrl_out(dev, param->iterations,
2417 param->length, param->vary, 1);
2418 break;
2419
2420 /* unaligned iso tests */
2421 case 22:
2422 if (dev->out_iso_pipe == 0 || param->sglen == 0)
2423 break;
2424 dev_info(&intf->dev,
2425 "TEST 22: write %d iso odd, %d entries of %d bytes\n",
2426 param->iterations,
2427 param->sglen, param->length);
145f48c5 2428 retval = test_queue(dev, param,
084fb206
MF
2429 dev->out_iso_pipe, dev->iso_out, 1);
2430 break;
2431
2432 case 23:
2433 if (dev->in_iso_pipe == 0 || param->sglen == 0)
2434 break;
2435 dev_info(&intf->dev,
2436 "TEST 23: read %d iso odd, %d entries of %d bytes\n",
2437 param->iterations,
2438 param->sglen, param->length);
145f48c5 2439 retval = test_queue(dev, param,
084fb206
MF
2440 dev->in_iso_pipe, dev->iso_in, 1);
2441 break;
2442
869410f8
AS
2443 /* unlink URBs from a bulk-OUT queue */
2444 case 24:
2445 if (dev->out_pipe == 0 || !param->length || param->sglen < 4)
2446 break;
2447 retval = 0;
2cb50000 2448 dev_info(&intf->dev, "TEST 24: unlink from %d queues of "
869410f8
AS
2449 "%d %d-byte writes\n",
2450 param->iterations, param->sglen, param->length);
2451 for (i = param->iterations; retval == 0 && i > 0; --i) {
2452 retval = unlink_queued(dev, dev->out_pipe,
2453 param->sglen, param->length);
2454 if (retval) {
2455 dev_err(&intf->dev,
2456 "unlink queued writes failed %d, "
2457 "iterations left %d\n", retval, i);
2458 break;
2459 }
2460 }
2461 break;
2462
457a0955
AV
2463 /* Simple non-queued interrupt I/O tests */
2464 case 25:
2465 if (dev->out_int_pipe == 0)
2466 break;
2467 dev_info(&intf->dev,
2468 "TEST 25: write %d bytes %u times\n",
2469 param->length, param->iterations);
2470 urb = simple_alloc_urb(udev, dev->out_int_pipe, param->length,
2471 dev->int_out->bInterval);
2472 if (!urb) {
2473 retval = -ENOMEM;
2474 break;
2475 }
2476 /* FIRMWARE: interrupt sink (maybe accepts short writes) */
2477 retval = simple_io(dev, urb, param->iterations, 0, 0, "test25");
2478 simple_free_urb(urb);
2479 break;
2480 case 26:
2481 if (dev->in_int_pipe == 0)
2482 break;
2483 dev_info(&intf->dev,
2484 "TEST 26: read %d bytes %u times\n",
2485 param->length, param->iterations);
2486 urb = simple_alloc_urb(udev, dev->in_int_pipe, param->length,
2487 dev->int_in->bInterval);
2488 if (!urb) {
2489 retval = -ENOMEM;
2490 break;
2491 }
2492 /* FIRMWARE: interrupt source (maybe generates short writes) */
2493 retval = simple_io(dev, urb, param->iterations, 0, 0, "test26");
2494 simple_free_urb(urb);
2495 break;
145f48c5
PC
2496 case 27:
2497 /* We do performance test, so ignore data compare */
2498 if (dev->out_pipe == 0 || param->sglen == 0 || pattern != 0)
2499 break;
2500 dev_info(&intf->dev,
2501 "TEST 27: bulk write %dMbytes\n", (param->iterations *
2502 param->sglen * param->length) / (1024 * 1024));
2503 retval = test_queue(dev, param,
2504 dev->out_pipe, NULL, 0);
2505 break;
2506 case 28:
2507 if (dev->in_pipe == 0 || param->sglen == 0 || pattern != 0)
2508 break;
2509 dev_info(&intf->dev,
2510 "TEST 28: bulk read %dMbytes\n", (param->iterations *
2511 param->sglen * param->length) / (1024 * 1024));
2512 retval = test_queue(dev, param,
2513 dev->in_pipe, NULL, 0);
2514 break;
1da177e4 2515 }
18fc4ebd
DD
2516 return retval;
2517}
2518
2519/*-------------------------------------------------------------------------*/
2520
2521/* We only have this one interface to user space, through usbfs.
2522 * User mode code can scan usbfs to find N different devices (maybe on
2523 * different busses) to use when testing, and allocate one thread per
2524 * test. So discovery is simplified, and we have no device naming issues.
2525 *
2526 * Don't use these only as stress/load tests. Use them along with with
2527 * other USB bus activity: plugging, unplugging, mousing, mp3 playback,
2528 * video capture, and so on. Run different tests at different times, in
2529 * different sequences. Nothing here should interact with other devices,
2530 * except indirectly by consuming USB bandwidth and CPU resources for test
2531 * threads and request completion. But the only way to know that for sure
2532 * is to test when HC queues are in use by many devices.
2533 *
2534 * WARNING: Because usbfs grabs udev->dev.sem before calling this ioctl(),
2535 * it locks out usbcore in certain code paths. Notably, if you disconnect
2536 * the device-under-test, hub_wq will wait block forever waiting for the
2537 * ioctl to complete ... so that usb_disconnect() can abort the pending
2538 * urbs and then call usbtest_disconnect(). To abort a test, you're best
2539 * off just killing the userspace task and waiting for it to exit.
2540 */
2541
2542static int
2543usbtest_ioctl(struct usb_interface *intf, unsigned int code, void *buf)
2544{
2545
2546 struct usbtest_dev *dev = usb_get_intfdata(intf);
2547 struct usbtest_param_64 *param_64 = buf;
2548 struct usbtest_param_32 temp;
2549 struct usbtest_param_32 *param_32 = buf;
2550 struct timespec64 start;
2551 struct timespec64 end;
2552 struct timespec64 duration;
2553 int retval = -EOPNOTSUPP;
2554
2555 /* FIXME USBDEVFS_CONNECTINFO doesn't say how fast the device is. */
2556
2557 pattern = mod_pattern;
2558
2559 if (mutex_lock_interruptible(&dev->lock))
2560 return -ERESTARTSYS;
2561
2562 /* FIXME: What if a system sleep starts while a test is running? */
2563
2564 /* some devices, like ez-usb default devices, need a non-default
2565 * altsetting to have any active endpoints. some tests change
2566 * altsettings; force a default so most tests don't need to check.
2567 */
2568 if (dev->info->alt >= 0) {
2569 if (intf->altsetting->desc.bInterfaceNumber) {
2570 retval = -ENODEV;
2571 goto free_mutex;
2572 }
2573 retval = set_altsetting(dev, dev->info->alt);
2574 if (retval) {
2575 dev_err(&intf->dev,
2576 "set altsetting to %d failed, %d\n",
2577 dev->info->alt, retval);
2578 goto free_mutex;
2579 }
2580 }
2581
2582 switch (code) {
2583 case USBTEST_REQUEST_64:
2584 temp.test_num = param_64->test_num;
2585 temp.iterations = param_64->iterations;
2586 temp.length = param_64->length;
2587 temp.sglen = param_64->sglen;
2588 temp.vary = param_64->vary;
2589 param_32 = &temp;
2590 break;
2591
2592 case USBTEST_REQUEST_32:
2593 break;
2594
2595 default:
2596 retval = -EOPNOTSUPP;
2597 goto free_mutex;
2598 }
2599
2600 ktime_get_ts64(&start);
2601
2602 retval = usbtest_do_ioctl(intf, param_32);
2603 if (retval)
2604 goto free_mutex;
2605
2606 ktime_get_ts64(&end);
2607
2608 duration = timespec64_sub(end, start);
2609
2610 temp.duration_sec = duration.tv_sec;
2611 temp.duration_usec = duration.tv_nsec/NSEC_PER_USEC;
2612
2613 switch (code) {
2614 case USBTEST_REQUEST_32:
2615 param_32->duration_sec = temp.duration_sec;
2616 param_32->duration_usec = temp.duration_usec;
2617 break;
2618
2619 case USBTEST_REQUEST_64:
2620 param_64->duration_sec = temp.duration_sec;
2621 param_64->duration_usec = temp.duration_usec;
2622 break;
1da177e4 2623 }
18fc4ebd
DD
2624
2625free_mutex:
1cfab028 2626 mutex_unlock(&dev->lock);
1da177e4
LT
2627 return retval;
2628}
2629
2630/*-------------------------------------------------------------------------*/
2631
fabbf219
MF
2632static unsigned force_interrupt;
2633module_param(force_interrupt, uint, 0);
2634MODULE_PARM_DESC(force_interrupt, "0 = test default; else interrupt");
1da177e4
LT
2635
2636#ifdef GENERIC
2637static unsigned short vendor;
2638module_param(vendor, ushort, 0);
fabbf219 2639MODULE_PARM_DESC(vendor, "vendor code (from usb-if)");
1da177e4
LT
2640
2641static unsigned short product;
2642module_param(product, ushort, 0);
fabbf219 2643MODULE_PARM_DESC(product, "product code (from vendor)");
1da177e4
LT
2644#endif
2645
2646static int
fabbf219 2647usbtest_probe(struct usb_interface *intf, const struct usb_device_id *id)
1da177e4
LT
2648{
2649 struct usb_device *udev;
2650 struct usbtest_dev *dev;
2651 struct usbtest_info *info;
2652 char *rtest, *wtest;
2653 char *irtest, *iwtest;
457a0955 2654 char *intrtest, *intwtest;
1da177e4 2655
fabbf219 2656 udev = interface_to_usbdev(intf);
1da177e4
LT
2657
2658#ifdef GENERIC
2659 /* specify devices by module parameters? */
2660 if (id->match_flags == 0) {
2661 /* vendor match required, product match optional */
2662 if (!vendor || le16_to_cpu(udev->descriptor.idVendor) != (u16)vendor)
2663 return -ENODEV;
2664 if (product && le16_to_cpu(udev->descriptor.idProduct) != (u16)product)
2665 return -ENODEV;
28ffd79c
DB
2666 dev_info(&intf->dev, "matched module params, "
2667 "vend=0x%04x prod=0x%04x\n",
1da177e4
LT
2668 le16_to_cpu(udev->descriptor.idVendor),
2669 le16_to_cpu(udev->descriptor.idProduct));
2670 }
2671#endif
2672
e94b1766 2673 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1da177e4
LT
2674 if (!dev)
2675 return -ENOMEM;
1da177e4
LT
2676 info = (struct usbtest_info *) id->driver_info;
2677 dev->info = info;
1cfab028 2678 mutex_init(&dev->lock);
1da177e4
LT
2679
2680 dev->intf = intf;
2681
2682 /* cacheline-aligned scratch for i/o */
fabbf219
MF
2683 dev->buf = kmalloc(TBUF_SIZE, GFP_KERNEL);
2684 if (dev->buf == NULL) {
2685 kfree(dev);
1da177e4
LT
2686 return -ENOMEM;
2687 }
2688
2689 /* NOTE this doesn't yet test the handful of difference that are
2690 * visible with high speed interrupts: bigger maxpacket (1K) and
2691 * "high bandwidth" modes (up to 3 packets/uframe).
2692 */
2693 rtest = wtest = "";
2694 irtest = iwtest = "";
457a0955 2695 intrtest = intwtest = "";
1da177e4
LT
2696 if (force_interrupt || udev->speed == USB_SPEED_LOW) {
2697 if (info->ep_in) {
fabbf219 2698 dev->in_pipe = usb_rcvintpipe(udev, info->ep_in);
1da177e4
LT
2699 rtest = " intr-in";
2700 }
2701 if (info->ep_out) {
fabbf219 2702 dev->out_pipe = usb_sndintpipe(udev, info->ep_out);
1da177e4
LT
2703 wtest = " intr-out";
2704 }
2705 } else {
d0b4652f 2706 if (override_alt >= 0 || info->autoconf) {
1da177e4
LT
2707 int status;
2708
fabbf219 2709 status = get_endpoints(dev, intf);
1da177e4 2710 if (status < 0) {
b6c63937 2711 WARNING(dev, "couldn't get endpoints, %d\n",
28ffd79c 2712 status);
f4a728d0
JL
2713 kfree(dev->buf);
2714 kfree(dev);
1da177e4
LT
2715 return status;
2716 }
2717 /* may find bulk or ISO pipes */
2718 } else {
2719 if (info->ep_in)
fabbf219 2720 dev->in_pipe = usb_rcvbulkpipe(udev,
1da177e4
LT
2721 info->ep_in);
2722 if (info->ep_out)
fabbf219 2723 dev->out_pipe = usb_sndbulkpipe(udev,
1da177e4
LT
2724 info->ep_out);
2725 }
2726 if (dev->in_pipe)
2727 rtest = " bulk-in";
2728 if (dev->out_pipe)
2729 wtest = " bulk-out";
2730 if (dev->in_iso_pipe)
2731 irtest = " iso-in";
2732 if (dev->out_iso_pipe)
2733 iwtest = " iso-out";
457a0955
AV
2734 if (dev->in_int_pipe)
2735 intrtest = " int-in";
2736 if (dev->out_int_pipe)
2737 intwtest = " int-out";
1da177e4
LT
2738 }
2739
fabbf219
MF
2740 usb_set_intfdata(intf, dev);
2741 dev_info(&intf->dev, "%s\n", info->name);
457a0955 2742 dev_info(&intf->dev, "%s {control%s%s%s%s%s%s%s} tests%s\n",
e538dfda 2743 usb_speed_string(udev->speed),
1da177e4
LT
2744 info->ctrl_out ? " in/out" : "",
2745 rtest, wtest,
2746 irtest, iwtest,
457a0955 2747 intrtest, intwtest,
1da177e4
LT
2748 info->alt >= 0 ? " (+alt)" : "");
2749 return 0;
2750}
2751
fabbf219 2752static int usbtest_suspend(struct usb_interface *intf, pm_message_t message)
ff7c79e4 2753{
ff7c79e4
DB
2754 return 0;
2755}
2756
fabbf219 2757static int usbtest_resume(struct usb_interface *intf)
ff7c79e4 2758{
ff7c79e4
DB
2759 return 0;
2760}
2761
2762
fabbf219 2763static void usbtest_disconnect(struct usb_interface *intf)
1da177e4 2764{
fabbf219 2765 struct usbtest_dev *dev = usb_get_intfdata(intf);
1da177e4 2766
fabbf219
MF
2767 usb_set_intfdata(intf, NULL);
2768 dev_dbg(&intf->dev, "disconnect\n");
2769 kfree(dev);
1da177e4
LT
2770}
2771
2772/* Basic testing only needs a device that can source or sink bulk traffic.
2773 * Any device can test control transfers (default with GENERIC binding).
2774 *
2775 * Several entries work with the default EP0 implementation that's built
2776 * into EZ-USB chips. There's a default vendor ID which can be overridden
2777 * by (very) small config EEPROMS, but otherwise all these devices act
2778 * identically until firmware is loaded: only EP0 works. It turns out
2779 * to be easy to make other endpoints work, without modifying that EP0
2780 * behavior. For now, we expect that kind of firmware.
2781 */
2782
2783/* an21xx or fx versions of ez-usb */
2784static struct usbtest_info ez1_info = {
2785 .name = "EZ-USB device",
2786 .ep_in = 2,
2787 .ep_out = 2,
2788 .alt = 1,
2789};
2790
2791/* fx2 version of ez-usb */
2792static struct usbtest_info ez2_info = {
2793 .name = "FX2 device",
2794 .ep_in = 6,
2795 .ep_out = 2,
2796 .alt = 1,
2797};
2798
2799/* ezusb family device with dedicated usb test firmware,
2800 */
2801static struct usbtest_info fw_info = {
2802 .name = "usb test device",
2803 .ep_in = 2,
2804 .ep_out = 2,
2805 .alt = 1,
fabbf219 2806 .autoconf = 1, /* iso and ctrl_out need autoconf */
1da177e4 2807 .ctrl_out = 1,
fabbf219 2808 .iso = 1, /* iso_ep's are #8 in/out */
1da177e4
LT
2809};
2810
2811/* peripheral running Linux and 'zero.c' test firmware, or
2812 * its user-mode cousin. different versions of this use
2813 * different hardware with the same vendor/product codes.
2814 * host side MUST rely on the endpoint descriptors.
2815 */
2816static struct usbtest_info gz_info = {
2817 .name = "Linux gadget zero",
2818 .autoconf = 1,
2819 .ctrl_out = 1,
4b85c624 2820 .iso = 1,
457a0955 2821 .intr = 1,
1da177e4
LT
2822 .alt = 0,
2823};
2824
2825static struct usbtest_info um_info = {
2826 .name = "Linux user mode test driver",
2827 .autoconf = 1,
2828 .alt = -1,
2829};
2830
2831static struct usbtest_info um2_info = {
2832 .name = "Linux user mode ISO test driver",
2833 .autoconf = 1,
2834 .iso = 1,
2835 .alt = -1,
2836};
2837
2838#ifdef IBOT2
2839/* this is a nice source of high speed bulk data;
2840 * uses an FX2, with firmware provided in the device
2841 */
2842static struct usbtest_info ibot2_info = {
2843 .name = "iBOT2 webcam",
2844 .ep_in = 2,
2845 .alt = -1,
2846};
2847#endif
2848
2849#ifdef GENERIC
2850/* we can use any device to test control traffic */
2851static struct usbtest_info generic_info = {
2852 .name = "Generic USB device",
2853 .alt = -1,
2854};
2855#endif
2856
1da177e4 2857
33b9e162 2858static const struct usb_device_id id_table[] = {
1da177e4 2859
1da177e4
LT
2860 /*-------------------------------------------------------------*/
2861
2862 /* EZ-USB devices which download firmware to replace (or in our
2863 * case augment) the default device implementation.
2864 */
2865
2866 /* generic EZ-USB FX controller */
fabbf219 2867 { USB_DEVICE(0x0547, 0x2235),
1da177e4 2868 .driver_info = (unsigned long) &ez1_info,
fabbf219 2869 },
1da177e4
LT
2870
2871 /* CY3671 development board with EZ-USB FX */
fabbf219 2872 { USB_DEVICE(0x0547, 0x0080),
1da177e4 2873 .driver_info = (unsigned long) &ez1_info,
fabbf219 2874 },
1da177e4
LT
2875
2876 /* generic EZ-USB FX2 controller (or development board) */
fabbf219 2877 { USB_DEVICE(0x04b4, 0x8613),
1da177e4 2878 .driver_info = (unsigned long) &ez2_info,
fabbf219 2879 },
1da177e4
LT
2880
2881 /* re-enumerated usb test device firmware */
fabbf219 2882 { USB_DEVICE(0xfff0, 0xfff0),
1da177e4 2883 .driver_info = (unsigned long) &fw_info,
fabbf219 2884 },
1da177e4
LT
2885
2886 /* "Gadget Zero" firmware runs under Linux */
fabbf219 2887 { USB_DEVICE(0x0525, 0xa4a0),
1da177e4 2888 .driver_info = (unsigned long) &gz_info,
fabbf219 2889 },
1da177e4
LT
2890
2891 /* so does a user-mode variant */
fabbf219 2892 { USB_DEVICE(0x0525, 0xa4a4),
1da177e4 2893 .driver_info = (unsigned long) &um_info,
fabbf219 2894 },
1da177e4
LT
2895
2896 /* ... and a user-mode variant that talks iso */
fabbf219 2897 { USB_DEVICE(0x0525, 0xa4a3),
1da177e4 2898 .driver_info = (unsigned long) &um2_info,
fabbf219 2899 },
1da177e4
LT
2900
2901#ifdef KEYSPAN_19Qi
2902 /* Keyspan 19qi uses an21xx (original EZ-USB) */
fabbf219
MF
2903 /* this does not coexist with the real Keyspan 19qi driver! */
2904 { USB_DEVICE(0x06cd, 0x010b),
1da177e4 2905 .driver_info = (unsigned long) &ez1_info,
fabbf219 2906 },
1da177e4
LT
2907#endif
2908
2909 /*-------------------------------------------------------------*/
2910
2911#ifdef IBOT2
2912 /* iBOT2 makes a nice source of high speed bulk-in data */
fabbf219
MF
2913 /* this does not coexist with a real iBOT2 driver! */
2914 { USB_DEVICE(0x0b62, 0x0059),
1da177e4 2915 .driver_info = (unsigned long) &ibot2_info,
fabbf219 2916 },
1da177e4
LT
2917#endif
2918
2919 /*-------------------------------------------------------------*/
2920
2921#ifdef GENERIC
2922 /* module params can specify devices to use for control tests */
2923 { .driver_info = (unsigned long) &generic_info, },
2924#endif
2925
2926 /*-------------------------------------------------------------*/
2927
2928 { }
2929};
fabbf219 2930MODULE_DEVICE_TABLE(usb, id_table);
1da177e4
LT
2931
2932static struct usb_driver usbtest_driver = {
1da177e4
LT
2933 .name = "usbtest",
2934 .id_table = id_table,
2935 .probe = usbtest_probe,
c532b29a 2936 .unlocked_ioctl = usbtest_ioctl,
1da177e4 2937 .disconnect = usbtest_disconnect,
ff7c79e4
DB
2938 .suspend = usbtest_suspend,
2939 .resume = usbtest_resume,
1da177e4
LT
2940};
2941
2942/*-------------------------------------------------------------------------*/
2943
fabbf219 2944static int __init usbtest_init(void)
1da177e4
LT
2945{
2946#ifdef GENERIC
2947 if (vendor)
28ffd79c 2948 pr_debug("params: vend=0x%04x prod=0x%04x\n", vendor, product);
1da177e4 2949#endif
fabbf219 2950 return usb_register(&usbtest_driver);
1da177e4 2951}
fabbf219 2952module_init(usbtest_init);
1da177e4 2953
fabbf219 2954static void __exit usbtest_exit(void)
1da177e4 2955{
fabbf219 2956 usb_deregister(&usbtest_driver);
1da177e4 2957}
fabbf219 2958module_exit(usbtest_exit);
1da177e4 2959
fabbf219
MF
2960MODULE_DESCRIPTION("USB Core/HCD Testing Driver");
2961MODULE_LICENSE("GPL");
1da177e4 2962