dummy_hcd.c 72.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * dummy_hcd.c -- Dummy/Loopback USB host and device emulator driver.
 *
 * Maintainer: Alan Stern <stern@rowland.harvard.edu>
 *
 * Copyright (C) 2003 David Brownell
 * Copyright (C) 2003-2005 Alan Stern
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */


/*
 * This exposes a device side "USB gadget" API, driven by requests to a
 * Linux-USB host controller driver.  USB traffic is simulated; there's
 * no need for USB hardware.  Use this with two other drivers:
 *
 *  - Gadget driver, responding to requests (slave);
 *  - Host-side device driver, as already familiar in Linux.
 *
 * Having this all in one kernel can help some stages of development,
 * bypassing some hardware (and driver) issues.  UML could help too.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ioport.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/timer.h>
#include <linux/list.h>
#include <linux/interrupt.h>
38
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
39
#include <linux/usb.h>
40
#include <linux/usb/gadget.h>
41
#include <linux/usb/hcd.h>
42
#include <linux/scatterlist.h>
L
Linus Torvalds 已提交
43 44

#include <asm/byteorder.h>
45
#include <linux/io.h>
L
Linus Torvalds 已提交
46 47 48 49
#include <asm/irq.h>
#include <asm/unaligned.h>

#define DRIVER_DESC	"USB Host+Gadget Emulator"
50
#define DRIVER_VERSION	"02 May 2005"
L
Linus Torvalds 已提交
51

52 53
#define POWER_BUDGET	500	/* in mA; use 8 for low-power port testing */

54 55
static const char	driver_name[] = "dummy_hcd";
static const char	driver_desc[] = "USB Host+Gadget Emulator";
L
Linus Torvalds 已提交
56

57
static const char	gadget_name[] = "dummy_udc";
L
Linus Torvalds 已提交
58

59 60 61
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_AUTHOR("David Brownell");
MODULE_LICENSE("GPL");
L
Linus Torvalds 已提交
62

63 64
struct dummy_hcd_module_parameters {
	bool is_super_speed;
65
	bool is_high_speed;
66
	unsigned int num;
67 68 69
};

static struct dummy_hcd_module_parameters mod_data = {
70 71
	.is_super_speed = false,
	.is_high_speed = true,
72
	.num = 1,
73 74 75
};
module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
76 77
module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
78 79
module_param_named(num, mod_data.num, uint, S_IRUGO);
MODULE_PARM_DESC(num, "number of emulated controllers");
L
Linus Torvalds 已提交
80 81 82 83 84 85 86 87 88
/*-------------------------------------------------------------------------*/

/* gadget side driver data structres */
struct dummy_ep {
	struct list_head		queue;
	unsigned long			last_io;	/* jiffies timestamp */
	struct usb_gadget		*gadget;
	const struct usb_endpoint_descriptor *desc;
	struct usb_ep			ep;
89 90 91 92
	unsigned			halted:1;
	unsigned			wedged:1;
	unsigned			already_seen:1;
	unsigned			setup_stage:1;
93
	unsigned			stream_en:1;
L
Linus Torvalds 已提交
94 95 96 97 98 99 100
};

struct dummy_request {
	struct list_head		queue;		/* ep's requests */
	struct usb_request		req;
};

101
static inline struct dummy_ep *usb_ep_to_dummy_ep(struct usb_ep *_ep)
L
Linus Torvalds 已提交
102
{
103
	return container_of(_ep, struct dummy_ep, ep);
L
Linus Torvalds 已提交
104 105 106 107 108
}

static inline struct dummy_request *usb_request_to_dummy_request
		(struct usb_request *_req)
{
109
	return container_of(_req, struct dummy_request, req);
L
Linus Torvalds 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
}

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

/*
 * Every device has ep0 for control requests, plus up to 30 more endpoints,
 * in one of two types:
 *
 *   - Configurable:  direction (in/out), type (bulk, iso, etc), and endpoint
 *     number can be changed.  Names like "ep-a" are used for this type.
 *
 *   - Fixed Function:  in other cases.  some characteristics may be mutable;
 *     that'd be hardware-specific.  Names like "ep12out-bulk" are used.
 *
 * Gadget drivers are responsible for not setting up conflicting endpoint
 * configurations, illegal or unsupported packet lengths, and so on.
 */

128
static const char ep0name[] = "ep0";
L
Linus Torvalds 已提交
129

130 131 132 133 134 135 136 137 138
static const struct {
	const char *name;
	const struct usb_ep_caps caps;
} ep_info[] = {
#define EP_INFO(_name, _caps) \
	{ \
		.name = _name, \
		.caps = _caps, \
	}
L
Linus Torvalds 已提交
139

140 141 142
	/* everyone has ep0 */
	EP_INFO(ep0name,
		USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_ALL)),
143
	/* act like a pxa250: fifteen fixed function endpoints */
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	EP_INFO("ep1in-bulk",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
	EP_INFO("ep2out-bulk",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
	EP_INFO("ep3in-iso",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_IN)),
	EP_INFO("ep4out-iso",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_OUT)),
	EP_INFO("ep5in-int",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
	EP_INFO("ep6in-bulk",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
	EP_INFO("ep7out-bulk",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
	EP_INFO("ep8in-iso",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_IN)),
	EP_INFO("ep9out-iso",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_OUT)),
	EP_INFO("ep10in-int",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
	EP_INFO("ep11in-bulk",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
	EP_INFO("ep12out-bulk",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
	EP_INFO("ep13in-iso",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_IN)),
	EP_INFO("ep14out-iso",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_OUT)),
	EP_INFO("ep15in-int",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
L
Linus Torvalds 已提交
174
	/* or like sa1100: two fixed function endpoints */
175 176 177 178
	EP_INFO("ep1out-bulk",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
	EP_INFO("ep2in-bulk",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
179
	/* and now some generic EPs so we have enough in multi config */
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	EP_INFO("ep3out",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_OUT)),
	EP_INFO("ep4in",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_IN)),
	EP_INFO("ep5out",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_OUT)),
	EP_INFO("ep6out",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_OUT)),
	EP_INFO("ep7in",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_IN)),
	EP_INFO("ep8out",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_OUT)),
	EP_INFO("ep9in",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_IN)),
	EP_INFO("ep10out",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_OUT)),
	EP_INFO("ep11out",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_OUT)),
	EP_INFO("ep12in",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_IN)),
	EP_INFO("ep13out",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_OUT)),
	EP_INFO("ep14in",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_IN)),
	EP_INFO("ep15out",
		USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_OUT)),

#undef EP_INFO
L
Linus Torvalds 已提交
208
};
209 210

#define DUMMY_ENDPOINTS	ARRAY_SIZE(ep_info)
L
Linus Torvalds 已提交
211

212 213
/*-------------------------------------------------------------------------*/

L
Linus Torvalds 已提交
214 215 216 217 218
#define FIFO_SIZE		64

struct urbp {
	struct urb		*urb;
	struct list_head	urbp_list;
219 220
	struct sg_mapping_iter	miter;
	u32			miter_started;
L
Linus Torvalds 已提交
221 222
};

223 224 225 226 227 228 229

enum dummy_rh_state {
	DUMMY_RH_RESET,
	DUMMY_RH_SUSPENDED,
	DUMMY_RH_RUNNING
};

230 231 232 233 234 235 236 237 238 239
struct dummy_hcd {
	struct dummy			*dum;
	enum dummy_rh_state		rh_state;
	struct timer_list		timer;
	u32				port_status;
	u32				old_status;
	unsigned long			re_timeout;

	struct usb_device		*udev;
	struct list_head		urbp_list;
240 241
	struct urbp			*next_frame_urbp;

242 243
	u32				stream_en_ep;
	u8				num_stream[30 / 2];
244 245 246 247 248 249

	unsigned			active:1;
	unsigned			old_active:1;
	unsigned			resuming:1;
};

L
Linus Torvalds 已提交
250 251 252 253 254 255
struct dummy {
	spinlock_t			lock;

	/*
	 * SLAVE/GADGET side support
	 */
256
	struct dummy_ep			ep[DUMMY_ENDPOINTS];
L
Linus Torvalds 已提交
257
	int				address;
258
	int				callback_usage;
L
Linus Torvalds 已提交
259 260 261
	struct usb_gadget		gadget;
	struct usb_gadget_driver	*driver;
	struct dummy_request		fifo_req;
262
	u8				fifo_buf[FIFO_SIZE];
L
Linus Torvalds 已提交
263
	u16				devstatus;
264
	unsigned			ints_enabled:1;
265
	unsigned			udc_suspended:1;
266
	unsigned			pullup:1;
L
Linus Torvalds 已提交
267 268 269 270

	/*
	 * MASTER/HOST side support
	 */
271
	struct dummy_hcd		*hs_hcd;
272
	struct dummy_hcd		*ss_hcd;
L
Linus Torvalds 已提交
273 274
};

275
static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
L
Linus Torvalds 已提交
276
{
277
	return (struct dummy_hcd *) (hcd->hcd_priv);
L
Linus Torvalds 已提交
278 279
}

280
static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
L
Linus Torvalds 已提交
281 282 283 284
{
	return container_of((void *) dum, struct usb_hcd, hcd_priv);
}

285
static inline struct device *dummy_dev(struct dummy_hcd *dum)
L
Linus Torvalds 已提交
286
{
287
	return dummy_hcd_to_hcd(dum)->self.controller;
L
Linus Torvalds 已提交
288 289
}

290
static inline struct device *udc_dev(struct dummy *dum)
291 292 293 294
{
	return dum->gadget.dev.parent;
}

295
static inline struct dummy *ep_to_dummy(struct dummy_ep *ep)
L
Linus Torvalds 已提交
296
{
297
	return container_of(ep->gadget, struct dummy, gadget);
L
Linus Torvalds 已提交
298 299
}

300
static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
L
Linus Torvalds 已提交
301
{
302
	struct dummy *dum = container_of(gadget, struct dummy, gadget);
303 304 305 306
	if (dum->gadget.speed == USB_SPEED_SUPER)
		return dum->ss_hcd;
	else
		return dum->hs_hcd;
L
Linus Torvalds 已提交
307 308
}

309
static inline struct dummy *gadget_dev_to_dummy(struct device *dev)
L
Linus Torvalds 已提交
310
{
311
	return container_of(dev, struct dummy, gadget.dev);
L
Linus Torvalds 已提交
312 313 314 315
}

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

316 317 318
/* SLAVE/GADGET SIDE UTILITY ROUTINES */

/* called with spinlock held */
319
static void nuke(struct dummy *dum, struct dummy_ep *ep)
320
{
321
	while (!list_empty(&ep->queue)) {
322 323
		struct dummy_request	*req;

324 325
		req = list_entry(ep->queue.next, struct dummy_request, queue);
		list_del_init(&req->queue);
326 327
		req->req.status = -ESHUTDOWN;

328
		spin_unlock(&dum->lock);
329
		usb_gadget_giveback_request(&ep->ep, &req->req);
330
		spin_lock(&dum->lock);
331 332 333 334
	}
}

/* caller must hold lock */
335
static void stop_activity(struct dummy *dum)
336
{
337
	int i;
338 339 340 341 342 343 344

	/* prevent any more requests */
	dum->address = 0;

	/* The timer is left running so that outstanding URBs can fail */

	/* nuke any pending requests first, so driver i/o is quiesced */
345 346
	for (i = 0; i < DUMMY_ENDPOINTS; ++i)
		nuke(dum, &dum->ep[i]);
347 348 349 350

	/* driver now does any non-usb quiescing necessary */
}

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
/**
 * set_link_state_by_speed() - Sets the current state of the link according to
 *	the hcd speed
 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
 *
 * This function updates the port_status according to the link state and the
 * speed of the hcd.
 */
static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
{
	struct dummy *dum = dum_hcd->dum;

	if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
		if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
			dum_hcd->port_status = 0;
		} else if (!dum->pullup || dum->udc_suspended) {
			/* UDC suspend must cause a disconnect */
			dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
						USB_PORT_STAT_ENABLE);
			if ((dum_hcd->old_status &
			     USB_PORT_STAT_CONNECTION) != 0)
				dum_hcd->port_status |=
					(USB_PORT_STAT_C_CONNECTION << 16);
		} else {
			/* device is connected and not suspended */
			dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
						 USB_PORT_STAT_SPEED_5GBPS) ;
			if ((dum_hcd->old_status &
			     USB_PORT_STAT_CONNECTION) == 0)
				dum_hcd->port_status |=
					(USB_PORT_STAT_C_CONNECTION << 16);
382 383 384 385
			if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) &&
			    (dum_hcd->port_status &
			     USB_PORT_STAT_LINK_STATE) == USB_SS_PORT_LS_U0 &&
			    dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
				dum_hcd->active = 1;
		}
	} else {
		if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
			dum_hcd->port_status = 0;
		} else if (!dum->pullup || dum->udc_suspended) {
			/* UDC suspend must cause a disconnect */
			dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
						USB_PORT_STAT_ENABLE |
						USB_PORT_STAT_LOW_SPEED |
						USB_PORT_STAT_HIGH_SPEED |
						USB_PORT_STAT_SUSPEND);
			if ((dum_hcd->old_status &
			     USB_PORT_STAT_CONNECTION) != 0)
				dum_hcd->port_status |=
					(USB_PORT_STAT_C_CONNECTION << 16);
		} else {
			dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
			if ((dum_hcd->old_status &
			     USB_PORT_STAT_CONNECTION) == 0)
				dum_hcd->port_status |=
					(USB_PORT_STAT_C_CONNECTION << 16);
			if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
				dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
			else if ((dum_hcd->port_status &
				  USB_PORT_STAT_SUSPEND) == 0 &&
					dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
				dum_hcd->active = 1;
		}
	}
}

