usbtest.c 64.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
8
#include <linux/scatterlist.h>
9
#include <linux/mutex.h>
L
Linus Torvalds 已提交
10 11 12 13 14 15

#include <linux/usb.h>


/*-------------------------------------------------------------------------*/

M
Martin Fuzzey 已提交
16
/* FIXME make these public somewhere; usbdevfs.h? */
L
Linus Torvalds 已提交
17
struct usbtest_param {
M
Martin Fuzzey 已提交
18
	/* inputs */
L
Linus Torvalds 已提交
19 20 21 22 23 24
	unsigned		test_num;	/* 0..(TEST_CASES-1) */
	unsigned		iterations;
	unsigned		length;
	unsigned		vary;
	unsigned		sglen;

M
Martin Fuzzey 已提交
25
	/* outputs */
L
Linus Torvalds 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	struct timeval		duration;
};
#define USBTEST_REQUEST	_IOWR('U', 100, struct usbtest_param)

/*-------------------------------------------------------------------------*/

#define	GENERIC		/* let probe() bind using module params */

/* Some devices that can be used for testing will have "real" drivers.
 * Entries for those need to be enabled here by hand, after disabling
 * that "real" driver.
 */
//#define	IBOT2		/* grab iBOT2 webcams */
//#define	KEYSPAN_19Qi	/* grab un-renumerated serial adapter */

/*-------------------------------------------------------------------------*/

struct usbtest_info {
	const char		*name;
	u8			ep_in;		/* bulk/intr source */
	u8			ep_out;		/* bulk/intr sink */
M
Martin Fuzzey 已提交
47 48 49
	unsigned		autoconf:1;
	unsigned		ctrl_out:1;
	unsigned		iso:1;		/* try iso in/out */
L
Linus Torvalds 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
	int			alt;
};

/* this is accessed only through usbfs ioctl calls.
 * one ioctl to issue a test ... one lock per device.
 * tests create other threads if they need them.
 * urbs and buffers are allocated dynamically,
 * and data generated deterministically.
 */
struct usbtest_dev {
	struct usb_interface	*intf;
	struct usbtest_info	*info;
	int			in_pipe;
	int			out_pipe;
	int			in_iso_pipe;
	int			out_iso_pipe;
	struct usb_endpoint_descriptor	*iso_in, *iso_out;
67
	struct mutex		lock;
L
Linus Torvalds 已提交
68 69 70 71 72

#define TBUF_SIZE	256
	u8			*buf;
};

M
Martin Fuzzey 已提交
73
static struct usb_device *testdev_to_usbdev(struct usbtest_dev *test)
L
Linus Torvalds 已提交
74
{
M
Martin Fuzzey 已提交
75
	return interface_to_usbdev(test->intf);
L
Linus Torvalds 已提交
76 77 78 79 80
}

/* set up all urbs so they can be used with either bulk or interrupt */
#define	INTERRUPT_RATE		1	/* msec/transfer */

