Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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. | |
1da177e4 LT |
13 | */ |
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 | ||
1da177e4 LT |
28 | #include <linux/module.h> |
29 | #include <linux/kernel.h> | |
30 | #include <linux/delay.h> | |
31 | #include <linux/ioport.h> | |
1da177e4 | 32 | #include <linux/slab.h> |
1da177e4 LT |
33 | #include <linux/errno.h> |
34 | #include <linux/init.h> | |
35 | #include <linux/timer.h> | |
36 | #include <linux/list.h> | |
37 | #include <linux/interrupt.h> | |
d052d1be | 38 | #include <linux/platform_device.h> |
1da177e4 | 39 | #include <linux/usb.h> |
9454a57a | 40 | #include <linux/usb/gadget.h> |
27729aad | 41 | #include <linux/usb/hcd.h> |
14fce33a | 42 | #include <linux/scatterlist.h> |
1da177e4 LT |
43 | |
44 | #include <asm/byteorder.h> | |
18f2cbaa | 45 | #include <linux/io.h> |
1da177e4 | 46 | #include <asm/irq.h> |
1da177e4 LT |
47 | #include <asm/unaligned.h> |
48 | ||
1da177e4 | 49 | #define DRIVER_DESC "USB Host+Gadget Emulator" |
391eca9d | 50 | #define DRIVER_VERSION "02 May 2005" |
1da177e4 | 51 | |
caf29f62 AS |
52 | #define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */ |
53 | ||
18f2cbaa SAS |
54 | static const char driver_name[] = "dummy_hcd"; |
55 | static const char driver_desc[] = "USB Host+Gadget Emulator"; | |
1da177e4 | 56 | |
18f2cbaa | 57 | static const char gadget_name[] = "dummy_udc"; |
1da177e4 | 58 | |
18f2cbaa SAS |
59 | MODULE_DESCRIPTION(DRIVER_DESC); |
60 | MODULE_AUTHOR("David Brownell"); | |
61 | MODULE_LICENSE("GPL"); | |
1da177e4 | 62 | |
1cd8fd28 TB |
63 | struct dummy_hcd_module_parameters { |
64 | bool is_super_speed; | |
7eca4c5a | 65 | bool is_high_speed; |
c7a1db45 | 66 | unsigned int num; |
1cd8fd28 TB |
67 | }; |
68 | ||
69 | static struct dummy_hcd_module_parameters mod_data = { | |
7eca4c5a TB |
70 | .is_super_speed = false, |
71 | .is_high_speed = true, | |
c7a1db45 | 72 | .num = 1, |
1cd8fd28 TB |
73 | }; |
74 | module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO); | |
75 | MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection"); | |
7eca4c5a TB |
76 | module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO); |
77 | MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection"); | |
c7a1db45 SAS |
78 | module_param_named(num, mod_data.num, uint, S_IRUGO); |
79 | MODULE_PARM_DESC(num, "number of emulated controllers"); | |
1da177e4 LT |
80 | /*-------------------------------------------------------------------------*/ |
81 | ||
82 | /* gadget side driver data structres */ | |
83 | struct dummy_ep { | |
84 | struct list_head queue; | |
85 | unsigned long last_io; /* jiffies timestamp */ | |
86 | struct usb_gadget *gadget; | |
87 | const struct usb_endpoint_descriptor *desc; | |
88 | struct usb_ep ep; | |
18f2cbaa SAS |
89 | unsigned halted:1; |
90 | unsigned wedged:1; | |
91 | unsigned already_seen:1; | |
92 | unsigned setup_stage:1; | |
a54c979f | 93 | unsigned stream_en:1; |
1da177e4 LT |
94 | }; |
95 | ||
96 | struct dummy_request { | |
97 | struct list_head queue; /* ep's requests */ | |
98 | struct usb_request req; | |
99 | }; | |
100 | ||
18f2cbaa | 101 | static inline struct dummy_ep *usb_ep_to_dummy_ep(struct usb_ep *_ep) |
1da177e4 | 102 | { |
18f2cbaa | 103 | return container_of(_ep, struct dummy_ep, ep); |
1da177e4 LT |
104 | } |
105 | ||
106 | static inline struct dummy_request *usb_request_to_dummy_request | |
107 | (struct usb_request *_req) | |
108 | { | |
18f2cbaa | 109 | return container_of(_req, struct dummy_request, req); |
1da177e4 LT |
110 | } |
111 | ||
112 | /*-------------------------------------------------------------------------*/ | |
113 | ||
114 | /* | |
115 | * Every device has ep0 for control requests, plus up to 30 more endpoints, | |
116 | * in one of two types: | |
117 | * | |
118 | * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint | |
119 | * number can be changed. Names like "ep-a" are used for this type. | |
120 | * | |
121 | * - Fixed Function: in other cases. some characteristics may be mutable; | |
122 | * that'd be hardware-specific. Names like "ep12out-bulk" are used. | |
123 | * | |
124 | * Gadget drivers are responsible for not setting up conflicting endpoint | |
125 | * configurations, illegal or unsupported packet lengths, and so on. | |
126 | */ | |
127 | ||
18f2cbaa | 128 | static const char ep0name[] = "ep0"; |
1da177e4 | 129 | |
18f2cbaa | 130 | static const char *const ep_name[] = { |
1da177e4 LT |
131 | ep0name, /* everyone has ep0 */ |
132 | ||
1d16638e | 133 | /* act like a pxa250: fifteen fixed function endpoints */ |
1da177e4 LT |
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", | |
1d16638e SAS |
141 | |
142 | /* and now some generic EPs so we have enough in multi config */ | |
143 | "ep3out", "ep4in", "ep5out", "ep6out", "ep7in", "ep8out", "ep9in", | |
144 | "ep10out", "ep11out", "ep12in", "ep13out", "ep14in", "ep15out", | |
1da177e4 | 145 | }; |
52950ed4 | 146 | #define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name) |
1da177e4 | 147 | |
d9b76251 AS |
148 | /*-------------------------------------------------------------------------*/ |
149 | ||
1da177e4 LT |
150 | #define FIFO_SIZE 64 |
151 | ||
152 | struct urbp { | |
153 | struct urb *urb; | |
154 | struct list_head urbp_list; | |
14fce33a SAS |
155 | struct sg_mapping_iter miter; |
156 | u32 miter_started; | |
1da177e4 LT |
157 | }; |
158 | ||
391eca9d AS |
159 | |
160 | enum dummy_rh_state { | |
161 | DUMMY_RH_RESET, | |
162 | DUMMY_RH_SUSPENDED, | |
163 | DUMMY_RH_RUNNING | |
164 | }; | |
165 | ||
cdfcbd2c TB |
166 | struct dummy_hcd { |
167 | struct dummy *dum; | |
168 | enum dummy_rh_state rh_state; | |
169 | struct timer_list timer; | |
170 | u32 port_status; | |
171 | u32 old_status; | |
172 | unsigned long re_timeout; | |
173 | ||
174 | struct usb_device *udev; | |
175 | struct list_head urbp_list; | |
a54c979f SAS |
176 | u32 stream_en_ep; |
177 | u8 num_stream[30 / 2]; | |
cdfcbd2c TB |
178 | |
179 | unsigned active:1; | |
180 | unsigned old_active:1; | |
181 | unsigned resuming:1; | |
182 | }; | |
183 | ||
1da177e4 LT |
184 | struct dummy { |
185 | spinlock_t lock; | |
186 | ||
187 | /* | |
188 | * SLAVE/GADGET side support | |
189 | */ | |
18f2cbaa | 190 | struct dummy_ep ep[DUMMY_ENDPOINTS]; |
1da177e4 LT |
191 | int address; |
192 | struct usb_gadget gadget; | |
193 | struct usb_gadget_driver *driver; | |
194 | struct dummy_request fifo_req; | |
18f2cbaa | 195 | u8 fifo_buf[FIFO_SIZE]; |
1da177e4 | 196 | u16 devstatus; |
391eca9d | 197 | unsigned udc_suspended:1; |
f1c39fad | 198 | unsigned pullup:1; |
1da177e4 LT |
199 | |
200 | /* | |
201 | * MASTER/HOST side support | |
202 | */ | |
cdfcbd2c | 203 | struct dummy_hcd *hs_hcd; |
1cd8fd28 | 204 | struct dummy_hcd *ss_hcd; |
1da177e4 LT |
205 | }; |
206 | ||
cdfcbd2c | 207 | static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd) |
1da177e4 | 208 | { |
cdfcbd2c | 209 | return (struct dummy_hcd *) (hcd->hcd_priv); |
1da177e4 LT |
210 | } |
211 | ||
cdfcbd2c | 212 | static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum) |
1da177e4 LT |
213 | { |
214 | return container_of((void *) dum, struct usb_hcd, hcd_priv); | |
215 | } | |
216 | ||
cdfcbd2c | 217 | static inline struct device *dummy_dev(struct dummy_hcd *dum) |
1da177e4 | 218 | { |
cdfcbd2c | 219 | return dummy_hcd_to_hcd(dum)->self.controller; |
1da177e4 LT |
220 | } |
221 | ||
18f2cbaa | 222 | static inline struct device *udc_dev(struct dummy *dum) |
d9b76251 AS |
223 | { |
224 | return dum->gadget.dev.parent; | |
225 | } | |
226 | ||
18f2cbaa | 227 | static inline struct dummy *ep_to_dummy(struct dummy_ep *ep) |
1da177e4 | 228 | { |
18f2cbaa | 229 | return container_of(ep->gadget, struct dummy, gadget); |
1da177e4 LT |
230 | } |
231 | ||
cdfcbd2c | 232 | static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget) |
1da177e4 | 233 | { |
cdfcbd2c | 234 | struct dummy *dum = container_of(gadget, struct dummy, gadget); |
1cd8fd28 TB |
235 | if (dum->gadget.speed == USB_SPEED_SUPER) |
236 | return dum->ss_hcd; | |
237 | else | |
238 | return dum->hs_hcd; | |
1da177e4 LT |
239 | } |
240 | ||
18f2cbaa | 241 | static inline struct dummy *gadget_dev_to_dummy(struct device *dev) |
1da177e4 | 242 | { |
18f2cbaa | 243 | return container_of(dev, struct dummy, gadget.dev); |
1da177e4 LT |
244 | } |
245 | ||
1da177e4 LT |
246 | /*-------------------------------------------------------------------------*/ |
247 | ||
f1c39fad AS |
248 | /* SLAVE/GADGET SIDE UTILITY ROUTINES */ |
249 | ||
250 | /* called with spinlock held */ | |
18f2cbaa | 251 | static void nuke(struct dummy *dum, struct dummy_ep *ep) |
f1c39fad | 252 | { |
18f2cbaa | 253 | while (!list_empty(&ep->queue)) { |
f1c39fad AS |
254 | struct dummy_request *req; |
255 | ||
18f2cbaa SAS |
256 | req = list_entry(ep->queue.next, struct dummy_request, queue); |
257 | list_del_init(&req->queue); | |
f1c39fad AS |
258 | req->req.status = -ESHUTDOWN; |
259 | ||
18f2cbaa SAS |
260 | spin_unlock(&dum->lock); |
261 | req->req.complete(&ep->ep, &req->req); | |
262 | spin_lock(&dum->lock); | |
f1c39fad AS |
263 | } |
264 | } | |
265 | ||
266 | /* caller must hold lock */ | |
18f2cbaa | 267 | static void stop_activity(struct dummy *dum) |
f1c39fad AS |
268 | { |
269 | struct dummy_ep *ep; | |
270 | ||
271 | /* prevent any more requests */ | |
272 | dum->address = 0; | |
273 | ||
274 | /* The timer is left running so that outstanding URBs can fail */ | |
275 | ||
276 | /* nuke any pending requests first, so driver i/o is quiesced */ | |
18f2cbaa SAS |
277 | list_for_each_entry(ep, &dum->gadget.ep_list, ep.ep_list) |
278 | nuke(dum, ep); | |
f1c39fad AS |
279 | |
280 | /* driver now does any non-usb quiescing necessary */ | |
281 | } | |
282 | ||
1cd8fd28 TB |
283 | /** |
284 | * set_link_state_by_speed() - Sets the current state of the link according to | |
285 | * the hcd speed | |
286 | * @dum_hcd: pointer to the dummy_hcd structure to update the link state for | |
287 | * | |
288 | * This function updates the port_status according to the link state and the | |
289 | * speed of the hcd. | |
290 | */ | |
291 | static void set_link_state_by_speed(struct dummy_hcd *dum_hcd) | |
292 | { | |
293 | struct dummy *dum = dum_hcd->dum; | |
294 | ||
295 | if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) { | |
296 | if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) { | |
297 | dum_hcd->port_status = 0; | |
298 | } else if (!dum->pullup || dum->udc_suspended) { | |
299 | /* UDC suspend must cause a disconnect */ | |
300 | dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION | | |
301 | USB_PORT_STAT_ENABLE); | |
302 | if ((dum_hcd->old_status & | |
303 | USB_PORT_STAT_CONNECTION) != 0) | |
304 | dum_hcd->port_status |= | |
305 | (USB_PORT_STAT_C_CONNECTION << 16); | |
306 | } else { | |
307 | /* device is connected and not suspended */ | |
308 | dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION | | |
309 | USB_PORT_STAT_SPEED_5GBPS) ; | |
310 | if ((dum_hcd->old_status & | |
311 | USB_PORT_STAT_CONNECTION) == 0) | |
312 | dum_hcd->port_status |= | |
313 | (USB_PORT_STAT_C_CONNECTION << 16); | |
314 | if ((dum_hcd->port_status & | |
315 | USB_PORT_STAT_ENABLE) == 1 && | |
316 | (dum_hcd->port_status & | |
317 | USB_SS_PORT_LS_U0) == 1 && | |
318 | dum_hcd->rh_state != DUMMY_RH_SUSPENDED) | |
319 | dum_hcd->active = 1; | |
320 | } | |
321 | } else { | |
322 | if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) { | |
323 | dum_hcd->port_status = 0; | |
324 | } else if (!dum->pullup || dum->udc_suspended) { | |
325 | /* UDC suspend must cause a disconnect */ | |
326 | dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION | | |
327 | USB_PORT_STAT_ENABLE | | |
328 | USB_PORT_STAT_LOW_SPEED | | |
329 | USB_PORT_STAT_HIGH_SPEED | | |
330 | USB_PORT_STAT_SUSPEND); | |
331 | if ((dum_hcd->old_status & | |
332 | USB_PORT_STAT_CONNECTION) != 0) | |
333 | dum_hcd->port_status |= | |
334 | (USB_PORT_STAT_C_CONNECTION << 16); | |
335 | } else { | |
336 | dum_hcd->port_status |= USB_PORT_STAT_CONNECTION; | |
337 | if ((dum_hcd->old_status & | |
338 | USB_PORT_STAT_CONNECTION) == 0) | |
339 | dum_hcd->port_status |= | |
340 | (USB_PORT_STAT_C_CONNECTION << 16); | |
341 | if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0) | |
342 | dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND; | |
343 | else if ((dum_hcd->port_status & | |
344 | USB_PORT_STAT_SUSPEND) == 0 && | |
345 | dum_hcd->rh_state != DUMMY_RH_SUSPENDED) | |
346 | dum_hcd->active = 1; | |
347 | } | |
348 | } | |
349 | } | |
350 | ||
f1c39fad | 351 | /* caller must hold lock */ |
cdfcbd2c | 352 | static void set_link_state(struct dummy_hcd *dum_hcd) |
f1c39fad | 353 | { |
cdfcbd2c TB |
354 | struct dummy *dum = dum_hcd->dum; |
355 | ||
356 | dum_hcd->active = 0; | |
1cd8fd28 TB |
357 | if (dum->pullup) |
358 | if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 && | |
359 | dum->gadget.speed != USB_SPEED_SUPER) || | |
360 | (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 && | |
361 | dum->gadget.speed == USB_SPEED_SUPER)) | |
362 | return; | |
363 | ||
364 | set_link_state_by_speed(dum_hcd); | |
f1c39fad | 365 | |
cdfcbd2c TB |
366 | if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 || |
367 | dum_hcd->active) | |
368 | dum_hcd->resuming = 0; | |
f1c39fad | 369 | |
1cd8fd28 | 370 | /* if !connected or reset */ |
cdfcbd2c TB |
371 | if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 || |
372 | (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) { | |
1cd8fd28 TB |
373 | /* |
374 | * We're connected and not reset (reset occurred now), | |
375 | * and driver attached - disconnect! | |
376 | */ | |
cdfcbd2c | 377 | if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 && |
1cd8fd28 TB |
378 | (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 && |
379 | dum->driver) { | |
380 | stop_activity(dum); | |
381 | spin_unlock(&dum->lock); | |
382 | dum->driver->disconnect(&dum->gadget); | |
383 | spin_lock(&dum->lock); | |
f1c39fad | 384 | } |
cdfcbd2c TB |
385 | } else if (dum_hcd->active != dum_hcd->old_active) { |
386 | if (dum_hcd->old_active && dum->driver->suspend) { | |
1cd8fd28 TB |
387 | spin_unlock(&dum->lock); |
388 | dum->driver->suspend(&dum->gadget); | |
389 | spin_lock(&dum->lock); | |
390 | } else if (!dum_hcd->old_active && dum->driver->resume) { | |
391 | spin_unlock(&dum->lock); | |
392 | dum->driver->resume(&dum->gadget); | |
393 | spin_lock(&dum->lock); | |
f1c39fad AS |
394 | } |
395 | } | |
396 | ||
cdfcbd2c TB |
397 | dum_hcd->old_status = dum_hcd->port_status; |
398 | dum_hcd->old_active = dum_hcd->active; | |
f1c39fad AS |
399 | } |
400 | ||
401 | /*-------------------------------------------------------------------------*/ | |
402 | ||
1da177e4 LT |
403 | /* SLAVE/GADGET SIDE DRIVER |
404 | * | |
405 | * This only tracks gadget state. All the work is done when the host | |
406 | * side tries some (emulated) i/o operation. Real device controller | |
407 | * drivers would do real i/o using dma, fifos, irqs, timers, etc. | |
408 | */ | |
409 | ||
410 | #define is_enabled(dum) \ | |
411 | (dum->port_status & USB_PORT_STAT_ENABLE) | |
412 | ||
18f2cbaa SAS |
413 | static int dummy_enable(struct usb_ep *_ep, |
414 | const struct usb_endpoint_descriptor *desc) | |
1da177e4 LT |
415 | { |
416 | struct dummy *dum; | |
cdfcbd2c | 417 | struct dummy_hcd *dum_hcd; |
1da177e4 LT |
418 | struct dummy_ep *ep; |
419 | unsigned max; | |
420 | int retval; | |
421 | ||
18f2cbaa | 422 | ep = usb_ep_to_dummy_ep(_ep); |
1da177e4 LT |
423 | if (!_ep || !desc || ep->desc || _ep->name == ep0name |
424 | || desc->bDescriptorType != USB_DT_ENDPOINT) | |
425 | return -EINVAL; | |
18f2cbaa | 426 | dum = ep_to_dummy(ep); |
cdfcbd2c TB |
427 | if (!dum->driver) |
428 | return -ESHUTDOWN; | |
719e52cb SAS |
429 | |
430 | dum_hcd = gadget_to_dummy_hcd(&dum->gadget); | |
cdfcbd2c | 431 | if (!is_enabled(dum_hcd)) |
1da177e4 | 432 | return -ESHUTDOWN; |
cdfcbd2c TB |
433 | |
434 | /* | |
435 | * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the | |
436 | * maximum packet size. | |
1cd8fd28 | 437 | * For SS devices the wMaxPacketSize is limited by 1024. |
cdfcbd2c | 438 | */ |
29cc8897 | 439 | max = usb_endpoint_maxp(desc) & 0x7ff; |
1da177e4 LT |
440 | |
441 | /* drivers must not request bad settings, since lower levels | |
442 | * (hardware or its drivers) may not check. some endpoints | |
443 | * can't do iso, many have maxpacket limitations, etc. | |
444 | * | |
445 | * since this "hardware" driver is here to help debugging, we | |
446 | * have some extra sanity checks. (there could be more though, | |
447 | * especially for "ep9out" style fixed function ones.) | |
448 | */ | |
449 | retval = -EINVAL; | |
59f08e6d | 450 | switch (usb_endpoint_type(desc)) { |
1da177e4 | 451 | case USB_ENDPOINT_XFER_BULK: |
18f2cbaa SAS |
452 | if (strstr(ep->ep.name, "-iso") |
453 | || strstr(ep->ep.name, "-int")) { | |
1da177e4 LT |
454 | goto done; |
455 | } | |
456 | switch (dum->gadget.speed) { | |
1cd8fd28 TB |
457 | case USB_SPEED_SUPER: |
458 | if (max == 1024) | |
459 | break; | |
460 | goto done; | |
1da177e4 LT |
461 | case USB_SPEED_HIGH: |
462 | if (max == 512) | |
463 | break; | |
9063ff44 IL |
464 | goto done; |
465 | case USB_SPEED_FULL: | |
466 | if (max == 8 || max == 16 || max == 32 || max == 64) | |
1da177e4 LT |
467 | /* we'll fake any legal size */ |
468 | break; | |
9063ff44 IL |
469 | /* save a return statement */ |
470 | default: | |
471 | goto done; | |
1da177e4 LT |
472 | } |
473 | break; | |
474 | case USB_ENDPOINT_XFER_INT: | |
18f2cbaa | 475 | if (strstr(ep->ep.name, "-iso")) /* bulk is ok */ |
1da177e4 LT |
476 | goto done; |
477 | /* real hardware might not handle all packet sizes */ | |
478 | switch (dum->gadget.speed) { | |
1cd8fd28 | 479 | case USB_SPEED_SUPER: |
1da177e4 LT |
480 | case USB_SPEED_HIGH: |
481 | if (max <= 1024) | |
482 | break; | |
483 | /* save a return statement */ | |
484 | case USB_SPEED_FULL: | |
485 | if (max <= 64) | |
486 | break; | |
487 | /* save a return statement */ | |
488 | default: | |
489 | if (max <= 8) | |
490 | break; | |
491 | goto done; | |
492 | } | |
493 | break; | |
494 | case USB_ENDPOINT_XFER_ISOC: | |
18f2cbaa SAS |
495 | if (strstr(ep->ep.name, "-bulk") |
496 | || strstr(ep->ep.name, "-int")) | |
1da177e4 LT |
497 | goto done; |
498 | /* real hardware might not handle all packet sizes */ | |
499 | switch (dum->gadget.speed) { | |
1cd8fd28 | 500 | case USB_SPEED_SUPER: |
1da177e4 LT |
501 | case USB_SPEED_HIGH: |
502 | if (max <= 1024) | |
503 | break; | |
504 | /* save a return statement */ | |
505 | case USB_SPEED_FULL: | |
506 | if (max <= 1023) | |
507 | break; | |
508 | /* save a return statement */ | |
509 | default: | |
510 | goto done; | |
511 | } | |
512 | break; | |
513 | default: | |
514 | /* few chips support control except on ep0 */ | |
515 | goto done; | |
516 | } | |
517 | ||
518 | _ep->maxpacket = max; | |
a54c979f SAS |
519 | if (usb_ss_max_streams(_ep->comp_desc)) { |
520 | if (!usb_endpoint_xfer_bulk(desc)) { | |
521 | dev_err(udc_dev(dum), "Can't enable stream support on " | |
522 | "non-bulk ep %s\n", _ep->name); | |
523 | return -EINVAL; | |
524 | } | |
525 | ep->stream_en = 1; | |
526 | } | |
3cf0ad02 | 527 | ep->desc = desc; |
1da177e4 | 528 | |
a54c979f | 529 | dev_dbg(udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n", |
1da177e4 LT |
530 | _ep->name, |
531 | desc->bEndpointAddress & 0x0f, | |
532 | (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out", | |
533 | ({ char *val; | |
59f08e6d | 534 | switch (usb_endpoint_type(desc)) { |
7c884fe4 TB |
535 | case USB_ENDPOINT_XFER_BULK: |
536 | val = "bulk"; | |
537 | break; | |
538 | case USB_ENDPOINT_XFER_ISOC: | |
539 | val = "iso"; | |
540 | break; | |
541 | case USB_ENDPOINT_XFER_INT: | |
542 | val = "intr"; | |
543 | break; | |
544 | default: | |
545 | val = "ctrl"; | |
546 | break; | |
1da177e4 | 547 | }; val; }), |
a54c979f | 548 | max, ep->stream_en ? "enabled" : "disabled"); |
1da177e4 LT |
549 | |
550 | /* at this point real hardware should be NAKing transfers | |
551 | * to that endpoint, until a buffer is queued to it. | |
552 | */ | |
851a526d | 553 | ep->halted = ep->wedged = 0; |
1da177e4 LT |
554 | retval = 0; |
555 | done: | |
556 | return retval; | |
557 | } | |
558 | ||
18f2cbaa | 559 | static int dummy_disable(struct usb_ep *_ep) |
1da177e4 LT |
560 | { |
561 | struct dummy_ep *ep; | |
562 | struct dummy *dum; | |
563 | unsigned long flags; | |
564 | int retval; | |
565 | ||
18f2cbaa | 566 | ep = usb_ep_to_dummy_ep(_ep); |
1da177e4 LT |
567 | if (!_ep || !ep->desc || _ep->name == ep0name) |
568 | return -EINVAL; | |
18f2cbaa | 569 | dum = ep_to_dummy(ep); |
1da177e4 | 570 | |
18f2cbaa | 571 | spin_lock_irqsave(&dum->lock, flags); |
1da177e4 | 572 | ep->desc = NULL; |
a54c979f | 573 | ep->stream_en = 0; |
1da177e4 | 574 | retval = 0; |
18f2cbaa SAS |
575 | nuke(dum, ep); |
576 | spin_unlock_irqrestore(&dum->lock, flags); | |
1da177e4 | 577 | |
18f2cbaa | 578 | dev_dbg(udc_dev(dum), "disabled %s\n", _ep->name); |
1da177e4 LT |
579 | return retval; |
580 | } | |
581 | ||
18f2cbaa SAS |
582 | static struct usb_request *dummy_alloc_request(struct usb_ep *_ep, |
583 | gfp_t mem_flags) | |
1da177e4 LT |
584 | { |
585 | struct dummy_ep *ep; | |
586 | struct dummy_request *req; | |
587 | ||
588 | if (!_ep) | |
589 | return NULL; | |
18f2cbaa | 590 | ep = usb_ep_to_dummy_ep(_ep); |
1da177e4 | 591 | |
7039f422 | 592 | req = kzalloc(sizeof(*req), mem_flags); |
1da177e4 LT |
593 | if (!req) |
594 | return NULL; | |
18f2cbaa | 595 | INIT_LIST_HEAD(&req->queue); |
1da177e4 LT |
596 | return &req->req; |
597 | } | |
598 | ||
18f2cbaa | 599 | static void dummy_free_request(struct usb_ep *_ep, struct usb_request *_req) |
1da177e4 | 600 | { |
1da177e4 LT |
601 | struct dummy_request *req; |
602 | ||
f99987bb | 603 | if (!_ep || !_req) { |
72688dc9 | 604 | WARN_ON(1); |
1da177e4 | 605 | return; |
f99987bb | 606 | } |
1da177e4 | 607 | |
18f2cbaa SAS |
608 | req = usb_request_to_dummy_request(_req); |
609 | WARN_ON(!list_empty(&req->queue)); | |
610 | kfree(req); | |
1da177e4 LT |
611 | } |
612 | ||
18f2cbaa | 613 | static void fifo_complete(struct usb_ep *ep, struct usb_request *req) |
1da177e4 LT |
614 | { |
615 | } | |
616 | ||
18f2cbaa | 617 | static int dummy_queue(struct usb_ep *_ep, struct usb_request *_req, |
55016f10 | 618 | gfp_t mem_flags) |
1da177e4 LT |
619 | { |
620 | struct dummy_ep *ep; | |
621 | struct dummy_request *req; | |
622 | struct dummy *dum; | |
cdfcbd2c | 623 | struct dummy_hcd *dum_hcd; |
1da177e4 LT |
624 | unsigned long flags; |
625 | ||
18f2cbaa SAS |
626 | req = usb_request_to_dummy_request(_req); |
627 | if (!_req || !list_empty(&req->queue) || !_req->complete) | |
1da177e4 LT |
628 | return -EINVAL; |
629 | ||
18f2cbaa | 630 | ep = usb_ep_to_dummy_ep(_ep); |
1da177e4 LT |
631 | if (!_ep || (!ep->desc && _ep->name != ep0name)) |
632 | return -EINVAL; | |
633 | ||
18f2cbaa | 634 | dum = ep_to_dummy(ep); |
719e52cb | 635 | dum_hcd = gadget_to_dummy_hcd(&dum->gadget); |
cdfcbd2c | 636 | if (!dum->driver || !is_enabled(dum_hcd)) |
1da177e4 LT |
637 | return -ESHUTDOWN; |
638 | ||
639 | #if 0 | |
18f2cbaa | 640 | dev_dbg(udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n", |
1da177e4 LT |
641 | ep, _req, _ep->name, _req->length, _req->buf); |
642 | #endif | |
1da177e4 LT |
643 | _req->status = -EINPROGRESS; |
644 | _req->actual = 0; | |
18f2cbaa | 645 | spin_lock_irqsave(&dum->lock, flags); |
1da177e4 LT |
646 | |
647 | /* implement an emulated single-request FIFO */ | |
648 | if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) && | |
18f2cbaa SAS |
649 | list_empty(&dum->fifo_req.queue) && |
650 | list_empty(&ep->queue) && | |
1da177e4 LT |
651 | _req->length <= FIFO_SIZE) { |
652 | req = &dum->fifo_req; | |
653 | req->req = *_req; | |
654 | req->req.buf = dum->fifo_buf; | |
18f2cbaa | 655 | memcpy(dum->fifo_buf, _req->buf, _req->length); |
1da177e4 LT |
656 | req->req.context = dum; |
657 | req->req.complete = fifo_complete; | |
658 | ||
c728df70 | 659 | list_add_tail(&req->queue, &ep->queue); |
18f2cbaa | 660 | spin_unlock(&dum->lock); |
1da177e4 LT |
661 | _req->actual = _req->length; |
662 | _req->status = 0; | |
18f2cbaa SAS |
663 | _req->complete(_ep, _req); |
664 | spin_lock(&dum->lock); | |
c728df70 DB |
665 | } else |
666 | list_add_tail(&req->queue, &ep->queue); | |
18f2cbaa | 667 | spin_unlock_irqrestore(&dum->lock, flags); |
1da177e4 LT |
668 | |
669 | /* real hardware would likely enable transfers here, in case | |
670 | * it'd been left NAKing. | |
671 | */ | |
672 | return 0; | |
673 | } | |
674 | ||
18f2cbaa | 675 | static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req) |
1da177e4 LT |
676 | { |
677 | struct dummy_ep *ep; | |
678 | struct dummy *dum; | |
679 | int retval = -EINVAL; | |
680 | unsigned long flags; | |
681 | struct dummy_request *req = NULL; | |
682 | ||
683 | if (!_ep || !_req) | |
684 | return retval; | |
18f2cbaa SAS |
685 | ep = usb_ep_to_dummy_ep(_ep); |
686 | dum = ep_to_dummy(ep); | |
1da177e4 LT |
687 | |
688 | if (!dum->driver) | |
689 | return -ESHUTDOWN; | |
690 | ||
18f2cbaa SAS |
691 | local_irq_save(flags); |
692 | spin_lock(&dum->lock); | |
693 | list_for_each_entry(req, &ep->queue, queue) { | |
1da177e4 | 694 | if (&req->req == _req) { |
18f2cbaa | 695 | list_del_init(&req->queue); |
1da177e4 LT |
696 | _req->status = -ECONNRESET; |
697 | retval = 0; | |
698 | break; | |
699 | } | |
700 | } | |
18f2cbaa | 701 | spin_unlock(&dum->lock); |
1da177e4 LT |
702 | |
703 | if (retval == 0) { | |
18f2cbaa | 704 | dev_dbg(udc_dev(dum), |
1da177e4 LT |
705 | "dequeued req %p from %s, len %d buf %p\n", |
706 | req, _ep->name, _req->length, _req->buf); | |
18f2cbaa | 707 | _req->complete(_ep, _req); |
1da177e4 | 708 | } |
18f2cbaa | 709 | local_irq_restore(flags); |
1da177e4 LT |
710 | return retval; |
711 | } | |
712 | ||
713 | static int | |
851a526d | 714 | dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged) |
1da177e4 LT |
715 | { |
716 | struct dummy_ep *ep; | |
717 | struct dummy *dum; | |
718 | ||
719 | if (!_ep) | |
720 | return -EINVAL; | |
18f2cbaa SAS |
721 | ep = usb_ep_to_dummy_ep(_ep); |
722 | dum = ep_to_dummy(ep); | |
1da177e4 LT |
723 | if (!dum->driver) |
724 | return -ESHUTDOWN; | |
725 | if (!value) | |
851a526d | 726 | ep->halted = ep->wedged = 0; |
1da177e4 | 727 | else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) && |
18f2cbaa | 728 | !list_empty(&ep->queue)) |
1da177e4 | 729 | return -EAGAIN; |
851a526d | 730 | else { |
1da177e4 | 731 | ep->halted = 1; |
851a526d AS |
732 | if (wedged) |
733 | ep->wedged = 1; | |
734 | } | |
1da177e4 LT |
735 | /* FIXME clear emulated data toggle too */ |
736 | return 0; | |
737 | } | |
738 | ||
851a526d AS |
739 | static int |
740 | dummy_set_halt(struct usb_ep *_ep, int value) | |
741 | { | |
742 | return dummy_set_halt_and_wedge(_ep, value, 0); | |
743 | } | |
744 | ||
745 | static int dummy_set_wedge(struct usb_ep *_ep) | |
746 | { | |
747 | if (!_ep || _ep->name == ep0name) | |
748 | return -EINVAL; | |
749 | return dummy_set_halt_and_wedge(_ep, 1, 1); | |
750 | } | |
751 | ||
1da177e4 LT |
752 | static const struct usb_ep_ops dummy_ep_ops = { |
753 | .enable = dummy_enable, | |
754 | .disable = dummy_disable, | |
755 | ||
756 | .alloc_request = dummy_alloc_request, | |
757 | .free_request = dummy_free_request, | |
758 | ||
1da177e4 LT |
759 | .queue = dummy_queue, |
760 | .dequeue = dummy_dequeue, | |
761 | ||
762 | .set_halt = dummy_set_halt, | |
851a526d | 763 | .set_wedge = dummy_set_wedge, |
1da177e4 LT |
764 | }; |
765 | ||
766 | /*-------------------------------------------------------------------------*/ | |
767 | ||
768 | /* there are both host and device side versions of this call ... */ | |
18f2cbaa | 769 | static int dummy_g_get_frame(struct usb_gadget *_gadget) |
1da177e4 LT |
770 | { |
771 | struct timeval tv; | |
772 | ||
18f2cbaa | 773 | do_gettimeofday(&tv); |
1da177e4 LT |
774 | return tv.tv_usec / 1000; |
775 | } | |
776 | ||
18f2cbaa | 777 | static int dummy_wakeup(struct usb_gadget *_gadget) |
1da177e4 | 778 | { |
cdfcbd2c | 779 | struct dummy_hcd *dum_hcd; |
1da177e4 | 780 | |
cdfcbd2c TB |
781 | dum_hcd = gadget_to_dummy_hcd(_gadget); |
782 | if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE) | |
5742b0c9 | 783 | | (1 << USB_DEVICE_REMOTE_WAKEUP)))) |
1da177e4 | 784 | return -EINVAL; |
cdfcbd2c | 785 | if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0) |
391eca9d | 786 | return -ENOLINK; |
cdfcbd2c TB |
787 | if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 && |
788 | dum_hcd->rh_state != DUMMY_RH_SUSPENDED) | |
391eca9d AS |
789 | return -EIO; |
790 | ||
791 | /* FIXME: What if the root hub is suspended but the port isn't? */ | |
1da177e4 LT |
792 | |
793 | /* hub notices our request, issues downstream resume, etc */ | |
cdfcbd2c TB |
794 | dum_hcd->resuming = 1; |
795 | dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20); | |
796 | mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout); | |
1da177e4 LT |
797 | return 0; |
798 | } | |
799 | ||
18f2cbaa | 800 | static int dummy_set_selfpowered(struct usb_gadget *_gadget, int value) |
1da177e4 LT |
801 | { |
802 | struct dummy *dum; | |
803 | ||
18f2cbaa | 804 | dum = gadget_to_dummy_hcd(_gadget)->dum; |
1da177e4 LT |
805 | if (value) |
806 | dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED); | |
807 | else | |
808 | dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED); | |
809 | return 0; | |
810 | } | |
811 | ||
d262127c | 812 | static void dummy_udc_update_ep0(struct dummy *dum) |
b5738413 | 813 | { |
c6884191 | 814 | if (dum->gadget.speed == USB_SPEED_SUPER) |
b5738413 | 815 | dum->ep[0].ep.maxpacket = 9; |
c6884191 | 816 | else |
b5738413 | 817 | dum->ep[0].ep.maxpacket = 64; |
b5738413 SAS |
818 | } |
819 | ||
18f2cbaa | 820 | static int dummy_pullup(struct usb_gadget *_gadget, int value) |
f1c39fad | 821 | { |
d8a14a85 | 822 | struct dummy_hcd *dum_hcd; |
f1c39fad AS |
823 | struct dummy *dum; |
824 | unsigned long flags; | |
825 | ||
b5738413 SAS |
826 | dum = gadget_dev_to_dummy(&_gadget->dev); |
827 | ||
828 | if (value && dum->driver) { | |
829 | if (mod_data.is_super_speed) | |
7177aed4 | 830 | dum->gadget.speed = dum->driver->max_speed; |
b5738413 SAS |
831 | else if (mod_data.is_high_speed) |
832 | dum->gadget.speed = min_t(u8, USB_SPEED_HIGH, | |
7177aed4 | 833 | dum->driver->max_speed); |
b5738413 SAS |
834 | else |
835 | dum->gadget.speed = USB_SPEED_FULL; | |
d262127c | 836 | dummy_udc_update_ep0(dum); |
b5738413 | 837 | |
7177aed4 | 838 | if (dum->gadget.speed < dum->driver->max_speed) |
b5738413 | 839 | dev_dbg(udc_dev(dum), "This device can perform faster" |
7177aed4 MN |
840 | " if you connect it to a %s port...\n", |
841 | usb_speed_string(dum->driver->max_speed)); | |
b5738413 | 842 | } |
d8a14a85 | 843 | dum_hcd = gadget_to_dummy_hcd(_gadget); |
d8a14a85 | 844 | |
18f2cbaa | 845 | spin_lock_irqsave(&dum->lock, flags); |
f1c39fad | 846 | dum->pullup = (value != 0); |
d8a14a85 | 847 | set_link_state(dum_hcd); |
18f2cbaa | 848 | spin_unlock_irqrestore(&dum->lock, flags); |
b5738413 | 849 | |
d8a14a85 | 850 | usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd)); |
f1c39fad AS |
851 | return 0; |
852 | } | |
853 | ||
aa074739 SAS |
854 | static int dummy_udc_start(struct usb_gadget *g, |
855 | struct usb_gadget_driver *driver); | |
856 | static int dummy_udc_stop(struct usb_gadget *g, | |
857 | struct usb_gadget_driver *driver); | |
0f91349b | 858 | |
1da177e4 LT |
859 | static const struct usb_gadget_ops dummy_ops = { |
860 | .get_frame = dummy_g_get_frame, | |
861 | .wakeup = dummy_wakeup, | |
862 | .set_selfpowered = dummy_set_selfpowered, | |
f1c39fad | 863 | .pullup = dummy_pullup, |
aa074739 SAS |
864 | .udc_start = dummy_udc_start, |
865 | .udc_stop = dummy_udc_stop, | |
1da177e4 LT |
866 | }; |
867 | ||
868 | /*-------------------------------------------------------------------------*/ | |
869 | ||
870 | /* "function" sysfs attribute */ | |
18f2cbaa SAS |
871 | static ssize_t show_function(struct device *dev, struct device_attribute *attr, |
872 | char *buf) | |
1da177e4 | 873 | { |
18f2cbaa | 874 | struct dummy *dum = gadget_dev_to_dummy(dev); |
1da177e4 LT |
875 | |
876 | if (!dum->driver || !dum->driver->function) | |
877 | return 0; | |
18f2cbaa | 878 | return scnprintf(buf, PAGE_SIZE, "%s\n", dum->driver->function); |
1da177e4 | 879 | } |
18f2cbaa | 880 | static DEVICE_ATTR(function, S_IRUGO, show_function, NULL); |
1da177e4 LT |
881 | |
882 | /*-------------------------------------------------------------------------*/ | |
883 | ||
884 | /* | |
885 | * Driver registration/unregistration. | |
886 | * | |
887 | * This is basically hardware-specific; there's usually only one real USB | |
888 | * device (not host) controller since that's how USB devices are intended | |
889 | * to work. So most implementations of these api calls will rely on the | |
890 | * fact that only one driver will ever bind to the hardware. But curious | |
891 | * hardware can be built with discrete components, so the gadget API doesn't | |
892 | * require that assumption. | |
893 | * | |
894 | * For this emulator, it might be convenient to create a usb slave device | |
895 | * for each driver that registers: just add to a big root hub. | |
896 | */ | |
897 | ||
aa074739 SAS |
898 | static int dummy_udc_start(struct usb_gadget *g, |
899 | struct usb_gadget_driver *driver) | |
1da177e4 | 900 | { |
aa074739 SAS |
901 | struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g); |
902 | struct dummy *dum = dum_hcd->dum; | |
1da177e4 | 903 | |
7177aed4 | 904 | if (driver->max_speed == USB_SPEED_UNKNOWN) |
1da177e4 LT |
905 | return -EINVAL; |
906 | ||
907 | /* | |
908 | * SLAVE side init ... the layer above hardware, which | |
909 | * can't enumerate without help from the driver we're binding. | |
910 | */ | |
5742b0c9 | 911 | |
1da177e4 | 912 | dum->devstatus = 0; |
1da177e4 | 913 | |
1da177e4 | 914 | dum->driver = driver; |
18f2cbaa | 915 | dev_dbg(udc_dev(dum), "binding gadget driver '%s'\n", |
1da177e4 | 916 | driver->driver.name); |
1da177e4 LT |
917 | return 0; |
918 | } | |
1da177e4 | 919 | |
aa074739 SAS |
920 | static int dummy_udc_stop(struct usb_gadget *g, |
921 | struct usb_gadget_driver *driver) | |
1da177e4 | 922 | { |
aa074739 SAS |
923 | struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g); |
924 | struct dummy *dum = dum_hcd->dum; | |
1da177e4 | 925 | |
18f2cbaa | 926 | dev_dbg(udc_dev(dum), "unregister gadget driver '%s'\n", |
1da177e4 LT |
927 | driver->driver.name); |
928 | ||
1da177e4 | 929 | dum->driver = NULL; |
25427874 | 930 | |
1da177e4 LT |
931 | return 0; |
932 | } | |
1da177e4 LT |
933 | |
934 | #undef is_enabled | |
935 | ||
d9b76251 AS |
936 | /* The gadget structure is stored inside the hcd structure and will be |
937 | * released along with it. */ | |
0fb57599 SAS |
938 | static void init_dummy_udc_hw(struct dummy *dum) |
939 | { | |
940 | int i; | |
941 | ||
942 | INIT_LIST_HEAD(&dum->gadget.ep_list); | |
943 | for (i = 0; i < DUMMY_ENDPOINTS; i++) { | |
944 | struct dummy_ep *ep = &dum->ep[i]; | |
945 | ||
946 | if (!ep_name[i]) | |
947 | break; | |
948 | ep->ep.name = ep_name[i]; | |
949 | ep->ep.ops = &dummy_ep_ops; | |
950 | list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list); | |
951 | ep->halted = ep->wedged = ep->already_seen = | |
952 | ep->setup_stage = 0; | |
953 | ep->ep.maxpacket = ~0; | |
c6884191 | 954 | ep->ep.max_streams = 16; |
0fb57599 SAS |
955 | ep->last_io = jiffies; |
956 | ep->gadget = &dum->gadget; | |
957 | ep->desc = NULL; | |
958 | INIT_LIST_HEAD(&ep->queue); | |
959 | } | |
960 | ||
961 | dum->gadget.ep0 = &dum->ep[0].ep; | |
962 | list_del_init(&dum->ep[0].ep.ep_list); | |
963 | INIT_LIST_HEAD(&dum->fifo_req.queue); | |
f8744d40 SAS |
964 | |
965 | #ifdef CONFIG_USB_OTG | |
966 | dum->gadget.is_otg = 1; | |
967 | #endif | |
0fb57599 SAS |
968 | } |
969 | ||
18f2cbaa | 970 | static int dummy_udc_probe(struct platform_device *pdev) |
d9b76251 | 971 | { |
b100a2f3 | 972 | struct dummy *dum; |
d9b76251 AS |
973 | int rc; |
974 | ||
b100a2f3 | 975 | dum = *((void **)dev_get_platdata(&pdev->dev)); |
d9b76251 AS |
976 | dum->gadget.name = gadget_name; |
977 | dum->gadget.ops = &dummy_ops; | |
d327ab5b | 978 | dum->gadget.max_speed = USB_SPEED_SUPER; |
d9b76251 | 979 | |
8364d6b0 | 980 | dum->gadget.dev.parent = &pdev->dev; |
0fb57599 SAS |
981 | init_dummy_udc_hw(dum); |
982 | ||
0f91349b SAS |
983 | rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget); |
984 | if (rc < 0) | |
985 | goto err_udc; | |
986 | ||
18f2cbaa | 987 | rc = device_create_file(&dum->gadget.dev, &dev_attr_function); |
efd54a36 | 988 | if (rc < 0) |
0f91349b SAS |
989 | goto err_dev; |
990 | platform_set_drvdata(pdev, dum); | |
991 | return rc; | |
992 | ||
993 | err_dev: | |
994 | usb_del_gadget_udc(&dum->gadget); | |
995 | err_udc: | |
d9b76251 AS |
996 | return rc; |
997 | } | |
998 | ||
18f2cbaa | 999 | static int dummy_udc_remove(struct platform_device *pdev) |
d9b76251 | 1000 | { |
18f2cbaa | 1001 | struct dummy *dum = platform_get_drvdata(pdev); |
d9b76251 | 1002 | |
0f91349b | 1003 | usb_del_gadget_udc(&dum->gadget); |
18f2cbaa | 1004 | device_remove_file(&dum->gadget.dev, &dev_attr_function); |
d9b76251 AS |
1005 | return 0; |
1006 | } | |
1007 | ||
fc0b721f SAS |
1008 | static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd, |
1009 | int suspend) | |
391eca9d | 1010 | { |
fc0b721f SAS |
1011 | spin_lock_irq(&dum->lock); |
1012 | dum->udc_suspended = suspend; | |
d8a14a85 | 1013 | set_link_state(dum_hcd); |
fc0b721f SAS |
1014 | spin_unlock_irq(&dum->lock); |
1015 | } | |
1016 | ||
1017 | static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state) | |
1018 | { | |
1019 | struct dummy *dum = platform_get_drvdata(pdev); | |
1020 | struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget); | |
391eca9d | 1021 | |
fc0b721f SAS |
1022 | dev_dbg(&pdev->dev, "%s\n", __func__); |
1023 | dummy_udc_pm(dum, dum_hcd, 1); | |
d8a14a85 | 1024 | usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd)); |
391eca9d AS |
1025 | return 0; |
1026 | } | |
1027 | ||
fc0b721f | 1028 | static int dummy_udc_resume(struct platform_device *pdev) |
391eca9d | 1029 | { |
fc0b721f SAS |
1030 | struct dummy *dum = platform_get_drvdata(pdev); |
1031 | struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget); | |
391eca9d | 1032 | |
fc0b721f SAS |
1033 | dev_dbg(&pdev->dev, "%s\n", __func__); |
1034 | dummy_udc_pm(dum, dum_hcd, 0); | |
d8a14a85 | 1035 | usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd)); |
391eca9d AS |
1036 | return 0; |
1037 | } | |
1038 | ||
3ae5eaec | 1039 | static struct platform_driver dummy_udc_driver = { |
d9b76251 AS |
1040 | .probe = dummy_udc_probe, |
1041 | .remove = dummy_udc_remove, | |
391eca9d AS |
1042 | .suspend = dummy_udc_suspend, |
1043 | .resume = dummy_udc_resume, | |
3ae5eaec RK |
1044 | .driver = { |
1045 | .name = (char *) gadget_name, | |
1046 | .owner = THIS_MODULE, | |
1047 | }, | |
d9b76251 AS |
1048 | }; |
1049 | ||
1da177e4 LT |
1050 | /*-------------------------------------------------------------------------*/ |
1051 | ||
a54c979f SAS |
1052 | static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc) |
1053 | { | |
1054 | unsigned int index; | |
1055 | ||
1056 | index = usb_endpoint_num(desc) << 1; | |
1057 | if (usb_endpoint_dir_in(desc)) | |
1058 | index |= 1; | |
1059 | return index; | |
1060 | } | |
1061 | ||
1da177e4 LT |
1062 | /* MASTER/HOST SIDE DRIVER |
1063 | * | |
1064 | * this uses the hcd framework to hook up to host side drivers. | |
1065 | * its root hub will only have one device, otherwise it acts like | |
1066 | * a normal host controller. | |
1067 | * | |
1068 | * when urbs are queued, they're just stuck on a list that we | |
1069 | * scan in a timer callback. that callback connects writes from | |
1070 | * the host with reads from the device, and so on, based on the | |
1071 | * usb 2.0 rules. | |
1072 | */ | |
1073 | ||
a54c979f SAS |
1074 | static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb) |
1075 | { | |
1076 | const struct usb_endpoint_descriptor *desc = &urb->ep->desc; | |
1077 | u32 index; | |
1078 | ||
1079 | if (!usb_endpoint_xfer_bulk(desc)) | |
1080 | return 0; | |
1081 | ||
1082 | index = dummy_get_ep_idx(desc); | |
1083 | return (1 << index) & dum_hcd->stream_en_ep; | |
1084 | } | |
1085 | ||
1086 | /* | |
1087 | * The max stream number is saved as a nibble so for the 30 possible endpoints | |
1088 | * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0 | |
1089 | * means we use only 1 stream). The maximum according to the spec is 16bit so | |
1090 | * if the 16 stream limit is about to go, the array size should be incremented | |
1091 | * to 30 elements of type u16. | |
1092 | */ | |
1093 | static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd, | |
1094 | unsigned int pipe) | |
1095 | { | |
1096 | int max_streams; | |
1097 | ||
1098 | max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)]; | |
1099 | if (usb_pipeout(pipe)) | |
1100 | max_streams >>= 4; | |
1101 | else | |
1102 | max_streams &= 0xf; | |
1103 | max_streams++; | |
1104 | return max_streams; | |
1105 | } | |
1106 | ||
1107 | static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd, | |
1108 | unsigned int pipe, unsigned int streams) | |
1109 | { | |
1110 | int max_streams; | |
1111 | ||
1112 | streams--; | |
1113 | max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)]; | |
1114 | if (usb_pipeout(pipe)) { | |
1115 | streams <<= 4; | |
1116 | max_streams &= 0xf; | |
1117 | } else { | |
1118 | max_streams &= 0xf0; | |
1119 | } | |
1120 | max_streams |= streams; | |
1121 | dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams; | |
1122 | } | |
1123 | ||
1124 | static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb) | |
1125 | { | |
1126 | unsigned int max_streams; | |
1127 | int enabled; | |
1128 | ||
1129 | enabled = dummy_ep_stream_en(dum_hcd, urb); | |
1130 | if (!urb->stream_id) { | |
1131 | if (enabled) | |
1132 | return -EINVAL; | |
1133 | return 0; | |
1134 | } | |
1135 | if (!enabled) | |
1136 | return -EINVAL; | |
1137 | ||
1138 | max_streams = get_max_streams_for_pipe(dum_hcd, | |
1139 | usb_pipeendpoint(urb->pipe)); | |
1140 | if (urb->stream_id > max_streams) { | |
1141 | dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n", | |
1142 | urb->stream_id); | |
1143 | BUG(); | |
1144 | return -EINVAL; | |
1145 | } | |
1146 | return 0; | |
1147 | } | |
1148 | ||
18f2cbaa | 1149 | static int dummy_urb_enqueue( |
1da177e4 | 1150 | struct usb_hcd *hcd, |
1da177e4 | 1151 | struct urb *urb, |
55016f10 | 1152 | gfp_t mem_flags |
1da177e4 | 1153 | ) { |
cdfcbd2c | 1154 | struct dummy_hcd *dum_hcd; |
1da177e4 LT |
1155 | struct urbp *urbp; |
1156 | unsigned long flags; | |
e9df41c5 | 1157 | int rc; |
1da177e4 | 1158 | |
18f2cbaa | 1159 | urbp = kmalloc(sizeof *urbp, mem_flags); |
1da177e4 LT |
1160 | if (!urbp) |
1161 | return -ENOMEM; | |
1162 | urbp->urb = urb; | |
14fce33a | 1163 | urbp->miter_started = 0; |
1da177e4 | 1164 | |
cdfcbd2c TB |
1165 | dum_hcd = hcd_to_dummy_hcd(hcd); |
1166 | spin_lock_irqsave(&dum_hcd->dum->lock, flags); | |
a54c979f SAS |
1167 | |
1168 | rc = dummy_validate_stream(dum_hcd, urb); | |
1169 | if (rc) { | |
1170 | kfree(urbp); | |
1171 | goto done; | |
1172 | } | |
1173 | ||
e9df41c5 AS |
1174 | rc = usb_hcd_link_urb_to_ep(hcd, urb); |
1175 | if (rc) { | |
1176 | kfree(urbp); | |
1177 | goto done; | |
1178 | } | |
1da177e4 | 1179 | |
cdfcbd2c TB |
1180 | if (!dum_hcd->udev) { |
1181 | dum_hcd->udev = urb->dev; | |
1182 | usb_get_dev(dum_hcd->udev); | |
1183 | } else if (unlikely(dum_hcd->udev != urb->dev)) | |
1184 | dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n"); | |
1da177e4 | 1185 | |
cdfcbd2c | 1186 | list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list); |
1da177e4 | 1187 | urb->hcpriv = urbp; |
18f2cbaa | 1188 | if (usb_pipetype(urb->pipe) == PIPE_CONTROL) |
1da177e4 LT |
1189 | urb->error_count = 1; /* mark as a new urb */ |
1190 | ||
1191 | /* kick the scheduler, it'll do the rest */ | |
cdfcbd2c TB |
1192 | if (!timer_pending(&dum_hcd->timer)) |
1193 | mod_timer(&dum_hcd->timer, jiffies + 1); | |
1da177e4 | 1194 | |
e9df41c5 | 1195 | done: |
cdfcbd2c | 1196 | spin_unlock_irqrestore(&dum_hcd->dum->lock, flags); |
e9df41c5 | 1197 | return rc; |
1da177e4 LT |
1198 | } |
1199 | ||
e9df41c5 | 1200 | static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) |
1da177e4 | 1201 | { |
cdfcbd2c | 1202 | struct dummy_hcd *dum_hcd; |
391eca9d | 1203 | unsigned long flags; |
e9df41c5 | 1204 | int rc; |
391eca9d AS |
1205 | |
1206 | /* giveback happens automatically in timer callback, | |
1207 | * so make sure the callback happens */ | |
cdfcbd2c TB |
1208 | dum_hcd = hcd_to_dummy_hcd(hcd); |
1209 | spin_lock_irqsave(&dum_hcd->dum->lock, flags); | |
e9df41c5 AS |
1210 | |
1211 | rc = usb_hcd_check_unlink_urb(hcd, urb, status); | |
cdfcbd2c TB |
1212 | if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING && |
1213 | !list_empty(&dum_hcd->urbp_list)) | |
1214 | mod_timer(&dum_hcd->timer, jiffies); | |
e9df41c5 | 1215 | |
cdfcbd2c | 1216 | spin_unlock_irqrestore(&dum_hcd->dum->lock, flags); |
e9df41c5 | 1217 | return rc; |
1da177e4 LT |
1218 | } |
1219 | ||
a04ce20d SAS |
1220 | static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req, |
1221 | u32 len) | |
1222 | { | |
1223 | void *ubuf, *rbuf; | |
14fce33a | 1224 | struct urbp *urbp = urb->hcpriv; |
a04ce20d | 1225 | int to_host; |
14fce33a SAS |
1226 | struct sg_mapping_iter *miter = &urbp->miter; |
1227 | u32 trans = 0; | |
1228 | u32 this_sg; | |
1229 | bool next_sg; | |
a04ce20d SAS |
1230 | |
1231 | to_host = usb_pipein(urb->pipe); | |
1232 | rbuf = req->req.buf + req->req.actual; | |
a04ce20d | 1233 | |
14fce33a SAS |
1234 | if (!urb->num_sgs) { |
1235 | ubuf = urb->transfer_buffer + urb->actual_length; | |
1236 | if (to_host) | |
1237 | memcpy(ubuf, rbuf, len); | |
1238 | else | |
1239 | memcpy(rbuf, ubuf, len); | |
1240 | return len; | |
1241 | } | |
1242 | ||
1243 | if (!urbp->miter_started) { | |
1244 | u32 flags = SG_MITER_ATOMIC; | |
1245 | ||
1246 | if (to_host) | |
1247 | flags |= SG_MITER_TO_SG; | |
1248 | else | |
1249 | flags |= SG_MITER_FROM_SG; | |
1250 | ||
1251 | sg_miter_start(miter, urb->sg, urb->num_sgs, flags); | |
1252 | urbp->miter_started = 1; | |
1253 | } | |
1254 | next_sg = sg_miter_next(miter); | |
1255 | if (next_sg == false) { | |
1256 | WARN_ON_ONCE(1); | |
1257 | return -EINVAL; | |
1258 | } | |
1259 | do { | |
1260 | ubuf = miter->addr; | |
1261 | this_sg = min_t(u32, len, miter->length); | |
1262 | miter->consumed = this_sg; | |
1263 | trans += this_sg; | |
1264 | ||
1265 | if (to_host) | |
1266 | memcpy(ubuf, rbuf, this_sg); | |
1267 | else | |
1268 | memcpy(rbuf, ubuf, this_sg); | |
1269 | len -= this_sg; | |
1270 | ||
1271 | if (!len) | |
1272 | break; | |
1273 | next_sg = sg_miter_next(miter); | |
1274 | if (next_sg == false) { | |
1275 | WARN_ON_ONCE(1); | |
1276 | return -EINVAL; | |
1277 | } | |
1278 | ||
1279 | rbuf += this_sg; | |
1280 | } while (1); | |
1281 | ||
1282 | sg_miter_stop(miter); | |
1283 | return trans; | |
a04ce20d SAS |
1284 | } |
1285 | ||
1da177e4 | 1286 | /* transfer up to a frame's worth; caller must own lock */ |
a54c979f SAS |
1287 | static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb, |
1288 | struct dummy_ep *ep, int limit, int *status) | |
1da177e4 | 1289 | { |
a54c979f | 1290 | struct dummy *dum = dum_hcd->dum; |
1da177e4 LT |
1291 | struct dummy_request *req; |
1292 | ||
1293 | top: | |
1294 | /* if there's no request queued, the device is NAKing; return */ | |
18f2cbaa | 1295 | list_for_each_entry(req, &ep->queue, queue) { |
1da177e4 LT |
1296 | unsigned host_len, dev_len, len; |
1297 | int is_short, to_host; | |
1298 | int rescan = 0; | |
1299 | ||
a54c979f SAS |
1300 | if (dummy_ep_stream_en(dum_hcd, urb)) { |
1301 | if ((urb->stream_id != req->req.stream_id)) | |
1302 | continue; | |
1303 | } | |
1304 | ||
1da177e4 LT |
1305 | /* 1..N packets of ep->ep.maxpacket each ... the last one |
1306 | * may be short (including zero length). | |
1307 | * | |
1308 | * writer can send a zlp explicitly (length 0) or implicitly | |
1309 | * (length mod maxpacket zero, and 'zero' flag); they always | |
1310 | * terminate reads. | |
1311 | */ | |
1312 | host_len = urb->transfer_buffer_length - urb->actual_length; | |
1313 | dev_len = req->req.length - req->req.actual; | |
18f2cbaa | 1314 | len = min(host_len, dev_len); |
1da177e4 LT |
1315 | |
1316 | /* FIXME update emulated data toggle too */ | |
1317 | ||
18f2cbaa SAS |
1318 | to_host = usb_pipein(urb->pipe); |
1319 | if (unlikely(len == 0)) | |
1da177e4 LT |
1320 | is_short = 1; |
1321 | else { | |
1da177e4 LT |
1322 | /* not enough bandwidth left? */ |
1323 | if (limit < ep->ep.maxpacket && limit < len) | |
1324 | break; | |
18f2cbaa | 1325 | len = min_t(unsigned, len, limit); |
1da177e4 LT |
1326 | if (len == 0) |
1327 | break; | |
1328 | ||
1329 | /* use an extra pass for the final short packet */ | |
1330 | if (len > ep->ep.maxpacket) { | |
1331 | rescan = 1; | |
1332 | len -= (len % ep->ep.maxpacket); | |
1333 | } | |
1334 | is_short = (len % ep->ep.maxpacket) != 0; | |
1335 | ||
a04ce20d SAS |
1336 | len = dummy_perform_transfer(urb, req, len); |
1337 | ||
1da177e4 | 1338 | ep->last_io = jiffies; |
b1443ac0 | 1339 | if ((int)len < 0) { |
14fce33a SAS |
1340 | req->req.status = len; |
1341 | } else { | |
1342 | limit -= len; | |
1343 | urb->actual_length += len; | |
1344 | req->req.actual += len; | |
1345 | } | |
1da177e4 LT |
1346 | } |
1347 | ||
1348 | /* short packets terminate, maybe with overflow/underflow. | |
1349 | * it's only really an error to write too much. | |
1350 | * | |
1351 | * partially filling a buffer optionally blocks queue advances | |
1352 | * (so completion handlers can clean up the queue) but we don't | |
b0d9efba | 1353 | * need to emulate such data-in-flight. |
1da177e4 LT |
1354 | */ |
1355 | if (is_short) { | |
1356 | if (host_len == dev_len) { | |
1357 | req->req.status = 0; | |
4d2f110c | 1358 | *status = 0; |
1da177e4 LT |
1359 | } else if (to_host) { |
1360 | req->req.status = 0; | |
1361 | if (dev_len > host_len) | |
4d2f110c | 1362 | *status = -EOVERFLOW; |
1da177e4 | 1363 | else |
4d2f110c | 1364 | *status = 0; |
1da177e4 | 1365 | } else if (!to_host) { |
4d2f110c | 1366 | *status = 0; |
1da177e4 LT |
1367 | if (host_len > dev_len) |
1368 | req->req.status = -EOVERFLOW; | |
1369 | else | |
1370 | req->req.status = 0; | |
1371 | } | |
1372 | ||
1373 | /* many requests terminate without a short packet */ | |
1374 | } else { | |
1375 | if (req->req.length == req->req.actual | |
1376 | && !req->req.zero) | |
1377 | req->req.status = 0; | |
1378 | if (urb->transfer_buffer_length == urb->actual_length | |
1379 | && !(urb->transfer_flags | |
4d2f110c AS |
1380 | & URB_ZERO_PACKET)) |
1381 | *status = 0; | |
1da177e4 LT |
1382 | } |
1383 | ||
1384 | /* device side completion --> continuable */ | |
1385 | if (req->req.status != -EINPROGRESS) { | |
18f2cbaa | 1386 | list_del_init(&req->queue); |
1da177e4 | 1387 | |
18f2cbaa SAS |
1388 | spin_unlock(&dum->lock); |
1389 | req->req.complete(&ep->ep, &req->req); | |
1390 | spin_lock(&dum->lock); | |
1da177e4 LT |
1391 | |
1392 | /* requests might have been unlinked... */ | |
1393 | rescan = 1; | |
1394 | } | |
1395 | ||
1396 | /* host side completion --> terminate */ | |
4d2f110c | 1397 | if (*status != -EINPROGRESS) |
1da177e4 LT |
1398 | break; |
1399 | ||
1400 | /* rescan to continue with any other queued i/o */ | |
1401 | if (rescan) | |
1402 | goto top; | |
1403 | } | |
1404 | return limit; | |
1405 | } | |
1406 | ||
18f2cbaa | 1407 | static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep) |
1da177e4 LT |
1408 | { |
1409 | int limit = ep->ep.maxpacket; | |
1410 | ||
1411 | if (dum->gadget.speed == USB_SPEED_HIGH) { | |
1412 | int tmp; | |
1413 | ||
1414 | /* high bandwidth mode */ | |
29cc8897 | 1415 | tmp = usb_endpoint_maxp(ep->desc); |
1da177e4 LT |
1416 | tmp = (tmp >> 11) & 0x03; |
1417 | tmp *= 8 /* applies to entire frame */; | |
1418 | limit += limit * tmp; | |
1419 | } | |
1cd8fd28 | 1420 | if (dum->gadget.speed == USB_SPEED_SUPER) { |
59f08e6d | 1421 | switch (usb_endpoint_type(ep->desc)) { |
1cd8fd28 TB |
1422 | case USB_ENDPOINT_XFER_ISOC: |
1423 | /* Sec. 4.4.8.2 USB3.0 Spec */ | |
1424 | limit = 3 * 16 * 1024 * 8; | |
1425 | break; | |
1426 | case USB_ENDPOINT_XFER_INT: | |
1427 | /* Sec. 4.4.7.2 USB3.0 Spec */ | |
1428 | limit = 3 * 1024 * 8; | |
1429 | break; | |
1430 | case USB_ENDPOINT_XFER_BULK: | |
1431 | default: | |
1432 | break; | |
1433 | } | |
1434 | } | |
1da177e4 LT |
1435 | return limit; |
1436 | } | |
1437 | ||
cdfcbd2c | 1438 | #define is_active(dum_hcd) ((dum_hcd->port_status & \ |
1da177e4 LT |
1439 | (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \ |
1440 | USB_PORT_STAT_SUSPEND)) \ | |
1441 | == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE)) | |
1442 | ||
18f2cbaa | 1443 | static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address) |
1da177e4 LT |
1444 | { |
1445 | int i; | |
1446 | ||
1cd8fd28 TB |
1447 | if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ? |
1448 | dum->ss_hcd : dum->hs_hcd))) | |
1da177e4 LT |
1449 | return NULL; |
1450 | if ((address & ~USB_DIR_IN) == 0) | |
18f2cbaa | 1451 | return &dum->ep[0]; |
1da177e4 | 1452 | for (i = 1; i < DUMMY_ENDPOINTS; i++) { |
18f2cbaa | 1453 | struct dummy_ep *ep = &dum->ep[i]; |
1da177e4 LT |
1454 | |
1455 | if (!ep->desc) | |
1456 | continue; | |
1457 | if (ep->desc->bEndpointAddress == address) | |
1458 | return ep; | |
1459 | } | |
1460 | return NULL; | |
1461 | } | |
1462 | ||
1463 | #undef is_active | |
1464 | ||
1465 | #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE) | |
1466 | #define Dev_InRequest (Dev_Request | USB_DIR_IN) | |
1467 | #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE) | |
1468 | #define Intf_InRequest (Intf_Request | USB_DIR_IN) | |
1469 | #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT) | |
1470 | #define Ep_InRequest (Ep_Request | USB_DIR_IN) | |
1471 | ||
8be8a9d3 TB |
1472 | |
1473 | /** | |
1474 | * handle_control_request() - handles all control transfers | |
1475 | * @dum: pointer to dummy (the_controller) | |
1476 | * @urb: the urb request to handle | |
1477 | * @setup: pointer to the setup data for a USB device control | |
1478 | * request | |
1479 | * @status: pointer to request handling status | |
1480 | * | |
1481 | * Return 0 - if the request was handled | |
1482 | * 1 - if the request wasn't handles | |
1483 | * error code on error | |
1484 | */ | |
cdfcbd2c | 1485 | static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb, |
8be8a9d3 TB |
1486 | struct usb_ctrlrequest *setup, |
1487 | int *status) | |
1488 | { | |
1489 | struct dummy_ep *ep2; | |
cdfcbd2c | 1490 | struct dummy *dum = dum_hcd->dum; |
8be8a9d3 TB |
1491 | int ret_val = 1; |
1492 | unsigned w_index; | |
1493 | unsigned w_value; | |
1494 | ||
1495 | w_index = le16_to_cpu(setup->wIndex); | |
1496 | w_value = le16_to_cpu(setup->wValue); | |
1497 | switch (setup->bRequest) { | |
1498 | case USB_REQ_SET_ADDRESS: | |
1499 | if (setup->bRequestType != Dev_Request) | |
1500 | break; | |
1501 | dum->address = w_value; | |
1502 | *status = 0; | |
1503 | dev_dbg(udc_dev(dum), "set_address = %d\n", | |
1504 | w_value); | |
1505 | ret_val = 0; | |
1506 | break; | |
1507 | case USB_REQ_SET_FEATURE: | |
1508 | if (setup->bRequestType == Dev_Request) { | |
1509 | ret_val = 0; | |
1510 | switch (w_value) { | |
1511 | case USB_DEVICE_REMOTE_WAKEUP: | |
1512 | break; | |
1513 | case USB_DEVICE_B_HNP_ENABLE: | |
1514 | dum->gadget.b_hnp_enable = 1; | |
1515 | break; | |
1516 | case USB_DEVICE_A_HNP_SUPPORT: | |
1517 | dum->gadget.a_hnp_support = 1; | |
1518 | break; | |
1519 | case USB_DEVICE_A_ALT_HNP_SUPPORT: | |
1520 | dum->gadget.a_alt_hnp_support = 1; | |
1521 | break; | |
1cd8fd28 TB |
1522 | case USB_DEVICE_U1_ENABLE: |
1523 | if (dummy_hcd_to_hcd(dum_hcd)->speed == | |
1524 | HCD_USB3) | |
1525 | w_value = USB_DEV_STAT_U1_ENABLED; | |
1526 | else | |
1527 | ret_val = -EOPNOTSUPP; | |
1528 | break; | |
1529 | case USB_DEVICE_U2_ENABLE: | |
1530 | if (dummy_hcd_to_hcd(dum_hcd)->speed == | |
1531 | HCD_USB3) | |
1532 | w_value = USB_DEV_STAT_U2_ENABLED; | |
1533 | else | |
1534 | ret_val = -EOPNOTSUPP; | |
1535 | break; | |
1536 | case USB_DEVICE_LTM_ENABLE: | |
1537 | if (dummy_hcd_to_hcd(dum_hcd)->speed == | |
1538 | HCD_USB3) | |
1539 | w_value = USB_DEV_STAT_LTM_ENABLED; | |
1540 | else | |
1541 | ret_val = -EOPNOTSUPP; | |
1542 | break; | |
8be8a9d3 TB |
1543 | default: |
1544 | ret_val = -EOPNOTSUPP; | |
1545 | } | |
1546 | if (ret_val == 0) { | |
1547 | dum->devstatus |= (1 << w_value); | |
1548 | *status = 0; | |
1549 | } | |
1550 | } else if (setup->bRequestType == Ep_Request) { | |
1551 | /* endpoint halt */ | |
1552 | ep2 = find_endpoint(dum, w_index); | |
1553 | if (!ep2 || ep2->ep.name == ep0name) { | |
1554 | ret_val = -EOPNOTSUPP; | |
1555 | break; | |
1556 | } | |
1557 | ep2->halted = 1; | |
1558 | ret_val = 0; | |
1559 | *status = 0; | |
1560 | } | |
1561 | break; | |
1562 | case USB_REQ_CLEAR_FEATURE: | |
1563 | if (setup->bRequestType == Dev_Request) { | |
1564 | ret_val = 0; | |
1565 | switch (w_value) { | |
1566 | case USB_DEVICE_REMOTE_WAKEUP: | |
1567 | w_value = USB_DEVICE_REMOTE_WAKEUP; | |
1568 | break; | |
1cd8fd28 TB |
1569 | case USB_DEVICE_U1_ENABLE: |
1570 | if (dummy_hcd_to_hcd(dum_hcd)->speed == | |
1571 | HCD_USB3) | |
1572 | w_value = USB_DEV_STAT_U1_ENABLED; | |
1573 | else | |
1574 | ret_val = -EOPNOTSUPP; | |
1575 | break; | |
1576 | case USB_DEVICE_U2_ENABLE: | |
1577 | if (dummy_hcd_to_hcd(dum_hcd)->speed == | |
1578 | HCD_USB3) | |
1579 | w_value = USB_DEV_STAT_U2_ENABLED; | |
1580 | else | |
1581 | ret_val = -EOPNOTSUPP; | |
1582 | break; | |
1583 | case USB_DEVICE_LTM_ENABLE: | |
1584 | if (dummy_hcd_to_hcd(dum_hcd)->speed == | |
1585 | HCD_USB3) | |
1586 | w_value = USB_DEV_STAT_LTM_ENABLED; | |
1587 | else | |
1588 | ret_val = -EOPNOTSUPP; | |
1589 | break; | |
8be8a9d3 TB |
1590 | default: |
1591 | ret_val = -EOPNOTSUPP; | |
1592 | break; | |
1593 | } | |
1594 | if (ret_val == 0) { | |
1595 | dum->devstatus &= ~(1 << w_value); | |
1596 | *status = 0; | |
1597 | } | |
1598 | } else if (setup->bRequestType == Ep_Request) { | |
1599 | /* endpoint halt */ | |
1600 | ep2 = find_endpoint(dum, w_index); | |
1601 | if (!ep2) { | |
1602 | ret_val = -EOPNOTSUPP; | |
1603 | break; | |
1604 | } | |
1605 | if (!ep2->wedged) | |
1606 | ep2->halted = 0; | |
1607 | ret_val = 0; | |
1608 | *status = 0; | |
1609 | } | |
1610 | break; | |
1611 | case USB_REQ_GET_STATUS: | |
1612 | if (setup->bRequestType == Dev_InRequest | |
1613 | || setup->bRequestType == Intf_InRequest | |
1614 | || setup->bRequestType == Ep_InRequest) { | |
1615 | char *buf; | |
1616 | /* | |
1617 | * device: remote wakeup, selfpowered | |
1618 | * interface: nothing | |
1619 | * endpoint: halt | |
1620 | */ | |
1621 | buf = (char *)urb->transfer_buffer; | |
1622 | if (urb->transfer_buffer_length > 0) { | |
1623 | if (setup->bRequestType == Ep_InRequest) { | |
1624 | ep2 = find_endpoint(dum, w_index); | |
1625 | if (!ep2) { | |
1626 | ret_val = -EOPNOTSUPP; | |
1627 | break; | |
1628 | } | |
1629 | buf[0] = ep2->halted; | |
1630 | } else if (setup->bRequestType == | |
1631 | Dev_InRequest) { | |
1632 | buf[0] = (u8)dum->devstatus; | |
1633 | } else | |
1634 | buf[0] = 0; | |
1635 | } | |
1636 | if (urb->transfer_buffer_length > 1) | |
1637 | buf[1] = 0; | |
1638 | urb->actual_length = min_t(u32, 2, | |
1639 | urb->transfer_buffer_length); | |
1640 | ret_val = 0; | |
1641 | *status = 0; | |
1642 | } | |
1643 | break; | |
1644 | } | |
1645 | return ret_val; | |
1646 | } | |
1647 | ||
1da177e4 LT |
1648 | /* drive both sides of the transfers; looks like irq handlers to |
1649 | * both drivers except the callbacks aren't in_irq(). | |
1650 | */ | |
cdfcbd2c | 1651 | static void dummy_timer(unsigned long _dum_hcd) |
1da177e4 | 1652 | { |
cdfcbd2c TB |
1653 | struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd; |
1654 | struct dummy *dum = dum_hcd->dum; | |
1da177e4 LT |
1655 | struct urbp *urbp, *tmp; |
1656 | unsigned long flags; | |
1657 | int limit, total; | |
1658 | int i; | |
1659 | ||
1660 | /* simplistic model for one frame's bandwidth */ | |
1661 | switch (dum->gadget.speed) { | |
1662 | case USB_SPEED_LOW: | |
1663 | total = 8/*bytes*/ * 12/*packets*/; | |
1664 | break; | |
1665 | case USB_SPEED_FULL: | |
1666 | total = 64/*bytes*/ * 19/*packets*/; | |
1667 | break; | |
1668 | case USB_SPEED_HIGH: | |
1669 | total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/; | |
1670 | break; | |
1cd8fd28 TB |
1671 | case USB_SPEED_SUPER: |
1672 | /* Bus speed is 500000 bytes/ms, so use a little less */ | |
1673 | total = 490000; | |
1674 | break; | |
1da177e4 | 1675 | default: |
cdfcbd2c | 1676 | dev_err(dummy_dev(dum_hcd), "bogus device speed\n"); |
1da177e4 LT |
1677 | return; |
1678 | } | |
1679 | ||
1680 | /* FIXME if HZ != 1000 this will probably misbehave ... */ | |
1681 | ||
1682 | /* look at each urb queued by the host side driver */ | |
18f2cbaa | 1683 | spin_lock_irqsave(&dum->lock, flags); |
1da177e4 | 1684 | |
cdfcbd2c TB |
1685 | if (!dum_hcd->udev) { |
1686 | dev_err(dummy_dev(dum_hcd), | |
1da177e4 | 1687 | "timer fired with no URBs pending?\n"); |
18f2cbaa | 1688 | spin_unlock_irqrestore(&dum->lock, flags); |
1da177e4 LT |
1689 | return; |
1690 | } | |
1691 | ||
1692 | for (i = 0; i < DUMMY_ENDPOINTS; i++) { | |
18f2cbaa | 1693 | if (!ep_name[i]) |
1da177e4 | 1694 | break; |
18f2cbaa | 1695 | dum->ep[i].already_seen = 0; |
1da177e4 LT |
1696 | } |
1697 | ||
1698 | restart: | |
cdfcbd2c | 1699 | list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) { |
1da177e4 LT |
1700 | struct urb *urb; |
1701 | struct dummy_request *req; | |
1702 | u8 address; | |
1703 | struct dummy_ep *ep = NULL; | |
1704 | int type; | |
4d2f110c | 1705 | int status = -EINPROGRESS; |
1da177e4 LT |
1706 | |
1707 | urb = urbp->urb; | |
eb231054 | 1708 | if (urb->unlinked) |
1da177e4 | 1709 | goto return_urb; |
cdfcbd2c | 1710 | else if (dum_hcd->rh_state != DUMMY_RH_RUNNING) |
391eca9d | 1711 | continue; |
18f2cbaa | 1712 | type = usb_pipetype(urb->pipe); |
1da177e4 LT |
1713 | |
1714 | /* used up this frame's non-periodic bandwidth? | |
1715 | * FIXME there's infinite bandwidth for control and | |
1716 | * periodic transfers ... unrealistic. | |
1717 | */ | |
1718 | if (total <= 0 && type == PIPE_BULK) | |
1719 | continue; | |
1720 | ||
1721 | /* find the gadget's ep for this request (if configured) */ | |
1722 | address = usb_pipeendpoint (urb->pipe); | |
18f2cbaa | 1723 | if (usb_pipein(urb->pipe)) |
1da177e4 LT |
1724 | address |= USB_DIR_IN; |
1725 | ep = find_endpoint(dum, address); | |
1726 | if (!ep) { | |
1727 | /* set_configuration() disagreement */ | |
cdfcbd2c | 1728 | dev_dbg(dummy_dev(dum_hcd), |
1da177e4 LT |
1729 | "no ep configured for urb %p\n", |
1730 | urb); | |
4d2f110c | 1731 | status = -EPROTO; |
1da177e4 LT |
1732 | goto return_urb; |
1733 | } | |
1734 | ||
1735 | if (ep->already_seen) | |
1736 | continue; | |
1737 | ep->already_seen = 1; | |
18f2cbaa | 1738 | if (ep == &dum->ep[0] && urb->error_count) { |
1da177e4 LT |
1739 | ep->setup_stage = 1; /* a new urb */ |
1740 | urb->error_count = 0; | |
1741 | } | |
1742 | if (ep->halted && !ep->setup_stage) { | |
1743 | /* NOTE: must not be iso! */ | |
cdfcbd2c | 1744 | dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n", |
1da177e4 | 1745 | ep->ep.name, urb); |
4d2f110c | 1746 | status = -EPIPE; |
1da177e4 LT |
1747 | goto return_urb; |
1748 | } | |
1749 | /* FIXME make sure both ends agree on maxpacket */ | |
1750 | ||
1751 | /* handle control requests */ | |
18f2cbaa | 1752 | if (ep == &dum->ep[0] && ep->setup_stage) { |
1da177e4 LT |
1753 | struct usb_ctrlrequest setup; |
1754 | int value = 1; | |
1da177e4 | 1755 | |
18f2cbaa | 1756 | setup = *(struct usb_ctrlrequest *) urb->setup_packet; |
1da177e4 | 1757 | /* paranoia, in case of stale queued data */ |
18f2cbaa SAS |
1758 | list_for_each_entry(req, &ep->queue, queue) { |
1759 | list_del_init(&req->queue); | |
1da177e4 | 1760 | req->req.status = -EOVERFLOW; |
18f2cbaa | 1761 | dev_dbg(udc_dev(dum), "stale req = %p\n", |
1da177e4 LT |
1762 | req); |
1763 | ||
18f2cbaa SAS |
1764 | spin_unlock(&dum->lock); |
1765 | req->req.complete(&ep->ep, &req->req); | |
1766 | spin_lock(&dum->lock); | |
1da177e4 LT |
1767 | ep->already_seen = 0; |
1768 | goto restart; | |
1769 | } | |
1770 | ||
1771 | /* gadget driver never sees set_address or operations | |
1772 | * on standard feature flags. some hardware doesn't | |
1773 | * even expose them. | |
1774 | */ | |
1775 | ep->last_io = jiffies; | |
1776 | ep->setup_stage = 0; | |
1777 | ep->halted = 0; | |
1da177e4 | 1778 | |
cdfcbd2c | 1779 | value = handle_control_request(dum_hcd, urb, &setup, |
8be8a9d3 | 1780 | &status); |
1da177e4 LT |
1781 | |
1782 | /* gadget driver handles all other requests. block | |
1783 | * until setup() returns; no reentrancy issues etc. | |
1784 | */ | |
1785 | if (value > 0) { | |
18f2cbaa SAS |
1786 | spin_unlock(&dum->lock); |
1787 | value = dum->driver->setup(&dum->gadget, | |
1da177e4 | 1788 | &setup); |
18f2cbaa | 1789 | spin_lock(&dum->lock); |
1da177e4 LT |
1790 | |
1791 | if (value >= 0) { | |
1792 | /* no delays (max 64KB data stage) */ | |
1793 | limit = 64*1024; | |
1794 | goto treat_control_like_bulk; | |
1795 | } | |
1796 | /* error, see below */ | |
1797 | } | |
1798 | ||
1799 | if (value < 0) { | |
1800 | if (value != -EOPNOTSUPP) | |
18f2cbaa | 1801 | dev_dbg(udc_dev(dum), |
1da177e4 LT |
1802 | "setup --> %d\n", |
1803 | value); | |
4d2f110c | 1804 | status = -EPIPE; |
1da177e4 LT |
1805 | urb->actual_length = 0; |
1806 | } | |
1807 | ||
1808 | goto return_urb; | |
1809 | } | |
1810 | ||
1811 | /* non-control requests */ | |
1812 | limit = total; | |
18f2cbaa | 1813 | switch (usb_pipetype(urb->pipe)) { |
1da177e4 LT |
1814 | case PIPE_ISOCHRONOUS: |
1815 | /* FIXME is it urb->interval since the last xfer? | |
1816 | * use urb->iso_frame_desc[i]. | |
1817 | * complete whether or not ep has requests queued. | |
1818 | * report random errors, to debug drivers. | |
1819 | */ | |
18f2cbaa | 1820 | limit = max(limit, periodic_bytes(dum, ep)); |
4d2f110c | 1821 | status = -ENOSYS; |
1da177e4 LT |
1822 | break; |
1823 | ||
1824 | case PIPE_INTERRUPT: | |
1825 | /* FIXME is it urb->interval since the last xfer? | |
1826 | * this almost certainly polls too fast. | |
1827 | */ | |
18f2cbaa | 1828 | limit = max(limit, periodic_bytes(dum, ep)); |
1da177e4 LT |
1829 | /* FALLTHROUGH */ |
1830 | ||
1da177e4 | 1831 | default: |
18f2cbaa | 1832 | treat_control_like_bulk: |
1da177e4 | 1833 | ep->last_io = jiffies; |
a54c979f | 1834 | total = transfer(dum_hcd, urb, ep, limit, &status); |
1da177e4 LT |
1835 | break; |
1836 | } | |
1837 | ||
1838 | /* incomplete transfer? */ | |
4d2f110c | 1839 | if (status == -EINPROGRESS) |
1da177e4 LT |
1840 | continue; |
1841 | ||
1842 | return_urb: | |
18f2cbaa SAS |
1843 | list_del(&urbp->urbp_list); |
1844 | kfree(urbp); | |
1da177e4 LT |
1845 | if (ep) |
1846 | ep->already_seen = ep->setup_stage = 0; | |
1847 | ||
cdfcbd2c | 1848 | usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb); |
18f2cbaa | 1849 | spin_unlock(&dum->lock); |
cdfcbd2c | 1850 | usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status); |
18f2cbaa | 1851 | spin_lock(&dum->lock); |
1da177e4 LT |
1852 | |
1853 | goto restart; | |
1854 | } | |
1855 | ||
cdfcbd2c TB |
1856 | if (list_empty(&dum_hcd->urbp_list)) { |
1857 | usb_put_dev(dum_hcd->udev); | |
1858 | dum_hcd->udev = NULL; | |
1859 | } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) { | |
391eca9d | 1860 | /* want a 1 msec delay here */ |
cdfcbd2c | 1861 | mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1)); |
1da177e4 LT |
1862 | } |
1863 | ||
18f2cbaa | 1864 | spin_unlock_irqrestore(&dum->lock, flags); |
1da177e4 LT |
1865 | } |
1866 | ||
1867 | /*-------------------------------------------------------------------------*/ | |
1868 | ||
1869 | #define PORT_C_MASK \ | |
c2db8b5e AS |
1870 | ((USB_PORT_STAT_C_CONNECTION \ |
1871 | | USB_PORT_STAT_C_ENABLE \ | |
1872 | | USB_PORT_STAT_C_SUSPEND \ | |
1873 | | USB_PORT_STAT_C_OVERCURRENT \ | |
1874 | | USB_PORT_STAT_C_RESET) << 16) | |
1da177e4 | 1875 | |
18f2cbaa | 1876 | static int dummy_hub_status(struct usb_hcd *hcd, char *buf) |
1da177e4 | 1877 | { |
cdfcbd2c | 1878 | struct dummy_hcd *dum_hcd; |
1da177e4 | 1879 | unsigned long flags; |
391eca9d | 1880 | int retval = 0; |
1da177e4 | 1881 | |
cdfcbd2c | 1882 | dum_hcd = hcd_to_dummy_hcd(hcd); |
1da177e4 | 1883 | |
cdfcbd2c | 1884 | spin_lock_irqsave(&dum_hcd->dum->lock, flags); |
541c7d43 | 1885 | if (!HCD_HW_ACCESSIBLE(hcd)) |
391eca9d | 1886 | goto done; |
f1c39fad | 1887 | |
cdfcbd2c TB |
1888 | if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) { |
1889 | dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16); | |
1890 | dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND; | |
1891 | set_link_state(dum_hcd); | |
f1c39fad AS |
1892 | } |
1893 | ||
cdfcbd2c | 1894 | if ((dum_hcd->port_status & PORT_C_MASK) != 0) { |
1da177e4 | 1895 | *buf = (1 << 1); |
cdfcbd2c TB |
1896 | dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n", |
1897 | dum_hcd->port_status); | |
1da177e4 | 1898 | retval = 1; |
cdfcbd2c | 1899 | if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED) |
18f2cbaa | 1900 | usb_hcd_resume_root_hub(hcd); |
1da177e4 | 1901 | } |
391eca9d | 1902 | done: |
cdfcbd2c | 1903 | spin_unlock_irqrestore(&dum_hcd->dum->lock, flags); |
1da177e4 LT |
1904 | return retval; |
1905 | } | |
1906 | ||
3b9c1c5b | 1907 | /* usb 3.0 root hub device descriptor */ |
814cf212 | 1908 | static struct { |
3b9c1c5b SAS |
1909 | struct usb_bos_descriptor bos; |
1910 | struct usb_ss_cap_descriptor ss_cap; | |
1911 | } __packed usb3_bos_desc = { | |
1912 | ||
1913 | .bos = { | |
1914 | .bLength = USB_DT_BOS_SIZE, | |
1915 | .bDescriptorType = USB_DT_BOS, | |
1916 | .wTotalLength = cpu_to_le16(sizeof(usb3_bos_desc)), | |
1917 | .bNumDeviceCaps = 1, | |
1918 | }, | |
1919 | .ss_cap = { | |
1920 | .bLength = USB_DT_USB_SS_CAP_SIZE, | |
1921 | .bDescriptorType = USB_DT_DEVICE_CAPABILITY, | |
1922 | .bDevCapabilityType = USB_SS_CAP_TYPE, | |
1923 | .wSpeedSupported = cpu_to_le16(USB_5GBPS_OPERATION), | |
1924 | .bFunctionalitySupport = ilog2(USB_5GBPS_OPERATION), | |
1925 | }, | |
1926 | }; | |
1927 | ||
1cd8fd28 TB |
1928 | static inline void |
1929 | ss_hub_descriptor(struct usb_hub_descriptor *desc) | |
1930 | { | |
1931 | memset(desc, 0, sizeof *desc); | |
1932 | desc->bDescriptorType = 0x2a; | |
1933 | desc->bDescLength = 12; | |
1934 | desc->wHubCharacteristics = cpu_to_le16(0x0001); | |
1935 | desc->bNbrPorts = 1; | |
1936 | desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/ | |
1937 | desc->u.ss.DeviceRemovable = 0xffff; | |
1938 | } | |
1939 | ||
18f2cbaa | 1940 | static inline void hub_descriptor(struct usb_hub_descriptor *desc) |
1da177e4 | 1941 | { |
18f2cbaa | 1942 | memset(desc, 0, sizeof *desc); |
1da177e4 LT |
1943 | desc->bDescriptorType = 0x29; |
1944 | desc->bDescLength = 9; | |
fd05e720 | 1945 | desc->wHubCharacteristics = cpu_to_le16(0x0001); |
1da177e4 | 1946 | desc->bNbrPorts = 1; |
dbe79bbe JY |
1947 | desc->u.hs.DeviceRemovable[0] = 0xff; |
1948 | desc->u.hs.DeviceRemovable[1] = 0xff; | |
1da177e4 LT |
1949 | } |
1950 | ||
18f2cbaa | 1951 | static int dummy_hub_control( |
1da177e4 LT |
1952 | struct usb_hcd *hcd, |
1953 | u16 typeReq, | |
1954 | u16 wValue, | |
1955 | u16 wIndex, | |
1956 | char *buf, | |
1957 | u16 wLength | |
1958 | ) { | |
cdfcbd2c | 1959 | struct dummy_hcd *dum_hcd; |
1da177e4 LT |
1960 | int retval = 0; |
1961 | unsigned long flags; | |
1962 | ||
541c7d43 | 1963 | if (!HCD_HW_ACCESSIBLE(hcd)) |
391eca9d AS |
1964 | return -ETIMEDOUT; |
1965 | ||
cdfcbd2c TB |
1966 | dum_hcd = hcd_to_dummy_hcd(hcd); |
1967 | ||
1968 | spin_lock_irqsave(&dum_hcd->dum->lock, flags); | |
1da177e4 LT |
1969 | switch (typeReq) { |
1970 | case ClearHubFeature: | |
1971 | break; | |
1972 | case ClearPortFeature: | |
1973 | switch (wValue) { | |
1974 | case USB_PORT_FEAT_SUSPEND: | |
1cd8fd28 TB |
1975 | if (hcd->speed == HCD_USB3) { |
1976 | dev_dbg(dummy_dev(dum_hcd), | |
1977 | "USB_PORT_FEAT_SUSPEND req not " | |
1978 | "supported for USB 3.0 roothub\n"); | |
1979 | goto error; | |
1980 | } | |
cdfcbd2c | 1981 | if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) { |
1da177e4 | 1982 | /* 20msec resume signaling */ |
cdfcbd2c TB |
1983 | dum_hcd->resuming = 1; |
1984 | dum_hcd->re_timeout = jiffies + | |
f1c39fad | 1985 | msecs_to_jiffies(20); |
1da177e4 LT |
1986 | } |
1987 | break; | |
1988 | case USB_PORT_FEAT_POWER: | |
1cd8fd28 TB |
1989 | if (hcd->speed == HCD_USB3) { |
1990 | if (dum_hcd->port_status & USB_PORT_STAT_POWER) | |
1991 | dev_dbg(dummy_dev(dum_hcd), | |
1992 | "power-off\n"); | |
1993 | } else | |
1994 | if (dum_hcd->port_status & | |
1995 | USB_SS_PORT_STAT_POWER) | |
1996 | dev_dbg(dummy_dev(dum_hcd), | |
1997 | "power-off\n"); | |
f1c39fad | 1998 | /* FALLS THROUGH */ |
1da177e4 | 1999 | default: |
cdfcbd2c TB |
2000 | dum_hcd->port_status &= ~(1 << wValue); |
2001 | set_link_state(dum_hcd); | |
1da177e4 LT |
2002 | } |
2003 | break; | |
2004 | case GetHubDescriptor: | |
1cd8fd28 TB |
2005 | if (hcd->speed == HCD_USB3 && |
2006 | (wLength < USB_DT_SS_HUB_SIZE || | |
2007 | wValue != (USB_DT_SS_HUB << 8))) { | |
2008 | dev_dbg(dummy_dev(dum_hcd), | |
2009 | "Wrong hub descriptor type for " | |
2010 | "USB 3.0 roothub.\n"); | |
2011 | goto error; | |
2012 | } | |
2013 | if (hcd->speed == HCD_USB3) | |
2014 | ss_hub_descriptor((struct usb_hub_descriptor *) buf); | |
2015 | else | |
2016 | hub_descriptor((struct usb_hub_descriptor *) buf); | |
1da177e4 | 2017 | break; |
3b9c1c5b SAS |
2018 | |
2019 | case DeviceRequest | USB_REQ_GET_DESCRIPTOR: | |
2020 | if (hcd->speed != HCD_USB3) | |
2021 | goto error; | |
2022 | ||
2023 | if ((wValue >> 8) != USB_DT_BOS) | |
2024 | goto error; | |
2025 | ||
2026 | memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc)); | |
2027 | retval = sizeof(usb3_bos_desc); | |
2028 | break; | |
2029 | ||
1da177e4 | 2030 | case GetHubStatus: |
18f2cbaa | 2031 | *(__le32 *) buf = cpu_to_le32(0); |
1da177e4 LT |
2032 | break; |
2033 | case GetPortStatus: | |
2034 | if (wIndex != 1) | |
2035 | retval = -EPIPE; | |
2036 | ||
2037 | /* whoever resets or resumes must GetPortStatus to | |
2038 | * complete it!! | |
2039 | */ | |
cdfcbd2c TB |
2040 | if (dum_hcd->resuming && |
2041 | time_after_eq(jiffies, dum_hcd->re_timeout)) { | |
2042 | dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16); | |
2043 | dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND; | |
1da177e4 | 2044 | } |
cdfcbd2c TB |
2045 | if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 && |
2046 | time_after_eq(jiffies, dum_hcd->re_timeout)) { | |
2047 | dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16); | |
2048 | dum_hcd->port_status &= ~USB_PORT_STAT_RESET; | |
2049 | if (dum_hcd->dum->pullup) { | |
2050 | dum_hcd->port_status |= USB_PORT_STAT_ENABLE; | |
1cd8fd28 TB |
2051 | |
2052 | if (hcd->speed < HCD_USB3) { | |
2053 | switch (dum_hcd->dum->gadget.speed) { | |
2054 | case USB_SPEED_HIGH: | |
2055 | dum_hcd->port_status |= | |
2056 | USB_PORT_STAT_HIGH_SPEED; | |
2057 | break; | |
2058 | case USB_SPEED_LOW: | |
2059 | dum_hcd->dum->gadget.ep0-> | |
2060 | maxpacket = 8; | |
2061 | dum_hcd->port_status |= | |
2062 | USB_PORT_STAT_LOW_SPEED; | |
2063 | break; | |
2064 | default: | |
2065 | dum_hcd->dum->gadget.speed = | |
2066 | USB_SPEED_FULL; | |
2067 | break; | |
2068 | } | |
1da177e4 LT |
2069 | } |
2070 | } | |
2071 | } | |
cdfcbd2c | 2072 | set_link_state(dum_hcd); |
18f2cbaa SAS |
2073 | ((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status); |
2074 | ((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16); | |
1da177e4 LT |
2075 | break; |
2076 | case SetHubFeature: | |
2077 | retval = -EPIPE; | |
2078 | break; | |
2079 | case SetPortFeature: | |
2080 | switch (wValue) { | |
1cd8fd28 TB |
2081 | case USB_PORT_FEAT_LINK_STATE: |
2082 | if (hcd->speed != HCD_USB3) { | |
2083 | dev_dbg(dummy_dev(dum_hcd), | |
2084 | "USB_PORT_FEAT_LINK_STATE req not " | |
2085 | "supported for USB 2.0 roothub\n"); | |
2086 | goto error; | |
2087 | } | |
2088 | /* | |
2089 | * Since this is dummy we don't have an actual link so | |
2090 | * there is nothing to do for the SET_LINK_STATE cmd | |
2091 | */ | |
2092 | break; | |
2093 | case USB_PORT_FEAT_U1_TIMEOUT: | |
2094 | case USB_PORT_FEAT_U2_TIMEOUT: | |
2095 | /* TODO: add suspend/resume support! */ | |
2096 | if (hcd->speed != HCD_USB3) { | |
2097 | dev_dbg(dummy_dev(dum_hcd), | |
2098 | "USB_PORT_FEAT_U1/2_TIMEOUT req not " | |
2099 | "supported for USB 2.0 roothub\n"); | |
2100 | goto error; | |
2101 | } | |
2102 | break; | |
1da177e4 | 2103 | case USB_PORT_FEAT_SUSPEND: |
1cd8fd28 TB |
2104 | /* Applicable only for USB2.0 hub */ |
2105 | if (hcd->speed == HCD_USB3) { | |
2106 | dev_dbg(dummy_dev(dum_hcd), | |
2107 | "USB_PORT_FEAT_SUSPEND req not " | |
2108 | "supported for USB 3.0 roothub\n"); | |
2109 | goto error; | |
2110 | } | |
cdfcbd2c TB |
2111 | if (dum_hcd->active) { |
2112 | dum_hcd->port_status |= USB_PORT_STAT_SUSPEND; | |
f1c39fad AS |
2113 | |
2114 | /* HNP would happen here; for now we | |
2115 | * assume b_bus_req is always true. | |
2116 | */ | |
cdfcbd2c | 2117 | set_link_state(dum_hcd); |
f1c39fad | 2118 | if (((1 << USB_DEVICE_B_HNP_ENABLE) |
cdfcbd2c TB |
2119 | & dum_hcd->dum->devstatus) != 0) |
2120 | dev_dbg(dummy_dev(dum_hcd), | |
5742b0c9 | 2121 | "no HNP yet!\n"); |
1da177e4 LT |
2122 | } |
2123 | break; | |
f1c39fad | 2124 | case USB_PORT_FEAT_POWER: |
1cd8fd28 TB |
2125 | if (hcd->speed == HCD_USB3) |
2126 | dum_hcd->port_status |= USB_SS_PORT_STAT_POWER; | |
2127 | else | |
2128 | dum_hcd->port_status |= USB_PORT_STAT_POWER; | |
cdfcbd2c | 2129 | set_link_state(dum_hcd); |
f1c39fad | 2130 | break; |
1cd8fd28 TB |
2131 | case USB_PORT_FEAT_BH_PORT_RESET: |
2132 | /* Applicable only for USB3.0 hub */ | |
2133 | if (hcd->speed != HCD_USB3) { | |
2134 | dev_dbg(dummy_dev(dum_hcd), | |
2135 | "USB_PORT_FEAT_BH_PORT_RESET req not " | |
2136 | "supported for USB 2.0 roothub\n"); | |
2137 | goto error; | |
2138 | } | |
2139 | /* FALLS THROUGH */ | |
1da177e4 | 2140 | case USB_PORT_FEAT_RESET: |
f1c39fad | 2141 | /* if it's already enabled, disable */ |
1cd8fd28 TB |
2142 | if (hcd->speed == HCD_USB3) { |
2143 | dum_hcd->port_status = 0; | |
2144 | dum_hcd->port_status = | |
2145 | (USB_SS_PORT_STAT_POWER | | |
2146 | USB_PORT_STAT_CONNECTION | | |
2147 | USB_PORT_STAT_RESET); | |
2148 | } else | |
2149 | dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE | |
f1c39fad AS |
2150 | | USB_PORT_STAT_LOW_SPEED |
2151 | | USB_PORT_STAT_HIGH_SPEED); | |
cdfcbd2c TB |
2152 | /* |
2153 | * We want to reset device status. All but the | |
2154 | * Self powered feature | |
2155 | */ | |
2156 | dum_hcd->dum->devstatus &= | |
2157 | (1 << USB_DEVICE_SELF_POWERED); | |
1cd8fd28 TB |
2158 | /* |
2159 | * FIXME USB3.0: what is the correct reset signaling | |
2160 | * interval? Is it still 50msec as for HS? | |
2161 | */ | |
cdfcbd2c | 2162 | dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50); |
f1c39fad | 2163 | /* FALLS THROUGH */ |
1da177e4 | 2164 | default: |
1cd8fd28 TB |
2165 | if (hcd->speed == HCD_USB3) { |
2166 | if ((dum_hcd->port_status & | |
2167 | USB_SS_PORT_STAT_POWER) != 0) { | |
2168 | dum_hcd->port_status |= (1 << wValue); | |
2169 | set_link_state(dum_hcd); | |
2170 | } | |
2171 | } else | |
2172 | if ((dum_hcd->port_status & | |
2173 | USB_PORT_STAT_POWER) != 0) { | |
2174 | dum_hcd->port_status |= (1 << wValue); | |
2175 | set_link_state(dum_hcd); | |
2176 | } | |
2177 | } | |
2178 | break; | |
2179 | case GetPortErrorCount: | |
2180 | if (hcd->speed != HCD_USB3) { | |
2181 | dev_dbg(dummy_dev(dum_hcd), | |
2182 | "GetPortErrorCount req not " | |
2183 | "supported for USB 2.0 roothub\n"); | |
2184 | goto error; | |
2185 | } | |
2186 | /* We'll always return 0 since this is a dummy hub */ | |
2187 | *(__le32 *) buf = cpu_to_le32(0); | |
2188 | break; | |
2189 | case SetHubDepth: | |
2190 | if (hcd->speed != HCD_USB3) { | |
2191 | dev_dbg(dummy_dev(dum_hcd), | |
2192 | "SetHubDepth req not supported for " | |
2193 | "USB 2.0 roothub\n"); | |
2194 | goto error; | |
1da177e4 LT |
2195 | } |
2196 | break; | |
1da177e4 | 2197 | default: |
cdfcbd2c | 2198 | dev_dbg(dummy_dev(dum_hcd), |
1da177e4 LT |
2199 | "hub control req%04x v%04x i%04x l%d\n", |
2200 | typeReq, wValue, wIndex, wLength); | |
1cd8fd28 | 2201 | error: |
1da177e4 LT |
2202 | /* "protocol stall" on error */ |
2203 | retval = -EPIPE; | |
2204 | } | |
cdfcbd2c | 2205 | spin_unlock_irqrestore(&dum_hcd->dum->lock, flags); |
685eb93f | 2206 | |
cdfcbd2c | 2207 | if ((dum_hcd->port_status & PORT_C_MASK) != 0) |
18f2cbaa | 2208 | usb_hcd_poll_rh_status(hcd); |
1da177e4 LT |
2209 | return retval; |
2210 | } | |
2211 | ||
18f2cbaa | 2212 | static int dummy_bus_suspend(struct usb_hcd *hcd) |
391eca9d | 2213 | { |
cdfcbd2c | 2214 | struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd); |
391eca9d | 2215 | |
18f2cbaa | 2216 | dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__); |
3cf0a22e | 2217 | |
cdfcbd2c TB |
2218 | spin_lock_irq(&dum_hcd->dum->lock); |
2219 | dum_hcd->rh_state = DUMMY_RH_SUSPENDED; | |
2220 | set_link_state(dum_hcd); | |
3cf0a22e | 2221 | hcd->state = HC_STATE_SUSPENDED; |
cdfcbd2c | 2222 | spin_unlock_irq(&dum_hcd->dum->lock); |
391eca9d AS |
2223 | return 0; |
2224 | } | |
2225 | ||
18f2cbaa | 2226 | static int dummy_bus_resume(struct usb_hcd *hcd) |
391eca9d | 2227 | { |
cdfcbd2c | 2228 | struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd); |
3cf0a22e AS |
2229 | int rc = 0; |
2230 | ||
18f2cbaa | 2231 | dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__); |
391eca9d | 2232 | |
cdfcbd2c | 2233 | spin_lock_irq(&dum_hcd->dum->lock); |
541c7d43 | 2234 | if (!HCD_HW_ACCESSIBLE(hcd)) { |
cfa59dab | 2235 | rc = -ESHUTDOWN; |
3cf0a22e | 2236 | } else { |
cdfcbd2c TB |
2237 | dum_hcd->rh_state = DUMMY_RH_RUNNING; |
2238 | set_link_state(dum_hcd); | |
2239 | if (!list_empty(&dum_hcd->urbp_list)) | |
2240 | mod_timer(&dum_hcd->timer, jiffies); | |
3cf0a22e AS |
2241 | hcd->state = HC_STATE_RUNNING; |
2242 | } | |
cdfcbd2c | 2243 | spin_unlock_irq(&dum_hcd->dum->lock); |
3cf0a22e | 2244 | return rc; |
391eca9d | 2245 | } |
1da177e4 LT |
2246 | |
2247 | /*-------------------------------------------------------------------------*/ | |
2248 | ||
18f2cbaa | 2249 | static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb) |
1da177e4 | 2250 | { |
18f2cbaa | 2251 | int ep = usb_pipeendpoint(urb->pipe); |
1da177e4 | 2252 | |
18f2cbaa | 2253 | return snprintf(buf, size, |
1da177e4 LT |
2254 | "urb/%p %s ep%d%s%s len %d/%d\n", |
2255 | urb, | |
2256 | ({ char *s; | |
18f2cbaa SAS |
2257 | switch (urb->dev->speed) { |
2258 | case USB_SPEED_LOW: | |
7c884fe4 TB |
2259 | s = "ls"; |
2260 | break; | |
18f2cbaa | 2261 | case USB_SPEED_FULL: |
7c884fe4 TB |
2262 | s = "fs"; |
2263 | break; | |
18f2cbaa | 2264 | case USB_SPEED_HIGH: |
7c884fe4 TB |
2265 | s = "hs"; |
2266 | break; | |
18f2cbaa | 2267 | case USB_SPEED_SUPER: |
1cd8fd28 TB |
2268 | s = "ss"; |
2269 | break; | |
18f2cbaa | 2270 | default: |
7c884fe4 TB |
2271 | s = "?"; |
2272 | break; | |
1da177e4 | 2273 | }; s; }), |
18f2cbaa | 2274 | ep, ep ? (usb_pipein(urb->pipe) ? "in" : "out") : "", |
1da177e4 | 2275 | ({ char *s; \ |
18f2cbaa SAS |
2276 | switch (usb_pipetype(urb->pipe)) { \ |
2277 | case PIPE_CONTROL: \ | |
7c884fe4 TB |
2278 | s = ""; \ |
2279 | break; \ | |
18f2cbaa | 2280 | case PIPE_BULK: \ |
7c884fe4 TB |
2281 | s = "-bulk"; \ |
2282 | break; \ | |
18f2cbaa | 2283 | case PIPE_INTERRUPT: \ |
7c884fe4 TB |
2284 | s = "-int"; \ |
2285 | break; \ | |
18f2cbaa | 2286 | default: \ |
7c884fe4 TB |
2287 | s = "-iso"; \ |
2288 | break; \ | |
18f2cbaa | 2289 | }; s; }), |
1da177e4 LT |
2290 | urb->actual_length, urb->transfer_buffer_length); |
2291 | } | |
2292 | ||
18f2cbaa SAS |
2293 | static ssize_t show_urbs(struct device *dev, struct device_attribute *attr, |
2294 | char *buf) | |
1da177e4 | 2295 | { |
18f2cbaa | 2296 | struct usb_hcd *hcd = dev_get_drvdata(dev); |
cdfcbd2c | 2297 | struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd); |
1da177e4 LT |
2298 | struct urbp *urbp; |
2299 | size_t size = 0; | |
2300 | unsigned long flags; | |
2301 | ||
cdfcbd2c TB |
2302 | spin_lock_irqsave(&dum_hcd->dum->lock, flags); |
2303 | list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) { | |
1da177e4 LT |
2304 | size_t temp; |
2305 | ||
18f2cbaa | 2306 | temp = show_urb(buf, PAGE_SIZE - size, urbp->urb); |
1da177e4 LT |
2307 | buf += temp; |
2308 | size += temp; | |
2309 | } | |
cdfcbd2c | 2310 | spin_unlock_irqrestore(&dum_hcd->dum->lock, flags); |
1da177e4 LT |
2311 | |
2312 | return size; | |
2313 | } | |
18f2cbaa | 2314 | static DEVICE_ATTR(urbs, S_IRUGO, show_urbs, NULL); |
1da177e4 | 2315 | |
1cd8fd28 TB |
2316 | static int dummy_start_ss(struct dummy_hcd *dum_hcd) |
2317 | { | |
2318 | init_timer(&dum_hcd->timer); | |
2319 | dum_hcd->timer.function = dummy_timer; | |
2320 | dum_hcd->timer.data = (unsigned long)dum_hcd; | |
2321 | dum_hcd->rh_state = DUMMY_RH_RUNNING; | |
a54c979f | 2322 | dum_hcd->stream_en_ep = 0; |
1cd8fd28 TB |
2323 | INIT_LIST_HEAD(&dum_hcd->urbp_list); |
2324 | dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET; | |
2325 | dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING; | |
2326 | dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1; | |
2327 | #ifdef CONFIG_USB_OTG | |
2328 | dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1; | |
2329 | #endif | |
2330 | return 0; | |
2331 | ||
2332 | /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */ | |
2333 | return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs); | |
2334 | } | |
2335 | ||
cdfcbd2c | 2336 | static int dummy_start(struct usb_hcd *hcd) |
1da177e4 | 2337 | { |
cdfcbd2c | 2338 | struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd); |
1da177e4 LT |
2339 | |
2340 | /* | |
2341 | * MASTER side init ... we emulate a root hub that'll only ever | |
2342 | * talk to one device (the slave side). Also appears in sysfs, | |
2343 | * just like more familiar pci-based HCDs. | |
2344 | */ | |
1cd8fd28 TB |
2345 | if (!usb_hcd_is_primary_hcd(hcd)) |
2346 | return dummy_start_ss(dum_hcd); | |
2347 | ||
cdfcbd2c TB |
2348 | spin_lock_init(&dum_hcd->dum->lock); |
2349 | init_timer(&dum_hcd->timer); | |
2350 | dum_hcd->timer.function = dummy_timer; | |
2351 | dum_hcd->timer.data = (unsigned long)dum_hcd; | |
2352 | dum_hcd->rh_state = DUMMY_RH_RUNNING; | |
1da177e4 | 2353 | |
cdfcbd2c | 2354 | INIT_LIST_HEAD(&dum_hcd->urbp_list); |
1da177e4 | 2355 | |
caf29f62 | 2356 | hcd->power_budget = POWER_BUDGET; |
1da177e4 | 2357 | hcd->state = HC_STATE_RUNNING; |
685eb93f | 2358 | hcd->uses_new_polling = 1; |
1da177e4 | 2359 | |
5742b0c9 AS |
2360 | #ifdef CONFIG_USB_OTG |
2361 | hcd->self.otg_port = 1; | |
2362 | #endif | |
2363 | ||
1da177e4 | 2364 | /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */ |
cdfcbd2c | 2365 | return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs); |
1da177e4 LT |
2366 | } |
2367 | ||
18f2cbaa | 2368 | static void dummy_stop(struct usb_hcd *hcd) |
1da177e4 LT |
2369 | { |
2370 | struct dummy *dum; | |
2371 | ||
18f2cbaa | 2372 | dum = hcd_to_dummy_hcd(hcd)->dum; |
cdfcbd2c TB |
2373 | device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs); |
2374 | usb_gadget_unregister_driver(dum->driver); | |
2375 | dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n"); | |
1da177e4 LT |
2376 | } |
2377 | ||
2378 | /*-------------------------------------------------------------------------*/ | |
2379 | ||
18f2cbaa | 2380 | static int dummy_h_get_frame(struct usb_hcd *hcd) |
1da177e4 | 2381 | { |
18f2cbaa | 2382 | return dummy_g_get_frame(NULL); |
1da177e4 LT |
2383 | } |
2384 | ||
cdfcbd2c TB |
2385 | static int dummy_setup(struct usb_hcd *hcd) |
2386 | { | |
b100a2f3 SAS |
2387 | struct dummy *dum; |
2388 | ||
2389 | dum = *((void **)dev_get_platdata(hcd->self.controller)); | |
14fce33a | 2390 | hcd->self.sg_tablesize = ~0; |
cdfcbd2c | 2391 | if (usb_hcd_is_primary_hcd(hcd)) { |
b100a2f3 SAS |
2392 | dum->hs_hcd = hcd_to_dummy_hcd(hcd); |
2393 | dum->hs_hcd->dum = dum; | |
1cd8fd28 TB |
2394 | /* |
2395 | * Mark the first roothub as being USB 2.0. | |
2396 | * The USB 3.0 roothub will be registered later by | |
2397 | * dummy_hcd_probe() | |
2398 | */ | |
cdfcbd2c TB |
2399 | hcd->speed = HCD_USB2; |
2400 | hcd->self.root_hub->speed = USB_SPEED_HIGH; | |
1cd8fd28 | 2401 | } else { |
b100a2f3 SAS |
2402 | dum->ss_hcd = hcd_to_dummy_hcd(hcd); |
2403 | dum->ss_hcd->dum = dum; | |
1cd8fd28 TB |
2404 | hcd->speed = HCD_USB3; |
2405 | hcd->self.root_hub->speed = USB_SPEED_SUPER; | |
cdfcbd2c TB |
2406 | } |
2407 | return 0; | |
2408 | } | |
2409 | ||
1cd8fd28 | 2410 | /* Change a group of bulk endpoints to support multiple stream IDs */ |
d81f3e4f | 2411 | static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev, |
1cd8fd28 TB |
2412 | struct usb_host_endpoint **eps, unsigned int num_eps, |
2413 | unsigned int num_streams, gfp_t mem_flags) | |
2414 | { | |
a54c979f SAS |
2415 | struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd); |
2416 | unsigned long flags; | |
2417 | int max_stream; | |
2418 | int ret_streams = num_streams; | |
2419 | unsigned int index; | |
2420 | unsigned int i; | |
2421 | ||
2422 | if (!num_eps) | |
2423 | return -EINVAL; | |
2424 | ||
2425 | spin_lock_irqsave(&dum_hcd->dum->lock, flags); | |
2426 | for (i = 0; i < num_eps; i++) { | |
2427 | index = dummy_get_ep_idx(&eps[i]->desc); | |
2428 | if ((1 << index) & dum_hcd->stream_en_ep) { | |
2429 | ret_streams = -EINVAL; | |
2430 | goto out; | |
2431 | } | |
2432 | max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp); | |
2433 | if (!max_stream) { | |
2434 | ret_streams = -EINVAL; | |
2435 | goto out; | |
2436 | } | |
2437 | if (max_stream < ret_streams) { | |
2438 | dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u " | |
2439 | "stream IDs.\n", | |
2440 | eps[i]->desc.bEndpointAddress, | |
2441 | max_stream); | |
2442 | ret_streams = max_stream; | |
2443 | } | |
2444 | } | |
2445 | ||
2446 | for (i = 0; i < num_eps; i++) { | |
2447 | index = dummy_get_ep_idx(&eps[i]->desc); | |
2448 | dum_hcd->stream_en_ep |= 1 << index; | |
2449 | set_max_streams_for_pipe(dum_hcd, | |
2450 | usb_endpoint_num(&eps[i]->desc), ret_streams); | |
2451 | } | |
2452 | out: | |
2453 | spin_unlock_irqrestore(&dum_hcd->dum->lock, flags); | |
2454 | return ret_streams; | |
1cd8fd28 TB |
2455 | } |
2456 | ||
2457 | /* Reverts a group of bulk endpoints back to not using stream IDs. */ | |
d81f3e4f | 2458 | static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev, |
1cd8fd28 TB |
2459 | struct usb_host_endpoint **eps, unsigned int num_eps, |
2460 | gfp_t mem_flags) | |
2461 | { | |
a54c979f SAS |
2462 | struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd); |
2463 | unsigned long flags; | |
2464 | int ret; | |
2465 | unsigned int index; | |
2466 | unsigned int i; | |
2467 | ||
2468 | spin_lock_irqsave(&dum_hcd->dum->lock, flags); | |
2469 | for (i = 0; i < num_eps; i++) { | |
2470 | index = dummy_get_ep_idx(&eps[i]->desc); | |
2471 | if (!((1 << index) & dum_hcd->stream_en_ep)) { | |
2472 | ret = -EINVAL; | |
2473 | goto out; | |
2474 | } | |
2475 | } | |
2476 | ||
2477 | for (i = 0; i < num_eps; i++) { | |
2478 | index = dummy_get_ep_idx(&eps[i]->desc); | |
2479 | dum_hcd->stream_en_ep &= ~(1 << index); | |
2480 | set_max_streams_for_pipe(dum_hcd, | |
2481 | usb_endpoint_num(&eps[i]->desc), 0); | |
2482 | } | |
2483 | ret = 0; | |
2484 | out: | |
2485 | spin_unlock_irqrestore(&dum_hcd->dum->lock, flags); | |
2486 | return ret; | |
1cd8fd28 TB |
2487 | } |
2488 | ||
2489 | static struct hc_driver dummy_hcd = { | |
1da177e4 LT |
2490 | .description = (char *) driver_name, |
2491 | .product_desc = "Dummy host controller", | |
cdfcbd2c | 2492 | .hcd_priv_size = sizeof(struct dummy_hcd), |
1da177e4 | 2493 | |
1cd8fd28 | 2494 | .flags = HCD_USB3 | HCD_SHARED, |
1da177e4 | 2495 | |
cdfcbd2c | 2496 | .reset = dummy_setup, |
1da177e4 LT |
2497 | .start = dummy_start, |
2498 | .stop = dummy_stop, | |
2499 | ||
18f2cbaa SAS |
2500 | .urb_enqueue = dummy_urb_enqueue, |
2501 | .urb_dequeue = dummy_urb_dequeue, | |
1da177e4 | 2502 | |
18f2cbaa | 2503 | .get_frame_number = dummy_h_get_frame, |
1da177e4 | 2504 | |
18f2cbaa SAS |
2505 | .hub_status_data = dummy_hub_status, |
2506 | .hub_control = dummy_hub_control, | |
0c0382e3 AS |
2507 | .bus_suspend = dummy_bus_suspend, |
2508 | .bus_resume = dummy_bus_resume, | |
1cd8fd28 TB |
2509 | |
2510 | .alloc_streams = dummy_alloc_streams, | |
2511 | .free_streams = dummy_free_streams, | |
1da177e4 LT |
2512 | }; |
2513 | ||
8364d6b0 | 2514 | static int dummy_hcd_probe(struct platform_device *pdev) |
1da177e4 | 2515 | { |
b100a2f3 | 2516 | struct dummy *dum; |
cdfcbd2c | 2517 | struct usb_hcd *hs_hcd; |
1cd8fd28 | 2518 | struct usb_hcd *ss_hcd; |
1da177e4 LT |
2519 | int retval; |
2520 | ||
8364d6b0 | 2521 | dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc); |
b100a2f3 | 2522 | dum = *((void **)dev_get_platdata(&pdev->dev)); |
1da177e4 | 2523 | |
1cd8fd28 TB |
2524 | if (!mod_data.is_super_speed) |
2525 | dummy_hcd.flags = HCD_USB2; | |
cdfcbd2c TB |
2526 | hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev)); |
2527 | if (!hs_hcd) | |
1da177e4 | 2528 | return -ENOMEM; |
cdfcbd2c | 2529 | hs_hcd->has_tt = 1; |
1da177e4 | 2530 | |
cdfcbd2c | 2531 | retval = usb_add_hcd(hs_hcd, 0, 0); |
1b68a4ca SAS |
2532 | if (retval) |
2533 | goto put_usb2_hcd; | |
1cd8fd28 TB |
2534 | |
2535 | if (mod_data.is_super_speed) { | |
2536 | ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev, | |
2537 | dev_name(&pdev->dev), hs_hcd); | |
2538 | if (!ss_hcd) { | |
2539 | retval = -ENOMEM; | |
2540 | goto dealloc_usb2_hcd; | |
2541 | } | |
2542 | ||
2543 | retval = usb_add_hcd(ss_hcd, 0, 0); | |
2544 | if (retval) | |
2545 | goto put_usb3_hcd; | |
2546 | } | |
2547 | return 0; | |
2548 | ||
2549 | put_usb3_hcd: | |
2550 | usb_put_hcd(ss_hcd); | |
2551 | dealloc_usb2_hcd: | |
1b68a4ca SAS |
2552 | usb_remove_hcd(hs_hcd); |
2553 | put_usb2_hcd: | |
1cd8fd28 | 2554 | usb_put_hcd(hs_hcd); |
b100a2f3 | 2555 | dum->hs_hcd = dum->ss_hcd = NULL; |
1da177e4 LT |
2556 | return retval; |
2557 | } | |
2558 | ||
cdfcbd2c | 2559 | static int dummy_hcd_remove(struct platform_device *pdev) |
1da177e4 | 2560 | { |
cdfcbd2c TB |
2561 | struct dummy *dum; |
2562 | ||
18f2cbaa | 2563 | dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum; |
1cd8fd28 TB |
2564 | |
2565 | if (dum->ss_hcd) { | |
2566 | usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd)); | |
2567 | usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd)); | |
2568 | } | |
2569 | ||
cdfcbd2c TB |
2570 | usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd)); |
2571 | usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd)); | |
1cd8fd28 | 2572 | |
b100a2f3 SAS |
2573 | dum->hs_hcd = NULL; |
2574 | dum->ss_hcd = NULL; | |
1da177e4 | 2575 | |
d9b76251 | 2576 | return 0; |
1da177e4 LT |
2577 | } |
2578 | ||
18f2cbaa | 2579 | static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state) |
391eca9d AS |
2580 | { |
2581 | struct usb_hcd *hcd; | |
cdfcbd2c | 2582 | struct dummy_hcd *dum_hcd; |
3cf0a22e | 2583 | int rc = 0; |
391eca9d | 2584 | |
18f2cbaa | 2585 | dev_dbg(&pdev->dev, "%s\n", __func__); |
391eca9d | 2586 | |
18f2cbaa | 2587 | hcd = platform_get_drvdata(pdev); |
cdfcbd2c TB |
2588 | dum_hcd = hcd_to_dummy_hcd(hcd); |
2589 | if (dum_hcd->rh_state == DUMMY_RH_RUNNING) { | |
3cf0a22e AS |
2590 | dev_warn(&pdev->dev, "Root hub isn't suspended!\n"); |
2591 | rc = -EBUSY; | |
2592 | } else | |
2593 | clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); | |
2594 | return rc; | |
391eca9d AS |
2595 | } |
2596 | ||
18f2cbaa | 2597 | static int dummy_hcd_resume(struct platform_device *pdev) |
391eca9d AS |
2598 | { |
2599 | struct usb_hcd *hcd; | |
2600 | ||
18f2cbaa | 2601 | dev_dbg(&pdev->dev, "%s\n", __func__); |
391eca9d | 2602 | |
18f2cbaa | 2603 | hcd = platform_get_drvdata(pdev); |
3cf0a22e | 2604 | set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); |
18f2cbaa | 2605 | usb_hcd_poll_rh_status(hcd); |
391eca9d AS |
2606 | return 0; |
2607 | } | |
2608 | ||
3ae5eaec | 2609 | static struct platform_driver dummy_hcd_driver = { |
d9b76251 AS |
2610 | .probe = dummy_hcd_probe, |
2611 | .remove = dummy_hcd_remove, | |
391eca9d AS |
2612 | .suspend = dummy_hcd_suspend, |
2613 | .resume = dummy_hcd_resume, | |
3ae5eaec RK |
2614 | .driver = { |
2615 | .name = (char *) driver_name, | |
2616 | .owner = THIS_MODULE, | |
2617 | }, | |
d9b76251 | 2618 | }; |
1da177e4 | 2619 | |
d9b76251 | 2620 | /*-------------------------------------------------------------------------*/ |
b100a2f3 | 2621 | #define MAX_NUM_UDC 2 |
b2113136 SAS |
2622 | static struct platform_device *the_udc_pdev[MAX_NUM_UDC]; |
2623 | static struct platform_device *the_hcd_pdev[MAX_NUM_UDC]; | |
1da177e4 | 2624 | |
18f2cbaa | 2625 | static int __init init(void) |
1da177e4 | 2626 | { |
a89a2cd3 | 2627 | int retval = -ENOMEM; |
c7a1db45 | 2628 | int i; |
b100a2f3 | 2629 | struct dummy *dum[MAX_NUM_UDC]; |
1da177e4 | 2630 | |
18f2cbaa | 2631 | if (usb_disabled()) |
1da177e4 | 2632 | return -ENODEV; |
d9b76251 | 2633 | |
7eca4c5a TB |
2634 | if (!mod_data.is_high_speed && mod_data.is_super_speed) |
2635 | return -EINVAL; | |
2636 | ||
c7a1db45 SAS |
2637 | if (mod_data.num < 1 || mod_data.num > MAX_NUM_UDC) { |
2638 | pr_err("Number of emulated UDC must be in range of 1…%d\n", | |
2639 | MAX_NUM_UDC); | |
2640 | return -EINVAL; | |
2641 | } | |
b100a2f3 | 2642 | |
c7a1db45 SAS |
2643 | for (i = 0; i < mod_data.num; i++) { |
2644 | the_hcd_pdev[i] = platform_device_alloc(driver_name, i); | |
2645 | if (!the_hcd_pdev[i]) { | |
2646 | i--; | |
2647 | while (i >= 0) | |
2648 | platform_device_put(the_hcd_pdev[i--]); | |
2649 | return retval; | |
2650 | } | |
2651 | } | |
2652 | for (i = 0; i < mod_data.num; i++) { | |
2653 | the_udc_pdev[i] = platform_device_alloc(gadget_name, i); | |
2654 | if (!the_udc_pdev[i]) { | |
2655 | i--; | |
2656 | while (i >= 0) | |
2657 | platform_device_put(the_udc_pdev[i--]); | |
2658 | goto err_alloc_udc; | |
2659 | } | |
2660 | } | |
b100a2f3 SAS |
2661 | for (i = 0; i < mod_data.num; i++) { |
2662 | dum[i] = kzalloc(sizeof(struct dummy), GFP_KERNEL); | |
481d3042 WY |
2663 | if (!dum[i]) { |
2664 | retval = -ENOMEM; | |
b100a2f3 | 2665 | goto err_add_pdata; |
481d3042 | 2666 | } |
b100a2f3 SAS |
2667 | retval = platform_device_add_data(the_hcd_pdev[i], &dum[i], |
2668 | sizeof(void *)); | |
2669 | if (retval) | |
2670 | goto err_add_pdata; | |
2671 | retval = platform_device_add_data(the_udc_pdev[i], &dum[i], | |
2672 | sizeof(void *)); | |
2673 | if (retval) | |
2674 | goto err_add_pdata; | |
2675 | } | |
d9b76251 | 2676 | |
a89a2cd3 AS |
2677 | retval = platform_driver_register(&dummy_hcd_driver); |
2678 | if (retval < 0) | |
b100a2f3 | 2679 | goto err_add_pdata; |
a89a2cd3 | 2680 | retval = platform_driver_register(&dummy_udc_driver); |
d9b76251 AS |
2681 | if (retval < 0) |
2682 | goto err_register_udc_driver; | |
2683 | ||
c7a1db45 SAS |
2684 | for (i = 0; i < mod_data.num; i++) { |
2685 | retval = platform_device_add(the_hcd_pdev[i]); | |
2686 | if (retval < 0) { | |
2687 | i--; | |
2688 | while (i >= 0) | |
2689 | platform_device_del(the_hcd_pdev[i--]); | |
2690 | goto err_add_hcd; | |
2691 | } | |
2692 | } | |
b100a2f3 SAS |
2693 | for (i = 0; i < mod_data.num; i++) { |
2694 | if (!dum[i]->hs_hcd || | |
2695 | (!dum[i]->ss_hcd && mod_data.is_super_speed)) { | |
2696 | /* | |
2697 | * The hcd was added successfully but its probe | |
2698 | * function failed for some reason. | |
2699 | */ | |
2700 | retval = -EINVAL; | |
2701 | goto err_add_udc; | |
2702 | } | |
865835fa | 2703 | } |
c7a1db45 | 2704 | |
c7a1db45 SAS |
2705 | for (i = 0; i < mod_data.num; i++) { |
2706 | retval = platform_device_add(the_udc_pdev[i]); | |
2707 | if (retval < 0) { | |
2708 | i--; | |
2709 | while (i >= 0) | |
2710 | platform_device_del(the_udc_pdev[i]); | |
2711 | goto err_add_udc; | |
2712 | } | |
2713 | } | |
2714 | ||
2715 | for (i = 0; i < mod_data.num; i++) { | |
2716 | if (!platform_get_drvdata(the_udc_pdev[i])) { | |
2717 | /* | |
2718 | * The udc was added successfully but its probe | |
2719 | * function failed for some reason. | |
2720 | */ | |
2721 | retval = -EINVAL; | |
2722 | goto err_probe_udc; | |
2723 | } | |
865835fa | 2724 | } |
d9b76251 AS |
2725 | return retval; |
2726 | ||
865835fa | 2727 | err_probe_udc: |
c7a1db45 SAS |
2728 | for (i = 0; i < mod_data.num; i++) |
2729 | platform_device_del(the_udc_pdev[i]); | |
a89a2cd3 | 2730 | err_add_udc: |
c7a1db45 SAS |
2731 | for (i = 0; i < mod_data.num; i++) |
2732 | platform_device_del(the_hcd_pdev[i]); | |
a89a2cd3 AS |
2733 | err_add_hcd: |
2734 | platform_driver_unregister(&dummy_udc_driver); | |
d9b76251 | 2735 | err_register_udc_driver: |
a89a2cd3 | 2736 | platform_driver_unregister(&dummy_hcd_driver); |
b100a2f3 SAS |
2737 | err_add_pdata: |
2738 | for (i = 0; i < mod_data.num; i++) | |
2739 | kfree(dum[i]); | |
c7a1db45 SAS |
2740 | for (i = 0; i < mod_data.num; i++) |
2741 | platform_device_put(the_udc_pdev[i]); | |
a89a2cd3 | 2742 | err_alloc_udc: |
c7a1db45 SAS |
2743 | for (i = 0; i < mod_data.num; i++) |
2744 | platform_device_put(the_hcd_pdev[i]); | |
1da177e4 LT |
2745 | return retval; |
2746 | } | |
18f2cbaa | 2747 | module_init(init); |
1da177e4 | 2748 | |
18f2cbaa | 2749 | static void __exit cleanup(void) |
1da177e4 | 2750 | { |
c7a1db45 SAS |
2751 | int i; |
2752 | ||
2753 | for (i = 0; i < mod_data.num; i++) { | |
b100a2f3 SAS |
2754 | struct dummy *dum; |
2755 | ||
2756 | dum = *((void **)dev_get_platdata(&the_udc_pdev[i]->dev)); | |
2757 | ||
c7a1db45 SAS |
2758 | platform_device_unregister(the_udc_pdev[i]); |
2759 | platform_device_unregister(the_hcd_pdev[i]); | |
b100a2f3 | 2760 | kfree(dum); |
c7a1db45 | 2761 | } |
a89a2cd3 AS |
2762 | platform_driver_unregister(&dummy_udc_driver); |
2763 | platform_driver_unregister(&dummy_hcd_driver); | |
1da177e4 | 2764 | } |
18f2cbaa | 2765 | module_exit(cleanup); |