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 <sys/sensors.h>
45 #include <machine/cpufunc.h>
46 #include <machine/clock.h>
52 #include <sys/module.h>
53 #include <dev/acpica5/acpivar.h>
54 #if defined(__FreeBSD__)
55 #include <dev/led/led.h>
57 #include <sys/sysctl.h>
58 #include <bus/isa/rtc.h>
60 #define _COMPONENT ACPI_OEM
61 ACPI_MODULE_NAME("IBM")
63 /* Internal methods */
64 #define ACPI_IBM_METHOD_EVENTS 1
65 #define ACPI_IBM_METHOD_EVENTMASK 2
66 #define ACPI_IBM_METHOD_HOTKEY 3
67 #define ACPI_IBM_METHOD_BRIGHTNESS 4
68 #define ACPI_IBM_METHOD_VOLUME 5
69 #define ACPI_IBM_METHOD_MUTE 6
70 #define ACPI_IBM_METHOD_THINKLIGHT 7
71 #define ACPI_IBM_METHOD_BLUETOOTH 8
72 #define ACPI_IBM_METHOD_WLAN 9
73 #define ACPI_IBM_METHOD_FANSPEED 10
74 #define ACPI_IBM_METHOD_FANLEVEL 11
75 #define ACPI_IBM_METHOD_FANSTATUS 12
76 #define ACPI_IBM_METHOD_THERMAL 13
79 #define IBM_RTC_HOTKEY1 0x64
80 #define IBM_RTC_MASK_HOME (1 << 0)
81 #define IBM_RTC_MASK_SEARCH (1 << 1)
82 #define IBM_RTC_MASK_MAIL (1 << 2)
83 #define IBM_RTC_MASK_WLAN (1 << 5)
84 #define IBM_RTC_HOTKEY2 0x65
85 #define IBM_RTC_MASK_THINKPAD (1 << 3)
86 #define IBM_RTC_MASK_ZOOM (1 << 5)
87 #define IBM_RTC_MASK_VIDEO (1 << 6)
88 #define IBM_RTC_MASK_HIBERNATE (1 << 7)
89 #define IBM_RTC_THINKLIGHT 0x66
90 #define IBM_RTC_MASK_THINKLIGHT (1 << 4)
91 #define IBM_RTC_SCREENEXPAND 0x67
92 #define IBM_RTC_MASK_SCREENEXPAND (1 << 5)
93 #define IBM_RTC_BRIGHTNESS 0x6c
94 #define IBM_RTC_MASK_BRIGHTNESS (1 << 5)
95 #define IBM_RTC_VOLUME 0x6e
96 #define IBM_RTC_MASK_VOLUME (1 << 7)
98 /* Embedded Controller registers */
99 #define IBM_EC_BRIGHTNESS 0x31
100 #define IBM_EC_MASK_BRI 0x7
101 #define IBM_EC_VOLUME 0x30
102 #define IBM_EC_MASK_VOL 0xf
103 #define IBM_EC_MASK_MUTE (1 << 6)
104 #define IBM_EC_FANSTATUS 0x2F
105 #define IBM_EC_MASK_FANLEVEL 0x3f
106 #define IBM_EC_MASK_FANDISENGAGED (1 << 6)
107 #define IBM_EC_MASK_FANSTATUS (1 << 7)
108 #define IBM_EC_FANSPEED 0x84
111 #define IBM_CMOS_VOLUME_DOWN 0
112 #define IBM_CMOS_VOLUME_UP 1
113 #define IBM_CMOS_VOLUME_MUTE 2
114 #define IBM_CMOS_BRIGHTNESS_UP 4
115 #define IBM_CMOS_BRIGHTNESS_DOWN 5
118 #define IBM_NAME_KEYLIGHT "KBLT"
119 #define IBM_NAME_WLAN_BT_GET "GBDC"
120 #define IBM_NAME_WLAN_BT_SET "SBDC"
121 #define IBM_NAME_MASK_BT (1 << 1)
122 #define IBM_NAME_MASK_WLAN (1 << 2)
123 #define IBM_NAME_THERMAL_GET "TMP7"
124 #define IBM_NAME_THERMAL_UPDT "UPDT"
126 #define IBM_NAME_EVENTS_STATUS_GET "DHKC"
127 #define IBM_NAME_EVENTS_MASK_GET "DHKN"
128 #define IBM_NAME_EVENTS_STATUS_SET "MHKC"
129 #define IBM_NAME_EVENTS_MASK_SET "MHKM"
130 #define IBM_NAME_EVENTS_GET "MHKP"
131 #define IBM_NAME_EVENTS_AVAILMASK "MHKA"
133 #define IBM_NUM_SENSORS 9
134 #define IBM_THERMAL_SENSORS 8
136 #define ABS(x) (((x) < 0)? -(x) : (x))
138 struct acpi_ibm_softc
{
142 /* Embedded controller */
144 ACPI_HANDLE ec_handle
;
147 ACPI_HANDLE cmos_handle
;
150 ACPI_HANDLE fan_handle
;
153 /* Keylight commands and states */
154 ACPI_HANDLE light_handle
;
158 int light_get_supported
;
159 int light_set_supported
;
161 /* led(4) interface */
162 struct cdev
*led_dev
;
167 int thermal_updt_supported
;
169 unsigned int events_availmask
;
170 unsigned int events_initialmask
;
171 int events_mask_supported
;
174 /* sensors(9) related */
175 struct ksensordev sensordev
;
176 struct ksensor sensors
[IBM_NUM_SENSORS
];
178 struct sysctl_ctx_list sysctl_ctx
;
179 struct sysctl_oid
*sysctl_tree
;
187 } acpi_ibm_sysctls
[] = {
190 .method
= ACPI_IBM_METHOD_EVENTS
,
191 .description
= "ACPI events enable",
192 .access
= CTLTYPE_INT
| CTLFLAG_RW
196 .method
= ACPI_IBM_METHOD_EVENTMASK
,
197 .description
= "ACPI eventmask",
198 .access
= CTLTYPE_INT
| CTLFLAG_RW
202 .method
= ACPI_IBM_METHOD_HOTKEY
,
203 .description
= "Key Status",
204 .access
= CTLTYPE_INT
| CTLFLAG_RD
207 .name
= "lcd_brightness",
208 .method
= ACPI_IBM_METHOD_BRIGHTNESS
,
209 .description
= "LCD Brightness",
210 .access
= CTLTYPE_INT
| CTLFLAG_RW
214 .method
= ACPI_IBM_METHOD_VOLUME
,
215 .description
= "Volume",
216 .access
= CTLTYPE_INT
| CTLFLAG_RW
220 .method
= ACPI_IBM_METHOD_MUTE
,
221 .description
= "Mute",
222 .access
= CTLTYPE_INT
| CTLFLAG_RW
225 .name
= "thinklight",
226 .method
= ACPI_IBM_METHOD_THINKLIGHT
,
227 .description
= "Thinklight enable",
228 .access
= CTLTYPE_INT
| CTLFLAG_RW
232 .method
= ACPI_IBM_METHOD_BLUETOOTH
,
233 .description
= "Bluetooth enable",
234 .access
= CTLTYPE_INT
| CTLFLAG_RW
238 .method
= ACPI_IBM_METHOD_WLAN
,
239 .description
= "WLAN enable",
240 .access
= CTLTYPE_INT
| CTLFLAG_RD
244 .method
= ACPI_IBM_METHOD_FANSPEED
,
245 .description
= "Fan speed",
246 .access
= CTLTYPE_INT
| CTLFLAG_RD
250 .method
= ACPI_IBM_METHOD_FANLEVEL
,
251 .description
= "Fan level",
252 .access
= CTLTYPE_INT
| CTLFLAG_RW
256 .method
= ACPI_IBM_METHOD_FANSTATUS
,
257 .description
= "Fan enable",
258 .access
= CTLTYPE_INT
| CTLFLAG_RW
264 ACPI_SERIAL_DECL(ibm
, "ACPI IBM extras");
266 static int acpi_ibm_probe(device_t dev
);
267 static int acpi_ibm_attach(device_t dev
);
268 static int acpi_ibm_detach(device_t dev
);
270 #if defined(__FreeBSD__)
271 static void ibm_led(void *softc
, int onoff
);
272 static void ibm_led_task(struct acpi_ibm_softc
*sc
, int pending __unused
);
275 static int acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS
);
276 static int acpi_ibm_sysctl_init(struct acpi_ibm_softc
*sc
, int method
);
277 static int acpi_ibm_sysctl_get(struct acpi_ibm_softc
*sc
, int method
);
278 static int acpi_ibm_sysctl_set(struct acpi_ibm_softc
*sc
, int method
, int val
);
280 static int acpi_ibm_eventmask_set(struct acpi_ibm_softc
*sc
, int val
);
281 static void acpi_ibm_notify(ACPI_HANDLE h
, UINT32 notify
, void *context
);
283 static void acpi_ibm_refresh(void *);
285 static device_method_t acpi_ibm_methods
[] = {
286 /* Device interface */
287 DEVMETHOD(device_probe
, acpi_ibm_probe
),
288 DEVMETHOD(device_attach
, acpi_ibm_attach
),
289 DEVMETHOD(device_detach
, acpi_ibm_detach
),
294 static driver_t acpi_ibm_driver
= {
297 sizeof(struct acpi_ibm_softc
),
300 static devclass_t acpi_ibm_devclass
;
302 DRIVER_MODULE(acpi_ibm
, acpi
, acpi_ibm_driver
, acpi_ibm_devclass
,
304 MODULE_DEPEND(acpi_ibm
, acpi
, 1, 1, 1);
305 static char *ibm_ids
[] = {"IBM0057", "IBM0068", NULL
};
307 #if defined(__FreeBSD__)
309 ibm_led(void *softc
, int onoff
)
311 struct acpi_ibm_softc
* sc
= (struct acpi_ibm_softc
*) softc
;
313 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__
);
319 sc
->led_state
= onoff
;
321 AcpiOsExecute(OSL_NOTIFY_HANDLER
, (void *)ibm_led_task
, sc
);
325 ibm_led_task(struct acpi_ibm_softc
*sc
, int pending __unused
)
327 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__
);
329 ACPI_SERIAL_BEGIN(ibm
);
330 acpi_ibm_sysctl_set(sc
, ACPI_IBM_METHOD_THINKLIGHT
, sc
->led_state
);
331 ACPI_SERIAL_END(ibm
);
338 acpi_ibm_probe(device_t dev
)
340 if (acpi_disabled("ibm") ||
341 ACPI_ID_PROBE(device_get_parent(dev
), dev
, ibm_ids
) == NULL
||
342 device_get_unit(dev
) != 0)
345 device_set_desc(dev
, "IBM ThinkPad ACPI Extras");
351 acpi_ibm_attach(device_t dev
)
353 struct acpi_ibm_softc
*sc
;
354 devclass_t ec_devclass
;
355 struct acpi_softc
*acpi_sc
;
358 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__
);
360 sc
= device_get_softc(dev
);
362 sc
->handle
= acpi_get_handle(dev
);
364 /* Look for the first embedded controller */
365 if (!(ec_devclass
= devclass_find ("acpi_ec"))) {
367 device_printf(dev
, "Couldn't find acpi_ec devclass\n");
370 if (!(sc
->ec_dev
= devclass_get_device(ec_devclass
, 0))) {
372 device_printf(dev
, "Couldn't find acpi_ec device\n");
375 sc
->ec_handle
= acpi_get_handle(sc
->ec_dev
);
376 acpi_sc
= acpi_device_get_parent_softc(dev
);
378 ACPI_SERIAL_BEGIN(ibm
);
380 /* Get the sysctl tree */
381 sysctl_ctx_init(&sc
->sysctl_ctx
);
382 sc
->sysctl_tree
= SYSCTL_ADD_NODE(&sc
->sysctl_ctx
,
383 SYSCTL_CHILDREN(acpi_sc
->acpi_sysctl_tree
), OID_AUTO
,
384 "thinkpad", CTLFLAG_RD
, 0, "");
386 /* Look for event mask and hook up the nodes */
387 sc
->events_mask_supported
= ACPI_SUCCESS(acpi_GetInteger(sc
->handle
,
388 IBM_NAME_EVENTS_MASK_GET
, &sc
->events_initialmask
));
390 if (sc
->events_mask_supported
) {
391 SYSCTL_ADD_INT(&sc
->sysctl_ctx
,
392 SYSCTL_CHILDREN(sc
->sysctl_tree
), OID_AUTO
,
393 "initialmask", CTLFLAG_RD
,
394 &sc
->events_initialmask
, 0, "Initial eventmask");
396 /* The availmask is the bitmask of supported events */
397 if (ACPI_FAILURE(acpi_GetInteger(sc
->handle
,
398 IBM_NAME_EVENTS_AVAILMASK
, &sc
->events_availmask
)))
399 sc
->events_availmask
= 0xffffffff;
401 SYSCTL_ADD_INT(&sc
->sysctl_ctx
,
402 SYSCTL_CHILDREN(sc
->sysctl_tree
), OID_AUTO
,
403 "availmask", CTLFLAG_RD
,
404 &sc
->events_availmask
, 0, "Mask of supported events");
407 /* Hook up proc nodes */
408 for (int i
= 0; acpi_ibm_sysctls
[i
].name
!= NULL
; i
++) {
409 if (!acpi_ibm_sysctl_init(sc
, acpi_ibm_sysctls
[i
].method
))
412 SYSCTL_ADD_PROC(&sc
->sysctl_ctx
,
413 SYSCTL_CHILDREN(sc
->sysctl_tree
), OID_AUTO
,
414 acpi_ibm_sysctls
[i
].name
, acpi_ibm_sysctls
[i
].access
,
415 sc
, i
, acpi_ibm_sysctl
, "I",
416 acpi_ibm_sysctls
[i
].description
);
418 ACPI_SERIAL_END(ibm
);
420 /* Handle notifies */
421 AcpiInstallNotifyHandler(sc
->handle
, ACPI_DEVICE_NOTIFY
,
422 acpi_ibm_notify
, dev
);
424 /* Attach sensors(9). */
425 if (sensor_task_register(sc
, acpi_ibm_refresh
, 5)) {
426 device_printf(sc
->dev
, "unable to register update task\n");
429 strlcpy(sc
->sensordev
.xname
, device_get_nameunit(sc
->dev
),
430 sizeof(sc
->sensordev
.xname
));
432 for (i
= 0; i
< IBM_THERMAL_SENSORS
; i
++) {
433 sc
->sensors
[i
].type
= SENSOR_TEMP
;
434 sensor_attach(&sc
->sensordev
, &sc
->sensors
[i
]);
437 sc
->sensors
[i
].type
= SENSOR_FANRPM
;
438 sensor_attach(&sc
->sensordev
, &sc
->sensors
[i
]);
440 sensordev_install(&sc
->sensordev
);
442 #if defined(__FreeBSD__)
443 /* Hook up light to led(4) */
444 if (sc
->light_set_supported
)
445 sc
->led_dev
= led_create_state(ibm_led
, sc
, "thinklight", sc
->light_val
);
452 acpi_ibm_detach(device_t dev
)
455 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__
);
457 struct acpi_ibm_softc
*sc
= device_get_softc(dev
);
459 /* Disable events and restore eventmask */
460 ACPI_SERIAL_BEGIN(ibm
);
461 acpi_ibm_sysctl_set(sc
, ACPI_IBM_METHOD_EVENTS
, 0);
462 acpi_ibm_sysctl_set(sc
, ACPI_IBM_METHOD_EVENTMASK
, sc
->events_initialmask
);
463 ACPI_SERIAL_END(ibm
);
465 AcpiRemoveNotifyHandler(sc
->handle
, ACPI_DEVICE_NOTIFY
, acpi_ibm_notify
);
467 sensordev_deinstall(&sc
->sensordev
);
468 for (i
= 0; i
< IBM_THERMAL_SENSORS
; i
++)
469 sensor_detach(&sc
->sensordev
, &sc
->sensors
[i
]);
470 sensor_task_unregister(sc
);
472 #if defined(__FreeBSD__)
473 if (sc
->led_dev
!= NULL
)
474 led_destroy(sc
->led_dev
);
481 acpi_ibm_eventmask_set(struct acpi_ibm_softc
*sc
, int val
)
484 ACPI_OBJECT_LIST args
;
487 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__
);
488 ACPI_SERIAL_ASSERT(ibm
);
492 arg
[0].Type
= ACPI_TYPE_INTEGER
;
493 arg
[1].Type
= ACPI_TYPE_INTEGER
;
495 for (int i
= 0; i
< 32; ++i
) {
496 arg
[0].Integer
.Value
= i
+1;
497 arg
[1].Integer
.Value
= (((1 << i
) & val
) != 0);
498 status
= AcpiEvaluateObject(sc
->handle
,
499 IBM_NAME_EVENTS_MASK_SET
, &args
, NULL
);
501 if (ACPI_FAILURE(status
))
509 acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS
)
511 struct acpi_ibm_softc
*sc
;
517 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__
);
519 sc
= (struct acpi_ibm_softc
*)oidp
->oid_arg1
;
520 function
= oidp
->oid_arg2
;
521 method
= acpi_ibm_sysctls
[function
].method
;
523 ACPI_SERIAL_BEGIN(ibm
);
524 arg
= acpi_ibm_sysctl_get(sc
, method
);
525 error
= sysctl_handle_int(oidp
, &arg
, 0, req
);
528 if (error
!= 0 || req
->newptr
== NULL
)
532 error
= acpi_ibm_sysctl_set(sc
, method
, arg
);
535 ACPI_SERIAL_END(ibm
);
540 acpi_ibm_sysctl_get(struct acpi_ibm_softc
*sc
, int method
)
545 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__
);
546 ACPI_SERIAL_ASSERT(ibm
);
549 case ACPI_IBM_METHOD_EVENTS
:
550 acpi_GetInteger(sc
->handle
, IBM_NAME_EVENTS_STATUS_GET
, &val
);
553 case ACPI_IBM_METHOD_EVENTMASK
:
554 if (sc
->events_mask_supported
)
555 acpi_GetInteger(sc
->handle
, IBM_NAME_EVENTS_MASK_GET
, &val
);
558 case ACPI_IBM_METHOD_HOTKEY
:
560 * Construct the hotkey as a bitmask as illustrated below.
561 * Note that whenever a key was pressed, the respecting bit
562 * toggles and nothing else changes.
563 * +--+--+-+-+-+-+-+-+-+-+-+-+
564 * |11|10|9|8|7|6|5|4|3|2|1|0|
565 * +--+--+-+-+-+-+-+-+-+-+-+-+
566 * | | | | | | | | | | | |
567 * | | | | | | | | | | | +- Home Button
568 * | | | | | | | | | | +--- Search Button
569 * | | | | | | | | | +----- Mail Button
570 * | | | | | | | | +------- Thinkpad Button
571 * | | | | | | | +--------- Zoom (Fn + Space)
572 * | | | | | | +----------- WLAN Button
573 * | | | | | +------------- Video Button
574 * | | | | +--------------- Hibernate Button
575 * | | | +----------------- Thinklight Button
576 * | | +------------------- Screen expand (Fn + F8)
577 * | +--------------------- Brightness
578 * +------------------------ Volume/Mute
580 key
= rtcin(IBM_RTC_HOTKEY1
);
581 val
= (IBM_RTC_MASK_HOME
| IBM_RTC_MASK_SEARCH
| IBM_RTC_MASK_MAIL
| IBM_RTC_MASK_WLAN
) & key
;
582 key
= rtcin(IBM_RTC_HOTKEY2
);
583 val
|= (IBM_RTC_MASK_THINKPAD
| IBM_RTC_MASK_VIDEO
| IBM_RTC_MASK_HIBERNATE
) & key
;
584 val
|= (IBM_RTC_MASK_ZOOM
& key
) >> 1;
585 key
= rtcin(IBM_RTC_THINKLIGHT
);
586 val
|= (IBM_RTC_MASK_THINKLIGHT
& key
) << 4;
587 key
= rtcin(IBM_RTC_SCREENEXPAND
);
588 val
|= (IBM_RTC_MASK_THINKLIGHT
& key
) << 4;
589 key
= rtcin(IBM_RTC_BRIGHTNESS
);
590 val
|= (IBM_RTC_MASK_BRIGHTNESS
& key
) << 5;
591 key
= rtcin(IBM_RTC_VOLUME
);
592 val
|= (IBM_RTC_MASK_VOLUME
& key
) << 4;
595 case ACPI_IBM_METHOD_BRIGHTNESS
:
596 ACPI_EC_READ(sc
->ec_dev
, IBM_EC_BRIGHTNESS
, &val_ec
, 1);
597 val
= val_ec
& IBM_EC_MASK_BRI
;
600 case ACPI_IBM_METHOD_VOLUME
:
601 ACPI_EC_READ(sc
->ec_dev
, IBM_EC_VOLUME
, &val_ec
, 1);
602 val
= val_ec
& IBM_EC_MASK_VOL
;
605 case ACPI_IBM_METHOD_MUTE
:
606 ACPI_EC_READ(sc
->ec_dev
, IBM_EC_VOLUME
, &val_ec
, 1);
607 val
= ((val_ec
& IBM_EC_MASK_MUTE
) == IBM_EC_MASK_MUTE
);
610 case ACPI_IBM_METHOD_THINKLIGHT
:
611 if (sc
->light_get_supported
)
612 acpi_GetInteger(sc
->ec_handle
, IBM_NAME_KEYLIGHT
, &val
);
617 case ACPI_IBM_METHOD_BLUETOOTH
:
618 acpi_GetInteger(sc
->handle
, IBM_NAME_WLAN_BT_GET
, &val
);
619 sc
->wlan_bt_flags
= val
;
620 val
= ((val
& IBM_NAME_MASK_BT
) != 0);
623 case ACPI_IBM_METHOD_WLAN
:
624 acpi_GetInteger(sc
->handle
, IBM_NAME_WLAN_BT_GET
, &val
);
625 sc
->wlan_bt_flags
= val
;
626 val
= ((val
& IBM_NAME_MASK_WLAN
) != 0);
629 case ACPI_IBM_METHOD_FANSPEED
:
630 if (sc
->fan_handle
) {
631 if(ACPI_FAILURE(acpi_GetInteger(sc
->fan_handle
, NULL
, &val
)))
635 ACPI_EC_READ(sc
->ec_dev
, IBM_EC_FANSPEED
, &val_ec
, 2);
640 case ACPI_IBM_METHOD_FANLEVEL
:
642 * The IBM_EC_FANSTATUS register works as follows:
643 * Bit 0-5 indicate the level at which the fan operates. Only
644 * values between 0 and 7 have an effect. Everything
645 * above 7 is treated the same as level 7
646 * Bit 6 overrides the fan speed limit if set to 1
647 * Bit 7 indicates at which mode the fan operates:
648 * manual (0) or automatic (1)
650 if (!sc
->fan_handle
) {
651 ACPI_EC_READ(sc
->ec_dev
, IBM_EC_FANSTATUS
, &val_ec
, 1);
652 val
= val_ec
& IBM_EC_MASK_FANLEVEL
;
656 case ACPI_IBM_METHOD_FANSTATUS
:
657 if (!sc
->fan_handle
) {
658 ACPI_EC_READ(sc
->ec_dev
, IBM_EC_FANSTATUS
, &val_ec
, 1);
659 val
= (val_ec
& IBM_EC_MASK_FANSTATUS
) == IBM_EC_MASK_FANSTATUS
;
670 acpi_ibm_sysctl_set(struct acpi_ibm_softc
*sc
, int method
, int arg
)
675 ACPI_OBJECT_LIST Args
;
678 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__
);
679 ACPI_SERIAL_ASSERT(ibm
);
682 case ACPI_IBM_METHOD_EVENTS
:
683 if (arg
< 0 || arg
> 1)
686 status
= acpi_SetInteger(sc
->handle
, IBM_NAME_EVENTS_STATUS_SET
, arg
);
687 if (ACPI_FAILURE(status
))
689 if (sc
->events_mask_supported
)
690 return acpi_ibm_eventmask_set(sc
, sc
->events_availmask
);
693 case ACPI_IBM_METHOD_EVENTMASK
:
694 if (sc
->events_mask_supported
)
695 return acpi_ibm_eventmask_set(sc
, arg
);
698 case ACPI_IBM_METHOD_BRIGHTNESS
:
699 if (arg
< 0 || arg
> 7)
702 if (sc
->cmos_handle
) {
703 /* Read the current brightness */
704 status
= ACPI_EC_READ(sc
->ec_dev
, IBM_EC_BRIGHTNESS
, &val_ec
, 1);
705 if (ACPI_FAILURE(status
))
707 val
= val_ec
& IBM_EC_MASK_BRI
;
711 Arg
.Type
= ACPI_TYPE_INTEGER
;
712 Arg
.Integer
.Value
= (arg
> val
) ? IBM_CMOS_BRIGHTNESS_UP
: IBM_CMOS_BRIGHTNESS_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_BRIGHTNESS
, arg
, 1);
724 case ACPI_IBM_METHOD_VOLUME
:
725 if (arg
< 0 || arg
> 14)
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
= (arg
> val
) ? IBM_CMOS_VOLUME_UP
: IBM_CMOS_VOLUME_DOWN
;
740 step
= (arg
> val
) ? 1 : -1;
741 for (int i
= val
; i
!= arg
; i
+= step
) {
742 status
= AcpiEvaluateObject(sc
->cmos_handle
, NULL
, &Args
, NULL
);
743 if (ACPI_FAILURE(status
))
747 return ACPI_EC_WRITE(sc
->ec_dev
, IBM_EC_VOLUME
, arg
+ (val_ec
& (~IBM_EC_MASK_VOL
)), 1);
750 case ACPI_IBM_METHOD_MUTE
:
751 if (arg
< 0 || arg
> 1)
754 status
= ACPI_EC_READ(sc
->ec_dev
, IBM_EC_VOLUME
, &val_ec
, 1);
755 if (ACPI_FAILURE(status
))
758 if (sc
->cmos_handle
) {
759 val
= val_ec
& IBM_EC_MASK_VOL
;
763 Arg
.Type
= ACPI_TYPE_INTEGER
;
764 Arg
.Integer
.Value
= IBM_CMOS_VOLUME_MUTE
;
766 status
= AcpiEvaluateObject(sc
->cmos_handle
, NULL
, &Args
, NULL
);
767 if (ACPI_FAILURE(status
))
770 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);
773 case ACPI_IBM_METHOD_THINKLIGHT
:
774 if (arg
< 0 || arg
> 1)
777 if (sc
->light_set_supported
) {
780 Arg
.Type
= ACPI_TYPE_INTEGER
;
781 Arg
.Integer
.Value
= arg
? sc
->light_cmd_on
: sc
->light_cmd_off
;
783 status
= AcpiEvaluateObject(sc
->light_handle
, NULL
, &Args
, NULL
);
784 if (ACPI_SUCCESS(status
))
790 case ACPI_IBM_METHOD_BLUETOOTH
:
791 if (arg
< 0 || arg
> 1)
794 val
= (arg
== 1) ? sc
->wlan_bt_flags
| IBM_NAME_MASK_BT
: sc
->wlan_bt_flags
& (~IBM_NAME_MASK_BT
);
795 return acpi_SetInteger(sc
->handle
, IBM_NAME_WLAN_BT_SET
, val
);
798 case ACPI_IBM_METHOD_FANLEVEL
:
799 if (arg
< 0 || arg
> 7)
802 if (!sc
->fan_handle
) {
803 /* Read the current fanstatus */
804 ACPI_EC_READ(sc
->ec_dev
, IBM_EC_FANSTATUS
, &val_ec
, 1);
805 val
= val_ec
& (~IBM_EC_MASK_FANLEVEL
);
807 return ACPI_EC_WRITE(sc
->ec_dev
, IBM_EC_FANSTATUS
, val
| arg
, 1);
811 case ACPI_IBM_METHOD_FANSTATUS
:
812 if (arg
< 0 || arg
> 1)
815 if (!sc
->fan_handle
) {
816 /* Read the current fanstatus */
817 ACPI_EC_READ(sc
->ec_dev
, IBM_EC_FANSTATUS
, &val_ec
, 1);
819 return ACPI_EC_WRITE(sc
->ec_dev
, IBM_EC_FANSTATUS
,
820 (arg
== 1) ? (val_ec
| IBM_EC_MASK_FANSTATUS
) : (val_ec
& (~IBM_EC_MASK_FANSTATUS
)), 1);
829 acpi_ibm_sysctl_init(struct acpi_ibm_softc
*sc
, int method
)
832 ACPI_OBJECT_TYPE cmos_t
;
833 ACPI_HANDLE ledb_handle
;
836 case ACPI_IBM_METHOD_EVENTS
:
837 /* Events are disabled by default */
840 case ACPI_IBM_METHOD_EVENTMASK
:
841 return (sc
->events_mask_supported
);
843 case ACPI_IBM_METHOD_HOTKEY
:
844 case ACPI_IBM_METHOD_BRIGHTNESS
:
845 case ACPI_IBM_METHOD_VOLUME
:
846 case ACPI_IBM_METHOD_MUTE
:
847 /* EC is required here, which was aready checked before */
850 case ACPI_IBM_METHOD_THINKLIGHT
:
851 sc
->cmos_handle
= NULL
;
852 sc
->light_get_supported
= ACPI_SUCCESS(acpi_GetInteger(
853 sc
->ec_handle
, IBM_NAME_KEYLIGHT
, &sc
->light_val
));
855 if ((ACPI_SUCCESS(AcpiGetHandle(sc
->handle
, "\\UCMS", &sc
->light_handle
)) ||
856 ACPI_SUCCESS(AcpiGetHandle(sc
->handle
, "\\CMOS", &sc
->light_handle
)) ||
857 ACPI_SUCCESS(AcpiGetHandle(sc
->handle
, "\\CMS", &sc
->light_handle
))) &&
858 ACPI_SUCCESS(AcpiGetType(sc
->light_handle
, &cmos_t
)) &&
859 cmos_t
== ACPI_TYPE_METHOD
) {
860 sc
->light_cmd_on
= 0x0c;
861 sc
->light_cmd_off
= 0x0d;
862 sc
->cmos_handle
= sc
->light_handle
;
864 else if (ACPI_SUCCESS(AcpiGetHandle(sc
->handle
, "\\LGHT", &sc
->light_handle
))) {
865 sc
->light_cmd_on
= 1;
866 sc
->light_cmd_off
= 0;
869 sc
->light_handle
= NULL
;
871 sc
->light_set_supported
= (sc
->light_handle
&&
872 ACPI_FAILURE(AcpiGetHandle(sc
->ec_handle
, "LEDB", &ledb_handle
)));
874 if (sc
->light_get_supported
)
877 if (sc
->light_set_supported
) {
884 case ACPI_IBM_METHOD_BLUETOOTH
:
885 case ACPI_IBM_METHOD_WLAN
:
886 if (ACPI_SUCCESS(acpi_GetInteger(sc
->handle
, IBM_NAME_WLAN_BT_GET
, &dummy
)))
890 case ACPI_IBM_METHOD_FANSPEED
:
892 * Some models report the fan speed in levels from 0-7
893 * Newer models report it contiguously
896 (ACPI_SUCCESS(AcpiGetHandle(sc
->handle
, "GFAN", &sc
->fan_handle
)) ||
897 ACPI_SUCCESS(AcpiGetHandle(sc
->handle
, "\\FSPD", &sc
->fan_handle
)));
900 case ACPI_IBM_METHOD_FANLEVEL
:
901 case ACPI_IBM_METHOD_FANSTATUS
:
903 * Fan status is only supported on those models,
904 * which report fan RPM contiguously, not in levels
910 case ACPI_IBM_METHOD_THERMAL
:
911 if (ACPI_SUCCESS(acpi_GetInteger(sc
->ec_handle
, IBM_NAME_THERMAL_GET
, &dummy
))) {
912 sc
->thermal_updt_supported
= ACPI_SUCCESS(acpi_GetInteger(sc
->ec_handle
, IBM_NAME_THERMAL_UPDT
, &dummy
));
921 acpi_ibm_refresh(void *arg
)
923 struct acpi_ibm_softc
*sc
;
924 char temp_cmd
[] = "TMP0";
925 int i
, data
, temp
[8];
928 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__
);
930 sc
= (struct acpi_ibm_softc
*)arg
;
932 ACPI_SERIAL_BEGIN(ibm
);
934 for (i
= 0; i
< IBM_THERMAL_SENSORS
; ++i
) {
935 temp_cmd
[3] = '0' + i
;
938 * The TMPx methods seem to return +/- 128 or 0
939 * when the respecting sensor is not available
941 if (ACPI_FAILURE(acpi_GetInteger(sc
->ec_handle
, temp_cmd
,
942 &temp
[i
])) || ABS(temp
[i
]) == 128 || temp
[i
] == 0) {
943 sc
->sensors
[i
].flags
|= SENSOR_FINVALID
;
946 else if (sc
->thermal_updt_supported
) {
947 /* Temperature is reported in tenth of Kelvin */
948 sc
->sensors
[i
].value
= data
* 100000;
950 sc
->sensors
[i
].value
= data
* 1000000 + 273150000;
952 sc
->sensors
[i
].flags
&= ~SENSOR_FINVALID
;
953 if (sc
->fan_handle
) {
954 if (ACPI_FAILURE(acpi_GetInteger(sc
->fan_handle
,
956 sc
->sensors
[i
].flags
|= SENSOR_FINVALID
;
960 ACPI_EC_READ(sc
->ec_dev
, IBM_EC_FANSPEED
, &speed
, 2);
964 sc
->sensors
[i
].value
= data
;
966 ACPI_SERIAL_END(ibm
);
970 acpi_ibm_notify(ACPI_HANDLE h
, UINT32 notify
, void *context
)
972 int event
, arg
, type
;
973 device_t dev
= context
;
974 struct acpi_ibm_softc
*sc
= device_get_softc(dev
);
976 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__
, notify
);
979 device_printf(dev
, "Unknown notify\n");
982 acpi_GetInteger(acpi_get_handle(dev
), IBM_NAME_EVENTS_GET
, &event
);
988 type
= (event
>> 12) & 0xf;
992 if (!(sc
->events_availmask
& (1 << (arg
- 1)))) {
993 device_printf(dev
, "Unknown key %d\n", arg
);
998 acpi_UserNotify("IBM", h
, (arg
& 0xff));