git: 3e90716331cd - main - LinuxKPI: Add acpi_dev_get_first_match_dev and acpi_device_handle

From: Vladimir Kondratyev <wulf_at_FreeBSD.org>
Date: Sun, 21 Jul 2024 13:14:52 UTC
The branch main has been updated by wulf:

URL: https://cgit.FreeBSD.org/src/commit/?id=3e90716331cdbdee7465213d389a33f90dad11cf

commit 3e90716331cdbdee7465213d389a33f90dad11cf
Author:     Vladimir Kondratyev <wulf@FreeBSD.org>
AuthorDate: 2024-07-21 13:09:37 +0000
Commit:     Vladimir Kondratyev <wulf@FreeBSD.org>
CommitDate: 2024-07-21 13:09:37 +0000

    LinuxKPI: Add acpi_dev_get_first_match_dev and acpi_device_handle
    
    acpi_dev_get_first_match_dev returns the first match of ACPI device
    and acpi_device_handle returns its ACPI handle.
    
    Sponsored by:   Serenity Cyber Security, LLC
    MFC after:      1 week
    Reviewed by:    manu
    Differential Revision:  https://reviews.freebsd.org/D45847
---
 sys/compat/linuxkpi/common/include/acpi/acpi_bus.h |  7 +++++
 sys/compat/linuxkpi/common/include/linux/acpi.h    |  4 +++
 sys/compat/linuxkpi/common/src/linux_acpi.c        | 32 ++++++++++++++++++++++
 3 files changed, 43 insertions(+)

diff --git a/sys/compat/linuxkpi/common/include/acpi/acpi_bus.h b/sys/compat/linuxkpi/common/include/acpi/acpi_bus.h
index f107902a26ad..65bcbe7f1bdd 100644
--- a/sys/compat/linuxkpi/common/include/acpi/acpi_bus.h
+++ b/sys/compat/linuxkpi/common/include/acpi/acpi_bus.h
@@ -29,6 +29,9 @@
 #ifndef _LINUXKPI_ACPI_ACPI_BUS_H_
 #define _LINUXKPI_ACPI_ACPI_BUS_H_
 
+/* Aliase struct acpi_device to device_t */
+#define	acpi_device	_device
+
 typedef char acpi_device_class[20];
 
 struct acpi_bus_event {
@@ -38,6 +41,8 @@ struct acpi_bus_event {
 };
 
 #define	acpi_dev_present(...)	lkpi_acpi_dev_present(__VA_ARGS__)
+#define	acpi_dev_get_first_match_dev(...)	\
+	lkpi_acpi_dev_get_first_match_dev(__VA_ARGS__)
 
 ACPI_HANDLE	bsd_acpi_get_handle(device_t bsddev);
 bool		acpi_check_dsm(ACPI_HANDLE handle, const char *uuid, int rev,
@@ -50,5 +55,7 @@ int		unregister_acpi_notifier(struct notifier_block *nb);
 uint32_t	acpi_target_system_state(void);
 bool		lkpi_acpi_dev_present(const char *hid, const char *uid,
 		    int64_t hrv);
+struct acpi_device *lkpi_acpi_dev_get_first_match_dev(const char *hid,
+		    const char *uid, int64_t hrv);
 
 #endif /* _LINUXKPI_ACPI_ACPI_BUS_H_ */
diff --git a/sys/compat/linuxkpi/common/include/linux/acpi.h b/sys/compat/linuxkpi/common/include/linux/acpi.h
index 610aa0784cb9..3e1ec1b20626 100644
--- a/sys/compat/linuxkpi/common/include/linux/acpi.h
+++ b/sys/compat/linuxkpi/common/include/linux/acpi.h
@@ -39,6 +39,10 @@
 
 #define	ACPI_HANDLE(dev)	\
     ((dev)->bsddev != NULL ? bsd_acpi_get_handle((dev)->bsddev) : NULL)
+#define	acpi_device_handle(dev)	\
+    ((dev) != NULL ? bsd_acpi_get_handle(dev) : NULL)
+static inline void acpi_dev_put(struct acpi_device *adev) {}
+#define	acpi_handle_debug(handle, fmt, ...)
 
 #endif
 
diff --git a/sys/compat/linuxkpi/common/src/linux_acpi.c b/sys/compat/linuxkpi/common/src/linux_acpi.c
index 60ec838e9da7..6a9afb3ddff0 100644
--- a/sys/compat/linuxkpi/common/src/linux_acpi.c
+++ b/sys/compat/linuxkpi/common/src/linux_acpi.c
@@ -180,6 +180,7 @@ struct acpi_dev_present_ctx {
 	const char *hid;
 	const char *uid;
 	int64_t hrv;
+	struct acpi_device *dev;
 };
 
 static ACPI_STATUS
@@ -187,6 +188,7 @@ acpi_dev_present_cb(ACPI_HANDLE handle, UINT32 level, void *context,
     void **result)
 {
 	ACPI_DEVICE_INFO *devinfo;
+	struct acpi_device *dev;
 	struct acpi_dev_present_ctx *match = context;
 	bool present = false;
 	UINT32 sta, hrv;
@@ -230,6 +232,11 @@ acpi_dev_present_cb(ACPI_HANDLE handle, UINT32 level, void *context,
 			return (AE_OK);
 	}
 
+	dev = acpi_get_device(handle);
+	if (dev == NULL)
+		return (AE_OK);
+	match->dev = dev;
+
 	return (AE_ERROR);
 }
 
@@ -249,6 +256,24 @@ lkpi_acpi_dev_present(const char *hid, const char *uid, int64_t hrv)
 	return (rv == AE_ERROR);
 }
 
+struct acpi_device *
+lkpi_acpi_dev_get_first_match_dev(const char *hid, const char *uid,
+    int64_t hrv)
+{
+	struct acpi_dev_present_ctx match;
+	int rv;
+
+	match.hid = hid;
+	match.uid = uid;
+	match.hrv = hrv;
+	match.dev = NULL;
+
+	rv = AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
+	    ACPI_UINT32_MAX, acpi_dev_present_cb, NULL, &match, NULL);
+
+	return (rv == AE_ERROR ? match.dev : NULL);
+}
+
 static void
 linux_register_acpi_event_handlers(void *arg __unused)
 {
@@ -322,4 +347,11 @@ lkpi_acpi_dev_present(const char *hid, const char *uid, int64_t hrv)
 	return (false);
 }
 
+struct acpi_device *
+lkpi_acpi_dev_get_first_match_dev(const char *hid, const char *uid,
+    int64_t hrv)
+{
+	return (NULL);
+}
+
 #endif	/* !DEV_ACPI */