xref: /freebsd-src/sys/dev/acpi_support/acpi_hp.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1df849145SRui Paulo /*-
2df849145SRui Paulo  * Copyright (c) 2009 Michael Gmelin <freebsd@grem.de>
3df849145SRui Paulo  * All rights reserved.
4df849145SRui Paulo  *
5df849145SRui Paulo  * Redistribution and use in source and binary forms, with or without
6df849145SRui Paulo  * modification, are permitted provided that the following conditions
7df849145SRui Paulo  * are met:
8df849145SRui Paulo  * 1. Redistributions of source code must retain the above copyright
9df849145SRui Paulo  *    notice, this list of conditions and the following disclaimer.
10df849145SRui Paulo  * 2. Redistributions in binary form must reproduce the above copyright
11df849145SRui Paulo  *    notice, this list of conditions and the following disclaimer in the
12df849145SRui Paulo  *    documentation and/or other materials provided with the distribution.
13df849145SRui Paulo  *
14df849145SRui Paulo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15df849145SRui Paulo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16df849145SRui Paulo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17df849145SRui Paulo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18df849145SRui Paulo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19df849145SRui Paulo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20df849145SRui Paulo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21df849145SRui Paulo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22df849145SRui Paulo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23df849145SRui Paulo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24df849145SRui Paulo  * SUCH DAMAGE.
25df849145SRui Paulo  */
26df849145SRui Paulo 
27df849145SRui Paulo #include <sys/cdefs.h>
28df849145SRui Paulo /*
29df849145SRui Paulo  * Driver for extra ACPI-controlled features found on HP laptops
30df849145SRui Paulo  * that use a WMI enabled BIOS (e.g. HP Compaq 8510p and 6510p).
31df849145SRui Paulo  * Allows to control and read status of integrated hardware and read
32df849145SRui Paulo  * BIOS settings through CMI.
33df849145SRui Paulo  * Inspired by the hp-wmi driver, which implements a subset of these
34df849145SRui Paulo  * features (hotkeys) on Linux.
35df849145SRui Paulo  *
36df849145SRui Paulo  * HP CMI whitepaper:
37df849145SRui Paulo  *     http://h20331.www2.hp.com/Hpsub/downloads/cmi_whitepaper.pdf
38df849145SRui Paulo  * wmi-hp for Linux:
39df849145SRui Paulo  *     http://www.kernel.org
40df849145SRui Paulo  * WMI and ACPI:
41df849145SRui Paulo  *     http://www.microsoft.com/whdc/system/pnppwr/wmi/wmi-acpi.mspx
42df849145SRui Paulo  */
43df849145SRui Paulo 
44df849145SRui Paulo #include "opt_acpi.h"
45df849145SRui Paulo #include <sys/param.h>
46df849145SRui Paulo #include <sys/conf.h>
47df849145SRui Paulo #include <sys/uio.h>
48df849145SRui Paulo #include <sys/proc.h>
49df849145SRui Paulo #include <sys/kernel.h>
50df849145SRui Paulo #include <sys/bus.h>
51df849145SRui Paulo #include <sys/sbuf.h>
52df849145SRui Paulo #include <sys/module.h>
53df849145SRui Paulo #include <sys/sysctl.h>
54df849145SRui Paulo 
55df849145SRui Paulo #include <contrib/dev/acpica/include/acpi.h>
56df849145SRui Paulo #include <contrib/dev/acpica/include/accommon.h>
57df849145SRui Paulo #include <dev/acpica/acpivar.h>
58df849145SRui Paulo #include "acpi_wmi_if.h"
59df849145SRui Paulo 
60df849145SRui Paulo #define _COMPONENT	ACPI_OEM
61df849145SRui Paulo ACPI_MODULE_NAME("HP")
62df849145SRui Paulo 
63df849145SRui Paulo #define ACPI_HP_WMI_EVENT_GUID		"95F24279-4D7B-4334-9387-ACCDC67EF61C"
64df849145SRui Paulo #define ACPI_HP_WMI_BIOS_GUID		"5FB7F034-2C63-45E9-BE91-3D44E2C707E4"
65df849145SRui Paulo #define ACPI_HP_WMI_CMI_GUID		"2D114B49-2DFB-4130-B8FE-4A3C09E75133"
66df849145SRui Paulo 
67df849145SRui Paulo #define ACPI_HP_WMI_DISPLAY_COMMAND		0x1
68df849145SRui Paulo #define ACPI_HP_WMI_HDDTEMP_COMMAND		0x2
69df849145SRui Paulo #define ACPI_HP_WMI_ALS_COMMAND			0x3
70df849145SRui Paulo #define ACPI_HP_WMI_DOCK_COMMAND		0x4
71df849145SRui Paulo #define ACPI_HP_WMI_WIRELESS_COMMAND		0x5
721f842820SAlexander Motin #define ACPI_HP_WMI_BIOS_COMMAND		0x9
731f842820SAlexander Motin #define ACPI_HP_WMI_FEATURE_COMMAND		0xb
741f842820SAlexander Motin #define ACPI_HP_WMI_HOTKEY_COMMAND		0xc
751f842820SAlexander Motin #define ACPI_HP_WMI_FEATURE2_COMMAND		0xd
761f842820SAlexander Motin #define ACPI_HP_WMI_WIRELESS2_COMMAND		0x1b
771f842820SAlexander Motin #define ACPI_HP_WMI_POSTCODEERROR_COMMAND	0x2a
78df849145SRui Paulo 
79df849145SRui Paulo #define ACPI_HP_METHOD_WLAN_ENABLED			1
80df849145SRui Paulo #define ACPI_HP_METHOD_WLAN_RADIO			2
81df849145SRui Paulo #define ACPI_HP_METHOD_WLAN_ON_AIR			3
82df849145SRui Paulo #define ACPI_HP_METHOD_WLAN_ENABLE_IF_RADIO_ON		4
83df849145SRui Paulo #define ACPI_HP_METHOD_WLAN_DISABLE_IF_RADIO_OFF	5
84df849145SRui Paulo #define ACPI_HP_METHOD_BLUETOOTH_ENABLED		6
85df849145SRui Paulo #define ACPI_HP_METHOD_BLUETOOTH_RADIO			7
86df849145SRui Paulo #define ACPI_HP_METHOD_BLUETOOTH_ON_AIR			8
87df849145SRui Paulo #define ACPI_HP_METHOD_BLUETOOTH_ENABLE_IF_RADIO_ON	9
88df849145SRui Paulo #define ACPI_HP_METHOD_BLUETOOTH_DISABLE_IF_RADIO_OFF	10
89df849145SRui Paulo #define ACPI_HP_METHOD_WWAN_ENABLED			11
90df849145SRui Paulo #define ACPI_HP_METHOD_WWAN_RADIO			12
91df849145SRui Paulo #define ACPI_HP_METHOD_WWAN_ON_AIR			13
92df849145SRui Paulo #define ACPI_HP_METHOD_WWAN_ENABLE_IF_RADIO_ON		14
93df849145SRui Paulo #define ACPI_HP_METHOD_WWAN_DISABLE_IF_RADIO_OFF	15
94df849145SRui Paulo #define ACPI_HP_METHOD_ALS				16
95df849145SRui Paulo #define ACPI_HP_METHOD_DISPLAY				17
96df849145SRui Paulo #define ACPI_HP_METHOD_HDDTEMP				18
97df849145SRui Paulo #define ACPI_HP_METHOD_DOCK				19
98df849145SRui Paulo #define ACPI_HP_METHOD_CMI_DETAIL			20
999e760f25SRui Paulo #define ACPI_HP_METHOD_VERBOSE				21
100df849145SRui Paulo 
101df849145SRui Paulo #define HP_MASK_WWAN_ON_AIR			0x1000000
102df849145SRui Paulo #define HP_MASK_BLUETOOTH_ON_AIR		0x10000
103df849145SRui Paulo #define HP_MASK_WLAN_ON_AIR			0x100
104df849145SRui Paulo #define HP_MASK_WWAN_RADIO			0x8000000
105df849145SRui Paulo #define HP_MASK_BLUETOOTH_RADIO			0x80000
106df849145SRui Paulo #define HP_MASK_WLAN_RADIO			0x800
107df849145SRui Paulo #define HP_MASK_WWAN_ENABLED			0x2000000
108df849145SRui Paulo #define HP_MASK_BLUETOOTH_ENABLED		0x20000
109df849145SRui Paulo #define HP_MASK_WLAN_ENABLED			0x200
110df849145SRui Paulo 
1111f842820SAlexander Motin #define ACPI_HP_EVENT_DOCK			0x01
1121f842820SAlexander Motin #define ACPI_HP_EVENT_PARK_HDD			0x02
1131f842820SAlexander Motin #define ACPI_HP_EVENT_SMART_ADAPTER		0x03
1141f842820SAlexander Motin #define ACPI_HP_EVENT_BEZEL_BUTTON		0x04
1151f842820SAlexander Motin #define ACPI_HP_EVENT_WIRELESS			0x05
1161f842820SAlexander Motin #define ACPI_HP_EVENT_CPU_BATTERY_THROTTLE	0x06
1171f842820SAlexander Motin #define ACPI_HP_EVENT_LOCK_SWITCH		0x07
1181f842820SAlexander Motin #define ACPI_HP_EVENT_LID_SWITCH		0x08
1191f842820SAlexander Motin #define ACPI_HP_EVENT_SCREEN_ROTATION		0x09
1201f842820SAlexander Motin #define ACPI_HP_EVENT_COOLSENSE_SYSTEM_MOBILE	0x0A
1211f842820SAlexander Motin #define ACPI_HP_EVENT_COOLSENSE_SYSTEM_HOT	0x0B
1221f842820SAlexander Motin #define ACPI_HP_EVENT_PROXIMITY_SENSOR		0x0C
1231f842820SAlexander Motin #define ACPI_HP_EVENT_BACKLIT_KB_BRIGHTNESS	0x0D
1241f842820SAlexander Motin #define ACPI_HP_EVENT_PEAKSHIFT_PERIOD		0x0F
1251f842820SAlexander Motin #define ACPI_HP_EVENT_BATTERY_CHARGE_PERIOD	0x10
1261f842820SAlexander Motin 
127df849145SRui Paulo #define ACPI_HP_CMI_DETAIL_PATHS		0x01
128df849145SRui Paulo #define ACPI_HP_CMI_DETAIL_ENUMS		0x02
129df849145SRui Paulo #define ACPI_HP_CMI_DETAIL_FLAGS		0x04
1300f73b657SRui Paulo #define ACPI_HP_CMI_DETAIL_SHOW_MAX_INSTANCE	0x08
131df849145SRui Paulo 
1321f842820SAlexander Motin #define ACPI_HP_WMI_RET_WRONG_SIGNATURE		0x02
1331f842820SAlexander Motin #define ACPI_HP_WMI_RET_UNKNOWN_COMMAND		0x03
1341f842820SAlexander Motin #define ACPI_HP_WMI_RET_UNKNOWN_CMDTYPE		0x04
1351f842820SAlexander Motin #define ACPI_HP_WMI_RET_INVALID_PARAMETERS	0x05
1361f842820SAlexander Motin 
137df849145SRui Paulo struct acpi_hp_inst_seq_pair {
138df849145SRui Paulo 	UINT32	sequence;	/* sequence number as suggested by cmi bios */
139df849145SRui Paulo 	UINT8	instance;	/* object instance on guid */
140df849145SRui Paulo };
141df849145SRui Paulo 
142df849145SRui Paulo struct acpi_hp_softc {
143df849145SRui Paulo 	device_t	dev;
144df849145SRui Paulo 	device_t	wmi_dev;
145df849145SRui Paulo 	int		has_notify;		/* notification GUID found */
146df849145SRui Paulo 	int		has_cmi;		/* CMI GUID found */
1471f842820SAlexander Motin 	int		has_wireless;		/* Wireless command found */
148df849145SRui Paulo 	int		cmi_detail;		/* CMI detail level
149df849145SRui Paulo 						   (set by sysctl) */
1509e760f25SRui Paulo 	int		verbose;		/* add debug output */
151df849145SRui Paulo 	int		wlan_enable_if_radio_on;	/* set by sysctl */
152df849145SRui Paulo 	int		wlan_disable_if_radio_off;	/* set by sysctl */
153df849145SRui Paulo 	int		bluetooth_enable_if_radio_on;	/* set by sysctl */
154df849145SRui Paulo 	int		bluetooth_disable_if_radio_off;	/* set by sysctl */
155df849145SRui Paulo 	int		wwan_enable_if_radio_on;	/* set by sysctl */
156df849145SRui Paulo 	int		wwan_disable_if_radio_off;	/* set by sysctl */
157df849145SRui Paulo 	int		was_wlan_on_air;		/* last known WLAN
158df849145SRui Paulo 							   on air status */
159df849145SRui Paulo 	int		was_bluetooth_on_air;		/* last known BT
160df849145SRui Paulo 							   on air status */
161df849145SRui Paulo 	int		was_wwan_on_air;		/* last known WWAN
162df849145SRui Paulo 							   on air status */
163df849145SRui Paulo 	struct sysctl_ctx_list	*sysctl_ctx;
164df849145SRui Paulo 	struct sysctl_oid	*sysctl_tree;
165df849145SRui Paulo 	struct cdev	*hpcmi_dev_t;		/* hpcmi device handle */
166df849145SRui Paulo 	struct sbuf	hpcmi_sbuf;		/* /dev/hpcmi output sbuf */
167df849145SRui Paulo 	pid_t		hpcmi_open_pid;		/* pid operating on
168df849145SRui Paulo 						   /dev/hpcmi */
169df849145SRui Paulo 	int		hpcmi_bufptr;		/* current pointer position
170df849145SRui Paulo 						   in /dev/hpcmi output buffer
171df849145SRui Paulo 						 */
172df849145SRui Paulo 	int		cmi_order_size;		/* size of cmi_order list */
173df849145SRui Paulo 	struct acpi_hp_inst_seq_pair cmi_order[128];	/* list of CMI
174df849145SRui Paulo 			     instances ordered by BIOS suggested sequence */
175df849145SRui Paulo };
176df849145SRui Paulo 
177df849145SRui Paulo static struct {
178df849145SRui Paulo 	char	*name;
179df849145SRui Paulo 	int	method;
180df849145SRui Paulo 	char	*description;
181f0188618SHans Petter Selasky 	int	flag_rdonly;
182df849145SRui Paulo } acpi_hp_sysctls[] = {
183df849145SRui Paulo 	{
184df849145SRui Paulo 		.name		= "wlan_enabled",
185df849145SRui Paulo 		.method		= ACPI_HP_METHOD_WLAN_ENABLED,
186df849145SRui Paulo 		.description	= "Enable/Disable WLAN (WiFi)",
187df849145SRui Paulo 	},
188df849145SRui Paulo 	{
189df849145SRui Paulo 		.name		= "wlan_radio",
190df849145SRui Paulo 		.method		= ACPI_HP_METHOD_WLAN_RADIO,
191df849145SRui Paulo 		.description	= "WLAN radio status",
192f0188618SHans Petter Selasky 		.flag_rdonly	= 1
193df849145SRui Paulo 	},
194df849145SRui Paulo 	{
195df849145SRui Paulo 		.name		= "wlan_on_air",
196df849145SRui Paulo 		.method		= ACPI_HP_METHOD_WLAN_ON_AIR,
197df849145SRui Paulo 		.description	= "WLAN radio ready to use (enabled and radio)",
198f0188618SHans Petter Selasky 		.flag_rdonly	= 1
199df849145SRui Paulo 	},
200df849145SRui Paulo 	{
201df849145SRui Paulo 		.name		= "wlan_enable_if_radio_on",
202df849145SRui Paulo 		.method		= ACPI_HP_METHOD_WLAN_ENABLE_IF_RADIO_ON,
203df849145SRui Paulo 		.description	= "Enable WLAN if radio is turned on",
204df849145SRui Paulo 	},
205df849145SRui Paulo 	{
206df849145SRui Paulo 		.name		= "wlan_disable_if_radio_off",
207df849145SRui Paulo 		.method		= ACPI_HP_METHOD_WLAN_DISABLE_IF_RADIO_OFF,
208df849145SRui Paulo 		.description	= "Disable WLAN if radio is turned off",
209df849145SRui Paulo 	},
210df849145SRui Paulo 	{
211df849145SRui Paulo 		.name		= "bt_enabled",
212df849145SRui Paulo 		.method		= ACPI_HP_METHOD_BLUETOOTH_ENABLED,
213df849145SRui Paulo 		.description	= "Enable/Disable Bluetooth",
214df849145SRui Paulo 	},
215df849145SRui Paulo 	{
216df849145SRui Paulo 		.name		= "bt_radio",
217df849145SRui Paulo 		.method		= ACPI_HP_METHOD_BLUETOOTH_RADIO,
218df849145SRui Paulo 		.description	= "Bluetooth radio status",
219f0188618SHans Petter Selasky 		.flag_rdonly	= 1
220df849145SRui Paulo 	},
221df849145SRui Paulo 	{
222df849145SRui Paulo 		.name		= "bt_on_air",
223df849145SRui Paulo 		.method		= ACPI_HP_METHOD_BLUETOOTH_ON_AIR,
224df849145SRui Paulo 		.description	= "Bluetooth radio ready to use"
225df849145SRui Paulo 				    " (enabled and radio)",
226f0188618SHans Petter Selasky 		.flag_rdonly	= 1
227df849145SRui Paulo 	},
228df849145SRui Paulo 	{
229df849145SRui Paulo 		.name		= "bt_enable_if_radio_on",
230df849145SRui Paulo 		.method		= ACPI_HP_METHOD_BLUETOOTH_ENABLE_IF_RADIO_ON,
231df849145SRui Paulo 		.description	= "Enable bluetooth if radio is turned on",
232df849145SRui Paulo 	},
233df849145SRui Paulo 	{
234df849145SRui Paulo 		.name		= "bt_disable_if_radio_off",
235df849145SRui Paulo 		.method		= ACPI_HP_METHOD_BLUETOOTH_DISABLE_IF_RADIO_OFF,
236df849145SRui Paulo 		.description	= "Disable bluetooth if radio is turned off",
237df849145SRui Paulo 	},
238df849145SRui Paulo 	{
239df849145SRui Paulo 		.name		= "wwan_enabled",
240df849145SRui Paulo 		.method		= ACPI_HP_METHOD_WWAN_ENABLED,
241df849145SRui Paulo 		.description	= "Enable/Disable WWAN (UMTS)",
242df849145SRui Paulo 	},
243df849145SRui Paulo 	{
244df849145SRui Paulo 		.name		= "wwan_radio",
245df849145SRui Paulo 		.method		= ACPI_HP_METHOD_WWAN_RADIO,
246df849145SRui Paulo 		.description	= "WWAN radio status",
247f0188618SHans Petter Selasky 		.flag_rdonly	= 1
248df849145SRui Paulo 	},
249df849145SRui Paulo 	{
250df849145SRui Paulo 		.name		= "wwan_on_air",
251df849145SRui Paulo 		.method		= ACPI_HP_METHOD_WWAN_ON_AIR,
252df849145SRui Paulo 		.description	= "WWAN radio ready to use (enabled and radio)",
253f0188618SHans Petter Selasky 		.flag_rdonly	= 1
254df849145SRui Paulo 	},
255df849145SRui Paulo 	{
256df849145SRui Paulo 		.name		= "wwan_enable_if_radio_on",
257df849145SRui Paulo 		.method		= ACPI_HP_METHOD_WWAN_ENABLE_IF_RADIO_ON,
258df849145SRui Paulo 		.description	= "Enable WWAN if radio is turned on",
259df849145SRui Paulo 	},
260df849145SRui Paulo 	{
261df849145SRui Paulo 		.name		= "wwan_disable_if_radio_off",
262df849145SRui Paulo 		.method		= ACPI_HP_METHOD_WWAN_DISABLE_IF_RADIO_OFF,
263df849145SRui Paulo 		.description	= "Disable WWAN if radio is turned off",
264df849145SRui Paulo 	},
265df849145SRui Paulo 	{
266df849145SRui Paulo 		.name		= "als_enabled",
267df849145SRui Paulo 		.method		= ACPI_HP_METHOD_ALS,
268df849145SRui Paulo 		.description	= "Enable/Disable ALS (Ambient light sensor)",
269df849145SRui Paulo 	},
270df849145SRui Paulo 	{
271df849145SRui Paulo 		.name		= "display",
272df849145SRui Paulo 		.method		= ACPI_HP_METHOD_DISPLAY,
273df849145SRui Paulo 		.description	= "Display status",
274f0188618SHans Petter Selasky 		.flag_rdonly	= 1
275df849145SRui Paulo 	},
276df849145SRui Paulo 	{
277df849145SRui Paulo 		.name		= "hdd_temperature",
278df849145SRui Paulo 		.method		= ACPI_HP_METHOD_HDDTEMP,
279df849145SRui Paulo 		.description	= "HDD temperature",
280f0188618SHans Petter Selasky 		.flag_rdonly	= 1
281df849145SRui Paulo 	},
282df849145SRui Paulo 	{
283df849145SRui Paulo 		.name		= "is_docked",
284df849145SRui Paulo 		.method		= ACPI_HP_METHOD_DOCK,
285df849145SRui Paulo 		.description	= "Docking station status",
286f0188618SHans Petter Selasky 		.flag_rdonly	= 1
287df849145SRui Paulo 	},
288df849145SRui Paulo 	{
289df849145SRui Paulo 		.name		= "cmi_detail",
290df849145SRui Paulo 		.method		= ACPI_HP_METHOD_CMI_DETAIL,
291df849145SRui Paulo 		.description	= "Details shown in CMI output "
292df849145SRui Paulo 				    "(cat /dev/hpcmi)",
293df849145SRui Paulo 	},
2949e760f25SRui Paulo 	{
2959e760f25SRui Paulo 		.name		= "verbose",
2969e760f25SRui Paulo 		.method		= ACPI_HP_METHOD_VERBOSE,
2979e760f25SRui Paulo 		.description	= "Verbosity level",
2989e760f25SRui Paulo 	},
299df849145SRui Paulo 	{ NULL, 0, NULL, 0 }
300df849145SRui Paulo };
301df849145SRui Paulo 
302df849145SRui Paulo ACPI_SERIAL_DECL(hp, "HP ACPI-WMI Mapping");
303df849145SRui Paulo 
3049fa1edb8SAndriy Gapon static void	acpi_hp_identify(driver_t *driver, device_t parent);
305df849145SRui Paulo static int	acpi_hp_probe(device_t dev);
306df849145SRui Paulo static int	acpi_hp_attach(device_t dev);
307df849145SRui Paulo static int	acpi_hp_detach(device_t dev);
308df849145SRui Paulo 
309df849145SRui Paulo static void	acpi_hp_evaluate_auto_on_off(struct acpi_hp_softc* sc);
310df849145SRui Paulo static int	acpi_hp_sysctl(SYSCTL_HANDLER_ARGS);
311df849145SRui Paulo static int	acpi_hp_sysctl_set(struct acpi_hp_softc *sc, int method,
312df849145SRui Paulo 		    int arg, int oldarg);
313df849145SRui Paulo static int	acpi_hp_sysctl_get(struct acpi_hp_softc *sc, int method);
314df849145SRui Paulo static int	acpi_hp_exec_wmi_command(device_t wmi_dev, int command,
3151f842820SAlexander Motin 		    int is_write, int val, int *retval);
316df849145SRui Paulo static void	acpi_hp_notify(ACPI_HANDLE h, UINT32 notify, void *context);
317df849145SRui Paulo static int	acpi_hp_get_cmi_block(device_t wmi_dev, const char* guid,
318df849145SRui Paulo 		    UINT8 instance, char* outbuf, size_t outsize,
319df849145SRui Paulo 		    UINT32* sequence, int detail);
320df849145SRui Paulo static void	acpi_hp_hex_decode(char* buffer);
321df849145SRui Paulo 
322df849145SRui Paulo static d_open_t	acpi_hp_hpcmi_open;
323df849145SRui Paulo static d_close_t acpi_hp_hpcmi_close;
324df849145SRui Paulo static d_read_t	acpi_hp_hpcmi_read;
325df849145SRui Paulo 
326df849145SRui Paulo /* handler /dev/hpcmi device */
327df849145SRui Paulo static struct cdevsw hpcmi_cdevsw = {
328df849145SRui Paulo 	.d_version = D_VERSION,
329df849145SRui Paulo 	.d_open = acpi_hp_hpcmi_open,
330df849145SRui Paulo 	.d_close = acpi_hp_hpcmi_close,
331df849145SRui Paulo 	.d_read = acpi_hp_hpcmi_read,
332df849145SRui Paulo 	.d_name = "hpcmi",
333df849145SRui Paulo };
334df849145SRui Paulo 
335df849145SRui Paulo static device_method_t acpi_hp_methods[] = {
3369fa1edb8SAndriy Gapon 	DEVMETHOD(device_identify, acpi_hp_identify),
337df849145SRui Paulo 	DEVMETHOD(device_probe, acpi_hp_probe),
338df849145SRui Paulo 	DEVMETHOD(device_attach, acpi_hp_attach),
339df849145SRui Paulo 	DEVMETHOD(device_detach, acpi_hp_detach),
34061bfd867SSofian Brabez 
34161bfd867SSofian Brabez 	DEVMETHOD_END
342df849145SRui Paulo };
343df849145SRui Paulo 
344df849145SRui Paulo static driver_t	acpi_hp_driver = {
345df849145SRui Paulo 	"acpi_hp",
346df849145SRui Paulo 	acpi_hp_methods,
347df849145SRui Paulo 	sizeof(struct acpi_hp_softc),
348df849145SRui Paulo };
349df849145SRui Paulo 
350*90161e72SJohn Baldwin DRIVER_MODULE(acpi_hp, acpi_wmi, acpi_hp_driver, 0, 0);
351df849145SRui Paulo MODULE_DEPEND(acpi_hp, acpi_wmi, 1, 1, 1);
352df849145SRui Paulo MODULE_DEPEND(acpi_hp, acpi, 1, 1, 1);
353df849145SRui Paulo 
354df849145SRui Paulo static void
acpi_hp_evaluate_auto_on_off(struct acpi_hp_softc * sc)355df849145SRui Paulo acpi_hp_evaluate_auto_on_off(struct acpi_hp_softc *sc)
356df849145SRui Paulo {
3571f842820SAlexander Motin 	int	res;
358df849145SRui Paulo 	int	wireless;
359df849145SRui Paulo 	int	new_wlan_status;
360df849145SRui Paulo 	int	new_bluetooth_status;
361df849145SRui Paulo 	int	new_wwan_status;
362df849145SRui Paulo 
3631f842820SAlexander Motin 	res = acpi_hp_exec_wmi_command(sc->wmi_dev,
3641f842820SAlexander Motin 	    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &wireless);
3651f842820SAlexander Motin 	if (res != 0) {
3661f842820SAlexander Motin 		device_printf(sc->wmi_dev, "Wireless command error %x\n", res);
3671f842820SAlexander Motin 		return;
3681f842820SAlexander Motin 	}
369df849145SRui Paulo 	new_wlan_status = -1;
370df849145SRui Paulo 	new_bluetooth_status = -1;
371df849145SRui Paulo 	new_wwan_status = -1;
372df849145SRui Paulo 
3739e760f25SRui Paulo 	if (sc->verbose)
374df849145SRui Paulo 		device_printf(sc->wmi_dev, "Wireless status is %x\n", wireless);
375df849145SRui Paulo 	if (sc->wlan_disable_if_radio_off && !(wireless & HP_MASK_WLAN_RADIO)
376df849145SRui Paulo 	    &&  (wireless & HP_MASK_WLAN_ENABLED)) {
377df849145SRui Paulo 		acpi_hp_exec_wmi_command(sc->wmi_dev,
3781f842820SAlexander Motin 		    ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x100, NULL);
379df849145SRui Paulo 		new_wlan_status = 0;
380df849145SRui Paulo 	}
381df849145SRui Paulo 	else if (sc->wlan_enable_if_radio_on && (wireless & HP_MASK_WLAN_RADIO)
382df849145SRui Paulo 		&&  !(wireless & HP_MASK_WLAN_ENABLED)) {
383df849145SRui Paulo 		acpi_hp_exec_wmi_command(sc->wmi_dev,
3841f842820SAlexander Motin 		    ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x101, NULL);
385df849145SRui Paulo 		new_wlan_status = 1;
386df849145SRui Paulo 	}
387df849145SRui Paulo 	if (sc->bluetooth_disable_if_radio_off &&
388df849145SRui Paulo 	    !(wireless & HP_MASK_BLUETOOTH_RADIO) &&
389df849145SRui Paulo 	    (wireless & HP_MASK_BLUETOOTH_ENABLED)) {
390df849145SRui Paulo 		acpi_hp_exec_wmi_command(sc->wmi_dev,
3911f842820SAlexander Motin 		    ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x200, NULL);
392df849145SRui Paulo 		new_bluetooth_status = 0;
393df849145SRui Paulo 	}
394df849145SRui Paulo 	else if (sc->bluetooth_enable_if_radio_on &&
395df849145SRui Paulo 		(wireless & HP_MASK_BLUETOOTH_RADIO) &&
396df849145SRui Paulo 		!(wireless & HP_MASK_BLUETOOTH_ENABLED)) {
397df849145SRui Paulo 		acpi_hp_exec_wmi_command(sc->wmi_dev,
3981f842820SAlexander Motin 		    ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x202, NULL);
399df849145SRui Paulo 		new_bluetooth_status = 1;
400df849145SRui Paulo 	}
401df849145SRui Paulo 	if (sc->wwan_disable_if_radio_off &&
402df849145SRui Paulo 	    !(wireless & HP_MASK_WWAN_RADIO) &&
403df849145SRui Paulo 	    (wireless & HP_MASK_WWAN_ENABLED)) {
404df849145SRui Paulo 		acpi_hp_exec_wmi_command(sc->wmi_dev,
4051f842820SAlexander Motin 		ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x400, NULL);
406df849145SRui Paulo 		new_wwan_status = 0;
407df849145SRui Paulo 	}
408df849145SRui Paulo 	else if (sc->wwan_enable_if_radio_on &&
409df849145SRui Paulo 		(wireless & HP_MASK_WWAN_RADIO) &&
410df849145SRui Paulo 		!(wireless & HP_MASK_WWAN_ENABLED)) {
411df849145SRui Paulo 		acpi_hp_exec_wmi_command(sc->wmi_dev,
4121f842820SAlexander Motin 		    ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x404, NULL);
413df849145SRui Paulo 		new_wwan_status = 1;
414df849145SRui Paulo 	}
415df849145SRui Paulo 
416df849145SRui Paulo 	if (new_wlan_status == -1) {
417df849145SRui Paulo 		new_wlan_status = (wireless & HP_MASK_WLAN_ON_AIR);
418df849145SRui Paulo 		if ((new_wlan_status?1:0) != sc->was_wlan_on_air) {
419df849145SRui Paulo 			sc->was_wlan_on_air = sc->was_wlan_on_air?0:1;
4209e760f25SRui Paulo 			if (sc->verbose)
421df849145SRui Paulo 				device_printf(sc->wmi_dev,
422df849145SRui Paulo 			    	    "WLAN on air changed to %i "
423df849145SRui Paulo 			    	    "(new_wlan_status is %i)\n",
424df849145SRui Paulo 			    	    sc->was_wlan_on_air, new_wlan_status);
4259fa1edb8SAndriy Gapon 			acpi_UserNotify("HP", ACPI_ROOT_OBJECT,
426df849145SRui Paulo 			    0xc0+sc->was_wlan_on_air);
427df849145SRui Paulo 		}
428df849145SRui Paulo 	}
429df849145SRui Paulo 	if (new_bluetooth_status == -1) {
430df849145SRui Paulo 		new_bluetooth_status = (wireless & HP_MASK_BLUETOOTH_ON_AIR);
431df849145SRui Paulo 		if ((new_bluetooth_status?1:0) != sc->was_bluetooth_on_air) {
432df849145SRui Paulo 			sc->was_bluetooth_on_air = sc->was_bluetooth_on_air?
433df849145SRui Paulo 			    0:1;
4349e760f25SRui Paulo 			if (sc->verbose)
4359e760f25SRui Paulo 				device_printf(sc->wmi_dev,
4369e760f25SRui Paulo 				    "BLUETOOTH on air changed"
437df849145SRui Paulo 				    " to %i (new_bluetooth_status is %i)\n",
4389e760f25SRui Paulo 				    sc->was_bluetooth_on_air,
4399e760f25SRui Paulo 				    new_bluetooth_status);
4409fa1edb8SAndriy Gapon 			acpi_UserNotify("HP", ACPI_ROOT_OBJECT,
441df849145SRui Paulo 			    0xd0+sc->was_bluetooth_on_air);
442df849145SRui Paulo 		}
443df849145SRui Paulo 	}
444df849145SRui Paulo 	if (new_wwan_status == -1) {
445df849145SRui Paulo 		new_wwan_status = (wireless & HP_MASK_WWAN_ON_AIR);
446df849145SRui Paulo 		if ((new_wwan_status?1:0) != sc->was_wwan_on_air) {
447df849145SRui Paulo 			sc->was_wwan_on_air = sc->was_wwan_on_air?0:1;
4489e760f25SRui Paulo 			if (sc->verbose)
4499e760f25SRui Paulo 				device_printf(sc->wmi_dev,
4509e760f25SRui Paulo 				    "WWAN on air changed to %i"
451df849145SRui Paulo 			    	    " (new_wwan_status is %i)\n",
452df849145SRui Paulo 				    sc->was_wwan_on_air, new_wwan_status);
4539fa1edb8SAndriy Gapon 			acpi_UserNotify("HP", ACPI_ROOT_OBJECT,
454df849145SRui Paulo 			    0xe0+sc->was_wwan_on_air);
455df849145SRui Paulo 		}
456df849145SRui Paulo 	}
457df849145SRui Paulo }
458df849145SRui Paulo 
4599fa1edb8SAndriy Gapon static void
acpi_hp_identify(driver_t * driver,device_t parent)4609fa1edb8SAndriy Gapon acpi_hp_identify(driver_t *driver, device_t parent)
4619fa1edb8SAndriy Gapon {
4629fa1edb8SAndriy Gapon 
4639fa1edb8SAndriy Gapon 	/* Don't do anything if driver is disabled. */
4649fa1edb8SAndriy Gapon 	if (acpi_disabled("hp"))
4659fa1edb8SAndriy Gapon 		return;
4669fa1edb8SAndriy Gapon 
4679fa1edb8SAndriy Gapon 	/* Add only a single device instance. */
4689fa1edb8SAndriy Gapon 	if (device_find_child(parent, "acpi_hp", -1) != NULL)
4699fa1edb8SAndriy Gapon 		return;
4709fa1edb8SAndriy Gapon 
4711f842820SAlexander Motin 	/* Check BIOS GUID to see whether system is compatible. */
4721f842820SAlexander Motin 	if (!ACPI_WMI_PROVIDES_GUID_STRING(parent,
4731f842820SAlexander Motin 	    ACPI_HP_WMI_BIOS_GUID))
4741f842820SAlexander Motin 		return;
4751f842820SAlexander Motin 
476f5aadc99SAndriy Gapon 	if (BUS_ADD_CHILD(parent, 0, "acpi_hp", -1) == NULL)
4779fa1edb8SAndriy Gapon 		device_printf(parent, "add acpi_hp child failed\n");
4789fa1edb8SAndriy Gapon }
4799fa1edb8SAndriy Gapon 
480df849145SRui Paulo static int
acpi_hp_probe(device_t dev)481df849145SRui Paulo acpi_hp_probe(device_t dev)
482df849145SRui Paulo {
4839fa1edb8SAndriy Gapon 
484df849145SRui Paulo 	device_set_desc(dev, "HP ACPI-WMI Mapping");
485df849145SRui Paulo 	return (0);
486df849145SRui Paulo }
487df849145SRui Paulo 
488df849145SRui Paulo static int
acpi_hp_attach(device_t dev)489df849145SRui Paulo acpi_hp_attach(device_t dev)
490df849145SRui Paulo {
491df849145SRui Paulo 	struct acpi_hp_softc	*sc;
492df849145SRui Paulo 	int			arg;
493df849145SRui Paulo 
494df849145SRui Paulo 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
495df849145SRui Paulo 
496df849145SRui Paulo 	sc = device_get_softc(dev);
497df849145SRui Paulo 	sc->dev = dev;
498df849145SRui Paulo 	sc->has_notify = 0;
499df849145SRui Paulo 	sc->has_cmi = 0;
500df849145SRui Paulo 	sc->bluetooth_enable_if_radio_on = 0;
501df849145SRui Paulo 	sc->bluetooth_disable_if_radio_off = 0;
502df849145SRui Paulo 	sc->wlan_enable_if_radio_on = 0;
503df849145SRui Paulo 	sc->wlan_disable_if_radio_off = 0;
504df849145SRui Paulo 	sc->wlan_enable_if_radio_on = 0;
505df849145SRui Paulo 	sc->wlan_disable_if_radio_off = 0;
506df849145SRui Paulo 	sc->was_wlan_on_air = 0;
507df849145SRui Paulo 	sc->was_bluetooth_on_air = 0;
508df849145SRui Paulo 	sc->was_wwan_on_air = 0;
509df849145SRui Paulo 	sc->cmi_detail = 0;
510df849145SRui Paulo 	sc->cmi_order_size = -1;
5111f842820SAlexander Motin 	sc->verbose = bootverbose;
512df849145SRui Paulo 	memset(sc->cmi_order, 0, sizeof(sc->cmi_order));
513df849145SRui Paulo 
514f5aadc99SAndriy Gapon 	sc->wmi_dev = device_get_parent(dev);
515df849145SRui Paulo 	if (!ACPI_WMI_PROVIDES_GUID_STRING(sc->wmi_dev,
516df849145SRui Paulo 	    ACPI_HP_WMI_BIOS_GUID)) {
517df849145SRui Paulo 		device_printf(dev,
518df849145SRui Paulo 		    "WMI device does not provide the HP BIOS GUID\n");
519df849145SRui Paulo 		return (EINVAL);
520df849145SRui Paulo 	}
521df849145SRui Paulo 	if (ACPI_WMI_PROVIDES_GUID_STRING(sc->wmi_dev,
522df849145SRui Paulo 	    ACPI_HP_WMI_EVENT_GUID)) {
523df849145SRui Paulo 		device_printf(dev,
524df849145SRui Paulo 		    "HP event GUID detected, installing event handler\n");
525df849145SRui Paulo 		if (ACPI_WMI_INSTALL_EVENT_HANDLER(sc->wmi_dev,
526df849145SRui Paulo 		    ACPI_HP_WMI_EVENT_GUID, acpi_hp_notify, dev)) {
527df849145SRui Paulo 			device_printf(dev,
528df849145SRui Paulo 			    "Could not install notification handler!\n");
529df849145SRui Paulo 		}
530df849145SRui Paulo 		else {
531df849145SRui Paulo 			sc->has_notify = 1;
532df849145SRui Paulo 		}
533df849145SRui Paulo 	}
5340f73b657SRui Paulo 	if ((sc->has_cmi =
5350f73b657SRui Paulo 	    ACPI_WMI_PROVIDES_GUID_STRING(sc->wmi_dev, ACPI_HP_WMI_CMI_GUID)
5360f73b657SRui Paulo 	    )) {
537df849145SRui Paulo 		device_printf(dev, "HP CMI GUID detected\n");
538df849145SRui Paulo 	}
539df849145SRui Paulo 
540df849145SRui Paulo 	if (sc->has_cmi) {
541df849145SRui Paulo 		sc->hpcmi_dev_t = make_dev(&hpcmi_cdevsw, 0, UID_ROOT,
542df849145SRui Paulo 			    GID_WHEEL, 0644, "hpcmi");
543df849145SRui Paulo 		sc->hpcmi_dev_t->si_drv1 = sc;
544df849145SRui Paulo 		sc->hpcmi_open_pid = 0;
545df849145SRui Paulo 		sc->hpcmi_bufptr = -1;
546df849145SRui Paulo 	}
547df849145SRui Paulo 
5481f842820SAlexander Motin 	if (acpi_hp_exec_wmi_command(sc->wmi_dev,
5491f842820SAlexander Motin 	    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, NULL) == 0)
5501f842820SAlexander Motin 		sc->has_wireless = 1;
5511f842820SAlexander Motin 
552df849145SRui Paulo 	ACPI_SERIAL_BEGIN(hp);
553df849145SRui Paulo 
554df849145SRui Paulo 	sc->sysctl_ctx = device_get_sysctl_ctx(dev);
555df849145SRui Paulo 	sc->sysctl_tree = device_get_sysctl_tree(dev);
556df849145SRui Paulo 	for (int i = 0; acpi_hp_sysctls[i].name != NULL; ++i) {
557df849145SRui Paulo 		arg = 0;
5581f842820SAlexander Motin 		if (((!sc->has_notify || !sc->has_wireless) &&
559df849145SRui Paulo 		    (acpi_hp_sysctls[i].method ==
560df849145SRui Paulo 			ACPI_HP_METHOD_WLAN_ENABLE_IF_RADIO_ON ||
561df849145SRui Paulo 		    acpi_hp_sysctls[i].method ==
562df849145SRui Paulo 			ACPI_HP_METHOD_WLAN_DISABLE_IF_RADIO_OFF ||
563df849145SRui Paulo 		    acpi_hp_sysctls[i].method ==
564df849145SRui Paulo 			ACPI_HP_METHOD_BLUETOOTH_ENABLE_IF_RADIO_ON ||
565df849145SRui Paulo 		    acpi_hp_sysctls[i].method ==
566df849145SRui Paulo 			ACPI_HP_METHOD_BLUETOOTH_DISABLE_IF_RADIO_OFF ||
567df849145SRui Paulo 		    acpi_hp_sysctls[i].method ==
568df849145SRui Paulo 			ACPI_HP_METHOD_WWAN_ENABLE_IF_RADIO_ON ||
569df849145SRui Paulo 		    acpi_hp_sysctls[i].method ==
570df849145SRui Paulo 			ACPI_HP_METHOD_WWAN_DISABLE_IF_RADIO_OFF)) ||
571df849145SRui Paulo 		    (arg = acpi_hp_sysctl_get(sc,
572df849145SRui Paulo 		    acpi_hp_sysctls[i].method)) < 0) {
573df849145SRui Paulo 			continue;
574df849145SRui Paulo 		}
575df849145SRui Paulo 		if (acpi_hp_sysctls[i].method == ACPI_HP_METHOD_WLAN_ON_AIR) {
576df849145SRui Paulo 			sc->was_wlan_on_air = arg;
577df849145SRui Paulo 		}
578df849145SRui Paulo 		else if (acpi_hp_sysctls[i].method ==
579df849145SRui Paulo 			    ACPI_HP_METHOD_BLUETOOTH_ON_AIR) {
580df849145SRui Paulo 			sc->was_bluetooth_on_air = arg;
581df849145SRui Paulo 		}
582df849145SRui Paulo 		else if (acpi_hp_sysctls[i].method ==
583df849145SRui Paulo 			    ACPI_HP_METHOD_WWAN_ON_AIR) {
584df849145SRui Paulo 			sc->was_wwan_on_air = arg;
585df849145SRui Paulo 		}
586df849145SRui Paulo 
587f0188618SHans Petter Selasky 		if (acpi_hp_sysctls[i].flag_rdonly != 0) {
588df849145SRui Paulo 			SYSCTL_ADD_PROC(sc->sysctl_ctx,
589df849145SRui Paulo 			    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
5907029da5cSPawel Biernacki 			    acpi_hp_sysctls[i].name,
5916237a1ccSAlexander Motin 			    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
592df849145SRui Paulo 			    sc, i, acpi_hp_sysctl, "I",
593df849145SRui Paulo 			    acpi_hp_sysctls[i].description);
594f0188618SHans Petter Selasky 		} else {
595f0188618SHans Petter Selasky 			SYSCTL_ADD_PROC(sc->sysctl_ctx,
596f0188618SHans Petter Selasky 			    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
5977029da5cSPawel Biernacki 			    acpi_hp_sysctls[i].name,
5986237a1ccSAlexander Motin 			    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
599f0188618SHans Petter Selasky 			    sc, i, acpi_hp_sysctl, "I",
600f0188618SHans Petter Selasky 			    acpi_hp_sysctls[i].description);
601f0188618SHans Petter Selasky 		}
602df849145SRui Paulo 	}
603df849145SRui Paulo 	ACPI_SERIAL_END(hp);
604df849145SRui Paulo 
605df849145SRui Paulo 	return (0);
606df849145SRui Paulo }
607df849145SRui Paulo 
608df849145SRui Paulo static int
acpi_hp_detach(device_t dev)609df849145SRui Paulo acpi_hp_detach(device_t dev)
610df849145SRui Paulo {
611897986b4SJohn Baldwin 	struct acpi_hp_softc *sc;
612df849145SRui Paulo 
613df849145SRui Paulo 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
614897986b4SJohn Baldwin 	sc = device_get_softc(dev);
615897986b4SJohn Baldwin 	if (sc->has_cmi && sc->hpcmi_open_pid != 0)
616897986b4SJohn Baldwin 		return (EBUSY);
617897986b4SJohn Baldwin 
6181f842820SAlexander Motin 	if (sc->has_notify) {
6191f842820SAlexander Motin 		ACPI_WMI_REMOVE_EVENT_HANDLER(sc->wmi_dev,
6201f842820SAlexander Motin 		    ACPI_HP_WMI_EVENT_GUID);
6211f842820SAlexander Motin 	}
622897986b4SJohn Baldwin 
623897986b4SJohn Baldwin 	if (sc->has_cmi) {
624df849145SRui Paulo 		if (sc->hpcmi_bufptr != -1) {
625df849145SRui Paulo 			sbuf_delete(&sc->hpcmi_sbuf);
626df849145SRui Paulo 			sc->hpcmi_bufptr = -1;
627df849145SRui Paulo 		}
628df849145SRui Paulo 		sc->hpcmi_open_pid = 0;
629df849145SRui Paulo 		destroy_dev(sc->hpcmi_dev_t);
630df849145SRui Paulo 	}
631df849145SRui Paulo 
632897986b4SJohn Baldwin 	return (0);
633df849145SRui Paulo }
634df849145SRui Paulo 
635df849145SRui Paulo static int
acpi_hp_sysctl(SYSCTL_HANDLER_ARGS)636df849145SRui Paulo acpi_hp_sysctl(SYSCTL_HANDLER_ARGS)
637df849145SRui Paulo {
638df849145SRui Paulo 	struct acpi_hp_softc	*sc;
639df849145SRui Paulo 	int			arg;
640df849145SRui Paulo 	int			oldarg;
641df849145SRui Paulo 	int			error = 0;
642df849145SRui Paulo 	int			function;
643df849145SRui Paulo 	int			method;
644df849145SRui Paulo 
645df849145SRui Paulo 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
646df849145SRui Paulo 
647df849145SRui Paulo 	sc = (struct acpi_hp_softc *)oidp->oid_arg1;
648df849145SRui Paulo 	function = oidp->oid_arg2;
649df849145SRui Paulo 	method = acpi_hp_sysctls[function].method;
650df849145SRui Paulo 
651df849145SRui Paulo 	ACPI_SERIAL_BEGIN(hp);
652df849145SRui Paulo 	arg = acpi_hp_sysctl_get(sc, method);
653df849145SRui Paulo 	oldarg = arg;
654df849145SRui Paulo 	error = sysctl_handle_int(oidp, &arg, 0, req);
655df849145SRui Paulo 	if (!error && req->newptr != NULL) {
656df849145SRui Paulo 		error = acpi_hp_sysctl_set(sc, method, arg, oldarg);
657df849145SRui Paulo 	}
658df849145SRui Paulo 	ACPI_SERIAL_END(hp);
659df849145SRui Paulo 
660df849145SRui Paulo 	return (error);
661df849145SRui Paulo }
662df849145SRui Paulo 
663df849145SRui Paulo static int
acpi_hp_sysctl_get(struct acpi_hp_softc * sc,int method)664df849145SRui Paulo acpi_hp_sysctl_get(struct acpi_hp_softc *sc, int method)
665df849145SRui Paulo {
666df849145SRui Paulo 	int	val = 0;
667df849145SRui Paulo 
668df849145SRui Paulo 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
669df849145SRui Paulo 	ACPI_SERIAL_ASSERT(hp);
670df849145SRui Paulo 
671df849145SRui Paulo 	switch (method) {
672df849145SRui Paulo 	case ACPI_HP_METHOD_WLAN_ENABLED:
6731f842820SAlexander Motin 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
6741f842820SAlexander Motin 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
6751f842820SAlexander Motin 			return (-EINVAL);
676df849145SRui Paulo 		val = ((val & HP_MASK_WLAN_ENABLED) != 0);
677df849145SRui Paulo 		break;
678df849145SRui Paulo 	case ACPI_HP_METHOD_WLAN_RADIO:
6791f842820SAlexander Motin 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
6801f842820SAlexander Motin 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
6811f842820SAlexander Motin 			return (-EINVAL);
682df849145SRui Paulo 		val = ((val & HP_MASK_WLAN_RADIO) != 0);
683df849145SRui Paulo 		break;
684df849145SRui Paulo 	case ACPI_HP_METHOD_WLAN_ON_AIR:
6851f842820SAlexander Motin 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
6861f842820SAlexander Motin 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
6871f842820SAlexander Motin 			return (-EINVAL);
688df849145SRui Paulo 		val = ((val & HP_MASK_WLAN_ON_AIR) != 0);
689df849145SRui Paulo 		break;
690df849145SRui Paulo 	case ACPI_HP_METHOD_WLAN_ENABLE_IF_RADIO_ON:
691df849145SRui Paulo 		val = sc->wlan_enable_if_radio_on;
692df849145SRui Paulo 		break;
693df849145SRui Paulo 	case ACPI_HP_METHOD_WLAN_DISABLE_IF_RADIO_OFF:
694df849145SRui Paulo 		val = sc->wlan_disable_if_radio_off;
695df849145SRui Paulo 		break;
696df849145SRui Paulo 	case ACPI_HP_METHOD_BLUETOOTH_ENABLED:
6971f842820SAlexander Motin 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
6981f842820SAlexander Motin 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
6991f842820SAlexander Motin 			return (-EINVAL);
700df849145SRui Paulo 		val = ((val & HP_MASK_BLUETOOTH_ENABLED) != 0);
701df849145SRui Paulo 		break;
702df849145SRui Paulo 	case ACPI_HP_METHOD_BLUETOOTH_RADIO:
7031f842820SAlexander Motin 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
7041f842820SAlexander Motin 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
7051f842820SAlexander Motin 			return (-EINVAL);
706df849145SRui Paulo 		val = ((val & HP_MASK_BLUETOOTH_RADIO) != 0);
707df849145SRui Paulo 		break;
708df849145SRui Paulo 	case ACPI_HP_METHOD_BLUETOOTH_ON_AIR:
7091f842820SAlexander Motin 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
7101f842820SAlexander Motin 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
7111f842820SAlexander Motin 			return (-EINVAL);
712df849145SRui Paulo 		val = ((val & HP_MASK_BLUETOOTH_ON_AIR) != 0);
713df849145SRui Paulo 		break;
714df849145SRui Paulo 	case ACPI_HP_METHOD_BLUETOOTH_ENABLE_IF_RADIO_ON:
715df849145SRui Paulo 		val = sc->bluetooth_enable_if_radio_on;
716df849145SRui Paulo 		break;
717df849145SRui Paulo 	case ACPI_HP_METHOD_BLUETOOTH_DISABLE_IF_RADIO_OFF:
718df849145SRui Paulo 		val = sc->bluetooth_disable_if_radio_off;
719df849145SRui Paulo 		break;
720df849145SRui Paulo 	case ACPI_HP_METHOD_WWAN_ENABLED:
7211f842820SAlexander Motin 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
7221f842820SAlexander Motin 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
7231f842820SAlexander Motin 			return (-EINVAL);
724df849145SRui Paulo 		val = ((val & HP_MASK_WWAN_ENABLED) != 0);
725df849145SRui Paulo 		break;
726df849145SRui Paulo 	case ACPI_HP_METHOD_WWAN_RADIO:
7271f842820SAlexander Motin 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
7281f842820SAlexander Motin 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
7291f842820SAlexander Motin 			return (-EINVAL);
730df849145SRui Paulo 		val = ((val & HP_MASK_WWAN_RADIO) != 0);
731df849145SRui Paulo 		break;
732df849145SRui Paulo 	case ACPI_HP_METHOD_WWAN_ON_AIR:
7331f842820SAlexander Motin 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
7341f842820SAlexander Motin 		    ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0, &val))
7351f842820SAlexander Motin 			return (-EINVAL);
736df849145SRui Paulo 		val = ((val & HP_MASK_WWAN_ON_AIR) != 0);
737df849145SRui Paulo 		break;
738df849145SRui Paulo 	case ACPI_HP_METHOD_WWAN_ENABLE_IF_RADIO_ON:
739df849145SRui Paulo 		val = sc->wwan_enable_if_radio_on;
740df849145SRui Paulo 		break;
741df849145SRui Paulo 	case ACPI_HP_METHOD_WWAN_DISABLE_IF_RADIO_OFF:
742df849145SRui Paulo 		val = sc->wwan_disable_if_radio_off;
743df849145SRui Paulo 		break;
744df849145SRui Paulo 	case ACPI_HP_METHOD_ALS:
7451f842820SAlexander Motin 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
7461f842820SAlexander Motin 		    ACPI_HP_WMI_ALS_COMMAND, 0, 0, &val))
7471f842820SAlexander Motin 			return (-EINVAL);
748df849145SRui Paulo 		break;
749df849145SRui Paulo 	case ACPI_HP_METHOD_DISPLAY:
7501f842820SAlexander Motin 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
7511f842820SAlexander Motin 		    ACPI_HP_WMI_DISPLAY_COMMAND, 0, 0, &val))
7521f842820SAlexander Motin 			return (-EINVAL);
753df849145SRui Paulo 		break;
754df849145SRui Paulo 	case ACPI_HP_METHOD_HDDTEMP:
7551f842820SAlexander Motin 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
7561f842820SAlexander Motin 		    ACPI_HP_WMI_HDDTEMP_COMMAND, 0, 0, &val))
7571f842820SAlexander Motin 			return (-EINVAL);
758df849145SRui Paulo 		break;
759df849145SRui Paulo 	case ACPI_HP_METHOD_DOCK:
7601f842820SAlexander Motin 		if (acpi_hp_exec_wmi_command(sc->wmi_dev,
7611f842820SAlexander Motin 		    ACPI_HP_WMI_DOCK_COMMAND, 0, 0, &val))
7621f842820SAlexander Motin 			return (-EINVAL);
763df849145SRui Paulo 		break;
764df849145SRui Paulo 	case ACPI_HP_METHOD_CMI_DETAIL:
765df849145SRui Paulo 		val = sc->cmi_detail;
766df849145SRui Paulo 		break;
7679e760f25SRui Paulo 	case ACPI_HP_METHOD_VERBOSE:
7689e760f25SRui Paulo 		val = sc->verbose;
7699e760f25SRui Paulo 		break;
770df849145SRui Paulo 	}
771df849145SRui Paulo 
772df849145SRui Paulo 	return (val);
773df849145SRui Paulo }
774df849145SRui Paulo 
775df849145SRui Paulo static int
acpi_hp_sysctl_set(struct acpi_hp_softc * sc,int method,int arg,int oldarg)776df849145SRui Paulo acpi_hp_sysctl_set(struct acpi_hp_softc *sc, int method, int arg, int oldarg)
777df849145SRui Paulo {
778df849145SRui Paulo 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
779df849145SRui Paulo 	ACPI_SERIAL_ASSERT(hp);
780df849145SRui Paulo 
7819e760f25SRui Paulo 	if (method != ACPI_HP_METHOD_CMI_DETAIL &&
7829e760f25SRui Paulo 	    method != ACPI_HP_METHOD_VERBOSE)
783df849145SRui Paulo 		arg = arg?1:0;
784df849145SRui Paulo 
785df849145SRui Paulo 	if (arg != oldarg) {
786df849145SRui Paulo 		switch (method) {
787df849145SRui Paulo 		case ACPI_HP_METHOD_WLAN_ENABLED:
7881f842820SAlexander Motin 			if (acpi_hp_exec_wmi_command(sc->wmi_dev,
789df849145SRui Paulo 			    ACPI_HP_WMI_WIRELESS_COMMAND, 1,
7901f842820SAlexander Motin 			    arg?0x101:0x100, NULL))
7911f842820SAlexander Motin 				return (-EINVAL);
7921f842820SAlexander Motin 			break;
793df849145SRui Paulo 		case ACPI_HP_METHOD_WLAN_ENABLE_IF_RADIO_ON:
794df849145SRui Paulo 			sc->wlan_enable_if_radio_on = arg;
795df849145SRui Paulo 			acpi_hp_evaluate_auto_on_off(sc);
796df849145SRui Paulo 			break;
797df849145SRui Paulo 		case ACPI_HP_METHOD_WLAN_DISABLE_IF_RADIO_OFF:
798df849145SRui Paulo 			sc->wlan_disable_if_radio_off = arg;
799df849145SRui Paulo 			acpi_hp_evaluate_auto_on_off(sc);
800df849145SRui Paulo 			break;
801df849145SRui Paulo 		case ACPI_HP_METHOD_BLUETOOTH_ENABLED:
8021f842820SAlexander Motin 			if (acpi_hp_exec_wmi_command(sc->wmi_dev,
803df849145SRui Paulo 			    ACPI_HP_WMI_WIRELESS_COMMAND, 1,
8041f842820SAlexander Motin 			    arg?0x202:0x200, NULL))
8051f842820SAlexander Motin 				return (-EINVAL);
8061f842820SAlexander Motin 			break;
807df849145SRui Paulo 		case ACPI_HP_METHOD_BLUETOOTH_ENABLE_IF_RADIO_ON:
808df849145SRui Paulo 			sc->bluetooth_enable_if_radio_on = arg;
809df849145SRui Paulo 			acpi_hp_evaluate_auto_on_off(sc);
810df849145SRui Paulo 			break;
811df849145SRui Paulo 		case ACPI_HP_METHOD_BLUETOOTH_DISABLE_IF_RADIO_OFF:
812df849145SRui Paulo 			sc->bluetooth_disable_if_radio_off = arg?1:0;
813df849145SRui Paulo 			acpi_hp_evaluate_auto_on_off(sc);
814df849145SRui Paulo 			break;
815df849145SRui Paulo 		case ACPI_HP_METHOD_WWAN_ENABLED:
8161f842820SAlexander Motin 			if (acpi_hp_exec_wmi_command(sc->wmi_dev,
817df849145SRui Paulo 			    ACPI_HP_WMI_WIRELESS_COMMAND, 1,
8181f842820SAlexander Motin 			    arg?0x404:0x400, NULL))
8191f842820SAlexander Motin 				return (-EINVAL);
8201f842820SAlexander Motin 			break;
821df849145SRui Paulo 		case ACPI_HP_METHOD_WWAN_ENABLE_IF_RADIO_ON:
822df849145SRui Paulo 			sc->wwan_enable_if_radio_on = arg?1:0;
823df849145SRui Paulo 			acpi_hp_evaluate_auto_on_off(sc);
824df849145SRui Paulo 			break;
825df849145SRui Paulo 		case ACPI_HP_METHOD_WWAN_DISABLE_IF_RADIO_OFF:
826df849145SRui Paulo 			sc->wwan_disable_if_radio_off = arg?1:0;
827df849145SRui Paulo 			acpi_hp_evaluate_auto_on_off(sc);
828df849145SRui Paulo 			break;
829df849145SRui Paulo 		case ACPI_HP_METHOD_ALS:
8301f842820SAlexander Motin 			if (acpi_hp_exec_wmi_command(sc->wmi_dev,
8311f842820SAlexander Motin 			    ACPI_HP_WMI_ALS_COMMAND, 1, arg?1:0, NULL))
8321f842820SAlexander Motin 				return (-EINVAL);
8331f842820SAlexander Motin 			break;
834df849145SRui Paulo 		case ACPI_HP_METHOD_CMI_DETAIL:
835df849145SRui Paulo 			sc->cmi_detail = arg;
8360f73b657SRui Paulo 			if ((arg & ACPI_HP_CMI_DETAIL_SHOW_MAX_INSTANCE) !=
8370f73b657SRui Paulo 			    (oldarg & ACPI_HP_CMI_DETAIL_SHOW_MAX_INSTANCE)) {
8380f73b657SRui Paulo 			    sc->cmi_order_size = -1;
8390f73b657SRui Paulo 			}
840df849145SRui Paulo 			break;
8419e760f25SRui Paulo 		case ACPI_HP_METHOD_VERBOSE:
8429e760f25SRui Paulo 			sc->verbose = arg;
8439e760f25SRui Paulo 			break;
844df849145SRui Paulo 		}
845df849145SRui Paulo 	}
846df849145SRui Paulo 
847df849145SRui Paulo 	return (0);
848df849145SRui Paulo }
849df849145SRui Paulo 
850df849145SRui Paulo static __inline void
acpi_hp_free_buffer(ACPI_BUFFER * buf)851df849145SRui Paulo acpi_hp_free_buffer(ACPI_BUFFER* buf) {
852df849145SRui Paulo 	if (buf && buf->Pointer) {
853df849145SRui Paulo 		AcpiOsFree(buf->Pointer);
854df849145SRui Paulo 	}
855df849145SRui Paulo }
856df849145SRui Paulo 
857df849145SRui Paulo static void
acpi_hp_notify(ACPI_HANDLE h,UINT32 notify,void * context)858df849145SRui Paulo acpi_hp_notify(ACPI_HANDLE h, UINT32 notify, void *context)
859df849145SRui Paulo {
860df849145SRui Paulo 	device_t dev = context;
861df849145SRui Paulo 	ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
862df849145SRui Paulo 
863df849145SRui Paulo 	struct acpi_hp_softc *sc = device_get_softc(dev);
864df849145SRui Paulo 	ACPI_BUFFER response = { ACPI_ALLOCATE_BUFFER, NULL };
865df849145SRui Paulo 	ACPI_OBJECT *obj;
866df849145SRui Paulo 	ACPI_WMI_GET_EVENT_DATA(sc->wmi_dev, notify, &response);
867df849145SRui Paulo 	obj = (ACPI_OBJECT*) response.Pointer;
868df849145SRui Paulo 	if (obj && obj->Type == ACPI_TYPE_BUFFER && obj->Buffer.Length == 8) {
8691f842820SAlexander Motin 		switch (*((UINT8 *) obj->Buffer.Pointer)) {
8701f842820SAlexander Motin 		case ACPI_HP_EVENT_WIRELESS:
871df849145SRui Paulo 			acpi_hp_evaluate_auto_on_off(sc);
8721f842820SAlexander Motin 			break;
8731f842820SAlexander Motin 		default:
8741f842820SAlexander Motin 			if (sc->verbose) {
8751f842820SAlexander Motin 				device_printf(sc->dev, "Event %02x\n",
8761f842820SAlexander Motin 				    *((UINT8 *) obj->Buffer.Pointer));
8771f842820SAlexander Motin 			}
8781f842820SAlexander Motin 			break;
879df849145SRui Paulo 		}
880df849145SRui Paulo 	}
881df849145SRui Paulo 	acpi_hp_free_buffer(&response);
882df849145SRui Paulo }
883df849145SRui Paulo 
884df849145SRui Paulo static int
acpi_hp_exec_wmi_command(device_t wmi_dev,int command,int is_write,int val,int * retval)8851f842820SAlexander Motin acpi_hp_exec_wmi_command(device_t wmi_dev, int command, int is_write,
8861f842820SAlexander Motin     int val, int *retval)
887df849145SRui Paulo {
8881f842820SAlexander Motin 	UINT32		params[4+32] = { 0x55434553, is_write ? 2 : 1,
8891f842820SAlexander Motin 			    command, 4, val};
890df849145SRui Paulo 	UINT32*		result;
891df849145SRui Paulo 	ACPI_OBJECT	*obj;
892df849145SRui Paulo 	ACPI_BUFFER	in = { sizeof(params), &params };
893df849145SRui Paulo 	ACPI_BUFFER	out = { ACPI_ALLOCATE_BUFFER, NULL };
8941f842820SAlexander Motin 	int res;
895df849145SRui Paulo 
896df849145SRui Paulo 	if (ACPI_FAILURE(ACPI_WMI_EVALUATE_CALL(wmi_dev, ACPI_HP_WMI_BIOS_GUID,
897df849145SRui Paulo 		    0, 0x3, &in, &out))) {
898df849145SRui Paulo 		acpi_hp_free_buffer(&out);
899df849145SRui Paulo 		return (-EINVAL);
900df849145SRui Paulo 	}
901df849145SRui Paulo 	obj = out.Pointer;
902df849145SRui Paulo 	if (!obj || obj->Type != ACPI_TYPE_BUFFER) {
903df849145SRui Paulo 		acpi_hp_free_buffer(&out);
904df849145SRui Paulo 		return (-EINVAL);
905df849145SRui Paulo 	}
906df849145SRui Paulo 	result = (UINT32*) obj->Buffer.Pointer;
9071f842820SAlexander Motin 	res = result[1];
9081f842820SAlexander Motin 	if (res == 0 && retval != NULL)
9091f842820SAlexander Motin 		*retval = result[2];
910df849145SRui Paulo 	acpi_hp_free_buffer(&out);
911df849145SRui Paulo 
9121f842820SAlexander Motin 	return (res);
913df849145SRui Paulo }
914df849145SRui Paulo 
915df849145SRui Paulo static __inline char*
acpi_hp_get_string_from_object(ACPI_OBJECT * obj,char * dst,size_t size)916df849145SRui Paulo acpi_hp_get_string_from_object(ACPI_OBJECT* obj, char* dst, size_t size) {
917df849145SRui Paulo 	int	length;
9189e760f25SRui Paulo 
919df849145SRui Paulo 	dst[0] = 0;
920df849145SRui Paulo 	if (obj->Type == ACPI_TYPE_STRING) {
921df849145SRui Paulo 		length = obj->String.Length+1;
922df849145SRui Paulo 		if (length > size) {
923df849145SRui Paulo 			length = size - 1;
924df849145SRui Paulo 		}
925df849145SRui Paulo 		strlcpy(dst, obj->String.Pointer, length);
926df849145SRui Paulo 		acpi_hp_hex_decode(dst);
927df849145SRui Paulo 	}
928df849145SRui Paulo 
929df849145SRui Paulo 	return (dst);
930df849145SRui Paulo }
931df849145SRui Paulo 
932df849145SRui Paulo /*
933df849145SRui Paulo  * Read BIOS Setting block in instance "instance".
934df849145SRui Paulo  * The block returned is ACPI_TYPE_PACKAGE which should contain the following
935df849145SRui Paulo  * elements:
936df849145SRui Paulo  * Index Meaning
937df849145SRui Paulo  * 0        Setting Name [string]
938df849145SRui Paulo  * 1        Value (comma separated, asterisk marks the current value) [string]
939df849145SRui Paulo  * 2        Path within the bios hierarchy [string]
940df849145SRui Paulo  * 3        IsReadOnly [int]
941df849145SRui Paulo  * 4        DisplayInUI [int]
942df849145SRui Paulo  * 5        RequiresPhysicalPresence [int]
943df849145SRui Paulo  * 6        Sequence for ordering within the bios settings (absolute) [int]
944df849145SRui Paulo  * 7        Length of prerequisites array [int]
9459e760f25SRui Paulo  * 8..8+[7] PrerequisiteN [string]
9469e760f25SRui Paulo  * 9+[7]    Current value (in case of enum) [string] / Array length [int]
9479e760f25SRui Paulo  * 10+[7]   Enum length [int] / Array values
9489e760f25SRui Paulo  * 11+[7]ff Enum value at index x [string]
949df849145SRui Paulo  */
950df849145SRui Paulo static int
acpi_hp_get_cmi_block(device_t wmi_dev,const char * guid,UINT8 instance,char * outbuf,size_t outsize,UINT32 * sequence,int detail)951df849145SRui Paulo acpi_hp_get_cmi_block(device_t wmi_dev, const char* guid, UINT8 instance,
952df849145SRui Paulo     char* outbuf, size_t outsize, UINT32* sequence, int detail)
953df849145SRui Paulo {
954df849145SRui Paulo 	ACPI_OBJECT	*obj;
955df849145SRui Paulo 	ACPI_BUFFER	out = { ACPI_ALLOCATE_BUFFER, NULL };
956df849145SRui Paulo 	int		i;
957df849145SRui Paulo 	int		outlen;
958df849145SRui Paulo 	int		has_enums = 0;
9599e760f25SRui Paulo 	int		valuebase = 0;
9602cafe2f9SXin LI 	char		string_buffer[255];
961df849145SRui Paulo 	int		enumbase;
962df849145SRui Paulo 
963df849145SRui Paulo 	outlen = 0;
964df849145SRui Paulo 	outbuf[0] = 0;
965df849145SRui Paulo 	if (ACPI_FAILURE(ACPI_WMI_GET_BLOCK(wmi_dev, guid, instance, &out))) {
966df849145SRui Paulo 		acpi_hp_free_buffer(&out);
967df849145SRui Paulo 		return (-EINVAL);
968df849145SRui Paulo 	}
969df849145SRui Paulo 	obj = out.Pointer;
97080fba821SJung-uk Kim 	if (!obj || obj->Type != ACPI_TYPE_PACKAGE) {
971df849145SRui Paulo 		acpi_hp_free_buffer(&out);
972df849145SRui Paulo 		return (-EINVAL);
973df849145SRui Paulo 	}
974df849145SRui Paulo 
9751f842820SAlexander Motin 	/* Check if first 6 bytes matches our expectations. */
9761f842820SAlexander Motin 	if (obj->Package.Count < 8 ||
9771f842820SAlexander Motin 	    obj->Package.Elements[0].Type != ACPI_TYPE_STRING ||
9781f842820SAlexander Motin 	    obj->Package.Elements[1].Type != ACPI_TYPE_STRING ||
9791f842820SAlexander Motin 	    obj->Package.Elements[2].Type != ACPI_TYPE_STRING ||
9801f842820SAlexander Motin 	    obj->Package.Elements[3].Type != ACPI_TYPE_INTEGER ||
9811f842820SAlexander Motin 	    obj->Package.Elements[4].Type != ACPI_TYPE_INTEGER ||
9821f842820SAlexander Motin 	    obj->Package.Elements[5].Type != ACPI_TYPE_INTEGER ||
9831f842820SAlexander Motin 	    obj->Package.Elements[6].Type != ACPI_TYPE_INTEGER ||
9841f842820SAlexander Motin 	    obj->Package.Elements[7].Type != ACPI_TYPE_INTEGER) {
9851f842820SAlexander Motin 		acpi_hp_free_buffer(&out);
9861f842820SAlexander Motin 		return (-EINVAL);
9879e760f25SRui Paulo 	}
9889e760f25SRui Paulo 
9891f842820SAlexander Motin 	/* Skip prerequisites and optionally array. */
9901f842820SAlexander Motin 	valuebase = 8 + obj->Package.Elements[7].Integer.Value;
9911f842820SAlexander Motin 	if (obj->Package.Count <= valuebase) {
9921f842820SAlexander Motin 		acpi_hp_free_buffer(&out);
9931f842820SAlexander Motin 		return (-EINVAL);
9941f842820SAlexander Motin 	}
9951f842820SAlexander Motin 	if (obj->Package.Elements[valuebase].Type == ACPI_TYPE_INTEGER)
9961f842820SAlexander Motin 		valuebase += 1 + obj->Package.Elements[valuebase].Integer.Value;
9971f842820SAlexander Motin 
9981f842820SAlexander Motin 	/* Check if we have value and enum. */
9991f842820SAlexander Motin 	if (obj->Package.Count <= valuebase + 1 ||
10001f842820SAlexander Motin 	    obj->Package.Elements[valuebase].Type != ACPI_TYPE_STRING ||
10011f842820SAlexander Motin 	    obj->Package.Elements[valuebase+1].Type != ACPI_TYPE_INTEGER) {
10021f842820SAlexander Motin 		acpi_hp_free_buffer(&out);
10031f842820SAlexander Motin 		return (-EINVAL);
10041f842820SAlexander Motin 	}
10059e760f25SRui Paulo 	enumbase = valuebase + 1;
10061f842820SAlexander Motin 	if (obj->Package.Count <= valuebase +
10071f842820SAlexander Motin 	        obj->Package.Elements[enumbase].Integer.Value) {
10081f842820SAlexander Motin 		acpi_hp_free_buffer(&out);
10091f842820SAlexander Motin 		return (-EINVAL);
10101f842820SAlexander Motin 	}
10111f842820SAlexander Motin 
1012df849145SRui Paulo 	if (detail & ACPI_HP_CMI_DETAIL_PATHS) {
1013df849145SRui Paulo 		strlcat(outbuf, acpi_hp_get_string_from_object(
10142cafe2f9SXin LI 		    &obj->Package.Elements[2],
10152cafe2f9SXin LI 		    string_buffer, sizeof(string_buffer)), outsize);
1016df849145SRui Paulo 		outlen += 48;
1017df849145SRui Paulo 		while (strlen(outbuf) < outlen)
1018df849145SRui Paulo 			strlcat(outbuf, " ", outsize);
1019df849145SRui Paulo 	}
1020df849145SRui Paulo 	strlcat(outbuf, acpi_hp_get_string_from_object(
10212cafe2f9SXin LI 	    &obj->Package.Elements[0],
10222cafe2f9SXin LI 	    string_buffer, sizeof(string_buffer)), outsize);
1023df849145SRui Paulo 	outlen += 43;
1024df849145SRui Paulo 	while (strlen(outbuf) < outlen)
1025df849145SRui Paulo 		strlcat(outbuf, " ", outsize);
1026df849145SRui Paulo 	strlcat(outbuf, acpi_hp_get_string_from_object(
10272cafe2f9SXin LI 	    &obj->Package.Elements[valuebase],
10282cafe2f9SXin LI 	    string_buffer, sizeof(string_buffer)), outsize);
1029df849145SRui Paulo 	outlen += 21;
1030df849145SRui Paulo 	while (strlen(outbuf) < outlen)
1031df849145SRui Paulo 		strlcat(outbuf, " ", outsize);
1032df849145SRui Paulo 	for (i = 0; i < strlen(outbuf); ++i)
1033df849145SRui Paulo 		if (outbuf[i] == '\\')
1034df849145SRui Paulo 			outbuf[i] = '/';
1035df849145SRui Paulo 	if (detail & ACPI_HP_CMI_DETAIL_ENUMS) {
1036df849145SRui Paulo 		for (i = enumbase + 1; i < enumbase + 1 +
10371f842820SAlexander Motin 		    obj->Package.Elements[enumbase].Integer.Value; ++i) {
1038df849145SRui Paulo 			acpi_hp_get_string_from_object(
10392cafe2f9SXin LI 			    &obj->Package.Elements[i],
10402cafe2f9SXin LI 			    string_buffer, sizeof(string_buffer));
1041df849145SRui Paulo 			if (strlen(string_buffer) > 1 ||
1042df849145SRui Paulo 			    (strlen(string_buffer) == 1 &&
1043df849145SRui Paulo 			    string_buffer[0] != ' ')) {
1044df849145SRui Paulo 				if (has_enums)
1045df849145SRui Paulo 					strlcat(outbuf, "/", outsize);
1046df849145SRui Paulo 				else
1047df849145SRui Paulo 					strlcat(outbuf, " (", outsize);
1048df849145SRui Paulo 				strlcat(outbuf, string_buffer, outsize);
1049df849145SRui Paulo 				has_enums = 1;
1050df849145SRui Paulo 			}
1051df849145SRui Paulo 		}
1052df849145SRui Paulo 	}
1053df849145SRui Paulo 	if (has_enums)
1054df849145SRui Paulo 		strlcat(outbuf, ")", outsize);
1055df849145SRui Paulo 	if (detail & ACPI_HP_CMI_DETAIL_FLAGS) {
1056df849145SRui Paulo 		strlcat(outbuf, obj->Package.Elements[3].Integer.Value ?
1057df849145SRui Paulo 		    " [ReadOnly]" : "", outsize);
1058df849145SRui Paulo 		strlcat(outbuf, obj->Package.Elements[4].Integer.Value ?
1059df849145SRui Paulo 		    "" : " [NOUI]", outsize);
1060df849145SRui Paulo 		strlcat(outbuf, obj->Package.Elements[5].Integer.Value ?
1061df849145SRui Paulo 		    " [RPP]" : "", outsize);
1062df849145SRui Paulo 	}
1063df849145SRui Paulo 	*sequence = (UINT32) obj->Package.Elements[6].Integer.Value;
1064df849145SRui Paulo 	acpi_hp_free_buffer(&out);
1065df849145SRui Paulo 
1066df849145SRui Paulo 	return (0);
1067df849145SRui Paulo }
1068df849145SRui Paulo 
1069df849145SRui Paulo /*
1070df849145SRui Paulo  * Convert given two digit hex string (hexin) to an UINT8 referenced
1071df849145SRui Paulo  * by byteout.
1072df849145SRui Paulo  * Return != 0 if the was a problem (invalid input)
1073df849145SRui Paulo  */
acpi_hp_hex_to_int(const UINT8 * hexin,UINT8 * byteout)1074df849145SRui Paulo static __inline int acpi_hp_hex_to_int(const UINT8 *hexin, UINT8 *byteout)
1075df849145SRui Paulo {
1076df849145SRui Paulo 	unsigned int	hi;
1077df849145SRui Paulo 	unsigned int	lo;
1078df849145SRui Paulo 
1079df849145SRui Paulo 	hi = hexin[0];
1080df849145SRui Paulo 	lo = hexin[1];
1081df849145SRui Paulo 	if ('0' <= hi && hi <= '9')
1082df849145SRui Paulo 		hi -= '0';
1083df849145SRui Paulo 	else if ('A' <= hi && hi <= 'F')
1084df849145SRui Paulo 		hi -= ('A' - 10);
1085df849145SRui Paulo 	else if ('a' <= hi && hi <= 'f')
1086df849145SRui Paulo 		hi -= ('a' - 10);
1087df849145SRui Paulo 	else
1088df849145SRui Paulo 		return (1);
1089df849145SRui Paulo 	if ('0' <= lo && lo <= '9')
1090df849145SRui Paulo 		lo -= '0';
1091df849145SRui Paulo 	else if ('A' <= lo && lo <= 'F')
1092df849145SRui Paulo 		lo -= ('A' - 10);
1093df849145SRui Paulo 	else if ('a' <= lo && lo <= 'f')
1094df849145SRui Paulo 		lo -= ('a' - 10);
1095df849145SRui Paulo 	else
1096df849145SRui Paulo 		return (1);
1097df849145SRui Paulo 	*byteout = (hi << 4) + lo;
1098df849145SRui Paulo 
1099df849145SRui Paulo 	return (0);
1100df849145SRui Paulo }
1101df849145SRui Paulo 
1102df849145SRui Paulo static void
acpi_hp_hex_decode(char * buffer)1103df849145SRui Paulo acpi_hp_hex_decode(char* buffer)
1104df849145SRui Paulo {
1105df849145SRui Paulo 	int	i;
1106df849145SRui Paulo 	int	length = strlen(buffer);
1107df849145SRui Paulo 	UINT8	*uin;
1108df849145SRui Paulo 	UINT8	uout;
1109df849145SRui Paulo 
11104ed3c0e7SPedro F. Giffuni 	if (rounddown((int)length, 2) == length || length < 10)
11114ed3c0e7SPedro F. Giffuni 		return;
1112df849145SRui Paulo 
1113df849145SRui Paulo 	for (i = 0; i<length; ++i) {
1114df849145SRui Paulo 		if (!((i+1)%3)) {
1115df849145SRui Paulo 			if (buffer[i] != ' ')
1116df849145SRui Paulo 				return;
1117df849145SRui Paulo 		}
1118df849145SRui Paulo 		else
1119df849145SRui Paulo 			if (!((buffer[i] >= '0' && buffer[i] <= '9') ||
1120df849145SRui Paulo 		    	    (buffer[i] >= 'A' && buffer[i] <= 'F')))
1121df849145SRui Paulo 				return;
1122df849145SRui Paulo 	}
1123df849145SRui Paulo 
1124df849145SRui Paulo 	for (i = 0; i<length; i += 3) {
1125df849145SRui Paulo 		uin = &buffer[i];
1126df849145SRui Paulo 		uout = 0;
1127df849145SRui Paulo 		acpi_hp_hex_to_int(uin, &uout);
1128df849145SRui Paulo 		buffer[i/3] = (char) uout;
1129df849145SRui Paulo 	}
1130df849145SRui Paulo 	buffer[(length+1)/3] = 0;
1131df849145SRui Paulo }
1132df849145SRui Paulo 
1133df849145SRui Paulo /*
1134df849145SRui Paulo  * open hpcmi device
1135df849145SRui Paulo  */
1136df849145SRui Paulo static int
acpi_hp_hpcmi_open(struct cdev * dev,int flags,int mode,struct thread * td)1137df849145SRui Paulo acpi_hp_hpcmi_open(struct cdev* dev, int flags, int mode, struct thread *td)
1138df849145SRui Paulo {
1139df849145SRui Paulo 	struct acpi_hp_softc	*sc;
1140df849145SRui Paulo 	int			ret;
1141df849145SRui Paulo 
1142df849145SRui Paulo 	if (dev == NULL || dev->si_drv1 == NULL)
1143df849145SRui Paulo 		return (EBADF);
1144df849145SRui Paulo 	sc = dev->si_drv1;
1145df849145SRui Paulo 
1146df849145SRui Paulo 	ACPI_SERIAL_BEGIN(hp);
1147df849145SRui Paulo 	if (sc->hpcmi_open_pid != 0) {
1148df849145SRui Paulo 		ret = EBUSY;
1149df849145SRui Paulo 	}
1150df849145SRui Paulo 	else {
1151df849145SRui Paulo 		if (sbuf_new(&sc->hpcmi_sbuf, NULL, 4096, SBUF_AUTOEXTEND)
1152df849145SRui Paulo 		    == NULL) {
1153df849145SRui Paulo 			ret = ENXIO;
1154df849145SRui Paulo 		} else {
1155df849145SRui Paulo 			sc->hpcmi_open_pid = td->td_proc->p_pid;
1156df849145SRui Paulo 			sc->hpcmi_bufptr = 0;
1157df849145SRui Paulo 			ret = 0;
1158df849145SRui Paulo 		}
1159df849145SRui Paulo 	}
1160df849145SRui Paulo 	ACPI_SERIAL_END(hp);
1161df849145SRui Paulo 
1162df849145SRui Paulo 	return (ret);
1163df849145SRui Paulo }
1164df849145SRui Paulo 
1165df849145SRui Paulo /*
1166df849145SRui Paulo  * close hpcmi device
1167df849145SRui Paulo  */
1168df849145SRui Paulo static int
acpi_hp_hpcmi_close(struct cdev * dev,int flags,int mode,struct thread * td)1169df849145SRui Paulo acpi_hp_hpcmi_close(struct cdev* dev, int flags, int mode, struct thread *td)
1170df849145SRui Paulo {
1171df849145SRui Paulo 	struct acpi_hp_softc	*sc;
1172df849145SRui Paulo 	int			ret;
1173df849145SRui Paulo 
1174df849145SRui Paulo 	if (dev == NULL || dev->si_drv1 == NULL)
1175df849145SRui Paulo 		return (EBADF);
1176df849145SRui Paulo 	sc = dev->si_drv1;
1177df849145SRui Paulo 
1178df849145SRui Paulo 	ACPI_SERIAL_BEGIN(hp);
1179df849145SRui Paulo 	if (sc->hpcmi_open_pid == 0) {
1180df849145SRui Paulo 		ret = EBADF;
1181df849145SRui Paulo 	}
1182df849145SRui Paulo 	else {
1183df849145SRui Paulo 		if (sc->hpcmi_bufptr != -1) {
1184df849145SRui Paulo 			sbuf_delete(&sc->hpcmi_sbuf);
1185df849145SRui Paulo 			sc->hpcmi_bufptr = -1;
1186df849145SRui Paulo 		}
1187df849145SRui Paulo 		sc->hpcmi_open_pid = 0;
1188df849145SRui Paulo 		ret = 0;
1189df849145SRui Paulo 	}
1190df849145SRui Paulo 	ACPI_SERIAL_END(hp);
1191df849145SRui Paulo 
1192df849145SRui Paulo 	return (ret);
1193df849145SRui Paulo }
1194df849145SRui Paulo 
1195df849145SRui Paulo /*
1196df849145SRui Paulo  * Read from hpcmi bios information
1197df849145SRui Paulo  */
1198df849145SRui Paulo static int
acpi_hp_hpcmi_read(struct cdev * dev,struct uio * buf,int flag)1199df849145SRui Paulo acpi_hp_hpcmi_read(struct cdev *dev, struct uio *buf, int flag)
1200df849145SRui Paulo {
1201df849145SRui Paulo 	struct acpi_hp_softc	*sc;
1202df849145SRui Paulo 	int			pos, i, l, ret;
1203df849145SRui Paulo 	UINT8			instance;
12040f73b657SRui Paulo 	UINT8			maxInstance;
1205df849145SRui Paulo 	UINT32			sequence;
12062cafe2f9SXin LI 	char			line[1025];
1207df849145SRui Paulo 
1208df849145SRui Paulo 	if (dev == NULL || dev->si_drv1 == NULL)
1209df849145SRui Paulo 		return (EBADF);
1210df849145SRui Paulo 	sc = dev->si_drv1;
1211df849145SRui Paulo 
1212df849145SRui Paulo 	ACPI_SERIAL_BEGIN(hp);
1213df849145SRui Paulo 	if (sc->hpcmi_open_pid != buf->uio_td->td_proc->p_pid
1214df849145SRui Paulo 	    || sc->hpcmi_bufptr == -1) {
1215df849145SRui Paulo 		ret = EBADF;
1216df849145SRui Paulo 	}
1217df849145SRui Paulo 	else {
1218df849145SRui Paulo 		if (!sbuf_done(&sc->hpcmi_sbuf)) {
1219df849145SRui Paulo 			if (sc->cmi_order_size < 0) {
12200f73b657SRui Paulo 				maxInstance = sc->has_cmi;
12210f73b657SRui Paulo 				if (!(sc->cmi_detail &
12220f73b657SRui Paulo 				    ACPI_HP_CMI_DETAIL_SHOW_MAX_INSTANCE) &&
12230f73b657SRui Paulo 				    maxInstance > 0) {
12240f73b657SRui Paulo 					maxInstance--;
12250f73b657SRui Paulo 				}
1226df849145SRui Paulo 				sc->cmi_order_size = 0;
12270f73b657SRui Paulo 				for (instance = 0; instance < maxInstance;
1228df849145SRui Paulo 				    ++instance) {
1229df849145SRui Paulo 					if (acpi_hp_get_cmi_block(sc->wmi_dev,
1230df849145SRui Paulo 						ACPI_HP_WMI_CMI_GUID, instance,
12312cafe2f9SXin LI 						line, sizeof(line), &sequence,
1232df849145SRui Paulo 						sc->cmi_detail)) {
12330f73b657SRui Paulo 						instance = maxInstance;
1234df849145SRui Paulo 					}
1235df849145SRui Paulo 					else {
1236df849145SRui Paulo 						pos = sc->cmi_order_size;
1237df849145SRui Paulo 						for (i=0;
1238df849145SRui Paulo 						  i<sc->cmi_order_size && i<127;
1239df849145SRui Paulo 						     ++i) {
1240df849145SRui Paulo 				if (sc->cmi_order[i].sequence > sequence) {
1241df849145SRui Paulo 								pos = i;
1242df849145SRui Paulo 								break;
1243df849145SRui Paulo 							}
1244df849145SRui Paulo 						}
1245df849145SRui Paulo 						for (i=sc->cmi_order_size;
1246df849145SRui Paulo 						    i>pos;
1247df849145SRui Paulo 						    --i) {
1248df849145SRui Paulo 						sc->cmi_order[i].sequence =
1249df849145SRui Paulo 						    sc->cmi_order[i-1].sequence;
1250df849145SRui Paulo 						sc->cmi_order[i].instance =
1251df849145SRui Paulo 						    sc->cmi_order[i-1].instance;
1252df849145SRui Paulo 						}
1253df849145SRui Paulo 						sc->cmi_order[pos].sequence =
1254df849145SRui Paulo 						    sequence;
1255df849145SRui Paulo 						sc->cmi_order[pos].instance =
1256df849145SRui Paulo 						    instance;
1257df849145SRui Paulo 						sc->cmi_order_size++;
1258df849145SRui Paulo 					}
1259df849145SRui Paulo 				}
1260df849145SRui Paulo 			}
1261df849145SRui Paulo 			for (i=0; i<sc->cmi_order_size; ++i) {
1262df849145SRui Paulo 				if (!acpi_hp_get_cmi_block(sc->wmi_dev,
1263df849145SRui Paulo 				    ACPI_HP_WMI_CMI_GUID,
12642cafe2f9SXin LI 				    sc->cmi_order[i].instance, line, sizeof(line),
1265df849145SRui Paulo 				    &sequence, sc->cmi_detail)) {
1266df849145SRui Paulo 					sbuf_printf(&sc->hpcmi_sbuf, "%s\n", line);
1267df849145SRui Paulo 				}
1268df849145SRui Paulo 			}
1269df849145SRui Paulo 			sbuf_finish(&sc->hpcmi_sbuf);
1270df849145SRui Paulo 		}
1271df849145SRui Paulo 		if (sbuf_len(&sc->hpcmi_sbuf) <= 0) {
1272df849145SRui Paulo 			sbuf_delete(&sc->hpcmi_sbuf);
1273df849145SRui Paulo 			sc->hpcmi_bufptr = -1;
1274df849145SRui Paulo 			sc->hpcmi_open_pid = 0;
1275df849145SRui Paulo 			ret = ENOMEM;
1276df849145SRui Paulo 		} else {
1277df849145SRui Paulo 			l = min(buf->uio_resid, sbuf_len(&sc->hpcmi_sbuf) -
1278df849145SRui Paulo 			    sc->hpcmi_bufptr);
1279df849145SRui Paulo 			ret = (l > 0)?uiomove(sbuf_data(&sc->hpcmi_sbuf) +
1280df849145SRui Paulo 			    sc->hpcmi_bufptr, l, buf) : 0;
1281df849145SRui Paulo 			sc->hpcmi_bufptr += l;
1282df849145SRui Paulo 		}
1283df849145SRui Paulo 	}
1284df849145SRui Paulo 	ACPI_SERIAL_END(hp);
1285df849145SRui Paulo 
1286df849145SRui Paulo 	return (ret);
1287df849145SRui Paulo }
1288