81 82
#define ERROR(tdev, fmt, args...) \
	dev_err(&(tdev)->intf->dev , fmt , ## args)
83
#define WARNING(tdev, fmt, args...) \
84
	dev_warn(&(tdev)->intf->dev , fmt , ## args)
L
Linus Torvalds 已提交
85

86 87
#define GUARD_BYTE	0xA5

L
Linus Torvalds 已提交
88 89 90
/*-------------------------------------------------------------------------*/

static int
M
Martin Fuzzey 已提交
91
get_endpoints(struct usbtest_dev *dev, struct usb_interface *intf)
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
{
	int				tmp;
	struct usb_host_interface	*alt;
	struct usb_host_endpoint	*in, *out;
	struct usb_host_endpoint	*iso_in, *iso_out;
	struct usb_device		*udev;

	for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
		unsigned	ep;

		in = out = NULL;
		iso_in = iso_out = NULL;
		alt = intf->altsetting + tmp;

		/* take the first altsetting with in-bulk + out-bulk;
107
		 * ignore other endpoints and altsettings.
L
Linus Torvalds 已提交
108 109 110 111 112 113 114 115 116 117 118
		 */
		for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
			struct usb_host_endpoint	*e;

			e = alt->endpoint + ep;
			switch (e->desc.bmAttributes) {
			case USB_ENDPOINT_XFER_BULK:
				break;
			case USB_ENDPOINT_XFER_ISOC:
				if (dev->info->iso)
					goto try_iso;
M
Martin Fuzzey 已提交
119
				/* FALLTHROUGH */
L
Linus Torvalds 已提交
120 121 122
			default:
				continue;
			}
123
			if (usb_endpoint_dir_in(&e->desc)) {
L
Linus Torvalds 已提交
124 125 126 127 128 129 130 131
				if (!in)
					in = e;
			} else {
				if (!out)
					out = e;
			}
			continue;
try_iso:
132
			if (usb_endpoint_dir_in(&e->desc)) {
L
Linus Torvalds 已提交
133 134 135 136 137 138 139
				if (!iso_in)
					iso_in = e;
			} else {
				if (!iso_out)
					iso_out = e;
			}
		}
140
		if ((in && out)  ||  iso_in || iso_out)
L
Linus Torvalds 已提交
141 142 143 144 145
			goto found;
	}
	return -EINVAL;

found:
M
Martin Fuzzey 已提交
146
	udev = testdev_to_usbdev(dev);
L
Linus Torvalds 已提交
147
	if (alt->desc.bAlternateSetting != 0) {
M
Martin Fuzzey 已提交
148
		tmp = usb_set_interface(udev,
L
Linus Torvalds 已提交
149 150 151 152 153 154 155
				alt->desc.bInterfaceNumber,
				alt->desc.bAlternateSetting);
		if (tmp < 0)
			return tmp;
	}

	if (in) {
M
Martin Fuzzey 已提交
156
		dev->in_pipe = usb_rcvbulkpipe(udev,
L
Linus Torvalds 已提交
157
			in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
M
Martin Fuzzey 已提交
158
		dev->out_pipe = usb_sndbulkpipe(udev,
L
Linus Torvalds 已提交
159 160 161 162
			out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
	}
	if (iso_in) {
		dev->iso_in = &iso_in->desc;
M
Martin Fuzzey 已提交
163
		dev->in_iso_pipe = usb_rcvisocpipe(udev,
L
Linus Torvalds 已提交
164 165
				iso_in->desc.bEndpointAddress
					& USB_ENDPOINT_NUMBER_MASK);
166 167 168
	}

	if (iso_out) {
L
Linus Torvalds 已提交
169
		dev->iso_out = &iso_out->desc;
M
Martin Fuzzey 已提交
170
		dev->out_iso_pipe = usb_sndisocpipe(udev,
L
Linus Torvalds 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
				iso_out->desc.bEndpointAddress
					& USB_ENDPOINT_NUMBER_MASK);
	}
	return 0;
}

/*-------------------------------------------------------------------------*/

/* Support for testing basic non-queued I/O streams.
 *
 * These just package urbs as requests that can be easily canceled.
 * Each urb's data buffer is dynamically allocated; callers can fill
 * them with non-zero test data (or test for it) when appropriate.
 */

M
Martin Fuzzey 已提交
186
static void simple_callback(struct urb *urb)
L
Linus Torvalds 已提交
187
{
188
	complete(urb->context);
L
Linus Torvalds 已提交
189 190
}

191
static struct urb *usbtest_alloc_urb(
L
Linus Torvalds 已提交
192 193
	struct usb_device	*udev,
	int			pipe,
194 195 196
	unsigned long		bytes,
	unsigned		transfer_flags,
	unsigned		offset)
L
Linus Torvalds 已提交
197 198 199
{
	struct urb		*urb;

M
Martin Fuzzey 已提交
200
	urb = usb_alloc_urb(0, GFP_KERNEL);
L
Linus Torvalds 已提交
201 202
	if (!urb)
		return urb;
M
Martin Fuzzey 已提交
203
	usb_fill_bulk_urb(urb, udev, pipe, NULL, bytes, simple_callback, NULL);
L
Linus Torvalds 已提交
204 205 206
	urb->interval = (udev->speed == USB_SPEED_HIGH)
			? (INTERRUPT_RATE << 3)
			: INTERRUPT_RATE;
207
	urb->transfer_flags = transfer_flags;
M
Martin Fuzzey 已提交
208
	if (usb_pipein(pipe))
L
Linus Torvalds 已提交
209
		urb->transfer_flags |= URB_SHORT_NOT_OK;
210 211 212 213 214 215 216

	if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
		urb->transfer_buffer = usb_alloc_coherent(udev, bytes + offset,
			GFP_KERNEL, &urb->transfer_dma);
	else
		urb->transfer_buffer = kmalloc(bytes + offset, GFP_KERNEL);

L
Linus Torvalds 已提交
217
	if (!urb->transfer_buffer) {
M
Martin Fuzzey 已提交
218
		usb_free_urb(urb);
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
		return NULL;
	}

	/* To test unaligned transfers add an offset and fill the
		unused memory with a guard value */
	if (offset) {
		memset(urb->transfer_buffer, GUARD_BYTE, offset);
		urb->transfer_buffer += offset;
		if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
			urb->transfer_dma += offset;
	}

	/* For inbound transfers use guard byte so that test fails if
		data not correctly copied */
	memset(urb->transfer_buffer,
			usb_pipein(urb->pipe) ? GUARD_BYTE : 0,
			bytes);
L
Linus Torvalds 已提交
236 237 238
	return urb;
}

239 240 241 242 243 244 245 246
static struct urb *simple_alloc_urb(
	struct usb_device	*udev,
	int			pipe,
	unsigned long		bytes)
{
	return usbtest_alloc_urb(udev, pipe, bytes, URB_NO_TRANSFER_DMA_MAP, 0);
}

M
Martin Fuzzey 已提交
247
static unsigned pattern;
248 249 250
static unsigned mod_pattern;
module_param_named(pattern, mod_pattern, uint, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(mod_pattern, "i/o pattern (0 == zeroes)");
L
Linus Torvalds 已提交
251

M
Martin Fuzzey 已提交
252
static inline void simple_fill_buf(struct urb *urb)
L
Linus Torvalds 已提交
253 254 255 256 257 258 259
{
	unsigned	i;
	u8		*buf = urb->transfer_buffer;
	unsigned	len = urb->transfer_buffer_length;

	switch (pattern) {
	default:
M
Martin Fuzzey 已提交
260
		/* FALLTHROUGH */
L
Linus Torvalds 已提交
261
	case 0:
M
Martin Fuzzey 已提交
262
		memset(buf, 0, len);
L
Linus Torvalds 已提交
263 264 265 266 267 268 269 270
		break;
	case 1:			/* mod63 */
		for (i = 0; i < len; i++)
			*buf++ = (u8) (i % 63);
		break;
	}
}

271
static inline unsigned long buffer_offset(void *buf)
272
{
273
	return (unsigned long)buf & (ARCH_KMALLOC_MINALIGN - 1);
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
}

static int check_guard_bytes(struct usbtest_dev *tdev, struct urb *urb)
{
	u8 *buf = urb->transfer_buffer;
	u8 *guard = buf - buffer_offset(buf);
	unsigned i;

	for (i = 0; guard < buf; i++, guard++) {
		if (*guard != GUARD_BYTE) {
			ERROR(tdev, "guard byte[%d] %d (not %d)\n",
				i, *guard, GUARD_BYTE);
			return -EINVAL;
		}
	}
	return 0;
}

static int simple_check_buf(struct usbtest_dev *tdev, struct urb *urb)
L
Linus Torvalds 已提交
293 294 295 296 297 298
{
	unsigned	i;
	u8		expected;
	u8		*buf = urb->transfer_buffer;
	unsigned	len = urb->actual_length;

299 300 301 302
	int ret = check_guard_bytes(tdev, urb);
	if (ret)
		return ret;

L
Linus Torvalds 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
	for (i = 0; i < len; i++, buf++) {
		switch (pattern) {
		/* all-zeroes has no synchronization issues */
		case 0:
			expected = 0;
			break;
		/* mod63 stays in sync with short-terminated transfers,
		 * or otherwise when host and gadget agree on how large
		 * each usb transfer request should be.  resync is done
		 * with set_interface or set_config.
		 */
		case 1:			/* mod63 */
			expected = i % 63;
			break;
		/* always fail unsupported patterns */
		default:
			expected = !*buf;
			break;
		}
		if (*buf == expected)
			continue;
324
		ERROR(tdev, "buf[%d] = %d (not %d)\n", i, *buf, expected);
L
Linus Torvalds 已提交
325 326 327 328 329
		return -EINVAL;
	}
	return 0;
}

M
Martin Fuzzey 已提交
330
static void simple_free_urb(struct urb *urb)
L
Linus Torvalds 已提交
331
{
332
	unsigned long offset = buffer_offset(urb->transfer_buffer);
333 334 335 336 337 338 339 340 341

	if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
		usb_free_coherent(
			urb->dev,
			urb->transfer_buffer_length + offset,
			urb->transfer_buffer - offset,
			urb->transfer_dma - offset);
	else
		kfree(urb->transfer_buffer - offset);
M
Martin Fuzzey 已提交
342
	usb_free_urb(urb);
L
Linus Torvalds 已提交
343 344
}

M
Martin Fuzzey 已提交
345
static int simple_io(
346
	struct usbtest_dev	*tdev,
L
Linus Torvalds 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360
	struct urb		*urb,
	int			iterations,
	int			vary,
	int			expected,
	const char		*label
)
{
	struct usb_device	*udev = urb->dev;
	int			max = urb->transfer_buffer_length;
	struct completion	completion;
	int			retval = 0;

	urb->context = &completion;
	while (retval == 0 && iterations-- > 0) {
M
Martin Fuzzey 已提交
361
		init_completion(&completion);
362
		if (usb_pipeout(urb->pipe)) {
M
Martin Fuzzey 已提交
363
			simple_fill_buf(urb);
364 365
			urb->transfer_flags |= URB_ZERO_PACKET;
		}
M
Martin Fuzzey 已提交
366 367
		retval = usb_submit_urb(urb, GFP_KERNEL);
		if (retval != 0)
L
Linus Torvalds 已提交
368 369 370
			break;

		/* NOTE:  no timeouts; can't be broken out of by interrupt */
M
Martin Fuzzey 已提交
371
		wait_for_completion(&completion);
L
Linus Torvalds 已提交
372 373
		retval = urb->status;
		urb->dev = udev;
M
Martin Fuzzey 已提交
374
		if (retval == 0 && usb_pipein(urb->pipe))
375
			retval = simple_check_buf(tdev, urb);
L
Linus Torvalds 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

		if (vary) {
			int	len = urb->transfer_buffer_length;

			len += vary;
			len %= max;
			if (len == 0)
				len = (vary < max) ? vary : max;
			urb->transfer_buffer_length = len;
		}

		/* FIXME if endpoint halted, clear halt (and log) */
	}
	urb->transfer_buffer_length = max;

	if (expected != retval)
392
		dev_err(&udev->dev,
L
Linus Torvalds 已提交
393 394 395 396 397 398 399 400 401 402 403 404
			"%s failed, iterations left %d, status %d (not %d)\n",
				label, iterations, retval, expected);
	return retval;
}


/*-------------------------------------------------------------------------*/

/* We use scatterlist primitives to test queued I/O.
 * Yes, this also tests the scatterlist primitives.
 */

M
Martin Fuzzey 已提交
405
static void free_sglist(struct scatterlist *sg, int nents)
L
Linus Torvalds 已提交
406 407
{
	unsigned		i;
408

L
Linus Torvalds 已提交
409 410 411
	if (!sg)
		return;
	for (i = 0; i < nents; i++) {
J
Jens Axboe 已提交
412
		if (!sg_page(&sg[i]))
L
Linus Torvalds 已提交
413
			continue;
M
Martin Fuzzey 已提交
414
		kfree(sg_virt(&sg[i]));
L
Linus Torvalds 已提交
415
	}
M
Martin Fuzzey 已提交
416
	kfree(sg);
L
Linus Torvalds 已提交
417 418 419
}

static struct scatterlist *
M
Martin Fuzzey 已提交
420
alloc_sglist(int nents, int max, int vary)
L
Linus Torvalds 已提交
421 422 423 424 425
{
	struct scatterlist	*sg;
	unsigned		i;
	unsigned		size = max;

426 427 428
	if (max == 0)
		return NULL;

429
	sg = kmalloc_array(nents, sizeof *sg, GFP_KERNEL);
L
Linus Torvalds 已提交
430 431
	if (!sg)
		return NULL;
432
	sg_init_table(sg, nents);
L
Linus Torvalds 已提交
433 434 435

	for (i = 0; i < nents; i++) {
		char		*buf;
436
		unsigned	j;
L
Linus Torvalds 已提交
437

M
Martin Fuzzey 已提交
438
		buf = kzalloc(size, GFP_KERNEL);
L
Linus Torvalds 已提交
439
		if (!buf) {
M
Martin Fuzzey 已提交
440
			free_sglist(sg, i);
L
Linus Torvalds 已提交
441 442 443 444
			return NULL;
		}

		/* kmalloc pages are always physically contiguous! */
445
		sg_set_buf(&sg[i], buf, size);
L
Linus Torvalds 已提交
446

447 448 449 450 451 452 453 454 455 456
		switch (pattern) {
		case 0:
			/* already zeroed */
			break;
		case 1:
			for (j = 0; j < size; j++)
				*buf++ = (u8) (j % 63);
			break;
		}

L
Linus Torvalds 已提交
457 458 459 460 461 462 463 464 465 466 467
		if (vary) {
			size += vary;
			size %= max;
			if (size == 0)
				size = (vary < max) ? vary : max;
		}
	}

	return sg;
}

M
Martin Fuzzey 已提交
468
static int perform_sglist(
469
	struct usbtest_dev	*tdev,
L
Linus Torvalds 已提交
470 471 472 473 474 475 476
	unsigned		iterations,
	int			pipe,
	struct usb_sg_request	*req,
	struct scatterlist	*sg,
	int			nents
)
{
477
	struct usb_device	*udev = testdev_to_usbdev(tdev);
L
Linus Torvalds 已提交
478 479 480
	int			retval = 0;

	while (retval == 0 && iterations-- > 0) {
M
Martin Fuzzey 已提交
481
		retval = usb_sg_init(req, udev, pipe,
L
Linus Torvalds 已提交
482 483 484
				(udev->speed == USB_SPEED_HIGH)
					? (INTERRUPT_RATE << 3)
					: INTERRUPT_RATE,
485
				sg, nents, 0, GFP_KERNEL);
486

L
Linus Torvalds 已提交
487 488
		if (retval)
			break;
M
Martin Fuzzey 已提交
489
		usb_sg_wait(req);
L
Linus Torvalds 已提交
490 491
		retval = req->status;

492 493
		/* FIXME check resulting data pattern */

L
Linus Torvalds 已提交
494 495 496
		/* FIXME if endpoint halted, clear halt (and log) */
	}

M
Martin Fuzzey 已提交
497 498 499
	/* FIXME for unlink or fault handling tests, don't report
	 * failure if retval is as we expected ...
	 */
L
Linus Torvalds 已提交
500
	if (retval)
501 502
		ERROR(tdev, "perform_sglist failed, "
				"iterations left %d, status %d\n",
L
Linus Torvalds 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
				iterations, retval);
	return retval;
}


/*-------------------------------------------------------------------------*/

/* unqueued control message testing
 *
 * there's a nice set of device functional requirements in chapter 9 of the
 * usb 2.0 spec, which we can apply to ANY device, even ones that don't use
 * special test firmware.
 *
 * we know the device is configured (or suspended) by the time it's visible
 * through usbfs.  we can't change that, so we won't test enumeration (which
 * worked 'well enough' to get here, this time), power management (ditto),
 * or remote wakeup (which needs human interaction).
 */

static unsigned realworld = 1;
M
Martin Fuzzey 已提交
523 524
module_param(realworld, uint, 0);
MODULE_PARM_DESC(realworld, "clear to demand stricter spec compliance");
L
Linus Torvalds 已提交
525

M
Martin Fuzzey 已提交
526
static int get_altsetting(struct usbtest_dev *dev)
L
Linus Torvalds 已提交
527 528
{
	struct usb_interface	*iface = dev->intf;
M
Martin Fuzzey 已提交
529
	struct usb_device	*udev = interface_to_usbdev(iface);
L
Linus Torvalds 已提交
530 531
	int			retval;

M
Martin Fuzzey 已提交
532
	retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
L
Linus Torvalds 已提交
533
			USB_REQ_GET_INTERFACE, USB_DIR_IN|USB_RECIP_INTERFACE,
M
Martin Fuzzey 已提交
534
			0, iface->altsetting[0].desc.bInterfaceNumber,
L
Linus Torvalds 已提交
535 536 537
			dev->buf, 1, USB_CTRL_GET_TIMEOUT);
	switch (retval) {
	case 1:
M
Martin Fuzzey 已提交
538
		return dev->buf[0];
L
Linus Torvalds 已提交
539 540
	case 0:
		retval = -ERANGE;
M
Martin Fuzzey 已提交
541
		/* FALLTHROUGH */
L
Linus Torvalds 已提交
542 543 544 545 546
	default:
		return retval;
	}
}

M
Martin Fuzzey 已提交
547
static int set_altsetting(struct usbtest_dev *dev, int alternate)
L
Linus Torvalds 已提交
548 549 550 551 552 553 554
{
	struct usb_interface		*iface = dev->intf;
	struct usb_device		*udev;

	if (alternate < 0 || alternate >= 256)
		return -EINVAL;

M
Martin Fuzzey 已提交
555 556 557
	udev = interface_to_usbdev(iface);
	return usb_set_interface(udev,
			iface->altsetting[0].desc.bInterfaceNumber,
L
Linus Torvalds 已提交
558 559 560
			alternate);
}

561
static int is_good_config(struct usbtest_dev *tdev, int len)
L
Linus Torvalds 已提交
562 563
{
	struct usb_config_descriptor	*config;
564

L
Linus Torvalds 已提交
565 566
	if (len < sizeof *config)
		return 0;
567
	config = (struct usb_config_descriptor *) tdev->buf;
L
Linus Torvalds 已提交
568 569 570 571 572

	switch (config->bDescriptorType) {
	case USB_DT_CONFIG:
	case USB_DT_OTHER_SPEED_CONFIG:
		if (config->bLength != 9) {
573
			ERROR(tdev, "bogus config descriptor length\n");
L
Linus Torvalds 已提交
574 575 576 577
			return 0;
		}
		/* this bit 'must be 1' but often isn't */
		if (!realworld && !(config->bmAttributes & 0x80)) {
578
			ERROR(tdev, "high bit of config attributes not set\n");
L
Linus Torvalds 已提交
579 580 581
			return 0;
		}
		if (config->bmAttributes & 0x1f) {	/* reserved == 0 */
582
			ERROR(tdev, "reserved config bits set\n");
L
Linus Torvalds 已提交
583 584 585 586 587 588 589
			return 0;
		}
		break;
	default:
		return 0;
	}

M
Martin Fuzzey 已提交
590
	if (le16_to_cpu(config->wTotalLength) == len)	/* read it all */
L
Linus Torvalds 已提交
591
		return 1;
M
Martin Fuzzey 已提交
592
	if (le16_to_cpu(config->wTotalLength) >= TBUF_SIZE)	/* max partial read */
L
Linus Torvalds 已提交
593
		return 1;
594
	ERROR(tdev, "bogus config descriptor read size\n");
L
Linus Torvalds 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
	return 0;
}

/* sanity test for standard requests working with usb_control_mesg() and some
 * of the utility functions which use it.
 *
 * this doesn't test how endpoint halts behave or data toggles get set, since
 * we won't do I/O to bulk/interrupt endpoints here (which is how to change
 * halt or toggle).  toggle testing is impractical without support from hcds.
 *
 * this avoids failing devices linux would normally work with, by not testing
 * config/altsetting operations for devices that only support their defaults.
 * such devices rarely support those needless operations.
 *
 * NOTE that since this is a sanity test, it's not examining boundary cases
 * to see if usbcore, hcd, and device all behave right.  such testing would
 * involve varied read sizes and other operation sequences.
 */
M
Martin Fuzzey 已提交
613
static int ch9_postconfig(struct usbtest_dev *dev)
L
Linus Torvalds 已提交
614 615
{
	struct usb_interface	*iface = dev->intf;
M
Martin Fuzzey 已提交
616
	struct usb_device	*udev = interface_to_usbdev(iface);
L
Linus Torvalds 已提交
617 618 619 620 621 622 623 624
	int			i, alt, retval;

	/* [9.2.3] if there's more than one altsetting, we need to be able to
	 * set and get each one.  mostly trusts the descriptors from usbcore.
	 */
	for (i = 0; i < iface->num_altsetting; i++) {

		/* 9.2.3 constrains the range here */
M
Martin Fuzzey 已提交
625
		alt = iface->altsetting[i].desc.bAlternateSetting;
L
Linus Torvalds 已提交
626
		if (alt < 0 || alt >= iface->num_altsetting) {
627
			dev_err(&iface->dev,
L
Linus Torvalds 已提交
628 629 630 631 632 633 634 635 636
					"invalid alt [%d].bAltSetting = %d\n",
					i, alt);
		}

		/* [real world] get/set unimplemented if there's only one */
		if (realworld && iface->num_altsetting == 1)
			continue;

		/* [9.4.10] set_interface */
M
Martin Fuzzey 已提交
637
		retval = set_altsetting(dev, alt);
L
Linus Torvalds 已提交
638
		if (retval) {
639
			dev_err(&iface->dev, "can't set_interface = %d, %d\n",
L
Linus Torvalds 已提交
640 641 642 643 644
					alt, retval);
			return retval;
		}

		/* [9.4.4] get_interface always works */
M
Martin Fuzzey 已提交
645
		retval = get_altsetting(dev);
L
Linus Torvalds 已提交
646
		if (retval != alt) {
647
			dev_err(&iface->dev, "get alt should be %d, was %d\n",
L
Linus Torvalds 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661
					alt, retval);
			return (retval < 0) ? retval : -EDOM;
		}

	}

	/* [real world] get_config unimplemented if there's only one */
	if (!realworld || udev->descriptor.bNumConfigurations != 1) {
		int	expected = udev->actconfig->desc.bConfigurationValue;

		/* [9.4.2] get_configuration always works
		 * ... although some cheap devices (like one TI Hub I've got)
		 * won't return config descriptors except before set_config.
		 */
M
Martin Fuzzey 已提交
662
		retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
L
Linus Torvalds 已提交
663 664 665
				USB_REQ_GET_CONFIGURATION,
				USB_DIR_IN | USB_RECIP_DEVICE,
				0, 0, dev->buf, 1, USB_CTRL_GET_TIMEOUT);
M
Martin Fuzzey 已提交
666
		if (retval != 1 || dev->buf[0] != expected) {
667
			dev_err(&iface->dev, "get config --> %d %d (1 %d)\n",
D
David Brownell 已提交
668
				retval, dev->buf[0], expected);
L
Linus Torvalds 已提交
669 670 671 672 673
			return (retval < 0) ? retval : -EDOM;
		}
	}

	/* there's always [9.4.3] a device descriptor [9.6.1] */
M
Martin Fuzzey 已提交
674
	retval = usb_get_descriptor(udev, USB_DT_DEVICE, 0,
L
Linus Torvalds 已提交
675 676
			dev->buf, sizeof udev->descriptor);
	if (retval != sizeof udev->descriptor) {
677
		dev_err(&iface->dev, "dev descriptor --> %d\n", retval);
L
Linus Torvalds 已提交
678 679 680 681 682
		return (retval < 0) ? retval : -EDOM;
	}

	/* there's always [9.4.3] at least one config descriptor [9.6.3] */
	for (i = 0; i < udev->descriptor.bNumConfigurations; i++) {
M
Martin Fuzzey 已提交
683
		retval = usb_get_descriptor(udev, USB_DT_CONFIG, i,
L
Linus Torvalds 已提交
684
				dev->buf, TBUF_SIZE);
685 686
		if (!is_good_config(dev, retval)) {
			dev_err(&iface->dev,
L
Linus Torvalds 已提交
687 688 689 690 691
					"config [%d] descriptor --> %d\n",
					i, retval);
			return (retval < 0) ? retval : -EDOM;
		}

M
Martin Fuzzey 已提交
692 693 694
		/* FIXME cross-checking udev->config[i] to make sure usbcore
		 * parsed it right (etc) would be good testing paranoia
		 */
L
Linus Torvalds 已提交
695 696 697 698
	}

	/* and sometimes [9.2.6.6] speed dependent descriptors */
	if (le16_to_cpu(udev->descriptor.bcdUSB) == 0x0200) {
M
Martin Fuzzey 已提交
699
		struct usb_qualifier_descriptor *d = NULL;
L
Linus Torvalds 已提交
700 701

		/* device qualifier [9.6.2] */
M
Martin Fuzzey 已提交
702
		retval = usb_get_descriptor(udev,
L
Linus Torvalds 已提交
703
				USB_DT_DEVICE_QUALIFIER, 0, dev->buf,
M
Martin Fuzzey 已提交
704
				sizeof(struct usb_qualifier_descriptor));
L
Linus Torvalds 已提交
705 706
		if (retval == -EPIPE) {
			if (udev->speed == USB_SPEED_HIGH) {
707
				dev_err(&iface->dev,
L
Linus Torvalds 已提交
708 709 710 711 712
						"hs dev qualifier --> %d\n",
						retval);
				return (retval < 0) ? retval : -EDOM;
			}
			/* usb2.0 but not high-speed capable; fine */
M
Martin Fuzzey 已提交
713
		} else if (retval != sizeof(struct usb_qualifier_descriptor)) {
714
			dev_err(&iface->dev, "dev qualifier --> %d\n", retval);
L
Linus Torvalds 已提交
715 716 717 718 719 720 721 722
			return (retval < 0) ? retval : -EDOM;
		} else
			d = (struct usb_qualifier_descriptor *) dev->buf;

		/* might not have [9.6.2] any other-speed configs [9.6.4] */
		if (d) {
			unsigned max = d->bNumConfigurations;
			for (i = 0; i < max; i++) {
M
Martin Fuzzey 已提交
723
				retval = usb_get_descriptor(udev,
L
Linus Torvalds 已提交
724 725
					USB_DT_OTHER_SPEED_CONFIG, i,
					dev->buf, TBUF_SIZE);
726 727
				if (!is_good_config(dev, retval)) {
					dev_err(&iface->dev,
L
Linus Torvalds 已提交
728 729 730 731 732 733 734
						"other speed config --> %d\n",
						retval);
					return (retval < 0) ? retval : -EDOM;
				}
			}
		}
	}
M
Martin Fuzzey 已提交
735
	/* FIXME fetch strings from at least the device descriptor */
L
Linus Torvalds 已提交
736 737

	/* [9.4.5] get_status always works */
M
Martin Fuzzey 已提交
738
	retval = usb_get_status(udev, USB_RECIP_DEVICE, 0, dev->buf);
L
Linus Torvalds 已提交
739
	if (retval != 2) {
740
		dev_err(&iface->dev, "get dev status --> %d\n", retval);
L
Linus Torvalds 已提交
741 742 743
		return (retval < 0) ? retval : -EDOM;
	}

M
Martin Fuzzey 已提交
744 745 746
	/* FIXME configuration.bmAttributes says if we could try to set/clear
	 * the device's remote wakeup feature ... if we can, test that here
	 */
L
Linus Torvalds 已提交
747

M
Martin Fuzzey 已提交
748 749
	retval = usb_get_status(udev, USB_RECIP_INTERFACE,
			iface->altsetting[0].desc.bInterfaceNumber, dev->buf);
L
Linus Torvalds 已提交
750
	if (retval != 2) {
751
		dev_err(&iface->dev, "get interface status --> %d\n", retval);
L
Linus Torvalds 已提交
752 753
		return (retval < 0) ? retval : -EDOM;
	}
M
Martin Fuzzey 已提交
754
	/* FIXME get status for each endpoint in the interface */
755

L
Linus Torvalds 已提交
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
	return 0;
}

/*-------------------------------------------------------------------------*/

/* use ch9 requests to test whether:
 *   (a) queues work for control, keeping N subtests queued and
 *       active (auto-resubmit) for M loops through the queue.
 *   (b) protocol stalls (control-only) will autorecover.
 *       it's not like bulk/intr; no halt clearing.
 *   (c) short control reads are reported and handled.
 *   (d) queues are always processed in-order
 */

struct ctrl_ctx {
	spinlock_t		lock;
	struct usbtest_dev	*dev;
	struct completion	complete;
	unsigned		count;
	unsigned		pending;
	int			status;
	struct urb		**urb;
	struct usbtest_param	*param;
	int			last;
};

#define NUM_SUBCASES	15		/* how many test subcases here? */

struct subcase {
	struct usb_ctrlrequest	setup;
	int			number;
	int			expected;
};

M
Martin Fuzzey 已提交
790
static void ctrl_complete(struct urb *urb)
L
Linus Torvalds 已提交
791 792 793 794 795 796 797
{
	struct ctrl_ctx		*ctx = urb->context;
	struct usb_ctrlrequest	*reqp;
	struct subcase		*subcase;
	int			status = urb->status;

	reqp = (struct usb_ctrlrequest *)urb->setup_packet;
M
Martin Fuzzey 已提交
798
	subcase = container_of(reqp, struct subcase, setup);
L
Linus Torvalds 已提交
799

M
Martin Fuzzey 已提交
800
	spin_lock(&ctx->lock);
L
Linus Torvalds 已提交
801 802 803 804 805 806 807 808 809
	ctx->count--;
	ctx->pending--;

	/* queue must transfer and complete in fifo order, unless
	 * usb_unlink_urb() is used to unlink something not at the
	 * physical queue head (not tested).
	 */
	if (subcase->number > 0) {
		if ((subcase->number - ctx->last) != 1) {
810 811 812
			ERROR(ctx->dev,
				"subcase %d completed out of order, last %d\n",
				subcase->number, ctx->last);
L
Linus Torvalds 已提交
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
			status = -EDOM;
			ctx->last = subcase->number;
			goto error;
		}
	}
	ctx->last = subcase->number;

	/* succeed or fault in only one way? */
	if (status == subcase->expected)
		status = 0;

	/* async unlink for cleanup? */
	else if (status != -ECONNRESET) {

		/* some faults are allowed, not required */
		if (subcase->expected > 0 && (
829 830
			  ((status == -subcase->expected	/* happened */
			   || status == 0))))			/* didn't */
L
Linus Torvalds 已提交
831 832 833 834 835
			status = 0;
		/* sometimes more than one fault is allowed */
		else if (subcase->number == 12 && status == -EPIPE)
			status = 0;
		else
836
			ERROR(ctx->dev, "subtest %d error, status %d\n",
L
Linus Torvalds 已提交
837 838 839 840 841 842 843 844 845 846
					subcase->number, status);
	}

	/* unexpected status codes mean errors; ideally, in hardware */
	if (status) {
error:
		if (ctx->status == 0) {
			int		i;

			ctx->status = status;
847 848
			ERROR(ctx->dev, "control queue %02x.%02x, err %d, "
					"%d left, subcase %d, len %d/%d\n",
L
Linus Torvalds 已提交
849
					reqp->bRequestType, reqp->bRequest,
850 851 852
					status, ctx->count, subcase->number,
					urb->actual_length,
					urb->transfer_buffer_length);
L
Linus Torvalds 已提交
853 854 855 856 857 858 859

			/* FIXME this "unlink everything" exit route should
			 * be a separate test case.
			 */

			/* unlink whatever's still pending */
			for (i = 1; i < ctx->param->sglen; i++) {
M
Martin Fuzzey 已提交
860 861 862
				struct urb *u = ctx->urb[
							(i + subcase->number)
							% ctx->param->sglen];
L
Linus Torvalds 已提交
863 864 865

				if (u == urb || !u->dev)
					continue;
866
				spin_unlock(&ctx->lock);
M
Martin Fuzzey 已提交
867
				status = usb_unlink_urb(u);
868
				spin_lock(&ctx->lock);
L
Linus Torvalds 已提交
869 870 871 872 873 874
				switch (status) {
				case -EINPROGRESS:
				case -EBUSY:
				case -EIDRM:
					continue;
				default:
875 876
					ERROR(ctx->dev, "urb unlink --> %d\n",
							status);
L
Linus Torvalds 已提交
877 878 879 880 881 882 883 884
				}
			}
			status = ctx->status;
		}
	}

	/* resubmit if we need to, else mark this as done */
	if ((status == 0) && (ctx->pending < ctx->count)) {
M
Martin Fuzzey 已提交
885 886
		status = usb_submit_urb(urb, GFP_ATOMIC);
		if (status != 0) {
887 888
			ERROR(ctx->dev,
				"can't resubmit ctrl %02x.%02x, err %d\n",
L
Linus Torvalds 已提交
889 890 891 892 893 894
				reqp->bRequestType, reqp->bRequest, status);
			urb->dev = NULL;
		} else
			ctx->pending++;
	} else
		urb->dev = NULL;
895

L
Linus Torvalds 已提交
896 897
	/* signal completion when nothing's queued */
	if (ctx->pending == 0)
M
Martin Fuzzey 已提交
898 899
		complete(&ctx->complete);
	spin_unlock(&ctx->lock);
L
Linus Torvalds 已提交
900 901 902
}

static int
M
Martin Fuzzey 已提交
903
test_ctrl_queue(struct usbtest_dev *dev, struct usbtest_param *param)
L
Linus Torvalds 已提交
904
{
M
Martin Fuzzey 已提交
905
	struct usb_device	*udev = testdev_to_usbdev(dev);
L
Linus Torvalds 已提交
906 907 908 909
	struct urb		**urb;
	struct ctrl_ctx		context;
	int			i;

910 911 912
	if (param->sglen == 0 || param->iterations > UINT_MAX / param->sglen)
		return -EOPNOTSUPP;

M
Martin Fuzzey 已提交
913
	spin_lock_init(&context.lock);
L
Linus Torvalds 已提交
914
	context.dev = dev;
M
Martin Fuzzey 已提交
915
	init_completion(&context.complete);
L
Linus Torvalds 已提交
916 917 918 919 920 921 922 923 924 925
	context.count = param->sglen * param->iterations;
	context.pending = 0;
	context.status = -ENOMEM;
	context.param = param;
	context.last = -1;

	/* allocate and init the urbs we'll queue.
	 * as with bulk/intr sglists, sglen is the queue depth; it also
	 * controls which subtests run (more tests than sglen) or rerun.
	 */
926
	urb = kcalloc(param->sglen, sizeof(struct urb *), GFP_KERNEL);
L
Linus Torvalds 已提交
927 928 929
	if (!urb)
		return -ENOMEM;
	for (i = 0; i < param->sglen; i++) {
M
Martin Fuzzey 已提交
930
		int			pipe = usb_rcvctrlpipe(udev, 0);
L
Linus Torvalds 已提交
931 932 933 934
		unsigned		len;
		struct urb		*u;
		struct usb_ctrlrequest	req;
		struct subcase		*reqp;
935 936 937 938 939

		/* sign of this variable means:
		 *  -: tested code must return this (negative) error code
		 *  +: tested code may return this (negative too) error code
		 */
L
Linus Torvalds 已提交
940 941 942 943 944 945
		int			expected = 0;

		/* requests here are mostly expected to succeed on any
		 * device, but some are chosen to trigger protocol stalls
		 * or short reads.
		 */
M
Martin Fuzzey 已提交
946
		memset(&req, 0, sizeof req);
L
Linus Torvalds 已提交
947 948 949 950
		req.bRequest = USB_REQ_GET_DESCRIPTOR;
		req.bRequestType = USB_DIR_IN|USB_RECIP_DEVICE;

		switch (i % NUM_SUBCASES) {
M
Martin Fuzzey 已提交
951 952 953
		case 0:		/* get device descriptor */
			req.wValue = cpu_to_le16(USB_DT_DEVICE << 8);
			len = sizeof(struct usb_device_descriptor);
L
Linus Torvalds 已提交
954
			break;
M
Martin Fuzzey 已提交
955 956 957
		case 1:		/* get first config descriptor (only) */
			req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
			len = sizeof(struct usb_config_descriptor);
L
Linus Torvalds 已提交
958
			break;
M
Martin Fuzzey 已提交
959
		case 2:		/* get altsetting (OFTEN STALLS) */
L
Linus Torvalds 已提交
960 961
			req.bRequest = USB_REQ_GET_INTERFACE;
			req.bRequestType = USB_DIR_IN|USB_RECIP_INTERFACE;
M
Martin Fuzzey 已提交
962
			/* index = 0 means first interface */
L
Linus Torvalds 已提交
963 964 965
			len = 1;
			expected = EPIPE;
			break;
M
Martin Fuzzey 已提交
966
		case 3:		/* get interface status */
L
Linus Torvalds 已提交
967 968
			req.bRequest = USB_REQ_GET_STATUS;
			req.bRequestType = USB_DIR_IN|USB_RECIP_INTERFACE;
M
Martin Fuzzey 已提交
969
			/* interface 0 */
L
Linus Torvalds 已提交
970 971
			len = 2;
			break;
M
Martin Fuzzey 已提交
972
		case 4:		/* get device status */
L
Linus Torvalds 已提交
973 974 975 976
			req.bRequest = USB_REQ_GET_STATUS;
			req.bRequestType = USB_DIR_IN|USB_RECIP_DEVICE;
			len = 2;
			break;
M
Martin Fuzzey 已提交
977
		case 5:		/* get device qualifier (MAY STALL) */
L
Linus Torvalds 已提交
978
			req.wValue = cpu_to_le16 (USB_DT_DEVICE_QUALIFIER << 8);
M
Martin Fuzzey 已提交
979
			len = sizeof(struct usb_qualifier_descriptor);
L
Linus Torvalds 已提交
980 981 982
			if (udev->speed != USB_SPEED_HIGH)
				expected = EPIPE;
			break;
M
Martin Fuzzey 已提交
983 984 985 986
		case 6:		/* get first config descriptor, plus interface */
			req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
			len = sizeof(struct usb_config_descriptor);
			len += sizeof(struct usb_interface_descriptor);
L
Linus Torvalds 已提交
987
			break;
M
Martin Fuzzey 已提交
988
		case 7:		/* get interface descriptor (ALWAYS STALLS) */
L
Linus Torvalds 已提交
989
			req.wValue = cpu_to_le16 (USB_DT_INTERFACE << 8);
M
Martin Fuzzey 已提交
990 991
			/* interface == 0 */
			len = sizeof(struct usb_interface_descriptor);
992
			expected = -EPIPE;
L
Linus Torvalds 已提交
993
			break;
M
Martin Fuzzey 已提交
994 995 996
		/* NOTE: two consecutive stalls in the queue here.
		 *  that tests fault recovery a bit more aggressively. */
		case 8:		/* clear endpoint halt (MAY STALL) */
L
Linus Torvalds 已提交
997 998
			req.bRequest = USB_REQ_CLEAR_FEATURE;
			req.bRequestType = USB_RECIP_ENDPOINT;
M
Martin Fuzzey 已提交
999 1000
			/* wValue 0 == ep halt */
			/* wIndex 0 == ep0 (shouldn't halt!) */
L
Linus Torvalds 已提交
1001
			len = 0;
M
Martin Fuzzey 已提交
1002
			pipe = usb_sndctrlpipe(udev, 0);
L
Linus Torvalds 已提交
1003 1004
			expected = EPIPE;
			break;
M
Martin Fuzzey 已提交
1005
		case 9:		/* get endpoint status */
L
Linus Torvalds 已提交
1006 1007
			req.bRequest = USB_REQ_GET_STATUS;
			req.bRequestType = USB_DIR_IN|USB_RECIP_ENDPOINT;
M
Martin Fuzzey 已提交
1008
			/* endpoint 0 */
L
Linus Torvalds 已提交
1009 1010
			len = 2;
			break;
M
Martin Fuzzey 已提交
1011 1012
		case 10:	/* trigger short read (EREMOTEIO) */
			req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
L
Linus Torvalds 已提交
1013 1014 1015
			len = 1024;
			expected = -EREMOTEIO;
			break;
M
Martin Fuzzey 已提交
1016 1017 1018 1019 1020
		/* NOTE: two consecutive _different_ faults in the queue. */
		case 11:	/* get endpoint descriptor (ALWAYS STALLS) */
			req.wValue = cpu_to_le16(USB_DT_ENDPOINT << 8);
			/* endpoint == 0 */
			len = sizeof(struct usb_interface_descriptor);
L
Linus Torvalds 已提交
1021 1022
			expected = EPIPE;
			break;
M
Martin Fuzzey 已提交
1023 1024 1025 1026 1027 1028 1029
		/* NOTE: sometimes even a third fault in the queue! */
		case 12:	/* get string 0 descriptor (MAY STALL) */
			req.wValue = cpu_to_le16(USB_DT_STRING << 8);
			/* string == 0, for language IDs */
			len = sizeof(struct usb_interface_descriptor);
			/* may succeed when > 4 languages */
			expected = EREMOTEIO;	/* or EPIPE, if no strings */
L
Linus Torvalds 已提交
1030
			break;
M
Martin Fuzzey 已提交
1031 1032 1033
		case 13:	/* short read, resembling case 10 */
			req.wValue = cpu_to_le16((USB_DT_CONFIG << 8) | 0);
			/* last data packet "should" be DATA1, not DATA0 */
1034 1035 1036 1037
			if (udev->speed == USB_SPEED_SUPER)
				len = 1024 - 512;
			else
				len = 1024 - udev->descriptor.bMaxPacketSize0;
L
Linus Torvalds 已提交
1038 1039
			expected = -EREMOTEIO;
			break;
M
Martin Fuzzey 已提交
1040 1041
		case 14:	/* short read; try to fill the last packet */
			req.wValue = cpu_to_le16((USB_DT_DEVICE << 8) | 0);
1042
			/* device descriptor size == 18 bytes */
L
Linus Torvalds 已提交
1043
			len = udev->descriptor.bMaxPacketSize0;
1044 1045
			if (udev->speed == USB_SPEED_SUPER)
				len = 512;
L
Linus Torvalds 已提交
1046
			switch (len) {
M
Martin Fuzzey 已提交
1047 1048 1049 1050 1051 1052
			case 8:
				len = 24;
				break;
			case 16:
				len = 32;
				break;
L
Linus Torvalds 已提交
1053 1054 1055 1056
			}
			expected = -EREMOTEIO;
			break;
		default:
1057
			ERROR(dev, "bogus number of ctrl queue testcases!\n");
L
Linus Torvalds 已提交
1058 1059 1060
			context.status = -EINVAL;
			goto cleanup;
		}
M
Martin Fuzzey 已提交
1061 1062
		req.wLength = cpu_to_le16(len);
		urb[i] = u = simple_alloc_urb(udev, pipe, len);
L
Linus Torvalds 已提交
1063 1064 1065
		if (!u)
			goto cleanup;

1066
		reqp = kmalloc(sizeof *reqp, GFP_KERNEL);
L
Linus Torvalds 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
		if (!reqp)
			goto cleanup;
		reqp->setup = req;
		reqp->number = i % NUM_SUBCASES;
		reqp->expected = expected;
		u->setup_packet = (char *) &reqp->setup;

		u->context = &context;
		u->complete = ctrl_complete;
	}

	/* queue the urbs */
	context.urb = urb;
M
Martin Fuzzey 已提交
1080
	spin_lock_irq(&context.lock);
L
Linus Torvalds 已提交
1081
	for (i = 0; i < param->sglen; i++) {
M
Martin Fuzzey 已提交
1082
		context.status = usb_submit_urb(urb[i], GFP_ATOMIC);
L
Linus Torvalds 已提交
1083
		if (context.status != 0) {
1084
			ERROR(dev, "can't submit urb[%d], status %d\n",
L
Linus Torvalds 已提交
1085 1086 1087 1088 1089 1090
					i, context.status);
			context.count = context.pending;
			break;
		}
		context.pending++;
	}
M
Martin Fuzzey 已提交
1091
	spin_unlock_irq(&context.lock);
L
Linus Torvalds 已提交
1092 1093 1094 1095 1096

	/* FIXME  set timer and time out; provide a disconnect hook */

	/* wait for the last one to complete */
	if (context.pending > 0)
M
Martin Fuzzey 已提交
1097
		wait_for_completion(&context.complete);
L
Linus Torvalds 已提交
1098 1099 1100

cleanup:
	for (i = 0; i < param->sglen; i++) {
M
Martin Fuzzey 已提交
1101
		if (!urb[i])
L
Linus Torvalds 已提交
1102
			continue;
M
Martin Fuzzey 已提交
1103
		urb[i]->dev = udev;
1104
		kfree(urb[i]->setup_packet);
M
Martin Fuzzey 已提交
1105
		simple_free_urb(urb[i]);
L
Linus Torvalds 已提交
1106
	}
M
Martin Fuzzey 已提交
1107
	kfree(urb);
L
Linus Torvalds 已提交
1108 1109 1110 1111 1112 1113 1114
	return context.status;
}
#undef NUM_SUBCASES


/*-------------------------------------------------------------------------*/

M
Martin Fuzzey 已提交
1115
static void unlink1_callback(struct urb *urb)
L
Linus Torvalds 已提交
1116 1117 1118
{
	int	status = urb->status;

M
Martin Fuzzey 已提交
1119
	/* we "know" -EPIPE (stall) never happens */
L
Linus Torvalds 已提交
1120
	if (!status)
M
Martin Fuzzey 已提交
1121
		status = usb_submit_urb(urb, GFP_ATOMIC);
L
Linus Torvalds 已提交
1122 1123
	if (status) {
		urb->status = status;
1124
		complete(urb->context);
L
Linus Torvalds 已提交
1125 1126 1127
	}
}

M
Martin Fuzzey 已提交
1128
static int unlink1(struct usbtest_dev *dev, int pipe, int size, int async)
L
Linus Torvalds 已提交
1129 1130 1131 1132 1133
{
	struct urb		*urb;
	struct completion	completion;
	int			retval = 0;

M
Martin Fuzzey 已提交
1134 1135
	init_completion(&completion);
	urb = simple_alloc_urb(testdev_to_usbdev(dev), pipe, size);
L
Linus Torvalds 已提交
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
	if (!urb)
		return -ENOMEM;
	urb->context = &completion;
	urb->complete = unlink1_callback;

	/* keep the endpoint busy.  there are lots of hc/hcd-internal
	 * states, and testing should get to all of them over time.
	 *
	 * FIXME want additional tests for when endpoint is STALLing
	 * due to errors, or is just NAKing requests.
	 */
M
Martin Fuzzey 已提交
1147 1148
	retval = usb_submit_urb(urb, GFP_KERNEL);
	if (retval != 0) {
1149
		dev_err(&dev->intf->dev, "submit fail %d\n", retval);
L
Linus Torvalds 已提交
1150 1151 1152 1153 1154 1155
		return retval;
	}

	/* unlinking that should always work.  variable delay tests more
	 * hcd states and code paths, even with little other system load.
	 */
M
Martin Fuzzey 已提交
1156
	msleep(jiffies % (2 * INTERRUPT_RATE));
L
Linus Torvalds 已提交
1157
	if (async) {
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
		while (!completion_done(&completion)) {
			retval = usb_unlink_urb(urb);

			switch (retval) {
			case -EBUSY:
			case -EIDRM:
				/* we can't unlink urbs while they're completing
				 * or if they've completed, and we haven't
				 * resubmitted. "normal" drivers would prevent
				 * resubmission, but since we're testing unlink
				 * paths, we can't.
				 */
				ERROR(dev, "unlink retry\n");
				continue;
			case 0:
			case -EINPROGRESS:
				break;

			default:
				dev_err(&dev->intf->dev,
					"unlink fail %d\n", retval);
				return retval;
			}

			break;
L
Linus Torvalds 已提交
1183 1184
		}
	} else
M
Martin Fuzzey 已提交
1185
		usb_kill_urb(urb);
L
Linus Torvalds 已提交
1186

M
Martin Fuzzey 已提交
1187
	wait_for_completion(&completion);
L
Linus Torvalds 已提交
1188
	retval = urb->status;
M
Martin Fuzzey 已提交
1189
	simple_free_urb(urb);
L
Linus Torvalds 已提交
1190 1191 1192 1193 1194 1195 1196 1197

	if (async)
		return (retval == -ECONNRESET) ? 0 : retval - 1000;
	else
		return (retval == -ENOENT || retval == -EPERM) ?
				0 : retval - 2000;
}

M
Martin Fuzzey 已提交
1198
static int unlink_simple(struct usbtest_dev *dev, int pipe, int len)
L
Linus Torvalds 已提交
1199 1200 1201 1202
{
	int			retval = 0;

	/* test sync and async paths */
M
Martin Fuzzey 已提交
1203
	retval = unlink1(dev, pipe, len, 1);
L
Linus Torvalds 已提交
1204
	if (!retval)
M
Martin Fuzzey 已提交
1205
		retval = unlink1(dev, pipe, len, 0);
L
Linus Torvalds 已提交
1206 1207 1208 1209 1210
	return retval;
}

/*-------------------------------------------------------------------------*/

1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
struct queued_ctx {
	struct completion	complete;
	atomic_t		pending;
	unsigned		num;
	int			status;
	struct urb		**urbs;
};

static void unlink_queued_callback(struct urb *urb)
{
	int			status = urb->status;
	struct queued_ctx	*ctx = urb->context;

	if (ctx->status)
		goto done;
	if (urb == ctx->urbs[ctx->num - 4] || urb == ctx->urbs[ctx->num - 2]) {
		if (status == -ECONNRESET)
			goto done;
		/* What error should we report if the URB completed normally? */
	}
	if (status != 0)
		ctx->status = status;

 done:
	if (atomic_dec_and_test(&ctx->pending))
		complete(&ctx->complete);
}

static int unlink_queued(struct usbtest_dev *dev, int pipe, unsigned num,
		unsigned size)
{
	struct queued_ctx	ctx;
	struct usb_device	*udev = testdev_to_usbdev(dev);
	void			*buf;
	dma_addr_t		buf_dma;
	int			i;
	int			retval = -ENOMEM;

	init_completion(&ctx.complete);
	atomic_set(&ctx.pending, 1);	/* One more than the actual value */
	ctx.num = num;
	ctx.status = 0;

	buf = usb_alloc_coherent(udev, size, GFP_KERNEL, &buf_dma);
	if (!buf)
		return retval;
	memset(buf, 0, size);

	/* Allocate and init the urbs we'll queue */
	ctx.urbs = kcalloc(num, sizeof(struct urb *), GFP_KERNEL);
	if (!ctx.urbs)
		goto free_buf;
	for (i = 0; i < num; i++) {
		ctx.urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
		if (!ctx.urbs[i])
			goto free_urbs;
		usb_fill_bulk_urb(ctx.urbs[i], udev, pipe, buf, size,
				unlink_queued_callback, &ctx);
		ctx.urbs[i]->transfer_dma = buf_dma;
		ctx.urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
	}

	/* Submit all the URBs and then unlink URBs num - 4 and num - 2. */
	for (i = 0; i < num; i++) {
		atomic_inc(&ctx.pending);
		retval = usb_submit_urb(ctx.urbs[i], GFP_KERNEL);
		if (retval != 0) {
			dev_err(&dev->intf->dev, "submit urbs[%d] fail %d\n",
					i, retval);
			atomic_dec(&ctx.pending);
			ctx.status = retval;
			break;
		}
	}
	if (i == num) {
		usb_unlink_urb(ctx.urbs[num - 4]);
		usb_unlink_urb(ctx.urbs[num - 2]);
	} else {
		while (--i >= 0)
			usb_unlink_urb(ctx.urbs[i]);
	}

	if (atomic_dec_and_test(&ctx.pending))		/* The extra count */
		complete(&ctx.complete);
	wait_for_completion(&ctx.complete);
	retval = ctx.status;

 free_urbs:
	for (i = 0; i < num; i++)
		usb_free_urb(ctx.urbs[i]);
	kfree(ctx.urbs);
 free_buf:
	usb_free_coherent(udev, size, buf, buf_dma);
	return retval;
}

/*-------------------------------------------------------------------------*/

1309
static int verify_not_halted(struct usbtest_dev *tdev, int ep, struct urb *urb)
L
Linus Torvalds 已提交
1310 1311 1312 1313 1314
{
	int	retval;
	u16	status;

	/* shouldn't look or act halted */
M
Martin Fuzzey 已提交
1315
	retval = usb_get_status(urb->dev, USB_RECIP_ENDPOINT, ep, &status);
L
Linus Torvalds 已提交
1316
	if (retval < 0) {
1317 1318
		ERROR(tdev, "ep %02x couldn't get no-halt status, %d\n",
				ep, retval);
L
Linus Torvalds 已提交
1319 1320 1321
		return retval;
	}
	if (status != 0) {
1322
		ERROR(tdev, "ep %02x bogus status: %04x != 0\n", ep, status);
L
Linus Torvalds 已提交
1323 1324
		return -EINVAL;
	}
1325
	retval = simple_io(tdev, urb, 1, 0, 0, __func__);
L
Linus Torvalds 已提交
1326 1327 1328 1329 1330
	if (retval != 0)
		return -EINVAL;
	return 0;
}

1331
static int verify_halted(struct usbtest_dev *tdev, int ep, struct urb *urb)
L
Linus Torvalds 已提交
1332 1333 1334 1335 1336
{
	int	retval;
	u16	status;

	/* should look and act halted */
M
Martin Fuzzey 已提交
1337
	retval = usb_get_status(urb->dev, USB_RECIP_ENDPOINT, ep, &status);
L
Linus Torvalds 已提交
1338
	if (retval < 0) {
1339 1340
		ERROR(tdev, "ep %02x couldn't get halt status, %d\n",
				ep, retval);
L
Linus Torvalds 已提交
1341 1342
		return retval;
	}
1343
	le16_to_cpus(&status);
L
Linus Torvalds 已提交
1344
	if (status != 1) {
1345
		ERROR(tdev, "ep %02x bogus status: %04x != 1\n", ep, status);
L
Linus Torvalds 已提交
1346 1347
		return -EINVAL;
	}
1348
	retval = simple_io(tdev, urb, 1, 0, -EPIPE, __func__);
L
Linus Torvalds 已提交
1349 1350
	if (retval != -EPIPE)
		return -EINVAL;
1351
	retval = simple_io(tdev, urb, 1, 0, -EPIPE, "verify_still_halted");
L
Linus Torvalds 已提交
1352 1353 1354 1355 1356
	if (retval != -EPIPE)
		return -EINVAL;
	return 0;
}

1357
static int test_halt(struct usbtest_dev *tdev, int ep, struct urb *urb)
L
Linus Torvalds 已提交
1358 1359 1360 1361
{
	int	retval;

	/* shouldn't look or act halted now */
1362
	retval = verify_not_halted(tdev, ep, urb);
L
Linus Torvalds 已提交
1363 1364 1365 1366
	if (retval < 0)
		return retval;

	/* set halt (protocol test only), verify it worked */
M
Martin Fuzzey 已提交
1367
	retval = usb_control_msg(urb->dev, usb_sndctrlpipe(urb->dev, 0),
L
Linus Torvalds 已提交
1368 1369 1370 1371
			USB_REQ_SET_FEATURE, USB_RECIP_ENDPOINT,
			USB_ENDPOINT_HALT, ep,
			NULL, 0, USB_CTRL_SET_TIMEOUT);
	if (retval < 0) {
1372
		ERROR(tdev, "ep %02x couldn't set halt, %d\n", ep, retval);
L
Linus Torvalds 已提交
1373 1374
		return retval;
	}
1375
	retval = verify_halted(tdev, ep, urb);
L
Linus Torvalds 已提交
1376 1377 1378 1379
	if (retval < 0)
		return retval;

	/* clear halt (tests API + protocol), verify it worked */
M
Martin Fuzzey 已提交
1380
	retval = usb_clear_halt(urb->dev, urb->pipe);
L
Linus Torvalds 已提交
1381
	if (retval < 0) {
1382
		ERROR(tdev, "ep %02x couldn't clear halt, %d\n", ep, retval);
L
Linus Torvalds 已提交
1383 1384
		return retval;
	}
1385
	retval = verify_not_halted(tdev, ep, urb);
L
Linus Torvalds 已提交
1386 1387 1388 1389 1390 1391 1392 1393
	if (retval < 0)
		return retval;

	/* NOTE:  could also verify SET_INTERFACE clear halts ... */

	return 0;
}

M
Martin Fuzzey 已提交
1394
static int halt_simple(struct usbtest_dev *dev)
L
Linus Torvalds 已提交
1395
{
1396 1397 1398 1399
	int			ep;
	int			retval = 0;
	struct urb		*urb;
	struct usb_device	*udev = testdev_to_usbdev(dev);
L
Linus Torvalds 已提交
1400

1401 1402 1403 1404
	if (udev->speed == USB_SPEED_SUPER)
		urb = simple_alloc_urb(udev, 0, 1024);
	else
		urb = simple_alloc_urb(udev, 0, 512);
L
Linus Torvalds 已提交
1405 1406 1407 1408
	if (urb == NULL)
		return -ENOMEM;

	if (dev->in_pipe) {
M
Martin Fuzzey 已提交
1409
		ep = usb_pipeendpoint(dev->in_pipe) | USB_DIR_IN;
L
Linus Torvalds 已提交
1410
		urb->pipe = dev->in_pipe;
1411
		retval = test_halt(dev, ep, urb);
L
Linus Torvalds 已提交
1412 1413 1414 1415 1416
		if (retval < 0)
			goto done;
	}

	if (dev->out_pipe) {
M
Martin Fuzzey 已提交
1417
		ep = usb_pipeendpoint(dev->out_pipe);
L
Linus Torvalds 已提交
1418
		urb->pipe = dev->out_pipe;
1419
		retval = test_halt(dev, ep, urb);
L
Linus Torvalds 已提交
1420 1421
	}
done:
M
Martin Fuzzey 已提交
1422
	simple_free_urb(urb);
L
Linus Torvalds 已提交
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
	return retval;
}

/*-------------------------------------------------------------------------*/

/* Control OUT tests use the vendor control requests from Intel's
 * USB 2.0 compliance test device:  write a buffer, read it back.
 *
 * Intel's spec only _requires_ that it work for one packet, which
 * is pretty weak.   Some HCDs place limits here; most devices will
 * need to be able to handle more than one OUT data packet.  We'll
 * try whatever we're told to try.
 */
M
Martin Fuzzey 已提交
1436
static int ctrl_out(struct usbtest_dev *dev,
1437
		unsigned count, unsigned length, unsigned vary, unsigned offset)
L
Linus Torvalds 已提交
1438
{
1439 1440
	unsigned		i, j, len;
	int			retval;
L
Linus Torvalds 已提交
1441 1442 1443
	u8			*buf;
	char			*what = "?";
	struct usb_device	*udev;
1444

D
David Brownell 已提交
1445
	if (length < 1 || length > 0xffff || vary >= length)
L
Linus Torvalds 已提交
1446 1447
		return -EINVAL;

1448
	buf = kmalloc(length + offset, GFP_KERNEL);
L
Linus Torvalds 已提交
1449 1450 1451
	if (!buf)
		return -ENOMEM;

1452
	buf += offset;
M
Martin Fuzzey 已提交
1453
	udev = testdev_to_usbdev(dev);
L
Linus Torvalds 已提交
1454 1455 1456 1457 1458 1459 1460 1461 1462
	len = length;
	retval = 0;

	/* NOTE:  hardware might well act differently if we pushed it
	 * with lots back-to-back queued requests.
	 */
	for (i = 0; i < count; i++) {
		/* write patterned data */
		for (j = 0; j < len; j++)
M
Martin Fuzzey 已提交
1463 1464
			buf[j] = i + j;
		retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
L
Linus Torvalds 已提交
1465 1466 1467 1468
				0x5b, USB_DIR_OUT|USB_TYPE_VENDOR,
				0, 0, buf, len, USB_CTRL_SET_TIMEOUT);
		if (retval != len) {
			what = "write";
D
David Brownell 已提交
1469
			if (retval >= 0) {
1470
				ERROR(dev, "ctrl_out, wlen %d (expected %d)\n",
D
David Brownell 已提交
1471 1472 1473
						retval, len);
				retval = -EBADMSG;
			}
L
Linus Torvalds 已提交
1474 1475 1476 1477
			break;
		}

		/* read it back -- assuming nothing intervened!!  */
M
Martin Fuzzey 已提交
1478
		retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
L
Linus Torvalds 已提交
1479 1480 1481 1482
				0x5c, USB_DIR_IN|USB_TYPE_VENDOR,
				0, 0, buf, len, USB_CTRL_GET_TIMEOUT);
		if (retval != len) {
			what = "read";
D
David Brownell 已提交
1483
			if (retval >= 0) {
1484
				ERROR(dev, "ctrl_out, rlen %d (expected %d)\n",
D
David Brownell 已提交
1485 1486 1487
						retval, len);
				retval = -EBADMSG;
			}
L
Linus Torvalds 已提交
1488 1489 1490 1491 1492
			break;
		}

		/* fail if we can't verify */
		for (j = 0; j < len; j++) {
M
Martin Fuzzey 已提交
1493
			if (buf[j] != (u8) (i + j)) {
1494
				ERROR(dev, "ctrl_out, byte %d is %d not %d\n",
M
Martin Fuzzey 已提交
1495
					j, buf[j], (u8) i + j);
L
Linus Torvalds 已提交
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
				retval = -EBADMSG;
				break;
			}
		}
		if (retval < 0) {
			what = "verify";
			break;
		}

		len += vary;
D
David Brownell 已提交
1506 1507

		/* [real world] the "zero bytes IN" case isn't really used.
J
Joe Perches 已提交
1508
		 * hardware can easily trip up in this weird case, since its
D
David Brownell 已提交
1509 1510
		 * status stage is IN, not OUT like other ep0in transfers.
		 */
L
Linus Torvalds 已提交
1511
		if (len > length)
D
David Brownell 已提交
1512
			len = realworld ? 1 : 0;
L
Linus Torvalds 已提交
1513 1514 1515
	}

	if (retval < 0)
