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