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