blob: 95d584dbed13b08516f23d90d83acb973d989a3a [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
133 /* act like a net2280: high speed, six configurable endpoints */
134 "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
135
136 /* or like pxa250: fifteen fixed function endpoints */
137 "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
138 "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
139 "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
140 "ep15in-int",
141
142 /* or like sa1100: two fixed function endpoints */
143 "ep1out-bulk", "ep2in-bulk",
144};
Tobias Klauser52950ed2005-12-11 16:20:08 +0100145#define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Alan Sternd9b76252005-05-03 16:15:43 -0400147/*-------------------------------------------------------------------------*/
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#define FIFO_SIZE 64
150
151struct urbp {
152 struct urb *urb;
153 struct list_head urbp_list;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +0100154 struct sg_mapping_iter miter;
155 u32 miter_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
Alan Stern391eca92005-05-10 15:34:16 -0400158
159enum dummy_rh_state {
160 DUMMY_RH_RESET,
161 DUMMY_RH_SUSPENDED,
162 DUMMY_RH_RUNNING
163};
164
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300165struct dummy_hcd {
166 struct dummy *dum;
167 enum dummy_rh_state rh_state;
168 struct timer_list timer;
169 u32 port_status;
170 u32 old_status;
171 unsigned long re_timeout;
172
173 struct usb_device *udev;
174 struct list_head urbp_list;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100175 u32 stream_en_ep;
176 u8 num_stream[30 / 2];
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300177
178 unsigned active:1;
179 unsigned old_active:1;
180 unsigned resuming:1;
181};
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183struct dummy {
184 spinlock_t lock;
185
186 /*
187 * SLAVE/GADGET side support
188 */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100189 struct dummy_ep ep[DUMMY_ENDPOINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 int address;
191 struct usb_gadget gadget;
192 struct usb_gadget_driver *driver;
193 struct dummy_request fifo_req;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100194 u8 fifo_buf[FIFO_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 u16 devstatus;
Alan Stern391eca92005-05-10 15:34:16 -0400196 unsigned udc_suspended:1;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400197 unsigned pullup:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 /*
200 * MASTER/HOST side support
201 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300202 struct dummy_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300203 struct dummy_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300206static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300208 return (struct dummy_hcd *) (hcd->hcd_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300211static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 return container_of((void *) dum, struct usb_hcd, hcd_priv);
214}
215
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300216static inline struct device *dummy_dev(struct dummy_hcd *dum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300218 return dummy_hcd_to_hcd(dum)->self.controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100221static inline struct device *udc_dev(struct dummy *dum)
Alan Sternd9b76252005-05-03 16:15:43 -0400222{
223 return dum->gadget.dev.parent;
224}
225
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100226static inline struct dummy *ep_to_dummy(struct dummy_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100228 return container_of(ep->gadget, struct dummy, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300231static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300233 struct dummy *dum = container_of(gadget, struct dummy, gadget);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300234 if (dum->gadget.speed == USB_SPEED_SUPER)
235 return dum->ss_hcd;
236 else
237 return dum->hs_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100240static inline struct dummy *gadget_dev_to_dummy(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100242 return container_of(dev, struct dummy, gadget.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245/*-------------------------------------------------------------------------*/
246
Alan Sternf1c39fa2005-05-03 16:24:04 -0400247/* SLAVE/GADGET SIDE UTILITY ROUTINES */
248
249/* called with spinlock held */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100250static void nuke(struct dummy *dum, struct dummy_ep *ep)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400251{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100252 while (!list_empty(&ep->queue)) {
Alan Sternf1c39fa2005-05-03 16:24:04 -0400253 struct dummy_request *req;
254
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100255 req = list_entry(ep->queue.next, struct dummy_request, queue);
256 list_del_init(&req->queue);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400257 req->req.status = -ESHUTDOWN;
258
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100259 spin_unlock(&dum->lock);
260 req->req.complete(&ep->ep, &req->req);
261 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400262 }
263}
264
265/* caller must hold lock */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100266static void stop_activity(struct dummy *dum)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400267{
268 struct dummy_ep *ep;
269
270 /* prevent any more requests */
271 dum->address = 0;
272
273 /* The timer is left running so that outstanding URBs can fail */
274
275 /* nuke any pending requests first, so driver i/o is quiesced */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100276 list_for_each_entry(ep, &dum->gadget.ep_list, ep.ep_list)
277 nuke(dum, ep);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400278
279 /* driver now does any non-usb quiescing necessary */
280}
281
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300282/**
283 * set_link_state_by_speed() - Sets the current state of the link according to
284 * the hcd speed
285 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
286 *
287 * This function updates the port_status according to the link state and the
288 * speed of the hcd.
289 */
290static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
291{
292 struct dummy *dum = dum_hcd->dum;
293
294 if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
295 if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
296 dum_hcd->port_status = 0;
297 } else if (!dum->pullup || dum->udc_suspended) {
298 /* UDC suspend must cause a disconnect */
299 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
300 USB_PORT_STAT_ENABLE);
301 if ((dum_hcd->old_status &
302 USB_PORT_STAT_CONNECTION) != 0)
303 dum_hcd->port_status |=
304 (USB_PORT_STAT_C_CONNECTION << 16);
305 } else {
306 /* device is connected and not suspended */
307 dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
308 USB_PORT_STAT_SPEED_5GBPS) ;
309 if ((dum_hcd->old_status &
310 USB_PORT_STAT_CONNECTION) == 0)
311 dum_hcd->port_status |=
312 (USB_PORT_STAT_C_CONNECTION << 16);
313 if ((dum_hcd->port_status &
314 USB_PORT_STAT_ENABLE) == 1 &&
315 (dum_hcd->port_status &
316 USB_SS_PORT_LS_U0) == 1 &&
317 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
318 dum_hcd->active = 1;
319 }
320 } else {
321 if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
322 dum_hcd->port_status = 0;
323 } else if (!dum->pullup || dum->udc_suspended) {
324 /* UDC suspend must cause a disconnect */
325 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
326 USB_PORT_STAT_ENABLE |
327 USB_PORT_STAT_LOW_SPEED |
328 USB_PORT_STAT_HIGH_SPEED |
329 USB_PORT_STAT_SUSPEND);
330 if ((dum_hcd->old_status &
331 USB_PORT_STAT_CONNECTION) != 0)
332 dum_hcd->port_status |=
333 (USB_PORT_STAT_C_CONNECTION << 16);
334 } else {
335 dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
336 if ((dum_hcd->old_status &
337 USB_PORT_STAT_CONNECTION) == 0)
338 dum_hcd->port_status |=
339 (USB_PORT_STAT_C_CONNECTION << 16);
340 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
341 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
342 else if ((dum_hcd->port_status &
343 USB_PORT_STAT_SUSPEND) == 0 &&
344 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
345 dum_hcd->active = 1;
346 }
347 }
348}
349
Alan Sternf1c39fa2005-05-03 16:24:04 -0400350/* caller must hold lock */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300351static void set_link_state(struct dummy_hcd *dum_hcd)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400352{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300353 struct dummy *dum = dum_hcd->dum;
354
355 dum_hcd->active = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300356 if (dum->pullup)
357 if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
358 dum->gadget.speed != USB_SPEED_SUPER) ||
359 (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
360 dum->gadget.speed == USB_SPEED_SUPER))
361 return;
Alan Stern391eca92005-05-10 15:34:16 -0400362
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300363 set_link_state_by_speed(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400364
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300365 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
366 dum_hcd->active)
367 dum_hcd->resuming = 0;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400368
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300369 /* if !connected or reset */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300370 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
371 (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300372 /*
373 * We're connected and not reset (reset occurred now),
374 * and driver attached - disconnect!
375 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300376 if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300377 (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
378 dum->driver) {
379 stop_activity(dum);
380 spin_unlock(&dum->lock);
381 dum->driver->disconnect(&dum->gadget);
382 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400383 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300384 } else if (dum_hcd->active != dum_hcd->old_active) {
385 if (dum_hcd->old_active && dum->driver->suspend) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300386 spin_unlock(&dum->lock);
387 dum->driver->suspend(&dum->gadget);
388 spin_lock(&dum->lock);
389 } else if (!dum_hcd->old_active && dum->driver->resume) {
390 spin_unlock(&dum->lock);
391 dum->driver->resume(&dum->gadget);
392 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400393 }
394 }
395
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300396 dum_hcd->old_status = dum_hcd->port_status;
397 dum_hcd->old_active = dum_hcd->active;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400398}
399
400/*-------------------------------------------------------------------------*/
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402/* SLAVE/GADGET SIDE DRIVER
403 *
404 * This only tracks gadget state. All the work is done when the host
405 * side tries some (emulated) i/o operation. Real device controller
406 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
407 */
408
409#define is_enabled(dum) \
410 (dum->port_status & USB_PORT_STAT_ENABLE)
411
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100412static int dummy_enable(struct usb_ep *_ep,
413 const struct usb_endpoint_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300416 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 struct dummy_ep *ep;
418 unsigned max;
419 int retval;
420
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100421 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (!_ep || !desc || ep->desc || _ep->name == ep0name
423 || desc->bDescriptorType != USB_DT_ENDPOINT)
424 return -EINVAL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100425 dum = ep_to_dummy(ep);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300426 if (!dum->driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return -ESHUTDOWN;
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200428
429 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300430 if (!is_enabled(dum_hcd))
431 return -ESHUTDOWN;
432
433 /*
434 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
435 * maximum packet size.
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300436 * For SS devices the wMaxPacketSize is limited by 1024.
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300437 */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700438 max = usb_endpoint_maxp(desc) & 0x7ff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 /* drivers must not request bad settings, since lower levels
441 * (hardware or its drivers) may not check. some endpoints
442 * can't do iso, many have maxpacket limitations, etc.
443 *
444 * since this "hardware" driver is here to help debugging, we
445 * have some extra sanity checks. (there could be more though,
446 * especially for "ep9out" style fixed function ones.)
447 */
448 retval = -EINVAL;
Sebastian Andrzej Siewior59f08e62012-01-12 12:53:14 +0100449 switch (usb_endpoint_type(desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 case USB_ENDPOINT_XFER_BULK:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100451 if (strstr(ep->ep.name, "-iso")
452 || strstr(ep->ep.name, "-int")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 goto done;
454 }
455 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300456 case USB_SPEED_SUPER:
457 if (max == 1024)
458 break;
459 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 case USB_SPEED_HIGH:
461 if (max == 512)
462 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700463 goto done;
464 case USB_SPEED_FULL:
465 if (max == 8 || max == 16 || max == 32 || max == 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /* we'll fake any legal size */
467 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700468 /* save a return statement */
469 default:
470 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472 break;
473 case USB_ENDPOINT_XFER_INT:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100474 if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 goto done;
476 /* real hardware might not handle all packet sizes */
477 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300478 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 case USB_SPEED_HIGH:
480 if (max <= 1024)
481 break;
482 /* save a return statement */
483 case USB_SPEED_FULL:
484 if (max <= 64)
485 break;
486 /* save a return statement */
487 default:
488 if (max <= 8)
489 break;
490 goto done;
491 }
492 break;
493 case USB_ENDPOINT_XFER_ISOC:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100494 if (strstr(ep->ep.name, "-bulk")
495 || strstr(ep->ep.name, "-int"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 goto done;
497 /* real hardware might not handle all packet sizes */
498 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300499 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 case USB_SPEED_HIGH:
501 if (max <= 1024)
502 break;
503 /* save a return statement */
504 case USB_SPEED_FULL:
505 if (max <= 1023)
506 break;
507 /* save a return statement */
508 default:
509 goto done;
510 }
511 break;
512 default:
513 /* few chips support control except on ep0 */
514 goto done;
515 }
516
517 _ep->maxpacket = max;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100518 if (usb_ss_max_streams(_ep->comp_desc)) {
519 if (!usb_endpoint_xfer_bulk(desc)) {
520 dev_err(udc_dev(dum), "Can't enable stream support on "
521 "non-bulk ep %s\n", _ep->name);
522 return -EINVAL;
523 }
524 ep->stream_en = 1;
525 }
Sebastian Andrzej Siewior3cf0ad02012-01-25 11:51:19 +0100526 ep->desc = desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100528 dev_dbg(udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 _ep->name,
530 desc->bEndpointAddress & 0x0f,
531 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
532 ({ char *val;
Sebastian Andrzej Siewior59f08e62012-01-12 12:53:14 +0100533 switch (usb_endpoint_type(desc)) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +0300534 case USB_ENDPOINT_XFER_BULK:
535 val = "bulk";
536 break;
537 case USB_ENDPOINT_XFER_ISOC:
538 val = "iso";
539 break;
540 case USB_ENDPOINT_XFER_INT:
541 val = "intr";
542 break;
543 default:
544 val = "ctrl";
545 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }; val; }),
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100547 max, ep->stream_en ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 /* at this point real hardware should be NAKing transfers
550 * to that endpoint, until a buffer is queued to it.
551 */
Alan Stern851a5262008-08-14 15:48:30 -0400552 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 retval = 0;
554done:
555 return retval;
556}
557
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100558static int dummy_disable(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 struct dummy_ep *ep;
561 struct dummy *dum;
562 unsigned long flags;
563 int retval;
564
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 retval = 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100574 nuke(dum, ep);
575 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100577 dev_dbg(udc_dev(dum), "disabled %s\n", _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return retval;
579}
580
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100581static struct usb_request *dummy_alloc_request(struct usb_ep *_ep,
582 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 struct dummy_ep *ep;
585 struct dummy_request *req;
586
587 if (!_ep)
588 return NULL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100589 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800591 req = kzalloc(sizeof(*req), mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (!req)
593 return NULL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100594 INIT_LIST_HEAD(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return &req->req;
596}
597
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100598static void dummy_free_request(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 struct dummy_request *req;
601
Sebastian Andrzej Siewiorf99987b2012-02-09 09:24:59 +0100602 if (!_ep || !_req) {
Sasha Levin72688dc2012-05-11 06:39:37 +0200603 WARN_ON(1);
Sebastian Andrzej Siewior20edfbb2012-01-25 15:18:58 +0100604 return;
Sebastian Andrzej Siewiorf99987b2012-02-09 09:24:59 +0100605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100607 req = usb_request_to_dummy_request(_req);
608 WARN_ON(!list_empty(&req->queue));
609 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100612static void fifo_complete(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614}
615
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100616static int dummy_queue(struct usb_ep *_ep, struct usb_request *_req,
Al Viro55016f12005-10-21 03:21:58 -0400617 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 struct dummy_ep *ep;
620 struct dummy_request *req;
621 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300622 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 unsigned long flags;
624
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100625 req = usb_request_to_dummy_request(_req);
626 if (!_req || !list_empty(&req->queue) || !_req->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return -EINVAL;
628
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100629 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (!_ep || (!ep->desc && _ep->name != ep0name))
631 return -EINVAL;
632
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100633 dum = ep_to_dummy(ep);
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200634 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300635 if (!dum->driver || !is_enabled(dum_hcd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return -ESHUTDOWN;
637
638#if 0
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100639 dev_dbg(udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 ep, _req, _ep->name, _req->length, _req->buf);
641#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 _req->status = -EINPROGRESS;
643 _req->actual = 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100644 spin_lock_irqsave(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 /* implement an emulated single-request FIFO */
647 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100648 list_empty(&dum->fifo_req.queue) &&
649 list_empty(&ep->queue) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 _req->length <= FIFO_SIZE) {
651 req = &dum->fifo_req;
652 req->req = *_req;
653 req->req.buf = dum->fifo_buf;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100654 memcpy(dum->fifo_buf, _req->buf, _req->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 req->req.context = dum;
656 req->req.complete = fifo_complete;
657
David Brownellc728df72008-07-26 08:06:24 -0700658 list_add_tail(&req->queue, &ep->queue);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100659 spin_unlock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 _req->actual = _req->length;
661 _req->status = 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100662 _req->complete(_ep, _req);
663 spin_lock(&dum->lock);
David Brownellc728df72008-07-26 08:06:24 -0700664 } else
665 list_add_tail(&req->queue, &ep->queue);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100666 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 /* real hardware would likely enable transfers here, in case
669 * it'd been left NAKing.
670 */
671 return 0;
672}
673
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100674static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 struct dummy_ep *ep;
677 struct dummy *dum;
678 int retval = -EINVAL;
679 unsigned long flags;
680 struct dummy_request *req = NULL;
681
682 if (!_ep || !_req)
683 return retval;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100684 ep = usb_ep_to_dummy_ep(_ep);
685 dum = ep_to_dummy(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 if (!dum->driver)
688 return -ESHUTDOWN;
689
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100690 local_irq_save(flags);
691 spin_lock(&dum->lock);
692 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (&req->req == _req) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100694 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 _req->status = -ECONNRESET;
696 retval = 0;
697 break;
698 }
699 }
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100700 spin_unlock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 if (retval == 0) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100703 dev_dbg(udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 "dequeued req %p from %s, len %d buf %p\n",
705 req, _ep->name, _req->length, _req->buf);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100706 _req->complete(_ep, _req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100708 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return retval;
710}
711
712static int
Alan Stern851a5262008-08-14 15:48:30 -0400713dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 struct dummy_ep *ep;
716 struct dummy *dum;
717
718 if (!_ep)
719 return -EINVAL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100720 ep = usb_ep_to_dummy_ep(_ep);
721 dum = ep_to_dummy(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (!dum->driver)
723 return -ESHUTDOWN;
724 if (!value)
Alan Stern851a5262008-08-14 15:48:30 -0400725 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100727 !list_empty(&ep->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return -EAGAIN;
Alan Stern851a5262008-08-14 15:48:30 -0400729 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 ep->halted = 1;
Alan Stern851a5262008-08-14 15:48:30 -0400731 if (wedged)
732 ep->wedged = 1;
733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /* FIXME clear emulated data toggle too */
735 return 0;
736}
737
Alan Stern851a5262008-08-14 15:48:30 -0400738static int
739dummy_set_halt(struct usb_ep *_ep, int value)
740{
741 return dummy_set_halt_and_wedge(_ep, value, 0);
742}
743
744static int dummy_set_wedge(struct usb_ep *_ep)
745{
746 if (!_ep || _ep->name == ep0name)
747 return -EINVAL;
748 return dummy_set_halt_and_wedge(_ep, 1, 1);
749}
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751static const struct usb_ep_ops dummy_ep_ops = {
752 .enable = dummy_enable,
753 .disable = dummy_disable,
754
755 .alloc_request = dummy_alloc_request,
756 .free_request = dummy_free_request,
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 .queue = dummy_queue,
759 .dequeue = dummy_dequeue,
760
761 .set_halt = dummy_set_halt,
Alan Stern851a5262008-08-14 15:48:30 -0400762 .set_wedge = dummy_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763};
764
765/*-------------------------------------------------------------------------*/
766
767/* there are both host and device side versions of this call ... */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100768static int dummy_g_get_frame(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 struct timeval tv;
771
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100772 do_gettimeofday(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return tv.tv_usec / 1000;
774}
775
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100776static int dummy_wakeup(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300778 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300780 dum_hcd = gadget_to_dummy_hcd(_gadget);
781 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
Alan Stern5742b0c2005-05-02 11:25:17 -0400782 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return -EINVAL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300784 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
Alan Stern391eca92005-05-10 15:34:16 -0400785 return -ENOLINK;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300786 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
787 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -0400788 return -EIO;
789
790 /* FIXME: What if the root hub is suspended but the port isn't? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 /* hub notices our request, issues downstream resume, etc */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300793 dum_hcd->resuming = 1;
794 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
795 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return 0;
797}
798
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100799static int dummy_set_selfpowered(struct usb_gadget *_gadget, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 struct dummy *dum;
802
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100803 dum = gadget_to_dummy_hcd(_gadget)->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (value)
805 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
806 else
807 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
808 return 0;
809}
810
Sebastian Andrzej Siewiord2621272012-01-09 13:14:59 +0100811static void dummy_udc_update_ep0(struct dummy *dum)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200812{
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100813 if (dum->gadget.speed == USB_SPEED_SUPER)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200814 dum->ep[0].ep.maxpacket = 9;
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100815 else
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200816 dum->ep[0].ep.maxpacket = 64;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200817}
818
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100819static int dummy_pullup(struct usb_gadget *_gadget, int value)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400820{
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200821 struct dummy_hcd *dum_hcd;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400822 struct dummy *dum;
823 unsigned long flags;
824
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200825 dum = gadget_dev_to_dummy(&_gadget->dev);
826
827 if (value && dum->driver) {
828 if (mod_data.is_super_speed)
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100829 dum->gadget.speed = dum->driver->max_speed;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200830 else if (mod_data.is_high_speed)
831 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100832 dum->driver->max_speed);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200833 else
834 dum->gadget.speed = USB_SPEED_FULL;
Sebastian Andrzej Siewiord2621272012-01-09 13:14:59 +0100835 dummy_udc_update_ep0(dum);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200836
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100837 if (dum->gadget.speed < dum->driver->max_speed)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200838 dev_dbg(udc_dev(dum), "This device can perform faster"
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100839 " if you connect it to a %s port...\n",
840 usb_speed_string(dum->driver->max_speed));
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200841 }
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200842 dum_hcd = gadget_to_dummy_hcd(_gadget);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200843
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100844 spin_lock_irqsave(&dum->lock, flags);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400845 dum->pullup = (value != 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200846 set_link_state(dum_hcd);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100847 spin_unlock_irqrestore(&dum->lock, flags);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200848
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200849 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Sternf1c39fa2005-05-03 16:24:04 -0400850 return 0;
851}
852
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200853static int dummy_udc_start(struct usb_gadget *g,
854 struct usb_gadget_driver *driver);
855static int dummy_udc_stop(struct usb_gadget *g,
856 struct usb_gadget_driver *driver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858static const struct usb_gadget_ops dummy_ops = {
859 .get_frame = dummy_g_get_frame,
860 .wakeup = dummy_wakeup,
861 .set_selfpowered = dummy_set_selfpowered,
Alan Sternf1c39fa2005-05-03 16:24:04 -0400862 .pullup = dummy_pullup,
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200863 .udc_start = dummy_udc_start,
864 .udc_stop = dummy_udc_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865};
866
867/*-------------------------------------------------------------------------*/
868
869/* "function" sysfs attribute */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100870static ssize_t show_function(struct device *dev, struct device_attribute *attr,
871 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100873 struct dummy *dum = gadget_dev_to_dummy(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 if (!dum->driver || !dum->driver->function)
876 return 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100877 return scnprintf(buf, PAGE_SIZE, "%s\n", dum->driver->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100879static DEVICE_ATTR(function, S_IRUGO, show_function, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881/*-------------------------------------------------------------------------*/
882
883/*
884 * Driver registration/unregistration.
885 *
886 * This is basically hardware-specific; there's usually only one real USB
887 * device (not host) controller since that's how USB devices are intended
888 * to work. So most implementations of these api calls will rely on the
889 * fact that only one driver will ever bind to the hardware. But curious
890 * hardware can be built with discrete components, so the gadget API doesn't
891 * require that assumption.
892 *
893 * For this emulator, it might be convenient to create a usb slave device
894 * for each driver that registers: just add to a big root hub.
895 */
896
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200897static int dummy_udc_start(struct usb_gadget *g,
898 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200900 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
901 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100903 if (driver->max_speed == USB_SPEED_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return -EINVAL;
905
906 /*
907 * SLAVE side init ... the layer above hardware, which
908 * can't enumerate without help from the driver we're binding.
909 */
Alan Stern5742b0c2005-05-02 11:25:17 -0400910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 dum->devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 dum->driver = driver;
Alan Sternd0d86072012-08-20 16:12:47 -0400914 dum->gadget.dev.driver = &driver->driver;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100915 dev_dbg(udc_dev(dum), "binding gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return 0;
918}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200920static int dummy_udc_stop(struct usb_gadget *g,
921 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200923 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
924 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100926 dev_dbg(udc_dev(dum), "unregister gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 driver->driver.name);
928
Alan Sternd0d86072012-08-20 16:12:47 -0400929 dum->gadget.dev.driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 dum->driver = NULL;
Sebastian Andrzej Siewior25427872011-06-16 20:36:55 +0200931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return 0;
933}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935#undef is_enabled
936
Alan Sternd9b76252005-05-03 16:15:43 -0400937/* The gadget structure is stored inside the hcd structure and will be
938 * released along with it. */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100939static void dummy_gadget_release(struct device *dev)
Alan Sternd9b76252005-05-03 16:15:43 -0400940{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300941 return;
Alan Sternd9b76252005-05-03 16:15:43 -0400942}
943
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200944static void init_dummy_udc_hw(struct dummy *dum)
945{
946 int i;
947
948 INIT_LIST_HEAD(&dum->gadget.ep_list);
949 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
950 struct dummy_ep *ep = &dum->ep[i];
951
952 if (!ep_name[i])
953 break;
954 ep->ep.name = ep_name[i];
955 ep->ep.ops = &dummy_ep_ops;
956 list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
957 ep->halted = ep->wedged = ep->already_seen =
958 ep->setup_stage = 0;
959 ep->ep.maxpacket = ~0;
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100960 ep->ep.max_streams = 16;
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200961 ep->last_io = jiffies;
962 ep->gadget = &dum->gadget;
963 ep->desc = NULL;
964 INIT_LIST_HEAD(&ep->queue);
965 }
966
967 dum->gadget.ep0 = &dum->ep[0].ep;
968 list_del_init(&dum->ep[0].ep.ep_list);
969 INIT_LIST_HEAD(&dum->fifo_req.queue);
Sebastian Andrzej Siewiorf8744d42011-06-23 14:26:13 +0200970
971#ifdef CONFIG_USB_OTG
972 dum->gadget.is_otg = 1;
973#endif
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200974}
975
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100976static int dummy_udc_probe(struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -0400977{
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +0100978 struct dummy *dum;
Alan Sternd9b76252005-05-03 16:15:43 -0400979 int rc;
980
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +0100981 dum = *((void **)dev_get_platdata(&pdev->dev));
Alan Sternd9b76252005-05-03 16:15:43 -0400982 dum->gadget.name = gadget_name;
983 dum->gadget.ops = &dummy_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100984 dum->gadget.max_speed = USB_SPEED_SUPER;
Alan Sternd9b76252005-05-03 16:15:43 -0400985
Kay Sievers0031a062008-05-02 06:02:41 +0200986 dev_set_name(&dum->gadget.dev, "gadget");
Alan Stern8364d6b2005-11-14 12:16:30 -0500987 dum->gadget.dev.parent = &pdev->dev;
Alan Sternd9b76252005-05-03 16:15:43 -0400988 dum->gadget.dev.release = dummy_gadget_release;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100989 rc = device_register(&dum->gadget.dev);
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530990 if (rc < 0) {
991 put_device(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -0400992 return rc;
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530993 }
Alan Sternd9b76252005-05-03 16:15:43 -0400994
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200995 init_dummy_udc_hw(dum);
996
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300997 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
998 if (rc < 0)
999 goto err_udc;
1000
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001001 rc = device_create_file(&dum->gadget.dev, &dev_attr_function);
Alan Sternefd54a32006-09-25 11:55:56 -04001002 if (rc < 0)
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001003 goto err_dev;
1004 platform_set_drvdata(pdev, dum);
1005 return rc;
1006
1007err_dev:
1008 usb_del_gadget_udc(&dum->gadget);
1009err_udc:
1010 device_unregister(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -04001011 return rc;
1012}
1013
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001014static int dummy_udc_remove(struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -04001015{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001016 struct dummy *dum = platform_get_drvdata(pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04001017
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001018 usb_del_gadget_udc(&dum->gadget);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001019 platform_set_drvdata(pdev, NULL);
1020 device_remove_file(&dum->gadget.dev, &dev_attr_function);
1021 device_unregister(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -04001022 return 0;
1023}
1024
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001025static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1026 int suspend)
Alan Stern391eca92005-05-10 15:34:16 -04001027{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001028 spin_lock_irq(&dum->lock);
1029 dum->udc_suspended = suspend;
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001030 set_link_state(dum_hcd);
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001031 spin_unlock_irq(&dum->lock);
1032}
Alan Stern391eca92005-05-10 15:34:16 -04001033
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001034static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1035{
1036 struct dummy *dum = platform_get_drvdata(pdev);
1037 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1038
1039 dev_dbg(&pdev->dev, "%s\n", __func__);
1040 dummy_udc_pm(dum, dum_hcd, 1);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001041 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001042 return 0;
1043}
1044
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001045static int dummy_udc_resume(struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04001046{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001047 struct dummy *dum = platform_get_drvdata(pdev);
1048 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Alan Stern391eca92005-05-10 15:34:16 -04001049
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001050 dev_dbg(&pdev->dev, "%s\n", __func__);
1051 dummy_udc_pm(dum, dum_hcd, 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001052 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001053 return 0;
1054}
1055
Russell King3ae5eae2005-11-09 22:32:44 +00001056static struct platform_driver dummy_udc_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04001057 .probe = dummy_udc_probe,
1058 .remove = dummy_udc_remove,
Alan Stern391eca92005-05-10 15:34:16 -04001059 .suspend = dummy_udc_suspend,
1060 .resume = dummy_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001061 .driver = {
1062 .name = (char *) gadget_name,
1063 .owner = THIS_MODULE,
1064 },
Alan Sternd9b76252005-05-03 16:15:43 -04001065};
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067/*-------------------------------------------------------------------------*/
1068
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001069static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc)
1070{
1071 unsigned int index;
1072
1073 index = usb_endpoint_num(desc) << 1;
1074 if (usb_endpoint_dir_in(desc))
1075 index |= 1;
1076 return index;
1077}
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079/* MASTER/HOST SIDE DRIVER
1080 *
1081 * this uses the hcd framework to hook up to host side drivers.
1082 * its root hub will only have one device, otherwise it acts like
1083 * a normal host controller.
1084 *
1085 * when urbs are queued, they're just stuck on a list that we
1086 * scan in a timer callback. that callback connects writes from
1087 * the host with reads from the device, and so on, based on the
1088 * usb 2.0 rules.
1089 */
1090
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001091static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb)
1092{
1093 const struct usb_endpoint_descriptor *desc = &urb->ep->desc;
1094 u32 index;
1095
1096 if (!usb_endpoint_xfer_bulk(desc))
1097 return 0;
1098
1099 index = dummy_get_ep_idx(desc);
1100 return (1 << index) & dum_hcd->stream_en_ep;
1101}
1102
1103/*
1104 * The max stream number is saved as a nibble so for the 30 possible endpoints
1105 * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
1106 * means we use only 1 stream). The maximum according to the spec is 16bit so
1107 * if the 16 stream limit is about to go, the array size should be incremented
1108 * to 30 elements of type u16.
1109 */
1110static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1111 unsigned int pipe)
1112{
1113 int max_streams;
1114
1115 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1116 if (usb_pipeout(pipe))
1117 max_streams >>= 4;
1118 else
1119 max_streams &= 0xf;
1120 max_streams++;
1121 return max_streams;
1122}
1123
1124static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1125 unsigned int pipe, unsigned int streams)
1126{
1127 int max_streams;
1128
1129 streams--;
1130 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1131 if (usb_pipeout(pipe)) {
1132 streams <<= 4;
1133 max_streams &= 0xf;
1134 } else {
1135 max_streams &= 0xf0;
1136 }
1137 max_streams |= streams;
1138 dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams;
1139}
1140
1141static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb)
1142{
1143 unsigned int max_streams;
1144 int enabled;
1145
1146 enabled = dummy_ep_stream_en(dum_hcd, urb);
1147 if (!urb->stream_id) {
1148 if (enabled)
1149 return -EINVAL;
1150 return 0;
1151 }
1152 if (!enabled)
1153 return -EINVAL;
1154
1155 max_streams = get_max_streams_for_pipe(dum_hcd,
1156 usb_pipeendpoint(urb->pipe));
1157 if (urb->stream_id > max_streams) {
1158 dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n",
1159 urb->stream_id);
1160 BUG();
1161 return -EINVAL;
1162 }
1163 return 0;
1164}
1165
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001166static int dummy_urb_enqueue(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 struct usb_hcd *hcd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001169 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001171 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 struct urbp *urbp;
1173 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001174 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001176 urbp = kmalloc(sizeof *urbp, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (!urbp)
1178 return -ENOMEM;
1179 urbp->urb = urb;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001180 urbp->miter_started = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001182 dum_hcd = hcd_to_dummy_hcd(hcd);
1183 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001184
1185 rc = dummy_validate_stream(dum_hcd, urb);
1186 if (rc) {
1187 kfree(urbp);
1188 goto done;
1189 }
1190
Alan Sterne9df41c2007-08-08 11:48:02 -04001191 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1192 if (rc) {
1193 kfree(urbp);
1194 goto done;
1195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001197 if (!dum_hcd->udev) {
1198 dum_hcd->udev = urb->dev;
1199 usb_get_dev(dum_hcd->udev);
1200 } else if (unlikely(dum_hcd->udev != urb->dev))
1201 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001203 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 urb->hcpriv = urbp;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001205 if (usb_pipetype(urb->pipe) == PIPE_CONTROL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 urb->error_count = 1; /* mark as a new urb */
1207
1208 /* kick the scheduler, it'll do the rest */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001209 if (!timer_pending(&dum_hcd->timer))
1210 mod_timer(&dum_hcd->timer, jiffies + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Alan Sterne9df41c2007-08-08 11:48:02 -04001212 done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001213 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001214 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
Alan Sterne9df41c2007-08-08 11:48:02 -04001217static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001219 struct dummy_hcd *dum_hcd;
Alan Stern391eca92005-05-10 15:34:16 -04001220 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001221 int rc;
Alan Stern391eca92005-05-10 15:34:16 -04001222
1223 /* giveback happens automatically in timer callback,
1224 * so make sure the callback happens */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001225 dum_hcd = hcd_to_dummy_hcd(hcd);
1226 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001227
1228 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001229 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1230 !list_empty(&dum_hcd->urbp_list))
1231 mod_timer(&dum_hcd->timer, jiffies);
Alan Sterne9df41c2007-08-08 11:48:02 -04001232
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001233 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001234 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001237static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
1238 u32 len)
1239{
1240 void *ubuf, *rbuf;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001241 struct urbp *urbp = urb->hcpriv;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001242 int to_host;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001243 struct sg_mapping_iter *miter = &urbp->miter;
1244 u32 trans = 0;
1245 u32 this_sg;
1246 bool next_sg;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001247
1248 to_host = usb_pipein(urb->pipe);
1249 rbuf = req->req.buf + req->req.actual;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001250
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001251 if (!urb->num_sgs) {
1252 ubuf = urb->transfer_buffer + urb->actual_length;
1253 if (to_host)
1254 memcpy(ubuf, rbuf, len);
1255 else
1256 memcpy(rbuf, ubuf, len);
1257 return len;
1258 }
1259
1260 if (!urbp->miter_started) {
1261 u32 flags = SG_MITER_ATOMIC;
1262
1263 if (to_host)
1264 flags |= SG_MITER_TO_SG;
1265 else
1266 flags |= SG_MITER_FROM_SG;
1267
1268 sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
1269 urbp->miter_started = 1;
1270 }
1271 next_sg = sg_miter_next(miter);
1272 if (next_sg == false) {
1273 WARN_ON_ONCE(1);
1274 return -EINVAL;
1275 }
1276 do {
1277 ubuf = miter->addr;
1278 this_sg = min_t(u32, len, miter->length);
1279 miter->consumed = this_sg;
1280 trans += this_sg;
1281
1282 if (to_host)
1283 memcpy(ubuf, rbuf, this_sg);
1284 else
1285 memcpy(rbuf, ubuf, this_sg);
1286 len -= this_sg;
1287
1288 if (!len)
1289 break;
1290 next_sg = sg_miter_next(miter);
1291 if (next_sg == false) {
1292 WARN_ON_ONCE(1);
1293 return -EINVAL;
1294 }
1295
1296 rbuf += this_sg;
1297 } while (1);
1298
1299 sg_miter_stop(miter);
1300 return trans;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001301}
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303/* transfer up to a frame's worth; caller must own lock */
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001304static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb,
1305 struct dummy_ep *ep, int limit, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001307 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 struct dummy_request *req;
1309
1310top:
1311 /* if there's no request queued, the device is NAKing; return */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001312 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 unsigned host_len, dev_len, len;
1314 int is_short, to_host;
1315 int rescan = 0;
1316
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001317 if (dummy_ep_stream_en(dum_hcd, urb)) {
1318 if ((urb->stream_id != req->req.stream_id))
1319 continue;
1320 }
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 /* 1..N packets of ep->ep.maxpacket each ... the last one
1323 * may be short (including zero length).
1324 *
1325 * writer can send a zlp explicitly (length 0) or implicitly
1326 * (length mod maxpacket zero, and 'zero' flag); they always
1327 * terminate reads.
1328 */
1329 host_len = urb->transfer_buffer_length - urb->actual_length;
1330 dev_len = req->req.length - req->req.actual;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001331 len = min(host_len, dev_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 /* FIXME update emulated data toggle too */
1334
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001335 to_host = usb_pipein(urb->pipe);
1336 if (unlikely(len == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 is_short = 1;
1338 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /* not enough bandwidth left? */
1340 if (limit < ep->ep.maxpacket && limit < len)
1341 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001342 len = min_t(unsigned, len, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (len == 0)
1344 break;
1345
1346 /* use an extra pass for the final short packet */
1347 if (len > ep->ep.maxpacket) {
1348 rescan = 1;
1349 len -= (len % ep->ep.maxpacket);
1350 }
1351 is_short = (len % ep->ep.maxpacket) != 0;
1352
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001353 len = dummy_perform_transfer(urb, req, len);
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 ep->last_io = jiffies;
Dan Carpenterb1443ac0e2012-03-02 21:51:00 +03001356 if ((int)len < 0) {
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001357 req->req.status = len;
1358 } else {
1359 limit -= len;
1360 urb->actual_length += len;
1361 req->req.actual += len;
1362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364
1365 /* short packets terminate, maybe with overflow/underflow.
1366 * it's only really an error to write too much.
1367 *
1368 * partially filling a buffer optionally blocks queue advances
1369 * (so completion handlers can clean up the queue) but we don't
Alan Sternb0d9efb2007-08-21 15:39:21 -04001370 * need to emulate such data-in-flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 */
1372 if (is_short) {
1373 if (host_len == dev_len) {
1374 req->req.status = 0;
Alan Stern4d2f1102007-08-24 15:40:10 -04001375 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 } else if (to_host) {
1377 req->req.status = 0;
1378 if (dev_len > host_len)
Alan Stern4d2f1102007-08-24 15:40:10 -04001379 *status = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 else
Alan Stern4d2f1102007-08-24 15:40:10 -04001381 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 } else if (!to_host) {
Alan Stern4d2f1102007-08-24 15:40:10 -04001383 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (host_len > dev_len)
1385 req->req.status = -EOVERFLOW;
1386 else
1387 req->req.status = 0;
1388 }
1389
1390 /* many requests terminate without a short packet */
1391 } else {
1392 if (req->req.length == req->req.actual
1393 && !req->req.zero)
1394 req->req.status = 0;
1395 if (urb->transfer_buffer_length == urb->actual_length
1396 && !(urb->transfer_flags
Alan Stern4d2f1102007-08-24 15:40:10 -04001397 & URB_ZERO_PACKET))
1398 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400
1401 /* device side completion --> continuable */
1402 if (req->req.status != -EINPROGRESS) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001403 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001405 spin_unlock(&dum->lock);
1406 req->req.complete(&ep->ep, &req->req);
1407 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 /* requests might have been unlinked... */
1410 rescan = 1;
1411 }
1412
1413 /* host side completion --> terminate */
Alan Stern4d2f1102007-08-24 15:40:10 -04001414 if (*status != -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 break;
1416
1417 /* rescan to continue with any other queued i/o */
1418 if (rescan)
1419 goto top;
1420 }
1421 return limit;
1422}
1423
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001424static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
1426 int limit = ep->ep.maxpacket;
1427
1428 if (dum->gadget.speed == USB_SPEED_HIGH) {
1429 int tmp;
1430
1431 /* high bandwidth mode */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001432 tmp = usb_endpoint_maxp(ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 tmp = (tmp >> 11) & 0x03;
1434 tmp *= 8 /* applies to entire frame */;
1435 limit += limit * tmp;
1436 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001437 if (dum->gadget.speed == USB_SPEED_SUPER) {
Sebastian Andrzej Siewior59f08e62012-01-12 12:53:14 +01001438 switch (usb_endpoint_type(ep->desc)) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001439 case USB_ENDPOINT_XFER_ISOC:
1440 /* Sec. 4.4.8.2 USB3.0 Spec */
1441 limit = 3 * 16 * 1024 * 8;
1442 break;
1443 case USB_ENDPOINT_XFER_INT:
1444 /* Sec. 4.4.7.2 USB3.0 Spec */
1445 limit = 3 * 1024 * 8;
1446 break;
1447 case USB_ENDPOINT_XFER_BULK:
1448 default:
1449 break;
1450 }
1451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return limit;
1453}
1454
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001455#define is_active(dum_hcd) ((dum_hcd->port_status & \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1457 USB_PORT_STAT_SUSPEND)) \
1458 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1459
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001460static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
1462 int i;
1463
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001464 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1465 dum->ss_hcd : dum->hs_hcd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return NULL;
1467 if ((address & ~USB_DIR_IN) == 0)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001468 return &dum->ep[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001470 struct dummy_ep *ep = &dum->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 if (!ep->desc)
1473 continue;
1474 if (ep->desc->bEndpointAddress == address)
1475 return ep;
1476 }
1477 return NULL;
1478}
1479
1480#undef is_active
1481
1482#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1483#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1484#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1485#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1486#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1487#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1488
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001489
1490/**
1491 * handle_control_request() - handles all control transfers
1492 * @dum: pointer to dummy (the_controller)
1493 * @urb: the urb request to handle
1494 * @setup: pointer to the setup data for a USB device control
1495 * request
1496 * @status: pointer to request handling status
1497 *
1498 * Return 0 - if the request was handled
1499 * 1 - if the request wasn't handles
1500 * error code on error
1501 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001502static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001503 struct usb_ctrlrequest *setup,
1504 int *status)
1505{
1506 struct dummy_ep *ep2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001507 struct dummy *dum = dum_hcd->dum;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001508 int ret_val = 1;
1509 unsigned w_index;
1510 unsigned w_value;
1511
1512 w_index = le16_to_cpu(setup->wIndex);
1513 w_value = le16_to_cpu(setup->wValue);
1514 switch (setup->bRequest) {
1515 case USB_REQ_SET_ADDRESS:
1516 if (setup->bRequestType != Dev_Request)
1517 break;
1518 dum->address = w_value;
1519 *status = 0;
1520 dev_dbg(udc_dev(dum), "set_address = %d\n",
1521 w_value);
1522 ret_val = 0;
1523 break;
1524 case USB_REQ_SET_FEATURE:
1525 if (setup->bRequestType == Dev_Request) {
1526 ret_val = 0;
1527 switch (w_value) {
1528 case USB_DEVICE_REMOTE_WAKEUP:
1529 break;
1530 case USB_DEVICE_B_HNP_ENABLE:
1531 dum->gadget.b_hnp_enable = 1;
1532 break;
1533 case USB_DEVICE_A_HNP_SUPPORT:
1534 dum->gadget.a_hnp_support = 1;
1535 break;
1536 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1537 dum->gadget.a_alt_hnp_support = 1;
1538 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001539 case USB_DEVICE_U1_ENABLE:
1540 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1541 HCD_USB3)
1542 w_value = USB_DEV_STAT_U1_ENABLED;
1543 else
1544 ret_val = -EOPNOTSUPP;
1545 break;
1546 case USB_DEVICE_U2_ENABLE:
1547 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1548 HCD_USB3)
1549 w_value = USB_DEV_STAT_U2_ENABLED;
1550 else
1551 ret_val = -EOPNOTSUPP;
1552 break;
1553 case USB_DEVICE_LTM_ENABLE:
1554 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1555 HCD_USB3)
1556 w_value = USB_DEV_STAT_LTM_ENABLED;
1557 else
1558 ret_val = -EOPNOTSUPP;
1559 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001560 default:
1561 ret_val = -EOPNOTSUPP;
1562 }
1563 if (ret_val == 0) {
1564 dum->devstatus |= (1 << w_value);
1565 *status = 0;
1566 }
1567 } else if (setup->bRequestType == Ep_Request) {
1568 /* endpoint halt */
1569 ep2 = find_endpoint(dum, w_index);
1570 if (!ep2 || ep2->ep.name == ep0name) {
1571 ret_val = -EOPNOTSUPP;
1572 break;
1573 }
1574 ep2->halted = 1;
1575 ret_val = 0;
1576 *status = 0;
1577 }
1578 break;
1579 case USB_REQ_CLEAR_FEATURE:
1580 if (setup->bRequestType == Dev_Request) {
1581 ret_val = 0;
1582 switch (w_value) {
1583 case USB_DEVICE_REMOTE_WAKEUP:
1584 w_value = USB_DEVICE_REMOTE_WAKEUP;
1585 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001586 case USB_DEVICE_U1_ENABLE:
1587 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1588 HCD_USB3)
1589 w_value = USB_DEV_STAT_U1_ENABLED;
1590 else
1591 ret_val = -EOPNOTSUPP;
1592 break;
1593 case USB_DEVICE_U2_ENABLE:
1594 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1595 HCD_USB3)
1596 w_value = USB_DEV_STAT_U2_ENABLED;
1597 else
1598 ret_val = -EOPNOTSUPP;
1599 break;
1600 case USB_DEVICE_LTM_ENABLE:
1601 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1602 HCD_USB3)
1603 w_value = USB_DEV_STAT_LTM_ENABLED;
1604 else
1605 ret_val = -EOPNOTSUPP;
1606 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001607 default:
1608 ret_val = -EOPNOTSUPP;
1609 break;
1610 }
1611 if (ret_val == 0) {
1612 dum->devstatus &= ~(1 << w_value);
1613 *status = 0;
1614 }
1615 } else if (setup->bRequestType == Ep_Request) {
1616 /* endpoint halt */
1617 ep2 = find_endpoint(dum, w_index);
1618 if (!ep2) {
1619 ret_val = -EOPNOTSUPP;
1620 break;
1621 }
1622 if (!ep2->wedged)
1623 ep2->halted = 0;
1624 ret_val = 0;
1625 *status = 0;
1626 }
1627 break;
1628 case USB_REQ_GET_STATUS:
1629 if (setup->bRequestType == Dev_InRequest
1630 || setup->bRequestType == Intf_InRequest
1631 || setup->bRequestType == Ep_InRequest) {
1632 char *buf;
1633 /*
1634 * device: remote wakeup, selfpowered
1635 * interface: nothing
1636 * endpoint: halt
1637 */
1638 buf = (char *)urb->transfer_buffer;
1639 if (urb->transfer_buffer_length > 0) {
1640 if (setup->bRequestType == Ep_InRequest) {
1641 ep2 = find_endpoint(dum, w_index);
1642 if (!ep2) {
1643 ret_val = -EOPNOTSUPP;
1644 break;
1645 }
1646 buf[0] = ep2->halted;
1647 } else if (setup->bRequestType ==
1648 Dev_InRequest) {
1649 buf[0] = (u8)dum->devstatus;
1650 } else
1651 buf[0] = 0;
1652 }
1653 if (urb->transfer_buffer_length > 1)
1654 buf[1] = 0;
1655 urb->actual_length = min_t(u32, 2,
1656 urb->transfer_buffer_length);
1657 ret_val = 0;
1658 *status = 0;
1659 }
1660 break;
1661 }
1662 return ret_val;
1663}
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665/* drive both sides of the transfers; looks like irq handlers to
1666 * both drivers except the callbacks aren't in_irq().
1667 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001668static void dummy_timer(unsigned long _dum_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001670 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1671 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 struct urbp *urbp, *tmp;
1673 unsigned long flags;
1674 int limit, total;
1675 int i;
1676
1677 /* simplistic model for one frame's bandwidth */
1678 switch (dum->gadget.speed) {
1679 case USB_SPEED_LOW:
1680 total = 8/*bytes*/ * 12/*packets*/;
1681 break;
1682 case USB_SPEED_FULL:
1683 total = 64/*bytes*/ * 19/*packets*/;
1684 break;
1685 case USB_SPEED_HIGH:
1686 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1687 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001688 case USB_SPEED_SUPER:
1689 /* Bus speed is 500000 bytes/ms, so use a little less */
1690 total = 490000;
1691 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001693 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 return;
1695 }
1696
1697 /* FIXME if HZ != 1000 this will probably misbehave ... */
1698
1699 /* look at each urb queued by the host side driver */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001700 spin_lock_irqsave(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001702 if (!dum_hcd->udev) {
1703 dev_err(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 "timer fired with no URBs pending?\n");
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001705 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 return;
1707 }
1708
1709 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001710 if (!ep_name[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001712 dum->ep[i].already_seen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
1714
1715restart:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001716 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 struct urb *urb;
1718 struct dummy_request *req;
1719 u8 address;
1720 struct dummy_ep *ep = NULL;
1721 int type;
Alan Stern4d2f1102007-08-24 15:40:10 -04001722 int status = -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 urb = urbp->urb;
Alan Sterneb231052007-08-21 15:40:36 -04001725 if (urb->unlinked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 goto return_urb;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001727 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
Alan Stern391eca92005-05-10 15:34:16 -04001728 continue;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001729 type = usb_pipetype(urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 /* used up this frame's non-periodic bandwidth?
1732 * FIXME there's infinite bandwidth for control and
1733 * periodic transfers ... unrealistic.
1734 */
1735 if (total <= 0 && type == PIPE_BULK)
1736 continue;
1737
1738 /* find the gadget's ep for this request (if configured) */
1739 address = usb_pipeendpoint (urb->pipe);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001740 if (usb_pipein(urb->pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 address |= USB_DIR_IN;
1742 ep = find_endpoint(dum, address);
1743 if (!ep) {
1744 /* set_configuration() disagreement */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001745 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 "no ep configured for urb %p\n",
1747 urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001748 status = -EPROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 goto return_urb;
1750 }
1751
1752 if (ep->already_seen)
1753 continue;
1754 ep->already_seen = 1;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001755 if (ep == &dum->ep[0] && urb->error_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 ep->setup_stage = 1; /* a new urb */
1757 urb->error_count = 0;
1758 }
1759 if (ep->halted && !ep->setup_stage) {
1760 /* NOTE: must not be iso! */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001761 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 ep->ep.name, urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001763 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 goto return_urb;
1765 }
1766 /* FIXME make sure both ends agree on maxpacket */
1767
1768 /* handle control requests */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001769 if (ep == &dum->ep[0] && ep->setup_stage) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 struct usb_ctrlrequest setup;
1771 int value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001773 setup = *(struct usb_ctrlrequest *) urb->setup_packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 /* paranoia, in case of stale queued data */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001775 list_for_each_entry(req, &ep->queue, queue) {
1776 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 req->req.status = -EOVERFLOW;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001778 dev_dbg(udc_dev(dum), "stale req = %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 req);
1780
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001781 spin_unlock(&dum->lock);
1782 req->req.complete(&ep->ep, &req->req);
1783 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 ep->already_seen = 0;
1785 goto restart;
1786 }
1787
1788 /* gadget driver never sees set_address or operations
1789 * on standard feature flags. some hardware doesn't
1790 * even expose them.
1791 */
1792 ep->last_io = jiffies;
1793 ep->setup_stage = 0;
1794 ep->halted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001796 value = handle_control_request(dum_hcd, urb, &setup,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001797 &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 /* gadget driver handles all other requests. block
1800 * until setup() returns; no reentrancy issues etc.
1801 */
1802 if (value > 0) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001803 spin_unlock(&dum->lock);
1804 value = dum->driver->setup(&dum->gadget,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 &setup);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001806 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 if (value >= 0) {
1809 /* no delays (max 64KB data stage) */
1810 limit = 64*1024;
1811 goto treat_control_like_bulk;
1812 }
1813 /* error, see below */
1814 }
1815
1816 if (value < 0) {
1817 if (value != -EOPNOTSUPP)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001818 dev_dbg(udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 "setup --> %d\n",
1820 value);
Alan Stern4d2f1102007-08-24 15:40:10 -04001821 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 urb->actual_length = 0;
1823 }
1824
1825 goto return_urb;
1826 }
1827
1828 /* non-control requests */
1829 limit = total;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001830 switch (usb_pipetype(urb->pipe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 case PIPE_ISOCHRONOUS:
1832 /* FIXME is it urb->interval since the last xfer?
1833 * use urb->iso_frame_desc[i].
1834 * complete whether or not ep has requests queued.
1835 * report random errors, to debug drivers.
1836 */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001837 limit = max(limit, periodic_bytes(dum, ep));
Alan Stern4d2f1102007-08-24 15:40:10 -04001838 status = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 break;
1840
1841 case PIPE_INTERRUPT:
1842 /* FIXME is it urb->interval since the last xfer?
1843 * this almost certainly polls too fast.
1844 */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001845 limit = max(limit, periodic_bytes(dum, ep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 /* FALLTHROUGH */
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 default:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001849treat_control_like_bulk:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 ep->last_io = jiffies;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001851 total = transfer(dum_hcd, urb, ep, limit, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 break;
1853 }
1854
1855 /* incomplete transfer? */
Alan Stern4d2f1102007-08-24 15:40:10 -04001856 if (status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 continue;
1858
1859return_urb:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001860 list_del(&urbp->urbp_list);
1861 kfree(urbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 if (ep)
1863 ep->already_seen = ep->setup_stage = 0;
1864
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001865 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001866 spin_unlock(&dum->lock);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001867 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001868 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 goto restart;
1871 }
1872
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001873 if (list_empty(&dum_hcd->urbp_list)) {
1874 usb_put_dev(dum_hcd->udev);
1875 dum_hcd->udev = NULL;
1876 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern391eca92005-05-10 15:34:16 -04001877 /* want a 1 msec delay here */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001878 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
1880
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001881 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
1884/*-------------------------------------------------------------------------*/
1885
1886#define PORT_C_MASK \
Alan Sternc2db8b52005-04-29 16:30:48 -04001887 ((USB_PORT_STAT_C_CONNECTION \
1888 | USB_PORT_STAT_C_ENABLE \
1889 | USB_PORT_STAT_C_SUSPEND \
1890 | USB_PORT_STAT_C_OVERCURRENT \
1891 | USB_PORT_STAT_C_RESET) << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001893static int dummy_hub_status(struct usb_hcd *hcd, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001895 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 unsigned long flags;
Alan Stern391eca92005-05-10 15:34:16 -04001897 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001899 dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001901 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001902 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001903 goto done;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001904
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001905 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1906 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1907 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1908 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001909 }
1910
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001911 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 *buf = (1 << 1);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001913 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1914 dum_hcd->port_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 retval = 1;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001916 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001917 usb_hcd_resume_root_hub(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 }
Alan Stern391eca92005-05-10 15:34:16 -04001919done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001920 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 return retval;
1922}
1923
Sebastian Andrzej Siewior3b9c1c52012-08-19 21:54:59 +02001924/* usb 3.0 root hub device descriptor */
1925struct {
1926 struct usb_bos_descriptor bos;
1927 struct usb_ss_cap_descriptor ss_cap;
1928} __packed usb3_bos_desc = {
1929
1930 .bos = {
1931 .bLength = USB_DT_BOS_SIZE,
1932 .bDescriptorType = USB_DT_BOS,
1933 .wTotalLength = cpu_to_le16(sizeof(usb3_bos_desc)),
1934 .bNumDeviceCaps = 1,
1935 },
1936 .ss_cap = {
1937 .bLength = USB_DT_USB_SS_CAP_SIZE,
1938 .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
1939 .bDevCapabilityType = USB_SS_CAP_TYPE,
1940 .wSpeedSupported = cpu_to_le16(USB_5GBPS_OPERATION),
1941 .bFunctionalitySupport = ilog2(USB_5GBPS_OPERATION),
1942 },
1943};
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945static inline void
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001946ss_hub_descriptor(struct usb_hub_descriptor *desc)
1947{
1948 memset(desc, 0, sizeof *desc);
1949 desc->bDescriptorType = 0x2a;
1950 desc->bDescLength = 12;
1951 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1952 desc->bNbrPorts = 1;
1953 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1954 desc->u.ss.DeviceRemovable = 0xffff;
1955}
1956
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001957static inline void hub_descriptor(struct usb_hub_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001959 memset(desc, 0, sizeof *desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 desc->bDescriptorType = 0x29;
1961 desc->bDescLength = 9;
Al Virofd05e722008-04-28 07:00:16 +01001962 desc->wHubCharacteristics = cpu_to_le16(0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 desc->bNbrPorts = 1;
John Youndbe79bb2001-09-17 00:00:00 -07001964 desc->u.hs.DeviceRemovable[0] = 0xff;
1965 desc->u.hs.DeviceRemovable[1] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001968static int dummy_hub_control(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 struct usb_hcd *hcd,
1970 u16 typeReq,
1971 u16 wValue,
1972 u16 wIndex,
1973 char *buf,
1974 u16 wLength
1975) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001976 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 int retval = 0;
1978 unsigned long flags;
1979
Alan Stern541c7d42010-06-22 16:39:10 -04001980 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001981 return -ETIMEDOUT;
1982
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001983 dum_hcd = hcd_to_dummy_hcd(hcd);
1984
1985 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 switch (typeReq) {
1987 case ClearHubFeature:
1988 break;
1989 case ClearPortFeature:
1990 switch (wValue) {
1991 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001992 if (hcd->speed == HCD_USB3) {
1993 dev_dbg(dummy_dev(dum_hcd),
1994 "USB_PORT_FEAT_SUSPEND req not "
1995 "supported for USB 3.0 roothub\n");
1996 goto error;
1997 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001998 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 /* 20msec resume signaling */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002000 dum_hcd->resuming = 1;
2001 dum_hcd->re_timeout = jiffies +
Alan Sternf1c39fa2005-05-03 16:24:04 -04002002 msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 }
2004 break;
2005 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002006 if (hcd->speed == HCD_USB3) {
2007 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
2008 dev_dbg(dummy_dev(dum_hcd),
2009 "power-off\n");
2010 } else
2011 if (dum_hcd->port_status &
2012 USB_SS_PORT_STAT_POWER)
2013 dev_dbg(dummy_dev(dum_hcd),
2014 "power-off\n");
Alan Sternf1c39fa2005-05-03 16:24:04 -04002015 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002017 dum_hcd->port_status &= ~(1 << wValue);
2018 set_link_state(dum_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
2020 break;
2021 case GetHubDescriptor:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002022 if (hcd->speed == HCD_USB3 &&
2023 (wLength < USB_DT_SS_HUB_SIZE ||
2024 wValue != (USB_DT_SS_HUB << 8))) {
2025 dev_dbg(dummy_dev(dum_hcd),
2026 "Wrong hub descriptor type for "
2027 "USB 3.0 roothub.\n");
2028 goto error;
2029 }
2030 if (hcd->speed == HCD_USB3)
2031 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
2032 else
2033 hub_descriptor((struct usb_hub_descriptor *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 break;
Sebastian Andrzej Siewior3b9c1c52012-08-19 21:54:59 +02002035
2036 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
2037 if (hcd->speed != HCD_USB3)
2038 goto error;
2039
2040 if ((wValue >> 8) != USB_DT_BOS)
2041 goto error;
2042
2043 memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc));
2044 retval = sizeof(usb3_bos_desc);
2045 break;
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 case GetHubStatus:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002048 *(__le32 *) buf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 break;
2050 case GetPortStatus:
2051 if (wIndex != 1)
2052 retval = -EPIPE;
2053
2054 /* whoever resets or resumes must GetPortStatus to
2055 * complete it!!
2056 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002057 if (dum_hcd->resuming &&
2058 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2059 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
2060 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002062 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
2063 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2064 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
2065 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
2066 if (dum_hcd->dum->pullup) {
2067 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002068
2069 if (hcd->speed < HCD_USB3) {
2070 switch (dum_hcd->dum->gadget.speed) {
2071 case USB_SPEED_HIGH:
2072 dum_hcd->port_status |=
2073 USB_PORT_STAT_HIGH_SPEED;
2074 break;
2075 case USB_SPEED_LOW:
2076 dum_hcd->dum->gadget.ep0->
2077 maxpacket = 8;
2078 dum_hcd->port_status |=
2079 USB_PORT_STAT_LOW_SPEED;
2080 break;
2081 default:
2082 dum_hcd->dum->gadget.speed =
2083 USB_SPEED_FULL;
2084 break;
2085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
2087 }
2088 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002089 set_link_state(dum_hcd);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002090 ((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status);
2091 ((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 break;
2093 case SetHubFeature:
2094 retval = -EPIPE;
2095 break;
2096 case SetPortFeature:
2097 switch (wValue) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002098 case USB_PORT_FEAT_LINK_STATE:
2099 if (hcd->speed != HCD_USB3) {
2100 dev_dbg(dummy_dev(dum_hcd),
2101 "USB_PORT_FEAT_LINK_STATE req not "
2102 "supported for USB 2.0 roothub\n");
2103 goto error;
2104 }
2105 /*
2106 * Since this is dummy we don't have an actual link so
2107 * there is nothing to do for the SET_LINK_STATE cmd
2108 */
2109 break;
2110 case USB_PORT_FEAT_U1_TIMEOUT:
2111 case USB_PORT_FEAT_U2_TIMEOUT:
2112 /* TODO: add suspend/resume support! */
2113 if (hcd->speed != HCD_USB3) {
2114 dev_dbg(dummy_dev(dum_hcd),
2115 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
2116 "supported for USB 2.0 roothub\n");
2117 goto error;
2118 }
2119 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002121 /* Applicable only for USB2.0 hub */
2122 if (hcd->speed == HCD_USB3) {
2123 dev_dbg(dummy_dev(dum_hcd),
2124 "USB_PORT_FEAT_SUSPEND req not "
2125 "supported for USB 3.0 roothub\n");
2126 goto error;
2127 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002128 if (dum_hcd->active) {
2129 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
Alan Sternf1c39fa2005-05-03 16:24:04 -04002130
2131 /* HNP would happen here; for now we
2132 * assume b_bus_req is always true.
2133 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002134 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002135 if (((1 << USB_DEVICE_B_HNP_ENABLE)
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002136 & dum_hcd->dum->devstatus) != 0)
2137 dev_dbg(dummy_dev(dum_hcd),
Alan Stern5742b0c2005-05-02 11:25:17 -04002138 "no HNP yet!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
2140 break;
Alan Sternf1c39fa2005-05-03 16:24:04 -04002141 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002142 if (hcd->speed == HCD_USB3)
2143 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
2144 else
2145 dum_hcd->port_status |= USB_PORT_STAT_POWER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002146 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002147 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002148 case USB_PORT_FEAT_BH_PORT_RESET:
2149 /* Applicable only for USB3.0 hub */
2150 if (hcd->speed != HCD_USB3) {
2151 dev_dbg(dummy_dev(dum_hcd),
2152 "USB_PORT_FEAT_BH_PORT_RESET req not "
2153 "supported for USB 2.0 roothub\n");
2154 goto error;
2155 }
2156 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 case USB_PORT_FEAT_RESET:
Alan Sternf1c39fa2005-05-03 16:24:04 -04002158 /* if it's already enabled, disable */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002159 if (hcd->speed == HCD_USB3) {
2160 dum_hcd->port_status = 0;
2161 dum_hcd->port_status =
2162 (USB_SS_PORT_STAT_POWER |
2163 USB_PORT_STAT_CONNECTION |
2164 USB_PORT_STAT_RESET);
2165 } else
2166 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
Alan Sternf1c39fa2005-05-03 16:24:04 -04002167 | USB_PORT_STAT_LOW_SPEED
2168 | USB_PORT_STAT_HIGH_SPEED);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002169 /*
2170 * We want to reset device status. All but the
2171 * Self powered feature
2172 */
2173 dum_hcd->dum->devstatus &=
2174 (1 << USB_DEVICE_SELF_POWERED);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002175 /*
2176 * FIXME USB3.0: what is the correct reset signaling
2177 * interval? Is it still 50msec as for HS?
2178 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002179 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002180 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 default:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002182 if (hcd->speed == HCD_USB3) {
2183 if ((dum_hcd->port_status &
2184 USB_SS_PORT_STAT_POWER) != 0) {
2185 dum_hcd->port_status |= (1 << wValue);
2186 set_link_state(dum_hcd);
2187 }
2188 } else
2189 if ((dum_hcd->port_status &
2190 USB_PORT_STAT_POWER) != 0) {
2191 dum_hcd->port_status |= (1 << wValue);
2192 set_link_state(dum_hcd);
2193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
2195 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002196 case GetPortErrorCount:
2197 if (hcd->speed != HCD_USB3) {
2198 dev_dbg(dummy_dev(dum_hcd),
2199 "GetPortErrorCount req not "
2200 "supported for USB 2.0 roothub\n");
2201 goto error;
2202 }
2203 /* We'll always return 0 since this is a dummy hub */
2204 *(__le32 *) buf = cpu_to_le32(0);
2205 break;
2206 case SetHubDepth:
2207 if (hcd->speed != HCD_USB3) {
2208 dev_dbg(dummy_dev(dum_hcd),
2209 "SetHubDepth req not supported for "
2210 "USB 2.0 roothub\n");
2211 goto error;
2212 }
2213 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002215 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 "hub control req%04x v%04x i%04x l%d\n",
2217 typeReq, wValue, wIndex, wLength);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002218error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 /* "protocol stall" on error */
2220 retval = -EPIPE;
2221 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002222 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Stern685eb932005-05-03 16:27:26 -04002223
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002224 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002225 usb_hcd_poll_rh_status(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 return retval;
2227}
2228
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002229static int dummy_bus_suspend(struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002230{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002231 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002232
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002233 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern3cf0a222005-11-29 12:08:15 -05002234
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002235 spin_lock_irq(&dum_hcd->dum->lock);
2236 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2237 set_link_state(dum_hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002238 hcd->state = HC_STATE_SUSPENDED;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002239 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern391eca92005-05-10 15:34:16 -04002240 return 0;
2241}
2242
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002243static int dummy_bus_resume(struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002244{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002245 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002246 int rc = 0;
2247
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002248 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002249
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002250 spin_lock_irq(&dum_hcd->dum->lock);
Alan Stern541c7d42010-06-22 16:39:10 -04002251 if (!HCD_HW_ACCESSIBLE(hcd)) {
Alan Sterncfa59da2007-06-21 16:25:35 -04002252 rc = -ESHUTDOWN;
Alan Stern3cf0a222005-11-29 12:08:15 -05002253 } else {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002254 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2255 set_link_state(dum_hcd);
2256 if (!list_empty(&dum_hcd->urbp_list))
2257 mod_timer(&dum_hcd->timer, jiffies);
Alan Stern3cf0a222005-11-29 12:08:15 -05002258 hcd->state = HC_STATE_RUNNING;
2259 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002260 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern3cf0a222005-11-29 12:08:15 -05002261 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002262}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264/*-------------------------------------------------------------------------*/
2265
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002266static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002268 int ep = usb_pipeendpoint(urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002270 return snprintf(buf, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 "urb/%p %s ep%d%s%s len %d/%d\n",
2272 urb,
2273 ({ char *s;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002274 switch (urb->dev->speed) {
2275 case USB_SPEED_LOW:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002276 s = "ls";
2277 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002278 case USB_SPEED_FULL:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002279 s = "fs";
2280 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002281 case USB_SPEED_HIGH:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002282 s = "hs";
2283 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002284 case USB_SPEED_SUPER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002285 s = "ss";
2286 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002287 default:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002288 s = "?";
2289 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 }; s; }),
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002291 ep, ep ? (usb_pipein(urb->pipe) ? "in" : "out") : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 ({ char *s; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002293 switch (usb_pipetype(urb->pipe)) { \
2294 case PIPE_CONTROL: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002295 s = ""; \
2296 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002297 case PIPE_BULK: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002298 s = "-bulk"; \
2299 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002300 case PIPE_INTERRUPT: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002301 s = "-int"; \
2302 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002303 default: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002304 s = "-iso"; \
2305 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002306 }; s; }),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 urb->actual_length, urb->transfer_buffer_length);
2308}
2309
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002310static ssize_t show_urbs(struct device *dev, struct device_attribute *attr,
2311 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002313 struct usb_hcd *hcd = dev_get_drvdata(dev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002314 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 struct urbp *urbp;
2316 size_t size = 0;
2317 unsigned long flags;
2318
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002319 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2320 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 size_t temp;
2322
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002323 temp = show_urb(buf, PAGE_SIZE - size, urbp->urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 buf += temp;
2325 size += temp;
2326 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002327 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329 return size;
2330}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002331static DEVICE_ATTR(urbs, S_IRUGO, show_urbs, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002333static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2334{
2335 init_timer(&dum_hcd->timer);
2336 dum_hcd->timer.function = dummy_timer;
2337 dum_hcd->timer.data = (unsigned long)dum_hcd;
2338 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002339 dum_hcd->stream_en_ep = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002340 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2341 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2342 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2343 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2344#ifdef CONFIG_USB_OTG
2345 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2346#endif
2347 return 0;
2348
2349 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2350 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2351}
2352
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002353static int dummy_start(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002355 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
2357 /*
2358 * MASTER side init ... we emulate a root hub that'll only ever
2359 * talk to one device (the slave side). Also appears in sysfs,
2360 * just like more familiar pci-based HCDs.
2361 */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002362 if (!usb_hcd_is_primary_hcd(hcd))
2363 return dummy_start_ss(dum_hcd);
2364
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002365 spin_lock_init(&dum_hcd->dum->lock);
2366 init_timer(&dum_hcd->timer);
2367 dum_hcd->timer.function = dummy_timer;
2368 dum_hcd->timer.data = (unsigned long)dum_hcd;
2369 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002371 INIT_LIST_HEAD(&dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Alan Sterncaf29f62007-12-06 11:10:39 -05002373 hcd->power_budget = POWER_BUDGET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 hcd->state = HC_STATE_RUNNING;
Alan Stern685eb932005-05-03 16:27:26 -04002375 hcd->uses_new_polling = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Alan Stern5742b0c2005-05-02 11:25:17 -04002377#ifdef CONFIG_USB_OTG
2378 hcd->self.otg_port = 1;
2379#endif
2380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002382 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002385static void dummy_stop(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386{
2387 struct dummy *dum;
2388
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002389 dum = hcd_to_dummy_hcd(hcd)->dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002390 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2391 usb_gadget_unregister_driver(dum->driver);
2392 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393}
2394
2395/*-------------------------------------------------------------------------*/
2396
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002397static int dummy_h_get_frame(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002399 return dummy_g_get_frame(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400}
2401
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002402static int dummy_setup(struct usb_hcd *hcd)
2403{
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002404 struct dummy *dum;
2405
2406 dum = *((void **)dev_get_platdata(hcd->self.controller));
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01002407 hcd->self.sg_tablesize = ~0;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002408 if (usb_hcd_is_primary_hcd(hcd)) {
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002409 dum->hs_hcd = hcd_to_dummy_hcd(hcd);
2410 dum->hs_hcd->dum = dum;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002411 /*
2412 * Mark the first roothub as being USB 2.0.
2413 * The USB 3.0 roothub will be registered later by
2414 * dummy_hcd_probe()
2415 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002416 hcd->speed = HCD_USB2;
2417 hcd->self.root_hub->speed = USB_SPEED_HIGH;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002418 } else {
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002419 dum->ss_hcd = hcd_to_dummy_hcd(hcd);
2420 dum->ss_hcd->dum = dum;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002421 hcd->speed = HCD_USB3;
2422 hcd->self.root_hub->speed = USB_SPEED_SUPER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002423 }
2424 return 0;
2425}
2426
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002427/* Change a group of bulk endpoints to support multiple stream IDs */
Sebastian Andrzej Siewiord81f3e42012-01-09 13:15:00 +01002428static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002429 struct usb_host_endpoint **eps, unsigned int num_eps,
2430 unsigned int num_streams, gfp_t mem_flags)
2431{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002432 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2433 unsigned long flags;
2434 int max_stream;
2435 int ret_streams = num_streams;
2436 unsigned int index;
2437 unsigned int i;
2438
2439 if (!num_eps)
2440 return -EINVAL;
2441
2442 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2443 for (i = 0; i < num_eps; i++) {
2444 index = dummy_get_ep_idx(&eps[i]->desc);
2445 if ((1 << index) & dum_hcd->stream_en_ep) {
2446 ret_streams = -EINVAL;
2447 goto out;
2448 }
2449 max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp);
2450 if (!max_stream) {
2451 ret_streams = -EINVAL;
2452 goto out;
2453 }
2454 if (max_stream < ret_streams) {
2455 dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u "
2456 "stream IDs.\n",
2457 eps[i]->desc.bEndpointAddress,
2458 max_stream);
2459 ret_streams = max_stream;
2460 }
2461 }
2462
2463 for (i = 0; i < num_eps; i++) {
2464 index = dummy_get_ep_idx(&eps[i]->desc);
2465 dum_hcd->stream_en_ep |= 1 << index;
2466 set_max_streams_for_pipe(dum_hcd,
2467 usb_endpoint_num(&eps[i]->desc), ret_streams);
2468 }
2469out:
2470 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2471 return ret_streams;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002472}
2473
2474/* Reverts a group of bulk endpoints back to not using stream IDs. */
Sebastian Andrzej Siewiord81f3e42012-01-09 13:15:00 +01002475static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002476 struct usb_host_endpoint **eps, unsigned int num_eps,
2477 gfp_t mem_flags)
2478{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002479 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2480 unsigned long flags;
2481 int ret;
2482 unsigned int index;
2483 unsigned int i;
2484
2485 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2486 for (i = 0; i < num_eps; i++) {
2487 index = dummy_get_ep_idx(&eps[i]->desc);
2488 if (!((1 << index) & dum_hcd->stream_en_ep)) {
2489 ret = -EINVAL;
2490 goto out;
2491 }
2492 }
2493
2494 for (i = 0; i < num_eps; i++) {
2495 index = dummy_get_ep_idx(&eps[i]->desc);
2496 dum_hcd->stream_en_ep &= ~(1 << index);
2497 set_max_streams_for_pipe(dum_hcd,
2498 usb_endpoint_num(&eps[i]->desc), 0);
2499 }
2500 ret = 0;
2501out:
2502 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2503 return ret;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002504}
2505
2506static struct hc_driver dummy_hcd = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 .description = (char *) driver_name,
2508 .product_desc = "Dummy host controller",
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002509 .hcd_priv_size = sizeof(struct dummy_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002511 .flags = HCD_USB3 | HCD_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002513 .reset = dummy_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 .start = dummy_start,
2515 .stop = dummy_stop,
2516
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002517 .urb_enqueue = dummy_urb_enqueue,
2518 .urb_dequeue = dummy_urb_dequeue,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002520 .get_frame_number = dummy_h_get_frame,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002522 .hub_status_data = dummy_hub_status,
2523 .hub_control = dummy_hub_control,
Alan Stern0c0382e2005-10-13 17:08:02 -04002524 .bus_suspend = dummy_bus_suspend,
2525 .bus_resume = dummy_bus_resume,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002526
2527 .alloc_streams = dummy_alloc_streams,
2528 .free_streams = dummy_free_streams,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529};
2530
Alan Stern8364d6b2005-11-14 12:16:30 -05002531static int dummy_hcd_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002533 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002534 struct usb_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002535 struct usb_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 int retval;
2537
Alan Stern8364d6b2005-11-14 12:16:30 -05002538 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002539 dum = *((void **)dev_get_platdata(&pdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002541 if (!mod_data.is_super_speed)
2542 dummy_hcd.flags = HCD_USB2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002543 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2544 if (!hs_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 return -ENOMEM;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002546 hs_hcd->has_tt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002548 retval = usb_add_hcd(hs_hcd, 0, 0);
Sebastian Andrzej Siewior1b68a4c2012-08-19 21:54:58 +02002549 if (retval)
2550 goto put_usb2_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002551
2552 if (mod_data.is_super_speed) {
2553 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2554 dev_name(&pdev->dev), hs_hcd);
2555 if (!ss_hcd) {
2556 retval = -ENOMEM;
2557 goto dealloc_usb2_hcd;
2558 }
2559
2560 retval = usb_add_hcd(ss_hcd, 0, 0);
2561 if (retval)
2562 goto put_usb3_hcd;
2563 }
2564 return 0;
2565
2566put_usb3_hcd:
2567 usb_put_hcd(ss_hcd);
2568dealloc_usb2_hcd:
Sebastian Andrzej Siewior1b68a4c2012-08-19 21:54:58 +02002569 usb_remove_hcd(hs_hcd);
2570put_usb2_hcd:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002571 usb_put_hcd(hs_hcd);
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002572 dum->hs_hcd = dum->ss_hcd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 return retval;
2574}
2575
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002576static int dummy_hcd_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002578 struct dummy *dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002580 dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002581
2582 if (dum->ss_hcd) {
2583 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2584 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2585 }
2586
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002587 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2588 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002589
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002590 dum->hs_hcd = NULL;
2591 dum->ss_hcd = NULL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002592
Alan Sternd9b76252005-05-03 16:15:43 -04002593 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594}
2595
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002596static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state)
Alan Stern391eca92005-05-10 15:34:16 -04002597{
2598 struct usb_hcd *hcd;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002599 struct dummy_hcd *dum_hcd;
Alan Stern3cf0a222005-11-29 12:08:15 -05002600 int rc = 0;
Alan Stern391eca92005-05-10 15:34:16 -04002601
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002602 dev_dbg(&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002603
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002604 hcd = platform_get_drvdata(pdev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002605 dum_hcd = hcd_to_dummy_hcd(hcd);
2606 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern3cf0a222005-11-29 12:08:15 -05002607 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2608 rc = -EBUSY;
2609 } else
2610 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2611 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002612}
2613
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002614static int dummy_hcd_resume(struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04002615{
2616 struct usb_hcd *hcd;
2617
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002618 dev_dbg(&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002619
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002620 hcd = platform_get_drvdata(pdev);
Alan Stern3cf0a222005-11-29 12:08:15 -05002621 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002622 usb_hcd_poll_rh_status(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002623 return 0;
2624}
2625
Russell King3ae5eae2005-11-09 22:32:44 +00002626static struct platform_driver dummy_hcd_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04002627 .probe = dummy_hcd_probe,
2628 .remove = dummy_hcd_remove,
Alan Stern391eca92005-05-10 15:34:16 -04002629 .suspend = dummy_hcd_suspend,
2630 .resume = dummy_hcd_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002631 .driver = {
2632 .name = (char *) driver_name,
2633 .owner = THIS_MODULE,
2634 },
Alan Sternd9b76252005-05-03 16:15:43 -04002635};
2636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002638#define MAX_NUM_UDC 2
Sebastian Andrzej Siewiorb2113132012-10-30 12:53:17 +01002639static struct platform_device *the_udc_pdev[MAX_NUM_UDC];
2640static struct platform_device *the_hcd_pdev[MAX_NUM_UDC];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002642static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002644 int retval = -ENOMEM;
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002645 int i;
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002646 struct dummy *dum[MAX_NUM_UDC];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002648 if (usb_disabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 return -ENODEV;
Alan Sternd9b76252005-05-03 16:15:43 -04002650
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +03002651 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2652 return -EINVAL;
2653
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002654 if (mod_data.num < 1 || mod_data.num > MAX_NUM_UDC) {
2655 pr_err("Number of emulated UDC must be in range of 1…%d\n",
2656 MAX_NUM_UDC);
2657 return -EINVAL;
2658 }
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002659
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002660 for (i = 0; i < mod_data.num; i++) {
2661 the_hcd_pdev[i] = platform_device_alloc(driver_name, i);
2662 if (!the_hcd_pdev[i]) {
2663 i--;
2664 while (i >= 0)
2665 platform_device_put(the_hcd_pdev[i--]);
2666 return retval;
2667 }
2668 }
2669 for (i = 0; i < mod_data.num; i++) {
2670 the_udc_pdev[i] = platform_device_alloc(gadget_name, i);
2671 if (!the_udc_pdev[i]) {
2672 i--;
2673 while (i >= 0)
2674 platform_device_put(the_udc_pdev[i--]);
2675 goto err_alloc_udc;
2676 }
2677 }
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002678 for (i = 0; i < mod_data.num; i++) {
2679 dum[i] = kzalloc(sizeof(struct dummy), GFP_KERNEL);
2680 if (!dum[i])
2681 goto err_add_pdata;
2682 retval = platform_device_add_data(the_hcd_pdev[i], &dum[i],
2683 sizeof(void *));
2684 if (retval)
2685 goto err_add_pdata;
2686 retval = platform_device_add_data(the_udc_pdev[i], &dum[i],
2687 sizeof(void *));
2688 if (retval)
2689 goto err_add_pdata;
2690 }
Alan Sternd9b76252005-05-03 16:15:43 -04002691
Alan Sterna89a2cd2008-04-07 15:03:25 -04002692 retval = platform_driver_register(&dummy_hcd_driver);
2693 if (retval < 0)
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002694 goto err_add_pdata;
Alan Sterna89a2cd2008-04-07 15:03:25 -04002695 retval = platform_driver_register(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002696 if (retval < 0)
2697 goto err_register_udc_driver;
2698
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002699 for (i = 0; i < mod_data.num; i++) {
2700 retval = platform_device_add(the_hcd_pdev[i]);
2701 if (retval < 0) {
2702 i--;
2703 while (i >= 0)
2704 platform_device_del(the_hcd_pdev[i--]);
2705 goto err_add_hcd;
2706 }
2707 }
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002708 for (i = 0; i < mod_data.num; i++) {
2709 if (!dum[i]->hs_hcd ||
2710 (!dum[i]->ss_hcd && mod_data.is_super_speed)) {
2711 /*
2712 * The hcd was added successfully but its probe
2713 * function failed for some reason.
2714 */
2715 retval = -EINVAL;
2716 goto err_add_udc;
2717 }
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002718 }
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002719
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002720 for (i = 0; i < mod_data.num; i++) {
2721 retval = platform_device_add(the_udc_pdev[i]);
2722 if (retval < 0) {
2723 i--;
2724 while (i >= 0)
2725 platform_device_del(the_udc_pdev[i]);
2726 goto err_add_udc;
2727 }
2728 }
2729
2730 for (i = 0; i < mod_data.num; i++) {
2731 if (!platform_get_drvdata(the_udc_pdev[i])) {
2732 /*
2733 * The udc was added successfully but its probe
2734 * function failed for some reason.
2735 */
2736 retval = -EINVAL;
2737 goto err_probe_udc;
2738 }
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002739 }
Alan Sternd9b76252005-05-03 16:15:43 -04002740 return retval;
2741
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002742err_probe_udc:
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002743 for (i = 0; i < mod_data.num; i++)
2744 platform_device_del(the_udc_pdev[i]);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002745err_add_udc:
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002746 for (i = 0; i < mod_data.num; i++)
2747 platform_device_del(the_hcd_pdev[i]);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002748err_add_hcd:
2749 platform_driver_unregister(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002750err_register_udc_driver:
Alan Sterna89a2cd2008-04-07 15:03:25 -04002751 platform_driver_unregister(&dummy_hcd_driver);
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002752err_add_pdata:
2753 for (i = 0; i < mod_data.num; i++)
2754 kfree(dum[i]);
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002755 for (i = 0; i < mod_data.num; i++)
2756 platform_device_put(the_udc_pdev[i]);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002757err_alloc_udc:
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002758 for (i = 0; i < mod_data.num; i++)
2759 platform_device_put(the_hcd_pdev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 return retval;
2761}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002762module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002764static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002766 int i;
2767
2768 for (i = 0; i < mod_data.num; i++) {
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002769 struct dummy *dum;
2770
2771 dum = *((void **)dev_get_platdata(&the_udc_pdev[i]->dev));
2772
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002773 platform_device_unregister(the_udc_pdev[i]);
2774 platform_device_unregister(the_hcd_pdev[i]);
Sebastian Andrzej Siewiorb100a2f2012-10-29 18:09:56 +01002775 kfree(dum);
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002776 }
Alan Sterna89a2cd2008-04-07 15:03:25 -04002777 platform_driver_unregister(&dummy_udc_driver);
2778 platform_driver_unregister(&dummy_hcd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002780module_exit(cleanup);