blob: 9170a4c7ced412724db3645eb58cf3e5f61aa3a7 [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 Siewior18f2cba2012-01-12 12:53:15 +0100602 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
604 return;
605
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100606 req = usb_request_to_dummy_request(_req);
607 WARN_ON(!list_empty(&req->queue));
608 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100611static void fifo_complete(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613}
614
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100615static int dummy_queue(struct usb_ep *_ep, struct usb_request *_req,
Al Viro55016f12005-10-21 03:21:58 -0400616 gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 struct dummy_ep *ep;
619 struct dummy_request *req;
620 struct dummy *dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300621 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 unsigned long flags;
623
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100624 req = usb_request_to_dummy_request(_req);
625 if (!_req || !list_empty(&req->queue) || !_req->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return -EINVAL;
627
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100628 ep = usb_ep_to_dummy_ep(_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (!_ep || (!ep->desc && _ep->name != ep0name))
630 return -EINVAL;
631
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100632 dum = ep_to_dummy(ep);
Sebastian Andrzej Siewior719e52c2011-06-16 20:36:57 +0200633 dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300634 if (!dum->driver || !is_enabled(dum_hcd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return -ESHUTDOWN;
636
637#if 0
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100638 dev_dbg(udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 ep, _req, _ep->name, _req->length, _req->buf);
640#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 _req->status = -EINPROGRESS;
642 _req->actual = 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100643 spin_lock_irqsave(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 /* implement an emulated single-request FIFO */
646 if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100647 list_empty(&dum->fifo_req.queue) &&
648 list_empty(&ep->queue) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 _req->length <= FIFO_SIZE) {
650 req = &dum->fifo_req;
651 req->req = *_req;
652 req->req.buf = dum->fifo_buf;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100653 memcpy(dum->fifo_buf, _req->buf, _req->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 req->req.context = dum;
655 req->req.complete = fifo_complete;
656
David Brownellc728df72008-07-26 08:06:24 -0700657 list_add_tail(&req->queue, &ep->queue);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100658 spin_unlock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 _req->actual = _req->length;
660 _req->status = 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100661 _req->complete(_ep, _req);
662 spin_lock(&dum->lock);
David Brownellc728df72008-07-26 08:06:24 -0700663 } else
664 list_add_tail(&req->queue, &ep->queue);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100665 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /* real hardware would likely enable transfers here, in case
668 * it'd been left NAKing.
669 */
670 return 0;
671}
672
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100673static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 struct dummy_ep *ep;
676 struct dummy *dum;
677 int retval = -EINVAL;
678 unsigned long flags;
679 struct dummy_request *req = NULL;
680
681 if (!_ep || !_req)
682 return retval;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100683 ep = usb_ep_to_dummy_ep(_ep);
684 dum = ep_to_dummy(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 if (!dum->driver)
687 return -ESHUTDOWN;
688
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100689 local_irq_save(flags);
690 spin_lock(&dum->lock);
691 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (&req->req == _req) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100693 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 _req->status = -ECONNRESET;
695 retval = 0;
696 break;
697 }
698 }
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100699 spin_unlock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 if (retval == 0) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100702 dev_dbg(udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 "dequeued req %p from %s, len %d buf %p\n",
704 req, _ep->name, _req->length, _req->buf);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100705 _req->complete(_ep, _req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100707 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return retval;
709}
710
711static int
Alan Stern851a5262008-08-14 15:48:30 -0400712dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 struct dummy_ep *ep;
715 struct dummy *dum;
716
717 if (!_ep)
718 return -EINVAL;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100719 ep = usb_ep_to_dummy_ep(_ep);
720 dum = ep_to_dummy(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (!dum->driver)
722 return -ESHUTDOWN;
723 if (!value)
Alan Stern851a5262008-08-14 15:48:30 -0400724 ep->halted = ep->wedged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100726 !list_empty(&ep->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return -EAGAIN;
Alan Stern851a5262008-08-14 15:48:30 -0400728 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 ep->halted = 1;
Alan Stern851a5262008-08-14 15:48:30 -0400730 if (wedged)
731 ep->wedged = 1;
732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /* FIXME clear emulated data toggle too */
734 return 0;
735}
736
Alan Stern851a5262008-08-14 15:48:30 -0400737static int
738dummy_set_halt(struct usb_ep *_ep, int value)
739{
740 return dummy_set_halt_and_wedge(_ep, value, 0);
741}
742
743static int dummy_set_wedge(struct usb_ep *_ep)
744{
745 if (!_ep || _ep->name == ep0name)
746 return -EINVAL;
747 return dummy_set_halt_and_wedge(_ep, 1, 1);
748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750static const struct usb_ep_ops dummy_ep_ops = {
751 .enable = dummy_enable,
752 .disable = dummy_disable,
753
754 .alloc_request = dummy_alloc_request,
755 .free_request = dummy_free_request,
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 .queue = dummy_queue,
758 .dequeue = dummy_dequeue,
759
760 .set_halt = dummy_set_halt,
Alan Stern851a5262008-08-14 15:48:30 -0400761 .set_wedge = dummy_set_wedge,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762};
763
764/*-------------------------------------------------------------------------*/
765
766/* there are both host and device side versions of this call ... */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100767static int dummy_g_get_frame(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 struct timeval tv;
770
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100771 do_gettimeofday(&tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return tv.tv_usec / 1000;
773}
774
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100775static int dummy_wakeup(struct usb_gadget *_gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300777 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300779 dum_hcd = gadget_to_dummy_hcd(_gadget);
780 if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
Alan Stern5742b0c2005-05-02 11:25:17 -0400781 | (1 << USB_DEVICE_REMOTE_WAKEUP))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return -EINVAL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300783 if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
Alan Stern391eca92005-05-10 15:34:16 -0400784 return -ENOLINK;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300785 if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
786 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
Alan Stern391eca92005-05-10 15:34:16 -0400787 return -EIO;
788
789 /* FIXME: What if the root hub is suspended but the port isn't? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 /* hub notices our request, issues downstream resume, etc */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300792 dum_hcd->resuming = 1;
793 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
794 mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return 0;
796}
797
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100798static int dummy_set_selfpowered(struct usb_gadget *_gadget, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 struct dummy *dum;
801
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100802 dum = gadget_to_dummy_hcd(_gadget)->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (value)
804 dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
805 else
806 dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
807 return 0;
808}
809
Sebastian Andrzej Siewiord2621272012-01-09 13:14:59 +0100810static void dummy_udc_update_ep0(struct dummy *dum)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200811{
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100812 if (dum->gadget.speed == USB_SPEED_SUPER)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200813 dum->ep[0].ep.maxpacket = 9;
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100814 else
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200815 dum->ep[0].ep.maxpacket = 64;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200816}
817
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100818static int dummy_pullup(struct usb_gadget *_gadget, int value)
Alan Sternf1c39fa2005-05-03 16:24:04 -0400819{
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200820 struct dummy_hcd *dum_hcd;
Alan Sternf1c39fa2005-05-03 16:24:04 -0400821 struct dummy *dum;
822 unsigned long flags;
823
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200824 dum = gadget_dev_to_dummy(&_gadget->dev);
825
826 if (value && dum->driver) {
827 if (mod_data.is_super_speed)
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100828 dum->gadget.speed = dum->driver->max_speed;
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200829 else if (mod_data.is_high_speed)
830 dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100831 dum->driver->max_speed);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200832 else
833 dum->gadget.speed = USB_SPEED_FULL;
Sebastian Andrzej Siewiord2621272012-01-09 13:14:59 +0100834 dummy_udc_update_ep0(dum);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200835
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100836 if (dum->gadget.speed < dum->driver->max_speed)
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200837 dev_dbg(udc_dev(dum), "This device can perform faster"
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100838 " if you connect it to a %s port...\n",
839 usb_speed_string(dum->driver->max_speed));
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200840 }
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200841 dum_hcd = gadget_to_dummy_hcd(_gadget);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200842
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100843 spin_lock_irqsave(&dum->lock, flags);
Alan Sternf1c39fa2005-05-03 16:24:04 -0400844 dum->pullup = (value != 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200845 set_link_state(dum_hcd);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100846 spin_unlock_irqrestore(&dum->lock, flags);
Sebastian Andrzej Siewiorb5738412011-06-23 14:26:14 +0200847
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +0200848 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Sternf1c39fa2005-05-03 16:24:04 -0400849 return 0;
850}
851
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200852static int dummy_udc_start(struct usb_gadget *g,
853 struct usb_gadget_driver *driver);
854static int dummy_udc_stop(struct usb_gadget *g,
855 struct usb_gadget_driver *driver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857static const struct usb_gadget_ops dummy_ops = {
858 .get_frame = dummy_g_get_frame,
859 .wakeup = dummy_wakeup,
860 .set_selfpowered = dummy_set_selfpowered,
Alan Sternf1c39fa2005-05-03 16:24:04 -0400861 .pullup = dummy_pullup,
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200862 .udc_start = dummy_udc_start,
863 .udc_stop = dummy_udc_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864};
865
866/*-------------------------------------------------------------------------*/
867
868/* "function" sysfs attribute */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100869static ssize_t show_function(struct device *dev, struct device_attribute *attr,
870 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100872 struct dummy *dum = gadget_dev_to_dummy(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 if (!dum->driver || !dum->driver->function)
875 return 0;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100876 return scnprintf(buf, PAGE_SIZE, "%s\n", dum->driver->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100878static DEVICE_ATTR(function, S_IRUGO, show_function, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880/*-------------------------------------------------------------------------*/
881
882/*
883 * Driver registration/unregistration.
884 *
885 * This is basically hardware-specific; there's usually only one real USB
886 * device (not host) controller since that's how USB devices are intended
887 * to work. So most implementations of these api calls will rely on the
888 * fact that only one driver will ever bind to the hardware. But curious
889 * hardware can be built with discrete components, so the gadget API doesn't
890 * require that assumption.
891 *
892 * For this emulator, it might be convenient to create a usb slave device
893 * for each driver that registers: just add to a big root hub.
894 */
895
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200896static int dummy_udc_start(struct usb_gadget *g,
897 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200899 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
900 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Michal Nazarewicz7177aed2011-11-19 18:27:38 +0100902 if (driver->max_speed == USB_SPEED_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return -EINVAL;
904
905 /*
906 * SLAVE side init ... the layer above hardware, which
907 * can't enumerate without help from the driver we're binding.
908 */
Alan Stern5742b0c2005-05-02 11:25:17 -0400909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 dum->devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 dum->driver = driver;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100913 dev_dbg(udc_dev(dum), "binding gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return 0;
916}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200918static int dummy_udc_stop(struct usb_gadget *g,
919 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Sebastian Andrzej Siewioraa074732011-06-23 14:26:17 +0200921 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
922 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100924 dev_dbg(udc_dev(dum), "unregister gadget driver '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 driver->driver.name);
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 dum->driver = NULL;
Sebastian Andrzej Siewior25427872011-06-16 20:36:55 +0200928
929 dummy_pullup(&dum->gadget, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return 0;
931}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933#undef is_enabled
934
Alan Sternd9b76252005-05-03 16:15:43 -0400935/* The gadget structure is stored inside the hcd structure and will be
936 * released along with it. */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100937static void dummy_gadget_release(struct device *dev)
Alan Sternd9b76252005-05-03 16:15:43 -0400938{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300939 return;
Alan Sternd9b76252005-05-03 16:15:43 -0400940}
941
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200942static void init_dummy_udc_hw(struct dummy *dum)
943{
944 int i;
945
946 INIT_LIST_HEAD(&dum->gadget.ep_list);
947 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
948 struct dummy_ep *ep = &dum->ep[i];
949
950 if (!ep_name[i])
951 break;
952 ep->ep.name = ep_name[i];
953 ep->ep.ops = &dummy_ep_ops;
954 list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
955 ep->halted = ep->wedged = ep->already_seen =
956 ep->setup_stage = 0;
957 ep->ep.maxpacket = ~0;
Sebastian Andrzej Siewiorc6884192012-01-09 13:14:56 +0100958 ep->ep.max_streams = 16;
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200959 ep->last_io = jiffies;
960 ep->gadget = &dum->gadget;
961 ep->desc = NULL;
962 INIT_LIST_HEAD(&ep->queue);
963 }
964
965 dum->gadget.ep0 = &dum->ep[0].ep;
966 list_del_init(&dum->ep[0].ep.ep_list);
967 INIT_LIST_HEAD(&dum->fifo_req.queue);
Sebastian Andrzej Siewiorf8744d42011-06-23 14:26:13 +0200968
969#ifdef CONFIG_USB_OTG
970 dum->gadget.is_otg = 1;
971#endif
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200972}
973
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100974static int dummy_udc_probe(struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -0400975{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +0300976 struct dummy *dum = &the_controller;
Alan Sternd9b76252005-05-03 16:15:43 -0400977 int rc;
978
979 dum->gadget.name = gadget_name;
980 dum->gadget.ops = &dummy_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +0100981 dum->gadget.max_speed = USB_SPEED_SUPER;
Alan Sternd9b76252005-05-03 16:15:43 -0400982
Kay Sievers0031a062008-05-02 06:02:41 +0200983 dev_set_name(&dum->gadget.dev, "gadget");
Alan Stern8364d6b2005-11-14 12:16:30 -0500984 dum->gadget.dev.parent = &pdev->dev;
Alan Sternd9b76252005-05-03 16:15:43 -0400985 dum->gadget.dev.release = dummy_gadget_release;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100986 rc = device_register(&dum->gadget.dev);
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530987 if (rc < 0) {
988 put_device(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -0400989 return rc;
Rahul Ruikar75d87cd2010-10-07 09:40:45 +0530990 }
Alan Sternd9b76252005-05-03 16:15:43 -0400991
Sebastian Andrzej Siewior0fb57592011-06-23 14:26:12 +0200992 init_dummy_udc_hw(dum);
993
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +0300994 rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
995 if (rc < 0)
996 goto err_udc;
997
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +0100998 rc = device_create_file(&dum->gadget.dev, &dev_attr_function);
Alan Sternefd54a32006-09-25 11:55:56 -0400999 if (rc < 0)
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001000 goto err_dev;
1001 platform_set_drvdata(pdev, dum);
1002 return rc;
1003
1004err_dev:
1005 usb_del_gadget_udc(&dum->gadget);
1006err_udc:
1007 device_unregister(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -04001008 return rc;
1009}
1010
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001011static int dummy_udc_remove(struct platform_device *pdev)
Alan Sternd9b76252005-05-03 16:15:43 -04001012{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001013 struct dummy *dum = platform_get_drvdata(pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04001014
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001015 usb_del_gadget_udc(&dum->gadget);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001016 platform_set_drvdata(pdev, NULL);
1017 device_remove_file(&dum->gadget.dev, &dev_attr_function);
1018 device_unregister(&dum->gadget.dev);
Alan Sternd9b76252005-05-03 16:15:43 -04001019 return 0;
1020}
1021
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001022static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1023 int suspend)
Alan Stern391eca92005-05-10 15:34:16 -04001024{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001025 spin_lock_irq(&dum->lock);
1026 dum->udc_suspended = suspend;
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001027 set_link_state(dum_hcd);
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001028 spin_unlock_irq(&dum->lock);
1029}
Alan Stern391eca92005-05-10 15:34:16 -04001030
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001031static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1032{
1033 struct dummy *dum = platform_get_drvdata(pdev);
1034 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1035
1036 dev_dbg(&pdev->dev, "%s\n", __func__);
1037 dummy_udc_pm(dum, dum_hcd, 1);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001038 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001039 return 0;
1040}
1041
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001042static int dummy_udc_resume(struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04001043{
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001044 struct dummy *dum = platform_get_drvdata(pdev);
1045 struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
Alan Stern391eca92005-05-10 15:34:16 -04001046
Sebastian Andrzej Siewiorfc0b7212011-06-17 19:43:13 +02001047 dev_dbg(&pdev->dev, "%s\n", __func__);
1048 dummy_udc_pm(dum, dum_hcd, 0);
Sebastian Andrzej Siewiord8a14a82011-06-17 10:11:41 +02001049 usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
Alan Stern391eca92005-05-10 15:34:16 -04001050 return 0;
1051}
1052
Russell King3ae5eae2005-11-09 22:32:44 +00001053static struct platform_driver dummy_udc_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04001054 .probe = dummy_udc_probe,
1055 .remove = dummy_udc_remove,
Alan Stern391eca92005-05-10 15:34:16 -04001056 .suspend = dummy_udc_suspend,
1057 .resume = dummy_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001058 .driver = {
1059 .name = (char *) gadget_name,
1060 .owner = THIS_MODULE,
1061 },
Alan Sternd9b76252005-05-03 16:15:43 -04001062};
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064/*-------------------------------------------------------------------------*/
1065
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001066static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc)
1067{
1068 unsigned int index;
1069
1070 index = usb_endpoint_num(desc) << 1;
1071 if (usb_endpoint_dir_in(desc))
1072 index |= 1;
1073 return index;
1074}
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076/* MASTER/HOST SIDE DRIVER
1077 *
1078 * this uses the hcd framework to hook up to host side drivers.
1079 * its root hub will only have one device, otherwise it acts like
1080 * a normal host controller.
1081 *
1082 * when urbs are queued, they're just stuck on a list that we
1083 * scan in a timer callback. that callback connects writes from
1084 * the host with reads from the device, and so on, based on the
1085 * usb 2.0 rules.
1086 */
1087
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001088static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb)
1089{
1090 const struct usb_endpoint_descriptor *desc = &urb->ep->desc;
1091 u32 index;
1092
1093 if (!usb_endpoint_xfer_bulk(desc))
1094 return 0;
1095
1096 index = dummy_get_ep_idx(desc);
1097 return (1 << index) & dum_hcd->stream_en_ep;
1098}
1099
1100/*
1101 * The max stream number is saved as a nibble so for the 30 possible endpoints
1102 * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
1103 * means we use only 1 stream). The maximum according to the spec is 16bit so
1104 * if the 16 stream limit is about to go, the array size should be incremented
1105 * to 30 elements of type u16.
1106 */
1107static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1108 unsigned int pipe)
1109{
1110 int max_streams;
1111
1112 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1113 if (usb_pipeout(pipe))
1114 max_streams >>= 4;
1115 else
1116 max_streams &= 0xf;
1117 max_streams++;
1118 return max_streams;
1119}
1120
1121static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
1122 unsigned int pipe, unsigned int streams)
1123{
1124 int max_streams;
1125
1126 streams--;
1127 max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
1128 if (usb_pipeout(pipe)) {
1129 streams <<= 4;
1130 max_streams &= 0xf;
1131 } else {
1132 max_streams &= 0xf0;
1133 }
1134 max_streams |= streams;
1135 dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams;
1136}
1137
1138static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb)
1139{
1140 unsigned int max_streams;
1141 int enabled;
1142
1143 enabled = dummy_ep_stream_en(dum_hcd, urb);
1144 if (!urb->stream_id) {
1145 if (enabled)
1146 return -EINVAL;
1147 return 0;
1148 }
1149 if (!enabled)
1150 return -EINVAL;
1151
1152 max_streams = get_max_streams_for_pipe(dum_hcd,
1153 usb_pipeendpoint(urb->pipe));
1154 if (urb->stream_id > max_streams) {
1155 dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n",
1156 urb->stream_id);
1157 BUG();
1158 return -EINVAL;
1159 }
1160 return 0;
1161}
1162
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001163static int dummy_urb_enqueue(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 struct usb_hcd *hcd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 struct urb *urb,
Al Viro55016f12005-10-21 03:21:58 -04001166 gfp_t mem_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001168 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 struct urbp *urbp;
1170 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001171 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001173 urbp = kmalloc(sizeof *urbp, mem_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (!urbp)
1175 return -ENOMEM;
1176 urbp->urb = urb;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001177 urbp->miter_started = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001179 dum_hcd = hcd_to_dummy_hcd(hcd);
1180 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001181
1182 rc = dummy_validate_stream(dum_hcd, urb);
1183 if (rc) {
1184 kfree(urbp);
1185 goto done;
1186 }
1187
Alan Sterne9df41c2007-08-08 11:48:02 -04001188 rc = usb_hcd_link_urb_to_ep(hcd, urb);
1189 if (rc) {
1190 kfree(urbp);
1191 goto done;
1192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001194 if (!dum_hcd->udev) {
1195 dum_hcd->udev = urb->dev;
1196 usb_get_dev(dum_hcd->udev);
1197 } else if (unlikely(dum_hcd->udev != urb->dev))
1198 dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001200 list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 urb->hcpriv = urbp;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001202 if (usb_pipetype(urb->pipe) == PIPE_CONTROL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 urb->error_count = 1; /* mark as a new urb */
1204
1205 /* kick the scheduler, it'll do the rest */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001206 if (!timer_pending(&dum_hcd->timer))
1207 mod_timer(&dum_hcd->timer, jiffies + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Alan Sterne9df41c2007-08-08 11:48:02 -04001209 done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001210 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001211 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Alan Sterne9df41c2007-08-08 11:48:02 -04001214static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001216 struct dummy_hcd *dum_hcd;
Alan Stern391eca92005-05-10 15:34:16 -04001217 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -04001218 int rc;
Alan Stern391eca92005-05-10 15:34:16 -04001219
1220 /* giveback happens automatically in timer callback,
1221 * so make sure the callback happens */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001222 dum_hcd = hcd_to_dummy_hcd(hcd);
1223 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001224
1225 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001226 if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1227 !list_empty(&dum_hcd->urbp_list))
1228 mod_timer(&dum_hcd->timer, jiffies);
Alan Sterne9df41c2007-08-08 11:48:02 -04001229
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001230 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -04001231 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001234static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
1235 u32 len)
1236{
1237 void *ubuf, *rbuf;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001238 struct urbp *urbp = urb->hcpriv;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001239 int to_host;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001240 struct sg_mapping_iter *miter = &urbp->miter;
1241 u32 trans = 0;
1242 u32 this_sg;
1243 bool next_sg;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001244
1245 to_host = usb_pipein(urb->pipe);
1246 rbuf = req->req.buf + req->req.actual;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001247
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001248 if (!urb->num_sgs) {
1249 ubuf = urb->transfer_buffer + urb->actual_length;
1250 if (to_host)
1251 memcpy(ubuf, rbuf, len);
1252 else
1253 memcpy(rbuf, ubuf, len);
1254 return len;
1255 }
1256
1257 if (!urbp->miter_started) {
1258 u32 flags = SG_MITER_ATOMIC;
1259
1260 if (to_host)
1261 flags |= SG_MITER_TO_SG;
1262 else
1263 flags |= SG_MITER_FROM_SG;
1264
1265 sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
1266 urbp->miter_started = 1;
1267 }
1268 next_sg = sg_miter_next(miter);
1269 if (next_sg == false) {
1270 WARN_ON_ONCE(1);
1271 return -EINVAL;
1272 }
1273 do {
1274 ubuf = miter->addr;
1275 this_sg = min_t(u32, len, miter->length);
1276 miter->consumed = this_sg;
1277 trans += this_sg;
1278
1279 if (to_host)
1280 memcpy(ubuf, rbuf, this_sg);
1281 else
1282 memcpy(rbuf, ubuf, this_sg);
1283 len -= this_sg;
1284
1285 if (!len)
1286 break;
1287 next_sg = sg_miter_next(miter);
1288 if (next_sg == false) {
1289 WARN_ON_ONCE(1);
1290 return -EINVAL;
1291 }
1292
1293 rbuf += this_sg;
1294 } while (1);
1295
1296 sg_miter_stop(miter);
1297 return trans;
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001298}
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300/* transfer up to a frame's worth; caller must own lock */
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001301static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb,
1302 struct dummy_ep *ep, int limit, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001304 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 struct dummy_request *req;
1306
1307top:
1308 /* if there's no request queued, the device is NAKing; return */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001309 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 unsigned host_len, dev_len, len;
1311 int is_short, to_host;
1312 int rescan = 0;
1313
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001314 if (dummy_ep_stream_en(dum_hcd, urb)) {
1315 if ((urb->stream_id != req->req.stream_id))
1316 continue;
1317 }
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 /* 1..N packets of ep->ep.maxpacket each ... the last one
1320 * may be short (including zero length).
1321 *
1322 * writer can send a zlp explicitly (length 0) or implicitly
1323 * (length mod maxpacket zero, and 'zero' flag); they always
1324 * terminate reads.
1325 */
1326 host_len = urb->transfer_buffer_length - urb->actual_length;
1327 dev_len = req->req.length - req->req.actual;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001328 len = min(host_len, dev_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 /* FIXME update emulated data toggle too */
1331
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001332 to_host = usb_pipein(urb->pipe);
1333 if (unlikely(len == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 is_short = 1;
1335 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 /* not enough bandwidth left? */
1337 if (limit < ep->ep.maxpacket && limit < len)
1338 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001339 len = min_t(unsigned, len, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (len == 0)
1341 break;
1342
1343 /* use an extra pass for the final short packet */
1344 if (len > ep->ep.maxpacket) {
1345 rescan = 1;
1346 len -= (len % ep->ep.maxpacket);
1347 }
1348 is_short = (len % ep->ep.maxpacket) != 0;
1349
Sebastian Andrzej Siewiora04ce202012-01-09 13:14:57 +01001350 len = dummy_perform_transfer(urb, req, len);
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 ep->last_io = jiffies;
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01001353 if (len < 0) {
1354 req->req.status = len;
1355 } else {
1356 limit -= len;
1357 urb->actual_length += len;
1358 req->req.actual += len;
1359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
1361
1362 /* short packets terminate, maybe with overflow/underflow.
1363 * it's only really an error to write too much.
1364 *
1365 * partially filling a buffer optionally blocks queue advances
1366 * (so completion handlers can clean up the queue) but we don't
Alan Sternb0d9efb2007-08-21 15:39:21 -04001367 * need to emulate such data-in-flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 */
1369 if (is_short) {
1370 if (host_len == dev_len) {
1371 req->req.status = 0;
Alan Stern4d2f1102007-08-24 15:40:10 -04001372 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 } else if (to_host) {
1374 req->req.status = 0;
1375 if (dev_len > host_len)
Alan Stern4d2f1102007-08-24 15:40:10 -04001376 *status = -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 else
Alan Stern4d2f1102007-08-24 15:40:10 -04001378 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 } else if (!to_host) {
Alan Stern4d2f1102007-08-24 15:40:10 -04001380 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (host_len > dev_len)
1382 req->req.status = -EOVERFLOW;
1383 else
1384 req->req.status = 0;
1385 }
1386
1387 /* many requests terminate without a short packet */
1388 } else {
1389 if (req->req.length == req->req.actual
1390 && !req->req.zero)
1391 req->req.status = 0;
1392 if (urb->transfer_buffer_length == urb->actual_length
1393 && !(urb->transfer_flags
Alan Stern4d2f1102007-08-24 15:40:10 -04001394 & URB_ZERO_PACKET))
1395 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397
1398 /* device side completion --> continuable */
1399 if (req->req.status != -EINPROGRESS) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001400 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001402 spin_unlock(&dum->lock);
1403 req->req.complete(&ep->ep, &req->req);
1404 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 /* requests might have been unlinked... */
1407 rescan = 1;
1408 }
1409
1410 /* host side completion --> terminate */
Alan Stern4d2f1102007-08-24 15:40:10 -04001411 if (*status != -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 break;
1413
1414 /* rescan to continue with any other queued i/o */
1415 if (rescan)
1416 goto top;
1417 }
1418 return limit;
1419}
1420
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001421static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 int limit = ep->ep.maxpacket;
1424
1425 if (dum->gadget.speed == USB_SPEED_HIGH) {
1426 int tmp;
1427
1428 /* high bandwidth mode */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001429 tmp = usb_endpoint_maxp(ep->desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 tmp = (tmp >> 11) & 0x03;
1431 tmp *= 8 /* applies to entire frame */;
1432 limit += limit * tmp;
1433 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001434 if (dum->gadget.speed == USB_SPEED_SUPER) {
Sebastian Andrzej Siewior59f08e62012-01-12 12:53:14 +01001435 switch (usb_endpoint_type(ep->desc)) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001436 case USB_ENDPOINT_XFER_ISOC:
1437 /* Sec. 4.4.8.2 USB3.0 Spec */
1438 limit = 3 * 16 * 1024 * 8;
1439 break;
1440 case USB_ENDPOINT_XFER_INT:
1441 /* Sec. 4.4.7.2 USB3.0 Spec */
1442 limit = 3 * 1024 * 8;
1443 break;
1444 case USB_ENDPOINT_XFER_BULK:
1445 default:
1446 break;
1447 }
1448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return limit;
1450}
1451
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001452#define is_active(dum_hcd) ((dum_hcd->port_status & \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1454 USB_PORT_STAT_SUSPEND)) \
1455 == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1456
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001457static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 int i;
1460
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001461 if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1462 dum->ss_hcd : dum->hs_hcd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return NULL;
1464 if ((address & ~USB_DIR_IN) == 0)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001465 return &dum->ep[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 for (i = 1; i < DUMMY_ENDPOINTS; i++) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001467 struct dummy_ep *ep = &dum->ep[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 if (!ep->desc)
1470 continue;
1471 if (ep->desc->bEndpointAddress == address)
1472 return ep;
1473 }
1474 return NULL;
1475}
1476
1477#undef is_active
1478
1479#define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1480#define Dev_InRequest (Dev_Request | USB_DIR_IN)
1481#define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1482#define Intf_InRequest (Intf_Request | USB_DIR_IN)
1483#define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1484#define Ep_InRequest (Ep_Request | USB_DIR_IN)
1485
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001486
1487/**
1488 * handle_control_request() - handles all control transfers
1489 * @dum: pointer to dummy (the_controller)
1490 * @urb: the urb request to handle
1491 * @setup: pointer to the setup data for a USB device control
1492 * request
1493 * @status: pointer to request handling status
1494 *
1495 * Return 0 - if the request was handled
1496 * 1 - if the request wasn't handles
1497 * error code on error
1498 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001499static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001500 struct usb_ctrlrequest *setup,
1501 int *status)
1502{
1503 struct dummy_ep *ep2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001504 struct dummy *dum = dum_hcd->dum;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001505 int ret_val = 1;
1506 unsigned w_index;
1507 unsigned w_value;
1508
1509 w_index = le16_to_cpu(setup->wIndex);
1510 w_value = le16_to_cpu(setup->wValue);
1511 switch (setup->bRequest) {
1512 case USB_REQ_SET_ADDRESS:
1513 if (setup->bRequestType != Dev_Request)
1514 break;
1515 dum->address = w_value;
1516 *status = 0;
1517 dev_dbg(udc_dev(dum), "set_address = %d\n",
1518 w_value);
1519 ret_val = 0;
1520 break;
1521 case USB_REQ_SET_FEATURE:
1522 if (setup->bRequestType == Dev_Request) {
1523 ret_val = 0;
1524 switch (w_value) {
1525 case USB_DEVICE_REMOTE_WAKEUP:
1526 break;
1527 case USB_DEVICE_B_HNP_ENABLE:
1528 dum->gadget.b_hnp_enable = 1;
1529 break;
1530 case USB_DEVICE_A_HNP_SUPPORT:
1531 dum->gadget.a_hnp_support = 1;
1532 break;
1533 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1534 dum->gadget.a_alt_hnp_support = 1;
1535 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001536 case USB_DEVICE_U1_ENABLE:
1537 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1538 HCD_USB3)
1539 w_value = USB_DEV_STAT_U1_ENABLED;
1540 else
1541 ret_val = -EOPNOTSUPP;
1542 break;
1543 case USB_DEVICE_U2_ENABLE:
1544 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1545 HCD_USB3)
1546 w_value = USB_DEV_STAT_U2_ENABLED;
1547 else
1548 ret_val = -EOPNOTSUPP;
1549 break;
1550 case USB_DEVICE_LTM_ENABLE:
1551 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1552 HCD_USB3)
1553 w_value = USB_DEV_STAT_LTM_ENABLED;
1554 else
1555 ret_val = -EOPNOTSUPP;
1556 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001557 default:
1558 ret_val = -EOPNOTSUPP;
1559 }
1560 if (ret_val == 0) {
1561 dum->devstatus |= (1 << w_value);
1562 *status = 0;
1563 }
1564 } else if (setup->bRequestType == Ep_Request) {
1565 /* endpoint halt */
1566 ep2 = find_endpoint(dum, w_index);
1567 if (!ep2 || ep2->ep.name == ep0name) {
1568 ret_val = -EOPNOTSUPP;
1569 break;
1570 }
1571 ep2->halted = 1;
1572 ret_val = 0;
1573 *status = 0;
1574 }
1575 break;
1576 case USB_REQ_CLEAR_FEATURE:
1577 if (setup->bRequestType == Dev_Request) {
1578 ret_val = 0;
1579 switch (w_value) {
1580 case USB_DEVICE_REMOTE_WAKEUP:
1581 w_value = USB_DEVICE_REMOTE_WAKEUP;
1582 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001583 case USB_DEVICE_U1_ENABLE:
1584 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1585 HCD_USB3)
1586 w_value = USB_DEV_STAT_U1_ENABLED;
1587 else
1588 ret_val = -EOPNOTSUPP;
1589 break;
1590 case USB_DEVICE_U2_ENABLE:
1591 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1592 HCD_USB3)
1593 w_value = USB_DEV_STAT_U2_ENABLED;
1594 else
1595 ret_val = -EOPNOTSUPP;
1596 break;
1597 case USB_DEVICE_LTM_ENABLE:
1598 if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1599 HCD_USB3)
1600 w_value = USB_DEV_STAT_LTM_ENABLED;
1601 else
1602 ret_val = -EOPNOTSUPP;
1603 break;
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001604 default:
1605 ret_val = -EOPNOTSUPP;
1606 break;
1607 }
1608 if (ret_val == 0) {
1609 dum->devstatus &= ~(1 << w_value);
1610 *status = 0;
1611 }
1612 } else if (setup->bRequestType == Ep_Request) {
1613 /* endpoint halt */
1614 ep2 = find_endpoint(dum, w_index);
1615 if (!ep2) {
1616 ret_val = -EOPNOTSUPP;
1617 break;
1618 }
1619 if (!ep2->wedged)
1620 ep2->halted = 0;
1621 ret_val = 0;
1622 *status = 0;
1623 }
1624 break;
1625 case USB_REQ_GET_STATUS:
1626 if (setup->bRequestType == Dev_InRequest
1627 || setup->bRequestType == Intf_InRequest
1628 || setup->bRequestType == Ep_InRequest) {
1629 char *buf;
1630 /*
1631 * device: remote wakeup, selfpowered
1632 * interface: nothing
1633 * endpoint: halt
1634 */
1635 buf = (char *)urb->transfer_buffer;
1636 if (urb->transfer_buffer_length > 0) {
1637 if (setup->bRequestType == Ep_InRequest) {
1638 ep2 = find_endpoint(dum, w_index);
1639 if (!ep2) {
1640 ret_val = -EOPNOTSUPP;
1641 break;
1642 }
1643 buf[0] = ep2->halted;
1644 } else if (setup->bRequestType ==
1645 Dev_InRequest) {
1646 buf[0] = (u8)dum->devstatus;
1647 } else
1648 buf[0] = 0;
1649 }
1650 if (urb->transfer_buffer_length > 1)
1651 buf[1] = 0;
1652 urb->actual_length = min_t(u32, 2,
1653 urb->transfer_buffer_length);
1654 ret_val = 0;
1655 *status = 0;
1656 }
1657 break;
1658 }
1659 return ret_val;
1660}
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662/* drive both sides of the transfers; looks like irq handlers to
1663 * both drivers except the callbacks aren't in_irq().
1664 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001665static void dummy_timer(unsigned long _dum_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001667 struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
1668 struct dummy *dum = dum_hcd->dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 struct urbp *urbp, *tmp;
1670 unsigned long flags;
1671 int limit, total;
1672 int i;
1673
1674 /* simplistic model for one frame's bandwidth */
1675 switch (dum->gadget.speed) {
1676 case USB_SPEED_LOW:
1677 total = 8/*bytes*/ * 12/*packets*/;
1678 break;
1679 case USB_SPEED_FULL:
1680 total = 64/*bytes*/ * 19/*packets*/;
1681 break;
1682 case USB_SPEED_HIGH:
1683 total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1684 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001685 case USB_SPEED_SUPER:
1686 /* Bus speed is 500000 bytes/ms, so use a little less */
1687 total = 490000;
1688 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001690 dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 return;
1692 }
1693
1694 /* FIXME if HZ != 1000 this will probably misbehave ... */
1695
1696 /* look at each urb queued by the host side driver */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001697 spin_lock_irqsave(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001699 if (!dum_hcd->udev) {
1700 dev_err(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 "timer fired with no URBs pending?\n");
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001702 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return;
1704 }
1705
1706 for (i = 0; i < DUMMY_ENDPOINTS; i++) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001707 if (!ep_name[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001709 dum->ep[i].already_seen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711
1712restart:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001713 list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 struct urb *urb;
1715 struct dummy_request *req;
1716 u8 address;
1717 struct dummy_ep *ep = NULL;
1718 int type;
Alan Stern4d2f1102007-08-24 15:40:10 -04001719 int status = -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 urb = urbp->urb;
Alan Sterneb231052007-08-21 15:40:36 -04001722 if (urb->unlinked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 goto return_urb;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001724 else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
Alan Stern391eca92005-05-10 15:34:16 -04001725 continue;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001726 type = usb_pipetype(urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 /* used up this frame's non-periodic bandwidth?
1729 * FIXME there's infinite bandwidth for control and
1730 * periodic transfers ... unrealistic.
1731 */
1732 if (total <= 0 && type == PIPE_BULK)
1733 continue;
1734
1735 /* find the gadget's ep for this request (if configured) */
1736 address = usb_pipeendpoint (urb->pipe);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001737 if (usb_pipein(urb->pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 address |= USB_DIR_IN;
1739 ep = find_endpoint(dum, address);
1740 if (!ep) {
1741 /* set_configuration() disagreement */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001742 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 "no ep configured for urb %p\n",
1744 urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001745 status = -EPROTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 goto return_urb;
1747 }
1748
1749 if (ep->already_seen)
1750 continue;
1751 ep->already_seen = 1;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001752 if (ep == &dum->ep[0] && urb->error_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 ep->setup_stage = 1; /* a new urb */
1754 urb->error_count = 0;
1755 }
1756 if (ep->halted && !ep->setup_stage) {
1757 /* NOTE: must not be iso! */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001758 dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 ep->ep.name, urb);
Alan Stern4d2f1102007-08-24 15:40:10 -04001760 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 goto return_urb;
1762 }
1763 /* FIXME make sure both ends agree on maxpacket */
1764
1765 /* handle control requests */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001766 if (ep == &dum->ep[0] && ep->setup_stage) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 struct usb_ctrlrequest setup;
1768 int value = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001770 setup = *(struct usb_ctrlrequest *) urb->setup_packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 /* paranoia, in case of stale queued data */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001772 list_for_each_entry(req, &ep->queue, queue) {
1773 list_del_init(&req->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 req->req.status = -EOVERFLOW;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001775 dev_dbg(udc_dev(dum), "stale req = %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 req);
1777
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001778 spin_unlock(&dum->lock);
1779 req->req.complete(&ep->ep, &req->req);
1780 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 ep->already_seen = 0;
1782 goto restart;
1783 }
1784
1785 /* gadget driver never sees set_address or operations
1786 * on standard feature flags. some hardware doesn't
1787 * even expose them.
1788 */
1789 ep->last_io = jiffies;
1790 ep->setup_stage = 0;
1791 ep->halted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001793 value = handle_control_request(dum_hcd, urb, &setup,
Tatyana Brokhman8be8a9d2010-11-01 17:38:05 +02001794 &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 /* gadget driver handles all other requests. block
1797 * until setup() returns; no reentrancy issues etc.
1798 */
1799 if (value > 0) {
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001800 spin_unlock(&dum->lock);
1801 value = dum->driver->setup(&dum->gadget,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 &setup);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001803 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 if (value >= 0) {
1806 /* no delays (max 64KB data stage) */
1807 limit = 64*1024;
1808 goto treat_control_like_bulk;
1809 }
1810 /* error, see below */
1811 }
1812
1813 if (value < 0) {
1814 if (value != -EOPNOTSUPP)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001815 dev_dbg(udc_dev(dum),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 "setup --> %d\n",
1817 value);
Alan Stern4d2f1102007-08-24 15:40:10 -04001818 status = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 urb->actual_length = 0;
1820 }
1821
1822 goto return_urb;
1823 }
1824
1825 /* non-control requests */
1826 limit = total;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001827 switch (usb_pipetype(urb->pipe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 case PIPE_ISOCHRONOUS:
1829 /* FIXME is it urb->interval since the last xfer?
1830 * use urb->iso_frame_desc[i].
1831 * complete whether or not ep has requests queued.
1832 * report random errors, to debug drivers.
1833 */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001834 limit = max(limit, periodic_bytes(dum, ep));
Alan Stern4d2f1102007-08-24 15:40:10 -04001835 status = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 break;
1837
1838 case PIPE_INTERRUPT:
1839 /* FIXME is it urb->interval since the last xfer?
1840 * this almost certainly polls too fast.
1841 */
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001842 limit = max(limit, periodic_bytes(dum, ep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 /* FALLTHROUGH */
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 default:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001846treat_control_like_bulk:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 ep->last_io = jiffies;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01001848 total = transfer(dum_hcd, urb, ep, limit, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 break;
1850 }
1851
1852 /* incomplete transfer? */
Alan Stern4d2f1102007-08-24 15:40:10 -04001853 if (status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 continue;
1855
1856return_urb:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001857 list_del(&urbp->urbp_list);
1858 kfree(urbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if (ep)
1860 ep->already_seen = ep->setup_stage = 0;
1861
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001862 usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001863 spin_unlock(&dum->lock);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001864 usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001865 spin_lock(&dum->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 goto restart;
1868 }
1869
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001870 if (list_empty(&dum_hcd->urbp_list)) {
1871 usb_put_dev(dum_hcd->udev);
1872 dum_hcd->udev = NULL;
1873 } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern391eca92005-05-10 15:34:16 -04001874 /* want a 1 msec delay here */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001875 mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 }
1877
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001878 spin_unlock_irqrestore(&dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880
1881/*-------------------------------------------------------------------------*/
1882
1883#define PORT_C_MASK \
Alan Sternc2db8b52005-04-29 16:30:48 -04001884 ((USB_PORT_STAT_C_CONNECTION \
1885 | USB_PORT_STAT_C_ENABLE \
1886 | USB_PORT_STAT_C_SUSPEND \
1887 | USB_PORT_STAT_C_OVERCURRENT \
1888 | USB_PORT_STAT_C_RESET) << 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001890static int dummy_hub_status(struct usb_hcd *hcd, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001892 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 unsigned long flags;
Alan Stern391eca92005-05-10 15:34:16 -04001894 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001896 dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001898 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -04001899 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001900 goto done;
Alan Sternf1c39fa2005-05-03 16:24:04 -04001901
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001902 if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1903 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1904 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1905 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04001906 }
1907
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001908 if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 *buf = (1 << 1);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001910 dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1911 dum_hcd->port_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 retval = 1;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001913 if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001914 usb_hcd_resume_root_hub(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
Alan Stern391eca92005-05-10 15:34:16 -04001916done:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001917 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return retval;
1919}
1920
1921static inline void
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001922ss_hub_descriptor(struct usb_hub_descriptor *desc)
1923{
1924 memset(desc, 0, sizeof *desc);
1925 desc->bDescriptorType = 0x2a;
1926 desc->bDescLength = 12;
1927 desc->wHubCharacteristics = cpu_to_le16(0x0001);
1928 desc->bNbrPorts = 1;
1929 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1930 desc->u.ss.DeviceRemovable = 0xffff;
1931}
1932
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001933static inline void hub_descriptor(struct usb_hub_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001935 memset(desc, 0, sizeof *desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 desc->bDescriptorType = 0x29;
1937 desc->bDescLength = 9;
Al Virofd05e722008-04-28 07:00:16 +01001938 desc->wHubCharacteristics = cpu_to_le16(0x0001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 desc->bNbrPorts = 1;
John Youndbe79bb2001-09-17 00:00:00 -07001940 desc->u.hs.DeviceRemovable[0] = 0xff;
1941 desc->u.hs.DeviceRemovable[1] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01001944static int dummy_hub_control(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 struct usb_hcd *hcd,
1946 u16 typeReq,
1947 u16 wValue,
1948 u16 wIndex,
1949 char *buf,
1950 u16 wLength
1951) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001952 struct dummy_hcd *dum_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 int retval = 0;
1954 unsigned long flags;
1955
Alan Stern541c7d42010-06-22 16:39:10 -04001956 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern391eca92005-05-10 15:34:16 -04001957 return -ETIMEDOUT;
1958
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001959 dum_hcd = hcd_to_dummy_hcd(hcd);
1960
1961 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 switch (typeReq) {
1963 case ClearHubFeature:
1964 break;
1965 case ClearPortFeature:
1966 switch (wValue) {
1967 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001968 if (hcd->speed == HCD_USB3) {
1969 dev_dbg(dummy_dev(dum_hcd),
1970 "USB_PORT_FEAT_SUSPEND req not "
1971 "supported for USB 3.0 roothub\n");
1972 goto error;
1973 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001974 if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 /* 20msec resume signaling */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001976 dum_hcd->resuming = 1;
1977 dum_hcd->re_timeout = jiffies +
Alan Sternf1c39fa2005-05-03 16:24:04 -04001978 msecs_to_jiffies(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
1980 break;
1981 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001982 if (hcd->speed == HCD_USB3) {
1983 if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1984 dev_dbg(dummy_dev(dum_hcd),
1985 "power-off\n");
1986 } else
1987 if (dum_hcd->port_status &
1988 USB_SS_PORT_STAT_POWER)
1989 dev_dbg(dummy_dev(dum_hcd),
1990 "power-off\n");
Alan Sternf1c39fa2005-05-03 16:24:04 -04001991 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03001993 dum_hcd->port_status &= ~(1 << wValue);
1994 set_link_state(dum_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
1996 break;
1997 case GetHubDescriptor:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03001998 if (hcd->speed == HCD_USB3 &&
1999 (wLength < USB_DT_SS_HUB_SIZE ||
2000 wValue != (USB_DT_SS_HUB << 8))) {
2001 dev_dbg(dummy_dev(dum_hcd),
2002 "Wrong hub descriptor type for "
2003 "USB 3.0 roothub.\n");
2004 goto error;
2005 }
2006 if (hcd->speed == HCD_USB3)
2007 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
2008 else
2009 hub_descriptor((struct usb_hub_descriptor *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 break;
2011 case GetHubStatus:
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002012 *(__le32 *) buf = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 break;
2014 case GetPortStatus:
2015 if (wIndex != 1)
2016 retval = -EPIPE;
2017
2018 /* whoever resets or resumes must GetPortStatus to
2019 * complete it!!
2020 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002021 if (dum_hcd->resuming &&
2022 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2023 dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
2024 dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002026 if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
2027 time_after_eq(jiffies, dum_hcd->re_timeout)) {
2028 dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
2029 dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
2030 if (dum_hcd->dum->pullup) {
2031 dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002032
2033 if (hcd->speed < HCD_USB3) {
2034 switch (dum_hcd->dum->gadget.speed) {
2035 case USB_SPEED_HIGH:
2036 dum_hcd->port_status |=
2037 USB_PORT_STAT_HIGH_SPEED;
2038 break;
2039 case USB_SPEED_LOW:
2040 dum_hcd->dum->gadget.ep0->
2041 maxpacket = 8;
2042 dum_hcd->port_status |=
2043 USB_PORT_STAT_LOW_SPEED;
2044 break;
2045 default:
2046 dum_hcd->dum->gadget.speed =
2047 USB_SPEED_FULL;
2048 break;
2049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
2051 }
2052 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002053 set_link_state(dum_hcd);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002054 ((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status);
2055 ((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 break;
2057 case SetHubFeature:
2058 retval = -EPIPE;
2059 break;
2060 case SetPortFeature:
2061 switch (wValue) {
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002062 case USB_PORT_FEAT_LINK_STATE:
2063 if (hcd->speed != HCD_USB3) {
2064 dev_dbg(dummy_dev(dum_hcd),
2065 "USB_PORT_FEAT_LINK_STATE req not "
2066 "supported for USB 2.0 roothub\n");
2067 goto error;
2068 }
2069 /*
2070 * Since this is dummy we don't have an actual link so
2071 * there is nothing to do for the SET_LINK_STATE cmd
2072 */
2073 break;
2074 case USB_PORT_FEAT_U1_TIMEOUT:
2075 case USB_PORT_FEAT_U2_TIMEOUT:
2076 /* TODO: add suspend/resume support! */
2077 if (hcd->speed != HCD_USB3) {
2078 dev_dbg(dummy_dev(dum_hcd),
2079 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
2080 "supported for USB 2.0 roothub\n");
2081 goto error;
2082 }
2083 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 case USB_PORT_FEAT_SUSPEND:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002085 /* Applicable only for USB2.0 hub */
2086 if (hcd->speed == HCD_USB3) {
2087 dev_dbg(dummy_dev(dum_hcd),
2088 "USB_PORT_FEAT_SUSPEND req not "
2089 "supported for USB 3.0 roothub\n");
2090 goto error;
2091 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002092 if (dum_hcd->active) {
2093 dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
Alan Sternf1c39fa2005-05-03 16:24:04 -04002094
2095 /* HNP would happen here; for now we
2096 * assume b_bus_req is always true.
2097 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002098 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002099 if (((1 << USB_DEVICE_B_HNP_ENABLE)
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002100 & dum_hcd->dum->devstatus) != 0)
2101 dev_dbg(dummy_dev(dum_hcd),
Alan Stern5742b0c2005-05-02 11:25:17 -04002102 "no HNP yet!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 }
2104 break;
Alan Sternf1c39fa2005-05-03 16:24:04 -04002105 case USB_PORT_FEAT_POWER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002106 if (hcd->speed == HCD_USB3)
2107 dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
2108 else
2109 dum_hcd->port_status |= USB_PORT_STAT_POWER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002110 set_link_state(dum_hcd);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002111 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002112 case USB_PORT_FEAT_BH_PORT_RESET:
2113 /* Applicable only for USB3.0 hub */
2114 if (hcd->speed != HCD_USB3) {
2115 dev_dbg(dummy_dev(dum_hcd),
2116 "USB_PORT_FEAT_BH_PORT_RESET req not "
2117 "supported for USB 2.0 roothub\n");
2118 goto error;
2119 }
2120 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 case USB_PORT_FEAT_RESET:
Alan Sternf1c39fa2005-05-03 16:24:04 -04002122 /* if it's already enabled, disable */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002123 if (hcd->speed == HCD_USB3) {
2124 dum_hcd->port_status = 0;
2125 dum_hcd->port_status =
2126 (USB_SS_PORT_STAT_POWER |
2127 USB_PORT_STAT_CONNECTION |
2128 USB_PORT_STAT_RESET);
2129 } else
2130 dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
Alan Sternf1c39fa2005-05-03 16:24:04 -04002131 | USB_PORT_STAT_LOW_SPEED
2132 | USB_PORT_STAT_HIGH_SPEED);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002133 /*
2134 * We want to reset device status. All but the
2135 * Self powered feature
2136 */
2137 dum_hcd->dum->devstatus &=
2138 (1 << USB_DEVICE_SELF_POWERED);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002139 /*
2140 * FIXME USB3.0: what is the correct reset signaling
2141 * interval? Is it still 50msec as for HS?
2142 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002143 dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
Alan Sternf1c39fa2005-05-03 16:24:04 -04002144 /* FALLS THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 default:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002146 if (hcd->speed == HCD_USB3) {
2147 if ((dum_hcd->port_status &
2148 USB_SS_PORT_STAT_POWER) != 0) {
2149 dum_hcd->port_status |= (1 << wValue);
2150 set_link_state(dum_hcd);
2151 }
2152 } else
2153 if ((dum_hcd->port_status &
2154 USB_PORT_STAT_POWER) != 0) {
2155 dum_hcd->port_status |= (1 << wValue);
2156 set_link_state(dum_hcd);
2157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 }
2159 break;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002160 case GetPortErrorCount:
2161 if (hcd->speed != HCD_USB3) {
2162 dev_dbg(dummy_dev(dum_hcd),
2163 "GetPortErrorCount req not "
2164 "supported for USB 2.0 roothub\n");
2165 goto error;
2166 }
2167 /* We'll always return 0 since this is a dummy hub */
2168 *(__le32 *) buf = cpu_to_le32(0);
2169 break;
2170 case SetHubDepth:
2171 if (hcd->speed != HCD_USB3) {
2172 dev_dbg(dummy_dev(dum_hcd),
2173 "SetHubDepth req not supported for "
2174 "USB 2.0 roothub\n");
2175 goto error;
2176 }
2177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 default:
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002179 dev_dbg(dummy_dev(dum_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 "hub control req%04x v%04x i%04x l%d\n",
2181 typeReq, wValue, wIndex, wLength);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002182error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 /* "protocol stall" on error */
2184 retval = -EPIPE;
2185 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002186 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Alan Stern685eb932005-05-03 16:27:26 -04002187
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002188 if ((dum_hcd->port_status & PORT_C_MASK) != 0)
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002189 usb_hcd_poll_rh_status(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return retval;
2191}
2192
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002193static int dummy_bus_suspend(struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002194{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002195 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002196
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002197 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern3cf0a222005-11-29 12:08:15 -05002198
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002199 spin_lock_irq(&dum_hcd->dum->lock);
2200 dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2201 set_link_state(dum_hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002202 hcd->state = HC_STATE_SUSPENDED;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002203 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern391eca92005-05-10 15:34:16 -04002204 return 0;
2205}
2206
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002207static int dummy_bus_resume(struct usb_hcd *hcd)
Alan Stern391eca92005-05-10 15:34:16 -04002208{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002209 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Alan Stern3cf0a222005-11-29 12:08:15 -05002210 int rc = 0;
2211
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002212 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002213
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002214 spin_lock_irq(&dum_hcd->dum->lock);
Alan Stern541c7d42010-06-22 16:39:10 -04002215 if (!HCD_HW_ACCESSIBLE(hcd)) {
Alan Sterncfa59da2007-06-21 16:25:35 -04002216 rc = -ESHUTDOWN;
Alan Stern3cf0a222005-11-29 12:08:15 -05002217 } else {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002218 dum_hcd->rh_state = DUMMY_RH_RUNNING;
2219 set_link_state(dum_hcd);
2220 if (!list_empty(&dum_hcd->urbp_list))
2221 mod_timer(&dum_hcd->timer, jiffies);
Alan Stern3cf0a222005-11-29 12:08:15 -05002222 hcd->state = HC_STATE_RUNNING;
2223 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002224 spin_unlock_irq(&dum_hcd->dum->lock);
Alan Stern3cf0a222005-11-29 12:08:15 -05002225 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002226}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
2228/*-------------------------------------------------------------------------*/
2229
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002230static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002232 int ep = usb_pipeendpoint(urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002234 return snprintf(buf, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 "urb/%p %s ep%d%s%s len %d/%d\n",
2236 urb,
2237 ({ char *s;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002238 switch (urb->dev->speed) {
2239 case USB_SPEED_LOW:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002240 s = "ls";
2241 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002242 case USB_SPEED_FULL:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002243 s = "fs";
2244 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002245 case USB_SPEED_HIGH:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002246 s = "hs";
2247 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002248 case USB_SPEED_SUPER:
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002249 s = "ss";
2250 break;
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002251 default:
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002252 s = "?";
2253 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 }; s; }),
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002255 ep, ep ? (usb_pipein(urb->pipe) ? "in" : "out") : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 ({ char *s; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002257 switch (usb_pipetype(urb->pipe)) { \
2258 case PIPE_CONTROL: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002259 s = ""; \
2260 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002261 case PIPE_BULK: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002262 s = "-bulk"; \
2263 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002264 case PIPE_INTERRUPT: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002265 s = "-int"; \
2266 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002267 default: \
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +03002268 s = "-iso"; \
2269 break; \
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002270 }; s; }),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 urb->actual_length, urb->transfer_buffer_length);
2272}
2273
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002274static ssize_t show_urbs(struct device *dev, struct device_attribute *attr,
2275 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002277 struct usb_hcd *hcd = dev_get_drvdata(dev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002278 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 struct urbp *urbp;
2280 size_t size = 0;
2281 unsigned long flags;
2282
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002283 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2284 list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 size_t temp;
2286
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002287 temp = show_urb(buf, PAGE_SIZE - size, urbp->urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 buf += temp;
2289 size += temp;
2290 }
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002291 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293 return size;
2294}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002295static DEVICE_ATTR(urbs, S_IRUGO, show_urbs, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002297static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2298{
2299 init_timer(&dum_hcd->timer);
2300 dum_hcd->timer.function = dummy_timer;
2301 dum_hcd->timer.data = (unsigned long)dum_hcd;
2302 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002303 dum_hcd->stream_en_ep = 0;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002304 INIT_LIST_HEAD(&dum_hcd->urbp_list);
2305 dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2306 dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2307 dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2308#ifdef CONFIG_USB_OTG
2309 dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2310#endif
2311 return 0;
2312
2313 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2314 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2315}
2316
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002317static int dummy_start(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002319 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 /*
2322 * MASTER side init ... we emulate a root hub that'll only ever
2323 * talk to one device (the slave side). Also appears in sysfs,
2324 * just like more familiar pci-based HCDs.
2325 */
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002326 if (!usb_hcd_is_primary_hcd(hcd))
2327 return dummy_start_ss(dum_hcd);
2328
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002329 spin_lock_init(&dum_hcd->dum->lock);
2330 init_timer(&dum_hcd->timer);
2331 dum_hcd->timer.function = dummy_timer;
2332 dum_hcd->timer.data = (unsigned long)dum_hcd;
2333 dum_hcd->rh_state = DUMMY_RH_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002335 INIT_LIST_HEAD(&dum_hcd->urbp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Alan Sterncaf29f62007-12-06 11:10:39 -05002337 hcd->power_budget = POWER_BUDGET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 hcd->state = HC_STATE_RUNNING;
Alan Stern685eb932005-05-03 16:27:26 -04002339 hcd->uses_new_polling = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Alan Stern5742b0c2005-05-02 11:25:17 -04002341#ifdef CONFIG_USB_OTG
2342 hcd->self.otg_port = 1;
2343#endif
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002346 return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347}
2348
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002349static void dummy_stop(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
2351 struct dummy *dum;
2352
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002353 dum = hcd_to_dummy_hcd(hcd)->dum;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002354 device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2355 usb_gadget_unregister_driver(dum->driver);
2356 dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357}
2358
2359/*-------------------------------------------------------------------------*/
2360
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002361static int dummy_h_get_frame(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002363 return dummy_g_get_frame(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002366static int dummy_setup(struct usb_hcd *hcd)
2367{
Sebastian Andrzej Siewior14fce332012-01-09 19:30:50 +01002368 hcd->self.sg_tablesize = ~0;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002369 if (usb_hcd_is_primary_hcd(hcd)) {
2370 the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
2371 the_controller.hs_hcd->dum = &the_controller;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002372 /*
2373 * Mark the first roothub as being USB 2.0.
2374 * The USB 3.0 roothub will be registered later by
2375 * dummy_hcd_probe()
2376 */
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002377 hcd->speed = HCD_USB2;
2378 hcd->self.root_hub->speed = USB_SPEED_HIGH;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002379 } else {
2380 the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
2381 the_controller.ss_hcd->dum = &the_controller;
2382 hcd->speed = HCD_USB3;
2383 hcd->self.root_hub->speed = USB_SPEED_SUPER;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002384 }
2385 return 0;
2386}
2387
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002388/* Change a group of bulk endpoints to support multiple stream IDs */
Sebastian Andrzej Siewiord81f3e42012-01-09 13:15:00 +01002389static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002390 struct usb_host_endpoint **eps, unsigned int num_eps,
2391 unsigned int num_streams, gfp_t mem_flags)
2392{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002393 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2394 unsigned long flags;
2395 int max_stream;
2396 int ret_streams = num_streams;
2397 unsigned int index;
2398 unsigned int i;
2399
2400 if (!num_eps)
2401 return -EINVAL;
2402
2403 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2404 for (i = 0; i < num_eps; i++) {
2405 index = dummy_get_ep_idx(&eps[i]->desc);
2406 if ((1 << index) & dum_hcd->stream_en_ep) {
2407 ret_streams = -EINVAL;
2408 goto out;
2409 }
2410 max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp);
2411 if (!max_stream) {
2412 ret_streams = -EINVAL;
2413 goto out;
2414 }
2415 if (max_stream < ret_streams) {
2416 dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u "
2417 "stream IDs.\n",
2418 eps[i]->desc.bEndpointAddress,
2419 max_stream);
2420 ret_streams = max_stream;
2421 }
2422 }
2423
2424 for (i = 0; i < num_eps; i++) {
2425 index = dummy_get_ep_idx(&eps[i]->desc);
2426 dum_hcd->stream_en_ep |= 1 << index;
2427 set_max_streams_for_pipe(dum_hcd,
2428 usb_endpoint_num(&eps[i]->desc), ret_streams);
2429 }
2430out:
2431 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2432 return ret_streams;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002433}
2434
2435/* Reverts a group of bulk endpoints back to not using stream IDs. */
Sebastian Andrzej Siewiord81f3e42012-01-09 13:15:00 +01002436static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002437 struct usb_host_endpoint **eps, unsigned int num_eps,
2438 gfp_t mem_flags)
2439{
Sebastian Andrzej Siewiora54c9792012-01-13 21:51:47 +01002440 struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2441 unsigned long flags;
2442 int ret;
2443 unsigned int index;
2444 unsigned int i;
2445
2446 spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2447 for (i = 0; i < num_eps; i++) {
2448 index = dummy_get_ep_idx(&eps[i]->desc);
2449 if (!((1 << index) & dum_hcd->stream_en_ep)) {
2450 ret = -EINVAL;
2451 goto out;
2452 }
2453 }
2454
2455 for (i = 0; i < num_eps; i++) {
2456 index = dummy_get_ep_idx(&eps[i]->desc);
2457 dum_hcd->stream_en_ep &= ~(1 << index);
2458 set_max_streams_for_pipe(dum_hcd,
2459 usb_endpoint_num(&eps[i]->desc), 0);
2460 }
2461 ret = 0;
2462out:
2463 spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2464 return ret;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002465}
2466
2467static struct hc_driver dummy_hcd = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 .description = (char *) driver_name,
2469 .product_desc = "Dummy host controller",
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002470 .hcd_priv_size = sizeof(struct dummy_hcd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002472 .flags = HCD_USB3 | HCD_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002474 .reset = dummy_setup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 .start = dummy_start,
2476 .stop = dummy_stop,
2477
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002478 .urb_enqueue = dummy_urb_enqueue,
2479 .urb_dequeue = dummy_urb_dequeue,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002481 .get_frame_number = dummy_h_get_frame,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002483 .hub_status_data = dummy_hub_status,
2484 .hub_control = dummy_hub_control,
Alan Stern0c0382e2005-10-13 17:08:02 -04002485 .bus_suspend = dummy_bus_suspend,
2486 .bus_resume = dummy_bus_resume,
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002487
2488 .alloc_streams = dummy_alloc_streams,
2489 .free_streams = dummy_free_streams,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490};
2491
Alan Stern8364d6b2005-11-14 12:16:30 -05002492static int dummy_hcd_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002494 struct usb_hcd *hs_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002495 struct usb_hcd *ss_hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 int retval;
2497
Alan Stern8364d6b2005-11-14 12:16:30 -05002498 dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002500 if (!mod_data.is_super_speed)
2501 dummy_hcd.flags = HCD_USB2;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002502 hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2503 if (!hs_hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 return -ENOMEM;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002505 hs_hcd->has_tt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002507 retval = usb_add_hcd(hs_hcd, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 if (retval != 0) {
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002509 usb_put_hcd(hs_hcd);
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002510 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 }
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002512
2513 if (mod_data.is_super_speed) {
2514 ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2515 dev_name(&pdev->dev), hs_hcd);
2516 if (!ss_hcd) {
2517 retval = -ENOMEM;
2518 goto dealloc_usb2_hcd;
2519 }
2520
2521 retval = usb_add_hcd(ss_hcd, 0, 0);
2522 if (retval)
2523 goto put_usb3_hcd;
2524 }
2525 return 0;
2526
2527put_usb3_hcd:
2528 usb_put_hcd(ss_hcd);
2529dealloc_usb2_hcd:
2530 usb_put_hcd(hs_hcd);
2531 the_controller.hs_hcd = the_controller.ss_hcd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 return retval;
2533}
2534
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002535static int dummy_hcd_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536{
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002537 struct dummy *dum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002539 dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002540
2541 if (dum->ss_hcd) {
2542 usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2543 usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2544 }
2545
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002546 usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2547 usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002548
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002549 the_controller.hs_hcd = NULL;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002550 the_controller.ss_hcd = NULL;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002551
Alan Sternd9b76252005-05-03 16:15:43 -04002552 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553}
2554
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002555static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state)
Alan Stern391eca92005-05-10 15:34:16 -04002556{
2557 struct usb_hcd *hcd;
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002558 struct dummy_hcd *dum_hcd;
Alan Stern3cf0a222005-11-29 12:08:15 -05002559 int rc = 0;
Alan Stern391eca92005-05-10 15:34:16 -04002560
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002561 dev_dbg(&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002562
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002563 hcd = platform_get_drvdata(pdev);
Tatyana Brokhmancdfcbd22011-06-29 16:41:51 +03002564 dum_hcd = hcd_to_dummy_hcd(hcd);
2565 if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
Alan Stern3cf0a222005-11-29 12:08:15 -05002566 dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2567 rc = -EBUSY;
2568 } else
2569 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2570 return rc;
Alan Stern391eca92005-05-10 15:34:16 -04002571}
2572
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002573static int dummy_hcd_resume(struct platform_device *pdev)
Alan Stern391eca92005-05-10 15:34:16 -04002574{
2575 struct usb_hcd *hcd;
2576
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002577 dev_dbg(&pdev->dev, "%s\n", __func__);
Alan Stern391eca92005-05-10 15:34:16 -04002578
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002579 hcd = platform_get_drvdata(pdev);
Alan Stern3cf0a222005-11-29 12:08:15 -05002580 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002581 usb_hcd_poll_rh_status(hcd);
Alan Stern391eca92005-05-10 15:34:16 -04002582 return 0;
2583}
2584
Russell King3ae5eae2005-11-09 22:32:44 +00002585static struct platform_driver dummy_hcd_driver = {
Alan Sternd9b76252005-05-03 16:15:43 -04002586 .probe = dummy_hcd_probe,
2587 .remove = dummy_hcd_remove,
Alan Stern391eca92005-05-10 15:34:16 -04002588 .suspend = dummy_hcd_suspend,
2589 .resume = dummy_hcd_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00002590 .driver = {
2591 .name = (char *) driver_name,
2592 .owner = THIS_MODULE,
2593 },
Alan Sternd9b76252005-05-03 16:15:43 -04002594};
2595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596/*-------------------------------------------------------------------------*/
2597
Alan Sterna89a2cd2008-04-07 15:03:25 -04002598static struct platform_device *the_udc_pdev;
2599static struct platform_device *the_hcd_pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002601static int __init init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002603 int retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002605 if (usb_disabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 return -ENODEV;
Alan Sternd9b76252005-05-03 16:15:43 -04002607
Tatyana Brokhman7eca4c52011-06-29 16:41:53 +03002608 if (!mod_data.is_high_speed && mod_data.is_super_speed)
2609 return -EINVAL;
2610
Alan Sterna89a2cd2008-04-07 15:03:25 -04002611 the_hcd_pdev = platform_device_alloc(driver_name, -1);
2612 if (!the_hcd_pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 return retval;
Alan Sterna89a2cd2008-04-07 15:03:25 -04002614 the_udc_pdev = platform_device_alloc(gadget_name, -1);
2615 if (!the_udc_pdev)
2616 goto err_alloc_udc;
Alan Sternd9b76252005-05-03 16:15:43 -04002617
Alan Sterna89a2cd2008-04-07 15:03:25 -04002618 retval = platform_driver_register(&dummy_hcd_driver);
2619 if (retval < 0)
2620 goto err_register_hcd_driver;
2621 retval = platform_driver_register(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002622 if (retval < 0)
2623 goto err_register_udc_driver;
2624
Alan Sterna89a2cd2008-04-07 15:03:25 -04002625 retval = platform_device_add(the_hcd_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002626 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002627 goto err_add_hcd;
Tatyana Brokhman1cd8fd22011-06-29 16:41:52 +03002628 if (!the_controller.hs_hcd ||
2629 (!the_controller.ss_hcd && mod_data.is_super_speed)) {
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002630 /*
2631 * The hcd was added successfully but its probe function failed
2632 * for some reason.
2633 */
2634 retval = -EINVAL;
2635 goto err_add_udc;
2636 }
Alan Sterna89a2cd2008-04-07 15:03:25 -04002637 retval = platform_device_add(the_udc_pdev);
Alan Sternd9b76252005-05-03 16:15:43 -04002638 if (retval < 0)
Alan Sterna89a2cd2008-04-07 15:03:25 -04002639 goto err_add_udc;
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002640 if (!platform_get_drvdata(the_udc_pdev)) {
2641 /*
2642 * The udc was added successfully but its probe function failed
2643 * for some reason.
2644 */
2645 retval = -EINVAL;
2646 goto err_probe_udc;
2647 }
Alan Sternd9b76252005-05-03 16:15:43 -04002648 return retval;
2649
Sebastian Andrzej Siewior865835f2011-04-15 20:37:06 +02002650err_probe_udc:
2651 platform_device_del(the_udc_pdev);
Alan Sterna89a2cd2008-04-07 15:03:25 -04002652err_add_udc:
2653 platform_device_del(the_hcd_pdev);
2654err_add_hcd:
2655 platform_driver_unregister(&dummy_udc_driver);
Alan Sternd9b76252005-05-03 16:15:43 -04002656err_register_udc_driver:
Alan Sterna89a2cd2008-04-07 15:03:25 -04002657 platform_driver_unregister(&dummy_hcd_driver);
2658err_register_hcd_driver:
2659 platform_device_put(the_udc_pdev);
2660err_alloc_udc:
2661 platform_device_put(the_hcd_pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 return retval;
2663}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002664module_init(init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002666static void __exit cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
Alan Sterna89a2cd2008-04-07 15:03:25 -04002668 platform_device_unregister(the_udc_pdev);
2669 platform_device_unregister(the_hcd_pdev);
2670 platform_driver_unregister(&dummy_udc_driver);
2671 platform_driver_unregister(&dummy_hcd_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672}
Sebastian Andrzej Siewior18f2cba2012-01-12 12:53:15 +01002673module_exit(cleanup);