M
Martin Fuzzey 已提交
1516
		ERROR(dev, "ctrl_out %s failed, code %d, count %d\n",
L
Linus Torvalds 已提交
1517 1518
			what, retval, i);

1519
	kfree(buf - offset);
L
Linus Torvalds 已提交
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
	return retval;
}

/*-------------------------------------------------------------------------*/

/* ISO tests ... mimics common usage
 *  - buffer length is split into N packets (mostly maxpacket sized)
 *  - multi-buffers according to sglen
 */

struct iso_context {
	unsigned		count;
	unsigned		pending;
	spinlock_t		lock;
	struct completion	done;
1535
	int			submit_error;
L
Linus Torvalds 已提交
1536
	unsigned long		errors;
1537
	unsigned long		packet_count;
L
Linus Torvalds 已提交
1538 1539 1540
	struct usbtest_dev	*dev;
};

M
Martin Fuzzey 已提交
1541
static void iso_callback(struct urb *urb)
L
Linus Torvalds 已提交
1542 1543 1544 1545 1546 1547
{
	struct iso_context	*ctx = urb->context;

	spin_lock(&ctx->lock);
	ctx->count--;

1548
	ctx->packet_count += urb->number_of_packets;
L
Linus Torvalds 已提交
1549 1550
	if (urb->error_count > 0)
		ctx->errors += urb->error_count;
1551 1552
	else if (urb->status != 0)
		ctx->errors += urb->number_of_packets;
1553 1554
	else if (urb->actual_length != urb->transfer_buffer_length)
		ctx->errors++;
1555 1556
	else if (check_guard_bytes(ctx->dev, urb) != 0)
		ctx->errors++;
L
Linus Torvalds 已提交
1557

1558 1559
	if (urb->status == 0 && ctx->count > (ctx->pending - 1)
			&& !ctx->submit_error) {
M
Martin Fuzzey 已提交
1560
		int status = usb_submit_urb(urb, GFP_ATOMIC);
L
Linus Torvalds 已提交
1561 1562 1563 1564
		switch (status) {
		case 0:
			goto done;
		default:
1565
			dev_err(&ctx->dev->intf->dev,
L
Linus Torvalds 已提交
1566 1567 1568 1569
					"iso resubmit err %d\n",
					status);
			/* FALLTHROUGH */
		case -ENODEV:			/* disconnected */
1570 1571
		case -ESHUTDOWN:		/* endpoint disabled */
			ctx->submit_error = 1;
L
Linus Torvalds 已提交
1572 1573 1574 1575 1576 1577 1578
			break;
		}
	}

	ctx->pending--;
	if (ctx->pending == 0) {
		if (ctx->errors)
1579
			dev_err(&ctx->dev->intf->dev,
1580 1581
				"iso test, %lu errors out of %lu\n",
				ctx->errors, ctx->packet_count);
M
Martin Fuzzey 已提交
1582
		complete(&ctx->done);
L
Linus Torvalds 已提交
1583 1584 1585 1586 1587
	}
done:
	spin_unlock(&ctx->lock);
}

