USB: Remove usages of dev->power.power_state
[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
37#define DEBUG
38
1da177e4
LT
39#include <linux/module.h>
40#include <linux/kernel.h>
41#include <linux/delay.h>
42#include <linux/ioport.h>
1da177e4 43#include <linux/slab.h>
1da177e4
LT
44#include <linux/errno.h>
45#include <linux/init.h>
46#include <linux/timer.h>
47#include <linux/list.h>
48#include <linux/interrupt.h>
d052d1be 49#include <linux/platform_device.h>
1da177e4
LT
50#include <linux/usb.h>
51#include <linux/usb_gadget.h>
52
53#include <asm/byteorder.h>
54#include <asm/io.h>
55#include <asm/irq.h>
56#include <asm/system.h>
57#include <asm/unaligned.h>
58
59
60#include "../core/hcd.h"
61
62
63#define DRIVER_DESC "USB Host+Gadget Emulator"
391eca9d 64#define DRIVER_VERSION "02 May 2005"
1da177e4
LT
65
66static const char driver_name [] = "dummy_hcd";
67static const char driver_desc [] = "USB Host+Gadget Emulator";
68
69static const char gadget_name [] = "dummy_udc";
70
71MODULE_DESCRIPTION (DRIVER_DESC);
72MODULE_AUTHOR ("David Brownell");
73MODULE_LICENSE ("GPL");
74
75/*-------------------------------------------------------------------------*/
76
77/* gadget side driver data structres */
78struct dummy_ep {
79 struct list_head queue;
80 unsigned long last_io; /* jiffies timestamp */
81 struct usb_gadget *gadget;
82 const struct usb_endpoint_descriptor *desc;
83 struct usb_ep ep;
84 unsigned halted : 1;
85 unsigned already_seen : 1;
86 unsigned setup_stage : 1;
87};
88
89struct dummy_request {
90 struct list_head queue; /* ep's requests */
91 struct usb_request req;
92};
93
94static inline struct dummy_ep *usb_ep_to_dummy_ep (struct usb_ep *_ep)
95{
96 return container_of (_ep, struct dummy_ep, ep);
97}
98
99static inline struct dummy_request *usb_request_to_dummy_request
100 (struct usb_request *_req)
101{
102 return container_of (_req, struct dummy_request, req);
103}
104
105/*-------------------------------------------------------------------------*/
106
107/*
108 * Every device has ep0 for control requests, plus up to 30 more endpoints,
109 * in one of two types:
110 *
111 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
112 * number can be changed. Names like "ep-a" are used for this type.
113 *
114 * - Fixed Function: in other cases. some characteristics may be mutable;
115 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
116 *
117 * Gadget drivers are responsible for not setting up conflicting endpoint
118 * configurations, illegal or unsupported packet lengths, and so on.
119 */
120
121static const char ep0name [] = "ep0";
122
123static const char *const ep_name [] = {
124 ep0name, /* everyone has ep0 */
125
126 /* act like a net2280: high speed, six configurable endpoints */
127 "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
128
129 /* or like pxa250: fifteen fixed function endpoints */
130 "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
131 "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
132 "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
133 "ep15in-int",
134
135 /* or like sa1100: two fixed function endpoints */
136 "ep1out-bulk", "ep2in-bulk",
137};
52950ed4 138#define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
1da177e4 139
d9b76251
AS
140/*-------------------------------------------------------------------------*/
141
1da177e4
LT
142#define FIFO_SIZE 64
143
144struct urbp {
145 struct urb *urb;
146 struct list_head urbp_list;
147};
148
391eca9d
AS
149
150enum dummy_rh_state {
151 DUMMY_RH_RESET,
152 DUMMY_RH_SUSPENDED,
153 DUMMY_RH_RUNNING
154};
155
1da177e4
LT
156struct dummy {
157 spinlock_t lock;
158
159 /*
160 * SLAVE/GADGET side support
161 */
162 struct dummy_ep ep [DUMMY_ENDPOINTS];
163 int address;
164 struct usb_gadget gadget;
165 struct usb_gadget_driver *driver;
166 struct dummy_request fifo_req;
167 u8 fifo_buf [FIFO_SIZE];
168 u16 devstatus;
391eca9d 169 unsigned udc_suspended:1;
f1c39fad
AS
170 unsigned pullup:1;
171 unsigned active:1;
172 unsigned old_active:1;
1da177e4
LT
173
174 /*
175 * MASTER/HOST side support
176 */
391eca9d 177 enum dummy_rh_state rh_state;
1da177e4
LT
178 struct timer_list timer;
179 u32 port_status;
f1c39fad 180 u32 old_status;
1da177e4
LT
181 unsigned resuming:1;
182 unsigned long re_timeout;
183
184 struct usb_device *udev;
185 struct list_head urbp_list;
186};
187
188static inline struct dummy *hcd_to_dummy (struct usb_hcd *hcd)
189{
190 return (struct dummy *) (hcd->hcd_priv);
191}
192
193static inline struct usb_hcd *dummy_to_hcd (struct dummy *dum)
194{
195 return container_of((void *) dum, struct usb_hcd, hcd_priv);
196}
197
198static inline struct device *dummy_dev (struct dummy *dum)
199{
200 return dummy_to_hcd(dum)->self.controller;
201}
202
d9b76251
AS
203static inline struct device *udc_dev (struct dummy *dum)
204{
205 return dum->gadget.dev.parent;
206}
207
1da177e4
LT
208static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
209{
210 return container_of (ep->gadget, struct dummy, gadget);
211}
212
213static inline struct dummy *gadget_to_dummy (struct usb_gadget *gadget)
214{
215 return container_of (gadget, struct dummy, gadget);
216}
217
218static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
219{
220 return container_of (dev, struct dummy, gadget.dev);
221}
222
223static struct dummy *the_controller;
224
225/*-------------------------------------------------------------------------*/
226
f1c39fad
AS
227/* SLAVE/GADGET SIDE UTILITY ROUTINES */
228
229/* called with spinlock held */
230static void nuke (struct dummy *dum, struct dummy_ep *ep)
231{
232 while (!list_empty (&ep->queue)) {
233 struct dummy_request *req;
234
235 req = list_entry (ep->queue.next, struct dummy_request, queue);
236 list_del_init (&req->queue);
237 req->req.status = -ESHUTDOWN;
238
239 spin_unlock (&dum->lock);
240 req->req.complete (&ep->ep, &req->req);
241 spin_lock (&dum->lock);
242 }
243}
244
245/* caller must hold lock */
246static void
247stop_activity (struct dummy *dum)
248{
249 struct dummy_ep *ep;
250
251 /* prevent any more requests */
252 dum->address = 0;
253
254 /* The timer is left running so that outstanding URBs can fail */
255
256 /* nuke any pending requests first, so driver i/o is quiesced */
257 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
258 nuke (dum, ep);
259
260 /* driver now does any non-usb quiescing necessary */
261}
262
263/* caller must hold lock */
264static void
265set_link_state (struct dummy *dum)
266{
267 dum->active = 0;
268 if ((dum->port_status & USB_PORT_STAT_POWER) == 0)
269 dum->port_status = 0;
391eca9d
AS
270
271 /* UDC suspend must cause a disconnect */
272 else if (!dum->pullup || dum->udc_suspended) {
f1c39fad
AS
273 dum->port_status &= ~(USB_PORT_STAT_CONNECTION |
274 USB_PORT_STAT_ENABLE |
275 USB_PORT_STAT_LOW_SPEED |
276 USB_PORT_STAT_HIGH_SPEED |
277 USB_PORT_STAT_SUSPEND);
278 if ((dum->old_status & USB_PORT_STAT_CONNECTION) != 0)
279 dum->port_status |= (USB_PORT_STAT_C_CONNECTION << 16);
280 } else {
281 dum->port_status |= USB_PORT_STAT_CONNECTION;
282 if ((dum->old_status & USB_PORT_STAT_CONNECTION) == 0)
283 dum->port_status |= (USB_PORT_STAT_C_CONNECTION << 16);
284 if ((dum->port_status & USB_PORT_STAT_ENABLE) == 0)
285 dum->port_status &= ~USB_PORT_STAT_SUSPEND;
391eca9d
AS
286 else if ((dum->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
287 dum->rh_state != DUMMY_RH_SUSPENDED)
f1c39fad
AS
288 dum->active = 1;
289 }
290
291 if ((dum->port_status & USB_PORT_STAT_ENABLE) == 0 || dum->active)
292 dum->resuming = 0;
293
294 if ((dum->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
295 (dum->port_status & USB_PORT_STAT_RESET) != 0) {
296 if ((dum->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
297 (dum->old_status & USB_PORT_STAT_RESET) == 0 &&
298 dum->driver) {
299 stop_activity (dum);
300 spin_unlock (&dum->lock);
301 dum->driver->disconnect (&dum->gadget);
302 spin_lock (&dum->lock);
303 }
304 } else if (dum->active != dum->old_active) {
305 if (dum->old_active && dum->driver->suspend) {
306 spin_unlock (&dum->lock);
307 dum->driver->suspend (&dum->gadget);
308 spin_lock (&dum->lock);
309 } else if (!dum->old_active && dum->driver->resume) {
310 spin_unlock (&dum->lock);
311 dum->driver->resume (&dum->gadget);
312 spin_lock (&dum->lock);
313 }
314 }
315
316 dum->old_status = dum->port_status;
317 dum->old_active = dum->active;
318}
319
320/*-------------------------------------------------------------------------*/
321
1da177e4
LT
322/* SLAVE/GADGET SIDE DRIVER
323 *
324 * This only tracks gadget state. All the work is done when the host
325 * side tries some (emulated) i/o operation. Real device controller
326 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
327 */
328
329#define is_enabled(dum) \
330 (dum->port_status & USB_PORT_STAT_ENABLE)
331
332static int
333dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
334{
335 struct dummy *dum;
336 struct dummy_ep *ep;
337 unsigned max;
338 int retval;
339
340 ep = usb_ep_to_dummy_ep (_ep);
341 if (!_ep || !desc || ep->desc || _ep->name == ep0name
342 || desc->bDescriptorType != USB_DT_ENDPOINT)
343 return -EINVAL;
344 dum = ep_to_dummy (ep);
345 if (!dum->driver || !is_enabled (dum))
346 return -ESHUTDOWN;
347 max = le16_to_cpu(desc->wMaxPacketSize) & 0x3ff;
348
349 /* drivers must not request bad settings, since lower levels
350 * (hardware or its drivers) may not check. some endpoints
351 * can't do iso, many have maxpacket limitations, etc.
352 *
353 * since this "hardware" driver is here to help debugging, we
354 * have some extra sanity checks. (there could be more though,
355 * especially for "ep9out" style fixed function ones.)
356 */
357 retval = -EINVAL;
358 switch (desc->bmAttributes & 0x03) {
359 case USB_ENDPOINT_XFER_BULK:
360 if (strstr (ep->ep.name, "-iso")
361 || strstr (ep->ep.name, "-int")) {
362 goto done;
363 }
364 switch (dum->gadget.speed) {
365 case USB_SPEED_HIGH:
366 if (max == 512)
367 break;
368 /* conserve return statements */
369 default:
370 switch (max) {
371 case 8: case 16: case 32: case 64:
372 /* we'll fake any legal size */
373 break;
374 default:
375 case USB_SPEED_LOW:
376 goto done;
377 }
378 }
379 break;
380 case USB_ENDPOINT_XFER_INT:
381 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
382 goto done;
383 /* real hardware might not handle all packet sizes */
384 switch (dum->gadget.speed) {
385 case USB_SPEED_HIGH:
386 if (max <= 1024)
387 break;
388 /* save a return statement */
389 case USB_SPEED_FULL:
390 if (max <= 64)
391 break;
392 /* save a return statement */
393 default:
394 if (max <= 8)
395 break;
396 goto done;
397 }
398 break;
399 case USB_ENDPOINT_XFER_ISOC:
400 if (strstr (ep->ep.name, "-bulk")
401 || strstr (ep->ep.name, "-int"))
402 goto done;
403 /* real hardware might not handle all packet sizes */
404 switch (dum->gadget.speed) {
405 case USB_SPEED_HIGH:
406 if (max <= 1024)
407 break;
408 /* save a return statement */
409 case USB_SPEED_FULL:
410 if (max <= 1023)
411 break;
412 /* save a return statement */
413 default:
414 goto done;
415 }
416 break;
417 default:
418 /* few chips support control except on ep0 */
419 goto done;
420 }
421
422 _ep->maxpacket = max;
423 ep->desc = desc;
424
d9b76251 425 dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
1da177e4
LT
426 _ep->name,
427 desc->bEndpointAddress & 0x0f,
428 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
429 ({ char *val;
430 switch (desc->bmAttributes & 0x03) {
431 case USB_ENDPOINT_XFER_BULK: val = "bulk"; break;
432 case USB_ENDPOINT_XFER_ISOC: val = "iso"; break;
433 case USB_ENDPOINT_XFER_INT: val = "intr"; break;
434 default: val = "ctrl"; break;
435 }; val; }),
436 max);
437
438 /* at this point real hardware should be NAKing transfers
439 * to that endpoint, until a buffer is queued to it.
440 */
441 retval = 0;
442done:
443 return retval;
444}
445
1da177e4
LT
446static int dummy_disable (struct usb_ep *_ep)
447{
448 struct dummy_ep *ep;
449 struct dummy *dum;
450 unsigned long flags;
451 int retval;
452
453 ep = usb_ep_to_dummy_ep (_ep);
454 if (!_ep || !ep->desc || _ep->name == ep0name)
455 return -EINVAL;
456 dum = ep_to_dummy (ep);
457
458 spin_lock_irqsave (&dum->lock, flags);
459 ep->desc = NULL;
460 retval = 0;
461 nuke (dum, ep);
462 spin_unlock_irqrestore (&dum->lock, flags);
463
d9b76251 464 dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name);
1da177e4
LT
465 return retval;
466}
467
468static struct usb_request *
55016f10 469dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
1da177e4
LT
470{
471 struct dummy_ep *ep;
472 struct dummy_request *req;
473
474 if (!_ep)
475 return NULL;
476 ep = usb_ep_to_dummy_ep (_ep);
477
7039f422 478 req = kzalloc(sizeof(*req), mem_flags);
1da177e4
LT
479 if (!req)
480 return NULL;
1da177e4
LT
481 INIT_LIST_HEAD (&req->queue);
482 return &req->req;
483}
484
485static void
486dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
487{
488 struct dummy_ep *ep;
489 struct dummy_request *req;
490
491 ep = usb_ep_to_dummy_ep (_ep);
492 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
493 return;
494
495 req = usb_request_to_dummy_request (_req);
496 WARN_ON (!list_empty (&req->queue));
497 kfree (req);
498}
499
500static void *
501dummy_alloc_buffer (
502 struct usb_ep *_ep,
503 unsigned bytes,
504 dma_addr_t *dma,
55016f10 505 gfp_t mem_flags
1da177e4
LT
506) {
507 char *retval;
508 struct dummy_ep *ep;
509 struct dummy *dum;
510
511 ep = usb_ep_to_dummy_ep (_ep);
512 dum = ep_to_dummy (ep);
513
514 if (!dum->driver)
515 return NULL;
516 retval = kmalloc (bytes, mem_flags);
517 *dma = (dma_addr_t) retval;
518 return retval;
519}
520
521static void
522dummy_free_buffer (
523 struct usb_ep *_ep,
524 void *buf,
525 dma_addr_t dma,
526 unsigned bytes
527) {
528 if (bytes)
529 kfree (buf);
530}
531
532static void
533fifo_complete (struct usb_ep *ep, struct usb_request *req)
534{
535}
536
537static int
5db539e4 538dummy_queue (struct usb_ep *_ep, struct usb_request *_req,
55016f10 539 gfp_t mem_flags)
1da177e4
LT
540{
541 struct dummy_ep *ep;
542 struct dummy_request *req;
543 struct dummy *dum;
544 unsigned long flags;
545
546 req = usb_request_to_dummy_request (_req);
547 if (!_req || !list_empty (&req->queue) || !_req->complete)
548 return -EINVAL;
549
550 ep = usb_ep_to_dummy_ep (_ep);
551 if (!_ep || (!ep->desc && _ep->name != ep0name))
552 return -EINVAL;
553
554 dum = ep_to_dummy (ep);
555 if (!dum->driver || !is_enabled (dum))
556 return -ESHUTDOWN;
557
558#if 0
d9b76251 559 dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
1da177e4
LT
560 ep, _req, _ep->name, _req->length, _req->buf);
561#endif
562
563 _req->status = -EINPROGRESS;
564 _req->actual = 0;
565 spin_lock_irqsave (&dum->lock, flags);
566
567 /* implement an emulated single-request FIFO */
568 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
569 list_empty (&dum->fifo_req.queue) &&
570 list_empty (&ep->queue) &&
571 _req->length <= FIFO_SIZE) {
572 req = &dum->fifo_req;
573 req->req = *_req;
574 req->req.buf = dum->fifo_buf;
575 memcpy (dum->fifo_buf, _req->buf, _req->length);
576 req->req.context = dum;
577 req->req.complete = fifo_complete;
578
579 spin_unlock (&dum->lock);
580 _req->actual = _req->length;
581 _req->status = 0;
582 _req->complete (_ep, _req);
583 spin_lock (&dum->lock);
584 }
585 list_add_tail (&req->queue, &ep->queue);
586 spin_unlock_irqrestore (&dum->lock, flags);
587
588 /* real hardware would likely enable transfers here, in case
589 * it'd been left NAKing.
590 */
591 return 0;
592}
593
594static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
595{
596 struct dummy_ep *ep;
597 struct dummy *dum;
598 int retval = -EINVAL;
599 unsigned long flags;
600 struct dummy_request *req = NULL;
601
602 if (!_ep || !_req)
603 return retval;
604 ep = usb_ep_to_dummy_ep (_ep);
605 dum = ep_to_dummy (ep);
606
607 if (!dum->driver)
608 return -ESHUTDOWN;
609
b4dbda1a
AS
610 local_irq_save (flags);
611 spin_lock (&dum->lock);
1da177e4
LT
612 list_for_each_entry (req, &ep->queue, queue) {
613 if (&req->req == _req) {
614 list_del_init (&req->queue);
615 _req->status = -ECONNRESET;
616 retval = 0;
617 break;
618 }
619 }
b4dbda1a 620 spin_unlock (&dum->lock);
1da177e4
LT
621
622 if (retval == 0) {
d9b76251 623 dev_dbg (udc_dev(dum),
1da177e4
LT
624 "dequeued req %p from %s, len %d buf %p\n",
625 req, _ep->name, _req->length, _req->buf);
626 _req->complete (_ep, _req);
627 }
b4dbda1a 628 local_irq_restore (flags);
1da177e4
LT
629 return retval;
630}
631
632static int
633dummy_set_halt (struct usb_ep *_ep, int value)
634{
635 struct dummy_ep *ep;
636 struct dummy *dum;
637
638 if (!_ep)
639 return -EINVAL;
640 ep = usb_ep_to_dummy_ep (_ep);
641 dum = ep_to_dummy (ep);
642 if (!dum->driver)
643 return -ESHUTDOWN;
644 if (!value)
645 ep->halted = 0;
646 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
647 !list_empty (&ep->queue))
648 return -EAGAIN;
649 else
650 ep->halted = 1;
651 /* FIXME clear emulated data toggle too */
652 return 0;
653}
654
655static const struct usb_ep_ops dummy_ep_ops = {
656 .enable = dummy_enable,
657 .disable = dummy_disable,
658
659 .alloc_request = dummy_alloc_request,
660 .free_request = dummy_free_request,
661
662 .alloc_buffer = dummy_alloc_buffer,
663 .free_buffer = dummy_free_buffer,
664 /* map, unmap, ... eventually hook the "generic" dma calls */
665
666 .queue = dummy_queue,
667 .dequeue = dummy_dequeue,
668
669 .set_halt = dummy_set_halt,
670};
671
672/*-------------------------------------------------------------------------*/
673
674/* there are both host and device side versions of this call ... */
675static int dummy_g_get_frame (struct usb_gadget *_gadget)
676{
677 struct timeval tv;
678
679 do_gettimeofday (&tv);
680 return tv.tv_usec / 1000;
681}
682
683static int dummy_wakeup (struct usb_gadget *_gadget)
684{
685 struct dummy *dum;
686
687 dum = gadget_to_dummy (_gadget);
391eca9d 688 if (!(dum->devstatus & ( (1 << USB_DEVICE_B_HNP_ENABLE)
5742b0c9 689 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
1da177e4 690 return -EINVAL;
391eca9d
AS
691 if ((dum->port_status & USB_PORT_STAT_CONNECTION) == 0)
692 return -ENOLINK;
693 if ((dum->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
694 dum->rh_state != DUMMY_RH_SUSPENDED)
695 return -EIO;
696
697 /* FIXME: What if the root hub is suspended but the port isn't? */
1da177e4
LT
698
699 /* hub notices our request, issues downstream resume, etc */
700 dum->resuming = 1;
f1c39fad 701 dum->re_timeout = jiffies + msecs_to_jiffies(20);
685eb93f 702 mod_timer (&dummy_to_hcd (dum)->rh_timer, dum->re_timeout);
1da177e4
LT
703 return 0;
704}
705
706static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
707{
708 struct dummy *dum;
709
710 dum = gadget_to_dummy (_gadget);
711 if (value)
712 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
713 else
714 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
715 return 0;
716}
717
f1c39fad
AS
718static int dummy_pullup (struct usb_gadget *_gadget, int value)
719{
720 struct dummy *dum;
721 unsigned long flags;
722
723 dum = gadget_to_dummy (_gadget);
724 spin_lock_irqsave (&dum->lock, flags);
725 dum->pullup = (value != 0);
726 set_link_state (dum);
727 spin_unlock_irqrestore (&dum->lock, flags);
685eb93f
AS
728
729 usb_hcd_poll_rh_status (dummy_to_hcd (dum));
f1c39fad
AS
730 return 0;
731}
732
1da177e4
LT
733static const struct usb_gadget_ops dummy_ops = {
734 .get_frame = dummy_g_get_frame,
735 .wakeup = dummy_wakeup,
736 .set_selfpowered = dummy_set_selfpowered,
f1c39fad 737 .pullup = dummy_pullup,
1da177e4
LT
738};
739
740/*-------------------------------------------------------------------------*/
741
742/* "function" sysfs attribute */
743static ssize_t
10523b3b 744show_function (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
745{
746 struct dummy *dum = gadget_dev_to_dummy (dev);
747
748 if (!dum->driver || !dum->driver->function)
749 return 0;
750 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
751}
cc095b0b 752static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1da177e4
LT
753
754/*-------------------------------------------------------------------------*/
755
756/*
757 * Driver registration/unregistration.
758 *
759 * This is basically hardware-specific; there's usually only one real USB
760 * device (not host) controller since that's how USB devices are intended
761 * to work. So most implementations of these api calls will rely on the
762 * fact that only one driver will ever bind to the hardware. But curious
763 * hardware can be built with discrete components, so the gadget API doesn't
764 * require that assumption.
765 *
766 * For this emulator, it might be convenient to create a usb slave device
767 * for each driver that registers: just add to a big root hub.
768 */
769
1da177e4
LT
770int
771usb_gadget_register_driver (struct usb_gadget_driver *driver)
772{
773 struct dummy *dum = the_controller;
774 int retval, i;
775
776 if (!dum)
777 return -EINVAL;
778 if (dum->driver)
779 return -EBUSY;
6bea476c 780 if (!driver->bind || !driver->setup
1da177e4
LT
781 || driver->speed == USB_SPEED_UNKNOWN)
782 return -EINVAL;
783
784 /*
785 * SLAVE side init ... the layer above hardware, which
786 * can't enumerate without help from the driver we're binding.
787 */
5742b0c9 788
1da177e4 789 dum->devstatus = 0;
1da177e4
LT
790
791 INIT_LIST_HEAD (&dum->gadget.ep_list);
792 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
793 struct dummy_ep *ep = &dum->ep [i];
794
795 if (!ep_name [i])
796 break;
797 ep->ep.name = ep_name [i];
798 ep->ep.ops = &dummy_ep_ops;
799 list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
800 ep->halted = ep->already_seen = ep->setup_stage = 0;
801 ep->ep.maxpacket = ~0;
802 ep->last_io = jiffies;
803 ep->gadget = &dum->gadget;
804 ep->desc = NULL;
805 INIT_LIST_HEAD (&ep->queue);
806 }
807
808 dum->gadget.ep0 = &dum->ep [0].ep;
809 dum->ep [0].ep.maxpacket = 64;
810 list_del_init (&dum->ep [0].ep.ep_list);
811 INIT_LIST_HEAD(&dum->fifo_req.queue);
812
813 dum->driver = driver;
814 dum->gadget.dev.driver = &driver->driver;
d9b76251 815 dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
1da177e4 816 driver->driver.name);
efd54a36
AS
817 if ((retval = driver->bind (&dum->gadget)) != 0)
818 goto err_bind_gadget;
1da177e4 819
1da177e4 820 driver->driver.bus = dum->gadget.dev.parent->bus;
efd54a36
AS
821 if ((retval = driver_register (&driver->driver)) != 0)
822 goto err_register;
823 if ((retval = device_bind_driver (&dum->gadget.dev)) != 0)
824 goto err_bind_driver;
1da177e4
LT
825
826 /* khubd will enumerate this in a while */
f1c39fad
AS
827 spin_lock_irq (&dum->lock);
828 dum->pullup = 1;
829 set_link_state (dum);
830 spin_unlock_irq (&dum->lock);
685eb93f
AS
831
832 usb_hcd_poll_rh_status (dummy_to_hcd (dum));
1da177e4 833 return 0;
efd54a36
AS
834
835err_bind_driver:
836 driver_unregister (&driver->driver);
837err_register:
6bea476c
DB
838 if (driver->unbind)
839 driver->unbind (&dum->gadget);
efd54a36
AS
840 spin_lock_irq (&dum->lock);
841 dum->pullup = 0;
842 set_link_state (dum);
843 spin_unlock_irq (&dum->lock);
844err_bind_gadget:
845 dum->driver = NULL;
846 dum->gadget.dev.driver = NULL;
847 return retval;
1da177e4
LT
848}
849EXPORT_SYMBOL (usb_gadget_register_driver);
850
1da177e4
LT
851int
852usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
853{
854 struct dummy *dum = the_controller;
855 unsigned long flags;
856
857 if (!dum)
858 return -ENODEV;
6bea476c 859 if (!driver || driver != dum->driver || !driver->unbind)
1da177e4
LT
860 return -EINVAL;
861
d9b76251 862 dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
1da177e4
LT
863 driver->driver.name);
864
865 spin_lock_irqsave (&dum->lock, flags);
f1c39fad
AS
866 dum->pullup = 0;
867 set_link_state (dum);
1da177e4
LT
868 spin_unlock_irqrestore (&dum->lock, flags);
869
870 driver->unbind (&dum->gadget);
871 dum->driver = NULL;
872
873 device_release_driver (&dum->gadget.dev);
1da177e4
LT
874 driver_unregister (&driver->driver);
875
f1c39fad
AS
876 spin_lock_irqsave (&dum->lock, flags);
877 dum->pullup = 0;
878 set_link_state (dum);
879 spin_unlock_irqrestore (&dum->lock, flags);
880
685eb93f 881 usb_hcd_poll_rh_status (dummy_to_hcd (dum));
1da177e4
LT
882 return 0;
883}
884EXPORT_SYMBOL (usb_gadget_unregister_driver);
885
886#undef is_enabled
887
cc095b0b
AS
888/* just declare this in any driver that really need it */
889extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
890
1da177e4
LT
891int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
892{
893 return -ENOSYS;
894}
895EXPORT_SYMBOL (net2280_set_fifo_mode);
896
d9b76251
AS
897
898/* The gadget structure is stored inside the hcd structure and will be
899 * released along with it. */
900static void
901dummy_gadget_release (struct device *dev)
902{
d9b76251
AS
903 struct dummy *dum = gadget_dev_to_dummy (dev);
904
17200583 905 usb_put_hcd (dummy_to_hcd (dum));
d9b76251
AS
906}
907
8364d6b0 908static int dummy_udc_probe (struct platform_device *pdev)
d9b76251
AS
909{
910 struct dummy *dum = the_controller;
911 int rc;
912
913 dum->gadget.name = gadget_name;
914 dum->gadget.ops = &dummy_ops;
915 dum->gadget.is_dualspeed = 1;
916
917 /* maybe claim OTG support, though we won't complete HNP */
918 dum->gadget.is_otg = (dummy_to_hcd(dum)->self.otg_port != 0);
919
920 strcpy (dum->gadget.dev.bus_id, "gadget");
8364d6b0 921 dum->gadget.dev.parent = &pdev->dev;
d9b76251
AS
922 dum->gadget.dev.release = dummy_gadget_release;
923 rc = device_register (&dum->gadget.dev);
924 if (rc < 0)
925 return rc;
926
17200583 927 usb_get_hcd (dummy_to_hcd (dum));
d9b76251 928
8364d6b0 929 platform_set_drvdata (pdev, dum);
efd54a36
AS
930 rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
931 if (rc < 0)
932 device_unregister (&dum->gadget.dev);
d9b76251
AS
933 return rc;
934}
935
8364d6b0 936static int dummy_udc_remove (struct platform_device *pdev)
d9b76251 937{
8364d6b0 938 struct dummy *dum = platform_get_drvdata (pdev);
d9b76251 939
8364d6b0 940 platform_set_drvdata (pdev, NULL);
d9b76251
AS
941 device_remove_file (&dum->gadget.dev, &dev_attr_function);
942 device_unregister (&dum->gadget.dev);
943 return 0;
944}
945
8364d6b0 946static int dummy_udc_suspend (struct platform_device *pdev, pm_message_t state)
391eca9d 947{
8364d6b0 948 struct dummy *dum = platform_get_drvdata(pdev);
391eca9d 949
8364d6b0 950 dev_dbg (&pdev->dev, "%s\n", __FUNCTION__);
391eca9d
AS
951 spin_lock_irq (&dum->lock);
952 dum->udc_suspended = 1;
953 set_link_state (dum);
954 spin_unlock_irq (&dum->lock);
955
8364d6b0 956 pdev->dev.power.power_state = state;
391eca9d
AS
957 usb_hcd_poll_rh_status (dummy_to_hcd (dum));
958 return 0;
959}
960
8364d6b0 961static int dummy_udc_resume (struct platform_device *pdev)
391eca9d 962{
8364d6b0 963 struct dummy *dum = platform_get_drvdata(pdev);
391eca9d 964
8364d6b0 965 dev_dbg (&pdev->dev, "%s\n", __FUNCTION__);
391eca9d
AS
966 spin_lock_irq (&dum->lock);
967 dum->udc_suspended = 0;
968 set_link_state (dum);
969 spin_unlock_irq (&dum->lock);
970
8364d6b0 971 pdev->dev.power.power_state = PMSG_ON;
391eca9d
AS
972 usb_hcd_poll_rh_status (dummy_to_hcd (dum));
973 return 0;
974}
975
3ae5eaec 976static struct platform_driver dummy_udc_driver = {
d9b76251
AS
977 .probe = dummy_udc_probe,
978 .remove = dummy_udc_remove,
391eca9d
AS
979 .suspend = dummy_udc_suspend,
980 .resume = dummy_udc_resume,
3ae5eaec
RK
981 .driver = {
982 .name = (char *) gadget_name,
983 .owner = THIS_MODULE,
984 },
d9b76251
AS
985};
986
1da177e4
LT
987/*-------------------------------------------------------------------------*/
988
989/* MASTER/HOST SIDE DRIVER
990 *
991 * this uses the hcd framework to hook up to host side drivers.
992 * its root hub will only have one device, otherwise it acts like
993 * a normal host controller.
994 *
995 * when urbs are queued, they're just stuck on a list that we
996 * scan in a timer callback. that callback connects writes from
997 * the host with reads from the device, and so on, based on the
998 * usb 2.0 rules.
999 */
1000
1001static int dummy_urb_enqueue (
1002 struct usb_hcd *hcd,
1003 struct usb_host_endpoint *ep,
1004 struct urb *urb,
55016f10 1005 gfp_t mem_flags
1da177e4
LT
1006) {
1007 struct dummy *dum;
1008 struct urbp *urbp;
1009 unsigned long flags;
1010
1011 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1012 return -EINVAL;
1013
1014 urbp = kmalloc (sizeof *urbp, mem_flags);
1015 if (!urbp)
1016 return -ENOMEM;
1017 urbp->urb = urb;
1018
1019 dum = hcd_to_dummy (hcd);
1020 spin_lock_irqsave (&dum->lock, flags);
1021
1022 if (!dum->udev) {
1023 dum->udev = urb->dev;
1024 usb_get_dev (dum->udev);
1025 } else if (unlikely (dum->udev != urb->dev))
1026 dev_err (dummy_dev(dum), "usb_device address has changed!\n");
1027
1028 list_add_tail (&urbp->urbp_list, &dum->urbp_list);
1029 urb->hcpriv = urbp;
1030 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
1031 urb->error_count = 1; /* mark as a new urb */
1032
1033 /* kick the scheduler, it'll do the rest */
1034 if (!timer_pending (&dum->timer))
1035 mod_timer (&dum->timer, jiffies + 1);
1036
1037 spin_unlock_irqrestore (&dum->lock, flags);
1038 return 0;
1039}
1040
1041static int dummy_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
1042{
391eca9d
AS
1043 struct dummy *dum;
1044 unsigned long flags;
1045
1046 /* giveback happens automatically in timer callback,
1047 * so make sure the callback happens */
1048 dum = hcd_to_dummy (hcd);
1049 spin_lock_irqsave (&dum->lock, flags);
1050 if (dum->rh_state != DUMMY_RH_RUNNING && !list_empty(&dum->urbp_list))
1051 mod_timer (&dum->timer, jiffies);
1052 spin_unlock_irqrestore (&dum->lock, flags);
1da177e4
LT
1053 return 0;
1054}
1055
1056static void maybe_set_status (struct urb *urb, int status)
1057{
1058 spin_lock (&urb->lock);
1059 if (urb->status == -EINPROGRESS)
1060 urb->status = status;
1061 spin_unlock (&urb->lock);
1062}
1063
1064/* transfer up to a frame's worth; caller must own lock */
1065static int
1066transfer (struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit)
1067{
1068 struct dummy_request *req;
1069
1070top:
1071 /* if there's no request queued, the device is NAKing; return */
1072 list_for_each_entry (req, &ep->queue, queue) {
1073 unsigned host_len, dev_len, len;
1074 int is_short, to_host;
1075 int rescan = 0;
1076
1077 /* 1..N packets of ep->ep.maxpacket each ... the last one
1078 * may be short (including zero length).
1079 *
1080 * writer can send a zlp explicitly (length 0) or implicitly
1081 * (length mod maxpacket zero, and 'zero' flag); they always
1082 * terminate reads.
1083 */
1084 host_len = urb->transfer_buffer_length - urb->actual_length;
1085 dev_len = req->req.length - req->req.actual;
1086 len = min (host_len, dev_len);
1087
1088 /* FIXME update emulated data toggle too */
1089
1090 to_host = usb_pipein (urb->pipe);
1091 if (unlikely (len == 0))
1092 is_short = 1;
1093 else {
1094 char *ubuf, *rbuf;
1095
1096 /* not enough bandwidth left? */
1097 if (limit < ep->ep.maxpacket && limit < len)
1098 break;
1099 len = min (len, (unsigned) limit);
1100 if (len == 0)
1101 break;
1102
1103 /* use an extra pass for the final short packet */
1104 if (len > ep->ep.maxpacket) {
1105 rescan = 1;
1106 len -= (len % ep->ep.maxpacket);
1107 }
1108 is_short = (len % ep->ep.maxpacket) != 0;
1109
1110 /* else transfer packet(s) */
1111 ubuf = urb->transfer_buffer + urb->actual_length;
1112 rbuf = req->req.buf + req->req.actual;
1113 if (to_host)
1114 memcpy (ubuf, rbuf, len);
1115 else
1116 memcpy (rbuf, ubuf, len);
1117 ep->last_io = jiffies;
1118
1119 limit -= len;
1120 urb->actual_length += len;
1121 req->req.actual += len;
1122 }
1123
1124 /* short packets terminate, maybe with overflow/underflow.
1125 * it's only really an error to write too much.
1126 *
1127 * partially filling a buffer optionally blocks queue advances
1128 * (so completion handlers can clean up the queue) but we don't
1129 * need to emulate such data-in-flight. so we only show part
1130 * of the URB_SHORT_NOT_OK effect: completion status.
1131 */
1132 if (is_short) {
1133 if (host_len == dev_len) {
1134 req->req.status = 0;
1135 maybe_set_status (urb, 0);
1136 } else if (to_host) {
1137 req->req.status = 0;
1138 if (dev_len > host_len)
1139 maybe_set_status (urb, -EOVERFLOW);
1140 else
1141 maybe_set_status (urb,
1142 (urb->transfer_flags
1143 & URB_SHORT_NOT_OK)
1144 ? -EREMOTEIO : 0);
1145 } else if (!to_host) {
1146 maybe_set_status (urb, 0);
1147 if (host_len > dev_len)
1148 req->req.status = -EOVERFLOW;
1149 else
1150 req->req.status = 0;
1151 }
1152
1153 /* many requests terminate without a short packet */
1154 } else {
1155 if (req->req.length == req->req.actual
1156 && !req->req.zero)
1157 req->req.status = 0;
1158 if (urb->transfer_buffer_length == urb->actual_length
1159 && !(urb->transfer_flags
1160 & URB_ZERO_PACKET)) {
1161 maybe_set_status (urb, 0);
1162 }
1163 }
1164
1165 /* device side completion --> continuable */
1166 if (req->req.status != -EINPROGRESS) {
1167 list_del_init (&req->queue);
1168
1169 spin_unlock (&dum->lock);
1170 req->req.complete (&ep->ep, &req->req);
1171 spin_lock (&dum->lock);
1172
1173 /* requests might have been unlinked... */
1174 rescan = 1;
1175 }
1176
1177 /* host side completion --> terminate */
1178 if (urb->status != -EINPROGRESS)
1179 break;
1180
1181 /* rescan to continue with any other queued i/o */
1182 if (rescan)
1183 goto top;
1184 }
1185 return limit;
1186}
1187
1188static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1189{
1190 int limit = ep->ep.maxpacket;
1191
1192 if (dum->gadget.speed == USB_SPEED_HIGH) {
1193 int tmp;
1194
1195 /* high bandwidth mode */
1196 tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
1da177e4
LT
1197 tmp = (tmp >> 11) & 0x03;
1198 tmp *= 8 /* applies to entire frame */;
1199 limit += limit * tmp;
1200 }
1201 return limit;
1202}
1203
1204#define is_active(dum) ((dum->port_status & \
1205 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1206 USB_PORT_STAT_SUSPEND)) \
1207 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1208
1209static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1210{
1211 int i;
1212
1213 if (!is_active (dum))
1214 return NULL;
1215 if ((address & ~USB_DIR_IN) == 0)
1216 return &dum->ep [0];
1217 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1218 struct dummy_ep *ep = &dum->ep [i];
1219
1220 if (!ep->desc)
1221 continue;
1222 if (ep->desc->bEndpointAddress == address)
1223 return ep;
1224 }
1225 return NULL;
1226}
1227
1228#undef is_active
1229
1230#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1231#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1232#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1233#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1234#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1235#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1236
1237/* drive both sides of the transfers; looks like irq handlers to
1238 * both drivers except the callbacks aren't in_irq().
1239 */
1240static void dummy_timer (unsigned long _dum)
1241{
1242 struct dummy *dum = (struct dummy *) _dum;
1243 struct urbp *urbp, *tmp;
1244 unsigned long flags;
1245 int limit, total;
1246 int i;
1247
1248 /* simplistic model for one frame's bandwidth */
1249 switch (dum->gadget.speed) {
1250 case USB_SPEED_LOW:
1251 total = 8/*bytes*/ * 12/*packets*/;
1252 break;
1253 case USB_SPEED_FULL:
1254 total = 64/*bytes*/ * 19/*packets*/;
1255 break;
1256 case USB_SPEED_HIGH:
1257 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1258 break;
1259 default:
1260 dev_err (dummy_dev(dum), "bogus device speed\n");
1261 return;
1262 }
1263
1264 /* FIXME if HZ != 1000 this will probably misbehave ... */
1265
1266 /* look at each urb queued by the host side driver */
1267 spin_lock_irqsave (&dum->lock, flags);
1268
1269 if (!dum->udev) {
1270 dev_err (dummy_dev(dum),
1271 "timer fired with no URBs pending?\n");
1272 spin_unlock_irqrestore (&dum->lock, flags);
1273 return;
1274 }
1275
1276 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1277 if (!ep_name [i])
1278 break;
1279 dum->ep [i].already_seen = 0;
1280 }
1281
1282restart:
1283 list_for_each_entry_safe (urbp, tmp, &dum->urbp_list, urbp_list) {
1284 struct urb *urb;
1285 struct dummy_request *req;
1286 u8 address;
1287 struct dummy_ep *ep = NULL;
1288 int type;
1289
1290 urb = urbp->urb;
1291 if (urb->status != -EINPROGRESS) {
1292 /* likely it was just unlinked */
1293 goto return_urb;
391eca9d
AS
1294 } else if (dum->rh_state != DUMMY_RH_RUNNING)
1295 continue;
1da177e4
LT
1296 type = usb_pipetype (urb->pipe);
1297
1298 /* used up this frame's non-periodic bandwidth?
1299 * FIXME there's infinite bandwidth for control and
1300 * periodic transfers ... unrealistic.
1301 */
1302 if (total <= 0 && type == PIPE_BULK)
1303 continue;
1304
1305 /* find the gadget's ep for this request (if configured) */
1306 address = usb_pipeendpoint (urb->pipe);
1307 if (usb_pipein (urb->pipe))
1308 address |= USB_DIR_IN;
1309 ep = find_endpoint(dum, address);
1310 if (!ep) {
1311 /* set_configuration() disagreement */
1312 dev_dbg (dummy_dev(dum),
1313 "no ep configured for urb %p\n",
1314 urb);
1315 maybe_set_status (urb, -EPROTO);
1316 goto return_urb;
1317 }
1318
1319 if (ep->already_seen)
1320 continue;
1321 ep->already_seen = 1;
1322 if (ep == &dum->ep [0] && urb->error_count) {
1323 ep->setup_stage = 1; /* a new urb */
1324 urb->error_count = 0;
1325 }
1326 if (ep->halted && !ep->setup_stage) {
1327 /* NOTE: must not be iso! */
1328 dev_dbg (dummy_dev(dum), "ep %s halted, urb %p\n",
1329 ep->ep.name, urb);
1330 maybe_set_status (urb, -EPIPE);
1331 goto return_urb;
1332 }
1333 /* FIXME make sure both ends agree on maxpacket */
1334
1335 /* handle control requests */
1336 if (ep == &dum->ep [0] && ep->setup_stage) {
1337 struct usb_ctrlrequest setup;
1338 int value = 1;
1339 struct dummy_ep *ep2;
cc095b0b
AS
1340 unsigned w_index;
1341 unsigned w_value;
1da177e4
LT
1342
1343 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
cc095b0b
AS
1344 w_index = le16_to_cpu(setup.wIndex);
1345 w_value = le16_to_cpu(setup.wValue);
1346 if (le16_to_cpu(setup.wLength) !=
1347 urb->transfer_buffer_length) {
1da177e4
LT
1348 maybe_set_status (urb, -EOVERFLOW);
1349 goto return_urb;
1350 }
1351
1352 /* paranoia, in case of stale queued data */
1353 list_for_each_entry (req, &ep->queue, queue) {
1354 list_del_init (&req->queue);
1355 req->req.status = -EOVERFLOW;
d9b76251 1356 dev_dbg (udc_dev(dum), "stale req = %p\n",
1da177e4
LT
1357 req);
1358
1359 spin_unlock (&dum->lock);
1360 req->req.complete (&ep->ep, &req->req);
1361 spin_lock (&dum->lock);
1362 ep->already_seen = 0;
1363 goto restart;
1364 }
1365
1366 /* gadget driver never sees set_address or operations
1367 * on standard feature flags. some hardware doesn't
1368 * even expose them.
1369 */
1370 ep->last_io = jiffies;
1371 ep->setup_stage = 0;
1372 ep->halted = 0;
1373 switch (setup.bRequest) {
1374 case USB_REQ_SET_ADDRESS:
1375 if (setup.bRequestType != Dev_Request)
1376 break;
cc095b0b 1377 dum->address = w_value;
1da177e4 1378 maybe_set_status (urb, 0);
d9b76251 1379 dev_dbg (udc_dev(dum), "set_address = %d\n",
cc095b0b 1380 w_value);
1da177e4
LT
1381 value = 0;
1382 break;
1383 case USB_REQ_SET_FEATURE:
1384 if (setup.bRequestType == Dev_Request) {
1385 value = 0;
cc095b0b 1386 switch (w_value) {
1da177e4
LT
1387 case USB_DEVICE_REMOTE_WAKEUP:
1388 break;
5742b0c9
AS
1389 case USB_DEVICE_B_HNP_ENABLE:
1390 dum->gadget.b_hnp_enable = 1;
1391 break;
1392 case USB_DEVICE_A_HNP_SUPPORT:
1393 dum->gadget.a_hnp_support = 1;
1394 break;
1395 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1396 dum->gadget.a_alt_hnp_support
1397 = 1;
1398 break;
1da177e4
LT
1399 default:
1400 value = -EOPNOTSUPP;
1401 }
1402 if (value == 0) {
1403 dum->devstatus |=
cc095b0b 1404 (1 << w_value);
1da177e4
LT
1405 maybe_set_status (urb, 0);
1406 }
1407
1408 } else if (setup.bRequestType == Ep_Request) {
1409 // endpoint halt
cc095b0b 1410 ep2 = find_endpoint (dum, w_index);
1da177e4
LT
1411 if (!ep2) {
1412 value = -EOPNOTSUPP;
1413 break;
1414 }
1415 ep2->halted = 1;
1416 value = 0;
1417 maybe_set_status (urb, 0);
1418 }
1419 break;
1420 case USB_REQ_CLEAR_FEATURE:
1421 if (setup.bRequestType == Dev_Request) {
cc095b0b 1422 switch (w_value) {
1da177e4
LT
1423 case USB_DEVICE_REMOTE_WAKEUP:
1424 dum->devstatus &= ~(1 <<
1425 USB_DEVICE_REMOTE_WAKEUP);
1426 value = 0;
1427 maybe_set_status (urb, 0);
1428 break;
1429 default:
1430 value = -EOPNOTSUPP;
1431 break;
1432 }
1433 } else if (setup.bRequestType == Ep_Request) {
1434 // endpoint halt
cc095b0b 1435 ep2 = find_endpoint (dum, w_index);
1da177e4
LT
1436 if (!ep2) {
1437 value = -EOPNOTSUPP;
1438 break;
1439 }
1440 ep2->halted = 0;
1441 value = 0;
1442 maybe_set_status (urb, 0);
1443 }
1444 break;
1445 case USB_REQ_GET_STATUS:
1446 if (setup.bRequestType == Dev_InRequest
1447 || setup.bRequestType
1448 == Intf_InRequest
1449 || setup.bRequestType
1450 == Ep_InRequest
1451 ) {
1452 char *buf;
1453
1454 // device: remote wakeup, selfpowered
1455 // interface: nothing
1456 // endpoint: halt
1457 buf = (char *)urb->transfer_buffer;
1458 if (urb->transfer_buffer_length > 0) {
1459 if (setup.bRequestType ==
1460 Ep_InRequest) {
cc095b0b 1461 ep2 = find_endpoint (dum, w_index);
1da177e4
LT
1462 if (!ep2) {
1463 value = -EOPNOTSUPP;
1464 break;
1465 }
1466 buf [0] = ep2->halted;
1467 } else if (setup.bRequestType ==
1468 Dev_InRequest) {
1469 buf [0] = (u8)
1470 dum->devstatus;
1471 } else
1472 buf [0] = 0;
1473 }
1474 if (urb->transfer_buffer_length > 1)
1475 buf [1] = 0;
1476 urb->actual_length = min (2,
1477 urb->transfer_buffer_length);
1478 value = 0;
1479 maybe_set_status (urb, 0);
1480 }
1481 break;
1482 }
1483
1484 /* gadget driver handles all other requests. block
1485 * until setup() returns; no reentrancy issues etc.
1486 */
1487 if (value > 0) {
1488 spin_unlock (&dum->lock);
1489 value = dum->driver->setup (&dum->gadget,
1490 &setup);
1491 spin_lock (&dum->lock);
1492
1493 if (value >= 0) {
1494 /* no delays (max 64KB data stage) */
1495 limit = 64*1024;
1496 goto treat_control_like_bulk;
1497 }
1498 /* error, see below */
1499 }
1500
1501 if (value < 0) {
1502 if (value != -EOPNOTSUPP)
d9b76251 1503 dev_dbg (udc_dev(dum),
1da177e4
LT
1504 "setup --> %d\n",
1505 value);
1506 maybe_set_status (urb, -EPIPE);
1507 urb->actual_length = 0;
1508 }
1509
1510 goto return_urb;
1511 }
1512
1513 /* non-control requests */
1514 limit = total;
1515 switch (usb_pipetype (urb->pipe)) {
1516 case PIPE_ISOCHRONOUS:
1517 /* FIXME is it urb->interval since the last xfer?
1518 * use urb->iso_frame_desc[i].
1519 * complete whether or not ep has requests queued.
1520 * report random errors, to debug drivers.
1521 */
1522 limit = max (limit, periodic_bytes (dum, ep));
1523 maybe_set_status (urb, -ENOSYS);
1524 break;
1525
1526 case PIPE_INTERRUPT:
1527 /* FIXME is it urb->interval since the last xfer?
1528 * this almost certainly polls too fast.
1529 */
1530 limit = max (limit, periodic_bytes (dum, ep));
1531 /* FALLTHROUGH */
1532
1533 // case PIPE_BULK: case PIPE_CONTROL:
1534 default:
1535 treat_control_like_bulk:
1536 ep->last_io = jiffies;
1537 total = transfer (dum, urb, ep, limit);
1538 break;
1539 }
1540
1541 /* incomplete transfer? */
1542 if (urb->status == -EINPROGRESS)
1543 continue;
1544
1545return_urb:
1546 urb->hcpriv = NULL;
1547 list_del (&urbp->urbp_list);
1548 kfree (urbp);
1549 if (ep)
1550 ep->already_seen = ep->setup_stage = 0;
1551
1552 spin_unlock (&dum->lock);
28431146 1553 usb_hcd_giveback_urb (dummy_to_hcd(dum), urb);
1da177e4
LT
1554 spin_lock (&dum->lock);
1555
1556 goto restart;
1557 }
1558
391eca9d 1559 if (list_empty (&dum->urbp_list)) {
1da177e4
LT
1560 usb_put_dev (dum->udev);
1561 dum->udev = NULL;
391eca9d
AS
1562 } else if (dum->rh_state == DUMMY_RH_RUNNING) {
1563 /* want a 1 msec delay here */
1564 mod_timer (&dum->timer, jiffies + msecs_to_jiffies(1));
1da177e4
LT
1565 }
1566
1567 spin_unlock_irqrestore (&dum->lock, flags);
1568}
1569
1570/*-------------------------------------------------------------------------*/
1571
1572#define PORT_C_MASK \
c2db8b5e
AS
1573 ((USB_PORT_STAT_C_CONNECTION \
1574 | USB_PORT_STAT_C_ENABLE \
1575 | USB_PORT_STAT_C_SUSPEND \
1576 | USB_PORT_STAT_C_OVERCURRENT \
1577 | USB_PORT_STAT_C_RESET) << 16)
1da177e4
LT
1578
1579static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1580{
1581 struct dummy *dum;
1582 unsigned long flags;
391eca9d 1583 int retval = 0;
1da177e4
LT
1584
1585 dum = hcd_to_dummy (hcd);
1586
1587 spin_lock_irqsave (&dum->lock, flags);
3cf0a22e 1588 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
391eca9d 1589 goto done;
f1c39fad
AS
1590
1591 if (dum->resuming && time_after_eq (jiffies, dum->re_timeout)) {
1592 dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1593 dum->port_status &= ~USB_PORT_STAT_SUSPEND;
1594 set_link_state (dum);
1595 }
1596
391eca9d 1597 if ((dum->port_status & PORT_C_MASK) != 0) {
1da177e4
LT
1598 *buf = (1 << 1);
1599 dev_dbg (dummy_dev(dum), "port status 0x%08x has changes\n",
391eca9d 1600 dum->port_status);
1da177e4 1601 retval = 1;
391eca9d
AS
1602 if (dum->rh_state == DUMMY_RH_SUSPENDED)
1603 usb_hcd_resume_root_hub (hcd);
1da177e4 1604 }
391eca9d 1605done:
1da177e4
LT
1606 spin_unlock_irqrestore (&dum->lock, flags);
1607 return retval;
1608}
1609
1610static inline void
1611hub_descriptor (struct usb_hub_descriptor *desc)
1612{
1613 memset (desc, 0, sizeof *desc);
1614 desc->bDescriptorType = 0x29;
1615 desc->bDescLength = 9;
cc095b0b
AS
1616 desc->wHubCharacteristics = (__force __u16)
1617 (__constant_cpu_to_le16 (0x0001));
1da177e4
LT
1618 desc->bNbrPorts = 1;
1619 desc->bitmap [0] = 0xff;
1620 desc->bitmap [1] = 0xff;
1621}
1622
1623static int dummy_hub_control (
1624 struct usb_hcd *hcd,
1625 u16 typeReq,
1626 u16 wValue,
1627 u16 wIndex,
1628 char *buf,
1629 u16 wLength
1630) {
1631 struct dummy *dum;
1632 int retval = 0;
1633 unsigned long flags;
1634
3cf0a22e 1635 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
391eca9d
AS
1636 return -ETIMEDOUT;
1637
1da177e4
LT
1638 dum = hcd_to_dummy (hcd);
1639 spin_lock_irqsave (&dum->lock, flags);
1640 switch (typeReq) {
1641 case ClearHubFeature:
1642 break;
1643 case ClearPortFeature:
1644 switch (wValue) {
1645 case USB_PORT_FEAT_SUSPEND:
c2db8b5e 1646 if (dum->port_status & USB_PORT_STAT_SUSPEND) {
1da177e4
LT
1647 /* 20msec resume signaling */
1648 dum->resuming = 1;
1649 dum->re_timeout = jiffies +
f1c39fad 1650 msecs_to_jiffies(20);
1da177e4
LT
1651 }
1652 break;
1653 case USB_PORT_FEAT_POWER:
f1c39fad
AS
1654 if (dum->port_status & USB_PORT_STAT_POWER)
1655 dev_dbg (dummy_dev(dum), "power-off\n");
1656 /* FALLS THROUGH */
1da177e4
LT
1657 default:
1658 dum->port_status &= ~(1 << wValue);
f1c39fad 1659 set_link_state (dum);
1da177e4
LT
1660 }
1661 break;
1662 case GetHubDescriptor:
1663 hub_descriptor ((struct usb_hub_descriptor *) buf);
1664 break;
1665 case GetHubStatus:
cc095b0b 1666 *(__le32 *) buf = __constant_cpu_to_le32 (0);
1da177e4
LT
1667 break;
1668 case GetPortStatus:
1669 if (wIndex != 1)
1670 retval = -EPIPE;
1671
1672 /* whoever resets or resumes must GetPortStatus to
1673 * complete it!!
1674 */
f1c39fad
AS
1675 if (dum->resuming &&
1676 time_after_eq (jiffies, dum->re_timeout)) {
c2db8b5e
AS
1677 dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1678 dum->port_status &= ~USB_PORT_STAT_SUSPEND;
1da177e4 1679 }
f1c39fad
AS
1680 if ((dum->port_status & USB_PORT_STAT_RESET) != 0 &&
1681 time_after_eq (jiffies, dum->re_timeout)) {
c2db8b5e
AS
1682 dum->port_status |= (USB_PORT_STAT_C_RESET << 16);
1683 dum->port_status &= ~USB_PORT_STAT_RESET;
f1c39fad 1684 if (dum->pullup) {
1da177e4
LT
1685 dum->port_status |= USB_PORT_STAT_ENABLE;
1686 /* give it the best speed we agree on */
1687 dum->gadget.speed = dum->driver->speed;
1688 dum->gadget.ep0->maxpacket = 64;
1689 switch (dum->gadget.speed) {
1690 case USB_SPEED_HIGH:
1691 dum->port_status |=
1692 USB_PORT_STAT_HIGH_SPEED;
1693 break;
1694 case USB_SPEED_LOW:
1695 dum->gadget.ep0->maxpacket = 8;
1696 dum->port_status |=
1697 USB_PORT_STAT_LOW_SPEED;
1698 break;
1699 default:
1700 dum->gadget.speed = USB_SPEED_FULL;
1701 break;
1702 }
1703 }
1704 }
f1c39fad 1705 set_link_state (dum);
cc095b0b
AS
1706 ((__le16 *) buf)[0] = cpu_to_le16 (dum->port_status);
1707 ((__le16 *) buf)[1] = cpu_to_le16 (dum->port_status >> 16);
1da177e4
LT
1708 break;
1709 case SetHubFeature:
1710 retval = -EPIPE;
1711 break;
1712 case SetPortFeature:
1713 switch (wValue) {
1714 case USB_PORT_FEAT_SUSPEND:
f1c39fad 1715 if (dum->active) {
c2db8b5e 1716 dum->port_status |= USB_PORT_STAT_SUSPEND;
f1c39fad
AS
1717
1718 /* HNP would happen here; for now we
1719 * assume b_bus_req is always true.
1720 */
1721 set_link_state (dum);
1722 if (((1 << USB_DEVICE_B_HNP_ENABLE)
1723 & dum->devstatus) != 0)
1724 dev_dbg (dummy_dev(dum),
5742b0c9 1725 "no HNP yet!\n");
1da177e4
LT
1726 }
1727 break;
f1c39fad
AS
1728 case USB_PORT_FEAT_POWER:
1729 dum->port_status |= USB_PORT_STAT_POWER;
1730 set_link_state (dum);
1731 break;
1da177e4 1732 case USB_PORT_FEAT_RESET:
f1c39fad
AS
1733 /* if it's already enabled, disable */
1734 dum->port_status &= ~(USB_PORT_STAT_ENABLE
1735 | USB_PORT_STAT_LOW_SPEED
1736 | USB_PORT_STAT_HIGH_SPEED);
391eca9d 1737 dum->devstatus = 0;
1da177e4
LT
1738 /* 50msec reset signaling */
1739 dum->re_timeout = jiffies + msecs_to_jiffies(50);
f1c39fad 1740 /* FALLS THROUGH */
1da177e4 1741 default:
f1c39fad
AS
1742 if ((dum->port_status & USB_PORT_STAT_POWER) != 0) {
1743 dum->port_status |= (1 << wValue);
1744 set_link_state (dum);
1745 }
1da177e4
LT
1746 }
1747 break;
1748
1749 default:
1750 dev_dbg (dummy_dev(dum),
1751 "hub control req%04x v%04x i%04x l%d\n",
1752 typeReq, wValue, wIndex, wLength);
1753
1754 /* "protocol stall" on error */
1755 retval = -EPIPE;
1756 }
1757 spin_unlock_irqrestore (&dum->lock, flags);
685eb93f
AS
1758
1759 if ((dum->port_status & PORT_C_MASK) != 0)
1760 usb_hcd_poll_rh_status (hcd);
1da177e4
LT
1761 return retval;
1762}
1763
0c0382e3 1764static int dummy_bus_suspend (struct usb_hcd *hcd)
391eca9d
AS
1765{
1766 struct dummy *dum = hcd_to_dummy (hcd);
1767
3cf0a22e
AS
1768 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __FUNCTION__);
1769
391eca9d
AS
1770 spin_lock_irq (&dum->lock);
1771 dum->rh_state = DUMMY_RH_SUSPENDED;
1772 set_link_state (dum);
3cf0a22e 1773 hcd->state = HC_STATE_SUSPENDED;
391eca9d
AS
1774 spin_unlock_irq (&dum->lock);
1775 return 0;
1776}
1777
0c0382e3 1778static int dummy_bus_resume (struct usb_hcd *hcd)
391eca9d
AS
1779{
1780 struct dummy *dum = hcd_to_dummy (hcd);
3cf0a22e
AS
1781 int rc = 0;
1782
1783 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __FUNCTION__);
391eca9d
AS
1784
1785 spin_lock_irq (&dum->lock);
3cf0a22e
AS
1786 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
1787 dev_warn (&hcd->self.root_hub->dev, "HC isn't running!\n");
1788 rc = -ENODEV;
1789 } else {
1790 dum->rh_state = DUMMY_RH_RUNNING;
1791 set_link_state (dum);
1792 if (!list_empty(&dum->urbp_list))
1793 mod_timer (&dum->timer, jiffies);
1794 hcd->state = HC_STATE_RUNNING;
1795 }
391eca9d 1796 spin_unlock_irq (&dum->lock);
3cf0a22e 1797 return rc;
391eca9d 1798}
1da177e4
LT
1799
1800/*-------------------------------------------------------------------------*/
1801
1802static inline ssize_t
1803show_urb (char *buf, size_t size, struct urb *urb)
1804{
1805 int ep = usb_pipeendpoint (urb->pipe);
1806
1807 return snprintf (buf, size,
1808 "urb/%p %s ep%d%s%s len %d/%d\n",
1809 urb,
1810 ({ char *s;
1811 switch (urb->dev->speed) {
1812 case USB_SPEED_LOW: s = "ls"; break;
1813 case USB_SPEED_FULL: s = "fs"; break;
1814 case USB_SPEED_HIGH: s = "hs"; break;
1815 default: s = "?"; break;
1816 }; s; }),
1817 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
1818 ({ char *s; \
1819 switch (usb_pipetype (urb->pipe)) { \
1820 case PIPE_CONTROL: s = ""; break; \
1821 case PIPE_BULK: s = "-bulk"; break; \
1822 case PIPE_INTERRUPT: s = "-int"; break; \
1823 default: s = "-iso"; break; \
1824 }; s;}),
1825 urb->actual_length, urb->transfer_buffer_length);
1826}
1827
1828static ssize_t
10523b3b 1829show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
1830{
1831 struct usb_hcd *hcd = dev_get_drvdata (dev);
1832 struct dummy *dum = hcd_to_dummy (hcd);
1833 struct urbp *urbp;
1834 size_t size = 0;
1835 unsigned long flags;
1836
1837 spin_lock_irqsave (&dum->lock, flags);
1838 list_for_each_entry (urbp, &dum->urbp_list, urbp_list) {
1839 size_t temp;
1840
1841 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
1842 buf += temp;
1843 size += temp;
1844 }
1845 spin_unlock_irqrestore (&dum->lock, flags);
1846
1847 return size;
1848}
1849static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
1850
1851static int dummy_start (struct usb_hcd *hcd)
1852{
1853 struct dummy *dum;
1da177e4
LT
1854
1855 dum = hcd_to_dummy (hcd);
1856
1857 /*
1858 * MASTER side init ... we emulate a root hub that'll only ever
1859 * talk to one device (the slave side). Also appears in sysfs,
1860 * just like more familiar pci-based HCDs.
1861 */
1862 spin_lock_init (&dum->lock);
1863 init_timer (&dum->timer);
1864 dum->timer.function = dummy_timer;
1865 dum->timer.data = (unsigned long) dum;
391eca9d 1866 dum->rh_state = DUMMY_RH_RUNNING;
1da177e4
LT
1867
1868 INIT_LIST_HEAD (&dum->urbp_list);
1869
bc96c0ad
AS
1870 /* only show a low-power port: just 8mA */
1871 hcd->power_budget = 8;
1da177e4 1872 hcd->state = HC_STATE_RUNNING;
685eb93f 1873 hcd->uses_new_polling = 1;
1da177e4 1874
5742b0c9
AS
1875#ifdef CONFIG_USB_OTG
1876 hcd->self.otg_port = 1;
1877#endif
1878
1da177e4 1879 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
efd54a36 1880 return device_create_file (dummy_dev(dum), &dev_attr_urbs);
1da177e4
LT
1881}
1882
1883static void dummy_stop (struct usb_hcd *hcd)
1884{
1885 struct dummy *dum;
1886
1887 dum = hcd_to_dummy (hcd);
1888
1889 device_remove_file (dummy_dev(dum), &dev_attr_urbs);
1da177e4 1890 usb_gadget_unregister_driver (dum->driver);
1da177e4
LT
1891 dev_info (dummy_dev(dum), "stopped\n");
1892}
1893
1894/*-------------------------------------------------------------------------*/
1895
1896static int dummy_h_get_frame (struct usb_hcd *hcd)
1897{
1898 return dummy_g_get_frame (NULL);
1899}
1900
1901static const struct hc_driver dummy_hcd = {
1902 .description = (char *) driver_name,
1903 .product_desc = "Dummy host controller",
1904 .hcd_priv_size = sizeof(struct dummy),
1905
1906 .flags = HCD_USB2,
1907
1908 .start = dummy_start,
1909 .stop = dummy_stop,
1910
1911 .urb_enqueue = dummy_urb_enqueue,
1912 .urb_dequeue = dummy_urb_dequeue,
1913
1914 .get_frame_number = dummy_h_get_frame,
1915
1916 .hub_status_data = dummy_hub_status,
1917 .hub_control = dummy_hub_control,
0c0382e3
AS
1918 .bus_suspend = dummy_bus_suspend,
1919 .bus_resume = dummy_bus_resume,
1da177e4
LT
1920};
1921
8364d6b0 1922static int dummy_hcd_probe(struct platform_device *pdev)
1da177e4
LT
1923{
1924 struct usb_hcd *hcd;
1925 int retval;
1926
8364d6b0 1927 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
1da177e4 1928
8364d6b0 1929 hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, pdev->dev.bus_id);
1da177e4
LT
1930 if (!hcd)
1931 return -ENOMEM;
1932 the_controller = hcd_to_dummy (hcd);
1933
1934 retval = usb_add_hcd(hcd, 0, 0);
1935 if (retval != 0) {
1936 usb_put_hcd (hcd);
1937 the_controller = NULL;
1938 }
1939 return retval;
1940}
1941
8364d6b0 1942static int dummy_hcd_remove (struct platform_device *pdev)
1da177e4
LT
1943{
1944 struct usb_hcd *hcd;
1945
8364d6b0 1946 hcd = platform_get_drvdata (pdev);
1da177e4
LT
1947 usb_remove_hcd (hcd);
1948 usb_put_hcd (hcd);
1949 the_controller = NULL;
d9b76251 1950 return 0;
1da177e4
LT
1951}
1952
8364d6b0 1953static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
391eca9d
AS
1954{
1955 struct usb_hcd *hcd;
3cf0a22e
AS
1956 struct dummy *dum;
1957 int rc = 0;
391eca9d 1958
8364d6b0 1959 dev_dbg (&pdev->dev, "%s\n", __FUNCTION__);
391eca9d 1960
3cf0a22e
AS
1961 hcd = platform_get_drvdata (pdev);
1962 dum = hcd_to_dummy (hcd);
1963 if (dum->rh_state == DUMMY_RH_RUNNING) {
1964 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
1965 rc = -EBUSY;
1966 } else
1967 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1968 return rc;
391eca9d
AS
1969}
1970
8364d6b0 1971static int dummy_hcd_resume (struct platform_device *pdev)
391eca9d
AS
1972{
1973 struct usb_hcd *hcd;
1974
8364d6b0 1975 dev_dbg (&pdev->dev, "%s\n", __FUNCTION__);
391eca9d 1976
3cf0a22e
AS
1977 hcd = platform_get_drvdata (pdev);
1978 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
391eca9d
AS
1979 usb_hcd_poll_rh_status (hcd);
1980 return 0;
1981}
1982
3ae5eaec 1983static struct platform_driver dummy_hcd_driver = {
d9b76251
AS
1984 .probe = dummy_hcd_probe,
1985 .remove = dummy_hcd_remove,
391eca9d
AS
1986 .suspend = dummy_hcd_suspend,
1987 .resume = dummy_hcd_resume,
3ae5eaec
RK
1988 .driver = {
1989 .name = (char *) driver_name,
1990 .owner = THIS_MODULE,
1991 },
d9b76251 1992};
1da177e4 1993
d9b76251 1994/*-------------------------------------------------------------------------*/
1da177e4 1995
d9b76251
AS
1996/* These don't need to do anything because the pdev structures are
1997 * statically allocated. */
1998static void
1999dummy_udc_release (struct device *dev) {}
1da177e4 2000
d9b76251
AS
2001static void
2002dummy_hcd_release (struct device *dev) {}
2003
2004static struct platform_device the_udc_pdev = {
2005 .name = (char *) gadget_name,
2006 .id = -1,
2007 .dev = {
2008 .release = dummy_udc_release,
2009 },
2010};
1da177e4 2011
d9b76251
AS
2012static struct platform_device the_hcd_pdev = {
2013 .name = (char *) driver_name,
2014 .id = -1,
2015 .dev = {
2016 .release = dummy_hcd_release,
2017 },
2018};
1da177e4
LT
2019
2020static int __init init (void)
2021{
2022 int retval;
2023
2024 if (usb_disabled ())
2025 return -ENODEV;
d9b76251 2026
3ae5eaec 2027 retval = platform_driver_register (&dummy_hcd_driver);
d9b76251 2028 if (retval < 0)
1da177e4 2029 return retval;
d9b76251 2030
3ae5eaec 2031 retval = platform_driver_register (&dummy_udc_driver);
d9b76251
AS
2032 if (retval < 0)
2033 goto err_register_udc_driver;
2034
2035 retval = platform_device_register (&the_hcd_pdev);
2036 if (retval < 0)
2037 goto err_register_hcd;
2038
2039 retval = platform_device_register (&the_udc_pdev);
2040 if (retval < 0)
2041 goto err_register_udc;
2042 return retval;
2043
2044err_register_udc:
2045 platform_device_unregister (&the_hcd_pdev);
2046err_register_hcd:
3ae5eaec 2047 platform_driver_unregister (&dummy_udc_driver);
d9b76251 2048err_register_udc_driver:
3ae5eaec 2049 platform_driver_unregister (&dummy_hcd_driver);
1da177e4
LT
2050 return retval;
2051}
2052module_init (init);
2053
2054static void __exit cleanup (void)
2055{
d9b76251
AS
2056 platform_device_unregister (&the_udc_pdev);
2057 platform_device_unregister (&the_hcd_pdev);
3ae5eaec
RK
2058 platform_driver_unregister (&dummy_udc_driver);
2059 platform_driver_unregister (&dummy_hcd_driver);
1da177e4
LT
2060}
2061module_exit (cleanup);
This page took 0.36242 seconds and 5 git commands to generate.