xref: /freebsd/sys/dev/acpi_support/acpi_ibm.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*-
2  * Copyright (c) 2004 Takanori Watanabe
3  * Copyright (c) 2005 Markus Brueffer <markus@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 /*
32  * Driver for extra ACPI-controlled gadgets found on IBM ThinkPad laptops.
33  * Inspired by the ibm-acpi and tpb projects which implement these features
34  * on Linux.
35  *
36  *   acpi-ibm: <http://ibm-acpi.sourceforge.net/>
37  *        tpb: <http://www.nongnu.org/tpb/>
38  */
39 
40 #include "opt_acpi.h"
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/bus.h>
44 #include <machine/cpufunc.h>
45 #include <contrib/dev/acpica/acpi.h>
46 #include "acpi_if.h"
47 #include <sys/module.h>
48 #include <dev/acpica/acpivar.h>
49 #include <dev/led/led.h>
50 #include <sys/sysctl.h>
51 #include <machine/clock.h>
52 
53 #define _COMPONENT	ACPI_OEM
54 ACPI_MODULE_NAME("IBM")
55 
56 /* Internal methods */
57 #define ACPI_IBM_METHOD_EVENTS		1
58 #define ACPI_IBM_METHOD_EVENTMASK	2
59 #define ACPI_IBM_METHOD_HOTKEY		3
60 #define ACPI_IBM_METHOD_BRIGHTNESS	4
61 #define ACPI_IBM_METHOD_VOLUME		5
62 #define ACPI_IBM_METHOD_MUTE		6
63 #define ACPI_IBM_METHOD_THINKLIGHT	7
64 #define ACPI_IBM_METHOD_BLUETOOTH	8
65 #define ACPI_IBM_METHOD_WLAN		9
66 #define ACPI_IBM_METHOD_FANSPEED	10
67 #define ACPI_IBM_METHOD_FANLEVEL	11
68 #define ACPI_IBM_METHOD_FANSTATUS	12
69 #define ACPI_IBM_METHOD_THERMAL		13
70 
71 /* Hotkeys/Buttons */
72 #define IBM_RTC_HOTKEY1			0x64
73 #define   IBM_RTC_MASK_HOME		(1 << 0)
74 #define   IBM_RTC_MASK_SEARCH		(1 << 1)
75 #define   IBM_RTC_MASK_MAIL		(1 << 2)
76 #define   IBM_RTC_MASK_WLAN		(1 << 5)
77 #define IBM_RTC_HOTKEY2			0x65
78 #define   IBM_RTC_MASK_THINKPAD		(1 << 3)
79 #define   IBM_RTC_MASK_ZOOM		(1 << 5)
80 #define   IBM_RTC_MASK_VIDEO		(1 << 6)
81 #define   IBM_RTC_MASK_HIBERNATE	(1 << 7)
82 #define IBM_RTC_THINKLIGHT		0x66
83 #define   IBM_RTC_MASK_THINKLIGHT	(1 << 4)
84 #define IBM_RTC_SCREENEXPAND		0x67
85 #define   IBM_RTC_MASK_SCREENEXPAND	(1 << 5)
86 #define IBM_RTC_BRIGHTNESS		0x6c
87 #define   IBM_RTC_MASK_BRIGHTNESS	(1 << 5)
88 #define IBM_RTC_VOLUME			0x6e
89 #define   IBM_RTC_MASK_VOLUME		(1 << 7)
90 
91 /* Embedded Controller registers */
92 #define IBM_EC_BRIGHTNESS		0x31
93 #define   IBM_EC_MASK_BRI		0x7
94 #define IBM_EC_VOLUME			0x30
95 #define   IBM_EC_MASK_VOL		0xf
96 #define   IBM_EC_MASK_MUTE		(1 << 6)
97 #define IBM_EC_FANSTATUS		0x2F
98 #define   IBM_EC_MASK_FANLEVEL		0x3f
99 #define   IBM_EC_MASK_FANDISENGAGED	(1 << 6)
100 #define   IBM_EC_MASK_FANSTATUS		(1 << 7)
101 #define IBM_EC_FANSPEED			0x84
102 
103 /* CMOS Commands */
104 #define IBM_CMOS_VOLUME_DOWN		0
105 #define IBM_CMOS_VOLUME_UP		1
106 #define IBM_CMOS_VOLUME_MUTE		2
107 #define IBM_CMOS_BRIGHTNESS_UP		4
108 #define IBM_CMOS_BRIGHTNESS_DOWN	5
109 
110 /* ACPI methods */
111 #define IBM_NAME_KEYLIGHT		"KBLT"
112 #define IBM_NAME_WLAN_BT_GET		"GBDC"
113 #define IBM_NAME_WLAN_BT_SET		"SBDC"
114 #define   IBM_NAME_MASK_BT		(1 << 1)
115 #define   IBM_NAME_MASK_WLAN		(1 << 2)
116 #define IBM_NAME_THERMAL_GET		"TMP7"
117 #define IBM_NAME_THERMAL_UPDT		"UPDT"
118 
119 #define IBM_NAME_EVENTS_STATUS_GET	"DHKC"
120 #define IBM_NAME_EVENTS_MASK_GET	"DHKN"
121 #define IBM_NAME_EVENTS_STATUS_SET	"MHKC"
122 #define IBM_NAME_EVENTS_MASK_SET	"MHKM"
123 #define IBM_NAME_EVENTS_GET		"MHKP"
124 #define IBM_NAME_EVENTS_AVAILMASK	"MHKA"
125 
126 #define ABS(x) (((x) < 0)? -(x) : (x))
127 
128 struct acpi_ibm_softc {
129 	device_t	dev;
130 	ACPI_HANDLE	handle;
131 
132 	/* Embedded controller */
133 	device_t	ec_dev;
134 	ACPI_HANDLE	ec_handle;
135 
136 	/* CMOS */
137 	ACPI_HANDLE	cmos_handle;
138 
139 	/* Fan status */
140 	ACPI_HANDLE	fan_handle;
141 	int		fan_levels;
142 
143 	/* Keylight commands and states */
144 	ACPI_HANDLE	light_handle;
145 	int		light_cmd_on;
146 	int		light_cmd_off;
147 	int		light_val;
148 	int		light_get_supported;
149 	int		light_set_supported;
150 
151 	/* led(4) interface */
152 	struct cdev	*led_dev;
153 	int		led_busy;
154 	int		led_state;
155 
156 	int		wlan_bt_flags;
157 	int		thermal_updt_supported;
158 
159 	unsigned int	events_availmask;
160 	unsigned int	events_initialmask;
161 	int		events_mask_supported;
162 	int		events_enable;
163 
164 	struct sysctl_ctx_list	*sysctl_ctx;
165 	struct sysctl_oid	*sysctl_tree;
166 };
167 
168 static struct {
169 	char	*name;
170 	int	method;
171 	char	*description;
172 	int	access;
173 } acpi_ibm_sysctls[] = {
174 	{
175 		.name		= "events",
176 		.method		= ACPI_IBM_METHOD_EVENTS,
177 		.description	= "ACPI events enable",
178 		.access		= CTLTYPE_INT | CTLFLAG_RW
179 	},
180 	{
181 		.name		= "eventmask",
182 		.method		= ACPI_IBM_METHOD_EVENTMASK,
183 		.description	= "ACPI eventmask",
184 		.access		= CTLTYPE_INT | CTLFLAG_RW
185 	},
186 	{
187 		.name		= "hotkey",
188 		.method		= ACPI_IBM_METHOD_HOTKEY,
189 		.description	= "Key Status",
190 		.access		= CTLTYPE_INT | CTLFLAG_RD
191 	},
192 	{
193 		.name		= "lcd_brightness",
194 		.method		= ACPI_IBM_METHOD_BRIGHTNESS,
195 		.description	= "LCD Brightness",
196 		.access		= CTLTYPE_INT | CTLFLAG_RW
197 	},
198 	{
199 		.name		= "volume",
200 		.method		= ACPI_IBM_METHOD_VOLUME,
201 		.description	= "Volume",
202 		.access		= CTLTYPE_INT | CTLFLAG_RW
203 	},
204 	{
205 		.name		= "mute",
206 		.method		= ACPI_IBM_METHOD_MUTE,
207 		.description	= "Mute",
208 		.access		= CTLTYPE_INT | CTLFLAG_RW
209 	},
210 	{
211 		.name		= "thinklight",
212 		.method		= ACPI_IBM_METHOD_THINKLIGHT,
213 		.description	= "Thinklight enable",
214 		.access		= CTLTYPE_INT | CTLFLAG_RW
215 	},
216 	{
217 		.name		= "bluetooth",
218 		.method		= ACPI_IBM_METHOD_BLUETOOTH,
219 		.description	= "Bluetooth enable",
220 		.access		= CTLTYPE_INT | CTLFLAG_RW
221 	},
222 	{
223 		.name		= "wlan",
224 		.method		= ACPI_IBM_METHOD_WLAN,
225 		.description	= "WLAN enable",
226 		.access		= CTLTYPE_INT | CTLFLAG_RD
227 	},
228 	{
229 		.name		= "fan_speed",
230 		.method		= ACPI_IBM_METHOD_FANSPEED,
231 		.description	= "Fan speed",
232 		.access		= CTLTYPE_INT | CTLFLAG_RD
233 	},
234 	{
235 		.name		= "fan_level",
236 		.method		= ACPI_IBM_METHOD_FANLEVEL,
237 		.description	= "Fan level",
238 		.access		= CTLTYPE_INT | CTLFLAG_RW
239 	},
240 	{
241 		.name		= "fan",
242 		.method		= ACPI_IBM_METHOD_FANSTATUS,
243 		.description	= "Fan enable",
244 		.access		= CTLTYPE_INT | CTLFLAG_RW
245 	},
246 
247 	{ NULL, 0, NULL, 0 }
248 };
249 
250 ACPI_SERIAL_DECL(ibm, "ACPI IBM extras");
251 
252 static int	acpi_ibm_probe(device_t dev);
253 static int	acpi_ibm_attach(device_t dev);
254 static int	acpi_ibm_detach(device_t dev);
255 
256 static void	ibm_led(void *softc, int onoff);
257 static void	ibm_led_task(struct acpi_ibm_softc *sc, int pending __unused);
258 
259 static int	acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS);
260 static int	acpi_ibm_sysctl_init(struct acpi_ibm_softc *sc, int method);
261 static int	acpi_ibm_sysctl_get(struct acpi_ibm_softc *sc, int method);
262 static int	acpi_ibm_sysctl_set(struct acpi_ibm_softc *sc, int method, int val);
263 
264 static int	acpi_ibm_eventmask_set(struct acpi_ibm_softc *sc, int val);
265 static int	acpi_ibm_thermal_sysctl(SYSCTL_HANDLER_ARGS);
266 static void	acpi_ibm_notify(ACPI_HANDLE h, UINT32 notify, void *context);
267 
268 static device_method_t acpi_ibm_methods[] = {
269 	/* Device interface */
270 	DEVMETHOD(device_probe, acpi_ibm_probe),
271 	DEVMETHOD(device_attach, acpi_ibm_attach),
272 	DEVMETHOD(device_detach, acpi_ibm_detach),
273 
274 	{0, 0}
275 };
276 
277 static driver_t	acpi_ibm_driver = {
278 	"acpi_ibm",
279 	acpi_ibm_methods,
280 	sizeof(struct acpi_ibm_softc),
281 };
282 
283 static devclass_t acpi_ibm_devclass;
284 
285 DRIVER_MODULE(acpi_ibm, acpi, acpi_ibm_driver, acpi_ibm_devclass,
286 	      0, 0);
287 MODULE_DEPEND(acpi_ibm, acpi, 1, 1, 1);
288 static char    *ibm_ids[] = {"IBM0068", NULL};
289 
290 static void
291 ibm_led(void *softc, int onoff)
292 {
293 	struct acpi_ibm_softc* sc = (struct acpi_ibm_softc*) softc;
294 
295 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
296 
297 	if (sc->led_busy)
298 		return;
299 
300 	sc->led_busy = 1;
301 	sc->led_state = onoff;
302 
303 	AcpiOsExecute(OSL_NOTIFY_HANDLER, (void *)ibm_led_task, sc);
304 }
305 
306 static void
307 ibm_led_task(struct acpi_ibm_softc *sc, int pending __unused)
308 {
309 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
310 
311 	ACPI_SERIAL_BEGIN(ibm);
312 	acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_THINKLIGHT, sc->led_state);
313 	ACPI_SERIAL_END(ibm);
314 
315 	sc->led_busy = 0;
316 }
317 
318 static int
319 acpi_ibm_probe(device_t dev)
320 {
321 	if (acpi_disabled("ibm") ||
322 	    ACPI_ID_PROBE(device_get_parent(dev), dev, ibm_ids) == NULL ||
323 	    device_get_unit(dev) != 0)
324 		return (ENXIO);
325 
326 	device_set_desc(dev, "IBM ThinkPad ACPI Extras");
327 
328 	return (0);
329 }
330 
331 static int
332 acpi_ibm_attach(device_t dev)
333 {
334 	struct acpi_ibm_softc	*sc;
335 	struct acpi_softc	*acpi_sc;
336 	devclass_t		ec_devclass;
337 
338 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
339 
340 	sc = device_get_softc(dev);
341 	sc->dev = dev;
342 	sc->handle = acpi_get_handle(dev);
343 
344 	acpi_sc = acpi_device_get_parent_softc(dev);
345 
346 	/* Look for the first embedded controller */
347         if (!(ec_devclass = devclass_find ("acpi_ec"))) {
348 		if (bootverbose)
349 			device_printf(dev, "Couldn't find acpi_ec devclass\n");
350 		return (EINVAL);
351 	}
352         if (!(sc->ec_dev = devclass_get_device(ec_devclass, 0))) {
353 		if (bootverbose)
354 			device_printf(dev, "Couldn't find acpi_ec device\n");
355 		return (EINVAL);
356 	}
357 	sc->ec_handle = acpi_get_handle(sc->ec_dev);
358 
359 	ACPI_SERIAL_BEGIN(ibm);
360 
361 	/* Get the sysctl tree */
362 	sc->sysctl_ctx = device_get_sysctl_ctx(dev);
363 	sc->sysctl_tree = device_get_sysctl_tree(dev);
364 
365 	/* Look for event mask and hook up the nodes */
366 	sc->events_mask_supported = ACPI_SUCCESS(acpi_GetInteger(sc->handle,
367 	    IBM_NAME_EVENTS_MASK_GET, &sc->events_initialmask));
368 
369 	if (sc->events_mask_supported) {
370 		SYSCTL_ADD_INT(sc->sysctl_ctx,
371 		    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
372 		    "initialmask", CTLFLAG_RD,
373 		    &sc->events_initialmask, 0, "Initial eventmask");
374 
375 		/* The availmask is the bitmask of supported events */
376 		if (ACPI_FAILURE(acpi_GetInteger(sc->handle,
377 		    IBM_NAME_EVENTS_AVAILMASK, &sc->events_availmask)))
378 			sc->events_availmask = 0xffffffff;
379 
380 		SYSCTL_ADD_INT(sc->sysctl_ctx,
381 		    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
382 		    "availmask", CTLFLAG_RD,
383 		    &sc->events_availmask, 0, "Mask of supported events");
384 	}
385 
386 	/* Hook up proc nodes */
387 	for (int i = 0; acpi_ibm_sysctls[i].name != NULL; i++) {
388 		if (!acpi_ibm_sysctl_init(sc, acpi_ibm_sysctls[i].method))
389 			continue;
390 
391 		SYSCTL_ADD_PROC(sc->sysctl_ctx,
392 		    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
393 		    acpi_ibm_sysctls[i].name, acpi_ibm_sysctls[i].access,
394 		    sc, i, acpi_ibm_sysctl, "I",
395 		    acpi_ibm_sysctls[i].description);
396 	}
397 
398 	/* Hook up thermal node */
399 	if (acpi_ibm_sysctl_init(sc, ACPI_IBM_METHOD_THERMAL)) {
400 		SYSCTL_ADD_PROC(sc->sysctl_ctx,
401 		    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
402 		    "thermal", CTLTYPE_STRING | CTLFLAG_RD,
403 		    sc, 0, acpi_ibm_thermal_sysctl, "I",
404 		    "Thermal zones");
405 	}
406 
407 	ACPI_SERIAL_END(ibm);
408 
409 	/* Handle notifies */
410 	AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
411 	    acpi_ibm_notify, dev);
412 
413 	/* Hook up light to led(4) */
414 	if (sc->light_set_supported)
415 		sc->led_dev = led_create_state(ibm_led, sc, "thinklight", sc->light_val);
416 
417 	return (0);
418 }
419 
420 static int
421 acpi_ibm_detach(device_t dev)
422 {
423 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
424 
425 	struct acpi_ibm_softc *sc = device_get_softc(dev);
426 
427 	/* Disable events and restore eventmask */
428 	ACPI_SERIAL_BEGIN(ibm);
429 	acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTS, 0);
430 	acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTMASK, sc->events_initialmask);
431 	ACPI_SERIAL_END(ibm);
432 
433 	AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY, acpi_ibm_notify);
434 
435 	if (sc->led_dev != NULL)
436 		led_destroy(sc->led_dev);
437 
438 	return (0);
439 }
440 
441 static int
442 acpi_ibm_eventmask_set(struct acpi_ibm_softc *sc, int val)
443 {
444 	ACPI_OBJECT		arg[2];
445 	ACPI_OBJECT_LIST	args;
446 	ACPI_STATUS		status;
447 
448 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
449 	ACPI_SERIAL_ASSERT(ibm);
450 
451 	args.Count = 2;
452 	args.Pointer = arg;
453 	arg[0].Type = ACPI_TYPE_INTEGER;
454 	arg[1].Type = ACPI_TYPE_INTEGER;
455 
456 	for (int i = 0; i < 32; ++i) {
457 		arg[0].Integer.Value = i+1;
458 		arg[1].Integer.Value = (((1 << i) & val) != 0);
459 		status = AcpiEvaluateObject(sc->handle,
460 		    IBM_NAME_EVENTS_MASK_SET, &args, NULL);
461 
462 		if (ACPI_FAILURE(status))
463 			return (status);
464 	}
465 
466 	return (0);
467 }
468 
469 static int
470 acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS)
471 {
472 	struct acpi_ibm_softc	*sc;
473 	int			arg;
474 	int			error = 0;
475 	int			function;
476 	int			method;
477 
478 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
479 
480 	sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
481 	function = oidp->oid_arg2;
482 	method = acpi_ibm_sysctls[function].method;
483 
484 	ACPI_SERIAL_BEGIN(ibm);
485 	arg = acpi_ibm_sysctl_get(sc, method);
486 	error = sysctl_handle_int(oidp, &arg, 0, req);
487 
488 	/* Sanity check */
489 	if (error != 0 || req->newptr == NULL)
490 		goto out;
491 
492 	/* Update */
493 	error = acpi_ibm_sysctl_set(sc, method, arg);
494 
495 out:
496 	ACPI_SERIAL_END(ibm);
497 	return (error);
498 }
499 
500 static int
501 acpi_ibm_sysctl_get(struct acpi_ibm_softc *sc, int method)
502 {
503 	ACPI_INTEGER	val_ec;
504 	int 		val = 0, key;
505 
506 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
507 	ACPI_SERIAL_ASSERT(ibm);
508 
509 	switch (method) {
510 	case ACPI_IBM_METHOD_EVENTS:
511 		acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_GET, &val);
512 		break;
513 
514 	case ACPI_IBM_METHOD_EVENTMASK:
515 		if (sc->events_mask_supported)
516 			acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_MASK_GET, &val);
517 		break;
518 
519 	case ACPI_IBM_METHOD_HOTKEY:
520 		/*
521 		 * Construct the hotkey as a bitmask as illustrated below.
522 		 * Note that whenever a key was pressed, the respecting bit
523 		 * toggles and nothing else changes.
524 		 * +--+--+-+-+-+-+-+-+-+-+-+-+
525 		 * |11|10|9|8|7|6|5|4|3|2|1|0|
526 		 * +--+--+-+-+-+-+-+-+-+-+-+-+
527 		 *   |  | | | | | | | | | | |
528 		 *   |  | | | | | | | | | | +- Home Button
529 		 *   |  | | | | | | | | | +--- Search Button
530 		 *   |  | | | | | | | | +----- Mail Button
531 		 *   |  | | | | | | | +------- Thinkpad Button
532 		 *   |  | | | | | | +--------- Zoom (Fn + Space)
533 		 *   |  | | | | | +----------- WLAN Button
534 		 *   |  | | | | +------------- Video Button
535 		 *   |  | | | +--------------- Hibernate Button
536 		 *   |  | | +----------------- Thinklight Button
537 		 *   |  | +------------------- Screen expand (Fn + F8)
538 		 *   |  +--------------------- Brightness
539 		 *   +------------------------ Volume/Mute
540 		 */
541 		key = rtcin(IBM_RTC_HOTKEY1);
542 		val = (IBM_RTC_MASK_HOME | IBM_RTC_MASK_SEARCH | IBM_RTC_MASK_MAIL | IBM_RTC_MASK_WLAN) & key;
543 		key = rtcin(IBM_RTC_HOTKEY2);
544 		val |= (IBM_RTC_MASK_THINKPAD | IBM_RTC_MASK_VIDEO | IBM_RTC_MASK_HIBERNATE) & key;
545 		val |= (IBM_RTC_MASK_ZOOM & key) >> 1;
546 		key = rtcin(IBM_RTC_THINKLIGHT);
547 		val |= (IBM_RTC_MASK_THINKLIGHT & key) << 4;
548 		key = rtcin(IBM_RTC_SCREENEXPAND);
549 		val |= (IBM_RTC_MASK_THINKLIGHT & key) << 4;
550 		key = rtcin(IBM_RTC_BRIGHTNESS);
551 		val |= (IBM_RTC_MASK_BRIGHTNESS & key) << 5;
552 		key = rtcin(IBM_RTC_VOLUME);
553 		val |= (IBM_RTC_MASK_VOLUME & key) << 4;
554 		break;
555 
556 	case ACPI_IBM_METHOD_BRIGHTNESS:
557 		ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS, &val_ec, 1);
558 		val = val_ec & IBM_EC_MASK_BRI;
559 		break;
560 
561 	case ACPI_IBM_METHOD_VOLUME:
562 		ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
563 		val = val_ec & IBM_EC_MASK_VOL;
564 		break;
565 
566 	case ACPI_IBM_METHOD_MUTE:
567 		ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
568 		val = ((val_ec & IBM_EC_MASK_MUTE) == IBM_EC_MASK_MUTE);
569 		break;
570 
571 	case ACPI_IBM_METHOD_THINKLIGHT:
572 		if (sc->light_get_supported)
573 			acpi_GetInteger(sc->ec_handle, IBM_NAME_KEYLIGHT, &val);
574 		else
575 			val = sc->light_val;
576 		break;
577 
578 	case ACPI_IBM_METHOD_BLUETOOTH:
579 		acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &val);
580 		sc->wlan_bt_flags = val;
581 		val = ((val & IBM_NAME_MASK_BT) != 0);
582 		break;
583 
584 	case ACPI_IBM_METHOD_WLAN:
585 		acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &val);
586 		sc->wlan_bt_flags = val;
587 		val = ((val & IBM_NAME_MASK_WLAN) != 0);
588 		break;
589 
590 	case ACPI_IBM_METHOD_FANSPEED:
591 		if (sc->fan_handle) {
592 			if(ACPI_FAILURE(acpi_GetInteger(sc->fan_handle, NULL, &val)))
593 				val = -1;
594 		}
595 		else {
596 			ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSPEED, &val_ec, 2);
597 			val = val_ec;
598 		}
599 		break;
600 
601 	case ACPI_IBM_METHOD_FANLEVEL:
602 		/*
603 		 * The IBM_EC_FANSTATUS register works as follows:
604 		 * Bit 0-5 indicate the level at which the fan operates. Only
605 		 *       values between 0 and 7 have an effect. Everything
606 		 *       above 7 is treated the same as level 7
607 		 * Bit 6 overrides the fan speed limit if set to 1
608 		 * Bit 7 indicates at which mode the fan operates:
609 		 *       manual (0) or automatic (1)
610 		 */
611 		if (!sc->fan_handle) {
612 			ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
613 			val = val_ec & IBM_EC_MASK_FANLEVEL;
614 		}
615 		break;
616 
617 	case ACPI_IBM_METHOD_FANSTATUS:
618 		if (!sc->fan_handle) {
619 			ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
620 			val = (val_ec & IBM_EC_MASK_FANSTATUS) == IBM_EC_MASK_FANSTATUS;
621 		}
622 		else
623 			val = -1;
624 		break;
625 	}
626 
627 	return (val);
628 }
629 
630 static int
631 acpi_ibm_sysctl_set(struct acpi_ibm_softc *sc, int method, int arg)
632 {
633 	int			val, step;
634 	ACPI_INTEGER		val_ec;
635 	ACPI_OBJECT		Arg;
636 	ACPI_OBJECT_LIST	Args;
637 	ACPI_STATUS		status;
638 
639 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
640 	ACPI_SERIAL_ASSERT(ibm);
641 
642 	switch (method) {
643 	case ACPI_IBM_METHOD_EVENTS:
644 		if (arg < 0 || arg > 1)
645 			return (EINVAL);
646 
647 		status = acpi_SetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_SET, arg);
648 		if (ACPI_FAILURE(status))
649 			return (status);
650 		if (sc->events_mask_supported)
651 			return acpi_ibm_eventmask_set(sc, sc->events_availmask);
652 		break;
653 
654 	case ACPI_IBM_METHOD_EVENTMASK:
655 		if (sc->events_mask_supported)
656 			return acpi_ibm_eventmask_set(sc, arg);
657 		break;
658 
659 	case ACPI_IBM_METHOD_BRIGHTNESS:
660 		if (arg < 0 || arg > 7)
661 			return (EINVAL);
662 
663 		if (sc->cmos_handle) {
664 			/* Read the current brightness */
665 			status = ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS, &val_ec, 1);
666 			if (ACPI_FAILURE(status))
667 				return (status);
668 			val = val_ec & IBM_EC_MASK_BRI;
669 
670 			Args.Count = 1;
671 			Args.Pointer = &Arg;
672 			Arg.Type = ACPI_TYPE_INTEGER;
673 			Arg.Integer.Value = (arg > val) ? IBM_CMOS_BRIGHTNESS_UP : IBM_CMOS_BRIGHTNESS_DOWN;
674 
675 			step = (arg > val) ? 1 : -1;
676 			for (int i = val; i != arg; i += step) {
677 				status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
678 				if (ACPI_FAILURE(status))
679 					break;
680 			}
681 		}
682 		return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_BRIGHTNESS, arg, 1);
683 		break;
684 
685 	case ACPI_IBM_METHOD_VOLUME:
686 		if (arg < 0 || arg > 14)
687 			return (EINVAL);
688 
689 		status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
690 		if (ACPI_FAILURE(status))
691 			return (status);
692 
693 		if (sc->cmos_handle) {
694 			val = val_ec & IBM_EC_MASK_VOL;
695 
696 			Args.Count = 1;
697 			Args.Pointer = &Arg;
698 			Arg.Type = ACPI_TYPE_INTEGER;
699 			Arg.Integer.Value = (arg > val) ? IBM_CMOS_VOLUME_UP : IBM_CMOS_VOLUME_DOWN;
700 
701 			step = (arg > val) ? 1 : -1;
702 			for (int i = val; i != arg; i += step) {
703 				status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
704 				if (ACPI_FAILURE(status))
705 					break;
706 			}
707 		}
708 		return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME, arg + (val_ec & (~IBM_EC_MASK_VOL)), 1);
709 		break;
710 
711 	case ACPI_IBM_METHOD_MUTE:
712 		if (arg < 0 || arg > 1)
713 			return (EINVAL);
714 
715 		status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
716 		if (ACPI_FAILURE(status))
717 			return (status);
718 
719 		if (sc->cmos_handle) {
720 			val = val_ec & IBM_EC_MASK_VOL;
721 
722 			Args.Count = 1;
723 			Args.Pointer = &Arg;
724 			Arg.Type = ACPI_TYPE_INTEGER;
725 			Arg.Integer.Value = IBM_CMOS_VOLUME_MUTE;
726 
727 			status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
728 			if (ACPI_FAILURE(status))
729 				break;
730 		}
731 		return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME, (arg==1) ? val_ec | IBM_EC_MASK_MUTE : val_ec & (~IBM_EC_MASK_MUTE), 1);
732 		break;
733 
734 	case ACPI_IBM_METHOD_THINKLIGHT:
735 		if (arg < 0 || arg > 1)
736 			return (EINVAL);
737 
738 		if (sc->light_set_supported) {
739 			Args.Count = 1;
740 			Args.Pointer = &Arg;
741 			Arg.Type = ACPI_TYPE_INTEGER;
742 			Arg.Integer.Value = arg ? sc->light_cmd_on : sc->light_cmd_off;
743 
744 			status = AcpiEvaluateObject(sc->light_handle, NULL, &Args, NULL);
745 			if (ACPI_SUCCESS(status))
746 				sc->light_val = arg;
747 			return (status);
748 		}
749 		break;
750 
751 	case ACPI_IBM_METHOD_BLUETOOTH:
752 		if (arg < 0 || arg > 1)
753 			return (EINVAL);
754 
755 		val = (arg == 1) ? sc->wlan_bt_flags | IBM_NAME_MASK_BT : sc->wlan_bt_flags & (~IBM_NAME_MASK_BT);
756 		return acpi_SetInteger(sc->handle, IBM_NAME_WLAN_BT_SET, val);
757 		break;
758 
759 	case ACPI_IBM_METHOD_FANLEVEL:
760 		if (arg < 0 || arg > 7)
761 			return (EINVAL);
762 
763 		if (!sc->fan_handle) {
764 			/* Read the current fanstatus */
765 			ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
766 			val = val_ec & (~IBM_EC_MASK_FANLEVEL);
767 
768 			return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_FANSTATUS, val | arg, 1);
769 		}
770 		break;
771 
772 	case ACPI_IBM_METHOD_FANSTATUS:
773 		if (arg < 0 || arg > 1)
774 			return (EINVAL);
775 
776 		if (!sc->fan_handle) {
777 			/* Read the current fanstatus */
778 			ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
779 
780 			return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_FANSTATUS,
781 				(arg == 1) ? (val_ec | IBM_EC_MASK_FANSTATUS) : (val_ec & (~IBM_EC_MASK_FANSTATUS)), 1);
782 		}
783 		break;
784 	}
785 
786 	return (0);
787 }
788 
789 static int
790 acpi_ibm_sysctl_init(struct acpi_ibm_softc *sc, int method)
791 {
792 	int 			dummy;
793 	ACPI_OBJECT_TYPE 	cmos_t;
794 	ACPI_HANDLE		ledb_handle;
795 
796 	switch (method) {
797 	case ACPI_IBM_METHOD_EVENTS:
798 		/* Events are disabled by default */
799 		return (TRUE);
800 
801 	case ACPI_IBM_METHOD_EVENTMASK:
802 		return (sc->events_mask_supported);
803 
804 	case ACPI_IBM_METHOD_HOTKEY:
805 	case ACPI_IBM_METHOD_BRIGHTNESS:
806 	case ACPI_IBM_METHOD_VOLUME:
807 	case ACPI_IBM_METHOD_MUTE:
808 		/* EC is required here, which was aready checked before */
809 		return (TRUE);
810 
811 	case ACPI_IBM_METHOD_THINKLIGHT:
812 		sc->cmos_handle = NULL;
813 		sc->light_get_supported = ACPI_SUCCESS(acpi_GetInteger(
814 		    sc->ec_handle, IBM_NAME_KEYLIGHT, &sc->light_val));
815 
816 		if ((ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\UCMS", &sc->light_handle)) ||
817 		     ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\CMOS", &sc->light_handle)) ||
818 		     ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\CMS", &sc->light_handle))) &&
819 		     ACPI_SUCCESS(AcpiGetType(sc->light_handle, &cmos_t)) &&
820 		     cmos_t == ACPI_TYPE_METHOD) {
821 			sc->light_cmd_on = 0x0c;
822 			sc->light_cmd_off = 0x0d;
823 			sc->cmos_handle = sc->light_handle;
824 		}
825 		else if (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\LGHT", &sc->light_handle))) {
826 			sc->light_cmd_on = 1;
827 			sc->light_cmd_off = 0;
828 		}
829 		else
830 			sc->light_handle = NULL;
831 
832 		sc->light_set_supported = (sc->light_handle &&
833 		    ACPI_FAILURE(AcpiGetHandle(sc->ec_handle, "LEDB", &ledb_handle)));
834 
835 		if (sc->light_get_supported)
836 			return (TRUE);
837 
838 		if (sc->light_set_supported) {
839 			sc->light_val = 0;
840 			return (TRUE);
841 		}
842 
843 		return (FALSE);
844 
845 	case ACPI_IBM_METHOD_BLUETOOTH:
846 	case ACPI_IBM_METHOD_WLAN:
847 		if (ACPI_SUCCESS(acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &dummy)))
848 			return (TRUE);
849 		return (FALSE);
850 
851 	case ACPI_IBM_METHOD_FANSPEED:
852 		/*
853 		 * Some models report the fan speed in levels from 0-7
854 		 * Newer models report it contiguously
855 		 */
856 		sc->fan_levels =
857 		    (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "GFAN", &sc->fan_handle)) ||
858 		     ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\FSPD", &sc->fan_handle)));
859 		return (TRUE);
860 
861 	case ACPI_IBM_METHOD_FANLEVEL:
862 	case ACPI_IBM_METHOD_FANSTATUS:
863 		/*
864 		 * Fan status is only supported on those models,
865 		 * which report fan RPM contiguously, not in levels
866 		 */
867 		if (sc->fan_levels)
868 			return (FALSE);
869 		return (TRUE);
870 
871 	case ACPI_IBM_METHOD_THERMAL:
872 		if (ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle, IBM_NAME_THERMAL_GET, &dummy))) {
873 			sc->thermal_updt_supported = ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle, IBM_NAME_THERMAL_UPDT, &dummy));
874 			return (TRUE);
875 		}
876 		return (FALSE);
877 	}
878 	return (FALSE);
879 }
880 
881 static int
882 acpi_ibm_thermal_sysctl(SYSCTL_HANDLER_ARGS)
883 {
884 	struct acpi_ibm_softc	*sc;
885 	int			error = 0;
886 	char			temp_cmd[] = "TMP0";
887 	int			temp[8];
888 
889 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
890 
891 	sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
892 
893 	ACPI_SERIAL_BEGIN(ibm);
894 
895 	for (int i = 0; i < 8; ++i) {
896 		temp_cmd[3] = '0' + i;
897 
898 		/*
899 		 * The TMPx methods seem to return +/- 128 or 0
900 		 * when the respecting sensor is not available
901 		 */
902 		if (ACPI_FAILURE(acpi_GetInteger(sc->ec_handle, temp_cmd,
903 		    &temp[i])) || ABS(temp[i]) == 128 || temp[i] == 0)
904 			temp[i] = -1;
905 		else if (sc->thermal_updt_supported)
906 			/* Temperature is reported in tenth of Kelvin */
907 			temp[i] = (temp[i] - 2732 + 5) / 10;
908 	}
909 
910 	error = sysctl_handle_opaque(oidp, &temp, 8*sizeof(int), req);
911 
912 	ACPI_SERIAL_END(ibm);
913 	return (error);
914 }
915 
916 static void
917 acpi_ibm_notify(ACPI_HANDLE h, UINT32 notify, void *context)
918 {
919 	int		event, arg, type;
920 	device_t	dev = context;
921 	struct acpi_ibm_softc *sc = device_get_softc(dev);
922 
923 	ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
924 
925 	if (notify != 0x80)
926 		device_printf(dev, "Unknown notify\n");
927 
928 	for (;;) {
929 		acpi_GetInteger(acpi_get_handle(dev), IBM_NAME_EVENTS_GET, &event);
930 
931 		if (event == 0)
932 			break;
933 
934 
935 		type = (event >> 12) & 0xf;
936 		arg = event & 0xfff;
937 		switch (type) {
938 		case 1:
939 			if (!(sc->events_availmask & (1 << (arg - 1)))) {
940 				device_printf(dev, "Unknown key %d\n", arg);
941 				break;
942 			}
943 
944 			/* Notify devd(8) */
945 			acpi_UserNotify("IBM", h, (arg & 0xff));
946 			break;
947 		default:
948 			break;
949 		}
950 	}
951 }
952