Skip to content

Commit b259923

Browse files
jfischer-nocarlescufi
authored andcommitted
[nrf fromtree] usb: device_next: fail on initialization if no HID device is registered
Do not register the device when the class instance has already been initialised. Fail on initialization if no HID device is registered. Signed-off-by: Johann Fischer <[email protected]> (cherry picked from commit c62575e)
1 parent a940f9d commit b259923

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

subsys/usb/device_next/class/usbd_hid.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ struct usbd_hid_descriptor {
5757
};
5858

5959
enum {
60+
HID_DEV_CLASS_INITIALIZED,
6061
HID_DEV_CLASS_ENABLED,
6162
};
6263

@@ -503,7 +504,14 @@ static int usbd_hid_init(struct usbd_class_data *const c_data)
503504
const struct device *dev = usbd_class_get_private(c_data);
504505
const struct hid_device_config *dcfg = dev->config;
505506
struct usbd_hid_descriptor *const desc = dcfg->desc;
507+
struct hid_device_data *const ddata = dev->data;
508+
509+
if (ddata->ops == NULL || ddata->rdesc == NULL || !ddata->rsize) {
510+
LOG_ERR("HID device does not seem to be registered");
511+
return -EINVAL;
512+
}
506513

514+
atomic_set_bit(&ddata->state, HID_DEV_CLASS_INITIALIZED);
507515
LOG_DBG("HID class %s init", c_data->name);
508516

509517
if (dcfg->if_desc_data != NULL && desc->if0.iInterface == 0) {
@@ -519,6 +527,10 @@ static int usbd_hid_init(struct usbd_class_data *const c_data)
519527

520528
static void usbd_hid_shutdown(struct usbd_class_data *const c_data)
521529
{
530+
const struct device *dev = usbd_class_get_private(c_data);
531+
struct hid_device_data *const ddata = dev->data;
532+
533+
atomic_clear_bit(&ddata->state, HID_DEV_CLASS_INITIALIZED);
522534
LOG_DBG("HID class %s shutdown", c_data->name);
523535
}
524536

@@ -628,7 +640,7 @@ static int hid_dev_register(const struct device *dev,
628640
struct hid_device_data *const ddata = dev->data;
629641
struct usbd_hid_descriptor *const desc = dcfg->desc;
630642

631-
if (atomic_test_bit(&ddata->state, HID_DEV_CLASS_ENABLED)) {
643+
if (atomic_test_bit(&ddata->state, HID_DEV_CLASS_INITIALIZED)) {
632644
return -EALREADY;
633645
}
634646

0 commit comments

Comments
 (0)