418
/* caller must hold lock */
419
static void set_link_state(struct dummy_hcd *dum_hcd)
420
{
421
	struct dummy *dum = dum_hcd->dum;
422
	unsigned int power_bit;
423 424

	dum_hcd->active = 0;
425 426 427 428 429 430 431 432
	if (dum->pullup)
		if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
		     dum->gadget.speed != USB_SPEED_SUPER) ||
		    (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
		     dum->gadget.speed == USB_SPEED_SUPER))
			return;

	set_link_state_by_speed(dum_hcd);
433 434
	power_bit = (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 ?
			USB_SS_PORT_STAT_POWER : USB_PORT_STAT_POWER);
435

436 437 438
	if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
	     dum_hcd->active)
		dum_hcd->resuming = 0;
439

440
	/* Currently !connected or in reset */
441
	if ((dum_hcd->port_status & power_bit) == 0 ||
442
			(dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
443
		unsigned int disconnect = power_bit &
444
				dum_hcd->old_status & (~dum_hcd->port_status);
445
		unsigned int reset = USB_PORT_STAT_RESET &
446 447 448
				(~dum_hcd->old_status) & dum_hcd->port_status;

		/* Report reset and disconnect events to the driver */
449
		if (dum->ints_enabled && (disconnect || reset)) {
450
			stop_activity(dum);
451 452
			++dum->callback_usage;
			spin_unlock(&dum->lock);
453 454 455 456
			if (reset)
				usb_gadget_udc_reset(&dum->gadget, dum->driver);
			else
				dum->driver->disconnect(&dum->gadget);
457 458
			spin_lock(&dum->lock);
			--dum->callback_usage;
459
		}
460 461 462 463
	} else if (dum_hcd->active != dum_hcd->old_active &&
			dum->ints_enabled) {
		++dum->callback_usage;
		spin_unlock(&dum->lock);
464
		if (dum_hcd->old_active && dum->driver->suspend)
465
			dum->driver->suspend(&dum->gadget);
466
		else if (!dum_hcd->old_active &&  dum->driver->resume)
467
			dum->driver->resume(&dum->gadget);
468 469
		spin_lock(&dum->lock);
		--dum->callback_usage;
470 471
	}

472 473
	dum_hcd->old_status = dum_hcd->port_status;
	dum_hcd->old_active = dum_hcd->active;
474 475 476 477
}

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

L
Linus Torvalds 已提交
478 479 480 481 482 483 484 485 486 487
/* SLAVE/GADGET SIDE DRIVER
 *
 * This only tracks gadget state.  All the work is done when the host
 * side tries some (emulated) i/o operation.  Real device controller
 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
 */

#define is_enabled(dum) \
	(dum->port_status & USB_PORT_STAT_ENABLE)

488 489
static int dummy_enable(struct usb_ep *_ep,
		const struct usb_endpoint_descriptor *desc)
L
Linus Torvalds 已提交
490 491
{
	struct dummy		*dum;
492
	struct dummy_hcd	*dum_hcd;
L
Linus Torvalds 已提交
493 494 495 496
	struct dummy_ep		*ep;
	unsigned		max;
	int			retval;

497
	ep = usb_ep_to_dummy_ep(_ep);
L
Linus Torvalds 已提交
498 499 500
	if (!_ep || !desc || ep->desc || _ep->name == ep0name
			|| desc->bDescriptorType != USB_DT_ENDPOINT)
		return -EINVAL;
501
	dum = ep_to_dummy(ep);
502 503
	if (!dum->driver)
		return -ESHUTDOWN;
504 505

	dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
506
	if (!is_enabled(dum_hcd))
L
Linus Torvalds 已提交
507
		return -ESHUTDOWN;
508 509 510 511

	/*
	 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
	 * maximum packet size.
512
	 * For SS devices the wMaxPacketSize is limited by 1024.
513
	 */
514
	max = usb_endpoint_maxp(desc);
L
Linus Torvalds 已提交
515 516 517 518 519 520 521 522 523 524

	/* drivers must not request bad settings, since lower levels
	 * (hardware or its drivers) may not check.  some endpoints
	 * can't do iso, many have maxpacket limitations, etc.
	 *
	 * since this "hardware" driver is here to help debugging, we
	 * have some extra sanity checks.  (there could be more though,
	 * especially for "ep9out" style fixed function ones.)
	 */
	retval = -EINVAL;
525
	switch (usb_endpoint_type(desc)) {
L
Linus Torvalds 已提交
526
	case USB_ENDPOINT_XFER_BULK:
527 528
		if (strstr(ep->ep.name, "-iso")
				|| strstr(ep->ep.name, "-int")) {
L
Linus Torvalds 已提交
529 530 531
			goto done;
		}
		switch (dum->gadget.speed) {
532 533 534 535
		case USB_SPEED_SUPER:
			if (max == 1024)
				break;
			goto done;
L
Linus Torvalds 已提交
536 537 538
		case USB_SPEED_HIGH:
			if (max == 512)
				break;
539 540 541
			goto done;
		case USB_SPEED_FULL:
			if (max == 8 || max == 16 || max == 32 || max == 64)
L
Linus Torvalds 已提交
542 543
				/* we'll fake any legal size */
				break;
544 545 546
			/* save a return statement */
		default:
			goto done;
L
Linus Torvalds 已提交
547 548 549
		}
		break;
	case USB_ENDPOINT_XFER_INT:
550
		if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
L
Linus Torvalds 已提交
551 552 553
			goto done;
		/* real hardware might not handle all packet sizes */
		switch (dum->gadget.speed) {
554
		case USB_SPEED_SUPER:
L
Linus Torvalds 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
		case USB_SPEED_HIGH:
			if (max <= 1024)
				break;
			/* save a return statement */
		case USB_SPEED_FULL:
			if (max <= 64)
				break;
			/* save a return statement */
		default:
			if (max <= 8)
				break;
			goto done;
		}
		break;
	case USB_ENDPOINT_XFER_ISOC:
570 571
		if (strstr(ep->ep.name, "-bulk")
				|| strstr(ep->ep.name, "-int"))
L
Linus Torvalds 已提交
572 573 574
			goto done;
		/* real hardware might not handle all packet sizes */
		switch (dum->gadget.speed) {
575
		case USB_SPEED_SUPER:
L
Linus Torvalds 已提交
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
		case USB_SPEED_HIGH:
			if (max <= 1024)
				break;
			/* save a return statement */
		case USB_SPEED_FULL:
			if (max <= 1023)
				break;
			/* save a return statement */
		default:
			goto done;
		}
		break;
	default:
		/* few chips support control except on ep0 */
		goto done;
	}

	_ep->maxpacket = max;
594 595 596 597 598 599 600 601
	if (usb_ss_max_streams(_ep->comp_desc)) {
		if (!usb_endpoint_xfer_bulk(desc)) {
			dev_err(udc_dev(dum), "Can't enable stream support on "
					"non-bulk ep %s\n", _ep->name);
			return -EINVAL;
		}
		ep->stream_en = 1;
	}
602
	ep->desc = desc;
L
Linus Torvalds 已提交
603

604
	dev_dbg(udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n",
L
Linus Torvalds 已提交
605 606 607 608
		_ep->name,
		desc->bEndpointAddress & 0x0f,
		(desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
		({ char *val;
609
		 switch (usb_endpoint_type(desc)) {
T
Tatyana Brokhman 已提交
610 611 612 613 614 615 616 617 618 619 620 621
		 case USB_ENDPOINT_XFER_BULK:
			 val = "bulk";
			 break;
		 case USB_ENDPOINT_XFER_ISOC:
			 val = "iso";
			 break;
		 case USB_ENDPOINT_XFER_INT:
			 val = "intr";
			 break;
		 default:
			 val = "ctrl";
			 break;
J
Joe Perches 已提交
622
		 } val; }),
623
		max, ep->stream_en ? "enabled" : "disabled");
L
Linus Torvalds 已提交
624 625 626 627

	/* at this point real hardware should be NAKing transfers
	 * to that endpoint, until a buffer is queued to it.
	 */
628
	ep->halted = ep->wedged = 0;
L
Linus Torvalds 已提交
629 630 631 632 633
	retval = 0;
done:
	return retval;
}

634
static int dummy_disable(struct usb_ep *_ep)
L
Linus Torvalds 已提交
635 636 637 638 639
{
	struct dummy_ep		*ep;
	struct dummy		*dum;
	unsigned long		flags;

640
	ep = usb_ep_to_dummy_ep(_ep);
L
Linus Torvalds 已提交
641 642
	if (!_ep || !ep->desc || _ep->name == ep0name)
		return -EINVAL;
643
	dum = ep_to_dummy(ep);
L
Linus Torvalds 已提交
644

645
	spin_lock_irqsave(&dum->lock, flags);
L
Linus Torvalds 已提交
646
	ep->desc = NULL;
647
	ep->stream_en = 0;
648 649
	nuke(dum, ep);
	spin_unlock_irqrestore(&dum->lock, flags);
L
Linus Torvalds 已提交
650

651
	dev_dbg(udc_dev(dum), "disabled %s\n", _ep->name);
652
	return 0;
L
Linus Torvalds 已提交
653 654
}

655 656
static struct usb_request *dummy_alloc_request(struct usb_ep *_ep,
		gfp_t mem_flags)
L
Linus Torvalds 已提交
657 658 659 660 661 662
{
	struct dummy_request	*req;

	if (!_ep)
		return NULL;

663
	req = kzalloc(sizeof(*req), mem_flags);
L
Linus Torvalds 已提交
664 665
	if (!req)
		return NULL;
666
	INIT_LIST_HEAD(&req->queue);
L
Linus Torvalds 已提交
667 668 669
	return &req->req;
}

670
static void dummy_free_request(struct usb_ep *_ep, struct usb_request *_req)
L
Linus Torvalds 已提交
671 672 673
{
	struct dummy_request	*req;

674
	if (!_ep || !_req) {
675
		WARN_ON(1);
L
Linus Torvalds 已提交
676
		return;
677
	}
L
Linus Torvalds 已提交
678

679 680 681
	req = usb_request_to_dummy_request(_req);
	WARN_ON(!list_empty(&req->queue));
	kfree(req);
L
Linus Torvalds 已提交
682 683
}

684
static void fifo_complete(struct usb_ep *ep, struct usb_request *req)
L
Linus Torvalds 已提交
685 686 687
{
}

688
static int dummy_queue(struct usb_ep *_ep, struct usb_request *_req,
A
Al Viro 已提交
689
		gfp_t mem_flags)
L
Linus Torvalds 已提交
690 691 692 693
{
	struct dummy_ep		*ep;
	struct dummy_request	*req;
	struct dummy		*dum;
694
	struct dummy_hcd	*dum_hcd;
L
Linus Torvalds 已提交
695 696
	unsigned long		flags;

697 698
	req = usb_request_to_dummy_request(_req);
	if (!_req || !list_empty(&req->queue) || !_req->complete)
L
Linus Torvalds 已提交
699 700
		return -EINVAL;

701
	ep = usb_ep_to_dummy_ep(_ep);
L
Linus Torvalds 已提交
702 703 704
	if (!_ep || (!ep->desc && _ep->name != ep0name))
		return -EINVAL;

705
	dum = ep_to_dummy(ep);
706
	dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
707
	if (!dum->driver || !is_enabled(dum_hcd))
L
Linus Torvalds 已提交
708 709 710
		return -ESHUTDOWN;

#if 0
711
	dev_dbg(udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
L
Linus Torvalds 已提交
712 713 714 715
			ep, _req, _ep->name, _req->length, _req->buf);
#endif
	_req->status = -EINPROGRESS;
	_req->actual = 0;
716
	spin_lock_irqsave(&dum->lock, flags);
L
Linus Torvalds 已提交
717 718 719

	/* implement an emulated single-request FIFO */
	if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
720 721
			list_empty(&dum->fifo_req.queue) &&
			list_empty(&ep->queue) &&
L
Linus Torvalds 已提交
722 723 724 725
			_req->length <= FIFO_SIZE) {
		req = &dum->fifo_req;
		req->req = *_req;
		req->req.buf = dum->fifo_buf;
726
		memcpy(dum->fifo_buf, _req->buf, _req->length);
L
Linus Torvalds 已提交
727 728 729
		req->req.context = dum;
		req->req.complete = fifo_complete;

730
		list_add_tail(&req->queue, &ep->queue);
731
		spin_unlock(&dum->lock);
L
Linus Torvalds 已提交
732 733
		_req->actual = _req->length;
		_req->status = 0;
734
		usb_gadget_giveback_request(_ep, _req);
735
		spin_lock(&dum->lock);
736 737
	}  else
		list_add_tail(&req->queue, &ep->queue);
738
	spin_unlock_irqrestore(&dum->lock, flags);
L
Linus Torvalds 已提交
739 740 741 742 743 744 745

	/* real hardware would likely enable transfers here, in case
	 * it'd been left NAKing.
	 */
	return 0;
}

