kernel/acpi: Bring in acpi_wmi(4) from FreeBSD, which acpi_hp(4) needs.
authorSascha Wildner <saw@online.de>
Sat, 17 Mar 2012 16:53:51 +0000 (17:53 +0100)
committerSascha Wildner <saw@online.de>
Sat, 17 Mar 2012 16:58:20 +0000 (17:58 +0100)
Also, hook acpi_hp(4) back into the build.

Some fixes to acpi_hp(4) were brought in, too.

It is all untested, unfortunately. The hope is, that someone with a
HP laptop might check it out.

15 files changed:
Makefile_upgrade.inc
share/man/man4/Makefile
share/man/man4/acpi.4
share/man/man4/acpi_hp.4
share/man/man4/acpi_wmi.4 [new file with mode: 0644]
sys/conf/files
sys/conf/kmod.mk
sys/config/LINT
sys/config/LINT64
sys/dev/acpica5/Makefile
sys/dev/acpica5/acpi_hp/Makefile
sys/dev/acpica5/acpi_hp/acpi_hp.c
sys/dev/acpica5/acpi_wmi/Makefile [new file with mode: 0644]
sys/dev/acpica5/acpi_wmi/acpi_wmi.c [new file with mode: 0644]
sys/dev/acpica5/acpi_wmi_if.m

index 66fdd82..6668334 100644 (file)
@@ -2029,8 +2029,3 @@ TO_REMOVE+=/usr/share/man/man4/rum.4.gz
 TO_REMOVE+=/boot/modules/if_ural.ko
 TO_REMOVE+=/usr/share/man/cat4/ural.4.gz
 TO_REMOVE+=/usr/share/man/man4/ural.4.gz
-
-# XXX Remove when acpi_hp(4) has been fixed
-TO_REMOVE+=/boot/kernel/acpi_hp.ko
-TO_REMOVE+=/usr/share/man/cat4/acpi_hp.4.gz
-TO_REMOVE+=/usr/share/man/man4/acpi_hp.4.gz
index 46edd05..096f38c 100644 (file)
@@ -5,12 +5,14 @@ MAN=  aac.4 \
        acpi.4 \
        acpi_asus.4 \
        acpi_fujitsu.4 \
+       acpi_hp.4 \
        acpi_panasonic.4 \
        acpi_sony.4 \
        acpi_thermal.4 \
        acpi_thinkpad.4 \
        acpi_toshiba.4 \
        acpi_video.4 \
+       acpi_wmi.4 \
        adv.4 \
        adw.4 \
        ae.4 \
@@ -380,9 +382,6 @@ MAN=        aac.4 \
 #      rum.4 \
 #      ural.4 \
 
-# XXX re-add when acpi_hp(4) has been fixed
-#      acpi_hp.4 \
-
 MLINKS=        acpi_thermal.4 acpi_tz.4
 MLINKS+=agp.4 agpgart.4
 MLINKS+=bktr.4 brooktree.4
index 77d1249..c18f677 100644 (file)
@@ -580,7 +580,7 @@ ACPI is only found and supported on i386/ia32, ia64, and x86_64.
 .Xr kenv 1 ,
 .Xr acpi_asus 4 ,
 .Xr acpi_fujitsu 4 ,
-.\".Xr acpi_hp 4 ,
+.Xr acpi_hp 4 ,
 .Xr acpi_panasonic 4 ,
 .Xr acpi_sony 4 ,
 .Xr acpi_thermal 4 ,
index 68c906f..ab37ffc 100644 (file)
@@ -22,9 +22,9 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.\" $FreeBSD: src/share/man/man4/acpi_hp.4,v 1.4 2009/07/03 21:12:37 rpaulo Exp $
+.\" $FreeBSD: src/share/man/man4/acpi_hp.4,v 1.7 2010/10/08 12:40:16 uqs Exp $
 .\"
-.Dd November 11, 2009
+.Dd March 16, 2012
 .Dt ACPI_HP 4
 .Os
 .Sh NAME
@@ -246,7 +246,7 @@ sysctl hw.acpi.hp.cmi_detail=7
 .Ed
 .Sh SEE ALSO
 .Xr acpi 4 ,
-.\".Xr acpi_wmi 4 ,
+.Xr acpi_wmi 4 ,
 .Xr devfs 5 ,
 .Xr sysctl.conf 5 ,
 .Xr devd 8 ,
@@ -279,7 +279,7 @@ http://www.microsoft.com/whdc/system/pnppwr/wmi/wmi-acpi.mspx
 This manual page was written by
 .An Michael Gmelin Aq freebsd@grem.de
 .Sh BUGS
-This driver is experimental and has only been tested on CURRENT i386 on an
+This driver is experimental and has only been tested on i386 on an
 HP Compaq 8510p which featured all supported wireless devices (WWAN/BT/WLAN).
 Expect undefined results when operating on different hardware.
 .Pp
diff --git a/share/man/man4/acpi_wmi.4 b/share/man/man4/acpi_wmi.4
new file mode 100644 (file)
index 0000000..9467983
--- /dev/null
@@ -0,0 +1,96 @@
+.\" Copyright (c) 2009 Michael Gmelin
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD: src/share/man/man4/acpi_wmi.4,v 1.7 2011/11/22 16:44:50 jh Exp $
+.\"
+.Dd March 13, 2012
+.Dt ACPI_WMI 4
+.Os
+.Sh NAME
+.Nm acpi_wmi
+.Nd "ACPI to WMI mapping driver"
+.Sh SYNOPSIS
+To compile this driver into the kernel,
+place the following line in your
+kernel configuration file:
+.Bd -ragged -offset indent
+.Cd "device acpi_wmi"
+.Ed
+.Pp
+Alternatively, to load the driver as a
+module at boot time, place the following line in
+.Xr loader.conf 5 :
+.Bd -literal -offset indent
+acpi_wmi_load="YES"
+.Ed
+.Sh DESCRIPTION
+The
+.Nm
+driver provides an interface for vendor specific WMI implementations
+(e.g. HP and Acer laptops).
+It creates /dev/wmistat%d, which can be read to get
+information about GUIDs found in the system.
+.Sh FILES
+.Bl -tag -width /dev/wmistat%d -compact
+.It Pa /dev/wmistat%d
+WMI status device.
+.El
+.Sh EXAMPLES
+.Bd -literal
+# cat /dev/wmistat0
+GUID                                  INST EXPE METH STR EVENT OID
+{5FB7F034-2C63-45E9-BE91-3D44E2C707E4}   1 NO   WMAA NO  NO    AA
+{95F24279-4D7B-4334-9387-ACCDC67EF61C}   1 NO   NO   NO  0x80+ -
+{2B814318-4BE8-4707-9D84-A190A859B5D0}   1 NO   NO   NO  0xA0  -
+{05901221-D566-11D1-B2F0-00A0C9062910}   1 NO   NO   NO  NO    AB
+{1F4C91EB-DC5C-460B-951D-C7CB9B4B8D5E}   1 NO   WMBA NO  NO    BA
+{2D114B49-2DFB-4130-B8FE-4A3C09E75133}  57 NO   NO   NO  NO    BC
+{988D08E3-68F4-4C35-AF3E-6A1B8106F83C}  20 NO   NO   NO  NO    BD
+{14EA9746-CE1F-4098-A0E0-7045CB4DA745}   1 NO   NO   NO  NO    BE
+{322F2028-0F84-4901-988E-015176049E2D}   2 NO   NO   NO  NO    BF
+{8232DE3D-663D-4327-A8F4-E293ADB9BF05}   0 NO   NO   NO  NO    BG
+{8F1F6436-9F42-42C8-BADC-0E9424F20C9A}   0 NO   NO   NO  NO    BH
+{8F1F6435-9F42-42C8-BADC-0E9424F20C9A}   0 NO   NO   NO  NO    BI
+.Ed
+.Sh SEE ALSO
+.Xr acpi 4 ,
+.Sh HISTORY
+The
+.Nm
+device driver first appeared in
+.Fx 8.0 .
+.Sh AUTHORS
+.An -nosplit
+The
+.Nm
+driver was written by
+.An Michael Gmelin Aq freebsd@grem.de
+.Pp
+Work has been inspired by the Linux acpi-wmi driver written by Carlos Corbacho
+.Pp
+See http://www.microsoft.com/whdc/system/pnppwr/wmi/wmi-acpi.mspx for
+the specification of ACPI-WMI.
+.Pp
+This manual page was written by
+.An Michael Gmelin Aq freebsd@grem.de
index 870047c..e9f2474 100644 (file)
@@ -1969,7 +1969,7 @@ ${OSACPI_MI_DIR}/acpi_powerres.c  optional acpi
 ${OSACPI_MI_DIR}/acpi_resource.c       optional acpi
 ${OSACPI_MI_DIR}/acpi_thermal.c                optional acpi
 ${OSACPI_MI_DIR}/acpi_timer.c          optional acpi
