2 * Copyright (c) 2004 Takanori Watanabe
3 * Copyright (c) 2005 Markus Brueffer <markus@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/sys/dev/acpi_support/acpi_ibm.c,v 1.19 2009/06/05 18:44:36 jkim
29 #include <sys/cdefs.h>
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
36 * acpi-ibm: <http://ibm-acpi.sourceforge.net/>
37 * tpb: <http://www.nongnu.org/tpb/>
41 #include <sys/param.h>
42 #include <sys/kernel.h>
44 #include <machine/cpufunc.h>
45 #include <machine/clock.h>
51 #include <sys/module.h>
52 #include <dev/acpica5/acpivar.h>
53 #if defined(__FreeBSD__)
54 #include <dev/led/led.h>
56 #include <sys/sysctl.h>
57 #include <bus/isa/rtc.h>
59 #define _COMPONENT ACPI_OEM
60 ACPI_MODULE_NAME("IBM")
62 /* Internal methods */
63 #define ACPI_IBM_METHOD_EVENTS 1
64 #define ACPI_IBM_METHOD_EVENTMASK 2
65 #define ACPI_IBM_METHOD_HOTKEY 3
66 #define ACPI_IBM_METHOD_BRIGHTNESS 4
67 #define ACPI_IBM_METHOD_VOLUME 5
68 #define ACPI_IBM_METHOD_MUTE 6
69 #define ACPI_IBM_METHOD_THINKLIGHT 7
70 #define ACPI_IBM_METHOD_BLUETOOTH 8
71 #define ACPI_IBM_METHOD_WLAN 9
72 #define ACPI_IBM_METHOD_FANSPEED 10
73 #define ACPI_IBM_METHOD_FANLEVEL 11
74 #define ACPI_IBM_METHOD_FANSTATUS 12
75 #define ACPI_IBM_METHOD_THERMAL 13
78 #define IBM_RTC_HOTKEY1 0x64
79 #define IBM_RTC_MASK_HOME (1 << 0)
80 #define IBM_RTC_MASK_SEARCH (1 << 1)
81 #define IBM_RTC_MASK_MAIL (1 << 2)
82 #define IBM_RTC_MASK_WLAN (1 << 5)
83 #define IBM_RTC_HOTKEY2 0x65
84 #define IBM_RTC_MASK_THINKPAD (1 << 3)
85 #define IBM_RTC_MASK_ZOOM (1 << 5)
86 #define IBM_RTC_MASK_VIDEO (1 << 6)
87 #define IBM_RTC_MASK_HIBERNATE (1 << 7)
88 #define IBM_RTC_THINKLIGHT 0x66
89 #define IBM_RTC_MASK_THINKLIGHT (1 << 4)
90 #define IBM_RTC_SCREENEXPAND 0x67
91 #define IBM_RTC_MASK_SCREENEXPAND (1 << 5)
92 #define IBM_RTC_BRIGHTNESS 0x6c
93 #define IBM_RTC_MASK_BRIGHTNESS (1 << 5)
94 #define IBM_RTC_VOLUME 0x6e
95 #define IBM_RTC_MASK_VOLUME (1 << 7)
97 /* Embedded Controller registers */
98 #define IBM_EC_BRIGHTNESS 0x31
99 #define IBM_EC_MASK_BRI 0x7
100 #define IBM_EC_VOLUME 0x30
101 #define IBM_EC_MASK_VOL 0xf
102 #define IBM_EC_MASK_MUTE (1 << 6)
103 #define IBM_EC_FANSTATUS 0x2F
104 #define IBM_EC_MASK_FANLEVEL 0x3f
105 #define IBM_EC_MASK_FANDISENGAGED (1 << 6)
106 #define IBM_EC_MASK_FANSTATUS (1 << 7)
107 #define IBM_EC_FANSPEED 0x84
110 #define IBM_CMOS_VOLUME_DOWN 0
111 #define IBM_CMOS_VOLUME_UP 1
112 #define IBM_CMOS_VOLUME_MUTE 2
113 #define IBM_CMOS_BRIGHTNESS_UP 4
114 #define IBM_CMOS_BRIGHTNESS_DOWN 5
117 #define IBM_NAME_KEYLIGHT "KBLT"
118 #define IBM_NAME_WLAN_BT_GET "GBDC"
119 #define IBM_NAME_WLAN_BT_SET "SBDC"
120 #define IBM_NAME_MASK_BT (1 << 1)
121 #define IBM_NAME_MASK_WLAN (1 << 2)
122 #define IBM_NAME_THERMAL_GET "TMP7"
123 #define IBM_NAME_THERMAL_UPDT "UPDT"
125 #define IBM_NAME_EVENTS_STATUS_GET "DHKC"
126 #define IBM_NAME_EVENTS_MASK_GET "DHKN"
127 #define IBM_NAME_EVENTS_STATUS_SET "MHKC"
128 #define IBM_NAME_EVENTS_MASK_SET "MHKM"
129 #define IBM_NAME_EVENTS_GET "MHKP"
130 #define IBM_NAME_EVENTS_AVAILMASK "MHKA"
132 #define ABS(x) (((x) < 0)? -(x) : (x))
134 struct acpi_ibm_softc {
138 /* Embedded controller */
140 ACPI_HANDLE ec_handle;
143 ACPI_HANDLE cmos_handle;
146 ACPI_HANDLE fan_handle;
149 /* Keylight commands and states */
150 ACPI_HANDLE light_handle;
154 int light_get_supported;
155 int light_set_supported;
157 /* led(4) interface */
158 struct cdev *led_dev;
163 int thermal_updt_supported;
165 unsigned int events_availmask;
166 unsigned int events_initialmask;
167 int events_mask_supported;
170 struct sysctl_ctx_list sysctl_ctx;
171 struct sysctl_oid *sysctl_tree;
179 } acpi_ibm_sysctls[] = {
182 .method = ACPI_IBM_METHOD_EVENTS,
183 .description = "ACPI events enable",
184 .access = CTLTYPE_INT | CTLFLAG_RW
188 .method = ACPI_IBM_METHOD_EVENTMASK,
189 .description = "ACPI eventmask",
190 .access = CTLTYPE_INT | CTLFLAG_RW
194 .method = ACPI_IBM_METHOD_HOTKEY,
195 .description = "Key Status",
196 .access = CTLTYPE_INT | CTLFLAG_RD
199 .name = "lcd_brightness",
200 .method = ACPI_IBM_METHOD_BRIGHTNESS,
201 .description = "LCD Brightness",
202 .access = CTLTYPE_INT | CTLFLAG_RW
206 .method = ACPI_IBM_METHOD_VOLUME,
207 .description = "Volume",
208 .access = CTLTYPE_INT | CTLFLAG_RW
212 .method = ACPI_IBM_METHOD_MUTE,
213 .description = "Mute",
214 .access = CTLTYPE_INT | CTLFLAG_RW
217 .name = "thinklight",
218 .method = ACPI_IBM_METHOD_THINKLIGHT,
219 .description = "Thinklight enable",
220 .access = CTLTYPE_INT | CTLFLAG_RW
224 .method = ACPI_IBM_METHOD_BLUETOOTH,
225 .description = "Bluetooth enable",
226 .access = CTLTYPE_INT | CTLFLAG_RW
230 .method = ACPI_IBM_METHOD_WLAN,
231 .description = "WLAN enable",
232 .access = CTLTYPE_INT | CTLFLAG_RD
236 .method = ACPI_IBM_METHOD_FANSPEED,
237 .description = "Fan speed",
238 .access = CTLTYPE_INT | CTLFLAG_RD
242 .method = ACPI_IBM_METHOD_FANLEVEL,
243 .description = "Fan level",
244 .access = CTLTYPE_INT | CTLFLAG_RW
248 .method = ACPI_IBM_METHOD_FANSTATUS,
249 .description = "Fan enable",
250 .access = CTLTYPE_INT | CTLFLAG_RW
256 ACPI_SERIAL_DECL(ibm, "ACPI IBM extras");
258 static int acpi_ibm_probe(device_t dev);
259 static int acpi_ibm_attach(device_t dev);
260 static int acpi_ibm_detach(device_t dev);
262 #if defined(__FreeBSD__)
263 static void ibm_led(void *softc, int onoff);
264 static void ibm_led_task(struct acpi_ibm_softc *sc, int pending __unused);
267 static int acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS);
268 static int acpi_ibm_sysctl_init(struct acpi_ibm_softc *sc, int method);
269 static int acpi_ibm_sysctl_get(struct acpi_ibm_softc *sc, int method);
270 static int acpi_ibm_sysctl_set(struct acpi_ibm_softc *sc, int method, int val);
272 static int acpi_ibm_eventmask_set(struct acpi_ibm_softc *sc, int val);
273 static int acpi_ibm_thermal_sysctl(SYSCTL_HANDLER_ARGS);
274 static void acpi_ibm_notify(ACPI_HANDLE h, UINT32 notify, void *context);
276 static device_method_t acpi_ibm_methods[] = {
277 /* Device interface */
278 DEVMETHOD(device_probe, acpi_ibm_probe),
279 DEVMETHOD(device_attach, acpi_ibm_attach),
280 DEVMETHOD(device_detach, acpi_ibm_detach),
285 static driver_t acpi_ibm_driver = {
288 sizeof(struct acpi_ibm_softc),
291 static devclass_t acpi_ibm_devclass;
293 DRIVER_MODULE(acpi_ibm, acpi, acpi_ibm_driver, acpi_ibm_devclass,
295 MODULE_DEPEND(acpi_ibm, acpi, 1, 1, 1);
296 static char *ibm_ids[] = {"IBM0057", "IBM0068", NULL};
298 #if defined(__FreeBSD__)
300 ibm_led(void *softc, int onoff)
302 struct acpi_ibm_softc* sc = (struct acpi_ibm_softc*) softc;
304 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
310 sc->led_state = onoff;
312 AcpiOsExecute(OSL_NOTIFY_HANDLER, (void *)ibm_led_task, sc);
316 ibm_led_task(struct acpi_ibm_softc *sc, int pending __unused)
318 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
320 ACPI_SERIAL_BEGIN(ibm);
321 acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_THINKLIGHT, sc->led_state);
322 ACPI_SERIAL_END(ibm);
329 acpi_ibm_probe(device_t dev)
331 if (acpi_disabled("ibm") ||
332 ACPI_ID_PROBE(device_get_parent(dev), dev, ibm_ids) == NULL ||
333 device_get_unit(dev) != 0)
336 device_set_desc(dev, "IBM ThinkPad ACPI Extras");
342 acpi_ibm_attach(device_t dev)
344 struct acpi_ibm_softc *sc;
345 devclass_t ec_devclass;
346 struct acpi_softc *acpi_sc;
348 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
350 sc = device_get_softc(dev);
352 sc->handle = acpi_get_handle(dev);
354 /* Look for the first embedded controller */
355 if (!(ec_devclass = devclass_find ("acpi_ec"))) {
357 device_printf(dev, "Couldn't find acpi_ec devclass\n");
360 if (!(sc->ec_dev = devclass_get_device(ec_devclass, 0))) {
362 device_printf(dev, "Couldn't find acpi_ec device\n");
365 sc->ec_handle = acpi_get_handle(sc->ec_dev);
366 acpi_sc = acpi_device_get_parent_softc(dev);
368 ACPI_SERIAL_BEGIN(ibm);
370 /* Get the sysctl tree */
371 sysctl_ctx_init(&sc->sysctl_ctx);
372 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
373 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO,
374 "thinkpad", CTLFLAG_RD, 0, "");
376 /* Look for event mask and hook up the nodes */
377 sc->events_mask_supported = ACPI_SUCCESS(acpi_GetInteger(sc->handle,
378 IBM_NAME_EVENTS_MASK_GET, &sc->events_initialmask));
380 if (sc->events_mask_supported) {
381 SYSCTL_ADD_INT(&sc->sysctl_ctx,
382 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
383 "initialmask", CTLFLAG_RD,
384 &sc->events_initialmask, 0, "Initial eventmask");
386 /* The availmask is the bitmask of supported events */
387 if (ACPI_FAILURE(acpi_GetInteger(sc->handle,
388 IBM_NAME_EVENTS_AVAILMASK, &sc->events_availmask)))
389 sc->events_availmask = 0xffffffff;
391 SYSCTL_ADD_INT(&sc->sysctl_ctx,
392 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
393 "availmask", CTLFLAG_RD,
394 &sc->events_availmask, 0, "Mask of supported events");
397 /* Hook up proc nodes */
398 for (int i = 0; acpi_ibm_sysctls[i].name != NULL; i++) {
399 if (!acpi_ibm_sysctl_init(sc, acpi_ibm_sysctls[i].method))
402 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
403 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
404 acpi_ibm_sysctls[i].name, acpi_ibm_sysctls[i].access,
405 sc, i, acpi_ibm_sysctl, "I",
406 acpi_ibm_sysctls[i].description);
409 /* Hook up thermal node */
410 if (acpi_ibm_sysctl_init(sc, ACPI_IBM_METHOD_THERMAL)) {
411 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
412 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
413 "thermal", CTLTYPE_STRING | CTLFLAG_RD,
414 sc, 0, acpi_ibm_thermal_sysctl, "I",
418 ACPI_SERIAL_END(ibm);
420 /* Handle notifies */
421 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
422 acpi_ibm_notify, dev);
423 #if defined(__FreeBSD__)
424 /* Hook up light to led(4) */
425 if (sc->light_set_supported)
426 sc->led_dev = led_create_state(ibm_led, sc, "thinklight", sc->light_val);
433 acpi_ibm_detach(device_t dev)
435 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
437 struct acpi_ibm_softc *sc = device_get_softc(dev);
439 /* Disable events and restore eventmask */
440 ACPI_SERIAL_BEGIN(ibm);
441 acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTS, 0);
442 acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTMASK, sc->events_initialmask);
443 ACPI_SERIAL_END(ibm);
445 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY, acpi_ibm_notify);
446 #if defined(__FreeBSD__)
447 if (sc->led_dev != NULL)
448 led_destroy(sc->led_dev);
455 acpi_ibm_eventmask_set(struct acpi_ibm_softc *sc, int val)
458 ACPI_OBJECT_LIST args;
461 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
462 ACPI_SERIAL_ASSERT(ibm);
466 arg[0].Type = ACPI_TYPE_INTEGER;
467 arg[1].Type = ACPI_TYPE_INTEGER;
469 for (int i = 0; i < 32; ++i) {
470 arg[0].Integer.Value = i+1;
471 arg[1].Integer.Value = (((1 << i) & val) != 0);
472 status = AcpiEvaluateObject(sc->handle,
473 IBM_NAME_EVENTS_MASK_SET, &args, NULL);
475 if (ACPI_FAILURE(status))
483 acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS)
485 struct acpi_ibm_softc *sc;
491 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
493 sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
494 function = oidp->oid_arg2;
495 method = acpi_ibm_sysctls[function].method;
497 ACPI_SERIAL_BEGIN(ibm);
498 arg = acpi_ibm_sysctl_get(sc, method);
499 error = sysctl_handle_int(oidp, &arg, 0, req);
502 if (error != 0 || req->newptr == NULL)
506 error = acpi_ibm_sysctl_set(sc, method, arg);
509 ACPI_SERIAL_END(ibm);
514 acpi_ibm_sysctl_get(struct acpi_ibm_softc *sc, int method)
519 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
520 ACPI_SERIAL_ASSERT(ibm);
523 case ACPI_IBM_METHOD_EVENTS:
524 acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_GET, &val);
527 case ACPI_IBM_METHOD_EVENTMASK:
528 if (sc->events_mask_supported)
529 acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_MASK_GET, &val);
532 case ACPI_IBM_METHOD_HOTKEY:
534 * Construct the hotkey as a bitmask as illustrated below.
535 * Note that whenever a key was pressed, the respecting bit
536 * toggles and nothing else changes.
537 * +--+--+-+-+-+-+-+-+-+-+-+-+
538 * |11|10|9|8|7|6|5|4|3|2|1|0|
539 * +--+--+-+-+-+-+-+-+-+-+-+-+
540 * | | | | | | | | | | | |
541 * | | | | | | | | | | | +- Home Button
542 * | | | | | | | | | | +--- Search Button
543 * | | | | | | | | | +----- Mail Button
544 * | | | | | | | | +------- Thinkpad Button
545 * | | | | | | | +--------- Zoom (Fn + Space)
546 * | | | | | | +----------- WLAN Button
547 * | | | | | +------------- Video Button
548 * | | | | +--------------- Hibernate Button
549 * | | | +----------------- Thinklight Button
550 * | | +------------------- Screen expand (Fn + F8)
551 * | +--------------------- Brightness
552 * +------------------------ Volume/Mute
554 key = rtcin(IBM_RTC_HOTKEY1);
555 val = (IBM_RTC_MASK_HOME | IBM_RTC_MASK_SEARCH | IBM_RTC_MASK_MAIL | IBM_RTC_MASK_WLAN) & key;
556 key = rtcin(IBM_RTC_HOTKEY2);
557 val |= (IBM_RTC_MASK_THINKPAD | IBM_RTC_MASK_VIDEO | IBM_RTC_MASK_HIBERNATE) & key;
558 val |= (IBM_RTC_MASK_ZOOM & key) >> 1;
559 key = rtcin(IBM_RTC_THINKLIGHT);
560 val |= (IBM_RTC_MASK_THINKLIGHT & key) << 4;
561 key = rtcin(IBM_RTC_SCREENEXPAND);
562 val |= (IBM_RTC_MASK_THINKLIGHT & key) << 4;
563 key = rtcin(IBM_RTC_BRIGHTNESS);
564 val |= (IBM_RTC_MASK_BRIGHTNESS & key) << 5;
565 key = rtcin(IBM_RTC_VOLUME);
566 val |= (IBM_RTC_MASK_VOLUME & key) << 4;
569 case ACPI_IBM_METHOD_BRIGHTNESS:
570 ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS, &val_ec, 1);
571 val = val_ec & IBM_EC_MASK_BRI;
574 case ACPI_IBM_METHOD_VOLUME:
575 ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
576 val = val_ec & IBM_EC_MASK_VOL;
579 case ACPI_IBM_METHOD_MUTE:
580 ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
581 val = ((val_ec & IBM_EC_MASK_MUTE) == IBM_EC_MASK_MUTE);
584 case ACPI_IBM_METHOD_THINKLIGHT:
585 if (sc->light_get_supported)
586 acpi_GetInteger(sc->ec_handle, IBM_NAME_KEYLIGHT, &val);
591 case ACPI_IBM_METHOD_BLUETOOTH:
592 acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &val);
593 sc->wlan_bt_flags = val;
594 val = ((val & IBM_NAME_MASK_BT) != 0);
597 case ACPI_IBM_METHOD_WLAN:
598 acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &val);
599 sc->wlan_bt_flags = val;
600 val = ((val & IBM_NAME_MASK_WLAN) != 0);
603 case ACPI_IBM_METHOD_FANSPEED:
604 if (sc->fan_handle) {
605 if(ACPI_FAILURE(acpi_GetInteger(sc->fan_handle, NULL, &val)))
609 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSPEED, &val_ec, 2);
614 case ACPI_IBM_METHOD_FANLEVEL:
616 * The IBM_EC_FANSTATUS register works as follows:
617 * Bit 0-5 indicate the level at which the fan operates. Only
618 * values between 0 and 7 have an effect. Everything
619 * above 7 is treated the same as level 7
620 * Bit 6 overrides the fan speed limit if set to 1
621 * Bit 7 indicates at which mode the fan operates:
622 * manual (0) or automatic (1)
624 if (!sc->fan_handle) {
625 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
626 val = val_ec & IBM_EC_MASK_FANLEVEL;
630 case ACPI_IBM_METHOD_FANSTATUS:
631 if (!sc->fan_handle) {
632 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
633 val = (val_ec & IBM_EC_MASK_FANSTATUS) == IBM_EC_MASK_FANSTATUS;
644 acpi_ibm_sysctl_set(struct acpi_ibm_softc *sc, int method, int arg)
649 ACPI_OBJECT_LIST Args;
652 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
653 ACPI_SERIAL_ASSERT(ibm);
656 case ACPI_IBM_METHOD_EVENTS:
657 if (arg < 0 || arg > 1)
660 status = acpi_SetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_SET, arg);
661 if (ACPI_FAILURE(status))
663 if (sc->events_mask_supported)
664 return acpi_ibm_eventmask_set(sc, sc->events_availmask);
667 case ACPI_IBM_METHOD_EVENTMASK:
668 if (sc->events_mask_supported)
669 return acpi_ibm_eventmask_set(sc, arg);
672 case ACPI_IBM_METHOD_BRIGHTNESS:
673 if (arg < 0 || arg > 7)
676 if (sc->cmos_handle) {
677 /* Read the current brightness */
678 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS, &val_ec, 1);
679 if (ACPI_FAILURE(status))
681 val = val_ec & IBM_EC_MASK_BRI;
685 Arg.Type = ACPI_TYPE_INTEGER;
686 Arg.Integer.Value = (arg > val) ? IBM_CMOS_BRIGHTNESS_UP : IBM_CMOS_BRIGHTNESS_DOWN;
688 step = (arg > val) ? 1 : -1;
689 for (int i = val; i != arg; i += step) {
690 status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
691 if (ACPI_FAILURE(status))
695 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_BRIGHTNESS, arg, 1);
698 case ACPI_IBM_METHOD_VOLUME:
699 if (arg < 0 || arg > 14)
702 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
703 if (ACPI_FAILURE(status))
706 if (sc->cmos_handle) {
707 val = val_ec & IBM_EC_MASK_VOL;
711 Arg.Type = ACPI_TYPE_INTEGER;
712 Arg.Integer.Value = (arg > val) ? IBM_CMOS_VOLUME_UP : IBM_CMOS_VOLUME_DOWN;
714 step = (arg > val) ? 1 : -1;
715 for (int i = val; i != arg; i += step) {
716 status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
717 if (ACPI_FAILURE(status))
721 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME, arg + (val_ec & (~IBM_EC_MASK_VOL)), 1);
724 case ACPI_IBM_METHOD_MUTE:
725 if (arg < 0 || arg > 1)
728 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
729 if (ACPI_FAILURE(status))
732 if (sc->cmos_handle) {
733 val = val_ec & IBM_EC_MASK_VOL;
737 Arg.Type = ACPI_TYPE_INTEGER;
738 Arg.Integer.Value = IBM_CMOS_VOLUME_MUTE;
740 status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
741 if (ACPI_FAILURE(status))
744 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);
747 case ACPI_IBM_METHOD_THINKLIGHT:
748 if (arg < 0 || arg > 1)
751 if (sc->light_set_supported) {
754 Arg.Type = ACPI_TYPE_INTEGER;
755 Arg.Integer.Value = arg ? sc->light_cmd_on : sc->light_cmd_off;
757 status = AcpiEvaluateObject(sc->light_handle, NULL, &Args, NULL);
758 if (ACPI_SUCCESS(status))
764 case ACPI_IBM_METHOD_BLUETOOTH:
765 if (arg < 0 || arg > 1)
768 val = (arg == 1) ? sc->wlan_bt_flags | IBM_NAME_MASK_BT : sc->wlan_bt_flags & (~IBM_NAME_MASK_BT);
769 return acpi_SetInteger(sc->handle, IBM_NAME_WLAN_BT_SET, val);
772 case ACPI_IBM_METHOD_FANLEVEL:
773 if (arg < 0 || arg > 7)
776 if (!sc->fan_handle) {
777 /* Read the current fanstatus */
778 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
779 val = val_ec & (~IBM_EC_MASK_FANLEVEL);
781 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_FANSTATUS, val | arg, 1);
785 case ACPI_IBM_METHOD_FANSTATUS:
786 if (arg < 0 || arg > 1)
789 if (!sc->fan_handle) {
790 /* Read the current fanstatus */
791 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
793 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_FANSTATUS,
794 (arg == 1) ? (val_ec | IBM_EC_MASK_FANSTATUS) : (val_ec & (~IBM_EC_MASK_FANSTATUS)), 1);
803 acpi_ibm_sysctl_init(struct acpi_ibm_softc *sc, int method)
806 ACPI_OBJECT_TYPE cmos_t;
807 ACPI_HANDLE ledb_handle;
810 case ACPI_IBM_METHOD_EVENTS:
811 /* Events are disabled by default */
814 case ACPI_IBM_METHOD_EVENTMASK:
815 return (sc->events_mask_supported);
817 case ACPI_IBM_METHOD_HOTKEY:
818 case ACPI_IBM_METHOD_BRIGHTNESS:
819 case ACPI_IBM_METHOD_VOLUME:
820 case ACPI_IBM_METHOD_MUTE:
821 /* EC is required here, which was aready checked before */
824 case ACPI_IBM_METHOD_THINKLIGHT:
825 sc->cmos_handle = NULL;
826 sc->light_get_supported = ACPI_SUCCESS(acpi_GetInteger(
827 sc->ec_handle, IBM_NAME_KEYLIGHT, &sc->light_val));
829 if ((ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\UCMS", &sc->light_handle)) ||
830 ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\CMOS", &sc->light_handle)) ||
831 ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\CMS", &sc->light_handle))) &&
832 ACPI_SUCCESS(AcpiGetType(sc->light_handle, &cmos_t)) &&
833 cmos_t == ACPI_TYPE_METHOD) {
834 sc->light_cmd_on = 0x0c;
835 sc->light_cmd_off = 0x0d;
836 sc->cmos_handle = sc->light_handle;
838 else if (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\LGHT", &sc->light_handle))) {
839 sc->light_cmd_on = 1;
840 sc->light_cmd_off = 0;
843 sc->light_handle = NULL;
845 sc->light_set_supported = (sc->light_handle &&
846 ACPI_FAILURE(AcpiGetHandle(sc->ec_handle, "LEDB", &ledb_handle)));
848 if (sc->light_get_supported)
851 if (sc->light_set_supported) {
858 case ACPI_IBM_METHOD_BLUETOOTH:
859 case ACPI_IBM_METHOD_WLAN:
860 if (ACPI_SUCCESS(acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &dummy)))
864 case ACPI_IBM_METHOD_FANSPEED:
866 * Some models report the fan speed in levels from 0-7
867 * Newer models report it contiguously
870 (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "GFAN", &sc->fan_handle)) ||
871 ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\FSPD", &sc->fan_handle)));
874 case ACPI_IBM_METHOD_FANLEVEL:
875 case ACPI_IBM_METHOD_FANSTATUS:
877 * Fan status is only supported on those models,
878 * which report fan RPM contiguously, not in levels
884 case ACPI_IBM_METHOD_THERMAL:
885 if (ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle, IBM_NAME_THERMAL_GET, &dummy))) {
886 sc->thermal_updt_supported = ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle, IBM_NAME_THERMAL_UPDT, &dummy));
895 acpi_ibm_thermal_sysctl(SYSCTL_HANDLER_ARGS)
897 struct acpi_ibm_softc *sc;
899 char temp_cmd[] = "TMP0";
902 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
904 sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
906 ACPI_SERIAL_BEGIN(ibm);
908 for (int i = 0; i < 8; ++i) {
909 temp_cmd[3] = '0' + i;
912 * The TMPx methods seem to return +/- 128 or 0
913 * when the respecting sensor is not available
915 if (ACPI_FAILURE(acpi_GetInteger(sc->ec_handle, temp_cmd,
916 &temp[i])) || ABS(temp[i]) == 128 || temp[i] == 0)
918 else if (sc->thermal_updt_supported)
919 /* Temperature is reported in tenth of Kelvin */
920 temp[i] = (temp[i] - 2732 + 5) / 10;
923 error = sysctl_handle_opaque(oidp, &temp, 8*sizeof(int), req);
925 ACPI_SERIAL_END(ibm);
930 acpi_ibm_notify(ACPI_HANDLE h, UINT32 notify, void *context)
932 int event, arg, type;
933 device_t dev = context;
934 struct acpi_ibm_softc *sc = device_get_softc(dev);
936 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
939 device_printf(dev, "Unknown notify\n");
942 acpi_GetInteger(acpi_get_handle(dev), IBM_NAME_EVENTS_GET, &event);
948 type = (event >> 12) & 0xf;
952 if (!(sc->events_availmask & (1 << (arg - 1)))) {
953 device_printf(dev, "Unknown key %d\n", arg);
958 acpi_UserNotify("IBM", h, (arg & 0xff));