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