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