blob: fcbff1cd180c0b84cd84dee7803890096cbc5bae [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/module.h>
38#include <linux/kernel.h>
39#include <linux/delay.h>
40#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/errno.h>
43#include <linux/init.h>
44#include <linux/timer.h>
45#include <linux/list.h>
46#include <linux/interrupt.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010047#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/usb.h>
David Brownell9454a572007-10-04 18:05:17 -070049#include <linux/usb/gadget.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020050#include <linux/usb/hcd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#include <asm/byteorder.h>
53#include <asm/io.h>
54#include <asm/irq.h>
55#include <asm/system.h>
56#include <asm/unaligned.h>
57
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define DRIVER_DESC "USB Host+Gadget Emulator"
Alan Stern391eca92005-05-10 15:34:16 -040060#define DRIVER_VERSION "02 May 2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Alan Sterncaf29f62007-12-06 11:10:39 -050062#define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static const char driver_name [] = "dummy_hcd";
65static const char driver_desc [] = "USB Host+Gadget Emulator";
66
67static const char gadget_name [] = "dummy_udc";
68
69MODULE_DESCRIPTION (DRIVER_DESC);
70MODULE_AUTHOR ("David Brownell");
71MODULE_LICENSE ("GPL");
72
73/*-------------------------------------------------------------------------*/
74
75/* gadget side driver data structres */
76struct dummy_ep {
77 struct list_head queue;
78 unsigned long last_io; /* jiffies timestamp */
79 struct usb_gadget *gadget;
80 const struct usb_endpoint_descriptor *desc;
81 struct usb_ep ep;
82 unsigned halted : 1;
Alan Stern851a5262008-08-14 15:48:30 -040083 unsigned wedged : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 unsigned already_seen : 1;
85 unsigned setup_stage : 1;
86};
87
88struct dummy_request {
89 struct list_head queue; /* ep's requests */
90 struct usb_request req;
91};
92
93static inline struct dummy_ep *usb_ep_to_dummy_ep (struct usb_ep *_ep)
94{
95 return container_of (_ep, struct dummy_ep, ep);
96}
97
98static inline struct dummy_request *usb_request_to_dummy_request
99 (struct usb_request *_req)
100{
101 return container_of (_req, struct dummy_request, req);
102}
103
104/*-------------------------------------------------------------------------*/
105
106/*
107 * Every device has ep0 for control requests, plus up to 30 more endpoints,
108 * in one of two types:
109 *
110 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
111 * number can be changed. Names like "ep-a" are used for this type.
112 *
113 * - Fixed Function: in other cases. some characteristics may be mutable;
114 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
115 *
116 * Gadget drivers are responsible for not setting up conflicting endpoint
117 * configurations, illegal or unsupported packet lengths, and so on.
118 */
119
120static const char ep0name [] = "ep0";
121
122static const char *const ep_name [] = {
123 ep0name, /* everyone has ep0 */
124
125 /* act like a net2280: high speed, six configurable endpoints */
126 "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
127
128 /* or like pxa250: fifteen fixed function endpoints */
129 "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
130 "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
131 "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
132 "ep15in-int",
133
134 /* or like sa1100: two fixed function endpoints */
135 "ep1out-bulk", "ep2in-bulk",
136};
Tobias Klauser52950ed2005-12-11 16:20:08 +0100137#define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Alan Sternd9b76252005-05-03 16:15:43 -0400139/*-------------------------------------------------------------------------*/
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#define FIFO_SIZE 64
142
143struct urbp {
144 struct urb *urb;
145 struct list_head urbp_list;
146};
147
Alan Stern391eca92005-05-10 15:34:16 -0400148
149enum dummy_rh_state {
150 DUMMY_RH_RESET,
151 DUMMY_RH_SUSPENDED,
152 DUMMY_RH_RUNNING
153};
154
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300155struct dummy_hcd {
156 struct dummy *dum;
157 enum dummy_rh_state rh_state;
158 struct timer_list timer;
159 u32 port_status;
160 u32 old_status;
161 unsigned long re_timeout;
162
163 struct usb_device *udev;
164 struct list_head urbp_list;
165
166 unsigned active:1;
167 unsigned old_active:1;
168 unsigned resuming:1;
169};
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171struct dummy {
172 spinlock_t lock;
173
174 /*
175 * SLAVE/GADGET side support
176 */
177 struct dummy_ep ep [DUMMY_ENDPOINTS];
178 int address;
179 struct usb_gadget gadget;
180 struct usb_gadget_driver *driver;
181 struct dummy_request fifo_req;
182 u8 fifo_buf [FIFO_SIZE];
183 u16 devstatus;
Alan Stern391eca92005-05-10 15:34:16 -0400184 unsigned udc_suspended:1;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400185 unsigned pullup:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 /*
188 * MASTER/HOST side support
189 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300190 struct dummy_hcd *hs_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191};
192
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300193static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300195 return (struct dummy_hcd *) (hcd->hcd_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300198static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 return container_of((void *) dum, struct usb_hcd, hcd_priv);
201}
202
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300203static inline struct device *dummy_dev(struct dummy_hcd *dum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300205 return dummy_hcd_to_hcd(dum)->self.controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Alan Sternd9b76252005-05-03 16:15:43 -0400208static inline struct device *udc_dev (struct dummy *dum)
209{
210 return dum->gadget.dev.parent;
211}
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
214{
215 return container_of (ep->gadget, struct dummy, gadget);
216}
217
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300218static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300220 struct dummy *dum = container_of(gadget, struct dummy, gadget);
221 return dum->hs_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
225{
226 return container_of (dev, struct dummy, gadget.dev);
227}
228
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300229static struct dummy the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231/*-------------------------------------------------------------------------*/
232
Alan Sternf1c39fa2005-05-03 16:24:04 -0400233/* SLAVE/GADGET SIDE UTILITY ROUTINES */
234
235/* called with spinlock held */
236static void nuke (struct dummy *dum, struct dummy_ep *ep)
237{
238 while (!list_empty (&ep->queue)) {
239 struct dummy_request *req;
240
241 req = list_entry (ep->queue.next, struct dummy_request, queue);
242 list_del_init (&req->queue);
243 req->req.status = -ESHUTDOWN;
244
245 spin_unlock (&dum->lock);
246 req->req.complete (&ep->ep, &req->req);
247 spin_lock (&dum->lock);
248 }
249}
250
251/* caller must hold lock */
252static void
253stop_activity (struct dummy *dum)
254{
255 struct dummy_ep *ep;
256
257 /* prevent any more requests */
258 dum->address = 0;
259
260 /* The timer is left running so that outstanding URBs can fail */
261
262 /* nuke any pending requests first, so driver i/o is quiesced */
263 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
264 nuke (dum, ep);
265
266 /* driver now does any non-usb quiescing necessary */
267}
268
269/* caller must hold lock */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300270static void set_link_state(struct dummy_hcd *dum_hcd)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400271{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300272 struct dummy *dum = dum_hcd->dum;
273
274 dum_hcd->active = 0;
275 if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0)
276 dum_hcd->port_status = 0;
Alan Stern391eca92005-05-10 15:34:16 -0400277
278 /* UDC suspend must cause a disconnect */
279 else if (!dum->pullup || dum->udc_suspended) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300280 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
Alan Sternf1c39fa2005-05-03 16:24:04 -0400281 USB_PORT_STAT_ENABLE |
282 USB_PORT_STAT_LOW_SPEED |
283 USB_PORT_STAT_HIGH_SPEED |
284 USB_PORT_STAT_SUSPEND);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300285 if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0)
286 dum_hcd->port_status |=
287 (USB_PORT_STAT_C_CONNECTION << 16);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400288 } else {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300289 dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
290 if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) == 0)
291 dum_hcd->port_status |=
292 (USB_PORT_STAT_C_CONNECTION << 16);
293 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
294 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
295 else if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
296 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
297 dum_hcd->active = 1;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400298 }
299
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300300 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
301 dum_hcd->active)
302 dum_hcd->resuming = 0;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400303
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300304 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
305 (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
306 if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
307 (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
Alan Sternf1c39fa2005-05-03 16:24:04 -0400308 dum->driver) {
309 stop_activity (dum);
310 spin_unlock (&dum->lock);
311 dum->driver->disconnect (&dum->gadget);
312 spin_lock (&dum->lock);
313 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300314 } else if (dum_hcd->active != dum_hcd->old_active) {
315 if (dum_hcd->old_active && dum->driver->suspend) {
Alan Sternf1c39fa2005-05-03 16:24:04 -0400316 spin_unlock (&dum->lock);
317 dum->driver->suspend (&dum->gadget);
318 spin_lock (&dum->lock);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300319 } else if (!dum_hcd->old_active && dum->driver->resume) {
Alan Sternf1c39fa2005-05-03 16:24:04 -0400320 spin_unlock (&dum->lock);
321 dum->driver->resume (&dum->gadget);
322 spin_lock (&dum->lock);
323 }
324 }
325
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300326 dum_hcd->old_status = dum_hcd->port_status;
327 dum_hcd->old_active = dum_hcd->active;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400328}
329
330/*-------------------------------------------------------------------------*/
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332/* SLAVE/GADGET SIDE DRIVER
333 *
334 * This only tracks gadget state. All the work is done when the host
335 * side tries some (emulated) i/o operation. Real device controller
336 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
337 */
338
339#define is_enabled(dum) \
340 (dum->port_status & USB_PORT_STAT_ENABLE)
341
342static int
343dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
344{
345 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300346 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct dummy_ep *ep;
348 unsigned max;
349 int retval;
350
351 ep = usb_ep_to_dummy_ep (_ep);
352 if (!_ep || !desc || ep->desc || _ep->name == ep0name
353 || desc->bDescriptorType != USB_DT_ENDPOINT)
354 return -EINVAL;
355 dum = ep_to_dummy (ep);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300356 if (!dum->driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return -ESHUTDOWN;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300358 dum_hcd = dum->hs_hcd;
359 if (!is_enabled(dum_hcd))
360 return -ESHUTDOWN;
361
362 /*
363 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
364 * maximum packet size.
365 */
366 max = le16_to_cpu(desc->wMaxPacketSize) & 0x7ff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 /* drivers must not request bad settings, since lower levels
369 * (hardware or its drivers) may not check. some endpoints
370 * can't do iso, many have maxpacket limitations, etc.
371 *
372 * since this "hardware" driver is here to help debugging, we
373 * have some extra sanity checks. (there could be more though,
374 * especially for "ep9out" style fixed function ones.)
375 */
376 retval = -EINVAL;
377 switch (desc->bmAttributes & 0x03) {
378 case USB_ENDPOINT_XFER_BULK:
379 if (strstr (ep->ep.name, "-iso")
380 || strstr (ep->ep.name, "-int")) {
381 goto done;
382 }
383 switch (dum->gadget.speed) {
384 case USB_SPEED_HIGH:
385 if (max == 512)
386 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700387 goto done;
388 case USB_SPEED_FULL:
389 if (max == 8 || max == 16 || max == 32 || max == 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* we'll fake any legal size */
391 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700392 /* save a return statement */
393 default:
394 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396 break;
397 case USB_ENDPOINT_XFER_INT:
398 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
399 goto done;
400 /* real hardware might not handle all packet sizes */
401 switch (dum->gadget.speed) {
402 case USB_SPEED_HIGH:
403 if (max <= 1024)
404 break;
405 /* save a return statement */
406 case USB_SPEED_FULL:
407 if (max <= 64)
408 break;
409 /* save a return statement */
410 default:
411 if (max <= 8)
412 break;
413 goto done;
414 }
415 break;
416 case USB_ENDPOINT_XFER_ISOC:
417 if (strstr (ep->ep.name, "-bulk")
418 || strstr (ep->ep.name, "-int"))
419 goto done;
420 /* real hardware might not handle all packet sizes */
421 switch (dum->gadget.speed) {
422 case USB_SPEED_HIGH:
423 if (max <= 1024)
424 break;
425 /* save a return statement */
426 case USB_SPEED_FULL:
427 if (max <= 1023)
428 break;
429 /* save a return statement */
430 default:
431 goto done;
432 }
433 break;
434 default:
435 /* few chips support control except on ep0 */
436 goto done;
437 }
438
439 _ep->maxpacket = max;
440 ep->desc = desc;
441
Alan Sternd9b76252005-05-03 16:15:43 -0400442 dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 _ep->name,
444 desc->bEndpointAddress & 0x0f,
445 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
446 ({ char *val;
447 switch (desc->bmAttributes & 0x03) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +0300448 case USB_ENDPOINT_XFER_BULK:
449 val = "bulk";
450 break;
451 case USB_ENDPOINT_XFER_ISOC:
452 val = "iso";
453 break;
454 case USB_ENDPOINT_XFER_INT:
455 val = "intr";
456 break;
457 default:
458 val = "ctrl";
459 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }; val; }),
461 max);
462
463 /* at this point real hardware should be NAKing transfers
464 * to that endpoint, until a buffer is queued to it.
465 */
Alan Stern851a5262008-08-14 15:48:30 -0400466 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 retval = 0;
468done:
469 return retval;
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472static int dummy_disable (struct usb_ep *_ep)
473{
474 struct dummy_ep *ep;
475 struct dummy *dum;
476 unsigned long flags;
477 int retval;
478
479 ep = usb_ep_to_dummy_ep (_ep);
480 if (!_ep || !ep->desc || _ep->name == ep0name)
481 return -EINVAL;
482 dum = ep_to_dummy (ep);
483
484 spin_lock_irqsave (&dum->lock, flags);
485 ep->desc = NULL;
486 retval = 0;
487 nuke (dum, ep);
488 spin_unlock_irqrestore (&dum->lock, flags);
489
Alan Sternd9b76252005-05-03 16:15:43 -0400490 dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return retval;
492}
493
494static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -0400495dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 struct dummy_ep *ep;
498 struct dummy_request *req;
499
500 if (!_ep)
501 return NULL;
502 ep = usb_ep_to_dummy_ep (_ep);
503
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800504 req = kzalloc(sizeof(*req), mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (!req)
506 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 INIT_LIST_HEAD (&req->queue);
508 return &req->req;
509}
510
511static void
512dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
513{
514 struct dummy_ep *ep;
515 struct dummy_request *req;
516
517 ep = usb_ep_to_dummy_ep (_ep);
518 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
519 return;
520
521 req = usb_request_to_dummy_request (_req);
522 WARN_ON (!list_empty (&req->queue));
523 kfree (req);
524}
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526static void
527fifo_complete (struct usb_ep *ep, struct usb_request *req)
528{
529}
530
531static int
Olav Kongas5db539e2005-06-23 20:25:36 +0300532dummy_queue (struct usb_ep *_ep, struct usb_request *_req,
Al Viro55016f12005-10-21 03:21:58 -0400533 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 struct dummy_ep *ep;
536 struct dummy_request *req;
537 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300538 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 unsigned long flags;
540
541 req = usb_request_to_dummy_request (_req);
542 if (!_req || !list_empty (&req->queue) || !_req->complete)
543 return -EINVAL;
544
545 ep = usb_ep_to_dummy_ep (_ep);
546 if (!_ep || (!ep->desc && _ep->name != ep0name))
547 return -EINVAL;
548
549 dum = ep_to_dummy (ep);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300550 dum_hcd = dum->hs_hcd;
551 if (!dum->driver || !is_enabled(dum_hcd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return -ESHUTDOWN;
553
554#if 0
Alan Sternd9b76252005-05-03 16:15:43 -0400555 dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 ep, _req, _ep->name, _req->length, _req->buf);
557#endif
558
559 _req->status = -EINPROGRESS;
560 _req->actual = 0;
561 spin_lock_irqsave (&dum->lock, flags);
562
563 /* implement an emulated single-request FIFO */
564 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
565 list_empty (&dum->fifo_req.queue) &&
566 list_empty (&ep->queue) &&
567 _req->length <= FIFO_SIZE) {
568 req = &dum->fifo_req;
569 req->req = *_req;
570 req->req.buf = dum->fifo_buf;
571 memcpy (dum->fifo_buf, _req->buf, _req->length);
572 req->req.context = dum;
573 req->req.complete = fifo_complete;
574
David Brownellc728df72008-07-26 08:06:24 -0700575 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 spin_unlock (&dum->lock);
577 _req->actual = _req->length;
578 _req->status = 0;
579 _req->complete (_ep, _req);
580 spin_lock (&dum->lock);
David Brownellc728df72008-07-26 08:06:24 -0700581 } else
582 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 spin_unlock_irqrestore (&dum->lock, flags);
584
585 /* real hardware would likely enable transfers here, in case
586 * it'd been left NAKing.
587 */
588 return 0;
589}
590
591static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
592{
593 struct dummy_ep *ep;
594 struct dummy *dum;
595 int retval = -EINVAL;
596 unsigned long flags;
597 struct dummy_request *req = NULL;
598
599 if (!_ep || !_req)
600 return retval;
601 ep = usb_ep_to_dummy_ep (_ep);
602 dum = ep_to_dummy (ep);
603
604 if (!dum->driver)
605 return -ESHUTDOWN;
606
Alan Sternb4dbda12006-07-28 17:07:34 -0400607 local_irq_save (flags);
608 spin_lock (&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 list_for_each_entry (req, &ep->queue, queue) {
610 if (&req->req == _req) {
611 list_del_init (&req->queue);
612 _req->status = -ECONNRESET;
613 retval = 0;
614 break;
615 }
616 }
Alan Sternb4dbda12006-07-28 17:07:34 -0400617 spin_unlock (&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 if (retval == 0) {
Alan Sternd9b76252005-05-03 16:15:43 -0400620 dev_dbg (udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 "dequeued req %p from %s, len %d buf %p\n",
622 req, _ep->name, _req->length, _req->buf);
623 _req->complete (_ep, _req);
624 }
Alan Sternb4dbda12006-07-28 17:07:34 -0400625 local_irq_restore (flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return retval;
627}
628
629static int
Alan Stern851a5262008-08-14 15:48:30 -0400630dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 struct dummy_ep *ep;
633 struct dummy *dum;
634
635 if (!_ep)
636 return -EINVAL;
637 ep = usb_ep_to_dummy_ep (_ep);
638 dum = ep_to_dummy (ep);
639 if (!dum->driver)
640 return -ESHUTDOWN;
641 if (!value)
Alan Stern851a5262008-08-14 15:48:30 -0400642 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
644 !list_empty (&ep->queue))
645 return -EAGAIN;
Alan Stern851a5262008-08-14 15:48:30 -0400646 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 ep->halted = 1;
Alan Stern851a5262008-08-14 15:48:30 -0400648 if (wedged)
649 ep->wedged = 1;
650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /* FIXME clear emulated data toggle too */
652 return 0;
653}
654
Alan Stern851a5262008-08-14 15:48:30 -0400655static int
656dummy_set_halt(struct usb_ep *_ep, int value)
657{
658 return dummy_set_halt_and_wedge(_ep, value, 0);
659}
660
661static int dummy_set_wedge(struct usb_ep *_ep)
662{
663 if (!_ep || _ep->name == ep0name)
664 return -EINVAL;
665 return dummy_set_halt_and_wedge(_ep, 1, 1);
666}
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668static const struct usb_ep_ops dummy_ep_ops = {
669 .enable = dummy_enable,
670 .disable = dummy_disable,
671
672 .alloc_request = dummy_alloc_request,
673 .free_request = dummy_free_request,
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 .queue = dummy_queue,
676 .dequeue = dummy_dequeue,
677
678 .set_halt = dummy_set_halt,
Alan Stern851a5262008-08-14 15:48:30 -0400679 .set_wedge = dummy_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680};
681
682/*-------------------------------------------------------------------------*/
683
684/* there are both host and device side versions of this call ... */
685static int dummy_g_get_frame (struct usb_gadget *_gadget)
686{
687 struct timeval tv;
688
689 do_gettimeofday (&tv);
690 return tv.tv_usec / 1000;
691}
692
693static int dummy_wakeup (struct usb_gadget *_gadget)
694{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300695 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300697 dum_hcd = gadget_to_dummy_hcd(_gadget);
698 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
Alan Stern5742b0c2005-05-02 11:25:17 -0400699 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return -EINVAL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300701 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
Alan Stern391eca92005-05-10 15:34:16 -0400702 return -ENOLINK;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300703 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
704 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -0400705 return -EIO;
706
707 /* FIXME: What if the root hub is suspended but the port isn't? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /* hub notices our request, issues downstream resume, etc */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300710 dum_hcd->resuming = 1;
711 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
712 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return 0;
714}
715
716static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
717{
718 struct dummy *dum;
719
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300720 dum = (gadget_to_dummy_hcd(_gadget))->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (value)
722 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
723 else
724 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
725 return 0;
726}
727
Alan Sternf1c39fa2005-05-03 16:24:04 -0400728static int dummy_pullup (struct usb_gadget *_gadget, int value)
729{
730 struct dummy *dum;
731 unsigned long flags;
732
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300733 dum = gadget_to_dummy_hcd(_gadget)->dum;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400734 spin_lock_irqsave (&dum->lock, flags);
735 dum->pullup = (value != 0);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300736 set_link_state(dum->hs_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400737 spin_unlock_irqrestore (&dum->lock, flags);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300738 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum->hs_hcd));
Alan Sternf1c39fa2005-05-03 16:24:04 -0400739 return 0;
740}
741
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300742static int dummy_udc_start(struct usb_gadget_driver *driver,
743 int (*bind)(struct usb_gadget *));
744static int dummy_udc_stop(struct usb_gadget_driver *driver);
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746static const struct usb_gadget_ops dummy_ops = {
747 .get_frame = dummy_g_get_frame,
748 .wakeup = dummy_wakeup,
749 .set_selfpowered = dummy_set_selfpowered,
Alan Sternf1c39fa2005-05-03 16:24:04 -0400750 .pullup = dummy_pullup,
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300751 .start = dummy_udc_start,
752 .stop = dummy_udc_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753};
754
755/*-------------------------------------------------------------------------*/
756
757/* "function" sysfs attribute */
758static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400759show_function (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 struct dummy *dum = gadget_dev_to_dummy (dev);
762
763 if (!dum->driver || !dum->driver->function)
764 return 0;
765 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
766}
Alan Sterncc095b02005-05-10 15:28:38 -0400767static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769/*-------------------------------------------------------------------------*/
770
771/*
772 * Driver registration/unregistration.
773 *
774 * This is basically hardware-specific; there's usually only one real USB
775 * device (not host) controller since that's how USB devices are intended
776 * to work. So most implementations of these api calls will rely on the
777 * fact that only one driver will ever bind to the hardware. But curious
778 * hardware can be built with discrete components, so the gadget API doesn't
779 * require that assumption.
780 *
781 * For this emulator, it might be convenient to create a usb slave device
782 * for each driver that registers: just add to a big root hub.
783 */
784
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300785static int dummy_udc_start(struct usb_gadget_driver *driver,
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +0200786 int (*bind)(struct usb_gadget *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300788 struct dummy *dum = &the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 int retval, i;
790
791 if (!dum)
792 return -EINVAL;
793 if (dum->driver)
794 return -EBUSY;
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +0200795 if (!bind || !driver->setup || driver->speed == USB_SPEED_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return -EINVAL;
797
798 /*
799 * SLAVE side init ... the layer above hardware, which
800 * can't enumerate without help from the driver we're binding.
801 */
Alan Stern5742b0c2005-05-02 11:25:17 -0400802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 dum->devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 INIT_LIST_HEAD (&dum->gadget.ep_list);
806 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
807 struct dummy_ep *ep = &dum->ep [i];
808
809 if (!ep_name [i])
810 break;
811 ep->ep.name = ep_name [i];
812 ep->ep.ops = &dummy_ep_ops;
813 list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
Alan Stern851a5262008-08-14 15:48:30 -0400814 ep->halted = ep->wedged = ep->already_seen =
815 ep->setup_stage = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 ep->ep.maxpacket = ~0;
817 ep->last_io = jiffies;
818 ep->gadget = &dum->gadget;
819 ep->desc = NULL;
820 INIT_LIST_HEAD (&ep->queue);
821 }
822
823 dum->gadget.ep0 = &dum->ep [0].ep;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300824 dum->gadget.speed = min((u8)driver->speed, (u8)USB_SPEED_HIGH) ;
825 dum->ep[0].ep.maxpacket = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 list_del_init (&dum->ep [0].ep.ep_list);
827 INIT_LIST_HEAD(&dum->fifo_req.queue);
828
Alan Stern59331012007-11-20 16:28:55 -0500829 driver->driver.bus = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 dum->driver = driver;
831 dum->gadget.dev.driver = &driver->driver;
Alan Sternd9b76252005-05-03 16:15:43 -0400832 dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 driver->driver.name);
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +0200834 retval = bind(&dum->gadget);
Alan Stern59331012007-11-20 16:28:55 -0500835 if (retval) {
836 dum->driver = NULL;
837 dum->gadget.dev.driver = NULL;
838 return retval;
839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /* khubd will enumerate this in a while */
Alan Sternf1c39fa2005-05-03 16:24:04 -0400842 spin_lock_irq (&dum->lock);
843 dum->pullup = 1;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300844 dum->gadget.is_otg =
845 (dummy_hcd_to_hcd(dum->hs_hcd)->self.otg_port != 0);
846 set_link_state(dum->hs_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400847 spin_unlock_irq (&dum->lock);
Alan Stern685eb932005-05-03 16:27:26 -0400848
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300849 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum->hs_hcd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return 0;
851}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300853static int dummy_udc_stop(struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300855 struct dummy *dum = &the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 unsigned long flags;
857
858 if (!dum)
859 return -ENODEV;
David Brownell6bea4762006-12-05 03:15:33 -0800860 if (!driver || driver != dum->driver || !driver->unbind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return -EINVAL;
862
Alan Sternd9b76252005-05-03 16:15:43 -0400863 dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 driver->driver.name);
865
866 spin_lock_irqsave (&dum->lock, flags);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400867 dum->pullup = 0;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300868 set_link_state(dum->hs_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 spin_unlock_irqrestore (&dum->lock, flags);
870
871 driver->unbind (&dum->gadget);
Alan Stern59331012007-11-20 16:28:55 -0500872 dum->gadget.dev.driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 dum->driver = NULL;
874
Alan Sternf1c39fa2005-05-03 16:24:04 -0400875 spin_lock_irqsave (&dum->lock, flags);
876 dum->pullup = 0;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300877 set_link_state(dum->hs_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400878 spin_unlock_irqrestore (&dum->lock, flags);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300879 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum->hs_hcd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return 0;
881}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883#undef is_enabled
884
Alan Sterncc095b02005-05-10 15:28:38 -0400885/* just declare this in any driver that really need it */
886extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
889{
890 return -ENOSYS;
891}
892EXPORT_SYMBOL (net2280_set_fifo_mode);
893
Alan Sternd9b76252005-05-03 16:15:43 -0400894
895/* The gadget structure is stored inside the hcd structure and will be
896 * released along with it. */
897static void
898dummy_gadget_release (struct device *dev)
899{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300900 return;
Alan Sternd9b76252005-05-03 16:15:43 -0400901}
902
Alan Stern8364d6b2005-11-14 12:16:30 -0500903static int dummy_udc_probe (struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -0400904{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300905 struct dummy *dum = &the_controller;
Alan Sternd9b76252005-05-03 16:15:43 -0400906 int rc;
907
908 dum->gadget.name = gadget_name;
909 dum->gadget.ops = &dummy_ops;
910 dum->gadget.is_dualspeed = 1;
911
Kay Sievers0031a062008-05-02 06:02:41 +0200912 dev_set_name(&dum->gadget.dev, "gadget");
Alan Stern8364d6b2005-11-14 12:16:30 -0500913 dum->gadget.dev.parent = &pdev->dev;
Alan Sternd9b76252005-05-03 16:15:43 -0400914 dum->gadget.dev.release = dummy_gadget_release;
915 rc = device_register (&dum->gadget.dev);
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530916 if (rc < 0) {
917 put_device(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -0400918 return rc;
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530919 }
Alan Sternd9b76252005-05-03 16:15:43 -0400920
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300921 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
922 if (rc < 0)
923 goto err_udc;
924
Alan Sternefd54a32006-09-25 11:55:56 -0400925 rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
926 if (rc < 0)
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300927 goto err_dev;
928 platform_set_drvdata(pdev, dum);
929 return rc;
930
931err_dev:
932 usb_del_gadget_udc(&dum->gadget);
933err_udc:
934 device_unregister(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -0400935 return rc;
936}
937
Alan Stern8364d6b2005-11-14 12:16:30 -0500938static int dummy_udc_remove (struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -0400939{
Alan Stern8364d6b2005-11-14 12:16:30 -0500940 struct dummy *dum = platform_get_drvdata (pdev);
Alan Sternd9b76252005-05-03 16:15:43 -0400941
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300942 usb_del_gadget_udc(&dum->gadget);
Alan Stern8364d6b2005-11-14 12:16:30 -0500943 platform_set_drvdata (pdev, NULL);
Alan Sternd9b76252005-05-03 16:15:43 -0400944 device_remove_file (&dum->gadget.dev, &dev_attr_function);
945 device_unregister (&dum->gadget.dev);
946 return 0;
947}
948
Alan Stern8364d6b2005-11-14 12:16:30 -0500949static int dummy_udc_suspend (struct platform_device *pdev, pm_message_t state)
Alan Stern391eca92005-05-10 15:34:16 -0400950{
Alan Stern8364d6b2005-11-14 12:16:30 -0500951 struct dummy *dum = platform_get_drvdata(pdev);
Alan Stern391eca92005-05-10 15:34:16 -0400952
Harvey Harrison441b62c2008-03-03 16:08:34 -0800953 dev_dbg (&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -0400954 spin_lock_irq (&dum->lock);
955 dum->udc_suspended = 1;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300956 set_link_state(dum->hs_hcd);
Alan Stern391eca92005-05-10 15:34:16 -0400957 spin_unlock_irq (&dum->lock);
958
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300959 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum->hs_hcd));
Alan Stern391eca92005-05-10 15:34:16 -0400960 return 0;
961}
962
Alan Stern8364d6b2005-11-14 12:16:30 -0500963static int dummy_udc_resume (struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -0400964{
Alan Stern8364d6b2005-11-14 12:16:30 -0500965 struct dummy *dum = platform_get_drvdata(pdev);
Alan Stern391eca92005-05-10 15:34:16 -0400966
Harvey Harrison441b62c2008-03-03 16:08:34 -0800967 dev_dbg (&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -0400968 spin_lock_irq (&dum->lock);
969 dum->udc_suspended = 0;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300970 set_link_state(dum->hs_hcd);
Alan Stern391eca92005-05-10 15:34:16 -0400971 spin_unlock_irq (&dum->lock);
972
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300973 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum->hs_hcd));
Alan Stern391eca92005-05-10 15:34:16 -0400974 return 0;
975}
976
Russell King3ae5eae2005-11-09 22:32:44 +0000977static struct platform_driver dummy_udc_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -0400978 .probe = dummy_udc_probe,
979 .remove = dummy_udc_remove,
Alan Stern391eca92005-05-10 15:34:16 -0400980 .suspend = dummy_udc_suspend,
981 .resume = dummy_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +0000982 .driver = {
983 .name = (char *) gadget_name,
984 .owner = THIS_MODULE,
985 },
Alan Sternd9b76252005-05-03 16:15:43 -0400986};
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988/*-------------------------------------------------------------------------*/
989
990/* MASTER/HOST SIDE DRIVER
991 *
992 * this uses the hcd framework to hook up to host side drivers.
993 * its root hub will only have one device, otherwise it acts like
994 * a normal host controller.
995 *
996 * when urbs are queued, they're just stuck on a list that we
997 * scan in a timer callback. that callback connects writes from
998 * the host with reads from the device, and so on, based on the
999 * usb 2.0 rules.
1000 */
1001
1002static int dummy_urb_enqueue (
1003 struct usb_hcd *hcd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001005 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001007 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 struct urbp *urbp;
1009 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001010 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1013 return -EINVAL;
1014
1015 urbp = kmalloc (sizeof *urbp, mem_flags);
1016 if (!urbp)
1017 return -ENOMEM;
1018 urbp->urb = urb;
1019
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001020 dum_hcd = hcd_to_dummy_hcd(hcd);
1021 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001022 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1023 if (rc) {
1024 kfree(urbp);
1025 goto done;
1026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001028 if (!dum_hcd->udev) {
1029 dum_hcd->udev = urb->dev;
1030 usb_get_dev(dum_hcd->udev);
1031 } else if (unlikely(dum_hcd->udev != urb->dev))
1032 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001034 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 urb->hcpriv = urbp;
1036 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
1037 urb->error_count = 1; /* mark as a new urb */
1038
1039 /* kick the scheduler, it'll do the rest */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001040 if (!timer_pending(&dum_hcd->timer))
1041 mod_timer(&dum_hcd->timer, jiffies + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Alan Sterne9df41c2007-08-08 11:48:02 -04001043 done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001044 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001045 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
Alan Sterne9df41c2007-08-08 11:48:02 -04001048static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001050 struct dummy_hcd *dum_hcd;
Alan Stern391eca92005-05-10 15:34:16 -04001051 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001052 int rc;
Alan Stern391eca92005-05-10 15:34:16 -04001053
1054 /* giveback happens automatically in timer callback,
1055 * so make sure the callback happens */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001056 dum_hcd = hcd_to_dummy_hcd(hcd);
1057 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001058
1059 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001060 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1061 !list_empty(&dum_hcd->urbp_list))
1062 mod_timer(&dum_hcd->timer, jiffies);
Alan Sterne9df41c2007-08-08 11:48:02 -04001063
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001064 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001065 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068/* transfer up to a frame's worth; caller must own lock */
1069static int
Alan Stern4d2f1102007-08-24 15:40:10 -04001070transfer(struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit,
1071 int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
1073 struct dummy_request *req;
1074
1075top:
1076 /* if there's no request queued, the device is NAKing; return */
1077 list_for_each_entry (req, &ep->queue, queue) {
1078 unsigned host_len, dev_len, len;
1079 int is_short, to_host;
1080 int rescan = 0;
1081
1082 /* 1..N packets of ep->ep.maxpacket each ... the last one
1083 * may be short (including zero length).
1084 *
1085 * writer can send a zlp explicitly (length 0) or implicitly
1086 * (length mod maxpacket zero, and 'zero' flag); they always
1087 * terminate reads.
1088 */
1089 host_len = urb->transfer_buffer_length - urb->actual_length;
1090 dev_len = req->req.length - req->req.actual;
1091 len = min (host_len, dev_len);
1092
1093 /* FIXME update emulated data toggle too */
1094
1095 to_host = usb_pipein (urb->pipe);
1096 if (unlikely (len == 0))
1097 is_short = 1;
1098 else {
1099 char *ubuf, *rbuf;
1100
1101 /* not enough bandwidth left? */
1102 if (limit < ep->ep.maxpacket && limit < len)
1103 break;
1104 len = min (len, (unsigned) limit);
1105 if (len == 0)
1106 break;
1107
1108 /* use an extra pass for the final short packet */
1109 if (len > ep->ep.maxpacket) {
1110 rescan = 1;
1111 len -= (len % ep->ep.maxpacket);
1112 }
1113 is_short = (len % ep->ep.maxpacket) != 0;
1114
1115 /* else transfer packet(s) */
1116 ubuf = urb->transfer_buffer + urb->actual_length;
1117 rbuf = req->req.buf + req->req.actual;
1118 if (to_host)
1119 memcpy (ubuf, rbuf, len);
1120 else
1121 memcpy (rbuf, ubuf, len);
1122 ep->last_io = jiffies;
1123
1124 limit -= len;
1125 urb->actual_length += len;
1126 req->req.actual += len;
1127 }
1128
1129 /* short packets terminate, maybe with overflow/underflow.
1130 * it's only really an error to write too much.
1131 *
1132 * partially filling a buffer optionally blocks queue advances
1133 * (so completion handlers can clean up the queue) but we don't
Alan Sternb0d9efb2007-08-21 15:39:21 -04001134 * need to emulate such data-in-flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 */
1136 if (is_short) {
1137 if (host_len == dev_len) {
1138 req->req.status = 0;
Alan Stern4d2f1102007-08-24 15:40:10 -04001139 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 } else if (to_host) {
1141 req->req.status = 0;
1142 if (dev_len > host_len)
Alan Stern4d2f1102007-08-24 15:40:10 -04001143 *status = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 else
Alan Stern4d2f1102007-08-24 15:40:10 -04001145 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 } else if (!to_host) {
Alan Stern4d2f1102007-08-24 15:40:10 -04001147 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (host_len > dev_len)
1149 req->req.status = -EOVERFLOW;
1150 else
1151 req->req.status = 0;
1152 }
1153
1154 /* many requests terminate without a short packet */
1155 } else {
1156 if (req->req.length == req->req.actual
1157 && !req->req.zero)
1158 req->req.status = 0;
1159 if (urb->transfer_buffer_length == urb->actual_length
1160 && !(urb->transfer_flags
Alan Stern4d2f1102007-08-24 15:40:10 -04001161 & URB_ZERO_PACKET))
1162 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
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 */
Alan Stern4d2f1102007-08-24 15:40:10 -04001178 if (*status != -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 break;
1180
1181 /* rescan to continue with any other queued i/o */
1182 if (rescan)
1183 goto top;
1184 }
1185 return limit;
1186}
1187
1188static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1189{
1190 int limit = ep->ep.maxpacket;
1191
1192 if (dum->gadget.speed == USB_SPEED_HIGH) {
1193 int tmp;
1194
1195 /* high bandwidth mode */
1196 tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 tmp = (tmp >> 11) & 0x03;
1198 tmp *= 8 /* applies to entire frame */;
1199 limit += limit * tmp;
1200 }
1201 return limit;
1202}
1203
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001204#define is_active(dum_hcd) ((dum_hcd->port_status & \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1206 USB_PORT_STAT_SUSPEND)) \
1207 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1208
1209static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1210{
1211 int i;
1212
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001213 if (!is_active(dum->hs_hcd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 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
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001237
1238/**
1239 * handle_control_request() - handles all control transfers
1240 * @dum: pointer to dummy (the_controller)
1241 * @urb: the urb request to handle
1242 * @setup: pointer to the setup data for a USB device control
1243 * request
1244 * @status: pointer to request handling status
1245 *
1246 * Return 0 - if the request was handled
1247 * 1 - if the request wasn't handles
1248 * error code on error
1249 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001250static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001251 struct usb_ctrlrequest *setup,
1252 int *status)
1253{
1254 struct dummy_ep *ep2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001255 struct dummy *dum = dum_hcd->dum;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001256 int ret_val = 1;
1257 unsigned w_index;
1258 unsigned w_value;
1259
1260 w_index = le16_to_cpu(setup->wIndex);
1261 w_value = le16_to_cpu(setup->wValue);
1262 switch (setup->bRequest) {
1263 case USB_REQ_SET_ADDRESS:
1264 if (setup->bRequestType != Dev_Request)
1265 break;
1266 dum->address = w_value;
1267 *status = 0;
1268 dev_dbg(udc_dev(dum), "set_address = %d\n",
1269 w_value);
1270 ret_val = 0;
1271 break;
1272 case USB_REQ_SET_FEATURE:
1273 if (setup->bRequestType == Dev_Request) {
1274 ret_val = 0;
1275 switch (w_value) {
1276 case USB_DEVICE_REMOTE_WAKEUP:
1277 break;
1278 case USB_DEVICE_B_HNP_ENABLE:
1279 dum->gadget.b_hnp_enable = 1;
1280 break;
1281 case USB_DEVICE_A_HNP_SUPPORT:
1282 dum->gadget.a_hnp_support = 1;
1283 break;
1284 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1285 dum->gadget.a_alt_hnp_support = 1;
1286 break;
1287 default:
1288 ret_val = -EOPNOTSUPP;
1289 }
1290 if (ret_val == 0) {
1291 dum->devstatus |= (1 << w_value);
1292 *status = 0;
1293 }
1294 } else if (setup->bRequestType == Ep_Request) {
1295 /* endpoint halt */
1296 ep2 = find_endpoint(dum, w_index);
1297 if (!ep2 || ep2->ep.name == ep0name) {
1298 ret_val = -EOPNOTSUPP;
1299 break;
1300 }
1301 ep2->halted = 1;
1302 ret_val = 0;
1303 *status = 0;
1304 }
1305 break;
1306 case USB_REQ_CLEAR_FEATURE:
1307 if (setup->bRequestType == Dev_Request) {
1308 ret_val = 0;
1309 switch (w_value) {
1310 case USB_DEVICE_REMOTE_WAKEUP:
1311 w_value = USB_DEVICE_REMOTE_WAKEUP;
1312 break;
1313 default:
1314 ret_val = -EOPNOTSUPP;
1315 break;
1316 }
1317 if (ret_val == 0) {
1318 dum->devstatus &= ~(1 << w_value);
1319 *status = 0;
1320 }
1321 } else if (setup->bRequestType == Ep_Request) {
1322 /* endpoint halt */
1323 ep2 = find_endpoint(dum, w_index);
1324 if (!ep2) {
1325 ret_val = -EOPNOTSUPP;
1326 break;
1327 }
1328 if (!ep2->wedged)
1329 ep2->halted = 0;
1330 ret_val = 0;
1331 *status = 0;
1332 }
1333 break;
1334 case USB_REQ_GET_STATUS:
1335 if (setup->bRequestType == Dev_InRequest
1336 || setup->bRequestType == Intf_InRequest
1337 || setup->bRequestType == Ep_InRequest) {
1338 char *buf;
1339 /*
1340 * device: remote wakeup, selfpowered
1341 * interface: nothing
1342 * endpoint: halt
1343 */
1344 buf = (char *)urb->transfer_buffer;
1345 if (urb->transfer_buffer_length > 0) {
1346 if (setup->bRequestType == Ep_InRequest) {
1347 ep2 = find_endpoint(dum, w_index);
1348 if (!ep2) {
1349 ret_val = -EOPNOTSUPP;
1350 break;
1351 }
1352 buf[0] = ep2->halted;
1353 } else if (setup->bRequestType ==
1354 Dev_InRequest) {
1355 buf[0] = (u8)dum->devstatus;
1356 } else
1357 buf[0] = 0;
1358 }
1359 if (urb->transfer_buffer_length > 1)
1360 buf[1] = 0;
1361 urb->actual_length = min_t(u32, 2,
1362 urb->transfer_buffer_length);
1363 ret_val = 0;
1364 *status = 0;
1365 }
1366 break;
1367 }
1368 return ret_val;
1369}
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371/* drive both sides of the transfers; looks like irq handlers to
1372 * both drivers except the callbacks aren't in_irq().
1373 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001374static void dummy_timer(unsigned long _dum_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001376 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1377 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 struct urbp *urbp, *tmp;
1379 unsigned long flags;
1380 int limit, total;
1381 int i;
1382
1383 /* simplistic model for one frame's bandwidth */
1384 switch (dum->gadget.speed) {
1385 case USB_SPEED_LOW:
1386 total = 8/*bytes*/ * 12/*packets*/;
1387 break;
1388 case USB_SPEED_FULL:
1389 total = 64/*bytes*/ * 19/*packets*/;
1390 break;
1391 case USB_SPEED_HIGH:
1392 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1393 break;
1394 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001395 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return;
1397 }
1398
1399 /* FIXME if HZ != 1000 this will probably misbehave ... */
1400
1401 /* look at each urb queued by the host side driver */
1402 spin_lock_irqsave (&dum->lock, flags);
1403
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001404 if (!dum_hcd->udev) {
1405 dev_err(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 "timer fired with no URBs pending?\n");
1407 spin_unlock_irqrestore (&dum->lock, flags);
1408 return;
1409 }
1410
1411 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1412 if (!ep_name [i])
1413 break;
1414 dum->ep [i].already_seen = 0;
1415 }
1416
1417restart:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001418 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 struct urb *urb;
1420 struct dummy_request *req;
1421 u8 address;
1422 struct dummy_ep *ep = NULL;
1423 int type;
Alan Stern4d2f1102007-08-24 15:40:10 -04001424 int status = -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 urb = urbp->urb;
Alan Sterneb231052007-08-21 15:40:36 -04001427 if (urb->unlinked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 goto return_urb;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001429 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
Alan Stern391eca92005-05-10 15:34:16 -04001430 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 type = usb_pipetype (urb->pipe);
1432
1433 /* used up this frame's non-periodic bandwidth?
1434 * FIXME there's infinite bandwidth for control and
1435 * periodic transfers ... unrealistic.
1436 */
1437 if (total <= 0 && type == PIPE_BULK)
1438 continue;
1439
1440 /* find the gadget's ep for this request (if configured) */
1441 address = usb_pipeendpoint (urb->pipe);
1442 if (usb_pipein (urb->pipe))
1443 address |= USB_DIR_IN;
1444 ep = find_endpoint(dum, address);
1445 if (!ep) {
1446 /* set_configuration() disagreement */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001447 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 "no ep configured for urb %p\n",
1449 urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001450 status = -EPROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 goto return_urb;
1452 }
1453
1454 if (ep->already_seen)
1455 continue;
1456 ep->already_seen = 1;
1457 if (ep == &dum->ep [0] && urb->error_count) {
1458 ep->setup_stage = 1; /* a new urb */
1459 urb->error_count = 0;
1460 }
1461 if (ep->halted && !ep->setup_stage) {
1462 /* NOTE: must not be iso! */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001463 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 ep->ep.name, urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001465 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 goto return_urb;
1467 }
1468 /* FIXME make sure both ends agree on maxpacket */
1469
1470 /* handle control requests */
1471 if (ep == &dum->ep [0] && ep->setup_stage) {
1472 struct usb_ctrlrequest setup;
1473 int value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 /* paranoia, in case of stale queued data */
1477 list_for_each_entry (req, &ep->queue, queue) {
1478 list_del_init (&req->queue);
1479 req->req.status = -EOVERFLOW;
Alan Sternd9b76252005-05-03 16:15:43 -04001480 dev_dbg (udc_dev(dum), "stale req = %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 req);
1482
1483 spin_unlock (&dum->lock);
1484 req->req.complete (&ep->ep, &req->req);
1485 spin_lock (&dum->lock);
1486 ep->already_seen = 0;
1487 goto restart;
1488 }
1489
1490 /* gadget driver never sees set_address or operations
1491 * on standard feature flags. some hardware doesn't
1492 * even expose them.
1493 */
1494 ep->last_io = jiffies;
1495 ep->setup_stage = 0;
1496 ep->halted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001498 value = handle_control_request(dum_hcd, urb, &setup,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001499 &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 /* gadget driver handles all other requests. block
1502 * until setup() returns; no reentrancy issues etc.
1503 */
1504 if (value > 0) {
1505 spin_unlock (&dum->lock);
1506 value = dum->driver->setup (&dum->gadget,
1507 &setup);
1508 spin_lock (&dum->lock);
1509
1510 if (value >= 0) {
1511 /* no delays (max 64KB data stage) */
1512 limit = 64*1024;
1513 goto treat_control_like_bulk;
1514 }
1515 /* error, see below */
1516 }
1517
1518 if (value < 0) {
1519 if (value != -EOPNOTSUPP)
Alan Sternd9b76252005-05-03 16:15:43 -04001520 dev_dbg (udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 "setup --> %d\n",
1522 value);
Alan Stern4d2f1102007-08-24 15:40:10 -04001523 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 urb->actual_length = 0;
1525 }
1526
1527 goto return_urb;
1528 }
1529
1530 /* non-control requests */
1531 limit = total;
1532 switch (usb_pipetype (urb->pipe)) {
1533 case PIPE_ISOCHRONOUS:
1534 /* FIXME is it urb->interval since the last xfer?
1535 * use urb->iso_frame_desc[i].
1536 * complete whether or not ep has requests queued.
1537 * report random errors, to debug drivers.
1538 */
1539 limit = max (limit, periodic_bytes (dum, ep));
Alan Stern4d2f1102007-08-24 15:40:10 -04001540 status = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 break;
1542
1543 case PIPE_INTERRUPT:
1544 /* FIXME is it urb->interval since the last xfer?
1545 * this almost certainly polls too fast.
1546 */
1547 limit = max (limit, periodic_bytes (dum, ep));
1548 /* FALLTHROUGH */
1549
1550 // case PIPE_BULK: case PIPE_CONTROL:
1551 default:
1552 treat_control_like_bulk:
1553 ep->last_io = jiffies;
Alan Stern4d2f1102007-08-24 15:40:10 -04001554 total = transfer(dum, urb, ep, limit, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 break;
1556 }
1557
1558 /* incomplete transfer? */
Alan Stern4d2f1102007-08-24 15:40:10 -04001559 if (status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 continue;
1561
1562return_urb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 list_del (&urbp->urbp_list);
1564 kfree (urbp);
1565 if (ep)
1566 ep->already_seen = ep->setup_stage = 0;
1567
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001568 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 spin_unlock (&dum->lock);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001570 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 spin_lock (&dum->lock);
1572
1573 goto restart;
1574 }
1575
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001576 if (list_empty(&dum_hcd->urbp_list)) {
1577 usb_put_dev(dum_hcd->udev);
1578 dum_hcd->udev = NULL;
1579 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern391eca92005-05-10 15:34:16 -04001580 /* want a 1 msec delay here */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001581 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
1583
1584 spin_unlock_irqrestore (&dum->lock, flags);
1585}
1586
1587/*-------------------------------------------------------------------------*/
1588
1589#define PORT_C_MASK \
Alan Sternc2db8b52005-04-29 16:30:48 -04001590 ((USB_PORT_STAT_C_CONNECTION \
1591 | USB_PORT_STAT_C_ENABLE \
1592 | USB_PORT_STAT_C_SUSPEND \
1593 | USB_PORT_STAT_C_OVERCURRENT \
1594 | USB_PORT_STAT_C_RESET) << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1597{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001598 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 unsigned long flags;
Alan Stern391eca92005-05-10 15:34:16 -04001600 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001602 dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001604 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001605 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001606 goto done;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001607
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001608 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1609 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1610 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1611 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001612 }
1613
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001614 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 *buf = (1 << 1);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001616 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1617 dum_hcd->port_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 retval = 1;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001619 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -04001620 usb_hcd_resume_root_hub (hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
Alan Stern391eca92005-05-10 15:34:16 -04001622done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001623 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return retval;
1625}
1626
1627static inline void
1628hub_descriptor (struct usb_hub_descriptor *desc)
1629{
1630 memset (desc, 0, sizeof *desc);
1631 desc->bDescriptorType = 0x29;
1632 desc->bDescLength = 9;
Al Virofd05e722008-04-28 07:00:16 +01001633 desc->wHubCharacteristics = cpu_to_le16(0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 desc->bNbrPorts = 1;
John Youndbe79bb2001-09-17 00:00:00 -07001635 desc->u.hs.DeviceRemovable[0] = 0xff;
1636 desc->u.hs.DeviceRemovable[1] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
1639static int dummy_hub_control (
1640 struct usb_hcd *hcd,
1641 u16 typeReq,
1642 u16 wValue,
1643 u16 wIndex,
1644 char *buf,
1645 u16 wLength
1646) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001647 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 int retval = 0;
1649 unsigned long flags;
1650
Alan Stern541c7d42010-06-22 16:39:10 -04001651 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001652 return -ETIMEDOUT;
1653
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001654 dum_hcd = hcd_to_dummy_hcd(hcd);
1655
1656 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 switch (typeReq) {
1658 case ClearHubFeature:
1659 break;
1660 case ClearPortFeature:
1661 switch (wValue) {
1662 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001663 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 /* 20msec resume signaling */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001665 dum_hcd->resuming = 1;
1666 dum_hcd->re_timeout = jiffies +
Alan Sternf1c39fa2005-05-03 16:24:04 -04001667 msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
1669 break;
1670 case USB_PORT_FEAT_POWER:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001671 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1672 dev_dbg(dummy_dev(dum_hcd), "power-off\n");
Alan Sternf1c39fa2005-05-03 16:24:04 -04001673 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001675 dum_hcd->port_status &= ~(1 << wValue);
1676 set_link_state(dum_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
1678 break;
1679 case GetHubDescriptor:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001680 hub_descriptor((struct usb_hub_descriptor *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 break;
1682 case GetHubStatus:
Harvey Harrison551509d2009-02-11 14:11:36 -08001683 *(__le32 *) buf = cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 break;
1685 case GetPortStatus:
1686 if (wIndex != 1)
1687 retval = -EPIPE;
1688
1689 /* whoever resets or resumes must GetPortStatus to
1690 * complete it!!
1691 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001692 if (dum_hcd->resuming &&
1693 time_after_eq(jiffies, dum_hcd->re_timeout)) {
1694 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1695 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001697 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
1698 time_after_eq(jiffies, dum_hcd->re_timeout)) {
1699 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
1700 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
1701 if (dum_hcd->dum->pullup) {
1702 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
1703 switch (dum_hcd->dum->gadget.speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 case USB_SPEED_HIGH:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001705 dum_hcd->port_status |=
1706 USB_PORT_STAT_HIGH_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 break;
1708 case USB_SPEED_LOW:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001709 dum_hcd->dum->gadget.ep0->
1710 maxpacket = 8;
1711 dum_hcd->port_status |=
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 USB_PORT_STAT_LOW_SPEED;
1713 break;
1714 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001715 dum_hcd->dum->gadget.speed =
1716 USB_SPEED_FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 break;
1718 }
1719 }
1720 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001721 set_link_state(dum_hcd);
1722 ((__le16 *) buf)[0] = cpu_to_le16 (dum_hcd->port_status);
1723 ((__le16 *) buf)[1] = cpu_to_le16 (dum_hcd->port_status >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 break;
1725 case SetHubFeature:
1726 retval = -EPIPE;
1727 break;
1728 case SetPortFeature:
1729 switch (wValue) {
1730 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001731 if (dum_hcd->active) {
1732 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001733
1734 /* HNP would happen here; for now we
1735 * assume b_bus_req is always true.
1736 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001737 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001738 if (((1 << USB_DEVICE_B_HNP_ENABLE)
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001739 & dum_hcd->dum->devstatus) != 0)
1740 dev_dbg(dummy_dev(dum_hcd),
Alan Stern5742b0c2005-05-02 11:25:17 -04001741 "no HNP yet!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
1743 break;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001744 case USB_PORT_FEAT_POWER:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001745 dum_hcd->port_status |= USB_PORT_STAT_POWER;
1746 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001747 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 case USB_PORT_FEAT_RESET:
Alan Sternf1c39fa2005-05-03 16:24:04 -04001749 /* if it's already enabled, disable */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001750 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
Alan Sternf1c39fa2005-05-03 16:24:04 -04001751 | USB_PORT_STAT_LOW_SPEED
1752 | USB_PORT_STAT_HIGH_SPEED);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001753 /*
1754 * We want to reset device status. All but the
1755 * Self powered feature
1756 */
1757 dum_hcd->dum->devstatus &=
1758 (1 << USB_DEVICE_SELF_POWERED);
1759 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001760 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001762 if ((dum_hcd->port_status &
1763 USB_PORT_STAT_POWER) != 0) {
1764 dum_hcd->port_status |= (1 << wValue);
1765 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 }
1768 break;
1769
1770 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001771 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 "hub control req%04x v%04x i%04x l%d\n",
1773 typeReq, wValue, wIndex, wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 /* "protocol stall" on error */
1775 retval = -EPIPE;
1776 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001777 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Stern685eb932005-05-03 16:27:26 -04001778
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001779 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
Alan Stern685eb932005-05-03 16:27:26 -04001780 usb_hcd_poll_rh_status (hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 return retval;
1782}
1783
Alan Stern0c0382e2005-10-13 17:08:02 -04001784static int dummy_bus_suspend (struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04001785{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001786 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04001787
Harvey Harrison441b62c2008-03-03 16:08:34 -08001788 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern3cf0a222005-11-29 12:08:15 -05001789
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001790 spin_lock_irq(&dum_hcd->dum->lock);
1791 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
1792 set_link_state(dum_hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05001793 hcd->state = HC_STATE_SUSPENDED;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001794 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern391eca92005-05-10 15:34:16 -04001795 return 0;
1796}
1797
Alan Stern0c0382e2005-10-13 17:08:02 -04001798static int dummy_bus_resume (struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04001799{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001800 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05001801 int rc = 0;
1802
Harvey Harrison441b62c2008-03-03 16:08:34 -08001803 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04001804
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001805 spin_lock_irq(&dum_hcd->dum->lock);
Alan Stern541c7d42010-06-22 16:39:10 -04001806 if (!HCD_HW_ACCESSIBLE(hcd)) {
Alan Sterncfa59da2007-06-21 16:25:35 -04001807 rc = -ESHUTDOWN;
Alan Stern3cf0a222005-11-29 12:08:15 -05001808 } else {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001809 dum_hcd->rh_state = DUMMY_RH_RUNNING;
1810 set_link_state(dum_hcd);
1811 if (!list_empty(&dum_hcd->urbp_list))
1812 mod_timer(&dum_hcd->timer, jiffies);
Alan Stern3cf0a222005-11-29 12:08:15 -05001813 hcd->state = HC_STATE_RUNNING;
1814 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001815 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern3cf0a222005-11-29 12:08:15 -05001816 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04001817}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819/*-------------------------------------------------------------------------*/
1820
1821static inline ssize_t
1822show_urb (char *buf, size_t size, struct urb *urb)
1823{
1824 int ep = usb_pipeendpoint (urb->pipe);
1825
1826 return snprintf (buf, size,
1827 "urb/%p %s ep%d%s%s len %d/%d\n",
1828 urb,
1829 ({ char *s;
1830 switch (urb->dev->speed) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03001831 case USB_SPEED_LOW:
1832 s = "ls";
1833 break;
1834 case USB_SPEED_FULL:
1835 s = "fs";
1836 break;
1837 case USB_SPEED_HIGH:
1838 s = "hs";
1839 break;
1840 default:
1841 s = "?";
1842 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }; s; }),
1844 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
1845 ({ char *s; \
1846 switch (usb_pipetype (urb->pipe)) { \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03001847 case PIPE_CONTROL: \
1848 s = ""; \
1849 break; \
1850 case PIPE_BULK: \
1851 s = "-bulk"; \
1852 break; \
1853 case PIPE_INTERRUPT: \
1854 s = "-int"; \
1855 break; \
1856 default: \
1857 s = "-iso"; \
1858 break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }; s;}),
1860 urb->actual_length, urb->transfer_buffer_length);
1861}
1862
1863static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -04001864show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
1866 struct usb_hcd *hcd = dev_get_drvdata (dev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001867 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 struct urbp *urbp;
1869 size_t size = 0;
1870 unsigned long flags;
1871
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001872 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1873 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 size_t temp;
1875
1876 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
1877 buf += temp;
1878 size += temp;
1879 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001880 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 return size;
1883}
1884static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
1885
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001886static int dummy_start(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001888 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 /*
1891 * MASTER side init ... we emulate a root hub that'll only ever
1892 * talk to one device (the slave side). Also appears in sysfs,
1893 * just like more familiar pci-based HCDs.
1894 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001895 spin_lock_init(&dum_hcd->dum->lock);
1896 init_timer(&dum_hcd->timer);
1897 dum_hcd->timer.function = dummy_timer;
1898 dum_hcd->timer.data = (unsigned long)dum_hcd;
1899 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001901 INIT_LIST_HEAD(&dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Alan Sterncaf29f62007-12-06 11:10:39 -05001903 hcd->power_budget = POWER_BUDGET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 hcd->state = HC_STATE_RUNNING;
Alan Stern685eb932005-05-03 16:27:26 -04001905 hcd->uses_new_polling = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Alan Stern5742b0c2005-05-02 11:25:17 -04001907#ifdef CONFIG_USB_OTG
1908 hcd->self.otg_port = 1;
1909#endif
1910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001912 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
1914
1915static void dummy_stop (struct usb_hcd *hcd)
1916{
1917 struct dummy *dum;
1918
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001919 dum = (hcd_to_dummy_hcd(hcd))->dum;
1920 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
1921 usb_gadget_unregister_driver(dum->driver);
1922 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
1925/*-------------------------------------------------------------------------*/
1926
1927static int dummy_h_get_frame (struct usb_hcd *hcd)
1928{
1929 return dummy_g_get_frame (NULL);
1930}
1931
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001932static int dummy_setup(struct usb_hcd *hcd)
1933{
1934 if (usb_hcd_is_primary_hcd(hcd)) {
1935 the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
1936 the_controller.hs_hcd->dum = &the_controller;
1937 /* Mark the first roothub as being USB 2.0. */
1938 hcd->speed = HCD_USB2;
1939 hcd->self.root_hub->speed = USB_SPEED_HIGH;
1940 }
1941 return 0;
1942}
1943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944static const struct hc_driver dummy_hcd = {
1945 .description = (char *) driver_name,
1946 .product_desc = "Dummy host controller",
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001947 .hcd_priv_size = sizeof(struct dummy_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 .flags = HCD_USB2,
1950
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001951 .reset = dummy_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 .start = dummy_start,
1953 .stop = dummy_stop,
1954
1955 .urb_enqueue = dummy_urb_enqueue,
1956 .urb_dequeue = dummy_urb_dequeue,
1957
1958 .get_frame_number = dummy_h_get_frame,
1959
1960 .hub_status_data = dummy_hub_status,
1961 .hub_control = dummy_hub_control,
Alan Stern0c0382e2005-10-13 17:08:02 -04001962 .bus_suspend = dummy_bus_suspend,
1963 .bus_resume = dummy_bus_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964};
1965
Alan Stern8364d6b2005-11-14 12:16:30 -05001966static int dummy_hcd_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001968 struct usb_hcd *hs_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 int retval;
1970
Alan Stern8364d6b2005-11-14 12:16:30 -05001971 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001973 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
1974 if (!hs_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return -ENOMEM;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001976 hs_hcd->has_tt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001978 retval = usb_add_hcd(hs_hcd, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 if (retval != 0) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001980 usb_put_hcd(hs_hcd);
1981 the_controller.hs_hcd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
1983 return retval;
1984}
1985
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001986static int dummy_hcd_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001988 struct dummy *dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001990 dum = (hcd_to_dummy_hcd(platform_get_drvdata(pdev)))->dum;
1991 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
1992 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
1993 the_controller.hs_hcd = NULL;
1994
Alan Sternd9b76252005-05-03 16:15:43 -04001995 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
Alan Stern8364d6b2005-11-14 12:16:30 -05001998static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
Alan Stern391eca92005-05-10 15:34:16 -04001999{
2000 struct usb_hcd *hcd;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002001 struct dummy_hcd *dum_hcd;
Alan Stern3cf0a222005-11-29 12:08:15 -05002002 int rc = 0;
Alan Stern391eca92005-05-10 15:34:16 -04002003
Harvey Harrison441b62c2008-03-03 16:08:34 -08002004 dev_dbg (&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002005
Alan Stern3cf0a222005-11-29 12:08:15 -05002006 hcd = platform_get_drvdata (pdev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002007 dum_hcd = hcd_to_dummy_hcd(hcd);
2008 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern3cf0a222005-11-29 12:08:15 -05002009 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2010 rc = -EBUSY;
2011 } else
2012 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2013 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002014}
2015
Alan Stern8364d6b2005-11-14 12:16:30 -05002016static int dummy_hcd_resume (struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04002017{
2018 struct usb_hcd *hcd;
2019
Harvey Harrison441b62c2008-03-03 16:08:34 -08002020 dev_dbg (&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002021
Alan Stern3cf0a222005-11-29 12:08:15 -05002022 hcd = platform_get_drvdata (pdev);
2023 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Alan Stern391eca92005-05-10 15:34:16 -04002024 usb_hcd_poll_rh_status (hcd);
2025 return 0;
2026}
2027
Russell King3ae5eae2005-11-09 22:32:44 +00002028static struct platform_driver dummy_hcd_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04002029 .probe = dummy_hcd_probe,
2030 .remove = dummy_hcd_remove,
Alan Stern391eca92005-05-10 15:34:16 -04002031 .suspend = dummy_hcd_suspend,
2032 .resume = dummy_hcd_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002033 .driver = {
2034 .name = (char *) driver_name,
2035 .owner = THIS_MODULE,
2036 },
Alan Sternd9b76252005-05-03 16:15:43 -04002037};
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039/*-------------------------------------------------------------------------*/
2040
Alan Sterna89a2cd2008-04-07 15:03:25 -04002041static struct platform_device *the_udc_pdev;
2042static struct platform_device *the_hcd_pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044static int __init init (void)
2045{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002046 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 if (usb_disabled ())
2049 return -ENODEV;
Alan Sternd9b76252005-05-03 16:15:43 -04002050
Alan Sterna89a2cd2008-04-07 15:03:25 -04002051 the_hcd_pdev = platform_device_alloc(driver_name, -1);
2052 if (!the_hcd_pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return retval;
Alan Sterna89a2cd2008-04-07 15:03:25 -04002054 the_udc_pdev = platform_device_alloc(gadget_name, -1);
2055 if (!the_udc_pdev)
2056 goto err_alloc_udc;
Alan Sternd9b76252005-05-03 16:15:43 -04002057
Alan Sterna89a2cd2008-04-07 15:03:25 -04002058 retval = platform_driver_register(&dummy_hcd_driver);
2059 if (retval < 0)
2060 goto err_register_hcd_driver;
2061 retval = platform_driver_register(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002062 if (retval < 0)
2063 goto err_register_udc_driver;
2064
Alan Sterna89a2cd2008-04-07 15:03:25 -04002065 retval = platform_device_add(the_hcd_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002066 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002067 goto err_add_hcd;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002068 if (!the_controller.hs_hcd) {
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002069 /*
2070 * The hcd was added successfully but its probe function failed
2071 * for some reason.
2072 */
2073 retval = -EINVAL;
2074 goto err_add_udc;
2075 }
Alan Sterna89a2cd2008-04-07 15:03:25 -04002076 retval = platform_device_add(the_udc_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002077 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002078 goto err_add_udc;
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002079 if (!platform_get_drvdata(the_udc_pdev)) {
2080 /*
2081 * The udc was added successfully but its probe function failed
2082 * for some reason.
2083 */
2084 retval = -EINVAL;
2085 goto err_probe_udc;
2086 }
Alan Sternd9b76252005-05-03 16:15:43 -04002087 return retval;
2088
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002089err_probe_udc:
2090 platform_device_del(the_udc_pdev);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002091err_add_udc:
2092 platform_device_del(the_hcd_pdev);
2093err_add_hcd:
2094 platform_driver_unregister(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002095err_register_udc_driver:
Alan Sterna89a2cd2008-04-07 15:03:25 -04002096 platform_driver_unregister(&dummy_hcd_driver);
2097err_register_hcd_driver:
2098 platform_device_put(the_udc_pdev);
2099err_alloc_udc:
2100 platform_device_put(the_hcd_pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 return retval;
2102}
2103module_init (init);
2104
2105static void __exit cleanup (void)
2106{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002107 platform_device_unregister(the_udc_pdev);
2108 platform_device_unregister(the_hcd_pdev);
2109 platform_driver_unregister(&dummy_udc_driver);
2110 platform_driver_unregister(&dummy_hcd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112module_exit (cleanup);