1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
53
54static const char driver_name[] = "dummy_hcd";
55static const char driver_desc[] = "USB Host+Gadget Emulator";
56
57static const char gadget_name[] = "dummy_udc";
58
59MODULE_DESCRIPTION(DRIVER_DESC);
60MODULE_AUTHOR("David Brownell");
61MODULE_LICENSE("GPL");
62
63struct dummy_hcd_module_parameters {
64 bool is_super_speed;
65 bool is_high_speed;
66 unsigned int num;
67};
68
69static struct dummy_hcd_module_parameters mod_data = {
70 .is_super_speed = false,
71 .is_high_speed = true,
72 .num = 1,
73};
74module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
75MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
76module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
77MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
78module_param_named(num, mod_data.num, uint, S_IRUGO);
79MODULE_PARM_DESC(num, "number of emulated controllers");
80
81
82
83struct dummy_ep {
84 struct list_head queue;
85 unsigned long last_io;
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
96struct dummy_request {
97 struct list_head queue;
98 struct usb_request req;
99};
100
101static 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
106static 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
116
117
118
119
120
121
122
123
124
125
126
127
128static const char ep0name[] = "ep0";
129
130static const char *const ep_name[] = {
131 ep0name,
132
133
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
140 "ep1out-bulk", "ep2in-bulk",
141
142
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
152struct urbp {
153 struct urb *urb;
154 struct list_head urbp_list;
155 struct sg_mapping_iter miter;
156 u32 miter_started;
157};
158
159
160enum dummy_rh_state {
161 DUMMY_RH_RESET,
162 DUMMY_RH_SUSPENDED,
163 DUMMY_RH_RUNNING
164};
165
166struct 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
184struct dummy {
185 spinlock_t lock;
186
187
188
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
202
203 struct dummy_hcd *hs_hcd;
204 struct dummy_hcd *ss_hcd;
205};
206
207static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
208{
209 return (struct dummy_hcd *) (hcd->hcd_priv);
210}
211
212static 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
217static inline struct device *dummy_dev(struct dummy_hcd *dum)
218{
219 return dummy_hcd_to_hcd(dum)->self.controller;
220}
221
222static inline struct device *udc_dev(struct dummy *dum)
223{
224 return dum->gadget.dev.parent;
225}
226
227static inline struct dummy *ep_to_dummy(struct dummy_ep *ep)
228{
229 return container_of(ep->gadget, struct dummy, gadget);
230}
231
232static 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
241static 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
249
250
251static 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
267static void stop_activity(struct dummy *dum)
268{
269 struct dummy_ep *ep;
270
271
272 dum->address = 0;
273
274
275
276
277 list_for_each_entry(ep, &dum->gadget.ep_list, ep.ep_list)
278 nuke(dum, ep);
279
280
281}
282
283
284
285
286
287
288
289
290
291static 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
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
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
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
352static 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
371 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
372 (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
373
374
375
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
404
405
406
407
408
409
410#define is_enabled(dum) \
411 (dum->port_status & USB_PORT_STAT_ENABLE)
412
413static 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
436
437
438
439 max = usb_endpoint_maxp(desc) & 0x7ff;
440
441
442
443
444
445
446
447
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
468 break;
469
470 default:
471 goto done;
472 }
473 break;
474 case USB_ENDPOINT_XFER_INT:
475 if (strstr(ep->ep.name, "-iso"))
476 goto done;
477
478 switch (dum->gadget.speed) {
479 case USB_SPEED_SUPER:
480 case USB_SPEED_HIGH:
481 if (max <= 1024)
482 break;
483
484 case USB_SPEED_FULL:
485 if (max <= 64)
486 break;
487
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
499 switch (dum->gadget.speed) {
500 case USB_SPEED_SUPER:
501 case USB_SPEED_HIGH:
502 if (max <= 1024)
503 break;
504
505 case USB_SPEED_FULL:
506 if (max <= 1023)
507 break;
508
509 default:
510 goto done;
511 }
512 break;
513 default:
514
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
551
552
553 ep->halted = ep->wedged = 0;
554 retval = 0;
555done:
556 return retval;
557}
558
559static int dummy_disable(struct usb_ep *_ep)
560{
561 struct dummy_ep *ep;
562 struct dummy *dum;
563 unsigned long flags;
564
565 ep = usb_ep_to_dummy_ep(_ep);
566 if (!_ep || !ep->desc || _ep->name == ep0name)
567 return -EINVAL;
568 dum = ep_to_dummy(ep);
569
570 spin_lock_irqsave(&dum->lock, flags);
571 ep->desc = NULL;
572 ep->stream_en = 0;
573 nuke(dum, ep);
574 spin_unlock_irqrestore(&dum->lock, flags);
575
576 dev_dbg(udc_dev(dum), "disabled %s\n", _ep->name);
577 return 0;
578}
579
580static struct usb_request *dummy_alloc_request(struct usb_ep *_ep,
581 gfp_t mem_flags)
582{
583 struct dummy_ep *ep;
584 struct dummy_request *req;
585
586 if (!_ep)
587 return NULL;
588 ep = usb_ep_to_dummy_ep(_ep);
589
590 req = kzalloc(sizeof(*req), mem_flags);
591 if (!req)
592 return NULL;
593 INIT_LIST_HEAD(&req->queue);
594 return &req->req;
595}
596
597static void dummy_free_request(struct usb_ep *_ep, struct usb_request *_req)
598{
599 struct dummy_request *req;
600
601 if (!_ep || !_req) {
602 WARN_ON(1);
603 return;
604 }
605
606 req = usb_request_to_dummy_request(_req);
607 WARN_ON(!list_empty(&req->queue));
608 kfree(req);
609}
610
611static void fifo_complete(struct usb_ep *ep, struct usb_request *req)
612{
613}
614
615static int dummy_queue(struct usb_ep *_ep, struct usb_request *_req,
616 gfp_t mem_flags)
617{
618 struct dummy_ep *ep;
619 struct dummy_request *req;
620 struct dummy *dum;
621 struct dummy_hcd *dum_hcd;
622 unsigned long flags;
623
624 req = usb_request_to_dummy_request(_req);
625 if (!_req || !list_empty(&req->queue) || !_req->complete)
626 return -EINVAL;
627
628 ep = usb_ep_to_dummy_ep(_ep);
629 if (!_ep || (!ep->desc && _ep->name != ep0name))
630 return -EINVAL;
631
632 dum = ep_to_dummy(ep);
633 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
634 if (!dum->driver || !is_enabled(dum_hcd))
635 return -ESHUTDOWN;
636
637#if 0
638 dev_dbg(udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
639 ep, _req, _ep->name, _req->length, _req->buf);
640#endif
641 _req->status = -EINPROGRESS;
642 _req->actual = 0;
643 spin_lock_irqsave(&dum->lock, flags);
644
645
646 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
647 list_empty(&dum->fifo_req.queue) &&
648 list_empty(&ep->queue) &&
649 _req->length <= FIFO_SIZE) {
650 req = &dum->fifo_req;
651 req->req = *_req;
652 req->req.buf = dum->fifo_buf;
653 memcpy(dum->fifo_buf, _req->buf, _req->length);
654 req->req.context = dum;
655 req->req.complete = fifo_complete;
656
657 list_add_tail(&req->queue, &ep->queue);
658 spin_unlock(&dum->lock);
659 _req->actual = _req->length;
660 _req->status = 0;
661 _req->complete(_ep, _req);
662 spin_lock(&dum->lock);
663 } else
664 list_add_tail(&req->queue, &ep->queue);
665 spin_unlock_irqrestore(&dum->lock, flags);
666
667
668
669
670 return 0;
671}
672
673static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req)
674{
675 struct dummy_ep *ep;
676 struct dummy *dum;
677 int retval = -EINVAL;
678 unsigned long flags;
679 struct dummy_request *req = NULL;
680
681 if (!_ep || !_req)
682 return retval;
683 ep = usb_ep_to_dummy_ep(_ep);
684 dum = ep_to_dummy(ep);
685
686 if (!dum->driver)
687 return -ESHUTDOWN;
688
689 local_irq_save(flags);
690 spin_lock(&dum->lock);
691 list_for_each_entry(req, &ep->queue, queue) {
692 if (&req->req == _req) {
693 list_del_init(&req->queue);
694 _req->status = -ECONNRESET;
695 retval = 0;
696 break;
697 }
698 }
699 spin_unlock(&dum->lock);
700
701 if (retval == 0) {
702 dev_dbg(udc_dev(dum),
703 "dequeued req %p from %s, len %d buf %p\n",
704 req, _ep->name, _req->length, _req->buf);
705 _req->complete(_ep, _req);
706 }
707 local_irq_restore(flags);
708 return retval;
709}
710
711static int
712dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
713{
714 struct dummy_ep *ep;
715 struct dummy *dum;
716
717 if (!_ep)
718 return -EINVAL;
719 ep = usb_ep_to_dummy_ep(_ep);
720 dum = ep_to_dummy(ep);
721 if (!dum->driver)
722 return -ESHUTDOWN;
723 if (!value)
724 ep->halted = ep->wedged = 0;
725 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
726 !list_empty(&ep->queue))
727 return -EAGAIN;
728 else {
729 ep->halted = 1;
730 if (wedged)
731 ep->wedged = 1;
732 }
733
734 return 0;
735}
736
737static int
738dummy_set_halt(struct usb_ep *_ep, int value)
739{
740 return dummy_set_halt_and_wedge(_ep, value, 0);
741}
742
743static int dummy_set_wedge(struct usb_ep *_ep)
744{
745 if (!_ep || _ep->name == ep0name)
746 return -EINVAL;
747 return dummy_set_halt_and_wedge(_ep, 1, 1);
748}
749
750static const struct usb_ep_ops dummy_ep_ops = {
751 .enable = dummy_enable,
752 .disable = dummy_disable,
753
754 .alloc_request = dummy_alloc_request,
755 .free_request = dummy_free_request,
756
757 .queue = dummy_queue,
758 .dequeue = dummy_dequeue,
759
760 .set_halt = dummy_set_halt,
761 .set_wedge = dummy_set_wedge,
762};
763
764
765
766
767static int dummy_g_get_frame(struct usb_gadget *_gadget)
768{
769 struct timeval tv;
770
771 do_gettimeofday(&tv);
772 return tv.tv_usec / 1000;
773}
774
775static int dummy_wakeup(struct usb_gadget *_gadget)
776{
777 struct dummy_hcd *dum_hcd;
778
779 dum_hcd = gadget_to_dummy_hcd(_gadget);
780 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
781 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
782 return -EINVAL;
783 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
784 return -ENOLINK;
785 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
786 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
787 return -EIO;
788
789
790
791
792 dum_hcd->resuming = 1;
793 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
794 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
795 return 0;
796}
797
798static int dummy_set_selfpowered(struct usb_gadget *_gadget, int value)
799{
800 struct dummy *dum;
801
802 dum = gadget_to_dummy_hcd(_gadget)->dum;
803 if (value)
804 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
805 else
806 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
807 return 0;
808}
809
810static void dummy_udc_update_ep0(struct dummy *dum)
811{
812 if (dum->gadget.speed == USB_SPEED_SUPER)
813 dum->ep[0].ep.maxpacket = 9;
814 else
815 dum->ep[0].ep.maxpacket = 64;
816}
817
818static int dummy_pullup(struct usb_gadget *_gadget, int value)
819{
820 struct dummy_hcd *dum_hcd;
821 struct dummy *dum;
822 unsigned long flags;
823
824 dum = gadget_dev_to_dummy(&_gadget->dev);
825
826 if (value && dum->driver) {
827 if (mod_data.is_super_speed)
828 dum->gadget.speed = dum->driver->max_speed;
829 else if (mod_data.is_high_speed)
830 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
831 dum->driver->max_speed);
832 else
833 dum->gadget.speed = USB_SPEED_FULL;
834 dummy_udc_update_ep0(dum);
835
836 if (dum->gadget.speed < dum->driver->max_speed)
837 dev_dbg(udc_dev(dum), "This device can perform faster"
838 " if you connect it to a %s port...\n",
839 usb_speed_string(dum->driver->max_speed));
840 }
841 dum_hcd = gadget_to_dummy_hcd(_gadget);
842
843 spin_lock_irqsave(&dum->lock, flags);
844 dum->pullup = (value != 0);
845 set_link_state(dum_hcd);
846 spin_unlock_irqrestore(&dum->lock, flags);
847
848 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
849 return 0;
850}
851
852static int dummy_udc_start(struct usb_gadget *g,
853 struct usb_gadget_driver *driver);
854static int dummy_udc_stop(struct usb_gadget *g,
855 struct usb_gadget_driver *driver);
856
857static const struct usb_gadget_ops dummy_ops = {
858 .get_frame = dummy_g_get_frame,
859 .wakeup = dummy_wakeup,
860 .set_selfpowered = dummy_set_selfpowered,
861 .pullup = dummy_pullup,
862 .udc_start = dummy_udc_start,
863 .udc_stop = dummy_udc_stop,
864};
865
866
867
868
869static ssize_t function_show(struct device *dev, struct device_attribute *attr,
870 char *buf)
871{
872 struct dummy *dum = gadget_dev_to_dummy(dev);
873
874 if (!dum->driver || !dum->driver->function)
875 return 0;
876 return scnprintf(buf, PAGE_SIZE, "%s\n", dum->driver->function);
877}
878static DEVICE_ATTR_RO(function);
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896static int dummy_udc_start(struct usb_gadget *g,
897 struct usb_gadget_driver *driver)
898{
899 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
900 struct dummy *dum = dum_hcd->dum;
901
902 if (driver->max_speed == USB_SPEED_UNKNOWN)
903 return -EINVAL;
904
905
906
907
908
909
910 dum->devstatus = 0;
911
912 dum->driver = driver;
913 dev_dbg(udc_dev(dum), "binding gadget driver '%s'\n",
914 driver->driver.name);
915 return 0;
916}
917
918static int dummy_udc_stop(struct usb_gadget *g,
919 struct usb_gadget_driver *driver)
920{
921 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
922 struct dummy *dum = dum_hcd->dum;
923
924 if (driver)
925 dev_dbg(udc_dev(dum), "unregister gadget driver '%s'\n",
926 driver->driver.name);
927
928 dum->driver = NULL;
929
930 return 0;
931}
932
933#undef is_enabled
934
935
936
937static void init_dummy_udc_hw(struct dummy *dum)
938{
939 int i;
940
941 INIT_LIST_HEAD(&dum->gadget.ep_list);
942 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
943 struct dummy_ep *ep = &dum->ep[i];
944
945 if (!ep_name[i])
946 break;
947 ep->ep.name = ep_name[i];
948 ep->ep.ops = &dummy_ep_ops;
949 list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
950 ep->halted = ep->wedged = ep->already_seen =
951 ep->setup_stage = 0;
952 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
953 ep->ep.max_streams = 16;
954 ep->last_io = jiffies;
955 ep->gadget = &dum->gadget;
956 ep->desc = NULL;
957 INIT_LIST_HEAD(&ep->queue);
958 }
959
960 dum->gadget.ep0 = &dum->ep[0].ep;
961 list_del_init(&dum->ep[0].ep.ep_list);
962 INIT_LIST_HEAD(&dum->fifo_req.queue);
963
964#ifdef CONFIG_USB_OTG
965 dum->gadget.is_otg = 1;
966#endif
967}
968
969static int dummy_udc_probe(struct platform_device *pdev)
970{
971 struct dummy *dum;
972 int rc;
973
974 dum = *((void **)dev_get_platdata(&pdev->dev));
975 dum->gadget.name = gadget_name;
976 dum->gadget.ops = &dummy_ops;
977 dum->gadget.max_speed = USB_SPEED_SUPER;
978
979 dum->gadget.dev.parent = &pdev->dev;
980 init_dummy_udc_hw(dum);
981
982 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
983 if (rc < 0)
984 goto err_udc;
985
986 rc = device_create_file(&dum->gadget.dev, &dev_attr_function);
987 if (rc < 0)
988 goto err_dev;
989 platform_set_drvdata(pdev, dum);
990 return rc;
991
992err_dev:
993 usb_del_gadget_udc(&dum->gadget);
994err_udc:
995 return rc;
996}
997
998static int dummy_udc_remove(struct platform_device *pdev)
999{
1000 struct dummy *dum = platform_get_drvdata(pdev);
1001
1002 device_remove_file(&dum->gadget.dev, &dev_attr_function);
1003 usb_del_gadget_udc(&dum->gadget);
1004 return 0;
1005}
1006
1007static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1008 int suspend)
1009{
1010 spin_lock_irq(&dum->lock);
1011 dum->udc_suspended = suspend;
1012 set_link_state(dum_hcd);
1013 spin_unlock_irq(&dum->lock);
1014}
1015
1016static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1017{
1018 struct dummy *dum = platform_get_drvdata(pdev);
1019 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1020
1021 dev_dbg(&pdev->dev, "%s\n", __func__);
1022 dummy_udc_pm(dum, dum_hcd, 1);
1023 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
1024 return 0;
1025}
1026
1027static int dummy_udc_resume(struct platform_device *pdev)
1028{
1029 struct dummy *dum = platform_get_drvdata(pdev);
1030 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1031
1032 dev_dbg(&pdev->dev, "%s\n", __func__);
1033 dummy_udc_pm(dum, dum_hcd, 0);
1034 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
1035 return 0;
1036}
1037
1038static struct platform_driver dummy_udc_driver = {
1039 .probe = dummy_udc_probe,
1040 .remove = dummy_udc_remove,
1041 .suspend = dummy_udc_suspend,
1042 .resume = dummy_udc_resume,
1043 .driver = {
1044 .name = (char *) gadget_name,
1045 .owner = THIS_MODULE,
1046 },
1047};
1048
1049
1050
1051static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc)
1052{
1053 unsigned int index;
1054
1055 index = usb_endpoint_num(desc) << 1;
1056 if (usb_endpoint_dir_in(desc))
1057 index |= 1;
1058 return index;
1059}
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb)
1074{
1075 const struct usb_endpoint_descriptor *desc = &urb->ep->desc;
1076 u32 index;
1077
1078 if (!usb_endpoint_xfer_bulk(desc))
1079 return 0;
1080
1081 index = dummy_get_ep_idx(desc);
1082 return (1 << index) & dum_hcd->stream_en_ep;
1083}
1084
1085
1086
1087
1088
1089
1090
1091
1092static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1093 unsigned int pipe)
1094{
1095 int max_streams;
1096
1097 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1098 if (usb_pipeout(pipe))
1099 max_streams >>= 4;
1100 else
1101 max_streams &= 0xf;
1102 max_streams++;
1103 return max_streams;
1104}
1105
1106static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1107 unsigned int pipe, unsigned int streams)
1108{
1109 int max_streams;
1110
1111 streams--;
1112 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1113 if (usb_pipeout(pipe)) {
1114 streams <<= 4;
1115 max_streams &= 0xf;
1116 } else {
1117 max_streams &= 0xf0;
1118 }
1119 max_streams |= streams;
1120 dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams;
1121}
1122
1123static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb)
1124{
1125 unsigned int max_streams;
1126 int enabled;
1127
1128 enabled = dummy_ep_stream_en(dum_hcd, urb);
1129 if (!urb->stream_id) {
1130 if (enabled)
1131 return -EINVAL;
1132 return 0;
1133 }
1134 if (!enabled)
1135 return -EINVAL;
1136
1137 max_streams = get_max_streams_for_pipe(dum_hcd,
1138 usb_pipeendpoint(urb->pipe));
1139 if (urb->stream_id > max_streams) {
1140 dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n",
1141 urb->stream_id);
1142 BUG();
1143 return -EINVAL;
1144 }
1145 return 0;
1146}
1147
1148static int dummy_urb_enqueue(
1149 struct usb_hcd *hcd,
1150 struct urb *urb,
1151 gfp_t mem_flags
1152) {
1153 struct dummy_hcd *dum_hcd;
1154 struct urbp *urbp;
1155 unsigned long flags;
1156 int rc;
1157
1158 urbp = kmalloc(sizeof *urbp, mem_flags);
1159 if (!urbp)
1160 return -ENOMEM;
1161 urbp->urb = urb;
1162 urbp->miter_started = 0;
1163
1164 dum_hcd = hcd_to_dummy_hcd(hcd);
1165 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1166
1167 rc = dummy_validate_stream(dum_hcd, urb);
1168 if (rc) {
1169 kfree(urbp);
1170 goto done;
1171 }
1172
1173 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1174 if (rc) {
1175 kfree(urbp);
1176 goto done;
1177 }
1178
1179 if (!dum_hcd->udev) {
1180 dum_hcd->udev = urb->dev;
1181 usb_get_dev(dum_hcd->udev);
1182 } else if (unlikely(dum_hcd->udev != urb->dev))
1183 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
1184
1185 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
1186 urb->hcpriv = urbp;
1187 if (usb_pipetype(urb->pipe) == PIPE_CONTROL)
1188 urb->error_count = 1;
1189
1190
1191 if (!timer_pending(&dum_hcd->timer))
1192 mod_timer(&dum_hcd->timer, jiffies + 1);
1193
1194 done:
1195 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1196 return rc;
1197}
1198
1199static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1200{
1201 struct dummy_hcd *dum_hcd;
1202 unsigned long flags;
1203 int rc;
1204
1205
1206
1207 dum_hcd = hcd_to_dummy_hcd(hcd);
1208 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1209
1210 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1211 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1212 !list_empty(&dum_hcd->urbp_list))
1213 mod_timer(&dum_hcd->timer, jiffies);
1214
1215 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1216 return rc;
1217}
1218
1219static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
1220 u32 len)
1221{
1222 void *ubuf, *rbuf;
1223 struct urbp *urbp = urb->hcpriv;
1224 int to_host;
1225 struct sg_mapping_iter *miter = &urbp->miter;
1226 u32 trans = 0;
1227 u32 this_sg;
1228 bool next_sg;
1229
1230 to_host = usb_pipein(urb->pipe);
1231 rbuf = req->req.buf + req->req.actual;
1232
1233 if (!urb->num_sgs) {
1234 ubuf = urb->transfer_buffer + urb->actual_length;
1235 if (to_host)
1236 memcpy(ubuf, rbuf, len);
1237 else
1238 memcpy(rbuf, ubuf, len);
1239 return len;
1240 }
1241
1242 if (!urbp->miter_started) {
1243 u32 flags = SG_MITER_ATOMIC;
1244
1245 if (to_host)
1246 flags |= SG_MITER_TO_SG;
1247 else
1248 flags |= SG_MITER_FROM_SG;
1249
1250 sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
1251 urbp->miter_started = 1;
1252 }
1253 next_sg = sg_miter_next(miter);
1254 if (next_sg == false) {
1255 WARN_ON_ONCE(1);
1256 return -EINVAL;
1257 }
1258 do {
1259 ubuf = miter->addr;
1260 this_sg = min_t(u32, len, miter->length);
1261 miter->consumed = this_sg;
1262 trans += this_sg;
1263
1264 if (to_host)
1265 memcpy(ubuf, rbuf, this_sg);
1266 else
1267 memcpy(rbuf, ubuf, this_sg);
1268 len -= this_sg;
1269
1270 if (!len)
1271 break;
1272 next_sg = sg_miter_next(miter);
1273 if (next_sg == false) {
1274 WARN_ON_ONCE(1);
1275 return -EINVAL;
1276 }
1277
1278 rbuf += this_sg;
1279 } while (1);
1280
1281 sg_miter_stop(miter);
1282 return trans;
1283}
1284
1285
1286static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb,
1287 struct dummy_ep *ep, int limit, int *status)
1288{
1289 struct dummy *dum = dum_hcd->dum;
1290 struct dummy_request *req;
1291
1292top:
1293
1294 list_for_each_entry(req, &ep->queue, queue) {
1295 unsigned host_len, dev_len, len;
1296 int is_short, to_host;
1297 int rescan = 0;
1298
1299 if (dummy_ep_stream_en(dum_hcd, urb)) {
1300 if ((urb->stream_id != req->req.stream_id))
1301 continue;
1302 }
1303
1304
1305
1306
1307
1308
1309
1310
1311 host_len = urb->transfer_buffer_length - urb->actual_length;
1312 dev_len = req->req.length - req->req.actual;
1313 len = min(host_len, dev_len);
1314
1315
1316
1317 to_host = usb_pipein(urb->pipe);
1318 if (unlikely(len == 0))
1319 is_short = 1;
1320 else {
1321
1322 if (limit < ep->ep.maxpacket && limit < len)
1323 break;
1324 len = min_t(unsigned, len, limit);
1325 if (len == 0)
1326 break;
1327
1328
1329 if (len > ep->ep.maxpacket) {
1330 rescan = 1;
1331 len -= (len % ep->ep.maxpacket);
1332 }
1333 is_short = (len % ep->ep.maxpacket) != 0;
1334
1335 len = dummy_perform_transfer(urb, req, len);
1336
1337 ep->last_io = jiffies;
1338 if ((int)len < 0) {
1339 req->req.status = len;
1340 } else {
1341 limit -= len;
1342 urb->actual_length += len;
1343 req->req.actual += len;
1344 }
1345 }
1346
1347
1348
1349
1350
1351
1352
1353
1354 if (is_short) {
1355 if (host_len == dev_len) {
1356 req->req.status = 0;
1357 *status = 0;
1358 } else if (to_host) {
1359 req->req.status = 0;
1360 if (dev_len > host_len)
1361 *status = -EOVERFLOW;
1362 else
1363 *status = 0;
1364 } else if (!to_host) {
1365 *status = 0;
1366 if (host_len > dev_len)
1367 req->req.status = -EOVERFLOW;
1368 else
1369 req->req.status = 0;
1370 }
1371
1372
1373 } else {
1374 if (req->req.length == req->req.actual
1375 && !req->req.zero)
1376 req->req.status = 0;
1377 if (urb->transfer_buffer_length == urb->actual_length
1378 && !(urb->transfer_flags
1379 & URB_ZERO_PACKET))
1380 *status = 0;
1381 }
1382
1383
1384 if (req->req.status != -EINPROGRESS) {
1385 list_del_init(&req->queue);
1386
1387 spin_unlock(&dum->lock);
1388 req->req.complete(&ep->ep, &req->req);
1389 spin_lock(&dum->lock);
1390
1391
1392 rescan = 1;
1393 }
1394
1395
1396 if (*status != -EINPROGRESS)
1397 break;
1398
1399
1400 if (rescan)
1401 goto top;
1402 }
1403 return limit;
1404}
1405
1406static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep)
1407{
1408 int limit = ep->ep.maxpacket;
1409
1410 if (dum->gadget.speed == USB_SPEED_HIGH) {
1411 int tmp;
1412
1413
1414 tmp = usb_endpoint_maxp(ep->desc);
1415 tmp = (tmp >> 11) & 0x03;
1416 tmp *= 8 ;
1417 limit += limit * tmp;
1418 }
1419 if (dum->gadget.speed == USB_SPEED_SUPER) {
1420 switch (usb_endpoint_type(ep->desc)) {
1421 case USB_ENDPOINT_XFER_ISOC:
1422
1423 limit = 3 * 16 * 1024 * 8;
1424 break;
1425 case USB_ENDPOINT_XFER_INT:
1426
1427 limit = 3 * 1024 * 8;
1428 break;
1429 case USB_ENDPOINT_XFER_BULK:
1430 default:
1431 break;
1432 }
1433 }
1434 return limit;
1435}
1436
1437#define is_active(dum_hcd) ((dum_hcd->port_status & \
1438 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1439 USB_PORT_STAT_SUSPEND)) \
1440 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1441
1442static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address)
1443{
1444 int i;
1445
1446 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1447 dum->ss_hcd : dum->hs_hcd)))
1448 return NULL;
1449 if ((address & ~USB_DIR_IN) == 0)
1450 return &dum->ep[0];
1451 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1452 struct dummy_ep *ep = &dum->ep[i];
1453
1454 if (!ep->desc)
1455 continue;
1456 if (ep->desc->bEndpointAddress == address)
1457 return ep;
1458 }
1459 return NULL;
1460}
1461
1462#undef is_active
1463
1464#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1465#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1466#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1467#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1468#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1469#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
1485 struct usb_ctrlrequest *setup,
1486 int *status)
1487{
1488 struct dummy_ep *ep2;
1489 struct dummy *dum = dum_hcd->dum;
1490 int ret_val = 1;
1491 unsigned w_index;
1492 unsigned w_value;
1493
1494 w_index = le16_to_cpu(setup->wIndex);
1495 w_value = le16_to_cpu(setup->wValue);
1496 switch (setup->bRequest) {
1497 case USB_REQ_SET_ADDRESS:
1498 if (setup->bRequestType != Dev_Request)
1499 break;
1500 dum->address = w_value;
1501 *status = 0;
1502 dev_dbg(udc_dev(dum), "set_address = %d\n",
1503 w_value);
1504 ret_val = 0;
1505 break;
1506 case USB_REQ_SET_FEATURE:
1507 if (setup->bRequestType == Dev_Request) {
1508 ret_val = 0;
1509 switch (w_value) {
1510 case USB_DEVICE_REMOTE_WAKEUP:
1511 break;
1512 case USB_DEVICE_B_HNP_ENABLE:
1513 dum->gadget.b_hnp_enable = 1;
1514 break;
1515 case USB_DEVICE_A_HNP_SUPPORT:
1516 dum->gadget.a_hnp_support = 1;
1517 break;
1518 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1519 dum->gadget.a_alt_hnp_support = 1;
1520 break;
1521 case USB_DEVICE_U1_ENABLE:
1522 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1523 HCD_USB3)
1524 w_value = USB_DEV_STAT_U1_ENABLED;
1525 else
1526 ret_val = -EOPNOTSUPP;
1527 break;
1528 case USB_DEVICE_U2_ENABLE:
1529 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1530 HCD_USB3)
1531 w_value = USB_DEV_STAT_U2_ENABLED;
1532 else
1533 ret_val = -EOPNOTSUPP;
1534 break;
1535 case USB_DEVICE_LTM_ENABLE:
1536 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1537 HCD_USB3)
1538 w_value = USB_DEV_STAT_LTM_ENABLED;
1539 else
1540 ret_val = -EOPNOTSUPP;
1541 break;
1542 default:
1543 ret_val = -EOPNOTSUPP;
1544 }
1545 if (ret_val == 0) {
1546 dum->devstatus |= (1 << w_value);
1547 *status = 0;
1548 }
1549 } else if (setup->bRequestType == Ep_Request) {
1550
1551 ep2 = find_endpoint(dum, w_index);
1552 if (!ep2 || ep2->ep.name == ep0name) {
1553 ret_val = -EOPNOTSUPP;
1554 break;
1555 }
1556 ep2->halted = 1;
1557 ret_val = 0;
1558 *status = 0;
1559 }
1560 break;
1561 case USB_REQ_CLEAR_FEATURE:
1562 if (setup->bRequestType == Dev_Request) {
1563 ret_val = 0;
1564 switch (w_value) {
1565 case USB_DEVICE_REMOTE_WAKEUP:
1566 w_value = USB_DEVICE_REMOTE_WAKEUP;
1567 break;
1568 case USB_DEVICE_U1_ENABLE:
1569 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1570 HCD_USB3)
1571 w_value = USB_DEV_STAT_U1_ENABLED;
1572 else
1573 ret_val = -EOPNOTSUPP;
1574 break;
1575 case USB_DEVICE_U2_ENABLE:
1576 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1577 HCD_USB3)
1578 w_value = USB_DEV_STAT_U2_ENABLED;
1579 else
1580 ret_val = -EOPNOTSUPP;
1581 break;
1582 case USB_DEVICE_LTM_ENABLE:
1583 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1584 HCD_USB3)
1585 w_value = USB_DEV_STAT_LTM_ENABLED;
1586 else
1587 ret_val = -EOPNOTSUPP;
1588 break;
1589 default:
1590 ret_val = -EOPNOTSUPP;
1591 break;
1592 }
1593 if (ret_val == 0) {
1594 dum->devstatus &= ~(1 << w_value);
1595 *status = 0;
1596 }
1597 } else if (setup->bRequestType == Ep_Request) {
1598
1599 ep2 = find_endpoint(dum, w_index);
1600 if (!ep2) {
1601 ret_val = -EOPNOTSUPP;
1602 break;
1603 }
1604 if (!ep2->wedged)
1605 ep2->halted = 0;
1606 ret_val = 0;
1607 *status = 0;
1608 }
1609 break;
1610 case USB_REQ_GET_STATUS:
1611 if (setup->bRequestType == Dev_InRequest
1612 || setup->bRequestType == Intf_InRequest
1613 || setup->bRequestType == Ep_InRequest) {
1614 char *buf;
1615
1616
1617
1618
1619
1620 buf = (char *)urb->transfer_buffer;
1621 if (urb->transfer_buffer_length > 0) {
1622 if (setup->bRequestType == Ep_InRequest) {
1623 ep2 = find_endpoint(dum, w_index);
1624 if (!ep2) {
1625 ret_val = -EOPNOTSUPP;
1626 break;
1627 }
1628 buf[0] = ep2->halted;
1629 } else if (setup->bRequestType ==
1630 Dev_InRequest) {
1631 buf[0] = (u8)dum->devstatus;
1632 } else
1633 buf[0] = 0;
1634 }
1635 if (urb->transfer_buffer_length > 1)
1636 buf[1] = 0;
1637 urb->actual_length = min_t(u32, 2,
1638 urb->transfer_buffer_length);
1639 ret_val = 0;
1640 *status = 0;
1641 }
1642 break;
1643 }
1644 return ret_val;
1645}
1646
1647
1648
1649
1650static void dummy_timer(unsigned long _dum_hcd)
1651{
1652 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1653 struct dummy *dum = dum_hcd->dum;
1654 struct urbp *urbp, *tmp;
1655 unsigned long flags;
1656 int limit, total;
1657 int i;
1658
1659
1660 switch (dum->gadget.speed) {
1661 case USB_SPEED_LOW:
1662 total = 8 * 12;
1663 break;
1664 case USB_SPEED_FULL:
1665 total = 64 * 19;
1666 break;
1667 case USB_SPEED_HIGH:
1668 total = 512 * 13 * 8;
1669 break;
1670 case USB_SPEED_SUPER:
1671
1672 total = 490000;
1673 break;
1674 default:
1675 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
1676 return;
1677 }
1678
1679
1680
1681
1682 spin_lock_irqsave(&dum->lock, flags);
1683
1684 if (!dum_hcd->udev) {
1685 dev_err(dummy_dev(dum_hcd),
1686 "timer fired with no URBs pending?\n");
1687 spin_unlock_irqrestore(&dum->lock, flags);
1688 return;
1689 }
1690
1691 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1692 if (!ep_name[i])
1693 break;
1694 dum->ep[i].already_seen = 0;
1695 }
1696
1697restart:
1698 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
1699 struct urb *urb;
1700 struct dummy_request *req;
1701 u8 address;
1702 struct dummy_ep *ep = NULL;
1703 int type;
1704 int status = -EINPROGRESS;
1705
1706 urb = urbp->urb;
1707 if (urb->unlinked)
1708 goto return_urb;
1709 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
1710 continue;
1711 type = usb_pipetype(urb->pipe);
1712
1713
1714
1715
1716
1717 if (total <= 0 && type == PIPE_BULK)
1718 continue;
1719
1720
1721 address = usb_pipeendpoint (urb->pipe);
1722 if (usb_pipein(urb->pipe))
1723 address |= USB_DIR_IN;
1724 ep = find_endpoint(dum, address);
1725 if (!ep) {
1726
1727 dev_dbg(dummy_dev(dum_hcd),
1728 "no ep configured for urb %p\n",
1729 urb);
1730 status = -EPROTO;
1731 goto return_urb;
1732 }
1733
1734 if (ep->already_seen)
1735 continue;
1736 ep->already_seen = 1;
1737 if (ep == &dum->ep[0] && urb->error_count) {
1738 ep->setup_stage = 1;
1739 urb->error_count = 0;
1740 }
1741 if (ep->halted && !ep->setup_stage) {
1742
1743 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
1744 ep->ep.name, urb);
1745 status = -EPIPE;
1746 goto return_urb;
1747 }
1748
1749
1750
1751 if (ep == &dum->ep[0] && ep->setup_stage) {
1752 struct usb_ctrlrequest setup;
1753 int value = 1;
1754
1755 setup = *(struct usb_ctrlrequest *) urb->setup_packet;
1756
1757 list_for_each_entry(req, &ep->queue, queue) {
1758 list_del_init(&req->queue);
1759 req->req.status = -EOVERFLOW;
1760 dev_dbg(udc_dev(dum), "stale req = %p\n",
1761 req);
1762
1763 spin_unlock(&dum->lock);
1764 req->req.complete(&ep->ep, &req->req);
1765 spin_lock(&dum->lock);
1766 ep->already_seen = 0;
1767 goto restart;
1768 }
1769
1770
1771
1772
1773
1774 ep->last_io = jiffies;
1775 ep->setup_stage = 0;
1776 ep->halted = 0;
1777
1778 value = handle_control_request(dum_hcd, urb, &setup,
1779 &status);
1780
1781
1782
1783
1784 if (value > 0) {
1785 spin_unlock(&dum->lock);
1786 value = dum->driver->setup(&dum->gadget,
1787 &setup);
1788 spin_lock(&dum->lock);
1789
1790 if (value >= 0) {
1791
1792 limit = 64*1024;
1793 goto treat_control_like_bulk;
1794 }
1795
1796 }
1797
1798 if (value < 0) {
1799 if (value != -EOPNOTSUPP)
1800 dev_dbg(udc_dev(dum),
1801 "setup --> %d\n",
1802 value);
1803 status = -EPIPE;
1804 urb->actual_length = 0;
1805 }
1806
1807 goto return_urb;
1808 }
1809
1810
1811 limit = total;
1812 switch (usb_pipetype(urb->pipe)) {
1813 case PIPE_ISOCHRONOUS:
1814
1815
1816
1817
1818
1819 limit = max(limit, periodic_bytes(dum, ep));
1820 status = -ENOSYS;
1821 break;
1822
1823 case PIPE_INTERRUPT:
1824
1825
1826
1827 limit = max(limit, periodic_bytes(dum, ep));
1828
1829
1830 default:
1831treat_control_like_bulk:
1832 ep->last_io = jiffies;
1833 total = transfer(dum_hcd, urb, ep, limit, &status);
1834 break;
1835 }
1836
1837
1838 if (status == -EINPROGRESS)
1839 continue;
1840
1841return_urb:
1842 list_del(&urbp->urbp_list);
1843 kfree(urbp);
1844 if (ep)
1845 ep->already_seen = ep->setup_stage = 0;
1846
1847 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
1848 spin_unlock(&dum->lock);
1849 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
1850 spin_lock(&dum->lock);
1851
1852 goto restart;
1853 }
1854
1855 if (list_empty(&dum_hcd->urbp_list)) {
1856 usb_put_dev(dum_hcd->udev);
1857 dum_hcd->udev = NULL;
1858 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
1859
1860 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
1861 }
1862
1863 spin_unlock_irqrestore(&dum->lock, flags);
1864}
1865
1866
1867
1868#define PORT_C_MASK \
1869 ((USB_PORT_STAT_C_CONNECTION \
1870 | USB_PORT_STAT_C_ENABLE \
1871 | USB_PORT_STAT_C_SUSPEND \
1872 | USB_PORT_STAT_C_OVERCURRENT \
1873 | USB_PORT_STAT_C_RESET) << 16)
1874
1875static int dummy_hub_status(struct usb_hcd *hcd, char *buf)
1876{
1877 struct dummy_hcd *dum_hcd;
1878 unsigned long flags;
1879 int retval = 0;
1880
1881 dum_hcd = hcd_to_dummy_hcd(hcd);
1882
1883 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1884 if (!HCD_HW_ACCESSIBLE(hcd))
1885 goto done;
1886
1887 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1888 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1889 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1890 set_link_state(dum_hcd);
1891 }
1892
1893 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
1894 *buf = (1 << 1);
1895 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1896 dum_hcd->port_status);
1897 retval = 1;
1898 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
1899 usb_hcd_resume_root_hub(hcd);
1900 }
1901done:
1902 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1903 return retval;
1904}
1905
1906
1907static struct {
1908 struct usb_bos_descriptor bos;
1909 struct usb_ss_cap_descriptor ss_cap;
1910} __packed usb3_bos_desc = {
1911
1912 .bos = {
1913 .bLength = USB_DT_BOS_SIZE,
1914 .bDescriptorType = USB_DT_BOS,
1915 .wTotalLength = cpu_to_le16(sizeof(usb3_bos_desc)),
1916 .bNumDeviceCaps = 1,
1917 },
1918 .ss_cap = {
1919 .bLength = USB_DT_USB_SS_CAP_SIZE,
1920 .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
1921 .bDevCapabilityType = USB_SS_CAP_TYPE,
1922 .wSpeedSupported = cpu_to_le16(USB_5GBPS_OPERATION),
1923 .bFunctionalitySupport = ilog2(USB_5GBPS_OPERATION),
1924 },
1925};
1926
1927static inline void
1928ss_hub_descriptor(struct usb_hub_descriptor *desc)
1929{
1930 memset(desc, 0, sizeof *desc);
1931 desc->bDescriptorType = 0x2a;
1932 desc->bDescLength = 12;
1933 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1934 desc->bNbrPorts = 1;
1935 desc->u.ss.bHubHdrDecLat = 0x04;
1936 desc->u.ss.DeviceRemovable = 0xffff;
1937}
1938
1939static inline void hub_descriptor(struct usb_hub_descriptor *desc)
1940{
1941 memset(desc, 0, sizeof *desc);
1942 desc->bDescriptorType = 0x29;
1943 desc->bDescLength = 9;
1944 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1945 desc->bNbrPorts = 1;
1946 desc->u.hs.DeviceRemovable[0] = 0xff;
1947 desc->u.hs.DeviceRemovable[1] = 0xff;
1948}
1949
1950static int dummy_hub_control(
1951 struct usb_hcd *hcd,
1952 u16 typeReq,
1953 u16 wValue,
1954 u16 wIndex,
1955 char *buf,
1956 u16 wLength
1957) {
1958 struct dummy_hcd *dum_hcd;
1959 int retval = 0;
1960 unsigned long flags;
1961
1962 if (!HCD_HW_ACCESSIBLE(hcd))
1963 return -ETIMEDOUT;
1964
1965 dum_hcd = hcd_to_dummy_hcd(hcd);
1966
1967 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1968 switch (typeReq) {
1969 case ClearHubFeature:
1970 break;
1971 case ClearPortFeature:
1972 switch (wValue) {
1973 case USB_PORT_FEAT_SUSPEND:
1974 if (hcd->speed == HCD_USB3) {
1975 dev_dbg(dummy_dev(dum_hcd),
1976 "USB_PORT_FEAT_SUSPEND req not "
1977 "supported for USB 3.0 roothub\n");
1978 goto error;
1979 }
1980 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
1981
1982 dum_hcd->resuming = 1;
1983 dum_hcd->re_timeout = jiffies +
1984 msecs_to_jiffies(20);
1985 }
1986 break;
1987 case USB_PORT_FEAT_POWER:
1988 if (hcd->speed == HCD_USB3) {
1989 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1990 dev_dbg(dummy_dev(dum_hcd),
1991 "power-off\n");
1992 } else
1993 if (dum_hcd->port_status &
1994 USB_SS_PORT_STAT_POWER)
1995 dev_dbg(dummy_dev(dum_hcd),
1996 "power-off\n");
1997
1998 default:
1999 dum_hcd->port_status &= ~(1 << wValue);
2000 set_link_state(dum_hcd);
2001 }
2002 break;
2003 case GetHubDescriptor:
2004 if (hcd->speed == HCD_USB3 &&
2005 (wLength < USB_DT_SS_HUB_SIZE ||
2006 wValue != (USB_DT_SS_HUB << 8))) {
2007 dev_dbg(dummy_dev(dum_hcd),
2008 "Wrong hub descriptor type for "
2009 "USB 3.0 roothub.\n");
2010 goto error;
2011 }
2012 if (hcd->speed == HCD_USB3)
2013 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
2014 else
2015 hub_descriptor((struct usb_hub_descriptor *) buf);
2016 break;
2017
2018 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
2019 if (hcd->speed != HCD_USB3)
2020 goto error;
2021
2022 if ((wValue >> 8) != USB_DT_BOS)
2023 goto error;
2024
2025 memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc));
2026 retval = sizeof(usb3_bos_desc);
2027 break;
2028
2029 case GetHubStatus:
2030 *(__le32 *) buf = cpu_to_le32(0);
2031 break;
2032 case GetPortStatus:
2033 if (wIndex != 1)
2034 retval = -EPIPE;
2035
2036
2037
2038
2039 if (dum_hcd->resuming &&
2040 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2041 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
2042 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
2043 }
2044 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
2045 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2046 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
2047 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
2048 if (dum_hcd->dum->pullup) {
2049 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
2050
2051 if (hcd->speed < HCD_USB3) {
2052 switch (dum_hcd->dum->gadget.speed) {
2053 case USB_SPEED_HIGH:
2054 dum_hcd->port_status |=
2055 USB_PORT_STAT_HIGH_SPEED;
2056 break;
2057 case USB_SPEED_LOW:
2058 dum_hcd->dum->gadget.ep0->
2059 maxpacket = 8;
2060 dum_hcd->port_status |=
2061 USB_PORT_STAT_LOW_SPEED;
2062 break;
2063 default:
2064 dum_hcd->dum->gadget.speed =
2065 USB_SPEED_FULL;
2066 break;
2067 }
2068 }
2069 }
2070 }
2071 set_link_state(dum_hcd);
2072 ((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status);
2073 ((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16);
2074 break;
2075 case SetHubFeature:
2076 retval = -EPIPE;
2077 break;
2078 case SetPortFeature:
2079 switch (wValue) {
2080 case USB_PORT_FEAT_LINK_STATE:
2081 if (hcd->speed != HCD_USB3) {
2082 dev_dbg(dummy_dev(dum_hcd),
2083 "USB_PORT_FEAT_LINK_STATE req not "
2084 "supported for USB 2.0 roothub\n");
2085 goto error;
2086 }
2087
2088
2089
2090
2091 break;
2092 case USB_PORT_FEAT_U1_TIMEOUT:
2093 case USB_PORT_FEAT_U2_TIMEOUT:
2094
2095 if (hcd->speed != HCD_USB3) {
2096 dev_dbg(dummy_dev(dum_hcd),
2097 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
2098 "supported for USB 2.0 roothub\n");
2099 goto error;
2100 }
2101 break;
2102 case USB_PORT_FEAT_SUSPEND:
2103
2104 if (hcd->speed == HCD_USB3) {
2105 dev_dbg(dummy_dev(dum_hcd),
2106 "USB_PORT_FEAT_SUSPEND req not "
2107 "supported for USB 3.0 roothub\n");
2108 goto error;
2109 }
2110 if (dum_hcd->active) {
2111 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
2112
2113
2114
2115
2116 set_link_state(dum_hcd);
2117 if (((1 << USB_DEVICE_B_HNP_ENABLE)
2118 & dum_hcd->dum->devstatus) != 0)
2119 dev_dbg(dummy_dev(dum_hcd),
2120 "no HNP yet!\n");
2121 }
2122 break;
2123 case USB_PORT_FEAT_POWER:
2124 if (hcd->speed == HCD_USB3)
2125 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
2126 else
2127 dum_hcd->port_status |= USB_PORT_STAT_POWER;
2128 set_link_state(dum_hcd);
2129 break;
2130 case USB_PORT_FEAT_BH_PORT_RESET:
2131
2132 if (hcd->speed != HCD_USB3) {
2133 dev_dbg(dummy_dev(dum_hcd),
2134 "USB_PORT_FEAT_BH_PORT_RESET req not "
2135 "supported for USB 2.0 roothub\n");
2136 goto error;
2137 }
2138
2139 case USB_PORT_FEAT_RESET:
2140
2141 if (hcd->speed == HCD_USB3) {
2142 dum_hcd->port_status = 0;
2143 dum_hcd->port_status =
2144 (USB_SS_PORT_STAT_POWER |
2145 USB_PORT_STAT_CONNECTION |
2146 USB_PORT_STAT_RESET);
2147 } else
2148 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
2149 | USB_PORT_STAT_LOW_SPEED
2150 | USB_PORT_STAT_HIGH_SPEED);
2151
2152
2153
2154
2155 dum_hcd->dum->devstatus &=
2156 (1 << USB_DEVICE_SELF_POWERED);
2157
2158
2159
2160
2161 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
2162
2163 default:
2164 if (hcd->speed == HCD_USB3) {
2165 if ((dum_hcd->port_status &
2166 USB_SS_PORT_STAT_POWER) != 0) {
2167 dum_hcd->port_status |= (1 << wValue);
2168 set_link_state(dum_hcd);
2169 }
2170 } else
2171 if ((dum_hcd->port_status &
2172 USB_PORT_STAT_POWER) != 0) {
2173 dum_hcd->port_status |= (1 << wValue);
2174 set_link_state(dum_hcd);
2175 }
2176 }
2177 break;
2178 case GetPortErrorCount:
2179 if (hcd->speed != HCD_USB3) {
2180 dev_dbg(dummy_dev(dum_hcd),
2181 "GetPortErrorCount req not "
2182 "supported for USB 2.0 roothub\n");
2183 goto error;
2184 }
2185
2186 *(__le32 *) buf = cpu_to_le32(0);
2187 break;
2188 case SetHubDepth:
2189 if (hcd->speed != HCD_USB3) {
2190 dev_dbg(dummy_dev(dum_hcd),
2191 "SetHubDepth req not supported for "
2192 "USB 2.0 roothub\n");
2193 goto error;
2194 }
2195 break;
2196 default:
2197 dev_dbg(dummy_dev(dum_hcd),
2198 "hub control req%04x v%04x i%04x l%d\n",
2199 typeReq, wValue, wIndex, wLength);
2200error:
2201
2202 retval = -EPIPE;
2203 }
2204 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2205
2206 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
2207 usb_hcd_poll_rh_status(hcd);
2208 return retval;
2209}
2210
2211static int dummy_bus_suspend(struct usb_hcd *hcd)
2212{
2213 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2214
2215 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
2216
2217 spin_lock_irq(&dum_hcd->dum->lock);
2218 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2219 set_link_state(dum_hcd);
2220 hcd->state = HC_STATE_SUSPENDED;
2221 spin_unlock_irq(&dum_hcd->dum->lock);
2222 return 0;
2223}
2224
2225static int dummy_bus_resume(struct usb_hcd *hcd)
2226{
2227 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2228 int rc = 0;
2229
2230 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
2231
2232 spin_lock_irq(&dum_hcd->dum->lock);
2233 if (!HCD_HW_ACCESSIBLE(hcd)) {
2234 rc = -ESHUTDOWN;
2235 } else {
2236 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2237 set_link_state(dum_hcd);
2238 if (!list_empty(&dum_hcd->urbp_list))
2239 mod_timer(&dum_hcd->timer, jiffies);
2240 hcd->state = HC_STATE_RUNNING;
2241 }
2242 spin_unlock_irq(&dum_hcd->dum->lock);
2243 return rc;
2244}
2245
2246
2247
2248static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb)
2249{
2250 int ep = usb_pipeendpoint(urb->pipe);
2251
2252 return snprintf(buf, size,
2253 "urb/%p %s ep%d%s%s len %d/%d\n",
2254 urb,
2255 ({ char *s;
2256 switch (urb->dev->speed) {
2257 case USB_SPEED_LOW:
2258 s = "ls";
2259 break;
2260 case USB_SPEED_FULL:
2261 s = "fs";
2262 break;
2263 case USB_SPEED_HIGH:
2264 s = "hs";
2265 break;
2266 case USB_SPEED_SUPER:
2267 s = "ss";
2268 break;
2269 default:
2270 s = "?";
2271 break;
2272 } s; }),
2273 ep, ep ? (usb_pipein(urb->pipe) ? "in" : "out") : "",
2274 ({ char *s; \
2275 switch (usb_pipetype(urb->pipe)) { \
2276 case PIPE_CONTROL: \
2277 s = ""; \
2278 break; \
2279 case PIPE_BULK: \
2280 s = "-bulk"; \
2281 break; \
2282 case PIPE_INTERRUPT: \
2283 s = "-int"; \
2284 break; \
2285 default: \
2286 s = "-iso"; \
2287 break; \
2288 } s; }),
2289 urb->actual_length, urb->transfer_buffer_length);
2290}
2291
2292static ssize_t urbs_show(struct device *dev, struct device_attribute *attr,
2293 char *buf)
2294{
2295 struct usb_hcd *hcd = dev_get_drvdata(dev);
2296 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2297 struct urbp *urbp;
2298 size_t size = 0;
2299 unsigned long flags;
2300
2301 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2302 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
2303 size_t temp;
2304
2305 temp = show_urb(buf, PAGE_SIZE - size, urbp->urb);
2306 buf += temp;
2307 size += temp;
2308 }
2309 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2310
2311 return size;
2312}
2313static DEVICE_ATTR_RO(urbs);
2314
2315static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2316{
2317 init_timer(&dum_hcd->timer);
2318 dum_hcd->timer.function = dummy_timer;
2319 dum_hcd->timer.data = (unsigned long)dum_hcd;
2320 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2321 dum_hcd->stream_en_ep = 0;
2322 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2323 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2324 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2325 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2326#ifdef CONFIG_USB_OTG
2327 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2328#endif
2329 return 0;
2330
2331
2332 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2333}
2334
2335static int dummy_start(struct usb_hcd *hcd)
2336{
2337 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2338
2339
2340
2341
2342
2343
2344 if (!usb_hcd_is_primary_hcd(hcd))
2345 return dummy_start_ss(dum_hcd);
2346
2347 spin_lock_init(&dum_hcd->dum->lock);
2348 init_timer(&dum_hcd->timer);
2349 dum_hcd->timer.function = dummy_timer;
2350 dum_hcd->timer.data = (unsigned long)dum_hcd;
2351 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2352
2353 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2354
2355 hcd->power_budget = POWER_BUDGET;
2356 hcd->state = HC_STATE_RUNNING;
2357 hcd->uses_new_polling = 1;
2358
2359#ifdef CONFIG_USB_OTG
2360 hcd->self.otg_port = 1;
2361#endif
2362
2363
2364 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2365}
2366
2367static void dummy_stop(struct usb_hcd *hcd)
2368{
2369 struct dummy *dum;
2370
2371 dum = hcd_to_dummy_hcd(hcd)->dum;
2372 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2373 usb_gadget_unregister_driver(dum->driver);
2374 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
2375}
2376
2377
2378
2379static int dummy_h_get_frame(struct usb_hcd *hcd)
2380{
2381 return dummy_g_get_frame(NULL);
2382}
2383
2384static int dummy_setup(struct usb_hcd *hcd)
2385{
2386 struct dummy *dum;
2387
2388 dum = *((void **)dev_get_platdata(hcd->self.controller));
2389 hcd->self.sg_tablesize = ~0;
2390 if (usb_hcd_is_primary_hcd(hcd)) {
2391 dum->hs_hcd = hcd_to_dummy_hcd(hcd);
2392 dum->hs_hcd->dum = dum;
2393
2394
2395
2396
2397
2398 hcd->speed = HCD_USB2;
2399 hcd->self.root_hub->speed = USB_SPEED_HIGH;
2400 } else {
2401 dum->ss_hcd = hcd_to_dummy_hcd(hcd);
2402 dum->ss_hcd->dum = dum;
2403 hcd->speed = HCD_USB3;
2404 hcd->self.root_hub->speed = USB_SPEED_SUPER;
2405 }
2406 return 0;
2407}
2408
2409
2410static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
2411 struct usb_host_endpoint **eps, unsigned int num_eps,
2412 unsigned int num_streams, gfp_t mem_flags)
2413{
2414 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2415 unsigned long flags;
2416 int max_stream;
2417 int ret_streams = num_streams;
2418 unsigned int index;
2419 unsigned int i;
2420
2421 if (!num_eps)
2422 return -EINVAL;
2423
2424 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2425 for (i = 0; i < num_eps; i++) {
2426 index = dummy_get_ep_idx(&eps[i]->desc);
2427 if ((1 << index) & dum_hcd->stream_en_ep) {
2428 ret_streams = -EINVAL;
2429 goto out;
2430 }
2431 max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp);
2432 if (!max_stream) {
2433 ret_streams = -EINVAL;
2434 goto out;
2435 }
2436 if (max_stream < ret_streams) {
2437 dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u "
2438 "stream IDs.\n",
2439 eps[i]->desc.bEndpointAddress,
2440 max_stream);
2441 ret_streams = max_stream;
2442 }
2443 }
2444
2445 for (i = 0; i < num_eps; i++) {
2446 index = dummy_get_ep_idx(&eps[i]->desc);
2447 dum_hcd->stream_en_ep |= 1 << index;
2448 set_max_streams_for_pipe(dum_hcd,
2449 usb_endpoint_num(&eps[i]->desc), ret_streams);
2450 }
2451out:
2452 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2453 return ret_streams;
2454}
2455
2456
2457static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
2458 struct usb_host_endpoint **eps, unsigned int num_eps,
2459 gfp_t mem_flags)
2460{
2461 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2462 unsigned long flags;
2463 int ret;
2464 unsigned int index;
2465 unsigned int i;
2466
2467 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2468 for (i = 0; i < num_eps; i++) {
2469 index = dummy_get_ep_idx(&eps[i]->desc);
2470 if (!((1 << index) & dum_hcd->stream_en_ep)) {
2471 ret = -EINVAL;
2472 goto out;
2473 }
2474 }
2475
2476 for (i = 0; i < num_eps; i++) {
2477 index = dummy_get_ep_idx(&eps[i]->desc);
2478 dum_hcd->stream_en_ep &= ~(1 << index);
2479 set_max_streams_for_pipe(dum_hcd,
2480 usb_endpoint_num(&eps[i]->desc), 0);
2481 }
2482 ret = 0;
2483out:
2484 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2485 return ret;
2486}
2487
2488static struct hc_driver dummy_hcd = {
2489 .description = (char *) driver_name,
2490 .product_desc = "Dummy host controller",
2491 .hcd_priv_size = sizeof(struct dummy_hcd),
2492
2493 .flags = HCD_USB3 | HCD_SHARED,
2494
2495 .reset = dummy_setup,
2496 .start = dummy_start,
2497 .stop = dummy_stop,
2498
2499 .urb_enqueue = dummy_urb_enqueue,
2500 .urb_dequeue = dummy_urb_dequeue,
2501
2502 .get_frame_number = dummy_h_get_frame,
2503
2504 .hub_status_data = dummy_hub_status,
2505 .hub_control = dummy_hub_control,
2506 .bus_suspend = dummy_bus_suspend,
2507 .bus_resume = dummy_bus_resume,
2508
2509 .alloc_streams = dummy_alloc_streams,
2510 .free_streams = dummy_free_streams,
2511};
2512
2513static int dummy_hcd_probe(struct platform_device *pdev)
2514{
2515 struct dummy *dum;
2516 struct usb_hcd *hs_hcd;
2517 struct usb_hcd *ss_hcd;
2518 int retval;
2519
2520 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
2521 dum = *((void **)dev_get_platdata(&pdev->dev));
2522
2523 if (!mod_data.is_super_speed)
2524 dummy_hcd.flags = HCD_USB2;
2525 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2526 if (!hs_hcd)
2527 return -ENOMEM;
2528 hs_hcd->has_tt = 1;
2529
2530 retval = usb_add_hcd(hs_hcd, 0, 0);
2531 if (retval)
2532 goto put_usb2_hcd;
2533
2534 if (mod_data.is_super_speed) {
2535 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2536 dev_name(&pdev->dev), hs_hcd);
2537 if (!ss_hcd) {
2538 retval = -ENOMEM;
2539 goto dealloc_usb2_hcd;
2540 }
2541
2542 retval = usb_add_hcd(ss_hcd, 0, 0);
2543 if (retval)
2544 goto put_usb3_hcd;
2545 }
2546 return 0;
2547
2548put_usb3_hcd:
2549 usb_put_hcd(ss_hcd);
2550dealloc_usb2_hcd:
2551 usb_remove_hcd(hs_hcd);
2552put_usb2_hcd:
2553 usb_put_hcd(hs_hcd);
2554 dum->hs_hcd = dum->ss_hcd = NULL;
2555 return retval;
2556}
2557
2558static int dummy_hcd_remove(struct platform_device *pdev)
2559{
2560 struct dummy *dum;
2561
2562 dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum;
2563
2564 if (dum->ss_hcd) {
2565 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2566 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2567 }
2568
2569 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2570 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2571
2572 dum->hs_hcd = NULL;
2573 dum->ss_hcd = NULL;
2574
2575 return 0;
2576}
2577
2578static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state)
2579{
2580 struct usb_hcd *hcd;
2581 struct dummy_hcd *dum_hcd;
2582 int rc = 0;
2583
2584 dev_dbg(&pdev->dev, "%s\n", __func__);
2585
2586 hcd = platform_get_drvdata(pdev);
2587 dum_hcd = hcd_to_dummy_hcd(hcd);
2588 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
2589 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2590 rc = -EBUSY;
2591 } else
2592 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2593 return rc;
2594}
2595
2596static int dummy_hcd_resume(struct platform_device *pdev)
2597{
2598 struct usb_hcd *hcd;
2599
2600 dev_dbg(&pdev->dev, "%s\n", __func__);
2601
2602 hcd = platform_get_drvdata(pdev);
2603 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2604 usb_hcd_poll_rh_status(hcd);
2605 return 0;
2606}
2607
2608static struct platform_driver dummy_hcd_driver = {
2609 .probe = dummy_hcd_probe,
2610 .remove = dummy_hcd_remove,
2611 .suspend = dummy_hcd_suspend,
2612 .resume = dummy_hcd_resume,
2613 .driver = {
2614 .name = (char *) driver_name,
2615 .owner = THIS_MODULE,
2616 },
2617};
2618
2619
2620#define MAX_NUM_UDC 2
2621static struct platform_device *the_udc_pdev[MAX_NUM_UDC];
2622static struct platform_device *the_hcd_pdev[MAX_NUM_UDC];
2623
2624static int __init init(void)
2625{
2626 int retval = -ENOMEM;
2627 int i;
2628 struct dummy *dum[MAX_NUM_UDC];
2629
2630 if (usb_disabled())
2631 return -ENODEV;
2632
2633 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2634 return -EINVAL;
2635
2636 if (mod_data.num < 1 || mod_data.num > MAX_NUM_UDC) {
2637 pr_err("Number of emulated UDC must be in range of 1…%d\n",
2638 MAX_NUM_UDC);
2639 return -EINVAL;
2640 }
2641
2642 for (i = 0; i < mod_data.num; i++) {
2643 the_hcd_pdev[i] = platform_device_alloc(driver_name, i);
2644 if (!the_hcd_pdev[i]) {
2645 i--;
2646 while (i >= 0)
2647 platform_device_put(the_hcd_pdev[i--]);
2648 return retval;
2649 }
2650 }
2651 for (i = 0; i < mod_data.num; i++) {
2652 the_udc_pdev[i] = platform_device_alloc(gadget_name, i);
2653 if (!the_udc_pdev[i]) {
2654 i--;
2655 while (i >= 0)
2656 platform_device_put(the_udc_pdev[i--]);
2657 goto err_alloc_udc;
2658 }
2659 }
2660 for (i = 0; i < mod_data.num; i++) {
2661 dum[i] = kzalloc(sizeof(struct dummy), GFP_KERNEL);
2662 if (!dum[i]) {
2663 retval = -ENOMEM;
2664 goto err_add_pdata;
2665 }
2666 retval = platform_device_add_data(the_hcd_pdev[i], &dum[i],
2667 sizeof(void *));
2668 if (retval)
2669 goto err_add_pdata;
2670 retval = platform_device_add_data(the_udc_pdev[i], &dum[i],
2671 sizeof(void *));
2672 if (retval)
2673 goto err_add_pdata;
2674 }
2675
2676 retval = platform_driver_register(&dummy_hcd_driver);
2677 if (retval < 0)
2678 goto err_add_pdata;
2679 retval = platform_driver_register(&dummy_udc_driver);
2680 if (retval < 0)
2681 goto err_register_udc_driver;
2682
2683 for (i = 0; i < mod_data.num; i++) {
2684 retval = platform_device_add(the_hcd_pdev[i]);
2685 if (retval < 0) {
2686 i--;
2687 while (i >= 0)
2688 platform_device_del(the_hcd_pdev[i--]);
2689 goto err_add_hcd;
2690 }
2691 }
2692 for (i = 0; i < mod_data.num; i++) {
2693 if (!dum[i]->hs_hcd ||
2694 (!dum[i]->ss_hcd && mod_data.is_super_speed)) {
2695
2696
2697
2698
2699 retval = -EINVAL;
2700 goto err_add_udc;
2701 }
2702 }
2703
2704 for (i = 0; i < mod_data.num; i++) {
2705 retval = platform_device_add(the_udc_pdev[i]);
2706 if (retval < 0) {
2707 i--;
2708 while (i >= 0)
2709 platform_device_del(the_udc_pdev[i]);
2710 goto err_add_udc;
2711 }
2712 }
2713
2714 for (i = 0; i < mod_data.num; i++) {
2715 if (!platform_get_drvdata(the_udc_pdev[i])) {
2716
2717
2718
2719
2720 retval = -EINVAL;
2721 goto err_probe_udc;
2722 }
2723 }
2724 return retval;
2725
2726err_probe_udc:
2727 for (i = 0; i < mod_data.num; i++)
2728 platform_device_del(the_udc_pdev[i]);
2729err_add_udc:
2730 for (i = 0; i < mod_data.num; i++)
2731 platform_device_del(the_hcd_pdev[i]);
2732err_add_hcd:
2733 platform_driver_unregister(&dummy_udc_driver);
2734err_register_udc_driver:
2735 platform_driver_unregister(&dummy_hcd_driver);
2736err_add_pdata:
2737 for (i = 0; i < mod_data.num; i++)
2738 kfree(dum[i]);
2739 for (i = 0; i < mod_data.num; i++)
2740 platform_device_put(the_udc_pdev[i]);
2741err_alloc_udc:
2742 for (i = 0; i < mod_data.num; i++)
2743 platform_device_put(the_hcd_pdev[i]);
2744 return retval;
2745}
2746module_init(init);
2747
2748static void __exit cleanup(void)
2749{
2750 int i;
2751
2752 for (i = 0; i < mod_data.num; i++) {
2753 struct dummy *dum;
2754
2755 dum = *((void **)dev_get_platdata(&the_udc_pdev[i]->dev));
2756
2757 platform_device_unregister(the_udc_pdev[i]);
2758 platform_device_unregister(the_hcd_pdev[i]);
2759 kfree(dum);
2760 }
2761 platform_driver_unregister(&dummy_udc_driver);
2762 platform_driver_unregister(&dummy_hcd_driver);
2763}
2764module_exit(cleanup);
2765