usbdcore-multiple_configs.patch
[u-boot-openmoko/mini2440.git] / drivers / usb / usbdcore.c
bloba2e671156de7762428ce7b2c1d428f658dcab165
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 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 #include <malloc.h>
33 #include "usbdcore.h"
34 #include <usb_dfu.h>
36 #define MAX_INTERFACES 2
39 int maxstrings = 20;
41 /* Global variables ************************************************************************** */
43 struct usb_string_descriptor **usb_strings;
45 int usb_devices;
47 extern struct usb_function_driver ep0_driver;
49 int registered_functions;
50 int registered_devices;
52 char *usbd_device_events[] = {
53 "DEVICE_UNKNOWN",
54 "DEVICE_INIT",
55 "DEVICE_CREATE",
56 "DEVICE_HUB_CONFIGURED",
57 "DEVICE_RESET",
58 "DEVICE_ADDRESS_ASSIGNED",
59 "DEVICE_CONFIGURED",
60 "DEVICE_SET_INTERFACE",
61 "DEVICE_SET_FEATURE",
62 "DEVICE_CLEAR_FEATURE",
63 "DEVICE_DE_CONFIGURED",
64 "DEVICE_BUS_INACTIVE",
65 "DEVICE_BUS_ACTIVITY",
66 "DEVICE_POWER_INTERRUPTION",
67 "DEVICE_HUB_RESET",
68 "DEVICE_DESTROY",
69 "DEVICE_FUNCTION_PRIVATE",
72 char *usbd_device_states[] = {
73 "STATE_INIT",
74 "STATE_CREATED",
75 "STATE_ATTACHED",
76 "STATE_POWERED",
77 "STATE_DEFAULT",
78 "STATE_ADDRESSED",
79 "STATE_CONFIGURED",
80 "STATE_UNKNOWN",
83 char *usbd_device_requests[] = {
84 "GET STATUS", /* 0 */
85 "CLEAR FEATURE", /* 1 */
86 "RESERVED", /* 2 */
87 "SET FEATURE", /* 3 */
88 "RESERVED", /* 4 */
89 "SET ADDRESS", /* 5 */
90 "GET DESCRIPTOR", /* 6 */
91 "SET DESCRIPTOR", /* 7 */
92 "GET CONFIGURATION", /* 8 */
93 "SET CONFIGURATION", /* 9 */
94 "GET INTERFACE", /* 10 */
95 "SET INTERFACE", /* 11 */
96 "SYNC FRAME", /* 12 */
99 char *usbd_device_descriptors[] = {
100 "UNKNOWN", /* 0 */
101 "DEVICE", /* 1 */
102 "CONFIG", /* 2 */
103 "STRING", /* 3 */
104 "INTERFACE", /* 4 */
105 "ENDPOINT", /* 5 */
106 "DEVICE QUALIFIER", /* 6 */
107 "OTHER SPEED", /* 7 */
108 "INTERFACE POWER", /* 8 */
111 char *usbd_device_status[] = {
112 "USBD_OPENING",
113 "USBD_OK",
114 "USBD_SUSPENDED",
115 "USBD_CLOSING",
119 /* Descriptor support functions ************************************************************** */
123 * usbd_get_string - find and return a string descriptor
124 * @index: string index to return
126 * Find an indexed string and return a pointer to a it.
128 struct usb_string_descriptor *usbd_get_string (__u8 index)
130 if (index >= maxstrings) {
131 return NULL;
133 return usb_strings[index];
137 /* Access to device descriptor functions ***************************************************** */
140 /* *
141 * usbd_device_configuration_instance - find a configuration instance for this device
142 * @device:
143 * @configuration: index to configuration, 0 - N-1
145 * Get specifed device configuration. Index should be bConfigurationValue-1.
147 static struct usb_configuration_instance *usbd_device_configuration_instance (struct usb_device_instance *device,
148 unsigned int port, unsigned int configuration)
150 if (configuration >= device->configurations)
151 return NULL;
153 return device->configuration_instance_array + configuration;
157 /* *
158 * usbd_device_interface_instance
159 * @device:
160 * @configuration: index to configuration, 0 - N-1
161 * @interface: index to interface
163 * Return the specified interface descriptor for the specified device.
165 struct usb_interface_instance *usbd_device_interface_instance (struct usb_device_instance *device, int port, int configuration, int interface)
167 struct usb_configuration_instance *configuration_instance;
169 if ((configuration_instance = usbd_device_configuration_instance (device, port, configuration)) == NULL) {
170 return NULL;
172 if (interface >= configuration_instance->interfaces) {
173 return NULL;
175 return configuration_instance->interface_instance_array + interface;
178 /* *
179 * usbd_device_alternate_descriptor_list
180 * @device:
181 * @configuration: index to configuration, 0 - N-1
182 * @interface: index to interface
183 * @alternate: alternate setting
185 * Return the specified alternate descriptor for the specified device.
187 struct usb_alternate_instance *usbd_device_alternate_instance (struct usb_device_instance *device, int port, int configuration, int interface, int alternate)
189 struct usb_interface_instance *interface_instance;
191 if ((interface_instance = usbd_device_interface_instance (device, port, configuration, interface)) == NULL) {
192 return NULL;
195 if (alternate >= interface_instance->alternates) {
196 return NULL;
199 return interface_instance->alternates_instance_array + alternate;
203 /* *
204 * usbd_device_device_descriptor
205 * @device: which device
206 * @configuration: index to configuration, 0 - N-1
207 * @port: which port
209 * Return the specified configuration descriptor for the specified device.
211 struct usb_device_descriptor *usbd_device_device_descriptor (struct usb_device_instance *device, int port)
213 #ifdef CONFIG_USBD_DFU
214 if (device->dfu_state != DFU_STATE_appIDLE)
215 return device->dfu_dev_desc;
216 #endif
217 return (device->device_descriptor);
222 * usbd_device_configuration_descriptor
223 * @device: which device
224 * @port: which port
225 * @configuration: index to configuration, 0 - N-1
227 * Return the specified configuration descriptor for the specified device.
229 struct usb_configuration_descriptor *usbd_device_configuration_descriptor (struct
230 usb_device_instance
231 *device, int port, int configuration)
233 struct usb_configuration_instance *configuration_instance;
234 if (!(configuration_instance = usbd_device_configuration_instance (device, port, configuration))) {
235 return NULL;
237 #ifdef CONFIG_USBD_DFU
238 if (device->dfu_state != DFU_STATE_appIDLE)
239 return (&device->dfu_cfg_desc->ucfg);
240 #endif
241 return (configuration_instance->configuration_descriptor);
246 * usbd_device_interface_descriptor
247 * @device: which device
248 * @port: which port
249 * @configuration: index to configuration, 0 - N-1
250 * @interface: index to interface
251 * @alternate: alternate setting
253 * Return the specified interface descriptor for the specified device.
255 struct usb_interface_descriptor *usbd_device_interface_descriptor (struct usb_device_instance
256 *device, int port, int configuration, int interface, int alternate)
258 struct usb_interface_instance *interface_instance;
259 if (!(interface_instance = usbd_device_interface_instance (device, port, configuration, interface))) {
260 return NULL;
262 #ifdef CONFIG_USBD_DFU
263 if (device->dfu_state != DFU_STATE_appIDLE) {
264 if (alternate < 0 || alternate >= DFU_NUM_ALTERNATES)
265 return NULL;
266 return &device->dfu_cfg_desc->uif[alternate];
268 #endif
269 if ((alternate < 0) || (alternate >= interface_instance->alternates)) {
270 return NULL;
272 return (interface_instance->alternates_instance_array[alternate].interface_descriptor);
276 * usbd_device_endpoint_descriptor_index
277 * @device: which device
278 * @port: which port
279 * @configuration: index to configuration, 0 - N-1
280 * @interface: index to interface
281 * @alternate: index setting
282 * @index: which index
284 * Return the specified endpoint descriptor for the specified device.
286 struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor_index (struct usb_device_instance
287 *device, int port, int configuration, int interface, int alternate, int index)
289 struct usb_alternate_instance *alternate_instance;
291 if (!(alternate_instance = usbd_device_alternate_instance (device, port, configuration, interface, alternate))) {
292 return NULL;
294 if (index >= alternate_instance->endpoints) {
295 return NULL;
297 return *(alternate_instance->endpoints_descriptor_array + index);
302 * usbd_device_endpoint_transfersize
303 * @device: which device
304 * @port: which port
305 * @configuration: index to configuration, 0 - N-1
306 * @interface: index to interface
307 * @index: which index
309 * Return the specified endpoint transfer size;
311 int usbd_device_endpoint_transfersize (struct usb_device_instance *device, int port, int configuration, int interface, int alternate, int index)
313 struct usb_alternate_instance *alternate_instance;
315 if (!(alternate_instance = usbd_device_alternate_instance (device, port, configuration, interface, alternate))) {
316 return 0;
318 if (index >= alternate_instance->endpoints) {
319 return 0;
321 return *(alternate_instance->endpoint_transfersize_array + index);
326 * usbd_device_endpoint_descriptor
327 * @device: which device
328 * @port: which port
329 * @configuration: index to configuration, 0 - N-1
330 * @interface: index to interface
331 * @alternate: alternate setting
332 * @endpoint: which endpoint
334 * Return the specified endpoint descriptor for the specified device.
336 struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor (struct usb_device_instance *device, int port, int configuration, int interface, int alternate, int endpoint)
338 struct usb_endpoint_descriptor *endpoint_descriptor;
339 int i;
341 for (i = 0; !(endpoint_descriptor = usbd_device_endpoint_descriptor_index (device, port, configuration, interface, alternate, i)); i++) {
342 if (endpoint_descriptor->bEndpointAddress == endpoint) {
343 return endpoint_descriptor;
346 return NULL;
350 * usbd_endpoint_halted
351 * @device: point to struct usb_device_instance
352 * @endpoint: endpoint to check
354 * Return non-zero if endpoint is halted.
356 int usbd_endpoint_halted (struct usb_device_instance *device, int endpoint)
358 return (device->status == USB_STATUS_HALT);
363 * usbd_rcv_complete - complete a receive
364 * @endpoint:
365 * @len:
366 * @urb_bad:
368 * Called from rcv interrupt to complete.
370 void usbd_rcv_complete(struct usb_endpoint_instance *endpoint, int len, int urb_bad)
372 if (endpoint) {
373 struct urb *rcv_urb;
375 /*usbdbg("len: %d urb: %p\n", len, endpoint->rcv_urb); */
377 /* if we had an urb then update actual_length, dispatch if neccessary */
378 if ((rcv_urb = endpoint->rcv_urb)) {
380 /*usbdbg("actual: %d buffer: %d\n", */
381 /*rcv_urb->actual_length, rcv_urb->buffer_length); */
383 /* check the urb is ok, are we adding data less than the packetsize */
384 if (!urb_bad && (len <= endpoint->rcv_packetSize)) {
385 /*usbdbg("updating actual_length by %d\n",len); */
387 /* increment the received data size */
388 rcv_urb->actual_length += len;
390 } else {
391 usberr(" RECV_ERROR actual: %d buffer: %d urb_bad: %d\n",
392 rcv_urb->actual_length, rcv_urb->buffer_length, urb_bad);
394 rcv_urb->actual_length = 0;
395 rcv_urb->status = RECV_ERROR;
397 } else {
398 usberr("no rcv_urb!");
400 } else {
401 usberr("no endpoint!");
407 * usbd_tx_complete - complete a transmit
408 * @endpoint:
409 * @resetart:
411 * Called from tx interrupt to complete.
413 void usbd_tx_complete (struct usb_endpoint_instance *endpoint)
415 if (endpoint) {
416 struct urb *tx_urb;
418 /* if we have a tx_urb advance or reset, finish if complete */
419 if ((tx_urb = endpoint->tx_urb)) {
420 int sent = endpoint->last;
421 endpoint->sent += sent;
422 endpoint->last -= sent;
424 if( (endpoint->tx_urb->actual_length - endpoint->sent) <= 0 ) {
425 tx_urb->actual_length = 0;
426 endpoint->sent = 0;
427 endpoint->last = 0;
429 /* Remove from active, save for re-use */
430 urb_detach(tx_urb);
431 urb_append(&endpoint->done, tx_urb);
432 /*usbdbg("done->next %p, tx_urb %p, done %p", */
433 /* endpoint->done.next, tx_urb, &endpoint->done); */
435 endpoint->tx_urb = first_urb_detached(&endpoint->tx);
436 if( endpoint->tx_urb ) {
437 endpoint->tx_queue--;
438 usbdbg("got urb from tx list");
440 if( !endpoint->tx_urb ) {
441 /*usbdbg("taking urb from done list"); */
442 endpoint->tx_urb = first_urb_detached(&endpoint->done);
444 if( !endpoint->tx_urb ) {
445 usbdbg("allocating new urb for tx_urb");
446 endpoint->tx_urb = usbd_alloc_urb(tx_urb->device, endpoint);
453 /* URB linked list functions ***************************************************** */
456 * Initialize an urb_link to be a single element list.
457 * If the urb_link is being used as a distinguished list head
458 * the list is empty when the head is the only link in the list.
460 void urb_link_init (urb_link * ul)
462 if (ul) {
463 ul->prev = ul->next = ul;
468 * Detach an urb_link from a list, and set it
469 * up as a single element list, so no dangling
470 * pointers can be followed, and so it can be
471 * joined to another list if so desired.
473 void urb_detach (struct urb *urb)
475 if (urb) {
476 urb_link *ul = &urb->link;
477 ul->next->prev = ul->prev;
478 ul->prev->next = ul->next;
479 urb_link_init (ul);
484 * Return the first urb_link in a list with a distinguished
485 * head "hd", or NULL if the list is empty. This will also
486 * work as a predicate, returning NULL if empty, and non-NULL
487 * otherwise.
489 urb_link *first_urb_link (urb_link * hd)
491 urb_link *nx;
492 if (NULL != hd && NULL != (nx = hd->next) && nx != hd) {
493 /* There is at least one element in the list */
494 /* (besides the distinguished head). */
495 return (nx);
497 /* The list is empty */
498 return (NULL);
502 * Return the first urb in a list with a distinguished
503 * head "hd", or NULL if the list is empty.
505 struct urb *first_urb (urb_link * hd)
507 urb_link *nx;
508 if (NULL == (nx = first_urb_link (hd))) {
509 /* The list is empty */
510 return (NULL);
512 return (p2surround (struct urb, link, nx));
516 * Detach and return the first urb in a list with a distinguished
517 * head "hd", or NULL if the list is empty.
520 struct urb *first_urb_detached (urb_link * hd)
522 struct urb *urb;
523 if ((urb = first_urb (hd))) {
524 urb_detach (urb);
526 return urb;
531 * Append an urb_link (or a whole list of
532 * urb_links) to the tail of another list
533 * of urb_links.
535 void urb_append (urb_link * hd, struct urb *urb)
537 if (hd && urb) {
538 urb_link *new = &urb->link;
540 /* This allows the new urb to be a list of urbs, */
541 /* with new pointing at the first, but the link */
542 /* must be initialized. */
543 /* Order is important here... */
544 urb_link *pul = hd->prev;
545 new->prev->next = hd;
546 hd->prev = new->prev;
547 new->prev = pul;
548 pul->next = new;
552 /* URB create/destroy functions ***************************************************** */
555 * usbd_alloc_urb - allocate an URB appropriate for specified endpoint
556 * @device: device instance
557 * @endpoint: endpoint
559 * Allocate an urb structure. The usb device urb structure is used to
560 * contain all data associated with a transfer, including a setup packet for
561 * control transfers.
563 * NOTE: endpoint_address MUST contain a direction flag.
565 struct urb *usbd_alloc_urb (struct usb_device_instance *device, struct usb_endpoint_instance *endpoint)
567 struct urb *urb;
569 if( !(urb = (struct urb*)malloc(sizeof(struct urb))) ) {
570 usberr(" F A T A L: malloc(%u) FAILED!!!!", sizeof(struct urb));
571 return NULL;
574 /* Fill in known fields */
575 memset(urb, 0, sizeof(struct urb));
576 urb->endpoint = endpoint;
577 urb->device = device;
578 urb->buffer = (u8*)urb->buffer_data;
579 urb->buffer_length = sizeof(urb->buffer_data);
581 urb_link_init (&urb->link);
583 return urb;
587 * usbd_dealloc_urb - deallocate an URB and associated buffer
588 * @urb: pointer to an urb structure
590 * Deallocate an urb structure and associated data.
592 void usbd_dealloc_urb (struct urb *urb)
594 if (urb) {
595 free (urb);
599 /* Event signaling functions ***************************************************** */
602 * usbd_device_event - called to respond to various usb events
603 * @device: pointer to struct device
604 * @event: event to respond to
606 * Used by a Bus driver to indicate an event.
608 void usbd_device_event_irq (struct usb_device_instance *device, usb_device_event_t event, int data)
610 usb_device_state_t state;
612 if (!device || !device->bus) {
613 usberr("(%p,%d) NULL device or device->bus", device, event);
614 return;
617 state = device->device_state;
619 usbinfo("%s", usbd_device_events[event]);
621 switch (event) {
622 case DEVICE_UNKNOWN:
623 break;
624 case DEVICE_INIT:
625 device->device_state = STATE_INIT;
626 break;
628 case DEVICE_CREATE:
629 device->device_state = STATE_ATTACHED;
630 break;
632 case DEVICE_HUB_CONFIGURED:
633 device->device_state = STATE_POWERED;
634 break;
636 case DEVICE_RESET:
637 device->device_state = STATE_DEFAULT;
638 device->address = 0;
639 break;
641 case DEVICE_ADDRESS_ASSIGNED:
642 device->device_state = STATE_ADDRESSED;
643 break;
645 case DEVICE_CONFIGURED:
646 device->device_state = STATE_CONFIGURED;
647 break;
649 case DEVICE_DE_CONFIGURED:
650 device->device_state = STATE_ADDRESSED;
651 break;
653 case DEVICE_BUS_INACTIVE:
654 if (device->status != USBD_CLOSING) {
655 device->status = USBD_SUSPENDED;
657 break;
658 case DEVICE_BUS_ACTIVITY:
659 if (device->status != USBD_CLOSING) {
660 device->status = USBD_OK;
662 break;
664 case DEVICE_SET_INTERFACE:
665 break;
666 case DEVICE_SET_FEATURE:
667 break;
668 case DEVICE_CLEAR_FEATURE:
669 break;
671 case DEVICE_POWER_INTERRUPTION:
672 device->device_state = STATE_POWERED;
673 break;
674 case DEVICE_HUB_RESET:
675 device->device_state = STATE_ATTACHED;
676 break;
677 case DEVICE_DESTROY:
678 device->device_state = STATE_UNKNOWN;
679 break;
681 case DEVICE_FUNCTION_PRIVATE:
682 break;
684 default:
685 usbdbg("event %d - not handled",event);
686 break;
688 /*usbdbg("%s event: %d oldstate: %d newstate: %d status: %d address: %d",
689 device->name, event, state,
690 device->device_state, device->status, device->address); */
692 /* tell the bus interface driver */
693 if( device->event ) {
694 /* usbdbg("calling device->event"); */
695 device->event(device, event, data);
697 #ifdef CONFIG_USBD_DFU
698 dfu_event(device, event, data);
699 #endif