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