2022-06-30 15:17:03

by Stefan Binding

[permalink] [raw]
Subject: [PATCH v5 0/2] Read _SUB from ACPI to be able to identify firmware

CS35L41 has a DSP which is able to run firmware, as well as a tuning file.
Different systems may want to use different firmwares and tuning files, and
some firmwares/tunings may not be compatible with other systems.
To allow a system to select the correct fimware/tuning, we can read an _SUB
from the ACPI. This _SUB can then be used to uniquely identify the system
in the firmware/tuning file name.

Add a helper function which reads the _SUB, so this can be used by other
parts in the future.
Add support inside the CS35L41 ASoC driver to read this _SUB, and save it
appropriately.

Changes since v4:
- Rename function

Changes since v3:
- Fix 32 bit format string warning

Changes since v2:
- Fix error in function prototype

Changes since v1:
- Add length validation for SSID String
- Rename API
- Allocate memory inside API
- Use ACPI_HANDLE macro instead of ACPI_COMPANION
- Improve error handling

Stefan Binding (2):
ACPI: utils: Add api to read _SUB from ACPI
ASoC: cs35l41: Read System Name from ACPI _SUB to identify firmware

drivers/acpi/utils.c | 38 ++++++++++++++++++++++++++++++++++++++
include/linux/acpi.h | 6 ++++++
sound/soc/codecs/cs35l41.c | 24 ++++++++++++++++++++++++
3 files changed, 68 insertions(+)

--
2.25.1


2022-06-30 15:27:18

by Stefan Binding

[permalink] [raw]
Subject: [PATCH v5 1/2] ACPI: utils: Add api to read _SUB from ACPI

Add a wrapper function to read the _SUB string from ACPI.

Signed-off-by: Stefan Binding <[email protected]>
---
drivers/acpi/utils.c | 38 ++++++++++++++++++++++++++++++++++++++
include/linux/acpi.h | 6 ++++++
2 files changed, 44 insertions(+)

diff --git a/drivers/acpi/utils.c b/drivers/acpi/utils.c
index 3a9773a09e19..394954f4b6ef 100644
--- a/drivers/acpi/utils.c
+++ b/drivers/acpi/utils.c
@@ -291,6 +291,44 @@ int acpi_get_local_address(acpi_handle handle, u32 *addr)
}
EXPORT_SYMBOL(acpi_get_local_address);

+#define ACPI_MAX_SUB_BUF_SIZE 9
+
+const char *acpi_get_subsystem_id(acpi_handle handle)
+{
+ struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
+ union acpi_object *obj;
+ acpi_status status;
+ const char *sub;
+
+ status = acpi_evaluate_object(handle, METHOD_NAME__SUB, NULL, &buffer);
+ if (ACPI_FAILURE(status)) {
+ acpi_handle_debug(handle, "Reading ACPI _SUB failed: %#x\n", status);
+ return ERR_PTR(-ENODATA);
+ }
+
+ obj = buffer.pointer;
+ if (obj->type == ACPI_TYPE_STRING) {
+ if (strlen(obj->string.pointer) < ACPI_MAX_SUB_BUF_SIZE &&
+ strlen(obj->string.pointer) > 0) {
+ sub = kstrdup(obj->string.pointer, GFP_KERNEL);
+ if (!sub)
+ sub = ERR_PTR(-ENOMEM);
+ } else {
+ acpi_handle_err(handle, "ACPI _SUB Length %zu is Invalid\n",
+ strlen(obj->string.pointer));
+ sub = ERR_PTR(-EINVAL);
+ }
+ } else {
+ acpi_handle_warn(handle, "Warning ACPI _SUB did not return a string\n");
+ sub = ERR_PTR(-EINVAL);
+ }
+
+ acpi_os_free(buffer.pointer);
+
+ return sub;
+}
+EXPORT_SYMBOL_GPL(acpi_get_subsystem_id);
+
acpi_status
acpi_evaluate_reference(acpi_handle handle,
acpi_string pathname,
diff --git a/include/linux/acpi.h b/include/linux/acpi.h
index 7b96a8bff6d2..7651198ad6f5 100644
--- a/include/linux/acpi.h
+++ b/include/linux/acpi.h
@@ -762,6 +762,7 @@ static inline u64 acpi_arch_get_root_pointer(void)
#endif

int acpi_get_local_address(acpi_handle handle, u32 *addr);
+const char *acpi_get_subsystem_id(acpi_handle handle);

#else /* !CONFIG_ACPI */

@@ -1023,6 +1024,11 @@ static inline int acpi_get_local_address(acpi_handle handle, u32 *addr)
return -ENODEV;
}

