From patchwork Thu Jun 13 20:15:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 13697423 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA659130A4D; Thu, 13 Jun 2024 20:15:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718309738; cv=none; b=VbZT26GgHY/VVFJvSqrVX/Upvm3eUxeVUCd0zW329w4Lv7IlT5thmoDXflMuR6kquWD/7WocRZbqbwwjGY6aR7zSZ4+ojFi8PFfJQhTdI1c86pFUiMQP7YLTA1L09B/QIWa+LFkpp2kZjK/5Boe2bekdHsWf+cU3ygZ/xmVJ238= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718309738; c=relaxed/simple; bh=wuIQfkHjHbUZk36PrIyzZVKJCwONssrSuDQUKyE3G5M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DDQly55kfAlKE+yE+nkyw6cQh0wladrKepMzWpTVzmqfYsL6CatHODG368gQHqYBnVmEGxQ27fY5jc96J7nHFe8zG6eOmQ7PSuFLDyVgtge8Fqkas3qai/Z0X2sjQsAn6GeH36FZRYkRyc7DiRHlE9NelHg5+7bQn4ybr6C5zOk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=iJViCu4B; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="iJViCu4B" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1718309732; bh=wuIQfkHjHbUZk36PrIyzZVKJCwONssrSuDQUKyE3G5M=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=iJViCu4BWydYRUpcLgnPGEsG9KdqygZVvPyoLRbwSu2rQQNpSiJakyuBd1FBDBW12 5ATqbmNx0D6tlp5lU3p3oizPAfxjhh2PY71xT3gdqtX1dJHYXkGhNrd/FbctnzieKs LZU9UZWKQtdVT0M87U8QSvNj+S7OJcRRyb/6KsjY= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Thu, 13 Jun 2024 22:15:32 +0200 Subject: [PATCH 1/5] ACPI: sysfs: convert utf-16 from _STR to utf-8 only once Precedence: bulk X-Mailing-List: linux-acpi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240613-acpi-sysfs-groups-v1-1-665e0deb052a@weissschuh.net> References: <20240613-acpi-sysfs-groups-v1-0-665e0deb052a@weissschuh.net> In-Reply-To: <20240613-acpi-sysfs-groups-v1-0-665e0deb052a@weissschuh.net> To: "Rafael J. Wysocki" , Len Brown , Robert Moore Cc: linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, acpica-devel@lists.linux.dev, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1718309732; l=4089; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=wuIQfkHjHbUZk36PrIyzZVKJCwONssrSuDQUKyE3G5M=; b=R8UTwK2oub0RzCvElNIobM/59YinmrpFME0s4J3K483SIT4LkKzFZdZJbdLdN1un29b+4qh5I 4ll3lIsCBhNDbgW+Z3CEKKA+PR2rpKadc5+jB4E1j8zo/8RVPWo42IU X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= The ACPI _STR method returns an UTF-16 string that is converted to utf-8 before printing it in sysfs. Currently this conversion is performed every time the "description" sysfs attribute is read, which is not necessary. Only perform the conversion once and cache the result. Signed-off-by: Thomas Weißschuh --- drivers/acpi/device_sysfs.c | 63 ++++++++++++++++++++++++++++----------------- include/acpi/acpi_bus.h | 2 +- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/drivers/acpi/device_sysfs.c b/drivers/acpi/device_sysfs.c index 23373faa35ec..4bedbe8f57ed 100644 --- a/drivers/acpi/device_sysfs.c +++ b/drivers/acpi/device_sysfs.c @@ -439,24 +439,11 @@ static ssize_t description_show(struct device *dev, char *buf) { struct acpi_device *acpi_dev = to_acpi_device(dev); - int result; - if (acpi_dev->pnp.str_obj == NULL) + if (acpi_dev->pnp.str == NULL) return 0; - /* - * The _STR object contains a Unicode identifier for a device. - * We need to convert to utf-8 so it can be displayed. - */ - result = utf16s_to_utf8s( - (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer, - acpi_dev->pnp.str_obj->buffer.length, - UTF16_LITTLE_ENDIAN, buf, - PAGE_SIZE - 1); - - buf[result++] = '\n'; - - return result; + return sysfs_emit("%s\n", acpi_dev->pnp.str); } static DEVICE_ATTR_RO(description); @@ -507,14 +494,46 @@ static ssize_t status_show(struct device *dev, struct device_attribute *attr, } static DEVICE_ATTR_RO(status); +static const char *acpi_device_str(struct acpi_device *dev) +{ + struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; + union acpi_object *str_obj; + acpi_status status; + const char *ret; + char buf[512]; + int result; + + if (!acpi_has_method(dev->handle, "_STR")) + return NULL; + + status = acpi_evaluate_object(dev->handle, "_STR", + NULL, &buffer); + if (ACPI_FAILURE(status)) + return NULL; + + str_obj = buffer.pointer; + /* + * The _STR object contains a Unicode identifier for a device. + * We need to convert to utf-8 so it can be displayed. + */ + result = utf16s_to_utf8s((wchar_t *)str_obj->buffer.pointer, + str_obj->buffer.length, + UTF16_LITTLE_ENDIAN, + buf, sizeof(buf) - 1); + buf[result++] = '\0'; + + ret = kstrdup(buf, GFP_KERNEL); + kfree(buffer.pointer); + + return ret; +} + /** * acpi_device_setup_files - Create sysfs attributes of an ACPI device. * @dev: ACPI device object. */ int acpi_device_setup_files(struct acpi_device *dev) { - struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; - acpi_status status; int result = 0; /* @@ -539,12 +558,8 @@ int acpi_device_setup_files(struct acpi_device *dev) /* * If device has _STR, 'description' file is created */ - if (acpi_has_method(dev->handle, "_STR")) { - status = acpi_evaluate_object(dev->handle, "_STR", - NULL, &buffer); - if (ACPI_FAILURE(status)) - buffer.pointer = NULL; - dev->pnp.str_obj = buffer.pointer; + dev->pnp.str = acpi_device_str(dev); + if (dev->pnp.str) { result = device_create_file(&dev->dev, &dev_attr_description); if (result) goto end; @@ -618,7 +633,7 @@ void acpi_device_remove_files(struct acpi_device *dev) * If device has _STR, remove 'description' file */ if (acpi_has_method(dev->handle, "_STR")) { - kfree(dev->pnp.str_obj); + kfree(dev->pnp.str); device_remove_file(&dev->dev, &dev_attr_description); } /* diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h index 1a4dfd7a1c4a..32e3105c9ece 100644 --- a/include/acpi/acpi_bus.h +++ b/include/acpi/acpi_bus.h @@ -254,7 +254,7 @@ struct acpi_device_pnp { struct list_head ids; /* _HID and _CIDs */ acpi_device_name device_name; /* Driver-determined */ acpi_device_class device_class; /* " */ - union acpi_object *str_obj; /* unicode string for _STR method */ + const char *str; /* _STR */ }; #define acpi_device_bid(d) ((d)->pnp.bus_id) From patchwork Thu Jun 13 20:15:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 13697422 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A8FC612F37B; Thu, 13 Jun 2024 20:15:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718309737; cv=none; b=U6Qns5ywqYFbWxr2B3cpzqFaiNZmOqdHfmM3pIvFWKgRUcFO8RJOlvunYoiIukPIJBnwUf6ofeot0R44Th4YGZJc777kw4fjoe4ErmqVU5te3BIgKCg2wWKOR1kqHOcoebUU+Qg0Gxri3UCHbiOj/k0p5zPRYE655qno2Sfr8NY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718309737; c=relaxed/simple; bh=V+7rNBrVBMiquqsDaHIG89BLDRn+4swfipLg+5xOqn0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=vBhU4TwSc1w8wb6e35vcsTcvpiE6vuvjDWh6DgDZRZppqXFZ+qv4S+IWRj5P2FjUcxLfwPrDdsneINIJrmvijE9IVIR3KL3dzR4iBgUbEdsrvTT9IHmXg7HHGADVe2OkWN2JA+BBu3DiPl1VqiW1P1+FBFWJp5LmrTp3jyiGLos= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=qVIo4kfU; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="qVIo4kfU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1718309732; bh=V+7rNBrVBMiquqsDaHIG89BLDRn+4swfipLg+5xOqn0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=qVIo4kfU0Fsy9ywTFamSR9/eOzVb2gWHpVzthTCpEi1ZYrL6EhJ1Mb1degz7Iyp3U cxSOxuAajCGTGoZ8Uwdtr57WjNn8uSXDF7EfThlkBuPywMqFp1l7/y00gMtdQP9oo4 0WWe84rX6FM56FK0Sfazv11vbqgklylm40zOJM60= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Thu, 13 Jun 2024 22:15:33 +0200 Subject: [PATCH 2/5] ACPI: sysfs: use device lifecycle for _STR result Precedence: bulk X-Mailing-List: linux-acpi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240613-acpi-sysfs-groups-v1-2-665e0deb052a@weissschuh.net> References: <20240613-acpi-sysfs-groups-v1-0-665e0deb052a@weissschuh.net> In-Reply-To: <20240613-acpi-sysfs-groups-v1-0-665e0deb052a@weissschuh.net> To: "Rafael J. Wysocki" , Len Brown , Robert Moore Cc: linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, acpica-devel@lists.linux.dev, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1718309732; l=1932; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=V+7rNBrVBMiquqsDaHIG89BLDRn+4swfipLg+5xOqn0=; b=1FnIhB0nONyobBwXP9cfdPRxo46K7f9X4+4wRu9l3XogLAy9UzjQdGbX4CcS052uoU+rzXphK JU2Wf0F3gjSCfLsCKAWwGDc/a2qTC64v6VsRoIjKqDAiE5iyw24asNf X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= The string assigned to dev->pnp.str effectively shares the lifetime of the device. Use devm_-APIs to avoid a manual cleanup path. This will be useful when the attributes themselves will be managed by the device core. Signed-off-by: Thomas Weißschuh --- drivers/acpi/device_sysfs.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/drivers/acpi/device_sysfs.c b/drivers/acpi/device_sysfs.c index 4bedbe8f57ed..d0ca159d93e1 100644 --- a/drivers/acpi/device_sysfs.c +++ b/drivers/acpi/device_sysfs.c @@ -494,7 +494,7 @@ static ssize_t status_show(struct device *dev, struct device_attribute *attr, } static DEVICE_ATTR_RO(status); -static const char *acpi_device_str(struct acpi_device *dev) +static const char *devm_acpi_device_str(struct acpi_device *dev) { struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; union acpi_object *str_obj; @@ -522,7 +522,7 @@ static const char *acpi_device_str(struct acpi_device *dev) buf, sizeof(buf) - 1); buf[result++] = '\0'; - ret = kstrdup(buf, GFP_KERNEL); + ret = devm_kstrdup(&dev->dev, buf, GFP_KERNEL); kfree(buffer.pointer); return ret; @@ -558,7 +558,7 @@ int acpi_device_setup_files(struct acpi_device *dev) /* * If device has _STR, 'description' file is created */ - dev->pnp.str = acpi_device_str(dev); + dev->pnp.str = devm_acpi_device_str(dev); if (dev->pnp.str) { result = device_create_file(&dev->dev, &dev_attr_description); if (result) @@ -632,10 +632,8 @@ void acpi_device_remove_files(struct acpi_device *dev) /* * If device has _STR, remove 'description' file */ - if (acpi_has_method(dev->handle, "_STR")) { - kfree(dev->pnp.str); + if (acpi_has_method(dev->handle, "_STR")) device_remove_file(&dev->dev, &dev_attr_description); - } /* * If device has _EJ0, remove 'eject' file. */ From patchwork Thu Jun 13 20:15:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 13697421 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D8A415A4D5; Thu, 13 Jun 2024 20:15:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718309737; cv=none; b=mKVZctwxFOl14glgGBtn8F8uyksX1i6CSoQrnAYZzIhceIjvNimp/8Ynop+p3/ZLBE0jSbcBiRuPC5oxRsqTVw2Y6cyuVaQos9XYwTNfmZVlJiUWVnbxyA/03747eLuDUHCRzl19L1NRt4+XhidgrY/GjEuoApP6KUSGQV2pIzU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718309737; c=relaxed/simple; bh=OV3NvfZWfLxpDfggoJaTx32OTDbVKKTvikYlf0Uiq74=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=h1VL99IKIqwuyB4xH0PHuXBS8HEZEVE3cx4Z8Hb9iDCjYmdR2kSXrtNR3zoEm36o+d9wvXDJPPZvmxfaKu9bJxnl6e8OcSR3ppxSchjF3Zlv81nIx0QEGx2EOi3YLInoItT8cLAXOQokWU/gpKlnT6DzpcxgzWpCkwVELB9eaN0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=ANLHL5zV; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="ANLHL5zV" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1718309732; bh=OV3NvfZWfLxpDfggoJaTx32OTDbVKKTvikYlf0Uiq74=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ANLHL5zV23safuJoiH1GVUeQ+YKvME4p08n+h8lLYULGtQyJROol4ncn2fh7zENvA nFJ/wO5AW+vUFmIg8dEx6+X1CHVFZb4eUtuGzjj9oUynLy0H7///p/Zh5B07/C7tiw 7RkDEW0XZmw2veWdF/FD54731Qf5iUMgvkrNW5ok= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Thu, 13 Jun 2024 22:15:34 +0200 Subject: [PATCH 3/5] ACPI: sysfs: manage attributes as attribute_group Precedence: bulk X-Mailing-List: linux-acpi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240613-acpi-sysfs-groups-v1-3-665e0deb052a@weissschuh.net> References: <20240613-acpi-sysfs-groups-v1-0-665e0deb052a@weissschuh.net> In-Reply-To: <20240613-acpi-sysfs-groups-v1-0-665e0deb052a@weissschuh.net> To: "Rafael J. Wysocki" , Len Brown , Robert Moore Cc: linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, acpica-devel@lists.linux.dev, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1718309732; l=6704; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=OV3NvfZWfLxpDfggoJaTx32OTDbVKKTvikYlf0Uiq74=; b=GwwqBwQv+scW4j2SxrdzqcUz6nzETJkcUaVKV9Whhk73xIuG97M67oDFBpZC7OcYeOdiHfvtb pTzcGsJEZUSCxN147YfM4DAQV+YsOHNQzQWrTHHHk4qtFL6H5JJpCKU X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= The current manual attribute management is inconsistent and brittle. Not all return values of device_create_file() are checked and the cleanup logic needs to be kept up to date manually. Moving all attributes into an attribute_group and using the is_visible() callback allows the management of all attributes as a single unit. Signed-off-by: Thomas Weißschuh --- drivers/acpi/device_sysfs.c | 190 ++++++++++++++++++++------------------------ 1 file changed, 84 insertions(+), 106 deletions(-) diff --git a/drivers/acpi/device_sysfs.c b/drivers/acpi/device_sysfs.c index d0ca159d93e1..a673488066b3 100644 --- a/drivers/acpi/device_sysfs.c +++ b/drivers/acpi/device_sysfs.c @@ -494,6 +494,88 @@ static ssize_t status_show(struct device *dev, struct device_attribute *attr, } static DEVICE_ATTR_RO(status); +static struct attribute *acpi_attrs[] = { + &dev_attr_path.attr, + &dev_attr_hid.attr, + &dev_attr_modalias.attr, + &dev_attr_description.attr, + &dev_attr_adr.attr, + &dev_attr_uid.attr, + &dev_attr_sun.attr, + &dev_attr_hrv.attr, + &dev_attr_status.attr, + &dev_attr_eject.attr, + &dev_attr_power_state.attr, + &dev_attr_real_power_state.attr, + NULL +}; + +static bool acpi_show_attr(struct acpi_device *dev, const struct device_attribute *attr) +{ + /* + * Devices gotten from FADT don't have a "path" attribute + */ + if (attr == &dev_attr_path) + return dev->handle; + + if (attr == &dev_attr_hid || attr == &dev_attr_modalias) + return !list_empty(&dev->pnp.ids); + + /* + * If device has _STR, 'description' file is created + */ + if (attr == &dev_attr_description) + return dev->pnp.str; + + if (attr == &dev_attr_adr) + return dev->pnp.type.bus_address; + + if (attr == &dev_attr_uid) + return acpi_device_uid(dev); + + if (attr == &dev_attr_sun) + return acpi_has_method(dev->handle, "_SUN"); + + if (attr == &dev_attr_hrv) + return acpi_has_method(dev->handle, "_HRV"); + + if (attr == &dev_attr_status) + return acpi_has_method(dev->handle, "_STA"); + + /* + * If device has _EJ0, 'eject' file is created that is used to trigger + * hot-removal function from userland. + */ + if (attr == &dev_attr_eject) + return acpi_has_method(dev->handle, "_EJ0"); + + if (attr == &dev_attr_power_state) + return dev->flags.power_manageable; + + if (attr == &dev_attr_real_power_state) + return dev->flags.power_manageable && dev->power.flags.power_resources; + + dev_warn_once(&dev->dev, "Unexpected attribute: %s\n", attr->attr.name); + return false; +} + +static umode_t acpi_attr_is_visible(struct kobject *kobj, + struct attribute *attr, + int attrno) +{ + struct acpi_device *dev = to_acpi_device(kobj_to_dev(kobj)); + + if (acpi_show_attr(dev, container_of(attr, struct device_attribute, attr))) + return attr->mode; + else + return 0; +} + +static const struct attribute_group acpi_group = { + .attrs = acpi_attrs, + .is_visible = acpi_attr_is_visible, +}; + static const char *devm_acpi_device_str(struct acpi_device *dev) { struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; @@ -536,81 +618,11 @@ int acpi_device_setup_files(struct acpi_device *dev) { int result = 0; - /* - * Devices gotten from FADT don't have a "path" attribute - */ - if (dev->handle) { - result = device_create_file(&dev->dev, &dev_attr_path); - if (result) - goto end; - } - - if (!list_empty(&dev->pnp.ids)) { - result = device_create_file(&dev->dev, &dev_attr_hid); - if (result) - goto end; - - result = device_create_file(&dev->dev, &dev_attr_modalias); - if (result) - goto end; - } - - /* - * If device has _STR, 'description' file is created - */ dev->pnp.str = devm_acpi_device_str(dev); - if (dev->pnp.str) { - result = device_create_file(&dev->dev, &dev_attr_description); - if (result) - goto end; - } - - if (dev->pnp.type.bus_address) - result = device_create_file(&dev->dev, &dev_attr_adr); - if (acpi_device_uid(dev)) - result = device_create_file(&dev->dev, &dev_attr_uid); - - if (acpi_has_method(dev->handle, "_SUN")) { - result = device_create_file(&dev->dev, &dev_attr_sun); - if (result) - goto end; - } - - if (acpi_has_method(dev->handle, "_HRV")) { - result = device_create_file(&dev->dev, &dev_attr_hrv); - if (result) - goto end; - } - - if (acpi_has_method(dev->handle, "_STA")) { - result = device_create_file(&dev->dev, &dev_attr_status); - if (result) - goto end; - } - - /* - * If device has _EJ0, 'eject' file is created that is used to trigger - * hot-removal function from userland. - */ - if (acpi_has_method(dev->handle, "_EJ0")) { - result = device_create_file(&dev->dev, &dev_attr_eject); - if (result) - return result; - } - - if (dev->flags.power_manageable) { - result = device_create_file(&dev->dev, &dev_attr_power_state); - if (result) - return result; - - if (dev->power.flags.power_resources) - result = device_create_file(&dev->dev, - &dev_attr_real_power_state); - } + result = device_add_group(&dev->dev, &acpi_group); acpi_expose_nondev_subnodes(&dev->dev.kobj, &dev->data); -end: return result; } @@ -621,39 +633,5 @@ int acpi_device_setup_files(struct acpi_device *dev) void acpi_device_remove_files(struct acpi_device *dev) { acpi_hide_nondev_subnodes(&dev->data); - - if (dev->flags.power_manageable) { - device_remove_file(&dev->dev, &dev_attr_power_state); - if (dev->power.flags.power_resources) - device_remove_file(&dev->dev, - &dev_attr_real_power_state); - } - - /* - * If device has _STR, remove 'description' file - */ - if (acpi_has_method(dev->handle, "_STR")) - device_remove_file(&dev->dev, &dev_attr_description); - /* - * If device has _EJ0, remove 'eject' file. - */ - if (acpi_has_method(dev->handle, "_EJ0")) - device_remove_file(&dev->dev, &dev_attr_eject); - - if (acpi_has_method(dev->handle, "_SUN")) - device_remove_file(&dev->dev, &dev_attr_sun); - - if (acpi_has_method(dev->handle, "_HRV")) - device_remove_file(&dev->dev, &dev_attr_hrv); - - if (acpi_device_uid(dev)) - device_remove_file(&dev->dev, &dev_attr_uid); - if (dev->pnp.type.bus_address) - device_remove_file(&dev->dev, &dev_attr_adr); - device_remove_file(&dev->dev, &dev_attr_modalias); - device_remove_file(&dev->dev, &dev_attr_hid); - if (acpi_has_method(dev->handle, "_STA")) - device_remove_file(&dev->dev, &dev_attr_status); - if (dev->handle) - device_remove_file(&dev->dev, &dev_attr_path); + device_remove_group(&dev->dev, &acpi_group); } From patchwork Thu Jun 13 20:15:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 13697424 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 40929137931; Thu, 13 Jun 2024 20:15:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718309740; cv=none; b=H7XmAAPJQ81yAOIXvxm274nBDvdZ414tVfDKRg0q6blu/ea1g3MXhMHmNbleg60roEqB0I2/kaf6zq6qk0YUMCugkL3Hqd1cBnIryidkYuWWvyVK4Bz1dp6A/ZAImTjJjeQRj3JJIf9HB/ysO8xlISl/JvNTo/HWkIdba/3hHcs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718309740; c=relaxed/simple; bh=Wibaem2muyMRSUGsOkiWV4LRhlCo/E5mStSJIoYEpqc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=B3JDRc3S6o1BlXM+BJlRE5RiaLC1BIHUlyaGOxd/+GJ6OOs9LKkiMnWGSYrncbEiM8sFxL3+a7t81xNx+FiSWw8vRyO/kcpvKsimRQjWK/Zmhz/d0Bl2RoYvePtkLehLgNvMRf/9M1tZW4kGTbN+Z+I4ZH07lgAI3fsIVgxyohM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=Vfc8BmPA; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="Vfc8BmPA" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1718309732; bh=Wibaem2muyMRSUGsOkiWV4LRhlCo/E5mStSJIoYEpqc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Vfc8BmPARWzf7qfetgooexXKzNr0QqQ3BFDOmWH4ATs3OaPwyQW2wToBC7e7ymR5N oErhuLVLmLAdJw3Dj8yKnC8nWZbr8pYwcu8HDfj9gF2AGyBDRb+izoLSfnF+8RLLik qmzB2TaxKkja1NzyV+ZXzin6nzKnbsWtFTV6Bdd4= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Thu, 13 Jun 2024 22:15:35 +0200 Subject: [PATCH 4/5] ACPI: sysfs: manage sysfs attributes through device core Precedence: bulk X-Mailing-List: linux-acpi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240613-acpi-sysfs-groups-v1-4-665e0deb052a@weissschuh.net> References: <20240613-acpi-sysfs-groups-v1-0-665e0deb052a@weissschuh.net> In-Reply-To: <20240613-acpi-sysfs-groups-v1-0-665e0deb052a@weissschuh.net> To: "Rafael J. Wysocki" , Len Brown , Robert Moore Cc: linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, acpica-devel@lists.linux.dev, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1718309732; l=2575; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=Wibaem2muyMRSUGsOkiWV4LRhlCo/E5mStSJIoYEpqc=; b=qUJNj/gSuC+i8EkW98Yb9mxUeGPU+gnjv0yh5/B7Z/ZCUFurxsAdGIvU9Ba4qwuHSPxmeHKNs 31wgZiuKiw9DyNCn8gW/H2Vzg3mdTZpWy1RoVyN4Gi+106WkFAO3I7r X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Now that the acpi sysfs attributes are organized around an attribute_group the device core can manage them. Signed-off-by: Thomas Weißschuh --- drivers/acpi/device_sysfs.c | 8 +++++--- drivers/acpi/internal.h | 1 + drivers/acpi/scan.c | 1 + 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/drivers/acpi/device_sysfs.c b/drivers/acpi/device_sysfs.c index a673488066b3..f1e8928254c2 100644 --- a/drivers/acpi/device_sysfs.c +++ b/drivers/acpi/device_sysfs.c @@ -576,6 +576,11 @@ static const struct attribute_group acpi_group = { .is_visible = acpi_attr_is_visible, }; +const struct attribute_group *acpi_groups[] = { + &acpi_group, + NULL +}; + static const char *devm_acpi_device_str(struct acpi_device *dev) { struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; @@ -619,8 +624,6 @@ int acpi_device_setup_files(struct acpi_device *dev) int result = 0; dev->pnp.str = devm_acpi_device_str(dev); - result = device_add_group(&dev->dev, &acpi_group); - acpi_expose_nondev_subnodes(&dev->dev.kobj, &dev->data); return result; @@ -633,5 +636,4 @@ int acpi_device_setup_files(struct acpi_device *dev) void acpi_device_remove_files(struct acpi_device *dev) { acpi_hide_nondev_subnodes(&dev->data); - device_remove_group(&dev->dev, &acpi_group); } diff --git a/drivers/acpi/internal.h b/drivers/acpi/internal.h index 2a0e9fc7b74c..63dd78d80508 100644 --- a/drivers/acpi/internal.h +++ b/drivers/acpi/internal.h @@ -120,6 +120,7 @@ int acpi_tie_acpi_dev(struct acpi_device *adev); int acpi_device_add(struct acpi_device *device); int acpi_device_setup_files(struct acpi_device *dev); void acpi_device_remove_files(struct acpi_device *dev); +extern const struct attribute_group *acpi_groups[]; void acpi_device_add_finalize(struct acpi_device *device); void acpi_free_pnp_ids(struct acpi_device_pnp *pnp); bool acpi_device_is_enabled(const struct acpi_device *adev); diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index 503773707e01..c15fffefca0a 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -1813,6 +1813,7 @@ void acpi_init_device_object(struct acpi_device *device, acpi_handle handle, device->dev.parent = parent ? &parent->dev : NULL; device->dev.release = release; device->dev.bus = &acpi_bus_type; + device->dev.groups = acpi_groups; fwnode_init(&device->fwnode, &acpi_device_fwnode_ops); acpi_set_device_status(device, ACPI_STA_DEFAULT); acpi_device_get_busid(device); From patchwork Thu Jun 13 20:15:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 13697425 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DB34213B2AC; Thu, 13 Jun 2024 20:15:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718309740; cv=none; b=rYOGYx/eZHisVEN6NKRxS5IvqAcAjCvsHMiJGWDywiOnoQBiVfqeuteB/+NSwZ9t08rAaw4ONU7R8kG7Lax+8rj00h0Am6FQ/IpPlfAl2kI4EA41ozkBafHUO+qx6uk9IeOdVK1IOL01yet9IXulLJ7ARruJdHRcwNQkKSTQy28= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718309740; c=relaxed/simple; bh=uz3G8XwtTUx7pGyHKuMJTv6ZsMZ4E3FM8B10yoRcG+E=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lmWekFVfQAPtbjteqbk0mR777JZYCnlauPOumIDAcUOEitOjEVcAKQmT/BHqn0cWA4OoDff9g0vhlb1Ky6bdHl+MOZKyv/kBuXLmTmojOiQPbKziLuoeQodzO7FWnh8lzmJxOUKmlCnKdhgz+htNQQCzyIiLFxGvvNy45UOLOB8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=s6UXzttw; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="s6UXzttw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1718309732; bh=uz3G8XwtTUx7pGyHKuMJTv6ZsMZ4E3FM8B10yoRcG+E=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=s6UXzttwVWIX3YF/8qTQzMhi4Of/9wP5SpKKBIvpDYcC529qGm6PpxXuTg23mfzRI Y+5pEBsmKJF96OJdOKY3uovY72jyXLKaC50MF/z7zfFMp8tK/P9ARNGvr9gm0W+anF aAuQl2T2E0KbTB+QlPFUDpW9yhx7lIYP+DvPfpqE= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Thu, 13 Jun 2024 22:15:36 +0200 Subject: [PATCH 5/5] ACPI: sysfs: remove return value of acpi_device_setup_files() Precedence: bulk X-Mailing-List: linux-acpi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240613-acpi-sysfs-groups-v1-5-665e0deb052a@weissschuh.net> References: <20240613-acpi-sysfs-groups-v1-0-665e0deb052a@weissschuh.net> In-Reply-To: <20240613-acpi-sysfs-groups-v1-0-665e0deb052a@weissschuh.net> To: "Rafael J. Wysocki" , Len Brown , Robert Moore Cc: linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, acpica-devel@lists.linux.dev, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1718309732; l=2134; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=uz3G8XwtTUx7pGyHKuMJTv6ZsMZ4E3FM8B10yoRcG+E=; b=2cu/wdwm0qQfux6dwms8j3LcvJ8gZoJhkLB9ueU/8vGNsIVx3IQACCQxdNND4BTZmwPDWhzpx vvds8e2vZ1gD8qGfBYEn7FlQS6upf7abphkvk4/BG0HvVeFCoYzQig7 X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= The function can not fail anymore, so drop its return value. Signed-off-by: Thomas Weißschuh --- drivers/acpi/device_sysfs.c | 6 +----- drivers/acpi/internal.h | 2 +- drivers/acpi/scan.c | 5 +---- 3 files changed, 3 insertions(+), 10 deletions(-) diff --git a/drivers/acpi/device_sysfs.c b/drivers/acpi/device_sysfs.c index f1e8928254c2..c85ec754931c 100644 --- a/drivers/acpi/device_sysfs.c +++ b/drivers/acpi/device_sysfs.c @@ -619,14 +619,10 @@ static const char *devm_acpi_device_str(struct acpi_device *dev) * acpi_device_setup_files - Create sysfs attributes of an ACPI device. * @dev: ACPI device object. */ -int acpi_device_setup_files(struct acpi_device *dev) +void acpi_device_setup_files(struct acpi_device *dev) { - int result = 0; - dev->pnp.str = devm_acpi_device_str(dev); acpi_expose_nondev_subnodes(&dev->dev.kobj, &dev->data); - - return result; } /** diff --git a/drivers/acpi/internal.h b/drivers/acpi/internal.h index 63dd78d80508..e71fecbf731c 100644 --- a/drivers/acpi/internal.h +++ b/drivers/acpi/internal.h @@ -118,7 +118,7 @@ void acpi_init_device_object(struct acpi_device *device, acpi_handle handle, int type, void (*release)(struct device *)); int acpi_tie_acpi_dev(struct acpi_device *adev); int acpi_device_add(struct acpi_device *device); -int acpi_device_setup_files(struct acpi_device *dev); +void acpi_device_setup_files(struct acpi_device *dev); void acpi_device_remove_files(struct acpi_device *dev); extern const struct attribute_group *acpi_groups[]; void acpi_device_add_finalize(struct acpi_device *device); diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index c15fffefca0a..49a8172fe0de 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -766,10 +766,7 @@ int acpi_device_add(struct acpi_device *device) goto err; } - result = acpi_device_setup_files(device); - if (result) - pr_err("Error creating sysfs interface for device %s\n", - dev_name(&device->dev)); + acpi_device_setup_files(device); return 0;