746
static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req)
L
Linus Torvalds 已提交
747 748 749 750 751 752 753 754 755
{
	struct dummy_ep		*ep;
	struct dummy		*dum;
	int			retval = -EINVAL;
	unsigned long		flags;
	struct dummy_request	*req = NULL;

	if (!_ep || !_req)
		return retval;
756 757
	ep = usb_ep_to_dummy_ep(_ep);
	dum = ep_to_dummy(ep);
L
Linus Torvalds 已提交
758 759 760 761

	if (!dum->driver)
		return -ESHUTDOWN;

762 763 764
	local_irq_save(flags);
	spin_lock(&dum->lock);
	list_for_each_entry(req, &ep->queue, queue) {
L
Linus Torvalds 已提交
765
		if (&req->req == _req) {
766
			list_del_init(&req->queue);
L
Linus Torvalds 已提交
767 768 769 770 771
			_req->status = -ECONNRESET;
			retval = 0;
			break;
		}
	}
772
	spin_unlock(&dum->lock);
L
Linus Torvalds 已提交
773 774

	if (retval == 0) {
775
		dev_dbg(udc_dev(dum),
L
Linus Torvalds 已提交
776 777
				"dequeued req %p from %s, len %d buf %p\n",
				req, _ep->name, _req->length, _req->buf);
778
		usb_gadget_giveback_request(_ep, _req);
L
Linus Torvalds 已提交
779
	}
780
	local_irq_restore(flags);
L
Linus Torvalds 已提交
781 782 783 784
	return retval;
}

static int
785
dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
L
Linus Torvalds 已提交
786 787 788 789 790 791
{
	struct dummy_ep		*ep;
	struct dummy		*dum;

	if (!_ep)
		return -EINVAL;
792 793
	ep = usb_ep_to_dummy_ep(_ep);
	dum = ep_to_dummy(ep);
L
Linus Torvalds 已提交
794 795 796
	if (!dum->driver)
		return -ESHUTDOWN;
	if (!value)
797
		ep->halted = ep->wedged = 0;
L
Linus Torvalds 已提交
798
	else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
799
			!list_empty(&ep->queue))
L
Linus Torvalds 已提交
800
		return -EAGAIN;
801
	else {
L
Linus Torvalds 已提交
802
		ep->halted = 1;
803 804 805
		if (wedged)
			ep->wedged = 1;
	}
L
Linus Torvalds 已提交
806 807 808 809
	/* FIXME clear emulated data toggle too */
	return 0;
}

810 811 812 813 814 815 816 817 818 819 820 821 822
static int
dummy_set_halt(struct usb_ep *_ep, int value)
{
	return dummy_set_halt_and_wedge(_ep, value, 0);
}

static int dummy_set_wedge(struct usb_ep *_ep)
{
	if (!_ep || _ep->name == ep0name)
		return -EINVAL;
	return dummy_set_halt_and_wedge(_ep, 1, 1);
}

L
Linus Torvalds 已提交
823 824 825 826 827 828 829 830 831 832 833
static const struct usb_ep_ops dummy_ep_ops = {
	.enable		= dummy_enable,
	.disable	= dummy_disable,

	.alloc_request	= dummy_alloc_request,
	.free_request	= dummy_free_request,

	.queue		= dummy_queue,
	.dequeue	= dummy_dequeue,

	.set_halt	= dummy_set_halt,
834
	.set_wedge	= dummy_set_wedge,
L
Linus Torvalds 已提交
835 836 837 838 839
};

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

/* there are both host and device side versions of this call ... */
840
static int dummy_g_get_frame(struct usb_gadget *_gadget)
L
Linus Torvalds 已提交
841
{
842
	struct timespec64 ts64;
L
Linus Torvalds 已提交
843

844 845
	ktime_get_ts64(&ts64);
	return ts64.tv_nsec / NSEC_PER_MSEC;
L
Linus Torvalds 已提交
846 847
}

848
static int dummy_wakeup(struct usb_gadget *_gadget)
L
Linus Torvalds 已提交
849
{
850
	struct dummy_hcd *dum_hcd;
L
Linus Torvalds 已提交
851

852 853
	dum_hcd = gadget_to_dummy_hcd(_gadget);
	if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
854
				| (1 << USB_DEVICE_REMOTE_WAKEUP))))
L
Linus Torvalds 已提交
855
		return -EINVAL;
856
	if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
857
		return -ENOLINK;
858 859
	if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
			 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
860 861 862
		return -EIO;

	/* FIXME: What if the root hub is suspended but the port isn't? */
L
Linus Torvalds 已提交
863 864

	/* hub notices our request, issues downstream resume, etc */
865 866 867
	dum_hcd->resuming = 1;
	dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
	mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
L
Linus Torvalds 已提交
868 869 870
	return 0;
}

871
static int dummy_set_selfpowered(struct usb_gadget *_gadget, int value)
L
Linus Torvalds 已提交
872 873 874
{
	struct dummy	*dum;

875
	_gadget->is_selfpowered = (value != 0);
876
	dum = gadget_to_dummy_hcd(_gadget)->dum;
L
Linus Torvalds 已提交
877 878 879 880 881 882 883
	if (value)
		dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
	else
		dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
	return 0;
}

884
static void dummy_udc_update_ep0(struct dummy *dum)
885
{
886
	if (dum->gadget.speed == USB_SPEED_SUPER)
887
		dum->ep[0].ep.maxpacket = 9;
888
	else
889 890 891
		dum->ep[0].ep.maxpacket = 64;
}

892
static int dummy_pullup(struct usb_gadget *_gadget, int value)
893
{
894
	struct dummy_hcd *dum_hcd;
895 896 897
	struct dummy	*dum;
	unsigned long	flags;

898
	dum = gadget_dev_to_dummy(&_gadget->dev);
899 900
	dum_hcd = gadget_to_dummy_hcd(_gadget);

901
	spin_lock_irqsave(&dum->lock, flags);
902
	dum->pullup = (value != 0);
903
	set_link_state(dum_hcd);
904
	spin_unlock_irqrestore(&dum->lock, flags);
905

906
	usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
907 908 909
	return 0;
}

910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
static void dummy_udc_set_speed(struct usb_gadget *_gadget,
		enum usb_device_speed speed)
{
	struct dummy	*dum;

	dum = gadget_dev_to_dummy(&_gadget->dev);

	 if (mod_data.is_super_speed)
		 dum->gadget.speed = min_t(u8, USB_SPEED_SUPER, speed);
	 else if (mod_data.is_high_speed)
		 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH, speed);
	 else
		 dum->gadget.speed = USB_SPEED_FULL;

	dummy_udc_update_ep0(dum);

	if (dum->gadget.speed < speed)
		dev_dbg(udc_dev(dum), "This device can perform faster"
			" if you connect it to a %s port...\n",
			usb_speed_string(speed));
}

932 933
static int dummy_udc_start(struct usb_gadget *g,
		struct usb_gadget_driver *driver);
934
static int dummy_udc_stop(struct usb_gadget *g);
935

L
Linus Torvalds 已提交
936 937 938 939
static const struct usb_gadget_ops dummy_ops = {
	.get_frame	= dummy_g_get_frame,
	.wakeup		= dummy_wakeup,
	.set_selfpowered = dummy_set_selfpowered,
940
	.pullup		= dummy_pullup,
941 942
	.udc_start	= dummy_udc_start,
	.udc_stop	= dummy_udc_stop,
943
	.udc_set_speed	= dummy_udc_set_speed,
L
Linus Torvalds 已提交
944 945 946 947 948
};

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

/* "function" sysfs attribute */
949
static ssize_t function_show(struct device *dev, struct device_attribute *attr,
950
		char *buf)
L
Linus Torvalds 已提交
951
{
952
	struct dummy	*dum = gadget_dev_to_dummy(dev);
L
Linus Torvalds 已提交
953 954 955

	if (!dum->driver || !dum->driver->function)
		return 0;
956
	return scnprintf(buf, PAGE_SIZE, "%s\n", dum->driver->function);
L
Linus Torvalds 已提交
957
}
958
static DEVICE_ATTR_RO(function);
L
Linus Torvalds 已提交
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975

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

/*
 * Driver registration/unregistration.
 *
 * This is basically hardware-specific; there's usually only one real USB
 * device (not host) controller since that's how USB devices are intended
 * to work.  So most implementations of these api calls will rely on the
 * fact that only one driver will ever bind to the hardware.  But curious
 * hardware can be built with discrete components, so the gadget API doesn't
 * require that assumption.
 *
 * For this emulator, it might be convenient to create a usb slave device
 * for each driver that registers:  just add to a big root hub.
 */

976 977
static int dummy_udc_start(struct usb_gadget *g,
		struct usb_gadget_driver *driver)
L
Linus Torvalds 已提交
978
{
979 980
	struct dummy_hcd	*dum_hcd = gadget_to_dummy_hcd(g);
	struct dummy		*dum = dum_hcd->dum;
L
Linus Torvalds 已提交
981

982
	if (driver->max_speed == USB_SPEED_UNKNOWN)
L
Linus Torvalds 已提交
983 984 985 986 987 988
		return -EINVAL;

	/*
	 * SLAVE side init ... the layer above hardware, which
	 * can't enumerate without help from the driver we're binding.
	 */
989

990
	spin_lock_irq(&dum->lock);
L
Linus Torvalds 已提交
991 992
	dum->devstatus = 0;
	dum->driver = driver;
993 994
	dum->ints_enabled = 1;
	spin_unlock_irq(&dum->lock);
995

L
Linus Torvalds 已提交
996 997 998
	return 0;
}

999
static int dummy_udc_stop(struct usb_gadget *g)
L
Linus Torvalds 已提交
1000
{
1001 1002
	struct dummy_hcd	*dum_hcd = gadget_to_dummy_hcd(g);
	struct dummy		*dum = dum_hcd->dum;
L
Linus Torvalds 已提交
1003

1004
	spin_lock_irq(&dum->lock);
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
	dum->ints_enabled = 0;
	stop_activity(dum);

	/* emulate synchronize_irq(): wait for callbacks to finish */
	while (dum->callback_usage > 0) {
		spin_unlock_irq(&dum->lock);
		usleep_range(1000, 2000);
		spin_lock_irq(&dum->lock);
	}

L
Linus Torvalds 已提交
1015
	dum->driver = NULL;
1016
	spin_unlock_irq(&dum->lock);
1017

L
Linus Torvalds 已提交
1018 1019 1020 1021 1022
	return 0;
}

#undef is_enabled

1023 1024
/* The gadget structure is stored inside the hcd structure and will be
 * released along with it. */
1025 1026 1027 1028 1029 1030 1031 1032
static void init_dummy_udc_hw(struct dummy *dum)
{
	int i;

	INIT_LIST_HEAD(&dum->gadget.ep_list);
	for (i = 0; i < DUMMY_ENDPOINTS; i++) {
		struct dummy_ep	*ep = &dum->ep[i];

1033
		if (!ep_info[i].name)
1034
			break;
1035 1036
		ep->ep.name = ep_info[i].name;
		ep->ep.caps = ep_info[i].caps;
1037 1038 1039 1040
		ep->ep.ops = &dummy_ep_ops;
		list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
		ep->halted = ep->wedged = ep->already_seen =
				ep->setup_stage = 0;
1041
		usb_ep_set_maxpacket_limit(&ep->ep, ~0);
1042
		ep->ep.max_streams = 16;
1043 1044 1045 1046 1047 1048 1049 1050 1051
		ep->last_io = jiffies;
		ep->gadget = &dum->gadget;
		ep->desc = NULL;
		INIT_LIST_HEAD(&ep->queue);
	}

	dum->gadget.ep0 = &dum->ep[0].ep;
	list_del_init(&dum->ep[0].ep.ep_list);
	INIT_LIST_HEAD(&dum->fifo_req.queue);
1052 1053 1054 1055

#ifdef CONFIG_USB_OTG
	dum->gadget.is_otg = 1;
#endif
1056 1057
}

1058
static int dummy_udc_probe(struct platform_device *pdev)
1059
{
1060
	struct dummy	*dum;
1061 1062
	int		rc;

1063
	dum = *((void **)dev_get_platdata(&pdev->dev));
1064 1065
	/* Clear usb_gadget region for new registration to udc-core */
	memzero_explicit(&dum->gadget, sizeof(struct usb_gadget));
1066 1067
	dum->gadget.name = gadget_name;
	dum->gadget.ops = &dummy_ops;
1068 1069 1070 1071 1072 1073
	if (mod_data.is_super_speed)
		dum->gadget.max_speed = USB_SPEED_SUPER;
	else if (mod_data.is_high_speed)
		dum->gadget.max_speed = USB_SPEED_HIGH;
	else
		dum->gadget.max_speed = USB_SPEED_FULL;
1074

1075
	dum->gadget.dev.parent = &pdev->dev;
1076 1077
	init_dummy_udc_hw(dum);

1078 1079 1080 1081
	rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
	if (rc < 0)
		goto err_udc;

1082
	rc = device_create_file(&dum->gadget.dev, &dev_attr_function);
1083
	if (rc < 0)
1084 1085 1086 1087 1088 1089 1090
		goto err_dev;
	platform_set_drvdata(pdev, dum);
	return rc;

err_dev:
	usb_del_gadget_udc(&dum->gadget);
err_udc:
1091 1092 1093
	return rc;
}