-#${OSACPI_MI_DIR}/acpi_wmi_if.m                optional acpi
+${OSACPI_MI_DIR}/acpi_wmi_if.m         optional acpi
 ${OSACPI_MI_DIR}/Osd/OsdCache.c                optional acpi
 ${OSACPI_MI_DIR}/Osd/OsdDebug.c                optional acpi
 ${OSACPI_MI_DIR}/Osd/OsdHardware.c     optional acpi
@@ -1982,12 +1982,13 @@ ${OSACPI_MI_DIR}/Osd/OsdTable.c         optional acpi
 
 ${OSACPI_MI_DIR}/acpi_asus/acpi_asus.c         optional acpi_asus acpi
 ${OSACPI_MI_DIR}/acpi_fujitsu/acpi_fujitsu.c   optional acpi_fujitsu acpi
-#${OSACPI_MI_DIR}/acpi_hp/acpi_hp.c            optional acpi_hp acpi
+${OSACPI_MI_DIR}/acpi_hp/acpi_hp.c             optional acpi_hp acpi
 ${OSACPI_MI_DIR}/acpi_panasonic/acpi_panasonic.c optional acpi_panasonic acpi
 ${OSACPI_MI_DIR}/acpi_sony/acpi_sony.c         optional acpi_sony acpi
 ${OSACPI_MI_DIR}/acpi_thinkpad/acpi_thinkpad.c optional acpi_thinkpad acpi
 ${OSACPI_MI_DIR}/acpi_toshiba/acpi_toshiba.c   optional acpi_toshiba acpi
 ${OSACPI_MI_DIR}/acpi_video/acpi_video.c       optional acpi_video acpi
+${OSACPI_MI_DIR}/acpi_wmi/acpi_wmi.c           optional acpi_wmi acpi
 ${OSACPI_MI_DIR}/aibs/atk0110.c                        optional aibs acpi
 
 # dsched stuff
index 759b450..71517eb 100644 (file)
@@ -300,7 +300,7 @@ MFILES?= kern/bus_if.m kern/device_if.m bus/iicbus/iicbb_if.m \
     bus/pccard/card_if.m bus/pccard/power_if.m bus/pci/pci_if.m \
     bus/pci/pcib_if.m \
     bus/ppbus/ppbus_if.m bus/smbus/smbus_if.m bus/usb/usb_if.m \
-    dev/acpica5/acpi_if.m dev/disk/nata/ata_if.m \
+    dev/acpica5/acpi_if.m dev/acpica5/acpi_wmi_if.m dev/disk/nata/ata_if.m \
     dev/sound/pcm/ac97_if.m dev/sound/pcm/channel_if.m \
     dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m \
     libiconv/iconv_converter_if.m dev/agp/agp_if.m opencrypto/cryptodev_if.m \
index df09a72..6f0280e 100644 (file)
@@ -2399,6 +2399,9 @@ device            rdrand          # hardware RNG for RdRand
 device         acpi
 options        ACPI_DEBUG
 
+# ACPI WMI Mapping driver
+device         acpi_wmi
+
 # ACPI Asus Extras (LCD backlight/brightness, video output, etc.)
 device         acpi_asus
 
@@ -2406,7 +2409,7 @@ device            acpi_asus
 device         acpi_fujitsu
 
 # ACPI extras driver for HP laptops
-#device                acpi_hp
+device         acpi_hp
 
 # ACPI Panasonic Extras (LCD backlight/brightness, video output, etc.)
 device         acpi_panasonic
index 803bb30..bcc60df 100644 (file)
@@ -2164,6 +2164,9 @@ device            rdrand          # hardware RNG for RdRand
 device         acpi
 options        ACPI_DEBUG
 
+# ACPI WMI Mapping driver
+device         acpi_wmi
+
 # ACPI Asus Extras (LCD backlight/brightness, video output, etc.)
 device         acpi_asus
 
@@ -2171,7 +2174,7 @@ device            acpi_asus
 device         acpi_fujitsu
 
 # ACPI extras driver for HP laptops
-#device                acpi_hp
+device         acpi_hp
 
 # ACPI Panasonic Extras (LCD backlight/brightness, video output, etc.)
 device         acpi_panasonic
index 2ff929f..a32cacc 100644 (file)
@@ -115,7 +115,7 @@ acpi_wakecode.h: acpi_wakecode.S
        ${MAKE} -f ${SYSDIR}/${OSACPI_MD_DIR}/Makefile \
                MAKESRCPATH=${SYSDIR}/${OSACPI_MD_DIR}
 
-SUBDIR=        acpi_asus acpi_fujitsu acpi_panasonic acpi_sony acpi_thinkpad acpi_toshiba acpi_video aibs
-#SUBDIR+=acpi_hp
+SUBDIR=        acpi_asus acpi_fujitsu acpi_hp acpi_panasonic acpi_sony
+SUBDIR+=acpi_thinkpad acpi_toshiba acpi_video acpi_wmi aibs
 
 .include <bsd.kmod.mk>
index c439ae3..7ab26a0 100644 (file)
@@ -1,5 +1,7 @@
+# $FreeBSD: src/sys/modules/acpi/acpi_hp/Makefile,v 1.1 2009/06/23 13:17:25 rpaulo Exp $
+
 KMOD=  acpi_hp
-CFLAGS+=-I${.OBJDIR}/.. -I${.CURDIR}/..
+CFLAGS+=-I${.CURDIR}/..
 SRCS=  acpi_hp.c opt_acpi.h acpi_if.h acpi_wmi_if.h device_if.h bus_if.h
 
 .include <bsd.kmod.mk>
index e086a69..37d8989 100644 (file)
@@ -23,7 +23,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: src/sys/dev/acpi_support/acpi_hp.c,v 1.3 2009/07/03 21:12:37 rpaulo
+ * $FreeBSD: src/sys/dev/acpi_support/acpi_hp.c,v 1.7 2010/09/11 08:09:14 avg Exp $
  */
 
 /*
@@ -109,8 +109,6 @@ ACPI_MODULE_NAME("HP")
 #define ACPI_HP_CMI_DETAIL_FLAGS               0x04
 #define ACPI_HP_CMI_DETAIL_SHOW_MAX_INSTANCE   0x08
 
-#define sbuf_done(s)           ((s)->s_flags & SBUF_FINISHED)
-
 struct acpi_hp_inst_seq_pair {
        UINT32  sequence;       /* sequence number as suggested by cmi bios */
        UINT8   instance;       /* object instance on guid */