M
Martin Fuzzey 已提交
1588
static struct urb *iso_alloc_urb(
L
Linus Torvalds 已提交
1589 1590 1591
	struct usb_device	*udev,
	int			pipe,
	struct usb_endpoint_descriptor	*desc,
1592 1593
	long			bytes,
	unsigned offset
L
Linus Torvalds 已提交
1594 1595 1596 1597 1598 1599 1600
)
{
	struct urb		*urb;
	unsigned		i, maxp, packets;

	if (bytes < 0 || !desc)
		return NULL;
1601 1602
	maxp = 0x7ff & usb_endpoint_maxp(desc);
	maxp *= 1 + (0x3 & (usb_endpoint_maxp(desc) >> 11));
J
Julia Lawall 已提交
1603
	packets = DIV_ROUND_UP(bytes, maxp);
L
Linus Torvalds 已提交
1604

M
Martin Fuzzey 已提交
1605
	urb = usb_alloc_urb(packets, GFP_KERNEL);
L
Linus Torvalds 已提交
1606 1607 1608 1609 1610 1611 1612
	if (!urb)
		return urb;
	urb->dev = udev;
	urb->pipe = pipe;

	urb->number_of_packets = packets;
	urb->transfer_buffer_length = bytes;
1613 1614 1615
	urb->transfer_buffer = usb_alloc_coherent(udev, bytes + offset,
							GFP_KERNEL,
							&urb->transfer_dma);
L
Linus Torvalds 已提交
1616
	if (!urb->transfer_buffer) {
M
Martin Fuzzey 已提交
1617
		usb_free_urb(urb);
L
Linus Torvalds 已提交
1618 1619
		return NULL;
	}
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
	if (offset) {
		memset(urb->transfer_buffer, GUARD_BYTE, offset);
		urb->transfer_buffer += offset;
		urb->transfer_dma += offset;
	}
	/* For inbound transfers use guard byte so that test fails if
		data not correctly copied */
	memset(urb->transfer_buffer,
			usb_pipein(urb->pipe) ? GUARD_BYTE : 0,
			bytes);

L
Linus Torvalds 已提交
1631 1632
	for (i = 0; i < packets; i++) {
		/* here, only the last packet will be short */
M
Martin Fuzzey 已提交
1633
		urb->iso_frame_desc[i].length = min((unsigned) bytes, maxp);
L
Linus Torvalds 已提交
1634 1635 1636 1637 1638 1639
		bytes -= urb->iso_frame_desc[i].length;

		urb->iso_frame_desc[i].offset = maxp * i;
	}

	urb->complete = iso_callback;
M
Martin Fuzzey 已提交
1640
	/* urb->context = SET BY CALLER */
L
Linus Torvalds 已提交
1641 1642 1643 1644 1645 1646
	urb->interval = 1 << (desc->bInterval - 1);
	urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
	return urb;
}

