Skip to content

Commit

Permalink
usb: hid: default request handlers
Browse files Browse the repository at this point in the history
HID class now defaults to universal request callbacks in case they
are not provided by the application. This applies to following,
class-specific requests: Get_Report, Set_Report, Get_Idle, Set_Idle,
Get_Protocol and Set_Protocol.
Tested with USB3CV.

Signed-off-by: Marcin Szymczyk <Marcin.Szymczyk@nordicsemi.no>
  • Loading branch information
masz-nordic authored and nashif committed Dec 4, 2018
1 parent 4565ca8 commit 6a4ddff
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 42 deletions.
35 changes: 1 addition & 34 deletions samples/subsys/usb/hid/src/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2016-2018 Intel Corporation.
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -56,34 +57,6 @@ static const u8_t hid_report_desc[] = {
HID_MI_COLLECTION_END,
};

static int debug_cb(struct usb_setup_packet *setup, s32_t *len,
u8_t **data)
{
LOG_DBG("Debug callback");

return -ENOTSUP;
}

static int set_idle_cb(struct usb_setup_packet *setup, s32_t *len,
u8_t **data)
{
LOG_DBG("Set Idle callback");

/* TODO: Do something */

return 0;
}

static int get_report_cb(struct usb_setup_packet *setup, s32_t *len,
u8_t **data)
{
LOG_DBG("Get report callback");

/* TODO: Do something */

return 0;
}

static void send_report(struct k_work *work)
{
static u8_t report_1[2] = { REPORT_ID_1, 0x00 };
Expand Down Expand Up @@ -117,12 +90,6 @@ static void status_cb(enum usb_dc_status_code status, const u8_t *param)
}

static const struct hid_ops ops = {
.get_report = get_report_cb,
.get_idle = debug_cb,
.get_protocol = debug_cb,
.set_report = debug_cb,
.set_idle = set_idle_cb,
.set_protocol = debug_cb,
.int_in_ready = in_ready_cb,
.status_cb = status_cb,
};
Expand Down
102 changes: 94 additions & 8 deletions subsys/usb/class/hid/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Human Interface Device (HID) USB class core
*
* Copyright (c) 2018 Intel Corporation
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -89,6 +90,66 @@ static struct hid_device_info {
const struct hid_ops *ops;
} hid_device;

static int hid_on_get_idle(struct usb_setup_packet *setup, s32_t *len,
u8_t **data)
{
LOG_DBG("Get Idle callback");

/* TODO: Do something. */

return -ENOTSUP;
}

static int hid_on_get_report(struct usb_setup_packet *setup, s32_t *len,
u8_t **data)
{
LOG_DBG("Get Report callback");

/* TODO: Do something. */

return 0;
}

static int hid_on_get_protocol(struct usb_setup_packet *setup, s32_t *len,
u8_t **data)
{
LOG_DBG("Get Protocol callback");

/* TODO: Do something. */

return -ENOTSUP;
}

static int hid_on_set_idle(struct usb_setup_packet *setup, s32_t *len,
u8_t **data)
{
LOG_DBG("Set Idle callback");

/* TODO: Do something. */

return 0;
}

static int hid_on_set_report(struct usb_setup_packet *setup, s32_t *len,
u8_t **data)
{
LOG_DBG("Set Report callback");

/* TODO: Do something. */

return -ENOTSUP;
}

static int hid_on_set_protocol(struct usb_setup_packet *setup, s32_t *len,
u8_t **data)
{
LOG_DBG("Set Protocol callback");

/* TODO: Do something. */

return -ENOTSUP;
}

static void usb_set_hid_report_size(u16_t size)
{
sys_put_le16(size,
Expand Down Expand Up @@ -140,14 +201,28 @@ static int hid_class_handle_req(struct usb_setup_packet *setup,

if (REQTYPE_GET_DIR(setup->bmRequestType) == REQTYPE_DIR_TO_HOST) {
switch (setup->bRequest) {
case HID_GET_IDLE:
if (hid_device.ops->get_idle) {
return hid_device.ops->get_idle(setup, len,
data);
} else {
return hid_on_get_idle(setup, len, data);
}
break;
case HID_GET_REPORT:
LOG_DBG("Get Report");
if (hid_device.ops->get_report) {
return hid_device.ops->get_report(setup, len,
data);
} else {
LOG_ERR("Mandatory request not supported");
return -EINVAL;
return hid_on_get_report(setup, len, data);
}
break;
case HID_GET_PROTOCOL:
if (hid_device.ops->get_protocol) {
return hid_device.ops->get_protocol(setup, len,
data);
} else {
return hid_on_get_protocol(setup, len, data);
}
break;
default:
Expand All @@ -157,18 +232,29 @@ static int hid_class_handle_req(struct usb_setup_packet *setup,
} else {
switch (setup->bRequest) {
case HID_SET_IDLE:
LOG_DBG("Set Idle");
if (hid_device.ops->set_idle) {
return hid_device.ops->set_idle(setup, len,
data);
} else {
return hid_on_set_idle(setup, len, data);
}
break;
case HID_SET_REPORT:
if (hid_device.ops->set_report == NULL) {
LOG_ERR("set_report not implemented");
return -EINVAL;
if (hid_device.ops->set_report) {
return hid_device.ops->set_report(setup, len,
data);
} else {
return hid_on_set_report(setup, len, data);
}
break;
case HID_SET_PROTOCOL:
if (hid_device.ops->set_protocol) {
return hid_device.ops->set_protocol(setup, len,
data);
} else {
return hid_on_set_protocol(setup, len, data);
}
return hid_device.ops->set_report(setup, len, data);
break;
default:
LOG_ERR("Unhandled request 0x%x", setup->bRequest);
break;
Expand Down

0 comments on commit 6a4ddff

Please sign in to comment.