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. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 | */ | |
23 | ||
24 | ||
25 | /* | |
26 | * This exposes a device side "USB gadget" API, driven by requests to a | |
27 | * Linux-USB host controller driver. USB traffic is simulated; there's | |
28 | * no need for USB hardware. Use this with two other drivers: | |
29 | * | |
30 | * - Gadget driver, responding to requests (slave); | |
31 | * - Host-side device driver, as already familiar in Linux. | |
32 | * | |
33 | * Having this all in one kernel can help some stages of development, | |
34 | * bypassing some hardware (and driver) issues. UML could help too. | |
35 | */ | |
36 | ||
37 | #define DEBUG | |
38 | ||
39 | #include <linux/config.h> | |
40 | #include <linux/module.h> | |
41 | #include <linux/kernel.h> | |
42 | #include <linux/delay.h> | |
43 | #include <linux/ioport.h> | |
44 | #include <linux/sched.h> | |
45 | #include <linux/slab.h> | |
46 | #include <linux/smp_lock.h> | |
47 | #include <linux/errno.h> | |
48 | #include <linux/init.h> | |
49 | #include <linux/timer.h> | |
50 | #include <linux/list.h> | |
51 | #include <linux/interrupt.h> | |
52 | #include <linux/version.h> | |
53 | ||
54 | #include <linux/usb.h> | |
55 | #include <linux/usb_gadget.h> | |
56 | ||
57 | #include <asm/byteorder.h> | |
58 | #include <asm/io.h> | |
59 | #include <asm/irq.h> | |
60 | #include <asm/system.h> | |
61 | #include <asm/unaligned.h> | |
62 | ||
63 | ||
64 | #include "../core/hcd.h" | |
65 | ||
66 | ||
67 | #define DRIVER_DESC "USB Host+Gadget Emulator" | |
68 | #define DRIVER_VERSION "17 Dec 2004" | |
69 | ||
70 | static const char driver_name [] = "dummy_hcd"; | |
71 | static const char driver_desc [] = "USB Host+Gadget Emulator"; | |
72 | ||
73 | static const char gadget_name [] = "dummy_udc"; | |
74 | ||
75 | MODULE_DESCRIPTION (DRIVER_DESC); | |
76 | MODULE_AUTHOR ("David Brownell"); | |
77 | MODULE_LICENSE ("GPL"); | |
78 | ||
79 | /*-------------------------------------------------------------------------*/ | |
80 | ||
81 | /* gadget side driver data structres */ | |
82 | struct dummy_ep { | |
83 | struct list_head queue; | |
84 | unsigned long last_io; /* jiffies timestamp */ | |
85 | struct usb_gadget *gadget; | |
86 | const struct usb_endpoint_descriptor *desc; | |
87 | struct usb_ep ep; | |
88 | unsigned halted : 1; | |
89 | unsigned already_seen : 1; | |
90 | unsigned setup_stage : 1; | |
91 | }; | |
92 | ||
93 | struct dummy_request { | |
94 | struct list_head queue; /* ep's requests */ | |
95 | struct usb_request req; | |
96 | }; | |
97 | ||
98 | static inline struct dummy_ep *usb_ep_to_dummy_ep (struct usb_ep *_ep) | |
99 | { | |
100 | return container_of (_ep, struct dummy_ep, ep); | |
101 | } | |
102 | ||
103 | static inline struct dummy_request *usb_request_to_dummy_request | |
104 | (struct usb_request *_req) | |
105 | { | |
106 | return container_of (_req, struct dummy_request, req); | |
107 | } | |
108 | ||
109 | /*-------------------------------------------------------------------------*/ | |
110 | ||
111 | /* | |
112 | * Every device has ep0 for control requests, plus up to 30 more endpoints, | |
113 | * in one of two types: | |
114 | * | |
115 | * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint | |
116 | * number can be changed. Names like "ep-a" are used for this type. | |
117 | * | |
118 | * - Fixed Function: in other cases. some characteristics may be mutable; | |
119 | * that'd be hardware-specific. Names like "ep12out-bulk" are used. | |
120 | * | |
121 | * Gadget drivers are responsible for not setting up conflicting endpoint | |
122 | * configurations, illegal or unsupported packet lengths, and so on. | |
123 | */ | |
124 | ||
125 | static const char ep0name [] = "ep0"; | |
126 | ||
127 | static const char *const ep_name [] = { | |
128 | ep0name, /* everyone has ep0 */ | |
129 | ||
130 | /* act like a net2280: high speed, six configurable endpoints */ | |
131 | "ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f", | |
132 | ||
133 | /* or like pxa250: fifteen fixed function endpoints */ | |
134 | "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int", | |
135 | "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int", | |
136 | "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso", | |
137 | "ep15in-int", | |
138 | ||
139 | /* or like sa1100: two fixed function endpoints */ | |
140 | "ep1out-bulk", "ep2in-bulk", | |
141 | }; | |
142 | #define DUMMY_ENDPOINTS (sizeof(ep_name)/sizeof(char *)) | |
143 | ||
d9b76251 AS |
144 | /*-------------------------------------------------------------------------*/ |
145 | ||
1da177e4 LT |
146 | #define FIFO_SIZE 64 |
147 | ||
148 | struct urbp { | |
149 | struct urb *urb; | |
150 | struct list_head urbp_list; | |
151 | }; | |
152 | ||
153 | struct dummy { | |
154 | spinlock_t lock; | |
155 | ||
156 | /* | |
157 | * SLAVE/GADGET side support | |
158 | */ | |
159 | struct dummy_ep ep [DUMMY_ENDPOINTS]; | |
160 | int address; | |
161 | struct usb_gadget gadget; | |
162 | struct usb_gadget_driver *driver; | |
163 | struct dummy_request fifo_req; | |
164 | u8 fifo_buf [FIFO_SIZE]; | |
165 | u16 devstatus; | |
f1c39fad AS |
166 | unsigned pullup:1; |
167 | unsigned active:1; | |
168 | unsigned old_active:1; | |
1da177e4 LT |
169 | |
170 | /* | |
171 | * MASTER/HOST side support | |
172 | */ | |
173 | struct timer_list timer; | |
174 | u32 port_status; | |
f1c39fad | 175 | u32 old_status; |
1da177e4 LT |
176 | unsigned resuming:1; |
177 | unsigned long re_timeout; | |
178 | ||
179 | struct usb_device *udev; | |
180 | struct list_head urbp_list; | |
181 | }; | |
182 | ||
183 | static inline struct dummy *hcd_to_dummy (struct usb_hcd *hcd) | |
184 | { | |
185 | return (struct dummy *) (hcd->hcd_priv); | |
186 | } | |
187 | ||
188 | static inline struct usb_hcd *dummy_to_hcd (struct dummy *dum) | |
189 | { | |
190 | return container_of((void *) dum, struct usb_hcd, hcd_priv); | |
191 | } | |
192 | ||
193 | static inline struct device *dummy_dev (struct dummy *dum) | |
194 | { | |
195 | return dummy_to_hcd(dum)->self.controller; | |
196 | } | |
197 | ||
d9b76251 AS |
198 | static inline struct device *udc_dev (struct dummy *dum) |
199 | { | |
200 | return dum->gadget.dev.parent; | |
201 | } | |
202 | ||
1da177e4 LT |
203 | static inline struct dummy *ep_to_dummy (struct dummy_ep *ep) |
204 | { | |
205 | return container_of (ep->gadget, struct dummy, gadget); | |
206 | } | |
207 | ||
208 | static inline struct dummy *gadget_to_dummy (struct usb_gadget *gadget) | |
209 | { | |
210 | return container_of (gadget, struct dummy, gadget); | |
211 | } | |
212 | ||
213 | static inline struct dummy *gadget_dev_to_dummy (struct device *dev) | |
214 | { | |
215 | return container_of (dev, struct dummy, gadget.dev); | |
216 | } | |
217 | ||
218 | static struct dummy *the_controller; | |
219 | ||
220 | /*-------------------------------------------------------------------------*/ | |
221 | ||
f1c39fad AS |
222 | /* SLAVE/GADGET SIDE UTILITY ROUTINES */ |
223 | ||
224 | /* called with spinlock held */ | |
225 | static void nuke (struct dummy *dum, struct dummy_ep *ep) | |
226 | { | |
227 | while (!list_empty (&ep->queue)) { | |
228 | struct dummy_request *req; | |
229 | ||
230 | req = list_entry (ep->queue.next, struct dummy_request, queue); | |
231 | list_del_init (&req->queue); | |
232 | req->req.status = -ESHUTDOWN; | |
233 | ||
234 | spin_unlock (&dum->lock); | |
235 | req->req.complete (&ep->ep, &req->req); | |
236 | spin_lock (&dum->lock); | |
237 | } | |
238 | } | |
239 | ||
240 | /* caller must hold lock */ | |
241 | static void | |
242 | stop_activity (struct dummy *dum) | |
243 | { | |
244 | struct dummy_ep *ep; | |
245 | ||
246 | /* prevent any more requests */ | |
247 | dum->address = 0; | |
248 | ||
249 | /* The timer is left running so that outstanding URBs can fail */ | |
250 | ||
251 | /* nuke any pending requests first, so driver i/o is quiesced */ | |
252 | list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list) | |
253 | nuke (dum, ep); | |
254 | ||
255 | /* driver now does any non-usb quiescing necessary */ | |
256 | } | |
257 | ||
258 | /* caller must hold lock */ | |
259 | static void | |
260 | set_link_state (struct dummy *dum) | |
261 | { | |
262 | dum->active = 0; | |
263 | if ((dum->port_status & USB_PORT_STAT_POWER) == 0) | |
264 | dum->port_status = 0; | |
265 | else if (!dum->pullup) { | |
266 | dum->port_status &= ~(USB_PORT_STAT_CONNECTION | | |
267 | USB_PORT_STAT_ENABLE | | |
268 | USB_PORT_STAT_LOW_SPEED | | |
269 | USB_PORT_STAT_HIGH_SPEED | | |
270 | USB_PORT_STAT_SUSPEND); | |
271 | if ((dum->old_status & USB_PORT_STAT_CONNECTION) != 0) | |
272 | dum->port_status |= (USB_PORT_STAT_C_CONNECTION << 16); | |
273 | } else { | |
274 | dum->port_status |= USB_PORT_STAT_CONNECTION; | |
275 | if ((dum->old_status & USB_PORT_STAT_CONNECTION) == 0) | |
276 | dum->port_status |= (USB_PORT_STAT_C_CONNECTION << 16); | |
277 | if ((dum->port_status & USB_PORT_STAT_ENABLE) == 0) | |
278 | dum->port_status &= ~USB_PORT_STAT_SUSPEND; | |
279 | else if ((dum->port_status & USB_PORT_STAT_SUSPEND) == 0) | |
280 | dum->active = 1; | |
281 | } | |
282 | ||
283 | if ((dum->port_status & USB_PORT_STAT_ENABLE) == 0 || dum->active) | |
284 | dum->resuming = 0; | |
285 | ||
286 | if ((dum->port_status & USB_PORT_STAT_CONNECTION) == 0 || | |
287 | (dum->port_status & USB_PORT_STAT_RESET) != 0) { | |
288 | if ((dum->old_status & USB_PORT_STAT_CONNECTION) != 0 && | |
289 | (dum->old_status & USB_PORT_STAT_RESET) == 0 && | |
290 | dum->driver) { | |
291 | stop_activity (dum); | |
292 | spin_unlock (&dum->lock); | |
293 | dum->driver->disconnect (&dum->gadget); | |
294 | spin_lock (&dum->lock); | |
295 | } | |
296 | } else if (dum->active != dum->old_active) { | |
297 | if (dum->old_active && dum->driver->suspend) { | |
298 | spin_unlock (&dum->lock); | |
299 | dum->driver->suspend (&dum->gadget); | |
300 | spin_lock (&dum->lock); | |
301 | } else if (!dum->old_active && dum->driver->resume) { | |
302 | spin_unlock (&dum->lock); | |
303 | dum->driver->resume (&dum->gadget); | |
304 | spin_lock (&dum->lock); | |
305 | } | |
306 | } | |
307 | ||
308 | dum->old_status = dum->port_status; | |
309 | dum->old_active = dum->active; | |
310 | } | |
311 | ||
312 | /*-------------------------------------------------------------------------*/ | |
313 | ||
1da177e4 LT |
314 | /* SLAVE/GADGET SIDE DRIVER |
315 | * | |
316 | * This only tracks gadget state. All the work is done when the host | |
317 | * side tries some (emulated) i/o operation. Real device controller | |
318 | * drivers would do real i/o using dma, fifos, irqs, timers, etc. | |
319 | */ | |
320 | ||
321 | #define is_enabled(dum) \ | |
322 | (dum->port_status & USB_PORT_STAT_ENABLE) | |
323 | ||
324 | static int | |
325 | dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc) | |
326 | { | |
327 | struct dummy *dum; | |
328 | struct dummy_ep *ep; | |
329 | unsigned max; | |
330 | int retval; | |
331 | ||
332 | ep = usb_ep_to_dummy_ep (_ep); | |
333 | if (!_ep || !desc || ep->desc || _ep->name == ep0name | |
334 | || desc->bDescriptorType != USB_DT_ENDPOINT) | |
335 | return -EINVAL; | |
336 | dum = ep_to_dummy (ep); | |
337 | if (!dum->driver || !is_enabled (dum)) | |
338 | return -ESHUTDOWN; | |
339 | max = le16_to_cpu(desc->wMaxPacketSize) & 0x3ff; | |
340 | ||
341 | /* drivers must not request bad settings, since lower levels | |
342 | * (hardware or its drivers) may not check. some endpoints | |
343 | * can't do iso, many have maxpacket limitations, etc. | |
344 | * | |
345 | * since this "hardware" driver is here to help debugging, we | |
346 | * have some extra sanity checks. (there could be more though, | |
347 | * especially for "ep9out" style fixed function ones.) | |
348 | */ | |
349 | retval = -EINVAL; | |
350 | switch (desc->bmAttributes & 0x03) { | |
351 | case USB_ENDPOINT_XFER_BULK: | |
352 | if (strstr (ep->ep.name, "-iso") | |
353 | || strstr (ep->ep.name, "-int")) { | |
354 | goto done; | |
355 | } | |
356 | switch (dum->gadget.speed) { | |
357 | case USB_SPEED_HIGH: | |
358 | if (max == 512) | |
359 | break; | |
360 | /* conserve return statements */ | |
361 | default: | |
362 | switch (max) { | |
363 | case 8: case 16: case 32: case 64: | |
364 | /* we'll fake any legal size */ | |
365 | break; | |
366 | default: | |
367 | case USB_SPEED_LOW: | |
368 | goto done; | |
369 | } | |
370 | } | |
371 | break; | |
372 | case USB_ENDPOINT_XFER_INT: | |
373 | if (strstr (ep->ep.name, "-iso")) /* bulk is ok */ | |
374 | goto done; | |
375 | /* real hardware might not handle all packet sizes */ | |
376 | switch (dum->gadget.speed) { | |
377 | case USB_SPEED_HIGH: | |
378 | if (max <= 1024) | |
379 | break; | |
380 | /* save a return statement */ | |
381 | case USB_SPEED_FULL: | |
382 | if (max <= 64) | |
383 | break; | |
384 | /* save a return statement */ | |
385 | default: | |
386 | if (max <= 8) | |
387 | break; | |
388 | goto done; | |
389 | } | |
390 | break; | |
391 | case USB_ENDPOINT_XFER_ISOC: | |
392 | if (strstr (ep->ep.name, "-bulk") | |
393 | || strstr (ep->ep.name, "-int")) | |
394 | goto done; | |
395 | /* real hardware might not handle all packet sizes */ | |
396 | switch (dum->gadget.speed) { | |
397 | case USB_SPEED_HIGH: | |
398 | if (max <= 1024) | |
399 | break; | |
400 | /* save a return statement */ | |
401 | case USB_SPEED_FULL: | |
402 | if (max <= 1023) | |
403 | break; | |
404 | /* save a return statement */ | |
405 | default: | |
406 | goto done; | |
407 | } | |
408 | break; | |
409 | default: | |
410 | /* few chips support control except on ep0 */ | |
411 | goto done; | |
412 | } | |
413 | ||
414 | _ep->maxpacket = max; | |
415 | ep->desc = desc; | |
416 | ||
d9b76251 | 417 | dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n", |
1da177e4 LT |
418 | _ep->name, |
419 | desc->bEndpointAddress & 0x0f, | |
420 | (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out", | |
421 | ({ char *val; | |
422 | switch (desc->bmAttributes & 0x03) { | |
423 | case USB_ENDPOINT_XFER_BULK: val = "bulk"; break; | |
424 | case USB_ENDPOINT_XFER_ISOC: val = "iso"; break; | |
425 | case USB_ENDPOINT_XFER_INT: val = "intr"; break; | |
426 | default: val = "ctrl"; break; | |
427 | }; val; }), | |
428 | max); | |
429 | ||
430 | /* at this point real hardware should be NAKing transfers | |
431 | * to that endpoint, until a buffer is queued to it. | |
432 | */ | |
433 | retval = 0; | |
434 | done: | |
435 | return retval; | |
436 | } | |
437 | ||
1da177e4 LT |
438 | static int dummy_disable (struct usb_ep *_ep) |
439 | { | |
440 | struct dummy_ep *ep; | |
441 | struct dummy *dum; | |
442 | unsigned long flags; | |
443 | int retval; | |
444 | ||
445 | ep = usb_ep_to_dummy_ep (_ep); | |
446 | if (!_ep || !ep->desc || _ep->name == ep0name) | |
447 | return -EINVAL; | |
448 | dum = ep_to_dummy (ep); | |
449 | ||
450 | spin_lock_irqsave (&dum->lock, flags); | |
451 | ep->desc = NULL; | |
452 | retval = 0; | |
453 | nuke (dum, ep); | |
454 | spin_unlock_irqrestore (&dum->lock, flags); | |
455 | ||
d9b76251 | 456 | dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name); |
1da177e4 LT |
457 | return retval; |
458 | } | |
459 | ||
460 | static struct usb_request * | |
461 | dummy_alloc_request (struct usb_ep *_ep, int mem_flags) | |
462 | { | |
463 | struct dummy_ep *ep; | |
464 | struct dummy_request *req; | |
465 | ||
466 | if (!_ep) | |
467 | return NULL; | |
468 | ep = usb_ep_to_dummy_ep (_ep); | |
469 | ||
470 | req = kmalloc (sizeof *req, mem_flags); | |
471 | if (!req) | |
472 | return NULL; | |
473 | memset (req, 0, sizeof *req); | |
474 | INIT_LIST_HEAD (&req->queue); | |
475 | return &req->req; | |
476 | } | |
477 | ||
478 | static void | |
479 | dummy_free_request (struct usb_ep *_ep, struct usb_request *_req) | |
480 | { | |
481 | struct dummy_ep *ep; | |
482 | struct dummy_request *req; | |
483 | ||
484 | ep = usb_ep_to_dummy_ep (_ep); | |
485 | if (!ep || !_req || (!ep->desc && _ep->name != ep0name)) | |
486 | return; | |
487 | ||
488 | req = usb_request_to_dummy_request (_req); | |
489 | WARN_ON (!list_empty (&req->queue)); | |
490 | kfree (req); | |
491 | } | |
492 | ||
493 | static void * | |
494 | dummy_alloc_buffer ( | |
495 | struct usb_ep *_ep, | |
496 | unsigned bytes, | |
497 | dma_addr_t *dma, | |
498 | int mem_flags | |
499 | ) { | |
500 | char *retval; | |
501 | struct dummy_ep *ep; | |
502 | struct dummy *dum; | |
503 | ||
504 | ep = usb_ep_to_dummy_ep (_ep); | |
505 | dum = ep_to_dummy (ep); | |
506 | ||
507 | if (!dum->driver) | |
508 | return NULL; | |
509 | retval = kmalloc (bytes, mem_flags); | |
510 | *dma = (dma_addr_t) retval; | |
511 | return retval; | |
512 | } | |
513 | ||
514 | static void | |
515 | dummy_free_buffer ( | |
516 | struct usb_ep *_ep, | |
517 | void *buf, | |
518 | dma_addr_t dma, | |
519 | unsigned bytes | |
520 | ) { | |
521 | if (bytes) | |
522 | kfree (buf); | |
523 | } | |
524 | ||
525 | static void | |
526 | fifo_complete (struct usb_ep *ep, struct usb_request *req) | |
527 | { | |
528 | } | |
529 | ||
530 | static int | |
531 | dummy_queue (struct usb_ep *_ep, struct usb_request *_req, int mem_flags) | |
532 | { | |
533 | struct dummy_ep *ep; | |
534 | struct dummy_request *req; | |
535 | struct dummy *dum; | |
536 | unsigned long flags; | |
537 | ||
538 | req = usb_request_to_dummy_request (_req); | |
539 | if (!_req || !list_empty (&req->queue) || !_req->complete) | |
540 | return -EINVAL; | |
541 | ||
542 | ep = usb_ep_to_dummy_ep (_ep); | |
543 | if (!_ep || (!ep->desc && _ep->name != ep0name)) | |
544 | return -EINVAL; | |
545 | ||
546 | dum = ep_to_dummy (ep); | |
547 | if (!dum->driver || !is_enabled (dum)) | |
548 | return -ESHUTDOWN; | |
549 | ||
550 | #if 0 | |
d9b76251 | 551 | dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n", |
1da177e4 LT |
552 | ep, _req, _ep->name, _req->length, _req->buf); |
553 | #endif | |
554 | ||
555 | _req->status = -EINPROGRESS; | |
556 | _req->actual = 0; | |
557 | spin_lock_irqsave (&dum->lock, flags); | |
558 | ||
559 | /* implement an emulated single-request FIFO */ | |
560 | if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) && | |
561 | list_empty (&dum->fifo_req.queue) && | |
562 | list_empty (&ep->queue) && | |
563 | _req->length <= FIFO_SIZE) { | |
564 | req = &dum->fifo_req; | |
565 | req->req = *_req; | |
566 | req->req.buf = dum->fifo_buf; | |
567 | memcpy (dum->fifo_buf, _req->buf, _req->length); | |
568 | req->req.context = dum; | |
569 | req->req.complete = fifo_complete; | |
570 | ||
571 | spin_unlock (&dum->lock); | |
572 | _req->actual = _req->length; | |
573 | _req->status = 0; | |
574 | _req->complete (_ep, _req); | |
575 | spin_lock (&dum->lock); | |
576 | } | |
577 | list_add_tail (&req->queue, &ep->queue); | |
578 | spin_unlock_irqrestore (&dum->lock, flags); | |
579 | ||
580 | /* real hardware would likely enable transfers here, in case | |
581 | * it'd been left NAKing. | |
582 | */ | |
583 | return 0; | |
584 | } | |
585 | ||
586 | static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req) | |
587 | { | |
588 | struct dummy_ep *ep; | |
589 | struct dummy *dum; | |
590 | int retval = -EINVAL; | |
591 | unsigned long flags; | |
592 | struct dummy_request *req = NULL; | |
593 | ||
594 | if (!_ep || !_req) | |
595 | return retval; | |
596 | ep = usb_ep_to_dummy_ep (_ep); | |
597 | dum = ep_to_dummy (ep); | |
598 | ||
599 | if (!dum->driver) | |
600 | return -ESHUTDOWN; | |
601 | ||
602 | spin_lock_irqsave (&dum->lock, flags); | |
603 | list_for_each_entry (req, &ep->queue, queue) { | |
604 | if (&req->req == _req) { | |
605 | list_del_init (&req->queue); | |
606 | _req->status = -ECONNRESET; | |
607 | retval = 0; | |
608 | break; | |
609 | } | |
610 | } | |
611 | spin_unlock_irqrestore (&dum->lock, flags); | |
612 | ||
613 | if (retval == 0) { | |
d9b76251 | 614 | dev_dbg (udc_dev(dum), |
1da177e4 LT |
615 | "dequeued req %p from %s, len %d buf %p\n", |
616 | req, _ep->name, _req->length, _req->buf); | |
617 | _req->complete (_ep, _req); | |
618 | } | |
619 | return retval; | |
620 | } | |
621 | ||
622 | static int | |
623 | dummy_set_halt (struct usb_ep *_ep, int value) | |
624 | { | |
625 | struct dummy_ep *ep; | |
626 | struct dummy *dum; | |
627 | ||
628 | if (!_ep) | |
629 | return -EINVAL; | |
630 | ep = usb_ep_to_dummy_ep (_ep); | |
631 | dum = ep_to_dummy (ep); | |
632 | if (!dum->driver) | |
633 | return -ESHUTDOWN; | |
634 | if (!value) | |
635 | ep->halted = 0; | |
636 | else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) && | |
637 | !list_empty (&ep->queue)) | |
638 | return -EAGAIN; | |
639 | else | |
640 | ep->halted = 1; | |
641 | /* FIXME clear emulated data toggle too */ | |
642 | return 0; | |
643 | } | |
644 | ||
645 | static const struct usb_ep_ops dummy_ep_ops = { | |
646 | .enable = dummy_enable, | |
647 | .disable = dummy_disable, | |
648 | ||
649 | .alloc_request = dummy_alloc_request, | |
650 | .free_request = dummy_free_request, | |
651 | ||
652 | .alloc_buffer = dummy_alloc_buffer, | |
653 | .free_buffer = dummy_free_buffer, | |
654 | /* map, unmap, ... eventually hook the "generic" dma calls */ | |
655 | ||
656 | .queue = dummy_queue, | |
657 | .dequeue = dummy_dequeue, | |
658 | ||
659 | .set_halt = dummy_set_halt, | |
660 | }; | |
661 | ||
662 | /*-------------------------------------------------------------------------*/ | |
663 | ||
664 | /* there are both host and device side versions of this call ... */ | |
665 | static int dummy_g_get_frame (struct usb_gadget *_gadget) | |
666 | { | |
667 | struct timeval tv; | |
668 | ||
669 | do_gettimeofday (&tv); | |
670 | return tv.tv_usec / 1000; | |
671 | } | |
672 | ||
673 | static int dummy_wakeup (struct usb_gadget *_gadget) | |
674 | { | |
675 | struct dummy *dum; | |
676 | ||
677 | dum = gadget_to_dummy (_gadget); | |
c2db8b5e | 678 | if (!(dum->port_status & USB_PORT_STAT_SUSPEND) |
5742b0c9 AS |
679 | || !(dum->devstatus & |
680 | ( (1 << USB_DEVICE_B_HNP_ENABLE) | |
681 | | (1 << USB_DEVICE_REMOTE_WAKEUP)))) | |
1da177e4 LT |
682 | return -EINVAL; |
683 | ||
684 | /* hub notices our request, issues downstream resume, etc */ | |
685 | dum->resuming = 1; | |
f1c39fad | 686 | dum->re_timeout = jiffies + msecs_to_jiffies(20); |
685eb93f | 687 | mod_timer (&dummy_to_hcd (dum)->rh_timer, dum->re_timeout); |
1da177e4 LT |
688 | return 0; |
689 | } | |
690 | ||
691 | static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value) | |
692 | { | |
693 | struct dummy *dum; | |
694 | ||
695 | dum = gadget_to_dummy (_gadget); | |
696 | if (value) | |
697 | dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED); | |
698 | else | |
699 | dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED); | |
700 | return 0; | |
701 | } | |
702 | ||
f1c39fad AS |
703 | static int dummy_pullup (struct usb_gadget *_gadget, int value) |
704 | { | |
705 | struct dummy *dum; | |
706 | unsigned long flags; | |
707 | ||
708 | dum = gadget_to_dummy (_gadget); | |
709 | spin_lock_irqsave (&dum->lock, flags); | |
710 | dum->pullup = (value != 0); | |
711 | set_link_state (dum); | |
712 | spin_unlock_irqrestore (&dum->lock, flags); | |
685eb93f AS |
713 | |
714 | usb_hcd_poll_rh_status (dummy_to_hcd (dum)); | |
f1c39fad AS |
715 | return 0; |
716 | } | |
717 | ||
1da177e4 LT |
718 | static const struct usb_gadget_ops dummy_ops = { |
719 | .get_frame = dummy_g_get_frame, | |
720 | .wakeup = dummy_wakeup, | |
721 | .set_selfpowered = dummy_set_selfpowered, | |
f1c39fad | 722 | .pullup = dummy_pullup, |
1da177e4 LT |
723 | }; |
724 | ||
725 | /*-------------------------------------------------------------------------*/ | |
726 | ||
727 | /* "function" sysfs attribute */ | |
728 | static ssize_t | |
10523b3b | 729 | show_function (struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
730 | { |
731 | struct dummy *dum = gadget_dev_to_dummy (dev); | |
732 | ||
733 | if (!dum->driver || !dum->driver->function) | |
734 | return 0; | |
735 | return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function); | |
736 | } | |
cc095b0b | 737 | static DEVICE_ATTR (function, S_IRUGO, show_function, NULL); |
1da177e4 LT |
738 | |
739 | /*-------------------------------------------------------------------------*/ | |
740 | ||
741 | /* | |
742 | * Driver registration/unregistration. | |
743 | * | |
744 | * This is basically hardware-specific; there's usually only one real USB | |
745 | * device (not host) controller since that's how USB devices are intended | |
746 | * to work. So most implementations of these api calls will rely on the | |
747 | * fact that only one driver will ever bind to the hardware. But curious | |
748 | * hardware can be built with discrete components, so the gadget API doesn't | |
749 | * require that assumption. | |
750 | * | |
751 | * For this emulator, it might be convenient to create a usb slave device | |
752 | * for each driver that registers: just add to a big root hub. | |
753 | */ | |
754 | ||
1da177e4 LT |
755 | int |
756 | usb_gadget_register_driver (struct usb_gadget_driver *driver) | |
757 | { | |
758 | struct dummy *dum = the_controller; | |
759 | int retval, i; | |
760 | ||
761 | if (!dum) | |
762 | return -EINVAL; | |
763 | if (dum->driver) | |
764 | return -EBUSY; | |
765 | if (!driver->bind || !driver->unbind || !driver->setup | |
766 | || driver->speed == USB_SPEED_UNKNOWN) | |
767 | return -EINVAL; | |
768 | ||
769 | /* | |
770 | * SLAVE side init ... the layer above hardware, which | |
771 | * can't enumerate without help from the driver we're binding. | |
772 | */ | |
5742b0c9 | 773 | |
1da177e4 | 774 | dum->devstatus = 0; |
1da177e4 LT |
775 | |
776 | INIT_LIST_HEAD (&dum->gadget.ep_list); | |
777 | for (i = 0; i < DUMMY_ENDPOINTS; i++) { | |
778 | struct dummy_ep *ep = &dum->ep [i]; | |
779 | ||
780 | if (!ep_name [i]) | |
781 | break; | |
782 | ep->ep.name = ep_name [i]; | |
783 | ep->ep.ops = &dummy_ep_ops; | |
784 | list_add_tail (&ep->ep.ep_list, &dum->gadget.ep_list); | |
785 | ep->halted = ep->already_seen = ep->setup_stage = 0; | |
786 | ep->ep.maxpacket = ~0; | |
787 | ep->last_io = jiffies; | |
788 | ep->gadget = &dum->gadget; | |
789 | ep->desc = NULL; | |
790 | INIT_LIST_HEAD (&ep->queue); | |
791 | } | |
792 | ||
793 | dum->gadget.ep0 = &dum->ep [0].ep; | |
794 | dum->ep [0].ep.maxpacket = 64; | |
795 | list_del_init (&dum->ep [0].ep.ep_list); | |
796 | INIT_LIST_HEAD(&dum->fifo_req.queue); | |
797 | ||
798 | dum->driver = driver; | |
799 | dum->gadget.dev.driver = &driver->driver; | |
d9b76251 | 800 | dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n", |
1da177e4 LT |
801 | driver->driver.name); |
802 | if ((retval = driver->bind (&dum->gadget)) != 0) { | |
803 | dum->driver = NULL; | |
804 | dum->gadget.dev.driver = NULL; | |
805 | return retval; | |
806 | } | |
807 | ||
1da177e4 LT |
808 | driver->driver.bus = dum->gadget.dev.parent->bus; |
809 | driver_register (&driver->driver); | |
1da177e4 LT |
810 | device_bind_driver (&dum->gadget.dev); |
811 | ||
812 | /* khubd will enumerate this in a while */ | |
f1c39fad AS |
813 | spin_lock_irq (&dum->lock); |
814 | dum->pullup = 1; | |
815 | set_link_state (dum); | |
816 | spin_unlock_irq (&dum->lock); | |
685eb93f AS |
817 | |
818 | usb_hcd_poll_rh_status (dummy_to_hcd (dum)); | |
1da177e4 LT |
819 | return 0; |
820 | } | |
821 | EXPORT_SYMBOL (usb_gadget_register_driver); | |
822 | ||
1da177e4 LT |
823 | int |
824 | usb_gadget_unregister_driver (struct usb_gadget_driver *driver) | |
825 | { | |
826 | struct dummy *dum = the_controller; | |
827 | unsigned long flags; | |
828 | ||
829 | if (!dum) | |
830 | return -ENODEV; | |
831 | if (!driver || driver != dum->driver) | |
832 | return -EINVAL; | |
833 | ||
d9b76251 | 834 | dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n", |
1da177e4 LT |
835 | driver->driver.name); |
836 | ||
837 | spin_lock_irqsave (&dum->lock, flags); | |
f1c39fad AS |
838 | dum->pullup = 0; |
839 | set_link_state (dum); | |
1da177e4 LT |
840 | spin_unlock_irqrestore (&dum->lock, flags); |
841 | ||
842 | driver->unbind (&dum->gadget); | |
843 | dum->driver = NULL; | |
844 | ||
845 | device_release_driver (&dum->gadget.dev); | |
1da177e4 LT |
846 | driver_unregister (&driver->driver); |
847 | ||
f1c39fad AS |
848 | spin_lock_irqsave (&dum->lock, flags); |
849 | dum->pullup = 0; | |
850 | set_link_state (dum); | |
851 | spin_unlock_irqrestore (&dum->lock, flags); | |
852 | ||
685eb93f | 853 | usb_hcd_poll_rh_status (dummy_to_hcd (dum)); |
1da177e4 LT |
854 | return 0; |
855 | } | |
856 | EXPORT_SYMBOL (usb_gadget_unregister_driver); | |
857 | ||
858 | #undef is_enabled | |
859 | ||
cc095b0b AS |
860 | /* just declare this in any driver that really need it */ |
861 | extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode); | |
862 | ||
1da177e4 LT |
863 | int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode) |
864 | { | |
865 | return -ENOSYS; | |
866 | } | |
867 | EXPORT_SYMBOL (net2280_set_fifo_mode); | |
868 | ||
d9b76251 AS |
869 | |
870 | /* The gadget structure is stored inside the hcd structure and will be | |
871 | * released along with it. */ | |
872 | static void | |
873 | dummy_gadget_release (struct device *dev) | |
874 | { | |
875 | #if 0 /* usb_bus_put isn't EXPORTed! */ | |
876 | struct dummy *dum = gadget_dev_to_dummy (dev); | |
877 | ||
878 | usb_bus_put (&dummy_to_hcd (dum)->self); | |
879 | #endif | |
880 | } | |
881 | ||
882 | static int dummy_udc_probe (struct device *dev) | |
883 | { | |
884 | struct dummy *dum = the_controller; | |
885 | int rc; | |
886 | ||
887 | dum->gadget.name = gadget_name; | |
888 | dum->gadget.ops = &dummy_ops; | |
889 | dum->gadget.is_dualspeed = 1; | |
890 | ||
891 | /* maybe claim OTG support, though we won't complete HNP */ | |
892 | dum->gadget.is_otg = (dummy_to_hcd(dum)->self.otg_port != 0); | |
893 | ||
894 | strcpy (dum->gadget.dev.bus_id, "gadget"); | |
895 | dum->gadget.dev.parent = dev; | |
896 | dum->gadget.dev.release = dummy_gadget_release; | |
897 | rc = device_register (&dum->gadget.dev); | |
898 | if (rc < 0) | |
899 | return rc; | |
900 | ||
901 | #if 0 /* usb_bus_get isn't EXPORTed! */ | |
902 | usb_bus_get (&dummy_to_hcd (dum)->self); | |
903 | #endif | |
904 | ||
905 | dev_set_drvdata (dev, dum); | |
906 | device_create_file (&dum->gadget.dev, &dev_attr_function); | |
907 | return rc; | |
908 | } | |
909 | ||
910 | static int dummy_udc_remove (struct device *dev) | |
911 | { | |
912 | struct dummy *dum = dev_get_drvdata (dev); | |
913 | ||
914 | dev_set_drvdata (dev, NULL); | |
915 | device_remove_file (&dum->gadget.dev, &dev_attr_function); | |
916 | device_unregister (&dum->gadget.dev); | |
917 | return 0; | |
918 | } | |
919 | ||
920 | static struct device_driver dummy_udc_driver = { | |
921 | .name = (char *) gadget_name, | |
922 | .bus = &platform_bus_type, | |
923 | .probe = dummy_udc_probe, | |
924 | .remove = dummy_udc_remove, | |
925 | }; | |
926 | ||
1da177e4 LT |
927 | /*-------------------------------------------------------------------------*/ |
928 | ||
929 | /* MASTER/HOST SIDE DRIVER | |
930 | * | |
931 | * this uses the hcd framework to hook up to host side drivers. | |
932 | * its root hub will only have one device, otherwise it acts like | |
933 | * a normal host controller. | |
934 | * | |
935 | * when urbs are queued, they're just stuck on a list that we | |
936 | * scan in a timer callback. that callback connects writes from | |
937 | * the host with reads from the device, and so on, based on the | |
938 | * usb 2.0 rules. | |
939 | */ | |
940 | ||
941 | static int dummy_urb_enqueue ( | |
942 | struct usb_hcd *hcd, | |
943 | struct usb_host_endpoint *ep, | |
944 | struct urb *urb, | |
945 | int mem_flags | |
946 | ) { | |
947 | struct dummy *dum; | |
948 | struct urbp *urbp; | |
949 | unsigned long flags; | |
950 | ||
951 | if (!urb->transfer_buffer && urb->transfer_buffer_length) | |
952 | return -EINVAL; | |
953 | ||
954 | urbp = kmalloc (sizeof *urbp, mem_flags); | |
955 | if (!urbp) | |
956 | return -ENOMEM; | |
957 | urbp->urb = urb; | |
958 | ||
959 | dum = hcd_to_dummy (hcd); | |
960 | spin_lock_irqsave (&dum->lock, flags); | |
961 | ||
962 | if (!dum->udev) { | |
963 | dum->udev = urb->dev; | |
964 | usb_get_dev (dum->udev); | |
965 | } else if (unlikely (dum->udev != urb->dev)) | |
966 | dev_err (dummy_dev(dum), "usb_device address has changed!\n"); | |
967 | ||
968 | list_add_tail (&urbp->urbp_list, &dum->urbp_list); | |
969 | urb->hcpriv = urbp; | |
970 | if (usb_pipetype (urb->pipe) == PIPE_CONTROL) | |
971 | urb->error_count = 1; /* mark as a new urb */ | |
972 | ||
973 | /* kick the scheduler, it'll do the rest */ | |
974 | if (!timer_pending (&dum->timer)) | |
975 | mod_timer (&dum->timer, jiffies + 1); | |
976 | ||
977 | spin_unlock_irqrestore (&dum->lock, flags); | |
978 | return 0; | |
979 | } | |
980 | ||
981 | static int dummy_urb_dequeue (struct usb_hcd *hcd, struct urb *urb) | |
982 | { | |
983 | /* giveback happens automatically in timer callback */ | |
984 | return 0; | |
985 | } | |
986 | ||
987 | static void maybe_set_status (struct urb *urb, int status) | |
988 | { | |
989 | spin_lock (&urb->lock); | |
990 | if (urb->status == -EINPROGRESS) | |
991 | urb->status = status; | |
992 | spin_unlock (&urb->lock); | |
993 | } | |
994 | ||
995 | /* transfer up to a frame's worth; caller must own lock */ | |
996 | static int | |
997 | transfer (struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit) | |
998 | { | |
999 | struct dummy_request *req; | |
1000 | ||
1001 | top: | |
1002 | /* if there's no request queued, the device is NAKing; return */ | |
1003 | list_for_each_entry (req, &ep->queue, queue) { | |
1004 | unsigned host_len, dev_len, len; | |
1005 | int is_short, to_host; | |
1006 | int rescan = 0; | |
1007 | ||
1008 | /* 1..N packets of ep->ep.maxpacket each ... the last one | |
1009 | * may be short (including zero length). | |
1010 | * | |
1011 | * writer can send a zlp explicitly (length 0) or implicitly | |
1012 | * (length mod maxpacket zero, and 'zero' flag); they always | |
1013 | * terminate reads. | |
1014 | */ | |
1015 | host_len = urb->transfer_buffer_length - urb->actual_length; | |
1016 | dev_len = req->req.length - req->req.actual; | |
1017 | len = min (host_len, dev_len); | |
1018 | ||
1019 | /* FIXME update emulated data toggle too */ | |
1020 | ||
1021 | to_host = usb_pipein (urb->pipe); | |
1022 | if (unlikely (len == 0)) | |
1023 | is_short = 1; | |
1024 | else { | |
1025 | char *ubuf, *rbuf; | |
1026 | ||
1027 | /* not enough bandwidth left? */ | |
1028 | if (limit < ep->ep.maxpacket && limit < len) | |
1029 | break; | |
1030 | len = min (len, (unsigned) limit); | |
1031 | if (len == 0) | |
1032 | break; | |
1033 | ||
1034 | /* use an extra pass for the final short packet */ | |
1035 | if (len > ep->ep.maxpacket) { | |
1036 | rescan = 1; | |
1037 | len -= (len % ep->ep.maxpacket); | |
1038 | } | |
1039 | is_short = (len % ep->ep.maxpacket) != 0; | |
1040 | ||
1041 | /* else transfer packet(s) */ | |
1042 | ubuf = urb->transfer_buffer + urb->actual_length; | |
1043 | rbuf = req->req.buf + req->req.actual; | |
1044 | if (to_host) | |
1045 | memcpy (ubuf, rbuf, len); | |
1046 | else | |
1047 | memcpy (rbuf, ubuf, len); | |
1048 | ep->last_io = jiffies; | |
1049 | ||
1050 | limit -= len; | |
1051 | urb->actual_length += len; | |
1052 | req->req.actual += len; | |
1053 | } | |
1054 | ||
1055 | /* short packets terminate, maybe with overflow/underflow. | |
1056 | * it's only really an error to write too much. | |
1057 | * | |
1058 | * partially filling a buffer optionally blocks queue advances | |
1059 | * (so completion handlers can clean up the queue) but we don't | |
1060 | * need to emulate such data-in-flight. so we only show part | |
1061 | * of the URB_SHORT_NOT_OK effect: completion status. | |
1062 | */ | |
1063 | if (is_short) { | |
1064 | if (host_len == dev_len) { | |
1065 | req->req.status = 0; | |
1066 | maybe_set_status (urb, 0); | |
1067 | } else if (to_host) { | |
1068 | req->req.status = 0; | |
1069 | if (dev_len > host_len) | |
1070 | maybe_set_status (urb, -EOVERFLOW); | |
1071 | else | |
1072 | maybe_set_status (urb, | |
1073 | (urb->transfer_flags | |
1074 | & URB_SHORT_NOT_OK) | |
1075 | ? -EREMOTEIO : 0); | |
1076 | } else if (!to_host) { | |
1077 | maybe_set_status (urb, 0); | |
1078 | if (host_len > dev_len) | |
1079 | req->req.status = -EOVERFLOW; | |
1080 | else | |
1081 | req->req.status = 0; | |
1082 | } | |
1083 | ||
1084 | /* many requests terminate without a short packet */ | |
1085 | } else { | |
1086 | if (req->req.length == req->req.actual | |
1087 | && !req->req.zero) | |
1088 | req->req.status = 0; | |
1089 | if (urb->transfer_buffer_length == urb->actual_length | |
1090 | && !(urb->transfer_flags | |
1091 | & URB_ZERO_PACKET)) { | |
1092 | maybe_set_status (urb, 0); | |
1093 | } | |
1094 | } | |
1095 | ||
1096 | /* device side completion --> continuable */ | |
1097 | if (req->req.status != -EINPROGRESS) { | |
1098 | list_del_init (&req->queue); | |
1099 | ||
1100 | spin_unlock (&dum->lock); | |
1101 | req->req.complete (&ep->ep, &req->req); | |
1102 | spin_lock (&dum->lock); | |
1103 | ||
1104 | /* requests might have been unlinked... */ | |
1105 | rescan = 1; | |
1106 | } | |
1107 | ||
1108 | /* host side completion --> terminate */ | |
1109 | if (urb->status != -EINPROGRESS) | |
1110 | break; | |
1111 | ||
1112 | /* rescan to continue with any other queued i/o */ | |
1113 | if (rescan) | |
1114 | goto top; | |
1115 | } | |
1116 | return limit; | |
1117 | } | |
1118 | ||
1119 | static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep) | |
1120 | { | |
1121 | int limit = ep->ep.maxpacket; | |
1122 | ||
1123 | if (dum->gadget.speed == USB_SPEED_HIGH) { | |
1124 | int tmp; | |
1125 | ||
1126 | /* high bandwidth mode */ | |
1127 | tmp = le16_to_cpu(ep->desc->wMaxPacketSize); | |
1da177e4 LT |
1128 | tmp = (tmp >> 11) & 0x03; |
1129 | tmp *= 8 /* applies to entire frame */; | |
1130 | limit += limit * tmp; | |
1131 | } | |
1132 | return limit; | |
1133 | } | |
1134 | ||
1135 | #define is_active(dum) ((dum->port_status & \ | |
1136 | (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \ | |
1137 | USB_PORT_STAT_SUSPEND)) \ | |
1138 | == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE)) | |
1139 | ||
1140 | static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address) | |
1141 | { | |
1142 | int i; | |
1143 | ||
1144 | if (!is_active (dum)) | |
1145 | return NULL; | |
1146 | if ((address & ~USB_DIR_IN) == 0) | |
1147 | return &dum->ep [0]; | |
1148 | for (i = 1; i < DUMMY_ENDPOINTS; i++) { | |
1149 | struct dummy_ep *ep = &dum->ep [i]; | |
1150 | ||
1151 | if (!ep->desc) | |
1152 | continue; | |
1153 | if (ep->desc->bEndpointAddress == address) | |
1154 | return ep; | |
1155 | } | |
1156 | return NULL; | |
1157 | } | |
1158 | ||
1159 | #undef is_active | |
1160 | ||
1161 | #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE) | |
1162 | #define Dev_InRequest (Dev_Request | USB_DIR_IN) | |
1163 | #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE) | |
1164 | #define Intf_InRequest (Intf_Request | USB_DIR_IN) | |
1165 | #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT) | |
1166 | #define Ep_InRequest (Ep_Request | USB_DIR_IN) | |
1167 | ||
1168 | /* drive both sides of the transfers; looks like irq handlers to | |
1169 | * both drivers except the callbacks aren't in_irq(). | |
1170 | */ | |
1171 | static void dummy_timer (unsigned long _dum) | |
1172 | { | |
1173 | struct dummy *dum = (struct dummy *) _dum; | |
1174 | struct urbp *urbp, *tmp; | |
1175 | unsigned long flags; | |
1176 | int limit, total; | |
1177 | int i; | |
1178 | ||
1179 | /* simplistic model for one frame's bandwidth */ | |
1180 | switch (dum->gadget.speed) { | |
1181 | case USB_SPEED_LOW: | |
1182 | total = 8/*bytes*/ * 12/*packets*/; | |
1183 | break; | |
1184 | case USB_SPEED_FULL: | |
1185 | total = 64/*bytes*/ * 19/*packets*/; | |
1186 | break; | |
1187 | case USB_SPEED_HIGH: | |
1188 | total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/; | |
1189 | break; | |
1190 | default: | |
1191 | dev_err (dummy_dev(dum), "bogus device speed\n"); | |
1192 | return; | |
1193 | } | |
1194 | ||
1195 | /* FIXME if HZ != 1000 this will probably misbehave ... */ | |
1196 | ||
1197 | /* look at each urb queued by the host side driver */ | |
1198 | spin_lock_irqsave (&dum->lock, flags); | |
1199 | ||
1200 | if (!dum->udev) { | |
1201 | dev_err (dummy_dev(dum), | |
1202 | "timer fired with no URBs pending?\n"); | |
1203 | spin_unlock_irqrestore (&dum->lock, flags); | |
1204 | return; | |
1205 | } | |
1206 | ||
1207 | for (i = 0; i < DUMMY_ENDPOINTS; i++) { | |
1208 | if (!ep_name [i]) | |
1209 | break; | |
1210 | dum->ep [i].already_seen = 0; | |
1211 | } | |
1212 | ||
1213 | restart: | |
1214 | list_for_each_entry_safe (urbp, tmp, &dum->urbp_list, urbp_list) { | |
1215 | struct urb *urb; | |
1216 | struct dummy_request *req; | |
1217 | u8 address; | |
1218 | struct dummy_ep *ep = NULL; | |
1219 | int type; | |
1220 | ||
1221 | urb = urbp->urb; | |
1222 | if (urb->status != -EINPROGRESS) { | |
1223 | /* likely it was just unlinked */ | |
1224 | goto return_urb; | |
1225 | } | |
1226 | type = usb_pipetype (urb->pipe); | |
1227 | ||
1228 | /* used up this frame's non-periodic bandwidth? | |
1229 | * FIXME there's infinite bandwidth for control and | |
1230 | * periodic transfers ... unrealistic. | |
1231 | */ | |
1232 | if (total <= 0 && type == PIPE_BULK) | |
1233 | continue; | |
1234 | ||
1235 | /* find the gadget's ep for this request (if configured) */ | |
1236 | address = usb_pipeendpoint (urb->pipe); | |
1237 | if (usb_pipein (urb->pipe)) | |
1238 | address |= USB_DIR_IN; | |
1239 | ep = find_endpoint(dum, address); | |
1240 | if (!ep) { | |
1241 | /* set_configuration() disagreement */ | |
1242 | dev_dbg (dummy_dev(dum), | |
1243 | "no ep configured for urb %p\n", | |
1244 | urb); | |
1245 | maybe_set_status (urb, -EPROTO); | |
1246 | goto return_urb; | |
1247 | } | |
1248 | ||
1249 | if (ep->already_seen) | |
1250 | continue; | |
1251 | ep->already_seen = 1; | |
1252 | if (ep == &dum->ep [0] && urb->error_count) { | |
1253 | ep->setup_stage = 1; /* a new urb */ | |
1254 | urb->error_count = 0; | |
1255 | } | |
1256 | if (ep->halted && !ep->setup_stage) { | |
1257 | /* NOTE: must not be iso! */ | |
1258 | dev_dbg (dummy_dev(dum), "ep %s halted, urb %p\n", | |
1259 | ep->ep.name, urb); | |
1260 | maybe_set_status (urb, -EPIPE); | |
1261 | goto return_urb; | |
1262 | } | |
1263 | /* FIXME make sure both ends agree on maxpacket */ | |
1264 | ||
1265 | /* handle control requests */ | |
1266 | if (ep == &dum->ep [0] && ep->setup_stage) { | |
1267 | struct usb_ctrlrequest setup; | |
1268 | int value = 1; | |
1269 | struct dummy_ep *ep2; | |
cc095b0b AS |
1270 | unsigned w_index; |
1271 | unsigned w_value; | |
1da177e4 LT |
1272 | |
1273 | setup = *(struct usb_ctrlrequest*) urb->setup_packet; | |
cc095b0b AS |
1274 | w_index = le16_to_cpu(setup.wIndex); |
1275 | w_value = le16_to_cpu(setup.wValue); | |
1276 | if (le16_to_cpu(setup.wLength) != | |
1277 | urb->transfer_buffer_length) { | |
1da177e4 LT |
1278 | maybe_set_status (urb, -EOVERFLOW); |
1279 | goto return_urb; | |
1280 | } | |
1281 | ||
1282 | /* paranoia, in case of stale queued data */ | |
1283 | list_for_each_entry (req, &ep->queue, queue) { | |
1284 | list_del_init (&req->queue); | |
1285 | req->req.status = -EOVERFLOW; | |
d9b76251 | 1286 | dev_dbg (udc_dev(dum), "stale req = %p\n", |
1da177e4 LT |
1287 | req); |
1288 | ||
1289 | spin_unlock (&dum->lock); | |
1290 | req->req.complete (&ep->ep, &req->req); | |
1291 | spin_lock (&dum->lock); | |
1292 | ep->already_seen = 0; | |
1293 | goto restart; | |
1294 | } | |
1295 | ||
1296 | /* gadget driver never sees set_address or operations | |
1297 | * on standard feature flags. some hardware doesn't | |
1298 | * even expose them. | |
1299 | */ | |
1300 | ep->last_io = jiffies; | |
1301 | ep->setup_stage = 0; | |
1302 | ep->halted = 0; | |
1303 | switch (setup.bRequest) { | |
1304 | case USB_REQ_SET_ADDRESS: | |
1305 | if (setup.bRequestType != Dev_Request) | |
1306 | break; | |
cc095b0b | 1307 | dum->address = w_value; |
1da177e4 | 1308 | maybe_set_status (urb, 0); |
d9b76251 | 1309 | dev_dbg (udc_dev(dum), "set_address = %d\n", |
cc095b0b | 1310 | w_value); |
1da177e4 LT |
1311 | value = 0; |
1312 | break; | |
1313 | case USB_REQ_SET_FEATURE: | |
1314 | if (setup.bRequestType == Dev_Request) { | |
1315 | value = 0; | |
cc095b0b | 1316 | switch (w_value) { |
1da177e4 LT |
1317 | case USB_DEVICE_REMOTE_WAKEUP: |
1318 | break; | |
5742b0c9 AS |
1319 | case USB_DEVICE_B_HNP_ENABLE: |
1320 | dum->gadget.b_hnp_enable = 1; | |
1321 | break; | |
1322 | case USB_DEVICE_A_HNP_SUPPORT: | |
1323 | dum->gadget.a_hnp_support = 1; | |
1324 | break; | |
1325 | case USB_DEVICE_A_ALT_HNP_SUPPORT: | |
1326 | dum->gadget.a_alt_hnp_support | |
1327 | = 1; | |
1328 | break; | |
1da177e4 LT |
1329 | default: |
1330 | value = -EOPNOTSUPP; | |
1331 | } | |
1332 | if (value == 0) { | |
1333 | dum->devstatus |= | |
cc095b0b | 1334 | (1 << w_value); |
1da177e4 LT |
1335 | maybe_set_status (urb, 0); |
1336 | } | |
1337 | ||
1338 | } else if (setup.bRequestType == Ep_Request) { | |
1339 | // endpoint halt | |
cc095b0b | 1340 | ep2 = find_endpoint (dum, w_index); |
1da177e4 LT |
1341 | if (!ep2) { |
1342 | value = -EOPNOTSUPP; | |
1343 | break; | |
1344 | } | |
1345 | ep2->halted = 1; | |
1346 | value = 0; | |
1347 | maybe_set_status (urb, 0); | |
1348 | } | |
1349 | break; | |
1350 | case USB_REQ_CLEAR_FEATURE: | |
1351 | if (setup.bRequestType == Dev_Request) { | |
cc095b0b | 1352 | switch (w_value) { |
1da177e4 LT |
1353 | case USB_DEVICE_REMOTE_WAKEUP: |
1354 | dum->devstatus &= ~(1 << | |
1355 | USB_DEVICE_REMOTE_WAKEUP); | |
1356 | value = 0; | |
1357 | maybe_set_status (urb, 0); | |
1358 | break; | |
1359 | default: | |
1360 | value = -EOPNOTSUPP; | |
1361 | break; | |
1362 | } | |
1363 | } else if (setup.bRequestType == Ep_Request) { | |
1364 | // endpoint halt | |
cc095b0b | 1365 | ep2 = find_endpoint (dum, w_index); |
1da177e4 LT |
1366 | if (!ep2) { |
1367 | value = -EOPNOTSUPP; | |
1368 | break; | |
1369 | } | |
1370 | ep2->halted = 0; | |
1371 | value = 0; | |
1372 | maybe_set_status (urb, 0); | |
1373 | } | |
1374 | break; | |
1375 | case USB_REQ_GET_STATUS: | |
1376 | if (setup.bRequestType == Dev_InRequest | |
1377 | || setup.bRequestType | |
1378 | == Intf_InRequest | |
1379 | || setup.bRequestType | |
1380 | == Ep_InRequest | |
1381 | ) { | |
1382 | char *buf; | |
1383 | ||
1384 | // device: remote wakeup, selfpowered | |
1385 | // interface: nothing | |
1386 | // endpoint: halt | |
1387 | buf = (char *)urb->transfer_buffer; | |
1388 | if (urb->transfer_buffer_length > 0) { | |
1389 | if (setup.bRequestType == | |
1390 | Ep_InRequest) { | |
cc095b0b | 1391 | ep2 = find_endpoint (dum, w_index); |
1da177e4 LT |
1392 | if (!ep2) { |
1393 | value = -EOPNOTSUPP; | |
1394 | break; | |
1395 | } | |
1396 | buf [0] = ep2->halted; | |
1397 | } else if (setup.bRequestType == | |
1398 | Dev_InRequest) { | |
1399 | buf [0] = (u8) | |
1400 | dum->devstatus; | |
1401 | } else | |
1402 | buf [0] = 0; | |
1403 | } | |
1404 | if (urb->transfer_buffer_length > 1) | |
1405 | buf [1] = 0; | |
1406 | urb->actual_length = min (2, | |
1407 | urb->transfer_buffer_length); | |
1408 | value = 0; | |
1409 | maybe_set_status (urb, 0); | |
1410 | } | |
1411 | break; | |
1412 | } | |
1413 | ||
1414 | /* gadget driver handles all other requests. block | |
1415 | * until setup() returns; no reentrancy issues etc. | |
1416 | */ | |
1417 | if (value > 0) { | |
1418 | spin_unlock (&dum->lock); | |
1419 | value = dum->driver->setup (&dum->gadget, | |
1420 | &setup); | |
1421 | spin_lock (&dum->lock); | |
1422 | ||
1423 | if (value >= 0) { | |
1424 | /* no delays (max 64KB data stage) */ | |
1425 | limit = 64*1024; | |
1426 | goto treat_control_like_bulk; | |
1427 | } | |
1428 | /* error, see below */ | |
1429 | } | |
1430 | ||
1431 | if (value < 0) { | |
1432 | if (value != -EOPNOTSUPP) | |
d9b76251 | 1433 | dev_dbg (udc_dev(dum), |
1da177e4 LT |
1434 | "setup --> %d\n", |
1435 | value); | |
1436 | maybe_set_status (urb, -EPIPE); | |
1437 | urb->actual_length = 0; | |
1438 | } | |
1439 | ||
1440 | goto return_urb; | |
1441 | } | |
1442 | ||
1443 | /* non-control requests */ | |
1444 | limit = total; | |
1445 | switch (usb_pipetype (urb->pipe)) { | |
1446 | case PIPE_ISOCHRONOUS: | |
1447 | /* FIXME is it urb->interval since the last xfer? | |
1448 | * use urb->iso_frame_desc[i]. | |
1449 | * complete whether or not ep has requests queued. | |
1450 | * report random errors, to debug drivers. | |
1451 | */ | |
1452 | limit = max (limit, periodic_bytes (dum, ep)); | |
1453 | maybe_set_status (urb, -ENOSYS); | |
1454 | break; | |
1455 | ||
1456 | case PIPE_INTERRUPT: | |
1457 | /* FIXME is it urb->interval since the last xfer? | |
1458 | * this almost certainly polls too fast. | |
1459 | */ | |
1460 | limit = max (limit, periodic_bytes (dum, ep)); | |
1461 | /* FALLTHROUGH */ | |
1462 | ||
1463 | // case PIPE_BULK: case PIPE_CONTROL: | |
1464 | default: | |
1465 | treat_control_like_bulk: | |
1466 | ep->last_io = jiffies; | |
1467 | total = transfer (dum, urb, ep, limit); | |
1468 | break; | |
1469 | } | |
1470 | ||
1471 | /* incomplete transfer? */ | |
1472 | if (urb->status == -EINPROGRESS) | |
1473 | continue; | |
1474 | ||
1475 | return_urb: | |
1476 | urb->hcpriv = NULL; | |
1477 | list_del (&urbp->urbp_list); | |
1478 | kfree (urbp); | |
1479 | if (ep) | |
1480 | ep->already_seen = ep->setup_stage = 0; | |
1481 | ||
1482 | spin_unlock (&dum->lock); | |
1483 | usb_hcd_giveback_urb (dummy_to_hcd(dum), urb, NULL); | |
1484 | spin_lock (&dum->lock); | |
1485 | ||
1486 | goto restart; | |
1487 | } | |
1488 | ||
1489 | /* want a 1 msec delay here */ | |
1490 | if (!list_empty (&dum->urbp_list)) | |
1491 | mod_timer (&dum->timer, jiffies + msecs_to_jiffies(1)); | |
1492 | else { | |
1493 | usb_put_dev (dum->udev); | |
1494 | dum->udev = NULL; | |
1495 | } | |
1496 | ||
1497 | spin_unlock_irqrestore (&dum->lock, flags); | |
1498 | } | |
1499 | ||
1500 | /*-------------------------------------------------------------------------*/ | |
1501 | ||
1502 | #define PORT_C_MASK \ | |
c2db8b5e AS |
1503 | ((USB_PORT_STAT_C_CONNECTION \ |
1504 | | USB_PORT_STAT_C_ENABLE \ | |
1505 | | USB_PORT_STAT_C_SUSPEND \ | |
1506 | | USB_PORT_STAT_C_OVERCURRENT \ | |
1507 | | USB_PORT_STAT_C_RESET) << 16) | |
1da177e4 LT |
1508 | |
1509 | static int dummy_hub_status (struct usb_hcd *hcd, char *buf) | |
1510 | { | |
1511 | struct dummy *dum; | |
1512 | unsigned long flags; | |
1513 | int retval; | |
1514 | ||
1515 | dum = hcd_to_dummy (hcd); | |
1516 | ||
1517 | spin_lock_irqsave (&dum->lock, flags); | |
f1c39fad AS |
1518 | |
1519 | if (dum->resuming && time_after_eq (jiffies, dum->re_timeout)) { | |
1520 | dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16); | |
1521 | dum->port_status &= ~USB_PORT_STAT_SUSPEND; | |
1522 | set_link_state (dum); | |
1523 | } | |
1524 | ||
1da177e4 LT |
1525 | if (!(dum->port_status & PORT_C_MASK)) |
1526 | retval = 0; | |
1527 | else { | |
1528 | *buf = (1 << 1); | |
1529 | dev_dbg (dummy_dev(dum), "port status 0x%08x has changes\n", | |
1530 | dum->port_status); | |
1531 | retval = 1; | |
1532 | } | |
1533 | spin_unlock_irqrestore (&dum->lock, flags); | |
1534 | return retval; | |
1535 | } | |
1536 | ||
1537 | static inline void | |
1538 | hub_descriptor (struct usb_hub_descriptor *desc) | |
1539 | { | |
1540 | memset (desc, 0, sizeof *desc); | |
1541 | desc->bDescriptorType = 0x29; | |
1542 | desc->bDescLength = 9; | |
cc095b0b AS |
1543 | desc->wHubCharacteristics = (__force __u16) |
1544 | (__constant_cpu_to_le16 (0x0001)); | |
1da177e4 LT |
1545 | desc->bNbrPorts = 1; |
1546 | desc->bitmap [0] = 0xff; | |
1547 | desc->bitmap [1] = 0xff; | |
1548 | } | |
1549 | ||
1550 | static int dummy_hub_control ( | |
1551 | struct usb_hcd *hcd, | |
1552 | u16 typeReq, | |
1553 | u16 wValue, | |
1554 | u16 wIndex, | |
1555 | char *buf, | |
1556 | u16 wLength | |
1557 | ) { | |
1558 | struct dummy *dum; | |
1559 | int retval = 0; | |
1560 | unsigned long flags; | |
1561 | ||
1562 | dum = hcd_to_dummy (hcd); | |
1563 | spin_lock_irqsave (&dum->lock, flags); | |
1564 | switch (typeReq) { | |
1565 | case ClearHubFeature: | |
1566 | break; | |
1567 | case ClearPortFeature: | |
1568 | switch (wValue) { | |
1569 | case USB_PORT_FEAT_SUSPEND: | |
c2db8b5e | 1570 | if (dum->port_status & USB_PORT_STAT_SUSPEND) { |
1da177e4 LT |
1571 | /* 20msec resume signaling */ |
1572 | dum->resuming = 1; | |
1573 | dum->re_timeout = jiffies + | |
f1c39fad | 1574 | msecs_to_jiffies(20); |
1da177e4 LT |
1575 | } |
1576 | break; | |
1577 | case USB_PORT_FEAT_POWER: | |
f1c39fad AS |
1578 | if (dum->port_status & USB_PORT_STAT_POWER) |
1579 | dev_dbg (dummy_dev(dum), "power-off\n"); | |
1580 | /* FALLS THROUGH */ | |
1da177e4 LT |
1581 | default: |
1582 | dum->port_status &= ~(1 << wValue); | |
f1c39fad | 1583 | set_link_state (dum); |
1da177e4 LT |
1584 | } |
1585 | break; | |
1586 | case GetHubDescriptor: | |
1587 | hub_descriptor ((struct usb_hub_descriptor *) buf); | |
1588 | break; | |
1589 | case GetHubStatus: | |
cc095b0b | 1590 | *(__le32 *) buf = __constant_cpu_to_le32 (0); |
1da177e4 LT |
1591 | break; |
1592 | case GetPortStatus: | |
1593 | if (wIndex != 1) | |
1594 | retval = -EPIPE; | |
1595 | ||
1596 | /* whoever resets or resumes must GetPortStatus to | |
1597 | * complete it!! | |
1598 | */ | |
f1c39fad AS |
1599 | if (dum->resuming && |
1600 | time_after_eq (jiffies, dum->re_timeout)) { | |
c2db8b5e AS |
1601 | dum->port_status |= (USB_PORT_STAT_C_SUSPEND << 16); |
1602 | dum->port_status &= ~USB_PORT_STAT_SUSPEND; | |
1da177e4 | 1603 | } |
f1c39fad AS |
1604 | if ((dum->port_status & USB_PORT_STAT_RESET) != 0 && |
1605 | time_after_eq (jiffies, dum->re_timeout)) { | |
c2db8b5e AS |
1606 | dum->port_status |= (USB_PORT_STAT_C_RESET << 16); |
1607 | dum->port_status &= ~USB_PORT_STAT_RESET; | |
f1c39fad | 1608 | if (dum->pullup) { |
1da177e4 LT |
1609 | dum->port_status |= USB_PORT_STAT_ENABLE; |
1610 | /* give it the best speed we agree on */ | |
1611 | dum->gadget.speed = dum->driver->speed; | |
1612 | dum->gadget.ep0->maxpacket = 64; | |
1613 | switch (dum->gadget.speed) { | |
1614 | case USB_SPEED_HIGH: | |
1615 | dum->port_status |= | |
1616 | USB_PORT_STAT_HIGH_SPEED; | |
1617 | break; | |
1618 | case USB_SPEED_LOW: | |
1619 | dum->gadget.ep0->maxpacket = 8; | |
1620 | dum->port_status |= | |
1621 | USB_PORT_STAT_LOW_SPEED; | |
1622 | break; | |
1623 | default: | |
1624 | dum->gadget.speed = USB_SPEED_FULL; | |
1625 | break; | |
1626 | } | |
1627 | } | |
1628 | } | |
f1c39fad | 1629 | set_link_state (dum); |
cc095b0b AS |
1630 | ((__le16 *) buf)[0] = cpu_to_le16 (dum->port_status); |
1631 | ((__le16 *) buf)[1] = cpu_to_le16 (dum->port_status >> 16); | |
1da177e4 LT |
1632 | break; |
1633 | case SetHubFeature: | |
1634 | retval = -EPIPE; | |
1635 | break; | |
1636 | case SetPortFeature: | |
1637 | switch (wValue) { | |
1638 | case USB_PORT_FEAT_SUSPEND: | |
f1c39fad | 1639 | if (dum->active) { |
c2db8b5e | 1640 | dum->port_status |= USB_PORT_STAT_SUSPEND; |
f1c39fad AS |
1641 | |
1642 | /* HNP would happen here; for now we | |
1643 | * assume b_bus_req is always true. | |
1644 | */ | |
1645 | set_link_state (dum); | |
1646 | if (((1 << USB_DEVICE_B_HNP_ENABLE) | |
1647 | & dum->devstatus) != 0) | |
1648 | dev_dbg (dummy_dev(dum), | |
5742b0c9 | 1649 | "no HNP yet!\n"); |
1da177e4 LT |
1650 | } |
1651 | break; | |
f1c39fad AS |
1652 | case USB_PORT_FEAT_POWER: |
1653 | dum->port_status |= USB_PORT_STAT_POWER; | |
1654 | set_link_state (dum); | |
1655 | break; | |
1da177e4 | 1656 | case USB_PORT_FEAT_RESET: |
f1c39fad AS |
1657 | /* if it's already enabled, disable */ |
1658 | dum->port_status &= ~(USB_PORT_STAT_ENABLE | |
1659 | | USB_PORT_STAT_LOW_SPEED | |
1660 | | USB_PORT_STAT_HIGH_SPEED); | |
1da177e4 LT |
1661 | /* 50msec reset signaling */ |
1662 | dum->re_timeout = jiffies + msecs_to_jiffies(50); | |
f1c39fad | 1663 | /* FALLS THROUGH */ |
1da177e4 | 1664 | default: |
f1c39fad AS |
1665 | if ((dum->port_status & USB_PORT_STAT_POWER) != 0) { |
1666 | dum->port_status |= (1 << wValue); | |
1667 | set_link_state (dum); | |
1668 | } | |
1da177e4 LT |
1669 | } |
1670 | break; | |
1671 | ||
1672 | default: | |
1673 | dev_dbg (dummy_dev(dum), | |
1674 | "hub control req%04x v%04x i%04x l%d\n", | |
1675 | typeReq, wValue, wIndex, wLength); | |
1676 | ||
1677 | /* "protocol stall" on error */ | |
1678 | retval = -EPIPE; | |
1679 | } | |
1680 | spin_unlock_irqrestore (&dum->lock, flags); | |
685eb93f AS |
1681 | |
1682 | if ((dum->port_status & PORT_C_MASK) != 0) | |
1683 | usb_hcd_poll_rh_status (hcd); | |
1da177e4 LT |
1684 | return retval; |
1685 | } | |
1686 | ||
1687 | ||
1688 | /*-------------------------------------------------------------------------*/ | |
1689 | ||
1690 | static inline ssize_t | |
1691 | show_urb (char *buf, size_t size, struct urb *urb) | |
1692 | { | |
1693 | int ep = usb_pipeendpoint (urb->pipe); | |
1694 | ||
1695 | return snprintf (buf, size, | |
1696 | "urb/%p %s ep%d%s%s len %d/%d\n", | |
1697 | urb, | |
1698 | ({ char *s; | |
1699 | switch (urb->dev->speed) { | |
1700 | case USB_SPEED_LOW: s = "ls"; break; | |
1701 | case USB_SPEED_FULL: s = "fs"; break; | |
1702 | case USB_SPEED_HIGH: s = "hs"; break; | |
1703 | default: s = "?"; break; | |
1704 | }; s; }), | |
1705 | ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "", | |
1706 | ({ char *s; \ | |
1707 | switch (usb_pipetype (urb->pipe)) { \ | |
1708 | case PIPE_CONTROL: s = ""; break; \ | |
1709 | case PIPE_BULK: s = "-bulk"; break; \ | |
1710 | case PIPE_INTERRUPT: s = "-int"; break; \ | |
1711 | default: s = "-iso"; break; \ | |
1712 | }; s;}), | |
1713 | urb->actual_length, urb->transfer_buffer_length); | |
1714 | } | |
1715 | ||
1716 | static ssize_t | |
10523b3b | 1717 | show_urbs (struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
1718 | { |
1719 | struct usb_hcd *hcd = dev_get_drvdata (dev); | |
1720 | struct dummy *dum = hcd_to_dummy (hcd); | |
1721 | struct urbp *urbp; | |
1722 | size_t size = 0; | |
1723 | unsigned long flags; | |
1724 | ||
1725 | spin_lock_irqsave (&dum->lock, flags); | |
1726 | list_for_each_entry (urbp, &dum->urbp_list, urbp_list) { | |
1727 | size_t temp; | |
1728 | ||
1729 | temp = show_urb (buf, PAGE_SIZE - size, urbp->urb); | |
1730 | buf += temp; | |
1731 | size += temp; | |
1732 | } | |
1733 | spin_unlock_irqrestore (&dum->lock, flags); | |
1734 | ||
1735 | return size; | |
1736 | } | |
1737 | static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL); | |
1738 | ||
1739 | static int dummy_start (struct usb_hcd *hcd) | |
1740 | { | |
1741 | struct dummy *dum; | |
1da177e4 LT |
1742 | |
1743 | dum = hcd_to_dummy (hcd); | |
1744 | ||
1745 | /* | |
1746 | * MASTER side init ... we emulate a root hub that'll only ever | |
1747 | * talk to one device (the slave side). Also appears in sysfs, | |
1748 | * just like more familiar pci-based HCDs. | |
1749 | */ | |
1750 | spin_lock_init (&dum->lock); | |
1751 | init_timer (&dum->timer); | |
1752 | dum->timer.function = dummy_timer; | |
1753 | dum->timer.data = (unsigned long) dum; | |
1754 | ||
1755 | INIT_LIST_HEAD (&dum->urbp_list); | |
1756 | ||
bc96c0ad AS |
1757 | /* only show a low-power port: just 8mA */ |
1758 | hcd->power_budget = 8; | |
1da177e4 | 1759 | hcd->state = HC_STATE_RUNNING; |
685eb93f | 1760 | hcd->uses_new_polling = 1; |
1da177e4 | 1761 | |
5742b0c9 AS |
1762 | #ifdef CONFIG_USB_OTG |
1763 | hcd->self.otg_port = 1; | |
1764 | #endif | |
1765 | ||
1da177e4 LT |
1766 | /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */ |
1767 | device_create_file (dummy_dev(dum), &dev_attr_urbs); | |
1768 | return 0; | |
1da177e4 LT |
1769 | } |
1770 | ||
1771 | static void dummy_stop (struct usb_hcd *hcd) | |
1772 | { | |
1773 | struct dummy *dum; | |
1774 | ||
1775 | dum = hcd_to_dummy (hcd); | |
1776 | ||
1777 | device_remove_file (dummy_dev(dum), &dev_attr_urbs); | |
1da177e4 | 1778 | usb_gadget_unregister_driver (dum->driver); |
1da177e4 LT |
1779 | dev_info (dummy_dev(dum), "stopped\n"); |
1780 | } | |
1781 | ||
1782 | /*-------------------------------------------------------------------------*/ | |
1783 | ||
1784 | static int dummy_h_get_frame (struct usb_hcd *hcd) | |
1785 | { | |
1786 | return dummy_g_get_frame (NULL); | |
1787 | } | |
1788 | ||
1789 | static const struct hc_driver dummy_hcd = { | |
1790 | .description = (char *) driver_name, | |
1791 | .product_desc = "Dummy host controller", | |
1792 | .hcd_priv_size = sizeof(struct dummy), | |
1793 | ||
1794 | .flags = HCD_USB2, | |
1795 | ||
1796 | .start = dummy_start, | |
1797 | .stop = dummy_stop, | |
1798 | ||
1799 | .urb_enqueue = dummy_urb_enqueue, | |
1800 | .urb_dequeue = dummy_urb_dequeue, | |
1801 | ||
1802 | .get_frame_number = dummy_h_get_frame, | |
1803 | ||
1804 | .hub_status_data = dummy_hub_status, | |
1805 | .hub_control = dummy_hub_control, | |
1806 | }; | |
1807 | ||
d9b76251 | 1808 | static int dummy_hcd_probe (struct device *dev) |
1da177e4 LT |
1809 | { |
1810 | struct usb_hcd *hcd; | |
1811 | int retval; | |
1812 | ||
1813 | dev_info (dev, "%s, driver " DRIVER_VERSION "\n", driver_desc); | |
1814 | ||
1815 | hcd = usb_create_hcd (&dummy_hcd, dev, dev->bus_id); | |
1816 | if (!hcd) | |
1817 | return -ENOMEM; | |
1818 | the_controller = hcd_to_dummy (hcd); | |
1819 | ||
1820 | retval = usb_add_hcd(hcd, 0, 0); | |
1821 | if (retval != 0) { | |
1822 | usb_put_hcd (hcd); | |
1823 | the_controller = NULL; | |
1824 | } | |
1825 | return retval; | |
1826 | } | |
1827 | ||
d9b76251 | 1828 | static int dummy_hcd_remove (struct device *dev) |
1da177e4 LT |
1829 | { |
1830 | struct usb_hcd *hcd; | |
1831 | ||
1832 | hcd = dev_get_drvdata (dev); | |
1833 | usb_remove_hcd (hcd); | |
1834 | usb_put_hcd (hcd); | |
1835 | the_controller = NULL; | |
d9b76251 | 1836 | return 0; |
1da177e4 LT |
1837 | } |
1838 | ||
d9b76251 AS |
1839 | static struct device_driver dummy_hcd_driver = { |
1840 | .name = (char *) driver_name, | |
1841 | .bus = &platform_bus_type, | |
1842 | .probe = dummy_hcd_probe, | |
1843 | .remove = dummy_hcd_remove, | |
1844 | }; | |
1da177e4 | 1845 | |
d9b76251 | 1846 | /*-------------------------------------------------------------------------*/ |
1da177e4 | 1847 | |
d9b76251 AS |
1848 | /* These don't need to do anything because the pdev structures are |
1849 | * statically allocated. */ | |
1850 | static void | |
1851 | dummy_udc_release (struct device *dev) {} | |
1da177e4 | 1852 | |
d9b76251 AS |
1853 | static void |
1854 | dummy_hcd_release (struct device *dev) {} | |
1855 | ||
1856 | static struct platform_device the_udc_pdev = { | |
1857 | .name = (char *) gadget_name, | |
1858 | .id = -1, | |
1859 | .dev = { | |
1860 | .release = dummy_udc_release, | |
1861 | }, | |
1862 | }; | |
1da177e4 | 1863 | |
d9b76251 AS |
1864 | static struct platform_device the_hcd_pdev = { |
1865 | .name = (char *) driver_name, | |
1866 | .id = -1, | |
1867 | .dev = { | |
1868 | .release = dummy_hcd_release, | |
1869 | }, | |
1870 | }; | |
1da177e4 LT |
1871 | |
1872 | static int __init init (void) | |
1873 | { | |
1874 | int retval; | |
1875 | ||
1876 | if (usb_disabled ()) | |
1877 | return -ENODEV; | |
d9b76251 AS |
1878 | |
1879 | retval = driver_register (&dummy_hcd_driver); | |
1880 | if (retval < 0) | |
1da177e4 | 1881 | return retval; |
d9b76251 AS |
1882 | |
1883 | retval = driver_register (&dummy_udc_driver); | |
1884 | if (retval < 0) | |
1885 | goto err_register_udc_driver; | |
1886 | ||
1887 | retval = platform_device_register (&the_hcd_pdev); | |
1888 | if (retval < 0) | |
1889 | goto err_register_hcd; | |
1890 | ||
1891 | retval = platform_device_register (&the_udc_pdev); | |
1892 | if (retval < 0) | |
1893 | goto err_register_udc; | |
1894 | return retval; | |
1895 | ||
1896 | err_register_udc: | |
1897 | platform_device_unregister (&the_hcd_pdev); | |
1898 | err_register_hcd: | |
1899 | driver_unregister (&dummy_udc_driver); | |
1900 | err_register_udc_driver: | |
1901 | driver_unregister (&dummy_hcd_driver); | |
1da177e4 LT |
1902 | return retval; |
1903 | } | |
1904 | module_init (init); | |
1905 | ||
1906 | static void __exit cleanup (void) | |
1907 | { | |
d9b76251 AS |
1908 | platform_device_unregister (&the_udc_pdev); |
1909 | platform_device_unregister (&the_hcd_pdev); | |
1910 | driver_unregister (&dummy_udc_driver); | |
1911 | driver_unregister (&dummy_hcd_driver); | |
1da177e4 LT |
1912 | } |
1913 | module_exit (cleanup); |