xref: /freebsd/sys/dev/acpi_support/acpi_ibm.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /*-
2  * Copyright (c) 2004 Takanori Watanabe
3  * Copyright (c) 2005 Markus Brueffer <markus@FreeBSD.org>
4  * All rights reserved.
5  * Copyright (c) 2020 Ali Abdallah <ali.abdallah@suse.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 /*
31  * Driver for extra ACPI-controlled gadgets found on ThinkPad laptops.
32  * Inspired by the ibm-acpi and tpb projects which implement these features
33  * on Linux.
34  *
35  *   acpi-ibm: <http://ibm-acpi.sourceforge.net/>
36  *        tpb: <http://www.nongnu.org/tpb/>
37  */
38 
39 #include "opt_acpi.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/bus.h>
44 #include <machine/cpufunc.h>
45 
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
48 
49 #include "acpi_if.h"
50 #include <sys/module.h>
51 #include <dev/acpica/acpivar.h>
52 #include <dev/led/led.h>
53 #include <sys/power.h>
54 #include <sys/sbuf.h>
55 #include <sys/sysctl.h>
56 #include <isa/rtc.h>
57 
58 #define _COMPONENT	ACPI_OEM
59 ACPI_MODULE_NAME("IBM")
60 
61 /* Internal methods */
62 #define ACPI_IBM_METHOD_EVENTS		1
63 #define ACPI_IBM_METHOD_EVENTMASK	2
64 #define ACPI_IBM_METHOD_HOTKEY		3
65 #define ACPI_IBM_METHOD_BRIGHTNESS	4
66 #define ACPI_IBM_METHOD_VOLUME		5
67 #define ACPI_IBM_METHOD_MUTE		6
68 #define ACPI_IBM_METHOD_THINKLIGHT	7
69 #define ACPI_IBM_METHOD_BLUETOOTH	8
70 #define ACPI_IBM_METHOD_WLAN		9
71 #define ACPI_IBM_METHOD_FANSPEED	10
72 #define ACPI_IBM_METHOD_FANLEVEL	11
73 #define ACPI_IBM_METHOD_FANSTATUS	12
74 #define ACPI_IBM_METHOD_THERMAL		13
75 #define ACPI_IBM_METHOD_HANDLEREVENTS	14
76 #define ACPI_IBM_METHOD_MIC_LED		15
77 #define ACPI_IBM_METHOD_PRIVACYGUARD	16
78 
79 /* Hotkeys/Buttons */
80 #define IBM_RTC_HOTKEY1			0x64
81 #define   IBM_RTC_MASK_HOME		(1 << 0)
82 #define   IBM_RTC_MASK_SEARCH		(1 << 1)
83 #define   IBM_RTC_MASK_MAIL		(1 << 2)
84 #define   IBM_RTC_MASK_WLAN		(1 << 5)
85 #define IBM_RTC_HOTKEY2			0x65
86 #define   IBM_RTC_MASK_THINKPAD		(1 << 3)
87 #define   IBM_RTC_MASK_ZOOM		(1 << 5)
88 #define   IBM_RTC_MASK_VIDEO		(1 << 6)
89 #define   IBM_RTC_MASK_HIBERNATE	(1 << 7)
90 #define IBM_RTC_THINKLIGHT		0x66
91 #define   IBM_RTC_MASK_THINKLIGHT	(1 << 4)
92 #define IBM_RTC_SCREENEXPAND		0x67
93 #define   IBM_RTC_MASK_SCREENEXPAND	(1 << 5)
94 #define IBM_RTC_BRIGHTNESS		0x6c
95 #define   IBM_RTC_MASK_BRIGHTNESS	(1 << 5)
96 #define IBM_RTC_VOLUME			0x6e
97 #define   IBM_RTC_MASK_VOLUME		(1 << 7)
98 
99 /* Embedded Controller registers */
100 #define IBM_EC_BRIGHTNESS		0x31
101 #define   IBM_EC_MASK_BRI		0x7
102 #define IBM_EC_VOLUME			0x30
103 #define   IBM_EC_MASK_VOL		0xf
104 #define   IBM_EC_MASK_MUTE		(1 << 6)
105 #define IBM_EC_FANSTATUS		0x2F
106 #define   IBM_EC_MASK_FANLEVEL		0x3f
107 #define   IBM_EC_MASK_FANUNTHROTTLED	(1 << 6)
108 #define   IBM_EC_MASK_FANSTATUS		(1 << 7)
109 #define IBM_EC_FANSPEED			0x84
110 
111 /* CMOS Commands */
112 #define IBM_CMOS_VOLUME_DOWN		0
113 #define IBM_CMOS_VOLUME_UP		1
114 #define IBM_CMOS_VOLUME_MUTE		2
115 #define IBM_CMOS_BRIGHTNESS_UP		4
116 #define IBM_CMOS_BRIGHTNESS_DOWN	5
117 
118 /* ACPI methods */
119 #define IBM_NAME_KEYLIGHT		"KBLT"
120 #define IBM_NAME_WLAN_BT_GET		"GBDC"
121 #define IBM_NAME_WLAN_BT_SET		"SBDC"
122 #define   IBM_NAME_MASK_BT		(1 << 1)
123 #define   IBM_NAME_MASK_WLAN		(1 << 2)
124 #define IBM_NAME_THERMAL_GET		"TMP7"
125 #define IBM_NAME_THERMAL_UPDT		"UPDT"
126 #define IBM_NAME_PRIVACYGUARD_GET	"GSSS"
127 #define IBM_NAME_PRIVACYGUARD_SET	"SSSS"
128 
129 #define IBM_NAME_EVENTS_STATUS_GET	"DHKC"
130 #define IBM_NAME_EVENTS_MASK_GET	"DHKN"
131 #define IBM_NAME_EVENTS_STATUS_SET	"MHKC"
132 #define IBM_NAME_EVENTS_MASK_SET	"MHKM"
133 #define IBM_NAME_EVENTS_GET		"MHKP"
134 #define IBM_NAME_EVENTS_AVAILMASK	"MHKA"
135 
136 /* Event Code */
137 #define IBM_EVENT_LCD_BACKLIGHT		0x03
138 #define IBM_EVENT_SUSPEND_TO_RAM	0x04
139 #define IBM_EVENT_BLUETOOTH		0x05
140 #define IBM_EVENT_SCREEN_EXPAND		0x07
141 #define IBM_EVENT_SUSPEND_TO_DISK	0x0c
142 #define IBM_EVENT_BRIGHTNESS_UP		0x10
143 #define IBM_EVENT_BRIGHTNESS_DOWN	0x11
144 #define IBM_EVENT_THINKLIGHT		0x12
145 #define IBM_EVENT_ZOOM			0x14
146 #define IBM_EVENT_VOLUME_UP		0x15
147 #define IBM_EVENT_VOLUME_DOWN		0x16
148 #define IBM_EVENT_MUTE			0x17
149 #define IBM_EVENT_ACCESS_IBM_BUTTON	0x18
150 
151 /* Device-specific register flags */
152 #define IBM_FLAG_PRIVACYGUARD_DEVICE_PRESENT	0x10000
153 #define IBM_FLAG_PRIVACYGUARD_ON	0x1
154 
155 #define ABS(x) (((x) < 0)? -(x) : (x))
156 
157 struct acpi_ibm_softc {
158 	device_t	dev;
159 	ACPI_HANDLE	handle;
160 
161 	/* Embedded controller */
162 	device_t	ec_dev;
163 	ACPI_HANDLE	ec_handle;
164 
165 	/* CMOS */
166 	ACPI_HANDLE	cmos_handle;
167 
168 	/* Fan status */
169 	ACPI_HANDLE	fan_handle;
170 	int		fan_levels;
171 
172 	/* Keylight commands and states */
173 	ACPI_HANDLE	light_handle;
174 	int		light_cmd_on;
175 	int		light_cmd_off;
176 	int		light_val;
177 	int		light_get_supported;
178 	int		light_set_supported;
179 
180 	/* led(4) interface */
181 	struct cdev	*led_dev;
182 	int		led_busy;
183 	int		led_state;
184 
185 	/* Mic led handle */
186 	ACPI_HANDLE	mic_led_handle;
187 	int		mic_led_state;
188 
189 	int		wlan_bt_flags;
190 	int		thermal_updt_supported;
191 
192 	unsigned int	events_availmask;
193 	unsigned int	events_initialmask;
194 	int		events_mask_supported;
195 	int		events_enable;
196 
197 	unsigned int	handler_events;
198 
199 	struct sysctl_ctx_list	*sysctl_ctx;
200 	struct sysctl_oid	*sysctl_tree;
201 };
202 
203 static struct {
204 	char	*name;
205 	int	method;
206 	char	*description;
207 	int	flag_rdonly;
208 } acpi_ibm_sysctls[] = {
209 	{
210 		.name		= "events",
211 		.method		= ACPI_IBM_METHOD_EVENTS,
212 		.description	= "ACPI events enable",
213 	},
214 	{
215 		.name		= "eventmask",
216 		.method		= ACPI_IBM_METHOD_EVENTMASK,
217 		.description	= "ACPI eventmask",
218 	},
219 	{
220 		.name		= "hotkey",
221 		.method		= ACPI_IBM_METHOD_HOTKEY,
222 		.description	= "Key Status",
223 		.flag_rdonly	= 1
224 	},
225 	{
226 		.name		= "lcd_brightness",
227 		.method		= ACPI_IBM_METHOD_BRIGHTNESS,
228 		.description	= "LCD Brightness",
229 	},
230 	{
231 		.name		= "volume",
232 		.method		= ACPI_IBM_METHOD_VOLUME,
233 		.description	= "Volume",
234 	},
235 	{
236 		.name		= "mute",
237 		.method		= ACPI_IBM_METHOD_MUTE,
238 		.description	= "Mute",
239 	},
240 	{
241 		.name		= "thinklight",
242 		.method		= ACPI_IBM_METHOD_THINKLIGHT,
243 		.description	= "Thinklight enable",
244 	},
245 	{
246 		.name		= "bluetooth",
247 		.method		= ACPI_IBM_METHOD_BLUETOOTH,
248 		.description	= "Bluetooth enable",
249 	},
250 	{
251 		.name		= "wlan",
252 		.method		= ACPI_IBM_METHOD_WLAN,
253 		.description	= "WLAN enable",
254 		.flag_rdonly	= 1
255 	},
256 	{
257 		.name		= "fan_speed",
258 		.method		= ACPI_IBM_METHOD_FANSPEED,
259 		.description	= "Fan speed",
260 		.flag_rdonly	= 1
261 	},
262 	{
263 		.name		= "fan_level",
264 		.method		= ACPI_IBM_METHOD_FANLEVEL,
265 		.description	= "Fan level, 0-7 (recommended max), "
266 				  "8 (unthrottled, full-speed)",
267 	},
268 	{
269 		.name		= "fan",
270 		.method		= ACPI_IBM_METHOD_FANSTATUS,
271 		.description	= "Fan enable",
272 	},
273 	{
274 		.name		= "mic_led",
275 		.method		= ACPI_IBM_METHOD_MIC_LED,
276 		.description	= "Mic led",
277 	},
278 	{
279 		.name		= "privacyguard",
280 		.method		= ACPI_IBM_METHOD_PRIVACYGUARD,
281 		.description	= "PrivacyGuard enable",
282 	},
283 	{ NULL, 0, NULL, 0 }
284 };
285 
286 /*
287  * Per-model default list of event mask.
288  */
289 #define	ACPI_IBM_HKEY_RFKILL_MASK		(1 << 4)
290 #define	ACPI_IBM_HKEY_DSWITCH_MASK		(1 << 6)
291 #define	ACPI_IBM_HKEY_BRIGHTNESS_UP_MASK	(1 << 15)
292 #define	ACPI_IBM_HKEY_BRIGHTNESS_DOWN_MASK	(1 << 16)
293 #define	ACPI_IBM_HKEY_SEARCH_MASK		(1 << 18)
294 #define	ACPI_IBM_HKEY_MICMUTE_MASK		(1 << 26)
295 #define	ACPI_IBM_HKEY_SETTINGS_MASK		(1 << 28)
296 #define	ACPI_IBM_HKEY_VIEWOPEN_MASK		(1 << 30)
297 #define	ACPI_IBM_HKEY_VIEWALL_MASK		(1 << 31)
298 
299 struct acpi_ibm_models {
300 	const char *maker;
301 	const char *product;
302 	uint32_t eventmask;
303 } acpi_ibm_models[] = {
304 	{ "LENOVO", "20BSCTO1WW",
305 	  ACPI_IBM_HKEY_RFKILL_MASK |
306 	  ACPI_IBM_HKEY_DSWITCH_MASK |
307 	  ACPI_IBM_HKEY_BRIGHTNESS_UP_MASK |
308 	  ACPI_IBM_HKEY_BRIGHTNESS_DOWN_MASK |
309 	  ACPI_IBM_HKEY_SEARCH_MASK |
310 	  ACPI_IBM_HKEY_MICMUTE_MASK |
311 	  ACPI_IBM_HKEY_SETTINGS_MASK |
312 	  ACPI_IBM_HKEY_VIEWOPEN_MASK |
313 	  ACPI_IBM_HKEY_VIEWALL_MASK
314 	}
315 };
316 
317 ACPI_SERIAL_DECL(ibm, "ThinkPad ACPI Extras");
318 
319 static int	acpi_ibm_probe(device_t dev);
320 static int	acpi_ibm_attach(device_t dev);
321 static int	acpi_ibm_detach(device_t dev);
322 static int	acpi_ibm_resume(device_t dev);
323 
324 static void	ibm_led(void *softc, int onoff);
325 static void	ibm_led_task(struct acpi_ibm_softc *sc, int pending __unused);
326 
327 static int	acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS);
328 static int	acpi_ibm_sysctl_init(struct acpi_ibm_softc *sc, int method);
329 static int	acpi_ibm_sysctl_get(struct acpi_ibm_softc *sc, int method);
330 static int	acpi_ibm_sysctl_set(struct acpi_ibm_softc *sc, int method, int val);
331 
332 static int	acpi_ibm_eventmask_set(struct acpi_ibm_softc *sc, int val);
333 static int	acpi_ibm_thermal_sysctl(SYSCTL_HANDLER_ARGS);
334 static int	acpi_ibm_handlerevents_sysctl(SYSCTL_HANDLER_ARGS);
335 static void	acpi_ibm_notify(ACPI_HANDLE h, UINT32 notify, void *context);
336 
337 static int	acpi_ibm_brightness_set(struct acpi_ibm_softc *sc, int arg);
338 static int	acpi_ibm_bluetooth_set(struct acpi_ibm_softc *sc, int arg);
339 static int	acpi_ibm_thinklight_set(struct acpi_ibm_softc *sc, int arg);
340 static int	acpi_ibm_volume_set(struct acpi_ibm_softc *sc, int arg);
341 static int	acpi_ibm_mute_set(struct acpi_ibm_softc *sc, int arg);
342 static int	acpi_ibm_privacyguard_get(struct acpi_ibm_softc *sc);
343 static ACPI_STATUS	acpi_ibm_privacyguard_set(struct acpi_ibm_softc *sc, int arg);
344 static ACPI_STATUS	acpi_ibm_privacyguard_acpi_call(struct acpi_ibm_softc *sc, bool write, int *arg);
345 
346 static int	acpi_status_to_errno(ACPI_STATUS status);
347 
348 static device_method_t acpi_ibm_methods[] = {
349 	/* Device interface */
350 	DEVMETHOD(device_probe, acpi_ibm_probe),
351 	DEVMETHOD(device_attach, acpi_ibm_attach),
352 	DEVMETHOD(device_detach, acpi_ibm_detach),
353 	DEVMETHOD(device_resume, acpi_ibm_resume),
354 
355 	DEVMETHOD_END
356 };
357 
358 static driver_t	acpi_ibm_driver = {
359 	"acpi_ibm",
360 	acpi_ibm_methods,
361 	sizeof(struct acpi_ibm_softc),
362 };
363 
364 DRIVER_MODULE(acpi_ibm, acpi, acpi_ibm_driver, 0, 0);
365 MODULE_DEPEND(acpi_ibm, acpi, 1, 1, 1);
366 static char    *ibm_ids[] = {"IBM0068", "LEN0068", "LEN0268", NULL};
367 
368 static int
369 acpi_status_to_errno(ACPI_STATUS status)
370 {
371 	switch (status) {
372 	case AE_OK:
373 		return (0);
374 	case AE_BAD_PARAMETER:
375 		return (EINVAL);
376 	default:
377 		return (ENODEV);
378 	}
379 }
380 
381 static void
382 ibm_led(void *softc, int onoff)
383 {
384 	struct acpi_ibm_softc *sc = softc;
385 
386 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
387 
388 	if (sc->led_busy)
389 		return;
390 
391 	sc->led_busy = 1;
392 	sc->led_state = onoff;
393 
394 	AcpiOsExecute(OSL_NOTIFY_HANDLER, (void *)ibm_led_task, sc);
395 }
396 
397 static void
398 ibm_led_task(struct acpi_ibm_softc *sc, int pending __unused)
399 {
400 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
401 
402 	ACPI_SERIAL_BEGIN(ibm);
403 	acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_THINKLIGHT, sc->led_state);
404 	ACPI_SERIAL_END(ibm);
405 
406 	sc->led_busy = 0;
407 }
408 
409 static int
410 acpi_ibm_mic_led_set(struct acpi_ibm_softc *sc, int arg)
411 {
412 	ACPI_OBJECT_LIST input;
413 	ACPI_OBJECT params[1];
414 	ACPI_STATUS status;
415 
416 	if (arg < 0 || arg > 1)
417 		return (EINVAL);
418 
419 	if (sc->mic_led_handle) {
420 		params[0].Type = ACPI_TYPE_INTEGER;
421 		params[0].Integer.Value = 0;
422 		/* mic led: 0 off, 2 on */
423 		if (arg == 1)
424 			params[0].Integer.Value = 2;
425 
426 		input.Pointer = params;
427 		input.Count = 1;
428 
429 		status = AcpiEvaluateObject(sc->handle, "MMTS", &input, NULL);
430 		if (ACPI_SUCCESS(status))
431 			sc->mic_led_state = arg;
432 		return (status);
433 	}
434 
435 	return (0);
436 }
437 
438 static int
439 acpi_ibm_probe(device_t dev)
440 {
441 	int rv;
442 
443 	if (acpi_disabled("ibm") || device_get_unit(dev) != 0)
444 		return (ENXIO);
445 	rv = ACPI_ID_PROBE(device_get_parent(dev), dev, ibm_ids, NULL);
446 
447 	if (rv <= 0)
448 		device_set_desc(dev, "ThinkPad ACPI Extras");
449 
450 	return (rv);
451 }
452 
453 static int
454 acpi_ibm_attach(device_t dev)
455 {
456 	int i;
457 	int hkey;
458 	struct acpi_ibm_softc *sc;
459 	char *maker, *product;
460 	ACPI_OBJECT_LIST input;
461 	ACPI_OBJECT params[1];
462 	ACPI_OBJECT out_obj;
463 	ACPI_BUFFER result;
464 	devclass_t ec_devclass;
465 
466 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
467 
468 	sc = device_get_softc(dev);
469 	sc->dev = dev;
470 	sc->handle = acpi_get_handle(dev);
471 
472 	/* Look for the first embedded controller */
473 	if (!(ec_devclass = devclass_find ("acpi_ec"))) {
474 		if (bootverbose)
475 			device_printf(dev, "Couldn't find acpi_ec devclass\n");
476 		return (EINVAL);
477 	}
478 	if (!(sc->ec_dev = devclass_get_device(ec_devclass, 0))) {
479 		if (bootverbose)
480 			device_printf(dev, "Couldn't find acpi_ec device\n");
481 		return (EINVAL);
482 	}
483 	sc->ec_handle = acpi_get_handle(sc->ec_dev);
484 
485 	/* Get the sysctl tree */
486 	sc->sysctl_ctx = device_get_sysctl_ctx(dev);
487 	sc->sysctl_tree = device_get_sysctl_tree(dev);
488 
489 	/* Look for event mask and hook up the nodes */
490 	sc->events_mask_supported = ACPI_SUCCESS(acpi_GetInteger(sc->handle,
491 	    IBM_NAME_EVENTS_MASK_GET, &sc->events_initialmask));
492 
493 	if (sc->events_mask_supported) {
494 		SYSCTL_ADD_UINT(sc->sysctl_ctx,
495 		    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "initialmask",
496 		    CTLFLAG_RD, &sc->events_initialmask, 0,
497 		    "Initial eventmask");
498 
499 		if (ACPI_SUCCESS (acpi_GetInteger(sc->handle, "MHKV", &hkey))) {
500 			device_printf(dev, "Firmware version is 0x%X\n", hkey);
501 			switch (hkey >> 8) {
502 			case 1:
503 				/* The availmask is the bitmask of supported events */
504 				if (ACPI_FAILURE(acpi_GetInteger(sc->handle,
505 				    IBM_NAME_EVENTS_AVAILMASK, &sc->events_availmask)))
506 					sc->events_availmask = 0xffffffff;
507 				break;
508 
509 			case 2:
510 				result.Length = sizeof(out_obj);
511 				result.Pointer = &out_obj;
512 				params[0].Type = ACPI_TYPE_INTEGER;
513 				params[0].Integer.Value = 1;
514 				input.Pointer = params;
515 				input.Count = 1;
516 
517 				sc->events_availmask = 0xffffffff;
518 
519 				if (ACPI_SUCCESS(AcpiEvaluateObject (sc->handle,
520 				    IBM_NAME_EVENTS_AVAILMASK, &input, &result)))
521 					sc->events_availmask = out_obj.Integer.Value;
522 				break;
523 			default:
524 				device_printf(dev, "Unknown firmware version 0x%x\n", hkey);
525 				break;
526 			}
527 		} else
528 			sc->events_availmask = 0xffffffff;
529 
530 		SYSCTL_ADD_UINT(sc->sysctl_ctx,
531 				SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
532 				"availmask", CTLFLAG_RD,
533 				&sc->events_availmask, 0, "Mask of supported events");
534 	}
535 
536 	/* Hook up proc nodes */
537 	for (int i = 0; acpi_ibm_sysctls[i].name != NULL; i++) {
538 		if (!acpi_ibm_sysctl_init(sc, acpi_ibm_sysctls[i].method))
539 			continue;
540 
541 		if (acpi_ibm_sysctls[i].flag_rdonly != 0) {
542 			SYSCTL_ADD_PROC(sc->sysctl_ctx,
543 			    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
544 			    acpi_ibm_sysctls[i].name,
545 			    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
546 			    sc, i, acpi_ibm_sysctl, "I",
547 			    acpi_ibm_sysctls[i].description);
548 		} else {
549 			SYSCTL_ADD_PROC(sc->sysctl_ctx,
550 			    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
551 			    acpi_ibm_sysctls[i].name,
552 			    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
553 			    sc, i, acpi_ibm_sysctl, "I",
554 			    acpi_ibm_sysctls[i].description);
555 		}
556 	}
557 
558 	/* Hook up thermal node */
559 	if (acpi_ibm_sysctl_init(sc, ACPI_IBM_METHOD_THERMAL)) {
560 		SYSCTL_ADD_PROC(sc->sysctl_ctx,
561 		    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "thermal",
562 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
563 		    acpi_ibm_thermal_sysctl, "I", "Thermal zones");
564 	}
565 
566 	/* Hook up handlerevents node */
567 	if (acpi_ibm_sysctl_init(sc, ACPI_IBM_METHOD_HANDLEREVENTS)) {
568 		SYSCTL_ADD_PROC(sc->sysctl_ctx,
569 		    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "handlerevents",
570 		    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
571 		    acpi_ibm_handlerevents_sysctl, "I",
572 		    "devd(8) events handled by acpi_ibm");
573 	}
574 
575 	/* Handle notifies */
576 	AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
577 	    acpi_ibm_notify, dev);
578 
579 	/* Hook up light to led(4) */
580 	if (sc->light_set_supported)
581 		sc->led_dev = led_create_state(ibm_led, sc, "thinklight",
582 		    (sc->light_val ? 1 : 0));
583 
584 	/* Enable per-model events. */
585 	maker = kern_getenv("smbios.system.maker");
586 	product = kern_getenv("smbios.system.product");
587 	if (maker == NULL || product == NULL)
588 		goto nosmbios;
589 
590 	for (i = 0; i < nitems(acpi_ibm_models); i++) {
591 		if (strcmp(maker, acpi_ibm_models[i].maker) == 0 &&
592 		    strcmp(product, acpi_ibm_models[i].product) == 0) {
593 			ACPI_SERIAL_BEGIN(ibm);
594 			acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTMASK,
595 			    acpi_ibm_models[i].eventmask);
596 			ACPI_SERIAL_END(ibm);
597 		}
598 	}
599 
600 nosmbios:
601 	freeenv(maker);
602 	freeenv(product);
603 
604 	/* Enable events by default. */
605 	ACPI_SERIAL_BEGIN(ibm);
606 	acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTS, 1);
607 	ACPI_SERIAL_END(ibm);
608 
609 	return (0);
610 }
611 
612 static int
613 acpi_ibm_detach(device_t dev)
614 {
615 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
616 
617 	struct acpi_ibm_softc *sc = device_get_softc(dev);
618 
619 	/* Disable events and restore eventmask */
620 	ACPI_SERIAL_BEGIN(ibm);
621 	acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTS, 0);
622 	acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTMASK, sc->events_initialmask);
623 	ACPI_SERIAL_END(ibm);
624 
625 	AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY, acpi_ibm_notify);
626 
627 	if (sc->led_dev != NULL)
628 		led_destroy(sc->led_dev);
629 
630 	return (0);
631 }
632 
633 static int
634 acpi_ibm_resume(device_t dev)
635 {
636 	struct acpi_ibm_softc *sc = device_get_softc(dev);
637 
638 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
639 
640 	ACPI_SERIAL_BEGIN(ibm);
641 	for (int i = 0; acpi_ibm_sysctls[i].name != NULL; i++) {
642 		int val;
643 
644 		val = acpi_ibm_sysctl_get(sc, i);
645 
646 		if (acpi_ibm_sysctls[i].flag_rdonly != 0)
647 			continue;
648 
649 		acpi_ibm_sysctl_set(sc, i, val);
650 	}
651 	ACPI_SERIAL_END(ibm);
652 
653 	/* The mic led does not turn back on when sysctl_set is called in the above loop */
654 	acpi_ibm_mic_led_set(sc, sc->mic_led_state);
655 
656 	return (0);
657 }
658 
659 static int
660 acpi_ibm_eventmask_set(struct acpi_ibm_softc *sc, int val)
661 {
662 	ACPI_OBJECT		arg[2];
663 	ACPI_OBJECT_LIST	args;
664 	ACPI_STATUS		status;
665 
666 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
667 	ACPI_SERIAL_ASSERT(ibm);
668 
669 	args.Count = 2;
670 	args.Pointer = arg;
671 	arg[0].Type = ACPI_TYPE_INTEGER;
672 	arg[1].Type = ACPI_TYPE_INTEGER;
673 
674 	for (int i = 0; i < 32; ++i) {
675 		arg[0].Integer.Value = i + 1;
676 		arg[1].Integer.Value = (((1 << i) & val) != 0);
677 		status = AcpiEvaluateObject(sc->handle,
678 		    IBM_NAME_EVENTS_MASK_SET, &args, NULL);
679 
680 		if (ACPI_FAILURE(status))
681 			return (status);
682 	}
683 
684 	return (0);
685 }
686 
687 static int
688 acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS)
689 {
690 	struct acpi_ibm_softc	*sc;
691 	int			arg;
692 	int			error = 0;
693 	int			function;
694 	int			method;
695 
696 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
697 
698 	sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
699 	function = oidp->oid_arg2;
700 	method = acpi_ibm_sysctls[function].method;
701 
702 	ACPI_SERIAL_BEGIN(ibm);
703 	arg = acpi_ibm_sysctl_get(sc, method);
704 	error = sysctl_handle_int(oidp, &arg, 0, req);
705 
706 	/* Sanity check */
707 	if (error != 0 || req->newptr == NULL)
708 		goto out;
709 
710 	/* Update */
711 	error = acpi_ibm_sysctl_set(sc, method, arg);
712 
713 out:
714 	ACPI_SERIAL_END(ibm);
715 	return (error);
716 }
717 
718 static int
719 acpi_ibm_sysctl_get(struct acpi_ibm_softc *sc, int method)
720 {
721 	UINT64		val_ec;
722 	int 		val = 0, key;
723 
724 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
725 	ACPI_SERIAL_ASSERT(ibm);
726 
727 	switch (method) {
728 	case ACPI_IBM_METHOD_EVENTS:
729 		acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_GET, &val);
730 		break;
731 
732 	case ACPI_IBM_METHOD_EVENTMASK:
733 		if (sc->events_mask_supported)
734 			acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_MASK_GET, &val);
735 		break;
736 
737 	case ACPI_IBM_METHOD_HOTKEY:
738 		/*
739 		 * Construct the hotkey as a bitmask as illustrated below.
740 		 * Note that whenever a key was pressed, the respecting bit
741 		 * toggles and nothing else changes.
742 		 * +--+--+-+-+-+-+-+-+-+-+-+-+
743 		 * |11|10|9|8|7|6|5|4|3|2|1|0|
744 		 * +--+--+-+-+-+-+-+-+-+-+-+-+
745 		 *   |  | | | | | | | | | | |
746 		 *   |  | | | | | | | | | | +- Home Button
747 		 *   |  | | | | | | | | | +--- Search Button
748 		 *   |  | | | | | | | | +----- Mail Button
749 		 *   |  | | | | | | | +------- Thinkpad Button
750 		 *   |  | | | | | | +--------- Zoom (Fn + Space)
751 		 *   |  | | | | | +----------- WLAN Button
752 		 *   |  | | | | +------------- Video Button
753 		 *   |  | | | +--------------- Hibernate Button
754 		 *   |  | | +----------------- Thinklight Button
755 		 *   |  | +------------------- Screen expand (Fn + F8)
756 		 *   |  +--------------------- Brightness
757 		 *   +------------------------ Volume/Mute
758 		 */
759 		key = rtcin(IBM_RTC_HOTKEY1);
760 		val = (IBM_RTC_MASK_HOME | IBM_RTC_MASK_SEARCH | IBM_RTC_MASK_MAIL | IBM_RTC_MASK_WLAN) & key;
761 		key = rtcin(IBM_RTC_HOTKEY2);
762 		val |= (IBM_RTC_MASK_THINKPAD | IBM_RTC_MASK_VIDEO | IBM_RTC_MASK_HIBERNATE) & key;
763 		val |= (IBM_RTC_MASK_ZOOM & key) >> 1;
764 		key = rtcin(IBM_RTC_THINKLIGHT);
765 		val |= (IBM_RTC_MASK_THINKLIGHT & key) << 4;
766 		key = rtcin(IBM_RTC_SCREENEXPAND);
767 		val |= (IBM_RTC_MASK_THINKLIGHT & key) << 4;
768 		key = rtcin(IBM_RTC_BRIGHTNESS);
769 		val |= (IBM_RTC_MASK_BRIGHTNESS & key) << 5;
770 		key = rtcin(IBM_RTC_VOLUME);
771 		val |= (IBM_RTC_MASK_VOLUME & key) << 4;
772 		break;
773 
774 	case ACPI_IBM_METHOD_BRIGHTNESS:
775 		ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS, &val_ec, 1);
776 		val = val_ec & IBM_EC_MASK_BRI;
777 		break;
778 
779 	case ACPI_IBM_METHOD_VOLUME:
780 		ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
781 		val = val_ec & IBM_EC_MASK_VOL;
782 		break;
783 
784 	case ACPI_IBM_METHOD_MUTE:
785 		ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
786 		val = ((val_ec & IBM_EC_MASK_MUTE) == IBM_EC_MASK_MUTE);
787 		break;
788 
789 	case ACPI_IBM_METHOD_THINKLIGHT:
790 		if (sc->light_get_supported)
791 			acpi_GetInteger(sc->ec_handle, IBM_NAME_KEYLIGHT, &val);
792 		else
793 			val = sc->light_val;
794 		break;
795 
796 	case ACPI_IBM_METHOD_BLUETOOTH:
797 		acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &val);
798 		sc->wlan_bt_flags = val;
799 		val = ((val & IBM_NAME_MASK_BT) != 0);
800 		break;
801 
802 	case ACPI_IBM_METHOD_WLAN:
803 		acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &val);
804 		sc->wlan_bt_flags = val;
805 		val = ((val & IBM_NAME_MASK_WLAN) != 0);
806 		break;
807 
808 	case ACPI_IBM_METHOD_FANSPEED:
809 		if (sc->fan_handle) {
810 			if(ACPI_FAILURE(acpi_GetInteger(sc->fan_handle, NULL, &val)))
811 				val = -1;
812 		} else {
813 			ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSPEED, &val_ec, 2);
814 			val = val_ec;
815 		}
816 		break;
817 
818 	case ACPI_IBM_METHOD_FANLEVEL:
819 		/*
820 		 * The IBM_EC_FANSTATUS register works as follows:
821 		 * Bit 0-5 indicate the level at which the fan operates. Only
822 		 *       values between 0 and 7 have an effect. Everything
823 		 *       above 7 is treated the same as level 7
824 		 * Bit 6 overrides the fan speed limit if set to 1
825 		 * Bit 7 indicates at which mode the fan operates:
826 		 *       manual (0) or automatic (1)
827 		 */
828 		if (!sc->fan_handle) {
829 			ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
830 			if (val_ec & IBM_EC_MASK_FANUNTHROTTLED)
831 				val = 8;
832 			else
833 				val = val_ec & IBM_EC_MASK_FANLEVEL;
834 		}
835 		break;
836 
837 	case ACPI_IBM_METHOD_FANSTATUS:
838 		if (!sc->fan_handle) {
839 			ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
840 			val = (val_ec & IBM_EC_MASK_FANSTATUS) == IBM_EC_MASK_FANSTATUS;
841 		} else
842 			val = -1;
843 		break;
844 
845 	case ACPI_IBM_METHOD_MIC_LED:
846 		if (sc->mic_led_handle)
847 			return sc->mic_led_state;
848 		else
849 			val = -1;
850 		break;
851 
852 	case ACPI_IBM_METHOD_PRIVACYGUARD:
853 		val = acpi_ibm_privacyguard_get(sc);
854 		break;
855 	}
856 
857 	return (val);
858 }
859 
860 static int
861 acpi_ibm_sysctl_set(struct acpi_ibm_softc *sc, int method, int arg)
862 {
863 	int			val;
864 	UINT64			val_ec;
865 	ACPI_STATUS		status;
866 
867 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
868 	ACPI_SERIAL_ASSERT(ibm);
869 
870 	switch (method) {
871 	case ACPI_IBM_METHOD_EVENTS:
872 		if (arg < 0 || arg > 1)
873 			return (EINVAL);
874 
875 		status = acpi_SetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_SET, arg);
876 		if (ACPI_FAILURE(status))
877 			return (status);
878 		if (sc->events_mask_supported)
879 			return acpi_ibm_eventmask_set(sc, sc->events_availmask);
880 		break;
881 
882 	case ACPI_IBM_METHOD_EVENTMASK:
883 		if (sc->events_mask_supported)
884 			return acpi_ibm_eventmask_set(sc, arg);
885 		break;
886 
887 	case ACPI_IBM_METHOD_BRIGHTNESS:
888 		return acpi_ibm_brightness_set(sc, arg);
889 		break;
890 
891 	case ACPI_IBM_METHOD_VOLUME:
892 		return acpi_ibm_volume_set(sc, arg);
893 		break;
894 
895 	case ACPI_IBM_METHOD_MUTE:
896 		return acpi_ibm_mute_set(sc, arg);
897 		break;
898 
899 	case ACPI_IBM_METHOD_MIC_LED:
900 		return acpi_ibm_mic_led_set(sc, arg);
901 		break;
902 
903 	case ACPI_IBM_METHOD_THINKLIGHT:
904 		return acpi_ibm_thinklight_set(sc, arg);
905 		break;
906 
907 	case ACPI_IBM_METHOD_BLUETOOTH:
908 		return acpi_ibm_bluetooth_set(sc, arg);
909 		break;
910 
911 	case ACPI_IBM_METHOD_PRIVACYGUARD:
912 		return (acpi_status_to_errno(acpi_ibm_privacyguard_set(sc, arg)));
913 		break;
914 
915 	case ACPI_IBM_METHOD_FANLEVEL:
916 		if (arg < 0 || arg > 8)
917 			return (EINVAL);
918 
919 		if (!sc->fan_handle) {
920 			/* Read the current fan status. */
921 			ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
922 			val = val_ec & ~(IBM_EC_MASK_FANLEVEL |
923 			    IBM_EC_MASK_FANUNTHROTTLED);
924 
925 			if (arg == 8)
926 				/* Full speed, set the unthrottled bit. */
927 				val |= 7 | IBM_EC_MASK_FANUNTHROTTLED;
928 			else
929 				val |= arg;
930 
931 			return (ACPI_EC_WRITE(sc->ec_dev, IBM_EC_FANSTATUS, val,
932 			    1));
933 		}
934 		break;
935 
936 	case ACPI_IBM_METHOD_FANSTATUS:
937 		if (arg < 0 || arg > 1)
938 			return (EINVAL);
939 
940 		if (!sc->fan_handle) {
941 			/* Read the current fanstatus */
942 			ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
943 
944 			return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_FANSTATUS,
945 				(arg == 1) ? (val_ec | IBM_EC_MASK_FANSTATUS) : (val_ec & (~IBM_EC_MASK_FANSTATUS)), 1);
946 		}
947 		break;
948 	}
949 
950 	return (0);
951 }
952 
953 static int
954 acpi_ibm_sysctl_init(struct acpi_ibm_softc *sc, int method)
955 {
956 	int 			dummy;
957 	ACPI_OBJECT_TYPE 	cmos_t;
958 	ACPI_HANDLE		ledb_handle;
959 
960 	switch (method) {
961 	case ACPI_IBM_METHOD_EVENTS:
962 		return (TRUE);
963 
964 	case ACPI_IBM_METHOD_EVENTMASK:
965 		return (sc->events_mask_supported);
966 
967 	case ACPI_IBM_METHOD_HOTKEY:
968 	case ACPI_IBM_METHOD_BRIGHTNESS:
969 	case ACPI_IBM_METHOD_VOLUME:
970 	case ACPI_IBM_METHOD_MUTE:
971 		/* EC is required here, which was already checked before */
972 		return (TRUE);
973 
974 	case ACPI_IBM_METHOD_MIC_LED:
975 		if (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "MMTS", &sc->mic_led_handle)))
976 		{
977 			/* Turn off mic led by default */
978 			acpi_ibm_mic_led_set(sc, 0);
979 			return (TRUE);
980 		} else
981 			sc->mic_led_handle = NULL;
982 		return (FALSE);
983 
984 	case ACPI_IBM_METHOD_THINKLIGHT:
985 		sc->cmos_handle = NULL;
986 		sc->light_get_supported = ACPI_SUCCESS(acpi_GetInteger(
987 		    sc->ec_handle, IBM_NAME_KEYLIGHT, &sc->light_val));
988 
989 		if ((ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\UCMS", &sc->light_handle)) ||
990 		     ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\CMOS", &sc->light_handle)) ||
991 		     ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\CMS", &sc->light_handle))) &&
992 		     ACPI_SUCCESS(AcpiGetType(sc->light_handle, &cmos_t)) &&
993 		     cmos_t == ACPI_TYPE_METHOD) {
994 			sc->light_cmd_on = 0x0c;
995 			sc->light_cmd_off = 0x0d;
996 			sc->cmos_handle = sc->light_handle;
997 		}
998 		else if (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\LGHT", &sc->light_handle))) {
999 			sc->light_cmd_on = 1;
1000 			sc->light_cmd_off = 0;
1001 		} else
1002 			sc->light_handle = NULL;
1003 
1004 		sc->light_set_supported = (sc->light_handle &&
1005 		    ACPI_FAILURE(AcpiGetHandle(sc->ec_handle, "LEDB", &ledb_handle)));
1006 
1007 		if (sc->light_get_supported)
1008 			return (TRUE);
1009 
1010 		if (sc->light_set_supported) {
1011 			sc->light_val = 0;
1012 			return (TRUE);
1013 		}
1014 
1015 		return (FALSE);
1016 
1017 	case ACPI_IBM_METHOD_BLUETOOTH:
1018 	case ACPI_IBM_METHOD_WLAN:
1019 		if (ACPI_SUCCESS(acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &dummy)))
1020 			return (TRUE);
1021 		return (FALSE);
1022 
1023 	case ACPI_IBM_METHOD_FANSPEED:
1024 		/*
1025 		 * Some models report the fan speed in levels from 0-7
1026 		 * Newer models report it contiguously
1027 		 */
1028 		sc->fan_levels =
1029 		    (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "GFAN", &sc->fan_handle)) ||
1030 		     ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\FSPD", &sc->fan_handle)));
1031 		return (TRUE);
1032 
1033 	case ACPI_IBM_METHOD_FANLEVEL:
1034 	case ACPI_IBM_METHOD_FANSTATUS:
1035 		/*
1036 		 * Fan status is only supported on those models,
1037 		 * which report fan RPM contiguously, not in levels
1038 		 */
1039 		if (sc->fan_levels)
1040 			return (FALSE);
1041 		return (TRUE);
1042 
1043 	case ACPI_IBM_METHOD_THERMAL:
1044 		if (ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle, IBM_NAME_THERMAL_GET, &dummy))) {
1045 			sc->thermal_updt_supported = ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle, IBM_NAME_THERMAL_UPDT, &dummy));
1046 			return (TRUE);
1047 		}
1048 		return (FALSE);
1049 
1050 	case ACPI_IBM_METHOD_HANDLEREVENTS:
1051 		return (TRUE);
1052 
1053 	case ACPI_IBM_METHOD_PRIVACYGUARD:
1054 		return (acpi_ibm_privacyguard_get(sc) != -1);
1055 	}
1056 	return (FALSE);
1057 }
1058 
1059 static int
1060 acpi_ibm_thermal_sysctl(SYSCTL_HANDLER_ARGS)
1061 {
1062 	struct acpi_ibm_softc	*sc;
1063 	int			error = 0;
1064 	char			temp_cmd[] = "TMP0";
1065 	int			temp[8];
1066 
1067 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1068 
1069 	sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
1070 
1071 	ACPI_SERIAL_BEGIN(ibm);
1072 
1073 	for (int i = 0; i < 8; ++i) {
1074 		temp_cmd[3] = '0' + i;
1075 
1076 		/*
1077 		 * The TMPx methods seem to return +/- 128 or 0
1078 		 * when the respecting sensor is not available
1079 		 */
1080 		if (ACPI_FAILURE(acpi_GetInteger(sc->ec_handle, temp_cmd,
1081 		    &temp[i])) || ABS(temp[i]) == 128 || temp[i] == 0)
1082 			temp[i] = -1;
1083 		else if (sc->thermal_updt_supported)
1084 			/* Temperature is reported in tenth of Kelvin */
1085 			temp[i] = (temp[i] - 2731 + 5) / 10;
1086 	}
1087 
1088 	error = sysctl_handle_opaque(oidp, &temp, 8*sizeof(int), req);
1089 
1090 	ACPI_SERIAL_END(ibm);
1091 	return (error);
1092 }
1093 
1094 static int
1095 acpi_ibm_handlerevents_sysctl(SYSCTL_HANDLER_ARGS)
1096 {
1097 	struct acpi_ibm_softc	*sc;
1098 	int			error = 0;
1099 	struct sbuf		sb;
1100 	char			*cp, *ep;
1101 	int			l, val;
1102 	unsigned int		handler_events;
1103 	char			temp[128];
1104 
1105 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1106 
1107 	sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
1108 
1109 	if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
1110 		return (ENOMEM);
1111 
1112 	ACPI_SERIAL_BEGIN(ibm);
1113 
1114 	/* Get old values if this is a get request. */
1115 	if (req->newptr == NULL) {
1116 		for (int i = 0; i < 8 * sizeof(sc->handler_events); i++)
1117 			if (sc->handler_events & (1 << i))
1118 				sbuf_printf(&sb, "0x%02x ", i + 1);
1119 		if (sbuf_len(&sb) == 0)
1120 			sbuf_printf(&sb, "NONE");
1121 	}
1122 
1123 	sbuf_trim(&sb);
1124 	sbuf_finish(&sb);
1125 	strlcpy(temp, sbuf_data(&sb), sizeof(temp));
1126 	sbuf_delete(&sb);
1127 
1128 	error = sysctl_handle_string(oidp, temp, sizeof(temp), req);
1129 
1130 	/* Check for error or no change */
1131 	if (error != 0 || req->newptr == NULL)
1132 		goto out;
1133 
1134 	/* If the user is setting a string, parse it. */
1135 	handler_events = 0;
1136 	cp = temp;
1137 	while (*cp) {
1138 		if (isspace(*cp)) {
1139 			cp++;
1140 			continue;
1141 		}
1142 
1143 		ep = cp;
1144 
1145 		while (*ep && !isspace(*ep))
1146 			ep++;
1147 
1148 		l = ep - cp;
1149 		if (l == 0)
1150 			break;
1151 
1152 		if (strncmp(cp, "NONE", 4) == 0) {
1153 			cp = ep;
1154 			continue;
1155 		}
1156 
1157 		if (l >= 3 && cp[0] == '0' && (cp[1] == 'X' || cp[1] == 'x'))
1158 			val = strtoul(cp, &ep, 16);
1159 		else
1160 			val = strtoul(cp, &ep, 10);
1161 
1162 		if (val == 0 || ep == cp || val >= 8 * sizeof(handler_events)) {
1163 			cp[l] = '\0';
1164 			device_printf(sc->dev, "invalid event code: %s\n", cp);
1165 			error = EINVAL;
1166 			goto out;
1167 		}
1168 
1169 		handler_events |= 1 << (val - 1);
1170 
1171 		cp = ep;
1172 	}
1173 
1174 	sc->handler_events = handler_events;
1175 out:
1176 	ACPI_SERIAL_END(ibm);
1177 	return (error);
1178 }
1179 
1180 static int
1181 acpi_ibm_brightness_set(struct acpi_ibm_softc *sc, int arg)
1182 {
1183 	int			val, step;
1184 	UINT64			val_ec;
1185 	ACPI_OBJECT		Arg;
1186 	ACPI_OBJECT_LIST	Args;
1187 	ACPI_STATUS		status;
1188 
1189 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1190 	ACPI_SERIAL_ASSERT(ibm);
1191 
1192 	if (arg < 0 || arg > 7)
1193 		return (EINVAL);
1194 
1195 	/* Read the current brightness */
1196 	status = ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS, &val_ec, 1);
1197 	if (ACPI_FAILURE(status))
1198 		return (status);
1199 
1200 	if (sc->cmos_handle) {
1201 		val = val_ec & IBM_EC_MASK_BRI;
1202 
1203 		Args.Count = 1;
1204 		Args.Pointer = &Arg;
1205 		Arg.Type = ACPI_TYPE_INTEGER;
1206 		Arg.Integer.Value = (arg > val) ? IBM_CMOS_BRIGHTNESS_UP :
1207 						  IBM_CMOS_BRIGHTNESS_DOWN;
1208 
1209 		step = (arg > val) ? 1 : -1;
1210 		for (int i = val; i != arg; i += step) {
1211 			status = AcpiEvaluateObject(sc->cmos_handle, NULL,
1212 						    &Args, NULL);
1213 			if (ACPI_FAILURE(status)) {
1214 				/* Record the last value */
1215 				if (i != val) {
1216 					ACPI_EC_WRITE(sc->ec_dev,
1217 					    IBM_EC_BRIGHTNESS, i - step, 1);
1218 				}
1219 				return (status);
1220 			}
1221 		}
1222 	}
1223 
1224 	return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_BRIGHTNESS, arg, 1);
1225 }
1226 
1227 static int
1228 acpi_ibm_bluetooth_set(struct acpi_ibm_softc *sc, int arg)
1229 {
1230 	int			val;
1231 
1232 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1233 	ACPI_SERIAL_ASSERT(ibm);
1234 
1235 	if (arg < 0 || arg > 1)
1236 		return (EINVAL);
1237 
1238 	val = (arg == 1) ? sc->wlan_bt_flags | IBM_NAME_MASK_BT :
1239 			   sc->wlan_bt_flags & (~IBM_NAME_MASK_BT);
1240 	return acpi_SetInteger(sc->handle, IBM_NAME_WLAN_BT_SET, val);
1241 }
1242 
1243 static int
1244 acpi_ibm_thinklight_set(struct acpi_ibm_softc *sc, int arg)
1245 {
1246 	ACPI_OBJECT		Arg;
1247 	ACPI_OBJECT_LIST	Args;
1248 	ACPI_STATUS		status;
1249 
1250 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1251 	ACPI_SERIAL_ASSERT(ibm);
1252 
1253 	if (arg < 0 || arg > 1)
1254 		return (EINVAL);
1255 
1256 	if (sc->light_set_supported) {
1257 		Args.Count = 1;
1258 		Args.Pointer = &Arg;
1259 		Arg.Type = ACPI_TYPE_INTEGER;
1260 		Arg.Integer.Value = arg ? sc->light_cmd_on : sc->light_cmd_off;
1261 
1262 		status = AcpiEvaluateObject(sc->light_handle, NULL,
1263 					    &Args, NULL);
1264 		if (ACPI_SUCCESS(status))
1265 			sc->light_val = arg;
1266 		return (status);
1267 	}
1268 
1269 	return (0);
1270 }
1271 
1272 /*
1273  * Helper function to make a get or set ACPI call to the PrivacyGuard handle.
1274  * Only meant to be used internally by the get/set functions below.
1275  */
1276 static ACPI_STATUS
1277 acpi_ibm_privacyguard_acpi_call(struct acpi_ibm_softc *sc, bool write, int *arg)
1278 {
1279 	ACPI_OBJECT		Arg;
1280 	ACPI_OBJECT_LIST	Args;
1281 	ACPI_STATUS		status;
1282 	ACPI_OBJECT		out_obj;
1283 	ACPI_BUFFER		result;
1284 
1285 	Arg.Type = ACPI_TYPE_INTEGER;
1286 	Arg.Integer.Value = (write ? *arg : 0);
1287 	Args.Count = 1;
1288 	Args.Pointer = &Arg;
1289 	result.Length = sizeof(out_obj);
1290 	result.Pointer = &out_obj;
1291 
1292 	status = AcpiEvaluateObject(sc->handle,
1293 	    (write ? IBM_NAME_PRIVACYGUARD_SET : IBM_NAME_PRIVACYGUARD_GET),
1294 	    &Args, &result);
1295 	if (ACPI_SUCCESS(status) && !write)
1296 		*arg = out_obj.Integer.Value;
1297 
1298 	return (status);
1299 }
1300 
1301 /*
1302  * Returns -1 if the device is not present.
1303  */
1304 static int
1305 acpi_ibm_privacyguard_get(struct acpi_ibm_softc *sc)
1306 {
1307 	ACPI_STATUS status;
1308 	int val;
1309 
1310 	status = acpi_ibm_privacyguard_acpi_call(sc, false, &val);
1311 	if (ACPI_SUCCESS(status) &&
1312 	    (val & IBM_FLAG_PRIVACYGUARD_DEVICE_PRESENT))
1313 		return (val & IBM_FLAG_PRIVACYGUARD_ON);
1314 
1315 	return (-1);
1316 }
1317 
1318 static ACPI_STATUS
1319 acpi_ibm_privacyguard_set(struct acpi_ibm_softc *sc, int arg)
1320 {
1321 	if (arg < 0 || arg > 1)
1322 		return (AE_BAD_PARAMETER);
1323 
1324 	return (acpi_ibm_privacyguard_acpi_call(sc, true, &arg));
1325 }
1326 
1327 static int
1328 acpi_ibm_volume_set(struct acpi_ibm_softc *sc, int arg)
1329 {
1330 	int			val, step;
1331 	UINT64			val_ec;
1332 	ACPI_OBJECT		Arg;
1333 	ACPI_OBJECT_LIST	Args;
1334 	ACPI_STATUS		status;
1335 
1336 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1337 	ACPI_SERIAL_ASSERT(ibm);
1338 
1339 	if (arg < 0 || arg > 14)
1340 		return (EINVAL);
1341 
1342 	/* Read the current volume */
1343 	status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
1344 	if (ACPI_FAILURE(status))
1345 		return (status);
1346 
1347 	if (sc->cmos_handle) {
1348 		val = val_ec & IBM_EC_MASK_VOL;
1349 
1350 		Args.Count = 1;
1351 		Args.Pointer = &Arg;
1352 		Arg.Type = ACPI_TYPE_INTEGER;
1353 		Arg.Integer.Value = (arg > val) ? IBM_CMOS_VOLUME_UP :
1354 						  IBM_CMOS_VOLUME_DOWN;
1355 
1356 		step = (arg > val) ? 1 : -1;
1357 		for (int i = val; i != arg; i += step) {
1358 			status = AcpiEvaluateObject(sc->cmos_handle, NULL,
1359 						    &Args, NULL);
1360 			if (ACPI_FAILURE(status)) {
1361 				/* Record the last value */
1362 				if (i != val) {
1363 					val_ec = i - step +
1364 						 (val_ec & (~IBM_EC_MASK_VOL));
1365 					ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME,
1366 						      val_ec, 1);
1367 				}
1368 				return (status);
1369 			}
1370 		}
1371 	}
1372 
1373 	val_ec = arg + (val_ec & (~IBM_EC_MASK_VOL));
1374 	return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME, val_ec, 1);
1375 }
1376 
1377 static int
1378 acpi_ibm_mute_set(struct acpi_ibm_softc *sc, int arg)
1379 {
1380 	UINT64			val_ec;
1381 	ACPI_OBJECT		Arg;
1382 	ACPI_OBJECT_LIST	Args;
1383 	ACPI_STATUS		status;
1384 
1385 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1386 	ACPI_SERIAL_ASSERT(ibm);
1387 
1388 	if (arg < 0 || arg > 1)
1389 		return (EINVAL);
1390 
1391 	status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
1392 	if (ACPI_FAILURE(status))
1393 		return (status);
1394 
1395 	if (sc->cmos_handle) {
1396 		Args.Count = 1;
1397 		Args.Pointer = &Arg;
1398 		Arg.Type = ACPI_TYPE_INTEGER;
1399 		Arg.Integer.Value = IBM_CMOS_VOLUME_MUTE;
1400 
1401 		status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
1402 		if (ACPI_FAILURE(status))
1403 			return (status);
1404 	}
1405 
1406 	val_ec = (arg == 1) ? val_ec | IBM_EC_MASK_MUTE :
1407 			      val_ec & (~IBM_EC_MASK_MUTE);
1408 	return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME, val_ec, 1);
1409 }
1410 
1411 static void
1412 acpi_ibm_eventhandler(struct acpi_ibm_softc *sc, int arg)
1413 {
1414 	int			val;
1415 	UINT64			val_ec;
1416 	ACPI_STATUS		status;
1417 
1418 	ACPI_SERIAL_BEGIN(ibm);
1419 	switch (arg) {
1420 	case IBM_EVENT_SUSPEND_TO_RAM:
1421 		power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
1422 		break;
1423 
1424 	case IBM_EVENT_BLUETOOTH:
1425 		acpi_ibm_bluetooth_set(sc, (sc->wlan_bt_flags == 0));
1426 		break;
1427 
1428 	case IBM_EVENT_BRIGHTNESS_UP:
1429 	case IBM_EVENT_BRIGHTNESS_DOWN:
1430 		/* Read the current brightness */
1431 		status = ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS,
1432 				      &val_ec, 1);
1433 		if (ACPI_FAILURE(status))
1434 			return;
1435 
1436 		val = val_ec & IBM_EC_MASK_BRI;
1437 		val = (arg == IBM_EVENT_BRIGHTNESS_UP) ? val + 1 : val - 1;
1438 		acpi_ibm_brightness_set(sc, val);
1439 		break;
1440 
1441 	case IBM_EVENT_THINKLIGHT:
1442 		acpi_ibm_thinklight_set(sc, (sc->light_val == 0));
1443 		break;
1444 
1445 	case IBM_EVENT_VOLUME_UP:
1446 	case IBM_EVENT_VOLUME_DOWN:
1447 		/* Read the current volume */
1448 		status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
1449 		if (ACPI_FAILURE(status))
1450 			return;
1451 
1452 		val = val_ec & IBM_EC_MASK_VOL;
1453 		val = (arg == IBM_EVENT_VOLUME_UP) ? val + 1 : val - 1;
1454 		acpi_ibm_volume_set(sc, val);
1455 		break;
1456 
1457 	case IBM_EVENT_MUTE:
1458 		/* Read the current value */
1459 		status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
1460 		if (ACPI_FAILURE(status))
1461 			return;
1462 
1463 		val = ((val_ec & IBM_EC_MASK_MUTE) == IBM_EC_MASK_MUTE);
1464 		acpi_ibm_mute_set(sc, (val == 0));
1465 		break;
1466 
1467 	default:
1468 		break;
1469 	}
1470 	ACPI_SERIAL_END(ibm);
1471 }
1472 
1473 static void
1474 acpi_ibm_notify(ACPI_HANDLE h, UINT32 notify, void *context)
1475 {
1476 	int		event, arg, type;
1477 	device_t	dev = context;
1478 	struct acpi_ibm_softc *sc = device_get_softc(dev);
1479 
1480 	ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
1481 
1482 	if (notify != 0x80)
1483 		device_printf(dev, "Unknown notify\n");
1484 
1485 	for (;;) {
1486 		acpi_GetInteger(acpi_get_handle(dev), IBM_NAME_EVENTS_GET, &event);
1487 		if (event == 0)
1488 			break;
1489 
1490 		type = (event >> 12) & 0xf;
1491 		arg = event & 0xfff;
1492 		switch (type) {
1493 		case 1:
1494 			if (!(sc->events_availmask & (1 << (arg - 1)))) {
1495 				device_printf(dev, "Unknown key %d\n", arg);
1496 				break;
1497 			}
1498 
1499 			/* Execute event handler */
1500 			if (sc->handler_events & (1 << (arg - 1)))
1501 				acpi_ibm_eventhandler(sc, (arg & 0xff));
1502 
1503 			/* Notify devd(8) */
1504 			acpi_UserNotify("IBM", h, (arg & 0xff));
1505 			break;
1506 		default:
1507 			break;
1508 		}
1509 	}
1510 }
1511