HelenOS sources
This source file includes following definitions.
- is_endpoint_descriptor
- is_superspeed_companion_descriptor
- endpoint_fits_description
- find_endpoint_mapping
- process_endpoint
- process_interface
- usb_pipe_initialize_from_configuration
#include <usb/dev/pipes.h>
#include <usb/dev/dp.h>
#include <usb/dev/request.h>
#include <usb/usb.h>
#include <usb/debug.h>
#include <usb/descriptor.h>
#include <assert.h>
#include <errno.h>
#define DEV_DESCR_MAX_PACKET_SIZE_OFFSET 7
#define NESTING(parentname, childname) \
{ \
.child = USB_DESCTYPE_##childname, \
.parent = USB_DESCTYPE_##parentname, \
}
#define LAST_NESTING { -1, -1 }
static const usb_dp_descriptor_nesting_t descriptor_nesting[] = {
NESTING(CONFIGURATION, INTERFACE),
NESTING(INTERFACE, ENDPOINT),
NESTING(INTERFACE, HUB),
NESTING(INTERFACE, HID),
NESTING(HID, HID_REPORT),
NESTING(ENDPOINT, SSPEED_EP_COMPANION),
LAST_NESTING
};
static inline bool is_endpoint_descriptor(const uint8_t *descriptor)
{
return descriptor[1] == USB_DESCTYPE_ENDPOINT;
}
static inline bool is_superspeed_companion_descriptor(const uint8_t *descriptor)
{
return descriptor[1] == USB_DESCTYPE_SSPEED_EP_COMPANION;
}
static bool endpoint_fits_description(const usb_endpoint_description_t *wanted,
const usb_endpoint_description_t *found)
{
#define _SAME(fieldname) ((wanted->fieldname) == (found->fieldname))
if (!_SAME(direction)) {
return false;
}
if (!_SAME(transfer_type)) {
return false;
}
if ((wanted->interface_class >= 0) && !_SAME(interface_class)) {
return false;
}
if ((wanted->interface_subclass >= 0) && !_SAME(interface_subclass)) {
return false;
}
if ((wanted->interface_protocol >= 0) && !_SAME(interface_protocol)) {
return false;
}
#undef _SAME
return true;
}
static usb_endpoint_mapping_t *find_endpoint_mapping(
usb_endpoint_mapping_t *mapping, size_t mapping_count,
const usb_endpoint_description_t *found_endpoint,
int interface_number, int interface_setting)
{
while (mapping_count > 0) {
bool interface_number_fits = (mapping->interface_no < 0) ||
(mapping->interface_no == interface_number);
bool interface_setting_fits = (mapping->interface_setting < 0) ||
(mapping->interface_setting == interface_setting);
bool endpoint_descriptions_fits = endpoint_fits_description(
mapping->description, found_endpoint);
if (interface_number_fits &&
interface_setting_fits &&
endpoint_descriptions_fits &&
!mapping->present) {
return mapping;
}
mapping++;
mapping_count--;
}
return NULL;
}
static errno_t process_endpoint(
usb_endpoint_mapping_t *mapping, size_t mapping_count,
usb_standard_interface_descriptor_t *interface,
usb_standard_endpoint_descriptor_t *endpoint_desc,
usb_superspeed_endpoint_companion_descriptor_t *companion_desc,
usb_dev_session_t *bus_session)
{
const usb_endpoint_description_t description = {
.transfer_type = USB_ED_GET_TRANSFER_TYPE(*endpoint_desc),
.direction = USB_ED_GET_DIR(*endpoint_desc),
.interface_class = interface->interface_class,
.interface_subclass = interface->interface_subclass,
.interface_protocol = interface->interface_protocol,
};
usb_endpoint_mapping_t *ep_mapping = find_endpoint_mapping(mapping,
mapping_count, &description,
interface->interface_number, interface->alternate_setting);
if (ep_mapping == NULL) {
return ENOENT;
}
if (ep_mapping->present) {
return EEXIST;
}
errno_t err = usb_pipe_initialize(&ep_mapping->pipe, bus_session);
if (err)
return err;
ep_mapping->present = true;
ep_mapping->descriptor = endpoint_desc;
ep_mapping->companion_descriptor = companion_desc;
ep_mapping->interface = interface;
return EOK;
}
static errno_t process_interface(
usb_endpoint_mapping_t *mapping, size_t mapping_count,
const usb_dp_parser_t *parser, const usb_dp_parser_data_t *parser_data,
const uint8_t *interface_descriptor, usb_dev_session_t *bus_session)
{
const uint8_t *descriptor = usb_dp_get_nested_descriptor(parser,
parser_data, interface_descriptor);
if (descriptor == NULL) {
return ENOENT;
}
do {
if (is_endpoint_descriptor(descriptor)) {
const uint8_t *companion_desc = usb_dp_get_nested_descriptor(parser,
parser_data, descriptor);
if (companion_desc && !is_superspeed_companion_descriptor(companion_desc)) {
companion_desc = NULL;
}
(void) process_endpoint(mapping, mapping_count,
(usb_standard_interface_descriptor_t *)
interface_descriptor,
(usb_standard_endpoint_descriptor_t *)
descriptor,
(usb_superspeed_endpoint_companion_descriptor_t *)
companion_desc,
bus_session);
}
descriptor = usb_dp_get_sibling_descriptor(parser, parser_data,
interface_descriptor, descriptor);
} while (descriptor != NULL);
return EOK;
}
errno_t usb_pipe_initialize_from_configuration(
usb_endpoint_mapping_t *mapping, size_t mapping_count,
const uint8_t *config_descriptor, size_t config_descriptor_size,
usb_dev_session_t *bus_session)
{
if (config_descriptor == NULL)
return EBADMEM;
if (config_descriptor_size <
sizeof(usb_standard_configuration_descriptor_t)) {
return ERANGE;
}
for (size_t i = 0; i < mapping_count; i++) {
mapping[i].present = false;
mapping[i].descriptor = NULL;
mapping[i].interface = NULL;
}
const usb_dp_parser_t dp_parser = {
.nesting = descriptor_nesting
};
const usb_dp_parser_data_t dp_data = {
.data = config_descriptor,
.size = config_descriptor_size,
};
const uint8_t *interface = usb_dp_get_nested_descriptor(&dp_parser,
&dp_data, config_descriptor);
if (interface == NULL) {
return ENOENT;
}
do {
(void) process_interface(mapping, mapping_count,
&dp_parser, &dp_data, interface, bus_session);
interface = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
config_descriptor, interface);
} while (interface != NULL);
return EOK;
}
HelenOS homepage, sources at GitHub