1 // SPDX-License-Identifier: GPL-2.0+
7 * linux/drivers/usbd/usbd.c.c - USB Device Core Layer
9 * Copyright (c) 2000, 2001, 2002 Lineo
10 * Copyright (c) 2001 Hewlett Packard
21 #include <usbdevice.h>
23 #define MAX_INTERFACES 2
27 /* Global variables ************************************************************************** */
29 struct usb_string_descriptor **usb_strings;
33 extern struct usb_function_driver ep0_driver;
35 int registered_functions;
36 int registered_devices;
38 __maybe_unused static char *usbd_device_events[] = {
42 "DEVICE_HUB_CONFIGURED",
44 "DEVICE_ADDRESS_ASSIGNED",
46 "DEVICE_SET_INTERFACE",
48 "DEVICE_CLEAR_FEATURE",
49 "DEVICE_DE_CONFIGURED",
50 "DEVICE_BUS_INACTIVE",
51 "DEVICE_BUS_ACTIVITY",
52 "DEVICE_POWER_INTERRUPTION",
55 "DEVICE_FUNCTION_PRIVATE",
58 __maybe_unused static char *usbd_device_status[] = {
65 #define USBD_DEVICE_STATUS(x) (((unsigned int)x <= USBD_CLOSING) ? usbd_device_status[x] : "UNKNOWN")
67 /* Descriptor support functions ************************************************************** */
70 * usbd_get_string - find and return a string descriptor
71 * @index: string index to return
73 * Find an indexed string and return a pointer to a it.
75 struct usb_string_descriptor *usbd_get_string (__u8 index)
77 if (index >= maxstrings) {
80 return usb_strings[index];
83 /* Access to device descriptor functions ***************************************************** */
86 * usbd_device_configuration_instance - find a configuration instance for this device
88 * @configuration: index to configuration, 0 - N-1
90 * Get specifed device configuration. Index should be bConfigurationValue-1.
92 static struct usb_configuration_instance *usbd_device_configuration_instance (struct usb_device_instance *device,
93 unsigned int port, unsigned int configuration)
95 if (configuration >= device->configurations)
98 return device->configuration_instance_array + configuration;
102 * usbd_device_interface_instance
104 * @configuration: index to configuration, 0 - N-1
105 * @interface: index to interface
107 * Return the specified interface descriptor for the specified device.
109 struct usb_interface_instance *usbd_device_interface_instance (struct usb_device_instance *device, int port, int configuration, int interface)
111 struct usb_configuration_instance *configuration_instance;
113 if ((configuration_instance = usbd_device_configuration_instance (device, port, configuration)) == NULL) {
116 if (interface >= configuration_instance->interfaces) {
119 return configuration_instance->interface_instance_array + interface;
123 * usbd_device_alternate_descriptor_list
125 * @configuration: index to configuration, 0 - N-1
126 * @interface: index to interface
127 * @alternate: alternate setting
129 * Return the specified alternate descriptor for the specified device.
131 struct usb_alternate_instance *usbd_device_alternate_instance (struct usb_device_instance *device, int port, int configuration, int interface, int alternate)
133 struct usb_interface_instance *interface_instance;
135 if ((interface_instance = usbd_device_interface_instance (device, port, configuration, interface)) == NULL) {
139 if (alternate >= interface_instance->alternates) {
143 return interface_instance->alternates_instance_array + alternate;
147 * usbd_device_device_descriptor
148 * @device: which device
149 * @configuration: index to configuration, 0 - N-1
152 * Return the specified configuration descriptor for the specified device.
154 struct usb_device_descriptor *usbd_device_device_descriptor (struct usb_device_instance *device, int port)
156 return (device->device_descriptor);
160 * usbd_device_configuration_descriptor
161 * @device: which device
163 * @configuration: index to configuration, 0 - N-1
165 * Return the specified configuration descriptor for the specified device.
167 struct usb_configuration_descriptor *usbd_device_configuration_descriptor (struct
169 *device, int port, int configuration)
171 struct usb_configuration_instance *configuration_instance;
172 if (!(configuration_instance = usbd_device_configuration_instance (device, port, configuration))) {
175 return (configuration_instance->configuration_descriptor);
179 * usbd_device_interface_descriptor
180 * @device: which device
182 * @configuration: index to configuration, 0 - N-1
183 * @interface: index to interface
184 * @alternate: alternate setting
186 * Return the specified interface descriptor for the specified device.
188 struct usb_interface_descriptor *usbd_device_interface_descriptor (struct usb_device_instance
189 *device, int port, int configuration, int interface, int alternate)
191 struct usb_interface_instance *interface_instance;
192 if (!(interface_instance = usbd_device_interface_instance (device, port, configuration, interface))) {
195 if ((alternate < 0) || (alternate >= interface_instance->alternates)) {
198 return (interface_instance->alternates_instance_array[alternate].interface_descriptor);
202 * usbd_device_endpoint_descriptor_index
203 * @device: which device
205 * @configuration: index to configuration, 0 - N-1
206 * @interface: index to interface
207 * @alternate: index setting
208 * @index: which index
210 * Return the specified endpoint descriptor for the specified device.
212 struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor_index (struct usb_device_instance
213 *device, int port, int configuration, int interface, int alternate, int index)
215 struct usb_alternate_instance *alternate_instance;
217 if (!(alternate_instance = usbd_device_alternate_instance (device, port, configuration, interface, alternate))) {
220 if (index >= alternate_instance->endpoints) {
223 return *(alternate_instance->endpoints_descriptor_array + index);
227 * usbd_device_endpoint_transfersize
228 * @device: which device
230 * @configuration: index to configuration, 0 - N-1
231 * @interface: index to interface
232 * @index: which index
234 * Return the specified endpoint transfer size;
236 int usbd_device_endpoint_transfersize (struct usb_device_instance *device, int port, int configuration, int interface, int alternate, int index)
238 struct usb_alternate_instance *alternate_instance;
240 if (!(alternate_instance = usbd_device_alternate_instance (device, port, configuration, interface, alternate))) {
243 if (index >= alternate_instance->endpoints) {
246 return *(alternate_instance->endpoint_transfersize_array + index);
250 * usbd_device_endpoint_descriptor
251 * @device: which device
253 * @configuration: index to configuration, 0 - N-1
254 * @interface: index to interface
255 * @alternate: alternate setting
256 * @endpoint: which endpoint
258 * Return the specified endpoint descriptor for the specified device.
260 struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor (struct usb_device_instance *device, int port, int configuration, int interface, int alternate, int endpoint)
262 struct usb_endpoint_descriptor *endpoint_descriptor;
265 for (i = 0; !(endpoint_descriptor = usbd_device_endpoint_descriptor_index (device, port, configuration, interface, alternate, i)); i++) {
266 if (endpoint_descriptor->bEndpointAddress == endpoint) {
267 return endpoint_descriptor;
274 * usbd_endpoint_halted
275 * @device: point to struct usb_device_instance
276 * @endpoint: endpoint to check
278 * Return non-zero if endpoint is halted.
280 int usbd_endpoint_halted (struct usb_device_instance *device, int endpoint)
282 return (device->status == USB_STATUS_HALT);
286 * usbd_rcv_complete - complete a receive
291 * Called from rcv interrupt to complete.
293 void usbd_rcv_complete(struct usb_endpoint_instance *endpoint, int len, int urb_bad)
298 /*usbdbg("len: %d urb: %p\n", len, endpoint->rcv_urb); */
300 /* if we had an urb then update actual_length, dispatch if neccessary */
301 if ((rcv_urb = endpoint->rcv_urb)) {
303 /*usbdbg("actual: %d buffer: %d\n", */
304 /*rcv_urb->actual_length, rcv_urb->buffer_length); */
306 /* check the urb is ok, are we adding data less than the packetsize */
307 if (!urb_bad && (len <= endpoint->rcv_packetSize)) {
308 /*usbdbg("updating actual_length by %d\n",len); */
310 /* increment the received data size */
311 rcv_urb->actual_length += len;
314 usberr(" RECV_ERROR actual: %d buffer: %d urb_bad: %d\n",
315 rcv_urb->actual_length, rcv_urb->buffer_length, urb_bad);
317 rcv_urb->actual_length = 0;
318 rcv_urb->status = RECV_ERROR;
321 usberr("no rcv_urb!");
324 usberr("no endpoint!");
330 * usbd_tx_complete - complete a transmit
334 * Called from tx interrupt to complete.
336 void usbd_tx_complete (struct usb_endpoint_instance *endpoint)
341 /* if we have a tx_urb advance or reset, finish if complete */
342 if ((tx_urb = endpoint->tx_urb)) {
343 int sent = endpoint->last;
344 endpoint->sent += sent;
345 endpoint->last -= sent;
347 if( (endpoint->tx_urb->actual_length - endpoint->sent) <= 0 ) {
348 tx_urb->actual_length = 0;
352 /* Remove from active, save for re-use */
354 urb_append(&endpoint->done, tx_urb);
355 /*usbdbg("done->next %p, tx_urb %p, done %p", */
356 /* endpoint->done.next, tx_urb, &endpoint->done); */
358 endpoint->tx_urb = first_urb_detached(&endpoint->tx);
359 if( endpoint->tx_urb ) {
360 endpoint->tx_queue--;
361 usbdbg("got urb from tx list");
363 if( !endpoint->tx_urb ) {
364 /*usbdbg("taking urb from done list"); */
365 endpoint->tx_urb = first_urb_detached(&endpoint->done);
367 if( !endpoint->tx_urb ) {
368 usbdbg("allocating new urb for tx_urb");
369 endpoint->tx_urb = usbd_alloc_urb(tx_urb->device, endpoint);
376 /* URB linked list functions ***************************************************** */
379 * Initialize an urb_link to be a single element list.
380 * If the urb_link is being used as a distinguished list head
381 * the list is empty when the head is the only link in the list.
383 void urb_link_init (urb_link * ul)
386 ul->prev = ul->next = ul;
391 * Detach an urb_link from a list, and set it
392 * up as a single element list, so no dangling
393 * pointers can be followed, and so it can be
394 * joined to another list if so desired.
396 void urb_detach (struct urb *urb)
399 urb_link *ul = &urb->link;
400 ul->next->prev = ul->prev;
401 ul->prev->next = ul->next;
407 * Return the first urb_link in a list with a distinguished
408 * head "hd", or NULL if the list is empty. This will also
409 * work as a predicate, returning NULL if empty, and non-NULL
412 urb_link *first_urb_link (urb_link * hd)
415 if (NULL != hd && NULL != (nx = hd->next) && nx != hd) {
416 /* There is at least one element in the list */
417 /* (besides the distinguished head). */
420 /* The list is empty */
425 * Return the first urb in a list with a distinguished
426 * head "hd", or NULL if the list is empty.
428 struct urb *first_urb (urb_link * hd)
431 if (NULL == (nx = first_urb_link (hd))) {
432 /* The list is empty */
435 return (p2surround (struct urb, link, nx));
439 * Detach and return the first urb in a list with a distinguished
440 * head "hd", or NULL if the list is empty.
443 struct urb *first_urb_detached (urb_link * hd)
446 if ((urb = first_urb (hd))) {
453 * Append an urb_link (or a whole list of
454 * urb_links) to the tail of another list
457 void urb_append (urb_link * hd, struct urb *urb)
460 urb_link *new = &urb->link;
462 /* This allows the new urb to be a list of urbs, */
463 /* with new pointing at the first, but the link */
464 /* must be initialized. */
465 /* Order is important here... */
466 urb_link *pul = hd->prev;
467 new->prev->next = hd;
468 hd->prev = new->prev;
474 /* URB create/destroy functions ***************************************************** */
477 * usbd_alloc_urb - allocate an URB appropriate for specified endpoint
478 * @device: device instance
479 * @endpoint: endpoint
481 * Allocate an urb structure. The usb device urb structure is used to
482 * contain all data associated with a transfer, including a setup packet for
485 * NOTE: endpoint_address MUST contain a direction flag.
487 struct urb *usbd_alloc_urb (struct usb_device_instance *device,
488 struct usb_endpoint_instance *endpoint)
492 if (!(urb = (struct urb *) malloc (sizeof (struct urb)))) {
493 usberr (" F A T A L: malloc(%zu) FAILED!!!!",
494 sizeof (struct urb));
498 /* Fill in known fields */
499 memset (urb, 0, sizeof (struct urb));
500 urb->endpoint = endpoint;
501 urb->device = device;
502 urb->buffer = (u8 *) urb->buffer_data;
503 urb->buffer_length = sizeof (urb->buffer_data);
505 urb_link_init (&urb->link);
511 * usbd_dealloc_urb - deallocate an URB and associated buffer
512 * @urb: pointer to an urb structure
514 * Deallocate an urb structure and associated data.
516 void usbd_dealloc_urb (struct urb *urb)
523 /* Event signaling functions ***************************************************** */
526 * usbd_device_event - called to respond to various usb events
527 * @device: pointer to struct device
528 * @event: event to respond to
530 * Used by a Bus driver to indicate an event.
532 void usbd_device_event_irq (struct usb_device_instance *device, usb_device_event_t event, int data)
534 usb_device_state_t state;
536 if (!device || !device->bus) {
537 usberr("(%p,%d) NULL device or device->bus", device, event);
541 state = device->device_state;
543 usbinfo("%s", usbd_device_events[event]);
549 device->device_state = STATE_INIT;
553 device->device_state = STATE_ATTACHED;
556 case DEVICE_HUB_CONFIGURED:
557 device->device_state = STATE_POWERED;
561 device->device_state = STATE_DEFAULT;
565 case DEVICE_ADDRESS_ASSIGNED:
566 device->device_state = STATE_ADDRESSED;
569 case DEVICE_CONFIGURED:
570 device->device_state = STATE_CONFIGURED;
573 case DEVICE_DE_CONFIGURED:
574 device->device_state = STATE_ADDRESSED;
577 case DEVICE_BUS_INACTIVE:
578 if (device->status != USBD_CLOSING) {
579 device->status = USBD_SUSPENDED;
582 case DEVICE_BUS_ACTIVITY:
583 if (device->status != USBD_CLOSING) {
584 device->status = USBD_OK;
588 case DEVICE_SET_INTERFACE:
590 case DEVICE_SET_FEATURE:
592 case DEVICE_CLEAR_FEATURE:
595 case DEVICE_POWER_INTERRUPTION:
596 device->device_state = STATE_POWERED;
598 case DEVICE_HUB_RESET:
599 device->device_state = STATE_ATTACHED;
602 device->device_state = STATE_UNKNOWN;
605 case DEVICE_FUNCTION_PRIVATE:
609 usbdbg("event %d - not handled",event);
612 debug("%s event: %d oldstate: %d newstate: %d status: %d address: %d",
613 device->name, event, state,
614 device->device_state, device->status, device->address);
616 /* tell the bus interface driver */
617 if( device->event ) {
618 /* usbdbg("calling device->event"); */
619 device->event(device, event, data);