]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
HID: hid-lenovo-go-s: Add MCU ID Attribute
authorDerek J. Clark <derekjohn.clark@gmail.com>
Tue, 10 Mar 2026 07:29:29 +0000 (07:29 +0000)
committerJiri Kosina <jkosina@suse.com>
Tue, 10 Mar 2026 16:53:17 +0000 (17:53 +0100)
Adds command to probe for the MCU ID of the Lenovo Legion Go S
Controller and assign it to a device attribute.

Reviewed-by: Mark Pearson <mpearson-lenovo@squebb.ca>
Signed-off-by: Derek J. Clark <derekjohn.clark@gmail.com>
Signed-off-by: Jiri Kosina <jkosina@suse.com>
drivers/hid/hid-lenovo-go-s.c

index c9f57dfa145aaea033e1a2bdf3808d24530fec12..8ee75f724b5b773f056fd450096004bf39c370f1 100644 (file)
@@ -18,6 +18,7 @@
 #include <linux/mutex.h>
 #include <linux/printk.h>
 #include <linux/string.h>
+#include <linux/sysfs.h>
 #include <linux/types.h>
 #include <linux/unaligned.h>
 #include <linux/usb.h>
@@ -34,8 +35,13 @@ static struct hid_gos_cfg {
        struct completion send_cmd_complete;
        struct hid_device *hdev;
        struct mutex cfg_mutex; /*ensure single synchronous output report*/
+       u8 mcu_id[12];
 } drvdata;
 
+struct gos_cfg_attr {
+       u8 index;
+};
+
 struct command_report {
        u8 cmd;
        u8 sub_cmd;
@@ -70,6 +76,14 @@ static int hid_gos_version_event(u8 *data)
        return 0;
 }
 
+static int hid_gos_mcu_id_event(struct command_report *cmd_rep)
+{
+       drvdata.mcu_id[0] = cmd_rep->sub_cmd;
+       memcpy(&drvdata.mcu_id[1], cmd_rep->data, 11);
+
+       return 0;
+}
+
 static int get_endpoint_address(struct hid_device *hdev)
 {
        struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
@@ -103,6 +117,9 @@ static int hid_gos_raw_event(struct hid_device *hdev, struct hid_report *report,
        case GET_VERSION:
                ret = hid_gos_version_event(data);
                break;
+       case GET_MCU_ID:
+               ret = hid_gos_mcu_id_event(cmd_rep);
+               break;
        default:
                ret = -EINVAL;
                break;
@@ -157,10 +174,41 @@ static int mcu_property_out(struct hid_device *hdev, u8 command, u8 index,
        return 0;
 }
 
+static ssize_t mcu_id_show(struct device *dev, struct device_attribute *attr,
+                          char *buf)
+{
+       return sysfs_emit(buf, "%*phN\n", 12, &drvdata.mcu_id);
+}
+
+/* MCU */
+static DEVICE_ATTR_RO(mcu_id);
+
+static struct attribute *legos_mcu_attrs[] = {
+       &dev_attr_mcu_id.attr,
+       NULL,
+};
+
+static const struct attribute_group mcu_attr_group = {
+       .attrs = legos_mcu_attrs,
+};
+
+static const struct attribute_group *top_level_attr_groups[] = {
+       &mcu_attr_group,
+       NULL,
+};
+
 static void cfg_setup(struct work_struct *work)
 {
        int ret;
 
+       /* MCU */
+       ret = mcu_property_out(drvdata.hdev, GET_MCU_ID, FEATURE_NONE, NULL, 0);
+       if (ret) {
+               dev_err(&drvdata.hdev->dev, "Failed to retrieve MCU ID: %i\n",
+                       ret);
+               return;
+       }
+
        ret = mcu_property_out(drvdata.hdev, GET_VERSION, FEATURE_NONE, NULL, 0);
        if (ret) {
                dev_err(&drvdata.hdev->dev, "Failed to retrieve MCU Version: %i\n", ret);
@@ -177,6 +225,13 @@ static int hid_gos_cfg_probe(struct hid_device *hdev,
        drvdata.hdev = hdev;
        mutex_init(&drvdata.cfg_mutex);
 
+       ret = sysfs_create_groups(&hdev->dev.kobj, top_level_attr_groups);
+       if (ret) {
+               dev_err_probe(&hdev->dev, ret,
+                             "Failed to create gamepad configuration attributes\n");
+               return ret;
+       }
+
        init_completion(&drvdata.send_cmd_complete);
 
        /* Executing calls prior to returning from probe will lock the MCU. Schedule
@@ -196,6 +251,7 @@ static void hid_gos_cfg_remove(struct hid_device *hdev)
 {
        guard(mutex)(&drvdata.cfg_mutex);
        cancel_delayed_work_sync(&drvdata.gos_cfg_setup);
+       sysfs_remove_groups(&hdev->dev.kobj, top_level_attr_groups);
        hid_hw_close(hdev);
        hid_hw_stop(hdev);
        hid_set_drvdata(hdev, NULL);