Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions include/zephyr/usb/usbd.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,33 @@ struct usbd_str_desc_data {
* Example callback code fragment:
*
* @code{.c}
* static int foo_to_host_cb(const struct usbd_context *const ctx,
* const struct usb_setup_packet *const setup,
* struct net_buf *const buf)
* static net_buf *foo_to_host_cb(const struct usbd_context *const ctx,
* const struct usb_setup_packet *const setup)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this commit makes it simpler, but rather inconsistent. You actually add more code, +511 −319.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The starting point is consistent but IMHO overly complicated and not really well suited for memory constrained devices.

* {
* if (setup->wIndex == WEBUSB_REQ_GET_URL) {
* struct net_buf *buf;
* uint16_t len;
* uint8_t index = USB_GET_DESCRIPTOR_INDEX(setup->wValue);
*
* if (index != SAMPLE_WEBUSB_LANDING_PAGE) {
* return -ENOTSUP;
* errno = -ENOTSUP;
* return NULL;
* }
*
* net_buf_add_mem(buf, &webusb_origin_url,
* MIN(net_buf_tailroom(buf), sizeof(webusb_origin_url)));
* len = MIN(setup->wLength, sizeof(webusb_origin_url));
* buf = usbd_ep_ctrl_data_in_alloc(ctx, len);
* if (buf == NULL) {
* errno = -ENOMEM;
* return NULL;
* }
*
* net_buf_add_mem(buf, &webusb_origin_url, len);
*
* return 0;
* return buf;
* }
*
* return -ENOTSUP;
* errno = -ENOTSUP;
* return NULL;
* }
* @endcode
*/
Expand All @@ -128,9 +137,8 @@ struct usbd_vreq_node {
/** Vendor code (bRequest value) */
const uint8_t code;
/** Vendor request callback for device-to-host direction */
int (*to_host)(const struct usbd_context *const ctx,
const struct usb_setup_packet *const setup,
struct net_buf *const buf);
struct net_buf *(*to_host)(const struct usbd_context *const ctx,
const struct usb_setup_packet *const setup);
/** Vendor request callback for host-to-device direction */
int (*to_dev)(const struct usbd_context *const ctx,
const struct usb_setup_packet *const setup,
Expand Down Expand Up @@ -342,9 +350,8 @@ struct usbd_class_api {
const struct net_buf *const buf);

/** USB control request handler to host */
int (*control_to_host)(struct usbd_class_data *const c_data,
const struct usb_setup_packet *const setup,
struct net_buf *const buf);
struct net_buf *(*control_to_host)(struct usbd_class_data *const c_data,
const struct usb_setup_packet *const setup);

/** Endpoint request completion event handler */
int (*request)(struct usbd_class_data *const c_data,
Expand Down
22 changes: 15 additions & 7 deletions samples/subsys/dap/src/msosv2.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,31 @@ struct bos_msosv2_descriptor bos_msosv2_desc = {
},
};

static int msosv2_to_host_cb(const struct usbd_context *const ctx,
const struct usb_setup_packet *const setup,
struct net_buf *const buf)
static struct net_buf *msosv2_to_host_cb(const struct usbd_context *const ctx,
const struct usb_setup_packet *const setup)
{
LOG_INF("Vendor callback to host");

if (setup->bRequest == SAMPLE_MSOS2_VENDOR_CODE &&
setup->wIndex == MS_OS_20_DESCRIPTOR_INDEX) {
struct net_buf *buf;
uint16_t len = MIN(setup->wLength, sizeof(msosv2_desc));

LOG_INF("Get MS OS 2.0 Descriptor Set");

net_buf_add_mem(buf, &msosv2_desc,
MIN(net_buf_tailroom(buf), sizeof(msosv2_desc)));
buf = usbd_ep_ctrl_data_in_alloc(ctx, len);
if (buf == NULL) {
errno = -ENOMEM;
return NULL;
}

net_buf_add_mem(buf, &msosv2_desc, len);

return 0;
return buf;
}

return -ENOTSUP;
errno = -ENOTSUP;
return NULL;
}

USBD_DESC_BOS_VREQ_DEFINE(bos_vreq_msosv2, sizeof(bos_msosv2_desc), &bos_msosv2_desc,
Expand Down
25 changes: 17 additions & 8 deletions samples/subsys/dap/src/webusb.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,36 @@ static const uint8_t webusb_origin_url[] = {
'o', 'r', 'g', '/',
};

static int webusb_to_host_cb(const struct usbd_context *const ctx,
const struct usb_setup_packet *const setup,
struct net_buf *const buf)
static struct net_buf *webusb_to_host_cb(const struct usbd_context *const ctx,
const struct usb_setup_packet *const setup)
{
LOG_INF("Vendor callback to host");

if (setup->wIndex == WEBUSB_REQ_GET_URL) {
struct net_buf *buf;
uint16_t len;
uint8_t index = USB_GET_DESCRIPTOR_INDEX(setup->wValue);

if (index != SAMPLE_WEBUSB_LANDING_PAGE) {
return -ENOTSUP;
errno = -ENOTSUP;
return NULL;
}

len = MIN(setup->wLength, sizeof(webusb_origin_url));
buf = usbd_ep_ctrl_data_in_alloc(ctx, len);
if (buf == NULL) {
errno = -ENOMEM;
return NULL;
}

LOG_INF("Get URL request, index %u", index);
net_buf_add_mem(buf, &webusb_origin_url,
MIN(net_buf_tailroom(buf), sizeof(webusb_origin_url)));
net_buf_add_mem(buf, &webusb_origin_url, len);

return 0;
return buf;
}

return -ENOTSUP;
errno = -ENOTSUP;
return NULL;
}

USBD_DESC_BOS_VREQ_DEFINE(bos_vreq_webusb, sizeof(bos_cap_webusb), &bos_cap_webusb,
Expand Down
23 changes: 16 additions & 7 deletions samples/subsys/usb/webusb/src/msosv2.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,32 @@ struct bos_msosv2_descriptor bos_msosv2_desc = {
},
};

static int msosv2_to_host_cb(const struct usbd_context *const ctx,
const struct usb_setup_packet *const setup,
struct net_buf *const buf)
static struct net_buf *msosv2_to_host_cb(const struct usbd_context *const ctx,
const struct usb_setup_packet *const setup)
{
LOG_INF("Vendor callback to host");

if (setup->bRequest == SAMPLE_MSOS2_VENDOR_CODE &&
setup->wIndex == MS_OS_20_DESCRIPTOR_INDEX) {
struct net_buf *buf;
uint16_t len;

LOG_INF("Get MS OS 2.0 Descriptor Set");

net_buf_add_mem(buf, &msosv2_desc,
MIN(net_buf_tailroom(buf), sizeof(msosv2_desc)));
len = MIN(setup->wLength, sizeof(msosv2_desc));
buf = usbd_ep_ctrl_data_in_alloc(ctx, len);
if (buf == NULL) {
errno = -ENOMEM;
return NULL;
}

net_buf_add_mem(buf, &msosv2_desc, len);

return 0;
return buf;
}

return -ENOTSUP;
errno = -ENOTSUP;
return NULL;
}

USBD_DESC_BOS_VREQ_DEFINE(bos_vreq_msosv2, sizeof(bos_msosv2_desc), &bos_msosv2_desc,
Expand Down
25 changes: 17 additions & 8 deletions samples/subsys/usb/webusb/src/webusb.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,36 @@ static const uint8_t webusb_origin_url[] = {
'l', 'o', 'c', 'a', 'l', 'h', 'o', 's', 't', ':', '8', '0', '0', '0'
};

static int webusb_to_host_cb(const struct usbd_context *const ctx,
const struct usb_setup_packet *const setup,
struct net_buf *const buf)
static struct net_buf *webusb_to_host_cb(const struct usbd_context *const ctx,
const struct usb_setup_packet *const setup)
{
LOG_INF("Vendor callback to host");

if (setup->wIndex == WEBUSB_REQ_GET_URL) {
struct net_buf *buf;
uint16_t len;
uint8_t index = USB_GET_DESCRIPTOR_INDEX(setup->wValue);

if (index != SAMPLE_WEBUSB_LANDING_PAGE) {
return -ENOTSUP;
errno = -ENOTSUP;
return NULL;
}

len = MIN(setup->wLength, sizeof(webusb_origin_url));
buf = usbd_ep_ctrl_data_in_alloc(ctx, len);
if (buf == NULL) {
errno = -ENOMEM;
return NULL;
}

LOG_INF("Get URL request, index %u", index);
net_buf_add_mem(buf, &webusb_origin_url,
MIN(net_buf_tailroom(buf), sizeof(webusb_origin_url)));
net_buf_add_mem(buf, &webusb_origin_url, len);

return 0;
return buf;
}

return -ENOTSUP;
errno = -ENOTSUP;
return NULL;
}

USBD_DESC_BOS_VREQ_DEFINE(bos_vreq_webusb, sizeof(bos_cap_webusb), &bos_cap_webusb,
Expand Down
26 changes: 16 additions & 10 deletions subsys/usb/device_next/class/loopback.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,29 +202,35 @@ static void lb_update(struct usbd_class_data *c_data,
c_data, iface, alternate);
}

static int lb_control_to_host(struct usbd_class_data *c_data,
const struct usb_setup_packet *const setup,
struct net_buf *const buf)
static struct net_buf *lb_control_to_host(struct usbd_class_data *c_data,
const struct usb_setup_packet *const setup)
{
if (setup->RequestType.recipient != USB_REQTYPE_RECIPIENT_DEVICE) {
errno = -ENOTSUP;
return 0;
return NULL;
}

if (setup->bRequest == LB_VENDOR_REQ_IN) {
net_buf_add_mem(buf, lb_buf,
MIN(sizeof(lb_buf), setup->wLength));
struct net_buf *buf;
uint16_t len = MIN(sizeof(lb_buf), setup->wLength);

buf = usbd_ep_ctrl_data_in_alloc(usbd_class_get_ctx(c_data), len);
if (buf == NULL) {
errno = -ENOMEM;
return NULL;
}

LOG_WRN("Device-to-Host, wLength %u | %zu", setup->wLength,
MIN(sizeof(lb_buf), setup->wLength));
net_buf_add_mem(buf, lb_buf, len);

return 0;
LOG_WRN("Device-to-Host, wLength %u | %zu", setup->wLength, len);

return buf;
}

LOG_ERR("Class request 0x%x not supported", setup->bRequest);
errno = -ENOTSUP;

return 0;
return NULL;
}

static int lb_control_to_dev(struct usbd_class_data *c_data,
Expand Down
16 changes: 9 additions & 7 deletions subsys/usb/device_next/class/usbd_cdc_acm.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,31 +484,33 @@ static void cdc_acm_update_linestate(struct cdc_acm_uart_data *const data)
}
}

static int usbd_cdc_acm_cth(struct usbd_class_data *const c_data,
const struct usb_setup_packet *const setup,
struct net_buf *const buf)
static struct net_buf *usbd_cdc_acm_cth(struct usbd_class_data *const c_data,
const struct usb_setup_packet *const setup)
{
const struct device *dev = usbd_class_get_private(c_data);
struct cdc_acm_uart_data *data = dev->data;
struct net_buf *buf;
size_t min_len;

if (setup->bRequest == GET_LINE_CODING) {
min_len = MIN(sizeof(data->line_coding), setup->wLength);

buf = usbd_ep_ctrl_data_in_alloc(usbd_class_get_ctx(c_data), min_len);
if (buf == NULL) {
errno = -ENOMEM;
return 0;
return NULL;
}

min_len = MIN(sizeof(data->line_coding), setup->wLength);
net_buf_add_mem(buf, &data->line_coding, min_len);

return 0;
return buf;
}

LOG_DBG("bmRequestType 0x%02x bRequest 0x%02x unsupported",
setup->bmRequestType, setup->bRequest);
errno = -ENOTSUP;

return 0;
return NULL;
}

static int usbd_cdc_acm_ctd(struct usbd_class_data *const c_data,
Expand Down
30 changes: 22 additions & 8 deletions subsys/usb/device_next/class/usbd_cdc_ncm.c
Original file line number Diff line number Diff line change
Expand Up @@ -918,9 +918,25 @@ static int usbd_cdc_ncm_ctd(struct usbd_class_data *const c_data,
return 0;
}

static int usbd_cdc_ncm_cth(struct usbd_class_data *const c_data,
const struct usb_setup_packet *const setup,
struct net_buf *const buf)
static struct net_buf *cdc_ncm_cth_response(struct usbd_class_data *const c_data,
const struct usb_setup_packet *const setup,
const void *data, uint16_t data_len)
{
struct net_buf *buf;
uint16_t len = MIN(setup->wLength, data_len);

buf = usbd_ep_ctrl_data_in_alloc(usbd_class_get_ctx(c_data), len);
if (buf == NULL) {
errno = -ENOMEM;
return NULL;
}

net_buf_add_mem(buf, data, len);
return buf;
}

static struct net_buf *usbd_cdc_ncm_cth(struct usbd_class_data *const c_data,
const struct usb_setup_packet *const setup)
{
LOG_DBG("%d: %d %d %d %d", setup->RequestType.type, setup->bRequest,
setup->wLength, setup->wIndex, setup->wValue);
Expand Down Expand Up @@ -948,8 +964,7 @@ static int usbd_cdc_ncm_cth(struct usbd_class_data *const c_data,
};

LOG_DBG("GET_NTB_PARAMETERS");
net_buf_add_mem(buf, &ntb_params, sizeof(ntb_params));
break;
return cdc_ncm_cth_response(c_data, setup, &ntb_params, sizeof(ntb_params));
}

case GET_NTB_INPUT_SIZE: {
Expand All @@ -960,8 +975,7 @@ static int usbd_cdc_ncm_cth(struct usbd_class_data *const c_data,
};

LOG_DBG("GET_NTB_INPUT_SIZE");
net_buf_add_mem(buf, &input_size, sizeof(input_size));
break;
return cdc_ncm_cth_response(c_data, setup, &input_size, sizeof(input_size));
}

default:
Expand All @@ -971,7 +985,7 @@ static int usbd_cdc_ncm_cth(struct usbd_class_data *const c_data,
}

out:
return 0;
return NULL;
}

static int usbd_cdc_ncm_init(struct usbd_class_data *const c_data)
Expand Down
Loading
Loading