blob: e1cd56c5e2a86b6ce58ff485dc19b5d80593464f [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>
47#include <asm/system.h>
48#include <asm/unaligned.h>
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define DRIVER_DESC "USB Host+Gadget Emulator"
Alan Stern391eca92005-05-10 15:34:16 -040051#define DRIVER_VERSION "02 May 2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Alan Sterncaf29f62007-12-06 11:10:39 -050053#define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */
54
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +010055static const char driver_name[] = "dummy_hcd";
56static const char driver_desc[] = "USB Host+Gadget Emulator";
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +010058static const char gadget_name[] = "dummy_udc";
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +010060MODULE_DESCRIPTION(DRIVER_DESC);
61MODULE_AUTHOR("David Brownell");
62MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +030064struct dummy_hcd_module_parameters {
65 bool is_super_speed;
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +030066 bool is_high_speed;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +030067};
68
69static struct dummy_hcd_module_parameters mod_data = {
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +030070 .is_super_speed = false,
71 .is_high_speed = true,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +030072};
73module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
74MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +030075module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
76MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*-------------------------------------------------------------------------*/
78
79/* gadget side driver data structres */
80struct dummy_ep {
81 struct list_head queue;
82 unsigned long last_io; /* jiffies timestamp */
83 struct usb_gadget *gadget;
84 const struct usb_endpoint_descriptor *desc;
85 struct usb_ep ep;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +010086 unsigned halted:1;
87 unsigned wedged:1;
88 unsigned already_seen:1;
89 unsigned setup_stage:1;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +010090 unsigned stream_en:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091};
92
93struct dummy_request {
94 struct list_head queue; /* ep's requests */
95 struct usb_request req;
96};
97
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +010098static inline struct dummy_ep *usb_ep_to_dummy_ep(struct usb_ep *_ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100100 return container_of(_ep, struct dummy_ep, ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103static inline struct dummy_request *usb_request_to_dummy_request
104 (struct usb_request *_req)
105{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100106 return container_of(_req, struct dummy_request, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109/*-------------------------------------------------------------------------*/
110
111/*
112 * Every device has ep0 for control requests, plus up to 30 more endpoints,
113 * in one of two types:
114 *
115 * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
116 * number can be changed. Names like "ep-a" are used for this type.
117 *
118 * - Fixed Function: in other cases. some characteristics may be mutable;
119 * that'd be hardware-specific. Names like "ep12out-bulk" are used.
120 *
121 * Gadget drivers are responsible for not setting up conflicting endpoint
122 * configurations, illegal or unsupported packet lengths, and so on.
123 */
124
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100125static const char ep0name[] = "ep0";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100127static const char *const ep_name[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 ep0name, /* everyone has ep0 */
129
130 /* act like a net2280: high speed, six configurable endpoints */
131 "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
132
133 /* or like pxa250: fifteen fixed function endpoints */
134 "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
135 "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
136 "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
137 "ep15in-int",
138
139 /* or like sa1100: two fixed function endpoints */
140 "ep1out-bulk", "ep2in-bulk",
141};
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
931 dummy_pullup(&dum->gadget, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return 0;
933}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935#undef is_enabled
936
Alan Sternd9b76252005-05-03 16:15:43 -0400937/* The gadget structure is stored inside the hcd structure and will be
938 * released along with it. */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100939static void dummy_gadget_release(struct device *dev)
Alan Sternd9b76252005-05-03 16:15:43 -0400940{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300941 return;
Alan Sternd9b76252005-05-03 16:15:43 -0400942}
943
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200944static void init_dummy_udc_hw(struct dummy *dum)
945{
946 int i;
947
948 INIT_LIST_HEAD(&dum->gadget.ep_list);
949 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
950 struct dummy_ep *ep = &dum->ep[i];
951
952 if (!ep_name[i])
953 break;
954 ep->ep.name = ep_name[i];
955 ep->ep.ops = &dummy_ep_ops;
956 list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
957 ep->halted = ep->wedged = ep->already_seen =
958 ep->setup_stage = 0;
959 ep->ep.maxpacket = ~0;
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100960 ep->ep.max_streams = 16;
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200961 ep->last_io = jiffies;
962 ep->gadget = &dum->gadget;
963 ep->desc = NULL;
964 INIT_LIST_HEAD(&ep->queue);
965 }
966
967 dum->gadget.ep0 = &dum->ep[0].ep;
968 list_del_init(&dum->ep[0].ep.ep_list);
969 INIT_LIST_HEAD(&dum->fifo_req.queue);
Sebastian Andrzej Siewiorf8744d42011-06-23 14:26:13 +0200970
971#ifdef CONFIG_USB_OTG
972 dum->gadget.is_otg = 1;
973#endif
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200974}
975
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100976static int dummy_udc_probe(struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -0400977{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300978 struct dummy *dum = &the_controller;
Alan Sternd9b76252005-05-03 16:15:43 -0400979 int rc;
980
981 dum->gadget.name = gadget_name;
982 dum->gadget.ops = &dummy_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100983 dum->gadget.max_speed = USB_SPEED_SUPER;
Alan Sternd9b76252005-05-03 16:15:43 -0400984
Kay Sievers0031a062008-05-02 06:02:41 +0200985 dev_set_name(&dum->gadget.dev, "gadget");
Alan Stern8364d6b2005-11-14 12:16:30 -0500986 dum->gadget.dev.parent = &pdev->dev;
Alan Sternd9b76252005-05-03 16:15:43 -0400987 dum->gadget.dev.release = dummy_gadget_release;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100988 rc = device_register(&dum->gadget.dev);
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530989 if (rc < 0) {
990 put_device(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -0400991 return rc;
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530992 }
Alan Sternd9b76252005-05-03 16:15:43 -0400993
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200994 init_dummy_udc_hw(dum);
995
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300996 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
997 if (rc < 0)
998 goto err_udc;
999
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001000 rc = device_create_file(&dum->gadget.dev, &dev_attr_function);
Alan Sternefd54a32006-09-25 11:55:56 -04001001 if (rc < 0)
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001002 goto err_dev;
1003 platform_set_drvdata(pdev, dum);
1004 return rc;
1005
1006err_dev:
1007 usb_del_gadget_udc(&dum->gadget);
1008err_udc:
1009 device_unregister(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -04001010 return rc;
1011}
1012
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001013static int dummy_udc_remove(struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -04001014{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001015 struct dummy *dum = platform_get_drvdata(pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04001016
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001017 usb_del_gadget_udc(&dum->gadget);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001018 platform_set_drvdata(pdev, NULL);
1019 device_remove_file(&dum->gadget.dev, &dev_attr_function);
1020 device_unregister(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -04001021 return 0;
1022}
1023
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001024static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1025 int suspend)
Alan Stern391eca92005-05-10 15:34:16 -04001026{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001027 spin_lock_irq(&dum->lock);
1028 dum->udc_suspended = suspend;
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001029 set_link_state(dum_hcd);
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001030 spin_unlock_irq(&dum->lock);
1031}
Alan Stern391eca92005-05-10 15:34:16 -04001032
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001033static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1034{
1035 struct dummy *dum = platform_get_drvdata(pdev);
1036 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1037
1038 dev_dbg(&pdev->dev, "%s\n", __func__);
1039 dummy_udc_pm(dum, dum_hcd, 1);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001040 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001041 return 0;
1042}
1043
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001044static int dummy_udc_resume(struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04001045{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001046 struct dummy *dum = platform_get_drvdata(pdev);
1047 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Alan Stern391eca92005-05-10 15:34:16 -04001048
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001049 dev_dbg(&pdev->dev, "%s\n", __func__);
1050 dummy_udc_pm(dum, dum_hcd, 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001051 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001052 return 0;
1053}
1054
Russell King3ae5eae2005-11-09 22:32:44 +00001055static struct platform_driver dummy_udc_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04001056 .probe = dummy_udc_probe,
1057 .remove = dummy_udc_remove,
Alan Stern391eca92005-05-10 15:34:16 -04001058 .suspend = dummy_udc_suspend,
1059 .resume = dummy_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001060 .driver = {
1061 .name = (char *) gadget_name,
1062 .owner = THIS_MODULE,
1063 },
Alan Sternd9b76252005-05-03 16:15:43 -04001064};
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066/*-------------------------------------------------------------------------*/
1067
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001068static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc)
1069{
1070 unsigned int index;
1071
1072 index = usb_endpoint_num(desc) << 1;
1073 if (usb_endpoint_dir_in(desc))
1074 index |= 1;
1075 return index;
1076}
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078/* MASTER/HOST SIDE DRIVER
1079 *
1080 * this uses the hcd framework to hook up to host side drivers.
1081 * its root hub will only have one device, otherwise it acts like
1082 * a normal host controller.
1083 *
1084 * when urbs are queued, they're just stuck on a list that we
1085 * scan in a timer callback. that callback connects writes from
1086 * the host with reads from the device, and so on, based on the
1087 * usb 2.0 rules.
1088 */
1089
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001090static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb)
1091{
1092 const struct usb_endpoint_descriptor *desc = &urb->ep->desc;
1093 u32 index;
1094
1095 if (!usb_endpoint_xfer_bulk(desc))
1096 return 0;
1097
1098 index = dummy_get_ep_idx(desc);
1099 return (1 << index) & dum_hcd->stream_en_ep;
1100}
1101
1102/*
1103 * The max stream number is saved as a nibble so for the 30 possible endpoints
1104 * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
1105 * means we use only 1 stream). The maximum according to the spec is 16bit so
1106 * if the 16 stream limit is about to go, the array size should be incremented
1107 * to 30 elements of type u16.
1108 */
1109static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1110 unsigned int pipe)
1111{
1112 int max_streams;
1113
1114 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1115 if (usb_pipeout(pipe))
1116 max_streams >>= 4;
1117 else
1118 max_streams &= 0xf;
1119 max_streams++;
1120 return max_streams;
1121}
1122
1123static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1124 unsigned int pipe, unsigned int streams)
1125{
1126 int max_streams;
1127
1128 streams--;
1129 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1130 if (usb_pipeout(pipe)) {
1131 streams <<= 4;
1132 max_streams &= 0xf;
1133 } else {
1134 max_streams &= 0xf0;
1135 }
1136 max_streams |= streams;
1137 dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams;
1138}
1139
1140static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb)
1141{
1142 unsigned int max_streams;
1143 int enabled;
1144
1145 enabled = dummy_ep_stream_en(dum_hcd, urb);
1146 if (!urb->stream_id) {
1147 if (enabled)
1148 return -EINVAL;
1149 return 0;
1150 }
1151 if (!enabled)
1152 return -EINVAL;
1153
1154 max_streams = get_max_streams_for_pipe(dum_hcd,
1155 usb_pipeendpoint(urb->pipe));
1156 if (urb->stream_id > max_streams) {
1157 dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n",
1158 urb->stream_id);
1159 BUG();
1160 return -EINVAL;
1161 }
1162 return 0;
1163}
1164
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001165static int dummy_urb_enqueue(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 struct usb_hcd *hcd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001168 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001170 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 struct urbp *urbp;
1172 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001173 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001175 urbp = kmalloc(sizeof *urbp, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (!urbp)
1177 return -ENOMEM;
1178 urbp->urb = urb;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001179 urbp->miter_started = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001181 dum_hcd = hcd_to_dummy_hcd(hcd);
1182 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001183
1184 rc = dummy_validate_stream(dum_hcd, urb);
1185 if (rc) {
1186 kfree(urbp);
1187 goto done;
1188 }
1189
Alan Sterne9df41c2007-08-08 11:48:02 -04001190 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1191 if (rc) {
1192 kfree(urbp);
1193 goto done;
1194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001196 if (!dum_hcd->udev) {
1197 dum_hcd->udev = urb->dev;
1198 usb_get_dev(dum_hcd->udev);
1199 } else if (unlikely(dum_hcd->udev != urb->dev))
1200 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001202 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 urb->hcpriv = urbp;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001204 if (usb_pipetype(urb->pipe) == PIPE_CONTROL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 urb->error_count = 1; /* mark as a new urb */
1206
1207 /* kick the scheduler, it'll do the rest */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001208 if (!timer_pending(&dum_hcd->timer))
1209 mod_timer(&dum_hcd->timer, jiffies + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Alan Sterne9df41c2007-08-08 11:48:02 -04001211 done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001212 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001213 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Alan Sterne9df41c2007-08-08 11:48:02 -04001216static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001218 struct dummy_hcd *dum_hcd;
Alan Stern391eca92005-05-10 15:34:16 -04001219 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001220 int rc;
Alan Stern391eca92005-05-10 15:34:16 -04001221
1222 /* giveback happens automatically in timer callback,
1223 * so make sure the callback happens */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001224 dum_hcd = hcd_to_dummy_hcd(hcd);
1225 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001226
1227 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001228 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1229 !list_empty(&dum_hcd->urbp_list))
1230 mod_timer(&dum_hcd->timer, jiffies);
Alan Sterne9df41c2007-08-08 11:48:02 -04001231
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001232 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001233 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001236static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
1237 u32 len)
1238{
1239 void *ubuf, *rbuf;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001240 struct urbp *urbp = urb->hcpriv;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001241 int to_host;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001242 struct sg_mapping_iter *miter = &urbp->miter;
1243 u32 trans = 0;
1244 u32 this_sg;
1245 bool next_sg;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001246
1247 to_host = usb_pipein(urb->pipe);
1248 rbuf = req->req.buf + req->req.actual;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001249
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001250 if (!urb->num_sgs) {
1251 ubuf = urb->transfer_buffer + urb->actual_length;
1252 if (to_host)
1253 memcpy(ubuf, rbuf, len);
1254 else
1255 memcpy(rbuf, ubuf, len);
1256 return len;
1257 }
1258
1259 if (!urbp->miter_started) {
1260 u32 flags = SG_MITER_ATOMIC;
1261
1262 if (to_host)
1263 flags |= SG_MITER_TO_SG;
1264 else
1265 flags |= SG_MITER_FROM_SG;
1266
1267 sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
1268 urbp->miter_started = 1;
1269 }
1270 next_sg = sg_miter_next(miter);
1271 if (next_sg == false) {
1272 WARN_ON_ONCE(1);
1273 return -EINVAL;
1274 }
1275 do {
1276 ubuf = miter->addr;
1277 this_sg = min_t(u32, len, miter->length);
1278 miter->consumed = this_sg;
1279 trans += this_sg;
1280
1281 if (to_host)
1282 memcpy(ubuf, rbuf, this_sg);
1283 else
1284 memcpy(rbuf, ubuf, this_sg);
1285 len -= this_sg;
1286
1287 if (!len)
1288 break;
1289 next_sg = sg_miter_next(miter);
1290 if (next_sg == false) {
1291 WARN_ON_ONCE(1);
1292 return -EINVAL;
1293 }
1294
1295 rbuf += this_sg;
1296 } while (1);
1297
1298 sg_miter_stop(miter);
1299 return trans;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001300}
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302/* transfer up to a frame's worth; caller must own lock */
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001303static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb,
1304 struct dummy_ep *ep, int limit, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001306 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 struct dummy_request *req;
1308
1309top:
1310 /* if there's no request queued, the device is NAKing; return */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001311 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 unsigned host_len, dev_len, len;
1313 int is_short, to_host;
1314 int rescan = 0;
1315
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001316 if (dummy_ep_stream_en(dum_hcd, urb)) {
1317 if ((urb->stream_id != req->req.stream_id))
1318 continue;
1319 }
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 /* 1..N packets of ep->ep.maxpacket each ... the last one
1322 * may be short (including zero length).
1323 *
1324 * writer can send a zlp explicitly (length 0) or implicitly
1325 * (length mod maxpacket zero, and 'zero' flag); they always
1326 * terminate reads.
1327 */
1328 host_len = urb->transfer_buffer_length - urb->actual_length;
1329 dev_len = req->req.length - req->req.actual;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001330 len = min(host_len, dev_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 /* FIXME update emulated data toggle too */
1333
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001334 to_host = usb_pipein(urb->pipe);
1335 if (unlikely(len == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 is_short = 1;
1337 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 /* not enough bandwidth left? */
1339 if (limit < ep->ep.maxpacket && limit < len)
1340 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001341 len = min_t(unsigned, len, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (len == 0)
1343 break;
1344
1345 /* use an extra pass for the final short packet */
1346 if (len > ep->ep.maxpacket) {
1347 rescan = 1;
1348 len -= (len % ep->ep.maxpacket);
1349 }
1350 is_short = (len % ep->ep.maxpacket) != 0;
1351
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001352 len = dummy_perform_transfer(urb, req, len);
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 ep->last_io = jiffies;
Dan Carpenterb1443ac0e2012-03-02 21:51:00 +03001355 if ((int)len < 0) {
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001356 req->req.status = len;
1357 } else {
1358 limit -= len;
1359 urb->actual_length += len;
1360 req->req.actual += len;
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363
1364 /* short packets terminate, maybe with overflow/underflow.
1365 * it's only really an error to write too much.
1366 *
1367 * partially filling a buffer optionally blocks queue advances
1368 * (so completion handlers can clean up the queue) but we don't
Alan Sternb0d9efb2007-08-21 15:39:21 -04001369 * need to emulate such data-in-flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 */
1371 if (is_short) {
1372 if (host_len == dev_len) {
1373 req->req.status = 0;
Alan Stern4d2f1102007-08-24 15:40:10 -04001374 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 } else if (to_host) {
1376 req->req.status = 0;
1377 if (dev_len > host_len)
Alan Stern4d2f1102007-08-24 15:40:10 -04001378 *status = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 else
Alan Stern4d2f1102007-08-24 15:40:10 -04001380 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 } else if (!to_host) {
Alan Stern4d2f1102007-08-24 15:40:10 -04001382 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (host_len > dev_len)
1384 req->req.status = -EOVERFLOW;
1385 else
1386 req->req.status = 0;
1387 }
1388
1389 /* many requests terminate without a short packet */
1390 } else {
1391 if (req->req.length == req->req.actual
1392 && !req->req.zero)
1393 req->req.status = 0;
1394 if (urb->transfer_buffer_length == urb->actual_length
1395 && !(urb->transfer_flags
Alan Stern4d2f1102007-08-24 15:40:10 -04001396 & URB_ZERO_PACKET))
1397 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399
1400 /* device side completion --> continuable */
1401 if (req->req.status != -EINPROGRESS) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001402 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001404 spin_unlock(&dum->lock);
1405 req->req.complete(&ep->ep, &req->req);
1406 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 /* requests might have been unlinked... */
1409 rescan = 1;
1410 }
1411
1412 /* host side completion --> terminate */
Alan Stern4d2f1102007-08-24 15:40:10 -04001413 if (*status != -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 break;
1415
1416 /* rescan to continue with any other queued i/o */
1417 if (rescan)
1418 goto top;
1419 }
1420 return limit;
1421}
1422
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001423static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 int limit = ep->ep.maxpacket;
1426
1427 if (dum->gadget.speed == USB_SPEED_HIGH) {
1428 int tmp;
1429
1430 /* high bandwidth mode */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001431 tmp = usb_endpoint_maxp(ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 tmp = (tmp >> 11) & 0x03;
1433 tmp *= 8 /* applies to entire frame */;
1434 limit += limit * tmp;
1435 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001436 if (dum->gadget.speed == USB_SPEED_SUPER) {
Sebastian Andrzej Siewior59f08e62012-01-12 12:53:14 +01001437 switch (usb_endpoint_type(ep->desc)) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001438 case USB_ENDPOINT_XFER_ISOC:
1439 /* Sec. 4.4.8.2 USB3.0 Spec */
1440 limit = 3 * 16 * 1024 * 8;
1441 break;
1442 case USB_ENDPOINT_XFER_INT:
1443 /* Sec. 4.4.7.2 USB3.0 Spec */
1444 limit = 3 * 1024 * 8;
1445 break;
1446 case USB_ENDPOINT_XFER_BULK:
1447 default:
1448 break;
1449 }
1450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return limit;
1452}
1453
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001454#define is_active(dum_hcd) ((dum_hcd->port_status & \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1456 USB_PORT_STAT_SUSPEND)) \
1457 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1458
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001459static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 int i;
1462
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001463 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1464 dum->ss_hcd : dum->hs_hcd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return NULL;
1466 if ((address & ~USB_DIR_IN) == 0)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001467 return &dum->ep[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001469 struct dummy_ep *ep = &dum->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 if (!ep->desc)
1472 continue;
1473 if (ep->desc->bEndpointAddress == address)
1474 return ep;
1475 }
1476 return NULL;
1477}
1478
1479#undef is_active
1480
1481#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1482#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1483#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1484#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1485#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1486#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1487
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001488
1489/**
1490 * handle_control_request() - handles all control transfers
1491 * @dum: pointer to dummy (the_controller)
1492 * @urb: the urb request to handle
1493 * @setup: pointer to the setup data for a USB device control
1494 * request
1495 * @status: pointer to request handling status
1496 *
1497 * Return 0 - if the request was handled
1498 * 1 - if the request wasn't handles
1499 * error code on error
1500 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001501static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001502 struct usb_ctrlrequest *setup,
1503 int *status)
1504{
1505 struct dummy_ep *ep2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001506 struct dummy *dum = dum_hcd->dum;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001507 int ret_val = 1;
1508 unsigned w_index;
1509 unsigned w_value;
1510
1511 w_index = le16_to_cpu(setup->wIndex);
1512 w_value = le16_to_cpu(setup->wValue);
1513 switch (setup->bRequest) {
1514 case USB_REQ_SET_ADDRESS:
1515 if (setup->bRequestType != Dev_Request)
1516 break;
1517 dum->address = w_value;
1518 *status = 0;
1519 dev_dbg(udc_dev(dum), "set_address = %d\n",
1520 w_value);
1521 ret_val = 0;
1522 break;
1523 case USB_REQ_SET_FEATURE:
1524 if (setup->bRequestType == Dev_Request) {
1525 ret_val = 0;
1526 switch (w_value) {
1527 case USB_DEVICE_REMOTE_WAKEUP:
1528 break;
1529 case USB_DEVICE_B_HNP_ENABLE:
1530 dum->gadget.b_hnp_enable = 1;
1531 break;
1532 case USB_DEVICE_A_HNP_SUPPORT:
1533 dum->gadget.a_hnp_support = 1;
1534 break;
1535 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1536 dum->gadget.a_alt_hnp_support = 1;
1537 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001538 case USB_DEVICE_U1_ENABLE:
1539 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1540 HCD_USB3)
1541 w_value = USB_DEV_STAT_U1_ENABLED;
1542 else
1543 ret_val = -EOPNOTSUPP;
1544 break;
1545 case USB_DEVICE_U2_ENABLE:
1546 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1547 HCD_USB3)
1548 w_value = USB_DEV_STAT_U2_ENABLED;
1549 else
1550 ret_val = -EOPNOTSUPP;
1551 break;
1552 case USB_DEVICE_LTM_ENABLE:
1553 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1554 HCD_USB3)
1555 w_value = USB_DEV_STAT_LTM_ENABLED;
1556 else
1557 ret_val = -EOPNOTSUPP;
1558 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001559 default:
1560 ret_val = -EOPNOTSUPP;
1561 }
1562 if (ret_val == 0) {
1563 dum->devstatus |= (1 << w_value);
1564 *status = 0;
1565 }
1566 } else if (setup->bRequestType == Ep_Request) {
1567 /* endpoint halt */
1568 ep2 = find_endpoint(dum, w_index);
1569 if (!ep2 || ep2->ep.name == ep0name) {
1570 ret_val = -EOPNOTSUPP;
1571 break;
1572 }
1573 ep2->halted = 1;
1574 ret_val = 0;
1575 *status = 0;
1576 }
1577 break;
1578 case USB_REQ_CLEAR_FEATURE:
1579 if (setup->bRequestType == Dev_Request) {
1580 ret_val = 0;
1581 switch (w_value) {
1582 case USB_DEVICE_REMOTE_WAKEUP:
1583 w_value = USB_DEVICE_REMOTE_WAKEUP;
1584 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001585 case USB_DEVICE_U1_ENABLE:
1586 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1587 HCD_USB3)
1588 w_value = USB_DEV_STAT_U1_ENABLED;
1589 else
1590 ret_val = -EOPNOTSUPP;
1591 break;
1592 case USB_DEVICE_U2_ENABLE:
1593 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1594 HCD_USB3)
1595 w_value = USB_DEV_STAT_U2_ENABLED;
1596 else
1597 ret_val = -EOPNOTSUPP;
1598 break;
1599 case USB_DEVICE_LTM_ENABLE:
1600 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1601 HCD_USB3)
1602 w_value = USB_DEV_STAT_LTM_ENABLED;
1603 else
1604 ret_val = -EOPNOTSUPP;
1605 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001606 default:
1607 ret_val = -EOPNOTSUPP;
1608 break;
1609 }
1610 if (ret_val == 0) {
1611 dum->devstatus &= ~(1 << w_value);
1612 *status = 0;
1613 }
1614 } else if (setup->bRequestType == Ep_Request) {
1615 /* endpoint halt */
1616 ep2 = find_endpoint(dum, w_index);
1617 if (!ep2) {
1618 ret_val = -EOPNOTSUPP;
1619 break;
1620 }
1621 if (!ep2->wedged)
1622 ep2->halted = 0;
1623 ret_val = 0;
1624 *status = 0;
1625 }
1626 break;
1627 case USB_REQ_GET_STATUS:
1628 if (setup->bRequestType == Dev_InRequest
1629 || setup->bRequestType == Intf_InRequest
1630 || setup->bRequestType == Ep_InRequest) {
1631 char *buf;
1632 /*
1633 * device: remote wakeup, selfpowered
1634 * interface: nothing
1635 * endpoint: halt
1636 */
1637 buf = (char *)urb->transfer_buffer;
1638 if (urb->transfer_buffer_length > 0) {
1639 if (setup->bRequestType == Ep_InRequest) {
1640 ep2 = find_endpoint(dum, w_index);
1641 if (!ep2) {
1642 ret_val = -EOPNOTSUPP;
1643 break;
1644 }
1645 buf[0] = ep2->halted;
1646 } else if (setup->bRequestType ==
1647 Dev_InRequest) {
1648 buf[0] = (u8)dum->devstatus;
1649 } else
1650 buf[0] = 0;
1651 }
1652 if (urb->transfer_buffer_length > 1)
1653 buf[1] = 0;
1654 urb->actual_length = min_t(u32, 2,
1655 urb->transfer_buffer_length);
1656 ret_val = 0;
1657 *status = 0;
1658 }
1659 break;
1660 }
1661 return ret_val;
1662}
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664/* drive both sides of the transfers; looks like irq handlers to
1665 * both drivers except the callbacks aren't in_irq().
1666 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001667static void dummy_timer(unsigned long _dum_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001669 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1670 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 struct urbp *urbp, *tmp;
1672 unsigned long flags;
1673 int limit, total;
1674 int i;
1675
1676 /* simplistic model for one frame's bandwidth */
1677 switch (dum->gadget.speed) {
1678 case USB_SPEED_LOW:
1679 total = 8/*bytes*/ * 12/*packets*/;
1680 break;
1681 case USB_SPEED_FULL:
1682 total = 64/*bytes*/ * 19/*packets*/;
1683 break;
1684 case USB_SPEED_HIGH:
1685 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1686 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001687 case USB_SPEED_SUPER:
1688 /* Bus speed is 500000 bytes/ms, so use a little less */
1689 total = 490000;
1690 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001692 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return;
1694 }
1695
1696 /* FIXME if HZ != 1000 this will probably misbehave ... */
1697
1698 /* look at each urb queued by the host side driver */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001699 spin_lock_irqsave(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001701 if (!dum_hcd->udev) {
1702 dev_err(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 "timer fired with no URBs pending?\n");
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001704 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return;
1706 }
1707
1708 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001709 if (!ep_name[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001711 dum->ep[i].already_seen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
1713
1714restart:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001715 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 struct urb *urb;
1717 struct dummy_request *req;
1718 u8 address;
1719 struct dummy_ep *ep = NULL;
1720 int type;
Alan Stern4d2f1102007-08-24 15:40:10 -04001721 int status = -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723 urb = urbp->urb;
Alan Sterneb231052007-08-21 15:40:36 -04001724 if (urb->unlinked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 goto return_urb;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001726 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
Alan Stern391eca92005-05-10 15:34:16 -04001727 continue;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001728 type = usb_pipetype(urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 /* used up this frame's non-periodic bandwidth?
1731 * FIXME there's infinite bandwidth for control and
1732 * periodic transfers ... unrealistic.
1733 */
1734 if (total <= 0 && type == PIPE_BULK)
1735 continue;
1736
1737 /* find the gadget's ep for this request (if configured) */
1738 address = usb_pipeendpoint (urb->pipe);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001739 if (usb_pipein(urb->pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 address |= USB_DIR_IN;
1741 ep = find_endpoint(dum, address);
1742 if (!ep) {
1743 /* set_configuration() disagreement */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001744 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 "no ep configured for urb %p\n",
1746 urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001747 status = -EPROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 goto return_urb;
1749 }
1750
1751 if (ep->already_seen)
1752 continue;
1753 ep->already_seen = 1;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001754 if (ep == &dum->ep[0] && urb->error_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 ep->setup_stage = 1; /* a new urb */
1756 urb->error_count = 0;
1757 }
1758 if (ep->halted && !ep->setup_stage) {
1759 /* NOTE: must not be iso! */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001760 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 ep->ep.name, urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001762 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 goto return_urb;
1764 }
1765 /* FIXME make sure both ends agree on maxpacket */
1766
1767 /* handle control requests */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001768 if (ep == &dum->ep[0] && ep->setup_stage) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 struct usb_ctrlrequest setup;
1770 int value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001772 setup = *(struct usb_ctrlrequest *) urb->setup_packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 /* paranoia, in case of stale queued data */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001774 list_for_each_entry(req, &ep->queue, queue) {
1775 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 req->req.status = -EOVERFLOW;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001777 dev_dbg(udc_dev(dum), "stale req = %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 req);
1779
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001780 spin_unlock(&dum->lock);
1781 req->req.complete(&ep->ep, &req->req);
1782 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 ep->already_seen = 0;
1784 goto restart;
1785 }
1786
1787 /* gadget driver never sees set_address or operations
1788 * on standard feature flags. some hardware doesn't
1789 * even expose them.
1790 */
1791 ep->last_io = jiffies;
1792 ep->setup_stage = 0;
1793 ep->halted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001795 value = handle_control_request(dum_hcd, urb, &setup,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001796 &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 /* gadget driver handles all other requests. block
1799 * until setup() returns; no reentrancy issues etc.
1800 */
1801 if (value > 0) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001802 spin_unlock(&dum->lock);
1803 value = dum->driver->setup(&dum->gadget,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 &setup);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001805 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 if (value >= 0) {
1808 /* no delays (max 64KB data stage) */
1809 limit = 64*1024;
1810 goto treat_control_like_bulk;
1811 }
1812 /* error, see below */
1813 }
1814
1815 if (value < 0) {
1816 if (value != -EOPNOTSUPP)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001817 dev_dbg(udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 "setup --> %d\n",
1819 value);
Alan Stern4d2f1102007-08-24 15:40:10 -04001820 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 urb->actual_length = 0;
1822 }
1823
1824 goto return_urb;
1825 }
1826
1827 /* non-control requests */
1828 limit = total;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001829 switch (usb_pipetype(urb->pipe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 case PIPE_ISOCHRONOUS:
1831 /* FIXME is it urb->interval since the last xfer?
1832 * use urb->iso_frame_desc[i].
1833 * complete whether or not ep has requests queued.
1834 * report random errors, to debug drivers.
1835 */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001836 limit = max(limit, periodic_bytes(dum, ep));
Alan Stern4d2f1102007-08-24 15:40:10 -04001837 status = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 break;
1839
1840 case PIPE_INTERRUPT:
1841 /* FIXME is it urb->interval since the last xfer?
1842 * this almost certainly polls too fast.
1843 */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001844 limit = max(limit, periodic_bytes(dum, ep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 /* FALLTHROUGH */
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 default:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001848treat_control_like_bulk:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 ep->last_io = jiffies;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001850 total = transfer(dum_hcd, urb, ep, limit, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 break;
1852 }
1853
1854 /* incomplete transfer? */
Alan Stern4d2f1102007-08-24 15:40:10 -04001855 if (status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 continue;
1857
1858return_urb:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001859 list_del(&urbp->urbp_list);
1860 kfree(urbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (ep)
1862 ep->already_seen = ep->setup_stage = 0;
1863
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001864 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001865 spin_unlock(&dum->lock);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001866 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001867 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 goto restart;
1870 }
1871
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001872 if (list_empty(&dum_hcd->urbp_list)) {
1873 usb_put_dev(dum_hcd->udev);
1874 dum_hcd->udev = NULL;
1875 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern391eca92005-05-10 15:34:16 -04001876 /* want a 1 msec delay here */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001877 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
1879
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001880 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
1883/*-------------------------------------------------------------------------*/
1884
1885#define PORT_C_MASK \
Alan Sternc2db8b52005-04-29 16:30:48 -04001886 ((USB_PORT_STAT_C_CONNECTION \
1887 | USB_PORT_STAT_C_ENABLE \
1888 | USB_PORT_STAT_C_SUSPEND \
1889 | USB_PORT_STAT_C_OVERCURRENT \
1890 | USB_PORT_STAT_C_RESET) << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001892static int dummy_hub_status(struct usb_hcd *hcd, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001894 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 unsigned long flags;
Alan Stern391eca92005-05-10 15:34:16 -04001896 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001898 dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001900 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001901 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001902 goto done;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001903
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001904 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1905 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1906 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1907 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001908 }
1909
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001910 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 *buf = (1 << 1);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001912 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1913 dum_hcd->port_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 retval = 1;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001915 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001916 usb_hcd_resume_root_hub(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
Alan Stern391eca92005-05-10 15:34:16 -04001918done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001919 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return retval;
1921}
1922
1923static inline void
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001924ss_hub_descriptor(struct usb_hub_descriptor *desc)
1925{
1926 memset(desc, 0, sizeof *desc);
1927 desc->bDescriptorType = 0x2a;
1928 desc->bDescLength = 12;
1929 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1930 desc->bNbrPorts = 1;
1931 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1932 desc->u.ss.DeviceRemovable = 0xffff;
1933}
1934
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001935static inline void hub_descriptor(struct usb_hub_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001937 memset(desc, 0, sizeof *desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 desc->bDescriptorType = 0x29;
1939 desc->bDescLength = 9;
Al Virofd05e722008-04-28 07:00:16 +01001940 desc->wHubCharacteristics = cpu_to_le16(0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 desc->bNbrPorts = 1;
John Youndbe79bb2001-09-17 00:00:00 -07001942 desc->u.hs.DeviceRemovable[0] = 0xff;
1943 desc->u.hs.DeviceRemovable[1] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001946static int dummy_hub_control(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 struct usb_hcd *hcd,
1948 u16 typeReq,
1949 u16 wValue,
1950 u16 wIndex,
1951 char *buf,
1952 u16 wLength
1953) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001954 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 int retval = 0;
1956 unsigned long flags;
1957
Alan Stern541c7d42010-06-22 16:39:10 -04001958 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001959 return -ETIMEDOUT;
1960
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001961 dum_hcd = hcd_to_dummy_hcd(hcd);
1962
1963 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 switch (typeReq) {
1965 case ClearHubFeature:
1966 break;
1967 case ClearPortFeature:
1968 switch (wValue) {
1969 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001970 if (hcd->speed == HCD_USB3) {
1971 dev_dbg(dummy_dev(dum_hcd),
1972 "USB_PORT_FEAT_SUSPEND req not "
1973 "supported for USB 3.0 roothub\n");
1974 goto error;
1975 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001976 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 /* 20msec resume signaling */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001978 dum_hcd->resuming = 1;
1979 dum_hcd->re_timeout = jiffies +
Alan Sternf1c39fa2005-05-03 16:24:04 -04001980 msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
1982 break;
1983 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001984 if (hcd->speed == HCD_USB3) {
1985 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1986 dev_dbg(dummy_dev(dum_hcd),
1987 "power-off\n");
1988 } else
1989 if (dum_hcd->port_status &
1990 USB_SS_PORT_STAT_POWER)
1991 dev_dbg(dummy_dev(dum_hcd),
1992 "power-off\n");
Alan Sternf1c39fa2005-05-03 16:24:04 -04001993 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001995 dum_hcd->port_status &= ~(1 << wValue);
1996 set_link_state(dum_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
1998 break;
1999 case GetHubDescriptor:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002000 if (hcd->speed == HCD_USB3 &&
2001 (wLength < USB_DT_SS_HUB_SIZE ||
2002 wValue != (USB_DT_SS_HUB << 8))) {
2003 dev_dbg(dummy_dev(dum_hcd),
2004 "Wrong hub descriptor type for "
2005 "USB 3.0 roothub.\n");
2006 goto error;
2007 }
2008 if (hcd->speed == HCD_USB3)
2009 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
2010 else
2011 hub_descriptor((struct usb_hub_descriptor *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 break;
2013 case GetHubStatus:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002014 *(__le32 *) buf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 break;
2016 case GetPortStatus:
2017 if (wIndex != 1)
2018 retval = -EPIPE;
2019
2020 /* whoever resets or resumes must GetPortStatus to
2021 * complete it!!
2022 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002023 if (dum_hcd->resuming &&
2024 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2025 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
2026 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002028 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
2029 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2030 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
2031 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
2032 if (dum_hcd->dum->pullup) {
2033 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002034
2035 if (hcd->speed < HCD_USB3) {
2036 switch (dum_hcd->dum->gadget.speed) {
2037 case USB_SPEED_HIGH:
2038 dum_hcd->port_status |=
2039 USB_PORT_STAT_HIGH_SPEED;
2040 break;
2041 case USB_SPEED_LOW:
2042 dum_hcd->dum->gadget.ep0->
2043 maxpacket = 8;
2044 dum_hcd->port_status |=
2045 USB_PORT_STAT_LOW_SPEED;
2046 break;
2047 default:
2048 dum_hcd->dum->gadget.speed =
2049 USB_SPEED_FULL;
2050 break;
2051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 }
2053 }
2054 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002055 set_link_state(dum_hcd);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002056 ((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status);
2057 ((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 break;
2059 case SetHubFeature:
2060 retval = -EPIPE;
2061 break;
2062 case SetPortFeature:
2063 switch (wValue) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002064 case USB_PORT_FEAT_LINK_STATE:
2065 if (hcd->speed != HCD_USB3) {
2066 dev_dbg(dummy_dev(dum_hcd),
2067 "USB_PORT_FEAT_LINK_STATE req not "
2068 "supported for USB 2.0 roothub\n");
2069 goto error;
2070 }
2071 /*
2072 * Since this is dummy we don't have an actual link so
2073 * there is nothing to do for the SET_LINK_STATE cmd
2074 */
2075 break;
2076 case USB_PORT_FEAT_U1_TIMEOUT:
2077 case USB_PORT_FEAT_U2_TIMEOUT:
2078 /* TODO: add suspend/resume support! */
2079 if (hcd->speed != HCD_USB3) {
2080 dev_dbg(dummy_dev(dum_hcd),
2081 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
2082 "supported for USB 2.0 roothub\n");
2083 goto error;
2084 }
2085 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002087 /* Applicable only for USB2.0 hub */
2088 if (hcd->speed == HCD_USB3) {
2089 dev_dbg(dummy_dev(dum_hcd),
2090 "USB_PORT_FEAT_SUSPEND req not "
2091 "supported for USB 3.0 roothub\n");
2092 goto error;
2093 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002094 if (dum_hcd->active) {
2095 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
Alan Sternf1c39fa2005-05-03 16:24:04 -04002096
2097 /* HNP would happen here; for now we
2098 * assume b_bus_req is always true.
2099 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002100 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002101 if (((1 << USB_DEVICE_B_HNP_ENABLE)
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002102 & dum_hcd->dum->devstatus) != 0)
2103 dev_dbg(dummy_dev(dum_hcd),
Alan Stern5742b0c2005-05-02 11:25:17 -04002104 "no HNP yet!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
2106 break;
Alan Sternf1c39fa2005-05-03 16:24:04 -04002107 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002108 if (hcd->speed == HCD_USB3)
2109 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
2110 else
2111 dum_hcd->port_status |= USB_PORT_STAT_POWER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002112 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002113 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002114 case USB_PORT_FEAT_BH_PORT_RESET:
2115 /* Applicable only for USB3.0 hub */
2116 if (hcd->speed != HCD_USB3) {
2117 dev_dbg(dummy_dev(dum_hcd),
2118 "USB_PORT_FEAT_BH_PORT_RESET req not "
2119 "supported for USB 2.0 roothub\n");
2120 goto error;
2121 }
2122 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 case USB_PORT_FEAT_RESET:
Alan Sternf1c39fa2005-05-03 16:24:04 -04002124 /* if it's already enabled, disable */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002125 if (hcd->speed == HCD_USB3) {
2126 dum_hcd->port_status = 0;
2127 dum_hcd->port_status =
2128 (USB_SS_PORT_STAT_POWER |
2129 USB_PORT_STAT_CONNECTION |
2130 USB_PORT_STAT_RESET);
2131 } else
2132 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
Alan Sternf1c39fa2005-05-03 16:24:04 -04002133 | USB_PORT_STAT_LOW_SPEED
2134 | USB_PORT_STAT_HIGH_SPEED);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002135 /*
2136 * We want to reset device status. All but the
2137 * Self powered feature
2138 */
2139 dum_hcd->dum->devstatus &=
2140 (1 << USB_DEVICE_SELF_POWERED);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002141 /*
2142 * FIXME USB3.0: what is the correct reset signaling
2143 * interval? Is it still 50msec as for HS?
2144 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002145 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002146 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 default:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002148 if (hcd->speed == HCD_USB3) {
2149 if ((dum_hcd->port_status &
2150 USB_SS_PORT_STAT_POWER) != 0) {
2151 dum_hcd->port_status |= (1 << wValue);
2152 set_link_state(dum_hcd);
2153 }
2154 } else
2155 if ((dum_hcd->port_status &
2156 USB_PORT_STAT_POWER) != 0) {
2157 dum_hcd->port_status |= (1 << wValue);
2158 set_link_state(dum_hcd);
2159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 }
2161 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002162 case GetPortErrorCount:
2163 if (hcd->speed != HCD_USB3) {
2164 dev_dbg(dummy_dev(dum_hcd),
2165 "GetPortErrorCount req not "
2166 "supported for USB 2.0 roothub\n");
2167 goto error;
2168 }
2169 /* We'll always return 0 since this is a dummy hub */
2170 *(__le32 *) buf = cpu_to_le32(0);
2171 break;
2172 case SetHubDepth:
2173 if (hcd->speed != HCD_USB3) {
2174 dev_dbg(dummy_dev(dum_hcd),
2175 "SetHubDepth req not supported for "
2176 "USB 2.0 roothub\n");
2177 goto error;
2178 }
2179 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002181 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 "hub control req%04x v%04x i%04x l%d\n",
2183 typeReq, wValue, wIndex, wLength);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002184error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 /* "protocol stall" on error */
2186 retval = -EPIPE;
2187 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002188 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Stern685eb932005-05-03 16:27:26 -04002189
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002190 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002191 usb_hcd_poll_rh_status(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return retval;
2193}
2194
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002195static int dummy_bus_suspend(struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002196{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002197 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002198
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002199 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern3cf0a222005-11-29 12:08:15 -05002200
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002201 spin_lock_irq(&dum_hcd->dum->lock);
2202 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2203 set_link_state(dum_hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002204 hcd->state = HC_STATE_SUSPENDED;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002205 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern391eca92005-05-10 15:34:16 -04002206 return 0;
2207}
2208
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002209static int dummy_bus_resume(struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002210{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002211 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002212 int rc = 0;
2213
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002214 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002215
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002216 spin_lock_irq(&dum_hcd->dum->lock);
Alan Stern541c7d42010-06-22 16:39:10 -04002217 if (!HCD_HW_ACCESSIBLE(hcd)) {
Alan Sterncfa59da2007-06-21 16:25:35 -04002218 rc = -ESHUTDOWN;
Alan Stern3cf0a222005-11-29 12:08:15 -05002219 } else {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002220 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2221 set_link_state(dum_hcd);
2222 if (!list_empty(&dum_hcd->urbp_list))
2223 mod_timer(&dum_hcd->timer, jiffies);
Alan Stern3cf0a222005-11-29 12:08:15 -05002224 hcd->state = HC_STATE_RUNNING;
2225 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002226 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern3cf0a222005-11-29 12:08:15 -05002227 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002228}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230/*-------------------------------------------------------------------------*/
2231
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002232static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002234 int ep = usb_pipeendpoint(urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002236 return snprintf(buf, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 "urb/%p %s ep%d%s%s len %d/%d\n",
2238 urb,
2239 ({ char *s;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002240 switch (urb->dev->speed) {
2241 case USB_SPEED_LOW:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002242 s = "ls";
2243 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002244 case USB_SPEED_FULL:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002245 s = "fs";
2246 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002247 case USB_SPEED_HIGH:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002248 s = "hs";
2249 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002250 case USB_SPEED_SUPER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002251 s = "ss";
2252 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002253 default:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002254 s = "?";
2255 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 }; s; }),
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002257 ep, ep ? (usb_pipein(urb->pipe) ? "in" : "out") : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 ({ char *s; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002259 switch (usb_pipetype(urb->pipe)) { \
2260 case PIPE_CONTROL: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002261 s = ""; \
2262 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002263 case PIPE_BULK: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002264 s = "-bulk"; \
2265 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002266 case PIPE_INTERRUPT: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002267 s = "-int"; \
2268 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002269 default: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002270 s = "-iso"; \
2271 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002272 }; s; }),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 urb->actual_length, urb->transfer_buffer_length);
2274}
2275
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002276static ssize_t show_urbs(struct device *dev, struct device_attribute *attr,
2277 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002279 struct usb_hcd *hcd = dev_get_drvdata(dev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002280 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 struct urbp *urbp;
2282 size_t size = 0;
2283 unsigned long flags;
2284
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002285 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2286 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 size_t temp;
2288
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002289 temp = show_urb(buf, PAGE_SIZE - size, urbp->urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 buf += temp;
2291 size += temp;
2292 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002293 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 return size;
2296}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002297static DEVICE_ATTR(urbs, S_IRUGO, show_urbs, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002299static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2300{
2301 init_timer(&dum_hcd->timer);
2302 dum_hcd->timer.function = dummy_timer;
2303 dum_hcd->timer.data = (unsigned long)dum_hcd;
2304 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002305 dum_hcd->stream_en_ep = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002306 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2307 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2308 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2309 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2310#ifdef CONFIG_USB_OTG
2311 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2312#endif
2313 return 0;
2314
2315 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2316 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2317}
2318
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002319static int dummy_start(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002321 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 /*
2324 * MASTER side init ... we emulate a root hub that'll only ever
2325 * talk to one device (the slave side). Also appears in sysfs,
2326 * just like more familiar pci-based HCDs.
2327 */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002328 if (!usb_hcd_is_primary_hcd(hcd))
2329 return dummy_start_ss(dum_hcd);
2330
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002331 spin_lock_init(&dum_hcd->dum->lock);
2332 init_timer(&dum_hcd->timer);
2333 dum_hcd->timer.function = dummy_timer;
2334 dum_hcd->timer.data = (unsigned long)dum_hcd;
2335 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002337 INIT_LIST_HEAD(&dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Alan Sterncaf29f62007-12-06 11:10:39 -05002339 hcd->power_budget = POWER_BUDGET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 hcd->state = HC_STATE_RUNNING;
Alan Stern685eb932005-05-03 16:27:26 -04002341 hcd->uses_new_polling = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Alan Stern5742b0c2005-05-02 11:25:17 -04002343#ifdef CONFIG_USB_OTG
2344 hcd->self.otg_port = 1;
2345#endif
2346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002348 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349}
2350
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002351static void dummy_stop(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
2353 struct dummy *dum;
2354
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002355 dum = hcd_to_dummy_hcd(hcd)->dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002356 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2357 usb_gadget_unregister_driver(dum->driver);
2358 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359}
2360
2361/*-------------------------------------------------------------------------*/
2362
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002363static int dummy_h_get_frame(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002365 return dummy_g_get_frame(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002368static int dummy_setup(struct usb_hcd *hcd)
2369{
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01002370 hcd->self.sg_tablesize = ~0;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002371 if (usb_hcd_is_primary_hcd(hcd)) {
2372 the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
2373 the_controller.hs_hcd->dum = &the_controller;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002374 /*
2375 * Mark the first roothub as being USB 2.0.
2376 * The USB 3.0 roothub will be registered later by
2377 * dummy_hcd_probe()
2378 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002379 hcd->speed = HCD_USB2;
2380 hcd->self.root_hub->speed = USB_SPEED_HIGH;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002381 } else {
2382 the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
2383 the_controller.ss_hcd->dum = &the_controller;
2384 hcd->speed = HCD_USB3;
2385 hcd->self.root_hub->speed = USB_SPEED_SUPER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002386 }
2387 return 0;
2388}
2389
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002390/* Change a group of bulk endpoints to support multiple stream IDs */
Sebastian Andrzej Siewiord81f3e42012-01-09 13:15:00 +01002391static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002392 struct usb_host_endpoint **eps, unsigned int num_eps,
2393 unsigned int num_streams, gfp_t mem_flags)
2394{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002395 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2396 unsigned long flags;
2397 int max_stream;
2398 int ret_streams = num_streams;
2399 unsigned int index;
2400 unsigned int i;
2401
2402 if (!num_eps)
2403 return -EINVAL;
2404
2405 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2406 for (i = 0; i < num_eps; i++) {
2407 index = dummy_get_ep_idx(&eps[i]->desc);
2408 if ((1 << index) & dum_hcd->stream_en_ep) {
2409 ret_streams = -EINVAL;
2410 goto out;
2411 }
2412 max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp);
2413 if (!max_stream) {
2414 ret_streams = -EINVAL;
2415 goto out;
2416 }
2417 if (max_stream < ret_streams) {
2418 dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u "
2419 "stream IDs.\n",
2420 eps[i]->desc.bEndpointAddress,
2421 max_stream);
2422 ret_streams = max_stream;
2423 }
2424 }
2425
2426 for (i = 0; i < num_eps; i++) {
2427 index = dummy_get_ep_idx(&eps[i]->desc);
2428 dum_hcd->stream_en_ep |= 1 << index;
2429 set_max_streams_for_pipe(dum_hcd,
2430 usb_endpoint_num(&eps[i]->desc), ret_streams);
2431 }
2432out:
2433 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2434 return ret_streams;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002435}
2436
2437/* Reverts a group of bulk endpoints back to not using stream IDs. */
Sebastian Andrzej Siewiord81f3e42012-01-09 13:15:00 +01002438static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002439 struct usb_host_endpoint **eps, unsigned int num_eps,
2440 gfp_t mem_flags)
2441{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002442 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2443 unsigned long flags;
2444 int ret;
2445 unsigned int index;
2446 unsigned int i;
2447
2448 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2449 for (i = 0; i < num_eps; i++) {
2450 index = dummy_get_ep_idx(&eps[i]->desc);
2451 if (!((1 << index) & dum_hcd->stream_en_ep)) {
2452 ret = -EINVAL;
2453 goto out;
2454 }
2455 }
2456
2457 for (i = 0; i < num_eps; i++) {
2458 index = dummy_get_ep_idx(&eps[i]->desc);
2459 dum_hcd->stream_en_ep &= ~(1 << index);
2460 set_max_streams_for_pipe(dum_hcd,
2461 usb_endpoint_num(&eps[i]->desc), 0);
2462 }
2463 ret = 0;
2464out:
2465 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2466 return ret;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002467}
2468
2469static struct hc_driver dummy_hcd = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 .description = (char *) driver_name,
2471 .product_desc = "Dummy host controller",
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002472 .hcd_priv_size = sizeof(struct dummy_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002474 .flags = HCD_USB3 | HCD_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002476 .reset = dummy_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 .start = dummy_start,
2478 .stop = dummy_stop,
2479
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002480 .urb_enqueue = dummy_urb_enqueue,
2481 .urb_dequeue = dummy_urb_dequeue,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002483 .get_frame_number = dummy_h_get_frame,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002485 .hub_status_data = dummy_hub_status,
2486 .hub_control = dummy_hub_control,
Alan Stern0c0382e2005-10-13 17:08:02 -04002487 .bus_suspend = dummy_bus_suspend,
2488 .bus_resume = dummy_bus_resume,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002489
2490 .alloc_streams = dummy_alloc_streams,
2491 .free_streams = dummy_free_streams,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492};
2493
Alan Stern8364d6b2005-11-14 12:16:30 -05002494static int dummy_hcd_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002496 struct usb_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002497 struct usb_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 int retval;
2499
Alan Stern8364d6b2005-11-14 12:16:30 -05002500 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002502 if (!mod_data.is_super_speed)
2503 dummy_hcd.flags = HCD_USB2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002504 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2505 if (!hs_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 return -ENOMEM;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002507 hs_hcd->has_tt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002509 retval = usb_add_hcd(hs_hcd, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 if (retval != 0) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002511 usb_put_hcd(hs_hcd);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002512 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002514
2515 if (mod_data.is_super_speed) {
2516 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2517 dev_name(&pdev->dev), hs_hcd);
2518 if (!ss_hcd) {
2519 retval = -ENOMEM;
2520 goto dealloc_usb2_hcd;
2521 }
2522
2523 retval = usb_add_hcd(ss_hcd, 0, 0);
2524 if (retval)
2525 goto put_usb3_hcd;
2526 }
2527 return 0;
2528
2529put_usb3_hcd:
2530 usb_put_hcd(ss_hcd);
2531dealloc_usb2_hcd:
2532 usb_put_hcd(hs_hcd);
2533 the_controller.hs_hcd = the_controller.ss_hcd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 return retval;
2535}
2536
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002537static int dummy_hcd_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002539 struct dummy *dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002541 dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002542
2543 if (dum->ss_hcd) {
2544 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2545 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2546 }
2547
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002548 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2549 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002550
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002551 the_controller.hs_hcd = NULL;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002552 the_controller.ss_hcd = NULL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002553
Alan Sternd9b76252005-05-03 16:15:43 -04002554 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555}
2556
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002557static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state)
Alan Stern391eca92005-05-10 15:34:16 -04002558{
2559 struct usb_hcd *hcd;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002560 struct dummy_hcd *dum_hcd;
Alan Stern3cf0a222005-11-29 12:08:15 -05002561 int rc = 0;
Alan Stern391eca92005-05-10 15:34:16 -04002562
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002563 dev_dbg(&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002564
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002565 hcd = platform_get_drvdata(pdev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002566 dum_hcd = hcd_to_dummy_hcd(hcd);
2567 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern3cf0a222005-11-29 12:08:15 -05002568 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2569 rc = -EBUSY;
2570 } else
2571 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2572 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002573}
2574
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002575static int dummy_hcd_resume(struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04002576{
2577 struct usb_hcd *hcd;
2578
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002579 dev_dbg(&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002580
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002581 hcd = platform_get_drvdata(pdev);
Alan Stern3cf0a222005-11-29 12:08:15 -05002582 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002583 usb_hcd_poll_rh_status(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002584 return 0;
2585}
2586
Russell King3ae5eae2005-11-09 22:32:44 +00002587static struct platform_driver dummy_hcd_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04002588 .probe = dummy_hcd_probe,
2589 .remove = dummy_hcd_remove,
Alan Stern391eca92005-05-10 15:34:16 -04002590 .suspend = dummy_hcd_suspend,
2591 .resume = dummy_hcd_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002592 .driver = {
2593 .name = (char *) driver_name,
2594 .owner = THIS_MODULE,
2595 },
Alan Sternd9b76252005-05-03 16:15:43 -04002596};
2597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598/*-------------------------------------------------------------------------*/
2599
Alan Sterna89a2cd2008-04-07 15:03:25 -04002600static struct platform_device *the_udc_pdev;
2601static struct platform_device *the_hcd_pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002603static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002605 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002607 if (usb_disabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 return -ENODEV;
Alan Sternd9b76252005-05-03 16:15:43 -04002609
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +03002610 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2611 return -EINVAL;
2612
Alan Sterna89a2cd2008-04-07 15:03:25 -04002613 the_hcd_pdev = platform_device_alloc(driver_name, -1);
2614 if (!the_hcd_pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 return retval;
Alan Sterna89a2cd2008-04-07 15:03:25 -04002616 the_udc_pdev = platform_device_alloc(gadget_name, -1);
2617 if (!the_udc_pdev)
2618 goto err_alloc_udc;
Alan Sternd9b76252005-05-03 16:15:43 -04002619
Alan Sterna89a2cd2008-04-07 15:03:25 -04002620 retval = platform_driver_register(&dummy_hcd_driver);
2621 if (retval < 0)
2622 goto err_register_hcd_driver;
2623 retval = platform_driver_register(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002624 if (retval < 0)
2625 goto err_register_udc_driver;
2626
Alan Sterna89a2cd2008-04-07 15:03:25 -04002627 retval = platform_device_add(the_hcd_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002628 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002629 goto err_add_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002630 if (!the_controller.hs_hcd ||
2631 (!the_controller.ss_hcd && mod_data.is_super_speed)) {
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002632 /*
2633 * The hcd was added successfully but its probe function failed
2634 * for some reason.
2635 */
2636 retval = -EINVAL;
2637 goto err_add_udc;
2638 }
Alan Sterna89a2cd2008-04-07 15:03:25 -04002639 retval = platform_device_add(the_udc_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002640 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002641 goto err_add_udc;
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002642 if (!platform_get_drvdata(the_udc_pdev)) {
2643 /*
2644 * The udc was added successfully but its probe function failed
2645 * for some reason.
2646 */
2647 retval = -EINVAL;
2648 goto err_probe_udc;
2649 }
Alan Sternd9b76252005-05-03 16:15:43 -04002650 return retval;
2651
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002652err_probe_udc:
2653 platform_device_del(the_udc_pdev);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002654err_add_udc:
2655 platform_device_del(the_hcd_pdev);
2656err_add_hcd:
2657 platform_driver_unregister(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002658err_register_udc_driver:
Alan Sterna89a2cd2008-04-07 15:03:25 -04002659 platform_driver_unregister(&dummy_hcd_driver);
2660err_register_hcd_driver:
2661 platform_device_put(the_udc_pdev);
2662err_alloc_udc:
2663 platform_device_put(the_hcd_pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 return retval;
2665}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002666module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002668static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002670 platform_device_unregister(the_udc_pdev);
2671 platform_device_unregister(the_hcd_pdev);
2672 platform_driver_unregister(&dummy_udc_driver);
2673 platform_driver_unregister(&dummy_hcd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002675module_exit(cleanup);