blob: afdbb1cbf5d94d972c52f57f4099a1bb0202bef5 [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;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +030066};
67
68static struct dummy_hcd_module_parameters mod_data = {
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +030069 .is_super_speed = false,
70 .is_high_speed = true,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +030071};
72module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
73MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +030074module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
75MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/*-------------------------------------------------------------------------*/
77
78/* gadget side driver data structres */
79struct dummy_ep {
80 struct list_head queue;
81 unsigned long last_io; /* jiffies timestamp */
82 struct usb_gadget *gadget;
83 const struct usb_endpoint_descriptor *desc;
84 struct usb_ep ep;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +010085 unsigned halted:1;
86 unsigned wedged:1;
87 unsigned already_seen:1;
88 unsigned setup_stage:1;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +010089 unsigned stream_en:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
91
92struct dummy_request {
93 struct list_head queue; /* ep's requests */
94 struct usb_request req;
95};
96
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +010097static inline struct dummy_ep *usb_ep_to_dummy_ep(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +010099 return container_of(_ep, struct dummy_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102static inline struct dummy_request *usb_request_to_dummy_request
103 (struct usb_request *_req)
104{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100105 return container_of(_req, struct dummy_request, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108/*-------------------------------------------------------------------------*/
109
110/*
111 * Every device has ep0 for control requests, plus up to 30 more endpoints,
112 * in one of two types:
113 *
114 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
115 * number can be changed. Names like "ep-a" are used for this type.
116 *
117 * - Fixed Function: in other cases. some characteristics may be mutable;
118 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
119 *
120 * Gadget drivers are responsible for not setting up conflicting endpoint
121 * configurations, illegal or unsupported packet lengths, and so on.
122 */
123
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100124static const char ep0name[] = "ep0";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100126static const char *const ep_name[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 ep0name, /* everyone has ep0 */
128
129 /* act like a net2280: high speed, six configurable endpoints */
130 "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
131
132 /* or like pxa250: fifteen fixed function endpoints */
133 "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
134 "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
135 "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
136 "ep15in-int",
137
138 /* or like sa1100: two fixed function endpoints */
139 "ep1out-bulk", "ep2in-bulk",
140};
Tobias Klauser52950ed2005-12-11 16:20:08 +0100141#define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Alan Sternd9b76252005-05-03 16:15:43 -0400143/*-------------------------------------------------------------------------*/
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define FIFO_SIZE 64
146
147struct urbp {
148 struct urb *urb;
149 struct list_head urbp_list;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +0100150 struct sg_mapping_iter miter;
151 u32 miter_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152};
153
Alan Stern391eca92005-05-10 15:34:16 -0400154
155enum dummy_rh_state {
156 DUMMY_RH_RESET,
157 DUMMY_RH_SUSPENDED,
158 DUMMY_RH_RUNNING
159};
160
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300161struct dummy_hcd {
162 struct dummy *dum;
163 enum dummy_rh_state rh_state;
164 struct timer_list timer;
165 u32 port_status;
166 u32 old_status;
167 unsigned long re_timeout;
168
169 struct usb_device *udev;
170 struct list_head urbp_list;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100171 u32 stream_en_ep;
172 u8 num_stream[30 / 2];
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300173
174 unsigned active:1;
175 unsigned old_active:1;
176 unsigned resuming:1;
177};
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179struct dummy {
180 spinlock_t lock;
181
182 /*
183 * SLAVE/GADGET side support
184 */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100185 struct dummy_ep ep[DUMMY_ENDPOINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int address;
187 struct usb_gadget gadget;
188 struct usb_gadget_driver *driver;
189 struct dummy_request fifo_req;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100190 u8 fifo_buf[FIFO_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 u16 devstatus;
Alan Stern391eca92005-05-10 15:34:16 -0400192 unsigned udc_suspended:1;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400193 unsigned pullup:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 /*
196 * MASTER/HOST side support
197 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300198 struct dummy_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300199 struct dummy_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200};
201
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300202static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300204 return (struct dummy_hcd *) (hcd->hcd_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300207static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 return container_of((void *) dum, struct usb_hcd, hcd_priv);
210}
211
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300212static inline struct device *dummy_dev(struct dummy_hcd *dum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300214 return dummy_hcd_to_hcd(dum)->self.controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100217static inline struct device *udc_dev(struct dummy *dum)
Alan Sternd9b76252005-05-03 16:15:43 -0400218{
219 return dum->gadget.dev.parent;
220}
221
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100222static inline struct dummy *ep_to_dummy(struct dummy_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100224 return container_of(ep->gadget, struct dummy, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300227static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300229 struct dummy *dum = container_of(gadget, struct dummy, gadget);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300230 if (dum->gadget.speed == USB_SPEED_SUPER)
231 return dum->ss_hcd;
232 else
233 return dum->hs_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100236static inline struct dummy *gadget_dev_to_dummy(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100238 return container_of(dev, struct dummy, gadget.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300241static struct dummy the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243/*-------------------------------------------------------------------------*/
244
Alan Sternf1c39fa2005-05-03 16:24:04 -0400245/* SLAVE/GADGET SIDE UTILITY ROUTINES */
246
247/* called with spinlock held */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100248static void nuke(struct dummy *dum, struct dummy_ep *ep)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400249{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100250 while (!list_empty(&ep->queue)) {
Alan Sternf1c39fa2005-05-03 16:24:04 -0400251 struct dummy_request *req;
252
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100253 req = list_entry(ep->queue.next, struct dummy_request, queue);
254 list_del_init(&req->queue);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400255 req->req.status = -ESHUTDOWN;
256
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100257 spin_unlock(&dum->lock);
258 req->req.complete(&ep->ep, &req->req);
259 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400260 }
261}
262
263/* caller must hold lock */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100264static void stop_activity(struct dummy *dum)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400265{
266 struct dummy_ep *ep;
267
268 /* prevent any more requests */
269 dum->address = 0;
270
271 /* The timer is left running so that outstanding URBs can fail */
272
273 /* nuke any pending requests first, so driver i/o is quiesced */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100274 list_for_each_entry(ep, &dum->gadget.ep_list, ep.ep_list)
275 nuke(dum, ep);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400276
277 /* driver now does any non-usb quiescing necessary */
278}
279
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300280/**
281 * set_link_state_by_speed() - Sets the current state of the link according to
282 * the hcd speed
283 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
284 *
285 * This function updates the port_status according to the link state and the
286 * speed of the hcd.
287 */
288static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
289{
290 struct dummy *dum = dum_hcd->dum;
291
292 if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
293 if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
294 dum_hcd->port_status = 0;
295 } else if (!dum->pullup || dum->udc_suspended) {
296 /* UDC suspend must cause a disconnect */
297 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
298 USB_PORT_STAT_ENABLE);
299 if ((dum_hcd->old_status &
300 USB_PORT_STAT_CONNECTION) != 0)
301 dum_hcd->port_status |=
302 (USB_PORT_STAT_C_CONNECTION << 16);
303 } else {
304 /* device is connected and not suspended */
305 dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
306 USB_PORT_STAT_SPEED_5GBPS) ;
307 if ((dum_hcd->old_status &
308 USB_PORT_STAT_CONNECTION) == 0)
309 dum_hcd->port_status |=
310 (USB_PORT_STAT_C_CONNECTION << 16);
311 if ((dum_hcd->port_status &
312 USB_PORT_STAT_ENABLE) == 1 &&
313 (dum_hcd->port_status &
314 USB_SS_PORT_LS_U0) == 1 &&
315 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
316 dum_hcd->active = 1;
317 }
318 } else {
319 if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
320 dum_hcd->port_status = 0;
321 } else if (!dum->pullup || dum->udc_suspended) {
322 /* UDC suspend must cause a disconnect */
323 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
324 USB_PORT_STAT_ENABLE |
325 USB_PORT_STAT_LOW_SPEED |
326 USB_PORT_STAT_HIGH_SPEED |
327 USB_PORT_STAT_SUSPEND);
328 if ((dum_hcd->old_status &
329 USB_PORT_STAT_CONNECTION) != 0)
330 dum_hcd->port_status |=
331 (USB_PORT_STAT_C_CONNECTION << 16);
332 } else {
333 dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
334 if ((dum_hcd->old_status &
335 USB_PORT_STAT_CONNECTION) == 0)
336 dum_hcd->port_status |=
337 (USB_PORT_STAT_C_CONNECTION << 16);
338 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
339 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
340 else if ((dum_hcd->port_status &
341 USB_PORT_STAT_SUSPEND) == 0 &&
342 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
343 dum_hcd->active = 1;
344 }
345 }
346}
347
Alan Sternf1c39fa2005-05-03 16:24:04 -0400348/* caller must hold lock */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300349static void set_link_state(struct dummy_hcd *dum_hcd)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400350{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300351 struct dummy *dum = dum_hcd->dum;
352
353 dum_hcd->active = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300354 if (dum->pullup)
355 if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
356 dum->gadget.speed != USB_SPEED_SUPER) ||
357 (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
358 dum->gadget.speed == USB_SPEED_SUPER))
359 return;
Alan Stern391eca92005-05-10 15:34:16 -0400360
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300361 set_link_state_by_speed(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400362
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300363 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
364 dum_hcd->active)
365 dum_hcd->resuming = 0;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400366
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300367 /* if !connected or reset */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300368 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
369 (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300370 /*
371 * We're connected and not reset (reset occurred now),
372 * and driver attached - disconnect!
373 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300374 if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300375 (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
376 dum->driver) {
377 stop_activity(dum);
378 spin_unlock(&dum->lock);
379 dum->driver->disconnect(&dum->gadget);
380 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400381 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300382 } else if (dum_hcd->active != dum_hcd->old_active) {
383 if (dum_hcd->old_active && dum->driver->suspend) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300384 spin_unlock(&dum->lock);
385 dum->driver->suspend(&dum->gadget);
386 spin_lock(&dum->lock);
387 } else if (!dum_hcd->old_active && dum->driver->resume) {
388 spin_unlock(&dum->lock);
389 dum->driver->resume(&dum->gadget);
390 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400391 }
392 }
393
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300394 dum_hcd->old_status = dum_hcd->port_status;
395 dum_hcd->old_active = dum_hcd->active;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400396}
397
398/*-------------------------------------------------------------------------*/
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/* SLAVE/GADGET SIDE DRIVER
401 *
402 * This only tracks gadget state. All the work is done when the host
403 * side tries some (emulated) i/o operation. Real device controller
404 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
405 */
406
407#define is_enabled(dum) \
408 (dum->port_status & USB_PORT_STAT_ENABLE)
409
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100410static int dummy_enable(struct usb_ep *_ep,
411 const struct usb_endpoint_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300414 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 struct dummy_ep *ep;
416 unsigned max;
417 int retval;
418
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100419 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (!_ep || !desc || ep->desc || _ep->name == ep0name
421 || desc->bDescriptorType != USB_DT_ENDPOINT)
422 return -EINVAL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100423 dum = ep_to_dummy(ep);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300424 if (!dum->driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return -ESHUTDOWN;
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200426
427 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300428 if (!is_enabled(dum_hcd))
429 return -ESHUTDOWN;
430
431 /*
432 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
433 * maximum packet size.
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300434 * For SS devices the wMaxPacketSize is limited by 1024.
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300435 */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700436 max = usb_endpoint_maxp(desc) & 0x7ff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 /* drivers must not request bad settings, since lower levels
439 * (hardware or its drivers) may not check. some endpoints
440 * can't do iso, many have maxpacket limitations, etc.
441 *
442 * since this "hardware" driver is here to help debugging, we
443 * have some extra sanity checks. (there could be more though,
444 * especially for "ep9out" style fixed function ones.)
445 */
446 retval = -EINVAL;
Sebastian Andrzej Siewior59f08e62012-01-12 12:53:14 +0100447 switch (usb_endpoint_type(desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 case USB_ENDPOINT_XFER_BULK:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100449 if (strstr(ep->ep.name, "-iso")
450 || strstr(ep->ep.name, "-int")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 goto done;
452 }
453 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300454 case USB_SPEED_SUPER:
455 if (max == 1024)
456 break;
457 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 case USB_SPEED_HIGH:
459 if (max == 512)
460 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700461 goto done;
462 case USB_SPEED_FULL:
463 if (max == 8 || max == 16 || max == 32 || max == 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /* we'll fake any legal size */
465 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700466 /* save a return statement */
467 default:
468 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470 break;
471 case USB_ENDPOINT_XFER_INT:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100472 if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 goto done;
474 /* real hardware might not handle all packet sizes */
475 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300476 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 case USB_SPEED_HIGH:
478 if (max <= 1024)
479 break;
480 /* save a return statement */
481 case USB_SPEED_FULL:
482 if (max <= 64)
483 break;
484 /* save a return statement */
485 default:
486 if (max <= 8)
487 break;
488 goto done;
489 }
490 break;
491 case USB_ENDPOINT_XFER_ISOC:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100492 if (strstr(ep->ep.name, "-bulk")
493 || strstr(ep->ep.name, "-int"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 goto done;
495 /* real hardware might not handle all packet sizes */
496 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300497 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 case USB_SPEED_HIGH:
499 if (max <= 1024)
500 break;
501 /* save a return statement */
502 case USB_SPEED_FULL:
503 if (max <= 1023)
504 break;
505 /* save a return statement */
506 default:
507 goto done;
508 }
509 break;
510 default:
511 /* few chips support control except on ep0 */
512 goto done;
513 }
514
515 _ep->maxpacket = max;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100516 if (usb_ss_max_streams(_ep->comp_desc)) {
517 if (!usb_endpoint_xfer_bulk(desc)) {
518 dev_err(udc_dev(dum), "Can't enable stream support on "
519 "non-bulk ep %s\n", _ep->name);
520 return -EINVAL;
521 }
522 ep->stream_en = 1;
523 }
Sebastian Andrzej Siewior3cf0ad02012-01-25 11:51:19 +0100524 ep->desc = desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100526 dev_dbg(udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 _ep->name,
528 desc->bEndpointAddress & 0x0f,
529 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
530 ({ char *val;
Sebastian Andrzej Siewior59f08e62012-01-12 12:53:14 +0100531 switch (usb_endpoint_type(desc)) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +0300532 case USB_ENDPOINT_XFER_BULK:
533 val = "bulk";
534 break;
535 case USB_ENDPOINT_XFER_ISOC:
536 val = "iso";
537 break;
538 case USB_ENDPOINT_XFER_INT:
539 val = "intr";
540 break;
541 default:
542 val = "ctrl";
543 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }; val; }),
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100545 max, ep->stream_en ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 /* at this point real hardware should be NAKing transfers
548 * to that endpoint, until a buffer is queued to it.
549 */
Alan Stern851a5262008-08-14 15:48:30 -0400550 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 retval = 0;
552done:
553 return retval;
554}
555
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100556static int dummy_disable(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 struct dummy_ep *ep;
559 struct dummy *dum;
560 unsigned long flags;
561 int retval;
562
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100563 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (!_ep || !ep->desc || _ep->name == ep0name)
565 return -EINVAL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100566 dum = ep_to_dummy(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100568 spin_lock_irqsave(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 ep->desc = NULL;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100570 ep->stream_en = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 retval = 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100572 nuke(dum, ep);
573 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100575 dev_dbg(udc_dev(dum), "disabled %s\n", _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return retval;
577}
578
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100579static struct usb_request *dummy_alloc_request(struct usb_ep *_ep,
580 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 struct dummy_ep *ep;
583 struct dummy_request *req;
584
585 if (!_ep)
586 return NULL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100587 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800589 req = kzalloc(sizeof(*req), mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (!req)
591 return NULL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100592 INIT_LIST_HEAD(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return &req->req;
594}
595
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100596static void dummy_free_request(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 struct dummy_request *req;
599
Sebastian Andrzej Siewiorf99987b2012-02-09 09:24:59 +0100600 if (!_ep || !_req) {
Sasha Levin72688dc2012-05-11 06:39:37 +0200601 WARN_ON(1);
Sebastian Andrzej Siewior20edfbb2012-01-25 15:18:58 +0100602 return;
Sebastian Andrzej Siewiorf99987b2012-02-09 09:24:59 +0100603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100605 req = usb_request_to_dummy_request(_req);
606 WARN_ON(!list_empty(&req->queue));
607 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100610static void fifo_complete(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612}
613
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100614static int dummy_queue(struct usb_ep *_ep, struct usb_request *_req,
Al Viro55016f12005-10-21 03:21:58 -0400615 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 struct dummy_ep *ep;
618 struct dummy_request *req;
619 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300620 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 unsigned long flags;
622
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100623 req = usb_request_to_dummy_request(_req);
624 if (!_req || !list_empty(&req->queue) || !_req->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return -EINVAL;
626
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100627 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (!_ep || (!ep->desc && _ep->name != ep0name))
629 return -EINVAL;
630
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100631 dum = ep_to_dummy(ep);
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200632 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300633 if (!dum->driver || !is_enabled(dum_hcd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return -ESHUTDOWN;
635
636#if 0
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100637 dev_dbg(udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 ep, _req, _ep->name, _req->length, _req->buf);
639#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 _req->status = -EINPROGRESS;
641 _req->actual = 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100642 spin_lock_irqsave(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 /* implement an emulated single-request FIFO */
645 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100646 list_empty(&dum->fifo_req.queue) &&
647 list_empty(&ep->queue) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 _req->length <= FIFO_SIZE) {
649 req = &dum->fifo_req;
650 req->req = *_req;
651 req->req.buf = dum->fifo_buf;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100652 memcpy(dum->fifo_buf, _req->buf, _req->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 req->req.context = dum;
654 req->req.complete = fifo_complete;
655
David Brownellc728df72008-07-26 08:06:24 -0700656 list_add_tail(&req->queue, &ep->queue);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100657 spin_unlock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 _req->actual = _req->length;
659 _req->status = 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100660 _req->complete(_ep, _req);
661 spin_lock(&dum->lock);
David Brownellc728df72008-07-26 08:06:24 -0700662 } else
663 list_add_tail(&req->queue, &ep->queue);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100664 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 /* real hardware would likely enable transfers here, in case
667 * it'd been left NAKing.
668 */
669 return 0;
670}
671
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100672static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 struct dummy_ep *ep;
675 struct dummy *dum;
676 int retval = -EINVAL;
677 unsigned long flags;
678 struct dummy_request *req = NULL;
679
680 if (!_ep || !_req)
681 return retval;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100682 ep = usb_ep_to_dummy_ep(_ep);
683 dum = ep_to_dummy(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 if (!dum->driver)
686 return -ESHUTDOWN;
687
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100688 local_irq_save(flags);
689 spin_lock(&dum->lock);
690 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (&req->req == _req) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100692 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 _req->status = -ECONNRESET;
694 retval = 0;
695 break;
696 }
697 }
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100698 spin_unlock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 if (retval == 0) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100701 dev_dbg(udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 "dequeued req %p from %s, len %d buf %p\n",
703 req, _ep->name, _req->length, _req->buf);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100704 _req->complete(_ep, _req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100706 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return retval;
708}
709
710static int
Alan Stern851a5262008-08-14 15:48:30 -0400711dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 struct dummy_ep *ep;
714 struct dummy *dum;
715
716 if (!_ep)
717 return -EINVAL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100718 ep = usb_ep_to_dummy_ep(_ep);
719 dum = ep_to_dummy(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (!dum->driver)
721 return -ESHUTDOWN;
722 if (!value)
Alan Stern851a5262008-08-14 15:48:30 -0400723 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100725 !list_empty(&ep->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return -EAGAIN;
Alan Stern851a5262008-08-14 15:48:30 -0400727 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 ep->halted = 1;
Alan Stern851a5262008-08-14 15:48:30 -0400729 if (wedged)
730 ep->wedged = 1;
731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 /* FIXME clear emulated data toggle too */
733 return 0;
734}
735
Alan Stern851a5262008-08-14 15:48:30 -0400736static int
737dummy_set_halt(struct usb_ep *_ep, int value)
738{
739 return dummy_set_halt_and_wedge(_ep, value, 0);
740}
741
742static int dummy_set_wedge(struct usb_ep *_ep)
743{
744 if (!_ep || _ep->name == ep0name)
745 return -EINVAL;
746 return dummy_set_halt_and_wedge(_ep, 1, 1);
747}
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749static const struct usb_ep_ops dummy_ep_ops = {
750 .enable = dummy_enable,
751 .disable = dummy_disable,
752
753 .alloc_request = dummy_alloc_request,
754 .free_request = dummy_free_request,
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 .queue = dummy_queue,
757 .dequeue = dummy_dequeue,
758
759 .set_halt = dummy_set_halt,
Alan Stern851a5262008-08-14 15:48:30 -0400760 .set_wedge = dummy_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761};
762
763/*-------------------------------------------------------------------------*/
764
765/* there are both host and device side versions of this call ... */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100766static int dummy_g_get_frame(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 struct timeval tv;
769
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100770 do_gettimeofday(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return tv.tv_usec / 1000;
772}
773
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100774static int dummy_wakeup(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300776 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300778 dum_hcd = gadget_to_dummy_hcd(_gadget);
779 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
Alan Stern5742b0c2005-05-02 11:25:17 -0400780 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return -EINVAL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300782 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
Alan Stern391eca92005-05-10 15:34:16 -0400783 return -ENOLINK;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300784 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
785 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -0400786 return -EIO;
787
788 /* FIXME: What if the root hub is suspended but the port isn't? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 /* hub notices our request, issues downstream resume, etc */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300791 dum_hcd->resuming = 1;
792 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
793 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return 0;
795}
796
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100797static int dummy_set_selfpowered(struct usb_gadget *_gadget, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799 struct dummy *dum;
800
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100801 dum = gadget_to_dummy_hcd(_gadget)->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (value)
803 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
804 else
805 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
806 return 0;
807}
808
Sebastian Andrzej Siewiord2621272012-01-09 13:14:59 +0100809static void dummy_udc_update_ep0(struct dummy *dum)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200810{
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100811 if (dum->gadget.speed == USB_SPEED_SUPER)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200812 dum->ep[0].ep.maxpacket = 9;
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100813 else
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200814 dum->ep[0].ep.maxpacket = 64;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200815}
816
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100817static int dummy_pullup(struct usb_gadget *_gadget, int value)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400818{
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200819 struct dummy_hcd *dum_hcd;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400820 struct dummy *dum;
821 unsigned long flags;
822
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200823 dum = gadget_dev_to_dummy(&_gadget->dev);
824
825 if (value && dum->driver) {
826 if (mod_data.is_super_speed)
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100827 dum->gadget.speed = dum->driver->max_speed;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200828 else if (mod_data.is_high_speed)
829 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100830 dum->driver->max_speed);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200831 else
832 dum->gadget.speed = USB_SPEED_FULL;
Sebastian Andrzej Siewiord2621272012-01-09 13:14:59 +0100833 dummy_udc_update_ep0(dum);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200834
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100835 if (dum->gadget.speed < dum->driver->max_speed)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200836 dev_dbg(udc_dev(dum), "This device can perform faster"
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100837 " if you connect it to a %s port...\n",
838 usb_speed_string(dum->driver->max_speed));
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200839 }
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200840 dum_hcd = gadget_to_dummy_hcd(_gadget);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200841
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100842 spin_lock_irqsave(&dum->lock, flags);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400843 dum->pullup = (value != 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200844 set_link_state(dum_hcd);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100845 spin_unlock_irqrestore(&dum->lock, flags);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200846
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200847 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Sternf1c39fa2005-05-03 16:24:04 -0400848 return 0;
849}
850
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200851static int dummy_udc_start(struct usb_gadget *g,
852 struct usb_gadget_driver *driver);
853static int dummy_udc_stop(struct usb_gadget *g,
854 struct usb_gadget_driver *driver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856static const struct usb_gadget_ops dummy_ops = {
857 .get_frame = dummy_g_get_frame,
858 .wakeup = dummy_wakeup,
859 .set_selfpowered = dummy_set_selfpowered,
Alan Sternf1c39fa2005-05-03 16:24:04 -0400860 .pullup = dummy_pullup,
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200861 .udc_start = dummy_udc_start,
862 .udc_stop = dummy_udc_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863};
864
865/*-------------------------------------------------------------------------*/
866
867/* "function" sysfs attribute */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100868static ssize_t show_function(struct device *dev, struct device_attribute *attr,
869 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100871 struct dummy *dum = gadget_dev_to_dummy(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 if (!dum->driver || !dum->driver->function)
874 return 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100875 return scnprintf(buf, PAGE_SIZE, "%s\n", dum->driver->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100877static DEVICE_ATTR(function, S_IRUGO, show_function, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879/*-------------------------------------------------------------------------*/
880
881/*
882 * Driver registration/unregistration.
883 *
884 * This is basically hardware-specific; there's usually only one real USB
885 * device (not host) controller since that's how USB devices are intended
886 * to work. So most implementations of these api calls will rely on the
887 * fact that only one driver will ever bind to the hardware. But curious
888 * hardware can be built with discrete components, so the gadget API doesn't
889 * require that assumption.
890 *
891 * For this emulator, it might be convenient to create a usb slave device
892 * for each driver that registers: just add to a big root hub.
893 */
894
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200895static int dummy_udc_start(struct usb_gadget *g,
896 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200898 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
899 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100901 if (driver->max_speed == USB_SPEED_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return -EINVAL;
903
904 /*
905 * SLAVE side init ... the layer above hardware, which
906 * can't enumerate without help from the driver we're binding.
907 */
Alan Stern5742b0c2005-05-02 11:25:17 -0400908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 dum->devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 dum->driver = driver;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100912 dev_dbg(udc_dev(dum), "binding gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return 0;
915}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200917static int dummy_udc_stop(struct usb_gadget *g,
918 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200920 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
921 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100923 dev_dbg(udc_dev(dum), "unregister gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 driver->driver.name);
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 dum->driver = NULL;
Sebastian Andrzej Siewior25427872011-06-16 20:36:55 +0200927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return 0;
929}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931#undef is_enabled
932
Alan Sternd9b76252005-05-03 16:15:43 -0400933/* The gadget structure is stored inside the hcd structure and will be
934 * released along with it. */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100935static void dummy_gadget_release(struct device *dev)
Alan Sternd9b76252005-05-03 16:15:43 -0400936{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300937 return;
Alan Sternd9b76252005-05-03 16:15:43 -0400938}
939
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200940static void init_dummy_udc_hw(struct dummy *dum)
941{
942 int i;
943
944 INIT_LIST_HEAD(&dum->gadget.ep_list);
945 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
946 struct dummy_ep *ep = &dum->ep[i];
947
948 if (!ep_name[i])
949 break;
950 ep->ep.name = ep_name[i];
951 ep->ep.ops = &dummy_ep_ops;
952 list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
953 ep->halted = ep->wedged = ep->already_seen =
954 ep->setup_stage = 0;
955 ep->ep.maxpacket = ~0;
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100956 ep->ep.max_streams = 16;
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200957 ep->last_io = jiffies;
958 ep->gadget = &dum->gadget;
959 ep->desc = NULL;
960 INIT_LIST_HEAD(&ep->queue);
961 }
962
963 dum->gadget.ep0 = &dum->ep[0].ep;
964 list_del_init(&dum->ep[0].ep.ep_list);
965 INIT_LIST_HEAD(&dum->fifo_req.queue);
Sebastian Andrzej Siewiorf8744d42011-06-23 14:26:13 +0200966
967#ifdef CONFIG_USB_OTG
968 dum->gadget.is_otg = 1;
969#endif
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200970}
971
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100972static int dummy_udc_probe(struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -0400973{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300974 struct dummy *dum = &the_controller;
Alan Sternd9b76252005-05-03 16:15:43 -0400975 int rc;
976
977 dum->gadget.name = gadget_name;
978 dum->gadget.ops = &dummy_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100979 dum->gadget.max_speed = USB_SPEED_SUPER;
Alan Sternd9b76252005-05-03 16:15:43 -0400980
Kay Sievers0031a062008-05-02 06:02:41 +0200981 dev_set_name(&dum->gadget.dev, "gadget");
Alan Stern8364d6b2005-11-14 12:16:30 -0500982 dum->gadget.dev.parent = &pdev->dev;
Alan Sternd9b76252005-05-03 16:15:43 -0400983 dum->gadget.dev.release = dummy_gadget_release;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100984 rc = device_register(&dum->gadget.dev);
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530985 if (rc < 0) {
986 put_device(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -0400987 return rc;
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530988 }
Alan Sternd9b76252005-05-03 16:15:43 -0400989
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200990 init_dummy_udc_hw(dum);
991
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300992 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
993 if (rc < 0)
994 goto err_udc;
995
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100996 rc = device_create_file(&dum->gadget.dev, &dev_attr_function);
Alan Sternefd54a32006-09-25 11:55:56 -0400997 if (rc < 0)
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300998 goto err_dev;
999 platform_set_drvdata(pdev, dum);
1000 return rc;
1001
1002err_dev:
1003 usb_del_gadget_udc(&dum->gadget);
1004err_udc:
1005 device_unregister(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -04001006 return rc;
1007}
1008
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001009static int dummy_udc_remove(struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -04001010{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001011 struct dummy *dum = platform_get_drvdata(pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04001012
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001013 usb_del_gadget_udc(&dum->gadget);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001014 platform_set_drvdata(pdev, NULL);
1015 device_remove_file(&dum->gadget.dev, &dev_attr_function);
1016 device_unregister(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -04001017 return 0;
1018}
1019
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001020static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1021 int suspend)
Alan Stern391eca92005-05-10 15:34:16 -04001022{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001023 spin_lock_irq(&dum->lock);
1024 dum->udc_suspended = suspend;
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001025 set_link_state(dum_hcd);
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001026 spin_unlock_irq(&dum->lock);
1027}
Alan Stern391eca92005-05-10 15:34:16 -04001028
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001029static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1030{
1031 struct dummy *dum = platform_get_drvdata(pdev);
1032 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1033
1034 dev_dbg(&pdev->dev, "%s\n", __func__);
1035 dummy_udc_pm(dum, dum_hcd, 1);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001036 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001037 return 0;
1038}
1039
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001040static int dummy_udc_resume(struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04001041{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001042 struct dummy *dum = platform_get_drvdata(pdev);
1043 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Alan Stern391eca92005-05-10 15:34:16 -04001044
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001045 dev_dbg(&pdev->dev, "%s\n", __func__);
1046 dummy_udc_pm(dum, dum_hcd, 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001047 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001048 return 0;
1049}
1050
Russell King3ae5eae2005-11-09 22:32:44 +00001051static struct platform_driver dummy_udc_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04001052 .probe = dummy_udc_probe,
1053 .remove = dummy_udc_remove,
Alan Stern391eca92005-05-10 15:34:16 -04001054 .suspend = dummy_udc_suspend,
1055 .resume = dummy_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001056 .driver = {
1057 .name = (char *) gadget_name,
1058 .owner = THIS_MODULE,
1059 },
Alan Sternd9b76252005-05-03 16:15:43 -04001060};
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062/*-------------------------------------------------------------------------*/
1063
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001064static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc)
1065{
1066 unsigned int index;
1067
1068 index = usb_endpoint_num(desc) << 1;
1069 if (usb_endpoint_dir_in(desc))
1070 index |= 1;
1071 return index;
1072}
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074/* MASTER/HOST SIDE DRIVER
1075 *
1076 * this uses the hcd framework to hook up to host side drivers.
1077 * its root hub will only have one device, otherwise it acts like
1078 * a normal host controller.
1079 *
1080 * when urbs are queued, they're just stuck on a list that we
1081 * scan in a timer callback. that callback connects writes from
1082 * the host with reads from the device, and so on, based on the
1083 * usb 2.0 rules.
1084 */
1085
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001086static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb)
1087{
1088 const struct usb_endpoint_descriptor *desc = &urb->ep->desc;
1089 u32 index;
1090
1091 if (!usb_endpoint_xfer_bulk(desc))
1092 return 0;
1093
1094 index = dummy_get_ep_idx(desc);
1095 return (1 << index) & dum_hcd->stream_en_ep;
1096}
1097
1098/*
1099 * The max stream number is saved as a nibble so for the 30 possible endpoints
1100 * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
1101 * means we use only 1 stream). The maximum according to the spec is 16bit so
1102 * if the 16 stream limit is about to go, the array size should be incremented
1103 * to 30 elements of type u16.
1104 */
1105static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1106 unsigned int pipe)
1107{
1108 int max_streams;
1109
1110 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1111 if (usb_pipeout(pipe))
1112 max_streams >>= 4;
1113 else
1114 max_streams &= 0xf;
1115 max_streams++;
1116 return max_streams;
1117}
1118
1119static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1120 unsigned int pipe, unsigned int streams)
1121{
1122 int max_streams;
1123
1124 streams--;
1125 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1126 if (usb_pipeout(pipe)) {
1127 streams <<= 4;
1128 max_streams &= 0xf;
1129 } else {
1130 max_streams &= 0xf0;
1131 }
1132 max_streams |= streams;
1133 dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams;
1134}
1135
1136static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb)
1137{
1138 unsigned int max_streams;
1139 int enabled;
1140
1141 enabled = dummy_ep_stream_en(dum_hcd, urb);
1142 if (!urb->stream_id) {
1143 if (enabled)
1144 return -EINVAL;
1145 return 0;
1146 }
1147 if (!enabled)
1148 return -EINVAL;
1149
1150 max_streams = get_max_streams_for_pipe(dum_hcd,
1151 usb_pipeendpoint(urb->pipe));
1152 if (urb->stream_id > max_streams) {
1153 dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n",
1154 urb->stream_id);
1155 BUG();
1156 return -EINVAL;
1157 }
1158 return 0;
1159}
1160
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001161static int dummy_urb_enqueue(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 struct usb_hcd *hcd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001164 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001166 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 struct urbp *urbp;
1168 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001169 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001171 urbp = kmalloc(sizeof *urbp, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (!urbp)
1173 return -ENOMEM;
1174 urbp->urb = urb;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001175 urbp->miter_started = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001177 dum_hcd = hcd_to_dummy_hcd(hcd);
1178 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001179
1180 rc = dummy_validate_stream(dum_hcd, urb);
1181 if (rc) {
1182 kfree(urbp);
1183 goto done;
1184 }
1185
Alan Sterne9df41c2007-08-08 11:48:02 -04001186 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1187 if (rc) {
1188 kfree(urbp);
1189 goto done;
1190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001192 if (!dum_hcd->udev) {
1193 dum_hcd->udev = urb->dev;
1194 usb_get_dev(dum_hcd->udev);
1195 } else if (unlikely(dum_hcd->udev != urb->dev))
1196 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001198 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 urb->hcpriv = urbp;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001200 if (usb_pipetype(urb->pipe) == PIPE_CONTROL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 urb->error_count = 1; /* mark as a new urb */
1202
1203 /* kick the scheduler, it'll do the rest */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001204 if (!timer_pending(&dum_hcd->timer))
1205 mod_timer(&dum_hcd->timer, jiffies + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Alan Sterne9df41c2007-08-08 11:48:02 -04001207 done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001208 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001209 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
Alan Sterne9df41c2007-08-08 11:48:02 -04001212static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001214 struct dummy_hcd *dum_hcd;
Alan Stern391eca92005-05-10 15:34:16 -04001215 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001216 int rc;
Alan Stern391eca92005-05-10 15:34:16 -04001217
1218 /* giveback happens automatically in timer callback,
1219 * so make sure the callback happens */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001220 dum_hcd = hcd_to_dummy_hcd(hcd);
1221 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001222
1223 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001224 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1225 !list_empty(&dum_hcd->urbp_list))
1226 mod_timer(&dum_hcd->timer, jiffies);
Alan Sterne9df41c2007-08-08 11:48:02 -04001227
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001228 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001229 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001232static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
1233 u32 len)
1234{
1235 void *ubuf, *rbuf;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001236 struct urbp *urbp = urb->hcpriv;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001237 int to_host;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001238 struct sg_mapping_iter *miter = &urbp->miter;
1239 u32 trans = 0;
1240 u32 this_sg;
1241 bool next_sg;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001242
1243 to_host = usb_pipein(urb->pipe);
1244 rbuf = req->req.buf + req->req.actual;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001245
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001246 if (!urb->num_sgs) {
1247 ubuf = urb->transfer_buffer + urb->actual_length;
1248 if (to_host)
1249 memcpy(ubuf, rbuf, len);
1250 else
1251 memcpy(rbuf, ubuf, len);
1252 return len;
1253 }
1254
1255 if (!urbp->miter_started) {
1256 u32 flags = SG_MITER_ATOMIC;
1257
1258 if (to_host)
1259 flags |= SG_MITER_TO_SG;
1260 else
1261 flags |= SG_MITER_FROM_SG;
1262
1263 sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
1264 urbp->miter_started = 1;
1265 }
1266 next_sg = sg_miter_next(miter);
1267 if (next_sg == false) {
1268 WARN_ON_ONCE(1);
1269 return -EINVAL;
1270 }
1271 do {
1272 ubuf = miter->addr;
1273 this_sg = min_t(u32, len, miter->length);
1274 miter->consumed = this_sg;
1275 trans += this_sg;
1276
1277 if (to_host)
1278 memcpy(ubuf, rbuf, this_sg);
1279 else
1280 memcpy(rbuf, ubuf, this_sg);
1281 len -= this_sg;
1282
1283 if (!len)
1284 break;
1285 next_sg = sg_miter_next(miter);
1286 if (next_sg == false) {
1287 WARN_ON_ONCE(1);
1288 return -EINVAL;
1289 }
1290
1291 rbuf += this_sg;
1292 } while (1);
1293
1294 sg_miter_stop(miter);
1295 return trans;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001296}
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298/* transfer up to a frame's worth; caller must own lock */
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001299static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb,
1300 struct dummy_ep *ep, int limit, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001302 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 struct dummy_request *req;
1304
1305top:
1306 /* if there's no request queued, the device is NAKing; return */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001307 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 unsigned host_len, dev_len, len;
1309 int is_short, to_host;
1310 int rescan = 0;
1311
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001312 if (dummy_ep_stream_en(dum_hcd, urb)) {
1313 if ((urb->stream_id != req->req.stream_id))
1314 continue;
1315 }
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 /* 1..N packets of ep->ep.maxpacket each ... the last one
1318 * may be short (including zero length).
1319 *
1320 * writer can send a zlp explicitly (length 0) or implicitly
1321 * (length mod maxpacket zero, and 'zero' flag); they always
1322 * terminate reads.
1323 */
1324 host_len = urb->transfer_buffer_length - urb->actual_length;
1325 dev_len = req->req.length - req->req.actual;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001326 len = min(host_len, dev_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 /* FIXME update emulated data toggle too */
1329
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001330 to_host = usb_pipein(urb->pipe);
1331 if (unlikely(len == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 is_short = 1;
1333 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 /* not enough bandwidth left? */
1335 if (limit < ep->ep.maxpacket && limit < len)
1336 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001337 len = min_t(unsigned, len, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (len == 0)
1339 break;
1340
1341 /* use an extra pass for the final short packet */
1342 if (len > ep->ep.maxpacket) {
1343 rescan = 1;
1344 len -= (len % ep->ep.maxpacket);
1345 }
1346 is_short = (len % ep->ep.maxpacket) != 0;
1347
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001348 len = dummy_perform_transfer(urb, req, len);
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 ep->last_io = jiffies;
Dan Carpenterb1443ac0e2012-03-02 21:51:00 +03001351 if ((int)len < 0) {
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001352 req->req.status = len;
1353 } else {
1354 limit -= len;
1355 urb->actual_length += len;
1356 req->req.actual += len;
1357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359
1360 /* short packets terminate, maybe with overflow/underflow.
1361 * it's only really an error to write too much.
1362 *
1363 * partially filling a buffer optionally blocks queue advances
1364 * (so completion handlers can clean up the queue) but we don't
Alan Sternb0d9efb2007-08-21 15:39:21 -04001365 * need to emulate such data-in-flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 */
1367 if (is_short) {
1368 if (host_len == dev_len) {
1369 req->req.status = 0;
Alan Stern4d2f1102007-08-24 15:40:10 -04001370 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 } else if (to_host) {
1372 req->req.status = 0;
1373 if (dev_len > host_len)
Alan Stern4d2f1102007-08-24 15:40:10 -04001374 *status = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 else
Alan Stern4d2f1102007-08-24 15:40:10 -04001376 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 } else if (!to_host) {
Alan Stern4d2f1102007-08-24 15:40:10 -04001378 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (host_len > dev_len)
1380 req->req.status = -EOVERFLOW;
1381 else
1382 req->req.status = 0;
1383 }
1384
1385 /* many requests terminate without a short packet */
1386 } else {
1387 if (req->req.length == req->req.actual
1388 && !req->req.zero)
1389 req->req.status = 0;
1390 if (urb->transfer_buffer_length == urb->actual_length
1391 && !(urb->transfer_flags
Alan Stern4d2f1102007-08-24 15:40:10 -04001392 & URB_ZERO_PACKET))
1393 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
1395
1396 /* device side completion --> continuable */
1397 if (req->req.status != -EINPROGRESS) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001398 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001400 spin_unlock(&dum->lock);
1401 req->req.complete(&ep->ep, &req->req);
1402 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 /* requests might have been unlinked... */
1405 rescan = 1;
1406 }
1407
1408 /* host side completion --> terminate */
Alan Stern4d2f1102007-08-24 15:40:10 -04001409 if (*status != -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 break;
1411
1412 /* rescan to continue with any other queued i/o */
1413 if (rescan)
1414 goto top;
1415 }
1416 return limit;
1417}
1418
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001419static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421 int limit = ep->ep.maxpacket;
1422
1423 if (dum->gadget.speed == USB_SPEED_HIGH) {
1424 int tmp;
1425
1426 /* high bandwidth mode */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001427 tmp = usb_endpoint_maxp(ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 tmp = (tmp >> 11) & 0x03;
1429 tmp *= 8 /* applies to entire frame */;
1430 limit += limit * tmp;
1431 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001432 if (dum->gadget.speed == USB_SPEED_SUPER) {
Sebastian Andrzej Siewior59f08e62012-01-12 12:53:14 +01001433 switch (usb_endpoint_type(ep->desc)) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001434 case USB_ENDPOINT_XFER_ISOC:
1435 /* Sec. 4.4.8.2 USB3.0 Spec */
1436 limit = 3 * 16 * 1024 * 8;
1437 break;
1438 case USB_ENDPOINT_XFER_INT:
1439 /* Sec. 4.4.7.2 USB3.0 Spec */
1440 limit = 3 * 1024 * 8;
1441 break;
1442 case USB_ENDPOINT_XFER_BULK:
1443 default:
1444 break;
1445 }
1446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return limit;
1448}
1449
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001450#define is_active(dum_hcd) ((dum_hcd->port_status & \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1452 USB_PORT_STAT_SUSPEND)) \
1453 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1454
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001455static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 int i;
1458
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001459 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1460 dum->ss_hcd : dum->hs_hcd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return NULL;
1462 if ((address & ~USB_DIR_IN) == 0)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001463 return &dum->ep[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001465 struct dummy_ep *ep = &dum->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 if (!ep->desc)
1468 continue;
1469 if (ep->desc->bEndpointAddress == address)
1470 return ep;
1471 }
1472 return NULL;
1473}
1474
1475#undef is_active
1476
1477#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1478#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1479#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1480#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1481#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1482#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1483
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001484
1485/**
1486 * handle_control_request() - handles all control transfers
1487 * @dum: pointer to dummy (the_controller)
1488 * @urb: the urb request to handle
1489 * @setup: pointer to the setup data for a USB device control
1490 * request
1491 * @status: pointer to request handling status
1492 *
1493 * Return 0 - if the request was handled
1494 * 1 - if the request wasn't handles
1495 * error code on error
1496 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001497static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001498 struct usb_ctrlrequest *setup,
1499 int *status)
1500{
1501 struct dummy_ep *ep2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001502 struct dummy *dum = dum_hcd->dum;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001503 int ret_val = 1;
1504 unsigned w_index;
1505 unsigned w_value;
1506
1507 w_index = le16_to_cpu(setup->wIndex);
1508 w_value = le16_to_cpu(setup->wValue);
1509 switch (setup->bRequest) {
1510 case USB_REQ_SET_ADDRESS:
1511 if (setup->bRequestType != Dev_Request)
1512 break;
1513 dum->address = w_value;
1514 *status = 0;
1515 dev_dbg(udc_dev(dum), "set_address = %d\n",
1516 w_value);
1517 ret_val = 0;
1518 break;
1519 case USB_REQ_SET_FEATURE:
1520 if (setup->bRequestType == Dev_Request) {
1521 ret_val = 0;
1522 switch (w_value) {
1523 case USB_DEVICE_REMOTE_WAKEUP:
1524 break;
1525 case USB_DEVICE_B_HNP_ENABLE:
1526 dum->gadget.b_hnp_enable = 1;
1527 break;
1528 case USB_DEVICE_A_HNP_SUPPORT:
1529 dum->gadget.a_hnp_support = 1;
1530 break;
1531 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1532 dum->gadget.a_alt_hnp_support = 1;
1533 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001534 case USB_DEVICE_U1_ENABLE:
1535 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1536 HCD_USB3)
1537 w_value = USB_DEV_STAT_U1_ENABLED;
1538 else
1539 ret_val = -EOPNOTSUPP;
1540 break;
1541 case USB_DEVICE_U2_ENABLE:
1542 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1543 HCD_USB3)
1544 w_value = USB_DEV_STAT_U2_ENABLED;
1545 else
1546 ret_val = -EOPNOTSUPP;
1547 break;
1548 case USB_DEVICE_LTM_ENABLE:
1549 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1550 HCD_USB3)
1551 w_value = USB_DEV_STAT_LTM_ENABLED;
1552 else
1553 ret_val = -EOPNOTSUPP;
1554 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001555 default:
1556 ret_val = -EOPNOTSUPP;
1557 }
1558 if (ret_val == 0) {
1559 dum->devstatus |= (1 << w_value);
1560 *status = 0;
1561 }
1562 } else if (setup->bRequestType == Ep_Request) {
1563 /* endpoint halt */
1564 ep2 = find_endpoint(dum, w_index);
1565 if (!ep2 || ep2->ep.name == ep0name) {
1566 ret_val = -EOPNOTSUPP;
1567 break;
1568 }
1569 ep2->halted = 1;
1570 ret_val = 0;
1571 *status = 0;
1572 }
1573 break;
1574 case USB_REQ_CLEAR_FEATURE:
1575 if (setup->bRequestType == Dev_Request) {
1576 ret_val = 0;
1577 switch (w_value) {
1578 case USB_DEVICE_REMOTE_WAKEUP:
1579 w_value = USB_DEVICE_REMOTE_WAKEUP;
1580 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001581 case USB_DEVICE_U1_ENABLE:
1582 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1583 HCD_USB3)
1584 w_value = USB_DEV_STAT_U1_ENABLED;
1585 else
1586 ret_val = -EOPNOTSUPP;
1587 break;
1588 case USB_DEVICE_U2_ENABLE:
1589 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1590 HCD_USB3)
1591 w_value = USB_DEV_STAT_U2_ENABLED;
1592 else
1593 ret_val = -EOPNOTSUPP;
1594 break;
1595 case USB_DEVICE_LTM_ENABLE:
1596 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1597 HCD_USB3)
1598 w_value = USB_DEV_STAT_LTM_ENABLED;
1599 else
1600 ret_val = -EOPNOTSUPP;
1601 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001602 default:
1603 ret_val = -EOPNOTSUPP;
1604 break;
1605 }
1606 if (ret_val == 0) {
1607 dum->devstatus &= ~(1 << w_value);
1608 *status = 0;
1609 }
1610 } else if (setup->bRequestType == Ep_Request) {
1611 /* endpoint halt */
1612 ep2 = find_endpoint(dum, w_index);
1613 if (!ep2) {
1614 ret_val = -EOPNOTSUPP;
1615 break;
1616 }
1617 if (!ep2->wedged)
1618 ep2->halted = 0;
1619 ret_val = 0;
1620 *status = 0;
1621 }
1622 break;
1623 case USB_REQ_GET_STATUS:
1624 if (setup->bRequestType == Dev_InRequest
1625 || setup->bRequestType == Intf_InRequest
1626 || setup->bRequestType == Ep_InRequest) {
1627 char *buf;
1628 /*
1629 * device: remote wakeup, selfpowered
1630 * interface: nothing
1631 * endpoint: halt
1632 */
1633 buf = (char *)urb->transfer_buffer;
1634 if (urb->transfer_buffer_length > 0) {
1635 if (setup->bRequestType == Ep_InRequest) {
1636 ep2 = find_endpoint(dum, w_index);
1637 if (!ep2) {
1638 ret_val = -EOPNOTSUPP;
1639 break;
1640 }
1641 buf[0] = ep2->halted;
1642 } else if (setup->bRequestType ==
1643 Dev_InRequest) {
1644 buf[0] = (u8)dum->devstatus;
1645 } else
1646 buf[0] = 0;
1647 }
1648 if (urb->transfer_buffer_length > 1)
1649 buf[1] = 0;
1650 urb->actual_length = min_t(u32, 2,
1651 urb->transfer_buffer_length);
1652 ret_val = 0;
1653 *status = 0;
1654 }
1655 break;
1656 }
1657 return ret_val;
1658}
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660/* drive both sides of the transfers; looks like irq handlers to
1661 * both drivers except the callbacks aren't in_irq().
1662 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001663static void dummy_timer(unsigned long _dum_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001665 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1666 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 struct urbp *urbp, *tmp;
1668 unsigned long flags;
1669 int limit, total;
1670 int i;
1671
1672 /* simplistic model for one frame's bandwidth */
1673 switch (dum->gadget.speed) {
1674 case USB_SPEED_LOW:
1675 total = 8/*bytes*/ * 12/*packets*/;
1676 break;
1677 case USB_SPEED_FULL:
1678 total = 64/*bytes*/ * 19/*packets*/;
1679 break;
1680 case USB_SPEED_HIGH:
1681 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1682 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001683 case USB_SPEED_SUPER:
1684 /* Bus speed is 500000 bytes/ms, so use a little less */
1685 total = 490000;
1686 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001688 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return;
1690 }
1691
1692 /* FIXME if HZ != 1000 this will probably misbehave ... */
1693
1694 /* look at each urb queued by the host side driver */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001695 spin_lock_irqsave(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001697 if (!dum_hcd->udev) {
1698 dev_err(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 "timer fired with no URBs pending?\n");
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001700 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return;
1702 }
1703
1704 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001705 if (!ep_name[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001707 dum->ep[i].already_seen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
1709
1710restart:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001711 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 struct urb *urb;
1713 struct dummy_request *req;
1714 u8 address;
1715 struct dummy_ep *ep = NULL;
1716 int type;
Alan Stern4d2f1102007-08-24 15:40:10 -04001717 int status = -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 urb = urbp->urb;
Alan Sterneb231052007-08-21 15:40:36 -04001720 if (urb->unlinked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 goto return_urb;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001722 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
Alan Stern391eca92005-05-10 15:34:16 -04001723 continue;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001724 type = usb_pipetype(urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 /* used up this frame's non-periodic bandwidth?
1727 * FIXME there's infinite bandwidth for control and
1728 * periodic transfers ... unrealistic.
1729 */
1730 if (total <= 0 && type == PIPE_BULK)
1731 continue;
1732
1733 /* find the gadget's ep for this request (if configured) */
1734 address = usb_pipeendpoint (urb->pipe);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001735 if (usb_pipein(urb->pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 address |= USB_DIR_IN;
1737 ep = find_endpoint(dum, address);
1738 if (!ep) {
1739 /* set_configuration() disagreement */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001740 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 "no ep configured for urb %p\n",
1742 urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001743 status = -EPROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 goto return_urb;
1745 }
1746
1747 if (ep->already_seen)
1748 continue;
1749 ep->already_seen = 1;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001750 if (ep == &dum->ep[0] && urb->error_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 ep->setup_stage = 1; /* a new urb */
1752 urb->error_count = 0;
1753 }
1754 if (ep->halted && !ep->setup_stage) {
1755 /* NOTE: must not be iso! */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001756 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 ep->ep.name, urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001758 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 goto return_urb;
1760 }
1761 /* FIXME make sure both ends agree on maxpacket */
1762
1763 /* handle control requests */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001764 if (ep == &dum->ep[0] && ep->setup_stage) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 struct usb_ctrlrequest setup;
1766 int value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001768 setup = *(struct usb_ctrlrequest *) urb->setup_packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 /* paranoia, in case of stale queued data */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001770 list_for_each_entry(req, &ep->queue, queue) {
1771 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 req->req.status = -EOVERFLOW;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001773 dev_dbg(udc_dev(dum), "stale req = %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 req);
1775
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001776 spin_unlock(&dum->lock);
1777 req->req.complete(&ep->ep, &req->req);
1778 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 ep->already_seen = 0;
1780 goto restart;
1781 }
1782
1783 /* gadget driver never sees set_address or operations
1784 * on standard feature flags. some hardware doesn't
1785 * even expose them.
1786 */
1787 ep->last_io = jiffies;
1788 ep->setup_stage = 0;
1789 ep->halted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001791 value = handle_control_request(dum_hcd, urb, &setup,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001792 &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 /* gadget driver handles all other requests. block
1795 * until setup() returns; no reentrancy issues etc.
1796 */
1797 if (value > 0) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001798 spin_unlock(&dum->lock);
1799 value = dum->driver->setup(&dum->gadget,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 &setup);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001801 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 if (value >= 0) {
1804 /* no delays (max 64KB data stage) */
1805 limit = 64*1024;
1806 goto treat_control_like_bulk;
1807 }
1808 /* error, see below */
1809 }
1810
1811 if (value < 0) {
1812 if (value != -EOPNOTSUPP)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001813 dev_dbg(udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 "setup --> %d\n",
1815 value);
Alan Stern4d2f1102007-08-24 15:40:10 -04001816 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 urb->actual_length = 0;
1818 }
1819
1820 goto return_urb;
1821 }
1822
1823 /* non-control requests */
1824 limit = total;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001825 switch (usb_pipetype(urb->pipe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 case PIPE_ISOCHRONOUS:
1827 /* FIXME is it urb->interval since the last xfer?
1828 * use urb->iso_frame_desc[i].
1829 * complete whether or not ep has requests queued.
1830 * report random errors, to debug drivers.
1831 */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001832 limit = max(limit, periodic_bytes(dum, ep));
Alan Stern4d2f1102007-08-24 15:40:10 -04001833 status = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 break;
1835
1836 case PIPE_INTERRUPT:
1837 /* FIXME is it urb->interval since the last xfer?
1838 * this almost certainly polls too fast.
1839 */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001840 limit = max(limit, periodic_bytes(dum, ep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 /* FALLTHROUGH */
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 default:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001844treat_control_like_bulk:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 ep->last_io = jiffies;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001846 total = transfer(dum_hcd, urb, ep, limit, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 break;
1848 }
1849
1850 /* incomplete transfer? */
Alan Stern4d2f1102007-08-24 15:40:10 -04001851 if (status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 continue;
1853
1854return_urb:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001855 list_del(&urbp->urbp_list);
1856 kfree(urbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if (ep)
1858 ep->already_seen = ep->setup_stage = 0;
1859
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001860 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001861 spin_unlock(&dum->lock);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001862 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001863 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 goto restart;
1866 }
1867
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001868 if (list_empty(&dum_hcd->urbp_list)) {
1869 usb_put_dev(dum_hcd->udev);
1870 dum_hcd->udev = NULL;
1871 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern391eca92005-05-10 15:34:16 -04001872 /* want a 1 msec delay here */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001873 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
1875
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001876 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
1879/*-------------------------------------------------------------------------*/
1880
1881#define PORT_C_MASK \
Alan Sternc2db8b52005-04-29 16:30:48 -04001882 ((USB_PORT_STAT_C_CONNECTION \
1883 | USB_PORT_STAT_C_ENABLE \
1884 | USB_PORT_STAT_C_SUSPEND \
1885 | USB_PORT_STAT_C_OVERCURRENT \
1886 | USB_PORT_STAT_C_RESET) << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001888static int dummy_hub_status(struct usb_hcd *hcd, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001890 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 unsigned long flags;
Alan Stern391eca92005-05-10 15:34:16 -04001892 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001894 dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001896 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001897 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001898 goto done;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001899
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001900 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1901 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1902 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1903 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001904 }
1905
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001906 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 *buf = (1 << 1);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001908 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1909 dum_hcd->port_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 retval = 1;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001911 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001912 usb_hcd_resume_root_hub(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
Alan Stern391eca92005-05-10 15:34:16 -04001914done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001915 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return retval;
1917}
1918
Sebastian Andrzej Siewior3b9c1c52012-08-19 21:54:59 +02001919/* usb 3.0 root hub device descriptor */
1920struct {
1921 struct usb_bos_descriptor bos;
1922 struct usb_ss_cap_descriptor ss_cap;
1923} __packed usb3_bos_desc = {
1924
1925 .bos = {
1926 .bLength = USB_DT_BOS_SIZE,
1927 .bDescriptorType = USB_DT_BOS,
1928 .wTotalLength = cpu_to_le16(sizeof(usb3_bos_desc)),
1929 .bNumDeviceCaps = 1,
1930 },
1931 .ss_cap = {
1932 .bLength = USB_DT_USB_SS_CAP_SIZE,
1933 .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
1934 .bDevCapabilityType = USB_SS_CAP_TYPE,
1935 .wSpeedSupported = cpu_to_le16(USB_5GBPS_OPERATION),
1936 .bFunctionalitySupport = ilog2(USB_5GBPS_OPERATION),
1937 },
1938};
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940static inline void
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001941ss_hub_descriptor(struct usb_hub_descriptor *desc)
1942{
1943 memset(desc, 0, sizeof *desc);
1944 desc->bDescriptorType = 0x2a;
1945 desc->bDescLength = 12;
1946 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1947 desc->bNbrPorts = 1;
1948 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1949 desc->u.ss.DeviceRemovable = 0xffff;
1950}
1951
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001952static inline void hub_descriptor(struct usb_hub_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001954 memset(desc, 0, sizeof *desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 desc->bDescriptorType = 0x29;
1956 desc->bDescLength = 9;
Al Virofd05e722008-04-28 07:00:16 +01001957 desc->wHubCharacteristics = cpu_to_le16(0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 desc->bNbrPorts = 1;
John Youndbe79bb2001-09-17 00:00:00 -07001959 desc->u.hs.DeviceRemovable[0] = 0xff;
1960 desc->u.hs.DeviceRemovable[1] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001963static int dummy_hub_control(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 struct usb_hcd *hcd,
1965 u16 typeReq,
1966 u16 wValue,
1967 u16 wIndex,
1968 char *buf,
1969 u16 wLength
1970) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001971 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 int retval = 0;
1973 unsigned long flags;
1974
Alan Stern541c7d42010-06-22 16:39:10 -04001975 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001976 return -ETIMEDOUT;
1977
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001978 dum_hcd = hcd_to_dummy_hcd(hcd);
1979
1980 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 switch (typeReq) {
1982 case ClearHubFeature:
1983 break;
1984 case ClearPortFeature:
1985 switch (wValue) {
1986 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001987 if (hcd->speed == HCD_USB3) {
1988 dev_dbg(dummy_dev(dum_hcd),
1989 "USB_PORT_FEAT_SUSPEND req not "
1990 "supported for USB 3.0 roothub\n");
1991 goto error;
1992 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001993 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 /* 20msec resume signaling */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001995 dum_hcd->resuming = 1;
1996 dum_hcd->re_timeout = jiffies +
Alan Sternf1c39fa2005-05-03 16:24:04 -04001997 msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
1999 break;
2000 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002001 if (hcd->speed == HCD_USB3) {
2002 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
2003 dev_dbg(dummy_dev(dum_hcd),
2004 "power-off\n");
2005 } else
2006 if (dum_hcd->port_status &
2007 USB_SS_PORT_STAT_POWER)
2008 dev_dbg(dummy_dev(dum_hcd),
2009 "power-off\n");
Alan Sternf1c39fa2005-05-03 16:24:04 -04002010 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002012 dum_hcd->port_status &= ~(1 << wValue);
2013 set_link_state(dum_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
2015 break;
2016 case GetHubDescriptor:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002017 if (hcd->speed == HCD_USB3 &&
2018 (wLength < USB_DT_SS_HUB_SIZE ||
2019 wValue != (USB_DT_SS_HUB << 8))) {
2020 dev_dbg(dummy_dev(dum_hcd),
2021 "Wrong hub descriptor type for "
2022 "USB 3.0 roothub.\n");
2023 goto error;
2024 }
2025 if (hcd->speed == HCD_USB3)
2026 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
2027 else
2028 hub_descriptor((struct usb_hub_descriptor *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 break;
Sebastian Andrzej Siewior3b9c1c52012-08-19 21:54:59 +02002030
2031 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
2032 if (hcd->speed != HCD_USB3)
2033 goto error;
2034
2035 if ((wValue >> 8) != USB_DT_BOS)
2036 goto error;
2037
2038 memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc));
2039 retval = sizeof(usb3_bos_desc);
2040 break;
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 case GetHubStatus:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002043 *(__le32 *) buf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 break;
2045 case GetPortStatus:
2046 if (wIndex != 1)
2047 retval = -EPIPE;
2048
2049 /* whoever resets or resumes must GetPortStatus to
2050 * complete it!!
2051 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002052 if (dum_hcd->resuming &&
2053 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2054 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
2055 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002057 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
2058 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2059 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
2060 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
2061 if (dum_hcd->dum->pullup) {
2062 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002063
2064 if (hcd->speed < HCD_USB3) {
2065 switch (dum_hcd->dum->gadget.speed) {
2066 case USB_SPEED_HIGH:
2067 dum_hcd->port_status |=
2068 USB_PORT_STAT_HIGH_SPEED;
2069 break;
2070 case USB_SPEED_LOW:
2071 dum_hcd->dum->gadget.ep0->
2072 maxpacket = 8;
2073 dum_hcd->port_status |=
2074 USB_PORT_STAT_LOW_SPEED;
2075 break;
2076 default:
2077 dum_hcd->dum->gadget.speed =
2078 USB_SPEED_FULL;
2079 break;
2080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
2082 }
2083 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002084 set_link_state(dum_hcd);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002085 ((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status);
2086 ((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 break;
2088 case SetHubFeature:
2089 retval = -EPIPE;
2090 break;
2091 case SetPortFeature:
2092 switch (wValue) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002093 case USB_PORT_FEAT_LINK_STATE:
2094 if (hcd->speed != HCD_USB3) {
2095 dev_dbg(dummy_dev(dum_hcd),
2096 "USB_PORT_FEAT_LINK_STATE req not "
2097 "supported for USB 2.0 roothub\n");
2098 goto error;
2099 }
2100 /*
2101 * Since this is dummy we don't have an actual link so
2102 * there is nothing to do for the SET_LINK_STATE cmd
2103 */
2104 break;
2105 case USB_PORT_FEAT_U1_TIMEOUT:
2106 case USB_PORT_FEAT_U2_TIMEOUT:
2107 /* TODO: add suspend/resume support! */
2108 if (hcd->speed != HCD_USB3) {
2109 dev_dbg(dummy_dev(dum_hcd),
2110 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
2111 "supported for USB 2.0 roothub\n");
2112 goto error;
2113 }
2114 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002116 /* Applicable only for USB2.0 hub */
2117 if (hcd->speed == HCD_USB3) {
2118 dev_dbg(dummy_dev(dum_hcd),
2119 "USB_PORT_FEAT_SUSPEND req not "
2120 "supported for USB 3.0 roothub\n");
2121 goto error;
2122 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002123 if (dum_hcd->active) {
2124 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
Alan Sternf1c39fa2005-05-03 16:24:04 -04002125
2126 /* HNP would happen here; for now we
2127 * assume b_bus_req is always true.
2128 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002129 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002130 if (((1 << USB_DEVICE_B_HNP_ENABLE)
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002131 & dum_hcd->dum->devstatus) != 0)
2132 dev_dbg(dummy_dev(dum_hcd),
Alan Stern5742b0c2005-05-02 11:25:17 -04002133 "no HNP yet!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
2135 break;
Alan Sternf1c39fa2005-05-03 16:24:04 -04002136 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002137 if (hcd->speed == HCD_USB3)
2138 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
2139 else
2140 dum_hcd->port_status |= USB_PORT_STAT_POWER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002141 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002142 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002143 case USB_PORT_FEAT_BH_PORT_RESET:
2144 /* Applicable only for USB3.0 hub */
2145 if (hcd->speed != HCD_USB3) {
2146 dev_dbg(dummy_dev(dum_hcd),
2147 "USB_PORT_FEAT_BH_PORT_RESET req not "
2148 "supported for USB 2.0 roothub\n");
2149 goto error;
2150 }
2151 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 case USB_PORT_FEAT_RESET:
Alan Sternf1c39fa2005-05-03 16:24:04 -04002153 /* if it's already enabled, disable */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002154 if (hcd->speed == HCD_USB3) {
2155 dum_hcd->port_status = 0;
2156 dum_hcd->port_status =
2157 (USB_SS_PORT_STAT_POWER |
2158 USB_PORT_STAT_CONNECTION |
2159 USB_PORT_STAT_RESET);
2160 } else
2161 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
Alan Sternf1c39fa2005-05-03 16:24:04 -04002162 | USB_PORT_STAT_LOW_SPEED
2163 | USB_PORT_STAT_HIGH_SPEED);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002164 /*
2165 * We want to reset device status. All but the
2166 * Self powered feature
2167 */
2168 dum_hcd->dum->devstatus &=
2169 (1 << USB_DEVICE_SELF_POWERED);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002170 /*
2171 * FIXME USB3.0: what is the correct reset signaling
2172 * interval? Is it still 50msec as for HS?
2173 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002174 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002175 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 default:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002177 if (hcd->speed == HCD_USB3) {
2178 if ((dum_hcd->port_status &
2179 USB_SS_PORT_STAT_POWER) != 0) {
2180 dum_hcd->port_status |= (1 << wValue);
2181 set_link_state(dum_hcd);
2182 }
2183 } else
2184 if ((dum_hcd->port_status &
2185 USB_PORT_STAT_POWER) != 0) {
2186 dum_hcd->port_status |= (1 << wValue);
2187 set_link_state(dum_hcd);
2188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 }
2190 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002191 case GetPortErrorCount:
2192 if (hcd->speed != HCD_USB3) {
2193 dev_dbg(dummy_dev(dum_hcd),
2194 "GetPortErrorCount req not "
2195 "supported for USB 2.0 roothub\n");
2196 goto error;
2197 }
2198 /* We'll always return 0 since this is a dummy hub */
2199 *(__le32 *) buf = cpu_to_le32(0);
2200 break;
2201 case SetHubDepth:
2202 if (hcd->speed != HCD_USB3) {
2203 dev_dbg(dummy_dev(dum_hcd),
2204 "SetHubDepth req not supported for "
2205 "USB 2.0 roothub\n");
2206 goto error;
2207 }
2208 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002210 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 "hub control req%04x v%04x i%04x l%d\n",
2212 typeReq, wValue, wIndex, wLength);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002213error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 /* "protocol stall" on error */
2215 retval = -EPIPE;
2216 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002217 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Stern685eb932005-05-03 16:27:26 -04002218
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002219 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002220 usb_hcd_poll_rh_status(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 return retval;
2222}
2223
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002224static int dummy_bus_suspend(struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002225{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002226 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002227
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002228 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern3cf0a222005-11-29 12:08:15 -05002229
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002230 spin_lock_irq(&dum_hcd->dum->lock);
2231 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2232 set_link_state(dum_hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002233 hcd->state = HC_STATE_SUSPENDED;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002234 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern391eca92005-05-10 15:34:16 -04002235 return 0;
2236}
2237
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002238static int dummy_bus_resume(struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002239{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002240 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002241 int rc = 0;
2242
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002243 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002244
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002245 spin_lock_irq(&dum_hcd->dum->lock);
Alan Stern541c7d42010-06-22 16:39:10 -04002246 if (!HCD_HW_ACCESSIBLE(hcd)) {
Alan Sterncfa59da2007-06-21 16:25:35 -04002247 rc = -ESHUTDOWN;
Alan Stern3cf0a222005-11-29 12:08:15 -05002248 } else {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002249 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2250 set_link_state(dum_hcd);
2251 if (!list_empty(&dum_hcd->urbp_list))
2252 mod_timer(&dum_hcd->timer, jiffies);
Alan Stern3cf0a222005-11-29 12:08:15 -05002253 hcd->state = HC_STATE_RUNNING;
2254 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002255 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern3cf0a222005-11-29 12:08:15 -05002256 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002257}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
2259/*-------------------------------------------------------------------------*/
2260
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002261static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002263 int ep = usb_pipeendpoint(urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002265 return snprintf(buf, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 "urb/%p %s ep%d%s%s len %d/%d\n",
2267 urb,
2268 ({ char *s;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002269 switch (urb->dev->speed) {
2270 case USB_SPEED_LOW:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002271 s = "ls";
2272 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002273 case USB_SPEED_FULL:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002274 s = "fs";
2275 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002276 case USB_SPEED_HIGH:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002277 s = "hs";
2278 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002279 case USB_SPEED_SUPER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002280 s = "ss";
2281 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002282 default:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002283 s = "?";
2284 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 }; s; }),
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002286 ep, ep ? (usb_pipein(urb->pipe) ? "in" : "out") : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 ({ char *s; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002288 switch (usb_pipetype(urb->pipe)) { \
2289 case PIPE_CONTROL: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002290 s = ""; \
2291 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002292 case PIPE_BULK: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002293 s = "-bulk"; \
2294 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002295 case PIPE_INTERRUPT: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002296 s = "-int"; \
2297 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002298 default: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002299 s = "-iso"; \
2300 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002301 }; s; }),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 urb->actual_length, urb->transfer_buffer_length);
2303}
2304
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002305static ssize_t show_urbs(struct device *dev, struct device_attribute *attr,
2306 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002308 struct usb_hcd *hcd = dev_get_drvdata(dev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002309 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 struct urbp *urbp;
2311 size_t size = 0;
2312 unsigned long flags;
2313
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002314 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2315 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 size_t temp;
2317
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002318 temp = show_urb(buf, PAGE_SIZE - size, urbp->urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 buf += temp;
2320 size += temp;
2321 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002322 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
2324 return size;
2325}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002326static DEVICE_ATTR(urbs, S_IRUGO, show_urbs, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002328static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2329{
2330 init_timer(&dum_hcd->timer);
2331 dum_hcd->timer.function = dummy_timer;
2332 dum_hcd->timer.data = (unsigned long)dum_hcd;
2333 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002334 dum_hcd->stream_en_ep = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002335 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2336 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2337 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2338 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2339#ifdef CONFIG_USB_OTG
2340 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2341#endif
2342 return 0;
2343
2344 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2345 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2346}
2347
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002348static int dummy_start(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002350 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
2352 /*
2353 * MASTER side init ... we emulate a root hub that'll only ever
2354 * talk to one device (the slave side). Also appears in sysfs,
2355 * just like more familiar pci-based HCDs.
2356 */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002357 if (!usb_hcd_is_primary_hcd(hcd))
2358 return dummy_start_ss(dum_hcd);
2359
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002360 spin_lock_init(&dum_hcd->dum->lock);
2361 init_timer(&dum_hcd->timer);
2362 dum_hcd->timer.function = dummy_timer;
2363 dum_hcd->timer.data = (unsigned long)dum_hcd;
2364 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002366 INIT_LIST_HEAD(&dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Alan Sterncaf29f62007-12-06 11:10:39 -05002368 hcd->power_budget = POWER_BUDGET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 hcd->state = HC_STATE_RUNNING;
Alan Stern685eb932005-05-03 16:27:26 -04002370 hcd->uses_new_polling = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Alan Stern5742b0c2005-05-02 11:25:17 -04002372#ifdef CONFIG_USB_OTG
2373 hcd->self.otg_port = 1;
2374#endif
2375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002377 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002380static void dummy_stop(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
2382 struct dummy *dum;
2383
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002384 dum = hcd_to_dummy_hcd(hcd)->dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002385 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2386 usb_gadget_unregister_driver(dum->driver);
2387 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388}
2389
2390/*-------------------------------------------------------------------------*/
2391
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002392static int dummy_h_get_frame(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002394 return dummy_g_get_frame(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395}
2396
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002397static int dummy_setup(struct usb_hcd *hcd)
2398{
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01002399 hcd->self.sg_tablesize = ~0;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002400 if (usb_hcd_is_primary_hcd(hcd)) {
2401 the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
2402 the_controller.hs_hcd->dum = &the_controller;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002403 /*
2404 * Mark the first roothub as being USB 2.0.
2405 * The USB 3.0 roothub will be registered later by
2406 * dummy_hcd_probe()
2407 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002408 hcd->speed = HCD_USB2;
2409 hcd->self.root_hub->speed = USB_SPEED_HIGH;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002410 } else {
2411 the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
2412 the_controller.ss_hcd->dum = &the_controller;
2413 hcd->speed = HCD_USB3;
2414 hcd->self.root_hub->speed = USB_SPEED_SUPER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002415 }
2416 return 0;
2417}
2418
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002419/* Change a group of bulk endpoints to support multiple stream IDs */
Sebastian Andrzej Siewiord81f3e42012-01-09 13:15:00 +01002420static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002421 struct usb_host_endpoint **eps, unsigned int num_eps,
2422 unsigned int num_streams, gfp_t mem_flags)
2423{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002424 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2425 unsigned long flags;
2426 int max_stream;
2427 int ret_streams = num_streams;
2428 unsigned int index;
2429 unsigned int i;
2430
2431 if (!num_eps)
2432 return -EINVAL;
2433
2434 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2435 for (i = 0; i < num_eps; i++) {
2436 index = dummy_get_ep_idx(&eps[i]->desc);
2437 if ((1 << index) & dum_hcd->stream_en_ep) {
2438 ret_streams = -EINVAL;
2439 goto out;
2440 }
2441 max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp);
2442 if (!max_stream) {
2443 ret_streams = -EINVAL;
2444 goto out;
2445 }
2446 if (max_stream < ret_streams) {
2447 dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u "
2448 "stream IDs.\n",
2449 eps[i]->desc.bEndpointAddress,
2450 max_stream);
2451 ret_streams = max_stream;
2452 }
2453 }
2454
2455 for (i = 0; i < num_eps; i++) {
2456 index = dummy_get_ep_idx(&eps[i]->desc);
2457 dum_hcd->stream_en_ep |= 1 << index;
2458 set_max_streams_for_pipe(dum_hcd,
2459 usb_endpoint_num(&eps[i]->desc), ret_streams);
2460 }
2461out:
2462 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2463 return ret_streams;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002464}
2465
2466/* Reverts a group of bulk endpoints back to not using stream IDs. */
Sebastian Andrzej Siewiord81f3e42012-01-09 13:15:00 +01002467static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002468 struct usb_host_endpoint **eps, unsigned int num_eps,
2469 gfp_t mem_flags)
2470{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002471 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2472 unsigned long flags;
2473 int ret;
2474 unsigned int index;
2475 unsigned int i;
2476
2477 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2478 for (i = 0; i < num_eps; i++) {
2479 index = dummy_get_ep_idx(&eps[i]->desc);
2480 if (!((1 << index) & dum_hcd->stream_en_ep)) {
2481 ret = -EINVAL;
2482 goto out;
2483 }
2484 }
2485
2486 for (i = 0; i < num_eps; i++) {
2487 index = dummy_get_ep_idx(&eps[i]->desc);
2488 dum_hcd->stream_en_ep &= ~(1 << index);
2489 set_max_streams_for_pipe(dum_hcd,
2490 usb_endpoint_num(&eps[i]->desc), 0);
2491 }
2492 ret = 0;
2493out:
2494 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2495 return ret;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002496}
2497
2498static struct hc_driver dummy_hcd = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 .description = (char *) driver_name,
2500 .product_desc = "Dummy host controller",
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002501 .hcd_priv_size = sizeof(struct dummy_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002503 .flags = HCD_USB3 | HCD_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002505 .reset = dummy_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 .start = dummy_start,
2507 .stop = dummy_stop,
2508
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002509 .urb_enqueue = dummy_urb_enqueue,
2510 .urb_dequeue = dummy_urb_dequeue,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002512 .get_frame_number = dummy_h_get_frame,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002514 .hub_status_data = dummy_hub_status,
2515 .hub_control = dummy_hub_control,
Alan Stern0c0382e2005-10-13 17:08:02 -04002516 .bus_suspend = dummy_bus_suspend,
2517 .bus_resume = dummy_bus_resume,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002518
2519 .alloc_streams = dummy_alloc_streams,
2520 .free_streams = dummy_free_streams,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521};
2522
Alan Stern8364d6b2005-11-14 12:16:30 -05002523static int dummy_hcd_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002525 struct usb_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002526 struct usb_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 int retval;
2528
Alan Stern8364d6b2005-11-14 12:16:30 -05002529 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002531 if (!mod_data.is_super_speed)
2532 dummy_hcd.flags = HCD_USB2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002533 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2534 if (!hs_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 return -ENOMEM;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002536 hs_hcd->has_tt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002538 retval = usb_add_hcd(hs_hcd, 0, 0);
Sebastian Andrzej Siewior1b68a4c2012-08-19 21:54:58 +02002539 if (retval)
2540 goto put_usb2_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002541
2542 if (mod_data.is_super_speed) {
2543 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2544 dev_name(&pdev->dev), hs_hcd);
2545 if (!ss_hcd) {
2546 retval = -ENOMEM;
2547 goto dealloc_usb2_hcd;
2548 }
2549
2550 retval = usb_add_hcd(ss_hcd, 0, 0);
2551 if (retval)
2552 goto put_usb3_hcd;
2553 }
2554 return 0;
2555
2556put_usb3_hcd:
2557 usb_put_hcd(ss_hcd);
2558dealloc_usb2_hcd:
Sebastian Andrzej Siewior1b68a4c2012-08-19 21:54:58 +02002559 usb_remove_hcd(hs_hcd);
2560put_usb2_hcd:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002561 usb_put_hcd(hs_hcd);
2562 the_controller.hs_hcd = the_controller.ss_hcd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 return retval;
2564}
2565
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002566static int dummy_hcd_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002568 struct dummy *dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002570 dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002571
2572 if (dum->ss_hcd) {
2573 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2574 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2575 }
2576
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002577 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2578 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002579
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002580 the_controller.hs_hcd = NULL;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002581 the_controller.ss_hcd = NULL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002582
Alan Sternd9b76252005-05-03 16:15:43 -04002583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002586static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state)
Alan Stern391eca92005-05-10 15:34:16 -04002587{
2588 struct usb_hcd *hcd;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002589 struct dummy_hcd *dum_hcd;
Alan Stern3cf0a222005-11-29 12:08:15 -05002590 int rc = 0;
Alan Stern391eca92005-05-10 15:34:16 -04002591
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002592 dev_dbg(&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002593
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002594 hcd = platform_get_drvdata(pdev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002595 dum_hcd = hcd_to_dummy_hcd(hcd);
2596 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern3cf0a222005-11-29 12:08:15 -05002597 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2598 rc = -EBUSY;
2599 } else
2600 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2601 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002602}
2603
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002604static int dummy_hcd_resume(struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04002605{
2606 struct usb_hcd *hcd;
2607
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002608 dev_dbg(&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002609
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002610 hcd = platform_get_drvdata(pdev);
Alan Stern3cf0a222005-11-29 12:08:15 -05002611 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002612 usb_hcd_poll_rh_status(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002613 return 0;
2614}
2615
Russell King3ae5eae2005-11-09 22:32:44 +00002616static struct platform_driver dummy_hcd_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04002617 .probe = dummy_hcd_probe,
2618 .remove = dummy_hcd_remove,
Alan Stern391eca92005-05-10 15:34:16 -04002619 .suspend = dummy_hcd_suspend,
2620 .resume = dummy_hcd_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002621 .driver = {
2622 .name = (char *) driver_name,
2623 .owner = THIS_MODULE,
2624 },
Alan Sternd9b76252005-05-03 16:15:43 -04002625};
2626
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627/*-------------------------------------------------------------------------*/
2628
Alan Sterna89a2cd2008-04-07 15:03:25 -04002629static struct platform_device *the_udc_pdev;
2630static struct platform_device *the_hcd_pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002632static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002634 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002636 if (usb_disabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 return -ENODEV;
Alan Sternd9b76252005-05-03 16:15:43 -04002638
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +03002639 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2640 return -EINVAL;
2641
Alan Sterna89a2cd2008-04-07 15:03:25 -04002642 the_hcd_pdev = platform_device_alloc(driver_name, -1);
2643 if (!the_hcd_pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 return retval;
Alan Sterna89a2cd2008-04-07 15:03:25 -04002645 the_udc_pdev = platform_device_alloc(gadget_name, -1);
2646 if (!the_udc_pdev)
2647 goto err_alloc_udc;
Alan Sternd9b76252005-05-03 16:15:43 -04002648
Alan Sterna89a2cd2008-04-07 15:03:25 -04002649 retval = platform_driver_register(&dummy_hcd_driver);
2650 if (retval < 0)
2651 goto err_register_hcd_driver;
2652 retval = platform_driver_register(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002653 if (retval < 0)
2654 goto err_register_udc_driver;
2655
Alan Sterna89a2cd2008-04-07 15:03:25 -04002656 retval = platform_device_add(the_hcd_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002657 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002658 goto err_add_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002659 if (!the_controller.hs_hcd ||
2660 (!the_controller.ss_hcd && mod_data.is_super_speed)) {
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002661 /*
2662 * The hcd was added successfully but its probe function failed
2663 * for some reason.
2664 */
2665 retval = -EINVAL;
2666 goto err_add_udc;
2667 }
Alan Sterna89a2cd2008-04-07 15:03:25 -04002668 retval = platform_device_add(the_udc_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002669 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002670 goto err_add_udc;
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002671 if (!platform_get_drvdata(the_udc_pdev)) {
2672 /*
2673 * The udc was added successfully but its probe function failed
2674 * for some reason.
2675 */
2676 retval = -EINVAL;
2677 goto err_probe_udc;
2678 }
Alan Sternd9b76252005-05-03 16:15:43 -04002679 return retval;
2680
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002681err_probe_udc:
2682 platform_device_del(the_udc_pdev);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002683err_add_udc:
2684 platform_device_del(the_hcd_pdev);
2685err_add_hcd:
2686 platform_driver_unregister(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002687err_register_udc_driver:
Alan Sterna89a2cd2008-04-07 15:03:25 -04002688 platform_driver_unregister(&dummy_hcd_driver);
2689err_register_hcd_driver:
2690 platform_device_put(the_udc_pdev);
2691err_alloc_udc:
2692 platform_device_put(the_hcd_pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 return retval;
2694}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002695module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002697static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002699 platform_device_unregister(the_udc_pdev);
2700 platform_device_unregister(the_hcd_pdev);
2701 platform_driver_unregister(&dummy_udc_driver);
2702 platform_driver_unregister(&dummy_hcd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002704module_exit(cleanup);