blob: 9a7b436bd925dc851b756fa69c3fbea040a86506 [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
Sebastian Andrzej Siewior98bea522012-11-20 13:23:15 +0100129 /* act like a pxa250: fifteen fixed function endpoints */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
131 "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
132 "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
133 "ep15in-int",
134
135 /* or like sa1100: two fixed function endpoints */
136 "ep1out-bulk", "ep2in-bulk",
Sebastian Andrzej Siewior98bea522012-11-20 13:23:15 +0100137
138 /* and now some generic EPs so we have enough in multi config */
139 "ep3out", "ep4in", "ep5out", "ep6out", "ep7in", "ep8out", "ep9in",
140 "ep10out", "ep11out", "ep12in", "ep13out", "ep14in", "ep15out",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
Tobias Klauser52950ed2005-12-11 16:20:08 +0100142#define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Alan Sternd9b76252005-05-03 16:15:43 -0400144/*-------------------------------------------------------------------------*/
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#define FIFO_SIZE 64
147
148struct urbp {
149 struct urb *urb;
150 struct list_head urbp_list;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +0100151 struct sg_mapping_iter miter;
152 u32 miter_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153};
154
Alan Stern391eca92005-05-10 15:34:16 -0400155
156enum dummy_rh_state {
157 DUMMY_RH_RESET,
158 DUMMY_RH_SUSPENDED,
159 DUMMY_RH_RUNNING
160};
161
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300162struct dummy_hcd {
163 struct dummy *dum;
164 enum dummy_rh_state rh_state;
165 struct timer_list timer;
166 u32 port_status;
167 u32 old_status;
168 unsigned long re_timeout;
169
170 struct usb_device *udev;
171 struct list_head urbp_list;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100172 u32 stream_en_ep;
173 u8 num_stream[30 / 2];
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300174
175 unsigned active:1;
176 unsigned old_active:1;
177 unsigned resuming:1;
178};
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180struct dummy {
181 spinlock_t lock;
182
183 /*
184 * SLAVE/GADGET side support
185 */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100186 struct dummy_ep ep[DUMMY_ENDPOINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 int address;
188 struct usb_gadget gadget;
189 struct usb_gadget_driver *driver;
190 struct dummy_request fifo_req;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100191 u8 fifo_buf[FIFO_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 u16 devstatus;
Alan Stern391eca92005-05-10 15:34:16 -0400193 unsigned udc_suspended:1;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400194 unsigned pullup:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 /*
197 * MASTER/HOST side support
198 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300199 struct dummy_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300200 struct dummy_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201};
202
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300203static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300205 return (struct dummy_hcd *) (hcd->hcd_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300208static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 return container_of((void *) dum, struct usb_hcd, hcd_priv);
211}
212
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300213static inline struct device *dummy_dev(struct dummy_hcd *dum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300215 return dummy_hcd_to_hcd(dum)->self.controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100218static inline struct device *udc_dev(struct dummy *dum)
Alan Sternd9b76252005-05-03 16:15:43 -0400219{
220 return dum->gadget.dev.parent;
221}
222
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100223static inline struct dummy *ep_to_dummy(struct dummy_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100225 return container_of(ep->gadget, struct dummy, gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300228static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300230 struct dummy *dum = container_of(gadget, struct dummy, gadget);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300231 if (dum->gadget.speed == USB_SPEED_SUPER)
232 return dum->ss_hcd;
233 else
234 return dum->hs_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100237static inline struct dummy *gadget_dev_to_dummy(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100239 return container_of(dev, struct dummy, gadget.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300242static struct dummy the_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244/*-------------------------------------------------------------------------*/
245
Alan Sternf1c39fa2005-05-03 16:24:04 -0400246/* SLAVE/GADGET SIDE UTILITY ROUTINES */
247
248/* called with spinlock held */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100249static void nuke(struct dummy *dum, struct dummy_ep *ep)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400250{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100251 while (!list_empty(&ep->queue)) {
Alan Sternf1c39fa2005-05-03 16:24:04 -0400252 struct dummy_request *req;
253
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100254 req = list_entry(ep->queue.next, struct dummy_request, queue);
255 list_del_init(&req->queue);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400256 req->req.status = -ESHUTDOWN;
257
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100258 spin_unlock(&dum->lock);
259 req->req.complete(&ep->ep, &req->req);
260 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400261 }
262}
263
264/* caller must hold lock */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100265static void stop_activity(struct dummy *dum)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400266{
267 struct dummy_ep *ep;
268
269 /* prevent any more requests */
270 dum->address = 0;
271
272 /* The timer is left running so that outstanding URBs can fail */
273
274 /* nuke any pending requests first, so driver i/o is quiesced */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100275 list_for_each_entry(ep, &dum->gadget.ep_list, ep.ep_list)
276 nuke(dum, ep);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400277
278 /* driver now does any non-usb quiescing necessary */
279}
280
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300281/**
282 * set_link_state_by_speed() - Sets the current state of the link according to
283 * the hcd speed
284 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
285 *
286 * This function updates the port_status according to the link state and the
287 * speed of the hcd.
288 */
289static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
290{
291 struct dummy *dum = dum_hcd->dum;
292
293 if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
294 if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
295 dum_hcd->port_status = 0;
296 } else if (!dum->pullup || dum->udc_suspended) {
297 /* UDC suspend must cause a disconnect */
298 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
299 USB_PORT_STAT_ENABLE);
300 if ((dum_hcd->old_status &
301 USB_PORT_STAT_CONNECTION) != 0)
302 dum_hcd->port_status |=
303 (USB_PORT_STAT_C_CONNECTION << 16);
304 } else {
305 /* device is connected and not suspended */
306 dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
307 USB_PORT_STAT_SPEED_5GBPS) ;
308 if ((dum_hcd->old_status &
309 USB_PORT_STAT_CONNECTION) == 0)
310 dum_hcd->port_status |=
311 (USB_PORT_STAT_C_CONNECTION << 16);
312 if ((dum_hcd->port_status &
313 USB_PORT_STAT_ENABLE) == 1 &&
314 (dum_hcd->port_status &
315 USB_SS_PORT_LS_U0) == 1 &&
316 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
317 dum_hcd->active = 1;
318 }
319 } else {
320 if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
321 dum_hcd->port_status = 0;
322 } else if (!dum->pullup || dum->udc_suspended) {
323 /* UDC suspend must cause a disconnect */
324 dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
325 USB_PORT_STAT_ENABLE |
326 USB_PORT_STAT_LOW_SPEED |
327 USB_PORT_STAT_HIGH_SPEED |
328 USB_PORT_STAT_SUSPEND);
329 if ((dum_hcd->old_status &
330 USB_PORT_STAT_CONNECTION) != 0)
331 dum_hcd->port_status |=
332 (USB_PORT_STAT_C_CONNECTION << 16);
333 } else {
334 dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
335 if ((dum_hcd->old_status &
336 USB_PORT_STAT_CONNECTION) == 0)
337 dum_hcd->port_status |=
338 (USB_PORT_STAT_C_CONNECTION << 16);
339 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
340 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
341 else if ((dum_hcd->port_status &
342 USB_PORT_STAT_SUSPEND) == 0 &&
343 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
344 dum_hcd->active = 1;
345 }
346 }
347}
348
Alan Sternf1c39fa2005-05-03 16:24:04 -0400349/* caller must hold lock */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300350static void set_link_state(struct dummy_hcd *dum_hcd)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400351{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300352 struct dummy *dum = dum_hcd->dum;
353
354 dum_hcd->active = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300355 if (dum->pullup)
356 if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
357 dum->gadget.speed != USB_SPEED_SUPER) ||
358 (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
359 dum->gadget.speed == USB_SPEED_SUPER))
360 return;
Alan Stern391eca92005-05-10 15:34:16 -0400361
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300362 set_link_state_by_speed(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400363
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300364 if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
365 dum_hcd->active)
366 dum_hcd->resuming = 0;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400367
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300368 /* if !connected or reset */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300369 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
370 (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300371 /*
372 * We're connected and not reset (reset occurred now),
373 * and driver attached - disconnect!
374 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300375 if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300376 (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
377 dum->driver) {
378 stop_activity(dum);
379 spin_unlock(&dum->lock);
380 dum->driver->disconnect(&dum->gadget);
381 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400382 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300383 } else if (dum_hcd->active != dum_hcd->old_active) {
384 if (dum_hcd->old_active && dum->driver->suspend) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300385 spin_unlock(&dum->lock);
386 dum->driver->suspend(&dum->gadget);
387 spin_lock(&dum->lock);
388 } else if (!dum_hcd->old_active && dum->driver->resume) {
389 spin_unlock(&dum->lock);
390 dum->driver->resume(&dum->gadget);
391 spin_lock(&dum->lock);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400392 }
393 }
394
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300395 dum_hcd->old_status = dum_hcd->port_status;
396 dum_hcd->old_active = dum_hcd->active;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400397}
398
399/*-------------------------------------------------------------------------*/
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401/* SLAVE/GADGET SIDE DRIVER
402 *
403 * This only tracks gadget state. All the work is done when the host
404 * side tries some (emulated) i/o operation. Real device controller
405 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
406 */
407
408#define is_enabled(dum) \
409 (dum->port_status & USB_PORT_STAT_ENABLE)
410
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100411static int dummy_enable(struct usb_ep *_ep,
412 const struct usb_endpoint_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300415 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 struct dummy_ep *ep;
417 unsigned max;
418 int retval;
419
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100420 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (!_ep || !desc || ep->desc || _ep->name == ep0name
422 || desc->bDescriptorType != USB_DT_ENDPOINT)
423 return -EINVAL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100424 dum = ep_to_dummy(ep);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300425 if (!dum->driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return -ESHUTDOWN;
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200427
428 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300429 if (!is_enabled(dum_hcd))
430 return -ESHUTDOWN;
431
432 /*
433 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
434 * maximum packet size.
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300435 * For SS devices the wMaxPacketSize is limited by 1024.
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300436 */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700437 max = usb_endpoint_maxp(desc) & 0x7ff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 /* drivers must not request bad settings, since lower levels
440 * (hardware or its drivers) may not check. some endpoints
441 * can't do iso, many have maxpacket limitations, etc.
442 *
443 * since this "hardware" driver is here to help debugging, we
444 * have some extra sanity checks. (there could be more though,
445 * especially for "ep9out" style fixed function ones.)
446 */
447 retval = -EINVAL;
Sebastian Andrzej Siewior59f08e62012-01-12 12:53:14 +0100448 switch (usb_endpoint_type(desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 case USB_ENDPOINT_XFER_BULK:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100450 if (strstr(ep->ep.name, "-iso")
451 || strstr(ep->ep.name, "-int")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 goto done;
453 }
454 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300455 case USB_SPEED_SUPER:
456 if (max == 1024)
457 break;
458 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 case USB_SPEED_HIGH:
460 if (max == 512)
461 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700462 goto done;
463 case USB_SPEED_FULL:
464 if (max == 8 || max == 16 || max == 32 || max == 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /* we'll fake any legal size */
466 break;
Ingo van Lil9063ff42008-03-28 14:50:26 -0700467 /* save a return statement */
468 default:
469 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471 break;
472 case USB_ENDPOINT_XFER_INT:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100473 if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 goto done;
475 /* real hardware might not handle all packet sizes */
476 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300477 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 case USB_SPEED_HIGH:
479 if (max <= 1024)
480 break;
481 /* save a return statement */
482 case USB_SPEED_FULL:
483 if (max <= 64)
484 break;
485 /* save a return statement */
486 default:
487 if (max <= 8)
488 break;
489 goto done;
490 }
491 break;
492 case USB_ENDPOINT_XFER_ISOC:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100493 if (strstr(ep->ep.name, "-bulk")
494 || strstr(ep->ep.name, "-int"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 goto done;
496 /* real hardware might not handle all packet sizes */
497 switch (dum->gadget.speed) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +0300498 case USB_SPEED_SUPER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 case USB_SPEED_HIGH:
500 if (max <= 1024)
501 break;
502 /* save a return statement */
503 case USB_SPEED_FULL:
504 if (max <= 1023)
505 break;
506 /* save a return statement */
507 default:
508 goto done;
509 }
510 break;
511 default:
512 /* few chips support control except on ep0 */
513 goto done;
514 }
515
516 _ep->maxpacket = max;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100517 if (usb_ss_max_streams(_ep->comp_desc)) {
518 if (!usb_endpoint_xfer_bulk(desc)) {
519 dev_err(udc_dev(dum), "Can't enable stream support on "
520 "non-bulk ep %s\n", _ep->name);
521 return -EINVAL;
522 }
523 ep->stream_en = 1;
524 }
Sebastian Andrzej Siewior3cf0ad02012-01-25 11:51:19 +0100525 ep->desc = desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100527 dev_dbg(udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 _ep->name,
529 desc->bEndpointAddress & 0x0f,
530 (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
531 ({ char *val;
Sebastian Andrzej Siewior59f08e62012-01-12 12:53:14 +0100532 switch (usb_endpoint_type(desc)) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +0300533 case USB_ENDPOINT_XFER_BULK:
534 val = "bulk";
535 break;
536 case USB_ENDPOINT_XFER_ISOC:
537 val = "iso";
538 break;
539 case USB_ENDPOINT_XFER_INT:
540 val = "intr";
541 break;
542 default:
543 val = "ctrl";
544 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }; val; }),
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100546 max, ep->stream_en ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 /* at this point real hardware should be NAKing transfers
549 * to that endpoint, until a buffer is queued to it.
550 */
Alan Stern851a5262008-08-14 15:48:30 -0400551 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 retval = 0;
553done:
554 return retval;
555}
556
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100557static int dummy_disable(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 struct dummy_ep *ep;
560 struct dummy *dum;
561 unsigned long flags;
562 int retval;
563
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100564 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (!_ep || !ep->desc || _ep->name == ep0name)
566 return -EINVAL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100567 dum = ep_to_dummy(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100569 spin_lock_irqsave(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 ep->desc = NULL;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +0100571 ep->stream_en = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 retval = 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100573 nuke(dum, ep);
574 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100576 dev_dbg(udc_dev(dum), "disabled %s\n", _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return retval;
578}
579
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100580static struct usb_request *dummy_alloc_request(struct usb_ep *_ep,
581 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 struct dummy_ep *ep;
584 struct dummy_request *req;
585
586 if (!_ep)
587 return NULL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100588 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800590 req = kzalloc(sizeof(*req), mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (!req)
592 return NULL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100593 INIT_LIST_HEAD(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return &req->req;
595}
596
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100597static void dummy_free_request(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 struct dummy_ep *ep;
600 struct dummy_request *req;
601
Sebastian Andrzej Siewior20edfbb2012-01-25 15:18:58 +0100602 if (!_ep || !_req)
603 return;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100604 ep = usb_ep_to_dummy_ep(_ep);
Sebastian Andrzej Siewior20edfbb2012-01-25 15:18:58 +0100605 if (!ep->desc && _ep->name != ep0name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return;
607
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100608 req = usb_request_to_dummy_request(_req);
609 WARN_ON(!list_empty(&req->queue));
610 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100613static void fifo_complete(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615}
616
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100617static int dummy_queue(struct usb_ep *_ep, struct usb_request *_req,
Al Viro55016f12005-10-21 03:21:58 -0400618 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 struct dummy_ep *ep;
621 struct dummy_request *req;
622 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300623 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 unsigned long flags;
625
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100626 req = usb_request_to_dummy_request(_req);
627 if (!_req || !list_empty(&req->queue) || !_req->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return -EINVAL;
629
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100630 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (!_ep || (!ep->desc && _ep->name != ep0name))
632 return -EINVAL;
633
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100634 dum = ep_to_dummy(ep);
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200635 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300636 if (!dum->driver || !is_enabled(dum_hcd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return -ESHUTDOWN;
638
639#if 0
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100640 dev_dbg(udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 ep, _req, _ep->name, _req->length, _req->buf);
642#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 _req->status = -EINPROGRESS;
644 _req->actual = 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100645 spin_lock_irqsave(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 /* implement an emulated single-request FIFO */
648 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100649 list_empty(&dum->fifo_req.queue) &&
650 list_empty(&ep->queue) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 _req->length <= FIFO_SIZE) {
652 req = &dum->fifo_req;
653 req->req = *_req;
654 req->req.buf = dum->fifo_buf;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100655 memcpy(dum->fifo_buf, _req->buf, _req->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 req->req.context = dum;
657 req->req.complete = fifo_complete;
658
David Brownellc728df72008-07-26 08:06:24 -0700659 list_add_tail(&req->queue, &ep->queue);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100660 spin_unlock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 _req->actual = _req->length;
662 _req->status = 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100663 _req->complete(_ep, _req);
664 spin_lock(&dum->lock);
David Brownellc728df72008-07-26 08:06:24 -0700665 } else
666 list_add_tail(&req->queue, &ep->queue);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100667 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 /* real hardware would likely enable transfers here, in case
670 * it'd been left NAKing.
671 */
672 return 0;
673}
674
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100675static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 struct dummy_ep *ep;
678 struct dummy *dum;
679 int retval = -EINVAL;
680 unsigned long flags;
681 struct dummy_request *req = NULL;
682
683 if (!_ep || !_req)
684 return retval;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100685 ep = usb_ep_to_dummy_ep(_ep);
686 dum = ep_to_dummy(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 if (!dum->driver)
689 return -ESHUTDOWN;
690
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100691 local_irq_save(flags);
692 spin_lock(&dum->lock);
693 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (&req->req == _req) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100695 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 _req->status = -ECONNRESET;
697 retval = 0;
698 break;
699 }
700 }
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100701 spin_unlock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 if (retval == 0) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100704 dev_dbg(udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 "dequeued req %p from %s, len %d buf %p\n",
706 req, _ep->name, _req->length, _req->buf);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100707 _req->complete(_ep, _req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100709 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return retval;
711}
712
713static int
Alan Stern851a5262008-08-14 15:48:30 -0400714dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 struct dummy_ep *ep;
717 struct dummy *dum;
718
719 if (!_ep)
720 return -EINVAL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100721 ep = usb_ep_to_dummy_ep(_ep);
722 dum = ep_to_dummy(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (!dum->driver)
724 return -ESHUTDOWN;
725 if (!value)
Alan Stern851a5262008-08-14 15:48:30 -0400726 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100728 !list_empty(&ep->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return -EAGAIN;
Alan Stern851a5262008-08-14 15:48:30 -0400730 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 ep->halted = 1;
Alan Stern851a5262008-08-14 15:48:30 -0400732 if (wedged)
733 ep->wedged = 1;
734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 /* FIXME clear emulated data toggle too */
736 return 0;
737}
738
Alan Stern851a5262008-08-14 15:48:30 -0400739static int
740dummy_set_halt(struct usb_ep *_ep, int value)
741{
742 return dummy_set_halt_and_wedge(_ep, value, 0);
743}
744
745static int dummy_set_wedge(struct usb_ep *_ep)
746{
747 if (!_ep || _ep->name == ep0name)
748 return -EINVAL;
749 return dummy_set_halt_and_wedge(_ep, 1, 1);
750}
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752static const struct usb_ep_ops dummy_ep_ops = {
753 .enable = dummy_enable,
754 .disable = dummy_disable,
755
756 .alloc_request = dummy_alloc_request,
757 .free_request = dummy_free_request,
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 .queue = dummy_queue,
760 .dequeue = dummy_dequeue,
761
762 .set_halt = dummy_set_halt,
Alan Stern851a5262008-08-14 15:48:30 -0400763 .set_wedge = dummy_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764};
765
766/*-------------------------------------------------------------------------*/
767
768/* there are both host and device side versions of this call ... */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100769static int dummy_g_get_frame(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 struct timeval tv;
772
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100773 do_gettimeofday(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return tv.tv_usec / 1000;
775}
776
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100777static int dummy_wakeup(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300779 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300781 dum_hcd = gadget_to_dummy_hcd(_gadget);
782 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
Alan Stern5742b0c2005-05-02 11:25:17 -0400783 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return -EINVAL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300785 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
Alan Stern391eca92005-05-10 15:34:16 -0400786 return -ENOLINK;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300787 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
788 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -0400789 return -EIO;
790
791 /* FIXME: What if the root hub is suspended but the port isn't? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 /* hub notices our request, issues downstream resume, etc */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300794 dum_hcd->resuming = 1;
795 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
796 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return 0;
798}
799
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100800static int dummy_set_selfpowered(struct usb_gadget *_gadget, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 struct dummy *dum;
803
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100804 dum = gadget_to_dummy_hcd(_gadget)->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (value)
806 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
807 else
808 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
809 return 0;
810}
811
Sebastian Andrzej Siewiord2621272012-01-09 13:14:59 +0100812static void dummy_udc_update_ep0(struct dummy *dum)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200813{
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100814 if (dum->gadget.speed == USB_SPEED_SUPER)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200815 dum->ep[0].ep.maxpacket = 9;
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100816 else
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200817 dum->ep[0].ep.maxpacket = 64;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200818}
819
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100820static int dummy_pullup(struct usb_gadget *_gadget, int value)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400821{
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200822 struct dummy_hcd *dum_hcd;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400823 struct dummy *dum;
824 unsigned long flags;
825
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200826 dum = gadget_dev_to_dummy(&_gadget->dev);
827
828 if (value && dum->driver) {
829 if (mod_data.is_super_speed)
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100830 dum->gadget.speed = dum->driver->max_speed;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200831 else if (mod_data.is_high_speed)
832 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100833 dum->driver->max_speed);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200834 else
835 dum->gadget.speed = USB_SPEED_FULL;
Sebastian Andrzej Siewiord2621272012-01-09 13:14:59 +0100836 dummy_udc_update_ep0(dum);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200837
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100838 if (dum->gadget.speed < dum->driver->max_speed)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200839 dev_dbg(udc_dev(dum), "This device can perform faster"
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100840 " if you connect it to a %s port...\n",
841 usb_speed_string(dum->driver->max_speed));
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200842 }
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200843 dum_hcd = gadget_to_dummy_hcd(_gadget);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200844
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100845 spin_lock_irqsave(&dum->lock, flags);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400846 dum->pullup = (value != 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200847 set_link_state(dum_hcd);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100848 spin_unlock_irqrestore(&dum->lock, flags);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200849
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200850 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Sternf1c39fa2005-05-03 16:24:04 -0400851 return 0;
852}
853
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200854static int dummy_udc_start(struct usb_gadget *g,
855 struct usb_gadget_driver *driver);
856static int dummy_udc_stop(struct usb_gadget *g,
857 struct usb_gadget_driver *driver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859static const struct usb_gadget_ops dummy_ops = {
860 .get_frame = dummy_g_get_frame,
861 .wakeup = dummy_wakeup,
862 .set_selfpowered = dummy_set_selfpowered,
Alan Sternf1c39fa2005-05-03 16:24:04 -0400863 .pullup = dummy_pullup,
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200864 .udc_start = dummy_udc_start,
865 .udc_stop = dummy_udc_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866};
867
868/*-------------------------------------------------------------------------*/
869
870/* "function" sysfs attribute */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100871static ssize_t show_function(struct device *dev, struct device_attribute *attr,
872 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100874 struct dummy *dum = gadget_dev_to_dummy(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 if (!dum->driver || !dum->driver->function)
877 return 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100878 return scnprintf(buf, PAGE_SIZE, "%s\n", dum->driver->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100880static DEVICE_ATTR(function, S_IRUGO, show_function, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882/*-------------------------------------------------------------------------*/
883
884/*
885 * Driver registration/unregistration.
886 *
887 * This is basically hardware-specific; there's usually only one real USB
888 * device (not host) controller since that's how USB devices are intended
889 * to work. So most implementations of these api calls will rely on the
890 * fact that only one driver will ever bind to the hardware. But curious
891 * hardware can be built with discrete components, so the gadget API doesn't
892 * require that assumption.
893 *
894 * For this emulator, it might be convenient to create a usb slave device
895 * for each driver that registers: just add to a big root hub.
896 */
897
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200898static int dummy_udc_start(struct usb_gadget *g,
899 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200901 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
902 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100904 if (driver->max_speed == USB_SPEED_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return -EINVAL;
906
907 /*
908 * SLAVE side init ... the layer above hardware, which
909 * can't enumerate without help from the driver we're binding.
910 */
Alan Stern5742b0c2005-05-02 11:25:17 -0400911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 dum->devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 dum->driver = driver;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100915 dev_dbg(udc_dev(dum), "binding gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return 0;
918}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200920static int dummy_udc_stop(struct usb_gadget *g,
921 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200923 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
924 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100926 dev_dbg(udc_dev(dum), "unregister gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 driver->driver.name);
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 dum->driver = NULL;
Sebastian Andrzej Siewior25427872011-06-16 20:36:55 +0200930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return 0;
932}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934#undef is_enabled
935
Alan Sternd9b76252005-05-03 16:15:43 -0400936/* The gadget structure is stored inside the hcd structure and will be
937 * released along with it. */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100938static void dummy_gadget_release(struct device *dev)
Alan Sternd9b76252005-05-03 16:15:43 -0400939{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300940 return;
Alan Sternd9b76252005-05-03 16:15:43 -0400941}
942
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200943static void init_dummy_udc_hw(struct dummy *dum)
944{
945 int i;
946
947 INIT_LIST_HEAD(&dum->gadget.ep_list);
948 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
949 struct dummy_ep *ep = &dum->ep[i];
950
951 if (!ep_name[i])
952 break;
953 ep->ep.name = ep_name[i];
954 ep->ep.ops = &dummy_ep_ops;
955 list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
956 ep->halted = ep->wedged = ep->already_seen =
957 ep->setup_stage = 0;
958 ep->ep.maxpacket = ~0;
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100959 ep->ep.max_streams = 16;
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200960 ep->last_io = jiffies;
961 ep->gadget = &dum->gadget;
962 ep->desc = NULL;
963 INIT_LIST_HEAD(&ep->queue);
964 }
965
966 dum->gadget.ep0 = &dum->ep[0].ep;
967 list_del_init(&dum->ep[0].ep.ep_list);
968 INIT_LIST_HEAD(&dum->fifo_req.queue);
Sebastian Andrzej Siewiorf8744d42011-06-23 14:26:13 +0200969
970#ifdef CONFIG_USB_OTG
971 dum->gadget.is_otg = 1;
972#endif
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200973}
974
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100975static int dummy_udc_probe(struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -0400976{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300977 struct dummy *dum = &the_controller;
Alan Sternd9b76252005-05-03 16:15:43 -0400978 int rc;
979
980 dum->gadget.name = gadget_name;
981 dum->gadget.ops = &dummy_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100982 dum->gadget.max_speed = USB_SPEED_SUPER;
Alan Sternd9b76252005-05-03 16:15:43 -0400983
Kay Sievers0031a062008-05-02 06:02:41 +0200984 dev_set_name(&dum->gadget.dev, "gadget");
Alan Stern8364d6b2005-11-14 12:16:30 -0500985 dum->gadget.dev.parent = &pdev->dev;
Alan Sternd9b76252005-05-03 16:15:43 -0400986 dum->gadget.dev.release = dummy_gadget_release;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100987 rc = device_register(&dum->gadget.dev);
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530988 if (rc < 0) {
989 put_device(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -0400990 return rc;
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530991 }
Alan Sternd9b76252005-05-03 16:15:43 -0400992
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200993 init_dummy_udc_hw(dum);
994
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300995 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
996 if (rc < 0)
997 goto err_udc;
998
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100999 rc = device_create_file(&dum->gadget.dev, &dev_attr_function);
Alan Sternefd54a32006-09-25 11:55:56 -04001000 if (rc < 0)
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001001 goto err_dev;
1002 platform_set_drvdata(pdev, dum);
1003 return rc;
1004
1005err_dev:
1006 usb_del_gadget_udc(&dum->gadget);
1007err_udc:
1008 device_unregister(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -04001009 return rc;
1010}
1011
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001012static int dummy_udc_remove(struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -04001013{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001014 struct dummy *dum = platform_get_drvdata(pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04001015
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001016 usb_del_gadget_udc(&dum->gadget);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001017 platform_set_drvdata(pdev, NULL);
1018 device_remove_file(&dum->gadget.dev, &dev_attr_function);
1019 device_unregister(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -04001020 return 0;
1021}
1022
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001023static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1024 int suspend)
Alan Stern391eca92005-05-10 15:34:16 -04001025{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001026 spin_lock_irq(&dum->lock);
1027 dum->udc_suspended = suspend;
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001028 set_link_state(dum_hcd);
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001029 spin_unlock_irq(&dum->lock);
1030}
Alan Stern391eca92005-05-10 15:34:16 -04001031
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001032static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1033{
1034 struct dummy *dum = platform_get_drvdata(pdev);
1035 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1036
1037 dev_dbg(&pdev->dev, "%s\n", __func__);
1038 dummy_udc_pm(dum, dum_hcd, 1);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001039 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001040 return 0;
1041}
1042
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001043static int dummy_udc_resume(struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04001044{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001045 struct dummy *dum = platform_get_drvdata(pdev);
1046 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Alan Stern391eca92005-05-10 15:34:16 -04001047
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001048 dev_dbg(&pdev->dev, "%s\n", __func__);
1049 dummy_udc_pm(dum, dum_hcd, 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001050 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001051 return 0;
1052}
1053
Russell King3ae5eae2005-11-09 22:32:44 +00001054static struct platform_driver dummy_udc_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04001055 .probe = dummy_udc_probe,
1056 .remove = dummy_udc_remove,
Alan Stern391eca92005-05-10 15:34:16 -04001057 .suspend = dummy_udc_suspend,
1058 .resume = dummy_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001059 .driver = {
1060 .name = (char *) gadget_name,
1061 .owner = THIS_MODULE,
1062 },
Alan Sternd9b76252005-05-03 16:15:43 -04001063};
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065/*-------------------------------------------------------------------------*/
1066
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001067static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc)
1068{
1069 unsigned int index;
1070
1071 index = usb_endpoint_num(desc) << 1;
1072 if (usb_endpoint_dir_in(desc))
1073 index |= 1;
1074 return index;
1075}
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077/* MASTER/HOST SIDE DRIVER
1078 *
1079 * this uses the hcd framework to hook up to host side drivers.
1080 * its root hub will only have one device, otherwise it acts like
1081 * a normal host controller.
1082 *
1083 * when urbs are queued, they're just stuck on a list that we
1084 * scan in a timer callback. that callback connects writes from
1085 * the host with reads from the device, and so on, based on the
1086 * usb 2.0 rules.
1087 */
1088
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001089static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb)
1090{
1091 const struct usb_endpoint_descriptor *desc = &urb->ep->desc;
1092 u32 index;
1093
1094 if (!usb_endpoint_xfer_bulk(desc))
1095 return 0;
1096
1097 index = dummy_get_ep_idx(desc);
1098 return (1 << index) & dum_hcd->stream_en_ep;
1099}
1100
1101/*
1102 * The max stream number is saved as a nibble so for the 30 possible endpoints
1103 * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
1104 * means we use only 1 stream). The maximum according to the spec is 16bit so
1105 * if the 16 stream limit is about to go, the array size should be incremented
1106 * to 30 elements of type u16.
1107 */
1108static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1109 unsigned int pipe)
1110{
1111 int max_streams;
1112
1113 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1114 if (usb_pipeout(pipe))
1115 max_streams >>= 4;
1116 else
1117 max_streams &= 0xf;
1118 max_streams++;
1119 return max_streams;
1120}
1121
1122static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1123 unsigned int pipe, unsigned int streams)
1124{
1125 int max_streams;
1126
1127 streams--;
1128 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1129 if (usb_pipeout(pipe)) {
1130 streams <<= 4;
1131 max_streams &= 0xf;
1132 } else {
1133 max_streams &= 0xf0;
1134 }
1135 max_streams |= streams;
1136 dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams;
1137}
1138
1139static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb)
1140{
1141 unsigned int max_streams;
1142 int enabled;
1143
1144 enabled = dummy_ep_stream_en(dum_hcd, urb);
1145 if (!urb->stream_id) {
1146 if (enabled)
1147 return -EINVAL;
1148 return 0;
1149 }
1150 if (!enabled)
1151 return -EINVAL;
1152
1153 max_streams = get_max_streams_for_pipe(dum_hcd,
1154 usb_pipeendpoint(urb->pipe));
1155 if (urb->stream_id > max_streams) {
1156 dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n",
1157 urb->stream_id);
1158 BUG();
1159 return -EINVAL;
1160 }
1161 return 0;
1162}
1163
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001164static int dummy_urb_enqueue(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 struct usb_hcd *hcd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001167 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001169 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 struct urbp *urbp;
1171 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001172 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001174 urbp = kmalloc(sizeof *urbp, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (!urbp)
1176 return -ENOMEM;
1177 urbp->urb = urb;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001178 urbp->miter_started = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001180 dum_hcd = hcd_to_dummy_hcd(hcd);
1181 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001182
1183 rc = dummy_validate_stream(dum_hcd, urb);
1184 if (rc) {
1185 kfree(urbp);
1186 goto done;
1187 }
1188
Alan Sterne9df41c2007-08-08 11:48:02 -04001189 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1190 if (rc) {
1191 kfree(urbp);
1192 goto done;
1193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001195 if (!dum_hcd->udev) {
1196 dum_hcd->udev = urb->dev;
1197 usb_get_dev(dum_hcd->udev);
1198 } else if (unlikely(dum_hcd->udev != urb->dev))
1199 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001201 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 urb->hcpriv = urbp;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001203 if (usb_pipetype(urb->pipe) == PIPE_CONTROL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 urb->error_count = 1; /* mark as a new urb */
1205
1206 /* kick the scheduler, it'll do the rest */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001207 if (!timer_pending(&dum_hcd->timer))
1208 mod_timer(&dum_hcd->timer, jiffies + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Alan Sterne9df41c2007-08-08 11:48:02 -04001210 done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001211 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001212 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
Alan Sterne9df41c2007-08-08 11:48:02 -04001215static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001217 struct dummy_hcd *dum_hcd;
Alan Stern391eca92005-05-10 15:34:16 -04001218 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001219 int rc;
Alan Stern391eca92005-05-10 15:34:16 -04001220
1221 /* giveback happens automatically in timer callback,
1222 * so make sure the callback happens */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001223 dum_hcd = hcd_to_dummy_hcd(hcd);
1224 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001225
1226 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001227 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1228 !list_empty(&dum_hcd->urbp_list))
1229 mod_timer(&dum_hcd->timer, jiffies);
Alan Sterne9df41c2007-08-08 11:48:02 -04001230
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001231 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001232 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001235static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
1236 u32 len)
1237{
1238 void *ubuf, *rbuf;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001239 struct urbp *urbp = urb->hcpriv;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001240 int to_host;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001241 struct sg_mapping_iter *miter = &urbp->miter;
1242 u32 trans = 0;
1243 u32 this_sg;
1244 bool next_sg;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001245
1246 to_host = usb_pipein(urb->pipe);
1247 rbuf = req->req.buf + req->req.actual;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001248
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001249 if (!urb->num_sgs) {
1250 ubuf = urb->transfer_buffer + urb->actual_length;
1251 if (to_host)
1252 memcpy(ubuf, rbuf, len);
1253 else
1254 memcpy(rbuf, ubuf, len);
1255 return len;
1256 }
1257
1258 if (!urbp->miter_started) {
1259 u32 flags = SG_MITER_ATOMIC;
1260
1261 if (to_host)
1262 flags |= SG_MITER_TO_SG;
1263 else
1264 flags |= SG_MITER_FROM_SG;
1265
1266 sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
1267 urbp->miter_started = 1;
1268 }
1269 next_sg = sg_miter_next(miter);
1270 if (next_sg == false) {
1271 WARN_ON_ONCE(1);
1272 return -EINVAL;
1273 }
1274 do {
1275 ubuf = miter->addr;
1276 this_sg = min_t(u32, len, miter->length);
1277 miter->consumed = this_sg;
1278 trans += this_sg;
1279
1280 if (to_host)
1281 memcpy(ubuf, rbuf, this_sg);
1282 else
1283 memcpy(rbuf, ubuf, this_sg);
1284 len -= this_sg;
1285
1286 if (!len)
1287 break;
1288 next_sg = sg_miter_next(miter);
1289 if (next_sg == false) {
1290 WARN_ON_ONCE(1);
1291 return -EINVAL;
1292 }
1293
1294 rbuf += this_sg;
1295 } while (1);
1296
1297 sg_miter_stop(miter);
1298 return trans;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001299}
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301/* transfer up to a frame's worth; caller must own lock */
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001302static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb,
1303 struct dummy_ep *ep, int limit, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001305 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 struct dummy_request *req;
1307
1308top:
1309 /* if there's no request queued, the device is NAKing; return */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001310 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 unsigned host_len, dev_len, len;
1312 int is_short, to_host;
1313 int rescan = 0;
1314
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001315 if (dummy_ep_stream_en(dum_hcd, urb)) {
1316 if ((urb->stream_id != req->req.stream_id))
1317 continue;
1318 }
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 /* 1..N packets of ep->ep.maxpacket each ... the last one
1321 * may be short (including zero length).
1322 *
1323 * writer can send a zlp explicitly (length 0) or implicitly
1324 * (length mod maxpacket zero, and 'zero' flag); they always
1325 * terminate reads.
1326 */
1327 host_len = urb->transfer_buffer_length - urb->actual_length;
1328 dev_len = req->req.length - req->req.actual;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001329 len = min(host_len, dev_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 /* FIXME update emulated data toggle too */
1332
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001333 to_host = usb_pipein(urb->pipe);
1334 if (unlikely(len == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 is_short = 1;
1336 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /* not enough bandwidth left? */
1338 if (limit < ep->ep.maxpacket && limit < len)
1339 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001340 len = min_t(unsigned, len, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (len == 0)
1342 break;
1343
1344 /* use an extra pass for the final short packet */
1345 if (len > ep->ep.maxpacket) {
1346 rescan = 1;
1347 len -= (len % ep->ep.maxpacket);
1348 }
1349 is_short = (len % ep->ep.maxpacket) != 0;
1350
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001351 len = dummy_perform_transfer(urb, req, len);
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 ep->last_io = jiffies;
Dan Carpenterb1443ac0e2012-03-02 21:51:00 +03001354 if ((int)len < 0) {
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001355 req->req.status = len;
1356 } else {
1357 limit -= len;
1358 urb->actual_length += len;
1359 req->req.actual += len;
1360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
1362
1363 /* short packets terminate, maybe with overflow/underflow.
1364 * it's only really an error to write too much.
1365 *
1366 * partially filling a buffer optionally blocks queue advances
1367 * (so completion handlers can clean up the queue) but we don't
Alan Sternb0d9efb2007-08-21 15:39:21 -04001368 * need to emulate such data-in-flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 */
1370 if (is_short) {
1371 if (host_len == dev_len) {
1372 req->req.status = 0;
Alan Stern4d2f1102007-08-24 15:40:10 -04001373 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 } else if (to_host) {
1375 req->req.status = 0;
1376 if (dev_len > host_len)
Alan Stern4d2f1102007-08-24 15:40:10 -04001377 *status = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 else
Alan Stern4d2f1102007-08-24 15:40:10 -04001379 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 } else if (!to_host) {
Alan Stern4d2f1102007-08-24 15:40:10 -04001381 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (host_len > dev_len)
1383 req->req.status = -EOVERFLOW;
1384 else
1385 req->req.status = 0;
1386 }
1387
1388 /* many requests terminate without a short packet */
1389 } else {
1390 if (req->req.length == req->req.actual
1391 && !req->req.zero)
1392 req->req.status = 0;
1393 if (urb->transfer_buffer_length == urb->actual_length
1394 && !(urb->transfer_flags
Alan Stern4d2f1102007-08-24 15:40:10 -04001395 & URB_ZERO_PACKET))
1396 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398
1399 /* device side completion --> continuable */
1400 if (req->req.status != -EINPROGRESS) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001401 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001403 spin_unlock(&dum->lock);
1404 req->req.complete(&ep->ep, &req->req);
1405 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 /* requests might have been unlinked... */
1408 rescan = 1;
1409 }
1410
1411 /* host side completion --> terminate */
Alan Stern4d2f1102007-08-24 15:40:10 -04001412 if (*status != -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 break;
1414
1415 /* rescan to continue with any other queued i/o */
1416 if (rescan)
1417 goto top;
1418 }
1419 return limit;
1420}
1421
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001422static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
1424 int limit = ep->ep.maxpacket;
1425
1426 if (dum->gadget.speed == USB_SPEED_HIGH) {
1427 int tmp;
1428
1429 /* high bandwidth mode */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001430 tmp = usb_endpoint_maxp(ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 tmp = (tmp >> 11) & 0x03;
1432 tmp *= 8 /* applies to entire frame */;
1433 limit += limit * tmp;
1434 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001435 if (dum->gadget.speed == USB_SPEED_SUPER) {
Sebastian Andrzej Siewior59f08e62012-01-12 12:53:14 +01001436 switch (usb_endpoint_type(ep->desc)) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001437 case USB_ENDPOINT_XFER_ISOC:
1438 /* Sec. 4.4.8.2 USB3.0 Spec */
1439 limit = 3 * 16 * 1024 * 8;
1440 break;
1441 case USB_ENDPOINT_XFER_INT:
1442 /* Sec. 4.4.7.2 USB3.0 Spec */
1443 limit = 3 * 1024 * 8;
1444 break;
1445 case USB_ENDPOINT_XFER_BULK:
1446 default:
1447 break;
1448 }
1449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return limit;
1451}
1452
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001453#define is_active(dum_hcd) ((dum_hcd->port_status & \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1455 USB_PORT_STAT_SUSPEND)) \
1456 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1457
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001458static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
1460 int i;
1461
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001462 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1463 dum->ss_hcd : dum->hs_hcd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return NULL;
1465 if ((address & ~USB_DIR_IN) == 0)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001466 return &dum->ep[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001468 struct dummy_ep *ep = &dum->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 if (!ep->desc)
1471 continue;
1472 if (ep->desc->bEndpointAddress == address)
1473 return ep;
1474 }
1475 return NULL;
1476}
1477
1478#undef is_active
1479
1480#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1481#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1482#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1483#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1484#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1485#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1486
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001487
1488/**
1489 * handle_control_request() - handles all control transfers
1490 * @dum: pointer to dummy (the_controller)
1491 * @urb: the urb request to handle
1492 * @setup: pointer to the setup data for a USB device control
1493 * request
1494 * @status: pointer to request handling status
1495 *
1496 * Return 0 - if the request was handled
1497 * 1 - if the request wasn't handles
1498 * error code on error
1499 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001500static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001501 struct usb_ctrlrequest *setup,
1502 int *status)
1503{
1504 struct dummy_ep *ep2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001505 struct dummy *dum = dum_hcd->dum;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001506 int ret_val = 1;
1507 unsigned w_index;
1508 unsigned w_value;
1509
1510 w_index = le16_to_cpu(setup->wIndex);
1511 w_value = le16_to_cpu(setup->wValue);
1512 switch (setup->bRequest) {
1513 case USB_REQ_SET_ADDRESS:
1514 if (setup->bRequestType != Dev_Request)
1515 break;
1516 dum->address = w_value;
1517 *status = 0;
1518 dev_dbg(udc_dev(dum), "set_address = %d\n",
1519 w_value);
1520 ret_val = 0;
1521 break;
1522 case USB_REQ_SET_FEATURE:
1523 if (setup->bRequestType == Dev_Request) {
1524 ret_val = 0;
1525 switch (w_value) {
1526 case USB_DEVICE_REMOTE_WAKEUP:
1527 break;
1528 case USB_DEVICE_B_HNP_ENABLE:
1529 dum->gadget.b_hnp_enable = 1;
1530 break;
1531 case USB_DEVICE_A_HNP_SUPPORT:
1532 dum->gadget.a_hnp_support = 1;
1533 break;
1534 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1535 dum->gadget.a_alt_hnp_support = 1;
1536 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001537 case USB_DEVICE_U1_ENABLE:
1538 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1539 HCD_USB3)
1540 w_value = USB_DEV_STAT_U1_ENABLED;
1541 else
1542 ret_val = -EOPNOTSUPP;
1543 break;
1544 case USB_DEVICE_U2_ENABLE:
1545 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1546 HCD_USB3)
1547 w_value = USB_DEV_STAT_U2_ENABLED;
1548 else
1549 ret_val = -EOPNOTSUPP;
1550 break;
1551 case USB_DEVICE_LTM_ENABLE:
1552 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1553 HCD_USB3)
1554 w_value = USB_DEV_STAT_LTM_ENABLED;
1555 else
1556 ret_val = -EOPNOTSUPP;
1557 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001558 default:
1559 ret_val = -EOPNOTSUPP;
1560 }
1561 if (ret_val == 0) {
1562 dum->devstatus |= (1 << w_value);
1563 *status = 0;
1564 }
1565 } else if (setup->bRequestType == Ep_Request) {
1566 /* endpoint halt */
1567 ep2 = find_endpoint(dum, w_index);
1568 if (!ep2 || ep2->ep.name == ep0name) {
1569 ret_val = -EOPNOTSUPP;
1570 break;
1571 }
1572 ep2->halted = 1;
1573 ret_val = 0;
1574 *status = 0;
1575 }
1576 break;
1577 case USB_REQ_CLEAR_FEATURE:
1578 if (setup->bRequestType == Dev_Request) {
1579 ret_val = 0;
1580 switch (w_value) {
1581 case USB_DEVICE_REMOTE_WAKEUP:
1582 w_value = USB_DEVICE_REMOTE_WAKEUP;
1583 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001584 case USB_DEVICE_U1_ENABLE:
1585 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1586 HCD_USB3)
1587 w_value = USB_DEV_STAT_U1_ENABLED;
1588 else
1589 ret_val = -EOPNOTSUPP;
1590 break;
1591 case USB_DEVICE_U2_ENABLE:
1592 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1593 HCD_USB3)
1594 w_value = USB_DEV_STAT_U2_ENABLED;
1595 else
1596 ret_val = -EOPNOTSUPP;
1597 break;
1598 case USB_DEVICE_LTM_ENABLE:
1599 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1600 HCD_USB3)
1601 w_value = USB_DEV_STAT_LTM_ENABLED;
1602 else
1603 ret_val = -EOPNOTSUPP;
1604 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001605 default:
1606 ret_val = -EOPNOTSUPP;
1607 break;
1608 }
1609 if (ret_val == 0) {
1610 dum->devstatus &= ~(1 << w_value);
1611 *status = 0;
1612 }
1613 } else if (setup->bRequestType == Ep_Request) {
1614 /* endpoint halt */
1615 ep2 = find_endpoint(dum, w_index);
1616 if (!ep2) {
1617 ret_val = -EOPNOTSUPP;
1618 break;
1619 }
1620 if (!ep2->wedged)
1621 ep2->halted = 0;
1622 ret_val = 0;
1623 *status = 0;
1624 }
1625 break;
1626 case USB_REQ_GET_STATUS:
1627 if (setup->bRequestType == Dev_InRequest
1628 || setup->bRequestType == Intf_InRequest
1629 || setup->bRequestType == Ep_InRequest) {
1630 char *buf;
1631 /*
1632 * device: remote wakeup, selfpowered
1633 * interface: nothing
1634 * endpoint: halt
1635 */
1636 buf = (char *)urb->transfer_buffer;
1637 if (urb->transfer_buffer_length > 0) {
1638 if (setup->bRequestType == Ep_InRequest) {
1639 ep2 = find_endpoint(dum, w_index);
1640 if (!ep2) {
1641 ret_val = -EOPNOTSUPP;
1642 break;
1643 }
1644 buf[0] = ep2->halted;
1645 } else if (setup->bRequestType ==
1646 Dev_InRequest) {
1647 buf[0] = (u8)dum->devstatus;
1648 } else
1649 buf[0] = 0;
1650 }
1651 if (urb->transfer_buffer_length > 1)
1652 buf[1] = 0;
1653 urb->actual_length = min_t(u32, 2,
1654 urb->transfer_buffer_length);
1655 ret_val = 0;
1656 *status = 0;
1657 }
1658 break;
1659 }
1660 return ret_val;
1661}
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663/* drive both sides of the transfers; looks like irq handlers to
1664 * both drivers except the callbacks aren't in_irq().
1665 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001666static void dummy_timer(unsigned long _dum_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001668 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1669 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 struct urbp *urbp, *tmp;
1671 unsigned long flags;
1672 int limit, total;
1673 int i;
1674
1675 /* simplistic model for one frame's bandwidth */
1676 switch (dum->gadget.speed) {
1677 case USB_SPEED_LOW:
1678 total = 8/*bytes*/ * 12/*packets*/;
1679 break;
1680 case USB_SPEED_FULL:
1681 total = 64/*bytes*/ * 19/*packets*/;
1682 break;
1683 case USB_SPEED_HIGH:
1684 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1685 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001686 case USB_SPEED_SUPER:
1687 /* Bus speed is 500000 bytes/ms, so use a little less */
1688 total = 490000;
1689 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001691 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 return;
1693 }
1694
1695 /* FIXME if HZ != 1000 this will probably misbehave ... */
1696
1697 /* look at each urb queued by the host side driver */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001698 spin_lock_irqsave(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001700 if (!dum_hcd->udev) {
1701 dev_err(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 "timer fired with no URBs pending?\n");
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001703 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return;
1705 }
1706
1707 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001708 if (!ep_name[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001710 dum->ep[i].already_seen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
1712
1713restart:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001714 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 struct urb *urb;
1716 struct dummy_request *req;
1717 u8 address;
1718 struct dummy_ep *ep = NULL;
1719 int type;
Alan Stern4d2f1102007-08-24 15:40:10 -04001720 int status = -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 urb = urbp->urb;
Alan Sterneb231052007-08-21 15:40:36 -04001723 if (urb->unlinked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 goto return_urb;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001725 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
Alan Stern391eca92005-05-10 15:34:16 -04001726 continue;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001727 type = usb_pipetype(urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729 /* used up this frame's non-periodic bandwidth?
1730 * FIXME there's infinite bandwidth for control and
1731 * periodic transfers ... unrealistic.
1732 */
1733 if (total <= 0 && type == PIPE_BULK)
1734 continue;
1735
1736 /* find the gadget's ep for this request (if configured) */
1737 address = usb_pipeendpoint (urb->pipe);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001738 if (usb_pipein(urb->pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 address |= USB_DIR_IN;
1740 ep = find_endpoint(dum, address);
1741 if (!ep) {
1742 /* set_configuration() disagreement */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001743 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 "no ep configured for urb %p\n",
1745 urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001746 status = -EPROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 goto return_urb;
1748 }
1749
1750 if (ep->already_seen)
1751 continue;
1752 ep->already_seen = 1;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001753 if (ep == &dum->ep[0] && urb->error_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 ep->setup_stage = 1; /* a new urb */
1755 urb->error_count = 0;
1756 }
1757 if (ep->halted && !ep->setup_stage) {
1758 /* NOTE: must not be iso! */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001759 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 ep->ep.name, urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001761 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 goto return_urb;
1763 }
1764 /* FIXME make sure both ends agree on maxpacket */
1765
1766 /* handle control requests */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001767 if (ep == &dum->ep[0] && ep->setup_stage) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 struct usb_ctrlrequest setup;
1769 int value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001771 setup = *(struct usb_ctrlrequest *) urb->setup_packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 /* paranoia, in case of stale queued data */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001773 list_for_each_entry(req, &ep->queue, queue) {
1774 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 req->req.status = -EOVERFLOW;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001776 dev_dbg(udc_dev(dum), "stale req = %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 req);
1778
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001779 spin_unlock(&dum->lock);
1780 req->req.complete(&ep->ep, &req->req);
1781 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 ep->already_seen = 0;
1783 goto restart;
1784 }
1785
1786 /* gadget driver never sees set_address or operations
1787 * on standard feature flags. some hardware doesn't
1788 * even expose them.
1789 */
1790 ep->last_io = jiffies;
1791 ep->setup_stage = 0;
1792 ep->halted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001794 value = handle_control_request(dum_hcd, urb, &setup,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001795 &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 /* gadget driver handles all other requests. block
1798 * until setup() returns; no reentrancy issues etc.
1799 */
1800 if (value > 0) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001801 spin_unlock(&dum->lock);
1802 value = dum->driver->setup(&dum->gadget,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 &setup);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001804 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 if (value >= 0) {
1807 /* no delays (max 64KB data stage) */
1808 limit = 64*1024;
1809 goto treat_control_like_bulk;
1810 }
1811 /* error, see below */
1812 }
1813
1814 if (value < 0) {
1815 if (value != -EOPNOTSUPP)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001816 dev_dbg(udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 "setup --> %d\n",
1818 value);
Alan Stern4d2f1102007-08-24 15:40:10 -04001819 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 urb->actual_length = 0;
1821 }
1822
1823 goto return_urb;
1824 }
1825
1826 /* non-control requests */
1827 limit = total;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001828 switch (usb_pipetype(urb->pipe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 case PIPE_ISOCHRONOUS:
1830 /* FIXME is it urb->interval since the last xfer?
1831 * use urb->iso_frame_desc[i].
1832 * complete whether or not ep has requests queued.
1833 * report random errors, to debug drivers.
1834 */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001835 limit = max(limit, periodic_bytes(dum, ep));
Alan Stern4d2f1102007-08-24 15:40:10 -04001836 status = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 break;
1838
1839 case PIPE_INTERRUPT:
1840 /* FIXME is it urb->interval since the last xfer?
1841 * this almost certainly polls too fast.
1842 */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001843 limit = max(limit, periodic_bytes(dum, ep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 /* FALLTHROUGH */
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 default:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001847treat_control_like_bulk:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 ep->last_io = jiffies;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001849 total = transfer(dum_hcd, urb, ep, limit, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 break;
1851 }
1852
1853 /* incomplete transfer? */
Alan Stern4d2f1102007-08-24 15:40:10 -04001854 if (status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 continue;
1856
1857return_urb:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001858 list_del(&urbp->urbp_list);
1859 kfree(urbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (ep)
1861 ep->already_seen = ep->setup_stage = 0;
1862
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001863 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001864 spin_unlock(&dum->lock);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001865 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001866 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 goto restart;
1869 }
1870
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001871 if (list_empty(&dum_hcd->urbp_list)) {
1872 usb_put_dev(dum_hcd->udev);
1873 dum_hcd->udev = NULL;
1874 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern391eca92005-05-10 15:34:16 -04001875 /* want a 1 msec delay here */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001876 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
1878
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001879 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
1882/*-------------------------------------------------------------------------*/
1883
1884#define PORT_C_MASK \
Alan Sternc2db8b52005-04-29 16:30:48 -04001885 ((USB_PORT_STAT_C_CONNECTION \
1886 | USB_PORT_STAT_C_ENABLE \
1887 | USB_PORT_STAT_C_SUSPEND \
1888 | USB_PORT_STAT_C_OVERCURRENT \
1889 | USB_PORT_STAT_C_RESET) << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001891static int dummy_hub_status(struct usb_hcd *hcd, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001893 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 unsigned long flags;
Alan Stern391eca92005-05-10 15:34:16 -04001895 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001897 dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001899 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001900 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001901 goto done;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001902
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001903 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1904 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1905 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1906 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001907 }
1908
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001909 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 *buf = (1 << 1);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001911 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1912 dum_hcd->port_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 retval = 1;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001914 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001915 usb_hcd_resume_root_hub(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
Alan Stern391eca92005-05-10 15:34:16 -04001917done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001918 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return retval;
1920}
1921
1922static inline void
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001923ss_hub_descriptor(struct usb_hub_descriptor *desc)
1924{
1925 memset(desc, 0, sizeof *desc);
1926 desc->bDescriptorType = 0x2a;
1927 desc->bDescLength = 12;
1928 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1929 desc->bNbrPorts = 1;
1930 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1931 desc->u.ss.DeviceRemovable = 0xffff;
1932}
1933
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001934static inline void hub_descriptor(struct usb_hub_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001936 memset(desc, 0, sizeof *desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 desc->bDescriptorType = 0x29;
1938 desc->bDescLength = 9;
Al Virofd05e722008-04-28 07:00:16 +01001939 desc->wHubCharacteristics = cpu_to_le16(0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 desc->bNbrPorts = 1;
John Youndbe79bb2001-09-17 00:00:00 -07001941 desc->u.hs.DeviceRemovable[0] = 0xff;
1942 desc->u.hs.DeviceRemovable[1] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001945static int dummy_hub_control(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 struct usb_hcd *hcd,
1947 u16 typeReq,
1948 u16 wValue,
1949 u16 wIndex,
1950 char *buf,
1951 u16 wLength
1952) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001953 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 int retval = 0;
1955 unsigned long flags;
1956
Alan Stern541c7d42010-06-22 16:39:10 -04001957 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001958 return -ETIMEDOUT;
1959
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001960 dum_hcd = hcd_to_dummy_hcd(hcd);
1961
1962 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 switch (typeReq) {
1964 case ClearHubFeature:
1965 break;
1966 case ClearPortFeature:
1967 switch (wValue) {
1968 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001969 if (hcd->speed == HCD_USB3) {
1970 dev_dbg(dummy_dev(dum_hcd),
1971 "USB_PORT_FEAT_SUSPEND req not "
1972 "supported for USB 3.0 roothub\n");
1973 goto error;
1974 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001975 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 /* 20msec resume signaling */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001977 dum_hcd->resuming = 1;
1978 dum_hcd->re_timeout = jiffies +
Alan Sternf1c39fa2005-05-03 16:24:04 -04001979 msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
1981 break;
1982 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001983 if (hcd->speed == HCD_USB3) {
1984 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1985 dev_dbg(dummy_dev(dum_hcd),
1986 "power-off\n");
1987 } else
1988 if (dum_hcd->port_status &
1989 USB_SS_PORT_STAT_POWER)
1990 dev_dbg(dummy_dev(dum_hcd),
1991 "power-off\n");
Alan Sternf1c39fa2005-05-03 16:24:04 -04001992 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001994 dum_hcd->port_status &= ~(1 << wValue);
1995 set_link_state(dum_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997 break;
1998 case GetHubDescriptor:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001999 if (hcd->speed == HCD_USB3 &&
2000 (wLength < USB_DT_SS_HUB_SIZE ||
2001 wValue != (USB_DT_SS_HUB << 8))) {
2002 dev_dbg(dummy_dev(dum_hcd),
2003 "Wrong hub descriptor type for "
2004 "USB 3.0 roothub.\n");
2005 goto error;
2006 }
2007 if (hcd->speed == HCD_USB3)
2008 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
2009 else
2010 hub_descriptor((struct usb_hub_descriptor *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 break;
2012 case GetHubStatus:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002013 *(__le32 *) buf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 break;
2015 case GetPortStatus:
2016 if (wIndex != 1)
2017 retval = -EPIPE;
2018
2019 /* whoever resets or resumes must GetPortStatus to
2020 * complete it!!
2021 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002022 if (dum_hcd->resuming &&
2023 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2024 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
2025 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002027 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
2028 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2029 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
2030 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
2031 if (dum_hcd->dum->pullup) {
2032 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002033
2034 if (hcd->speed < HCD_USB3) {
2035 switch (dum_hcd->dum->gadget.speed) {
2036 case USB_SPEED_HIGH:
2037 dum_hcd->port_status |=
2038 USB_PORT_STAT_HIGH_SPEED;
2039 break;
2040 case USB_SPEED_LOW:
2041 dum_hcd->dum->gadget.ep0->
2042 maxpacket = 8;
2043 dum_hcd->port_status |=
2044 USB_PORT_STAT_LOW_SPEED;
2045 break;
2046 default:
2047 dum_hcd->dum->gadget.speed =
2048 USB_SPEED_FULL;
2049 break;
2050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052 }
2053 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002054 set_link_state(dum_hcd);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002055 ((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status);
2056 ((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 break;
2058 case SetHubFeature:
2059 retval = -EPIPE;
2060 break;
2061 case SetPortFeature:
2062 switch (wValue) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002063 case USB_PORT_FEAT_LINK_STATE:
2064 if (hcd->speed != HCD_USB3) {
2065 dev_dbg(dummy_dev(dum_hcd),
2066 "USB_PORT_FEAT_LINK_STATE req not "
2067 "supported for USB 2.0 roothub\n");
2068 goto error;
2069 }
2070 /*
2071 * Since this is dummy we don't have an actual link so
2072 * there is nothing to do for the SET_LINK_STATE cmd
2073 */
2074 break;
2075 case USB_PORT_FEAT_U1_TIMEOUT:
2076 case USB_PORT_FEAT_U2_TIMEOUT:
2077 /* TODO: add suspend/resume support! */
2078 if (hcd->speed != HCD_USB3) {
2079 dev_dbg(dummy_dev(dum_hcd),
2080 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
2081 "supported for USB 2.0 roothub\n");
2082 goto error;
2083 }
2084 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002086 /* Applicable only for USB2.0 hub */
2087 if (hcd->speed == HCD_USB3) {
2088 dev_dbg(dummy_dev(dum_hcd),
2089 "USB_PORT_FEAT_SUSPEND req not "
2090 "supported for USB 3.0 roothub\n");
2091 goto error;
2092 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002093 if (dum_hcd->active) {
2094 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
Alan Sternf1c39fa2005-05-03 16:24:04 -04002095
2096 /* HNP would happen here; for now we
2097 * assume b_bus_req is always true.
2098 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002099 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002100 if (((1 << USB_DEVICE_B_HNP_ENABLE)
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002101 & dum_hcd->dum->devstatus) != 0)
2102 dev_dbg(dummy_dev(dum_hcd),
Alan Stern5742b0c2005-05-02 11:25:17 -04002103 "no HNP yet!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
2105 break;
Alan Sternf1c39fa2005-05-03 16:24:04 -04002106 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002107 if (hcd->speed == HCD_USB3)
2108 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
2109 else
2110 dum_hcd->port_status |= USB_PORT_STAT_POWER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002111 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002112 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002113 case USB_PORT_FEAT_BH_PORT_RESET:
2114 /* Applicable only for USB3.0 hub */
2115 if (hcd->speed != HCD_USB3) {
2116 dev_dbg(dummy_dev(dum_hcd),
2117 "USB_PORT_FEAT_BH_PORT_RESET req not "
2118 "supported for USB 2.0 roothub\n");
2119 goto error;
2120 }
2121 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 case USB_PORT_FEAT_RESET:
Alan Sternf1c39fa2005-05-03 16:24:04 -04002123 /* if it's already enabled, disable */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002124 if (hcd->speed == HCD_USB3) {
2125 dum_hcd->port_status = 0;
2126 dum_hcd->port_status =
2127 (USB_SS_PORT_STAT_POWER |
2128 USB_PORT_STAT_CONNECTION |
2129 USB_PORT_STAT_RESET);
2130 } else
2131 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
Alan Sternf1c39fa2005-05-03 16:24:04 -04002132 | USB_PORT_STAT_LOW_SPEED
2133 | USB_PORT_STAT_HIGH_SPEED);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002134 /*
2135 * We want to reset device status. All but the
2136 * Self powered feature
2137 */
2138 dum_hcd->dum->devstatus &=
2139 (1 << USB_DEVICE_SELF_POWERED);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002140 /*
2141 * FIXME USB3.0: what is the correct reset signaling
2142 * interval? Is it still 50msec as for HS?
2143 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002144 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002145 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 default:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002147 if (hcd->speed == HCD_USB3) {
2148 if ((dum_hcd->port_status &
2149 USB_SS_PORT_STAT_POWER) != 0) {
2150 dum_hcd->port_status |= (1 << wValue);
2151 set_link_state(dum_hcd);
2152 }
2153 } else
2154 if ((dum_hcd->port_status &
2155 USB_PORT_STAT_POWER) != 0) {
2156 dum_hcd->port_status |= (1 << wValue);
2157 set_link_state(dum_hcd);
2158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 }
2160 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002161 case GetPortErrorCount:
2162 if (hcd->speed != HCD_USB3) {
2163 dev_dbg(dummy_dev(dum_hcd),
2164 "GetPortErrorCount req not "
2165 "supported for USB 2.0 roothub\n");
2166 goto error;
2167 }
2168 /* We'll always return 0 since this is a dummy hub */
2169 *(__le32 *) buf = cpu_to_le32(0);
2170 break;
2171 case SetHubDepth:
2172 if (hcd->speed != HCD_USB3) {
2173 dev_dbg(dummy_dev(dum_hcd),
2174 "SetHubDepth req not supported for "
2175 "USB 2.0 roothub\n");
2176 goto error;
2177 }
2178 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002180 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 "hub control req%04x v%04x i%04x l%d\n",
2182 typeReq, wValue, wIndex, wLength);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002183error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 /* "protocol stall" on error */
2185 retval = -EPIPE;
2186 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002187 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Stern685eb932005-05-03 16:27:26 -04002188
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002189 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002190 usb_hcd_poll_rh_status(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 return retval;
2192}
2193
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002194static int dummy_bus_suspend(struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002195{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002196 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002197
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002198 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern3cf0a222005-11-29 12:08:15 -05002199
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002200 spin_lock_irq(&dum_hcd->dum->lock);
2201 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2202 set_link_state(dum_hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002203 hcd->state = HC_STATE_SUSPENDED;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002204 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern391eca92005-05-10 15:34:16 -04002205 return 0;
2206}
2207
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002208static int dummy_bus_resume(struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002209{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002210 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002211 int rc = 0;
2212
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002213 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002214
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002215 spin_lock_irq(&dum_hcd->dum->lock);
Alan Stern541c7d42010-06-22 16:39:10 -04002216 if (!HCD_HW_ACCESSIBLE(hcd)) {
Alan Sterncfa59da2007-06-21 16:25:35 -04002217 rc = -ESHUTDOWN;
Alan Stern3cf0a222005-11-29 12:08:15 -05002218 } else {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002219 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2220 set_link_state(dum_hcd);
2221 if (!list_empty(&dum_hcd->urbp_list))
2222 mod_timer(&dum_hcd->timer, jiffies);
Alan Stern3cf0a222005-11-29 12:08:15 -05002223 hcd->state = HC_STATE_RUNNING;
2224 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002225 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern3cf0a222005-11-29 12:08:15 -05002226 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002227}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229/*-------------------------------------------------------------------------*/
2230
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002231static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002233 int ep = usb_pipeendpoint(urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002235 return snprintf(buf, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 "urb/%p %s ep%d%s%s len %d/%d\n",
2237 urb,
2238 ({ char *s;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002239 switch (urb->dev->speed) {
2240 case USB_SPEED_LOW:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002241 s = "ls";
2242 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002243 case USB_SPEED_FULL:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002244 s = "fs";
2245 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002246 case USB_SPEED_HIGH:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002247 s = "hs";
2248 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002249 case USB_SPEED_SUPER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002250 s = "ss";
2251 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002252 default:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002253 s = "?";
2254 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 }; s; }),
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002256 ep, ep ? (usb_pipein(urb->pipe) ? "in" : "out") : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 ({ char *s; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002258 switch (usb_pipetype(urb->pipe)) { \
2259 case PIPE_CONTROL: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002260 s = ""; \
2261 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002262 case PIPE_BULK: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002263 s = "-bulk"; \
2264 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002265 case PIPE_INTERRUPT: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002266 s = "-int"; \
2267 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002268 default: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002269 s = "-iso"; \
2270 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002271 }; s; }),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 urb->actual_length, urb->transfer_buffer_length);
2273}
2274
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002275static ssize_t show_urbs(struct device *dev, struct device_attribute *attr,
2276 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002278 struct usb_hcd *hcd = dev_get_drvdata(dev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002279 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 struct urbp *urbp;
2281 size_t size = 0;
2282 unsigned long flags;
2283
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002284 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2285 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 size_t temp;
2287
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002288 temp = show_urb(buf, PAGE_SIZE - size, urbp->urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 buf += temp;
2290 size += temp;
2291 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002292 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 return size;
2295}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002296static DEVICE_ATTR(urbs, S_IRUGO, show_urbs, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002298static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2299{
2300 init_timer(&dum_hcd->timer);
2301 dum_hcd->timer.function = dummy_timer;
2302 dum_hcd->timer.data = (unsigned long)dum_hcd;
2303 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002304 dum_hcd->stream_en_ep = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002305 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2306 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2307 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2308 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2309#ifdef CONFIG_USB_OTG
2310 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2311#endif
2312 return 0;
2313
2314 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2315 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2316}
2317
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002318static int dummy_start(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002320 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 /*
2323 * MASTER side init ... we emulate a root hub that'll only ever
2324 * talk to one device (the slave side). Also appears in sysfs,
2325 * just like more familiar pci-based HCDs.
2326 */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002327 if (!usb_hcd_is_primary_hcd(hcd))
2328 return dummy_start_ss(dum_hcd);
2329
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002330 spin_lock_init(&dum_hcd->dum->lock);
2331 init_timer(&dum_hcd->timer);
2332 dum_hcd->timer.function = dummy_timer;
2333 dum_hcd->timer.data = (unsigned long)dum_hcd;
2334 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002336 INIT_LIST_HEAD(&dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Alan Sterncaf29f62007-12-06 11:10:39 -05002338 hcd->power_budget = POWER_BUDGET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 hcd->state = HC_STATE_RUNNING;
Alan Stern685eb932005-05-03 16:27:26 -04002340 hcd->uses_new_polling = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Alan Stern5742b0c2005-05-02 11:25:17 -04002342#ifdef CONFIG_USB_OTG
2343 hcd->self.otg_port = 1;
2344#endif
2345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002347 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
2349
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002350static void dummy_stop(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
2352 struct dummy *dum;
2353
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002354 dum = hcd_to_dummy_hcd(hcd)->dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002355 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2356 usb_gadget_unregister_driver(dum->driver);
2357 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358}
2359
2360/*-------------------------------------------------------------------------*/
2361
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002362static int dummy_h_get_frame(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002364 return dummy_g_get_frame(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365}
2366
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002367static int dummy_setup(struct usb_hcd *hcd)
2368{
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01002369 hcd->self.sg_tablesize = ~0;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002370 if (usb_hcd_is_primary_hcd(hcd)) {
2371 the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
2372 the_controller.hs_hcd->dum = &the_controller;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002373 /*
2374 * Mark the first roothub as being USB 2.0.
2375 * The USB 3.0 roothub will be registered later by
2376 * dummy_hcd_probe()
2377 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002378 hcd->speed = HCD_USB2;
2379 hcd->self.root_hub->speed = USB_SPEED_HIGH;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002380 } else {
2381 the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
2382 the_controller.ss_hcd->dum = &the_controller;
2383 hcd->speed = HCD_USB3;
2384 hcd->self.root_hub->speed = USB_SPEED_SUPER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002385 }
2386 return 0;
2387}
2388
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002389/* Change a group of bulk endpoints to support multiple stream IDs */
Sebastian Andrzej Siewiord81f3e42012-01-09 13:15:00 +01002390static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002391 struct usb_host_endpoint **eps, unsigned int num_eps,
2392 unsigned int num_streams, gfp_t mem_flags)
2393{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002394 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2395 unsigned long flags;
2396 int max_stream;
2397 int ret_streams = num_streams;
2398 unsigned int index;
2399 unsigned int i;
2400
2401 if (!num_eps)
2402 return -EINVAL;
2403
2404 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2405 for (i = 0; i < num_eps; i++) {
2406 index = dummy_get_ep_idx(&eps[i]->desc);
2407 if ((1 << index) & dum_hcd->stream_en_ep) {
2408 ret_streams = -EINVAL;
2409 goto out;
2410 }
2411 max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp);
2412 if (!max_stream) {
2413 ret_streams = -EINVAL;
2414 goto out;
2415 }
2416 if (max_stream < ret_streams) {
2417 dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u "
2418 "stream IDs.\n",
2419 eps[i]->desc.bEndpointAddress,
2420 max_stream);
2421 ret_streams = max_stream;
2422 }
2423 }
2424
2425 for (i = 0; i < num_eps; i++) {
2426 index = dummy_get_ep_idx(&eps[i]->desc);
2427 dum_hcd->stream_en_ep |= 1 << index;
2428 set_max_streams_for_pipe(dum_hcd,
2429 usb_endpoint_num(&eps[i]->desc), ret_streams);
2430 }
2431out:
2432 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2433 return ret_streams;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002434}
2435
2436/* Reverts a group of bulk endpoints back to not using stream IDs. */
Sebastian Andrzej Siewiord81f3e42012-01-09 13:15:00 +01002437static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002438 struct usb_host_endpoint **eps, unsigned int num_eps,
2439 gfp_t mem_flags)
2440{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002441 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2442 unsigned long flags;
2443 int ret;
2444 unsigned int index;
2445 unsigned int i;
2446
2447 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2448 for (i = 0; i < num_eps; i++) {
2449 index = dummy_get_ep_idx(&eps[i]->desc);
2450 if (!((1 << index) & dum_hcd->stream_en_ep)) {
2451 ret = -EINVAL;
2452 goto out;
2453 }
2454 }
2455
2456 for (i = 0; i < num_eps; i++) {
2457 index = dummy_get_ep_idx(&eps[i]->desc);
2458 dum_hcd->stream_en_ep &= ~(1 << index);
2459 set_max_streams_for_pipe(dum_hcd,
2460 usb_endpoint_num(&eps[i]->desc), 0);
2461 }
2462 ret = 0;
2463out:
2464 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2465 return ret;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002466}
2467
2468static struct hc_driver dummy_hcd = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 .description = (char *) driver_name,
2470 .product_desc = "Dummy host controller",
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002471 .hcd_priv_size = sizeof(struct dummy_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002473 .flags = HCD_USB3 | HCD_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002475 .reset = dummy_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 .start = dummy_start,
2477 .stop = dummy_stop,
2478
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002479 .urb_enqueue = dummy_urb_enqueue,
2480 .urb_dequeue = dummy_urb_dequeue,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002482 .get_frame_number = dummy_h_get_frame,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002484 .hub_status_data = dummy_hub_status,
2485 .hub_control = dummy_hub_control,
Alan Stern0c0382e2005-10-13 17:08:02 -04002486 .bus_suspend = dummy_bus_suspend,
2487 .bus_resume = dummy_bus_resume,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002488
2489 .alloc_streams = dummy_alloc_streams,
2490 .free_streams = dummy_free_streams,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491};
2492
Alan Stern8364d6b2005-11-14 12:16:30 -05002493static int dummy_hcd_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002495 struct usb_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002496 struct usb_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 int retval;
2498
Alan Stern8364d6b2005-11-14 12:16:30 -05002499 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002501 if (!mod_data.is_super_speed)
2502 dummy_hcd.flags = HCD_USB2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002503 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2504 if (!hs_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 return -ENOMEM;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002506 hs_hcd->has_tt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002508 retval = usb_add_hcd(hs_hcd, 0, 0);
Sebastian Andrzej Siewior47c8e862012-08-19 21:54:58 +02002509 if (retval)
2510 goto put_usb2_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002511
2512 if (mod_data.is_super_speed) {
2513 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2514 dev_name(&pdev->dev), hs_hcd);
2515 if (!ss_hcd) {
2516 retval = -ENOMEM;
2517 goto dealloc_usb2_hcd;
2518 }
2519
2520 retval = usb_add_hcd(ss_hcd, 0, 0);
2521 if (retval)
2522 goto put_usb3_hcd;
2523 }
2524 return 0;
2525
2526put_usb3_hcd:
2527 usb_put_hcd(ss_hcd);
2528dealloc_usb2_hcd:
Sebastian Andrzej Siewior47c8e862012-08-19 21:54:58 +02002529 usb_remove_hcd(hs_hcd);
2530put_usb2_hcd:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002531 usb_put_hcd(hs_hcd);
2532 the_controller.hs_hcd = the_controller.ss_hcd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 return retval;
2534}
2535
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002536static int dummy_hcd_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002538 struct dummy *dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002540 dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002541
2542 if (dum->ss_hcd) {
2543 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2544 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2545 }
2546
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002547 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2548 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002549
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002550 the_controller.hs_hcd = NULL;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002551 the_controller.ss_hcd = NULL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002552
Alan Sternd9b76252005-05-03 16:15:43 -04002553 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554}
2555
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002556static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state)
Alan Stern391eca92005-05-10 15:34:16 -04002557{
2558 struct usb_hcd *hcd;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002559 struct dummy_hcd *dum_hcd;
Alan Stern3cf0a222005-11-29 12:08:15 -05002560 int rc = 0;
Alan Stern391eca92005-05-10 15:34:16 -04002561
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002562 dev_dbg(&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002563
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002564 hcd = platform_get_drvdata(pdev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002565 dum_hcd = hcd_to_dummy_hcd(hcd);
2566 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern3cf0a222005-11-29 12:08:15 -05002567 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2568 rc = -EBUSY;
2569 } else
2570 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2571 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002572}
2573
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002574static int dummy_hcd_resume(struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04002575{
2576 struct usb_hcd *hcd;
2577
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002578 dev_dbg(&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002579
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002580 hcd = platform_get_drvdata(pdev);
Alan Stern3cf0a222005-11-29 12:08:15 -05002581 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002582 usb_hcd_poll_rh_status(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002583 return 0;
2584}
2585
Russell King3ae5eae2005-11-09 22:32:44 +00002586static struct platform_driver dummy_hcd_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04002587 .probe = dummy_hcd_probe,
2588 .remove = dummy_hcd_remove,
Alan Stern391eca92005-05-10 15:34:16 -04002589 .suspend = dummy_hcd_suspend,
2590 .resume = dummy_hcd_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002591 .driver = {
2592 .name = (char *) driver_name,
2593 .owner = THIS_MODULE,
2594 },
Alan Sternd9b76252005-05-03 16:15:43 -04002595};
2596
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597/*-------------------------------------------------------------------------*/
2598
Alan Sterna89a2cd2008-04-07 15:03:25 -04002599static struct platform_device *the_udc_pdev;
2600static struct platform_device *the_hcd_pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002602static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002604 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002606 if (usb_disabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return -ENODEV;
Alan Sternd9b76252005-05-03 16:15:43 -04002608
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +03002609 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2610 return -EINVAL;
2611
Alan Sterna89a2cd2008-04-07 15:03:25 -04002612 the_hcd_pdev = platform_device_alloc(driver_name, -1);
2613 if (!the_hcd_pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 return retval;
Alan Sterna89a2cd2008-04-07 15:03:25 -04002615 the_udc_pdev = platform_device_alloc(gadget_name, -1);
2616 if (!the_udc_pdev)
2617 goto err_alloc_udc;
Alan Sternd9b76252005-05-03 16:15:43 -04002618
Alan Sterna89a2cd2008-04-07 15:03:25 -04002619 retval = platform_driver_register(&dummy_hcd_driver);
2620 if (retval < 0)
2621 goto err_register_hcd_driver;
2622 retval = platform_driver_register(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002623 if (retval < 0)
2624 goto err_register_udc_driver;
2625
Alan Sterna89a2cd2008-04-07 15:03:25 -04002626 retval = platform_device_add(the_hcd_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002627 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002628 goto err_add_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002629 if (!the_controller.hs_hcd ||
2630 (!the_controller.ss_hcd && mod_data.is_super_speed)) {
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002631 /*
2632 * The hcd was added successfully but its probe function failed
2633 * for some reason.
2634 */
2635 retval = -EINVAL;
2636 goto err_add_udc;
2637 }
Alan Sterna89a2cd2008-04-07 15:03:25 -04002638 retval = platform_device_add(the_udc_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002639 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002640 goto err_add_udc;
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002641 if (!platform_get_drvdata(the_udc_pdev)) {
2642 /*
2643 * The udc was added successfully but its probe function failed
2644 * for some reason.
2645 */
2646 retval = -EINVAL;
2647 goto err_probe_udc;
2648 }
Alan Sternd9b76252005-05-03 16:15:43 -04002649 return retval;
2650
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002651err_probe_udc:
2652 platform_device_del(the_udc_pdev);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002653err_add_udc:
2654 platform_device_del(the_hcd_pdev);
2655err_add_hcd:
2656 platform_driver_unregister(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002657err_register_udc_driver:
Alan Sterna89a2cd2008-04-07 15:03:25 -04002658 platform_driver_unregister(&dummy_hcd_driver);
2659err_register_hcd_driver:
2660 platform_device_put(the_udc_pdev);
2661err_alloc_udc:
2662 platform_device_put(the_hcd_pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 return retval;
2664}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002665module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002667static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002669 platform_device_unregister(the_udc_pdev);
2670 platform_device_unregister(the_hcd_pdev);
2671 platform_driver_unregister(&dummy_udc_driver);
2672 platform_driver_unregister(&dummy_hcd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002674module_exit(cleanup);