fcb5526cb085d68658bc936fd93650af9a3e33c4
[safe/jmp/linux-2.6] / drivers / usb / gadget / dummy_hcd.c
1 /*
2  * dummy_hcd.c -- Dummy/Loopback USB host and device emulator driver.
3  *
4  * Maintainer: Alan Stern <stern@rowland.harvard.edu>
5  *
6  * Copyright (C) 2003 David Brownell
7  * Copyright (C) 2003-2005 Alan Stern
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * 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
39 #include <linux/module.h>
40 #include <linux/kernel.h>
41 #include <linux/delay.h>
42 #include <linux/ioport.h>
43 #include <linux/slab.h>
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>
49 #include <linux/platform_device.h>
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"
64 #define DRIVER_VERSION  "02 May 2005"
65
66 static const char       driver_name [] = "dummy_hcd";
67 static const char       driver_desc [] = "USB Host+Gadget Emulator";
68
69 static const char       gadget_name [] = "dummy_udc";
70
71 MODULE_DESCRIPTION (DRIVER_DESC);
72 MODULE_AUTHOR ("David Brownell");
73 MODULE_LICENSE ("GPL");
74
75 /*-------------------------------------------------------------------------*/
76
77 /* gadget side driver data structres */
78 struct 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
89 struct dummy_request {
90         struct list_head                queue;          /* ep's requests */
91         struct usb_request              req;
92 };
93
94 static 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
99 static 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
121 static const char ep0name [] = "ep0";
122
123 static 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 };
138 #define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
139
140 /*-------------------------------------------------------------------------*/
141
142 #define FIFO_SIZE               64
143
144 struct urbp {
145         struct urb              *urb;
146         struct list_head        urbp_list;
147 };
148
149
150 enum dummy_rh_state {
151         DUMMY_RH_RESET,
152         DUMMY_RH_SUSPENDED,
153         DUMMY_RH_RUNNING
154 };
155
156 struct 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;
169         unsigned                        udc_suspended:1;
170         unsigned                        pullup:1;
171         unsigned                        active:1;
172         unsigned                        old_active:1;
173
174         /*
175          * MASTER/HOST side support
176          */
177         enum dummy_rh_state             rh_state;
178         struct timer_list               timer;
179         u32                             port_status;
180         u32                             old_status;
181         unsigned                        resuming:1;
182         unsigned long                   re_timeout;
183
184         struct usb_device               *udev;
185         struct list_head                urbp_list;
186 };
187
188 static inline struct dummy *hcd_to_dummy (struct usb_hcd *hcd)
189 {
190         return (struct dummy *) (hcd->hcd_priv);
191 }
192
193 static inline struct usb_hcd *dummy_to_hcd (struct dummy *dum)
194 {
195         return container_of((void *) dum, struct usb_hcd, hcd_priv);
196 }
197
198 static inline struct device *dummy_dev (struct dummy *dum)
199 {
200         return dummy_to_hcd(dum)->self.controller;
201 }
202
203 static inline struct device *udc_dev (struct dummy *dum)
204 {
205         return dum->gadget.dev.parent;
206 }
207
208 static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
209 {
210         return container_of (ep->gadget, struct dummy, gadget);
211 }
212
213 static inline struct dummy *gadget_to_dummy (struct usb_gadget *gadget)
214 {
215         return container_of (gadget, struct dummy, gadget);
216 }
217
218 static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
219 {
220         return container_of (dev, struct dummy, gadget.dev);
221 }
222
223 static struct dummy                     *the_controller;
224
225 /*-------------------------------------------------------------------------*/
226
227 /* SLAVE/GADGET SIDE UTILITY ROUTINES */
228
229 /* called with spinlock held */
230 static 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 */
246 static void
247 stop_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 */
264 static void
265 set_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;
270
271         /* UDC suspend must cause a disconnect */
272         else if (!dum->pullup || dum->udc_suspended) {
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;
286                 else if ((dum->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
287                                 dum->rh_state != DUMMY_RH_SUSPENDED)
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
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
332 static int
333 dummy_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
425         dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
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;
442 done:
443         return retval;
444 }
445
446 static 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
464         dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name);
465         return retval;
466 }
467
468 static struct usb_request *
469 dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
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
478         req = kzalloc(sizeof(*req), mem_flags);
479         if (!req)
480                 return NULL;
481         INIT_LIST_HEAD (&req->queue);
482         return &req->req;
483 }
484
485 static void
486 dummy_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
500 static void *
501 dummy_alloc_buffer (
502         struct usb_ep *_ep,
503         unsigned bytes,
504         dma_addr_t *dma,
505         gfp_t mem_flags
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
521 static void
522 dummy_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
532 static void
533 fifo_complete (struct usb_ep *ep, struct usb_request *req)
534 {
535 }
536
537 static int
538 dummy_queue (struct usb_ep *_ep, struct usb_request *_req,
539                 gfp_t mem_flags)
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
559         dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
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
594 static 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
610         local_irq_save (flags);
611         spin_lock (&dum->lock);
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         }
620         spin_unlock (&dum->lock);
621
622         if (retval == 0) {
623                 dev_dbg (udc_dev(dum),
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         }
628         local_irq_restore (flags);
629         return retval;
630 }
631
632 static int
633 dummy_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
655 static 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 ... */
675 static 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
683 static int dummy_wakeup (struct usb_gadget *_gadget)
684 {
685         struct dummy    *dum;
686
687         dum = gadget_to_dummy (_gadget);
688         if (!(dum->devstatus &  ( (1 << USB_DEVICE_B_HNP_ENABLE)
689                                 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
690                 return -EINVAL;
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? */
698
699         /* hub notices our request, issues downstream resume, etc */
700         dum->resuming = 1;
701         dum->re_timeout = jiffies + msecs_to_jiffies(20);
702         mod_timer (&dummy_to_hcd (dum)->rh_timer, dum->re_timeout);
703         return 0;
704 }
705
706 static 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
718 static 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);
728
729         usb_hcd_poll_rh_status (dummy_to_hcd (dum));
730         return 0;
731 }
732
733 static const struct usb_gadget_ops dummy_ops = {
734         .get_frame      = dummy_g_get_frame,
735         .wakeup         = dummy_wakeup,
736         .set_selfpowered = dummy_set_selfpowered,
737         .pullup         = dummy_pullup,
738 };
739
740 /*-------------------------------------------------------------------------*/
741
742 /* "function" sysfs attribute */
743 static ssize_t
744 show_function (struct device *dev, struct device_attribute *attr, char *buf)
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 }
752 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
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
770 int
771 usb_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;
780         if (!driver->bind || !driver->setup
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          */
788
789         dum->devstatus = 0;
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;
815         dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
816                         driver->driver.name);
817         if ((retval = driver->bind (&dum->gadget)) != 0)
818                 goto err_bind_gadget;
819
820         driver->driver.bus = dum->gadget.dev.parent->bus;
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;
825
826         /* khubd will enumerate this in a while */
827         spin_lock_irq (&dum->lock);
828         dum->pullup = 1;
829         set_link_state (dum);
830         spin_unlock_irq (&dum->lock);
831
832         usb_hcd_poll_rh_status (dummy_to_hcd (dum));
833         return 0;
834
835 err_bind_driver:
836         driver_unregister (&driver->driver);
837 err_register:
838         if (driver->unbind)
839                 driver->unbind (&dum->gadget);
840         spin_lock_irq (&dum->lock);
841         dum->pullup = 0;
842         set_link_state (dum);
843         spin_unlock_irq (&dum->lock);
844 err_bind_gadget:
845         dum->driver = NULL;
846         dum->gadget.dev.driver = NULL;
847         return retval;
848 }
849 EXPORT_SYMBOL (usb_gadget_register_driver);
850
851 int
852 usb_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;
859         if (!driver || driver != dum->driver || !driver->unbind)
860                 return -EINVAL;
861
862         dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
863                         driver->driver.name);
864
865         spin_lock_irqsave (&dum->lock, flags);
866         dum->pullup = 0;
867         set_link_state (dum);
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);
874         driver_unregister (&driver->driver);
875
876         spin_lock_irqsave (&dum->lock, flags);
877         dum->pullup = 0;
878         set_link_state (dum);
879         spin_unlock_irqrestore (&dum->lock, flags);
880
881         usb_hcd_poll_rh_status (dummy_to_hcd (dum));
882         return 0;
883 }
884 EXPORT_SYMBOL (usb_gadget_unregister_driver);
885
886 #undef is_enabled
887
888 /* just declare this in any driver that really need it */
889 extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
890
891 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
892 {
893         return -ENOSYS;
894 }
895 EXPORT_SYMBOL (net2280_set_fifo_mode);
896
897
898 /* The gadget structure is stored inside the hcd structure and will be
899  * released along with it. */
900 static void
901 dummy_gadget_release (struct device *dev)
902 {
903         struct dummy    *dum = gadget_dev_to_dummy (dev);
904
905         usb_put_hcd (dummy_to_hcd (dum));
906 }
907
908 static int dummy_udc_probe (struct platform_device *pdev)
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");
921         dum->gadget.dev.parent = &pdev->dev;
922         dum->gadget.dev.release = dummy_gadget_release;
923         rc = device_register (&dum->gadget.dev);
924         if (rc < 0)
925                 return rc;
926
927         usb_get_hcd (dummy_to_hcd (dum));
928
929         platform_set_drvdata (pdev, dum);
930         rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
931         if (rc < 0)
932                 device_unregister (&dum->gadget.dev);
933         return rc;
934 }
935
936 static int dummy_udc_remove (struct platform_device *pdev)
937 {
938         struct dummy    *dum = platform_get_drvdata (pdev);
939
940         platform_set_drvdata (pdev, NULL);
941         device_remove_file (&dum->gadget.dev, &dev_attr_function);
942         device_unregister (&dum->gadget.dev);
943         return 0;
944 }
945
946 static int dummy_udc_suspend (struct platform_device *pdev, pm_message_t state)
947 {
948         struct dummy    *dum = platform_get_drvdata(pdev);
949
950         dev_dbg (&pdev->dev, "%s\n", __FUNCTION__);
951         spin_lock_irq (&dum->lock);
952         dum->udc_suspended = 1;
953         set_link_state (dum);
954         spin_unlock_irq (&dum->lock);
955
956         pdev->dev.power.power_state = state;
957         usb_hcd_poll_rh_status (dummy_to_hcd (dum));
958         return 0;
959 }
960
961 static int dummy_udc_resume (struct platform_device *pdev)
962 {
963         struct dummy    *dum = platform_get_drvdata(pdev);
964
965         dev_dbg (&pdev->dev, "%s\n", __FUNCTION__);
966         spin_lock_irq (&dum->lock);
967         dum->udc_suspended = 0;
968         set_link_state (dum);
969         spin_unlock_irq (&dum->lock);
970
971         pdev->dev.power.power_state = PMSG_ON;
972         usb_hcd_poll_rh_status (dummy_to_hcd (dum));
973         return 0;
974 }
975
976 static struct platform_driver dummy_udc_driver = {
977         .probe          = dummy_udc_probe,
978         .remove         = dummy_udc_remove,
979         .suspend        = dummy_udc_suspend,
980         .resume         = dummy_udc_resume,
981         .driver         = {
982                 .name   = (char *) gadget_name,
983                 .owner  = THIS_MODULE,
984         },
985 };
986
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
1001 static int dummy_urb_enqueue (
1002         struct usb_hcd                  *hcd,
1003         struct usb_host_endpoint        *ep,
1004         struct urb                      *urb,
1005         gfp_t                           mem_flags
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
1041 static int dummy_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
1042 {
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);
1053         return 0;
1054 }
1055
1056 static 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 */
1065 static int
1066 transfer (struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit)
1067 {
1068         struct dummy_request    *req;
1069
1070 top:
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
1188 static 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);
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
1209 static 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  */
1240 static 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
1282 restart:
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;
1294                 } else if (dum->rh_state != DUMMY_RH_RUNNING)
1295                         continue;
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;
1340                         unsigned                        w_index;
1341                         unsigned                        w_value;
1342
1343                         setup = *(struct usb_ctrlrequest*) urb->setup_packet;
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) {
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;
1356                                 dev_dbg (udc_dev(dum), "stale req = %p\n",
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;
1377                                 dum->address = w_value;
1378                                 maybe_set_status (urb, 0);
1379                                 dev_dbg (udc_dev(dum), "set_address = %d\n",
1380                                                 w_value);
1381                                 value = 0;
1382                                 break;
1383                         case USB_REQ_SET_FEATURE:
1384                                 if (setup.bRequestType == Dev_Request) {
1385                                         value = 0;
1386                                         switch (w_value) {
1387                                         case USB_DEVICE_REMOTE_WAKEUP:
1388                                                 break;
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;
1399                                         default:
1400                                                 value = -EOPNOTSUPP;
1401                                         }
1402                                         if (value == 0) {
1403                                                 dum->devstatus |=
1404                                                         (1 << w_value);
1405                                                 maybe_set_status (urb, 0);
1406                                         }
1407
1408                                 } else if (setup.bRequestType == Ep_Request) {
1409                                         // endpoint halt
1410                                         ep2 = find_endpoint (dum, w_index);
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) {
1422                                         switch (w_value) {
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
1435                                         ep2 = find_endpoint (dum, w_index);
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) {
1461         ep2 = find_endpoint (dum, w_index);
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)
1503                                         dev_dbg (udc_dev(dum),
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
1545 return_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);
1553                 usb_hcd_giveback_urb (dummy_to_hcd(dum), urb);
1554                 spin_lock (&dum->lock);
1555
1556                 goto restart;
1557         }
1558
1559         if (list_empty (&dum->urbp_list)) {
1560                 usb_put_dev (dum->udev);
1561                 dum->udev = NULL;
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));
1565         }
1566
1567         spin_unlock_irqrestore (&dum->lock, flags);
1568 }
1569
1570 /*-------------------------------------------------------------------------*/
1571
1572 #define PORT_C_MASK \
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)
1578
1579 static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1580 {
1581         struct dummy            *dum;
1582         unsigned long           flags;
1583         int                     retval = 0;
1584
1585         dum = hcd_to_dummy (hcd);
1586
1587         spin_lock_irqsave (&dum->lock, flags);
1588         if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
1589                 goto done;
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
1597         if ((dum->port_status & PORT_C_MASK) != 0) {
1598                 *buf = (1 << 1);
1599                 dev_dbg (dummy_dev(dum), "port status 0x%08x has changes\n",
1600                                 dum->port_status);
1601                 retval = 1;
1602                 if (dum->rh_state == DUMMY_RH_SUSPENDED)
1603                         usb_hcd_resume_root_hub (hcd);
1604         }
1605 done:
1606         spin_unlock_irqrestore (&dum->lock, flags);
1607         return retval;
1608 }
1609
1610 static inline void
1611 hub_descriptor (struct usb_hub_descriptor *desc)
1612 {
1613         memset (desc, 0, sizeof *desc);
1614         desc->bDescriptorType = 0x29;
1615         desc->bDescLength = 9;
1616         desc->wHubCharacteristics = (__force __u16)
1617                         (__constant_cpu_to_le16 (0x0001));
1618         desc->bNbrPorts = 1;
1619         desc->bitmap [0] = 0xff;
1620         desc->bitmap [1] = 0xff;
1621 }
1622
1623 static 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
1635         if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
1636                 return -ETIMEDOUT;
1637
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:
1646                         if (dum->port_status & USB_PORT_STAT_SUSPEND) {
1647                                 /* 20msec resume signaling */
1648                                 dum->resuming = 1;
1649                                 dum->re_timeout = jiffies +
1650                                                 msecs_to_jiffies(20);
1651                         }
1652                         break;
1653                 case USB_PORT_FEAT_POWER:
1654                         if (dum->port_status & USB_PORT_STAT_POWER)
1655                                 dev_dbg (dummy_dev(dum), "power-off\n");
1656                         /* FALLS THROUGH */
1657                 default:
1658                         dum->port_status &= ~(1 << wValue);
1659                         set_link_state (dum);
1660                 }
1661                 break;
1662         case GetHubDescriptor:
1663                 hub_descriptor ((struct usb_hub_descriptor *) buf);
1664                 break;
1665         case GetHubStatus:
1666                 *(__le32 *) buf = __constant_cpu_to_le32 (0);
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                  */
1675                 if (dum->resuming &&
1676                                 time_after_eq (jiffies, dum->re_timeout)) {
1677                         dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1678                         dum->port_status &= ~USB_PORT_STAT_SUSPEND;
1679                 }
1680                 if ((dum->port_status & USB_PORT_STAT_RESET) != 0 &&
1681                                 time_after_eq (jiffies, dum->re_timeout)) {
1682                         dum->port_status |= (USB_PORT_STAT_C_RESET << 16);
1683                         dum->port_status &= ~USB_PORT_STAT_RESET;
1684                         if (dum->pullup) {
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                 }
1705                 set_link_state (dum);
1706                 ((__le16 *) buf)[0] = cpu_to_le16 (dum->port_status);
1707                 ((__le16 *) buf)[1] = cpu_to_le16 (dum->port_status >> 16);
1708                 break;
1709         case SetHubFeature:
1710                 retval = -EPIPE;
1711                 break;
1712         case SetPortFeature:
1713                 switch (wValue) {
1714                 case USB_PORT_FEAT_SUSPEND:
1715                         if (dum->active) {
1716                                 dum->port_status |= USB_PORT_STAT_SUSPEND;
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),
1725                                                         "no HNP yet!\n");
1726                         }
1727                         break;
1728                 case USB_PORT_FEAT_POWER:
1729                         dum->port_status |= USB_PORT_STAT_POWER;
1730                         set_link_state (dum);
1731                         break;
1732                 case USB_PORT_FEAT_RESET:
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);
1737                         dum->devstatus = 0;
1738                         /* 50msec reset signaling */
1739                         dum->re_timeout = jiffies + msecs_to_jiffies(50);
1740                         /* FALLS THROUGH */
1741                 default:
1742                         if ((dum->port_status & USB_PORT_STAT_POWER) != 0) {
1743                                 dum->port_status |= (1 << wValue);
1744                                 set_link_state (dum);
1745                         }
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);
1758
1759         if ((dum->port_status & PORT_C_MASK) != 0)
1760                 usb_hcd_poll_rh_status (hcd);
1761         return retval;
1762 }
1763
1764 static int dummy_bus_suspend (struct usb_hcd *hcd)
1765 {
1766         struct dummy *dum = hcd_to_dummy (hcd);
1767
1768         dev_dbg (&hcd->self.root_hub->dev, "%s\n", __FUNCTION__);
1769
1770         spin_lock_irq (&dum->lock);
1771         dum->rh_state = DUMMY_RH_SUSPENDED;
1772         set_link_state (dum);
1773         hcd->state = HC_STATE_SUSPENDED;
1774         spin_unlock_irq (&dum->lock);
1775         return 0;
1776 }
1777
1778 static int dummy_bus_resume (struct usb_hcd *hcd)
1779 {
1780         struct dummy *dum = hcd_to_dummy (hcd);
1781         int rc = 0;
1782
1783         dev_dbg (&hcd->self.root_hub->dev, "%s\n", __FUNCTION__);
1784
1785         spin_lock_irq (&dum->lock);
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         }
1796         spin_unlock_irq (&dum->lock);
1797         return rc;
1798 }
1799
1800 /*-------------------------------------------------------------------------*/
1801
1802 static inline ssize_t
1803 show_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
1828 static ssize_t
1829 show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
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 }
1849 static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
1850
1851 static int dummy_start (struct usb_hcd *hcd)
1852 {
1853         struct dummy            *dum;
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;
1866         dum->rh_state = DUMMY_RH_RUNNING;
1867
1868         INIT_LIST_HEAD (&dum->urbp_list);
1869
1870         /* only show a low-power port: just 8mA */
1871         hcd->power_budget = 8;
1872         hcd->state = HC_STATE_RUNNING;
1873         hcd->uses_new_polling = 1;
1874
1875 #ifdef CONFIG_USB_OTG
1876         hcd->self.otg_port = 1;
1877 #endif
1878
1879         /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
1880         return device_create_file (dummy_dev(dum), &dev_attr_urbs);
1881 }
1882
1883 static 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);
1890         usb_gadget_unregister_driver (dum->driver);
1891         dev_info (dummy_dev(dum), "stopped\n");
1892 }
1893
1894 /*-------------------------------------------------------------------------*/
1895
1896 static int dummy_h_get_frame (struct usb_hcd *hcd)
1897 {
1898         return dummy_g_get_frame (NULL);
1899 }
1900
1901 static 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,
1918         .bus_suspend =          dummy_bus_suspend,
1919         .bus_resume =           dummy_bus_resume,
1920 };
1921
1922 static int dummy_hcd_probe(struct platform_device *pdev)
1923 {
1924         struct usb_hcd          *hcd;
1925         int                     retval;
1926
1927         dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
1928
1929         hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, pdev->dev.bus_id);
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
1942 static int dummy_hcd_remove (struct platform_device *pdev)
1943 {
1944         struct usb_hcd          *hcd;
1945
1946         hcd = platform_get_drvdata (pdev);
1947         usb_remove_hcd (hcd);
1948         usb_put_hcd (hcd);
1949         the_controller = NULL;
1950         return 0;
1951 }
1952
1953 static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
1954 {
1955         struct usb_hcd          *hcd;
1956         struct dummy            *dum;
1957         int                     rc = 0;
1958
1959         dev_dbg (&pdev->dev, "%s\n", __FUNCTION__);
1960
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;
1969 }
1970
1971 static int dummy_hcd_resume (struct platform_device *pdev)
1972 {
1973         struct usb_hcd          *hcd;
1974
1975         dev_dbg (&pdev->dev, "%s\n", __FUNCTION__);
1976
1977         hcd = platform_get_drvdata (pdev);
1978         set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1979         usb_hcd_poll_rh_status (hcd);
1980         return 0;
1981 }
1982
1983 static struct platform_driver dummy_hcd_driver = {
1984         .probe          = dummy_hcd_probe,
1985         .remove         = dummy_hcd_remove,
1986         .suspend        = dummy_hcd_suspend,
1987         .resume         = dummy_hcd_resume,
1988         .driver         = {
1989                 .name   = (char *) driver_name,
1990                 .owner  = THIS_MODULE,
1991         },
1992 };
1993
1994 /*-------------------------------------------------------------------------*/
1995
1996 /* These don't need to do anything because the pdev structures are
1997  * statically allocated. */
1998 static void
1999 dummy_udc_release (struct device *dev) {}
2000
2001 static void
2002 dummy_hcd_release (struct device *dev) {}
2003
2004 static struct platform_device           the_udc_pdev = {
2005         .name           = (char *) gadget_name,
2006         .id             = -1,
2007         .dev            = {
2008                 .release        = dummy_udc_release,
2009         },
2010 };
2011
2012 static struct platform_device           the_hcd_pdev = {
2013         .name           = (char *) driver_name,
2014         .id             = -1,
2015         .dev            = {
2016                 .release        = dummy_hcd_release,
2017         },
2018 };
2019
2020 static int __init init (void)
2021 {
2022         int     retval;
2023
2024         if (usb_disabled ())
2025                 return -ENODEV;
2026
2027         retval = platform_driver_register (&dummy_hcd_driver);
2028         if (retval < 0)
2029                 return retval;
2030
2031         retval = platform_driver_register (&dummy_udc_driver);
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
2044 err_register_udc:
2045         platform_device_unregister (&the_hcd_pdev);
2046 err_register_hcd:
2047         platform_driver_unregister (&dummy_udc_driver);
2048 err_register_udc_driver:
2049         platform_driver_unregister (&dummy_hcd_driver);
2050         return retval;
2051 }
2052 module_init (init);
2053
2054 static void __exit cleanup (void)
2055 {
2056         platform_device_unregister (&the_udc_pdev);
2057         platform_device_unregister (&the_hcd_pdev);
2058         platform_driver_unregister (&dummy_udc_driver);
2059         platform_driver_unregister (&dummy_hcd_driver);
2060 }
2061 module_exit (cleanup);