blob: 42ce41d71b7fd8d0e97dc6c96fb40040fd9eb6a8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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/config.h>
40#include <linux/module.h>
41#include <linux/kernel.h>
42#include <linux/delay.h>
43#include <linux/ioport.h>
44#include <linux/sched.h>
45#include <linux/slab.h>
46#include <linux/smp_lock.h>
47#include <linux/errno.h>
48#include <linux/init.h>
49#include <linux/timer.h>
50#include <linux/list.h>
51#include <linux/interrupt.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010052#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/usb.h>
54#include <linux/usb_gadget.h>
55
56#include <asm/byteorder.h>
57#include <asm/io.h>
58#include <asm/irq.h>
59#include <asm/system.h>
60#include <asm/unaligned.h>
61
62
63#include "../core/hcd.h"
64
65
66#define DRIVER_DESC "USB Host+Gadget Emulator"
Alan Stern391eca92005-05-10 15:34:16 -040067#define DRIVER_VERSION "02 May 2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69static const char driver_name [] = "dummy_hcd";
70static const char driver_desc [] = "USB Host+Gadget Emulator";
71
72static const char gadget_name [] = "dummy_udc";
73
74MODULE_DESCRIPTION (DRIVER_DESC);
75MODULE_AUTHOR ("David Brownell");
76MODULE_LICENSE ("GPL");
77
78/*-------------------------------------------------------------------------*/
79
80/* gadget side driver data structres */
81struct dummy_ep {
82 struct list_head queue;
83 unsigned long last_io; /* jiffies timestamp */
84 struct usb_gadget *gadget;
85 const struct usb_endpoint_descriptor *desc;
86 struct usb_ep ep;
87 unsigned halted : 1;
88 unsigned already_seen : 1;
89 unsigned setup_stage : 1;
90};
91
92struct dummy_request {
93 struct list_head queue; /* ep's requests */
94 struct usb_request req;
95};
96
97static inline struct dummy_ep *usb_ep_to_dummy_ep (struct usb_ep *_ep)
98{
99 return container_of (_ep, struct dummy_ep, ep);
100}
101
102static inline struct dummy_request *usb_request_to_dummy_request
103 (struct usb_request *_req)
104{
105 return container_of (_req, struct dummy_request, req);
106}
107
108/*-------------------------------------------------------------------------*/
109
110/*
111 * Every device has ep0 for control requests, plus up to 30 more endpoints,
112 * in one of two types:
113 *
114 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
115 * number can be changed. Names like "ep-a" are used for this type.
116 *
117 * - Fixed Function: in other cases. some characteristics may be mutable;
118 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
119 *
120 * Gadget drivers are responsible for not setting up conflicting endpoint
121 * configurations, illegal or unsupported packet lengths, and so on.
122 */
123
124static const char ep0name [] = "ep0";
125
126static const char *const ep_name [] = {
127 ep0name, /* everyone has ep0 */
128
129 /* act like a net2280: high speed, six configurable endpoints */
130 "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
131
132 /* or like pxa250: fifteen fixed function endpoints */
133 "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
134 "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
135 "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
136 "ep15in-int",
137
138 /* or like sa1100: two fixed function endpoints */
139 "ep1out-bulk", "ep2in-bulk",
140};
Tobias Klauser52950ed2005-12-11 16:20:08 +0100141#define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Alan Sternd9b76252005-05-03 16:15:43 -0400143/*-------------------------------------------------------------------------*/
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define FIFO_SIZE 64
146
147struct urbp {
148 struct urb *urb;
149 struct list_head urbp_list;
150};
151
Alan Stern391eca92005-05-10 15:34:16 -0400152
153enum dummy_rh_state {
154 DUMMY_RH_RESET,
155 DUMMY_RH_SUSPENDED,
156 DUMMY_RH_RUNNING
157};
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159struct dummy {
160 spinlock_t lock;
161
162 /*
163 * SLAVE/GADGET side support
164 */
165 struct dummy_ep ep [DUMMY_ENDPOINTS];
166 int address;
167 struct usb_gadget gadget;
168 struct usb_gadget_driver *driver;
169 struct dummy_request fifo_req;
170 u8 fifo_buf [FIFO_SIZE];
171 u16 devstatus;
Alan Stern391eca92005-05-10 15:34:16 -0400172 unsigned udc_suspended:1;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400173 unsigned pullup:1;
174 unsigned active:1;
175 unsigned old_active:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 /*
178 * MASTER/HOST side support
179 */
Alan Stern391eca92005-05-10 15:34:16 -0400180 enum dummy_rh_state rh_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 struct timer_list timer;
182 u32 port_status;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400183 u32 old_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 unsigned resuming:1;
185 unsigned long re_timeout;
186
187 struct usb_device *udev;
188 struct list_head urbp_list;
189};
190
191static inline struct dummy *hcd_to_dummy (struct usb_hcd *hcd)
192{
193 return (struct dummy *) (hcd->hcd_priv);
194}
195
196static inline struct usb_hcd *dummy_to_hcd (struct dummy *dum)
197{
198 return container_of((void *) dum, struct usb_hcd, hcd_priv);
199}
200
201static inline struct device *dummy_dev (struct dummy *dum)
202{
203 return dummy_to_hcd(dum)->self.controller;
204}
205
Alan Sternd9b76252005-05-03 16:15:43 -0400206static inline struct device *udc_dev (struct dummy *dum)
207{
208 return dum->gadget.dev.parent;
209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
212{
213 return container_of (ep->gadget, struct dummy, gadget);
214}
215
216static inline struct dummy *gadget_to_dummy (struct usb_gadget *gadget)
217{
218 return container_of (gadget, struct dummy, gadget);
219}
220
221static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
222{
223 return container_of (dev, struct dummy, gadget.dev);
224}
225
226static struct dummy *the_controller;
227
228/*-------------------------------------------------------------------------*/
229
Alan Sternf1c39fa2005-05-03 16:24:04 -0400230/* SLAVE/GADGET SIDE UTILITY ROUTINES */
231
232/* called with spinlock held */
233static void nuke (struct dummy *dum, struct dummy_ep *ep)
234{
235 while (!list_empty (&ep->queue)) {
236 struct dummy_request *req;
237
238 req = list_entry (ep->queue.next, struct dummy_request, queue);
239 list_del_init (&req->queue);
240 req->req.status = -ESHUTDOWN;
241
242 spin_unlock (&dum->lock);
243 req->req.complete (&ep->ep, &req->req);
244 spin_lock (&dum->lock);
245 }
246}
247
248/* caller must hold lock */
249static void
250stop_activity (struct dummy *dum)
251{
252 struct dummy_ep *ep;
253
254 /* prevent any more requests */
255 dum->address = 0;
256
257 /* The timer is left running so that outstanding URBs can fail */
258
259 /* nuke any pending requests first, so driver i/o is quiesced */
260 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
261 nuke (dum, ep);
262
263 /* driver now does any non-usb quiescing necessary */
264}
265
266/* caller must hold lock */
267static void
268set_link_state (struct dummy *dum)
269{
270 dum->active = 0;
271 if ((dum->port_status & USB_PORT_STAT_POWER) == 0)
272 dum->port_status = 0;
Alan Stern391eca92005-05-10 15:34:16 -0400273
274 /* UDC suspend must cause a disconnect */
275 else if (!dum->pullup || dum->udc_suspended) {
Alan Sternf1c39fa2005-05-03 16:24:04 -0400276 dum->port_status &= ~(USB_PORT_STAT_CONNECTION |
277 USB_PORT_STAT_ENABLE |
278 USB_PORT_STAT_LOW_SPEED |
279 USB_PORT_STAT_HIGH_SPEED |
280 USB_PORT_STAT_SUSPEND);
281 if ((dum->old_status & USB_PORT_STAT_CONNECTION) != 0)
282 dum->port_status |= (USB_PORT_STAT_C_CONNECTION << 16);
283 } else {
284 dum->port_status |= USB_PORT_STAT_CONNECTION;
285 if ((dum->old_status & USB_PORT_STAT_CONNECTION) == 0)
286 dum->port_status |= (USB_PORT_STAT_C_CONNECTION << 16);
287 if ((dum->port_status & USB_PORT_STAT_ENABLE) == 0)
288 dum->port_status &= ~USB_PORT_STAT_SUSPEND;
Alan Stern391eca92005-05-10 15:34:16 -0400289 else if ((dum->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
290 dum->rh_state != DUMMY_RH_SUSPENDED)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400291 dum->active = 1;
292 }
293
294 if ((dum->port_status & USB_PORT_STAT_ENABLE) == 0 || dum->active)
295 dum->resuming = 0;
296
297 if ((dum->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
298 (dum->port_status & USB_PORT_STAT_RESET) != 0) {
299 if ((dum->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
300 (dum->old_status & USB_PORT_STAT_RESET) == 0 &&
301 dum->driver) {
302 stop_activity (dum);
303 spin_unlock (&dum->lock);
304 dum->driver->disconnect (&dum->gadget);
305 spin_lock (&dum->lock);
306 }
307 } else if (dum->active != dum->old_active) {
308 if (dum->old_active && dum->driver->suspend) {
309 spin_unlock (&dum->lock);
310 dum->driver->suspend (&dum->gadget);
311 spin_lock (&dum->lock);
312 } else if (!dum->old_active && dum->driver->resume) {
313 spin_unlock (&dum->lock);
314 dum->driver->resume (&dum->gadget);
315 spin_lock (&dum->lock);
316 }
317 }
318
319 dum->old_status = dum->port_status;
320 dum->old_active = dum->active;
321}
322
323/*-------------------------------------------------------------------------*/
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/* SLAVE/GADGET SIDE DRIVER
326 *
327 * This only tracks gadget state. All the work is done when the host
328 * side tries some (emulated) i/o operation. Real device controller
329 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
330 */
331
332#define is_enabled(dum) \
333 (dum->port_status & USB_PORT_STAT_ENABLE)
334
335static int
336dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
337{
338 struct dummy *dum;
339 struct dummy_ep *ep;
340 unsigned max;
341 int retval;
342
343 ep = usb_ep_to_dummy_ep (_ep);
344 if (!_ep || !desc || ep->desc || _ep->name == ep0name
345 || desc->bDescriptorType != USB_DT_ENDPOINT)
346 return -EINVAL;
347 dum = ep_to_dummy (ep);
348 if (!dum->driver || !is_enabled (dum))
349 return -ESHUTDOWN;
350 max = le16_to_cpu(desc->wMaxPacketSize) & 0x3ff;
351
352 /* drivers must not request bad settings, since lower levels
353 * (hardware or its drivers) may not check. some endpoints
354 * can't do iso, many have maxpacket limitations, etc.
355 *
356 * since this "hardware" driver is here to help debugging, we
357 * have some extra sanity checks. (there could be more though,
358 * especially for "ep9out" style fixed function ones.)
359 */
360 retval = -EINVAL;
361 switch (desc->bmAttributes & 0x03) {
362 case USB_ENDPOINT_XFER_BULK:
363 if (strstr (ep->ep.name, "-iso")
364 || strstr (ep->ep.name, "-int")) {
365 goto done;
366 }
367 switch (dum->gadget.speed) {
368 case USB_SPEED_HIGH:
369 if (max == 512)
370 break;
371 /* conserve return statements */
372 default:
373 switch (max) {
374 case 8: case 16: case 32: case 64:
375 /* we'll fake any legal size */
376 break;
377 default:
378 case USB_SPEED_LOW:
379 goto done;
380 }
381 }
382 break;
383 case USB_ENDPOINT_XFER_INT:
384 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
385 goto done;
386 /* real hardware might not handle all packet sizes */
387 switch (dum->gadget.speed) {
388 case USB_SPEED_HIGH:
389 if (max <= 1024)
390 break;
391 /* save a return statement */
392 case USB_SPEED_FULL:
393 if (max <= 64)
394 break;
395 /* save a return statement */
396 default:
397 if (max <= 8)
398 break;
399 goto done;
400 }
401 break;
402 case USB_ENDPOINT_XFER_ISOC:
403 if (strstr (ep->ep.name, "-bulk")
404 || strstr (ep->ep.name, "-int"))
405 goto done;
406 /* real hardware might not handle all packet sizes */
407 switch (dum->gadget.speed) {
408 case USB_SPEED_HIGH:
409 if (max <= 1024)
410 break;
411 /* save a return statement */
412 case USB_SPEED_FULL:
413 if (max <= 1023)
414 break;
415 /* save a return statement */
416 default:
417 goto done;
418 }
419 break;
420 default:
421 /* few chips support control except on ep0 */
422 goto done;
423 }
424
425 _ep->maxpacket = max;
426 ep->desc = desc;
427
Alan Sternd9b76252005-05-03 16:15:43 -0400428 dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 _ep->name,
430 desc->bEndpointAddress & 0x0f,
431 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
432 ({ char *val;
433 switch (desc->bmAttributes & 0x03) {
434 case USB_ENDPOINT_XFER_BULK: val = "bulk"; break;
435 case USB_ENDPOINT_XFER_ISOC: val = "iso"; break;
436 case USB_ENDPOINT_XFER_INT: val = "intr"; break;
437 default: val = "ctrl"; break;
438 }; val; }),
439 max);
440
441 /* at this point real hardware should be NAKing transfers
442 * to that endpoint, until a buffer is queued to it.
443 */
444 retval = 0;
445done:
446 return retval;
447}
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449static int dummy_disable (struct usb_ep *_ep)
450{
451 struct dummy_ep *ep;
452 struct dummy *dum;
453 unsigned long flags;
454 int retval;
455
456 ep = usb_ep_to_dummy_ep (_ep);
457 if (!_ep || !ep->desc || _ep->name == ep0name)
458 return -EINVAL;
459 dum = ep_to_dummy (ep);
460
461 spin_lock_irqsave (&dum->lock, flags);
462 ep->desc = NULL;
463 retval = 0;
464 nuke (dum, ep);
465 spin_unlock_irqrestore (&dum->lock, flags);
466
Alan Sternd9b76252005-05-03 16:15:43 -0400467 dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return retval;
469}
470
471static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -0400472dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 struct dummy_ep *ep;
475 struct dummy_request *req;
476
477 if (!_ep)
478 return NULL;
479 ep = usb_ep_to_dummy_ep (_ep);
480
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800481 req = kzalloc(sizeof(*req), mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (!req)
483 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 INIT_LIST_HEAD (&req->queue);
485 return &req->req;
486}
487
488static void
489dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
490{
491 struct dummy_ep *ep;
492 struct dummy_request *req;
493
494 ep = usb_ep_to_dummy_ep (_ep);
495 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
496 return;
497
498 req = usb_request_to_dummy_request (_req);
499 WARN_ON (!list_empty (&req->queue));
500 kfree (req);
501}
502
503static void *
504dummy_alloc_buffer (
505 struct usb_ep *_ep,
506 unsigned bytes,
507 dma_addr_t *dma,
Al Viro55016f12005-10-21 03:21:58 -0400508 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509) {
510 char *retval;
511 struct dummy_ep *ep;
512 struct dummy *dum;
513
514 ep = usb_ep_to_dummy_ep (_ep);
515 dum = ep_to_dummy (ep);
516
517 if (!dum->driver)
518 return NULL;
519 retval = kmalloc (bytes, mem_flags);
520 *dma = (dma_addr_t) retval;
521 return retval;
522}
523
524static void
525dummy_free_buffer (
526 struct usb_ep *_ep,
527 void *buf,
528 dma_addr_t dma,
529 unsigned bytes
530) {
531 if (bytes)
532 kfree (buf);
533}
534
535static void
536fifo_complete (struct usb_ep *ep, struct usb_request *req)
537{
538}
539
540static int
Olav Kongas5db539e2005-06-23 20:25:36 +0300541dummy_queue (struct usb_ep *_ep, struct usb_request *_req,
Al Viro55016f12005-10-21 03:21:58 -0400542 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 struct dummy_ep *ep;
545 struct dummy_request *req;
546 struct dummy *dum;
547 unsigned long flags;
548
549 req = usb_request_to_dummy_request (_req);
550 if (!_req || !list_empty (&req->queue) || !_req->complete)
551 return -EINVAL;
552
553 ep = usb_ep_to_dummy_ep (_ep);
554 if (!_ep || (!ep->desc && _ep->name != ep0name))
555 return -EINVAL;
556
557 dum = ep_to_dummy (ep);
558 if (!dum->driver || !is_enabled (dum))
559 return -ESHUTDOWN;
560
561#if 0
Alan Sternd9b76252005-05-03 16:15:43 -0400562 dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 ep, _req, _ep->name, _req->length, _req->buf);
564#endif
565
566 _req->status = -EINPROGRESS;
567 _req->actual = 0;
568 spin_lock_irqsave (&dum->lock, flags);
569
570 /* implement an emulated single-request FIFO */
571 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
572 list_empty (&dum->fifo_req.queue) &&
573 list_empty (&ep->queue) &&
574 _req->length <= FIFO_SIZE) {
575 req = &dum->fifo_req;
576 req->req = *_req;
577 req->req.buf = dum->fifo_buf;
578 memcpy (dum->fifo_buf, _req->buf, _req->length);
579 req->req.context = dum;
580 req->req.complete = fifo_complete;
581
582 spin_unlock (&dum->lock);
583 _req->actual = _req->length;
584 _req->status = 0;
585 _req->complete (_ep, _req);
586 spin_lock (&dum->lock);
587 }
588 list_add_tail (&req->queue, &ep->queue);
589 spin_unlock_irqrestore (&dum->lock, flags);
590
591 /* real hardware would likely enable transfers here, in case
592 * it'd been left NAKing.
593 */
594 return 0;
595}
596
597static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
598{
599 struct dummy_ep *ep;
600 struct dummy *dum;
601 int retval = -EINVAL;
602 unsigned long flags;
603 struct dummy_request *req = NULL;
604
605 if (!_ep || !_req)
606 return retval;
607 ep = usb_ep_to_dummy_ep (_ep);
608 dum = ep_to_dummy (ep);
609
610 if (!dum->driver)
611 return -ESHUTDOWN;
612
613 spin_lock_irqsave (&dum->lock, flags);
614 list_for_each_entry (req, &ep->queue, queue) {
615 if (&req->req == _req) {
616 list_del_init (&req->queue);
617 _req->status = -ECONNRESET;
618 retval = 0;
619 break;
620 }
621 }
622 spin_unlock_irqrestore (&dum->lock, flags);
623
624 if (retval == 0) {
Alan Sternd9b76252005-05-03 16:15:43 -0400625 dev_dbg (udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 "dequeued req %p from %s, len %d buf %p\n",
627 req, _ep->name, _req->length, _req->buf);
628 _req->complete (_ep, _req);
629 }
630 return retval;
631}
632
633static int
634dummy_set_halt (struct usb_ep *_ep, int value)
635{
636 struct dummy_ep *ep;
637 struct dummy *dum;
638
639 if (!_ep)
640 return -EINVAL;
641 ep = usb_ep_to_dummy_ep (_ep);
642 dum = ep_to_dummy (ep);
643 if (!dum->driver)
644 return -ESHUTDOWN;
645 if (!value)
646 ep->halted = 0;
647 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
648 !list_empty (&ep->queue))
649 return -EAGAIN;
650 else
651 ep->halted = 1;
652 /* FIXME clear emulated data toggle too */
653 return 0;
654}
655
656static const struct usb_ep_ops dummy_ep_ops = {
657 .enable = dummy_enable,
658 .disable = dummy_disable,
659
660 .alloc_request = dummy_alloc_request,
661 .free_request = dummy_free_request,
662
663 .alloc_buffer = dummy_alloc_buffer,
664 .free_buffer = dummy_free_buffer,
665 /* map, unmap, ... eventually hook the "generic" dma calls */
666
667 .queue = dummy_queue,
668 .dequeue = dummy_dequeue,
669
670 .set_halt = dummy_set_halt,
671};
672
673/*-------------------------------------------------------------------------*/
674
675/* there are both host and device side versions of this call ... */
676static int dummy_g_get_frame (struct usb_gadget *_gadget)
677{
678 struct timeval tv;
679
680 do_gettimeofday (&tv);
681 return tv.tv_usec / 1000;
682}
683
684static int dummy_wakeup (struct usb_gadget *_gadget)
685{
686 struct dummy *dum;
687
688 dum = gadget_to_dummy (_gadget);
Alan Stern391eca92005-05-10 15:34:16 -0400689 if (!(dum->devstatus & ( (1 << USB_DEVICE_B_HNP_ENABLE)
Alan Stern5742b0c2005-05-02 11:25:17 -0400690 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return -EINVAL;
Alan Stern391eca92005-05-10 15:34:16 -0400692 if ((dum->port_status & USB_PORT_STAT_CONNECTION) == 0)
693 return -ENOLINK;
694 if ((dum->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
695 dum->rh_state != DUMMY_RH_SUSPENDED)
696 return -EIO;
697
698 /* FIXME: What if the root hub is suspended but the port isn't? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 /* hub notices our request, issues downstream resume, etc */
701 dum->resuming = 1;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400702 dum->re_timeout = jiffies + msecs_to_jiffies(20);
Alan Stern685eb932005-05-03 16:27:26 -0400703 mod_timer (&dummy_to_hcd (dum)->rh_timer, dum->re_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return 0;
705}
706
707static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
708{
709 struct dummy *dum;
710
711 dum = gadget_to_dummy (_gadget);
712 if (value)
713 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
714 else
715 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
716 return 0;
717}
718
Alan Sternf1c39fa2005-05-03 16:24:04 -0400719static int dummy_pullup (struct usb_gadget *_gadget, int value)
720{
721 struct dummy *dum;
722 unsigned long flags;
723
724 dum = gadget_to_dummy (_gadget);
725 spin_lock_irqsave (&dum->lock, flags);
726 dum->pullup = (value != 0);
727 set_link_state (dum);
728 spin_unlock_irqrestore (&dum->lock, flags);
Alan Stern685eb932005-05-03 16:27:26 -0400729
730 usb_hcd_poll_rh_status (dummy_to_hcd (dum));
Alan Sternf1c39fa2005-05-03 16:24:04 -0400731 return 0;
732}
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734static const struct usb_gadget_ops dummy_ops = {
735 .get_frame = dummy_g_get_frame,
736 .wakeup = dummy_wakeup,
737 .set_selfpowered = dummy_set_selfpowered,
Alan Sternf1c39fa2005-05-03 16:24:04 -0400738 .pullup = dummy_pullup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739};
740
741/*-------------------------------------------------------------------------*/
742
743/* "function" sysfs attribute */
744static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400745show_function (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 struct dummy *dum = gadget_dev_to_dummy (dev);
748
749 if (!dum->driver || !dum->driver->function)
750 return 0;
751 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
752}
Alan Sterncc095b02005-05-10 15:28:38 -0400753static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755/*-------------------------------------------------------------------------*/
756
757/*
758 * Driver registration/unregistration.
759 *
760 * This is basically hardware-specific; there's usually only one real USB
761 * device (not host) controller since that's how USB devices are intended
762 * to work. So most implementations of these api calls will rely on the
763 * fact that only one driver will ever bind to the hardware. But curious
764 * hardware can be built with discrete components, so the gadget API doesn't
765 * require that assumption.
766 *
767 * For this emulator, it might be convenient to create a usb slave device
768 * for each driver that registers: just add to a big root hub.
769 */
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771int
772usb_gadget_register_driver (struct usb_gadget_driver *driver)
773{
774 struct dummy *dum = the_controller;
775 int retval, i;
776
777 if (!dum)
778 return -EINVAL;
779 if (dum->driver)
780 return -EBUSY;
781 if (!driver->bind || !driver->unbind || !driver->setup
782 || driver->speed == USB_SPEED_UNKNOWN)
783 return -EINVAL;
784
785 /*
786 * SLAVE side init ... the layer above hardware, which
787 * can't enumerate without help from the driver we're binding.
788 */
Alan Stern5742b0c2005-05-02 11:25:17 -0400789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 dum->devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 INIT_LIST_HEAD (&dum->gadget.ep_list);
793 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
794 struct dummy_ep *ep = &dum->ep [i];
795
796 if (!ep_name [i])
797 break;
798 ep->ep.name = ep_name [i];
799 ep->ep.ops = &dummy_ep_ops;
800 list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
801 ep->halted = ep->already_seen = ep->setup_stage = 0;
802 ep->ep.maxpacket = ~0;
803 ep->last_io = jiffies;
804 ep->gadget = &dum->gadget;
805 ep->desc = NULL;
806 INIT_LIST_HEAD (&ep->queue);
807 }
808
809 dum->gadget.ep0 = &dum->ep [0].ep;
810 dum->ep [0].ep.maxpacket = 64;
811 list_del_init (&dum->ep [0].ep.ep_list);
812 INIT_LIST_HEAD(&dum->fifo_req.queue);
813
814 dum->driver = driver;
815 dum->gadget.dev.driver = &driver->driver;
Alan Sternd9b76252005-05-03 16:15:43 -0400816 dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 driver->driver.name);
818 if ((retval = driver->bind (&dum->gadget)) != 0) {
819 dum->driver = NULL;
820 dum->gadget.dev.driver = NULL;
821 return retval;
822 }
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 driver->driver.bus = dum->gadget.dev.parent->bus;
825 driver_register (&driver->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 device_bind_driver (&dum->gadget.dev);
827
828 /* khubd will enumerate this in a while */
Alan Sternf1c39fa2005-05-03 16:24:04 -0400829 spin_lock_irq (&dum->lock);
830 dum->pullup = 1;
831 set_link_state (dum);
832 spin_unlock_irq (&dum->lock);
Alan Stern685eb932005-05-03 16:27:26 -0400833
834 usb_hcd_poll_rh_status (dummy_to_hcd (dum));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return 0;
836}
837EXPORT_SYMBOL (usb_gadget_register_driver);
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839int
840usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
841{
842 struct dummy *dum = the_controller;
843 unsigned long flags;
844
845 if (!dum)
846 return -ENODEV;
847 if (!driver || driver != dum->driver)
848 return -EINVAL;
849
Alan Sternd9b76252005-05-03 16:15:43 -0400850 dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 driver->driver.name);
852
853 spin_lock_irqsave (&dum->lock, flags);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400854 dum->pullup = 0;
855 set_link_state (dum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 spin_unlock_irqrestore (&dum->lock, flags);
857
858 driver->unbind (&dum->gadget);
859 dum->driver = NULL;
860
861 device_release_driver (&dum->gadget.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 driver_unregister (&driver->driver);
863
Alan Sternf1c39fa2005-05-03 16:24:04 -0400864 spin_lock_irqsave (&dum->lock, flags);
865 dum->pullup = 0;
866 set_link_state (dum);
867 spin_unlock_irqrestore (&dum->lock, flags);
868
Alan Stern685eb932005-05-03 16:27:26 -0400869 usb_hcd_poll_rh_status (dummy_to_hcd (dum));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return 0;
871}
872EXPORT_SYMBOL (usb_gadget_unregister_driver);
873
874#undef is_enabled
875
Alan Sterncc095b02005-05-10 15:28:38 -0400876/* just declare this in any driver that really need it */
877extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
880{
881 return -ENOSYS;
882}
883EXPORT_SYMBOL (net2280_set_fifo_mode);
884
Alan Sternd9b76252005-05-03 16:15:43 -0400885
886/* The gadget structure is stored inside the hcd structure and will be
887 * released along with it. */
888static void
889dummy_gadget_release (struct device *dev)
890{
891#if 0 /* usb_bus_put isn't EXPORTed! */
892 struct dummy *dum = gadget_dev_to_dummy (dev);
893
894 usb_bus_put (&dummy_to_hcd (dum)->self);
895#endif
896}
897
Alan Stern8364d6b2005-11-14 12:16:30 -0500898static int dummy_udc_probe (struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -0400899{
900 struct dummy *dum = the_controller;
901 int rc;
902
903 dum->gadget.name = gadget_name;
904 dum->gadget.ops = &dummy_ops;
905 dum->gadget.is_dualspeed = 1;
906
907 /* maybe claim OTG support, though we won't complete HNP */
908 dum->gadget.is_otg = (dummy_to_hcd(dum)->self.otg_port != 0);
909
910 strcpy (dum->gadget.dev.bus_id, "gadget");
Alan Stern8364d6b2005-11-14 12:16:30 -0500911 dum->gadget.dev.parent = &pdev->dev;
Alan Sternd9b76252005-05-03 16:15:43 -0400912 dum->gadget.dev.release = dummy_gadget_release;
913 rc = device_register (&dum->gadget.dev);
914 if (rc < 0)
915 return rc;
916
917#if 0 /* usb_bus_get isn't EXPORTed! */
918 usb_bus_get (&dummy_to_hcd (dum)->self);
919#endif
920
Alan Stern8364d6b2005-11-14 12:16:30 -0500921 platform_set_drvdata (pdev, dum);
Alan Sternd9b76252005-05-03 16:15:43 -0400922 device_create_file (&dum->gadget.dev, &dev_attr_function);
923 return rc;
924}
925
Alan Stern8364d6b2005-11-14 12:16:30 -0500926static int dummy_udc_remove (struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -0400927{
Alan Stern8364d6b2005-11-14 12:16:30 -0500928 struct dummy *dum = platform_get_drvdata (pdev);
Alan Sternd9b76252005-05-03 16:15:43 -0400929
Alan Stern8364d6b2005-11-14 12:16:30 -0500930 platform_set_drvdata (pdev, NULL);
Alan Sternd9b76252005-05-03 16:15:43 -0400931 device_remove_file (&dum->gadget.dev, &dev_attr_function);
932 device_unregister (&dum->gadget.dev);
933 return 0;
934}
935
Alan Stern8364d6b2005-11-14 12:16:30 -0500936static int dummy_udc_suspend (struct platform_device *pdev, pm_message_t state)
Alan Stern391eca92005-05-10 15:34:16 -0400937{
Alan Stern8364d6b2005-11-14 12:16:30 -0500938 struct dummy *dum = platform_get_drvdata(pdev);
Alan Stern391eca92005-05-10 15:34:16 -0400939
Alan Stern8364d6b2005-11-14 12:16:30 -0500940 dev_dbg (&pdev->dev, "%s\n", __FUNCTION__);
Alan Stern391eca92005-05-10 15:34:16 -0400941 spin_lock_irq (&dum->lock);
942 dum->udc_suspended = 1;
943 set_link_state (dum);
944 spin_unlock_irq (&dum->lock);
945
Alan Stern8364d6b2005-11-14 12:16:30 -0500946 pdev->dev.power.power_state = state;
Alan Stern391eca92005-05-10 15:34:16 -0400947 usb_hcd_poll_rh_status (dummy_to_hcd (dum));
948 return 0;
949}
950
Alan Stern8364d6b2005-11-14 12:16:30 -0500951static int dummy_udc_resume (struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -0400952{
Alan Stern8364d6b2005-11-14 12:16:30 -0500953 struct dummy *dum = platform_get_drvdata(pdev);
Alan Stern391eca92005-05-10 15:34:16 -0400954
Alan Stern8364d6b2005-11-14 12:16:30 -0500955 dev_dbg (&pdev->dev, "%s\n", __FUNCTION__);
Alan Stern391eca92005-05-10 15:34:16 -0400956 spin_lock_irq (&dum->lock);
957 dum->udc_suspended = 0;
958 set_link_state (dum);
959 spin_unlock_irq (&dum->lock);
960
Alan Stern8364d6b2005-11-14 12:16:30 -0500961 pdev->dev.power.power_state = PMSG_ON;
Alan Stern391eca92005-05-10 15:34:16 -0400962 usb_hcd_poll_rh_status (dummy_to_hcd (dum));
963 return 0;
964}
965
Russell King3ae5eae2005-11-09 22:32:44 +0000966static struct platform_driver dummy_udc_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -0400967 .probe = dummy_udc_probe,
968 .remove = dummy_udc_remove,
Alan Stern391eca92005-05-10 15:34:16 -0400969 .suspend = dummy_udc_suspend,
970 .resume = dummy_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +0000971 .driver = {
972 .name = (char *) gadget_name,
973 .owner = THIS_MODULE,
974 },
Alan Sternd9b76252005-05-03 16:15:43 -0400975};
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977/*-------------------------------------------------------------------------*/
978
979/* MASTER/HOST SIDE DRIVER
980 *
981 * this uses the hcd framework to hook up to host side drivers.
982 * its root hub will only have one device, otherwise it acts like
983 * a normal host controller.
984 *
985 * when urbs are queued, they're just stuck on a list that we
986 * scan in a timer callback. that callback connects writes from
987 * the host with reads from the device, and so on, based on the
988 * usb 2.0 rules.
989 */
990
991static int dummy_urb_enqueue (
992 struct usb_hcd *hcd,
993 struct usb_host_endpoint *ep,
994 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -0400995 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996) {
997 struct dummy *dum;
998 struct urbp *urbp;
999 unsigned long flags;
1000
1001 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1002 return -EINVAL;
1003
1004 urbp = kmalloc (sizeof *urbp, mem_flags);
1005 if (!urbp)
1006 return -ENOMEM;
1007 urbp->urb = urb;
1008
1009 dum = hcd_to_dummy (hcd);
1010 spin_lock_irqsave (&dum->lock, flags);
1011
1012 if (!dum->udev) {
1013 dum->udev = urb->dev;
1014 usb_get_dev (dum->udev);
1015 } else if (unlikely (dum->udev != urb->dev))
1016 dev_err (dummy_dev(dum), "usb_device address has changed!\n");
1017
1018 list_add_tail (&urbp->urbp_list, &dum->urbp_list);
1019 urb->hcpriv = urbp;
1020 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
1021 urb->error_count = 1; /* mark as a new urb */
1022
1023 /* kick the scheduler, it'll do the rest */
1024 if (!timer_pending (&dum->timer))
1025 mod_timer (&dum->timer, jiffies + 1);
1026
1027 spin_unlock_irqrestore (&dum->lock, flags);
1028 return 0;
1029}
1030
1031static int dummy_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
1032{
Alan Stern391eca92005-05-10 15:34:16 -04001033 struct dummy *dum;
1034 unsigned long flags;
1035
1036 /* giveback happens automatically in timer callback,
1037 * so make sure the callback happens */
1038 dum = hcd_to_dummy (hcd);
1039 spin_lock_irqsave (&dum->lock, flags);
1040 if (dum->rh_state != DUMMY_RH_RUNNING && !list_empty(&dum->urbp_list))
1041 mod_timer (&dum->timer, jiffies);
1042 spin_unlock_irqrestore (&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return 0;
1044}
1045
1046static void maybe_set_status (struct urb *urb, int status)
1047{
1048 spin_lock (&urb->lock);
1049 if (urb->status == -EINPROGRESS)
1050 urb->status = status;
1051 spin_unlock (&urb->lock);
1052}
1053
1054/* transfer up to a frame's worth; caller must own lock */
1055static int
1056transfer (struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit)
1057{
1058 struct dummy_request *req;
1059
1060top:
1061 /* if there's no request queued, the device is NAKing; return */
1062 list_for_each_entry (req, &ep->queue, queue) {
1063 unsigned host_len, dev_len, len;
1064 int is_short, to_host;
1065 int rescan = 0;
1066
1067 /* 1..N packets of ep->ep.maxpacket each ... the last one
1068 * may be short (including zero length).
1069 *
1070 * writer can send a zlp explicitly (length 0) or implicitly
1071 * (length mod maxpacket zero, and 'zero' flag); they always
1072 * terminate reads.
1073 */
1074 host_len = urb->transfer_buffer_length - urb->actual_length;
1075 dev_len = req->req.length - req->req.actual;
1076 len = min (host_len, dev_len);
1077
1078 /* FIXME update emulated data toggle too */
1079
1080 to_host = usb_pipein (urb->pipe);
1081 if (unlikely (len == 0))
1082 is_short = 1;
1083 else {
1084 char *ubuf, *rbuf;
1085
1086 /* not enough bandwidth left? */
1087 if (limit < ep->ep.maxpacket && limit < len)
1088 break;
1089 len = min (len, (unsigned) limit);
1090 if (len == 0)
1091 break;
1092
1093 /* use an extra pass for the final short packet */
1094 if (len > ep->ep.maxpacket) {
1095 rescan = 1;
1096 len -= (len % ep->ep.maxpacket);
1097 }
1098 is_short = (len % ep->ep.maxpacket) != 0;
1099
1100 /* else transfer packet(s) */
1101 ubuf = urb->transfer_buffer + urb->actual_length;
1102 rbuf = req->req.buf + req->req.actual;
1103 if (to_host)
1104 memcpy (ubuf, rbuf, len);
1105 else
1106 memcpy (rbuf, ubuf, len);
1107 ep->last_io = jiffies;
1108
1109 limit -= len;
1110 urb->actual_length += len;
1111 req->req.actual += len;
1112 }
1113
1114 /* short packets terminate, maybe with overflow/underflow.
1115 * it's only really an error to write too much.
1116 *
1117 * partially filling a buffer optionally blocks queue advances
1118 * (so completion handlers can clean up the queue) but we don't
1119 * need to emulate such data-in-flight. so we only show part
1120 * of the URB_SHORT_NOT_OK effect: completion status.
1121 */
1122 if (is_short) {
1123 if (host_len == dev_len) {
1124 req->req.status = 0;
1125 maybe_set_status (urb, 0);
1126 } else if (to_host) {
1127 req->req.status = 0;
1128 if (dev_len > host_len)
1129 maybe_set_status (urb, -EOVERFLOW);
1130 else
1131 maybe_set_status (urb,
1132 (urb->transfer_flags
1133 & URB_SHORT_NOT_OK)
1134 ? -EREMOTEIO : 0);
1135 } else if (!to_host) {
1136 maybe_set_status (urb, 0);
1137 if (host_len > dev_len)
1138 req->req.status = -EOVERFLOW;
1139 else
1140 req->req.status = 0;
1141 }
1142
1143 /* many requests terminate without a short packet */
1144 } else {
1145 if (req->req.length == req->req.actual
1146 && !req->req.zero)
1147 req->req.status = 0;
1148 if (urb->transfer_buffer_length == urb->actual_length
1149 && !(urb->transfer_flags
1150 & URB_ZERO_PACKET)) {
1151 maybe_set_status (urb, 0);
1152 }
1153 }
1154
1155 /* device side completion --> continuable */
1156 if (req->req.status != -EINPROGRESS) {
1157 list_del_init (&req->queue);
1158
1159 spin_unlock (&dum->lock);
1160 req->req.complete (&ep->ep, &req->req);
1161 spin_lock (&dum->lock);
1162
1163 /* requests might have been unlinked... */
1164 rescan = 1;
1165 }
1166
1167 /* host side completion --> terminate */
1168 if (urb->status != -EINPROGRESS)
1169 break;
1170
1171 /* rescan to continue with any other queued i/o */
1172 if (rescan)
1173 goto top;
1174 }
1175 return limit;
1176}
1177
1178static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1179{
1180 int limit = ep->ep.maxpacket;
1181
1182 if (dum->gadget.speed == USB_SPEED_HIGH) {
1183 int tmp;
1184
1185 /* high bandwidth mode */
1186 tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 tmp = (tmp >> 11) & 0x03;
1188 tmp *= 8 /* applies to entire frame */;
1189 limit += limit * tmp;
1190 }
1191 return limit;
1192}
1193
1194#define is_active(dum) ((dum->port_status & \
1195 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1196 USB_PORT_STAT_SUSPEND)) \
1197 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1198
1199static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1200{
1201 int i;
1202
1203 if (!is_active (dum))
1204 return NULL;
1205 if ((address & ~USB_DIR_IN) == 0)
1206 return &dum->ep [0];
1207 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1208 struct dummy_ep *ep = &dum->ep [i];
1209
1210 if (!ep->desc)
1211 continue;
1212 if (ep->desc->bEndpointAddress == address)
1213 return ep;
1214 }
1215 return NULL;
1216}
1217
1218#undef is_active
1219
1220#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1221#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1222#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1223#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1224#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1225#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1226
1227/* drive both sides of the transfers; looks like irq handlers to
1228 * both drivers except the callbacks aren't in_irq().
1229 */
1230static void dummy_timer (unsigned long _dum)
1231{
1232 struct dummy *dum = (struct dummy *) _dum;
1233 struct urbp *urbp, *tmp;
1234 unsigned long flags;
1235 int limit, total;
1236 int i;
1237
1238 /* simplistic model for one frame's bandwidth */
1239 switch (dum->gadget.speed) {
1240 case USB_SPEED_LOW:
1241 total = 8/*bytes*/ * 12/*packets*/;
1242 break;
1243 case USB_SPEED_FULL:
1244 total = 64/*bytes*/ * 19/*packets*/;
1245 break;
1246 case USB_SPEED_HIGH:
1247 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1248 break;
1249 default:
1250 dev_err (dummy_dev(dum), "bogus device speed\n");
1251 return;
1252 }
1253
1254 /* FIXME if HZ != 1000 this will probably misbehave ... */
1255
1256 /* look at each urb queued by the host side driver */
1257 spin_lock_irqsave (&dum->lock, flags);
1258
1259 if (!dum->udev) {
1260 dev_err (dummy_dev(dum),
1261 "timer fired with no URBs pending?\n");
1262 spin_unlock_irqrestore (&dum->lock, flags);
1263 return;
1264 }
1265
1266 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1267 if (!ep_name [i])
1268 break;
1269 dum->ep [i].already_seen = 0;
1270 }
1271
1272restart:
1273 list_for_each_entry_safe (urbp, tmp, &dum->urbp_list, urbp_list) {
1274 struct urb *urb;
1275 struct dummy_request *req;
1276 u8 address;
1277 struct dummy_ep *ep = NULL;
1278 int type;
1279
1280 urb = urbp->urb;
1281 if (urb->status != -EINPROGRESS) {
1282 /* likely it was just unlinked */
1283 goto return_urb;
Alan Stern391eca92005-05-10 15:34:16 -04001284 } else if (dum->rh_state != DUMMY_RH_RUNNING)
1285 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 type = usb_pipetype (urb->pipe);
1287
1288 /* used up this frame's non-periodic bandwidth?
1289 * FIXME there's infinite bandwidth for control and
1290 * periodic transfers ... unrealistic.
1291 */
1292 if (total <= 0 && type == PIPE_BULK)
1293 continue;
1294
1295 /* find the gadget's ep for this request (if configured) */
1296 address = usb_pipeendpoint (urb->pipe);
1297 if (usb_pipein (urb->pipe))
1298 address |= USB_DIR_IN;
1299 ep = find_endpoint(dum, address);
1300 if (!ep) {
1301 /* set_configuration() disagreement */
1302 dev_dbg (dummy_dev(dum),
1303 "no ep configured for urb %p\n",
1304 urb);
1305 maybe_set_status (urb, -EPROTO);
1306 goto return_urb;
1307 }
1308
1309 if (ep->already_seen)
1310 continue;
1311 ep->already_seen = 1;
1312 if (ep == &dum->ep [0] && urb->error_count) {
1313 ep->setup_stage = 1; /* a new urb */
1314 urb->error_count = 0;
1315 }
1316 if (ep->halted && !ep->setup_stage) {
1317 /* NOTE: must not be iso! */
1318 dev_dbg (dummy_dev(dum), "ep %s halted, urb %p\n",
1319 ep->ep.name, urb);
1320 maybe_set_status (urb, -EPIPE);
1321 goto return_urb;
1322 }
1323 /* FIXME make sure both ends agree on maxpacket */
1324
1325 /* handle control requests */
1326 if (ep == &dum->ep [0] && ep->setup_stage) {
1327 struct usb_ctrlrequest setup;
1328 int value = 1;
1329 struct dummy_ep *ep2;
Alan Sterncc095b02005-05-10 15:28:38 -04001330 unsigned w_index;
1331 unsigned w_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
Alan Sterncc095b02005-05-10 15:28:38 -04001334 w_index = le16_to_cpu(setup.wIndex);
1335 w_value = le16_to_cpu(setup.wValue);
1336 if (le16_to_cpu(setup.wLength) !=
1337 urb->transfer_buffer_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 maybe_set_status (urb, -EOVERFLOW);
1339 goto return_urb;
1340 }
1341
1342 /* paranoia, in case of stale queued data */
1343 list_for_each_entry (req, &ep->queue, queue) {
1344 list_del_init (&req->queue);
1345 req->req.status = -EOVERFLOW;
Alan Sternd9b76252005-05-03 16:15:43 -04001346 dev_dbg (udc_dev(dum), "stale req = %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 req);
1348
1349 spin_unlock (&dum->lock);
1350 req->req.complete (&ep->ep, &req->req);
1351 spin_lock (&dum->lock);
1352 ep->already_seen = 0;
1353 goto restart;
1354 }
1355
1356 /* gadget driver never sees set_address or operations
1357 * on standard feature flags. some hardware doesn't
1358 * even expose them.
1359 */
1360 ep->last_io = jiffies;
1361 ep->setup_stage = 0;
1362 ep->halted = 0;
1363 switch (setup.bRequest) {
1364 case USB_REQ_SET_ADDRESS:
1365 if (setup.bRequestType != Dev_Request)
1366 break;
Alan Sterncc095b02005-05-10 15:28:38 -04001367 dum->address = w_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 maybe_set_status (urb, 0);
Alan Sternd9b76252005-05-03 16:15:43 -04001369 dev_dbg (udc_dev(dum), "set_address = %d\n",
Alan Sterncc095b02005-05-10 15:28:38 -04001370 w_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 value = 0;
1372 break;
1373 case USB_REQ_SET_FEATURE:
1374 if (setup.bRequestType == Dev_Request) {
1375 value = 0;
Alan Sterncc095b02005-05-10 15:28:38 -04001376 switch (w_value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 case USB_DEVICE_REMOTE_WAKEUP:
1378 break;
Alan Stern5742b0c2005-05-02 11:25:17 -04001379 case USB_DEVICE_B_HNP_ENABLE:
1380 dum->gadget.b_hnp_enable = 1;
1381 break;
1382 case USB_DEVICE_A_HNP_SUPPORT:
1383 dum->gadget.a_hnp_support = 1;
1384 break;
1385 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1386 dum->gadget.a_alt_hnp_support
1387 = 1;
1388 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 default:
1390 value = -EOPNOTSUPP;
1391 }
1392 if (value == 0) {
1393 dum->devstatus |=
Alan Sterncc095b02005-05-10 15:28:38 -04001394 (1 << w_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 maybe_set_status (urb, 0);
1396 }
1397
1398 } else if (setup.bRequestType == Ep_Request) {
1399 // endpoint halt
Alan Sterncc095b02005-05-10 15:28:38 -04001400 ep2 = find_endpoint (dum, w_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (!ep2) {
1402 value = -EOPNOTSUPP;
1403 break;
1404 }
1405 ep2->halted = 1;
1406 value = 0;
1407 maybe_set_status (urb, 0);
1408 }
1409 break;
1410 case USB_REQ_CLEAR_FEATURE:
1411 if (setup.bRequestType == Dev_Request) {
Alan Sterncc095b02005-05-10 15:28:38 -04001412 switch (w_value) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 case USB_DEVICE_REMOTE_WAKEUP:
1414 dum->devstatus &= ~(1 <<
1415 USB_DEVICE_REMOTE_WAKEUP);
1416 value = 0;
1417 maybe_set_status (urb, 0);
1418 break;
1419 default:
1420 value = -EOPNOTSUPP;
1421 break;
1422 }
1423 } else if (setup.bRequestType == Ep_Request) {
1424 // endpoint halt
Alan Sterncc095b02005-05-10 15:28:38 -04001425 ep2 = find_endpoint (dum, w_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (!ep2) {
1427 value = -EOPNOTSUPP;
1428 break;
1429 }
1430 ep2->halted = 0;
1431 value = 0;
1432 maybe_set_status (urb, 0);
1433 }
1434 break;
1435 case USB_REQ_GET_STATUS:
1436 if (setup.bRequestType == Dev_InRequest
1437 || setup.bRequestType
1438 == Intf_InRequest
1439 || setup.bRequestType
1440 == Ep_InRequest
1441 ) {
1442 char *buf;
1443
1444 // device: remote wakeup, selfpowered
1445 // interface: nothing
1446 // endpoint: halt
1447 buf = (char *)urb->transfer_buffer;
1448 if (urb->transfer_buffer_length > 0) {
1449 if (setup.bRequestType ==
1450 Ep_InRequest) {
Alan Sterncc095b02005-05-10 15:28:38 -04001451 ep2 = find_endpoint (dum, w_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (!ep2) {
1453 value = -EOPNOTSUPP;
1454 break;
1455 }
1456 buf [0] = ep2->halted;
1457 } else if (setup.bRequestType ==
1458 Dev_InRequest) {
1459 buf [0] = (u8)
1460 dum->devstatus;
1461 } else
1462 buf [0] = 0;
1463 }
1464 if (urb->transfer_buffer_length > 1)
1465 buf [1] = 0;
1466 urb->actual_length = min (2,
1467 urb->transfer_buffer_length);
1468 value = 0;
1469 maybe_set_status (urb, 0);
1470 }
1471 break;
1472 }
1473
1474 /* gadget driver handles all other requests. block
1475 * until setup() returns; no reentrancy issues etc.
1476 */
1477 if (value > 0) {
1478 spin_unlock (&dum->lock);
1479 value = dum->driver->setup (&dum->gadget,
1480 &setup);
1481 spin_lock (&dum->lock);
1482
1483 if (value >= 0) {
1484 /* no delays (max 64KB data stage) */
1485 limit = 64*1024;
1486 goto treat_control_like_bulk;
1487 }
1488 /* error, see below */
1489 }
1490
1491 if (value < 0) {
1492 if (value != -EOPNOTSUPP)
Alan Sternd9b76252005-05-03 16:15:43 -04001493 dev_dbg (udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 "setup --> %d\n",
1495 value);
1496 maybe_set_status (urb, -EPIPE);
1497 urb->actual_length = 0;
1498 }
1499
1500 goto return_urb;
1501 }
1502
1503 /* non-control requests */
1504 limit = total;
1505 switch (usb_pipetype (urb->pipe)) {
1506 case PIPE_ISOCHRONOUS:
1507 /* FIXME is it urb->interval since the last xfer?
1508 * use urb->iso_frame_desc[i].
1509 * complete whether or not ep has requests queued.
1510 * report random errors, to debug drivers.
1511 */
1512 limit = max (limit, periodic_bytes (dum, ep));
1513 maybe_set_status (urb, -ENOSYS);
1514 break;
1515
1516 case PIPE_INTERRUPT:
1517 /* FIXME is it urb->interval since the last xfer?
1518 * this almost certainly polls too fast.
1519 */
1520 limit = max (limit, periodic_bytes (dum, ep));
1521 /* FALLTHROUGH */
1522
1523 // case PIPE_BULK: case PIPE_CONTROL:
1524 default:
1525 treat_control_like_bulk:
1526 ep->last_io = jiffies;
1527 total = transfer (dum, urb, ep, limit);
1528 break;
1529 }
1530
1531 /* incomplete transfer? */
1532 if (urb->status == -EINPROGRESS)
1533 continue;
1534
1535return_urb:
1536 urb->hcpriv = NULL;
1537 list_del (&urbp->urbp_list);
1538 kfree (urbp);
1539 if (ep)
1540 ep->already_seen = ep->setup_stage = 0;
1541
1542 spin_unlock (&dum->lock);
1543 usb_hcd_giveback_urb (dummy_to_hcd(dum), urb, NULL);
1544 spin_lock (&dum->lock);
1545
1546 goto restart;
1547 }
1548
Alan Stern391eca92005-05-10 15:34:16 -04001549 if (list_empty (&dum->urbp_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 usb_put_dev (dum->udev);
1551 dum->udev = NULL;
Alan Stern391eca92005-05-10 15:34:16 -04001552 } else if (dum->rh_state == DUMMY_RH_RUNNING) {
1553 /* want a 1 msec delay here */
1554 mod_timer (&dum->timer, jiffies + msecs_to_jiffies(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556
1557 spin_unlock_irqrestore (&dum->lock, flags);
1558}
1559
1560/*-------------------------------------------------------------------------*/
1561
1562#define PORT_C_MASK \
Alan Sternc2db8b52005-04-29 16:30:48 -04001563 ((USB_PORT_STAT_C_CONNECTION \
1564 | USB_PORT_STAT_C_ENABLE \
1565 | USB_PORT_STAT_C_SUSPEND \
1566 | USB_PORT_STAT_C_OVERCURRENT \
1567 | USB_PORT_STAT_C_RESET) << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1570{
1571 struct dummy *dum;
1572 unsigned long flags;
Alan Stern391eca92005-05-10 15:34:16 -04001573 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 dum = hcd_to_dummy (hcd);
1576
1577 spin_lock_irqsave (&dum->lock, flags);
Alan Stern3cf0a222005-11-29 12:08:15 -05001578 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
Alan Stern391eca92005-05-10 15:34:16 -04001579 goto done;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001580
1581 if (dum->resuming && time_after_eq (jiffies, dum->re_timeout)) {
1582 dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1583 dum->port_status &= ~USB_PORT_STAT_SUSPEND;
1584 set_link_state (dum);
1585 }
1586
Alan Stern391eca92005-05-10 15:34:16 -04001587 if ((dum->port_status & PORT_C_MASK) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 *buf = (1 << 1);
1589 dev_dbg (dummy_dev(dum), "port status 0x%08x has changes\n",
Alan Stern391eca92005-05-10 15:34:16 -04001590 dum->port_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 retval = 1;
Alan Stern391eca92005-05-10 15:34:16 -04001592 if (dum->rh_state == DUMMY_RH_SUSPENDED)
1593 usb_hcd_resume_root_hub (hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
Alan Stern391eca92005-05-10 15:34:16 -04001595done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 spin_unlock_irqrestore (&dum->lock, flags);
1597 return retval;
1598}
1599
1600static inline void
1601hub_descriptor (struct usb_hub_descriptor *desc)
1602{
1603 memset (desc, 0, sizeof *desc);
1604 desc->bDescriptorType = 0x29;
1605 desc->bDescLength = 9;
Alan Sterncc095b02005-05-10 15:28:38 -04001606 desc->wHubCharacteristics = (__force __u16)
1607 (__constant_cpu_to_le16 (0x0001));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 desc->bNbrPorts = 1;
1609 desc->bitmap [0] = 0xff;
1610 desc->bitmap [1] = 0xff;
1611}
1612
1613static int dummy_hub_control (
1614 struct usb_hcd *hcd,
1615 u16 typeReq,
1616 u16 wValue,
1617 u16 wIndex,
1618 char *buf,
1619 u16 wLength
1620) {
1621 struct dummy *dum;
1622 int retval = 0;
1623 unsigned long flags;
1624
Alan Stern3cf0a222005-11-29 12:08:15 -05001625 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
Alan Stern391eca92005-05-10 15:34:16 -04001626 return -ETIMEDOUT;
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 dum = hcd_to_dummy (hcd);
1629 spin_lock_irqsave (&dum->lock, flags);
1630 switch (typeReq) {
1631 case ClearHubFeature:
1632 break;
1633 case ClearPortFeature:
1634 switch (wValue) {
1635 case USB_PORT_FEAT_SUSPEND:
Alan Sternc2db8b52005-04-29 16:30:48 -04001636 if (dum->port_status & USB_PORT_STAT_SUSPEND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 /* 20msec resume signaling */
1638 dum->resuming = 1;
1639 dum->re_timeout = jiffies +
Alan Sternf1c39fa2005-05-03 16:24:04 -04001640 msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
1642 break;
1643 case USB_PORT_FEAT_POWER:
Alan Sternf1c39fa2005-05-03 16:24:04 -04001644 if (dum->port_status & USB_PORT_STAT_POWER)
1645 dev_dbg (dummy_dev(dum), "power-off\n");
1646 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 default:
1648 dum->port_status &= ~(1 << wValue);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001649 set_link_state (dum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
1651 break;
1652 case GetHubDescriptor:
1653 hub_descriptor ((struct usb_hub_descriptor *) buf);
1654 break;
1655 case GetHubStatus:
Alan Sterncc095b02005-05-10 15:28:38 -04001656 *(__le32 *) buf = __constant_cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 break;
1658 case GetPortStatus:
1659 if (wIndex != 1)
1660 retval = -EPIPE;
1661
1662 /* whoever resets or resumes must GetPortStatus to
1663 * complete it!!
1664 */
Alan Sternf1c39fa2005-05-03 16:24:04 -04001665 if (dum->resuming &&
1666 time_after_eq (jiffies, dum->re_timeout)) {
Alan Sternc2db8b52005-04-29 16:30:48 -04001667 dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1668 dum->port_status &= ~USB_PORT_STAT_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
Alan Sternf1c39fa2005-05-03 16:24:04 -04001670 if ((dum->port_status & USB_PORT_STAT_RESET) != 0 &&
1671 time_after_eq (jiffies, dum->re_timeout)) {
Alan Sternc2db8b52005-04-29 16:30:48 -04001672 dum->port_status |= (USB_PORT_STAT_C_RESET << 16);
1673 dum->port_status &= ~USB_PORT_STAT_RESET;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001674 if (dum->pullup) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 dum->port_status |= USB_PORT_STAT_ENABLE;
1676 /* give it the best speed we agree on */
1677 dum->gadget.speed = dum->driver->speed;
1678 dum->gadget.ep0->maxpacket = 64;
1679 switch (dum->gadget.speed) {
1680 case USB_SPEED_HIGH:
1681 dum->port_status |=
1682 USB_PORT_STAT_HIGH_SPEED;
1683 break;
1684 case USB_SPEED_LOW:
1685 dum->gadget.ep0->maxpacket = 8;
1686 dum->port_status |=
1687 USB_PORT_STAT_LOW_SPEED;
1688 break;
1689 default:
1690 dum->gadget.speed = USB_SPEED_FULL;
1691 break;
1692 }
1693 }
1694 }
Alan Sternf1c39fa2005-05-03 16:24:04 -04001695 set_link_state (dum);
Alan Sterncc095b02005-05-10 15:28:38 -04001696 ((__le16 *) buf)[0] = cpu_to_le16 (dum->port_status);
1697 ((__le16 *) buf)[1] = cpu_to_le16 (dum->port_status >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 break;
1699 case SetHubFeature:
1700 retval = -EPIPE;
1701 break;
1702 case SetPortFeature:
1703 switch (wValue) {
1704 case USB_PORT_FEAT_SUSPEND:
Alan Sternf1c39fa2005-05-03 16:24:04 -04001705 if (dum->active) {
Alan Sternc2db8b52005-04-29 16:30:48 -04001706 dum->port_status |= USB_PORT_STAT_SUSPEND;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001707
1708 /* HNP would happen here; for now we
1709 * assume b_bus_req is always true.
1710 */
1711 set_link_state (dum);
1712 if (((1 << USB_DEVICE_B_HNP_ENABLE)
1713 & dum->devstatus) != 0)
1714 dev_dbg (dummy_dev(dum),
Alan Stern5742b0c2005-05-02 11:25:17 -04001715 "no HNP yet!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717 break;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001718 case USB_PORT_FEAT_POWER:
1719 dum->port_status |= USB_PORT_STAT_POWER;
1720 set_link_state (dum);
1721 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 case USB_PORT_FEAT_RESET:
Alan Sternf1c39fa2005-05-03 16:24:04 -04001723 /* if it's already enabled, disable */
1724 dum->port_status &= ~(USB_PORT_STAT_ENABLE
1725 | USB_PORT_STAT_LOW_SPEED
1726 | USB_PORT_STAT_HIGH_SPEED);
Alan Stern391eca92005-05-10 15:34:16 -04001727 dum->devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 /* 50msec reset signaling */
1729 dum->re_timeout = jiffies + msecs_to_jiffies(50);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001730 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 default:
Alan Sternf1c39fa2005-05-03 16:24:04 -04001732 if ((dum->port_status & USB_PORT_STAT_POWER) != 0) {
1733 dum->port_status |= (1 << wValue);
1734 set_link_state (dum);
1735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
1737 break;
1738
1739 default:
1740 dev_dbg (dummy_dev(dum),
1741 "hub control req%04x v%04x i%04x l%d\n",
1742 typeReq, wValue, wIndex, wLength);
1743
1744 /* "protocol stall" on error */
1745 retval = -EPIPE;
1746 }
1747 spin_unlock_irqrestore (&dum->lock, flags);
Alan Stern685eb932005-05-03 16:27:26 -04001748
1749 if ((dum->port_status & PORT_C_MASK) != 0)
1750 usb_hcd_poll_rh_status (hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return retval;
1752}
1753
Alan Stern0c0382e2005-10-13 17:08:02 -04001754static int dummy_bus_suspend (struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04001755{
1756 struct dummy *dum = hcd_to_dummy (hcd);
1757
Alan Stern3cf0a222005-11-29 12:08:15 -05001758 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __FUNCTION__);
1759
Alan Stern391eca92005-05-10 15:34:16 -04001760 spin_lock_irq (&dum->lock);
1761 dum->rh_state = DUMMY_RH_SUSPENDED;
1762 set_link_state (dum);
Alan Stern3cf0a222005-11-29 12:08:15 -05001763 hcd->state = HC_STATE_SUSPENDED;
Alan Stern391eca92005-05-10 15:34:16 -04001764 spin_unlock_irq (&dum->lock);
1765 return 0;
1766}
1767
Alan Stern0c0382e2005-10-13 17:08:02 -04001768static int dummy_bus_resume (struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04001769{
1770 struct dummy *dum = hcd_to_dummy (hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05001771 int rc = 0;
1772
1773 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __FUNCTION__);
Alan Stern391eca92005-05-10 15:34:16 -04001774
1775 spin_lock_irq (&dum->lock);
Alan Stern3cf0a222005-11-29 12:08:15 -05001776 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
1777 dev_warn (&hcd->self.root_hub->dev, "HC isn't running!\n");
1778 rc = -ENODEV;
1779 } else {
1780 dum->rh_state = DUMMY_RH_RUNNING;
1781 set_link_state (dum);
1782 if (!list_empty(&dum->urbp_list))
1783 mod_timer (&dum->timer, jiffies);
1784 hcd->state = HC_STATE_RUNNING;
1785 }
Alan Stern391eca92005-05-10 15:34:16 -04001786 spin_unlock_irq (&dum->lock);
Alan Stern3cf0a222005-11-29 12:08:15 -05001787 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04001788}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790/*-------------------------------------------------------------------------*/
1791
1792static inline ssize_t
1793show_urb (char *buf, size_t size, struct urb *urb)
1794{
1795 int ep = usb_pipeendpoint (urb->pipe);
1796
1797 return snprintf (buf, size,
1798 "urb/%p %s ep%d%s%s len %d/%d\n",
1799 urb,
1800 ({ char *s;
1801 switch (urb->dev->speed) {
1802 case USB_SPEED_LOW: s = "ls"; break;
1803 case USB_SPEED_FULL: s = "fs"; break;
1804 case USB_SPEED_HIGH: s = "hs"; break;
1805 default: s = "?"; break;
1806 }; s; }),
1807 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
1808 ({ char *s; \
1809 switch (usb_pipetype (urb->pipe)) { \
1810 case PIPE_CONTROL: s = ""; break; \
1811 case PIPE_BULK: s = "-bulk"; break; \
1812 case PIPE_INTERRUPT: s = "-int"; break; \
1813 default: s = "-iso"; break; \
1814 }; s;}),
1815 urb->actual_length, urb->transfer_buffer_length);
1816}
1817
1818static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -04001819show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
1821 struct usb_hcd *hcd = dev_get_drvdata (dev);
1822 struct dummy *dum = hcd_to_dummy (hcd);
1823 struct urbp *urbp;
1824 size_t size = 0;
1825 unsigned long flags;
1826
1827 spin_lock_irqsave (&dum->lock, flags);
1828 list_for_each_entry (urbp, &dum->urbp_list, urbp_list) {
1829 size_t temp;
1830
1831 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
1832 buf += temp;
1833 size += temp;
1834 }
1835 spin_unlock_irqrestore (&dum->lock, flags);
1836
1837 return size;
1838}
1839static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
1840
1841static int dummy_start (struct usb_hcd *hcd)
1842{
1843 struct dummy *dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 dum = hcd_to_dummy (hcd);
1846
1847 /*
1848 * MASTER side init ... we emulate a root hub that'll only ever
1849 * talk to one device (the slave side). Also appears in sysfs,
1850 * just like more familiar pci-based HCDs.
1851 */
1852 spin_lock_init (&dum->lock);
1853 init_timer (&dum->timer);
1854 dum->timer.function = dummy_timer;
1855 dum->timer.data = (unsigned long) dum;
Alan Stern391eca92005-05-10 15:34:16 -04001856 dum->rh_state = DUMMY_RH_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 INIT_LIST_HEAD (&dum->urbp_list);
1859
Alan Sternbc96c0a2005-04-25 11:21:31 -04001860 /* only show a low-power port: just 8mA */
1861 hcd->power_budget = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 hcd->state = HC_STATE_RUNNING;
Alan Stern685eb932005-05-03 16:27:26 -04001863 hcd->uses_new_polling = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Alan Stern5742b0c2005-05-02 11:25:17 -04001865#ifdef CONFIG_USB_OTG
1866 hcd->self.otg_port = 1;
1867#endif
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
1870 device_create_file (dummy_dev(dum), &dev_attr_urbs);
1871 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873
1874static void dummy_stop (struct usb_hcd *hcd)
1875{
1876 struct dummy *dum;
1877
1878 dum = hcd_to_dummy (hcd);
1879
1880 device_remove_file (dummy_dev(dum), &dev_attr_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 usb_gadget_unregister_driver (dum->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 dev_info (dummy_dev(dum), "stopped\n");
1883}
1884
1885/*-------------------------------------------------------------------------*/
1886
1887static int dummy_h_get_frame (struct usb_hcd *hcd)
1888{
1889 return dummy_g_get_frame (NULL);
1890}
1891
1892static const struct hc_driver dummy_hcd = {
1893 .description = (char *) driver_name,
1894 .product_desc = "Dummy host controller",
1895 .hcd_priv_size = sizeof(struct dummy),
1896
1897 .flags = HCD_USB2,
1898
1899 .start = dummy_start,
1900 .stop = dummy_stop,
1901
1902 .urb_enqueue = dummy_urb_enqueue,
1903 .urb_dequeue = dummy_urb_dequeue,
1904
1905 .get_frame_number = dummy_h_get_frame,
1906
1907 .hub_status_data = dummy_hub_status,
1908 .hub_control = dummy_hub_control,
Alan Stern0c0382e2005-10-13 17:08:02 -04001909 .bus_suspend = dummy_bus_suspend,
1910 .bus_resume = dummy_bus_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911};
1912
Alan Stern8364d6b2005-11-14 12:16:30 -05001913static int dummy_hcd_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
1915 struct usb_hcd *hcd;
1916 int retval;
1917
Alan Stern8364d6b2005-11-14 12:16:30 -05001918 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Alan Stern8364d6b2005-11-14 12:16:30 -05001920 hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, pdev->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 if (!hcd)
1922 return -ENOMEM;
1923 the_controller = hcd_to_dummy (hcd);
1924
1925 retval = usb_add_hcd(hcd, 0, 0);
1926 if (retval != 0) {
1927 usb_put_hcd (hcd);
1928 the_controller = NULL;
1929 }
1930 return retval;
1931}
1932
Alan Stern8364d6b2005-11-14 12:16:30 -05001933static int dummy_hcd_remove (struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
1935 struct usb_hcd *hcd;
1936
Alan Stern8364d6b2005-11-14 12:16:30 -05001937 hcd = platform_get_drvdata (pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 usb_remove_hcd (hcd);
1939 usb_put_hcd (hcd);
1940 the_controller = NULL;
Alan Sternd9b76252005-05-03 16:15:43 -04001941 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
Alan Stern8364d6b2005-11-14 12:16:30 -05001944static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
Alan Stern391eca92005-05-10 15:34:16 -04001945{
1946 struct usb_hcd *hcd;
Alan Stern3cf0a222005-11-29 12:08:15 -05001947 struct dummy *dum;
1948 int rc = 0;
Alan Stern391eca92005-05-10 15:34:16 -04001949
Alan Stern8364d6b2005-11-14 12:16:30 -05001950 dev_dbg (&pdev->dev, "%s\n", __FUNCTION__);
Alan Stern391eca92005-05-10 15:34:16 -04001951
Alan Stern3cf0a222005-11-29 12:08:15 -05001952 hcd = platform_get_drvdata (pdev);
1953 dum = hcd_to_dummy (hcd);
1954 if (dum->rh_state == DUMMY_RH_RUNNING) {
1955 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
1956 rc = -EBUSY;
1957 } else
1958 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1959 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04001960}
1961
Alan Stern8364d6b2005-11-14 12:16:30 -05001962static int dummy_hcd_resume (struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04001963{
1964 struct usb_hcd *hcd;
1965
Alan Stern8364d6b2005-11-14 12:16:30 -05001966 dev_dbg (&pdev->dev, "%s\n", __FUNCTION__);
Alan Stern391eca92005-05-10 15:34:16 -04001967
Alan Stern3cf0a222005-11-29 12:08:15 -05001968 hcd = platform_get_drvdata (pdev);
1969 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Alan Stern391eca92005-05-10 15:34:16 -04001970 usb_hcd_poll_rh_status (hcd);
1971 return 0;
1972}
1973
Russell King3ae5eae2005-11-09 22:32:44 +00001974static struct platform_driver dummy_hcd_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04001975 .probe = dummy_hcd_probe,
1976 .remove = dummy_hcd_remove,
Alan Stern391eca92005-05-10 15:34:16 -04001977 .suspend = dummy_hcd_suspend,
1978 .resume = dummy_hcd_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001979 .driver = {
1980 .name = (char *) driver_name,
1981 .owner = THIS_MODULE,
1982 },
Alan Sternd9b76252005-05-03 16:15:43 -04001983};
1984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985/*-------------------------------------------------------------------------*/
1986
Alan Sternd9b76252005-05-03 16:15:43 -04001987/* These don't need to do anything because the pdev structures are
1988 * statically allocated. */
1989static void
1990dummy_udc_release (struct device *dev) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Alan Sternd9b76252005-05-03 16:15:43 -04001992static void
1993dummy_hcd_release (struct device *dev) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Alan Sternd9b76252005-05-03 16:15:43 -04001995static struct platform_device the_udc_pdev = {
1996 .name = (char *) gadget_name,
1997 .id = -1,
1998 .dev = {
1999 .release = dummy_udc_release,
2000 },
2001};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Alan Sternd9b76252005-05-03 16:15:43 -04002003static struct platform_device the_hcd_pdev = {
2004 .name = (char *) driver_name,
2005 .id = -1,
2006 .dev = {
2007 .release = dummy_hcd_release,
2008 },
2009};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011static int __init init (void)
2012{
2013 int retval;
2014
2015 if (usb_disabled ())
2016 return -ENODEV;
Alan Sternd9b76252005-05-03 16:15:43 -04002017
Russell King3ae5eae2005-11-09 22:32:44 +00002018 retval = platform_driver_register (&dummy_hcd_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002019 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return retval;
Alan Sternd9b76252005-05-03 16:15:43 -04002021
Russell King3ae5eae2005-11-09 22:32:44 +00002022 retval = platform_driver_register (&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002023 if (retval < 0)
2024 goto err_register_udc_driver;
2025
2026 retval = platform_device_register (&the_hcd_pdev);
2027 if (retval < 0)
2028 goto err_register_hcd;
2029
2030 retval = platform_device_register (&the_udc_pdev);
2031 if (retval < 0)
2032 goto err_register_udc;
2033 return retval;
2034
2035err_register_udc:
2036 platform_device_unregister (&the_hcd_pdev);
2037err_register_hcd:
Russell King3ae5eae2005-11-09 22:32:44 +00002038 platform_driver_unregister (&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002039err_register_udc_driver:
Russell King3ae5eae2005-11-09 22:32:44 +00002040 platform_driver_unregister (&dummy_hcd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return retval;
2042}
2043module_init (init);
2044
2045static void __exit cleanup (void)
2046{
Alan Sternd9b76252005-05-03 16:15:43 -04002047 platform_device_unregister (&the_udc_pdev);
2048 platform_device_unregister (&the_hcd_pdev);
Russell King3ae5eae2005-11-09 22:32:44 +00002049 platform_driver_unregister (&dummy_udc_driver);
2050 platform_driver_unregister (&dummy_hcd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052module_exit (cleanup);