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