1 // SPDX-License-Identifier: GPL-2.0+
4 * Gerry Hamel, geh@ti.com, Texas Instruments
7 * linux/drivers/usbd/usbd.c.c - USB Device Core Layer
9 * Copyright (c) 2000, 2001, 2002 Lineo
10 * Copyright (c) 2001 Hewlett Packard
13 * Stuart Lynne <sl@lineo.com>,
14 * Tom Rushworth <tbr@lineo.com>,
15 * Bruce Balden <balden@lineo.com>
19 #include <usbdevice.h>
21 #define MAX_INTERFACES 2
26 /* Global variables ************************************************************************** */
28 struct usb_string_descriptor **usb_strings;
32 extern struct usb_function_driver ep0_driver;
34 int registered_functions;
35 int registered_devices;
37 char *usbd_device_events[] = {
41 "DEVICE_HUB_CONFIGURED",
43 "DEVICE_ADDRESS_ASSIGNED",
45 "DEVICE_SET_INTERFACE",
47 "DEVICE_CLEAR_FEATURE",
48 "DEVICE_DE_CONFIGURED",
49 "DEVICE_BUS_INACTIVE",
50 "DEVICE_BUS_ACTIVITY",
51 "DEVICE_POWER_INTERRUPTION",
54 "DEVICE_FUNCTION_PRIVATE",
57 char *usbd_device_states[] = {
68 char *usbd_device_requests[] = {
70 "CLEAR FEATURE", /* 1 */
72 "SET FEATURE", /* 3 */
74 "SET ADDRESS", /* 5 */
75 "GET DESCRIPTOR", /* 6 */
76 "SET DESCRIPTOR", /* 7 */
77 "GET CONFIGURATION", /* 8 */
78 "SET CONFIGURATION", /* 9 */
79 "GET INTERFACE", /* 10 */
80 "SET INTERFACE", /* 11 */
81 "SYNC FRAME", /* 12 */
84 char *usbd_device_descriptors[] = {
91 "DEVICE QUALIFIER", /* 6 */
92 "OTHER SPEED", /* 7 */
93 "INTERFACE POWER", /* 8 */
96 char *usbd_device_status[] = {
104 /* Descriptor support functions ************************************************************** */
108 * usbd_get_string - find and return a string descriptor
109 * @index: string index to return
111 * Find an indexed string and return a pointer to a it.
113 struct usb_string_descriptor *usbd_get_string (__u8 index)
115 if (index >= maxstrings) {
118 return usb_strings[index];
122 /* Access to device descriptor functions ***************************************************** */
126 * usbd_device_configuration_instance - find a configuration instance for this device
128 * @configuration: index to configuration, 0 - N-1
130 * Get specifed device configuration. Index should be bConfigurationValue-1.
132 static struct usb_configuration_instance *usbd_device_configuration_instance (struct usb_device_instance *device,
133 unsigned int port, unsigned int configuration)
135 if (configuration >= device->configurations)
138 return device->configuration_instance_array + configuration;
143 * usbd_device_interface_instance
145 * @configuration: index to configuration, 0 - N-1
146 * @interface: index to interface
148 * Return the specified interface descriptor for the specified device.
150 struct usb_interface_instance *usbd_device_interface_instance (struct usb_device_instance *device, int port, int configuration, int interface)
152 struct usb_configuration_instance *configuration_instance;
154 if ((configuration_instance = usbd_device_configuration_instance (device, port, configuration)) == NULL) {
157 if (interface >= configuration_instance->interfaces) {
160 return configuration_instance->interface_instance_array + interface;
164 * usbd_device_alternate_descriptor_list
166 * @configuration: index to configuration, 0 - N-1
167 * @interface: index to interface
168 * @alternate: alternate setting
170 * Return the specified alternate descriptor for the specified device.
172 struct usb_alternate_instance *usbd_device_alternate_instance (struct usb_device_instance *device, int port, int configuration, int interface, int alternate)
174 struct usb_interface_instance *interface_instance;
176 if ((interface_instance = usbd_device_interface_instance (device, port, configuration, interface)) == NULL) {
180 if (alternate >= interface_instance->alternates) {
184 return interface_instance->alternates_instance_array + alternate;
189 * usbd_device_device_descriptor
190 * @device: which device
191 * @configuration: index to configuration, 0 - N-1
194 * Return the specified configuration descriptor for the specified device.
196 struct usb_device_descriptor *usbd_device_device_descriptor (struct usb_device_instance *device, int port)
198 return (device->device_descriptor);
202 * usbd_device_configuration_descriptor
203 * @device: which device
205 * @configuration: index to configuration, 0 - N-1
207 * Return the specified configuration descriptor for the specified device.
209 struct usb_configuration_descriptor *usbd_device_configuration_descriptor (struct
211 *device, int port, int configuration)
213 struct usb_configuration_instance *configuration_instance;
214 if (!(configuration_instance = usbd_device_configuration_instance (device, port, configuration))) {
217 return (configuration_instance->configuration_descriptor);
222 * usbd_device_interface_descriptor
223 * @device: which device
225 * @configuration: index to configuration, 0 - N-1
226 * @interface: index to interface
227 * @alternate: alternate setting
229 * Return the specified interface descriptor for the specified device.
231 struct usb_interface_descriptor *usbd_device_interface_descriptor (struct usb_device_instance
232 *device, int port, int configuration, int interface, int alternate)
234 struct usb_interface_instance *interface_instance;
235 if (!(interface_instance = usbd_device_interface_instance (device, port, configuration, interface))) {
238 if ((alternate < 0) || (alternate >= interface_instance->alternates)) {
241 return (interface_instance->alternates_instance_array[alternate].interface_descriptor);
245 * usbd_device_endpoint_descriptor_index
246 * @device: which device
248 * @configuration: index to configuration, 0 - N-1
249 * @interface: index to interface
250 * @alternate: index setting
251 * @index: which index
253 * Return the specified endpoint descriptor for the specified device.
255 struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor_index (struct usb_device_instance
256 *device, int port, int configuration, int interface, int alternate, int index)
258 struct usb_alternate_instance *alternate_instance;
260 if (!(alternate_instance = usbd_device_alternate_instance (device, port, configuration, interface, alternate))) {
263 if (index >= alternate_instance->endpoints) {
266 return *(alternate_instance->endpoints_descriptor_array + index);
271 * usbd_device_endpoint_transfersize
272 * @device: which device
274 * @configuration: index to configuration, 0 - N-1
275 * @interface: index to interface
276 * @index: which index
278 * Return the specified endpoint transfer size;
280 int usbd_device_endpoint_transfersize (struct usb_device_instance *device, int port, int configuration, int interface, int alternate, int index)
282 struct usb_alternate_instance *alternate_instance;
284 if (!(alternate_instance = usbd_device_alternate_instance (device, port, configuration, interface, alternate))) {
287 if (index >= alternate_instance->endpoints) {
290 return *(alternate_instance->endpoint_transfersize_array + index);
295 * usbd_device_endpoint_descriptor
296 * @device: which device
298 * @configuration: index to configuration, 0 - N-1
299 * @interface: index to interface
300 * @alternate: alternate setting
301 * @endpoint: which endpoint
303 * Return the specified endpoint descriptor for the specified device.
305 struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor (struct usb_device_instance *device, int port, int configuration, int interface, int alternate, int endpoint)
307 struct usb_endpoint_descriptor *endpoint_descriptor;
310 for (i = 0; !(endpoint_descriptor = usbd_device_endpoint_descriptor_index (device, port, configuration, interface, alternate, i)); i++) {
311 if (endpoint_descriptor->bEndpointAddress == endpoint) {
312 return endpoint_descriptor;
319 * usbd_endpoint_halted
320 * @device: point to struct usb_device_instance
321 * @endpoint: endpoint to check
323 * Return non-zero if endpoint is halted.
325 int usbd_endpoint_halted (struct usb_device_instance *device, int endpoint)
327 return (device->status == USB_STATUS_HALT);
332 * usbd_rcv_complete - complete a receive
337 * Called from rcv interrupt to complete.
339 void usbd_rcv_complete(struct usb_endpoint_instance *endpoint, int len, int urb_bad)
344 /*usbdbg("len: %d urb: %p\n", len, endpoint->rcv_urb); */
346 /* if we had an urb then update actual_length, dispatch if neccessary */
347 if ((rcv_urb = endpoint->rcv_urb)) {
349 /*usbdbg("actual: %d buffer: %d\n", */
350 /*rcv_urb->actual_length, rcv_urb->buffer_length); */
352 /* check the urb is ok, are we adding data less than the packetsize */
353 if (!urb_bad && (len <= endpoint->rcv_packetSize)) {
354 /*usbdbg("updating actual_length by %d\n",len); */
356 /* increment the received data size */
357 rcv_urb->actual_length += len;
360 usberr(" RECV_ERROR actual: %d buffer: %d urb_bad: %d\n",
361 rcv_urb->actual_length, rcv_urb->buffer_length, urb_bad);
363 rcv_urb->actual_length = 0;
364 rcv_urb->status = RECV_ERROR;
367 usberr("no rcv_urb!");
370 usberr("no endpoint!");
376 * usbd_tx_complete - complete a transmit
380 * Called from tx interrupt to complete.
382 void usbd_tx_complete (struct usb_endpoint_instance *endpoint)
387 /* if we have a tx_urb advance or reset, finish if complete */
388 if ((tx_urb = endpoint->tx_urb)) {
389 int sent = endpoint->last;
390 endpoint->sent += sent;
391 endpoint->last -= sent;
393 if( (endpoint->tx_urb->actual_length - endpoint->sent) <= 0 ) {
394 tx_urb->actual_length = 0;
398 /* Remove from active, save for re-use */
400 urb_append(&endpoint->done, tx_urb);
401 /*usbdbg("done->next %p, tx_urb %p, done %p", */
402 /* endpoint->done.next, tx_urb, &endpoint->done); */
404 endpoint->tx_urb = first_urb_detached(&endpoint->tx);
405 if( endpoint->tx_urb ) {
406 endpoint->tx_queue--;
407 usbdbg("got urb from tx list");
409 if( !endpoint->tx_urb ) {
410 /*usbdbg("taking urb from done list"); */
411 endpoint->tx_urb = first_urb_detached(&endpoint->done);
413 if( !endpoint->tx_urb ) {
414 usbdbg("allocating new urb for tx_urb");
415 endpoint->tx_urb = usbd_alloc_urb(tx_urb->device, endpoint);
422 /* URB linked list functions ***************************************************** */
425 * Initialize an urb_link to be a single element list.
426 * If the urb_link is being used as a distinguished list head
427 * the list is empty when the head is the only link in the list.
429 void urb_link_init (urb_link * ul)
432 ul->prev = ul->next = ul;
437 * Detach an urb_link from a list, and set it
438 * up as a single element list, so no dangling
439 * pointers can be followed, and so it can be
440 * joined to another list if so desired.
442 void urb_detach (struct urb *urb)
445 urb_link *ul = &urb->link;
446 ul->next->prev = ul->prev;
447 ul->prev->next = ul->next;
453 * Return the first urb_link in a list with a distinguished
454 * head "hd", or NULL if the list is empty. This will also
455 * work as a predicate, returning NULL if empty, and non-NULL
458 urb_link *first_urb_link (urb_link * hd)
461 if (NULL != hd && NULL != (nx = hd->next) && nx != hd) {
462 /* There is at least one element in the list */
463 /* (besides the distinguished head). */
466 /* The list is empty */
471 * Return the first urb in a list with a distinguished
472 * head "hd", or NULL if the list is empty.
474 struct urb *first_urb (urb_link * hd)
477 if (NULL == (nx = first_urb_link (hd))) {
478 /* The list is empty */
481 return (p2surround (struct urb, link, nx));
485 * Detach and return the first urb in a list with a distinguished
486 * head "hd", or NULL if the list is empty.
489 struct urb *first_urb_detached (urb_link * hd)
492 if ((urb = first_urb (hd))) {
500 * Append an urb_link (or a whole list of
501 * urb_links) to the tail of another list
504 void urb_append (urb_link * hd, struct urb *urb)
507 urb_link *new = &urb->link;
509 /* This allows the new urb to be a list of urbs, */
510 /* with new pointing at the first, but the link */
511 /* must be initialized. */
512 /* Order is important here... */
513 urb_link *pul = hd->prev;
514 new->prev->next = hd;
515 hd->prev = new->prev;
521 /* URB create/destroy functions ***************************************************** */
524 * usbd_alloc_urb - allocate an URB appropriate for specified endpoint
525 * @device: device instance
526 * @endpoint: endpoint
528 * Allocate an urb structure. The usb device urb structure is used to
529 * contain all data associated with a transfer, including a setup packet for
532 * NOTE: endpoint_address MUST contain a direction flag.
534 struct urb *usbd_alloc_urb (struct usb_device_instance *device,
535 struct usb_endpoint_instance *endpoint)
539 if (!(urb = (struct urb *) malloc (sizeof (struct urb)))) {
540 usberr (" F A T A L: malloc(%zu) FAILED!!!!",
541 sizeof (struct urb));
545 /* Fill in known fields */
546 memset (urb, 0, sizeof (struct urb));
547 urb->endpoint = endpoint;
548 urb->device = device;
549 urb->buffer = (u8 *) urb->buffer_data;
550 urb->buffer_length = sizeof (urb->buffer_data);
552 urb_link_init (&urb->link);
558 * usbd_dealloc_urb - deallocate an URB and associated buffer
559 * @urb: pointer to an urb structure
561 * Deallocate an urb structure and associated data.
563 void usbd_dealloc_urb (struct urb *urb)
570 /* Event signaling functions ***************************************************** */
573 * usbd_device_event - called to respond to various usb events
574 * @device: pointer to struct device
575 * @event: event to respond to
577 * Used by a Bus driver to indicate an event.
579 void usbd_device_event_irq (struct usb_device_instance *device, usb_device_event_t event, int data)
581 usb_device_state_t state;
583 if (!device || !device->bus) {
584 usberr("(%p,%d) NULL device or device->bus", device, event);
588 state = device->device_state;
590 usbinfo("%s", usbd_device_events[event]);
596 device->device_state = STATE_INIT;
600 device->device_state = STATE_ATTACHED;
603 case DEVICE_HUB_CONFIGURED:
604 device->device_state = STATE_POWERED;
608 device->device_state = STATE_DEFAULT;
612 case DEVICE_ADDRESS_ASSIGNED:
613 device->device_state = STATE_ADDRESSED;
616 case DEVICE_CONFIGURED:
617 device->device_state = STATE_CONFIGURED;
620 case DEVICE_DE_CONFIGURED:
621 device->device_state = STATE_ADDRESSED;
624 case DEVICE_BUS_INACTIVE:
625 if (device->status != USBD_CLOSING) {
626 device->status = USBD_SUSPENDED;
629 case DEVICE_BUS_ACTIVITY:
630 if (device->status != USBD_CLOSING) {
631 device->status = USBD_OK;
635 case DEVICE_SET_INTERFACE:
637 case DEVICE_SET_FEATURE:
639 case DEVICE_CLEAR_FEATURE:
642 case DEVICE_POWER_INTERRUPTION:
643 device->device_state = STATE_POWERED;
645 case DEVICE_HUB_RESET:
646 device->device_state = STATE_ATTACHED;
649 device->device_state = STATE_UNKNOWN;
652 case DEVICE_FUNCTION_PRIVATE:
656 usbdbg("event %d - not handled",event);
659 debug("%s event: %d oldstate: %d newstate: %d status: %d address: %d",
660 device->name, event, state,
661 device->device_state, device->status, device->address);
663 /* tell the bus interface driver */
664 if( device->event ) {
665 /* usbdbg("calling device->event"); */
666 device->event(device, event, data);