static int
M
Martin Fuzzey 已提交
1647
test_iso_queue(struct usbtest_dev *dev, struct usbtest_param *param,
1648
		int pipe, struct usb_endpoint_descriptor *desc, unsigned offset)
L
Linus Torvalds 已提交
1649 1650 1651 1652 1653
{
	struct iso_context	context;
	struct usb_device	*udev;
	unsigned		i;
	unsigned long		packets = 0;
1654
	int			status = 0;
L
Linus Torvalds 已提交
1655 1656 1657 1658 1659
	struct urb		*urbs[10];	/* FIXME no limit */

	if (param->sglen > 10)
		return -EDOM;

1660
	memset(&context, 0, sizeof context);
L
Linus Torvalds 已提交
1661 1662
	context.count = param->iterations * param->sglen;
	context.dev = dev;
M
Martin Fuzzey 已提交
1663 1664
	init_completion(&context.done);
	spin_lock_init(&context.lock);
L
Linus Torvalds 已提交
1665

M
Martin Fuzzey 已提交
1666 1667
	memset(urbs, 0, sizeof urbs);
	udev = testdev_to_usbdev(dev);
1668
	dev_info(&dev->intf->dev,
L
Linus Torvalds 已提交
1669 1670 1671
		"... iso period %d %sframes, wMaxPacket %04x\n",
		1 << (desc->bInterval - 1),
		(udev->speed == USB_SPEED_HIGH) ? "micro" : "",
1672
		usb_endpoint_maxp(desc));
L
Linus Torvalds 已提交
1673 1674

	for (i = 0; i < param->sglen; i++) {
M
Martin Fuzzey 已提交
1675
		urbs[i] = iso_alloc_urb(udev, pipe, desc,
1676
					param->length, offset);
M
Martin Fuzzey 已提交
1677
		if (!urbs[i]) {
L
Linus Torvalds 已提交
1678 1679 1680 1681
			status = -ENOMEM;
			goto fail;
		}
		packets += urbs[i]->number_of_packets;
M
Martin Fuzzey 已提交
1682
		urbs[i]->context = &context;
L
Linus Torvalds 已提交
1683 1684
	}
	packets *= param->iterations;
1685
	dev_info(&dev->intf->dev,
L
Linus Torvalds 已提交
1686 1687 1688 1689 1690
		"... total %lu msec (%lu packets)\n",
		(packets * (1 << (desc->bInterval - 1)))
			/ ((udev->speed == USB_SPEED_HIGH) ? 8 : 1),
		packets);

M
Martin Fuzzey 已提交
1691
	spin_lock_irq(&context.lock);
L
Linus Torvalds 已提交
1692
	for (i = 0; i < param->sglen; i++) {
1693
		++context.pending;
M
Martin Fuzzey 已提交
1694
		status = usb_submit_urb(urbs[i], GFP_ATOMIC);
L
Linus Torvalds 已提交
1695
		if (status < 0) {
M
Martin Fuzzey 已提交
1696
			ERROR(dev, "submit iso[%d], error %d\n", i, status);
L
Linus Torvalds 已提交
1697
			if (i == 0) {
M
Martin Fuzzey 已提交
1698
				spin_unlock_irq(&context.lock);
L
Linus Torvalds 已提交
1699 1700 1701
				goto fail;
			}

M
Martin Fuzzey 已提交
1702
			simple_free_urb(urbs[i]);
1703
			urbs[i] = NULL;
L
Linus Torvalds 已提交
1704
			context.pending--;
1705 1706
			context.submit_error = 1;
			break;
L
Linus Torvalds 已提交
1707 1708
		}
	}
M
Martin Fuzzey 已提交
1709
	spin_unlock_irq(&context.lock);
L
Linus Torvalds 已提交
1710

M
Martin Fuzzey 已提交
1711
	wait_for_completion(&context.done);
1712

1713 1714 1715 1716
	for (i = 0; i < param->sglen; i++) {
		if (urbs[i])
			simple_free_urb(urbs[i]);
	}
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
	/*
	 * Isochronous transfers are expected to fail sometimes.  As an
	 * arbitrary limit, we will report an error if any submissions
	 * fail or if the transfer failure rate is > 10%.
	 */
	if (status != 0)
		;
	else if (context.submit_error)
		status = -EACCES;
	else if (context.errors > context.packet_count / 10)
		status = -EIO;
	return status;
L
Linus Torvalds 已提交
1729 1730 1731

fail:
	for (i = 0; i < param->sglen; i++) {
M
Martin Fuzzey 已提交
1732 1733
		if (urbs[i])
			simple_free_urb(urbs[i]);
L
Linus Torvalds 已提交
1734 1735 1736 1737
	}
	return status;
}

