arm: vf610: fix double iomux configuration for vf610twr board
[u-boot/qq2440-u-boot.git] / drivers / usb / gadget / core.c
blob30d55a49ac9bf07cd4871958b5687f2c6b9530e8
1 /*
2 * (C) Copyright 2003
3 * Gerry Hamel, geh@ti.com, Texas Instruments
5 * Based on
6 * linux/drivers/usbd/usbd.c.c - USB Device Core Layer
8 * Copyright (c) 2000, 2001, 2002 Lineo
9 * Copyright (c) 2001 Hewlett Packard
11 * By:
12 * Stuart Lynne <sl@lineo.com>,
13 * Tom Rushworth <tbr@lineo.com>,
14 * Bruce Balden <balden@lineo.com>
16 * SPDX-License-Identifier: GPL-2.0+
19 #include <malloc.h>
20 #include <usbdevice.h>
22 #define MAX_INTERFACES 2
25 int maxstrings = 20;
27 /* Global variables ************************************************************************** */
29 struct usb_string_descriptor **usb_strings;
31 int usb_devices;
33 extern struct usb_function_driver ep0_driver;
35 int registered_functions;
36 int registered_devices;
38 char *usbd_device_events[] = {
39 "DEVICE_UNKNOWN",
40 "DEVICE_INIT",
41 "DEVICE_CREATE",
42 "DEVICE_HUB_CONFIGURED",
43 "DEVICE_RESET",
44 "DEVICE_ADDRESS_ASSIGNED",
45 "DEVICE_CONFIGURED",
46 "DEVICE_SET_INTERFACE",
47 "DEVICE_SET_FEATURE",
48 "DEVICE_CLEAR_FEATURE",
49 "DEVICE_DE_CONFIGURED",
50 "DEVICE_BUS_INACTIVE",
51 "DEVICE_BUS_ACTIVITY",
52 "DEVICE_POWER_INTERRUPTION",
53 "DEVICE_HUB_RESET",
54 "DEVICE_DESTROY",
55 "DEVICE_FUNCTION_PRIVATE",
58 char *usbd_device_states[] = {
59 "STATE_INIT",
60 "STATE_CREATED",
61 "STATE_ATTACHED",
62 "STATE_POWERED",
63 "STATE_DEFAULT",
64 "STATE_ADDRESSED",
65 "STATE_CONFIGURED",
66 "STATE_UNKNOWN",
69 char *usbd_device_requests[] = {
70 "GET STATUS", /* 0 */
71 "CLEAR FEATURE", /* 1 */
72 "RESERVED", /* 2 */
73 "SET FEATURE", /* 3 */
74 "RESERVED", /* 4 */
75 "SET ADDRESS", /* 5 */
76 "GET DESCRIPTOR", /* 6 */
77 "SET DESCRIPTOR", /* 7 */
78 "GET CONFIGURATION", /* 8 */
79 "SET CONFIGURATION", /* 9 */
80 "GET INTERFACE", /* 10 */
81 "SET INTERFACE", /* 11 */
82 "SYNC FRAME", /* 12 */
85 char *usbd_device_descriptors[] = {
86 "UNKNOWN", /* 0 */
87 "DEVICE", /* 1 */
88 "CONFIG", /* 2 */
89 "STRING", /* 3 */
90 "INTERFACE", /* 4 */
91 "ENDPOINT", /* 5 */
92 "DEVICE QUALIFIER", /* 6 */
93 "OTHER SPEED", /* 7 */
94 "INTERFACE POWER", /* 8 */
97 char *usbd_device_status[] = {
98 "USBD_OPENING",
99 "USBD_OK",
100 "USBD_SUSPENDED",
101 "USBD_CLOSING",
105 /* Descriptor support functions ************************************************************** */
109 * usbd_get_string - find and return a string descriptor
110 * @index: string index to return
112 * Find an indexed string and return a pointer to a it.
114 struct usb_string_descriptor *usbd_get_string (__u8 index)
116 if (index >= maxstrings) {
117 return NULL;
119 return usb_strings[index];
123 /* Access to device descriptor functions ***************************************************** */
126 /* *
127 * usbd_device_configuration_instance - find a configuration instance for this device
128 * @device:
129 * @configuration: index to configuration, 0 - N-1
131 * Get specifed device configuration. Index should be bConfigurationValue-1.
133 static struct usb_configuration_instance *usbd_device_configuration_instance (struct usb_device_instance *device,
134 unsigned int port, unsigned int configuration)
136 if (configuration >= device->configurations)
137 return NULL;
139 return device->configuration_instance_array + configuration;
143 /* *
144 * usbd_device_interface_instance
145 * @device:
146 * @configuration: index to configuration, 0 - N-1
147 * @interface: index to interface
149 * Return the specified interface descriptor for the specified device.
151 struct usb_interface_instance *usbd_device_interface_instance (struct usb_device_instance *device, int port, int configuration, int interface)
153 struct usb_configuration_instance *configuration_instance;
155 if ((configuration_instance = usbd_device_configuration_instance (device, port, configuration)) == NULL) {
156 return NULL;
158 if (interface >= configuration_instance->interfaces) {
159 return NULL;
161 return configuration_instance->interface_instance_array + interface;
164 /* *
165 * usbd_device_alternate_descriptor_list
166 * @device:
167 * @configuration: index to configuration, 0 - N-1
168 * @interface: index to interface
169 * @alternate: alternate setting
171 * Return the specified alternate descriptor for the specified device.
173 struct usb_alternate_instance *usbd_device_alternate_instance (struct usb_device_instance *device, int port, int configuration, int interface, int alternate)
175 struct usb_interface_instance *interface_instance;
177 if ((interface_instance = usbd_device_interface_instance (device, port, configuration, interface)) == NULL) {
178 return NULL;
181 if (alternate >= interface_instance->alternates) {
182 return NULL;
185 return interface_instance->alternates_instance_array + alternate;
189 /* *
190 * usbd_device_device_descriptor
191 * @device: which device
192 * @configuration: index to configuration, 0 - N-1
193 * @port: which port
195 * Return the specified configuration descriptor for the specified device.
197 struct usb_device_descriptor *usbd_device_device_descriptor (struct usb_device_instance *device, int port)
199 return (device->device_descriptor);
203 * usbd_device_configuration_descriptor
204 * @device: which device
205 * @port: which port
206 * @configuration: index to configuration, 0 - N-1
208 * Return the specified configuration descriptor for the specified device.
210 struct usb_configuration_descriptor *usbd_device_configuration_descriptor (struct
211 usb_device_instance
212 *device, int port, int configuration)
214 struct usb_configuration_instance *configuration_instance;
215 if (!(configuration_instance = usbd_device_configuration_instance (device, port, configuration))) {
216 return NULL;
218 return (configuration_instance->configuration_descriptor);
223 * usbd_device_interface_descriptor
224 * @device: which device
225 * @port: which port
226 * @configuration: index to configuration, 0 - N-1
227 * @interface: index to interface
228 * @alternate: alternate setting
230 * Return the specified interface descriptor for the specified device.
232 struct usb_interface_descriptor *usbd_device_interface_descriptor (struct usb_device_instance
233 *device, int port, int configuration, int interface, int alternate)
235 struct usb_interface_instance *interface_instance;
236 if (!(interface_instance = usbd_device_interface_instance (device, port, configuration, interface))) {
237 return NULL;
239 if ((alternate < 0) || (alternate >= interface_instance->alternates)) {
240 return NULL;
242 return (interface_instance->alternates_instance_array[alternate].interface_descriptor);
246 * usbd_device_endpoint_descriptor_index
247 * @device: which device
248 * @port: which port
249 * @configuration: index to configuration, 0 - N-1
250 * @interface: index to interface
251 * @alternate: index setting
252 * @index: which index
254 * Return the specified endpoint descriptor for the specified device.
256 struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor_index (struct usb_device_instance
257 *device, int port, int configuration, int interface, int alternate, int index)
259 struct usb_alternate_instance *alternate_instance;
261 if (!(alternate_instance = usbd_device_alternate_instance (device, port, configuration, interface, alternate))) {
262 return NULL;
264 if (index >= alternate_instance->endpoints) {
265 return NULL;
267 return *(alternate_instance->endpoints_descriptor_array + index);
272 * usbd_device_endpoint_transfersize
273 * @device: which device
274 * @port: which port
275 * @configuration: index to configuration, 0 - N-1
276 * @interface: index to interface
277 * @index: which index
279 * Return the specified endpoint transfer size;
281 int usbd_device_endpoint_transfersize (struct usb_device_instance *device, int port, int configuration, int interface, int alternate, int index)
283 struct usb_alternate_instance *alternate_instance;
285 if (!(alternate_instance = usbd_device_alternate_instance (device, port, configuration, interface, alternate))) {
286 return 0;
288 if (index >= alternate_instance->endpoints) {
289 return 0;
291 return *(alternate_instance->endpoint_transfersize_array + index);
296 * usbd_device_endpoint_descriptor
297 * @device: which device
298 * @port: which port
299 * @configuration: index to configuration, 0 - N-1
300 * @interface: index to interface
301 * @alternate: alternate setting
302 * @endpoint: which endpoint
304 * Return the specified endpoint descriptor for the specified device.
306 struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor (struct usb_device_instance *device, int port, int configuration, int interface, int alternate, int endpoint)
308 struct usb_endpoint_descriptor *endpoint_descriptor;
309 int i;
311 for (i = 0; !(endpoint_descriptor = usbd_device_endpoint_descriptor_index (device, port, configuration, interface, alternate, i)); i++) {
312 if (endpoint_descriptor->bEndpointAddress == endpoint) {
313 return endpoint_descriptor;
316 return NULL;
320 * usbd_endpoint_halted
321 * @device: point to struct usb_device_instance
322 * @endpoint: endpoint to check
324 * Return non-zero if endpoint is halted.
326 int usbd_endpoint_halted (struct usb_device_instance *device, int endpoint)
328 return (device->status == USB_STATUS_HALT);
333 * usbd_rcv_complete - complete a receive
334 * @endpoint:
335 * @len:
336 * @urb_bad:
338 * Called from rcv interrupt to complete.
340 void usbd_rcv_complete(struct usb_endpoint_instance *endpoint, int len, int urb_bad)
342 if (endpoint) {
343 struct urb *rcv_urb;
345 /*usbdbg("len: %d urb: %p\n", len, endpoint->rcv_urb); */
347 /* if we had an urb then update actual_length, dispatch if neccessary */
348 if ((rcv_urb = endpoint->rcv_urb)) {
350 /*usbdbg("actual: %d buffer: %d\n", */
351 /*rcv_urb->actual_length, rcv_urb->buffer_length); */
353 /* check the urb is ok, are we adding data less than the packetsize */
354 if (!urb_bad && (len <= endpoint->rcv_packetSize)) {
355 /*usbdbg("updating actual_length by %d\n",len); */
357 /* increment the received data size */
358 rcv_urb->actual_length += len;
360 } else {
361 usberr(" RECV_ERROR actual: %d buffer: %d urb_bad: %d\n",
362 rcv_urb->actual_length, rcv_urb->buffer_length, urb_bad);
364 rcv_urb->actual_length = 0;
365 rcv_urb->status = RECV_ERROR;
367 } else {
368 usberr("no rcv_urb!");
370 } else {
371 usberr("no endpoint!");
377 * usbd_tx_complete - complete a transmit
378 * @endpoint:
379 * @resetart:
381 * Called from tx interrupt to complete.
383 void usbd_tx_complete (struct usb_endpoint_instance *endpoint)
385 if (endpoint) {
386 struct urb *tx_urb;
388 /* if we have a tx_urb advance or reset, finish if complete */
389 if ((tx_urb = endpoint->tx_urb)) {
390 int sent = endpoint->last;
391 endpoint->sent += sent;
392 endpoint->last -= sent;
394 if( (endpoint->tx_urb->actual_length - endpoint->sent) <= 0 ) {
395 tx_urb->actual_length = 0;
396 endpoint->sent = 0;
397 endpoint->last = 0;
399 /* Remove from active, save for re-use */
400 urb_detach(tx_urb);
401 urb_append(&endpoint->done, tx_urb);
402 /*usbdbg("done->next %p, tx_urb %p, done %p", */
403 /* endpoint->done.next, tx_urb, &endpoint->done); */
405 endpoint->tx_urb = first_urb_detached(&endpoint->tx);
406 if( endpoint->tx_urb ) {
407 endpoint->tx_queue--;
408 usbdbg("got urb from tx list");
410 if( !endpoint->tx_urb ) {
411 /*usbdbg("taking urb from done list"); */
412 endpoint->tx_urb = first_urb_detached(&endpoint->done);
414 if( !endpoint->tx_urb ) {
415 usbdbg("allocating new urb for tx_urb");
416 endpoint->tx_urb = usbd_alloc_urb(tx_urb->device, endpoint);
423 /* URB linked list functions ***************************************************** */
426 * Initialize an urb_link to be a single element list.
427 * If the urb_link is being used as a distinguished list head
428 * the list is empty when the head is the only link in the list.
430 void urb_link_init (urb_link * ul)
432 if (ul) {
433 ul->prev = ul->next = ul;
438 * Detach an urb_link from a list, and set it
439 * up as a single element list, so no dangling
440 * pointers can be followed, and so it can be
441 * joined to another list if so desired.
443 void urb_detach (struct urb *urb)
445 if (urb) {
446 urb_link *ul = &urb->link;
447 ul->next->prev = ul->prev;
448 ul->prev->next = ul->next;
449 urb_link_init (ul);
454 * Return the first urb_link in a list with a distinguished
455 * head "hd", or NULL if the list is empty. This will also
456 * work as a predicate, returning NULL if empty, and non-NULL
457 * otherwise.
459 urb_link *first_urb_link (urb_link * hd)
461 urb_link *nx;
462 if (NULL != hd && NULL != (nx = hd->next) && nx != hd) {
463 /* There is at least one element in the list */
464 /* (besides the distinguished head). */
465 return (nx);
467 /* The list is empty */
468 return (NULL);
472 * Return the first urb in a list with a distinguished
473 * head "hd", or NULL if the list is empty.
475 struct urb *first_urb (urb_link * hd)
477 urb_link *nx;
478 if (NULL == (nx = first_urb_link (hd))) {
479 /* The list is empty */
480 return (NULL);
482 return (p2surround (struct urb, link, nx));
486 * Detach and return the first urb in a list with a distinguished
487 * head "hd", or NULL if the list is empty.
490 struct urb *first_urb_detached (urb_link * hd)
492 struct urb *urb;
493 if ((urb = first_urb (hd))) {
494 urb_detach (urb);
496 return urb;
501 * Append an urb_link (or a whole list of
502 * urb_links) to the tail of another list
503 * of urb_links.
505 void urb_append (urb_link * hd, struct urb *urb)
507 if (hd && urb) {
508 urb_link *new = &urb->link;
510 /* This allows the new urb to be a list of urbs, */
511 /* with new pointing at the first, but the link */
512 /* must be initialized. */
513 /* Order is important here... */
514 urb_link *pul = hd->prev;
515 new->prev->next = hd;
516 hd->prev = new->prev;
517 new->prev = pul;
518 pul->next = new;
522 /* URB create/destroy functions ***************************************************** */
525 * usbd_alloc_urb - allocate an URB appropriate for specified endpoint
526 * @device: device instance
527 * @endpoint: endpoint
529 * Allocate an urb structure. The usb device urb structure is used to
530 * contain all data associated with a transfer, including a setup packet for
531 * control transfers.
533 * NOTE: endpoint_address MUST contain a direction flag.
535 struct urb *usbd_alloc_urb (struct usb_device_instance *device,
536 struct usb_endpoint_instance *endpoint)
538 struct urb *urb;
540 if (!(urb = (struct urb *) malloc (sizeof (struct urb)))) {
541 usberr (" F A T A L: malloc(%zu) FAILED!!!!",
542 sizeof (struct urb));
543 return NULL;
546 /* Fill in known fields */
547 memset (urb, 0, sizeof (struct urb));
548 urb->endpoint = endpoint;
549 urb->device = device;
550 urb->buffer = (u8 *) urb->buffer_data;
551 urb->buffer_length = sizeof (urb->buffer_data);
553 urb_link_init (&urb->link);
555 return urb;
559 * usbd_dealloc_urb - deallocate an URB and associated buffer
560 * @urb: pointer to an urb structure
562 * Deallocate an urb structure and associated data.
564 void usbd_dealloc_urb (struct urb *urb)
566 if (urb) {
567 free (urb);
571 /* Event signaling functions ***************************************************** */
574 * usbd_device_event - called to respond to various usb events
575 * @device: pointer to struct device
576 * @event: event to respond to
578 * Used by a Bus driver to indicate an event.
580 void usbd_device_event_irq (struct usb_device_instance *device, usb_device_event_t event, int data)
582 usb_device_state_t state;
584 if (!device || !device->bus) {
585 usberr("(%p,%d) NULL device or device->bus", device, event);
586 return;
589 state = device->device_state;
591 usbinfo("%s", usbd_device_events[event]);
593 switch (event) {
594 case DEVICE_UNKNOWN:
595 break;
596 case DEVICE_INIT:
597 device->device_state = STATE_INIT;
598 break;
600 case DEVICE_CREATE:
601 device->device_state = STATE_ATTACHED;
602 break;
604 case DEVICE_HUB_CONFIGURED:
605 device->device_state = STATE_POWERED;
606 break;
608 case DEVICE_RESET:
609 device->device_state = STATE_DEFAULT;
610 device->address = 0;
611 break;
613 case DEVICE_ADDRESS_ASSIGNED:
614 device->device_state = STATE_ADDRESSED;
615 break;
617 case DEVICE_CONFIGURED:
618 device->device_state = STATE_CONFIGURED;
619 break;
621 case DEVICE_DE_CONFIGURED:
622 device->device_state = STATE_ADDRESSED;
623 break;
625 case DEVICE_BUS_INACTIVE:
626 if (device->status != USBD_CLOSING) {
627 device->status = USBD_SUSPENDED;
629 break;
630 case DEVICE_BUS_ACTIVITY:
631 if (device->status != USBD_CLOSING) {
632 device->status = USBD_OK;
634 break;
636 case DEVICE_SET_INTERFACE:
637 break;
638 case DEVICE_SET_FEATURE:
639 break;
640 case DEVICE_CLEAR_FEATURE:
641 break;
643 case DEVICE_POWER_INTERRUPTION:
644 device->device_state = STATE_POWERED;
645 break;
646 case DEVICE_HUB_RESET:
647 device->device_state = STATE_ATTACHED;
648 break;
649 case DEVICE_DESTROY:
650 device->device_state = STATE_UNKNOWN;
651 break;
653 case DEVICE_FUNCTION_PRIVATE:
654 break;
656 default:
657 usbdbg("event %d - not handled",event);
658 break;
660 debug("%s event: %d oldstate: %d newstate: %d status: %d address: %d",
661 device->name, event, state,
662 device->device_state, device->status, device->address);
664 /* tell the bus interface driver */
665 if( device->event ) {
666 /* usbdbg("calling device->event"); */
667 device->event(device, event, data);