HelenOS sources
This source file includes following definitions.
- usb_control_request_set
- usb_control_request_get
- usb_request_get_status
- usb_request_clear_feature
- usb_request_set_feature
- usb_request_get_descriptor
- usb_request_get_descriptor_alloc
- usb_request_get_device_descriptor
- usb_request_get_bare_configuration_descriptor
- usb_request_get_full_configuration_descriptor
- usb_request_get_full_configuration_descriptor_alloc
- usb_request_set_descriptor
- usb_request_get_configuration
- usb_request_set_configuration
- usb_request_get_interface
- usb_request_set_interface
- usb_request_get_supported_languages
- usb_request_get_string
- usb_request_clear_endpoint_halt
- usb_pipe_clear_halt
- usb_request_get_endpoint_status
#include <usb/dev/request.h>
#include <usb/request.h>
#include <usb/usb.h>
#include <errno.h>
#include <mem.h>
#include <stdlib.h>
#include <str.h>
#define MAX_DATA_LENGTH ((size_t)(0xFFFF))
static_assert(sizeof(usb_device_request_setup_packet_t) == 8, "");
errno_t usb_control_request_set(usb_pipe_t *pipe,
usb_request_type_t request_type, usb_request_recipient_t recipient,
uint8_t request, uint16_t value, uint16_t index,
const void *data, size_t data_size)
{
if (pipe == NULL) {
return EBADMEM;
}
if (data_size > MAX_DATA_LENGTH) {
return ERANGE;
}
if ((data_size > 0) && (data == NULL)) {
return EBADMEM;
}
const usb_device_request_setup_packet_t setup_packet = {
.request_type = (request_type << 5) | recipient,
.request = request,
.value = value,
.index = index,
.length = (uint16_t) data_size,
};
return usb_pipe_control_write(pipe,
&setup_packet, sizeof(setup_packet), data, data_size);
}
errno_t usb_control_request_get(usb_pipe_t *pipe,
usb_request_type_t request_type, usb_request_recipient_t recipient,
uint8_t request, uint16_t value, uint16_t index,
void *data, size_t data_size, size_t *actual_data_size)
{
if (pipe == NULL) {
return EBADMEM;
}
if (data_size > MAX_DATA_LENGTH) {
return ERANGE;
}
if ((data_size > 0) && (data == NULL)) {
return EBADMEM;
}
const usb_device_request_setup_packet_t setup_packet = {
.request_type = SETUP_REQUEST_TYPE_DEVICE_TO_HOST |
(request_type << 5) | recipient,
.request = request,
.value = uint16_host2usb(value),
.index = uint16_host2usb(index),
.length = uint16_host2usb(data_size),
};
return usb_pipe_control_read(pipe, &setup_packet, sizeof(setup_packet),
data, data_size, actual_data_size);
}
errno_t usb_request_get_status(usb_pipe_t *pipe,
usb_request_recipient_t recipient, uint16_t index,
uint16_t *status)
{
if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0)) {
return EINVAL;
}
if (status == NULL) {
return EBADMEM;
}
uint16_t status_usb_endianess;
size_t data_transferred_size;
errno_t rc = usb_control_request_get(pipe, USB_REQUEST_TYPE_STANDARD,
recipient, USB_DEVREQ_GET_STATUS, 0, uint16_host2usb(index),
&status_usb_endianess, 2, &data_transferred_size);
if (rc != EOK) {
return rc;
}
if (data_transferred_size != 2) {
return ELIMIT;
}
*status = uint16_usb2host(status_usb_endianess);
return EOK;
}
errno_t usb_request_clear_feature(usb_pipe_t *pipe,
usb_request_type_t request_type, usb_request_recipient_t recipient,
uint16_t feature_selector, uint16_t index)
{
if (request_type == USB_REQUEST_TYPE_STANDARD) {
if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0)) {
return EINVAL;
}
}
return usb_control_request_set(pipe,
request_type, recipient, USB_DEVREQ_CLEAR_FEATURE,
uint16_host2usb(feature_selector), uint16_host2usb(index), NULL, 0);
}
errno_t usb_request_set_feature(usb_pipe_t *pipe,
usb_request_type_t request_type, usb_request_recipient_t recipient,
uint16_t feature_selector, uint16_t index)
{
if (request_type == USB_REQUEST_TYPE_STANDARD) {
if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0)) {
return EINVAL;
}
}
return usb_control_request_set(pipe,
request_type, recipient, USB_DEVREQ_SET_FEATURE,
uint16_host2usb(feature_selector), uint16_host2usb(index), NULL, 0);
}
errno_t usb_request_get_descriptor(usb_pipe_t *pipe,
usb_request_type_t request_type, usb_request_recipient_t recipient,
uint8_t descriptor_type, uint8_t descriptor_index,
uint16_t language,
void *buffer, size_t size, size_t *actual_size)
{
if (buffer == NULL) {
return EBADMEM;
}
if (size == 0) {
return EINVAL;
}
const uint16_t wValue = descriptor_index | (descriptor_type << 8);
return usb_control_request_get(pipe,
request_type, recipient,
USB_DEVREQ_GET_DESCRIPTOR,
wValue, language,
buffer, size, actual_size);
}
errno_t usb_request_get_descriptor_alloc(usb_pipe_t *pipe,
usb_request_type_t request_type, usb_request_recipient_t recipient,
uint8_t descriptor_type, uint8_t descriptor_index,
uint16_t language,
void **buffer_ptr, size_t *buffer_size)
{
if (buffer_ptr == NULL) {
return EBADMEM;
}
errno_t rc;
uint8_t tmp_buffer;
size_t bytes_transferred;
rc = usb_request_get_descriptor(pipe, request_type, recipient,
descriptor_type, descriptor_index, language,
&tmp_buffer, sizeof(tmp_buffer), &bytes_transferred);
if (rc != EOK) {
return rc;
}
if (bytes_transferred != 1) {
return ELIMIT;
}
const size_t size = tmp_buffer;
if (size == 0) {
return ELIMIT;
}
void *buffer = malloc(size);
if (buffer == NULL) {
return ENOMEM;
}
rc = usb_request_get_descriptor(pipe, request_type, recipient,
descriptor_type, descriptor_index, language,
buffer, size, &bytes_transferred);
if (rc != EOK) {
free(buffer);
return rc;
}
if (bytes_transferred != size) {
free(buffer);
return ELIMIT;
}
*buffer_ptr = buffer;
if (buffer_size != NULL) {
*buffer_size = size;
}
return EOK;
}
errno_t usb_request_get_device_descriptor(usb_pipe_t *pipe,
usb_standard_device_descriptor_t *descriptor)
{
if (descriptor == NULL) {
return EBADMEM;
}
size_t actually_transferred = 0;
usb_standard_device_descriptor_t descriptor_tmp;
errno_t rc = usb_request_get_descriptor(pipe,
USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
USB_DESCTYPE_DEVICE, 0, 0, &descriptor_tmp, sizeof(descriptor_tmp),
&actually_transferred);
if (rc != EOK) {
return rc;
}
if (actually_transferred < sizeof(descriptor_tmp)) {
return ELIMIT;
}
memcpy(descriptor, &descriptor_tmp, sizeof(descriptor_tmp));
return EOK;
}
errno_t usb_request_get_bare_configuration_descriptor(usb_pipe_t *pipe,
int index, usb_standard_configuration_descriptor_t *descriptor)
{
if (descriptor == NULL) {
return EBADMEM;
}
if ((index < 0) || (index > 0xFF)) {
return ERANGE;
}
size_t actually_transferred = 0;
usb_standard_configuration_descriptor_t descriptor_tmp;
const errno_t rc = usb_request_get_descriptor(pipe,
USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
USB_DESCTYPE_CONFIGURATION, index, 0,
&descriptor_tmp, sizeof(descriptor_tmp),
&actually_transferred);
if (rc != EOK) {
return rc;
}
if (actually_transferred < sizeof(descriptor_tmp)) {
return ELIMIT;
}
memcpy(descriptor, &descriptor_tmp, sizeof(descriptor_tmp));
return EOK;
}
errno_t usb_request_get_full_configuration_descriptor(usb_pipe_t *pipe,
int index, void *descriptor, size_t descriptor_size, size_t *actual_size)
{
if ((index < 0) || (index > 0xFF)) {
return ERANGE;
}
return usb_request_get_descriptor(pipe,
USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
USB_DESCTYPE_CONFIGURATION, index, 0,
descriptor, descriptor_size, actual_size);
}
errno_t usb_request_get_full_configuration_descriptor_alloc(
usb_pipe_t *pipe, int index,
void **descriptor_ptr, size_t *descriptor_size)
{
errno_t rc;
if (descriptor_ptr == NULL) {
return EBADMEM;
}
usb_standard_configuration_descriptor_t bare_config;
rc = usb_request_get_bare_configuration_descriptor(pipe, index,
&bare_config);
if (rc != EOK) {
return rc;
}
if (bare_config.descriptor_type != USB_DESCTYPE_CONFIGURATION) {
return ENOENT;
}
const size_t total_length = uint16_usb2host(bare_config.total_length);
if (total_length < sizeof(bare_config)) {
return ELIMIT;
}
void *buffer = malloc(total_length);
if (buffer == NULL) {
return ENOMEM;
}
size_t transferred = 0;
rc = usb_request_get_full_configuration_descriptor(pipe, index,
buffer, total_length, &transferred);
if (rc != EOK) {
free(buffer);
return rc;
}
if (transferred != total_length) {
free(buffer);
return ELIMIT;
}
*descriptor_ptr = buffer;
if (descriptor_size != NULL) {
*descriptor_size = total_length;
}
return EOK;
}
errno_t usb_request_set_descriptor(usb_pipe_t *pipe,
usb_request_type_t request_type, usb_request_recipient_t recipient,
uint8_t descriptor_type, uint8_t descriptor_index,
uint16_t language, const void *buffer, size_t size)
{
if (buffer == NULL) {
return EBADMEM;
}
if (size == 0) {
return EINVAL;
}
uint16_t wValue = descriptor_index | (descriptor_type << 8);
return usb_control_request_set(pipe,
request_type, recipient, USB_DEVREQ_SET_DESCRIPTOR,
wValue, language, buffer, size);
}
errno_t usb_request_get_configuration(usb_pipe_t *pipe,
uint8_t *configuration_value)
{
uint8_t value;
size_t actual_size;
const errno_t rc = usb_control_request_get(pipe,
USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
USB_DEVREQ_GET_CONFIGURATION, 0, 0, &value, 1, &actual_size);
if (rc != EOK) {
return rc;
}
if (actual_size != 1) {
return ELIMIT;
}
if (configuration_value != NULL) {
*configuration_value = value;
}
return EOK;
}
errno_t usb_request_set_configuration(usb_pipe_t *pipe,
uint8_t configuration_value)
{
const uint16_t config_value =
uint16_host2usb((uint16_t) configuration_value);
return usb_control_request_set(pipe,
USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
USB_DEVREQ_SET_CONFIGURATION, config_value, 0,
NULL, 0);
}
errno_t usb_request_get_interface(usb_pipe_t *pipe,
uint8_t interface_index, uint8_t *alternate_setting)
{
uint8_t value;
size_t actual_size;
const errno_t rc = usb_control_request_get(pipe,
USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
USB_DEVREQ_GET_INTERFACE,
0, uint16_host2usb((uint16_t) interface_index),
&value, sizeof(value), &actual_size);
if (rc != EOK) {
return rc;
}
if (actual_size != 1) {
return ELIMIT;
}
if (alternate_setting != NULL) {
*alternate_setting = value;
}
return EOK;
}
errno_t usb_request_set_interface(usb_pipe_t *pipe,
uint8_t interface_index, uint8_t alternate_setting)
{
return usb_control_request_set(pipe,
USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
USB_DEVREQ_SET_INTERFACE,
uint16_host2usb((uint16_t) alternate_setting),
uint16_host2usb((uint16_t) interface_index),
NULL, 0);
}
errno_t usb_request_get_supported_languages(usb_pipe_t *pipe,
l18_win_locales_t **languages_ptr, size_t *languages_count)
{
if (languages_ptr == NULL || languages_count == NULL) {
return EBADMEM;
}
uint8_t *string_descriptor = NULL;
size_t string_descriptor_size = 0;
const errno_t rc = usb_request_get_descriptor_alloc(pipe,
USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
USB_DESCTYPE_STRING, 0, 0,
(void **) &string_descriptor, &string_descriptor_size);
if (rc != EOK) {
return rc;
}
if (string_descriptor_size <= 2) {
free(string_descriptor);
return EEMPTY;
}
string_descriptor_size -= 2;
if ((string_descriptor_size % 2) != 0) {
free(string_descriptor);
return ESTALL;
}
const size_t langs_count = string_descriptor_size / 2;
l18_win_locales_t *langs =
calloc(langs_count, sizeof(l18_win_locales_t));
if (langs == NULL) {
free(string_descriptor);
return ENOMEM;
}
for (size_t i = 0; i < langs_count; i++) {
const uint16_t lang_code =
(string_descriptor[2 + 2 * i + 1] << 8) +
string_descriptor[2 + 2 * i];
langs[i] = uint16_usb2host(lang_code);
}
free(string_descriptor);
*languages_ptr = langs;
*languages_count = langs_count;
return EOK;
}
errno_t usb_request_get_string(usb_pipe_t *pipe,
size_t index, l18_win_locales_t lang, char **string_ptr)
{
if (string_ptr == NULL) {
return EBADMEM;
}
if ((index < 1) || (index > 0xFF)) {
return ERANGE;
}
if (lang > L18N_WIN_LOCALE_MAX) {
return ERANGE;
}
errno_t rc;
uint8_t *string = NULL;
char32_t *string_chars = NULL;
size_t string_size;
rc = usb_request_get_descriptor_alloc(pipe,
USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
USB_DESCTYPE_STRING, index, uint16_host2usb(lang),
(void **) &string, &string_size);
if (rc != EOK) {
goto leave;
}
if (string_size <= 2) {
rc = EEMPTY;
goto leave;
}
string_size -= 2;
if ((string_size % 2) != 0) {
rc = ESTALL;
goto leave;
}
const size_t string_char_count = string_size / 2;
string_chars = malloc(sizeof(char32_t) * (string_char_count + 1));
if (string_chars == NULL) {
rc = ENOMEM;
goto leave;
}
for (size_t i = 0; i < string_char_count; i++) {
const uint16_t uni_char = (string[2 + 2 * i + 1] << 8) +
string[2 + 2 * i];
string_chars[i] = uni_char;
}
string_chars[string_char_count] = 0;
char *str = wstr_to_astr(string_chars);
if (str == NULL) {
rc = ENOMEM;
goto leave;
}
*string_ptr = str;
rc = EOK;
leave:
free(string);
free(string_chars);
return rc;
}
static errno_t usb_request_clear_endpoint_halt(usb_pipe_t *pipe, uint16_t ep_index)
{
return usb_request_clear_feature(pipe,
USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_ENDPOINT,
uint16_host2usb(USB_FEATURE_ENDPOINT_HALT),
uint16_host2usb(ep_index));
}
errno_t usb_pipe_clear_halt(usb_pipe_t *ctrl_pipe, usb_pipe_t *target_pipe)
{
if ((ctrl_pipe == NULL) || (target_pipe == NULL)) {
return EINVAL;
}
uint16_t index = target_pipe->desc.endpoint_no;
index |= (target_pipe->desc.direction == USB_DIRECTION_IN) << 7;
return usb_request_clear_endpoint_halt(ctrl_pipe, index);
}
errno_t usb_request_get_endpoint_status(usb_pipe_t *ctrl_pipe, usb_pipe_t *pipe,
uint16_t *status)
{
uint16_t status_tmp;
uint16_t pipe_index = (uint16_t) pipe->desc.endpoint_no;
errno_t rc = usb_request_get_status(ctrl_pipe,
USB_REQUEST_RECIPIENT_ENDPOINT, uint16_host2usb(pipe_index),
&status_tmp);
if (rc != EOK) {
return rc;
}
if (status != NULL) {
*status = uint16_usb2host(status_tmp);
}
return EOK;
}
HelenOS homepage, sources at GitHub