Skip to content
Permalink
Browse files

usb: sam0: Ensure API arguments are valid

Make sure the parameters for the API functions are valid, return error
otherwise.

This is expected by the tests/subsys/usb/device test case.

Signed-off-by: Benjamin Valentin <benpicco@googlemail.com>
  • Loading branch information...
benpicco authored and galak committed Apr 23, 2019
1 parent ffafa9c commit f95f5ae41e5f95ff4da491a633f204e19fbc8817
Showing with 64 additions and 0 deletions.
  1. +64 −0 drivers/usb/device/usb_dc_sam0.c
@@ -362,6 +362,11 @@ int usb_dc_ep_set_stall(const u8_t ep)
u8_t ep_num = ep & ~USB_EP_DIR_MASK;
UsbDeviceEndpoint *endpoint = &regs->DeviceEndpoint[ep_num];

if (ep_num >= USB_NUM_ENDPOINTS) {
LOG_ERR("endpoint index/address out of range");
return -1;
}

if (for_in) {
endpoint->EPSTATUSSET.bit.STALLRQ1 = 1;
} else {
@@ -378,6 +383,11 @@ int usb_dc_ep_clear_stall(const u8_t ep)
u8_t ep_num = ep & ~USB_EP_DIR_MASK;
UsbDeviceEndpoint *endpoint = &regs->DeviceEndpoint[ep_num];

if (ep_num >= USB_NUM_ENDPOINTS) {
LOG_ERR("endpoint index/address out of range");
return -1;
}

if (for_in) {
endpoint->EPSTATUSCLR.bit.STALLRQ1 = 1;
} else {
@@ -394,6 +404,16 @@ int usb_dc_ep_is_stalled(const u8_t ep, u8_t *stalled)
u8_t ep_num = ep & ~USB_EP_DIR_MASK;
UsbDeviceEndpoint *endpoint = &regs->DeviceEndpoint[ep_num];

if (ep_num >= USB_NUM_ENDPOINTS) {
LOG_ERR("endpoint index/address out of range");
return -1;
}

if (stalled == NULL) {
LOG_ERR("parameter must not be NULL");
return -1;
}

if (for_in) {
*stalled = endpoint->EPSTATUS.bit.STALLRQ1;
} else {
@@ -411,6 +431,11 @@ int usb_dc_ep_enable(const u8_t ep)
u8_t ep_num = ep & ~USB_EP_DIR_MASK;
UsbDeviceEndpoint *endpoint = &regs->DeviceEndpoint[ep_num];

if (ep_num >= USB_NUM_ENDPOINTS) {
LOG_ERR("endpoint index/address out of range");
return -1;
}

if (for_in) {
endpoint->EPSTATUSCLR.bit.BK1RDY = 1;
} else {
@@ -434,6 +459,11 @@ int usb_dc_ep_write(u8_t ep, const u8_t *buf, u32_t len, u32_t *ret_bytes)
UsbDeviceDescriptor *desc = &data->descriptors[ep_num];
u32_t addr = desc->DeviceDescBank[1].ADDR.reg;

if (ep_num >= USB_NUM_ENDPOINTS) {
LOG_ERR("endpoint index/address out of range");
return -1;
}

if (endpoint->EPSTATUS.bit.BK1RDY) {
/* Write in progress, drop */
return -EAGAIN;
@@ -469,6 +499,11 @@ int usb_dc_ep_read_ex(u8_t ep, u8_t *buf, u32_t max_data_len,
u32_t take;
int remain;

if (ep_num >= USB_NUM_ENDPOINTS) {
LOG_ERR("endpoint index/address out of range");
return -1;
}

if (!endpoint->EPSTATUS.bit.BK0RDY) {
return -EAGAIN;
}
@@ -527,6 +562,11 @@ int usb_dc_ep_read_continue(u8_t ep)
u8_t ep_num = ep & ~USB_EP_DIR_MASK;
UsbDeviceEndpoint *endpoint = &regs->DeviceEndpoint[ep_num];

if (ep_num >= USB_NUM_ENDPOINTS) {
LOG_ERR("endpoint index/address out of range");
return -1;
}

endpoint->EPSTATUSCLR.bit.BK0RDY = 1;
data->out_at = 0U;

@@ -539,6 +579,11 @@ int usb_dc_ep_set_callback(const u8_t ep, const usb_dc_ep_callback cb)
u8_t for_in = ep & USB_EP_DIR_MASK;
u8_t ep_num = ep & ~USB_EP_DIR_MASK;

if (ep_num >= USB_NUM_ENDPOINTS) {
LOG_ERR("endpoint index/address out of range");
return -1;
}

data->ep_cb[for_in ? 1 : 0][ep_num] = cb;

return 0;
@@ -547,14 +592,33 @@ int usb_dc_ep_set_callback(const u8_t ep, const usb_dc_ep_callback cb)
int usb_dc_ep_mps(const u8_t ep)
{
struct usb_sam0_data *data = usb_sam0_get_data();
UsbDevice *regs = &REGS->DEVICE;
u8_t for_in = ep & USB_EP_DIR_MASK;
u8_t ep_num = ep & ~USB_EP_DIR_MASK;
UsbDeviceDescriptor *desc = &data->descriptors[ep_num];
UsbDeviceEndpoint *endpoint = &regs->DeviceEndpoint[ep];
int size;

if (ep_num >= USB_NUM_ENDPOINTS) {
LOG_ERR("endpoint index/address out of range");
return -1;
}

if (for_in) {

/* if endpoint is not configured, this should return 0 */
if (endpoint->EPCFG.bit.EPTYPE1 == 0) {
return 0;
}

size = desc->DeviceDescBank[1].PCKSIZE.bit.SIZE;
} else {

/* if endpoint is not configured, this should return 0 */
if (endpoint->EPCFG.bit.EPTYPE0 == 0) {
return 0;
}

size = desc->DeviceDescBank[0].PCKSIZE.bit.SIZE;
}

0 comments on commit f95f5ae

Please sign in to comment.
You can’t perform that action at this time.