]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/acpi_support/acpi_wmi.c
Import tzdata 2020c
[FreeBSD/FreeBSD.git] / sys / dev / acpi_support / acpi_wmi.c
1 /*-
2  * Copyright (c) 2009 Michael Gmelin <freebsd@grem.de>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 /*
31  * Driver for acpi-wmi mapping, provides an interface for vendor specific
32  * implementations (e.g. HP and Acer laptops).
33  * Inspired by the ACPI-WMI mapping driver (c) 2008-2008 Carlos Corbacho which
34  * implements this functionality for Linux.
35  *
36  * WMI and ACPI: http://www.microsoft.com/whdc/system/pnppwr/wmi/wmi-acpi.mspx
37  * acpi-wmi for Linux: http://www.kernel.org
38  */
39
40 #include "opt_acpi.h"
41 #include <sys/param.h>
42 #include <sys/conf.h>
43 #include <sys/uio.h>
44 #include <sys/proc.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/sbuf.h>
48 #include <sys/module.h>
49 #include <sys/bus.h>
50
51 #include <contrib/dev/acpica/include/acpi.h>
52 #include <contrib/dev/acpica/include/accommon.h>
53 #include <dev/acpica/acpivar.h>
54 #include "acpi_wmi_if.h"
55
56 static MALLOC_DEFINE(M_ACPIWMI, "acpiwmi", "ACPI-WMI mapping");
57
58 #define _COMPONENT      ACPI_OEM
59 ACPI_MODULE_NAME("ACPI_WMI");
60
61 #define ACPI_WMI_REGFLAG_EXPENSIVE      0x1 /* GUID flag: Expensive operation */
62 #define ACPI_WMI_REGFLAG_METHOD         0x2     /* GUID flag: Method call */
63 #define ACPI_WMI_REGFLAG_STRING         0x4     /* GUID flag: String */
64 #define ACPI_WMI_REGFLAG_EVENT          0x8     /* GUID flag: Event */
65 #define ACPI_WMI_BMOF_UUID "05901221-D566-11D1-B2F0-00A0C9062910"
66
67 /*
68  * acpi_wmi driver private structure
69  */
70 struct acpi_wmi_softc {
71         device_t        wmi_dev;        /* wmi device id */
72         ACPI_HANDLE     wmi_handle;     /* handle of the PNP0C14 node */
73         device_t        ec_dev;         /* acpi_ec0 */
74         struct cdev     *wmistat_dev_t; /* wmistat device handle */
75         struct sbuf     wmistat_sbuf;   /* sbuf for /dev/wmistat output */
76         pid_t           wmistat_open_pid; /* pid operating on /dev/wmistat */
77         int             wmistat_bufptr; /* /dev/wmistat ptr to buffer position */
78         char            *mofbuf;
79
80         TAILQ_HEAD(wmi_info_list_head, wmi_info) wmi_info_list;
81 };
82
83 /*
84  * Struct that holds information about
85  * about a single GUID entry in _WDG
86  */
87 struct guid_info {
88         char    guid[16];       /* 16 byte non human readable GUID */
89         char    oid[2];         /* object id or event notify id (first byte) */
90         UINT8   max_instance;   /* highest instance known for this GUID */
91         UINT8   flags;          /* ACPI_WMI_REGFLAG_%s */
92 };
93
94 /* WExx event generation state (on/off) */
95 enum event_generation_state {
96         EVENT_GENERATION_ON = 1,
97         EVENT_GENERATION_OFF = 0
98 };
99
100 /*
101  * Information about one entry in _WDG.
102  * List of those is used to lookup information by GUID.
103  */
104 struct wmi_info {
105         TAILQ_ENTRY(wmi_info)   wmi_list;
106         struct guid_info        ginfo;          /* information on guid */
107         ACPI_NOTIFY_HANDLER     event_handler;/* client provided event handler */
108         void                    *event_handler_user_data; /* ev handler cookie  */
109 };
110
111 ACPI_SERIAL_DECL(acpi_wmi, "ACPI-WMI Mapping");
112
113 /* public interface - declaration */
114 /* standard device interface*/
115 static int              acpi_wmi_probe(device_t dev);
116 static int              acpi_wmi_attach(device_t dev);
117 static int              acpi_wmi_detach(device_t dev);
118 /* see acpi_wmi_if.m */
119 static int              acpi_wmi_provides_guid_string_method(device_t dev,
120                             const char *guid_string);
121 static ACPI_STATUS      acpi_wmi_evaluate_call_method(device_t dev,
122                             const char *guid_string, UINT8 instance,
123                             UINT32 method_id, const ACPI_BUFFER *in,
124                             ACPI_BUFFER *out);
125 static ACPI_STATUS      acpi_wmi_install_event_handler_method(device_t dev,
126                             const char *guid_string, ACPI_NOTIFY_HANDLER handler,
127                             void *data);
128 static ACPI_STATUS      acpi_wmi_remove_event_handler_method(device_t dev,
129                             const char *guid_string);
130 static ACPI_STATUS      acpi_wmi_get_event_data_method(device_t dev,
131                             UINT32 event_id, ACPI_BUFFER *out);
132 static ACPI_STATUS      acpi_wmi_get_block_method(device_t dev,
133                             const char *guid_string,
134                             UINT8 instance, ACPI_BUFFER *out);
135 static ACPI_STATUS      acpi_wmi_set_block_method(device_t dev,
136                             const char *guid_string,
137                             UINT8 instance, const ACPI_BUFFER *in);
138 /* private interface - declaration */
139 /* callbacks */
140 static void             acpi_wmi_notify_handler(ACPI_HANDLE h, UINT32 notify,
141                             void *context);
142 static ACPI_STATUS      acpi_wmi_ec_handler(UINT32 function,
143                             ACPI_PHYSICAL_ADDRESS address, UINT32 width,
144                             UINT64 *value, void *context,
145                             void *region_context);
146 /* helpers */
147 static ACPI_STATUS      acpi_wmi_read_wdg_blocks(struct acpi_wmi_softc *sc, ACPI_HANDLE h);
148 static ACPI_STATUS      acpi_wmi_toggle_we_event_generation(device_t dev,
149                             struct wmi_info *winfo,
150                             enum event_generation_state state);
151 static int              acpi_wmi_guid_string_to_guid(const UINT8 *guid_string,
152                             UINT8 *guid);
153 static struct wmi_info* acpi_wmi_lookup_wmi_info_by_guid_string(struct acpi_wmi_softc *sc,
154                             const char *guid_string);
155
156 static d_open_t acpi_wmi_wmistat_open;
157 static d_close_t acpi_wmi_wmistat_close;
158 static d_read_t acpi_wmi_wmistat_read;
159
160 /* handler /dev/wmistat device */
161 static struct cdevsw wmistat_cdevsw = {
162         .d_version = D_VERSION,
163         .d_open = acpi_wmi_wmistat_open,
164         .d_close = acpi_wmi_wmistat_close,
165         .d_read = acpi_wmi_wmistat_read,
166         .d_name = "wmistat",
167 };
168
169 static device_method_t acpi_wmi_methods[] = {
170         /* Device interface */
171         DEVMETHOD(device_probe, acpi_wmi_probe),
172         DEVMETHOD(device_attach, acpi_wmi_attach),
173         DEVMETHOD(device_detach, acpi_wmi_detach),
174
175         /* bus interface */
176         DEVMETHOD(bus_add_child,        bus_generic_add_child),
177
178         /* acpi_wmi interface */
179         DEVMETHOD(acpi_wmi_provides_guid_string,
180                     acpi_wmi_provides_guid_string_method),
181         DEVMETHOD(acpi_wmi_evaluate_call, acpi_wmi_evaluate_call_method),
182         DEVMETHOD(acpi_wmi_install_event_handler,
183                     acpi_wmi_install_event_handler_method),
184         DEVMETHOD(acpi_wmi_remove_event_handler,
185                     acpi_wmi_remove_event_handler_method),
186         DEVMETHOD(acpi_wmi_get_event_data, acpi_wmi_get_event_data_method),
187         DEVMETHOD(acpi_wmi_get_block, acpi_wmi_get_block_method),
188         DEVMETHOD(acpi_wmi_set_block, acpi_wmi_set_block_method),
189
190         DEVMETHOD_END
191 };
192
193 static driver_t acpi_wmi_driver = {
194         "acpi_wmi",
195         acpi_wmi_methods,
196         sizeof(struct acpi_wmi_softc),
197 };
198
199 static devclass_t acpi_wmi_devclass;
200 DRIVER_MODULE(acpi_wmi, acpi, acpi_wmi_driver, acpi_wmi_devclass, 0, 0);
201 MODULE_VERSION(acpi_wmi, 1);
202 MODULE_DEPEND(acpi_wmi, acpi, 1, 1, 1);
203 static char *wmi_ids[] = {"PNP0C14", NULL};
204
205 /*
206  * Probe for the PNP0C14 ACPI node
207  */
208 static int
209 acpi_wmi_probe(device_t dev)
210 {
211         int rv; 
212
213         if (acpi_disabled("wmi"))
214                 return (ENXIO);
215         rv = ACPI_ID_PROBE(device_get_parent(dev), dev, wmi_ids, NULL);
216         if (rv <= 0)
217                 device_set_desc(dev, "ACPI-WMI mapping");
218
219         return (rv);
220 }
221
222 /*
223  * Attach the device by:
224  * - Looking for the first ACPI EC device
225  * - Install the notify handler
226  * - Install the EC address space handler
227  * - Look for the _WDG node and read GUID information blocks
228  */
229 static int
230 acpi_wmi_attach(device_t dev)
231 {
232         struct acpi_wmi_softc *sc;
233         int ret;
234         ACPI_STATUS status;
235
236         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
237         sc = device_get_softc(dev);
238         ret = ENXIO;
239
240         ACPI_SERIAL_BEGIN(acpi_wmi);
241         sc->wmi_dev = dev;
242         sc->wmi_handle = acpi_get_handle(dev);
243         TAILQ_INIT(&sc->wmi_info_list);
244         /* XXX Only works with one EC, but nearly all systems only have one. */
245         if ((sc->ec_dev = devclass_get_device(devclass_find("acpi_ec"), 0))
246             == NULL)
247                 device_printf(dev, "cannot find EC device\n");
248         else if (ACPI_FAILURE((status = AcpiInstallNotifyHandler(sc->wmi_handle,
249                     ACPI_DEVICE_NOTIFY, acpi_wmi_notify_handler, sc))))
250                 device_printf(sc->wmi_dev, "couldn't install notify handler - %s\n",
251                     AcpiFormatException(status));
252         else if (ACPI_FAILURE((status = AcpiInstallAddressSpaceHandler(
253                     sc->wmi_handle, ACPI_ADR_SPACE_EC, acpi_wmi_ec_handler,
254                     NULL, sc)))) {
255                 device_printf(sc->wmi_dev, "couldn't install EC handler - %s\n",
256                     AcpiFormatException(status));
257                 AcpiRemoveNotifyHandler(sc->wmi_handle, ACPI_DEVICE_NOTIFY,
258                     acpi_wmi_notify_handler);
259         } else if (ACPI_FAILURE((status = acpi_wmi_read_wdg_blocks(sc,
260                     sc->wmi_handle)))) {
261                 device_printf(sc->wmi_dev, "couldn't parse _WDG - %s\n",
262                     AcpiFormatException(status));
263                 AcpiRemoveNotifyHandler(sc->wmi_handle, ACPI_DEVICE_NOTIFY,
264                     acpi_wmi_notify_handler);
265                 AcpiRemoveAddressSpaceHandler(sc->wmi_handle, ACPI_ADR_SPACE_EC,
266                     acpi_wmi_ec_handler);
267         } else {
268                 sc->wmistat_dev_t = make_dev(&wmistat_cdevsw, 0, UID_ROOT,
269                     GID_WHEEL, 0644, "wmistat%d", device_get_unit(dev));
270                 sc->wmistat_dev_t->si_drv1 = sc;
271                 sc->wmistat_open_pid = 0;
272                 sc->wmistat_bufptr = -1;
273                 ret = 0;
274         }
275         ACPI_SERIAL_END(acpi_wmi);
276
277         if (acpi_wmi_provides_guid_string_method(dev, ACPI_WMI_BMOF_UUID)) {
278                 ACPI_BUFFER out = { ACPI_ALLOCATE_BUFFER, NULL };
279                 ACPI_OBJECT *obj;
280
281                 device_printf(dev, "Embedded MOF found\n");
282                 status = acpi_wmi_get_block_method(dev,  ACPI_WMI_BMOF_UUID,
283                     0, &out);
284                 if (ACPI_SUCCESS(status)) {
285                         obj = out.Pointer;
286                         if (obj && obj->Type == ACPI_TYPE_BUFFER) {
287                                 SYSCTL_ADD_OPAQUE(device_get_sysctl_ctx(dev),
288                                     SYSCTL_CHILDREN(
289                                         device_get_sysctl_tree(dev)),
290                                     OID_AUTO, "bmof", 
291                                     CTLFLAG_RD | CTLFLAG_MPSAFE,
292                                     obj->Buffer.Pointer,
293                                     obj->Buffer.Length,
294                                     "A", "MOF Blob");
295                         }
296                 }
297                 sc->mofbuf = out.Pointer;
298         }
299                 
300         if (ret == 0) {
301                 bus_generic_probe(dev);
302                 ret = bus_generic_attach(dev);
303         }
304
305         return (ret);
306 }
307
308 /*
309  * Detach the driver by:
310  * - Removing notification handler
311  * - Removing address space handler
312  * - Turning off event generation for all WExx event activated by
313  *   child drivers
314  */
315 static int
316 acpi_wmi_detach(device_t dev)
317 {
318         struct wmi_info *winfo, *tmp;
319         struct acpi_wmi_softc *sc;
320         int ret;
321
322         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
323         sc = device_get_softc(dev);
324         ACPI_SERIAL_BEGIN(acpi_wmi);
325
326         if (sc->wmistat_open_pid != 0) {
327                 ret = EBUSY;
328         } else {
329                 AcpiRemoveNotifyHandler(sc->wmi_handle, ACPI_DEVICE_NOTIFY,
330                     acpi_wmi_notify_handler);
331                 AcpiRemoveAddressSpaceHandler(sc->wmi_handle,
332                     ACPI_ADR_SPACE_EC, acpi_wmi_ec_handler);
333                 TAILQ_FOREACH_SAFE(winfo, &sc->wmi_info_list, wmi_list, tmp) {
334                         if (winfo->event_handler)
335                                 acpi_wmi_toggle_we_event_generation(dev,
336                                     winfo, EVENT_GENERATION_OFF);
337                         TAILQ_REMOVE(&sc->wmi_info_list, winfo, wmi_list);
338                         free(winfo, M_ACPIWMI);
339                 }
340                 if (sc->wmistat_bufptr != -1) {
341                         sbuf_delete(&sc->wmistat_sbuf);
342                         sc->wmistat_bufptr = -1;
343                 }
344                 sc->wmistat_open_pid = 0;
345                 destroy_dev(sc->wmistat_dev_t);
346                 ret = 0;
347                 AcpiOsFree(sc->mofbuf);
348         }
349         ACPI_SERIAL_END(acpi_wmi);
350
351         return (ret);
352 }
353
354 /*
355  * Check if the given GUID string (human readable format
356  * AABBCCDD-EEFF-GGHH-IIJJ-KKLLMMNNOOPP)
357  * exists within _WDG
358  */
359 static int
360 acpi_wmi_provides_guid_string_method(device_t dev, const char *guid_string)
361 {
362         struct acpi_wmi_softc *sc;
363         struct wmi_info *winfo;
364         int ret;
365
366         sc = device_get_softc(dev);
367         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
368         ACPI_SERIAL_BEGIN(acpi_wmi);
369         winfo = acpi_wmi_lookup_wmi_info_by_guid_string(sc, guid_string);
370         ret = (winfo == NULL)?0:winfo->ginfo.max_instance+1;
371         ACPI_SERIAL_END(acpi_wmi);
372
373         return (ret);
374 }
375
376 /*
377  * Call a method "method_id" on the given GUID block
378  * write result into user provided output buffer
379  */
380 static ACPI_STATUS
381 acpi_wmi_evaluate_call_method(device_t dev, const char *guid_string,
382     UINT8 instance, UINT32 method_id, const ACPI_BUFFER *in, ACPI_BUFFER *out)
383 {
384         ACPI_OBJECT params[3];
385         ACPI_OBJECT_LIST input;
386         char method[5] = "WMxx";
387         struct wmi_info *winfo;
388         struct acpi_wmi_softc *sc;
389         ACPI_STATUS status;
390
391         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
392
393         sc = device_get_softc(dev);
394         ACPI_SERIAL_BEGIN(acpi_wmi);
395         if ((winfo = acpi_wmi_lookup_wmi_info_by_guid_string(sc, guid_string))
396                     == NULL)
397                 status = AE_NOT_FOUND;
398         else if (!(winfo->ginfo.flags & ACPI_WMI_REGFLAG_METHOD))
399                 status = AE_BAD_DATA;
400         else if (instance > winfo->ginfo.max_instance)
401                 status = AE_BAD_PARAMETER;
402         else {
403                 params[0].Type = ACPI_TYPE_INTEGER;
404                 params[0].Integer.Value = instance;
405                 params[1].Type = ACPI_TYPE_INTEGER;
406                 params[1].Integer.Value = method_id;
407                 input.Pointer = params;
408                 input.Count = 2;
409                 if (in) {
410                         params[2].Type =
411                             (winfo->ginfo.flags & ACPI_WMI_REGFLAG_STRING)
412                             ?ACPI_TYPE_STRING:ACPI_TYPE_BUFFER;
413                         params[2].Buffer.Length = in->Length;
414                         params[2].Buffer.Pointer = in->Pointer;
415                         input.Count = 3;
416                 }
417                 method[2] = winfo->ginfo.oid[0];
418                 method[3] = winfo->ginfo.oid[1];
419                 status = AcpiEvaluateObject(sc->wmi_handle, method,
420                             &input, out);
421         }
422         ACPI_SERIAL_END(acpi_wmi);
423
424         return (status);
425 }
426
427 /*
428  * Install a user provided event_handler on the given GUID
429  * provided *data will be passed on callback
430  * If there is already an existing event handler registered it will be silently
431  * discarded
432  */
433 static ACPI_STATUS
434 acpi_wmi_install_event_handler_method(device_t dev, const char *guid_string,
435     ACPI_NOTIFY_HANDLER event_handler, void *data)
436 {
437         struct acpi_wmi_softc *sc = device_get_softc(dev);
438         struct wmi_info *winfo;
439         ACPI_STATUS status;
440
441         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
442
443         status = AE_OK;
444         ACPI_SERIAL_BEGIN(acpi_wmi);
445         if (guid_string == NULL || event_handler == NULL)
446                 status = AE_BAD_PARAMETER;
447         else if ((winfo = acpi_wmi_lookup_wmi_info_by_guid_string(sc, guid_string))
448                     == NULL)
449                 status = AE_NOT_EXIST;
450         else if (winfo->event_handler != NULL ||
451                 (status = acpi_wmi_toggle_we_event_generation(dev, winfo,
452                     EVENT_GENERATION_ON)) == AE_OK) {
453                 winfo->event_handler = event_handler;
454                 winfo->event_handler_user_data = data;
455         }
456         ACPI_SERIAL_END(acpi_wmi);
457
458         return (status);
459 }
460
461 /*
462  * Remove a previously installed event handler from the given GUID
463  * If there was none installed, this call is silently discarded and
464  * reported as AE_OK
465  */
466 static ACPI_STATUS
467 acpi_wmi_remove_event_handler_method(device_t dev, const char *guid_string)
468 {
469         struct acpi_wmi_softc *sc = device_get_softc(dev);
470         struct wmi_info *winfo;
471         ACPI_STATUS status;
472
473         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
474
475         status = AE_OK;
476         ACPI_SERIAL_BEGIN(acpi_wmi);
477         if (guid_string &&
478             (winfo = acpi_wmi_lookup_wmi_info_by_guid_string(sc, guid_string))
479             != NULL && winfo->event_handler) {
480                 status = acpi_wmi_toggle_we_event_generation(dev, winfo,
481                             EVENT_GENERATION_OFF);
482                 winfo->event_handler = NULL;
483                 winfo->event_handler_user_data = NULL;
484         }
485         ACPI_SERIAL_END(acpi_wmi);
486
487         return (status);
488 }
489
490 /*
491  * Get details on an event received through a callback registered
492  * through ACPI_WMI_REMOVE_EVENT_HANDLER into a user provided output buffer.
493  * (event_id equals "notify" passed in the callback)
494  */
495 static ACPI_STATUS
496 acpi_wmi_get_event_data_method(device_t dev, UINT32 event_id, ACPI_BUFFER *out)
497 {
498         ACPI_OBJECT_LIST input;
499         ACPI_OBJECT params[1];
500         struct acpi_wmi_softc *sc;
501         struct wmi_info *winfo;
502         ACPI_STATUS status;
503
504         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
505
506         sc = device_get_softc(dev);
507         status = AE_NOT_FOUND;  
508         ACPI_SERIAL_BEGIN(acpi_wmi);
509         params[0].Type = ACPI_TYPE_INTEGER;
510         params[0].Integer.Value = event_id;
511         input.Pointer = params;
512         input.Count = 1;
513         TAILQ_FOREACH(winfo, &sc->wmi_info_list, wmi_list) {
514                 if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EVENT) &&
515                     ((UINT8) winfo->ginfo.oid[0] == event_id)) {
516                         status = AcpiEvaluateObject(sc->wmi_handle, "_WED",
517                                     &input, out);
518                         break;
519                 }
520         }
521         ACPI_SERIAL_END(acpi_wmi);
522
523         return (status);
524 }
525
526 /*
527  * Read a block of data from the given GUID (using WQxx (query))
528  * Will be returned in a user provided buffer (out).
529  * If the method is marked as expensive (ACPI_WMI_REGFLAG_EXPENSIVE)
530  * we will first call the WCxx control method to lock the node to
531  * lock the node for data collection and release it afterwards.
532  * (Failed WCxx calls are ignored to "support" broken implementations)
533  */
534 static ACPI_STATUS
535 acpi_wmi_get_block_method(device_t dev, const char *guid_string, UINT8 instance,
536         ACPI_BUFFER *out)
537 {
538         char wc_method[5] = "WCxx";
539         char wq_method[5] = "WQxx";
540         ACPI_OBJECT_LIST wc_input;
541         ACPI_OBJECT_LIST wq_input;
542         ACPI_OBJECT wc_params[1];
543         ACPI_OBJECT wq_params[1];
544         ACPI_HANDLE wc_handle;
545         struct acpi_wmi_softc *sc;
546         struct wmi_info *winfo;
547         ACPI_STATUS status;
548         ACPI_STATUS wc_status;
549
550         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
551
552         sc = device_get_softc(dev);
553         wc_status = AE_ERROR;
554         ACPI_SERIAL_BEGIN(acpi_wmi);
555         if (guid_string == NULL || out == NULL)
556                 status = AE_BAD_PARAMETER;
557         else if ((winfo = acpi_wmi_lookup_wmi_info_by_guid_string(sc, guid_string))
558                     == NULL)
559                 status = AE_ERROR;
560         else if (instance > winfo->ginfo.max_instance)
561                 status = AE_BAD_PARAMETER;
562         else if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EVENT) ||
563             (winfo->ginfo.flags & ACPI_WMI_REGFLAG_METHOD))
564                 status = AE_ERROR;
565         else {
566                 wq_params[0].Type = ACPI_TYPE_INTEGER;
567                 wq_params[0].Integer.Value = instance;
568                 wq_input.Pointer = wq_params;
569                 wq_input.Count = 1;
570                 if (winfo->ginfo.flags & ACPI_WMI_REGFLAG_EXPENSIVE) {
571                         wc_params[0].Type = ACPI_TYPE_INTEGER;
572                         wc_params[0].Integer.Value = 1;
573                         wc_input.Pointer = wc_params;
574                         wc_input.Count = 1;
575                         wc_method[2] = winfo->ginfo.oid[0];
576                         wc_method[3] = winfo->ginfo.oid[1];
577                         wc_status = AcpiGetHandle(sc->wmi_handle, wc_method,
578                                     &wc_handle);
579                         if (ACPI_SUCCESS(wc_status))
580                                 wc_status = AcpiEvaluateObject(wc_handle,
581                                                 wc_method, &wc_input, NULL);
582                 }
583                 wq_method[2] = winfo->ginfo.oid[0];
584                 wq_method[3] = winfo->ginfo.oid[1];
585                 status = AcpiEvaluateObject(sc->wmi_handle, wq_method,
586                             &wq_input, out);
587                 if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EXPENSIVE)
588                     && ACPI_SUCCESS(wc_status)) {
589                         wc_params[0].Integer.Value = 0;
590                         status = AcpiEvaluateObject(wc_handle, wc_method,
591                                     &wc_input, NULL);  /* XXX this might be
592                                                          the wrong status to
593                                                          return? */
594                 }
595         }
596         ACPI_SERIAL_END(acpi_wmi);
597
598         return (status);
599 }
600
601 /*
602  * Write a block of data to the given GUID (using WSxx)
603  */
604 static ACPI_STATUS
605 acpi_wmi_set_block_method(device_t dev, const char *guid_string, UINT8 instance,
606         const ACPI_BUFFER *in)
607 {
608         char method[5] = "WSxx";
609         ACPI_OBJECT_LIST input;
610         ACPI_OBJECT params[2];
611         struct wmi_info *winfo;
612         struct acpi_wmi_softc *sc;
613         ACPI_STATUS status;
614
615         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
616
617         sc = device_get_softc(dev);
618         ACPI_SERIAL_BEGIN(acpi_wmi);
619         if (guid_string == NULL || in == NULL)
620                 status = AE_BAD_DATA;
621         else if ((winfo = acpi_wmi_lookup_wmi_info_by_guid_string(sc, guid_string))
622                     == NULL)
623                 status = AE_ERROR;
624         else if (instance > winfo->ginfo.max_instance)
625                 status = AE_BAD_PARAMETER;
626         else if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EVENT) ||
627                     (winfo->ginfo.flags & ACPI_WMI_REGFLAG_METHOD))
628                 status = AE_ERROR;
629         else {
630                 params[0].Type = ACPI_TYPE_INTEGER;
631                 params[0].Integer.Value = instance;
632                 input.Pointer = params;
633                 input.Count = 2;
634                 params[1].Type = (winfo->ginfo.flags & ACPI_WMI_REGFLAG_STRING)
635                     ?ACPI_TYPE_STRING:ACPI_TYPE_BUFFER;
636                 params[1].Buffer.Length = in->Length;
637                 params[1].Buffer.Pointer = in->Pointer;
638                 method[2] = winfo->ginfo.oid[0];
639                 method[3] = winfo->ginfo.oid[1];
640                 status = AcpiEvaluateObject(sc->wmi_handle, method,
641                             &input, NULL);
642         }
643         ACPI_SERIAL_END(acpi_wmi);
644
645         return (status);
646 }
647
648 /*
649  * Handle events received and dispatch them to
650  * stakeholders that registered through ACPI_WMI_INSTALL_EVENT_HANDLER
651  */
652 static void
653 acpi_wmi_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
654 {
655         struct acpi_wmi_softc *sc = context;
656         ACPI_NOTIFY_HANDLER handler;
657         void *handler_data;
658         struct wmi_info *winfo;
659
660         ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
661
662         handler = NULL;
663         handler_data = NULL;
664         ACPI_SERIAL_BEGIN(acpi_wmi);
665         TAILQ_FOREACH(winfo, &sc->wmi_info_list, wmi_list) {
666                 if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EVENT) &&
667                                 ((UINT8) winfo->ginfo.oid[0] == notify)) {
668                         if (winfo->event_handler) {
669                                 handler = winfo->event_handler;
670                                 handler_data = winfo->event_handler_user_data;
671                                 break;
672                         }
673                 }
674         }
675         ACPI_SERIAL_END(acpi_wmi);
676         if (handler) {
677                 handler(h, notify, handler_data);
678         }
679 }
680
681 /*
682  * Handle EC address space notifications reveived on the WDG node
683  * (this mimics EcAddressSpaceHandler in acpi_ec.c)
684  */
685 static ACPI_STATUS
686 acpi_wmi_ec_handler(UINT32 function, ACPI_PHYSICAL_ADDRESS address,
687     UINT32 width, UINT64 *value, void *context,
688     void *region_context)
689 {
690         struct acpi_wmi_softc *sc;
691         int i;
692         UINT64 ec_data;
693         UINT8 ec_addr;
694         ACPI_STATUS status;
695
696         ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)address);
697
698         sc = (struct acpi_wmi_softc *)context;
699         if (width % 8 != 0 || value == NULL || context == NULL)
700                 return (AE_BAD_PARAMETER);
701         if (address + (width / 8) - 1 > 0xFF)
702                 return (AE_BAD_ADDRESS);
703         if (function == ACPI_READ)
704                 *value = 0;
705         ec_addr = address;
706         status = AE_ERROR;
707
708         for (i = 0; i < width; i += 8, ++ec_addr) {
709                 switch (function) {
710                 case ACPI_READ:
711                         status = ACPI_EC_READ(sc->ec_dev, ec_addr, &ec_data, 1);
712                         if (ACPI_SUCCESS(status))
713                                 *value |= ((UINT64)ec_data) << i;
714                 break;
715                 case ACPI_WRITE:
716                         ec_data = (UINT8)((*value) >> i);
717                         status = ACPI_EC_WRITE(sc->ec_dev, ec_addr, ec_data, 1);
718                         break;
719                 default:
720                         device_printf(sc->wmi_dev,
721                             "invalid acpi_wmi_ec_handler function %d\n",
722                             function);
723                         status = AE_BAD_PARAMETER;
724                         break;
725                 }
726                 if (ACPI_FAILURE(status))
727                         break;
728         }
729
730         return (status);
731 }
732
733 /*
734  * Read GUID blocks from the _WDG node
735  * into wmi_info_list.
736  */
737 static ACPI_STATUS
738 acpi_wmi_read_wdg_blocks(struct acpi_wmi_softc *sc, ACPI_HANDLE h)
739 {
740         ACPI_BUFFER out = {ACPI_ALLOCATE_BUFFER, NULL};
741         struct guid_info *ginfo;
742         ACPI_OBJECT *obj;
743         struct wmi_info *winfo;
744         UINT32 i;
745         UINT32 wdg_block_count;
746         ACPI_STATUS status;
747
748         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
749
750         ACPI_SERIAL_ASSERT(acpi_wmi);
751         if (ACPI_FAILURE(status = AcpiEvaluateObject(h, "_WDG", NULL, &out)))
752                 return (status);
753         obj = (ACPI_OBJECT*) out.Pointer;
754         wdg_block_count = obj->Buffer.Length / sizeof(struct guid_info);
755         if ((ginfo = malloc(obj->Buffer.Length, M_ACPIWMI, M_NOWAIT))
756                     == NULL) {
757                 AcpiOsFree(out.Pointer);
758                 return (AE_NO_MEMORY);
759         }
760         memcpy(ginfo, obj->Buffer.Pointer, obj->Buffer.Length);
761         for (i = 0; i < wdg_block_count; ++i) {
762                 if ((winfo = malloc(sizeof(struct wmi_info), M_ACPIWMI,
763                             M_NOWAIT | M_ZERO)) == NULL) {
764                         AcpiOsFree(out.Pointer);
765                         free(ginfo, M_ACPIWMI);
766                         return (AE_NO_MEMORY);
767                 }
768                 winfo->ginfo = ginfo[i];
769                 TAILQ_INSERT_TAIL(&sc->wmi_info_list, winfo, wmi_list);
770         }
771         AcpiOsFree(out.Pointer);
772         free(ginfo, M_ACPIWMI);
773
774         return (status);
775 }
776
777 /*
778  * Toggle event generation in for the given GUID (passed by winfo)
779  * Turn on to get notified (through acpi_wmi_notify_handler) if events happen
780  * on the given GUID.
781  */
782 static ACPI_STATUS
783 acpi_wmi_toggle_we_event_generation(device_t dev, struct wmi_info *winfo,
784     enum event_generation_state state)
785 {
786         char method[5] = "WExx";
787         ACPI_OBJECT_LIST input;
788         ACPI_OBJECT params[1];
789         struct acpi_wmi_softc *sc;
790         ACPI_STATUS status;
791
792         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
793
794         sc = device_get_softc(dev);
795         ACPI_SERIAL_ASSERT(acpi_wmi);
796         params[0].Type = ACPI_TYPE_INTEGER;
797         params[0].Integer.Value = state==EVENT_GENERATION_ON?1:0;
798         input.Pointer = params;
799         input.Count = 1;
800
801         UINT8 hi = ((UINT8) winfo->ginfo.oid[0]) >> 4;
802         UINT8 lo = ((UINT8) winfo->ginfo.oid[0]) & 0xf;
803         method[2] = (hi > 9 ? hi + 55: hi + 48);
804         method[3] = (lo > 9 ? lo + 55: lo + 48);
805         status = AcpiEvaluateObject(sc->wmi_handle, method, &input, NULL);
806         if (status == AE_NOT_FOUND) status = AE_OK;
807
808         return (status);
809 }
810
811 /*
812  * Convert given two digit hex string (hexin) to an UINT8 referenced
813  * by byteout.
814  * Return != 0 if the was a problem (invalid input)
815  */
816 static __inline int acpi_wmi_hex_to_int(const UINT8 *hexin, UINT8 *byteout)
817 {
818         unsigned int hi;
819         unsigned int lo;
820
821         hi = hexin[0];
822         lo = hexin[1];
823         if ('0' <= hi && hi <= '9')
824                 hi -= '0';
825         else if ('A' <= hi && hi <= 'F')
826                 hi -= ('A' - 10);
827         else if ('a' <= hi && hi <= 'f')
828                 hi -= ('a' - 10);
829         else
830                 return (1);
831         if ('0' <= lo && lo <= '9')
832                 lo -= '0';
833         else if ('A' <= lo && lo <= 'F')
834                 lo -= ('A' - 10);
835         else if ('a' <= lo && lo <= 'f')
836                 lo -= ('a' - 10);
837         else
838                 return (1);
839         *byteout = (hi << 4) + lo;
840
841         return (0);
842 }
843
844 /*
845  * Convert a human readable 36 character GUID into a 16byte
846  * machine readable one.
847  * The basic algorithm looks as follows:
848  * Input:  AABBCCDD-EEFF-GGHH-IIJJ-KKLLMMNNOOPP
849  * Output: DCBAFEHGIJKLMNOP
850  * (AA BB CC etc. represent two digit hex numbers == bytes)
851  * Return != 0 if passed guid string is invalid
852  */
853 static int
854 acpi_wmi_guid_string_to_guid(const UINT8 *guid_string, UINT8 *guid)
855 {
856         static const int mapping[20] = {3, 2, 1, 0, -1, 5, 4, -1, 7, 6, -1,
857             8, 9, -1, 10, 11, 12, 13, 14, 15};
858         int i;
859
860         for (i = 0; i < 20; ++i, ++guid_string) {
861                 if (mapping[i] >= 0) {
862                         if (acpi_wmi_hex_to_int(guid_string,
863                             &guid[mapping[i]]))
864                                 return (-1);
865                         ++guid_string;
866                 } else if (*guid_string != '-')
867                         return (-1);
868         }
869
870         return (0);
871 }
872
873 /*
874  * Lookup a wmi_info structure in wmi_list based on a
875  * human readable GUID
876  * Return NULL if the GUID is unknown in the _WDG
877  */
878 static struct wmi_info*
879 acpi_wmi_lookup_wmi_info_by_guid_string(struct acpi_wmi_softc *sc, const char *guid_string)
880 {
881         char guid[16];
882         struct wmi_info *winfo;
883
884         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
885
886         ACPI_SERIAL_ASSERT(acpi_wmi);
887
888         if (!acpi_wmi_guid_string_to_guid(guid_string, guid)) {
889                 TAILQ_FOREACH(winfo, &sc->wmi_info_list, wmi_list) {
890                         if (!memcmp(winfo->ginfo.guid, guid, 16)) {
891                                 return (winfo);
892                         }
893                 }
894         }
895
896         return (NULL);
897 }
898
899 /*
900  * open wmistat device
901  */
902 static int
903 acpi_wmi_wmistat_open(struct cdev* dev, int flags, int mode, struct thread *td)
904 {
905         struct acpi_wmi_softc *sc;
906         int ret;
907
908         if (dev == NULL || dev->si_drv1 == NULL)
909                 return (EBADF);
910         sc = dev->si_drv1;
911
912         ACPI_SERIAL_BEGIN(acpi_wmi);
913         if (sc->wmistat_open_pid != 0) {
914                 ret = EBUSY;
915         }
916         else {
917                 if (sbuf_new(&sc->wmistat_sbuf, NULL, 4096, SBUF_AUTOEXTEND)
918                             == NULL) {
919                         ret = ENXIO;
920                 } else {
921                         sc->wmistat_open_pid = td->td_proc->p_pid;
922                         sc->wmistat_bufptr = 0;
923                         ret = 0;
924                 }
925         }
926         ACPI_SERIAL_END(acpi_wmi);
927
928         return (ret);
929 }
930
931 /*
932  * close wmistat device
933  */
934 static int
935 acpi_wmi_wmistat_close(struct cdev* dev, int flags, int mode,
936     struct thread *td)
937 {
938         struct acpi_wmi_softc *sc;
939         int ret;
940
941         if (dev == NULL || dev->si_drv1 == NULL)
942                 return (EBADF);
943         sc = dev->si_drv1;
944
945         ACPI_SERIAL_BEGIN(acpi_wmi);
946         if (sc->wmistat_open_pid == 0) {
947                 ret = EBADF;
948         }
949         else {
950                 if (sc->wmistat_bufptr != -1) {
951                         sbuf_delete(&sc->wmistat_sbuf);
952                         sc->wmistat_bufptr = -1;
953                 }
954                 sc->wmistat_open_pid = 0;
955                 ret = 0;
956         }
957         ACPI_SERIAL_END(acpi_wmi);
958
959         return (ret);
960 }
961
962 /*
963  * Read from wmistat guid information
964  */
965 static int
966 acpi_wmi_wmistat_read(struct cdev *dev, struct uio *buf, int flag)
967 {
968         struct acpi_wmi_softc *sc;
969         struct wmi_info *winfo;
970         int l;
971         int ret;
972         UINT8* guid;
973
974         if (dev == NULL || dev->si_drv1 == NULL)
975                 return (EBADF);
976         sc = dev->si_drv1;
977
978         ACPI_SERIAL_BEGIN(acpi_wmi);
979         if (sc->wmistat_open_pid != buf->uio_td->td_proc->p_pid ||
980                         sc->wmistat_bufptr == -1) {
981                 ret = EBADF;
982         }
983         else {
984                 if (!sbuf_done(&sc->wmistat_sbuf)) {
985                         sbuf_printf(&sc->wmistat_sbuf, "GUID                 "
986                                     "                 INST EXPE METH STR "
987                                     "EVENT OID\n");
988                         TAILQ_FOREACH(winfo, &sc->wmi_info_list, wmi_list) {
989                                 guid = (UINT8*)winfo->ginfo.guid;
990                                 sbuf_printf(&sc->wmistat_sbuf,
991                                             "{%02X%02X%02X%02X-%02X%02X-"
992                                             "%02X%02X-%02X%02X-%02X%02X"
993                                             "%02X%02X%02X%02X} %3d %-5s",
994                                         guid[3], guid[2], guid[1], guid[0],
995                                         guid[5], guid[4],
996                                         guid[7], guid[6],
997                                         guid[8], guid[9],
998                                         guid[10], guid[11], guid[12],
999                                         guid[13], guid[14], guid[15],
1000                                         winfo->ginfo.max_instance,
1001                                         (winfo->ginfo.flags&
1002                                                 ACPI_WMI_REGFLAG_EXPENSIVE)?
1003                                                 "YES":"NO"
1004                                         );
1005                                 if (winfo->ginfo.flags&ACPI_WMI_REGFLAG_METHOD)
1006                                         sbuf_printf(&sc->wmistat_sbuf,
1007                                                     "WM%c%c ",
1008                                                     winfo->ginfo.oid[0],
1009                                                     winfo->ginfo.oid[1]);
1010                                 else
1011                                         sbuf_printf(&sc->wmistat_sbuf, "NO   ");
1012                                 sbuf_printf(&sc->wmistat_sbuf, "%-4s",
1013                                             (winfo->ginfo.flags&
1014                                             ACPI_WMI_REGFLAG_STRING)?"YES":"NO"
1015                                         );
1016                                 if (winfo->ginfo.flags&ACPI_WMI_REGFLAG_EVENT)
1017                                         sbuf_printf(&sc->wmistat_sbuf,
1018                                                     "0x%02X%s -\n",
1019                                                     (UINT8)winfo->ginfo.oid[0],
1020                                                     winfo->event_handler==NULL?
1021                                                     " ":"+");
1022                                 else
1023                                         sbuf_printf(&sc->wmistat_sbuf,
1024                                                     "NO    %c%c\n",
1025                                                     winfo->ginfo.oid[0],
1026                                                     winfo->ginfo.oid[1]);
1027                         }
1028                         sbuf_finish(&sc->wmistat_sbuf);
1029                 }
1030                 if (sbuf_len(&sc->wmistat_sbuf) <= 0) {
1031                         sbuf_delete(&sc->wmistat_sbuf);
1032                         sc->wmistat_bufptr = -1;
1033                         sc->wmistat_open_pid = 0;
1034                         ret = ENOMEM;
1035                 } else {
1036                         l = min(buf->uio_resid, sbuf_len(&sc->wmistat_sbuf) -
1037                                     sc->wmistat_bufptr);
1038                         ret = (l > 0)?uiomove(sbuf_data(&sc->wmistat_sbuf) +
1039                                     sc->wmistat_bufptr, l, buf) : 0;
1040                         sc->wmistat_bufptr += l;
1041                 }
1042         }
1043         ACPI_SERIAL_END(acpi_wmi);
1044
1045         return (ret);
1046 }