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