1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757
static int test_unaligned_bulk(
	struct usbtest_dev *tdev,
	int pipe,
	unsigned length,
	int iterations,
	unsigned transfer_flags,
	const char *label)
{
	int retval;
	struct urb *urb = usbtest_alloc_urb(
		testdev_to_usbdev(tdev), pipe, length, transfer_flags, 1);

	if (!urb)
		return -ENOMEM;

	retval = simple_io(tdev, urb, iterations, 0, 0, label);
	simple_free_urb(urb);
	return retval;
}

L
Linus Torvalds 已提交
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
/*-------------------------------------------------------------------------*/

/* We only have this one interface to user space, through usbfs.
 * User mode code can scan usbfs to find N different devices (maybe on
 * different busses) to use when testing, and allocate one thread per
 * test.  So discovery is simplified, and we have no device naming issues.
 *
 * Don't use these only as stress/load tests.  Use them along with with
 * other USB bus activity:  plugging, unplugging, mousing, mp3 playback,
 * video capture, and so on.  Run different tests at different times, in
 * different sequences.  Nothing here should interact with other devices,
 * except indirectly by consuming USB bandwidth and CPU resources for test
 * threads and request completion.  But the only way to know that for sure
 * is to test when HC queues are in use by many devices.
1772 1773 1774 1775 1776 1777 1778
 *
 * WARNING:  Because usbfs grabs udev->dev.sem before calling this ioctl(),
 * it locks out usbcore in certain code paths.  Notably, if you disconnect
 * the device-under-test, khubd will wait block forever waiting for the
 * ioctl to complete ... so that usb_disconnect() can abort the pending
 * urbs and then call usbtest_disconnect().  To abort a test, you're best
 * off just killing the userspace task and waiting for it to exit.
L
Linus Torvalds 已提交
1779 1780 1781
 */

