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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
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 <contrib/dev/acpica/acpi.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>
53 #define _COMPONENT ACPI_OEM
54 ACPI_MODULE_NAME("IBM")
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_FANSTATUS 11
68 #define ACPI_IBM_METHOD_THERMAL 12
71 #define IBM_RTC_HOTKEY1 0x64
72 #define IBM_RTC_MASK_HOME (1 << 0)
73 #define IBM_RTC_MASK_SEARCH (1 << 1)
74 #define IBM_RTC_MASK_MAIL (1 << 2)
75 #define IBM_RTC_MASK_WLAN (1 << 5)
76 #define IBM_RTC_HOTKEY2 0x65
77 #define IBM_RTC_MASK_THINKPAD (1 << 3)
78 #define IBM_RTC_MASK_ZOOM (1 << 5)
79 #define IBM_RTC_MASK_VIDEO (1 << 6)
80 #define IBM_RTC_MASK_HIBERNATE (1 << 7)
81 #define IBM_RTC_THINKLIGHT 0x66
82 #define IBM_RTC_MASK_THINKLIGHT (1 << 4)
83 #define IBM_RTC_SCREENEXPAND 0x67
84 #define IBM_RTC_MASK_SCREENEXPAND (1 << 5)
85 #define IBM_RTC_BRIGHTNESS 0x6c
86 #define IBM_RTC_MASK_BRIGHTNESS (1 << 5)
87 #define IBM_RTC_VOLUME 0x6e
88 #define IBM_RTC_MASK_VOLUME (1 << 7)
90 /* Embedded Controller registers */
91 #define IBM_EC_BRIGHTNESS 0x31
92 #define IBM_EC_MASK_BRI 0x7
93 #define IBM_EC_VOLUME 0x30
94 #define IBM_EC_MASK_VOL 0xf
95 #define IBM_EC_MASK_MUTE (1 << 6)
96 #define IBM_EC_FANSTATUS 0x2F
97 #define IBM_EC_MASK_FANSTATUS (1 << 7)
98 #define IBM_EC_FANSPEED 0x84
101 #define IBM_CMOS_VOLUME_DOWN 0
102 #define IBM_CMOS_VOLUME_UP 1
103 #define IBM_CMOS_VOLUME_MUTE 2
104 #define IBM_CMOS_BRIGHTNESS_UP 4
105 #define IBM_CMOS_BRIGHTNESS_DOWN 5
108 #define IBM_NAME_KEYLIGHT "KBLT"
109 #define IBM_NAME_WLAN_BT_GET "GBDC"
110 #define IBM_NAME_WLAN_BT_SET "SBDC"
111 #define IBM_NAME_MASK_BT (1 << 1)
112 #define IBM_NAME_MASK_WLAN (1 << 2)
113 #define IBM_NAME_THERMAL_GET "TMP7"
114 #define IBM_NAME_THERMAL_UPDT "UPDT"
116 #define IBM_NAME_EVENTS_STATUS_GET "DHKC"
117 #define IBM_NAME_EVENTS_MASK_GET "DHKN"
118 #define IBM_NAME_EVENTS_STATUS_SET "MHKC"
119 #define IBM_NAME_EVENTS_MASK_SET "MHKM"
120 #define IBM_NAME_EVENTS_GET "MHKP"
121 #define IBM_NAME_EVENTS_AVAILMASK "MHKA"
123 #define ABS(x) (((x) < 0)? -(x) : (x))
125 struct acpi_ibm_softc {
129 /* Embedded controller */
131 ACPI_HANDLE ec_handle;
134 ACPI_HANDLE cmos_handle;
137 ACPI_HANDLE fan_handle;
140 /* Keylight commands and states */
141 ACPI_HANDLE light_handle;
145 int light_get_supported;
146 int light_set_supported;
148 /* led(4) interface */
149 struct cdev *led_dev;
154 int thermal_updt_supported;
156 unsigned int events_availmask;
157 unsigned int events_initialmask;
158 int events_mask_supported;
161 struct sysctl_ctx_list *sysctl_ctx;
162 struct sysctl_oid *sysctl_tree;
170 } acpi_ibm_sysctls[] = {
173 .method = ACPI_IBM_METHOD_EVENTS,
174 .description = "ACPI events enable",
175 .access = CTLTYPE_INT | CTLFLAG_RW
179 .method = ACPI_IBM_METHOD_EVENTMASK,
180 .description = "ACPI eventmask",
181 .access = CTLTYPE_INT | CTLFLAG_RW
185 .method = ACPI_IBM_METHOD_HOTKEY,
186 .description = "Key Status",
187 .access = CTLTYPE_INT | CTLFLAG_RD
190 .name = "lcd_brightness",
191 .method = ACPI_IBM_METHOD_BRIGHTNESS,
192 .description = "LCD Brightness",
193 .access = CTLTYPE_INT | CTLFLAG_RW
197 .method = ACPI_IBM_METHOD_VOLUME,
198 .description = "Volume",
199 .access = CTLTYPE_INT | CTLFLAG_RW
203 .method = ACPI_IBM_METHOD_MUTE,
204 .description = "Mute",
205 .access = CTLTYPE_INT | CTLFLAG_RW
208 .name = "thinklight",
209 .method = ACPI_IBM_METHOD_THINKLIGHT,
210 .description = "Thinklight enable",
211 .access = CTLTYPE_INT | CTLFLAG_RW
215 .method = ACPI_IBM_METHOD_BLUETOOTH,
216 .description = "Bluetooth enable",
217 .access = CTLTYPE_INT | CTLFLAG_RW
221 .method = ACPI_IBM_METHOD_WLAN,
222 .description = "WLAN enable",
223 .access = CTLTYPE_INT | CTLFLAG_RD
227 .method = ACPI_IBM_METHOD_FANSPEED,
228 .description = "Fan speed",
229 .access = CTLTYPE_INT | CTLFLAG_RD
233 .method = ACPI_IBM_METHOD_FANSTATUS,
234 .description = "Fan enable",
235 .access = CTLTYPE_INT | CTLFLAG_RD
241 ACPI_SERIAL_DECL(ibm, "ACPI IBM extras");
243 static int acpi_ibm_probe(device_t dev);
244 static int acpi_ibm_attach(device_t dev);
245 static int acpi_ibm_detach(device_t dev);
247 static void ibm_led(void *softc, int onoff);
248 static void ibm_led_task(struct acpi_ibm_softc *sc, int pending __unused);
250 static int acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS);
251 static int acpi_ibm_sysctl_init(struct acpi_ibm_softc *sc, int method);
252 static int acpi_ibm_sysctl_get(struct acpi_ibm_softc *sc, int method);
253 static int acpi_ibm_sysctl_set(struct acpi_ibm_softc *sc, int method, int val);
255 static int acpi_ibm_eventmask_set(struct acpi_ibm_softc *sc, int val);
256 static int acpi_ibm_thermal_sysctl(SYSCTL_HANDLER_ARGS);
257 static void acpi_ibm_notify(ACPI_HANDLE h, UINT32 notify, void *context);
259 static device_method_t acpi_ibm_methods[] = {
260 /* Device interface */
261 DEVMETHOD(device_probe, acpi_ibm_probe),
262 DEVMETHOD(device_attach, acpi_ibm_attach),
263 DEVMETHOD(device_detach, acpi_ibm_detach),
268 static driver_t acpi_ibm_driver = {
271 sizeof(struct acpi_ibm_softc),
274 static devclass_t acpi_ibm_devclass;
276 DRIVER_MODULE(acpi_ibm, acpi, acpi_ibm_driver, acpi_ibm_devclass,
278 MODULE_DEPEND(acpi_ibm, acpi, 1, 1, 1);
279 static char *ibm_ids[] = {"IBM0068", NULL};
282 ibm_led(void *softc, int onoff)
284 struct acpi_ibm_softc* sc = (struct acpi_ibm_softc*) softc;
286 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
292 sc->led_state = onoff;
294 AcpiOsQueueForExecution(OSD_PRIORITY_LO,
295 (void *)ibm_led_task, sc);
299 ibm_led_task(struct acpi_ibm_softc *sc, int pending __unused)
301 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
303 ACPI_SERIAL_BEGIN(ibm);
304 acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_THINKLIGHT, sc->led_state);
305 ACPI_SERIAL_END(ibm);
311 acpi_ibm_probe(device_t dev)
313 if (acpi_disabled("ibm") ||
314 ACPI_ID_PROBE(device_get_parent(dev), dev, ibm_ids) == NULL ||
315 device_get_unit(dev) != 0)
318 device_set_desc(dev, "IBM ThinkPad ACPI Extras");
324 acpi_ibm_attach(device_t dev)
326 struct acpi_ibm_softc *sc;
327 struct acpi_softc *acpi_sc;
328 devclass_t ec_devclass;
330 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
332 sc = device_get_softc(dev);
334 sc->handle = acpi_get_handle(dev);
336 acpi_sc = acpi_device_get_parent_softc(dev);
338 /* Look for the first embedded controller */
339 if (!(ec_devclass = devclass_find ("acpi_ec"))) {
341 device_printf(dev, "Couldn't find acpi_ec devclass\n");
344 if (!(sc->ec_dev = devclass_get_device(ec_devclass, 0))) {
346 device_printf(dev, "Couldn't find acpi_ec device\n");
349 sc->ec_handle = acpi_get_handle(sc->ec_dev);
351 ACPI_SERIAL_BEGIN(ibm);
353 /* Get the sysctl tree */
354 sc->sysctl_ctx = device_get_sysctl_ctx(dev);
355 sc->sysctl_tree = device_get_sysctl_tree(dev);
357 /* Look for event mask and hook up the nodes */
358 sc->events_mask_supported = ACPI_SUCCESS(acpi_GetInteger(sc->handle,
359 IBM_NAME_EVENTS_MASK_GET, &sc->events_initialmask));
361 if (sc->events_mask_supported) {
362 SYSCTL_ADD_INT(sc->sysctl_ctx,
363 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
364 "initialmask", CTLFLAG_RD,
365 &sc->events_initialmask, 0, "Initial eventmask");
367 /* The availmask is the bitmask of supported events */
368 if (ACPI_FAILURE(acpi_GetInteger(sc->handle,
369 IBM_NAME_EVENTS_AVAILMASK, &sc->events_availmask)))
370 sc->events_availmask = 0xffffffff;
372 SYSCTL_ADD_INT(sc->sysctl_ctx,
373 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
374 "availmask", CTLFLAG_RD,
375 &sc->events_availmask, 0, "Mask of supported events");
378 /* Hook up proc nodes */
379 for (int i = 0; acpi_ibm_sysctls[i].name != NULL; i++) {
380 if (!acpi_ibm_sysctl_init(sc, acpi_ibm_sysctls[i].method))
383 SYSCTL_ADD_PROC(sc->sysctl_ctx,
384 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
385 acpi_ibm_sysctls[i].name, acpi_ibm_sysctls[i].access,
386 sc, i, acpi_ibm_sysctl, "I",
387 acpi_ibm_sysctls[i].description);
390 /* Hook up thermal node */
391 if (acpi_ibm_sysctl_init(sc, ACPI_IBM_METHOD_THERMAL)) {
392 SYSCTL_ADD_PROC(sc->sysctl_ctx,
393 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
394 "thermal", CTLTYPE_STRING | CTLFLAG_RD,
395 sc, 0, acpi_ibm_thermal_sysctl, "I",
399 ACPI_SERIAL_END(ibm);
401 /* Handle notifies */
402 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
403 acpi_ibm_notify, dev);
405 /* Hook up light to led(4) */
406 if (sc->light_set_supported)
407 sc->led_dev = led_create(ibm_led, sc, "thinklight");
413 acpi_ibm_detach(device_t dev)
415 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
417 struct acpi_ibm_softc *sc = device_get_softc(dev);
419 /* Disable events and restore eventmask */
420 ACPI_SERIAL_BEGIN(ibm);
421 acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTS, 0);
422 acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTMASK, sc->events_initialmask);
423 ACPI_SERIAL_END(ibm);
425 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY, acpi_ibm_notify);
427 if (sc->led_dev != NULL)
428 led_destroy(sc->led_dev);
434 acpi_ibm_eventmask_set(struct acpi_ibm_softc *sc, int val)
437 ACPI_OBJECT_LIST args;
440 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
441 ACPI_SERIAL_ASSERT(ibm);
445 arg[0].Type = ACPI_TYPE_INTEGER;
446 arg[1].Type = ACPI_TYPE_INTEGER;
448 for (int i = 0; i < 32; ++i) {
449 arg[0].Integer.Value = i+1;
450 arg[1].Integer.Value = (((1 << i) & val) != 0);
451 status = AcpiEvaluateObject(sc->handle,
452 IBM_NAME_EVENTS_MASK_SET, &args, NULL);
454 if (ACPI_FAILURE(status))
462 acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS)
464 struct acpi_ibm_softc *sc;
470 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
472 sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
473 function = oidp->oid_arg2;
474 method = acpi_ibm_sysctls[function].method;
476 ACPI_SERIAL_BEGIN(ibm);
477 arg = acpi_ibm_sysctl_get(sc, method);
478 error = sysctl_handle_int(oidp, &arg, 0, req);
481 if (error != 0 || req->newptr == NULL)
485 error = acpi_ibm_sysctl_set(sc, method, arg);
488 ACPI_SERIAL_END(ibm);
493 acpi_ibm_sysctl_get(struct acpi_ibm_softc *sc, int method)
498 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
499 ACPI_SERIAL_ASSERT(ibm);
502 case ACPI_IBM_METHOD_EVENTS:
503 acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_GET, &val);
506 case ACPI_IBM_METHOD_EVENTMASK:
507 if (sc->events_mask_supported)
508 acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_MASK_GET, &val);
511 case ACPI_IBM_METHOD_HOTKEY:
513 * Construct the hotkey as a bitmask as illustrated below.
514 * Note that whenever a key was pressed, the respecting bit
515 * toggles and nothing else changes.
516 * +--+--+-+-+-+-+-+-+-+-+-+-+
517 * |11|10|9|8|7|6|5|4|3|2|1|0|
518 * +--+--+-+-+-+-+-+-+-+-+-+-+
519 * | | | | | | | | | | | |
520 * | | | | | | | | | | | +- Home Button
521 * | | | | | | | | | | +--- Search Button
522 * | | | | | | | | | +----- Mail Button
523 * | | | | | | | | +------- Thinkpad Button
524 * | | | | | | | +--------- Zoom (Fn + Space)
525 * | | | | | | +----------- WLAN Button
526 * | | | | | +------------- Video Button
527 * | | | | +--------------- Hibernate Button
528 * | | | +----------------- Thinklight Button
529 * | | +------------------- Screen expand (Fn + F8)
530 * | +--------------------- Brightness
531 * +------------------------ Volume/Mute
533 key = rtcin(IBM_RTC_HOTKEY1);
534 val = (IBM_RTC_MASK_HOME | IBM_RTC_MASK_SEARCH | IBM_RTC_MASK_MAIL | IBM_RTC_MASK_WLAN) & key;
535 key = rtcin(IBM_RTC_HOTKEY2);
536 val |= (IBM_RTC_MASK_THINKPAD | IBM_RTC_MASK_VIDEO | IBM_RTC_MASK_HIBERNATE) & key;
537 val |= (IBM_RTC_MASK_ZOOM & key) >> 1;
538 key = rtcin(IBM_RTC_THINKLIGHT);
539 val |= (IBM_RTC_MASK_THINKLIGHT & key) << 4;
540 key = rtcin(IBM_RTC_SCREENEXPAND);
541 val |= (IBM_RTC_MASK_THINKLIGHT & key) << 4;
542 key = rtcin(IBM_RTC_BRIGHTNESS);
543 val |= (IBM_RTC_MASK_BRIGHTNESS & key) << 5;
544 key = rtcin(IBM_RTC_VOLUME);
545 val |= (IBM_RTC_MASK_VOLUME & key) << 4;
548 case ACPI_IBM_METHOD_BRIGHTNESS:
549 ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS, &val_ec, 1);
550 val = val_ec & IBM_EC_MASK_BRI;
553 case ACPI_IBM_METHOD_VOLUME:
554 ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
555 val = val_ec & IBM_EC_MASK_VOL;
558 case ACPI_IBM_METHOD_MUTE:
559 ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
560 val = ((val_ec & IBM_EC_MASK_MUTE) == IBM_EC_MASK_MUTE);
563 case ACPI_IBM_METHOD_THINKLIGHT:
564 if (sc->light_get_supported)
565 acpi_GetInteger(sc->ec_handle, IBM_NAME_KEYLIGHT, &val);
570 case ACPI_IBM_METHOD_BLUETOOTH:
571 acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &val);
572 sc->wlan_bt_flags = val;
573 val = ((val & IBM_NAME_MASK_BT) != 0);
576 case ACPI_IBM_METHOD_WLAN:
577 acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &val);
578 sc->wlan_bt_flags = val;
579 val = ((val & IBM_NAME_MASK_WLAN) != 0);
582 case ACPI_IBM_METHOD_FANSPEED:
583 if (sc->fan_handle) {
584 if(ACPI_FAILURE(acpi_GetInteger(sc->fan_handle, NULL, &val)))
588 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSPEED, &val_ec, 2);
593 case ACPI_IBM_METHOD_FANSTATUS:
594 if (!sc->fan_handle) {
595 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
596 val = (val_ec & IBM_EC_MASK_FANSTATUS) == IBM_EC_MASK_FANSTATUS;
607 acpi_ibm_sysctl_set(struct acpi_ibm_softc *sc, int method, int arg)
612 ACPI_OBJECT_LIST Args;
615 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
616 ACPI_SERIAL_ASSERT(ibm);
619 case ACPI_IBM_METHOD_EVENTS:
620 if (arg < 0 || arg > 1)
623 status = acpi_SetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_SET, arg);
624 if (ACPI_FAILURE(status))
626 if (sc->events_mask_supported)
627 return acpi_ibm_eventmask_set(sc, sc->events_availmask);
630 case ACPI_IBM_METHOD_EVENTMASK:
631 if (sc->events_mask_supported)
632 return acpi_ibm_eventmask_set(sc, arg);
635 case ACPI_IBM_METHOD_BRIGHTNESS:
636 if (arg < 0 || arg > 7)
639 if (sc->cmos_handle) {
640 /* Read the current brightness */
641 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS, &val_ec, 1);
642 if (ACPI_FAILURE(status))
644 val = val_ec & IBM_EC_MASK_BRI;
648 Arg.Type = ACPI_TYPE_INTEGER;
649 Arg.Integer.Value = (arg > val) ? IBM_CMOS_BRIGHTNESS_UP : IBM_CMOS_BRIGHTNESS_DOWN;
651 step = (arg > val) ? 1 : -1;
652 for (int i = val; i != arg; i += step) {
653 status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
654 if (ACPI_FAILURE(status))
658 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_BRIGHTNESS, arg, 1);
661 case ACPI_IBM_METHOD_VOLUME:
662 if (arg < 0 || arg > 14)
665 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
666 if (ACPI_FAILURE(status))
669 if (sc->cmos_handle) {
670 val = val_ec & IBM_EC_MASK_VOL;
674 Arg.Type = ACPI_TYPE_INTEGER;
675 Arg.Integer.Value = (arg > val) ? IBM_CMOS_VOLUME_UP : IBM_CMOS_VOLUME_DOWN;
677 step = (arg > val) ? 1 : -1;
678 for (int i = val; i != arg; i += step) {
679 status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
680 if (ACPI_FAILURE(status))
684 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME, arg + (val_ec & (~IBM_EC_MASK_VOL)), 1);
687 case ACPI_IBM_METHOD_MUTE:
688 if (arg < 0 || arg > 1)
691 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
692 if (ACPI_FAILURE(status))
695 if (sc->cmos_handle) {
696 val = val_ec & IBM_EC_MASK_VOL;
700 Arg.Type = ACPI_TYPE_INTEGER;
701 Arg.Integer.Value = IBM_CMOS_VOLUME_MUTE;
703 status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
704 if (ACPI_FAILURE(status))
707 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);
710 case ACPI_IBM_METHOD_THINKLIGHT:
711 if (arg < 0 || arg > 1)
714 if (sc->light_set_supported) {
717 Arg.Type = ACPI_TYPE_INTEGER;
718 Arg.Integer.Value = arg ? sc->light_cmd_on : sc->light_cmd_off;
720 status = AcpiEvaluateObject(sc->light_handle, NULL, &Args, NULL);
721 if (ACPI_SUCCESS(status))
727 case ACPI_IBM_METHOD_BLUETOOTH:
728 if (arg < 0 || arg > 1)
731 val = (arg == 1) ? sc->wlan_bt_flags | IBM_NAME_MASK_BT : sc->wlan_bt_flags & (~IBM_NAME_MASK_BT);
732 return acpi_SetInteger(sc->handle, IBM_NAME_WLAN_BT_SET, val);
740 acpi_ibm_sysctl_init(struct acpi_ibm_softc *sc, int method)
743 ACPI_OBJECT_TYPE cmos_t;
744 ACPI_HANDLE ledb_handle;
747 case ACPI_IBM_METHOD_EVENTS:
748 /* Events are disabled by default */
751 case ACPI_IBM_METHOD_EVENTMASK:
752 return (sc->events_mask_supported);
754 case ACPI_IBM_METHOD_HOTKEY:
755 case ACPI_IBM_METHOD_BRIGHTNESS:
756 case ACPI_IBM_METHOD_VOLUME:
757 case ACPI_IBM_METHOD_MUTE:
758 /* EC is required here, which was aready checked before */
761 case ACPI_IBM_METHOD_THINKLIGHT:
762 sc->cmos_handle = NULL;
763 sc->light_get_supported = ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle, IBM_NAME_KEYLIGHT, &dummy));
765 if ((ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\UCMS", &sc->light_handle)) ||
766 ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\CMOS", &sc->light_handle)) ||
767 ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\CMS", &sc->light_handle))) &&
768 ACPI_SUCCESS(AcpiGetType(sc->light_handle, &cmos_t)) &&
769 cmos_t == ACPI_TYPE_METHOD) {
770 sc->light_cmd_on = 0x0c;
771 sc->light_cmd_off = 0x0d;
772 sc->cmos_handle = sc->light_handle;
774 else if (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\LGHT", &sc->light_handle))) {
775 sc->light_cmd_on = 1;
776 sc->light_cmd_off = 0;
779 sc->light_handle = NULL;
781 sc->light_set_supported = (sc->light_handle &&
782 ACPI_FAILURE(AcpiGetHandle(sc->ec_handle, "LEDB", &ledb_handle)));
784 if (sc->light_get_supported || sc->light_set_supported) {
791 case ACPI_IBM_METHOD_BLUETOOTH:
792 case ACPI_IBM_METHOD_WLAN:
793 if (ACPI_SUCCESS(acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &dummy)))
797 case ACPI_IBM_METHOD_FANSPEED:
799 * Some models report the fan speed in levels from 0-7
800 * Newer models report it contiguously
803 (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "GFAN", &sc->fan_handle)) ||
804 ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\FSPD", &sc->fan_handle)));
807 case ACPI_IBM_METHOD_FANSTATUS:
809 * Fan status is only supported on those models,
810 * which report fan RPM contiguously, not in levels
816 case ACPI_IBM_METHOD_THERMAL:
817 if (ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle, IBM_NAME_THERMAL_GET, &dummy))) {
818 sc->thermal_updt_supported = ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle, IBM_NAME_THERMAL_UPDT, &dummy));
827 acpi_ibm_thermal_sysctl(SYSCTL_HANDLER_ARGS)
829 struct acpi_ibm_softc *sc;
831 char temp_cmd[] = "TMP0";
834 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
836 sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
838 ACPI_SERIAL_BEGIN(ibm);
840 for (int i = 0; i < 8; ++i) {
841 temp_cmd[3] = '0' + i;
844 * The TMPx methods seem to return +/- 128 or 0
845 * when the respecting sensor is not available
847 if (ACPI_FAILURE(acpi_GetInteger(sc->ec_handle, temp_cmd,
848 &temp[i])) || ABS(temp[i]) == 128 || temp[i] == 0)
850 else if (sc->thermal_updt_supported)
851 /* Temperature is reported in tenth of Kelvin */
852 temp[i] = (temp[i] - 2732 + 5) / 10;
855 error = sysctl_handle_opaque(oidp, &temp, 8*sizeof(int), req);
857 ACPI_SERIAL_END(ibm);
862 acpi_ibm_notify(ACPI_HANDLE h, UINT32 notify, void *context)
864 int event, arg, type;
865 device_t dev = context;
866 struct acpi_ibm_softc *sc = device_get_softc(dev);
868 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
871 device_printf(dev, "Unknown notify\n");
874 acpi_GetInteger(acpi_get_handle(dev), IBM_NAME_EVENTS_GET, &event);
880 type = (event >> 12) & 0xf;
884 if (!(sc->events_availmask & (1 << (arg - 1)))) {
885 device_printf(dev, "Unknown key %d\n", arg);
890 acpi_UserNotify("IBM", h, (arg & 0xff));