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