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