blob: 865faf0932afdfe9a3001fcfbe415a6af048e393 [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;
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200432
433 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300434 if (!is_enabled(dum_hcd))
435 return -ESHUTDOWN;
436
437 /*
438 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
439 * maximum packet size.
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300440 * For SS devices the wMaxPacketSize is limited by 1024.
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300441 */
442 max = le16_to_cpu(desc->wMaxPacketSize) & 0x7ff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 /* drivers must not request bad settings, since lower levels
445 * (hardware or its drivers) may not check. some endpoints
446 * can't do iso, many have maxpacket limitations, etc.
447 *
448 * since this "hardware" driver is here to help debugging, we
449 * have some extra sanity checks. (there could be more though,
450 * especially for "ep9out" style fixed function ones.)
451 */
452 retval = -EINVAL;
453 switch (desc->bmAttributes & 0x03) {
454 case USB_ENDPOINT_XFER_BULK:
455 if (strstr (ep->ep.name, "-iso")
456 || strstr (ep->ep.name, "-int")) {
457 goto done;
458 }
459 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300460 case USB_SPEED_SUPER:
461 if (max == 1024)
462 break;
463 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 case USB_SPEED_HIGH:
465 if (max == 512)
466 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700467 goto done;
468 case USB_SPEED_FULL:
469 if (max == 8 || max == 16 || max == 32 || max == 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /* we'll fake any legal size */
471 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700472 /* save a return statement */
473 default:
474 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476 break;
477 case USB_ENDPOINT_XFER_INT:
478 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
479 goto done;
480 /* real hardware might not handle all packet sizes */
481 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300482 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 case USB_SPEED_HIGH:
484 if (max <= 1024)
485 break;
486 /* save a return statement */
487 case USB_SPEED_FULL:
488 if (max <= 64)
489 break;
490 /* save a return statement */
491 default:
492 if (max <= 8)
493 break;
494 goto done;
495 }
496 break;
497 case USB_ENDPOINT_XFER_ISOC:
498 if (strstr (ep->ep.name, "-bulk")
499 || strstr (ep->ep.name, "-int"))
500 goto done;
501 /* real hardware might not handle all packet sizes */
502 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300503 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 case USB_SPEED_HIGH:
505 if (max <= 1024)
506 break;
507 /* save a return statement */
508 case USB_SPEED_FULL:
509 if (max <= 1023)
510 break;
511 /* save a return statement */
512 default:
513 goto done;
514 }
515 break;
516 default:
517 /* few chips support control except on ep0 */
518 goto done;
519 }
520
521 _ep->maxpacket = max;
522 ep->desc = desc;
523
Alan Sternd9b76252005-05-03 16:15:43 -0400524 dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 _ep->name,
526 desc->bEndpointAddress & 0x0f,
527 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
528 ({ char *val;
529 switch (desc->bmAttributes & 0x03) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +0300530 case USB_ENDPOINT_XFER_BULK:
531 val = "bulk";
532 break;
533 case USB_ENDPOINT_XFER_ISOC:
534 val = "iso";
535 break;
536 case USB_ENDPOINT_XFER_INT:
537 val = "intr";
538 break;
539 default:
540 val = "ctrl";
541 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }; val; }),
543 max);
544
545 /* at this point real hardware should be NAKing transfers
546 * to that endpoint, until a buffer is queued to it.
547 */
Alan Stern851a5262008-08-14 15:48:30 -0400548 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 retval = 0;
550done:
551 return retval;
552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554static int dummy_disable (struct usb_ep *_ep)
555{
556 struct dummy_ep *ep;
557 struct dummy *dum;
558 unsigned long flags;
559 int retval;
560
561 ep = usb_ep_to_dummy_ep (_ep);
562 if (!_ep || !ep->desc || _ep->name == ep0name)
563 return -EINVAL;
564 dum = ep_to_dummy (ep);
565
566 spin_lock_irqsave (&dum->lock, flags);
567 ep->desc = NULL;
568 retval = 0;
569 nuke (dum, ep);
570 spin_unlock_irqrestore (&dum->lock, flags);
571
Alan Sternd9b76252005-05-03 16:15:43 -0400572 dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return retval;
574}
575
576static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -0400577dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 struct dummy_ep *ep;
580 struct dummy_request *req;
581
582 if (!_ep)
583 return NULL;
584 ep = usb_ep_to_dummy_ep (_ep);
585
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800586 req = kzalloc(sizeof(*req), mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (!req)
588 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 INIT_LIST_HEAD (&req->queue);
590 return &req->req;
591}
592
593static void
594dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
595{
596 struct dummy_ep *ep;
597 struct dummy_request *req;
598
599 ep = usb_ep_to_dummy_ep (_ep);
600 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
601 return;
602
603 req = usb_request_to_dummy_request (_req);
604 WARN_ON (!list_empty (&req->queue));
605 kfree (req);
606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608static void
609fifo_complete (struct usb_ep *ep, struct usb_request *req)
610{
611}
612
613static int
Olav Kongas5db539e2005-06-23 20:25:36 +0300614dummy_queue (struct usb_ep *_ep, struct usb_request *_req,
Al Viro55016f12005-10-21 03:21:58 -0400615 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 struct dummy_ep *ep;
618 struct dummy_request *req;
619 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300620 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 unsigned long flags;
622
623 req = usb_request_to_dummy_request (_req);
624 if (!_req || !list_empty (&req->queue) || !_req->complete)
625 return -EINVAL;
626
627 ep = usb_ep_to_dummy_ep (_ep);
628 if (!_ep || (!ep->desc && _ep->name != ep0name))
629 return -EINVAL;
630
631 dum = ep_to_dummy (ep);
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200632 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300633 if (!dum->driver || !is_enabled(dum_hcd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return -ESHUTDOWN;
635
636#if 0
Alan Sternd9b76252005-05-03 16:15:43 -0400637 dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 ep, _req, _ep->name, _req->length, _req->buf);
639#endif
640
641 _req->status = -EINPROGRESS;
642 _req->actual = 0;
643 spin_lock_irqsave (&dum->lock, flags);
644
645 /* implement an emulated single-request FIFO */
646 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
647 list_empty (&dum->fifo_req.queue) &&
648 list_empty (&ep->queue) &&
649 _req->length <= FIFO_SIZE) {
650 req = &dum->fifo_req;
651 req->req = *_req;
652 req->req.buf = dum->fifo_buf;
653 memcpy (dum->fifo_buf, _req->buf, _req->length);
654 req->req.context = dum;
655 req->req.complete = fifo_complete;
656
David Brownellc728df72008-07-26 08:06:24 -0700657 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 spin_unlock (&dum->lock);
659 _req->actual = _req->length;
660 _req->status = 0;
661 _req->complete (_ep, _req);
662 spin_lock (&dum->lock);
David Brownellc728df72008-07-26 08:06:24 -0700663 } else
664 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 spin_unlock_irqrestore (&dum->lock, flags);
666
667 /* real hardware would likely enable transfers here, in case
668 * it'd been left NAKing.
669 */
670 return 0;
671}
672
673static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
674{
675 struct dummy_ep *ep;
676 struct dummy *dum;
677 int retval = -EINVAL;
678 unsigned long flags;
679 struct dummy_request *req = NULL;
680
681 if (!_ep || !_req)
682 return retval;
683 ep = usb_ep_to_dummy_ep (_ep);
684 dum = ep_to_dummy (ep);
685
686 if (!dum->driver)
687 return -ESHUTDOWN;
688
Alan Sternb4dbda12006-07-28 17:07:34 -0400689 local_irq_save (flags);
690 spin_lock (&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 list_for_each_entry (req, &ep->queue, queue) {
692 if (&req->req == _req) {
693 list_del_init (&req->queue);
694 _req->status = -ECONNRESET;
695 retval = 0;
696 break;
697 }
698 }
Alan Sternb4dbda12006-07-28 17:07:34 -0400699 spin_unlock (&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 if (retval == 0) {
Alan Sternd9b76252005-05-03 16:15:43 -0400702 dev_dbg (udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 "dequeued req %p from %s, len %d buf %p\n",
704 req, _ep->name, _req->length, _req->buf);
705 _req->complete (_ep, _req);
706 }
Alan Sternb4dbda12006-07-28 17:07:34 -0400707 local_irq_restore (flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return retval;
709}
710
711static int
Alan Stern851a5262008-08-14 15:48:30 -0400712dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 struct dummy_ep *ep;
715 struct dummy *dum;
716
717 if (!_ep)
718 return -EINVAL;
719 ep = usb_ep_to_dummy_ep (_ep);
720 dum = ep_to_dummy (ep);
721 if (!dum->driver)
722 return -ESHUTDOWN;
723 if (!value)
Alan Stern851a5262008-08-14 15:48:30 -0400724 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
726 !list_empty (&ep->queue))
727 return -EAGAIN;
Alan Stern851a5262008-08-14 15:48:30 -0400728 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 ep->halted = 1;
Alan Stern851a5262008-08-14 15:48:30 -0400730 if (wedged)
731 ep->wedged = 1;
732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /* FIXME clear emulated data toggle too */
734 return 0;
735}
736
Alan Stern851a5262008-08-14 15:48:30 -0400737static int
738dummy_set_halt(struct usb_ep *_ep, int value)
739{
740 return dummy_set_halt_and_wedge(_ep, value, 0);
741}
742
743static int dummy_set_wedge(struct usb_ep *_ep)
744{
745 if (!_ep || _ep->name == ep0name)
746 return -EINVAL;
747 return dummy_set_halt_and_wedge(_ep, 1, 1);
748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750static const struct usb_ep_ops dummy_ep_ops = {
751 .enable = dummy_enable,
752 .disable = dummy_disable,
753
754 .alloc_request = dummy_alloc_request,
755 .free_request = dummy_free_request,
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 .queue = dummy_queue,
758 .dequeue = dummy_dequeue,
759
760 .set_halt = dummy_set_halt,
Alan Stern851a5262008-08-14 15:48:30 -0400761 .set_wedge = dummy_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762};
763
764/*-------------------------------------------------------------------------*/
765
766/* there are both host and device side versions of this call ... */
767static int dummy_g_get_frame (struct usb_gadget *_gadget)
768{
769 struct timeval tv;
770
771 do_gettimeofday (&tv);
772 return tv.tv_usec / 1000;
773}
774
775static int dummy_wakeup (struct usb_gadget *_gadget)
776{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300777 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300779 dum_hcd = gadget_to_dummy_hcd(_gadget);
780 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
Alan Stern5742b0c2005-05-02 11:25:17 -0400781 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return -EINVAL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300783 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
Alan Stern391eca92005-05-10 15:34:16 -0400784 return -ENOLINK;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300785 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
786 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -0400787 return -EIO;
788
789 /* FIXME: What if the root hub is suspended but the port isn't? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 /* hub notices our request, issues downstream resume, etc */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300792 dum_hcd->resuming = 1;
793 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
794 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return 0;
796}
797
798static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
799{
800 struct dummy *dum;
801
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300802 dum = (gadget_to_dummy_hcd(_gadget))->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (value)
804 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
805 else
806 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
807 return 0;
808}
809
Alan Sternf1c39fa2005-05-03 16:24:04 -0400810static int dummy_pullup (struct usb_gadget *_gadget, int value)
811{
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200812 struct dummy_hcd *dum_hcd;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400813 struct dummy *dum;
814 unsigned long flags;
815
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200816 dum_hcd = gadget_to_dummy_hcd(_gadget);
817 dum = dum_hcd->dum;
818
Alan Sternf1c39fa2005-05-03 16:24:04 -0400819 spin_lock_irqsave (&dum->lock, flags);
820 dum->pullup = (value != 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200821 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400822 spin_unlock_irqrestore (&dum->lock, flags);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200823 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Sternf1c39fa2005-05-03 16:24:04 -0400824 return 0;
825}
826
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300827static int dummy_udc_start(struct usb_gadget_driver *driver,
828 int (*bind)(struct usb_gadget *));
829static int dummy_udc_stop(struct usb_gadget_driver *driver);
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831static const struct usb_gadget_ops dummy_ops = {
832 .get_frame = dummy_g_get_frame,
833 .wakeup = dummy_wakeup,
834 .set_selfpowered = dummy_set_selfpowered,
Alan Sternf1c39fa2005-05-03 16:24:04 -0400835 .pullup = dummy_pullup,
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300836 .start = dummy_udc_start,
837 .stop = dummy_udc_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838};
839
840/*-------------------------------------------------------------------------*/
841
842/* "function" sysfs attribute */
843static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400844show_function (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
846 struct dummy *dum = gadget_dev_to_dummy (dev);
847
848 if (!dum->driver || !dum->driver->function)
849 return 0;
850 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
851}
Alan Sterncc095b02005-05-10 15:28:38 -0400852static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854/*-------------------------------------------------------------------------*/
855
856/*
857 * Driver registration/unregistration.
858 *
859 * This is basically hardware-specific; there's usually only one real USB
860 * device (not host) controller since that's how USB devices are intended
861 * to work. So most implementations of these api calls will rely on the
862 * fact that only one driver will ever bind to the hardware. But curious
863 * hardware can be built with discrete components, so the gadget API doesn't
864 * require that assumption.
865 *
866 * For this emulator, it might be convenient to create a usb slave device
867 * for each driver that registers: just add to a big root hub.
868 */
869
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300870static int dummy_udc_start(struct usb_gadget_driver *driver,
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +0200871 int (*bind)(struct usb_gadget *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300873 struct dummy *dum = &the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 int retval, i;
875
876 if (!dum)
877 return -EINVAL;
878 if (dum->driver)
879 return -EBUSY;
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +0200880 if (!bind || !driver->setup || driver->speed == USB_SPEED_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return -EINVAL;
882
883 /*
884 * SLAVE side init ... the layer above hardware, which
885 * can't enumerate without help from the driver we're binding.
886 */
Alan Stern5742b0c2005-05-02 11:25:17 -0400887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 dum->devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 INIT_LIST_HEAD (&dum->gadget.ep_list);
891 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
892 struct dummy_ep *ep = &dum->ep [i];
893
894 if (!ep_name [i])
895 break;
896 ep->ep.name = ep_name [i];
897 ep->ep.ops = &dummy_ep_ops;
898 list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list);
Alan Stern851a5262008-08-14 15:48:30 -0400899 ep->halted = ep->wedged = ep->already_seen =
900 ep->setup_stage = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 ep->ep.maxpacket = ~0;
902 ep->last_io = jiffies;
903 ep->gadget = &dum->gadget;
904 ep->desc = NULL;
905 INIT_LIST_HEAD (&ep->queue);
906 }
907
908 dum->gadget.ep0 = &dum->ep [0].ep;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300909 if (mod_data.is_super_speed)
910 dum->gadget.speed = driver->speed;
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +0300911 else if (mod_data.is_high_speed)
912 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH, driver->speed);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300913 else
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +0300914 dum->gadget.speed = USB_SPEED_FULL;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300915 if (dum->gadget.speed < driver->speed)
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +0300916 dev_dbg(udc_dev(dum), "This device can perform faster"
917 " if you connect it to a %s port...\n",
918 (driver->speed == USB_SPEED_SUPER ?
919 "SuperSpeed" : "HighSpeed"));
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300920
921 if (dum->gadget.speed == USB_SPEED_SUPER) {
922 for (i = 0; i < DUMMY_ENDPOINTS; i++)
923 dum->ep[i].ep.max_streams = 0x10;
924 dum->ep[0].ep.maxpacket = 9;
925 } else
926 dum->ep[0].ep.maxpacket = 64;
Sebastian Andrzej Siewior99fd1402011-06-16 20:36:56 +0200927
928 if (dum->gadget.speed == USB_SPEED_SUPER)
929 dum->gadget.is_otg =
930 (dummy_hcd_to_hcd(dum->ss_hcd)->self.otg_port != 0);
931 else
932 dum->gadget.is_otg =
933 (dummy_hcd_to_hcd(dum->hs_hcd)->self.otg_port != 0);
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 list_del_init (&dum->ep [0].ep.ep_list);
936 INIT_LIST_HEAD(&dum->fifo_req.queue);
937
Alan Stern59331012007-11-20 16:28:55 -0500938 driver->driver.bus = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 dum->driver = driver;
940 dum->gadget.dev.driver = &driver->driver;
Alan Sternd9b76252005-05-03 16:15:43 -0400941 dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 driver->driver.name);
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +0200943 retval = bind(&dum->gadget);
Alan Stern59331012007-11-20 16:28:55 -0500944 if (retval) {
945 dum->driver = NULL;
946 dum->gadget.dev.driver = NULL;
947 return retval;
948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Sebastian Andrzej Siewior25427872011-06-16 20:36:55 +0200950 /* khubd will enumerate this in a while */
951 dummy_pullup(&dum->gadget, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return 0;
953}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300955static int dummy_udc_stop(struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300957 struct dummy *dum = &the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 if (!dum)
960 return -ENODEV;
David Brownell6bea4762006-12-05 03:15:33 -0800961 if (!driver || driver != dum->driver || !driver->unbind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return -EINVAL;
963
Alan Sternd9b76252005-05-03 16:15:43 -0400964 dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 driver->driver.name);
966
Sebastian Andrzej Siewior25427872011-06-16 20:36:55 +0200967 dummy_pullup(&dum->gadget, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 driver->unbind (&dum->gadget);
Alan Stern59331012007-11-20 16:28:55 -0500970 dum->gadget.dev.driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 dum->driver = NULL;
Sebastian Andrzej Siewior25427872011-06-16 20:36:55 +0200972
973 dummy_pullup(&dum->gadget, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return 0;
975}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977#undef is_enabled
978
Alan Sterncc095b02005-05-10 15:28:38 -0400979/* just declare this in any driver that really need it */
980extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
983{
984 return -ENOSYS;
985}
986EXPORT_SYMBOL (net2280_set_fifo_mode);
987
Alan Sternd9b76252005-05-03 16:15:43 -0400988
989/* The gadget structure is stored inside the hcd structure and will be
990 * released along with it. */
991static void
992dummy_gadget_release (struct device *dev)
993{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300994 return;
Alan Sternd9b76252005-05-03 16:15:43 -0400995}
996
Alan Stern8364d6b2005-11-14 12:16:30 -0500997static int dummy_udc_probe (struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -0400998{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300999 struct dummy *dum = &the_controller;
Alan Sternd9b76252005-05-03 16:15:43 -04001000 int rc;
1001
1002 dum->gadget.name = gadget_name;
1003 dum->gadget.ops = &dummy_ops;
1004 dum->gadget.is_dualspeed = 1;
1005
Kay Sievers0031a062008-05-02 06:02:41 +02001006 dev_set_name(&dum->gadget.dev, "gadget");
Alan Stern8364d6b2005-11-14 12:16:30 -05001007 dum->gadget.dev.parent = &pdev->dev;
Alan Sternd9b76252005-05-03 16:15:43 -04001008 dum->gadget.dev.release = dummy_gadget_release;
1009 rc = device_register (&dum->gadget.dev);
Rahul Ruikar75d87cd2010-10-07 09:40:45 +05301010 if (rc < 0) {
1011 put_device(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -04001012 return rc;
Rahul Ruikar75d87cd2010-10-07 09:40:45 +05301013 }
Alan Sternd9b76252005-05-03 16:15:43 -04001014
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001015 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
1016 if (rc < 0)
1017 goto err_udc;
1018
Alan Sternefd54a32006-09-25 11:55:56 -04001019 rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
1020 if (rc < 0)
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001021 goto err_dev;
1022 platform_set_drvdata(pdev, dum);
1023 return rc;
1024
1025err_dev:
1026 usb_del_gadget_udc(&dum->gadget);
1027err_udc:
1028 device_unregister(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -04001029 return rc;
1030}
1031
Alan Stern8364d6b2005-11-14 12:16:30 -05001032static int dummy_udc_remove (struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -04001033{
Alan Stern8364d6b2005-11-14 12:16:30 -05001034 struct dummy *dum = platform_get_drvdata (pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04001035
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001036 usb_del_gadget_udc(&dum->gadget);
Alan Stern8364d6b2005-11-14 12:16:30 -05001037 platform_set_drvdata (pdev, NULL);
Alan Sternd9b76252005-05-03 16:15:43 -04001038 device_remove_file (&dum->gadget.dev, &dev_attr_function);
1039 device_unregister (&dum->gadget.dev);
1040 return 0;
1041}
1042
Alan Stern8364d6b2005-11-14 12:16:30 -05001043static int dummy_udc_suspend (struct platform_device *pdev, pm_message_t state)
Alan Stern391eca92005-05-10 15:34:16 -04001044{
Alan Stern8364d6b2005-11-14 12:16:30 -05001045 struct dummy *dum = platform_get_drvdata(pdev);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001046 struct dummy_hcd *dum_hcd;
Alan Stern391eca92005-05-10 15:34:16 -04001047
Harvey Harrison441b62c2008-03-03 16:08:34 -08001048 dev_dbg (&pdev->dev, "%s\n", __func__);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001049 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Alan Stern391eca92005-05-10 15:34:16 -04001050 spin_lock_irq (&dum->lock);
1051 dum->udc_suspended = 1;
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001052 set_link_state(dum_hcd);
Alan Stern391eca92005-05-10 15:34:16 -04001053 spin_unlock_irq (&dum->lock);
1054
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001055 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001056 return 0;
1057}
1058
Alan Stern8364d6b2005-11-14 12:16:30 -05001059static int dummy_udc_resume (struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04001060{
Alan Stern8364d6b2005-11-14 12:16:30 -05001061 struct dummy *dum = platform_get_drvdata(pdev);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001062 struct dummy_hcd *dum_hcd;
Alan Stern391eca92005-05-10 15:34:16 -04001063
Harvey Harrison441b62c2008-03-03 16:08:34 -08001064 dev_dbg (&pdev->dev, "%s\n", __func__);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001065 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Alan Stern391eca92005-05-10 15:34:16 -04001066 spin_lock_irq (&dum->lock);
1067 dum->udc_suspended = 0;
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001068 set_link_state(dum_hcd);
Alan Stern391eca92005-05-10 15:34:16 -04001069 spin_unlock_irq (&dum->lock);
1070
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001071 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001072 return 0;
1073}
1074
Russell King3ae5eae2005-11-09 22:32:44 +00001075static struct platform_driver dummy_udc_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04001076 .probe = dummy_udc_probe,
1077 .remove = dummy_udc_remove,
Alan Stern391eca92005-05-10 15:34:16 -04001078 .suspend = dummy_udc_suspend,
1079 .resume = dummy_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001080 .driver = {
1081 .name = (char *) gadget_name,
1082 .owner = THIS_MODULE,
1083 },
Alan Sternd9b76252005-05-03 16:15:43 -04001084};
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086/*-------------------------------------------------------------------------*/
1087
1088/* MASTER/HOST SIDE DRIVER
1089 *
1090 * this uses the hcd framework to hook up to host side drivers.
1091 * its root hub will only have one device, otherwise it acts like
1092 * a normal host controller.
1093 *
1094 * when urbs are queued, they're just stuck on a list that we
1095 * scan in a timer callback. that callback connects writes from
1096 * the host with reads from the device, and so on, based on the
1097 * usb 2.0 rules.
1098 */
1099
1100static int dummy_urb_enqueue (
1101 struct usb_hcd *hcd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001103 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001105 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 struct urbp *urbp;
1107 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001108 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1111 return -EINVAL;
1112
1113 urbp = kmalloc (sizeof *urbp, mem_flags);
1114 if (!urbp)
1115 return -ENOMEM;
1116 urbp->urb = urb;
1117
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001118 dum_hcd = hcd_to_dummy_hcd(hcd);
1119 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001120 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1121 if (rc) {
1122 kfree(urbp);
1123 goto done;
1124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001126 if (!dum_hcd->udev) {
1127 dum_hcd->udev = urb->dev;
1128 usb_get_dev(dum_hcd->udev);
1129 } else if (unlikely(dum_hcd->udev != urb->dev))
1130 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001132 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 urb->hcpriv = urbp;
1134 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
1135 urb->error_count = 1; /* mark as a new urb */
1136
1137 /* kick the scheduler, it'll do the rest */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001138 if (!timer_pending(&dum_hcd->timer))
1139 mod_timer(&dum_hcd->timer, jiffies + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Alan Sterne9df41c2007-08-08 11:48:02 -04001141 done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001142 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001143 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
Alan Sterne9df41c2007-08-08 11:48:02 -04001146static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001148 struct dummy_hcd *dum_hcd;
Alan Stern391eca92005-05-10 15:34:16 -04001149 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001150 int rc;
Alan Stern391eca92005-05-10 15:34:16 -04001151
1152 /* giveback happens automatically in timer callback,
1153 * so make sure the callback happens */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001154 dum_hcd = hcd_to_dummy_hcd(hcd);
1155 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001156
1157 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001158 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1159 !list_empty(&dum_hcd->urbp_list))
1160 mod_timer(&dum_hcd->timer, jiffies);
Alan Sterne9df41c2007-08-08 11:48:02 -04001161
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001162 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001163 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166/* transfer up to a frame's worth; caller must own lock */
1167static int
Alan Stern4d2f1102007-08-24 15:40:10 -04001168transfer(struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit,
1169 int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 struct dummy_request *req;
1172
1173top:
1174 /* if there's no request queued, the device is NAKing; return */
1175 list_for_each_entry (req, &ep->queue, queue) {
1176 unsigned host_len, dev_len, len;
1177 int is_short, to_host;
1178 int rescan = 0;
1179
1180 /* 1..N packets of ep->ep.maxpacket each ... the last one
1181 * may be short (including zero length).
1182 *
1183 * writer can send a zlp explicitly (length 0) or implicitly
1184 * (length mod maxpacket zero, and 'zero' flag); they always
1185 * terminate reads.
1186 */
1187 host_len = urb->transfer_buffer_length - urb->actual_length;
1188 dev_len = req->req.length - req->req.actual;
1189 len = min (host_len, dev_len);
1190
1191 /* FIXME update emulated data toggle too */
1192
1193 to_host = usb_pipein (urb->pipe);
1194 if (unlikely (len == 0))
1195 is_short = 1;
1196 else {
1197 char *ubuf, *rbuf;
1198
1199 /* not enough bandwidth left? */
1200 if (limit < ep->ep.maxpacket && limit < len)
1201 break;
1202 len = min (len, (unsigned) limit);
1203 if (len == 0)
1204 break;
1205
1206 /* use an extra pass for the final short packet */
1207 if (len > ep->ep.maxpacket) {
1208 rescan = 1;
1209 len -= (len % ep->ep.maxpacket);
1210 }
1211 is_short = (len % ep->ep.maxpacket) != 0;
1212
1213 /* else transfer packet(s) */
1214 ubuf = urb->transfer_buffer + urb->actual_length;
1215 rbuf = req->req.buf + req->req.actual;
1216 if (to_host)
1217 memcpy (ubuf, rbuf, len);
1218 else
1219 memcpy (rbuf, ubuf, len);
1220 ep->last_io = jiffies;
1221
1222 limit -= len;
1223 urb->actual_length += len;
1224 req->req.actual += len;
1225 }
1226
1227 /* short packets terminate, maybe with overflow/underflow.
1228 * it's only really an error to write too much.
1229 *
1230 * partially filling a buffer optionally blocks queue advances
1231 * (so completion handlers can clean up the queue) but we don't
Alan Sternb0d9efb2007-08-21 15:39:21 -04001232 * need to emulate such data-in-flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 */
1234 if (is_short) {
1235 if (host_len == dev_len) {
1236 req->req.status = 0;
Alan Stern4d2f1102007-08-24 15:40:10 -04001237 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 } else if (to_host) {
1239 req->req.status = 0;
1240 if (dev_len > host_len)
Alan Stern4d2f1102007-08-24 15:40:10 -04001241 *status = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 else
Alan Stern4d2f1102007-08-24 15:40:10 -04001243 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 } else if (!to_host) {
Alan Stern4d2f1102007-08-24 15:40:10 -04001245 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (host_len > dev_len)
1247 req->req.status = -EOVERFLOW;
1248 else
1249 req->req.status = 0;
1250 }
1251
1252 /* many requests terminate without a short packet */
1253 } else {
1254 if (req->req.length == req->req.actual
1255 && !req->req.zero)
1256 req->req.status = 0;
1257 if (urb->transfer_buffer_length == urb->actual_length
1258 && !(urb->transfer_flags
Alan Stern4d2f1102007-08-24 15:40:10 -04001259 & URB_ZERO_PACKET))
1260 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
1262
1263 /* device side completion --> continuable */
1264 if (req->req.status != -EINPROGRESS) {
1265 list_del_init (&req->queue);
1266
1267 spin_unlock (&dum->lock);
1268 req->req.complete (&ep->ep, &req->req);
1269 spin_lock (&dum->lock);
1270
1271 /* requests might have been unlinked... */
1272 rescan = 1;
1273 }
1274
1275 /* host side completion --> terminate */
Alan Stern4d2f1102007-08-24 15:40:10 -04001276 if (*status != -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 break;
1278
1279 /* rescan to continue with any other queued i/o */
1280 if (rescan)
1281 goto top;
1282 }
1283 return limit;
1284}
1285
1286static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1287{
1288 int limit = ep->ep.maxpacket;
1289
1290 if (dum->gadget.speed == USB_SPEED_HIGH) {
1291 int tmp;
1292
1293 /* high bandwidth mode */
1294 tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 tmp = (tmp >> 11) & 0x03;
1296 tmp *= 8 /* applies to entire frame */;
1297 limit += limit * tmp;
1298 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001299 if (dum->gadget.speed == USB_SPEED_SUPER) {
1300 switch (ep->desc->bmAttributes & 0x03) {
1301 case USB_ENDPOINT_XFER_ISOC:
1302 /* Sec. 4.4.8.2 USB3.0 Spec */
1303 limit = 3 * 16 * 1024 * 8;
1304 break;
1305 case USB_ENDPOINT_XFER_INT:
1306 /* Sec. 4.4.7.2 USB3.0 Spec */
1307 limit = 3 * 1024 * 8;
1308 break;
1309 case USB_ENDPOINT_XFER_BULK:
1310 default:
1311 break;
1312 }
1313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return limit;
1315}
1316
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001317#define is_active(dum_hcd) ((dum_hcd->port_status & \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1319 USB_PORT_STAT_SUSPEND)) \
1320 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1321
1322static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1323{
1324 int i;
1325
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001326 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1327 dum->ss_hcd : dum->hs_hcd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return NULL;
1329 if ((address & ~USB_DIR_IN) == 0)
1330 return &dum->ep [0];
1331 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1332 struct dummy_ep *ep = &dum->ep [i];
1333
1334 if (!ep->desc)
1335 continue;
1336 if (ep->desc->bEndpointAddress == address)
1337 return ep;
1338 }
1339 return NULL;
1340}
1341
1342#undef is_active
1343
1344#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1345#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1346#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1347#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1348#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1349#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1350
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001351
1352/**
1353 * handle_control_request() - handles all control transfers
1354 * @dum: pointer to dummy (the_controller)
1355 * @urb: the urb request to handle
1356 * @setup: pointer to the setup data for a USB device control
1357 * request
1358 * @status: pointer to request handling status
1359 *
1360 * Return 0 - if the request was handled
1361 * 1 - if the request wasn't handles
1362 * error code on error
1363 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001364static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001365 struct usb_ctrlrequest *setup,
1366 int *status)
1367{
1368 struct dummy_ep *ep2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001369 struct dummy *dum = dum_hcd->dum;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001370 int ret_val = 1;
1371 unsigned w_index;
1372 unsigned w_value;
1373
1374 w_index = le16_to_cpu(setup->wIndex);
1375 w_value = le16_to_cpu(setup->wValue);
1376 switch (setup->bRequest) {
1377 case USB_REQ_SET_ADDRESS:
1378 if (setup->bRequestType != Dev_Request)
1379 break;
1380 dum->address = w_value;
1381 *status = 0;
1382 dev_dbg(udc_dev(dum), "set_address = %d\n",
1383 w_value);
1384 ret_val = 0;
1385 break;
1386 case USB_REQ_SET_FEATURE:
1387 if (setup->bRequestType == Dev_Request) {
1388 ret_val = 0;
1389 switch (w_value) {
1390 case USB_DEVICE_REMOTE_WAKEUP:
1391 break;
1392 case USB_DEVICE_B_HNP_ENABLE:
1393 dum->gadget.b_hnp_enable = 1;
1394 break;
1395 case USB_DEVICE_A_HNP_SUPPORT:
1396 dum->gadget.a_hnp_support = 1;
1397 break;
1398 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1399 dum->gadget.a_alt_hnp_support = 1;
1400 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001401 case USB_DEVICE_U1_ENABLE:
1402 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1403 HCD_USB3)
1404 w_value = USB_DEV_STAT_U1_ENABLED;
1405 else
1406 ret_val = -EOPNOTSUPP;
1407 break;
1408 case USB_DEVICE_U2_ENABLE:
1409 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1410 HCD_USB3)
1411 w_value = USB_DEV_STAT_U2_ENABLED;
1412 else
1413 ret_val = -EOPNOTSUPP;
1414 break;
1415 case USB_DEVICE_LTM_ENABLE:
1416 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1417 HCD_USB3)
1418 w_value = USB_DEV_STAT_LTM_ENABLED;
1419 else
1420 ret_val = -EOPNOTSUPP;
1421 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001422 default:
1423 ret_val = -EOPNOTSUPP;
1424 }
1425 if (ret_val == 0) {
1426 dum->devstatus |= (1 << w_value);
1427 *status = 0;
1428 }
1429 } else if (setup->bRequestType == Ep_Request) {
1430 /* endpoint halt */
1431 ep2 = find_endpoint(dum, w_index);
1432 if (!ep2 || ep2->ep.name == ep0name) {
1433 ret_val = -EOPNOTSUPP;
1434 break;
1435 }
1436 ep2->halted = 1;
1437 ret_val = 0;
1438 *status = 0;
1439 }
1440 break;
1441 case USB_REQ_CLEAR_FEATURE:
1442 if (setup->bRequestType == Dev_Request) {
1443 ret_val = 0;
1444 switch (w_value) {
1445 case USB_DEVICE_REMOTE_WAKEUP:
1446 w_value = USB_DEVICE_REMOTE_WAKEUP;
1447 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001448 case USB_DEVICE_U1_ENABLE:
1449 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1450 HCD_USB3)
1451 w_value = USB_DEV_STAT_U1_ENABLED;
1452 else
1453 ret_val = -EOPNOTSUPP;
1454 break;
1455 case USB_DEVICE_U2_ENABLE:
1456 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1457 HCD_USB3)
1458 w_value = USB_DEV_STAT_U2_ENABLED;
1459 else
1460 ret_val = -EOPNOTSUPP;
1461 break;
1462 case USB_DEVICE_LTM_ENABLE:
1463 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1464 HCD_USB3)
1465 w_value = USB_DEV_STAT_LTM_ENABLED;
1466 else
1467 ret_val = -EOPNOTSUPP;
1468 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001469 default:
1470 ret_val = -EOPNOTSUPP;
1471 break;
1472 }
1473 if (ret_val == 0) {
1474 dum->devstatus &= ~(1 << w_value);
1475 *status = 0;
1476 }
1477 } else if (setup->bRequestType == Ep_Request) {
1478 /* endpoint halt */
1479 ep2 = find_endpoint(dum, w_index);
1480 if (!ep2) {
1481 ret_val = -EOPNOTSUPP;
1482 break;
1483 }
1484 if (!ep2->wedged)
1485 ep2->halted = 0;
1486 ret_val = 0;
1487 *status = 0;
1488 }
1489 break;
1490 case USB_REQ_GET_STATUS:
1491 if (setup->bRequestType == Dev_InRequest
1492 || setup->bRequestType == Intf_InRequest
1493 || setup->bRequestType == Ep_InRequest) {
1494 char *buf;
1495 /*
1496 * device: remote wakeup, selfpowered
1497 * interface: nothing
1498 * endpoint: halt
1499 */
1500 buf = (char *)urb->transfer_buffer;
1501 if (urb->transfer_buffer_length > 0) {
1502 if (setup->bRequestType == Ep_InRequest) {
1503 ep2 = find_endpoint(dum, w_index);
1504 if (!ep2) {
1505 ret_val = -EOPNOTSUPP;
1506 break;
1507 }
1508 buf[0] = ep2->halted;
1509 } else if (setup->bRequestType ==
1510 Dev_InRequest) {
1511 buf[0] = (u8)dum->devstatus;
1512 } else
1513 buf[0] = 0;
1514 }
1515 if (urb->transfer_buffer_length > 1)
1516 buf[1] = 0;
1517 urb->actual_length = min_t(u32, 2,
1518 urb->transfer_buffer_length);
1519 ret_val = 0;
1520 *status = 0;
1521 }
1522 break;
1523 }
1524 return ret_val;
1525}
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527/* drive both sides of the transfers; looks like irq handlers to
1528 * both drivers except the callbacks aren't in_irq().
1529 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001530static void dummy_timer(unsigned long _dum_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001532 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1533 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 struct urbp *urbp, *tmp;
1535 unsigned long flags;
1536 int limit, total;
1537 int i;
1538
1539 /* simplistic model for one frame's bandwidth */
1540 switch (dum->gadget.speed) {
1541 case USB_SPEED_LOW:
1542 total = 8/*bytes*/ * 12/*packets*/;
1543 break;
1544 case USB_SPEED_FULL:
1545 total = 64/*bytes*/ * 19/*packets*/;
1546 break;
1547 case USB_SPEED_HIGH:
1548 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1549 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001550 case USB_SPEED_SUPER:
1551 /* Bus speed is 500000 bytes/ms, so use a little less */
1552 total = 490000;
1553 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001555 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return;
1557 }
1558
1559 /* FIXME if HZ != 1000 this will probably misbehave ... */
1560
1561 /* look at each urb queued by the host side driver */
1562 spin_lock_irqsave (&dum->lock, flags);
1563
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001564 if (!dum_hcd->udev) {
1565 dev_err(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 "timer fired with no URBs pending?\n");
1567 spin_unlock_irqrestore (&dum->lock, flags);
1568 return;
1569 }
1570
1571 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1572 if (!ep_name [i])
1573 break;
1574 dum->ep [i].already_seen = 0;
1575 }
1576
1577restart:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001578 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 struct urb *urb;
1580 struct dummy_request *req;
1581 u8 address;
1582 struct dummy_ep *ep = NULL;
1583 int type;
Alan Stern4d2f1102007-08-24 15:40:10 -04001584 int status = -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 urb = urbp->urb;
Alan Sterneb231052007-08-21 15:40:36 -04001587 if (urb->unlinked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 goto return_urb;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001589 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
Alan Stern391eca92005-05-10 15:34:16 -04001590 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 type = usb_pipetype (urb->pipe);
1592
1593 /* used up this frame's non-periodic bandwidth?
1594 * FIXME there's infinite bandwidth for control and
1595 * periodic transfers ... unrealistic.
1596 */
1597 if (total <= 0 && type == PIPE_BULK)
1598 continue;
1599
1600 /* find the gadget's ep for this request (if configured) */
1601 address = usb_pipeendpoint (urb->pipe);
1602 if (usb_pipein (urb->pipe))
1603 address |= USB_DIR_IN;
1604 ep = find_endpoint(dum, address);
1605 if (!ep) {
1606 /* set_configuration() disagreement */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001607 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 "no ep configured for urb %p\n",
1609 urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001610 status = -EPROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 goto return_urb;
1612 }
1613
1614 if (ep->already_seen)
1615 continue;
1616 ep->already_seen = 1;
1617 if (ep == &dum->ep [0] && urb->error_count) {
1618 ep->setup_stage = 1; /* a new urb */
1619 urb->error_count = 0;
1620 }
1621 if (ep->halted && !ep->setup_stage) {
1622 /* NOTE: must not be iso! */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001623 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 ep->ep.name, urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001625 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 goto return_urb;
1627 }
1628 /* FIXME make sure both ends agree on maxpacket */
1629
1630 /* handle control requests */
1631 if (ep == &dum->ep [0] && ep->setup_stage) {
1632 struct usb_ctrlrequest setup;
1633 int value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 /* paranoia, in case of stale queued data */
1637 list_for_each_entry (req, &ep->queue, queue) {
1638 list_del_init (&req->queue);
1639 req->req.status = -EOVERFLOW;
Alan Sternd9b76252005-05-03 16:15:43 -04001640 dev_dbg (udc_dev(dum), "stale req = %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 req);
1642
1643 spin_unlock (&dum->lock);
1644 req->req.complete (&ep->ep, &req->req);
1645 spin_lock (&dum->lock);
1646 ep->already_seen = 0;
1647 goto restart;
1648 }
1649
1650 /* gadget driver never sees set_address or operations
1651 * on standard feature flags. some hardware doesn't
1652 * even expose them.
1653 */
1654 ep->last_io = jiffies;
1655 ep->setup_stage = 0;
1656 ep->halted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001658 value = handle_control_request(dum_hcd, urb, &setup,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001659 &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 /* gadget driver handles all other requests. block
1662 * until setup() returns; no reentrancy issues etc.
1663 */
1664 if (value > 0) {
1665 spin_unlock (&dum->lock);
1666 value = dum->driver->setup (&dum->gadget,
1667 &setup);
1668 spin_lock (&dum->lock);
1669
1670 if (value >= 0) {
1671 /* no delays (max 64KB data stage) */
1672 limit = 64*1024;
1673 goto treat_control_like_bulk;
1674 }
1675 /* error, see below */
1676 }
1677
1678 if (value < 0) {
1679 if (value != -EOPNOTSUPP)
Alan Sternd9b76252005-05-03 16:15:43 -04001680 dev_dbg (udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 "setup --> %d\n",
1682 value);
Alan Stern4d2f1102007-08-24 15:40:10 -04001683 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 urb->actual_length = 0;
1685 }
1686
1687 goto return_urb;
1688 }
1689
1690 /* non-control requests */
1691 limit = total;
1692 switch (usb_pipetype (urb->pipe)) {
1693 case PIPE_ISOCHRONOUS:
1694 /* FIXME is it urb->interval since the last xfer?
1695 * use urb->iso_frame_desc[i].
1696 * complete whether or not ep has requests queued.
1697 * report random errors, to debug drivers.
1698 */
1699 limit = max (limit, periodic_bytes (dum, ep));
Alan Stern4d2f1102007-08-24 15:40:10 -04001700 status = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 break;
1702
1703 case PIPE_INTERRUPT:
1704 /* FIXME is it urb->interval since the last xfer?
1705 * this almost certainly polls too fast.
1706 */
1707 limit = max (limit, periodic_bytes (dum, ep));
1708 /* FALLTHROUGH */
1709
1710 // case PIPE_BULK: case PIPE_CONTROL:
1711 default:
1712 treat_control_like_bulk:
1713 ep->last_io = jiffies;
Alan Stern4d2f1102007-08-24 15:40:10 -04001714 total = transfer(dum, urb, ep, limit, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 break;
1716 }
1717
1718 /* incomplete transfer? */
Alan Stern4d2f1102007-08-24 15:40:10 -04001719 if (status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 continue;
1721
1722return_urb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 list_del (&urbp->urbp_list);
1724 kfree (urbp);
1725 if (ep)
1726 ep->already_seen = ep->setup_stage = 0;
1727
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001728 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 spin_unlock (&dum->lock);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001730 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 spin_lock (&dum->lock);
1732
1733 goto restart;
1734 }
1735
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001736 if (list_empty(&dum_hcd->urbp_list)) {
1737 usb_put_dev(dum_hcd->udev);
1738 dum_hcd->udev = NULL;
1739 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern391eca92005-05-10 15:34:16 -04001740 /* want a 1 msec delay here */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001741 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
1743
1744 spin_unlock_irqrestore (&dum->lock, flags);
1745}
1746
1747/*-------------------------------------------------------------------------*/
1748
1749#define PORT_C_MASK \
Alan Sternc2db8b52005-04-29 16:30:48 -04001750 ((USB_PORT_STAT_C_CONNECTION \
1751 | USB_PORT_STAT_C_ENABLE \
1752 | USB_PORT_STAT_C_SUSPEND \
1753 | USB_PORT_STAT_C_OVERCURRENT \
1754 | USB_PORT_STAT_C_RESET) << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1757{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001758 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 unsigned long flags;
Alan Stern391eca92005-05-10 15:34:16 -04001760 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001762 dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001764 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001765 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001766 goto done;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001767
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001768 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1769 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1770 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1771 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001772 }
1773
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001774 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 *buf = (1 << 1);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001776 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1777 dum_hcd->port_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 retval = 1;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001779 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -04001780 usb_hcd_resume_root_hub (hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
Alan Stern391eca92005-05-10 15:34:16 -04001782done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001783 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 return retval;
1785}
1786
1787static inline void
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001788ss_hub_descriptor(struct usb_hub_descriptor *desc)
1789{
1790 memset(desc, 0, sizeof *desc);
1791 desc->bDescriptorType = 0x2a;
1792 desc->bDescLength = 12;
1793 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1794 desc->bNbrPorts = 1;
1795 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1796 desc->u.ss.DeviceRemovable = 0xffff;
1797}
1798
1799static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800hub_descriptor (struct usb_hub_descriptor *desc)
1801{
1802 memset (desc, 0, sizeof *desc);
1803 desc->bDescriptorType = 0x29;
1804 desc->bDescLength = 9;
Al Virofd05e722008-04-28 07:00:16 +01001805 desc->wHubCharacteristics = cpu_to_le16(0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 desc->bNbrPorts = 1;
John Youndbe79bb2001-09-17 00:00:00 -07001807 desc->u.hs.DeviceRemovable[0] = 0xff;
1808 desc->u.hs.DeviceRemovable[1] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
1811static int dummy_hub_control (
1812 struct usb_hcd *hcd,
1813 u16 typeReq,
1814 u16 wValue,
1815 u16 wIndex,
1816 char *buf,
1817 u16 wLength
1818) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001819 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 int retval = 0;
1821 unsigned long flags;
1822
Alan Stern541c7d42010-06-22 16:39:10 -04001823 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001824 return -ETIMEDOUT;
1825
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001826 dum_hcd = hcd_to_dummy_hcd(hcd);
1827
1828 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 switch (typeReq) {
1830 case ClearHubFeature:
1831 break;
1832 case ClearPortFeature:
1833 switch (wValue) {
1834 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001835 if (hcd->speed == HCD_USB3) {
1836 dev_dbg(dummy_dev(dum_hcd),
1837 "USB_PORT_FEAT_SUSPEND req not "
1838 "supported for USB 3.0 roothub\n");
1839 goto error;
1840 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001841 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 /* 20msec resume signaling */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001843 dum_hcd->resuming = 1;
1844 dum_hcd->re_timeout = jiffies +
Alan Sternf1c39fa2005-05-03 16:24:04 -04001845 msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 }
1847 break;
1848 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001849 if (hcd->speed == HCD_USB3) {
1850 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1851 dev_dbg(dummy_dev(dum_hcd),
1852 "power-off\n");
1853 } else
1854 if (dum_hcd->port_status &
1855 USB_SS_PORT_STAT_POWER)
1856 dev_dbg(dummy_dev(dum_hcd),
1857 "power-off\n");
Alan Sternf1c39fa2005-05-03 16:24:04 -04001858 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001860 dum_hcd->port_status &= ~(1 << wValue);
1861 set_link_state(dum_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
1863 break;
1864 case GetHubDescriptor:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001865 if (hcd->speed == HCD_USB3 &&
1866 (wLength < USB_DT_SS_HUB_SIZE ||
1867 wValue != (USB_DT_SS_HUB << 8))) {
1868 dev_dbg(dummy_dev(dum_hcd),
1869 "Wrong hub descriptor type for "
1870 "USB 3.0 roothub.\n");
1871 goto error;
1872 }
1873 if (hcd->speed == HCD_USB3)
1874 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
1875 else
1876 hub_descriptor((struct usb_hub_descriptor *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 break;
1878 case GetHubStatus:
Harvey Harrison551509d2009-02-11 14:11:36 -08001879 *(__le32 *) buf = cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 break;
1881 case GetPortStatus:
1882 if (wIndex != 1)
1883 retval = -EPIPE;
1884
1885 /* whoever resets or resumes must GetPortStatus to
1886 * complete it!!
1887 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001888 if (dum_hcd->resuming &&
1889 time_after_eq(jiffies, dum_hcd->re_timeout)) {
1890 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1891 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001893 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
1894 time_after_eq(jiffies, dum_hcd->re_timeout)) {
1895 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
1896 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
1897 if (dum_hcd->dum->pullup) {
1898 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001899
1900 if (hcd->speed < HCD_USB3) {
1901 switch (dum_hcd->dum->gadget.speed) {
1902 case USB_SPEED_HIGH:
1903 dum_hcd->port_status |=
1904 USB_PORT_STAT_HIGH_SPEED;
1905 break;
1906 case USB_SPEED_LOW:
1907 dum_hcd->dum->gadget.ep0->
1908 maxpacket = 8;
1909 dum_hcd->port_status |=
1910 USB_PORT_STAT_LOW_SPEED;
1911 break;
1912 default:
1913 dum_hcd->dum->gadget.speed =
1914 USB_SPEED_FULL;
1915 break;
1916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
1918 }
1919 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001920 set_link_state(dum_hcd);
1921 ((__le16 *) buf)[0] = cpu_to_le16 (dum_hcd->port_status);
1922 ((__le16 *) buf)[1] = cpu_to_le16 (dum_hcd->port_status >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 break;
1924 case SetHubFeature:
1925 retval = -EPIPE;
1926 break;
1927 case SetPortFeature:
1928 switch (wValue) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001929 case USB_PORT_FEAT_LINK_STATE:
1930 if (hcd->speed != HCD_USB3) {
1931 dev_dbg(dummy_dev(dum_hcd),
1932 "USB_PORT_FEAT_LINK_STATE req not "
1933 "supported for USB 2.0 roothub\n");
1934 goto error;
1935 }
1936 /*
1937 * Since this is dummy we don't have an actual link so
1938 * there is nothing to do for the SET_LINK_STATE cmd
1939 */
1940 break;
1941 case USB_PORT_FEAT_U1_TIMEOUT:
1942 case USB_PORT_FEAT_U2_TIMEOUT:
1943 /* TODO: add suspend/resume support! */
1944 if (hcd->speed != HCD_USB3) {
1945 dev_dbg(dummy_dev(dum_hcd),
1946 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
1947 "supported for USB 2.0 roothub\n");
1948 goto error;
1949 }
1950 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001952 /* Applicable only for USB2.0 hub */
1953 if (hcd->speed == HCD_USB3) {
1954 dev_dbg(dummy_dev(dum_hcd),
1955 "USB_PORT_FEAT_SUSPEND req not "
1956 "supported for USB 3.0 roothub\n");
1957 goto error;
1958 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001959 if (dum_hcd->active) {
1960 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001961
1962 /* HNP would happen here; for now we
1963 * assume b_bus_req is always true.
1964 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001965 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001966 if (((1 << USB_DEVICE_B_HNP_ENABLE)
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001967 & dum_hcd->dum->devstatus) != 0)
1968 dev_dbg(dummy_dev(dum_hcd),
Alan Stern5742b0c2005-05-02 11:25:17 -04001969 "no HNP yet!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
1971 break;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001972 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001973 if (hcd->speed == HCD_USB3)
1974 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
1975 else
1976 dum_hcd->port_status |= USB_PORT_STAT_POWER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001977 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001978 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001979 case USB_PORT_FEAT_BH_PORT_RESET:
1980 /* Applicable only for USB3.0 hub */
1981 if (hcd->speed != HCD_USB3) {
1982 dev_dbg(dummy_dev(dum_hcd),
1983 "USB_PORT_FEAT_BH_PORT_RESET req not "
1984 "supported for USB 2.0 roothub\n");
1985 goto error;
1986 }
1987 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 case USB_PORT_FEAT_RESET:
Alan Sternf1c39fa2005-05-03 16:24:04 -04001989 /* if it's already enabled, disable */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001990 if (hcd->speed == HCD_USB3) {
1991 dum_hcd->port_status = 0;
1992 dum_hcd->port_status =
1993 (USB_SS_PORT_STAT_POWER |
1994 USB_PORT_STAT_CONNECTION |
1995 USB_PORT_STAT_RESET);
1996 } else
1997 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
Alan Sternf1c39fa2005-05-03 16:24:04 -04001998 | USB_PORT_STAT_LOW_SPEED
1999 | USB_PORT_STAT_HIGH_SPEED);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002000 /*
2001 * We want to reset device status. All but the
2002 * Self powered feature
2003 */
2004 dum_hcd->dum->devstatus &=
2005 (1 << USB_DEVICE_SELF_POWERED);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002006 /*
2007 * FIXME USB3.0: what is the correct reset signaling
2008 * interval? Is it still 50msec as for HS?
2009 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002010 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002011 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 default:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002013 if (hcd->speed == HCD_USB3) {
2014 if ((dum_hcd->port_status &
2015 USB_SS_PORT_STAT_POWER) != 0) {
2016 dum_hcd->port_status |= (1 << wValue);
2017 set_link_state(dum_hcd);
2018 }
2019 } else
2020 if ((dum_hcd->port_status &
2021 USB_PORT_STAT_POWER) != 0) {
2022 dum_hcd->port_status |= (1 << wValue);
2023 set_link_state(dum_hcd);
2024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
2026 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002027 case GetPortErrorCount:
2028 if (hcd->speed != HCD_USB3) {
2029 dev_dbg(dummy_dev(dum_hcd),
2030 "GetPortErrorCount req not "
2031 "supported for USB 2.0 roothub\n");
2032 goto error;
2033 }
2034 /* We'll always return 0 since this is a dummy hub */
2035 *(__le32 *) buf = cpu_to_le32(0);
2036 break;
2037 case SetHubDepth:
2038 if (hcd->speed != HCD_USB3) {
2039 dev_dbg(dummy_dev(dum_hcd),
2040 "SetHubDepth req not supported for "
2041 "USB 2.0 roothub\n");
2042 goto error;
2043 }
2044 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002046 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 "hub control req%04x v%04x i%04x l%d\n",
2048 typeReq, wValue, wIndex, wLength);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002049error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 /* "protocol stall" on error */
2051 retval = -EPIPE;
2052 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002053 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Stern685eb932005-05-03 16:27:26 -04002054
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002055 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
Alan Stern685eb932005-05-03 16:27:26 -04002056 usb_hcd_poll_rh_status (hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return retval;
2058}
2059
Alan Stern0c0382e2005-10-13 17:08:02 -04002060static int dummy_bus_suspend (struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002061{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002062 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002063
Harvey Harrison441b62c2008-03-03 16:08:34 -08002064 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern3cf0a222005-11-29 12:08:15 -05002065
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002066 spin_lock_irq(&dum_hcd->dum->lock);
2067 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2068 set_link_state(dum_hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002069 hcd->state = HC_STATE_SUSPENDED;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002070 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern391eca92005-05-10 15:34:16 -04002071 return 0;
2072}
2073
Alan Stern0c0382e2005-10-13 17:08:02 -04002074static int dummy_bus_resume (struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002075{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002076 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002077 int rc = 0;
2078
Harvey Harrison441b62c2008-03-03 16:08:34 -08002079 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002080
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002081 spin_lock_irq(&dum_hcd->dum->lock);
Alan Stern541c7d42010-06-22 16:39:10 -04002082 if (!HCD_HW_ACCESSIBLE(hcd)) {
Alan Sterncfa59da2007-06-21 16:25:35 -04002083 rc = -ESHUTDOWN;
Alan Stern3cf0a222005-11-29 12:08:15 -05002084 } else {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002085 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2086 set_link_state(dum_hcd);
2087 if (!list_empty(&dum_hcd->urbp_list))
2088 mod_timer(&dum_hcd->timer, jiffies);
Alan Stern3cf0a222005-11-29 12:08:15 -05002089 hcd->state = HC_STATE_RUNNING;
2090 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002091 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern3cf0a222005-11-29 12:08:15 -05002092 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002093}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095/*-------------------------------------------------------------------------*/
2096
2097static inline ssize_t
2098show_urb (char *buf, size_t size, struct urb *urb)
2099{
2100 int ep = usb_pipeendpoint (urb->pipe);
2101
2102 return snprintf (buf, size,
2103 "urb/%p %s ep%d%s%s len %d/%d\n",
2104 urb,
2105 ({ char *s;
2106 switch (urb->dev->speed) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002107 case USB_SPEED_LOW:
2108 s = "ls";
2109 break;
2110 case USB_SPEED_FULL:
2111 s = "fs";
2112 break;
2113 case USB_SPEED_HIGH:
2114 s = "hs";
2115 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002116 case USB_SPEED_SUPER:
2117 s = "ss";
2118 break;
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002119 default:
2120 s = "?";
2121 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 }; s; }),
2123 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
2124 ({ char *s; \
2125 switch (usb_pipetype (urb->pipe)) { \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002126 case PIPE_CONTROL: \
2127 s = ""; \
2128 break; \
2129 case PIPE_BULK: \
2130 s = "-bulk"; \
2131 break; \
2132 case PIPE_INTERRUPT: \
2133 s = "-int"; \
2134 break; \
2135 default: \
2136 s = "-iso"; \
2137 break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }; s;}),
2139 urb->actual_length, urb->transfer_buffer_length);
2140}
2141
2142static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -04002143show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
2145 struct usb_hcd *hcd = dev_get_drvdata (dev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002146 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 struct urbp *urbp;
2148 size_t size = 0;
2149 unsigned long flags;
2150
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002151 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2152 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 size_t temp;
2154
2155 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
2156 buf += temp;
2157 size += temp;
2158 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002159 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 return size;
2162}
2163static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
2164
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002165static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2166{
2167 init_timer(&dum_hcd->timer);
2168 dum_hcd->timer.function = dummy_timer;
2169 dum_hcd->timer.data = (unsigned long)dum_hcd;
2170 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2171 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2172 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2173 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2174 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2175#ifdef CONFIG_USB_OTG
2176 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2177#endif
2178 return 0;
2179
2180 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2181 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2182}
2183
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002184static int dummy_start(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002186 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 /*
2189 * MASTER side init ... we emulate a root hub that'll only ever
2190 * talk to one device (the slave side). Also appears in sysfs,
2191 * just like more familiar pci-based HCDs.
2192 */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002193 if (!usb_hcd_is_primary_hcd(hcd))
2194 return dummy_start_ss(dum_hcd);
2195
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002196 spin_lock_init(&dum_hcd->dum->lock);
2197 init_timer(&dum_hcd->timer);
2198 dum_hcd->timer.function = dummy_timer;
2199 dum_hcd->timer.data = (unsigned long)dum_hcd;
2200 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002202 INIT_LIST_HEAD(&dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Alan Sterncaf29f62007-12-06 11:10:39 -05002204 hcd->power_budget = POWER_BUDGET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 hcd->state = HC_STATE_RUNNING;
Alan Stern685eb932005-05-03 16:27:26 -04002206 hcd->uses_new_polling = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Alan Stern5742b0c2005-05-02 11:25:17 -04002208#ifdef CONFIG_USB_OTG
2209 hcd->self.otg_port = 1;
2210#endif
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002213 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
2216static void dummy_stop (struct usb_hcd *hcd)
2217{
2218 struct dummy *dum;
2219
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002220 dum = (hcd_to_dummy_hcd(hcd))->dum;
2221 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2222 usb_gadget_unregister_driver(dum->driver);
2223 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}
2225
2226/*-------------------------------------------------------------------------*/
2227
2228static int dummy_h_get_frame (struct usb_hcd *hcd)
2229{
2230 return dummy_g_get_frame (NULL);
2231}
2232
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002233static int dummy_setup(struct usb_hcd *hcd)
2234{
2235 if (usb_hcd_is_primary_hcd(hcd)) {
2236 the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
2237 the_controller.hs_hcd->dum = &the_controller;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002238 /*
2239 * Mark the first roothub as being USB 2.0.
2240 * The USB 3.0 roothub will be registered later by
2241 * dummy_hcd_probe()
2242 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002243 hcd->speed = HCD_USB2;
2244 hcd->self.root_hub->speed = USB_SPEED_HIGH;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002245 } else {
2246 the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
2247 the_controller.ss_hcd->dum = &the_controller;
2248 hcd->speed = HCD_USB3;
2249 hcd->self.root_hub->speed = USB_SPEED_SUPER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002250 }
2251 return 0;
2252}
2253
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002254/* Change a group of bulk endpoints to support multiple stream IDs */
2255int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
2256 struct usb_host_endpoint **eps, unsigned int num_eps,
2257 unsigned int num_streams, gfp_t mem_flags)
2258{
2259 if (hcd->speed != HCD_USB3)
2260 dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
2261 "%s() - ERROR! Not supported for USB2.0 roothub\n",
2262 __func__);
2263 return 0;
2264}
2265
2266/* Reverts a group of bulk endpoints back to not using stream IDs. */
2267int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
2268 struct usb_host_endpoint **eps, unsigned int num_eps,
2269 gfp_t mem_flags)
2270{
2271 if (hcd->speed != HCD_USB3)
2272 dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
2273 "%s() - ERROR! Not supported for USB2.0 roothub\n",
2274 __func__);
2275 return 0;
2276}
2277
2278static struct hc_driver dummy_hcd = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 .description = (char *) driver_name,
2280 .product_desc = "Dummy host controller",
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002281 .hcd_priv_size = sizeof(struct dummy_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002283 .flags = HCD_USB3 | HCD_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002285 .reset = dummy_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 .start = dummy_start,
2287 .stop = dummy_stop,
2288
2289 .urb_enqueue = dummy_urb_enqueue,
2290 .urb_dequeue = dummy_urb_dequeue,
2291
2292 .get_frame_number = dummy_h_get_frame,
2293
2294 .hub_status_data = dummy_hub_status,
2295 .hub_control = dummy_hub_control,
Alan Stern0c0382e2005-10-13 17:08:02 -04002296 .bus_suspend = dummy_bus_suspend,
2297 .bus_resume = dummy_bus_resume,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002298
2299 .alloc_streams = dummy_alloc_streams,
2300 .free_streams = dummy_free_streams,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301};
2302
Alan Stern8364d6b2005-11-14 12:16:30 -05002303static int dummy_hcd_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002305 struct usb_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002306 struct usb_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 int retval;
2308
Alan Stern8364d6b2005-11-14 12:16:30 -05002309 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002311 if (!mod_data.is_super_speed)
2312 dummy_hcd.flags = HCD_USB2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002313 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2314 if (!hs_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return -ENOMEM;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002316 hs_hcd->has_tt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002318 retval = usb_add_hcd(hs_hcd, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (retval != 0) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002320 usb_put_hcd(hs_hcd);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002321 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002323
2324 if (mod_data.is_super_speed) {
2325 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2326 dev_name(&pdev->dev), hs_hcd);
2327 if (!ss_hcd) {
2328 retval = -ENOMEM;
2329 goto dealloc_usb2_hcd;
2330 }
2331
2332 retval = usb_add_hcd(ss_hcd, 0, 0);
2333 if (retval)
2334 goto put_usb3_hcd;
2335 }
2336 return 0;
2337
2338put_usb3_hcd:
2339 usb_put_hcd(ss_hcd);
2340dealloc_usb2_hcd:
2341 usb_put_hcd(hs_hcd);
2342 the_controller.hs_hcd = the_controller.ss_hcd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return retval;
2344}
2345
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002346static int dummy_hcd_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002348 struct dummy *dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002350 dum = (hcd_to_dummy_hcd(platform_get_drvdata(pdev)))->dum;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002351
2352 if (dum->ss_hcd) {
2353 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2354 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2355 }
2356
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002357 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2358 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002359
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002360 the_controller.hs_hcd = NULL;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002361 the_controller.ss_hcd = NULL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002362
Alan Sternd9b76252005-05-03 16:15:43 -04002363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
Alan Stern8364d6b2005-11-14 12:16:30 -05002366static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
Alan Stern391eca92005-05-10 15:34:16 -04002367{
2368 struct usb_hcd *hcd;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002369 struct dummy_hcd *dum_hcd;
Alan Stern3cf0a222005-11-29 12:08:15 -05002370 int rc = 0;
Alan Stern391eca92005-05-10 15:34:16 -04002371
Harvey Harrison441b62c2008-03-03 16:08:34 -08002372 dev_dbg (&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002373
Alan Stern3cf0a222005-11-29 12:08:15 -05002374 hcd = platform_get_drvdata (pdev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002375 dum_hcd = hcd_to_dummy_hcd(hcd);
2376 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern3cf0a222005-11-29 12:08:15 -05002377 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2378 rc = -EBUSY;
2379 } else
2380 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2381 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002382}
2383
Alan Stern8364d6b2005-11-14 12:16:30 -05002384static int dummy_hcd_resume (struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04002385{
2386 struct usb_hcd *hcd;
2387
Harvey Harrison441b62c2008-03-03 16:08:34 -08002388 dev_dbg (&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002389
Alan Stern3cf0a222005-11-29 12:08:15 -05002390 hcd = platform_get_drvdata (pdev);
2391 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Alan Stern391eca92005-05-10 15:34:16 -04002392 usb_hcd_poll_rh_status (hcd);
2393 return 0;
2394}
2395
Russell King3ae5eae2005-11-09 22:32:44 +00002396static struct platform_driver dummy_hcd_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04002397 .probe = dummy_hcd_probe,
2398 .remove = dummy_hcd_remove,
Alan Stern391eca92005-05-10 15:34:16 -04002399 .suspend = dummy_hcd_suspend,
2400 .resume = dummy_hcd_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002401 .driver = {
2402 .name = (char *) driver_name,
2403 .owner = THIS_MODULE,
2404 },
Alan Sternd9b76252005-05-03 16:15:43 -04002405};
2406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407/*-------------------------------------------------------------------------*/
2408
Alan Sterna89a2cd2008-04-07 15:03:25 -04002409static struct platform_device *the_udc_pdev;
2410static struct platform_device *the_hcd_pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
2412static int __init init (void)
2413{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002414 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416 if (usb_disabled ())
2417 return -ENODEV;
Alan Sternd9b76252005-05-03 16:15:43 -04002418
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +03002419 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2420 return -EINVAL;
2421
Alan Sterna89a2cd2008-04-07 15:03:25 -04002422 the_hcd_pdev = platform_device_alloc(driver_name, -1);
2423 if (!the_hcd_pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 return retval;
Alan Sterna89a2cd2008-04-07 15:03:25 -04002425 the_udc_pdev = platform_device_alloc(gadget_name, -1);
2426 if (!the_udc_pdev)
2427 goto err_alloc_udc;
Alan Sternd9b76252005-05-03 16:15:43 -04002428
Alan Sterna89a2cd2008-04-07 15:03:25 -04002429 retval = platform_driver_register(&dummy_hcd_driver);
2430 if (retval < 0)
2431 goto err_register_hcd_driver;
2432 retval = platform_driver_register(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002433 if (retval < 0)
2434 goto err_register_udc_driver;
2435
Alan Sterna89a2cd2008-04-07 15:03:25 -04002436 retval = platform_device_add(the_hcd_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002437 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002438 goto err_add_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002439 if (!the_controller.hs_hcd ||
2440 (!the_controller.ss_hcd && mod_data.is_super_speed)) {
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002441 /*
2442 * The hcd was added successfully but its probe function failed
2443 * for some reason.
2444 */
2445 retval = -EINVAL;
2446 goto err_add_udc;
2447 }
Alan Sterna89a2cd2008-04-07 15:03:25 -04002448 retval = platform_device_add(the_udc_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002449 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002450 goto err_add_udc;
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002451 if (!platform_get_drvdata(the_udc_pdev)) {
2452 /*
2453 * The udc was added successfully but its probe function failed
2454 * for some reason.
2455 */
2456 retval = -EINVAL;
2457 goto err_probe_udc;
2458 }
Alan Sternd9b76252005-05-03 16:15:43 -04002459 return retval;
2460
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002461err_probe_udc:
2462 platform_device_del(the_udc_pdev);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002463err_add_udc:
2464 platform_device_del(the_hcd_pdev);
2465err_add_hcd:
2466 platform_driver_unregister(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002467err_register_udc_driver:
Alan Sterna89a2cd2008-04-07 15:03:25 -04002468 platform_driver_unregister(&dummy_hcd_driver);
2469err_register_hcd_driver:
2470 platform_device_put(the_udc_pdev);
2471err_alloc_udc:
2472 platform_device_put(the_hcd_pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 return retval;
2474}
2475module_init (init);
2476
2477static void __exit cleanup (void)
2478{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002479 platform_device_unregister(the_udc_pdev);
2480 platform_device_unregister(the_hcd_pdev);
2481 platform_driver_unregister(&dummy_udc_driver);
2482 platform_driver_unregister(&dummy_hcd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
2484module_exit (cleanup);