@@ -118,7 +116,6 @@ struct acpi_hp_inst_seq_pair {
 
 struct acpi_hp_softc {
        device_t        dev;
-       ACPI_HANDLE     handle;
        device_t        wmi_dev;
        int             has_notify;             /* notification GUID found */
        int             has_cmi;                /* CMI GUID found */
@@ -291,6 +288,7 @@ static struct {
 
 ACPI_SERIAL_DECL(hp, "HP ACPI-WMI Mapping");
 
+static void    acpi_hp_identify(driver_t *driver, device_t parent);
 static int     acpi_hp_probe(device_t dev);
 static int     acpi_hp_attach(device_t dev);
 static int     acpi_hp_detach(device_t dev);
@@ -327,6 +325,7 @@ static struct dev_ops hpcmi_ops = {
 };
 
 static device_method_t acpi_hp_methods[] = {
+       DEVMETHOD(device_identify, acpi_hp_identify),
        DEVMETHOD(device_probe, acpi_hp_probe),
        DEVMETHOD(device_attach, acpi_hp_attach),
        DEVMETHOD(device_detach, acpi_hp_detach),
@@ -341,8 +340,9 @@ static driver_t     acpi_hp_driver = {
 
 static devclass_t acpi_hp_devclass;
 
-DRIVER_MODULE(acpi_hp, acpi, acpi_hp_driver, acpi_hp_devclass,
-               0, 0);
+DRIVER_MODULE(acpi_hp, acpi_wmi, acpi_hp_driver, acpi_hp_devclass,
+               NULL, NULL);
+MODULE_VERSION(acpi_hp, 1);
 MODULE_DEPEND(acpi_hp, acpi_wmi, 1, 1, 1);
 MODULE_DEPEND(acpi_hp, acpi, 1, 1, 1);
 
@@ -412,7 +412,7 @@ acpi_hp_evaluate_auto_on_off(struct acpi_hp_softc *sc)
                                    "WLAN on air changed to %i "
                                    "(new_wlan_status is %i)\n",
                                    sc->was_wlan_on_air, new_wlan_status);
-                       acpi_UserNotify("HP", sc->handle,
+                       acpi_UserNotify("HP", ACPI_ROOT_OBJECT,
                            0xc0+sc->was_wlan_on_air);
                }
        }
@@ -427,7 +427,7 @@ acpi_hp_evaluate_auto_on_off(struct acpi_hp_softc *sc)
                                    " to %i (new_bluetooth_status is %i)\n",
                                    sc->was_bluetooth_on_air,
                                    new_bluetooth_status);
-                       acpi_UserNotify("HP", sc->handle,
+                       acpi_UserNotify("HP", ACPI_ROOT_OBJECT,
                            0xd0+sc->was_bluetooth_on_air);
                }
        }
@@ -440,19 +440,33 @@ acpi_hp_evaluate_auto_on_off(struct acpi_hp_softc *sc)
                                    "WWAN on air changed to %i"
                                    " (new_wwan_status is %i)\n",
                                    sc->was_wwan_on_air, new_wwan_status);
-                       acpi_UserNotify("HP", sc->handle,
+                       acpi_UserNotify("HP", ACPI_ROOT_OBJECT,
                            0xe0+sc->was_wwan_on_air);
                }
        }
 }
 
+static void
+acpi_hp_identify(driver_t *driver, device_t parent)
+{
+
+       /* Don't do anything if driver is disabled. */
+       if (acpi_disabled("hp"))
+               return;
+
+       /* Add only a single device instance. */
+       if (device_find_child(parent, "acpi_hp", -1) != NULL)
+               return;
+
+       if (BUS_ADD_CHILD(parent, parent, 0, "acpi_hp", -1) == NULL)
+               device_printf(parent, "add acpi_hp child failed\n");
+}
+
 static int
 acpi_hp_probe(device_t dev)
 {
-       if (acpi_disabled("hp") || device_get_unit(dev) != 0)
-               return (ENXIO);
-       device_set_desc(dev, "HP ACPI-WMI Mapping");
 
+       device_set_desc(dev, "HP ACPI-WMI Mapping");
        return (0);
 }
 
