blob: db815c2da7ed42743a907b793ed8556d1864d728 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * dummy_hcd.c -- Dummy/Loopback USB host and device emulator driver.
3 *
4 * Maintainer: Alan Stern <stern@rowland.harvard.edu>
5 *
6 * Copyright (C) 2003 David Brownell
7 * Copyright (C) 2003-2005 Alan Stern
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
15
16/*
17 * This exposes a device side "USB gadget" API, driven by requests to a
18 * Linux-USB host controller driver. USB traffic is simulated; there's
19 * no need for USB hardware. Use this with two other drivers:
20 *
21 * - Gadget driver, responding to requests (slave);
22 * - Host-side device driver, as already familiar in Linux.
23 *
24 * Having this all in one kernel can help some stages of development,
25 * bypassing some hardware (and driver) issues. UML could help too.
26 */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/kernel.h>
30#include <linux/delay.h>
31#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/errno.h>
34#include <linux/init.h>
35#include <linux/timer.h>
36#include <linux/list.h>
37#include <linux/interrupt.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010038#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/usb.h>
David Brownell9454a572007-10-04 18:05:17 -070040#include <linux/usb/gadget.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020041#include <linux/usb/hcd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/byteorder.h>
44#include <asm/io.h>
45#include <asm/irq.h>
46#include <asm/system.h>
47#include <asm/unaligned.h>
48
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define DRIVER_DESC "USB Host+Gadget Emulator"
Alan Stern391eca92005-05-10 15:34:16 -040051#define DRIVER_VERSION "02 May 2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Alan Sterncaf29f62007-12-06 11:10:39 -050053#define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static const char driver_name [] = "dummy_hcd";
56static const char driver_desc [] = "USB Host+Gadget Emulator";
57
58static const char gadget_name [] = "dummy_udc";
59
60MODULE_DESCRIPTION (DRIVER_DESC);
61MODULE_AUTHOR ("David Brownell");
62MODULE_LICENSE ("GPL");
63
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +030064struct dummy_hcd_module_parameters {
65 bool is_super_speed;
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +030066 bool is_high_speed;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +030067};
68
69static struct dummy_hcd_module_parameters mod_data = {
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +030070 .is_super_speed = false,
71 .is_high_speed = true,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +030072};
73module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
74MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +030075module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
76MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*-------------------------------------------------------------------------*/
78
79/* gadget side driver data structres */
80struct dummy_ep {
81 struct list_head queue;
82 unsigned long last_io; /* jiffies timestamp */
83 struct usb_gadget *gadget;
84 const struct usb_endpoint_descriptor *desc;
85 struct usb_ep ep;
86 unsigned halted : 1;
Alan Stern851a5262008-08-14 15:48:30 -040087 unsigned wedged : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 unsigned already_seen : 1;
89 unsigned setup_stage : 1;
90};
91
92struct dummy_request {
93 struct list_head queue; /* ep's requests */
94 struct usb_request req;
95};
96
97static inline struct dummy_ep *usb_ep_to_dummy_ep (struct usb_ep *_ep)
98{
99 return container_of (_ep, struct dummy_ep, ep);
100}
101
102static inline struct dummy_request *usb_request_to_dummy_request
103 (struct usb_request *_req)
104{
105 return container_of (_req, struct dummy_request, req);
106}
107
108/*-------------------------------------------------------------------------*/
109
110/*
111 * Every device has ep0 for control requests, plus up to 30 more endpoints,
112 * in one of two types:
113 *
114 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
115 * number can be changed. Names like "ep-a" are used for this type.
116 *
117 * - Fixed Function: in other cases. some characteristics may be mutable;
118 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
119 *
120 * Gadget drivers are responsible for not setting up conflicting endpoint
121 * configurations, illegal or unsupported packet lengths, and so on.
122 */
123
124static const char ep0name [] = "ep0";
125
126static const char *const ep_name [] = {
127 ep0name, /* everyone has ep0 */
128
129 /* act like a net2280: high speed, six configurable endpoints */
130 "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
131
132 /* or like pxa250: fifteen fixed function endpoints */
133 "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
134 "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
135 "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
136 "ep15in-int",
137
138 /* or like sa1100: two fixed function endpoints */
139 "ep1out-bulk", "ep2in-bulk",
140};
Tobias Klauser52950ed2005-12-11 16:20:08 +0100141#define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Alan Sternd9b76252005-05-03 16:15:43 -0400143/*-------------------------------------------------------------------------*/
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define FIFO_SIZE 64
146
147struct urbp {
148 struct urb *urb;
149 struct list_head urbp_list;
150};
151
Alan Stern391eca92005-05-10 15:34:16 -0400152
153enum dummy_rh_state {
154 DUMMY_RH_RESET,
155 DUMMY_RH_SUSPENDED,
156 DUMMY_RH_RUNNING
157};
158
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300159struct dummy_hcd {
160 struct dummy *dum;
161 enum dummy_rh_state rh_state;
162 struct timer_list timer;
163 u32 port_status;
164 u32 old_status;
165 unsigned long re_timeout;
166
167 struct usb_device *udev;
168 struct list_head urbp_list;
169
170 unsigned active:1;
171 unsigned old_active:1;
172 unsigned resuming:1;
173};
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175struct dummy {
176 spinlock_t lock;
177
178 /*
179 * SLAVE/GADGET side support
180 */
181 struct dummy_ep ep [DUMMY_ENDPOINTS];
182 int address;
183 struct usb_gadget gadget;
184 struct usb_gadget_driver *driver;
185 struct dummy_request fifo_req;
186 u8 fifo_buf [FIFO_SIZE];
187 u16 devstatus;
Alan Stern391eca92005-05-10 15:34:16 -0400188 unsigned udc_suspended:1;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400189 unsigned pullup:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 /*
192 * MASTER/HOST side support
193 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300194 struct dummy_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300195 struct dummy_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300198static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300200 return (struct dummy_hcd *) (hcd->hcd_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300203static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 return container_of((void *) dum, struct usb_hcd, hcd_priv);
206}
207
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300208static inline struct device *dummy_dev(struct dummy_hcd *dum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300210 return dummy_hcd_to_hcd(dum)->self.controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Alan Sternd9b76252005-05-03 16:15:43 -0400213static inline struct device *udc_dev (struct dummy *dum)
214{
215 return dum->gadget.dev.parent;
216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
219{
220 return container_of (ep->gadget, struct dummy, gadget);
221}
222
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300223static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300225 struct dummy *dum = container_of(gadget, struct dummy, gadget);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300226 if (dum->gadget.speed == USB_SPEED_SUPER)
227 return dum->ss_hcd;
228 else
229 return dum->hs_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
233{
234 return container_of (dev, struct dummy, gadget.dev);
235}
236
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300237static struct dummy the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239/*-------------------------------------------------------------------------*/
240
Alan Sternf1c39fa2005-05-03 16:24:04 -0400241/* SLAVE/GADGET SIDE UTILITY ROUTINES */
242
243/* called with spinlock held */
244static void nuke (struct dummy *dum, struct dummy_ep *ep)
245{
246 while (!list_empty (&ep->queue)) {
247 struct dummy_request *req;
248
249 req = list_entry (ep->queue.next, struct dummy_request, queue);
250 list_del_init (&req->queue);
251 req->req.status = -ESHUTDOWN;
252
253 spin_unlock (&dum->lock);
254 req->req.complete (&ep->ep, &req->req);
255 spin_lock (&dum->lock);
256 }
257}
258
259/* caller must hold lock */
260static void
261stop_activity (struct dummy *dum)
262{
263 struct dummy_ep *ep;
264
265 /* prevent any more requests */
266 dum->address = 0;
267
268 /* The timer is left running so that outstanding URBs can fail */
269
270 /* nuke any pending requests first, so driver i/o is quiesced */
271 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
272 nuke (dum, ep);
273
274 /* driver now does any non-usb quiescing necessary */
275}
276
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300277/**
278 * set_link_state_by_speed() - Sets the current state of the link according to
279 * the hcd speed
280 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
281 *
282 * This function updates the port_status according to the link state and the
283 * speed of the hcd.
284 */
285static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
286{
287 struct dummy *dum = dum_hcd->dum;
288
289 if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
290 if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
291 dum_hcd->port_status = 0;
292 } else if (!dum->pullup || dum->udc_suspended) {
293 /* UDC suspend must cause a disconnect */
294 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
295 USB_PORT_STAT_ENABLE);
296 if ((dum_hcd->old_status &
297 USB_PORT_STAT_CONNECTION) != 0)
298 dum_hcd->port_status |=
299 (USB_PORT_STAT_C_CONNECTION << 16);
300 } else {
301 /* device is connected and not suspended */
302 dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
303 USB_PORT_STAT_SPEED_5GBPS) ;
304 if ((dum_hcd->old_status &
305 USB_PORT_STAT_CONNECTION) == 0)
306 dum_hcd->port_status |=
307 (USB_PORT_STAT_C_CONNECTION << 16);
308 if ((dum_hcd->port_status &
309 USB_PORT_STAT_ENABLE) == 1 &&
310 (dum_hcd->port_status &
311 USB_SS_PORT_LS_U0) == 1 &&
312 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
313 dum_hcd->active = 1;
314 }
315 } else {
316 if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
317 dum_hcd->port_status = 0;
318 } else if (!dum->pullup || dum->udc_suspended) {
319 /* UDC suspend must cause a disconnect */
320 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
321 USB_PORT_STAT_ENABLE |
322 USB_PORT_STAT_LOW_SPEED |
323 USB_PORT_STAT_HIGH_SPEED |
324 USB_PORT_STAT_SUSPEND);
325 if ((dum_hcd->old_status &
326 USB_PORT_STAT_CONNECTION) != 0)
327 dum_hcd->port_status |=
328 (USB_PORT_STAT_C_CONNECTION << 16);
329 } else {
330 dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
331 if ((dum_hcd->old_status &
332 USB_PORT_STAT_CONNECTION) == 0)
333 dum_hcd->port_status |=
334 (USB_PORT_STAT_C_CONNECTION << 16);
335 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
336 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
337 else if ((dum_hcd->port_status &
338 USB_PORT_STAT_SUSPEND) == 0 &&
339 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
340 dum_hcd->active = 1;
341 }
342 }
343}
344
Alan Sternf1c39fa2005-05-03 16:24:04 -0400345/* caller must hold lock */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300346static void set_link_state(struct dummy_hcd *dum_hcd)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400347{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300348 struct dummy *dum = dum_hcd->dum;
349
350 dum_hcd->active = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300351 if (dum->pullup)
352 if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
353 dum->gadget.speed != USB_SPEED_SUPER) ||
354 (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
355 dum->gadget.speed == USB_SPEED_SUPER))
356 return;
Alan Stern391eca92005-05-10 15:34:16 -0400357
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300358 set_link_state_by_speed(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400359
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300360 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
361 dum_hcd->active)
362 dum_hcd->resuming = 0;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400363
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300364 /* if !connected or reset */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300365 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
366 (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300367 /*
368 * We're connected and not reset (reset occurred now),
369 * and driver attached - disconnect!
370 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300371 if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300372 (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
373 dum->driver) {
374 stop_activity(dum);
375 spin_unlock(&dum->lock);
376 dum->driver->disconnect(&dum->gadget);
377 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400378 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300379 } else if (dum_hcd->active != dum_hcd->old_active) {
380 if (dum_hcd->old_active && dum->driver->suspend) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300381 spin_unlock(&dum->lock);
382 dum->driver->suspend(&dum->gadget);
383 spin_lock(&dum->lock);
384 } else if (!dum_hcd->old_active && dum->driver->resume) {
385 spin_unlock(&dum->lock);
386 dum->driver->resume(&dum->gadget);
387 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400388 }
389 }
390
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300391 dum_hcd->old_status = dum_hcd->port_status;
392 dum_hcd->old_active = dum_hcd->active;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400393}
394
395/*-------------------------------------------------------------------------*/
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397/* SLAVE/GADGET SIDE DRIVER
398 *
399 * This only tracks gadget state. All the work is done when the host
400 * side tries some (emulated) i/o operation. Real device controller
401 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
402 */
403
404#define is_enabled(dum) \
405 (dum->port_status & USB_PORT_STAT_ENABLE)
406
407static int
408dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
409{
410 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300411 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 struct dummy_ep *ep;
413 unsigned max;
414 int retval;
415
416 ep = usb_ep_to_dummy_ep (_ep);
417 if (!_ep || !desc || ep->desc || _ep->name == ep0name
418 || desc->bDescriptorType != USB_DT_ENDPOINT)
419 return -EINVAL;
420 dum = ep_to_dummy (ep);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300421 if (!dum->driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return -ESHUTDOWN;
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200423
424 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300425 if (!is_enabled(dum_hcd))
426 return -ESHUTDOWN;
427
428 /*
429 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
430 * maximum packet size.
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300431 * For SS devices the wMaxPacketSize is limited by 1024.
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300432 */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700433 max = usb_endpoint_maxp(desc) & 0x7ff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /* drivers must not request bad settings, since lower levels
436 * (hardware or its drivers) may not check. some endpoints
437 * can't do iso, many have maxpacket limitations, etc.
438 *
439 * since this "hardware" driver is here to help debugging, we
440 * have some extra sanity checks. (there could be more though,
441 * especially for "ep9out" style fixed function ones.)
442 */
443 retval = -EINVAL;
444 switch (desc->bmAttributes & 0x03) {
445 case USB_ENDPOINT_XFER_BULK:
446 if (strstr (ep->ep.name, "-iso")
447 || strstr (ep->ep.name, "-int")) {
448 goto done;
449 }
450 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300451 case USB_SPEED_SUPER:
452 if (max == 1024)
453 break;
454 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 case USB_SPEED_HIGH:
456 if (max == 512)
457 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700458 goto done;
459 case USB_SPEED_FULL:
460 if (max == 8 || max == 16 || max == 32 || max == 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /* we'll fake any legal size */
462 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700463 /* save a return statement */
464 default:
465 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467 break;
468 case USB_ENDPOINT_XFER_INT:
469 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
470 goto done;
471 /* real hardware might not handle all packet sizes */
472 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300473 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 case USB_SPEED_HIGH:
475 if (max <= 1024)
476 break;
477 /* save a return statement */
478 case USB_SPEED_FULL:
479 if (max <= 64)
480 break;
481 /* save a return statement */
482 default:
483 if (max <= 8)
484 break;
485 goto done;
486 }
487 break;
488 case USB_ENDPOINT_XFER_ISOC:
489 if (strstr (ep->ep.name, "-bulk")
490 || strstr (ep->ep.name, "-int"))
491 goto done;
492 /* real hardware might not handle all packet sizes */
493 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300494 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 case USB_SPEED_HIGH:
496 if (max <= 1024)
497 break;
498 /* save a return statement */
499 case USB_SPEED_FULL:
500 if (max <= 1023)
501 break;
502 /* save a return statement */
503 default:
504 goto done;
505 }
506 break;
507 default:
508 /* few chips support control except on ep0 */
509 goto done;
510 }
511
512 _ep->maxpacket = max;
513 ep->desc = desc;
514
Alan Sternd9b76252005-05-03 16:15:43 -0400515 dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 _ep->name,
517 desc->bEndpointAddress & 0x0f,
518 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
519 ({ char *val;
520 switch (desc->bmAttributes & 0x03) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +0300521 case USB_ENDPOINT_XFER_BULK:
522 val = "bulk";
523 break;
524 case USB_ENDPOINT_XFER_ISOC:
525 val = "iso";
526 break;
527 case USB_ENDPOINT_XFER_INT:
528 val = "intr";
529 break;
530 default:
531 val = "ctrl";
532 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }; val; }),
534 max);
535
536 /* at this point real hardware should be NAKing transfers
537 * to that endpoint, until a buffer is queued to it.
538 */
Alan Stern851a5262008-08-14 15:48:30 -0400539 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 retval = 0;
541done:
542 return retval;
543}
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545static int dummy_disable (struct usb_ep *_ep)
546{
547 struct dummy_ep *ep;
548 struct dummy *dum;
549 unsigned long flags;
550 int retval;
551
552 ep = usb_ep_to_dummy_ep (_ep);
553 if (!_ep || !ep->desc || _ep->name == ep0name)
554 return -EINVAL;
555 dum = ep_to_dummy (ep);
556
557 spin_lock_irqsave (&dum->lock, flags);
558 ep->desc = NULL;
559 retval = 0;
560 nuke (dum, ep);
561 spin_unlock_irqrestore (&dum->lock, flags);
562
Alan Sternd9b76252005-05-03 16:15:43 -0400563 dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return retval;
565}
566
567static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -0400568dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 struct dummy_ep *ep;
571 struct dummy_request *req;
572
573 if (!_ep)
574 return NULL;
575 ep = usb_ep_to_dummy_ep (_ep);
576
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800577 req = kzalloc(sizeof(*req), mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (!req)
579 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 INIT_LIST_HEAD (&req->queue);
581 return &req->req;
582}
583
584static void
585dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
586{
587 struct dummy_ep *ep;
588 struct dummy_request *req;
589
590 ep = usb_ep_to_dummy_ep (_ep);
591 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
592 return;
593
594 req = usb_request_to_dummy_request (_req);
595 WARN_ON (!list_empty (&req->queue));
596 kfree (req);
597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599static void
600fifo_complete (struct usb_ep *ep, struct usb_request *req)
601{
602}
603
604static int
Olav Kongas5db539e2005-06-23 20:25:36 +0300605dummy_queue (struct usb_ep *_ep, struct usb_request *_req,
Al Viro55016f12005-10-21 03:21:58 -0400606 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 struct dummy_ep *ep;
609 struct dummy_request *req;
610 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300611 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 unsigned long flags;
613
614 req = usb_request_to_dummy_request (_req);
615 if (!_req || !list_empty (&req->queue) || !_req->complete)
616 return -EINVAL;
617
618 ep = usb_ep_to_dummy_ep (_ep);
619 if (!_ep || (!ep->desc && _ep->name != ep0name))
620 return -EINVAL;
621
622 dum = ep_to_dummy (ep);
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200623 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300624 if (!dum->driver || !is_enabled(dum_hcd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return -ESHUTDOWN;
626
627#if 0
Alan Sternd9b76252005-05-03 16:15:43 -0400628 dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 ep, _req, _ep->name, _req->length, _req->buf);
630#endif
631
632 _req->status = -EINPROGRESS;
633 _req->actual = 0;
634 spin_lock_irqsave (&dum->lock, flags);
635
636 /* implement an emulated single-request FIFO */
637 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
638 list_empty (&dum->fifo_req.queue) &&
639 list_empty (&ep->queue) &&
640 _req->length <= FIFO_SIZE) {
641 req = &dum->fifo_req;
642 req->req = *_req;
643 req->req.buf = dum->fifo_buf;
644 memcpy (dum->fifo_buf, _req->buf, _req->length);
645 req->req.context = dum;
646 req->req.complete = fifo_complete;
647
David Brownellc728df72008-07-26 08:06:24 -0700648 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 spin_unlock (&dum->lock);
650 _req->actual = _req->length;
651 _req->status = 0;
652 _req->complete (_ep, _req);
653 spin_lock (&dum->lock);
David Brownellc728df72008-07-26 08:06:24 -0700654 } else
655 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 spin_unlock_irqrestore (&dum->lock, flags);
657
658 /* real hardware would likely enable transfers here, in case
659 * it'd been left NAKing.
660 */
661 return 0;
662}
663
664static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
665{
666 struct dummy_ep *ep;
667 struct dummy *dum;
668 int retval = -EINVAL;
669 unsigned long flags;
670 struct dummy_request *req = NULL;
671
672 if (!_ep || !_req)
673 return retval;
674 ep = usb_ep_to_dummy_ep (_ep);
675 dum = ep_to_dummy (ep);
676
677 if (!dum->driver)
678 return -ESHUTDOWN;
679
Alan Sternb4dbda12006-07-28 17:07:34 -0400680 local_irq_save (flags);
681 spin_lock (&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 list_for_each_entry (req, &ep->queue, queue) {
683 if (&req->req == _req) {
684 list_del_init (&req->queue);
685 _req->status = -ECONNRESET;
686 retval = 0;
687 break;
688 }
689 }
Alan Sternb4dbda12006-07-28 17:07:34 -0400690 spin_unlock (&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 if (retval == 0) {
Alan Sternd9b76252005-05-03 16:15:43 -0400693 dev_dbg (udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 "dequeued req %p from %s, len %d buf %p\n",
695 req, _ep->name, _req->length, _req->buf);
696 _req->complete (_ep, _req);
697 }
Alan Sternb4dbda12006-07-28 17:07:34 -0400698 local_irq_restore (flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return retval;
700}
701
702static int
Alan Stern851a5262008-08-14 15:48:30 -0400703dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 struct dummy_ep *ep;
706 struct dummy *dum;
707
708 if (!_ep)
709 return -EINVAL;
710 ep = usb_ep_to_dummy_ep (_ep);
711 dum = ep_to_dummy (ep);
712 if (!dum->driver)
713 return -ESHUTDOWN;
714 if (!value)
Alan Stern851a5262008-08-14 15:48:30 -0400715 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
717 !list_empty (&ep->queue))
718 return -EAGAIN;
Alan Stern851a5262008-08-14 15:48:30 -0400719 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 ep->halted = 1;
Alan Stern851a5262008-08-14 15:48:30 -0400721 if (wedged)
722 ep->wedged = 1;
723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 /* FIXME clear emulated data toggle too */
725 return 0;
726}
727
Alan Stern851a5262008-08-14 15:48:30 -0400728static int
729dummy_set_halt(struct usb_ep *_ep, int value)
730{
731 return dummy_set_halt_and_wedge(_ep, value, 0);
732}
733
734static int dummy_set_wedge(struct usb_ep *_ep)
735{
736 if (!_ep || _ep->name == ep0name)
737 return -EINVAL;
738 return dummy_set_halt_and_wedge(_ep, 1, 1);
739}
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741static const struct usb_ep_ops dummy_ep_ops = {
742 .enable = dummy_enable,
743 .disable = dummy_disable,
744
745 .alloc_request = dummy_alloc_request,
746 .free_request = dummy_free_request,
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 .queue = dummy_queue,
749 .dequeue = dummy_dequeue,
750
751 .set_halt = dummy_set_halt,
Alan Stern851a5262008-08-14 15:48:30 -0400752 .set_wedge = dummy_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753};
754
755/*-------------------------------------------------------------------------*/
756
757/* there are both host and device side versions of this call ... */
758static int dummy_g_get_frame (struct usb_gadget *_gadget)
759{
760 struct timeval tv;
761
762 do_gettimeofday (&tv);
763 return tv.tv_usec / 1000;
764}
765
766static int dummy_wakeup (struct usb_gadget *_gadget)
767{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300768 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300770 dum_hcd = gadget_to_dummy_hcd(_gadget);
771 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
Alan Stern5742b0c2005-05-02 11:25:17 -0400772 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return -EINVAL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300774 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
Alan Stern391eca92005-05-10 15:34:16 -0400775 return -ENOLINK;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300776 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
777 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -0400778 return -EIO;
779
780 /* FIXME: What if the root hub is suspended but the port isn't? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 /* hub notices our request, issues downstream resume, etc */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300783 dum_hcd->resuming = 1;
784 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
785 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return 0;
787}
788
789static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
790{
791 struct dummy *dum;
792
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300793 dum = (gadget_to_dummy_hcd(_gadget))->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (value)
795 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
796 else
797 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
798 return 0;
799}
800
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200801static void dummy_udc_udpate_ep0(struct dummy *dum)
802{
803 u32 i;
804
805 if (dum->gadget.speed == USB_SPEED_SUPER) {
806 for (i = 0; i < DUMMY_ENDPOINTS; i++)
807 dum->ep[i].ep.max_streams = 0x10;
808 dum->ep[0].ep.maxpacket = 9;
809 } else {
810 for (i = 0; i < DUMMY_ENDPOINTS; i++)
811 dum->ep[i].ep.max_streams = 0;
812 dum->ep[0].ep.maxpacket = 64;
813 }
814}
815
Alan Sternf1c39fa2005-05-03 16:24:04 -0400816static int dummy_pullup (struct usb_gadget *_gadget, int value)
817{
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200818 struct dummy_hcd *dum_hcd;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400819 struct dummy *dum;
820 unsigned long flags;
821
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200822 dum = gadget_dev_to_dummy(&_gadget->dev);
823
824 if (value && dum->driver) {
825 if (mod_data.is_super_speed)
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100826 dum->gadget.speed = dum->driver->max_speed;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200827 else if (mod_data.is_high_speed)
828 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100829 dum->driver->max_speed);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200830 else
831 dum->gadget.speed = USB_SPEED_FULL;
832 dummy_udc_udpate_ep0(dum);
833
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100834 if (dum->gadget.speed < dum->driver->max_speed)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200835 dev_dbg(udc_dev(dum), "This device can perform faster"
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100836 " if you connect it to a %s port...\n",
837 usb_speed_string(dum->driver->max_speed));
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200838 }
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200839 dum_hcd = gadget_to_dummy_hcd(_gadget);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200840
Alan Sternf1c39fa2005-05-03 16:24:04 -0400841 spin_lock_irqsave (&dum->lock, flags);
842 dum->pullup = (value != 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200843 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400844 spin_unlock_irqrestore (&dum->lock, flags);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200845
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200846 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Sternf1c39fa2005-05-03 16:24:04 -0400847 return 0;
848}
849
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200850static int dummy_udc_start(struct usb_gadget *g,
851 struct usb_gadget_driver *driver);
852static int dummy_udc_stop(struct usb_gadget *g,
853 struct usb_gadget_driver *driver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855static const struct usb_gadget_ops dummy_ops = {
856 .get_frame = dummy_g_get_frame,
857 .wakeup = dummy_wakeup,
858 .set_selfpowered = dummy_set_selfpowered,
Alan Sternf1c39fa2005-05-03 16:24:04 -0400859 .pullup = dummy_pullup,
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200860 .udc_start = dummy_udc_start,
861 .udc_stop = dummy_udc_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862};
863
864/*-------------------------------------------------------------------------*/
865
866/* "function" sysfs attribute */
867static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400868show_function (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 struct dummy *dum = gadget_dev_to_dummy (dev);
871
872 if (!dum->driver || !dum->driver->function)
873 return 0;
874 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
875}
Alan Sterncc095b02005-05-10 15:28:38 -0400876static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878/*-------------------------------------------------------------------------*/
879
880/*
881 * Driver registration/unregistration.
882 *
883 * This is basically hardware-specific; there's usually only one real USB
884 * device (not host) controller since that's how USB devices are intended
885 * to work. So most implementations of these api calls will rely on the
886 * fact that only one driver will ever bind to the hardware. But curious
887 * hardware can be built with discrete components, so the gadget API doesn't
888 * require that assumption.
889 *
890 * For this emulator, it might be convenient to create a usb slave device
891 * for each driver that registers: just add to a big root hub.
892 */
893
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200894static int dummy_udc_start(struct usb_gadget *g,
895 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200897 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
898 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100900 if (driver->max_speed == USB_SPEED_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return -EINVAL;
902
903 /*
904 * SLAVE side init ... the layer above hardware, which
905 * can't enumerate without help from the driver we're binding.
906 */
Alan Stern5742b0c2005-05-02 11:25:17 -0400907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 dum->devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 dum->driver = driver;
Alan Sternd9b76252005-05-03 16:15:43 -0400911 dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return 0;
914}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200916static int dummy_udc_stop(struct usb_gadget *g,
917 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200919 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
920 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Alan Sternd9b76252005-05-03 16:15:43 -0400922 dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 driver->driver.name);
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 dum->driver = NULL;
Sebastian Andrzej Siewior25427872011-06-16 20:36:55 +0200926
927 dummy_pullup(&dum->gadget, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return 0;
929}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931#undef is_enabled
932
Alan Sternd9b76252005-05-03 16:15:43 -0400933/* The gadget structure is stored inside the hcd structure and will be
934 * released along with it. */
935static void
936dummy_gadget_release (struct device *dev)
937{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300938 return;
Alan Sternd9b76252005-05-03 16:15:43 -0400939}
940
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200941static void init_dummy_udc_hw(struct dummy *dum)
942{
943 int i;
944
945 INIT_LIST_HEAD(&dum->gadget.ep_list);
946 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
947 struct dummy_ep *ep = &dum->ep[i];
948
949 if (!ep_name[i])
950 break;
951 ep->ep.name = ep_name[i];
952 ep->ep.ops = &dummy_ep_ops;
953 list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
954 ep->halted = ep->wedged = ep->already_seen =
955 ep->setup_stage = 0;
956 ep->ep.maxpacket = ~0;
957 ep->last_io = jiffies;
958 ep->gadget = &dum->gadget;
959 ep->desc = NULL;
960 INIT_LIST_HEAD(&ep->queue);
961 }
962
963 dum->gadget.ep0 = &dum->ep[0].ep;
964 list_del_init(&dum->ep[0].ep.ep_list);
965 INIT_LIST_HEAD(&dum->fifo_req.queue);
Sebastian Andrzej Siewiorf8744d42011-06-23 14:26:13 +0200966
967#ifdef CONFIG_USB_OTG
968 dum->gadget.is_otg = 1;
969#endif
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200970}
971
Alan Stern8364d6b2005-11-14 12:16:30 -0500972static int dummy_udc_probe (struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -0400973{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300974 struct dummy *dum = &the_controller;
Alan Sternd9b76252005-05-03 16:15:43 -0400975 int rc;
976
977 dum->gadget.name = gadget_name;
978 dum->gadget.ops = &dummy_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100979 dum->gadget.max_speed = USB_SPEED_SUPER;
Alan Sternd9b76252005-05-03 16:15:43 -0400980
Kay Sievers0031a062008-05-02 06:02:41 +0200981 dev_set_name(&dum->gadget.dev, "gadget");
Alan Stern8364d6b2005-11-14 12:16:30 -0500982 dum->gadget.dev.parent = &pdev->dev;
Alan Sternd9b76252005-05-03 16:15:43 -0400983 dum->gadget.dev.release = dummy_gadget_release;
984 rc = device_register (&dum->gadget.dev);
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530985 if (rc < 0) {
986 put_device(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -0400987 return rc;
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530988 }
Alan Sternd9b76252005-05-03 16:15:43 -0400989
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200990 init_dummy_udc_hw(dum);
991
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300992 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
993 if (rc < 0)
994 goto err_udc;
995
Alan Sternefd54a32006-09-25 11:55:56 -0400996 rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
997 if (rc < 0)
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300998 goto err_dev;
999 platform_set_drvdata(pdev, dum);
1000 return rc;
1001
1002err_dev:
1003 usb_del_gadget_udc(&dum->gadget);
1004err_udc:
1005 device_unregister(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -04001006 return rc;
1007}
1008
Alan Stern8364d6b2005-11-14 12:16:30 -05001009static int dummy_udc_remove (struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -04001010{
Alan Stern8364d6b2005-11-14 12:16:30 -05001011 struct dummy *dum = platform_get_drvdata (pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04001012
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001013 usb_del_gadget_udc(&dum->gadget);
Alan Stern8364d6b2005-11-14 12:16:30 -05001014 platform_set_drvdata (pdev, NULL);
Alan Sternd9b76252005-05-03 16:15:43 -04001015 device_remove_file (&dum->gadget.dev, &dev_attr_function);
1016 device_unregister (&dum->gadget.dev);
1017 return 0;
1018}
1019
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001020static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1021 int suspend)
Alan Stern391eca92005-05-10 15:34:16 -04001022{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001023 spin_lock_irq(&dum->lock);
1024 dum->udc_suspended = suspend;
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001025 set_link_state(dum_hcd);
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001026 spin_unlock_irq(&dum->lock);
1027}
Alan Stern391eca92005-05-10 15:34:16 -04001028
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001029static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1030{
1031 struct dummy *dum = platform_get_drvdata(pdev);
1032 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1033
1034 dev_dbg(&pdev->dev, "%s\n", __func__);
1035 dummy_udc_pm(dum, dum_hcd, 1);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001036 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001037 return 0;
1038}
1039
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001040static int dummy_udc_resume(struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04001041{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001042 struct dummy *dum = platform_get_drvdata(pdev);
1043 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Alan Stern391eca92005-05-10 15:34:16 -04001044
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001045 dev_dbg(&pdev->dev, "%s\n", __func__);
1046 dummy_udc_pm(dum, dum_hcd, 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001047 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001048 return 0;
1049}
1050
Russell King3ae5eae2005-11-09 22:32:44 +00001051static struct platform_driver dummy_udc_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04001052 .probe = dummy_udc_probe,
1053 .remove = dummy_udc_remove,
Alan Stern391eca92005-05-10 15:34:16 -04001054 .suspend = dummy_udc_suspend,
1055 .resume = dummy_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001056 .driver = {
1057 .name = (char *) gadget_name,
1058 .owner = THIS_MODULE,
1059 },
Alan Sternd9b76252005-05-03 16:15:43 -04001060};
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062/*-------------------------------------------------------------------------*/
1063
1064/* MASTER/HOST SIDE DRIVER
1065 *
1066 * this uses the hcd framework to hook up to host side drivers.
1067 * its root hub will only have one device, otherwise it acts like
1068 * a normal host controller.
1069 *
1070 * when urbs are queued, they're just stuck on a list that we
1071 * scan in a timer callback. that callback connects writes from
1072 * the host with reads from the device, and so on, based on the
1073 * usb 2.0 rules.
1074 */
1075
1076static int dummy_urb_enqueue (
1077 struct usb_hcd *hcd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001079 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001081 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 struct urbp *urbp;
1083 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001084 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 if (!urb->transfer_buffer && urb->transfer_buffer_length)
1087 return -EINVAL;
1088
1089 urbp = kmalloc (sizeof *urbp, mem_flags);
1090 if (!urbp)
1091 return -ENOMEM;
1092 urbp->urb = urb;
1093
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001094 dum_hcd = hcd_to_dummy_hcd(hcd);
1095 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001096 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1097 if (rc) {
1098 kfree(urbp);
1099 goto done;
1100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001102 if (!dum_hcd->udev) {
1103 dum_hcd->udev = urb->dev;
1104 usb_get_dev(dum_hcd->udev);
1105 } else if (unlikely(dum_hcd->udev != urb->dev))
1106 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001108 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 urb->hcpriv = urbp;
1110 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
1111 urb->error_count = 1; /* mark as a new urb */
1112
1113 /* kick the scheduler, it'll do the rest */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001114 if (!timer_pending(&dum_hcd->timer))
1115 mod_timer(&dum_hcd->timer, jiffies + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Alan Sterne9df41c2007-08-08 11:48:02 -04001117 done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001118 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001119 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
Alan Sterne9df41c2007-08-08 11:48:02 -04001122static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001124 struct dummy_hcd *dum_hcd;
Alan Stern391eca92005-05-10 15:34:16 -04001125 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001126 int rc;
Alan Stern391eca92005-05-10 15:34:16 -04001127
1128 /* giveback happens automatically in timer callback,
1129 * so make sure the callback happens */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001130 dum_hcd = hcd_to_dummy_hcd(hcd);
1131 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001132
1133 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001134 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1135 !list_empty(&dum_hcd->urbp_list))
1136 mod_timer(&dum_hcd->timer, jiffies);
Alan Sterne9df41c2007-08-08 11:48:02 -04001137
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001138 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001139 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142/* transfer up to a frame's worth; caller must own lock */
1143static int
Alan Stern4d2f1102007-08-24 15:40:10 -04001144transfer(struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit,
1145 int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 struct dummy_request *req;
1148
1149top:
1150 /* if there's no request queued, the device is NAKing; return */
1151 list_for_each_entry (req, &ep->queue, queue) {
1152 unsigned host_len, dev_len, len;
1153 int is_short, to_host;
1154 int rescan = 0;
1155
1156 /* 1..N packets of ep->ep.maxpacket each ... the last one
1157 * may be short (including zero length).
1158 *
1159 * writer can send a zlp explicitly (length 0) or implicitly
1160 * (length mod maxpacket zero, and 'zero' flag); they always
1161 * terminate reads.
1162 */
1163 host_len = urb->transfer_buffer_length - urb->actual_length;
1164 dev_len = req->req.length - req->req.actual;
1165 len = min (host_len, dev_len);
1166
1167 /* FIXME update emulated data toggle too */
1168
1169 to_host = usb_pipein (urb->pipe);
1170 if (unlikely (len == 0))
1171 is_short = 1;
1172 else {
1173 char *ubuf, *rbuf;
1174
1175 /* not enough bandwidth left? */
1176 if (limit < ep->ep.maxpacket && limit < len)
1177 break;
1178 len = min (len, (unsigned) limit);
1179 if (len == 0)
1180 break;
1181
1182 /* use an extra pass for the final short packet */
1183 if (len > ep->ep.maxpacket) {
1184 rescan = 1;
1185 len -= (len % ep->ep.maxpacket);
1186 }
1187 is_short = (len % ep->ep.maxpacket) != 0;
1188
1189 /* else transfer packet(s) */
1190 ubuf = urb->transfer_buffer + urb->actual_length;
1191 rbuf = req->req.buf + req->req.actual;
1192 if (to_host)
1193 memcpy (ubuf, rbuf, len);
1194 else
1195 memcpy (rbuf, ubuf, len);
1196 ep->last_io = jiffies;
1197
1198 limit -= len;
1199 urb->actual_length += len;
1200 req->req.actual += len;
1201 }
1202
1203 /* short packets terminate, maybe with overflow/underflow.
1204 * it's only really an error to write too much.
1205 *
1206 * partially filling a buffer optionally blocks queue advances
1207 * (so completion handlers can clean up the queue) but we don't
Alan Sternb0d9efb2007-08-21 15:39:21 -04001208 * need to emulate such data-in-flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 */
1210 if (is_short) {
1211 if (host_len == dev_len) {
1212 req->req.status = 0;
Alan Stern4d2f1102007-08-24 15:40:10 -04001213 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 } else if (to_host) {
1215 req->req.status = 0;
1216 if (dev_len > host_len)
Alan Stern4d2f1102007-08-24 15:40:10 -04001217 *status = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 else
Alan Stern4d2f1102007-08-24 15:40:10 -04001219 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 } else if (!to_host) {
Alan Stern4d2f1102007-08-24 15:40:10 -04001221 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (host_len > dev_len)
1223 req->req.status = -EOVERFLOW;
1224 else
1225 req->req.status = 0;
1226 }
1227
1228 /* many requests terminate without a short packet */
1229 } else {
1230 if (req->req.length == req->req.actual
1231 && !req->req.zero)
1232 req->req.status = 0;
1233 if (urb->transfer_buffer_length == urb->actual_length
1234 && !(urb->transfer_flags
Alan Stern4d2f1102007-08-24 15:40:10 -04001235 & URB_ZERO_PACKET))
1236 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238
1239 /* device side completion --> continuable */
1240 if (req->req.status != -EINPROGRESS) {
1241 list_del_init (&req->queue);
1242
1243 spin_unlock (&dum->lock);
1244 req->req.complete (&ep->ep, &req->req);
1245 spin_lock (&dum->lock);
1246
1247 /* requests might have been unlinked... */
1248 rescan = 1;
1249 }
1250
1251 /* host side completion --> terminate */
Alan Stern4d2f1102007-08-24 15:40:10 -04001252 if (*status != -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 break;
1254
1255 /* rescan to continue with any other queued i/o */
1256 if (rescan)
1257 goto top;
1258 }
1259 return limit;
1260}
1261
1262static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1263{
1264 int limit = ep->ep.maxpacket;
1265
1266 if (dum->gadget.speed == USB_SPEED_HIGH) {
1267 int tmp;
1268
1269 /* high bandwidth mode */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001270 tmp = usb_endpoint_maxp(ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 tmp = (tmp >> 11) & 0x03;
1272 tmp *= 8 /* applies to entire frame */;
1273 limit += limit * tmp;
1274 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001275 if (dum->gadget.speed == USB_SPEED_SUPER) {
1276 switch (ep->desc->bmAttributes & 0x03) {
1277 case USB_ENDPOINT_XFER_ISOC:
1278 /* Sec. 4.4.8.2 USB3.0 Spec */
1279 limit = 3 * 16 * 1024 * 8;
1280 break;
1281 case USB_ENDPOINT_XFER_INT:
1282 /* Sec. 4.4.7.2 USB3.0 Spec */
1283 limit = 3 * 1024 * 8;
1284 break;
1285 case USB_ENDPOINT_XFER_BULK:
1286 default:
1287 break;
1288 }
1289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return limit;
1291}
1292
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001293#define is_active(dum_hcd) ((dum_hcd->port_status & \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1295 USB_PORT_STAT_SUSPEND)) \
1296 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1297
1298static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1299{
1300 int i;
1301
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001302 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1303 dum->ss_hcd : dum->hs_hcd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return NULL;
1305 if ((address & ~USB_DIR_IN) == 0)
1306 return &dum->ep [0];
1307 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1308 struct dummy_ep *ep = &dum->ep [i];
1309
1310 if (!ep->desc)
1311 continue;
1312 if (ep->desc->bEndpointAddress == address)
1313 return ep;
1314 }
1315 return NULL;
1316}
1317
1318#undef is_active
1319
1320#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1321#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1322#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1323#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1324#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1325#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1326
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001327
1328/**
1329 * handle_control_request() - handles all control transfers
1330 * @dum: pointer to dummy (the_controller)
1331 * @urb: the urb request to handle
1332 * @setup: pointer to the setup data for a USB device control
1333 * request
1334 * @status: pointer to request handling status
1335 *
1336 * Return 0 - if the request was handled
1337 * 1 - if the request wasn't handles
1338 * error code on error
1339 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001340static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001341 struct usb_ctrlrequest *setup,
1342 int *status)
1343{
1344 struct dummy_ep *ep2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001345 struct dummy *dum = dum_hcd->dum;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001346 int ret_val = 1;
1347 unsigned w_index;
1348 unsigned w_value;
1349
1350 w_index = le16_to_cpu(setup->wIndex);
1351 w_value = le16_to_cpu(setup->wValue);
1352 switch (setup->bRequest) {
1353 case USB_REQ_SET_ADDRESS:
1354 if (setup->bRequestType != Dev_Request)
1355 break;
1356 dum->address = w_value;
1357 *status = 0;
1358 dev_dbg(udc_dev(dum), "set_address = %d\n",
1359 w_value);
1360 ret_val = 0;
1361 break;
1362 case USB_REQ_SET_FEATURE:
1363 if (setup->bRequestType == Dev_Request) {
1364 ret_val = 0;
1365 switch (w_value) {
1366 case USB_DEVICE_REMOTE_WAKEUP:
1367 break;
1368 case USB_DEVICE_B_HNP_ENABLE:
1369 dum->gadget.b_hnp_enable = 1;
1370 break;
1371 case USB_DEVICE_A_HNP_SUPPORT:
1372 dum->gadget.a_hnp_support = 1;
1373 break;
1374 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1375 dum->gadget.a_alt_hnp_support = 1;
1376 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001377 case USB_DEVICE_U1_ENABLE:
1378 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1379 HCD_USB3)
1380 w_value = USB_DEV_STAT_U1_ENABLED;
1381 else
1382 ret_val = -EOPNOTSUPP;
1383 break;
1384 case USB_DEVICE_U2_ENABLE:
1385 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1386 HCD_USB3)
1387 w_value = USB_DEV_STAT_U2_ENABLED;
1388 else
1389 ret_val = -EOPNOTSUPP;
1390 break;
1391 case USB_DEVICE_LTM_ENABLE:
1392 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1393 HCD_USB3)
1394 w_value = USB_DEV_STAT_LTM_ENABLED;
1395 else
1396 ret_val = -EOPNOTSUPP;
1397 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001398 default:
1399 ret_val = -EOPNOTSUPP;
1400 }
1401 if (ret_val == 0) {
1402 dum->devstatus |= (1 << w_value);
1403 *status = 0;
1404 }
1405 } else if (setup->bRequestType == Ep_Request) {
1406 /* endpoint halt */
1407 ep2 = find_endpoint(dum, w_index);
1408 if (!ep2 || ep2->ep.name == ep0name) {
1409 ret_val = -EOPNOTSUPP;
1410 break;
1411 }
1412 ep2->halted = 1;
1413 ret_val = 0;
1414 *status = 0;
1415 }
1416 break;
1417 case USB_REQ_CLEAR_FEATURE:
1418 if (setup->bRequestType == Dev_Request) {
1419 ret_val = 0;
1420 switch (w_value) {
1421 case USB_DEVICE_REMOTE_WAKEUP:
1422 w_value = USB_DEVICE_REMOTE_WAKEUP;
1423 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001424 case USB_DEVICE_U1_ENABLE:
1425 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1426 HCD_USB3)
1427 w_value = USB_DEV_STAT_U1_ENABLED;
1428 else
1429 ret_val = -EOPNOTSUPP;
1430 break;
1431 case USB_DEVICE_U2_ENABLE:
1432 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1433 HCD_USB3)
1434 w_value = USB_DEV_STAT_U2_ENABLED;
1435 else
1436 ret_val = -EOPNOTSUPP;
1437 break;
1438 case USB_DEVICE_LTM_ENABLE:
1439 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1440 HCD_USB3)
1441 w_value = USB_DEV_STAT_LTM_ENABLED;
1442 else
1443 ret_val = -EOPNOTSUPP;
1444 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001445 default:
1446 ret_val = -EOPNOTSUPP;
1447 break;
1448 }
1449 if (ret_val == 0) {
1450 dum->devstatus &= ~(1 << w_value);
1451 *status = 0;
1452 }
1453 } else if (setup->bRequestType == Ep_Request) {
1454 /* endpoint halt */
1455 ep2 = find_endpoint(dum, w_index);
1456 if (!ep2) {
1457 ret_val = -EOPNOTSUPP;
1458 break;
1459 }
1460 if (!ep2->wedged)
1461 ep2->halted = 0;
1462 ret_val = 0;
1463 *status = 0;
1464 }
1465 break;
1466 case USB_REQ_GET_STATUS:
1467 if (setup->bRequestType == Dev_InRequest
1468 || setup->bRequestType == Intf_InRequest
1469 || setup->bRequestType == Ep_InRequest) {
1470 char *buf;
1471 /*
1472 * device: remote wakeup, selfpowered
1473 * interface: nothing
1474 * endpoint: halt
1475 */
1476 buf = (char *)urb->transfer_buffer;
1477 if (urb->transfer_buffer_length > 0) {
1478 if (setup->bRequestType == Ep_InRequest) {
1479 ep2 = find_endpoint(dum, w_index);
1480 if (!ep2) {
1481 ret_val = -EOPNOTSUPP;
1482 break;
1483 }
1484 buf[0] = ep2->halted;
1485 } else if (setup->bRequestType ==
1486 Dev_InRequest) {
1487 buf[0] = (u8)dum->devstatus;
1488 } else
1489 buf[0] = 0;
1490 }
1491 if (urb->transfer_buffer_length > 1)
1492 buf[1] = 0;
1493 urb->actual_length = min_t(u32, 2,
1494 urb->transfer_buffer_length);
1495 ret_val = 0;
1496 *status = 0;
1497 }
1498 break;
1499 }
1500 return ret_val;
1501}
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503/* drive both sides of the transfers; looks like irq handlers to
1504 * both drivers except the callbacks aren't in_irq().
1505 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001506static void dummy_timer(unsigned long _dum_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001508 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1509 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 struct urbp *urbp, *tmp;
1511 unsigned long flags;
1512 int limit, total;
1513 int i;
1514
1515 /* simplistic model for one frame's bandwidth */
1516 switch (dum->gadget.speed) {
1517 case USB_SPEED_LOW:
1518 total = 8/*bytes*/ * 12/*packets*/;
1519 break;
1520 case USB_SPEED_FULL:
1521 total = 64/*bytes*/ * 19/*packets*/;
1522 break;
1523 case USB_SPEED_HIGH:
1524 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1525 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001526 case USB_SPEED_SUPER:
1527 /* Bus speed is 500000 bytes/ms, so use a little less */
1528 total = 490000;
1529 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001531 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 return;
1533 }
1534
1535 /* FIXME if HZ != 1000 this will probably misbehave ... */
1536
1537 /* look at each urb queued by the host side driver */
1538 spin_lock_irqsave (&dum->lock, flags);
1539
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001540 if (!dum_hcd->udev) {
1541 dev_err(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 "timer fired with no URBs pending?\n");
1543 spin_unlock_irqrestore (&dum->lock, flags);
1544 return;
1545 }
1546
1547 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1548 if (!ep_name [i])
1549 break;
1550 dum->ep [i].already_seen = 0;
1551 }
1552
1553restart:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001554 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 struct urb *urb;
1556 struct dummy_request *req;
1557 u8 address;
1558 struct dummy_ep *ep = NULL;
1559 int type;
Alan Stern4d2f1102007-08-24 15:40:10 -04001560 int status = -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 urb = urbp->urb;
Alan Sterneb231052007-08-21 15:40:36 -04001563 if (urb->unlinked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 goto return_urb;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001565 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
Alan Stern391eca92005-05-10 15:34:16 -04001566 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 type = usb_pipetype (urb->pipe);
1568
1569 /* used up this frame's non-periodic bandwidth?
1570 * FIXME there's infinite bandwidth for control and
1571 * periodic transfers ... unrealistic.
1572 */
1573 if (total <= 0 && type == PIPE_BULK)
1574 continue;
1575
1576 /* find the gadget's ep for this request (if configured) */
1577 address = usb_pipeendpoint (urb->pipe);
1578 if (usb_pipein (urb->pipe))
1579 address |= USB_DIR_IN;
1580 ep = find_endpoint(dum, address);
1581 if (!ep) {
1582 /* set_configuration() disagreement */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001583 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 "no ep configured for urb %p\n",
1585 urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001586 status = -EPROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 goto return_urb;
1588 }
1589
1590 if (ep->already_seen)
1591 continue;
1592 ep->already_seen = 1;
1593 if (ep == &dum->ep [0] && urb->error_count) {
1594 ep->setup_stage = 1; /* a new urb */
1595 urb->error_count = 0;
1596 }
1597 if (ep->halted && !ep->setup_stage) {
1598 /* NOTE: must not be iso! */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001599 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 ep->ep.name, urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001601 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 goto return_urb;
1603 }
1604 /* FIXME make sure both ends agree on maxpacket */
1605
1606 /* handle control requests */
1607 if (ep == &dum->ep [0] && ep->setup_stage) {
1608 struct usb_ctrlrequest setup;
1609 int value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 /* paranoia, in case of stale queued data */
1613 list_for_each_entry (req, &ep->queue, queue) {
1614 list_del_init (&req->queue);
1615 req->req.status = -EOVERFLOW;
Alan Sternd9b76252005-05-03 16:15:43 -04001616 dev_dbg (udc_dev(dum), "stale req = %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 req);
1618
1619 spin_unlock (&dum->lock);
1620 req->req.complete (&ep->ep, &req->req);
1621 spin_lock (&dum->lock);
1622 ep->already_seen = 0;
1623 goto restart;
1624 }
1625
1626 /* gadget driver never sees set_address or operations
1627 * on standard feature flags. some hardware doesn't
1628 * even expose them.
1629 */
1630 ep->last_io = jiffies;
1631 ep->setup_stage = 0;
1632 ep->halted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001634 value = handle_control_request(dum_hcd, urb, &setup,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001635 &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 /* gadget driver handles all other requests. block
1638 * until setup() returns; no reentrancy issues etc.
1639 */
1640 if (value > 0) {
1641 spin_unlock (&dum->lock);
1642 value = dum->driver->setup (&dum->gadget,
1643 &setup);
1644 spin_lock (&dum->lock);
1645
1646 if (value >= 0) {
1647 /* no delays (max 64KB data stage) */
1648 limit = 64*1024;
1649 goto treat_control_like_bulk;
1650 }
1651 /* error, see below */
1652 }
1653
1654 if (value < 0) {
1655 if (value != -EOPNOTSUPP)
Alan Sternd9b76252005-05-03 16:15:43 -04001656 dev_dbg (udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 "setup --> %d\n",
1658 value);
Alan Stern4d2f1102007-08-24 15:40:10 -04001659 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 urb->actual_length = 0;
1661 }
1662
1663 goto return_urb;
1664 }
1665
1666 /* non-control requests */
1667 limit = total;
1668 switch (usb_pipetype (urb->pipe)) {
1669 case PIPE_ISOCHRONOUS:
1670 /* FIXME is it urb->interval since the last xfer?
1671 * use urb->iso_frame_desc[i].
1672 * complete whether or not ep has requests queued.
1673 * report random errors, to debug drivers.
1674 */
1675 limit = max (limit, periodic_bytes (dum, ep));
Alan Stern4d2f1102007-08-24 15:40:10 -04001676 status = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 break;
1678
1679 case PIPE_INTERRUPT:
1680 /* FIXME is it urb->interval since the last xfer?
1681 * this almost certainly polls too fast.
1682 */
1683 limit = max (limit, periodic_bytes (dum, ep));
1684 /* FALLTHROUGH */
1685
1686 // case PIPE_BULK: case PIPE_CONTROL:
1687 default:
1688 treat_control_like_bulk:
1689 ep->last_io = jiffies;
Alan Stern4d2f1102007-08-24 15:40:10 -04001690 total = transfer(dum, urb, ep, limit, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 break;
1692 }
1693
1694 /* incomplete transfer? */
Alan Stern4d2f1102007-08-24 15:40:10 -04001695 if (status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 continue;
1697
1698return_urb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 list_del (&urbp->urbp_list);
1700 kfree (urbp);
1701 if (ep)
1702 ep->already_seen = ep->setup_stage = 0;
1703
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001704 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 spin_unlock (&dum->lock);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001706 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 spin_lock (&dum->lock);
1708
1709 goto restart;
1710 }
1711
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001712 if (list_empty(&dum_hcd->urbp_list)) {
1713 usb_put_dev(dum_hcd->udev);
1714 dum_hcd->udev = NULL;
1715 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern391eca92005-05-10 15:34:16 -04001716 /* want a 1 msec delay here */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001717 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719
1720 spin_unlock_irqrestore (&dum->lock, flags);
1721}
1722
1723/*-------------------------------------------------------------------------*/
1724
1725#define PORT_C_MASK \
Alan Sternc2db8b52005-04-29 16:30:48 -04001726 ((USB_PORT_STAT_C_CONNECTION \
1727 | USB_PORT_STAT_C_ENABLE \
1728 | USB_PORT_STAT_C_SUSPEND \
1729 | USB_PORT_STAT_C_OVERCURRENT \
1730 | USB_PORT_STAT_C_RESET) << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1733{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001734 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 unsigned long flags;
Alan Stern391eca92005-05-10 15:34:16 -04001736 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001738 dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001740 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001741 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001742 goto done;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001743
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001744 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1745 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1746 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1747 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001748 }
1749
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001750 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 *buf = (1 << 1);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001752 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1753 dum_hcd->port_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 retval = 1;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001755 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -04001756 usb_hcd_resume_root_hub (hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
Alan Stern391eca92005-05-10 15:34:16 -04001758done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001759 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 return retval;
1761}
1762
1763static inline void
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001764ss_hub_descriptor(struct usb_hub_descriptor *desc)
1765{
1766 memset(desc, 0, sizeof *desc);
1767 desc->bDescriptorType = 0x2a;
1768 desc->bDescLength = 12;
1769 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1770 desc->bNbrPorts = 1;
1771 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1772 desc->u.ss.DeviceRemovable = 0xffff;
1773}
1774
1775static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776hub_descriptor (struct usb_hub_descriptor *desc)
1777{
1778 memset (desc, 0, sizeof *desc);
1779 desc->bDescriptorType = 0x29;
1780 desc->bDescLength = 9;
Al Virofd05e722008-04-28 07:00:16 +01001781 desc->wHubCharacteristics = cpu_to_le16(0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 desc->bNbrPorts = 1;
John Youndbe79bb2001-09-17 00:00:00 -07001783 desc->u.hs.DeviceRemovable[0] = 0xff;
1784 desc->u.hs.DeviceRemovable[1] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
1787static int dummy_hub_control (
1788 struct usb_hcd *hcd,
1789 u16 typeReq,
1790 u16 wValue,
1791 u16 wIndex,
1792 char *buf,
1793 u16 wLength
1794) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001795 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 int retval = 0;
1797 unsigned long flags;
1798
Alan Stern541c7d42010-06-22 16:39:10 -04001799 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001800 return -ETIMEDOUT;
1801
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001802 dum_hcd = hcd_to_dummy_hcd(hcd);
1803
1804 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 switch (typeReq) {
1806 case ClearHubFeature:
1807 break;
1808 case ClearPortFeature:
1809 switch (wValue) {
1810 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001811 if (hcd->speed == HCD_USB3) {
1812 dev_dbg(dummy_dev(dum_hcd),
1813 "USB_PORT_FEAT_SUSPEND req not "
1814 "supported for USB 3.0 roothub\n");
1815 goto error;
1816 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001817 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 /* 20msec resume signaling */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001819 dum_hcd->resuming = 1;
1820 dum_hcd->re_timeout = jiffies +
Alan Sternf1c39fa2005-05-03 16:24:04 -04001821 msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
1823 break;
1824 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001825 if (hcd->speed == HCD_USB3) {
1826 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1827 dev_dbg(dummy_dev(dum_hcd),
1828 "power-off\n");
1829 } else
1830 if (dum_hcd->port_status &
1831 USB_SS_PORT_STAT_POWER)
1832 dev_dbg(dummy_dev(dum_hcd),
1833 "power-off\n");
Alan Sternf1c39fa2005-05-03 16:24:04 -04001834 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001836 dum_hcd->port_status &= ~(1 << wValue);
1837 set_link_state(dum_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839 break;
1840 case GetHubDescriptor:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001841 if (hcd->speed == HCD_USB3 &&
1842 (wLength < USB_DT_SS_HUB_SIZE ||
1843 wValue != (USB_DT_SS_HUB << 8))) {
1844 dev_dbg(dummy_dev(dum_hcd),
1845 "Wrong hub descriptor type for "
1846 "USB 3.0 roothub.\n");
1847 goto error;
1848 }
1849 if (hcd->speed == HCD_USB3)
1850 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
1851 else
1852 hub_descriptor((struct usb_hub_descriptor *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 break;
1854 case GetHubStatus:
Harvey Harrison551509d2009-02-11 14:11:36 -08001855 *(__le32 *) buf = cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 break;
1857 case GetPortStatus:
1858 if (wIndex != 1)
1859 retval = -EPIPE;
1860
1861 /* whoever resets or resumes must GetPortStatus to
1862 * complete it!!
1863 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001864 if (dum_hcd->resuming &&
1865 time_after_eq(jiffies, dum_hcd->re_timeout)) {
1866 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1867 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001869 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
1870 time_after_eq(jiffies, dum_hcd->re_timeout)) {
1871 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
1872 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
1873 if (dum_hcd->dum->pullup) {
1874 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001875
1876 if (hcd->speed < HCD_USB3) {
1877 switch (dum_hcd->dum->gadget.speed) {
1878 case USB_SPEED_HIGH:
1879 dum_hcd->port_status |=
1880 USB_PORT_STAT_HIGH_SPEED;
1881 break;
1882 case USB_SPEED_LOW:
1883 dum_hcd->dum->gadget.ep0->
1884 maxpacket = 8;
1885 dum_hcd->port_status |=
1886 USB_PORT_STAT_LOW_SPEED;
1887 break;
1888 default:
1889 dum_hcd->dum->gadget.speed =
1890 USB_SPEED_FULL;
1891 break;
1892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894 }
1895 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001896 set_link_state(dum_hcd);
1897 ((__le16 *) buf)[0] = cpu_to_le16 (dum_hcd->port_status);
1898 ((__le16 *) buf)[1] = cpu_to_le16 (dum_hcd->port_status >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 break;
1900 case SetHubFeature:
1901 retval = -EPIPE;
1902 break;
1903 case SetPortFeature:
1904 switch (wValue) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001905 case USB_PORT_FEAT_LINK_STATE:
1906 if (hcd->speed != HCD_USB3) {
1907 dev_dbg(dummy_dev(dum_hcd),
1908 "USB_PORT_FEAT_LINK_STATE req not "
1909 "supported for USB 2.0 roothub\n");
1910 goto error;
1911 }
1912 /*
1913 * Since this is dummy we don't have an actual link so
1914 * there is nothing to do for the SET_LINK_STATE cmd
1915 */
1916 break;
1917 case USB_PORT_FEAT_U1_TIMEOUT:
1918 case USB_PORT_FEAT_U2_TIMEOUT:
1919 /* TODO: add suspend/resume support! */
1920 if (hcd->speed != HCD_USB3) {
1921 dev_dbg(dummy_dev(dum_hcd),
1922 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
1923 "supported for USB 2.0 roothub\n");
1924 goto error;
1925 }
1926 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001928 /* Applicable only for USB2.0 hub */
1929 if (hcd->speed == HCD_USB3) {
1930 dev_dbg(dummy_dev(dum_hcd),
1931 "USB_PORT_FEAT_SUSPEND req not "
1932 "supported for USB 3.0 roothub\n");
1933 goto error;
1934 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001935 if (dum_hcd->active) {
1936 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001937
1938 /* HNP would happen here; for now we
1939 * assume b_bus_req is always true.
1940 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001941 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001942 if (((1 << USB_DEVICE_B_HNP_ENABLE)
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001943 & dum_hcd->dum->devstatus) != 0)
1944 dev_dbg(dummy_dev(dum_hcd),
Alan Stern5742b0c2005-05-02 11:25:17 -04001945 "no HNP yet!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
1947 break;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001948 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001949 if (hcd->speed == HCD_USB3)
1950 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
1951 else
1952 dum_hcd->port_status |= USB_PORT_STAT_POWER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001953 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001954 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001955 case USB_PORT_FEAT_BH_PORT_RESET:
1956 /* Applicable only for USB3.0 hub */
1957 if (hcd->speed != HCD_USB3) {
1958 dev_dbg(dummy_dev(dum_hcd),
1959 "USB_PORT_FEAT_BH_PORT_RESET req not "
1960 "supported for USB 2.0 roothub\n");
1961 goto error;
1962 }
1963 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 case USB_PORT_FEAT_RESET:
Alan Sternf1c39fa2005-05-03 16:24:04 -04001965 /* if it's already enabled, disable */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001966 if (hcd->speed == HCD_USB3) {
1967 dum_hcd->port_status = 0;
1968 dum_hcd->port_status =
1969 (USB_SS_PORT_STAT_POWER |
1970 USB_PORT_STAT_CONNECTION |
1971 USB_PORT_STAT_RESET);
1972 } else
1973 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
Alan Sternf1c39fa2005-05-03 16:24:04 -04001974 | USB_PORT_STAT_LOW_SPEED
1975 | USB_PORT_STAT_HIGH_SPEED);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001976 /*
1977 * We want to reset device status. All but the
1978 * Self powered feature
1979 */
1980 dum_hcd->dum->devstatus &=
1981 (1 << USB_DEVICE_SELF_POWERED);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001982 /*
1983 * FIXME USB3.0: what is the correct reset signaling
1984 * interval? Is it still 50msec as for HS?
1985 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001986 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001987 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 default:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001989 if (hcd->speed == HCD_USB3) {
1990 if ((dum_hcd->port_status &
1991 USB_SS_PORT_STAT_POWER) != 0) {
1992 dum_hcd->port_status |= (1 << wValue);
1993 set_link_state(dum_hcd);
1994 }
1995 } else
1996 if ((dum_hcd->port_status &
1997 USB_PORT_STAT_POWER) != 0) {
1998 dum_hcd->port_status |= (1 << wValue);
1999 set_link_state(dum_hcd);
2000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 }
2002 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002003 case GetPortErrorCount:
2004 if (hcd->speed != HCD_USB3) {
2005 dev_dbg(dummy_dev(dum_hcd),
2006 "GetPortErrorCount req not "
2007 "supported for USB 2.0 roothub\n");
2008 goto error;
2009 }
2010 /* We'll always return 0 since this is a dummy hub */
2011 *(__le32 *) buf = cpu_to_le32(0);
2012 break;
2013 case SetHubDepth:
2014 if (hcd->speed != HCD_USB3) {
2015 dev_dbg(dummy_dev(dum_hcd),
2016 "SetHubDepth req not supported for "
2017 "USB 2.0 roothub\n");
2018 goto error;
2019 }
2020 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002022 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 "hub control req%04x v%04x i%04x l%d\n",
2024 typeReq, wValue, wIndex, wLength);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002025error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 /* "protocol stall" on error */
2027 retval = -EPIPE;
2028 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002029 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Stern685eb932005-05-03 16:27:26 -04002030
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002031 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
Alan Stern685eb932005-05-03 16:27:26 -04002032 usb_hcd_poll_rh_status (hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 return retval;
2034}
2035
Alan Stern0c0382e2005-10-13 17:08:02 -04002036static int dummy_bus_suspend (struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002037{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002038 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002039
Harvey Harrison441b62c2008-03-03 16:08:34 -08002040 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern3cf0a222005-11-29 12:08:15 -05002041
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002042 spin_lock_irq(&dum_hcd->dum->lock);
2043 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2044 set_link_state(dum_hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002045 hcd->state = HC_STATE_SUSPENDED;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002046 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern391eca92005-05-10 15:34:16 -04002047 return 0;
2048}
2049
Alan Stern0c0382e2005-10-13 17:08:02 -04002050static int dummy_bus_resume (struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002051{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002052 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002053 int rc = 0;
2054
Harvey Harrison441b62c2008-03-03 16:08:34 -08002055 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002056
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002057 spin_lock_irq(&dum_hcd->dum->lock);
Alan Stern541c7d42010-06-22 16:39:10 -04002058 if (!HCD_HW_ACCESSIBLE(hcd)) {
Alan Sterncfa59da2007-06-21 16:25:35 -04002059 rc = -ESHUTDOWN;
Alan Stern3cf0a222005-11-29 12:08:15 -05002060 } else {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002061 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2062 set_link_state(dum_hcd);
2063 if (!list_empty(&dum_hcd->urbp_list))
2064 mod_timer(&dum_hcd->timer, jiffies);
Alan Stern3cf0a222005-11-29 12:08:15 -05002065 hcd->state = HC_STATE_RUNNING;
2066 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002067 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern3cf0a222005-11-29 12:08:15 -05002068 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002069}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071/*-------------------------------------------------------------------------*/
2072
2073static inline ssize_t
2074show_urb (char *buf, size_t size, struct urb *urb)
2075{
2076 int ep = usb_pipeendpoint (urb->pipe);
2077
2078 return snprintf (buf, size,
2079 "urb/%p %s ep%d%s%s len %d/%d\n",
2080 urb,
2081 ({ char *s;
2082 switch (urb->dev->speed) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002083 case USB_SPEED_LOW:
2084 s = "ls";
2085 break;
2086 case USB_SPEED_FULL:
2087 s = "fs";
2088 break;
2089 case USB_SPEED_HIGH:
2090 s = "hs";
2091 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002092 case USB_SPEED_SUPER:
2093 s = "ss";
2094 break;
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002095 default:
2096 s = "?";
2097 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }; s; }),
2099 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
2100 ({ char *s; \
2101 switch (usb_pipetype (urb->pipe)) { \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002102 case PIPE_CONTROL: \
2103 s = ""; \
2104 break; \
2105 case PIPE_BULK: \
2106 s = "-bulk"; \
2107 break; \
2108 case PIPE_INTERRUPT: \
2109 s = "-int"; \
2110 break; \
2111 default: \
2112 s = "-iso"; \
2113 break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }; s;}),
2115 urb->actual_length, urb->transfer_buffer_length);
2116}
2117
2118static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -04002119show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
2121 struct usb_hcd *hcd = dev_get_drvdata (dev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002122 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 struct urbp *urbp;
2124 size_t size = 0;
2125 unsigned long flags;
2126
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002127 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2128 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 size_t temp;
2130
2131 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
2132 buf += temp;
2133 size += temp;
2134 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002135 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137 return size;
2138}
2139static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
2140
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002141static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2142{
2143 init_timer(&dum_hcd->timer);
2144 dum_hcd->timer.function = dummy_timer;
2145 dum_hcd->timer.data = (unsigned long)dum_hcd;
2146 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2147 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2148 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2149 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2150 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2151#ifdef CONFIG_USB_OTG
2152 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2153#endif
2154 return 0;
2155
2156 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2157 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2158}
2159
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002160static int dummy_start(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002162 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 /*
2165 * MASTER side init ... we emulate a root hub that'll only ever
2166 * talk to one device (the slave side). Also appears in sysfs,
2167 * just like more familiar pci-based HCDs.
2168 */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002169 if (!usb_hcd_is_primary_hcd(hcd))
2170 return dummy_start_ss(dum_hcd);
2171
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002172 spin_lock_init(&dum_hcd->dum->lock);
2173 init_timer(&dum_hcd->timer);
2174 dum_hcd->timer.function = dummy_timer;
2175 dum_hcd->timer.data = (unsigned long)dum_hcd;
2176 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002178 INIT_LIST_HEAD(&dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Alan Sterncaf29f62007-12-06 11:10:39 -05002180 hcd->power_budget = POWER_BUDGET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 hcd->state = HC_STATE_RUNNING;
Alan Stern685eb932005-05-03 16:27:26 -04002182 hcd->uses_new_polling = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Alan Stern5742b0c2005-05-02 11:25:17 -04002184#ifdef CONFIG_USB_OTG
2185 hcd->self.otg_port = 1;
2186#endif
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002189 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
2192static void dummy_stop (struct usb_hcd *hcd)
2193{
2194 struct dummy *dum;
2195
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002196 dum = (hcd_to_dummy_hcd(hcd))->dum;
2197 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2198 usb_gadget_unregister_driver(dum->driver);
2199 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200}
2201
2202/*-------------------------------------------------------------------------*/
2203
2204static int dummy_h_get_frame (struct usb_hcd *hcd)
2205{
2206 return dummy_g_get_frame (NULL);
2207}
2208
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002209static int dummy_setup(struct usb_hcd *hcd)
2210{
2211 if (usb_hcd_is_primary_hcd(hcd)) {
2212 the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
2213 the_controller.hs_hcd->dum = &the_controller;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002214 /*
2215 * Mark the first roothub as being USB 2.0.
2216 * The USB 3.0 roothub will be registered later by
2217 * dummy_hcd_probe()
2218 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002219 hcd->speed = HCD_USB2;
2220 hcd->self.root_hub->speed = USB_SPEED_HIGH;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002221 } else {
2222 the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
2223 the_controller.ss_hcd->dum = &the_controller;
2224 hcd->speed = HCD_USB3;
2225 hcd->self.root_hub->speed = USB_SPEED_SUPER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002226 }
2227 return 0;
2228}
2229
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002230/* Change a group of bulk endpoints to support multiple stream IDs */
2231int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
2232 struct usb_host_endpoint **eps, unsigned int num_eps,
2233 unsigned int num_streams, gfp_t mem_flags)
2234{
2235 if (hcd->speed != HCD_USB3)
2236 dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
2237 "%s() - ERROR! Not supported for USB2.0 roothub\n",
2238 __func__);
2239 return 0;
2240}
2241
2242/* Reverts a group of bulk endpoints back to not using stream IDs. */
2243int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
2244 struct usb_host_endpoint **eps, unsigned int num_eps,
2245 gfp_t mem_flags)
2246{
2247 if (hcd->speed != HCD_USB3)
2248 dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
2249 "%s() - ERROR! Not supported for USB2.0 roothub\n",
2250 __func__);
2251 return 0;
2252}
2253
2254static struct hc_driver dummy_hcd = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 .description = (char *) driver_name,
2256 .product_desc = "Dummy host controller",
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002257 .hcd_priv_size = sizeof(struct dummy_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002259 .flags = HCD_USB3 | HCD_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002261 .reset = dummy_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 .start = dummy_start,
2263 .stop = dummy_stop,
2264
2265 .urb_enqueue = dummy_urb_enqueue,
2266 .urb_dequeue = dummy_urb_dequeue,
2267
2268 .get_frame_number = dummy_h_get_frame,
2269
2270 .hub_status_data = dummy_hub_status,
2271 .hub_control = dummy_hub_control,
Alan Stern0c0382e2005-10-13 17:08:02 -04002272 .bus_suspend = dummy_bus_suspend,
2273 .bus_resume = dummy_bus_resume,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002274
2275 .alloc_streams = dummy_alloc_streams,
2276 .free_streams = dummy_free_streams,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277};
2278
Alan Stern8364d6b2005-11-14 12:16:30 -05002279static int dummy_hcd_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002281 struct usb_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002282 struct usb_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 int retval;
2284
Alan Stern8364d6b2005-11-14 12:16:30 -05002285 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002287 if (!mod_data.is_super_speed)
2288 dummy_hcd.flags = HCD_USB2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002289 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2290 if (!hs_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 return -ENOMEM;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002292 hs_hcd->has_tt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002294 retval = usb_add_hcd(hs_hcd, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 if (retval != 0) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002296 usb_put_hcd(hs_hcd);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002297 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002299
2300 if (mod_data.is_super_speed) {
2301 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2302 dev_name(&pdev->dev), hs_hcd);
2303 if (!ss_hcd) {
2304 retval = -ENOMEM;
2305 goto dealloc_usb2_hcd;
2306 }
2307
2308 retval = usb_add_hcd(ss_hcd, 0, 0);
2309 if (retval)
2310 goto put_usb3_hcd;
2311 }
2312 return 0;
2313
2314put_usb3_hcd:
2315 usb_put_hcd(ss_hcd);
2316dealloc_usb2_hcd:
2317 usb_put_hcd(hs_hcd);
2318 the_controller.hs_hcd = the_controller.ss_hcd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 return retval;
2320}
2321
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002322static int dummy_hcd_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002324 struct dummy *dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002326 dum = (hcd_to_dummy_hcd(platform_get_drvdata(pdev)))->dum;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002327
2328 if (dum->ss_hcd) {
2329 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2330 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2331 }
2332
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002333 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2334 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002335
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002336 the_controller.hs_hcd = NULL;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002337 the_controller.ss_hcd = NULL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002338
Alan Sternd9b76252005-05-03 16:15:43 -04002339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340}
2341
Alan Stern8364d6b2005-11-14 12:16:30 -05002342static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
Alan Stern391eca92005-05-10 15:34:16 -04002343{
2344 struct usb_hcd *hcd;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002345 struct dummy_hcd *dum_hcd;
Alan Stern3cf0a222005-11-29 12:08:15 -05002346 int rc = 0;
Alan Stern391eca92005-05-10 15:34:16 -04002347
Harvey Harrison441b62c2008-03-03 16:08:34 -08002348 dev_dbg (&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002349
Alan Stern3cf0a222005-11-29 12:08:15 -05002350 hcd = platform_get_drvdata (pdev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002351 dum_hcd = hcd_to_dummy_hcd(hcd);
2352 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern3cf0a222005-11-29 12:08:15 -05002353 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2354 rc = -EBUSY;
2355 } else
2356 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2357 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002358}
2359
Alan Stern8364d6b2005-11-14 12:16:30 -05002360static int dummy_hcd_resume (struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04002361{
2362 struct usb_hcd *hcd;
2363
Harvey Harrison441b62c2008-03-03 16:08:34 -08002364 dev_dbg (&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002365
Alan Stern3cf0a222005-11-29 12:08:15 -05002366 hcd = platform_get_drvdata (pdev);
2367 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Alan Stern391eca92005-05-10 15:34:16 -04002368 usb_hcd_poll_rh_status (hcd);
2369 return 0;
2370}
2371
Russell King3ae5eae2005-11-09 22:32:44 +00002372static struct platform_driver dummy_hcd_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04002373 .probe = dummy_hcd_probe,
2374 .remove = dummy_hcd_remove,
Alan Stern391eca92005-05-10 15:34:16 -04002375 .suspend = dummy_hcd_suspend,
2376 .resume = dummy_hcd_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002377 .driver = {
2378 .name = (char *) driver_name,
2379 .owner = THIS_MODULE,
2380 },
Alan Sternd9b76252005-05-03 16:15:43 -04002381};
2382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383/*-------------------------------------------------------------------------*/
2384
Alan Sterna89a2cd2008-04-07 15:03:25 -04002385static struct platform_device *the_udc_pdev;
2386static struct platform_device *the_hcd_pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
2388static int __init init (void)
2389{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002390 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
2392 if (usb_disabled ())
2393 return -ENODEV;
Alan Sternd9b76252005-05-03 16:15:43 -04002394
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +03002395 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2396 return -EINVAL;
2397
Alan Sterna89a2cd2008-04-07 15:03:25 -04002398 the_hcd_pdev = platform_device_alloc(driver_name, -1);
2399 if (!the_hcd_pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 return retval;
Alan Sterna89a2cd2008-04-07 15:03:25 -04002401 the_udc_pdev = platform_device_alloc(gadget_name, -1);
2402 if (!the_udc_pdev)
2403 goto err_alloc_udc;
Alan Sternd9b76252005-05-03 16:15:43 -04002404
Alan Sterna89a2cd2008-04-07 15:03:25 -04002405 retval = platform_driver_register(&dummy_hcd_driver);
2406 if (retval < 0)
2407 goto err_register_hcd_driver;
2408 retval = platform_driver_register(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002409 if (retval < 0)
2410 goto err_register_udc_driver;
2411
Alan Sterna89a2cd2008-04-07 15:03:25 -04002412 retval = platform_device_add(the_hcd_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002413 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002414 goto err_add_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002415 if (!the_controller.hs_hcd ||
2416 (!the_controller.ss_hcd && mod_data.is_super_speed)) {
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002417 /*
2418 * The hcd was added successfully but its probe function failed
2419 * for some reason.
2420 */
2421 retval = -EINVAL;
2422 goto err_add_udc;
2423 }
Alan Sterna89a2cd2008-04-07 15:03:25 -04002424 retval = platform_device_add(the_udc_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002425 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002426 goto err_add_udc;
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002427 if (!platform_get_drvdata(the_udc_pdev)) {
2428 /*
2429 * The udc was added successfully but its probe function failed
2430 * for some reason.
2431 */
2432 retval = -EINVAL;
2433 goto err_probe_udc;
2434 }
Alan Sternd9b76252005-05-03 16:15:43 -04002435 return retval;
2436
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002437err_probe_udc:
2438 platform_device_del(the_udc_pdev);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002439err_add_udc:
2440 platform_device_del(the_hcd_pdev);
2441err_add_hcd:
2442 platform_driver_unregister(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002443err_register_udc_driver:
Alan Sterna89a2cd2008-04-07 15:03:25 -04002444 platform_driver_unregister(&dummy_hcd_driver);
2445err_register_hcd_driver:
2446 platform_device_put(the_udc_pdev);
2447err_alloc_udc:
2448 platform_device_put(the_hcd_pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 return retval;
2450}
2451module_init (init);
2452
2453static void __exit cleanup (void)
2454{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002455 platform_device_unregister(the_udc_pdev);
2456 platform_device_unregister(the_hcd_pdev);
2457 platform_driver_unregister(&dummy_udc_driver);
2458 platform_driver_unregister(&dummy_hcd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459}
2460module_exit (cleanup);