1094
static int dummy_udc_remove(struct platform_device *pdev)
1095
{
1096
	struct dummy	*dum = platform_get_drvdata(pdev);
1097

1098
	device_remove_file(&dum->gadget.dev, &dev_attr_function);
1099
	usb_del_gadget_udc(&dum->gadget);
1100 1101 1102
	return 0;
}

1103 1104
static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
		int suspend)
1105
{
1106 1107
	spin_lock_irq(&dum->lock);
	dum->udc_suspended = suspend;
1108
	set_link_state(dum_hcd);
1109 1110 1111 1112 1113 1114 1115
	spin_unlock_irq(&dum->lock);
}

static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
{
	struct dummy		*dum = platform_get_drvdata(pdev);
	struct dummy_hcd	*dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1116

1117 1118
	dev_dbg(&pdev->dev, "%s\n", __func__);
	dummy_udc_pm(dum, dum_hcd, 1);
1119
	usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
1120 1121 1122
	return 0;
}

1123
static int dummy_udc_resume(struct platform_device *pdev)
1124
{
1125 1126
	struct dummy		*dum = platform_get_drvdata(pdev);
	struct dummy_hcd	*dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1127

1128 1129
	dev_dbg(&pdev->dev, "%s\n", __func__);
	dummy_udc_pm(dum, dum_hcd, 0);
1130
	usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
1131 1132 1133
	return 0;
}

1134
static struct platform_driver dummy_udc_driver = {
1135 1136
	.probe		= dummy_udc_probe,
	.remove		= dummy_udc_remove,
1137 1138
	.suspend	= dummy_udc_suspend,
	.resume		= dummy_udc_resume,
1139 1140 1141
	.driver		= {
		.name	= (char *) gadget_name,
	},
1142 1143
};

L
Linus Torvalds 已提交
1144 1145
/*-------------------------------------------------------------------------*/

1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc)
{
	unsigned int index;

	index = usb_endpoint_num(desc) << 1;
	if (usb_endpoint_dir_in(desc))
		index |= 1;
	return index;
}

L
Linus Torvalds 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
/* MASTER/HOST SIDE DRIVER
 *
 * this uses the hcd framework to hook up to host side drivers.
 * its root hub will only have one device, otherwise it acts like
 * a normal host controller.
 *
 * when urbs are queued, they're just stuck on a list that we
 * scan in a timer callback.  that callback connects writes from
 * the host with reads from the device, and so on, based on the
 * usb 2.0 rules.
 */

1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 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
static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb)
{
	const struct usb_endpoint_descriptor *desc = &urb->ep->desc;
	u32 index;

	if (!usb_endpoint_xfer_bulk(desc))
		return 0;

	index = dummy_get_ep_idx(desc);
	return (1 << index) & dum_hcd->stream_en_ep;
}

/*
 * The max stream number is saved as a nibble so for the 30 possible endpoints
 * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
 * means we use only 1 stream). The maximum according to the spec is 16bit so
 * if the 16 stream limit is about to go, the array size should be incremented
 * to 30 elements of type u16.
 */
static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
		unsigned int pipe)
{
	int max_streams;

	max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
	if (usb_pipeout(pipe))
		max_streams >>= 4;
	else
		max_streams &= 0xf;
	max_streams++;
	return max_streams;
}

static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
		unsigned int pipe, unsigned int streams)
{
	int max_streams;

	streams--;
	max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
	if (usb_pipeout(pipe)) {
		streams <<= 4;
		max_streams &= 0xf;
	} else {
		max_streams &= 0xf0;
	}
	max_streams |= streams;
	dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams;
}

static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb)
{
	unsigned int max_streams;
	int enabled;

	enabled = dummy_ep_stream_en(dum_hcd, urb);
	if (!urb->stream_id) {
		if (enabled)
			return -EINVAL;
		return 0;
	}
	if (!enabled)
		return -EINVAL;

	max_streams = get_max_streams_for_pipe(dum_hcd,
			usb_pipeendpoint(urb->pipe));
	if (urb->stream_id > max_streams) {
		dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n",
				urb->stream_id);
		BUG();
		return -EINVAL;
	}
	return 0;
}

1243
static int dummy_urb_enqueue(
L
Linus Torvalds 已提交
1244 1245
	struct usb_hcd			*hcd,
	struct urb			*urb,
A
Al Viro 已提交
1246
	gfp_t				mem_flags
L
Linus Torvalds 已提交
1247
) {
1248
	struct dummy_hcd *dum_hcd;
L
Linus Torvalds 已提交
1249 1250
	struct urbp	*urbp;
	unsigned long	flags;
1251
	int		rc;
L
Linus Torvalds 已提交
1252

1253
	urbp = kmalloc(sizeof *urbp, mem_flags);
L
Linus Torvalds 已提交
1254 1255 1256
	if (!urbp)
		return -ENOMEM;
	urbp->urb = urb;
1257
	urbp->miter_started = 0;
L
Linus Torvalds 已提交
1258

1259 1260
	dum_hcd = hcd_to_dummy_hcd(hcd);
	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1261 1262 1263 1264 1265 1266 1267

	rc = dummy_validate_stream(dum_hcd, urb);
	if (rc) {
		kfree(urbp);
		goto done;
	}

1268 1269 1270 1271 1272
	rc = usb_hcd_link_urb_to_ep(hcd, urb);
	if (rc) {
		kfree(urbp);
		goto done;
	}
L
Linus Torvalds 已提交
1273

1274 1275 1276 1277 1278
	if (!dum_hcd->udev) {
		dum_hcd->udev = urb->dev;
		usb_get_dev(dum_hcd->udev);
	} else if (unlikely(dum_hcd->udev != urb->dev))
		dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
L
Linus Torvalds 已提交
1279

1280
	list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
L
Linus Torvalds 已提交
1281
	urb->hcpriv = urbp;
1282 1283
	if (!dum_hcd->next_frame_urbp)
		dum_hcd->next_frame_urbp = urbp;
1284
	if (usb_pipetype(urb->pipe) == PIPE_CONTROL)
L
Linus Torvalds 已提交
1285 1286 1287
		urb->error_count = 1;		/* mark as a new urb */

	/* kick the scheduler, it'll do the rest */
1288 1289
	if (!timer_pending(&dum_hcd->timer))
		mod_timer(&dum_hcd->timer, jiffies + 1);
L
Linus Torvalds 已提交
1290

1291
 done:
1292
	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1293
	return rc;
L
Linus Torvalds 已提交
1294 1295
}

1296
static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
L
Linus Torvalds 已提交
1297
{
1298
	struct dummy_hcd *dum_hcd;
1299
	unsigned long	flags;
1300
	int		rc;
1301 1302 1303

	/* giveback happens automatically in timer callback,
	 * so make sure the callback happens */
1304 1305
	dum_hcd = hcd_to_dummy_hcd(hcd);
	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1306 1307

	rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1308 1309 1310
	if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
			!list_empty(&dum_hcd->urbp_list))
		mod_timer(&dum_hcd->timer, jiffies);
1311

1312
	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1313
	return rc;
L
Linus Torvalds 已提交
1314 1315
}

1316 1317 1318 1319
static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
		u32 len)
{
	void *ubuf, *rbuf;
1320
	struct urbp *urbp = urb->hcpriv;
1321
	int to_host;
1322 1323 1324 1325
	struct sg_mapping_iter *miter = &urbp->miter;
	u32 trans = 0;
	u32 this_sg;
	bool next_sg;
1326 1327 1328 1329

	to_host = usb_pipein(urb->pipe);
	rbuf = req->req.buf + req->req.actual;

1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
	if (!urb->num_sgs) {
		ubuf = urb->transfer_buffer + urb->actual_length;
		if (to_host)
			memcpy(ubuf, rbuf, len);
		else
			memcpy(rbuf, ubuf, len);
		return len;
	}

	if (!urbp->miter_started) {
		u32 flags = SG_MITER_ATOMIC;

		if (to_host)
			flags |= SG_MITER_TO_SG;
		else
			flags |= SG_MITER_FROM_SG;

		sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
		urbp->miter_started = 1;
	}
	next_sg = sg_miter_next(miter);
	if (next_sg == false) {
		WARN_ON_ONCE(1);
		return -EINVAL;
	}
	do {
		ubuf = miter->addr;
		this_sg = min_t(u32, len, miter->length);
		miter->consumed = this_sg;
		trans += this_sg;

		if (to_host)
			memcpy(ubuf, rbuf, this_sg);
		else
			memcpy(rbuf, ubuf, this_sg);
		len -= this_sg;

		if (!len)
			break;
		next_sg = sg_miter_next(miter);
		if (next_sg == false) {
			WARN_ON_ONCE(1);
			return -EINVAL;
		}

		rbuf += this_sg;
	} while (1);

	sg_miter_stop(miter);
	return trans;
1380 1381
}

L
Linus Torvalds 已提交
1382
/* transfer up to a frame's worth; caller must own lock */
1383 1384
static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb,
		struct dummy_ep *ep, int limit, int *status)
L
Linus Torvalds 已提交
1385
{
1386
	struct dummy		*dum = dum_hcd->dum;
L
Linus Torvalds 已提交
1387
	struct dummy_request	*req;
1388
	int			sent = 0;
L
Linus Torvalds 已提交
1389 1390 1391

top:
	/* if there's no request queued, the device is NAKing; return */
1392
	list_for_each_entry(req, &ep->queue, queue) {
L
Linus Torvalds 已提交
1393 1394 1395 1396
		unsigned	host_len, dev_len, len;
		int		is_short, to_host;
		int		rescan = 0;

1397 1398 1399 1400 1401
		if (dummy_ep_stream_en(dum_hcd, urb)) {
			if ((urb->stream_id != req->req.stream_id))
				continue;
		}

L
Linus Torvalds 已提交
1402 1403 1404 1405 1406 1407 1408 1409 1410
		/* 1..N packets of ep->ep.maxpacket each ... the last one
		 * may be short (including zero length).
		 *
		 * writer can send a zlp explicitly (length 0) or implicitly
		 * (length mod maxpacket zero, and 'zero' flag); they always
		 * terminate reads.
		 */
		host_len = urb->transfer_buffer_length - urb->actual_length;
		dev_len = req->req.length - req->req.actual;
1411
		len = min(host_len, dev_len);
L
Linus Torvalds 已提交
1412 1413 1414

		/* FIXME update emulated data toggle too */

1415 1416
		to_host = usb_pipein(urb->pipe);
		if (unlikely(len == 0))
L
Linus Torvalds 已提交
1417 1418 1419 1420 1421
			is_short = 1;
		else {
			/* not enough bandwidth left? */
			if (limit < ep->ep.maxpacket && limit < len)
				break;
1422
			len = min_t(unsigned, len, limit);
L
Linus Torvalds 已提交
1423 1424 1425
			if (len == 0)
				break;

1426 1427 1428 1429 1430 1431 1432 1433
			/* send multiple of maxpacket first, then remainder */
			if (len >= ep->ep.maxpacket) {
				is_short = 0;
				if (len % ep->ep.maxpacket)
					rescan = 1;
				len -= len % ep->ep.maxpacket;
			} else {
				is_short = 1;
L
Linus Torvalds 已提交
1434 1435
			}

1436 1437
			len = dummy_perform_transfer(urb, req, len);

L
Linus Torvalds 已提交
1438
			ep->last_io = jiffies;
1439
			if ((int)len < 0) {
1440 1441 1442
				req->req.status = len;
			} else {
				limit -= len;
1443
				sent += len;
1444 1445 1446
				urb->actual_length += len;
				req->req.actual += len;
			}
L
Linus Torvalds 已提交
1447 1448 1449 1450 1451 1452 1453
		}

		/* short packets terminate, maybe with overflow/underflow.
		 * it's only really an error to write too much.
		 *
		 * partially filling a buffer optionally blocks queue advances
		 * (so completion handlers can clean up the queue) but we don't
A
Alan Stern 已提交
1454
		 * need to emulate such data-in-flight.
L
Linus Torvalds 已提交
1455 1456 1457 1458
		 */
		if (is_short) {
			if (host_len == dev_len) {
				req->req.status = 0;
1459
				*status = 0;
L
Linus Torvalds 已提交
1460 1461 1462
			} else if (to_host) {
				req->req.status = 0;
				if (dev_len > host_len)
1463
					*status = -EOVERFLOW;
L
Linus Torvalds 已提交
1464
				else
1465
					*status = 0;
1466
			} else {
1467
				*status = 0;
L
Linus Torvalds 已提交
1468 1469 1470 1471 1472 1473
				if (host_len > dev_len)
					req->req.status = -EOVERFLOW;
				else
					req->req.status = 0;
			}

1474 1475 1476 1477
		/*
		 * many requests terminate without a short packet.
		 * send a zlp if demanded by flags.
		 */
L
Linus Torvalds 已提交
1478
		} else {
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
			if (req->req.length == req->req.actual) {
				if (req->req.zero && to_host)
					rescan = 1;
				else
					req->req.status = 0;
			}
			if (urb->transfer_buffer_length == urb->actual_length) {
				if (urb->transfer_flags & URB_ZERO_PACKET &&
				    !to_host)
					rescan = 1;
				else
					*status = 0;
			}
L
Linus Torvalds 已提交
1492 1493 1494 1495
		}

		/* device side completion --> continuable */
		if (req->req.status != -EINPROGRESS) {
1496
			list_del_init(&req->queue);
L
Linus Torvalds 已提交
1497

1498
			spin_unlock(&dum->lock);
1499
			usb_gadget_giveback_request(&ep->ep, &req->req);
1500
			spin_lock(&dum->lock);
L
Linus Torvalds 已提交
1501 1502 1503 1504 1505 1506

			/* requests might have been unlinked... */
			rescan = 1;
		}

		/* host side completion --> terminate */
1507
		if (*status != -EINPROGRESS)
L
Linus Torvalds 已提交
1508 1509 1510 1511 1512 1513
			break;

		/* rescan to continue with any other queued i/o */
		if (rescan)
			goto top;
	}
1514
	return sent;
L
Linus Torvalds 已提交
1515 1516
}

