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