Merge tag 'for-4.1' of git://git.kernel.org/pub/scm/linux/kernel/git/kishon/linux...
[deliverable/linux.git] / drivers / usb / gadget / udc / dummy_hcd.c
CommitLineData
1da177e4
LT
1/*
2 * dummy_hcd.c -- Dummy/Loopback USB host and device emulator driver.
3 *
4 * Maintainer: Alan Stern <stern@rowland.harvard.edu>
5 *
6 * Copyright (C) 2003 David Brownell
7 * Copyright (C) 2003-2005 Alan Stern
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
1da177e4
LT
13 */
14
15
16/*
17 * This exposes a device side "USB gadget" API, driven by requests to a
18 * Linux-USB host controller driver. USB traffic is simulated; there's
19 * no need for USB hardware. Use this with two other drivers:
20 *
21 * - Gadget driver, responding to requests (slave);
22 * - Host-side device driver, as already familiar in Linux.
23 *
24 * Having this all in one kernel can help some stages of development,
25 * bypassing some hardware (and driver) issues. UML could help too.
26 */
27
1da177e4
LT
28#include <linux/module.h>
29#include <linux/kernel.h>
30#include <linux/delay.h>
31#include <linux/ioport.h>
1da177e4 32#include <linux/slab.h>
1da177e4
LT
33#include <linux/errno.h>
34#include <linux/init.h>
35#include <linux/timer.h>
36#include <linux/list.h>
37#include <linux/interrupt.h>
d052d1be 38#include <linux/platform_device.h>
1da177e4 39#include <linux/usb.h>
9454a57a 40#include <linux/usb/gadget.h>
27729aad 41#include <linux/usb/hcd.h>
14fce33a 42#include <linux/scatterlist.h>
1da177e4
LT
43
44#include <asm/byteorder.h>
18f2cbaa 45#include <linux/io.h>
1da177e4 46#include <asm/irq.h>
1da177e4
LT
47#include <asm/unaligned.h>
48
1da177e4 49#define DRIVER_DESC "USB Host+Gadget Emulator"
391eca9d 50#define DRIVER_VERSION "02 May 2005"
1da177e4 51
caf29f62
AS
52#define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */
53
18f2cbaa
SAS
54static const char driver_name[] = "dummy_hcd";
55static const char driver_desc[] = "USB Host+Gadget Emulator";
1da177e4 56
18f2cbaa 57static const char gadget_name[] = "dummy_udc";
1da177e4 58
18f2cbaa
SAS
59MODULE_DESCRIPTION(DRIVER_DESC);
60MODULE_AUTHOR("David Brownell");
61MODULE_LICENSE("GPL");
1da177e4 62
1cd8fd28
TB
63struct dummy_hcd_module_parameters {
64 bool is_super_speed;
7eca4c5a 65 bool is_high_speed;
c7a1db45 66 unsigned int num;
1cd8fd28
TB
67};
68
69static struct dummy_hcd_module_parameters mod_data = {
7eca4c5a
TB
70 .is_super_speed = false,
71 .is_high_speed = true,
c7a1db45 72 .num = 1,
1cd8fd28
TB
73};
74module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
75MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
7eca4c5a
TB
76module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
77MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
c7a1db45
SAS
78module_param_named(num, mod_data.num, uint, S_IRUGO);
79MODULE_PARM_DESC(num, "number of emulated controllers");
1da177e4
LT
80/*-------------------------------------------------------------------------*/
81
82/* gadget side driver data structres */
83struct dummy_ep {
84 struct list_head queue;
85 unsigned long last_io; /* jiffies timestamp */
86 struct usb_gadget *gadget;
87 const struct usb_endpoint_descriptor *desc;
88 struct usb_ep ep;
18f2cbaa
SAS
89 unsigned halted:1;
90 unsigned wedged:1;
91 unsigned already_seen:1;
92 unsigned setup_stage:1;
a54c979f 93 unsigned stream_en:1;
1da177e4
LT
94};
95
96struct dummy_request {
97 struct list_head queue; /* ep's requests */
98 struct usb_request req;
99};
100
18f2cbaa 101static inline struct dummy_ep *usb_ep_to_dummy_ep(struct usb_ep *_ep)
1da177e4 102{
18f2cbaa 103 return container_of(_ep, struct dummy_ep, ep);
1da177e4
LT
104}
105
106static inline struct dummy_request *usb_request_to_dummy_request
107 (struct usb_request *_req)
108{
18f2cbaa 109 return container_of(_req, struct dummy_request, req);
1da177e4
LT
110}
111
112/*-------------------------------------------------------------------------*/
113
114/*
115 * Every device has ep0 for control requests, plus up to 30 more endpoints,
116 * in one of two types:
117 *
118 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
119 * number can be changed. Names like "ep-a" are used for this type.
120 *
121 * - Fixed Function: in other cases. some characteristics may be mutable;
122 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
123 *
124 * Gadget drivers are responsible for not setting up conflicting endpoint
125 * configurations, illegal or unsupported packet lengths, and so on.
126 */
127
18f2cbaa 128static const char ep0name[] = "ep0";
1da177e4 129
18f2cbaa 130static const char *const ep_name[] = {
1da177e4
LT
131 ep0name, /* everyone has ep0 */
132
1d16638e 133 /* act like a pxa250: fifteen fixed function endpoints */
1da177e4
LT
134 "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
135 "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
136 "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
137 "ep15in-int",
138
139 /* or like sa1100: two fixed function endpoints */
140 "ep1out-bulk", "ep2in-bulk",
1d16638e
SAS
141
142 /* and now some generic EPs so we have enough in multi config */
143 "ep3out", "ep4in", "ep5out", "ep6out", "ep7in", "ep8out", "ep9in",
144 "ep10out", "ep11out", "ep12in", "ep13out", "ep14in", "ep15out",
1da177e4 145};
52950ed4 146#define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
1da177e4 147
d9b76251
AS
148/*-------------------------------------------------------------------------*/
149
1da177e4
LT
150#define FIFO_SIZE 64
151
152struct urbp {
153 struct urb *urb;
154 struct list_head urbp_list;
14fce33a
SAS
155 struct sg_mapping_iter miter;
156 u32 miter_started;
1da177e4
LT
157};
158
391eca9d
AS
159
160enum dummy_rh_state {
161 DUMMY_RH_RESET,
162 DUMMY_RH_SUSPENDED,
163 DUMMY_RH_RUNNING
164};
165
cdfcbd2c
TB
166struct dummy_hcd {
167 struct dummy *dum;
168 enum dummy_rh_state rh_state;
169 struct timer_list timer;
170 u32 port_status;
171 u32 old_status;
172 unsigned long re_timeout;
173
174 struct usb_device *udev;
175 struct list_head urbp_list;
a54c979f
SAS
176 u32 stream_en_ep;
177 u8 num_stream[30 / 2];
cdfcbd2c
TB
178
179 unsigned active:1;
180 unsigned old_active:1;
181 unsigned resuming:1;
182};
183
1da177e4
LT
184struct dummy {
185 spinlock_t lock;
186
187 /*
188 * SLAVE/GADGET side support
189 */
18f2cbaa 190 struct dummy_ep ep[DUMMY_ENDPOINTS];
1da177e4
LT
191 int address;
192 struct usb_gadget gadget;
193 struct usb_gadget_driver *driver;
194 struct dummy_request fifo_req;
18f2cbaa 195 u8 fifo_buf[FIFO_SIZE];
1da177e4 196 u16 devstatus;
391eca9d 197 unsigned udc_suspended:1;
f1c39fad 198 unsigned pullup:1;
1da177e4
LT
199
200 /*
201 * MASTER/HOST side support
202 */
cdfcbd2c 203 struct dummy_hcd *hs_hcd;
1cd8fd28 204 struct dummy_hcd *ss_hcd;
1da177e4
LT
205};
206
cdfcbd2c 207static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
1da177e4 208{
cdfcbd2c 209 return (struct dummy_hcd *) (hcd->hcd_priv);
1da177e4
LT
210}
211
cdfcbd2c 212static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
1da177e4
LT
213{
214 return container_of((void *) dum, struct usb_hcd, hcd_priv);
215}
216
cdfcbd2c 217static inline struct device *dummy_dev(struct dummy_hcd *dum)
1da177e4 218{
cdfcbd2c 219 return dummy_hcd_to_hcd(dum)->self.controller;
1da177e4
LT
220}
221
18f2cbaa 222static inline struct device *udc_dev(struct dummy *dum)
d9b76251
AS
223{
224 return dum->gadget.dev.parent;
225}
226
18f2cbaa 227static inline struct dummy *ep_to_dummy(struct dummy_ep *ep)
1da177e4 228{
18f2cbaa 229 return container_of(ep->gadget, struct dummy, gadget);
1da177e4
LT
230}
231
cdfcbd2c 232static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
1da177e4 233{
cdfcbd2c 234 struct dummy *dum = container_of(gadget, struct dummy, gadget);
1cd8fd28
TB
235 if (dum->gadget.speed == USB_SPEED_SUPER)
236 return dum->ss_hcd;
237 else
238 return dum->hs_hcd;
1da177e4
LT
239}
240
18f2cbaa 241static inline struct dummy *gadget_dev_to_dummy(struct device *dev)
1da177e4 242{
18f2cbaa 243 return container_of(dev, struct dummy, gadget.dev);
1da177e4
LT
244}
245
1da177e4
LT
246/*-------------------------------------------------------------------------*/
247
f1c39fad
AS
248/* SLAVE/GADGET SIDE UTILITY ROUTINES */
249
250/* called with spinlock held */
18f2cbaa 251static void nuke(struct dummy *dum, struct dummy_ep *ep)
f1c39fad 252{
18f2cbaa 253 while (!list_empty(&ep->queue)) {
f1c39fad
AS
254 struct dummy_request *req;
255
18f2cbaa
SAS
256 req = list_entry(ep->queue.next, struct dummy_request, queue);
257 list_del_init(&req->queue);
f1c39fad
AS
258 req->req.status = -ESHUTDOWN;
259
18f2cbaa 260 spin_unlock(&dum->lock);
304f7e5e 261 usb_gadget_giveback_request(&ep->ep, &req->req);
18f2cbaa 262 spin_lock(&dum->lock);
f1c39fad
AS
263 }
264}
265
266/* caller must hold lock */
18f2cbaa 267static void stop_activity(struct dummy *dum)
f1c39fad
AS
268{
269 struct dummy_ep *ep;
270
271 /* prevent any more requests */
272 dum->address = 0;
273
274 /* The timer is left running so that outstanding URBs can fail */
275
276 /* nuke any pending requests first, so driver i/o is quiesced */
18f2cbaa
SAS
277 list_for_each_entry(ep, &dum->gadget.ep_list, ep.ep_list)
278 nuke(dum, ep);
f1c39fad
AS
279
280 /* driver now does any non-usb quiescing necessary */
281}
282
1cd8fd28
TB
283/**
284 * set_link_state_by_speed() - Sets the current state of the link according to
285 * the hcd speed
286 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
287 *
288 * This function updates the port_status according to the link state and the
289 * speed of the hcd.
290 */
291static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
292{
293 struct dummy *dum = dum_hcd->dum;
294
295 if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
296 if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
297 dum_hcd->port_status = 0;
298 } else if (!dum->pullup || dum->udc_suspended) {
299 /* UDC suspend must cause a disconnect */
300 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
301 USB_PORT_STAT_ENABLE);
302 if ((dum_hcd->old_status &
303 USB_PORT_STAT_CONNECTION) != 0)
304 dum_hcd->port_status |=
305 (USB_PORT_STAT_C_CONNECTION << 16);
306 } else {
307 /* device is connected and not suspended */
308 dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
309 USB_PORT_STAT_SPEED_5GBPS) ;
310 if ((dum_hcd->old_status &
311 USB_PORT_STAT_CONNECTION) == 0)
312 dum_hcd->port_status |=
313 (USB_PORT_STAT_C_CONNECTION << 16);
314 if ((dum_hcd->port_status &
315 USB_PORT_STAT_ENABLE) == 1 &&
316 (dum_hcd->port_status &
317 USB_SS_PORT_LS_U0) == 1 &&
318 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
319 dum_hcd->active = 1;
320 }
321 } else {
322 if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
323 dum_hcd->port_status = 0;
324 } else if (!dum->pullup || dum->udc_suspended) {
325 /* UDC suspend must cause a disconnect */
326 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
327 USB_PORT_STAT_ENABLE |
328 USB_PORT_STAT_LOW_SPEED |
329 USB_PORT_STAT_HIGH_SPEED |
330 USB_PORT_STAT_SUSPEND);
331 if ((dum_hcd->old_status &
332 USB_PORT_STAT_CONNECTION) != 0)
333 dum_hcd->port_status |=
334 (USB_PORT_STAT_C_CONNECTION << 16);
335 } else {
336 dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
337 if ((dum_hcd->old_status &
338 USB_PORT_STAT_CONNECTION) == 0)
339 dum_hcd->port_status |=
340 (USB_PORT_STAT_C_CONNECTION << 16);
341 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
342 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
343 else if ((dum_hcd->port_status &
344 USB_PORT_STAT_SUSPEND) == 0 &&
345 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
346 dum_hcd->active = 1;
347 }
348 }
349}
350
f1c39fad 351/* caller must hold lock */
cdfcbd2c 352static void set_link_state(struct dummy_hcd *dum_hcd)
f1c39fad 353{
cdfcbd2c
TB
354 struct dummy *dum = dum_hcd->dum;
355
356 dum_hcd->active = 0;
1cd8fd28
TB
357 if (dum->pullup)
358 if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
359 dum->gadget.speed != USB_SPEED_SUPER) ||
360 (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
361 dum->gadget.speed == USB_SPEED_SUPER))
362 return;
363
364 set_link_state_by_speed(dum_hcd);
f1c39fad 365
cdfcbd2c
TB
366 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
367 dum_hcd->active)
368 dum_hcd->resuming = 0;
f1c39fad 369
8480484d 370 /* Currently !connected or in reset */
cdfcbd2c
TB
371 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
372 (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
8480484d
AS
373 unsigned disconnect = USB_PORT_STAT_CONNECTION &
374 dum_hcd->old_status & (~dum_hcd->port_status);
375 unsigned reset = USB_PORT_STAT_RESET &
376 (~dum_hcd->old_status) & dum_hcd->port_status;
377
378 /* Report reset and disconnect events to the driver */
379 if (dum->driver && (disconnect || reset)) {
1cd8fd28
TB
380 stop_activity(dum);
381 spin_unlock(&dum->lock);
8480484d
AS
382 if (reset)
383 usb_gadget_udc_reset(&dum->gadget, dum->driver);
384 else
385 dum->driver->disconnect(&dum->gadget);
1cd8fd28 386 spin_lock(&dum->lock);
f1c39fad 387 }
cdfcbd2c
TB
388 } else if (dum_hcd->active != dum_hcd->old_active) {
389 if (dum_hcd->old_active && dum->driver->suspend) {
1cd8fd28
TB
390 spin_unlock(&dum->lock);
391 dum->driver->suspend(&dum->gadget);
392 spin_lock(&dum->lock);
393 } else if (!dum_hcd->old_active && dum->driver->resume) {
394 spin_unlock(&dum->lock);
395 dum->driver->resume(&dum->gadget);
396 spin_lock(&dum->lock);
f1c39fad
AS
397 }
398 }
399
cdfcbd2c
TB
400 dum_hcd->old_status = dum_hcd->port_status;
401 dum_hcd->old_active = dum_hcd->active;
f1c39fad
AS
402}
403
404/*-------------------------------------------------------------------------*/
405
1da177e4
LT
406/* SLAVE/GADGET SIDE DRIVER
407 *
408 * This only tracks gadget state. All the work is done when the host
409 * side tries some (emulated) i/o operation. Real device controller
410 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
411 */
412
413#define is_enabled(dum) \
414 (dum->port_status & USB_PORT_STAT_ENABLE)
415
18f2cbaa
SAS
416static int dummy_enable(struct usb_ep *_ep,
417 const struct usb_endpoint_descriptor *desc)
1da177e4
LT
418{
419 struct dummy *dum;
cdfcbd2c 420 struct dummy_hcd *dum_hcd;
1da177e4
LT
421 struct dummy_ep *ep;
422 unsigned max;
423 int retval;
424
18f2cbaa 425 ep = usb_ep_to_dummy_ep(_ep);
1da177e4
LT
426 if (!_ep || !desc || ep->desc || _ep->name == ep0name
427 || desc->bDescriptorType != USB_DT_ENDPOINT)
428 return -EINVAL;
18f2cbaa 429 dum = ep_to_dummy(ep);
cdfcbd2c
TB
430 if (!dum->driver)
431 return -ESHUTDOWN;
719e52cb
SAS
432
433 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
cdfcbd2c 434 if (!is_enabled(dum_hcd))
1da177e4 435 return -ESHUTDOWN;
cdfcbd2c
TB
436
437 /*
438 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
439 * maximum packet size.
1cd8fd28 440 * For SS devices the wMaxPacketSize is limited by 1024.
cdfcbd2c 441 */
29cc8897 442 max = usb_endpoint_maxp(desc) & 0x7ff;
1da177e4
LT
443
444 /* drivers must not request bad settings, since lower levels
445 * (hardware or its drivers) may not check. some endpoints
446 * can't do iso, many have maxpacket limitations, etc.
447 *
448 * since this "hardware" driver is here to help debugging, we
449 * have some extra sanity checks. (there could be more though,
450 * especially for "ep9out" style fixed function ones.)
451 */
452 retval = -EINVAL;
59f08e6d 453 switch (usb_endpoint_type(desc)) {
1da177e4 454 case USB_ENDPOINT_XFER_BULK:
18f2cbaa
SAS
455 if (strstr(ep->ep.name, "-iso")
456 || strstr(ep->ep.name, "-int")) {
1da177e4
LT
457 goto done;
458 }
459 switch (dum->gadget.speed) {
1cd8fd28
TB
460 case USB_SPEED_SUPER:
461 if (max == 1024)
462 break;
463 goto done;
1da177e4
LT
464 case USB_SPEED_HIGH:
465 if (max == 512)
466 break;
9063ff44
IL
467 goto done;
468 case USB_SPEED_FULL:
469 if (max == 8 || max == 16 || max == 32 || max == 64)
1da177e4
LT
470 /* we'll fake any legal size */
471 break;
9063ff44
IL
472 /* save a return statement */
473 default:
474 goto done;
1da177e4
LT
475 }
476 break;
477 case USB_ENDPOINT_XFER_INT:
18f2cbaa 478 if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
1da177e4
LT
479 goto done;
480 /* real hardware might not handle all packet sizes */
481 switch (dum->gadget.speed) {
1cd8fd28 482 case USB_SPEED_SUPER:
1da177e4
LT
483 case USB_SPEED_HIGH:
484 if (max <= 1024)
485 break;
486 /* save a return statement */
487 case USB_SPEED_FULL:
488 if (max <= 64)
489 break;
490 /* save a return statement */
491 default:
492 if (max <= 8)
493 break;
494 goto done;
495 }
496 break;
497 case USB_ENDPOINT_XFER_ISOC:
18f2cbaa
SAS
498 if (strstr(ep->ep.name, "-bulk")
499 || strstr(ep->ep.name, "-int"))
1da177e4
LT
500 goto done;
501 /* real hardware might not handle all packet sizes */
502 switch (dum->gadget.speed) {
1cd8fd28 503 case USB_SPEED_SUPER:
1da177e4
LT
504 case USB_SPEED_HIGH:
505 if (max <= 1024)
506 break;
507 /* save a return statement */
508 case USB_SPEED_FULL:
509 if (max <= 1023)
510 break;
511 /* save a return statement */
512 default:
513 goto done;
514 }
515 break;
516 default:
517 /* few chips support control except on ep0 */
518 goto done;
519 }
520
521 _ep->maxpacket = max;
a54c979f
SAS
522 if (usb_ss_max_streams(_ep->comp_desc)) {
523 if (!usb_endpoint_xfer_bulk(desc)) {
524 dev_err(udc_dev(dum), "Can't enable stream support on "
525 "non-bulk ep %s\n", _ep->name);
526 return -EINVAL;
527 }
528 ep->stream_en = 1;
529 }
3cf0ad02 530 ep->desc = desc;
1da177e4 531
a54c979f 532 dev_dbg(udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n",
1da177e4
LT
533 _ep->name,
534 desc->bEndpointAddress & 0x0f,
535 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
536 ({ char *val;
59f08e6d 537 switch (usb_endpoint_type(desc)) {
7c884fe4
TB
538 case USB_ENDPOINT_XFER_BULK:
539 val = "bulk";
540 break;
541 case USB_ENDPOINT_XFER_ISOC:
542 val = "iso";
543 break;
544 case USB_ENDPOINT_XFER_INT:
545 val = "intr";
546 break;
547 default:
548 val = "ctrl";
549 break;
2b84f92b 550 } val; }),
a54c979f 551 max, ep->stream_en ? "enabled" : "disabled");
1da177e4
LT
552
553 /* at this point real hardware should be NAKing transfers
554 * to that endpoint, until a buffer is queued to it.
555 */
851a526d 556 ep->halted = ep->wedged = 0;
1da177e4
LT
557 retval = 0;
558done:
559 return retval;
560}
561
18f2cbaa 562static int dummy_disable(struct usb_ep *_ep)
1da177e4
LT
563{
564 struct dummy_ep *ep;
565 struct dummy *dum;
566 unsigned long flags;
1da177e4 567
18f2cbaa 568 ep = usb_ep_to_dummy_ep(_ep);
1da177e4
LT
569 if (!_ep || !ep->desc || _ep->name == ep0name)
570 return -EINVAL;
18f2cbaa 571 dum = ep_to_dummy(ep);
1da177e4 572
18f2cbaa 573 spin_lock_irqsave(&dum->lock, flags);
1da177e4 574 ep->desc = NULL;
a54c979f 575 ep->stream_en = 0;
18f2cbaa
SAS
576 nuke(dum, ep);
577 spin_unlock_irqrestore(&dum->lock, flags);
1da177e4 578
18f2cbaa 579 dev_dbg(udc_dev(dum), "disabled %s\n", _ep->name);
849b1333 580 return 0;
1da177e4
LT
581}
582
18f2cbaa
SAS
583static struct usb_request *dummy_alloc_request(struct usb_ep *_ep,
584 gfp_t mem_flags)
1da177e4
LT
585{
586 struct dummy_ep *ep;
587 struct dummy_request *req;
588
589 if (!_ep)
590 return NULL;
18f2cbaa 591 ep = usb_ep_to_dummy_ep(_ep);
1da177e4 592
7039f422 593 req = kzalloc(sizeof(*req), mem_flags);
1da177e4
LT
594 if (!req)
595 return NULL;
18f2cbaa 596 INIT_LIST_HEAD(&req->queue);
1da177e4
LT
597 return &req->req;
598}
599
18f2cbaa 600static void dummy_free_request(struct usb_ep *_ep, struct usb_request *_req)
1da177e4 601{
1da177e4
LT
602 struct dummy_request *req;
603
f99987bb 604 if (!_ep || !_req) {
72688dc9 605 WARN_ON(1);
1da177e4 606 return;
f99987bb 607 }
1da177e4 608
18f2cbaa
SAS
609 req = usb_request_to_dummy_request(_req);
610 WARN_ON(!list_empty(&req->queue));
611 kfree(req);
1da177e4
LT
612}
613
18f2cbaa 614static void fifo_complete(struct usb_ep *ep, struct usb_request *req)
1da177e4
LT
615{
616}
617
18f2cbaa 618static int dummy_queue(struct usb_ep *_ep, struct usb_request *_req,
55016f10 619 gfp_t mem_flags)
1da177e4
LT
620{
621 struct dummy_ep *ep;
622 struct dummy_request *req;
623 struct dummy *dum;
cdfcbd2c 624 struct dummy_hcd *dum_hcd;
1da177e4
LT
625 unsigned long flags;
626
18f2cbaa
SAS
627 req = usb_request_to_dummy_request(_req);
628 if (!_req || !list_empty(&req->queue) || !_req->complete)
1da177e4
LT
629 return -EINVAL;
630
18f2cbaa 631 ep = usb_ep_to_dummy_ep(_ep);
1da177e4
LT
632 if (!_ep || (!ep->desc && _ep->name != ep0name))
633 return -EINVAL;
634
18f2cbaa 635 dum = ep_to_dummy(ep);
719e52cb 636 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
cdfcbd2c 637 if (!dum->driver || !is_enabled(dum_hcd))
1da177e4
LT
638 return -ESHUTDOWN;
639
640#if 0
18f2cbaa 641 dev_dbg(udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
1da177e4
LT
642 ep, _req, _ep->name, _req->length, _req->buf);
643#endif
1da177e4
LT
644 _req->status = -EINPROGRESS;
645 _req->actual = 0;
18f2cbaa 646 spin_lock_irqsave(&dum->lock, flags);
1da177e4
LT
647
648 /* implement an emulated single-request FIFO */
649 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
18f2cbaa
SAS
650 list_empty(&dum->fifo_req.queue) &&
651 list_empty(&ep->queue) &&
1da177e4
LT
652 _req->length <= FIFO_SIZE) {
653 req = &dum->fifo_req;
654 req->req = *_req;
655 req->req.buf = dum->fifo_buf;
18f2cbaa 656 memcpy(dum->fifo_buf, _req->buf, _req->length);
1da177e4
LT
657 req->req.context = dum;
658 req->req.complete = fifo_complete;
659
c728df70 660 list_add_tail(&req->queue, &ep->queue);
18f2cbaa 661 spin_unlock(&dum->lock);
1da177e4
LT
662 _req->actual = _req->length;
663 _req->status = 0;
304f7e5e 664 usb_gadget_giveback_request(_ep, _req);
18f2cbaa 665 spin_lock(&dum->lock);
c728df70
DB
666 } else
667 list_add_tail(&req->queue, &ep->queue);
18f2cbaa 668 spin_unlock_irqrestore(&dum->lock, flags);
1da177e4
LT
669
670 /* real hardware would likely enable transfers here, in case
671 * it'd been left NAKing.
672 */
673 return 0;
674}
675
18f2cbaa 676static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1da177e4
LT
677{
678 struct dummy_ep *ep;
679 struct dummy *dum;
680 int retval = -EINVAL;
681 unsigned long flags;
682 struct dummy_request *req = NULL;
683
684 if (!_ep || !_req)
685 return retval;
18f2cbaa
SAS
686 ep = usb_ep_to_dummy_ep(_ep);
687 dum = ep_to_dummy(ep);
1da177e4
LT
688
689 if (!dum->driver)
690 return -ESHUTDOWN;
691
18f2cbaa
SAS
692 local_irq_save(flags);
693 spin_lock(&dum->lock);
694 list_for_each_entry(req, &ep->queue, queue) {
1da177e4 695 if (&req->req == _req) {
18f2cbaa 696 list_del_init(&req->queue);
1da177e4
LT
697 _req->status = -ECONNRESET;
698 retval = 0;
699 break;
700 }
701 }
18f2cbaa 702 spin_unlock(&dum->lock);
1da177e4
LT
703
704 if (retval == 0) {
18f2cbaa 705 dev_dbg(udc_dev(dum),
1da177e4
LT
706 "dequeued req %p from %s, len %d buf %p\n",
707 req, _ep->name, _req->length, _req->buf);
304f7e5e 708 usb_gadget_giveback_request(_ep, _req);
1da177e4 709 }
18f2cbaa 710 local_irq_restore(flags);
1da177e4
LT
711 return retval;
712}
713
714static int
851a526d 715dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
1da177e4
LT
716{
717 struct dummy_ep *ep;
718 struct dummy *dum;
719
720 if (!_ep)
721 return -EINVAL;
18f2cbaa
SAS
722 ep = usb_ep_to_dummy_ep(_ep);
723 dum = ep_to_dummy(ep);
1da177e4
LT
724 if (!dum->driver)
725 return -ESHUTDOWN;
726 if (!value)
851a526d 727 ep->halted = ep->wedged = 0;
1da177e4 728 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
18f2cbaa 729 !list_empty(&ep->queue))
1da177e4 730 return -EAGAIN;
851a526d 731 else {
1da177e4 732 ep->halted = 1;
851a526d
AS
733 if (wedged)
734 ep->wedged = 1;
735 }
1da177e4
LT
736 /* FIXME clear emulated data toggle too */
737 return 0;
738}
739
851a526d
AS
740static int
741dummy_set_halt(struct usb_ep *_ep, int value)
742{
743 return dummy_set_halt_and_wedge(_ep, value, 0);
744}
745
746static int dummy_set_wedge(struct usb_ep *_ep)
747{
748 if (!_ep || _ep->name == ep0name)
749 return -EINVAL;
750 return dummy_set_halt_and_wedge(_ep, 1, 1);
751}
752
1da177e4
LT
753static const struct usb_ep_ops dummy_ep_ops = {
754 .enable = dummy_enable,
755 .disable = dummy_disable,
756
757 .alloc_request = dummy_alloc_request,
758 .free_request = dummy_free_request,
759
1da177e4
LT
760 .queue = dummy_queue,
761 .dequeue = dummy_dequeue,
762
763 .set_halt = dummy_set_halt,
851a526d 764 .set_wedge = dummy_set_wedge,
1da177e4
LT
765};
766
767/*-------------------------------------------------------------------------*/
768
769/* there are both host and device side versions of this call ... */
18f2cbaa 770static int dummy_g_get_frame(struct usb_gadget *_gadget)
1da177e4
LT
771{
772 struct timeval tv;
773
18f2cbaa 774 do_gettimeofday(&tv);
1da177e4
LT
775 return tv.tv_usec / 1000;
776}
777
18f2cbaa 778static int dummy_wakeup(struct usb_gadget *_gadget)
1da177e4 779{
cdfcbd2c 780 struct dummy_hcd *dum_hcd;
1da177e4 781
cdfcbd2c
TB
782 dum_hcd = gadget_to_dummy_hcd(_gadget);
783 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
5742b0c9 784 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
1da177e4 785 return -EINVAL;
cdfcbd2c 786 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
391eca9d 787 return -ENOLINK;
cdfcbd2c
TB
788 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
789 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
391eca9d
AS
790 return -EIO;
791
792 /* FIXME: What if the root hub is suspended but the port isn't? */
1da177e4
LT
793
794 /* hub notices our request, issues downstream resume, etc */
cdfcbd2c
TB
795 dum_hcd->resuming = 1;
796 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
797 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
1da177e4
LT
798 return 0;
799}
800
18f2cbaa 801static int dummy_set_selfpowered(struct usb_gadget *_gadget, int value)
1da177e4
LT
802{
803 struct dummy *dum;
804
5d9cb6af 805 _gadget->is_selfpowered = (value != 0);
18f2cbaa 806 dum = gadget_to_dummy_hcd(_gadget)->dum;
1da177e4
LT
807 if (value)
808 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
809 else
810 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
811 return 0;
812}
813
d262127c 814static void dummy_udc_update_ep0(struct dummy *dum)
b5738413 815{
c6884191 816 if (dum->gadget.speed == USB_SPEED_SUPER)
b5738413 817 dum->ep[0].ep.maxpacket = 9;
c6884191 818 else
b5738413 819 dum->ep[0].ep.maxpacket = 64;
b5738413
SAS
820}
821
18f2cbaa 822static int dummy_pullup(struct usb_gadget *_gadget, int value)
f1c39fad 823{
d8a14a85 824 struct dummy_hcd *dum_hcd;
f1c39fad
AS
825 struct dummy *dum;
826 unsigned long flags;
827
b5738413
SAS
828 dum = gadget_dev_to_dummy(&_gadget->dev);
829
830 if (value && dum->driver) {
831 if (mod_data.is_super_speed)
7177aed4 832 dum->gadget.speed = dum->driver->max_speed;
b5738413
SAS
833 else if (mod_data.is_high_speed)
834 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
7177aed4 835 dum->driver->max_speed);
b5738413
SAS
836 else
837 dum->gadget.speed = USB_SPEED_FULL;
d262127c 838 dummy_udc_update_ep0(dum);
b5738413 839
7177aed4 840 if (dum->gadget.speed < dum->driver->max_speed)
b5738413 841 dev_dbg(udc_dev(dum), "This device can perform faster"
7177aed4
MN
842 " if you connect it to a %s port...\n",
843 usb_speed_string(dum->driver->max_speed));
b5738413 844 }
d8a14a85 845 dum_hcd = gadget_to_dummy_hcd(_gadget);
d8a14a85 846
18f2cbaa 847 spin_lock_irqsave(&dum->lock, flags);
f1c39fad 848 dum->pullup = (value != 0);
d8a14a85 849 set_link_state(dum_hcd);
18f2cbaa 850 spin_unlock_irqrestore(&dum->lock, flags);
b5738413 851
d8a14a85 852 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
f1c39fad
AS
853 return 0;
854}
855
aa074739
SAS
856static int dummy_udc_start(struct usb_gadget *g,
857 struct usb_gadget_driver *driver);
22835b80 858static int dummy_udc_stop(struct usb_gadget *g);
0f91349b 859
1da177e4
LT
860static const struct usb_gadget_ops dummy_ops = {
861 .get_frame = dummy_g_get_frame,
862 .wakeup = dummy_wakeup,
863 .set_selfpowered = dummy_set_selfpowered,
f1c39fad 864 .pullup = dummy_pullup,
aa074739
SAS
865 .udc_start = dummy_udc_start,
866 .udc_stop = dummy_udc_stop,
1da177e4
LT
867};
868
869/*-------------------------------------------------------------------------*/
870
871/* "function" sysfs attribute */
ce26bd23 872static ssize_t function_show(struct device *dev, struct device_attribute *attr,
18f2cbaa 873 char *buf)
1da177e4 874{
18f2cbaa 875 struct dummy *dum = gadget_dev_to_dummy(dev);
1da177e4
LT
876
877 if (!dum->driver || !dum->driver->function)
878 return 0;
18f2cbaa 879 return scnprintf(buf, PAGE_SIZE, "%s\n", dum->driver->function);
1da177e4 880}
ce26bd23 881static DEVICE_ATTR_RO(function);
1da177e4
LT
882
883/*-------------------------------------------------------------------------*/
884
885/*
886 * Driver registration/unregistration.
887 *
888 * This is basically hardware-specific; there's usually only one real USB
889 * device (not host) controller since that's how USB devices are intended
890 * to work. So most implementations of these api calls will rely on the
891 * fact that only one driver will ever bind to the hardware. But curious
892 * hardware can be built with discrete components, so the gadget API doesn't
893 * require that assumption.
894 *
895 * For this emulator, it might be convenient to create a usb slave device
896 * for each driver that registers: just add to a big root hub.
897 */
898
aa074739
SAS
899static int dummy_udc_start(struct usb_gadget *g,
900 struct usb_gadget_driver *driver)
1da177e4 901{
aa074739
SAS
902 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
903 struct dummy *dum = dum_hcd->dum;
1da177e4 904
7177aed4 905 if (driver->max_speed == USB_SPEED_UNKNOWN)
1da177e4
LT
906 return -EINVAL;
907
908 /*
909 * SLAVE side init ... the layer above hardware, which
910 * can't enumerate without help from the driver we're binding.
911 */
5742b0c9 912
1da177e4 913 dum->devstatus = 0;
1da177e4 914 dum->driver = driver;
dd7e6dd5 915
1da177e4
LT
916 return 0;
917}
1da177e4 918
22835b80 919static int dummy_udc_stop(struct usb_gadget *g)
1da177e4 920{
aa074739
SAS
921 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
922 struct dummy *dum = dum_hcd->dum;
1da177e4 923
1da177e4 924 dum->driver = NULL;
25427874 925
1da177e4
LT
926 return 0;
927}
1da177e4
LT
928
929#undef is_enabled
930
d9b76251
AS
931/* The gadget structure is stored inside the hcd structure and will be
932 * released along with it. */
0fb57599
SAS
933static void init_dummy_udc_hw(struct dummy *dum)
934{
935 int i;
936
937 INIT_LIST_HEAD(&dum->gadget.ep_list);
938 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
939 struct dummy_ep *ep = &dum->ep[i];
940
941 if (!ep_name[i])
942 break;
943 ep->ep.name = ep_name[i];
944 ep->ep.ops = &dummy_ep_ops;
945 list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
946 ep->halted = ep->wedged = ep->already_seen =
947 ep->setup_stage = 0;
e117e742 948 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
c6884191 949 ep->ep.max_streams = 16;
0fb57599
SAS
950 ep->last_io = jiffies;
951 ep->gadget = &dum->gadget;
952 ep->desc = NULL;
953 INIT_LIST_HEAD(&ep->queue);
954 }
955
956 dum->gadget.ep0 = &dum->ep[0].ep;
957 list_del_init(&dum->ep[0].ep.ep_list);
958 INIT_LIST_HEAD(&dum->fifo_req.queue);
f8744d40
SAS
959
960#ifdef CONFIG_USB_OTG
961 dum->gadget.is_otg = 1;
962#endif
0fb57599
SAS
963}
964
18f2cbaa 965static int dummy_udc_probe(struct platform_device *pdev)
d9b76251 966{
b100a2f3 967 struct dummy *dum;
d9b76251
AS
968 int rc;
969
b100a2f3 970 dum = *((void **)dev_get_platdata(&pdev->dev));
d9b76251
AS
971 dum->gadget.name = gadget_name;
972 dum->gadget.ops = &dummy_ops;
d327ab5b 973 dum->gadget.max_speed = USB_SPEED_SUPER;
d9b76251 974
8364d6b0 975 dum->gadget.dev.parent = &pdev->dev;
0fb57599
SAS
976 init_dummy_udc_hw(dum);
977
0f91349b
SAS
978 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
979 if (rc < 0)
980 goto err_udc;
981
18f2cbaa 982 rc = device_create_file(&dum->gadget.dev, &dev_attr_function);
efd54a36 983 if (rc < 0)
0f91349b
SAS
984 goto err_dev;
985 platform_set_drvdata(pdev, dum);
986 return rc;
987
988err_dev:
989 usb_del_gadget_udc(&dum->gadget);
990err_udc:
d9b76251
AS
991 return rc;
992}
993
18f2cbaa 994static int dummy_udc_remove(struct platform_device *pdev)
d9b76251 995{
18f2cbaa 996 struct dummy *dum = platform_get_drvdata(pdev);
d9b76251 997
18f2cbaa 998 device_remove_file(&dum->gadget.dev, &dev_attr_function);
5f5610f6 999 usb_del_gadget_udc(&dum->gadget);
d9b76251
AS
1000 return 0;
1001}
1002
fc0b721f
SAS
1003static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1004 int suspend)
391eca9d 1005{
fc0b721f
SAS
1006 spin_lock_irq(&dum->lock);
1007 dum->udc_suspended = suspend;
d8a14a85 1008 set_link_state(dum_hcd);
fc0b721f
SAS
1009 spin_unlock_irq(&dum->lock);
1010}
1011
1012static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1013{
1014 struct dummy *dum = platform_get_drvdata(pdev);
1015 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
391eca9d 1016
fc0b721f
SAS
1017 dev_dbg(&pdev->dev, "%s\n", __func__);
1018 dummy_udc_pm(dum, dum_hcd, 1);
d8a14a85 1019 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
391eca9d
AS
1020 return 0;
1021}
1022
fc0b721f 1023static int dummy_udc_resume(struct platform_device *pdev)
391eca9d 1024{
fc0b721f
SAS
1025 struct dummy *dum = platform_get_drvdata(pdev);
1026 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
391eca9d 1027
fc0b721f
SAS
1028 dev_dbg(&pdev->dev, "%s\n", __func__);
1029 dummy_udc_pm(dum, dum_hcd, 0);
d8a14a85 1030 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
391eca9d
AS
1031 return 0;
1032}
1033
3ae5eaec 1034static struct platform_driver dummy_udc_driver = {
d9b76251
AS
1035 .probe = dummy_udc_probe,
1036 .remove = dummy_udc_remove,
391eca9d
AS
1037 .suspend = dummy_udc_suspend,
1038 .resume = dummy_udc_resume,
3ae5eaec
RK
1039 .driver = {
1040 .name = (char *) gadget_name,
3ae5eaec 1041 },
d9b76251
AS
1042};
1043
1da177e4
LT
1044/*-------------------------------------------------------------------------*/
1045
a54c979f
SAS
1046static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc)
1047{
1048 unsigned int index;
1049
1050 index = usb_endpoint_num(desc) << 1;
1051 if (usb_endpoint_dir_in(desc))
1052 index |= 1;
1053 return index;
1054}
1055
1da177e4
LT
1056/* MASTER/HOST SIDE DRIVER
1057 *
1058 * this uses the hcd framework to hook up to host side drivers.
1059 * its root hub will only have one device, otherwise it acts like
1060 * a normal host controller.
1061 *
1062 * when urbs are queued, they're just stuck on a list that we
1063 * scan in a timer callback. that callback connects writes from
1064 * the host with reads from the device, and so on, based on the
1065 * usb 2.0 rules.
1066 */
1067
a54c979f
SAS
1068static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb)
1069{
1070 const struct usb_endpoint_descriptor *desc = &urb->ep->desc;
1071 u32 index;
1072
1073 if (!usb_endpoint_xfer_bulk(desc))
1074 return 0;
1075
1076 index = dummy_get_ep_idx(desc);
1077 return (1 << index) & dum_hcd->stream_en_ep;
1078}
1079
1080/*
1081 * The max stream number is saved as a nibble so for the 30 possible endpoints
1082 * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
1083 * means we use only 1 stream). The maximum according to the spec is 16bit so
1084 * if the 16 stream limit is about to go, the array size should be incremented
1085 * to 30 elements of type u16.
1086 */
1087static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1088 unsigned int pipe)
1089{
1090 int max_streams;
1091
1092 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1093 if (usb_pipeout(pipe))
1094 max_streams >>= 4;
1095 else
1096 max_streams &= 0xf;
1097 max_streams++;
1098 return max_streams;
1099}
1100
1101static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1102 unsigned int pipe, unsigned int streams)
1103{
1104 int max_streams;
1105
1106 streams--;
1107 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1108 if (usb_pipeout(pipe)) {
1109 streams <<= 4;
1110 max_streams &= 0xf;
1111 } else {
1112 max_streams &= 0xf0;
1113 }
1114 max_streams |= streams;
1115 dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams;
1116}
1117
1118static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb)
1119{
1120 unsigned int max_streams;
1121 int enabled;
1122
1123 enabled = dummy_ep_stream_en(dum_hcd, urb);
1124 if (!urb->stream_id) {
1125 if (enabled)
1126 return -EINVAL;
1127 return 0;
1128 }
1129 if (!enabled)
1130 return -EINVAL;
1131
1132 max_streams = get_max_streams_for_pipe(dum_hcd,
1133 usb_pipeendpoint(urb->pipe));
1134 if (urb->stream_id > max_streams) {
1135 dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n",
1136 urb->stream_id);
1137 BUG();
1138 return -EINVAL;
1139 }
1140 return 0;
1141}
1142
18f2cbaa 1143static int dummy_urb_enqueue(
1da177e4 1144 struct usb_hcd *hcd,
1da177e4 1145 struct urb *urb,
55016f10 1146 gfp_t mem_flags
1da177e4 1147) {
cdfcbd2c 1148 struct dummy_hcd *dum_hcd;
1da177e4
LT
1149 struct urbp *urbp;
1150 unsigned long flags;
e9df41c5 1151 int rc;
1da177e4 1152
18f2cbaa 1153 urbp = kmalloc(sizeof *urbp, mem_flags);
1da177e4
LT
1154 if (!urbp)
1155 return -ENOMEM;
1156 urbp->urb = urb;
14fce33a 1157 urbp->miter_started = 0;
1da177e4 1158
cdfcbd2c
TB
1159 dum_hcd = hcd_to_dummy_hcd(hcd);
1160 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
a54c979f
SAS
1161
1162 rc = dummy_validate_stream(dum_hcd, urb);
1163 if (rc) {
1164 kfree(urbp);
1165 goto done;
1166 }
1167
e9df41c5
AS
1168 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1169 if (rc) {
1170 kfree(urbp);
1171 goto done;
1172 }
1da177e4 1173
cdfcbd2c
TB
1174 if (!dum_hcd->udev) {
1175 dum_hcd->udev = urb->dev;
1176 usb_get_dev(dum_hcd->udev);
1177 } else if (unlikely(dum_hcd->udev != urb->dev))
1178 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
1da177e4 1179
cdfcbd2c 1180 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
1da177e4 1181 urb->hcpriv = urbp;
18f2cbaa 1182 if (usb_pipetype(urb->pipe) == PIPE_CONTROL)
1da177e4
LT
1183 urb->error_count = 1; /* mark as a new urb */
1184
1185 /* kick the scheduler, it'll do the rest */
cdfcbd2c
TB
1186 if (!timer_pending(&dum_hcd->timer))
1187 mod_timer(&dum_hcd->timer, jiffies + 1);
1da177e4 1188
e9df41c5 1189 done:
cdfcbd2c 1190 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
e9df41c5 1191 return rc;
1da177e4
LT
1192}
1193
e9df41c5 1194static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1da177e4 1195{
cdfcbd2c 1196 struct dummy_hcd *dum_hcd;
391eca9d 1197 unsigned long flags;
e9df41c5 1198 int rc;
391eca9d
AS
1199
1200 /* giveback happens automatically in timer callback,
1201 * so make sure the callback happens */
cdfcbd2c
TB
1202 dum_hcd = hcd_to_dummy_hcd(hcd);
1203 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
e9df41c5
AS
1204
1205 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
cdfcbd2c
TB
1206 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1207 !list_empty(&dum_hcd->urbp_list))
1208 mod_timer(&dum_hcd->timer, jiffies);
e9df41c5 1209
cdfcbd2c 1210 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
e9df41c5 1211 return rc;
1da177e4
LT
1212}
1213
a04ce20d
SAS
1214static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
1215 u32 len)
1216{
1217 void *ubuf, *rbuf;
14fce33a 1218 struct urbp *urbp = urb->hcpriv;
a04ce20d 1219 int to_host;
14fce33a
SAS
1220 struct sg_mapping_iter *miter = &urbp->miter;
1221 u32 trans = 0;
1222 u32 this_sg;
1223 bool next_sg;
a04ce20d
SAS
1224
1225 to_host = usb_pipein(urb->pipe);
1226 rbuf = req->req.buf + req->req.actual;
a04ce20d 1227
14fce33a
SAS
1228 if (!urb->num_sgs) {
1229 ubuf = urb->transfer_buffer + urb->actual_length;
1230 if (to_host)
1231 memcpy(ubuf, rbuf, len);
1232 else
1233 memcpy(rbuf, ubuf, len);
1234 return len;
1235 }
1236
1237 if (!urbp->miter_started) {
1238 u32 flags = SG_MITER_ATOMIC;
1239
1240 if (to_host)
1241 flags |= SG_MITER_TO_SG;
1242 else
1243 flags |= SG_MITER_FROM_SG;
1244
1245 sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
1246 urbp->miter_started = 1;
1247 }
1248 next_sg = sg_miter_next(miter);
1249 if (next_sg == false) {
1250 WARN_ON_ONCE(1);
1251 return -EINVAL;
1252 }
1253 do {
1254 ubuf = miter->addr;
1255 this_sg = min_t(u32, len, miter->length);
1256 miter->consumed = this_sg;
1257 trans += this_sg;
1258
1259 if (to_host)
1260 memcpy(ubuf, rbuf, this_sg);
1261 else
1262 memcpy(rbuf, ubuf, this_sg);
1263 len -= this_sg;
1264
1265 if (!len)
1266 break;
1267 next_sg = sg_miter_next(miter);
1268 if (next_sg == false) {
1269 WARN_ON_ONCE(1);
1270 return -EINVAL;
1271 }
1272
1273 rbuf += this_sg;
1274 } while (1);
1275
1276 sg_miter_stop(miter);
1277 return trans;
a04ce20d
SAS
1278}
1279
1da177e4 1280/* transfer up to a frame's worth; caller must own lock */
a54c979f
SAS
1281static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb,
1282 struct dummy_ep *ep, int limit, int *status)
1da177e4 1283{
a54c979f 1284 struct dummy *dum = dum_hcd->dum;
1da177e4
LT
1285 struct dummy_request *req;
1286
1287top:
1288 /* if there's no request queued, the device is NAKing; return */
18f2cbaa 1289 list_for_each_entry(req, &ep->queue, queue) {
1da177e4
LT
1290 unsigned host_len, dev_len, len;
1291 int is_short, to_host;
1292 int rescan = 0;
1293
a54c979f
SAS
1294 if (dummy_ep_stream_en(dum_hcd, urb)) {
1295 if ((urb->stream_id != req->req.stream_id))
1296 continue;
1297 }
1298
1da177e4
LT
1299 /* 1..N packets of ep->ep.maxpacket each ... the last one
1300 * may be short (including zero length).
1301 *
1302 * writer can send a zlp explicitly (length 0) or implicitly
1303 * (length mod maxpacket zero, and 'zero' flag); they always
1304 * terminate reads.
1305 */
1306 host_len = urb->transfer_buffer_length - urb->actual_length;
1307 dev_len = req->req.length - req->req.actual;
18f2cbaa 1308 len = min(host_len, dev_len);
1da177e4
LT
1309
1310 /* FIXME update emulated data toggle too */
1311
18f2cbaa
SAS
1312 to_host = usb_pipein(urb->pipe);
1313 if (unlikely(len == 0))
1da177e4
LT
1314 is_short = 1;
1315 else {
1da177e4
LT
1316 /* not enough bandwidth left? */
1317 if (limit < ep->ep.maxpacket && limit < len)
1318 break;
18f2cbaa 1319 len = min_t(unsigned, len, limit);
1da177e4
LT
1320 if (len == 0)
1321 break;
1322
1323 /* use an extra pass for the final short packet */
1324 if (len > ep->ep.maxpacket) {
1325 rescan = 1;
1326 len -= (len % ep->ep.maxpacket);
1327 }
1328 is_short = (len % ep->ep.maxpacket) != 0;
1329
a04ce20d
SAS
1330 len = dummy_perform_transfer(urb, req, len);
1331
1da177e4 1332 ep->last_io = jiffies;
b1443ac0 1333 if ((int)len < 0) {
14fce33a
SAS
1334 req->req.status = len;
1335 } else {
1336 limit -= len;
1337 urb->actual_length += len;
1338 req->req.actual += len;
1339 }
1da177e4
LT
1340 }
1341
1342 /* short packets terminate, maybe with overflow/underflow.
1343 * it's only really an error to write too much.
1344 *
1345 * partially filling a buffer optionally blocks queue advances
1346 * (so completion handlers can clean up the queue) but we don't
b0d9efba 1347 * need to emulate such data-in-flight.
1da177e4
LT
1348 */
1349 if (is_short) {
1350 if (host_len == dev_len) {
1351 req->req.status = 0;
4d2f110c 1352 *status = 0;
1da177e4
LT
1353 } else if (to_host) {
1354 req->req.status = 0;
1355 if (dev_len > host_len)
4d2f110c 1356 *status = -EOVERFLOW;
1da177e4 1357 else
4d2f110c 1358 *status = 0;
1da177e4 1359 } else if (!to_host) {
4d2f110c 1360 *status = 0;
1da177e4
LT
1361 if (host_len > dev_len)
1362 req->req.status = -EOVERFLOW;
1363 else
1364 req->req.status = 0;
1365 }
1366
1367 /* many requests terminate without a short packet */
1368 } else {
1369 if (req->req.length == req->req.actual
1370 && !req->req.zero)
1371 req->req.status = 0;
1372 if (urb->transfer_buffer_length == urb->actual_length
1373 && !(urb->transfer_flags
4d2f110c
AS
1374 & URB_ZERO_PACKET))
1375 *status = 0;
1da177e4
LT
1376 }
1377
1378 /* device side completion --> continuable */
1379 if (req->req.status != -EINPROGRESS) {
18f2cbaa 1380 list_del_init(&req->queue);
1da177e4 1381
18f2cbaa 1382 spin_unlock(&dum->lock);
304f7e5e 1383 usb_gadget_giveback_request(&ep->ep, &req->req);
18f2cbaa 1384 spin_lock(&dum->lock);
1da177e4
LT
1385
1386 /* requests might have been unlinked... */
1387 rescan = 1;
1388 }
1389
1390 /* host side completion --> terminate */
4d2f110c 1391 if (*status != -EINPROGRESS)
1da177e4
LT
1392 break;
1393
1394 /* rescan to continue with any other queued i/o */
1395 if (rescan)
1396 goto top;
1397 }
1398 return limit;
1399}
1400
18f2cbaa 1401static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep)
1da177e4
LT
1402{
1403 int limit = ep->ep.maxpacket;
1404
1405 if (dum->gadget.speed == USB_SPEED_HIGH) {
1406 int tmp;
1407
1408 /* high bandwidth mode */
29cc8897 1409 tmp = usb_endpoint_maxp(ep->desc);
1da177e4
LT
1410 tmp = (tmp >> 11) & 0x03;
1411 tmp *= 8 /* applies to entire frame */;
1412 limit += limit * tmp;
1413 }
1cd8fd28 1414 if (dum->gadget.speed == USB_SPEED_SUPER) {
59f08e6d 1415 switch (usb_endpoint_type(ep->desc)) {
1cd8fd28
TB
1416 case USB_ENDPOINT_XFER_ISOC:
1417 /* Sec. 4.4.8.2 USB3.0 Spec */
1418 limit = 3 * 16 * 1024 * 8;
1419 break;
1420 case USB_ENDPOINT_XFER_INT:
1421 /* Sec. 4.4.7.2 USB3.0 Spec */
1422 limit = 3 * 1024 * 8;
1423 break;
1424 case USB_ENDPOINT_XFER_BULK:
1425 default:
1426 break;
1427 }
1428 }
1da177e4
LT
1429 return limit;
1430}
1431
cdfcbd2c 1432#define is_active(dum_hcd) ((dum_hcd->port_status & \
1da177e4
LT
1433 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1434 USB_PORT_STAT_SUSPEND)) \
1435 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1436
18f2cbaa 1437static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address)
1da177e4
LT
1438{
1439 int i;
1440
1cd8fd28
TB
1441 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1442 dum->ss_hcd : dum->hs_hcd)))
1da177e4
LT
1443 return NULL;
1444 if ((address & ~USB_DIR_IN) == 0)
18f2cbaa 1445 return &dum->ep[0];
1da177e4 1446 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
18f2cbaa 1447 struct dummy_ep *ep = &dum->ep[i];
1da177e4
LT
1448
1449 if (!ep->desc)
1450 continue;
1451 if (ep->desc->bEndpointAddress == address)
1452 return ep;
1453 }
1454 return NULL;
1455}
1456
1457#undef is_active
1458
1459#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1460#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1461#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1462#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1463#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1464#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1465
8be8a9d3
TB
1466
1467/**
1468 * handle_control_request() - handles all control transfers
1469 * @dum: pointer to dummy (the_controller)
1470 * @urb: the urb request to handle
1471 * @setup: pointer to the setup data for a USB device control
1472 * request
1473 * @status: pointer to request handling status
1474 *
1475 * Return 0 - if the request was handled
1476 * 1 - if the request wasn't handles
1477 * error code on error
1478 */
cdfcbd2c 1479static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
8be8a9d3
TB
1480 struct usb_ctrlrequest *setup,
1481 int *status)
1482{
1483 struct dummy_ep *ep2;
cdfcbd2c 1484 struct dummy *dum = dum_hcd->dum;
8be8a9d3
TB
1485 int ret_val = 1;
1486 unsigned w_index;
1487 unsigned w_value;
1488
1489 w_index = le16_to_cpu(setup->wIndex);
1490 w_value = le16_to_cpu(setup->wValue);
1491 switch (setup->bRequest) {
1492 case USB_REQ_SET_ADDRESS:
1493 if (setup->bRequestType != Dev_Request)
1494 break;
1495 dum->address = w_value;
1496 *status = 0;
1497 dev_dbg(udc_dev(dum), "set_address = %d\n",
1498 w_value);
1499 ret_val = 0;
1500 break;
1501 case USB_REQ_SET_FEATURE:
1502 if (setup->bRequestType == Dev_Request) {
1503 ret_val = 0;
1504 switch (w_value) {
1505 case USB_DEVICE_REMOTE_WAKEUP:
1506 break;
1507 case USB_DEVICE_B_HNP_ENABLE:
1508 dum->gadget.b_hnp_enable = 1;
1509 break;
1510 case USB_DEVICE_A_HNP_SUPPORT:
1511 dum->gadget.a_hnp_support = 1;
1512 break;
1513 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1514 dum->gadget.a_alt_hnp_support = 1;
1515 break;
1cd8fd28
TB
1516 case USB_DEVICE_U1_ENABLE:
1517 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1518 HCD_USB3)
1519 w_value = USB_DEV_STAT_U1_ENABLED;
1520 else
1521 ret_val = -EOPNOTSUPP;
1522 break;
1523 case USB_DEVICE_U2_ENABLE:
1524 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1525 HCD_USB3)
1526 w_value = USB_DEV_STAT_U2_ENABLED;
1527 else
1528 ret_val = -EOPNOTSUPP;
1529 break;
1530 case USB_DEVICE_LTM_ENABLE:
1531 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1532 HCD_USB3)
1533 w_value = USB_DEV_STAT_LTM_ENABLED;
1534 else
1535 ret_val = -EOPNOTSUPP;
1536 break;
8be8a9d3
TB
1537 default:
1538 ret_val = -EOPNOTSUPP;
1539 }
1540 if (ret_val == 0) {
1541 dum->devstatus |= (1 << w_value);
1542 *status = 0;
1543 }
1544 } else if (setup->bRequestType == Ep_Request) {
1545 /* endpoint halt */
1546 ep2 = find_endpoint(dum, w_index);
1547 if (!ep2 || ep2->ep.name == ep0name) {
1548 ret_val = -EOPNOTSUPP;
1549 break;
1550 }
1551 ep2->halted = 1;
1552 ret_val = 0;
1553 *status = 0;
1554 }
1555 break;
1556 case USB_REQ_CLEAR_FEATURE:
1557 if (setup->bRequestType == Dev_Request) {
1558 ret_val = 0;
1559 switch (w_value) {
1560 case USB_DEVICE_REMOTE_WAKEUP:
1561 w_value = USB_DEVICE_REMOTE_WAKEUP;
1562 break;
1cd8fd28
TB
1563 case USB_DEVICE_U1_ENABLE:
1564 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1565 HCD_USB3)
1566 w_value = USB_DEV_STAT_U1_ENABLED;
1567 else
1568 ret_val = -EOPNOTSUPP;
1569 break;
1570 case USB_DEVICE_U2_ENABLE:
1571 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1572 HCD_USB3)
1573 w_value = USB_DEV_STAT_U2_ENABLED;
1574 else
1575 ret_val = -EOPNOTSUPP;
1576 break;
1577 case USB_DEVICE_LTM_ENABLE:
1578 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1579 HCD_USB3)
1580 w_value = USB_DEV_STAT_LTM_ENABLED;
1581 else
1582 ret_val = -EOPNOTSUPP;
1583 break;
8be8a9d3
TB
1584 default:
1585 ret_val = -EOPNOTSUPP;
1586 break;
1587 }
1588 if (ret_val == 0) {
1589 dum->devstatus &= ~(1 << w_value);
1590 *status = 0;
1591 }
1592 } else if (setup->bRequestType == Ep_Request) {
1593 /* endpoint halt */
1594 ep2 = find_endpoint(dum, w_index);
1595 if (!ep2) {
1596 ret_val = -EOPNOTSUPP;
1597 break;
1598 }
1599 if (!ep2->wedged)
1600 ep2->halted = 0;
1601 ret_val = 0;
1602 *status = 0;
1603 }
1604 break;
1605 case USB_REQ_GET_STATUS:
1606 if (setup->bRequestType == Dev_InRequest
1607 || setup->bRequestType == Intf_InRequest
1608 || setup->bRequestType == Ep_InRequest) {
1609 char *buf;
1610 /*
1611 * device: remote wakeup, selfpowered
1612 * interface: nothing
1613 * endpoint: halt
1614 */
1615 buf = (char *)urb->transfer_buffer;
1616 if (urb->transfer_buffer_length > 0) {
1617 if (setup->bRequestType == Ep_InRequest) {
1618 ep2 = find_endpoint(dum, w_index);
1619 if (!ep2) {
1620 ret_val = -EOPNOTSUPP;
1621 break;
1622 }
1623 buf[0] = ep2->halted;
1624 } else if (setup->bRequestType ==
1625 Dev_InRequest) {
1626 buf[0] = (u8)dum->devstatus;
1627 } else
1628 buf[0] = 0;
1629 }
1630 if (urb->transfer_buffer_length > 1)
1631 buf[1] = 0;
1632 urb->actual_length = min_t(u32, 2,
1633 urb->transfer_buffer_length);
1634 ret_val = 0;
1635 *status = 0;
1636 }
1637 break;
1638 }
1639 return ret_val;
1640}
1641
1da177e4
LT
1642/* drive both sides of the transfers; looks like irq handlers to
1643 * both drivers except the callbacks aren't in_irq().
1644 */
cdfcbd2c 1645static void dummy_timer(unsigned long _dum_hcd)
1da177e4 1646{
cdfcbd2c
TB
1647 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1648 struct dummy *dum = dum_hcd->dum;
1da177e4
LT
1649 struct urbp *urbp, *tmp;
1650 unsigned long flags;
1651 int limit, total;
1652 int i;
1653
1654 /* simplistic model for one frame's bandwidth */
1655 switch (dum->gadget.speed) {
1656 case USB_SPEED_LOW:
1657 total = 8/*bytes*/ * 12/*packets*/;
1658 break;
1659 case USB_SPEED_FULL:
1660 total = 64/*bytes*/ * 19/*packets*/;
1661 break;
1662 case USB_SPEED_HIGH:
1663 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1664 break;
1cd8fd28
TB
1665 case USB_SPEED_SUPER:
1666 /* Bus speed is 500000 bytes/ms, so use a little less */
1667 total = 490000;
1668 break;
1da177e4 1669 default:
cdfcbd2c 1670 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
1da177e4
LT
1671 return;
1672 }
1673
1674 /* FIXME if HZ != 1000 this will probably misbehave ... */
1675
1676 /* look at each urb queued by the host side driver */
18f2cbaa 1677 spin_lock_irqsave(&dum->lock, flags);
1da177e4 1678
cdfcbd2c
TB
1679 if (!dum_hcd->udev) {
1680 dev_err(dummy_dev(dum_hcd),
1da177e4 1681 "timer fired with no URBs pending?\n");
18f2cbaa 1682 spin_unlock_irqrestore(&dum->lock, flags);
1da177e4
LT
1683 return;
1684 }
1685
1686 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
18f2cbaa 1687 if (!ep_name[i])
1da177e4 1688 break;
18f2cbaa 1689 dum->ep[i].already_seen = 0;
1da177e4
LT
1690 }
1691
1692restart:
cdfcbd2c 1693 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
1da177e4
LT
1694 struct urb *urb;
1695 struct dummy_request *req;
1696 u8 address;
1697 struct dummy_ep *ep = NULL;
1698 int type;
4d2f110c 1699 int status = -EINPROGRESS;
1da177e4
LT
1700
1701 urb = urbp->urb;
eb231054 1702 if (urb->unlinked)
1da177e4 1703 goto return_urb;
cdfcbd2c 1704 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
391eca9d 1705 continue;
18f2cbaa 1706 type = usb_pipetype(urb->pipe);
1da177e4
LT
1707
1708 /* used up this frame's non-periodic bandwidth?
1709 * FIXME there's infinite bandwidth for control and
1710 * periodic transfers ... unrealistic.
1711 */
1712 if (total <= 0 && type == PIPE_BULK)
1713 continue;
1714
1715 /* find the gadget's ep for this request (if configured) */
1716 address = usb_pipeendpoint (urb->pipe);
18f2cbaa 1717 if (usb_pipein(urb->pipe))
1da177e4
LT
1718 address |= USB_DIR_IN;
1719 ep = find_endpoint(dum, address);
1720 if (!ep) {
1721 /* set_configuration() disagreement */
cdfcbd2c 1722 dev_dbg(dummy_dev(dum_hcd),
1da177e4
LT
1723 "no ep configured for urb %p\n",
1724 urb);
4d2f110c 1725 status = -EPROTO;
1da177e4
LT
1726 goto return_urb;
1727 }
1728
1729 if (ep->already_seen)
1730 continue;
1731 ep->already_seen = 1;
18f2cbaa 1732 if (ep == &dum->ep[0] && urb->error_count) {
1da177e4
LT
1733 ep->setup_stage = 1; /* a new urb */
1734 urb->error_count = 0;
1735 }
1736 if (ep->halted && !ep->setup_stage) {
1737 /* NOTE: must not be iso! */
cdfcbd2c 1738 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
1da177e4 1739 ep->ep.name, urb);
4d2f110c 1740 status = -EPIPE;
1da177e4
LT
1741 goto return_urb;
1742 }
1743 /* FIXME make sure both ends agree on maxpacket */
1744
1745 /* handle control requests */
18f2cbaa 1746 if (ep == &dum->ep[0] && ep->setup_stage) {
1da177e4
LT
1747 struct usb_ctrlrequest setup;
1748 int value = 1;
1da177e4 1749
18f2cbaa 1750 setup = *(struct usb_ctrlrequest *) urb->setup_packet;
1da177e4 1751 /* paranoia, in case of stale queued data */
18f2cbaa
SAS
1752 list_for_each_entry(req, &ep->queue, queue) {
1753 list_del_init(&req->queue);
1da177e4 1754 req->req.status = -EOVERFLOW;
18f2cbaa 1755 dev_dbg(udc_dev(dum), "stale req = %p\n",
1da177e4
LT
1756 req);
1757
18f2cbaa 1758 spin_unlock(&dum->lock);
304f7e5e 1759 usb_gadget_giveback_request(&ep->ep, &req->req);
18f2cbaa 1760 spin_lock(&dum->lock);
1da177e4
LT
1761 ep->already_seen = 0;
1762 goto restart;
1763 }
1764
1765 /* gadget driver never sees set_address or operations
1766 * on standard feature flags. some hardware doesn't
1767 * even expose them.
1768 */
1769 ep->last_io = jiffies;
1770 ep->setup_stage = 0;
1771 ep->halted = 0;
1da177e4 1772
cdfcbd2c 1773 value = handle_control_request(dum_hcd, urb, &setup,
8be8a9d3 1774 &status);
1da177e4
LT
1775
1776 /* gadget driver handles all other requests. block
1777 * until setup() returns; no reentrancy issues etc.
1778 */
1779 if (value > 0) {
18f2cbaa
SAS
1780 spin_unlock(&dum->lock);
1781 value = dum->driver->setup(&dum->gadget,
1da177e4 1782 &setup);
18f2cbaa 1783 spin_lock(&dum->lock);
1da177e4
LT
1784
1785 if (value >= 0) {
1786 /* no delays (max 64KB data stage) */
1787 limit = 64*1024;
1788 goto treat_control_like_bulk;
1789 }
1790 /* error, see below */
1791 }
1792
1793 if (value < 0) {
1794 if (value != -EOPNOTSUPP)
18f2cbaa 1795 dev_dbg(udc_dev(dum),
1da177e4
LT
1796 "setup --> %d\n",
1797 value);
4d2f110c 1798 status = -EPIPE;
1da177e4
LT
1799 urb->actual_length = 0;
1800 }
1801
1802 goto return_urb;
1803 }
1804
1805 /* non-control requests */
1806 limit = total;
18f2cbaa 1807 switch (usb_pipetype(urb->pipe)) {
1da177e4
LT
1808 case PIPE_ISOCHRONOUS:
1809 /* FIXME is it urb->interval since the last xfer?
1810 * use urb->iso_frame_desc[i].
1811 * complete whether or not ep has requests queued.
1812 * report random errors, to debug drivers.
1813 */
18f2cbaa 1814 limit = max(limit, periodic_bytes(dum, ep));
4d2f110c 1815 status = -ENOSYS;
1da177e4
LT
1816 break;
1817
1818 case PIPE_INTERRUPT:
1819 /* FIXME is it urb->interval since the last xfer?
1820 * this almost certainly polls too fast.
1821 */
18f2cbaa 1822 limit = max(limit, periodic_bytes(dum, ep));
1da177e4
LT
1823 /* FALLTHROUGH */
1824
1da177e4 1825 default:
18f2cbaa 1826treat_control_like_bulk:
1da177e4 1827 ep->last_io = jiffies;
a54c979f 1828 total = transfer(dum_hcd, urb, ep, limit, &status);
1da177e4
LT
1829 break;
1830 }
1831
1832 /* incomplete transfer? */
4d2f110c 1833 if (status == -EINPROGRESS)
1da177e4
LT
1834 continue;
1835
1836return_urb:
18f2cbaa
SAS
1837 list_del(&urbp->urbp_list);
1838 kfree(urbp);
1da177e4
LT
1839 if (ep)
1840 ep->already_seen = ep->setup_stage = 0;
1841
cdfcbd2c 1842 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
18f2cbaa 1843 spin_unlock(&dum->lock);
cdfcbd2c 1844 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
18f2cbaa 1845 spin_lock(&dum->lock);
1da177e4
LT
1846
1847 goto restart;
1848 }
1849
cdfcbd2c
TB
1850 if (list_empty(&dum_hcd->urbp_list)) {
1851 usb_put_dev(dum_hcd->udev);
1852 dum_hcd->udev = NULL;
1853 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
391eca9d 1854 /* want a 1 msec delay here */
cdfcbd2c 1855 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
1da177e4
LT
1856 }
1857
18f2cbaa 1858 spin_unlock_irqrestore(&dum->lock, flags);
1da177e4
LT
1859}
1860
1861/*-------------------------------------------------------------------------*/
1862
1863#define PORT_C_MASK \
c2db8b5e
AS
1864 ((USB_PORT_STAT_C_CONNECTION \
1865 | USB_PORT_STAT_C_ENABLE \
1866 | USB_PORT_STAT_C_SUSPEND \
1867 | USB_PORT_STAT_C_OVERCURRENT \
1868 | USB_PORT_STAT_C_RESET) << 16)
1da177e4 1869
18f2cbaa 1870static int dummy_hub_status(struct usb_hcd *hcd, char *buf)
1da177e4 1871{
cdfcbd2c 1872 struct dummy_hcd *dum_hcd;
1da177e4 1873 unsigned long flags;
391eca9d 1874 int retval = 0;
1da177e4 1875
cdfcbd2c 1876 dum_hcd = hcd_to_dummy_hcd(hcd);
1da177e4 1877
cdfcbd2c 1878 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
541c7d43 1879 if (!HCD_HW_ACCESSIBLE(hcd))
391eca9d 1880 goto done;
f1c39fad 1881
cdfcbd2c
TB
1882 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1883 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1884 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1885 set_link_state(dum_hcd);
f1c39fad
AS
1886 }
1887
cdfcbd2c 1888 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
1da177e4 1889 *buf = (1 << 1);
cdfcbd2c
TB
1890 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1891 dum_hcd->port_status);
1da177e4 1892 retval = 1;
cdfcbd2c 1893 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
18f2cbaa 1894 usb_hcd_resume_root_hub(hcd);
1da177e4 1895 }
391eca9d 1896done:
cdfcbd2c 1897 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1da177e4
LT
1898 return retval;
1899}
1900
3b9c1c5b 1901/* usb 3.0 root hub device descriptor */
814cf212 1902static struct {
3b9c1c5b
SAS
1903 struct usb_bos_descriptor bos;
1904 struct usb_ss_cap_descriptor ss_cap;
1905} __packed usb3_bos_desc = {
1906
1907 .bos = {
1908 .bLength = USB_DT_BOS_SIZE,
1909 .bDescriptorType = USB_DT_BOS,
1910 .wTotalLength = cpu_to_le16(sizeof(usb3_bos_desc)),
1911 .bNumDeviceCaps = 1,
1912 },
1913 .ss_cap = {
1914 .bLength = USB_DT_USB_SS_CAP_SIZE,
1915 .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
1916 .bDevCapabilityType = USB_SS_CAP_TYPE,
1917 .wSpeedSupported = cpu_to_le16(USB_5GBPS_OPERATION),
1918 .bFunctionalitySupport = ilog2(USB_5GBPS_OPERATION),
1919 },
1920};
1921
1cd8fd28
TB
1922static inline void
1923ss_hub_descriptor(struct usb_hub_descriptor *desc)
1924{
1925 memset(desc, 0, sizeof *desc);
2569ffd5 1926 desc->bDescriptorType = USB_DT_SS_HUB;
1cd8fd28 1927 desc->bDescLength = 12;
d906d61c
SS
1928 desc->wHubCharacteristics = cpu_to_le16(
1929 HUB_CHAR_INDV_PORT_LPSM |
1930 HUB_CHAR_COMMON_OCPM);
1cd8fd28
TB
1931 desc->bNbrPorts = 1;
1932 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1933 desc->u.ss.DeviceRemovable = 0xffff;
1934}
1935
18f2cbaa 1936static inline void hub_descriptor(struct usb_hub_descriptor *desc)
1da177e4 1937{
18f2cbaa 1938 memset(desc, 0, sizeof *desc);
2569ffd5 1939 desc->bDescriptorType = USB_DT_HUB;
1da177e4 1940 desc->bDescLength = 9;
d906d61c
SS
1941 desc->wHubCharacteristics = cpu_to_le16(
1942 HUB_CHAR_INDV_PORT_LPSM |
1943 HUB_CHAR_COMMON_OCPM);
1da177e4 1944 desc->bNbrPorts = 1;
dbe79bbe
JY
1945 desc->u.hs.DeviceRemovable[0] = 0xff;
1946 desc->u.hs.DeviceRemovable[1] = 0xff;
1da177e4
LT
1947}
1948
18f2cbaa 1949static int dummy_hub_control(
1da177e4
LT
1950 struct usb_hcd *hcd,
1951 u16 typeReq,
1952 u16 wValue,
1953 u16 wIndex,
1954 char *buf,
1955 u16 wLength
1956) {
cdfcbd2c 1957 struct dummy_hcd *dum_hcd;
1da177e4
LT
1958 int retval = 0;
1959 unsigned long flags;
1960
541c7d43 1961 if (!HCD_HW_ACCESSIBLE(hcd))
391eca9d
AS
1962 return -ETIMEDOUT;
1963
cdfcbd2c
TB
1964 dum_hcd = hcd_to_dummy_hcd(hcd);
1965
1966 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1da177e4
LT
1967 switch (typeReq) {
1968 case ClearHubFeature:
1969 break;
1970 case ClearPortFeature:
1971 switch (wValue) {
1972 case USB_PORT_FEAT_SUSPEND:
1cd8fd28
TB
1973 if (hcd->speed == HCD_USB3) {
1974 dev_dbg(dummy_dev(dum_hcd),
1975 "USB_PORT_FEAT_SUSPEND req not "
1976 "supported for USB 3.0 roothub\n");
1977 goto error;
1978 }
cdfcbd2c 1979 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
1da177e4 1980 /* 20msec resume signaling */
cdfcbd2c
TB
1981 dum_hcd->resuming = 1;
1982 dum_hcd->re_timeout = jiffies +
f1c39fad 1983 msecs_to_jiffies(20);
1da177e4
LT
1984 }
1985 break;
1986 case USB_PORT_FEAT_POWER:
1cd8fd28
TB
1987 if (hcd->speed == HCD_USB3) {
1988 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1989 dev_dbg(dummy_dev(dum_hcd),
1990 "power-off\n");
1991 } else
1992 if (dum_hcd->port_status &
1993 USB_SS_PORT_STAT_POWER)
1994 dev_dbg(dummy_dev(dum_hcd),
1995 "power-off\n");
f1c39fad 1996 /* FALLS THROUGH */
1da177e4 1997 default:
cdfcbd2c
TB
1998 dum_hcd->port_status &= ~(1 << wValue);
1999 set_link_state(dum_hcd);
1da177e4
LT
2000 }
2001 break;
2002 case GetHubDescriptor:
1cd8fd28
TB
2003 if (hcd->speed == HCD_USB3 &&
2004 (wLength < USB_DT_SS_HUB_SIZE ||
2005 wValue != (USB_DT_SS_HUB << 8))) {
2006 dev_dbg(dummy_dev(dum_hcd),
2007 "Wrong hub descriptor type for "
2008 "USB 3.0 roothub.\n");
2009 goto error;
2010 }
2011 if (hcd->speed == HCD_USB3)
2012 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
2013 else
2014 hub_descriptor((struct usb_hub_descriptor *) buf);
1da177e4 2015 break;
3b9c1c5b
SAS
2016
2017 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
2018 if (hcd->speed != HCD_USB3)
2019 goto error;
2020
2021 if ((wValue >> 8) != USB_DT_BOS)
2022 goto error;
2023
2024 memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc));
2025 retval = sizeof(usb3_bos_desc);
2026 break;
2027
1da177e4 2028 case GetHubStatus:
18f2cbaa 2029 *(__le32 *) buf = cpu_to_le32(0);
1da177e4
LT
2030 break;
2031 case GetPortStatus:
2032 if (wIndex != 1)
2033 retval = -EPIPE;
2034
2035 /* whoever resets or resumes must GetPortStatus to
2036 * complete it!!
2037 */
cdfcbd2c
TB
2038 if (dum_hcd->resuming &&
2039 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2040 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
2041 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1da177e4 2042 }
cdfcbd2c
TB
2043 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
2044 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2045 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
2046 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
2047 if (dum_hcd->dum->pullup) {
2048 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
1cd8fd28
TB
2049
2050 if (hcd->speed < HCD_USB3) {
2051 switch (dum_hcd->dum->gadget.speed) {
2052 case USB_SPEED_HIGH:
2053 dum_hcd->port_status |=
2054 USB_PORT_STAT_HIGH_SPEED;
2055 break;
2056 case USB_SPEED_LOW:
2057 dum_hcd->dum->gadget.ep0->
2058 maxpacket = 8;
2059 dum_hcd->port_status |=
2060 USB_PORT_STAT_LOW_SPEED;
2061 break;
2062 default:
2063 dum_hcd->dum->gadget.speed =
2064 USB_SPEED_FULL;
2065 break;
2066 }
1da177e4
LT
2067 }
2068 }
2069 }
cdfcbd2c 2070 set_link_state(dum_hcd);
18f2cbaa
SAS
2071 ((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status);
2072 ((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16);
1da177e4
LT
2073 break;
2074 case SetHubFeature:
2075 retval = -EPIPE;
2076 break;
2077 case SetPortFeature:
2078 switch (wValue) {
1cd8fd28
TB
2079 case USB_PORT_FEAT_LINK_STATE:
2080 if (hcd->speed != HCD_USB3) {
2081 dev_dbg(dummy_dev(dum_hcd),
2082 "USB_PORT_FEAT_LINK_STATE req not "
2083 "supported for USB 2.0 roothub\n");
2084 goto error;
2085 }
2086 /*
2087 * Since this is dummy we don't have an actual link so
2088 * there is nothing to do for the SET_LINK_STATE cmd
2089 */
2090 break;
2091 case USB_PORT_FEAT_U1_TIMEOUT:
2092 case USB_PORT_FEAT_U2_TIMEOUT:
2093 /* TODO: add suspend/resume support! */
2094 if (hcd->speed != HCD_USB3) {
2095 dev_dbg(dummy_dev(dum_hcd),
2096 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
2097 "supported for USB 2.0 roothub\n");
2098 goto error;
2099 }
2100 break;
1da177e4 2101 case USB_PORT_FEAT_SUSPEND:
1cd8fd28
TB
2102 /* Applicable only for USB2.0 hub */
2103 if (hcd->speed == HCD_USB3) {
2104 dev_dbg(dummy_dev(dum_hcd),
2105 "USB_PORT_FEAT_SUSPEND req not "
2106 "supported for USB 3.0 roothub\n");
2107 goto error;
2108 }
cdfcbd2c
TB
2109 if (dum_hcd->active) {
2110 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
f1c39fad
AS
2111
2112 /* HNP would happen here; for now we
2113 * assume b_bus_req is always true.
2114 */
cdfcbd2c 2115 set_link_state(dum_hcd);
f1c39fad 2116 if (((1 << USB_DEVICE_B_HNP_ENABLE)
cdfcbd2c
TB
2117 & dum_hcd->dum->devstatus) != 0)
2118 dev_dbg(dummy_dev(dum_hcd),
5742b0c9 2119 "no HNP yet!\n");
1da177e4
LT
2120 }
2121 break;
f1c39fad 2122 case USB_PORT_FEAT_POWER:
1cd8fd28
TB
2123 if (hcd->speed == HCD_USB3)
2124 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
2125 else
2126 dum_hcd->port_status |= USB_PORT_STAT_POWER;
cdfcbd2c 2127 set_link_state(dum_hcd);
f1c39fad 2128 break;
1cd8fd28
TB
2129 case USB_PORT_FEAT_BH_PORT_RESET:
2130 /* Applicable only for USB3.0 hub */
2131 if (hcd->speed != HCD_USB3) {
2132 dev_dbg(dummy_dev(dum_hcd),
2133 "USB_PORT_FEAT_BH_PORT_RESET req not "
2134 "supported for USB 2.0 roothub\n");
2135 goto error;
2136 }
2137 /* FALLS THROUGH */
1da177e4 2138 case USB_PORT_FEAT_RESET:
f1c39fad 2139 /* if it's already enabled, disable */
1cd8fd28
TB
2140 if (hcd->speed == HCD_USB3) {
2141 dum_hcd->port_status = 0;
2142 dum_hcd->port_status =
2143 (USB_SS_PORT_STAT_POWER |
2144 USB_PORT_STAT_CONNECTION |
2145 USB_PORT_STAT_RESET);
2146 } else
2147 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
f1c39fad
AS
2148 | USB_PORT_STAT_LOW_SPEED
2149 | USB_PORT_STAT_HIGH_SPEED);
cdfcbd2c
TB
2150 /*
2151 * We want to reset device status. All but the
2152 * Self powered feature
2153 */
2154 dum_hcd->dum->devstatus &=
2155 (1 << USB_DEVICE_SELF_POWERED);
1cd8fd28
TB
2156 /*
2157 * FIXME USB3.0: what is the correct reset signaling
2158 * interval? Is it still 50msec as for HS?
2159 */
cdfcbd2c 2160 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
f1c39fad 2161 /* FALLS THROUGH */
1da177e4 2162 default:
1cd8fd28
TB
2163 if (hcd->speed == HCD_USB3) {
2164 if ((dum_hcd->port_status &
2165 USB_SS_PORT_STAT_POWER) != 0) {
2166 dum_hcd->port_status |= (1 << wValue);
2167 set_link_state(dum_hcd);
2168 }
2169 } else
2170 if ((dum_hcd->port_status &
2171 USB_PORT_STAT_POWER) != 0) {
2172 dum_hcd->port_status |= (1 << wValue);
2173 set_link_state(dum_hcd);
2174 }
2175 }
2176 break;
2177 case GetPortErrorCount:
2178 if (hcd->speed != HCD_USB3) {
2179 dev_dbg(dummy_dev(dum_hcd),
2180 "GetPortErrorCount req not "
2181 "supported for USB 2.0 roothub\n");
2182 goto error;
2183 }
2184 /* We'll always return 0 since this is a dummy hub */
2185 *(__le32 *) buf = cpu_to_le32(0);
2186 break;
2187 case SetHubDepth:
2188 if (hcd->speed != HCD_USB3) {
2189 dev_dbg(dummy_dev(dum_hcd),
2190 "SetHubDepth req not supported for "
2191 "USB 2.0 roothub\n");
2192 goto error;
1da177e4
LT
2193 }
2194 break;
1da177e4 2195 default:
cdfcbd2c 2196 dev_dbg(dummy_dev(dum_hcd),
1da177e4
LT
2197 "hub control req%04x v%04x i%04x l%d\n",
2198 typeReq, wValue, wIndex, wLength);
1cd8fd28 2199error:
1da177e4
LT
2200 /* "protocol stall" on error */
2201 retval = -EPIPE;
2202 }
cdfcbd2c 2203 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
685eb93f 2204
cdfcbd2c 2205 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
18f2cbaa 2206 usb_hcd_poll_rh_status(hcd);
1da177e4
LT
2207 return retval;
2208}
2209
18f2cbaa 2210static int dummy_bus_suspend(struct usb_hcd *hcd)
391eca9d 2211{
cdfcbd2c 2212 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
391eca9d 2213
18f2cbaa 2214 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
3cf0a22e 2215
cdfcbd2c
TB
2216 spin_lock_irq(&dum_hcd->dum->lock);
2217 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2218 set_link_state(dum_hcd);
3cf0a22e 2219 hcd->state = HC_STATE_SUSPENDED;
cdfcbd2c 2220 spin_unlock_irq(&dum_hcd->dum->lock);
391eca9d
AS
2221 return 0;
2222}
2223
18f2cbaa 2224static int dummy_bus_resume(struct usb_hcd *hcd)
391eca9d 2225{
cdfcbd2c 2226 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
3cf0a22e
AS
2227 int rc = 0;
2228
18f2cbaa 2229 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
391eca9d 2230
cdfcbd2c 2231 spin_lock_irq(&dum_hcd->dum->lock);
541c7d43 2232 if (!HCD_HW_ACCESSIBLE(hcd)) {
cfa59dab 2233 rc = -ESHUTDOWN;
3cf0a22e 2234 } else {
cdfcbd2c
TB
2235 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2236 set_link_state(dum_hcd);
2237 if (!list_empty(&dum_hcd->urbp_list))
2238 mod_timer(&dum_hcd->timer, jiffies);
3cf0a22e
AS
2239 hcd->state = HC_STATE_RUNNING;
2240 }
cdfcbd2c 2241 spin_unlock_irq(&dum_hcd->dum->lock);
3cf0a22e 2242 return rc;
391eca9d 2243}
1da177e4
LT
2244
2245/*-------------------------------------------------------------------------*/
2246
18f2cbaa 2247static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb)
1da177e4 2248{
18f2cbaa 2249 int ep = usb_pipeendpoint(urb->pipe);
1da177e4 2250
18f2cbaa 2251 return snprintf(buf, size,
1da177e4
LT
2252 "urb/%p %s ep%d%s%s len %d/%d\n",
2253 urb,
2254 ({ char *s;
18f2cbaa
SAS
2255 switch (urb->dev->speed) {
2256 case USB_SPEED_LOW:
7c884fe4
TB
2257 s = "ls";
2258 break;
18f2cbaa 2259 case USB_SPEED_FULL:
7c884fe4
TB
2260 s = "fs";
2261 break;
18f2cbaa 2262 case USB_SPEED_HIGH:
7c884fe4
TB
2263 s = "hs";
2264 break;
18f2cbaa 2265 case USB_SPEED_SUPER:
1cd8fd28
TB
2266 s = "ss";
2267 break;
18f2cbaa 2268 default:
7c884fe4
TB
2269 s = "?";
2270 break;
2b84f92b 2271 } s; }),
18f2cbaa 2272 ep, ep ? (usb_pipein(urb->pipe) ? "in" : "out") : "",
1da177e4 2273 ({ char *s; \
18f2cbaa
SAS
2274 switch (usb_pipetype(urb->pipe)) { \
2275 case PIPE_CONTROL: \
7c884fe4
TB
2276 s = ""; \
2277 break; \
18f2cbaa 2278 case PIPE_BULK: \
7c884fe4
TB
2279 s = "-bulk"; \
2280 break; \
18f2cbaa 2281 case PIPE_INTERRUPT: \
7c884fe4
TB
2282 s = "-int"; \
2283 break; \
18f2cbaa 2284 default: \
7c884fe4
TB
2285 s = "-iso"; \
2286 break; \
2b84f92b 2287 } s; }),
1da177e4
LT
2288 urb->actual_length, urb->transfer_buffer_length);
2289}
2290
ce26bd23 2291static ssize_t urbs_show(struct device *dev, struct device_attribute *attr,
18f2cbaa 2292 char *buf)
1da177e4 2293{
18f2cbaa 2294 struct usb_hcd *hcd = dev_get_drvdata(dev);
cdfcbd2c 2295 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
1da177e4
LT
2296 struct urbp *urbp;
2297 size_t size = 0;
2298 unsigned long flags;
2299
cdfcbd2c
TB
2300 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2301 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
1da177e4
LT
2302 size_t temp;
2303
18f2cbaa 2304 temp = show_urb(buf, PAGE_SIZE - size, urbp->urb);
1da177e4
LT
2305 buf += temp;
2306 size += temp;
2307 }
cdfcbd2c 2308 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1da177e4
LT
2309
2310 return size;
2311}
ce26bd23 2312static DEVICE_ATTR_RO(urbs);
1da177e4 2313
1cd8fd28
TB
2314static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2315{
2316 init_timer(&dum_hcd->timer);
2317 dum_hcd->timer.function = dummy_timer;
2318 dum_hcd->timer.data = (unsigned long)dum_hcd;
2319 dum_hcd->rh_state = DUMMY_RH_RUNNING;
a54c979f 2320 dum_hcd->stream_en_ep = 0;
1cd8fd28
TB
2321 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2322 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2323 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2324 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2325#ifdef CONFIG_USB_OTG
2326 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2327#endif
2328 return 0;
2329
2330 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2331 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2332}
2333
cdfcbd2c 2334static int dummy_start(struct usb_hcd *hcd)
1da177e4 2335{
cdfcbd2c 2336 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
1da177e4
LT
2337
2338 /*
2339 * MASTER side init ... we emulate a root hub that'll only ever
2340 * talk to one device (the slave side). Also appears in sysfs,
2341 * just like more familiar pci-based HCDs.
2342 */
1cd8fd28
TB
2343 if (!usb_hcd_is_primary_hcd(hcd))
2344 return dummy_start_ss(dum_hcd);
2345
cdfcbd2c
TB
2346 spin_lock_init(&dum_hcd->dum->lock);
2347 init_timer(&dum_hcd->timer);
2348 dum_hcd->timer.function = dummy_timer;
2349 dum_hcd->timer.data = (unsigned long)dum_hcd;
2350 dum_hcd->rh_state = DUMMY_RH_RUNNING;
1da177e4 2351
cdfcbd2c 2352 INIT_LIST_HEAD(&dum_hcd->urbp_list);
1da177e4 2353
caf29f62 2354 hcd->power_budget = POWER_BUDGET;
1da177e4 2355 hcd->state = HC_STATE_RUNNING;
685eb93f 2356 hcd->uses_new_polling = 1;
1da177e4 2357
5742b0c9
AS
2358#ifdef CONFIG_USB_OTG
2359 hcd->self.otg_port = 1;
2360#endif
2361
1da177e4 2362 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
cdfcbd2c 2363 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
1da177e4
LT
2364}
2365
18f2cbaa 2366static void dummy_stop(struct usb_hcd *hcd)
1da177e4
LT
2367{
2368 struct dummy *dum;
2369
18f2cbaa 2370 dum = hcd_to_dummy_hcd(hcd)->dum;
cdfcbd2c 2371 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
cdfcbd2c 2372 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
1da177e4
LT
2373}
2374
2375/*-------------------------------------------------------------------------*/
2376
18f2cbaa 2377static int dummy_h_get_frame(struct usb_hcd *hcd)
1da177e4 2378{
18f2cbaa 2379 return dummy_g_get_frame(NULL);
1da177e4
LT
2380}
2381
cdfcbd2c
TB
2382static int dummy_setup(struct usb_hcd *hcd)
2383{
b100a2f3
SAS
2384 struct dummy *dum;
2385
2386 dum = *((void **)dev_get_platdata(hcd->self.controller));
14fce33a 2387 hcd->self.sg_tablesize = ~0;
cdfcbd2c 2388 if (usb_hcd_is_primary_hcd(hcd)) {
b100a2f3
SAS
2389 dum->hs_hcd = hcd_to_dummy_hcd(hcd);
2390 dum->hs_hcd->dum = dum;
1cd8fd28
TB
2391 /*
2392 * Mark the first roothub as being USB 2.0.
2393 * The USB 3.0 roothub will be registered later by
2394 * dummy_hcd_probe()
2395 */
cdfcbd2c
TB
2396 hcd->speed = HCD_USB2;
2397 hcd->self.root_hub->speed = USB_SPEED_HIGH;
1cd8fd28 2398 } else {
b100a2f3
SAS
2399 dum->ss_hcd = hcd_to_dummy_hcd(hcd);
2400 dum->ss_hcd->dum = dum;
1cd8fd28
TB
2401 hcd->speed = HCD_USB3;
2402 hcd->self.root_hub->speed = USB_SPEED_SUPER;
cdfcbd2c
TB
2403 }
2404 return 0;
2405}
2406
1cd8fd28 2407/* Change a group of bulk endpoints to support multiple stream IDs */
d81f3e4f 2408static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
1cd8fd28
TB
2409 struct usb_host_endpoint **eps, unsigned int num_eps,
2410 unsigned int num_streams, gfp_t mem_flags)
2411{
a54c979f
SAS
2412 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2413 unsigned long flags;
2414 int max_stream;
2415 int ret_streams = num_streams;
2416 unsigned int index;
2417 unsigned int i;
2418
2419 if (!num_eps)
2420 return -EINVAL;
2421
2422 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2423 for (i = 0; i < num_eps; i++) {
2424 index = dummy_get_ep_idx(&eps[i]->desc);
2425 if ((1 << index) & dum_hcd->stream_en_ep) {
2426 ret_streams = -EINVAL;
2427 goto out;
2428 }
2429 max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp);
2430 if (!max_stream) {
2431 ret_streams = -EINVAL;
2432 goto out;
2433 }
2434 if (max_stream < ret_streams) {
2435 dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u "
2436 "stream IDs.\n",
2437 eps[i]->desc.bEndpointAddress,
2438 max_stream);
2439 ret_streams = max_stream;
2440 }
2441 }
2442
2443 for (i = 0; i < num_eps; i++) {
2444 index = dummy_get_ep_idx(&eps[i]->desc);
2445 dum_hcd->stream_en_ep |= 1 << index;
2446 set_max_streams_for_pipe(dum_hcd,
2447 usb_endpoint_num(&eps[i]->desc), ret_streams);
2448 }
2449out:
2450 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2451 return ret_streams;
1cd8fd28
TB
2452}
2453
2454/* Reverts a group of bulk endpoints back to not using stream IDs. */
d81f3e4f 2455static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
1cd8fd28
TB
2456 struct usb_host_endpoint **eps, unsigned int num_eps,
2457 gfp_t mem_flags)
2458{
a54c979f
SAS
2459 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2460 unsigned long flags;
2461 int ret;
2462 unsigned int index;
2463 unsigned int i;
2464
2465 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2466 for (i = 0; i < num_eps; i++) {
2467 index = dummy_get_ep_idx(&eps[i]->desc);
2468 if (!((1 << index) & dum_hcd->stream_en_ep)) {
2469 ret = -EINVAL;
2470 goto out;
2471 }
2472 }
2473
2474 for (i = 0; i < num_eps; i++) {
2475 index = dummy_get_ep_idx(&eps[i]->desc);
2476 dum_hcd->stream_en_ep &= ~(1 << index);
2477 set_max_streams_for_pipe(dum_hcd,
2478 usb_endpoint_num(&eps[i]->desc), 0);
2479 }
2480 ret = 0;
2481out:
2482 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2483 return ret;
1cd8fd28
TB
2484}
2485
2486static struct hc_driver dummy_hcd = {
1da177e4
LT
2487 .description = (char *) driver_name,
2488 .product_desc = "Dummy host controller",
cdfcbd2c 2489 .hcd_priv_size = sizeof(struct dummy_hcd),
1da177e4 2490
1cd8fd28 2491 .flags = HCD_USB3 | HCD_SHARED,
1da177e4 2492
cdfcbd2c 2493 .reset = dummy_setup,
1da177e4
LT
2494 .start = dummy_start,
2495 .stop = dummy_stop,
2496
18f2cbaa
SAS
2497 .urb_enqueue = dummy_urb_enqueue,
2498 .urb_dequeue = dummy_urb_dequeue,
1da177e4 2499
18f2cbaa 2500 .get_frame_number = dummy_h_get_frame,
1da177e4 2501
18f2cbaa
SAS
2502 .hub_status_data = dummy_hub_status,
2503 .hub_control = dummy_hub_control,
0c0382e3
AS
2504 .bus_suspend = dummy_bus_suspend,
2505 .bus_resume = dummy_bus_resume,
1cd8fd28
TB
2506
2507 .alloc_streams = dummy_alloc_streams,
2508 .free_streams = dummy_free_streams,
1da177e4
LT
2509};
2510
8364d6b0 2511static int dummy_hcd_probe(struct platform_device *pdev)
1da177e4 2512{
b100a2f3 2513 struct dummy *dum;
cdfcbd2c 2514 struct usb_hcd *hs_hcd;
1cd8fd28 2515 struct usb_hcd *ss_hcd;
1da177e4
LT
2516 int retval;
2517
8364d6b0 2518 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
b100a2f3 2519 dum = *((void **)dev_get_platdata(&pdev->dev));
1da177e4 2520
1cd8fd28
TB
2521 if (!mod_data.is_super_speed)
2522 dummy_hcd.flags = HCD_USB2;
cdfcbd2c
TB
2523 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2524 if (!hs_hcd)
1da177e4 2525 return -ENOMEM;
cdfcbd2c 2526 hs_hcd->has_tt = 1;
1da177e4 2527
cdfcbd2c 2528 retval = usb_add_hcd(hs_hcd, 0, 0);
1b68a4ca
SAS
2529 if (retval)
2530 goto put_usb2_hcd;
1cd8fd28
TB
2531
2532 if (mod_data.is_super_speed) {
2533 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2534 dev_name(&pdev->dev), hs_hcd);
2535 if (!ss_hcd) {
2536 retval = -ENOMEM;
2537 goto dealloc_usb2_hcd;
2538 }
2539
2540 retval = usb_add_hcd(ss_hcd, 0, 0);
2541 if (retval)
2542 goto put_usb3_hcd;
2543 }
2544 return 0;
2545
2546put_usb3_hcd:
2547 usb_put_hcd(ss_hcd);
2548dealloc_usb2_hcd:
1b68a4ca
SAS
2549 usb_remove_hcd(hs_hcd);
2550put_usb2_hcd:
1cd8fd28 2551 usb_put_hcd(hs_hcd);
b100a2f3 2552 dum->hs_hcd = dum->ss_hcd = NULL;
1da177e4
LT
2553 return retval;
2554}
2555
cdfcbd2c 2556static int dummy_hcd_remove(struct platform_device *pdev)
1da177e4 2557{
cdfcbd2c
TB
2558 struct dummy *dum;
2559
18f2cbaa 2560 dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum;
1cd8fd28
TB
2561
2562 if (dum->ss_hcd) {
2563 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2564 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2565 }
2566
cdfcbd2c
TB
2567 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2568 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
1cd8fd28 2569
b100a2f3
SAS
2570 dum->hs_hcd = NULL;
2571 dum->ss_hcd = NULL;
1da177e4 2572
d9b76251 2573 return 0;
1da177e4
LT
2574}
2575
18f2cbaa 2576static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state)
391eca9d
AS
2577{
2578 struct usb_hcd *hcd;
cdfcbd2c 2579 struct dummy_hcd *dum_hcd;
3cf0a22e 2580 int rc = 0;
391eca9d 2581
18f2cbaa 2582 dev_dbg(&pdev->dev, "%s\n", __func__);
391eca9d 2583
18f2cbaa 2584 hcd = platform_get_drvdata(pdev);
cdfcbd2c
TB
2585 dum_hcd = hcd_to_dummy_hcd(hcd);
2586 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
3cf0a22e
AS
2587 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2588 rc = -EBUSY;
2589 } else
2590 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2591 return rc;
391eca9d
AS
2592}
2593
18f2cbaa 2594static int dummy_hcd_resume(struct platform_device *pdev)
391eca9d
AS
2595{
2596 struct usb_hcd *hcd;
2597
18f2cbaa 2598 dev_dbg(&pdev->dev, "%s\n", __func__);
391eca9d 2599
18f2cbaa 2600 hcd = platform_get_drvdata(pdev);
3cf0a22e 2601 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
18f2cbaa 2602 usb_hcd_poll_rh_status(hcd);
391eca9d
AS
2603 return 0;
2604}
2605
3ae5eaec 2606static struct platform_driver dummy_hcd_driver = {
d9b76251
AS
2607 .probe = dummy_hcd_probe,
2608 .remove = dummy_hcd_remove,
391eca9d
AS
2609 .suspend = dummy_hcd_suspend,
2610 .resume = dummy_hcd_resume,
3ae5eaec
RK
2611 .driver = {
2612 .name = (char *) driver_name,
3ae5eaec 2613 },
d9b76251 2614};
1da177e4 2615
d9b76251 2616/*-------------------------------------------------------------------------*/
b100a2f3 2617#define MAX_NUM_UDC 2
b2113136
SAS
2618static struct platform_device *the_udc_pdev[MAX_NUM_UDC];
2619static struct platform_device *the_hcd_pdev[MAX_NUM_UDC];
1da177e4 2620
18f2cbaa 2621static int __init init(void)
1da177e4 2622{
a89a2cd3 2623 int retval = -ENOMEM;
c7a1db45 2624 int i;
b100a2f3 2625 struct dummy *dum[MAX_NUM_UDC];
1da177e4 2626
18f2cbaa 2627 if (usb_disabled())
1da177e4 2628 return -ENODEV;
d9b76251 2629
7eca4c5a
TB
2630 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2631 return -EINVAL;
2632
c7a1db45 2633 if (mod_data.num < 1 || mod_data.num > MAX_NUM_UDC) {
fa84acf0 2634 pr_err("Number of emulated UDC must be in range of 1...%d\n",
c7a1db45
SAS
2635 MAX_NUM_UDC);
2636 return -EINVAL;
2637 }
b100a2f3 2638
c7a1db45
SAS
2639 for (i = 0; i < mod_data.num; i++) {
2640 the_hcd_pdev[i] = platform_device_alloc(driver_name, i);
2641 if (!the_hcd_pdev[i]) {
2642 i--;
2643 while (i >= 0)
2644 platform_device_put(the_hcd_pdev[i--]);
2645 return retval;
2646 }
2647 }
2648 for (i = 0; i < mod_data.num; i++) {
2649 the_udc_pdev[i] = platform_device_alloc(gadget_name, i);
2650 if (!the_udc_pdev[i]) {
2651 i--;
2652 while (i >= 0)
2653 platform_device_put(the_udc_pdev[i--]);
2654 goto err_alloc_udc;
2655 }
2656 }
b100a2f3
SAS
2657 for (i = 0; i < mod_data.num; i++) {
2658 dum[i] = kzalloc(sizeof(struct dummy), GFP_KERNEL);
481d3042
WY
2659 if (!dum[i]) {
2660 retval = -ENOMEM;
b100a2f3 2661 goto err_add_pdata;
481d3042 2662 }
b100a2f3
SAS
2663 retval = platform_device_add_data(the_hcd_pdev[i], &dum[i],
2664 sizeof(void *));
2665 if (retval)
2666 goto err_add_pdata;
2667 retval = platform_device_add_data(the_udc_pdev[i], &dum[i],
2668 sizeof(void *));
2669 if (retval)
2670 goto err_add_pdata;
2671 }
d9b76251 2672
a89a2cd3
AS
2673 retval = platform_driver_register(&dummy_hcd_driver);
2674 if (retval < 0)
b100a2f3 2675 goto err_add_pdata;
a89a2cd3 2676 retval = platform_driver_register(&dummy_udc_driver);
d9b76251
AS
2677 if (retval < 0)
2678 goto err_register_udc_driver;
2679
c7a1db45
SAS
2680 for (i = 0; i < mod_data.num; i++) {
2681 retval = platform_device_add(the_hcd_pdev[i]);
2682 if (retval < 0) {
2683 i--;
2684 while (i >= 0)
2685 platform_device_del(the_hcd_pdev[i--]);
2686 goto err_add_hcd;
2687 }
2688 }
b100a2f3
SAS
2689 for (i = 0; i < mod_data.num; i++) {
2690 if (!dum[i]->hs_hcd ||
2691 (!dum[i]->ss_hcd && mod_data.is_super_speed)) {
2692 /*
2693 * The hcd was added successfully but its probe
2694 * function failed for some reason.
2695 */
2696 retval = -EINVAL;
2697 goto err_add_udc;
2698 }
865835fa 2699 }
c7a1db45 2700
c7a1db45
SAS
2701 for (i = 0; i < mod_data.num; i++) {
2702 retval = platform_device_add(the_udc_pdev[i]);
2703 if (retval < 0) {
2704 i--;
2705 while (i >= 0)
2706 platform_device_del(the_udc_pdev[i]);
2707 goto err_add_udc;
2708 }
2709 }
2710
2711 for (i = 0; i < mod_data.num; i++) {
2712 if (!platform_get_drvdata(the_udc_pdev[i])) {
2713 /*
2714 * The udc was added successfully but its probe
2715 * function failed for some reason.
2716 */
2717 retval = -EINVAL;
2718 goto err_probe_udc;
2719 }
865835fa 2720 }
d9b76251
AS
2721 return retval;
2722
865835fa 2723err_probe_udc:
c7a1db45
SAS
2724 for (i = 0; i < mod_data.num; i++)
2725 platform_device_del(the_udc_pdev[i]);
a89a2cd3 2726err_add_udc:
c7a1db45
SAS
2727 for (i = 0; i < mod_data.num; i++)
2728 platform_device_del(the_hcd_pdev[i]);
a89a2cd3
AS
2729err_add_hcd:
2730 platform_driver_unregister(&dummy_udc_driver);
d9b76251 2731err_register_udc_driver:
a89a2cd3 2732 platform_driver_unregister(&dummy_hcd_driver);
b100a2f3
SAS
2733err_add_pdata:
2734 for (i = 0; i < mod_data.num; i++)
2735 kfree(dum[i]);
c7a1db45
SAS
2736 for (i = 0; i < mod_data.num; i++)
2737 platform_device_put(the_udc_pdev[i]);
a89a2cd3 2738err_alloc_udc:
c7a1db45
SAS
2739 for (i = 0; i < mod_data.num; i++)
2740 platform_device_put(the_hcd_pdev[i]);
1da177e4
LT
2741 return retval;
2742}
18f2cbaa 2743module_init(init);
1da177e4 2744
18f2cbaa 2745static void __exit cleanup(void)
1da177e4 2746{
c7a1db45
SAS
2747 int i;
2748
2749 for (i = 0; i < mod_data.num; i++) {
b100a2f3
SAS
2750 struct dummy *dum;
2751
2752 dum = *((void **)dev_get_platdata(&the_udc_pdev[i]->dev));
2753
c7a1db45
SAS
2754 platform_device_unregister(the_udc_pdev[i]);
2755 platform_device_unregister(the_hcd_pdev[i]);
b100a2f3 2756 kfree(dum);
c7a1db45 2757 }
a89a2cd3
AS
2758 platform_driver_unregister(&dummy_udc_driver);
2759 platform_driver_unregister(&dummy_hcd_driver);
1da177e4 2760}
18f2cbaa 2761module_exit(cleanup);
This page took 1.146658 seconds and 5 git commands to generate.