1517
static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep)
L
Linus Torvalds 已提交
1518 1519 1520 1521 1522 1523 1524
{
	int	limit = ep->ep.maxpacket;

	if (dum->gadget.speed == USB_SPEED_HIGH) {
		int	tmp;

		/* high bandwidth mode */
1525
		tmp = usb_endpoint_maxp_mult(ep->desc);
L
Linus Torvalds 已提交
1526 1527 1528
		tmp *= 8 /* applies to entire frame */;
		limit += limit * tmp;
	}
1529
	if (dum->gadget.speed == USB_SPEED_SUPER) {
1530
		switch (usb_endpoint_type(ep->desc)) {
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
		case USB_ENDPOINT_XFER_ISOC:
			/* Sec. 4.4.8.2 USB3.0 Spec */
			limit = 3 * 16 * 1024 * 8;
			break;
		case USB_ENDPOINT_XFER_INT:
			/* Sec. 4.4.7.2 USB3.0 Spec */
			limit = 3 * 1024 * 8;
			break;
		case USB_ENDPOINT_XFER_BULK:
		default:
			break;
		}
	}
L
Linus Torvalds 已提交
1544 1545 1546
	return limit;
}

1547
#define is_active(dum_hcd)	((dum_hcd->port_status & \
L
Linus Torvalds 已提交
1548 1549 1550 1551
		(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
			USB_PORT_STAT_SUSPEND)) \
		== (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))

1552
static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address)
L
Linus Torvalds 已提交
1553 1554 1555
{
	int		i;

1556 1557
	if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
			dum->ss_hcd : dum->hs_hcd)))
L
Linus Torvalds 已提交
1558
		return NULL;
1559 1560
	if (!dum->ints_enabled)
		return NULL;
L
Linus Torvalds 已提交
1561
	if ((address & ~USB_DIR_IN) == 0)
1562
		return &dum->ep[0];
L
Linus Torvalds 已提交
1563
	for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1564
		struct dummy_ep	*ep = &dum->ep[i];
L
Linus Torvalds 已提交
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582

		if (!ep->desc)
			continue;
		if (ep->desc->bEndpointAddress == address)
			return ep;
	}
	return NULL;
}

#undef is_active

#define Dev_Request	(USB_TYPE_STANDARD | USB_RECIP_DEVICE)
#define Dev_InRequest	(Dev_Request | USB_DIR_IN)
#define Intf_Request	(USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
#define Intf_InRequest	(Intf_Request | USB_DIR_IN)
#define Ep_Request	(USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
#define Ep_InRequest	(Ep_Request | USB_DIR_IN)

1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595

/**
 * handle_control_request() - handles all control transfers
 * @dum: pointer to dummy (the_controller)
 * @urb: the urb request to handle
 * @setup: pointer to the setup data for a USB device control
 *	 request
 * @status: pointer to request handling status
 *
 * Return 0 - if the request was handled
 *	  1 - if the request wasn't handles
 *	  error code on error
 */
1596
static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
1597 1598 1599 1600
				  struct usb_ctrlrequest *setup,
				  int *status)
{
	struct dummy_ep		*ep2;
1601
	struct dummy		*dum = dum_hcd->dum;
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
	int			ret_val = 1;
	unsigned	w_index;
	unsigned	w_value;

	w_index = le16_to_cpu(setup->wIndex);
	w_value = le16_to_cpu(setup->wValue);
	switch (setup->bRequest) {
	case USB_REQ_SET_ADDRESS:
		if (setup->bRequestType != Dev_Request)
			break;
		dum->address = w_value;
		*status = 0;
		dev_dbg(udc_dev(dum), "set_address = %d\n",
				w_value);
		ret_val = 0;
		break;
	case USB_REQ_SET_FEATURE:
		if (setup->bRequestType == Dev_Request) {
			ret_val = 0;
			switch (w_value) {
			case USB_DEVICE_REMOTE_WAKEUP:
				break;
			case USB_DEVICE_B_HNP_ENABLE:
				dum->gadget.b_hnp_enable = 1;
				break;
			case USB_DEVICE_A_HNP_SUPPORT:
				dum->gadget.a_hnp_support = 1;
				break;
			case USB_DEVICE_A_ALT_HNP_SUPPORT:
				dum->gadget.a_alt_hnp_support = 1;
				break;
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
			case USB_DEVICE_U1_ENABLE:
				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
				    HCD_USB3)
					w_value = USB_DEV_STAT_U1_ENABLED;
				else
					ret_val = -EOPNOTSUPP;
				break;
			case USB_DEVICE_U2_ENABLE:
				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
				    HCD_USB3)
					w_value = USB_DEV_STAT_U2_ENABLED;
				else
					ret_val = -EOPNOTSUPP;
				break;
			case USB_DEVICE_LTM_ENABLE:
				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
				    HCD_USB3)
					w_value = USB_DEV_STAT_LTM_ENABLED;
				else
					ret_val = -EOPNOTSUPP;
				break;
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
			default:
				ret_val = -EOPNOTSUPP;
			}
			if (ret_val == 0) {
				dum->devstatus |= (1 << w_value);
				*status = 0;
			}
		} else if (setup->bRequestType == Ep_Request) {
			/* endpoint halt */
			ep2 = find_endpoint(dum, w_index);
			if (!ep2 || ep2->ep.name == ep0name) {
				ret_val = -EOPNOTSUPP;
				break;
			}
			ep2->halted = 1;
			ret_val = 0;
			*status = 0;
		}
		break;
	case USB_REQ_CLEAR_FEATURE:
		if (setup->bRequestType == Dev_Request) {
			ret_val = 0;
			switch (w_value) {
			case USB_DEVICE_REMOTE_WAKEUP:
				w_value = USB_DEVICE_REMOTE_WAKEUP;
				break;
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
			case USB_DEVICE_U1_ENABLE:
				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
				    HCD_USB3)
					w_value = USB_DEV_STAT_U1_ENABLED;
				else
					ret_val = -EOPNOTSUPP;
				break;
			case USB_DEVICE_U2_ENABLE:
				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
				    HCD_USB3)
					w_value = USB_DEV_STAT_U2_ENABLED;
				else
					ret_val = -EOPNOTSUPP;
				break;
			case USB_DEVICE_LTM_ENABLE:
				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
				    HCD_USB3)
					w_value = USB_DEV_STAT_LTM_ENABLED;
				else
					ret_val = -EOPNOTSUPP;
				break;
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
			default:
				ret_val = -EOPNOTSUPP;
				break;
			}
			if (ret_val == 0) {
				dum->devstatus &= ~(1 << w_value);
				*status = 0;
			}
		} else if (setup->bRequestType == Ep_Request) {
			/* endpoint halt */
			ep2 = find_endpoint(dum, w_index);
			if (!ep2) {
				ret_val = -EOPNOTSUPP;
				break;
			}
			if (!ep2->wedged)
				ep2->halted = 0;
			ret_val = 0;
			*status = 0;
		}
		break;
	case USB_REQ_GET_STATUS:
		if (setup->bRequestType == Dev_InRequest
				|| setup->bRequestType == Intf_InRequest
				|| setup->bRequestType == Ep_InRequest) {
			char *buf;
			/*
			 * device: remote wakeup, selfpowered
			 * interface: nothing
			 * endpoint: halt
			 */
			buf = (char *)urb->transfer_buffer;
			if (urb->transfer_buffer_length > 0) {
				if (setup->bRequestType == Ep_InRequest) {
					ep2 = find_endpoint(dum, w_index);
					if (!ep2) {
						ret_val = -EOPNOTSUPP;
						break;
					}
					buf[0] = ep2->halted;
				} else if (setup->bRequestType ==
					   Dev_InRequest) {
					buf[0] = (u8)dum->devstatus;
				} else
					buf[0] = 0;
			}
			if (urb->transfer_buffer_length > 1)
				buf[1] = 0;
			urb->actual_length = min_t(u32, 2,
				urb->transfer_buffer_length);
			ret_val = 0;
			*status = 0;
		}
		break;
	}
	return ret_val;
}

L
Linus Torvalds 已提交
1759 1760 1761
/* drive both sides of the transfers; looks like irq handlers to
 * both drivers except the callbacks aren't in_irq().
 */
1762
static void dummy_timer(unsigned long _dum_hcd)
L
Linus Torvalds 已提交
1763
{
1764 1765
	struct dummy_hcd	*dum_hcd = (struct dummy_hcd *) _dum_hcd;
	struct dummy		*dum = dum_hcd->dum;
L
Linus Torvalds 已提交
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
	struct urbp		*urbp, *tmp;
	unsigned long		flags;
	int			limit, total;
	int			i;

	/* simplistic model for one frame's bandwidth */
	switch (dum->gadget.speed) {
	case USB_SPEED_LOW:
		total = 8/*bytes*/ * 12/*packets*/;
		break;
	case USB_SPEED_FULL:
		total = 64/*bytes*/ * 19/*packets*/;
		break;
	case USB_SPEED_HIGH:
		total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
		break;
1782 1783 1784 1785
	case USB_SPEED_SUPER:
		/* Bus speed is 500000 bytes/ms, so use a little less */
		total = 490000;
		break;
L
Linus Torvalds 已提交
1786
	default:
1787
		dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
L
Linus Torvalds 已提交
1788 1789 1790 1791 1792 1793
		return;
	}

	/* FIXME if HZ != 1000 this will probably misbehave ... */

	/* look at each urb queued by the host side driver */
1794
	spin_lock_irqsave(&dum->lock, flags);
L
Linus Torvalds 已提交
1795

1796 1797
	if (!dum_hcd->udev) {
		dev_err(dummy_dev(dum_hcd),
L
Linus Torvalds 已提交
1798
				"timer fired with no URBs pending?\n");
1799
		spin_unlock_irqrestore(&dum->lock, flags);
L
Linus Torvalds 已提交
1800 1801
		return;
	}
1802
	dum_hcd->next_frame_urbp = NULL;
L
Linus Torvalds 已提交
1803 1804

	for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1805
		if (!ep_info[i].name)
L
Linus Torvalds 已提交
1806
			break;
1807
		dum->ep[i].already_seen = 0;
L
Linus Torvalds 已提交
1808 1809 1810
	}

restart:
1811
	list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
L
Linus Torvalds 已提交
1812 1813 1814 1815 1816
		struct urb		*urb;
		struct dummy_request	*req;
		u8			address;
		struct dummy_ep		*ep = NULL;
		int			type;
1817
		int			status = -EINPROGRESS;
L
Linus Torvalds 已提交
1818

1819 1820 1821 1822
		/* stop when we reach URBs queued after the timer interrupt */
		if (urbp == dum_hcd->next_frame_urbp)
			break;

L
Linus Torvalds 已提交
1823
		urb = urbp->urb;
A
Alan Stern 已提交
1824
		if (urb->unlinked)
L
Linus Torvalds 已提交
1825
			goto return_urb;
1826
		else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
1827
			continue;
1828
		type = usb_pipetype(urb->pipe);
L
Linus Torvalds 已提交
1829 1830 1831 1832 1833 1834 1835 1836 1837 1838

		/* used up this frame's non-periodic bandwidth?
		 * FIXME there's infinite bandwidth for control and
		 * periodic transfers ... unrealistic.
		 */
		if (total <= 0 && type == PIPE_BULK)
			continue;

		/* find the gadget's ep for this request (if configured) */
		address = usb_pipeendpoint (urb->pipe);
1839
		if (usb_pipein(urb->pipe))
L
Linus Torvalds 已提交
1840 1841 1842 1843
			address |= USB_DIR_IN;
		ep = find_endpoint(dum, address);
		if (!ep) {
			/* set_configuration() disagreement */
1844
			dev_dbg(dummy_dev(dum_hcd),
L
Linus Torvalds 已提交
1845 1846
				"no ep configured for urb %p\n",
				urb);
1847
			status = -EPROTO;
L
Linus Torvalds 已提交
1848 1849 1850 1851 1852 1853
			goto return_urb;
		}

		if (ep->already_seen)
			continue;
		ep->already_seen = 1;
1854
		if (ep == &dum->ep[0] && urb->error_count) {
L
Linus Torvalds 已提交
1855 1856 1857 1858 1859
			ep->setup_stage = 1;	/* a new urb */
			urb->error_count = 0;
		}
		if (ep->halted && !ep->setup_stage) {
			/* NOTE: must not be iso! */
1860
			dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
L
Linus Torvalds 已提交
1861
					ep->ep.name, urb);
1862
			status = -EPIPE;
L
Linus Torvalds 已提交
1863 1864 1865 1866 1867
			goto return_urb;
		}
		/* FIXME make sure both ends agree on maxpacket */

		/* handle control requests */
