blob: d040dc406b122779fa15efcebd261d9b98df5336 [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.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24
25/*
26 * This exposes a device side "USB gadget" API, driven by requests to a
27 * Linux-USB host controller driver. USB traffic is simulated; there's
28 * no need for USB hardware. Use this with two other drivers:
29 *
30 * - Gadget driver, responding to requests (slave);
31 * - Host-side device driver, as already familiar in Linux.
32 *
33 * Having this all in one kernel can help some stages of development,
34 * bypassing some hardware (and driver) issues. UML could help too.
35 */
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/module.h>
38#include <linux/kernel.h>
39#include <linux/delay.h>
40#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/errno.h>
43#include <linux/init.h>
44#include <linux/timer.h>
45#include <linux/list.h>
46#include <linux/interrupt.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010047#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/usb.h>
David Brownell9454a572007-10-04 18:05:17 -070049#include <linux/usb/gadget.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020050#include <linux/usb/hcd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#include <asm/byteorder.h>
53#include <asm/io.h>
54#include <asm/irq.h>
55#include <asm/system.h>
56#include <asm/unaligned.h>
57
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define DRIVER_DESC "USB Host+Gadget Emulator"
Alan Stern391eca92005-05-10 15:34:16 -040060#define DRIVER_VERSION "02 May 2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Alan Sterncaf29f62007-12-06 11:10:39 -050062#define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static const char driver_name [] = "dummy_hcd";
65static const char driver_desc [] = "USB Host+Gadget Emulator";
66
67static const char gadget_name [] = "dummy_udc";
68
69MODULE_DESCRIPTION (DRIVER_DESC);
70MODULE_AUTHOR ("David Brownell");
71MODULE_LICENSE ("GPL");
72
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +030073struct dummy_hcd_module_parameters {
74 bool is_super_speed;
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +030075 bool is_high_speed;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +030076};
77
78static struct dummy_hcd_module_parameters mod_data = {
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +030079 .is_super_speed = false,
80 .is_high_speed = true,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +030081};
82module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
83MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +030084module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
85MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/*-------------------------------------------------------------------------*/
87
88/* gadget side driver data structres */
89struct dummy_ep {
90 struct list_head queue;
91 unsigned long last_io; /* jiffies timestamp */
92 struct usb_gadget *gadget;
93 const struct usb_endpoint_descriptor *desc;
94 struct usb_ep ep;
95 unsigned halted : 1;
Alan Stern851a5262008-08-14 15:48:30 -040096 unsigned wedged : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 unsigned already_seen : 1;
98 unsigned setup_stage : 1;
99};
100
101struct dummy_request {
102 struct list_head queue; /* ep's requests */
103 struct usb_request req;
104};
105
106static inline struct dummy_ep *usb_ep_to_dummy_ep (struct usb_ep *_ep)
107{
108 return container_of (_ep, struct dummy_ep, ep);
109}
110
111static inline struct dummy_request *usb_request_to_dummy_request
112 (struct usb_request *_req)
113{
114 return container_of (_req, struct dummy_request, req);
115}
116
117/*-------------------------------------------------------------------------*/
118
119/*
120 * Every device has ep0 for control requests, plus up to 30 more endpoints,
121 * in one of two types:
122 *
123 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
124 * number can be changed. Names like "ep-a" are used for this type.
125 *
126 * - Fixed Function: in other cases. some characteristics may be mutable;
127 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
128 *
129 * Gadget drivers are responsible for not setting up conflicting endpoint
130 * configurations, illegal or unsupported packet lengths, and so on.
131 */
132
133static const char ep0name [] = "ep0";
134
135static const char *const ep_name [] = {
136 ep0name, /* everyone has ep0 */
137
138 /* act like a net2280: high speed, six configurable endpoints */
139 "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
140
141 /* or like pxa250: fifteen fixed function endpoints */
142 "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
143 "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
144 "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
145 "ep15in-int",
146
147 /* or like sa1100: two fixed function endpoints */
148 "ep1out-bulk", "ep2in-bulk",
149};
Tobias Klauser52950ed2005-12-11 16:20:08 +0100150#define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Alan Sternd9b76252005-05-03 16:15:43 -0400152/*-------------------------------------------------------------------------*/
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#define FIFO_SIZE 64
155
156struct urbp {
157 struct urb *urb;
158 struct list_head urbp_list;
159};
160
Alan Stern391eca92005-05-10 15:34:16 -0400161
162enum dummy_rh_state {
163 DUMMY_RH_RESET,
164 DUMMY_RH_SUSPENDED,
165 DUMMY_RH_RUNNING
166};
167
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300168struct dummy_hcd {
169 struct dummy *dum;
170 enum dummy_rh_state rh_state;
171 struct timer_list timer;
172 u32 port_status;
173 u32 old_status;
174 unsigned long re_timeout;
175
176 struct usb_device *udev;
177 struct list_head urbp_list;
178
179 unsigned active:1;
180 unsigned old_active:1;
181 unsigned resuming:1;
182};
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184struct dummy {
185 spinlock_t lock;
186
187 /*
188 * SLAVE/GADGET side support
189 */
190 struct dummy_ep ep [DUMMY_ENDPOINTS];
191 int address;
192 struct usb_gadget gadget;
193 struct usb_gadget_driver *driver;
194 struct dummy_request fifo_req;
195 u8 fifo_buf [FIFO_SIZE];
196 u16 devstatus;
Alan Stern391eca92005-05-10 15:34:16 -0400197 unsigned udc_suspended:1;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400198 unsigned pullup:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 /*
201 * MASTER/HOST side support
202 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300203 struct dummy_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300204 struct dummy_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205};
206
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300207static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300209 return (struct dummy_hcd *) (hcd->hcd_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300212static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 return container_of((void *) dum, struct usb_hcd, hcd_priv);
215}
216
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300217static inline struct device *dummy_dev(struct dummy_hcd *dum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300219 return dummy_hcd_to_hcd(dum)->self.controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Alan Sternd9b76252005-05-03 16:15:43 -0400222static inline struct device *udc_dev (struct dummy *dum)
223{
224 return dum->gadget.dev.parent;
225}
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
228{
229 return container_of (ep->gadget, struct dummy, gadget);
230}
231
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300232static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300234 struct dummy *dum = container_of(gadget, struct dummy, gadget);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300235 if (dum->gadget.speed == USB_SPEED_SUPER)
236 return dum->ss_hcd;
237 else
238 return dum->hs_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
242{
243 return container_of (dev, struct dummy, gadget.dev);
244}
245
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300246static struct dummy the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248/*-------------------------------------------------------------------------*/
249
Alan Sternf1c39fa2005-05-03 16:24:04 -0400250/* SLAVE/GADGET SIDE UTILITY ROUTINES */
251
252/* called with spinlock held */
253static void nuke (struct dummy *dum, struct dummy_ep *ep)
254{
255 while (!list_empty (&ep->queue)) {
256 struct dummy_request *req;
257
258 req = list_entry (ep->queue.next, struct dummy_request, queue);
259 list_del_init (&req->queue);
260 req->req.status = -ESHUTDOWN;
261
262 spin_unlock (&dum->lock);
263 req->req.complete (&ep->ep, &req->req);
264 spin_lock (&dum->lock);
265 }
266}
267
268/* caller must hold lock */
269static void
270stop_activity (struct dummy *dum)
271{
272 struct dummy_ep *ep;
273
274 /* prevent any more requests */
275 dum->address = 0;
276
277 /* The timer is left running so that outstanding URBs can fail */
278
279 /* nuke any pending requests first, so driver i/o is quiesced */
280 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
281 nuke (dum, ep);
282
283 /* driver now does any non-usb quiescing necessary */
284}
285
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300286/**
287 * set_link_state_by_speed() - Sets the current state of the link according to
288 * the hcd speed
289 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
290 *
291 * This function updates the port_status according to the link state and the
292 * speed of the hcd.
293 */
294static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
295{
296 struct dummy *dum = dum_hcd->dum;
297
298 if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
299 if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
300 dum_hcd->port_status = 0;
301 } else if (!dum->pullup || dum->udc_suspended) {
302 /* UDC suspend must cause a disconnect */
303 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
304 USB_PORT_STAT_ENABLE);
305 if ((dum_hcd->old_status &
306 USB_PORT_STAT_CONNECTION) != 0)
307 dum_hcd->port_status |=
308 (USB_PORT_STAT_C_CONNECTION << 16);
309 } else {
310 /* device is connected and not suspended */
311 dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
312 USB_PORT_STAT_SPEED_5GBPS) ;
313 if ((dum_hcd->old_status &
314 USB_PORT_STAT_CONNECTION) == 0)
315 dum_hcd->port_status |=
316 (USB_PORT_STAT_C_CONNECTION << 16);
317 if ((dum_hcd->port_status &
318 USB_PORT_STAT_ENABLE) == 1 &&
319 (dum_hcd->port_status &
320 USB_SS_PORT_LS_U0) == 1 &&
321 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
322 dum_hcd->active = 1;
323 }
324 } else {
325 if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
326 dum_hcd->port_status = 0;
327 } else if (!dum->pullup || dum->udc_suspended) {
328 /* UDC suspend must cause a disconnect */
329 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
330 USB_PORT_STAT_ENABLE |
331 USB_PORT_STAT_LOW_SPEED |
332 USB_PORT_STAT_HIGH_SPEED |
333 USB_PORT_STAT_SUSPEND);
334 if ((dum_hcd->old_status &
335 USB_PORT_STAT_CONNECTION) != 0)
336 dum_hcd->port_status |=
337 (USB_PORT_STAT_C_CONNECTION << 16);
338 } else {
339 dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
340 if ((dum_hcd->old_status &
341 USB_PORT_STAT_CONNECTION) == 0)
342 dum_hcd->port_status |=
343 (USB_PORT_STAT_C_CONNECTION << 16);
344 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
345 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
346 else if ((dum_hcd->port_status &
347 USB_PORT_STAT_SUSPEND) == 0 &&
348 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
349 dum_hcd->active = 1;
350 }
351 }
352}
353
Alan Sternf1c39fa2005-05-03 16:24:04 -0400354/* caller must hold lock */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300355static void set_link_state(struct dummy_hcd *dum_hcd)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400356{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300357 struct dummy *dum = dum_hcd->dum;
358
359 dum_hcd->active = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300360 if (dum->pullup)
361 if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
362 dum->gadget.speed != USB_SPEED_SUPER) ||
363 (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
364 dum->gadget.speed == USB_SPEED_SUPER))
365 return;
Alan Stern391eca92005-05-10 15:34:16 -0400366
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300367 set_link_state_by_speed(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400368
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300369 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
370 dum_hcd->active)
371 dum_hcd->resuming = 0;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400372
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300373 /* if !connected or reset */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300374 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
375 (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300376 /*
377 * We're connected and not reset (reset occurred now),
378 * and driver attached - disconnect!
379 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300380 if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300381 (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
382 dum->driver) {
383 stop_activity(dum);
384 spin_unlock(&dum->lock);
385 dum->driver->disconnect(&dum->gadget);
386 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400387 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300388 } else if (dum_hcd->active != dum_hcd->old_active) {
389 if (dum_hcd->old_active && dum->driver->suspend) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300390 spin_unlock(&dum->lock);
391 dum->driver->suspend(&dum->gadget);
392 spin_lock(&dum->lock);
393 } else if (!dum_hcd->old_active && dum->driver->resume) {
394 spin_unlock(&dum->lock);
395 dum->driver->resume(&dum->gadget);
396 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400397 }
398 }
399
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300400 dum_hcd->old_status = dum_hcd->port_status;
401 dum_hcd->old_active = dum_hcd->active;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400402}
403
404/*-------------------------------------------------------------------------*/
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/* SLAVE/GADGET SIDE DRIVER
407 *
408 * This only tracks gadget state. All the work is done when the host
409 * side tries some (emulated) i/o operation. Real device controller
410 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
411 */
412
413#define is_enabled(dum) \
414 (dum->port_status & USB_PORT_STAT_ENABLE)
415
416static int
417dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
418{
419 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300420 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct dummy_ep *ep;
422 unsigned max;
423 int retval;
424
425 ep = usb_ep_to_dummy_ep (_ep);
426 if (!_ep || !desc || ep->desc || _ep->name == ep0name
427 || desc->bDescriptorType != USB_DT_ENDPOINT)
428 return -EINVAL;
429 dum = ep_to_dummy (ep);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300430 if (!dum->driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return -ESHUTDOWN;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300432 if (dum->gadget.speed == USB_SPEED_SUPER)
433 dum_hcd = dum->ss_hcd;
434 else
435 dum_hcd = dum->hs_hcd;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300436 if (!is_enabled(dum_hcd))
437 return -ESHUTDOWN;
438
439 /*
440 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
441 * maximum packet size.
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300442 * For SS devices the wMaxPacketSize is limited by 1024.
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300443 */
444 max = le16_to_cpu(desc->wMaxPacketSize) & 0x7ff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /* drivers must not request bad settings, since lower levels
447 * (hardware or its drivers) may not check. some endpoints
448 * can't do iso, many have maxpacket limitations, etc.
449 *
450 * since this "hardware" driver is here to help debugging, we
451 * have some extra sanity checks. (there could be more though,
452 * especially for "ep9out" style fixed function ones.)
453 */
454 retval = -EINVAL;
455 switch (desc->bmAttributes & 0x03) {
456 case USB_ENDPOINT_XFER_BULK:
457 if (strstr (ep->ep.name, "-iso")
458 || strstr (ep->ep.name, "-int")) {
459 goto done;
460 }
461 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300462 case USB_SPEED_SUPER:
463 if (max == 1024)
464 break;
465 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 case USB_SPEED_HIGH:
467 if (max == 512)
468 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700469 goto done;
470 case USB_SPEED_FULL:
471 if (max == 8 || max == 16 || max == 32 || max == 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /* we'll fake any legal size */
473 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700474 /* save a return statement */
475 default:
476 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478 break;
479 case USB_ENDPOINT_XFER_INT:
480 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
481 goto done;
482 /* real hardware might not handle all packet sizes */
483 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300484 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 case USB_SPEED_HIGH:
486 if (max <= 1024)
487 break;
488 /* save a return statement */
489 case USB_SPEED_FULL:
490 if (max <= 64)
491 break;
492 /* save a return statement */
493 default:
494 if (max <= 8)
495 break;
496 goto done;
497 }
498 break;
499 case USB_ENDPOINT_XFER_ISOC:
500 if (strstr (ep->ep.name, "-bulk")
501 || strstr (ep->ep.name, "-int"))
502 goto done;
503 /* real hardware might not handle all packet sizes */
504 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300505 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 case USB_SPEED_HIGH:
507 if (max <= 1024)
508 break;
509 /* save a return statement */
510 case USB_SPEED_FULL:
511 if (max <= 1023)
512 break;
513 /* save a return statement */
514 default:
515 goto done;
516 }
517 break;
518 default:
519 /* few chips support control except on ep0 */
520 goto done;
521 }
522
523 _ep->maxpacket = max;
524 ep->desc = desc;
525
Alan Sternd9b76252005-05-03 16:15:43 -0400526 dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 _ep->name,
528 desc->bEndpointAddress & 0x0f,
529 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
530 ({ char *val;
531 switch (desc->bmAttributes & 0x03) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +0300532 case USB_ENDPOINT_XFER_BULK:
533 val = "bulk";
534 break;
535 case USB_ENDPOINT_XFER_ISOC:
536 val = "iso";
537 break;
538 case USB_ENDPOINT_XFER_INT:
539 val = "intr";
540 break;
541 default:
542 val = "ctrl";
543 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }; val; }),
545 max);
546
547 /* at this point real hardware should be NAKing transfers
548 * to that endpoint, until a buffer is queued to it.
549 */
Alan Stern851a5262008-08-14 15:48:30 -0400550 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 retval = 0;
552done:
553 return retval;
554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556static int dummy_disable (struct usb_ep *_ep)
557{
558 struct dummy_ep *ep;
559 struct dummy *dum;
560 unsigned long flags;
561 int retval;
562
563 ep = usb_ep_to_dummy_ep (_ep);
564 if (!_ep || !ep->desc || _ep->name == ep0name)
565 return -EINVAL;
566 dum = ep_to_dummy (ep);
567
568 spin_lock_irqsave (&dum->lock, flags);
569 ep->desc = NULL;
570 retval = 0;
571 nuke (dum, ep);
572 spin_unlock_irqrestore (&dum->lock, flags);
573
Alan Sternd9b76252005-05-03 16:15:43 -0400574 dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return retval;
576}
577
578static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -0400579dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
581 struct dummy_ep *ep;
582 struct dummy_request *req;
583
584 if (!_ep)
585 return NULL;
586 ep = usb_ep_to_dummy_ep (_ep);
587
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800588 req = kzalloc(sizeof(*req), mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (!req)
590 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 INIT_LIST_HEAD (&req->queue);
592 return &req->req;
593}
594
595static void
596dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
597{
598 struct dummy_ep *ep;
599 struct dummy_request *req;
600
601 ep = usb_ep_to_dummy_ep (_ep);
602 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
603 return;
604
605 req = usb_request_to_dummy_request (_req);
606 WARN_ON (!list_empty (&req->queue));
607 kfree (req);
608}
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610static void
611fifo_complete (struct usb_ep *ep, struct usb_request *req)
612{
613}
614
615static int
Olav Kongas5db539e2005-06-23 20:25:36 +0300616dummy_queue (struct usb_ep *_ep, struct usb_request *_req,
Al Viro55016f12005-10-21 03:21:58 -0400617 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 struct dummy_ep *ep;
620 struct dummy_request *req;
621 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300622 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 unsigned long flags;
624
625 req = usb_request_to_dummy_request (_req);
626 if (!_req || !list_empty (&req->queue) || !_req->complete)
627 return -EINVAL;
628
629 ep = usb_ep_to_dummy_ep (_ep);
630 if (!_ep || (!ep->desc && _ep->name != ep0name))
631 return -EINVAL;
632
633 dum = ep_to_dummy (ep);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300634 if (dum->gadget.speed == USB_SPEED_SUPER)
635 dum_hcd = dum->ss_hcd;
636 else
637 dum_hcd = dum->hs_hcd;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300638 if (!dum->driver || !is_enabled(dum_hcd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return -ESHUTDOWN;
640
641#if 0
Alan Sternd9b76252005-05-03 16:15:43 -0400642 dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 ep, _req, _ep->name, _req->length, _req->buf);
644#endif
645
646 _req->status = -EINPROGRESS;
647 _req->actual = 0;
648 spin_lock_irqsave (&dum->lock, flags);
649
650 /* implement an emulated single-request FIFO */
651 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
652 list_empty (&dum->fifo_req.queue) &&
653 list_empty (&ep->queue) &&
654 _req->length <= FIFO_SIZE) {
655 req = &dum->fifo_req;
656 req->req = *_req;
657 req->req.buf = dum->fifo_buf;
658 memcpy (dum->fifo_buf, _req->buf, _req->length);
659 req->req.context = dum;
660 req->req.complete = fifo_complete;
661
David Brownellc728df72008-07-26 08:06:24 -0700662 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 spin_unlock (&dum->lock);
664 _req->actual = _req->length;
665 _req->status = 0;
666 _req->complete (_ep, _req);
667 spin_lock (&dum->lock);
David Brownellc728df72008-07-26 08:06:24 -0700668 } else
669 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 spin_unlock_irqrestore (&dum->lock, flags);
671
672 /* real hardware would likely enable transfers here, in case
673 * it'd been left NAKing.
674 */
675 return 0;
676}
677
678static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
679{
680 struct dummy_ep *ep;
681 struct dummy *dum;
682 int retval = -EINVAL;
683 unsigned long flags;
684 struct dummy_request *req = NULL;
685
686 if (!_ep || !_req)
687 return retval;
688 ep = usb_ep_to_dummy_ep (_ep);
689 dum = ep_to_dummy (ep);
690
691 if (!dum->driver)
692 return -ESHUTDOWN;
693
Alan Sternb4dbda12006-07-28 17:07:34 -0400694 local_irq_save (flags);
695 spin_lock (&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 list_for_each_entry (req, &ep->queue, queue) {
697 if (&req->req == _req) {
698 list_del_init (&req->queue);
699 _req->status = -ECONNRESET;
700 retval = 0;
701 break;
702 }
703 }
Alan Sternb4dbda12006-07-28 17:07:34 -0400704 spin_unlock (&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 if (retval == 0) {
Alan Sternd9b76252005-05-03 16:15:43 -0400707 dev_dbg (udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 "dequeued req %p from %s, len %d buf %p\n",
709 req, _ep->name, _req->length, _req->buf);
710 _req->complete (_ep, _req);
711 }
Alan Sternb4dbda12006-07-28 17:07:34 -0400712 local_irq_restore (flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return retval;
714}
715
716static int
Alan Stern851a5262008-08-14 15:48:30 -0400717dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 struct dummy_ep *ep;
720 struct dummy *dum;
721
722 if (!_ep)
723 return -EINVAL;
724 ep = usb_ep_to_dummy_ep (_ep);
725 dum = ep_to_dummy (ep);
726 if (!dum->driver)
727 return -ESHUTDOWN;
728 if (!value)
Alan Stern851a5262008-08-14 15:48:30 -0400729 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
731 !list_empty (&ep->queue))
732 return -EAGAIN;
Alan Stern851a5262008-08-14 15:48:30 -0400733 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 ep->halted = 1;
Alan Stern851a5262008-08-14 15:48:30 -0400735 if (wedged)
736 ep->wedged = 1;
737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 /* FIXME clear emulated data toggle too */
739 return 0;
740}
741
Alan Stern851a5262008-08-14 15:48:30 -0400742static int
743dummy_set_halt(struct usb_ep *_ep, int value)
744{
745 return dummy_set_halt_and_wedge(_ep, value, 0);
746}
747
748static int dummy_set_wedge(struct usb_ep *_ep)
749{
750 if (!_ep || _ep->name == ep0name)
751 return -EINVAL;
752 return dummy_set_halt_and_wedge(_ep, 1, 1);
753}
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755static const struct usb_ep_ops dummy_ep_ops = {
756 .enable = dummy_enable,
757 .disable = dummy_disable,
758
759 .alloc_request = dummy_alloc_request,
760 .free_request = dummy_free_request,
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 .queue = dummy_queue,
763 .dequeue = dummy_dequeue,
764
765 .set_halt = dummy_set_halt,
Alan Stern851a5262008-08-14 15:48:30 -0400766 .set_wedge = dummy_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767};
768
769/*-------------------------------------------------------------------------*/
770
771/* there are both host and device side versions of this call ... */
772static int dummy_g_get_frame (struct usb_gadget *_gadget)
773{
774 struct timeval tv;
775
776 do_gettimeofday (&tv);
777 return tv.tv_usec / 1000;
778}
779
780static int dummy_wakeup (struct usb_gadget *_gadget)
781{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300782 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300784 dum_hcd = gadget_to_dummy_hcd(_gadget);
785 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
Alan Stern5742b0c2005-05-02 11:25:17 -0400786 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return -EINVAL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300788 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
Alan Stern391eca92005-05-10 15:34:16 -0400789 return -ENOLINK;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300790 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
791 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -0400792 return -EIO;
793
794 /* FIXME: What if the root hub is suspended but the port isn't? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 /* hub notices our request, issues downstream resume, etc */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300797 dum_hcd->resuming = 1;
798 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
799 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return 0;
801}
802
803static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
804{
805 struct dummy *dum;
806
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300807 dum = (gadget_to_dummy_hcd(_gadget))->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (value)
809 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
810 else
811 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
812 return 0;
813}
814
Alan Sternf1c39fa2005-05-03 16:24:04 -0400815static int dummy_pullup (struct usb_gadget *_gadget, int value)
816{
817 struct dummy *dum;
818 unsigned long flags;
819
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300820 dum = gadget_to_dummy_hcd(_gadget)->dum;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400821 spin_lock_irqsave (&dum->lock, flags);
822 dum->pullup = (value != 0);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300823 set_link_state((dum->gadget.speed == USB_SPEED_SUPER ?
824 dum->ss_hcd : dum->hs_hcd));
Alan Sternf1c39fa2005-05-03 16:24:04 -0400825 spin_unlock_irqrestore (&dum->lock, flags);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300826 usb_hcd_poll_rh_status((dum->gadget.speed == USB_SPEED_SUPER ?
827 dummy_hcd_to_hcd(dum->ss_hcd) :
828 dummy_hcd_to_hcd(dum->hs_hcd)));
Alan Sternf1c39fa2005-05-03 16:24:04 -0400829 return 0;
830}
831
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300832static int dummy_udc_start(struct usb_gadget_driver *driver,
833 int (*bind)(struct usb_gadget *));
834static int dummy_udc_stop(struct usb_gadget_driver *driver);
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836static const struct usb_gadget_ops dummy_ops = {
837 .get_frame = dummy_g_get_frame,
838 .wakeup = dummy_wakeup,
839 .set_selfpowered = dummy_set_selfpowered,
Alan Sternf1c39fa2005-05-03 16:24:04 -0400840 .pullup = dummy_pullup,
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300841 .start = dummy_udc_start,
842 .stop = dummy_udc_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843};
844
845/*-------------------------------------------------------------------------*/
846
847/* "function" sysfs attribute */
848static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400849show_function (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
851 struct dummy *dum = gadget_dev_to_dummy (dev);
852
853 if (!dum->driver || !dum->driver->function)
854 return 0;
855 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
856}
Alan Sterncc095b02005-05-10 15:28:38 -0400857static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859/*-------------------------------------------------------------------------*/
860
861/*
862 * Driver registration/unregistration.
863 *
864 * This is basically hardware-specific; there's usually only one real USB
865 * device (not host) controller since that's how USB devices are intended
866 * to work. So most implementations of these api calls will rely on the
867 * fact that only one driver will ever bind to the hardware. But curious
868 * hardware can be built with discrete components, so the gadget API doesn't
869 * require that assumption.
870 *
871 * For this emulator, it might be convenient to create a usb slave device
872 * for each driver that registers: just add to a big root hub.
873 */
874
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300875static int dummy_udc_start(struct usb_gadget_driver *driver,
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +0200876 int (*bind)(struct usb_gadget *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300878 struct dummy *dum = &the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 int retval, i;
880
881 if (!dum)
882 return -EINVAL;
883 if (dum->driver)
884 return -EBUSY;
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +0200885 if (!bind || !driver->setup || driver->speed == USB_SPEED_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return -EINVAL;
887
888 /*
889 * SLAVE side init ... the layer above hardware, which
890 * can't enumerate without help from the driver we're binding.
891 */
Alan Stern5742b0c2005-05-02 11:25:17 -0400892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 dum->devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 INIT_LIST_HEAD (&dum->gadget.ep_list);
896 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
897 struct dummy_ep *ep = &dum->ep [i];
898
899 if (!ep_name [i])
900 break;
901 ep->ep.name = ep_name [i];
902 ep->ep.ops = &dummy_ep_ops;
903 list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
Alan Stern851a5262008-08-14 15:48:30 -0400904 ep->halted = ep->wedged = ep->already_seen =
905 ep->setup_stage = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 ep->ep.maxpacket = ~0;
907 ep->last_io = jiffies;
908 ep->gadget = &dum->gadget;
909 ep->desc = NULL;
910 INIT_LIST_HEAD (&ep->queue);
911 }
912
913 dum->gadget.ep0 = &dum->ep [0].ep;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300914 if (mod_data.is_super_speed)
915 dum->gadget.speed = driver->speed;
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +0300916 else if (mod_data.is_high_speed)
917 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH, driver->speed);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300918 else
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +0300919 dum->gadget.speed = USB_SPEED_FULL;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300920 if (dum->gadget.speed < driver->speed)
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +0300921 dev_dbg(udc_dev(dum), "This device can perform faster"
922 " if you connect it to a %s port...\n",
923 (driver->speed == USB_SPEED_SUPER ?
924 "SuperSpeed" : "HighSpeed"));
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300925
926 if (dum->gadget.speed == USB_SPEED_SUPER) {
927 for (i = 0; i < DUMMY_ENDPOINTS; i++)
928 dum->ep[i].ep.max_streams = 0x10;
929 dum->ep[0].ep.maxpacket = 9;
930 } else
931 dum->ep[0].ep.maxpacket = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 list_del_init (&dum->ep [0].ep.ep_list);
933 INIT_LIST_HEAD(&dum->fifo_req.queue);
934
Alan Stern59331012007-11-20 16:28:55 -0500935 driver->driver.bus = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 dum->driver = driver;
937 dum->gadget.dev.driver = &driver->driver;
Alan Sternd9b76252005-05-03 16:15:43 -0400938 dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 driver->driver.name);
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +0200940 retval = bind(&dum->gadget);
Alan Stern59331012007-11-20 16:28:55 -0500941 if (retval) {
942 dum->driver = NULL;
943 dum->gadget.dev.driver = NULL;
944 return retval;
945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 /* khubd will enumerate this in a while */
Alan Sternf1c39fa2005-05-03 16:24:04 -0400948 spin_lock_irq (&dum->lock);
949 dum->pullup = 1;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300950 if (dum->gadget.speed == USB_SPEED_SUPER) {
951 dum->gadget.is_otg =
952 (dummy_hcd_to_hcd(dum->ss_hcd)->self.otg_port != 0);
953 set_link_state(dum->ss_hcd);
954 } else {
955 dum->gadget.is_otg =
956 (dummy_hcd_to_hcd(dum->hs_hcd)->self.otg_port != 0);
957 set_link_state(dum->hs_hcd);
958 }
959
Alan Sternf1c39fa2005-05-03 16:24:04 -0400960 spin_unlock_irq (&dum->lock);
Alan Stern685eb932005-05-03 16:27:26 -0400961
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300962 usb_hcd_poll_rh_status((dum->gadget.speed == USB_SPEED_SUPER ?
963 dummy_hcd_to_hcd(dum->ss_hcd) :
964 dummy_hcd_to_hcd(dum->hs_hcd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return 0;
966}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300968static int dummy_udc_stop(struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300970 struct dummy *dum = &the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 unsigned long flags;
972
973 if (!dum)
974 return -ENODEV;
David Brownell6bea4762006-12-05 03:15:33 -0800975 if (!driver || driver != dum->driver || !driver->unbind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return -EINVAL;
977
Alan Sternd9b76252005-05-03 16:15:43 -0400978 dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 driver->driver.name);
980
981 spin_lock_irqsave (&dum->lock, flags);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400982 dum->pullup = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300983 set_link_state((dum->gadget.speed == USB_SPEED_SUPER ?
984 dum->ss_hcd : dum->hs_hcd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 spin_unlock_irqrestore (&dum->lock, flags);
986
987 driver->unbind (&dum->gadget);
Alan Stern59331012007-11-20 16:28:55 -0500988 dum->gadget.dev.driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 dum->driver = NULL;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400990 spin_lock_irqsave (&dum->lock, flags);
991 dum->pullup = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300992 set_link_state((dum->gadget.speed == USB_SPEED_SUPER ?
993 dum->ss_hcd : dum->hs_hcd));
Alan Sternf1c39fa2005-05-03 16:24:04 -0400994 spin_unlock_irqrestore (&dum->lock, flags);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300995 usb_hcd_poll_rh_status((dum->gadget.speed == USB_SPEED_SUPER ?
996 dummy_hcd_to_hcd(dum->ss_hcd) :
997 dummy_hcd_to_hcd(dum->hs_hcd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return 0;
999}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001#undef is_enabled
1002
Alan Sterncc095b02005-05-10 15:28:38 -04001003/* just declare this in any driver that really need it */
1004extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
1007{
1008 return -ENOSYS;
1009}
1010EXPORT_SYMBOL (net2280_set_fifo_mode);
1011
Alan Sternd9b76252005-05-03 16:15:43 -04001012
1013/* The gadget structure is stored inside the hcd structure and will be
1014 * released along with it. */
1015static void
1016dummy_gadget_release (struct device *dev)
1017{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001018 return;
Alan Sternd9b76252005-05-03 16:15:43 -04001019}
1020
Alan Stern8364d6b2005-11-14 12:16:30 -05001021static int dummy_udc_probe (struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -04001022{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001023 struct dummy *dum = &the_controller;
Alan Sternd9b76252005-05-03 16:15:43 -04001024 int rc;
1025
1026 dum->gadget.name = gadget_name;
1027 dum->gadget.ops = &dummy_ops;
1028 dum->gadget.is_dualspeed = 1;
1029
Kay Sievers0031a062008-05-02 06:02:41 +02001030 dev_set_name(&dum->gadget.dev, "gadget");
Alan Stern8364d6b2005-11-14 12:16:30 -05001031 dum->gadget.dev.parent = &pdev->dev;
Alan Sternd9b76252005-05-03 16:15:43 -04001032 dum->gadget.dev.release = dummy_gadget_release;
1033 rc = device_register (&dum->gadget.dev);
Rahul Ruikar75d87cd2010-10-07 09:40:45 +05301034 if (rc < 0) {
1035 put_device(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -04001036 return rc;
Rahul Ruikar75d87cd2010-10-07 09:40:45 +05301037 }
Alan Sternd9b76252005-05-03 16:15:43 -04001038
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001039 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
1040 if (rc < 0)
1041 goto err_udc;
1042
Alan Sternefd54a32006-09-25 11:55:56 -04001043 rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
1044 if (rc < 0)
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001045 goto err_dev;
1046 platform_set_drvdata(pdev, dum);
1047 return rc;
1048
1049err_dev:
1050 usb_del_gadget_udc(&dum->gadget);
1051err_udc:
1052 device_unregister(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -04001053 return rc;
1054}
1055
Alan Stern8364d6b2005-11-14 12:16:30 -05001056static int dummy_udc_remove (struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -04001057{
Alan Stern8364d6b2005-11-14 12:16:30 -05001058 struct dummy *dum = platform_get_drvdata (pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04001059
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001060 usb_del_gadget_udc(&dum->gadget);
Alan Stern8364d6b2005-11-14 12:16:30 -05001061 platform_set_drvdata (pdev, NULL);
Alan Sternd9b76252005-05-03 16:15:43 -04001062 device_remove_file (&dum->gadget.dev, &dev_attr_function);
1063 device_unregister (&dum->gadget.dev);
1064 return 0;
1065}
1066
Alan Stern8364d6b2005-11-14 12:16:30 -05001067static int dummy_udc_suspend (struct platform_device *pdev, pm_message_t state)
Alan Stern391eca92005-05-10 15:34:16 -04001068{
Alan Stern8364d6b2005-11-14 12:16:30 -05001069 struct dummy *dum = platform_get_drvdata(pdev);
Alan Stern391eca92005-05-10 15:34:16 -04001070
Harvey Harrison441b62c2008-03-03 16:08:34 -08001071 dev_dbg (&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04001072 spin_lock_irq (&dum->lock);
1073 dum->udc_suspended = 1;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001074 set_link_state((dum->gadget.speed == USB_SPEED_SUPER ?
1075 dum->ss_hcd : dum->hs_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001076 spin_unlock_irq (&dum->lock);
1077
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001078 usb_hcd_poll_rh_status((dum->gadget.speed == USB_SPEED_SUPER ?
1079 dummy_hcd_to_hcd(dum->ss_hcd) :
1080 dummy_hcd_to_hcd(dum->hs_hcd)));
Alan Stern391eca92005-05-10 15:34:16 -04001081 return 0;
1082}
1083
Alan Stern8364d6b2005-11-14 12:16:30 -05001084static int dummy_udc_resume (struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04001085{
Alan Stern8364d6b2005-11-14 12:16:30 -05001086 struct dummy *dum = platform_get_drvdata(pdev);
Alan Stern391eca92005-05-10 15:34:16 -04001087
Harvey Harrison441b62c2008-03-03 16:08:34 -08001088 dev_dbg (&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04001089 spin_lock_irq (&dum->lock);
1090 dum->udc_suspended = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001091 set_link_state((dum->gadget.speed == USB_SPEED_SUPER ?
1092 dum->ss_hcd : dum->hs_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001093 spin_unlock_irq (&dum->lock);
1094
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001095 usb_hcd_poll_rh_status((dum->gadget.speed == USB_SPEED_SUPER ?
1096 dummy_hcd_to_hcd(dum->ss_hcd) :
1097 dummy_hcd_to_hcd(dum->hs_hcd)));
Alan Stern391eca92005-05-10 15:34:16 -04001098 return 0;
1099}
1100
Russell King3ae5eae2005-11-09 22:32:44 +00001101static struct platform_driver dummy_udc_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04001102 .probe = dummy_udc_probe,
1103 .remove = dummy_udc_remove,
Alan Stern391eca92005-05-10 15:34:16 -04001104 .suspend = dummy_udc_suspend,
1105 .resume = dummy_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001106 .driver = {
1107 .name = (char *) gadget_name,
1108 .owner = THIS_MODULE,
1109 },
Alan Sternd9b76252005-05-03 16:15:43 -04001110};
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112/*-------------------------------------------------------------------------*/
1113
1114/* MASTER/HOST SIDE DRIVER
1115 *
1116 * this uses the hcd framework to hook up to host side drivers.
1117 * its root hub will only have one device, otherwise it acts like
1118 * a normal host controller.
1119 *
1120 * when urbs are queued, they're just stuck on a list that we
1121 * scan in a timer callback. that callback connects writes from
1122 * the host with reads from the device, and so on, based on the
1123 * usb 2.0 rules.
1124 */
1125
1126static int dummy_urb_enqueue (
1127 struct usb_hcd *hcd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001129 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001131 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 struct urbp *urbp;
1133 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001134 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1137 return -EINVAL;
1138
1139 urbp = kmalloc (sizeof *urbp, mem_flags);
1140 if (!urbp)
1141 return -ENOMEM;
1142 urbp->urb = urb;
1143
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001144 dum_hcd = hcd_to_dummy_hcd(hcd);
1145 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001146 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1147 if (rc) {
1148 kfree(urbp);
1149 goto done;
1150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001152 if (!dum_hcd->udev) {
1153 dum_hcd->udev = urb->dev;
1154 usb_get_dev(dum_hcd->udev);
1155 } else if (unlikely(dum_hcd->udev != urb->dev))
1156 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001158 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 urb->hcpriv = urbp;
1160 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
1161 urb->error_count = 1; /* mark as a new urb */
1162
1163 /* kick the scheduler, it'll do the rest */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001164 if (!timer_pending(&dum_hcd->timer))
1165 mod_timer(&dum_hcd->timer, jiffies + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Alan Sterne9df41c2007-08-08 11:48:02 -04001167 done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001168 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001169 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
Alan Sterne9df41c2007-08-08 11:48:02 -04001172static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001174 struct dummy_hcd *dum_hcd;
Alan Stern391eca92005-05-10 15:34:16 -04001175 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001176 int rc;
Alan Stern391eca92005-05-10 15:34:16 -04001177
1178 /* giveback happens automatically in timer callback,
1179 * so make sure the callback happens */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001180 dum_hcd = hcd_to_dummy_hcd(hcd);
1181 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001182
1183 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001184 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1185 !list_empty(&dum_hcd->urbp_list))
1186 mod_timer(&dum_hcd->timer, jiffies);
Alan Sterne9df41c2007-08-08 11:48:02 -04001187
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001188 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001189 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192/* transfer up to a frame's worth; caller must own lock */
1193static int
Alan Stern4d2f1102007-08-24 15:40:10 -04001194transfer(struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit,
1195 int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 struct dummy_request *req;
1198
1199top:
1200 /* if there's no request queued, the device is NAKing; return */
1201 list_for_each_entry (req, &ep->queue, queue) {
1202 unsigned host_len, dev_len, len;
1203 int is_short, to_host;
1204 int rescan = 0;
1205
1206 /* 1..N packets of ep->ep.maxpacket each ... the last one
1207 * may be short (including zero length).
1208 *
1209 * writer can send a zlp explicitly (length 0) or implicitly
1210 * (length mod maxpacket zero, and 'zero' flag); they always
1211 * terminate reads.
1212 */
1213 host_len = urb->transfer_buffer_length - urb->actual_length;
1214 dev_len = req->req.length - req->req.actual;
1215 len = min (host_len, dev_len);
1216
1217 /* FIXME update emulated data toggle too */
1218
1219 to_host = usb_pipein (urb->pipe);
1220 if (unlikely (len == 0))
1221 is_short = 1;
1222 else {
1223 char *ubuf, *rbuf;
1224
1225 /* not enough bandwidth left? */
1226 if (limit < ep->ep.maxpacket && limit < len)
1227 break;
1228 len = min (len, (unsigned) limit);
1229 if (len == 0)
1230 break;
1231
1232 /* use an extra pass for the final short packet */
1233 if (len > ep->ep.maxpacket) {
1234 rescan = 1;
1235 len -= (len % ep->ep.maxpacket);
1236 }
1237 is_short = (len % ep->ep.maxpacket) != 0;
1238
1239 /* else transfer packet(s) */
1240 ubuf = urb->transfer_buffer + urb->actual_length;
1241 rbuf = req->req.buf + req->req.actual;
1242 if (to_host)
1243 memcpy (ubuf, rbuf, len);
1244 else
1245 memcpy (rbuf, ubuf, len);
1246 ep->last_io = jiffies;
1247
1248 limit -= len;
1249 urb->actual_length += len;
1250 req->req.actual += len;
1251 }
1252
1253 /* short packets terminate, maybe with overflow/underflow.
1254 * it's only really an error to write too much.
1255 *
1256 * partially filling a buffer optionally blocks queue advances
1257 * (so completion handlers can clean up the queue) but we don't
Alan Sternb0d9efb2007-08-21 15:39:21 -04001258 * need to emulate such data-in-flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 */
1260 if (is_short) {
1261 if (host_len == dev_len) {
1262 req->req.status = 0;
Alan Stern4d2f1102007-08-24 15:40:10 -04001263 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 } else if (to_host) {
1265 req->req.status = 0;
1266 if (dev_len > host_len)
Alan Stern4d2f1102007-08-24 15:40:10 -04001267 *status = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 else
Alan Stern4d2f1102007-08-24 15:40:10 -04001269 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 } else if (!to_host) {
Alan Stern4d2f1102007-08-24 15:40:10 -04001271 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (host_len > dev_len)
1273 req->req.status = -EOVERFLOW;
1274 else
1275 req->req.status = 0;
1276 }
1277
1278 /* many requests terminate without a short packet */
1279 } else {
1280 if (req->req.length == req->req.actual
1281 && !req->req.zero)
1282 req->req.status = 0;
1283 if (urb->transfer_buffer_length == urb->actual_length
1284 && !(urb->transfer_flags
Alan Stern4d2f1102007-08-24 15:40:10 -04001285 & URB_ZERO_PACKET))
1286 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288
1289 /* device side completion --> continuable */
1290 if (req->req.status != -EINPROGRESS) {
1291 list_del_init (&req->queue);
1292
1293 spin_unlock (&dum->lock);
1294 req->req.complete (&ep->ep, &req->req);
1295 spin_lock (&dum->lock);
1296
1297 /* requests might have been unlinked... */
1298 rescan = 1;
1299 }
1300
1301 /* host side completion --> terminate */
Alan Stern4d2f1102007-08-24 15:40:10 -04001302 if (*status != -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 break;
1304
1305 /* rescan to continue with any other queued i/o */
1306 if (rescan)
1307 goto top;
1308 }
1309 return limit;
1310}
1311
1312static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1313{
1314 int limit = ep->ep.maxpacket;
1315
1316 if (dum->gadget.speed == USB_SPEED_HIGH) {
1317 int tmp;
1318
1319 /* high bandwidth mode */
1320 tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 tmp = (tmp >> 11) & 0x03;
1322 tmp *= 8 /* applies to entire frame */;
1323 limit += limit * tmp;
1324 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001325 if (dum->gadget.speed == USB_SPEED_SUPER) {
1326 switch (ep->desc->bmAttributes & 0x03) {
1327 case USB_ENDPOINT_XFER_ISOC:
1328 /* Sec. 4.4.8.2 USB3.0 Spec */
1329 limit = 3 * 16 * 1024 * 8;
1330 break;
1331 case USB_ENDPOINT_XFER_INT:
1332 /* Sec. 4.4.7.2 USB3.0 Spec */
1333 limit = 3 * 1024 * 8;
1334 break;
1335 case USB_ENDPOINT_XFER_BULK:
1336 default:
1337 break;
1338 }
1339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return limit;
1341}
1342
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001343#define is_active(dum_hcd) ((dum_hcd->port_status & \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1345 USB_PORT_STAT_SUSPEND)) \
1346 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1347
1348static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1349{
1350 int i;
1351
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001352 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1353 dum->ss_hcd : dum->hs_hcd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return NULL;
1355 if ((address & ~USB_DIR_IN) == 0)
1356 return &dum->ep [0];
1357 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1358 struct dummy_ep *ep = &dum->ep [i];
1359
1360 if (!ep->desc)
1361 continue;
1362 if (ep->desc->bEndpointAddress == address)
1363 return ep;
1364 }
1365 return NULL;
1366}
1367
1368#undef is_active
1369
1370#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1371#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1372#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1373#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1374#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1375#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1376
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001377
1378/**
1379 * handle_control_request() - handles all control transfers
1380 * @dum: pointer to dummy (the_controller)
1381 * @urb: the urb request to handle
1382 * @setup: pointer to the setup data for a USB device control
1383 * request
1384 * @status: pointer to request handling status
1385 *
1386 * Return 0 - if the request was handled
1387 * 1 - if the request wasn't handles
1388 * error code on error
1389 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001390static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001391 struct usb_ctrlrequest *setup,
1392 int *status)
1393{
1394 struct dummy_ep *ep2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001395 struct dummy *dum = dum_hcd->dum;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001396 int ret_val = 1;
1397 unsigned w_index;
1398 unsigned w_value;
1399
1400 w_index = le16_to_cpu(setup->wIndex);
1401 w_value = le16_to_cpu(setup->wValue);
1402 switch (setup->bRequest) {
1403 case USB_REQ_SET_ADDRESS:
1404 if (setup->bRequestType != Dev_Request)
1405 break;
1406 dum->address = w_value;
1407 *status = 0;
1408 dev_dbg(udc_dev(dum), "set_address = %d\n",
1409 w_value);
1410 ret_val = 0;
1411 break;
1412 case USB_REQ_SET_FEATURE:
1413 if (setup->bRequestType == Dev_Request) {
1414 ret_val = 0;
1415 switch (w_value) {
1416 case USB_DEVICE_REMOTE_WAKEUP:
1417 break;
1418 case USB_DEVICE_B_HNP_ENABLE:
1419 dum->gadget.b_hnp_enable = 1;
1420 break;
1421 case USB_DEVICE_A_HNP_SUPPORT:
1422 dum->gadget.a_hnp_support = 1;
1423 break;
1424 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1425 dum->gadget.a_alt_hnp_support = 1;
1426 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001427 case USB_DEVICE_U1_ENABLE:
1428 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1429 HCD_USB3)
1430 w_value = USB_DEV_STAT_U1_ENABLED;
1431 else
1432 ret_val = -EOPNOTSUPP;
1433 break;
1434 case USB_DEVICE_U2_ENABLE:
1435 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1436 HCD_USB3)
1437 w_value = USB_DEV_STAT_U2_ENABLED;
1438 else
1439 ret_val = -EOPNOTSUPP;
1440 break;
1441 case USB_DEVICE_LTM_ENABLE:
1442 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1443 HCD_USB3)
1444 w_value = USB_DEV_STAT_LTM_ENABLED;
1445 else
1446 ret_val = -EOPNOTSUPP;
1447 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001448 default:
1449 ret_val = -EOPNOTSUPP;
1450 }
1451 if (ret_val == 0) {
1452 dum->devstatus |= (1 << w_value);
1453 *status = 0;
1454 }
1455 } else if (setup->bRequestType == Ep_Request) {
1456 /* endpoint halt */
1457 ep2 = find_endpoint(dum, w_index);
1458 if (!ep2 || ep2->ep.name == ep0name) {
1459 ret_val = -EOPNOTSUPP;
1460 break;
1461 }
1462 ep2->halted = 1;
1463 ret_val = 0;
1464 *status = 0;
1465 }
1466 break;
1467 case USB_REQ_CLEAR_FEATURE:
1468 if (setup->bRequestType == Dev_Request) {
1469 ret_val = 0;
1470 switch (w_value) {
1471 case USB_DEVICE_REMOTE_WAKEUP:
1472 w_value = USB_DEVICE_REMOTE_WAKEUP;
1473 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001474 case USB_DEVICE_U1_ENABLE:
1475 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1476 HCD_USB3)
1477 w_value = USB_DEV_STAT_U1_ENABLED;
1478 else
1479 ret_val = -EOPNOTSUPP;
1480 break;
1481 case USB_DEVICE_U2_ENABLE:
1482 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1483 HCD_USB3)
1484 w_value = USB_DEV_STAT_U2_ENABLED;
1485 else
1486 ret_val = -EOPNOTSUPP;
1487 break;
1488 case USB_DEVICE_LTM_ENABLE:
1489 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1490 HCD_USB3)
1491 w_value = USB_DEV_STAT_LTM_ENABLED;
1492 else
1493 ret_val = -EOPNOTSUPP;
1494 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001495 default:
1496 ret_val = -EOPNOTSUPP;
1497 break;
1498 }
1499 if (ret_val == 0) {
1500 dum->devstatus &= ~(1 << w_value);
1501 *status = 0;
1502 }
1503 } else if (setup->bRequestType == Ep_Request) {
1504 /* endpoint halt */
1505 ep2 = find_endpoint(dum, w_index);
1506 if (!ep2) {
1507 ret_val = -EOPNOTSUPP;
1508 break;
1509 }
1510 if (!ep2->wedged)
1511 ep2->halted = 0;
1512 ret_val = 0;
1513 *status = 0;
1514 }
1515 break;
1516 case USB_REQ_GET_STATUS:
1517 if (setup->bRequestType == Dev_InRequest
1518 || setup->bRequestType == Intf_InRequest
1519 || setup->bRequestType == Ep_InRequest) {
1520 char *buf;
1521 /*
1522 * device: remote wakeup, selfpowered
1523 * interface: nothing
1524 * endpoint: halt
1525 */
1526 buf = (char *)urb->transfer_buffer;
1527 if (urb->transfer_buffer_length > 0) {
1528 if (setup->bRequestType == Ep_InRequest) {
1529 ep2 = find_endpoint(dum, w_index);
1530 if (!ep2) {
1531 ret_val = -EOPNOTSUPP;
1532 break;
1533 }
1534 buf[0] = ep2->halted;
1535 } else if (setup->bRequestType ==
1536 Dev_InRequest) {
1537 buf[0] = (u8)dum->devstatus;
1538 } else
1539 buf[0] = 0;
1540 }
1541 if (urb->transfer_buffer_length > 1)
1542 buf[1] = 0;
1543 urb->actual_length = min_t(u32, 2,
1544 urb->transfer_buffer_length);
1545 ret_val = 0;
1546 *status = 0;
1547 }
1548 break;
1549 }
1550 return ret_val;
1551}
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553/* drive both sides of the transfers; looks like irq handlers to
1554 * both drivers except the callbacks aren't in_irq().
1555 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001556static void dummy_timer(unsigned long _dum_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001558 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1559 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 struct urbp *urbp, *tmp;
1561 unsigned long flags;
1562 int limit, total;
1563 int i;
1564
1565 /* simplistic model for one frame's bandwidth */
1566 switch (dum->gadget.speed) {
1567 case USB_SPEED_LOW:
1568 total = 8/*bytes*/ * 12/*packets*/;
1569 break;
1570 case USB_SPEED_FULL:
1571 total = 64/*bytes*/ * 19/*packets*/;
1572 break;
1573 case USB_SPEED_HIGH:
1574 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1575 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001576 case USB_SPEED_SUPER:
1577 /* Bus speed is 500000 bytes/ms, so use a little less */
1578 total = 490000;
1579 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001581 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return;
1583 }
1584
1585 /* FIXME if HZ != 1000 this will probably misbehave ... */
1586
1587 /* look at each urb queued by the host side driver */
1588 spin_lock_irqsave (&dum->lock, flags);
1589
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001590 if (!dum_hcd->udev) {
1591 dev_err(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 "timer fired with no URBs pending?\n");
1593 spin_unlock_irqrestore (&dum->lock, flags);
1594 return;
1595 }
1596
1597 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1598 if (!ep_name [i])
1599 break;
1600 dum->ep [i].already_seen = 0;
1601 }
1602
1603restart:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001604 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 struct urb *urb;
1606 struct dummy_request *req;
1607 u8 address;
1608 struct dummy_ep *ep = NULL;
1609 int type;
Alan Stern4d2f1102007-08-24 15:40:10 -04001610 int status = -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 urb = urbp->urb;
Alan Sterneb231052007-08-21 15:40:36 -04001613 if (urb->unlinked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 goto return_urb;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001615 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
Alan Stern391eca92005-05-10 15:34:16 -04001616 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 type = usb_pipetype (urb->pipe);
1618
1619 /* used up this frame's non-periodic bandwidth?
1620 * FIXME there's infinite bandwidth for control and
1621 * periodic transfers ... unrealistic.
1622 */
1623 if (total <= 0 && type == PIPE_BULK)
1624 continue;
1625
1626 /* find the gadget's ep for this request (if configured) */
1627 address = usb_pipeendpoint (urb->pipe);
1628 if (usb_pipein (urb->pipe))
1629 address |= USB_DIR_IN;
1630 ep = find_endpoint(dum, address);
1631 if (!ep) {
1632 /* set_configuration() disagreement */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001633 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 "no ep configured for urb %p\n",
1635 urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001636 status = -EPROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 goto return_urb;
1638 }
1639
1640 if (ep->already_seen)
1641 continue;
1642 ep->already_seen = 1;
1643 if (ep == &dum->ep [0] && urb->error_count) {
1644 ep->setup_stage = 1; /* a new urb */
1645 urb->error_count = 0;
1646 }
1647 if (ep->halted && !ep->setup_stage) {
1648 /* NOTE: must not be iso! */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001649 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 ep->ep.name, urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001651 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 goto return_urb;
1653 }
1654 /* FIXME make sure both ends agree on maxpacket */
1655
1656 /* handle control requests */
1657 if (ep == &dum->ep [0] && ep->setup_stage) {
1658 struct usb_ctrlrequest setup;
1659 int value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 /* paranoia, in case of stale queued data */
1663 list_for_each_entry (req, &ep->queue, queue) {
1664 list_del_init (&req->queue);
1665 req->req.status = -EOVERFLOW;
Alan Sternd9b76252005-05-03 16:15:43 -04001666 dev_dbg (udc_dev(dum), "stale req = %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 req);
1668
1669 spin_unlock (&dum->lock);
1670 req->req.complete (&ep->ep, &req->req);
1671 spin_lock (&dum->lock);
1672 ep->already_seen = 0;
1673 goto restart;
1674 }
1675
1676 /* gadget driver never sees set_address or operations
1677 * on standard feature flags. some hardware doesn't
1678 * even expose them.
1679 */
1680 ep->last_io = jiffies;
1681 ep->setup_stage = 0;
1682 ep->halted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001684 value = handle_control_request(dum_hcd, urb, &setup,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001685 &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 /* gadget driver handles all other requests. block
1688 * until setup() returns; no reentrancy issues etc.
1689 */
1690 if (value > 0) {
1691 spin_unlock (&dum->lock);
1692 value = dum->driver->setup (&dum->gadget,
1693 &setup);
1694 spin_lock (&dum->lock);
1695
1696 if (value >= 0) {
1697 /* no delays (max 64KB data stage) */
1698 limit = 64*1024;
1699 goto treat_control_like_bulk;
1700 }
1701 /* error, see below */
1702 }
1703
1704 if (value < 0) {
1705 if (value != -EOPNOTSUPP)
Alan Sternd9b76252005-05-03 16:15:43 -04001706 dev_dbg (udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 "setup --> %d\n",
1708 value);
Alan Stern4d2f1102007-08-24 15:40:10 -04001709 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 urb->actual_length = 0;
1711 }
1712
1713 goto return_urb;
1714 }
1715
1716 /* non-control requests */
1717 limit = total;
1718 switch (usb_pipetype (urb->pipe)) {
1719 case PIPE_ISOCHRONOUS:
1720 /* FIXME is it urb->interval since the last xfer?
1721 * use urb->iso_frame_desc[i].
1722 * complete whether or not ep has requests queued.
1723 * report random errors, to debug drivers.
1724 */
1725 limit = max (limit, periodic_bytes (dum, ep));
Alan Stern4d2f1102007-08-24 15:40:10 -04001726 status = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 break;
1728
1729 case PIPE_INTERRUPT:
1730 /* FIXME is it urb->interval since the last xfer?
1731 * this almost certainly polls too fast.
1732 */
1733 limit = max (limit, periodic_bytes (dum, ep));
1734 /* FALLTHROUGH */
1735
1736 // case PIPE_BULK: case PIPE_CONTROL:
1737 default:
1738 treat_control_like_bulk:
1739 ep->last_io = jiffies;
Alan Stern4d2f1102007-08-24 15:40:10 -04001740 total = transfer(dum, urb, ep, limit, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 break;
1742 }
1743
1744 /* incomplete transfer? */
Alan Stern4d2f1102007-08-24 15:40:10 -04001745 if (status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 continue;
1747
1748return_urb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 list_del (&urbp->urbp_list);
1750 kfree (urbp);
1751 if (ep)
1752 ep->already_seen = ep->setup_stage = 0;
1753
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001754 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 spin_unlock (&dum->lock);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001756 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 spin_lock (&dum->lock);
1758
1759 goto restart;
1760 }
1761
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001762 if (list_empty(&dum_hcd->urbp_list)) {
1763 usb_put_dev(dum_hcd->udev);
1764 dum_hcd->udev = NULL;
1765 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern391eca92005-05-10 15:34:16 -04001766 /* want a 1 msec delay here */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001767 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
1769
1770 spin_unlock_irqrestore (&dum->lock, flags);
1771}
1772
1773/*-------------------------------------------------------------------------*/
1774
1775#define PORT_C_MASK \
Alan Sternc2db8b52005-04-29 16:30:48 -04001776 ((USB_PORT_STAT_C_CONNECTION \
1777 | USB_PORT_STAT_C_ENABLE \
1778 | USB_PORT_STAT_C_SUSPEND \
1779 | USB_PORT_STAT_C_OVERCURRENT \
1780 | USB_PORT_STAT_C_RESET) << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1783{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001784 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 unsigned long flags;
Alan Stern391eca92005-05-10 15:34:16 -04001786 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001788 dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001790 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001791 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001792 goto done;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001793
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001794 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1795 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1796 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1797 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001798 }
1799
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001800 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 *buf = (1 << 1);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001802 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1803 dum_hcd->port_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 retval = 1;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001805 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -04001806 usb_hcd_resume_root_hub (hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
Alan Stern391eca92005-05-10 15:34:16 -04001808done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001809 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 return retval;
1811}
1812
1813static inline void
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001814ss_hub_descriptor(struct usb_hub_descriptor *desc)
1815{
1816 memset(desc, 0, sizeof *desc);
1817 desc->bDescriptorType = 0x2a;
1818 desc->bDescLength = 12;
1819 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1820 desc->bNbrPorts = 1;
1821 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1822 desc->u.ss.DeviceRemovable = 0xffff;
1823}
1824
1825static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826hub_descriptor (struct usb_hub_descriptor *desc)
1827{
1828 memset (desc, 0, sizeof *desc);
1829 desc->bDescriptorType = 0x29;
1830 desc->bDescLength = 9;
Al Virofd05e722008-04-28 07:00:16 +01001831 desc->wHubCharacteristics = cpu_to_le16(0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 desc->bNbrPorts = 1;
John Youndbe79bb2001-09-17 00:00:00 -07001833 desc->u.hs.DeviceRemovable[0] = 0xff;
1834 desc->u.hs.DeviceRemovable[1] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
1837static int dummy_hub_control (
1838 struct usb_hcd *hcd,
1839 u16 typeReq,
1840 u16 wValue,
1841 u16 wIndex,
1842 char *buf,
1843 u16 wLength
1844) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001845 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 int retval = 0;
1847 unsigned long flags;
1848
Alan Stern541c7d42010-06-22 16:39:10 -04001849 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001850 return -ETIMEDOUT;
1851
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001852 dum_hcd = hcd_to_dummy_hcd(hcd);
1853
1854 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 switch (typeReq) {
1856 case ClearHubFeature:
1857 break;
1858 case ClearPortFeature:
1859 switch (wValue) {
1860 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001861 if (hcd->speed == HCD_USB3) {
1862 dev_dbg(dummy_dev(dum_hcd),
1863 "USB_PORT_FEAT_SUSPEND req not "
1864 "supported for USB 3.0 roothub\n");
1865 goto error;
1866 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001867 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 /* 20msec resume signaling */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001869 dum_hcd->resuming = 1;
1870 dum_hcd->re_timeout = jiffies +
Alan Sternf1c39fa2005-05-03 16:24:04 -04001871 msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
1873 break;
1874 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001875 if (hcd->speed == HCD_USB3) {
1876 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1877 dev_dbg(dummy_dev(dum_hcd),
1878 "power-off\n");
1879 } else
1880 if (dum_hcd->port_status &
1881 USB_SS_PORT_STAT_POWER)
1882 dev_dbg(dummy_dev(dum_hcd),
1883 "power-off\n");
Alan Sternf1c39fa2005-05-03 16:24:04 -04001884 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001886 dum_hcd->port_status &= ~(1 << wValue);
1887 set_link_state(dum_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
1889 break;
1890 case GetHubDescriptor:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001891 if (hcd->speed == HCD_USB3 &&
1892 (wLength < USB_DT_SS_HUB_SIZE ||
1893 wValue != (USB_DT_SS_HUB << 8))) {
1894 dev_dbg(dummy_dev(dum_hcd),
1895 "Wrong hub descriptor type for "
1896 "USB 3.0 roothub.\n");
1897 goto error;
1898 }
1899 if (hcd->speed == HCD_USB3)
1900 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
1901 else
1902 hub_descriptor((struct usb_hub_descriptor *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 break;
1904 case GetHubStatus:
Harvey Harrison551509d2009-02-11 14:11:36 -08001905 *(__le32 *) buf = cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 break;
1907 case GetPortStatus:
1908 if (wIndex != 1)
1909 retval = -EPIPE;
1910
1911 /* whoever resets or resumes must GetPortStatus to
1912 * complete it!!
1913 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001914 if (dum_hcd->resuming &&
1915 time_after_eq(jiffies, dum_hcd->re_timeout)) {
1916 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1917 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001919 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
1920 time_after_eq(jiffies, dum_hcd->re_timeout)) {
1921 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
1922 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
1923 if (dum_hcd->dum->pullup) {
1924 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001925
1926 if (hcd->speed < HCD_USB3) {
1927 switch (dum_hcd->dum->gadget.speed) {
1928 case USB_SPEED_HIGH:
1929 dum_hcd->port_status |=
1930 USB_PORT_STAT_HIGH_SPEED;
1931 break;
1932 case USB_SPEED_LOW:
1933 dum_hcd->dum->gadget.ep0->
1934 maxpacket = 8;
1935 dum_hcd->port_status |=
1936 USB_PORT_STAT_LOW_SPEED;
1937 break;
1938 default:
1939 dum_hcd->dum->gadget.speed =
1940 USB_SPEED_FULL;
1941 break;
1942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
1944 }
1945 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001946 set_link_state(dum_hcd);
1947 ((__le16 *) buf)[0] = cpu_to_le16 (dum_hcd->port_status);
1948 ((__le16 *) buf)[1] = cpu_to_le16 (dum_hcd->port_status >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 break;
1950 case SetHubFeature:
1951 retval = -EPIPE;
1952 break;
1953 case SetPortFeature:
1954 switch (wValue) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001955 case USB_PORT_FEAT_LINK_STATE:
1956 if (hcd->speed != HCD_USB3) {
1957 dev_dbg(dummy_dev(dum_hcd),
1958 "USB_PORT_FEAT_LINK_STATE req not "
1959 "supported for USB 2.0 roothub\n");
1960 goto error;
1961 }
1962 /*
1963 * Since this is dummy we don't have an actual link so
1964 * there is nothing to do for the SET_LINK_STATE cmd
1965 */
1966 break;
1967 case USB_PORT_FEAT_U1_TIMEOUT:
1968 case USB_PORT_FEAT_U2_TIMEOUT:
1969 /* TODO: add suspend/resume support! */
1970 if (hcd->speed != HCD_USB3) {
1971 dev_dbg(dummy_dev(dum_hcd),
1972 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
1973 "supported for USB 2.0 roothub\n");
1974 goto error;
1975 }
1976 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001978 /* Applicable only for USB2.0 hub */
1979 if (hcd->speed == HCD_USB3) {
1980 dev_dbg(dummy_dev(dum_hcd),
1981 "USB_PORT_FEAT_SUSPEND req not "
1982 "supported for USB 3.0 roothub\n");
1983 goto error;
1984 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001985 if (dum_hcd->active) {
1986 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001987
1988 /* HNP would happen here; for now we
1989 * assume b_bus_req is always true.
1990 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001991 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001992 if (((1 << USB_DEVICE_B_HNP_ENABLE)
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001993 & dum_hcd->dum->devstatus) != 0)
1994 dev_dbg(dummy_dev(dum_hcd),
Alan Stern5742b0c2005-05-02 11:25:17 -04001995 "no HNP yet!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997 break;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001998 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001999 if (hcd->speed == HCD_USB3)
2000 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
2001 else
2002 dum_hcd->port_status |= USB_PORT_STAT_POWER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002003 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002004 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002005 case USB_PORT_FEAT_BH_PORT_RESET:
2006 /* Applicable only for USB3.0 hub */
2007 if (hcd->speed != HCD_USB3) {
2008 dev_dbg(dummy_dev(dum_hcd),
2009 "USB_PORT_FEAT_BH_PORT_RESET req not "
2010 "supported for USB 2.0 roothub\n");
2011 goto error;
2012 }
2013 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 case USB_PORT_FEAT_RESET:
Alan Sternf1c39fa2005-05-03 16:24:04 -04002015 /* if it's already enabled, disable */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002016 if (hcd->speed == HCD_USB3) {
2017 dum_hcd->port_status = 0;
2018 dum_hcd->port_status =
2019 (USB_SS_PORT_STAT_POWER |
2020 USB_PORT_STAT_CONNECTION |
2021 USB_PORT_STAT_RESET);
2022 } else
2023 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
Alan Sternf1c39fa2005-05-03 16:24:04 -04002024 | USB_PORT_STAT_LOW_SPEED
2025 | USB_PORT_STAT_HIGH_SPEED);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002026 /*
2027 * We want to reset device status. All but the
2028 * Self powered feature
2029 */
2030 dum_hcd->dum->devstatus &=
2031 (1 << USB_DEVICE_SELF_POWERED);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002032 /*
2033 * FIXME USB3.0: what is the correct reset signaling
2034 * interval? Is it still 50msec as for HS?
2035 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002036 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002037 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 default:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002039 if (hcd->speed == HCD_USB3) {
2040 if ((dum_hcd->port_status &
2041 USB_SS_PORT_STAT_POWER) != 0) {
2042 dum_hcd->port_status |= (1 << wValue);
2043 set_link_state(dum_hcd);
2044 }
2045 } else
2046 if ((dum_hcd->port_status &
2047 USB_PORT_STAT_POWER) != 0) {
2048 dum_hcd->port_status |= (1 << wValue);
2049 set_link_state(dum_hcd);
2050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002053 case GetPortErrorCount:
2054 if (hcd->speed != HCD_USB3) {
2055 dev_dbg(dummy_dev(dum_hcd),
2056 "GetPortErrorCount req not "
2057 "supported for USB 2.0 roothub\n");
2058 goto error;
2059 }
2060 /* We'll always return 0 since this is a dummy hub */
2061 *(__le32 *) buf = cpu_to_le32(0);
2062 break;
2063 case SetHubDepth:
2064 if (hcd->speed != HCD_USB3) {
2065 dev_dbg(dummy_dev(dum_hcd),
2066 "SetHubDepth req not supported for "
2067 "USB 2.0 roothub\n");
2068 goto error;
2069 }
2070 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002072 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 "hub control req%04x v%04x i%04x l%d\n",
2074 typeReq, wValue, wIndex, wLength);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002075error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 /* "protocol stall" on error */
2077 retval = -EPIPE;
2078 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002079 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Stern685eb932005-05-03 16:27:26 -04002080
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002081 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
Alan Stern685eb932005-05-03 16:27:26 -04002082 usb_hcd_poll_rh_status (hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return retval;
2084}
2085
Alan Stern0c0382e2005-10-13 17:08:02 -04002086static int dummy_bus_suspend (struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002087{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002088 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002089
Harvey Harrison441b62c2008-03-03 16:08:34 -08002090 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern3cf0a222005-11-29 12:08:15 -05002091
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002092 spin_lock_irq(&dum_hcd->dum->lock);
2093 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2094 set_link_state(dum_hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002095 hcd->state = HC_STATE_SUSPENDED;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002096 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern391eca92005-05-10 15:34:16 -04002097 return 0;
2098}
2099
Alan Stern0c0382e2005-10-13 17:08:02 -04002100static int dummy_bus_resume (struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002101{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002102 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002103 int rc = 0;
2104
Harvey Harrison441b62c2008-03-03 16:08:34 -08002105 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002106
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002107 spin_lock_irq(&dum_hcd->dum->lock);
Alan Stern541c7d42010-06-22 16:39:10 -04002108 if (!HCD_HW_ACCESSIBLE(hcd)) {
Alan Sterncfa59da2007-06-21 16:25:35 -04002109 rc = -ESHUTDOWN;
Alan Stern3cf0a222005-11-29 12:08:15 -05002110 } else {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002111 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2112 set_link_state(dum_hcd);
2113 if (!list_empty(&dum_hcd->urbp_list))
2114 mod_timer(&dum_hcd->timer, jiffies);
Alan Stern3cf0a222005-11-29 12:08:15 -05002115 hcd->state = HC_STATE_RUNNING;
2116 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002117 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern3cf0a222005-11-29 12:08:15 -05002118 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002119}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121/*-------------------------------------------------------------------------*/
2122
2123static inline ssize_t
2124show_urb (char *buf, size_t size, struct urb *urb)
2125{
2126 int ep = usb_pipeendpoint (urb->pipe);
2127
2128 return snprintf (buf, size,
2129 "urb/%p %s ep%d%s%s len %d/%d\n",
2130 urb,
2131 ({ char *s;
2132 switch (urb->dev->speed) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002133 case USB_SPEED_LOW:
2134 s = "ls";
2135 break;
2136 case USB_SPEED_FULL:
2137 s = "fs";
2138 break;
2139 case USB_SPEED_HIGH:
2140 s = "hs";
2141 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002142 case USB_SPEED_SUPER:
2143 s = "ss";
2144 break;
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002145 default:
2146 s = "?";
2147 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 }; s; }),
2149 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
2150 ({ char *s; \
2151 switch (usb_pipetype (urb->pipe)) { \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002152 case PIPE_CONTROL: \
2153 s = ""; \
2154 break; \
2155 case PIPE_BULK: \
2156 s = "-bulk"; \
2157 break; \
2158 case PIPE_INTERRUPT: \
2159 s = "-int"; \
2160 break; \
2161 default: \
2162 s = "-iso"; \
2163 break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }; s;}),
2165 urb->actual_length, urb->transfer_buffer_length);
2166}
2167
2168static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -04002169show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
2171 struct usb_hcd *hcd = dev_get_drvdata (dev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002172 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 struct urbp *urbp;
2174 size_t size = 0;
2175 unsigned long flags;
2176
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002177 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2178 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 size_t temp;
2180
2181 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
2182 buf += temp;
2183 size += temp;
2184 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002185 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 return size;
2188}
2189static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
2190
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002191static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2192{
2193 init_timer(&dum_hcd->timer);
2194 dum_hcd->timer.function = dummy_timer;
2195 dum_hcd->timer.data = (unsigned long)dum_hcd;
2196 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2197 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2198 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2199 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2200 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2201#ifdef CONFIG_USB_OTG
2202 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2203#endif
2204 return 0;
2205
2206 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2207 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2208}
2209
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002210static int dummy_start(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002212 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 /*
2215 * MASTER side init ... we emulate a root hub that'll only ever
2216 * talk to one device (the slave side). Also appears in sysfs,
2217 * just like more familiar pci-based HCDs.
2218 */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002219 if (!usb_hcd_is_primary_hcd(hcd))
2220 return dummy_start_ss(dum_hcd);
2221
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002222 spin_lock_init(&dum_hcd->dum->lock);
2223 init_timer(&dum_hcd->timer);
2224 dum_hcd->timer.function = dummy_timer;
2225 dum_hcd->timer.data = (unsigned long)dum_hcd;
2226 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002228 INIT_LIST_HEAD(&dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Alan Sterncaf29f62007-12-06 11:10:39 -05002230 hcd->power_budget = POWER_BUDGET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 hcd->state = HC_STATE_RUNNING;
Alan Stern685eb932005-05-03 16:27:26 -04002232 hcd->uses_new_polling = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Alan Stern5742b0c2005-05-02 11:25:17 -04002234#ifdef CONFIG_USB_OTG
2235 hcd->self.otg_port = 1;
2236#endif
2237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002239 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
2242static void dummy_stop (struct usb_hcd *hcd)
2243{
2244 struct dummy *dum;
2245
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002246 dum = (hcd_to_dummy_hcd(hcd))->dum;
2247 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2248 usb_gadget_unregister_driver(dum->driver);
2249 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250}
2251
2252/*-------------------------------------------------------------------------*/
2253
2254static int dummy_h_get_frame (struct usb_hcd *hcd)
2255{
2256 return dummy_g_get_frame (NULL);
2257}
2258
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002259static int dummy_setup(struct usb_hcd *hcd)
2260{
2261 if (usb_hcd_is_primary_hcd(hcd)) {
2262 the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
2263 the_controller.hs_hcd->dum = &the_controller;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002264 /*
2265 * Mark the first roothub as being USB 2.0.
2266 * The USB 3.0 roothub will be registered later by
2267 * dummy_hcd_probe()
2268 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002269 hcd->speed = HCD_USB2;
2270 hcd->self.root_hub->speed = USB_SPEED_HIGH;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002271 } else {
2272 the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
2273 the_controller.ss_hcd->dum = &the_controller;
2274 hcd->speed = HCD_USB3;
2275 hcd->self.root_hub->speed = USB_SPEED_SUPER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002276 }
2277 return 0;
2278}
2279
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002280/* Change a group of bulk endpoints to support multiple stream IDs */
2281int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
2282 struct usb_host_endpoint **eps, unsigned int num_eps,
2283 unsigned int num_streams, gfp_t mem_flags)
2284{
2285 if (hcd->speed != HCD_USB3)
2286 dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
2287 "%s() - ERROR! Not supported for USB2.0 roothub\n",
2288 __func__);
2289 return 0;
2290}
2291
2292/* Reverts a group of bulk endpoints back to not using stream IDs. */
2293int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
2294 struct usb_host_endpoint **eps, unsigned int num_eps,
2295 gfp_t mem_flags)
2296{
2297 if (hcd->speed != HCD_USB3)
2298 dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
2299 "%s() - ERROR! Not supported for USB2.0 roothub\n",
2300 __func__);
2301 return 0;
2302}
2303
2304static struct hc_driver dummy_hcd = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 .description = (char *) driver_name,
2306 .product_desc = "Dummy host controller",
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002307 .hcd_priv_size = sizeof(struct dummy_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002309 .flags = HCD_USB3 | HCD_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002311 .reset = dummy_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 .start = dummy_start,
2313 .stop = dummy_stop,
2314
2315 .urb_enqueue = dummy_urb_enqueue,
2316 .urb_dequeue = dummy_urb_dequeue,
2317
2318 .get_frame_number = dummy_h_get_frame,
2319
2320 .hub_status_data = dummy_hub_status,
2321 .hub_control = dummy_hub_control,
Alan Stern0c0382e2005-10-13 17:08:02 -04002322 .bus_suspend = dummy_bus_suspend,
2323 .bus_resume = dummy_bus_resume,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002324
2325 .alloc_streams = dummy_alloc_streams,
2326 .free_streams = dummy_free_streams,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327};
2328
Alan Stern8364d6b2005-11-14 12:16:30 -05002329static int dummy_hcd_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002331 struct usb_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002332 struct usb_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 int retval;
2334
Alan Stern8364d6b2005-11-14 12:16:30 -05002335 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002337 if (!mod_data.is_super_speed)
2338 dummy_hcd.flags = HCD_USB2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002339 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2340 if (!hs_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return -ENOMEM;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002342 hs_hcd->has_tt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002344 retval = usb_add_hcd(hs_hcd, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (retval != 0) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002346 usb_put_hcd(hs_hcd);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002347 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002349
2350 if (mod_data.is_super_speed) {
2351 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2352 dev_name(&pdev->dev), hs_hcd);
2353 if (!ss_hcd) {
2354 retval = -ENOMEM;
2355 goto dealloc_usb2_hcd;
2356 }
2357
2358 retval = usb_add_hcd(ss_hcd, 0, 0);
2359 if (retval)
2360 goto put_usb3_hcd;
2361 }
2362 return 0;
2363
2364put_usb3_hcd:
2365 usb_put_hcd(ss_hcd);
2366dealloc_usb2_hcd:
2367 usb_put_hcd(hs_hcd);
2368 the_controller.hs_hcd = the_controller.ss_hcd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return retval;
2370}
2371
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002372static int dummy_hcd_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002374 struct dummy *dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002376 dum = (hcd_to_dummy_hcd(platform_get_drvdata(pdev)))->dum;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002377
2378 if (dum->ss_hcd) {
2379 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2380 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2381 }
2382
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002383 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2384 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002385
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002386 the_controller.hs_hcd = NULL;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002387 the_controller.ss_hcd = NULL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002388
Alan Sternd9b76252005-05-03 16:15:43 -04002389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390}
2391
Alan Stern8364d6b2005-11-14 12:16:30 -05002392static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
Alan Stern391eca92005-05-10 15:34:16 -04002393{
2394 struct usb_hcd *hcd;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002395 struct dummy_hcd *dum_hcd;
Alan Stern3cf0a222005-11-29 12:08:15 -05002396 int rc = 0;
Alan Stern391eca92005-05-10 15:34:16 -04002397
Harvey Harrison441b62c2008-03-03 16:08:34 -08002398 dev_dbg (&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002399
Alan Stern3cf0a222005-11-29 12:08:15 -05002400 hcd = platform_get_drvdata (pdev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002401 dum_hcd = hcd_to_dummy_hcd(hcd);
2402 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern3cf0a222005-11-29 12:08:15 -05002403 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2404 rc = -EBUSY;
2405 } else
2406 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2407 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002408}
2409
Alan Stern8364d6b2005-11-14 12:16:30 -05002410static int dummy_hcd_resume (struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04002411{
2412 struct usb_hcd *hcd;
2413
Harvey Harrison441b62c2008-03-03 16:08:34 -08002414 dev_dbg (&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002415
Alan Stern3cf0a222005-11-29 12:08:15 -05002416 hcd = platform_get_drvdata (pdev);
2417 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Alan Stern391eca92005-05-10 15:34:16 -04002418 usb_hcd_poll_rh_status (hcd);
2419 return 0;
2420}
2421
Russell King3ae5eae2005-11-09 22:32:44 +00002422static struct platform_driver dummy_hcd_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04002423 .probe = dummy_hcd_probe,
2424 .remove = dummy_hcd_remove,
Alan Stern391eca92005-05-10 15:34:16 -04002425 .suspend = dummy_hcd_suspend,
2426 .resume = dummy_hcd_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002427 .driver = {
2428 .name = (char *) driver_name,
2429 .owner = THIS_MODULE,
2430 },
Alan Sternd9b76252005-05-03 16:15:43 -04002431};
2432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433/*-------------------------------------------------------------------------*/
2434
Alan Sterna89a2cd2008-04-07 15:03:25 -04002435static struct platform_device *the_udc_pdev;
2436static struct platform_device *the_hcd_pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
2438static int __init init (void)
2439{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002440 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 if (usb_disabled ())
2443 return -ENODEV;
Alan Sternd9b76252005-05-03 16:15:43 -04002444
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +03002445 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2446 return -EINVAL;
2447
Alan Sterna89a2cd2008-04-07 15:03:25 -04002448 the_hcd_pdev = platform_device_alloc(driver_name, -1);
2449 if (!the_hcd_pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 return retval;
Alan Sterna89a2cd2008-04-07 15:03:25 -04002451 the_udc_pdev = platform_device_alloc(gadget_name, -1);
2452 if (!the_udc_pdev)
2453 goto err_alloc_udc;
Alan Sternd9b76252005-05-03 16:15:43 -04002454
Alan Sterna89a2cd2008-04-07 15:03:25 -04002455 retval = platform_driver_register(&dummy_hcd_driver);
2456 if (retval < 0)
2457 goto err_register_hcd_driver;
2458 retval = platform_driver_register(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002459 if (retval < 0)
2460 goto err_register_udc_driver;
2461
Alan Sterna89a2cd2008-04-07 15:03:25 -04002462 retval = platform_device_add(the_hcd_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002463 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002464 goto err_add_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002465 if (!the_controller.hs_hcd ||
2466 (!the_controller.ss_hcd && mod_data.is_super_speed)) {
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002467 /*
2468 * The hcd was added successfully but its probe function failed
2469 * for some reason.
2470 */
2471 retval = -EINVAL;
2472 goto err_add_udc;
2473 }
Alan Sterna89a2cd2008-04-07 15:03:25 -04002474 retval = platform_device_add(the_udc_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002475 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002476 goto err_add_udc;
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002477 if (!platform_get_drvdata(the_udc_pdev)) {
2478 /*
2479 * The udc was added successfully but its probe function failed
2480 * for some reason.
2481 */
2482 retval = -EINVAL;
2483 goto err_probe_udc;
2484 }
Alan Sternd9b76252005-05-03 16:15:43 -04002485 return retval;
2486
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002487err_probe_udc:
2488 platform_device_del(the_udc_pdev);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002489err_add_udc:
2490 platform_device_del(the_hcd_pdev);
2491err_add_hcd:
2492 platform_driver_unregister(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002493err_register_udc_driver:
Alan Sterna89a2cd2008-04-07 15:03:25 -04002494 platform_driver_unregister(&dummy_hcd_driver);
2495err_register_hcd_driver:
2496 platform_device_put(the_udc_pdev);
2497err_alloc_udc:
2498 platform_device_put(the_hcd_pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 return retval;
2500}
2501module_init (init);
2502
2503static void __exit cleanup (void)
2504{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002505 platform_device_unregister(the_udc_pdev);
2506 platform_device_unregister(the_hcd_pdev);
2507 platform_driver_unregister(&dummy_udc_driver);
2508 platform_driver_unregister(&dummy_hcd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510module_exit (cleanup);