From patchwork Tue Jul 17 21:35:36 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Daniel M. Lambea" X-Patchwork-Id: 10530773 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 94AC760247 for ; Tue, 17 Jul 2018 21:35:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 87D03290D0 for ; Tue, 17 Jul 2018 21:35:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7BBB22957C; Tue, 17 Jul 2018 21:35:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0F11F290D0 for ; Tue, 17 Jul 2018 21:35:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729802AbeGQWK0 (ORCPT ); Tue, 17 Jul 2018 18:10:26 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:38243 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729675AbeGQWK0 (ORCPT ); Tue, 17 Jul 2018 18:10:26 -0400 Received: by mail-wr1-f66.google.com with SMTP id v14-v6so2600811wro.5 for ; Tue, 17 Jul 2018 14:35:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=ok1+5pQLJsidhTtKpE/GZkKLyfiQOddHC0bP80iL6GA=; b=dIaEbvtwk8SN1Axz1VaY8mzXED2TFBhVSnOVXSbboJT8kjmY2TK2usMlrf3XtAZuKg TabW2t168z6IxN5FO+lGZLw8eYOgju/Syli19l3AKmlVzJN9T9cbe1rEei32Mwqltk+3 mWSN5wul+vxCebwY6u0xwVQIEGGSWHApSfFddvVHTTjDF7l5Z4hx9cxEDgR2xv4/l9qB jCRRpWEiYDPT/5mhmqbI0LEr0d5w2Vmdd/2Rq4wX70lke/fekBcznZPo9CHlHgdPXZWx tgk93fjjvllkgYkd9CktwutPXAQv4EZEsfuDNZkKIQWKLR88wCF2QpejhBzptc3NVdT1 wdbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=ok1+5pQLJsidhTtKpE/GZkKLyfiQOddHC0bP80iL6GA=; b=DvUdwQ+j8+4Ny1LX5SGiCS/Hq2GEnhUyyIq55VjIAVHMcaqa3qCyrTP96lxW0MoSJE zXFZy/kXVU+YC+I/64j0TbtDE9h2RNzVVf862EBxZWJLldlrXmcGbCgS/oYzSJWKAwkD fux79UIfWhHAUl9p/QKfK6xyxe/mETOQ8GgxhTH1w71G/URGd/vrvJ67iGfL2MI/BheT 2LwGBv7luo5mNLbKpk7h4bOEGAx0FK0POboYT/Vn3gDY8fn0rS5w1RXC0a9ePTalhowd hVxL12epnduYk9y5iRfIhWdLSC9nY+q2AuOTG1G2IhhaHMCfDENyFYri9GCOVD/tEvHQ hUfQ== X-Gm-Message-State: AOUpUlFsYRdJp8MQ9WVfsVpbvcE/VCvUROdGNWoBV7gJByzOc6x0Ajk1 GJqGq7HHtNuQ+3SdkyEV19D7rg== X-Google-Smtp-Source: AAOMgpfHmOQOt96Kn+fFA97DoehyMKh1ReDoFGyNRlre3ChtbvjJQ0u2mGagDMK4PqDhafY9I+UrJw== X-Received: by 2002:adf:8806:: with SMTP id d6-v6mr2433288wrd.41.1531863350184; Tue, 17 Jul 2018 14:35:50 -0700 (PDT) Received: from localhost.localdomain (86.red-83-42-60.dynamicip.rima-tde.net. [83.42.60.86]) by smtp.gmail.com with ESMTPSA id y11-v6sm3303609wrr.84.2018.07.17.14.35.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 17 Jul 2018 14:35:49 -0700 (PDT) From: "Daniel M. Lambea" To: Jiri Kosina Cc: Benjamin Tissoires , linux-input@vger.kernel.org Subject: [PATCH v2 1/2] HID: cougar: make compare_device_paths reusable Date: Tue, 17 Jul 2018 22:35:36 +0100 Message-Id: <20180717213537.12851-1-dmlambea@gmail.com> X-Mailer: git-send-email 2.17.1 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- drivers/hid/hid-core.c | 23 +++++++++++++++++++++++ drivers/hid/wacom_sys.c | 18 +++--------------- include/linux/hid.h | 2 ++ 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c index 3942ee61bd1c..402ad974b31c 100644 --- a/drivers/hid/hid-core.c +++ b/drivers/hid/hid-core.c @@ -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); diff --git a/drivers/hid/wacom_sys.c b/drivers/hid/wacom_sys.c index d6797535fff9..ffe59a19b3a3 100644 --- a/drivers/hid/wacom_sys.c +++ b/drivers/hid/wacom_sys.c @@ -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; } diff --git a/include/linux/hid.h b/include/linux/hid.h index 773bcb1d4044..938d9ba6d7cd 100644 --- a/include/linux/hid.h +++ b/include/linux/hid.h @@ -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);