static int
M
Martin Fuzzey 已提交
1782
usbtest_ioctl(struct usb_interface *intf, unsigned int code, void *buf)
L
Linus Torvalds 已提交
1783
{
M
Martin Fuzzey 已提交
1784 1785
	struct usbtest_dev	*dev = usb_get_intfdata(intf);
	struct usb_device	*udev = testdev_to_usbdev(dev);
L
Linus Torvalds 已提交
1786 1787 1788 1789 1790 1791 1792 1793
	struct usbtest_param	*param = buf;
	int			retval = -EOPNOTSUPP;
	struct urb		*urb;
	struct scatterlist	*sg;
	struct usb_sg_request	req;
	struct timeval		start;
	unsigned		i;

M
Martin Fuzzey 已提交
1794
	/* FIXME USBDEVFS_CONNECTINFO doesn't say how fast the device is. */
L
Linus Torvalds 已提交
1795

1796 1797
	pattern = mod_pattern;

L
Linus Torvalds 已提交
1798 1799 1800
	if (code != USBTEST_REQUEST)
		return -EOPNOTSUPP;

1801
	if (param->iterations <= 0)
L
Linus Torvalds 已提交
1802 1803
		return -EINVAL;

1804
	if (mutex_lock_interruptible(&dev->lock))
L
Linus Torvalds 已提交
1805 1806
		return -ERESTARTSYS;

A
Alan Stern 已提交
1807
	/* FIXME: What if a system sleep starts while a test is running? */
D
David Brownell 已提交
1808

L
Linus Torvalds 已提交
1809 1810 1811 1812 1813
	/* some devices, like ez-usb default devices, need a non-default
	 * altsetting to have any active endpoints.  some tests change
	 * altsettings; force a default so most tests don't need to check.
	 */
	if (dev->info->alt >= 0) {
1814
		int	res;
L
Linus Torvalds 已提交
1815 1816

		if (intf->altsetting->desc.bInterfaceNumber) {
1817
			mutex_unlock(&dev->lock);
L
Linus Torvalds 已提交
1818 1819
			return -ENODEV;
		}
M
Martin Fuzzey 已提交
1820
		res = set_altsetting(dev, dev->info->alt);
L
Linus Torvalds 已提交
1821
		if (res) {
M
Martin Fuzzey 已提交
1822
			dev_err(&intf->dev,
L
Linus Torvalds 已提交
1823 1824
					"set altsetting to %d failed, %d\n",
					dev->info->alt, res);
1825
			mutex_unlock(&dev->lock);
L
Linus Torvalds 已提交
1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
			return res;
		}
	}

	/*
	 * Just a bunch of test cases that every HCD is expected to handle.
	 *
	 * Some may need specific firmware, though it'd be good to have
	 * one firmware image to handle all the test cases.
	 *
	 * FIXME add more tests!  cancel requests, verify the data, control
	 * queueing, concurrent read+write threads, and so on.
	 */
M
Martin Fuzzey 已提交
1839
	do_gettimeofday(&start);
L
Linus Torvalds 已提交
1840 1841 1842
	switch (param->test_num) {

	case 0:
1843
		dev_info(&intf->dev, "TEST 0:  NOP\n");
L
Linus Torvalds 已提交
1844 1845 1846 1847 1848 1849 1850
		retval = 0;
		break;

	/* Simple non-queued bulk I/O tests */
	case 1:
		if (dev->out_pipe == 0)
			break;
1851
		dev_info(&intf->dev,
L
Linus Torvalds 已提交
1852 1853
				"TEST 1:  write %d bytes %u times\n",
				param->length, param->iterations);
M
Martin Fuzzey 已提交
1854
		urb = simple_alloc_urb(udev, dev->out_pipe, param->length);
L
Linus Torvalds 已提交
1855 1856 1857 1858
		if (!urb) {
			retval = -ENOMEM;
			break;
		}
M
Martin Fuzzey 已提交
1859
		/* FIRMWARE:  bulk sink (maybe accepts short writes) */
1860
		retval = simple_io(dev, urb, param->iterations, 0, 0, "test1");
M
Martin Fuzzey 已提交
1861
		simple_free_urb(urb);
L
Linus Torvalds 已提交
1862 1863 1864 1865
		break;
	case 2:
		if (dev->in_pipe == 0)
			break;
1866
		dev_info(&intf->dev,
L
Linus Torvalds 已提交
1867 1868
				"TEST 2:  read %d bytes %u times\n",
				param->length, param->iterations);
M
Martin Fuzzey 已提交
1869
		urb = simple_alloc_urb(udev, dev->in_pipe, param->length);
L
Linus Torvalds 已提交
1870 1871 1872 1873
		if (!urb) {
			retval = -ENOMEM;
			break;
		}
M
Martin Fuzzey 已提交
1874
		/* FIRMWARE:  bulk source (maybe generates short writes) */
1875
		retval = simple_io(dev, urb, param->iterations, 0, 0, "test2");
M
Martin Fuzzey 已提交
1876
		simple_free_urb(urb);
L
Linus Torvalds 已提交
1877 1878 1879 1880
		break;
	case 3:
		if (dev->out_pipe == 0 || param->vary == 0)
			break;
1881
		dev_info(&intf->dev,
L
Linus Torvalds 已提交
1882 1883
				"TEST 3:  write/%d 0..%d bytes %u times\n",
				param->vary, param->length, param->iterations);
M
Martin Fuzzey 已提交
1884
		urb = simple_alloc_urb(udev, dev->out_pipe, param->length);
L
Linus Torvalds 已提交
1885 1886 1887 1888
		if (!urb) {
			retval = -ENOMEM;
			break;
		}
M
Martin Fuzzey 已提交
1889
		/* FIRMWARE:  bulk sink (maybe accepts short writes) */
1890
		retval = simple_io(dev, urb, param->iterations, param->vary,
L
Linus Torvalds 已提交
1891
					0, "test3");
M
Martin Fuzzey 已提交
1892
		simple_free_urb(urb);
L
Linus Torvalds 已提交
1893 1894 1895 1896
		break;
	case 4:
		if (dev->in_pipe == 0 || param->vary == 0)
			break;
1897
		dev_info(&intf->dev,
L
Linus Torvalds 已提交
1898 1899
				"TEST 4:  read/%d 0..%d bytes %u times\n",
				param->vary, param->length, param->iterations);
M
Martin Fuzzey 已提交
1900
		urb = simple_alloc_urb(udev, dev->in_pipe, param->length);
L
Linus Torvalds 已提交
1901 1902 1903 1904
		if (!urb) {
			retval = -ENOMEM;
			break;
		}
M
Martin Fuzzey 已提交
1905
		/* FIRMWARE:  bulk source (maybe generates short writes) */
1906
		retval = simple_io(dev, urb, param->iterations, param->vary,
L
Linus Torvalds 已提交
1907
					0, "test4");
M
Martin Fuzzey 已提交
1908
		simple_free_urb(urb);
L
Linus Torvalds 已提交
1909 1910 1911 1912 1913 1914
		break;

	/* Queued bulk I/O tests */
	case 5:
		if (dev->out_pipe == 0 || param->sglen == 0)
			break;
1915
		dev_info(&intf->dev,
L
Linus Torvalds 已提交
1916 1917 1918
			"TEST 5:  write %d sglists %d entries of %d bytes\n",
				param->iterations,
				param->sglen, param->length);
M
Martin Fuzzey 已提交
1919
		sg = alloc_sglist(param->sglen, param->length, 0);
L
Linus Torvalds 已提交
1920 1921 1922 1923
		if (!sg) {
			retval = -ENOMEM;
			break;
		}
M
Martin Fuzzey 已提交
1924
		/* FIRMWARE:  bulk sink (maybe accepts short writes) */
1925
		retval = perform_sglist(dev, param->iterations, dev->out_pipe,
L
Linus Torvalds 已提交
1926
				&req, sg, param->sglen);
M
Martin Fuzzey 已提交
1927
		free_sglist(sg, param->sglen);
L
Linus Torvalds 已提交
1928 1929 1930 1931 1932
		break;

	case 6:
		if (dev->in_pipe == 0 || param->sglen == 0)
			break;
1933
		dev_info(&intf->dev,
L
Linus Torvalds 已提交
1934 1935 1936
			"TEST 6:  read %d sglists %d entries of %d bytes\n",
				param->iterations,
				param->sglen, param->length);
M
Martin Fuzzey 已提交
1937
		sg = alloc_sglist(param->sglen, param->length, 0);
L
Linus Torvalds 已提交
1938 1939 1940 1941
		if (!sg) {
			retval = -ENOMEM;
			break;
		}
M
Martin Fuzzey 已提交
1942
		/* FIRMWARE:  bulk source (maybe generates short writes) */
1943
		retval = perform_sglist(dev, param->iterations, dev->in_pipe,
L
Linus Torvalds 已提交
1944
				&req, sg, param->sglen);
M
Martin Fuzzey 已提交
1945
		free_sglist(sg, param->sglen);
L
Linus Torvalds 已提交
1946 1947 1948 1949
		break;
	case 7:
		if (dev->out_pipe == 0 || param->sglen == 0 || param->vary == 0)
			break;
1950
		dev_info(&intf->dev,
L
Linus Torvalds 已提交
1951 1952 1953
			"TEST 7:  write/%d %d sglists %d entries 0..%d bytes\n",
				param->vary, param->iterations,
				param->sglen, param->length);
M
Martin Fuzzey 已提交
1954
		sg = alloc_sglist(param->sglen, param->length, param->vary);
L
Linus Torvalds 已提交
1955 1956 1957 1958
		if (!sg) {
			retval = -ENOMEM;
			break;
		}
M
Martin Fuzzey 已提交
1959
		/* FIRMWARE:  bulk sink (maybe accepts short writes) */
1960
		retval = perform_sglist(dev, param->iterations, dev->out_pipe,
L
Linus Torvalds 已提交
1961
				&req, sg, param->sglen);
M
Martin Fuzzey 已提交
1962
		free_sglist(sg, param->sglen);
L
Linus Torvalds 已提交
1963 1964 1965 1966
		break;
	case 8:
		if (dev->in_pipe == 0 || param->sglen == 0 || param->vary == 0)
			break;
1967
		dev_info(&intf->dev,
L
Linus Torvalds 已提交
1968 1969 1970
			"TEST 8:  read/%d %d sglists %d entries 0..%d bytes\n",
				param->vary, param->iterations,
				param->sglen, param->length);
M
Martin Fuzzey 已提交
1971
		sg = alloc_sglist(param->sglen, param->length, param->vary);
L
Linus Torvalds 已提交
1972 1973 1974 1975
		if (!sg) {
			retval = -ENOMEM;
			break;
		}
M
Martin Fuzzey 已提交
1976
		/* FIRMWARE:  bulk source (maybe generates short writes) */
1977
		retval = perform_sglist(dev, param->iterations, dev->in_pipe,
L
Linus Torvalds 已提交
1978
				&req, sg, param->sglen);
M
Martin Fuzzey 已提交
1979
		free_sglist(sg, param->sglen);
L
Linus Torvalds 已提交
1980 1981 1982 1983 1984
		break;

	/* non-queued sanity tests for control (chapter 9 subset) */
	case 9:
		retval = 0;
1985
		dev_info(&intf->dev,
L
Linus Torvalds 已提交
1986 1987 1988
			"TEST 9:  ch9 (subset) control tests, %d times\n",
				param->iterations);
		for (i = param->iterations; retval == 0 && i--; /* NOP */)
M
Martin Fuzzey 已提交
1989
			retval = ch9_postconfig(dev);
L
Linus Torvalds 已提交
1990
		if (retval)
1991 1992
			dev_err(&intf->dev, "ch9 subset failed, "
					"iterations left %d\n", i);
L
Linus Torvalds 已提交
1993 1994 1995 1996 1997
		break;

	/* queued control messaging */
	case 10:
		retval = 0;
1998
		dev_info(&intf->dev,
L
Linus Torvalds 已提交
1999 2000 2001
				"TEST 10:  queue %d control calls, %d times\n",
				param->sglen,
				param->iterations);
M
Martin Fuzzey 已提交
2002
		retval = test_ctrl_queue(dev, param);
L
Linus Torvalds 已提交
2003 2004 2005 2006 2007 2008 2009
		break;

	/* simple non-queued unlinks (ring with one urb) */
	case 11:
		if (dev->in_pipe == 0 || !param->length)
			break;
		retval = 0;
2010
		dev_info(&intf->dev, "TEST 11:  unlink %d reads of %d\n",
L
Linus Torvalds 已提交
2011 2012
				param->iterations, param->length);
		for (i = param->iterations; retval == 0 && i--; /* NOP */)
M
Martin Fuzzey 已提交
2013
			retval = unlink_simple(dev, dev->in_pipe,
L
Linus Torvalds 已提交
2014 2015
						param->length);
		if (retval)
2016
			dev_err(&intf->dev, "unlink reads failed %d, "
L
Linus Torvalds 已提交
2017 2018 2019 2020 2021 2022
				"iterations left %d\n", retval, i);
		break;
	case 12:
		if (dev->out_pipe == 0 || !param->length)
			break;
		retval = 0;
2023
		dev_info(&intf->dev, "TEST 12:  unlink %d writes of %d\n",
L
Linus Torvalds 已提交
2024 2025
				param->iterations, param->length);
		for (i = param->iterations; retval == 0 && i--; /* NOP */)
M
Martin Fuzzey 已提交
2026
			retval = unlink_simple(dev, dev->out_pipe,
L
Linus Torvalds 已提交
2027 2028
						param->length);
		if (retval)
2029
			dev_err(&intf->dev, "unlink writes failed %d, "
L
Linus Torvalds 已提交
2030 2031 2032 2033 2034 2035 2036 2037
				"iterations left %d\n", retval, i);
		break;

	/* ep halt tests */
	case 13:
		if (dev->out_pipe == 0 && dev->in_pipe == 0)
			break;
		retval = 0;
2038
		dev_info(&intf->dev, "TEST 13:  set/clear %d halts\n",
L
Linus Torvalds 已提交
2039 2040
				param->iterations);
		for (i = param->iterations; retval == 0 && i--; /* NOP */)
M
Martin Fuzzey 已提交
2041
			retval = halt_simple(dev);
2042

L
Linus Torvalds 已提交
2043
		if (retval)
2044
			ERROR(dev, "halts failed, iterations left %d\n", i);
L
Linus Torvalds 已提交
2045 2046 2047 2048 2049 2050
		break;

	/* control write tests */
	case 14:
		if (!dev->info->ctrl_out)
			break;
2051
		dev_info(&intf->dev, "TEST 14:  %d ep0out, %d..%d vary %d\n",
D
David Brownell 已提交
2052 2053 2054
				param->iterations,
				realworld ? 1 : 0, param->length,
				param->vary);
2055
		retval = ctrl_out(dev, param->iterations,
2056
				param->length, param->vary, 0);
L
Linus Torvalds 已提交
2057 2058 2059 2060 2061 2062
		break;

	/* iso write tests */
	case 15:
		if (dev->out_iso_pipe == 0 || param->sglen == 0)
			break;
2063
		dev_info(&intf->dev,
L
Linus Torvalds 已提交
2064 2065 2066
			"TEST 15:  write %d iso, %d entries of %d bytes\n",
				param->iterations,
				param->sglen, param->length);
M
Martin Fuzzey 已提交
2067 2068
		/* FIRMWARE:  iso sink */
		retval = test_iso_queue(dev, param,
2069
				dev->out_iso_pipe, dev->iso_out, 0);
L
Linus Torvalds 已提交
2070 2071 2072 2073 2074 2075
		break;

	/* iso read tests */
	case 16:
		if (dev->in_iso_pipe == 0 || param->sglen == 0)
			break;
2076
		dev_info(&intf->dev,
L
Linus Torvalds 已提交
2077 2078 2079
			"TEST 16:  read %d iso, %d entries of %d bytes\n",
				param->iterations,
				param->sglen, param->length);
M
Martin Fuzzey 已提交
2080 2081
		/* FIRMWARE:  iso source */
		retval = test_iso_queue(dev, param,
2082
				dev->in_iso_pipe, dev->iso_in, 0);
L
Linus Torvalds 已提交
2083 2084
		break;

M
Martin Fuzzey 已提交
2085
	/* FIXME scatterlist cancel (needs helper thread) */
L
Linus Torvalds 已提交
2086

2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176
	/* Tests for bulk I/O using DMA mapping by core and odd address */
	case 17:
		if (dev->out_pipe == 0)
			break;
		dev_info(&intf->dev,
			"TEST 17:  write odd addr %d bytes %u times core map\n",
			param->length, param->iterations);

		retval = test_unaligned_bulk(
				dev, dev->out_pipe,
				param->length, param->iterations,
				0, "test17");
		break;

	case 18:
		if (dev->in_pipe == 0)
			break;
		dev_info(&intf->dev,
			"TEST 18:  read odd addr %d bytes %u times core map\n",
			param->length, param->iterations);

		retval = test_unaligned_bulk(
				dev, dev->in_pipe,
				param->length, param->iterations,
				0, "test18");
		break;

	/* Tests for bulk I/O using premapped coherent buffer and odd address */
	case 19:
		if (dev->out_pipe == 0)
			break;
		dev_info(&intf->dev,
			"TEST 19:  write odd addr %d bytes %u times premapped\n",
			param->length, param->iterations);

		retval = test_unaligned_bulk(
				dev, dev->out_pipe,
				param->length, param->iterations,
				URB_NO_TRANSFER_DMA_MAP, "test19");
		break;

	case 20:
		if (dev->in_pipe == 0)
			break;
		dev_info(&intf->dev,
			"TEST 20:  read odd addr %d bytes %u times premapped\n",
			param->length, param->iterations);

		retval = test_unaligned_bulk(
				dev, dev->in_pipe,
				param->length, param->iterations,
				URB_NO_TRANSFER_DMA_MAP, "test20");
		break;

	/* control write tests with unaligned buffer */
	case 21:
		if (!dev->info->ctrl_out)
			break;
		dev_info(&intf->dev,
				"TEST 21:  %d ep0out odd addr, %d..%d vary %d\n",
				param->iterations,
				realworld ? 1 : 0, param->length,
				param->vary);
		retval = ctrl_out(dev, param->iterations,
				param->length, param->vary, 1);
		break;

	/* unaligned iso tests */
	case 22:
		if (dev->out_iso_pipe == 0 || param->sglen == 0)
			break;
		dev_info(&intf->dev,
			"TEST 22:  write %d iso odd, %d entries of %d bytes\n",
				param->iterations,
				param->sglen, param->length);
		retval = test_iso_queue(dev, param,
				dev->out_iso_pipe, dev->iso_out, 1);
		break;

	case 23:
		if (dev->in_iso_pipe == 0 || param->sglen == 0)
			break;
		dev_info(&intf->dev,
			"TEST 23:  read %d iso odd, %d entries of %d bytes\n",
				param->iterations,
				param->sglen, param->length);
		retval = test_iso_queue(dev, param,
				dev->in_iso_pipe, dev->iso_in, 1);
		break;

2177 2178 2179 2180 2181
	/* unlink URBs from a bulk-OUT queue */
	case 24:
		if (dev->out_pipe == 0 || !param->length || param->sglen < 4)
			break;
		retval = 0;
2182
		dev_info(&intf->dev, "TEST 24:  unlink from %d queues of "
2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196
				"%d %d-byte writes\n",
				param->iterations, param->sglen, param->length);
		for (i = param->iterations; retval == 0 && i > 0; --i) {
			retval = unlink_queued(dev, dev->out_pipe,
						param->sglen, param->length);
			if (retval) {
				dev_err(&intf->dev,
					"unlink queued writes failed %d, "
					"iterations left %d\n", retval, i);
				break;
			}
		}
		break;

L
Linus Torvalds 已提交
2197
	}
M
Martin Fuzzey 已提交
2198
	do_gettimeofday(&param->duration);
L
Linus Torvalds 已提交
2199 2200 2201 2202 2203 2204
	param->duration.tv_sec -= start.tv_sec;
	param->duration.tv_usec -= start.tv_usec;
	if (param->duration.tv_usec < 0) {
		param->duration.tv_usec += 1000 * 1000;
		param->duration.tv_sec -= 1;
	}
2205
	mutex_unlock(&dev->lock);
L
Linus Torvalds 已提交
2206 2207 2208 2209 2210
	return retval;
}

/*-------------------------------------------------------------------------*/

M
Martin Fuzzey 已提交
2211 2212 2213
static unsigned force_interrupt;
module_param(force_interrupt, uint, 0);
MODULE_PARM_DESC(force_interrupt, "0 = test default; else interrupt");
L
Linus Torvalds 已提交
2214 2215 2216 2217

#ifdef	GENERIC
static unsigned short vendor;
module_param(vendor, ushort, 0);
M
Martin Fuzzey 已提交
2218
MODULE_PARM_DESC(vendor, "vendor code (from usb-if)");
L
Linus Torvalds 已提交
2219 2220 2221

static unsigned short product;
module_param(product, ushort, 0);
M
Martin Fuzzey 已提交
2222
MODULE_PARM_DESC(product, "product code (from vendor)");
L
Linus Torvalds 已提交
2223 2224 2225
#endif

