3 * Gerry Hamel, geh@ti.com, Texas Instruments
6 * linux/drivers/usbd/usbd.c.c - USB Device Core Layer
8 * Copyright (c) 2000, 2001, 2002 Lineo
9 * Copyright (c) 2001 Hewlett Packard
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.
33 #include <usbdevice.h>
35 #define MAX_INTERFACES 2
40 /* Global variables ************************************************************************** */
42 struct usb_string_descriptor **usb_strings;
46 extern struct usb_function_driver ep0_driver;
48 int registered_functions;
49 int registered_devices;
51 char *usbd_device_events[] = {
55 "DEVICE_HUB_CONFIGURED",
57 "DEVICE_ADDRESS_ASSIGNED",
59 "DEVICE_SET_INTERFACE",
61 "DEVICE_CLEAR_FEATURE",
62 "DEVICE_DE_CONFIGURED",
63 "DEVICE_BUS_INACTIVE",
64 "DEVICE_BUS_ACTIVITY",
65 "DEVICE_POWER_INTERRUPTION",
68 "DEVICE_FUNCTION_PRIVATE",
71 char *usbd_device_states[] = {
82 char *usbd_device_requests[] = {
84 "CLEAR FEATURE", /* 1 */
86 "SET FEATURE", /* 3 */
88 "SET ADDRESS", /* 5 */
89 "GET DESCRIPTOR", /* 6 */
90 "SET DESCRIPTOR", /* 7 */
91 "GET CONFIGURATION", /* 8 */
92 "SET CONFIGURATION", /* 9 */
93 "GET INTERFACE", /* 10 */
94 "SET INTERFACE", /* 11 */
95 "SYNC FRAME", /* 12 */
98 char *usbd_device_descriptors[] = {
105 "DEVICE QUALIFIER", /* 6 */
106 "OTHER SPEED", /* 7 */
107 "INTERFACE POWER", /* 8 */
110 char *usbd_device_status[] = {
118 /* Descriptor support functions ************************************************************** */
122 * usbd_get_string - find and return a string descriptor
123 * @index: string index to return
125 * Find an indexed string and return a pointer to a it.
127 struct usb_string_descriptor *usbd_get_string (__u8 index)
129 if (index >= maxstrings) {
132 return usb_strings[index];
136 /* Access to device descriptor functions ***************************************************** */
140 * usbd_device_configuration_instance - find a configuration instance for this device
142 * @configuration: index to configuration, 0 - N-1
144 * Get specifed device configuration. Index should be bConfigurationValue-1.
146 static struct usb_configuration_instance *usbd_device_configuration_instance (struct usb_device_instance *device,
147 unsigned int port, unsigned int configuration)
149 if (configuration >= device->configurations)
152 return device->configuration_instance_array + configuration;
157 * usbd_device_interface_instance
159 * @configuration: index to configuration, 0 - N-1
160 * @interface: index to interface
162 * Return the specified interface descriptor for the specified device.
164 struct usb_interface_instance *usbd_device_interface_instance (struct usb_device_instance *device, int port, int configuration, int interface)
166 struct usb_configuration_instance *configuration_instance;
168 if ((configuration_instance = usbd_device_configuration_instance (device, port, configuration)) == NULL) {
171 if (interface >= configuration_instance->interfaces) {
174 return configuration_instance->interface_instance_array + interface;
178 * usbd_device_alternate_descriptor_list
180 * @configuration: index to configuration, 0 - N-1
181 * @interface: index to interface
182 * @alternate: alternate setting
184 * Return the specified alternate descriptor for the specified device.
186 struct usb_alternate_instance *usbd_device_alternate_instance (struct usb_device_instance *device, int port, int configuration, int interface, int alternate)
188 struct usb_interface_instance *interface_instance;
190 if ((interface_instance = usbd_device_interface_instance (device, port, configuration, interface)) == NULL) {
194 if (alternate >= interface_instance->alternates) {
198 return interface_instance->alternates_instance_array + alternate;
203 * usbd_device_device_descriptor
204 * @device: which device
205 * @configuration: index to configuration, 0 - N-1
208 * Return the specified configuration descriptor for the specified device.
210 struct usb_device_descriptor *usbd_device_device_descriptor (struct usb_device_instance *device, int port)
212 return (device->device_descriptor);
216 * usbd_device_configuration_descriptor
217 * @device: which device
219 * @configuration: index to configuration, 0 - N-1
221 * Return the specified configuration descriptor for the specified device.
223 struct usb_configuration_descriptor *usbd_device_configuration_descriptor (struct
225 *device, int port, int configuration)
227 struct usb_configuration_instance *configuration_instance;
228 if (!(configuration_instance = usbd_device_configuration_instance (device, port, configuration))) {
231 return (configuration_instance->configuration_descriptor);
236 * usbd_device_interface_descriptor
237 * @device: which device
239 * @configuration: index to configuration, 0 - N-1
240 * @interface: index to interface
241 * @alternate: alternate setting
243 * Return the specified interface descriptor for the specified device.
245 struct usb_interface_descriptor *usbd_device_interface_descriptor (struct usb_device_instance
246 *device, int port, int configuration, int interface, int alternate)
248 struct usb_interface_instance *interface_instance;
249 if (!(interface_instance = usbd_device_interface_instance (device, port, configuration, interface))) {
252 if ((alternate < 0) || (alternate >= interface_instance->alternates)) {
255 return (interface_instance->alternates_instance_array[alternate].interface_descriptor);
259 * usbd_device_endpoint_descriptor_index
260 * @device: which device
262 * @configuration: index to configuration, 0 - N-1
263 * @interface: index to interface
264 * @alternate: index setting
265 * @index: which index
267 * Return the specified endpoint descriptor for the specified device.
269 struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor_index (struct usb_device_instance
270 *device, int port, int configuration, int interface, int alternate, int index)
272 struct usb_alternate_instance *alternate_instance;
274 if (!(alternate_instance = usbd_device_alternate_instance (device, port, configuration, interface, alternate))) {
277 if (index >= alternate_instance->endpoints) {
280 return *(alternate_instance->endpoints_descriptor_array + index);
285 * usbd_device_endpoint_transfersize
286 * @device: which device
288 * @configuration: index to configuration, 0 - N-1
289 * @interface: index to interface
290 * @index: which index
292 * Return the specified endpoint transfer size;
294 int usbd_device_endpoint_transfersize (struct usb_device_instance *device, int port, int configuration, int interface, int alternate, int index)
296 struct usb_alternate_instance *alternate_instance;
298 if (!(alternate_instance = usbd_device_alternate_instance (device, port, configuration, interface, alternate))) {
301 if (index >= alternate_instance->endpoints) {
304 return *(alternate_instance->endpoint_transfersize_array + index);
309 * usbd_device_endpoint_descriptor
310 * @device: which device
312 * @configuration: index to configuration, 0 - N-1
313 * @interface: index to interface
314 * @alternate: alternate setting
315 * @endpoint: which endpoint
317 * Return the specified endpoint descriptor for the specified device.
319 struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor (struct usb_device_instance *device, int port, int configuration, int interface, int alternate, int endpoint)
321 struct usb_endpoint_descriptor *endpoint_descriptor;
324 for (i = 0; !(endpoint_descriptor = usbd_device_endpoint_descriptor_index (device, port, configuration, interface, alternate, i)); i++) {
325 if (endpoint_descriptor->bEndpointAddress == endpoint) {
326 return endpoint_descriptor;
333 * usbd_endpoint_halted
334 * @device: point to struct usb_device_instance
335 * @endpoint: endpoint to check
337 * Return non-zero if endpoint is halted.
339 int usbd_endpoint_halted (struct usb_device_instance *device, int endpoint)
341 return (device->status == USB_STATUS_HALT);
346 * usbd_rcv_complete - complete a receive
351 * Called from rcv interrupt to complete.
353 void usbd_rcv_complete(struct usb_endpoint_instance *endpoint, int len, int urb_bad)
358 /*usbdbg("len: %d urb: %p\n", len, endpoint->rcv_urb); */
360 /* if we had an urb then update actual_length, dispatch if neccessary */
361 if ((rcv_urb = endpoint->rcv_urb)) {
363 /*usbdbg("actual: %d buffer: %d\n", */
364 /*rcv_urb->actual_length, rcv_urb->buffer_length); */
366 /* check the urb is ok, are we adding data less than the packetsize */
367 if (!urb_bad && (len <= endpoint->rcv_packetSize)) {
368 /*usbdbg("updating actual_length by %d\n",len); */
370 /* increment the received data size */
371 rcv_urb->actual_length += len;
374 usberr(" RECV_ERROR actual: %d buffer: %d urb_bad: %d\n",
375 rcv_urb->actual_length, rcv_urb->buffer_length, urb_bad);
377 rcv_urb->actual_length = 0;
378 rcv_urb->status = RECV_ERROR;
381 usberr("no rcv_urb!");
384 usberr("no endpoint!");
390 * usbd_tx_complete - complete a transmit
394 * Called from tx interrupt to complete.
396 void usbd_tx_complete (struct usb_endpoint_instance *endpoint)
401 /* if we have a tx_urb advance or reset, finish if complete */
402 if ((tx_urb = endpoint->tx_urb)) {
403 int sent = endpoint->last;
404 endpoint->sent += sent;
405 endpoint->last -= sent;
407 if( (endpoint->tx_urb->actual_length - endpoint->sent) <= 0 ) {
408 tx_urb->actual_length = 0;
412 /* Remove from active, save for re-use */
414 urb_append(&endpoint->done, tx_urb);
415 /*usbdbg("done->next %p, tx_urb %p, done %p", */
416 /* endpoint->done.next, tx_urb, &endpoint->done); */
418 endpoint->tx_urb = first_urb_detached(&endpoint->tx);
419 if( endpoint->tx_urb ) {
420 endpoint->tx_queue--;
421 usbdbg("got urb from tx list");
423 if( !endpoint->tx_urb ) {
424 /*usbdbg("taking urb from done list"); */
425 endpoint->tx_urb = first_urb_detached(&endpoint->done);
427 if( !endpoint->tx_urb ) {
428 usbdbg("allocating new urb for tx_urb");
429 endpoint->tx_urb = usbd_alloc_urb(tx_urb->device, endpoint);
436 /* URB linked list functions ***************************************************** */
439 * Initialize an urb_link to be a single element list.
440 * If the urb_link is being used as a distinguished list head
441 * the list is empty when the head is the only link in the list.
443 void urb_link_init (urb_link * ul)
446 ul->prev = ul->next = ul;
451 * Detach an urb_link from a list, and set it
452 * up as a single element list, so no dangling
453 * pointers can be followed, and so it can be
454 * joined to another list if so desired.
456 void urb_detach (struct urb *urb)
459 urb_link *ul = &urb->link;
460 ul->next->prev = ul->prev;
461 ul->prev->next = ul->next;
467 * Return the first urb_link in a list with a distinguished
468 * head "hd", or NULL if the list is empty. This will also
469 * work as a predicate, returning NULL if empty, and non-NULL
472 urb_link *first_urb_link (urb_link * hd)
475 if (NULL != hd && NULL != (nx = hd->next) && nx != hd) {
476 /* There is at least one element in the list */
477 /* (besides the distinguished head). */
480 /* The list is empty */
485 * Return the first urb in a list with a distinguished
486 * head "hd", or NULL if the list is empty.
488 struct urb *first_urb (urb_link * hd)
491 if (NULL == (nx = first_urb_link (hd))) {
492 /* The list is empty */
495 return (p2surround (struct urb, link, nx));
499 * Detach and return the first urb in a list with a distinguished
500 * head "hd", or NULL if the list is empty.
503 struct urb *first_urb_detached (urb_link * hd)
506 if ((urb = first_urb (hd))) {
514 * Append an urb_link (or a whole list of
515 * urb_links) to the tail of another list
518 void urb_append (urb_link * hd, struct urb *urb)
521 urb_link *new = &urb->link;
523 /* This allows the new urb to be a list of urbs, */
524 /* with new pointing at the first, but the link */
525 /* must be initialized. */
526 /* Order is important here... */
527 urb_link *pul = hd->prev;
528 new->prev->next = hd;
529 hd->prev = new->prev;
535 /* URB create/destroy functions ***************************************************** */
538 * usbd_alloc_urb - allocate an URB appropriate for specified endpoint
539 * @device: device instance
540 * @endpoint: endpoint
542 * Allocate an urb structure. The usb device urb structure is used to
543 * contain all data associated with a transfer, including a setup packet for
546 * NOTE: endpoint_address MUST contain a direction flag.
548 struct urb *usbd_alloc_urb (struct usb_device_instance *device,
549 struct usb_endpoint_instance *endpoint)
553 if (!(urb = (struct urb *) malloc (sizeof (struct urb)))) {
554 usberr (" F A T A L: malloc(%zu) FAILED!!!!",
555 sizeof (struct urb));
559 /* Fill in known fields */
560 memset (urb, 0, sizeof (struct urb));
561 urb->endpoint = endpoint;
562 urb->device = device;
563 urb->buffer = (u8 *) urb->buffer_data;
564 urb->buffer_length = sizeof (urb->buffer_data);
566 urb_link_init (&urb->link);
572 * usbd_dealloc_urb - deallocate an URB and associated buffer
573 * @urb: pointer to an urb structure
575 * Deallocate an urb structure and associated data.
577 void usbd_dealloc_urb (struct urb *urb)
584 /* Event signaling functions ***************************************************** */
587 * usbd_device_event - called to respond to various usb events
588 * @device: pointer to struct device
589 * @event: event to respond to
591 * Used by a Bus driver to indicate an event.
593 void usbd_device_event_irq (struct usb_device_instance *device, usb_device_event_t event, int data)
595 usb_device_state_t state;
597 if (!device || !device->bus) {
598 usberr("(%p,%d) NULL device or device->bus", device, event);
602 state = device->device_state;
604 usbinfo("%s", usbd_device_events[event]);
610 device->device_state = STATE_INIT;
614 device->device_state = STATE_ATTACHED;
617 case DEVICE_HUB_CONFIGURED:
618 device->device_state = STATE_POWERED;
622 device->device_state = STATE_DEFAULT;
626 case DEVICE_ADDRESS_ASSIGNED:
627 device->device_state = STATE_ADDRESSED;
630 case DEVICE_CONFIGURED:
631 device->device_state = STATE_CONFIGURED;
634 case DEVICE_DE_CONFIGURED:
635 device->device_state = STATE_ADDRESSED;
638 case DEVICE_BUS_INACTIVE:
639 if (device->status != USBD_CLOSING) {
640 device->status = USBD_SUSPENDED;
643 case DEVICE_BUS_ACTIVITY:
644 if (device->status != USBD_CLOSING) {
645 device->status = USBD_OK;
649 case DEVICE_SET_INTERFACE:
651 case DEVICE_SET_FEATURE:
653 case DEVICE_CLEAR_FEATURE:
656 case DEVICE_POWER_INTERRUPTION:
657 device->device_state = STATE_POWERED;
659 case DEVICE_HUB_RESET:
660 device->device_state = STATE_ATTACHED;
663 device->device_state = STATE_UNKNOWN;
666 case DEVICE_FUNCTION_PRIVATE:
670 usbdbg("event %d - not handled",event);
673 debug("%s event: %d oldstate: %d newstate: %d status: %d address: %d",
674 device->name, event, state,
675 device->device_state, device->status, device->address);
677 /* tell the bus interface driver */
678 if( device->event ) {
679 /* usbdbg("calling device->event"); */
680 device->event(device, event, data);