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