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