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