xref: /openbmc/linux/drivers/usb/misc/usbtest.c (revision 8cf4328569acc37ac5c5b4eb27ae86c3758f627b)
11da177e4SLinus Torvalds #include <linux/kernel.h>
21da177e4SLinus Torvalds #include <linux/errno.h>
31da177e4SLinus Torvalds #include <linux/init.h>
41da177e4SLinus Torvalds #include <linux/slab.h>
51da177e4SLinus Torvalds #include <linux/mm.h>
61da177e4SLinus Torvalds #include <linux/module.h>
71da177e4SLinus Torvalds #include <linux/moduleparam.h>
8378f058cSDavid Hardeman #include <linux/scatterlist.h>
91cfab028SMatthias Kaehlcke #include <linux/mutex.h>
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/usb.h>
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
151da177e4SLinus Torvalds 
16d0b4652fSAlan Stern static int override_alt = -1;
17d0b4652fSAlan Stern module_param_named(alt, override_alt, int, 0644);
18d0b4652fSAlan Stern MODULE_PARM_DESC(alt, ">= 0 to override altsetting selection");
19d0b4652fSAlan Stern 
20d0b4652fSAlan Stern /*-------------------------------------------------------------------------*/
21d0b4652fSAlan Stern 
22fabbf219SMartin Fuzzey /* FIXME make these public somewhere; usbdevfs.h? */
231da177e4SLinus Torvalds struct usbtest_param {
24fabbf219SMartin Fuzzey 	/* inputs */
251da177e4SLinus Torvalds 	unsigned		test_num;	/* 0..(TEST_CASES-1) */
261da177e4SLinus Torvalds 	unsigned		iterations;
271da177e4SLinus Torvalds 	unsigned		length;
281da177e4SLinus Torvalds 	unsigned		vary;
291da177e4SLinus Torvalds 	unsigned		sglen;
301da177e4SLinus Torvalds 
31fabbf219SMartin Fuzzey 	/* outputs */
321da177e4SLinus Torvalds 	struct timeval		duration;
331da177e4SLinus Torvalds };
341da177e4SLinus Torvalds #define USBTEST_REQUEST	_IOWR('U', 100, struct usbtest_param)
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #define	GENERIC		/* let probe() bind using module params */
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds /* Some devices that can be used for testing will have "real" drivers.
411da177e4SLinus Torvalds  * Entries for those need to be enabled here by hand, after disabling
421da177e4SLinus Torvalds  * that "real" driver.
431da177e4SLinus Torvalds  */
441da177e4SLinus Torvalds //#define	IBOT2		/* grab iBOT2 webcams */
451da177e4SLinus Torvalds //#define	KEYSPAN_19Qi	/* grab un-renumerated serial adapter */
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds struct usbtest_info {
501da177e4SLinus Torvalds 	const char		*name;
511da177e4SLinus Torvalds 	u8			ep_in;		/* bulk/intr source */
521da177e4SLinus Torvalds 	u8			ep_out;		/* bulk/intr sink */
531da177e4SLinus Torvalds 	unsigned		autoconf:1;
541da177e4SLinus Torvalds 	unsigned		ctrl_out:1;
551da177e4SLinus Torvalds 	unsigned		iso:1;		/* try iso in/out */
561da177e4SLinus Torvalds 	int			alt;
571da177e4SLinus Torvalds };
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /* this is accessed only through usbfs ioctl calls.
601da177e4SLinus Torvalds  * one ioctl to issue a test ... one lock per device.
611da177e4SLinus Torvalds  * tests create other threads if they need them.
621da177e4SLinus Torvalds  * urbs and buffers are allocated dynamically,
631da177e4SLinus Torvalds  * and data generated deterministically.
641da177e4SLinus Torvalds  */
651da177e4SLinus Torvalds struct usbtest_dev {
661da177e4SLinus Torvalds 	struct usb_interface	*intf;
671da177e4SLinus Torvalds 	struct usbtest_info	*info;
681da177e4SLinus Torvalds 	int			in_pipe;
691da177e4SLinus Torvalds 	int			out_pipe;
701da177e4SLinus Torvalds 	int			in_iso_pipe;
711da177e4SLinus Torvalds 	int			out_iso_pipe;
721da177e4SLinus Torvalds 	struct usb_endpoint_descriptor	*iso_in, *iso_out;
731cfab028SMatthias Kaehlcke 	struct mutex		lock;
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds #define TBUF_SIZE	256
761da177e4SLinus Torvalds 	u8			*buf;
771da177e4SLinus Torvalds };
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds static struct usb_device *testdev_to_usbdev(struct usbtest_dev *test)
801da177e4SLinus Torvalds {
811da177e4SLinus Torvalds 	return interface_to_usbdev(test->intf);
821da177e4SLinus Torvalds }
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds /* set up all urbs so they can be used with either bulk or interrupt */
851da177e4SLinus Torvalds #define	INTERRUPT_RATE		1	/* msec/transfer */
861da177e4SLinus Torvalds 
8728ffd79cSDavid Brownell #define ERROR(tdev, fmt, args...) \
8828ffd79cSDavid Brownell 	dev_err(&(tdev)->intf->dev , fmt , ## args)
89b6c63937SArjan van de Ven #define WARNING(tdev, fmt, args...) \
9028ffd79cSDavid Brownell 	dev_warn(&(tdev)->intf->dev , fmt , ## args)
911da177e4SLinus Torvalds 
92084fb206SMartin Fuzzey #define GUARD_BYTE	0xA5
93084fb206SMartin Fuzzey 
941da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds static int
971da177e4SLinus Torvalds get_endpoints(struct usbtest_dev *dev, struct usb_interface *intf)
981da177e4SLinus Torvalds {
991da177e4SLinus Torvalds 	int				tmp;
1001da177e4SLinus Torvalds 	struct usb_host_interface	*alt;
1011da177e4SLinus Torvalds 	struct usb_host_endpoint	*in, *out;
1021da177e4SLinus Torvalds 	struct usb_host_endpoint	*iso_in, *iso_out;
1031da177e4SLinus Torvalds 	struct usb_device		*udev;
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
1061da177e4SLinus Torvalds 		unsigned	ep;
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 		in = out = NULL;
1091da177e4SLinus Torvalds 		iso_in = iso_out = NULL;
1101da177e4SLinus Torvalds 		alt = intf->altsetting + tmp;
1111da177e4SLinus Torvalds 
112d0b4652fSAlan Stern 		if (override_alt >= 0 &&
113d0b4652fSAlan Stern 				override_alt != alt->desc.bAlternateSetting)
114d0b4652fSAlan Stern 			continue;
115d0b4652fSAlan Stern 
1161da177e4SLinus Torvalds 		/* take the first altsetting with in-bulk + out-bulk;
11770f23fd6SJustin P. Mattock 		 * ignore other endpoints and altsettings.
1181da177e4SLinus Torvalds 		 */
1191da177e4SLinus Torvalds 		for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
1201da177e4SLinus Torvalds 			struct usb_host_endpoint	*e;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 			e = alt->endpoint + ep;
1239a37a503SHuang Rui 			switch (usb_endpoint_type(&e->desc)) {
1241da177e4SLinus Torvalds 			case USB_ENDPOINT_XFER_BULK:
1251da177e4SLinus Torvalds 				break;
1261da177e4SLinus Torvalds 			case USB_ENDPOINT_XFER_ISOC:
1271da177e4SLinus Torvalds 				if (dev->info->iso)
1281da177e4SLinus Torvalds 					goto try_iso;
129fabbf219SMartin Fuzzey 				/* FALLTHROUGH */
1301da177e4SLinus Torvalds 			default:
1311da177e4SLinus Torvalds 				continue;
1321da177e4SLinus Torvalds 			}
1334d823dd2SLuiz Fernando N. Capitulino 			if (usb_endpoint_dir_in(&e->desc)) {
1341da177e4SLinus Torvalds 				if (!in)
1351da177e4SLinus Torvalds 					in = e;
1361da177e4SLinus Torvalds 			} else {
1371da177e4SLinus Torvalds 				if (!out)
1381da177e4SLinus Torvalds 					out = e;
1391da177e4SLinus Torvalds 			}
1401da177e4SLinus Torvalds 			continue;
1411da177e4SLinus Torvalds try_iso:
1424d823dd2SLuiz Fernando N. Capitulino 			if (usb_endpoint_dir_in(&e->desc)) {
1431da177e4SLinus Torvalds 				if (!iso_in)
1441da177e4SLinus Torvalds 					iso_in = e;
1451da177e4SLinus Torvalds 			} else {
1461da177e4SLinus Torvalds 				if (!iso_out)
1471da177e4SLinus Torvalds 					iso_out = e;
1481da177e4SLinus Torvalds 			}
1491da177e4SLinus Torvalds 		}
150951fd8eeSMing Lei 		if ((in && out)  ||  iso_in || iso_out)
1511da177e4SLinus Torvalds 			goto found;
1521da177e4SLinus Torvalds 	}
1531da177e4SLinus Torvalds 	return -EINVAL;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds found:
1561da177e4SLinus Torvalds 	udev = testdev_to_usbdev(dev);
157d0b4652fSAlan Stern 	dev->info->alt = alt->desc.bAlternateSetting;
1581da177e4SLinus Torvalds 	if (alt->desc.bAlternateSetting != 0) {
1591da177e4SLinus Torvalds 		tmp = usb_set_interface(udev,
1601da177e4SLinus Torvalds 				alt->desc.bInterfaceNumber,
1611da177e4SLinus Torvalds 				alt->desc.bAlternateSetting);
1621da177e4SLinus Torvalds 		if (tmp < 0)
1631da177e4SLinus Torvalds 			return tmp;
1641da177e4SLinus Torvalds 	}
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	if (in) {
1671da177e4SLinus Torvalds 		dev->in_pipe = usb_rcvbulkpipe(udev,
1681da177e4SLinus Torvalds 			in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
1691da177e4SLinus Torvalds 		dev->out_pipe = usb_sndbulkpipe(udev,
1701da177e4SLinus Torvalds 			out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
1711da177e4SLinus Torvalds 	}
1721da177e4SLinus Torvalds 	if (iso_in) {
1731da177e4SLinus Torvalds 		dev->iso_in = &iso_in->desc;
1741da177e4SLinus Torvalds 		dev->in_iso_pipe = usb_rcvisocpipe(udev,
1751da177e4SLinus Torvalds 				iso_in->desc.bEndpointAddress
1761da177e4SLinus Torvalds 					& USB_ENDPOINT_NUMBER_MASK);
177951fd8eeSMing Lei 	}
178951fd8eeSMing Lei 
179951fd8eeSMing Lei 	if (iso_out) {
1801da177e4SLinus Torvalds 		dev->iso_out = &iso_out->desc;
1811da177e4SLinus Torvalds 		dev->out_iso_pipe = usb_sndisocpipe(udev,
1821da177e4SLinus Torvalds 				iso_out->desc.bEndpointAddress
1831da177e4SLinus Torvalds 					& USB_ENDPOINT_NUMBER_MASK);
1841da177e4SLinus Torvalds 	}
1851da177e4SLinus Torvalds 	return 0;
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds /* Support for testing basic non-queued I/O streams.
1911da177e4SLinus Torvalds  *
1921da177e4SLinus Torvalds  * These just package urbs as requests that can be easily canceled.
1931da177e4SLinus Torvalds  * Each urb's data buffer is dynamically allocated; callers can fill
1941da177e4SLinus Torvalds  * them with non-zero test data (or test for it) when appropriate.
1951da177e4SLinus Torvalds  */
1961da177e4SLinus Torvalds 
1977d12e780SDavid Howells static void simple_callback(struct urb *urb)
1981da177e4SLinus Torvalds {
199cdc97792SMing Lei 	complete(urb->context);
2001da177e4SLinus Torvalds }
2011da177e4SLinus Torvalds 
202084fb206SMartin Fuzzey static struct urb *usbtest_alloc_urb(
2031da177e4SLinus Torvalds 	struct usb_device	*udev,
2041da177e4SLinus Torvalds 	int			pipe,
205084fb206SMartin Fuzzey 	unsigned long		bytes,
206084fb206SMartin Fuzzey 	unsigned		transfer_flags,
207084fb206SMartin Fuzzey 	unsigned		offset)
2081da177e4SLinus Torvalds {
2091da177e4SLinus Torvalds 	struct urb		*urb;
2101da177e4SLinus Torvalds 
211e94b1766SChristoph Lameter 	urb = usb_alloc_urb(0, GFP_KERNEL);
2121da177e4SLinus Torvalds 	if (!urb)
2131da177e4SLinus Torvalds 		return urb;
2141da177e4SLinus Torvalds 	usb_fill_bulk_urb(urb, udev, pipe, NULL, bytes, simple_callback, NULL);
2151da177e4SLinus Torvalds 	urb->interval = (udev->speed == USB_SPEED_HIGH)
2161da177e4SLinus Torvalds 			? (INTERRUPT_RATE << 3)
2171da177e4SLinus Torvalds 			: INTERRUPT_RATE;
218084fb206SMartin Fuzzey 	urb->transfer_flags = transfer_flags;
2191da177e4SLinus Torvalds 	if (usb_pipein(pipe))
2201da177e4SLinus Torvalds 		urb->transfer_flags |= URB_SHORT_NOT_OK;
221084fb206SMartin Fuzzey 
222084fb206SMartin Fuzzey 	if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
223084fb206SMartin Fuzzey 		urb->transfer_buffer = usb_alloc_coherent(udev, bytes + offset,
224084fb206SMartin Fuzzey 			GFP_KERNEL, &urb->transfer_dma);
225084fb206SMartin Fuzzey 	else
226084fb206SMartin Fuzzey 		urb->transfer_buffer = kmalloc(bytes + offset, GFP_KERNEL);
227084fb206SMartin Fuzzey 
2281da177e4SLinus Torvalds 	if (!urb->transfer_buffer) {
2291da177e4SLinus Torvalds 		usb_free_urb(urb);
230084fb206SMartin Fuzzey 		return NULL;
231084fb206SMartin Fuzzey 	}
232084fb206SMartin Fuzzey 
233084fb206SMartin Fuzzey 	/* To test unaligned transfers add an offset and fill the
234084fb206SMartin Fuzzey 		unused memory with a guard value */
235084fb206SMartin Fuzzey 	if (offset) {
236084fb206SMartin Fuzzey 		memset(urb->transfer_buffer, GUARD_BYTE, offset);
237084fb206SMartin Fuzzey 		urb->transfer_buffer += offset;
238084fb206SMartin Fuzzey 		if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
239084fb206SMartin Fuzzey 			urb->transfer_dma += offset;
240084fb206SMartin Fuzzey 	}
241084fb206SMartin Fuzzey 
242084fb206SMartin Fuzzey 	/* For inbound transfers use guard byte so that test fails if
243084fb206SMartin Fuzzey 		data not correctly copied */
244084fb206SMartin Fuzzey 	memset(urb->transfer_buffer,
245084fb206SMartin Fuzzey 			usb_pipein(urb->pipe) ? GUARD_BYTE : 0,
246084fb206SMartin Fuzzey 			bytes);
2471da177e4SLinus Torvalds 	return urb;
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds 
250084fb206SMartin Fuzzey static struct urb *simple_alloc_urb(
251084fb206SMartin Fuzzey 	struct usb_device	*udev,
252084fb206SMartin Fuzzey 	int			pipe,
253084fb206SMartin Fuzzey 	unsigned long		bytes)
254084fb206SMartin Fuzzey {
255084fb206SMartin Fuzzey 	return usbtest_alloc_urb(udev, pipe, bytes, URB_NO_TRANSFER_DMA_MAP, 0);
256084fb206SMartin Fuzzey }
257084fb206SMartin Fuzzey 
258fabbf219SMartin Fuzzey static unsigned pattern;
2597f4e9854SVikram Pandita static unsigned mod_pattern;
2607f4e9854SVikram Pandita module_param_named(pattern, mod_pattern, uint, S_IRUGO | S_IWUSR);
2617f4e9854SVikram Pandita MODULE_PARM_DESC(mod_pattern, "i/o pattern (0 == zeroes)");
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds static inline void simple_fill_buf(struct urb *urb)
2641da177e4SLinus Torvalds {
2651da177e4SLinus Torvalds 	unsigned	i;
2661da177e4SLinus Torvalds 	u8		*buf = urb->transfer_buffer;
2671da177e4SLinus Torvalds 	unsigned	len = urb->transfer_buffer_length;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	switch (pattern) {
2701da177e4SLinus Torvalds 	default:
271fabbf219SMartin Fuzzey 		/* FALLTHROUGH */
2721da177e4SLinus Torvalds 	case 0:
2731da177e4SLinus Torvalds 		memset(buf, 0, len);
2741da177e4SLinus Torvalds 		break;
2751da177e4SLinus Torvalds 	case 1:			/* mod63 */
2761da177e4SLinus Torvalds 		for (i = 0; i < len; i++)
2771da177e4SLinus Torvalds 			*buf++ = (u8) (i % 63);
2781da177e4SLinus Torvalds 		break;
2791da177e4SLinus Torvalds 	}
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds 
282304b0b57SGreg Dietsche static inline unsigned long buffer_offset(void *buf)
283084fb206SMartin Fuzzey {
284304b0b57SGreg Dietsche 	return (unsigned long)buf & (ARCH_KMALLOC_MINALIGN - 1);
285084fb206SMartin Fuzzey }
286084fb206SMartin Fuzzey 
287084fb206SMartin Fuzzey static int check_guard_bytes(struct usbtest_dev *tdev, struct urb *urb)
288084fb206SMartin Fuzzey {
289084fb206SMartin Fuzzey 	u8 *buf = urb->transfer_buffer;
290084fb206SMartin Fuzzey 	u8 *guard = buf - buffer_offset(buf);
291084fb206SMartin Fuzzey 	unsigned i;
292084fb206SMartin Fuzzey 
293084fb206SMartin Fuzzey 	for (i = 0; guard < buf; i++, guard++) {
294084fb206SMartin Fuzzey 		if (*guard != GUARD_BYTE) {
295084fb206SMartin Fuzzey 			ERROR(tdev, "guard byte[%d] %d (not %d)\n",
296084fb206SMartin Fuzzey 				i, *guard, GUARD_BYTE);
297084fb206SMartin Fuzzey 			return -EINVAL;
298084fb206SMartin Fuzzey 		}
299084fb206SMartin Fuzzey 	}
300084fb206SMartin Fuzzey 	return 0;
301084fb206SMartin Fuzzey }
302084fb206SMartin Fuzzey 
303084fb206SMartin Fuzzey static int simple_check_buf(struct usbtest_dev *tdev, struct urb *urb)
3041da177e4SLinus Torvalds {
3051da177e4SLinus Torvalds 	unsigned	i;
3061da177e4SLinus Torvalds 	u8		expected;
3071da177e4SLinus Torvalds 	u8		*buf = urb->transfer_buffer;
3081da177e4SLinus Torvalds 	unsigned	len = urb->actual_length;
3091da177e4SLinus Torvalds 
310084fb206SMartin Fuzzey 	int ret = check_guard_bytes(tdev, urb);
311084fb206SMartin Fuzzey 	if (ret)
312084fb206SMartin Fuzzey 		return ret;
313084fb206SMartin Fuzzey 
3141da177e4SLinus Torvalds 	for (i = 0; i < len; i++, buf++) {
3151da177e4SLinus Torvalds 		switch (pattern) {
3161da177e4SLinus Torvalds 		/* all-zeroes has no synchronization issues */
3171da177e4SLinus Torvalds 		case 0:
3181da177e4SLinus Torvalds 			expected = 0;
3191da177e4SLinus Torvalds 			break;
3201da177e4SLinus Torvalds 		/* mod63 stays in sync with short-terminated transfers,
3211da177e4SLinus Torvalds 		 * or otherwise when host and gadget agree on how large
3221da177e4SLinus Torvalds 		 * each usb transfer request should be.  resync is done
3231da177e4SLinus Torvalds 		 * with set_interface or set_config.
3241da177e4SLinus Torvalds 		 */
3251da177e4SLinus Torvalds 		case 1:			/* mod63 */
3261da177e4SLinus Torvalds 			expected = i % 63;
3271da177e4SLinus Torvalds 			break;
3281da177e4SLinus Torvalds 		/* always fail unsupported patterns */
3291da177e4SLinus Torvalds 		default:
3301da177e4SLinus Torvalds 			expected = !*buf;
3311da177e4SLinus Torvalds 			break;
3321da177e4SLinus Torvalds 		}
3331da177e4SLinus Torvalds 		if (*buf == expected)
3341da177e4SLinus Torvalds 			continue;
33528ffd79cSDavid Brownell 		ERROR(tdev, "buf[%d] = %d (not %d)\n", i, *buf, expected);
3361da177e4SLinus Torvalds 		return -EINVAL;
3371da177e4SLinus Torvalds 	}
3381da177e4SLinus Torvalds 	return 0;
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds static void simple_free_urb(struct urb *urb)
3421da177e4SLinus Torvalds {
343304b0b57SGreg Dietsche 	unsigned long offset = buffer_offset(urb->transfer_buffer);
344084fb206SMartin Fuzzey 
345084fb206SMartin Fuzzey 	if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
346084fb206SMartin Fuzzey 		usb_free_coherent(
347084fb206SMartin Fuzzey 			urb->dev,
348084fb206SMartin Fuzzey 			urb->transfer_buffer_length + offset,
349084fb206SMartin Fuzzey 			urb->transfer_buffer - offset,
350084fb206SMartin Fuzzey 			urb->transfer_dma - offset);
351084fb206SMartin Fuzzey 	else
352084fb206SMartin Fuzzey 		kfree(urb->transfer_buffer - offset);
3531da177e4SLinus Torvalds 	usb_free_urb(urb);
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds static int simple_io(
35728ffd79cSDavid Brownell 	struct usbtest_dev	*tdev,
3581da177e4SLinus Torvalds 	struct urb		*urb,
3591da177e4SLinus Torvalds 	int			iterations,
3601da177e4SLinus Torvalds 	int			vary,
3611da177e4SLinus Torvalds 	int			expected,
3621da177e4SLinus Torvalds 	const char		*label
3631da177e4SLinus Torvalds )
3641da177e4SLinus Torvalds {
3651da177e4SLinus Torvalds 	struct usb_device	*udev = urb->dev;
3661da177e4SLinus Torvalds 	int			max = urb->transfer_buffer_length;
3671da177e4SLinus Torvalds 	struct completion	completion;
3681da177e4SLinus Torvalds 	int			retval = 0;
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	urb->context = &completion;
3711da177e4SLinus Torvalds 	while (retval == 0 && iterations-- > 0) {
3721da177e4SLinus Torvalds 		init_completion(&completion);
3737c79d094SSebastian Andrzej Siewior 		if (usb_pipeout(urb->pipe)) {
3741da177e4SLinus Torvalds 			simple_fill_buf(urb);
3757c79d094SSebastian Andrzej Siewior 			urb->transfer_flags |= URB_ZERO_PACKET;
3767c79d094SSebastian Andrzej Siewior 		}
377fabbf219SMartin Fuzzey 		retval = usb_submit_urb(urb, GFP_KERNEL);
378fabbf219SMartin Fuzzey 		if (retval != 0)
3791da177e4SLinus Torvalds 			break;
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 		/* NOTE:  no timeouts; can't be broken out of by interrupt */
3821da177e4SLinus Torvalds 		wait_for_completion(&completion);
3831da177e4SLinus Torvalds 		retval = urb->status;
3841da177e4SLinus Torvalds 		urb->dev = udev;
3851da177e4SLinus Torvalds 		if (retval == 0 && usb_pipein(urb->pipe))
38628ffd79cSDavid Brownell 			retval = simple_check_buf(tdev, urb);
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 		if (vary) {
3891da177e4SLinus Torvalds 			int	len = urb->transfer_buffer_length;
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 			len += vary;
3921da177e4SLinus Torvalds 			len %= max;
3931da177e4SLinus Torvalds 			if (len == 0)
3941da177e4SLinus Torvalds 				len = (vary < max) ? vary : max;
3951da177e4SLinus Torvalds 			urb->transfer_buffer_length = len;
3961da177e4SLinus Torvalds 		}
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 		/* FIXME if endpoint halted, clear halt (and log) */
3991da177e4SLinus Torvalds 	}
4001da177e4SLinus Torvalds 	urb->transfer_buffer_length = max;
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	if (expected != retval)
40328ffd79cSDavid Brownell 		dev_err(&udev->dev,
4041da177e4SLinus Torvalds 			"%s failed, iterations left %d, status %d (not %d)\n",
4051da177e4SLinus Torvalds 				label, iterations, retval, expected);
4061da177e4SLinus Torvalds 	return retval;
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds /* We use scatterlist primitives to test queued I/O.
4131da177e4SLinus Torvalds  * Yes, this also tests the scatterlist primitives.
4141da177e4SLinus Torvalds  */
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds static void free_sglist(struct scatterlist *sg, int nents)
4171da177e4SLinus Torvalds {
4181da177e4SLinus Torvalds 	unsigned		i;
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	if (!sg)
4211da177e4SLinus Torvalds 		return;
4221da177e4SLinus Torvalds 	for (i = 0; i < nents; i++) {
42345711f1aSJens Axboe 		if (!sg_page(&sg[i]))
4241da177e4SLinus Torvalds 			continue;
42545711f1aSJens Axboe 		kfree(sg_virt(&sg[i]));
4261da177e4SLinus Torvalds 	}
4271da177e4SLinus Torvalds 	kfree(sg);
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds static struct scatterlist *
4311da177e4SLinus Torvalds alloc_sglist(int nents, int max, int vary)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	struct scatterlist	*sg;
4341da177e4SLinus Torvalds 	unsigned		i;
4351da177e4SLinus Torvalds 	unsigned		size = max;
4361da177e4SLinus Torvalds 
437564e6989SDan Carpenter 	if (max == 0)
438564e6989SDan Carpenter 		return NULL;
439564e6989SDan Carpenter 
440f55055b4SHuang Rui 	sg = kmalloc_array(nents, sizeof(*sg), GFP_KERNEL);
4411da177e4SLinus Torvalds 	if (!sg)
4421da177e4SLinus Torvalds 		return NULL;
4434756febbSAlan Stern 	sg_init_table(sg, nents);
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	for (i = 0; i < nents; i++) {
4461da177e4SLinus Torvalds 		char		*buf;
4478b524901SDavid Brownell 		unsigned	j;
4481da177e4SLinus Torvalds 
449e94b1766SChristoph Lameter 		buf = kzalloc(size, GFP_KERNEL);
4501da177e4SLinus Torvalds 		if (!buf) {
4511da177e4SLinus Torvalds 			free_sglist(sg, i);
4521da177e4SLinus Torvalds 			return NULL;
4531da177e4SLinus Torvalds 		}
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 		/* kmalloc pages are always physically contiguous! */
4564756febbSAlan Stern 		sg_set_buf(&sg[i], buf, size);
4571da177e4SLinus Torvalds 
4588b524901SDavid Brownell 		switch (pattern) {
4598b524901SDavid Brownell 		case 0:
4608b524901SDavid Brownell 			/* already zeroed */
4618b524901SDavid Brownell 			break;
4628b524901SDavid Brownell 		case 1:
4638b524901SDavid Brownell 			for (j = 0; j < size; j++)
4648b524901SDavid Brownell 				*buf++ = (u8) (j % 63);
4658b524901SDavid Brownell 			break;
4668b524901SDavid Brownell 		}
4678b524901SDavid Brownell 
4681da177e4SLinus Torvalds 		if (vary) {
4691da177e4SLinus Torvalds 			size += vary;
4701da177e4SLinus Torvalds 			size %= max;
4711da177e4SLinus Torvalds 			if (size == 0)
4721da177e4SLinus Torvalds 				size = (vary < max) ? vary : max;
4731da177e4SLinus Torvalds 		}
4741da177e4SLinus Torvalds 	}
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	return sg;
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds static int perform_sglist(
48028ffd79cSDavid Brownell 	struct usbtest_dev	*tdev,
4811da177e4SLinus Torvalds 	unsigned		iterations,
4821da177e4SLinus Torvalds 	int			pipe,
4831da177e4SLinus Torvalds 	struct usb_sg_request	*req,
4841da177e4SLinus Torvalds 	struct scatterlist	*sg,
4851da177e4SLinus Torvalds 	int			nents
4861da177e4SLinus Torvalds )
4871da177e4SLinus Torvalds {
48828ffd79cSDavid Brownell 	struct usb_device	*udev = testdev_to_usbdev(tdev);
4891da177e4SLinus Torvalds 	int			retval = 0;
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	while (retval == 0 && iterations-- > 0) {
4921da177e4SLinus Torvalds 		retval = usb_sg_init(req, udev, pipe,
4931da177e4SLinus Torvalds 				(udev->speed == USB_SPEED_HIGH)
4941da177e4SLinus Torvalds 					? (INTERRUPT_RATE << 3)
4951da177e4SLinus Torvalds 					: INTERRUPT_RATE,
496e94b1766SChristoph Lameter 				sg, nents, 0, GFP_KERNEL);
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 		if (retval)
4991da177e4SLinus Torvalds 			break;
5001da177e4SLinus Torvalds 		usb_sg_wait(req);
5011da177e4SLinus Torvalds 		retval = req->status;
5021da177e4SLinus Torvalds 
5038b524901SDavid Brownell 		/* FIXME check resulting data pattern */
5048b524901SDavid Brownell 
5051da177e4SLinus Torvalds 		/* FIXME if endpoint halted, clear halt (and log) */
5061da177e4SLinus Torvalds 	}
5071da177e4SLinus Torvalds 
508fabbf219SMartin Fuzzey 	/* FIXME for unlink or fault handling tests, don't report
509fabbf219SMartin Fuzzey 	 * failure if retval is as we expected ...
510fabbf219SMartin Fuzzey 	 */
5111da177e4SLinus Torvalds 	if (retval)
51228ffd79cSDavid Brownell 		ERROR(tdev, "perform_sglist failed, "
51328ffd79cSDavid Brownell 				"iterations left %d, status %d\n",
5141da177e4SLinus Torvalds 				iterations, retval);
5151da177e4SLinus Torvalds 	return retval;
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds /* unqueued control message testing
5221da177e4SLinus Torvalds  *
5231da177e4SLinus Torvalds  * there's a nice set of device functional requirements in chapter 9 of the
5241da177e4SLinus Torvalds  * usb 2.0 spec, which we can apply to ANY device, even ones that don't use
5251da177e4SLinus Torvalds  * special test firmware.
5261da177e4SLinus Torvalds  *
5271da177e4SLinus Torvalds  * we know the device is configured (or suspended) by the time it's visible
5281da177e4SLinus Torvalds  * through usbfs.  we can't change that, so we won't test enumeration (which
5291da177e4SLinus Torvalds  * worked 'well enough' to get here, this time), power management (ditto),
5301da177e4SLinus Torvalds  * or remote wakeup (which needs human interaction).
5311da177e4SLinus Torvalds  */
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds static unsigned realworld = 1;
5341da177e4SLinus Torvalds module_param(realworld, uint, 0);
535ff7c79e4SDavid Brownell MODULE_PARM_DESC(realworld, "clear to demand stricter spec compliance");
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds static int get_altsetting(struct usbtest_dev *dev)
5381da177e4SLinus Torvalds {
5391da177e4SLinus Torvalds 	struct usb_interface	*iface = dev->intf;
5401da177e4SLinus Torvalds 	struct usb_device	*udev = interface_to_usbdev(iface);
5411da177e4SLinus Torvalds 	int			retval;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
5441da177e4SLinus Torvalds 			USB_REQ_GET_INTERFACE, USB_DIR_IN|USB_RECIP_INTERFACE,
5451da177e4SLinus Torvalds 			0, iface->altsetting[0].desc.bInterfaceNumber,
5461da177e4SLinus Torvalds 			dev->buf, 1, USB_CTRL_GET_TIMEOUT);
5471da177e4SLinus Torvalds 	switch (retval) {
5481da177e4SLinus Torvalds 	case 1:
5491da177e4SLinus Torvalds 		return dev->buf[0];
5501da177e4SLinus Torvalds 	case 0:
5511da177e4SLinus Torvalds 		retval = -ERANGE;
552fabbf219SMartin Fuzzey 		/* FALLTHROUGH */
5531da177e4SLinus Torvalds 	default:
5541da177e4SLinus Torvalds 		return retval;
5551da177e4SLinus Torvalds 	}
5561da177e4SLinus Torvalds }
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds static int set_altsetting(struct usbtest_dev *dev, int alternate)
5591da177e4SLinus Torvalds {
5601da177e4SLinus Torvalds 	struct usb_interface		*iface = dev->intf;
5611da177e4SLinus Torvalds 	struct usb_device		*udev;
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	if (alternate < 0 || alternate >= 256)
5641da177e4SLinus Torvalds 		return -EINVAL;
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	udev = interface_to_usbdev(iface);
5671da177e4SLinus Torvalds 	return usb_set_interface(udev,
5681da177e4SLinus Torvalds 			iface->altsetting[0].desc.bInterfaceNumber,
5691da177e4SLinus Torvalds 			alternate);
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds 
57228ffd79cSDavid Brownell static int is_good_config(struct usbtest_dev *tdev, int len)
5731da177e4SLinus Torvalds {
5741da177e4SLinus Torvalds 	struct usb_config_descriptor	*config;
5751da177e4SLinus Torvalds 
576f55055b4SHuang Rui 	if (len < sizeof(*config))
5771da177e4SLinus Torvalds 		return 0;
57828ffd79cSDavid Brownell 	config = (struct usb_config_descriptor *) tdev->buf;
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	switch (config->bDescriptorType) {
5811da177e4SLinus Torvalds 	case USB_DT_CONFIG:
5821da177e4SLinus Torvalds 	case USB_DT_OTHER_SPEED_CONFIG:
5831da177e4SLinus Torvalds 		if (config->bLength != 9) {
58428ffd79cSDavid Brownell 			ERROR(tdev, "bogus config descriptor length\n");
5851da177e4SLinus Torvalds 			return 0;
5861da177e4SLinus Torvalds 		}
5871da177e4SLinus Torvalds 		/* this bit 'must be 1' but often isn't */
5881da177e4SLinus Torvalds 		if (!realworld && !(config->bmAttributes & 0x80)) {
58928ffd79cSDavid Brownell 			ERROR(tdev, "high bit of config attributes not set\n");
5901da177e4SLinus Torvalds 			return 0;
5911da177e4SLinus Torvalds 		}
5921da177e4SLinus Torvalds 		if (config->bmAttributes & 0x1f) {	/* reserved == 0 */
59328ffd79cSDavid Brownell 			ERROR(tdev, "reserved config bits set\n");
5941da177e4SLinus Torvalds 			return 0;
5951da177e4SLinus Torvalds 		}
5961da177e4SLinus Torvalds 		break;
5971da177e4SLinus Torvalds 	default:
5981da177e4SLinus Torvalds 		return 0;
5991da177e4SLinus Torvalds 	}
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	if (le16_to_cpu(config->wTotalLength) == len)	/* read it all */
6021da177e4SLinus Torvalds 		return 1;
6031da177e4SLinus Torvalds 	if (le16_to_cpu(config->wTotalLength) >= TBUF_SIZE)	/* max partial read */
6041da177e4SLinus Torvalds 		return 1;
60528ffd79cSDavid Brownell 	ERROR(tdev, "bogus config descriptor read size\n");
6061da177e4SLinus Torvalds 	return 0;
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
60982f92672SHuang Rui static int is_good_ext(struct usbtest_dev *tdev, u8 *buf)
61082f92672SHuang Rui {
61182f92672SHuang Rui 	struct usb_ext_cap_descriptor *ext;
61282f92672SHuang Rui 	u32 attr;
61382f92672SHuang Rui 
61482f92672SHuang Rui 	ext = (struct usb_ext_cap_descriptor *) buf;
61582f92672SHuang Rui 
61682f92672SHuang Rui 	if (ext->bLength != USB_DT_USB_EXT_CAP_SIZE) {
61782f92672SHuang Rui 		ERROR(tdev, "bogus usb 2.0 extension descriptor length\n");
61882f92672SHuang Rui 		return 0;
61982f92672SHuang Rui 	}
62082f92672SHuang Rui 
62182f92672SHuang Rui 	attr = le32_to_cpu(ext->bmAttributes);
622875bc23aSHuang Rui 	/* bits[1:15] is used and others are reserved */
623875bc23aSHuang Rui 	if (attr & ~0xfffe) {	/* reserved == 0 */
62482f92672SHuang Rui 		ERROR(tdev, "reserved bits set\n");
62582f92672SHuang Rui 		return 0;
62682f92672SHuang Rui 	}
62782f92672SHuang Rui 
62882f92672SHuang Rui 	return 1;
62982f92672SHuang Rui }
63082f92672SHuang Rui 
631b8fef795SHuang Rui static int is_good_ss_cap(struct usbtest_dev *tdev, u8 *buf)
632b8fef795SHuang Rui {
633b8fef795SHuang Rui 	struct usb_ss_cap_descriptor *ss;
634b8fef795SHuang Rui 
635b8fef795SHuang Rui 	ss = (struct usb_ss_cap_descriptor *) buf;
636b8fef795SHuang Rui 
637b8fef795SHuang Rui 	if (ss->bLength != USB_DT_USB_SS_CAP_SIZE) {
638b8fef795SHuang Rui 		ERROR(tdev, "bogus superspeed device capability descriptor length\n");
639b8fef795SHuang Rui 		return 0;
640b8fef795SHuang Rui 	}
641b8fef795SHuang Rui 
642b8fef795SHuang Rui 	/*
643b8fef795SHuang Rui 	 * only bit[1] of bmAttributes is used for LTM and others are
644b8fef795SHuang Rui 	 * reserved
645b8fef795SHuang Rui 	 */
646b8fef795SHuang Rui 	if (ss->bmAttributes & ~0x02) {	/* reserved == 0 */
647b8fef795SHuang Rui 		ERROR(tdev, "reserved bits set in bmAttributes\n");
648b8fef795SHuang Rui 		return 0;
649b8fef795SHuang Rui 	}
650b8fef795SHuang Rui 
651b8fef795SHuang Rui 	/* bits[0:3] of wSpeedSupported is used and others are reserved */
652b8fef795SHuang Rui 	if (le16_to_cpu(ss->wSpeedSupported) & ~0x0f) {	/* reserved == 0 */
653b8fef795SHuang Rui 		ERROR(tdev, "reserved bits set in wSpeedSupported\n");
654b8fef795SHuang Rui 		return 0;
655b8fef795SHuang Rui 	}
656b8fef795SHuang Rui 
657b8fef795SHuang Rui 	return 1;
658b8fef795SHuang Rui }
659b8fef795SHuang Rui 
6608e29217fSHuang Rui static int is_good_con_id(struct usbtest_dev *tdev, u8 *buf)
6618e29217fSHuang Rui {
6628e29217fSHuang Rui 	struct usb_ss_container_id_descriptor *con_id;
6638e29217fSHuang Rui 
6648e29217fSHuang Rui 	con_id = (struct usb_ss_container_id_descriptor *) buf;
6658e29217fSHuang Rui 
6668e29217fSHuang Rui 	if (con_id->bLength != USB_DT_USB_SS_CONTN_ID_SIZE) {
6678e29217fSHuang Rui 		ERROR(tdev, "bogus container id descriptor length\n");
6688e29217fSHuang Rui 		return 0;
6698e29217fSHuang Rui 	}
6708e29217fSHuang Rui 
6718e29217fSHuang Rui 	if (con_id->bReserved) {	/* reserved == 0 */
6728e29217fSHuang Rui 		ERROR(tdev, "reserved bits set\n");
6738e29217fSHuang Rui 		return 0;
6748e29217fSHuang Rui 	}
6758e29217fSHuang Rui 
6768e29217fSHuang Rui 	return 1;
6778e29217fSHuang Rui }
6788e29217fSHuang Rui 
6791da177e4SLinus Torvalds /* sanity test for standard requests working with usb_control_mesg() and some
6801da177e4SLinus Torvalds  * of the utility functions which use it.
6811da177e4SLinus Torvalds  *
6821da177e4SLinus Torvalds  * this doesn't test how endpoint halts behave or data toggles get set, since
6831da177e4SLinus Torvalds  * we won't do I/O to bulk/interrupt endpoints here (which is how to change
6841da177e4SLinus Torvalds  * halt or toggle).  toggle testing is impractical without support from hcds.
6851da177e4SLinus Torvalds  *
6861da177e4SLinus Torvalds  * this avoids failing devices linux would normally work with, by not testing
6871da177e4SLinus Torvalds  * config/altsetting operations for devices that only support their defaults.
6881da177e4SLinus Torvalds  * such devices rarely support those needless operations.
6891da177e4SLinus Torvalds  *
6901da177e4SLinus Torvalds  * NOTE that since this is a sanity test, it's not examining boundary cases
6911da177e4SLinus Torvalds  * to see if usbcore, hcd, and device all behave right.  such testing would
6921da177e4SLinus Torvalds  * involve varied read sizes and other operation sequences.
6931da177e4SLinus Torvalds  */
6941da177e4SLinus Torvalds static int ch9_postconfig(struct usbtest_dev *dev)
6951da177e4SLinus Torvalds {
6961da177e4SLinus Torvalds 	struct usb_interface	*iface = dev->intf;
6971da177e4SLinus Torvalds 	struct usb_device	*udev = interface_to_usbdev(iface);
6981da177e4SLinus Torvalds 	int			i, alt, retval;
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	/* [9.2.3] if there's more than one altsetting, we need to be able to
7011da177e4SLinus Torvalds 	 * set and get each one.  mostly trusts the descriptors from usbcore.
7021da177e4SLinus Torvalds 	 */
7031da177e4SLinus Torvalds 	for (i = 0; i < iface->num_altsetting; i++) {
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 		/* 9.2.3 constrains the range here */
7061da177e4SLinus Torvalds 		alt = iface->altsetting[i].desc.bAlternateSetting;
7071da177e4SLinus Torvalds 		if (alt < 0 || alt >= iface->num_altsetting) {
70828ffd79cSDavid Brownell 			dev_err(&iface->dev,
7091da177e4SLinus Torvalds 					"invalid alt [%d].bAltSetting = %d\n",
7101da177e4SLinus Torvalds 					i, alt);
7111da177e4SLinus Torvalds 		}
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 		/* [real world] get/set unimplemented if there's only one */
7141da177e4SLinus Torvalds 		if (realworld && iface->num_altsetting == 1)
7151da177e4SLinus Torvalds 			continue;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 		/* [9.4.10] set_interface */
7181da177e4SLinus Torvalds 		retval = set_altsetting(dev, alt);
7191da177e4SLinus Torvalds 		if (retval) {
72028ffd79cSDavid Brownell 			dev_err(&iface->dev, "can't set_interface = %d, %d\n",
7211da177e4SLinus Torvalds 					alt, retval);
7221da177e4SLinus Torvalds 			return retval;
7231da177e4SLinus Torvalds 		}
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 		/* [9.4.4] get_interface always works */
7261da177e4SLinus Torvalds 		retval = get_altsetting(dev);
7271da177e4SLinus Torvalds 		if (retval != alt) {
72828ffd79cSDavid Brownell 			dev_err(&iface->dev, "get alt should be %d, was %d\n",
7291da177e4SLinus Torvalds 					alt, retval);
7301da177e4SLinus Torvalds 			return (retval < 0) ? retval : -EDOM;
7311da177e4SLinus Torvalds 		}
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	}
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	/* [real world] get_config unimplemented if there's only one */
7361da177e4SLinus Torvalds 	if (!realworld || udev->descriptor.bNumConfigurations != 1) {
7371da177e4SLinus Torvalds 		int	expected = udev->actconfig->desc.bConfigurationValue;
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 		/* [9.4.2] get_configuration always works
7401da177e4SLinus Torvalds 		 * ... although some cheap devices (like one TI Hub I've got)
7411da177e4SLinus Torvalds 		 * won't return config descriptors except before set_config.
7421da177e4SLinus Torvalds 		 */
7431da177e4SLinus Torvalds 		retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
7441da177e4SLinus Torvalds 				USB_REQ_GET_CONFIGURATION,
7451da177e4SLinus Torvalds 				USB_DIR_IN | USB_RECIP_DEVICE,
7461da177e4SLinus Torvalds 				0, 0, dev->buf, 1, USB_CTRL_GET_TIMEOUT);
7471da177e4SLinus Torvalds 		if (retval != 1 || dev->buf[0] != expected) {
74828ffd79cSDavid Brownell 			dev_err(&iface->dev, "get config --> %d %d (1 %d)\n",
749ff7c79e4SDavid Brownell 				retval, dev->buf[0], expected);
7501da177e4SLinus Torvalds 			return (retval < 0) ? retval : -EDOM;
7511da177e4SLinus Torvalds 		}
7521da177e4SLinus Torvalds 	}
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	/* there's always [9.4.3] a device descriptor [9.6.1] */
7551da177e4SLinus Torvalds 	retval = usb_get_descriptor(udev, USB_DT_DEVICE, 0,
756f55055b4SHuang Rui 			dev->buf, sizeof(udev->descriptor));
757f55055b4SHuang Rui 	if (retval != sizeof(udev->descriptor)) {
75828ffd79cSDavid Brownell 		dev_err(&iface->dev, "dev descriptor --> %d\n", retval);
7591da177e4SLinus Torvalds 		return (retval < 0) ? retval : -EDOM;
7601da177e4SLinus Torvalds 	}
7611da177e4SLinus Torvalds 
7629d3bd768SHuang Rui 	/*
7639d3bd768SHuang Rui 	 * there's always [9.4.3] a bos device descriptor [9.6.2] in USB
7649d3bd768SHuang Rui 	 * 3.0 spec
7659d3bd768SHuang Rui 	 */
766f625099fSHuang Rui 	if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0210) {
76782f92672SHuang Rui 		struct usb_bos_descriptor *bos = NULL;
76882f92672SHuang Rui 		struct usb_dev_cap_header *header = NULL;
76982f92672SHuang Rui 		unsigned total, num, length;
77082f92672SHuang Rui 		u8 *buf;
77182f92672SHuang Rui 
7729d3bd768SHuang Rui 		retval = usb_get_descriptor(udev, USB_DT_BOS, 0, dev->buf,
7739d3bd768SHuang Rui 				sizeof(*udev->bos->desc));
7749d3bd768SHuang Rui 		if (retval != sizeof(*udev->bos->desc)) {
7759d3bd768SHuang Rui 			dev_err(&iface->dev, "bos descriptor --> %d\n", retval);
7769d3bd768SHuang Rui 			return (retval < 0) ? retval : -EDOM;
7779d3bd768SHuang Rui 		}
77882f92672SHuang Rui 
77982f92672SHuang Rui 		bos = (struct usb_bos_descriptor *)dev->buf;
78082f92672SHuang Rui 		total = le16_to_cpu(bos->wTotalLength);
78182f92672SHuang Rui 		num = bos->bNumDeviceCaps;
78282f92672SHuang Rui 
78382f92672SHuang Rui 		if (total > TBUF_SIZE)
78482f92672SHuang Rui 			total = TBUF_SIZE;
78582f92672SHuang Rui 
78682f92672SHuang Rui 		/*
78782f92672SHuang Rui 		 * get generic device-level capability descriptors [9.6.2]
78882f92672SHuang Rui 		 * in USB 3.0 spec
78982f92672SHuang Rui 		 */
79082f92672SHuang Rui 		retval = usb_get_descriptor(udev, USB_DT_BOS, 0, dev->buf,
79182f92672SHuang Rui 				total);
79282f92672SHuang Rui 		if (retval != total) {
79382f92672SHuang Rui 			dev_err(&iface->dev, "bos descriptor set --> %d\n",
79482f92672SHuang Rui 					retval);
79582f92672SHuang Rui 			return (retval < 0) ? retval : -EDOM;
79682f92672SHuang Rui 		}
79782f92672SHuang Rui 
79882f92672SHuang Rui 		length = sizeof(*udev->bos->desc);
79982f92672SHuang Rui 		buf = dev->buf;
80082f92672SHuang Rui 		for (i = 0; i < num; i++) {
80182f92672SHuang Rui 			buf += length;
80282f92672SHuang Rui 			if (buf + sizeof(struct usb_dev_cap_header) >
80382f92672SHuang Rui 					dev->buf + total)
80482f92672SHuang Rui 				break;
80582f92672SHuang Rui 
80682f92672SHuang Rui 			header = (struct usb_dev_cap_header *)buf;
80782f92672SHuang Rui 			length = header->bLength;
80882f92672SHuang Rui 
80982f92672SHuang Rui 			if (header->bDescriptorType !=
81082f92672SHuang Rui 					USB_DT_DEVICE_CAPABILITY) {
81182f92672SHuang Rui 				dev_warn(&udev->dev, "not device capability descriptor, skip\n");
81282f92672SHuang Rui 				continue;
81382f92672SHuang Rui 			}
81482f92672SHuang Rui 
81582f92672SHuang Rui 			switch (header->bDevCapabilityType) {
81682f92672SHuang Rui 			case USB_CAP_TYPE_EXT:
81782f92672SHuang Rui 				if (buf + USB_DT_USB_EXT_CAP_SIZE >
81882f92672SHuang Rui 						dev->buf + total ||
81982f92672SHuang Rui 						!is_good_ext(dev, buf)) {
82082f92672SHuang Rui 					dev_err(&iface->dev, "bogus usb 2.0 extension descriptor\n");
82182f92672SHuang Rui 					return -EDOM;
82282f92672SHuang Rui 				}
82382f92672SHuang Rui 				break;
824b8fef795SHuang Rui 			case USB_SS_CAP_TYPE:
825b8fef795SHuang Rui 				if (buf + USB_DT_USB_SS_CAP_SIZE >
826b8fef795SHuang Rui 						dev->buf + total ||
827b8fef795SHuang Rui 						!is_good_ss_cap(dev, buf)) {
828b8fef795SHuang Rui 					dev_err(&iface->dev, "bogus superspeed device capability descriptor\n");
829b8fef795SHuang Rui 					return -EDOM;
830b8fef795SHuang Rui 				}
831b8fef795SHuang Rui 				break;
8328e29217fSHuang Rui 			case CONTAINER_ID_TYPE:
8338e29217fSHuang Rui 				if (buf + USB_DT_USB_SS_CONTN_ID_SIZE >
8348e29217fSHuang Rui 						dev->buf + total ||
8358e29217fSHuang Rui 						!is_good_con_id(dev, buf)) {
8368e29217fSHuang Rui 					dev_err(&iface->dev, "bogus container id descriptor\n");
8378e29217fSHuang Rui 					return -EDOM;
8388e29217fSHuang Rui 				}
8398e29217fSHuang Rui 				break;
84082f92672SHuang Rui 			default:
84182f92672SHuang Rui 				break;
84282f92672SHuang Rui 			}
84382f92672SHuang Rui 		}
8449d3bd768SHuang Rui 	}
8459d3bd768SHuang Rui 
8461da177e4SLinus Torvalds 	/* there's always [9.4.3] at least one config descriptor [9.6.3] */
8471da177e4SLinus Torvalds 	for (i = 0; i < udev->descriptor.bNumConfigurations; i++) {
8481da177e4SLinus Torvalds 		retval = usb_get_descriptor(udev, USB_DT_CONFIG, i,
8491da177e4SLinus Torvalds 				dev->buf, TBUF_SIZE);
85028ffd79cSDavid Brownell 		if (!is_good_config(dev, retval)) {
85128ffd79cSDavid Brownell 			dev_err(&iface->dev,
8521da177e4SLinus Torvalds 					"config [%d] descriptor --> %d\n",
8531da177e4SLinus Torvalds 					i, retval);
8541da177e4SLinus Torvalds 			return (retval < 0) ? retval : -EDOM;
8551da177e4SLinus Torvalds 		}
8561da177e4SLinus Torvalds 
857fabbf219SMartin Fuzzey 		/* FIXME cross-checking udev->config[i] to make sure usbcore
858fabbf219SMartin Fuzzey 		 * parsed it right (etc) would be good testing paranoia
859fabbf219SMartin Fuzzey 		 */
8601da177e4SLinus Torvalds 	}
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	/* and sometimes [9.2.6.6] speed dependent descriptors */
8631da177e4SLinus Torvalds 	if (le16_to_cpu(udev->descriptor.bcdUSB) == 0x0200) {
8641da177e4SLinus Torvalds 		struct usb_qualifier_descriptor *d = NULL;
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 		/* device qualifier [9.6.2] */
8671da177e4SLinus Torvalds 		retval = usb_get_descriptor(udev,
8681da177e4SLinus Torvalds 				USB_DT_DEVICE_QUALIFIER, 0, dev->buf,
8691da177e4SLinus Torvalds 				sizeof(struct usb_qualifier_descriptor));
8701da177e4SLinus Torvalds 		if (retval == -EPIPE) {
8711da177e4SLinus Torvalds 			if (udev->speed == USB_SPEED_HIGH) {
87228ffd79cSDavid Brownell 				dev_err(&iface->dev,
8731da177e4SLinus Torvalds 						"hs dev qualifier --> %d\n",
8741da177e4SLinus Torvalds 						retval);
8751da177e4SLinus Torvalds 				return (retval < 0) ? retval : -EDOM;
8761da177e4SLinus Torvalds 			}
8771da177e4SLinus Torvalds 			/* usb2.0 but not high-speed capable; fine */
8781da177e4SLinus Torvalds 		} else if (retval != sizeof(struct usb_qualifier_descriptor)) {
87928ffd79cSDavid Brownell 			dev_err(&iface->dev, "dev qualifier --> %d\n", retval);
8801da177e4SLinus Torvalds 			return (retval < 0) ? retval : -EDOM;
8811da177e4SLinus Torvalds 		} else
8821da177e4SLinus Torvalds 			d = (struct usb_qualifier_descriptor *) dev->buf;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 		/* might not have [9.6.2] any other-speed configs [9.6.4] */
8851da177e4SLinus Torvalds 		if (d) {
8861da177e4SLinus Torvalds 			unsigned max = d->bNumConfigurations;
8871da177e4SLinus Torvalds 			for (i = 0; i < max; i++) {
8881da177e4SLinus Torvalds 				retval = usb_get_descriptor(udev,
8891da177e4SLinus Torvalds 					USB_DT_OTHER_SPEED_CONFIG, i,
8901da177e4SLinus Torvalds 					dev->buf, TBUF_SIZE);
89128ffd79cSDavid Brownell 				if (!is_good_config(dev, retval)) {
89228ffd79cSDavid Brownell 					dev_err(&iface->dev,
8931da177e4SLinus Torvalds 						"other speed config --> %d\n",
8941da177e4SLinus Torvalds 						retval);
8951da177e4SLinus Torvalds 					return (retval < 0) ? retval : -EDOM;
8961da177e4SLinus Torvalds 				}
8971da177e4SLinus Torvalds 			}
8981da177e4SLinus Torvalds 		}
8991da177e4SLinus Torvalds 	}
900fabbf219SMartin Fuzzey 	/* FIXME fetch strings from at least the device descriptor */
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	/* [9.4.5] get_status always works */
9031da177e4SLinus Torvalds 	retval = usb_get_status(udev, USB_RECIP_DEVICE, 0, dev->buf);
90415b7336eSAlan Stern 	if (retval) {
90528ffd79cSDavid Brownell 		dev_err(&iface->dev, "get dev status --> %d\n", retval);
90615b7336eSAlan Stern 		return retval;
9071da177e4SLinus Torvalds 	}
9081da177e4SLinus Torvalds 
909fabbf219SMartin Fuzzey 	/* FIXME configuration.bmAttributes says if we could try to set/clear
910fabbf219SMartin Fuzzey 	 * the device's remote wakeup feature ... if we can, test that here
911fabbf219SMartin Fuzzey 	 */
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 	retval = usb_get_status(udev, USB_RECIP_INTERFACE,
9141da177e4SLinus Torvalds 			iface->altsetting[0].desc.bInterfaceNumber, dev->buf);
91515b7336eSAlan Stern 	if (retval) {
91628ffd79cSDavid Brownell 		dev_err(&iface->dev, "get interface status --> %d\n", retval);
91715b7336eSAlan Stern 		return retval;
9181da177e4SLinus Torvalds 	}
919fabbf219SMartin Fuzzey 	/* FIXME get status for each endpoint in the interface */
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds 	return 0;
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds /* use ch9 requests to test whether:
9271da177e4SLinus Torvalds  *   (a) queues work for control, keeping N subtests queued and
9281da177e4SLinus Torvalds  *       active (auto-resubmit) for M loops through the queue.
9291da177e4SLinus Torvalds  *   (b) protocol stalls (control-only) will autorecover.
9301da177e4SLinus Torvalds  *       it's not like bulk/intr; no halt clearing.
9311da177e4SLinus Torvalds  *   (c) short control reads are reported and handled.
9321da177e4SLinus Torvalds  *   (d) queues are always processed in-order
9331da177e4SLinus Torvalds  */
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds struct ctrl_ctx {
9361da177e4SLinus Torvalds 	spinlock_t		lock;
9371da177e4SLinus Torvalds 	struct usbtest_dev	*dev;
9381da177e4SLinus Torvalds 	struct completion	complete;
9391da177e4SLinus Torvalds 	unsigned		count;
9401da177e4SLinus Torvalds 	unsigned		pending;
9411da177e4SLinus Torvalds 	int			status;
9421da177e4SLinus Torvalds 	struct urb		**urb;
9431da177e4SLinus Torvalds 	struct usbtest_param	*param;
9441da177e4SLinus Torvalds 	int			last;
9451da177e4SLinus Torvalds };
9461da177e4SLinus Torvalds 
947c952a8baSHuang Rui #define NUM_SUBCASES	16		/* how many test subcases here? */
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds struct subcase {
9501da177e4SLinus Torvalds 	struct usb_ctrlrequest	setup;
9511da177e4SLinus Torvalds 	int			number;
9521da177e4SLinus Torvalds 	int			expected;
9531da177e4SLinus Torvalds };
9541da177e4SLinus Torvalds 
9557d12e780SDavid Howells static void ctrl_complete(struct urb *urb)
9561da177e4SLinus Torvalds {
9571da177e4SLinus Torvalds 	struct ctrl_ctx		*ctx = urb->context;
9581da177e4SLinus Torvalds 	struct usb_ctrlrequest	*reqp;
9591da177e4SLinus Torvalds 	struct subcase		*subcase;
9601da177e4SLinus Torvalds 	int			status = urb->status;
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	reqp = (struct usb_ctrlrequest *)urb->setup_packet;
9631da177e4SLinus Torvalds 	subcase = container_of(reqp, struct subcase, setup);
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	spin_lock(&ctx->lock);
9661da177e4SLinus Torvalds 	ctx->count--;
9671da177e4SLinus Torvalds 	ctx->pending--;
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	/* queue must transfer and complete in fifo order, unless
9701da177e4SLinus Torvalds 	 * usb_unlink_urb() is used to unlink something not at the
9711da177e4SLinus Torvalds 	 * physical queue head (not tested).
9721da177e4SLinus Torvalds 	 */
9731da177e4SLinus Torvalds 	if (subcase->number > 0) {
9741da177e4SLinus Torvalds 		if ((subcase->number - ctx->last) != 1) {
97528ffd79cSDavid Brownell 			ERROR(ctx->dev,
97628ffd79cSDavid Brownell 				"subcase %d completed out of order, last %d\n",
9771da177e4SLinus Torvalds 				subcase->number, ctx->last);
9781da177e4SLinus Torvalds 			status = -EDOM;
9791da177e4SLinus Torvalds 			ctx->last = subcase->number;
9801da177e4SLinus Torvalds 			goto error;
9811da177e4SLinus Torvalds 		}
9821da177e4SLinus Torvalds 	}
9831da177e4SLinus Torvalds 	ctx->last = subcase->number;
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 	/* succeed or fault in only one way? */
9861da177e4SLinus Torvalds 	if (status == subcase->expected)
9871da177e4SLinus Torvalds 		status = 0;
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	/* async unlink for cleanup? */
9901da177e4SLinus Torvalds 	else if (status != -ECONNRESET) {
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 		/* some faults are allowed, not required */
9931da177e4SLinus Torvalds 		if (subcase->expected > 0 && (
99459d99785SGreg Kroah-Hartman 			  ((status == -subcase->expected	/* happened */
99559d99785SGreg Kroah-Hartman 			   || status == 0))))			/* didn't */
9961da177e4SLinus Torvalds 			status = 0;
9971da177e4SLinus Torvalds 		/* sometimes more than one fault is allowed */
9981da177e4SLinus Torvalds 		else if (subcase->number == 12 && status == -EPIPE)
9991da177e4SLinus Torvalds 			status = 0;
10001da177e4SLinus Torvalds 		else
100128ffd79cSDavid Brownell 			ERROR(ctx->dev, "subtest %d error, status %d\n",
10021da177e4SLinus Torvalds 					subcase->number, status);
10031da177e4SLinus Torvalds 	}
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	/* unexpected status codes mean errors; ideally, in hardware */
10061da177e4SLinus Torvalds 	if (status) {
10071da177e4SLinus Torvalds error:
10081da177e4SLinus Torvalds 		if (ctx->status == 0) {
10091da177e4SLinus Torvalds 			int		i;
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 			ctx->status = status;
101228ffd79cSDavid Brownell 			ERROR(ctx->dev, "control queue %02x.%02x, err %d, "
101328ffd79cSDavid Brownell 					"%d left, subcase %d, len %d/%d\n",
10141da177e4SLinus Torvalds 					reqp->bRequestType, reqp->bRequest,
101528ffd79cSDavid Brownell 					status, ctx->count, subcase->number,
101628ffd79cSDavid Brownell 					urb->actual_length,
101728ffd79cSDavid Brownell 					urb->transfer_buffer_length);
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 			/* FIXME this "unlink everything" exit route should
10201da177e4SLinus Torvalds 			 * be a separate test case.
10211da177e4SLinus Torvalds 			 */
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 			/* unlink whatever's still pending */
10241da177e4SLinus Torvalds 			for (i = 1; i < ctx->param->sglen; i++) {
10251da177e4SLinus Torvalds 				struct urb *u = ctx->urb[
102628ffd79cSDavid Brownell 							(i + subcase->number)
102728ffd79cSDavid Brownell 							% ctx->param->sglen];
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 				if (u == urb || !u->dev)
10301da177e4SLinus Torvalds 					continue;
1031caa2a122SFranck Bui-Huu 				spin_unlock(&ctx->lock);
10321da177e4SLinus Torvalds 				status = usb_unlink_urb(u);
1033caa2a122SFranck Bui-Huu 				spin_lock(&ctx->lock);
10341da177e4SLinus Torvalds 				switch (status) {
10351da177e4SLinus Torvalds 				case -EINPROGRESS:
10361da177e4SLinus Torvalds 				case -EBUSY:
10371da177e4SLinus Torvalds 				case -EIDRM:
10381da177e4SLinus Torvalds 					continue;
10391da177e4SLinus Torvalds 				default:
104028ffd79cSDavid Brownell 					ERROR(ctx->dev, "urb unlink --> %d\n",
104128ffd79cSDavid Brownell 							status);
10421da177e4SLinus Torvalds 				}
10431da177e4SLinus Torvalds 			}
10441da177e4SLinus Torvalds 			status = ctx->status;
10451da177e4SLinus Torvalds 		}
10461da177e4SLinus Torvalds 	}
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 	/* resubmit if we need to, else mark this as done */
10491da177e4SLinus Torvalds 	if ((status == 0) && (ctx->pending < ctx->count)) {
1050fabbf219SMartin Fuzzey 		status = usb_submit_urb(urb, GFP_ATOMIC);
1051fabbf219SMartin Fuzzey 		if (status != 0) {
105228ffd79cSDavid Brownell 			ERROR(ctx->dev,
105328ffd79cSDavid Brownell 				"can't resubmit ctrl %02x.%02x, err %d\n",
10541da177e4SLinus Torvalds 				reqp->bRequestType, reqp->bRequest, status);
10551da177e4SLinus Torvalds 			urb->dev = NULL;
10561da177e4SLinus Torvalds 		} else
10571da177e4SLinus Torvalds 			ctx->pending++;
10581da177e4SLinus Torvalds 	} else
10591da177e4SLinus Torvalds 		urb->dev = NULL;
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds 	/* signal completion when nothing's queued */
10621da177e4SLinus Torvalds 	if (ctx->pending == 0)
10631da177e4SLinus Torvalds 		complete(&ctx->complete);
10641da177e4SLinus Torvalds 	spin_unlock(&ctx->lock);
10651da177e4SLinus Torvalds }
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds static int
10681da177e4SLinus Torvalds test_ctrl_queue(struct usbtest_dev *dev, struct usbtest_param *param)
10691da177e4SLinus Torvalds {
10701da177e4SLinus Torvalds 	struct usb_device	*udev = testdev_to_usbdev(dev);
10711da177e4SLinus Torvalds 	struct urb		**urb;
10721da177e4SLinus Torvalds 	struct ctrl_ctx		context;
10731da177e4SLinus Torvalds 	int			i;
10741da177e4SLinus Torvalds 
1075e65cdfaeSXi Wang 	if (param->sglen == 0 || param->iterations > UINT_MAX / param->sglen)
1076e65cdfaeSXi Wang 		return -EOPNOTSUPP;
1077e65cdfaeSXi Wang 
10781da177e4SLinus Torvalds 	spin_lock_init(&context.lock);
10791da177e4SLinus Torvalds 	context.dev = dev;
10801da177e4SLinus Torvalds 	init_completion(&context.complete);
10811da177e4SLinus Torvalds 	context.count = param->sglen * param->iterations;
10821da177e4SLinus Torvalds 	context.pending = 0;
10831da177e4SLinus Torvalds 	context.status = -ENOMEM;
10841da177e4SLinus Torvalds 	context.param = param;
10851da177e4SLinus Torvalds 	context.last = -1;
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 	/* allocate and init the urbs we'll queue.
10881da177e4SLinus Torvalds 	 * as with bulk/intr sglists, sglen is the queue depth; it also
10891da177e4SLinus Torvalds 	 * controls which subtests run (more tests than sglen) or rerun.
10901da177e4SLinus Torvalds 	 */
1091e94b1766SChristoph Lameter 	urb = kcalloc(param->sglen, sizeof(struct urb *), GFP_KERNEL);
10921da177e4SLinus Torvalds 	if (!urb)
10931da177e4SLinus Torvalds 		return -ENOMEM;
10941da177e4SLinus Torvalds 	for (i = 0; i < param->sglen; i++) {
10951da177e4SLinus Torvalds 		int			pipe = usb_rcvctrlpipe(udev, 0);
10961da177e4SLinus Torvalds 		unsigned		len;
10971da177e4SLinus Torvalds 		struct urb		*u;
10981da177e4SLinus Torvalds 		struct usb_ctrlrequest	req;
10991da177e4SLinus Torvalds 		struct subcase		*reqp;
11006def7553SMarcin Slusarz 
11016def7553SMarcin Slusarz 		/* sign of this variable means:
11026def7553SMarcin Slusarz 		 *  -: tested code must return this (negative) error code
11036def7553SMarcin Slusarz 		 *  +: tested code may return this (negative too) error code
11046def7553SMarcin Slusarz 		 */
11051da177e4SLinus Torvalds 		int			expected = 0;
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 		/* requests here are mostly expected to succeed on any
11081da177e4SLinus Torvalds 		 * device, but some are chosen to trigger protocol stalls
11091da177e4SLinus Torvalds 		 * or short reads.
11101da177e4SLinus Torvalds 		 */
1111f55055b4SHuang Rui 		memset(&req, 0, sizeof(req));
11121da177e4SLinus Torvalds 		req.bRequest = USB_REQ_GET_DESCRIPTOR;
11131da177e4SLinus Torvalds 		req.bRequestType = USB_DIR_IN|USB_RECIP_DEVICE;
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 		switch (i % NUM_SUBCASES) {
1116fabbf219SMartin Fuzzey 		case 0:		/* get device descriptor */
11171da177e4SLinus Torvalds 			req.wValue = cpu_to_le16(USB_DT_DEVICE << 8);
11181da177e4SLinus Torvalds 			len = sizeof(struct usb_device_descriptor);
11191da177e4SLinus Torvalds 			break;
1120fabbf219SMartin Fuzzey 		case 1:		/* get first config descriptor (only) */
11211da177e4SLinus Torvalds 			req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
11221da177e4SLinus Torvalds 			len = sizeof(struct usb_config_descriptor);
11231da177e4SLinus Torvalds 			break;
1124fabbf219SMartin Fuzzey 		case 2:		/* get altsetting (OFTEN STALLS) */
11251da177e4SLinus Torvalds 			req.bRequest = USB_REQ_GET_INTERFACE;
11261da177e4SLinus Torvalds 			req.bRequestType = USB_DIR_IN|USB_RECIP_INTERFACE;
1127fabbf219SMartin Fuzzey 			/* index = 0 means first interface */
11281da177e4SLinus Torvalds 			len = 1;
11291da177e4SLinus Torvalds 			expected = EPIPE;
11301da177e4SLinus Torvalds 			break;
1131fabbf219SMartin Fuzzey 		case 3:		/* get interface status */
11321da177e4SLinus Torvalds 			req.bRequest = USB_REQ_GET_STATUS;
11331da177e4SLinus Torvalds 			req.bRequestType = USB_DIR_IN|USB_RECIP_INTERFACE;
1134fabbf219SMartin Fuzzey 			/* interface 0 */
11351da177e4SLinus Torvalds 			len = 2;
11361da177e4SLinus Torvalds 			break;
1137fabbf219SMartin Fuzzey 		case 4:		/* get device status */
11381da177e4SLinus Torvalds 			req.bRequest = USB_REQ_GET_STATUS;
11391da177e4SLinus Torvalds 			req.bRequestType = USB_DIR_IN|USB_RECIP_DEVICE;
11401da177e4SLinus Torvalds 			len = 2;
11411da177e4SLinus Torvalds 			break;
1142fabbf219SMartin Fuzzey 		case 5:		/* get device qualifier (MAY STALL) */
11431da177e4SLinus Torvalds 			req.wValue = cpu_to_le16 (USB_DT_DEVICE_QUALIFIER << 8);
11441da177e4SLinus Torvalds 			len = sizeof(struct usb_qualifier_descriptor);
11451da177e4SLinus Torvalds 			if (udev->speed != USB_SPEED_HIGH)
11461da177e4SLinus Torvalds 				expected = EPIPE;
11471da177e4SLinus Torvalds 			break;
1148fabbf219SMartin Fuzzey 		case 6:		/* get first config descriptor, plus interface */
11491da177e4SLinus Torvalds 			req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
11501da177e4SLinus Torvalds 			len = sizeof(struct usb_config_descriptor);
11511da177e4SLinus Torvalds 			len += sizeof(struct usb_interface_descriptor);
11521da177e4SLinus Torvalds 			break;
1153fabbf219SMartin Fuzzey 		case 7:		/* get interface descriptor (ALWAYS STALLS) */
11541da177e4SLinus Torvalds 			req.wValue = cpu_to_le16 (USB_DT_INTERFACE << 8);
1155fabbf219SMartin Fuzzey 			/* interface == 0 */
11561da177e4SLinus Torvalds 			len = sizeof(struct usb_interface_descriptor);
115728ffd79cSDavid Brownell 			expected = -EPIPE;
11581da177e4SLinus Torvalds 			break;
1159fabbf219SMartin Fuzzey 		/* NOTE: two consecutive stalls in the queue here.
1160fabbf219SMartin Fuzzey 		 *  that tests fault recovery a bit more aggressively. */
1161fabbf219SMartin Fuzzey 		case 8:		/* clear endpoint halt (MAY STALL) */
11621da177e4SLinus Torvalds 			req.bRequest = USB_REQ_CLEAR_FEATURE;
11631da177e4SLinus Torvalds 			req.bRequestType = USB_RECIP_ENDPOINT;
1164fabbf219SMartin Fuzzey 			/* wValue 0 == ep halt */
1165fabbf219SMartin Fuzzey 			/* wIndex 0 == ep0 (shouldn't halt!) */
11661da177e4SLinus Torvalds 			len = 0;
11671da177e4SLinus Torvalds 			pipe = usb_sndctrlpipe(udev, 0);
11681da177e4SLinus Torvalds 			expected = EPIPE;
11691da177e4SLinus Torvalds 			break;
1170fabbf219SMartin Fuzzey 		case 9:		/* get endpoint status */
11711da177e4SLinus Torvalds 			req.bRequest = USB_REQ_GET_STATUS;
11721da177e4SLinus Torvalds 			req.bRequestType = USB_DIR_IN|USB_RECIP_ENDPOINT;
1173fabbf219SMartin Fuzzey 			/* endpoint 0 */
11741da177e4SLinus Torvalds 			len = 2;
11751da177e4SLinus Torvalds 			break;
1176fabbf219SMartin Fuzzey 		case 10:	/* trigger short read (EREMOTEIO) */
11771da177e4SLinus Torvalds 			req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
11781da177e4SLinus Torvalds 			len = 1024;
11791da177e4SLinus Torvalds 			expected = -EREMOTEIO;
11801da177e4SLinus Torvalds 			break;
1181fabbf219SMartin Fuzzey 		/* NOTE: two consecutive _different_ faults in the queue. */
1182fabbf219SMartin Fuzzey 		case 11:	/* get endpoint descriptor (ALWAYS STALLS) */
11831da177e4SLinus Torvalds 			req.wValue = cpu_to_le16(USB_DT_ENDPOINT << 8);
1184fabbf219SMartin Fuzzey 			/* endpoint == 0 */
11851da177e4SLinus Torvalds 			len = sizeof(struct usb_interface_descriptor);
11861da177e4SLinus Torvalds 			expected = EPIPE;
11871da177e4SLinus Torvalds 			break;
1188fabbf219SMartin Fuzzey 		/* NOTE: sometimes even a third fault in the queue! */
1189fabbf219SMartin Fuzzey 		case 12:	/* get string 0 descriptor (MAY STALL) */
11901da177e4SLinus Torvalds 			req.wValue = cpu_to_le16(USB_DT_STRING << 8);
1191fabbf219SMartin Fuzzey 			/* string == 0, for language IDs */
11921da177e4SLinus Torvalds 			len = sizeof(struct usb_interface_descriptor);
1193fabbf219SMartin Fuzzey 			/* may succeed when > 4 languages */
1194fabbf219SMartin Fuzzey 			expected = EREMOTEIO;	/* or EPIPE, if no strings */
11951da177e4SLinus Torvalds 			break;
1196fabbf219SMartin Fuzzey 		case 13:	/* short read, resembling case 10 */
11971da177e4SLinus Torvalds 			req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
1198fabbf219SMartin Fuzzey 			/* last data packet "should" be DATA1, not DATA0 */
11996a23ccd2SPaul Zimmerman 			if (udev->speed == USB_SPEED_SUPER)
12006a23ccd2SPaul Zimmerman 				len = 1024 - 512;
12016a23ccd2SPaul Zimmerman 			else
12021da177e4SLinus Torvalds 				len = 1024 - udev->descriptor.bMaxPacketSize0;
12031da177e4SLinus Torvalds 			expected = -EREMOTEIO;
12041da177e4SLinus Torvalds 			break;
1205fabbf219SMartin Fuzzey 		case 14:	/* short read; try to fill the last packet */
12061da177e4SLinus Torvalds 			req.wValue = cpu_to_le16((USB_DT_DEVICE << 8) | 0);
120728ffd79cSDavid Brownell 			/* device descriptor size == 18 bytes */
12081da177e4SLinus Torvalds 			len = udev->descriptor.bMaxPacketSize0;
120967e7d64bSSebastian Andrzej Siewior 			if (udev->speed == USB_SPEED_SUPER)
121067e7d64bSSebastian Andrzej Siewior 				len = 512;
12111da177e4SLinus Torvalds 			switch (len) {
1212fabbf219SMartin Fuzzey 			case 8:
1213fabbf219SMartin Fuzzey 				len = 24;
1214fabbf219SMartin Fuzzey 				break;
1215fabbf219SMartin Fuzzey 			case 16:
1216fabbf219SMartin Fuzzey 				len = 32;
1217fabbf219SMartin Fuzzey 				break;
12181da177e4SLinus Torvalds 			}
12191da177e4SLinus Torvalds 			expected = -EREMOTEIO;
12201da177e4SLinus Torvalds 			break;
1221c952a8baSHuang Rui 		case 15:
1222c952a8baSHuang Rui 			req.wValue = cpu_to_le16(USB_DT_BOS << 8);
1223c952a8baSHuang Rui 			if (udev->bos)
1224c952a8baSHuang Rui 				len = le16_to_cpu(udev->bos->desc->wTotalLength);
1225c952a8baSHuang Rui 			else
1226c952a8baSHuang Rui 				len = sizeof(struct usb_bos_descriptor);
1227*8cf43285SSarah Sharp 			if (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0201)
1228c952a8baSHuang Rui 				expected = -EPIPE;
1229c952a8baSHuang Rui 			break;
12301da177e4SLinus Torvalds 		default:
123128ffd79cSDavid Brownell 			ERROR(dev, "bogus number of ctrl queue testcases!\n");
12321da177e4SLinus Torvalds 			context.status = -EINVAL;
12331da177e4SLinus Torvalds 			goto cleanup;
12341da177e4SLinus Torvalds 		}
12351da177e4SLinus Torvalds 		req.wLength = cpu_to_le16(len);
12361da177e4SLinus Torvalds 		urb[i] = u = simple_alloc_urb(udev, pipe, len);
12371da177e4SLinus Torvalds 		if (!u)
12381da177e4SLinus Torvalds 			goto cleanup;
12391da177e4SLinus Torvalds 
1240f55055b4SHuang Rui 		reqp = kmalloc(sizeof(*reqp), GFP_KERNEL);
12411da177e4SLinus Torvalds 		if (!reqp)
12421da177e4SLinus Torvalds 			goto cleanup;
12431da177e4SLinus Torvalds 		reqp->setup = req;
12441da177e4SLinus Torvalds 		reqp->number = i % NUM_SUBCASES;
12451da177e4SLinus Torvalds 		reqp->expected = expected;
12461da177e4SLinus Torvalds 		u->setup_packet = (char *) &reqp->setup;
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds 		u->context = &context;
12491da177e4SLinus Torvalds 		u->complete = ctrl_complete;
12501da177e4SLinus Torvalds 	}
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 	/* queue the urbs */
12531da177e4SLinus Torvalds 	context.urb = urb;
12541da177e4SLinus Torvalds 	spin_lock_irq(&context.lock);
12551da177e4SLinus Torvalds 	for (i = 0; i < param->sglen; i++) {
125654e6ecb2SChristoph Lameter 		context.status = usb_submit_urb(urb[i], GFP_ATOMIC);
12571da177e4SLinus Torvalds 		if (context.status != 0) {
125828ffd79cSDavid Brownell 			ERROR(dev, "can't submit urb[%d], status %d\n",
12591da177e4SLinus Torvalds 					i, context.status);
12601da177e4SLinus Torvalds 			context.count = context.pending;
12611da177e4SLinus Torvalds 			break;
12621da177e4SLinus Torvalds 		}
12631da177e4SLinus Torvalds 		context.pending++;
12641da177e4SLinus Torvalds 	}
12651da177e4SLinus Torvalds 	spin_unlock_irq(&context.lock);
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 	/* FIXME  set timer and time out; provide a disconnect hook */
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 	/* wait for the last one to complete */
12701da177e4SLinus Torvalds 	if (context.pending > 0)
12711da177e4SLinus Torvalds 		wait_for_completion(&context.complete);
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds cleanup:
12741da177e4SLinus Torvalds 	for (i = 0; i < param->sglen; i++) {
12751da177e4SLinus Torvalds 		if (!urb[i])
12761da177e4SLinus Torvalds 			continue;
12771da177e4SLinus Torvalds 		urb[i]->dev = udev;
12780ede76fcSAlan Stern 		kfree(urb[i]->setup_packet);
12791da177e4SLinus Torvalds 		simple_free_urb(urb[i]);
12801da177e4SLinus Torvalds 	}
12811da177e4SLinus Torvalds 	kfree(urb);
12821da177e4SLinus Torvalds 	return context.status;
12831da177e4SLinus Torvalds }
12841da177e4SLinus Torvalds #undef NUM_SUBCASES
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
12881da177e4SLinus Torvalds 
12897d12e780SDavid Howells static void unlink1_callback(struct urb *urb)
12901da177e4SLinus Torvalds {
12911da177e4SLinus Torvalds 	int	status = urb->status;
12921da177e4SLinus Torvalds 
1293fabbf219SMartin Fuzzey 	/* we "know" -EPIPE (stall) never happens */
12941da177e4SLinus Torvalds 	if (!status)
129554e6ecb2SChristoph Lameter 		status = usb_submit_urb(urb, GFP_ATOMIC);
12961da177e4SLinus Torvalds 	if (status) {
12971da177e4SLinus Torvalds 		urb->status = status;
1298cdc97792SMing Lei 		complete(urb->context);
12991da177e4SLinus Torvalds 	}
13001da177e4SLinus Torvalds }
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds static int unlink1(struct usbtest_dev *dev, int pipe, int size, int async)
13031da177e4SLinus Torvalds {
13041da177e4SLinus Torvalds 	struct urb		*urb;
13051da177e4SLinus Torvalds 	struct completion	completion;
13061da177e4SLinus Torvalds 	int			retval = 0;
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	init_completion(&completion);
13091da177e4SLinus Torvalds 	urb = simple_alloc_urb(testdev_to_usbdev(dev), pipe, size);
13101da177e4SLinus Torvalds 	if (!urb)
13111da177e4SLinus Torvalds 		return -ENOMEM;
13121da177e4SLinus Torvalds 	urb->context = &completion;
13131da177e4SLinus Torvalds 	urb->complete = unlink1_callback;
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 	/* keep the endpoint busy.  there are lots of hc/hcd-internal
13161da177e4SLinus Torvalds 	 * states, and testing should get to all of them over time.
13171da177e4SLinus Torvalds 	 *
13181da177e4SLinus Torvalds 	 * FIXME want additional tests for when endpoint is STALLing
13191da177e4SLinus Torvalds 	 * due to errors, or is just NAKing requests.
13201da177e4SLinus Torvalds 	 */
1321fabbf219SMartin Fuzzey 	retval = usb_submit_urb(urb, GFP_KERNEL);
1322fabbf219SMartin Fuzzey 	if (retval != 0) {
132328ffd79cSDavid Brownell 		dev_err(&dev->intf->dev, "submit fail %d\n", retval);
13241da177e4SLinus Torvalds 		return retval;
13251da177e4SLinus Torvalds 	}
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 	/* unlinking that should always work.  variable delay tests more
13281da177e4SLinus Torvalds 	 * hcd states and code paths, even with little other system load.
13291da177e4SLinus Torvalds 	 */
13301da177e4SLinus Torvalds 	msleep(jiffies % (2 * INTERRUPT_RATE));
13311da177e4SLinus Torvalds 	if (async) {
13323b6c023fSMartin Fuzzey 		while (!completion_done(&completion)) {
13331da177e4SLinus Torvalds 			retval = usb_unlink_urb(urb);
13343b6c023fSMartin Fuzzey 
13353b6c023fSMartin Fuzzey 			switch (retval) {
13363b6c023fSMartin Fuzzey 			case -EBUSY:
13373b6c023fSMartin Fuzzey 			case -EIDRM:
13383b6c023fSMartin Fuzzey 				/* we can't unlink urbs while they're completing
13393b6c023fSMartin Fuzzey 				 * or if they've completed, and we haven't
13403b6c023fSMartin Fuzzey 				 * resubmitted. "normal" drivers would prevent
13413b6c023fSMartin Fuzzey 				 * resubmission, but since we're testing unlink
13423b6c023fSMartin Fuzzey 				 * paths, we can't.
13431da177e4SLinus Torvalds 				 */
134428ffd79cSDavid Brownell 				ERROR(dev, "unlink retry\n");
13453b6c023fSMartin Fuzzey 				continue;
13463b6c023fSMartin Fuzzey 			case 0:
13473b6c023fSMartin Fuzzey 			case -EINPROGRESS:
13483b6c023fSMartin Fuzzey 				break;
13493b6c023fSMartin Fuzzey 
13503b6c023fSMartin Fuzzey 			default:
13513b6c023fSMartin Fuzzey 				dev_err(&dev->intf->dev,
13523b6c023fSMartin Fuzzey 					"unlink fail %d\n", retval);
13533b6c023fSMartin Fuzzey 				return retval;
13543b6c023fSMartin Fuzzey 			}
13553b6c023fSMartin Fuzzey 
13563b6c023fSMartin Fuzzey 			break;
13571da177e4SLinus Torvalds 		}
13581da177e4SLinus Torvalds 	} else
13591da177e4SLinus Torvalds 		usb_kill_urb(urb);
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds 	wait_for_completion(&completion);
13621da177e4SLinus Torvalds 	retval = urb->status;
13631da177e4SLinus Torvalds 	simple_free_urb(urb);
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds 	if (async)
13661da177e4SLinus Torvalds 		return (retval == -ECONNRESET) ? 0 : retval - 1000;
13671da177e4SLinus Torvalds 	else
13681da177e4SLinus Torvalds 		return (retval == -ENOENT || retval == -EPERM) ?
13691da177e4SLinus Torvalds 				0 : retval - 2000;
13701da177e4SLinus Torvalds }
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds static int unlink_simple(struct usbtest_dev *dev, int pipe, int len)
13731da177e4SLinus Torvalds {
13741da177e4SLinus Torvalds 	int			retval = 0;
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 	/* test sync and async paths */
13771da177e4SLinus Torvalds 	retval = unlink1(dev, pipe, len, 1);
13781da177e4SLinus Torvalds 	if (!retval)
13791da177e4SLinus Torvalds 		retval = unlink1(dev, pipe, len, 0);
13801da177e4SLinus Torvalds 	return retval;
13811da177e4SLinus Torvalds }
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
13841da177e4SLinus Torvalds 
1385869410f8SAlan Stern struct queued_ctx {
1386869410f8SAlan Stern 	struct completion	complete;
1387869410f8SAlan Stern 	atomic_t		pending;
1388869410f8SAlan Stern 	unsigned		num;
1389869410f8SAlan Stern 	int			status;
1390869410f8SAlan Stern 	struct urb		**urbs;
1391869410f8SAlan Stern };
1392869410f8SAlan Stern 
1393869410f8SAlan Stern static void unlink_queued_callback(struct urb *urb)
1394869410f8SAlan Stern {
1395869410f8SAlan Stern 	int			status = urb->status;
1396869410f8SAlan Stern 	struct queued_ctx	*ctx = urb->context;
1397869410f8SAlan Stern 
1398869410f8SAlan Stern 	if (ctx->status)
1399869410f8SAlan Stern 		goto done;
1400869410f8SAlan Stern 	if (urb == ctx->urbs[ctx->num - 4] || urb == ctx->urbs[ctx->num - 2]) {
1401869410f8SAlan Stern 		if (status == -ECONNRESET)
1402869410f8SAlan Stern 			goto done;
1403869410f8SAlan Stern 		/* What error should we report if the URB completed normally? */
1404869410f8SAlan Stern 	}
1405869410f8SAlan Stern 	if (status != 0)
1406869410f8SAlan Stern 		ctx->status = status;
1407869410f8SAlan Stern 
1408869410f8SAlan Stern  done:
1409869410f8SAlan Stern 	if (atomic_dec_and_test(&ctx->pending))
1410869410f8SAlan Stern 		complete(&ctx->complete);
1411869410f8SAlan Stern }
1412869410f8SAlan Stern 
1413869410f8SAlan Stern static int unlink_queued(struct usbtest_dev *dev, int pipe, unsigned num,
1414869410f8SAlan Stern 		unsigned size)
1415869410f8SAlan Stern {
1416869410f8SAlan Stern 	struct queued_ctx	ctx;
1417869410f8SAlan Stern 	struct usb_device	*udev = testdev_to_usbdev(dev);
1418869410f8SAlan Stern 	void			*buf;
1419869410f8SAlan Stern 	dma_addr_t		buf_dma;
1420869410f8SAlan Stern 	int			i;
1421869410f8SAlan Stern 	int			retval = -ENOMEM;
1422869410f8SAlan Stern 
1423869410f8SAlan Stern 	init_completion(&ctx.complete);
1424869410f8SAlan Stern 	atomic_set(&ctx.pending, 1);	/* One more than the actual value */
1425869410f8SAlan Stern 	ctx.num = num;
1426869410f8SAlan Stern 	ctx.status = 0;
1427869410f8SAlan Stern 
1428869410f8SAlan Stern 	buf = usb_alloc_coherent(udev, size, GFP_KERNEL, &buf_dma);
1429869410f8SAlan Stern 	if (!buf)
1430869410f8SAlan Stern 		return retval;
1431869410f8SAlan Stern 	memset(buf, 0, size);
1432869410f8SAlan Stern 
1433869410f8SAlan Stern 	/* Allocate and init the urbs we'll queue */
1434869410f8SAlan Stern 	ctx.urbs = kcalloc(num, sizeof(struct urb *), GFP_KERNEL);
1435869410f8SAlan Stern 	if (!ctx.urbs)
1436869410f8SAlan Stern 		goto free_buf;
1437869410f8SAlan Stern 	for (i = 0; i < num; i++) {
1438869410f8SAlan Stern 		ctx.urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1439869410f8SAlan Stern 		if (!ctx.urbs[i])
1440869410f8SAlan Stern 			goto free_urbs;
1441869410f8SAlan Stern 		usb_fill_bulk_urb(ctx.urbs[i], udev, pipe, buf, size,
1442869410f8SAlan Stern 				unlink_queued_callback, &ctx);
1443869410f8SAlan Stern 		ctx.urbs[i]->transfer_dma = buf_dma;
1444869410f8SAlan Stern 		ctx.urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1445869410f8SAlan Stern 	}
1446869410f8SAlan Stern 
1447869410f8SAlan Stern 	/* Submit all the URBs and then unlink URBs num - 4 and num - 2. */
1448869410f8SAlan Stern 	for (i = 0; i < num; i++) {
1449869410f8SAlan Stern 		atomic_inc(&ctx.pending);
1450869410f8SAlan Stern 		retval = usb_submit_urb(ctx.urbs[i], GFP_KERNEL);
1451869410f8SAlan Stern 		if (retval != 0) {
1452869410f8SAlan Stern 			dev_err(&dev->intf->dev, "submit urbs[%d] fail %d\n",
1453869410f8SAlan Stern 					i, retval);
1454869410f8SAlan Stern 			atomic_dec(&ctx.pending);
1455869410f8SAlan Stern 			ctx.status = retval;
1456869410f8SAlan Stern 			break;
1457869410f8SAlan Stern 		}
1458869410f8SAlan Stern 	}
1459869410f8SAlan Stern 	if (i == num) {
1460869410f8SAlan Stern 		usb_unlink_urb(ctx.urbs[num - 4]);
1461869410f8SAlan Stern 		usb_unlink_urb(ctx.urbs[num - 2]);
1462869410f8SAlan Stern 	} else {
1463869410f8SAlan Stern 		while (--i >= 0)
1464869410f8SAlan Stern 			usb_unlink_urb(ctx.urbs[i]);
1465869410f8SAlan Stern 	}
1466869410f8SAlan Stern 
1467869410f8SAlan Stern 	if (atomic_dec_and_test(&ctx.pending))		/* The extra count */
1468869410f8SAlan Stern 		complete(&ctx.complete);
1469869410f8SAlan Stern 	wait_for_completion(&ctx.complete);
1470869410f8SAlan Stern 	retval = ctx.status;
1471869410f8SAlan Stern 
1472869410f8SAlan Stern  free_urbs:
1473869410f8SAlan Stern 	for (i = 0; i < num; i++)
1474869410f8SAlan Stern 		usb_free_urb(ctx.urbs[i]);
1475869410f8SAlan Stern 	kfree(ctx.urbs);
1476869410f8SAlan Stern  free_buf:
1477869410f8SAlan Stern 	usb_free_coherent(udev, size, buf, buf_dma);
1478869410f8SAlan Stern 	return retval;
1479869410f8SAlan Stern }
1480869410f8SAlan Stern 
1481869410f8SAlan Stern /*-------------------------------------------------------------------------*/
1482869410f8SAlan Stern 
148328ffd79cSDavid Brownell static int verify_not_halted(struct usbtest_dev *tdev, int ep, struct urb *urb)
14841da177e4SLinus Torvalds {
14851da177e4SLinus Torvalds 	int	retval;
14861da177e4SLinus Torvalds 	u16	status;
14871da177e4SLinus Torvalds 
14881da177e4SLinus Torvalds 	/* shouldn't look or act halted */
14891da177e4SLinus Torvalds 	retval = usb_get_status(urb->dev, USB_RECIP_ENDPOINT, ep, &status);
14901da177e4SLinus Torvalds 	if (retval < 0) {
149128ffd79cSDavid Brownell 		ERROR(tdev, "ep %02x couldn't get no-halt status, %d\n",
149228ffd79cSDavid Brownell 				ep, retval);
14931da177e4SLinus Torvalds 		return retval;
14941da177e4SLinus Torvalds 	}
14951da177e4SLinus Torvalds 	if (status != 0) {
149628ffd79cSDavid Brownell 		ERROR(tdev, "ep %02x bogus status: %04x != 0\n", ep, status);
14971da177e4SLinus Torvalds 		return -EINVAL;
14981da177e4SLinus Torvalds 	}
149928ffd79cSDavid Brownell 	retval = simple_io(tdev, urb, 1, 0, 0, __func__);
15001da177e4SLinus Torvalds 	if (retval != 0)
15011da177e4SLinus Torvalds 		return -EINVAL;
15021da177e4SLinus Torvalds 	return 0;
15031da177e4SLinus Torvalds }
15041da177e4SLinus Torvalds 
150528ffd79cSDavid Brownell static int verify_halted(struct usbtest_dev *tdev, int ep, struct urb *urb)
15061da177e4SLinus Torvalds {
15071da177e4SLinus Torvalds 	int	retval;
15081da177e4SLinus Torvalds 	u16	status;
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds 	/* should look and act halted */
15111da177e4SLinus Torvalds 	retval = usb_get_status(urb->dev, USB_RECIP_ENDPOINT, ep, &status);
15121da177e4SLinus Torvalds 	if (retval < 0) {
151328ffd79cSDavid Brownell 		ERROR(tdev, "ep %02x couldn't get halt status, %d\n",
151428ffd79cSDavid Brownell 				ep, retval);
15151da177e4SLinus Torvalds 		return retval;
15161da177e4SLinus Torvalds 	}
15171da177e4SLinus Torvalds 	if (status != 1) {
151828ffd79cSDavid Brownell 		ERROR(tdev, "ep %02x bogus status: %04x != 1\n", ep, status);
15191da177e4SLinus Torvalds 		return -EINVAL;
15201da177e4SLinus Torvalds 	}
152128ffd79cSDavid Brownell 	retval = simple_io(tdev, urb, 1, 0, -EPIPE, __func__);
15221da177e4SLinus Torvalds 	if (retval != -EPIPE)
15231da177e4SLinus Torvalds 		return -EINVAL;
152428ffd79cSDavid Brownell 	retval = simple_io(tdev, urb, 1, 0, -EPIPE, "verify_still_halted");
15251da177e4SLinus Torvalds 	if (retval != -EPIPE)
15261da177e4SLinus Torvalds 		return -EINVAL;
15271da177e4SLinus Torvalds 	return 0;
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds 
153028ffd79cSDavid Brownell static int test_halt(struct usbtest_dev *tdev, int ep, struct urb *urb)
15311da177e4SLinus Torvalds {
15321da177e4SLinus Torvalds 	int	retval;
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds 	/* shouldn't look or act halted now */
153528ffd79cSDavid Brownell 	retval = verify_not_halted(tdev, ep, urb);
15361da177e4SLinus Torvalds 	if (retval < 0)
15371da177e4SLinus Torvalds 		return retval;
15381da177e4SLinus Torvalds 
15391da177e4SLinus Torvalds 	/* set halt (protocol test only), verify it worked */
15401da177e4SLinus Torvalds 	retval = usb_control_msg(urb->dev, usb_sndctrlpipe(urb->dev, 0),
15411da177e4SLinus Torvalds 			USB_REQ_SET_FEATURE, USB_RECIP_ENDPOINT,
15421da177e4SLinus Torvalds 			USB_ENDPOINT_HALT, ep,
15431da177e4SLinus Torvalds 			NULL, 0, USB_CTRL_SET_TIMEOUT);
15441da177e4SLinus Torvalds 	if (retval < 0) {
154528ffd79cSDavid Brownell 		ERROR(tdev, "ep %02x couldn't set halt, %d\n", ep, retval);
15461da177e4SLinus Torvalds 		return retval;
15471da177e4SLinus Torvalds 	}
154828ffd79cSDavid Brownell 	retval = verify_halted(tdev, ep, urb);
15491da177e4SLinus Torvalds 	if (retval < 0)
15501da177e4SLinus Torvalds 		return retval;
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds 	/* clear halt (tests API + protocol), verify it worked */
15531da177e4SLinus Torvalds 	retval = usb_clear_halt(urb->dev, urb->pipe);
15541da177e4SLinus Torvalds 	if (retval < 0) {
155528ffd79cSDavid Brownell 		ERROR(tdev, "ep %02x couldn't clear halt, %d\n", ep, retval);
15561da177e4SLinus Torvalds 		return retval;
15571da177e4SLinus Torvalds 	}
155828ffd79cSDavid Brownell 	retval = verify_not_halted(tdev, ep, urb);
15591da177e4SLinus Torvalds 	if (retval < 0)
15601da177e4SLinus Torvalds 		return retval;
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds 	/* NOTE:  could also verify SET_INTERFACE clear halts ... */
15631da177e4SLinus Torvalds 
15641da177e4SLinus Torvalds 	return 0;
15651da177e4SLinus Torvalds }
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds static int halt_simple(struct usbtest_dev *dev)
15681da177e4SLinus Torvalds {
15691da177e4SLinus Torvalds 	int			ep;
15701da177e4SLinus Torvalds 	int			retval = 0;
15711da177e4SLinus Torvalds 	struct urb		*urb;
15726a23ccd2SPaul Zimmerman 	struct usb_device	*udev = testdev_to_usbdev(dev);
15731da177e4SLinus Torvalds 
15746a23ccd2SPaul Zimmerman 	if (udev->speed == USB_SPEED_SUPER)
15756a23ccd2SPaul Zimmerman 		urb = simple_alloc_urb(udev, 0, 1024);
15766a23ccd2SPaul Zimmerman 	else
15776a23ccd2SPaul Zimmerman 		urb = simple_alloc_urb(udev, 0, 512);
15781da177e4SLinus Torvalds 	if (urb == NULL)
15791da177e4SLinus Torvalds 		return -ENOMEM;
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds 	if (dev->in_pipe) {
15821da177e4SLinus Torvalds 		ep = usb_pipeendpoint(dev->in_pipe) | USB_DIR_IN;
15831da177e4SLinus Torvalds 		urb->pipe = dev->in_pipe;
158428ffd79cSDavid Brownell 		retval = test_halt(dev, ep, urb);
15851da177e4SLinus Torvalds 		if (retval < 0)
15861da177e4SLinus Torvalds 			goto done;
15871da177e4SLinus Torvalds 	}
15881da177e4SLinus Torvalds 
15891da177e4SLinus Torvalds 	if (dev->out_pipe) {
15901da177e4SLinus Torvalds 		ep = usb_pipeendpoint(dev->out_pipe);
15911da177e4SLinus Torvalds 		urb->pipe = dev->out_pipe;
159228ffd79cSDavid Brownell 		retval = test_halt(dev, ep, urb);
15931da177e4SLinus Torvalds 	}
15941da177e4SLinus Torvalds done:
15951da177e4SLinus Torvalds 	simple_free_urb(urb);
15961da177e4SLinus Torvalds 	return retval;
15971da177e4SLinus Torvalds }
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds /* Control OUT tests use the vendor control requests from Intel's
16021da177e4SLinus Torvalds  * USB 2.0 compliance test device:  write a buffer, read it back.
16031da177e4SLinus Torvalds  *
16041da177e4SLinus Torvalds  * Intel's spec only _requires_ that it work for one packet, which
16051da177e4SLinus Torvalds  * is pretty weak.   Some HCDs place limits here; most devices will
16061da177e4SLinus Torvalds  * need to be able to handle more than one OUT data packet.  We'll
16071da177e4SLinus Torvalds  * try whatever we're told to try.
16081da177e4SLinus Torvalds  */
16091da177e4SLinus Torvalds static int ctrl_out(struct usbtest_dev *dev,
1610084fb206SMartin Fuzzey 		unsigned count, unsigned length, unsigned vary, unsigned offset)
16111da177e4SLinus Torvalds {
1612f54fa84dSOrjan Friberg 	unsigned		i, j, len;
1613f54fa84dSOrjan Friberg 	int			retval;
16141da177e4SLinus Torvalds 	u8			*buf;
16151da177e4SLinus Torvalds 	char			*what = "?";
16161da177e4SLinus Torvalds 	struct usb_device	*udev;
16171da177e4SLinus Torvalds 
1618ff7c79e4SDavid Brownell 	if (length < 1 || length > 0xffff || vary >= length)
16191da177e4SLinus Torvalds 		return -EINVAL;
16201da177e4SLinus Torvalds 
1621084fb206SMartin Fuzzey 	buf = kmalloc(length + offset, GFP_KERNEL);
16221da177e4SLinus Torvalds 	if (!buf)
16231da177e4SLinus Torvalds 		return -ENOMEM;
16241da177e4SLinus Torvalds 
1625084fb206SMartin Fuzzey 	buf += offset;
16261da177e4SLinus Torvalds 	udev = testdev_to_usbdev(dev);
16271da177e4SLinus Torvalds 	len = length;
16281da177e4SLinus Torvalds 	retval = 0;
16291da177e4SLinus Torvalds 
16301da177e4SLinus Torvalds 	/* NOTE:  hardware might well act differently if we pushed it
16311da177e4SLinus Torvalds 	 * with lots back-to-back queued requests.
16321da177e4SLinus Torvalds 	 */
16331da177e4SLinus Torvalds 	for (i = 0; i < count; i++) {
16341da177e4SLinus Torvalds 		/* write patterned data */
16351da177e4SLinus Torvalds 		for (j = 0; j < len; j++)
16361da177e4SLinus Torvalds 			buf[j] = i + j;
16371da177e4SLinus Torvalds 		retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
16381da177e4SLinus Torvalds 				0x5b, USB_DIR_OUT|USB_TYPE_VENDOR,
16391da177e4SLinus Torvalds 				0, 0, buf, len, USB_CTRL_SET_TIMEOUT);
16401da177e4SLinus Torvalds 		if (retval != len) {
16411da177e4SLinus Torvalds 			what = "write";
1642ff7c79e4SDavid Brownell 			if (retval >= 0) {
164328ffd79cSDavid Brownell 				ERROR(dev, "ctrl_out, wlen %d (expected %d)\n",
1644ff7c79e4SDavid Brownell 						retval, len);
1645ff7c79e4SDavid Brownell 				retval = -EBADMSG;
1646ff7c79e4SDavid Brownell 			}
16471da177e4SLinus Torvalds 			break;
16481da177e4SLinus Torvalds 		}
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 		/* read it back -- assuming nothing intervened!!  */
16511da177e4SLinus Torvalds 		retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
16521da177e4SLinus Torvalds 				0x5c, USB_DIR_IN|USB_TYPE_VENDOR,
16531da177e4SLinus Torvalds 				0, 0, buf, len, USB_CTRL_GET_TIMEOUT);
16541da177e4SLinus Torvalds 		if (retval != len) {
16551da177e4SLinus Torvalds 			what = "read";
1656ff7c79e4SDavid Brownell 			if (retval >= 0) {
165728ffd79cSDavid Brownell 				ERROR(dev, "ctrl_out, rlen %d (expected %d)\n",
1658ff7c79e4SDavid Brownell 						retval, len);
1659ff7c79e4SDavid Brownell 				retval = -EBADMSG;
1660ff7c79e4SDavid Brownell 			}
16611da177e4SLinus Torvalds 			break;
16621da177e4SLinus Torvalds 		}
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds 		/* fail if we can't verify */
16651da177e4SLinus Torvalds 		for (j = 0; j < len; j++) {
16661da177e4SLinus Torvalds 			if (buf[j] != (u8) (i + j)) {
166728ffd79cSDavid Brownell 				ERROR(dev, "ctrl_out, byte %d is %d not %d\n",
16681da177e4SLinus Torvalds 					j, buf[j], (u8) i + j);
16691da177e4SLinus Torvalds 				retval = -EBADMSG;
16701da177e4SLinus Torvalds 				break;
16711da177e4SLinus Torvalds 			}
16721da177e4SLinus Torvalds 		}
16731da177e4SLinus Torvalds 		if (retval < 0) {
16741da177e4SLinus Torvalds 			what = "verify";
16751da177e4SLinus Torvalds 			break;
16761da177e4SLinus Torvalds 		}
16771da177e4SLinus Torvalds 
16781da177e4SLinus Torvalds 		len += vary;
1679ff7c79e4SDavid Brownell 
1680ff7c79e4SDavid Brownell 		/* [real world] the "zero bytes IN" case isn't really used.
1681dc0d5c1eSJoe Perches 		 * hardware can easily trip up in this weird case, since its
1682ff7c79e4SDavid Brownell 		 * status stage is IN, not OUT like other ep0in transfers.
1683ff7c79e4SDavid Brownell 		 */
16841da177e4SLinus Torvalds 		if (len > length)
1685ff7c79e4SDavid Brownell 			len = realworld ? 1 : 0;
16861da177e4SLinus Torvalds 	}
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds 	if (retval < 0)
168928ffd79cSDavid Brownell 		ERROR(dev, "ctrl_out %s failed, code %d, count %d\n",
16901da177e4SLinus Torvalds 			what, retval, i);
16911da177e4SLinus Torvalds 
1692084fb206SMartin Fuzzey 	kfree(buf - offset);
16931da177e4SLinus Torvalds 	return retval;
16941da177e4SLinus Torvalds }
16951da177e4SLinus Torvalds 
16961da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
16971da177e4SLinus Torvalds 
16981da177e4SLinus Torvalds /* ISO tests ... mimics common usage
16991da177e4SLinus Torvalds  *  - buffer length is split into N packets (mostly maxpacket sized)
17001da177e4SLinus Torvalds  *  - multi-buffers according to sglen
17011da177e4SLinus Torvalds  */
17021da177e4SLinus Torvalds 
17031da177e4SLinus Torvalds struct iso_context {
17041da177e4SLinus Torvalds 	unsigned		count;
17051da177e4SLinus Torvalds 	unsigned		pending;
17061da177e4SLinus Torvalds 	spinlock_t		lock;
17071da177e4SLinus Torvalds 	struct completion	done;
17089da2150fSAlan Stern 	int			submit_error;
17091da177e4SLinus Torvalds 	unsigned long		errors;
17109da2150fSAlan Stern 	unsigned long		packet_count;
17111da177e4SLinus Torvalds 	struct usbtest_dev	*dev;
17121da177e4SLinus Torvalds };
17131da177e4SLinus Torvalds 
17147d12e780SDavid Howells static void iso_callback(struct urb *urb)
17151da177e4SLinus Torvalds {
17161da177e4SLinus Torvalds 	struct iso_context	*ctx = urb->context;
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds 	spin_lock(&ctx->lock);
17191da177e4SLinus Torvalds 	ctx->count--;
17201da177e4SLinus Torvalds 
17219da2150fSAlan Stern 	ctx->packet_count += urb->number_of_packets;
17221da177e4SLinus Torvalds 	if (urb->error_count > 0)
17231da177e4SLinus Torvalds 		ctx->errors += urb->error_count;
17249da2150fSAlan Stern 	else if (urb->status != 0)
17259da2150fSAlan Stern 		ctx->errors += urb->number_of_packets;
172640aed524SMartin Fuzzey 	else if (urb->actual_length != urb->transfer_buffer_length)
172740aed524SMartin Fuzzey 		ctx->errors++;
1728084fb206SMartin Fuzzey 	else if (check_guard_bytes(ctx->dev, urb) != 0)
1729084fb206SMartin Fuzzey 		ctx->errors++;
17301da177e4SLinus Torvalds 
17319da2150fSAlan Stern 	if (urb->status == 0 && ctx->count > (ctx->pending - 1)
17329da2150fSAlan Stern 			&& !ctx->submit_error) {
17331da177e4SLinus Torvalds 		int status = usb_submit_urb(urb, GFP_ATOMIC);
17341da177e4SLinus Torvalds 		switch (status) {
17351da177e4SLinus Torvalds 		case 0:
17361da177e4SLinus Torvalds 			goto done;
17371da177e4SLinus Torvalds 		default:
173828ffd79cSDavid Brownell 			dev_err(&ctx->dev->intf->dev,
17391da177e4SLinus Torvalds 					"iso resubmit err %d\n",
17401da177e4SLinus Torvalds 					status);
17411da177e4SLinus Torvalds 			/* FALLTHROUGH */
17421da177e4SLinus Torvalds 		case -ENODEV:			/* disconnected */
17439da2150fSAlan Stern 		case -ESHUTDOWN:		/* endpoint disabled */
17449da2150fSAlan Stern 			ctx->submit_error = 1;
17451da177e4SLinus Torvalds 			break;
17461da177e4SLinus Torvalds 		}
17471da177e4SLinus Torvalds 	}
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds 	ctx->pending--;
17501da177e4SLinus Torvalds 	if (ctx->pending == 0) {
17511da177e4SLinus Torvalds 		if (ctx->errors)
175228ffd79cSDavid Brownell 			dev_err(&ctx->dev->intf->dev,
17539da2150fSAlan Stern 				"iso test, %lu errors out of %lu\n",
17549da2150fSAlan Stern 				ctx->errors, ctx->packet_count);
17551da177e4SLinus Torvalds 		complete(&ctx->done);
17561da177e4SLinus Torvalds 	}
17571da177e4SLinus Torvalds done:
17581da177e4SLinus Torvalds 	spin_unlock(&ctx->lock);
17591da177e4SLinus Torvalds }
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds static struct urb *iso_alloc_urb(
17621da177e4SLinus Torvalds 	struct usb_device	*udev,
17631da177e4SLinus Torvalds 	int			pipe,
17641da177e4SLinus Torvalds 	struct usb_endpoint_descriptor	*desc,
1765084fb206SMartin Fuzzey 	long			bytes,
1766084fb206SMartin Fuzzey 	unsigned offset
17671da177e4SLinus Torvalds )
17681da177e4SLinus Torvalds {
17691da177e4SLinus Torvalds 	struct urb		*urb;
17701da177e4SLinus Torvalds 	unsigned		i, maxp, packets;
17711da177e4SLinus Torvalds 
17721da177e4SLinus Torvalds 	if (bytes < 0 || !desc)
17731da177e4SLinus Torvalds 		return NULL;
177429cc8897SKuninori Morimoto 	maxp = 0x7ff & usb_endpoint_maxp(desc);
177529cc8897SKuninori Morimoto 	maxp *= 1 + (0x3 & (usb_endpoint_maxp(desc) >> 11));
1776dfa5ec79SJulia Lawall 	packets = DIV_ROUND_UP(bytes, maxp);
17771da177e4SLinus Torvalds 
1778e94b1766SChristoph Lameter 	urb = usb_alloc_urb(packets, GFP_KERNEL);
17791da177e4SLinus Torvalds 	if (!urb)
17801da177e4SLinus Torvalds 		return urb;
17811da177e4SLinus Torvalds 	urb->dev = udev;
17821da177e4SLinus Torvalds 	urb->pipe = pipe;
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds 	urb->number_of_packets = packets;
17851da177e4SLinus Torvalds 	urb->transfer_buffer_length = bytes;
1786084fb206SMartin Fuzzey 	urb->transfer_buffer = usb_alloc_coherent(udev, bytes + offset,
1787084fb206SMartin Fuzzey 							GFP_KERNEL,
17881da177e4SLinus Torvalds 							&urb->transfer_dma);
17891da177e4SLinus Torvalds 	if (!urb->transfer_buffer) {
17901da177e4SLinus Torvalds 		usb_free_urb(urb);
17911da177e4SLinus Torvalds 		return NULL;
17921da177e4SLinus Torvalds 	}
1793084fb206SMartin Fuzzey 	if (offset) {
1794084fb206SMartin Fuzzey 		memset(urb->transfer_buffer, GUARD_BYTE, offset);
1795084fb206SMartin Fuzzey 		urb->transfer_buffer += offset;
1796084fb206SMartin Fuzzey 		urb->transfer_dma += offset;
1797084fb206SMartin Fuzzey 	}
1798084fb206SMartin Fuzzey 	/* For inbound transfers use guard byte so that test fails if
1799084fb206SMartin Fuzzey 		data not correctly copied */
1800084fb206SMartin Fuzzey 	memset(urb->transfer_buffer,
1801084fb206SMartin Fuzzey 			usb_pipein(urb->pipe) ? GUARD_BYTE : 0,
1802084fb206SMartin Fuzzey 			bytes);
1803084fb206SMartin Fuzzey 
18041da177e4SLinus Torvalds 	for (i = 0; i < packets; i++) {
18051da177e4SLinus Torvalds 		/* here, only the last packet will be short */
18061da177e4SLinus Torvalds 		urb->iso_frame_desc[i].length = min((unsigned) bytes, maxp);
18071da177e4SLinus Torvalds 		bytes -= urb->iso_frame_desc[i].length;
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds 		urb->iso_frame_desc[i].offset = maxp * i;
18101da177e4SLinus Torvalds 	}
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds 	urb->complete = iso_callback;
1813fabbf219SMartin Fuzzey 	/* urb->context = SET BY CALLER */
18141da177e4SLinus Torvalds 	urb->interval = 1 << (desc->bInterval - 1);
18151da177e4SLinus Torvalds 	urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
18161da177e4SLinus Torvalds 	return urb;
18171da177e4SLinus Torvalds }
18181da177e4SLinus Torvalds 
18191da177e4SLinus Torvalds static int
18201da177e4SLinus Torvalds test_iso_queue(struct usbtest_dev *dev, struct usbtest_param *param,
1821084fb206SMartin Fuzzey 		int pipe, struct usb_endpoint_descriptor *desc, unsigned offset)
18221da177e4SLinus Torvalds {
18231da177e4SLinus Torvalds 	struct iso_context	context;
18241da177e4SLinus Torvalds 	struct usb_device	*udev;
18251da177e4SLinus Torvalds 	unsigned		i;
18261da177e4SLinus Torvalds 	unsigned long		packets = 0;
18279da2150fSAlan Stern 	int			status = 0;
18281da177e4SLinus Torvalds 	struct urb		*urbs[10];	/* FIXME no limit */
18291da177e4SLinus Torvalds 
18301da177e4SLinus Torvalds 	if (param->sglen > 10)
18311da177e4SLinus Torvalds 		return -EDOM;
18321da177e4SLinus Torvalds 
1833f55055b4SHuang Rui 	memset(&context, 0, sizeof(context));
18341da177e4SLinus Torvalds 	context.count = param->iterations * param->sglen;
18351da177e4SLinus Torvalds 	context.dev = dev;
18361da177e4SLinus Torvalds 	init_completion(&context.done);
18371da177e4SLinus Torvalds 	spin_lock_init(&context.lock);
18381da177e4SLinus Torvalds 
1839f55055b4SHuang Rui 	memset(urbs, 0, sizeof(urbs));
18401da177e4SLinus Torvalds 	udev = testdev_to_usbdev(dev);
184128ffd79cSDavid Brownell 	dev_info(&dev->intf->dev,
18421da177e4SLinus Torvalds 		"... iso period %d %sframes, wMaxPacket %04x\n",
18431da177e4SLinus Torvalds 		1 << (desc->bInterval - 1),
18441da177e4SLinus Torvalds 		(udev->speed == USB_SPEED_HIGH) ? "micro" : "",
184529cc8897SKuninori Morimoto 		usb_endpoint_maxp(desc));
18461da177e4SLinus Torvalds 
18471da177e4SLinus Torvalds 	for (i = 0; i < param->sglen; i++) {
18481da177e4SLinus Torvalds 		urbs[i] = iso_alloc_urb(udev, pipe, desc,
1849084fb206SMartin Fuzzey 					param->length, offset);
18501da177e4SLinus Torvalds 		if (!urbs[i]) {
18511da177e4SLinus Torvalds 			status = -ENOMEM;
18521da177e4SLinus Torvalds 			goto fail;
18531da177e4SLinus Torvalds 		}
18541da177e4SLinus Torvalds 		packets += urbs[i]->number_of_packets;
18551da177e4SLinus Torvalds 		urbs[i]->context = &context;
18561da177e4SLinus Torvalds 	}
18571da177e4SLinus Torvalds 	packets *= param->iterations;
185828ffd79cSDavid Brownell 	dev_info(&dev->intf->dev,
18591da177e4SLinus Torvalds 		"... total %lu msec (%lu packets)\n",
18601da177e4SLinus Torvalds 		(packets * (1 << (desc->bInterval - 1)))
18611da177e4SLinus Torvalds 			/ ((udev->speed == USB_SPEED_HIGH) ? 8 : 1),
18621da177e4SLinus Torvalds 		packets);
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds 	spin_lock_irq(&context.lock);
18651da177e4SLinus Torvalds 	for (i = 0; i < param->sglen; i++) {
18669da2150fSAlan Stern 		++context.pending;
186754e6ecb2SChristoph Lameter 		status = usb_submit_urb(urbs[i], GFP_ATOMIC);
18681da177e4SLinus Torvalds 		if (status < 0) {
18691da177e4SLinus Torvalds 			ERROR(dev, "submit iso[%d], error %d\n", i, status);
18701da177e4SLinus Torvalds 			if (i == 0) {
18711da177e4SLinus Torvalds 				spin_unlock_irq(&context.lock);
18721da177e4SLinus Torvalds 				goto fail;
18731da177e4SLinus Torvalds 			}
18741da177e4SLinus Torvalds 
18751da177e4SLinus Torvalds 			simple_free_urb(urbs[i]);
1876e10e1becSMing Lei 			urbs[i] = NULL;
18771da177e4SLinus Torvalds 			context.pending--;
18789da2150fSAlan Stern 			context.submit_error = 1;
18799da2150fSAlan Stern 			break;
18801da177e4SLinus Torvalds 		}
18811da177e4SLinus Torvalds 	}
18821da177e4SLinus Torvalds 	spin_unlock_irq(&context.lock);
18831da177e4SLinus Torvalds 
18841da177e4SLinus Torvalds 	wait_for_completion(&context.done);
18859da2150fSAlan Stern 
1886e10e1becSMing Lei 	for (i = 0; i < param->sglen; i++) {
1887e10e1becSMing Lei 		if (urbs[i])
1888e10e1becSMing Lei 			simple_free_urb(urbs[i]);
1889e10e1becSMing Lei 	}
18909da2150fSAlan Stern 	/*
18919da2150fSAlan Stern 	 * Isochronous transfers are expected to fail sometimes.  As an
18929da2150fSAlan Stern 	 * arbitrary limit, we will report an error if any submissions
18939da2150fSAlan Stern 	 * fail or if the transfer failure rate is > 10%.
18949da2150fSAlan Stern 	 */
18959da2150fSAlan Stern 	if (status != 0)
18969da2150fSAlan Stern 		;
18979da2150fSAlan Stern 	else if (context.submit_error)
18989da2150fSAlan Stern 		status = -EACCES;
18999da2150fSAlan Stern 	else if (context.errors > context.packet_count / 10)
19009da2150fSAlan Stern 		status = -EIO;
19019da2150fSAlan Stern 	return status;
19021da177e4SLinus Torvalds 
19031da177e4SLinus Torvalds fail:
19041da177e4SLinus Torvalds 	for (i = 0; i < param->sglen; i++) {
19051da177e4SLinus Torvalds 		if (urbs[i])
19061da177e4SLinus Torvalds 			simple_free_urb(urbs[i]);
19071da177e4SLinus Torvalds 	}
19081da177e4SLinus Torvalds 	return status;
19091da177e4SLinus Torvalds }
19101da177e4SLinus Torvalds 
1911084fb206SMartin Fuzzey static int test_unaligned_bulk(
1912084fb206SMartin Fuzzey 	struct usbtest_dev *tdev,
1913084fb206SMartin Fuzzey 	int pipe,
1914084fb206SMartin Fuzzey 	unsigned length,
1915084fb206SMartin Fuzzey 	int iterations,
1916084fb206SMartin Fuzzey 	unsigned transfer_flags,
1917084fb206SMartin Fuzzey 	const char *label)
1918084fb206SMartin Fuzzey {
1919084fb206SMartin Fuzzey 	int retval;
1920084fb206SMartin Fuzzey 	struct urb *urb = usbtest_alloc_urb(
1921084fb206SMartin Fuzzey 		testdev_to_usbdev(tdev), pipe, length, transfer_flags, 1);
1922084fb206SMartin Fuzzey 
1923084fb206SMartin Fuzzey 	if (!urb)
1924084fb206SMartin Fuzzey 		return -ENOMEM;
1925084fb206SMartin Fuzzey 
1926084fb206SMartin Fuzzey 	retval = simple_io(tdev, urb, iterations, 0, 0, label);
1927084fb206SMartin Fuzzey 	simple_free_urb(urb);
1928084fb206SMartin Fuzzey 	return retval;
1929084fb206SMartin Fuzzey }
1930084fb206SMartin Fuzzey 
19311da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
19321da177e4SLinus Torvalds 
19331da177e4SLinus Torvalds /* We only have this one interface to user space, through usbfs.
19341da177e4SLinus Torvalds  * User mode code can scan usbfs to find N different devices (maybe on
19351da177e4SLinus Torvalds  * different busses) to use when testing, and allocate one thread per
19361da177e4SLinus Torvalds  * test.  So discovery is simplified, and we have no device naming issues.
19371da177e4SLinus Torvalds  *
19381da177e4SLinus Torvalds  * Don't use these only as stress/load tests.  Use them along with with
19391da177e4SLinus Torvalds  * other USB bus activity:  plugging, unplugging, mousing, mp3 playback,
19401da177e4SLinus Torvalds  * video capture, and so on.  Run different tests at different times, in
19411da177e4SLinus Torvalds  * different sequences.  Nothing here should interact with other devices,
19421da177e4SLinus Torvalds  * except indirectly by consuming USB bandwidth and CPU resources for test
19431da177e4SLinus Torvalds  * threads and request completion.  But the only way to know that for sure
19441da177e4SLinus Torvalds  * is to test when HC queues are in use by many devices.
194528ffd79cSDavid Brownell  *
194628ffd79cSDavid Brownell  * WARNING:  Because usbfs grabs udev->dev.sem before calling this ioctl(),
194728ffd79cSDavid Brownell  * it locks out usbcore in certain code paths.  Notably, if you disconnect
194828ffd79cSDavid Brownell  * the device-under-test, khubd will wait block forever waiting for the
194928ffd79cSDavid Brownell  * ioctl to complete ... so that usb_disconnect() can abort the pending
195028ffd79cSDavid Brownell  * urbs and then call usbtest_disconnect().  To abort a test, you're best
195128ffd79cSDavid Brownell  * off just killing the userspace task and waiting for it to exit.
19521da177e4SLinus Torvalds  */
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds static int
19551da177e4SLinus Torvalds usbtest_ioctl(struct usb_interface *intf, unsigned int code, void *buf)
19561da177e4SLinus Torvalds {
19571da177e4SLinus Torvalds 	struct usbtest_dev	*dev = usb_get_intfdata(intf);
19581da177e4SLinus Torvalds 	struct usb_device	*udev = testdev_to_usbdev(dev);
19591da177e4SLinus Torvalds 	struct usbtest_param	*param = buf;
19601da177e4SLinus Torvalds 	int			retval = -EOPNOTSUPP;
19611da177e4SLinus Torvalds 	struct urb		*urb;
19621da177e4SLinus Torvalds 	struct scatterlist	*sg;
19631da177e4SLinus Torvalds 	struct usb_sg_request	req;
19641da177e4SLinus Torvalds 	struct timeval		start;
19651da177e4SLinus Torvalds 	unsigned		i;
19661da177e4SLinus Torvalds 
1967fabbf219SMartin Fuzzey 	/* FIXME USBDEVFS_CONNECTINFO doesn't say how fast the device is. */
19681da177e4SLinus Torvalds 
19697f4e9854SVikram Pandita 	pattern = mod_pattern;
19707f4e9854SVikram Pandita 
19711da177e4SLinus Torvalds 	if (code != USBTEST_REQUEST)
19721da177e4SLinus Torvalds 		return -EOPNOTSUPP;
19731da177e4SLinus Torvalds 
19748aafdf6aSroel kluin 	if (param->iterations <= 0)
19751da177e4SLinus Torvalds 		return -EINVAL;
19761da177e4SLinus Torvalds 
19771cfab028SMatthias Kaehlcke 	if (mutex_lock_interruptible(&dev->lock))
19781da177e4SLinus Torvalds 		return -ERESTARTSYS;
19791da177e4SLinus Torvalds 
198070a1c9e0SAlan Stern 	/* FIXME: What if a system sleep starts while a test is running? */
1981ff7c79e4SDavid Brownell 
19821da177e4SLinus Torvalds 	/* some devices, like ez-usb default devices, need a non-default
19831da177e4SLinus Torvalds 	 * altsetting to have any active endpoints.  some tests change
19841da177e4SLinus Torvalds 	 * altsettings; force a default so most tests don't need to check.
19851da177e4SLinus Torvalds 	 */
19861da177e4SLinus Torvalds 	if (dev->info->alt >= 0) {
19871da177e4SLinus Torvalds 		int	res;
19881da177e4SLinus Torvalds 
19891da177e4SLinus Torvalds 		if (intf->altsetting->desc.bInterfaceNumber) {
19901cfab028SMatthias Kaehlcke 			mutex_unlock(&dev->lock);
19911da177e4SLinus Torvalds 			return -ENODEV;
19921da177e4SLinus Torvalds 		}
19931da177e4SLinus Torvalds 		res = set_altsetting(dev, dev->info->alt);
19941da177e4SLinus Torvalds 		if (res) {
19951da177e4SLinus Torvalds 			dev_err(&intf->dev,
19961da177e4SLinus Torvalds 					"set altsetting to %d failed, %d\n",
19971da177e4SLinus Torvalds 					dev->info->alt, res);
19981cfab028SMatthias Kaehlcke 			mutex_unlock(&dev->lock);
19991da177e4SLinus Torvalds 			return res;
20001da177e4SLinus Torvalds 		}
20011da177e4SLinus Torvalds 	}
20021da177e4SLinus Torvalds 
20031da177e4SLinus Torvalds 	/*
20041da177e4SLinus Torvalds 	 * Just a bunch of test cases that every HCD is expected to handle.
20051da177e4SLinus Torvalds 	 *
20061da177e4SLinus Torvalds 	 * Some may need specific firmware, though it'd be good to have
20071da177e4SLinus Torvalds 	 * one firmware image to handle all the test cases.
20081da177e4SLinus Torvalds 	 *
20091da177e4SLinus Torvalds 	 * FIXME add more tests!  cancel requests, verify the data, control
20101da177e4SLinus Torvalds 	 * queueing, concurrent read+write threads, and so on.
20111da177e4SLinus Torvalds 	 */
20121da177e4SLinus Torvalds 	do_gettimeofday(&start);
20131da177e4SLinus Torvalds 	switch (param->test_num) {
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds 	case 0:
201628ffd79cSDavid Brownell 		dev_info(&intf->dev, "TEST 0:  NOP\n");
20171da177e4SLinus Torvalds 		retval = 0;
20181da177e4SLinus Torvalds 		break;
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds 	/* Simple non-queued bulk I/O tests */
20211da177e4SLinus Torvalds 	case 1:
20221da177e4SLinus Torvalds 		if (dev->out_pipe == 0)
20231da177e4SLinus Torvalds 			break;
202428ffd79cSDavid Brownell 		dev_info(&intf->dev,
20251da177e4SLinus Torvalds 				"TEST 1:  write %d bytes %u times\n",
20261da177e4SLinus Torvalds 				param->length, param->iterations);
20271da177e4SLinus Torvalds 		urb = simple_alloc_urb(udev, dev->out_pipe, param->length);
20281da177e4SLinus Torvalds 		if (!urb) {
20291da177e4SLinus Torvalds 			retval = -ENOMEM;
20301da177e4SLinus Torvalds 			break;
20311da177e4SLinus Torvalds 		}
2032fabbf219SMartin Fuzzey 		/* FIRMWARE:  bulk sink (maybe accepts short writes) */
203328ffd79cSDavid Brownell 		retval = simple_io(dev, urb, param->iterations, 0, 0, "test1");
20341da177e4SLinus Torvalds 		simple_free_urb(urb);
20351da177e4SLinus Torvalds 		break;
20361da177e4SLinus Torvalds 	case 2:
20371da177e4SLinus Torvalds 		if (dev->in_pipe == 0)
20381da177e4SLinus Torvalds 			break;
203928ffd79cSDavid Brownell 		dev_info(&intf->dev,
20401da177e4SLinus Torvalds 				"TEST 2:  read %d bytes %u times\n",
20411da177e4SLinus Torvalds 				param->length, param->iterations);
20421da177e4SLinus Torvalds 		urb = simple_alloc_urb(udev, dev->in_pipe, param->length);
20431da177e4SLinus Torvalds 		if (!urb) {
20441da177e4SLinus Torvalds 			retval = -ENOMEM;
20451da177e4SLinus Torvalds 			break;
20461da177e4SLinus Torvalds 		}
2047fabbf219SMartin Fuzzey 		/* FIRMWARE:  bulk source (maybe generates short writes) */
204828ffd79cSDavid Brownell 		retval = simple_io(dev, urb, param->iterations, 0, 0, "test2");
20491da177e4SLinus Torvalds 		simple_free_urb(urb);
20501da177e4SLinus Torvalds 		break;
20511da177e4SLinus Torvalds 	case 3:
20521da177e4SLinus Torvalds 		if (dev->out_pipe == 0 || param->vary == 0)
20531da177e4SLinus Torvalds 			break;
205428ffd79cSDavid Brownell 		dev_info(&intf->dev,
20551da177e4SLinus Torvalds 				"TEST 3:  write/%d 0..%d bytes %u times\n",
20561da177e4SLinus Torvalds 				param->vary, param->length, param->iterations);
20571da177e4SLinus Torvalds 		urb = simple_alloc_urb(udev, dev->out_pipe, param->length);
20581da177e4SLinus Torvalds 		if (!urb) {
20591da177e4SLinus Torvalds 			retval = -ENOMEM;
20601da177e4SLinus Torvalds 			break;
20611da177e4SLinus Torvalds 		}
2062fabbf219SMartin Fuzzey 		/* FIRMWARE:  bulk sink (maybe accepts short writes) */
206328ffd79cSDavid Brownell 		retval = simple_io(dev, urb, param->iterations, param->vary,
20641da177e4SLinus Torvalds 					0, "test3");
20651da177e4SLinus Torvalds 		simple_free_urb(urb);
20661da177e4SLinus Torvalds 		break;
20671da177e4SLinus Torvalds 	case 4:
20681da177e4SLinus Torvalds 		if (dev->in_pipe == 0 || param->vary == 0)
20691da177e4SLinus Torvalds 			break;
207028ffd79cSDavid Brownell 		dev_info(&intf->dev,
20711da177e4SLinus Torvalds 				"TEST 4:  read/%d 0..%d bytes %u times\n",
20721da177e4SLinus Torvalds 				param->vary, param->length, param->iterations);
20731da177e4SLinus Torvalds 		urb = simple_alloc_urb(udev, dev->in_pipe, param->length);
20741da177e4SLinus Torvalds 		if (!urb) {
20751da177e4SLinus Torvalds 			retval = -ENOMEM;
20761da177e4SLinus Torvalds 			break;
20771da177e4SLinus Torvalds 		}
2078fabbf219SMartin Fuzzey 		/* FIRMWARE:  bulk source (maybe generates short writes) */
207928ffd79cSDavid Brownell 		retval = simple_io(dev, urb, param->iterations, param->vary,
20801da177e4SLinus Torvalds 					0, "test4");
20811da177e4SLinus Torvalds 		simple_free_urb(urb);
20821da177e4SLinus Torvalds 		break;
20831da177e4SLinus Torvalds 
20841da177e4SLinus Torvalds 	/* Queued bulk I/O tests */
20851da177e4SLinus Torvalds 	case 5:
20861da177e4SLinus Torvalds 		if (dev->out_pipe == 0 || param->sglen == 0)
20871da177e4SLinus Torvalds 			break;
208828ffd79cSDavid Brownell 		dev_info(&intf->dev,
20891da177e4SLinus Torvalds 			"TEST 5:  write %d sglists %d entries of %d bytes\n",
20901da177e4SLinus Torvalds 				param->iterations,
20911da177e4SLinus Torvalds 				param->sglen, param->length);
20921da177e4SLinus Torvalds 		sg = alloc_sglist(param->sglen, param->length, 0);
20931da177e4SLinus Torvalds 		if (!sg) {
20941da177e4SLinus Torvalds 			retval = -ENOMEM;
20951da177e4SLinus Torvalds 			break;
20961da177e4SLinus Torvalds 		}
2097fabbf219SMartin Fuzzey 		/* FIRMWARE:  bulk sink (maybe accepts short writes) */
209828ffd79cSDavid Brownell 		retval = perform_sglist(dev, param->iterations, dev->out_pipe,
20991da177e4SLinus Torvalds 				&req, sg, param->sglen);
21001da177e4SLinus Torvalds 		free_sglist(sg, param->sglen);
21011da177e4SLinus Torvalds 		break;
21021da177e4SLinus Torvalds 
21031da177e4SLinus Torvalds 	case 6:
21041da177e4SLinus Torvalds 		if (dev->in_pipe == 0 || param->sglen == 0)
21051da177e4SLinus Torvalds 			break;
210628ffd79cSDavid Brownell 		dev_info(&intf->dev,
21071da177e4SLinus Torvalds 			"TEST 6:  read %d sglists %d entries of %d bytes\n",
21081da177e4SLinus Torvalds 				param->iterations,
21091da177e4SLinus Torvalds 				param->sglen, param->length);
21101da177e4SLinus Torvalds 		sg = alloc_sglist(param->sglen, param->length, 0);
21111da177e4SLinus Torvalds 		if (!sg) {
21121da177e4SLinus Torvalds 			retval = -ENOMEM;
21131da177e4SLinus Torvalds 			break;
21141da177e4SLinus Torvalds 		}
2115fabbf219SMartin Fuzzey 		/* FIRMWARE:  bulk source (maybe generates short writes) */
211628ffd79cSDavid Brownell 		retval = perform_sglist(dev, param->iterations, dev->in_pipe,
21171da177e4SLinus Torvalds 				&req, sg, param->sglen);
21181da177e4SLinus Torvalds 		free_sglist(sg, param->sglen);
21191da177e4SLinus Torvalds 		break;
21201da177e4SLinus Torvalds 	case 7:
21211da177e4SLinus Torvalds 		if (dev->out_pipe == 0 || param->sglen == 0 || param->vary == 0)
21221da177e4SLinus Torvalds 			break;
212328ffd79cSDavid Brownell 		dev_info(&intf->dev,
21241da177e4SLinus Torvalds 			"TEST 7:  write/%d %d sglists %d entries 0..%d bytes\n",
21251da177e4SLinus Torvalds 				param->vary, param->iterations,
21261da177e4SLinus Torvalds 				param->sglen, param->length);
21271da177e4SLinus Torvalds 		sg = alloc_sglist(param->sglen, param->length, param->vary);
21281da177e4SLinus Torvalds 		if (!sg) {
21291da177e4SLinus Torvalds 			retval = -ENOMEM;
21301da177e4SLinus Torvalds 			break;
21311da177e4SLinus Torvalds 		}
2132fabbf219SMartin Fuzzey 		/* FIRMWARE:  bulk sink (maybe accepts short writes) */
213328ffd79cSDavid Brownell 		retval = perform_sglist(dev, param->iterations, dev->out_pipe,
21341da177e4SLinus Torvalds 				&req, sg, param->sglen);
21351da177e4SLinus Torvalds 		free_sglist(sg, param->sglen);
21361da177e4SLinus Torvalds 		break;
21371da177e4SLinus Torvalds 	case 8:
21381da177e4SLinus Torvalds 		if (dev->in_pipe == 0 || param->sglen == 0 || param->vary == 0)
21391da177e4SLinus Torvalds 			break;
214028ffd79cSDavid Brownell 		dev_info(&intf->dev,
21411da177e4SLinus Torvalds 			"TEST 8:  read/%d %d sglists %d entries 0..%d bytes\n",
21421da177e4SLinus Torvalds 				param->vary, param->iterations,
21431da177e4SLinus Torvalds 				param->sglen, param->length);
21441da177e4SLinus Torvalds 		sg = alloc_sglist(param->sglen, param->length, param->vary);
21451da177e4SLinus Torvalds 		if (!sg) {
21461da177e4SLinus Torvalds 			retval = -ENOMEM;
21471da177e4SLinus Torvalds 			break;
21481da177e4SLinus Torvalds 		}
2149fabbf219SMartin Fuzzey 		/* FIRMWARE:  bulk source (maybe generates short writes) */
215028ffd79cSDavid Brownell 		retval = perform_sglist(dev, param->iterations, dev->in_pipe,
21511da177e4SLinus Torvalds 				&req, sg, param->sglen);
21521da177e4SLinus Torvalds 		free_sglist(sg, param->sglen);
21531da177e4SLinus Torvalds 		break;
21541da177e4SLinus Torvalds 
21551da177e4SLinus Torvalds 	/* non-queued sanity tests for control (chapter 9 subset) */
21561da177e4SLinus Torvalds 	case 9:
21571da177e4SLinus Torvalds 		retval = 0;
215828ffd79cSDavid Brownell 		dev_info(&intf->dev,
21591da177e4SLinus Torvalds 			"TEST 9:  ch9 (subset) control tests, %d times\n",
21601da177e4SLinus Torvalds 				param->iterations);
21611da177e4SLinus Torvalds 		for (i = param->iterations; retval == 0 && i--; /* NOP */)
21621da177e4SLinus Torvalds 			retval = ch9_postconfig(dev);
21631da177e4SLinus Torvalds 		if (retval)
216428ffd79cSDavid Brownell 			dev_err(&intf->dev, "ch9 subset failed, "
216528ffd79cSDavid Brownell 					"iterations left %d\n", i);
21661da177e4SLinus Torvalds 		break;
21671da177e4SLinus Torvalds 
21681da177e4SLinus Torvalds 	/* queued control messaging */
21691da177e4SLinus Torvalds 	case 10:
21701da177e4SLinus Torvalds 		retval = 0;
217128ffd79cSDavid Brownell 		dev_info(&intf->dev,
21721da177e4SLinus Torvalds 				"TEST 10:  queue %d control calls, %d times\n",
21731da177e4SLinus Torvalds 				param->sglen,
21741da177e4SLinus Torvalds 				param->iterations);
21751da177e4SLinus Torvalds 		retval = test_ctrl_queue(dev, param);
21761da177e4SLinus Torvalds 		break;
21771da177e4SLinus Torvalds 
21781da177e4SLinus Torvalds 	/* simple non-queued unlinks (ring with one urb) */
21791da177e4SLinus Torvalds 	case 11:
21801da177e4SLinus Torvalds 		if (dev->in_pipe == 0 || !param->length)
21811da177e4SLinus Torvalds 			break;
21821da177e4SLinus Torvalds 		retval = 0;
218328ffd79cSDavid Brownell 		dev_info(&intf->dev, "TEST 11:  unlink %d reads of %d\n",
21841da177e4SLinus Torvalds 				param->iterations, param->length);
21851da177e4SLinus Torvalds 		for (i = param->iterations; retval == 0 && i--; /* NOP */)
21861da177e4SLinus Torvalds 			retval = unlink_simple(dev, dev->in_pipe,
21871da177e4SLinus Torvalds 						param->length);
21881da177e4SLinus Torvalds 		if (retval)
218928ffd79cSDavid Brownell 			dev_err(&intf->dev, "unlink reads failed %d, "
21901da177e4SLinus Torvalds 				"iterations left %d\n", retval, i);
21911da177e4SLinus Torvalds 		break;
21921da177e4SLinus Torvalds 	case 12:
21931da177e4SLinus Torvalds 		if (dev->out_pipe == 0 || !param->length)
21941da177e4SLinus Torvalds 			break;
21951da177e4SLinus Torvalds 		retval = 0;
219628ffd79cSDavid Brownell 		dev_info(&intf->dev, "TEST 12:  unlink %d writes of %d\n",
21971da177e4SLinus Torvalds 				param->iterations, param->length);
21981da177e4SLinus Torvalds 		for (i = param->iterations; retval == 0 && i--; /* NOP */)
21991da177e4SLinus Torvalds 			retval = unlink_simple(dev, dev->out_pipe,
22001da177e4SLinus Torvalds 						param->length);
22011da177e4SLinus Torvalds 		if (retval)
220228ffd79cSDavid Brownell 			dev_err(&intf->dev, "unlink writes failed %d, "
22031da177e4SLinus Torvalds 				"iterations left %d\n", retval, i);
22041da177e4SLinus Torvalds 		break;
22051da177e4SLinus Torvalds 
22061da177e4SLinus Torvalds 	/* ep halt tests */
22071da177e4SLinus Torvalds 	case 13:
22081da177e4SLinus Torvalds 		if (dev->out_pipe == 0 && dev->in_pipe == 0)
22091da177e4SLinus Torvalds 			break;
22101da177e4SLinus Torvalds 		retval = 0;
221128ffd79cSDavid Brownell 		dev_info(&intf->dev, "TEST 13:  set/clear %d halts\n",
22121da177e4SLinus Torvalds 				param->iterations);
22131da177e4SLinus Torvalds 		for (i = param->iterations; retval == 0 && i--; /* NOP */)
22141da177e4SLinus Torvalds 			retval = halt_simple(dev);
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds 		if (retval)
221728ffd79cSDavid Brownell 			ERROR(dev, "halts failed, iterations left %d\n", i);
22181da177e4SLinus Torvalds 		break;
22191da177e4SLinus Torvalds 
22201da177e4SLinus Torvalds 	/* control write tests */
22211da177e4SLinus Torvalds 	case 14:
22221da177e4SLinus Torvalds 		if (!dev->info->ctrl_out)
22231da177e4SLinus Torvalds 			break;
222428ffd79cSDavid Brownell 		dev_info(&intf->dev, "TEST 14:  %d ep0out, %d..%d vary %d\n",
2225ff7c79e4SDavid Brownell 				param->iterations,
2226ff7c79e4SDavid Brownell 				realworld ? 1 : 0, param->length,
2227ff7c79e4SDavid Brownell 				param->vary);
22281da177e4SLinus Torvalds 		retval = ctrl_out(dev, param->iterations,
2229084fb206SMartin Fuzzey 				param->length, param->vary, 0);
22301da177e4SLinus Torvalds 		break;
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds 	/* iso write tests */
22331da177e4SLinus Torvalds 	case 15:
22341da177e4SLinus Torvalds 		if (dev->out_iso_pipe == 0 || param->sglen == 0)
22351da177e4SLinus Torvalds 			break;
223628ffd79cSDavid Brownell 		dev_info(&intf->dev,
22371da177e4SLinus Torvalds 			"TEST 15:  write %d iso, %d entries of %d bytes\n",
22381da177e4SLinus Torvalds 				param->iterations,
22391da177e4SLinus Torvalds 				param->sglen, param->length);
2240fabbf219SMartin Fuzzey 		/* FIRMWARE:  iso sink */
22411da177e4SLinus Torvalds 		retval = test_iso_queue(dev, param,
2242084fb206SMartin Fuzzey 				dev->out_iso_pipe, dev->iso_out, 0);
22431da177e4SLinus Torvalds 		break;
22441da177e4SLinus Torvalds 
22451da177e4SLinus Torvalds 	/* iso read tests */
22461da177e4SLinus Torvalds 	case 16:
22471da177e4SLinus Torvalds 		if (dev->in_iso_pipe == 0 || param->sglen == 0)
22481da177e4SLinus Torvalds 			break;
224928ffd79cSDavid Brownell 		dev_info(&intf->dev,
22501da177e4SLinus Torvalds 			"TEST 16:  read %d iso, %d entries of %d bytes\n",
22511da177e4SLinus Torvalds 				param->iterations,
22521da177e4SLinus Torvalds 				param->sglen, param->length);
2253fabbf219SMartin Fuzzey 		/* FIRMWARE:  iso source */
22541da177e4SLinus Torvalds 		retval = test_iso_queue(dev, param,
2255084fb206SMartin Fuzzey 				dev->in_iso_pipe, dev->iso_in, 0);
22561da177e4SLinus Torvalds 		break;
22571da177e4SLinus Torvalds 
2258fabbf219SMartin Fuzzey 	/* FIXME scatterlist cancel (needs helper thread) */
22591da177e4SLinus Torvalds 
2260084fb206SMartin Fuzzey 	/* Tests for bulk I/O using DMA mapping by core and odd address */
2261084fb206SMartin Fuzzey 	case 17:
2262084fb206SMartin Fuzzey 		if (dev->out_pipe == 0)
2263084fb206SMartin Fuzzey 			break;
2264084fb206SMartin Fuzzey 		dev_info(&intf->dev,
2265084fb206SMartin Fuzzey 			"TEST 17:  write odd addr %d bytes %u times core map\n",
2266084fb206SMartin Fuzzey 			param->length, param->iterations);
2267084fb206SMartin Fuzzey 
2268084fb206SMartin Fuzzey 		retval = test_unaligned_bulk(
2269084fb206SMartin Fuzzey 				dev, dev->out_pipe,
2270084fb206SMartin Fuzzey 				param->length, param->iterations,
2271084fb206SMartin Fuzzey 				0, "test17");
2272084fb206SMartin Fuzzey 		break;
2273084fb206SMartin Fuzzey 
2274084fb206SMartin Fuzzey 	case 18:
2275084fb206SMartin Fuzzey 		if (dev->in_pipe == 0)
2276084fb206SMartin Fuzzey 			break;
2277084fb206SMartin Fuzzey 		dev_info(&intf->dev,
2278084fb206SMartin Fuzzey 			"TEST 18:  read odd addr %d bytes %u times core map\n",
2279084fb206SMartin Fuzzey 			param->length, param->iterations);
2280084fb206SMartin Fuzzey 
2281084fb206SMartin Fuzzey 		retval = test_unaligned_bulk(
2282084fb206SMartin Fuzzey 				dev, dev->in_pipe,
2283084fb206SMartin Fuzzey 				param->length, param->iterations,
2284084fb206SMartin Fuzzey 				0, "test18");
2285084fb206SMartin Fuzzey 		break;
2286084fb206SMartin Fuzzey 
2287084fb206SMartin Fuzzey 	/* Tests for bulk I/O using premapped coherent buffer and odd address */
2288084fb206SMartin Fuzzey 	case 19:
2289084fb206SMartin Fuzzey 		if (dev->out_pipe == 0)
2290084fb206SMartin Fuzzey 			break;
2291084fb206SMartin Fuzzey 		dev_info(&intf->dev,
2292084fb206SMartin Fuzzey 			"TEST 19:  write odd addr %d bytes %u times premapped\n",
2293084fb206SMartin Fuzzey 			param->length, param->iterations);
2294084fb206SMartin Fuzzey 
2295084fb206SMartin Fuzzey 		retval = test_unaligned_bulk(
2296084fb206SMartin Fuzzey 				dev, dev->out_pipe,
2297084fb206SMartin Fuzzey 				param->length, param->iterations,
2298084fb206SMartin Fuzzey 				URB_NO_TRANSFER_DMA_MAP, "test19");
2299084fb206SMartin Fuzzey 		break;
2300084fb206SMartin Fuzzey 
2301084fb206SMartin Fuzzey 	case 20:
2302084fb206SMartin Fuzzey 		if (dev->in_pipe == 0)
2303084fb206SMartin Fuzzey 			break;
2304084fb206SMartin Fuzzey 		dev_info(&intf->dev,
2305084fb206SMartin Fuzzey 			"TEST 20:  read odd addr %d bytes %u times premapped\n",
2306084fb206SMartin Fuzzey 			param->length, param->iterations);
2307084fb206SMartin Fuzzey 
2308084fb206SMartin Fuzzey 		retval = test_unaligned_bulk(
2309084fb206SMartin Fuzzey 				dev, dev->in_pipe,
2310084fb206SMartin Fuzzey 				param->length, param->iterations,
2311084fb206SMartin Fuzzey 				URB_NO_TRANSFER_DMA_MAP, "test20");
2312084fb206SMartin Fuzzey 		break;
2313084fb206SMartin Fuzzey 
2314084fb206SMartin Fuzzey 	/* control write tests with unaligned buffer */
2315084fb206SMartin Fuzzey 	case 21:
2316084fb206SMartin Fuzzey 		if (!dev->info->ctrl_out)
2317084fb206SMartin Fuzzey 			break;
2318084fb206SMartin Fuzzey 		dev_info(&intf->dev,
2319084fb206SMartin Fuzzey 				"TEST 21:  %d ep0out odd addr, %d..%d vary %d\n",
2320084fb206SMartin Fuzzey 				param->iterations,
2321084fb206SMartin Fuzzey 				realworld ? 1 : 0, param->length,
2322084fb206SMartin Fuzzey 				param->vary);
2323084fb206SMartin Fuzzey 		retval = ctrl_out(dev, param->iterations,
2324084fb206SMartin Fuzzey 				param->length, param->vary, 1);
2325084fb206SMartin Fuzzey 		break;
2326084fb206SMartin Fuzzey 
2327084fb206SMartin Fuzzey 	/* unaligned iso tests */
2328084fb206SMartin Fuzzey 	case 22:
2329084fb206SMartin Fuzzey 		if (dev->out_iso_pipe == 0 || param->sglen == 0)
2330084fb206SMartin Fuzzey 			break;
2331084fb206SMartin Fuzzey 		dev_info(&intf->dev,
2332084fb206SMartin Fuzzey 			"TEST 22:  write %d iso odd, %d entries of %d bytes\n",
2333084fb206SMartin Fuzzey 				param->iterations,
2334084fb206SMartin Fuzzey 				param->sglen, param->length);
2335084fb206SMartin Fuzzey 		retval = test_iso_queue(dev, param,
2336084fb206SMartin Fuzzey 				dev->out_iso_pipe, dev->iso_out, 1);
2337084fb206SMartin Fuzzey 		break;
2338084fb206SMartin Fuzzey 
2339084fb206SMartin Fuzzey 	case 23:
2340084fb206SMartin Fuzzey 		if (dev->in_iso_pipe == 0 || param->sglen == 0)
2341084fb206SMartin Fuzzey 			break;
2342084fb206SMartin Fuzzey 		dev_info(&intf->dev,
2343084fb206SMartin Fuzzey 			"TEST 23:  read %d iso odd, %d entries of %d bytes\n",
2344084fb206SMartin Fuzzey 				param->iterations,
2345084fb206SMartin Fuzzey 				param->sglen, param->length);
2346084fb206SMartin Fuzzey 		retval = test_iso_queue(dev, param,
2347084fb206SMartin Fuzzey 				dev->in_iso_pipe, dev->iso_in, 1);
2348084fb206SMartin Fuzzey 		break;
2349084fb206SMartin Fuzzey 
2350869410f8SAlan Stern 	/* unlink URBs from a bulk-OUT queue */
2351869410f8SAlan Stern 	case 24:
2352869410f8SAlan Stern 		if (dev->out_pipe == 0 || !param->length || param->sglen < 4)
2353869410f8SAlan Stern 			break;
2354869410f8SAlan Stern 		retval = 0;
23552cb50000SAlan Stern 		dev_info(&intf->dev, "TEST 24:  unlink from %d queues of "
2356869410f8SAlan Stern 				"%d %d-byte writes\n",
2357869410f8SAlan Stern 				param->iterations, param->sglen, param->length);
2358869410f8SAlan Stern 		for (i = param->iterations; retval == 0 && i > 0; --i) {
2359869410f8SAlan Stern 			retval = unlink_queued(dev, dev->out_pipe,
2360869410f8SAlan Stern 						param->sglen, param->length);
2361869410f8SAlan Stern 			if (retval) {
2362869410f8SAlan Stern 				dev_err(&intf->dev,
2363869410f8SAlan Stern 					"unlink queued writes failed %d, "
2364869410f8SAlan Stern 					"iterations left %d\n", retval, i);
2365869410f8SAlan Stern 				break;
2366869410f8SAlan Stern 			}
2367869410f8SAlan Stern 		}
2368869410f8SAlan Stern 		break;
2369869410f8SAlan Stern 
23701da177e4SLinus Torvalds 	}
23711da177e4SLinus Torvalds 	do_gettimeofday(&param->duration);
23721da177e4SLinus Torvalds 	param->duration.tv_sec -= start.tv_sec;
23731da177e4SLinus Torvalds 	param->duration.tv_usec -= start.tv_usec;
23741da177e4SLinus Torvalds 	if (param->duration.tv_usec < 0) {
23751da177e4SLinus Torvalds 		param->duration.tv_usec += 1000 * 1000;
23761da177e4SLinus Torvalds 		param->duration.tv_sec -= 1;
23771da177e4SLinus Torvalds 	}
23781cfab028SMatthias Kaehlcke 	mutex_unlock(&dev->lock);
23791da177e4SLinus Torvalds 	return retval;
23801da177e4SLinus Torvalds }
23811da177e4SLinus Torvalds 
23821da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
23831da177e4SLinus Torvalds 
2384fabbf219SMartin Fuzzey static unsigned force_interrupt;
23851da177e4SLinus Torvalds module_param(force_interrupt, uint, 0);
23861da177e4SLinus Torvalds MODULE_PARM_DESC(force_interrupt, "0 = test default; else interrupt");
23871da177e4SLinus Torvalds 
23881da177e4SLinus Torvalds #ifdef	GENERIC
23891da177e4SLinus Torvalds static unsigned short vendor;
23901da177e4SLinus Torvalds module_param(vendor, ushort, 0);
23911da177e4SLinus Torvalds MODULE_PARM_DESC(vendor, "vendor code (from usb-if)");
23921da177e4SLinus Torvalds 
23931da177e4SLinus Torvalds static unsigned short product;
23941da177e4SLinus Torvalds module_param(product, ushort, 0);
23951da177e4SLinus Torvalds MODULE_PARM_DESC(product, "product code (from vendor)");
23961da177e4SLinus Torvalds #endif
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds static int
23991da177e4SLinus Torvalds usbtest_probe(struct usb_interface *intf, const struct usb_device_id *id)
24001da177e4SLinus Torvalds {
24011da177e4SLinus Torvalds 	struct usb_device	*udev;
24021da177e4SLinus Torvalds 	struct usbtest_dev	*dev;
24031da177e4SLinus Torvalds 	struct usbtest_info	*info;
24041da177e4SLinus Torvalds 	char			*rtest, *wtest;
24051da177e4SLinus Torvalds 	char			*irtest, *iwtest;
24061da177e4SLinus Torvalds 
24071da177e4SLinus Torvalds 	udev = interface_to_usbdev(intf);
24081da177e4SLinus Torvalds 
24091da177e4SLinus Torvalds #ifdef	GENERIC
24101da177e4SLinus Torvalds 	/* specify devices by module parameters? */
24111da177e4SLinus Torvalds 	if (id->match_flags == 0) {
24121da177e4SLinus Torvalds 		/* vendor match required, product match optional */
24131da177e4SLinus Torvalds 		if (!vendor || le16_to_cpu(udev->descriptor.idVendor) != (u16)vendor)
24141da177e4SLinus Torvalds 			return -ENODEV;
24151da177e4SLinus Torvalds 		if (product && le16_to_cpu(udev->descriptor.idProduct) != (u16)product)
24161da177e4SLinus Torvalds 			return -ENODEV;
241728ffd79cSDavid Brownell 		dev_info(&intf->dev, "matched module params, "
241828ffd79cSDavid Brownell 					"vend=0x%04x prod=0x%04x\n",
24191da177e4SLinus Torvalds 				le16_to_cpu(udev->descriptor.idVendor),
24201da177e4SLinus Torvalds 				le16_to_cpu(udev->descriptor.idProduct));
24211da177e4SLinus Torvalds 	}
24221da177e4SLinus Torvalds #endif
24231da177e4SLinus Torvalds 
2424e94b1766SChristoph Lameter 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
24251da177e4SLinus Torvalds 	if (!dev)
24261da177e4SLinus Torvalds 		return -ENOMEM;
24271da177e4SLinus Torvalds 	info = (struct usbtest_info *) id->driver_info;
24281da177e4SLinus Torvalds 	dev->info = info;
24291cfab028SMatthias Kaehlcke 	mutex_init(&dev->lock);
24301da177e4SLinus Torvalds 
24311da177e4SLinus Torvalds 	dev->intf = intf;
24321da177e4SLinus Torvalds 
24331da177e4SLinus Torvalds 	/* cacheline-aligned scratch for i/o */
2434fabbf219SMartin Fuzzey 	dev->buf = kmalloc(TBUF_SIZE, GFP_KERNEL);
2435fabbf219SMartin Fuzzey 	if (dev->buf == NULL) {
24361da177e4SLinus Torvalds 		kfree(dev);
24371da177e4SLinus Torvalds 		return -ENOMEM;
24381da177e4SLinus Torvalds 	}
24391da177e4SLinus Torvalds 
24401da177e4SLinus Torvalds 	/* NOTE this doesn't yet test the handful of difference that are
24411da177e4SLinus Torvalds 	 * visible with high speed interrupts:  bigger maxpacket (1K) and
24421da177e4SLinus Torvalds 	 * "high bandwidth" modes (up to 3 packets/uframe).
24431da177e4SLinus Torvalds 	 */
24441da177e4SLinus Torvalds 	rtest = wtest = "";
24451da177e4SLinus Torvalds 	irtest = iwtest = "";
24461da177e4SLinus Torvalds 	if (force_interrupt || udev->speed == USB_SPEED_LOW) {
24471da177e4SLinus Torvalds 		if (info->ep_in) {
24481da177e4SLinus Torvalds 			dev->in_pipe = usb_rcvintpipe(udev, info->ep_in);
24491da177e4SLinus Torvalds 			rtest = " intr-in";
24501da177e4SLinus Torvalds 		}
24511da177e4SLinus Torvalds 		if (info->ep_out) {
24521da177e4SLinus Torvalds 			dev->out_pipe = usb_sndintpipe(udev, info->ep_out);
24531da177e4SLinus Torvalds 			wtest = " intr-out";
24541da177e4SLinus Torvalds 		}
24551da177e4SLinus Torvalds 	} else {
2456d0b4652fSAlan Stern 		if (override_alt >= 0 || info->autoconf) {
24571da177e4SLinus Torvalds 			int status;
24581da177e4SLinus Torvalds 
24591da177e4SLinus Torvalds 			status = get_endpoints(dev, intf);
24601da177e4SLinus Torvalds 			if (status < 0) {
2461b6c63937SArjan van de Ven 				WARNING(dev, "couldn't get endpoints, %d\n",
246228ffd79cSDavid Brownell 						status);
2463f4a728d0SJulia Lawall 				kfree(dev->buf);
2464f4a728d0SJulia Lawall 				kfree(dev);
24651da177e4SLinus Torvalds 				return status;
24661da177e4SLinus Torvalds 			}
24671da177e4SLinus Torvalds 			/* may find bulk or ISO pipes */
24681da177e4SLinus Torvalds 		} else {
24691da177e4SLinus Torvalds 			if (info->ep_in)
24701da177e4SLinus Torvalds 				dev->in_pipe = usb_rcvbulkpipe(udev,
24711da177e4SLinus Torvalds 							info->ep_in);
24721da177e4SLinus Torvalds 			if (info->ep_out)
24731da177e4SLinus Torvalds 				dev->out_pipe = usb_sndbulkpipe(udev,
24741da177e4SLinus Torvalds 							info->ep_out);
24751da177e4SLinus Torvalds 		}
24761da177e4SLinus Torvalds 		if (dev->in_pipe)
24771da177e4SLinus Torvalds 			rtest = " bulk-in";
24781da177e4SLinus Torvalds 		if (dev->out_pipe)
24791da177e4SLinus Torvalds 			wtest = " bulk-out";
24801da177e4SLinus Torvalds 		if (dev->in_iso_pipe)
24811da177e4SLinus Torvalds 			irtest = " iso-in";
24821da177e4SLinus Torvalds 		if (dev->out_iso_pipe)
24831da177e4SLinus Torvalds 			iwtest = " iso-out";
24841da177e4SLinus Torvalds 	}
24851da177e4SLinus Torvalds 
24861da177e4SLinus Torvalds 	usb_set_intfdata(intf, dev);
24871da177e4SLinus Torvalds 	dev_info(&intf->dev, "%s\n", info->name);
2488e538dfdaSMichal Nazarewicz 	dev_info(&intf->dev, "%s {control%s%s%s%s%s} tests%s\n",
2489e538dfdaSMichal Nazarewicz 			usb_speed_string(udev->speed),
24901da177e4SLinus Torvalds 			info->ctrl_out ? " in/out" : "",
24911da177e4SLinus Torvalds 			rtest, wtest,
24921da177e4SLinus Torvalds 			irtest, iwtest,
24931da177e4SLinus Torvalds 			info->alt >= 0 ? " (+alt)" : "");
24941da177e4SLinus Torvalds 	return 0;
24951da177e4SLinus Torvalds }
24961da177e4SLinus Torvalds 
2497ff7c79e4SDavid Brownell static int usbtest_suspend(struct usb_interface *intf, pm_message_t message)
2498ff7c79e4SDavid Brownell {
2499ff7c79e4SDavid Brownell 	return 0;
2500ff7c79e4SDavid Brownell }
2501ff7c79e4SDavid Brownell 
2502ff7c79e4SDavid Brownell static int usbtest_resume(struct usb_interface *intf)
2503ff7c79e4SDavid Brownell {
2504ff7c79e4SDavid Brownell 	return 0;
2505ff7c79e4SDavid Brownell }
2506ff7c79e4SDavid Brownell 
2507ff7c79e4SDavid Brownell 
25081da177e4SLinus Torvalds static void usbtest_disconnect(struct usb_interface *intf)
25091da177e4SLinus Torvalds {
25101da177e4SLinus Torvalds 	struct usbtest_dev	*dev = usb_get_intfdata(intf);
25111da177e4SLinus Torvalds 
25121da177e4SLinus Torvalds 	usb_set_intfdata(intf, NULL);
25131da177e4SLinus Torvalds 	dev_dbg(&intf->dev, "disconnect\n");
25141da177e4SLinus Torvalds 	kfree(dev);
25151da177e4SLinus Torvalds }
25161da177e4SLinus Torvalds 
25171da177e4SLinus Torvalds /* Basic testing only needs a device that can source or sink bulk traffic.
25181da177e4SLinus Torvalds  * Any device can test control transfers (default with GENERIC binding).
25191da177e4SLinus Torvalds  *
25201da177e4SLinus Torvalds  * Several entries work with the default EP0 implementation that's built
25211da177e4SLinus Torvalds  * into EZ-USB chips.  There's a default vendor ID which can be overridden
25221da177e4SLinus Torvalds  * by (very) small config EEPROMS, but otherwise all these devices act
25231da177e4SLinus Torvalds  * identically until firmware is loaded:  only EP0 works.  It turns out
25241da177e4SLinus Torvalds  * to be easy to make other endpoints work, without modifying that EP0
25251da177e4SLinus Torvalds  * behavior.  For now, we expect that kind of firmware.
25261da177e4SLinus Torvalds  */
25271da177e4SLinus Torvalds 
25281da177e4SLinus Torvalds /* an21xx or fx versions of ez-usb */
25291da177e4SLinus Torvalds static struct usbtest_info ez1_info = {
25301da177e4SLinus Torvalds 	.name		= "EZ-USB device",
25311da177e4SLinus Torvalds 	.ep_in		= 2,
25321da177e4SLinus Torvalds 	.ep_out		= 2,
25331da177e4SLinus Torvalds 	.alt		= 1,
25341da177e4SLinus Torvalds };
25351da177e4SLinus Torvalds 
25361da177e4SLinus Torvalds /* fx2 version of ez-usb */
25371da177e4SLinus Torvalds static struct usbtest_info ez2_info = {
25381da177e4SLinus Torvalds 	.name		= "FX2 device",
25391da177e4SLinus Torvalds 	.ep_in		= 6,
25401da177e4SLinus Torvalds 	.ep_out		= 2,
25411da177e4SLinus Torvalds 	.alt		= 1,
25421da177e4SLinus Torvalds };
25431da177e4SLinus Torvalds 
25441da177e4SLinus Torvalds /* ezusb family device with dedicated usb test firmware,
25451da177e4SLinus Torvalds  */
25461da177e4SLinus Torvalds static struct usbtest_info fw_info = {
25471da177e4SLinus Torvalds 	.name		= "usb test device",
25481da177e4SLinus Torvalds 	.ep_in		= 2,
25491da177e4SLinus Torvalds 	.ep_out		= 2,
25501da177e4SLinus Torvalds 	.alt		= 1,
2551fabbf219SMartin Fuzzey 	.autoconf	= 1,		/* iso and ctrl_out need autoconf */
25521da177e4SLinus Torvalds 	.ctrl_out	= 1,
2553fabbf219SMartin Fuzzey 	.iso		= 1,		/* iso_ep's are #8 in/out */
25541da177e4SLinus Torvalds };
25551da177e4SLinus Torvalds 
25561da177e4SLinus Torvalds /* peripheral running Linux and 'zero.c' test firmware, or
25571da177e4SLinus Torvalds  * its user-mode cousin. different versions of this use
25581da177e4SLinus Torvalds  * different hardware with the same vendor/product codes.
25591da177e4SLinus Torvalds  * host side MUST rely on the endpoint descriptors.
25601da177e4SLinus Torvalds  */
25611da177e4SLinus Torvalds static struct usbtest_info gz_info = {
25621da177e4SLinus Torvalds 	.name		= "Linux gadget zero",
25631da177e4SLinus Torvalds 	.autoconf	= 1,
25641da177e4SLinus Torvalds 	.ctrl_out	= 1,
25654b85c624SBoyan Nedeltchev 	.iso		= 1,
25661da177e4SLinus Torvalds 	.alt		= 0,
25671da177e4SLinus Torvalds };
25681da177e4SLinus Torvalds 
25691da177e4SLinus Torvalds static struct usbtest_info um_info = {
25701da177e4SLinus Torvalds 	.name		= "Linux user mode test driver",
25711da177e4SLinus Torvalds 	.autoconf	= 1,
25721da177e4SLinus Torvalds 	.alt		= -1,
25731da177e4SLinus Torvalds };
25741da177e4SLinus Torvalds 
25751da177e4SLinus Torvalds static struct usbtest_info um2_info = {
25761da177e4SLinus Torvalds 	.name		= "Linux user mode ISO test driver",
25771da177e4SLinus Torvalds 	.autoconf	= 1,
25781da177e4SLinus Torvalds 	.iso		= 1,
25791da177e4SLinus Torvalds 	.alt		= -1,
25801da177e4SLinus Torvalds };
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds #ifdef IBOT2
25831da177e4SLinus Torvalds /* this is a nice source of high speed bulk data;
25841da177e4SLinus Torvalds  * uses an FX2, with firmware provided in the device
25851da177e4SLinus Torvalds  */
25861da177e4SLinus Torvalds static struct usbtest_info ibot2_info = {
25871da177e4SLinus Torvalds 	.name		= "iBOT2 webcam",
25881da177e4SLinus Torvalds 	.ep_in		= 2,
25891da177e4SLinus Torvalds 	.alt		= -1,
25901da177e4SLinus Torvalds };
25911da177e4SLinus Torvalds #endif
25921da177e4SLinus Torvalds 
25931da177e4SLinus Torvalds #ifdef GENERIC
25941da177e4SLinus Torvalds /* we can use any device to test control traffic */
25951da177e4SLinus Torvalds static struct usbtest_info generic_info = {
25961da177e4SLinus Torvalds 	.name		= "Generic USB device",
25971da177e4SLinus Torvalds 	.alt		= -1,
25981da177e4SLinus Torvalds };
25991da177e4SLinus Torvalds #endif
26001da177e4SLinus Torvalds 
26011da177e4SLinus Torvalds 
260233b9e162SNémeth Márton static const struct usb_device_id id_table[] = {
26031da177e4SLinus Torvalds 
26041da177e4SLinus Torvalds 	/*-------------------------------------------------------------*/
26051da177e4SLinus Torvalds 
26061da177e4SLinus Torvalds 	/* EZ-USB devices which download firmware to replace (or in our
26071da177e4SLinus Torvalds 	 * case augment) the default device implementation.
26081da177e4SLinus Torvalds 	 */
26091da177e4SLinus Torvalds 
26101da177e4SLinus Torvalds 	/* generic EZ-USB FX controller */
26111da177e4SLinus Torvalds 	{ USB_DEVICE(0x0547, 0x2235),
26121da177e4SLinus Torvalds 		.driver_info = (unsigned long) &ez1_info,
26131da177e4SLinus Torvalds 	},
26141da177e4SLinus Torvalds 
26151da177e4SLinus Torvalds 	/* CY3671 development board with EZ-USB FX */
26161da177e4SLinus Torvalds 	{ USB_DEVICE(0x0547, 0x0080),
26171da177e4SLinus Torvalds 		.driver_info = (unsigned long) &ez1_info,
26181da177e4SLinus Torvalds 	},
26191da177e4SLinus Torvalds 
26201da177e4SLinus Torvalds 	/* generic EZ-USB FX2 controller (or development board) */
26211da177e4SLinus Torvalds 	{ USB_DEVICE(0x04b4, 0x8613),
26221da177e4SLinus Torvalds 		.driver_info = (unsigned long) &ez2_info,
26231da177e4SLinus Torvalds 	},
26241da177e4SLinus Torvalds 
26251da177e4SLinus Torvalds 	/* re-enumerated usb test device firmware */
26261da177e4SLinus Torvalds 	{ USB_DEVICE(0xfff0, 0xfff0),
26271da177e4SLinus Torvalds 		.driver_info = (unsigned long) &fw_info,
26281da177e4SLinus Torvalds 	},
26291da177e4SLinus Torvalds 
26301da177e4SLinus Torvalds 	/* "Gadget Zero" firmware runs under Linux */
26311da177e4SLinus Torvalds 	{ USB_DEVICE(0x0525, 0xa4a0),
26321da177e4SLinus Torvalds 		.driver_info = (unsigned long) &gz_info,
26331da177e4SLinus Torvalds 	},
26341da177e4SLinus Torvalds 
26351da177e4SLinus Torvalds 	/* so does a user-mode variant */
26361da177e4SLinus Torvalds 	{ USB_DEVICE(0x0525, 0xa4a4),
26371da177e4SLinus Torvalds 		.driver_info = (unsigned long) &um_info,
26381da177e4SLinus Torvalds 	},
26391da177e4SLinus Torvalds 
26401da177e4SLinus Torvalds 	/* ... and a user-mode variant that talks iso */
26411da177e4SLinus Torvalds 	{ USB_DEVICE(0x0525, 0xa4a3),
26421da177e4SLinus Torvalds 		.driver_info = (unsigned long) &um2_info,
26431da177e4SLinus Torvalds 	},
26441da177e4SLinus Torvalds 
26451da177e4SLinus Torvalds #ifdef KEYSPAN_19Qi
26461da177e4SLinus Torvalds 	/* Keyspan 19qi uses an21xx (original EZ-USB) */
2647fabbf219SMartin Fuzzey 	/* this does not coexist with the real Keyspan 19qi driver! */
26481da177e4SLinus Torvalds 	{ USB_DEVICE(0x06cd, 0x010b),
26491da177e4SLinus Torvalds 		.driver_info = (unsigned long) &ez1_info,
26501da177e4SLinus Torvalds 	},
26511da177e4SLinus Torvalds #endif
26521da177e4SLinus Torvalds 
26531da177e4SLinus Torvalds 	/*-------------------------------------------------------------*/
26541da177e4SLinus Torvalds 
26551da177e4SLinus Torvalds #ifdef IBOT2
26561da177e4SLinus Torvalds 	/* iBOT2 makes a nice source of high speed bulk-in data */
2657fabbf219SMartin Fuzzey 	/* this does not coexist with a real iBOT2 driver! */
26581da177e4SLinus Torvalds 	{ USB_DEVICE(0x0b62, 0x0059),
26591da177e4SLinus Torvalds 		.driver_info = (unsigned long) &ibot2_info,
26601da177e4SLinus Torvalds 	},
26611da177e4SLinus Torvalds #endif
26621da177e4SLinus Torvalds 
26631da177e4SLinus Torvalds 	/*-------------------------------------------------------------*/
26641da177e4SLinus Torvalds 
26651da177e4SLinus Torvalds #ifdef GENERIC
26661da177e4SLinus Torvalds 	/* module params can specify devices to use for control tests */
26671da177e4SLinus Torvalds 	{ .driver_info = (unsigned long) &generic_info, },
26681da177e4SLinus Torvalds #endif
26691da177e4SLinus Torvalds 
26701da177e4SLinus Torvalds 	/*-------------------------------------------------------------*/
26711da177e4SLinus Torvalds 
26721da177e4SLinus Torvalds 	{ }
26731da177e4SLinus Torvalds };
26741da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, id_table);
26751da177e4SLinus Torvalds 
26761da177e4SLinus Torvalds static struct usb_driver usbtest_driver = {
26771da177e4SLinus Torvalds 	.name =		"usbtest",
26781da177e4SLinus Torvalds 	.id_table =	id_table,
26791da177e4SLinus Torvalds 	.probe =	usbtest_probe,
2680c532b29aSAndi Kleen 	.unlocked_ioctl = usbtest_ioctl,
26811da177e4SLinus Torvalds 	.disconnect =	usbtest_disconnect,
2682ff7c79e4SDavid Brownell 	.suspend =	usbtest_suspend,
2683ff7c79e4SDavid Brownell 	.resume =	usbtest_resume,
26841da177e4SLinus Torvalds };
26851da177e4SLinus Torvalds 
26861da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
26871da177e4SLinus Torvalds 
26881da177e4SLinus Torvalds static int __init usbtest_init(void)
26891da177e4SLinus Torvalds {
26901da177e4SLinus Torvalds #ifdef GENERIC
26911da177e4SLinus Torvalds 	if (vendor)
269228ffd79cSDavid Brownell 		pr_debug("params: vend=0x%04x prod=0x%04x\n", vendor, product);
26931da177e4SLinus Torvalds #endif
26941da177e4SLinus Torvalds 	return usb_register(&usbtest_driver);
26951da177e4SLinus Torvalds }
26961da177e4SLinus Torvalds module_init(usbtest_init);
26971da177e4SLinus Torvalds 
26981da177e4SLinus Torvalds static void __exit usbtest_exit(void)
26991da177e4SLinus Torvalds {
27001da177e4SLinus Torvalds 	usb_deregister(&usbtest_driver);
27011da177e4SLinus Torvalds }
27021da177e4SLinus Torvalds module_exit(usbtest_exit);
27031da177e4SLinus Torvalds 
27041da177e4SLinus Torvalds MODULE_DESCRIPTION("USB Core/HCD Testing Driver");
27051da177e4SLinus Torvalds MODULE_LICENSE("GPL");
27061da177e4SLinus Torvalds 
2707