blob: 1c69c760408ebaaa98c0e50f4da5b181763abc45 [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>
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +010045#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/unaligned.h>
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define DRIVER_DESC "USB Host+Gadget Emulator"
Alan Stern391eca92005-05-10 15:34:16 -040050#define DRIVER_VERSION "02 May 2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Alan Sterncaf29f62007-12-06 11:10:39 -050052#define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */
53
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +010054static const char driver_name[] = "dummy_hcd";
55static const char driver_desc[] = "USB Host+Gadget Emulator";
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +010057static const char gadget_name[] = "dummy_udc";
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +010059MODULE_DESCRIPTION(DRIVER_DESC);
60MODULE_AUTHOR("David Brownell");
61MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +030063struct dummy_hcd_module_parameters {
64 bool is_super_speed;
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +030065 bool is_high_speed;
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +010066 unsigned int num;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +030067};
68
69static struct dummy_hcd_module_parameters mod_data = {
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +030070 .is_super_speed = false,
71 .is_high_speed = true,
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +010072 .num = 1,
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");
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +010078module_param_named(num, mod_data.num, uint, S_IRUGO);
79MODULE_PARM_DESC(num, "number of emulated controllers");
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*-------------------------------------------------------------------------*/
81
82/* gadget side driver data structres */
83struct dummy_ep {
84 struct list_head queue;
85 unsigned long last_io; /* jiffies timestamp */
86 struct usb_gadget *gadget;
87 const struct usb_endpoint_descriptor *desc;
88 struct usb_ep ep;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +010089 unsigned halted:1;
90 unsigned wedged:1;
91 unsigned already_seen:1;
92 unsigned setup_stage:1;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +010093 unsigned stream_en:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094};
95
96struct dummy_request {
97 struct list_head queue; /* ep's requests */
98 struct usb_request req;
99};
100
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100101static inline struct dummy_ep *usb_ep_to_dummy_ep(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100103 return container_of(_ep, struct dummy_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106static inline struct dummy_request *usb_request_to_dummy_request
107 (struct usb_request *_req)
108{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100109 return container_of(_req, struct dummy_request, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112/*-------------------------------------------------------------------------*/
113
114/*
115 * Every device has ep0 for control requests, plus up to 30 more endpoints,
116 * in one of two types:
117 *
118 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
119 * number can be changed. Names like "ep-a" are used for this type.
120 *
121 * - Fixed Function: in other cases. some characteristics may be mutable;
122 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
123 *
124 * Gadget drivers are responsible for not setting up conflicting endpoint
125 * configurations, illegal or unsupported packet lengths, and so on.
126 */
127
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100128static const char ep0name[] = "ep0";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100130static const char *const ep_name[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 ep0name, /* everyone has ep0 */
132
Sebastian Andrzej Siewior1d166382012-11-20 13:23:15 +0100133 /* act like a pxa250: fifteen fixed function endpoints */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 "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",
Sebastian Andrzej Siewior1d166382012-11-20 13:23:15 +0100141
142 /* and now some generic EPs so we have enough in multi config */
143 "ep3out", "ep4in", "ep5out", "ep6out", "ep7in", "ep8out", "ep9in",
144 "ep10out", "ep11out", "ep12in", "ep13out", "ep14in", "ep15out",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
Tobias Klauser52950ed2005-12-11 16:20:08 +0100146#define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Alan Sternd9b76252005-05-03 16:15:43 -0400148/*-------------------------------------------------------------------------*/
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#define FIFO_SIZE 64
151
152struct urbp {
153 struct urb *urb;
154 struct list_head urbp_list;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +0100155 struct sg_mapping_iter miter;
156 u32 miter_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157};
158
Alan Stern391eca92005-05-10 15:34:16 -0400159
160enum dummy_rh_state {
161 DUMMY_RH_RESET,
162 DUMMY_RH_SUSPENDED,
163 DUMMY_RH_RUNNING
164};
165
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300166struct dummy_hcd {
167 struct dummy *dum;
168 enum dummy_rh_state rh_state;
169 struct timer_list timer;
170 u32 port_status;
171 u32 old_status;
172 unsigned long re_timeout;
173
174 struct usb_device *udev;
175 struct list_head urbp_list;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100176 u32 stream_en_ep;
177 u8 num_stream[30 / 2];
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300178
179 unsigned active:1;
180 unsigned old_active:1;
181 unsigned resuming:1;
182};
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184struct dummy {
185 spinlock_t lock;
186
187 /*
188 * SLAVE/GADGET side support
189 */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100190 struct dummy_ep ep[DUMMY_ENDPOINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 int address;
192 struct usb_gadget gadget;
193 struct usb_gadget_driver *driver;
194 struct dummy_request fifo_req;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100195 u8 fifo_buf[FIFO_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 u16 devstatus;
Alan Stern391eca92005-05-10 15:34:16 -0400197 unsigned udc_suspended:1;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400198 unsigned pullup:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 /*
201 * MASTER/HOST side support
202 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300203 struct dummy_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300204 struct dummy_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205};
206
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300207static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300209 return (struct dummy_hcd *) (hcd->hcd_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300212static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 return container_of((void *) dum, struct usb_hcd, hcd_priv);
215}
216
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300217static inline struct device *dummy_dev(struct dummy_hcd *dum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300219 return dummy_hcd_to_hcd(dum)->self.controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100222static inline struct device *udc_dev(struct dummy *dum)
Alan Sternd9b76252005-05-03 16:15:43 -0400223{
224 return dum->gadget.dev.parent;
225}
226
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100227static inline struct dummy *ep_to_dummy(struct dummy_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100229 return container_of(ep->gadget, struct dummy, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300232static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300234 struct dummy *dum = container_of(gadget, struct dummy, gadget);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300235 if (dum->gadget.speed == USB_SPEED_SUPER)
236 return dum->ss_hcd;
237 else
238 return dum->hs_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100241static inline struct dummy *gadget_dev_to_dummy(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100243 return container_of(dev, struct dummy, gadget.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/*-------------------------------------------------------------------------*/
247
Alan Sternf1c39fa2005-05-03 16:24:04 -0400248/* SLAVE/GADGET SIDE UTILITY ROUTINES */
249
250/* called with spinlock held */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100251static void nuke(struct dummy *dum, struct dummy_ep *ep)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400252{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100253 while (!list_empty(&ep->queue)) {
Alan Sternf1c39fa2005-05-03 16:24:04 -0400254 struct dummy_request *req;
255
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100256 req = list_entry(ep->queue.next, struct dummy_request, queue);
257 list_del_init(&req->queue);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400258 req->req.status = -ESHUTDOWN;
259
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100260 spin_unlock(&dum->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +0200261 usb_gadget_giveback_request(&ep->ep, &req->req);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100262 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400263 }
264}
265
266/* caller must hold lock */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100267static void stop_activity(struct dummy *dum)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400268{
269 struct dummy_ep *ep;
270
271 /* prevent any more requests */
272 dum->address = 0;
273
274 /* The timer is left running so that outstanding URBs can fail */
275
276 /* nuke any pending requests first, so driver i/o is quiesced */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100277 list_for_each_entry(ep, &dum->gadget.ep_list, ep.ep_list)
278 nuke(dum, ep);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400279
280 /* driver now does any non-usb quiescing necessary */
281}
282
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300283/**
284 * set_link_state_by_speed() - Sets the current state of the link according to
285 * the hcd speed
286 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
287 *
288 * This function updates the port_status according to the link state and the
289 * speed of the hcd.
290 */
291static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
292{
293 struct dummy *dum = dum_hcd->dum;
294
295 if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
296 if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
297 dum_hcd->port_status = 0;
298 } else if (!dum->pullup || dum->udc_suspended) {
299 /* UDC suspend must cause a disconnect */
300 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
301 USB_PORT_STAT_ENABLE);
302 if ((dum_hcd->old_status &
303 USB_PORT_STAT_CONNECTION) != 0)
304 dum_hcd->port_status |=
305 (USB_PORT_STAT_C_CONNECTION << 16);
306 } else {
307 /* device is connected and not suspended */
308 dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
309 USB_PORT_STAT_SPEED_5GBPS) ;
310 if ((dum_hcd->old_status &
311 USB_PORT_STAT_CONNECTION) == 0)
312 dum_hcd->port_status |=
313 (USB_PORT_STAT_C_CONNECTION << 16);
314 if ((dum_hcd->port_status &
315 USB_PORT_STAT_ENABLE) == 1 &&
316 (dum_hcd->port_status &
317 USB_SS_PORT_LS_U0) == 1 &&
318 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
319 dum_hcd->active = 1;
320 }
321 } else {
322 if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
323 dum_hcd->port_status = 0;
324 } else if (!dum->pullup || dum->udc_suspended) {
325 /* UDC suspend must cause a disconnect */
326 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
327 USB_PORT_STAT_ENABLE |
328 USB_PORT_STAT_LOW_SPEED |
329 USB_PORT_STAT_HIGH_SPEED |
330 USB_PORT_STAT_SUSPEND);
331 if ((dum_hcd->old_status &
332 USB_PORT_STAT_CONNECTION) != 0)
333 dum_hcd->port_status |=
334 (USB_PORT_STAT_C_CONNECTION << 16);
335 } else {
336 dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
337 if ((dum_hcd->old_status &
338 USB_PORT_STAT_CONNECTION) == 0)
339 dum_hcd->port_status |=
340 (USB_PORT_STAT_C_CONNECTION << 16);
341 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
342 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
343 else if ((dum_hcd->port_status &
344 USB_PORT_STAT_SUSPEND) == 0 &&
345 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
346 dum_hcd->active = 1;
347 }
348 }
349}
350
Alan Sternf1c39fa2005-05-03 16:24:04 -0400351/* caller must hold lock */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300352static void set_link_state(struct dummy_hcd *dum_hcd)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400353{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300354 struct dummy *dum = dum_hcd->dum;
355
356 dum_hcd->active = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300357 if (dum->pullup)
358 if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
359 dum->gadget.speed != USB_SPEED_SUPER) ||
360 (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
361 dum->gadget.speed == USB_SPEED_SUPER))
362 return;
Alan Stern391eca92005-05-10 15:34:16 -0400363
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300364 set_link_state_by_speed(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400365
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300366 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
367 dum_hcd->active)
368 dum_hcd->resuming = 0;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400369
Alan Stern84804842014-11-06 14:27:55 +0800370 /* Currently !connected or in reset */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300371 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
372 (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
Alan Stern84804842014-11-06 14:27:55 +0800373 unsigned disconnect = USB_PORT_STAT_CONNECTION &
374 dum_hcd->old_status & (~dum_hcd->port_status);
375 unsigned reset = USB_PORT_STAT_RESET &
376 (~dum_hcd->old_status) & dum_hcd->port_status;
377
378 /* Report reset and disconnect events to the driver */
379 if (dum->driver && (disconnect || reset)) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300380 stop_activity(dum);
381 spin_unlock(&dum->lock);
Alan Stern84804842014-11-06 14:27:55 +0800382 if (reset)
383 usb_gadget_udc_reset(&dum->gadget, dum->driver);
384 else
385 dum->driver->disconnect(&dum->gadget);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300386 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400387 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300388 } else if (dum_hcd->active != dum_hcd->old_active) {
389 if (dum_hcd->old_active && dum->driver->suspend) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300390 spin_unlock(&dum->lock);
391 dum->driver->suspend(&dum->gadget);
392 spin_lock(&dum->lock);
393 } else if (!dum_hcd->old_active && dum->driver->resume) {
394 spin_unlock(&dum->lock);
395 dum->driver->resume(&dum->gadget);
396 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400397 }
398 }
399
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300400 dum_hcd->old_status = dum_hcd->port_status;
401 dum_hcd->old_active = dum_hcd->active;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400402}
403
404/*-------------------------------------------------------------------------*/
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/* SLAVE/GADGET SIDE DRIVER
407 *
408 * This only tracks gadget state. All the work is done when the host
409 * side tries some (emulated) i/o operation. Real device controller
410 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
411 */
412
413#define is_enabled(dum) \
414 (dum->port_status & USB_PORT_STAT_ENABLE)
415
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100416static int dummy_enable(struct usb_ep *_ep,
417 const struct usb_endpoint_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300420 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct dummy_ep *ep;
422 unsigned max;
423 int retval;
424
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100425 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (!_ep || !desc || ep->desc || _ep->name == ep0name
427 || desc->bDescriptorType != USB_DT_ENDPOINT)
428 return -EINVAL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100429 dum = ep_to_dummy(ep);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300430 if (!dum->driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return -ESHUTDOWN;
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200432
433 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300434 if (!is_enabled(dum_hcd))
435 return -ESHUTDOWN;
436
437 /*
438 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
439 * maximum packet size.
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300440 * For SS devices the wMaxPacketSize is limited by 1024.
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300441 */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700442 max = usb_endpoint_maxp(desc) & 0x7ff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 /* drivers must not request bad settings, since lower levels
445 * (hardware or its drivers) may not check. some endpoints
446 * can't do iso, many have maxpacket limitations, etc.
447 *
448 * since this "hardware" driver is here to help debugging, we
449 * have some extra sanity checks. (there could be more though,
450 * especially for "ep9out" style fixed function ones.)
451 */
452 retval = -EINVAL;
Sebastian Andrzej Siewior59f08e62012-01-12 12:53:14 +0100453 switch (usb_endpoint_type(desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 case USB_ENDPOINT_XFER_BULK:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100455 if (strstr(ep->ep.name, "-iso")
456 || strstr(ep->ep.name, "-int")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 goto done;
458 }
459 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300460 case USB_SPEED_SUPER:
461 if (max == 1024)
462 break;
463 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 case USB_SPEED_HIGH:
465 if (max == 512)
466 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700467 goto done;
468 case USB_SPEED_FULL:
469 if (max == 8 || max == 16 || max == 32 || max == 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /* we'll fake any legal size */
471 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700472 /* save a return statement */
473 default:
474 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476 break;
477 case USB_ENDPOINT_XFER_INT:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100478 if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 goto done;
480 /* real hardware might not handle all packet sizes */
481 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300482 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 case USB_SPEED_HIGH:
484 if (max <= 1024)
485 break;
486 /* save a return statement */
487 case USB_SPEED_FULL:
488 if (max <= 64)
489 break;
490 /* save a return statement */
491 default:
492 if (max <= 8)
493 break;
494 goto done;
495 }
496 break;
497 case USB_ENDPOINT_XFER_ISOC:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100498 if (strstr(ep->ep.name, "-bulk")
499 || strstr(ep->ep.name, "-int"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 goto done;
501 /* real hardware might not handle all packet sizes */
502 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300503 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 case USB_SPEED_HIGH:
505 if (max <= 1024)
506 break;
507 /* save a return statement */
508 case USB_SPEED_FULL:
509 if (max <= 1023)
510 break;
511 /* save a return statement */
512 default:
513 goto done;
514 }
515 break;
516 default:
517 /* few chips support control except on ep0 */
518 goto done;
519 }
520
521 _ep->maxpacket = max;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100522 if (usb_ss_max_streams(_ep->comp_desc)) {
523 if (!usb_endpoint_xfer_bulk(desc)) {
524 dev_err(udc_dev(dum), "Can't enable stream support on "
525 "non-bulk ep %s\n", _ep->name);
526 return -EINVAL;
527 }
528 ep->stream_en = 1;
529 }
Sebastian Andrzej Siewior3cf0ad02012-01-25 11:51:19 +0100530 ep->desc = desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100532 dev_dbg(udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 _ep->name,
534 desc->bEndpointAddress & 0x0f,
535 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
536 ({ char *val;
Sebastian Andrzej Siewior59f08e62012-01-12 12:53:14 +0100537 switch (usb_endpoint_type(desc)) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +0300538 case USB_ENDPOINT_XFER_BULK:
539 val = "bulk";
540 break;
541 case USB_ENDPOINT_XFER_ISOC:
542 val = "iso";
543 break;
544 case USB_ENDPOINT_XFER_INT:
545 val = "intr";
546 break;
547 default:
548 val = "ctrl";
549 break;
Joe Perches2b84f922013-10-08 16:01:37 -0700550 } val; }),
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100551 max, ep->stream_en ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 /* at this point real hardware should be NAKing transfers
554 * to that endpoint, until a buffer is queued to it.
555 */
Alan Stern851a5262008-08-14 15:48:30 -0400556 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 retval = 0;
558done:
559 return retval;
560}
561
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100562static int dummy_disable(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 struct dummy_ep *ep;
565 struct dummy *dum;
566 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100568 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (!_ep || !ep->desc || _ep->name == ep0name)
570 return -EINVAL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100571 dum = ep_to_dummy(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100573 spin_lock_irqsave(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 ep->desc = NULL;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100575 ep->stream_en = 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100576 nuke(dum, ep);
577 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100579 dev_dbg(udc_dev(dum), "disabled %s\n", _ep->name);
Julia Lawall849b1332014-05-19 06:31:07 +0200580 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100583static struct usb_request *dummy_alloc_request(struct usb_ep *_ep,
584 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 struct dummy_ep *ep;
587 struct dummy_request *req;
588
589 if (!_ep)
590 return NULL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100591 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800593 req = kzalloc(sizeof(*req), mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (!req)
595 return NULL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100596 INIT_LIST_HEAD(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return &req->req;
598}
599
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100600static void dummy_free_request(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 struct dummy_request *req;
603
Sebastian Andrzej Siewiorf99987b2012-02-09 09:24:59 +0100604 if (!_ep || !_req) {
Sasha Levin72688dc2012-05-11 06:39:37 +0200605 WARN_ON(1);
Sebastian Andrzej Siewior20edfbb2012-01-25 15:18:58 +0100606 return;
Sebastian Andrzej Siewiorf99987b2012-02-09 09:24:59 +0100607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100609 req = usb_request_to_dummy_request(_req);
610 WARN_ON(!list_empty(&req->queue));
611 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100614static void fifo_complete(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616}
617
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100618static int dummy_queue(struct usb_ep *_ep, struct usb_request *_req,
Al Viro55016f12005-10-21 03:21:58 -0400619 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 struct dummy_ep *ep;
622 struct dummy_request *req;
623 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300624 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 unsigned long flags;
626
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100627 req = usb_request_to_dummy_request(_req);
628 if (!_req || !list_empty(&req->queue) || !_req->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return -EINVAL;
630
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100631 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (!_ep || (!ep->desc && _ep->name != ep0name))
633 return -EINVAL;
634
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100635 dum = ep_to_dummy(ep);
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200636 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300637 if (!dum->driver || !is_enabled(dum_hcd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return -ESHUTDOWN;
639
640#if 0
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100641 dev_dbg(udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 ep, _req, _ep->name, _req->length, _req->buf);
643#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 _req->status = -EINPROGRESS;
645 _req->actual = 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100646 spin_lock_irqsave(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 /* implement an emulated single-request FIFO */
649 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100650 list_empty(&dum->fifo_req.queue) &&
651 list_empty(&ep->queue) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 _req->length <= FIFO_SIZE) {
653 req = &dum->fifo_req;
654 req->req = *_req;
655 req->req.buf = dum->fifo_buf;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100656 memcpy(dum->fifo_buf, _req->buf, _req->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 req->req.context = dum;
658 req->req.complete = fifo_complete;
659
David Brownellc728df72008-07-26 08:06:24 -0700660 list_add_tail(&req->queue, &ep->queue);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100661 spin_unlock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 _req->actual = _req->length;
663 _req->status = 0;
Michal Sojka304f7e52014-09-24 22:43:19 +0200664 usb_gadget_giveback_request(_ep, _req);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100665 spin_lock(&dum->lock);
David Brownellc728df72008-07-26 08:06:24 -0700666 } else
667 list_add_tail(&req->queue, &ep->queue);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100668 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 /* real hardware would likely enable transfers here, in case
671 * it'd been left NAKing.
672 */
673 return 0;
674}
675
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100676static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 struct dummy_ep *ep;
679 struct dummy *dum;
680 int retval = -EINVAL;
681 unsigned long flags;
682 struct dummy_request *req = NULL;
683
684 if (!_ep || !_req)
685 return retval;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100686 ep = usb_ep_to_dummy_ep(_ep);
687 dum = ep_to_dummy(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 if (!dum->driver)
690 return -ESHUTDOWN;
691
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100692 local_irq_save(flags);
693 spin_lock(&dum->lock);
694 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (&req->req == _req) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100696 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 _req->status = -ECONNRESET;
698 retval = 0;
699 break;
700 }
701 }
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100702 spin_unlock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 if (retval == 0) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100705 dev_dbg(udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 "dequeued req %p from %s, len %d buf %p\n",
707 req, _ep->name, _req->length, _req->buf);
Michal Sojka304f7e52014-09-24 22:43:19 +0200708 usb_gadget_giveback_request(_ep, _req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100710 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return retval;
712}
713
714static int
Alan Stern851a5262008-08-14 15:48:30 -0400715dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 struct dummy_ep *ep;
718 struct dummy *dum;
719
720 if (!_ep)
721 return -EINVAL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100722 ep = usb_ep_to_dummy_ep(_ep);
723 dum = ep_to_dummy(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (!dum->driver)
725 return -ESHUTDOWN;
726 if (!value)
Alan Stern851a5262008-08-14 15:48:30 -0400727 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100729 !list_empty(&ep->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return -EAGAIN;
Alan Stern851a5262008-08-14 15:48:30 -0400731 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 ep->halted = 1;
Alan Stern851a5262008-08-14 15:48:30 -0400733 if (wedged)
734 ep->wedged = 1;
735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /* FIXME clear emulated data toggle too */
737 return 0;
738}
739
Alan Stern851a5262008-08-14 15:48:30 -0400740static int
741dummy_set_halt(struct usb_ep *_ep, int value)
742{
743 return dummy_set_halt_and_wedge(_ep, value, 0);
744}
745
746static int dummy_set_wedge(struct usb_ep *_ep)
747{
748 if (!_ep || _ep->name == ep0name)
749 return -EINVAL;
750 return dummy_set_halt_and_wedge(_ep, 1, 1);
751}
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753static const struct usb_ep_ops dummy_ep_ops = {
754 .enable = dummy_enable,
755 .disable = dummy_disable,
756
757 .alloc_request = dummy_alloc_request,
758 .free_request = dummy_free_request,
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 .queue = dummy_queue,
761 .dequeue = dummy_dequeue,
762
763 .set_halt = dummy_set_halt,
Alan Stern851a5262008-08-14 15:48:30 -0400764 .set_wedge = dummy_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765};
766
767/*-------------------------------------------------------------------------*/
768
769/* there are both host and device side versions of this call ... */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100770static int dummy_g_get_frame(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 struct timeval tv;
773
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100774 do_gettimeofday(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return tv.tv_usec / 1000;
776}
777
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100778static int dummy_wakeup(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300780 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300782 dum_hcd = gadget_to_dummy_hcd(_gadget);
783 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
Alan Stern5742b0c2005-05-02 11:25:17 -0400784 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return -EINVAL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300786 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
Alan Stern391eca92005-05-10 15:34:16 -0400787 return -ENOLINK;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300788 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
789 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -0400790 return -EIO;
791
792 /* FIXME: What if the root hub is suspended but the port isn't? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 /* hub notices our request, issues downstream resume, etc */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300795 dum_hcd->resuming = 1;
796 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
797 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return 0;
799}
800
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100801static int dummy_set_selfpowered(struct usb_gadget *_gadget, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 struct dummy *dum;
804
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100805 dum = gadget_to_dummy_hcd(_gadget)->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (value)
807 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
808 else
809 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
810 return 0;
811}
812
Sebastian Andrzej Siewiord2621272012-01-09 13:14:59 +0100813static void dummy_udc_update_ep0(struct dummy *dum)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200814{
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100815 if (dum->gadget.speed == USB_SPEED_SUPER)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200816 dum->ep[0].ep.maxpacket = 9;
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100817 else
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200818 dum->ep[0].ep.maxpacket = 64;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200819}
820
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100821static int dummy_pullup(struct usb_gadget *_gadget, int value)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400822{
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200823 struct dummy_hcd *dum_hcd;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400824 struct dummy *dum;
825 unsigned long flags;
826
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200827 dum = gadget_dev_to_dummy(&_gadget->dev);
828
829 if (value && dum->driver) {
830 if (mod_data.is_super_speed)
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100831 dum->gadget.speed = dum->driver->max_speed;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200832 else if (mod_data.is_high_speed)
833 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100834 dum->driver->max_speed);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200835 else
836 dum->gadget.speed = USB_SPEED_FULL;
Sebastian Andrzej Siewiord2621272012-01-09 13:14:59 +0100837 dummy_udc_update_ep0(dum);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200838
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100839 if (dum->gadget.speed < dum->driver->max_speed)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200840 dev_dbg(udc_dev(dum), "This device can perform faster"
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100841 " if you connect it to a %s port...\n",
842 usb_speed_string(dum->driver->max_speed));
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200843 }
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200844 dum_hcd = gadget_to_dummy_hcd(_gadget);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200845
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100846 spin_lock_irqsave(&dum->lock, flags);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400847 dum->pullup = (value != 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200848 set_link_state(dum_hcd);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100849 spin_unlock_irqrestore(&dum->lock, flags);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200850
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200851 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Sternf1c39fa2005-05-03 16:24:04 -0400852 return 0;
853}
854
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200855static int dummy_udc_start(struct usb_gadget *g,
856 struct usb_gadget_driver *driver);
Felipe Balbi22835b82014-10-17 12:05:12 -0500857static int dummy_udc_stop(struct usb_gadget *g);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859static const struct usb_gadget_ops dummy_ops = {
860 .get_frame = dummy_g_get_frame,
861 .wakeup = dummy_wakeup,
862 .set_selfpowered = dummy_set_selfpowered,
Alan Sternf1c39fa2005-05-03 16:24:04 -0400863 .pullup = dummy_pullup,
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200864 .udc_start = dummy_udc_start,
865 .udc_stop = dummy_udc_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866};
867
868/*-------------------------------------------------------------------------*/
869
870/* "function" sysfs attribute */
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700871static ssize_t function_show(struct device *dev, struct device_attribute *attr,
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100872 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100874 struct dummy *dum = gadget_dev_to_dummy(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 if (!dum->driver || !dum->driver->function)
877 return 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100878 return scnprintf(buf, PAGE_SIZE, "%s\n", dum->driver->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700880static DEVICE_ATTR_RO(function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882/*-------------------------------------------------------------------------*/
883
884/*
885 * Driver registration/unregistration.
886 *
887 * This is basically hardware-specific; there's usually only one real USB
888 * device (not host) controller since that's how USB devices are intended
889 * to work. So most implementations of these api calls will rely on the
890 * fact that only one driver will ever bind to the hardware. But curious
891 * hardware can be built with discrete components, so the gadget API doesn't
892 * require that assumption.
893 *
894 * For this emulator, it might be convenient to create a usb slave device
895 * for each driver that registers: just add to a big root hub.
896 */
897
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200898static int dummy_udc_start(struct usb_gadget *g,
899 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200901 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
902 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100904 if (driver->max_speed == USB_SPEED_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return -EINVAL;
906
907 /*
908 * SLAVE side init ... the layer above hardware, which
909 * can't enumerate without help from the driver we're binding.
910 */
Alan Stern5742b0c2005-05-02 11:25:17 -0400911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 dum->devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 dum->driver = driver;
Felipe Balbidd7e6dd2014-10-17 11:37:38 -0500914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return 0;
916}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Felipe Balbi22835b82014-10-17 12:05:12 -0500918static int dummy_udc_stop(struct usb_gadget *g)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200920 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
921 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 dum->driver = NULL;
Sebastian Andrzej Siewior25427872011-06-16 20:36:55 +0200924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return 0;
926}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928#undef is_enabled
929
Alan Sternd9b76252005-05-03 16:15:43 -0400930/* The gadget structure is stored inside the hcd structure and will be
931 * released along with it. */
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200932static void init_dummy_udc_hw(struct dummy *dum)
933{
934 int i;
935
936 INIT_LIST_HEAD(&dum->gadget.ep_list);
937 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
938 struct dummy_ep *ep = &dum->ep[i];
939
940 if (!ep_name[i])
941 break;
942 ep->ep.name = ep_name[i];
943 ep->ep.ops = &dummy_ep_ops;
944 list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
945 ep->halted = ep->wedged = ep->already_seen =
946 ep->setup_stage = 0;
Robert Baldygae117e742013-12-13 12:23:38 +0100947 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100948 ep->ep.max_streams = 16;
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200949 ep->last_io = jiffies;
950 ep->gadget = &dum->gadget;
951 ep->desc = NULL;
952 INIT_LIST_HEAD(&ep->queue);
953 }
954
955 dum->gadget.ep0 = &dum->ep[0].ep;
956 list_del_init(&dum->ep[0].ep.ep_list);
957 INIT_LIST_HEAD(&dum->fifo_req.queue);
Sebastian Andrzej Siewiorf8744d42011-06-23 14:26:13 +0200958
959#ifdef CONFIG_USB_OTG
960 dum->gadget.is_otg = 1;
961#endif
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200962}
963
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100964static int dummy_udc_probe(struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -0400965{
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +0100966 struct dummy *dum;
Alan Sternd9b76252005-05-03 16:15:43 -0400967 int rc;
968
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +0100969 dum = *((void **)dev_get_platdata(&pdev->dev));
Alan Sternd9b76252005-05-03 16:15:43 -0400970 dum->gadget.name = gadget_name;
971 dum->gadget.ops = &dummy_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100972 dum->gadget.max_speed = USB_SPEED_SUPER;
Alan Sternd9b76252005-05-03 16:15:43 -0400973
Alan Stern8364d6b2005-11-14 12:16:30 -0500974 dum->gadget.dev.parent = &pdev->dev;
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200975 init_dummy_udc_hw(dum);
976
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300977 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
978 if (rc < 0)
979 goto err_udc;
980
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100981 rc = device_create_file(&dum->gadget.dev, &dev_attr_function);
Alan Sternefd54a32006-09-25 11:55:56 -0400982 if (rc < 0)
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300983 goto err_dev;
984 platform_set_drvdata(pdev, dum);
985 return rc;
986
987err_dev:
988 usb_del_gadget_udc(&dum->gadget);
989err_udc:
Alan Sternd9b76252005-05-03 16:15:43 -0400990 return rc;
991}
992
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100993static int dummy_udc_remove(struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -0400994{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100995 struct dummy *dum = platform_get_drvdata(pdev);
Alan Sternd9b76252005-05-03 16:15:43 -0400996
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100997 device_remove_file(&dum->gadget.dev, &dev_attr_function);
Alan Stern5f5610f2013-07-30 15:18:15 -0400998 usb_del_gadget_udc(&dum->gadget);
Alan Sternd9b76252005-05-03 16:15:43 -0400999 return 0;
1000}
1001
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001002static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1003 int suspend)
Alan Stern391eca92005-05-10 15:34:16 -04001004{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001005 spin_lock_irq(&dum->lock);
1006 dum->udc_suspended = suspend;
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001007 set_link_state(dum_hcd);
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001008 spin_unlock_irq(&dum->lock);
1009}
Alan Stern391eca92005-05-10 15:34:16 -04001010
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001011static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1012{
1013 struct dummy *dum = platform_get_drvdata(pdev);
1014 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1015
1016 dev_dbg(&pdev->dev, "%s\n", __func__);
1017 dummy_udc_pm(dum, dum_hcd, 1);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001018 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001019 return 0;
1020}
1021
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001022static int dummy_udc_resume(struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04001023{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001024 struct dummy *dum = platform_get_drvdata(pdev);
1025 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Alan Stern391eca92005-05-10 15:34:16 -04001026
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001027 dev_dbg(&pdev->dev, "%s\n", __func__);
1028 dummy_udc_pm(dum, dum_hcd, 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001029 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001030 return 0;
1031}
1032
Russell King3ae5eae2005-11-09 22:32:44 +00001033static struct platform_driver dummy_udc_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04001034 .probe = dummy_udc_probe,
1035 .remove = dummy_udc_remove,
Alan Stern391eca92005-05-10 15:34:16 -04001036 .suspend = dummy_udc_suspend,
1037 .resume = dummy_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001038 .driver = {
1039 .name = (char *) gadget_name,
1040 .owner = THIS_MODULE,
1041 },
Alan Sternd9b76252005-05-03 16:15:43 -04001042};
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044/*-------------------------------------------------------------------------*/
1045
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001046static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc)
1047{
1048 unsigned int index;
1049
1050 index = usb_endpoint_num(desc) << 1;
1051 if (usb_endpoint_dir_in(desc))
1052 index |= 1;
1053 return index;
1054}
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056/* MASTER/HOST SIDE DRIVER
1057 *
1058 * this uses the hcd framework to hook up to host side drivers.
1059 * its root hub will only have one device, otherwise it acts like
1060 * a normal host controller.
1061 *
1062 * when urbs are queued, they're just stuck on a list that we
1063 * scan in a timer callback. that callback connects writes from
1064 * the host with reads from the device, and so on, based on the
1065 * usb 2.0 rules.
1066 */
1067
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001068static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb)
1069{
1070 const struct usb_endpoint_descriptor *desc = &urb->ep->desc;
1071 u32 index;
1072
1073 if (!usb_endpoint_xfer_bulk(desc))
1074 return 0;
1075
1076 index = dummy_get_ep_idx(desc);
1077 return (1 << index) & dum_hcd->stream_en_ep;
1078}
1079
1080/*
1081 * The max stream number is saved as a nibble so for the 30 possible endpoints
1082 * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
1083 * means we use only 1 stream). The maximum according to the spec is 16bit so
1084 * if the 16 stream limit is about to go, the array size should be incremented
1085 * to 30 elements of type u16.
1086 */
1087static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1088 unsigned int pipe)
1089{
1090 int max_streams;
1091
1092 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1093 if (usb_pipeout(pipe))
1094 max_streams >>= 4;
1095 else
1096 max_streams &= 0xf;
1097 max_streams++;
1098 return max_streams;
1099}
1100
1101static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1102 unsigned int pipe, unsigned int streams)
1103{
1104 int max_streams;
1105
1106 streams--;
1107 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1108 if (usb_pipeout(pipe)) {
1109 streams <<= 4;
1110 max_streams &= 0xf;
1111 } else {
1112 max_streams &= 0xf0;
1113 }
1114 max_streams |= streams;
1115 dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams;
1116}
1117
1118static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb)
1119{
1120 unsigned int max_streams;
1121 int enabled;
1122
1123 enabled = dummy_ep_stream_en(dum_hcd, urb);
1124 if (!urb->stream_id) {
1125 if (enabled)
1126 return -EINVAL;
1127 return 0;
1128 }
1129 if (!enabled)
1130 return -EINVAL;
1131
1132 max_streams = get_max_streams_for_pipe(dum_hcd,
1133 usb_pipeendpoint(urb->pipe));
1134 if (urb->stream_id > max_streams) {
1135 dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n",
1136 urb->stream_id);
1137 BUG();
1138 return -EINVAL;
1139 }
1140 return 0;
1141}
1142
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001143static int dummy_urb_enqueue(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 struct usb_hcd *hcd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001146 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001148 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 struct urbp *urbp;
1150 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001151 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001153 urbp = kmalloc(sizeof *urbp, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (!urbp)
1155 return -ENOMEM;
1156 urbp->urb = urb;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001157 urbp->miter_started = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001159 dum_hcd = hcd_to_dummy_hcd(hcd);
1160 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001161
1162 rc = dummy_validate_stream(dum_hcd, urb);
1163 if (rc) {
1164 kfree(urbp);
1165 goto done;
1166 }
1167
Alan Sterne9df41c2007-08-08 11:48:02 -04001168 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1169 if (rc) {
1170 kfree(urbp);
1171 goto done;
1172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001174 if (!dum_hcd->udev) {
1175 dum_hcd->udev = urb->dev;
1176 usb_get_dev(dum_hcd->udev);
1177 } else if (unlikely(dum_hcd->udev != urb->dev))
1178 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001180 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 urb->hcpriv = urbp;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001182 if (usb_pipetype(urb->pipe) == PIPE_CONTROL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 urb->error_count = 1; /* mark as a new urb */
1184
1185 /* kick the scheduler, it'll do the rest */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001186 if (!timer_pending(&dum_hcd->timer))
1187 mod_timer(&dum_hcd->timer, jiffies + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Alan Sterne9df41c2007-08-08 11:48:02 -04001189 done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001190 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001191 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Alan Sterne9df41c2007-08-08 11:48:02 -04001194static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001196 struct dummy_hcd *dum_hcd;
Alan Stern391eca92005-05-10 15:34:16 -04001197 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001198 int rc;
Alan Stern391eca92005-05-10 15:34:16 -04001199
1200 /* giveback happens automatically in timer callback,
1201 * so make sure the callback happens */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001202 dum_hcd = hcd_to_dummy_hcd(hcd);
1203 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001204
1205 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001206 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1207 !list_empty(&dum_hcd->urbp_list))
1208 mod_timer(&dum_hcd->timer, jiffies);
Alan Sterne9df41c2007-08-08 11:48:02 -04001209
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001210 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001211 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001214static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
1215 u32 len)
1216{
1217 void *ubuf, *rbuf;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001218 struct urbp *urbp = urb->hcpriv;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001219 int to_host;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001220 struct sg_mapping_iter *miter = &urbp->miter;
1221 u32 trans = 0;
1222 u32 this_sg;
1223 bool next_sg;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001224
1225 to_host = usb_pipein(urb->pipe);
1226 rbuf = req->req.buf + req->req.actual;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001227
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001228 if (!urb->num_sgs) {
1229 ubuf = urb->transfer_buffer + urb->actual_length;
1230 if (to_host)
1231 memcpy(ubuf, rbuf, len);
1232 else
1233 memcpy(rbuf, ubuf, len);
1234 return len;
1235 }
1236
1237 if (!urbp->miter_started) {
1238 u32 flags = SG_MITER_ATOMIC;
1239
1240 if (to_host)
1241 flags |= SG_MITER_TO_SG;
1242 else
1243 flags |= SG_MITER_FROM_SG;
1244
1245 sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
1246 urbp->miter_started = 1;
1247 }
1248 next_sg = sg_miter_next(miter);
1249 if (next_sg == false) {
1250 WARN_ON_ONCE(1);
1251 return -EINVAL;
1252 }
1253 do {
1254 ubuf = miter->addr;
1255 this_sg = min_t(u32, len, miter->length);
1256 miter->consumed = this_sg;
1257 trans += this_sg;
1258
1259 if (to_host)
1260 memcpy(ubuf, rbuf, this_sg);
1261 else
1262 memcpy(rbuf, ubuf, this_sg);
1263 len -= this_sg;
1264
1265 if (!len)
1266 break;
1267 next_sg = sg_miter_next(miter);
1268 if (next_sg == false) {
1269 WARN_ON_ONCE(1);
1270 return -EINVAL;
1271 }
1272
1273 rbuf += this_sg;
1274 } while (1);
1275
1276 sg_miter_stop(miter);
1277 return trans;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280/* transfer up to a frame's worth; caller must own lock */
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001281static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb,
1282 struct dummy_ep *ep, int limit, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001284 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 struct dummy_request *req;
1286
1287top:
1288 /* if there's no request queued, the device is NAKing; return */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001289 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 unsigned host_len, dev_len, len;
1291 int is_short, to_host;
1292 int rescan = 0;
1293
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001294 if (dummy_ep_stream_en(dum_hcd, urb)) {
1295 if ((urb->stream_id != req->req.stream_id))
1296 continue;
1297 }
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 /* 1..N packets of ep->ep.maxpacket each ... the last one
1300 * may be short (including zero length).
1301 *
1302 * writer can send a zlp explicitly (length 0) or implicitly
1303 * (length mod maxpacket zero, and 'zero' flag); they always
1304 * terminate reads.
1305 */
1306 host_len = urb->transfer_buffer_length - urb->actual_length;
1307 dev_len = req->req.length - req->req.actual;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001308 len = min(host_len, dev_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 /* FIXME update emulated data toggle too */
1311
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001312 to_host = usb_pipein(urb->pipe);
1313 if (unlikely(len == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 is_short = 1;
1315 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 /* not enough bandwidth left? */
1317 if (limit < ep->ep.maxpacket && limit < len)
1318 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001319 len = min_t(unsigned, len, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (len == 0)
1321 break;
1322
1323 /* use an extra pass for the final short packet */
1324 if (len > ep->ep.maxpacket) {
1325 rescan = 1;
1326 len -= (len % ep->ep.maxpacket);
1327 }
1328 is_short = (len % ep->ep.maxpacket) != 0;
1329
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001330 len = dummy_perform_transfer(urb, req, len);
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 ep->last_io = jiffies;
Dan Carpenterb1443ac0e2012-03-02 21:51:00 +03001333 if ((int)len < 0) {
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001334 req->req.status = len;
1335 } else {
1336 limit -= len;
1337 urb->actual_length += len;
1338 req->req.actual += len;
1339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341
1342 /* short packets terminate, maybe with overflow/underflow.
1343 * it's only really an error to write too much.
1344 *
1345 * partially filling a buffer optionally blocks queue advances
1346 * (so completion handlers can clean up the queue) but we don't
Alan Sternb0d9efb2007-08-21 15:39:21 -04001347 * need to emulate such data-in-flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 */
1349 if (is_short) {
1350 if (host_len == dev_len) {
1351 req->req.status = 0;
Alan Stern4d2f1102007-08-24 15:40:10 -04001352 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 } else if (to_host) {
1354 req->req.status = 0;
1355 if (dev_len > host_len)
Alan Stern4d2f1102007-08-24 15:40:10 -04001356 *status = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 else
Alan Stern4d2f1102007-08-24 15:40:10 -04001358 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 } else if (!to_host) {
Alan Stern4d2f1102007-08-24 15:40:10 -04001360 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (host_len > dev_len)
1362 req->req.status = -EOVERFLOW;
1363 else
1364 req->req.status = 0;
1365 }
1366
1367 /* many requests terminate without a short packet */
1368 } else {
1369 if (req->req.length == req->req.actual
1370 && !req->req.zero)
1371 req->req.status = 0;
1372 if (urb->transfer_buffer_length == urb->actual_length
1373 && !(urb->transfer_flags
Alan Stern4d2f1102007-08-24 15:40:10 -04001374 & URB_ZERO_PACKET))
1375 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377
1378 /* device side completion --> continuable */
1379 if (req->req.status != -EINPROGRESS) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001380 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001382 spin_unlock(&dum->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02001383 usb_gadget_giveback_request(&ep->ep, &req->req);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001384 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 /* requests might have been unlinked... */
1387 rescan = 1;
1388 }
1389
1390 /* host side completion --> terminate */
Alan Stern4d2f1102007-08-24 15:40:10 -04001391 if (*status != -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 break;
1393
1394 /* rescan to continue with any other queued i/o */
1395 if (rescan)
1396 goto top;
1397 }
1398 return limit;
1399}
1400
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001401static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
1403 int limit = ep->ep.maxpacket;
1404
1405 if (dum->gadget.speed == USB_SPEED_HIGH) {
1406 int tmp;
1407
1408 /* high bandwidth mode */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001409 tmp = usb_endpoint_maxp(ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 tmp = (tmp >> 11) & 0x03;
1411 tmp *= 8 /* applies to entire frame */;
1412 limit += limit * tmp;
1413 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001414 if (dum->gadget.speed == USB_SPEED_SUPER) {
Sebastian Andrzej Siewior59f08e62012-01-12 12:53:14 +01001415 switch (usb_endpoint_type(ep->desc)) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001416 case USB_ENDPOINT_XFER_ISOC:
1417 /* Sec. 4.4.8.2 USB3.0 Spec */
1418 limit = 3 * 16 * 1024 * 8;
1419 break;
1420 case USB_ENDPOINT_XFER_INT:
1421 /* Sec. 4.4.7.2 USB3.0 Spec */
1422 limit = 3 * 1024 * 8;
1423 break;
1424 case USB_ENDPOINT_XFER_BULK:
1425 default:
1426 break;
1427 }
1428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return limit;
1430}
1431
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001432#define is_active(dum_hcd) ((dum_hcd->port_status & \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1434 USB_PORT_STAT_SUSPEND)) \
1435 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1436
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001437static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 int i;
1440
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001441 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1442 dum->ss_hcd : dum->hs_hcd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return NULL;
1444 if ((address & ~USB_DIR_IN) == 0)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001445 return &dum->ep[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001447 struct dummy_ep *ep = &dum->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 if (!ep->desc)
1450 continue;
1451 if (ep->desc->bEndpointAddress == address)
1452 return ep;
1453 }
1454 return NULL;
1455}
1456
1457#undef is_active
1458
1459#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1460#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1461#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1462#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1463#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1464#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1465
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001466
1467/**
1468 * handle_control_request() - handles all control transfers
1469 * @dum: pointer to dummy (the_controller)
1470 * @urb: the urb request to handle
1471 * @setup: pointer to the setup data for a USB device control
1472 * request
1473 * @status: pointer to request handling status
1474 *
1475 * Return 0 - if the request was handled
1476 * 1 - if the request wasn't handles
1477 * error code on error
1478 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001479static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001480 struct usb_ctrlrequest *setup,
1481 int *status)
1482{
1483 struct dummy_ep *ep2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001484 struct dummy *dum = dum_hcd->dum;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001485 int ret_val = 1;
1486 unsigned w_index;
1487 unsigned w_value;
1488
1489 w_index = le16_to_cpu(setup->wIndex);
1490 w_value = le16_to_cpu(setup->wValue);
1491 switch (setup->bRequest) {
1492 case USB_REQ_SET_ADDRESS:
1493 if (setup->bRequestType != Dev_Request)
1494 break;
1495 dum->address = w_value;
1496 *status = 0;
1497 dev_dbg(udc_dev(dum), "set_address = %d\n",
1498 w_value);
1499 ret_val = 0;
1500 break;
1501 case USB_REQ_SET_FEATURE:
1502 if (setup->bRequestType == Dev_Request) {
1503 ret_val = 0;
1504 switch (w_value) {
1505 case USB_DEVICE_REMOTE_WAKEUP:
1506 break;
1507 case USB_DEVICE_B_HNP_ENABLE:
1508 dum->gadget.b_hnp_enable = 1;
1509 break;
1510 case USB_DEVICE_A_HNP_SUPPORT:
1511 dum->gadget.a_hnp_support = 1;
1512 break;
1513 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1514 dum->gadget.a_alt_hnp_support = 1;
1515 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001516 case USB_DEVICE_U1_ENABLE:
1517 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1518 HCD_USB3)
1519 w_value = USB_DEV_STAT_U1_ENABLED;
1520 else
1521 ret_val = -EOPNOTSUPP;
1522 break;
1523 case USB_DEVICE_U2_ENABLE:
1524 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1525 HCD_USB3)
1526 w_value = USB_DEV_STAT_U2_ENABLED;
1527 else
1528 ret_val = -EOPNOTSUPP;
1529 break;
1530 case USB_DEVICE_LTM_ENABLE:
1531 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1532 HCD_USB3)
1533 w_value = USB_DEV_STAT_LTM_ENABLED;
1534 else
1535 ret_val = -EOPNOTSUPP;
1536 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001537 default:
1538 ret_val = -EOPNOTSUPP;
1539 }
1540 if (ret_val == 0) {
1541 dum->devstatus |= (1 << w_value);
1542 *status = 0;
1543 }
1544 } else if (setup->bRequestType == Ep_Request) {
1545 /* endpoint halt */
1546 ep2 = find_endpoint(dum, w_index);
1547 if (!ep2 || ep2->ep.name == ep0name) {
1548 ret_val = -EOPNOTSUPP;
1549 break;
1550 }
1551 ep2->halted = 1;
1552 ret_val = 0;
1553 *status = 0;
1554 }
1555 break;
1556 case USB_REQ_CLEAR_FEATURE:
1557 if (setup->bRequestType == Dev_Request) {
1558 ret_val = 0;
1559 switch (w_value) {
1560 case USB_DEVICE_REMOTE_WAKEUP:
1561 w_value = USB_DEVICE_REMOTE_WAKEUP;
1562 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001563 case USB_DEVICE_U1_ENABLE:
1564 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1565 HCD_USB3)
1566 w_value = USB_DEV_STAT_U1_ENABLED;
1567 else
1568 ret_val = -EOPNOTSUPP;
1569 break;
1570 case USB_DEVICE_U2_ENABLE:
1571 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1572 HCD_USB3)
1573 w_value = USB_DEV_STAT_U2_ENABLED;
1574 else
1575 ret_val = -EOPNOTSUPP;
1576 break;
1577 case USB_DEVICE_LTM_ENABLE:
1578 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1579 HCD_USB3)
1580 w_value = USB_DEV_STAT_LTM_ENABLED;
1581 else
1582 ret_val = -EOPNOTSUPP;
1583 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001584 default:
1585 ret_val = -EOPNOTSUPP;
1586 break;
1587 }
1588 if (ret_val == 0) {
1589 dum->devstatus &= ~(1 << w_value);
1590 *status = 0;
1591 }
1592 } else if (setup->bRequestType == Ep_Request) {
1593 /* endpoint halt */
1594 ep2 = find_endpoint(dum, w_index);
1595 if (!ep2) {
1596 ret_val = -EOPNOTSUPP;
1597 break;
1598 }
1599 if (!ep2->wedged)
1600 ep2->halted = 0;
1601 ret_val = 0;
1602 *status = 0;
1603 }
1604 break;
1605 case USB_REQ_GET_STATUS:
1606 if (setup->bRequestType == Dev_InRequest
1607 || setup->bRequestType == Intf_InRequest
1608 || setup->bRequestType == Ep_InRequest) {
1609 char *buf;
1610 /*
1611 * device: remote wakeup, selfpowered
1612 * interface: nothing
1613 * endpoint: halt
1614 */
1615 buf = (char *)urb->transfer_buffer;
1616 if (urb->transfer_buffer_length > 0) {
1617 if (setup->bRequestType == Ep_InRequest) {
1618 ep2 = find_endpoint(dum, w_index);
1619 if (!ep2) {
1620 ret_val = -EOPNOTSUPP;
1621 break;
1622 }
1623 buf[0] = ep2->halted;
1624 } else if (setup->bRequestType ==
1625 Dev_InRequest) {
1626 buf[0] = (u8)dum->devstatus;
1627 } else
1628 buf[0] = 0;
1629 }
1630 if (urb->transfer_buffer_length > 1)
1631 buf[1] = 0;
1632 urb->actual_length = min_t(u32, 2,
1633 urb->transfer_buffer_length);
1634 ret_val = 0;
1635 *status = 0;
1636 }
1637 break;
1638 }
1639 return ret_val;
1640}
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642/* drive both sides of the transfers; looks like irq handlers to
1643 * both drivers except the callbacks aren't in_irq().
1644 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001645static void dummy_timer(unsigned long _dum_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001647 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1648 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 struct urbp *urbp, *tmp;
1650 unsigned long flags;
1651 int limit, total;
1652 int i;
1653
1654 /* simplistic model for one frame's bandwidth */
1655 switch (dum->gadget.speed) {
1656 case USB_SPEED_LOW:
1657 total = 8/*bytes*/ * 12/*packets*/;
1658 break;
1659 case USB_SPEED_FULL:
1660 total = 64/*bytes*/ * 19/*packets*/;
1661 break;
1662 case USB_SPEED_HIGH:
1663 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1664 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001665 case USB_SPEED_SUPER:
1666 /* Bus speed is 500000 bytes/ms, so use a little less */
1667 total = 490000;
1668 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001670 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 return;
1672 }
1673
1674 /* FIXME if HZ != 1000 this will probably misbehave ... */
1675
1676 /* look at each urb queued by the host side driver */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001677 spin_lock_irqsave(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001679 if (!dum_hcd->udev) {
1680 dev_err(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 "timer fired with no URBs pending?\n");
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001682 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return;
1684 }
1685
1686 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001687 if (!ep_name[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001689 dum->ep[i].already_seen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
1691
1692restart:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001693 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 struct urb *urb;
1695 struct dummy_request *req;
1696 u8 address;
1697 struct dummy_ep *ep = NULL;
1698 int type;
Alan Stern4d2f1102007-08-24 15:40:10 -04001699 int status = -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 urb = urbp->urb;
Alan Sterneb231052007-08-21 15:40:36 -04001702 if (urb->unlinked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 goto return_urb;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001704 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
Alan Stern391eca92005-05-10 15:34:16 -04001705 continue;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001706 type = usb_pipetype(urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 /* used up this frame's non-periodic bandwidth?
1709 * FIXME there's infinite bandwidth for control and
1710 * periodic transfers ... unrealistic.
1711 */
1712 if (total <= 0 && type == PIPE_BULK)
1713 continue;
1714
1715 /* find the gadget's ep for this request (if configured) */
1716 address = usb_pipeendpoint (urb->pipe);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001717 if (usb_pipein(urb->pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 address |= USB_DIR_IN;
1719 ep = find_endpoint(dum, address);
1720 if (!ep) {
1721 /* set_configuration() disagreement */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001722 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 "no ep configured for urb %p\n",
1724 urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001725 status = -EPROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 goto return_urb;
1727 }
1728
1729 if (ep->already_seen)
1730 continue;
1731 ep->already_seen = 1;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001732 if (ep == &dum->ep[0] && urb->error_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 ep->setup_stage = 1; /* a new urb */
1734 urb->error_count = 0;
1735 }
1736 if (ep->halted && !ep->setup_stage) {
1737 /* NOTE: must not be iso! */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001738 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 ep->ep.name, urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001740 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 goto return_urb;
1742 }
1743 /* FIXME make sure both ends agree on maxpacket */
1744
1745 /* handle control requests */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001746 if (ep == &dum->ep[0] && ep->setup_stage) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 struct usb_ctrlrequest setup;
1748 int value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001750 setup = *(struct usb_ctrlrequest *) urb->setup_packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /* paranoia, in case of stale queued data */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001752 list_for_each_entry(req, &ep->queue, queue) {
1753 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 req->req.status = -EOVERFLOW;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001755 dev_dbg(udc_dev(dum), "stale req = %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 req);
1757
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001758 spin_unlock(&dum->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02001759 usb_gadget_giveback_request(&ep->ep, &req->req);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001760 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 ep->already_seen = 0;
1762 goto restart;
1763 }
1764
1765 /* gadget driver never sees set_address or operations
1766 * on standard feature flags. some hardware doesn't
1767 * even expose them.
1768 */
1769 ep->last_io = jiffies;
1770 ep->setup_stage = 0;
1771 ep->halted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001773 value = handle_control_request(dum_hcd, urb, &setup,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001774 &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
1776 /* gadget driver handles all other requests. block
1777 * until setup() returns; no reentrancy issues etc.
1778 */
1779 if (value > 0) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001780 spin_unlock(&dum->lock);
1781 value = dum->driver->setup(&dum->gadget,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 &setup);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001783 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785 if (value >= 0) {
1786 /* no delays (max 64KB data stage) */
1787 limit = 64*1024;
1788 goto treat_control_like_bulk;
1789 }
1790 /* error, see below */
1791 }
1792
1793 if (value < 0) {
1794 if (value != -EOPNOTSUPP)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001795 dev_dbg(udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 "setup --> %d\n",
1797 value);
Alan Stern4d2f1102007-08-24 15:40:10 -04001798 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 urb->actual_length = 0;
1800 }
1801
1802 goto return_urb;
1803 }
1804
1805 /* non-control requests */
1806 limit = total;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001807 switch (usb_pipetype(urb->pipe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 case PIPE_ISOCHRONOUS:
1809 /* FIXME is it urb->interval since the last xfer?
1810 * use urb->iso_frame_desc[i].
1811 * complete whether or not ep has requests queued.
1812 * report random errors, to debug drivers.
1813 */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001814 limit = max(limit, periodic_bytes(dum, ep));
Alan Stern4d2f1102007-08-24 15:40:10 -04001815 status = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 break;
1817
1818 case PIPE_INTERRUPT:
1819 /* FIXME is it urb->interval since the last xfer?
1820 * this almost certainly polls too fast.
1821 */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001822 limit = max(limit, periodic_bytes(dum, ep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 /* FALLTHROUGH */
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 default:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001826treat_control_like_bulk:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 ep->last_io = jiffies;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001828 total = transfer(dum_hcd, urb, ep, limit, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 break;
1830 }
1831
1832 /* incomplete transfer? */
Alan Stern4d2f1102007-08-24 15:40:10 -04001833 if (status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 continue;
1835
1836return_urb:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001837 list_del(&urbp->urbp_list);
1838 kfree(urbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 if (ep)
1840 ep->already_seen = ep->setup_stage = 0;
1841
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001842 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001843 spin_unlock(&dum->lock);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001844 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001845 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 goto restart;
1848 }
1849
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001850 if (list_empty(&dum_hcd->urbp_list)) {
1851 usb_put_dev(dum_hcd->udev);
1852 dum_hcd->udev = NULL;
1853 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern391eca92005-05-10 15:34:16 -04001854 /* want a 1 msec delay here */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001855 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
1857
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001858 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
1861/*-------------------------------------------------------------------------*/
1862
1863#define PORT_C_MASK \
Alan Sternc2db8b52005-04-29 16:30:48 -04001864 ((USB_PORT_STAT_C_CONNECTION \
1865 | USB_PORT_STAT_C_ENABLE \
1866 | USB_PORT_STAT_C_SUSPEND \
1867 | USB_PORT_STAT_C_OVERCURRENT \
1868 | USB_PORT_STAT_C_RESET) << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001870static int dummy_hub_status(struct usb_hcd *hcd, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001872 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 unsigned long flags;
Alan Stern391eca92005-05-10 15:34:16 -04001874 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001876 dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001878 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001879 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001880 goto done;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001881
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001882 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1883 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1884 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1885 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001886 }
1887
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001888 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 *buf = (1 << 1);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001890 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1891 dum_hcd->port_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 retval = 1;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001893 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001894 usb_hcd_resume_root_hub(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
Alan Stern391eca92005-05-10 15:34:16 -04001896done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001897 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 return retval;
1899}
1900
Sebastian Andrzej Siewior3b9c1c52012-08-19 21:54:59 +02001901/* usb 3.0 root hub device descriptor */
Felipe Balbi814cf212013-03-22 16:50:47 +02001902static struct {
Sebastian Andrzej Siewior3b9c1c52012-08-19 21:54:59 +02001903 struct usb_bos_descriptor bos;
1904 struct usb_ss_cap_descriptor ss_cap;
1905} __packed usb3_bos_desc = {
1906
1907 .bos = {
1908 .bLength = USB_DT_BOS_SIZE,
1909 .bDescriptorType = USB_DT_BOS,
1910 .wTotalLength = cpu_to_le16(sizeof(usb3_bos_desc)),
1911 .bNumDeviceCaps = 1,
1912 },
1913 .ss_cap = {
1914 .bLength = USB_DT_USB_SS_CAP_SIZE,
1915 .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
1916 .bDevCapabilityType = USB_SS_CAP_TYPE,
1917 .wSpeedSupported = cpu_to_le16(USB_5GBPS_OPERATION),
1918 .bFunctionalitySupport = ilog2(USB_5GBPS_OPERATION),
1919 },
1920};
1921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922static inline void
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001923ss_hub_descriptor(struct usb_hub_descriptor *desc)
1924{
1925 memset(desc, 0, sizeof *desc);
1926 desc->bDescriptorType = 0x2a;
1927 desc->bDescLength = 12;
1928 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1929 desc->bNbrPorts = 1;
1930 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1931 desc->u.ss.DeviceRemovable = 0xffff;
1932}
1933
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001934static inline void hub_descriptor(struct usb_hub_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001936 memset(desc, 0, sizeof *desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 desc->bDescriptorType = 0x29;
1938 desc->bDescLength = 9;
Al Virofd05e722008-04-28 07:00:16 +01001939 desc->wHubCharacteristics = cpu_to_le16(0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 desc->bNbrPorts = 1;
John Youndbe79bb2001-09-17 00:00:00 -07001941 desc->u.hs.DeviceRemovable[0] = 0xff;
1942 desc->u.hs.DeviceRemovable[1] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001945static int dummy_hub_control(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 struct usb_hcd *hcd,
1947 u16 typeReq,
1948 u16 wValue,
1949 u16 wIndex,
1950 char *buf,
1951 u16 wLength
1952) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001953 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 int retval = 0;
1955 unsigned long flags;
1956
Alan Stern541c7d42010-06-22 16:39:10 -04001957 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001958 return -ETIMEDOUT;
1959
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001960 dum_hcd = hcd_to_dummy_hcd(hcd);
1961
1962 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 switch (typeReq) {
1964 case ClearHubFeature:
1965 break;
1966 case ClearPortFeature:
1967 switch (wValue) {
1968 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001969 if (hcd->speed == HCD_USB3) {
1970 dev_dbg(dummy_dev(dum_hcd),
1971 "USB_PORT_FEAT_SUSPEND req not "
1972 "supported for USB 3.0 roothub\n");
1973 goto error;
1974 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001975 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 /* 20msec resume signaling */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001977 dum_hcd->resuming = 1;
1978 dum_hcd->re_timeout = jiffies +
Alan Sternf1c39fa2005-05-03 16:24:04 -04001979 msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
1981 break;
1982 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001983 if (hcd->speed == HCD_USB3) {
1984 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1985 dev_dbg(dummy_dev(dum_hcd),
1986 "power-off\n");
1987 } else
1988 if (dum_hcd->port_status &
1989 USB_SS_PORT_STAT_POWER)
1990 dev_dbg(dummy_dev(dum_hcd),
1991 "power-off\n");
Alan Sternf1c39fa2005-05-03 16:24:04 -04001992 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001994 dum_hcd->port_status &= ~(1 << wValue);
1995 set_link_state(dum_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997 break;
1998 case GetHubDescriptor:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001999 if (hcd->speed == HCD_USB3 &&
2000 (wLength < USB_DT_SS_HUB_SIZE ||
2001 wValue != (USB_DT_SS_HUB << 8))) {
2002 dev_dbg(dummy_dev(dum_hcd),
2003 "Wrong hub descriptor type for "
2004 "USB 3.0 roothub.\n");
2005 goto error;
2006 }
2007 if (hcd->speed == HCD_USB3)
2008 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
2009 else
2010 hub_descriptor((struct usb_hub_descriptor *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 break;
Sebastian Andrzej Siewior3b9c1c52012-08-19 21:54:59 +02002012
2013 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
2014 if (hcd->speed != HCD_USB3)
2015 goto error;
2016
2017 if ((wValue >> 8) != USB_DT_BOS)
2018 goto error;
2019
2020 memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc));
2021 retval = sizeof(usb3_bos_desc);
2022 break;
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 case GetHubStatus:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002025 *(__le32 *) buf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 break;
2027 case GetPortStatus:
2028 if (wIndex != 1)
2029 retval = -EPIPE;
2030
2031 /* whoever resets or resumes must GetPortStatus to
2032 * complete it!!
2033 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002034 if (dum_hcd->resuming &&
2035 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2036 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
2037 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002039 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
2040 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2041 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
2042 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
2043 if (dum_hcd->dum->pullup) {
2044 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002045
2046 if (hcd->speed < HCD_USB3) {
2047 switch (dum_hcd->dum->gadget.speed) {
2048 case USB_SPEED_HIGH:
2049 dum_hcd->port_status |=
2050 USB_PORT_STAT_HIGH_SPEED;
2051 break;
2052 case USB_SPEED_LOW:
2053 dum_hcd->dum->gadget.ep0->
2054 maxpacket = 8;
2055 dum_hcd->port_status |=
2056 USB_PORT_STAT_LOW_SPEED;
2057 break;
2058 default:
2059 dum_hcd->dum->gadget.speed =
2060 USB_SPEED_FULL;
2061 break;
2062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
2064 }
2065 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002066 set_link_state(dum_hcd);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002067 ((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status);
2068 ((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 break;
2070 case SetHubFeature:
2071 retval = -EPIPE;
2072 break;
2073 case SetPortFeature:
2074 switch (wValue) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002075 case USB_PORT_FEAT_LINK_STATE:
2076 if (hcd->speed != HCD_USB3) {
2077 dev_dbg(dummy_dev(dum_hcd),
2078 "USB_PORT_FEAT_LINK_STATE req not "
2079 "supported for USB 2.0 roothub\n");
2080 goto error;
2081 }
2082 /*
2083 * Since this is dummy we don't have an actual link so
2084 * there is nothing to do for the SET_LINK_STATE cmd
2085 */
2086 break;
2087 case USB_PORT_FEAT_U1_TIMEOUT:
2088 case USB_PORT_FEAT_U2_TIMEOUT:
2089 /* TODO: add suspend/resume support! */
2090 if (hcd->speed != HCD_USB3) {
2091 dev_dbg(dummy_dev(dum_hcd),
2092 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
2093 "supported for USB 2.0 roothub\n");
2094 goto error;
2095 }
2096 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002098 /* Applicable only for USB2.0 hub */
2099 if (hcd->speed == HCD_USB3) {
2100 dev_dbg(dummy_dev(dum_hcd),
2101 "USB_PORT_FEAT_SUSPEND req not "
2102 "supported for USB 3.0 roothub\n");
2103 goto error;
2104 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002105 if (dum_hcd->active) {
2106 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
Alan Sternf1c39fa2005-05-03 16:24:04 -04002107
2108 /* HNP would happen here; for now we
2109 * assume b_bus_req is always true.
2110 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002111 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002112 if (((1 << USB_DEVICE_B_HNP_ENABLE)
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002113 & dum_hcd->dum->devstatus) != 0)
2114 dev_dbg(dummy_dev(dum_hcd),
Alan Stern5742b0c2005-05-02 11:25:17 -04002115 "no HNP yet!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
2117 break;
Alan Sternf1c39fa2005-05-03 16:24:04 -04002118 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002119 if (hcd->speed == HCD_USB3)
2120 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
2121 else
2122 dum_hcd->port_status |= USB_PORT_STAT_POWER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002123 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002124 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002125 case USB_PORT_FEAT_BH_PORT_RESET:
2126 /* Applicable only for USB3.0 hub */
2127 if (hcd->speed != HCD_USB3) {
2128 dev_dbg(dummy_dev(dum_hcd),
2129 "USB_PORT_FEAT_BH_PORT_RESET req not "
2130 "supported for USB 2.0 roothub\n");
2131 goto error;
2132 }
2133 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 case USB_PORT_FEAT_RESET:
Alan Sternf1c39fa2005-05-03 16:24:04 -04002135 /* if it's already enabled, disable */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002136 if (hcd->speed == HCD_USB3) {
2137 dum_hcd->port_status = 0;
2138 dum_hcd->port_status =
2139 (USB_SS_PORT_STAT_POWER |
2140 USB_PORT_STAT_CONNECTION |
2141 USB_PORT_STAT_RESET);
2142 } else
2143 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
Alan Sternf1c39fa2005-05-03 16:24:04 -04002144 | USB_PORT_STAT_LOW_SPEED
2145 | USB_PORT_STAT_HIGH_SPEED);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002146 /*
2147 * We want to reset device status. All but the
2148 * Self powered feature
2149 */
2150 dum_hcd->dum->devstatus &=
2151 (1 << USB_DEVICE_SELF_POWERED);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002152 /*
2153 * FIXME USB3.0: what is the correct reset signaling
2154 * interval? Is it still 50msec as for HS?
2155 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002156 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002157 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 default:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002159 if (hcd->speed == HCD_USB3) {
2160 if ((dum_hcd->port_status &
2161 USB_SS_PORT_STAT_POWER) != 0) {
2162 dum_hcd->port_status |= (1 << wValue);
2163 set_link_state(dum_hcd);
2164 }
2165 } else
2166 if ((dum_hcd->port_status &
2167 USB_PORT_STAT_POWER) != 0) {
2168 dum_hcd->port_status |= (1 << wValue);
2169 set_link_state(dum_hcd);
2170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }
2172 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002173 case GetPortErrorCount:
2174 if (hcd->speed != HCD_USB3) {
2175 dev_dbg(dummy_dev(dum_hcd),
2176 "GetPortErrorCount req not "
2177 "supported for USB 2.0 roothub\n");
2178 goto error;
2179 }
2180 /* We'll always return 0 since this is a dummy hub */
2181 *(__le32 *) buf = cpu_to_le32(0);
2182 break;
2183 case SetHubDepth:
2184 if (hcd->speed != HCD_USB3) {
2185 dev_dbg(dummy_dev(dum_hcd),
2186 "SetHubDepth req not supported for "
2187 "USB 2.0 roothub\n");
2188 goto error;
2189 }
2190 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002192 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 "hub control req%04x v%04x i%04x l%d\n",
2194 typeReq, wValue, wIndex, wLength);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002195error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 /* "protocol stall" on error */
2197 retval = -EPIPE;
2198 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002199 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Stern685eb932005-05-03 16:27:26 -04002200
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002201 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002202 usb_hcd_poll_rh_status(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 return retval;
2204}
2205
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002206static int dummy_bus_suspend(struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002207{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002208 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002209
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002210 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern3cf0a222005-11-29 12:08:15 -05002211
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002212 spin_lock_irq(&dum_hcd->dum->lock);
2213 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2214 set_link_state(dum_hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002215 hcd->state = HC_STATE_SUSPENDED;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002216 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern391eca92005-05-10 15:34:16 -04002217 return 0;
2218}
2219
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002220static int dummy_bus_resume(struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002221{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002222 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002223 int rc = 0;
2224
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002225 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002226
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002227 spin_lock_irq(&dum_hcd->dum->lock);
Alan Stern541c7d42010-06-22 16:39:10 -04002228 if (!HCD_HW_ACCESSIBLE(hcd)) {
Alan Sterncfa59da2007-06-21 16:25:35 -04002229 rc = -ESHUTDOWN;
Alan Stern3cf0a222005-11-29 12:08:15 -05002230 } else {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002231 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2232 set_link_state(dum_hcd);
2233 if (!list_empty(&dum_hcd->urbp_list))
2234 mod_timer(&dum_hcd->timer, jiffies);
Alan Stern3cf0a222005-11-29 12:08:15 -05002235 hcd->state = HC_STATE_RUNNING;
2236 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002237 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern3cf0a222005-11-29 12:08:15 -05002238 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002239}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
2241/*-------------------------------------------------------------------------*/
2242
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002243static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002245 int ep = usb_pipeendpoint(urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002247 return snprintf(buf, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 "urb/%p %s ep%d%s%s len %d/%d\n",
2249 urb,
2250 ({ char *s;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002251 switch (urb->dev->speed) {
2252 case USB_SPEED_LOW:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002253 s = "ls";
2254 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002255 case USB_SPEED_FULL:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002256 s = "fs";
2257 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002258 case USB_SPEED_HIGH:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002259 s = "hs";
2260 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002261 case USB_SPEED_SUPER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002262 s = "ss";
2263 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002264 default:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002265 s = "?";
2266 break;
Joe Perches2b84f922013-10-08 16:01:37 -07002267 } s; }),
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002268 ep, ep ? (usb_pipein(urb->pipe) ? "in" : "out") : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 ({ char *s; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002270 switch (usb_pipetype(urb->pipe)) { \
2271 case PIPE_CONTROL: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002272 s = ""; \
2273 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002274 case PIPE_BULK: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002275 s = "-bulk"; \
2276 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002277 case PIPE_INTERRUPT: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002278 s = "-int"; \
2279 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002280 default: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002281 s = "-iso"; \
2282 break; \
Joe Perches2b84f922013-10-08 16:01:37 -07002283 } s; }),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 urb->actual_length, urb->transfer_buffer_length);
2285}
2286
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07002287static ssize_t urbs_show(struct device *dev, struct device_attribute *attr,
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002288 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002290 struct usb_hcd *hcd = dev_get_drvdata(dev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002291 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 struct urbp *urbp;
2293 size_t size = 0;
2294 unsigned long flags;
2295
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002296 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2297 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 size_t temp;
2299
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002300 temp = show_urb(buf, PAGE_SIZE - size, urbp->urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 buf += temp;
2302 size += temp;
2303 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002304 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
2306 return size;
2307}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07002308static DEVICE_ATTR_RO(urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002310static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2311{
2312 init_timer(&dum_hcd->timer);
2313 dum_hcd->timer.function = dummy_timer;
2314 dum_hcd->timer.data = (unsigned long)dum_hcd;
2315 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002316 dum_hcd->stream_en_ep = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002317 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2318 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2319 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2320 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2321#ifdef CONFIG_USB_OTG
2322 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2323#endif
2324 return 0;
2325
2326 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2327 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2328}
2329
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002330static int dummy_start(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002332 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334 /*
2335 * MASTER side init ... we emulate a root hub that'll only ever
2336 * talk to one device (the slave side). Also appears in sysfs,
2337 * just like more familiar pci-based HCDs.
2338 */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002339 if (!usb_hcd_is_primary_hcd(hcd))
2340 return dummy_start_ss(dum_hcd);
2341
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002342 spin_lock_init(&dum_hcd->dum->lock);
2343 init_timer(&dum_hcd->timer);
2344 dum_hcd->timer.function = dummy_timer;
2345 dum_hcd->timer.data = (unsigned long)dum_hcd;
2346 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002348 INIT_LIST_HEAD(&dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Alan Sterncaf29f62007-12-06 11:10:39 -05002350 hcd->power_budget = POWER_BUDGET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 hcd->state = HC_STATE_RUNNING;
Alan Stern685eb932005-05-03 16:27:26 -04002352 hcd->uses_new_polling = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Alan Stern5742b0c2005-05-02 11:25:17 -04002354#ifdef CONFIG_USB_OTG
2355 hcd->self.otg_port = 1;
2356#endif
2357
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002359 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360}
2361
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002362static void dummy_stop(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
2364 struct dummy *dum;
2365
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002366 dum = hcd_to_dummy_hcd(hcd)->dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002367 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002368 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
2370
2371/*-------------------------------------------------------------------------*/
2372
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002373static int dummy_h_get_frame(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002375 return dummy_g_get_frame(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376}
2377
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002378static int dummy_setup(struct usb_hcd *hcd)
2379{
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002380 struct dummy *dum;
2381
2382 dum = *((void **)dev_get_platdata(hcd->self.controller));
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01002383 hcd->self.sg_tablesize = ~0;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002384 if (usb_hcd_is_primary_hcd(hcd)) {
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002385 dum->hs_hcd = hcd_to_dummy_hcd(hcd);
2386 dum->hs_hcd->dum = dum;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002387 /*
2388 * Mark the first roothub as being USB 2.0.
2389 * The USB 3.0 roothub will be registered later by
2390 * dummy_hcd_probe()
2391 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002392 hcd->speed = HCD_USB2;
2393 hcd->self.root_hub->speed = USB_SPEED_HIGH;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002394 } else {
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002395 dum->ss_hcd = hcd_to_dummy_hcd(hcd);
2396 dum->ss_hcd->dum = dum;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002397 hcd->speed = HCD_USB3;
2398 hcd->self.root_hub->speed = USB_SPEED_SUPER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002399 }
2400 return 0;
2401}
2402
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002403/* Change a group of bulk endpoints to support multiple stream IDs */
Sebastian Andrzej Siewiord81f3e42012-01-09 13:15:00 +01002404static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002405 struct usb_host_endpoint **eps, unsigned int num_eps,
2406 unsigned int num_streams, gfp_t mem_flags)
2407{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002408 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2409 unsigned long flags;
2410 int max_stream;
2411 int ret_streams = num_streams;
2412 unsigned int index;
2413 unsigned int i;
2414
2415 if (!num_eps)
2416 return -EINVAL;
2417
2418 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2419 for (i = 0; i < num_eps; i++) {
2420 index = dummy_get_ep_idx(&eps[i]->desc);
2421 if ((1 << index) & dum_hcd->stream_en_ep) {
2422 ret_streams = -EINVAL;
2423 goto out;
2424 }
2425 max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp);
2426 if (!max_stream) {
2427 ret_streams = -EINVAL;
2428 goto out;
2429 }
2430 if (max_stream < ret_streams) {
2431 dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u "
2432 "stream IDs.\n",
2433 eps[i]->desc.bEndpointAddress,
2434 max_stream);
2435 ret_streams = max_stream;
2436 }
2437 }
2438
2439 for (i = 0; i < num_eps; i++) {
2440 index = dummy_get_ep_idx(&eps[i]->desc);
2441 dum_hcd->stream_en_ep |= 1 << index;
2442 set_max_streams_for_pipe(dum_hcd,
2443 usb_endpoint_num(&eps[i]->desc), ret_streams);
2444 }
2445out:
2446 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2447 return ret_streams;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002448}
2449
2450/* Reverts a group of bulk endpoints back to not using stream IDs. */
Sebastian Andrzej Siewiord81f3e42012-01-09 13:15:00 +01002451static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002452 struct usb_host_endpoint **eps, unsigned int num_eps,
2453 gfp_t mem_flags)
2454{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002455 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2456 unsigned long flags;
2457 int ret;
2458 unsigned int index;
2459 unsigned int i;
2460
2461 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2462 for (i = 0; i < num_eps; i++) {
2463 index = dummy_get_ep_idx(&eps[i]->desc);
2464 if (!((1 << index) & dum_hcd->stream_en_ep)) {
2465 ret = -EINVAL;
2466 goto out;
2467 }
2468 }
2469
2470 for (i = 0; i < num_eps; i++) {
2471 index = dummy_get_ep_idx(&eps[i]->desc);
2472 dum_hcd->stream_en_ep &= ~(1 << index);
2473 set_max_streams_for_pipe(dum_hcd,
2474 usb_endpoint_num(&eps[i]->desc), 0);
2475 }
2476 ret = 0;
2477out:
2478 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2479 return ret;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002480}
2481
2482static struct hc_driver dummy_hcd = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 .description = (char *) driver_name,
2484 .product_desc = "Dummy host controller",
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002485 .hcd_priv_size = sizeof(struct dummy_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002487 .flags = HCD_USB3 | HCD_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002489 .reset = dummy_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 .start = dummy_start,
2491 .stop = dummy_stop,
2492
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002493 .urb_enqueue = dummy_urb_enqueue,
2494 .urb_dequeue = dummy_urb_dequeue,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002496 .get_frame_number = dummy_h_get_frame,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002498 .hub_status_data = dummy_hub_status,
2499 .hub_control = dummy_hub_control,
Alan Stern0c0382e2005-10-13 17:08:02 -04002500 .bus_suspend = dummy_bus_suspend,
2501 .bus_resume = dummy_bus_resume,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002502
2503 .alloc_streams = dummy_alloc_streams,
2504 .free_streams = dummy_free_streams,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505};
2506
Alan Stern8364d6b2005-11-14 12:16:30 -05002507static int dummy_hcd_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002509 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002510 struct usb_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002511 struct usb_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 int retval;
2513
Alan Stern8364d6b2005-11-14 12:16:30 -05002514 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002515 dum = *((void **)dev_get_platdata(&pdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002517 if (!mod_data.is_super_speed)
2518 dummy_hcd.flags = HCD_USB2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002519 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2520 if (!hs_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 return -ENOMEM;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002522 hs_hcd->has_tt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002524 retval = usb_add_hcd(hs_hcd, 0, 0);
Sebastian Andrzej Siewior1b68a4c2012-08-19 21:54:58 +02002525 if (retval)
2526 goto put_usb2_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002527
2528 if (mod_data.is_super_speed) {
2529 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2530 dev_name(&pdev->dev), hs_hcd);
2531 if (!ss_hcd) {
2532 retval = -ENOMEM;
2533 goto dealloc_usb2_hcd;
2534 }
2535
2536 retval = usb_add_hcd(ss_hcd, 0, 0);
2537 if (retval)
2538 goto put_usb3_hcd;
2539 }
2540 return 0;
2541
2542put_usb3_hcd:
2543 usb_put_hcd(ss_hcd);
2544dealloc_usb2_hcd:
Sebastian Andrzej Siewior1b68a4c2012-08-19 21:54:58 +02002545 usb_remove_hcd(hs_hcd);
2546put_usb2_hcd:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002547 usb_put_hcd(hs_hcd);
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002548 dum->hs_hcd = dum->ss_hcd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 return retval;
2550}
2551
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002552static int dummy_hcd_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002554 struct dummy *dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002556 dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002557
2558 if (dum->ss_hcd) {
2559 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2560 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2561 }
2562
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002563 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2564 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002565
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002566 dum->hs_hcd = NULL;
2567 dum->ss_hcd = NULL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002568
Alan Sternd9b76252005-05-03 16:15:43 -04002569 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
2571
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002572static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state)
Alan Stern391eca92005-05-10 15:34:16 -04002573{
2574 struct usb_hcd *hcd;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002575 struct dummy_hcd *dum_hcd;
Alan Stern3cf0a222005-11-29 12:08:15 -05002576 int rc = 0;
Alan Stern391eca92005-05-10 15:34:16 -04002577
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002578 dev_dbg(&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002579
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002580 hcd = platform_get_drvdata(pdev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002581 dum_hcd = hcd_to_dummy_hcd(hcd);
2582 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern3cf0a222005-11-29 12:08:15 -05002583 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2584 rc = -EBUSY;
2585 } else
2586 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2587 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002588}
2589
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002590static int dummy_hcd_resume(struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04002591{
2592 struct usb_hcd *hcd;
2593
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002594 dev_dbg(&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002595
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002596 hcd = platform_get_drvdata(pdev);
Alan Stern3cf0a222005-11-29 12:08:15 -05002597 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002598 usb_hcd_poll_rh_status(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002599 return 0;
2600}
2601
Russell King3ae5eae2005-11-09 22:32:44 +00002602static struct platform_driver dummy_hcd_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04002603 .probe = dummy_hcd_probe,
2604 .remove = dummy_hcd_remove,
Alan Stern391eca92005-05-10 15:34:16 -04002605 .suspend = dummy_hcd_suspend,
2606 .resume = dummy_hcd_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002607 .driver = {
2608 .name = (char *) driver_name,
2609 .owner = THIS_MODULE,
2610 },
Alan Sternd9b76252005-05-03 16:15:43 -04002611};
2612
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002614#define MAX_NUM_UDC 2
Sebastian Andrzej Siewiorb2113132012-10-30 12:53:17 +01002615static struct platform_device *the_udc_pdev[MAX_NUM_UDC];
2616static struct platform_device *the_hcd_pdev[MAX_NUM_UDC];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002618static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002620 int retval = -ENOMEM;
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002621 int i;
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002622 struct dummy *dum[MAX_NUM_UDC];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002624 if (usb_disabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 return -ENODEV;
Alan Sternd9b76252005-05-03 16:15:43 -04002626
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +03002627 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2628 return -EINVAL;
2629
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002630 if (mod_data.num < 1 || mod_data.num > MAX_NUM_UDC) {
2631 pr_err("Number of emulated UDC must be in range of 1…%d\n",
2632 MAX_NUM_UDC);
2633 return -EINVAL;
2634 }
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002635
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002636 for (i = 0; i < mod_data.num; i++) {
2637 the_hcd_pdev[i] = platform_device_alloc(driver_name, i);
2638 if (!the_hcd_pdev[i]) {
2639 i--;
2640 while (i >= 0)
2641 platform_device_put(the_hcd_pdev[i--]);
2642 return retval;
2643 }
2644 }
2645 for (i = 0; i < mod_data.num; i++) {
2646 the_udc_pdev[i] = platform_device_alloc(gadget_name, i);
2647 if (!the_udc_pdev[i]) {
2648 i--;
2649 while (i >= 0)
2650 platform_device_put(the_udc_pdev[i--]);
2651 goto err_alloc_udc;
2652 }
2653 }
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002654 for (i = 0; i < mod_data.num; i++) {
2655 dum[i] = kzalloc(sizeof(struct dummy), GFP_KERNEL);
Wei Yongjun481d3042013-05-07 19:50:06 +08002656 if (!dum[i]) {
2657 retval = -ENOMEM;
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002658 goto err_add_pdata;
Wei Yongjun481d3042013-05-07 19:50:06 +08002659 }
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002660 retval = platform_device_add_data(the_hcd_pdev[i], &dum[i],
2661 sizeof(void *));
2662 if (retval)
2663 goto err_add_pdata;
2664 retval = platform_device_add_data(the_udc_pdev[i], &dum[i],
2665 sizeof(void *));
2666 if (retval)
2667 goto err_add_pdata;
2668 }
Alan Sternd9b76252005-05-03 16:15:43 -04002669
Alan Sterna89a2cd2008-04-07 15:03:25 -04002670 retval = platform_driver_register(&dummy_hcd_driver);
2671 if (retval < 0)
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002672 goto err_add_pdata;
Alan Sterna89a2cd2008-04-07 15:03:25 -04002673 retval = platform_driver_register(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002674 if (retval < 0)
2675 goto err_register_udc_driver;
2676
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002677 for (i = 0; i < mod_data.num; i++) {
2678 retval = platform_device_add(the_hcd_pdev[i]);
2679 if (retval < 0) {
2680 i--;
2681 while (i >= 0)
2682 platform_device_del(the_hcd_pdev[i--]);
2683 goto err_add_hcd;
2684 }
2685 }
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002686 for (i = 0; i < mod_data.num; i++) {
2687 if (!dum[i]->hs_hcd ||
2688 (!dum[i]->ss_hcd && mod_data.is_super_speed)) {
2689 /*
2690 * The hcd was added successfully but its probe
2691 * function failed for some reason.
2692 */
2693 retval = -EINVAL;
2694 goto err_add_udc;
2695 }
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002696 }
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002697
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002698 for (i = 0; i < mod_data.num; i++) {
2699 retval = platform_device_add(the_udc_pdev[i]);
2700 if (retval < 0) {
2701 i--;
2702 while (i >= 0)
2703 platform_device_del(the_udc_pdev[i]);
2704 goto err_add_udc;
2705 }
2706 }
2707
2708 for (i = 0; i < mod_data.num; i++) {
2709 if (!platform_get_drvdata(the_udc_pdev[i])) {
2710 /*
2711 * The udc was added successfully but its probe
2712 * function failed for some reason.
2713 */
2714 retval = -EINVAL;
2715 goto err_probe_udc;
2716 }
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002717 }
Alan Sternd9b76252005-05-03 16:15:43 -04002718 return retval;
2719
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002720err_probe_udc:
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002721 for (i = 0; i < mod_data.num; i++)
2722 platform_device_del(the_udc_pdev[i]);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002723err_add_udc:
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002724 for (i = 0; i < mod_data.num; i++)
2725 platform_device_del(the_hcd_pdev[i]);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002726err_add_hcd:
2727 platform_driver_unregister(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002728err_register_udc_driver:
Alan Sterna89a2cd2008-04-07 15:03:25 -04002729 platform_driver_unregister(&dummy_hcd_driver);
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002730err_add_pdata:
2731 for (i = 0; i < mod_data.num; i++)
2732 kfree(dum[i]);
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002733 for (i = 0; i < mod_data.num; i++)
2734 platform_device_put(the_udc_pdev[i]);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002735err_alloc_udc:
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002736 for (i = 0; i < mod_data.num; i++)
2737 platform_device_put(the_hcd_pdev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return retval;
2739}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002740module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002742static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002744 int i;
2745
2746 for (i = 0; i < mod_data.num; i++) {
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002747 struct dummy *dum;
2748
2749 dum = *((void **)dev_get_platdata(&the_udc_pdev[i]->dev));
2750
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002751 platform_device_unregister(the_udc_pdev[i]);
2752 platform_device_unregister(the_hcd_pdev[i]);
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002753 kfree(dum);
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002754 }
Alan Sterna89a2cd2008-04-07 15:03:25 -04002755 platform_driver_unregister(&dummy_udc_driver);
2756 platform_driver_unregister(&dummy_hcd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002758module_exit(cleanup);