Home | History | Annotate | Line # | Download | only in wmi
wmi_acpi.c revision 1.8
      1  1.8  jmcneill /*	$NetBSD: wmi_acpi.c,v 1.8 2010/10/24 15:07:20 jmcneill Exp $	*/
      2  1.1    jruoho 
      3  1.1    jruoho /*-
      4  1.1    jruoho  * Copyright (c) 2009, 2010 Jukka Ruohonen <jruohonen (at) iki.fi>
      5  1.1    jruoho  * All rights reserved.
      6  1.1    jruoho  *
      7  1.1    jruoho  * Redistribution and use in source and binary forms, with or without
      8  1.1    jruoho  * modification, are permitted provided that the following conditions
      9  1.1    jruoho  * are met:
     10  1.1    jruoho  *
     11  1.1    jruoho  * 1. Redistributions of source code must retain the above copyright
     12  1.1    jruoho  *    notice, this list of conditions and the following disclaimer.
     13  1.1    jruoho  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1    jruoho  *    notice, this list of conditions and the following disclaimer in the
     15  1.1    jruoho  *    documentation and/or other materials provided with the distribution.
     16  1.1    jruoho  *
     17  1.1    jruoho  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  1.1    jruoho  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  1.1    jruoho  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  1.1    jruoho  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  1.1    jruoho  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.1    jruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  1.1    jruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  1.1    jruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  1.1    jruoho  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  1.1    jruoho  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  1.1    jruoho  * SUCH DAMAGE.
     28  1.1    jruoho  */
     29  1.1    jruoho #include <sys/cdefs.h>
     30  1.8  jmcneill __KERNEL_RCSID(0, "$NetBSD: wmi_acpi.c,v 1.8 2010/10/24 15:07:20 jmcneill Exp $");
     31  1.1    jruoho 
     32  1.1    jruoho #include <sys/param.h>
     33  1.1    jruoho #include <sys/device.h>
     34  1.1    jruoho #include <sys/endian.h>
     35  1.1    jruoho #include <sys/kmem.h>
     36  1.1    jruoho #include <sys/systm.h>
     37  1.1    jruoho 
     38  1.1    jruoho #include <dev/acpi/acpireg.h>
     39  1.1    jruoho #include <dev/acpi/acpivar.h>
     40  1.1    jruoho #include <dev/acpi/wmi/wmi_acpivar.h>
     41  1.1    jruoho 
     42  1.1    jruoho #define _COMPONENT          ACPI_RESOURCE_COMPONENT
     43  1.1    jruoho ACPI_MODULE_NAME            ("wmi_acpi")
     44  1.1    jruoho 
     45  1.1    jruoho /*
     46  1.1    jruoho  * This implements something called "Microsoft Windows Management
     47  1.1    jruoho  * Instrumentation" (WMI). This subset of ACPI is desribed in:
     48  1.1    jruoho  *
     49  1.1    jruoho  * http://www.microsoft.com/whdc/system/pnppwr/wmi/wmi-acpi.mspx
     50  1.1    jruoho  *
     51  1.1    jruoho  * (Obtained on Thu Feb 12 18:21:44 EET 2009.)
     52  1.1    jruoho  */
     53  1.1    jruoho 
     54  1.1    jruoho static int         acpi_wmi_match(device_t, cfdata_t, void *);
     55  1.1    jruoho static void        acpi_wmi_attach(device_t, device_t, void *);
     56  1.1    jruoho static int         acpi_wmi_detach(device_t, int);
     57  1.8  jmcneill static int         acpi_wmi_rescan(device_t, const char *, const int *);
     58  1.8  jmcneill static void        acpi_wmi_childdet(device_t, device_t);
     59  1.1    jruoho static int         acpi_wmi_print(void *, const char *);
     60  1.1    jruoho static bool        acpi_wmi_init(struct acpi_wmi_softc *);
     61  1.1    jruoho static bool        acpi_wmi_add(struct acpi_wmi_softc *, ACPI_OBJECT *);
     62  1.1    jruoho static void        acpi_wmi_del(struct acpi_wmi_softc *);
     63  1.7    jruoho static void	   acpi_wmi_dump(struct acpi_wmi_softc *);
     64  1.1    jruoho 
     65  1.1    jruoho static ACPI_STATUS acpi_wmi_guid_get(struct acpi_wmi_softc *,
     66  1.1    jruoho                                      const char *, struct wmi_t **);
     67  1.4    jruoho static void	   acpi_wmi_event_add(struct acpi_wmi_softc *);
     68  1.4    jruoho static void	   acpi_wmi_event_del(struct acpi_wmi_softc *);
     69  1.1    jruoho static void        acpi_wmi_event_handler(ACPI_HANDLE, uint32_t, void *);
     70  1.1    jruoho static bool        acpi_wmi_suspend(device_t, const pmf_qual_t *);
     71  1.1    jruoho static bool        acpi_wmi_resume(device_t, const pmf_qual_t *);
     72  1.1    jruoho static ACPI_STATUS acpi_wmi_enable(ACPI_HANDLE, const char *, bool, bool);
     73  1.1    jruoho static bool        acpi_wmi_input(struct wmi_t *, uint8_t, uint8_t);
     74  1.1    jruoho 
     75  1.1    jruoho const char * const acpi_wmi_ids[] = {
     76  1.1    jruoho 	"PNP0C14",
     77  1.6    jruoho 	"pnp0c14",
     78  1.1    jruoho 	NULL
     79  1.1    jruoho };
     80  1.1    jruoho 
     81  1.8  jmcneill CFATTACH_DECL2_NEW(acpiwmi, sizeof(struct acpi_wmi_softc),
     82  1.8  jmcneill     acpi_wmi_match, acpi_wmi_attach, acpi_wmi_detach, NULL,
     83  1.8  jmcneill     acpi_wmi_rescan, acpi_wmi_childdet);
     84  1.1    jruoho 
     85  1.1    jruoho static int
     86  1.1    jruoho acpi_wmi_match(device_t parent, cfdata_t match, void *aux)
     87  1.1    jruoho {
     88  1.1    jruoho 	struct acpi_attach_args *aa = aux;
     89  1.1    jruoho 
     90  1.1    jruoho 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
     91  1.1    jruoho 		return 0;
     92  1.1    jruoho 
     93  1.1    jruoho 	return acpi_match_hid(aa->aa_node->ad_devinfo, acpi_wmi_ids);
     94  1.1    jruoho }
     95  1.1    jruoho 
     96  1.1    jruoho static void
     97  1.1    jruoho acpi_wmi_attach(device_t parent, device_t self, void *aux)
     98  1.1    jruoho {
     99  1.1    jruoho 	struct acpi_wmi_softc *sc = device_private(self);
    100  1.1    jruoho 	struct acpi_attach_args *aa = aux;
    101  1.1    jruoho 
    102  1.1    jruoho 	sc->sc_dev = self;
    103  1.1    jruoho 	sc->sc_node = aa->aa_node;
    104  1.1    jruoho 
    105  1.1    jruoho 	sc->sc_child = NULL;
    106  1.1    jruoho 	sc->sc_handler = NULL;
    107  1.1    jruoho 
    108  1.1    jruoho 	aprint_naive("\n");
    109  1.1    jruoho 	aprint_normal(": ACPI WMI Interface\n");
    110  1.1    jruoho 
    111  1.1    jruoho 	if (acpi_wmi_init(sc) != true)
    112  1.1    jruoho 		return;
    113  1.1    jruoho 
    114  1.7    jruoho 	acpi_wmi_dump(sc);
    115  1.4    jruoho 	acpi_wmi_event_add(sc);
    116  1.1    jruoho 
    117  1.8  jmcneill 	acpi_wmi_rescan(self, NULL, NULL);
    118  1.4    jruoho 
    119  1.4    jruoho 	(void)pmf_device_register(self, acpi_wmi_suspend, acpi_wmi_resume);
    120  1.1    jruoho }
    121  1.1    jruoho 
    122  1.1    jruoho static int
    123  1.1    jruoho acpi_wmi_detach(device_t self, int flags)
    124  1.1    jruoho {
    125  1.1    jruoho 	struct acpi_wmi_softc *sc = device_private(self);
    126  1.1    jruoho 
    127  1.4    jruoho 	acpi_wmi_event_del(sc);
    128  1.1    jruoho 
    129  1.1    jruoho 	if (sc->sc_child != NULL)
    130  1.1    jruoho 		(void)config_detach(sc->sc_child, flags);
    131  1.1    jruoho 
    132  1.1    jruoho 	acpi_wmi_del(sc);
    133  1.1    jruoho 	pmf_device_deregister(self);
    134  1.1    jruoho 
    135  1.1    jruoho 	return 0;
    136  1.1    jruoho }
    137  1.1    jruoho 
    138  1.1    jruoho static int
    139  1.8  jmcneill acpi_wmi_rescan(device_t self, const char *ifattr, const int *locators)
    140  1.8  jmcneill {
    141  1.8  jmcneill 	struct acpi_wmi_softc *sc = device_private(self);
    142  1.8  jmcneill 
    143  1.8  jmcneill 	if (ifattr_match(ifattr, "acpiwmibus") && sc->sc_child == NULL)
    144  1.8  jmcneill 		sc->sc_child = config_found_ia(self, "acpiwmibus",
    145  1.8  jmcneill 		    NULL, acpi_wmi_print);
    146  1.8  jmcneill 
    147  1.8  jmcneill 	return 0;
    148  1.8  jmcneill }
    149  1.8  jmcneill 
    150  1.8  jmcneill static void
    151  1.8  jmcneill acpi_wmi_childdet(device_t self, device_t child)
    152  1.8  jmcneill {
    153  1.8  jmcneill 	struct acpi_wmi_softc *sc = device_private(self);
    154  1.8  jmcneill 
    155  1.8  jmcneill 	if (sc->sc_child == child)
    156  1.8  jmcneill 		sc->sc_child = NULL;
    157  1.8  jmcneill }
    158  1.8  jmcneill 
    159  1.8  jmcneill static int
    160  1.1    jruoho acpi_wmi_print(void *aux, const char *pnp)
    161  1.1    jruoho {
    162  1.1    jruoho 
    163  1.1    jruoho 	if (pnp != NULL)
    164  1.1    jruoho 		aprint_normal("acpiwmibus at %s", pnp);
    165  1.1    jruoho 
    166  1.1    jruoho 	return UNCONF;
    167  1.1    jruoho }
    168  1.1    jruoho 
    169  1.1    jruoho static bool
    170  1.1    jruoho acpi_wmi_init(struct acpi_wmi_softc *sc)
    171  1.1    jruoho {
    172  1.1    jruoho 	ACPI_OBJECT *obj;
    173  1.1    jruoho 	ACPI_BUFFER buf;
    174  1.1    jruoho 	ACPI_STATUS rv;
    175  1.1    jruoho 	uint32_t len;
    176  1.1    jruoho 
    177  1.1    jruoho 	rv = acpi_eval_struct(sc->sc_node->ad_handle, "_WDG", &buf);
    178  1.1    jruoho 
    179  1.1    jruoho 	if (ACPI_FAILURE(rv))
    180  1.1    jruoho 		goto fail;
    181  1.1    jruoho 
    182  1.1    jruoho 	obj = buf.Pointer;
    183  1.1    jruoho 
    184  1.1    jruoho 	if (obj->Type != ACPI_TYPE_BUFFER) {
    185  1.1    jruoho 		rv = AE_TYPE;
    186  1.1    jruoho 		goto fail;
    187  1.1    jruoho 	}
    188  1.1    jruoho 
    189  1.1    jruoho 	len = obj->Buffer.Length;
    190  1.1    jruoho 
    191  1.1    jruoho 	if (len != obj->Package.Count) {
    192  1.1    jruoho 		rv = AE_BAD_VALUE;
    193  1.1    jruoho 		goto fail;
    194  1.1    jruoho 	}
    195  1.1    jruoho 
    196  1.1    jruoho 	CTASSERT(sizeof(struct guid_t) == 20);
    197  1.1    jruoho 
    198  1.1    jruoho 	if (len < sizeof(struct guid_t) ||
    199  1.1    jruoho 	    len % sizeof(struct guid_t) != 0) {
    200  1.1    jruoho 		rv = AE_BAD_DATA;
    201  1.1    jruoho 		goto fail;
    202  1.1    jruoho 	}
    203  1.1    jruoho 
    204  1.1    jruoho 	return acpi_wmi_add(sc, obj);
    205  1.1    jruoho 
    206  1.1    jruoho fail:
    207  1.1    jruoho 	aprint_error_dev(sc->sc_dev, "failed to evaluate _WDG: %s\n",
    208  1.1    jruoho 	    AcpiFormatException(rv));
    209  1.1    jruoho 
    210  1.1    jruoho 	if (buf.Pointer != NULL)
    211  1.1    jruoho 		ACPI_FREE(buf.Pointer);
    212  1.1    jruoho 
    213  1.1    jruoho 	return false;
    214  1.1    jruoho }
    215  1.1    jruoho 
    216  1.1    jruoho static bool
    217  1.1    jruoho acpi_wmi_add(struct acpi_wmi_softc *sc, ACPI_OBJECT *obj)
    218  1.1    jruoho {
    219  1.1    jruoho 	struct wmi_t *wmi;
    220  1.1    jruoho 	size_t i, n, offset, siz;
    221  1.1    jruoho 
    222  1.1    jruoho 	siz = sizeof(struct guid_t);
    223  1.1    jruoho 	n = obj->Buffer.Length / siz;
    224  1.1    jruoho 
    225  1.1    jruoho 	SIMPLEQ_INIT(&sc->wmi_head);
    226  1.1    jruoho 
    227  1.1    jruoho 	for (i = offset = 0; i < n; ++i) {
    228  1.1    jruoho 
    229  1.1    jruoho 		if ((wmi = kmem_zalloc(sizeof(*wmi), KM_NOSLEEP)) == NULL)
    230  1.1    jruoho 			goto fail;
    231  1.1    jruoho 
    232  1.1    jruoho 		(void)memcpy(&wmi->guid, obj->Buffer.Pointer + offset, siz);
    233  1.1    jruoho 
    234  1.1    jruoho 		wmi->eevent = false;
    235  1.1    jruoho 		offset = offset + siz;
    236  1.1    jruoho 
    237  1.1    jruoho 		SIMPLEQ_INSERT_TAIL(&sc->wmi_head, wmi, wmi_link);
    238  1.1    jruoho 	}
    239  1.1    jruoho 
    240  1.1    jruoho 	ACPI_FREE(obj);
    241  1.1    jruoho 
    242  1.1    jruoho 	return true;
    243  1.1    jruoho 
    244  1.1    jruoho fail:
    245  1.1    jruoho 	ACPI_FREE(obj);
    246  1.1    jruoho 	acpi_wmi_del(sc);
    247  1.1    jruoho 
    248  1.1    jruoho 	return false;
    249  1.1    jruoho }
    250  1.1    jruoho 
    251  1.1    jruoho static void
    252  1.1    jruoho acpi_wmi_del(struct acpi_wmi_softc *sc)
    253  1.1    jruoho {
    254  1.1    jruoho 	struct wmi_t *wmi;
    255  1.1    jruoho 
    256  1.2    jruoho 	if (SIMPLEQ_EMPTY(&sc->wmi_head) != 0)
    257  1.1    jruoho 		return;
    258  1.1    jruoho 
    259  1.1    jruoho 	while (SIMPLEQ_FIRST(&sc->wmi_head) != NULL) {
    260  1.1    jruoho 
    261  1.1    jruoho 		wmi = SIMPLEQ_FIRST(&sc->wmi_head);
    262  1.1    jruoho 		SIMPLEQ_REMOVE_HEAD(&sc->wmi_head, wmi_link);
    263  1.1    jruoho 
    264  1.1    jruoho 		KASSERT(wmi != NULL);
    265  1.1    jruoho 
    266  1.1    jruoho 		kmem_free(wmi, sizeof(*wmi));
    267  1.1    jruoho 	}
    268  1.1    jruoho }
    269  1.1    jruoho 
    270  1.7    jruoho static void
    271  1.7    jruoho acpi_wmi_dump(struct acpi_wmi_softc *sc)
    272  1.7    jruoho {
    273  1.7    jruoho 	struct wmi_t *wmi;
    274  1.7    jruoho 
    275  1.7    jruoho 	KASSERT(SIMPLEQ_EMPTY(&sc->wmi_head) == 0);
    276  1.7    jruoho 
    277  1.7    jruoho 	SIMPLEQ_FOREACH(wmi, &sc->wmi_head, wmi_link) {
    278  1.7    jruoho 
    279  1.7    jruoho 		aprint_debug_dev(sc->sc_dev, "{%08X-%04X-%04X-",
    280  1.7    jruoho 		    wmi->guid.data1, wmi->guid.data2, wmi->guid.data3);
    281  1.7    jruoho 
    282  1.7    jruoho 		aprint_debug("%02X%02X-%02X%02X%02X%02X%02X%02X} ",
    283  1.7    jruoho 		    wmi->guid.data4[0], wmi->guid.data4[1],
    284  1.7    jruoho 		    wmi->guid.data4[2], wmi->guid.data4[3],
    285  1.7    jruoho 		    wmi->guid.data4[4], wmi->guid.data4[5],
    286  1.7    jruoho 		    wmi->guid.data4[6], wmi->guid.data4[7]);
    287  1.7    jruoho 
    288  1.7    jruoho 		aprint_debug("oid %04X count %02X flags %02X\n",
    289  1.7    jruoho 		    UGET16(wmi->guid.oid), wmi->guid.count, wmi->guid.flags);
    290  1.7    jruoho 	}
    291  1.7    jruoho }
    292  1.7    jruoho 
    293  1.1    jruoho static ACPI_STATUS
    294  1.1    jruoho acpi_wmi_guid_get(struct acpi_wmi_softc *sc,
    295  1.1    jruoho     const char *src, struct wmi_t **out)
    296  1.1    jruoho {
    297  1.1    jruoho 	struct wmi_t *wmi;
    298  1.1    jruoho 	struct guid_t *guid;
    299  1.1    jruoho 	char bin[16];
    300  1.1    jruoho 	char hex[2];
    301  1.1    jruoho 	const char *ptr;
    302  1.1    jruoho 	uint8_t i;
    303  1.1    jruoho 
    304  1.1    jruoho 	if (sc == NULL || src == NULL || strlen(src) != 36)
    305  1.1    jruoho 		return AE_BAD_PARAMETER;
    306  1.1    jruoho 
    307  1.1    jruoho 	for (ptr = src, i = 0; i < 16; i++) {
    308  1.1    jruoho 
    309  1.1    jruoho 		if (*ptr == '-')
    310  1.1    jruoho 			ptr++;
    311  1.1    jruoho 
    312  1.1    jruoho 		(void)memcpy(hex, ptr, 2);
    313  1.1    jruoho 
    314  1.2    jruoho 		if (HEXCHAR(hex[0]) == 0 || HEXCHAR(hex[1]) == 0)
    315  1.1    jruoho 			return AE_BAD_HEX_CONSTANT;
    316  1.1    jruoho 
    317  1.1    jruoho 		bin[i] = strtoul(hex, NULL, 16) & 0xFF;
    318  1.1    jruoho 
    319  1.1    jruoho 		ptr++;
    320  1.1    jruoho 		ptr++;
    321  1.1    jruoho 	}
    322  1.1    jruoho 
    323  1.1    jruoho 	guid = (struct guid_t *)bin;
    324  1.1    jruoho 	guid->data1 = be32toh(guid->data1);
    325  1.1    jruoho 	guid->data2 = be16toh(guid->data2);
    326  1.1    jruoho 	guid->data3 = be16toh(guid->data3);
    327  1.1    jruoho 
    328  1.1    jruoho 	SIMPLEQ_FOREACH(wmi, &sc->wmi_head, wmi_link) {
    329  1.1    jruoho 
    330  1.1    jruoho 		if (GUIDCMP(guid, &wmi->guid) != 0) {
    331  1.1    jruoho 
    332  1.1    jruoho 			if (out != NULL)
    333  1.1    jruoho 				*out = wmi;
    334  1.1    jruoho 
    335  1.1    jruoho 			return AE_OK;
    336  1.1    jruoho 		}
    337  1.1    jruoho 	}
    338  1.1    jruoho 
    339  1.1    jruoho 	return AE_NOT_FOUND;
    340  1.1    jruoho }
    341  1.1    jruoho 
    342  1.1    jruoho /*
    343  1.1    jruoho  * Checks if a GUID is present. Child devices
    344  1.1    jruoho  * can use this in their autoconf(9) routines.
    345  1.1    jruoho  */
    346  1.1    jruoho int
    347  1.1    jruoho acpi_wmi_guid_match(device_t self, const char *guid)
    348  1.1    jruoho {
    349  1.1    jruoho 	struct acpi_wmi_softc *sc = device_private(self);
    350  1.1    jruoho 	ACPI_STATUS rv;
    351  1.1    jruoho 
    352  1.1    jruoho 	rv = acpi_wmi_guid_get(sc, guid, NULL);
    353  1.1    jruoho 
    354  1.1    jruoho 	if (ACPI_SUCCESS(rv))
    355  1.1    jruoho 		return 1;
    356  1.1    jruoho 
    357  1.1    jruoho 	return 0;
    358  1.1    jruoho }
    359  1.1    jruoho 
    360  1.1    jruoho /*
    361  1.1    jruoho  * Adds internal event handler.
    362  1.1    jruoho  */
    363  1.4    jruoho static void
    364  1.1    jruoho acpi_wmi_event_add(struct acpi_wmi_softc *sc)
    365  1.1    jruoho {
    366  1.1    jruoho 	struct wmi_t *wmi;
    367  1.1    jruoho 	ACPI_STATUS rv;
    368  1.1    jruoho 
    369  1.4    jruoho 	if (acpi_register_notify(sc->sc_node, acpi_wmi_event_handler) != true)
    370  1.4    jruoho 		return;
    371  1.1    jruoho 
    372  1.6    jruoho 	/*
    373  1.6    jruoho 	 * Enable possible expensive events.
    374  1.6    jruoho 	 */
    375  1.1    jruoho 	SIMPLEQ_FOREACH(wmi, &sc->wmi_head, wmi_link) {
    376  1.1    jruoho 
    377  1.1    jruoho 		if ((wmi->guid.flags & ACPI_WMI_FLAG_EVENT) != 0 &&
    378  1.1    jruoho 		    (wmi->guid.flags & ACPI_WMI_FLAG_EXPENSIVE) != 0) {
    379  1.1    jruoho 
    380  1.1    jruoho 			rv = acpi_wmi_enable(sc->sc_node->ad_handle,
    381  1.1    jruoho 			    wmi->guid.oid, false, true);
    382  1.1    jruoho 
    383  1.1    jruoho 			if (ACPI_SUCCESS(rv)) {
    384  1.1    jruoho 				wmi->eevent = true;
    385  1.1    jruoho 				continue;
    386  1.1    jruoho 			}
    387  1.1    jruoho 
    388  1.6    jruoho 			aprint_debug_dev(sc->sc_dev, "failed to enable "
    389  1.1    jruoho 			    "expensive WExx: %s\n", AcpiFormatException(rv));
    390  1.1    jruoho 		}
    391  1.1    jruoho 	}
    392  1.1    jruoho }
    393  1.1    jruoho 
    394  1.1    jruoho /*
    395  1.1    jruoho  * Removes the internal event handler.
    396  1.1    jruoho  */
    397  1.4    jruoho static void
    398  1.1    jruoho acpi_wmi_event_del(struct acpi_wmi_softc *sc)
    399  1.1    jruoho {
    400  1.1    jruoho 	struct wmi_t *wmi;
    401  1.1    jruoho 	ACPI_STATUS rv;
    402  1.1    jruoho 
    403  1.4    jruoho 	acpi_deregister_notify(sc->sc_node);
    404  1.1    jruoho 
    405  1.1    jruoho 	SIMPLEQ_FOREACH(wmi, &sc->wmi_head, wmi_link) {
    406  1.1    jruoho 
    407  1.1    jruoho 		if (wmi->eevent != true)
    408  1.1    jruoho 			continue;
    409  1.1    jruoho 
    410  1.1    jruoho 		KASSERT((wmi->guid.flags & ACPI_WMI_FLAG_EVENT) != 0);
    411  1.1    jruoho 		KASSERT((wmi->guid.flags & ACPI_WMI_FLAG_EXPENSIVE) != 0);
    412  1.1    jruoho 
    413  1.1    jruoho 		rv = acpi_wmi_enable(sc->sc_node->ad_handle,
    414  1.1    jruoho 		    wmi->guid.oid, false, false);
    415  1.1    jruoho 
    416  1.1    jruoho 		if (ACPI_SUCCESS(rv)) {
    417  1.1    jruoho 			wmi->eevent = false;
    418  1.1    jruoho 			continue;
    419  1.1    jruoho 		}
    420  1.1    jruoho 
    421  1.6    jruoho 		aprint_debug_dev(sc->sc_dev, "failed to disable "
    422  1.1    jruoho 		    "expensive WExx: %s\n", AcpiFormatException(rv));
    423  1.1    jruoho 	}
    424  1.1    jruoho }
    425  1.1    jruoho 
    426  1.1    jruoho /*
    427  1.1    jruoho  * Returns extra information possibly associated with an event.
    428  1.1    jruoho  */
    429  1.1    jruoho ACPI_STATUS
    430  1.1    jruoho acpi_wmi_event_get(device_t self, uint32_t event, ACPI_BUFFER *obuf)
    431  1.1    jruoho {
    432  1.1    jruoho 	struct acpi_wmi_softc *sc = device_private(self);
    433  1.1    jruoho 	struct wmi_t *wmi;
    434  1.1    jruoho 	ACPI_OBJECT_LIST arg;
    435  1.1    jruoho 	ACPI_OBJECT obj;
    436  1.2    jruoho 	ACPI_HANDLE hdl;
    437  1.2    jruoho 
    438  1.1    jruoho 	if (sc == NULL || obuf == NULL)
    439  1.1    jruoho 		return AE_BAD_PARAMETER;
    440  1.1    jruoho 
    441  1.1    jruoho 	if (sc->sc_handler == NULL)
    442  1.1    jruoho 		return AE_ABORT_METHOD;
    443  1.1    jruoho 
    444  1.3    jruoho 	hdl = sc->sc_node->ad_handle;
    445  1.3    jruoho 
    446  1.1    jruoho 	obj.Type = ACPI_TYPE_INTEGER;
    447  1.1    jruoho 	obj.Integer.Value = event;
    448  1.1    jruoho 
    449  1.1    jruoho 	arg.Count = 0x01;
    450  1.1    jruoho 	arg.Pointer = &obj;
    451  1.1    jruoho 
    452  1.1    jruoho 	obuf->Pointer = NULL;
    453  1.1    jruoho 	obuf->Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    454  1.1    jruoho 
    455  1.1    jruoho 	SIMPLEQ_FOREACH(wmi, &sc->wmi_head, wmi_link) {
    456  1.1    jruoho 
    457  1.1    jruoho 		if ((wmi->guid.flags & ACPI_WMI_FLAG_EVENT) == 0)
    458  1.1    jruoho 			continue;
    459  1.1    jruoho 
    460  1.1    jruoho 		if (wmi->guid.nid != event)
    461  1.1    jruoho 			continue;
    462  1.1    jruoho 
    463  1.2    jruoho 		return AcpiEvaluateObject(hdl, "_WED", &arg, obuf);
    464  1.1    jruoho 	}
    465  1.1    jruoho 
    466  1.1    jruoho 	return AE_NOT_FOUND;
    467  1.1    jruoho }
    468  1.1    jruoho 
    469  1.1    jruoho /*
    470  1.1    jruoho  * Forwards events to the external handler through the internal one.
    471  1.1    jruoho  */
    472  1.1    jruoho static void
    473  1.1    jruoho acpi_wmi_event_handler(ACPI_HANDLE hdl, uint32_t evt, void *aux)
    474  1.1    jruoho {
    475  1.4    jruoho 	struct acpi_wmi_softc *sc;
    476  1.4    jruoho 	device_t self = aux;
    477  1.4    jruoho 
    478  1.4    jruoho 	sc = device_private(self);
    479  1.1    jruoho 
    480  1.1    jruoho 	if (sc->sc_child == NULL)
    481  1.1    jruoho 		return;
    482  1.1    jruoho 
    483  1.1    jruoho 	if (sc->sc_handler == NULL)
    484  1.1    jruoho 		return;
    485  1.1    jruoho 
    486  1.1    jruoho 	(*sc->sc_handler)(NULL, evt, sc->sc_child);
    487  1.1    jruoho }
    488  1.1    jruoho 
    489  1.1    jruoho ACPI_STATUS
    490  1.1    jruoho acpi_wmi_event_register(device_t self, ACPI_NOTIFY_HANDLER handler)
    491  1.1    jruoho {
    492  1.1    jruoho 	struct acpi_wmi_softc *sc = device_private(self);
    493  1.1    jruoho 
    494  1.1    jruoho 	if (sc == NULL)
    495  1.1    jruoho 		return AE_BAD_PARAMETER;
    496  1.1    jruoho 
    497  1.1    jruoho 	if (handler != NULL && sc->sc_handler != NULL)
    498  1.1    jruoho 		return AE_ALREADY_EXISTS;
    499  1.1    jruoho 
    500  1.1    jruoho 	sc->sc_handler = handler;
    501  1.1    jruoho 
    502  1.1    jruoho 	return AE_OK;
    503  1.1    jruoho }
    504  1.1    jruoho 
    505  1.1    jruoho ACPI_STATUS
    506  1.1    jruoho acpi_wmi_event_deregister(device_t self)
    507  1.1    jruoho {
    508  1.1    jruoho 	return acpi_wmi_event_register(self, NULL);
    509  1.1    jruoho }
    510  1.1    jruoho 
    511  1.1    jruoho /*
    512  1.1    jruoho  * As there is no prior knowledge about the expensive
    513  1.1    jruoho  * events that cause "significant overhead", try to
    514  1.1    jruoho  * disable (enable) these before suspending (resuming).
    515  1.1    jruoho  */
    516  1.1    jruoho static bool
    517  1.1    jruoho acpi_wmi_suspend(device_t self, const pmf_qual_t *qual)
    518  1.1    jruoho {
    519  1.1    jruoho 	struct acpi_wmi_softc *sc = device_private(self);
    520  1.1    jruoho 
    521  1.1    jruoho 	acpi_wmi_event_del(sc);
    522  1.1    jruoho 
    523  1.1    jruoho 	return true;
    524  1.1    jruoho }
    525  1.1    jruoho 
    526  1.1    jruoho static bool
    527  1.1    jruoho acpi_wmi_resume(device_t self, const pmf_qual_t *qual)
    528  1.1    jruoho {
    529  1.1    jruoho 	struct acpi_wmi_softc *sc = device_private(self);
    530  1.1    jruoho 
    531  1.1    jruoho 	acpi_wmi_event_add(sc);
    532  1.1    jruoho 
    533  1.1    jruoho 	return true;
    534  1.1    jruoho }
    535  1.1    jruoho 
    536  1.1    jruoho /*
    537  1.1    jruoho  * Enables or disables data collection (WCxx) or an event (WExx).
    538  1.1    jruoho  */
    539  1.1    jruoho static ACPI_STATUS
    540  1.1    jruoho acpi_wmi_enable(ACPI_HANDLE hdl, const char *oid, bool data, bool flag)
    541  1.1    jruoho {
    542  1.1    jruoho 	char path[5];
    543  1.1    jruoho 	const char *str;
    544  1.1    jruoho 
    545  1.1    jruoho 	str = (data != false) ? "WC" : "WE";
    546  1.1    jruoho 
    547  1.1    jruoho 	(void)strlcpy(path, str, sizeof(path));
    548  1.1    jruoho 	(void)strlcat(path, oid, sizeof(path));
    549  1.1    jruoho 
    550  1.1    jruoho 	return acpi_eval_set_integer(hdl, path, (flag != false) ? 0x01 : 0x00);
    551  1.1    jruoho }
    552  1.1    jruoho 
    553  1.1    jruoho static bool
    554  1.1    jruoho acpi_wmi_input(struct wmi_t *wmi, uint8_t flag, uint8_t idx)
    555  1.1    jruoho {
    556  1.1    jruoho 
    557  1.2    jruoho 	if ((wmi->guid.flags & flag) == 0)
    558  1.1    jruoho 		return false;
    559  1.1    jruoho 
    560  1.1    jruoho 	if (wmi->guid.count == 0x00)
    561  1.1    jruoho 		return false;
    562  1.1    jruoho 
    563  1.1    jruoho 	if (wmi->guid.count < idx)
    564  1.1    jruoho 		return false;
    565  1.1    jruoho 
    566  1.1    jruoho 	return true;
    567  1.1    jruoho }
    568  1.1    jruoho 
    569  1.1    jruoho /*
    570  1.1    jruoho  * Makes a WMI data block query (WQxx). The corresponding control
    571  1.1    jruoho  * method for data collection will be invoked if it is available.
    572  1.1    jruoho  */
    573  1.1    jruoho ACPI_STATUS
    574  1.1    jruoho acpi_wmi_data_query(device_t self, const char *guid,
    575  1.1    jruoho     uint8_t idx, ACPI_BUFFER *obuf)
    576  1.1    jruoho {
    577  1.1    jruoho 	struct acpi_wmi_softc *sc = device_private(self);
    578  1.1    jruoho 	struct wmi_t *wmi;
    579  1.1    jruoho 	char path[5] = "WQ";
    580  1.1    jruoho 	ACPI_OBJECT_LIST arg;
    581  1.1    jruoho 	ACPI_STATUS rv, rvxx;
    582  1.1    jruoho 	ACPI_OBJECT obj;
    583  1.1    jruoho 
    584  1.1    jruoho 	rvxx = AE_SUPPORT;
    585  1.1    jruoho 
    586  1.1    jruoho 	if (obuf == NULL)
    587  1.1    jruoho 		return AE_BAD_PARAMETER;
    588  1.1    jruoho 
    589  1.1    jruoho 	rv = acpi_wmi_guid_get(sc, guid, &wmi);
    590  1.1    jruoho 
    591  1.1    jruoho 	if (ACPI_FAILURE(rv))
    592  1.1    jruoho 		return rv;
    593  1.1    jruoho 
    594  1.1    jruoho 	if (acpi_wmi_input(wmi, ACPI_WMI_FLAG_DATA, idx) != true)
    595  1.1    jruoho 		return AE_BAD_DATA;
    596  1.1    jruoho 
    597  1.1    jruoho 	(void)strlcat(path, wmi->guid.oid, sizeof(path));
    598  1.1    jruoho 
    599  1.1    jruoho 	obj.Type = ACPI_TYPE_INTEGER;
    600  1.1    jruoho 	obj.Integer.Value = idx;
    601  1.1    jruoho 
    602  1.1    jruoho 	arg.Count = 0x01;
    603  1.1    jruoho 	arg.Pointer = &obj;
    604  1.1    jruoho 
    605  1.1    jruoho 	obuf->Pointer = NULL;
    606  1.1    jruoho 	obuf->Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    607  1.1    jruoho 
    608  1.1    jruoho 	/*
    609  1.1    jruoho 	 * If the expensive flag is set, we should enable
    610  1.1    jruoho 	 * data collection before evaluating the WQxx buffer.
    611  1.1    jruoho 	 */
    612  1.1    jruoho 	if ((wmi->guid.flags & ACPI_WMI_FLAG_EXPENSIVE) != 0) {
    613  1.1    jruoho 
    614  1.1    jruoho 		rvxx = acpi_wmi_enable(sc->sc_node->ad_handle,
    615  1.1    jruoho 		    wmi->guid.oid, true, true);
    616  1.1    jruoho 	}
    617  1.1    jruoho 
    618  1.1    jruoho 	rv = AcpiEvaluateObject(sc->sc_node->ad_handle, path, &arg, obuf);
    619  1.1    jruoho 
    620  1.1    jruoho 	/* No longer needed. */
    621  1.1    jruoho 	if (ACPI_SUCCESS(rvxx)) {
    622  1.1    jruoho 
    623  1.1    jruoho 		(void)acpi_wmi_enable(sc->sc_node->ad_handle,
    624  1.1    jruoho 		    wmi->guid.oid, true, false);
    625  1.1    jruoho 	}
    626  1.1    jruoho 
    627  1.1    jruoho #ifdef DIAGNOSTIC
    628  1.1    jruoho 	/*
    629  1.1    jruoho 	 * XXX: It appears that quite a few laptops have WQxx
    630  1.1    jruoho 	 * methods that are declared as expensive, but lack the
    631  1.1    jruoho 	 * corresponding WCxx control method.
    632  1.1    jruoho 	 *
    633  1.1    jruoho 	 * -- Acer Aspire One is one example <jruohonen (at) iki.fi>.
    634  1.1    jruoho 	 */
    635  1.1    jruoho 	if (ACPI_FAILURE(rvxx) && rvxx != AE_SUPPORT)
    636  1.1    jruoho 		aprint_error_dev(sc->sc_dev, "failed to evaluate WCxx "
    637  1.1    jruoho 		    "for %s: %s\n", path, AcpiFormatException(rvxx));
    638  1.1    jruoho #endif
    639  1.1    jruoho 	return rv;
    640  1.1    jruoho }
    641  1.1    jruoho 
    642  1.1    jruoho /*
    643  1.1    jruoho  * Writes to a data block (WSxx).
    644  1.1    jruoho  */
    645  1.1    jruoho ACPI_STATUS
    646  1.1    jruoho acpi_wmi_data_write(device_t self, const char *guid,
    647  1.1    jruoho     uint8_t idx, ACPI_BUFFER *ibuf)
    648  1.1    jruoho {
    649  1.1    jruoho 	struct acpi_wmi_softc *sc = device_private(self);
    650  1.1    jruoho 	struct wmi_t *wmi;
    651  1.1    jruoho 	ACPI_OBJECT_LIST arg;
    652  1.1    jruoho 	ACPI_OBJECT obj[2];
    653  1.1    jruoho 	char path[5] = "WS";
    654  1.1    jruoho 	ACPI_STATUS rv;
    655  1.1    jruoho 
    656  1.1    jruoho 	if (ibuf == NULL)
    657  1.1    jruoho 		return AE_BAD_PARAMETER;
    658  1.1    jruoho 
    659  1.1    jruoho 	rv = acpi_wmi_guid_get(sc, guid, &wmi);
    660  1.1    jruoho 
    661  1.1    jruoho 	if (ACPI_FAILURE(rv))
    662  1.1    jruoho 		return rv;
    663  1.1    jruoho 
    664  1.1    jruoho 	if (acpi_wmi_input(wmi, ACPI_WMI_FLAG_DATA, idx) != true)
    665  1.1    jruoho 		return AE_BAD_DATA;
    666  1.1    jruoho 
    667  1.1    jruoho 	(void)strlcat(path, wmi->guid.oid, sizeof(path));
    668  1.1    jruoho 
    669  1.1    jruoho 	obj[0].Integer.Value = idx;
    670  1.1    jruoho 	obj[0].Type = ACPI_TYPE_INTEGER;
    671  1.1    jruoho 
    672  1.1    jruoho 	obj[1].Buffer.Length = ibuf->Length;
    673  1.1    jruoho 	obj[1].Buffer.Pointer = ibuf->Pointer;
    674  1.1    jruoho 
    675  1.1    jruoho 	obj[1].Type = ((wmi->guid.flags & ACPI_WMI_FLAG_STRING) != 0) ?
    676  1.1    jruoho 	    ACPI_TYPE_STRING : ACPI_TYPE_BUFFER;
    677  1.1    jruoho 
    678  1.1    jruoho 	arg.Count = 0x02;
    679  1.1    jruoho 	arg.Pointer = obj;
    680  1.1    jruoho 
    681  1.1    jruoho 	return AcpiEvaluateObject(sc->sc_node->ad_handle, path, &arg, NULL);
    682  1.1    jruoho }
    683  1.1    jruoho 
    684  1.1    jruoho /*
    685  1.1    jruoho  * Executes a method (WMxx).
    686  1.1    jruoho  */
    687  1.1    jruoho ACPI_STATUS
    688  1.1    jruoho acpi_wmi_method(device_t self, const char *guid, uint8_t idx,
    689  1.1    jruoho     uint32_t mid, ACPI_BUFFER *ibuf, ACPI_BUFFER *obuf)
    690  1.1    jruoho {
    691  1.1    jruoho 	struct acpi_wmi_softc *sc = device_private(self);
    692  1.1    jruoho 	struct wmi_t *wmi;
    693  1.1    jruoho 	ACPI_OBJECT_LIST arg;
    694  1.1    jruoho 	ACPI_OBJECT obj[3];
    695  1.1    jruoho 	char path[5] = "WM";
    696  1.1    jruoho 	ACPI_STATUS rv;
    697  1.1    jruoho 
    698  1.1    jruoho 	if (ibuf == NULL || obuf == NULL)
    699  1.1    jruoho 		return AE_BAD_PARAMETER;
    700  1.1    jruoho 
    701  1.1    jruoho 	rv = acpi_wmi_guid_get(sc, guid, &wmi);
    702  1.1    jruoho 
    703  1.1    jruoho 	if (ACPI_FAILURE(rv))
    704  1.1    jruoho 		return rv;
    705  1.1    jruoho 
    706  1.1    jruoho 	if (acpi_wmi_input(wmi, ACPI_WMI_FLAG_METHOD, idx) != true)
    707  1.1    jruoho 		return AE_BAD_DATA;
    708  1.1    jruoho 
    709  1.1    jruoho 	(void)strlcat(path, wmi->guid.oid, sizeof(path));
    710  1.1    jruoho 
    711  1.1    jruoho 	obj[0].Integer.Value = idx;
    712  1.1    jruoho 	obj[1].Integer.Value = mid;
    713  1.1    jruoho 	obj[0].Type = obj[1].Type = ACPI_TYPE_INTEGER;
    714  1.1    jruoho 
    715  1.1    jruoho 	obj[2].Buffer.Length = ibuf->Length;
    716  1.1    jruoho 	obj[2].Buffer.Pointer = ibuf->Pointer;
    717  1.1    jruoho 
    718  1.1    jruoho 	obj[2].Type = ((wmi->guid.flags & ACPI_WMI_FLAG_STRING) != 0) ?
    719  1.1    jruoho 	    ACPI_TYPE_STRING : ACPI_TYPE_BUFFER;
    720  1.1    jruoho 
    721  1.1    jruoho 	arg.Count = 0x03;
    722  1.1    jruoho 	arg.Pointer = obj;
    723  1.1    jruoho 
    724  1.1    jruoho 	obuf->Pointer = NULL;
    725  1.1    jruoho 	obuf->Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    726  1.1    jruoho 
    727  1.1    jruoho 	return AcpiEvaluateObject(sc->sc_node->ad_handle, path, &arg, obuf);
    728  1.1    jruoho }
    729