USB descriptor definitions and handling. More...
Go to the source code of this file.
Data Structures | |
struct | usb_descriptor_header_t |
Common header used for all USB descriptors. More... | |
struct | descriptor_list_t |
Single item of a linked list of descriptors. More... | |
struct | usb_descriptor_body_device_t |
Device descriptor body. More... | |
struct | usb_descriptor_body_configuration_t |
Configuration descriptor body. More... | |
struct | usb_descriptor_body_interface_t |
Interface descriptor body. More... | |
struct | usb_descriptor_body_endpoint_t |
Endpoint descriptor body. More... | |
Macros | |
#define | USB_CONFIG_ATTRIBUTES(self_powered, remote_wakeup) ((1<<7)|((self_powered)<<6)|((remote_wakeup)<<5)) |
#define | USB_MAX_POWER(milli_amps) (milli_amps/2) |
Enumerations | |
enum | usb_descriptor_type_t { DESC_TYPE_DEVICE = 1, DESC_TYPE_CONFIGURATION = 2, DESC_TYPE_STRING = 3, DESC_TYPE_INTERFACE = 4, DESC_TYPE_ENDPOINT = 5 } |
Functions | |
uint8_t | usb_descriptor_size (const enum usb_descriptor_type_t type, const void *body, const enum memspace_t memspace) |
Calculate the total size of a USB descriptor. More... | |
uint8_t | usb_descriptor_body_size (const enum usb_descriptor_type_t type, const void *body, const enum memspace_t memspace) |
Calculate the size of a USB descriptor's body. More... | |
struct descriptor_list_t * | generate_descriptor_list (const struct usb_setup_packet_t *req) |
uint16_t | get_list_total_length (const struct descriptor_list_t *head) |
Calculate the sum of all descriptor sizes in a linked list. More... | |
USB descriptor definitions and handling.
In the USB protocol descriptors are a vital part of device enumeration. It is a flexible system, but requires some effort from the developer to implement properly. The constants, structs and functions provided here attempt to make adding new descriptors as painless as possible. Most structs defined in this file use the notation from the USB specification instead of notation used elsewhere in this project for easier referencing.
When a setup request response is generated using generate_descriptor_list(), a linked list with the required descriptors will be dynamically allocated. Since USB descriptors are usually only requested during device enumeration, the leaves the memory free to be used for other purposes during USB operation, such as remote display frame transfers.
Descriptor bodies are provided for the following descriptor types:
u"IceTop display"
List of used USB descriptors. Note that more descriptor types are defined by the USB 2.0 standard. Since these are not supported or used anywhere, there is currently no need to define them.
struct descriptor_list_t* generate_descriptor_list | ( | const struct usb_setup_packet_t * | req | ) |
Create a linked list of USB descriptors based on the provided setup request. Only pass setup packets that contain a GET_DESCRIPTOR request, as this function does not check the validity of the packet.
req | Pointer to the setup request packet with a GET_DESCRIPTOR request. |
uint16_t get_list_total_length | ( | const struct descriptor_list_t * | head | ) |
Calculate the sum of all descriptor sizes in a linked list.
This function is used when a configuration descriptor is provided, which needs to contain the total lenght off all returned descriptors.
head | Pointer to the first item in the list. |
uint8_t usb_descriptor_body_size | ( | const enum usb_descriptor_type_t | type, |
const void * | body, | ||
const enum memspace_t | memspace | ||
) |
Calculate the size of a USB descriptor's body.
Most descriptors have a predifined length, so in those cases this function just returns a constant. String descriptors however have no predetermined length, so their length needs to be determined at runtime.
type | Type of the descriptor. |
body | Pointer to the body of the descriptor. |
memspace | Memory space the descriptor body resides in. |
uint8_t usb_descriptor_size | ( | const enum usb_descriptor_type_t | type, |
const void * | body, | ||
const enum memspace_t | memspace | ||
) |
Calculate the total size of a USB descriptor.
This function performs the same calculation as usb_descriptor_body_size(), but adds the size of the descriptor header that is identical for all descriptors.