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