From cf2d32235bd36b2d3de86f7cfdd8fb075b0e1d1e Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Sat, 17 Nov 2018 00:51:27 +0100
Subject: [PATCH 7/7] Test fix

---
 .../source/components/debugger/dbdisply.c     |  5 ++-
 .../source/components/namespace/nsdumpdv.c    |  5 ++-
 .../source/components/namespace/nsxfname.c    | 22 ++++++---
 .../dev/acpica/source/include/actypes.h       |  2 +
 sys/dev/acpica/acpi.c                         | 45 +++++++++++--------
 5 files changed, 52 insertions(+), 27 deletions(-)

diff --git a/sys/contrib/dev/acpica/source/components/debugger/dbdisply.c b/sys/contrib/dev/acpica/source/components/debugger/dbdisply.c
index f88d7076d5..0a49932313 100644
--- a/sys/contrib/dev/acpica/source/components/debugger/dbdisply.c
+++ b/sys/contrib/dev/acpica/source/components/debugger/dbdisply.c
@@ -822,8 +822,9 @@ AcpiDbDisplayObjectType (
         return;
     }
 
-    AcpiOsPrintf ("ADR: %8.8X%8.8X, Flags: %X\n",
-        ACPI_FORMAT_UINT64 (Info->Address), Info->Flags);
+    AcpiOsPrintf ("ADR: %8.8X%8.8X, STA: %8.8X, Flags: %X\n",
+        ACPI_FORMAT_UINT64 (Info->Address),
+        Info->CurrentStatus, Info->Flags);
 
     AcpiOsPrintf ("S1D-%2.2X S2D-%2.2X S3D-%2.2X S4D-%2.2X\n",
         Info->HighestDstates[0], Info->HighestDstates[1],
diff --git a/sys/contrib/dev/acpica/source/components/namespace/nsdumpdv.c b/sys/contrib/dev/acpica/source/components/namespace/nsdumpdv.c
index 6ab5ba5214..92a772f2aa 100644
--- a/sys/contrib/dev/acpica/source/components/namespace/nsdumpdv.c
+++ b/sys/contrib/dev/acpica/source/components/namespace/nsdumpdv.c
@@ -208,8 +208,9 @@ AcpiNsDumpOneDevice (
         }
 
         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_TABLES,
-            "    HID: %s, ADR: %8.8X%8.8X\n",
-            Info->HardwareId.Value, ACPI_FORMAT_UINT64 (Info->Address)));
+            "    HID: %s, ADR: %8.8X%8.8X, Status: %X\n",
+            Info->HardwareId.Value, ACPI_FORMAT_UINT64 (Info->Address),
+            Info->CurrentStatus));
         ACPI_FREE (Info);
     }
 
diff --git a/sys/contrib/dev/acpica/source/components/namespace/nsxfname.c b/sys/contrib/dev/acpica/source/components/namespace/nsxfname.c
index f94c0e45de..d5b32eda9c 100644
--- a/sys/contrib/dev/acpica/source/components/namespace/nsxfname.c
+++ b/sys/contrib/dev/acpica/source/components/namespace/nsxfname.c
@@ -376,7 +376,7 @@ AcpiNsCopyDeviceId (
  *              namespace node and possibly by running several standard
  *              control methods (Such as in the case of a device.)
  *
- * For Device and Processor objects, run the Device _HID, _UID, _CID,
+ * For Device and Processor objects, run the Device _HID, _UID, _CID, _STA,
  * _CLS, _ADR, _SxW, and _SxD methods.
  *
  * Note: Allocates the return buffer, must be freed by the caller.
@@ -385,9 +385,8 @@ AcpiNsCopyDeviceId (
  * discovery namespace traversal. Therefore, no complex methods can be
  * executed, especially those that access operation regions. Therefore, do
  * not add any additional methods that could cause problems in this area.
- * Because of this reason support for the following methods has been removed:
- * 1) _SUB method was removed (11/2015)
- * 2) _STA method was removed (02/2018)
+ * this was the fate of the _SUB method which was found to cause such
+ * problems and was removed (11/2015).
  *
  ******************************************************************************/
 
