xref: /openbmc/linux/drivers/usb/gadget/udc/dummy_hcd.c (revision 2cc39179)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * dummy_hcd.c -- Dummy/Loopback USB host and device emulator driver.
4  *
5  * Maintainer: Alan Stern <stern@rowland.harvard.edu>
6  *
7  * Copyright (C) 2003 David Brownell
8  * Copyright (C) 2003-2005 Alan Stern
9  */
10 
11 
12 /*
13  * This exposes a device side "USB gadget" API, driven by requests to a
14  * Linux-USB host controller driver.  USB traffic is simulated; there's
15  * no need for USB hardware.  Use this with two other drivers:
16  *
17  *  - Gadget driver, responding to requests (device);
18  *  - Host-side device driver, as already familiar in Linux.
19  *
20  * Having this all in one kernel can help some stages of development,
21  * bypassing some hardware (and driver) issues.  UML could help too.
22  *
23  * Note: The emulation does not include isochronous transfers!
24  */
25 
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/delay.h>
29 #include <linux/ioport.h>
30 #include <linux/slab.h>
31 #include <linux/errno.h>
32 #include <linux/init.h>
33 #include <linux/timer.h>
34 #include <linux/list.h>
35 #include <linux/interrupt.h>
36 #include <linux/platform_device.h>
37 #include <linux/usb.h>
38 #include <linux/usb/gadget.h>
39 #include <linux/usb/hcd.h>
40 #include <linux/scatterlist.h>
41 
42 #include <asm/byteorder.h>
43 #include <linux/io.h>
44 #include <asm/irq.h>
45 #include <asm/unaligned.h>
46 
47 #define DRIVER_DESC	"USB Host+Gadget Emulator"
48 #define DRIVER_VERSION	"02 May 2005"
49 
50 #define POWER_BUDGET	500	/* in mA; use 8 for low-power port testing */
51 #define POWER_BUDGET_3	900	/* in mA */
52 
53 static const char	driver_name[] = "dummy_hcd";
54 static const char	driver_desc[] = "USB Host+Gadget Emulator";
55 
56 static const char	gadget_name[] = "dummy_udc";
57 
58 MODULE_DESCRIPTION(DRIVER_DESC);
59 MODULE_AUTHOR("David Brownell");
60 MODULE_LICENSE("GPL");
61 
62 struct dummy_hcd_module_parameters {
63 	bool is_super_speed;
64 	bool is_high_speed;
65 	unsigned int num;
66 };
67 
68 static struct dummy_hcd_module_parameters mod_data = {
69 	.is_super_speed = false,
70 	.is_high_speed = true,
71 	.num = 1,
72 };
73 module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
74 MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
75 module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
76 MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
77 module_param_named(num, mod_data.num, uint, S_IRUGO);
78 MODULE_PARM_DESC(num, "number of emulated controllers");
79 /*-------------------------------------------------------------------------*/
80 
81 /* gadget side driver data structres */
82 struct dummy_ep {
83 	struct list_head		queue;
84 	unsigned long			last_io;	/* jiffies timestamp */
85 	struct usb_gadget		*gadget;
86 	const struct usb_endpoint_descriptor *desc;
87 	struct usb_ep			ep;
88 	unsigned			halted:1;
89 	unsigned			wedged:1;
90 	unsigned			already_seen:1;
91 	unsigned			setup_stage:1;
92 	unsigned			stream_en:1;
93 };
94 
95 struct dummy_request {
96 	struct list_head		queue;		/* ep's requests */
97 	struct usb_request		req;
98 };
99 
100 static inline struct dummy_ep *usb_ep_to_dummy_ep(struct usb_ep *_ep)
101 {
102 	return container_of(_ep, struct dummy_ep, ep);
103 }
104 
105 static inline struct dummy_request *usb_request_to_dummy_request
106 		(struct usb_request *_req)
107 {
108 	return container_of(_req, struct dummy_request, req);
109 }
110 
111 /*-------------------------------------------------------------------------*/
112 
113 /*
114  * Every device has ep0 for control requests, plus up to 30 more endpoints,
115  * in one of two types:
116  *
117  *   - Configurable:  direction (in/out), type (bulk, iso, etc), and endpoint
118  *     number can be changed.  Names like "ep-a" are used for this type.
119  *
120  *   - Fixed Function:  in other cases.  some characteristics may be mutable;
121  *     that'd be hardware-specific.  Names like "ep12out-bulk" are used.
122  *
123  * Gadget drivers are responsible for not setting up conflicting endpoint
124  * configurations, illegal or unsupported packet lengths, and so on.
125  */
126 
127 static const char ep0name[] = "ep0";
128 
129 static const struct {
130 	const char *name;
131 	const struct usb_ep_caps caps;
132 } ep_info[] = {
133 #define EP_INFO(_name, _caps) \
134 	{ \
135 		.name = _name, \
136 		.caps = _caps, \
137 	}
138 
139 /* we don't provide isochronous endpoints since we don't support them */
140 #define TYPE_BULK_OR_INT	(USB_EP_CAPS_TYPE_BULK | USB_EP_CAPS_TYPE_INT)
141 
142 	/* everyone has ep0 */
143 	EP_INFO(ep0name,
144 		USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_ALL)),
145 	/* act like a pxa250: fifteen fixed function endpoints */
146 	EP_INFO("ep1in-bulk",
147 		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
148 	EP_INFO("ep2out-bulk",
149 		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
150 /*
151 	EP_INFO("ep3in-iso",
152 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_IN)),
153 	EP_INFO("ep4out-iso",
154 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_OUT)),
155 */
156 	EP_INFO("ep5in-int",
157 		USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
158 	EP_INFO("ep6in-bulk",
159 		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
160 	EP_INFO("ep7out-bulk",
161 		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
162 /*
163 	EP_INFO("ep8in-iso",
164 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_IN)),
165 	EP_INFO("ep9out-iso",
166 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_OUT)),
167 */
168 	EP_INFO("ep10in-int",
169 		USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
170 	EP_INFO("ep11in-bulk",
171 		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
172 	EP_INFO("ep12out-bulk",
173 		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
174 /*
175 	EP_INFO("ep13in-iso",
176 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_IN)),
177 	EP_INFO("ep14out-iso",
178 		USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_OUT)),
179 */
180 	EP_INFO("ep15in-int",
181 		USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
182 
183 	/* or like sa1100: two fixed function endpoints */
184 	EP_INFO("ep1out-bulk",
185 		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
186 	EP_INFO("ep2in-bulk",
187 		USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
188 
189 	/* and now some generic EPs so we have enough in multi config */
190 	EP_INFO("ep-aout",
191 		USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)),
192 	EP_INFO("ep-bin",
193 		USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_IN)),
194 	EP_INFO("ep-cout",
195 		USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)),
196 	EP_INFO("ep-dout",
197 		USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)),
198 	EP_INFO("ep-ein",
199 		USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_IN)),
200 	EP_INFO("ep-fout",
201 		USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)),
202 	EP_INFO("ep-gin",
203 		USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_IN)),
204 	EP_INFO("ep-hout",
205 		USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)),
206 	EP_INFO("ep-iout",
207 		USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)),
208 	EP_INFO("ep-jin",
209 		USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_IN)),
210 	EP_INFO("ep-kout",
211 		USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)),
212 	EP_INFO("ep-lin",
213 		USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_IN)),
214 	EP_INFO("ep-mout",
215 		USB_EP_CAPS(TYPE_BULK_OR_INT, USB_EP_CAPS_DIR_OUT)),
216 
217 #undef EP_INFO
218 };
219 
220 #define DUMMY_ENDPOINTS	ARRAY_SIZE(ep_info)
221 
222 /*-------------------------------------------------------------------------*/
223 
224 #define FIFO_SIZE		64
225 
226 struct urbp {
227 	struct urb		*urb;
228 	struct list_head	urbp_list;
229 	struct sg_mapping_iter	miter;
230 	u32			miter_started;
231 };
232 
233 
234 enum dummy_rh_state {
235 	DUMMY_RH_RESET,
236 	DUMMY_RH_SUSPENDED,
237 	DUMMY_RH_RUNNING
238 };
239 
240 struct dummy_hcd {
241 	struct dummy			*dum;
242 	enum dummy_rh_state		rh_state;
243 	struct timer_list		timer;
244 	u32				port_status;
245 	u32				old_status;
246 	unsigned long			re_timeout;
247 
248 	struct usb_device		*udev;
249 	struct list_head		urbp_list;
250 	struct urbp			*next_frame_urbp;
251 
252 	u32				stream_en_ep;
253 	u8				num_stream[30 / 2];
254 
255 	unsigned			active:1;
256 	unsigned			old_active:1;
257 	unsigned			resuming:1;
258 };
259 
260 struct dummy {
261 	spinlock_t			lock;
262 
263 	/*
264 	 * DEVICE/GADGET side support
265 	 */
266 	struct dummy_ep			ep[DUMMY_ENDPOINTS];
267 	int				address;
268 	int				callback_usage;
269 	struct usb_gadget		gadget;
270 	struct usb_gadget_driver	*driver;
271 	struct dummy_request		fifo_req;
272 	u8				fifo_buf[FIFO_SIZE];
273 	u16				devstatus;
274 	unsigned			ints_enabled:1;
275 	unsigned			udc_suspended:1;
276 	unsigned			pullup:1;
277 
278 	/*
279 	 * HOST side support
280 	 */
281 	struct dummy_hcd		*hs_hcd;
282 	struct dummy_hcd		*ss_hcd;
283 };
284 
285 static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
286 {
287 	return (struct dummy_hcd *) (hcd->hcd_priv);
288 }
289 
290 static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
291 {
292 	return container_of((void *) dum, struct usb_hcd, hcd_priv);
293 }
294 
295 static inline struct device *dummy_dev(struct dummy_hcd *dum)
296 {
297 	return dummy_hcd_to_hcd(dum)->self.controller;
298 }
299 
300 static inline struct device *udc_dev(struct dummy *dum)
301 {
302 	return dum->gadget.dev.parent;
303 }
304 
305 static inline struct dummy *ep_to_dummy(struct dummy_ep *ep)
306 {
307 	return container_of(ep->gadget, struct dummy, gadget);
308 }
309 
310 static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
311 {
312 	struct dummy *dum = container_of(gadget, struct dummy, gadget);
313 	if (dum->gadget.speed == USB_SPEED_SUPER)
314 		return dum->ss_hcd;
315 	else
316 		return dum->hs_hcd;
317 }
318 
319 static inline struct dummy *gadget_dev_to_dummy(struct device *dev)
320 {
321 	return container_of(dev, struct dummy, gadget.dev);
322 }
323 
324 /*-------------------------------------------------------------------------*/
325 
326 /* DEVICE/GADGET SIDE UTILITY ROUTINES */
327 
328 /* called with spinlock held */
329 static void nuke(struct dummy *dum, struct dummy_ep *ep)
330 {
331 	while (!list_empty(&ep->queue)) {
332 		struct dummy_request	*req;
333 
334 		req = list_entry(ep->queue.next, struct dummy_request, queue);
335 		list_del_init(&req->queue);
336 		req->req.status = -ESHUTDOWN;
337 
338 		spin_unlock(&dum->lock);
339 		usb_gadget_giveback_request(&ep->ep, &req->req);
340 		spin_lock(&dum->lock);
341 	}
342 }
343 
344 /* caller must hold lock */
345 static void stop_activity(struct dummy *dum)
346 {
347 	int i;
348 
349 	/* prevent any more requests */
350 	dum->address = 0;
351 
352 	/* The timer is left running so that outstanding URBs can fail */
353 
354 	/* nuke any pending requests first, so driver i/o is quiesced */
355 	for (i = 0; i < DUMMY_ENDPOINTS; ++i)
356 		nuke(dum, &dum->ep[i]);
357 
358 	/* driver now does any non-usb quiescing necessary */
359 }
360 
361 /**
362  * set_link_state_by_speed() - Sets the current state of the link according to
363  *	the hcd speed
364  * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
365  *
366  * This function updates the port_status according to the link state and the
367  * speed of the hcd.
368  */
369 static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
370 {
371 	struct dummy *dum = dum_hcd->dum;
372 
373 	if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
374 		if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
375 			dum_hcd->port_status = 0;
376 		} else if (!dum->pullup || dum->udc_suspended) {
377 			/* UDC suspend must cause a disconnect */
378 			dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
379 						USB_PORT_STAT_ENABLE);
380 			if ((dum_hcd->old_status &
381 			     USB_PORT_STAT_CONNECTION) != 0)
382 				dum_hcd->port_status |=
383 					(USB_PORT_STAT_C_CONNECTION << 16);
384 		} else {
385 			/* device is connected and not suspended */
386 			dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
387 						 USB_PORT_STAT_SPEED_5GBPS) ;
388 			if ((dum_hcd->old_status &
389 			     USB_PORT_STAT_CONNECTION) == 0)
390 				dum_hcd->port_status |=
391 					(USB_PORT_STAT_C_CONNECTION << 16);
392 			if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) &&
393 			    (dum_hcd->port_status &
394 			     USB_PORT_STAT_LINK_STATE) == USB_SS_PORT_LS_U0 &&
395 			    dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
396 				dum_hcd->active = 1;
397 		}
398 	} else {
399 		if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
400 			dum_hcd->port_status = 0;
401 		} else if (!dum->pullup || dum->udc_suspended) {
402 			/* UDC suspend must cause a disconnect */
403 			dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
404 						USB_PORT_STAT_ENABLE |
405 						USB_PORT_STAT_LOW_SPEED |
406 						USB_PORT_STAT_HIGH_SPEED |
407 						USB_PORT_STAT_SUSPEND);
408 			if ((dum_hcd->old_status &
409 			     USB_PORT_STAT_CONNECTION) != 0)
410 				dum_hcd->port_status |=
411 					(USB_PORT_STAT_C_CONNECTION << 16);
412 		} else {
413 			dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
414 			if ((dum_hcd->old_status &
415 			     USB_PORT_STAT_CONNECTION) == 0)
416 				dum_hcd->port_status |=
417 					(USB_PORT_STAT_C_CONNECTION << 16);
418 			if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
419 				dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
420 			else if ((dum_hcd->port_status &
421 				  USB_PORT_STAT_SUSPEND) == 0 &&
422 					dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
423 				dum_hcd->active = 1;
424 		}
425 	}
426 }
427 
428 /* caller must hold lock */
429 static void set_link_state(struct dummy_hcd *dum_hcd)
430 	__must_hold(&dum->lock)
431 {
432 	struct dummy *dum = dum_hcd->dum;
433 	unsigned int power_bit;
434 
435 	dum_hcd->active = 0;
436 	if (dum->pullup)
437 		if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
438 		     dum->gadget.speed != USB_SPEED_SUPER) ||
439 		    (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
440 		     dum->gadget.speed == USB_SPEED_SUPER))
441 			return;
442 
443 	set_link_state_by_speed(dum_hcd);
444 	power_bit = (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 ?
445 			USB_SS_PORT_STAT_POWER : USB_PORT_STAT_POWER);
446 
447 	if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
448 	     dum_hcd->active)
449 		dum_hcd->resuming = 0;
450 
451 	/* Currently !connected or in reset */
452 	if ((dum_hcd->port_status & power_bit) == 0 ||
453 			(dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
454 		unsigned int disconnect = power_bit &
455 				dum_hcd->old_status & (~dum_hcd->port_status);
456 		unsigned int reset = USB_PORT_STAT_RESET &
457 				(~dum_hcd->old_status) & dum_hcd->port_status;
458 
459 		/* Report reset and disconnect events to the driver */
460 		if (dum->ints_enabled && (disconnect || reset)) {
461 			stop_activity(dum);
462 			++dum->callback_usage;
463 			spin_unlock(&dum->lock);
464 			if (reset)
465 				usb_gadget_udc_reset(&dum->gadget, dum->driver);
466 			else
467 				dum->driver->disconnect(&dum->gadget);
468 			spin_lock(&dum->lock);
469 			--dum->callback_usage;
470 		}
471 	} else if (dum_hcd->active != dum_hcd->old_active &&
472 			dum->ints_enabled) {
473 		++dum->callback_usage;
474 		spin_unlock(&dum->lock);
475 		if (dum_hcd->old_active && dum->driver->suspend)
476 			dum->driver->suspend(&dum->gadget);
477 		else if (!dum_hcd->old_active &&  dum->driver->resume)
478 			dum->driver->resume(&dum->gadget);
479 		spin_lock(&dum->lock);
480 		--dum->callback_usage;
481 	}
482 
483 	dum_hcd->old_status = dum_hcd->port_status;
484 	dum_hcd->old_active = dum_hcd->active;
485 }
486 
487 /*-------------------------------------------------------------------------*/
488 
489 /* DEVICE/GADGET SIDE DRIVER
490  *
491  * This only tracks gadget state.  All the work is done when the host
492  * side tries some (emulated) i/o operation.  Real device controller
493  * drivers would do real i/o using dma, fifos, irqs, timers, etc.
494  */
495 
496 #define is_enabled(dum) \
497 	(dum->port_status & USB_PORT_STAT_ENABLE)
498 
499 static int dummy_enable(struct usb_ep *_ep,
500 		const struct usb_endpoint_descriptor *desc)
501 {
502 	struct dummy		*dum;
503 	struct dummy_hcd	*dum_hcd;
504 	struct dummy_ep		*ep;
505 	unsigned		max;
506 	int			retval;
507 
508 	ep = usb_ep_to_dummy_ep(_ep);
509 	if (!_ep || !desc || ep->desc || _ep->name == ep0name
510 			|| desc->bDescriptorType != USB_DT_ENDPOINT)
511 		return -EINVAL;
512 	dum = ep_to_dummy(ep);
513 	if (!dum->driver)
514 		return -ESHUTDOWN;
515 
516 	dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
517 	if (!is_enabled(dum_hcd))
518 		return -ESHUTDOWN;
519 
520 	/*
521 	 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
522 	 * maximum packet size.
523 	 * For SS devices the wMaxPacketSize is limited by 1024.
524 	 */
525 	max = usb_endpoint_maxp(desc);
526 
527 	/* drivers must not request bad settings, since lower levels
528 	 * (hardware or its drivers) may not check.  some endpoints
529 	 * can't do iso, many have maxpacket limitations, etc.
530 	 *
531 	 * since this "hardware" driver is here to help debugging, we
532 	 * have some extra sanity checks.  (there could be more though,
533 	 * especially for "ep9out" style fixed function ones.)
534 	 */
535 	retval = -EINVAL;
536 	switch (usb_endpoint_type(desc)) {
537 	case USB_ENDPOINT_XFER_BULK:
538 		if (strstr(ep->ep.name, "-iso")
539 				|| strstr(ep->ep.name, "-int")) {
540 			goto done;
541 		}
542 		switch (dum->gadget.speed) {
543 		case USB_SPEED_SUPER:
544 			if (max == 1024)
545 				break;
546 			goto done;
547 		case USB_SPEED_HIGH:
548 			if (max == 512)
549 				break;
550 			goto done;
551 		case USB_SPEED_FULL:
552 			if (max == 8 || max == 16 || max == 32 || max == 64)
553 				/* we'll fake any legal size */
554 				break;
555 			/* save a return statement */
556 			fallthrough;
557 		default:
558 			goto done;
559 		}
560 		break;
561 	case USB_ENDPOINT_XFER_INT:
562 		if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
563 			goto done;
564 		/* real hardware might not handle all packet sizes */
565 		switch (dum->gadget.speed) {
566 		case USB_SPEED_SUPER:
567 		case USB_SPEED_HIGH:
568 			if (max <= 1024)
569 				break;
570 			/* save a return statement */
571 			fallthrough;
572 		case USB_SPEED_FULL:
573 			if (max <= 64)
574 				break;
575 			/* save a return statement */
576 			fallthrough;
577 		default:
578 			if (max <= 8)
579 				break;
580 			goto done;
581 		}
582 		break;
583 	case USB_ENDPOINT_XFER_ISOC:
584 		if (strstr(ep->ep.name, "-bulk")
585 				|| strstr(ep->ep.name, "-int"))
586 			goto done;
587 		/* real hardware might not handle all packet sizes */
588 		switch (dum->gadget.speed) {
589 		case USB_SPEED_SUPER:
590 		case USB_SPEED_HIGH:
591 			if (max <= 1024)
592 				break;
593 			/* save a return statement */
594 			fallthrough;
595 		case USB_SPEED_FULL:
596 			if (max <= 1023)
597 				break;
598 			/* save a return statement */
599 			fallthrough;
600 		default:
601 			goto done;
602 		}
603 		break;
604 	default:
605 		/* few chips support control except on ep0 */
606 		goto done;
607 	}
608 
609 	_ep->maxpacket = max;
610 	if (usb_ss_max_streams(_ep->comp_desc)) {
611 		if (!usb_endpoint_xfer_bulk(desc)) {
612 			dev_err(udc_dev(dum), "Can't enable stream support on "
613 					"non-bulk ep %s\n", _ep->name);
614 			return -EINVAL;
615 		}
616 		ep->stream_en = 1;
617 	}
618 	ep->desc = desc;
619 
620 	dev_dbg(udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n",
621 		_ep->name,
622 		desc->bEndpointAddress & 0x0f,
623 		(desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
624 		usb_ep_type_string(usb_endpoint_type(desc)),
625 		max, ep->stream_en ? "enabled" : "disabled");
626 
627 	/* at this point real hardware should be NAKing transfers
628 	 * to that endpoint, until a buffer is queued to it.
629 	 */
630 	ep->halted = ep->wedged = 0;
631 	retval = 0;
632 done:
633 	return retval;
634 }
635 
636 static int dummy_disable(struct usb_ep *_ep)
637 {
638 	struct dummy_ep		*ep;
639 	struct dummy		*dum;
640 	unsigned long		flags;
641 
642 	ep = usb_ep_to_dummy_ep(_ep);
643 	if (!_ep || !ep->desc || _ep->name == ep0name)
644 		return -EINVAL;
645 	dum = ep_to_dummy(ep);
646 
647 	spin_lock_irqsave(&dum->lock, flags);
648 	ep->desc = NULL;
649 	ep->stream_en = 0;
650 	nuke(dum, ep);
651 	spin_unlock_irqrestore(&dum->lock, flags);
652 
653 	dev_dbg(udc_dev(dum), "disabled %s\n", _ep->name);
654 	return 0;
655 }
656 
657 static struct usb_request *dummy_alloc_request(struct usb_ep *_ep,
658 		gfp_t mem_flags)
659 {
660 	struct dummy_request	*req;
661 
662 	if (!_ep)
663 		return NULL;
664 
665 	req = kzalloc(sizeof(*req), mem_flags);
666 	if (!req)
667 		return NULL;
668 	INIT_LIST_HEAD(&req->queue);
669 	return &req->req;
670 }
671 
672 static void dummy_free_request(struct usb_ep *_ep, struct usb_request *_req)
673 {
674 	struct dummy_request	*req;
675 
676 	if (!_ep || !_req) {
677 		WARN_ON(1);
678 		return;
679 	}
680 
681 	req = usb_request_to_dummy_request(_req);
682 	WARN_ON(!list_empty(&req->queue));
683 	kfree(req);
684 }
685 
686 static void fifo_complete(struct usb_ep *ep, struct usb_request *req)
687 {
688 }
689 
690 static int dummy_queue(struct usb_ep *_ep, struct usb_request *_req,
691 		gfp_t mem_flags)
692 {
693 	struct dummy_ep		*ep;
694 	struct dummy_request	*req;
695 	struct dummy		*dum;
696 	struct dummy_hcd	*dum_hcd;
697 	unsigned long		flags;
698 
699 	req = usb_request_to_dummy_request(_req);
700 	if (!_req || !list_empty(&req->queue) || !_req->complete)
701 		return -EINVAL;
702 
703 	ep = usb_ep_to_dummy_ep(_ep);
704 	if (!_ep || (!ep->desc && _ep->name != ep0name))
705 		return -EINVAL;
706 
707 	dum = ep_to_dummy(ep);
708 	dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
709 	if (!dum->driver || !is_enabled(dum_hcd))
710 		return -ESHUTDOWN;
711 
712 #if 0
713 	dev_dbg(udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
714 			ep, _req, _ep->name, _req->length, _req->buf);
715 #endif
716 	_req->status = -EINPROGRESS;
717 	_req->actual = 0;
718 	spin_lock_irqsave(&dum->lock, flags);
719 
720 	/* implement an emulated single-request FIFO */
721 	if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
722 			list_empty(&dum->fifo_req.queue) &&
723 			list_empty(&ep->queue) &&
724 			_req->length <= FIFO_SIZE) {
725 		req = &dum->fifo_req;
726 		req->req = *_req;
727 		req->req.buf = dum->fifo_buf;
728 		memcpy(dum->fifo_buf, _req->buf, _req->length);
729 		req->req.context = dum;
730 		req->req.complete = fifo_complete;
731 
732 		list_add_tail(&req->queue, &ep->queue);
733 		spin_unlock(&dum->lock);
734 		_req->actual = _req->length;
735 		_req->status = 0;
736 		usb_gadget_giveback_request(_ep, _req);
737 		spin_lock(&dum->lock);
738 	}  else
739 		list_add_tail(&req->queue, &ep->queue);
740 	spin_unlock_irqrestore(&dum->lock, flags);
741 
742 	/* real hardware would likely enable transfers here, in case
743 	 * it'd been left NAKing.
744 	 */
745 	return 0;
746 }
747 
748 static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req)
749 {
750 	struct dummy_ep		*ep;
751 	struct dummy		*dum;
752 	int			retval = -EINVAL;
753 	unsigned long		flags;
754 	struct dummy_request	*req = NULL, *iter;
755 
756 	if (!_ep || !_req)
757 		return retval;
758 	ep = usb_ep_to_dummy_ep(_ep);
759 	dum = ep_to_dummy(ep);
760 
761 	if (!dum->driver)
762 		return -ESHUTDOWN;
763 
764 	local_irq_save(flags);
765 	spin_lock(&dum->lock);
766 	list_for_each_entry(iter, &ep->queue, queue) {
767 		if (&iter->req != _req)
768 			continue;
769 		list_del_init(&iter->queue);
770 		_req->status = -ECONNRESET;
771 		req = iter;
772 		retval = 0;
773 		break;
774 	}
775 	spin_unlock(&dum->lock);
776 
777 	if (retval == 0) {
778 		dev_dbg(udc_dev(dum),
779 				"dequeued req %p from %s, len %d buf %p\n",
780 				req, _ep->name, _req->length, _req->buf);
781 		usb_gadget_giveback_request(_ep, _req);
782 	}
783 	local_irq_restore(flags);
784 	return retval;
785 }
786 
787 static int
788 dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
789 {
790 	struct dummy_ep		*ep;
791 	struct dummy		*dum;
792 
793 	if (!_ep)
794 		return -EINVAL;
795 	ep = usb_ep_to_dummy_ep(_ep);
796 	dum = ep_to_dummy(ep);
797 	if (!dum->driver)
798 		return -ESHUTDOWN;
799 	if (!value)
800 		ep->halted = ep->wedged = 0;
801 	else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
802 			!list_empty(&ep->queue))
803 		return -EAGAIN;
804 	else {
805 		ep->halted = 1;
806 		if (wedged)
807 			ep->wedged = 1;
808 	}
809 	/* FIXME clear emulated data toggle too */
810 	return 0;
811 }
812 
813 static int
814 dummy_set_halt(struct usb_ep *_ep, int value)
815 {
816 	return dummy_set_halt_and_wedge(_ep, value, 0);
817 }
818 
819 static int dummy_set_wedge(struct usb_ep *_ep)
820 {
821 	if (!_ep || _ep->name == ep0name)
822 		return -EINVAL;
823 	return dummy_set_halt_and_wedge(_ep, 1, 1);
824 }
825 
826 static const struct usb_ep_ops dummy_ep_ops = {
827 	.enable		= dummy_enable,
828 	.disable	= dummy_disable,
829 
830 	.alloc_request	= dummy_alloc_request,
831 	.free_request	= dummy_free_request,
832 
833 	.queue		= dummy_queue,
834 	.dequeue	= dummy_dequeue,
835 
836 	.set_halt	= dummy_set_halt,
837 	.set_wedge	= dummy_set_wedge,
838 };
839 
840 /*-------------------------------------------------------------------------*/
841 
842 /* there are both host and device side versions of this call ... */
843 static int dummy_g_get_frame(struct usb_gadget *_gadget)
844 {
845 	struct timespec64 ts64;
846 
847 	ktime_get_ts64(&ts64);
848 	return ts64.tv_nsec / NSEC_PER_MSEC;
849 }
850 
851 static int dummy_wakeup(struct usb_gadget *_gadget)
852 {
853 	struct dummy_hcd *dum_hcd;
854 
855 	dum_hcd = gadget_to_dummy_hcd(_gadget);
856 	if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
857 				| (1 << USB_DEVICE_REMOTE_WAKEUP))))
858 		return -EINVAL;
859 	if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
860 		return -ENOLINK;
861 	if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
862 			 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
863 		return -EIO;
864 
865 	/* FIXME: What if the root hub is suspended but the port isn't? */
866 
867 	/* hub notices our request, issues downstream resume, etc */
868 	dum_hcd->resuming = 1;
869 	dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
870 	mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
871 	return 0;
872 }
873 
874 static int dummy_set_selfpowered(struct usb_gadget *_gadget, int value)
875 {
876 	struct dummy	*dum;
877 
878 	_gadget->is_selfpowered = (value != 0);
879 	dum = gadget_to_dummy_hcd(_gadget)->dum;
880 	if (value)
881 		dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
882 	else
883 		dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
884 	return 0;
885 }
886 
887 static void dummy_udc_update_ep0(struct dummy *dum)
888 {
889 	if (dum->gadget.speed == USB_SPEED_SUPER)
890 		dum->ep[0].ep.maxpacket = 9;
891 	else
892 		dum->ep[0].ep.maxpacket = 64;
893 }
894 
895 static int dummy_pullup(struct usb_gadget *_gadget, int value)
896 {
897 	struct dummy_hcd *dum_hcd;
898 	struct dummy	*dum;
899 	unsigned long	flags;
900 
901 	dum = gadget_dev_to_dummy(&_gadget->dev);
902 	dum_hcd = gadget_to_dummy_hcd(_gadget);
903 
904 	spin_lock_irqsave(&dum->lock, flags);
905 	dum->pullup = (value != 0);
906 	set_link_state(dum_hcd);
907 	if (value == 0) {
908 		/*
909 		 * Emulate synchronize_irq(): wait for callbacks to finish.
910 		 * This seems to be the best place to emulate the call to
911 		 * synchronize_irq() that's in usb_gadget_remove_driver().
912 		 * Doing it in dummy_udc_stop() would be too late since it
913 		 * is called after the unbind callback and unbind shouldn't
914 		 * be invoked until all the other callbacks are finished.
915 		 */
916 		while (dum->callback_usage > 0) {
917 			spin_unlock_irqrestore(&dum->lock, flags);
918 			usleep_range(1000, 2000);
919 			spin_lock_irqsave(&dum->lock, flags);
920 		}
921 	}
922 	spin_unlock_irqrestore(&dum->lock, flags);
923 
924 	usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
925 	return 0;
926 }
927 
928 static void dummy_udc_set_speed(struct usb_gadget *_gadget,
929 		enum usb_device_speed speed)
930 {
931 	struct dummy	*dum;
932 
933 	dum = gadget_dev_to_dummy(&_gadget->dev);
934 	dum->gadget.speed = speed;
935 	dummy_udc_update_ep0(dum);
936 }
937 
938 static void dummy_udc_async_callbacks(struct usb_gadget *_gadget, bool enable)
939 {
940 	struct dummy	*dum = gadget_dev_to_dummy(&_gadget->dev);
941 
942 	spin_lock_irq(&dum->lock);
943 	dum->ints_enabled = enable;
944 	spin_unlock_irq(&dum->lock);
945 }
946 
947 static int dummy_udc_start(struct usb_gadget *g,
948 		struct usb_gadget_driver *driver);
949 static int dummy_udc_stop(struct usb_gadget *g);
950 
951 static const struct usb_gadget_ops dummy_ops = {
952 	.get_frame	= dummy_g_get_frame,
953 	.wakeup		= dummy_wakeup,
954 	.set_selfpowered = dummy_set_selfpowered,
955 	.pullup		= dummy_pullup,
956 	.udc_start	= dummy_udc_start,
957 	.udc_stop	= dummy_udc_stop,
958 	.udc_set_speed	= dummy_udc_set_speed,
959 	.udc_async_callbacks = dummy_udc_async_callbacks,
960 };
961 
962 /*-------------------------------------------------------------------------*/
963 
964 /* "function" sysfs attribute */
965 static ssize_t function_show(struct device *dev, struct device_attribute *attr,
966 		char *buf)
967 {
968 	struct dummy	*dum = gadget_dev_to_dummy(dev);
969 
970 	if (!dum->driver || !dum->driver->function)
971 		return 0;
972 	return scnprintf(buf, PAGE_SIZE, "%s\n", dum->driver->function);
973 }
974 static DEVICE_ATTR_RO(function);
975 
976 /*-------------------------------------------------------------------------*/
977 
978 /*
979  * Driver registration/unregistration.
980  *
981  * This is basically hardware-specific; there's usually only one real USB
982  * device (not host) controller since that's how USB devices are intended
983  * to work.  So most implementations of these api calls will rely on the
984  * fact that only one driver will ever bind to the hardware.  But curious
985  * hardware can be built with discrete components, so the gadget API doesn't
986  * require that assumption.
987  *
988  * For this emulator, it might be convenient to create a usb device
989  * for each driver that registers:  just add to a big root hub.
990  */
991 
992 static int dummy_udc_start(struct usb_gadget *g,
993 		struct usb_gadget_driver *driver)
994 {
995 	struct dummy_hcd	*dum_hcd = gadget_to_dummy_hcd(g);
996 	struct dummy		*dum = dum_hcd->dum;
997 
998 	switch (g->speed) {
999 	/* All the speeds we support */
1000 	case USB_SPEED_LOW:
1001 	case USB_SPEED_FULL:
1002 	case USB_SPEED_HIGH:
1003 	case USB_SPEED_SUPER:
1004 		break;
1005 	default:
1006 		dev_err(dummy_dev(dum_hcd), "Unsupported driver max speed %d\n",
1007 				driver->max_speed);
1008 		return -EINVAL;
1009 	}
1010 
1011 	/*
1012 	 * DEVICE side init ... the layer above hardware, which
1013 	 * can't enumerate without help from the driver we're binding.
1014 	 */
1015 
1016 	spin_lock_irq(&dum->lock);
1017 	dum->devstatus = 0;
1018 	dum->driver = driver;
1019 	spin_unlock_irq(&dum->lock);
1020 
1021 	return 0;
1022 }
1023 
1024 static int dummy_udc_stop(struct usb_gadget *g)
1025 {
1026 	struct dummy_hcd	*dum_hcd = gadget_to_dummy_hcd(g);
1027 	struct dummy		*dum = dum_hcd->dum;
1028 
1029 	spin_lock_irq(&dum->lock);
1030 	dum->ints_enabled = 0;
1031 	stop_activity(dum);
1032 	dum->driver = NULL;
1033 	spin_unlock_irq(&dum->lock);
1034 
1035 	return 0;
1036 }
1037 
1038 #undef is_enabled
1039 
1040 /* The gadget structure is stored inside the hcd structure and will be
1041  * released along with it. */
1042 static void init_dummy_udc_hw(struct dummy *dum)
1043 {
1044 	int i;
1045 
1046 	INIT_LIST_HEAD(&dum->gadget.ep_list);
1047 	for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1048 		struct dummy_ep	*ep = &dum->ep[i];
1049 
1050 		if (!ep_info[i].name)
1051 			break;
1052 		ep->ep.name = ep_info[i].name;
1053 		ep->ep.caps = ep_info[i].caps;
1054 		ep->ep.ops = &dummy_ep_ops;
1055 		list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
1056 		ep->halted = ep->wedged = ep->already_seen =
1057 				ep->setup_stage = 0;
1058 		usb_ep_set_maxpacket_limit(&ep->ep, ~0);
1059 		ep->ep.max_streams = 16;
1060 		ep->last_io = jiffies;
1061 		ep->gadget = &dum->gadget;
1062 		ep->desc = NULL;
1063 		INIT_LIST_HEAD(&ep->queue);
1064 	}
1065 
1066 	dum->gadget.ep0 = &dum->ep[0].ep;
1067 	list_del_init(&dum->ep[0].ep.ep_list);
1068 	INIT_LIST_HEAD(&dum->fifo_req.queue);
1069 
1070 #ifdef CONFIG_USB_OTG
1071 	dum->gadget.is_otg = 1;
1072 #endif
1073 }
1074 
1075 static int dummy_udc_probe(struct platform_device *pdev)
1076 {
1077 	struct dummy	*dum;
1078 	int		rc;
1079 
1080 	dum = *((void **)dev_get_platdata(&pdev->dev));
1081 	/* Clear usb_gadget region for new registration to udc-core */
1082 	memzero_explicit(&dum->gadget, sizeof(struct usb_gadget));
1083 	dum->gadget.name = gadget_name;
1084 	dum->gadget.ops = &dummy_ops;
1085 	if (mod_data.is_super_speed)
1086 		dum->gadget.max_speed = USB_SPEED_SUPER;
1087 	else if (mod_data.is_high_speed)
1088 		dum->gadget.max_speed = USB_SPEED_HIGH;
1089 	else
1090 		dum->gadget.max_speed = USB_SPEED_FULL;
1091 
1092 	dum->gadget.dev.parent = &pdev->dev;
1093 	init_dummy_udc_hw(dum);
1094 
1095 	rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
1096 	if (rc < 0)
1097 		goto err_udc;
1098 
1099 	rc = device_create_file(&dum->gadget.dev, &dev_attr_function);
1100 	if (rc < 0)
1101 		goto err_dev;
1102 	platform_set_drvdata(pdev, dum);
1103 	return rc;
1104 
1105 err_dev:
1106 	usb_del_gadget_udc(&dum->gadget);
1107 err_udc:
1108 	return rc;
1109 }
1110 
1111 static int dummy_udc_remove(struct platform_device *pdev)
1112 {
1113 	struct dummy	*dum = platform_get_drvdata(pdev);
1114 
1115 	device_remove_file(&dum->gadget.dev, &dev_attr_function);
1116 	usb_del_gadget_udc(&dum->gadget);
1117 	return 0;
1118 }
1119 
1120 static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1121 		int suspend)
1122 {
1123 	spin_lock_irq(&dum->lock);
1124 	dum->udc_suspended = suspend;
1125 	set_link_state(dum_hcd);
1126 	spin_unlock_irq(&dum->lock);
1127 }
1128 
1129 static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1130 {
1131 	struct dummy		*dum = platform_get_drvdata(pdev);
1132 	struct dummy_hcd	*dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1133 
1134 	dev_dbg(&pdev->dev, "%s\n", __func__);
1135 	dummy_udc_pm(dum, dum_hcd, 1);
1136 	usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
1137 	return 0;
1138 }
1139 
1140 static int dummy_udc_resume(struct platform_device *pdev)
1141 {
1142 	struct dummy		*dum = platform_get_drvdata(pdev);
1143 	struct dummy_hcd	*dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1144 
1145 	dev_dbg(&pdev->dev, "%s\n", __func__);
1146 	dummy_udc_pm(dum, dum_hcd, 0);
1147 	usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
1148 	return 0;
1149 }
1150 
1151 static struct platform_driver dummy_udc_driver = {
1152 	.probe		= dummy_udc_probe,
1153 	.remove		= dummy_udc_remove,
1154 	.suspend	= dummy_udc_suspend,
1155 	.resume		= dummy_udc_resume,
1156 	.driver		= {
1157 		.name	= gadget_name,
1158 	},
1159 };
1160 
1161 /*-------------------------------------------------------------------------*/
1162 
1163 static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc)
1164 {
1165 	unsigned int index;
1166 
1167 	index = usb_endpoint_num(desc) << 1;
1168 	if (usb_endpoint_dir_in(desc))
1169 		index |= 1;
1170 	return index;
1171 }
1172 
1173 /* HOST SIDE DRIVER
1174  *
1175  * this uses the hcd framework to hook up to host side drivers.
1176  * its root hub will only have one device, otherwise it acts like
1177  * a normal host controller.
1178  *
1179  * when urbs are queued, they're just stuck on a list that we
1180  * scan in a timer callback.  that callback connects writes from
1181  * the host with reads from the device, and so on, based on the
1182  * usb 2.0 rules.
1183  */
1184 
1185 static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb)
1186 {
1187 	const struct usb_endpoint_descriptor *desc = &urb->ep->desc;
1188 	u32 index;
1189 
1190 	if (!usb_endpoint_xfer_bulk(desc))
1191 		return 0;
1192 
1193 	index = dummy_get_ep_idx(desc);
1194 	return (1 << index) & dum_hcd->stream_en_ep;
1195 }
1196 
1197 /*
1198  * The max stream number is saved as a nibble so for the 30 possible endpoints
1199  * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
1200  * means we use only 1 stream). The maximum according to the spec is 16bit so
1201  * if the 16 stream limit is about to go, the array size should be incremented
1202  * to 30 elements of type u16.
1203  */
1204 static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1205 		unsigned int pipe)
1206 {
1207 	int max_streams;
1208 
1209 	max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1210 	if (usb_pipeout(pipe))
1211 		max_streams >>= 4;
1212 	else
1213 		max_streams &= 0xf;
1214 	max_streams++;
1215 	return max_streams;
1216 }
1217 
1218 static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1219 		unsigned int pipe, unsigned int streams)
1220 {
1221 	int max_streams;
1222 
1223 	streams--;
1224 	max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1225 	if (usb_pipeout(pipe)) {
1226 		streams <<= 4;
1227 		max_streams &= 0xf;
1228 	} else {
1229 		max_streams &= 0xf0;
1230 	}
1231 	max_streams |= streams;
1232 	dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams;
1233 }
1234 
1235 static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb)
1236 {
1237 	unsigned int max_streams;
1238 	int enabled;
1239 
1240 	enabled = dummy_ep_stream_en(dum_hcd, urb);
1241 	if (!urb->stream_id) {
1242 		if (enabled)
1243 			return -EINVAL;
1244 		return 0;
1245 	}
1246 	if (!enabled)
1247 		return -EINVAL;
1248 
1249 	max_streams = get_max_streams_for_pipe(dum_hcd,
1250 			usb_pipeendpoint(urb->pipe));
1251 	if (urb->stream_id > max_streams) {
1252 		dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n",
1253 				urb->stream_id);
1254 		BUG();
1255 		return -EINVAL;
1256 	}
1257 	return 0;
1258 }
1259 
1260 static int dummy_urb_enqueue(
1261 	struct usb_hcd			*hcd,
1262 	struct urb			*urb,
1263 	gfp_t				mem_flags
1264 ) {
1265 	struct dummy_hcd *dum_hcd;
1266 	struct urbp	*urbp;
1267 	unsigned long	flags;
1268 	int		rc;
1269 
1270 	urbp = kmalloc(sizeof *urbp, mem_flags);
1271 	if (!urbp)
1272 		return -ENOMEM;
1273 	urbp->urb = urb;
1274 	urbp->miter_started = 0;
1275 
1276 	dum_hcd = hcd_to_dummy_hcd(hcd);
1277 	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1278 
1279 	rc = dummy_validate_stream(dum_hcd, urb);
1280 	if (rc) {
1281 		kfree(urbp);
1282 		goto done;
1283 	}
1284 
1285 	rc = usb_hcd_link_urb_to_ep(hcd, urb);
1286 	if (rc) {
1287 		kfree(urbp);
1288 		goto done;
1289 	}
1290 
1291 	if (!dum_hcd->udev) {
1292 		dum_hcd->udev = urb->dev;
1293 		usb_get_dev(dum_hcd->udev);
1294 	} else if (unlikely(dum_hcd->udev != urb->dev))
1295 		dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
1296 
1297 	list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
1298 	urb->hcpriv = urbp;
1299 	if (!dum_hcd->next_frame_urbp)
1300 		dum_hcd->next_frame_urbp = urbp;
1301 	if (usb_pipetype(urb->pipe) == PIPE_CONTROL)
1302 		urb->error_count = 1;		/* mark as a new urb */
1303 
1304 	/* kick the scheduler, it'll do the rest */
1305 	if (!timer_pending(&dum_hcd->timer))
1306 		mod_timer(&dum_hcd->timer, jiffies + 1);
1307 
1308  done:
1309 	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1310 	return rc;
1311 }
1312 
1313 static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1314 {
1315 	struct dummy_hcd *dum_hcd;
1316 	unsigned long	flags;
1317 	int		rc;
1318 
1319 	/* giveback happens automatically in timer callback,
1320 	 * so make sure the callback happens */
1321 	dum_hcd = hcd_to_dummy_hcd(hcd);
1322 	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1323 
1324 	rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1325 	if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1326 			!list_empty(&dum_hcd->urbp_list))
1327 		mod_timer(&dum_hcd->timer, jiffies);
1328 
1329 	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1330 	return rc;
1331 }
1332 
1333 static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
1334 		u32 len)
1335 {
1336 	void *ubuf, *rbuf;
1337 	struct urbp *urbp = urb->hcpriv;
1338 	int to_host;
1339 	struct sg_mapping_iter *miter = &urbp->miter;
1340 	u32 trans = 0;
1341 	u32 this_sg;
1342 	bool next_sg;
1343 
1344 	to_host = usb_urb_dir_in(urb);
1345 	rbuf = req->req.buf + req->req.actual;
1346 
1347 	if (!urb->num_sgs) {
1348 		ubuf = urb->transfer_buffer + urb->actual_length;
1349 		if (to_host)
1350 			memcpy(ubuf, rbuf, len);
1351 		else
1352 			memcpy(rbuf, ubuf, len);
1353 		return len;
1354 	}
1355 
1356 	if (!urbp->miter_started) {
1357 		u32 flags = SG_MITER_ATOMIC;
1358 
1359 		if (to_host)
1360 			flags |= SG_MITER_TO_SG;
1361 		else
1362 			flags |= SG_MITER_FROM_SG;
1363 
1364 		sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
1365 		urbp->miter_started = 1;
1366 	}
1367 	next_sg = sg_miter_next(miter);
1368 	if (next_sg == false) {
1369 		WARN_ON_ONCE(1);
1370 		return -EINVAL;
1371 	}
1372 	do {
1373 		ubuf = miter->addr;
1374 		this_sg = min_t(u32, len, miter->length);
1375 		miter->consumed = this_sg;
1376 		trans += this_sg;
1377 
1378 		if (to_host)
1379 			memcpy(ubuf, rbuf, this_sg);
1380 		else
1381 			memcpy(rbuf, ubuf, this_sg);
1382 		len -= this_sg;
1383 
1384 		if (!len)
1385 			break;
1386 		next_sg = sg_miter_next(miter);
1387 		if (next_sg == false) {
1388 			WARN_ON_ONCE(1);
1389 			return -EINVAL;
1390 		}
1391 
1392 		rbuf += this_sg;
1393 	} while (1);
1394 
1395 	sg_miter_stop(miter);
1396 	return trans;
1397 }
1398 
1399 /* transfer up to a frame's worth; caller must own lock */
1400 static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb,
1401 		struct dummy_ep *ep, int limit, int *status)
1402 {
1403 	struct dummy		*dum = dum_hcd->dum;
1404 	struct dummy_request	*req;
1405 	int			sent = 0;
1406 
1407 top:
1408 	/* if there's no request queued, the device is NAKing; return */
1409 	list_for_each_entry(req, &ep->queue, queue) {
1410 		unsigned	host_len, dev_len, len;
1411 		int		is_short, to_host;
1412 		int		rescan = 0;
1413 
1414 		if (dummy_ep_stream_en(dum_hcd, urb)) {
1415 			if ((urb->stream_id != req->req.stream_id))
1416 				continue;
1417 		}
1418 
1419 		/* 1..N packets of ep->ep.maxpacket each ... the last one
1420 		 * may be short (including zero length).
1421 		 *
1422 		 * writer can send a zlp explicitly (length 0) or implicitly
1423 		 * (length mod maxpacket zero, and 'zero' flag); they always
1424 		 * terminate reads.
1425 		 */
1426 		host_len = urb->transfer_buffer_length - urb->actual_length;
1427 		dev_len = req->req.length - req->req.actual;
1428 		len = min(host_len, dev_len);
1429 
1430 		/* FIXME update emulated data toggle too */
1431 
1432 		to_host = usb_urb_dir_in(urb);
1433 		if (unlikely(len == 0))
1434 			is_short = 1;
1435 		else {
1436 			/* not enough bandwidth left? */
1437 			if (limit < ep->ep.maxpacket && limit < len)
1438 				break;
1439 			len = min_t(unsigned, len, limit);
1440 			if (len == 0)
1441 				break;
1442 
1443 			/* send multiple of maxpacket first, then remainder */
1444 			if (len >= ep->ep.maxpacket) {
1445 				is_short = 0;
1446 				if (len % ep->ep.maxpacket)
1447 					rescan = 1;
1448 				len -= len % ep->ep.maxpacket;
1449 			} else {
1450 				is_short = 1;
1451 			}
1452 
1453 			len = dummy_perform_transfer(urb, req, len);
1454 
1455 			ep->last_io = jiffies;
1456 			if ((int)len < 0) {
1457 				req->req.status = len;
1458 			} else {
1459 				limit -= len;
1460 				sent += len;
1461 				urb->actual_length += len;
1462 				req->req.actual += len;
1463 			}
1464 		}
1465 
1466 		/* short packets terminate, maybe with overflow/underflow.
1467 		 * it's only really an error to write too much.
1468 		 *
1469 		 * partially filling a buffer optionally blocks queue advances
1470 		 * (so completion handlers can clean up the queue) but we don't
1471 		 * need to emulate such data-in-flight.
1472 		 */
1473 		if (is_short) {
1474 			if (host_len == dev_len) {
1475 				req->req.status = 0;
1476 				*status = 0;
1477 			} else if (to_host) {
1478 				req->req.status = 0;
1479 				if (dev_len > host_len)
1480 					*status = -EOVERFLOW;
1481 				else
1482 					*status = 0;
1483 			} else {
1484 				*status = 0;
1485 				if (host_len > dev_len)
1486 					req->req.status = -EOVERFLOW;
1487 				else
1488 					req->req.status = 0;
1489 			}
1490 
1491 		/*
1492 		 * many requests terminate without a short packet.
1493 		 * send a zlp if demanded by flags.
1494 		 */
1495 		} else {
1496 			if (req->req.length == req->req.actual) {
1497 				if (req->req.zero && to_host)
1498 					rescan = 1;
1499 				else
1500 					req->req.status = 0;
1501 			}
1502 			if (urb->transfer_buffer_length == urb->actual_length) {
1503 				if (urb->transfer_flags & URB_ZERO_PACKET &&
1504 				    !to_host)
1505 					rescan = 1;
1506 				else
1507 					*status = 0;
1508 			}
1509 		}
1510 
1511 		/* device side completion --> continuable */
1512 		if (req->req.status != -EINPROGRESS) {
1513 			list_del_init(&req->queue);
1514 
1515 			spin_unlock(&dum->lock);
1516 			usb_gadget_giveback_request(&ep->ep, &req->req);
1517 			spin_lock(&dum->lock);
1518 
1519 			/* requests might have been unlinked... */
1520 			rescan = 1;
1521 		}
1522 
1523 		/* host side completion --> terminate */
1524 		if (*status != -EINPROGRESS)
1525 			break;
1526 
1527 		/* rescan to continue with any other queued i/o */
1528 		if (rescan)
1529 			goto top;
1530 	}
1531 	return sent;
1532 }
1533 
1534 static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep)
1535 {
1536 	int	limit = ep->ep.maxpacket;
1537 
1538 	if (dum->gadget.speed == USB_SPEED_HIGH) {
1539 		int	tmp;
1540 
1541 		/* high bandwidth mode */
1542 		tmp = usb_endpoint_maxp_mult(ep->desc);
1543 		tmp *= 8 /* applies to entire frame */;
1544 		limit += limit * tmp;
1545 	}
1546 	if (dum->gadget.speed == USB_SPEED_SUPER) {
1547 		switch (usb_endpoint_type(ep->desc)) {
1548 		case USB_ENDPOINT_XFER_ISOC:
1549 			/* Sec. 4.4.8.2 USB3.0 Spec */
1550 			limit = 3 * 16 * 1024 * 8;
1551 			break;
1552 		case USB_ENDPOINT_XFER_INT:
1553 			/* Sec. 4.4.7.2 USB3.0 Spec */
1554 			limit = 3 * 1024 * 8;
1555 			break;
1556 		case USB_ENDPOINT_XFER_BULK:
1557 		default:
1558 			break;
1559 		}
1560 	}
1561 	return limit;
1562 }
1563 
1564 #define is_active(dum_hcd)	((dum_hcd->port_status & \
1565 		(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1566 			USB_PORT_STAT_SUSPEND)) \
1567 		== (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1568 
1569 static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address)
1570 {
1571 	int		i;
1572 
1573 	if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1574 			dum->ss_hcd : dum->hs_hcd)))
1575 		return NULL;
1576 	if (!dum->ints_enabled)
1577 		return NULL;
1578 	if ((address & ~USB_DIR_IN) == 0)
1579 		return &dum->ep[0];
1580 	for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1581 		struct dummy_ep	*ep = &dum->ep[i];
1582 
1583 		if (!ep->desc)
1584 			continue;
1585 		if (ep->desc->bEndpointAddress == address)
1586 			return ep;
1587 	}
1588 	return NULL;
1589 }
1590 
1591 #undef is_active
1592 
1593 #define Dev_Request	(USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1594 #define Dev_InRequest	(Dev_Request | USB_DIR_IN)
1595 #define Intf_Request	(USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1596 #define Intf_InRequest	(Intf_Request | USB_DIR_IN)
1597 #define Ep_Request	(USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1598 #define Ep_InRequest	(Ep_Request | USB_DIR_IN)
1599 
1600 
1601 /**
1602  * handle_control_request() - handles all control transfers
1603  * @dum_hcd: pointer to dummy (the_controller)
1604  * @urb: the urb request to handle
1605  * @setup: pointer to the setup data for a USB device control
1606  *	 request
1607  * @status: pointer to request handling status
1608  *
1609  * Return 0 - if the request was handled
1610  *	  1 - if the request wasn't handles
1611  *	  error code on error
1612  */
1613 static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
1614 				  struct usb_ctrlrequest *setup,
1615 				  int *status)
1616 {
1617 	struct dummy_ep		*ep2;
1618 	struct dummy		*dum = dum_hcd->dum;
1619 	int			ret_val = 1;
1620 	unsigned	w_index;
1621 	unsigned	w_value;
1622 
1623 	w_index = le16_to_cpu(setup->wIndex);
1624 	w_value = le16_to_cpu(setup->wValue);
1625 	switch (setup->bRequest) {
1626 	case USB_REQ_SET_ADDRESS:
1627 		if (setup->bRequestType != Dev_Request)
1628 			break;
1629 		dum->address = w_value;
1630 		*status = 0;
1631 		dev_dbg(udc_dev(dum), "set_address = %d\n",
1632 				w_value);
1633 		ret_val = 0;
1634 		break;
1635 	case USB_REQ_SET_FEATURE:
1636 		if (setup->bRequestType == Dev_Request) {
1637 			ret_val = 0;
1638 			switch (w_value) {
1639 			case USB_DEVICE_REMOTE_WAKEUP:
1640 				break;
1641 			case USB_DEVICE_B_HNP_ENABLE:
1642 				dum->gadget.b_hnp_enable = 1;
1643 				break;
1644 			case USB_DEVICE_A_HNP_SUPPORT:
1645 				dum->gadget.a_hnp_support = 1;
1646 				break;
1647 			case USB_DEVICE_A_ALT_HNP_SUPPORT:
1648 				dum->gadget.a_alt_hnp_support = 1;
1649 				break;
1650 			case USB_DEVICE_U1_ENABLE:
1651 				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1652 				    HCD_USB3)
1653 					w_value = USB_DEV_STAT_U1_ENABLED;
1654 				else
1655 					ret_val = -EOPNOTSUPP;
1656 				break;
1657 			case USB_DEVICE_U2_ENABLE:
1658 				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1659 				    HCD_USB3)
1660 					w_value = USB_DEV_STAT_U2_ENABLED;
1661 				else
1662 					ret_val = -EOPNOTSUPP;
1663 				break;
1664 			case USB_DEVICE_LTM_ENABLE:
1665 				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1666 				    HCD_USB3)
1667 					w_value = USB_DEV_STAT_LTM_ENABLED;
1668 				else
1669 					ret_val = -EOPNOTSUPP;
1670 				break;
1671 			default:
1672 				ret_val = -EOPNOTSUPP;
1673 			}
1674 			if (ret_val == 0) {
1675 				dum->devstatus |= (1 << w_value);
1676 				*status = 0;
1677 			}
1678 		} else if (setup->bRequestType == Ep_Request) {
1679 			/* endpoint halt */
1680 			ep2 = find_endpoint(dum, w_index);
1681 			if (!ep2 || ep2->ep.name == ep0name) {
1682 				ret_val = -EOPNOTSUPP;
1683 				break;
1684 			}
1685 			ep2->halted = 1;
1686 			ret_val = 0;
1687 			*status = 0;
1688 		}
1689 		break;
1690 	case USB_REQ_CLEAR_FEATURE:
1691 		if (setup->bRequestType == Dev_Request) {
1692 			ret_val = 0;
1693 			switch (w_value) {
1694 			case USB_DEVICE_REMOTE_WAKEUP:
1695 				w_value = USB_DEVICE_REMOTE_WAKEUP;
1696 				break;
1697 			case USB_DEVICE_U1_ENABLE:
1698 				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1699 				    HCD_USB3)
1700 					w_value = USB_DEV_STAT_U1_ENABLED;
1701 				else
1702 					ret_val = -EOPNOTSUPP;
1703 				break;
1704 			case USB_DEVICE_U2_ENABLE:
1705 				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1706 				    HCD_USB3)
1707 					w_value = USB_DEV_STAT_U2_ENABLED;
1708 				else
1709 					ret_val = -EOPNOTSUPP;
1710 				break;
1711 			case USB_DEVICE_LTM_ENABLE:
1712 				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1713 				    HCD_USB3)
1714 					w_value = USB_DEV_STAT_LTM_ENABLED;
1715 				else
1716 					ret_val = -EOPNOTSUPP;
1717 				break;
1718 			default:
1719 				ret_val = -EOPNOTSUPP;
1720 				break;
1721 			}
1722 			if (ret_val == 0) {
1723 				dum->devstatus &= ~(1 << w_value);
1724 				*status = 0;
1725 			}
1726 		} else if (setup->bRequestType == Ep_Request) {
1727 			/* endpoint halt */
1728 			ep2 = find_endpoint(dum, w_index);
1729 			if (!ep2) {
1730 				ret_val = -EOPNOTSUPP;
1731 				break;
1732 			}
1733 			if (!ep2->wedged)
1734 				ep2->halted = 0;
1735 			ret_val = 0;
1736 			*status = 0;
1737 		}
1738 		break;
1739 	case USB_REQ_GET_STATUS:
1740 		if (setup->bRequestType == Dev_InRequest
1741 				|| setup->bRequestType == Intf_InRequest
1742 				|| setup->bRequestType == Ep_InRequest) {
1743 			char *buf;
1744 			/*
1745 			 * device: remote wakeup, selfpowered
1746 			 * interface: nothing
1747 			 * endpoint: halt
1748 			 */
1749 			buf = (char *)urb->transfer_buffer;
1750 			if (urb->transfer_buffer_length > 0) {
1751 				if (setup->bRequestType == Ep_InRequest) {
1752 					ep2 = find_endpoint(dum, w_index);
1753 					if (!ep2) {
1754 						ret_val = -EOPNOTSUPP;
1755 						break;
1756 					}
1757 					buf[0] = ep2->halted;
1758 				} else if (setup->bRequestType ==
1759 					   Dev_InRequest) {
1760 					buf[0] = (u8)dum->devstatus;
1761 				} else
1762 					buf[0] = 0;
1763 			}
1764 			if (urb->transfer_buffer_length > 1)
1765 				buf[1] = 0;
1766 			urb->actual_length = min_t(u32, 2,
1767 				urb->transfer_buffer_length);
1768 			ret_val = 0;
1769 			*status = 0;
1770 		}
1771 		break;
1772 	}
1773 	return ret_val;
1774 }
1775 
1776 /*
1777  * Drive both sides of the transfers; looks like irq handlers to both
1778  * drivers except that the callbacks are invoked from soft interrupt
1779  * context.
1780  */
1781 static void dummy_timer(struct timer_list *t)
1782 {
1783 	struct dummy_hcd	*dum_hcd = from_timer(dum_hcd, t, timer);
1784 	struct dummy		*dum = dum_hcd->dum;
1785 	struct urbp		*urbp, *tmp;
1786 	unsigned long		flags;
1787 	int			limit, total;
1788 	int			i;
1789 
1790 	/* simplistic model for one frame's bandwidth */
1791 	/* FIXME: account for transaction and packet overhead */
1792 	switch (dum->gadget.speed) {
1793 	case USB_SPEED_LOW:
1794 		total = 8/*bytes*/ * 12/*packets*/;
1795 		break;
1796 	case USB_SPEED_FULL:
1797 		total = 64/*bytes*/ * 19/*packets*/;
1798 		break;
1799 	case USB_SPEED_HIGH:
1800 		total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1801 		break;
1802 	case USB_SPEED_SUPER:
1803 		/* Bus speed is 500000 bytes/ms, so use a little less */
1804 		total = 490000;
1805 		break;
1806 	default:	/* Can't happen */
1807 		dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
1808 		total = 0;
1809 		break;
1810 	}
1811 
1812 	/* FIXME if HZ != 1000 this will probably misbehave ... */
1813 
1814 	/* look at each urb queued by the host side driver */
1815 	spin_lock_irqsave(&dum->lock, flags);
1816 
1817 	if (!dum_hcd->udev) {
1818 		dev_err(dummy_dev(dum_hcd),
1819 				"timer fired with no URBs pending?\n");
1820 		spin_unlock_irqrestore(&dum->lock, flags);
1821 		return;
1822 	}
1823 	dum_hcd->next_frame_urbp = NULL;
1824 
1825 	for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1826 		if (!ep_info[i].name)
1827 			break;
1828 		dum->ep[i].already_seen = 0;
1829 	}
1830 
1831 restart:
1832 	list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
1833 		struct urb		*urb;
1834 		struct dummy_request	*req;
1835 		u8			address;
1836 		struct dummy_ep		*ep = NULL;
1837 		int			status = -EINPROGRESS;
1838 
1839 		/* stop when we reach URBs queued after the timer interrupt */
1840 		if (urbp == dum_hcd->next_frame_urbp)
1841 			break;
1842 
1843 		urb = urbp->urb;
1844 		if (urb->unlinked)
1845 			goto return_urb;
1846 		else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
1847 			continue;
1848 
1849 		/* Used up this frame's bandwidth? */
1850 		if (total <= 0)
1851 			continue;
1852 
1853 		/* find the gadget's ep for this request (if configured) */
1854 		address = usb_pipeendpoint (urb->pipe);
1855 		if (usb_urb_dir_in(urb))
1856 			address |= USB_DIR_IN;
1857 		ep = find_endpoint(dum, address);
1858 		if (!ep) {
1859 			/* set_configuration() disagreement */
1860 			dev_dbg(dummy_dev(dum_hcd),
1861 				"no ep configured for urb %p\n",
1862 				urb);
1863 			status = -EPROTO;
1864 			goto return_urb;
1865 		}
1866 
1867 		if (ep->already_seen)
1868 			continue;
1869 		ep->already_seen = 1;
1870 		if (ep == &dum->ep[0] && urb->error_count) {
1871 			ep->setup_stage = 1;	/* a new urb */
1872 			urb->error_count = 0;
1873 		}
1874 		if (ep->halted && !ep->setup_stage) {
1875 			/* NOTE: must not be iso! */
1876 			dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
1877 					ep->ep.name, urb);
1878 			status = -EPIPE;
1879 			goto return_urb;
1880 		}
1881 		/* FIXME make sure both ends agree on maxpacket */
1882 
1883 		/* handle control requests */
1884 		if (ep == &dum->ep[0] && ep->setup_stage) {
1885 			struct usb_ctrlrequest		setup;
1886 			int				value;
1887 
1888 			setup = *(struct usb_ctrlrequest *) urb->setup_packet;
1889 			/* paranoia, in case of stale queued data */
1890 			list_for_each_entry(req, &ep->queue, queue) {
1891 				list_del_init(&req->queue);
1892 				req->req.status = -EOVERFLOW;
1893 				dev_dbg(udc_dev(dum), "stale req = %p\n",
1894 						req);
1895 
1896 				spin_unlock(&dum->lock);
1897 				usb_gadget_giveback_request(&ep->ep, &req->req);
1898 				spin_lock(&dum->lock);
1899 				ep->already_seen = 0;
1900 				goto restart;
1901 			}
1902 
1903 			/* gadget driver never sees set_address or operations
1904 			 * on standard feature flags.  some hardware doesn't
1905 			 * even expose them.
1906 			 */
1907 			ep->last_io = jiffies;
1908 			ep->setup_stage = 0;
1909 			ep->halted = 0;
1910 
1911 			value = handle_control_request(dum_hcd, urb, &setup,
1912 						       &status);
1913 
1914 			/* gadget driver handles all other requests.  block
1915 			 * until setup() returns; no reentrancy issues etc.
1916 			 */
1917 			if (value > 0) {
1918 				++dum->callback_usage;
1919 				spin_unlock(&dum->lock);
1920 				value = dum->driver->setup(&dum->gadget,
1921 						&setup);
1922 				spin_lock(&dum->lock);
1923 				--dum->callback_usage;
1924 
1925 				if (value >= 0) {
1926 					/* no delays (max 64KB data stage) */
1927 					limit = 64*1024;
1928 					goto treat_control_like_bulk;
1929 				}
1930 				/* error, see below */
1931 			}
1932 
1933 			if (value < 0) {
1934 				if (value != -EOPNOTSUPP)
1935 					dev_dbg(udc_dev(dum),
1936 						"setup --> %d\n",
1937 						value);
1938 				status = -EPIPE;
1939 				urb->actual_length = 0;
1940 			}
1941 
1942 			goto return_urb;
1943 		}
1944 
1945 		/* non-control requests */
1946 		limit = total;
1947 		switch (usb_pipetype(urb->pipe)) {
1948 		case PIPE_ISOCHRONOUS:
1949 			/*
1950 			 * We don't support isochronous.  But if we did,
1951 			 * here are some of the issues we'd have to face:
1952 			 *
1953 			 * Is it urb->interval since the last xfer?
1954 			 * Use urb->iso_frame_desc[i].
1955 			 * Complete whether or not ep has requests queued.
1956 			 * Report random errors, to debug drivers.
1957 			 */
1958 			limit = max(limit, periodic_bytes(dum, ep));
1959 			status = -EINVAL;	/* fail all xfers */
1960 			break;
1961 
1962 		case PIPE_INTERRUPT:
1963 			/* FIXME is it urb->interval since the last xfer?
1964 			 * this almost certainly polls too fast.
1965 			 */
1966 			limit = max(limit, periodic_bytes(dum, ep));
1967 			fallthrough;
1968 
1969 		default:
1970 treat_control_like_bulk:
1971 			ep->last_io = jiffies;
1972 			total -= transfer(dum_hcd, urb, ep, limit, &status);
1973 			break;
1974 		}
1975 
1976 		/* incomplete transfer? */
1977 		if (status == -EINPROGRESS)
1978 			continue;
1979 
1980 return_urb:
1981 		list_del(&urbp->urbp_list);
1982 		kfree(urbp);
1983 		if (ep)
1984 			ep->already_seen = ep->setup_stage = 0;
1985 
1986 		usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
1987 		spin_unlock(&dum->lock);
1988 		usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
1989 		spin_lock(&dum->lock);
1990 
1991 		goto restart;
1992 	}
1993 
1994 	if (list_empty(&dum_hcd->urbp_list)) {
1995 		usb_put_dev(dum_hcd->udev);
1996 		dum_hcd->udev = NULL;
1997 	} else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
1998 		/* want a 1 msec delay here */
1999 		mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
2000 	}
2001 
2002 	spin_unlock_irqrestore(&dum->lock, flags);
2003 }
2004 
2005 /*-------------------------------------------------------------------------*/
2006 
2007 #define PORT_C_MASK \
2008 	((USB_PORT_STAT_C_CONNECTION \
2009 	| USB_PORT_STAT_C_ENABLE \
2010 	| USB_PORT_STAT_C_SUSPEND \
2011 	| USB_PORT_STAT_C_OVERCURRENT \
2012 	| USB_PORT_STAT_C_RESET) << 16)
2013 
2014 static int dummy_hub_status(struct usb_hcd *hcd, char *buf)
2015 {
2016 	struct dummy_hcd	*dum_hcd;
2017 	unsigned long		flags;
2018 	int			retval = 0;
2019 
2020 	dum_hcd = hcd_to_dummy_hcd(hcd);
2021 
2022 	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2023 	if (!HCD_HW_ACCESSIBLE(hcd))
2024 		goto done;
2025 
2026 	if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
2027 		dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
2028 		dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
2029 		set_link_state(dum_hcd);
2030 	}
2031 
2032 	if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
2033 		*buf = (1 << 1);
2034 		dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
2035 				dum_hcd->port_status);
2036 		retval = 1;
2037 		if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
2038 			usb_hcd_resume_root_hub(hcd);
2039 	}
2040 done:
2041 	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2042 	return retval;
2043 }
2044 
2045 /* usb 3.0 root hub device descriptor */
2046 static struct {
2047 	struct usb_bos_descriptor bos;
2048 	struct usb_ss_cap_descriptor ss_cap;
2049 } __packed usb3_bos_desc = {
2050 
2051 	.bos = {
2052 		.bLength		= USB_DT_BOS_SIZE,
2053 		.bDescriptorType	= USB_DT_BOS,
2054 		.wTotalLength		= cpu_to_le16(sizeof(usb3_bos_desc)),
2055 		.bNumDeviceCaps		= 1,
2056 	},
2057 	.ss_cap = {
2058 		.bLength		= USB_DT_USB_SS_CAP_SIZE,
2059 		.bDescriptorType	= USB_DT_DEVICE_CAPABILITY,
2060 		.bDevCapabilityType	= USB_SS_CAP_TYPE,
2061 		.wSpeedSupported	= cpu_to_le16(USB_5GBPS_OPERATION),
2062 		.bFunctionalitySupport	= ilog2(USB_5GBPS_OPERATION),
2063 	},
2064 };
2065 
2066 static inline void
2067 ss_hub_descriptor(struct usb_hub_descriptor *desc)
2068 {
2069 	memset(desc, 0, sizeof *desc);
2070 	desc->bDescriptorType = USB_DT_SS_HUB;
2071 	desc->bDescLength = 12;
2072 	desc->wHubCharacteristics = cpu_to_le16(
2073 			HUB_CHAR_INDV_PORT_LPSM |
2074 			HUB_CHAR_COMMON_OCPM);
2075 	desc->bNbrPorts = 1;
2076 	desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
2077 	desc->u.ss.DeviceRemovable = 0;
2078 }
2079 
2080 static inline void hub_descriptor(struct usb_hub_descriptor *desc)
2081 {
2082 	memset(desc, 0, sizeof *desc);
2083 	desc->bDescriptorType = USB_DT_HUB;
2084 	desc->bDescLength = 9;
2085 	desc->wHubCharacteristics = cpu_to_le16(
2086 			HUB_CHAR_INDV_PORT_LPSM |
2087 			HUB_CHAR_COMMON_OCPM);
2088 	desc->bNbrPorts = 1;
2089 	desc->u.hs.DeviceRemovable[0] = 0;
2090 	desc->u.hs.DeviceRemovable[1] = 0xff;	/* PortPwrCtrlMask */
2091 }
2092 
2093 static int dummy_hub_control(
2094 	struct usb_hcd	*hcd,
2095 	u16		typeReq,
2096 	u16		wValue,
2097 	u16		wIndex,
2098 	char		*buf,
2099 	u16		wLength
2100 ) {
2101 	struct dummy_hcd *dum_hcd;
2102 	int		retval = 0;
2103 	unsigned long	flags;
2104 
2105 	if (!HCD_HW_ACCESSIBLE(hcd))
2106 		return -ETIMEDOUT;
2107 
2108 	dum_hcd = hcd_to_dummy_hcd(hcd);
2109 
2110 	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2111 	switch (typeReq) {
2112 	case ClearHubFeature:
2113 		break;
2114 	case ClearPortFeature:
2115 		switch (wValue) {
2116 		case USB_PORT_FEAT_SUSPEND:
2117 			if (hcd->speed == HCD_USB3) {
2118 				dev_dbg(dummy_dev(dum_hcd),
2119 					 "USB_PORT_FEAT_SUSPEND req not "
2120 					 "supported for USB 3.0 roothub\n");
2121 				goto error;
2122 			}
2123 			if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
2124 				/* 20msec resume signaling */
2125 				dum_hcd->resuming = 1;
2126 				dum_hcd->re_timeout = jiffies +
2127 						msecs_to_jiffies(20);
2128 			}
2129 			break;
2130 		case USB_PORT_FEAT_POWER:
2131 			dev_dbg(dummy_dev(dum_hcd), "power-off\n");
2132 			if (hcd->speed == HCD_USB3)
2133 				dum_hcd->port_status &= ~USB_SS_PORT_STAT_POWER;
2134 			else
2135 				dum_hcd->port_status &= ~USB_PORT_STAT_POWER;
2136 			set_link_state(dum_hcd);
2137 			break;
2138 		case USB_PORT_FEAT_ENABLE:
2139 		case USB_PORT_FEAT_C_ENABLE:
2140 		case USB_PORT_FEAT_C_SUSPEND:
2141 			/* Not allowed for USB-3 */
2142 			if (hcd->speed == HCD_USB3)
2143 				goto error;
2144 			fallthrough;
2145 		case USB_PORT_FEAT_C_CONNECTION:
2146 		case USB_PORT_FEAT_C_RESET:
2147 			dum_hcd->port_status &= ~(1 << wValue);
2148 			set_link_state(dum_hcd);
2149 			break;
2150 		default:
2151 		/* Disallow INDICATOR and C_OVER_CURRENT */
2152 			goto error;
2153 		}
2154 		break;
2155 	case GetHubDescriptor:
2156 		if (hcd->speed == HCD_USB3 &&
2157 				(wLength < USB_DT_SS_HUB_SIZE ||
2158 				 wValue != (USB_DT_SS_HUB << 8))) {
2159 			dev_dbg(dummy_dev(dum_hcd),
2160 				"Wrong hub descriptor type for "
2161 				"USB 3.0 roothub.\n");
2162 			goto error;
2163 		}
2164 		if (hcd->speed == HCD_USB3)
2165 			ss_hub_descriptor((struct usb_hub_descriptor *) buf);
2166 		else
2167 			hub_descriptor((struct usb_hub_descriptor *) buf);
2168 		break;
2169 
2170 	case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
2171 		if (hcd->speed != HCD_USB3)
2172 			goto error;
2173 
2174 		if ((wValue >> 8) != USB_DT_BOS)
2175 			goto error;
2176 
2177 		memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc));
2178 		retval = sizeof(usb3_bos_desc);
2179 		break;
2180 
2181 	case GetHubStatus:
2182 		*(__le32 *) buf = cpu_to_le32(0);
2183 		break;
2184 	case GetPortStatus:
2185 		if (wIndex != 1)
2186 			retval = -EPIPE;
2187 
2188 		/* whoever resets or resumes must GetPortStatus to
2189 		 * complete it!!
2190 		 */
2191 		if (dum_hcd->resuming &&
2192 				time_after_eq(jiffies, dum_hcd->re_timeout)) {
2193 			dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
2194 			dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
2195 		}
2196 		if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
2197 				time_after_eq(jiffies, dum_hcd->re_timeout)) {
2198 			dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
2199 			dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
2200 			if (dum_hcd->dum->pullup) {
2201 				dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
2202 
2203 				if (hcd->speed < HCD_USB3) {
2204 					switch (dum_hcd->dum->gadget.speed) {
2205 					case USB_SPEED_HIGH:
2206 						dum_hcd->port_status |=
2207 						      USB_PORT_STAT_HIGH_SPEED;
2208 						break;
2209 					case USB_SPEED_LOW:
2210 						dum_hcd->dum->gadget.ep0->
2211 							maxpacket = 8;
2212 						dum_hcd->port_status |=
2213 							USB_PORT_STAT_LOW_SPEED;
2214 						break;
2215 					default:
2216 						break;
2217 					}
2218 				}
2219 			}
2220 		}
2221 		set_link_state(dum_hcd);
2222 		((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status);
2223 		((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16);
2224 		break;
2225 	case SetHubFeature:
2226 		retval = -EPIPE;
2227 		break;
2228 	case SetPortFeature:
2229 		switch (wValue) {
2230 		case USB_PORT_FEAT_LINK_STATE:
2231 			if (hcd->speed != HCD_USB3) {
2232 				dev_dbg(dummy_dev(dum_hcd),
2233 					 "USB_PORT_FEAT_LINK_STATE req not "
2234 					 "supported for USB 2.0 roothub\n");
2235 				goto error;
2236 			}
2237 			/*
2238 			 * Since this is dummy we don't have an actual link so
2239 			 * there is nothing to do for the SET_LINK_STATE cmd
2240 			 */
2241 			break;
2242 		case USB_PORT_FEAT_U1_TIMEOUT:
2243 		case USB_PORT_FEAT_U2_TIMEOUT:
2244 			/* TODO: add suspend/resume support! */
2245 			if (hcd->speed != HCD_USB3) {
2246 				dev_dbg(dummy_dev(dum_hcd),
2247 					 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
2248 					 "supported for USB 2.0 roothub\n");
2249 				goto error;
2250 			}
2251 			break;
2252 		case USB_PORT_FEAT_SUSPEND:
2253 			/* Applicable only for USB2.0 hub */
2254 			if (hcd->speed == HCD_USB3) {
2255 				dev_dbg(dummy_dev(dum_hcd),
2256 					 "USB_PORT_FEAT_SUSPEND req not "
2257 					 "supported for USB 3.0 roothub\n");
2258 				goto error;
2259 			}
2260 			if (dum_hcd->active) {
2261 				dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
2262 
2263 				/* HNP would happen here; for now we
2264 				 * assume b_bus_req is always true.
2265 				 */
2266 				set_link_state(dum_hcd);
2267 				if (((1 << USB_DEVICE_B_HNP_ENABLE)
2268 						& dum_hcd->dum->devstatus) != 0)
2269 					dev_dbg(dummy_dev(dum_hcd),
2270 							"no HNP yet!\n");
2271 			}
2272 			break;
2273 		case USB_PORT_FEAT_POWER:
2274 			if (hcd->speed == HCD_USB3)
2275 				dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
2276 			else
2277 				dum_hcd->port_status |= USB_PORT_STAT_POWER;
2278 			set_link_state(dum_hcd);
2279 			break;
2280 		case USB_PORT_FEAT_BH_PORT_RESET:
2281 			/* Applicable only for USB3.0 hub */
2282 			if (hcd->speed != HCD_USB3) {
2283 				dev_dbg(dummy_dev(dum_hcd),
2284 					 "USB_PORT_FEAT_BH_PORT_RESET req not "
2285 					 "supported for USB 2.0 roothub\n");
2286 				goto error;
2287 			}
2288 			fallthrough;
2289 		case USB_PORT_FEAT_RESET:
2290 			if (!(dum_hcd->port_status & USB_PORT_STAT_CONNECTION))
2291 				break;
2292 			/* if it's already enabled, disable */
2293 			if (hcd->speed == HCD_USB3) {
2294 				dum_hcd->port_status =
2295 					(USB_SS_PORT_STAT_POWER |
2296 					 USB_PORT_STAT_CONNECTION |
2297 					 USB_PORT_STAT_RESET);
2298 			} else {
2299 				dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
2300 					| USB_PORT_STAT_LOW_SPEED
2301 					| USB_PORT_STAT_HIGH_SPEED);
2302 				dum_hcd->port_status |= USB_PORT_STAT_RESET;
2303 			}
2304 			/*
2305 			 * We want to reset device status. All but the
2306 			 * Self powered feature
2307 			 */
2308 			dum_hcd->dum->devstatus &=
2309 				(1 << USB_DEVICE_SELF_POWERED);
2310 			/*
2311 			 * FIXME USB3.0: what is the correct reset signaling
2312 			 * interval? Is it still 50msec as for HS?
2313 			 */
2314 			dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
2315 			set_link_state(dum_hcd);
2316 			break;
2317 		case USB_PORT_FEAT_C_CONNECTION:
2318 		case USB_PORT_FEAT_C_RESET:
2319 		case USB_PORT_FEAT_C_ENABLE:
2320 		case USB_PORT_FEAT_C_SUSPEND:
2321 			/* Not allowed for USB-3, and ignored for USB-2 */
2322 			if (hcd->speed == HCD_USB3)
2323 				goto error;
2324 			break;
2325 		default:
2326 		/* Disallow TEST, INDICATOR, and C_OVER_CURRENT */
2327 			goto error;
2328 		}
2329 		break;
2330 	case GetPortErrorCount:
2331 		if (hcd->speed != HCD_USB3) {
2332 			dev_dbg(dummy_dev(dum_hcd),
2333 				 "GetPortErrorCount req not "
2334 				 "supported for USB 2.0 roothub\n");
2335 			goto error;
2336 		}
2337 		/* We'll always return 0 since this is a dummy hub */
2338 		*(__le32 *) buf = cpu_to_le32(0);
2339 		break;
2340 	case SetHubDepth:
2341 		if (hcd->speed != HCD_USB3) {
2342 			dev_dbg(dummy_dev(dum_hcd),
2343 				 "SetHubDepth req not supported for "
2344 				 "USB 2.0 roothub\n");
2345 			goto error;
2346 		}
2347 		break;
2348 	default:
2349 		dev_dbg(dummy_dev(dum_hcd),
2350 			"hub control req%04x v%04x i%04x l%d\n",
2351 			typeReq, wValue, wIndex, wLength);
2352 error:
2353 		/* "protocol stall" on error */
2354 		retval = -EPIPE;
2355 	}
2356 	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2357 
2358 	if ((dum_hcd->port_status & PORT_C_MASK) != 0)
2359 		usb_hcd_poll_rh_status(hcd);
2360 	return retval;
2361 }
2362 
2363 static int dummy_bus_suspend(struct usb_hcd *hcd)
2364 {
2365 	struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2366 
2367 	dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
2368 
2369 	spin_lock_irq(&dum_hcd->dum->lock);
2370 	dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2371 	set_link_state(dum_hcd);
2372 	hcd->state = HC_STATE_SUSPENDED;
2373 	spin_unlock_irq(&dum_hcd->dum->lock);
2374 	return 0;
2375 }
2376 
2377 static int dummy_bus_resume(struct usb_hcd *hcd)
2378 {
2379 	struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2380 	int rc = 0;
2381 
2382 	dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
2383 
2384 	spin_lock_irq(&dum_hcd->dum->lock);
2385 	if (!HCD_HW_ACCESSIBLE(hcd)) {
2386 		rc = -ESHUTDOWN;
2387 	} else {
2388 		dum_hcd->rh_state = DUMMY_RH_RUNNING;
2389 		set_link_state(dum_hcd);
2390 		if (!list_empty(&dum_hcd->urbp_list))
2391 			mod_timer(&dum_hcd->timer, jiffies);
2392 		hcd->state = HC_STATE_RUNNING;
2393 	}
2394 	spin_unlock_irq(&dum_hcd->dum->lock);
2395 	return rc;
2396 }
2397 
2398 /*-------------------------------------------------------------------------*/
2399 
2400 static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb)
2401 {
2402 	int ep = usb_pipeendpoint(urb->pipe);
2403 
2404 	return scnprintf(buf, size,
2405 		"urb/%p %s ep%d%s%s len %d/%d\n",
2406 		urb,
2407 		({ char *s;
2408 		switch (urb->dev->speed) {
2409 		case USB_SPEED_LOW:
2410 			s = "ls";
2411 			break;
2412 		case USB_SPEED_FULL:
2413 			s = "fs";
2414 			break;
2415 		case USB_SPEED_HIGH:
2416 			s = "hs";
2417 			break;
2418 		case USB_SPEED_SUPER:
2419 			s = "ss";
2420 			break;
2421 		default:
2422 			s = "?";
2423 			break;
2424 		 } s; }),
2425 		ep, ep ? (usb_urb_dir_in(urb) ? "in" : "out") : "",
2426 		({ char *s; \
2427 		switch (usb_pipetype(urb->pipe)) { \
2428 		case PIPE_CONTROL: \
2429 			s = ""; \
2430 			break; \
2431 		case PIPE_BULK: \
2432 			s = "-bulk"; \
2433 			break; \
2434 		case PIPE_INTERRUPT: \
2435 			s = "-int"; \
2436 			break; \
2437 		default: \
2438 			s = "-iso"; \
2439 			break; \
2440 		} s; }),
2441 		urb->actual_length, urb->transfer_buffer_length);
2442 }
2443 
2444 static ssize_t urbs_show(struct device *dev, struct device_attribute *attr,
2445 		char *buf)
2446 {
2447 	struct usb_hcd		*hcd = dev_get_drvdata(dev);
2448 	struct dummy_hcd	*dum_hcd = hcd_to_dummy_hcd(hcd);
2449 	struct urbp		*urbp;
2450 	size_t			size = 0;
2451 	unsigned long		flags;
2452 
2453 	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2454 	list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
2455 		size_t		temp;
2456 
2457 		temp = show_urb(buf, PAGE_SIZE - size, urbp->urb);
2458 		buf += temp;
2459 		size += temp;
2460 	}
2461 	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2462 
2463 	return size;
2464 }
2465 static DEVICE_ATTR_RO(urbs);
2466 
2467 static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2468 {
2469 	timer_setup(&dum_hcd->timer, dummy_timer, 0);
2470 	dum_hcd->rh_state = DUMMY_RH_RUNNING;
2471 	dum_hcd->stream_en_ep = 0;
2472 	INIT_LIST_HEAD(&dum_hcd->urbp_list);
2473 	dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET_3;
2474 	dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2475 	dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2476 #ifdef CONFIG_USB_OTG
2477 	dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2478 #endif
2479 	return 0;
2480 
2481 	/* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2482 	return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2483 }
2484 
2485 static int dummy_start(struct usb_hcd *hcd)
2486 {
2487 	struct dummy_hcd	*dum_hcd = hcd_to_dummy_hcd(hcd);
2488 
2489 	/*
2490 	 * HOST side init ... we emulate a root hub that'll only ever
2491 	 * talk to one device (the gadget side).  Also appears in sysfs,
2492 	 * just like more familiar pci-based HCDs.
2493 	 */
2494 	if (!usb_hcd_is_primary_hcd(hcd))
2495 		return dummy_start_ss(dum_hcd);
2496 
2497 	spin_lock_init(&dum_hcd->dum->lock);
2498 	timer_setup(&dum_hcd->timer, dummy_timer, 0);
2499 	dum_hcd->rh_state = DUMMY_RH_RUNNING;
2500 
2501 	INIT_LIST_HEAD(&dum_hcd->urbp_list);
2502 
2503 	hcd->power_budget = POWER_BUDGET;
2504 	hcd->state = HC_STATE_RUNNING;
2505 	hcd->uses_new_polling = 1;
2506 
2507 #ifdef CONFIG_USB_OTG
2508 	hcd->self.otg_port = 1;
2509 #endif
2510 
2511 	/* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2512 	return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2513 }
2514 
2515 static void dummy_stop(struct usb_hcd *hcd)
2516 {
2517 	device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2518 	dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
2519 }
2520 
2521 /*-------------------------------------------------------------------------*/
2522 
2523 static int dummy_h_get_frame(struct usb_hcd *hcd)
2524 {
2525 	return dummy_g_get_frame(NULL);
2526 }
2527 
2528 static int dummy_setup(struct usb_hcd *hcd)
2529 {
2530 	struct dummy *dum;
2531 
2532 	dum = *((void **)dev_get_platdata(hcd->self.controller));
2533 	hcd->self.sg_tablesize = ~0;
2534 	if (usb_hcd_is_primary_hcd(hcd)) {
2535 		dum->hs_hcd = hcd_to_dummy_hcd(hcd);
2536 		dum->hs_hcd->dum = dum;
2537 		/*
2538 		 * Mark the first roothub as being USB 2.0.
2539 		 * The USB 3.0 roothub will be registered later by
2540 		 * dummy_hcd_probe()
2541 		 */
2542 		hcd->speed = HCD_USB2;
2543 		hcd->self.root_hub->speed = USB_SPEED_HIGH;
2544 	} else {
2545 		dum->ss_hcd = hcd_to_dummy_hcd(hcd);
2546 		dum->ss_hcd->dum = dum;
2547 		hcd->speed = HCD_USB3;
2548 		hcd->self.root_hub->speed = USB_SPEED_SUPER;
2549 	}
2550 	return 0;
2551 }
2552 
2553 /* Change a group of bulk endpoints to support multiple stream IDs */
2554 static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
2555 	struct usb_host_endpoint **eps, unsigned int num_eps,
2556 	unsigned int num_streams, gfp_t mem_flags)
2557 {
2558 	struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2559 	unsigned long flags;
2560 	int max_stream;
2561 	int ret_streams = num_streams;
2562 	unsigned int index;
2563 	unsigned int i;
2564 
2565 	if (!num_eps)
2566 		return -EINVAL;
2567 
2568 	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2569 	for (i = 0; i < num_eps; i++) {
2570 		index = dummy_get_ep_idx(&eps[i]->desc);
2571 		if ((1 << index) & dum_hcd->stream_en_ep) {
2572 			ret_streams = -EINVAL;
2573 			goto out;
2574 		}
2575 		max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp);
2576 		if (!max_stream) {
2577 			ret_streams = -EINVAL;
2578 			goto out;
2579 		}
2580 		if (max_stream < ret_streams) {
2581 			dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u "
2582 					"stream IDs.\n",
2583 					eps[i]->desc.bEndpointAddress,
2584 					max_stream);
2585 			ret_streams = max_stream;
2586 		}
2587 	}
2588 
2589 	for (i = 0; i < num_eps; i++) {
2590 		index = dummy_get_ep_idx(&eps[i]->desc);
2591 		dum_hcd->stream_en_ep |= 1 << index;
2592 		set_max_streams_for_pipe(dum_hcd,
2593 				usb_endpoint_num(&eps[i]->desc), ret_streams);
2594 	}
2595 out:
2596 	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2597 	return ret_streams;
2598 }
2599 
2600 /* Reverts a group of bulk endpoints back to not using stream IDs. */
2601 static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
2602 	struct usb_host_endpoint **eps, unsigned int num_eps,
2603 	gfp_t mem_flags)
2604 {
2605 	struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2606 	unsigned long flags;
2607 	int ret;
2608 	unsigned int index;
2609 	unsigned int i;
2610 
2611 	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2612 	for (i = 0; i < num_eps; i++) {
2613 		index = dummy_get_ep_idx(&eps[i]->desc);
2614 		if (!((1 << index) & dum_hcd->stream_en_ep)) {
2615 			ret = -EINVAL;
2616 			goto out;
2617 		}
2618 	}
2619 
2620 	for (i = 0; i < num_eps; i++) {
2621 		index = dummy_get_ep_idx(&eps[i]->desc);
2622 		dum_hcd->stream_en_ep &= ~(1 << index);
2623 		set_max_streams_for_pipe(dum_hcd,
2624 				usb_endpoint_num(&eps[i]->desc), 0);
2625 	}
2626 	ret = 0;
2627 out:
2628 	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2629 	return ret;
2630 }
2631 
2632 static struct hc_driver dummy_hcd = {
2633 	.description =		(char *) driver_name,
2634 	.product_desc =		"Dummy host controller",
2635 	.hcd_priv_size =	sizeof(struct dummy_hcd),
2636 
2637 	.reset =		dummy_setup,
2638 	.start =		dummy_start,
2639 	.stop =			dummy_stop,
2640 
2641 	.urb_enqueue =		dummy_urb_enqueue,
2642 	.urb_dequeue =		dummy_urb_dequeue,
2643 
2644 	.get_frame_number =	dummy_h_get_frame,
2645 
2646 	.hub_status_data =	dummy_hub_status,
2647 	.hub_control =		dummy_hub_control,
2648 	.bus_suspend =		dummy_bus_suspend,
2649 	.bus_resume =		dummy_bus_resume,
2650 
2651 	.alloc_streams =	dummy_alloc_streams,
2652 	.free_streams =		dummy_free_streams,
2653 };
2654 
2655 static int dummy_hcd_probe(struct platform_device *pdev)
2656 {
2657 	struct dummy		*dum;
2658 	struct usb_hcd		*hs_hcd;
2659 	struct usb_hcd		*ss_hcd;
2660 	int			retval;
2661 
2662 	dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
2663 	dum = *((void **)dev_get_platdata(&pdev->dev));
2664 
2665 	if (mod_data.is_super_speed)
2666 		dummy_hcd.flags = HCD_USB3 | HCD_SHARED;
2667 	else if (mod_data.is_high_speed)
2668 		dummy_hcd.flags = HCD_USB2;
2669 	else
2670 		dummy_hcd.flags = HCD_USB11;
2671 	hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2672 	if (!hs_hcd)
2673 		return -ENOMEM;
2674 	hs_hcd->has_tt = 1;
2675 
2676 	retval = usb_add_hcd(hs_hcd, 0, 0);
2677 	if (retval)
2678 		goto put_usb2_hcd;
2679 
2680 	if (mod_data.is_super_speed) {
2681 		ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2682 					dev_name(&pdev->dev), hs_hcd);
2683 		if (!ss_hcd) {
2684 			retval = -ENOMEM;
2685 			goto dealloc_usb2_hcd;
2686 		}
2687 
2688 		retval = usb_add_hcd(ss_hcd, 0, 0);
2689 		if (retval)
2690 			goto put_usb3_hcd;
2691 	}
2692 	return 0;
2693 
2694 put_usb3_hcd:
2695 	usb_put_hcd(ss_hcd);
2696 dealloc_usb2_hcd:
2697 	usb_remove_hcd(hs_hcd);
2698 put_usb2_hcd:
2699 	usb_put_hcd(hs_hcd);
2700 	dum->hs_hcd = dum->ss_hcd = NULL;
2701 	return retval;
2702 }
2703 
2704 static int dummy_hcd_remove(struct platform_device *pdev)
2705 {
2706 	struct dummy		*dum;
2707 
2708 	dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum;
2709 
2710 	if (dum->ss_hcd) {
2711 		usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2712 		usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2713 	}
2714 
2715 	usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2716 	usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2717 
2718 	dum->hs_hcd = NULL;
2719 	dum->ss_hcd = NULL;
2720 
2721 	return 0;
2722 }
2723 
2724 static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state)
2725 {
2726 	struct usb_hcd		*hcd;
2727 	struct dummy_hcd	*dum_hcd;
2728 	int			rc = 0;
2729 
2730 	dev_dbg(&pdev->dev, "%s\n", __func__);
2731 
2732 	hcd = platform_get_drvdata(pdev);
2733 	dum_hcd = hcd_to_dummy_hcd(hcd);
2734 	if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
2735 		dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2736 		rc = -EBUSY;
2737 	} else
2738 		clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2739 	return rc;
2740 }
2741 
2742 static int dummy_hcd_resume(struct platform_device *pdev)
2743 {
2744 	struct usb_hcd		*hcd;
2745 
2746 	dev_dbg(&pdev->dev, "%s\n", __func__);
2747 
2748 	hcd = platform_get_drvdata(pdev);
2749 	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2750 	usb_hcd_poll_rh_status(hcd);
2751 	return 0;
2752 }
2753 
2754 static struct platform_driver dummy_hcd_driver = {
2755 	.probe		= dummy_hcd_probe,
2756 	.remove		= dummy_hcd_remove,
2757 	.suspend	= dummy_hcd_suspend,
2758 	.resume		= dummy_hcd_resume,
2759 	.driver		= {
2760 		.name	= driver_name,
2761 	},
2762 };
2763 
2764 /*-------------------------------------------------------------------------*/
2765 #define MAX_NUM_UDC	32
2766 static struct platform_device *the_udc_pdev[MAX_NUM_UDC];
2767 static struct platform_device *the_hcd_pdev[MAX_NUM_UDC];
2768 
2769 static int __init dummy_hcd_init(void)
2770 {
2771 	int	retval = -ENOMEM;
2772 	int	i;
2773 	struct	dummy *dum[MAX_NUM_UDC] = {};
2774 
2775 	if (usb_disabled())
2776 		return -ENODEV;
2777 
2778 	if (!mod_data.is_high_speed && mod_data.is_super_speed)
2779 		return -EINVAL;
2780 
2781 	if (mod_data.num < 1 || mod_data.num > MAX_NUM_UDC) {
2782 		pr_err("Number of emulated UDC must be in range of 1...%d\n",
2783 				MAX_NUM_UDC);
2784 		return -EINVAL;
2785 	}
2786 
2787 	for (i = 0; i < mod_data.num; i++) {
2788 		the_hcd_pdev[i] = platform_device_alloc(driver_name, i);
2789 		if (!the_hcd_pdev[i]) {
2790 			i--;
2791 			while (i >= 0)
2792 				platform_device_put(the_hcd_pdev[i--]);
2793 			return retval;
2794 		}
2795 	}
2796 	for (i = 0; i < mod_data.num; i++) {
2797 		the_udc_pdev[i] = platform_device_alloc(gadget_name, i);
2798 		if (!the_udc_pdev[i]) {
2799 			i--;
2800 			while (i >= 0)
2801 				platform_device_put(the_udc_pdev[i--]);
2802 			goto err_alloc_udc;
2803 		}
2804 	}
2805 	for (i = 0; i < mod_data.num; i++) {
2806 		dum[i] = kzalloc(sizeof(struct dummy), GFP_KERNEL);
2807 		if (!dum[i]) {
2808 			retval = -ENOMEM;
2809 			goto err_add_pdata;
2810 		}
2811 		retval = platform_device_add_data(the_hcd_pdev[i], &dum[i],
2812 				sizeof(void *));
2813 		if (retval)
2814 			goto err_add_pdata;
2815 		retval = platform_device_add_data(the_udc_pdev[i], &dum[i],
2816 				sizeof(void *));
2817 		if (retval)
2818 			goto err_add_pdata;
2819 	}
2820 
2821 	retval = platform_driver_register(&dummy_hcd_driver);
2822 	if (retval < 0)
2823 		goto err_add_pdata;
2824 	retval = platform_driver_register(&dummy_udc_driver);
2825 	if (retval < 0)
2826 		goto err_register_udc_driver;
2827 
2828 	for (i = 0; i < mod_data.num; i++) {
2829 		retval = platform_device_add(the_hcd_pdev[i]);
2830 		if (retval < 0) {
2831 			i--;
2832 			while (i >= 0)
2833 				platform_device_del(the_hcd_pdev[i--]);
2834 			goto err_add_hcd;
2835 		}
2836 	}
2837 	for (i = 0; i < mod_data.num; i++) {
2838 		if (!dum[i]->hs_hcd ||
2839 				(!dum[i]->ss_hcd && mod_data.is_super_speed)) {
2840 			/*
2841 			 * The hcd was added successfully but its probe
2842 			 * function failed for some reason.
2843 			 */
2844 			retval = -EINVAL;
2845 			goto err_add_udc;
2846 		}
2847 	}
2848 
2849 	for (i = 0; i < mod_data.num; i++) {
2850 		retval = platform_device_add(the_udc_pdev[i]);
2851 		if (retval < 0) {
2852 			i--;
2853 			while (i >= 0)
2854 				platform_device_del(the_udc_pdev[i--]);
2855 			goto err_add_udc;
2856 		}
2857 	}
2858 
2859 	for (i = 0; i < mod_data.num; i++) {
2860 		if (!platform_get_drvdata(the_udc_pdev[i])) {
2861 			/*
2862 			 * The udc was added successfully but its probe
2863 			 * function failed for some reason.
2864 			 */
2865 			retval = -EINVAL;
2866 			goto err_probe_udc;
2867 		}
2868 	}
2869 	return retval;
2870 
2871 err_probe_udc:
2872 	for (i = 0; i < mod_data.num; i++)
2873 		platform_device_del(the_udc_pdev[i]);
2874 err_add_udc:
2875 	for (i = 0; i < mod_data.num; i++)
2876 		platform_device_del(the_hcd_pdev[i]);
2877 err_add_hcd:
2878 	platform_driver_unregister(&dummy_udc_driver);
2879 err_register_udc_driver:
2880 	platform_driver_unregister(&dummy_hcd_driver);
2881 err_add_pdata:
2882 	for (i = 0; i < mod_data.num; i++)
2883 		kfree(dum[i]);
2884 	for (i = 0; i < mod_data.num; i++)
2885 		platform_device_put(the_udc_pdev[i]);
2886 err_alloc_udc:
2887 	for (i = 0; i < mod_data.num; i++)
2888 		platform_device_put(the_hcd_pdev[i]);
2889 	return retval;
2890 }
2891 module_init(dummy_hcd_init);
2892 
2893 static void __exit dummy_hcd_cleanup(void)
2894 {
2895 	int i;
2896 
2897 	for (i = 0; i < mod_data.num; i++) {
2898 		struct dummy *dum;
2899 
2900 		dum = *((void **)dev_get_platdata(&the_udc_pdev[i]->dev));
2901 
2902 		platform_device_unregister(the_udc_pdev[i]);
2903 		platform_device_unregister(the_hcd_pdev[i]);
2904 		kfree(dum);
2905 	}
2906 	platform_driver_unregister(&dummy_udc_driver);
2907 	platform_driver_unregister(&dummy_hcd_driver);
2908 }
2909 module_exit(dummy_hcd_cleanup);
2910