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 * SPDX-License-Identifier: GPL-2.0+
20 #include <usbdevice.h>
22 #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 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 char *usbd_device_states
[] = {
69 char *usbd_device_requests
[] = {
71 "CLEAR FEATURE", /* 1 */
73 "SET FEATURE", /* 3 */
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
[] = {
92 "DEVICE QUALIFIER", /* 6 */
93 "OTHER SPEED", /* 7 */
94 "INTERFACE POWER", /* 8 */
97 char *usbd_device_status
[] = {
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
) {
119 return usb_strings
[index
];
123 /* Access to device descriptor functions ***************************************************** */
127 * usbd_device_configuration_instance - find a configuration instance for this 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
)
139 return device
->configuration_instance_array
+ configuration
;
144 * usbd_device_interface_instance
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
) {
158 if (interface
>= configuration_instance
->interfaces
) {
161 return configuration_instance
->interface_instance_array
+ interface
;
165 * usbd_device_alternate_descriptor_list
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
) {
181 if (alternate
>= interface_instance
->alternates
) {
185 return interface_instance
->alternates_instance_array
+ alternate
;
190 * usbd_device_device_descriptor
191 * @device: which device
192 * @configuration: index to configuration, 0 - N-1
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
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
212 *device
, int port
, int configuration
)
214 struct usb_configuration_instance
*configuration_instance
;
215 if (!(configuration_instance
= usbd_device_configuration_instance (device
, port
, configuration
))) {
218 return (configuration_instance
->configuration_descriptor
);
223 * usbd_device_interface_descriptor
224 * @device: which device
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
))) {
239 if ((alternate
< 0) || (alternate
>= interface_instance
->alternates
)) {
242 return (interface_instance
->alternates_instance_array
[alternate
].interface_descriptor
);
246 * usbd_device_endpoint_descriptor_index
247 * @device: which device
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
))) {
264 if (index
>= alternate_instance
->endpoints
) {
267 return *(alternate_instance
->endpoints_descriptor_array
+ index
);
272 * usbd_device_endpoint_transfersize
273 * @device: which device
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
))) {
288 if (index
>= alternate_instance
->endpoints
) {
291 return *(alternate_instance
->endpoint_transfersize_array
+ index
);
296 * usbd_device_endpoint_descriptor
297 * @device: which device
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
;
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
;
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
338 * Called from rcv interrupt to complete.
340 void usbd_rcv_complete(struct usb_endpoint_instance
*endpoint
, int len
, int urb_bad
)
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
;
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
;
368 usberr("no rcv_urb!");
371 usberr("no endpoint!");
377 * usbd_tx_complete - complete a transmit
381 * Called from tx interrupt to complete.
383 void usbd_tx_complete (struct usb_endpoint_instance
*endpoint
)
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;
399 /* Remove from active, save for re-use */
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
)
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
)
446 urb_link
*ul
= &urb
->link
;
447 ul
->next
->prev
= ul
->prev
;
448 ul
->prev
->next
= ul
->next
;
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
459 urb_link
*first_urb_link (urb_link
* hd
)
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). */
467 /* The list is empty */
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
)
478 if (NULL
== (nx
= first_urb_link (hd
))) {
479 /* The list is empty */
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
)
493 if ((urb
= first_urb (hd
))) {
501 * Append an urb_link (or a whole list of
502 * urb_links) to the tail of another list
505 void urb_append (urb_link
* hd
, struct urb
*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
;
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
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
)
540 if (!(urb
= (struct urb
*) malloc (sizeof (struct urb
)))) {
541 usberr (" F A T A L: malloc(%zu) FAILED!!!!",
542 sizeof (struct urb
));
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
);
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
)
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
);
589 state
= device
->device_state
;
591 usbinfo("%s", usbd_device_events
[event
]);
597 device
->device_state
= STATE_INIT
;
601 device
->device_state
= STATE_ATTACHED
;
604 case DEVICE_HUB_CONFIGURED
:
605 device
->device_state
= STATE_POWERED
;
609 device
->device_state
= STATE_DEFAULT
;
613 case DEVICE_ADDRESS_ASSIGNED
:
614 device
->device_state
= STATE_ADDRESSED
;
617 case DEVICE_CONFIGURED
:
618 device
->device_state
= STATE_CONFIGURED
;
621 case DEVICE_DE_CONFIGURED
:
622 device
->device_state
= STATE_ADDRESSED
;
625 case DEVICE_BUS_INACTIVE
:
626 if (device
->status
!= USBD_CLOSING
) {
627 device
->status
= USBD_SUSPENDED
;
630 case DEVICE_BUS_ACTIVITY
:
631 if (device
->status
!= USBD_CLOSING
) {
632 device
->status
= USBD_OK
;
636 case DEVICE_SET_INTERFACE
:
638 case DEVICE_SET_FEATURE
:
640 case DEVICE_CLEAR_FEATURE
:
643 case DEVICE_POWER_INTERRUPTION
:
644 device
->device_state
= STATE_POWERED
;
646 case DEVICE_HUB_RESET
:
647 device
->device_state
= STATE_ATTACHED
;
650 device
->device_state
= STATE_UNKNOWN
;
653 case DEVICE_FUNCTION_PRIVATE
:
657 usbdbg("event %d - not handled",event
);
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
);