[PATCH 3/5] HID: Use hidraw_instance() in drivers
From: Dmitry Torokhov
Date: Wed Oct 14 2026 - 01:14:34 EST
Instead of directly dereferencing struct hidraw to inspect minor device
numbers, use hidraw_instance(). This removes the dependency of these
drivers on the internal struct hidraw layout.
For hid-cp2112, fall back to dev_name() when hidraw is not available.
For hid-led and hid-u2fzero, require CONFIG_HIDRAW and propagate errors
if hidraw is unavailable.
Assisted-by: LLM
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@xxxxxxxxx>
---
drivers/hid/Kconfig | 3 +++
drivers/hid/hid-cp2112.c | 13 ++++++++-----
drivers/hid/hid-led.c | 8 ++++++--
drivers/hid/hid-u2fzero.c | 8 ++++++--
4 files changed, 23 insertions(+), 9 deletions(-)
diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index f9bcaeb66385..1d2657edab27 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -587,6 +587,7 @@ config HID_LCPOWER
config HID_LED
tristate "Simple RGB LED support"
+ depends on HIDRAW
depends on LEDS_CLASS
help
Support for simple RGB LED devices. Currently supported are:
@@ -1252,6 +1253,7 @@ config HID_TOPRE
config HID_THINGM
tristate "ThingM blink(1) USB RGB LED"
+ depends on HIDRAW
depends on LEDS_CLASS
select HID_LED
help
@@ -1285,6 +1287,7 @@ config HID_UDRAW_PS3
config HID_U2FZERO
tristate "U2F Zero LED and RNG support"
depends on USB_HID
+ depends on HIDRAW
depends on LEDS_CLASS
depends on HW_RANDOM
help
diff --git a/drivers/hid/hid-cp2112.c b/drivers/hid/hid-cp2112.c
index 04379db93571..148946b04bff 100644
--- a/drivers/hid/hid-cp2112.c
+++ b/drivers/hid/hid-cp2112.c
@@ -1225,7 +1225,7 @@ static int cp2112_probe(struct hid_device *hdev, const struct hid_device_id *id)
struct gpio_irq_chip *girq;
struct i2c_timings timings;
u32 addr;
- int ret;
+ int ret, instance;
dev = devm_kzalloc(&hdev->dev, sizeof(*dev), GFP_KERNEL);
if (!dev)
@@ -1330,9 +1330,13 @@ static int cp2112_probe(struct hid_device *hdev, const struct hid_device_id *id)
dev->adap.algo = &smbus_algorithm;
dev->adap.algo_data = dev;
dev->adap.dev.parent = &hdev->dev;
- snprintf(dev->adap.name, sizeof(dev->adap.name),
- "CP2112 SMBus Bridge on hidraw%d",
- ((struct hidraw *)hdev->hidraw)->minor);
+ instance = hidraw_instance(hdev);
+ if (instance >= 0)
+ snprintf(dev->adap.name, sizeof(dev->adap.name),
+ "CP2112 SMBus Bridge on hidraw%d", instance);
+ else
+ snprintf(dev->adap.name, sizeof(dev->adap.name),
+ "CP2112 SMBus Bridge on %s", dev_name(&hdev->dev));
dev->hwversion = buf[2];
init_waitqueue_head(&dev->wait);
@@ -1494,4 +1498,3 @@ module_hid_driver(cp2112_driver);
MODULE_DESCRIPTION("Silicon Labs HID USB to SMBus master bridge");
MODULE_AUTHOR("David Barksdale <dbarksdale@xxxxxxxxxxx>");
MODULE_LICENSE("GPL");
-
diff --git a/drivers/hid/hid-led.c b/drivers/hid/hid-led.c
index 7d82f8d426bb..63fd723d9e69 100644
--- a/drivers/hid/hid-led.c
+++ b/drivers/hid/hid-led.c
@@ -448,7 +448,7 @@ static int hidled_init_rgb(struct hidled_rgb *rgb, unsigned int minor)
static int hidled_probe(struct hid_device *hdev, const struct hid_device_id *id)
{
struct hidled_device *ldev;
- unsigned int minor;
+ int minor;
int ret, i;
ldev = devm_kzalloc(&hdev->dev, sizeof(*ldev), GFP_KERNEL);
@@ -488,7 +488,11 @@ static int hidled_probe(struct hid_device *hdev, const struct hid_device_id *id)
if (ret)
return ret;
- minor = ((struct hidraw *) hdev->hidraw)->minor;
+ minor = hidraw_instance(hdev);
+ if (minor < 0) {
+ hid_hw_stop(hdev);
+ return minor;
+ }
for (i = 0; i < ldev->config->num_leds; i++) {
ldev->rgb[i].ldev = ldev;
diff --git a/drivers/hid/hid-u2fzero.c b/drivers/hid/hid-u2fzero.c
index 82404b6e2d25..83e223c12cd6 100644
--- a/drivers/hid/hid-u2fzero.c
+++ b/drivers/hid/hid-u2fzero.c
@@ -307,7 +307,7 @@ static int u2fzero_probe(struct hid_device *hdev,
const struct hid_device_id *id)
{
struct u2fzero_device *dev;
- unsigned int minor;
+ int minor;
int ret;
if (!hid_is_usb(hdev))
@@ -347,7 +347,11 @@ static int u2fzero_probe(struct hid_device *hdev,
dev->present = true;
- minor = ((struct hidraw *) hdev->hidraw)->minor;
+ minor = hidraw_instance(hdev);
+ if (minor < 0) {
+ ret = minor;
+ goto err_free_urb;
+ }
ret = u2fzero_init_led(dev, minor);
if (ret)
--
2.56.0.385.gd3acb90ef8-goog