blob: 8078ba23eceac80d131f0a3f392a7e2aa0c87407 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/kernel.h>
2#include <linux/errno.h>
3#include <linux/init.h>
4#include <linux/slab.h>
5#include <linux/mm.h>
6#include <linux/module.h>
7#include <linux/moduleparam.h>
David Hardeman378f0582005-09-17 17:55:31 +10008#include <linux/scatterlist.h>
Matthias Kaehlcke1cfab022007-12-13 16:15:33 -08009#include <linux/mutex.h>
Alan Stern35a58e82014-06-03 11:11:34 -040010#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/usb.h>
12
13
14/*-------------------------------------------------------------------------*/
15
Martin Fuzzeyfabbf212010-10-01 00:20:42 +020016/* FIXME make these public somewhere; usbdevfs.h? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017struct usbtest_param {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +020018 /* inputs */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 unsigned test_num; /* 0..(TEST_CASES-1) */
20 unsigned iterations;
21 unsigned length;
22 unsigned vary;
23 unsigned sglen;
24
Martin Fuzzeyfabbf212010-10-01 00:20:42 +020025 /* outputs */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 struct timeval duration;
27};
28#define USBTEST_REQUEST _IOWR('U', 100, struct usbtest_param)
29
30/*-------------------------------------------------------------------------*/
31
32#define GENERIC /* let probe() bind using module params */
33
34/* Some devices that can be used for testing will have "real" drivers.
35 * Entries for those need to be enabled here by hand, after disabling
36 * that "real" driver.
37 */
38//#define IBOT2 /* grab iBOT2 webcams */
39//#define KEYSPAN_19Qi /* grab un-renumerated serial adapter */
40
41/*-------------------------------------------------------------------------*/
42
43struct usbtest_info {
44 const char *name;
45 u8 ep_in; /* bulk/intr source */
46 u8 ep_out; /* bulk/intr sink */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +020047 unsigned autoconf:1;
48 unsigned ctrl_out:1;
49 unsigned iso:1; /* try iso in/out */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 int alt;
51};
52
53/* this is accessed only through usbfs ioctl calls.
54 * one ioctl to issue a test ... one lock per device.
55 * tests create other threads if they need them.
56 * urbs and buffers are allocated dynamically,
57 * and data generated deterministically.
58 */
59struct usbtest_dev {
60 struct usb_interface *intf;
61 struct usbtest_info *info;
62 int in_pipe;
63 int out_pipe;
64 int in_iso_pipe;
65 int out_iso_pipe;
66 struct usb_endpoint_descriptor *iso_in, *iso_out;
Matthias Kaehlcke1cfab022007-12-13 16:15:33 -080067 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define TBUF_SIZE 256
70 u8 *buf;
71};
72
Martin Fuzzeyfabbf212010-10-01 00:20:42 +020073static struct usb_device *testdev_to_usbdev(struct usbtest_dev *test)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Martin Fuzzeyfabbf212010-10-01 00:20:42 +020075 return interface_to_usbdev(test->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
78/* set up all urbs so they can be used with either bulk or interrupt */
79#define INTERRUPT_RATE 1 /* msec/transfer */
80
David Brownell28ffd792008-04-25 18:51:10 -070081#define ERROR(tdev, fmt, args...) \
82 dev_err(&(tdev)->intf->dev , fmt , ## args)
Arjan van de Venb6c63932008-07-25 01:45:52 -070083#define WARNING(tdev, fmt, args...) \
David Brownell28ffd792008-04-25 18:51:10 -070084 dev_warn(&(tdev)->intf->dev , fmt , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Martin Fuzzey084fb202011-01-16 19:17:11 +010086#define GUARD_BYTE 0xA5
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/*-------------------------------------------------------------------------*/
89
90static int
Martin Fuzzeyfabbf212010-10-01 00:20:42 +020091get_endpoints(struct usbtest_dev *dev, struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 int tmp;
94 struct usb_host_interface *alt;
95 struct usb_host_endpoint *in, *out;
96 struct usb_host_endpoint *iso_in, *iso_out;
97 struct usb_device *udev;
98
99 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
100 unsigned ep;
101
102 in = out = NULL;
103 iso_in = iso_out = NULL;
104 alt = intf->altsetting + tmp;
105
106 /* take the first altsetting with in-bulk + out-bulk;
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200107 * ignore other endpoints and altsettings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 */
109 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
110 struct usb_host_endpoint *e;
111
112 e = alt->endpoint + ep;
113 switch (e->desc.bmAttributes) {
114 case USB_ENDPOINT_XFER_BULK:
115 break;
116 case USB_ENDPOINT_XFER_ISOC:
117 if (dev->info->iso)
118 goto try_iso;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200119 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 default:
121 continue;
122 }
Luiz Fernando N. Capitulino4d823dd2006-10-26 13:03:02 -0300123 if (usb_endpoint_dir_in(&e->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (!in)
125 in = e;
126 } else {
127 if (!out)
128 out = e;
129 }
130 continue;
131try_iso:
Luiz Fernando N. Capitulino4d823dd2006-10-26 13:03:02 -0300132 if (usb_endpoint_dir_in(&e->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (!iso_in)
134 iso_in = e;
135 } else {
136 if (!iso_out)
137 iso_out = e;
138 }
139 }
Ming Lei951fd8e2010-08-02 22:09:17 +0800140 if ((in && out) || iso_in || iso_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 goto found;
142 }
143 return -EINVAL;
144
145found:
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200146 udev = testdev_to_usbdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (alt->desc.bAlternateSetting != 0) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200148 tmp = usb_set_interface(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 alt->desc.bInterfaceNumber,
150 alt->desc.bAlternateSetting);
151 if (tmp < 0)
152 return tmp;
153 }
154
155 if (in) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200156 dev->in_pipe = usb_rcvbulkpipe(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200158 dev->out_pipe = usb_sndbulkpipe(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
160 }
161 if (iso_in) {
162 dev->iso_in = &iso_in->desc;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200163 dev->in_iso_pipe = usb_rcvisocpipe(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 iso_in->desc.bEndpointAddress
165 & USB_ENDPOINT_NUMBER_MASK);
Ming Lei951fd8e2010-08-02 22:09:17 +0800166 }
167
168 if (iso_out) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 dev->iso_out = &iso_out->desc;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200170 dev->out_iso_pipe = usb_sndisocpipe(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 iso_out->desc.bEndpointAddress
172 & USB_ENDPOINT_NUMBER_MASK);
173 }
174 return 0;
175}
176
177/*-------------------------------------------------------------------------*/
178
179/* Support for testing basic non-queued I/O streams.
180 *
181 * These just package urbs as requests that can be easily canceled.
182 * Each urb's data buffer is dynamically allocated; callers can fill
183 * them with non-zero test data (or test for it) when appropriate.
184 */
185
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200186static void simple_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Ming Leicdc97792008-02-24 18:41:47 +0800188 complete(urb->context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Martin Fuzzey084fb202011-01-16 19:17:11 +0100191static struct urb *usbtest_alloc_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 struct usb_device *udev,
193 int pipe,
Martin Fuzzey084fb202011-01-16 19:17:11 +0100194 unsigned long bytes,
195 unsigned transfer_flags,
196 unsigned offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 struct urb *urb;
199
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200200 urb = usb_alloc_urb(0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (!urb)
202 return urb;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200203 usb_fill_bulk_urb(urb, udev, pipe, NULL, bytes, simple_callback, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 urb->interval = (udev->speed == USB_SPEED_HIGH)
205 ? (INTERRUPT_RATE << 3)
206 : INTERRUPT_RATE;
Martin Fuzzey084fb202011-01-16 19:17:11 +0100207 urb->transfer_flags = transfer_flags;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200208 if (usb_pipein(pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 urb->transfer_flags |= URB_SHORT_NOT_OK;
Martin Fuzzey084fb202011-01-16 19:17:11 +0100210
211 if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
212 urb->transfer_buffer = usb_alloc_coherent(udev, bytes + offset,
213 GFP_KERNEL, &urb->transfer_dma);
214 else
215 urb->transfer_buffer = kmalloc(bytes + offset, GFP_KERNEL);
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (!urb->transfer_buffer) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200218 usb_free_urb(urb);
Martin Fuzzey084fb202011-01-16 19:17:11 +0100219 return NULL;
220 }
221
222 /* To test unaligned transfers add an offset and fill the
223 unused memory with a guard value */
224 if (offset) {
225 memset(urb->transfer_buffer, GUARD_BYTE, offset);
226 urb->transfer_buffer += offset;
227 if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
228 urb->transfer_dma += offset;
229 }
230
231 /* For inbound transfers use guard byte so that test fails if
232 data not correctly copied */
233 memset(urb->transfer_buffer,
234 usb_pipein(urb->pipe) ? GUARD_BYTE : 0,
235 bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return urb;
237}
238
Martin Fuzzey084fb202011-01-16 19:17:11 +0100239static struct urb *simple_alloc_urb(
240 struct usb_device *udev,
241 int pipe,
242 unsigned long bytes)
243{
244 return usbtest_alloc_urb(udev, pipe, bytes, URB_NO_TRANSFER_DMA_MAP, 0);
245}
246
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200247static unsigned pattern;
Vikram Pandita7f4e9852009-11-09 21:24:32 -0600248static unsigned mod_pattern;
249module_param_named(pattern, mod_pattern, uint, S_IRUGO | S_IWUSR);
250MODULE_PARM_DESC(mod_pattern, "i/o pattern (0 == zeroes)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200252static inline void simple_fill_buf(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 unsigned i;
255 u8 *buf = urb->transfer_buffer;
256 unsigned len = urb->transfer_buffer_length;
257
258 switch (pattern) {
259 default:
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200260 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 case 0:
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200262 memset(buf, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 break;
264 case 1: /* mod63 */
265 for (i = 0; i < len; i++)
266 *buf++ = (u8) (i % 63);
267 break;
268 }
269}
270
Greg Dietsche304b0b52011-05-08 22:51:43 -0500271static inline unsigned long buffer_offset(void *buf)
Martin Fuzzey084fb202011-01-16 19:17:11 +0100272{
Greg Dietsche304b0b52011-05-08 22:51:43 -0500273 return (unsigned long)buf & (ARCH_KMALLOC_MINALIGN - 1);
Martin Fuzzey084fb202011-01-16 19:17:11 +0100274}
275
276static int check_guard_bytes(struct usbtest_dev *tdev, struct urb *urb)
277{
278 u8 *buf = urb->transfer_buffer;
279 u8 *guard = buf - buffer_offset(buf);
280 unsigned i;
281
282 for (i = 0; guard < buf; i++, guard++) {
283 if (*guard != GUARD_BYTE) {
284 ERROR(tdev, "guard byte[%d] %d (not %d)\n",
285 i, *guard, GUARD_BYTE);
286 return -EINVAL;
287 }
288 }
289 return 0;
290}
291
292static int simple_check_buf(struct usbtest_dev *tdev, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 unsigned i;
295 u8 expected;
296 u8 *buf = urb->transfer_buffer;
297 unsigned len = urb->actual_length;
298
Martin Fuzzey084fb202011-01-16 19:17:11 +0100299 int ret = check_guard_bytes(tdev, urb);
300 if (ret)
301 return ret;
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 for (i = 0; i < len; i++, buf++) {
304 switch (pattern) {
305 /* all-zeroes has no synchronization issues */
306 case 0:
307 expected = 0;
308 break;
309 /* mod63 stays in sync with short-terminated transfers,
310 * or otherwise when host and gadget agree on how large
311 * each usb transfer request should be. resync is done
312 * with set_interface or set_config.
313 */
314 case 1: /* mod63 */
315 expected = i % 63;
316 break;
317 /* always fail unsupported patterns */
318 default:
319 expected = !*buf;
320 break;
321 }
322 if (*buf == expected)
323 continue;
David Brownell28ffd792008-04-25 18:51:10 -0700324 ERROR(tdev, "buf[%d] = %d (not %d)\n", i, *buf, expected);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return -EINVAL;
326 }
327 return 0;
328}
329
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200330static void simple_free_urb(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Greg Dietsche304b0b52011-05-08 22:51:43 -0500332 unsigned long offset = buffer_offset(urb->transfer_buffer);
Martin Fuzzey084fb202011-01-16 19:17:11 +0100333
334 if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
335 usb_free_coherent(
336 urb->dev,
337 urb->transfer_buffer_length + offset,
338 urb->transfer_buffer - offset,
339 urb->transfer_dma - offset);
340 else
341 kfree(urb->transfer_buffer - offset);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200342 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200345static int simple_io(
David Brownell28ffd792008-04-25 18:51:10 -0700346 struct usbtest_dev *tdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct urb *urb,
348 int iterations,
349 int vary,
350 int expected,
351 const char *label
352)
353{
354 struct usb_device *udev = urb->dev;
355 int max = urb->transfer_buffer_length;
356 struct completion completion;
357 int retval = 0;
358
359 urb->context = &completion;
360 while (retval == 0 && iterations-- > 0) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200361 init_completion(&completion);
Sebastian Andrzej Siewior7c79d092011-08-23 10:44:54 +0200362 if (usb_pipeout(urb->pipe)) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200363 simple_fill_buf(urb);
Sebastian Andrzej Siewior7c79d092011-08-23 10:44:54 +0200364 urb->transfer_flags |= URB_ZERO_PACKET;
365 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200366 retval = usb_submit_urb(urb, GFP_KERNEL);
367 if (retval != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 break;
369
370 /* NOTE: no timeouts; can't be broken out of by interrupt */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200371 wait_for_completion(&completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 retval = urb->status;
373 urb->dev = udev;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200374 if (retval == 0 && usb_pipein(urb->pipe))
David Brownell28ffd792008-04-25 18:51:10 -0700375 retval = simple_check_buf(tdev, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 if (vary) {
378 int len = urb->transfer_buffer_length;
379
380 len += vary;
381 len %= max;
382 if (len == 0)
383 len = (vary < max) ? vary : max;
384 urb->transfer_buffer_length = len;
385 }
386
387 /* FIXME if endpoint halted, clear halt (and log) */
388 }
389 urb->transfer_buffer_length = max;
390
391 if (expected != retval)
David Brownell28ffd792008-04-25 18:51:10 -0700392 dev_err(&udev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 "%s failed, iterations left %d, status %d (not %d)\n",
394 label, iterations, retval, expected);
395 return retval;
396}
397
398
399/*-------------------------------------------------------------------------*/
400
401/* We use scatterlist primitives to test queued I/O.
402 * Yes, this also tests the scatterlist primitives.
403 */
404
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200405static void free_sglist(struct scatterlist *sg, int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 unsigned i;
David Brownell28ffd792008-04-25 18:51:10 -0700408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (!sg)
410 return;
411 for (i = 0; i < nents; i++) {
Jens Axboe45711f12007-10-22 21:19:53 +0200412 if (!sg_page(&sg[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 continue;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200414 kfree(sg_virt(&sg[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200416 kfree(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419static struct scatterlist *
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200420alloc_sglist(int nents, int max, int vary)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct scatterlist *sg;
423 unsigned i;
424 unsigned size = max;
425
Xi Wang8bde9a62012-04-09 15:48:45 -0400426 sg = kmalloc_array(nents, sizeof *sg, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (!sg)
428 return NULL;
Alan Stern4756feb2008-03-27 10:15:22 -0400429 sg_init_table(sg, nents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 for (i = 0; i < nents; i++) {
432 char *buf;
David Brownell8b524902006-04-02 10:20:15 -0800433 unsigned j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200435 buf = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (!buf) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200437 free_sglist(sg, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return NULL;
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 /* kmalloc pages are always physically contiguous! */
Alan Stern4756feb2008-03-27 10:15:22 -0400442 sg_set_buf(&sg[i], buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
David Brownell8b524902006-04-02 10:20:15 -0800444 switch (pattern) {
445 case 0:
446 /* already zeroed */
447 break;
448 case 1:
449 for (j = 0; j < size; j++)
450 *buf++ = (u8) (j % 63);
451 break;
452 }
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (vary) {
455 size += vary;
456 size %= max;
457 if (size == 0)
458 size = (vary < max) ? vary : max;
459 }
460 }
461
462 return sg;
463}
464
Alan Stern35a58e82014-06-03 11:11:34 -0400465static void sg_timeout(unsigned long _req)
466{
467 struct usb_sg_request *req = (struct usb_sg_request *) _req;
468
469 req->status = -ETIMEDOUT;
470 usb_sg_cancel(req);
471}
472
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200473static int perform_sglist(
David Brownell28ffd792008-04-25 18:51:10 -0700474 struct usbtest_dev *tdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 unsigned iterations,
476 int pipe,
477 struct usb_sg_request *req,
478 struct scatterlist *sg,
479 int nents
480)
481{
David Brownell28ffd792008-04-25 18:51:10 -0700482 struct usb_device *udev = testdev_to_usbdev(tdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 int retval = 0;
Alan Stern35a58e82014-06-03 11:11:34 -0400484 struct timer_list sg_timer;
485
486 setup_timer_on_stack(&sg_timer, sg_timeout, (unsigned long) req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 while (retval == 0 && iterations-- > 0) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200489 retval = usb_sg_init(req, udev, pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 (udev->speed == USB_SPEED_HIGH)
491 ? (INTERRUPT_RATE << 3)
492 : INTERRUPT_RATE,
Christoph Lametere94b1762006-12-06 20:33:17 -0800493 sg, nents, 0, GFP_KERNEL);
David Brownell28ffd792008-04-25 18:51:10 -0700494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (retval)
496 break;
Alan Stern35a58e82014-06-03 11:11:34 -0400497 mod_timer(&sg_timer, jiffies +
498 msecs_to_jiffies(SIMPLE_IO_TIMEOUT));
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200499 usb_sg_wait(req);
Alan Stern35a58e82014-06-03 11:11:34 -0400500 del_timer_sync(&sg_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 retval = req->status;
502
David Brownell8b524902006-04-02 10:20:15 -0800503 /* FIXME check resulting data pattern */
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /* FIXME if endpoint halted, clear halt (and log) */
506 }
507
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200508 /* FIXME for unlink or fault handling tests, don't report
509 * failure if retval is as we expected ...
510 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (retval)
David Brownell28ffd792008-04-25 18:51:10 -0700512 ERROR(tdev, "perform_sglist failed, "
513 "iterations left %d, status %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 iterations, retval);
515 return retval;
516}
517
518
519/*-------------------------------------------------------------------------*/
520
521/* unqueued control message testing
522 *
523 * there's a nice set of device functional requirements in chapter 9 of the
524 * usb 2.0 spec, which we can apply to ANY device, even ones that don't use
525 * special test firmware.
526 *
527 * we know the device is configured (or suspended) by the time it's visible
528 * through usbfs. we can't change that, so we won't test enumeration (which
529 * worked 'well enough' to get here, this time), power management (ditto),
530 * or remote wakeup (which needs human interaction).
531 */
532
533static unsigned realworld = 1;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200534module_param(realworld, uint, 0);
535MODULE_PARM_DESC(realworld, "clear to demand stricter spec compliance");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200537static int get_altsetting(struct usbtest_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 struct usb_interface *iface = dev->intf;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200540 struct usb_device *udev = interface_to_usbdev(iface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 int retval;
542
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200543 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 USB_REQ_GET_INTERFACE, USB_DIR_IN|USB_RECIP_INTERFACE,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200545 0, iface->altsetting[0].desc.bInterfaceNumber,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 dev->buf, 1, USB_CTRL_GET_TIMEOUT);
547 switch (retval) {
548 case 1:
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200549 return dev->buf[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 case 0:
551 retval = -ERANGE;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200552 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 default:
554 return retval;
555 }
556}
557
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200558static int set_altsetting(struct usbtest_dev *dev, int alternate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 struct usb_interface *iface = dev->intf;
561 struct usb_device *udev;
562
563 if (alternate < 0 || alternate >= 256)
564 return -EINVAL;
565
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200566 udev = interface_to_usbdev(iface);
567 return usb_set_interface(udev,
568 iface->altsetting[0].desc.bInterfaceNumber,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 alternate);
570}
571
David Brownell28ffd792008-04-25 18:51:10 -0700572static int is_good_config(struct usbtest_dev *tdev, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 struct usb_config_descriptor *config;
David Brownell28ffd792008-04-25 18:51:10 -0700575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (len < sizeof *config)
577 return 0;
David Brownell28ffd792008-04-25 18:51:10 -0700578 config = (struct usb_config_descriptor *) tdev->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 switch (config->bDescriptorType) {
581 case USB_DT_CONFIG:
582 case USB_DT_OTHER_SPEED_CONFIG:
583 if (config->bLength != 9) {
David Brownell28ffd792008-04-25 18:51:10 -0700584 ERROR(tdev, "bogus config descriptor length\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return 0;
586 }
587 /* this bit 'must be 1' but often isn't */
588 if (!realworld && !(config->bmAttributes & 0x80)) {
David Brownell28ffd792008-04-25 18:51:10 -0700589 ERROR(tdev, "high bit of config attributes not set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return 0;
591 }
592 if (config->bmAttributes & 0x1f) { /* reserved == 0 */
David Brownell28ffd792008-04-25 18:51:10 -0700593 ERROR(tdev, "reserved config bits set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return 0;
595 }
596 break;
597 default:
598 return 0;
599 }
600
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200601 if (le16_to_cpu(config->wTotalLength) == len) /* read it all */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return 1;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200603 if (le16_to_cpu(config->wTotalLength) >= TBUF_SIZE) /* max partial read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return 1;
David Brownell28ffd792008-04-25 18:51:10 -0700605 ERROR(tdev, "bogus config descriptor read size\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return 0;
607}
608
609/* sanity test for standard requests working with usb_control_mesg() and some
610 * of the utility functions which use it.
611 *
612 * this doesn't test how endpoint halts behave or data toggles get set, since
613 * we won't do I/O to bulk/interrupt endpoints here (which is how to change
614 * halt or toggle). toggle testing is impractical without support from hcds.
615 *
616 * this avoids failing devices linux would normally work with, by not testing
617 * config/altsetting operations for devices that only support their defaults.
618 * such devices rarely support those needless operations.
619 *
620 * NOTE that since this is a sanity test, it's not examining boundary cases
621 * to see if usbcore, hcd, and device all behave right. such testing would
622 * involve varied read sizes and other operation sequences.
623 */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200624static int ch9_postconfig(struct usbtest_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 struct usb_interface *iface = dev->intf;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200627 struct usb_device *udev = interface_to_usbdev(iface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 int i, alt, retval;
629
630 /* [9.2.3] if there's more than one altsetting, we need to be able to
631 * set and get each one. mostly trusts the descriptors from usbcore.
632 */
633 for (i = 0; i < iface->num_altsetting; i++) {
634
635 /* 9.2.3 constrains the range here */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200636 alt = iface->altsetting[i].desc.bAlternateSetting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (alt < 0 || alt >= iface->num_altsetting) {
David Brownell28ffd792008-04-25 18:51:10 -0700638 dev_err(&iface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 "invalid alt [%d].bAltSetting = %d\n",
640 i, alt);
641 }
642
643 /* [real world] get/set unimplemented if there's only one */
644 if (realworld && iface->num_altsetting == 1)
645 continue;
646
647 /* [9.4.10] set_interface */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200648 retval = set_altsetting(dev, alt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (retval) {
David Brownell28ffd792008-04-25 18:51:10 -0700650 dev_err(&iface->dev, "can't set_interface = %d, %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 alt, retval);
652 return retval;
653 }
654
655 /* [9.4.4] get_interface always works */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200656 retval = get_altsetting(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (retval != alt) {
David Brownell28ffd792008-04-25 18:51:10 -0700658 dev_err(&iface->dev, "get alt should be %d, was %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 alt, retval);
660 return (retval < 0) ? retval : -EDOM;
661 }
662
663 }
664
665 /* [real world] get_config unimplemented if there's only one */
666 if (!realworld || udev->descriptor.bNumConfigurations != 1) {
667 int expected = udev->actconfig->desc.bConfigurationValue;
668
669 /* [9.4.2] get_configuration always works
670 * ... although some cheap devices (like one TI Hub I've got)
671 * won't return config descriptors except before set_config.
672 */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200673 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 USB_REQ_GET_CONFIGURATION,
675 USB_DIR_IN | USB_RECIP_DEVICE,
676 0, 0, dev->buf, 1, USB_CTRL_GET_TIMEOUT);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200677 if (retval != 1 || dev->buf[0] != expected) {
David Brownell28ffd792008-04-25 18:51:10 -0700678 dev_err(&iface->dev, "get config --> %d %d (1 %d)\n",
David Brownellff7c79e2005-04-22 13:17:00 -0700679 retval, dev->buf[0], expected);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return (retval < 0) ? retval : -EDOM;
681 }
682 }
683
684 /* there's always [9.4.3] a device descriptor [9.6.1] */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200685 retval = usb_get_descriptor(udev, USB_DT_DEVICE, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 dev->buf, sizeof udev->descriptor);
687 if (retval != sizeof udev->descriptor) {
David Brownell28ffd792008-04-25 18:51:10 -0700688 dev_err(&iface->dev, "dev descriptor --> %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return (retval < 0) ? retval : -EDOM;
690 }
691
692 /* there's always [9.4.3] at least one config descriptor [9.6.3] */
693 for (i = 0; i < udev->descriptor.bNumConfigurations; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200694 retval = usb_get_descriptor(udev, USB_DT_CONFIG, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 dev->buf, TBUF_SIZE);
David Brownell28ffd792008-04-25 18:51:10 -0700696 if (!is_good_config(dev, retval)) {
697 dev_err(&iface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 "config [%d] descriptor --> %d\n",
699 i, retval);
700 return (retval < 0) ? retval : -EDOM;
701 }
702
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200703 /* FIXME cross-checking udev->config[i] to make sure usbcore
704 * parsed it right (etc) would be good testing paranoia
705 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707
708 /* and sometimes [9.2.6.6] speed dependent descriptors */
709 if (le16_to_cpu(udev->descriptor.bcdUSB) == 0x0200) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200710 struct usb_qualifier_descriptor *d = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 /* device qualifier [9.6.2] */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200713 retval = usb_get_descriptor(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 USB_DT_DEVICE_QUALIFIER, 0, dev->buf,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200715 sizeof(struct usb_qualifier_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (retval == -EPIPE) {
717 if (udev->speed == USB_SPEED_HIGH) {
David Brownell28ffd792008-04-25 18:51:10 -0700718 dev_err(&iface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 "hs dev qualifier --> %d\n",
720 retval);
721 return (retval < 0) ? retval : -EDOM;
722 }
723 /* usb2.0 but not high-speed capable; fine */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200724 } else if (retval != sizeof(struct usb_qualifier_descriptor)) {
David Brownell28ffd792008-04-25 18:51:10 -0700725 dev_err(&iface->dev, "dev qualifier --> %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return (retval < 0) ? retval : -EDOM;
727 } else
728 d = (struct usb_qualifier_descriptor *) dev->buf;
729
730 /* might not have [9.6.2] any other-speed configs [9.6.4] */
731 if (d) {
732 unsigned max = d->bNumConfigurations;
733 for (i = 0; i < max; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200734 retval = usb_get_descriptor(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 USB_DT_OTHER_SPEED_CONFIG, i,
736 dev->buf, TBUF_SIZE);
David Brownell28ffd792008-04-25 18:51:10 -0700737 if (!is_good_config(dev, retval)) {
738 dev_err(&iface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 "other speed config --> %d\n",
740 retval);
741 return (retval < 0) ? retval : -EDOM;
742 }
743 }
744 }
745 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200746 /* FIXME fetch strings from at least the device descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 /* [9.4.5] get_status always works */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200749 retval = usb_get_status(udev, USB_RECIP_DEVICE, 0, dev->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (retval != 2) {
David Brownell28ffd792008-04-25 18:51:10 -0700751 dev_err(&iface->dev, "get dev status --> %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return (retval < 0) ? retval : -EDOM;
753 }
754
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200755 /* FIXME configuration.bmAttributes says if we could try to set/clear
756 * the device's remote wakeup feature ... if we can, test that here
757 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200759 retval = usb_get_status(udev, USB_RECIP_INTERFACE,
760 iface->altsetting[0].desc.bInterfaceNumber, dev->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (retval != 2) {
David Brownell28ffd792008-04-25 18:51:10 -0700762 dev_err(&iface->dev, "get interface status --> %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return (retval < 0) ? retval : -EDOM;
764 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200765 /* FIXME get status for each endpoint in the interface */
David Brownell28ffd792008-04-25 18:51:10 -0700766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return 0;
768}
769
770/*-------------------------------------------------------------------------*/
771
772/* use ch9 requests to test whether:
773 * (a) queues work for control, keeping N subtests queued and
774 * active (auto-resubmit) for M loops through the queue.
775 * (b) protocol stalls (control-only) will autorecover.
776 * it's not like bulk/intr; no halt clearing.
777 * (c) short control reads are reported and handled.
778 * (d) queues are always processed in-order
779 */
780
781struct ctrl_ctx {
782 spinlock_t lock;
783 struct usbtest_dev *dev;
784 struct completion complete;
785 unsigned count;
786 unsigned pending;
787 int status;
788 struct urb **urb;
789 struct usbtest_param *param;
790 int last;
791};
792
793#define NUM_SUBCASES 15 /* how many test subcases here? */
794
795struct subcase {
796 struct usb_ctrlrequest setup;
797 int number;
798 int expected;
799};
800
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200801static void ctrl_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 struct ctrl_ctx *ctx = urb->context;
804 struct usb_ctrlrequest *reqp;
805 struct subcase *subcase;
806 int status = urb->status;
807
808 reqp = (struct usb_ctrlrequest *)urb->setup_packet;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200809 subcase = container_of(reqp, struct subcase, setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200811 spin_lock(&ctx->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 ctx->count--;
813 ctx->pending--;
814
815 /* queue must transfer and complete in fifo order, unless
816 * usb_unlink_urb() is used to unlink something not at the
817 * physical queue head (not tested).
818 */
819 if (subcase->number > 0) {
820 if ((subcase->number - ctx->last) != 1) {
David Brownell28ffd792008-04-25 18:51:10 -0700821 ERROR(ctx->dev,
822 "subcase %d completed out of order, last %d\n",
823 subcase->number, ctx->last);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 status = -EDOM;
825 ctx->last = subcase->number;
826 goto error;
827 }
828 }
829 ctx->last = subcase->number;
830
831 /* succeed or fault in only one way? */
832 if (status == subcase->expected)
833 status = 0;
834
835 /* async unlink for cleanup? */
836 else if (status != -ECONNRESET) {
837
838 /* some faults are allowed, not required */
839 if (subcase->expected > 0 && (
Greg Kroah-Hartman59d99782007-07-18 10:58:02 -0700840 ((status == -subcase->expected /* happened */
841 || status == 0)))) /* didn't */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 status = 0;
843 /* sometimes more than one fault is allowed */
844 else if (subcase->number == 12 && status == -EPIPE)
845 status = 0;
846 else
David Brownell28ffd792008-04-25 18:51:10 -0700847 ERROR(ctx->dev, "subtest %d error, status %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 subcase->number, status);
849 }
850
851 /* unexpected status codes mean errors; ideally, in hardware */
852 if (status) {
853error:
854 if (ctx->status == 0) {
855 int i;
856
857 ctx->status = status;
David Brownell28ffd792008-04-25 18:51:10 -0700858 ERROR(ctx->dev, "control queue %02x.%02x, err %d, "
859 "%d left, subcase %d, len %d/%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 reqp->bRequestType, reqp->bRequest,
David Brownell28ffd792008-04-25 18:51:10 -0700861 status, ctx->count, subcase->number,
862 urb->actual_length,
863 urb->transfer_buffer_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 /* FIXME this "unlink everything" exit route should
866 * be a separate test case.
867 */
868
869 /* unlink whatever's still pending */
870 for (i = 1; i < ctx->param->sglen; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200871 struct urb *u = ctx->urb[
872 (i + subcase->number)
873 % ctx->param->sglen];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 if (u == urb || !u->dev)
876 continue;
Franck Bui-Huucaa2a122006-05-15 19:23:53 +0200877 spin_unlock(&ctx->lock);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200878 status = usb_unlink_urb(u);
Franck Bui-Huucaa2a122006-05-15 19:23:53 +0200879 spin_lock(&ctx->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 switch (status) {
881 case -EINPROGRESS:
882 case -EBUSY:
883 case -EIDRM:
884 continue;
885 default:
David Brownell28ffd792008-04-25 18:51:10 -0700886 ERROR(ctx->dev, "urb unlink --> %d\n",
887 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889 }
890 status = ctx->status;
891 }
892 }
893
894 /* resubmit if we need to, else mark this as done */
895 if ((status == 0) && (ctx->pending < ctx->count)) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200896 status = usb_submit_urb(urb, GFP_ATOMIC);
897 if (status != 0) {
David Brownell28ffd792008-04-25 18:51:10 -0700898 ERROR(ctx->dev,
899 "can't resubmit ctrl %02x.%02x, err %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 reqp->bRequestType, reqp->bRequest, status);
901 urb->dev = NULL;
902 } else
903 ctx->pending++;
904 } else
905 urb->dev = NULL;
David Brownell28ffd792008-04-25 18:51:10 -0700906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 /* signal completion when nothing's queued */
908 if (ctx->pending == 0)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200909 complete(&ctx->complete);
910 spin_unlock(&ctx->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
913static int
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200914test_ctrl_queue(struct usbtest_dev *dev, struct usbtest_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200916 struct usb_device *udev = testdev_to_usbdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct urb **urb;
918 struct ctrl_ctx context;
919 int i;
920
Xi Wange65cdfa2012-04-09 15:48:55 -0400921 if (param->sglen == 0 || param->iterations > UINT_MAX / param->sglen)
922 return -EOPNOTSUPP;
923
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200924 spin_lock_init(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 context.dev = dev;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200926 init_completion(&context.complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 context.count = param->sglen * param->iterations;
928 context.pending = 0;
929 context.status = -ENOMEM;
930 context.param = param;
931 context.last = -1;
932
933 /* allocate and init the urbs we'll queue.
934 * as with bulk/intr sglists, sglen is the queue depth; it also
935 * controls which subtests run (more tests than sglen) or rerun.
936 */
Christoph Lametere94b1762006-12-06 20:33:17 -0800937 urb = kcalloc(param->sglen, sizeof(struct urb *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (!urb)
939 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 for (i = 0; i < param->sglen; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200941 int pipe = usb_rcvctrlpipe(udev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 unsigned len;
943 struct urb *u;
944 struct usb_ctrlrequest req;
945 struct subcase *reqp;
Marcin Slusarz6def7552008-05-12 20:17:25 +0200946
947 /* sign of this variable means:
948 * -: tested code must return this (negative) error code
949 * +: tested code may return this (negative too) error code
950 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 int expected = 0;
952
953 /* requests here are mostly expected to succeed on any
954 * device, but some are chosen to trigger protocol stalls
955 * or short reads.
956 */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200957 memset(&req, 0, sizeof req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 req.bRequest = USB_REQ_GET_DESCRIPTOR;
959 req.bRequestType = USB_DIR_IN|USB_RECIP_DEVICE;
960
961 switch (i % NUM_SUBCASES) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200962 case 0: /* get device descriptor */
963 req.wValue = cpu_to_le16(USB_DT_DEVICE << 8);
964 len = sizeof(struct usb_device_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200966 case 1: /* get first config descriptor (only) */
967 req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
968 len = sizeof(struct usb_config_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200970 case 2: /* get altsetting (OFTEN STALLS) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 req.bRequest = USB_REQ_GET_INTERFACE;
972 req.bRequestType = USB_DIR_IN|USB_RECIP_INTERFACE;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200973 /* index = 0 means first interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 len = 1;
975 expected = EPIPE;
976 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200977 case 3: /* get interface status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 req.bRequest = USB_REQ_GET_STATUS;
979 req.bRequestType = USB_DIR_IN|USB_RECIP_INTERFACE;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200980 /* interface 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 len = 2;
982 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200983 case 4: /* get device status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 req.bRequest = USB_REQ_GET_STATUS;
985 req.bRequestType = USB_DIR_IN|USB_RECIP_DEVICE;
986 len = 2;
987 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200988 case 5: /* get device qualifier (MAY STALL) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 req.wValue = cpu_to_le16 (USB_DT_DEVICE_QUALIFIER << 8);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200990 len = sizeof(struct usb_qualifier_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (udev->speed != USB_SPEED_HIGH)
992 expected = EPIPE;
993 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200994 case 6: /* get first config descriptor, plus interface */
995 req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
996 len = sizeof(struct usb_config_descriptor);
997 len += sizeof(struct usb_interface_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +0200999 case 7: /* get interface descriptor (ALWAYS STALLS) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 req.wValue = cpu_to_le16 (USB_DT_INTERFACE << 8);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001001 /* interface == 0 */
1002 len = sizeof(struct usb_interface_descriptor);
David Brownell28ffd792008-04-25 18:51:10 -07001003 expected = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001005 /* NOTE: two consecutive stalls in the queue here.
1006 * that tests fault recovery a bit more aggressively. */
1007 case 8: /* clear endpoint halt (MAY STALL) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 req.bRequest = USB_REQ_CLEAR_FEATURE;
1009 req.bRequestType = USB_RECIP_ENDPOINT;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001010 /* wValue 0 == ep halt */
1011 /* wIndex 0 == ep0 (shouldn't halt!) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 len = 0;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001013 pipe = usb_sndctrlpipe(udev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 expected = EPIPE;
1015 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001016 case 9: /* get endpoint status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 req.bRequest = USB_REQ_GET_STATUS;
1018 req.bRequestType = USB_DIR_IN|USB_RECIP_ENDPOINT;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001019 /* endpoint 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 len = 2;
1021 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001022 case 10: /* trigger short read (EREMOTEIO) */
1023 req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 len = 1024;
1025 expected = -EREMOTEIO;
1026 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001027 /* NOTE: two consecutive _different_ faults in the queue. */
1028 case 11: /* get endpoint descriptor (ALWAYS STALLS) */
1029 req.wValue = cpu_to_le16(USB_DT_ENDPOINT << 8);
1030 /* endpoint == 0 */
1031 len = sizeof(struct usb_interface_descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 expected = EPIPE;
1033 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001034 /* NOTE: sometimes even a third fault in the queue! */
1035 case 12: /* get string 0 descriptor (MAY STALL) */
1036 req.wValue = cpu_to_le16(USB_DT_STRING << 8);
1037 /* string == 0, for language IDs */
1038 len = sizeof(struct usb_interface_descriptor);
1039 /* may succeed when > 4 languages */
1040 expected = EREMOTEIO; /* or EPIPE, if no strings */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001042 case 13: /* short read, resembling case 10 */
1043 req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
1044 /* last data packet "should" be DATA1, not DATA0 */
Paul Zimmerman0053c7e2012-04-16 14:19:07 -07001045 if (udev->speed == USB_SPEED_SUPER)
1046 len = 1024 - 512;
1047 else
1048 len = 1024 - udev->descriptor.bMaxPacketSize0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 expected = -EREMOTEIO;
1050 break;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001051 case 14: /* short read; try to fill the last packet */
1052 req.wValue = cpu_to_le16((USB_DT_DEVICE << 8) | 0);
David Brownell28ffd792008-04-25 18:51:10 -07001053 /* device descriptor size == 18 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 len = udev->descriptor.bMaxPacketSize0;
Sebastian Andrzej Siewior67e7d642011-04-14 16:17:21 +02001055 if (udev->speed == USB_SPEED_SUPER)
1056 len = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 switch (len) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001058 case 8:
1059 len = 24;
1060 break;
1061 case 16:
1062 len = 32;
1063 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065 expected = -EREMOTEIO;
1066 break;
1067 default:
David Brownell28ffd792008-04-25 18:51:10 -07001068 ERROR(dev, "bogus number of ctrl queue testcases!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 context.status = -EINVAL;
1070 goto cleanup;
1071 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001072 req.wLength = cpu_to_le16(len);
1073 urb[i] = u = simple_alloc_urb(udev, pipe, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (!u)
1075 goto cleanup;
1076
Alan Stern0ede76f2010-03-05 15:10:17 -05001077 reqp = kmalloc(sizeof *reqp, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (!reqp)
1079 goto cleanup;
1080 reqp->setup = req;
1081 reqp->number = i % NUM_SUBCASES;
1082 reqp->expected = expected;
1083 u->setup_packet = (char *) &reqp->setup;
1084
1085 u->context = &context;
1086 u->complete = ctrl_complete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088
1089 /* queue the urbs */
1090 context.urb = urb;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001091 spin_lock_irq(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 for (i = 0; i < param->sglen; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001093 context.status = usb_submit_urb(urb[i], GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (context.status != 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001095 ERROR(dev, "can't submit urb[%d], status %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 i, context.status);
1097 context.count = context.pending;
1098 break;
1099 }
1100 context.pending++;
1101 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001102 spin_unlock_irq(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 /* FIXME set timer and time out; provide a disconnect hook */
1105
1106 /* wait for the last one to complete */
1107 if (context.pending > 0)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001108 wait_for_completion(&context.complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110cleanup:
1111 for (i = 0; i < param->sglen; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001112 if (!urb[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 continue;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001114 urb[i]->dev = udev;
Alan Stern0ede76f2010-03-05 15:10:17 -05001115 kfree(urb[i]->setup_packet);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001116 simple_free_urb(urb[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001118 kfree(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return context.status;
1120}
1121#undef NUM_SUBCASES
1122
1123
1124/*-------------------------------------------------------------------------*/
1125
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001126static void unlink1_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
1128 int status = urb->status;
1129
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001130 /* we "know" -EPIPE (stall) never happens */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (!status)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001132 status = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (status) {
1134 urb->status = status;
Ming Leicdc97792008-02-24 18:41:47 +08001135 complete(urb->context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137}
1138
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001139static int unlink1(struct usbtest_dev *dev, int pipe, int size, int async)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 struct urb *urb;
1142 struct completion completion;
1143 int retval = 0;
1144
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001145 init_completion(&completion);
1146 urb = simple_alloc_urb(testdev_to_usbdev(dev), pipe, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (!urb)
1148 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 urb->context = &completion;
1150 urb->complete = unlink1_callback;
1151
Huang Rui43e5e632014-05-26 10:55:36 +08001152 if (usb_pipeout(urb->pipe)) {
1153 simple_fill_buf(urb);
1154 urb->transfer_flags |= URB_ZERO_PACKET;
1155 }
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 /* keep the endpoint busy. there are lots of hc/hcd-internal
1158 * states, and testing should get to all of them over time.
1159 *
1160 * FIXME want additional tests for when endpoint is STALLing
1161 * due to errors, or is just NAKing requests.
1162 */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001163 retval = usb_submit_urb(urb, GFP_KERNEL);
1164 if (retval != 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001165 dev_err(&dev->intf->dev, "submit fail %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return retval;
1167 }
1168
1169 /* unlinking that should always work. variable delay tests more
1170 * hcd states and code paths, even with little other system load.
1171 */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001172 msleep(jiffies % (2 * INTERRUPT_RATE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (async) {
Martin Fuzzey3b6c0232009-06-04 23:20:38 +02001174 while (!completion_done(&completion)) {
1175 retval = usb_unlink_urb(urb);
1176
1177 switch (retval) {
1178 case -EBUSY:
1179 case -EIDRM:
1180 /* we can't unlink urbs while they're completing
1181 * or if they've completed, and we haven't
1182 * resubmitted. "normal" drivers would prevent
1183 * resubmission, but since we're testing unlink
1184 * paths, we can't.
1185 */
1186 ERROR(dev, "unlink retry\n");
1187 continue;
1188 case 0:
1189 case -EINPROGRESS:
1190 break;
1191
1192 default:
1193 dev_err(&dev->intf->dev,
1194 "unlink fail %d\n", retval);
1195 return retval;
1196 }
1197
1198 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
1200 } else
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001201 usb_kill_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001203 wait_for_completion(&completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 retval = urb->status;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001205 simple_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 if (async)
1208 return (retval == -ECONNRESET) ? 0 : retval - 1000;
1209 else
1210 return (retval == -ENOENT || retval == -EPERM) ?
1211 0 : retval - 2000;
1212}
1213
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001214static int unlink_simple(struct usbtest_dev *dev, int pipe, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
1216 int retval = 0;
1217
1218 /* test sync and async paths */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001219 retval = unlink1(dev, pipe, len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (!retval)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001221 retval = unlink1(dev, pipe, len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return retval;
1223}
1224
1225/*-------------------------------------------------------------------------*/
1226
Alan Stern869410f2011-04-14 11:21:04 -04001227struct queued_ctx {
1228 struct completion complete;
1229 atomic_t pending;
1230 unsigned num;
1231 int status;
1232 struct urb **urbs;
1233};
1234
1235static void unlink_queued_callback(struct urb *urb)
1236{
1237 int status = urb->status;
1238 struct queued_ctx *ctx = urb->context;
1239
1240 if (ctx->status)
1241 goto done;
1242 if (urb == ctx->urbs[ctx->num - 4] || urb == ctx->urbs[ctx->num - 2]) {
1243 if (status == -ECONNRESET)
1244 goto done;
1245 /* What error should we report if the URB completed normally? */
1246 }
1247 if (status != 0)
1248 ctx->status = status;
1249
1250 done:
1251 if (atomic_dec_and_test(&ctx->pending))
1252 complete(&ctx->complete);
1253}
1254
1255static int unlink_queued(struct usbtest_dev *dev, int pipe, unsigned num,
1256 unsigned size)
1257{
1258 struct queued_ctx ctx;
1259 struct usb_device *udev = testdev_to_usbdev(dev);
1260 void *buf;
1261 dma_addr_t buf_dma;
1262 int i;
1263 int retval = -ENOMEM;
1264
1265 init_completion(&ctx.complete);
1266 atomic_set(&ctx.pending, 1); /* One more than the actual value */
1267 ctx.num = num;
1268 ctx.status = 0;
1269
1270 buf = usb_alloc_coherent(udev, size, GFP_KERNEL, &buf_dma);
1271 if (!buf)
1272 return retval;
1273 memset(buf, 0, size);
1274
1275 /* Allocate and init the urbs we'll queue */
1276 ctx.urbs = kcalloc(num, sizeof(struct urb *), GFP_KERNEL);
1277 if (!ctx.urbs)
1278 goto free_buf;
1279 for (i = 0; i < num; i++) {
1280 ctx.urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1281 if (!ctx.urbs[i])
1282 goto free_urbs;
1283 usb_fill_bulk_urb(ctx.urbs[i], udev, pipe, buf, size,
1284 unlink_queued_callback, &ctx);
1285 ctx.urbs[i]->transfer_dma = buf_dma;
1286 ctx.urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Huang Rui43e5e632014-05-26 10:55:36 +08001287
1288 if (usb_pipeout(ctx.urbs[i]->pipe)) {
1289 simple_fill_buf(ctx.urbs[i]);
1290 ctx.urbs[i]->transfer_flags |= URB_ZERO_PACKET;
1291 }
Alan Stern869410f2011-04-14 11:21:04 -04001292 }
1293
1294 /* Submit all the URBs and then unlink URBs num - 4 and num - 2. */
1295 for (i = 0; i < num; i++) {
1296 atomic_inc(&ctx.pending);
1297 retval = usb_submit_urb(ctx.urbs[i], GFP_KERNEL);
1298 if (retval != 0) {
1299 dev_err(&dev->intf->dev, "submit urbs[%d] fail %d\n",
1300 i, retval);
1301 atomic_dec(&ctx.pending);
1302 ctx.status = retval;
1303 break;
1304 }
1305 }
1306 if (i == num) {
1307 usb_unlink_urb(ctx.urbs[num - 4]);
1308 usb_unlink_urb(ctx.urbs[num - 2]);
1309 } else {
1310 while (--i >= 0)
1311 usb_unlink_urb(ctx.urbs[i]);
1312 }
1313
1314 if (atomic_dec_and_test(&ctx.pending)) /* The extra count */
1315 complete(&ctx.complete);
1316 wait_for_completion(&ctx.complete);
1317 retval = ctx.status;
1318
1319 free_urbs:
1320 for (i = 0; i < num; i++)
1321 usb_free_urb(ctx.urbs[i]);
1322 kfree(ctx.urbs);
1323 free_buf:
1324 usb_free_coherent(udev, size, buf, buf_dma);
1325 return retval;
1326}
1327
1328/*-------------------------------------------------------------------------*/
1329
David Brownell28ffd792008-04-25 18:51:10 -07001330static int verify_not_halted(struct usbtest_dev *tdev, int ep, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
1332 int retval;
1333 u16 status;
1334
1335 /* shouldn't look or act halted */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001336 retval = usb_get_status(urb->dev, USB_RECIP_ENDPOINT, ep, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (retval < 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001338 ERROR(tdev, "ep %02x couldn't get no-halt status, %d\n",
1339 ep, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return retval;
1341 }
1342 if (status != 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001343 ERROR(tdev, "ep %02x bogus status: %04x != 0\n", ep, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return -EINVAL;
1345 }
David Brownell28ffd792008-04-25 18:51:10 -07001346 retval = simple_io(tdev, urb, 1, 0, 0, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (retval != 0)
1348 return -EINVAL;
1349 return 0;
1350}
1351
David Brownell28ffd792008-04-25 18:51:10 -07001352static int verify_halted(struct usbtest_dev *tdev, int ep, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
1354 int retval;
1355 u16 status;
1356
1357 /* should look and act halted */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001358 retval = usb_get_status(urb->dev, USB_RECIP_ENDPOINT, ep, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (retval < 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001360 ERROR(tdev, "ep %02x couldn't get halt status, %d\n",
1361 ep, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return retval;
1363 }
Jan Andersson6ce45602008-01-08 16:39:49 +01001364 le16_to_cpus(&status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (status != 1) {
David Brownell28ffd792008-04-25 18:51:10 -07001366 ERROR(tdev, "ep %02x bogus status: %04x != 1\n", ep, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return -EINVAL;
1368 }
David Brownell28ffd792008-04-25 18:51:10 -07001369 retval = simple_io(tdev, urb, 1, 0, -EPIPE, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (retval != -EPIPE)
1371 return -EINVAL;
David Brownell28ffd792008-04-25 18:51:10 -07001372 retval = simple_io(tdev, urb, 1, 0, -EPIPE, "verify_still_halted");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (retval != -EPIPE)
1374 return -EINVAL;
1375 return 0;
1376}
1377
David Brownell28ffd792008-04-25 18:51:10 -07001378static int test_halt(struct usbtest_dev *tdev, int ep, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
1380 int retval;
1381
1382 /* shouldn't look or act halted now */
David Brownell28ffd792008-04-25 18:51:10 -07001383 retval = verify_not_halted(tdev, ep, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (retval < 0)
1385 return retval;
1386
1387 /* set halt (protocol test only), verify it worked */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001388 retval = usb_control_msg(urb->dev, usb_sndctrlpipe(urb->dev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 USB_REQ_SET_FEATURE, USB_RECIP_ENDPOINT,
1390 USB_ENDPOINT_HALT, ep,
1391 NULL, 0, USB_CTRL_SET_TIMEOUT);
1392 if (retval < 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001393 ERROR(tdev, "ep %02x couldn't set halt, %d\n", ep, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return retval;
1395 }
David Brownell28ffd792008-04-25 18:51:10 -07001396 retval = verify_halted(tdev, ep, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 if (retval < 0)
1398 return retval;
1399
1400 /* clear halt (tests API + protocol), verify it worked */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001401 retval = usb_clear_halt(urb->dev, urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (retval < 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001403 ERROR(tdev, "ep %02x couldn't clear halt, %d\n", ep, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return retval;
1405 }
David Brownell28ffd792008-04-25 18:51:10 -07001406 retval = verify_not_halted(tdev, ep, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (retval < 0)
1408 return retval;
1409
1410 /* NOTE: could also verify SET_INTERFACE clear halts ... */
1411
1412 return 0;
1413}
1414
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001415static int halt_simple(struct usbtest_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Paul Zimmerman0053c7e2012-04-16 14:19:07 -07001417 int ep;
1418 int retval = 0;
1419 struct urb *urb;
1420 struct usb_device *udev = testdev_to_usbdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Paul Zimmerman0053c7e2012-04-16 14:19:07 -07001422 if (udev->speed == USB_SPEED_SUPER)
1423 urb = simple_alloc_urb(udev, 0, 1024);
1424 else
1425 urb = simple_alloc_urb(udev, 0, 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (urb == NULL)
1427 return -ENOMEM;
1428
1429 if (dev->in_pipe) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001430 ep = usb_pipeendpoint(dev->in_pipe) | USB_DIR_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 urb->pipe = dev->in_pipe;
David Brownell28ffd792008-04-25 18:51:10 -07001432 retval = test_halt(dev, ep, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (retval < 0)
1434 goto done;
1435 }
1436
1437 if (dev->out_pipe) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001438 ep = usb_pipeendpoint(dev->out_pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 urb->pipe = dev->out_pipe;
David Brownell28ffd792008-04-25 18:51:10 -07001440 retval = test_halt(dev, ep, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
1442done:
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001443 simple_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return retval;
1445}
1446
1447/*-------------------------------------------------------------------------*/
1448
1449/* Control OUT tests use the vendor control requests from Intel's
1450 * USB 2.0 compliance test device: write a buffer, read it back.
1451 *
1452 * Intel's spec only _requires_ that it work for one packet, which
1453 * is pretty weak. Some HCDs place limits here; most devices will
1454 * need to be able to handle more than one OUT data packet. We'll
1455 * try whatever we're told to try.
1456 */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001457static int ctrl_out(struct usbtest_dev *dev,
Martin Fuzzey084fb202011-01-16 19:17:11 +01001458 unsigned count, unsigned length, unsigned vary, unsigned offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Orjan Fribergf54fa842006-08-08 23:31:40 -07001460 unsigned i, j, len;
1461 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 u8 *buf;
1463 char *what = "?";
1464 struct usb_device *udev;
Orjan Fribergf54fa842006-08-08 23:31:40 -07001465
David Brownellff7c79e2005-04-22 13:17:00 -07001466 if (length < 1 || length > 0xffff || vary >= length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return -EINVAL;
1468
Martin Fuzzey084fb202011-01-16 19:17:11 +01001469 buf = kmalloc(length + offset, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (!buf)
1471 return -ENOMEM;
1472
Martin Fuzzey084fb202011-01-16 19:17:11 +01001473 buf += offset;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001474 udev = testdev_to_usbdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 len = length;
1476 retval = 0;
1477
1478 /* NOTE: hardware might well act differently if we pushed it
1479 * with lots back-to-back queued requests.
1480 */
1481 for (i = 0; i < count; i++) {
1482 /* write patterned data */
1483 for (j = 0; j < len; j++)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001484 buf[j] = i + j;
1485 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 0x5b, USB_DIR_OUT|USB_TYPE_VENDOR,
1487 0, 0, buf, len, USB_CTRL_SET_TIMEOUT);
1488 if (retval != len) {
1489 what = "write";
David Brownellff7c79e2005-04-22 13:17:00 -07001490 if (retval >= 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001491 ERROR(dev, "ctrl_out, wlen %d (expected %d)\n",
David Brownellff7c79e2005-04-22 13:17:00 -07001492 retval, len);
1493 retval = -EBADMSG;
1494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 break;
1496 }
1497
1498 /* read it back -- assuming nothing intervened!! */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001499 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 0x5c, USB_DIR_IN|USB_TYPE_VENDOR,
1501 0, 0, buf, len, USB_CTRL_GET_TIMEOUT);
1502 if (retval != len) {
1503 what = "read";
David Brownellff7c79e2005-04-22 13:17:00 -07001504 if (retval >= 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001505 ERROR(dev, "ctrl_out, rlen %d (expected %d)\n",
David Brownellff7c79e2005-04-22 13:17:00 -07001506 retval, len);
1507 retval = -EBADMSG;
1508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 break;
1510 }
1511
1512 /* fail if we can't verify */
1513 for (j = 0; j < len; j++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001514 if (buf[j] != (u8) (i + j)) {
David Brownell28ffd792008-04-25 18:51:10 -07001515 ERROR(dev, "ctrl_out, byte %d is %d not %d\n",
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001516 j, buf[j], (u8) i + j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 retval = -EBADMSG;
1518 break;
1519 }
1520 }
1521 if (retval < 0) {
1522 what = "verify";
1523 break;
1524 }
1525
1526 len += vary;
David Brownellff7c79e2005-04-22 13:17:00 -07001527
1528 /* [real world] the "zero bytes IN" case isn't really used.
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001529 * hardware can easily trip up in this weird case, since its
David Brownellff7c79e2005-04-22 13:17:00 -07001530 * status stage is IN, not OUT like other ep0in transfers.
1531 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (len > length)
David Brownellff7c79e2005-04-22 13:17:00 -07001533 len = realworld ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
1535
1536 if (retval < 0)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001537 ERROR(dev, "ctrl_out %s failed, code %d, count %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 what, retval, i);
1539
Martin Fuzzey084fb202011-01-16 19:17:11 +01001540 kfree(buf - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return retval;
1542}
1543
1544/*-------------------------------------------------------------------------*/
1545
1546/* ISO tests ... mimics common usage
1547 * - buffer length is split into N packets (mostly maxpacket sized)
1548 * - multi-buffers according to sglen
1549 */
1550
1551struct iso_context {
1552 unsigned count;
1553 unsigned pending;
1554 spinlock_t lock;
1555 struct completion done;
Alan Stern9da21502006-05-22 16:47:13 -04001556 int submit_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 unsigned long errors;
Alan Stern9da21502006-05-22 16:47:13 -04001558 unsigned long packet_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 struct usbtest_dev *dev;
1560};
1561
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001562static void iso_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
1564 struct iso_context *ctx = urb->context;
1565
1566 spin_lock(&ctx->lock);
1567 ctx->count--;
1568
Alan Stern9da21502006-05-22 16:47:13 -04001569 ctx->packet_count += urb->number_of_packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (urb->error_count > 0)
1571 ctx->errors += urb->error_count;
Alan Stern9da21502006-05-22 16:47:13 -04001572 else if (urb->status != 0)
1573 ctx->errors += urb->number_of_packets;
Martin Fuzzey40aed522010-10-01 00:20:48 +02001574 else if (urb->actual_length != urb->transfer_buffer_length)
1575 ctx->errors++;
Martin Fuzzey084fb202011-01-16 19:17:11 +01001576 else if (check_guard_bytes(ctx->dev, urb) != 0)
1577 ctx->errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Alan Stern9da21502006-05-22 16:47:13 -04001579 if (urb->status == 0 && ctx->count > (ctx->pending - 1)
1580 && !ctx->submit_error) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001581 int status = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 switch (status) {
1583 case 0:
1584 goto done;
1585 default:
David Brownell28ffd792008-04-25 18:51:10 -07001586 dev_err(&ctx->dev->intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 "iso resubmit err %d\n",
1588 status);
1589 /* FALLTHROUGH */
1590 case -ENODEV: /* disconnected */
Alan Stern9da21502006-05-22 16:47:13 -04001591 case -ESHUTDOWN: /* endpoint disabled */
1592 ctx->submit_error = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 break;
1594 }
1595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 ctx->pending--;
1598 if (ctx->pending == 0) {
1599 if (ctx->errors)
David Brownell28ffd792008-04-25 18:51:10 -07001600 dev_err(&ctx->dev->intf->dev,
Alan Stern9da21502006-05-22 16:47:13 -04001601 "iso test, %lu errors out of %lu\n",
1602 ctx->errors, ctx->packet_count);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001603 complete(&ctx->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
1605done:
1606 spin_unlock(&ctx->lock);
1607}
1608
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001609static struct urb *iso_alloc_urb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 struct usb_device *udev,
1611 int pipe,
1612 struct usb_endpoint_descriptor *desc,
Martin Fuzzey084fb202011-01-16 19:17:11 +01001613 long bytes,
1614 unsigned offset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615)
1616{
1617 struct urb *urb;
1618 unsigned i, maxp, packets;
1619
1620 if (bytes < 0 || !desc)
1621 return NULL;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001622 maxp = 0x7ff & usb_endpoint_maxp(desc);
1623 maxp *= 1 + (0x3 & (usb_endpoint_maxp(desc) >> 11));
Julia Lawalldfa5ec72008-03-04 15:25:11 -08001624 packets = DIV_ROUND_UP(bytes, maxp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001626 urb = usb_alloc_urb(packets, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (!urb)
1628 return urb;
1629 urb->dev = udev;
1630 urb->pipe = pipe;
1631
1632 urb->number_of_packets = packets;
1633 urb->transfer_buffer_length = bytes;
Martin Fuzzey084fb202011-01-16 19:17:11 +01001634 urb->transfer_buffer = usb_alloc_coherent(udev, bytes + offset,
1635 GFP_KERNEL,
1636 &urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (!urb->transfer_buffer) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001638 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 return NULL;
1640 }
Martin Fuzzey084fb202011-01-16 19:17:11 +01001641 if (offset) {
1642 memset(urb->transfer_buffer, GUARD_BYTE, offset);
1643 urb->transfer_buffer += offset;
1644 urb->transfer_dma += offset;
1645 }
1646 /* For inbound transfers use guard byte so that test fails if
1647 data not correctly copied */
1648 memset(urb->transfer_buffer,
1649 usb_pipein(urb->pipe) ? GUARD_BYTE : 0,
1650 bytes);
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 for (i = 0; i < packets; i++) {
1653 /* here, only the last packet will be short */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001654 urb->iso_frame_desc[i].length = min((unsigned) bytes, maxp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 bytes -= urb->iso_frame_desc[i].length;
1656
1657 urb->iso_frame_desc[i].offset = maxp * i;
1658 }
1659
1660 urb->complete = iso_callback;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001661 /* urb->context = SET BY CALLER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 urb->interval = 1 << (desc->bInterval - 1);
1663 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
1664 return urb;
1665}
1666
1667static int
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001668test_iso_queue(struct usbtest_dev *dev, struct usbtest_param *param,
Martin Fuzzey084fb202011-01-16 19:17:11 +01001669 int pipe, struct usb_endpoint_descriptor *desc, unsigned offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
1671 struct iso_context context;
1672 struct usb_device *udev;
1673 unsigned i;
1674 unsigned long packets = 0;
Alan Stern9da21502006-05-22 16:47:13 -04001675 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 struct urb *urbs[10]; /* FIXME no limit */
1677
1678 if (param->sglen > 10)
1679 return -EDOM;
1680
Alan Stern9da21502006-05-22 16:47:13 -04001681 memset(&context, 0, sizeof context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 context.count = param->iterations * param->sglen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 context.dev = dev;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001684 init_completion(&context.done);
1685 spin_lock_init(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001687 memset(urbs, 0, sizeof urbs);
1688 udev = testdev_to_usbdev(dev);
David Brownell28ffd792008-04-25 18:51:10 -07001689 dev_info(&dev->intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 "... iso period %d %sframes, wMaxPacket %04x\n",
1691 1 << (desc->bInterval - 1),
1692 (udev->speed == USB_SPEED_HIGH) ? "micro" : "",
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001693 usb_endpoint_maxp(desc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 for (i = 0; i < param->sglen; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001696 urbs[i] = iso_alloc_urb(udev, pipe, desc,
Martin Fuzzey084fb202011-01-16 19:17:11 +01001697 param->length, offset);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001698 if (!urbs[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 status = -ENOMEM;
1700 goto fail;
1701 }
1702 packets += urbs[i]->number_of_packets;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001703 urbs[i]->context = &context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
1705 packets *= param->iterations;
David Brownell28ffd792008-04-25 18:51:10 -07001706 dev_info(&dev->intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 "... total %lu msec (%lu packets)\n",
1708 (packets * (1 << (desc->bInterval - 1)))
1709 / ((udev->speed == USB_SPEED_HIGH) ? 8 : 1),
1710 packets);
1711
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001712 spin_lock_irq(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 for (i = 0; i < param->sglen; i++) {
Alan Stern9da21502006-05-22 16:47:13 -04001714 ++context.pending;
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001715 status = usb_submit_urb(urbs[i], GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 if (status < 0) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001717 ERROR(dev, "submit iso[%d], error %d\n", i, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if (i == 0) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001719 spin_unlock_irq(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 goto fail;
1721 }
1722
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001723 simple_free_urb(urbs[i]);
Ming Leie10e1be2010-08-02 22:09:01 +08001724 urbs[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 context.pending--;
Alan Stern9da21502006-05-22 16:47:13 -04001726 context.submit_error = 1;
1727 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001730 spin_unlock_irq(&context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001732 wait_for_completion(&context.done);
Alan Stern9da21502006-05-22 16:47:13 -04001733
Ming Leie10e1be2010-08-02 22:09:01 +08001734 for (i = 0; i < param->sglen; i++) {
1735 if (urbs[i])
1736 simple_free_urb(urbs[i]);
1737 }
Alan Stern9da21502006-05-22 16:47:13 -04001738 /*
1739 * Isochronous transfers are expected to fail sometimes. As an
1740 * arbitrary limit, we will report an error if any submissions
1741 * fail or if the transfer failure rate is > 10%.
1742 */
1743 if (status != 0)
1744 ;
1745 else if (context.submit_error)
1746 status = -EACCES;
1747 else if (context.errors > context.packet_count / 10)
1748 status = -EIO;
1749 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751fail:
1752 for (i = 0; i < param->sglen; i++) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001753 if (urbs[i])
1754 simple_free_urb(urbs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756 return status;
1757}
1758
Martin Fuzzey084fb202011-01-16 19:17:11 +01001759static int test_unaligned_bulk(
1760 struct usbtest_dev *tdev,
1761 int pipe,
1762 unsigned length,
1763 int iterations,
1764 unsigned transfer_flags,
1765 const char *label)
1766{
1767 int retval;
1768 struct urb *urb = usbtest_alloc_urb(
1769 testdev_to_usbdev(tdev), pipe, length, transfer_flags, 1);
1770
1771 if (!urb)
1772 return -ENOMEM;
1773
1774 retval = simple_io(tdev, urb, iterations, 0, 0, label);
1775 simple_free_urb(urb);
1776 return retval;
1777}
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779/*-------------------------------------------------------------------------*/
1780
1781/* We only have this one interface to user space, through usbfs.
1782 * User mode code can scan usbfs to find N different devices (maybe on
1783 * different busses) to use when testing, and allocate one thread per
1784 * test. So discovery is simplified, and we have no device naming issues.
1785 *
1786 * Don't use these only as stress/load tests. Use them along with with
1787 * other USB bus activity: plugging, unplugging, mousing, mp3 playback,
1788 * video capture, and so on. Run different tests at different times, in
1789 * different sequences. Nothing here should interact with other devices,
1790 * except indirectly by consuming USB bandwidth and CPU resources for test
1791 * threads and request completion. But the only way to know that for sure
1792 * is to test when HC queues are in use by many devices.
David Brownell28ffd792008-04-25 18:51:10 -07001793 *
1794 * WARNING: Because usbfs grabs udev->dev.sem before calling this ioctl(),
1795 * it locks out usbcore in certain code paths. Notably, if you disconnect
1796 * the device-under-test, khubd will wait block forever waiting for the
1797 * ioctl to complete ... so that usb_disconnect() can abort the pending
1798 * urbs and then call usbtest_disconnect(). To abort a test, you're best
1799 * off just killing the userspace task and waiting for it to exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 */
1801
1802static int
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001803usbtest_ioctl(struct usb_interface *intf, unsigned int code, void *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001805 struct usbtest_dev *dev = usb_get_intfdata(intf);
1806 struct usb_device *udev = testdev_to_usbdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 struct usbtest_param *param = buf;
1808 int retval = -EOPNOTSUPP;
1809 struct urb *urb;
1810 struct scatterlist *sg;
1811 struct usb_sg_request req;
1812 struct timeval start;
1813 unsigned i;
1814
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001815 /* FIXME USBDEVFS_CONNECTINFO doesn't say how fast the device is. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Vikram Pandita7f4e9852009-11-09 21:24:32 -06001817 pattern = mod_pattern;
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (code != USBTEST_REQUEST)
1820 return -EOPNOTSUPP;
1821
roel kluin8aafdf62008-10-21 00:36:44 -04001822 if (param->iterations <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return -EINVAL;
1824
Matthias Kaehlcke1cfab022007-12-13 16:15:33 -08001825 if (mutex_lock_interruptible(&dev->lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return -ERESTARTSYS;
1827
Alan Stern70a1c9e2008-03-06 17:00:58 -05001828 /* FIXME: What if a system sleep starts while a test is running? */
David Brownellff7c79e2005-04-22 13:17:00 -07001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 /* some devices, like ez-usb default devices, need a non-default
1831 * altsetting to have any active endpoints. some tests change
1832 * altsettings; force a default so most tests don't need to check.
1833 */
1834 if (dev->info->alt >= 0) {
David Brownell28ffd792008-04-25 18:51:10 -07001835 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 if (intf->altsetting->desc.bInterfaceNumber) {
Matthias Kaehlcke1cfab022007-12-13 16:15:33 -08001838 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return -ENODEV;
1840 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001841 res = set_altsetting(dev, dev->info->alt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (res) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001843 dev_err(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 "set altsetting to %d failed, %d\n",
1845 dev->info->alt, res);
Matthias Kaehlcke1cfab022007-12-13 16:15:33 -08001846 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return res;
1848 }
1849 }
1850
1851 /*
1852 * Just a bunch of test cases that every HCD is expected to handle.
1853 *
1854 * Some may need specific firmware, though it'd be good to have
1855 * one firmware image to handle all the test cases.
1856 *
1857 * FIXME add more tests! cancel requests, verify the data, control
1858 * queueing, concurrent read+write threads, and so on.
1859 */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001860 do_gettimeofday(&start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 switch (param->test_num) {
1862
1863 case 0:
David Brownell28ffd792008-04-25 18:51:10 -07001864 dev_info(&intf->dev, "TEST 0: NOP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 retval = 0;
1866 break;
1867
1868 /* Simple non-queued bulk I/O tests */
1869 case 1:
1870 if (dev->out_pipe == 0)
1871 break;
David Brownell28ffd792008-04-25 18:51:10 -07001872 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 "TEST 1: write %d bytes %u times\n",
1874 param->length, param->iterations);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001875 urb = simple_alloc_urb(udev, dev->out_pipe, param->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (!urb) {
1877 retval = -ENOMEM;
1878 break;
1879 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001880 /* FIRMWARE: bulk sink (maybe accepts short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07001881 retval = simple_io(dev, urb, param->iterations, 0, 0, "test1");
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001882 simple_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 break;
1884 case 2:
1885 if (dev->in_pipe == 0)
1886 break;
David Brownell28ffd792008-04-25 18:51:10 -07001887 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 "TEST 2: read %d bytes %u times\n",
1889 param->length, param->iterations);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001890 urb = simple_alloc_urb(udev, dev->in_pipe, param->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (!urb) {
1892 retval = -ENOMEM;
1893 break;
1894 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001895 /* FIRMWARE: bulk source (maybe generates short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07001896 retval = simple_io(dev, urb, param->iterations, 0, 0, "test2");
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001897 simple_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 break;
1899 case 3:
1900 if (dev->out_pipe == 0 || param->vary == 0)
1901 break;
David Brownell28ffd792008-04-25 18:51:10 -07001902 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 "TEST 3: write/%d 0..%d bytes %u times\n",
1904 param->vary, param->length, param->iterations);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001905 urb = simple_alloc_urb(udev, dev->out_pipe, param->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (!urb) {
1907 retval = -ENOMEM;
1908 break;
1909 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001910 /* FIRMWARE: bulk sink (maybe accepts short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07001911 retval = simple_io(dev, urb, param->iterations, param->vary,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 0, "test3");
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001913 simple_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 break;
1915 case 4:
1916 if (dev->in_pipe == 0 || param->vary == 0)
1917 break;
David Brownell28ffd792008-04-25 18:51:10 -07001918 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 "TEST 4: read/%d 0..%d bytes %u times\n",
1920 param->vary, param->length, param->iterations);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001921 urb = simple_alloc_urb(udev, dev->in_pipe, param->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (!urb) {
1923 retval = -ENOMEM;
1924 break;
1925 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001926 /* FIRMWARE: bulk source (maybe generates short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07001927 retval = simple_io(dev, urb, param->iterations, param->vary,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 0, "test4");
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001929 simple_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 break;
1931
1932 /* Queued bulk I/O tests */
1933 case 5:
1934 if (dev->out_pipe == 0 || param->sglen == 0)
1935 break;
David Brownell28ffd792008-04-25 18:51:10 -07001936 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 "TEST 5: write %d sglists %d entries of %d bytes\n",
1938 param->iterations,
1939 param->sglen, param->length);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001940 sg = alloc_sglist(param->sglen, param->length, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (!sg) {
1942 retval = -ENOMEM;
1943 break;
1944 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001945 /* FIRMWARE: bulk sink (maybe accepts short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07001946 retval = perform_sglist(dev, param->iterations, dev->out_pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 &req, sg, param->sglen);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001948 free_sglist(sg, param->sglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 break;
1950
1951 case 6:
1952 if (dev->in_pipe == 0 || param->sglen == 0)
1953 break;
David Brownell28ffd792008-04-25 18:51:10 -07001954 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 "TEST 6: read %d sglists %d entries of %d bytes\n",
1956 param->iterations,
1957 param->sglen, param->length);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001958 sg = alloc_sglist(param->sglen, param->length, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (!sg) {
1960 retval = -ENOMEM;
1961 break;
1962 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001963 /* FIRMWARE: bulk source (maybe generates short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07001964 retval = perform_sglist(dev, param->iterations, dev->in_pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 &req, sg, param->sglen);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001966 free_sglist(sg, param->sglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 break;
1968 case 7:
1969 if (dev->out_pipe == 0 || param->sglen == 0 || param->vary == 0)
1970 break;
David Brownell28ffd792008-04-25 18:51:10 -07001971 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 "TEST 7: write/%d %d sglists %d entries 0..%d bytes\n",
1973 param->vary, param->iterations,
1974 param->sglen, param->length);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001975 sg = alloc_sglist(param->sglen, param->length, param->vary);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if (!sg) {
1977 retval = -ENOMEM;
1978 break;
1979 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001980 /* FIRMWARE: bulk sink (maybe accepts short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07001981 retval = perform_sglist(dev, param->iterations, dev->out_pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 &req, sg, param->sglen);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001983 free_sglist(sg, param->sglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 break;
1985 case 8:
1986 if (dev->in_pipe == 0 || param->sglen == 0 || param->vary == 0)
1987 break;
David Brownell28ffd792008-04-25 18:51:10 -07001988 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 "TEST 8: read/%d %d sglists %d entries 0..%d bytes\n",
1990 param->vary, param->iterations,
1991 param->sglen, param->length);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001992 sg = alloc_sglist(param->sglen, param->length, param->vary);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 if (!sg) {
1994 retval = -ENOMEM;
1995 break;
1996 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02001997 /* FIRMWARE: bulk source (maybe generates short writes) */
David Brownell28ffd792008-04-25 18:51:10 -07001998 retval = perform_sglist(dev, param->iterations, dev->in_pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 &req, sg, param->sglen);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002000 free_sglist(sg, param->sglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 break;
2002
2003 /* non-queued sanity tests for control (chapter 9 subset) */
2004 case 9:
2005 retval = 0;
David Brownell28ffd792008-04-25 18:51:10 -07002006 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 "TEST 9: ch9 (subset) control tests, %d times\n",
2008 param->iterations);
2009 for (i = param->iterations; retval == 0 && i--; /* NOP */)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002010 retval = ch9_postconfig(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 if (retval)
David Brownell28ffd792008-04-25 18:51:10 -07002012 dev_err(&intf->dev, "ch9 subset failed, "
2013 "iterations left %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 break;
2015
2016 /* queued control messaging */
2017 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 retval = 0;
David Brownell28ffd792008-04-25 18:51:10 -07002019 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 "TEST 10: queue %d control calls, %d times\n",
2021 param->sglen,
2022 param->iterations);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002023 retval = test_ctrl_queue(dev, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 break;
2025
2026 /* simple non-queued unlinks (ring with one urb) */
2027 case 11:
2028 if (dev->in_pipe == 0 || !param->length)
2029 break;
2030 retval = 0;
David Brownell28ffd792008-04-25 18:51:10 -07002031 dev_info(&intf->dev, "TEST 11: unlink %d reads of %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 param->iterations, param->length);
2033 for (i = param->iterations; retval == 0 && i--; /* NOP */)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002034 retval = unlink_simple(dev, dev->in_pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 param->length);
2036 if (retval)
David Brownell28ffd792008-04-25 18:51:10 -07002037 dev_err(&intf->dev, "unlink reads failed %d, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 "iterations left %d\n", retval, i);
2039 break;
2040 case 12:
2041 if (dev->out_pipe == 0 || !param->length)
2042 break;
2043 retval = 0;
David Brownell28ffd792008-04-25 18:51:10 -07002044 dev_info(&intf->dev, "TEST 12: unlink %d writes of %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 param->iterations, param->length);
2046 for (i = param->iterations; retval == 0 && i--; /* NOP */)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002047 retval = unlink_simple(dev, dev->out_pipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 param->length);
2049 if (retval)
David Brownell28ffd792008-04-25 18:51:10 -07002050 dev_err(&intf->dev, "unlink writes failed %d, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 "iterations left %d\n", retval, i);
2052 break;
2053
2054 /* ep halt tests */
2055 case 13:
2056 if (dev->out_pipe == 0 && dev->in_pipe == 0)
2057 break;
2058 retval = 0;
David Brownell28ffd792008-04-25 18:51:10 -07002059 dev_info(&intf->dev, "TEST 13: set/clear %d halts\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 param->iterations);
2061 for (i = param->iterations; retval == 0 && i--; /* NOP */)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002062 retval = halt_simple(dev);
David Brownell28ffd792008-04-25 18:51:10 -07002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (retval)
David Brownell28ffd792008-04-25 18:51:10 -07002065 ERROR(dev, "halts failed, iterations left %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 break;
2067
2068 /* control write tests */
2069 case 14:
2070 if (!dev->info->ctrl_out)
2071 break;
David Brownell28ffd792008-04-25 18:51:10 -07002072 dev_info(&intf->dev, "TEST 14: %d ep0out, %d..%d vary %d\n",
David Brownellff7c79e2005-04-22 13:17:00 -07002073 param->iterations,
2074 realworld ? 1 : 0, param->length,
2075 param->vary);
David Brownell28ffd792008-04-25 18:51:10 -07002076 retval = ctrl_out(dev, param->iterations,
Martin Fuzzey084fb202011-01-16 19:17:11 +01002077 param->length, param->vary, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 break;
2079
2080 /* iso write tests */
2081 case 15:
2082 if (dev->out_iso_pipe == 0 || param->sglen == 0)
2083 break;
David Brownell28ffd792008-04-25 18:51:10 -07002084 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 "TEST 15: write %d iso, %d entries of %d bytes\n",
2086 param->iterations,
2087 param->sglen, param->length);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002088 /* FIRMWARE: iso sink */
2089 retval = test_iso_queue(dev, param,
Martin Fuzzey084fb202011-01-16 19:17:11 +01002090 dev->out_iso_pipe, dev->iso_out, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 break;
2092
2093 /* iso read tests */
2094 case 16:
2095 if (dev->in_iso_pipe == 0 || param->sglen == 0)
2096 break;
David Brownell28ffd792008-04-25 18:51:10 -07002097 dev_info(&intf->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 "TEST 16: read %d iso, %d entries of %d bytes\n",
2099 param->iterations,
2100 param->sglen, param->length);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002101 /* FIRMWARE: iso source */
2102 retval = test_iso_queue(dev, param,
Martin Fuzzey084fb202011-01-16 19:17:11 +01002103 dev->in_iso_pipe, dev->iso_in, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 break;
2105
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002106 /* FIXME scatterlist cancel (needs helper thread) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Martin Fuzzey084fb202011-01-16 19:17:11 +01002108 /* Tests for bulk I/O using DMA mapping by core and odd address */
2109 case 17:
2110 if (dev->out_pipe == 0)
2111 break;
2112 dev_info(&intf->dev,
2113 "TEST 17: write odd addr %d bytes %u times core map\n",
2114 param->length, param->iterations);
2115
2116 retval = test_unaligned_bulk(
2117 dev, dev->out_pipe,
2118 param->length, param->iterations,
2119 0, "test17");
2120 break;
2121
2122 case 18:
2123 if (dev->in_pipe == 0)
2124 break;
2125 dev_info(&intf->dev,
2126 "TEST 18: read odd addr %d bytes %u times core map\n",
2127 param->length, param->iterations);
2128
2129 retval = test_unaligned_bulk(
2130 dev, dev->in_pipe,
2131 param->length, param->iterations,
2132 0, "test18");
2133 break;
2134
2135 /* Tests for bulk I/O using premapped coherent buffer and odd address */
2136 case 19:
2137 if (dev->out_pipe == 0)
2138 break;
2139 dev_info(&intf->dev,
2140 "TEST 19: write odd addr %d bytes %u times premapped\n",
2141 param->length, param->iterations);
2142
2143 retval = test_unaligned_bulk(
2144 dev, dev->out_pipe,
2145 param->length, param->iterations,
2146 URB_NO_TRANSFER_DMA_MAP, "test19");
2147 break;
2148
2149 case 20:
2150 if (dev->in_pipe == 0)
2151 break;
2152 dev_info(&intf->dev,
2153 "TEST 20: read odd addr %d bytes %u times premapped\n",
2154 param->length, param->iterations);
2155
2156 retval = test_unaligned_bulk(
2157 dev, dev->in_pipe,
2158 param->length, param->iterations,
2159 URB_NO_TRANSFER_DMA_MAP, "test20");
2160 break;
2161
2162 /* control write tests with unaligned buffer */
2163 case 21:
2164 if (!dev->info->ctrl_out)
2165 break;
2166 dev_info(&intf->dev,
2167 "TEST 21: %d ep0out odd addr, %d..%d vary %d\n",
2168 param->iterations,
2169 realworld ? 1 : 0, param->length,
2170 param->vary);
2171 retval = ctrl_out(dev, param->iterations,
2172 param->length, param->vary, 1);
2173 break;
2174
2175 /* unaligned iso tests */
2176 case 22:
2177 if (dev->out_iso_pipe == 0 || param->sglen == 0)
2178 break;
2179 dev_info(&intf->dev,
2180 "TEST 22: write %d iso odd, %d entries of %d bytes\n",
2181 param->iterations,
2182 param->sglen, param->length);
2183 retval = test_iso_queue(dev, param,
2184 dev->out_iso_pipe, dev->iso_out, 1);
2185 break;
2186
2187 case 23:
2188 if (dev->in_iso_pipe == 0 || param->sglen == 0)
2189 break;
2190 dev_info(&intf->dev,
2191 "TEST 23: read %d iso odd, %d entries of %d bytes\n",
2192 param->iterations,
2193 param->sglen, param->length);
2194 retval = test_iso_queue(dev, param,
2195 dev->in_iso_pipe, dev->iso_in, 1);
2196 break;
2197
Alan Stern869410f2011-04-14 11:21:04 -04002198 /* unlink URBs from a bulk-OUT queue */
2199 case 24:
2200 if (dev->out_pipe == 0 || !param->length || param->sglen < 4)
2201 break;
2202 retval = 0;
2203 dev_info(&intf->dev, "TEST 17: unlink from %d queues of "
2204 "%d %d-byte writes\n",
2205 param->iterations, param->sglen, param->length);
2206 for (i = param->iterations; retval == 0 && i > 0; --i) {
2207 retval = unlink_queued(dev, dev->out_pipe,
2208 param->sglen, param->length);
2209 if (retval) {
2210 dev_err(&intf->dev,
2211 "unlink queued writes failed %d, "
2212 "iterations left %d\n", retval, i);
2213 break;
2214 }
2215 }
2216 break;
2217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 }
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002219 do_gettimeofday(&param->duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 param->duration.tv_sec -= start.tv_sec;
2221 param->duration.tv_usec -= start.tv_usec;
2222 if (param->duration.tv_usec < 0) {
2223 param->duration.tv_usec += 1000 * 1000;
2224 param->duration.tv_sec -= 1;
2225 }
Matthias Kaehlcke1cfab022007-12-13 16:15:33 -08002226 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 return retval;
2228}
2229
2230/*-------------------------------------------------------------------------*/
2231
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002232static unsigned force_interrupt;
2233module_param(force_interrupt, uint, 0);
2234MODULE_PARM_DESC(force_interrupt, "0 = test default; else interrupt");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236#ifdef GENERIC
2237static unsigned short vendor;
2238module_param(vendor, ushort, 0);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002239MODULE_PARM_DESC(vendor, "vendor code (from usb-if)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
2241static unsigned short product;
2242module_param(product, ushort, 0);
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002243MODULE_PARM_DESC(product, "product code (from vendor)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244#endif
2245
2246static int
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002247usbtest_probe(struct usb_interface *intf, const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
2249 struct usb_device *udev;
2250 struct usbtest_dev *dev;
2251 struct usbtest_info *info;
2252 char *rtest, *wtest;
2253 char *irtest, *iwtest;
2254
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002255 udev = interface_to_usbdev(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257#ifdef GENERIC
2258 /* specify devices by module parameters? */
2259 if (id->match_flags == 0) {
2260 /* vendor match required, product match optional */
2261 if (!vendor || le16_to_cpu(udev->descriptor.idVendor) != (u16)vendor)
2262 return -ENODEV;
2263 if (product && le16_to_cpu(udev->descriptor.idProduct) != (u16)product)
2264 return -ENODEV;
David Brownell28ffd792008-04-25 18:51:10 -07002265 dev_info(&intf->dev, "matched module params, "
2266 "vend=0x%04x prod=0x%04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 le16_to_cpu(udev->descriptor.idVendor),
2268 le16_to_cpu(udev->descriptor.idProduct));
2269 }
2270#endif
2271
Christoph Lametere94b1762006-12-06 20:33:17 -08002272 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (!dev)
2274 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 info = (struct usbtest_info *) id->driver_info;
2276 dev->info = info;
Matthias Kaehlcke1cfab022007-12-13 16:15:33 -08002277 mutex_init(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 dev->intf = intf;
2280
2281 /* cacheline-aligned scratch for i/o */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002282 dev->buf = kmalloc(TBUF_SIZE, GFP_KERNEL);
2283 if (dev->buf == NULL) {
2284 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return -ENOMEM;
2286 }
2287
2288 /* NOTE this doesn't yet test the handful of difference that are
2289 * visible with high speed interrupts: bigger maxpacket (1K) and
2290 * "high bandwidth" modes (up to 3 packets/uframe).
2291 */
2292 rtest = wtest = "";
2293 irtest = iwtest = "";
2294 if (force_interrupt || udev->speed == USB_SPEED_LOW) {
2295 if (info->ep_in) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002296 dev->in_pipe = usb_rcvintpipe(udev, info->ep_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 rtest = " intr-in";
2298 }
2299 if (info->ep_out) {
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002300 dev->out_pipe = usb_sndintpipe(udev, info->ep_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 wtest = " intr-out";
2302 }
2303 } else {
2304 if (info->autoconf) {
2305 int status;
2306
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002307 status = get_endpoints(dev, intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 if (status < 0) {
Arjan van de Venb6c63932008-07-25 01:45:52 -07002309 WARNING(dev, "couldn't get endpoints, %d\n",
David Brownell28ffd792008-04-25 18:51:10 -07002310 status);
Julia Lawallf4a728d2012-03-25 21:08:32 +02002311 kfree(dev->buf);
2312 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return status;
2314 }
2315 /* may find bulk or ISO pipes */
2316 } else {
2317 if (info->ep_in)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002318 dev->in_pipe = usb_rcvbulkpipe(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 info->ep_in);
2320 if (info->ep_out)
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002321 dev->out_pipe = usb_sndbulkpipe(udev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 info->ep_out);
2323 }
2324 if (dev->in_pipe)
2325 rtest = " bulk-in";
2326 if (dev->out_pipe)
2327 wtest = " bulk-out";
2328 if (dev->in_iso_pipe)
2329 irtest = " iso-in";
2330 if (dev->out_iso_pipe)
2331 iwtest = " iso-out";
2332 }
2333
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002334 usb_set_intfdata(intf, dev);
2335 dev_info(&intf->dev, "%s\n", info->name);
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02002336 dev_info(&intf->dev, "%s {control%s%s%s%s%s} tests%s\n",
2337 usb_speed_string(udev->speed),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 info->ctrl_out ? " in/out" : "",
2339 rtest, wtest,
2340 irtest, iwtest,
2341 info->alt >= 0 ? " (+alt)" : "");
2342 return 0;
2343}
2344
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002345static int usbtest_suspend(struct usb_interface *intf, pm_message_t message)
David Brownellff7c79e2005-04-22 13:17:00 -07002346{
David Brownellff7c79e2005-04-22 13:17:00 -07002347 return 0;
2348}
2349
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002350static int usbtest_resume(struct usb_interface *intf)
David Brownellff7c79e2005-04-22 13:17:00 -07002351{
David Brownellff7c79e2005-04-22 13:17:00 -07002352 return 0;
2353}
2354
2355
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002356static void usbtest_disconnect(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002358 struct usbtest_dev *dev = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002360 usb_set_intfdata(intf, NULL);
2361 dev_dbg(&intf->dev, "disconnect\n");
2362 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
2364
2365/* Basic testing only needs a device that can source or sink bulk traffic.
2366 * Any device can test control transfers (default with GENERIC binding).
2367 *
2368 * Several entries work with the default EP0 implementation that's built
2369 * into EZ-USB chips. There's a default vendor ID which can be overridden
2370 * by (very) small config EEPROMS, but otherwise all these devices act
2371 * identically until firmware is loaded: only EP0 works. It turns out
2372 * to be easy to make other endpoints work, without modifying that EP0
2373 * behavior. For now, we expect that kind of firmware.
2374 */
2375
2376/* an21xx or fx versions of ez-usb */
2377static struct usbtest_info ez1_info = {
2378 .name = "EZ-USB device",
2379 .ep_in = 2,
2380 .ep_out = 2,
2381 .alt = 1,
2382};
2383
2384/* fx2 version of ez-usb */
2385static struct usbtest_info ez2_info = {
2386 .name = "FX2 device",
2387 .ep_in = 6,
2388 .ep_out = 2,
2389 .alt = 1,
2390};
2391
2392/* ezusb family device with dedicated usb test firmware,
2393 */
2394static struct usbtest_info fw_info = {
2395 .name = "usb test device",
2396 .ep_in = 2,
2397 .ep_out = 2,
2398 .alt = 1,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002399 .autoconf = 1, /* iso and ctrl_out need autoconf */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 .ctrl_out = 1,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002401 .iso = 1, /* iso_ep's are #8 in/out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402};
2403
2404/* peripheral running Linux and 'zero.c' test firmware, or
2405 * its user-mode cousin. different versions of this use
2406 * different hardware with the same vendor/product codes.
2407 * host side MUST rely on the endpoint descriptors.
2408 */
2409static struct usbtest_info gz_info = {
2410 .name = "Linux gadget zero",
2411 .autoconf = 1,
2412 .ctrl_out = 1,
2413 .alt = 0,
2414};
2415
2416static struct usbtest_info um_info = {
2417 .name = "Linux user mode test driver",
2418 .autoconf = 1,
2419 .alt = -1,
2420};
2421
2422static struct usbtest_info um2_info = {
2423 .name = "Linux user mode ISO test driver",
2424 .autoconf = 1,
2425 .iso = 1,
2426 .alt = -1,
2427};
2428
2429#ifdef IBOT2
2430/* this is a nice source of high speed bulk data;
2431 * uses an FX2, with firmware provided in the device
2432 */
2433static struct usbtest_info ibot2_info = {
2434 .name = "iBOT2 webcam",
2435 .ep_in = 2,
2436 .alt = -1,
2437};
2438#endif
2439
2440#ifdef GENERIC
2441/* we can use any device to test control traffic */
2442static struct usbtest_info generic_info = {
2443 .name = "Generic USB device",
2444 .alt = -1,
2445};
2446#endif
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Németh Márton33b9e162010-01-10 15:34:45 +01002449static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 /*-------------------------------------------------------------*/
2452
2453 /* EZ-USB devices which download firmware to replace (or in our
2454 * case augment) the default device implementation.
2455 */
2456
2457 /* generic EZ-USB FX controller */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002458 { USB_DEVICE(0x0547, 0x2235),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 .driver_info = (unsigned long) &ez1_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002460 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
2462 /* CY3671 development board with EZ-USB FX */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002463 { USB_DEVICE(0x0547, 0x0080),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 .driver_info = (unsigned long) &ez1_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002465 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
2467 /* generic EZ-USB FX2 controller (or development board) */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002468 { USB_DEVICE(0x04b4, 0x8613),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 .driver_info = (unsigned long) &ez2_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002470 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472 /* re-enumerated usb test device firmware */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002473 { USB_DEVICE(0xfff0, 0xfff0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 .driver_info = (unsigned long) &fw_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002475 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 /* "Gadget Zero" firmware runs under Linux */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002478 { USB_DEVICE(0x0525, 0xa4a0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 .driver_info = (unsigned long) &gz_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002480 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
2482 /* so does a user-mode variant */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002483 { USB_DEVICE(0x0525, 0xa4a4),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 .driver_info = (unsigned long) &um_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002485 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
2487 /* ... and a user-mode variant that talks iso */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002488 { USB_DEVICE(0x0525, 0xa4a3),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 .driver_info = (unsigned long) &um2_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002490 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492#ifdef KEYSPAN_19Qi
2493 /* Keyspan 19qi uses an21xx (original EZ-USB) */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002494 /* this does not coexist with the real Keyspan 19qi driver! */
2495 { USB_DEVICE(0x06cd, 0x010b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 .driver_info = (unsigned long) &ez1_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002497 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498#endif
2499
2500 /*-------------------------------------------------------------*/
2501
2502#ifdef IBOT2
2503 /* iBOT2 makes a nice source of high speed bulk-in data */
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002504 /* this does not coexist with a real iBOT2 driver! */
2505 { USB_DEVICE(0x0b62, 0x0059),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 .driver_info = (unsigned long) &ibot2_info,
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002507 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508#endif
2509
2510 /*-------------------------------------------------------------*/
2511
2512#ifdef GENERIC
2513 /* module params can specify devices to use for control tests */
2514 { .driver_info = (unsigned long) &generic_info, },
2515#endif
2516
2517 /*-------------------------------------------------------------*/
2518
2519 { }
2520};
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002521MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523static struct usb_driver usbtest_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 .name = "usbtest",
2525 .id_table = id_table,
2526 .probe = usbtest_probe,
Andi Kleenc532b292010-06-01 23:04:41 +02002527 .unlocked_ioctl = usbtest_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 .disconnect = usbtest_disconnect,
David Brownellff7c79e2005-04-22 13:17:00 -07002529 .suspend = usbtest_suspend,
2530 .resume = usbtest_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531};
2532
2533/*-------------------------------------------------------------------------*/
2534
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002535static int __init usbtest_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536{
2537#ifdef GENERIC
2538 if (vendor)
David Brownell28ffd792008-04-25 18:51:10 -07002539 pr_debug("params: vend=0x%04x prod=0x%04x\n", vendor, product);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540#endif
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002541 return usb_register(&usbtest_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542}
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002543module_init(usbtest_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002545static void __exit usbtest_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002547 usb_deregister(&usbtest_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548}
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002549module_exit(usbtest_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Martin Fuzzeyfabbf212010-10-01 00:20:42 +02002551MODULE_DESCRIPTION("USB Core/HCD Testing Driver");
2552MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553