1868
		if (ep == &dum->ep[0] && ep->setup_stage) {
L
Linus Torvalds 已提交
1869 1870 1871
			struct usb_ctrlrequest		setup;
			int				value = 1;

1872
			setup = *(struct usb_ctrlrequest *) urb->setup_packet;
L
Linus Torvalds 已提交
1873
			/* paranoia, in case of stale queued data */
1874 1875
			list_for_each_entry(req, &ep->queue, queue) {
				list_del_init(&req->queue);
L
Linus Torvalds 已提交
1876
				req->req.status = -EOVERFLOW;
1877
				dev_dbg(udc_dev(dum), "stale req = %p\n",
L
Linus Torvalds 已提交
1878 1879
						req);

1880
				spin_unlock(&dum->lock);
1881
				usb_gadget_giveback_request(&ep->ep, &req->req);
1882
				spin_lock(&dum->lock);
L
Linus Torvalds 已提交
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894
				ep->already_seen = 0;
				goto restart;
			}

			/* gadget driver never sees set_address or operations
			 * on standard feature flags.  some hardware doesn't
			 * even expose them.
			 */
			ep->last_io = jiffies;
			ep->setup_stage = 0;
			ep->halted = 0;

1895
			value = handle_control_request(dum_hcd, urb, &setup,
1896
						       &status);
L
Linus Torvalds 已提交
1897 1898 1899 1900 1901

			/* gadget driver handles all other requests.  block
			 * until setup() returns; no reentrancy issues etc.
			 */
			if (value > 0) {
1902
				++dum->callback_usage;
1903 1904
				spin_unlock(&dum->lock);
				value = dum->driver->setup(&dum->gadget,
L
Linus Torvalds 已提交
1905
						&setup);
1906
				spin_lock(&dum->lock);
1907
				--dum->callback_usage;
L
Linus Torvalds 已提交
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918

				if (value >= 0) {
					/* no delays (max 64KB data stage) */
					limit = 64*1024;
					goto treat_control_like_bulk;
				}
				/* error, see below */
			}

			if (value < 0) {
				if (value != -EOPNOTSUPP)
1919
					dev_dbg(udc_dev(dum),
L
Linus Torvalds 已提交
1920 1921
						"setup --> %d\n",
						value);
1922
				status = -EPIPE;
L
Linus Torvalds 已提交
1923 1924 1925 1926 1927 1928 1929 1930
				urb->actual_length = 0;
			}

			goto return_urb;
		}

		/* non-control requests */
		limit = total;
1931
		switch (usb_pipetype(urb->pipe)) {
L
Linus Torvalds 已提交
1932 1933 1934 1935 1936 1937
		case PIPE_ISOCHRONOUS:
			/* FIXME is it urb->interval since the last xfer?
			 * use urb->iso_frame_desc[i].
			 * complete whether or not ep has requests queued.
			 * report random errors, to debug drivers.
			 */
1938
			limit = max(limit, periodic_bytes(dum, ep));
1939
			status = -ENOSYS;
L
Linus Torvalds 已提交
1940 1941 1942 1943 1944 1945
			break;

		case PIPE_INTERRUPT:
			/* FIXME is it urb->interval since the last xfer?
			 * this almost certainly polls too fast.
			 */
1946
			limit = max(limit, periodic_bytes(dum, ep));
L
Linus Torvalds 已提交
1947 1948 1949
			/* FALLTHROUGH */

		default:
1950
treat_control_like_bulk:
L
Linus Torvalds 已提交
1951
			ep->last_io = jiffies;
1952
			total -= transfer(dum_hcd, urb, ep, limit, &status);
L
Linus Torvalds 已提交
1953 1954 1955 1956
			break;
		}

		/* incomplete transfer? */
1957
		if (status == -EINPROGRESS)
L
Linus Torvalds 已提交
1958 1959 1960
			continue;

return_urb:
1961 1962
		list_del(&urbp->urbp_list);
		kfree(urbp);
L
Linus Torvalds 已提交
1963 1964 1965
		if (ep)
			ep->already_seen = ep->setup_stage = 0;

1966
		usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
1967
		spin_unlock(&dum->lock);
1968
		usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
1969
		spin_lock(&dum->lock);
L
Linus Torvalds 已提交
1970 1971 1972 1973

		goto restart;
	}

1974 1975 1976 1977
	if (list_empty(&dum_hcd->urbp_list)) {
		usb_put_dev(dum_hcd->udev);
		dum_hcd->udev = NULL;
	} else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
1978
		/* want a 1 msec delay here */
1979
		mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
L
Linus Torvalds 已提交
1980 1981
	}

1982
	spin_unlock_irqrestore(&dum->lock, flags);
L
Linus Torvalds 已提交
1983 1984 1985 1986 1987
}

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

#define PORT_C_MASK \
1988 1989 1990 1991 1992
	((USB_PORT_STAT_C_CONNECTION \
	| USB_PORT_STAT_C_ENABLE \
	| USB_PORT_STAT_C_SUSPEND \
	| USB_PORT_STAT_C_OVERCURRENT \
	| USB_PORT_STAT_C_RESET) << 16)
L
Linus Torvalds 已提交
1993

1994
static int dummy_hub_status(struct usb_hcd *hcd, char *buf)
L
Linus Torvalds 已提交
1995
{
1996
	struct dummy_hcd	*dum_hcd;
L
Linus Torvalds 已提交
1997
	unsigned long		flags;
1998
	int			retval = 0;
L
Linus Torvalds 已提交
1999

2000
	dum_hcd = hcd_to_dummy_hcd(hcd);
L
Linus Torvalds 已提交
2001

2002
	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2003
	if (!HCD_HW_ACCESSIBLE(hcd))
2004
		goto done;
2005

2006 2007 2008 2009
	if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
		dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
		dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
		set_link_state(dum_hcd);
2010 2011
	}

2012
	if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
L
Linus Torvalds 已提交
2013
		*buf = (1 << 1);
2014 2015
		dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
				dum_hcd->port_status);
L
Linus Torvalds 已提交
2016
		retval = 1;
2017
		if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
2018
			usb_hcd_resume_root_hub(hcd);
L
Linus Torvalds 已提交
2019
	}
2020
done:
2021
	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
L
Linus Torvalds 已提交
2022 2023 2024
	return retval;
}

2025
/* usb 3.0 root hub device descriptor */
2026
static struct {
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
	struct usb_bos_descriptor bos;
	struct usb_ss_cap_descriptor ss_cap;
} __packed usb3_bos_desc = {

	.bos = {
		.bLength		= USB_DT_BOS_SIZE,
		.bDescriptorType	= USB_DT_BOS,
		.wTotalLength		= cpu_to_le16(sizeof(usb3_bos_desc)),
		.bNumDeviceCaps		= 1,
	},
	.ss_cap = {
		.bLength		= USB_DT_USB_SS_CAP_SIZE,
		.bDescriptorType	= USB_DT_DEVICE_CAPABILITY,
		.bDevCapabilityType	= USB_SS_CAP_TYPE,
		.wSpeedSupported	= cpu_to_le16(USB_5GBPS_OPERATION),
		.bFunctionalitySupport	= ilog2(USB_5GBPS_OPERATION),
	},
};

2046 2047 2048 2049
static inline void
ss_hub_descriptor(struct usb_hub_descriptor *desc)
{
	memset(desc, 0, sizeof *desc);
S
Sergei Shtylyov 已提交
2050
	desc->bDescriptorType = USB_DT_SS_HUB;
2051
	desc->bDescLength = 12;
S
Sergei Shtylyov 已提交
2052 2053 2054
	desc->wHubCharacteristics = cpu_to_le16(
			HUB_CHAR_INDV_PORT_LPSM |
			HUB_CHAR_COMMON_OCPM);
2055 2056
	desc->bNbrPorts = 1;
	desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
2057
	desc->u.ss.DeviceRemovable = 0;
2058 2059
}

2060
static inline void hub_descriptor(struct usb_hub_descriptor *desc)
L
Linus Torvalds 已提交
2061
{
2062
	memset(desc, 0, sizeof *desc);
S
Sergei Shtylyov 已提交
2063
	desc->bDescriptorType = USB_DT_HUB;
L
Linus Torvalds 已提交
2064
	desc->bDescLength = 9;
S
Sergei Shtylyov 已提交
2065 2066 2067
	desc->wHubCharacteristics = cpu_to_le16(
			HUB_CHAR_INDV_PORT_LPSM |
			HUB_CHAR_COMMON_OCPM);
L
Linus Torvalds 已提交
2068
	desc->bNbrPorts = 1;
2069 2070
	desc->u.hs.DeviceRemovable[0] = 0;
	desc->u.hs.DeviceRemovable[1] = 0xff;	/* PortPwrCtrlMask */
L
Linus Torvalds 已提交
2071 2072
}

2073
static int dummy_hub_control(
L
Linus Torvalds 已提交
2074 2075 2076 2077 2078 2079 2080
	struct usb_hcd	*hcd,
	u16		typeReq,
	u16		wValue,
	u16		wIndex,
	char		*buf,
	u16		wLength
) {
2081
	struct dummy_hcd *dum_hcd;
L
Linus Torvalds 已提交
2082 2083 2084
	int		retval = 0;
	unsigned long	flags;

2085
	if (!HCD_HW_ACCESSIBLE(hcd))
2086 2087
		return -ETIMEDOUT;

2088 2089 2090
	dum_hcd = hcd_to_dummy_hcd(hcd);

	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
L
Linus Torvalds 已提交
2091 2092 2093 2094 2095 2096
	switch (typeReq) {
	case ClearHubFeature:
		break;
	case ClearPortFeature:
		switch (wValue) {
		case USB_PORT_FEAT_SUSPEND:
2097 2098 2099 2100 2101 2102
			if (hcd->speed == HCD_USB3) {
				dev_dbg(dummy_dev(dum_hcd),
					 "USB_PORT_FEAT_SUSPEND req not "
					 "supported for USB 3.0 roothub\n");
				goto error;
			}
2103
			if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
L
Linus Torvalds 已提交
2104
				/* 20msec resume signaling */
2105 2106
				dum_hcd->resuming = 1;
				dum_hcd->re_timeout = jiffies +
2107
						msecs_to_jiffies(20);
L
Linus Torvalds 已提交
2108 2109 2110
			}
			break;
		case USB_PORT_FEAT_POWER:
2111 2112 2113 2114 2115 2116 2117
			dev_dbg(dummy_dev(dum_hcd), "power-off\n");
			if (hcd->speed == HCD_USB3)
				dum_hcd->port_status &= ~USB_SS_PORT_STAT_POWER;
			else
				dum_hcd->port_status &= ~USB_PORT_STAT_POWER;
			set_link_state(dum_hcd);
			break;
L
Linus Torvalds 已提交
2118
		default:
2119 2120
			dum_hcd->port_status &= ~(1 << wValue);
			set_link_state(dum_hcd);
L
Linus Torvalds 已提交
2121 2122 2123
		}
		break;
	case GetHubDescriptor:
2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
		if (hcd->speed == HCD_USB3 &&
				(wLength < USB_DT_SS_HUB_SIZE ||
				 wValue != (USB_DT_SS_HUB << 8))) {
			dev_dbg(dummy_dev(dum_hcd),
				"Wrong hub descriptor type for "
				"USB 3.0 roothub.\n");
			goto error;
		}
		if (hcd->speed == HCD_USB3)
			ss_hub_descriptor((struct usb_hub_descriptor *) buf);
		else
			hub_descriptor((struct usb_hub_descriptor *) buf);
L
Linus Torvalds 已提交
2136
		break;
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148

	case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
		if (hcd->speed != HCD_USB3)
			goto error;

		if ((wValue >> 8) != USB_DT_BOS)
			goto error;

		memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc));
		retval = sizeof(usb3_bos_desc);
		break;

L
Linus Torvalds 已提交
2149
	case GetHubStatus:
2150
		*(__le32 *) buf = cpu_to_le32(0);
L
Linus Torvalds 已提交
2151 2152 2153 2154 2155 2156 2157 2158
		break;
	case GetPortStatus:
		if (wIndex != 1)
			retval = -EPIPE;

		/* whoever resets or resumes must GetPortStatus to
		 * complete it!!
		 */
2159 2160 2161 2162
		if (dum_hcd->resuming &&
				time_after_eq(jiffies, dum_hcd->re_timeout)) {
			dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
			dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
L
Linus Torvalds 已提交
2163
		}
2164 2165 2166 2167 2168 2169
		if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
				time_after_eq(jiffies, dum_hcd->re_timeout)) {
			dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
			dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
			if (dum_hcd->dum->pullup) {
				dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187

				if (hcd->speed < HCD_USB3) {
					switch (dum_hcd->dum->gadget.speed) {
					case USB_SPEED_HIGH:
						dum_hcd->port_status |=
						      USB_PORT_STAT_HIGH_SPEED;
						break;
					case USB_SPEED_LOW:
						dum_hcd->dum->gadget.ep0->
							maxpacket = 8;
						dum_hcd->port_status |=
							USB_PORT_STAT_LOW_SPEED;
						break;
					default:
						dum_hcd->dum->gadget.speed =
							USB_SPEED_FULL;
						break;
					}
L
Linus Torvalds 已提交
2188 2189 2190
				}
			}
		}
2191
		set_link_state(dum_hcd);
