HelenOS sources
This source file includes following definitions.
- interface_in_list
- create_interfaces
- usbmid_explore_device
#include <errno.h>
#include <str_error.h>
#include <stdlib.h>
#include <usb/classes/classes.h>
#include <usb/dev/request.h>
#include <usb/dev/dp.h>
#include "usbmid.h"
static bool interface_in_list(const list_t *list, int interface_no)
{
list_foreach(*list, link, const usbmid_interface_t, iface) {
if (iface->interface_no == interface_no) {
return true;
}
}
return false;
}
static errno_t create_interfaces(const uint8_t *config_descriptor,
size_t config_descriptor_size, list_t *list, usb_device_t *usb_dev)
{
assert(config_descriptor);
assert(usb_dev);
const usb_dp_parser_data_t data = {
.data = config_descriptor,
.size = config_descriptor_size,
.arg = NULL
};
static const usb_dp_parser_t parser = {
.nesting = usb_dp_standard_descriptor_nesting
};
const uint8_t *interface_ptr =
usb_dp_get_nested_descriptor(&parser, &data, config_descriptor);
for (; interface_ptr != NULL;
interface_ptr = usb_dp_get_sibling_descriptor(
&parser, &data, config_descriptor, interface_ptr)) {
if (interface_ptr[1] != USB_DESCTYPE_INTERFACE)
continue;
const usb_standard_interface_descriptor_t *interface =
(usb_standard_interface_descriptor_t *) interface_ptr;
if (interface_in_list(list, interface->interface_number)) {
continue;
}
usb_log_info("Creating child for interface %d (%s).",
interface->interface_number,
usb_str_class(interface->interface_class));
usbmid_interface_t *iface = NULL;
const errno_t rc = usbmid_spawn_interface_child(usb_dev, &iface,
&usb_device_descriptors(usb_dev)->device, interface);
if (rc != EOK) {
usb_log_error("Failed to create interface child for "
"%d (%s): %s.\n", interface->interface_number,
usb_str_class(interface->interface_class),
str_error(rc));
} else {
list_append(&iface->link, list);
}
}
return EOK;
}
errno_t usbmid_explore_device(usb_device_t *dev)
{
assert(dev);
const unsigned dev_class =
usb_device_descriptors(dev)->device.device_class;
if (dev_class != USB_CLASS_USE_INTERFACE) {
usb_log_warning(
"Device class: %u (%s), but expected class %u.\n",
dev_class, usb_str_class(dev_class),
USB_CLASS_USE_INTERFACE);
usb_log_error("Not a multi-interface device, refusing.");
return ENOTSUP;
}
const size_t config_descriptor_size =
usb_device_descriptors(dev)->full_config_size;
const void *config_descriptor_raw =
usb_device_descriptors(dev)->full_config;
const usb_standard_configuration_descriptor_t *config_descriptor =
config_descriptor_raw;
errno_t rc = usb_request_set_configuration(usb_device_get_default_pipe(dev),
config_descriptor->configuration_number);
if (rc != EOK) {
usb_log_error("Failed to set device configuration: %s.",
str_error(rc));
return rc;
}
usb_mid_t *usb_mid = usb_device_data_alloc(dev, sizeof(usb_mid_t));
if (!usb_mid) {
usb_log_error("Failed to create USB MID structure.");
return ENOMEM;
}
usb_mid->ctl_fun = usb_device_ddf_fun_create(dev, fun_exposed, "ctl");
if (usb_mid->ctl_fun == NULL) {
usb_log_error("Failed to create control function.");
return ENOMEM;
}
rc = ddf_fun_bind(usb_mid->ctl_fun);
if (rc != EOK) {
usb_log_error("Failed to bind control function: %s.",
str_error(rc));
ddf_fun_destroy(usb_mid->ctl_fun);
return rc;
}
list_initialize(&usb_mid->interface_list);
create_interfaces(config_descriptor_raw, config_descriptor_size,
&usb_mid->interface_list, dev);
return EOK;
}
HelenOS homepage, sources at GitHub