blob: 61e893ac25454d3250c83479f247e18f826bef6d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/kernel.h>
30#include <linux/delay.h>
31#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/errno.h>
34#include <linux/init.h>
35#include <linux/timer.h>
36#include <linux/list.h>
37#include <linux/interrupt.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010038#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/usb.h>
David Brownell9454a572007-10-04 18:05:17 -070040#include <linux/usb/gadget.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020041#include <linux/usb/hcd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/byteorder.h>
44#include <asm/io.h>
45#include <asm/irq.h>
46#include <asm/system.h>
47#include <asm/unaligned.h>
48
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define DRIVER_DESC "USB Host+Gadget Emulator"
Alan Stern391eca92005-05-10 15:34:16 -040051#define DRIVER_VERSION "02 May 2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Alan Sterncaf29f62007-12-06 11:10:39 -050053#define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static const char driver_name [] = "dummy_hcd";
56static const char driver_desc [] = "USB Host+Gadget Emulator";
57
58static const char gadget_name [] = "dummy_udc";
59
60MODULE_DESCRIPTION (DRIVER_DESC);
61MODULE_AUTHOR ("David Brownell");
62MODULE_LICENSE ("GPL");
63
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +030064struct dummy_hcd_module_parameters {
65 bool is_super_speed;
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +030066 bool is_high_speed;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +030067};
68
69static struct dummy_hcd_module_parameters mod_data = {
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +030070 .is_super_speed = false,
71 .is_high_speed = true,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +030072};
73module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
74MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +030075module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
76MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*-------------------------------------------------------------------------*/
78
79/* gadget side driver data structres */
80struct dummy_ep {
81 struct list_head queue;
82 unsigned long last_io; /* jiffies timestamp */
83 struct usb_gadget *gadget;
84 const struct usb_endpoint_descriptor *desc;
85 struct usb_ep ep;
86 unsigned halted : 1;
Alan Stern851a5262008-08-14 15:48:30 -040087 unsigned wedged : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 unsigned already_seen : 1;
89 unsigned setup_stage : 1;
90};
91
92struct dummy_request {
93 struct list_head queue; /* ep's requests */
94 struct usb_request req;
95};
96
97static inline struct dummy_ep *usb_ep_to_dummy_ep (struct usb_ep *_ep)
98{
99 return container_of (_ep, struct dummy_ep, ep);
100}
101
102static inline struct dummy_request *usb_request_to_dummy_request
103 (struct usb_request *_req)
104{
105 return container_of (_req, struct dummy_request, req);
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
124static const char ep0name [] = "ep0";
125
126static const char *const ep_name [] = {
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};
Tobias Klauser52950ed2005-12-11 16:20:08 +0100141#define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Alan Sternd9b76252005-05-03 16:15:43 -0400143/*-------------------------------------------------------------------------*/
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define FIFO_SIZE 64
146
147struct urbp {
148 struct urb *urb;
149 struct list_head urbp_list;
150};
151
Alan Stern391eca92005-05-10 15:34:16 -0400152
153enum dummy_rh_state {
154 DUMMY_RH_RESET,
155 DUMMY_RH_SUSPENDED,
156 DUMMY_RH_RUNNING
157};
158
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300159struct dummy_hcd {
160 struct dummy *dum;
161 enum dummy_rh_state rh_state;
162 struct timer_list timer;
163 u32 port_status;
164 u32 old_status;
165 unsigned long re_timeout;
166
167 struct usb_device *udev;
168 struct list_head urbp_list;
169
170 unsigned active:1;
171 unsigned old_active:1;
172 unsigned resuming:1;
173};
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175struct dummy {
176 spinlock_t lock;
177
178 /*
179 * SLAVE/GADGET side support
180 */
181 struct dummy_ep ep [DUMMY_ENDPOINTS];
182 int address;
183 struct usb_gadget gadget;
184 struct usb_gadget_driver *driver;
185 struct dummy_request fifo_req;
186 u8 fifo_buf [FIFO_SIZE];
187 u16 devstatus;
Alan Stern391eca92005-05-10 15:34:16 -0400188 unsigned udc_suspended:1;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400189 unsigned pullup:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 /*
192 * MASTER/HOST side support
193 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300194 struct dummy_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300195 struct dummy_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300198static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300200 return (struct dummy_hcd *) (hcd->hcd_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300203static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 return container_of((void *) dum, struct usb_hcd, hcd_priv);
206}
207
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300208static inline struct device *dummy_dev(struct dummy_hcd *dum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300210 return dummy_hcd_to_hcd(dum)->self.controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Alan Sternd9b76252005-05-03 16:15:43 -0400213static inline struct device *udc_dev (struct dummy *dum)
214{
215 return dum->gadget.dev.parent;
216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
219{
220 return container_of (ep->gadget, struct dummy, gadget);
221}
222
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300223static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300225 struct dummy *dum = container_of(gadget, struct dummy, gadget);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300226 if (dum->gadget.speed == USB_SPEED_SUPER)
227 return dum->ss_hcd;
228 else
229 return dum->hs_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
233{
234 return container_of (dev, struct dummy, gadget.dev);
235}
236
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300237static struct dummy the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239/*-------------------------------------------------------------------------*/
240
Alan Sternf1c39fa2005-05-03 16:24:04 -0400241/* SLAVE/GADGET SIDE UTILITY ROUTINES */
242
243/* called with spinlock held */
244static void nuke (struct dummy *dum, struct dummy_ep *ep)
245{
246 while (!list_empty (&ep->queue)) {
247 struct dummy_request *req;
248
249 req = list_entry (ep->queue.next, struct dummy_request, queue);
250 list_del_init (&req->queue);
251 req->req.status = -ESHUTDOWN;
252
253 spin_unlock (&dum->lock);
254 req->req.complete (&ep->ep, &req->req);
255 spin_lock (&dum->lock);
256 }
257}
258
259/* caller must hold lock */
260static void
261stop_activity (struct dummy *dum)
262{
263 struct dummy_ep *ep;
264
265 /* prevent any more requests */
266 dum->address = 0;
267
268 /* The timer is left running so that outstanding URBs can fail */
269
270 /* nuke any pending requests first, so driver i/o is quiesced */
271 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
272 nuke (dum, ep);
273
274 /* driver now does any non-usb quiescing necessary */
275}
276
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300277/**
278 * set_link_state_by_speed() - Sets the current state of the link according to
279 * the hcd speed
280 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
281 *
282 * This function updates the port_status according to the link state and the
283 * speed of the hcd.
284 */
285static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
286{
287 struct dummy *dum = dum_hcd->dum;
288
289 if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
290 if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
291 dum_hcd->port_status = 0;
292 } else if (!dum->pullup || dum->udc_suspended) {
293 /* UDC suspend must cause a disconnect */
294 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
295 USB_PORT_STAT_ENABLE);
296 if ((dum_hcd->old_status &
297 USB_PORT_STAT_CONNECTION) != 0)
298 dum_hcd->port_status |=
299 (USB_PORT_STAT_C_CONNECTION << 16);
300 } else {
301 /* device is connected and not suspended */
302 dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
303 USB_PORT_STAT_SPEED_5GBPS) ;
304 if ((dum_hcd->old_status &
305 USB_PORT_STAT_CONNECTION) == 0)
306 dum_hcd->port_status |=
307 (USB_PORT_STAT_C_CONNECTION << 16);
308 if ((dum_hcd->port_status &
309 USB_PORT_STAT_ENABLE) == 1 &&
310 (dum_hcd->port_status &
311 USB_SS_PORT_LS_U0) == 1 &&
312 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
313 dum_hcd->active = 1;
314 }
315 } else {
316 if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
317 dum_hcd->port_status = 0;
318 } else if (!dum->pullup || dum->udc_suspended) {
319 /* UDC suspend must cause a disconnect */
320 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
321 USB_PORT_STAT_ENABLE |
322 USB_PORT_STAT_LOW_SPEED |
323 USB_PORT_STAT_HIGH_SPEED |
324 USB_PORT_STAT_SUSPEND);
325 if ((dum_hcd->old_status &
326 USB_PORT_STAT_CONNECTION) != 0)
327 dum_hcd->port_status |=
328 (USB_PORT_STAT_C_CONNECTION << 16);
329 } else {
330 dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
331 if ((dum_hcd->old_status &
332 USB_PORT_STAT_CONNECTION) == 0)
333 dum_hcd->port_status |=
334 (USB_PORT_STAT_C_CONNECTION << 16);
335 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
336 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
337 else if ((dum_hcd->port_status &
338 USB_PORT_STAT_SUSPEND) == 0 &&
339 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
340 dum_hcd->active = 1;
341 }
342 }
343}
344
Alan Sternf1c39fa2005-05-03 16:24:04 -0400345/* caller must hold lock */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300346static void set_link_state(struct dummy_hcd *dum_hcd)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400347{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300348 struct dummy *dum = dum_hcd->dum;
349
350 dum_hcd->active = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300351 if (dum->pullup)
352 if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
353 dum->gadget.speed != USB_SPEED_SUPER) ||
354 (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
355 dum->gadget.speed == USB_SPEED_SUPER))
356 return;
Alan Stern391eca92005-05-10 15:34:16 -0400357
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300358 set_link_state_by_speed(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400359
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300360 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
361 dum_hcd->active)
362 dum_hcd->resuming = 0;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400363
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300364 /* if !connected or reset */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300365 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
366 (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300367 /*
368 * We're connected and not reset (reset occurred now),
369 * and driver attached - disconnect!
370 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300371 if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300372 (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
373 dum->driver) {
374 stop_activity(dum);
375 spin_unlock(&dum->lock);
376 dum->driver->disconnect(&dum->gadget);
377 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400378 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300379 } else if (dum_hcd->active != dum_hcd->old_active) {
380 if (dum_hcd->old_active && dum->driver->suspend) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300381 spin_unlock(&dum->lock);
382 dum->driver->suspend(&dum->gadget);
383 spin_lock(&dum->lock);
384 } else if (!dum_hcd->old_active && dum->driver->resume) {
385 spin_unlock(&dum->lock);
386 dum->driver->resume(&dum->gadget);
387 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400388 }
389 }
390
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300391 dum_hcd->old_status = dum_hcd->port_status;
392 dum_hcd->old_active = dum_hcd->active;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400393}
394
395/*-------------------------------------------------------------------------*/
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397/* SLAVE/GADGET SIDE DRIVER
398 *
399 * This only tracks gadget state. All the work is done when the host
400 * side tries some (emulated) i/o operation. Real device controller
401 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
402 */
403
404#define is_enabled(dum) \
405 (dum->port_status & USB_PORT_STAT_ENABLE)
406
407static int
408dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
409{
410 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300411 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 struct dummy_ep *ep;
413 unsigned max;
414 int retval;
415
416 ep = usb_ep_to_dummy_ep (_ep);
417 if (!_ep || !desc || ep->desc || _ep->name == ep0name
418 || desc->bDescriptorType != USB_DT_ENDPOINT)
419 return -EINVAL;
420 dum = ep_to_dummy (ep);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300421 if (!dum->driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return -ESHUTDOWN;
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200423
424 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300425 if (!is_enabled(dum_hcd))
426 return -ESHUTDOWN;
427
428 /*
429 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
430 * maximum packet size.
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300431 * For SS devices the wMaxPacketSize is limited by 1024.
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300432 */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700433 max = usb_endpoint_maxp(desc) & 0x7ff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /* drivers must not request bad settings, since lower levels
436 * (hardware or its drivers) may not check. some endpoints
437 * can't do iso, many have maxpacket limitations, etc.
438 *
439 * since this "hardware" driver is here to help debugging, we
440 * have some extra sanity checks. (there could be more though,
441 * especially for "ep9out" style fixed function ones.)
442 */
443 retval = -EINVAL;
444 switch (desc->bmAttributes & 0x03) {
445 case USB_ENDPOINT_XFER_BULK:
446 if (strstr (ep->ep.name, "-iso")
447 || strstr (ep->ep.name, "-int")) {
448 goto done;
449 }
450 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300451 case USB_SPEED_SUPER:
452 if (max == 1024)
453 break;
454 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 case USB_SPEED_HIGH:
456 if (max == 512)
457 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700458 goto done;
459 case USB_SPEED_FULL:
460 if (max == 8 || max == 16 || max == 32 || max == 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /* we'll fake any legal size */
462 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700463 /* save a return statement */
464 default:
465 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467 break;
468 case USB_ENDPOINT_XFER_INT:
469 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
470 goto done;
471 /* real hardware might not handle all packet sizes */
472 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300473 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 case USB_SPEED_HIGH:
475 if (max <= 1024)
476 break;
477 /* save a return statement */
478 case USB_SPEED_FULL:
479 if (max <= 64)
480 break;
481 /* save a return statement */
482 default:
483 if (max <= 8)
484 break;
485 goto done;
486 }
487 break;
488 case USB_ENDPOINT_XFER_ISOC:
489 if (strstr (ep->ep.name, "-bulk")
490 || strstr (ep->ep.name, "-int"))
491 goto done;
492 /* real hardware might not handle all packet sizes */
493 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300494 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 case USB_SPEED_HIGH:
496 if (max <= 1024)
497 break;
498 /* save a return statement */
499 case USB_SPEED_FULL:
500 if (max <= 1023)
501 break;
502 /* save a return statement */
503 default:
504 goto done;
505 }
506 break;
507 default:
508 /* few chips support control except on ep0 */
509 goto done;
510 }
511
512 _ep->maxpacket = max;
513 ep->desc = desc;
514
Alan Sternd9b76252005-05-03 16:15:43 -0400515 dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 _ep->name,
517 desc->bEndpointAddress & 0x0f,
518 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
519 ({ char *val;
520 switch (desc->bmAttributes & 0x03) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +0300521 case USB_ENDPOINT_XFER_BULK:
522 val = "bulk";
523 break;
524 case USB_ENDPOINT_XFER_ISOC:
525 val = "iso";
526 break;
527 case USB_ENDPOINT_XFER_INT:
528 val = "intr";
529 break;
530 default:
531 val = "ctrl";
532 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }; val; }),
534 max);
535
536 /* at this point real hardware should be NAKing transfers
537 * to that endpoint, until a buffer is queued to it.
538 */
Alan Stern851a5262008-08-14 15:48:30 -0400539 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 retval = 0;
541done:
542 return retval;
543}
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545static int dummy_disable (struct usb_ep *_ep)
546{
547 struct dummy_ep *ep;
548 struct dummy *dum;
549 unsigned long flags;
550 int retval;
551
552 ep = usb_ep_to_dummy_ep (_ep);
553 if (!_ep || !ep->desc || _ep->name == ep0name)
554 return -EINVAL;
555 dum = ep_to_dummy (ep);
556
557 spin_lock_irqsave (&dum->lock, flags);
558 ep->desc = NULL;
559 retval = 0;
560 nuke (dum, ep);
561 spin_unlock_irqrestore (&dum->lock, flags);
562
Alan Sternd9b76252005-05-03 16:15:43 -0400563 dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return retval;
565}
566
567static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -0400568dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 struct dummy_ep *ep;
571 struct dummy_request *req;
572
573 if (!_ep)
574 return NULL;
575 ep = usb_ep_to_dummy_ep (_ep);
576
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800577 req = kzalloc(sizeof(*req), mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (!req)
579 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 INIT_LIST_HEAD (&req->queue);
581 return &req->req;
582}
583
584static void
585dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
586{
587 struct dummy_ep *ep;
588 struct dummy_request *req;
589
590 ep = usb_ep_to_dummy_ep (_ep);
591 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
592 return;
593
594 req = usb_request_to_dummy_request (_req);
595 WARN_ON (!list_empty (&req->queue));
596 kfree (req);
597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599static void
600fifo_complete (struct usb_ep *ep, struct usb_request *req)
601{
602}
603
604static int
Olav Kongas5db539e2005-06-23 20:25:36 +0300605dummy_queue (struct usb_ep *_ep, struct usb_request *_req,
Al Viro55016f12005-10-21 03:21:58 -0400606 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 struct dummy_ep *ep;
609 struct dummy_request *req;
610 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300611 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 unsigned long flags;
613
614 req = usb_request_to_dummy_request (_req);
615 if (!_req || !list_empty (&req->queue) || !_req->complete)
616 return -EINVAL;
617
618 ep = usb_ep_to_dummy_ep (_ep);
619 if (!_ep || (!ep->desc && _ep->name != ep0name))
620 return -EINVAL;
621
622 dum = ep_to_dummy (ep);
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200623 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300624 if (!dum->driver || !is_enabled(dum_hcd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return -ESHUTDOWN;
626
627#if 0
Alan Sternd9b76252005-05-03 16:15:43 -0400628 dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 ep, _req, _ep->name, _req->length, _req->buf);
630#endif
631
632 _req->status = -EINPROGRESS;
633 _req->actual = 0;
634 spin_lock_irqsave (&dum->lock, flags);
635
636 /* implement an emulated single-request FIFO */
637 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
638 list_empty (&dum->fifo_req.queue) &&
639 list_empty (&ep->queue) &&
640 _req->length <= FIFO_SIZE) {
641 req = &dum->fifo_req;
642 req->req = *_req;
643 req->req.buf = dum->fifo_buf;
644 memcpy (dum->fifo_buf, _req->buf, _req->length);
645 req->req.context = dum;
646 req->req.complete = fifo_complete;
647
David Brownellc728df72008-07-26 08:06:24 -0700648 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 spin_unlock (&dum->lock);
650 _req->actual = _req->length;
651 _req->status = 0;
652 _req->complete (_ep, _req);
653 spin_lock (&dum->lock);
David Brownellc728df72008-07-26 08:06:24 -0700654 } else
655 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 spin_unlock_irqrestore (&dum->lock, flags);
657
658 /* real hardware would likely enable transfers here, in case
659 * it'd been left NAKing.
660 */
661 return 0;
662}
663
664static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
665{
666 struct dummy_ep *ep;
667 struct dummy *dum;
668 int retval = -EINVAL;
669 unsigned long flags;
670 struct dummy_request *req = NULL;
671
672 if (!_ep || !_req)
673 return retval;
674 ep = usb_ep_to_dummy_ep (_ep);
675 dum = ep_to_dummy (ep);
676
677 if (!dum->driver)
678 return -ESHUTDOWN;
679
Alan Sternb4dbda12006-07-28 17:07:34 -0400680 local_irq_save (flags);
681 spin_lock (&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 list_for_each_entry (req, &ep->queue, queue) {
683 if (&req->req == _req) {
684 list_del_init (&req->queue);
685 _req->status = -ECONNRESET;
686 retval = 0;
687 break;
688 }
689 }
Alan Sternb4dbda12006-07-28 17:07:34 -0400690 spin_unlock (&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 if (retval == 0) {
Alan Sternd9b76252005-05-03 16:15:43 -0400693 dev_dbg (udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 "dequeued req %p from %s, len %d buf %p\n",
695 req, _ep->name, _req->length, _req->buf);
696 _req->complete (_ep, _req);
697 }
Alan Sternb4dbda12006-07-28 17:07:34 -0400698 local_irq_restore (flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return retval;
700}
701
702static int
Alan Stern851a5262008-08-14 15:48:30 -0400703dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct dummy_ep *ep;
706 struct dummy *dum;
707
708 if (!_ep)
709 return -EINVAL;
710 ep = usb_ep_to_dummy_ep (_ep);
711 dum = ep_to_dummy (ep);
712 if (!dum->driver)
713 return -ESHUTDOWN;
714 if (!value)
Alan Stern851a5262008-08-14 15:48:30 -0400715 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
717 !list_empty (&ep->queue))
718 return -EAGAIN;
Alan Stern851a5262008-08-14 15:48:30 -0400719 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 ep->halted = 1;
Alan Stern851a5262008-08-14 15:48:30 -0400721 if (wedged)
722 ep->wedged = 1;
723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 /* FIXME clear emulated data toggle too */
725 return 0;
726}
727
Alan Stern851a5262008-08-14 15:48:30 -0400728static int
729dummy_set_halt(struct usb_ep *_ep, int value)
730{
731 return dummy_set_halt_and_wedge(_ep, value, 0);
732}
733
734static int dummy_set_wedge(struct usb_ep *_ep)
735{
736 if (!_ep || _ep->name == ep0name)
737 return -EINVAL;
738 return dummy_set_halt_and_wedge(_ep, 1, 1);
739}
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741static const struct usb_ep_ops dummy_ep_ops = {
742 .enable = dummy_enable,
743 .disable = dummy_disable,
744
745 .alloc_request = dummy_alloc_request,
746 .free_request = dummy_free_request,
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 .queue = dummy_queue,
749 .dequeue = dummy_dequeue,
750
751 .set_halt = dummy_set_halt,
Alan Stern851a5262008-08-14 15:48:30 -0400752 .set_wedge = dummy_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753};
754
755/*-------------------------------------------------------------------------*/
756
757/* there are both host and device side versions of this call ... */
758static int dummy_g_get_frame (struct usb_gadget *_gadget)
759{
760 struct timeval tv;
761
762 do_gettimeofday (&tv);
763 return tv.tv_usec / 1000;
764}
765
766static int dummy_wakeup (struct usb_gadget *_gadget)
767{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300768 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300770 dum_hcd = gadget_to_dummy_hcd(_gadget);
771 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
Alan Stern5742b0c2005-05-02 11:25:17 -0400772 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return -EINVAL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300774 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
Alan Stern391eca92005-05-10 15:34:16 -0400775 return -ENOLINK;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300776 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
777 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -0400778 return -EIO;
779
780 /* FIXME: What if the root hub is suspended but the port isn't? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 /* hub notices our request, issues downstream resume, etc */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300783 dum_hcd->resuming = 1;
784 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
785 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return 0;
787}
788
789static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
790{
791 struct dummy *dum;
792
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300793 dum = (gadget_to_dummy_hcd(_gadget))->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (value)
795 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
796 else
797 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
798 return 0;
799}
800
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200801static void dummy_udc_udpate_ep0(struct dummy *dum)
802{
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100803 if (dum->gadget.speed == USB_SPEED_SUPER)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200804 dum->ep[0].ep.maxpacket = 9;
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100805 else
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200806 dum->ep[0].ep.maxpacket = 64;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200807}
808
Alan Sternf1c39fa2005-05-03 16:24:04 -0400809static int dummy_pullup (struct usb_gadget *_gadget, int value)
810{
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200811 struct dummy_hcd *dum_hcd;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400812 struct dummy *dum;
813 unsigned long flags;
814
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200815 dum = gadget_dev_to_dummy(&_gadget->dev);
816
817 if (value && dum->driver) {
818 if (mod_data.is_super_speed)
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100819 dum->gadget.speed = dum->driver->max_speed;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200820 else if (mod_data.is_high_speed)
821 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100822 dum->driver->max_speed);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200823 else
824 dum->gadget.speed = USB_SPEED_FULL;
825 dummy_udc_udpate_ep0(dum);
826
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100827 if (dum->gadget.speed < dum->driver->max_speed)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200828 dev_dbg(udc_dev(dum), "This device can perform faster"
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100829 " if you connect it to a %s port...\n",
830 usb_speed_string(dum->driver->max_speed));
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200831 }
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200832 dum_hcd = gadget_to_dummy_hcd(_gadget);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200833
Alan Sternf1c39fa2005-05-03 16:24:04 -0400834 spin_lock_irqsave (&dum->lock, flags);
835 dum->pullup = (value != 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200836 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400837 spin_unlock_irqrestore (&dum->lock, flags);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200838
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200839 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Sternf1c39fa2005-05-03 16:24:04 -0400840 return 0;
841}
842
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200843static int dummy_udc_start(struct usb_gadget *g,
844 struct usb_gadget_driver *driver);
845static int dummy_udc_stop(struct usb_gadget *g,
846 struct usb_gadget_driver *driver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848static const struct usb_gadget_ops dummy_ops = {
849 .get_frame = dummy_g_get_frame,
850 .wakeup = dummy_wakeup,
851 .set_selfpowered = dummy_set_selfpowered,
Alan Sternf1c39fa2005-05-03 16:24:04 -0400852 .pullup = dummy_pullup,
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200853 .udc_start = dummy_udc_start,
854 .udc_stop = dummy_udc_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855};
856
857/*-------------------------------------------------------------------------*/
858
859/* "function" sysfs attribute */
860static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400861show_function (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 struct dummy *dum = gadget_dev_to_dummy (dev);
864
865 if (!dum->driver || !dum->driver->function)
866 return 0;
867 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
868}
Alan Sterncc095b02005-05-10 15:28:38 -0400869static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871/*-------------------------------------------------------------------------*/
872
873/*
874 * Driver registration/unregistration.
875 *
876 * This is basically hardware-specific; there's usually only one real USB
877 * device (not host) controller since that's how USB devices are intended
878 * to work. So most implementations of these api calls will rely on the
879 * fact that only one driver will ever bind to the hardware. But curious
880 * hardware can be built with discrete components, so the gadget API doesn't
881 * require that assumption.
882 *
883 * For this emulator, it might be convenient to create a usb slave device
884 * for each driver that registers: just add to a big root hub.
885 */
886
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200887static int dummy_udc_start(struct usb_gadget *g,
888 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200890 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
891 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100893 if (driver->max_speed == USB_SPEED_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return -EINVAL;
895
896 /*
897 * SLAVE side init ... the layer above hardware, which
898 * can't enumerate without help from the driver we're binding.
899 */
Alan Stern5742b0c2005-05-02 11:25:17 -0400900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 dum->devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 dum->driver = driver;
Alan Sternd9b76252005-05-03 16:15:43 -0400904 dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return 0;
907}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200909static int dummy_udc_stop(struct usb_gadget *g,
910 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200912 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
913 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Alan Sternd9b76252005-05-03 16:15:43 -0400915 dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 driver->driver.name);
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 dum->driver = NULL;
Sebastian Andrzej Siewior25427872011-06-16 20:36:55 +0200919
920 dummy_pullup(&dum->gadget, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return 0;
922}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924#undef is_enabled
925
Alan Sternd9b76252005-05-03 16:15:43 -0400926/* The gadget structure is stored inside the hcd structure and will be
927 * released along with it. */
928static void
929dummy_gadget_release (struct device *dev)
930{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300931 return;
Alan Sternd9b76252005-05-03 16:15:43 -0400932}
933
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200934static void init_dummy_udc_hw(struct dummy *dum)
935{
936 int i;
937
938 INIT_LIST_HEAD(&dum->gadget.ep_list);
939 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
940 struct dummy_ep *ep = &dum->ep[i];
941
942 if (!ep_name[i])
943 break;
944 ep->ep.name = ep_name[i];
945 ep->ep.ops = &dummy_ep_ops;
946 list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
947 ep->halted = ep->wedged = ep->already_seen =
948 ep->setup_stage = 0;
949 ep->ep.maxpacket = ~0;
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100950 ep->ep.max_streams = 16;
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200951 ep->last_io = jiffies;
952 ep->gadget = &dum->gadget;
953 ep->desc = NULL;
954 INIT_LIST_HEAD(&ep->queue);
955 }
956
957 dum->gadget.ep0 = &dum->ep[0].ep;
958 list_del_init(&dum->ep[0].ep.ep_list);
959 INIT_LIST_HEAD(&dum->fifo_req.queue);
Sebastian Andrzej Siewiorf8744d42011-06-23 14:26:13 +0200960
961#ifdef CONFIG_USB_OTG
962 dum->gadget.is_otg = 1;
963#endif
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200964}
965
Alan Stern8364d6b2005-11-14 12:16:30 -0500966static int dummy_udc_probe (struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -0400967{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300968 struct dummy *dum = &the_controller;
Alan Sternd9b76252005-05-03 16:15:43 -0400969 int rc;
970
971 dum->gadget.name = gadget_name;
972 dum->gadget.ops = &dummy_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100973 dum->gadget.max_speed = USB_SPEED_SUPER;
Alan Sternd9b76252005-05-03 16:15:43 -0400974
Kay Sievers0031a062008-05-02 06:02:41 +0200975 dev_set_name(&dum->gadget.dev, "gadget");
Alan Stern8364d6b2005-11-14 12:16:30 -0500976 dum->gadget.dev.parent = &pdev->dev;
Alan Sternd9b76252005-05-03 16:15:43 -0400977 dum->gadget.dev.release = dummy_gadget_release;
978 rc = device_register (&dum->gadget.dev);
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530979 if (rc < 0) {
980 put_device(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -0400981 return rc;
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530982 }
Alan Sternd9b76252005-05-03 16:15:43 -0400983
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200984 init_dummy_udc_hw(dum);
985
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300986 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
987 if (rc < 0)
988 goto err_udc;
989
Alan Sternefd54a32006-09-25 11:55:56 -0400990 rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
991 if (rc < 0)
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300992 goto err_dev;
993 platform_set_drvdata(pdev, dum);
994 return rc;
995
996err_dev:
997 usb_del_gadget_udc(&dum->gadget);
998err_udc:
999 device_unregister(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -04001000 return rc;
1001}
1002
Alan Stern8364d6b2005-11-14 12:16:30 -05001003static int dummy_udc_remove (struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -04001004{
Alan Stern8364d6b2005-11-14 12:16:30 -05001005 struct dummy *dum = platform_get_drvdata (pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04001006
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001007 usb_del_gadget_udc(&dum->gadget);
Alan Stern8364d6b2005-11-14 12:16:30 -05001008 platform_set_drvdata (pdev, NULL);
Alan Sternd9b76252005-05-03 16:15:43 -04001009 device_remove_file (&dum->gadget.dev, &dev_attr_function);
1010 device_unregister (&dum->gadget.dev);
1011 return 0;
1012}
1013
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001014static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1015 int suspend)
Alan Stern391eca92005-05-10 15:34:16 -04001016{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001017 spin_lock_irq(&dum->lock);
1018 dum->udc_suspended = suspend;
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001019 set_link_state(dum_hcd);
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001020 spin_unlock_irq(&dum->lock);
1021}
Alan Stern391eca92005-05-10 15:34:16 -04001022
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001023static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1024{
1025 struct dummy *dum = platform_get_drvdata(pdev);
1026 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1027
1028 dev_dbg(&pdev->dev, "%s\n", __func__);
1029 dummy_udc_pm(dum, dum_hcd, 1);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001030 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001031 return 0;
1032}
1033
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001034static int dummy_udc_resume(struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04001035{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001036 struct dummy *dum = platform_get_drvdata(pdev);
1037 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Alan Stern391eca92005-05-10 15:34:16 -04001038
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001039 dev_dbg(&pdev->dev, "%s\n", __func__);
1040 dummy_udc_pm(dum, dum_hcd, 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001041 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001042 return 0;
1043}
1044
Russell King3ae5eae2005-11-09 22:32:44 +00001045static struct platform_driver dummy_udc_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04001046 .probe = dummy_udc_probe,
1047 .remove = dummy_udc_remove,
Alan Stern391eca92005-05-10 15:34:16 -04001048 .suspend = dummy_udc_suspend,
1049 .resume = dummy_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001050 .driver = {
1051 .name = (char *) gadget_name,
1052 .owner = THIS_MODULE,
1053 },
Alan Sternd9b76252005-05-03 16:15:43 -04001054};
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056/*-------------------------------------------------------------------------*/
1057
1058/* MASTER/HOST SIDE DRIVER
1059 *
1060 * this uses the hcd framework to hook up to host side drivers.
1061 * its root hub will only have one device, otherwise it acts like
1062 * a normal host controller.
1063 *
1064 * when urbs are queued, they're just stuck on a list that we
1065 * scan in a timer callback. that callback connects writes from
1066 * the host with reads from the device, and so on, based on the
1067 * usb 2.0 rules.
1068 */
1069
1070static int dummy_urb_enqueue (
1071 struct usb_hcd *hcd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001073 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001075 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 struct urbp *urbp;
1077 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001078 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1081 return -EINVAL;
1082
1083 urbp = kmalloc (sizeof *urbp, mem_flags);
1084 if (!urbp)
1085 return -ENOMEM;
1086 urbp->urb = urb;
1087
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001088 dum_hcd = hcd_to_dummy_hcd(hcd);
1089 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001090 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1091 if (rc) {
1092 kfree(urbp);
1093 goto done;
1094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001096 if (!dum_hcd->udev) {
1097 dum_hcd->udev = urb->dev;
1098 usb_get_dev(dum_hcd->udev);
1099 } else if (unlikely(dum_hcd->udev != urb->dev))
1100 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001102 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 urb->hcpriv = urbp;
1104 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
1105 urb->error_count = 1; /* mark as a new urb */
1106
1107 /* kick the scheduler, it'll do the rest */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001108 if (!timer_pending(&dum_hcd->timer))
1109 mod_timer(&dum_hcd->timer, jiffies + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Alan Sterne9df41c2007-08-08 11:48:02 -04001111 done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001112 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001113 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
Alan Sterne9df41c2007-08-08 11:48:02 -04001116static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001118 struct dummy_hcd *dum_hcd;
Alan Stern391eca92005-05-10 15:34:16 -04001119 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001120 int rc;
Alan Stern391eca92005-05-10 15:34:16 -04001121
1122 /* giveback happens automatically in timer callback,
1123 * so make sure the callback happens */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001124 dum_hcd = hcd_to_dummy_hcd(hcd);
1125 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001126
1127 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001128 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1129 !list_empty(&dum_hcd->urbp_list))
1130 mod_timer(&dum_hcd->timer, jiffies);
Alan Sterne9df41c2007-08-08 11:48:02 -04001131
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001132 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001133 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001136static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
1137 u32 len)
1138{
1139 void *ubuf, *rbuf;
1140 int to_host;
1141
1142 to_host = usb_pipein(urb->pipe);
1143 rbuf = req->req.buf + req->req.actual;
1144 ubuf = urb->transfer_buffer + urb->actual_length;
1145
1146 if (to_host)
1147 memcpy(ubuf, rbuf, len);
1148 else
1149 memcpy(rbuf, ubuf, len);
1150 return len;
1151}
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153/* transfer up to a frame's worth; caller must own lock */
1154static int
Alan Stern4d2f1102007-08-24 15:40:10 -04001155transfer(struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit,
1156 int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 struct dummy_request *req;
1159
1160top:
1161 /* if there's no request queued, the device is NAKing; return */
1162 list_for_each_entry (req, &ep->queue, queue) {
1163 unsigned host_len, dev_len, len;
1164 int is_short, to_host;
1165 int rescan = 0;
1166
1167 /* 1..N packets of ep->ep.maxpacket each ... the last one
1168 * may be short (including zero length).
1169 *
1170 * writer can send a zlp explicitly (length 0) or implicitly
1171 * (length mod maxpacket zero, and 'zero' flag); they always
1172 * terminate reads.
1173 */
1174 host_len = urb->transfer_buffer_length - urb->actual_length;
1175 dev_len = req->req.length - req->req.actual;
1176 len = min (host_len, dev_len);
1177
1178 /* FIXME update emulated data toggle too */
1179
1180 to_host = usb_pipein (urb->pipe);
1181 if (unlikely (len == 0))
1182 is_short = 1;
1183 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 /* not enough bandwidth left? */
1185 if (limit < ep->ep.maxpacket && limit < len)
1186 break;
1187 len = min (len, (unsigned) limit);
1188 if (len == 0)
1189 break;
1190
1191 /* use an extra pass for the final short packet */
1192 if (len > ep->ep.maxpacket) {
1193 rescan = 1;
1194 len -= (len % ep->ep.maxpacket);
1195 }
1196 is_short = (len % ep->ep.maxpacket) != 0;
1197
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001198 len = dummy_perform_transfer(urb, req, len);
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 ep->last_io = jiffies;
1201
1202 limit -= len;
1203 urb->actual_length += len;
1204 req->req.actual += len;
1205 }
1206
1207 /* short packets terminate, maybe with overflow/underflow.
1208 * it's only really an error to write too much.
1209 *
1210 * partially filling a buffer optionally blocks queue advances
1211 * (so completion handlers can clean up the queue) but we don't
Alan Sternb0d9efb2007-08-21 15:39:21 -04001212 * need to emulate such data-in-flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 */
1214 if (is_short) {
1215 if (host_len == dev_len) {
1216 req->req.status = 0;
Alan Stern4d2f1102007-08-24 15:40:10 -04001217 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 } else if (to_host) {
1219 req->req.status = 0;
1220 if (dev_len > host_len)
Alan Stern4d2f1102007-08-24 15:40:10 -04001221 *status = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 else
Alan Stern4d2f1102007-08-24 15:40:10 -04001223 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 } else if (!to_host) {
Alan Stern4d2f1102007-08-24 15:40:10 -04001225 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (host_len > dev_len)
1227 req->req.status = -EOVERFLOW;
1228 else
1229 req->req.status = 0;
1230 }
1231
1232 /* many requests terminate without a short packet */
1233 } else {
1234 if (req->req.length == req->req.actual
1235 && !req->req.zero)
1236 req->req.status = 0;
1237 if (urb->transfer_buffer_length == urb->actual_length
1238 && !(urb->transfer_flags
Alan Stern4d2f1102007-08-24 15:40:10 -04001239 & URB_ZERO_PACKET))
1240 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242
1243 /* device side completion --> continuable */
1244 if (req->req.status != -EINPROGRESS) {
1245 list_del_init (&req->queue);
1246
1247 spin_unlock (&dum->lock);
1248 req->req.complete (&ep->ep, &req->req);
1249 spin_lock (&dum->lock);
1250
1251 /* requests might have been unlinked... */
1252 rescan = 1;
1253 }
1254
1255 /* host side completion --> terminate */
Alan Stern4d2f1102007-08-24 15:40:10 -04001256 if (*status != -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 break;
1258
1259 /* rescan to continue with any other queued i/o */
1260 if (rescan)
1261 goto top;
1262 }
1263 return limit;
1264}
1265
1266static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1267{
1268 int limit = ep->ep.maxpacket;
1269
1270 if (dum->gadget.speed == USB_SPEED_HIGH) {
1271 int tmp;
1272
1273 /* high bandwidth mode */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001274 tmp = usb_endpoint_maxp(ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 tmp = (tmp >> 11) & 0x03;
1276 tmp *= 8 /* applies to entire frame */;
1277 limit += limit * tmp;
1278 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001279 if (dum->gadget.speed == USB_SPEED_SUPER) {
1280 switch (ep->desc->bmAttributes & 0x03) {
1281 case USB_ENDPOINT_XFER_ISOC:
1282 /* Sec. 4.4.8.2 USB3.0 Spec */
1283 limit = 3 * 16 * 1024 * 8;
1284 break;
1285 case USB_ENDPOINT_XFER_INT:
1286 /* Sec. 4.4.7.2 USB3.0 Spec */
1287 limit = 3 * 1024 * 8;
1288 break;
1289 case USB_ENDPOINT_XFER_BULK:
1290 default:
1291 break;
1292 }
1293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return limit;
1295}
1296
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001297#define is_active(dum_hcd) ((dum_hcd->port_status & \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1299 USB_PORT_STAT_SUSPEND)) \
1300 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1301
1302static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1303{
1304 int i;
1305
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001306 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1307 dum->ss_hcd : dum->hs_hcd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return NULL;
1309 if ((address & ~USB_DIR_IN) == 0)
1310 return &dum->ep [0];
1311 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1312 struct dummy_ep *ep = &dum->ep [i];
1313
1314 if (!ep->desc)
1315 continue;
1316 if (ep->desc->bEndpointAddress == address)
1317 return ep;
1318 }
1319 return NULL;
1320}
1321
1322#undef is_active
1323
1324#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1325#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1326#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1327#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1328#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1329#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1330
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001331
1332/**
1333 * handle_control_request() - handles all control transfers
1334 * @dum: pointer to dummy (the_controller)
1335 * @urb: the urb request to handle
1336 * @setup: pointer to the setup data for a USB device control
1337 * request
1338 * @status: pointer to request handling status
1339 *
1340 * Return 0 - if the request was handled
1341 * 1 - if the request wasn't handles
1342 * error code on error
1343 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001344static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001345 struct usb_ctrlrequest *setup,
1346 int *status)
1347{
1348 struct dummy_ep *ep2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001349 struct dummy *dum = dum_hcd->dum;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001350 int ret_val = 1;
1351 unsigned w_index;
1352 unsigned w_value;
1353
1354 w_index = le16_to_cpu(setup->wIndex);
1355 w_value = le16_to_cpu(setup->wValue);
1356 switch (setup->bRequest) {
1357 case USB_REQ_SET_ADDRESS:
1358 if (setup->bRequestType != Dev_Request)
1359 break;
1360 dum->address = w_value;
1361 *status = 0;
1362 dev_dbg(udc_dev(dum), "set_address = %d\n",
1363 w_value);
1364 ret_val = 0;
1365 break;
1366 case USB_REQ_SET_FEATURE:
1367 if (setup->bRequestType == Dev_Request) {
1368 ret_val = 0;
1369 switch (w_value) {
1370 case USB_DEVICE_REMOTE_WAKEUP:
1371 break;
1372 case USB_DEVICE_B_HNP_ENABLE:
1373 dum->gadget.b_hnp_enable = 1;
1374 break;
1375 case USB_DEVICE_A_HNP_SUPPORT:
1376 dum->gadget.a_hnp_support = 1;
1377 break;
1378 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1379 dum->gadget.a_alt_hnp_support = 1;
1380 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001381 case USB_DEVICE_U1_ENABLE:
1382 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1383 HCD_USB3)
1384 w_value = USB_DEV_STAT_U1_ENABLED;
1385 else
1386 ret_val = -EOPNOTSUPP;
1387 break;
1388 case USB_DEVICE_U2_ENABLE:
1389 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1390 HCD_USB3)
1391 w_value = USB_DEV_STAT_U2_ENABLED;
1392 else
1393 ret_val = -EOPNOTSUPP;
1394 break;
1395 case USB_DEVICE_LTM_ENABLE:
1396 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1397 HCD_USB3)
1398 w_value = USB_DEV_STAT_LTM_ENABLED;
1399 else
1400 ret_val = -EOPNOTSUPP;
1401 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001402 default:
1403 ret_val = -EOPNOTSUPP;
1404 }
1405 if (ret_val == 0) {
1406 dum->devstatus |= (1 << w_value);
1407 *status = 0;
1408 }
1409 } else if (setup->bRequestType == Ep_Request) {
1410 /* endpoint halt */
1411 ep2 = find_endpoint(dum, w_index);
1412 if (!ep2 || ep2->ep.name == ep0name) {
1413 ret_val = -EOPNOTSUPP;
1414 break;
1415 }
1416 ep2->halted = 1;
1417 ret_val = 0;
1418 *status = 0;
1419 }
1420 break;
1421 case USB_REQ_CLEAR_FEATURE:
1422 if (setup->bRequestType == Dev_Request) {
1423 ret_val = 0;
1424 switch (w_value) {
1425 case USB_DEVICE_REMOTE_WAKEUP:
1426 w_value = USB_DEVICE_REMOTE_WAKEUP;
1427 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001428 case USB_DEVICE_U1_ENABLE:
1429 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1430 HCD_USB3)
1431 w_value = USB_DEV_STAT_U1_ENABLED;
1432 else
1433 ret_val = -EOPNOTSUPP;
1434 break;
1435 case USB_DEVICE_U2_ENABLE:
1436 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1437 HCD_USB3)
1438 w_value = USB_DEV_STAT_U2_ENABLED;
1439 else
1440 ret_val = -EOPNOTSUPP;
1441 break;
1442 case USB_DEVICE_LTM_ENABLE:
1443 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1444 HCD_USB3)
1445 w_value = USB_DEV_STAT_LTM_ENABLED;
1446 else
1447 ret_val = -EOPNOTSUPP;
1448 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001449 default:
1450 ret_val = -EOPNOTSUPP;
1451 break;
1452 }
1453 if (ret_val == 0) {
1454 dum->devstatus &= ~(1 << w_value);
1455 *status = 0;
1456 }
1457 } else if (setup->bRequestType == Ep_Request) {
1458 /* endpoint halt */
1459 ep2 = find_endpoint(dum, w_index);
1460 if (!ep2) {
1461 ret_val = -EOPNOTSUPP;
1462 break;
1463 }
1464 if (!ep2->wedged)
1465 ep2->halted = 0;
1466 ret_val = 0;
1467 *status = 0;
1468 }
1469 break;
1470 case USB_REQ_GET_STATUS:
1471 if (setup->bRequestType == Dev_InRequest
1472 || setup->bRequestType == Intf_InRequest
1473 || setup->bRequestType == Ep_InRequest) {
1474 char *buf;
1475 /*
1476 * device: remote wakeup, selfpowered
1477 * interface: nothing
1478 * endpoint: halt
1479 */
1480 buf = (char *)urb->transfer_buffer;
1481 if (urb->transfer_buffer_length > 0) {
1482 if (setup->bRequestType == Ep_InRequest) {
1483 ep2 = find_endpoint(dum, w_index);
1484 if (!ep2) {
1485 ret_val = -EOPNOTSUPP;
1486 break;
1487 }
1488 buf[0] = ep2->halted;
1489 } else if (setup->bRequestType ==
1490 Dev_InRequest) {
1491 buf[0] = (u8)dum->devstatus;
1492 } else
1493 buf[0] = 0;
1494 }
1495 if (urb->transfer_buffer_length > 1)
1496 buf[1] = 0;
1497 urb->actual_length = min_t(u32, 2,
1498 urb->transfer_buffer_length);
1499 ret_val = 0;
1500 *status = 0;
1501 }
1502 break;
1503 }
1504 return ret_val;
1505}
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507/* drive both sides of the transfers; looks like irq handlers to
1508 * both drivers except the callbacks aren't in_irq().
1509 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001510static void dummy_timer(unsigned long _dum_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001512 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1513 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 struct urbp *urbp, *tmp;
1515 unsigned long flags;
1516 int limit, total;
1517 int i;
1518
1519 /* simplistic model for one frame's bandwidth */
1520 switch (dum->gadget.speed) {
1521 case USB_SPEED_LOW:
1522 total = 8/*bytes*/ * 12/*packets*/;
1523 break;
1524 case USB_SPEED_FULL:
1525 total = 64/*bytes*/ * 19/*packets*/;
1526 break;
1527 case USB_SPEED_HIGH:
1528 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1529 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001530 case USB_SPEED_SUPER:
1531 /* Bus speed is 500000 bytes/ms, so use a little less */
1532 total = 490000;
1533 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001535 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return;
1537 }
1538
1539 /* FIXME if HZ != 1000 this will probably misbehave ... */
1540
1541 /* look at each urb queued by the host side driver */
1542 spin_lock_irqsave (&dum->lock, flags);
1543
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001544 if (!dum_hcd->udev) {
1545 dev_err(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 "timer fired with no URBs pending?\n");
1547 spin_unlock_irqrestore (&dum->lock, flags);
1548 return;
1549 }
1550
1551 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1552 if (!ep_name [i])
1553 break;
1554 dum->ep [i].already_seen = 0;
1555 }
1556
1557restart:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001558 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 struct urb *urb;
1560 struct dummy_request *req;
1561 u8 address;
1562 struct dummy_ep *ep = NULL;
1563 int type;
Alan Stern4d2f1102007-08-24 15:40:10 -04001564 int status = -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 urb = urbp->urb;
Alan Sterneb2310542007-08-21 15:40:36 -04001567 if (urb->unlinked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 goto return_urb;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001569 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
Alan Stern391eca92005-05-10 15:34:16 -04001570 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 type = usb_pipetype (urb->pipe);
1572
1573 /* used up this frame's non-periodic bandwidth?
1574 * FIXME there's infinite bandwidth for control and
1575 * periodic transfers ... unrealistic.
1576 */
1577 if (total <= 0 && type == PIPE_BULK)
1578 continue;
1579
1580 /* find the gadget's ep for this request (if configured) */
1581 address = usb_pipeendpoint (urb->pipe);
1582 if (usb_pipein (urb->pipe))
1583 address |= USB_DIR_IN;
1584 ep = find_endpoint(dum, address);
1585 if (!ep) {
1586 /* set_configuration() disagreement */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001587 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 "no ep configured for urb %p\n",
1589 urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001590 status = -EPROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 goto return_urb;
1592 }
1593
1594 if (ep->already_seen)
1595 continue;
1596 ep->already_seen = 1;
1597 if (ep == &dum->ep [0] && urb->error_count) {
1598 ep->setup_stage = 1; /* a new urb */
1599 urb->error_count = 0;
1600 }
1601 if (ep->halted && !ep->setup_stage) {
1602 /* NOTE: must not be iso! */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001603 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 ep->ep.name, urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001605 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 goto return_urb;
1607 }
1608 /* FIXME make sure both ends agree on maxpacket */
1609
1610 /* handle control requests */
1611 if (ep == &dum->ep [0] && ep->setup_stage) {
1612 struct usb_ctrlrequest setup;
1613 int value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 /* paranoia, in case of stale queued data */
1617 list_for_each_entry (req, &ep->queue, queue) {
1618 list_del_init (&req->queue);
1619 req->req.status = -EOVERFLOW;
Alan Sternd9b76252005-05-03 16:15:43 -04001620 dev_dbg (udc_dev(dum), "stale req = %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 req);
1622
1623 spin_unlock (&dum->lock);
1624 req->req.complete (&ep->ep, &req->req);
1625 spin_lock (&dum->lock);
1626 ep->already_seen = 0;
1627 goto restart;
1628 }
1629
1630 /* gadget driver never sees set_address or operations
1631 * on standard feature flags. some hardware doesn't
1632 * even expose them.
1633 */
1634 ep->last_io = jiffies;
1635 ep->setup_stage = 0;
1636 ep->halted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001638 value = handle_control_request(dum_hcd, urb, &setup,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001639 &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 /* gadget driver handles all other requests. block
1642 * until setup() returns; no reentrancy issues etc.
1643 */
1644 if (value > 0) {
1645 spin_unlock (&dum->lock);
1646 value = dum->driver->setup (&dum->gadget,
1647 &setup);
1648 spin_lock (&dum->lock);
1649
1650 if (value >= 0) {
1651 /* no delays (max 64KB data stage) */
1652 limit = 64*1024;
1653 goto treat_control_like_bulk;
1654 }
1655 /* error, see below */
1656 }
1657
1658 if (value < 0) {
1659 if (value != -EOPNOTSUPP)
Alan Sternd9b76252005-05-03 16:15:43 -04001660 dev_dbg (udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 "setup --> %d\n",
1662 value);
Alan Stern4d2f1102007-08-24 15:40:10 -04001663 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 urb->actual_length = 0;
1665 }
1666
1667 goto return_urb;
1668 }
1669
1670 /* non-control requests */
1671 limit = total;
1672 switch (usb_pipetype (urb->pipe)) {
1673 case PIPE_ISOCHRONOUS:
1674 /* FIXME is it urb->interval since the last xfer?
1675 * use urb->iso_frame_desc[i].
1676 * complete whether or not ep has requests queued.
1677 * report random errors, to debug drivers.
1678 */
1679 limit = max (limit, periodic_bytes (dum, ep));
Alan Stern4d2f1102007-08-24 15:40:10 -04001680 status = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 break;
1682
1683 case PIPE_INTERRUPT:
1684 /* FIXME is it urb->interval since the last xfer?
1685 * this almost certainly polls too fast.
1686 */
1687 limit = max (limit, periodic_bytes (dum, ep));
1688 /* FALLTHROUGH */
1689
1690 // case PIPE_BULK: case PIPE_CONTROL:
1691 default:
1692 treat_control_like_bulk:
1693 ep->last_io = jiffies;
Alan Stern4d2f1102007-08-24 15:40:10 -04001694 total = transfer(dum, urb, ep, limit, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 break;
1696 }
1697
1698 /* incomplete transfer? */
Alan Stern4d2f1102007-08-24 15:40:10 -04001699 if (status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 continue;
1701
1702return_urb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 list_del (&urbp->urbp_list);
1704 kfree (urbp);
1705 if (ep)
1706 ep->already_seen = ep->setup_stage = 0;
1707
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001708 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 spin_unlock (&dum->lock);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001710 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 spin_lock (&dum->lock);
1712
1713 goto restart;
1714 }
1715
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001716 if (list_empty(&dum_hcd->urbp_list)) {
1717 usb_put_dev(dum_hcd->udev);
1718 dum_hcd->udev = NULL;
1719 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern391eca92005-05-10 15:34:16 -04001720 /* want a 1 msec delay here */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001721 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723
1724 spin_unlock_irqrestore (&dum->lock, flags);
1725}
1726
1727/*-------------------------------------------------------------------------*/
1728
1729#define PORT_C_MASK \
Alan Sternc2db8b52005-04-29 16:30:48 -04001730 ((USB_PORT_STAT_C_CONNECTION \
1731 | USB_PORT_STAT_C_ENABLE \
1732 | USB_PORT_STAT_C_SUSPEND \
1733 | USB_PORT_STAT_C_OVERCURRENT \
1734 | USB_PORT_STAT_C_RESET) << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1737{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001738 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 unsigned long flags;
Alan Stern391eca92005-05-10 15:34:16 -04001740 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001742 dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001744 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001745 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001746 goto done;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001747
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001748 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1749 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1750 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1751 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001752 }
1753
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001754 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 *buf = (1 << 1);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001756 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1757 dum_hcd->port_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 retval = 1;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001759 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -04001760 usb_hcd_resume_root_hub (hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
Alan Stern391eca92005-05-10 15:34:16 -04001762done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001763 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return retval;
1765}
1766
1767static inline void
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001768ss_hub_descriptor(struct usb_hub_descriptor *desc)
1769{
1770 memset(desc, 0, sizeof *desc);
1771 desc->bDescriptorType = 0x2a;
1772 desc->bDescLength = 12;
1773 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1774 desc->bNbrPorts = 1;
1775 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1776 desc->u.ss.DeviceRemovable = 0xffff;
1777}
1778
1779static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780hub_descriptor (struct usb_hub_descriptor *desc)
1781{
1782 memset (desc, 0, sizeof *desc);
1783 desc->bDescriptorType = 0x29;
1784 desc->bDescLength = 9;
Al Virofd05e722008-04-28 07:00:16 +01001785 desc->wHubCharacteristics = cpu_to_le16(0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 desc->bNbrPorts = 1;
John Youndbe79bb2001-09-17 00:00:00 -07001787 desc->u.hs.DeviceRemovable[0] = 0xff;
1788 desc->u.hs.DeviceRemovable[1] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
1791static int dummy_hub_control (
1792 struct usb_hcd *hcd,
1793 u16 typeReq,
1794 u16 wValue,
1795 u16 wIndex,
1796 char *buf,
1797 u16 wLength
1798) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001799 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 int retval = 0;
1801 unsigned long flags;
1802
Alan Stern541c7d42010-06-22 16:39:10 -04001803 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001804 return -ETIMEDOUT;
1805
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001806 dum_hcd = hcd_to_dummy_hcd(hcd);
1807
1808 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 switch (typeReq) {
1810 case ClearHubFeature:
1811 break;
1812 case ClearPortFeature:
1813 switch (wValue) {
1814 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001815 if (hcd->speed == HCD_USB3) {
1816 dev_dbg(dummy_dev(dum_hcd),
1817 "USB_PORT_FEAT_SUSPEND req not "
1818 "supported for USB 3.0 roothub\n");
1819 goto error;
1820 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001821 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 /* 20msec resume signaling */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001823 dum_hcd->resuming = 1;
1824 dum_hcd->re_timeout = jiffies +
Alan Sternf1c39fa2005-05-03 16:24:04 -04001825 msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
1827 break;
1828 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001829 if (hcd->speed == HCD_USB3) {
1830 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1831 dev_dbg(dummy_dev(dum_hcd),
1832 "power-off\n");
1833 } else
1834 if (dum_hcd->port_status &
1835 USB_SS_PORT_STAT_POWER)
1836 dev_dbg(dummy_dev(dum_hcd),
1837 "power-off\n");
Alan Sternf1c39fa2005-05-03 16:24:04 -04001838 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001840 dum_hcd->port_status &= ~(1 << wValue);
1841 set_link_state(dum_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 }
1843 break;
1844 case GetHubDescriptor:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001845 if (hcd->speed == HCD_USB3 &&
1846 (wLength < USB_DT_SS_HUB_SIZE ||
1847 wValue != (USB_DT_SS_HUB << 8))) {
1848 dev_dbg(dummy_dev(dum_hcd),
1849 "Wrong hub descriptor type for "
1850 "USB 3.0 roothub.\n");
1851 goto error;
1852 }
1853 if (hcd->speed == HCD_USB3)
1854 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
1855 else
1856 hub_descriptor((struct usb_hub_descriptor *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 break;
1858 case GetHubStatus:
Harvey Harrison551509d2009-02-11 14:11:36 -08001859 *(__le32 *) buf = cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 break;
1861 case GetPortStatus:
1862 if (wIndex != 1)
1863 retval = -EPIPE;
1864
1865 /* whoever resets or resumes must GetPortStatus to
1866 * complete it!!
1867 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001868 if (dum_hcd->resuming &&
1869 time_after_eq(jiffies, dum_hcd->re_timeout)) {
1870 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1871 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001873 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
1874 time_after_eq(jiffies, dum_hcd->re_timeout)) {
1875 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
1876 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
1877 if (dum_hcd->dum->pullup) {
1878 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001879
1880 if (hcd->speed < HCD_USB3) {
1881 switch (dum_hcd->dum->gadget.speed) {
1882 case USB_SPEED_HIGH:
1883 dum_hcd->port_status |=
1884 USB_PORT_STAT_HIGH_SPEED;
1885 break;
1886 case USB_SPEED_LOW:
1887 dum_hcd->dum->gadget.ep0->
1888 maxpacket = 8;
1889 dum_hcd->port_status |=
1890 USB_PORT_STAT_LOW_SPEED;
1891 break;
1892 default:
1893 dum_hcd->dum->gadget.speed =
1894 USB_SPEED_FULL;
1895 break;
1896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
1898 }
1899 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001900 set_link_state(dum_hcd);
1901 ((__le16 *) buf)[0] = cpu_to_le16 (dum_hcd->port_status);
1902 ((__le16 *) buf)[1] = cpu_to_le16 (dum_hcd->port_status >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 break;
1904 case SetHubFeature:
1905 retval = -EPIPE;
1906 break;
1907 case SetPortFeature:
1908 switch (wValue) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001909 case USB_PORT_FEAT_LINK_STATE:
1910 if (hcd->speed != HCD_USB3) {
1911 dev_dbg(dummy_dev(dum_hcd),
1912 "USB_PORT_FEAT_LINK_STATE req not "
1913 "supported for USB 2.0 roothub\n");
1914 goto error;
1915 }
1916 /*
1917 * Since this is dummy we don't have an actual link so
1918 * there is nothing to do for the SET_LINK_STATE cmd
1919 */
1920 break;
1921 case USB_PORT_FEAT_U1_TIMEOUT:
1922 case USB_PORT_FEAT_U2_TIMEOUT:
1923 /* TODO: add suspend/resume support! */
1924 if (hcd->speed != HCD_USB3) {
1925 dev_dbg(dummy_dev(dum_hcd),
1926 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
1927 "supported for USB 2.0 roothub\n");
1928 goto error;
1929 }
1930 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001932 /* Applicable only for USB2.0 hub */
1933 if (hcd->speed == HCD_USB3) {
1934 dev_dbg(dummy_dev(dum_hcd),
1935 "USB_PORT_FEAT_SUSPEND req not "
1936 "supported for USB 3.0 roothub\n");
1937 goto error;
1938 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001939 if (dum_hcd->active) {
1940 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001941
1942 /* HNP would happen here; for now we
1943 * assume b_bus_req is always true.
1944 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001945 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001946 if (((1 << USB_DEVICE_B_HNP_ENABLE)
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001947 & dum_hcd->dum->devstatus) != 0)
1948 dev_dbg(dummy_dev(dum_hcd),
Alan Stern5742b0c2005-05-02 11:25:17 -04001949 "no HNP yet!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951 break;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001952 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001953 if (hcd->speed == HCD_USB3)
1954 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
1955 else
1956 dum_hcd->port_status |= USB_PORT_STAT_POWER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001957 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001958 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001959 case USB_PORT_FEAT_BH_PORT_RESET:
1960 /* Applicable only for USB3.0 hub */
1961 if (hcd->speed != HCD_USB3) {
1962 dev_dbg(dummy_dev(dum_hcd),
1963 "USB_PORT_FEAT_BH_PORT_RESET req not "
1964 "supported for USB 2.0 roothub\n");
1965 goto error;
1966 }
1967 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 case USB_PORT_FEAT_RESET:
Alan Sternf1c39fa2005-05-03 16:24:04 -04001969 /* if it's already enabled, disable */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001970 if (hcd->speed == HCD_USB3) {
1971 dum_hcd->port_status = 0;
1972 dum_hcd->port_status =
1973 (USB_SS_PORT_STAT_POWER |
1974 USB_PORT_STAT_CONNECTION |
1975 USB_PORT_STAT_RESET);
1976 } else
1977 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
Alan Sternf1c39fa2005-05-03 16:24:04 -04001978 | USB_PORT_STAT_LOW_SPEED
1979 | USB_PORT_STAT_HIGH_SPEED);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001980 /*
1981 * We want to reset device status. All but the
1982 * Self powered feature
1983 */
1984 dum_hcd->dum->devstatus &=
1985 (1 << USB_DEVICE_SELF_POWERED);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001986 /*
1987 * FIXME USB3.0: what is the correct reset signaling
1988 * interval? Is it still 50msec as for HS?
1989 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001990 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001991 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 default:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001993 if (hcd->speed == HCD_USB3) {
1994 if ((dum_hcd->port_status &
1995 USB_SS_PORT_STAT_POWER) != 0) {
1996 dum_hcd->port_status |= (1 << wValue);
1997 set_link_state(dum_hcd);
1998 }
1999 } else
2000 if ((dum_hcd->port_status &
2001 USB_PORT_STAT_POWER) != 0) {
2002 dum_hcd->port_status |= (1 << wValue);
2003 set_link_state(dum_hcd);
2004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
2006 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002007 case GetPortErrorCount:
2008 if (hcd->speed != HCD_USB3) {
2009 dev_dbg(dummy_dev(dum_hcd),
2010 "GetPortErrorCount req not "
2011 "supported for USB 2.0 roothub\n");
2012 goto error;
2013 }
2014 /* We'll always return 0 since this is a dummy hub */
2015 *(__le32 *) buf = cpu_to_le32(0);
2016 break;
2017 case SetHubDepth:
2018 if (hcd->speed != HCD_USB3) {
2019 dev_dbg(dummy_dev(dum_hcd),
2020 "SetHubDepth req not supported for "
2021 "USB 2.0 roothub\n");
2022 goto error;
2023 }
2024 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002026 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 "hub control req%04x v%04x i%04x l%d\n",
2028 typeReq, wValue, wIndex, wLength);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002029error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 /* "protocol stall" on error */
2031 retval = -EPIPE;
2032 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002033 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Stern685eb932005-05-03 16:27:26 -04002034
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002035 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
Alan Stern685eb932005-05-03 16:27:26 -04002036 usb_hcd_poll_rh_status (hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return retval;
2038}
2039
Alan Stern0c0382e2005-10-13 17:08:02 -04002040static int dummy_bus_suspend (struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002041{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002042 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002043
Harvey Harrison441b62c2008-03-03 16:08:34 -08002044 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern3cf0a222005-11-29 12:08:15 -05002045
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002046 spin_lock_irq(&dum_hcd->dum->lock);
2047 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2048 set_link_state(dum_hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002049 hcd->state = HC_STATE_SUSPENDED;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002050 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern391eca92005-05-10 15:34:16 -04002051 return 0;
2052}
2053
Alan Stern0c0382e2005-10-13 17:08:02 -04002054static int dummy_bus_resume (struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002055{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002056 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002057 int rc = 0;
2058
Harvey Harrison441b62c2008-03-03 16:08:34 -08002059 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002060
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002061 spin_lock_irq(&dum_hcd->dum->lock);
Alan Stern541c7d42010-06-22 16:39:10 -04002062 if (!HCD_HW_ACCESSIBLE(hcd)) {
Alan Sterncfa59da2007-06-21 16:25:35 -04002063 rc = -ESHUTDOWN;
Alan Stern3cf0a222005-11-29 12:08:15 -05002064 } else {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002065 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2066 set_link_state(dum_hcd);
2067 if (!list_empty(&dum_hcd->urbp_list))
2068 mod_timer(&dum_hcd->timer, jiffies);
Alan Stern3cf0a222005-11-29 12:08:15 -05002069 hcd->state = HC_STATE_RUNNING;
2070 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002071 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern3cf0a222005-11-29 12:08:15 -05002072 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002073}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075/*-------------------------------------------------------------------------*/
2076
2077static inline ssize_t
2078show_urb (char *buf, size_t size, struct urb *urb)
2079{
2080 int ep = usb_pipeendpoint (urb->pipe);
2081
2082 return snprintf (buf, size,
2083 "urb/%p %s ep%d%s%s len %d/%d\n",
2084 urb,
2085 ({ char *s;
2086 switch (urb->dev->speed) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002087 case USB_SPEED_LOW:
2088 s = "ls";
2089 break;
2090 case USB_SPEED_FULL:
2091 s = "fs";
2092 break;
2093 case USB_SPEED_HIGH:
2094 s = "hs";
2095 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002096 case USB_SPEED_SUPER:
2097 s = "ss";
2098 break;
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002099 default:
2100 s = "?";
2101 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }; s; }),
2103 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
2104 ({ char *s; \
2105 switch (usb_pipetype (urb->pipe)) { \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002106 case PIPE_CONTROL: \
2107 s = ""; \
2108 break; \
2109 case PIPE_BULK: \
2110 s = "-bulk"; \
2111 break; \
2112 case PIPE_INTERRUPT: \
2113 s = "-int"; \
2114 break; \
2115 default: \
2116 s = "-iso"; \
2117 break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }; s;}),
2119 urb->actual_length, urb->transfer_buffer_length);
2120}
2121
2122static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -04002123show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
2125 struct usb_hcd *hcd = dev_get_drvdata (dev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002126 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 struct urbp *urbp;
2128 size_t size = 0;
2129 unsigned long flags;
2130
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002131 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2132 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 size_t temp;
2134
2135 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
2136 buf += temp;
2137 size += temp;
2138 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002139 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 return size;
2142}
2143static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
2144
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002145static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2146{
2147 init_timer(&dum_hcd->timer);
2148 dum_hcd->timer.function = dummy_timer;
2149 dum_hcd->timer.data = (unsigned long)dum_hcd;
2150 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2151 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2152 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2153 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2154 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2155#ifdef CONFIG_USB_OTG
2156 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2157#endif
2158 return 0;
2159
2160 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2161 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2162}
2163
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002164static int dummy_start(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002166 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 /*
2169 * MASTER side init ... we emulate a root hub that'll only ever
2170 * talk to one device (the slave side). Also appears in sysfs,
2171 * just like more familiar pci-based HCDs.
2172 */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002173 if (!usb_hcd_is_primary_hcd(hcd))
2174 return dummy_start_ss(dum_hcd);
2175
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002176 spin_lock_init(&dum_hcd->dum->lock);
2177 init_timer(&dum_hcd->timer);
2178 dum_hcd->timer.function = dummy_timer;
2179 dum_hcd->timer.data = (unsigned long)dum_hcd;
2180 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002182 INIT_LIST_HEAD(&dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Alan Sterncaf29f62007-12-06 11:10:39 -05002184 hcd->power_budget = POWER_BUDGET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 hcd->state = HC_STATE_RUNNING;
Alan Stern685eb932005-05-03 16:27:26 -04002186 hcd->uses_new_polling = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Alan Stern5742b0c2005-05-02 11:25:17 -04002188#ifdef CONFIG_USB_OTG
2189 hcd->self.otg_port = 1;
2190#endif
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002193 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
2196static void dummy_stop (struct usb_hcd *hcd)
2197{
2198 struct dummy *dum;
2199
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002200 dum = (hcd_to_dummy_hcd(hcd))->dum;
2201 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2202 usb_gadget_unregister_driver(dum->driver);
2203 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204}
2205
2206/*-------------------------------------------------------------------------*/
2207
2208static int dummy_h_get_frame (struct usb_hcd *hcd)
2209{
2210 return dummy_g_get_frame (NULL);
2211}
2212
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002213static int dummy_setup(struct usb_hcd *hcd)
2214{
2215 if (usb_hcd_is_primary_hcd(hcd)) {
2216 the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
2217 the_controller.hs_hcd->dum = &the_controller;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002218 /*
2219 * Mark the first roothub as being USB 2.0.
2220 * The USB 3.0 roothub will be registered later by
2221 * dummy_hcd_probe()
2222 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002223 hcd->speed = HCD_USB2;
2224 hcd->self.root_hub->speed = USB_SPEED_HIGH;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002225 } else {
2226 the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
2227 the_controller.ss_hcd->dum = &the_controller;
2228 hcd->speed = HCD_USB3;
2229 hcd->self.root_hub->speed = USB_SPEED_SUPER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002230 }
2231 return 0;
2232}
2233
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002234/* Change a group of bulk endpoints to support multiple stream IDs */
2235int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
2236 struct usb_host_endpoint **eps, unsigned int num_eps,
2237 unsigned int num_streams, gfp_t mem_flags)
2238{
2239 if (hcd->speed != HCD_USB3)
2240 dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
2241 "%s() - ERROR! Not supported for USB2.0 roothub\n",
2242 __func__);
2243 return 0;
2244}
2245
2246/* Reverts a group of bulk endpoints back to not using stream IDs. */
2247int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
2248 struct usb_host_endpoint **eps, unsigned int num_eps,
2249 gfp_t mem_flags)
2250{
2251 if (hcd->speed != HCD_USB3)
2252 dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
2253 "%s() - ERROR! Not supported for USB2.0 roothub\n",
2254 __func__);
2255 return 0;
2256}
2257
2258static struct hc_driver dummy_hcd = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 .description = (char *) driver_name,
2260 .product_desc = "Dummy host controller",
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002261 .hcd_priv_size = sizeof(struct dummy_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002263 .flags = HCD_USB3 | HCD_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002265 .reset = dummy_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 .start = dummy_start,
2267 .stop = dummy_stop,
2268
2269 .urb_enqueue = dummy_urb_enqueue,
2270 .urb_dequeue = dummy_urb_dequeue,
2271
2272 .get_frame_number = dummy_h_get_frame,
2273
2274 .hub_status_data = dummy_hub_status,
2275 .hub_control = dummy_hub_control,
Alan Stern0c0382e2005-10-13 17:08:02 -04002276 .bus_suspend = dummy_bus_suspend,
2277 .bus_resume = dummy_bus_resume,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002278
2279 .alloc_streams = dummy_alloc_streams,
2280 .free_streams = dummy_free_streams,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281};
2282
Alan Stern8364d6b2005-11-14 12:16:30 -05002283static int dummy_hcd_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002285 struct usb_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002286 struct usb_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 int retval;
2288
Alan Stern8364d6b2005-11-14 12:16:30 -05002289 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002291 if (!mod_data.is_super_speed)
2292 dummy_hcd.flags = HCD_USB2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002293 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2294 if (!hs_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return -ENOMEM;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002296 hs_hcd->has_tt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002298 retval = usb_add_hcd(hs_hcd, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 if (retval != 0) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002300 usb_put_hcd(hs_hcd);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002301 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002303
2304 if (mod_data.is_super_speed) {
2305 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2306 dev_name(&pdev->dev), hs_hcd);
2307 if (!ss_hcd) {
2308 retval = -ENOMEM;
2309 goto dealloc_usb2_hcd;
2310 }
2311
2312 retval = usb_add_hcd(ss_hcd, 0, 0);
2313 if (retval)
2314 goto put_usb3_hcd;
2315 }
2316 return 0;
2317
2318put_usb3_hcd:
2319 usb_put_hcd(ss_hcd);
2320dealloc_usb2_hcd:
2321 usb_put_hcd(hs_hcd);
2322 the_controller.hs_hcd = the_controller.ss_hcd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 return retval;
2324}
2325
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002326static int dummy_hcd_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002328 struct dummy *dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002330 dum = (hcd_to_dummy_hcd(platform_get_drvdata(pdev)))->dum;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002331
2332 if (dum->ss_hcd) {
2333 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2334 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2335 }
2336
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002337 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2338 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002339
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002340 the_controller.hs_hcd = NULL;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002341 the_controller.ss_hcd = NULL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002342
Alan Sternd9b76252005-05-03 16:15:43 -04002343 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344}
2345
Alan Stern8364d6b2005-11-14 12:16:30 -05002346static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
Alan Stern391eca92005-05-10 15:34:16 -04002347{
2348 struct usb_hcd *hcd;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002349 struct dummy_hcd *dum_hcd;
Alan Stern3cf0a222005-11-29 12:08:15 -05002350 int rc = 0;
Alan Stern391eca92005-05-10 15:34:16 -04002351
Harvey Harrison441b62c2008-03-03 16:08:34 -08002352 dev_dbg (&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002353
Alan Stern3cf0a222005-11-29 12:08:15 -05002354 hcd = platform_get_drvdata (pdev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002355 dum_hcd = hcd_to_dummy_hcd(hcd);
2356 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern3cf0a222005-11-29 12:08:15 -05002357 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2358 rc = -EBUSY;
2359 } else
2360 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2361 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002362}
2363
Alan Stern8364d6b2005-11-14 12:16:30 -05002364static int dummy_hcd_resume (struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04002365{
2366 struct usb_hcd *hcd;
2367
Harvey Harrison441b62c2008-03-03 16:08:34 -08002368 dev_dbg (&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002369
Alan Stern3cf0a222005-11-29 12:08:15 -05002370 hcd = platform_get_drvdata (pdev);
2371 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Alan Stern391eca92005-05-10 15:34:16 -04002372 usb_hcd_poll_rh_status (hcd);
2373 return 0;
2374}
2375
Russell King3ae5eae2005-11-09 22:32:44 +00002376static struct platform_driver dummy_hcd_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04002377 .probe = dummy_hcd_probe,
2378 .remove = dummy_hcd_remove,
Alan Stern391eca92005-05-10 15:34:16 -04002379 .suspend = dummy_hcd_suspend,
2380 .resume = dummy_hcd_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002381 .driver = {
2382 .name = (char *) driver_name,
2383 .owner = THIS_MODULE,
2384 },
Alan Sternd9b76252005-05-03 16:15:43 -04002385};
2386
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387/*-------------------------------------------------------------------------*/
2388
Alan Sterna89a2cd2008-04-07 15:03:25 -04002389static struct platform_device *the_udc_pdev;
2390static struct platform_device *the_hcd_pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
2392static int __init init (void)
2393{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002394 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396 if (usb_disabled ())
2397 return -ENODEV;
Alan Sternd9b76252005-05-03 16:15:43 -04002398
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +03002399 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2400 return -EINVAL;
2401
Alan Sterna89a2cd2008-04-07 15:03:25 -04002402 the_hcd_pdev = platform_device_alloc(driver_name, -1);
2403 if (!the_hcd_pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 return retval;
Alan Sterna89a2cd2008-04-07 15:03:25 -04002405 the_udc_pdev = platform_device_alloc(gadget_name, -1);
2406 if (!the_udc_pdev)
2407 goto err_alloc_udc;
Alan Sternd9b76252005-05-03 16:15:43 -04002408
Alan Sterna89a2cd2008-04-07 15:03:25 -04002409 retval = platform_driver_register(&dummy_hcd_driver);
2410 if (retval < 0)
2411 goto err_register_hcd_driver;
2412 retval = platform_driver_register(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002413 if (retval < 0)
2414 goto err_register_udc_driver;
2415
Alan Sterna89a2cd2008-04-07 15:03:25 -04002416 retval = platform_device_add(the_hcd_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002417 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002418 goto err_add_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002419 if (!the_controller.hs_hcd ||
2420 (!the_controller.ss_hcd && mod_data.is_super_speed)) {
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002421 /*
2422 * The hcd was added successfully but its probe function failed
2423 * for some reason.
2424 */
2425 retval = -EINVAL;
2426 goto err_add_udc;
2427 }
Alan Sterna89a2cd2008-04-07 15:03:25 -04002428 retval = platform_device_add(the_udc_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002429 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002430 goto err_add_udc;
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002431 if (!platform_get_drvdata(the_udc_pdev)) {
2432 /*
2433 * The udc was added successfully but its probe function failed
2434 * for some reason.
2435 */
2436 retval = -EINVAL;
2437 goto err_probe_udc;
2438 }
Alan Sternd9b76252005-05-03 16:15:43 -04002439 return retval;
2440
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002441err_probe_udc:
2442 platform_device_del(the_udc_pdev);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002443err_add_udc:
2444 platform_device_del(the_hcd_pdev);
2445err_add_hcd:
2446 platform_driver_unregister(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002447err_register_udc_driver:
Alan Sterna89a2cd2008-04-07 15:03:25 -04002448 platform_driver_unregister(&dummy_hcd_driver);
2449err_register_hcd_driver:
2450 platform_device_put(the_udc_pdev);
2451err_alloc_udc:
2452 platform_device_put(the_hcd_pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 return retval;
2454}
2455module_init (init);
2456
2457static void __exit cleanup (void)
2458{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002459 platform_device_unregister(the_udc_pdev);
2460 platform_device_unregister(the_hcd_pdev);
2461 platform_driver_unregister(&dummy_udc_driver);
2462 platform_driver_unregister(&dummy_hcd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463}
2464module_exit (cleanup);