2192 2193
		((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status);
		((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16);
L
Linus Torvalds 已提交
2194 2195 2196 2197 2198 2199
		break;
	case SetHubFeature:
		retval = -EPIPE;
		break;
	case SetPortFeature:
		switch (wValue) {
2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221
		case USB_PORT_FEAT_LINK_STATE:
			if (hcd->speed != HCD_USB3) {
				dev_dbg(dummy_dev(dum_hcd),
					 "USB_PORT_FEAT_LINK_STATE req not "
					 "supported for USB 2.0 roothub\n");
				goto error;
			}
			/*
			 * Since this is dummy we don't have an actual link so
			 * there is nothing to do for the SET_LINK_STATE cmd
			 */
			break;
		case USB_PORT_FEAT_U1_TIMEOUT:
		case USB_PORT_FEAT_U2_TIMEOUT:
			/* TODO: add suspend/resume support! */
			if (hcd->speed != HCD_USB3) {
				dev_dbg(dummy_dev(dum_hcd),
					 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
					 "supported for USB 2.0 roothub\n");
				goto error;
			}
			break;
L
Linus Torvalds 已提交
2222
		case USB_PORT_FEAT_SUSPEND:
2223 2224 2225 2226 2227 2228 2229
			/* Applicable only for USB2.0 hub */
			if (hcd->speed == HCD_USB3) {
				dev_dbg(dummy_dev(dum_hcd),
					 "USB_PORT_FEAT_SUSPEND req not "
					 "supported for USB 3.0 roothub\n");
				goto error;
			}
2230 2231
			if (dum_hcd->active) {
				dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
2232 2233 2234 2235

				/* HNP would happen here; for now we
				 * assume b_bus_req is always true.
				 */
2236
				set_link_state(dum_hcd);
2237
				if (((1 << USB_DEVICE_B_HNP_ENABLE)
2238 2239
						& dum_hcd->dum->devstatus) != 0)
					dev_dbg(dummy_dev(dum_hcd),
2240
							"no HNP yet!\n");
L
Linus Torvalds 已提交
2241 2242
			}
			break;
2243
		case USB_PORT_FEAT_POWER:
2244 2245 2246 2247
			if (hcd->speed == HCD_USB3)
				dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
			else
				dum_hcd->port_status |= USB_PORT_STAT_POWER;
2248
			set_link_state(dum_hcd);
2249
			break;
2250 2251 2252 2253 2254 2255 2256 2257 2258
		case USB_PORT_FEAT_BH_PORT_RESET:
			/* Applicable only for USB3.0 hub */
			if (hcd->speed != HCD_USB3) {
				dev_dbg(dummy_dev(dum_hcd),
					 "USB_PORT_FEAT_BH_PORT_RESET req not "
					 "supported for USB 2.0 roothub\n");
				goto error;
			}
			/* FALLS THROUGH */
L
Linus Torvalds 已提交
2259
		case USB_PORT_FEAT_RESET:
2260
			/* if it's already enabled, disable */
2261 2262 2263 2264 2265 2266 2267 2268
			if (hcd->speed == HCD_USB3) {
				dum_hcd->port_status = 0;
				dum_hcd->port_status =
					(USB_SS_PORT_STAT_POWER |
					 USB_PORT_STAT_CONNECTION |
					 USB_PORT_STAT_RESET);
			} else
				dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
2269 2270
					| USB_PORT_STAT_LOW_SPEED
					| USB_PORT_STAT_HIGH_SPEED);
2271 2272 2273 2274 2275 2276
			/*
			 * We want to reset device status. All but the
			 * Self powered feature
			 */
			dum_hcd->dum->devstatus &=
				(1 << USB_DEVICE_SELF_POWERED);
2277 2278 2279 2280
			/*
			 * FIXME USB3.0: what is the correct reset signaling
			 * interval? Is it still 50msec as for HS?
			 */
2281
			dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
2282
			/* FALLS THROUGH */
L
Linus Torvalds 已提交
2283
		default:
2284 2285 2286 2287 2288 2289 2290 2291 2292 2293
			if (hcd->speed == HCD_USB3) {
				if ((dum_hcd->port_status &
				     USB_SS_PORT_STAT_POWER) != 0) {
					dum_hcd->port_status |= (1 << wValue);
				}
			} else
				if ((dum_hcd->port_status &
				     USB_PORT_STAT_POWER) != 0) {
					dum_hcd->port_status |= (1 << wValue);
				}
2294
			set_link_state(dum_hcd);
2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312
		}
		break;
	case GetPortErrorCount:
		if (hcd->speed != HCD_USB3) {
			dev_dbg(dummy_dev(dum_hcd),
				 "GetPortErrorCount req not "
				 "supported for USB 2.0 roothub\n");
			goto error;
		}
		/* We'll always return 0 since this is a dummy hub */
		*(__le32 *) buf = cpu_to_le32(0);
		break;
	case SetHubDepth:
		if (hcd->speed != HCD_USB3) {
			dev_dbg(dummy_dev(dum_hcd),
				 "SetHubDepth req not supported for "
				 "USB 2.0 roothub\n");
			goto error;
L
Linus Torvalds 已提交
2313 2314 2315
		}
		break;
	default:
2316
		dev_dbg(dummy_dev(dum_hcd),
L
Linus Torvalds 已提交
2317 2318
			"hub control req%04x v%04x i%04x l%d\n",
			typeReq, wValue, wIndex, wLength);
2319
error:
L
Linus Torvalds 已提交
2320 2321 2322
		/* "protocol stall" on error */
		retval = -EPIPE;
	}
2323
	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2324

2325
	if ((dum_hcd->port_status & PORT_C_MASK) != 0)
2326
		usb_hcd_poll_rh_status(hcd);
L
Linus Torvalds 已提交
2327 2328 2329
	return retval;
}

2330
static int dummy_bus_suspend(struct usb_hcd *hcd)
2331
{
2332
	struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2333

2334
	dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
2335

2336 2337 2338
	spin_lock_irq(&dum_hcd->dum->lock);
	dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
	set_link_state(dum_hcd);
2339
	hcd->state = HC_STATE_SUSPENDED;
2340
	spin_unlock_irq(&dum_hcd->dum->lock);
2341 2342 2343
	return 0;
}

2344
static int dummy_bus_resume(struct usb_hcd *hcd)
2345
{
2346
	struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2347 2348
	int rc = 0;

2349
	dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
2350

2351
	spin_lock_irq(&dum_hcd->dum->lock);
2352
	if (!HCD_HW_ACCESSIBLE(hcd)) {
2353
		rc = -ESHUTDOWN;
2354
	} else {
2355 2356 2357 2358
		dum_hcd->rh_state = DUMMY_RH_RUNNING;
		set_link_state(dum_hcd);
		if (!list_empty(&dum_hcd->urbp_list))
			mod_timer(&dum_hcd->timer, jiffies);
2359 2360
		hcd->state = HC_STATE_RUNNING;
	}
2361
	spin_unlock_irq(&dum_hcd->dum->lock);
2362
	return rc;
2363
}
L
Linus Torvalds 已提交
2364 2365 2366

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

2367
static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb)
L
Linus Torvalds 已提交
2368
{
2369
	int ep = usb_pipeendpoint(urb->pipe);
L
Linus Torvalds 已提交
2370

2371
	return snprintf(buf, size,
L
Linus Torvalds 已提交
2372 2373 2374
		"urb/%p %s ep%d%s%s len %d/%d\n",
		urb,
		({ char *s;
2375 2376
		switch (urb->dev->speed) {
		case USB_SPEED_LOW:
T
Tatyana Brokhman 已提交
2377 2378
			s = "ls";
			break;
2379
		case USB_SPEED_FULL:
T
Tatyana Brokhman 已提交
2380 2381
			s = "fs";
			break;
2382
		case USB_SPEED_HIGH:
T
Tatyana Brokhman 已提交
2383 2384
			s = "hs";
			break;
2385
		case USB_SPEED_SUPER:
2386 2387
			s = "ss";
			break;
2388
		default:
T
Tatyana Brokhman 已提交
2389 2390
			s = "?";
			break;
J
Joe Perches 已提交
2391
		 } s; }),
2392
		ep, ep ? (usb_pipein(urb->pipe) ? "in" : "out") : "",
L
Linus Torvalds 已提交
2393
		({ char *s; \
2394 2395
		switch (usb_pipetype(urb->pipe)) { \
		case PIPE_CONTROL: \
T
Tatyana Brokhman 已提交
2396 2397
			s = ""; \
			break; \
2398
		case PIPE_BULK: \
T
Tatyana Brokhman 已提交
2399 2400
			s = "-bulk"; \
			break; \
2401
		case PIPE_INTERRUPT: \
T
Tatyana Brokhman 已提交
2402 2403
			s = "-int"; \
			break; \
2404
		default: \
T
Tatyana Brokhman 已提交
2405 2406
			s = "-iso"; \
			break; \
J
Joe Perches 已提交
2407
		} s; }),
L
Linus Torvalds 已提交
2408 2409 2410
		urb->actual_length, urb->transfer_buffer_length);
}

2411
static ssize_t urbs_show(struct device *dev, struct device_attribute *attr,
2412
		char *buf)
L
Linus Torvalds 已提交
2413
{
2414
	struct usb_hcd		*hcd = dev_get_drvdata(dev);
2415
	struct dummy_hcd	*dum_hcd = hcd_to_dummy_hcd(hcd);
L
Linus Torvalds 已提交
2416 2417 2418 2419
	struct urbp		*urbp;
	size_t			size = 0;
	unsigned long		flags;

2420 2421
	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
	list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
L
Linus Torvalds 已提交
2422 2423
		size_t		temp;

2424
		temp = show_urb(buf, PAGE_SIZE - size, urbp->urb);
L
Linus Torvalds 已提交
2425 2426 2427
		buf += temp;
		size += temp;
	}
2428
	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
L
Linus Torvalds 已提交
2429 2430 2431

	return size;
}
2432
static DEVICE_ATTR_RO(urbs);
L
Linus Torvalds 已提交
2433

2434 2435 2436 2437 2438 2439
static int dummy_start_ss(struct dummy_hcd *dum_hcd)
{
	init_timer(&dum_hcd->timer);
	dum_hcd->timer.function = dummy_timer;
	dum_hcd->timer.data = (unsigned long)dum_hcd;
	dum_hcd->rh_state = DUMMY_RH_RUNNING;
2440
	dum_hcd->stream_en_ep = 0;
2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453
	INIT_LIST_HEAD(&dum_hcd->urbp_list);
	dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
	dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
	dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
#ifdef CONFIG_USB_OTG
	dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
#endif
	return 0;

	/* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
	return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
}

2454
static int dummy_start(struct usb_hcd *hcd)
L
Linus Torvalds 已提交
2455
{
2456
	struct dummy_hcd	*dum_hcd = hcd_to_dummy_hcd(hcd);
L
Linus Torvalds 已提交
2457 2458 2459 2460 2461 2462

	/*
	 * MASTER side init ... we emulate a root hub that'll only ever
	 * talk to one device (the slave side).  Also appears in sysfs,
	 * just like more familiar pci-based HCDs.
	 */
2463 2464 2465
	if (!usb_hcd_is_primary_hcd(hcd))
		return dummy_start_ss(dum_hcd);

2466 2467 2468 2469 2470
	spin_lock_init(&dum_hcd->dum->lock);
	init_timer(&dum_hcd->timer);
	dum_hcd->timer.function = dummy_timer;
	dum_hcd->timer.data = (unsigned long)dum_hcd;
	dum_hcd->rh_state = DUMMY_RH_RUNNING;
L
Linus Torvalds 已提交
2471

2472
	INIT_LIST_HEAD(&dum_hcd->urbp_list);
L
Linus Torvalds 已提交
2473

2474
	hcd->power_budget = POWER_BUDGET;
L
Linus Torvalds 已提交
2475
	hcd->state = HC_STATE_RUNNING;
2476
	hcd->uses_new_polling = 1;
L
Linus Torvalds 已提交
2477

2478 2479 2480 2481
#ifdef CONFIG_USB_OTG
	hcd->self.otg_port = 1;
#endif

L
Linus Torvalds 已提交
2482
	/* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2483
	return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
L
Linus Torvalds 已提交
2484 2485
}

2486
static void dummy_stop(struct usb_hcd *hcd)
L
Linus Torvalds 已提交
2487
{
2488 2489
	device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
	dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
L
Linus Torvalds 已提交
2490 2491 2492 2493
}

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

2494
static int dummy_h_get_frame(struct usb_hcd *hcd)
L
Linus Torvalds 已提交
2495
{
2496
	return dummy_g_get_frame(NULL);
L
Linus Torvalds 已提交
2497 2498
}

2499 2500
static int dummy_setup(struct usb_hcd *hcd)
{
2501 2502 2503
	struct dummy *dum;

	dum = *((void **)dev_get_platdata(hcd->self.controller));
2504
	hcd->self.sg_tablesize = ~0;
2505
	if (usb_hcd_is_primary_hcd(hcd)) {
2506 2507
		dum->hs_hcd = hcd_to_dummy_hcd(hcd);
		dum->hs_hcd->dum = dum;
2508 2509 2510 2511 2512
		/*
		 * Mark the first roothub as being USB 2.0.
		 * The USB 3.0 roothub will be registered later by
		 * dummy_hcd_probe()
		 */
2513 2514
		hcd->speed = HCD_USB2;
		hcd->self.root_hub->speed = USB_SPEED_HIGH;
2515
	} else {
2516 2517
		dum->ss_hcd = hcd_to_dummy_hcd(hcd);
		dum->ss_hcd->dum = dum;
2518 2519
		hcd->speed = HCD_USB3;
		hcd->self.root_hub->speed = USB_SPEED_SUPER;
2520 2521 2522 2523
	}
	return 0;
}

