blob: 2b54955d3166007b74a64f419cc34f8b76d440c3 [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);
261 req->req.complete(&ep->ep, &req->req);
262 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
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300370 /* if !connected or reset */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300371 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
372 (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300373 /*
374 * We're connected and not reset (reset occurred now),
375 * and driver attached - disconnect!
376 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300377 if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300378 (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
379 dum->driver) {
380 stop_activity(dum);
381 spin_unlock(&dum->lock);
382 dum->driver->disconnect(&dum->gadget);
383 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400384 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300385 } else if (dum_hcd->active != dum_hcd->old_active) {
386 if (dum_hcd->old_active && dum->driver->suspend) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300387 spin_unlock(&dum->lock);
388 dum->driver->suspend(&dum->gadget);
389 spin_lock(&dum->lock);
390 } else if (!dum_hcd->old_active && dum->driver->resume) {
391 spin_unlock(&dum->lock);
392 dum->driver->resume(&dum->gadget);
393 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400394 }
395 }
396
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300397 dum_hcd->old_status = dum_hcd->port_status;
398 dum_hcd->old_active = dum_hcd->active;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400399}
400
401/*-------------------------------------------------------------------------*/
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403/* SLAVE/GADGET SIDE DRIVER
404 *
405 * This only tracks gadget state. All the work is done when the host
406 * side tries some (emulated) i/o operation. Real device controller
407 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
408 */
409
410#define is_enabled(dum) \
411 (dum->port_status & USB_PORT_STAT_ENABLE)
412
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100413static int dummy_enable(struct usb_ep *_ep,
414 const struct usb_endpoint_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300417 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 struct dummy_ep *ep;
419 unsigned max;
420 int retval;
421
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100422 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (!_ep || !desc || ep->desc || _ep->name == ep0name
424 || desc->bDescriptorType != USB_DT_ENDPOINT)
425 return -EINVAL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100426 dum = ep_to_dummy(ep);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300427 if (!dum->driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return -ESHUTDOWN;
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200429
430 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300431 if (!is_enabled(dum_hcd))
432 return -ESHUTDOWN;
433
434 /*
435 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
436 * maximum packet size.
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300437 * For SS devices the wMaxPacketSize is limited by 1024.
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300438 */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700439 max = usb_endpoint_maxp(desc) & 0x7ff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 /* drivers must not request bad settings, since lower levels
442 * (hardware or its drivers) may not check. some endpoints
443 * can't do iso, many have maxpacket limitations, etc.
444 *
445 * since this "hardware" driver is here to help debugging, we
446 * have some extra sanity checks. (there could be more though,
447 * especially for "ep9out" style fixed function ones.)
448 */
449 retval = -EINVAL;
Sebastian Andrzej Siewior59f08e62012-01-12 12:53:14 +0100450 switch (usb_endpoint_type(desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 case USB_ENDPOINT_XFER_BULK:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100452 if (strstr(ep->ep.name, "-iso")
453 || strstr(ep->ep.name, "-int")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 goto done;
455 }
456 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300457 case USB_SPEED_SUPER:
458 if (max == 1024)
459 break;
460 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 case USB_SPEED_HIGH:
462 if (max == 512)
463 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700464 goto done;
465 case USB_SPEED_FULL:
466 if (max == 8 || max == 16 || max == 32 || max == 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /* we'll fake any legal size */
468 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700469 /* save a return statement */
470 default:
471 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473 break;
474 case USB_ENDPOINT_XFER_INT:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100475 if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 goto done;
477 /* real hardware might not handle all packet sizes */
478 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300479 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 case USB_SPEED_HIGH:
481 if (max <= 1024)
482 break;
483 /* save a return statement */
484 case USB_SPEED_FULL:
485 if (max <= 64)
486 break;
487 /* save a return statement */
488 default:
489 if (max <= 8)
490 break;
491 goto done;
492 }
493 break;
494 case USB_ENDPOINT_XFER_ISOC:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100495 if (strstr(ep->ep.name, "-bulk")
496 || strstr(ep->ep.name, "-int"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 goto done;
498 /* real hardware might not handle all packet sizes */
499 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300500 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 case USB_SPEED_HIGH:
502 if (max <= 1024)
503 break;
504 /* save a return statement */
505 case USB_SPEED_FULL:
506 if (max <= 1023)
507 break;
508 /* save a return statement */
509 default:
510 goto done;
511 }
512 break;
513 default:
514 /* few chips support control except on ep0 */
515 goto done;
516 }
517
518 _ep->maxpacket = max;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100519 if (usb_ss_max_streams(_ep->comp_desc)) {
520 if (!usb_endpoint_xfer_bulk(desc)) {
521 dev_err(udc_dev(dum), "Can't enable stream support on "
522 "non-bulk ep %s\n", _ep->name);
523 return -EINVAL;
524 }
525 ep->stream_en = 1;
526 }
Sebastian Andrzej Siewior3cf0ad02012-01-25 11:51:19 +0100527 ep->desc = desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100529 dev_dbg(udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 _ep->name,
531 desc->bEndpointAddress & 0x0f,
532 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
533 ({ char *val;
Sebastian Andrzej Siewior59f08e62012-01-12 12:53:14 +0100534 switch (usb_endpoint_type(desc)) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +0300535 case USB_ENDPOINT_XFER_BULK:
536 val = "bulk";
537 break;
538 case USB_ENDPOINT_XFER_ISOC:
539 val = "iso";
540 break;
541 case USB_ENDPOINT_XFER_INT:
542 val = "intr";
543 break;
544 default:
545 val = "ctrl";
546 break;
Joe Perches2b84f922013-10-08 16:01:37 -0700547 } val; }),
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100548 max, ep->stream_en ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /* at this point real hardware should be NAKing transfers
551 * to that endpoint, until a buffer is queued to it.
552 */
Alan Stern851a5262008-08-14 15:48:30 -0400553 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 retval = 0;
555done:
556 return retval;
557}
558
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100559static int dummy_disable(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 struct dummy_ep *ep;
562 struct dummy *dum;
563 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100565 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (!_ep || !ep->desc || _ep->name == ep0name)
567 return -EINVAL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100568 dum = ep_to_dummy(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100570 spin_lock_irqsave(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 ep->desc = NULL;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100572 ep->stream_en = 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100573 nuke(dum, ep);
574 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100576 dev_dbg(udc_dev(dum), "disabled %s\n", _ep->name);
Julia Lawall849b1332014-05-19 06:31:07 +0200577 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100580static struct usb_request *dummy_alloc_request(struct usb_ep *_ep,
581 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 struct dummy_ep *ep;
584 struct dummy_request *req;
585
586 if (!_ep)
587 return NULL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100588 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800590 req = kzalloc(sizeof(*req), mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (!req)
592 return NULL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100593 INIT_LIST_HEAD(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return &req->req;
595}
596
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100597static void dummy_free_request(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 struct dummy_request *req;
600
Sebastian Andrzej Siewiorf99987b2012-02-09 09:24:59 +0100601 if (!_ep || !_req) {
Sasha Levin72688dc2012-05-11 06:39:37 +0200602 WARN_ON(1);
Sebastian Andrzej Siewior20edfbb2012-01-25 15:18:58 +0100603 return;
Sebastian Andrzej Siewiorf99987b2012-02-09 09:24:59 +0100604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100606 req = usb_request_to_dummy_request(_req);
607 WARN_ON(!list_empty(&req->queue));
608 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100611static void fifo_complete(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613}
614
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100615static int dummy_queue(struct usb_ep *_ep, struct usb_request *_req,
Al Viro55016f12005-10-21 03:21:58 -0400616 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 struct dummy_ep *ep;
619 struct dummy_request *req;
620 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300621 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 unsigned long flags;
623
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100624 req = usb_request_to_dummy_request(_req);
625 if (!_req || !list_empty(&req->queue) || !_req->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return -EINVAL;
627
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100628 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (!_ep || (!ep->desc && _ep->name != ep0name))
630 return -EINVAL;
631
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100632 dum = ep_to_dummy(ep);
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200633 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300634 if (!dum->driver || !is_enabled(dum_hcd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return -ESHUTDOWN;
636
637#if 0
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100638 dev_dbg(udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 ep, _req, _ep->name, _req->length, _req->buf);
640#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 _req->status = -EINPROGRESS;
642 _req->actual = 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100643 spin_lock_irqsave(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 /* implement an emulated single-request FIFO */
646 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100647 list_empty(&dum->fifo_req.queue) &&
648 list_empty(&ep->queue) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 _req->length <= FIFO_SIZE) {
650 req = &dum->fifo_req;
651 req->req = *_req;
652 req->req.buf = dum->fifo_buf;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100653 memcpy(dum->fifo_buf, _req->buf, _req->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 req->req.context = dum;
655 req->req.complete = fifo_complete;
656
David Brownellc728df72008-07-26 08:06:24 -0700657 list_add_tail(&req->queue, &ep->queue);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100658 spin_unlock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 _req->actual = _req->length;
660 _req->status = 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100661 _req->complete(_ep, _req);
662 spin_lock(&dum->lock);
David Brownellc728df72008-07-26 08:06:24 -0700663 } else
664 list_add_tail(&req->queue, &ep->queue);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100665 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /* real hardware would likely enable transfers here, in case
668 * it'd been left NAKing.
669 */
670 return 0;
671}
672
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100673static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 struct dummy_ep *ep;
676 struct dummy *dum;
677 int retval = -EINVAL;
678 unsigned long flags;
679 struct dummy_request *req = NULL;
680
681 if (!_ep || !_req)
682 return retval;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100683 ep = usb_ep_to_dummy_ep(_ep);
684 dum = ep_to_dummy(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 if (!dum->driver)
687 return -ESHUTDOWN;
688
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100689 local_irq_save(flags);
690 spin_lock(&dum->lock);
691 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (&req->req == _req) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100693 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 _req->status = -ECONNRESET;
695 retval = 0;
696 break;
697 }
698 }
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100699 spin_unlock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 if (retval == 0) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100702 dev_dbg(udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 "dequeued req %p from %s, len %d buf %p\n",
704 req, _ep->name, _req->length, _req->buf);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100705 _req->complete(_ep, _req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100707 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return retval;
709}
710
711static int
Alan Stern851a5262008-08-14 15:48:30 -0400712dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 struct dummy_ep *ep;
715 struct dummy *dum;
716
717 if (!_ep)
718 return -EINVAL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100719 ep = usb_ep_to_dummy_ep(_ep);
720 dum = ep_to_dummy(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (!dum->driver)
722 return -ESHUTDOWN;
723 if (!value)
Alan Stern851a5262008-08-14 15:48:30 -0400724 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100726 !list_empty(&ep->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return -EAGAIN;
Alan Stern851a5262008-08-14 15:48:30 -0400728 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 ep->halted = 1;
Alan Stern851a5262008-08-14 15:48:30 -0400730 if (wedged)
731 ep->wedged = 1;
732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /* FIXME clear emulated data toggle too */
734 return 0;
735}
736
Alan Stern851a5262008-08-14 15:48:30 -0400737static int
738dummy_set_halt(struct usb_ep *_ep, int value)
739{
740 return dummy_set_halt_and_wedge(_ep, value, 0);
741}
742
743static int dummy_set_wedge(struct usb_ep *_ep)
744{
745 if (!_ep || _ep->name == ep0name)
746 return -EINVAL;
747 return dummy_set_halt_and_wedge(_ep, 1, 1);
748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750static const struct usb_ep_ops dummy_ep_ops = {
751 .enable = dummy_enable,
752 .disable = dummy_disable,
753
754 .alloc_request = dummy_alloc_request,
755 .free_request = dummy_free_request,
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 .queue = dummy_queue,
758 .dequeue = dummy_dequeue,
759
760 .set_halt = dummy_set_halt,
Alan Stern851a5262008-08-14 15:48:30 -0400761 .set_wedge = dummy_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762};
763
764/*-------------------------------------------------------------------------*/
765
766/* there are both host and device side versions of this call ... */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100767static int dummy_g_get_frame(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 struct timeval tv;
770
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100771 do_gettimeofday(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return tv.tv_usec / 1000;
773}
774
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100775static int dummy_wakeup(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300777 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300779 dum_hcd = gadget_to_dummy_hcd(_gadget);
780 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
Alan Stern5742b0c2005-05-02 11:25:17 -0400781 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return -EINVAL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300783 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
Alan Stern391eca92005-05-10 15:34:16 -0400784 return -ENOLINK;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300785 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
786 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -0400787 return -EIO;
788
789 /* FIXME: What if the root hub is suspended but the port isn't? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 /* hub notices our request, issues downstream resume, etc */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300792 dum_hcd->resuming = 1;
793 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
794 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return 0;
796}
797
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100798static int dummy_set_selfpowered(struct usb_gadget *_gadget, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 struct dummy *dum;
801
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100802 dum = gadget_to_dummy_hcd(_gadget)->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (value)
804 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
805 else
806 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
807 return 0;
808}
809
Sebastian Andrzej Siewiord2621272012-01-09 13:14:59 +0100810static void dummy_udc_update_ep0(struct dummy *dum)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200811{
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100812 if (dum->gadget.speed == USB_SPEED_SUPER)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200813 dum->ep[0].ep.maxpacket = 9;
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100814 else
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200815 dum->ep[0].ep.maxpacket = 64;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200816}
817
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100818static int dummy_pullup(struct usb_gadget *_gadget, int value)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400819{
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200820 struct dummy_hcd *dum_hcd;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400821 struct dummy *dum;
822 unsigned long flags;
823
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200824 dum = gadget_dev_to_dummy(&_gadget->dev);
825
826 if (value && dum->driver) {
827 if (mod_data.is_super_speed)
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100828 dum->gadget.speed = dum->driver->max_speed;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200829 else if (mod_data.is_high_speed)
830 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100831 dum->driver->max_speed);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200832 else
833 dum->gadget.speed = USB_SPEED_FULL;
Sebastian Andrzej Siewiord2621272012-01-09 13:14:59 +0100834 dummy_udc_update_ep0(dum);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200835
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100836 if (dum->gadget.speed < dum->driver->max_speed)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200837 dev_dbg(udc_dev(dum), "This device can perform faster"
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100838 " if you connect it to a %s port...\n",
839 usb_speed_string(dum->driver->max_speed));
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200840 }
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200841 dum_hcd = gadget_to_dummy_hcd(_gadget);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200842
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100843 spin_lock_irqsave(&dum->lock, flags);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400844 dum->pullup = (value != 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200845 set_link_state(dum_hcd);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100846 spin_unlock_irqrestore(&dum->lock, flags);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200847
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200848 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Sternf1c39fa2005-05-03 16:24:04 -0400849 return 0;
850}
851
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200852static int dummy_udc_start(struct usb_gadget *g,
853 struct usb_gadget_driver *driver);
854static int dummy_udc_stop(struct usb_gadget *g,
855 struct usb_gadget_driver *driver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857static const struct usb_gadget_ops dummy_ops = {
858 .get_frame = dummy_g_get_frame,
859 .wakeup = dummy_wakeup,
860 .set_selfpowered = dummy_set_selfpowered,
Alan Sternf1c39fa2005-05-03 16:24:04 -0400861 .pullup = dummy_pullup,
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200862 .udc_start = dummy_udc_start,
863 .udc_stop = dummy_udc_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864};
865
866/*-------------------------------------------------------------------------*/
867
868/* "function" sysfs attribute */
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700869static ssize_t function_show(struct device *dev, struct device_attribute *attr,
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100870 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100872 struct dummy *dum = gadget_dev_to_dummy(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 if (!dum->driver || !dum->driver->function)
875 return 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100876 return scnprintf(buf, PAGE_SIZE, "%s\n", dum->driver->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -0700878static DEVICE_ATTR_RO(function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880/*-------------------------------------------------------------------------*/
881
882/*
883 * Driver registration/unregistration.
884 *
885 * This is basically hardware-specific; there's usually only one real USB
886 * device (not host) controller since that's how USB devices are intended
887 * to work. So most implementations of these api calls will rely on the
888 * fact that only one driver will ever bind to the hardware. But curious
889 * hardware can be built with discrete components, so the gadget API doesn't
890 * require that assumption.
891 *
892 * For this emulator, it might be convenient to create a usb slave device
893 * for each driver that registers: just add to a big root hub.
894 */
895
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200896static int dummy_udc_start(struct usb_gadget *g,
897 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200899 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
900 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100902 if (driver->max_speed == USB_SPEED_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return -EINVAL;
904
905 /*
906 * SLAVE side init ... the layer above hardware, which
907 * can't enumerate without help from the driver we're binding.
908 */
Alan Stern5742b0c2005-05-02 11:25:17 -0400909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 dum->devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 dum->driver = driver;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100913 dev_dbg(udc_dev(dum), "binding gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return 0;
916}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200918static int dummy_udc_stop(struct usb_gadget *g,
919 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200921 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
922 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Alan Stern5f5610f2013-07-30 15:18:15 -0400924 if (driver)
925 dev_dbg(udc_dev(dum), "unregister gadget driver '%s'\n",
926 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 dum->driver = NULL;
Sebastian Andrzej Siewior25427872011-06-16 20:36:55 +0200929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return 0;
931}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933#undef is_enabled
934
Alan Sternd9b76252005-05-03 16:15:43 -0400935/* The gadget structure is stored inside the hcd structure and will be
936 * released along with it. */
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200937static void init_dummy_udc_hw(struct dummy *dum)
938{
939 int i;
940
941 INIT_LIST_HEAD(&dum->gadget.ep_list);
942 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
943 struct dummy_ep *ep = &dum->ep[i];
944
945 if (!ep_name[i])
946 break;
947 ep->ep.name = ep_name[i];
948 ep->ep.ops = &dummy_ep_ops;
949 list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
950 ep->halted = ep->wedged = ep->already_seen =
951 ep->setup_stage = 0;
Robert Baldygae117e742013-12-13 12:23:38 +0100952 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100953 ep->ep.max_streams = 16;
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200954 ep->last_io = jiffies;
955 ep->gadget = &dum->gadget;
956 ep->desc = NULL;
957 INIT_LIST_HEAD(&ep->queue);
958 }
959
960 dum->gadget.ep0 = &dum->ep[0].ep;
961 list_del_init(&dum->ep[0].ep.ep_list);
962 INIT_LIST_HEAD(&dum->fifo_req.queue);
Sebastian Andrzej Siewiorf8744d42011-06-23 14:26:13 +0200963
964#ifdef CONFIG_USB_OTG
965 dum->gadget.is_otg = 1;
966#endif
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200967}
968
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100969static int dummy_udc_probe(struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -0400970{
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +0100971 struct dummy *dum;
Alan Sternd9b76252005-05-03 16:15:43 -0400972 int rc;
973
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +0100974 dum = *((void **)dev_get_platdata(&pdev->dev));
Alan Sternd9b76252005-05-03 16:15:43 -0400975 dum->gadget.name = gadget_name;
976 dum->gadget.ops = &dummy_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100977 dum->gadget.max_speed = USB_SPEED_SUPER;
Alan Sternd9b76252005-05-03 16:15:43 -0400978
Alan Stern8364d6b2005-11-14 12:16:30 -0500979 dum->gadget.dev.parent = &pdev->dev;
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200980 init_dummy_udc_hw(dum);
981
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300982 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
983 if (rc < 0)
984 goto err_udc;
985
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100986 rc = device_create_file(&dum->gadget.dev, &dev_attr_function);
Alan Sternefd54a32006-09-25 11:55:56 -0400987 if (rc < 0)
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300988 goto err_dev;
989 platform_set_drvdata(pdev, dum);
990 return rc;
991
992err_dev:
993 usb_del_gadget_udc(&dum->gadget);
994err_udc:
Alan Sternd9b76252005-05-03 16:15:43 -0400995 return rc;
996}
997
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100998static int dummy_udc_remove(struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -0400999{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001000 struct dummy *dum = platform_get_drvdata(pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04001001
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001002 device_remove_file(&dum->gadget.dev, &dev_attr_function);
Alan Stern5f5610f2013-07-30 15:18:15 -04001003 usb_del_gadget_udc(&dum->gadget);
Alan Sternd9b76252005-05-03 16:15:43 -04001004 return 0;
1005}
1006
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001007static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1008 int suspend)
Alan Stern391eca92005-05-10 15:34:16 -04001009{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001010 spin_lock_irq(&dum->lock);
1011 dum->udc_suspended = suspend;
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001012 set_link_state(dum_hcd);
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001013 spin_unlock_irq(&dum->lock);
1014}
Alan Stern391eca92005-05-10 15:34:16 -04001015
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001016static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1017{
1018 struct dummy *dum = platform_get_drvdata(pdev);
1019 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1020
1021 dev_dbg(&pdev->dev, "%s\n", __func__);
1022 dummy_udc_pm(dum, dum_hcd, 1);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001023 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001024 return 0;
1025}
1026
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001027static int dummy_udc_resume(struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04001028{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001029 struct dummy *dum = platform_get_drvdata(pdev);
1030 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Alan Stern391eca92005-05-10 15:34:16 -04001031
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001032 dev_dbg(&pdev->dev, "%s\n", __func__);
1033 dummy_udc_pm(dum, dum_hcd, 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001034 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001035 return 0;
1036}
1037
Russell King3ae5eae2005-11-09 22:32:44 +00001038static struct platform_driver dummy_udc_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04001039 .probe = dummy_udc_probe,
1040 .remove = dummy_udc_remove,
Alan Stern391eca92005-05-10 15:34:16 -04001041 .suspend = dummy_udc_suspend,
1042 .resume = dummy_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001043 .driver = {
1044 .name = (char *) gadget_name,
1045 .owner = THIS_MODULE,
1046 },
Alan Sternd9b76252005-05-03 16:15:43 -04001047};
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049/*-------------------------------------------------------------------------*/
1050
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001051static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc)
1052{
1053 unsigned int index;
1054
1055 index = usb_endpoint_num(desc) << 1;
1056 if (usb_endpoint_dir_in(desc))
1057 index |= 1;
1058 return index;
1059}
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061/* MASTER/HOST SIDE DRIVER
1062 *
1063 * this uses the hcd framework to hook up to host side drivers.
1064 * its root hub will only have one device, otherwise it acts like
1065 * a normal host controller.
1066 *
1067 * when urbs are queued, they're just stuck on a list that we
1068 * scan in a timer callback. that callback connects writes from
1069 * the host with reads from the device, and so on, based on the
1070 * usb 2.0 rules.
1071 */
1072
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001073static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb)
1074{
1075 const struct usb_endpoint_descriptor *desc = &urb->ep->desc;
1076 u32 index;
1077
1078 if (!usb_endpoint_xfer_bulk(desc))
1079 return 0;
1080
1081 index = dummy_get_ep_idx(desc);
1082 return (1 << index) & dum_hcd->stream_en_ep;
1083}
1084
1085/*
1086 * The max stream number is saved as a nibble so for the 30 possible endpoints
1087 * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
1088 * means we use only 1 stream). The maximum according to the spec is 16bit so
1089 * if the 16 stream limit is about to go, the array size should be incremented
1090 * to 30 elements of type u16.
1091 */
1092static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1093 unsigned int pipe)
1094{
1095 int max_streams;
1096
1097 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1098 if (usb_pipeout(pipe))
1099 max_streams >>= 4;
1100 else
1101 max_streams &= 0xf;
1102 max_streams++;
1103 return max_streams;
1104}
1105
1106static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1107 unsigned int pipe, unsigned int streams)
1108{
1109 int max_streams;
1110
1111 streams--;
1112 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1113 if (usb_pipeout(pipe)) {
1114 streams <<= 4;
1115 max_streams &= 0xf;
1116 } else {
1117 max_streams &= 0xf0;
1118 }
1119 max_streams |= streams;
1120 dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams;
1121}
1122
1123static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb)
1124{
1125 unsigned int max_streams;
1126 int enabled;
1127
1128 enabled = dummy_ep_stream_en(dum_hcd, urb);
1129 if (!urb->stream_id) {
1130 if (enabled)
1131 return -EINVAL;
1132 return 0;
1133 }
1134 if (!enabled)
1135 return -EINVAL;
1136
1137 max_streams = get_max_streams_for_pipe(dum_hcd,
1138 usb_pipeendpoint(urb->pipe));
1139 if (urb->stream_id > max_streams) {
1140 dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n",
1141 urb->stream_id);
1142 BUG();
1143 return -EINVAL;
1144 }
1145 return 0;
1146}
1147
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001148static int dummy_urb_enqueue(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 struct usb_hcd *hcd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001151 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001153 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 struct urbp *urbp;
1155 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001156 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001158 urbp = kmalloc(sizeof *urbp, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (!urbp)
1160 return -ENOMEM;
1161 urbp->urb = urb;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001162 urbp->miter_started = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001164 dum_hcd = hcd_to_dummy_hcd(hcd);
1165 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001166
1167 rc = dummy_validate_stream(dum_hcd, urb);
1168 if (rc) {
1169 kfree(urbp);
1170 goto done;
1171 }
1172
Alan Sterne9df41c2007-08-08 11:48:02 -04001173 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1174 if (rc) {
1175 kfree(urbp);
1176 goto done;
1177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001179 if (!dum_hcd->udev) {
1180 dum_hcd->udev = urb->dev;
1181 usb_get_dev(dum_hcd->udev);
1182 } else if (unlikely(dum_hcd->udev != urb->dev))
1183 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001185 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 urb->hcpriv = urbp;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001187 if (usb_pipetype(urb->pipe) == PIPE_CONTROL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 urb->error_count = 1; /* mark as a new urb */
1189
1190 /* kick the scheduler, it'll do the rest */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001191 if (!timer_pending(&dum_hcd->timer))
1192 mod_timer(&dum_hcd->timer, jiffies + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Alan Sterne9df41c2007-08-08 11:48:02 -04001194 done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001195 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001196 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Alan Sterne9df41c2007-08-08 11:48:02 -04001199static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001201 struct dummy_hcd *dum_hcd;
Alan Stern391eca92005-05-10 15:34:16 -04001202 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001203 int rc;
Alan Stern391eca92005-05-10 15:34:16 -04001204
1205 /* giveback happens automatically in timer callback,
1206 * so make sure the callback happens */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001207 dum_hcd = hcd_to_dummy_hcd(hcd);
1208 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001209
1210 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001211 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1212 !list_empty(&dum_hcd->urbp_list))
1213 mod_timer(&dum_hcd->timer, jiffies);
Alan Sterne9df41c2007-08-08 11:48:02 -04001214
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001215 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001216 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001219static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
1220 u32 len)
1221{
1222 void *ubuf, *rbuf;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001223 struct urbp *urbp = urb->hcpriv;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001224 int to_host;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001225 struct sg_mapping_iter *miter = &urbp->miter;
1226 u32 trans = 0;
1227 u32 this_sg;
1228 bool next_sg;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001229
1230 to_host = usb_pipein(urb->pipe);
1231 rbuf = req->req.buf + req->req.actual;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001232
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001233 if (!urb->num_sgs) {
1234 ubuf = urb->transfer_buffer + urb->actual_length;
1235 if (to_host)
1236 memcpy(ubuf, rbuf, len);
1237 else
1238 memcpy(rbuf, ubuf, len);
1239 return len;
1240 }
1241
1242 if (!urbp->miter_started) {
1243 u32 flags = SG_MITER_ATOMIC;
1244
1245 if (to_host)
1246 flags |= SG_MITER_TO_SG;
1247 else
1248 flags |= SG_MITER_FROM_SG;
1249
1250 sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
1251 urbp->miter_started = 1;
1252 }
1253 next_sg = sg_miter_next(miter);
1254 if (next_sg == false) {
1255 WARN_ON_ONCE(1);
1256 return -EINVAL;
1257 }
1258 do {
1259 ubuf = miter->addr;
1260 this_sg = min_t(u32, len, miter->length);
1261 miter->consumed = this_sg;
1262 trans += this_sg;
1263
1264 if (to_host)
1265 memcpy(ubuf, rbuf, this_sg);
1266 else
1267 memcpy(rbuf, ubuf, this_sg);
1268 len -= this_sg;
1269
1270 if (!len)
1271 break;
1272 next_sg = sg_miter_next(miter);
1273 if (next_sg == false) {
1274 WARN_ON_ONCE(1);
1275 return -EINVAL;
1276 }
1277
1278 rbuf += this_sg;
1279 } while (1);
1280
1281 sg_miter_stop(miter);
1282 return trans;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001283}
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285/* transfer up to a frame's worth; caller must own lock */
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001286static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb,
1287 struct dummy_ep *ep, int limit, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001289 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 struct dummy_request *req;
1291
1292top:
1293 /* if there's no request queued, the device is NAKing; return */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001294 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 unsigned host_len, dev_len, len;
1296 int is_short, to_host;
1297 int rescan = 0;
1298
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001299 if (dummy_ep_stream_en(dum_hcd, urb)) {
1300 if ((urb->stream_id != req->req.stream_id))
1301 continue;
1302 }
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /* 1..N packets of ep->ep.maxpacket each ... the last one
1305 * may be short (including zero length).
1306 *
1307 * writer can send a zlp explicitly (length 0) or implicitly
1308 * (length mod maxpacket zero, and 'zero' flag); they always
1309 * terminate reads.
1310 */
1311 host_len = urb->transfer_buffer_length - urb->actual_length;
1312 dev_len = req->req.length - req->req.actual;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001313 len = min(host_len, dev_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 /* FIXME update emulated data toggle too */
1316
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001317 to_host = usb_pipein(urb->pipe);
1318 if (unlikely(len == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 is_short = 1;
1320 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 /* not enough bandwidth left? */
1322 if (limit < ep->ep.maxpacket && limit < len)
1323 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001324 len = min_t(unsigned, len, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 if (len == 0)
1326 break;
1327
1328 /* use an extra pass for the final short packet */
1329 if (len > ep->ep.maxpacket) {
1330 rescan = 1;
1331 len -= (len % ep->ep.maxpacket);
1332 }
1333 is_short = (len % ep->ep.maxpacket) != 0;
1334
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001335 len = dummy_perform_transfer(urb, req, len);
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 ep->last_io = jiffies;
Dan Carpenterb1443ac0e2012-03-02 21:51:00 +03001338 if ((int)len < 0) {
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001339 req->req.status = len;
1340 } else {
1341 limit -= len;
1342 urb->actual_length += len;
1343 req->req.actual += len;
1344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346
1347 /* short packets terminate, maybe with overflow/underflow.
1348 * it's only really an error to write too much.
1349 *
1350 * partially filling a buffer optionally blocks queue advances
1351 * (so completion handlers can clean up the queue) but we don't
Alan Sternb0d9efb2007-08-21 15:39:21 -04001352 * need to emulate such data-in-flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 */
1354 if (is_short) {
1355 if (host_len == dev_len) {
1356 req->req.status = 0;
Alan Stern4d2f1102007-08-24 15:40:10 -04001357 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 } else if (to_host) {
1359 req->req.status = 0;
1360 if (dev_len > host_len)
Alan Stern4d2f1102007-08-24 15:40:10 -04001361 *status = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 else
Alan Stern4d2f1102007-08-24 15:40:10 -04001363 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 } else if (!to_host) {
Alan Stern4d2f1102007-08-24 15:40:10 -04001365 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (host_len > dev_len)
1367 req->req.status = -EOVERFLOW;
1368 else
1369 req->req.status = 0;
1370 }
1371
1372 /* many requests terminate without a short packet */
1373 } else {
1374 if (req->req.length == req->req.actual
1375 && !req->req.zero)
1376 req->req.status = 0;
1377 if (urb->transfer_buffer_length == urb->actual_length
1378 && !(urb->transfer_flags
Alan Stern4d2f1102007-08-24 15:40:10 -04001379 & URB_ZERO_PACKET))
1380 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382
1383 /* device side completion --> continuable */
1384 if (req->req.status != -EINPROGRESS) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001385 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001387 spin_unlock(&dum->lock);
1388 req->req.complete(&ep->ep, &req->req);
1389 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 /* requests might have been unlinked... */
1392 rescan = 1;
1393 }
1394
1395 /* host side completion --> terminate */
Alan Stern4d2f1102007-08-24 15:40:10 -04001396 if (*status != -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 break;
1398
1399 /* rescan to continue with any other queued i/o */
1400 if (rescan)
1401 goto top;
1402 }
1403 return limit;
1404}
1405
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001406static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 int limit = ep->ep.maxpacket;
1409
1410 if (dum->gadget.speed == USB_SPEED_HIGH) {
1411 int tmp;
1412
1413 /* high bandwidth mode */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001414 tmp = usb_endpoint_maxp(ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 tmp = (tmp >> 11) & 0x03;
1416 tmp *= 8 /* applies to entire frame */;
1417 limit += limit * tmp;
1418 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001419 if (dum->gadget.speed == USB_SPEED_SUPER) {
Sebastian Andrzej Siewior59f08e62012-01-12 12:53:14 +01001420 switch (usb_endpoint_type(ep->desc)) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001421 case USB_ENDPOINT_XFER_ISOC:
1422 /* Sec. 4.4.8.2 USB3.0 Spec */
1423 limit = 3 * 16 * 1024 * 8;
1424 break;
1425 case USB_ENDPOINT_XFER_INT:
1426 /* Sec. 4.4.7.2 USB3.0 Spec */
1427 limit = 3 * 1024 * 8;
1428 break;
1429 case USB_ENDPOINT_XFER_BULK:
1430 default:
1431 break;
1432 }
1433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return limit;
1435}
1436
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001437#define is_active(dum_hcd) ((dum_hcd->port_status & \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1439 USB_PORT_STAT_SUSPEND)) \
1440 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1441
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001442static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
1444 int i;
1445
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001446 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1447 dum->ss_hcd : dum->hs_hcd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return NULL;
1449 if ((address & ~USB_DIR_IN) == 0)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001450 return &dum->ep[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001452 struct dummy_ep *ep = &dum->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 if (!ep->desc)
1455 continue;
1456 if (ep->desc->bEndpointAddress == address)
1457 return ep;
1458 }
1459 return NULL;
1460}
1461
1462#undef is_active
1463
1464#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1465#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1466#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1467#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1468#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1469#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1470
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001471
1472/**
1473 * handle_control_request() - handles all control transfers
1474 * @dum: pointer to dummy (the_controller)
1475 * @urb: the urb request to handle
1476 * @setup: pointer to the setup data for a USB device control
1477 * request
1478 * @status: pointer to request handling status
1479 *
1480 * Return 0 - if the request was handled
1481 * 1 - if the request wasn't handles
1482 * error code on error
1483 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001484static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001485 struct usb_ctrlrequest *setup,
1486 int *status)
1487{
1488 struct dummy_ep *ep2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001489 struct dummy *dum = dum_hcd->dum;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001490 int ret_val = 1;
1491 unsigned w_index;
1492 unsigned w_value;
1493
1494 w_index = le16_to_cpu(setup->wIndex);
1495 w_value = le16_to_cpu(setup->wValue);
1496 switch (setup->bRequest) {
1497 case USB_REQ_SET_ADDRESS:
1498 if (setup->bRequestType != Dev_Request)
1499 break;
1500 dum->address = w_value;
1501 *status = 0;
1502 dev_dbg(udc_dev(dum), "set_address = %d\n",
1503 w_value);
1504 ret_val = 0;
1505 break;
1506 case USB_REQ_SET_FEATURE:
1507 if (setup->bRequestType == Dev_Request) {
1508 ret_val = 0;
1509 switch (w_value) {
1510 case USB_DEVICE_REMOTE_WAKEUP:
1511 break;
1512 case USB_DEVICE_B_HNP_ENABLE:
1513 dum->gadget.b_hnp_enable = 1;
1514 break;
1515 case USB_DEVICE_A_HNP_SUPPORT:
1516 dum->gadget.a_hnp_support = 1;
1517 break;
1518 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1519 dum->gadget.a_alt_hnp_support = 1;
1520 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001521 case USB_DEVICE_U1_ENABLE:
1522 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1523 HCD_USB3)
1524 w_value = USB_DEV_STAT_U1_ENABLED;
1525 else
1526 ret_val = -EOPNOTSUPP;
1527 break;
1528 case USB_DEVICE_U2_ENABLE:
1529 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1530 HCD_USB3)
1531 w_value = USB_DEV_STAT_U2_ENABLED;
1532 else
1533 ret_val = -EOPNOTSUPP;
1534 break;
1535 case USB_DEVICE_LTM_ENABLE:
1536 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1537 HCD_USB3)
1538 w_value = USB_DEV_STAT_LTM_ENABLED;
1539 else
1540 ret_val = -EOPNOTSUPP;
1541 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001542 default:
1543 ret_val = -EOPNOTSUPP;
1544 }
1545 if (ret_val == 0) {
1546 dum->devstatus |= (1 << w_value);
1547 *status = 0;
1548 }
1549 } else if (setup->bRequestType == Ep_Request) {
1550 /* endpoint halt */
1551 ep2 = find_endpoint(dum, w_index);
1552 if (!ep2 || ep2->ep.name == ep0name) {
1553 ret_val = -EOPNOTSUPP;
1554 break;
1555 }
1556 ep2->halted = 1;
1557 ret_val = 0;
1558 *status = 0;
1559 }
1560 break;
1561 case USB_REQ_CLEAR_FEATURE:
1562 if (setup->bRequestType == Dev_Request) {
1563 ret_val = 0;
1564 switch (w_value) {
1565 case USB_DEVICE_REMOTE_WAKEUP:
1566 w_value = USB_DEVICE_REMOTE_WAKEUP;
1567 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001568 case USB_DEVICE_U1_ENABLE:
1569 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1570 HCD_USB3)
1571 w_value = USB_DEV_STAT_U1_ENABLED;
1572 else
1573 ret_val = -EOPNOTSUPP;
1574 break;
1575 case USB_DEVICE_U2_ENABLE:
1576 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1577 HCD_USB3)
1578 w_value = USB_DEV_STAT_U2_ENABLED;
1579 else
1580 ret_val = -EOPNOTSUPP;
1581 break;
1582 case USB_DEVICE_LTM_ENABLE:
1583 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1584 HCD_USB3)
1585 w_value = USB_DEV_STAT_LTM_ENABLED;
1586 else
1587 ret_val = -EOPNOTSUPP;
1588 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001589 default:
1590 ret_val = -EOPNOTSUPP;
1591 break;
1592 }
1593 if (ret_val == 0) {
1594 dum->devstatus &= ~(1 << w_value);
1595 *status = 0;
1596 }
1597 } else if (setup->bRequestType == Ep_Request) {
1598 /* endpoint halt */
1599 ep2 = find_endpoint(dum, w_index);
1600 if (!ep2) {
1601 ret_val = -EOPNOTSUPP;
1602 break;
1603 }
1604 if (!ep2->wedged)
1605 ep2->halted = 0;
1606 ret_val = 0;
1607 *status = 0;
1608 }
1609 break;
1610 case USB_REQ_GET_STATUS:
1611 if (setup->bRequestType == Dev_InRequest
1612 || setup->bRequestType == Intf_InRequest
1613 || setup->bRequestType == Ep_InRequest) {
1614 char *buf;
1615 /*
1616 * device: remote wakeup, selfpowered
1617 * interface: nothing
1618 * endpoint: halt
1619 */
1620 buf = (char *)urb->transfer_buffer;
1621 if (urb->transfer_buffer_length > 0) {
1622 if (setup->bRequestType == Ep_InRequest) {
1623 ep2 = find_endpoint(dum, w_index);
1624 if (!ep2) {
1625 ret_val = -EOPNOTSUPP;
1626 break;
1627 }
1628 buf[0] = ep2->halted;
1629 } else if (setup->bRequestType ==
1630 Dev_InRequest) {
1631 buf[0] = (u8)dum->devstatus;
1632 } else
1633 buf[0] = 0;
1634 }
1635 if (urb->transfer_buffer_length > 1)
1636 buf[1] = 0;
1637 urb->actual_length = min_t(u32, 2,
1638 urb->transfer_buffer_length);
1639 ret_val = 0;
1640 *status = 0;
1641 }
1642 break;
1643 }
1644 return ret_val;
1645}
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647/* drive both sides of the transfers; looks like irq handlers to
1648 * both drivers except the callbacks aren't in_irq().
1649 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001650static void dummy_timer(unsigned long _dum_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001652 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1653 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 struct urbp *urbp, *tmp;
1655 unsigned long flags;
1656 int limit, total;
1657 int i;
1658
1659 /* simplistic model for one frame's bandwidth */
1660 switch (dum->gadget.speed) {
1661 case USB_SPEED_LOW:
1662 total = 8/*bytes*/ * 12/*packets*/;
1663 break;
1664 case USB_SPEED_FULL:
1665 total = 64/*bytes*/ * 19/*packets*/;
1666 break;
1667 case USB_SPEED_HIGH:
1668 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1669 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001670 case USB_SPEED_SUPER:
1671 /* Bus speed is 500000 bytes/ms, so use a little less */
1672 total = 490000;
1673 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001675 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return;
1677 }
1678
1679 /* FIXME if HZ != 1000 this will probably misbehave ... */
1680
1681 /* look at each urb queued by the host side driver */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001682 spin_lock_irqsave(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001684 if (!dum_hcd->udev) {
1685 dev_err(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 "timer fired with no URBs pending?\n");
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001687 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 return;
1689 }
1690
1691 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001692 if (!ep_name[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001694 dum->ep[i].already_seen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696
1697restart:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001698 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 struct urb *urb;
1700 struct dummy_request *req;
1701 u8 address;
1702 struct dummy_ep *ep = NULL;
1703 int type;
Alan Stern4d2f1102007-08-24 15:40:10 -04001704 int status = -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 urb = urbp->urb;
Alan Sterneb231052007-08-21 15:40:36 -04001707 if (urb->unlinked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 goto return_urb;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001709 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
Alan Stern391eca92005-05-10 15:34:16 -04001710 continue;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001711 type = usb_pipetype(urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 /* used up this frame's non-periodic bandwidth?
1714 * FIXME there's infinite bandwidth for control and
1715 * periodic transfers ... unrealistic.
1716 */
1717 if (total <= 0 && type == PIPE_BULK)
1718 continue;
1719
1720 /* find the gadget's ep for this request (if configured) */
1721 address = usb_pipeendpoint (urb->pipe);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001722 if (usb_pipein(urb->pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 address |= USB_DIR_IN;
1724 ep = find_endpoint(dum, address);
1725 if (!ep) {
1726 /* set_configuration() disagreement */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001727 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 "no ep configured for urb %p\n",
1729 urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001730 status = -EPROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 goto return_urb;
1732 }
1733
1734 if (ep->already_seen)
1735 continue;
1736 ep->already_seen = 1;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001737 if (ep == &dum->ep[0] && urb->error_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 ep->setup_stage = 1; /* a new urb */
1739 urb->error_count = 0;
1740 }
1741 if (ep->halted && !ep->setup_stage) {
1742 /* NOTE: must not be iso! */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001743 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 ep->ep.name, urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001745 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 goto return_urb;
1747 }
1748 /* FIXME make sure both ends agree on maxpacket */
1749
1750 /* handle control requests */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001751 if (ep == &dum->ep[0] && ep->setup_stage) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 struct usb_ctrlrequest setup;
1753 int value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001755 setup = *(struct usb_ctrlrequest *) urb->setup_packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 /* paranoia, in case of stale queued data */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001757 list_for_each_entry(req, &ep->queue, queue) {
1758 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 req->req.status = -EOVERFLOW;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001760 dev_dbg(udc_dev(dum), "stale req = %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 req);
1762
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001763 spin_unlock(&dum->lock);
1764 req->req.complete(&ep->ep, &req->req);
1765 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 ep->already_seen = 0;
1767 goto restart;
1768 }
1769
1770 /* gadget driver never sees set_address or operations
1771 * on standard feature flags. some hardware doesn't
1772 * even expose them.
1773 */
1774 ep->last_io = jiffies;
1775 ep->setup_stage = 0;
1776 ep->halted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001778 value = handle_control_request(dum_hcd, urb, &setup,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001779 &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 /* gadget driver handles all other requests. block
1782 * until setup() returns; no reentrancy issues etc.
1783 */
1784 if (value > 0) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001785 spin_unlock(&dum->lock);
1786 value = dum->driver->setup(&dum->gadget,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 &setup);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001788 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 if (value >= 0) {
1791 /* no delays (max 64KB data stage) */
1792 limit = 64*1024;
1793 goto treat_control_like_bulk;
1794 }
1795 /* error, see below */
1796 }
1797
1798 if (value < 0) {
1799 if (value != -EOPNOTSUPP)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001800 dev_dbg(udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 "setup --> %d\n",
1802 value);
Alan Stern4d2f1102007-08-24 15:40:10 -04001803 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 urb->actual_length = 0;
1805 }
1806
1807 goto return_urb;
1808 }
1809
1810 /* non-control requests */
1811 limit = total;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001812 switch (usb_pipetype(urb->pipe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 case PIPE_ISOCHRONOUS:
1814 /* FIXME is it urb->interval since the last xfer?
1815 * use urb->iso_frame_desc[i].
1816 * complete whether or not ep has requests queued.
1817 * report random errors, to debug drivers.
1818 */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001819 limit = max(limit, periodic_bytes(dum, ep));
Alan Stern4d2f1102007-08-24 15:40:10 -04001820 status = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 break;
1822
1823 case PIPE_INTERRUPT:
1824 /* FIXME is it urb->interval since the last xfer?
1825 * this almost certainly polls too fast.
1826 */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001827 limit = max(limit, periodic_bytes(dum, ep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /* FALLTHROUGH */
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 default:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001831treat_control_like_bulk:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 ep->last_io = jiffies;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001833 total = transfer(dum_hcd, urb, ep, limit, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 break;
1835 }
1836
1837 /* incomplete transfer? */
Alan Stern4d2f1102007-08-24 15:40:10 -04001838 if (status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 continue;
1840
1841return_urb:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001842 list_del(&urbp->urbp_list);
1843 kfree(urbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (ep)
1845 ep->already_seen = ep->setup_stage = 0;
1846
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001847 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001848 spin_unlock(&dum->lock);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001849 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001850 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 goto restart;
1853 }
1854
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001855 if (list_empty(&dum_hcd->urbp_list)) {
1856 usb_put_dev(dum_hcd->udev);
1857 dum_hcd->udev = NULL;
1858 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern391eca92005-05-10 15:34:16 -04001859 /* want a 1 msec delay here */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001860 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
1862
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001863 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
1865
1866/*-------------------------------------------------------------------------*/
1867
1868#define PORT_C_MASK \
Alan Sternc2db8b52005-04-29 16:30:48 -04001869 ((USB_PORT_STAT_C_CONNECTION \
1870 | USB_PORT_STAT_C_ENABLE \
1871 | USB_PORT_STAT_C_SUSPEND \
1872 | USB_PORT_STAT_C_OVERCURRENT \
1873 | USB_PORT_STAT_C_RESET) << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001875static int dummy_hub_status(struct usb_hcd *hcd, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001877 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 unsigned long flags;
Alan Stern391eca92005-05-10 15:34:16 -04001879 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001881 dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001883 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001884 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001885 goto done;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001886
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001887 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1888 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1889 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1890 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001891 }
1892
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001893 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 *buf = (1 << 1);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001895 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1896 dum_hcd->port_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 retval = 1;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001898 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001899 usb_hcd_resume_root_hub(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 }
Alan Stern391eca92005-05-10 15:34:16 -04001901done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001902 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return retval;
1904}
1905
Sebastian Andrzej Siewior3b9c1c52012-08-19 21:54:59 +02001906/* usb 3.0 root hub device descriptor */
Felipe Balbi814cf212013-03-22 16:50:47 +02001907static struct {
Sebastian Andrzej Siewior3b9c1c52012-08-19 21:54:59 +02001908 struct usb_bos_descriptor bos;
1909 struct usb_ss_cap_descriptor ss_cap;
1910} __packed usb3_bos_desc = {
1911
1912 .bos = {
1913 .bLength = USB_DT_BOS_SIZE,
1914 .bDescriptorType = USB_DT_BOS,
1915 .wTotalLength = cpu_to_le16(sizeof(usb3_bos_desc)),
1916 .bNumDeviceCaps = 1,
1917 },
1918 .ss_cap = {
1919 .bLength = USB_DT_USB_SS_CAP_SIZE,
1920 .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
1921 .bDevCapabilityType = USB_SS_CAP_TYPE,
1922 .wSpeedSupported = cpu_to_le16(USB_5GBPS_OPERATION),
1923 .bFunctionalitySupport = ilog2(USB_5GBPS_OPERATION),
1924 },
1925};
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927static inline void
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001928ss_hub_descriptor(struct usb_hub_descriptor *desc)
1929{
1930 memset(desc, 0, sizeof *desc);
1931 desc->bDescriptorType = 0x2a;
1932 desc->bDescLength = 12;
1933 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1934 desc->bNbrPorts = 1;
1935 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1936 desc->u.ss.DeviceRemovable = 0xffff;
1937}
1938
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001939static inline void hub_descriptor(struct usb_hub_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001941 memset(desc, 0, sizeof *desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 desc->bDescriptorType = 0x29;
1943 desc->bDescLength = 9;
Al Virofd05e722008-04-28 07:00:16 +01001944 desc->wHubCharacteristics = cpu_to_le16(0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 desc->bNbrPorts = 1;
John Youndbe79bb2001-09-17 00:00:00 -07001946 desc->u.hs.DeviceRemovable[0] = 0xff;
1947 desc->u.hs.DeviceRemovable[1] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001950static int dummy_hub_control(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 struct usb_hcd *hcd,
1952 u16 typeReq,
1953 u16 wValue,
1954 u16 wIndex,
1955 char *buf,
1956 u16 wLength
1957) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001958 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 int retval = 0;
1960 unsigned long flags;
1961
Alan Stern541c7d42010-06-22 16:39:10 -04001962 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001963 return -ETIMEDOUT;
1964
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001965 dum_hcd = hcd_to_dummy_hcd(hcd);
1966
1967 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 switch (typeReq) {
1969 case ClearHubFeature:
1970 break;
1971 case ClearPortFeature:
1972 switch (wValue) {
1973 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001974 if (hcd->speed == HCD_USB3) {
1975 dev_dbg(dummy_dev(dum_hcd),
1976 "USB_PORT_FEAT_SUSPEND req not "
1977 "supported for USB 3.0 roothub\n");
1978 goto error;
1979 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001980 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 /* 20msec resume signaling */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001982 dum_hcd->resuming = 1;
1983 dum_hcd->re_timeout = jiffies +
Alan Sternf1c39fa2005-05-03 16:24:04 -04001984 msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 }
1986 break;
1987 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001988 if (hcd->speed == HCD_USB3) {
1989 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1990 dev_dbg(dummy_dev(dum_hcd),
1991 "power-off\n");
1992 } else
1993 if (dum_hcd->port_status &
1994 USB_SS_PORT_STAT_POWER)
1995 dev_dbg(dummy_dev(dum_hcd),
1996 "power-off\n");
Alan Sternf1c39fa2005-05-03 16:24:04 -04001997 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001999 dum_hcd->port_status &= ~(1 << wValue);
2000 set_link_state(dum_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 }
2002 break;
2003 case GetHubDescriptor:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002004 if (hcd->speed == HCD_USB3 &&
2005 (wLength < USB_DT_SS_HUB_SIZE ||
2006 wValue != (USB_DT_SS_HUB << 8))) {
2007 dev_dbg(dummy_dev(dum_hcd),
2008 "Wrong hub descriptor type for "
2009 "USB 3.0 roothub.\n");
2010 goto error;
2011 }
2012 if (hcd->speed == HCD_USB3)
2013 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
2014 else
2015 hub_descriptor((struct usb_hub_descriptor *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 break;
Sebastian Andrzej Siewior3b9c1c52012-08-19 21:54:59 +02002017
2018 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
2019 if (hcd->speed != HCD_USB3)
2020 goto error;
2021
2022 if ((wValue >> 8) != USB_DT_BOS)
2023 goto error;
2024
2025 memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc));
2026 retval = sizeof(usb3_bos_desc);
2027 break;
2028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 case GetHubStatus:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002030 *(__le32 *) buf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 break;
2032 case GetPortStatus:
2033 if (wIndex != 1)
2034 retval = -EPIPE;
2035
2036 /* whoever resets or resumes must GetPortStatus to
2037 * complete it!!
2038 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002039 if (dum_hcd->resuming &&
2040 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2041 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
2042 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002044 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
2045 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2046 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
2047 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
2048 if (dum_hcd->dum->pullup) {
2049 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002050
2051 if (hcd->speed < HCD_USB3) {
2052 switch (dum_hcd->dum->gadget.speed) {
2053 case USB_SPEED_HIGH:
2054 dum_hcd->port_status |=
2055 USB_PORT_STAT_HIGH_SPEED;
2056 break;
2057 case USB_SPEED_LOW:
2058 dum_hcd->dum->gadget.ep0->
2059 maxpacket = 8;
2060 dum_hcd->port_status |=
2061 USB_PORT_STAT_LOW_SPEED;
2062 break;
2063 default:
2064 dum_hcd->dum->gadget.speed =
2065 USB_SPEED_FULL;
2066 break;
2067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
2069 }
2070 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002071 set_link_state(dum_hcd);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002072 ((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status);
2073 ((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 break;
2075 case SetHubFeature:
2076 retval = -EPIPE;
2077 break;
2078 case SetPortFeature:
2079 switch (wValue) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002080 case USB_PORT_FEAT_LINK_STATE:
2081 if (hcd->speed != HCD_USB3) {
2082 dev_dbg(dummy_dev(dum_hcd),
2083 "USB_PORT_FEAT_LINK_STATE req not "
2084 "supported for USB 2.0 roothub\n");
2085 goto error;
2086 }
2087 /*
2088 * Since this is dummy we don't have an actual link so
2089 * there is nothing to do for the SET_LINK_STATE cmd
2090 */
2091 break;
2092 case USB_PORT_FEAT_U1_TIMEOUT:
2093 case USB_PORT_FEAT_U2_TIMEOUT:
2094 /* TODO: add suspend/resume support! */
2095 if (hcd->speed != HCD_USB3) {
2096 dev_dbg(dummy_dev(dum_hcd),
2097 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
2098 "supported for USB 2.0 roothub\n");
2099 goto error;
2100 }
2101 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002103 /* Applicable only for USB2.0 hub */
2104 if (hcd->speed == HCD_USB3) {
2105 dev_dbg(dummy_dev(dum_hcd),
2106 "USB_PORT_FEAT_SUSPEND req not "
2107 "supported for USB 3.0 roothub\n");
2108 goto error;
2109 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002110 if (dum_hcd->active) {
2111 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
Alan Sternf1c39fa2005-05-03 16:24:04 -04002112
2113 /* HNP would happen here; for now we
2114 * assume b_bus_req is always true.
2115 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002116 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002117 if (((1 << USB_DEVICE_B_HNP_ENABLE)
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002118 & dum_hcd->dum->devstatus) != 0)
2119 dev_dbg(dummy_dev(dum_hcd),
Alan Stern5742b0c2005-05-02 11:25:17 -04002120 "no HNP yet!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
2122 break;
Alan Sternf1c39fa2005-05-03 16:24:04 -04002123 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002124 if (hcd->speed == HCD_USB3)
2125 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
2126 else
2127 dum_hcd->port_status |= USB_PORT_STAT_POWER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002128 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002129 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002130 case USB_PORT_FEAT_BH_PORT_RESET:
2131 /* Applicable only for USB3.0 hub */
2132 if (hcd->speed != HCD_USB3) {
2133 dev_dbg(dummy_dev(dum_hcd),
2134 "USB_PORT_FEAT_BH_PORT_RESET req not "
2135 "supported for USB 2.0 roothub\n");
2136 goto error;
2137 }
2138 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 case USB_PORT_FEAT_RESET:
Alan Sternf1c39fa2005-05-03 16:24:04 -04002140 /* if it's already enabled, disable */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002141 if (hcd->speed == HCD_USB3) {
2142 dum_hcd->port_status = 0;
2143 dum_hcd->port_status =
2144 (USB_SS_PORT_STAT_POWER |
2145 USB_PORT_STAT_CONNECTION |
2146 USB_PORT_STAT_RESET);
2147 } else
2148 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
Alan Sternf1c39fa2005-05-03 16:24:04 -04002149 | USB_PORT_STAT_LOW_SPEED
2150 | USB_PORT_STAT_HIGH_SPEED);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002151 /*
2152 * We want to reset device status. All but the
2153 * Self powered feature
2154 */
2155 dum_hcd->dum->devstatus &=
2156 (1 << USB_DEVICE_SELF_POWERED);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002157 /*
2158 * FIXME USB3.0: what is the correct reset signaling
2159 * interval? Is it still 50msec as for HS?
2160 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002161 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002162 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 default:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002164 if (hcd->speed == HCD_USB3) {
2165 if ((dum_hcd->port_status &
2166 USB_SS_PORT_STAT_POWER) != 0) {
2167 dum_hcd->port_status |= (1 << wValue);
2168 set_link_state(dum_hcd);
2169 }
2170 } else
2171 if ((dum_hcd->port_status &
2172 USB_PORT_STAT_POWER) != 0) {
2173 dum_hcd->port_status |= (1 << wValue);
2174 set_link_state(dum_hcd);
2175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
2177 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002178 case GetPortErrorCount:
2179 if (hcd->speed != HCD_USB3) {
2180 dev_dbg(dummy_dev(dum_hcd),
2181 "GetPortErrorCount req not "
2182 "supported for USB 2.0 roothub\n");
2183 goto error;
2184 }
2185 /* We'll always return 0 since this is a dummy hub */
2186 *(__le32 *) buf = cpu_to_le32(0);
2187 break;
2188 case SetHubDepth:
2189 if (hcd->speed != HCD_USB3) {
2190 dev_dbg(dummy_dev(dum_hcd),
2191 "SetHubDepth req not supported for "
2192 "USB 2.0 roothub\n");
2193 goto error;
2194 }
2195 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002197 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 "hub control req%04x v%04x i%04x l%d\n",
2199 typeReq, wValue, wIndex, wLength);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002200error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 /* "protocol stall" on error */
2202 retval = -EPIPE;
2203 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002204 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Stern685eb932005-05-03 16:27:26 -04002205
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002206 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002207 usb_hcd_poll_rh_status(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 return retval;
2209}
2210
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002211static int dummy_bus_suspend(struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002212{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002213 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002214
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002215 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern3cf0a222005-11-29 12:08:15 -05002216
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002217 spin_lock_irq(&dum_hcd->dum->lock);
2218 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2219 set_link_state(dum_hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002220 hcd->state = HC_STATE_SUSPENDED;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002221 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern391eca92005-05-10 15:34:16 -04002222 return 0;
2223}
2224
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002225static int dummy_bus_resume(struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002226{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002227 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002228 int rc = 0;
2229
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002230 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002231
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002232 spin_lock_irq(&dum_hcd->dum->lock);
Alan Stern541c7d42010-06-22 16:39:10 -04002233 if (!HCD_HW_ACCESSIBLE(hcd)) {
Alan Sterncfa59da2007-06-21 16:25:35 -04002234 rc = -ESHUTDOWN;
Alan Stern3cf0a222005-11-29 12:08:15 -05002235 } else {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002236 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2237 set_link_state(dum_hcd);
2238 if (!list_empty(&dum_hcd->urbp_list))
2239 mod_timer(&dum_hcd->timer, jiffies);
Alan Stern3cf0a222005-11-29 12:08:15 -05002240 hcd->state = HC_STATE_RUNNING;
2241 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002242 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern3cf0a222005-11-29 12:08:15 -05002243 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002244}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246/*-------------------------------------------------------------------------*/
2247
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002248static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002250 int ep = usb_pipeendpoint(urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002252 return snprintf(buf, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 "urb/%p %s ep%d%s%s len %d/%d\n",
2254 urb,
2255 ({ char *s;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002256 switch (urb->dev->speed) {
2257 case USB_SPEED_LOW:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002258 s = "ls";
2259 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002260 case USB_SPEED_FULL:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002261 s = "fs";
2262 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002263 case USB_SPEED_HIGH:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002264 s = "hs";
2265 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002266 case USB_SPEED_SUPER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002267 s = "ss";
2268 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002269 default:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002270 s = "?";
2271 break;
Joe Perches2b84f922013-10-08 16:01:37 -07002272 } s; }),
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002273 ep, ep ? (usb_pipein(urb->pipe) ? "in" : "out") : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 ({ char *s; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002275 switch (usb_pipetype(urb->pipe)) { \
2276 case PIPE_CONTROL: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002277 s = ""; \
2278 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002279 case PIPE_BULK: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002280 s = "-bulk"; \
2281 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002282 case PIPE_INTERRUPT: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002283 s = "-int"; \
2284 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002285 default: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002286 s = "-iso"; \
2287 break; \
Joe Perches2b84f922013-10-08 16:01:37 -07002288 } s; }),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 urb->actual_length, urb->transfer_buffer_length);
2290}
2291
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07002292static ssize_t urbs_show(struct device *dev, struct device_attribute *attr,
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002293 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002295 struct usb_hcd *hcd = dev_get_drvdata(dev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002296 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 struct urbp *urbp;
2298 size_t size = 0;
2299 unsigned long flags;
2300
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002301 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2302 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 size_t temp;
2304
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002305 temp = show_urb(buf, PAGE_SIZE - size, urbp->urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 buf += temp;
2307 size += temp;
2308 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002309 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311 return size;
2312}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07002313static DEVICE_ATTR_RO(urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002315static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2316{
2317 init_timer(&dum_hcd->timer);
2318 dum_hcd->timer.function = dummy_timer;
2319 dum_hcd->timer.data = (unsigned long)dum_hcd;
2320 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002321 dum_hcd->stream_en_ep = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002322 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2323 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2324 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2325 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2326#ifdef CONFIG_USB_OTG
2327 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2328#endif
2329 return 0;
2330
2331 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2332 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2333}
2334
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002335static int dummy_start(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002337 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339 /*
2340 * MASTER side init ... we emulate a root hub that'll only ever
2341 * talk to one device (the slave side). Also appears in sysfs,
2342 * just like more familiar pci-based HCDs.
2343 */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002344 if (!usb_hcd_is_primary_hcd(hcd))
2345 return dummy_start_ss(dum_hcd);
2346
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002347 spin_lock_init(&dum_hcd->dum->lock);
2348 init_timer(&dum_hcd->timer);
2349 dum_hcd->timer.function = dummy_timer;
2350 dum_hcd->timer.data = (unsigned long)dum_hcd;
2351 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002353 INIT_LIST_HEAD(&dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Alan Sterncaf29f62007-12-06 11:10:39 -05002355 hcd->power_budget = POWER_BUDGET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 hcd->state = HC_STATE_RUNNING;
Alan Stern685eb932005-05-03 16:27:26 -04002357 hcd->uses_new_polling = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Alan Stern5742b0c2005-05-02 11:25:17 -04002359#ifdef CONFIG_USB_OTG
2360 hcd->self.otg_port = 1;
2361#endif
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002364 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365}
2366
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002367static void dummy_stop(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
2369 struct dummy *dum;
2370
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002371 dum = hcd_to_dummy_hcd(hcd)->dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002372 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2373 usb_gadget_unregister_driver(dum->driver);
2374 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}
2376
2377/*-------------------------------------------------------------------------*/
2378
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002379static int dummy_h_get_frame(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002381 return dummy_g_get_frame(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002384static int dummy_setup(struct usb_hcd *hcd)
2385{
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002386 struct dummy *dum;
2387
2388 dum = *((void **)dev_get_platdata(hcd->self.controller));
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01002389 hcd->self.sg_tablesize = ~0;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002390 if (usb_hcd_is_primary_hcd(hcd)) {
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002391 dum->hs_hcd = hcd_to_dummy_hcd(hcd);
2392 dum->hs_hcd->dum = dum;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002393 /*
2394 * Mark the first roothub as being USB 2.0.
2395 * The USB 3.0 roothub will be registered later by
2396 * dummy_hcd_probe()
2397 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002398 hcd->speed = HCD_USB2;
2399 hcd->self.root_hub->speed = USB_SPEED_HIGH;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002400 } else {
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002401 dum->ss_hcd = hcd_to_dummy_hcd(hcd);
2402 dum->ss_hcd->dum = dum;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002403 hcd->speed = HCD_USB3;
2404 hcd->self.root_hub->speed = USB_SPEED_SUPER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002405 }
2406 return 0;
2407}
2408
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002409/* Change a group of bulk endpoints to support multiple stream IDs */
Sebastian Andrzej Siewiord81f3e42012-01-09 13:15:00 +01002410static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002411 struct usb_host_endpoint **eps, unsigned int num_eps,
2412 unsigned int num_streams, gfp_t mem_flags)
2413{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002414 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2415 unsigned long flags;
2416 int max_stream;
2417 int ret_streams = num_streams;
2418 unsigned int index;
2419 unsigned int i;
2420
2421 if (!num_eps)
2422 return -EINVAL;
2423
2424 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2425 for (i = 0; i < num_eps; i++) {
2426 index = dummy_get_ep_idx(&eps[i]->desc);
2427 if ((1 << index) & dum_hcd->stream_en_ep) {
2428 ret_streams = -EINVAL;
2429 goto out;
2430 }
2431 max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp);
2432 if (!max_stream) {
2433 ret_streams = -EINVAL;
2434 goto out;
2435 }
2436 if (max_stream < ret_streams) {
2437 dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u "
2438 "stream IDs.\n",
2439 eps[i]->desc.bEndpointAddress,
2440 max_stream);
2441 ret_streams = max_stream;
2442 }
2443 }
2444
2445 for (i = 0; i < num_eps; i++) {
2446 index = dummy_get_ep_idx(&eps[i]->desc);
2447 dum_hcd->stream_en_ep |= 1 << index;
2448 set_max_streams_for_pipe(dum_hcd,
2449 usb_endpoint_num(&eps[i]->desc), ret_streams);
2450 }
2451out:
2452 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2453 return ret_streams;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002454}
2455
2456/* Reverts a group of bulk endpoints back to not using stream IDs. */
Sebastian Andrzej Siewiord81f3e42012-01-09 13:15:00 +01002457static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002458 struct usb_host_endpoint **eps, unsigned int num_eps,
2459 gfp_t mem_flags)
2460{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002461 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2462 unsigned long flags;
2463 int ret;
2464 unsigned int index;
2465 unsigned int i;
2466
2467 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2468 for (i = 0; i < num_eps; i++) {
2469 index = dummy_get_ep_idx(&eps[i]->desc);
2470 if (!((1 << index) & dum_hcd->stream_en_ep)) {
2471 ret = -EINVAL;
2472 goto out;
2473 }
2474 }
2475
2476 for (i = 0; i < num_eps; i++) {
2477 index = dummy_get_ep_idx(&eps[i]->desc);
2478 dum_hcd->stream_en_ep &= ~(1 << index);
2479 set_max_streams_for_pipe(dum_hcd,
2480 usb_endpoint_num(&eps[i]->desc), 0);
2481 }
2482 ret = 0;
2483out:
2484 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2485 return ret;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002486}
2487
2488static struct hc_driver dummy_hcd = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 .description = (char *) driver_name,
2490 .product_desc = "Dummy host controller",
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002491 .hcd_priv_size = sizeof(struct dummy_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002493 .flags = HCD_USB3 | HCD_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002495 .reset = dummy_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 .start = dummy_start,
2497 .stop = dummy_stop,
2498
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002499 .urb_enqueue = dummy_urb_enqueue,
2500 .urb_dequeue = dummy_urb_dequeue,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002502 .get_frame_number = dummy_h_get_frame,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002504 .hub_status_data = dummy_hub_status,
2505 .hub_control = dummy_hub_control,
Alan Stern0c0382e2005-10-13 17:08:02 -04002506 .bus_suspend = dummy_bus_suspend,
2507 .bus_resume = dummy_bus_resume,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002508
2509 .alloc_streams = dummy_alloc_streams,
2510 .free_streams = dummy_free_streams,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511};
2512
Alan Stern8364d6b2005-11-14 12:16:30 -05002513static int dummy_hcd_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002515 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002516 struct usb_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002517 struct usb_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 int retval;
2519
Alan Stern8364d6b2005-11-14 12:16:30 -05002520 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002521 dum = *((void **)dev_get_platdata(&pdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002523 if (!mod_data.is_super_speed)
2524 dummy_hcd.flags = HCD_USB2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002525 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2526 if (!hs_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 return -ENOMEM;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002528 hs_hcd->has_tt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002530 retval = usb_add_hcd(hs_hcd, 0, 0);
Sebastian Andrzej Siewior1b68a4c2012-08-19 21:54:58 +02002531 if (retval)
2532 goto put_usb2_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002533
2534 if (mod_data.is_super_speed) {
2535 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2536 dev_name(&pdev->dev), hs_hcd);
2537 if (!ss_hcd) {
2538 retval = -ENOMEM;
2539 goto dealloc_usb2_hcd;
2540 }
2541
2542 retval = usb_add_hcd(ss_hcd, 0, 0);
2543 if (retval)
2544 goto put_usb3_hcd;
2545 }
2546 return 0;
2547
2548put_usb3_hcd:
2549 usb_put_hcd(ss_hcd);
2550dealloc_usb2_hcd:
Sebastian Andrzej Siewior1b68a4c2012-08-19 21:54:58 +02002551 usb_remove_hcd(hs_hcd);
2552put_usb2_hcd:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002553 usb_put_hcd(hs_hcd);
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002554 dum->hs_hcd = dum->ss_hcd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 return retval;
2556}
2557
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002558static int dummy_hcd_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002560 struct dummy *dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002562 dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002563
2564 if (dum->ss_hcd) {
2565 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2566 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2567 }
2568
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002569 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2570 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002571
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002572 dum->hs_hcd = NULL;
2573 dum->ss_hcd = NULL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002574
Alan Sternd9b76252005-05-03 16:15:43 -04002575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002578static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state)
Alan Stern391eca92005-05-10 15:34:16 -04002579{
2580 struct usb_hcd *hcd;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002581 struct dummy_hcd *dum_hcd;
Alan Stern3cf0a222005-11-29 12:08:15 -05002582 int rc = 0;
Alan Stern391eca92005-05-10 15:34:16 -04002583
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002584 dev_dbg(&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002585
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002586 hcd = platform_get_drvdata(pdev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002587 dum_hcd = hcd_to_dummy_hcd(hcd);
2588 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern3cf0a222005-11-29 12:08:15 -05002589 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2590 rc = -EBUSY;
2591 } else
2592 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2593 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002594}
2595
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002596static int dummy_hcd_resume(struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04002597{
2598 struct usb_hcd *hcd;
2599
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002600 dev_dbg(&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002601
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002602 hcd = platform_get_drvdata(pdev);
Alan Stern3cf0a222005-11-29 12:08:15 -05002603 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002604 usb_hcd_poll_rh_status(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002605 return 0;
2606}
2607
Russell King3ae5eae2005-11-09 22:32:44 +00002608static struct platform_driver dummy_hcd_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04002609 .probe = dummy_hcd_probe,
2610 .remove = dummy_hcd_remove,
Alan Stern391eca92005-05-10 15:34:16 -04002611 .suspend = dummy_hcd_suspend,
2612 .resume = dummy_hcd_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002613 .driver = {
2614 .name = (char *) driver_name,
2615 .owner = THIS_MODULE,
2616 },
Alan Sternd9b76252005-05-03 16:15:43 -04002617};
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002620#define MAX_NUM_UDC 2
Sebastian Andrzej Siewiorb2113132012-10-30 12:53:17 +01002621static struct platform_device *the_udc_pdev[MAX_NUM_UDC];
2622static struct platform_device *the_hcd_pdev[MAX_NUM_UDC];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002624static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002626 int retval = -ENOMEM;
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002627 int i;
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002628 struct dummy *dum[MAX_NUM_UDC];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002630 if (usb_disabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 return -ENODEV;
Alan Sternd9b76252005-05-03 16:15:43 -04002632
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +03002633 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2634 return -EINVAL;
2635
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002636 if (mod_data.num < 1 || mod_data.num > MAX_NUM_UDC) {
2637 pr_err("Number of emulated UDC must be in range of 1…%d\n",
2638 MAX_NUM_UDC);
2639 return -EINVAL;
2640 }
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002641
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002642 for (i = 0; i < mod_data.num; i++) {
2643 the_hcd_pdev[i] = platform_device_alloc(driver_name, i);
2644 if (!the_hcd_pdev[i]) {
2645 i--;
2646 while (i >= 0)
2647 platform_device_put(the_hcd_pdev[i--]);
2648 return retval;
2649 }
2650 }
2651 for (i = 0; i < mod_data.num; i++) {
2652 the_udc_pdev[i] = platform_device_alloc(gadget_name, i);
2653 if (!the_udc_pdev[i]) {
2654 i--;
2655 while (i >= 0)
2656 platform_device_put(the_udc_pdev[i--]);
2657 goto err_alloc_udc;
2658 }
2659 }
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002660 for (i = 0; i < mod_data.num; i++) {
2661 dum[i] = kzalloc(sizeof(struct dummy), GFP_KERNEL);
Wei Yongjun481d3042013-05-07 19:50:06 +08002662 if (!dum[i]) {
2663 retval = -ENOMEM;
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002664 goto err_add_pdata;
Wei Yongjun481d3042013-05-07 19:50:06 +08002665 }
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002666 retval = platform_device_add_data(the_hcd_pdev[i], &dum[i],
2667 sizeof(void *));
2668 if (retval)
2669 goto err_add_pdata;
2670 retval = platform_device_add_data(the_udc_pdev[i], &dum[i],
2671 sizeof(void *));
2672 if (retval)
2673 goto err_add_pdata;
2674 }
Alan Sternd9b76252005-05-03 16:15:43 -04002675
Alan Sterna89a2cd2008-04-07 15:03:25 -04002676 retval = platform_driver_register(&dummy_hcd_driver);
2677 if (retval < 0)
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002678 goto err_add_pdata;
Alan Sterna89a2cd2008-04-07 15:03:25 -04002679 retval = platform_driver_register(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002680 if (retval < 0)
2681 goto err_register_udc_driver;
2682
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002683 for (i = 0; i < mod_data.num; i++) {
2684 retval = platform_device_add(the_hcd_pdev[i]);
2685 if (retval < 0) {
2686 i--;
2687 while (i >= 0)
2688 platform_device_del(the_hcd_pdev[i--]);
2689 goto err_add_hcd;
2690 }
2691 }
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002692 for (i = 0; i < mod_data.num; i++) {
2693 if (!dum[i]->hs_hcd ||
2694 (!dum[i]->ss_hcd && mod_data.is_super_speed)) {
2695 /*
2696 * The hcd was added successfully but its probe
2697 * function failed for some reason.
2698 */
2699 retval = -EINVAL;
2700 goto err_add_udc;
2701 }
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002702 }
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002703
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002704 for (i = 0; i < mod_data.num; i++) {
2705 retval = platform_device_add(the_udc_pdev[i]);
2706 if (retval < 0) {
2707 i--;
2708 while (i >= 0)
2709 platform_device_del(the_udc_pdev[i]);
2710 goto err_add_udc;
2711 }
2712 }
2713
2714 for (i = 0; i < mod_data.num; i++) {
2715 if (!platform_get_drvdata(the_udc_pdev[i])) {
2716 /*
2717 * The udc was added successfully but its probe
2718 * function failed for some reason.
2719 */
2720 retval = -EINVAL;
2721 goto err_probe_udc;
2722 }
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002723 }
Alan Sternd9b76252005-05-03 16:15:43 -04002724 return retval;
2725
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002726err_probe_udc:
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002727 for (i = 0; i < mod_data.num; i++)
2728 platform_device_del(the_udc_pdev[i]);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002729err_add_udc:
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002730 for (i = 0; i < mod_data.num; i++)
2731 platform_device_del(the_hcd_pdev[i]);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002732err_add_hcd:
2733 platform_driver_unregister(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002734err_register_udc_driver:
Alan Sterna89a2cd2008-04-07 15:03:25 -04002735 platform_driver_unregister(&dummy_hcd_driver);
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002736err_add_pdata:
2737 for (i = 0; i < mod_data.num; i++)
2738 kfree(dum[i]);
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002739 for (i = 0; i < mod_data.num; i++)
2740 platform_device_put(the_udc_pdev[i]);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002741err_alloc_udc:
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002742 for (i = 0; i < mod_data.num; i++)
2743 platform_device_put(the_hcd_pdev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return retval;
2745}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002746module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002748static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002750 int i;
2751
2752 for (i = 0; i < mod_data.num; i++) {
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002753 struct dummy *dum;
2754
2755 dum = *((void **)dev_get_platdata(&the_udc_pdev[i]->dev));
2756
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002757 platform_device_unregister(the_udc_pdev[i]);
2758 platform_device_unregister(the_hcd_pdev[i]);
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002759 kfree(dum);
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002760 }
Alan Sterna89a2cd2008-04-07 15:03:25 -04002761 platform_driver_unregister(&dummy_udc_driver);
2762 platform_driver_unregister(&dummy_hcd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002764module_exit(cleanup);