Message ID | 20231026083335.12551-1-raag.jadav@intel.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp515764vqb; Thu, 26 Oct 2023 01:34:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGO8z0qRvvYrE+2rgSX4XJ+DV21pI3B2SG//nJp+r5XYKuA7Eg9sQqQLofMjiLk6pYTyvBg X-Received: by 2002:a25:258e:0:b0:d9b:b77e:23a0 with SMTP id l136-20020a25258e000000b00d9bb77e23a0mr3361516ybl.11.1698309241459; Thu, 26 Oct 2023 01:34:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698309241; cv=none; d=google.com; s=arc-20160816; b=SJJ1LOIxjBtQrKdtVV/WX9itwAUQty8DIKTdOU96J4tpLkQV0+ZO0emld5nPzagBjH Wfgmi4N039GYacdwoUin5wB8dNmqIhJJtK7+km4RcmQIWlzQ+3yWjpoE1BPbDH/ay0QJ 4B6UUPw/ZsowzfuKydL8OVp3EYe8SIy945dANJIrry+pEjIwULCqBAJiGp7XOKlQLlA8 I/9qi13A8hkBXZ5++FX7KrYfA/7t6/TFZirtZsC1y1Jn9airyowRAunRVxkds1viQ6C+ mDNnHbrUVL3qgkhacwaIACvAaVKAYwMYYt0uYpTLFBK1b0W6W4pPffBlLm6lIFqinYT0 7AXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from :dkim-signature; bh=Ta60OdDxNBcgMz2I51p+8N9rWvGwnmX65IwKcs6F01E=; fh=2xctz/0e/SucHBlfO1OvWtxLt2i80Mg3jU6mKooxmDA=; b=ICfnHi1X0G6SCPyqCCC9hVngkUb5gIcH6zitHxUPpMxKnxdYzaVV2tlabkrRFxMRDP VXpW8fME8PoN8+UKCTULm3dBdkwrbSKDmWlBB3CjKx+lM7pmAn5z8C5UPGROywldaO2S PmwwEWhFK6mbdBBG4kkBVMXr8q1H6ycHmYlSt3HvcAAk0cuVl7pJMFCjtRUJiQHpBkGz ZvtXVpPuEiYG1nT3bnz6IoJQHFbHZ1mQa3Dnv2HgTgZ6xB17EbgZLv5hgoGh9HLSLvwg Ol4dsPcbrj2fO0wtoMiHck4P/GWukgGT6TBzalCUKPuK5DkfMiH9we7eqYgrZTWr/CRe IS2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QbTQGQUz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id v191-20020a25c5c8000000b00da053ac372csi5899899ybe.369.2023.10.26.01.34.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Oct 2023 01:34:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QbTQGQUz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 54E98820DA3C; Thu, 26 Oct 2023 01:34:00 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229705AbjJZId5 (ORCPT <rfc822;aposhian.dev@gmail.com> + 26 others); Thu, 26 Oct 2023 04:33:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52374 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344604AbjJZIdz (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 26 Oct 2023 04:33:55 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BC55128; Thu, 26 Oct 2023 01:33:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1698309233; x=1729845233; h=from:to:cc:subject:date:message-id; bh=8ikynLCsVFIJGquU/FrUnN4LC+5ZCI0bxGFqtHkRQ2Y=; b=QbTQGQUznNRp1vTBVjttVygMayiStSqWBuOvpYdXR+UH7y9moYZ9M+UG qiSTSx8hcwQD1ez6wHK0vyKgsZqBJgbVjLvShUhbIpALzZnWpZQcduGbS 0qtNeLPcTqMO8oRHGO4XDn2eYWZb4LyiMl9kkUBqw4Jfg8RKSpeikRtJ8 3X+i6YmiP16Xy+d+JofLP8L6eBzGUTXm+hwGX8GJNCn+jx8x8P0NUO1Jm d998CY4j93NpWfcbNcABCA+lZsVnLO9pF9/cELKlkCAHfElWk4j2X5qZD K0aY1eYOM9o2GEAByvxnPcO/m1vHU0AROkxyS24pbJDgHyGK1YdRZQOmY w==; X-IronPort-AV: E=McAfee;i="6600,9927,10874"; a="9046786" X-IronPort-AV: E=Sophos;i="6.03,253,1694761200"; d="scan'208";a="9046786" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Oct 2023 01:33:53 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10874"; a="1090526373" X-IronPort-AV: E=Sophos;i="6.03,253,1694761200"; d="scan'208";a="1090526373" Received: from inlubt0316.iind.intel.com ([10.191.20.213]) by fmsmga005.fm.intel.com with ESMTP; 26 Oct 2023 01:33:50 -0700 From: Raag Jadav <raag.jadav@intel.com> To: rafael@kernel.org, len.brown@intel.com, mika.westerberg@linux.intel.com, andriy.shevchenko@linux.intel.com Cc: linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, mallikarjunappa.sangannavar@intel.com, bala.senthil@intel.com, Raag Jadav <raag.jadav@intel.com> Subject: [PATCH v2] ACPI: LPSS: use acpi_dev_uid_match() for matching _UID Date: Thu, 26 Oct 2023 14:03:35 +0530 Message-Id: <20231026083335.12551-1-raag.jadav@intel.com> X-Mailer: git-send-email 2.17.1 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 26 Oct 2023 01:34:00 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780704720722812531 X-GMAIL-MSGID: 1780806311393774418 |
Series |
[v2] ACPI: LPSS: use acpi_dev_uid_match() for matching _UID
|
|
Commit Message
Raag Jadav
Oct. 26, 2023, 8:33 a.m. UTC
Now that we have a standard ACPI helper, we can use acpi_dev_uid_match() for matching _UID as per the original logic before commit 2a036e489eb1 ("ACPI: LPSS: Refactor _UID handling to use acpi_dev_uid_to_integer()"), instead of treating it as an integer. Signed-off-by: Raag Jadav <raag.jadav@intel.com> Acked-by: Mika Westerberg <mika.westerberg@linux.intel.com> --- Changes since v1: - Update commit message drivers/acpi/acpi_lpss.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) base-commit: f9c7f9d537da013471e5c7913a33b6489bdeb3cf
Comments
On Thu, Oct 26, 2023 at 02:03:35PM +0530, Raag Jadav wrote: > Now that we have a standard ACPI helper, we can use acpi_dev_uid_match() > for matching _UID as per the original logic before commit 2a036e489eb1 > ("ACPI: LPSS: Refactor _UID handling to use acpi_dev_uid_to_integer()"), > instead of treating it as an integer. > > Signed-off-by: Raag Jadav <raag.jadav@intel.com> > Acked-by: Mika Westerberg <mika.westerberg@linux.intel.com> The change still looks good to me, however I wonder if we could maybe improve acpi_dev_uid_match() to support both data types possible for _UID? This of course is separate patch (unless there are objections). There is the _Generic() thing and I think that can be used to make acpi_dev_uid_match() which takes either u64 (or maybe even unsigned int) or const char * and based on that picks the correct implementation. Not sure if that's possible, did not check but it would allow us to use one function everywhere instead of acpi_dev_uid_to_integer() and acpi_dev_uid_match().
On Fri, Oct 27, 2023 at 11:18:55AM +0300, Mika Westerberg wrote: > On Thu, Oct 26, 2023 at 02:03:35PM +0530, Raag Jadav wrote: > > Now that we have a standard ACPI helper, we can use acpi_dev_uid_match() > > for matching _UID as per the original logic before commit 2a036e489eb1 > > ("ACPI: LPSS: Refactor _UID handling to use acpi_dev_uid_to_integer()"), > > instead of treating it as an integer. > > > > Signed-off-by: Raag Jadav <raag.jadav@intel.com> > > Acked-by: Mika Westerberg <mika.westerberg@linux.intel.com> > > The change still looks good to me, however I wonder if we could maybe > improve acpi_dev_uid_match() to support both data types possible for > _UID? This of course is separate patch (unless there are objections). > > There is the _Generic() thing and I think that can be used to make > > acpi_dev_uid_match() > > which takes either u64 (or maybe even unsigned int) or const char * and > based on that picks the correct implementation. Not sure if that's > possible, did not check but it would allow us to use one function > everywhere instead of acpi_dev_uid_to_integer() and > acpi_dev_uid_match(). The way I see it, acpi_dev_uid_to_integer() is useful when drivers want to parse _UID and store it in their private data, so that it is available for making various decisions throughout the lifetime of the driver, as opposed to acpi_dev_uid_match() which is more useful for oneshot comparisons in my opinion. So I'm a bit conflicted about merging them into a single helper, unless ofcourse there is a way to serve both purposes. However, I do agree that we can improve acpi_dev_uid_match() by treating uids as integers underneath, instead of doing a raw string comparison. This would make it more aligned with the spec as suggested by Andy. Raag
On Fri, Oct 27, 2023 at 01:12:02PM +0300, Raag Jadav wrote: > On Fri, Oct 27, 2023 at 11:18:55AM +0300, Mika Westerberg wrote: > > On Thu, Oct 26, 2023 at 02:03:35PM +0530, Raag Jadav wrote: > > > Now that we have a standard ACPI helper, we can use acpi_dev_uid_match() > > > for matching _UID as per the original logic before commit 2a036e489eb1 > > > ("ACPI: LPSS: Refactor _UID handling to use acpi_dev_uid_to_integer()"), > > > instead of treating it as an integer. > > > > > > Signed-off-by: Raag Jadav <raag.jadav@intel.com> > > > Acked-by: Mika Westerberg <mika.westerberg@linux.intel.com> > > > > The change still looks good to me, however I wonder if we could maybe > > improve acpi_dev_uid_match() to support both data types possible for > > _UID? This of course is separate patch (unless there are objections). > > > > There is the _Generic() thing and I think that can be used to make > > > > acpi_dev_uid_match() > > > > which takes either u64 (or maybe even unsigned int) or const char * and > > based on that picks the correct implementation. Not sure if that's > > possible, did not check but it would allow us to use one function > > everywhere instead of acpi_dev_uid_to_integer() and > > acpi_dev_uid_match(). > > The way I see it, acpi_dev_uid_to_integer() is useful when drivers want to > parse _UID and store it in their private data, so that it is available for > making various decisions throughout the lifetime of the driver, as opposed > to acpi_dev_uid_match() which is more useful for oneshot comparisons in my > opinion. > > So I'm a bit conflicted about merging them into a single helper, unless > ofcourse there is a way to serve both purposes. Or perhaps something like, bool acpi_dev_uid_match(struct acpi_device *adev, const void *uid2, enum uid_type type) { u64 uid1_d, uid2_d; if (type == UID_TYPE_STR) { char *uid2_s = (char *)uid2; if (!(uid2_s && !kstrtou64(uid2_s, 0, &uid2_d))) return false; } else if (type == UID_TYPE_INT) { u64 *uid2_p; uid2_p = (u64 *)uid2; uid2_d = *uid2_p; } else { return false; } if (!acpi_dev_uid_to_integer(adev, &uid1_d) && uid1_d == uid2_d) return true; else return false; } Although this looks unnecessarily hideous. Raag
On Fri, Oct 27, 2023 at 05:17:12PM +0300, Raag Jadav wrote: > On Fri, Oct 27, 2023 at 01:12:02PM +0300, Raag Jadav wrote: > > On Fri, Oct 27, 2023 at 11:18:55AM +0300, Mika Westerberg wrote: > > > On Thu, Oct 26, 2023 at 02:03:35PM +0530, Raag Jadav wrote: > > > > Now that we have a standard ACPI helper, we can use acpi_dev_uid_match() > > > > for matching _UID as per the original logic before commit 2a036e489eb1 > > > > ("ACPI: LPSS: Refactor _UID handling to use acpi_dev_uid_to_integer()"), > > > > instead of treating it as an integer. > > > > > > > > Signed-off-by: Raag Jadav <raag.jadav@intel.com> > > > > Acked-by: Mika Westerberg <mika.westerberg@linux.intel.com> > > > > > > The change still looks good to me, however I wonder if we could maybe > > > improve acpi_dev_uid_match() to support both data types possible for > > > _UID? This of course is separate patch (unless there are objections). > > > > > > There is the _Generic() thing and I think that can be used to make > > > > > > acpi_dev_uid_match() > > > > > > which takes either u64 (or maybe even unsigned int) or const char * and > > > based on that picks the correct implementation. Not sure if that's > > > possible, did not check but it would allow us to use one function > > > everywhere instead of acpi_dev_uid_to_integer() and > > > acpi_dev_uid_match(). > > > > The way I see it, acpi_dev_uid_to_integer() is useful when drivers want to > > parse _UID and store it in their private data, so that it is available for > > making various decisions throughout the lifetime of the driver, as opposed > > to acpi_dev_uid_match() which is more useful for oneshot comparisons in my > > opinion. > > > > So I'm a bit conflicted about merging them into a single helper, unless > > ofcourse there is a way to serve both purposes. > > Or perhaps something like, > > bool acpi_dev_uid_match(struct acpi_device *adev, const void *uid2, enum uid_type type) > { > u64 uid1_d, uid2_d; > > if (type == UID_TYPE_STR) { > char *uid2_s = (char *)uid2; > if (!(uid2_s && !kstrtou64(uid2_s, 0, &uid2_d))) > return false; > } else if (type == UID_TYPE_INT) { > u64 *uid2_p; > uid2_p = (u64 *)uid2; > uid2_d = *uid2_p; > } else { > return false; > } > > if (!acpi_dev_uid_to_integer(adev, &uid1_d) && uid1_d == uid2_d) > return true; > else > return false; > } > > Although this looks unnecessarily hideous. Indeed, but using the _Generic() you should be able to have a single acpi_dev_uid_match() to work for either type so: acpi_dev_uid_match(adev, "1") and acpi_dev_uid_match(adev, 1) would both work with type checkings etc. Not sure if this is worth the trouble though.
On Fri, Oct 27, 2023 at 05:28:56PM +0300, Mika Westerberg wrote: > On Fri, Oct 27, 2023 at 05:17:12PM +0300, Raag Jadav wrote: > > Or perhaps something like, > > > > bool acpi_dev_uid_match(struct acpi_device *adev, const void *uid2, enum uid_type type) > > { > > u64 uid1_d, uid2_d; > > > > if (type == UID_TYPE_STR) { > > char *uid2_s = (char *)uid2; > > if (!(uid2_s && !kstrtou64(uid2_s, 0, &uid2_d))) > > return false; > > } else if (type == UID_TYPE_INT) { > > u64 *uid2_p; > > uid2_p = (u64 *)uid2; > > uid2_d = *uid2_p; > > } else { > > return false; > > } > > > > if (!acpi_dev_uid_to_integer(adev, &uid1_d) && uid1_d == uid2_d) > > return true; > > else > > return false; > > } > > > > Although this looks unnecessarily hideous. > > Indeed, but using the _Generic() you should be able to have > a single acpi_dev_uid_match() to work for either type so: > > acpi_dev_uid_match(adev, "1") > > and > > acpi_dev_uid_match(adev, 1) > > would both work with type checkings etc. > > Not sure if this is worth the trouble though. Well, in that case we can probably try both and hope for the best ;) bool acpi_dev_uid_match(struct acpi_device *adev, const char *uid2) { const char *uid1 = acpi_device_uid(adev); u64 uid1_d; return uid1 && uid2 && (!strcmp(uid1, uid2) || (!kstrtou64(uid1, 0, &uid1_d) && uid1_d == (u64)uid2)); } But I'm guessing the compiler wouldn't be very happy about this. Raag
On Fri, Oct 27, 2023 at 6:51 PM Raag Jadav <raag.jadav@intel.com> wrote: > > On Fri, Oct 27, 2023 at 05:28:56PM +0300, Mika Westerberg wrote: > > On Fri, Oct 27, 2023 at 05:17:12PM +0300, Raag Jadav wrote: > > > Or perhaps something like, > > > > > > bool acpi_dev_uid_match(struct acpi_device *adev, const void *uid2, enum uid_type type) > > > { > > > u64 uid1_d, uid2_d; > > > > > > if (type == UID_TYPE_STR) { > > > char *uid2_s = (char *)uid2; > > > if (!(uid2_s && !kstrtou64(uid2_s, 0, &uid2_d))) > > > return false; > > > } else if (type == UID_TYPE_INT) { > > > u64 *uid2_p; > > > uid2_p = (u64 *)uid2; > > > uid2_d = *uid2_p; > > > } else { > > > return false; > > > } > > > > > > if (!acpi_dev_uid_to_integer(adev, &uid1_d) && uid1_d == uid2_d) > > > return true; > > > else > > > return false; > > > } > > > > > > Although this looks unnecessarily hideous. > > > > Indeed, but using the _Generic() you should be able to have > > a single acpi_dev_uid_match() to work for either type so: > > > > acpi_dev_uid_match(adev, "1") > > > > and > > > > acpi_dev_uid_match(adev, 1) > > > > would both work with type checkings etc. > > > > Not sure if this is worth the trouble though. > > Well, in that case we can probably try both and hope for the best ;) > > bool acpi_dev_uid_match(struct acpi_device *adev, const char *uid2) > { > const char *uid1 = acpi_device_uid(adev); > u64 uid1_d; > > return uid1 && uid2 && (!strcmp(uid1, uid2) || > (!kstrtou64(uid1, 0, &uid1_d) && uid1_d == (u64)uid2)); > } > > But I'm guessing the compiler wouldn't be very happy about this. IMO it would be better to use the observation that if the uid2 string can be successfully cast to an int and the device's unique_id string can't be cast to an int (or the other way around), there is no match. If they both can be cast to an int, they match as long as the casting results are equal. If none of them can be cast to an int, they need to be compared as strings.
On Fri, Oct 27, 2023 at 7:19 PM Rafael J. Wysocki <rafael@kernel.org> wrote: > > On Fri, Oct 27, 2023 at 6:51 PM Raag Jadav <raag.jadav@intel.com> wrote: > > > > On Fri, Oct 27, 2023 at 05:28:56PM +0300, Mika Westerberg wrote: > > > On Fri, Oct 27, 2023 at 05:17:12PM +0300, Raag Jadav wrote: > > > > Or perhaps something like, > > > > > > > > bool acpi_dev_uid_match(struct acpi_device *adev, const void *uid2, enum uid_type type) > > > > { > > > > u64 uid1_d, uid2_d; > > > > > > > > if (type == UID_TYPE_STR) { > > > > char *uid2_s = (char *)uid2; > > > > if (!(uid2_s && !kstrtou64(uid2_s, 0, &uid2_d))) > > > > return false; > > > > } else if (type == UID_TYPE_INT) { > > > > u64 *uid2_p; > > > > uid2_p = (u64 *)uid2; > > > > uid2_d = *uid2_p; > > > > } else { > > > > return false; > > > > } > > > > > > > > if (!acpi_dev_uid_to_integer(adev, &uid1_d) && uid1_d == uid2_d) > > > > return true; > > > > else > > > > return false; > > > > } > > > > > > > > Although this looks unnecessarily hideous. > > > > > > Indeed, but using the _Generic() you should be able to have > > > a single acpi_dev_uid_match() to work for either type so: > > > > > > acpi_dev_uid_match(adev, "1") > > > > > > and > > > > > > acpi_dev_uid_match(adev, 1) > > > > > > would both work with type checkings etc. > > > > > > Not sure if this is worth the trouble though. > > > > Well, in that case we can probably try both and hope for the best ;) > > > > bool acpi_dev_uid_match(struct acpi_device *adev, const char *uid2) > > { > > const char *uid1 = acpi_device_uid(adev); > > u64 uid1_d; > > > > return uid1 && uid2 && (!strcmp(uid1, uid2) || > > (!kstrtou64(uid1, 0, &uid1_d) && uid1_d == (u64)uid2)); > > } > > > > But I'm guessing the compiler wouldn't be very happy about this. > > IMO it would be better to use the observation that if the uid2 string > can be successfully cast to an int and the device's unique_id string > can't be cast to an int (or the other way around), there is no match. > > If they both can be cast to an int, they match as long as the casting > results are equal. > > If none of them can be cast to an int, they need to be compared as strings. Or if the strings don't match literally, try to convert them both to ints and compare.
On Fri, Oct 27, 2023 at 07:40:38PM +0200, Rafael J. Wysocki wrote: > On Fri, Oct 27, 2023 at 7:19 PM Rafael J. Wysocki <rafael@kernel.org> wrote: > > > > On Fri, Oct 27, 2023 at 6:51 PM Raag Jadav <raag.jadav@intel.com> wrote: > > > > > > On Fri, Oct 27, 2023 at 05:28:56PM +0300, Mika Westerberg wrote: > > > > > > > > Indeed, but using the _Generic() you should be able to have > > > > a single acpi_dev_uid_match() to work for either type so: > > > > > > > > acpi_dev_uid_match(adev, "1") > > > > > > > > and > > > > > > > > acpi_dev_uid_match(adev, 1) > > > > > > > > would both work with type checkings etc. > > > > > > > > Not sure if this is worth the trouble though. > > > > > > Well, in that case we can probably try both and hope for the best ;) > > > > > > bool acpi_dev_uid_match(struct acpi_device *adev, const char *uid2) > > > { > > > const char *uid1 = acpi_device_uid(adev); > > > u64 uid1_d; > > > > > > return uid1 && uid2 && (!strcmp(uid1, uid2) || > > > (!kstrtou64(uid1, 0, &uid1_d) && uid1_d == (u64)uid2)); > > > } > > > > > > But I'm guessing the compiler wouldn't be very happy about this. > > > > IMO it would be better to use the observation that if the uid2 string > > can be successfully cast to an int and the device's unique_id string > > can't be cast to an int (or the other way around), there is no match. > > > > If they both can be cast to an int, they match as long as the casting > > results are equal. > > > > If none of them can be cast to an int, they need to be compared as strings. > > Or if the strings don't match literally, try to convert them both to > ints and compare. We'd probably end up with an oops trying to strcmp into a random address without knowing its type, so I think Mika's would be a better approach. #define acpi_dev_uid_match(adev, uid2) \ ({ \ const char *uid1 = acpi_device_uid(adev); \ u64 __uid1; \ \ _Generic(uid2, \ int: uid1 && !kstrtou64(uid1, 0, &__uid1) && (typeof(uid2))__uid1 == uid2, \ const char *: uid1 && uid2 && !strcmp(uid1, (const char *)uid2), \ default: false); \ \ }) This one I atleast got to compile, but I'm not very well versed with _Generic, so this could definitely use some comments. Raag
On Sat, Oct 28, 2023 at 11:58:12AM +0300, Raag Jadav wrote: > On Fri, Oct 27, 2023 at 07:40:38PM +0200, Rafael J. Wysocki wrote: ... > We'd probably end up with an oops trying to strcmp into a random address > without knowing its type, so I think Mika's would be a better approach. > > #define acpi_dev_uid_match(adev, uid2) \ > ({ \ > const char *uid1 = acpi_device_uid(adev); \ > u64 __uid1; \ > \ > _Generic(uid2, \ > int: uid1 && !kstrtou64(uid1, 0, &__uid1) && (typeof(uid2))__uid1 == uid2, \ > const char *: uid1 && uid2 && !strcmp(uid1, (const char *)uid2), \ > default: false); \ > \ > }) > > This one I atleast got to compile, but I'm not very well versed with _Generic, > so this could definitely use some comments. If you go this way, make _Generic() use simple in the macro with a help of two additional functions (per type). Also you need to take care about uid2 type to be _any_ unsigned integer. Or if you want to complicate things, then you need to distinguish signed and unsigned cases. P.S. All to me it seems way too overengineered w/o any potential prospective user.
On Mon, Oct 30, 2023 at 12:12:27PM +0200, Andy Shevchenko wrote: > On Sat, Oct 28, 2023 at 11:58:12AM +0300, Raag Jadav wrote: > > On Fri, Oct 27, 2023 at 07:40:38PM +0200, Rafael J. Wysocki wrote: > > ... > > > We'd probably end up with an oops trying to strcmp into a random address > > without knowing its type, so I think Mika's would be a better approach. > > > > #define acpi_dev_uid_match(adev, uid2) \ > > ({ \ > > const char *uid1 = acpi_device_uid(adev); \ > > u64 __uid1; \ > > \ > > _Generic(uid2, \ > > int: uid1 && !kstrtou64(uid1, 0, &__uid1) && (typeof(uid2))__uid1 == uid2, \ > > const char *: uid1 && uid2 && !strcmp(uid1, (const char *)uid2), \ > > default: false); \ > > \ > > }) > > > > This one I atleast got to compile, but I'm not very well versed with _Generic, > > so this could definitely use some comments. > > If you go this way, make _Generic() use simple in the macro with a help of two > additional functions (per type). Also you need to take care about uid2 type to > be _any_ unsigned integer. Or if you want to complicate things, then you need > to distinguish signed and unsigned cases. My initial thought was to have separate functions per type, but then I realized it would become an unnecessary inconvenience to maintain one per type. Having it inline with _Generic would make it relatively easier, but I'll leave it to the maintainers to decide. > P.S. > All to me it seems way too overengineered w/o any potential prospective user. I found a couple of acpi_dev_uid_to_integer() usages which could be simplified with this implementation, but let's see how everyone feels about this. Thanks for the comments, Raag
diff --git a/drivers/acpi/acpi_lpss.c b/drivers/acpi/acpi_lpss.c index 875de44961bf..6aaa6b066510 100644 --- a/drivers/acpi/acpi_lpss.c +++ b/drivers/acpi/acpi_lpss.c @@ -167,10 +167,8 @@ static struct pwm_lookup byt_pwm_lookup[] = { static void byt_pwm_setup(struct lpss_private_data *pdata) { - u64 uid; - /* Only call pwm_add_table for the first PWM controller */ - if (acpi_dev_uid_to_integer(pdata->adev, &uid) || uid != 1) + if (!acpi_dev_uid_match(pdata->adev, "1")) return; pwm_add_table(byt_pwm_lookup, ARRAY_SIZE(byt_pwm_lookup)); @@ -218,10 +216,8 @@ static struct pwm_lookup bsw_pwm_lookup[] = { static void bsw_pwm_setup(struct lpss_private_data *pdata) { - u64 uid; - /* Only call pwm_add_table for the first PWM controller */ - if (acpi_dev_uid_to_integer(pdata->adev, &uid) || uid != 1) + if (!acpi_dev_uid_match(pdata->adev, "1")) return; pwm_add_table(bsw_pwm_lookup, ARRAY_SIZE(bsw_pwm_lookup));