blob: 70f70ea133dd575f6b28a338a3dab120cbf02daf [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
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300245static struct dummy the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247/*-------------------------------------------------------------------------*/
248
Alan Sternf1c39fa2005-05-03 16:24:04 -0400249/* SLAVE/GADGET SIDE UTILITY ROUTINES */
250
251/* called with spinlock held */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100252static void nuke(struct dummy *dum, struct dummy_ep *ep)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400253{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100254 while (!list_empty(&ep->queue)) {
Alan Sternf1c39fa2005-05-03 16:24:04 -0400255 struct dummy_request *req;
256
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100257 req = list_entry(ep->queue.next, struct dummy_request, queue);
258 list_del_init(&req->queue);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400259 req->req.status = -ESHUTDOWN;
260
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100261 spin_unlock(&dum->lock);
262 req->req.complete(&ep->ep, &req->req);
263 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400264 }
265}
266
267/* caller must hold lock */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100268static void stop_activity(struct dummy *dum)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400269{
270 struct dummy_ep *ep;
271
272 /* prevent any more requests */
273 dum->address = 0;
274
275 /* The timer is left running so that outstanding URBs can fail */
276
277 /* nuke any pending requests first, so driver i/o is quiesced */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100278 list_for_each_entry(ep, &dum->gadget.ep_list, ep.ep_list)
279 nuke(dum, ep);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400280
281 /* driver now does any non-usb quiescing necessary */
282}
283
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300284/**
285 * set_link_state_by_speed() - Sets the current state of the link according to
286 * the hcd speed
287 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
288 *
289 * This function updates the port_status according to the link state and the
290 * speed of the hcd.
291 */
292static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
293{
294 struct dummy *dum = dum_hcd->dum;
295
296 if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
297 if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
298 dum_hcd->port_status = 0;
299 } else if (!dum->pullup || dum->udc_suspended) {
300 /* UDC suspend must cause a disconnect */
301 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
302 USB_PORT_STAT_ENABLE);
303 if ((dum_hcd->old_status &
304 USB_PORT_STAT_CONNECTION) != 0)
305 dum_hcd->port_status |=
306 (USB_PORT_STAT_C_CONNECTION << 16);
307 } else {
308 /* device is connected and not suspended */
309 dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
310 USB_PORT_STAT_SPEED_5GBPS) ;
311 if ((dum_hcd->old_status &
312 USB_PORT_STAT_CONNECTION) == 0)
313 dum_hcd->port_status |=
314 (USB_PORT_STAT_C_CONNECTION << 16);
315 if ((dum_hcd->port_status &
316 USB_PORT_STAT_ENABLE) == 1 &&
317 (dum_hcd->port_status &
318 USB_SS_PORT_LS_U0) == 1 &&
319 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
320 dum_hcd->active = 1;
321 }
322 } else {
323 if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
324 dum_hcd->port_status = 0;
325 } else if (!dum->pullup || dum->udc_suspended) {
326 /* UDC suspend must cause a disconnect */
327 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
328 USB_PORT_STAT_ENABLE |
329 USB_PORT_STAT_LOW_SPEED |
330 USB_PORT_STAT_HIGH_SPEED |
331 USB_PORT_STAT_SUSPEND);
332 if ((dum_hcd->old_status &
333 USB_PORT_STAT_CONNECTION) != 0)
334 dum_hcd->port_status |=
335 (USB_PORT_STAT_C_CONNECTION << 16);
336 } else {
337 dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
338 if ((dum_hcd->old_status &
339 USB_PORT_STAT_CONNECTION) == 0)
340 dum_hcd->port_status |=
341 (USB_PORT_STAT_C_CONNECTION << 16);
342 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
343 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
344 else if ((dum_hcd->port_status &
345 USB_PORT_STAT_SUSPEND) == 0 &&
346 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
347 dum_hcd->active = 1;
348 }
349 }
350}
351
Alan Sternf1c39fa2005-05-03 16:24:04 -0400352/* caller must hold lock */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300353static void set_link_state(struct dummy_hcd *dum_hcd)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400354{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300355 struct dummy *dum = dum_hcd->dum;
356
357 dum_hcd->active = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300358 if (dum->pullup)
359 if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
360 dum->gadget.speed != USB_SPEED_SUPER) ||
361 (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
362 dum->gadget.speed == USB_SPEED_SUPER))
363 return;
Alan Stern391eca92005-05-10 15:34:16 -0400364
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300365 set_link_state_by_speed(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400366
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300367 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
368 dum_hcd->active)
369 dum_hcd->resuming = 0;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400370
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300371 /* if !connected or reset */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300372 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
373 (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300374 /*
375 * We're connected and not reset (reset occurred now),
376 * and driver attached - disconnect!
377 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300378 if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300379 (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
380 dum->driver) {
381 stop_activity(dum);
382 spin_unlock(&dum->lock);
383 dum->driver->disconnect(&dum->gadget);
384 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400385 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300386 } else if (dum_hcd->active != dum_hcd->old_active) {
387 if (dum_hcd->old_active && dum->driver->suspend) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300388 spin_unlock(&dum->lock);
389 dum->driver->suspend(&dum->gadget);
390 spin_lock(&dum->lock);
391 } else if (!dum_hcd->old_active && dum->driver->resume) {
392 spin_unlock(&dum->lock);
393 dum->driver->resume(&dum->gadget);
394 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400395 }
396 }
397
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300398 dum_hcd->old_status = dum_hcd->port_status;
399 dum_hcd->old_active = dum_hcd->active;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400400}
401
402/*-------------------------------------------------------------------------*/
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404/* SLAVE/GADGET SIDE DRIVER
405 *
406 * This only tracks gadget state. All the work is done when the host
407 * side tries some (emulated) i/o operation. Real device controller
408 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
409 */
410
411#define is_enabled(dum) \
412 (dum->port_status & USB_PORT_STAT_ENABLE)
413
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100414static int dummy_enable(struct usb_ep *_ep,
415 const struct usb_endpoint_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300418 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 struct dummy_ep *ep;
420 unsigned max;
421 int retval;
422
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100423 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (!_ep || !desc || ep->desc || _ep->name == ep0name
425 || desc->bDescriptorType != USB_DT_ENDPOINT)
426 return -EINVAL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100427 dum = ep_to_dummy(ep);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300428 if (!dum->driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return -ESHUTDOWN;
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200430
431 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300432 if (!is_enabled(dum_hcd))
433 return -ESHUTDOWN;
434
435 /*
436 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
437 * maximum packet size.
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300438 * For SS devices the wMaxPacketSize is limited by 1024.
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300439 */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700440 max = usb_endpoint_maxp(desc) & 0x7ff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 /* drivers must not request bad settings, since lower levels
443 * (hardware or its drivers) may not check. some endpoints
444 * can't do iso, many have maxpacket limitations, etc.
445 *
446 * since this "hardware" driver is here to help debugging, we
447 * have some extra sanity checks. (there could be more though,
448 * especially for "ep9out" style fixed function ones.)
449 */
450 retval = -EINVAL;
Sebastian Andrzej Siewior59f08e62012-01-12 12:53:14 +0100451 switch (usb_endpoint_type(desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 case USB_ENDPOINT_XFER_BULK:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100453 if (strstr(ep->ep.name, "-iso")
454 || strstr(ep->ep.name, "-int")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 goto done;
456 }
457 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300458 case USB_SPEED_SUPER:
459 if (max == 1024)
460 break;
461 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 case USB_SPEED_HIGH:
463 if (max == 512)
464 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700465 goto done;
466 case USB_SPEED_FULL:
467 if (max == 8 || max == 16 || max == 32 || max == 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /* we'll fake any legal size */
469 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700470 /* save a return statement */
471 default:
472 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474 break;
475 case USB_ENDPOINT_XFER_INT:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100476 if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 goto done;
478 /* real hardware might not handle all packet sizes */
479 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300480 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 case USB_SPEED_HIGH:
482 if (max <= 1024)
483 break;
484 /* save a return statement */
485 case USB_SPEED_FULL:
486 if (max <= 64)
487 break;
488 /* save a return statement */
489 default:
490 if (max <= 8)
491 break;
492 goto done;
493 }
494 break;
495 case USB_ENDPOINT_XFER_ISOC:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100496 if (strstr(ep->ep.name, "-bulk")
497 || strstr(ep->ep.name, "-int"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 goto done;
499 /* real hardware might not handle all packet sizes */
500 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300501 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 case USB_SPEED_HIGH:
503 if (max <= 1024)
504 break;
505 /* save a return statement */
506 case USB_SPEED_FULL:
507 if (max <= 1023)
508 break;
509 /* save a return statement */
510 default:
511 goto done;
512 }
513 break;
514 default:
515 /* few chips support control except on ep0 */
516 goto done;
517 }
518
519 _ep->maxpacket = max;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100520 if (usb_ss_max_streams(_ep->comp_desc)) {
521 if (!usb_endpoint_xfer_bulk(desc)) {
522 dev_err(udc_dev(dum), "Can't enable stream support on "
523 "non-bulk ep %s\n", _ep->name);
524 return -EINVAL;
525 }
526 ep->stream_en = 1;
527 }
Sebastian Andrzej Siewior3cf0ad02012-01-25 11:51:19 +0100528 ep->desc = desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100530 dev_dbg(udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 _ep->name,
532 desc->bEndpointAddress & 0x0f,
533 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
534 ({ char *val;
Sebastian Andrzej Siewior59f08e62012-01-12 12:53:14 +0100535 switch (usb_endpoint_type(desc)) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +0300536 case USB_ENDPOINT_XFER_BULK:
537 val = "bulk";
538 break;
539 case USB_ENDPOINT_XFER_ISOC:
540 val = "iso";
541 break;
542 case USB_ENDPOINT_XFER_INT:
543 val = "intr";
544 break;
545 default:
546 val = "ctrl";
547 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }; val; }),
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100549 max, ep->stream_en ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /* at this point real hardware should be NAKing transfers
552 * to that endpoint, until a buffer is queued to it.
553 */
Alan Stern851a5262008-08-14 15:48:30 -0400554 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 retval = 0;
556done:
557 return retval;
558}
559
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100560static int dummy_disable(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 struct dummy_ep *ep;
563 struct dummy *dum;
564 unsigned long flags;
565 int retval;
566
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100567 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!_ep || !ep->desc || _ep->name == ep0name)
569 return -EINVAL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100570 dum = ep_to_dummy(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100572 spin_lock_irqsave(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 ep->desc = NULL;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100574 ep->stream_en = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 retval = 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100576 nuke(dum, ep);
577 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100579 dev_dbg(udc_dev(dum), "disabled %s\n", _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return retval;
581}
582
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100583static struct usb_request *dummy_alloc_request(struct usb_ep *_ep,
584 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 struct dummy_ep *ep;
587 struct dummy_request *req;
588
589 if (!_ep)
590 return NULL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100591 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800593 req = kzalloc(sizeof(*req), mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (!req)
595 return NULL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100596 INIT_LIST_HEAD(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return &req->req;
598}
599
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100600static void dummy_free_request(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 struct dummy_request *req;
603
Sebastian Andrzej Siewiorf99987b2012-02-09 09:24:59 +0100604 if (!_ep || !_req) {
Sasha Levin72688dc2012-05-11 06:39:37 +0200605 WARN_ON(1);
Sebastian Andrzej Siewior20edfbb2012-01-25 15:18:58 +0100606 return;
Sebastian Andrzej Siewiorf99987b2012-02-09 09:24:59 +0100607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100609 req = usb_request_to_dummy_request(_req);
610 WARN_ON(!list_empty(&req->queue));
611 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100614static void fifo_complete(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616}
617
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100618static int dummy_queue(struct usb_ep *_ep, struct usb_request *_req,
Al Viro55016f12005-10-21 03:21:58 -0400619 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 struct dummy_ep *ep;
622 struct dummy_request *req;
623 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300624 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 unsigned long flags;
626
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100627 req = usb_request_to_dummy_request(_req);
628 if (!_req || !list_empty(&req->queue) || !_req->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return -EINVAL;
630
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100631 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (!_ep || (!ep->desc && _ep->name != ep0name))
633 return -EINVAL;
634
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100635 dum = ep_to_dummy(ep);
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200636 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300637 if (!dum->driver || !is_enabled(dum_hcd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return -ESHUTDOWN;
639
640#if 0
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100641 dev_dbg(udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 ep, _req, _ep->name, _req->length, _req->buf);
643#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 _req->status = -EINPROGRESS;
645 _req->actual = 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100646 spin_lock_irqsave(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 /* implement an emulated single-request FIFO */
649 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100650 list_empty(&dum->fifo_req.queue) &&
651 list_empty(&ep->queue) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 _req->length <= FIFO_SIZE) {
653 req = &dum->fifo_req;
654 req->req = *_req;
655 req->req.buf = dum->fifo_buf;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100656 memcpy(dum->fifo_buf, _req->buf, _req->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 req->req.context = dum;
658 req->req.complete = fifo_complete;
659
David Brownellc728df72008-07-26 08:06:24 -0700660 list_add_tail(&req->queue, &ep->queue);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100661 spin_unlock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 _req->actual = _req->length;
663 _req->status = 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100664 _req->complete(_ep, _req);
665 spin_lock(&dum->lock);
David Brownellc728df72008-07-26 08:06:24 -0700666 } else
667 list_add_tail(&req->queue, &ep->queue);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100668 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 /* real hardware would likely enable transfers here, in case
671 * it'd been left NAKing.
672 */
673 return 0;
674}
675
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100676static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 struct dummy_ep *ep;
679 struct dummy *dum;
680 int retval = -EINVAL;
681 unsigned long flags;
682 struct dummy_request *req = NULL;
683
684 if (!_ep || !_req)
685 return retval;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100686 ep = usb_ep_to_dummy_ep(_ep);
687 dum = ep_to_dummy(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 if (!dum->driver)
690 return -ESHUTDOWN;
691
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100692 local_irq_save(flags);
693 spin_lock(&dum->lock);
694 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (&req->req == _req) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100696 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 _req->status = -ECONNRESET;
698 retval = 0;
699 break;
700 }
701 }
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100702 spin_unlock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 if (retval == 0) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100705 dev_dbg(udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 "dequeued req %p from %s, len %d buf %p\n",
707 req, _ep->name, _req->length, _req->buf);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100708 _req->complete(_ep, _req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100710 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return retval;
712}
713
714static int
Alan Stern851a5262008-08-14 15:48:30 -0400715dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 struct dummy_ep *ep;
718 struct dummy *dum;
719
720 if (!_ep)
721 return -EINVAL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100722 ep = usb_ep_to_dummy_ep(_ep);
723 dum = ep_to_dummy(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (!dum->driver)
725 return -ESHUTDOWN;
726 if (!value)
Alan Stern851a5262008-08-14 15:48:30 -0400727 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100729 !list_empty(&ep->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return -EAGAIN;
Alan Stern851a5262008-08-14 15:48:30 -0400731 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 ep->halted = 1;
Alan Stern851a5262008-08-14 15:48:30 -0400733 if (wedged)
734 ep->wedged = 1;
735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /* FIXME clear emulated data toggle too */
737 return 0;
738}
739
Alan Stern851a5262008-08-14 15:48:30 -0400740static int
741dummy_set_halt(struct usb_ep *_ep, int value)
742{
743 return dummy_set_halt_and_wedge(_ep, value, 0);
744}
745
746static int dummy_set_wedge(struct usb_ep *_ep)
747{
748 if (!_ep || _ep->name == ep0name)
749 return -EINVAL;
750 return dummy_set_halt_and_wedge(_ep, 1, 1);
751}
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753static const struct usb_ep_ops dummy_ep_ops = {
754 .enable = dummy_enable,
755 .disable = dummy_disable,
756
757 .alloc_request = dummy_alloc_request,
758 .free_request = dummy_free_request,
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 .queue = dummy_queue,
761 .dequeue = dummy_dequeue,
762
763 .set_halt = dummy_set_halt,
Alan Stern851a5262008-08-14 15:48:30 -0400764 .set_wedge = dummy_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765};
766
767/*-------------------------------------------------------------------------*/
768
769/* there are both host and device side versions of this call ... */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100770static int dummy_g_get_frame(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 struct timeval tv;
773
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100774 do_gettimeofday(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return tv.tv_usec / 1000;
776}
777
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100778static int dummy_wakeup(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300780 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300782 dum_hcd = gadget_to_dummy_hcd(_gadget);
783 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
Alan Stern5742b0c2005-05-02 11:25:17 -0400784 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return -EINVAL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300786 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
Alan Stern391eca92005-05-10 15:34:16 -0400787 return -ENOLINK;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300788 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
789 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -0400790 return -EIO;
791
792 /* FIXME: What if the root hub is suspended but the port isn't? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 /* hub notices our request, issues downstream resume, etc */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300795 dum_hcd->resuming = 1;
796 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
797 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return 0;
799}
800
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100801static int dummy_set_selfpowered(struct usb_gadget *_gadget, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 struct dummy *dum;
804
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100805 dum = gadget_to_dummy_hcd(_gadget)->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (value)
807 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
808 else
809 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
810 return 0;
811}
812
Sebastian Andrzej Siewiord2621272012-01-09 13:14:59 +0100813static void dummy_udc_update_ep0(struct dummy *dum)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200814{
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100815 if (dum->gadget.speed == USB_SPEED_SUPER)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200816 dum->ep[0].ep.maxpacket = 9;
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100817 else
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200818 dum->ep[0].ep.maxpacket = 64;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200819}
820
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100821static int dummy_pullup(struct usb_gadget *_gadget, int value)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400822{
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200823 struct dummy_hcd *dum_hcd;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400824 struct dummy *dum;
825 unsigned long flags;
826
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200827 dum = gadget_dev_to_dummy(&_gadget->dev);
828
829 if (value && dum->driver) {
830 if (mod_data.is_super_speed)
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100831 dum->gadget.speed = dum->driver->max_speed;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200832 else if (mod_data.is_high_speed)
833 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100834 dum->driver->max_speed);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200835 else
836 dum->gadget.speed = USB_SPEED_FULL;
Sebastian Andrzej Siewiord2621272012-01-09 13:14:59 +0100837 dummy_udc_update_ep0(dum);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200838
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100839 if (dum->gadget.speed < dum->driver->max_speed)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200840 dev_dbg(udc_dev(dum), "This device can perform faster"
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100841 " if you connect it to a %s port...\n",
842 usb_speed_string(dum->driver->max_speed));
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200843 }
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200844 dum_hcd = gadget_to_dummy_hcd(_gadget);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200845
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100846 spin_lock_irqsave(&dum->lock, flags);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400847 dum->pullup = (value != 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200848 set_link_state(dum_hcd);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100849 spin_unlock_irqrestore(&dum->lock, flags);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200850
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200851 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Sternf1c39fa2005-05-03 16:24:04 -0400852 return 0;
853}
854
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200855static int dummy_udc_start(struct usb_gadget *g,
856 struct usb_gadget_driver *driver);
857static int dummy_udc_stop(struct usb_gadget *g,
858 struct usb_gadget_driver *driver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860static const struct usb_gadget_ops dummy_ops = {
861 .get_frame = dummy_g_get_frame,
862 .wakeup = dummy_wakeup,
863 .set_selfpowered = dummy_set_selfpowered,
Alan Sternf1c39fa2005-05-03 16:24:04 -0400864 .pullup = dummy_pullup,
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200865 .udc_start = dummy_udc_start,
866 .udc_stop = dummy_udc_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867};
868
869/*-------------------------------------------------------------------------*/
870
871/* "function" sysfs attribute */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100872static ssize_t show_function(struct device *dev, struct device_attribute *attr,
873 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100875 struct dummy *dum = gadget_dev_to_dummy(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 if (!dum->driver || !dum->driver->function)
878 return 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100879 return scnprintf(buf, PAGE_SIZE, "%s\n", dum->driver->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100881static DEVICE_ATTR(function, S_IRUGO, show_function, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883/*-------------------------------------------------------------------------*/
884
885/*
886 * Driver registration/unregistration.
887 *
888 * This is basically hardware-specific; there's usually only one real USB
889 * device (not host) controller since that's how USB devices are intended
890 * to work. So most implementations of these api calls will rely on the
891 * fact that only one driver will ever bind to the hardware. But curious
892 * hardware can be built with discrete components, so the gadget API doesn't
893 * require that assumption.
894 *
895 * For this emulator, it might be convenient to create a usb slave device
896 * for each driver that registers: just add to a big root hub.
897 */
898
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200899static int dummy_udc_start(struct usb_gadget *g,
900 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200902 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
903 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100905 if (driver->max_speed == USB_SPEED_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return -EINVAL;
907
908 /*
909 * SLAVE side init ... the layer above hardware, which
910 * can't enumerate without help from the driver we're binding.
911 */
Alan Stern5742b0c2005-05-02 11:25:17 -0400912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 dum->devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 dum->driver = driver;
Alan Sternd0d86072012-08-20 16:12:47 -0400916 dum->gadget.dev.driver = &driver->driver;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100917 dev_dbg(udc_dev(dum), "binding gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return 0;
920}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200922static int dummy_udc_stop(struct usb_gadget *g,
923 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200925 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
926 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100928 dev_dbg(udc_dev(dum), "unregister gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 driver->driver.name);
930
Alan Sternd0d86072012-08-20 16:12:47 -0400931 dum->gadget.dev.driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 dum->driver = NULL;
Sebastian Andrzej Siewior25427872011-06-16 20:36:55 +0200933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return 0;
935}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937#undef is_enabled
938
Alan Sternd9b76252005-05-03 16:15:43 -0400939/* The gadget structure is stored inside the hcd structure and will be
940 * released along with it. */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100941static void dummy_gadget_release(struct device *dev)
Alan Sternd9b76252005-05-03 16:15:43 -0400942{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300943 return;
Alan Sternd9b76252005-05-03 16:15:43 -0400944}
945
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200946static void init_dummy_udc_hw(struct dummy *dum)
947{
948 int i;
949
950 INIT_LIST_HEAD(&dum->gadget.ep_list);
951 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
952 struct dummy_ep *ep = &dum->ep[i];
953
954 if (!ep_name[i])
955 break;
956 ep->ep.name = ep_name[i];
957 ep->ep.ops = &dummy_ep_ops;
958 list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
959 ep->halted = ep->wedged = ep->already_seen =
960 ep->setup_stage = 0;
961 ep->ep.maxpacket = ~0;
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100962 ep->ep.max_streams = 16;
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200963 ep->last_io = jiffies;
964 ep->gadget = &dum->gadget;
965 ep->desc = NULL;
966 INIT_LIST_HEAD(&ep->queue);
967 }
968
969 dum->gadget.ep0 = &dum->ep[0].ep;
970 list_del_init(&dum->ep[0].ep.ep_list);
971 INIT_LIST_HEAD(&dum->fifo_req.queue);
Sebastian Andrzej Siewiorf8744d42011-06-23 14:26:13 +0200972
973#ifdef CONFIG_USB_OTG
974 dum->gadget.is_otg = 1;
975#endif
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200976}
977
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100978static int dummy_udc_probe(struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -0400979{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300980 struct dummy *dum = &the_controller;
Alan Sternd9b76252005-05-03 16:15:43 -0400981 int rc;
982
983 dum->gadget.name = gadget_name;
984 dum->gadget.ops = &dummy_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100985 dum->gadget.max_speed = USB_SPEED_SUPER;
Alan Sternd9b76252005-05-03 16:15:43 -0400986
Kay Sievers0031a062008-05-02 06:02:41 +0200987 dev_set_name(&dum->gadget.dev, "gadget");
Alan Stern8364d6b2005-11-14 12:16:30 -0500988 dum->gadget.dev.parent = &pdev->dev;
Alan Sternd9b76252005-05-03 16:15:43 -0400989 dum->gadget.dev.release = dummy_gadget_release;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100990 rc = device_register(&dum->gadget.dev);
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530991 if (rc < 0) {
992 put_device(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -0400993 return rc;
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530994 }
Alan Sternd9b76252005-05-03 16:15:43 -0400995
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200996 init_dummy_udc_hw(dum);
997
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300998 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
999 if (rc < 0)
1000 goto err_udc;
1001
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001002 rc = device_create_file(&dum->gadget.dev, &dev_attr_function);
Alan Sternefd54a32006-09-25 11:55:56 -04001003 if (rc < 0)
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001004 goto err_dev;
1005 platform_set_drvdata(pdev, dum);
1006 return rc;
1007
1008err_dev:
1009 usb_del_gadget_udc(&dum->gadget);
1010err_udc:
1011 device_unregister(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -04001012 return rc;
1013}
1014
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001015static int dummy_udc_remove(struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -04001016{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001017 struct dummy *dum = platform_get_drvdata(pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04001018
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001019 usb_del_gadget_udc(&dum->gadget);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001020 platform_set_drvdata(pdev, NULL);
1021 device_remove_file(&dum->gadget.dev, &dev_attr_function);
1022 device_unregister(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -04001023 return 0;
1024}
1025
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001026static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1027 int suspend)
Alan Stern391eca92005-05-10 15:34:16 -04001028{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001029 spin_lock_irq(&dum->lock);
1030 dum->udc_suspended = suspend;
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001031 set_link_state(dum_hcd);
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001032 spin_unlock_irq(&dum->lock);
1033}
Alan Stern391eca92005-05-10 15:34:16 -04001034
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001035static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1036{
1037 struct dummy *dum = platform_get_drvdata(pdev);
1038 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1039
1040 dev_dbg(&pdev->dev, "%s\n", __func__);
1041 dummy_udc_pm(dum, dum_hcd, 1);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001042 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001043 return 0;
1044}
1045
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001046static int dummy_udc_resume(struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04001047{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001048 struct dummy *dum = platform_get_drvdata(pdev);
1049 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Alan Stern391eca92005-05-10 15:34:16 -04001050
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001051 dev_dbg(&pdev->dev, "%s\n", __func__);
1052 dummy_udc_pm(dum, dum_hcd, 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001053 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001054 return 0;
1055}
1056
Russell King3ae5eae2005-11-09 22:32:44 +00001057static struct platform_driver dummy_udc_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04001058 .probe = dummy_udc_probe,
1059 .remove = dummy_udc_remove,
Alan Stern391eca92005-05-10 15:34:16 -04001060 .suspend = dummy_udc_suspend,
1061 .resume = dummy_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001062 .driver = {
1063 .name = (char *) gadget_name,
1064 .owner = THIS_MODULE,
1065 },
Alan Sternd9b76252005-05-03 16:15:43 -04001066};
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068/*-------------------------------------------------------------------------*/
1069
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001070static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc)
1071{
1072 unsigned int index;
1073
1074 index = usb_endpoint_num(desc) << 1;
1075 if (usb_endpoint_dir_in(desc))
1076 index |= 1;
1077 return index;
1078}
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080/* MASTER/HOST SIDE DRIVER
1081 *
1082 * this uses the hcd framework to hook up to host side drivers.
1083 * its root hub will only have one device, otherwise it acts like
1084 * a normal host controller.
1085 *
1086 * when urbs are queued, they're just stuck on a list that we
1087 * scan in a timer callback. that callback connects writes from
1088 * the host with reads from the device, and so on, based on the
1089 * usb 2.0 rules.
1090 */
1091
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001092static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb)
1093{
1094 const struct usb_endpoint_descriptor *desc = &urb->ep->desc;
1095 u32 index;
1096
1097 if (!usb_endpoint_xfer_bulk(desc))
1098 return 0;
1099
1100 index = dummy_get_ep_idx(desc);
1101 return (1 << index) & dum_hcd->stream_en_ep;
1102}
1103
1104/*
1105 * The max stream number is saved as a nibble so for the 30 possible endpoints
1106 * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
1107 * means we use only 1 stream). The maximum according to the spec is 16bit so
1108 * if the 16 stream limit is about to go, the array size should be incremented
1109 * to 30 elements of type u16.
1110 */
1111static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1112 unsigned int pipe)
1113{
1114 int max_streams;
1115
1116 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1117 if (usb_pipeout(pipe))
1118 max_streams >>= 4;
1119 else
1120 max_streams &= 0xf;
1121 max_streams++;
1122 return max_streams;
1123}
1124
1125static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1126 unsigned int pipe, unsigned int streams)
1127{
1128 int max_streams;
1129
1130 streams--;
1131 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1132 if (usb_pipeout(pipe)) {
1133 streams <<= 4;
1134 max_streams &= 0xf;
1135 } else {
1136 max_streams &= 0xf0;
1137 }
1138 max_streams |= streams;
1139 dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams;
1140}
1141
1142static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb)
1143{
1144 unsigned int max_streams;
1145 int enabled;
1146
1147 enabled = dummy_ep_stream_en(dum_hcd, urb);
1148 if (!urb->stream_id) {
1149 if (enabled)
1150 return -EINVAL;
1151 return 0;
1152 }
1153 if (!enabled)
1154 return -EINVAL;
1155
1156 max_streams = get_max_streams_for_pipe(dum_hcd,
1157 usb_pipeendpoint(urb->pipe));
1158 if (urb->stream_id > max_streams) {
1159 dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n",
1160 urb->stream_id);
1161 BUG();
1162 return -EINVAL;
1163 }
1164 return 0;
1165}
1166
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001167static int dummy_urb_enqueue(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 struct usb_hcd *hcd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001170 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001172 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 struct urbp *urbp;
1174 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001175 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001177 urbp = kmalloc(sizeof *urbp, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (!urbp)
1179 return -ENOMEM;
1180 urbp->urb = urb;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001181 urbp->miter_started = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001183 dum_hcd = hcd_to_dummy_hcd(hcd);
1184 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001185
1186 rc = dummy_validate_stream(dum_hcd, urb);
1187 if (rc) {
1188 kfree(urbp);
1189 goto done;
1190 }
1191
Alan Sterne9df41c2007-08-08 11:48:02 -04001192 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1193 if (rc) {
1194 kfree(urbp);
1195 goto done;
1196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001198 if (!dum_hcd->udev) {
1199 dum_hcd->udev = urb->dev;
1200 usb_get_dev(dum_hcd->udev);
1201 } else if (unlikely(dum_hcd->udev != urb->dev))
1202 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001204 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 urb->hcpriv = urbp;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001206 if (usb_pipetype(urb->pipe) == PIPE_CONTROL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 urb->error_count = 1; /* mark as a new urb */
1208
1209 /* kick the scheduler, it'll do the rest */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001210 if (!timer_pending(&dum_hcd->timer))
1211 mod_timer(&dum_hcd->timer, jiffies + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Alan Sterne9df41c2007-08-08 11:48:02 -04001213 done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001214 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001215 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
Alan Sterne9df41c2007-08-08 11:48:02 -04001218static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001220 struct dummy_hcd *dum_hcd;
Alan Stern391eca92005-05-10 15:34:16 -04001221 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001222 int rc;
Alan Stern391eca92005-05-10 15:34:16 -04001223
1224 /* giveback happens automatically in timer callback,
1225 * so make sure the callback happens */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001226 dum_hcd = hcd_to_dummy_hcd(hcd);
1227 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001228
1229 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001230 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1231 !list_empty(&dum_hcd->urbp_list))
1232 mod_timer(&dum_hcd->timer, jiffies);
Alan Sterne9df41c2007-08-08 11:48:02 -04001233
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001234 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001235 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001238static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
1239 u32 len)
1240{
1241 void *ubuf, *rbuf;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001242 struct urbp *urbp = urb->hcpriv;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001243 int to_host;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001244 struct sg_mapping_iter *miter = &urbp->miter;
1245 u32 trans = 0;
1246 u32 this_sg;
1247 bool next_sg;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001248
1249 to_host = usb_pipein(urb->pipe);
1250 rbuf = req->req.buf + req->req.actual;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001251
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001252 if (!urb->num_sgs) {
1253 ubuf = urb->transfer_buffer + urb->actual_length;
1254 if (to_host)
1255 memcpy(ubuf, rbuf, len);
1256 else
1257 memcpy(rbuf, ubuf, len);
1258 return len;
1259 }
1260
1261 if (!urbp->miter_started) {
1262 u32 flags = SG_MITER_ATOMIC;
1263
1264 if (to_host)
1265 flags |= SG_MITER_TO_SG;
1266 else
1267 flags |= SG_MITER_FROM_SG;
1268
1269 sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
1270 urbp->miter_started = 1;
1271 }
1272 next_sg = sg_miter_next(miter);
1273 if (next_sg == false) {
1274 WARN_ON_ONCE(1);
1275 return -EINVAL;
1276 }
1277 do {
1278 ubuf = miter->addr;
1279 this_sg = min_t(u32, len, miter->length);
1280 miter->consumed = this_sg;
1281 trans += this_sg;
1282
1283 if (to_host)
1284 memcpy(ubuf, rbuf, this_sg);
1285 else
1286 memcpy(rbuf, ubuf, this_sg);
1287 len -= this_sg;
1288
1289 if (!len)
1290 break;
1291 next_sg = sg_miter_next(miter);
1292 if (next_sg == false) {
1293 WARN_ON_ONCE(1);
1294 return -EINVAL;
1295 }
1296
1297 rbuf += this_sg;
1298 } while (1);
1299
1300 sg_miter_stop(miter);
1301 return trans;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001302}
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304/* transfer up to a frame's worth; caller must own lock */
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001305static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb,
1306 struct dummy_ep *ep, int limit, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001308 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 struct dummy_request *req;
1310
1311top:
1312 /* if there's no request queued, the device is NAKing; return */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001313 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 unsigned host_len, dev_len, len;
1315 int is_short, to_host;
1316 int rescan = 0;
1317
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001318 if (dummy_ep_stream_en(dum_hcd, urb)) {
1319 if ((urb->stream_id != req->req.stream_id))
1320 continue;
1321 }
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 /* 1..N packets of ep->ep.maxpacket each ... the last one
1324 * may be short (including zero length).
1325 *
1326 * writer can send a zlp explicitly (length 0) or implicitly
1327 * (length mod maxpacket zero, and 'zero' flag); they always
1328 * terminate reads.
1329 */
1330 host_len = urb->transfer_buffer_length - urb->actual_length;
1331 dev_len = req->req.length - req->req.actual;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001332 len = min(host_len, dev_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 /* FIXME update emulated data toggle too */
1335
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001336 to_host = usb_pipein(urb->pipe);
1337 if (unlikely(len == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 is_short = 1;
1339 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 /* not enough bandwidth left? */
1341 if (limit < ep->ep.maxpacket && limit < len)
1342 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001343 len = min_t(unsigned, len, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (len == 0)
1345 break;
1346
1347 /* use an extra pass for the final short packet */
1348 if (len > ep->ep.maxpacket) {
1349 rescan = 1;
1350 len -= (len % ep->ep.maxpacket);
1351 }
1352 is_short = (len % ep->ep.maxpacket) != 0;
1353
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001354 len = dummy_perform_transfer(urb, req, len);
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 ep->last_io = jiffies;
Dan Carpenterb1443ac0e2012-03-02 21:51:00 +03001357 if ((int)len < 0) {
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001358 req->req.status = len;
1359 } else {
1360 limit -= len;
1361 urb->actual_length += len;
1362 req->req.actual += len;
1363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365
1366 /* short packets terminate, maybe with overflow/underflow.
1367 * it's only really an error to write too much.
1368 *
1369 * partially filling a buffer optionally blocks queue advances
1370 * (so completion handlers can clean up the queue) but we don't
Alan Sternb0d9efb2007-08-21 15:39:21 -04001371 * need to emulate such data-in-flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 */
1373 if (is_short) {
1374 if (host_len == dev_len) {
1375 req->req.status = 0;
Alan Stern4d2f1102007-08-24 15:40:10 -04001376 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 } else if (to_host) {
1378 req->req.status = 0;
1379 if (dev_len > host_len)
Alan Stern4d2f1102007-08-24 15:40:10 -04001380 *status = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 else
Alan Stern4d2f1102007-08-24 15:40:10 -04001382 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 } else if (!to_host) {
Alan Stern4d2f1102007-08-24 15:40:10 -04001384 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (host_len > dev_len)
1386 req->req.status = -EOVERFLOW;
1387 else
1388 req->req.status = 0;
1389 }
1390
1391 /* many requests terminate without a short packet */
1392 } else {
1393 if (req->req.length == req->req.actual
1394 && !req->req.zero)
1395 req->req.status = 0;
1396 if (urb->transfer_buffer_length == urb->actual_length
1397 && !(urb->transfer_flags
Alan Stern4d2f1102007-08-24 15:40:10 -04001398 & URB_ZERO_PACKET))
1399 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401
1402 /* device side completion --> continuable */
1403 if (req->req.status != -EINPROGRESS) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001404 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001406 spin_unlock(&dum->lock);
1407 req->req.complete(&ep->ep, &req->req);
1408 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 /* requests might have been unlinked... */
1411 rescan = 1;
1412 }
1413
1414 /* host side completion --> terminate */
Alan Stern4d2f1102007-08-24 15:40:10 -04001415 if (*status != -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 break;
1417
1418 /* rescan to continue with any other queued i/o */
1419 if (rescan)
1420 goto top;
1421 }
1422 return limit;
1423}
1424
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001425static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 int limit = ep->ep.maxpacket;
1428
1429 if (dum->gadget.speed == USB_SPEED_HIGH) {
1430 int tmp;
1431
1432 /* high bandwidth mode */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001433 tmp = usb_endpoint_maxp(ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 tmp = (tmp >> 11) & 0x03;
1435 tmp *= 8 /* applies to entire frame */;
1436 limit += limit * tmp;
1437 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001438 if (dum->gadget.speed == USB_SPEED_SUPER) {
Sebastian Andrzej Siewior59f08e62012-01-12 12:53:14 +01001439 switch (usb_endpoint_type(ep->desc)) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001440 case USB_ENDPOINT_XFER_ISOC:
1441 /* Sec. 4.4.8.2 USB3.0 Spec */
1442 limit = 3 * 16 * 1024 * 8;
1443 break;
1444 case USB_ENDPOINT_XFER_INT:
1445 /* Sec. 4.4.7.2 USB3.0 Spec */
1446 limit = 3 * 1024 * 8;
1447 break;
1448 case USB_ENDPOINT_XFER_BULK:
1449 default:
1450 break;
1451 }
1452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return limit;
1454}
1455
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001456#define is_active(dum_hcd) ((dum_hcd->port_status & \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1458 USB_PORT_STAT_SUSPEND)) \
1459 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1460
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001461static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
1463 int i;
1464
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001465 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1466 dum->ss_hcd : dum->hs_hcd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return NULL;
1468 if ((address & ~USB_DIR_IN) == 0)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001469 return &dum->ep[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001471 struct dummy_ep *ep = &dum->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 if (!ep->desc)
1474 continue;
1475 if (ep->desc->bEndpointAddress == address)
1476 return ep;
1477 }
1478 return NULL;
1479}
1480
1481#undef is_active
1482
1483#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1484#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1485#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1486#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1487#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1488#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1489
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001490
1491/**
1492 * handle_control_request() - handles all control transfers
1493 * @dum: pointer to dummy (the_controller)
1494 * @urb: the urb request to handle
1495 * @setup: pointer to the setup data for a USB device control
1496 * request
1497 * @status: pointer to request handling status
1498 *
1499 * Return 0 - if the request was handled
1500 * 1 - if the request wasn't handles
1501 * error code on error
1502 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001503static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001504 struct usb_ctrlrequest *setup,
1505 int *status)
1506{
1507 struct dummy_ep *ep2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001508 struct dummy *dum = dum_hcd->dum;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001509 int ret_val = 1;
1510 unsigned w_index;
1511 unsigned w_value;
1512
1513 w_index = le16_to_cpu(setup->wIndex);
1514 w_value = le16_to_cpu(setup->wValue);
1515 switch (setup->bRequest) {
1516 case USB_REQ_SET_ADDRESS:
1517 if (setup->bRequestType != Dev_Request)
1518 break;
1519 dum->address = w_value;
1520 *status = 0;
1521 dev_dbg(udc_dev(dum), "set_address = %d\n",
1522 w_value);
1523 ret_val = 0;
1524 break;
1525 case USB_REQ_SET_FEATURE:
1526 if (setup->bRequestType == Dev_Request) {
1527 ret_val = 0;
1528 switch (w_value) {
1529 case USB_DEVICE_REMOTE_WAKEUP:
1530 break;
1531 case USB_DEVICE_B_HNP_ENABLE:
1532 dum->gadget.b_hnp_enable = 1;
1533 break;
1534 case USB_DEVICE_A_HNP_SUPPORT:
1535 dum->gadget.a_hnp_support = 1;
1536 break;
1537 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1538 dum->gadget.a_alt_hnp_support = 1;
1539 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001540 case USB_DEVICE_U1_ENABLE:
1541 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1542 HCD_USB3)
1543 w_value = USB_DEV_STAT_U1_ENABLED;
1544 else
1545 ret_val = -EOPNOTSUPP;
1546 break;
1547 case USB_DEVICE_U2_ENABLE:
1548 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1549 HCD_USB3)
1550 w_value = USB_DEV_STAT_U2_ENABLED;
1551 else
1552 ret_val = -EOPNOTSUPP;
1553 break;
1554 case USB_DEVICE_LTM_ENABLE:
1555 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1556 HCD_USB3)
1557 w_value = USB_DEV_STAT_LTM_ENABLED;
1558 else
1559 ret_val = -EOPNOTSUPP;
1560 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001561 default:
1562 ret_val = -EOPNOTSUPP;
1563 }
1564 if (ret_val == 0) {
1565 dum->devstatus |= (1 << w_value);
1566 *status = 0;
1567 }
1568 } else if (setup->bRequestType == Ep_Request) {
1569 /* endpoint halt */
1570 ep2 = find_endpoint(dum, w_index);
1571 if (!ep2 || ep2->ep.name == ep0name) {
1572 ret_val = -EOPNOTSUPP;
1573 break;
1574 }
1575 ep2->halted = 1;
1576 ret_val = 0;
1577 *status = 0;
1578 }
1579 break;
1580 case USB_REQ_CLEAR_FEATURE:
1581 if (setup->bRequestType == Dev_Request) {
1582 ret_val = 0;
1583 switch (w_value) {
1584 case USB_DEVICE_REMOTE_WAKEUP:
1585 w_value = USB_DEVICE_REMOTE_WAKEUP;
1586 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001587 case USB_DEVICE_U1_ENABLE:
1588 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1589 HCD_USB3)
1590 w_value = USB_DEV_STAT_U1_ENABLED;
1591 else
1592 ret_val = -EOPNOTSUPP;
1593 break;
1594 case USB_DEVICE_U2_ENABLE:
1595 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1596 HCD_USB3)
1597 w_value = USB_DEV_STAT_U2_ENABLED;
1598 else
1599 ret_val = -EOPNOTSUPP;
1600 break;
1601 case USB_DEVICE_LTM_ENABLE:
1602 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1603 HCD_USB3)
1604 w_value = USB_DEV_STAT_LTM_ENABLED;
1605 else
1606 ret_val = -EOPNOTSUPP;
1607 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001608 default:
1609 ret_val = -EOPNOTSUPP;
1610 break;
1611 }
1612 if (ret_val == 0) {
1613 dum->devstatus &= ~(1 << w_value);
1614 *status = 0;
1615 }
1616 } else if (setup->bRequestType == Ep_Request) {
1617 /* endpoint halt */
1618 ep2 = find_endpoint(dum, w_index);
1619 if (!ep2) {
1620 ret_val = -EOPNOTSUPP;
1621 break;
1622 }
1623 if (!ep2->wedged)
1624 ep2->halted = 0;
1625 ret_val = 0;
1626 *status = 0;
1627 }
1628 break;
1629 case USB_REQ_GET_STATUS:
1630 if (setup->bRequestType == Dev_InRequest
1631 || setup->bRequestType == Intf_InRequest
1632 || setup->bRequestType == Ep_InRequest) {
1633 char *buf;
1634 /*
1635 * device: remote wakeup, selfpowered
1636 * interface: nothing
1637 * endpoint: halt
1638 */
1639 buf = (char *)urb->transfer_buffer;
1640 if (urb->transfer_buffer_length > 0) {
1641 if (setup->bRequestType == Ep_InRequest) {
1642 ep2 = find_endpoint(dum, w_index);
1643 if (!ep2) {
1644 ret_val = -EOPNOTSUPP;
1645 break;
1646 }
1647 buf[0] = ep2->halted;
1648 } else if (setup->bRequestType ==
1649 Dev_InRequest) {
1650 buf[0] = (u8)dum->devstatus;
1651 } else
1652 buf[0] = 0;
1653 }
1654 if (urb->transfer_buffer_length > 1)
1655 buf[1] = 0;
1656 urb->actual_length = min_t(u32, 2,
1657 urb->transfer_buffer_length);
1658 ret_val = 0;
1659 *status = 0;
1660 }
1661 break;
1662 }
1663 return ret_val;
1664}
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666/* drive both sides of the transfers; looks like irq handlers to
1667 * both drivers except the callbacks aren't in_irq().
1668 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001669static void dummy_timer(unsigned long _dum_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001671 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1672 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 struct urbp *urbp, *tmp;
1674 unsigned long flags;
1675 int limit, total;
1676 int i;
1677
1678 /* simplistic model for one frame's bandwidth */
1679 switch (dum->gadget.speed) {
1680 case USB_SPEED_LOW:
1681 total = 8/*bytes*/ * 12/*packets*/;
1682 break;
1683 case USB_SPEED_FULL:
1684 total = 64/*bytes*/ * 19/*packets*/;
1685 break;
1686 case USB_SPEED_HIGH:
1687 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1688 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001689 case USB_SPEED_SUPER:
1690 /* Bus speed is 500000 bytes/ms, so use a little less */
1691 total = 490000;
1692 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001694 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return;
1696 }
1697
1698 /* FIXME if HZ != 1000 this will probably misbehave ... */
1699
1700 /* look at each urb queued by the host side driver */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001701 spin_lock_irqsave(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001703 if (!dum_hcd->udev) {
1704 dev_err(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 "timer fired with no URBs pending?\n");
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001706 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 return;
1708 }
1709
1710 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001711 if (!ep_name[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001713 dum->ep[i].already_seen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
1715
1716restart:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001717 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 struct urb *urb;
1719 struct dummy_request *req;
1720 u8 address;
1721 struct dummy_ep *ep = NULL;
1722 int type;
Alan Stern4d2f1102007-08-24 15:40:10 -04001723 int status = -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 urb = urbp->urb;
Alan Sterneb231052007-08-21 15:40:36 -04001726 if (urb->unlinked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 goto return_urb;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001728 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
Alan Stern391eca92005-05-10 15:34:16 -04001729 continue;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001730 type = usb_pipetype(urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 /* used up this frame's non-periodic bandwidth?
1733 * FIXME there's infinite bandwidth for control and
1734 * periodic transfers ... unrealistic.
1735 */
1736 if (total <= 0 && type == PIPE_BULK)
1737 continue;
1738
1739 /* find the gadget's ep for this request (if configured) */
1740 address = usb_pipeendpoint (urb->pipe);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001741 if (usb_pipein(urb->pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 address |= USB_DIR_IN;
1743 ep = find_endpoint(dum, address);
1744 if (!ep) {
1745 /* set_configuration() disagreement */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001746 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 "no ep configured for urb %p\n",
1748 urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001749 status = -EPROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 goto return_urb;
1751 }
1752
1753 if (ep->already_seen)
1754 continue;
1755 ep->already_seen = 1;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001756 if (ep == &dum->ep[0] && urb->error_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 ep->setup_stage = 1; /* a new urb */
1758 urb->error_count = 0;
1759 }
1760 if (ep->halted && !ep->setup_stage) {
1761 /* NOTE: must not be iso! */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001762 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 ep->ep.name, urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001764 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 goto return_urb;
1766 }
1767 /* FIXME make sure both ends agree on maxpacket */
1768
1769 /* handle control requests */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001770 if (ep == &dum->ep[0] && ep->setup_stage) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 struct usb_ctrlrequest setup;
1772 int value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001774 setup = *(struct usb_ctrlrequest *) urb->setup_packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 /* paranoia, in case of stale queued data */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001776 list_for_each_entry(req, &ep->queue, queue) {
1777 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 req->req.status = -EOVERFLOW;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001779 dev_dbg(udc_dev(dum), "stale req = %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 req);
1781
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001782 spin_unlock(&dum->lock);
1783 req->req.complete(&ep->ep, &req->req);
1784 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 ep->already_seen = 0;
1786 goto restart;
1787 }
1788
1789 /* gadget driver never sees set_address or operations
1790 * on standard feature flags. some hardware doesn't
1791 * even expose them.
1792 */
1793 ep->last_io = jiffies;
1794 ep->setup_stage = 0;
1795 ep->halted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001797 value = handle_control_request(dum_hcd, urb, &setup,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001798 &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 /* gadget driver handles all other requests. block
1801 * until setup() returns; no reentrancy issues etc.
1802 */
1803 if (value > 0) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001804 spin_unlock(&dum->lock);
1805 value = dum->driver->setup(&dum->gadget,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 &setup);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001807 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 if (value >= 0) {
1810 /* no delays (max 64KB data stage) */
1811 limit = 64*1024;
1812 goto treat_control_like_bulk;
1813 }
1814 /* error, see below */
1815 }
1816
1817 if (value < 0) {
1818 if (value != -EOPNOTSUPP)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001819 dev_dbg(udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 "setup --> %d\n",
1821 value);
Alan Stern4d2f1102007-08-24 15:40:10 -04001822 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 urb->actual_length = 0;
1824 }
1825
1826 goto return_urb;
1827 }
1828
1829 /* non-control requests */
1830 limit = total;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001831 switch (usb_pipetype(urb->pipe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 case PIPE_ISOCHRONOUS:
1833 /* FIXME is it urb->interval since the last xfer?
1834 * use urb->iso_frame_desc[i].
1835 * complete whether or not ep has requests queued.
1836 * report random errors, to debug drivers.
1837 */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001838 limit = max(limit, periodic_bytes(dum, ep));
Alan Stern4d2f1102007-08-24 15:40:10 -04001839 status = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 break;
1841
1842 case PIPE_INTERRUPT:
1843 /* FIXME is it urb->interval since the last xfer?
1844 * this almost certainly polls too fast.
1845 */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001846 limit = max(limit, periodic_bytes(dum, ep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 /* FALLTHROUGH */
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 default:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001850treat_control_like_bulk:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 ep->last_io = jiffies;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001852 total = transfer(dum_hcd, urb, ep, limit, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 break;
1854 }
1855
1856 /* incomplete transfer? */
Alan Stern4d2f1102007-08-24 15:40:10 -04001857 if (status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 continue;
1859
1860return_urb:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001861 list_del(&urbp->urbp_list);
1862 kfree(urbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (ep)
1864 ep->already_seen = ep->setup_stage = 0;
1865
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001866 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001867 spin_unlock(&dum->lock);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001868 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001869 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 goto restart;
1872 }
1873
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001874 if (list_empty(&dum_hcd->urbp_list)) {
1875 usb_put_dev(dum_hcd->udev);
1876 dum_hcd->udev = NULL;
1877 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern391eca92005-05-10 15:34:16 -04001878 /* want a 1 msec delay here */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001879 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
1881
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001882 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
1885/*-------------------------------------------------------------------------*/
1886
1887#define PORT_C_MASK \
Alan Sternc2db8b52005-04-29 16:30:48 -04001888 ((USB_PORT_STAT_C_CONNECTION \
1889 | USB_PORT_STAT_C_ENABLE \
1890 | USB_PORT_STAT_C_SUSPEND \
1891 | USB_PORT_STAT_C_OVERCURRENT \
1892 | USB_PORT_STAT_C_RESET) << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001894static int dummy_hub_status(struct usb_hcd *hcd, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001896 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 unsigned long flags;
Alan Stern391eca92005-05-10 15:34:16 -04001898 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001900 dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001902 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001903 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001904 goto done;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001905
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001906 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1907 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1908 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1909 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001910 }
1911
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001912 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 *buf = (1 << 1);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001914 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1915 dum_hcd->port_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 retval = 1;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001917 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001918 usb_hcd_resume_root_hub(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
Alan Stern391eca92005-05-10 15:34:16 -04001920done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001921 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return retval;
1923}
1924
Sebastian Andrzej Siewior3b9c1c52012-08-19 21:54:59 +02001925/* usb 3.0 root hub device descriptor */
1926struct {
1927 struct usb_bos_descriptor bos;
1928 struct usb_ss_cap_descriptor ss_cap;
1929} __packed usb3_bos_desc = {
1930
1931 .bos = {
1932 .bLength = USB_DT_BOS_SIZE,
1933 .bDescriptorType = USB_DT_BOS,
1934 .wTotalLength = cpu_to_le16(sizeof(usb3_bos_desc)),
1935 .bNumDeviceCaps = 1,
1936 },
1937 .ss_cap = {
1938 .bLength = USB_DT_USB_SS_CAP_SIZE,
1939 .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
1940 .bDevCapabilityType = USB_SS_CAP_TYPE,
1941 .wSpeedSupported = cpu_to_le16(USB_5GBPS_OPERATION),
1942 .bFunctionalitySupport = ilog2(USB_5GBPS_OPERATION),
1943 },
1944};
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946static inline void
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001947ss_hub_descriptor(struct usb_hub_descriptor *desc)
1948{
1949 memset(desc, 0, sizeof *desc);
1950 desc->bDescriptorType = 0x2a;
1951 desc->bDescLength = 12;
1952 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1953 desc->bNbrPorts = 1;
1954 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1955 desc->u.ss.DeviceRemovable = 0xffff;
1956}
1957
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001958static inline void hub_descriptor(struct usb_hub_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001960 memset(desc, 0, sizeof *desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 desc->bDescriptorType = 0x29;
1962 desc->bDescLength = 9;
Al Virofd05e722008-04-28 07:00:16 +01001963 desc->wHubCharacteristics = cpu_to_le16(0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 desc->bNbrPorts = 1;
John Youndbe79bb2001-09-17 00:00:00 -07001965 desc->u.hs.DeviceRemovable[0] = 0xff;
1966 desc->u.hs.DeviceRemovable[1] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001969static int dummy_hub_control(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 struct usb_hcd *hcd,
1971 u16 typeReq,
1972 u16 wValue,
1973 u16 wIndex,
1974 char *buf,
1975 u16 wLength
1976) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001977 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 int retval = 0;
1979 unsigned long flags;
1980
Alan Stern541c7d42010-06-22 16:39:10 -04001981 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001982 return -ETIMEDOUT;
1983
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001984 dum_hcd = hcd_to_dummy_hcd(hcd);
1985
1986 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 switch (typeReq) {
1988 case ClearHubFeature:
1989 break;
1990 case ClearPortFeature:
1991 switch (wValue) {
1992 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001993 if (hcd->speed == HCD_USB3) {
1994 dev_dbg(dummy_dev(dum_hcd),
1995 "USB_PORT_FEAT_SUSPEND req not "
1996 "supported for USB 3.0 roothub\n");
1997 goto error;
1998 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001999 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 /* 20msec resume signaling */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002001 dum_hcd->resuming = 1;
2002 dum_hcd->re_timeout = jiffies +
Alan Sternf1c39fa2005-05-03 16:24:04 -04002003 msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
2005 break;
2006 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002007 if (hcd->speed == HCD_USB3) {
2008 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
2009 dev_dbg(dummy_dev(dum_hcd),
2010 "power-off\n");
2011 } else
2012 if (dum_hcd->port_status &
2013 USB_SS_PORT_STAT_POWER)
2014 dev_dbg(dummy_dev(dum_hcd),
2015 "power-off\n");
Alan Sternf1c39fa2005-05-03 16:24:04 -04002016 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002018 dum_hcd->port_status &= ~(1 << wValue);
2019 set_link_state(dum_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
2021 break;
2022 case GetHubDescriptor:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002023 if (hcd->speed == HCD_USB3 &&
2024 (wLength < USB_DT_SS_HUB_SIZE ||
2025 wValue != (USB_DT_SS_HUB << 8))) {
2026 dev_dbg(dummy_dev(dum_hcd),
2027 "Wrong hub descriptor type for "
2028 "USB 3.0 roothub.\n");
2029 goto error;
2030 }
2031 if (hcd->speed == HCD_USB3)
2032 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
2033 else
2034 hub_descriptor((struct usb_hub_descriptor *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 break;
Sebastian Andrzej Siewior3b9c1c52012-08-19 21:54:59 +02002036
2037 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
2038 if (hcd->speed != HCD_USB3)
2039 goto error;
2040
2041 if ((wValue >> 8) != USB_DT_BOS)
2042 goto error;
2043
2044 memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc));
2045 retval = sizeof(usb3_bos_desc);
2046 break;
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 case GetHubStatus:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002049 *(__le32 *) buf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 break;
2051 case GetPortStatus:
2052 if (wIndex != 1)
2053 retval = -EPIPE;
2054
2055 /* whoever resets or resumes must GetPortStatus to
2056 * complete it!!
2057 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002058 if (dum_hcd->resuming &&
2059 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2060 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
2061 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002063 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
2064 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2065 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
2066 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
2067 if (dum_hcd->dum->pullup) {
2068 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002069
2070 if (hcd->speed < HCD_USB3) {
2071 switch (dum_hcd->dum->gadget.speed) {
2072 case USB_SPEED_HIGH:
2073 dum_hcd->port_status |=
2074 USB_PORT_STAT_HIGH_SPEED;
2075 break;
2076 case USB_SPEED_LOW:
2077 dum_hcd->dum->gadget.ep0->
2078 maxpacket = 8;
2079 dum_hcd->port_status |=
2080 USB_PORT_STAT_LOW_SPEED;
2081 break;
2082 default:
2083 dum_hcd->dum->gadget.speed =
2084 USB_SPEED_FULL;
2085 break;
2086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
2088 }
2089 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002090 set_link_state(dum_hcd);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002091 ((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status);
2092 ((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 break;
2094 case SetHubFeature:
2095 retval = -EPIPE;
2096 break;
2097 case SetPortFeature:
2098 switch (wValue) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002099 case USB_PORT_FEAT_LINK_STATE:
2100 if (hcd->speed != HCD_USB3) {
2101 dev_dbg(dummy_dev(dum_hcd),
2102 "USB_PORT_FEAT_LINK_STATE req not "
2103 "supported for USB 2.0 roothub\n");
2104 goto error;
2105 }
2106 /*
2107 * Since this is dummy we don't have an actual link so
2108 * there is nothing to do for the SET_LINK_STATE cmd
2109 */
2110 break;
2111 case USB_PORT_FEAT_U1_TIMEOUT:
2112 case USB_PORT_FEAT_U2_TIMEOUT:
2113 /* TODO: add suspend/resume support! */
2114 if (hcd->speed != HCD_USB3) {
2115 dev_dbg(dummy_dev(dum_hcd),
2116 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
2117 "supported for USB 2.0 roothub\n");
2118 goto error;
2119 }
2120 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002122 /* Applicable only for USB2.0 hub */
2123 if (hcd->speed == HCD_USB3) {
2124 dev_dbg(dummy_dev(dum_hcd),
2125 "USB_PORT_FEAT_SUSPEND req not "
2126 "supported for USB 3.0 roothub\n");
2127 goto error;
2128 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002129 if (dum_hcd->active) {
2130 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
Alan Sternf1c39fa2005-05-03 16:24:04 -04002131
2132 /* HNP would happen here; for now we
2133 * assume b_bus_req is always true.
2134 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002135 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002136 if (((1 << USB_DEVICE_B_HNP_ENABLE)
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002137 & dum_hcd->dum->devstatus) != 0)
2138 dev_dbg(dummy_dev(dum_hcd),
Alan Stern5742b0c2005-05-02 11:25:17 -04002139 "no HNP yet!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
2141 break;
Alan Sternf1c39fa2005-05-03 16:24:04 -04002142 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002143 if (hcd->speed == HCD_USB3)
2144 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
2145 else
2146 dum_hcd->port_status |= USB_PORT_STAT_POWER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002147 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002148 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002149 case USB_PORT_FEAT_BH_PORT_RESET:
2150 /* Applicable only for USB3.0 hub */
2151 if (hcd->speed != HCD_USB3) {
2152 dev_dbg(dummy_dev(dum_hcd),
2153 "USB_PORT_FEAT_BH_PORT_RESET req not "
2154 "supported for USB 2.0 roothub\n");
2155 goto error;
2156 }
2157 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 case USB_PORT_FEAT_RESET:
Alan Sternf1c39fa2005-05-03 16:24:04 -04002159 /* if it's already enabled, disable */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002160 if (hcd->speed == HCD_USB3) {
2161 dum_hcd->port_status = 0;
2162 dum_hcd->port_status =
2163 (USB_SS_PORT_STAT_POWER |
2164 USB_PORT_STAT_CONNECTION |
2165 USB_PORT_STAT_RESET);
2166 } else
2167 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
Alan Sternf1c39fa2005-05-03 16:24:04 -04002168 | USB_PORT_STAT_LOW_SPEED
2169 | USB_PORT_STAT_HIGH_SPEED);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002170 /*
2171 * We want to reset device status. All but the
2172 * Self powered feature
2173 */
2174 dum_hcd->dum->devstatus &=
2175 (1 << USB_DEVICE_SELF_POWERED);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002176 /*
2177 * FIXME USB3.0: what is the correct reset signaling
2178 * interval? Is it still 50msec as for HS?
2179 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002180 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002181 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 default:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002183 if (hcd->speed == HCD_USB3) {
2184 if ((dum_hcd->port_status &
2185 USB_SS_PORT_STAT_POWER) != 0) {
2186 dum_hcd->port_status |= (1 << wValue);
2187 set_link_state(dum_hcd);
2188 }
2189 } else
2190 if ((dum_hcd->port_status &
2191 USB_PORT_STAT_POWER) != 0) {
2192 dum_hcd->port_status |= (1 << wValue);
2193 set_link_state(dum_hcd);
2194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 }
2196 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002197 case GetPortErrorCount:
2198 if (hcd->speed != HCD_USB3) {
2199 dev_dbg(dummy_dev(dum_hcd),
2200 "GetPortErrorCount req not "
2201 "supported for USB 2.0 roothub\n");
2202 goto error;
2203 }
2204 /* We'll always return 0 since this is a dummy hub */
2205 *(__le32 *) buf = cpu_to_le32(0);
2206 break;
2207 case SetHubDepth:
2208 if (hcd->speed != HCD_USB3) {
2209 dev_dbg(dummy_dev(dum_hcd),
2210 "SetHubDepth req not supported for "
2211 "USB 2.0 roothub\n");
2212 goto error;
2213 }
2214 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002216 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 "hub control req%04x v%04x i%04x l%d\n",
2218 typeReq, wValue, wIndex, wLength);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002219error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 /* "protocol stall" on error */
2221 retval = -EPIPE;
2222 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002223 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Stern685eb932005-05-03 16:27:26 -04002224
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002225 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002226 usb_hcd_poll_rh_status(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 return retval;
2228}
2229
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002230static int dummy_bus_suspend(struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002231{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002232 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002233
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002234 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern3cf0a222005-11-29 12:08:15 -05002235
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002236 spin_lock_irq(&dum_hcd->dum->lock);
2237 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2238 set_link_state(dum_hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002239 hcd->state = HC_STATE_SUSPENDED;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002240 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern391eca92005-05-10 15:34:16 -04002241 return 0;
2242}
2243
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002244static int dummy_bus_resume(struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002245{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002246 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002247 int rc = 0;
2248
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002249 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002250
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002251 spin_lock_irq(&dum_hcd->dum->lock);
Alan Stern541c7d42010-06-22 16:39:10 -04002252 if (!HCD_HW_ACCESSIBLE(hcd)) {
Alan Sterncfa59da2007-06-21 16:25:35 -04002253 rc = -ESHUTDOWN;
Alan Stern3cf0a222005-11-29 12:08:15 -05002254 } else {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002255 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2256 set_link_state(dum_hcd);
2257 if (!list_empty(&dum_hcd->urbp_list))
2258 mod_timer(&dum_hcd->timer, jiffies);
Alan Stern3cf0a222005-11-29 12:08:15 -05002259 hcd->state = HC_STATE_RUNNING;
2260 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002261 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern3cf0a222005-11-29 12:08:15 -05002262 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002263}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265/*-------------------------------------------------------------------------*/
2266
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002267static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002269 int ep = usb_pipeendpoint(urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002271 return snprintf(buf, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 "urb/%p %s ep%d%s%s len %d/%d\n",
2273 urb,
2274 ({ char *s;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002275 switch (urb->dev->speed) {
2276 case USB_SPEED_LOW:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002277 s = "ls";
2278 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002279 case USB_SPEED_FULL:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002280 s = "fs";
2281 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002282 case USB_SPEED_HIGH:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002283 s = "hs";
2284 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002285 case USB_SPEED_SUPER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002286 s = "ss";
2287 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002288 default:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002289 s = "?";
2290 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 }; s; }),
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002292 ep, ep ? (usb_pipein(urb->pipe) ? "in" : "out") : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 ({ char *s; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002294 switch (usb_pipetype(urb->pipe)) { \
2295 case PIPE_CONTROL: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002296 s = ""; \
2297 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002298 case PIPE_BULK: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002299 s = "-bulk"; \
2300 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002301 case PIPE_INTERRUPT: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002302 s = "-int"; \
2303 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002304 default: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002305 s = "-iso"; \
2306 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002307 }; s; }),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 urb->actual_length, urb->transfer_buffer_length);
2309}
2310
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002311static ssize_t show_urbs(struct device *dev, struct device_attribute *attr,
2312 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002314 struct usb_hcd *hcd = dev_get_drvdata(dev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002315 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 struct urbp *urbp;
2317 size_t size = 0;
2318 unsigned long flags;
2319
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002320 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2321 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 size_t temp;
2323
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002324 temp = show_urb(buf, PAGE_SIZE - size, urbp->urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 buf += temp;
2326 size += temp;
2327 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002328 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 return size;
2331}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002332static DEVICE_ATTR(urbs, S_IRUGO, show_urbs, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002334static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2335{
2336 init_timer(&dum_hcd->timer);
2337 dum_hcd->timer.function = dummy_timer;
2338 dum_hcd->timer.data = (unsigned long)dum_hcd;
2339 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002340 dum_hcd->stream_en_ep = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002341 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2342 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2343 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2344 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2345#ifdef CONFIG_USB_OTG
2346 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2347#endif
2348 return 0;
2349
2350 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2351 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2352}
2353
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002354static int dummy_start(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002356 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
2358 /*
2359 * MASTER side init ... we emulate a root hub that'll only ever
2360 * talk to one device (the slave side). Also appears in sysfs,
2361 * just like more familiar pci-based HCDs.
2362 */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002363 if (!usb_hcd_is_primary_hcd(hcd))
2364 return dummy_start_ss(dum_hcd);
2365
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002366 spin_lock_init(&dum_hcd->dum->lock);
2367 init_timer(&dum_hcd->timer);
2368 dum_hcd->timer.function = dummy_timer;
2369 dum_hcd->timer.data = (unsigned long)dum_hcd;
2370 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002372 INIT_LIST_HEAD(&dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
Alan Sterncaf29f62007-12-06 11:10:39 -05002374 hcd->power_budget = POWER_BUDGET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 hcd->state = HC_STATE_RUNNING;
Alan Stern685eb932005-05-03 16:27:26 -04002376 hcd->uses_new_polling = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Alan Stern5742b0c2005-05-02 11:25:17 -04002378#ifdef CONFIG_USB_OTG
2379 hcd->self.otg_port = 1;
2380#endif
2381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002383 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
2385
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002386static void dummy_stop(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
2388 struct dummy *dum;
2389
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002390 dum = hcd_to_dummy_hcd(hcd)->dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002391 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2392 usb_gadget_unregister_driver(dum->driver);
2393 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394}
2395
2396/*-------------------------------------------------------------------------*/
2397
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002398static int dummy_h_get_frame(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002400 return dummy_g_get_frame(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401}
2402
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002403static int dummy_setup(struct usb_hcd *hcd)
2404{
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01002405 hcd->self.sg_tablesize = ~0;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002406 if (usb_hcd_is_primary_hcd(hcd)) {
2407 the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
2408 the_controller.hs_hcd->dum = &the_controller;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002409 /*
2410 * Mark the first roothub as being USB 2.0.
2411 * The USB 3.0 roothub will be registered later by
2412 * dummy_hcd_probe()
2413 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002414 hcd->speed = HCD_USB2;
2415 hcd->self.root_hub->speed = USB_SPEED_HIGH;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002416 } else {
2417 the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
2418 the_controller.ss_hcd->dum = &the_controller;
2419 hcd->speed = HCD_USB3;
2420 hcd->self.root_hub->speed = USB_SPEED_SUPER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002421 }
2422 return 0;
2423}
2424
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002425/* Change a group of bulk endpoints to support multiple stream IDs */
Sebastian Andrzej Siewiord81f3e42012-01-09 13:15:00 +01002426static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002427 struct usb_host_endpoint **eps, unsigned int num_eps,
2428 unsigned int num_streams, gfp_t mem_flags)
2429{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002430 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2431 unsigned long flags;
2432 int max_stream;
2433 int ret_streams = num_streams;
2434 unsigned int index;
2435 unsigned int i;
2436
2437 if (!num_eps)
2438 return -EINVAL;
2439
2440 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2441 for (i = 0; i < num_eps; i++) {
2442 index = dummy_get_ep_idx(&eps[i]->desc);
2443 if ((1 << index) & dum_hcd->stream_en_ep) {
2444 ret_streams = -EINVAL;
2445 goto out;
2446 }
2447 max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp);
2448 if (!max_stream) {
2449 ret_streams = -EINVAL;
2450 goto out;
2451 }
2452 if (max_stream < ret_streams) {
2453 dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u "
2454 "stream IDs.\n",
2455 eps[i]->desc.bEndpointAddress,
2456 max_stream);
2457 ret_streams = max_stream;
2458 }
2459 }
2460
2461 for (i = 0; i < num_eps; i++) {
2462 index = dummy_get_ep_idx(&eps[i]->desc);
2463 dum_hcd->stream_en_ep |= 1 << index;
2464 set_max_streams_for_pipe(dum_hcd,
2465 usb_endpoint_num(&eps[i]->desc), ret_streams);
2466 }
2467out:
2468 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2469 return ret_streams;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002470}
2471
2472/* Reverts a group of bulk endpoints back to not using stream IDs. */
Sebastian Andrzej Siewiord81f3e42012-01-09 13:15:00 +01002473static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002474 struct usb_host_endpoint **eps, unsigned int num_eps,
2475 gfp_t mem_flags)
2476{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002477 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2478 unsigned long flags;
2479 int ret;
2480 unsigned int index;
2481 unsigned int i;
2482
2483 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2484 for (i = 0; i < num_eps; i++) {
2485 index = dummy_get_ep_idx(&eps[i]->desc);
2486 if (!((1 << index) & dum_hcd->stream_en_ep)) {
2487 ret = -EINVAL;
2488 goto out;
2489 }
2490 }
2491
2492 for (i = 0; i < num_eps; i++) {
2493 index = dummy_get_ep_idx(&eps[i]->desc);
2494 dum_hcd->stream_en_ep &= ~(1 << index);
2495 set_max_streams_for_pipe(dum_hcd,
2496 usb_endpoint_num(&eps[i]->desc), 0);
2497 }
2498 ret = 0;
2499out:
2500 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2501 return ret;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002502}
2503
2504static struct hc_driver dummy_hcd = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 .description = (char *) driver_name,
2506 .product_desc = "Dummy host controller",
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002507 .hcd_priv_size = sizeof(struct dummy_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002509 .flags = HCD_USB3 | HCD_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002511 .reset = dummy_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 .start = dummy_start,
2513 .stop = dummy_stop,
2514
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002515 .urb_enqueue = dummy_urb_enqueue,
2516 .urb_dequeue = dummy_urb_dequeue,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002518 .get_frame_number = dummy_h_get_frame,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002520 .hub_status_data = dummy_hub_status,
2521 .hub_control = dummy_hub_control,
Alan Stern0c0382e2005-10-13 17:08:02 -04002522 .bus_suspend = dummy_bus_suspend,
2523 .bus_resume = dummy_bus_resume,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002524
2525 .alloc_streams = dummy_alloc_streams,
2526 .free_streams = dummy_free_streams,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527};
2528
Alan Stern8364d6b2005-11-14 12:16:30 -05002529static int dummy_hcd_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002531 struct usb_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002532 struct usb_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 int retval;
2534
Alan Stern8364d6b2005-11-14 12:16:30 -05002535 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002537 if (!mod_data.is_super_speed)
2538 dummy_hcd.flags = HCD_USB2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002539 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2540 if (!hs_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 return -ENOMEM;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002542 hs_hcd->has_tt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002544 retval = usb_add_hcd(hs_hcd, 0, 0);
Sebastian Andrzej Siewior1b68a4c2012-08-19 21:54:58 +02002545 if (retval)
2546 goto put_usb2_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002547
2548 if (mod_data.is_super_speed) {
2549 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2550 dev_name(&pdev->dev), hs_hcd);
2551 if (!ss_hcd) {
2552 retval = -ENOMEM;
2553 goto dealloc_usb2_hcd;
2554 }
2555
2556 retval = usb_add_hcd(ss_hcd, 0, 0);
2557 if (retval)
2558 goto put_usb3_hcd;
2559 }
2560 return 0;
2561
2562put_usb3_hcd:
2563 usb_put_hcd(ss_hcd);
2564dealloc_usb2_hcd:
Sebastian Andrzej Siewior1b68a4c2012-08-19 21:54:58 +02002565 usb_remove_hcd(hs_hcd);
2566put_usb2_hcd:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002567 usb_put_hcd(hs_hcd);
2568 the_controller.hs_hcd = the_controller.ss_hcd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 return retval;
2570}
2571
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002572static int dummy_hcd_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002574 struct dummy *dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002576 dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002577
2578 if (dum->ss_hcd) {
2579 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2580 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2581 }
2582
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002583 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2584 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002585
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002586 the_controller.hs_hcd = NULL;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002587 the_controller.ss_hcd = NULL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002588
Alan Sternd9b76252005-05-03 16:15:43 -04002589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590}
2591
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002592static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state)
Alan Stern391eca92005-05-10 15:34:16 -04002593{
2594 struct usb_hcd *hcd;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002595 struct dummy_hcd *dum_hcd;
Alan Stern3cf0a222005-11-29 12:08:15 -05002596 int rc = 0;
Alan Stern391eca92005-05-10 15:34:16 -04002597
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002598 dev_dbg(&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002599
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002600 hcd = platform_get_drvdata(pdev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002601 dum_hcd = hcd_to_dummy_hcd(hcd);
2602 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern3cf0a222005-11-29 12:08:15 -05002603 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2604 rc = -EBUSY;
2605 } else
2606 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2607 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002608}
2609
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002610static int dummy_hcd_resume(struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04002611{
2612 struct usb_hcd *hcd;
2613
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002614 dev_dbg(&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002615
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002616 hcd = platform_get_drvdata(pdev);
Alan Stern3cf0a222005-11-29 12:08:15 -05002617 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002618 usb_hcd_poll_rh_status(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002619 return 0;
2620}
2621
Russell King3ae5eae2005-11-09 22:32:44 +00002622static struct platform_driver dummy_hcd_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04002623 .probe = dummy_hcd_probe,
2624 .remove = dummy_hcd_remove,
Alan Stern391eca92005-05-10 15:34:16 -04002625 .suspend = dummy_hcd_suspend,
2626 .resume = dummy_hcd_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002627 .driver = {
2628 .name = (char *) driver_name,
2629 .owner = THIS_MODULE,
2630 },
Alan Sternd9b76252005-05-03 16:15:43 -04002631};
2632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewiorb2113132012-10-30 12:53:17 +01002634#define MAX_NUM_UDC 1
2635static struct platform_device *the_udc_pdev[MAX_NUM_UDC];
2636static struct platform_device *the_hcd_pdev[MAX_NUM_UDC];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002638static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002640 int retval = -ENOMEM;
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002641 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002643 if (usb_disabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 return -ENODEV;
Alan Sternd9b76252005-05-03 16:15:43 -04002645
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +03002646 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2647 return -EINVAL;
2648
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002649 if (mod_data.num < 1 || mod_data.num > MAX_NUM_UDC) {
2650 pr_err("Number of emulated UDC must be in range of 1…%d\n",
2651 MAX_NUM_UDC);
2652 return -EINVAL;
2653 }
2654 for (i = 0; i < mod_data.num; i++) {
2655 the_hcd_pdev[i] = platform_device_alloc(driver_name, i);
2656 if (!the_hcd_pdev[i]) {
2657 i--;
2658 while (i >= 0)
2659 platform_device_put(the_hcd_pdev[i--]);
2660 return retval;
2661 }
2662 }
2663 for (i = 0; i < mod_data.num; i++) {
2664 the_udc_pdev[i] = platform_device_alloc(gadget_name, i);
2665 if (!the_udc_pdev[i]) {
2666 i--;
2667 while (i >= 0)
2668 platform_device_put(the_udc_pdev[i--]);
2669 goto err_alloc_udc;
2670 }
2671 }
Alan Sternd9b76252005-05-03 16:15:43 -04002672
Alan Sterna89a2cd2008-04-07 15:03:25 -04002673 retval = platform_driver_register(&dummy_hcd_driver);
2674 if (retval < 0)
2675 goto err_register_hcd_driver;
2676 retval = platform_driver_register(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002677 if (retval < 0)
2678 goto err_register_udc_driver;
2679
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002680 for (i = 0; i < mod_data.num; i++) {
2681 retval = platform_device_add(the_hcd_pdev[i]);
2682 if (retval < 0) {
2683 i--;
2684 while (i >= 0)
2685 platform_device_del(the_hcd_pdev[i--]);
2686 goto err_add_hcd;
2687 }
2688 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002689 if (!the_controller.hs_hcd ||
2690 (!the_controller.ss_hcd && mod_data.is_super_speed)) {
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002691 /*
2692 * The hcd was added successfully but its probe function failed
2693 * for some reason.
2694 */
2695 retval = -EINVAL;
2696 goto err_add_udc;
2697 }
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002698
2699
2700 for (i = 0; i < mod_data.num; i++) {
2701 retval = platform_device_add(the_udc_pdev[i]);
2702 if (retval < 0) {
2703 i--;
2704 while (i >= 0)
2705 platform_device_del(the_udc_pdev[i]);
2706 goto err_add_udc;
2707 }
2708 }
2709
2710 for (i = 0; i < mod_data.num; i++) {
2711 if (!platform_get_drvdata(the_udc_pdev[i])) {
2712 /*
2713 * The udc was added successfully but its probe
2714 * function failed for some reason.
2715 */
2716 retval = -EINVAL;
2717 goto err_probe_udc;
2718 }
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002719 }
Alan Sternd9b76252005-05-03 16:15:43 -04002720 return retval;
2721
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002722err_probe_udc:
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002723 for (i = 0; i < mod_data.num; i++)
2724 platform_device_del(the_udc_pdev[i]);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002725err_add_udc:
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002726 for (i = 0; i < mod_data.num; i++)
2727 platform_device_del(the_hcd_pdev[i]);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002728err_add_hcd:
2729 platform_driver_unregister(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002730err_register_udc_driver:
Alan Sterna89a2cd2008-04-07 15:03:25 -04002731 platform_driver_unregister(&dummy_hcd_driver);
2732err_register_hcd_driver:
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002733 for (i = 0; i < mod_data.num; i++)
2734 platform_device_put(the_udc_pdev[i]);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002735err_alloc_udc:
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002736 for (i = 0; i < mod_data.num; i++)
2737 platform_device_put(the_hcd_pdev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return retval;
2739}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002740module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002742static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
Sebastian Andrzej Siewiorc7a1db42012-10-29 18:09:55 +01002744 int i;
2745
2746 for (i = 0; i < mod_data.num; i++) {
2747 platform_device_unregister(the_udc_pdev[i]);
2748 platform_device_unregister(the_hcd_pdev[i]);
2749 }
Alan Sterna89a2cd2008-04-07 15:03:25 -04002750 platform_driver_unregister(&dummy_udc_driver);
2751 platform_driver_unregister(&dummy_hcd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002753module_exit(cleanup);