]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/acpi_support/acpi_wmi.c
MFV: r344447
[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
66 /*
67  * acpi_wmi driver private structure
68  */
69 struct acpi_wmi_softc {
70         device_t        wmi_dev;        /* wmi device id */
71         ACPI_HANDLE     wmi_handle;     /* handle of the PNP0C14 node */
72         device_t        ec_dev;         /* acpi_ec0 */
73         struct cdev     *wmistat_dev_t; /* wmistat device handle */
74         struct sbuf     wmistat_sbuf;   /* sbuf for /dev/wmistat output */
75         pid_t           wmistat_open_pid; /* pid operating on /dev/wmistat */
76         int             wmistat_bufptr; /* /dev/wmistat ptr to buffer position */
77         TAILQ_HEAD(wmi_info_list_head, wmi_info) wmi_info_list;
78 };
79
80 /*
81  * Struct that holds information about
82  * about a single GUID entry in _WDG
83  */
84 struct guid_info {
85         char    guid[16];       /* 16 byte non human readable GUID */
86         char    oid[2];         /* object id or event notify id (first byte) */
87         UINT8   max_instance;   /* highest instance known for this GUID */
88         UINT8   flags;          /* ACPI_WMI_REGFLAG_%s */
89 };
90
91 /* WExx event generation state (on/off) */
92 enum event_generation_state {
93         EVENT_GENERATION_ON = 1,
94         EVENT_GENERATION_OFF = 0
95 };
96
97
98 /*
99  * Information about one entry in _WDG.
100  * List of those is used to lookup information by GUID.
101  */
102 struct wmi_info {
103         TAILQ_ENTRY(wmi_info)   wmi_list;
104         struct guid_info        ginfo;          /* information on guid */
105         ACPI_NOTIFY_HANDLER     event_handler;/* client provided event handler */
106         void                    *event_handler_user_data; /* ev handler cookie  */
107 };
108
109
110 ACPI_SERIAL_DECL(acpi_wmi, "ACPI-WMI Mapping");
111
112 /* public interface - declaration */
113 /* standard device interface*/
114 static int              acpi_wmi_probe(device_t dev);
115 static int              acpi_wmi_attach(device_t dev);
116 static int              acpi_wmi_detach(device_t dev);
117 /* see acpi_wmi_if.m */
118 static int              acpi_wmi_provides_guid_string_method(device_t dev,
119                             const char *guid_string);
120 static ACPI_STATUS      acpi_wmi_evaluate_call_method(device_t dev,
121                             const char *guid_string, UINT8 instance,
122                             UINT32 method_id, const ACPI_BUFFER *in,
123                             ACPI_BUFFER *out);
124 static ACPI_STATUS      acpi_wmi_install_event_handler_method(device_t dev,
125                             const char *guid_string, ACPI_NOTIFY_HANDLER handler,
126                             void *data);
127 static ACPI_STATUS      acpi_wmi_remove_event_handler_method(device_t dev,
128                             const char *guid_string);
129 static ACPI_STATUS      acpi_wmi_get_event_data_method(device_t dev,
130                             UINT32 event_id, ACPI_BUFFER *out);
131 static ACPI_STATUS      acpi_wmi_get_block_method(device_t dev,
132                             const char *guid_string,
133                             UINT8 instance, ACPI_BUFFER *out);
134 static ACPI_STATUS      acpi_wmi_set_block_method(device_t dev,
135                             const char *guid_string,
136                             UINT8 instance, const ACPI_BUFFER *in);
137 /* private interface - declaration */
138 /* callbacks */
139 static void             acpi_wmi_notify_handler(ACPI_HANDLE h, UINT32 notify,
140                             void *context);
141 static ACPI_STATUS      acpi_wmi_ec_handler(UINT32 function,
142                             ACPI_PHYSICAL_ADDRESS address, UINT32 width,
143                             UINT64 *value, void *context,
144                             void *region_context);
145 /* helpers */
146 static ACPI_STATUS      acpi_wmi_read_wdg_blocks(struct acpi_wmi_softc *sc, ACPI_HANDLE h);
147 static ACPI_STATUS      acpi_wmi_toggle_we_event_generation(device_t dev,
148                             struct wmi_info *winfo,
149                             enum event_generation_state state);
150 static int              acpi_wmi_guid_string_to_guid(const UINT8 *guid_string,
151                             UINT8 *guid);
152 static struct wmi_info* acpi_wmi_lookup_wmi_info_by_guid_string(struct acpi_wmi_softc *sc,
153                             const char *guid_string);
154
155 static d_open_t acpi_wmi_wmistat_open;
156 static d_close_t acpi_wmi_wmistat_close;
157 static d_read_t acpi_wmi_wmistat_read;
158
159 /* handler /dev/wmistat device */
160 static struct cdevsw wmistat_cdevsw = {
161         .d_version = D_VERSION,
162         .d_open = acpi_wmi_wmistat_open,
163         .d_close = acpi_wmi_wmistat_close,
164         .d_read = acpi_wmi_wmistat_read,
165         .d_name = "wmistat",
166 };
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 (ret == 0) {
278                 bus_generic_probe(dev);
279                 ret = bus_generic_attach(dev);
280         }
281
282         return (ret);
283 }
284
285 /*
286  * Detach the driver by:
287  * - Removing notification handler
288  * - Removing address space handler
289  * - Turning off event generation for all WExx event activated by
290  *   child drivers
291  */
292 static int
293 acpi_wmi_detach(device_t dev)
294 {
295         struct wmi_info *winfo, *tmp;
296         struct acpi_wmi_softc *sc;
297         int ret;
298
299         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
300         sc = device_get_softc(dev);
301         ACPI_SERIAL_BEGIN(acpi_wmi);
302
303         if (sc->wmistat_open_pid != 0) {
304                 ret = EBUSY;
305         } else {
306                 AcpiRemoveNotifyHandler(sc->wmi_handle, ACPI_DEVICE_NOTIFY,
307                     acpi_wmi_notify_handler);
308                 AcpiRemoveAddressSpaceHandler(sc->wmi_handle,
309                     ACPI_ADR_SPACE_EC, acpi_wmi_ec_handler);
310                 TAILQ_FOREACH_SAFE(winfo, &sc->wmi_info_list, wmi_list, tmp) {
311                         if (winfo->event_handler)
312                                 acpi_wmi_toggle_we_event_generation(dev,
313                                     winfo, EVENT_GENERATION_OFF);
314                         TAILQ_REMOVE(&sc->wmi_info_list, winfo, wmi_list);
315                         free(winfo, M_ACPIWMI);
316                 }
317                 if (sc->wmistat_bufptr != -1) {
318                         sbuf_delete(&sc->wmistat_sbuf);
319                         sc->wmistat_bufptr = -1;
320                 }
321                 sc->wmistat_open_pid = 0;
322                 destroy_dev(sc->wmistat_dev_t);
323                 ret = 0;
324         }
325         ACPI_SERIAL_END(acpi_wmi);
326
327         return (ret);
328 }
329
330
331 /*
332  * Check if the given GUID string (human readable format
333  * AABBCCDD-EEFF-GGHH-IIJJ-KKLLMMNNOOPP)
334  * exists within _WDG
335  */
336 static int
337 acpi_wmi_provides_guid_string_method(device_t dev, const char *guid_string)
338 {
339         struct acpi_wmi_softc *sc;
340         struct wmi_info *winfo;
341         int ret;
342
343         sc = device_get_softc(dev);
344         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
345         ACPI_SERIAL_BEGIN(acpi_wmi);
346         winfo = acpi_wmi_lookup_wmi_info_by_guid_string(sc, guid_string);
347         ret = (winfo == NULL)?0:winfo->ginfo.max_instance+1;
348         ACPI_SERIAL_END(acpi_wmi);
349
350         return (ret);
351 }
352
353 /*
354  * Call a method "method_id" on the given GUID block
355  * write result into user provided output buffer
356  */
357 static ACPI_STATUS
358 acpi_wmi_evaluate_call_method(device_t dev, const char *guid_string,
359     UINT8 instance, UINT32 method_id, const ACPI_BUFFER *in, ACPI_BUFFER *out)
360 {
361         ACPI_OBJECT params[3];
362         ACPI_OBJECT_LIST input;
363         char method[5] = "WMxx";
364         struct wmi_info *winfo;
365         struct acpi_wmi_softc *sc;
366         ACPI_STATUS status;
367
368         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
369
370         sc = device_get_softc(dev);
371         ACPI_SERIAL_BEGIN(acpi_wmi);
372         if ((winfo = acpi_wmi_lookup_wmi_info_by_guid_string(sc, guid_string))
373                     == NULL)
374                 status = AE_NOT_FOUND;
375         else if (!(winfo->ginfo.flags & ACPI_WMI_REGFLAG_METHOD))
376                 status = AE_BAD_DATA;
377         else if (instance > winfo->ginfo.max_instance)
378                 status = AE_BAD_PARAMETER;
379         else {
380                 params[0].Type = ACPI_TYPE_INTEGER;
381                 params[0].Integer.Value = instance;
382                 params[1].Type = ACPI_TYPE_INTEGER;
383                 params[1].Integer.Value = method_id;
384                 input.Pointer = params;
385                 input.Count = 2;
386                 if (in) {
387                         params[2].Type =
388                             (winfo->ginfo.flags & ACPI_WMI_REGFLAG_STRING)
389                             ?ACPI_TYPE_STRING:ACPI_TYPE_BUFFER;
390                         params[2].Buffer.Length = in->Length;
391                         params[2].Buffer.Pointer = in->Pointer;
392                         input.Count = 3;
393                 }
394                 method[2] = winfo->ginfo.oid[0];
395                 method[3] = winfo->ginfo.oid[1];
396                 status = AcpiEvaluateObject(sc->wmi_handle, method,
397                             &input, out);
398         }
399         ACPI_SERIAL_END(acpi_wmi);
400
401         return (status);
402 }
403
404 /*
405  * Install a user provided event_handler on the given GUID
406  * provided *data will be passed on callback
407  * If there is already an existing event handler registered it will be silently
408  * discarded
409  */
410 static ACPI_STATUS
411 acpi_wmi_install_event_handler_method(device_t dev, const char *guid_string,
412     ACPI_NOTIFY_HANDLER event_handler, void *data)
413 {
414         struct acpi_wmi_softc *sc = device_get_softc(dev);
415         struct wmi_info *winfo;
416         ACPI_STATUS status;
417
418         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
419
420         status = AE_OK;
421         ACPI_SERIAL_BEGIN(acpi_wmi);
422         if (guid_string == NULL || event_handler == NULL)
423                 status = AE_BAD_PARAMETER;
424         else if ((winfo = acpi_wmi_lookup_wmi_info_by_guid_string(sc, guid_string))
425                     == NULL)
426                 status = AE_NOT_EXIST;
427         else if (winfo->event_handler != NULL ||
428                 (status = acpi_wmi_toggle_we_event_generation(dev, winfo,
429                     EVENT_GENERATION_ON)) == AE_OK) {
430                 winfo->event_handler = event_handler;
431                 winfo->event_handler_user_data = data;
432         }
433         ACPI_SERIAL_END(acpi_wmi);
434
435         return (status);
436 }
437
438 /*
439  * Remove a previously installed event handler from the given GUID
440  * If there was none installed, this call is silently discarded and
441  * reported as AE_OK
442  */
443 static ACPI_STATUS
444 acpi_wmi_remove_event_handler_method(device_t dev, const char *guid_string)
445 {
446         struct acpi_wmi_softc *sc = device_get_softc(dev);
447         struct wmi_info *winfo;
448         ACPI_STATUS status;
449
450         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
451
452         status = AE_OK;
453         ACPI_SERIAL_BEGIN(acpi_wmi);
454         if (guid_string &&
455             (winfo = acpi_wmi_lookup_wmi_info_by_guid_string(sc, guid_string))
456             != NULL && winfo->event_handler) {
457                 status = acpi_wmi_toggle_we_event_generation(dev, winfo,
458                             EVENT_GENERATION_OFF);
459                 winfo->event_handler = NULL;
460                 winfo->event_handler_user_data = NULL;
461         }
462         ACPI_SERIAL_END(acpi_wmi);
463
464         return (status);
465 }
466
467 /*
468  * Get details on an event received through a callback registered
469  * through ACPI_WMI_REMOVE_EVENT_HANDLER into a user provided output buffer.
470  * (event_id equals "notify" passed in the callback)
471  */
472 static ACPI_STATUS
473 acpi_wmi_get_event_data_method(device_t dev, UINT32 event_id, ACPI_BUFFER *out)
474 {
475         ACPI_OBJECT_LIST input;
476         ACPI_OBJECT params[1];
477         struct acpi_wmi_softc *sc;
478         struct wmi_info *winfo;
479         ACPI_STATUS status;
480
481         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
482
483         sc = device_get_softc(dev);
484         status = AE_NOT_FOUND;  
485         ACPI_SERIAL_BEGIN(acpi_wmi);
486         params[0].Type = ACPI_TYPE_INTEGER;
487         params[0].Integer.Value = event_id;
488         input.Pointer = params;
489         input.Count = 1;
490         TAILQ_FOREACH(winfo, &sc->wmi_info_list, wmi_list) {
491                 if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EVENT) &&
492                     ((UINT8) winfo->ginfo.oid[0] == event_id)) {
493                         status = AcpiEvaluateObject(sc->wmi_handle, "_WED",
494                                     &input, out);
495                         break;
496                 }
497         }
498         ACPI_SERIAL_END(acpi_wmi);
499
500         return (status);
501 }
502
503 /*
504  * Read a block of data from the given GUID (using WQxx (query))
505  * Will be returned in a user provided buffer (out).
506  * If the method is marked as expensive (ACPI_WMI_REGFLAG_EXPENSIVE)
507  * we will first call the WCxx control method to lock the node to
508  * lock the node for data collection and release it afterwards.
509  * (Failed WCxx calls are ignored to "support" broken implementations)
510  */
511 static ACPI_STATUS
512 acpi_wmi_get_block_method(device_t dev, const char *guid_string, UINT8 instance,
513         ACPI_BUFFER *out)
514 {
515         char wc_method[5] = "WCxx";
516         char wq_method[5] = "WQxx";
517         ACPI_OBJECT_LIST wc_input;
518         ACPI_OBJECT_LIST wq_input;
519         ACPI_OBJECT wc_params[1];
520         ACPI_OBJECT wq_params[1];
521         ACPI_HANDLE wc_handle;
522         struct acpi_wmi_softc *sc;
523         struct wmi_info *winfo;
524         ACPI_STATUS status;
525         ACPI_STATUS wc_status;
526
527         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
528
529         sc = device_get_softc(dev);
530         wc_status = AE_ERROR;
531         ACPI_SERIAL_BEGIN(acpi_wmi);
532         if (guid_string == NULL || out == NULL)
533                 status = AE_BAD_PARAMETER;
534         else if ((winfo = acpi_wmi_lookup_wmi_info_by_guid_string(sc, guid_string))
535                     == NULL)
536                 status = AE_ERROR;
537         else if (instance > winfo->ginfo.max_instance)
538                 status = AE_BAD_PARAMETER;
539         else if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EVENT) ||
540             (winfo->ginfo.flags & ACPI_WMI_REGFLAG_METHOD))
541                 status = AE_ERROR;
542         else {
543                 wq_params[0].Type = ACPI_TYPE_INTEGER;
544                 wq_params[0].Integer.Value = instance;
545                 wq_input.Pointer = wq_params;
546                 wq_input.Count = 1;
547                 if (winfo->ginfo.flags & ACPI_WMI_REGFLAG_EXPENSIVE) {
548                         wc_params[0].Type = ACPI_TYPE_INTEGER;
549                         wc_params[0].Integer.Value = 1;
550                         wc_input.Pointer = wc_params;
551                         wc_input.Count = 1;
552                         wc_method[2] = winfo->ginfo.oid[0];
553                         wc_method[3] = winfo->ginfo.oid[1];
554                         wc_status = AcpiGetHandle(sc->wmi_handle, wc_method,
555                                     &wc_handle);
556                         if (ACPI_SUCCESS(wc_status))
557                                 wc_status = AcpiEvaluateObject(wc_handle,
558                                                 wc_method, &wc_input, NULL);
559                 }
560                 wq_method[2] = winfo->ginfo.oid[0];
561                 wq_method[3] = winfo->ginfo.oid[1];
562                 status = AcpiEvaluateObject(sc->wmi_handle, wq_method,
563                             &wq_input, out);
564                 if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EXPENSIVE)
565                     && ACPI_SUCCESS(wc_status)) {
566                         wc_params[0].Integer.Value = 0;
567                         status = AcpiEvaluateObject(wc_handle, wc_method,
568                                     &wc_input, NULL);  /* XXX this might be
569                                                          the wrong status to
570                                                          return? */
571                 }
572         }
573         ACPI_SERIAL_END(acpi_wmi);
574
575         return (status);
576 }
577
578 /*
579  * Write a block of data to the given GUID (using WSxx)
580  */
581 static ACPI_STATUS
582 acpi_wmi_set_block_method(device_t dev, const char *guid_string, UINT8 instance,
583         const ACPI_BUFFER *in)
584 {
585         char method[5] = "WSxx";
586         ACPI_OBJECT_LIST input;
587         ACPI_OBJECT params[2];
588         struct wmi_info *winfo;
589         struct acpi_wmi_softc *sc;
590         ACPI_STATUS status;
591
592         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
593
594         sc = device_get_softc(dev);
595         ACPI_SERIAL_BEGIN(acpi_wmi);
596         if (guid_string == NULL || in == NULL)
597                 status = AE_BAD_DATA;
598         else if ((winfo = acpi_wmi_lookup_wmi_info_by_guid_string(sc, guid_string))
599                     == NULL)
600                 status = AE_ERROR;
601         else if (instance > winfo->ginfo.max_instance)
602                 status = AE_BAD_PARAMETER;
603         else if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EVENT) ||
604                     (winfo->ginfo.flags & ACPI_WMI_REGFLAG_METHOD))
605                 status = AE_ERROR;
606         else {
607                 params[0].Type = ACPI_TYPE_INTEGER;
608                 params[0].Integer.Value = instance;
609                 input.Pointer = params;
610                 input.Count = 2;
611                 params[1].Type = (winfo->ginfo.flags & ACPI_WMI_REGFLAG_STRING)
612                     ?ACPI_TYPE_STRING:ACPI_TYPE_BUFFER;
613                 params[1].Buffer.Length = in->Length;
614                 params[1].Buffer.Pointer = in->Pointer;
615                 method[2] = winfo->ginfo.oid[0];
616                 method[3] = winfo->ginfo.oid[1];
617                 status = AcpiEvaluateObject(sc->wmi_handle, method,
618                             &input, NULL);
619         }
620         ACPI_SERIAL_END(acpi_wmi);
621
622         return (status);
623 }
624
625 /*
626  * Handle events received and dispatch them to
627  * stakeholders that registered through ACPI_WMI_INSTALL_EVENT_HANDLER
628  */
629 static void
630 acpi_wmi_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
631 {
632         struct acpi_wmi_softc *sc = context;
633         ACPI_NOTIFY_HANDLER handler;
634         void *handler_data;
635         struct wmi_info *winfo;
636
637         ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
638
639         handler = NULL;
640         handler_data = NULL;
641         ACPI_SERIAL_BEGIN(acpi_wmi);
642         TAILQ_FOREACH(winfo, &sc->wmi_info_list, wmi_list) {
643                 if ((winfo->ginfo.flags & ACPI_WMI_REGFLAG_EVENT) &&
644                                 ((UINT8) winfo->ginfo.oid[0] == notify)) {
645                         if (winfo->event_handler) {
646                                 handler = winfo->event_handler;
647                                 handler_data = winfo->event_handler_user_data;
648                                 break;
649                         }
650                 }
651         }
652         ACPI_SERIAL_END(acpi_wmi);
653         if (handler) {
654                 handler(h, notify, handler_data);
655         }
656 }
657
658 /*
659  * Handle EC address space notifications reveived on the WDG node
660  * (this mimics EcAddressSpaceHandler in acpi_ec.c)
661  */
662 static ACPI_STATUS
663 acpi_wmi_ec_handler(UINT32 function, ACPI_PHYSICAL_ADDRESS address,
664     UINT32 width, UINT64 *value, void *context,
665     void *region_context)
666 {
667         struct acpi_wmi_softc *sc;
668         int i;
669         UINT64 ec_data;
670         UINT8 ec_addr;
671         ACPI_STATUS status;
672
673         ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)address);
674
675         sc = (struct acpi_wmi_softc *)context;
676         if (width % 8 != 0 || value == NULL || context == NULL)
677                 return (AE_BAD_PARAMETER);
678         if (address + (width / 8) - 1 > 0xFF)
679                 return (AE_BAD_ADDRESS);
680         if (function == ACPI_READ)
681                 *value = 0;
682         ec_addr = address;
683         status = AE_ERROR;
684
685         for (i = 0; i < width; i += 8, ++ec_addr) {
686                 switch (function) {
687                 case ACPI_READ:
688                         status = ACPI_EC_READ(sc->ec_dev, ec_addr, &ec_data, 1);
689                         if (ACPI_SUCCESS(status))
690                                 *value |= ((UINT64)ec_data) << i;
691                 break;
692                 case ACPI_WRITE:
693                         ec_data = (UINT8)((*value) >> i);
694                         status = ACPI_EC_WRITE(sc->ec_dev, ec_addr, ec_data, 1);
695                         break;
696                 default:
697                         device_printf(sc->wmi_dev,
698                             "invalid acpi_wmi_ec_handler function %d\n",
699                             function);
700                         status = AE_BAD_PARAMETER;
701                         break;
702                 }
703                 if (ACPI_FAILURE(status))
704                         break;
705         }
706
707         return (status);
708 }
709
710 /*
711  * Read GUID blocks from the _WDG node
712  * into wmi_info_list.
713  */
714 static ACPI_STATUS
715 acpi_wmi_read_wdg_blocks(struct acpi_wmi_softc *sc, ACPI_HANDLE h)
716 {
717         ACPI_BUFFER out = {ACPI_ALLOCATE_BUFFER, NULL};
718         struct guid_info *ginfo;
719         ACPI_OBJECT *obj;
720         struct wmi_info *winfo;
721         UINT32 i;
722         UINT32 wdg_block_count;
723         ACPI_STATUS status;
724
725         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
726
727         ACPI_SERIAL_ASSERT(acpi_wmi);
728         if (ACPI_FAILURE(status = AcpiEvaluateObject(h, "_WDG", NULL, &out)))
729                 return (status);
730         obj = (ACPI_OBJECT*) out.Pointer;
731         wdg_block_count = obj->Buffer.Length / sizeof(struct guid_info);
732         if ((ginfo = malloc(obj->Buffer.Length, M_ACPIWMI, M_NOWAIT))
733                     == NULL) {
734                 AcpiOsFree(out.Pointer);
735                 return (AE_NO_MEMORY);
736         }
737         memcpy(ginfo, obj->Buffer.Pointer, obj->Buffer.Length);
738         for (i = 0; i < wdg_block_count; ++i) {
739                 if ((winfo = malloc(sizeof(struct wmi_info), M_ACPIWMI,
740                             M_NOWAIT | M_ZERO)) == NULL) {
741                         AcpiOsFree(out.Pointer);
742                         free(ginfo, M_ACPIWMI);
743                         return (AE_NO_MEMORY);
744                 }
745                 winfo->ginfo = ginfo[i];
746                 TAILQ_INSERT_TAIL(&sc->wmi_info_list, winfo, wmi_list);
747         }
748         AcpiOsFree(out.Pointer);
749         free(ginfo, M_ACPIWMI);
750
751         return (status);
752 }
753
754 /*
755  * Toggle event generation in for the given GUID (passed by winfo)
756  * Turn on to get notified (through acpi_wmi_notify_handler) if events happen
757  * on the given GUID.
758  */
759 static ACPI_STATUS
760 acpi_wmi_toggle_we_event_generation(device_t dev, struct wmi_info *winfo,
761     enum event_generation_state state)
762 {
763         char method[5] = "WExx";
764         ACPI_OBJECT_LIST input;
765         ACPI_OBJECT params[1];
766         struct acpi_wmi_softc *sc;
767         ACPI_STATUS status;
768
769         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
770
771         sc = device_get_softc(dev);
772         ACPI_SERIAL_ASSERT(acpi_wmi);
773         params[0].Type = ACPI_TYPE_INTEGER;
774         params[0].Integer.Value = state==EVENT_GENERATION_ON?1:0;
775         input.Pointer = params;
776         input.Count = 1;
777         
778         UINT8 hi = ((UINT8) winfo->ginfo.oid[0]) >> 4;
779         UINT8 lo = ((UINT8) winfo->ginfo.oid[0]) & 0xf;
780         method[2] = (hi > 9 ? hi + 55: hi + 48);
781         method[3] = (lo > 9 ? lo + 55: lo + 48);
782         status = AcpiEvaluateObject(sc->wmi_handle, method, &input, NULL);
783         if (status == AE_NOT_FOUND) status = AE_OK;
784
785         return (status);
786 }
787
788 /*
789  * Convert given two digit hex string (hexin) to an UINT8 referenced
790  * by byteout.
791  * Return != 0 if the was a problem (invalid input)
792  */
793 static __inline int acpi_wmi_hex_to_int(const UINT8 *hexin, UINT8 *byteout)
794 {
795         unsigned int hi;
796         unsigned int lo;
797
798         hi = hexin[0];
799         lo = hexin[1];
800         if ('0' <= hi && hi <= '9')
801                 hi -= '0';
802         else if ('A' <= hi && hi <= 'F')
803                 hi -= ('A' - 10);
804         else if ('a' <= hi && hi <= 'f')
805                 hi -= ('a' - 10);
806         else
807                 return (1);
808         if ('0' <= lo && lo <= '9')
809                 lo -= '0';
810         else if ('A' <= lo && lo <= 'F')
811                 lo -= ('A' - 10);
812         else if ('a' <= lo && lo <= 'f')
813                 lo -= ('a' - 10);
814         else
815                 return (1);
816         *byteout = (hi << 4) + lo;
817
818         return (0);
819 }
820
821 /*
822  * Convert a human readable 36 character GUID into a 16byte
823  * machine readable one.
824  * The basic algorithm looks as follows:
825  * Input:  AABBCCDD-EEFF-GGHH-IIJJ-KKLLMMNNOOPP
826  * Output: DCBAFEHGIJKLMNOP
827  * (AA BB CC etc. represent two digit hex numbers == bytes)
828  * Return != 0 if passed guid string is invalid
829  */
830 static int
831 acpi_wmi_guid_string_to_guid(const UINT8 *guid_string, UINT8 *guid)
832 {
833         static const int mapping[20] = {3, 2, 1, 0, -1, 5, 4, -1, 7, 6, -1,
834             8, 9, -1, 10, 11, 12, 13, 14, 15};
835         int i;
836
837         for (i = 0; i < 20; ++i, ++guid_string) {
838                 if (mapping[i] >= 0) {
839                         if (acpi_wmi_hex_to_int(guid_string,
840                             &guid[mapping[i]]))
841                                 return (-1);
842                         ++guid_string;
843                 } else if (*guid_string != '-')
844                         return (-1);
845         }
846
847         return (0);
848 }
849
850 /*
851  * Lookup a wmi_info structure in wmi_list based on a
852  * human readable GUID
853  * Return NULL if the GUID is unknown in the _WDG
854  */
855 static struct wmi_info*
856 acpi_wmi_lookup_wmi_info_by_guid_string(struct acpi_wmi_softc *sc, const char *guid_string)
857 {
858         char guid[16];
859         struct wmi_info *winfo;
860
861         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
862
863         ACPI_SERIAL_ASSERT(acpi_wmi);
864
865         if (!acpi_wmi_guid_string_to_guid(guid_string, guid)) {
866                 TAILQ_FOREACH(winfo, &sc->wmi_info_list, wmi_list) {
867                         if (!memcmp(winfo->ginfo.guid, guid, 16)) {
868                                 return (winfo);
869                         }
870                 }
871         }
872
873         return (NULL);
874 }
875
876 /*
877  * open wmistat device
878  */
879 static int
880 acpi_wmi_wmistat_open(struct cdev* dev, int flags, int mode, struct thread *td)
881 {
882         struct acpi_wmi_softc *sc;
883         int ret;
884
885         if (dev == NULL || dev->si_drv1 == NULL)
886                 return (EBADF);
887         sc = dev->si_drv1;
888
889         ACPI_SERIAL_BEGIN(acpi_wmi);
890         if (sc->wmistat_open_pid != 0) {
891                 ret = EBUSY;
892         }
893         else {
894                 if (sbuf_new(&sc->wmistat_sbuf, NULL, 4096, SBUF_AUTOEXTEND)
895                             == NULL) {
896                         ret = ENXIO;
897                 } else {
898                         sc->wmistat_open_pid = td->td_proc->p_pid;
899                         sc->wmistat_bufptr = 0;
900                         ret = 0;
901                 }
902         }
903         ACPI_SERIAL_END(acpi_wmi);
904
905         return (ret);
906 }
907
908 /*
909  * close wmistat device
910  */
911 static int
912 acpi_wmi_wmistat_close(struct cdev* dev, int flags, int mode,
913     struct thread *td)
914 {
915         struct acpi_wmi_softc *sc;
916         int ret;
917
918         if (dev == NULL || dev->si_drv1 == NULL)
919                 return (EBADF);
920         sc = dev->si_drv1;
921
922         ACPI_SERIAL_BEGIN(acpi_wmi);
923         if (sc->wmistat_open_pid == 0) {
924                 ret = EBADF;
925         }
926         else {
927                 if (sc->wmistat_bufptr != -1) {
928                         sbuf_delete(&sc->wmistat_sbuf);
929                         sc->wmistat_bufptr = -1;
930                 }
931                 sc->wmistat_open_pid = 0;
932                 ret = 0;
933         }
934         ACPI_SERIAL_END(acpi_wmi);
935
936         return (ret);
937 }
938
939 /*
940  * Read from wmistat guid information
941  */
942 static int
943 acpi_wmi_wmistat_read(struct cdev *dev, struct uio *buf, int flag)
944 {
945         struct acpi_wmi_softc *sc;
946         struct wmi_info *winfo;
947         int l;
948         int ret;
949         UINT8* guid;
950
951         if (dev == NULL || dev->si_drv1 == NULL)
952                 return (EBADF);
953         sc = dev->si_drv1;
954         
955         ACPI_SERIAL_BEGIN(acpi_wmi);
956         if (sc->wmistat_open_pid != buf->uio_td->td_proc->p_pid ||
957                         sc->wmistat_bufptr == -1) {
958                 ret = EBADF;
959         }
960         else {
961                 if (!sbuf_done(&sc->wmistat_sbuf)) {
962                         sbuf_printf(&sc->wmistat_sbuf, "GUID                 "
963                                     "                 INST EXPE METH STR "
964                                     "EVENT OID\n");
965                         TAILQ_FOREACH(winfo, &sc->wmi_info_list, wmi_list) {
966                                 guid = (UINT8*)winfo->ginfo.guid;
967                                 sbuf_printf(&sc->wmistat_sbuf,
968                                             "{%02X%02X%02X%02X-%02X%02X-"
969                                             "%02X%02X-%02X%02X-%02X%02X"
970                                             "%02X%02X%02X%02X} %3d %-5s",
971                                         guid[3], guid[2], guid[1], guid[0],
972                                         guid[5], guid[4],
973                                         guid[7], guid[6],
974                                         guid[8], guid[9],
975                                         guid[10], guid[11], guid[12],
976                                         guid[13], guid[14], guid[15],
977                                         winfo->ginfo.max_instance,
978                                         (winfo->ginfo.flags&
979                                                 ACPI_WMI_REGFLAG_EXPENSIVE)?
980                                                 "YES":"NO"
981                                         );
982                                 if (winfo->ginfo.flags&ACPI_WMI_REGFLAG_METHOD)
983                                         sbuf_printf(&sc->wmistat_sbuf,
984                                                     "WM%c%c ",
985                                                     winfo->ginfo.oid[0],
986                                                     winfo->ginfo.oid[1]);
987                                 else
988                                         sbuf_printf(&sc->wmistat_sbuf, "NO   ");
989                                 sbuf_printf(&sc->wmistat_sbuf, "%-4s",
990                                             (winfo->ginfo.flags&
991                                             ACPI_WMI_REGFLAG_STRING)?"YES":"NO"
992                                         );
993                                 if (winfo->ginfo.flags&ACPI_WMI_REGFLAG_EVENT)
994                                         sbuf_printf(&sc->wmistat_sbuf,
995                                                     "0x%02X%s -\n",
996                                                     (UINT8)winfo->ginfo.oid[0],
997                                                     winfo->event_handler==NULL?
998                                                     " ":"+");
999                                 else
1000                                         sbuf_printf(&sc->wmistat_sbuf,
1001                                                     "NO    %c%c\n",
1002                                                     winfo->ginfo.oid[0],
1003                                                     winfo->ginfo.oid[1]);
1004                         }
1005                         sbuf_finish(&sc->wmistat_sbuf);
1006                 }
1007                 if (sbuf_len(&sc->wmistat_sbuf) <= 0) {
1008                         sbuf_delete(&sc->wmistat_sbuf);
1009                         sc->wmistat_bufptr = -1;
1010                         sc->wmistat_open_pid = 0;
1011                         ret = ENOMEM;
1012                 } else {
1013                         l = min(buf->uio_resid, sbuf_len(&sc->wmistat_sbuf) -
1014                                     sc->wmistat_bufptr);
1015                         ret = (l > 0)?uiomove(sbuf_data(&sc->wmistat_sbuf) +
1016                                     sc->wmistat_bufptr, l, buf) : 0;
1017                         sc->wmistat_bufptr += l;
1018                 }
1019         }
1020         ACPI_SERIAL_END(acpi_wmi);
1021
1022         return (ret);
1023 }