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