Skip to content

Commit

Permalink
usb: check request direction in request handlers
Browse files Browse the repository at this point in the history
Check request direction in standard device,
interface, and endpoint request handlers.

Signed-off-by: Johann Fischer <[email protected]>
  • Loading branch information
jfischer-no authored and nashif committed Aug 3, 2021
1 parent af3e109 commit 4b79912
Showing 1 changed file with 86 additions and 73 deletions.
159 changes: 86 additions & 73 deletions subsys/usb/usb_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -801,54 +801,61 @@ static bool usb_handle_std_device_req(struct usb_setup_packet *setup,
{
uint8_t *data = *data_buf;

switch (setup->bRequest) {
case USB_SREQ_GET_STATUS:
return usb_get_status(setup, len, data_buf);

case USB_SREQ_SET_ADDRESS:
LOG_DBG("Set Address %u request", setup->wValue);
return !usb_dc_set_address(setup->wValue);

case USB_SREQ_GET_DESCRIPTOR:
return usb_get_descriptor(setup, len, data_buf);

case USB_SREQ_GET_CONFIGURATION:
LOG_DBG("Get Configuration request");
/* indicate if we are configured */
data[0] = usb_dev.configuration;
*len = 1;
return true;

case USB_SREQ_SET_CONFIGURATION:
return usb_set_configuration(setup);

case USB_SREQ_CLEAR_FEATURE:
LOG_DBG("Clear Feature request");

if (IS_ENABLED(CONFIG_USB_DEVICE_REMOTE_WAKEUP)) {
if (setup->wValue == USB_SFS_REMOTE_WAKEUP) {
usb_dev.remote_wakeup = false;
return true;
}
if (usb_reqtype_is_to_host(setup)) {
switch (setup->bRequest) {
case USB_SREQ_GET_STATUS:
return usb_get_status(setup, len, data_buf);

case USB_SREQ_GET_DESCRIPTOR:
return usb_get_descriptor(setup, len, data_buf);

case USB_SREQ_GET_CONFIGURATION:
LOG_DBG("Get Configuration request");
/* indicate if we are configured */
data[0] = usb_dev.configuration;
*len = 1;
return true;
default:
break;
}
return false;
} else {
switch (setup->bRequest) {
case USB_SREQ_SET_ADDRESS:
LOG_DBG("Set Address %u request", setup->wValue);
return !usb_dc_set_address(setup->wValue);

case USB_SREQ_SET_CONFIGURATION:
return usb_set_configuration(setup);

case USB_SREQ_CLEAR_FEATURE:
LOG_DBG("Clear Feature request");

if (IS_ENABLED(CONFIG_USB_DEVICE_REMOTE_WAKEUP)) {
if (setup->wValue == USB_SFS_REMOTE_WAKEUP) {
usb_dev.remote_wakeup = false;
return true;
}
}
break;

case USB_SREQ_SET_FEATURE:
LOG_DBG("Set Feature request");
case USB_SREQ_SET_FEATURE:
LOG_DBG("Set Feature request");

if (IS_ENABLED(CONFIG_USB_DEVICE_REMOTE_WAKEUP)) {
if (setup->wValue == USB_SFS_REMOTE_WAKEUP) {
usb_dev.remote_wakeup = true;
return true;
if (IS_ENABLED(CONFIG_USB_DEVICE_REMOTE_WAKEUP)) {
if (setup->wValue == USB_SFS_REMOTE_WAKEUP) {
usb_dev.remote_wakeup = true;
return true;
}
}
}
return false;
break;

default:
LOG_DBG("Unsupported bmRequestType 0x%02x bRequest 0x%02x",
setup->bmRequestType, setup->bRequest);
default:
break;
}
}

LOG_DBG("Unsupported bmRequestType 0x%02x bRequest 0x%02x",
setup->bmRequestType, setup->bRequest);
return false;
}

Expand Down Expand Up @@ -903,25 +910,30 @@ static bool usb_handle_std_interface_req(struct usb_setup_packet *setup,
return false;
}

switch (setup->bRequest) {
case USB_SREQ_GET_STATUS:
/* no bits specified */
data[0] = 0U;
data[1] = 0U;
*len = 2;
return true;
if (usb_reqtype_is_to_host(setup)) {
switch (setup->bRequest) {
case USB_SREQ_GET_STATUS:
/* no bits specified */
data[0] = 0U;
data[1] = 0U;
*len = 2;
return true;

case USB_SREQ_GET_INTERFACE:
return usb_get_interface(setup, len, data_buf);
case USB_SREQ_GET_INTERFACE:
return usb_get_interface(setup, len, data_buf);

case USB_SREQ_SET_INTERFACE:
return usb_set_interface(setup);
default:
break;
}
} else {
if (setup->bRequest == USB_SREQ_SET_INTERFACE) {
return usb_set_interface(setup);
}

default:
LOG_DBG("Unsupported bmRequestType 0x%02x bRequest 0x%02x",
setup->bmRequestType, setup->bRequest);
}

LOG_DBG("Unsupported bmRequestType 0x%02x bRequest 0x%02x",
setup->bmRequestType, setup->bRequest);
return false;
}

Expand Down Expand Up @@ -1040,29 +1052,30 @@ static bool usb_halt_endpoint_req(struct usb_setup_packet *setup, bool halt)
static bool usb_handle_std_endpoint_req(struct usb_setup_packet *setup,
int32_t *len, uint8_t **data_buf)
{
switch (setup->bRequest) {
case USB_SREQ_GET_STATUS:
return usb_get_status_endpoint(setup, len, data_buf);

case USB_SREQ_CLEAR_FEATURE:
if (setup->wValue == USB_SFS_ENDPOINT_HALT) {
return usb_halt_endpoint_req(setup, false);
if (usb_reqtype_is_to_host(setup)) {
if (setup->bRequest == USB_SREQ_GET_STATUS) {
return usb_get_status_endpoint(setup, len, data_buf);
}

return false;

case USB_SREQ_SET_FEATURE:
if (setup->wValue == USB_SFS_ENDPOINT_HALT) {
return usb_halt_endpoint_req(setup, true);
} else {
switch (setup->bRequest) {
case USB_SREQ_CLEAR_FEATURE:
if (setup->wValue == USB_SFS_ENDPOINT_HALT) {
return usb_halt_endpoint_req(setup, false);
}
break;
case USB_SREQ_SET_FEATURE:
if (setup->wValue == USB_SFS_ENDPOINT_HALT) {
return usb_halt_endpoint_req(setup, true);
}
break;
default:
break;
}

return false;

default:
LOG_DBG("Unsupported bmRequestType 0x%02x bRequest 0x%02x",
setup->bmRequestType, setup->bRequest);
}

LOG_DBG("Unsupported bmRequestType 0x%02x bRequest 0x%02x",
setup->bmRequestType, setup->bRequest);
return false;
}

Expand Down

0 comments on commit 4b79912

Please sign in to comment.