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