static int
M
Martin Fuzzey 已提交
2226
usbtest_probe(struct usb_interface *intf, const struct usb_device_id *id)
L
Linus Torvalds 已提交
2227 2228 2229 2230 2231 2232 2233
{
	struct usb_device	*udev;
	struct usbtest_dev	*dev;
	struct usbtest_info	*info;
	char			*rtest, *wtest;
	char			*irtest, *iwtest;

M
Martin Fuzzey 已提交
2234
	udev = interface_to_usbdev(intf);
L
Linus Torvalds 已提交
2235 2236 2237 2238 2239 2240 2241 2242 2243

#ifdef	GENERIC
	/* specify devices by module parameters? */
	if (id->match_flags == 0) {
		/* vendor match required, product match optional */
		if (!vendor || le16_to_cpu(udev->descriptor.idVendor) != (u16)vendor)
			return -ENODEV;
		if (product && le16_to_cpu(udev->descriptor.idProduct) != (u16)product)
			return -ENODEV;
2244 2245
		dev_info(&intf->dev, "matched module params, "
					"vend=0x%04x prod=0x%04x\n",
L
Linus Torvalds 已提交
2246 2247 2248 2249 2250
				le16_to_cpu(udev->descriptor.idVendor),
				le16_to_cpu(udev->descriptor.idProduct));
	}
#endif

2251
	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
L
Linus Torvalds 已提交
2252 2253 2254 2255
	if (!dev)
		return -ENOMEM;
	info = (struct usbtest_info *) id->driver_info;
	dev->info = info;
2256
	mutex_init(&dev->lock);
L
Linus Torvalds 已提交
2257 2258 2259 2260

	dev->intf = intf;

	/* cacheline-aligned scratch for i/o */
M
Martin Fuzzey 已提交
2261 2262 2263
	dev->buf = kmalloc(TBUF_SIZE, GFP_KERNEL);
	if (dev->buf == NULL) {
		kfree(dev);
L
Linus Torvalds 已提交
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274
		return -ENOMEM;
	}

	/* NOTE this doesn't yet test the handful of difference that are
	 * visible with high speed interrupts:  bigger maxpacket (1K) and
	 * "high bandwidth" modes (up to 3 packets/uframe).
	 */
	rtest = wtest = "";
	irtest = iwtest = "";
	if (force_interrupt || udev->speed == USB_SPEED_LOW) {
		if (info->ep_in) {
M
Martin Fuzzey 已提交
2275
			dev->in_pipe = usb_rcvintpipe(udev, info->ep_in);
L
Linus Torvalds 已提交
2276 2277 2278
			rtest = " intr-in";
		}
		if (info->ep_out) {
M
Martin Fuzzey 已提交
2279
			dev->out_pipe = usb_sndintpipe(udev, info->ep_out);
L
Linus Torvalds 已提交
2280 2281 2282 2283 2284 2285
			wtest = " intr-out";
		}
	} else {
		if (info->autoconf) {
			int status;

M
Martin Fuzzey 已提交
2286
			status = get_endpoints(dev, intf);
L
Linus Torvalds 已提交
2287
			if (status < 0) {
2288
				WARNING(dev, "couldn't get endpoints, %d\n",
2289
						status);
2290 2291
				kfree(dev->buf);
				kfree(dev);
L
Linus Torvalds 已提交
2292 2293 2294 2295 2296
				return status;
			}
			/* may find bulk or ISO pipes */
		} else {
			if (info->ep_in)
M
Martin Fuzzey 已提交
2297
				dev->in_pipe = usb_rcvbulkpipe(udev,
L
Linus Torvalds 已提交
2298 2299
							info->ep_in);
			if (info->ep_out)
M
Martin Fuzzey 已提交
2300
				dev->out_pipe = usb_sndbulkpipe(udev,
L
Linus Torvalds 已提交
2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312
							info->ep_out);
		}
		if (dev->in_pipe)
			rtest = " bulk-in";
		if (dev->out_pipe)
			wtest = " bulk-out";
		if (dev->in_iso_pipe)
			irtest = " iso-in";
		if (dev->out_iso_pipe)
			iwtest = " iso-out";
	}

M
Martin Fuzzey 已提交
2313 2314
	usb_set_intfdata(intf, dev);
	dev_info(&intf->dev, "%s\n", info->name);
2315 2316
	dev_info(&intf->dev, "%s {control%s%s%s%s%s} tests%s\n",
			usb_speed_string(udev->speed),
L
Linus Torvalds 已提交
2317 2318 2319 2320 2321 2322 2323
			info->ctrl_out ? " in/out" : "",
			rtest, wtest,
			irtest, iwtest,
			info->alt >= 0 ? " (+alt)" : "");
	return 0;
}

M
Martin Fuzzey 已提交
2324
static int usbtest_suspend(struct usb_interface *intf, pm_message_t message)
D
David Brownell 已提交
2325 2326 2327 2328
{
	return 0;
}

M
Martin Fuzzey 已提交
2329
static int usbtest_resume(struct usb_interface *intf)
D
David Brownell 已提交
2330 2331 2332 2333 2334
{
	return 0;
}


M
Martin Fuzzey 已提交
2335
static void usbtest_disconnect(struct usb_interface *intf)
L
Linus Torvalds 已提交
2336
{
M
Martin Fuzzey 已提交
2337
	struct usbtest_dev	*dev = usb_get_intfdata(intf);
L
Linus Torvalds 已提交
2338

M
Martin Fuzzey 已提交
2339 2340 2341
	usb_set_intfdata(intf, NULL);
	dev_dbg(&intf->dev, "disconnect\n");
	kfree(dev);
L
Linus Torvalds 已提交
2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377
}

/* Basic testing only needs a device that can source or sink bulk traffic.
 * Any device can test control transfers (default with GENERIC binding).
 *
 * Several entries work with the default EP0 implementation that's built
 * into EZ-USB chips.  There's a default vendor ID which can be overridden
 * by (very) small config EEPROMS, but otherwise all these devices act
 * identically until firmware is loaded:  only EP0 works.  It turns out
 * to be easy to make other endpoints work, without modifying that EP0
 * behavior.  For now, we expect that kind of firmware.
 */

/* an21xx or fx versions of ez-usb */
static struct usbtest_info ez1_info = {
	.name		= "EZ-USB device",
	.ep_in		= 2,
	.ep_out		= 2,
	.alt		= 1,
};

/* fx2 version of ez-usb */
static struct usbtest_info ez2_info = {
	.name		= "FX2 device",
	.ep_in		= 6,
	.ep_out		= 2,
	.alt		= 1,
};

/* ezusb family device with dedicated usb test firmware,
 */
static struct usbtest_info fw_info = {
	.name		= "usb test device",
	.ep_in		= 2,
	.ep_out		= 2,
	.alt		= 1,
M
Martin Fuzzey 已提交
2378
	.autoconf	= 1,		/* iso and ctrl_out need autoconf */
L
Linus Torvalds 已提交
2379
	.ctrl_out	= 1,
M
Martin Fuzzey 已提交
2380
	.iso		= 1,		/* iso_ep's are #8 in/out */
L
Linus Torvalds 已提交
2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391
};

/* peripheral running Linux and 'zero.c' test firmware, or
 * its user-mode cousin. different versions of this use
 * different hardware with the same vendor/product codes.
 * host side MUST rely on the endpoint descriptors.
 */
static struct usbtest_info gz_info = {
	.name		= "Linux gadget zero",
	.autoconf	= 1,
	.ctrl_out	= 1,
2392
	.iso		= 1,
L
Linus Torvalds 已提交
2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428
	.alt		= 0,
};

static struct usbtest_info um_info = {
	.name		= "Linux user mode test driver",
	.autoconf	= 1,
	.alt		= -1,
};

static struct usbtest_info um2_info = {
	.name		= "Linux user mode ISO test driver",
	.autoconf	= 1,
	.iso		= 1,
	.alt		= -1,
};

#ifdef IBOT2
/* this is a nice source of high speed bulk data;
 * uses an FX2, with firmware provided in the device
 */
static struct usbtest_info ibot2_info = {
	.name		= "iBOT2 webcam",
	.ep_in		= 2,
	.alt		= -1,
};
#endif

#ifdef GENERIC
/* we can use any device to test control traffic */
static struct usbtest_info generic_info = {
	.name		= "Generic USB device",
	.alt		= -1,
};
#endif


2429
static const struct usb_device_id id_table[] = {
L
Linus Torvalds 已提交
2430 2431 2432 2433 2434 2435 2436 2437

	/*-------------------------------------------------------------*/

	/* EZ-USB devices which download firmware to replace (or in our
	 * case augment) the default device implementation.
	 */

	/* generic EZ-USB FX controller */
M
Martin Fuzzey 已提交
2438
	{ USB_DEVICE(0x0547, 0x2235),
L
Linus Torvalds 已提交
2439
		.driver_info = (unsigned long) &ez1_info,
M
Martin Fuzzey 已提交
2440
	},
L
Linus Torvalds 已提交
2441 2442

	/* CY3671 development board with EZ-USB FX */
M
Martin Fuzzey 已提交
2443
	{ USB_DEVICE(0x0547, 0x0080),
L
Linus Torvalds 已提交
2444
		.driver_info = (unsigned long) &ez1_info,
M
Martin Fuzzey 已提交
2445
	},
L
Linus Torvalds 已提交
2446 2447

	/* generic EZ-USB FX2 controller (or development board) */
M
Martin Fuzzey 已提交
2448
	{ USB_DEVICE(0x04b4, 0x8613),
L
Linus Torvalds 已提交
2449
		.driver_info = (unsigned long) &ez2_info,
M
Martin Fuzzey 已提交
2450
	},
L
Linus Torvalds 已提交
2451 2452

	/* re-enumerated usb test device firmware */
M
Martin Fuzzey 已提交
2453
	{ USB_DEVICE(0xfff0, 0xfff0),
L
Linus Torvalds 已提交
2454
		.driver_info = (unsigned long) &fw_info,
M
Martin Fuzzey 已提交
2455
	},
L
Linus Torvalds 已提交
2456 2457

	/* "Gadget Zero" firmware runs under Linux */
M
Martin Fuzzey 已提交
2458
	{ USB_DEVICE(0x0525, 0xa4a0),
L
Linus Torvalds 已提交
2459
		.driver_info = (unsigned long) &gz_info,
M
Martin Fuzzey 已提交
2460
	},
L
Linus Torvalds 已提交
2461 2462

	/* so does a user-mode variant */
M
Martin Fuzzey 已提交
2463
	{ USB_DEVICE(0x0525, 0xa4a4),
L
Linus Torvalds 已提交
2464
		.driver_info = (unsigned long) &um_info,
M
Martin Fuzzey 已提交
2465
	},
L
Linus Torvalds 已提交
2466 2467

	/* ... and a user-mode variant that talks iso */
M
Martin Fuzzey 已提交
2468
	{ USB_DEVICE(0x0525, 0xa4a3),
L
Linus Torvalds 已提交
2469
		.driver_info = (unsigned long) &um2_info,
M
Martin Fuzzey 已提交
2470
	},
L
Linus Torvalds 已提交
2471 2472 2473

#ifdef KEYSPAN_19Qi
	/* Keyspan 19qi uses an21xx (original EZ-USB) */
M
Martin Fuzzey 已提交
2474 2475
	/* this does not coexist with the real Keyspan 19qi driver! */
	{ USB_DEVICE(0x06cd, 0x010b),
L
Linus Torvalds 已提交
2476
		.driver_info = (unsigned long) &ez1_info,
M
Martin Fuzzey 已提交
2477
	},
L
Linus Torvalds 已提交
2478 2479 2480 2481 2482 2483
#endif

	/*-------------------------------------------------------------*/

#ifdef IBOT2
	/* iBOT2 makes a nice source of high speed bulk-in data */
M
Martin Fuzzey 已提交
2484 2485
	/* this does not coexist with a real iBOT2 driver! */
	{ USB_DEVICE(0x0b62, 0x0059),
L
Linus Torvalds 已提交
2486
		.driver_info = (unsigned long) &ibot2_info,
M
Martin Fuzzey 已提交
2487
	},
L
Linus Torvalds 已提交
2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500
#endif

	/*-------------------------------------------------------------*/

#ifdef GENERIC
	/* module params can specify devices to use for control tests */
	{ .driver_info = (unsigned long) &generic_info, },
#endif

	/*-------------------------------------------------------------*/

	{ }
};
M
Martin Fuzzey 已提交
2501
MODULE_DEVICE_TABLE(usb, id_table);
L
Linus Torvalds 已提交
2502 2503 2504 2505 2506

static struct usb_driver usbtest_driver = {
	.name =		"usbtest",
	.id_table =	id_table,
	.probe =	usbtest_probe,
2507
	.unlocked_ioctl = usbtest_ioctl,
L
Linus Torvalds 已提交
2508
	.disconnect =	usbtest_disconnect,
D
David Brownell 已提交
2509 2510
	.suspend =	usbtest_suspend,
	.resume =	usbtest_resume,
L
Linus Torvalds 已提交
2511 2512 2513 2514
};

/*-------------------------------------------------------------------------*/

M
Martin Fuzzey 已提交
2515
static int __init usbtest_init(void)
L
Linus Torvalds 已提交
2516 2517 2518
{
#ifdef GENERIC
	if (vendor)
2519
		pr_debug("params: vend=0x%04x prod=0x%04x\n", vendor, product);
L
Linus Torvalds 已提交
2520
#endif
M
Martin Fuzzey 已提交
2521
	return usb_register(&usbtest_driver);
L
Linus Torvalds 已提交
2522
}
M
Martin Fuzzey 已提交
2523
module_init(usbtest_init);
L
Linus Torvalds 已提交
2524

M
Martin Fuzzey 已提交
2525
static void __exit usbtest_exit(void)
L
Linus Torvalds 已提交
2526
{
M
Martin Fuzzey 已提交
2527
	usb_deregister(&usbtest_driver);
L
Linus Torvalds 已提交
2528
}
M
Martin Fuzzey 已提交
2529
module_exit(usbtest_exit);
L
Linus Torvalds 已提交
2530

M
Martin Fuzzey 已提交
2531 2532
MODULE_DESCRIPTION("USB Core/HCD Testing Driver");
MODULE_LICENSE("GPL");
L
Linus Torvalds 已提交
2533