+static inline const char *acpi_get_subsystem_id(acpi_handle handle)
+{
+ return ERR_PTR(-ENODEV);
+}
+
static inline int acpi_register_wakeup_handler(int wake_irq,
bool (*wakeup)(void *context), void *context)
{
--
2.25.1

2022-07-05 16:16:22

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [PATCH v5 1/2] ACPI: utils: Add api to read _SUB from ACPI

On Thu, Jun 30, 2022 at 5:01 PM Stefan Binding
<[email protected]> wrote:
>
> Add a wrapper function to read the _SUB string from ACPI.
>
> Signed-off-by: Stefan Binding <[email protected]>
> ---
> drivers/acpi/utils.c | 38 ++++++++++++++++++++++++++++++++++++++
> include/linux/acpi.h | 6 ++++++
> 2 files changed, 44 insertions(+)
>
> diff --git a/drivers/acpi/utils.c b/drivers/acpi/utils.c
> index 3a9773a09e19..394954f4b6ef 100644
> --- a/drivers/acpi/utils.c
> +++ b/drivers/acpi/utils.c
> @@ -291,6 +291,44 @@ int acpi_get_local_address(acpi_handle handle, u32 *addr)
> }
> EXPORT_SYMBOL(acpi_get_local_address);
>
> +#define ACPI_MAX_SUB_BUF_SIZE 9
> +
> +const char *acpi_get_subsystem_id(acpi_handle handle)
> +{
> + struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
> + union acpi_object *obj;
> + acpi_status status;
> + const char *sub;
> +
> + status = acpi_evaluate_object(handle, METHOD_NAME__SUB, NULL, &buffer);
> + if (ACPI_FAILURE(status)) {
> + acpi_handle_debug(handle, "Reading ACPI _SUB failed: %#x\n", status);
> + return ERR_PTR(-ENODATA);
> + }
> +
> + obj = buffer.pointer;
> + if (obj->type == ACPI_TYPE_STRING) {
> + if (strlen(obj->string.pointer) < ACPI_MAX_SUB_BUF_SIZE &&
> + strlen(obj->string.pointer) > 0) {
> + sub = kstrdup(obj->string.pointer, GFP_KERNEL);
> + if (!sub)
> + sub = ERR_PTR(-ENOMEM);

The error codes below are somewhat questionable.

> + } else {
> + acpi_handle_err(handle, "ACPI _SUB Length %zu is Invalid\n",
> + strlen(obj->string.pointer));
> + sub = ERR_PTR(-EINVAL);

This is as good as failure, so why not use -ENODATA here?

> + }
> + } else {
> + acpi_handle_warn(handle, "Warning ACPI _SUB did not return a string\n");
> + sub = ERR_PTR(-EINVAL);

Likewise.

> + }
> +
> + acpi_os_free(buffer.pointer);
> +
> + return sub;
> +}
> +EXPORT_SYMBOL_GPL(acpi_get_subsystem_id);
> +
> acpi_status
> acpi_evaluate_reference(acpi_handle handle,
> acpi_string pathname,
> diff --git a/include/linux/acpi.h b/include/linux/acpi.h
> index 7b96a8bff6d2..7651198ad6f5 100644
> --- a/include/linux/acpi.h
> +++ b/include/linux/acpi.h
> @@ -762,6 +762,7 @@ static inline u64 acpi_arch_get_root_pointer(void)
> #endif
>
> int acpi_get_local_address(acpi_handle handle, u32 *addr);
> +const char *acpi_get_subsystem_id(acpi_handle handle);
>
> #else /* !CONFIG_ACPI */
>
> @@ -1023,6 +1024,11 @@ static inline int acpi_get_local_address(acpi_handle handle, u32 *addr)
> return -ENODEV;
> }
>
> +static inline const char *acpi_get_subsystem_id(acpi_handle handle)
> +{
> + return ERR_PTR(-ENODEV);
> +}
> +
> static inline int acpi_register_wakeup_handler(int wake_irq,
> bool (*wakeup)(void *context), void *context)
> {
> --
> 2.25.1
>

2022-07-05 19:10:47

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v5 1/2] ACPI: utils: Add api to read _SUB from ACPI

On Thu, Jun 30, 2022 at 5:17 PM Stefan Binding
<[email protected]> wrote:
>
> Add a wrapper function to read the _SUB string from ACPI.

...

> + if (obj->type == ACPI_TYPE_STRING) {
> + if (strlen(obj->string.pointer) < ACPI_MAX_SUB_BUF_SIZE &&
> + strlen(obj->string.pointer) > 0) {
> + sub = kstrdup(obj->string.pointer, GFP_KERNEL);
> + if (!sub)
> + sub = ERR_PTR(-ENOMEM);
> + } else {
> + acpi_handle_err(handle, "ACPI _SUB Length %zu is Invalid\n",
> + strlen(obj->string.pointer));

Three times to evaluate strlen()... Can we do better, please?

> + sub = ERR_PTR(-EINVAL);
> + }
> + } else {
> + acpi_handle_warn(handle, "Warning ACPI _SUB did not return a string\n");
> + sub = ERR_PTR(-EINVAL);
> + }

--
With Best Regards,
Andy Shevchenko