HID: cougar: make compare_device_paths reusable
authorDaniel M. Lambea <dmlambea@gmail.com>
Tue, 17 Jul 2018 21:35:36 +0000 (22:35 +0100)
committerJiri Kosina <jkosina@suse.cz>
Mon, 23 Jul 2018 09:35:05 +0000 (11:35 +0200)
The function compare_device_paths from wacom_sys.c is generic
and useful for other drivers. Move the function to hid-core and
rename it as hid_compare_device_paths.

Signed-off-by: Daniel M. Lambea <dmlambea@gmail.com>
Reviewed-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
drivers/hid/hid-core.c
drivers/hid/wacom_sys.c
include/linux/hid.h

index 3942ee6..402ad97 100644 (file)
@@ -1939,6 +1939,29 @@ static int hid_bus_match(struct device *dev, struct device_driver *drv)
        return hid_match_device(hdev, hdrv) != NULL;
 }
 
+/**
+ * hid_compare_device_paths - check if both devices share the same path
+ * @hdev_a: hid device
+ * @hdev_b: hid device
+ * @separator: char to use as separator
+ *
+ * Check if two devices share the same path up to the last occurrence of
+ * the separator char. Both paths must exist (i.e., zero-length paths
+ * don't match).
+ */
+bool hid_compare_device_paths(struct hid_device *hdev_a,
+                             struct hid_device *hdev_b, char separator)
+{
+       int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
+       int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
+
+       if (n1 != n2 || n1 <= 0 || n2 <= 0)
+               return false;
+
+       return !strncmp(hdev_a->phys, hdev_b->phys, n1);
+}
+EXPORT_SYMBOL_GPL(hid_compare_device_paths);
+
 static int hid_device_probe(struct device *dev)
 {
        struct hid_driver *hdrv = to_hid_driver(dev->driver);
index d679753..ffe59a1 100644 (file)
@@ -703,18 +703,6 @@ struct wacom_hdev_data {
 static LIST_HEAD(wacom_udev_list);
 static DEFINE_MUTEX(wacom_udev_list_lock);
 
-static bool compare_device_paths(struct hid_device *hdev_a,
-               struct hid_device *hdev_b, char separator)
-{
-       int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
-       int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
-
-       if (n1 != n2 || n1 <= 0 || n2 <= 0)
-               return false;
-
-       return !strncmp(hdev_a->phys, hdev_b->phys, n1);
-}
-
 static bool wacom_are_sibling(struct hid_device *hdev,
                struct hid_device *sibling)
 {
@@ -737,10 +725,10 @@ static bool wacom_are_sibling(struct hid_device *hdev,
         * the same physical parent device path.
         */
        if (hdev->vendor == sibling->vendor && hdev->product == sibling->product) {
-               if (!compare_device_paths(hdev, sibling, '/'))
+               if (!hid_compare_device_paths(hdev, sibling, '/'))
                        return false;
        } else {
-               if (!compare_device_paths(hdev, sibling, '.'))
+               if (!hid_compare_device_paths(hdev, sibling, '.'))
                        return false;
        }
 
@@ -787,7 +775,7 @@ static struct wacom_hdev_data *wacom_get_hdev_data(struct hid_device *hdev)
 
        /* Try to find an already-probed interface from the same device */
        list_for_each_entry(data, &wacom_udev_list, list) {
-               if (compare_device_paths(hdev, data->dev, '/')) {
+               if (hid_compare_device_paths(hdev, data->dev, '/')) {
                        kref_get(&data->kref);
                        return data;
                }
index 773bcb1..938d9ba 100644 (file)
@@ -894,6 +894,8 @@ const struct hid_device_id *hid_match_id(const struct hid_device *hdev,
                                         const struct hid_device_id *id);
 const struct hid_device_id *hid_match_device(struct hid_device *hdev,
                                             struct hid_driver *hdrv);
+bool hid_compare_device_paths(struct hid_device *hdev_a,
+                             struct hid_device *hdev_b, char separator);
 s32 hid_snto32(__u32 value, unsigned n);
 __u32 hid_field_extract(const struct hid_device *hid, __u8 *report,
                     unsigned offset, unsigned n);