@@ -461,14 +475,12 @@ acpi_hp_attach(device_t dev)
 {
        struct acpi_hp_softc    *sc;
        struct acpi_softc       *acpi_sc;
-       devclass_t              wmi_devclass;
        int                     arg;
 
        ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
 
        sc = device_get_softc(dev);
        sc->dev = dev;
-       sc->handle = acpi_get_handle(dev);
        sc->has_notify = 0;
        sc->has_cmi = 0;
        sc->bluetooth_enable_if_radio_on = 0;
@@ -486,14 +498,7 @@ acpi_hp_attach(device_t dev)
        memset(sc->cmi_order, 0, sizeof(sc->cmi_order));
        acpi_sc = acpi_device_get_parent_softc(dev);
 
-       if (!(wmi_devclass = devclass_find ("acpi_wmi"))) {
-               device_printf(dev, "Couldn't find acpi_wmi devclass\n");
-               return (EINVAL);
-       }
-       if (!(sc->wmi_dev = devclass_get_device(wmi_devclass, 0))) {
-               device_printf(dev, "Couldn't find acpi_wmi device\n");
-               return (EINVAL);
-       }
+       sc->wmi_dev = device_get_parent(dev);
        if (!ACPI_WMI_PROVIDES_GUID_STRING(sc->wmi_dev,
            ACPI_HP_WMI_BIOS_GUID)) {
                device_printf(dev,
@@ -816,7 +821,7 @@ acpi_hp_notify(ACPI_HANDLE h, UINT32 notify, void *context)
        ACPI_OBJECT *obj;
        ACPI_WMI_GET_EVENT_DATA(sc->wmi_dev, notify, &response);
        obj = (ACPI_OBJECT*) response.Pointer;
-       if (obj && obj->Type == ACPI_TYPE_BUFFER && obj->Buffer.Length == 8) {
+       if (!obj || obj->Type != ACPI_TYPE_PACKAGE) {
                if (*((UINT8 *) obj->Buffer.Pointer) == 0x5) {
                        acpi_hp_evaluate_auto_on_off(sc);
                }
diff --git a/sys/dev/acpica5/acpi_wmi/Makefile b/sys/dev/acpica5/acpi_wmi/Makefile
new file mode 100644 (file)
index 0000000..25613f4
--- /dev/null
@@ -0,0 +1,7 @@
+# $FreeBSD: src/sys/modules/acpi/acpi_wmi/Makefile,v 1.1 2009/06/23 13:17:25 rpaulo Exp $
+
+KMOD=  acpi_wmi
+CFLAGS+=-I${.CURDIR}/..
+SRCS=  acpi_wmi.c opt_acpi.h acpi_if.h acpi_wmi_if.h device_if.h bus_if.h
+
+.include <bsd.kmod.mk>
diff --git a/sys/dev/acpica5/acpi_wmi/acpi_wmi.c b/sys/dev/acpica5/acpi_wmi/acpi_wmi.c
new file mode 100644 (file)
index 0000000..9a2ed70
--- /dev/null
@@ -0,0 +1,1019 @@
+/*-
+ * Copyright (c) 2009 Michael Gmelin <freebsd@grem.de>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/sys/dev/acpi_support/acpi_wmi.c,v 1.9 2011/11/22 21:28:20 marius Exp $
+ */
+
+/*
+ * Driver for acpi-wmi mapping, provides an interface for vendor specific
+ * implementations (e.g. HP and Acer laptops).
+ * Inspired by the ACPI-WMI mapping driver (c) 2008-2008 Carlos Corbacho which
+ * implements this functionality for Linux.
+ *
+ * WMI and ACPI: http://www.microsoft.com/whdc/system/pnppwr/wmi/wmi-acpi.mspx
+ * acpi-wmi for Linux: http://www.kernel.org
+ */
+
+#include "opt_acpi.h"
+#include <sys/param.h>
+#include <sys/conf.h>
+#include <sys/uio.h>
+#include <sys/proc.h>
+#include <sys/kernel.h>
+#include <sys/malloc.h>
+#include <sys/sbuf.h>
+#include <sys/module.h>
+#include <sys/bus.h>
+#include <sys/device.h>
+
+#include "acpi.h"
+#include "accommon.h"
+#include <dev/acpica5/acpivar.h>
+#include "acpi_wmi_if.h"
+
+static MALLOC_DEFINE(M_ACPIWMI, "acpiwmi", "ACPI-WMI mapping");
+
+#define _COMPONENT     ACPI_OEM
+ACPI_MODULE_NAME("ACPI_WMI");
+
+#define ACPI_WMI_REGFLAG_EXPENSIVE     0x1 /* GUID flag: Expensive operation */
+#define ACPI_WMI_REGFLAG_METHOD                0x2     /* GUID flag: Method call */
+#define ACPI_WMI_REGFLAG_STRING                0x4     /* GUID flag: String */
+#define ACPI_WMI_REGFLAG_EVENT         0x8     /* GUID flag: Event */
+
+/*
+ * acpi_wmi driver private structure
+ */
+struct acpi_wmi_softc {
+       device_t        wmi_dev;        /* wmi device id */
+       ACPI_HANDLE     wmi_handle;     /* handle of the PNP0C14 node */
+       device_t        ec_dev;         /* acpi_ec0 */
+       struct cdev     *wmistat_dev_t; /* wmistat device handle */
+       struct sbuf     wmistat_sbuf;   /* sbuf for /dev/wmistat output */
+       pid_t           wmistat_open_pid; /* pid operating on /dev/wmistat */
+       int             wmistat_bufptr; /* /dev/wmistat ptr to buffer position */
+};
+
+/*
+ * Struct that holds information about
+ * about a single GUID entry in _WDG
+ */
+struct guid_info {
+       char    guid[16];       /* 16 byte non human readable GUID */
+       char    oid[2];         /* object id or event notify id (first byte) */
+       UINT8   max_instance;   /* highest instance known for this GUID */
+       UINT8   flags;          /* ACPI_WMI_REGFLAG_%s */
+};
+
+/* WExx event generation state (on/off) */
+enum event_generation_state {
+       EVENT_GENERATION_ON = 1,
+       EVENT_GENERATION_OFF = 0
+};
+
+
+/*
+ * Information about one entry in _WDG.
+ * List of those is used to lookup information by GUID.
+ */
+struct wmi_info {
+       TAILQ_ENTRY(wmi_info)   wmi_list;
+       struct guid_info        ginfo;          /* information on guid */
+       ACPI_NOTIFY_HANDLER     event_handler;/* client provided event handler */
+       void                    *event_handler_user_data; /* ev handler cookie  */
+};
+
+TAILQ_HEAD(wmi_info_list_head, wmi_info)
+    wmi_info_list = TAILQ_HEAD_INITIALIZER(wmi_info_list);
+
+ACPI_SERIAL_DECL(acpi_wmi, "ACPI-WMI Mapping");
+
+/* public interface - declaration */
+/* standard device interface*/
+static int             acpi_wmi_probe(device_t dev);
+static int             acpi_wmi_attach(device_t dev);
+static int             acpi_wmi_detach(device_t dev);
+/* see acpi_wmi_if.m */
+static int             acpi_wmi_provides_guid_string_method(device_t dev,
+                           const char *guid_string);
+static ACPI_STATUS     acpi_wmi_evaluate_call_method(device_t dev,
+                           const char *guid_string, UINT8 instance,
+                           UINT32 method_id, const ACPI_BUFFER *in,
+                           ACPI_BUFFER *out);
+static ACPI_STATUS     acpi_wmi_install_event_handler_method(device_t dev,
+                           const char *guid_string, ACPI_NOTIFY_HANDLER handler,
+                           void *data);
+static ACPI_STATUS     acpi_wmi_remove_event_handler_method(device_t dev,
+                           const char *guid_string);
+static ACPI_STATUS     acpi_wmi_get_event_data_method(device_t dev,
+                           UINT32 event_id, ACPI_BUFFER *out);
+static ACPI_STATUS     acpi_wmi_get_block_method(device_t dev,
+                           const char *guid_string,
+                           UINT8 instance, ACPI_BUFFER *out);
+static ACPI_STATUS     acpi_wmi_set_block_method(device_t dev,
+                           const char *guid_string,
+                           UINT8 instance, const ACPI_BUFFER *in);
+/* private interface - declaration */
+/* callbacks */
+static void            acpi_wmi_notify_handler(ACPI_HANDLE h, UINT32 notify,
+                           void *context);
+static ACPI_STATUS     acpi_wmi_ec_handler(UINT32 function,
+                           ACPI_PHYSICAL_ADDRESS address, UINT32 width,
+                           UINT64 *value, void *context,
+                           void *region_context);
+/* helpers */
+static ACPI_STATUS     acpi_wmi_read_wdg_blocks(ACPI_HANDLE h);
+static ACPI_STATUS     acpi_wmi_toggle_we_event_generation(device_t dev,
+                           struct wmi_info *winfo,
+                           enum event_generation_state state);
+static int             acpi_wmi_guid_string_to_guid(const UINT8 *guid_string,
+                           UINT8 *guid);
+static struct wmi_info* acpi_wmi_lookup_wmi_info_by_guid_string(
+                           const char *guid_string);
+
+static d_open_t acpi_wmi_wmistat_open;
+static d_close_t acpi_wmi_wmistat_close;
+static d_read_t acpi_wmi_wmistat_read;
+
+/* handler /dev/wmistat device */
+static struct dev_ops wmistat_ops = {
+       { .name = "wmistat" },
+       .d_open = acpi_wmi_wmistat_open,
+       .d_close = acpi_wmi_wmistat_close,
+       .d_read = acpi_wmi_wmistat_read,
+};
+
+
+static device_method_t acpi_wmi_methods[] = {
+       /* Device interface */
+       DEVMETHOD(device_probe, acpi_wmi_probe),
+       DEVMETHOD(device_attach, acpi_wmi_attach),
+       DEVMETHOD(device_detach, acpi_wmi_detach),
+
+       /* bus interface */
+       DEVMETHOD(bus_add_child,        bus_generic_add_child),
+
+       /* acpi_wmi interface */
+       DEVMETHOD(acpi_wmi_provides_guid_string,
+                   acpi_wmi_provides_guid_string_method),
+       DEVMETHOD(acpi_wmi_evaluate_call, acpi_wmi_evaluate_call_method),
+       DEVMETHOD(acpi_wmi_install_event_handler,
+                   acpi_wmi_install_event_handler_method),
+       DEVMETHOD(acpi_wmi_remove_event_handler,
+                   acpi_wmi_remove_event_handler_method),
+       DEVMETHOD(acpi_wmi_get_event_data, acpi_wmi_get_event_data_method),
+       DEVMETHOD(acpi_wmi_get_block, acpi_wmi_get_block_method),
+       DEVMETHOD(acpi_wmi_set_block, acpi_wmi_set_block_method),
+
+       {0, 0}
+};
+
+static driver_t acpi_wmi_driver = {
+       "acpi_wmi",
+       acpi_wmi_methods,
+       sizeof(struct acpi_wmi_softc),
+};
+
+static devclass_t acpi_wmi_devclass;
+DRIVER_MODULE(acpi_wmi, acpi, acpi_wmi_driver, acpi_wmi_devclass, 0, 0);
+MODULE_VERSION(acpi_wmi, 1);
+MODULE_DEPEND(acpi_wmi, acpi, 1, 1, 1);
+static char *wmi_ids[] = {"PNP0C14", NULL};
+
+/*
+ * Probe for the PNP0C14 ACPI node
+ */
+static int
+acpi_wmi_probe(device_t dev)
+{
+       if (acpi_disabled("wmi") ||
+           ACPI_ID_PROBE(device_get_parent(dev), dev, wmi_ids) == NULL)
+               return (ENXIO);
+       device_set_desc(dev, "ACPI-WMI mapping");
+
+       return (0);
+}
+
+/*
+ * Attach the device by:
+ * - Looking for the first ACPI EC device
+ * - Install the notify handler
+ * - Install the EC address space handler
+ * - Look for the _WDG node and read GUID information blocks
+ */
+static int
+acpi_wmi_attach(device_t dev)
+{
+       struct acpi_wmi_softc *sc;
+       int ret;
+       ACPI_STATUS status;
+
+       ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
+       sc = device_get_softc(dev);
+       ret = ENXIO;
+
+       ACPI_SERIAL_BEGIN(acpi_wmi);
+       sc->wmi_dev = dev;
+       sc->wmi_handle = acpi_get_handle(dev);
+       TAILQ_INIT(&wmi_info_list);
+       /* XXX Only works with one EC, but nearly all systems only have one. */
+       if ((sc->ec_dev = devclass_get_device(devclass_find("acpi_ec"), 0))
+           == NULL)
+               device_printf(dev, "cannot find EC device\n");
+       else if (ACPI_FAILURE((status = AcpiInstallNotifyHandler(sc->wmi_handle,
+                   ACPI_DEVICE_NOTIFY, acpi_wmi_notify_handler, sc))))
+               device_printf(sc->wmi_dev, "couldn't install notify handler - %s\n",
+                   AcpiFormatException(status));
+       else if (ACPI_FAILURE((status = AcpiInstallAddressSpaceHandler(
+                   sc->wmi_handle, ACPI_ADR_SPACE_EC, acpi_wmi_ec_handler,
+                   NULL, sc)))) {
+               device_printf(sc->wmi_dev, "couldn't install EC handler - %s\n",
+                   AcpiFormatException(status));
+               AcpiRemoveNotifyHandler(sc->wmi_handle, ACPI_DEVICE_NOTIFY,
+                   acpi_wmi_notify_handler);
+       } else if (ACPI_FAILURE((status = acpi_wmi_read_wdg_blocks(
+                   sc->wmi_handle)))) {
+               device_printf(sc->wmi_dev, "couldn't parse _WDG - %s\n",
+                   AcpiFormatException(status));
+               AcpiRemoveNotifyHandler(sc->wmi_handle, ACPI_DEVICE_NOTIFY,
+                   acpi_wmi_notify_handler);
+               AcpiRemoveAddressSpaceHandler(sc->wmi_handle, ACPI_ADR_SPACE_EC,
+                   acpi_wmi_ec_handler);
+       } else {
+               sc->wmistat_dev_t = make_dev(&wmistat_ops, 0, UID_ROOT,
+                   GID_WHEEL, 0644, "wmistat%d", device_get_unit(dev));
+               sc->wmistat_dev_t->si_drv1 = sc;
+               sc->wmistat_open_pid = 0;
+               sc->wmistat_bufptr = -1;
+               ret = 0;
+       }
+       ACPI_SERIAL_END(acpi_wmi);
+
+       if (ret == 0) {
+               bus_generic_probe(dev);
+               ret = bus_generic_attach(dev);
+       }
+
+       return (ret);
+}
+
+/*
+ * Detach the driver by:
+ * - Removing notification handler
+ * - Removing address space handler
+ * - Turning off event generation for all WExx event activated by
+ *   child drivers
+ */
+static int
+acpi_wmi_detach(device_t dev)
+{
+       struct wmi_info *winfo, *tmp;
+       struct acpi_wmi_softc *sc;
+       int ret;
+
+       ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
+       sc = device_get_softc(dev);
+       ACPI_SERIAL_BEGIN(acpi_wmi);
+
+       if (sc->wmistat_open_pid != 0) {
+               ret = EBUSY;
+       } else {
+               AcpiRemoveNotifyHandler(sc->wmi_handle, ACPI_DEVICE_NOTIFY,
+                   acpi_wmi_notify_handler);
+               AcpiRemoveAddressSpaceHandler(sc->wmi_handle,
+                   ACPI_ADR_SPACE_EC, acpi_wmi_ec_handler);
+               TAILQ_FOREACH_MUTABLE(winfo, &wmi_info_list, wmi_list, tmp) {
+                       if (winfo->event_handler)
+                               acpi_wmi_toggle_we_event_generation(dev,
+                                   winfo, EVENT_GENERATION_OFF);
+                       TAILQ_REMOVE(&wmi_info_list, winfo, wmi_list);
+                       kfree(winfo, M_ACPIWMI);
+               }
+               if (sc->wmistat_bufptr != -1) {
+                       sbuf_delete(&sc->wmistat_sbuf);
+                       sc->wmistat_bufptr = -1;
+               }
+               sc->wmistat_open_pid = 0;
+               destroy_dev(sc->wmistat_dev_t);
+               ret = 0;
+       }
+       ACPI_SERIAL_END(acpi_wmi);
+
+       return (ret);
+}
+
+
+/*
+ * Check if the given GUID string (human readable format
+ * AABBCCDD-EEFF-GGHH-IIJJ-KKLLMMNNOOPP)
+ * exists within _WDG
+ */
+static int
+acpi_wmi_provides_guid_string_method(device_t dev, const char *guid_string)
+{
+       struct wmi_info *winfo;
+       int ret;
+
+       ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
+       ACPI_SERIAL_BEGIN(acpi_wmi);
+       winfo = acpi_wmi_lookup_wmi_info_by_guid_string(guid_string);
+       ret = (winfo == NULL)?0:winfo->ginfo.max_instance+1;
+       ACPI_SERIAL_END(acpi_wmi);
+
+       return (ret);
+}
+
+/*
+ * Call a method "method_id" on the given GUID block
+ * write result into user provided output buffer
+ */
+static ACPI_STATUS
+acpi_wmi_evaluate_call_method(device_t dev, const char *guid_string,
+    UINT8 instance, UINT32 method_id, const ACPI_BUFFER *in, ACPI_BUFFER *out)
+{
+       ACPI_OBJECT params[3];
+       ACPI_OBJECT_LIST input;
+       char method[5] = "WMxx";
+       struct wmi_info *winfo;
+       struct acpi_wmi_softc *sc;
+       ACPI_STATUS status;
+
+       ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
+
+       sc = device_get_softc(dev);
+       ACPI_SERIAL_BEGIN(acpi_wmi);
+       if ((winfo = acpi_wmi_lookup_wmi_info_by_guid_string(guid_string))
+                   == NULL)
+               status = AE_NOT_FOUND;
+       else if (!(winfo->ginfo.flags & ACPI_WMI_REGFLAG_METHOD))
+               status = AE_BAD_DATA;
+       else if (instance > winfo->ginfo.max_instance)
+               status = AE_BAD_PARAMETER;
+       else {
+               params[0].Type = ACPI_TYPE_INTEGER;
+               params[0].Integer.Value = instance;
+               params[1].Type = ACPI_TYPE_INTEGER;
+               params[1].Integer.Value = method_id;
+               input.Pointer = params;
+               input.Count = 2;
+               if (in) {
+                       params[2].Type =
+                           (winfo->ginfo.flags & ACPI_WMI_REGFLAG_STRING)
+                           ?ACPI_TYPE_STRING:ACPI_TYPE_BUFFER;
+                       params[2].Buffer.Length = in->Length;
+                       params[2].Buffer.Pointer = in->Pointer;
+                       input.Count = 3;
+               }
+               method[2] = winfo->ginfo.oid[0];
+               method[3] = winfo->ginfo.oid[1];
+               status = AcpiEvaluateObject(sc->wmi_handle, method,
+                           &input, out);
+       }
+       ACPI_SERIAL_END(acpi_wmi);
+
+       return (status);
+}
+
+/*
+ * Install a user provided event_handler on the given GUID
+ * provided *data will be passed on callback
+ * If there is already an existing event handler registered it will be silently
+ * discarded
+ */
+static ACPI_STATUS
+acpi_wmi_install_event_handler_method(device_t dev, const char *guid_string,
+    ACPI_NOTIFY_HANDLER event_handler, void *data)
+{
+       struct wmi_info *winfo;
+       ACPI_STATUS status;
+
+       ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
+
+       status = AE_OK;
+       ACPI_SERIAL_BEGIN(acpi_wmi);
+       if (guid_string == NULL || event_handler == NULL)
+               status = AE_BAD_PARAMETER;
+       else if ((winfo = acpi_wmi_lookup_wmi_info_by_guid_string(guid_string))
+                   == NULL)
+               status = AE_NOT_EXIST;
+       else if (winfo->event_handler != NULL ||
+               (status = acpi_wmi_toggle_we_event_generation(dev, winfo,
+                   EVENT_GENERATION_ON)) == AE_OK) {
+               winfo->event_handler = event_handler;
+               winfo->event_handler_user_data = data;
+       }
+       ACPI_SERIAL_END(acpi_wmi);
+
+       return (status);
+}
+
+/*
+ * Remove a previously installed event handler from the given GUID
+ * If there was none installed, this call is silently discarded and
+ * reported as AE_OK
+ */
+static ACPI_STATUS
+acpi_wmi_remove_event_handler_method(device_t dev, const char *guid_string)
+{
+       struct wmi_info *winfo;
+       ACPI_STATUS status;
+
+       ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
+
+       status = AE_OK;
+       ACPI_SERIAL_BEGIN(acpi_wmi);
+       if (guid_string &&
+           (winfo = acpi_wmi_lookup_wmi_info_by_guid_string(guid_string))
+           != NULL && winfo->event_handler) {
+               status = acpi_wmi_toggle_we_event_generation(dev, winfo,
+                           EVENT_GENERATION_OFF);
+               winfo->event_handler = NULL;
+               winfo->event_handler_user_data = NULL;
+       }
+       ACPI_SERIAL_END(acpi_wmi);
+
+       return (status);
+}
+
+/*
+ * Get details on an event received through a callback registered
+ * through ACPI_WMI_REMOVE_EVENT_HANDLER into a user provided output buffer.
+ * (event_id equals "notify" passed in the callback)
+ */
+static ACPI_STATUS
+acpi_wmi_get_event_data_method(device_t dev, UINT32 event_id, ACPI_BUFFER *out)
+{
+       ACPI_OBJECT_LIST input;
+       ACPI_OBJECT params[1];
+       struct acpi_wmi_softc *sc;
+       struct wmi_info *winfo;
+       ACPI_STATUS status;
+
+       ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
+
+       sc = device_get_softc(dev);
+       status = AE_NOT_FOUND;
+       ACPI_SERIAL_BEGIN(acpi_wmi);
+       params[0].Type = ACPI_TYPE_INTEGER;
+       params[0].Integer.Value = event_id;
+       input.Pointer = params;
+       input.Count = 1;
+       TAILQ_FOREACH(winfo, &wmi_info_list, wmi_list) {
+               if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EVENT) &&
+                   ((UINT8) winfo->ginfo.oid[0] == event_id)) {
+                       status = AcpiEvaluateObject(sc->wmi_handle, "_WED",
+                                   &input, out);
+                       break;
+               }
+       }
+       ACPI_SERIAL_END(acpi_wmi);
+
+       return (status);
+}
+
+/*
+ * Read a block of data from the given GUID (using WQxx (query))
+ * Will be returned in a user provided buffer (out).
+ * If the method is marked as expensive (ACPI_WMI_REGFLAG_EXPENSIVE)
+ * we will first call the WCxx control method to lock the node to
+ * lock the node for data collection and release it afterwards.
+ * (Failed WCxx calls are ignored to "support" broken implementations)
+ */
+static ACPI_STATUS
+acpi_wmi_get_block_method(device_t dev, const char *guid_string, UINT8 instance,
+       ACPI_BUFFER *out)
+{
+       char wc_method[5] = "WCxx";
+       char wq_method[5] = "WQxx";
+       ACPI_OBJECT_LIST wc_input;
+       ACPI_OBJECT_LIST wq_input;
+       ACPI_OBJECT wc_params[1];
+       ACPI_OBJECT wq_params[1];
+       ACPI_HANDLE wc_handle;
+       struct acpi_wmi_softc *sc;
+       struct wmi_info *winfo;
+       ACPI_STATUS status;
+       ACPI_STATUS wc_status;
+
+       ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
+
+       sc = device_get_softc(dev);
+       wc_status = AE_ERROR;
+       ACPI_SERIAL_BEGIN(acpi_wmi);
+       if (guid_string == NULL || out == NULL)
+               status = AE_BAD_PARAMETER;
+       else if ((winfo = acpi_wmi_lookup_wmi_info_by_guid_string(guid_string))
+                   == NULL)
+               status = AE_ERROR;
+       else if (instance > winfo->ginfo.max_instance)
+               status = AE_BAD_PARAMETER;
+       else if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EVENT) ||
+           (winfo->ginfo.flags & ACPI_WMI_REGFLAG_METHOD))
+               status = AE_ERROR;
+       else {
+               wq_params[0].Type = ACPI_TYPE_INTEGER;
+               wq_params[0].Integer.Value = instance;
+               wq_input.Pointer = wq_params;
+               wq_input.Count = 1;
+               if (winfo->ginfo.flags & ACPI_WMI_REGFLAG_EXPENSIVE) {
+                       wc_params[0].Type = ACPI_TYPE_INTEGER;
+                       wc_params[0].Integer.Value = 1;
+                       wc_input.Pointer = wc_params;
+                       wc_input.Count = 1;
+                       wc_method[2] = winfo->ginfo.oid[0];
+                       wc_method[3] = winfo->ginfo.oid[1];
+                       wc_status = AcpiGetHandle(sc->wmi_handle, wc_method,
+                                   &wc_handle);
+                       if (ACPI_SUCCESS(wc_status))
+                               wc_status = AcpiEvaluateObject(wc_handle,
+                                               wc_method, &wc_input, NULL);
+               }
+               wq_method[2] = winfo->ginfo.oid[0];
+               wq_method[3] = winfo->ginfo.oid[1];
+               status = AcpiEvaluateObject(sc->wmi_handle, wq_method,
+                           &wq_input, out);
+               if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EXPENSIVE)
+                   && ACPI_SUCCESS(wc_status)) {
+                       wc_params[0].Integer.Value = 0;
+                       status = AcpiEvaluateObject(wc_handle, wc_method,
+                                   &wc_input, NULL);  /* XXX this might be
+                                                        the wrong status to
+                                                        return? */
+               }
+       }
+       ACPI_SERIAL_END(acpi_wmi);
+
+       return (status);
+}
+
+/*
+ * Write a block of data to the given GUID (using WSxx)
+ */
+static ACPI_STATUS
+acpi_wmi_set_block_method(device_t dev, const char *guid_string, UINT8 instance,
+       const ACPI_BUFFER *in)
+{
+       char method[5] = "WSxx";
+       ACPI_OBJECT_LIST input;
+       ACPI_OBJECT params[2];
+       struct wmi_info *winfo;
+       struct acpi_wmi_softc *sc;
+       ACPI_STATUS status;
+
+       ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
+
+       sc = device_get_softc(dev);
+       ACPI_SERIAL_BEGIN(acpi_wmi);
+       if (guid_string == NULL || in == NULL)
+               status = AE_BAD_DATA;
+       else if ((winfo = acpi_wmi_lookup_wmi_info_by_guid_string(guid_string))
+                   == NULL)
+               status = AE_ERROR;
+       else if (instance > winfo->ginfo.max_instance)
+               status = AE_BAD_PARAMETER;
+       else if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EVENT) ||
+                   (winfo->ginfo.flags & ACPI_WMI_REGFLAG_METHOD))
+               status = AE_ERROR;
+       else {
+               params[0].Type = ACPI_TYPE_INTEGER;
+               params[0].Integer.Value = instance;
+               input.Pointer = params;
+               input.Count = 2;
+               params[1].Type = (winfo->ginfo.flags & ACPI_WMI_REGFLAG_STRING)
+                   ?ACPI_TYPE_STRING:ACPI_TYPE_BUFFER;
+               params[1].Buffer.Length = in->Length;
+               params[1].Buffer.Pointer = in->Pointer;
+               method[2] = winfo->ginfo.oid[0];
+               method[3] = winfo->ginfo.oid[1];
+               status = AcpiEvaluateObject(sc->wmi_handle, method,
+                           &input, NULL);
+       }
+       ACPI_SERIAL_END(acpi_wmi);
+
+       return (status);
+}
+
+/*
+ * Handle events received and dispatch them to
+ * stakeholders that registered through ACPI_WMI_INSTALL_EVENT_HANDLER
+ */
+static void
+acpi_wmi_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
+{
+       ACPI_NOTIFY_HANDLER handler;
+       void *handler_data;
+       struct wmi_info *winfo;
+
+       ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
+
+       handler = NULL;
+       handler_data = NULL;
+       ACPI_SERIAL_BEGIN(acpi_wmi);
+       TAILQ_FOREACH(winfo, &wmi_info_list, wmi_list) {
+               if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EVENT) &&
+                               ((UINT8) winfo->ginfo.oid[0] == notify)) {
+                       if (winfo->event_handler) {
+                               handler = winfo->event_handler;
+                               handler_data = winfo->event_handler_user_data;
+                               break;
+                       }
+               }
+       }
+       ACPI_SERIAL_END(acpi_wmi);
+       if (handler) {
+               handler(h, notify, handler_data);
+       }
+}
+
+/*
+ * Handle EC address space notifications reveived on the WDG node
+ * (this mimics EcAddressSpaceHandler in acpi_ec.c)
+ */
+static ACPI_STATUS
+acpi_wmi_ec_handler(UINT32 function, ACPI_PHYSICAL_ADDRESS address,
+    UINT32 width, UINT64 *value, void *context,
+    void *region_context)
+{
+       struct acpi_wmi_softc *sc;
+       int i;
+       UINT64 ec_data;
+       UINT8 ec_addr;
+       ACPI_STATUS status;
+
+       ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)address);
+
+       sc = (struct acpi_wmi_softc *)context;
+       if (width % 8 != 0 || value == NULL || context == NULL)
+               return (AE_BAD_PARAMETER);
+       if (address + (width / 8) - 1 > 0xFF)
+               return (AE_BAD_ADDRESS);
+       if (function == ACPI_READ)
+               *value = 0;
+       ec_addr = address;
+       status = AE_ERROR;
+
+       for (i = 0; i < width; i += 8, ++ec_addr) {
+               switch (function) {
+               case ACPI_READ:
+                       status = ACPI_EC_READ(sc->ec_dev, ec_addr, &ec_data, 1);
+                       if (ACPI_SUCCESS(status))
+                               *value |= ((UINT64)ec_data) << i;
+               break;
+               case ACPI_WRITE:
+                       ec_data = (UINT8)((*value) >> i);
+                       status = ACPI_EC_WRITE(sc->ec_dev, ec_addr, ec_data, 1);
+                       break;
+               default:
+                       device_printf(sc->wmi_dev,
+                           "invalid acpi_wmi_ec_handler function %d\n",
+                           function);
+                       status = AE_BAD_PARAMETER;
+                       break;
+               }
+               if (ACPI_FAILURE(status))
+                       break;
+       }
+
+       return (status);
+}
+
+/*
+ * Read GUID blocks from the _WDG node
+ * into wmi_info_list.
+ */
+static ACPI_STATUS
+acpi_wmi_read_wdg_blocks(ACPI_HANDLE h)
+{
+       ACPI_BUFFER out = {ACPI_ALLOCATE_BUFFER, NULL};
+       struct guid_info *ginfo;
+       ACPI_OBJECT *obj;
+       struct wmi_info *winfo;
+       UINT32 i;
+       UINT32 wdg_block_count;
+       ACPI_STATUS status;
+
+       ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
+
+       ACPI_SERIAL_ASSERT(acpi_wmi);
+       if (ACPI_FAILURE(status = AcpiEvaluateObject(h, "_WDG", NULL, &out)))
+               return (status);
+       obj = (ACPI_OBJECT*) out.Pointer;
+       wdg_block_count = obj->Buffer.Length / sizeof(struct guid_info);
+       if ((ginfo = kmalloc(obj->Buffer.Length, M_ACPIWMI, M_NOWAIT))
+                   == NULL) {
+               AcpiOsFree(out.Pointer);
+               return (AE_NO_MEMORY);
+       }
+       memcpy(ginfo, obj->Buffer.Pointer, obj->Buffer.Length);
+       for (i = 0; i < wdg_block_count; ++i) {
+               if ((winfo = kmalloc(sizeof(struct wmi_info), M_ACPIWMI,
+                           M_NOWAIT | M_ZERO)) == NULL) {
+                       AcpiOsFree(out.Pointer);
+                       kfree(ginfo, M_ACPIWMI);
+                       return (AE_NO_MEMORY);
+               }
+               winfo->ginfo = ginfo[i];
+               TAILQ_INSERT_TAIL(&wmi_info_list, winfo, wmi_list);
+       }
+       AcpiOsFree(out.Pointer);
+       kfree(ginfo, M_ACPIWMI);
+
+       return (status);
+}
+
+/*
+ * Toggle event generation in for the given GUID (passed by winfo)
+ * Turn on to get notified (through acpi_wmi_notify_handler) if events happen
+ * on the given GUID.
+ */
+static ACPI_STATUS
+acpi_wmi_toggle_we_event_generation(device_t dev, struct wmi_info *winfo,
+    enum event_generation_state state)
+{
+       char method[5] = "WExx";
+       ACPI_OBJECT_LIST input;
+       ACPI_OBJECT params[1];
+       struct acpi_wmi_softc *sc;
+       ACPI_STATUS status;
+
+       ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
+
+       sc = device_get_softc(dev);
+       ACPI_SERIAL_ASSERT(acpi_wmi);
+       params[0].Type = ACPI_TYPE_INTEGER;
+       params[0].Integer.Value = state==EVENT_GENERATION_ON?1:0;
+       input.Pointer = params;
+       input.Count = 1;
+
+       UINT8 hi = ((UINT8) winfo->ginfo.oid[0]) >> 4;
+       UINT8 lo = ((UINT8) winfo->ginfo.oid[0]) & 0xf;
+       method[2] = (hi > 9 ? hi + 55: hi + 48);
+       method[3] = (lo > 9 ? lo + 55: lo + 48);
+       status = AcpiEvaluateObject(sc->wmi_handle, method, &input, NULL);
+       if (status == AE_NOT_FOUND) status = AE_OK;
+
+       return (status);
+}
+
+/*
+ * Convert given two digit hex string (hexin) to an UINT8 referenced
+ * by byteout.
+ * Return != 0 if the was a problem (invalid input)
+ */
+static __inline int acpi_wmi_hex_to_int(const UINT8 *hexin, UINT8 *byteout)
+{
+       unsigned int hi;
+       unsigned int lo;
+
+       hi = hexin[0];
+       lo = hexin[1];
+       if ('0' <= hi && hi <= '9')
+               hi -= '0';
+       else if ('A' <= hi && hi <= 'F')
+               hi -= ('A' - 10);
+       else if ('a' <= hi && hi <= 'f')
+               hi -= ('a' - 10);
+       else
+               return (1);
+       if ('0' <= lo && lo <= '9')
+               lo -= '0';
+       else if ('A' <= lo && lo <= 'F')
+               lo -= ('A' - 10);
+       else if ('a' <= lo && lo <= 'f')
+               lo -= ('a' - 10);
+       else
+               return (1);
+       *byteout = (hi << 4) + lo;
+
+       return (0);
+}
+
+/*
+ * Convert a human readable 36 character GUID into a 16byte
+ * machine readable one.
+ * The basic algorithm looks as follows:
+ * Input:  AABBCCDD-EEFF-GGHH-IIJJ-KKLLMMNNOOPP
+ * Output: DCBAFEHGIJKLMNOP
+ * (AA BB CC etc. represent two digit hex numbers == bytes)
+ * Return != 0 if passed guid string is invalid
+ */
+static int
+acpi_wmi_guid_string_to_guid(const UINT8 *guid_string, UINT8 *guid)
+{
+       static const int mapping[20] = {3, 2, 1, 0, -1, 5, 4, -1, 7, 6, -1,
+           8, 9, -1, 10, 11, 12, 13, 14, 15};
+       int i;
+
+       for (i = 0; i < 20; ++i, ++guid_string) {
+               if (mapping[i] >= 0) {
+                       if (acpi_wmi_hex_to_int(guid_string,
+                           &guid[mapping[i]]))
+                               return (-1);
+                       ++guid_string;
+               } else if (*guid_string != '-')
+                       return (-1);
+       }
+
+       return (0);
+}
+
+/*
+ * Lookup a wmi_info structure in wmi_list based on a
+ * human readable GUID
+ * Return NULL if the GUID is unknown in the _WDG
+ */
+static struct wmi_info*
+acpi_wmi_lookup_wmi_info_by_guid_string(const char *guid_string)
+{
+       char guid[16];
+       struct wmi_info *winfo;
+
+       ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
+
+       ACPI_SERIAL_ASSERT(acpi_wmi);
+
+       if (!acpi_wmi_guid_string_to_guid(guid_string, guid)) {
+               TAILQ_FOREACH(winfo, &wmi_info_list, wmi_list) {
+                       if (!memcmp(winfo->ginfo.guid, guid, 16)) {
+                               return (winfo);
+                       }
+               }
+       }
+
+       return (NULL);
+}
+
+/*
+ * open wmistat device
+ */
+static int
+acpi_wmi_wmistat_open(struct dev_open_args *ap)
+{
+       struct cdev *dev = ap->a_head.a_dev;
+       struct thread *td = curthread;
+       struct acpi_wmi_softc *sc;
+       int ret;
+
+       if (dev == NULL || dev->si_drv1 == NULL)
+               return (EBADF);
+       sc = dev->si_drv1;
+
+       ACPI_SERIAL_BEGIN(acpi_wmi);
+       if (sc->wmistat_open_pid != 0) {
+               ret = EBUSY;
+       }
+       else {
+               if (sbuf_new(&sc->wmistat_sbuf, NULL, 4096, SBUF_AUTOEXTEND)
+                           == NULL) {
+                       ret = ENXIO;
+               } else {
+                       sc->wmistat_open_pid = td->td_proc->p_pid;
+                       sc->wmistat_bufptr = 0;
+                       ret = 0;
+               }
+       }
+       ACPI_SERIAL_END(acpi_wmi);
+
+       return (ret);
+}
+
+/*
+ * close wmistat device
+ */
+static int
+acpi_wmi_wmistat_close(struct dev_close_args *ap)
+{
+       struct cdev *dev = ap->a_head.a_dev;
+       struct acpi_wmi_softc *sc;
+       int ret;
+
+       if (dev == NULL || dev->si_drv1 == NULL)
+               return (EBADF);
+       sc = dev->si_drv1;
+
+       ACPI_SERIAL_BEGIN(acpi_wmi);
+       if (sc->wmistat_open_pid == 0) {
+               ret = EBADF;
+       }
+       else {
+               if (sc->wmistat_bufptr != -1) {
+                       sbuf_delete(&sc->wmistat_sbuf);
+                       sc->wmistat_bufptr = -1;
+               }
+               sc->wmistat_open_pid = 0;
+               ret = 0;
+       }
+       ACPI_SERIAL_END(acpi_wmi);
+
+       return (ret);
+}
+
+/*
+ * Read from wmistat guid information
+ */
+static int
+acpi_wmi_wmistat_read(struct dev_read_args *ap)
+{
+       struct cdev *dev = ap->a_head.a_dev;
+       struct uio *buf = ap->a_uio;
+       struct acpi_wmi_softc *sc;
+       struct wmi_info *winfo;
+       int l;
+       int ret;
+       UINT8* guid;
+
+       if (dev == NULL || dev->si_drv1 == NULL)
+               return (EBADF);
+       sc = dev->si_drv1;
+
+       ACPI_SERIAL_BEGIN(acpi_wmi);
+       if (sc->wmistat_open_pid != buf->uio_td->td_proc->p_pid ||
+                       sc->wmistat_bufptr == -1) {
+               ret = EBADF;
+       }
+       else {
+               if (!sbuf_done(&sc->wmistat_sbuf)) {
+                       sbuf_printf(&sc->wmistat_sbuf, "GUID                 "
+                                   "                 INST EXPE METH STR "
+                                   "EVENT OID\n");
+                       TAILQ_FOREACH(winfo, &wmi_info_list, wmi_list) {
+                               guid = (UINT8*)winfo->ginfo.guid;
+                               sbuf_printf(&sc->wmistat_sbuf,
+                                           "{%02X%02X%02X%02X-%02X%02X-"
+                                           "%02X%02X-%02X%02X-%02X%02X"
+                                           "%02X%02X%02X%02X} %3d %-5s",
+                                       guid[3], guid[2], guid[1], guid[0],
+                                       guid[5], guid[4],
+                                       guid[7], guid[6],
+                                       guid[8], guid[9],
+                                       guid[10], guid[11], guid[12],
+                                       guid[13], guid[14], guid[15],
+                                       winfo->ginfo.max_instance,
+                                       (winfo->ginfo.flags&
+                                               ACPI_WMI_REGFLAG_EXPENSIVE)?
+                                               "YES":"NO"
+                                       );
+                               if (winfo->ginfo.flags&ACPI_WMI_REGFLAG_METHOD)
+                                       sbuf_printf(&sc->wmistat_sbuf,
+                                                   "WM%c%c ",
+                                                   winfo->ginfo.oid[0],
+                                                   winfo->ginfo.oid[1]);
+                               else
+                                       sbuf_printf(&sc->wmistat_sbuf, "NO   ");
+                               sbuf_printf(&sc->wmistat_sbuf, "%-4s",
+                                           (winfo->ginfo.flags&
+                                           ACPI_WMI_REGFLAG_STRING)?"YES":"NO"
+                                       );
+                               if (winfo->ginfo.flags&ACPI_WMI_REGFLAG_EVENT)
+                                       sbuf_printf(&sc->wmistat_sbuf,
+                                                   "0x%02X%s -\n",
+                                                   (UINT8)winfo->ginfo.oid[0],
+                                                   winfo->event_handler==NULL?
+                                                   " ":"+");
+                               else
+                                       sbuf_printf(&sc->wmistat_sbuf,
+                                                   "NO    %c%c\n",
+                                                   winfo->ginfo.oid[0],
+                                                   winfo->ginfo.oid[1]);
+                       }
+                       sbuf_finish(&sc->wmistat_sbuf);
+               }
+               if (sbuf_len(&sc->wmistat_sbuf) <= 0) {
+                       sbuf_delete(&sc->wmistat_sbuf);
+                       sc->wmistat_bufptr = -1;
+                       sc->wmistat_open_pid = 0;
+                       ret = ENOMEM;
+               } else {
+                       l = min(buf->uio_resid, sbuf_len(&sc->wmistat_sbuf) -
+                                   sc->wmistat_bufptr);
+                       ret = (l > 0)?uiomove(sbuf_data(&sc->wmistat_sbuf) +
+                                   sc->wmistat_bufptr, l, buf) : 0;
+                       sc->wmistat_bufptr += l;
+               }
+       }
+       ACPI_SERIAL_END(acpi_wmi);
+
+       return (ret);
+}
index 5924920..9f95ec4 100644 (file)
@@ -28,7 +28,7 @@
 
 #include <sys/bus.h>
 #include <sys/types.h>
-#include <contrib/dev/acpica/include/acpi.h>
+#include "acpi.h"
 
 INTERFACE acpi_wmi;