blob: e57989ccc0e56100cf1f889d54f788df9b919d12 [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>
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +010042#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <asm/byteorder.h>
45#include <asm/io.h>
46#include <asm/irq.h>
47#include <asm/system.h>
48#include <asm/unaligned.h>
49
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define DRIVER_DESC "USB Host+Gadget Emulator"
Alan Stern391eca92005-05-10 15:34:16 -040052#define DRIVER_VERSION "02 May 2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Alan Sterncaf29f62007-12-06 11:10:39 -050054#define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static const char driver_name [] = "dummy_hcd";
57static const char driver_desc [] = "USB Host+Gadget Emulator";
58
59static const char gadget_name [] = "dummy_udc";
60
61MODULE_DESCRIPTION (DRIVER_DESC);
62MODULE_AUTHOR ("David Brownell");
63MODULE_LICENSE ("GPL");
64
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +030065struct dummy_hcd_module_parameters {
66 bool is_super_speed;
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +030067 bool is_high_speed;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +030068};
69
70static struct dummy_hcd_module_parameters mod_data = {
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +030071 .is_super_speed = false,
72 .is_high_speed = true,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +030073};
74module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
75MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +030076module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
77MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/*-------------------------------------------------------------------------*/
79
80/* gadget side driver data structres */
81struct dummy_ep {
82 struct list_head queue;
83 unsigned long last_io; /* jiffies timestamp */
84 struct usb_gadget *gadget;
85 const struct usb_endpoint_descriptor *desc;
86 struct usb_ep ep;
87 unsigned halted : 1;
Alan Stern851a5262008-08-14 15:48:30 -040088 unsigned wedged : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 unsigned already_seen : 1;
90 unsigned setup_stage : 1;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +010091 unsigned stream_en:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092};
93
94struct dummy_request {
95 struct list_head queue; /* ep's requests */
96 struct usb_request req;
97};
98
99static inline struct dummy_ep *usb_ep_to_dummy_ep (struct usb_ep *_ep)
100{
101 return container_of (_ep, struct dummy_ep, ep);
102}
103
104static inline struct dummy_request *usb_request_to_dummy_request
105 (struct usb_request *_req)
106{
107 return container_of (_req, struct dummy_request, req);
108}
109
110/*-------------------------------------------------------------------------*/
111
112/*
113 * Every device has ep0 for control requests, plus up to 30 more endpoints,
114 * in one of two types:
115 *
116 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
117 * number can be changed. Names like "ep-a" are used for this type.
118 *
119 * - Fixed Function: in other cases. some characteristics may be mutable;
120 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
121 *
122 * Gadget drivers are responsible for not setting up conflicting endpoint
123 * configurations, illegal or unsupported packet lengths, and so on.
124 */
125
126static const char ep0name [] = "ep0";
127
128static const char *const ep_name [] = {
129 ep0name, /* everyone has ep0 */
130
131 /* act like a net2280: high speed, six configurable endpoints */
132 "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
133
134 /* or like pxa250: fifteen fixed function endpoints */
135 "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
136 "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
137 "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
138 "ep15in-int",
139
140 /* or like sa1100: two fixed function endpoints */
141 "ep1out-bulk", "ep2in-bulk",
142};
Tobias Klauser52950ed2005-12-11 16:20:08 +0100143#define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Alan Sternd9b76252005-05-03 16:15:43 -0400145/*-------------------------------------------------------------------------*/
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#define FIFO_SIZE 64
148
149struct urbp {
150 struct urb *urb;
151 struct list_head urbp_list;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +0100152 struct sg_mapping_iter miter;
153 u32 miter_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155
Alan Stern391eca92005-05-10 15:34:16 -0400156
157enum dummy_rh_state {
158 DUMMY_RH_RESET,
159 DUMMY_RH_SUSPENDED,
160 DUMMY_RH_RUNNING
161};
162
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300163struct dummy_hcd {
164 struct dummy *dum;
165 enum dummy_rh_state rh_state;
166 struct timer_list timer;
167 u32 port_status;
168 u32 old_status;
169 unsigned long re_timeout;
170
171 struct usb_device *udev;
172 struct list_head urbp_list;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100173 u32 stream_en_ep;
174 u8 num_stream[30 / 2];
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300175
176 unsigned active:1;
177 unsigned old_active:1;
178 unsigned resuming:1;
179};
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181struct dummy {
182 spinlock_t lock;
183
184 /*
185 * SLAVE/GADGET side support
186 */
187 struct dummy_ep ep [DUMMY_ENDPOINTS];
188 int address;
189 struct usb_gadget gadget;
190 struct usb_gadget_driver *driver;
191 struct dummy_request fifo_req;
192 u8 fifo_buf [FIFO_SIZE];
193 u16 devstatus;
Alan Stern391eca92005-05-10 15:34:16 -0400194 unsigned udc_suspended:1;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400195 unsigned pullup:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 /*
198 * MASTER/HOST side support
199 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300200 struct dummy_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300201 struct dummy_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202};
203
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300204static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300206 return (struct dummy_hcd *) (hcd->hcd_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300209static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 return container_of((void *) dum, struct usb_hcd, hcd_priv);
212}
213
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300214static inline struct device *dummy_dev(struct dummy_hcd *dum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300216 return dummy_hcd_to_hcd(dum)->self.controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Alan Sternd9b76252005-05-03 16:15:43 -0400219static inline struct device *udc_dev (struct dummy *dum)
220{
221 return dum->gadget.dev.parent;
222}
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
225{
226 return container_of (ep->gadget, struct dummy, gadget);
227}
228
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300229static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300231 struct dummy *dum = container_of(gadget, struct dummy, gadget);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300232 if (dum->gadget.speed == USB_SPEED_SUPER)
233 return dum->ss_hcd;
234 else
235 return dum->hs_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
239{
240 return container_of (dev, struct dummy, gadget.dev);
241}
242
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300243static struct dummy the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245/*-------------------------------------------------------------------------*/
246
Alan Sternf1c39fa2005-05-03 16:24:04 -0400247/* SLAVE/GADGET SIDE UTILITY ROUTINES */
248
249/* called with spinlock held */
250static void nuke (struct dummy *dum, struct dummy_ep *ep)
251{
252 while (!list_empty (&ep->queue)) {
253 struct dummy_request *req;
254
255 req = list_entry (ep->queue.next, struct dummy_request, queue);
256 list_del_init (&req->queue);
257 req->req.status = -ESHUTDOWN;
258
259 spin_unlock (&dum->lock);
260 req->req.complete (&ep->ep, &req->req);
261 spin_lock (&dum->lock);
262 }
263}
264
265/* caller must hold lock */
266static void
267stop_activity (struct dummy *dum)
268{
269 struct dummy_ep *ep;
270
271 /* prevent any more requests */
272 dum->address = 0;
273
274 /* The timer is left running so that outstanding URBs can fail */
275
276 /* nuke any pending requests first, so driver i/o is quiesced */
277 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
278 nuke (dum, ep);
279
280 /* driver now does any non-usb quiescing necessary */
281}
282
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300283/**
284 * set_link_state_by_speed() - Sets the current state of the link according to
285 * the hcd speed
286 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
287 *
288 * This function updates the port_status according to the link state and the
289 * speed of the hcd.
290 */
291static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
292{
293 struct dummy *dum = dum_hcd->dum;
294
295 if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
296 if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
297 dum_hcd->port_status = 0;
298 } else if (!dum->pullup || dum->udc_suspended) {
299 /* UDC suspend must cause a disconnect */
300 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
301 USB_PORT_STAT_ENABLE);
302 if ((dum_hcd->old_status &
303 USB_PORT_STAT_CONNECTION) != 0)
304 dum_hcd->port_status |=
305 (USB_PORT_STAT_C_CONNECTION << 16);
306 } else {
307 /* device is connected and not suspended */
308 dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
309 USB_PORT_STAT_SPEED_5GBPS) ;
310 if ((dum_hcd->old_status &
311 USB_PORT_STAT_CONNECTION) == 0)
312 dum_hcd->port_status |=
313 (USB_PORT_STAT_C_CONNECTION << 16);
314 if ((dum_hcd->port_status &
315 USB_PORT_STAT_ENABLE) == 1 &&
316 (dum_hcd->port_status &
317 USB_SS_PORT_LS_U0) == 1 &&
318 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
319 dum_hcd->active = 1;
320 }
321 } else {
322 if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
323 dum_hcd->port_status = 0;
324 } else if (!dum->pullup || dum->udc_suspended) {
325 /* UDC suspend must cause a disconnect */
326 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
327 USB_PORT_STAT_ENABLE |
328 USB_PORT_STAT_LOW_SPEED |
329 USB_PORT_STAT_HIGH_SPEED |
330 USB_PORT_STAT_SUSPEND);
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 } else {
336 dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
337 if ((dum_hcd->old_status &
338 USB_PORT_STAT_CONNECTION) == 0)
339 dum_hcd->port_status |=
340 (USB_PORT_STAT_C_CONNECTION << 16);
341 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
342 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
343 else if ((dum_hcd->port_status &
344 USB_PORT_STAT_SUSPEND) == 0 &&
345 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
346 dum_hcd->active = 1;
347 }
348 }
349}
350
Alan Sternf1c39fa2005-05-03 16:24:04 -0400351/* caller must hold lock */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300352static void set_link_state(struct dummy_hcd *dum_hcd)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400353{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300354 struct dummy *dum = dum_hcd->dum;
355
356 dum_hcd->active = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300357 if (dum->pullup)
358 if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
359 dum->gadget.speed != USB_SPEED_SUPER) ||
360 (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
361 dum->gadget.speed == USB_SPEED_SUPER))
362 return;
Alan Stern391eca92005-05-10 15:34:16 -0400363
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300364 set_link_state_by_speed(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400365
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300366 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
367 dum_hcd->active)
368 dum_hcd->resuming = 0;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400369
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300370 /* if !connected or reset */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300371 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
372 (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300373 /*
374 * We're connected and not reset (reset occurred now),
375 * and driver attached - disconnect!
376 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300377 if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300378 (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
379 dum->driver) {
380 stop_activity(dum);
381 spin_unlock(&dum->lock);
382 dum->driver->disconnect(&dum->gadget);
383 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400384 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300385 } else if (dum_hcd->active != dum_hcd->old_active) {
386 if (dum_hcd->old_active && dum->driver->suspend) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300387 spin_unlock(&dum->lock);
388 dum->driver->suspend(&dum->gadget);
389 spin_lock(&dum->lock);
390 } else if (!dum_hcd->old_active && dum->driver->resume) {
391 spin_unlock(&dum->lock);
392 dum->driver->resume(&dum->gadget);
393 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400394 }
395 }
396
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300397 dum_hcd->old_status = dum_hcd->port_status;
398 dum_hcd->old_active = dum_hcd->active;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400399}
400
401/*-------------------------------------------------------------------------*/
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403/* SLAVE/GADGET SIDE DRIVER
404 *
405 * This only tracks gadget state. All the work is done when the host
406 * side tries some (emulated) i/o operation. Real device controller
407 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
408 */
409
410#define is_enabled(dum) \
411 (dum->port_status & USB_PORT_STAT_ENABLE)
412
413static int
414dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
415{
416 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300417 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 struct dummy_ep *ep;
419 unsigned max;
420 int retval;
421
422 ep = usb_ep_to_dummy_ep (_ep);
423 if (!_ep || !desc || ep->desc || _ep->name == ep0name
424 || desc->bDescriptorType != USB_DT_ENDPOINT)
425 return -EINVAL;
426 dum = ep_to_dummy (ep);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300427 if (!dum->driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return -ESHUTDOWN;
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200429
430 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300431 if (!is_enabled(dum_hcd))
432 return -ESHUTDOWN;
433
434 /*
435 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
436 * maximum packet size.
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300437 * For SS devices the wMaxPacketSize is limited by 1024.
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300438 */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700439 max = usb_endpoint_maxp(desc) & 0x7ff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 /* drivers must not request bad settings, since lower levels
442 * (hardware or its drivers) may not check. some endpoints
443 * can't do iso, many have maxpacket limitations, etc.
444 *
445 * since this "hardware" driver is here to help debugging, we
446 * have some extra sanity checks. (there could be more though,
447 * especially for "ep9out" style fixed function ones.)
448 */
449 retval = -EINVAL;
450 switch (desc->bmAttributes & 0x03) {
451 case USB_ENDPOINT_XFER_BULK:
452 if (strstr (ep->ep.name, "-iso")
453 || strstr (ep->ep.name, "-int")) {
454 goto done;
455 }
456 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300457 case USB_SPEED_SUPER:
458 if (max == 1024)
459 break;
460 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 case USB_SPEED_HIGH:
462 if (max == 512)
463 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700464 goto done;
465 case USB_SPEED_FULL:
466 if (max == 8 || max == 16 || max == 32 || max == 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /* we'll fake any legal size */
468 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700469 /* save a return statement */
470 default:
471 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473 break;
474 case USB_ENDPOINT_XFER_INT:
475 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
476 goto done;
477 /* real hardware might not handle all packet sizes */
478 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300479 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 case USB_SPEED_HIGH:
481 if (max <= 1024)
482 break;
483 /* save a return statement */
484 case USB_SPEED_FULL:
485 if (max <= 64)
486 break;
487 /* save a return statement */
488 default:
489 if (max <= 8)
490 break;
491 goto done;
492 }
493 break;
494 case USB_ENDPOINT_XFER_ISOC:
495 if (strstr (ep->ep.name, "-bulk")
496 || strstr (ep->ep.name, "-int"))
497 goto done;
498 /* real hardware might not handle all packet sizes */
499 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300500 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 case USB_SPEED_HIGH:
502 if (max <= 1024)
503 break;
504 /* save a return statement */
505 case USB_SPEED_FULL:
506 if (max <= 1023)
507 break;
508 /* save a return statement */
509 default:
510 goto done;
511 }
512 break;
513 default:
514 /* few chips support control except on ep0 */
515 goto done;
516 }
517
518 _ep->maxpacket = max;
519 ep->desc = desc;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100520 if (usb_ss_max_streams(_ep->comp_desc)) {
521 if (!usb_endpoint_xfer_bulk(desc)) {
522 dev_err(udc_dev(dum), "Can't enable stream support on "
523 "non-bulk ep %s\n", _ep->name);
524 return -EINVAL;
525 }
526 ep->stream_en = 1;
527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100529 dev_dbg(udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 _ep->name,
531 desc->bEndpointAddress & 0x0f,
532 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
533 ({ char *val;
534 switch (desc->bmAttributes & 0x03) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +0300535 case USB_ENDPOINT_XFER_BULK:
536 val = "bulk";
537 break;
538 case USB_ENDPOINT_XFER_ISOC:
539 val = "iso";
540 break;
541 case USB_ENDPOINT_XFER_INT:
542 val = "intr";
543 break;
544 default:
545 val = "ctrl";
546 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }; val; }),
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100548 max, ep->stream_en ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /* at this point real hardware should be NAKing transfers
551 * to that endpoint, until a buffer is queued to it.
552 */
Alan Stern851a5262008-08-14 15:48:30 -0400553 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 retval = 0;
555done:
556 return retval;
557}
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559static int dummy_disable (struct usb_ep *_ep)
560{
561 struct dummy_ep *ep;
562 struct dummy *dum;
563 unsigned long flags;
564 int retval;
565
566 ep = usb_ep_to_dummy_ep (_ep);
567 if (!_ep || !ep->desc || _ep->name == ep0name)
568 return -EINVAL;
569 dum = ep_to_dummy (ep);
570
571 spin_lock_irqsave (&dum->lock, flags);
572 ep->desc = NULL;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100573 ep->stream_en = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 retval = 0;
575 nuke (dum, ep);
576 spin_unlock_irqrestore (&dum->lock, flags);
577
Alan Sternd9b76252005-05-03 16:15:43 -0400578 dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return retval;
580}
581
582static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -0400583dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 struct dummy_ep *ep;
586 struct dummy_request *req;
587
588 if (!_ep)
589 return NULL;
590 ep = usb_ep_to_dummy_ep (_ep);
591
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800592 req = kzalloc(sizeof(*req), mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (!req)
594 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 INIT_LIST_HEAD (&req->queue);
596 return &req->req;
597}
598
599static void
600dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
601{
602 struct dummy_ep *ep;
603 struct dummy_request *req;
604
605 ep = usb_ep_to_dummy_ep (_ep);
606 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
607 return;
608
609 req = usb_request_to_dummy_request (_req);
610 WARN_ON (!list_empty (&req->queue));
611 kfree (req);
612}
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614static void
615fifo_complete (struct usb_ep *ep, struct usb_request *req)
616{
617}
618
619static int
Olav Kongas5db539e2005-06-23 20:25:36 +0300620dummy_queue (struct usb_ep *_ep, struct usb_request *_req,
Al Viro55016f12005-10-21 03:21:58 -0400621 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 struct dummy_ep *ep;
624 struct dummy_request *req;
625 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300626 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 unsigned long flags;
628
629 req = usb_request_to_dummy_request (_req);
630 if (!_req || !list_empty (&req->queue) || !_req->complete)
631 return -EINVAL;
632
633 ep = usb_ep_to_dummy_ep (_ep);
634 if (!_ep || (!ep->desc && _ep->name != ep0name))
635 return -EINVAL;
636
637 dum = ep_to_dummy (ep);
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200638 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300639 if (!dum->driver || !is_enabled(dum_hcd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return -ESHUTDOWN;
641
642#if 0
Alan Sternd9b76252005-05-03 16:15:43 -0400643 dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 ep, _req, _ep->name, _req->length, _req->buf);
645#endif
646
647 _req->status = -EINPROGRESS;
648 _req->actual = 0;
649 spin_lock_irqsave (&dum->lock, flags);
650
651 /* implement an emulated single-request FIFO */
652 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
653 list_empty (&dum->fifo_req.queue) &&
654 list_empty (&ep->queue) &&
655 _req->length <= FIFO_SIZE) {
656 req = &dum->fifo_req;
657 req->req = *_req;
658 req->req.buf = dum->fifo_buf;
659 memcpy (dum->fifo_buf, _req->buf, _req->length);
660 req->req.context = dum;
661 req->req.complete = fifo_complete;
662
David Brownellc728df72008-07-26 08:06:24 -0700663 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 spin_unlock (&dum->lock);
665 _req->actual = _req->length;
666 _req->status = 0;
667 _req->complete (_ep, _req);
668 spin_lock (&dum->lock);
David Brownellc728df72008-07-26 08:06:24 -0700669 } else
670 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 spin_unlock_irqrestore (&dum->lock, flags);
672
673 /* real hardware would likely enable transfers here, in case
674 * it'd been left NAKing.
675 */
676 return 0;
677}
678
679static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
680{
681 struct dummy_ep *ep;
682 struct dummy *dum;
683 int retval = -EINVAL;
684 unsigned long flags;
685 struct dummy_request *req = NULL;
686
687 if (!_ep || !_req)
688 return retval;
689 ep = usb_ep_to_dummy_ep (_ep);
690 dum = ep_to_dummy (ep);
691
692 if (!dum->driver)
693 return -ESHUTDOWN;
694
Alan Sternb4dbda12006-07-28 17:07:34 -0400695 local_irq_save (flags);
696 spin_lock (&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 list_for_each_entry (req, &ep->queue, queue) {
698 if (&req->req == _req) {
699 list_del_init (&req->queue);
700 _req->status = -ECONNRESET;
701 retval = 0;
702 break;
703 }
704 }
Alan Sternb4dbda12006-07-28 17:07:34 -0400705 spin_unlock (&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 if (retval == 0) {
Alan Sternd9b76252005-05-03 16:15:43 -0400708 dev_dbg (udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 "dequeued req %p from %s, len %d buf %p\n",
710 req, _ep->name, _req->length, _req->buf);
711 _req->complete (_ep, _req);
712 }
Alan Sternb4dbda12006-07-28 17:07:34 -0400713 local_irq_restore (flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return retval;
715}
716
717static int
Alan Stern851a5262008-08-14 15:48:30 -0400718dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 struct dummy_ep *ep;
721 struct dummy *dum;
722
723 if (!_ep)
724 return -EINVAL;
725 ep = usb_ep_to_dummy_ep (_ep);
726 dum = ep_to_dummy (ep);
727 if (!dum->driver)
728 return -ESHUTDOWN;
729 if (!value)
Alan Stern851a5262008-08-14 15:48:30 -0400730 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
732 !list_empty (&ep->queue))
733 return -EAGAIN;
Alan Stern851a5262008-08-14 15:48:30 -0400734 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 ep->halted = 1;
Alan Stern851a5262008-08-14 15:48:30 -0400736 if (wedged)
737 ep->wedged = 1;
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /* FIXME clear emulated data toggle too */
740 return 0;
741}
742
Alan Stern851a5262008-08-14 15:48:30 -0400743static int
744dummy_set_halt(struct usb_ep *_ep, int value)
745{
746 return dummy_set_halt_and_wedge(_ep, value, 0);
747}
748
749static int dummy_set_wedge(struct usb_ep *_ep)
750{
751 if (!_ep || _ep->name == ep0name)
752 return -EINVAL;
753 return dummy_set_halt_and_wedge(_ep, 1, 1);
754}
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756static const struct usb_ep_ops dummy_ep_ops = {
757 .enable = dummy_enable,
758 .disable = dummy_disable,
759
760 .alloc_request = dummy_alloc_request,
761 .free_request = dummy_free_request,
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 .queue = dummy_queue,
764 .dequeue = dummy_dequeue,
765
766 .set_halt = dummy_set_halt,
Alan Stern851a5262008-08-14 15:48:30 -0400767 .set_wedge = dummy_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768};
769
770/*-------------------------------------------------------------------------*/
771
772/* there are both host and device side versions of this call ... */
773static int dummy_g_get_frame (struct usb_gadget *_gadget)
774{
775 struct timeval tv;
776
777 do_gettimeofday (&tv);
778 return tv.tv_usec / 1000;
779}
780
781static int dummy_wakeup (struct usb_gadget *_gadget)
782{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300783 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300785 dum_hcd = gadget_to_dummy_hcd(_gadget);
786 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
Alan Stern5742b0c2005-05-02 11:25:17 -0400787 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return -EINVAL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300789 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
Alan Stern391eca92005-05-10 15:34:16 -0400790 return -ENOLINK;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300791 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
792 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -0400793 return -EIO;
794
795 /* FIXME: What if the root hub is suspended but the port isn't? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 /* hub notices our request, issues downstream resume, etc */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300798 dum_hcd->resuming = 1;
799 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
800 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return 0;
802}
803
804static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
805{
806 struct dummy *dum;
807
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300808 dum = (gadget_to_dummy_hcd(_gadget))->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (value)
810 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
811 else
812 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
813 return 0;
814}
815
Sebastian Andrzej Siewiord2621272012-01-09 13:14:59 +0100816static void dummy_udc_update_ep0(struct dummy *dum)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200817{
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100818 if (dum->gadget.speed == USB_SPEED_SUPER)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200819 dum->ep[0].ep.maxpacket = 9;
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100820 else
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200821 dum->ep[0].ep.maxpacket = 64;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200822}
823
Alan Sternf1c39fa2005-05-03 16:24:04 -0400824static int dummy_pullup (struct usb_gadget *_gadget, int value)
825{
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200826 struct dummy_hcd *dum_hcd;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400827 struct dummy *dum;
828 unsigned long flags;
829
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200830 dum = gadget_dev_to_dummy(&_gadget->dev);
831
832 if (value && dum->driver) {
833 if (mod_data.is_super_speed)
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100834 dum->gadget.speed = dum->driver->max_speed;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200835 else if (mod_data.is_high_speed)
836 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100837 dum->driver->max_speed);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200838 else
839 dum->gadget.speed = USB_SPEED_FULL;
Sebastian Andrzej Siewiord2621272012-01-09 13:14:59 +0100840 dummy_udc_update_ep0(dum);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200841
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100842 if (dum->gadget.speed < dum->driver->max_speed)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200843 dev_dbg(udc_dev(dum), "This device can perform faster"
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100844 " if you connect it to a %s port...\n",
845 usb_speed_string(dum->driver->max_speed));
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200846 }
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200847 dum_hcd = gadget_to_dummy_hcd(_gadget);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200848
Alan Sternf1c39fa2005-05-03 16:24:04 -0400849 spin_lock_irqsave (&dum->lock, flags);
850 dum->pullup = (value != 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200851 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400852 spin_unlock_irqrestore (&dum->lock, flags);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200853
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200854 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Sternf1c39fa2005-05-03 16:24:04 -0400855 return 0;
856}
857
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200858static int dummy_udc_start(struct usb_gadget *g,
859 struct usb_gadget_driver *driver);
860static int dummy_udc_stop(struct usb_gadget *g,
861 struct usb_gadget_driver *driver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863static const struct usb_gadget_ops dummy_ops = {
864 .get_frame = dummy_g_get_frame,
865 .wakeup = dummy_wakeup,
866 .set_selfpowered = dummy_set_selfpowered,
Alan Sternf1c39fa2005-05-03 16:24:04 -0400867 .pullup = dummy_pullup,
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200868 .udc_start = dummy_udc_start,
869 .udc_stop = dummy_udc_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870};
871
872/*-------------------------------------------------------------------------*/
873
874/* "function" sysfs attribute */
875static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400876show_function (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 struct dummy *dum = gadget_dev_to_dummy (dev);
879
880 if (!dum->driver || !dum->driver->function)
881 return 0;
882 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
883}
Alan Sterncc095b02005-05-10 15:28:38 -0400884static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886/*-------------------------------------------------------------------------*/
887
888/*
889 * Driver registration/unregistration.
890 *
891 * This is basically hardware-specific; there's usually only one real USB
892 * device (not host) controller since that's how USB devices are intended
893 * to work. So most implementations of these api calls will rely on the
894 * fact that only one driver will ever bind to the hardware. But curious
895 * hardware can be built with discrete components, so the gadget API doesn't
896 * require that assumption.
897 *
898 * For this emulator, it might be convenient to create a usb slave device
899 * for each driver that registers: just add to a big root hub.
900 */
901
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200902static int dummy_udc_start(struct usb_gadget *g,
903 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200905 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
906 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100908 if (driver->max_speed == USB_SPEED_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return -EINVAL;
910
911 /*
912 * SLAVE side init ... the layer above hardware, which
913 * can't enumerate without help from the driver we're binding.
914 */
Alan Stern5742b0c2005-05-02 11:25:17 -0400915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 dum->devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 dum->driver = driver;
Alan Sternd9b76252005-05-03 16:15:43 -0400919 dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return 0;
922}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200924static int dummy_udc_stop(struct usb_gadget *g,
925 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200927 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
928 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Alan Sternd9b76252005-05-03 16:15:43 -0400930 dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 driver->driver.name);
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 dum->driver = NULL;
Sebastian Andrzej Siewior25427872011-06-16 20:36:55 +0200934
935 dummy_pullup(&dum->gadget, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return 0;
937}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939#undef is_enabled
940
Alan Sternd9b76252005-05-03 16:15:43 -0400941/* The gadget structure is stored inside the hcd structure and will be
942 * released along with it. */
943static void
944dummy_gadget_release (struct device *dev)
945{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300946 return;
Alan Sternd9b76252005-05-03 16:15:43 -0400947}
948
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200949static void init_dummy_udc_hw(struct dummy *dum)
950{
951 int i;
952
953 INIT_LIST_HEAD(&dum->gadget.ep_list);
954 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
955 struct dummy_ep *ep = &dum->ep[i];
956
957 if (!ep_name[i])
958 break;
959 ep->ep.name = ep_name[i];
960 ep->ep.ops = &dummy_ep_ops;
961 list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
962 ep->halted = ep->wedged = ep->already_seen =
963 ep->setup_stage = 0;
964 ep->ep.maxpacket = ~0;
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100965 ep->ep.max_streams = 16;
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200966 ep->last_io = jiffies;
967 ep->gadget = &dum->gadget;
968 ep->desc = NULL;
969 INIT_LIST_HEAD(&ep->queue);
970 }
971
972 dum->gadget.ep0 = &dum->ep[0].ep;
973 list_del_init(&dum->ep[0].ep.ep_list);
974 INIT_LIST_HEAD(&dum->fifo_req.queue);
Sebastian Andrzej Siewiorf8744d42011-06-23 14:26:13 +0200975
976#ifdef CONFIG_USB_OTG
977 dum->gadget.is_otg = 1;
978#endif
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200979}
980
Alan Stern8364d6b2005-11-14 12:16:30 -0500981static int dummy_udc_probe (struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -0400982{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300983 struct dummy *dum = &the_controller;
Alan Sternd9b76252005-05-03 16:15:43 -0400984 int rc;
985
986 dum->gadget.name = gadget_name;
987 dum->gadget.ops = &dummy_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100988 dum->gadget.max_speed = USB_SPEED_SUPER;
Alan Sternd9b76252005-05-03 16:15:43 -0400989
Kay Sievers0031a062008-05-02 06:02:41 +0200990 dev_set_name(&dum->gadget.dev, "gadget");
Alan Stern8364d6b2005-11-14 12:16:30 -0500991 dum->gadget.dev.parent = &pdev->dev;
Alan Sternd9b76252005-05-03 16:15:43 -0400992 dum->gadget.dev.release = dummy_gadget_release;
993 rc = device_register (&dum->gadget.dev);
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530994 if (rc < 0) {
995 put_device(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -0400996 return rc;
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530997 }
Alan Sternd9b76252005-05-03 16:15:43 -0400998
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200999 init_dummy_udc_hw(dum);
1000
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001001 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
1002 if (rc < 0)
1003 goto err_udc;
1004
Alan Sternefd54a32006-09-25 11:55:56 -04001005 rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
1006 if (rc < 0)
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001007 goto err_dev;
1008 platform_set_drvdata(pdev, dum);
1009 return rc;
1010
1011err_dev:
1012 usb_del_gadget_udc(&dum->gadget);
1013err_udc:
1014 device_unregister(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -04001015 return rc;
1016}
1017
Alan Stern8364d6b2005-11-14 12:16:30 -05001018static int dummy_udc_remove (struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -04001019{
Alan Stern8364d6b2005-11-14 12:16:30 -05001020 struct dummy *dum = platform_get_drvdata (pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04001021
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001022 usb_del_gadget_udc(&dum->gadget);
Alan Stern8364d6b2005-11-14 12:16:30 -05001023 platform_set_drvdata (pdev, NULL);
Alan Sternd9b76252005-05-03 16:15:43 -04001024 device_remove_file (&dum->gadget.dev, &dev_attr_function);
1025 device_unregister (&dum->gadget.dev);
1026 return 0;
1027}
1028
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001029static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1030 int suspend)
Alan Stern391eca92005-05-10 15:34:16 -04001031{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001032 spin_lock_irq(&dum->lock);
1033 dum->udc_suspended = suspend;
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001034 set_link_state(dum_hcd);
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001035 spin_unlock_irq(&dum->lock);
1036}
Alan Stern391eca92005-05-10 15:34:16 -04001037
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001038static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1039{
1040 struct dummy *dum = platform_get_drvdata(pdev);
1041 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1042
1043 dev_dbg(&pdev->dev, "%s\n", __func__);
1044 dummy_udc_pm(dum, dum_hcd, 1);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001045 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001046 return 0;
1047}
1048
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001049static int dummy_udc_resume(struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04001050{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001051 struct dummy *dum = platform_get_drvdata(pdev);
1052 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Alan Stern391eca92005-05-10 15:34:16 -04001053
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001054 dev_dbg(&pdev->dev, "%s\n", __func__);
1055 dummy_udc_pm(dum, dum_hcd, 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001056 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001057 return 0;
1058}
1059
Russell King3ae5eae2005-11-09 22:32:44 +00001060static struct platform_driver dummy_udc_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04001061 .probe = dummy_udc_probe,
1062 .remove = dummy_udc_remove,
Alan Stern391eca92005-05-10 15:34:16 -04001063 .suspend = dummy_udc_suspend,
1064 .resume = dummy_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001065 .driver = {
1066 .name = (char *) gadget_name,
1067 .owner = THIS_MODULE,
1068 },
Alan Sternd9b76252005-05-03 16:15:43 -04001069};
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071/*-------------------------------------------------------------------------*/
1072
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001073static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc)
1074{
1075 unsigned int index;
1076
1077 index = usb_endpoint_num(desc) << 1;
1078 if (usb_endpoint_dir_in(desc))
1079 index |= 1;
1080 return index;
1081}
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083/* MASTER/HOST SIDE DRIVER
1084 *
1085 * this uses the hcd framework to hook up to host side drivers.
1086 * its root hub will only have one device, otherwise it acts like
1087 * a normal host controller.
1088 *
1089 * when urbs are queued, they're just stuck on a list that we
1090 * scan in a timer callback. that callback connects writes from
1091 * the host with reads from the device, and so on, based on the
1092 * usb 2.0 rules.
1093 */
1094
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001095static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb)
1096{
1097 const struct usb_endpoint_descriptor *desc = &urb->ep->desc;
1098 u32 index;
1099
1100 if (!usb_endpoint_xfer_bulk(desc))
1101 return 0;
1102
1103 index = dummy_get_ep_idx(desc);
1104 return (1 << index) & dum_hcd->stream_en_ep;
1105}
1106
1107/*
1108 * The max stream number is saved as a nibble so for the 30 possible endpoints
1109 * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
1110 * means we use only 1 stream). The maximum according to the spec is 16bit so
1111 * if the 16 stream limit is about to go, the array size should be incremented
1112 * to 30 elements of type u16.
1113 */
1114static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1115 unsigned int pipe)
1116{
1117 int max_streams;
1118
1119 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1120 if (usb_pipeout(pipe))
1121 max_streams >>= 4;
1122 else
1123 max_streams &= 0xf;
1124 max_streams++;
1125 return max_streams;
1126}
1127
1128static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1129 unsigned int pipe, unsigned int streams)
1130{
1131 int max_streams;
1132
1133 streams--;
1134 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1135 if (usb_pipeout(pipe)) {
1136 streams <<= 4;
1137 max_streams &= 0xf;
1138 } else {
1139 max_streams &= 0xf0;
1140 }
1141 max_streams |= streams;
1142 dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams;
1143}
1144
1145static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb)
1146{
1147 unsigned int max_streams;
1148 int enabled;
1149
1150 enabled = dummy_ep_stream_en(dum_hcd, urb);
1151 if (!urb->stream_id) {
1152 if (enabled)
1153 return -EINVAL;
1154 return 0;
1155 }
1156 if (!enabled)
1157 return -EINVAL;
1158
1159 max_streams = get_max_streams_for_pipe(dum_hcd,
1160 usb_pipeendpoint(urb->pipe));
1161 if (urb->stream_id > max_streams) {
1162 dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n",
1163 urb->stream_id);
1164 BUG();
1165 return -EINVAL;
1166 }
1167 return 0;
1168}
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170static int dummy_urb_enqueue (
1171 struct usb_hcd *hcd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001173 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001175 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 struct urbp *urbp;
1177 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001178 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 urbp = kmalloc (sizeof *urbp, mem_flags);
1181 if (!urbp)
1182 return -ENOMEM;
1183 urbp->urb = urb;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001184 urbp->miter_started = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001186 dum_hcd = hcd_to_dummy_hcd(hcd);
1187 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001188
1189 rc = dummy_validate_stream(dum_hcd, urb);
1190 if (rc) {
1191 kfree(urbp);
1192 goto done;
1193 }
1194
Alan Sterne9df41c2007-08-08 11:48:02 -04001195 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1196 if (rc) {
1197 kfree(urbp);
1198 goto done;
1199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001201 if (!dum_hcd->udev) {
1202 dum_hcd->udev = urb->dev;
1203 usb_get_dev(dum_hcd->udev);
1204 } else if (unlikely(dum_hcd->udev != urb->dev))
1205 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001207 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 urb->hcpriv = urbp;
1209 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
1210 urb->error_count = 1; /* mark as a new urb */
1211
1212 /* kick the scheduler, it'll do the rest */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001213 if (!timer_pending(&dum_hcd->timer))
1214 mod_timer(&dum_hcd->timer, jiffies + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Alan Sterne9df41c2007-08-08 11:48:02 -04001216 done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001217 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001218 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
Alan Sterne9df41c2007-08-08 11:48:02 -04001221static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001223 struct dummy_hcd *dum_hcd;
Alan Stern391eca92005-05-10 15:34:16 -04001224 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001225 int rc;
Alan Stern391eca92005-05-10 15:34:16 -04001226
1227 /* giveback happens automatically in timer callback,
1228 * so make sure the callback happens */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001229 dum_hcd = hcd_to_dummy_hcd(hcd);
1230 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001231
1232 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001233 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1234 !list_empty(&dum_hcd->urbp_list))
1235 mod_timer(&dum_hcd->timer, jiffies);
Alan Sterne9df41c2007-08-08 11:48:02 -04001236
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001237 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001238 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001241static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
1242 u32 len)
1243{
1244 void *ubuf, *rbuf;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001245 struct urbp *urbp = urb->hcpriv;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001246 int to_host;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001247 struct sg_mapping_iter *miter = &urbp->miter;
1248 u32 trans = 0;
1249 u32 this_sg;
1250 bool next_sg;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001251
1252 to_host = usb_pipein(urb->pipe);
1253 rbuf = req->req.buf + req->req.actual;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001254
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001255 if (!urb->num_sgs) {
1256 ubuf = urb->transfer_buffer + urb->actual_length;
1257 if (to_host)
1258 memcpy(ubuf, rbuf, len);
1259 else
1260 memcpy(rbuf, ubuf, len);
1261 return len;
1262 }
1263
1264 if (!urbp->miter_started) {
1265 u32 flags = SG_MITER_ATOMIC;
1266
1267 if (to_host)
1268 flags |= SG_MITER_TO_SG;
1269 else
1270 flags |= SG_MITER_FROM_SG;
1271
1272 sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
1273 urbp->miter_started = 1;
1274 }
1275 next_sg = sg_miter_next(miter);
1276 if (next_sg == false) {
1277 WARN_ON_ONCE(1);
1278 return -EINVAL;
1279 }
1280 do {
1281 ubuf = miter->addr;
1282 this_sg = min_t(u32, len, miter->length);
1283 miter->consumed = this_sg;
1284 trans += this_sg;
1285
1286 if (to_host)
1287 memcpy(ubuf, rbuf, this_sg);
1288 else
1289 memcpy(rbuf, ubuf, this_sg);
1290 len -= this_sg;
1291
1292 if (!len)
1293 break;
1294 next_sg = sg_miter_next(miter);
1295 if (next_sg == false) {
1296 WARN_ON_ONCE(1);
1297 return -EINVAL;
1298 }
1299
1300 rbuf += this_sg;
1301 } while (1);
1302
1303 sg_miter_stop(miter);
1304 return trans;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001305}
1306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307/* transfer up to a frame's worth; caller must own lock */
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001308static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb,
1309 struct dummy_ep *ep, int limit, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001311 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 struct dummy_request *req;
1313
1314top:
1315 /* if there's no request queued, the device is NAKing; return */
1316 list_for_each_entry (req, &ep->queue, queue) {
1317 unsigned host_len, dev_len, len;
1318 int is_short, to_host;
1319 int rescan = 0;
1320
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001321 if (dummy_ep_stream_en(dum_hcd, urb)) {
1322 if ((urb->stream_id != req->req.stream_id))
1323 continue;
1324 }
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 /* 1..N packets of ep->ep.maxpacket each ... the last one
1327 * may be short (including zero length).
1328 *
1329 * writer can send a zlp explicitly (length 0) or implicitly
1330 * (length mod maxpacket zero, and 'zero' flag); they always
1331 * terminate reads.
1332 */
1333 host_len = urb->transfer_buffer_length - urb->actual_length;
1334 dev_len = req->req.length - req->req.actual;
1335 len = min (host_len, dev_len);
1336
1337 /* FIXME update emulated data toggle too */
1338
1339 to_host = usb_pipein (urb->pipe);
1340 if (unlikely (len == 0))
1341 is_short = 1;
1342 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 /* not enough bandwidth left? */
1344 if (limit < ep->ep.maxpacket && limit < len)
1345 break;
1346 len = min (len, (unsigned) limit);
1347 if (len == 0)
1348 break;
1349
1350 /* use an extra pass for the final short packet */
1351 if (len > ep->ep.maxpacket) {
1352 rescan = 1;
1353 len -= (len % ep->ep.maxpacket);
1354 }
1355 is_short = (len % ep->ep.maxpacket) != 0;
1356
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001357 len = dummy_perform_transfer(urb, req, len);
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 ep->last_io = jiffies;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001360 if (len < 0) {
1361 req->req.status = len;
1362 } else {
1363 limit -= len;
1364 urb->actual_length += len;
1365 req->req.actual += len;
1366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368
1369 /* short packets terminate, maybe with overflow/underflow.
1370 * it's only really an error to write too much.
1371 *
1372 * partially filling a buffer optionally blocks queue advances
1373 * (so completion handlers can clean up the queue) but we don't
Alan Sternb0d9efb2007-08-21 15:39:21 -04001374 * need to emulate such data-in-flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 */
1376 if (is_short) {
1377 if (host_len == dev_len) {
1378 req->req.status = 0;
Alan Stern4d2f1102007-08-24 15:40:10 -04001379 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 } else if (to_host) {
1381 req->req.status = 0;
1382 if (dev_len > host_len)
Alan Stern4d2f1102007-08-24 15:40:10 -04001383 *status = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 else
Alan Stern4d2f1102007-08-24 15:40:10 -04001385 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 } else if (!to_host) {
Alan Stern4d2f1102007-08-24 15:40:10 -04001387 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (host_len > dev_len)
1389 req->req.status = -EOVERFLOW;
1390 else
1391 req->req.status = 0;
1392 }
1393
1394 /* many requests terminate without a short packet */
1395 } else {
1396 if (req->req.length == req->req.actual
1397 && !req->req.zero)
1398 req->req.status = 0;
1399 if (urb->transfer_buffer_length == urb->actual_length
1400 && !(urb->transfer_flags
Alan Stern4d2f1102007-08-24 15:40:10 -04001401 & URB_ZERO_PACKET))
1402 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404
1405 /* device side completion --> continuable */
1406 if (req->req.status != -EINPROGRESS) {
1407 list_del_init (&req->queue);
1408
1409 spin_unlock (&dum->lock);
1410 req->req.complete (&ep->ep, &req->req);
1411 spin_lock (&dum->lock);
1412
1413 /* requests might have been unlinked... */
1414 rescan = 1;
1415 }
1416
1417 /* host side completion --> terminate */
Alan Stern4d2f1102007-08-24 15:40:10 -04001418 if (*status != -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 break;
1420
1421 /* rescan to continue with any other queued i/o */
1422 if (rescan)
1423 goto top;
1424 }
1425 return limit;
1426}
1427
1428static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1429{
1430 int limit = ep->ep.maxpacket;
1431
1432 if (dum->gadget.speed == USB_SPEED_HIGH) {
1433 int tmp;
1434
1435 /* high bandwidth mode */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001436 tmp = usb_endpoint_maxp(ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 tmp = (tmp >> 11) & 0x03;
1438 tmp *= 8 /* applies to entire frame */;
1439 limit += limit * tmp;
1440 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001441 if (dum->gadget.speed == USB_SPEED_SUPER) {
1442 switch (ep->desc->bmAttributes & 0x03) {
1443 case USB_ENDPOINT_XFER_ISOC:
1444 /* Sec. 4.4.8.2 USB3.0 Spec */
1445 limit = 3 * 16 * 1024 * 8;
1446 break;
1447 case USB_ENDPOINT_XFER_INT:
1448 /* Sec. 4.4.7.2 USB3.0 Spec */
1449 limit = 3 * 1024 * 8;
1450 break;
1451 case USB_ENDPOINT_XFER_BULK:
1452 default:
1453 break;
1454 }
1455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return limit;
1457}
1458
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001459#define is_active(dum_hcd) ((dum_hcd->port_status & \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1461 USB_PORT_STAT_SUSPEND)) \
1462 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1463
1464static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1465{
1466 int i;
1467
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001468 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1469 dum->ss_hcd : dum->hs_hcd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return NULL;
1471 if ((address & ~USB_DIR_IN) == 0)
1472 return &dum->ep [0];
1473 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1474 struct dummy_ep *ep = &dum->ep [i];
1475
1476 if (!ep->desc)
1477 continue;
1478 if (ep->desc->bEndpointAddress == address)
1479 return ep;
1480 }
1481 return NULL;
1482}
1483
1484#undef is_active
1485
1486#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1487#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1488#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1489#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1490#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1491#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1492
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001493
1494/**
1495 * handle_control_request() - handles all control transfers
1496 * @dum: pointer to dummy (the_controller)
1497 * @urb: the urb request to handle
1498 * @setup: pointer to the setup data for a USB device control
1499 * request
1500 * @status: pointer to request handling status
1501 *
1502 * Return 0 - if the request was handled
1503 * 1 - if the request wasn't handles
1504 * error code on error
1505 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001506static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001507 struct usb_ctrlrequest *setup,
1508 int *status)
1509{
1510 struct dummy_ep *ep2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001511 struct dummy *dum = dum_hcd->dum;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001512 int ret_val = 1;
1513 unsigned w_index;
1514 unsigned w_value;
1515
1516 w_index = le16_to_cpu(setup->wIndex);
1517 w_value = le16_to_cpu(setup->wValue);
1518 switch (setup->bRequest) {
1519 case USB_REQ_SET_ADDRESS:
1520 if (setup->bRequestType != Dev_Request)
1521 break;
1522 dum->address = w_value;
1523 *status = 0;
1524 dev_dbg(udc_dev(dum), "set_address = %d\n",
1525 w_value);
1526 ret_val = 0;
1527 break;
1528 case USB_REQ_SET_FEATURE:
1529 if (setup->bRequestType == Dev_Request) {
1530 ret_val = 0;
1531 switch (w_value) {
1532 case USB_DEVICE_REMOTE_WAKEUP:
1533 break;
1534 case USB_DEVICE_B_HNP_ENABLE:
1535 dum->gadget.b_hnp_enable = 1;
1536 break;
1537 case USB_DEVICE_A_HNP_SUPPORT:
1538 dum->gadget.a_hnp_support = 1;
1539 break;
1540 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1541 dum->gadget.a_alt_hnp_support = 1;
1542 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001543 case USB_DEVICE_U1_ENABLE:
1544 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1545 HCD_USB3)
1546 w_value = USB_DEV_STAT_U1_ENABLED;
1547 else
1548 ret_val = -EOPNOTSUPP;
1549 break;
1550 case USB_DEVICE_U2_ENABLE:
1551 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1552 HCD_USB3)
1553 w_value = USB_DEV_STAT_U2_ENABLED;
1554 else
1555 ret_val = -EOPNOTSUPP;
1556 break;
1557 case USB_DEVICE_LTM_ENABLE:
1558 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1559 HCD_USB3)
1560 w_value = USB_DEV_STAT_LTM_ENABLED;
1561 else
1562 ret_val = -EOPNOTSUPP;
1563 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001564 default:
1565 ret_val = -EOPNOTSUPP;
1566 }
1567 if (ret_val == 0) {
1568 dum->devstatus |= (1 << w_value);
1569 *status = 0;
1570 }
1571 } else if (setup->bRequestType == Ep_Request) {
1572 /* endpoint halt */
1573 ep2 = find_endpoint(dum, w_index);
1574 if (!ep2 || ep2->ep.name == ep0name) {
1575 ret_val = -EOPNOTSUPP;
1576 break;
1577 }
1578 ep2->halted = 1;
1579 ret_val = 0;
1580 *status = 0;
1581 }
1582 break;
1583 case USB_REQ_CLEAR_FEATURE:
1584 if (setup->bRequestType == Dev_Request) {
1585 ret_val = 0;
1586 switch (w_value) {
1587 case USB_DEVICE_REMOTE_WAKEUP:
1588 w_value = USB_DEVICE_REMOTE_WAKEUP;
1589 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001590 case USB_DEVICE_U1_ENABLE:
1591 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1592 HCD_USB3)
1593 w_value = USB_DEV_STAT_U1_ENABLED;
1594 else
1595 ret_val = -EOPNOTSUPP;
1596 break;
1597 case USB_DEVICE_U2_ENABLE:
1598 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1599 HCD_USB3)
1600 w_value = USB_DEV_STAT_U2_ENABLED;
1601 else
1602 ret_val = -EOPNOTSUPP;
1603 break;
1604 case USB_DEVICE_LTM_ENABLE:
1605 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1606 HCD_USB3)
1607 w_value = USB_DEV_STAT_LTM_ENABLED;
1608 else
1609 ret_val = -EOPNOTSUPP;
1610 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001611 default:
1612 ret_val = -EOPNOTSUPP;
1613 break;
1614 }
1615 if (ret_val == 0) {
1616 dum->devstatus &= ~(1 << w_value);
1617 *status = 0;
1618 }
1619 } else if (setup->bRequestType == Ep_Request) {
1620 /* endpoint halt */
1621 ep2 = find_endpoint(dum, w_index);
1622 if (!ep2) {
1623 ret_val = -EOPNOTSUPP;
1624 break;
1625 }
1626 if (!ep2->wedged)
1627 ep2->halted = 0;
1628 ret_val = 0;
1629 *status = 0;
1630 }
1631 break;
1632 case USB_REQ_GET_STATUS:
1633 if (setup->bRequestType == Dev_InRequest
1634 || setup->bRequestType == Intf_InRequest
1635 || setup->bRequestType == Ep_InRequest) {
1636 char *buf;
1637 /*
1638 * device: remote wakeup, selfpowered
1639 * interface: nothing
1640 * endpoint: halt
1641 */
1642 buf = (char *)urb->transfer_buffer;
1643 if (urb->transfer_buffer_length > 0) {
1644 if (setup->bRequestType == Ep_InRequest) {
1645 ep2 = find_endpoint(dum, w_index);
1646 if (!ep2) {
1647 ret_val = -EOPNOTSUPP;
1648 break;
1649 }
1650 buf[0] = ep2->halted;
1651 } else if (setup->bRequestType ==
1652 Dev_InRequest) {
1653 buf[0] = (u8)dum->devstatus;
1654 } else
1655 buf[0] = 0;
1656 }
1657 if (urb->transfer_buffer_length > 1)
1658 buf[1] = 0;
1659 urb->actual_length = min_t(u32, 2,
1660 urb->transfer_buffer_length);
1661 ret_val = 0;
1662 *status = 0;
1663 }
1664 break;
1665 }
1666 return ret_val;
1667}
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669/* drive both sides of the transfers; looks like irq handlers to
1670 * both drivers except the callbacks aren't in_irq().
1671 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001672static void dummy_timer(unsigned long _dum_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001674 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1675 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 struct urbp *urbp, *tmp;
1677 unsigned long flags;
1678 int limit, total;
1679 int i;
1680
1681 /* simplistic model for one frame's bandwidth */
1682 switch (dum->gadget.speed) {
1683 case USB_SPEED_LOW:
1684 total = 8/*bytes*/ * 12/*packets*/;
1685 break;
1686 case USB_SPEED_FULL:
1687 total = 64/*bytes*/ * 19/*packets*/;
1688 break;
1689 case USB_SPEED_HIGH:
1690 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1691 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001692 case USB_SPEED_SUPER:
1693 /* Bus speed is 500000 bytes/ms, so use a little less */
1694 total = 490000;
1695 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001697 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return;
1699 }
1700
1701 /* FIXME if HZ != 1000 this will probably misbehave ... */
1702
1703 /* look at each urb queued by the host side driver */
1704 spin_lock_irqsave (&dum->lock, flags);
1705
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001706 if (!dum_hcd->udev) {
1707 dev_err(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 "timer fired with no URBs pending?\n");
1709 spin_unlock_irqrestore (&dum->lock, flags);
1710 return;
1711 }
1712
1713 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1714 if (!ep_name [i])
1715 break;
1716 dum->ep [i].already_seen = 0;
1717 }
1718
1719restart:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001720 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 struct urb *urb;
1722 struct dummy_request *req;
1723 u8 address;
1724 struct dummy_ep *ep = NULL;
1725 int type;
Alan Stern4d2f1102007-08-24 15:40:10 -04001726 int status = -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 urb = urbp->urb;
Alan Sterneb231052007-08-21 15:40:36 -04001729 if (urb->unlinked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 goto return_urb;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001731 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
Alan Stern391eca92005-05-10 15:34:16 -04001732 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 type = usb_pipetype (urb->pipe);
1734
1735 /* used up this frame's non-periodic bandwidth?
1736 * FIXME there's infinite bandwidth for control and
1737 * periodic transfers ... unrealistic.
1738 */
1739 if (total <= 0 && type == PIPE_BULK)
1740 continue;
1741
1742 /* find the gadget's ep for this request (if configured) */
1743 address = usb_pipeendpoint (urb->pipe);
1744 if (usb_pipein (urb->pipe))
1745 address |= USB_DIR_IN;
1746 ep = find_endpoint(dum, address);
1747 if (!ep) {
1748 /* set_configuration() disagreement */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001749 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 "no ep configured for urb %p\n",
1751 urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001752 status = -EPROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 goto return_urb;
1754 }
1755
1756 if (ep->already_seen)
1757 continue;
1758 ep->already_seen = 1;
1759 if (ep == &dum->ep [0] && urb->error_count) {
1760 ep->setup_stage = 1; /* a new urb */
1761 urb->error_count = 0;
1762 }
1763 if (ep->halted && !ep->setup_stage) {
1764 /* NOTE: must not be iso! */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001765 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 ep->ep.name, urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001767 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 goto return_urb;
1769 }
1770 /* FIXME make sure both ends agree on maxpacket */
1771
1772 /* handle control requests */
1773 if (ep == &dum->ep [0] && ep->setup_stage) {
1774 struct usb_ctrlrequest setup;
1775 int value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 /* paranoia, in case of stale queued data */
1779 list_for_each_entry (req, &ep->queue, queue) {
1780 list_del_init (&req->queue);
1781 req->req.status = -EOVERFLOW;
Alan Sternd9b76252005-05-03 16:15:43 -04001782 dev_dbg (udc_dev(dum), "stale req = %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 req);
1784
1785 spin_unlock (&dum->lock);
1786 req->req.complete (&ep->ep, &req->req);
1787 spin_lock (&dum->lock);
1788 ep->already_seen = 0;
1789 goto restart;
1790 }
1791
1792 /* gadget driver never sees set_address or operations
1793 * on standard feature flags. some hardware doesn't
1794 * even expose them.
1795 */
1796 ep->last_io = jiffies;
1797 ep->setup_stage = 0;
1798 ep->halted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001800 value = handle_control_request(dum_hcd, urb, &setup,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001801 &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 /* gadget driver handles all other requests. block
1804 * until setup() returns; no reentrancy issues etc.
1805 */
1806 if (value > 0) {
1807 spin_unlock (&dum->lock);
1808 value = dum->driver->setup (&dum->gadget,
1809 &setup);
1810 spin_lock (&dum->lock);
1811
1812 if (value >= 0) {
1813 /* no delays (max 64KB data stage) */
1814 limit = 64*1024;
1815 goto treat_control_like_bulk;
1816 }
1817 /* error, see below */
1818 }
1819
1820 if (value < 0) {
1821 if (value != -EOPNOTSUPP)
Alan Sternd9b76252005-05-03 16:15:43 -04001822 dev_dbg (udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 "setup --> %d\n",
1824 value);
Alan Stern4d2f1102007-08-24 15:40:10 -04001825 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 urb->actual_length = 0;
1827 }
1828
1829 goto return_urb;
1830 }
1831
1832 /* non-control requests */
1833 limit = total;
1834 switch (usb_pipetype (urb->pipe)) {
1835 case PIPE_ISOCHRONOUS:
1836 /* FIXME is it urb->interval since the last xfer?
1837 * use urb->iso_frame_desc[i].
1838 * complete whether or not ep has requests queued.
1839 * report random errors, to debug drivers.
1840 */
1841 limit = max (limit, periodic_bytes (dum, ep));
Alan Stern4d2f1102007-08-24 15:40:10 -04001842 status = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 break;
1844
1845 case PIPE_INTERRUPT:
1846 /* FIXME is it urb->interval since the last xfer?
1847 * this almost certainly polls too fast.
1848 */
1849 limit = max (limit, periodic_bytes (dum, ep));
1850 /* FALLTHROUGH */
1851
1852 // case PIPE_BULK: case PIPE_CONTROL:
1853 default:
1854 treat_control_like_bulk:
1855 ep->last_io = jiffies;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001856 total = transfer(dum_hcd, urb, ep, limit, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 break;
1858 }
1859
1860 /* incomplete transfer? */
Alan Stern4d2f1102007-08-24 15:40:10 -04001861 if (status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 continue;
1863
1864return_urb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 list_del (&urbp->urbp_list);
1866 kfree (urbp);
1867 if (ep)
1868 ep->already_seen = ep->setup_stage = 0;
1869
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001870 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 spin_unlock (&dum->lock);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001872 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 spin_lock (&dum->lock);
1874
1875 goto restart;
1876 }
1877
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001878 if (list_empty(&dum_hcd->urbp_list)) {
1879 usb_put_dev(dum_hcd->udev);
1880 dum_hcd->udev = NULL;
1881 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern391eca92005-05-10 15:34:16 -04001882 /* want a 1 msec delay here */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001883 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
1885
1886 spin_unlock_irqrestore (&dum->lock, flags);
1887}
1888
1889/*-------------------------------------------------------------------------*/
1890
1891#define PORT_C_MASK \
Alan Sternc2db8b52005-04-29 16:30:48 -04001892 ((USB_PORT_STAT_C_CONNECTION \
1893 | USB_PORT_STAT_C_ENABLE \
1894 | USB_PORT_STAT_C_SUSPEND \
1895 | USB_PORT_STAT_C_OVERCURRENT \
1896 | USB_PORT_STAT_C_RESET) << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1899{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001900 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 unsigned long flags;
Alan Stern391eca92005-05-10 15:34:16 -04001902 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001904 dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001906 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001907 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001908 goto done;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001909
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001910 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1911 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1912 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1913 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001914 }
1915
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001916 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 *buf = (1 << 1);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001918 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1919 dum_hcd->port_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 retval = 1;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001921 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -04001922 usb_hcd_resume_root_hub (hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 }
Alan Stern391eca92005-05-10 15:34:16 -04001924done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001925 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return retval;
1927}
1928
1929static inline void
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001930ss_hub_descriptor(struct usb_hub_descriptor *desc)
1931{
1932 memset(desc, 0, sizeof *desc);
1933 desc->bDescriptorType = 0x2a;
1934 desc->bDescLength = 12;
1935 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1936 desc->bNbrPorts = 1;
1937 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1938 desc->u.ss.DeviceRemovable = 0xffff;
1939}
1940
1941static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942hub_descriptor (struct usb_hub_descriptor *desc)
1943{
1944 memset (desc, 0, sizeof *desc);
1945 desc->bDescriptorType = 0x29;
1946 desc->bDescLength = 9;
Al Virofd05e722008-04-28 07:00:16 +01001947 desc->wHubCharacteristics = cpu_to_le16(0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 desc->bNbrPorts = 1;
John Youndbe79bb2001-09-17 00:00:00 -07001949 desc->u.hs.DeviceRemovable[0] = 0xff;
1950 desc->u.hs.DeviceRemovable[1] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
1952
1953static int dummy_hub_control (
1954 struct usb_hcd *hcd,
1955 u16 typeReq,
1956 u16 wValue,
1957 u16 wIndex,
1958 char *buf,
1959 u16 wLength
1960) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001961 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 int retval = 0;
1963 unsigned long flags;
1964
Alan Stern541c7d42010-06-22 16:39:10 -04001965 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001966 return -ETIMEDOUT;
1967
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001968 dum_hcd = hcd_to_dummy_hcd(hcd);
1969
1970 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 switch (typeReq) {
1972 case ClearHubFeature:
1973 break;
1974 case ClearPortFeature:
1975 switch (wValue) {
1976 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001977 if (hcd->speed == HCD_USB3) {
1978 dev_dbg(dummy_dev(dum_hcd),
1979 "USB_PORT_FEAT_SUSPEND req not "
1980 "supported for USB 3.0 roothub\n");
1981 goto error;
1982 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001983 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 /* 20msec resume signaling */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001985 dum_hcd->resuming = 1;
1986 dum_hcd->re_timeout = jiffies +
Alan Sternf1c39fa2005-05-03 16:24:04 -04001987 msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
1989 break;
1990 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001991 if (hcd->speed == HCD_USB3) {
1992 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1993 dev_dbg(dummy_dev(dum_hcd),
1994 "power-off\n");
1995 } else
1996 if (dum_hcd->port_status &
1997 USB_SS_PORT_STAT_POWER)
1998 dev_dbg(dummy_dev(dum_hcd),
1999 "power-off\n");
Alan Sternf1c39fa2005-05-03 16:24:04 -04002000 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002002 dum_hcd->port_status &= ~(1 << wValue);
2003 set_link_state(dum_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
2005 break;
2006 case GetHubDescriptor:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002007 if (hcd->speed == HCD_USB3 &&
2008 (wLength < USB_DT_SS_HUB_SIZE ||
2009 wValue != (USB_DT_SS_HUB << 8))) {
2010 dev_dbg(dummy_dev(dum_hcd),
2011 "Wrong hub descriptor type for "
2012 "USB 3.0 roothub.\n");
2013 goto error;
2014 }
2015 if (hcd->speed == HCD_USB3)
2016 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
2017 else
2018 hub_descriptor((struct usb_hub_descriptor *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 break;
2020 case GetHubStatus:
Harvey Harrison551509d2009-02-11 14:11:36 -08002021 *(__le32 *) buf = cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 break;
2023 case GetPortStatus:
2024 if (wIndex != 1)
2025 retval = -EPIPE;
2026
2027 /* whoever resets or resumes must GetPortStatus to
2028 * complete it!!
2029 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002030 if (dum_hcd->resuming &&
2031 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2032 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
2033 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002035 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
2036 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2037 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
2038 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
2039 if (dum_hcd->dum->pullup) {
2040 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002041
2042 if (hcd->speed < HCD_USB3) {
2043 switch (dum_hcd->dum->gadget.speed) {
2044 case USB_SPEED_HIGH:
2045 dum_hcd->port_status |=
2046 USB_PORT_STAT_HIGH_SPEED;
2047 break;
2048 case USB_SPEED_LOW:
2049 dum_hcd->dum->gadget.ep0->
2050 maxpacket = 8;
2051 dum_hcd->port_status |=
2052 USB_PORT_STAT_LOW_SPEED;
2053 break;
2054 default:
2055 dum_hcd->dum->gadget.speed =
2056 USB_SPEED_FULL;
2057 break;
2058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
2060 }
2061 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002062 set_link_state(dum_hcd);
2063 ((__le16 *) buf)[0] = cpu_to_le16 (dum_hcd->port_status);
2064 ((__le16 *) buf)[1] = cpu_to_le16 (dum_hcd->port_status >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 break;
2066 case SetHubFeature:
2067 retval = -EPIPE;
2068 break;
2069 case SetPortFeature:
2070 switch (wValue) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002071 case USB_PORT_FEAT_LINK_STATE:
2072 if (hcd->speed != HCD_USB3) {
2073 dev_dbg(dummy_dev(dum_hcd),
2074 "USB_PORT_FEAT_LINK_STATE req not "
2075 "supported for USB 2.0 roothub\n");
2076 goto error;
2077 }
2078 /*
2079 * Since this is dummy we don't have an actual link so
2080 * there is nothing to do for the SET_LINK_STATE cmd
2081 */
2082 break;
2083 case USB_PORT_FEAT_U1_TIMEOUT:
2084 case USB_PORT_FEAT_U2_TIMEOUT:
2085 /* TODO: add suspend/resume support! */
2086 if (hcd->speed != HCD_USB3) {
2087 dev_dbg(dummy_dev(dum_hcd),
2088 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
2089 "supported for USB 2.0 roothub\n");
2090 goto error;
2091 }
2092 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002094 /* Applicable only for USB2.0 hub */
2095 if (hcd->speed == HCD_USB3) {
2096 dev_dbg(dummy_dev(dum_hcd),
2097 "USB_PORT_FEAT_SUSPEND req not "
2098 "supported for USB 3.0 roothub\n");
2099 goto error;
2100 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002101 if (dum_hcd->active) {
2102 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
Alan Sternf1c39fa2005-05-03 16:24:04 -04002103
2104 /* HNP would happen here; for now we
2105 * assume b_bus_req is always true.
2106 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002107 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002108 if (((1 << USB_DEVICE_B_HNP_ENABLE)
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002109 & dum_hcd->dum->devstatus) != 0)
2110 dev_dbg(dummy_dev(dum_hcd),
Alan Stern5742b0c2005-05-02 11:25:17 -04002111 "no HNP yet!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
2113 break;
Alan Sternf1c39fa2005-05-03 16:24:04 -04002114 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002115 if (hcd->speed == HCD_USB3)
2116 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
2117 else
2118 dum_hcd->port_status |= USB_PORT_STAT_POWER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002119 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002120 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002121 case USB_PORT_FEAT_BH_PORT_RESET:
2122 /* Applicable only for USB3.0 hub */
2123 if (hcd->speed != HCD_USB3) {
2124 dev_dbg(dummy_dev(dum_hcd),
2125 "USB_PORT_FEAT_BH_PORT_RESET req not "
2126 "supported for USB 2.0 roothub\n");
2127 goto error;
2128 }
2129 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 case USB_PORT_FEAT_RESET:
Alan Sternf1c39fa2005-05-03 16:24:04 -04002131 /* if it's already enabled, disable */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002132 if (hcd->speed == HCD_USB3) {
2133 dum_hcd->port_status = 0;
2134 dum_hcd->port_status =
2135 (USB_SS_PORT_STAT_POWER |
2136 USB_PORT_STAT_CONNECTION |
2137 USB_PORT_STAT_RESET);
2138 } else
2139 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
Alan Sternf1c39fa2005-05-03 16:24:04 -04002140 | USB_PORT_STAT_LOW_SPEED
2141 | USB_PORT_STAT_HIGH_SPEED);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002142 /*
2143 * We want to reset device status. All but the
2144 * Self powered feature
2145 */
2146 dum_hcd->dum->devstatus &=
2147 (1 << USB_DEVICE_SELF_POWERED);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002148 /*
2149 * FIXME USB3.0: what is the correct reset signaling
2150 * interval? Is it still 50msec as for HS?
2151 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002152 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002153 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 default:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002155 if (hcd->speed == HCD_USB3) {
2156 if ((dum_hcd->port_status &
2157 USB_SS_PORT_STAT_POWER) != 0) {
2158 dum_hcd->port_status |= (1 << wValue);
2159 set_link_state(dum_hcd);
2160 }
2161 } else
2162 if ((dum_hcd->port_status &
2163 USB_PORT_STAT_POWER) != 0) {
2164 dum_hcd->port_status |= (1 << wValue);
2165 set_link_state(dum_hcd);
2166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
2168 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002169 case GetPortErrorCount:
2170 if (hcd->speed != HCD_USB3) {
2171 dev_dbg(dummy_dev(dum_hcd),
2172 "GetPortErrorCount req not "
2173 "supported for USB 2.0 roothub\n");
2174 goto error;
2175 }
2176 /* We'll always return 0 since this is a dummy hub */
2177 *(__le32 *) buf = cpu_to_le32(0);
2178 break;
2179 case SetHubDepth:
2180 if (hcd->speed != HCD_USB3) {
2181 dev_dbg(dummy_dev(dum_hcd),
2182 "SetHubDepth req not supported for "
2183 "USB 2.0 roothub\n");
2184 goto error;
2185 }
2186 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002188 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 "hub control req%04x v%04x i%04x l%d\n",
2190 typeReq, wValue, wIndex, wLength);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002191error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 /* "protocol stall" on error */
2193 retval = -EPIPE;
2194 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002195 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Stern685eb932005-05-03 16:27:26 -04002196
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002197 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
Alan Stern685eb932005-05-03 16:27:26 -04002198 usb_hcd_poll_rh_status (hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 return retval;
2200}
2201
Alan Stern0c0382e2005-10-13 17:08:02 -04002202static int dummy_bus_suspend (struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002203{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002204 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002205
Harvey Harrison441b62c2008-03-03 16:08:34 -08002206 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern3cf0a222005-11-29 12:08:15 -05002207
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002208 spin_lock_irq(&dum_hcd->dum->lock);
2209 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2210 set_link_state(dum_hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002211 hcd->state = HC_STATE_SUSPENDED;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002212 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern391eca92005-05-10 15:34:16 -04002213 return 0;
2214}
2215
Alan Stern0c0382e2005-10-13 17:08:02 -04002216static int dummy_bus_resume (struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002217{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002218 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002219 int rc = 0;
2220
Harvey Harrison441b62c2008-03-03 16:08:34 -08002221 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002222
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002223 spin_lock_irq(&dum_hcd->dum->lock);
Alan Stern541c7d42010-06-22 16:39:10 -04002224 if (!HCD_HW_ACCESSIBLE(hcd)) {
Alan Sterncfa59da2007-06-21 16:25:35 -04002225 rc = -ESHUTDOWN;
Alan Stern3cf0a222005-11-29 12:08:15 -05002226 } else {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002227 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2228 set_link_state(dum_hcd);
2229 if (!list_empty(&dum_hcd->urbp_list))
2230 mod_timer(&dum_hcd->timer, jiffies);
Alan Stern3cf0a222005-11-29 12:08:15 -05002231 hcd->state = HC_STATE_RUNNING;
2232 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002233 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern3cf0a222005-11-29 12:08:15 -05002234 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002235}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237/*-------------------------------------------------------------------------*/
2238
2239static inline ssize_t
2240show_urb (char *buf, size_t size, struct urb *urb)
2241{
2242 int ep = usb_pipeendpoint (urb->pipe);
2243
2244 return snprintf (buf, size,
2245 "urb/%p %s ep%d%s%s len %d/%d\n",
2246 urb,
2247 ({ char *s;
2248 switch (urb->dev->speed) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002249 case USB_SPEED_LOW:
2250 s = "ls";
2251 break;
2252 case USB_SPEED_FULL:
2253 s = "fs";
2254 break;
2255 case USB_SPEED_HIGH:
2256 s = "hs";
2257 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002258 case USB_SPEED_SUPER:
2259 s = "ss";
2260 break;
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002261 default:
2262 s = "?";
2263 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }; s; }),
2265 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
2266 ({ char *s; \
2267 switch (usb_pipetype (urb->pipe)) { \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002268 case PIPE_CONTROL: \
2269 s = ""; \
2270 break; \
2271 case PIPE_BULK: \
2272 s = "-bulk"; \
2273 break; \
2274 case PIPE_INTERRUPT: \
2275 s = "-int"; \
2276 break; \
2277 default: \
2278 s = "-iso"; \
2279 break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }; s;}),
2281 urb->actual_length, urb->transfer_buffer_length);
2282}
2283
2284static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -04002285show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
2287 struct usb_hcd *hcd = dev_get_drvdata (dev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002288 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 struct urbp *urbp;
2290 size_t size = 0;
2291 unsigned long flags;
2292
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002293 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2294 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 size_t temp;
2296
2297 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
2298 buf += temp;
2299 size += temp;
2300 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002301 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 return size;
2304}
2305static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
2306
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002307static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2308{
2309 init_timer(&dum_hcd->timer);
2310 dum_hcd->timer.function = dummy_timer;
2311 dum_hcd->timer.data = (unsigned long)dum_hcd;
2312 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002313 dum_hcd->stream_en_ep = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002314 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2315 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2316 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2317 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2318#ifdef CONFIG_USB_OTG
2319 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2320#endif
2321 return 0;
2322
2323 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2324 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2325}
2326
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002327static int dummy_start(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002329 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
2331 /*
2332 * MASTER side init ... we emulate a root hub that'll only ever
2333 * talk to one device (the slave side). Also appears in sysfs,
2334 * just like more familiar pci-based HCDs.
2335 */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002336 if (!usb_hcd_is_primary_hcd(hcd))
2337 return dummy_start_ss(dum_hcd);
2338
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002339 spin_lock_init(&dum_hcd->dum->lock);
2340 init_timer(&dum_hcd->timer);
2341 dum_hcd->timer.function = dummy_timer;
2342 dum_hcd->timer.data = (unsigned long)dum_hcd;
2343 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002345 INIT_LIST_HEAD(&dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Alan Sterncaf29f62007-12-06 11:10:39 -05002347 hcd->power_budget = POWER_BUDGET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 hcd->state = HC_STATE_RUNNING;
Alan Stern685eb932005-05-03 16:27:26 -04002349 hcd->uses_new_polling = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Alan Stern5742b0c2005-05-02 11:25:17 -04002351#ifdef CONFIG_USB_OTG
2352 hcd->self.otg_port = 1;
2353#endif
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002356 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357}
2358
2359static void dummy_stop (struct usb_hcd *hcd)
2360{
2361 struct dummy *dum;
2362
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002363 dum = (hcd_to_dummy_hcd(hcd))->dum;
2364 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2365 usb_gadget_unregister_driver(dum->driver);
2366 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
2368
2369/*-------------------------------------------------------------------------*/
2370
2371static int dummy_h_get_frame (struct usb_hcd *hcd)
2372{
2373 return dummy_g_get_frame (NULL);
2374}
2375
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002376static int dummy_setup(struct usb_hcd *hcd)
2377{
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01002378 hcd->self.sg_tablesize = ~0;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002379 if (usb_hcd_is_primary_hcd(hcd)) {
2380 the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
2381 the_controller.hs_hcd->dum = &the_controller;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002382 /*
2383 * Mark the first roothub as being USB 2.0.
2384 * The USB 3.0 roothub will be registered later by
2385 * dummy_hcd_probe()
2386 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002387 hcd->speed = HCD_USB2;
2388 hcd->self.root_hub->speed = USB_SPEED_HIGH;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002389 } else {
2390 the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
2391 the_controller.ss_hcd->dum = &the_controller;
2392 hcd->speed = HCD_USB3;
2393 hcd->self.root_hub->speed = USB_SPEED_SUPER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002394 }
2395 return 0;
2396}
2397
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002398/* Change a group of bulk endpoints to support multiple stream IDs */
Sebastian Andrzej Siewiord81f3e42012-01-09 13:15:00 +01002399static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002400 struct usb_host_endpoint **eps, unsigned int num_eps,
2401 unsigned int num_streams, gfp_t mem_flags)
2402{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002403 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2404 unsigned long flags;
2405 int max_stream;
2406 int ret_streams = num_streams;
2407 unsigned int index;
2408 unsigned int i;
2409
2410 if (!num_eps)
2411 return -EINVAL;
2412
2413 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2414 for (i = 0; i < num_eps; i++) {
2415 index = dummy_get_ep_idx(&eps[i]->desc);
2416 if ((1 << index) & dum_hcd->stream_en_ep) {
2417 ret_streams = -EINVAL;
2418 goto out;
2419 }
2420 max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp);
2421 if (!max_stream) {
2422 ret_streams = -EINVAL;
2423 goto out;
2424 }
2425 if (max_stream < ret_streams) {
2426 dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u "
2427 "stream IDs.\n",
2428 eps[i]->desc.bEndpointAddress,
2429 max_stream);
2430 ret_streams = max_stream;
2431 }
2432 }
2433
2434 for (i = 0; i < num_eps; i++) {
2435 index = dummy_get_ep_idx(&eps[i]->desc);
2436 dum_hcd->stream_en_ep |= 1 << index;
2437 set_max_streams_for_pipe(dum_hcd,
2438 usb_endpoint_num(&eps[i]->desc), ret_streams);
2439 }
2440out:
2441 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2442 return ret_streams;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002443}
2444
2445/* Reverts a group of bulk endpoints back to not using stream IDs. */
Sebastian Andrzej Siewiord81f3e42012-01-09 13:15:00 +01002446static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002447 struct usb_host_endpoint **eps, unsigned int num_eps,
2448 gfp_t mem_flags)
2449{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002450 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2451 unsigned long flags;
2452 int ret;
2453 unsigned int index;
2454 unsigned int i;
2455
2456 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2457 for (i = 0; i < num_eps; i++) {
2458 index = dummy_get_ep_idx(&eps[i]->desc);
2459 if (!((1 << index) & dum_hcd->stream_en_ep)) {
2460 ret = -EINVAL;
2461 goto out;
2462 }
2463 }
2464
2465 for (i = 0; i < num_eps; i++) {
2466 index = dummy_get_ep_idx(&eps[i]->desc);
2467 dum_hcd->stream_en_ep &= ~(1 << index);
2468 set_max_streams_for_pipe(dum_hcd,
2469 usb_endpoint_num(&eps[i]->desc), 0);
2470 }
2471 ret = 0;
2472out:
2473 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2474 return ret;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002475}
2476
2477static struct hc_driver dummy_hcd = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 .description = (char *) driver_name,
2479 .product_desc = "Dummy host controller",
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002480 .hcd_priv_size = sizeof(struct dummy_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002482 .flags = HCD_USB3 | HCD_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002484 .reset = dummy_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 .start = dummy_start,
2486 .stop = dummy_stop,
2487
2488 .urb_enqueue = dummy_urb_enqueue,
2489 .urb_dequeue = dummy_urb_dequeue,
2490
2491 .get_frame_number = dummy_h_get_frame,
2492
2493 .hub_status_data = dummy_hub_status,
2494 .hub_control = dummy_hub_control,
Alan Stern0c0382e2005-10-13 17:08:02 -04002495 .bus_suspend = dummy_bus_suspend,
2496 .bus_resume = dummy_bus_resume,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002497
2498 .alloc_streams = dummy_alloc_streams,
2499 .free_streams = dummy_free_streams,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500};
2501
Alan Stern8364d6b2005-11-14 12:16:30 -05002502static int dummy_hcd_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002504 struct usb_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002505 struct usb_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 int retval;
2507
Alan Stern8364d6b2005-11-14 12:16:30 -05002508 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002510 if (!mod_data.is_super_speed)
2511 dummy_hcd.flags = HCD_USB2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002512 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2513 if (!hs_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 return -ENOMEM;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002515 hs_hcd->has_tt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002517 retval = usb_add_hcd(hs_hcd, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 if (retval != 0) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002519 usb_put_hcd(hs_hcd);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002520 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002522
2523 if (mod_data.is_super_speed) {
2524 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2525 dev_name(&pdev->dev), hs_hcd);
2526 if (!ss_hcd) {
2527 retval = -ENOMEM;
2528 goto dealloc_usb2_hcd;
2529 }
2530
2531 retval = usb_add_hcd(ss_hcd, 0, 0);
2532 if (retval)
2533 goto put_usb3_hcd;
2534 }
2535 return 0;
2536
2537put_usb3_hcd:
2538 usb_put_hcd(ss_hcd);
2539dealloc_usb2_hcd:
2540 usb_put_hcd(hs_hcd);
2541 the_controller.hs_hcd = the_controller.ss_hcd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 return retval;
2543}
2544
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002545static int dummy_hcd_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002547 struct dummy *dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002549 dum = (hcd_to_dummy_hcd(platform_get_drvdata(pdev)))->dum;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002550
2551 if (dum->ss_hcd) {
2552 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2553 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2554 }
2555
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002556 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2557 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002558
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002559 the_controller.hs_hcd = NULL;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002560 the_controller.ss_hcd = NULL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002561
Alan Sternd9b76252005-05-03 16:15:43 -04002562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563}
2564
Alan Stern8364d6b2005-11-14 12:16:30 -05002565static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
Alan Stern391eca92005-05-10 15:34:16 -04002566{
2567 struct usb_hcd *hcd;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002568 struct dummy_hcd *dum_hcd;
Alan Stern3cf0a222005-11-29 12:08:15 -05002569 int rc = 0;
Alan Stern391eca92005-05-10 15:34:16 -04002570
Harvey Harrison441b62c2008-03-03 16:08:34 -08002571 dev_dbg (&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002572
Alan Stern3cf0a222005-11-29 12:08:15 -05002573 hcd = platform_get_drvdata (pdev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002574 dum_hcd = hcd_to_dummy_hcd(hcd);
2575 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern3cf0a222005-11-29 12:08:15 -05002576 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2577 rc = -EBUSY;
2578 } else
2579 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2580 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002581}
2582
Alan Stern8364d6b2005-11-14 12:16:30 -05002583static int dummy_hcd_resume (struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04002584{
2585 struct usb_hcd *hcd;
2586
Harvey Harrison441b62c2008-03-03 16:08:34 -08002587 dev_dbg (&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002588
Alan Stern3cf0a222005-11-29 12:08:15 -05002589 hcd = platform_get_drvdata (pdev);
2590 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Alan Stern391eca92005-05-10 15:34:16 -04002591 usb_hcd_poll_rh_status (hcd);
2592 return 0;
2593}
2594
Russell King3ae5eae2005-11-09 22:32:44 +00002595static struct platform_driver dummy_hcd_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04002596 .probe = dummy_hcd_probe,
2597 .remove = dummy_hcd_remove,
Alan Stern391eca92005-05-10 15:34:16 -04002598 .suspend = dummy_hcd_suspend,
2599 .resume = dummy_hcd_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002600 .driver = {
2601 .name = (char *) driver_name,
2602 .owner = THIS_MODULE,
2603 },
Alan Sternd9b76252005-05-03 16:15:43 -04002604};
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606/*-------------------------------------------------------------------------*/
2607
Alan Sterna89a2cd2008-04-07 15:03:25 -04002608static struct platform_device *the_udc_pdev;
2609static struct platform_device *the_hcd_pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
2611static int __init init (void)
2612{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002613 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
2615 if (usb_disabled ())
2616 return -ENODEV;
Alan Sternd9b76252005-05-03 16:15:43 -04002617
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +03002618 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2619 return -EINVAL;
2620
Alan Sterna89a2cd2008-04-07 15:03:25 -04002621 the_hcd_pdev = platform_device_alloc(driver_name, -1);
2622 if (!the_hcd_pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 return retval;
Alan Sterna89a2cd2008-04-07 15:03:25 -04002624 the_udc_pdev = platform_device_alloc(gadget_name, -1);
2625 if (!the_udc_pdev)
2626 goto err_alloc_udc;
Alan Sternd9b76252005-05-03 16:15:43 -04002627
Alan Sterna89a2cd2008-04-07 15:03:25 -04002628 retval = platform_driver_register(&dummy_hcd_driver);
2629 if (retval < 0)
2630 goto err_register_hcd_driver;
2631 retval = platform_driver_register(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002632 if (retval < 0)
2633 goto err_register_udc_driver;
2634
Alan Sterna89a2cd2008-04-07 15:03:25 -04002635 retval = platform_device_add(the_hcd_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002636 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002637 goto err_add_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002638 if (!the_controller.hs_hcd ||
2639 (!the_controller.ss_hcd && mod_data.is_super_speed)) {
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002640 /*
2641 * The hcd was added successfully but its probe function failed
2642 * for some reason.
2643 */
2644 retval = -EINVAL;
2645 goto err_add_udc;
2646 }
Alan Sterna89a2cd2008-04-07 15:03:25 -04002647 retval = platform_device_add(the_udc_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002648 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002649 goto err_add_udc;
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002650 if (!platform_get_drvdata(the_udc_pdev)) {
2651 /*
2652 * The udc was added successfully but its probe function failed
2653 * for some reason.
2654 */
2655 retval = -EINVAL;
2656 goto err_probe_udc;
2657 }
Alan Sternd9b76252005-05-03 16:15:43 -04002658 return retval;
2659
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002660err_probe_udc:
2661 platform_device_del(the_udc_pdev);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002662err_add_udc:
2663 platform_device_del(the_hcd_pdev);
2664err_add_hcd:
2665 platform_driver_unregister(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002666err_register_udc_driver:
Alan Sterna89a2cd2008-04-07 15:03:25 -04002667 platform_driver_unregister(&dummy_hcd_driver);
2668err_register_hcd_driver:
2669 platform_device_put(the_udc_pdev);
2670err_alloc_udc:
2671 platform_device_put(the_hcd_pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 return retval;
2673}
2674module_init (init);
2675
2676static void __exit cleanup (void)
2677{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002678 platform_device_unregister(the_udc_pdev);
2679 platform_device_unregister(the_hcd_pdev);
2680 platform_driver_unregister(&dummy_udc_driver);
2681 platform_driver_unregister(&dummy_hcd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682}
2683module_exit (cleanup);