blob: 626473238b3c87e5802b33a54e60fac879f9dda8 [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;
91};
92
93struct dummy_request {
94 struct list_head queue; /* ep's requests */
95 struct usb_request req;
96};
97
98static inline struct dummy_ep *usb_ep_to_dummy_ep (struct usb_ep *_ep)
99{
100 return container_of (_ep, struct dummy_ep, ep);
101}
102
103static inline struct dummy_request *usb_request_to_dummy_request
104 (struct usb_request *_req)
105{
106 return container_of (_req, struct dummy_request, req);
107}
108
109/*-------------------------------------------------------------------------*/
110
111/*
112 * Every device has ep0 for control requests, plus up to 30 more endpoints,
113 * in one of two types:
114 *
115 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
116 * number can be changed. Names like "ep-a" are used for this type.
117 *
118 * - Fixed Function: in other cases. some characteristics may be mutable;
119 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
120 *
121 * Gadget drivers are responsible for not setting up conflicting endpoint
122 * configurations, illegal or unsupported packet lengths, and so on.
123 */
124
125static const char ep0name [] = "ep0";
126
127static const char *const ep_name [] = {
128 ep0name, /* everyone has ep0 */
129
130 /* act like a net2280: high speed, six configurable endpoints */
131 "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
132
133 /* or like pxa250: fifteen fixed function endpoints */
134 "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
135 "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
136 "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
137 "ep15in-int",
138
139 /* or like sa1100: two fixed function endpoints */
140 "ep1out-bulk", "ep2in-bulk",
141};
Tobias Klauser52950ed2005-12-11 16:20:08 +0100142#define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Alan Sternd9b76252005-05-03 16:15:43 -0400144/*-------------------------------------------------------------------------*/
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#define FIFO_SIZE 64
147
148struct urbp {
149 struct urb *urb;
150 struct list_head urbp_list;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +0100151 struct sg_mapping_iter miter;
152 u32 miter_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153};
154
Alan Stern391eca92005-05-10 15:34:16 -0400155
156enum dummy_rh_state {
157 DUMMY_RH_RESET,
158 DUMMY_RH_SUSPENDED,
159 DUMMY_RH_RUNNING
160};
161
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300162struct dummy_hcd {
163 struct dummy *dum;
164 enum dummy_rh_state rh_state;
165 struct timer_list timer;
166 u32 port_status;
167 u32 old_status;
168 unsigned long re_timeout;
169
170 struct usb_device *udev;
171 struct list_head urbp_list;
172
173 unsigned active:1;
174 unsigned old_active:1;
175 unsigned resuming:1;
176};
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178struct dummy {
179 spinlock_t lock;
180
181 /*
182 * SLAVE/GADGET side support
183 */
184 struct dummy_ep ep [DUMMY_ENDPOINTS];
185 int address;
186 struct usb_gadget gadget;
187 struct usb_gadget_driver *driver;
188 struct dummy_request fifo_req;
189 u8 fifo_buf [FIFO_SIZE];
190 u16 devstatus;
Alan Stern391eca92005-05-10 15:34:16 -0400191 unsigned udc_suspended:1;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400192 unsigned pullup:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 /*
195 * MASTER/HOST side support
196 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300197 struct dummy_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300198 struct dummy_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199};
200
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300201static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300203 return (struct dummy_hcd *) (hcd->hcd_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300206static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 return container_of((void *) dum, struct usb_hcd, hcd_priv);
209}
210
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300211static inline struct device *dummy_dev(struct dummy_hcd *dum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300213 return dummy_hcd_to_hcd(dum)->self.controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Alan Sternd9b76252005-05-03 16:15:43 -0400216static inline struct device *udc_dev (struct dummy *dum)
217{
218 return dum->gadget.dev.parent;
219}
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
222{
223 return container_of (ep->gadget, struct dummy, gadget);
224}
225
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300226static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300228 struct dummy *dum = container_of(gadget, struct dummy, gadget);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300229 if (dum->gadget.speed == USB_SPEED_SUPER)
230 return dum->ss_hcd;
231 else
232 return dum->hs_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
236{
237 return container_of (dev, struct dummy, gadget.dev);
238}
239
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300240static struct dummy the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242/*-------------------------------------------------------------------------*/
243
Alan Sternf1c39fa2005-05-03 16:24:04 -0400244/* SLAVE/GADGET SIDE UTILITY ROUTINES */
245
246/* called with spinlock held */
247static void nuke (struct dummy *dum, struct dummy_ep *ep)
248{
249 while (!list_empty (&ep->queue)) {
250 struct dummy_request *req;
251
252 req = list_entry (ep->queue.next, struct dummy_request, queue);
253 list_del_init (&req->queue);
254 req->req.status = -ESHUTDOWN;
255
256 spin_unlock (&dum->lock);
257 req->req.complete (&ep->ep, &req->req);
258 spin_lock (&dum->lock);
259 }
260}
261
262/* caller must hold lock */
263static void
264stop_activity (struct dummy *dum)
265{
266 struct dummy_ep *ep;
267
268 /* prevent any more requests */
269 dum->address = 0;
270
271 /* The timer is left running so that outstanding URBs can fail */
272
273 /* nuke any pending requests first, so driver i/o is quiesced */
274 list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
275 nuke (dum, ep);
276
277 /* driver now does any non-usb quiescing necessary */
278}
279
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300280/**
281 * set_link_state_by_speed() - Sets the current state of the link according to
282 * the hcd speed
283 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
284 *
285 * This function updates the port_status according to the link state and the
286 * speed of the hcd.
287 */
288static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
289{
290 struct dummy *dum = dum_hcd->dum;
291
292 if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
293 if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
294 dum_hcd->port_status = 0;
295 } else if (!dum->pullup || dum->udc_suspended) {
296 /* UDC suspend must cause a disconnect */
297 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
298 USB_PORT_STAT_ENABLE);
299 if ((dum_hcd->old_status &
300 USB_PORT_STAT_CONNECTION) != 0)
301 dum_hcd->port_status |=
302 (USB_PORT_STAT_C_CONNECTION << 16);
303 } else {
304 /* device is connected and not suspended */
305 dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
306 USB_PORT_STAT_SPEED_5GBPS) ;
307 if ((dum_hcd->old_status &
308 USB_PORT_STAT_CONNECTION) == 0)
309 dum_hcd->port_status |=
310 (USB_PORT_STAT_C_CONNECTION << 16);
311 if ((dum_hcd->port_status &
312 USB_PORT_STAT_ENABLE) == 1 &&
313 (dum_hcd->port_status &
314 USB_SS_PORT_LS_U0) == 1 &&
315 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
316 dum_hcd->active = 1;
317 }
318 } else {
319 if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
320 dum_hcd->port_status = 0;
321 } else if (!dum->pullup || dum->udc_suspended) {
322 /* UDC suspend must cause a disconnect */
323 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
324 USB_PORT_STAT_ENABLE |
325 USB_PORT_STAT_LOW_SPEED |
326 USB_PORT_STAT_HIGH_SPEED |
327 USB_PORT_STAT_SUSPEND);
328 if ((dum_hcd->old_status &
329 USB_PORT_STAT_CONNECTION) != 0)
330 dum_hcd->port_status |=
331 (USB_PORT_STAT_C_CONNECTION << 16);
332 } else {
333 dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
334 if ((dum_hcd->old_status &
335 USB_PORT_STAT_CONNECTION) == 0)
336 dum_hcd->port_status |=
337 (USB_PORT_STAT_C_CONNECTION << 16);
338 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
339 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
340 else if ((dum_hcd->port_status &
341 USB_PORT_STAT_SUSPEND) == 0 &&
342 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
343 dum_hcd->active = 1;
344 }
345 }
346}
347
Alan Sternf1c39fa2005-05-03 16:24:04 -0400348/* caller must hold lock */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300349static void set_link_state(struct dummy_hcd *dum_hcd)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400350{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300351 struct dummy *dum = dum_hcd->dum;
352
353 dum_hcd->active = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300354 if (dum->pullup)
355 if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
356 dum->gadget.speed != USB_SPEED_SUPER) ||
357 (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
358 dum->gadget.speed == USB_SPEED_SUPER))
359 return;
Alan Stern391eca92005-05-10 15:34:16 -0400360
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300361 set_link_state_by_speed(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400362
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300363 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
364 dum_hcd->active)
365 dum_hcd->resuming = 0;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400366
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300367 /* if !connected or reset */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300368 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
369 (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300370 /*
371 * We're connected and not reset (reset occurred now),
372 * and driver attached - disconnect!
373 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300374 if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300375 (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
376 dum->driver) {
377 stop_activity(dum);
378 spin_unlock(&dum->lock);
379 dum->driver->disconnect(&dum->gadget);
380 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400381 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300382 } else if (dum_hcd->active != dum_hcd->old_active) {
383 if (dum_hcd->old_active && dum->driver->suspend) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300384 spin_unlock(&dum->lock);
385 dum->driver->suspend(&dum->gadget);
386 spin_lock(&dum->lock);
387 } else if (!dum_hcd->old_active && dum->driver->resume) {
388 spin_unlock(&dum->lock);
389 dum->driver->resume(&dum->gadget);
390 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400391 }
392 }
393
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300394 dum_hcd->old_status = dum_hcd->port_status;
395 dum_hcd->old_active = dum_hcd->active;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400396}
397
398/*-------------------------------------------------------------------------*/
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/* SLAVE/GADGET SIDE DRIVER
401 *
402 * This only tracks gadget state. All the work is done when the host
403 * side tries some (emulated) i/o operation. Real device controller
404 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
405 */
406
407#define is_enabled(dum) \
408 (dum->port_status & USB_PORT_STAT_ENABLE)
409
410static int
411dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
412{
413 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300414 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 struct dummy_ep *ep;
416 unsigned max;
417 int retval;
418
419 ep = usb_ep_to_dummy_ep (_ep);
420 if (!_ep || !desc || ep->desc || _ep->name == ep0name
421 || desc->bDescriptorType != USB_DT_ENDPOINT)
422 return -EINVAL;
423 dum = ep_to_dummy (ep);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300424 if (!dum->driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return -ESHUTDOWN;
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200426
427 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300428 if (!is_enabled(dum_hcd))
429 return -ESHUTDOWN;
430
431 /*
432 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
433 * maximum packet size.
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300434 * For SS devices the wMaxPacketSize is limited by 1024.
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300435 */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700436 max = usb_endpoint_maxp(desc) & 0x7ff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 /* drivers must not request bad settings, since lower levels
439 * (hardware or its drivers) may not check. some endpoints
440 * can't do iso, many have maxpacket limitations, etc.
441 *
442 * since this "hardware" driver is here to help debugging, we
443 * have some extra sanity checks. (there could be more though,
444 * especially for "ep9out" style fixed function ones.)
445 */
446 retval = -EINVAL;
447 switch (desc->bmAttributes & 0x03) {
448 case USB_ENDPOINT_XFER_BULK:
449 if (strstr (ep->ep.name, "-iso")
450 || strstr (ep->ep.name, "-int")) {
451 goto done;
452 }
453 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300454 case USB_SPEED_SUPER:
455 if (max == 1024)
456 break;
457 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 case USB_SPEED_HIGH:
459 if (max == 512)
460 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700461 goto done;
462 case USB_SPEED_FULL:
463 if (max == 8 || max == 16 || max == 32 || max == 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /* we'll fake any legal size */
465 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700466 /* save a return statement */
467 default:
468 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470 break;
471 case USB_ENDPOINT_XFER_INT:
472 if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
473 goto done;
474 /* real hardware might not handle all packet sizes */
475 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300476 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 case USB_SPEED_HIGH:
478 if (max <= 1024)
479 break;
480 /* save a return statement */
481 case USB_SPEED_FULL:
482 if (max <= 64)
483 break;
484 /* save a return statement */
485 default:
486 if (max <= 8)
487 break;
488 goto done;
489 }
490 break;
491 case USB_ENDPOINT_XFER_ISOC:
492 if (strstr (ep->ep.name, "-bulk")
493 || strstr (ep->ep.name, "-int"))
494 goto done;
495 /* real hardware might not handle all packet sizes */
496 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300497 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 case USB_SPEED_HIGH:
499 if (max <= 1024)
500 break;
501 /* save a return statement */
502 case USB_SPEED_FULL:
503 if (max <= 1023)
504 break;
505 /* save a return statement */
506 default:
507 goto done;
508 }
509 break;
510 default:
511 /* few chips support control except on ep0 */
512 goto done;
513 }
514
515 _ep->maxpacket = max;
516 ep->desc = desc;
517
Alan Sternd9b76252005-05-03 16:15:43 -0400518 dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 _ep->name,
520 desc->bEndpointAddress & 0x0f,
521 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
522 ({ char *val;
523 switch (desc->bmAttributes & 0x03) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +0300524 case USB_ENDPOINT_XFER_BULK:
525 val = "bulk";
526 break;
527 case USB_ENDPOINT_XFER_ISOC:
528 val = "iso";
529 break;
530 case USB_ENDPOINT_XFER_INT:
531 val = "intr";
532 break;
533 default:
534 val = "ctrl";
535 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }; val; }),
537 max);
538
539 /* at this point real hardware should be NAKing transfers
540 * to that endpoint, until a buffer is queued to it.
541 */
Alan Stern851a5262008-08-14 15:48:30 -0400542 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 retval = 0;
544done:
545 return retval;
546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548static int dummy_disable (struct usb_ep *_ep)
549{
550 struct dummy_ep *ep;
551 struct dummy *dum;
552 unsigned long flags;
553 int retval;
554
555 ep = usb_ep_to_dummy_ep (_ep);
556 if (!_ep || !ep->desc || _ep->name == ep0name)
557 return -EINVAL;
558 dum = ep_to_dummy (ep);
559
560 spin_lock_irqsave (&dum->lock, flags);
561 ep->desc = NULL;
562 retval = 0;
563 nuke (dum, ep);
564 spin_unlock_irqrestore (&dum->lock, flags);
565
Alan Sternd9b76252005-05-03 16:15:43 -0400566 dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return retval;
568}
569
570static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -0400571dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 struct dummy_ep *ep;
574 struct dummy_request *req;
575
576 if (!_ep)
577 return NULL;
578 ep = usb_ep_to_dummy_ep (_ep);
579
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800580 req = kzalloc(sizeof(*req), mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (!req)
582 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 INIT_LIST_HEAD (&req->queue);
584 return &req->req;
585}
586
587static void
588dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
589{
590 struct dummy_ep *ep;
591 struct dummy_request *req;
592
593 ep = usb_ep_to_dummy_ep (_ep);
594 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
595 return;
596
597 req = usb_request_to_dummy_request (_req);
598 WARN_ON (!list_empty (&req->queue));
599 kfree (req);
600}
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602static void
603fifo_complete (struct usb_ep *ep, struct usb_request *req)
604{
605}
606
607static int
Olav Kongas5db539e2005-06-23 20:25:36 +0300608dummy_queue (struct usb_ep *_ep, struct usb_request *_req,
Al Viro55016f12005-10-21 03:21:58 -0400609 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 struct dummy_ep *ep;
612 struct dummy_request *req;
613 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300614 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 unsigned long flags;
616
617 req = usb_request_to_dummy_request (_req);
618 if (!_req || !list_empty (&req->queue) || !_req->complete)
619 return -EINVAL;
620
621 ep = usb_ep_to_dummy_ep (_ep);
622 if (!_ep || (!ep->desc && _ep->name != ep0name))
623 return -EINVAL;
624
625 dum = ep_to_dummy (ep);
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200626 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300627 if (!dum->driver || !is_enabled(dum_hcd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return -ESHUTDOWN;
629
630#if 0
Alan Sternd9b76252005-05-03 16:15:43 -0400631 dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 ep, _req, _ep->name, _req->length, _req->buf);
633#endif
634
635 _req->status = -EINPROGRESS;
636 _req->actual = 0;
637 spin_lock_irqsave (&dum->lock, flags);
638
639 /* implement an emulated single-request FIFO */
640 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
641 list_empty (&dum->fifo_req.queue) &&
642 list_empty (&ep->queue) &&
643 _req->length <= FIFO_SIZE) {
644 req = &dum->fifo_req;
645 req->req = *_req;
646 req->req.buf = dum->fifo_buf;
647 memcpy (dum->fifo_buf, _req->buf, _req->length);
648 req->req.context = dum;
649 req->req.complete = fifo_complete;
650
David Brownellc728df72008-07-26 08:06:24 -0700651 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 spin_unlock (&dum->lock);
653 _req->actual = _req->length;
654 _req->status = 0;
655 _req->complete (_ep, _req);
656 spin_lock (&dum->lock);
David Brownellc728df72008-07-26 08:06:24 -0700657 } else
658 list_add_tail(&req->queue, &ep->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 spin_unlock_irqrestore (&dum->lock, flags);
660
661 /* real hardware would likely enable transfers here, in case
662 * it'd been left NAKing.
663 */
664 return 0;
665}
666
667static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
668{
669 struct dummy_ep *ep;
670 struct dummy *dum;
671 int retval = -EINVAL;
672 unsigned long flags;
673 struct dummy_request *req = NULL;
674
675 if (!_ep || !_req)
676 return retval;
677 ep = usb_ep_to_dummy_ep (_ep);
678 dum = ep_to_dummy (ep);
679
680 if (!dum->driver)
681 return -ESHUTDOWN;
682
Alan Sternb4dbda12006-07-28 17:07:34 -0400683 local_irq_save (flags);
684 spin_lock (&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 list_for_each_entry (req, &ep->queue, queue) {
686 if (&req->req == _req) {
687 list_del_init (&req->queue);
688 _req->status = -ECONNRESET;
689 retval = 0;
690 break;
691 }
692 }
Alan Sternb4dbda12006-07-28 17:07:34 -0400693 spin_unlock (&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 if (retval == 0) {
Alan Sternd9b76252005-05-03 16:15:43 -0400696 dev_dbg (udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 "dequeued req %p from %s, len %d buf %p\n",
698 req, _ep->name, _req->length, _req->buf);
699 _req->complete (_ep, _req);
700 }
Alan Sternb4dbda12006-07-28 17:07:34 -0400701 local_irq_restore (flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return retval;
703}
704
705static int
Alan Stern851a5262008-08-14 15:48:30 -0400706dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
708 struct dummy_ep *ep;
709 struct dummy *dum;
710
711 if (!_ep)
712 return -EINVAL;
713 ep = usb_ep_to_dummy_ep (_ep);
714 dum = ep_to_dummy (ep);
715 if (!dum->driver)
716 return -ESHUTDOWN;
717 if (!value)
Alan Stern851a5262008-08-14 15:48:30 -0400718 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
720 !list_empty (&ep->queue))
721 return -EAGAIN;
Alan Stern851a5262008-08-14 15:48:30 -0400722 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 ep->halted = 1;
Alan Stern851a5262008-08-14 15:48:30 -0400724 if (wedged)
725 ep->wedged = 1;
726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /* FIXME clear emulated data toggle too */
728 return 0;
729}
730
Alan Stern851a5262008-08-14 15:48:30 -0400731static int
732dummy_set_halt(struct usb_ep *_ep, int value)
733{
734 return dummy_set_halt_and_wedge(_ep, value, 0);
735}
736
737static int dummy_set_wedge(struct usb_ep *_ep)
738{
739 if (!_ep || _ep->name == ep0name)
740 return -EINVAL;
741 return dummy_set_halt_and_wedge(_ep, 1, 1);
742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744static const struct usb_ep_ops dummy_ep_ops = {
745 .enable = dummy_enable,
746 .disable = dummy_disable,
747
748 .alloc_request = dummy_alloc_request,
749 .free_request = dummy_free_request,
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 .queue = dummy_queue,
752 .dequeue = dummy_dequeue,
753
754 .set_halt = dummy_set_halt,
Alan Stern851a5262008-08-14 15:48:30 -0400755 .set_wedge = dummy_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756};
757
758/*-------------------------------------------------------------------------*/
759
760/* there are both host and device side versions of this call ... */
761static int dummy_g_get_frame (struct usb_gadget *_gadget)
762{
763 struct timeval tv;
764
765 do_gettimeofday (&tv);
766 return tv.tv_usec / 1000;
767}
768
769static int dummy_wakeup (struct usb_gadget *_gadget)
770{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300771 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300773 dum_hcd = gadget_to_dummy_hcd(_gadget);
774 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
Alan Stern5742b0c2005-05-02 11:25:17 -0400775 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return -EINVAL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300777 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
Alan Stern391eca92005-05-10 15:34:16 -0400778 return -ENOLINK;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300779 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
780 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -0400781 return -EIO;
782
783 /* FIXME: What if the root hub is suspended but the port isn't? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /* hub notices our request, issues downstream resume, etc */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300786 dum_hcd->resuming = 1;
787 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
788 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return 0;
790}
791
792static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
793{
794 struct dummy *dum;
795
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300796 dum = (gadget_to_dummy_hcd(_gadget))->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (value)
798 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
799 else
800 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
801 return 0;
802}
803
Sebastian Andrzej Siewiord2621272012-01-09 13:14:59 +0100804static void dummy_udc_update_ep0(struct dummy *dum)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200805{
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100806 if (dum->gadget.speed == USB_SPEED_SUPER)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200807 dum->ep[0].ep.maxpacket = 9;
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100808 else
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200809 dum->ep[0].ep.maxpacket = 64;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200810}
811
Alan Sternf1c39fa2005-05-03 16:24:04 -0400812static int dummy_pullup (struct usb_gadget *_gadget, int value)
813{
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200814 struct dummy_hcd *dum_hcd;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400815 struct dummy *dum;
816 unsigned long flags;
817
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200818 dum = gadget_dev_to_dummy(&_gadget->dev);
819
820 if (value && dum->driver) {
821 if (mod_data.is_super_speed)
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100822 dum->gadget.speed = dum->driver->max_speed;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200823 else if (mod_data.is_high_speed)
824 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100825 dum->driver->max_speed);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200826 else
827 dum->gadget.speed = USB_SPEED_FULL;
Sebastian Andrzej Siewiord2621272012-01-09 13:14:59 +0100828 dummy_udc_update_ep0(dum);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200829
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100830 if (dum->gadget.speed < dum->driver->max_speed)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200831 dev_dbg(udc_dev(dum), "This device can perform faster"
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100832 " if you connect it to a %s port...\n",
833 usb_speed_string(dum->driver->max_speed));
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200834 }
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200835 dum_hcd = gadget_to_dummy_hcd(_gadget);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200836
Alan Sternf1c39fa2005-05-03 16:24:04 -0400837 spin_lock_irqsave (&dum->lock, flags);
838 dum->pullup = (value != 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200839 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400840 spin_unlock_irqrestore (&dum->lock, flags);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200841
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200842 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Sternf1c39fa2005-05-03 16:24:04 -0400843 return 0;
844}
845
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200846static int dummy_udc_start(struct usb_gadget *g,
847 struct usb_gadget_driver *driver);
848static int dummy_udc_stop(struct usb_gadget *g,
849 struct usb_gadget_driver *driver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851static const struct usb_gadget_ops dummy_ops = {
852 .get_frame = dummy_g_get_frame,
853 .wakeup = dummy_wakeup,
854 .set_selfpowered = dummy_set_selfpowered,
Alan Sternf1c39fa2005-05-03 16:24:04 -0400855 .pullup = dummy_pullup,
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200856 .udc_start = dummy_udc_start,
857 .udc_stop = dummy_udc_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858};
859
860/*-------------------------------------------------------------------------*/
861
862/* "function" sysfs attribute */
863static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400864show_function (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 struct dummy *dum = gadget_dev_to_dummy (dev);
867
868 if (!dum->driver || !dum->driver->function)
869 return 0;
870 return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
871}
Alan Sterncc095b02005-05-10 15:28:38 -0400872static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874/*-------------------------------------------------------------------------*/
875
876/*
877 * Driver registration/unregistration.
878 *
879 * This is basically hardware-specific; there's usually only one real USB
880 * device (not host) controller since that's how USB devices are intended
881 * to work. So most implementations of these api calls will rely on the
882 * fact that only one driver will ever bind to the hardware. But curious
883 * hardware can be built with discrete components, so the gadget API doesn't
884 * require that assumption.
885 *
886 * For this emulator, it might be convenient to create a usb slave device
887 * for each driver that registers: just add to a big root hub.
888 */
889
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200890static int dummy_udc_start(struct usb_gadget *g,
891 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200893 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
894 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100896 if (driver->max_speed == USB_SPEED_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return -EINVAL;
898
899 /*
900 * SLAVE side init ... the layer above hardware, which
901 * can't enumerate without help from the driver we're binding.
902 */
Alan Stern5742b0c2005-05-02 11:25:17 -0400903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 dum->devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 dum->driver = driver;
Alan Sternd9b76252005-05-03 16:15:43 -0400907 dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return 0;
910}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200912static int dummy_udc_stop(struct usb_gadget *g,
913 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200915 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
916 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Alan Sternd9b76252005-05-03 16:15:43 -0400918 dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 driver->driver.name);
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 dum->driver = NULL;
Sebastian Andrzej Siewior25427872011-06-16 20:36:55 +0200922
923 dummy_pullup(&dum->gadget, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return 0;
925}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927#undef is_enabled
928
Alan Sternd9b76252005-05-03 16:15:43 -0400929/* The gadget structure is stored inside the hcd structure and will be
930 * released along with it. */
931static void
932dummy_gadget_release (struct device *dev)
933{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300934 return;
Alan Sternd9b76252005-05-03 16:15:43 -0400935}
936
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200937static void init_dummy_udc_hw(struct dummy *dum)
938{
939 int i;
940
941 INIT_LIST_HEAD(&dum->gadget.ep_list);
942 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
943 struct dummy_ep *ep = &dum->ep[i];
944
945 if (!ep_name[i])
946 break;
947 ep->ep.name = ep_name[i];
948 ep->ep.ops = &dummy_ep_ops;
949 list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
950 ep->halted = ep->wedged = ep->already_seen =
951 ep->setup_stage = 0;
952 ep->ep.maxpacket = ~0;
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100953 ep->ep.max_streams = 16;
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200954 ep->last_io = jiffies;
955 ep->gadget = &dum->gadget;
956 ep->desc = NULL;
957 INIT_LIST_HEAD(&ep->queue);
958 }
959
960 dum->gadget.ep0 = &dum->ep[0].ep;
961 list_del_init(&dum->ep[0].ep.ep_list);
962 INIT_LIST_HEAD(&dum->fifo_req.queue);
Sebastian Andrzej Siewiorf8744d42011-06-23 14:26:13 +0200963
964#ifdef CONFIG_USB_OTG
965 dum->gadget.is_otg = 1;
966#endif
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200967}
968
Alan Stern8364d6b2005-11-14 12:16:30 -0500969static int dummy_udc_probe (struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -0400970{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300971 struct dummy *dum = &the_controller;
Alan Sternd9b76252005-05-03 16:15:43 -0400972 int rc;
973
974 dum->gadget.name = gadget_name;
975 dum->gadget.ops = &dummy_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100976 dum->gadget.max_speed = USB_SPEED_SUPER;
Alan Sternd9b76252005-05-03 16:15:43 -0400977
Kay Sievers0031a062008-05-02 06:02:41 +0200978 dev_set_name(&dum->gadget.dev, "gadget");
Alan Stern8364d6b2005-11-14 12:16:30 -0500979 dum->gadget.dev.parent = &pdev->dev;
Alan Sternd9b76252005-05-03 16:15:43 -0400980 dum->gadget.dev.release = dummy_gadget_release;
981 rc = device_register (&dum->gadget.dev);
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530982 if (rc < 0) {
983 put_device(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -0400984 return rc;
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530985 }
Alan Sternd9b76252005-05-03 16:15:43 -0400986
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200987 init_dummy_udc_hw(dum);
988
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300989 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
990 if (rc < 0)
991 goto err_udc;
992
Alan Sternefd54a32006-09-25 11:55:56 -0400993 rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
994 if (rc < 0)
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300995 goto err_dev;
996 platform_set_drvdata(pdev, dum);
997 return rc;
998
999err_dev:
1000 usb_del_gadget_udc(&dum->gadget);
1001err_udc:
1002 device_unregister(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -04001003 return rc;
1004}
1005
Alan Stern8364d6b2005-11-14 12:16:30 -05001006static int dummy_udc_remove (struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -04001007{
Alan Stern8364d6b2005-11-14 12:16:30 -05001008 struct dummy *dum = platform_get_drvdata (pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04001009
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001010 usb_del_gadget_udc(&dum->gadget);
Alan Stern8364d6b2005-11-14 12:16:30 -05001011 platform_set_drvdata (pdev, NULL);
Alan Sternd9b76252005-05-03 16:15:43 -04001012 device_remove_file (&dum->gadget.dev, &dev_attr_function);
1013 device_unregister (&dum->gadget.dev);
1014 return 0;
1015}
1016
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001017static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1018 int suspend)
Alan Stern391eca92005-05-10 15:34:16 -04001019{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001020 spin_lock_irq(&dum->lock);
1021 dum->udc_suspended = suspend;
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001022 set_link_state(dum_hcd);
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001023 spin_unlock_irq(&dum->lock);
1024}
Alan Stern391eca92005-05-10 15:34:16 -04001025
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001026static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1027{
1028 struct dummy *dum = platform_get_drvdata(pdev);
1029 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1030
1031 dev_dbg(&pdev->dev, "%s\n", __func__);
1032 dummy_udc_pm(dum, dum_hcd, 1);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001033 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001034 return 0;
1035}
1036
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001037static int dummy_udc_resume(struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04001038{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001039 struct dummy *dum = platform_get_drvdata(pdev);
1040 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Alan Stern391eca92005-05-10 15:34:16 -04001041
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001042 dev_dbg(&pdev->dev, "%s\n", __func__);
1043 dummy_udc_pm(dum, dum_hcd, 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001044 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001045 return 0;
1046}
1047
Russell King3ae5eae2005-11-09 22:32:44 +00001048static struct platform_driver dummy_udc_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04001049 .probe = dummy_udc_probe,
1050 .remove = dummy_udc_remove,
Alan Stern391eca92005-05-10 15:34:16 -04001051 .suspend = dummy_udc_suspend,
1052 .resume = dummy_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001053 .driver = {
1054 .name = (char *) gadget_name,
1055 .owner = THIS_MODULE,
1056 },
Alan Sternd9b76252005-05-03 16:15:43 -04001057};
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059/*-------------------------------------------------------------------------*/
1060
1061/* MASTER/HOST SIDE DRIVER
1062 *
1063 * this uses the hcd framework to hook up to host side drivers.
1064 * its root hub will only have one device, otherwise it acts like
1065 * a normal host controller.
1066 *
1067 * when urbs are queued, they're just stuck on a list that we
1068 * scan in a timer callback. that callback connects writes from
1069 * the host with reads from the device, and so on, based on the
1070 * usb 2.0 rules.
1071 */
1072
1073static int dummy_urb_enqueue (
1074 struct usb_hcd *hcd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001076 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001078 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 struct urbp *urbp;
1080 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001081 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 urbp = kmalloc (sizeof *urbp, mem_flags);
1084 if (!urbp)
1085 return -ENOMEM;
1086 urbp->urb = urb;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001087 urbp->miter_started = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001089 dum_hcd = hcd_to_dummy_hcd(hcd);
1090 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001091 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1092 if (rc) {
1093 kfree(urbp);
1094 goto done;
1095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001097 if (!dum_hcd->udev) {
1098 dum_hcd->udev = urb->dev;
1099 usb_get_dev(dum_hcd->udev);
1100 } else if (unlikely(dum_hcd->udev != urb->dev))
1101 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001103 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 urb->hcpriv = urbp;
1105 if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
1106 urb->error_count = 1; /* mark as a new urb */
1107
1108 /* kick the scheduler, it'll do the rest */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001109 if (!timer_pending(&dum_hcd->timer))
1110 mod_timer(&dum_hcd->timer, jiffies + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Alan Sterne9df41c2007-08-08 11:48:02 -04001112 done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001113 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001114 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
Alan Sterne9df41c2007-08-08 11:48:02 -04001117static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001119 struct dummy_hcd *dum_hcd;
Alan Stern391eca92005-05-10 15:34:16 -04001120 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001121 int rc;
Alan Stern391eca92005-05-10 15:34:16 -04001122
1123 /* giveback happens automatically in timer callback,
1124 * so make sure the callback happens */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001125 dum_hcd = hcd_to_dummy_hcd(hcd);
1126 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001127
1128 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001129 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1130 !list_empty(&dum_hcd->urbp_list))
1131 mod_timer(&dum_hcd->timer, jiffies);
Alan Sterne9df41c2007-08-08 11:48:02 -04001132
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001133 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001134 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001137static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
1138 u32 len)
1139{
1140 void *ubuf, *rbuf;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001141 struct urbp *urbp = urb->hcpriv;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001142 int to_host;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001143 struct sg_mapping_iter *miter = &urbp->miter;
1144 u32 trans = 0;
1145 u32 this_sg;
1146 bool next_sg;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001147
1148 to_host = usb_pipein(urb->pipe);
1149 rbuf = req->req.buf + req->req.actual;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001150
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001151 if (!urb->num_sgs) {
1152 ubuf = urb->transfer_buffer + urb->actual_length;
1153 if (to_host)
1154 memcpy(ubuf, rbuf, len);
1155 else
1156 memcpy(rbuf, ubuf, len);
1157 return len;
1158 }
1159
1160 if (!urbp->miter_started) {
1161 u32 flags = SG_MITER_ATOMIC;
1162
1163 if (to_host)
1164 flags |= SG_MITER_TO_SG;
1165 else
1166 flags |= SG_MITER_FROM_SG;
1167
1168 sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
1169 urbp->miter_started = 1;
1170 }
1171 next_sg = sg_miter_next(miter);
1172 if (next_sg == false) {
1173 WARN_ON_ONCE(1);
1174 return -EINVAL;
1175 }
1176 do {
1177 ubuf = miter->addr;
1178 this_sg = min_t(u32, len, miter->length);
1179 miter->consumed = this_sg;
1180 trans += this_sg;
1181
1182 if (to_host)
1183 memcpy(ubuf, rbuf, this_sg);
1184 else
1185 memcpy(rbuf, ubuf, this_sg);
1186 len -= this_sg;
1187
1188 if (!len)
1189 break;
1190 next_sg = sg_miter_next(miter);
1191 if (next_sg == false) {
1192 WARN_ON_ONCE(1);
1193 return -EINVAL;
1194 }
1195
1196 rbuf += this_sg;
1197 } while (1);
1198
1199 sg_miter_stop(miter);
1200 return trans;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001201}
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203/* transfer up to a frame's worth; caller must own lock */
1204static int
Alan Stern4d2f1102007-08-24 15:40:10 -04001205transfer(struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit,
1206 int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 struct dummy_request *req;
1209
1210top:
1211 /* if there's no request queued, the device is NAKing; return */
1212 list_for_each_entry (req, &ep->queue, queue) {
1213 unsigned host_len, dev_len, len;
1214 int is_short, to_host;
1215 int rescan = 0;
1216
1217 /* 1..N packets of ep->ep.maxpacket each ... the last one
1218 * may be short (including zero length).
1219 *
1220 * writer can send a zlp explicitly (length 0) or implicitly
1221 * (length mod maxpacket zero, and 'zero' flag); they always
1222 * terminate reads.
1223 */
1224 host_len = urb->transfer_buffer_length - urb->actual_length;
1225 dev_len = req->req.length - req->req.actual;
1226 len = min (host_len, dev_len);
1227
1228 /* FIXME update emulated data toggle too */
1229
1230 to_host = usb_pipein (urb->pipe);
1231 if (unlikely (len == 0))
1232 is_short = 1;
1233 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 /* not enough bandwidth left? */
1235 if (limit < ep->ep.maxpacket && limit < len)
1236 break;
1237 len = min (len, (unsigned) limit);
1238 if (len == 0)
1239 break;
1240
1241 /* use an extra pass for the final short packet */
1242 if (len > ep->ep.maxpacket) {
1243 rescan = 1;
1244 len -= (len % ep->ep.maxpacket);
1245 }
1246 is_short = (len % ep->ep.maxpacket) != 0;
1247
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001248 len = dummy_perform_transfer(urb, req, len);
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 ep->last_io = jiffies;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001251 if (len < 0) {
1252 req->req.status = len;
1253 } else {
1254 limit -= len;
1255 urb->actual_length += len;
1256 req->req.actual += len;
1257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259
1260 /* short packets terminate, maybe with overflow/underflow.
1261 * it's only really an error to write too much.
1262 *
1263 * partially filling a buffer optionally blocks queue advances
1264 * (so completion handlers can clean up the queue) but we don't
Alan Sternb0d9efb2007-08-21 15:39:21 -04001265 * need to emulate such data-in-flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 */
1267 if (is_short) {
1268 if (host_len == dev_len) {
1269 req->req.status = 0;
Alan Stern4d2f1102007-08-24 15:40:10 -04001270 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 } else if (to_host) {
1272 req->req.status = 0;
1273 if (dev_len > host_len)
Alan Stern4d2f1102007-08-24 15:40:10 -04001274 *status = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 else
Alan Stern4d2f1102007-08-24 15:40:10 -04001276 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 } else if (!to_host) {
Alan Stern4d2f1102007-08-24 15:40:10 -04001278 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (host_len > dev_len)
1280 req->req.status = -EOVERFLOW;
1281 else
1282 req->req.status = 0;
1283 }
1284
1285 /* many requests terminate without a short packet */
1286 } else {
1287 if (req->req.length == req->req.actual
1288 && !req->req.zero)
1289 req->req.status = 0;
1290 if (urb->transfer_buffer_length == urb->actual_length
1291 && !(urb->transfer_flags
Alan Stern4d2f1102007-08-24 15:40:10 -04001292 & URB_ZERO_PACKET))
1293 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
1295
1296 /* device side completion --> continuable */
1297 if (req->req.status != -EINPROGRESS) {
1298 list_del_init (&req->queue);
1299
1300 spin_unlock (&dum->lock);
1301 req->req.complete (&ep->ep, &req->req);
1302 spin_lock (&dum->lock);
1303
1304 /* requests might have been unlinked... */
1305 rescan = 1;
1306 }
1307
1308 /* host side completion --> terminate */
Alan Stern4d2f1102007-08-24 15:40:10 -04001309 if (*status != -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 break;
1311
1312 /* rescan to continue with any other queued i/o */
1313 if (rescan)
1314 goto top;
1315 }
1316 return limit;
1317}
1318
1319static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1320{
1321 int limit = ep->ep.maxpacket;
1322
1323 if (dum->gadget.speed == USB_SPEED_HIGH) {
1324 int tmp;
1325
1326 /* high bandwidth mode */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001327 tmp = usb_endpoint_maxp(ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 tmp = (tmp >> 11) & 0x03;
1329 tmp *= 8 /* applies to entire frame */;
1330 limit += limit * tmp;
1331 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001332 if (dum->gadget.speed == USB_SPEED_SUPER) {
1333 switch (ep->desc->bmAttributes & 0x03) {
1334 case USB_ENDPOINT_XFER_ISOC:
1335 /* Sec. 4.4.8.2 USB3.0 Spec */
1336 limit = 3 * 16 * 1024 * 8;
1337 break;
1338 case USB_ENDPOINT_XFER_INT:
1339 /* Sec. 4.4.7.2 USB3.0 Spec */
1340 limit = 3 * 1024 * 8;
1341 break;
1342 case USB_ENDPOINT_XFER_BULK:
1343 default:
1344 break;
1345 }
1346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return limit;
1348}
1349
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001350#define is_active(dum_hcd) ((dum_hcd->port_status & \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1352 USB_PORT_STAT_SUSPEND)) \
1353 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1354
1355static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1356{
1357 int i;
1358
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001359 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1360 dum->ss_hcd : dum->hs_hcd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return NULL;
1362 if ((address & ~USB_DIR_IN) == 0)
1363 return &dum->ep [0];
1364 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1365 struct dummy_ep *ep = &dum->ep [i];
1366
1367 if (!ep->desc)
1368 continue;
1369 if (ep->desc->bEndpointAddress == address)
1370 return ep;
1371 }
1372 return NULL;
1373}
1374
1375#undef is_active
1376
1377#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1378#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1379#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1380#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1381#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1382#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1383
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001384
1385/**
1386 * handle_control_request() - handles all control transfers
1387 * @dum: pointer to dummy (the_controller)
1388 * @urb: the urb request to handle
1389 * @setup: pointer to the setup data for a USB device control
1390 * request
1391 * @status: pointer to request handling status
1392 *
1393 * Return 0 - if the request was handled
1394 * 1 - if the request wasn't handles
1395 * error code on error
1396 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001397static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001398 struct usb_ctrlrequest *setup,
1399 int *status)
1400{
1401 struct dummy_ep *ep2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001402 struct dummy *dum = dum_hcd->dum;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001403 int ret_val = 1;
1404 unsigned w_index;
1405 unsigned w_value;
1406
1407 w_index = le16_to_cpu(setup->wIndex);
1408 w_value = le16_to_cpu(setup->wValue);
1409 switch (setup->bRequest) {
1410 case USB_REQ_SET_ADDRESS:
1411 if (setup->bRequestType != Dev_Request)
1412 break;
1413 dum->address = w_value;
1414 *status = 0;
1415 dev_dbg(udc_dev(dum), "set_address = %d\n",
1416 w_value);
1417 ret_val = 0;
1418 break;
1419 case USB_REQ_SET_FEATURE:
1420 if (setup->bRequestType == Dev_Request) {
1421 ret_val = 0;
1422 switch (w_value) {
1423 case USB_DEVICE_REMOTE_WAKEUP:
1424 break;
1425 case USB_DEVICE_B_HNP_ENABLE:
1426 dum->gadget.b_hnp_enable = 1;
1427 break;
1428 case USB_DEVICE_A_HNP_SUPPORT:
1429 dum->gadget.a_hnp_support = 1;
1430 break;
1431 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1432 dum->gadget.a_alt_hnp_support = 1;
1433 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001434 case USB_DEVICE_U1_ENABLE:
1435 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1436 HCD_USB3)
1437 w_value = USB_DEV_STAT_U1_ENABLED;
1438 else
1439 ret_val = -EOPNOTSUPP;
1440 break;
1441 case USB_DEVICE_U2_ENABLE:
1442 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1443 HCD_USB3)
1444 w_value = USB_DEV_STAT_U2_ENABLED;
1445 else
1446 ret_val = -EOPNOTSUPP;
1447 break;
1448 case USB_DEVICE_LTM_ENABLE:
1449 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1450 HCD_USB3)
1451 w_value = USB_DEV_STAT_LTM_ENABLED;
1452 else
1453 ret_val = -EOPNOTSUPP;
1454 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001455 default:
1456 ret_val = -EOPNOTSUPP;
1457 }
1458 if (ret_val == 0) {
1459 dum->devstatus |= (1 << w_value);
1460 *status = 0;
1461 }
1462 } else if (setup->bRequestType == Ep_Request) {
1463 /* endpoint halt */
1464 ep2 = find_endpoint(dum, w_index);
1465 if (!ep2 || ep2->ep.name == ep0name) {
1466 ret_val = -EOPNOTSUPP;
1467 break;
1468 }
1469 ep2->halted = 1;
1470 ret_val = 0;
1471 *status = 0;
1472 }
1473 break;
1474 case USB_REQ_CLEAR_FEATURE:
1475 if (setup->bRequestType == Dev_Request) {
1476 ret_val = 0;
1477 switch (w_value) {
1478 case USB_DEVICE_REMOTE_WAKEUP:
1479 w_value = USB_DEVICE_REMOTE_WAKEUP;
1480 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001481 case USB_DEVICE_U1_ENABLE:
1482 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1483 HCD_USB3)
1484 w_value = USB_DEV_STAT_U1_ENABLED;
1485 else
1486 ret_val = -EOPNOTSUPP;
1487 break;
1488 case USB_DEVICE_U2_ENABLE:
1489 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1490 HCD_USB3)
1491 w_value = USB_DEV_STAT_U2_ENABLED;
1492 else
1493 ret_val = -EOPNOTSUPP;
1494 break;
1495 case USB_DEVICE_LTM_ENABLE:
1496 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1497 HCD_USB3)
1498 w_value = USB_DEV_STAT_LTM_ENABLED;
1499 else
1500 ret_val = -EOPNOTSUPP;
1501 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001502 default:
1503 ret_val = -EOPNOTSUPP;
1504 break;
1505 }
1506 if (ret_val == 0) {
1507 dum->devstatus &= ~(1 << w_value);
1508 *status = 0;
1509 }
1510 } else if (setup->bRequestType == Ep_Request) {
1511 /* endpoint halt */
1512 ep2 = find_endpoint(dum, w_index);
1513 if (!ep2) {
1514 ret_val = -EOPNOTSUPP;
1515 break;
1516 }
1517 if (!ep2->wedged)
1518 ep2->halted = 0;
1519 ret_val = 0;
1520 *status = 0;
1521 }
1522 break;
1523 case USB_REQ_GET_STATUS:
1524 if (setup->bRequestType == Dev_InRequest
1525 || setup->bRequestType == Intf_InRequest
1526 || setup->bRequestType == Ep_InRequest) {
1527 char *buf;
1528 /*
1529 * device: remote wakeup, selfpowered
1530 * interface: nothing
1531 * endpoint: halt
1532 */
1533 buf = (char *)urb->transfer_buffer;
1534 if (urb->transfer_buffer_length > 0) {
1535 if (setup->bRequestType == Ep_InRequest) {
1536 ep2 = find_endpoint(dum, w_index);
1537 if (!ep2) {
1538 ret_val = -EOPNOTSUPP;
1539 break;
1540 }
1541 buf[0] = ep2->halted;
1542 } else if (setup->bRequestType ==
1543 Dev_InRequest) {
1544 buf[0] = (u8)dum->devstatus;
1545 } else
1546 buf[0] = 0;
1547 }
1548 if (urb->transfer_buffer_length > 1)
1549 buf[1] = 0;
1550 urb->actual_length = min_t(u32, 2,
1551 urb->transfer_buffer_length);
1552 ret_val = 0;
1553 *status = 0;
1554 }
1555 break;
1556 }
1557 return ret_val;
1558}
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560/* drive both sides of the transfers; looks like irq handlers to
1561 * both drivers except the callbacks aren't in_irq().
1562 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001563static void dummy_timer(unsigned long _dum_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001565 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1566 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 struct urbp *urbp, *tmp;
1568 unsigned long flags;
1569 int limit, total;
1570 int i;
1571
1572 /* simplistic model for one frame's bandwidth */
1573 switch (dum->gadget.speed) {
1574 case USB_SPEED_LOW:
1575 total = 8/*bytes*/ * 12/*packets*/;
1576 break;
1577 case USB_SPEED_FULL:
1578 total = 64/*bytes*/ * 19/*packets*/;
1579 break;
1580 case USB_SPEED_HIGH:
1581 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1582 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001583 case USB_SPEED_SUPER:
1584 /* Bus speed is 500000 bytes/ms, so use a little less */
1585 total = 490000;
1586 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001588 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return;
1590 }
1591
1592 /* FIXME if HZ != 1000 this will probably misbehave ... */
1593
1594 /* look at each urb queued by the host side driver */
1595 spin_lock_irqsave (&dum->lock, flags);
1596
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001597 if (!dum_hcd->udev) {
1598 dev_err(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 "timer fired with no URBs pending?\n");
1600 spin_unlock_irqrestore (&dum->lock, flags);
1601 return;
1602 }
1603
1604 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1605 if (!ep_name [i])
1606 break;
1607 dum->ep [i].already_seen = 0;
1608 }
1609
1610restart:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001611 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 struct urb *urb;
1613 struct dummy_request *req;
1614 u8 address;
1615 struct dummy_ep *ep = NULL;
1616 int type;
Alan Stern4d2f1102007-08-24 15:40:10 -04001617 int status = -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 urb = urbp->urb;
Alan Sterneb231052007-08-21 15:40:36 -04001620 if (urb->unlinked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 goto return_urb;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001622 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
Alan Stern391eca92005-05-10 15:34:16 -04001623 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 type = usb_pipetype (urb->pipe);
1625
1626 /* used up this frame's non-periodic bandwidth?
1627 * FIXME there's infinite bandwidth for control and
1628 * periodic transfers ... unrealistic.
1629 */
1630 if (total <= 0 && type == PIPE_BULK)
1631 continue;
1632
1633 /* find the gadget's ep for this request (if configured) */
1634 address = usb_pipeendpoint (urb->pipe);
1635 if (usb_pipein (urb->pipe))
1636 address |= USB_DIR_IN;
1637 ep = find_endpoint(dum, address);
1638 if (!ep) {
1639 /* set_configuration() disagreement */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001640 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 "no ep configured for urb %p\n",
1642 urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001643 status = -EPROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 goto return_urb;
1645 }
1646
1647 if (ep->already_seen)
1648 continue;
1649 ep->already_seen = 1;
1650 if (ep == &dum->ep [0] && urb->error_count) {
1651 ep->setup_stage = 1; /* a new urb */
1652 urb->error_count = 0;
1653 }
1654 if (ep->halted && !ep->setup_stage) {
1655 /* NOTE: must not be iso! */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001656 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 ep->ep.name, urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001658 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 goto return_urb;
1660 }
1661 /* FIXME make sure both ends agree on maxpacket */
1662
1663 /* handle control requests */
1664 if (ep == &dum->ep [0] && ep->setup_stage) {
1665 struct usb_ctrlrequest setup;
1666 int value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 setup = *(struct usb_ctrlrequest*) urb->setup_packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 /* paranoia, in case of stale queued data */
1670 list_for_each_entry (req, &ep->queue, queue) {
1671 list_del_init (&req->queue);
1672 req->req.status = -EOVERFLOW;
Alan Sternd9b76252005-05-03 16:15:43 -04001673 dev_dbg (udc_dev(dum), "stale req = %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 req);
1675
1676 spin_unlock (&dum->lock);
1677 req->req.complete (&ep->ep, &req->req);
1678 spin_lock (&dum->lock);
1679 ep->already_seen = 0;
1680 goto restart;
1681 }
1682
1683 /* gadget driver never sees set_address or operations
1684 * on standard feature flags. some hardware doesn't
1685 * even expose them.
1686 */
1687 ep->last_io = jiffies;
1688 ep->setup_stage = 0;
1689 ep->halted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001691 value = handle_control_request(dum_hcd, urb, &setup,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001692 &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 /* gadget driver handles all other requests. block
1695 * until setup() returns; no reentrancy issues etc.
1696 */
1697 if (value > 0) {
1698 spin_unlock (&dum->lock);
1699 value = dum->driver->setup (&dum->gadget,
1700 &setup);
1701 spin_lock (&dum->lock);
1702
1703 if (value >= 0) {
1704 /* no delays (max 64KB data stage) */
1705 limit = 64*1024;
1706 goto treat_control_like_bulk;
1707 }
1708 /* error, see below */
1709 }
1710
1711 if (value < 0) {
1712 if (value != -EOPNOTSUPP)
Alan Sternd9b76252005-05-03 16:15:43 -04001713 dev_dbg (udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 "setup --> %d\n",
1715 value);
Alan Stern4d2f1102007-08-24 15:40:10 -04001716 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 urb->actual_length = 0;
1718 }
1719
1720 goto return_urb;
1721 }
1722
1723 /* non-control requests */
1724 limit = total;
1725 switch (usb_pipetype (urb->pipe)) {
1726 case PIPE_ISOCHRONOUS:
1727 /* FIXME is it urb->interval since the last xfer?
1728 * use urb->iso_frame_desc[i].
1729 * complete whether or not ep has requests queued.
1730 * report random errors, to debug drivers.
1731 */
1732 limit = max (limit, periodic_bytes (dum, ep));
Alan Stern4d2f1102007-08-24 15:40:10 -04001733 status = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 break;
1735
1736 case PIPE_INTERRUPT:
1737 /* FIXME is it urb->interval since the last xfer?
1738 * this almost certainly polls too fast.
1739 */
1740 limit = max (limit, periodic_bytes (dum, ep));
1741 /* FALLTHROUGH */
1742
1743 // case PIPE_BULK: case PIPE_CONTROL:
1744 default:
1745 treat_control_like_bulk:
1746 ep->last_io = jiffies;
Alan Stern4d2f1102007-08-24 15:40:10 -04001747 total = transfer(dum, urb, ep, limit, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 break;
1749 }
1750
1751 /* incomplete transfer? */
Alan Stern4d2f1102007-08-24 15:40:10 -04001752 if (status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 continue;
1754
1755return_urb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 list_del (&urbp->urbp_list);
1757 kfree (urbp);
1758 if (ep)
1759 ep->already_seen = ep->setup_stage = 0;
1760
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001761 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 spin_unlock (&dum->lock);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001763 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 spin_lock (&dum->lock);
1765
1766 goto restart;
1767 }
1768
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001769 if (list_empty(&dum_hcd->urbp_list)) {
1770 usb_put_dev(dum_hcd->udev);
1771 dum_hcd->udev = NULL;
1772 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern391eca92005-05-10 15:34:16 -04001773 /* want a 1 msec delay here */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001774 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
1776
1777 spin_unlock_irqrestore (&dum->lock, flags);
1778}
1779
1780/*-------------------------------------------------------------------------*/
1781
1782#define PORT_C_MASK \
Alan Sternc2db8b52005-04-29 16:30:48 -04001783 ((USB_PORT_STAT_C_CONNECTION \
1784 | USB_PORT_STAT_C_ENABLE \
1785 | USB_PORT_STAT_C_SUSPEND \
1786 | USB_PORT_STAT_C_OVERCURRENT \
1787 | USB_PORT_STAT_C_RESET) << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1790{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001791 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 unsigned long flags;
Alan Stern391eca92005-05-10 15:34:16 -04001793 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001795 dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001797 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001798 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001799 goto done;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001800
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001801 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1802 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1803 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1804 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001805 }
1806
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001807 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 *buf = (1 << 1);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001809 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1810 dum_hcd->port_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 retval = 1;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001812 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -04001813 usb_hcd_resume_root_hub (hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 }
Alan Stern391eca92005-05-10 15:34:16 -04001815done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001816 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 return retval;
1818}
1819
1820static inline void
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001821ss_hub_descriptor(struct usb_hub_descriptor *desc)
1822{
1823 memset(desc, 0, sizeof *desc);
1824 desc->bDescriptorType = 0x2a;
1825 desc->bDescLength = 12;
1826 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1827 desc->bNbrPorts = 1;
1828 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1829 desc->u.ss.DeviceRemovable = 0xffff;
1830}
1831
1832static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833hub_descriptor (struct usb_hub_descriptor *desc)
1834{
1835 memset (desc, 0, sizeof *desc);
1836 desc->bDescriptorType = 0x29;
1837 desc->bDescLength = 9;
Al Virofd05e722008-04-28 07:00:16 +01001838 desc->wHubCharacteristics = cpu_to_le16(0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 desc->bNbrPorts = 1;
John Youndbe79bb2001-09-17 00:00:00 -07001840 desc->u.hs.DeviceRemovable[0] = 0xff;
1841 desc->u.hs.DeviceRemovable[1] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
1844static int dummy_hub_control (
1845 struct usb_hcd *hcd,
1846 u16 typeReq,
1847 u16 wValue,
1848 u16 wIndex,
1849 char *buf,
1850 u16 wLength
1851) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001852 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 int retval = 0;
1854 unsigned long flags;
1855
Alan Stern541c7d42010-06-22 16:39:10 -04001856 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001857 return -ETIMEDOUT;
1858
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001859 dum_hcd = hcd_to_dummy_hcd(hcd);
1860
1861 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 switch (typeReq) {
1863 case ClearHubFeature:
1864 break;
1865 case ClearPortFeature:
1866 switch (wValue) {
1867 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001868 if (hcd->speed == HCD_USB3) {
1869 dev_dbg(dummy_dev(dum_hcd),
1870 "USB_PORT_FEAT_SUSPEND req not "
1871 "supported for USB 3.0 roothub\n");
1872 goto error;
1873 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001874 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 /* 20msec resume signaling */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001876 dum_hcd->resuming = 1;
1877 dum_hcd->re_timeout = jiffies +
Alan Sternf1c39fa2005-05-03 16:24:04 -04001878 msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
1880 break;
1881 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001882 if (hcd->speed == HCD_USB3) {
1883 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1884 dev_dbg(dummy_dev(dum_hcd),
1885 "power-off\n");
1886 } else
1887 if (dum_hcd->port_status &
1888 USB_SS_PORT_STAT_POWER)
1889 dev_dbg(dummy_dev(dum_hcd),
1890 "power-off\n");
Alan Sternf1c39fa2005-05-03 16:24:04 -04001891 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001893 dum_hcd->port_status &= ~(1 << wValue);
1894 set_link_state(dum_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
1896 break;
1897 case GetHubDescriptor:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001898 if (hcd->speed == HCD_USB3 &&
1899 (wLength < USB_DT_SS_HUB_SIZE ||
1900 wValue != (USB_DT_SS_HUB << 8))) {
1901 dev_dbg(dummy_dev(dum_hcd),
1902 "Wrong hub descriptor type for "
1903 "USB 3.0 roothub.\n");
1904 goto error;
1905 }
1906 if (hcd->speed == HCD_USB3)
1907 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
1908 else
1909 hub_descriptor((struct usb_hub_descriptor *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 break;
1911 case GetHubStatus:
Harvey Harrison551509d2009-02-11 14:11:36 -08001912 *(__le32 *) buf = cpu_to_le32 (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 break;
1914 case GetPortStatus:
1915 if (wIndex != 1)
1916 retval = -EPIPE;
1917
1918 /* whoever resets or resumes must GetPortStatus to
1919 * complete it!!
1920 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001921 if (dum_hcd->resuming &&
1922 time_after_eq(jiffies, dum_hcd->re_timeout)) {
1923 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1924 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001926 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
1927 time_after_eq(jiffies, dum_hcd->re_timeout)) {
1928 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
1929 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
1930 if (dum_hcd->dum->pullup) {
1931 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001932
1933 if (hcd->speed < HCD_USB3) {
1934 switch (dum_hcd->dum->gadget.speed) {
1935 case USB_SPEED_HIGH:
1936 dum_hcd->port_status |=
1937 USB_PORT_STAT_HIGH_SPEED;
1938 break;
1939 case USB_SPEED_LOW:
1940 dum_hcd->dum->gadget.ep0->
1941 maxpacket = 8;
1942 dum_hcd->port_status |=
1943 USB_PORT_STAT_LOW_SPEED;
1944 break;
1945 default:
1946 dum_hcd->dum->gadget.speed =
1947 USB_SPEED_FULL;
1948 break;
1949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951 }
1952 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001953 set_link_state(dum_hcd);
1954 ((__le16 *) buf)[0] = cpu_to_le16 (dum_hcd->port_status);
1955 ((__le16 *) buf)[1] = cpu_to_le16 (dum_hcd->port_status >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 break;
1957 case SetHubFeature:
1958 retval = -EPIPE;
1959 break;
1960 case SetPortFeature:
1961 switch (wValue) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001962 case USB_PORT_FEAT_LINK_STATE:
1963 if (hcd->speed != HCD_USB3) {
1964 dev_dbg(dummy_dev(dum_hcd),
1965 "USB_PORT_FEAT_LINK_STATE req not "
1966 "supported for USB 2.0 roothub\n");
1967 goto error;
1968 }
1969 /*
1970 * Since this is dummy we don't have an actual link so
1971 * there is nothing to do for the SET_LINK_STATE cmd
1972 */
1973 break;
1974 case USB_PORT_FEAT_U1_TIMEOUT:
1975 case USB_PORT_FEAT_U2_TIMEOUT:
1976 /* TODO: add suspend/resume support! */
1977 if (hcd->speed != HCD_USB3) {
1978 dev_dbg(dummy_dev(dum_hcd),
1979 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
1980 "supported for USB 2.0 roothub\n");
1981 goto error;
1982 }
1983 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001985 /* Applicable only for USB2.0 hub */
1986 if (hcd->speed == HCD_USB3) {
1987 dev_dbg(dummy_dev(dum_hcd),
1988 "USB_PORT_FEAT_SUSPEND req not "
1989 "supported for USB 3.0 roothub\n");
1990 goto error;
1991 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001992 if (dum_hcd->active) {
1993 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001994
1995 /* HNP would happen here; for now we
1996 * assume b_bus_req is always true.
1997 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001998 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001999 if (((1 << USB_DEVICE_B_HNP_ENABLE)
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002000 & dum_hcd->dum->devstatus) != 0)
2001 dev_dbg(dummy_dev(dum_hcd),
Alan Stern5742b0c2005-05-02 11:25:17 -04002002 "no HNP yet!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 }
2004 break;
Alan Sternf1c39fa2005-05-03 16:24:04 -04002005 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002006 if (hcd->speed == HCD_USB3)
2007 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
2008 else
2009 dum_hcd->port_status |= USB_PORT_STAT_POWER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002010 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002011 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002012 case USB_PORT_FEAT_BH_PORT_RESET:
2013 /* Applicable only for USB3.0 hub */
2014 if (hcd->speed != HCD_USB3) {
2015 dev_dbg(dummy_dev(dum_hcd),
2016 "USB_PORT_FEAT_BH_PORT_RESET req not "
2017 "supported for USB 2.0 roothub\n");
2018 goto error;
2019 }
2020 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 case USB_PORT_FEAT_RESET:
Alan Sternf1c39fa2005-05-03 16:24:04 -04002022 /* if it's already enabled, disable */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002023 if (hcd->speed == HCD_USB3) {
2024 dum_hcd->port_status = 0;
2025 dum_hcd->port_status =
2026 (USB_SS_PORT_STAT_POWER |
2027 USB_PORT_STAT_CONNECTION |
2028 USB_PORT_STAT_RESET);
2029 } else
2030 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
Alan Sternf1c39fa2005-05-03 16:24:04 -04002031 | USB_PORT_STAT_LOW_SPEED
2032 | USB_PORT_STAT_HIGH_SPEED);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002033 /*
2034 * We want to reset device status. All but the
2035 * Self powered feature
2036 */
2037 dum_hcd->dum->devstatus &=
2038 (1 << USB_DEVICE_SELF_POWERED);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002039 /*
2040 * FIXME USB3.0: what is the correct reset signaling
2041 * interval? Is it still 50msec as for HS?
2042 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002043 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002044 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 default:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002046 if (hcd->speed == HCD_USB3) {
2047 if ((dum_hcd->port_status &
2048 USB_SS_PORT_STAT_POWER) != 0) {
2049 dum_hcd->port_status |= (1 << wValue);
2050 set_link_state(dum_hcd);
2051 }
2052 } else
2053 if ((dum_hcd->port_status &
2054 USB_PORT_STAT_POWER) != 0) {
2055 dum_hcd->port_status |= (1 << wValue);
2056 set_link_state(dum_hcd);
2057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
2059 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002060 case GetPortErrorCount:
2061 if (hcd->speed != HCD_USB3) {
2062 dev_dbg(dummy_dev(dum_hcd),
2063 "GetPortErrorCount req not "
2064 "supported for USB 2.0 roothub\n");
2065 goto error;
2066 }
2067 /* We'll always return 0 since this is a dummy hub */
2068 *(__le32 *) buf = cpu_to_le32(0);
2069 break;
2070 case SetHubDepth:
2071 if (hcd->speed != HCD_USB3) {
2072 dev_dbg(dummy_dev(dum_hcd),
2073 "SetHubDepth req not supported for "
2074 "USB 2.0 roothub\n");
2075 goto error;
2076 }
2077 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002079 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 "hub control req%04x v%04x i%04x l%d\n",
2081 typeReq, wValue, wIndex, wLength);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002082error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 /* "protocol stall" on error */
2084 retval = -EPIPE;
2085 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002086 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Stern685eb932005-05-03 16:27:26 -04002087
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002088 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
Alan Stern685eb932005-05-03 16:27:26 -04002089 usb_hcd_poll_rh_status (hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return retval;
2091}
2092
Alan Stern0c0382e2005-10-13 17:08:02 -04002093static int dummy_bus_suspend (struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002094{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002095 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002096
Harvey Harrison441b62c2008-03-03 16:08:34 -08002097 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern3cf0a222005-11-29 12:08:15 -05002098
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002099 spin_lock_irq(&dum_hcd->dum->lock);
2100 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2101 set_link_state(dum_hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002102 hcd->state = HC_STATE_SUSPENDED;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002103 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern391eca92005-05-10 15:34:16 -04002104 return 0;
2105}
2106
Alan Stern0c0382e2005-10-13 17:08:02 -04002107static int dummy_bus_resume (struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002108{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002109 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002110 int rc = 0;
2111
Harvey Harrison441b62c2008-03-03 16:08:34 -08002112 dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002113
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002114 spin_lock_irq(&dum_hcd->dum->lock);
Alan Stern541c7d42010-06-22 16:39:10 -04002115 if (!HCD_HW_ACCESSIBLE(hcd)) {
Alan Sterncfa59da2007-06-21 16:25:35 -04002116 rc = -ESHUTDOWN;
Alan Stern3cf0a222005-11-29 12:08:15 -05002117 } else {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002118 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2119 set_link_state(dum_hcd);
2120 if (!list_empty(&dum_hcd->urbp_list))
2121 mod_timer(&dum_hcd->timer, jiffies);
Alan Stern3cf0a222005-11-29 12:08:15 -05002122 hcd->state = HC_STATE_RUNNING;
2123 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002124 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern3cf0a222005-11-29 12:08:15 -05002125 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002126}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128/*-------------------------------------------------------------------------*/
2129
2130static inline ssize_t
2131show_urb (char *buf, size_t size, struct urb *urb)
2132{
2133 int ep = usb_pipeendpoint (urb->pipe);
2134
2135 return snprintf (buf, size,
2136 "urb/%p %s ep%d%s%s len %d/%d\n",
2137 urb,
2138 ({ char *s;
2139 switch (urb->dev->speed) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002140 case USB_SPEED_LOW:
2141 s = "ls";
2142 break;
2143 case USB_SPEED_FULL:
2144 s = "fs";
2145 break;
2146 case USB_SPEED_HIGH:
2147 s = "hs";
2148 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002149 case USB_SPEED_SUPER:
2150 s = "ss";
2151 break;
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002152 default:
2153 s = "?";
2154 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }; s; }),
2156 ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
2157 ({ char *s; \
2158 switch (usb_pipetype (urb->pipe)) { \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002159 case PIPE_CONTROL: \
2160 s = ""; \
2161 break; \
2162 case PIPE_BULK: \
2163 s = "-bulk"; \
2164 break; \
2165 case PIPE_INTERRUPT: \
2166 s = "-int"; \
2167 break; \
2168 default: \
2169 s = "-iso"; \
2170 break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }; s;}),
2172 urb->actual_length, urb->transfer_buffer_length);
2173}
2174
2175static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -04002176show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
2178 struct usb_hcd *hcd = dev_get_drvdata (dev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002179 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 struct urbp *urbp;
2181 size_t size = 0;
2182 unsigned long flags;
2183
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002184 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2185 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 size_t temp;
2187
2188 temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
2189 buf += temp;
2190 size += temp;
2191 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002192 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194 return size;
2195}
2196static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
2197
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002198static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2199{
2200 init_timer(&dum_hcd->timer);
2201 dum_hcd->timer.function = dummy_timer;
2202 dum_hcd->timer.data = (unsigned long)dum_hcd;
2203 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2204 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2205 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2206 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2207 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2208#ifdef CONFIG_USB_OTG
2209 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2210#endif
2211 return 0;
2212
2213 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2214 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2215}
2216
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002217static int dummy_start(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002219 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
2221 /*
2222 * MASTER side init ... we emulate a root hub that'll only ever
2223 * talk to one device (the slave side). Also appears in sysfs,
2224 * just like more familiar pci-based HCDs.
2225 */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002226 if (!usb_hcd_is_primary_hcd(hcd))
2227 return dummy_start_ss(dum_hcd);
2228
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002229 spin_lock_init(&dum_hcd->dum->lock);
2230 init_timer(&dum_hcd->timer);
2231 dum_hcd->timer.function = dummy_timer;
2232 dum_hcd->timer.data = (unsigned long)dum_hcd;
2233 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002235 INIT_LIST_HEAD(&dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Alan Sterncaf29f62007-12-06 11:10:39 -05002237 hcd->power_budget = POWER_BUDGET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 hcd->state = HC_STATE_RUNNING;
Alan Stern685eb932005-05-03 16:27:26 -04002239 hcd->uses_new_polling = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Alan Stern5742b0c2005-05-02 11:25:17 -04002241#ifdef CONFIG_USB_OTG
2242 hcd->self.otg_port = 1;
2243#endif
2244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002246 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247}
2248
2249static void dummy_stop (struct usb_hcd *hcd)
2250{
2251 struct dummy *dum;
2252
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002253 dum = (hcd_to_dummy_hcd(hcd))->dum;
2254 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2255 usb_gadget_unregister_driver(dum->driver);
2256 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257}
2258
2259/*-------------------------------------------------------------------------*/
2260
2261static int dummy_h_get_frame (struct usb_hcd *hcd)
2262{
2263 return dummy_g_get_frame (NULL);
2264}
2265
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002266static int dummy_setup(struct usb_hcd *hcd)
2267{
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01002268 hcd->self.sg_tablesize = ~0;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002269 if (usb_hcd_is_primary_hcd(hcd)) {
2270 the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
2271 the_controller.hs_hcd->dum = &the_controller;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002272 /*
2273 * Mark the first roothub as being USB 2.0.
2274 * The USB 3.0 roothub will be registered later by
2275 * dummy_hcd_probe()
2276 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002277 hcd->speed = HCD_USB2;
2278 hcd->self.root_hub->speed = USB_SPEED_HIGH;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002279 } else {
2280 the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
2281 the_controller.ss_hcd->dum = &the_controller;
2282 hcd->speed = HCD_USB3;
2283 hcd->self.root_hub->speed = USB_SPEED_SUPER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002284 }
2285 return 0;
2286}
2287
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002288/* Change a group of bulk endpoints to support multiple stream IDs */
Sebastian Andrzej Siewiord81f3e42012-01-09 13:15:00 +01002289static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002290 struct usb_host_endpoint **eps, unsigned int num_eps,
2291 unsigned int num_streams, gfp_t mem_flags)
2292{
2293 if (hcd->speed != HCD_USB3)
2294 dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
2295 "%s() - ERROR! Not supported for USB2.0 roothub\n",
2296 __func__);
2297 return 0;
2298}
2299
2300/* Reverts a group of bulk endpoints back to not using stream IDs. */
Sebastian Andrzej Siewiord81f3e42012-01-09 13:15:00 +01002301static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002302 struct usb_host_endpoint **eps, unsigned int num_eps,
2303 gfp_t mem_flags)
2304{
2305 if (hcd->speed != HCD_USB3)
2306 dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
2307 "%s() - ERROR! Not supported for USB2.0 roothub\n",
2308 __func__);
2309 return 0;
2310}
2311
2312static struct hc_driver dummy_hcd = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 .description = (char *) driver_name,
2314 .product_desc = "Dummy host controller",
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002315 .hcd_priv_size = sizeof(struct dummy_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002317 .flags = HCD_USB3 | HCD_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002319 .reset = dummy_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 .start = dummy_start,
2321 .stop = dummy_stop,
2322
2323 .urb_enqueue = dummy_urb_enqueue,
2324 .urb_dequeue = dummy_urb_dequeue,
2325
2326 .get_frame_number = dummy_h_get_frame,
2327
2328 .hub_status_data = dummy_hub_status,
2329 .hub_control = dummy_hub_control,
Alan Stern0c0382e2005-10-13 17:08:02 -04002330 .bus_suspend = dummy_bus_suspend,
2331 .bus_resume = dummy_bus_resume,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002332
2333 .alloc_streams = dummy_alloc_streams,
2334 .free_streams = dummy_free_streams,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335};
2336
Alan Stern8364d6b2005-11-14 12:16:30 -05002337static int dummy_hcd_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002339 struct usb_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002340 struct usb_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 int retval;
2342
Alan Stern8364d6b2005-11-14 12:16:30 -05002343 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002345 if (!mod_data.is_super_speed)
2346 dummy_hcd.flags = HCD_USB2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002347 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2348 if (!hs_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return -ENOMEM;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002350 hs_hcd->has_tt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002352 retval = usb_add_hcd(hs_hcd, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 if (retval != 0) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002354 usb_put_hcd(hs_hcd);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002355 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002357
2358 if (mod_data.is_super_speed) {
2359 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2360 dev_name(&pdev->dev), hs_hcd);
2361 if (!ss_hcd) {
2362 retval = -ENOMEM;
2363 goto dealloc_usb2_hcd;
2364 }
2365
2366 retval = usb_add_hcd(ss_hcd, 0, 0);
2367 if (retval)
2368 goto put_usb3_hcd;
2369 }
2370 return 0;
2371
2372put_usb3_hcd:
2373 usb_put_hcd(ss_hcd);
2374dealloc_usb2_hcd:
2375 usb_put_hcd(hs_hcd);
2376 the_controller.hs_hcd = the_controller.ss_hcd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 return retval;
2378}
2379
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002380static int dummy_hcd_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002382 struct dummy *dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002384 dum = (hcd_to_dummy_hcd(platform_get_drvdata(pdev)))->dum;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002385
2386 if (dum->ss_hcd) {
2387 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2388 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2389 }
2390
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002391 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2392 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002393
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002394 the_controller.hs_hcd = NULL;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002395 the_controller.ss_hcd = NULL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002396
Alan Sternd9b76252005-05-03 16:15:43 -04002397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398}
2399
Alan Stern8364d6b2005-11-14 12:16:30 -05002400static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
Alan Stern391eca92005-05-10 15:34:16 -04002401{
2402 struct usb_hcd *hcd;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002403 struct dummy_hcd *dum_hcd;
Alan Stern3cf0a222005-11-29 12:08:15 -05002404 int rc = 0;
Alan Stern391eca92005-05-10 15:34:16 -04002405
Harvey Harrison441b62c2008-03-03 16:08:34 -08002406 dev_dbg (&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002407
Alan Stern3cf0a222005-11-29 12:08:15 -05002408 hcd = platform_get_drvdata (pdev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002409 dum_hcd = hcd_to_dummy_hcd(hcd);
2410 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern3cf0a222005-11-29 12:08:15 -05002411 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2412 rc = -EBUSY;
2413 } else
2414 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2415 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002416}
2417
Alan Stern8364d6b2005-11-14 12:16:30 -05002418static int dummy_hcd_resume (struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04002419{
2420 struct usb_hcd *hcd;
2421
Harvey Harrison441b62c2008-03-03 16:08:34 -08002422 dev_dbg (&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002423
Alan Stern3cf0a222005-11-29 12:08:15 -05002424 hcd = platform_get_drvdata (pdev);
2425 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Alan Stern391eca92005-05-10 15:34:16 -04002426 usb_hcd_poll_rh_status (hcd);
2427 return 0;
2428}
2429
Russell King3ae5eae2005-11-09 22:32:44 +00002430static struct platform_driver dummy_hcd_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04002431 .probe = dummy_hcd_probe,
2432 .remove = dummy_hcd_remove,
Alan Stern391eca92005-05-10 15:34:16 -04002433 .suspend = dummy_hcd_suspend,
2434 .resume = dummy_hcd_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002435 .driver = {
2436 .name = (char *) driver_name,
2437 .owner = THIS_MODULE,
2438 },
Alan Sternd9b76252005-05-03 16:15:43 -04002439};
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441/*-------------------------------------------------------------------------*/
2442
Alan Sterna89a2cd2008-04-07 15:03:25 -04002443static struct platform_device *the_udc_pdev;
2444static struct platform_device *the_hcd_pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
2446static int __init init (void)
2447{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002448 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
2450 if (usb_disabled ())
2451 return -ENODEV;
Alan Sternd9b76252005-05-03 16:15:43 -04002452
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +03002453 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2454 return -EINVAL;
2455
Alan Sterna89a2cd2008-04-07 15:03:25 -04002456 the_hcd_pdev = platform_device_alloc(driver_name, -1);
2457 if (!the_hcd_pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 return retval;
Alan Sterna89a2cd2008-04-07 15:03:25 -04002459 the_udc_pdev = platform_device_alloc(gadget_name, -1);
2460 if (!the_udc_pdev)
2461 goto err_alloc_udc;
Alan Sternd9b76252005-05-03 16:15:43 -04002462
Alan Sterna89a2cd2008-04-07 15:03:25 -04002463 retval = platform_driver_register(&dummy_hcd_driver);
2464 if (retval < 0)
2465 goto err_register_hcd_driver;
2466 retval = platform_driver_register(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002467 if (retval < 0)
2468 goto err_register_udc_driver;
2469
Alan Sterna89a2cd2008-04-07 15:03:25 -04002470 retval = platform_device_add(the_hcd_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002471 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002472 goto err_add_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002473 if (!the_controller.hs_hcd ||
2474 (!the_controller.ss_hcd && mod_data.is_super_speed)) {
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002475 /*
2476 * The hcd was added successfully but its probe function failed
2477 * for some reason.
2478 */
2479 retval = -EINVAL;
2480 goto err_add_udc;
2481 }
Alan Sterna89a2cd2008-04-07 15:03:25 -04002482 retval = platform_device_add(the_udc_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002483 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002484 goto err_add_udc;
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002485 if (!platform_get_drvdata(the_udc_pdev)) {
2486 /*
2487 * The udc was added successfully but its probe function failed
2488 * for some reason.
2489 */
2490 retval = -EINVAL;
2491 goto err_probe_udc;
2492 }
Alan Sternd9b76252005-05-03 16:15:43 -04002493 return retval;
2494
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002495err_probe_udc:
2496 platform_device_del(the_udc_pdev);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002497err_add_udc:
2498 platform_device_del(the_hcd_pdev);
2499err_add_hcd:
2500 platform_driver_unregister(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002501err_register_udc_driver:
Alan Sterna89a2cd2008-04-07 15:03:25 -04002502 platform_driver_unregister(&dummy_hcd_driver);
2503err_register_hcd_driver:
2504 platform_device_put(the_udc_pdev);
2505err_alloc_udc:
2506 platform_device_put(the_hcd_pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 return retval;
2508}
2509module_init (init);
2510
2511static void __exit cleanup (void)
2512{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002513 platform_device_unregister(the_udc_pdev);
2514 platform_device_unregister(the_hcd_pdev);
2515 platform_driver_unregister(&dummy_udc_driver);
2516 platform_driver_unregister(&dummy_hcd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518module_exit (cleanup);