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