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