usb: gadget: remove net2280_set_fifo_mode()
[deliverable/linux.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
LT
889
890 INIT_LIST_HEAD (&dum->gadget.ep_list);
891 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
892 struct dummy_ep *ep = &dum->ep [i];
893
894 if (!ep_name [i])
895 break;
896 ep->ep.name = ep_name [i];
897 ep->ep.ops = &dummy_ep_ops;
898 list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
851a526d
AS
899 ep->halted = ep->wedged = ep->already_seen =
900 ep->setup_stage = 0;
1da177e4
LT
901 ep->ep.maxpacket = ~0;
902 ep->last_io = jiffies;
903 ep->gadget = &dum->gadget;
904 ep->desc = NULL;
905 INIT_LIST_HEAD (&ep->queue);
906 }
907
908 dum->gadget.ep0 = &dum->ep [0].ep;
1cd8fd28
TB
909 if (mod_data.is_super_speed)
910 dum->gadget.speed = driver->speed;
7eca4c5a
TB
911 else if (mod_data.is_high_speed)
912 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH, driver->speed);
1cd8fd28 913 else
7eca4c5a 914 dum->gadget.speed = USB_SPEED_FULL;
1cd8fd28 915 if (dum->gadget.speed < driver->speed)
7eca4c5a
TB
916 dev_dbg(udc_dev(dum), "This device can perform faster"
917 " if you connect it to a %s port...\n",
918 (driver->speed == USB_SPEED_SUPER ?
919 "SuperSpeed" : "HighSpeed"));
1cd8fd28
TB
920
921 if (dum->gadget.speed == USB_SPEED_SUPER) {
922 for (i = 0; i < DUMMY_ENDPOINTS; i++)
923 dum->ep[i].ep.max_streams = 0x10;
924 dum->ep[0].ep.maxpacket = 9;
925 } else
926 dum->ep[0].ep.maxpacket = 64;
99fd1408
SAS
927
928 if (dum->gadget.speed == USB_SPEED_SUPER)
929 dum->gadget.is_otg =
930 (dummy_hcd_to_hcd(dum->ss_hcd)->self.otg_port != 0);
931 else
932 dum->gadget.is_otg =
933 (dummy_hcd_to_hcd(dum->hs_hcd)->self.otg_port != 0);
934
1da177e4
LT
935 list_del_init (&dum->ep [0].ep.ep_list);
936 INIT_LIST_HEAD(&dum->fifo_req.queue);
937
59331017 938 driver->driver.bus = NULL;
1da177e4
LT
939 dum->driver = driver;
940 dum->gadget.dev.driver = &driver->driver;
d9b76251 941 dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
1da177e4 942 driver->driver.name);
b0fca50f 943 retval = bind(&dum->gadget);
59331017
AS
944 if (retval) {
945 dum->driver = NULL;
946 dum->gadget.dev.driver = NULL;
947 return retval;
948 }
1da177e4 949
25427874
SAS
950 /* khubd will enumerate this in a while */
951 dummy_pullup(&dum->gadget, 1);
1da177e4
LT
952 return 0;
953}
1da177e4 954
0f91349b 955static int dummy_udc_stop(struct usb_gadget_driver *driver)
1da177e4 956{
cdfcbd2c 957 struct dummy *dum = &the_controller;
1da177e4
LT
958
959 if (!dum)
960 return -ENODEV;
6bea476c 961 if (!driver || driver != dum->driver || !driver->unbind)
1da177e4
LT
962 return -EINVAL;
963
d9b76251 964 dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
1da177e4
LT
965 driver->driver.name);
966
25427874 967 dummy_pullup(&dum->gadget, 0);
1da177e4
LT
968
969 driver->unbind (&dum->gadget);
59331017 970 dum->gadget.dev.driver = NULL;
1da177e4 971 dum->driver = NULL;
25427874
SAS
972
973 dummy_pullup(&dum->gadget, 0);
1da177e4
LT
974 return 0;
975}
1da177e4
LT
976
977#undef is_enabled
978
d9b76251
AS
979/* The gadget structure is stored inside the hcd structure and will be
980 * released along with it. */
981static void
982dummy_gadget_release (struct device *dev)
983{
cdfcbd2c 984 return;
d9b76251
AS
985}
986
8364d6b0 987static int dummy_udc_probe (struct platform_device *pdev)
d9b76251 988{
cdfcbd2c 989 struct dummy *dum = &the_controller;
d9b76251
AS
990 int rc;
991
992 dum->gadget.name = gadget_name;
993 dum->gadget.ops = &dummy_ops;
994 dum->gadget.is_dualspeed = 1;
995
0031a06e 996 dev_set_name(&dum->gadget.dev, "gadget");
8364d6b0 997 dum->gadget.dev.parent = &pdev->dev;
d9b76251
AS
998 dum->gadget.dev.release = dummy_gadget_release;
999 rc = device_register (&dum->gadget.dev);
75d87cdf
RR
1000 if (rc < 0) {
1001 put_device(&dum->gadget.dev);
d9b76251 1002 return rc;
75d87cdf 1003 }
d9b76251 1004
0f91349b
SAS
1005 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
1006 if (rc < 0)
1007 goto err_udc;
1008
efd54a36
AS
1009 rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
1010 if (rc < 0)
0f91349b
SAS
1011 goto err_dev;
1012 platform_set_drvdata(pdev, dum);
1013 return rc;
1014
1015err_dev:
1016 usb_del_gadget_udc(&dum->gadget);
1017err_udc:
1018 device_unregister(&dum->gadget.dev);
d9b76251
AS
1019 return rc;
1020}
1021
8364d6b0 1022static int dummy_udc_remove (struct platform_device *pdev)
d9b76251 1023{
8364d6b0 1024 struct dummy *dum = platform_get_drvdata (pdev);
d9b76251 1025
0f91349b 1026 usb_del_gadget_udc(&dum->gadget);
8364d6b0 1027 platform_set_drvdata (pdev, NULL);
d9b76251
AS
1028 device_remove_file (&dum->gadget.dev, &dev_attr_function);
1029 device_unregister (&dum->gadget.dev);
1030 return 0;
1031}
1032
8364d6b0 1033static int dummy_udc_suspend (struct platform_device *pdev, pm_message_t state)
391eca9d 1034{
8364d6b0 1035 struct dummy *dum = platform_get_drvdata(pdev);
d8a14a85 1036 struct dummy_hcd *dum_hcd;
391eca9d 1037
441b62c1 1038 dev_dbg (&pdev->dev, "%s\n", __func__);
d8a14a85 1039 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
391eca9d
AS
1040 spin_lock_irq (&dum->lock);
1041 dum->udc_suspended = 1;
d8a14a85 1042 set_link_state(dum_hcd);
391eca9d
AS
1043 spin_unlock_irq (&dum->lock);
1044
d8a14a85 1045 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
391eca9d
AS
1046 return 0;
1047}
1048
8364d6b0 1049static int dummy_udc_resume (struct platform_device *pdev)
391eca9d 1050{
8364d6b0 1051 struct dummy *dum = platform_get_drvdata(pdev);
d8a14a85 1052 struct dummy_hcd *dum_hcd;
391eca9d 1053
441b62c1 1054 dev_dbg (&pdev->dev, "%s\n", __func__);
d8a14a85 1055 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
391eca9d
AS
1056 spin_lock_irq (&dum->lock);
1057 dum->udc_suspended = 0;
d8a14a85 1058 set_link_state(dum_hcd);
391eca9d
AS
1059 spin_unlock_irq (&dum->lock);
1060
d8a14a85 1061 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
391eca9d
AS
1062 return 0;
1063}
1064
3ae5eaec 1065static struct platform_driver dummy_udc_driver = {
d9b76251
AS
1066 .probe = dummy_udc_probe,
1067 .remove = dummy_udc_remove,
391eca9d
AS
1068 .suspend = dummy_udc_suspend,
1069 .resume = dummy_udc_resume,
3ae5eaec
RK
1070 .driver = {
1071 .name = (char *) gadget_name,
1072 .owner = THIS_MODULE,
1073 },
d9b76251
AS
1074};
1075
1da177e4
LT
1076/*-------------------------------------------------------------------------*/
1077
1078/* MASTER/HOST SIDE DRIVER
1079 *
1080 * this uses the hcd framework to hook up to host side drivers.
1081 * its root hub will only have one device, otherwise it acts like
1082 * a normal host controller.
1083 *
1084 * when urbs are queued, they're just stuck on a list that we
1085 * scan in a timer callback. that callback connects writes from
1086 * the host with reads from the device, and so on, based on the
1087 * usb 2.0 rules.
1088 */
1089
1090static int dummy_urb_enqueue (
1091 struct usb_hcd *hcd,
1da177e4 1092 struct urb *urb,
55016f10 1093 gfp_t mem_flags
1da177e4 1094) {
cdfcbd2c 1095 struct dummy_hcd *dum_hcd;
1da177e4
LT
1096 struct urbp *urbp;
1097 unsigned long flags;
e9df41c5 1098 int rc;
1da177e4
LT
1099
1100 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1101 return -EINVAL;
1102
1103 urbp = kmalloc (sizeof *urbp, mem_flags);
1104 if (!urbp)
1105 return -ENOMEM;
1106 urbp->urb = urb;
1107
cdfcbd2c
TB
1108 dum_hcd = hcd_to_dummy_hcd(hcd);
1109 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
e9df41c5
AS
1110 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1111 if (rc) {
1112 kfree(urbp);
1113 goto done;
1114 }
1da177e4 1115
cdfcbd2c
TB
1116 if (!dum_hcd->udev) {
1117 dum_hcd->udev = urb->dev;
1118 usb_get_dev(dum_hcd->udev);
1119 } else if (unlikely(dum_hcd->udev != urb->dev))
1120 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
1da177e4 1121
cdfcbd2c 1122 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
1da177e4
LT
1123 urb->hcpriv = urbp;
1124 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
1125 urb->error_count = 1; /* mark as a new urb */
1126
1127 /* kick the scheduler, it'll do the rest */
cdfcbd2c
TB
1128 if (!timer_pending(&dum_hcd->timer))
1129 mod_timer(&dum_hcd->timer, jiffies + 1);
1da177e4 1130
e9df41c5 1131 done:
cdfcbd2c 1132 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
e9df41c5 1133 return rc;
1da177e4
LT
1134}
1135
e9df41c5 1136static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1da177e4 1137{
cdfcbd2c 1138 struct dummy_hcd *dum_hcd;
391eca9d 1139 unsigned long flags;
e9df41c5 1140 int rc;
391eca9d
AS
1141
1142 /* giveback happens automatically in timer callback,
1143 * so make sure the callback happens */
cdfcbd2c
TB
1144 dum_hcd = hcd_to_dummy_hcd(hcd);
1145 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
e9df41c5
AS
1146
1147 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
cdfcbd2c
TB
1148 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1149 !list_empty(&dum_hcd->urbp_list))
1150 mod_timer(&dum_hcd->timer, jiffies);
e9df41c5 1151
cdfcbd2c 1152 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
e9df41c5 1153 return rc;
1da177e4
LT
1154}
1155
1da177e4
LT
1156/* transfer up to a frame's worth; caller must own lock */
1157static int
4d2f110c
AS
1158transfer(struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit,
1159 int *status)
1da177e4
LT
1160{
1161 struct dummy_request *req;
1162
1163top:
1164 /* if there's no request queued, the device is NAKing; return */
1165 list_for_each_entry (req, &ep->queue, queue) {
1166 unsigned host_len, dev_len, len;
1167 int is_short, to_host;
1168 int rescan = 0;
1169
1170 /* 1..N packets of ep->ep.maxpacket each ... the last one
1171 * may be short (including zero length).
1172 *
1173 * writer can send a zlp explicitly (length 0) or implicitly
1174 * (length mod maxpacket zero, and 'zero' flag); they always
1175 * terminate reads.
1176 */
1177 host_len = urb->transfer_buffer_length - urb->actual_length;
1178 dev_len = req->req.length - req->req.actual;
1179 len = min (host_len, dev_len);
1180
1181 /* FIXME update emulated data toggle too */
1182
1183 to_host = usb_pipein (urb->pipe);
1184 if (unlikely (len == 0))
1185 is_short = 1;
1186 else {
1187 char *ubuf, *rbuf;
1188
1189 /* not enough bandwidth left? */
1190 if (limit < ep->ep.maxpacket && limit < len)
1191 break;
1192 len = min (len, (unsigned) limit);
1193 if (len == 0)
1194 break;
1195
1196 /* use an extra pass for the final short packet */
1197 if (len > ep->ep.maxpacket) {
1198 rescan = 1;
1199 len -= (len % ep->ep.maxpacket);
1200 }
1201 is_short = (len % ep->ep.maxpacket) != 0;
1202
1203 /* else transfer packet(s) */
1204 ubuf = urb->transfer_buffer + urb->actual_length;
1205 rbuf = req->req.buf + req->req.actual;
1206 if (to_host)
1207 memcpy (ubuf, rbuf, len);
1208 else
1209 memcpy (rbuf, ubuf, len);
1210 ep->last_io = jiffies;
1211
1212 limit -= len;
1213 urb->actual_length += len;
1214 req->req.actual += len;
1215 }
1216
1217 /* short packets terminate, maybe with overflow/underflow.
1218 * it's only really an error to write too much.
1219 *
1220 * partially filling a buffer optionally blocks queue advances
1221 * (so completion handlers can clean up the queue) but we don't
b0d9efba 1222 * need to emulate such data-in-flight.
1da177e4
LT
1223 */
1224 if (is_short) {
1225 if (host_len == dev_len) {
1226 req->req.status = 0;
4d2f110c 1227 *status = 0;
1da177e4
LT
1228 } else if (to_host) {
1229 req->req.status = 0;
1230 if (dev_len > host_len)
4d2f110c 1231 *status = -EOVERFLOW;
1da177e4 1232 else
4d2f110c 1233 *status = 0;
1da177e4 1234 } else if (!to_host) {
4d2f110c 1235 *status = 0;
1da177e4
LT
1236 if (host_len > dev_len)
1237 req->req.status = -EOVERFLOW;
1238 else
1239 req->req.status = 0;
1240 }
1241
1242 /* many requests terminate without a short packet */
1243 } else {
1244 if (req->req.length == req->req.actual
1245 && !req->req.zero)
1246 req->req.status = 0;
1247 if (urb->transfer_buffer_length == urb->actual_length
1248 && !(urb->transfer_flags
4d2f110c
AS
1249 & URB_ZERO_PACKET))
1250 *status = 0;
1da177e4
LT
1251 }
1252
1253 /* device side completion --> continuable */
1254 if (req->req.status != -EINPROGRESS) {
1255 list_del_init (&req->queue);
1256
1257 spin_unlock (&dum->lock);
1258 req->req.complete (&ep->ep, &req->req);
1259 spin_lock (&dum->lock);
1260
1261 /* requests might have been unlinked... */
1262 rescan = 1;
1263 }
1264
1265 /* host side completion --> terminate */
4d2f110c 1266 if (*status != -EINPROGRESS)
1da177e4
LT
1267 break;
1268
1269 /* rescan to continue with any other queued i/o */
1270 if (rescan)
1271 goto top;
1272 }
1273 return limit;
1274}
1275
1276static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1277{
1278 int limit = ep->ep.maxpacket;
1279
1280 if (dum->gadget.speed == USB_SPEED_HIGH) {
1281 int tmp;
1282
1283 /* high bandwidth mode */
1284 tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
1da177e4
LT
1285 tmp = (tmp >> 11) & 0x03;
1286 tmp *= 8 /* applies to entire frame */;
1287 limit += limit * tmp;
1288 }
1cd8fd28
TB
1289 if (dum->gadget.speed == USB_SPEED_SUPER) {
1290 switch (ep->desc->bmAttributes & 0x03) {
1291 case USB_ENDPOINT_XFER_ISOC:
1292 /* Sec. 4.4.8.2 USB3.0 Spec */
1293 limit = 3 * 16 * 1024 * 8;
1294 break;
1295 case USB_ENDPOINT_XFER_INT:
1296 /* Sec. 4.4.7.2 USB3.0 Spec */
1297 limit = 3 * 1024 * 8;
1298 break;
1299 case USB_ENDPOINT_XFER_BULK:
1300 default:
1301 break;
1302 }
1303 }
1da177e4
LT
1304 return limit;
1305}
1306
cdfcbd2c 1307#define is_active(dum_hcd) ((dum_hcd->port_status & \
1da177e4
LT
1308 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1309 USB_PORT_STAT_SUSPEND)) \
1310 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1311
1312static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1313{
1314 int i;
1315
1cd8fd28
TB
1316 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1317 dum->ss_hcd : dum->hs_hcd)))
1da177e4
LT
1318 return NULL;
1319 if ((address & ~USB_DIR_IN) == 0)
1320 return &dum->ep [0];
1321 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1322 struct dummy_ep *ep = &dum->ep [i];
1323
1324 if (!ep->desc)
1325 continue;
1326 if (ep->desc->bEndpointAddress == address)
1327 return ep;
1328 }
1329 return NULL;
1330}
1331
1332#undef is_active
1333
1334#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1335#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1336#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1337#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1338#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1339#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1340
8be8a9d3
TB
1341
1342/**
1343 * handle_control_request() - handles all control transfers
1344 * @dum: pointer to dummy (the_controller)
1345 * @urb: the urb request to handle
1346 * @setup: pointer to the setup data for a USB device control
1347 * request
1348 * @status: pointer to request handling status
1349 *
1350 * Return 0 - if the request was handled
1351 * 1 - if the request wasn't handles
1352 * error code on error
1353 */
cdfcbd2c 1354static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
8be8a9d3
TB
1355 struct usb_ctrlrequest *setup,
1356 int *status)
1357{
1358 struct dummy_ep *ep2;
cdfcbd2c 1359 struct dummy *dum = dum_hcd->dum;
8be8a9d3
TB
1360 int ret_val = 1;
1361 unsigned w_index;
1362 unsigned w_value;
1363
1364 w_index = le16_to_cpu(setup->wIndex);
1365 w_value = le16_to_cpu(setup->wValue);
1366 switch (setup->bRequest) {
1367 case USB_REQ_SET_ADDRESS:
1368 if (setup->bRequestType != Dev_Request)
1369 break;
1370 dum->address = w_value;
1371 *status = 0;
1372 dev_dbg(udc_dev(dum), "set_address = %d\n",
1373 w_value);
1374 ret_val = 0;
1375 break;
1376 case USB_REQ_SET_FEATURE:
1377 if (setup->bRequestType == Dev_Request) {
1378 ret_val = 0;
1379 switch (w_value) {
1380 case USB_DEVICE_REMOTE_WAKEUP:
1381 break;
1382 case USB_DEVICE_B_HNP_ENABLE:
1383 dum->gadget.b_hnp_enable = 1;
1384 break;
1385 case USB_DEVICE_A_HNP_SUPPORT:
1386 dum->gadget.a_hnp_support = 1;
1387 break;
1388 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1389 dum->gadget.a_alt_hnp_support = 1;
1390 break;
1cd8fd28
TB
1391 case USB_DEVICE_U1_ENABLE:
1392 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1393 HCD_USB3)
1394 w_value = USB_DEV_STAT_U1_ENABLED;
1395 else
1396 ret_val = -EOPNOTSUPP;
1397 break;
1398 case USB_DEVICE_U2_ENABLE:
1399 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1400 HCD_USB3)
1401 w_value = USB_DEV_STAT_U2_ENABLED;
1402 else
1403 ret_val = -EOPNOTSUPP;
1404 break;
1405 case USB_DEVICE_LTM_ENABLE:
1406 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1407 HCD_USB3)
1408 w_value = USB_DEV_STAT_LTM_ENABLED;
1409 else
1410 ret_val = -EOPNOTSUPP;
1411 break;
8be8a9d3
TB
1412 default:
1413 ret_val = -EOPNOTSUPP;
1414 }
1415 if (ret_val == 0) {
1416 dum->devstatus |= (1 << w_value);
1417 *status = 0;
1418 }
1419 } else if (setup->bRequestType == Ep_Request) {
1420 /* endpoint halt */
1421 ep2 = find_endpoint(dum, w_index);
1422 if (!ep2 || ep2->ep.name == ep0name) {
1423 ret_val = -EOPNOTSUPP;
1424 break;
1425 }
1426 ep2->halted = 1;
1427 ret_val = 0;
1428 *status = 0;
1429 }
1430 break;
1431 case USB_REQ_CLEAR_FEATURE:
1432 if (setup->bRequestType == Dev_Request) {
1433 ret_val = 0;
1434 switch (w_value) {
1435 case USB_DEVICE_REMOTE_WAKEUP:
1436 w_value = USB_DEVICE_REMOTE_WAKEUP;
1437 break;
1cd8fd28
TB
1438 case USB_DEVICE_U1_ENABLE:
1439 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1440 HCD_USB3)
1441 w_value = USB_DEV_STAT_U1_ENABLED;
1442 else
1443 ret_val = -EOPNOTSUPP;
1444 break;
1445 case USB_DEVICE_U2_ENABLE:
1446 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1447 HCD_USB3)
1448 w_value = USB_DEV_STAT_U2_ENABLED;
1449 else
1450 ret_val = -EOPNOTSUPP;
1451 break;
1452 case USB_DEVICE_LTM_ENABLE:
1453 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1454 HCD_USB3)
1455 w_value = USB_DEV_STAT_LTM_ENABLED;
1456 else
1457 ret_val = -EOPNOTSUPP;
1458 break;
8be8a9d3
TB
1459 default:
1460 ret_val = -EOPNOTSUPP;
1461 break;
1462 }
1463 if (ret_val == 0) {
1464 dum->devstatus &= ~(1 << w_value);
1465 *status = 0;
1466 }
1467 } else if (setup->bRequestType == Ep_Request) {
1468 /* endpoint halt */
1469 ep2 = find_endpoint(dum, w_index);
1470 if (!ep2) {
1471 ret_val = -EOPNOTSUPP;
1472 break;
1473 }
1474 if (!ep2->wedged)
1475 ep2->halted = 0;
1476 ret_val = 0;
1477 *status = 0;
1478 }
1479 break;
1480 case USB_REQ_GET_STATUS:
1481 if (setup->bRequestType == Dev_InRequest
1482 || setup->bRequestType == Intf_InRequest
1483 || setup->bRequestType == Ep_InRequest) {
1484 char *buf;
1485 /*
1486 * device: remote wakeup, selfpowered
1487 * interface: nothing
1488 * endpoint: halt
1489 */
1490 buf = (char *)urb->transfer_buffer;
1491 if (urb->transfer_buffer_length > 0) {
1492 if (setup->bRequestType == Ep_InRequest) {
1493 ep2 = find_endpoint(dum, w_index);
1494 if (!ep2) {
1495 ret_val = -EOPNOTSUPP;
1496 break;
1497 }
1498 buf[0] = ep2->halted;
1499 } else if (setup->bRequestType ==
1500 Dev_InRequest) {
1501 buf[0] = (u8)dum->devstatus;
1502 } else
1503 buf[0] = 0;
1504 }
1505 if (urb->transfer_buffer_length > 1)
1506 buf[1] = 0;
1507 urb->actual_length = min_t(u32, 2,
1508 urb->transfer_buffer_length);
1509 ret_val = 0;
1510 *status = 0;
1511 }
1512 break;
1513 }
1514 return ret_val;
1515}
1516
1da177e4
LT
1517/* drive both sides of the transfers; looks like irq handlers to
1518 * both drivers except the callbacks aren't in_irq().
1519 */
cdfcbd2c 1520static void dummy_timer(unsigned long _dum_hcd)
1da177e4 1521{
cdfcbd2c
TB
1522 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1523 struct dummy *dum = dum_hcd->dum;
1da177e4
LT
1524 struct urbp *urbp, *tmp;
1525 unsigned long flags;
1526 int limit, total;
1527 int i;
1528
1529 /* simplistic model for one frame's bandwidth */
1530 switch (dum->gadget.speed) {
1531 case USB_SPEED_LOW:
1532 total = 8/*bytes*/ * 12/*packets*/;
1533 break;
1534 case USB_SPEED_FULL:
1535 total = 64/*bytes*/ * 19/*packets*/;
1536 break;
1537 case USB_SPEED_HIGH:
1538 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1539 break;
1cd8fd28
TB
1540 case USB_SPEED_SUPER:
1541 /* Bus speed is 500000 bytes/ms, so use a little less */
1542 total = 490000;
1543 break;
1da177e4 1544 default:
cdfcbd2c 1545 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
1da177e4
LT
1546 return;
1547 }
1548
1549 /* FIXME if HZ != 1000 this will probably misbehave ... */
1550
1551 /* look at each urb queued by the host side driver */
1552 spin_lock_irqsave (&dum->lock, flags);
1553
cdfcbd2c
TB
1554 if (!dum_hcd->udev) {
1555 dev_err(dummy_dev(dum_hcd),
1da177e4
LT
1556 "timer fired with no URBs pending?\n");
1557 spin_unlock_irqrestore (&dum->lock, flags);
1558 return;
1559 }
1560
1561 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1562 if (!ep_name [i])
1563 break;
1564 dum->ep [i].already_seen = 0;
1565 }
1566
1567restart:
cdfcbd2c 1568 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
1da177e4
LT
1569 struct urb *urb;
1570 struct dummy_request *req;
1571 u8 address;
1572 struct dummy_ep *ep = NULL;
1573 int type;
4d2f110c 1574 int status = -EINPROGRESS;
1da177e4
LT
1575
1576 urb = urbp->urb;
eb231054 1577 if (urb->unlinked)
1da177e4 1578 goto return_urb;
cdfcbd2c 1579 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
391eca9d 1580 continue;
1da177e4
LT
1581 type = usb_pipetype (urb->pipe);
1582
1583 /* used up this frame's non-periodic bandwidth?
1584 * FIXME there's infinite bandwidth for control and
1585 * periodic transfers ... unrealistic.
1586 */
1587 if (total <= 0 && type == PIPE_BULK)
1588 continue;
1589
1590 /* find the gadget's ep for this request (if configured) */
1591 address = usb_pipeendpoint (urb->pipe);
1592 if (usb_pipein (urb->pipe))
1593 address |= USB_DIR_IN;
1594 ep = find_endpoint(dum, address);
1595 if (!ep) {
1596 /* set_configuration() disagreement */
cdfcbd2c 1597 dev_dbg(dummy_dev(dum_hcd),
1da177e4
LT
1598 "no ep configured for urb %p\n",
1599 urb);
4d2f110c 1600 status = -EPROTO;
1da177e4
LT
1601 goto return_urb;
1602 }
1603
1604 if (ep->already_seen)
1605 continue;
1606 ep->already_seen = 1;
1607 if (ep == &dum->ep [0] && urb->error_count) {
1608 ep->setup_stage = 1; /* a new urb */
1609 urb->error_count = 0;
1610 }
1611 if (ep->halted && !ep->setup_stage) {
1612 /* NOTE: must not be iso! */
cdfcbd2c 1613 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
1da177e4 1614 ep->ep.name, urb);
4d2f110c 1615 status = -EPIPE;
1da177e4
LT
1616 goto return_urb;
1617 }
1618 /* FIXME make sure both ends agree on maxpacket */
1619
1620 /* handle control requests */
1621 if (ep == &dum->ep [0] && ep->setup_stage) {
1622 struct usb_ctrlrequest setup;
1623 int value = 1;
1da177e4
LT
1624
1625 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
1da177e4
LT
1626 /* paranoia, in case of stale queued data */
1627 list_for_each_entry (req, &ep->queue, queue) {
1628 list_del_init (&req->queue);
1629 req->req.status = -EOVERFLOW;
d9b76251 1630 dev_dbg (udc_dev(dum), "stale req = %p\n",
1da177e4
LT
1631 req);
1632
1633 spin_unlock (&dum->lock);
1634 req->req.complete (&ep->ep, &req->req);
1635 spin_lock (&dum->lock);
1636 ep->already_seen = 0;
1637 goto restart;
1638 }
1639
1640 /* gadget driver never sees set_address or operations
1641 * on standard feature flags. some hardware doesn't
1642 * even expose them.
1643 */
1644 ep->last_io = jiffies;
1645 ep->setup_stage = 0;
1646 ep->halted = 0;
1da177e4 1647
cdfcbd2c 1648 value = handle_control_request(dum_hcd, urb, &setup,
8be8a9d3 1649 &status);
1da177e4
LT
1650
1651 /* gadget driver handles all other requests. block
1652 * until setup() returns; no reentrancy issues etc.
1653 */
1654 if (value > 0) {
1655 spin_unlock (&dum->lock);
1656 value = dum->driver->setup (&dum->gadget,
1657 &setup);
1658 spin_lock (&dum->lock);
1659
1660 if (value >= 0) {
1661 /* no delays (max 64KB data stage) */
1662 limit = 64*1024;
1663 goto treat_control_like_bulk;
1664 }
1665 /* error, see below */
1666 }
1667
1668 if (value < 0) {
1669 if (value != -EOPNOTSUPP)
d9b76251 1670 dev_dbg (udc_dev(dum),
1da177e4
LT
1671 "setup --> %d\n",
1672 value);
4d2f110c 1673 status = -EPIPE;
1da177e4
LT
1674 urb->actual_length = 0;
1675 }
1676
1677 goto return_urb;
1678 }
1679
1680 /* non-control requests */
1681 limit = total;
1682 switch (usb_pipetype (urb->pipe)) {
1683 case PIPE_ISOCHRONOUS:
1684 /* FIXME is it urb->interval since the last xfer?
1685 * use urb->iso_frame_desc[i].
1686 * complete whether or not ep has requests queued.
1687 * report random errors, to debug drivers.
1688 */
1689 limit = max (limit, periodic_bytes (dum, ep));
4d2f110c 1690 status = -ENOSYS;
1da177e4
LT
1691 break;
1692
1693 case PIPE_INTERRUPT:
1694 /* FIXME is it urb->interval since the last xfer?
1695 * this almost certainly polls too fast.
1696 */
1697 limit = max (limit, periodic_bytes (dum, ep));
1698 /* FALLTHROUGH */
1699
1700 // case PIPE_BULK: case PIPE_CONTROL:
1701 default:
1702 treat_control_like_bulk:
1703 ep->last_io = jiffies;
4d2f110c 1704 total = transfer(dum, urb, ep, limit, &status);
1da177e4
LT
1705 break;
1706 }
1707
1708 /* incomplete transfer? */
4d2f110c 1709 if (status == -EINPROGRESS)
1da177e4
LT
1710 continue;
1711
1712return_urb:
1da177e4
LT
1713 list_del (&urbp->urbp_list);
1714 kfree (urbp);
1715 if (ep)
1716 ep->already_seen = ep->setup_stage = 0;
1717
cdfcbd2c 1718 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
1da177e4 1719 spin_unlock (&dum->lock);
cdfcbd2c 1720 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
1da177e4
LT
1721 spin_lock (&dum->lock);
1722
1723 goto restart;
1724 }
1725
cdfcbd2c
TB
1726 if (list_empty(&dum_hcd->urbp_list)) {
1727 usb_put_dev(dum_hcd->udev);
1728 dum_hcd->udev = NULL;
1729 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
391eca9d 1730 /* want a 1 msec delay here */
cdfcbd2c 1731 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
1da177e4
LT
1732 }
1733
1734 spin_unlock_irqrestore (&dum->lock, flags);
1735}
1736
1737/*-------------------------------------------------------------------------*/
1738
1739#define PORT_C_MASK \
c2db8b5e
AS
1740 ((USB_PORT_STAT_C_CONNECTION \
1741 | USB_PORT_STAT_C_ENABLE \
1742 | USB_PORT_STAT_C_SUSPEND \
1743 | USB_PORT_STAT_C_OVERCURRENT \
1744 | USB_PORT_STAT_C_RESET) << 16)
1da177e4
LT
1745
1746static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1747{
cdfcbd2c 1748 struct dummy_hcd *dum_hcd;
1da177e4 1749 unsigned long flags;
391eca9d 1750 int retval = 0;
1da177e4 1751
cdfcbd2c 1752 dum_hcd = hcd_to_dummy_hcd(hcd);
1da177e4 1753
cdfcbd2c 1754 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
541c7d43 1755 if (!HCD_HW_ACCESSIBLE(hcd))
391eca9d 1756 goto done;
f1c39fad 1757
cdfcbd2c
TB
1758 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1759 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1760 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1761 set_link_state(dum_hcd);
f1c39fad
AS
1762 }
1763
cdfcbd2c 1764 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
1da177e4 1765 *buf = (1 << 1);
cdfcbd2c
TB
1766 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1767 dum_hcd->port_status);
1da177e4 1768 retval = 1;
cdfcbd2c 1769 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
391eca9d 1770 usb_hcd_resume_root_hub (hcd);
1da177e4 1771 }
391eca9d 1772done:
cdfcbd2c 1773 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1da177e4
LT
1774 return retval;
1775}
1776
1cd8fd28
TB
1777static inline void
1778ss_hub_descriptor(struct usb_hub_descriptor *desc)
1779{
1780 memset(desc, 0, sizeof *desc);
1781 desc->bDescriptorType = 0x2a;
1782 desc->bDescLength = 12;
1783 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1784 desc->bNbrPorts = 1;
1785 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1786 desc->u.ss.DeviceRemovable = 0xffff;
1787}
1788
1da177e4
LT
1789static inline void
1790hub_descriptor (struct usb_hub_descriptor *desc)
1791{
1792 memset (desc, 0, sizeof *desc);
1793 desc->bDescriptorType = 0x29;
1794 desc->bDescLength = 9;
fd05e720 1795 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1da177e4 1796 desc->bNbrPorts = 1;
dbe79bbe
JY
1797 desc->u.hs.DeviceRemovable[0] = 0xff;
1798 desc->u.hs.DeviceRemovable[1] = 0xff;
1da177e4
LT
1799}
1800
1801static int dummy_hub_control (
1802 struct usb_hcd *hcd,
1803 u16 typeReq,
1804 u16 wValue,
1805 u16 wIndex,
1806 char *buf,
1807 u16 wLength
1808) {
cdfcbd2c 1809 struct dummy_hcd *dum_hcd;
1da177e4
LT
1810 int retval = 0;
1811 unsigned long flags;
1812
541c7d43 1813 if (!HCD_HW_ACCESSIBLE(hcd))
391eca9d
AS
1814 return -ETIMEDOUT;
1815
cdfcbd2c
TB
1816 dum_hcd = hcd_to_dummy_hcd(hcd);
1817
1818 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1da177e4
LT
1819 switch (typeReq) {
1820 case ClearHubFeature:
1821 break;
1822 case ClearPortFeature:
1823 switch (wValue) {
1824 case USB_PORT_FEAT_SUSPEND:
1cd8fd28
TB
1825 if (hcd->speed == HCD_USB3) {
1826 dev_dbg(dummy_dev(dum_hcd),
1827 "USB_PORT_FEAT_SUSPEND req not "
1828 "supported for USB 3.0 roothub\n");
1829 goto error;
1830 }
cdfcbd2c 1831 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
1da177e4 1832 /* 20msec resume signaling */
cdfcbd2c
TB
1833 dum_hcd->resuming = 1;
1834 dum_hcd->re_timeout = jiffies +
f1c39fad 1835 msecs_to_jiffies(20);
1da177e4
LT
1836 }
1837 break;
1838 case USB_PORT_FEAT_POWER:
1cd8fd28
TB
1839 if (hcd->speed == HCD_USB3) {
1840 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1841 dev_dbg(dummy_dev(dum_hcd),
1842 "power-off\n");
1843 } else
1844 if (dum_hcd->port_status &
1845 USB_SS_PORT_STAT_POWER)
1846 dev_dbg(dummy_dev(dum_hcd),
1847 "power-off\n");
f1c39fad 1848 /* FALLS THROUGH */
1da177e4 1849 default:
cdfcbd2c
TB
1850 dum_hcd->port_status &= ~(1 << wValue);
1851 set_link_state(dum_hcd);
1da177e4
LT
1852 }
1853 break;
1854 case GetHubDescriptor:
1cd8fd28
TB
1855 if (hcd->speed == HCD_USB3 &&
1856 (wLength < USB_DT_SS_HUB_SIZE ||
1857 wValue != (USB_DT_SS_HUB << 8))) {
1858 dev_dbg(dummy_dev(dum_hcd),
1859 "Wrong hub descriptor type for "
1860 "USB 3.0 roothub.\n");
1861 goto error;
1862 }
1863 if (hcd->speed == HCD_USB3)
1864 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
1865 else
1866 hub_descriptor((struct usb_hub_descriptor *) buf);
1da177e4
LT
1867 break;
1868 case GetHubStatus:
551509d2 1869 *(__le32 *) buf = cpu_to_le32 (0);
1da177e4
LT
1870 break;
1871 case GetPortStatus:
1872 if (wIndex != 1)
1873 retval = -EPIPE;
1874
1875 /* whoever resets or resumes must GetPortStatus to
1876 * complete it!!
1877 */
cdfcbd2c
TB
1878 if (dum_hcd->resuming &&
1879 time_after_eq(jiffies, dum_hcd->re_timeout)) {
1880 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1881 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1da177e4 1882 }
cdfcbd2c
TB
1883 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
1884 time_after_eq(jiffies, dum_hcd->re_timeout)) {
1885 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
1886 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
1887 if (dum_hcd->dum->pullup) {
1888 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
1cd8fd28
TB
1889
1890 if (hcd->speed < HCD_USB3) {
1891 switch (dum_hcd->dum->gadget.speed) {
1892 case USB_SPEED_HIGH:
1893 dum_hcd->port_status |=
1894 USB_PORT_STAT_HIGH_SPEED;
1895 break;
1896 case USB_SPEED_LOW:
1897 dum_hcd->dum->gadget.ep0->
1898 maxpacket = 8;
1899 dum_hcd->port_status |=
1900 USB_PORT_STAT_LOW_SPEED;
1901 break;
1902 default:
1903 dum_hcd->dum->gadget.speed =
1904 USB_SPEED_FULL;
1905 break;
1906 }
1da177e4
LT
1907 }
1908 }
1909 }
cdfcbd2c
TB
1910 set_link_state(dum_hcd);
1911 ((__le16 *) buf)[0] = cpu_to_le16 (dum_hcd->port_status);
1912 ((__le16 *) buf)[1] = cpu_to_le16 (dum_hcd->port_status >> 16);
1da177e4
LT
1913 break;
1914 case SetHubFeature:
1915 retval = -EPIPE;
1916 break;
1917 case SetPortFeature:
1918 switch (wValue) {
1cd8fd28
TB
1919 case USB_PORT_FEAT_LINK_STATE:
1920 if (hcd->speed != HCD_USB3) {
1921 dev_dbg(dummy_dev(dum_hcd),
1922 "USB_PORT_FEAT_LINK_STATE req not "
1923 "supported for USB 2.0 roothub\n");
1924 goto error;
1925 }
1926 /*
1927 * Since this is dummy we don't have an actual link so
1928 * there is nothing to do for the SET_LINK_STATE cmd
1929 */
1930 break;
1931 case USB_PORT_FEAT_U1_TIMEOUT:
1932 case USB_PORT_FEAT_U2_TIMEOUT:
1933 /* TODO: add suspend/resume support! */
1934 if (hcd->speed != HCD_USB3) {
1935 dev_dbg(dummy_dev(dum_hcd),
1936 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
1937 "supported for USB 2.0 roothub\n");
1938 goto error;
1939 }
1940 break;
1da177e4 1941 case USB_PORT_FEAT_SUSPEND:
1cd8fd28
TB
1942 /* Applicable only for USB2.0 hub */
1943 if (hcd->speed == HCD_USB3) {
1944 dev_dbg(dummy_dev(dum_hcd),
1945 "USB_PORT_FEAT_SUSPEND req not "
1946 "supported for USB 3.0 roothub\n");
1947 goto error;
1948 }
cdfcbd2c
TB
1949 if (dum_hcd->active) {
1950 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
f1c39fad
AS
1951
1952 /* HNP would happen here; for now we
1953 * assume b_bus_req is always true.
1954 */
cdfcbd2c 1955 set_link_state(dum_hcd);
f1c39fad 1956 if (((1 << USB_DEVICE_B_HNP_ENABLE)
cdfcbd2c
TB
1957 & dum_hcd->dum->devstatus) != 0)
1958 dev_dbg(dummy_dev(dum_hcd),
5742b0c9 1959 "no HNP yet!\n");
1da177e4
LT
1960 }
1961 break;
f1c39fad 1962 case USB_PORT_FEAT_POWER:
1cd8fd28
TB
1963 if (hcd->speed == HCD_USB3)
1964 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
1965 else
1966 dum_hcd->port_status |= USB_PORT_STAT_POWER;
cdfcbd2c 1967 set_link_state(dum_hcd);
f1c39fad 1968 break;
1cd8fd28
TB
1969 case USB_PORT_FEAT_BH_PORT_RESET:
1970 /* Applicable only for USB3.0 hub */
1971 if (hcd->speed != HCD_USB3) {
1972 dev_dbg(dummy_dev(dum_hcd),
1973 "USB_PORT_FEAT_BH_PORT_RESET req not "
1974 "supported for USB 2.0 roothub\n");
1975 goto error;
1976 }
1977 /* FALLS THROUGH */
1da177e4 1978 case USB_PORT_FEAT_RESET:
f1c39fad 1979 /* if it's already enabled, disable */
1cd8fd28
TB
1980 if (hcd->speed == HCD_USB3) {
1981 dum_hcd->port_status = 0;
1982 dum_hcd->port_status =
1983 (USB_SS_PORT_STAT_POWER |
1984 USB_PORT_STAT_CONNECTION |
1985 USB_PORT_STAT_RESET);
1986 } else
1987 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
f1c39fad
AS
1988 | USB_PORT_STAT_LOW_SPEED
1989 | USB_PORT_STAT_HIGH_SPEED);
cdfcbd2c
TB
1990 /*
1991 * We want to reset device status. All but the
1992 * Self powered feature
1993 */
1994 dum_hcd->dum->devstatus &=
1995 (1 << USB_DEVICE_SELF_POWERED);
1cd8fd28
TB
1996 /*
1997 * FIXME USB3.0: what is the correct reset signaling
1998 * interval? Is it still 50msec as for HS?
1999 */
cdfcbd2c 2000 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
f1c39fad 2001 /* FALLS THROUGH */
1da177e4 2002 default:
1cd8fd28
TB
2003 if (hcd->speed == HCD_USB3) {
2004 if ((dum_hcd->port_status &
2005 USB_SS_PORT_STAT_POWER) != 0) {
2006 dum_hcd->port_status |= (1 << wValue);
2007 set_link_state(dum_hcd);
2008 }
2009 } else
2010 if ((dum_hcd->port_status &
2011 USB_PORT_STAT_POWER) != 0) {
2012 dum_hcd->port_status |= (1 << wValue);
2013 set_link_state(dum_hcd);
2014 }
2015 }
2016 break;
2017 case GetPortErrorCount:
2018 if (hcd->speed != HCD_USB3) {
2019 dev_dbg(dummy_dev(dum_hcd),
2020 "GetPortErrorCount req not "
2021 "supported for USB 2.0 roothub\n");
2022 goto error;
2023 }
2024 /* We'll always return 0 since this is a dummy hub */
2025 *(__le32 *) buf = cpu_to_le32(0);
2026 break;
2027 case SetHubDepth:
2028 if (hcd->speed != HCD_USB3) {
2029 dev_dbg(dummy_dev(dum_hcd),
2030 "SetHubDepth req not supported for "
2031 "USB 2.0 roothub\n");
2032 goto error;
1da177e4
LT
2033 }
2034 break;
1da177e4 2035 default:
cdfcbd2c 2036 dev_dbg(dummy_dev(dum_hcd),
1da177e4
LT
2037 "hub control req%04x v%04x i%04x l%d\n",
2038 typeReq, wValue, wIndex, wLength);
1cd8fd28 2039error:
1da177e4
LT
2040 /* "protocol stall" on error */
2041 retval = -EPIPE;
2042 }
cdfcbd2c 2043 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
685eb93f 2044
cdfcbd2c 2045 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
685eb93f 2046 usb_hcd_poll_rh_status (hcd);
1da177e4
LT
2047 return retval;
2048}
2049
0c0382e3 2050static int dummy_bus_suspend (struct usb_hcd *hcd)
391eca9d 2051{
cdfcbd2c 2052 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
391eca9d 2053
441b62c1 2054 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
3cf0a22e 2055
cdfcbd2c
TB
2056 spin_lock_irq(&dum_hcd->dum->lock);
2057 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2058 set_link_state(dum_hcd);
3cf0a22e 2059 hcd->state = HC_STATE_SUSPENDED;
cdfcbd2c 2060 spin_unlock_irq(&dum_hcd->dum->lock);
391eca9d
AS
2061 return 0;
2062}
2063
0c0382e3 2064static int dummy_bus_resume (struct usb_hcd *hcd)
391eca9d 2065{
cdfcbd2c 2066 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
3cf0a22e
AS
2067 int rc = 0;
2068
441b62c1 2069 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
391eca9d 2070
cdfcbd2c 2071 spin_lock_irq(&dum_hcd->dum->lock);
541c7d43 2072 if (!HCD_HW_ACCESSIBLE(hcd)) {
cfa59dab 2073 rc = -ESHUTDOWN;
3cf0a22e 2074 } else {
cdfcbd2c
TB
2075 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2076 set_link_state(dum_hcd);
2077 if (!list_empty(&dum_hcd->urbp_list))
2078 mod_timer(&dum_hcd->timer, jiffies);
3cf0a22e
AS
2079 hcd->state = HC_STATE_RUNNING;
2080 }
cdfcbd2c 2081 spin_unlock_irq(&dum_hcd->dum->lock);
3cf0a22e 2082 return rc;
391eca9d 2083}
1da177e4
LT
2084
2085/*-------------------------------------------------------------------------*/
2086
2087static inline ssize_t
2088show_urb (char *buf, size_t size, struct urb *urb)
2089{
2090 int ep = usb_pipeendpoint (urb->pipe);
2091
2092 return snprintf (buf, size,
2093 "urb/%p %s ep%d%s%s len %d/%d\n",
2094 urb,
2095 ({ char *s;
2096 switch (urb->dev->speed) {
7c884fe4
TB
2097 case USB_SPEED_LOW:
2098 s = "ls";
2099 break;
2100 case USB_SPEED_FULL:
2101 s = "fs";
2102 break;
2103 case USB_SPEED_HIGH:
2104 s = "hs";
2105 break;
1cd8fd28
TB
2106 case USB_SPEED_SUPER:
2107 s = "ss";
2108 break;
7c884fe4
TB
2109 default:
2110 s = "?";
2111 break;
1da177e4
LT
2112 }; s; }),
2113 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
2114 ({ char *s; \
2115 switch (usb_pipetype (urb->pipe)) { \
7c884fe4
TB
2116 case PIPE_CONTROL: \
2117 s = ""; \
2118 break; \
2119 case PIPE_BULK: \
2120 s = "-bulk"; \
2121 break; \
2122 case PIPE_INTERRUPT: \
2123 s = "-int"; \
2124 break; \
2125 default: \
2126 s = "-iso"; \
2127 break; \
1da177e4
LT
2128 }; s;}),
2129 urb->actual_length, urb->transfer_buffer_length);
2130}
2131
2132static ssize_t
10523b3b 2133show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
2134{
2135 struct usb_hcd *hcd = dev_get_drvdata (dev);
cdfcbd2c 2136 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
1da177e4
LT
2137 struct urbp *urbp;
2138 size_t size = 0;
2139 unsigned long flags;
2140
cdfcbd2c
TB
2141 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2142 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
1da177e4
LT
2143 size_t temp;
2144
2145 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
2146 buf += temp;
2147 size += temp;
2148 }
cdfcbd2c 2149 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1da177e4
LT
2150
2151 return size;
2152}
2153static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
2154
1cd8fd28
TB
2155static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2156{
2157 init_timer(&dum_hcd->timer);
2158 dum_hcd->timer.function = dummy_timer;
2159 dum_hcd->timer.data = (unsigned long)dum_hcd;
2160 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2161 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2162 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2163 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2164 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2165#ifdef CONFIG_USB_OTG
2166 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2167#endif
2168 return 0;
2169
2170 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2171 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2172}
2173
cdfcbd2c 2174static int dummy_start(struct usb_hcd *hcd)
1da177e4 2175{
cdfcbd2c 2176 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
1da177e4
LT
2177
2178 /*
2179 * MASTER side init ... we emulate a root hub that'll only ever
2180 * talk to one device (the slave side). Also appears in sysfs,
2181 * just like more familiar pci-based HCDs.
2182 */
1cd8fd28
TB
2183 if (!usb_hcd_is_primary_hcd(hcd))
2184 return dummy_start_ss(dum_hcd);
2185
cdfcbd2c
TB
2186 spin_lock_init(&dum_hcd->dum->lock);
2187 init_timer(&dum_hcd->timer);
2188 dum_hcd->timer.function = dummy_timer;
2189 dum_hcd->timer.data = (unsigned long)dum_hcd;
2190 dum_hcd->rh_state = DUMMY_RH_RUNNING;
1da177e4 2191
cdfcbd2c 2192 INIT_LIST_HEAD(&dum_hcd->urbp_list);
1da177e4 2193
caf29f62 2194 hcd->power_budget = POWER_BUDGET;
1da177e4 2195 hcd->state = HC_STATE_RUNNING;
685eb93f 2196 hcd->uses_new_polling = 1;
1da177e4 2197
5742b0c9
AS
2198#ifdef CONFIG_USB_OTG
2199 hcd->self.otg_port = 1;
2200#endif
2201
1da177e4 2202 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
cdfcbd2c 2203 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
1da177e4
LT
2204}
2205
2206static void dummy_stop (struct usb_hcd *hcd)
2207{
2208 struct dummy *dum;
2209
cdfcbd2c
TB
2210 dum = (hcd_to_dummy_hcd(hcd))->dum;
2211 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2212 usb_gadget_unregister_driver(dum->driver);
2213 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
1da177e4
LT
2214}
2215
2216/*-------------------------------------------------------------------------*/
2217
2218static int dummy_h_get_frame (struct usb_hcd *hcd)
2219{
2220 return dummy_g_get_frame (NULL);
2221}
2222
cdfcbd2c
TB
2223static int dummy_setup(struct usb_hcd *hcd)
2224{
2225 if (usb_hcd_is_primary_hcd(hcd)) {
2226 the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
2227 the_controller.hs_hcd->dum = &the_controller;
1cd8fd28
TB
2228 /*
2229 * Mark the first roothub as being USB 2.0.
2230 * The USB 3.0 roothub will be registered later by
2231 * dummy_hcd_probe()
2232 */
cdfcbd2c
TB
2233 hcd->speed = HCD_USB2;
2234 hcd->self.root_hub->speed = USB_SPEED_HIGH;
1cd8fd28
TB
2235 } else {
2236 the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
2237 the_controller.ss_hcd->dum = &the_controller;
2238 hcd->speed = HCD_USB3;
2239 hcd->self.root_hub->speed = USB_SPEED_SUPER;
cdfcbd2c
TB
2240 }
2241 return 0;
2242}
2243
1cd8fd28
TB
2244/* Change a group of bulk endpoints to support multiple stream IDs */
2245int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
2246 struct usb_host_endpoint **eps, unsigned int num_eps,
2247 unsigned int num_streams, gfp_t mem_flags)
2248{
2249 if (hcd->speed != HCD_USB3)
2250 dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
2251 "%s() - ERROR! Not supported for USB2.0 roothub\n",
2252 __func__);
2253 return 0;
2254}
2255
2256/* Reverts a group of bulk endpoints back to not using stream IDs. */
2257int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
2258 struct usb_host_endpoint **eps, unsigned int num_eps,
2259 gfp_t mem_flags)
2260{
2261 if (hcd->speed != HCD_USB3)
2262 dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
2263 "%s() - ERROR! Not supported for USB2.0 roothub\n",
2264 __func__);
2265 return 0;
2266}
2267
2268static struct hc_driver dummy_hcd = {
1da177e4
LT
2269 .description = (char *) driver_name,
2270 .product_desc = "Dummy host controller",
cdfcbd2c 2271 .hcd_priv_size = sizeof(struct dummy_hcd),
1da177e4 2272
1cd8fd28 2273 .flags = HCD_USB3 | HCD_SHARED,
1da177e4 2274
cdfcbd2c 2275 .reset = dummy_setup,
1da177e4
LT
2276 .start = dummy_start,
2277 .stop = dummy_stop,
2278
2279 .urb_enqueue = dummy_urb_enqueue,
2280 .urb_dequeue = dummy_urb_dequeue,
2281
2282 .get_frame_number = dummy_h_get_frame,
2283
2284 .hub_status_data = dummy_hub_status,
2285 .hub_control = dummy_hub_control,
0c0382e3
AS
2286 .bus_suspend = dummy_bus_suspend,
2287 .bus_resume = dummy_bus_resume,
1cd8fd28
TB
2288
2289 .alloc_streams = dummy_alloc_streams,
2290 .free_streams = dummy_free_streams,
1da177e4
LT
2291};
2292
8364d6b0 2293static int dummy_hcd_probe(struct platform_device *pdev)
1da177e4 2294{
cdfcbd2c 2295 struct usb_hcd *hs_hcd;
1cd8fd28 2296 struct usb_hcd *ss_hcd;
1da177e4
LT
2297 int retval;
2298
8364d6b0 2299 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
1da177e4 2300
1cd8fd28
TB
2301 if (!mod_data.is_super_speed)
2302 dummy_hcd.flags = HCD_USB2;
cdfcbd2c
TB
2303 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2304 if (!hs_hcd)
1da177e4 2305 return -ENOMEM;
cdfcbd2c 2306 hs_hcd->has_tt = 1;
1da177e4 2307
cdfcbd2c 2308 retval = usb_add_hcd(hs_hcd, 0, 0);
1da177e4 2309 if (retval != 0) {
cdfcbd2c 2310 usb_put_hcd(hs_hcd);
1cd8fd28 2311 return retval;
1da177e4 2312 }
1cd8fd28
TB
2313
2314 if (mod_data.is_super_speed) {
2315 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2316 dev_name(&pdev->dev), hs_hcd);
2317 if (!ss_hcd) {
2318 retval = -ENOMEM;
2319 goto dealloc_usb2_hcd;
2320 }
2321
2322 retval = usb_add_hcd(ss_hcd, 0, 0);
2323 if (retval)
2324 goto put_usb3_hcd;
2325 }
2326 return 0;
2327
2328put_usb3_hcd:
2329 usb_put_hcd(ss_hcd);
2330dealloc_usb2_hcd:
2331 usb_put_hcd(hs_hcd);
2332 the_controller.hs_hcd = the_controller.ss_hcd = NULL;
1da177e4
LT
2333 return retval;
2334}
2335
cdfcbd2c 2336static int dummy_hcd_remove(struct platform_device *pdev)
1da177e4 2337{
cdfcbd2c
TB
2338 struct dummy *dum;
2339
2340 dum = (hcd_to_dummy_hcd(platform_get_drvdata(pdev)))->dum;
1cd8fd28
TB
2341
2342 if (dum->ss_hcd) {
2343 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2344 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2345 }
2346
cdfcbd2c
TB
2347 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2348 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
1cd8fd28 2349
cdfcbd2c 2350 the_controller.hs_hcd = NULL;
1cd8fd28 2351 the_controller.ss_hcd = NULL;
1da177e4 2352
d9b76251 2353 return 0;
1da177e4
LT
2354}
2355
8364d6b0 2356static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
391eca9d
AS
2357{
2358 struct usb_hcd *hcd;
cdfcbd2c 2359 struct dummy_hcd *dum_hcd;
3cf0a22e 2360 int rc = 0;
391eca9d 2361
441b62c1 2362 dev_dbg (&pdev->dev, "%s\n", __func__);
391eca9d 2363
3cf0a22e 2364 hcd = platform_get_drvdata (pdev);
cdfcbd2c
TB
2365 dum_hcd = hcd_to_dummy_hcd(hcd);
2366 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
3cf0a22e
AS
2367 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2368 rc = -EBUSY;
2369 } else
2370 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2371 return rc;
391eca9d
AS
2372}
2373
8364d6b0 2374static int dummy_hcd_resume (struct platform_device *pdev)
391eca9d
AS
2375{
2376 struct usb_hcd *hcd;
2377
441b62c1 2378 dev_dbg (&pdev->dev, "%s\n", __func__);
391eca9d 2379
3cf0a22e
AS
2380 hcd = platform_get_drvdata (pdev);
2381 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
391eca9d
AS
2382 usb_hcd_poll_rh_status (hcd);
2383 return 0;
2384}
2385
3ae5eaec 2386static struct platform_driver dummy_hcd_driver = {
d9b76251
AS
2387 .probe = dummy_hcd_probe,
2388 .remove = dummy_hcd_remove,
391eca9d
AS
2389 .suspend = dummy_hcd_suspend,
2390 .resume = dummy_hcd_resume,
3ae5eaec
RK
2391 .driver = {
2392 .name = (char *) driver_name,
2393 .owner = THIS_MODULE,
2394 },
d9b76251 2395};
1da177e4 2396
d9b76251 2397/*-------------------------------------------------------------------------*/
1da177e4 2398
a89a2cd3
AS
2399static struct platform_device *the_udc_pdev;
2400static struct platform_device *the_hcd_pdev;
1da177e4
LT
2401
2402static int __init init (void)
2403{
a89a2cd3 2404 int retval = -ENOMEM;
1da177e4
LT
2405
2406 if (usb_disabled ())
2407 return -ENODEV;
d9b76251 2408
7eca4c5a
TB
2409 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2410 return -EINVAL;
2411
a89a2cd3
AS
2412 the_hcd_pdev = platform_device_alloc(driver_name, -1);
2413 if (!the_hcd_pdev)
1da177e4 2414 return retval;
a89a2cd3
AS
2415 the_udc_pdev = platform_device_alloc(gadget_name, -1);
2416 if (!the_udc_pdev)
2417 goto err_alloc_udc;
d9b76251 2418
a89a2cd3
AS
2419 retval = platform_driver_register(&dummy_hcd_driver);
2420 if (retval < 0)
2421 goto err_register_hcd_driver;
2422 retval = platform_driver_register(&dummy_udc_driver);
d9b76251
AS
2423 if (retval < 0)
2424 goto err_register_udc_driver;
2425
a89a2cd3 2426 retval = platform_device_add(the_hcd_pdev);
d9b76251 2427 if (retval < 0)
a89a2cd3 2428 goto err_add_hcd;
1cd8fd28
TB
2429 if (!the_controller.hs_hcd ||
2430 (!the_controller.ss_hcd && mod_data.is_super_speed)) {
865835fa
SAS
2431 /*
2432 * The hcd was added successfully but its probe function failed
2433 * for some reason.
2434 */
2435 retval = -EINVAL;
2436 goto err_add_udc;
2437 }
a89a2cd3 2438 retval = platform_device_add(the_udc_pdev);
d9b76251 2439 if (retval < 0)
a89a2cd3 2440 goto err_add_udc;
865835fa
SAS
2441 if (!platform_get_drvdata(the_udc_pdev)) {
2442 /*
2443 * The udc was added successfully but its probe function failed
2444 * for some reason.
2445 */
2446 retval = -EINVAL;
2447 goto err_probe_udc;
2448 }
d9b76251
AS
2449 return retval;
2450
865835fa
SAS
2451err_probe_udc:
2452 platform_device_del(the_udc_pdev);
a89a2cd3
AS
2453err_add_udc:
2454 platform_device_del(the_hcd_pdev);
2455err_add_hcd:
2456 platform_driver_unregister(&dummy_udc_driver);
d9b76251 2457err_register_udc_driver:
a89a2cd3
AS
2458 platform_driver_unregister(&dummy_hcd_driver);
2459err_register_hcd_driver:
2460 platform_device_put(the_udc_pdev);
2461err_alloc_udc:
2462 platform_device_put(the_hcd_pdev);
1da177e4
LT
2463 return retval;
2464}
2465module_init (init);
2466
2467static void __exit cleanup (void)
2468{
a89a2cd3
AS
2469 platform_device_unregister(the_udc_pdev);
2470 platform_device_unregister(the_hcd_pdev);
2471 platform_driver_unregister(&dummy_udc_driver);
2472 platform_driver_unregister(&dummy_hcd_driver);
1da177e4
LT
2473}
2474module_exit (cleanup);
This page took 0.779512 seconds and 5 git commands to generate.