firmware: arm_scmi: Refactor device matching logic to eliminate duplication
authorSudeep Holla <sudeep.holla@arm.com>
Mon, 17 Mar 2025 10:31:22 +0000 (10:31 +0000)
committerSudeep Holla <sudeep.holla@arm.com>
Mon, 14 Apr 2025 09:12:01 +0000 (10:12 +0100)
Currently, the device matching logic is duplicated in two functions:
  - scmi_dev_match() used by the scmi_bus to match scmi_devices with
    scmi_drivers.
  - scmi_child_dev_find() used to check for the presence of a device
    with the same name and protocol_id to avoid adding duplicates to
    the bus.

Refactor the code to eliminate the redundant matching logic and
consolidates the functionality for better maintainability and efficiency.

Message-Id: <20250317-b4-scmi_minor_cleanup-v2-2-f4be99bd9864@arm.com>
Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>
drivers/firmware/arm_scmi/bus.c

index fada4cf0cef719dc6f9babd49c406b69ead70137..6055e44cc3a9ab95750254c644c4aeb756d43a03 100644 (file)
@@ -201,52 +201,49 @@ scmi_protocol_table_unregister(const struct scmi_device_id *id_table)
                scmi_protocol_device_unrequest(entry);
 }
 
-static const struct scmi_device_id *
-scmi_dev_match_id(struct scmi_device *scmi_dev, const struct scmi_driver *scmi_drv)
+static int scmi_dev_match_by_id_table(struct scmi_device *scmi_dev,
+                                     const struct scmi_device_id *id_table)
 {
-       const struct scmi_device_id *id = scmi_drv->id_table;
+       if (!id_table || !id_table->name)
+               return 0;
 
-       if (!id)
-               return NULL;
-
-       for (; id->protocol_id && id->name; id++)
-               if (id->protocol_id == scmi_dev->protocol_id &&
-                   !strcmp(id->name, scmi_dev->name))
-                       return id;
+       for (; id_table->protocol_id && id_table->name; id_table++)
+               if (id_table->protocol_id == scmi_dev->protocol_id &&
+                   !strcmp(id_table->name, scmi_dev->name))
+                       return 1;
+       return 0;
+}
 
-       return NULL;
+static int scmi_dev_match_id(struct scmi_device *scmi_dev,
+                            const struct scmi_driver *scmi_drv)
+{
+       return scmi_dev_match_by_id_table(scmi_dev, scmi_drv->id_table);
 }
 
 static int scmi_dev_match(struct device *dev, const struct device_driver *drv)
 {
        const struct scmi_driver *scmi_drv = to_scmi_driver(drv);
        struct scmi_device *scmi_dev = to_scmi_dev(dev);
-       const struct scmi_device_id *id;
-
-       id = scmi_dev_match_id(scmi_dev, scmi_drv);
-       if (id)
-               return 1;
 
-       return 0;
+       return scmi_dev_match_id(scmi_dev, scmi_drv);
 }
 
 static int scmi_match_by_id_table(struct device *dev, const void *data)
 {
-       struct scmi_device *sdev = to_scmi_dev(dev);
+       struct scmi_device *scmi_dev = to_scmi_dev(dev);
        const struct scmi_device_id *id_table = data;
 
-       return sdev->protocol_id == id_table->protocol_id &&
-               (id_table->name && !strcmp(sdev->name, id_table->name));
+       return scmi_dev_match_by_id_table(scmi_dev, id_table);
 }
 
 static struct scmi_device *scmi_child_dev_find(struct device *parent,
                                               int prot_id, const char *name)
 {
-       struct scmi_device_id id_table;
+       struct scmi_device_id id_table[2] = { 0 };
        struct device *dev;
 
-       id_table.protocol_id = prot_id;
-       id_table.name = name;
+       id_table[0].protocol_id = prot_id;
+       id_table[0].name = name;
 
        dev = device_find_child(parent, &id_table, scmi_match_by_id_table);
        if (!dev)