@@ -518,13 +517,26 @@ AcpiGetObjectInfo (
     {
         /*
          * Get extra info for ACPI Device/Processor objects only:
-         * Run the _ADR and, SxW, and _SxD methods.
+         * Run the _STA, _ADR and, SxW, and _SxD methods.
          *
          * Notes: none of these methods are required, so they may or may
          * not be present for this device. The Info->Valid bitfield is used
          * to indicate which methods were found and run successfully.
+         *
+         * For _STA, if the method does not exist, then (as per the ACPI
+         * specification), the returned CurrentStatus flags will indicate
+         * that the device is present/functional/enabled. Otherwise, the
+         * CurrentStatus flags reflect the value returned from _STA.
          */
 
+        /* Execute the Device._STA method */
+
+        Status = AcpiUtExecute_STA (Node, &Info->CurrentStatus);
+        if (ACPI_SUCCESS (Status))
+        {
+            Valid |= ACPI_VALID_STA;
+        }
+
         /* Execute the Device._ADR method */
 
         Status = AcpiUtEvaluateNumericObject (METHOD_NAME__ADR, Node,
diff --git a/sys/contrib/dev/acpica/source/include/actypes.h b/sys/contrib/dev/acpica/source/include/actypes.h
index 4d2438c9ec..478de53cde 100644
--- a/sys/contrib/dev/acpica/source/include/actypes.h
+++ b/sys/contrib/dev/acpica/source/include/actypes.h
@@ -1390,6 +1390,7 @@ typedef struct acpi_device_info
     UINT8                           Flags;              /* Miscellaneous info */
     UINT8                           HighestDstates[4];  /* _SxD values: 0xFF indicates not valid */
     UINT8                           LowestDstates[5];   /* _SxW values: 0xFF indicates not valid */
+    UINT32                          CurrentStatus;      /* _STA value */
     UINT64                          Address;            /* _ADR value */
     ACPI_PNP_DEVICE_ID              HardwareId;         /* _HID value */
     ACPI_PNP_DEVICE_ID              UniqueId;           /* _UID value */
@@ -1404,6 +1405,7 @@ typedef struct acpi_device_info
 
 /* Flags for Valid field above (AcpiGetObjectInfo) */
 
+#define ACPI_VALID_STA                  0x0001
 #define ACPI_VALID_ADR                  0x0002
 #define ACPI_VALID_HID                  0x0004
 #define ACPI_VALID_UID                  0x0008
diff --git a/sys/dev/acpica/acpi.c b/sys/dev/acpica/acpi.c
index ad19a68d99..f07294657d 100644
--- a/sys/dev/acpica/acpi.c
+++ b/sys/dev/acpica/acpi.c
@@ -881,9 +881,8 @@ acpi_driver_added(device_t dev, driver_t *driver)
 	child = devlist[i];
 	if (device_get_state(child) == DS_NOTPRESENT) {
 	    /* pci_set_powerstate(child, PCI_POWERSTATE_D0); */
-	    if (device_probe_and_attach(child) != 0) {
+	    if (device_probe_and_attach(child) != 0)
 		; /* pci_set_powerstate(child, PCI_POWERSTATE_D3); */
-	    }
 	}
     }
     kfree(devlist, M_TEMP);
@@ -1971,20 +1970,25 @@ acpi_enable_fixed_events(struct acpi_softc *sc)
 BOOLEAN
 acpi_DeviceIsPresent(device_t dev)
 {
+    ACPI_DEVICE_INFO	*devinfo;
     ACPI_HANDLE		h;
-    UINT32		s;
-    ACPI_STATUS		status;
+    int			ret;
 
-    h = acpi_get_handle(dev);
-    if (h == NULL)
+    ret = FALSE;
+    if ((h = acpi_get_handle(dev)) == NULL ||
+	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
 	return (FALSE);
-    status = acpi_GetInteger(h, "_STA", &s);
 
     /* If no _STA method, must be present */
-    if (ACPI_FAILURE(status))
-	return (status == AE_NOT_FOUND ? TRUE : FALSE);
+    if ((devinfo->Valid & ACPI_VALID_STA) == 0)
+	ret = TRUE;
 
-    return (ACPI_DEVICE_PRESENT(s) ? TRUE : FALSE);
+    /* Return true for 'present' and 'functioning' */
+    if (ACPI_DEVICE_PRESENT(devinfo->CurrentStatus))
+	ret = TRUE;
+
+    AcpiOsFree(devinfo);
+    return (ret);
 }
 
 /*
@@ -1993,20 +1997,25 @@ acpi_DeviceIsPresent(device_t dev)
 BOOLEAN
 acpi_BatteryIsPresent(device_t dev)
 {
+    ACPI_DEVICE_INFO	*devinfo;
     ACPI_HANDLE		h;
-    UINT32		s;
-    ACPI_STATUS		status;
+    int			ret;
 
-    h = acpi_get_handle(dev);
-    if (h == NULL)
+    ret = FALSE;
+    if ((h = acpi_get_handle(dev)) == NULL ||
+	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
 	return (FALSE);
-    status = acpi_GetInteger(h, "_STA", &s);
 
     /* If no _STA method, must be present */
-    if (ACPI_FAILURE(status))
-	return (status == AE_NOT_FOUND ? TRUE : FALSE);
+    if ((devinfo->Valid & ACPI_VALID_STA) == 0)
+	ret = TRUE;
 
-    return (ACPI_BATTERY_PRESENT(s) ? TRUE : FALSE);
+    /* Return true for 'present', 'battery present', and 'functioning' */
+    if (ACPI_BATTERY_PRESENT(devinfo->CurrentStatus))
+	ret = TRUE;
+
+    AcpiOsFree(devinfo);
+    return (ret);
 }
 
 /*
-- 
2.19.1

