Merge branch 'vendor/BIND' into bind_vendor2
[dragonfly.git] / sys / dev / acpica5 / acpi_hp / acpi_hp.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  * $FreeBSD: src/sys/dev/acpi_support/acpi_hp.c,v 1.3 2009/07/03 21:12:37 rpaulo
26  */
27
28 #include <sys/cdefs.h>
29
30 /*
31  * Driver for extra ACPI-controlled features found on HP laptops
32  * that use a WMI enabled BIOS (e.g. HP Compaq 8510p and 6510p).
33  * Allows to control and read status of integrated hardware and read
34  * BIOS settings through CMI.
35  * Inspired by the hp-wmi driver, which implements a subset of these
36  * features (hotkeys) on Linux.
37  *
38  * HP CMI whitepaper:
39  *     http://h20331.www2.hp.com/Hpsub/downloads/cmi_whitepaper.pdf
40  * wmi-hp for Linux:
41  *     http://www.kernel.org
42  * WMI and ACPI:
43  *     http://www.microsoft.com/whdc/system/pnppwr/wmi/wmi-acpi.mspx
44  */
45
46 #include "opt_acpi.h"
47 #include <sys/param.h>
48 #include <sys/conf.h>
49 #include <sys/uio.h>
50 #include <sys/proc.h>
51 #include <sys/kernel.h>
52 #include <sys/bus.h>
53 #include <sys/sbuf.h>
54 #include <sys/module.h>
55 #include <sys/sysctl.h>
56 #include <sys/device.h>
57
58 #include "acpi.h"
59 #include "accommon.h"
60 #include <dev/acpica5/acpivar.h>
61 #include "acpi_wmi_if.h"
62
63 #define _COMPONENT      ACPI_OEM
64 ACPI_MODULE_NAME("HP")
65
66 #define ACPI_HP_WMI_EVENT_GUID          "95F24279-4D7B-4334-9387-ACCDC67EF61C"
67 #define ACPI_HP_WMI_BIOS_GUID           "5FB7F034-2C63-45E9-BE91-3D44E2C707E4"
68 #define ACPI_HP_WMI_CMI_GUID            "2D114B49-2DFB-4130-B8FE-4A3C09E75133"
69
70 #define ACPI_HP_WMI_DISPLAY_COMMAND     0x1
71 #define ACPI_HP_WMI_HDDTEMP_COMMAND     0x2
72 #define ACPI_HP_WMI_ALS_COMMAND         0x3
73 #define ACPI_HP_WMI_DOCK_COMMAND        0x4
74 #define ACPI_HP_WMI_WIRELESS_COMMAND    0x5
75
76 #define ACPI_HP_METHOD_WLAN_ENABLED                     1
77 #define ACPI_HP_METHOD_WLAN_RADIO                       2
78 #define ACPI_HP_METHOD_WLAN_ON_AIR                      3
79 #define ACPI_HP_METHOD_WLAN_ENABLE_IF_RADIO_ON          4
80 #define ACPI_HP_METHOD_WLAN_DISABLE_IF_RADIO_OFF        5
81 #define ACPI_HP_METHOD_BLUETOOTH_ENABLED                6
82 #define ACPI_HP_METHOD_BLUETOOTH_RADIO                  7
83 #define ACPI_HP_METHOD_BLUETOOTH_ON_AIR                 8
84 #define ACPI_HP_METHOD_BLUETOOTH_ENABLE_IF_RADIO_ON     9
85 #define ACPI_HP_METHOD_BLUETOOTH_DISABLE_IF_RADIO_OFF   10
86 #define ACPI_HP_METHOD_WWAN_ENABLED                     11
87 #define ACPI_HP_METHOD_WWAN_RADIO                       12
88 #define ACPI_HP_METHOD_WWAN_ON_AIR                      13
89 #define ACPI_HP_METHOD_WWAN_ENABLE_IF_RADIO_ON          14
90 #define ACPI_HP_METHOD_WWAN_DISABLE_IF_RADIO_OFF        15
91 #define ACPI_HP_METHOD_ALS                              16
92 #define ACPI_HP_METHOD_DISPLAY                          17
93 #define ACPI_HP_METHOD_HDDTEMP                          18
94 #define ACPI_HP_METHOD_DOCK                             19
95 #define ACPI_HP_METHOD_CMI_DETAIL                       20
96 #define ACPI_HP_METHOD_VERBOSE                          21
97
98 #define HP_MASK_WWAN_ON_AIR                     0x1000000
99 #define HP_MASK_BLUETOOTH_ON_AIR                0x10000
100 #define HP_MASK_WLAN_ON_AIR                     0x100
101 #define HP_MASK_WWAN_RADIO                      0x8000000
102 #define HP_MASK_BLUETOOTH_RADIO                 0x80000
103 #define HP_MASK_WLAN_RADIO                      0x800
104 #define HP_MASK_WWAN_ENABLED                    0x2000000
105 #define HP_MASK_BLUETOOTH_ENABLED               0x20000
106 #define HP_MASK_WLAN_ENABLED                    0x200
107
108 #define ACPI_HP_CMI_DETAIL_PATHS                0x01
109 #define ACPI_HP_CMI_DETAIL_ENUMS                0x02
110 #define ACPI_HP_CMI_DETAIL_FLAGS                0x04
111 #define ACPI_HP_CMI_DETAIL_SHOW_MAX_INSTANCE    0x08
112
113 #define sbuf_done(s)            ((s)->s_flags & SBUF_FINISHED)
114
115 struct acpi_hp_inst_seq_pair {
116         UINT32  sequence;       /* sequence number as suggested by cmi bios */
117         UINT8   instance;       /* object instance on guid */
118 };
119
120 struct acpi_hp_softc {
121         device_t        dev;
122         ACPI_HANDLE     handle;
123         device_t        wmi_dev;
124         int             has_notify;             /* notification GUID found */
125         int             has_cmi;                /* CMI GUID found */
126         int             cmi_detail;             /* CMI detail level
127                                                    (set by sysctl) */
128         int             verbose;                /* add debug output */
129         int             wlan_enable_if_radio_on;        /* set by sysctl */
130         int             wlan_disable_if_radio_off;      /* set by sysctl */
131         int             bluetooth_enable_if_radio_on;   /* set by sysctl */
132         int             bluetooth_disable_if_radio_off; /* set by sysctl */
133         int             wwan_enable_if_radio_on;        /* set by sysctl */
134         int             wwan_disable_if_radio_off;      /* set by sysctl */
135         int             was_wlan_on_air;                /* last known WLAN
136                                                            on air status */
137         int             was_bluetooth_on_air;           /* last known BT
138                                                            on air status */
139         int             was_wwan_on_air;                /* last known WWAN
140                                                            on air status */
141         struct sysctl_ctx_list  sysctl_ctx;
142         struct sysctl_oid       *sysctl_tree;
143         struct cdev     *hpcmi_dev_t;           /* hpcmi device handle */
144         struct sbuf     hpcmi_sbuf;             /* /dev/hpcmi output sbuf */
145         pid_t           hpcmi_open_pid;         /* pid operating on
146                                                    /dev/hpcmi */
147         int             hpcmi_bufptr;           /* current pointer position
148                                                    in /dev/hpcmi output buffer
149                                                  */
150         int             cmi_order_size;         /* size of cmi_order list */
151         struct acpi_hp_inst_seq_pair cmi_order[128];    /* list of CMI
152                              instances ordered by BIOS suggested sequence */
153 };
154
155 static struct {
156         char    *name;
157         int     method;
158         char    *description;
159         int     access;
160 } acpi_hp_sysctls[] = {
161         {
162                 .name           = "wlan_enabled",
163                 .method         = ACPI_HP_METHOD_WLAN_ENABLED,
164                 .description    = "Enable/Disable WLAN (WiFi)",
165                 .access         = CTLTYPE_INT | CTLFLAG_RW
166         },
167         {
168                 .name           = "wlan_radio",
169                 .method         = ACPI_HP_METHOD_WLAN_RADIO,
170                 .description    = "WLAN radio status",
171                 .access         = CTLTYPE_INT | CTLFLAG_RD
172         },
173         {
174                 .name           = "wlan_on_air",
175                 .method         = ACPI_HP_METHOD_WLAN_ON_AIR,
176                 .description    = "WLAN radio ready to use (enabled and radio)",
177                 .access         = CTLTYPE_INT | CTLFLAG_RD
178         },
179         {
180                 .name           = "wlan_enable_if_radio_on",
181                 .method         = ACPI_HP_METHOD_WLAN_ENABLE_IF_RADIO_ON,
182                 .description    = "Enable WLAN if radio is turned on",
183                 .access         = CTLTYPE_INT | CTLFLAG_RW
184         },
185         {
186                 .name           = "wlan_disable_if_radio_off",
187                 .method         = ACPI_HP_METHOD_WLAN_DISABLE_IF_RADIO_OFF,
188                 .description    = "Disable WLAN if radio is turned off",
189                 .access         = CTLTYPE_INT | CTLFLAG_RW
190         },
191         {
192                 .name           = "bt_enabled",
193                 .method         = ACPI_HP_METHOD_BLUETOOTH_ENABLED,
194                 .description    = "Enable/Disable Bluetooth",
195                 .access         = CTLTYPE_INT | CTLFLAG_RW
196         },
197         {
198                 .name           = "bt_radio",
199                 .method         = ACPI_HP_METHOD_BLUETOOTH_RADIO,
200                 .description    = "Bluetooth radio status",
201                 .access         = CTLTYPE_INT | CTLFLAG_RD
202         },
203         {
204                 .name           = "bt_on_air",
205                 .method         = ACPI_HP_METHOD_BLUETOOTH_ON_AIR,
206                 .description    = "Bluetooth radio ready to use"
207                                     " (enabled and radio)",
208                 .access         = CTLTYPE_INT | CTLFLAG_RD
209         },
210         {
211                 .name           = "bt_enable_if_radio_on",
212                 .method         = ACPI_HP_METHOD_BLUETOOTH_ENABLE_IF_RADIO_ON,
213                 .description    = "Enable bluetooth if radio is turned on",
214                 .access         = CTLTYPE_INT | CTLFLAG_RW
215         },
216         {
217                 .name           = "bt_disable_if_radio_off",
218                 .method         = ACPI_HP_METHOD_BLUETOOTH_DISABLE_IF_RADIO_OFF,
219                 .description    = "Disable bluetooth if radio is turned off",
220                 .access         = CTLTYPE_INT | CTLFLAG_RW
221         },
222         {
223                 .name           = "wwan_enabled",
224                 .method         = ACPI_HP_METHOD_WWAN_ENABLED,
225                 .description    = "Enable/Disable WWAN (UMTS)",
226                 .access         = CTLTYPE_INT | CTLFLAG_RW
227         },
228         {
229                 .name           = "wwan_radio",
230                 .method         = ACPI_HP_METHOD_WWAN_RADIO,
231                 .description    = "WWAN radio status",
232                 .access         = CTLTYPE_INT | CTLFLAG_RD
233         },
234         {
235                 .name           = "wwan_on_air",
236                 .method         = ACPI_HP_METHOD_WWAN_ON_AIR,
237                 .description    = "WWAN radio ready to use (enabled and radio)",
238                 .access         = CTLTYPE_INT | CTLFLAG_RD
239         },
240         {
241                 .name           = "wwan_enable_if_radio_on",
242                 .method         = ACPI_HP_METHOD_WWAN_ENABLE_IF_RADIO_ON,
243                 .description    = "Enable WWAN if radio is turned on",
244                 .access         = CTLTYPE_INT | CTLFLAG_RW
245         },
246         {
247                 .name           = "wwan_disable_if_radio_off",
248                 .method         = ACPI_HP_METHOD_WWAN_DISABLE_IF_RADIO_OFF,
249                 .description    = "Disable WWAN if radio is turned off",
250                 .access         = CTLTYPE_INT | CTLFLAG_RW
251         },
252         {
253                 .name           = "als_enabled",
254                 .method         = ACPI_HP_METHOD_ALS,
255                 .description    = "Enable/Disable ALS (Ambient light sensor)",
256                 .access         = CTLTYPE_INT | CTLFLAG_RW
257         },
258         {
259                 .name           = "display",
260                 .method         = ACPI_HP_METHOD_DISPLAY,
261                 .description    = "Display status",
262                 .access         = CTLTYPE_INT | CTLFLAG_RD
263         },
264         {
265                 .name           = "hdd_temperature",
266                 .method         = ACPI_HP_METHOD_HDDTEMP,
267                 .description    = "HDD temperature",
268                 .access         = CTLTYPE_INT | CTLFLAG_RD
269         },
270         {
271                 .name           = "is_docked",
272                 .method         = ACPI_HP_METHOD_DOCK,
273                 .description    = "Docking station status",
274                 .access         = CTLTYPE_INT | CTLFLAG_RD
275         },
276         {
277                 .name           = "cmi_detail",
278                 .method         = ACPI_HP_METHOD_CMI_DETAIL,
279                 .description    = "Details shown in CMI output "
280                                     "(cat /dev/hpcmi)",
281                 .access         = CTLTYPE_INT | CTLFLAG_RW
282         },
283         {
284                 .name           = "verbose",
285                 .method         = ACPI_HP_METHOD_VERBOSE,
286                 .description    = "Verbosity level",
287                 .access         = CTLTYPE_INT | CTLFLAG_RW
288         },
289
290         { NULL, 0, NULL, 0 }
291 };
292
293 ACPI_SERIAL_DECL(hp, "HP ACPI-WMI Mapping");
294
295 static int      acpi_hp_probe(device_t dev);
296 static int      acpi_hp_attach(device_t dev);
297 static int      acpi_hp_detach(device_t dev);
298
299 static void     acpi_hp_evaluate_auto_on_off(struct acpi_hp_softc* sc);
300 static int      acpi_hp_sysctl(SYSCTL_HANDLER_ARGS);
301 static int      acpi_hp_sysctl_set(struct acpi_hp_softc *sc, int method,
302                     int arg, int oldarg);
303 static int      acpi_hp_sysctl_get(struct acpi_hp_softc *sc, int method);
304 static int      acpi_hp_exec_wmi_command(device_t wmi_dev, int command,
305                     int is_write, int val);
306 static void     acpi_hp_notify(ACPI_HANDLE h, UINT32 notify, void *context);
307 static int      acpi_hp_get_cmi_block(device_t wmi_dev, const char* guid,
308                     UINT8 instance, char* outbuf, size_t outsize,
309                     UINT32* sequence, int detail);
310 static void     acpi_hp_hex_decode(char* buffer);
311
312 #if defined (__FreeBSD__)
313 static d_open_t acpi_hp_hpcmi_open;
314 static d_close_t acpi_hp_hpcmi_close;
315 static d_read_t acpi_hp_hpcmi_read;
316 #else 
317 static int acpi_hp_hpcmi_open(struct dev_open_args *ap);
318 static int acpi_hp_hpcmi_close(struct dev_close_args *ap);
319 static int acpi_hp_hpcmi_read(struct dev_read_args *ap);
320 #endif
321
322 /* handler /dev/hpcmi device */
323 struct dev_ops hpcmi_ops = {
324         { .name = "hpcmi" },
325         .d_open = acpi_hp_hpcmi_open,
326         .d_close = acpi_hp_hpcmi_close,
327         .d_read = acpi_hp_hpcmi_read,
328 };
329
330 static device_method_t acpi_hp_methods[] = {
331         DEVMETHOD(device_probe, acpi_hp_probe),
332         DEVMETHOD(device_attach, acpi_hp_attach),
333         DEVMETHOD(device_detach, acpi_hp_detach),
334         {0, 0}
335 };
336
337 static driver_t acpi_hp_driver = {
338         "acpi_hp",
339         acpi_hp_methods,
340         sizeof(struct acpi_hp_softc),
341 };
342
343 static devclass_t acpi_hp_devclass;
344
345 DRIVER_MODULE(acpi_hp, acpi, acpi_hp_driver, acpi_hp_devclass,
346                 0, 0);
347 MODULE_DEPEND(acpi_hp, acpi_wmi, 1, 1, 1);
348 MODULE_DEPEND(acpi_hp, acpi, 1, 1, 1);
349
350 static void     
351 acpi_hp_evaluate_auto_on_off(struct acpi_hp_softc *sc)
352 {
353         int     wireless;
354         int     new_wlan_status;
355         int     new_bluetooth_status;
356         int     new_wwan_status;
357
358         wireless = acpi_hp_exec_wmi_command(sc->wmi_dev,
359                     ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0);
360         new_wlan_status = -1;
361         new_bluetooth_status = -1;
362         new_wwan_status = -1;
363
364         if (sc->verbose)
365                 device_printf(sc->wmi_dev, "Wireless status is %x\n", wireless);
366         if (sc->wlan_disable_if_radio_off && !(wireless & HP_MASK_WLAN_RADIO)
367             &&  (wireless & HP_MASK_WLAN_ENABLED)) {
368                 acpi_hp_exec_wmi_command(sc->wmi_dev,
369                     ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x100);
370                 new_wlan_status = 0;
371         }
372         else if (sc->wlan_enable_if_radio_on && (wireless & HP_MASK_WLAN_RADIO)
373                 &&  !(wireless & HP_MASK_WLAN_ENABLED)) {
374                 acpi_hp_exec_wmi_command(sc->wmi_dev,
375                     ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x101);
376                 new_wlan_status = 1;
377         }
378         if (sc->bluetooth_disable_if_radio_off &&
379             !(wireless & HP_MASK_BLUETOOTH_RADIO) &&
380             (wireless & HP_MASK_BLUETOOTH_ENABLED)) {
381                 acpi_hp_exec_wmi_command(sc->wmi_dev,
382                     ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x200);
383                 new_bluetooth_status = 0;
384         }
385         else if (sc->bluetooth_enable_if_radio_on &&
386                 (wireless & HP_MASK_BLUETOOTH_RADIO) &&
387                 !(wireless & HP_MASK_BLUETOOTH_ENABLED)) {
388                 acpi_hp_exec_wmi_command(sc->wmi_dev,
389                     ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x202);
390                 new_bluetooth_status = 1;
391         }
392         if (sc->wwan_disable_if_radio_off &&
393             !(wireless & HP_MASK_WWAN_RADIO) &&
394             (wireless & HP_MASK_WWAN_ENABLED)) {
395                 acpi_hp_exec_wmi_command(sc->wmi_dev,
396                 ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x400);
397                 new_wwan_status = 0;
398         }
399         else if (sc->wwan_enable_if_radio_on &&
400                 (wireless & HP_MASK_WWAN_RADIO) &&
401                 !(wireless & HP_MASK_WWAN_ENABLED)) {
402                 acpi_hp_exec_wmi_command(sc->wmi_dev,
403                     ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x404);
404                 new_wwan_status = 1;
405         }
406
407         if (new_wlan_status == -1) {
408                 new_wlan_status = (wireless & HP_MASK_WLAN_ON_AIR);
409                 if ((new_wlan_status?1:0) != sc->was_wlan_on_air) {
410                         sc->was_wlan_on_air = sc->was_wlan_on_air?0:1;
411                         if (sc->verbose)
412                                 device_printf(sc->wmi_dev,
413                                     "WLAN on air changed to %i "
414                                     "(new_wlan_status is %i)\n",
415                                     sc->was_wlan_on_air, new_wlan_status);
416                         acpi_UserNotify("HP", sc->handle,
417                             0xc0+sc->was_wlan_on_air);
418                 }
419         }
420         if (new_bluetooth_status == -1) {
421                 new_bluetooth_status = (wireless & HP_MASK_BLUETOOTH_ON_AIR);
422                 if ((new_bluetooth_status?1:0) != sc->was_bluetooth_on_air) {
423                         sc->was_bluetooth_on_air = sc->was_bluetooth_on_air?
424                             0:1;
425                         if (sc->verbose)
426                                 device_printf(sc->wmi_dev,
427                                     "BLUETOOTH on air changed"
428                                     " to %i (new_bluetooth_status is %i)\n",
429                                     sc->was_bluetooth_on_air,
430                                     new_bluetooth_status);
431                         acpi_UserNotify("HP", sc->handle,
432                             0xd0+sc->was_bluetooth_on_air);
433                 }
434         }
435         if (new_wwan_status == -1) {
436                 new_wwan_status = (wireless & HP_MASK_WWAN_ON_AIR);
437                 if ((new_wwan_status?1:0) != sc->was_wwan_on_air) {
438                         sc->was_wwan_on_air = sc->was_wwan_on_air?0:1;
439                         if (sc->verbose)
440                                 device_printf(sc->wmi_dev,
441                                     "WWAN on air changed to %i"
442                                     " (new_wwan_status is %i)\n",
443                                     sc->was_wwan_on_air, new_wwan_status);
444                         acpi_UserNotify("HP", sc->handle,
445                             0xe0+sc->was_wwan_on_air);
446                 }
447         }
448 }
449
450 static int
451 acpi_hp_probe(device_t dev)
452 {
453         if (acpi_disabled("hp") || device_get_unit(dev) != 0)
454                 return (ENXIO);
455         device_set_desc(dev, "HP ACPI-WMI Mapping");
456
457         return (0);
458 }
459
460 static int
461 acpi_hp_attach(device_t dev)
462 {
463         struct acpi_hp_softc    *sc;
464         struct acpi_softc       *acpi_sc;
465         devclass_t              wmi_devclass;
466         int                     arg;
467
468         ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
469
470         sc = device_get_softc(dev);
471         sc->dev = dev;
472         sc->handle = acpi_get_handle(dev);
473         sc->has_notify = 0;
474         sc->has_cmi = 0;
475         sc->bluetooth_enable_if_radio_on = 0;
476         sc->bluetooth_disable_if_radio_off = 0;
477         sc->wlan_enable_if_radio_on = 0;
478         sc->wlan_disable_if_radio_off = 0;
479         sc->wlan_enable_if_radio_on = 0;
480         sc->wlan_disable_if_radio_off = 0;
481         sc->was_wlan_on_air = 0;
482         sc->was_bluetooth_on_air = 0;
483         sc->was_wwan_on_air = 0;
484         sc->cmi_detail = 0;
485         sc->cmi_order_size = -1;
486         sc->verbose = 0;
487         memset(sc->cmi_order, 0, sizeof(sc->cmi_order));
488         acpi_sc = acpi_device_get_parent_softc(dev);
489
490         if (!(wmi_devclass = devclass_find ("acpi_wmi"))) {
491                 device_printf(dev, "Couldn't find acpi_wmi devclass\n");
492                 return (EINVAL);
493         }
494         if (!(sc->wmi_dev = devclass_get_device(wmi_devclass, 0))) {
495                 device_printf(dev, "Couldn't find acpi_wmi device\n");
496                 return (EINVAL);
497         }
498         if (!ACPI_WMI_PROVIDES_GUID_STRING(sc->wmi_dev,
499             ACPI_HP_WMI_BIOS_GUID)) {
500                 device_printf(dev,
501                     "WMI device does not provide the HP BIOS GUID\n");
502                 return (EINVAL);
503         }
504         if (ACPI_WMI_PROVIDES_GUID_STRING(sc->wmi_dev,
505             ACPI_HP_WMI_EVENT_GUID)) {
506                 device_printf(dev,
507                     "HP event GUID detected, installing event handler\n");
508                 if (ACPI_WMI_INSTALL_EVENT_HANDLER(sc->wmi_dev,
509                     ACPI_HP_WMI_EVENT_GUID, acpi_hp_notify, dev)) {
510                         device_printf(dev,
511                             "Could not install notification handler!\n");
512                 }
513                 else {
514                         sc->has_notify = 1;
515                 }
516         }
517         if ((sc->has_cmi = 
518             ACPI_WMI_PROVIDES_GUID_STRING(sc->wmi_dev, ACPI_HP_WMI_CMI_GUID)
519             )) {
520                 device_printf(dev, "HP CMI GUID detected\n");
521         }
522
523         if (sc->has_cmi) {
524                 sc->hpcmi_dev_t = make_dev(&hpcmi_ops, 0, UID_ROOT,
525                             GID_WHEEL, 0644, "hpcmi");
526                 sc->hpcmi_dev_t->si_drv1 = sc;
527                 sc->hpcmi_open_pid = 0;
528                 sc->hpcmi_bufptr = -1;
529         }
530
531         ACPI_SERIAL_BEGIN(hp);
532
533         sysctl_ctx_init(&sc->sysctl_ctx);
534         sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
535                 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO,
536                 "hp", CTLFLAG_RD, 0, "");
537
538         for (int i = 0; acpi_hp_sysctls[i].name != NULL; ++i) {
539                 arg = 0;
540                 if ((!sc->has_notify &&
541                     (acpi_hp_sysctls[i].method ==
542                         ACPI_HP_METHOD_WLAN_ENABLE_IF_RADIO_ON ||
543                     acpi_hp_sysctls[i].method ==
544                         ACPI_HP_METHOD_WLAN_DISABLE_IF_RADIO_OFF ||
545                     acpi_hp_sysctls[i].method ==
546                         ACPI_HP_METHOD_BLUETOOTH_ENABLE_IF_RADIO_ON ||
547                     acpi_hp_sysctls[i].method ==
548                         ACPI_HP_METHOD_BLUETOOTH_DISABLE_IF_RADIO_OFF ||
549                     acpi_hp_sysctls[i].method ==
550                         ACPI_HP_METHOD_WWAN_ENABLE_IF_RADIO_ON ||
551                     acpi_hp_sysctls[i].method ==
552                         ACPI_HP_METHOD_WWAN_DISABLE_IF_RADIO_OFF)) ||
553                     (arg = acpi_hp_sysctl_get(sc,
554                     acpi_hp_sysctls[i].method)) < 0) {
555                         continue;
556                 }
557                 if (acpi_hp_sysctls[i].method == ACPI_HP_METHOD_WLAN_ON_AIR) {
558                         sc->was_wlan_on_air = arg;
559                 }
560                 else if (acpi_hp_sysctls[i].method ==
561                             ACPI_HP_METHOD_BLUETOOTH_ON_AIR) {
562                         sc->was_bluetooth_on_air = arg;
563                 }
564                 else if (acpi_hp_sysctls[i].method ==
565                             ACPI_HP_METHOD_WWAN_ON_AIR) {
566                         sc->was_wwan_on_air = arg;
567                 }
568
569                 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
570                 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
571                         acpi_hp_sysctls[i].name, acpi_hp_sysctls[i].access,
572                         sc, i, acpi_hp_sysctl, "I",
573                         acpi_hp_sysctls[i].description);
574         }
575         ACPI_SERIAL_END(hp);
576
577         return (0);
578 }
579
580 static int
581 acpi_hp_detach(device_t dev)
582 {
583         int     ret;
584         
585         ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
586         struct acpi_hp_softc *sc = device_get_softc(dev);
587         if (sc->has_cmi && sc->hpcmi_open_pid != 0) {
588                 ret = EBUSY;
589         }
590         else {
591                 if (sc->has_notify) {
592                         ACPI_WMI_REMOVE_EVENT_HANDLER(dev,
593                             ACPI_HP_WMI_EVENT_GUID);
594                 }
595                 if (sc->hpcmi_bufptr != -1) {
596                         sbuf_delete(&sc->hpcmi_sbuf);
597                         sc->hpcmi_bufptr = -1;
598                 }
599                 sc->hpcmi_open_pid = 0;
600                 destroy_dev(sc->hpcmi_dev_t);
601                 ret = 0;
602         }
603
604         return (ret);
605 }
606
607 static int
608 acpi_hp_sysctl(SYSCTL_HANDLER_ARGS)
609 {
610         struct acpi_hp_softc    *sc;
611         int                     arg;
612         int                     oldarg;
613         int                     error = 0;
614         int                     function;
615         int                     method;
616         
617         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
618
619         sc = (struct acpi_hp_softc *)oidp->oid_arg1;
620         function = oidp->oid_arg2;
621         method = acpi_hp_sysctls[function].method;
622
623         ACPI_SERIAL_BEGIN(hp);
624         arg = acpi_hp_sysctl_get(sc, method);
625         oldarg = arg;
626         error = sysctl_handle_int(oidp, &arg, 0, req);
627         if (!error && req->newptr != NULL) {
628                 error = acpi_hp_sysctl_set(sc, method, arg, oldarg);
629         }
630         ACPI_SERIAL_END(hp);
631
632         return (error);
633 }
634
635 static int
636 acpi_hp_sysctl_get(struct acpi_hp_softc *sc, int method)
637 {
638         int     val = 0;
639
640         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
641         ACPI_SERIAL_ASSERT(hp);
642
643         switch (method) {
644         case ACPI_HP_METHOD_WLAN_ENABLED:
645                 val = acpi_hp_exec_wmi_command(sc->wmi_dev,
646                         ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0);
647                 val = ((val & HP_MASK_WLAN_ENABLED) != 0);
648                 break;
649         case ACPI_HP_METHOD_WLAN_RADIO:
650                 val = acpi_hp_exec_wmi_command(sc->wmi_dev,
651                         ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0);
652                 val = ((val & HP_MASK_WLAN_RADIO) != 0);
653                 break;
654         case ACPI_HP_METHOD_WLAN_ON_AIR:
655                 val = acpi_hp_exec_wmi_command(sc->wmi_dev,
656                         ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0);
657                 val = ((val & HP_MASK_WLAN_ON_AIR) != 0);
658                 break;
659         case ACPI_HP_METHOD_WLAN_ENABLE_IF_RADIO_ON:
660                 val = sc->wlan_enable_if_radio_on;
661                 break;
662         case ACPI_HP_METHOD_WLAN_DISABLE_IF_RADIO_OFF:
663                 val = sc->wlan_disable_if_radio_off;
664                 break;
665         case ACPI_HP_METHOD_BLUETOOTH_ENABLED:
666                 val = acpi_hp_exec_wmi_command(sc->wmi_dev,
667                         ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0);
668                 val = ((val & HP_MASK_BLUETOOTH_ENABLED) != 0);
669                 break;
670         case ACPI_HP_METHOD_BLUETOOTH_RADIO:
671                 val = acpi_hp_exec_wmi_command(sc->wmi_dev,
672                         ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0);
673                 val = ((val & HP_MASK_BLUETOOTH_RADIO) != 0);
674                 break;
675         case ACPI_HP_METHOD_BLUETOOTH_ON_AIR:
676                 val = acpi_hp_exec_wmi_command(sc->wmi_dev,
677                         ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0);
678                 val = ((val & HP_MASK_BLUETOOTH_ON_AIR) != 0);
679                 break;
680         case ACPI_HP_METHOD_BLUETOOTH_ENABLE_IF_RADIO_ON:
681                 val = sc->bluetooth_enable_if_radio_on;
682                 break;
683         case ACPI_HP_METHOD_BLUETOOTH_DISABLE_IF_RADIO_OFF:
684                 val = sc->bluetooth_disable_if_radio_off;
685                 break;
686         case ACPI_HP_METHOD_WWAN_ENABLED:
687                 val = acpi_hp_exec_wmi_command(sc->wmi_dev,
688                         ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0);
689                 val = ((val & HP_MASK_WWAN_ENABLED) != 0);
690                 break;
691         case ACPI_HP_METHOD_WWAN_RADIO:
692                 val = acpi_hp_exec_wmi_command(sc->wmi_dev,
693                         ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0);
694                 val = ((val & HP_MASK_WWAN_RADIO) != 0);
695                 break;
696         case ACPI_HP_METHOD_WWAN_ON_AIR:
697                 val = acpi_hp_exec_wmi_command(sc->wmi_dev,
698                         ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0);
699                 val = ((val & HP_MASK_WWAN_ON_AIR) != 0);
700                 break;
701         case ACPI_HP_METHOD_WWAN_ENABLE_IF_RADIO_ON:
702                 val = sc->wwan_enable_if_radio_on;
703                 break;
704         case ACPI_HP_METHOD_WWAN_DISABLE_IF_RADIO_OFF:
705                 val = sc->wwan_disable_if_radio_off;
706                 break;
707         case ACPI_HP_METHOD_ALS:
708                 val = acpi_hp_exec_wmi_command(sc->wmi_dev,
709                         ACPI_HP_WMI_ALS_COMMAND, 0, 0);
710                 break;
711         case ACPI_HP_METHOD_DISPLAY:
712                 val = acpi_hp_exec_wmi_command(sc->wmi_dev,
713                         ACPI_HP_WMI_DISPLAY_COMMAND, 0, 0);
714                 break;
715         case ACPI_HP_METHOD_HDDTEMP:
716                 val = acpi_hp_exec_wmi_command(sc->wmi_dev,
717                         ACPI_HP_WMI_HDDTEMP_COMMAND, 0, 0);
718                 break;
719         case ACPI_HP_METHOD_DOCK:
720                 val = acpi_hp_exec_wmi_command(sc->wmi_dev,
721                         ACPI_HP_WMI_DOCK_COMMAND, 0, 0);
722                 break;
723         case ACPI_HP_METHOD_CMI_DETAIL:
724                 val = sc->cmi_detail;
725                 break;
726         case ACPI_HP_METHOD_VERBOSE:
727                 val = sc->verbose;
728                 break;
729         }
730
731         return (val);
732 }
733
734 static int
735 acpi_hp_sysctl_set(struct acpi_hp_softc *sc, int method, int arg, int oldarg)
736 {
737         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
738         ACPI_SERIAL_ASSERT(hp);
739
740         if (method != ACPI_HP_METHOD_CMI_DETAIL &&
741             method != ACPI_HP_METHOD_VERBOSE)
742                 arg = arg?1:0;
743
744         if (arg != oldarg) {
745                 switch (method) {
746                 case ACPI_HP_METHOD_WLAN_ENABLED:
747                         return (acpi_hp_exec_wmi_command(sc->wmi_dev,
748                                     ACPI_HP_WMI_WIRELESS_COMMAND, 1,
749                                     arg?0x101:0x100));
750                 case ACPI_HP_METHOD_WLAN_ENABLE_IF_RADIO_ON:
751                         sc->wlan_enable_if_radio_on = arg;
752                         acpi_hp_evaluate_auto_on_off(sc);
753                         break;
754                 case ACPI_HP_METHOD_WLAN_DISABLE_IF_RADIO_OFF:
755                         sc->wlan_disable_if_radio_off = arg;
756                         acpi_hp_evaluate_auto_on_off(sc);
757                         break;
758                 case ACPI_HP_METHOD_BLUETOOTH_ENABLED:
759                         return (acpi_hp_exec_wmi_command(sc->wmi_dev,
760                                     ACPI_HP_WMI_WIRELESS_COMMAND, 1,
761                                     arg?0x202:0x200));
762                 case ACPI_HP_METHOD_BLUETOOTH_ENABLE_IF_RADIO_ON:
763                         sc->bluetooth_enable_if_radio_on = arg;
764                         acpi_hp_evaluate_auto_on_off(sc);
765                         break;
766                 case ACPI_HP_METHOD_BLUETOOTH_DISABLE_IF_RADIO_OFF:
767                         sc->bluetooth_disable_if_radio_off = arg?1:0;
768                         acpi_hp_evaluate_auto_on_off(sc);
769                         break;
770                 case ACPI_HP_METHOD_WWAN_ENABLED:
771                         return (acpi_hp_exec_wmi_command(sc->wmi_dev,
772                                     ACPI_HP_WMI_WIRELESS_COMMAND, 1,
773                                     arg?0x404:0x400));
774                 case ACPI_HP_METHOD_WWAN_ENABLE_IF_RADIO_ON:
775                         sc->wwan_enable_if_radio_on = arg?1:0;
776                         acpi_hp_evaluate_auto_on_off(sc);
777                         break;
778                 case ACPI_HP_METHOD_WWAN_DISABLE_IF_RADIO_OFF:
779                         sc->wwan_disable_if_radio_off = arg?1:0;
780                         acpi_hp_evaluate_auto_on_off(sc);
781                         break;
782                 case ACPI_HP_METHOD_ALS:
783                         return (acpi_hp_exec_wmi_command(sc->wmi_dev,
784                                     ACPI_HP_WMI_ALS_COMMAND, 1,
785                                     arg?1:0));
786                 case ACPI_HP_METHOD_CMI_DETAIL:
787                         sc->cmi_detail = arg;
788                         if ((arg & ACPI_HP_CMI_DETAIL_SHOW_MAX_INSTANCE) != 
789                             (oldarg & ACPI_HP_CMI_DETAIL_SHOW_MAX_INSTANCE)) {
790                             sc->cmi_order_size = -1;
791                         }
792                         break;
793                 case ACPI_HP_METHOD_VERBOSE:
794                         sc->verbose = arg;
795                         break;
796                 }
797         }
798
799         return (0);
800 }
801
802 static __inline void
803 acpi_hp_free_buffer(ACPI_BUFFER* buf) {
804         if (buf && buf->Pointer) {
805                 AcpiOsFree(buf->Pointer);
806         }
807 }
808
809 static void
810 acpi_hp_notify(ACPI_HANDLE h, UINT32 notify, void *context)
811 {
812         device_t dev = context;
813         ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
814
815         struct acpi_hp_softc *sc = device_get_softc(dev);
816         ACPI_BUFFER response = { ACPI_ALLOCATE_BUFFER, NULL };
817         ACPI_OBJECT *obj;
818         ACPI_WMI_GET_EVENT_DATA(sc->wmi_dev, notify, &response);
819         obj = (ACPI_OBJECT*) response.Pointer;
820         if (obj && obj->Type == ACPI_TYPE_BUFFER && obj->Buffer.Length == 8) {
821                 if (*((UINT8 *) obj->Buffer.Pointer) == 0x5) {
822                         acpi_hp_evaluate_auto_on_off(sc);
823                 }
824         }
825         acpi_hp_free_buffer(&response);
826 }
827
828 static int
829 acpi_hp_exec_wmi_command(device_t wmi_dev, int command, int is_write, int val)
830 {
831         UINT32          params[5] = { 0x55434553,
832                             is_write?2:1,
833                             command,
834                             is_write?4:0,
835                             val};
836         UINT32*         result;
837         ACPI_OBJECT     *obj;
838         ACPI_BUFFER     in = { sizeof(params), &params };
839         ACPI_BUFFER     out = { ACPI_ALLOCATE_BUFFER, NULL };
840         int retval;
841         
842         if (ACPI_FAILURE(ACPI_WMI_EVALUATE_CALL(wmi_dev, ACPI_HP_WMI_BIOS_GUID,
843                     0, 0x3, &in, &out))) {
844                 acpi_hp_free_buffer(&out);
845                 return (-EINVAL);
846         }
847         obj = out.Pointer;
848         if (!obj || obj->Type != ACPI_TYPE_BUFFER) {
849                 acpi_hp_free_buffer(&out);
850                 return (-EINVAL);
851         }
852         result = (UINT32*) obj->Buffer.Pointer;
853         retval = result[2];
854         if (result[1] > 0) {
855                 retval = result[1];
856         }
857         acpi_hp_free_buffer(&out);
858
859         return (retval);
860 }
861
862 static __inline char*
863 acpi_hp_get_string_from_object(ACPI_OBJECT* obj, char* dst, size_t size) {
864         int     length;
865
866         dst[0] = 0;
867         if (obj->Type == ACPI_TYPE_STRING) {
868                 length = obj->String.Length+1;
869                 if (length > size) {
870                         length = size - 1;
871                 }
872                 strlcpy(dst, obj->String.Pointer, length);
873                 acpi_hp_hex_decode(dst);
874         }
875
876         return (dst);
877 }
878
879
880 /*
881  * Read BIOS Setting block in instance "instance".
882  * The block returned is ACPI_TYPE_PACKAGE which should contain the following
883  * elements:
884  * Index Meaning
885  * 0        Setting Name [string]
886  * 1        Value (comma separated, asterisk marks the current value) [string]
887  * 2        Path within the bios hierarchy [string]
888  * 3        IsReadOnly [int]
889  * 4        DisplayInUI [int]
890  * 5        RequiresPhysicalPresence [int]
891  * 6        Sequence for ordering within the bios settings (absolute) [int]
892  * 7        Length of prerequisites array [int]
893  * 8..8+[7] PrerequisiteN [string]
894  * 9+[7]    Current value (in case of enum) [string] / Array length [int]
895  * 10+[7]   Enum length [int] / Array values
896  * 11+[7]ff Enum value at index x [string]
897  */
898 static int
899 acpi_hp_get_cmi_block(device_t wmi_dev, const char* guid, UINT8 instance,
900     char* outbuf, size_t outsize, UINT32* sequence, int detail)
901 {
902         ACPI_OBJECT     *obj;
903         ACPI_BUFFER     out = { ACPI_ALLOCATE_BUFFER, NULL };
904         int             i;
905         int             outlen;
906         int             size = 255;
907         int             has_enums = 0;
908         int             valuebase = 0;
909         char            string_buffer[size];
910         int             enumbase;
911
912         outlen = 0;
913         outbuf[0] = 0;  
914         if (ACPI_FAILURE(ACPI_WMI_GET_BLOCK(wmi_dev, guid, instance, &out))) {
915                 acpi_hp_free_buffer(&out);
916                 return (-EINVAL);
917         }
918         obj = out.Pointer;
919         if (!obj && obj->Type != ACPI_TYPE_PACKAGE) {
920                 acpi_hp_free_buffer(&out);
921                 return (-EINVAL);
922         }
923
924         if (obj->Package.Count >= 8 &&
925             obj->Package.Elements[7].Type == ACPI_TYPE_INTEGER) {
926             valuebase = 8 + obj->Package.Elements[7].Integer.Value;
927         }
928
929         /* check if this matches our expectations based on limited knowledge */
930         if (valuebase > 7 && obj->Package.Count > valuebase + 1 &&
931             obj->Package.Elements[0].Type == ACPI_TYPE_STRING &&
932             obj->Package.Elements[1].Type == ACPI_TYPE_STRING &&
933             obj->Package.Elements[2].Type == ACPI_TYPE_STRING &&
934             obj->Package.Elements[3].Type == ACPI_TYPE_INTEGER &&
935             obj->Package.Elements[4].Type == ACPI_TYPE_INTEGER &&
936             obj->Package.Elements[5].Type == ACPI_TYPE_INTEGER &&
937             obj->Package.Elements[6].Type == ACPI_TYPE_INTEGER &&
938             obj->Package.Elements[valuebase].Type == ACPI_TYPE_STRING &&
939             obj->Package.Elements[valuebase+1].Type == ACPI_TYPE_INTEGER &&
940             obj->Package.Count > valuebase + 
941                 obj->Package.Elements[valuebase+1].Integer.Value
942            ) {
943                 enumbase = valuebase + 1;
944                 if (detail & ACPI_HP_CMI_DETAIL_PATHS) {
945                         strlcat(outbuf, acpi_hp_get_string_from_object(
946                                 &obj->Package.Elements[2], string_buffer, size),
947                                 outsize);
948                         outlen += 48;
949                         while (strlen(outbuf) < outlen)
950                                 strlcat(outbuf, " ", outsize);
951                 }
952                 strlcat(outbuf, acpi_hp_get_string_from_object(
953                                 &obj->Package.Elements[0], string_buffer, size),
954                                 outsize);
955                 outlen += 43;
956                 while (strlen(outbuf) < outlen)
957                         strlcat(outbuf, " ", outsize);
958                 strlcat(outbuf, acpi_hp_get_string_from_object(
959                                 &obj->Package.Elements[valuebase], string_buffer, 
960                                 size),
961                                 outsize);
962                 outlen += 21;
963                 while (strlen(outbuf) < outlen)
964                         strlcat(outbuf, " ", outsize);
965                 for (i = 0; i < strlen(outbuf); ++i)
966                         if (outbuf[i] == '\\')
967                                 outbuf[i] = '/';
968                 if (detail & ACPI_HP_CMI_DETAIL_ENUMS) {
969                         for (i = enumbase + 1; i < enumbase + 1 +
970                             obj->Package.Elements[enumbase].Integer.Value;
971                             ++i) {
972                                 acpi_hp_get_string_from_object(
973                                     &obj->Package.Elements[i], string_buffer,
974                                     size);
975                                 if (strlen(string_buffer) > 1 ||
976                                     (strlen(string_buffer) == 1 &&
977                                     string_buffer[0] != ' ')) {
978                                         if (has_enums)
979                                                 strlcat(outbuf, "/", outsize);
980                                         else
981                                                 strlcat(outbuf, " (", outsize);
982                                         strlcat(outbuf, string_buffer, outsize);
983                                         has_enums = 1;
984                                 }
985                         }
986                 }
987                 if (has_enums)
988                         strlcat(outbuf, ")", outsize);
989                 if (detail & ACPI_HP_CMI_DETAIL_FLAGS) {
990                         strlcat(outbuf, obj->Package.Elements[3].Integer.Value?
991                             " [ReadOnly]":"", outsize);
992                         strlcat(outbuf, obj->Package.Elements[4].Integer.Value?
993                             "":" [NOUI]", outsize);
994                         strlcat(outbuf, obj->Package.Elements[5].Integer.Value?
995                             " [RPP]":"", outsize);
996                 }
997                 *sequence = (UINT32) obj->Package.Elements[6].Integer.Value;
998         }
999         acpi_hp_free_buffer(&out);
1000
1001         return (0);
1002 }
1003
1004
1005
1006 /*
1007  * Convert given two digit hex string (hexin) to an UINT8 referenced
1008  * by byteout.
1009  * Return != 0 if the was a problem (invalid input)
1010  */
1011 static __inline int acpi_hp_hex_to_int(const UINT8 *hexin, UINT8 *byteout)
1012 {
1013         unsigned int    hi;
1014         unsigned int    lo;
1015
1016         hi = hexin[0];
1017         lo = hexin[1];
1018         if ('0' <= hi && hi <= '9')
1019                 hi -= '0';
1020         else if ('A' <= hi && hi <= 'F')
1021                 hi -= ('A' - 10);
1022         else if ('a' <= hi && hi <= 'f')
1023                 hi -= ('a' - 10);
1024         else
1025                 return (1);
1026         if ('0' <= lo && lo <= '9')
1027                 lo -= '0';
1028         else if ('A' <= lo && lo <= 'F')
1029                 lo -= ('A' - 10);
1030         else if ('a' <= lo && lo <= 'f')
1031                 lo -= ('a' - 10);
1032         else
1033                 return (1);
1034         *byteout = (hi << 4) + lo;
1035
1036         return (0);
1037 }
1038
1039
1040 static void
1041 acpi_hp_hex_decode(char* buffer)
1042 {
1043         int     i;
1044         int     length = strlen(buffer);
1045         UINT8   *uin;
1046         UINT8   uout;
1047
1048         if (((int)length/2)*2 == length || length < 10) return;
1049
1050         for (i = 0; i<length; ++i) {
1051                 if (!((i+1)%3)) {
1052                         if (buffer[i] != ' ')
1053                                 return;
1054                 }
1055                 else
1056                         if (!((buffer[i] >= '0' && buffer[i] <= '9') ||
1057                             (buffer[i] >= 'A' && buffer[i] <= 'F')))
1058                                 return;                 
1059         }
1060
1061         for (i = 0; i<length; i += 3) {
1062                 uin = &buffer[i];
1063                 uout = 0;
1064                 acpi_hp_hex_to_int(uin, &uout);
1065                 buffer[i/3] = (char) uout;
1066         }
1067         buffer[(length+1)/3] = 0;
1068 }
1069
1070
1071 /*
1072  * open hpcmi device
1073  */
1074 static int
1075 acpi_hp_hpcmi_open(struct dev_open_args *ap)
1076 {
1077         struct acpi_hp_softc    *sc;
1078         int                     ret;
1079         struct cdev *dev = ap->a_head.a_dev;
1080         struct thread *td = curthread;
1081
1082         if (dev == NULL || dev->si_drv1 == NULL)
1083                 return (EBADF);
1084         sc = dev->si_drv1;
1085
1086         ACPI_SERIAL_BEGIN(hp);
1087         if (sc->hpcmi_open_pid != 0) {
1088                 ret = EBUSY;
1089         }
1090         else {
1091                 if (sbuf_new(&sc->hpcmi_sbuf, NULL, 4096, SBUF_AUTOEXTEND)
1092                     == NULL) {
1093                         ret = ENXIO;
1094                 } else {
1095                         sc->hpcmi_open_pid = td->td_proc->p_pid;
1096                         sc->hpcmi_bufptr = 0;
1097                         ret = 0;
1098                 }
1099         }
1100         ACPI_SERIAL_END(hp);
1101
1102         return (ret);
1103 }
1104
1105 /*
1106  * close hpcmi device
1107  */
1108 static int
1109 acpi_hp_hpcmi_close(struct dev_close_args *ap)
1110 {
1111         struct acpi_hp_softc    *sc;
1112         int                     ret;
1113         struct cdev *dev = ap->a_head.a_dev;
1114
1115         if (dev == NULL || dev->si_drv1 == NULL)
1116                 return (EBADF);
1117         sc = dev->si_drv1;
1118
1119         ACPI_SERIAL_BEGIN(hp);
1120         if (sc->hpcmi_open_pid == 0) {
1121                 ret = EBADF;
1122         }
1123         else {
1124                 if (sc->hpcmi_bufptr != -1) {
1125                         sbuf_delete(&sc->hpcmi_sbuf);
1126                         sc->hpcmi_bufptr = -1;
1127                 }
1128                 sc->hpcmi_open_pid = 0;
1129                 ret = 0;
1130         }
1131         ACPI_SERIAL_END(hp);
1132
1133         return (ret);
1134 }
1135
1136 /*
1137  * Read from hpcmi bios information
1138  */
1139 static int
1140 acpi_hp_hpcmi_read(struct dev_read_args *ap)
1141 {
1142         struct acpi_hp_softc    *sc;
1143         int                     pos, i, l, ret;
1144         UINT8                   instance;
1145         UINT8                   maxInstance;
1146         UINT32                  sequence;
1147         int                     linesize = 1025;
1148         char                    line[linesize];
1149         struct cdev *dev = ap->a_head.a_dev;
1150         struct uio *buf = ap->a_uio;
1151
1152         if (dev == NULL || dev->si_drv1 == NULL)
1153                 return (EBADF);
1154         sc = dev->si_drv1;
1155         
1156         ACPI_SERIAL_BEGIN(hp);
1157         if (sc->hpcmi_open_pid != buf->uio_td->td_proc->p_pid
1158             || sc->hpcmi_bufptr == -1) {
1159                 ret = EBADF;
1160         }
1161         else {
1162                 if (!sbuf_done(&sc->hpcmi_sbuf)) {
1163                         if (sc->cmi_order_size < 0) {
1164                                 maxInstance = sc->has_cmi;
1165                                 if (!(sc->cmi_detail & 
1166                                     ACPI_HP_CMI_DETAIL_SHOW_MAX_INSTANCE) &&
1167                                     maxInstance > 0) {
1168                                         maxInstance--;
1169                                 }
1170                                 sc->cmi_order_size = 0;
1171                                 for (instance = 0; instance < maxInstance;
1172                                     ++instance) {
1173                                         if (acpi_hp_get_cmi_block(sc->wmi_dev,
1174                                                 ACPI_HP_WMI_CMI_GUID, instance,
1175                                                 line, linesize, &sequence,
1176                                                 sc->cmi_detail)) {
1177                                                 instance = maxInstance;
1178                                         }
1179                                         else {
1180                                                 pos = sc->cmi_order_size;
1181                                                 for (i=0;
1182                                                   i<sc->cmi_order_size && i<127;
1183                                                      ++i) {
1184                                 if (sc->cmi_order[i].sequence > sequence) {
1185                                                                 pos = i;
1186                                                                 break;                                                  
1187                                                         }
1188                                                 }
1189                                                 for (i=sc->cmi_order_size;
1190                                                     i>pos;
1191                                                     --i) {
1192                                                 sc->cmi_order[i].sequence =
1193                                                     sc->cmi_order[i-1].sequence;
1194                                                 sc->cmi_order[i].instance =
1195                                                     sc->cmi_order[i-1].instance;
1196                                                 }
1197                                                 sc->cmi_order[pos].sequence =
1198                                                     sequence;
1199                                                 sc->cmi_order[pos].instance =
1200                                                     instance;
1201                                                 sc->cmi_order_size++;
1202                                         }
1203                                 }
1204                         }
1205                         for (i=0; i<sc->cmi_order_size; ++i) {
1206                                 if (!acpi_hp_get_cmi_block(sc->wmi_dev,
1207                                     ACPI_HP_WMI_CMI_GUID,
1208                                     sc->cmi_order[i].instance, line, linesize,
1209                                     &sequence, sc->cmi_detail)) {
1210                                         sbuf_printf(&sc->hpcmi_sbuf, "%s\n", line);
1211                                 }
1212                         }
1213                         sbuf_finish(&sc->hpcmi_sbuf);
1214                 }
1215                 if (sbuf_len(&sc->hpcmi_sbuf) <= 0) {
1216                         sbuf_delete(&sc->hpcmi_sbuf);
1217                         sc->hpcmi_bufptr = -1;
1218                         sc->hpcmi_open_pid = 0;
1219                         ret = ENOMEM;
1220                 } else {
1221                         l = min(buf->uio_resid, sbuf_len(&sc->hpcmi_sbuf) -
1222                             sc->hpcmi_bufptr);
1223                         ret = (l > 0)?uiomove(sbuf_data(&sc->hpcmi_sbuf) +
1224                             sc->hpcmi_bufptr, l, buf) : 0;
1225                         sc->hpcmi_bufptr += l;
1226                 }
1227         }
1228         ACPI_SERIAL_END(hp);
1229
1230         return (ret);
1231 }