Skip to contentSkip to conversation

HID: core: Expose id attributes in sysfs

PATCH Applied Vicki Pfau 2 participants 2 messages Jun 24 – Aug 3 HID

lore
Share & tools
lore

Vicki Pfau

author
#0 lore raw

PATCH 1/1 HID: core: Expose id attributes in sysfs

udev rules for handling input devices generally match on idVendor and idProduct for USB hidraw or id/vendor and id/product for evdev nodes. However, hidraw nodes that aren't created by the USB subsystem will only expose this information to udev via the kernel path itself. This leads to doing substring matching, which can be error-prone or overzealous. Instead, since the HID subsystem already has this information, we can expose it directly in the same format that evdev exposes it.

---
 drivers/hid/hid-core.c | 45 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)
drivers/hid/hid-core.c +44 -1
index 41a79e43c82b..397de63297c6 100644
@@ -2900,6 +2900,45 @@ static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
 }
 static DEVICE_ATTR_RO(modalias);
 
+/*
+ * Expose this as bustype instead of bus as
+ * that's the name the input subsystem uses
+ */
+static ssize_t bustype_show(struct device *dev, struct device_attribute *a,
+			     char *buf)
+{
+	struct hid_device *hdev = to_hid_device(dev);
+
+	return sysfs_emit(buf, "%04x\n", hdev->bus);
+}
+static DEVICE_ATTR_RO(bustype);
+
+#define HID_DEV_ID_ATTR(name)				\
+static ssize_t name##_show(struct device *dev,		\
+			struct device_attribute *attr,	\
+			char *buf)			\
+{							\
+	struct hid_device *hdev = to_hid_device(dev);	\
+							\
+	return sysfs_emit(buf, "%04x\n", hdev->name);	\
+}							\
+static DEVICE_ATTR_RO(name)
+
+HID_DEV_ID_ATTR(vendor);
+HID_DEV_ID_ATTR(product);
+HID_DEV_ID_ATTR(version);
+
+static struct attribute *hid_dev_id_attrs[] = {
+	&dev_attr_bustype.attr,
+	&dev_attr_vendor.attr,
+	&dev_attr_product.attr,
+	&dev_attr_version.attr,
+	NULL
+};
+static const struct attribute_group hid_dev_id_attr_group = {
+	.name	= "id",
+	.attrs	= hid_dev_id_attrs,
+};
 static struct attribute *hid_dev_attrs[] = {
 	&dev_attr_modalias.attr,
 	NULL,
@@ -2912,7 +2951,11 @@ static const struct attribute_group hid_dev_group = {
 	.attrs = hid_dev_attrs,
 	.bin_attrs = hid_dev_bin_attrs,
 };
-__ATTRIBUTE_GROUPS(hid_dev);
+static const struct attribute_group *hid_dev_groups[] = {
+	&hid_dev_group,
+	&hid_dev_id_attr_group,
+	NULL
+};
 
 static int hid_uevent(const struct device *dev, struct kobj_uevent_env *env)
 {
Signature · 1 line
2.54.0
maintainer Applied
#1 lore raw
Quoted 7 lines · Vicki Pfau

udev rules for handling input devices generally match on idVendor and idProduct for USB hidraw or id/vendor and id/product for evdev nodes. However, hidraw nodes that aren't created by the USB subsystem will only expose this information to udev via the kernel path itself. This leads to doing substring matching, which can be error-prone or overzealous. Instead, since the HID subsystem already has this information, we can expose it directly in the same format that evdev exposes it.

Applied, thank you.

Signature · 2 lines
Jiri Kosina
SUSE Labs