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