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