2524
/* Change a group of bulk endpoints to support multiple stream IDs */
2525
static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
2526 2527 2528
	struct usb_host_endpoint **eps, unsigned int num_eps,
	unsigned int num_streams, gfp_t mem_flags)
{
2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568
	struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
	unsigned long flags;
	int max_stream;
	int ret_streams = num_streams;
	unsigned int index;
	unsigned int i;

	if (!num_eps)
		return -EINVAL;

	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
	for (i = 0; i < num_eps; i++) {
		index = dummy_get_ep_idx(&eps[i]->desc);
		if ((1 << index) & dum_hcd->stream_en_ep) {
			ret_streams = -EINVAL;
			goto out;
		}
		max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp);
		if (!max_stream) {
			ret_streams = -EINVAL;
			goto out;
		}
		if (max_stream < ret_streams) {
			dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u "
					"stream IDs.\n",
					eps[i]->desc.bEndpointAddress,
					max_stream);
			ret_streams = max_stream;
		}
	}

	for (i = 0; i < num_eps; i++) {
		index = dummy_get_ep_idx(&eps[i]->desc);
		dum_hcd->stream_en_ep |= 1 << index;
		set_max_streams_for_pipe(dum_hcd,
				usb_endpoint_num(&eps[i]->desc), ret_streams);
	}
out:
	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
	return ret_streams;
2569 2570 2571
}

/* Reverts a group of bulk endpoints back to not using stream IDs. */
2572
static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
2573 2574 2575
	struct usb_host_endpoint **eps, unsigned int num_eps,
	gfp_t mem_flags)
{
2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600
	struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
	unsigned long flags;
	int ret;
	unsigned int index;
	unsigned int i;

	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
	for (i = 0; i < num_eps; i++) {
		index = dummy_get_ep_idx(&eps[i]->desc);
		if (!((1 << index) & dum_hcd->stream_en_ep)) {
			ret = -EINVAL;
			goto out;
		}
	}

	for (i = 0; i < num_eps; i++) {
		index = dummy_get_ep_idx(&eps[i]->desc);
		dum_hcd->stream_en_ep &= ~(1 << index);
		set_max_streams_for_pipe(dum_hcd,
				usb_endpoint_num(&eps[i]->desc), 0);
	}
	ret = 0;
out:
	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
	return ret;
2601 2602 2603
}

static struct hc_driver dummy_hcd = {
L
Linus Torvalds 已提交
2604 2605
	.description =		(char *) driver_name,
	.product_desc =		"Dummy host controller",
2606
	.hcd_priv_size =	sizeof(struct dummy_hcd),
L
Linus Torvalds 已提交
2607

2608
	.reset =		dummy_setup,
L
Linus Torvalds 已提交
2609 2610 2611
	.start =		dummy_start,
	.stop =			dummy_stop,

2612 2613
	.urb_enqueue =		dummy_urb_enqueue,
	.urb_dequeue =		dummy_urb_dequeue,
L
Linus Torvalds 已提交
2614

2615
	.get_frame_number =	dummy_h_get_frame,
L
Linus Torvalds 已提交
2616

2617 2618
	.hub_status_data =	dummy_hub_status,
	.hub_control =		dummy_hub_control,
2619 2620
	.bus_suspend =		dummy_bus_suspend,
	.bus_resume =		dummy_bus_resume,
2621 2622 2623

	.alloc_streams =	dummy_alloc_streams,
	.free_streams =		dummy_free_streams,
L
Linus Torvalds 已提交
2624 2625
};

2626
static int dummy_hcd_probe(struct platform_device *pdev)
L
Linus Torvalds 已提交
2627
{
2628
	struct dummy		*dum;
2629
	struct usb_hcd		*hs_hcd;
2630
	struct usb_hcd		*ss_hcd;
L
Linus Torvalds 已提交
2631 2632
	int			retval;

2633
	dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
2634
	dum = *((void **)dev_get_platdata(&pdev->dev));
L
Linus Torvalds 已提交
2635

2636 2637 2638
	if (mod_data.is_super_speed)
		dummy_hcd.flags = HCD_USB3 | HCD_SHARED;
	else if (mod_data.is_high_speed)
2639
		dummy_hcd.flags = HCD_USB2;
2640 2641
	else
		dummy_hcd.flags = HCD_USB11;
2642 2643
	hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
	if (!hs_hcd)
L
Linus Torvalds 已提交
2644
		return -ENOMEM;
2645
	hs_hcd->has_tt = 1;
L
Linus Torvalds 已提交
2646

2647
	retval = usb_add_hcd(hs_hcd, 0, 0);
2648 2649
	if (retval)
		goto put_usb2_hcd;
2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667

	if (mod_data.is_super_speed) {
		ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
					dev_name(&pdev->dev), hs_hcd);
		if (!ss_hcd) {
			retval = -ENOMEM;
			goto dealloc_usb2_hcd;
		}

		retval = usb_add_hcd(ss_hcd, 0, 0);
		if (retval)
			goto put_usb3_hcd;
	}
	return 0;

put_usb3_hcd:
	usb_put_hcd(ss_hcd);
dealloc_usb2_hcd:
2668 2669
	usb_remove_hcd(hs_hcd);
put_usb2_hcd:
2670
	usb_put_hcd(hs_hcd);
2671
	dum->hs_hcd = dum->ss_hcd = NULL;
L
Linus Torvalds 已提交
2672 2673 2674
	return retval;
}

2675
static int dummy_hcd_remove(struct platform_device *pdev)
L
Linus Torvalds 已提交
2676
{
2677 2678
	struct dummy		*dum;

2679
	dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum;
2680 2681 2682 2683 2684 2685

	if (dum->ss_hcd) {
		usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
		usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
	}

2686 2687
	usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
	usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2688

2689 2690
	dum->hs_hcd = NULL;
	dum->ss_hcd = NULL;
L
Linus Torvalds 已提交
2691

2692
	return 0;
L
Linus Torvalds 已提交
2693 2694
}

2695
static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state)
2696 2697
{
	struct usb_hcd		*hcd;
2698
	struct dummy_hcd	*dum_hcd;
2699
	int			rc = 0;
2700

2701
	dev_dbg(&pdev->dev, "%s\n", __func__);
2702

2703
	hcd = platform_get_drvdata(pdev);
2704 2705
	dum_hcd = hcd_to_dummy_hcd(hcd);
	if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
2706 2707 2708 2709 2710
		dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
		rc = -EBUSY;
	} else
		clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
	return rc;
2711 2712
}

2713
static int dummy_hcd_resume(struct platform_device *pdev)
2714 2715 2716
{
	struct usb_hcd		*hcd;

2717
	dev_dbg(&pdev->dev, "%s\n", __func__);
2718

2719
	hcd = platform_get_drvdata(pdev);
2720
	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2721
	usb_hcd_poll_rh_status(hcd);
2722 2723 2724
	return 0;
}

2725
static struct platform_driver dummy_hcd_driver = {
2726 2727
	.probe		= dummy_hcd_probe,
	.remove		= dummy_hcd_remove,
2728 2729
	.suspend	= dummy_hcd_suspend,
	.resume		= dummy_hcd_resume,
2730 2731 2732
	.driver		= {
		.name	= (char *) driver_name,
	},
2733
};
L
Linus Torvalds 已提交
2734

2735
/*-------------------------------------------------------------------------*/
2736
#define MAX_NUM_UDC	2
2737 2738
static struct platform_device *the_udc_pdev[MAX_NUM_UDC];
static struct platform_device *the_hcd_pdev[MAX_NUM_UDC];
L
Linus Torvalds 已提交
2739

2740
static int __init init(void)
L
Linus Torvalds 已提交
2741
{
2742
	int	retval = -ENOMEM;
2743
	int	i;
2744
	struct	dummy *dum[MAX_NUM_UDC];
L
Linus Torvalds 已提交
2745

2746
	if (usb_disabled())
L
Linus Torvalds 已提交
2747
		return -ENODEV;
2748

2749 2750 2751
	if (!mod_data.is_high_speed && mod_data.is_super_speed)
		return -EINVAL;

2752
	if (mod_data.num < 1 || mod_data.num > MAX_NUM_UDC) {
2753
		pr_err("Number of emulated UDC must be in range of 1...%d\n",
2754 2755 2756
				MAX_NUM_UDC);
		return -EINVAL;
	}
2757

2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775
	for (i = 0; i < mod_data.num; i++) {
		the_hcd_pdev[i] = platform_device_alloc(driver_name, i);
		if (!the_hcd_pdev[i]) {
			i--;
			while (i >= 0)
				platform_device_put(the_hcd_pdev[i--]);
			return retval;
		}
	}
	for (i = 0; i < mod_data.num; i++) {
		the_udc_pdev[i] = platform_device_alloc(gadget_name, i);
		if (!the_udc_pdev[i]) {
			i--;
			while (i >= 0)
				platform_device_put(the_udc_pdev[i--]);
			goto err_alloc_udc;
		}
	}
2776 2777
	for (i = 0; i < mod_data.num; i++) {
		dum[i] = kzalloc(sizeof(struct dummy), GFP_KERNEL);
2778 2779
		if (!dum[i]) {
			retval = -ENOMEM;
2780
			goto err_add_pdata;
2781
		}
2782 2783 2784 2785 2786 2787 2788 2789 2790
		retval = platform_device_add_data(the_hcd_pdev[i], &dum[i],
				sizeof(void *));
		if (retval)
			goto err_add_pdata;
		retval = platform_device_add_data(the_udc_pdev[i], &dum[i],
				sizeof(void *));
		if (retval)
			goto err_add_pdata;
	}
2791

2792 2793
	retval = platform_driver_register(&dummy_hcd_driver);
	if (retval < 0)
2794
		goto err_add_pdata;
2795
	retval = platform_driver_register(&dummy_udc_driver);
2796 2797 2798
	if (retval < 0)
		goto err_register_udc_driver;

2799 2800 2801 2802 2803 2804 2805 2806 2807
	for (i = 0; i < mod_data.num; i++) {
		retval = platform_device_add(the_hcd_pdev[i]);
		if (retval < 0) {
			i--;
			while (i >= 0)
				platform_device_del(the_hcd_pdev[i--]);
			goto err_add_hcd;
		}
	}
2808 2809 2810 2811 2812 2813 2814 2815 2816 2817
	for (i = 0; i < mod_data.num; i++) {
		if (!dum[i]->hs_hcd ||
				(!dum[i]->ss_hcd && mod_data.is_super_speed)) {
			/*
			 * The hcd was added successfully but its probe
			 * function failed for some reason.
			 */
			retval = -EINVAL;
			goto err_add_udc;
		}
2818
	}
2819 2820 2821 2822 2823 2824

	for (i = 0; i < mod_data.num; i++) {
		retval = platform_device_add(the_udc_pdev[i]);
		if (retval < 0) {
			i--;
			while (i >= 0)
2825
				platform_device_del(the_udc_pdev[i--]);
2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838
			goto err_add_udc;
		}
	}

	for (i = 0; i < mod_data.num; i++) {
		if (!platform_get_drvdata(the_udc_pdev[i])) {
			/*
			 * The udc was added successfully but its probe
			 * function failed for some reason.
			 */
			retval = -EINVAL;
			goto err_probe_udc;
		}
2839
	}
2840 2841
	return retval;

2842
err_probe_udc:
2843 2844
	for (i = 0; i < mod_data.num; i++)
		platform_device_del(the_udc_pdev[i]);
2845
err_add_udc:
2846 2847
	for (i = 0; i < mod_data.num; i++)
		platform_device_del(the_hcd_pdev[i]);
2848 2849
err_add_hcd:
	platform_driver_unregister(&dummy_udc_driver);
2850
err_register_udc_driver:
2851
	platform_driver_unregister(&dummy_hcd_driver);
2852 2853 2854
err_add_pdata:
	for (i = 0; i < mod_data.num; i++)
		kfree(dum[i]);
2855 2856
	for (i = 0; i < mod_data.num; i++)
		platform_device_put(the_udc_pdev[i]);
2857
err_alloc_udc:
2858 2859
	for (i = 0; i < mod_data.num; i++)
		platform_device_put(the_hcd_pdev[i]);
L
Linus Torvalds 已提交
2860 2861
	return retval;
}
2862
module_init(init);
L
Linus Torvalds 已提交
2863

2864
static void __exit cleanup(void)
L
Linus Torvalds 已提交
2865
{
2866 2867 2868
	int i;

	for (i = 0; i < mod_data.num; i++) {
2869 2870 2871 2872
		struct dummy *dum;

		dum = *((void **)dev_get_platdata(&the_udc_pdev[i]->dev));

2873 2874
		platform_device_unregister(the_udc_pdev[i]);
		platform_device_unregister(the_hcd_pdev[i]);
2875
		kfree(dum);
2876
	}
2877 2878
	platform_driver_unregister(&dummy_udc_driver);
	platform_driver_unregister(&dummy_hcd_driver);
L
Linus Torvalds 已提交
2879
}
2880
module_exit(cleanup);