9122e1179aeeaa22658125a58770b50d86a9d18c
[dragonfly.git] / sys / dev / acpica5 / acpi_thinkpad / acpi_thinkpad.c
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  * $FreeBSD: src/sys/dev/acpi_support/acpi_ibm.c,v 1.19 2009/06/05 18:44:36 jkim
27  */
28
29 #include <sys/cdefs.h>
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 <machine/clock.h>
46
47 #include "acpi.h"
48 #include "accommon.h"
49
50 #include "acpi_if.h"
51 #include <sys/module.h>
52 #include <dev/acpica5/acpivar.h>
53 #if defined(__FreeBSD__)
54 #include <dev/led/led.h>
55 #endif
56 #include <sys/sysctl.h>
57 #include <bus/isa/rtc.h>
58
59 #define _COMPONENT      ACPI_OEM
60 ACPI_MODULE_NAME("IBM")
61
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
76
77 /* Hotkeys/Buttons */
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)
96
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
108
109 /* CMOS Commands */
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
115
116 /* ACPI methods */
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"
124
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"
131
132 #define ABS(x) (((x) < 0)? -(x) : (x))
133
134 struct acpi_ibm_softc {
135         device_t        dev;
136         ACPI_HANDLE     handle;
137
138         /* Embedded controller */
139         device_t        ec_dev;
140         ACPI_HANDLE     ec_handle;
141
142         /* CMOS */
143         ACPI_HANDLE     cmos_handle;
144
145         /* Fan status */
146         ACPI_HANDLE     fan_handle;
147         int             fan_levels;
148
149         /* Keylight commands and states */
150         ACPI_HANDLE     light_handle;
151         int             light_cmd_on;
152         int             light_cmd_off;
153         int             light_val;
154         int             light_get_supported;
155         int             light_set_supported;
156
157         /* led(4) interface */
158         struct cdev     *led_dev;
159         int             led_busy;
160         int             led_state;
161
162         int             wlan_bt_flags;
163         int             thermal_updt_supported;
164
165         unsigned int    events_availmask;
166         unsigned int    events_initialmask;
167         int             events_mask_supported;
168         int             events_enable;
169
170         struct sysctl_ctx_list  sysctl_ctx;
171         struct sysctl_oid       *sysctl_tree;
172 };
173
174 static struct {
175         char    *name;
176         int     method;
177         char    *description;
178         int     access;
179 } acpi_ibm_sysctls[] = {
180         {
181                 .name           = "events",
182                 .method         = ACPI_IBM_METHOD_EVENTS,
183                 .description    = "ACPI events enable",
184                 .access         = CTLTYPE_INT | CTLFLAG_RW
185         },
186         {
187                 .name           = "eventmask",
188                 .method         = ACPI_IBM_METHOD_EVENTMASK,
189                 .description    = "ACPI eventmask",
190                 .access         = CTLTYPE_INT | CTLFLAG_RW
191         },
192         {
193                 .name           = "hotkey",
194                 .method         = ACPI_IBM_METHOD_HOTKEY,
195                 .description    = "Key Status",
196                 .access         = CTLTYPE_INT | CTLFLAG_RD
197         },
198         {
199                 .name           = "lcd_brightness",
200                 .method         = ACPI_IBM_METHOD_BRIGHTNESS,
201                 .description    = "LCD Brightness",
202                 .access         = CTLTYPE_INT | CTLFLAG_RW
203         },
204         {
205                 .name           = "volume",
206                 .method         = ACPI_IBM_METHOD_VOLUME,
207                 .description    = "Volume",
208                 .access         = CTLTYPE_INT | CTLFLAG_RW
209         },
210         {
211                 .name           = "mute",
212                 .method         = ACPI_IBM_METHOD_MUTE,
213                 .description    = "Mute",
214                 .access         = CTLTYPE_INT | CTLFLAG_RW
215         },
216         {
217                 .name           = "thinklight",
218                 .method         = ACPI_IBM_METHOD_THINKLIGHT,
219                 .description    = "Thinklight enable",
220                 .access         = CTLTYPE_INT | CTLFLAG_RW
221         },
222         {
223                 .name           = "bluetooth",
224                 .method         = ACPI_IBM_METHOD_BLUETOOTH,
225                 .description    = "Bluetooth enable",
226                 .access         = CTLTYPE_INT | CTLFLAG_RW
227         },
228         {
229                 .name           = "wlan",
230                 .method         = ACPI_IBM_METHOD_WLAN,
231                 .description    = "WLAN enable",
232                 .access         = CTLTYPE_INT | CTLFLAG_RD
233         },
234         {
235                 .name           = "fan_speed",
236                 .method         = ACPI_IBM_METHOD_FANSPEED,
237                 .description    = "Fan speed",
238                 .access         = CTLTYPE_INT | CTLFLAG_RD
239         },
240         {
241                 .name           = "fan_level",
242                 .method         = ACPI_IBM_METHOD_FANLEVEL,
243                 .description    = "Fan level",
244                 .access         = CTLTYPE_INT | CTLFLAG_RW
245         },
246         {
247                 .name           = "fan",
248                 .method         = ACPI_IBM_METHOD_FANSTATUS,
249                 .description    = "Fan enable",
250                 .access         = CTLTYPE_INT | CTLFLAG_RW
251         },
252
253         { NULL, 0, NULL, 0 }
254 };
255
256 ACPI_SERIAL_DECL(ibm, "ACPI IBM extras");
257
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);
261
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);
265 #endif
266
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);
271
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);
275
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),
281
282         {0, 0}
283 };
284
285 static driver_t acpi_ibm_driver = {
286         "acpi_ibm",
287         acpi_ibm_methods,
288         sizeof(struct acpi_ibm_softc),
289 };
290
291 static devclass_t acpi_ibm_devclass;
292
293 DRIVER_MODULE(acpi_ibm, acpi, acpi_ibm_driver, acpi_ibm_devclass,
294               0, 0);
295 MODULE_DEPEND(acpi_ibm, acpi, 1, 1, 1);
296 static char    *ibm_ids[] = {"IBM0057", "IBM0068", NULL};
297
298 #if defined(__FreeBSD__)
299 static void
300 ibm_led(void *softc, int onoff)
301 {
302         struct acpi_ibm_softc* sc = (struct acpi_ibm_softc*) softc;
303
304         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
305
306         if (sc->led_busy)
307                 return;
308
309         sc->led_busy = 1;
310         sc->led_state = onoff;
311
312         AcpiOsExecute(OSL_NOTIFY_HANDLER, (void *)ibm_led_task, sc);
313 }
314
315 static void
316 ibm_led_task(struct acpi_ibm_softc *sc, int pending __unused)
317 {
318         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
319
320         ACPI_SERIAL_BEGIN(ibm);
321         acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_THINKLIGHT, sc->led_state);
322         ACPI_SERIAL_END(ibm);
323
324         sc->led_busy = 0;
325 }
326 #endif
327
328 static int
329 acpi_ibm_probe(device_t dev)
330 {
331         if (acpi_disabled("ibm") ||
332             ACPI_ID_PROBE(device_get_parent(dev), dev, ibm_ids) == NULL ||
333             device_get_unit(dev) != 0)
334                 return (ENXIO);
335
336         device_set_desc(dev, "IBM ThinkPad ACPI Extras");
337
338         return (0);
339 }
340
341 static int
342 acpi_ibm_attach(device_t dev)
343 {
344         struct acpi_ibm_softc   *sc;
345         devclass_t              ec_devclass;
346         struct acpi_softc       *acpi_sc;
347
348         ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
349
350         sc = device_get_softc(dev);
351         sc->dev = dev;
352         sc->handle = acpi_get_handle(dev);
353
354         /* Look for the first embedded controller */
355         if (!(ec_devclass = devclass_find ("acpi_ec"))) {
356                 if (bootverbose)
357                         device_printf(dev, "Couldn't find acpi_ec devclass\n");
358                 return (EINVAL);
359         }
360         if (!(sc->ec_dev = devclass_get_device(ec_devclass, 0))) {
361                 if (bootverbose)
362                         device_printf(dev, "Couldn't find acpi_ec device\n");
363                 return (EINVAL);
364         }
365         sc->ec_handle = acpi_get_handle(sc->ec_dev);
366         acpi_sc = acpi_device_get_parent_softc(dev);
367
368         ACPI_SERIAL_BEGIN(ibm);
369
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, "");
375
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));
379
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");
385
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;
390
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");
395         }
396
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))
400                         continue;
401
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);
407         }
408
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",
415                     "Thermal zones");
416         }
417
418         ACPI_SERIAL_END(ibm);
419
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);
427 #endif
428
429         return (0);
430 }
431
432 static int
433 acpi_ibm_detach(device_t dev)
434 {
435         ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
436
437         struct acpi_ibm_softc *sc = device_get_softc(dev);
438
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);
444
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);
449 #endif
450
451         return (0);
452 }
453
454 static int
455 acpi_ibm_eventmask_set(struct acpi_ibm_softc *sc, int val)
456 {
457         ACPI_OBJECT             arg[2];
458         ACPI_OBJECT_LIST        args;
459         ACPI_STATUS             status;
460
461         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
462         ACPI_SERIAL_ASSERT(ibm);
463
464         args.Count = 2;
465         args.Pointer = arg;
466         arg[0].Type = ACPI_TYPE_INTEGER;
467         arg[1].Type = ACPI_TYPE_INTEGER;
468
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);
474
475                 if (ACPI_FAILURE(status))
476                         return (status);
477         }
478
479         return (0);
480 }
481
482 static int
483 acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS)
484 {
485         struct acpi_ibm_softc   *sc;
486         int                     arg;
487         int                     error = 0;
488         int                     function;
489         int                     method;
490         
491         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
492
493         sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
494         function = oidp->oid_arg2;
495         method = acpi_ibm_sysctls[function].method;
496
497         ACPI_SERIAL_BEGIN(ibm);
498         arg = acpi_ibm_sysctl_get(sc, method);
499         error = sysctl_handle_int(oidp, &arg, 0, req);
500
501         /* Sanity check */
502         if (error != 0 || req->newptr == NULL)
503                 goto out;
504
505         /* Update */
506         error = acpi_ibm_sysctl_set(sc, method, arg);
507
508 out:
509         ACPI_SERIAL_END(ibm);
510         return (error);
511 }
512
513 static int
514 acpi_ibm_sysctl_get(struct acpi_ibm_softc *sc, int method)
515 {
516         ACPI_INTEGER    val_ec;
517         int             val = 0, key;
518
519         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
520         ACPI_SERIAL_ASSERT(ibm);
521
522         switch (method) {
523         case ACPI_IBM_METHOD_EVENTS:
524                 acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_GET, &val);
525                 break;
526
527         case ACPI_IBM_METHOD_EVENTMASK:
528                 if (sc->events_mask_supported)
529                         acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_MASK_GET, &val);
530                 break;
531
532         case ACPI_IBM_METHOD_HOTKEY:
533                 /*
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
553                  */
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;
567                 break;
568
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;
572                 break;
573
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;
577                 break;
578
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);
582                 break;
583
584         case ACPI_IBM_METHOD_THINKLIGHT:
585                 if (sc->light_get_supported)
586                         acpi_GetInteger(sc->ec_handle, IBM_NAME_KEYLIGHT, &val);
587                 else
588                         val = sc->light_val;
589                 break;
590
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);
595                 break;
596
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);
601                 break;
602
603         case ACPI_IBM_METHOD_FANSPEED:
604                 if (sc->fan_handle) {
605                         if(ACPI_FAILURE(acpi_GetInteger(sc->fan_handle, NULL, &val)))
606                                 val = -1;
607                 }
608                 else {
609                         ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSPEED, &val_ec, 2);
610                         val = val_ec;
611                 }
612                 break;
613
614         case ACPI_IBM_METHOD_FANLEVEL:
615                 /*
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)
623                  */
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;
627                 }
628                 break;
629
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;
634                 }
635                 else
636                         val = -1;
637                 break;
638         }
639
640         return (val);
641 }
642
643 static int
644 acpi_ibm_sysctl_set(struct acpi_ibm_softc *sc, int method, int arg)
645 {
646         int                     val, step;
647         ACPI_INTEGER            val_ec;
648         ACPI_OBJECT             Arg;
649         ACPI_OBJECT_LIST        Args;
650         ACPI_STATUS             status;
651
652         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
653         ACPI_SERIAL_ASSERT(ibm);
654
655         switch (method) {
656         case ACPI_IBM_METHOD_EVENTS:
657                 if (arg < 0 || arg > 1)
658                         return (EINVAL);
659
660                 status = acpi_SetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_SET, arg);
661                 if (ACPI_FAILURE(status))
662                         return (status);
663                 if (sc->events_mask_supported)
664                         return acpi_ibm_eventmask_set(sc, sc->events_availmask);
665                 break;
666
667         case ACPI_IBM_METHOD_EVENTMASK:
668                 if (sc->events_mask_supported)
669                         return acpi_ibm_eventmask_set(sc, arg);
670                 break;
671
672         case ACPI_IBM_METHOD_BRIGHTNESS:
673                 if (arg < 0 || arg > 7)
674                         return (EINVAL);
675
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))
680                                 return (status);
681                         val = val_ec & IBM_EC_MASK_BRI;
682
683                         Args.Count = 1;
684                         Args.Pointer = &Arg;
685                         Arg.Type = ACPI_TYPE_INTEGER;
686                         Arg.Integer.Value = (arg > val) ? IBM_CMOS_BRIGHTNESS_UP : IBM_CMOS_BRIGHTNESS_DOWN;
687
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))
692                                         break;
693                         }
694                 }
695                 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_BRIGHTNESS, arg, 1);
696                 break;
697
698         case ACPI_IBM_METHOD_VOLUME:
699                 if (arg < 0 || arg > 14)
700                         return (EINVAL);
701
702                 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
703                 if (ACPI_FAILURE(status))
704                         return (status);
705
706                 if (sc->cmos_handle) {
707                         val = val_ec & IBM_EC_MASK_VOL;
708
709                         Args.Count = 1;
710                         Args.Pointer = &Arg;
711                         Arg.Type = ACPI_TYPE_INTEGER;
712                         Arg.Integer.Value = (arg > val) ? IBM_CMOS_VOLUME_UP : IBM_CMOS_VOLUME_DOWN;
713
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))
718                                         break;
719                         }
720                 }
721                 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME, arg + (val_ec & (~IBM_EC_MASK_VOL)), 1);
722                 break;
723
724         case ACPI_IBM_METHOD_MUTE:
725                 if (arg < 0 || arg > 1)
726                         return (EINVAL);
727
728                 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
729                 if (ACPI_FAILURE(status))
730                         return (status);
731
732                 if (sc->cmos_handle) {
733                         val = val_ec & IBM_EC_MASK_VOL;
734
735                         Args.Count = 1;
736                         Args.Pointer = &Arg;
737                         Arg.Type = ACPI_TYPE_INTEGER;
738                         Arg.Integer.Value = IBM_CMOS_VOLUME_MUTE;
739
740                         status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
741                         if (ACPI_FAILURE(status))
742                                 break;
743                 }
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);
745                 break;
746
747         case ACPI_IBM_METHOD_THINKLIGHT:
748                 if (arg < 0 || arg > 1)
749                         return (EINVAL);
750
751                 if (sc->light_set_supported) {
752                         Args.Count = 1;
753                         Args.Pointer = &Arg;
754                         Arg.Type = ACPI_TYPE_INTEGER;
755                         Arg.Integer.Value = arg ? sc->light_cmd_on : sc->light_cmd_off;
756
757                         status = AcpiEvaluateObject(sc->light_handle, NULL, &Args, NULL);
758                         if (ACPI_SUCCESS(status))
759                                 sc->light_val = arg;
760                         return (status);
761                 }
762                 break;
763
764         case ACPI_IBM_METHOD_BLUETOOTH:
765                 if (arg < 0 || arg > 1)
766                         return (EINVAL);
767
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);
770                 break;
771
772         case ACPI_IBM_METHOD_FANLEVEL:
773                 if (arg < 0 || arg > 7)
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                         val = val_ec & (~IBM_EC_MASK_FANLEVEL);
780
781                         return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_FANSTATUS, val | arg, 1);
782                 }
783                 break;
784
785         case ACPI_IBM_METHOD_FANSTATUS:
786                 if (arg < 0 || arg > 1)
787                         return (EINVAL);
788
789                 if (!sc->fan_handle) {
790                         /* Read the current fanstatus */
791                         ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
792
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);
795                 }
796                 break;
797         }
798
799         return (0);
800 }
801
802 static int
803 acpi_ibm_sysctl_init(struct acpi_ibm_softc *sc, int method)
804 {
805         int                     dummy;
806         ACPI_OBJECT_TYPE        cmos_t;
807         ACPI_HANDLE             ledb_handle;
808
809         switch (method) {
810         case ACPI_IBM_METHOD_EVENTS:
811                 /* Events are disabled by default */
812                 return (TRUE);
813
814         case ACPI_IBM_METHOD_EVENTMASK:
815                 return (sc->events_mask_supported);
816
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 */
822                 return (TRUE);
823
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));
828
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;
837                 }
838                 else if (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\LGHT", &sc->light_handle))) {
839                         sc->light_cmd_on = 1;
840                         sc->light_cmd_off = 0;
841                 }
842                 else
843                         sc->light_handle = NULL;
844
845                 sc->light_set_supported = (sc->light_handle &&
846                     ACPI_FAILURE(AcpiGetHandle(sc->ec_handle, "LEDB", &ledb_handle)));
847
848                 if (sc->light_get_supported)
849                         return (TRUE);
850
851                 if (sc->light_set_supported) {
852                         sc->light_val = 0;
853                         return (TRUE);
854                 }
855
856                 return (FALSE);
857
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)))
861                         return (TRUE);
862                 return (FALSE);
863
864         case ACPI_IBM_METHOD_FANSPEED:
865                 /* 
866                  * Some models report the fan speed in levels from 0-7
867                  * Newer models report it contiguously
868                  */
869                 sc->fan_levels =
870                     (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "GFAN", &sc->fan_handle)) ||
871                      ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\FSPD", &sc->fan_handle)));
872                 return (TRUE);
873
874         case ACPI_IBM_METHOD_FANLEVEL:
875         case ACPI_IBM_METHOD_FANSTATUS:
876                 /* 
877                  * Fan status is only supported on those models,
878                  * which report fan RPM contiguously, not in levels
879                  */
880                 if (sc->fan_levels)
881                         return (FALSE);
882                 return (TRUE);
883
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));
887                         return (TRUE);
888                 }
889                 return (FALSE);
890         }
891         return (FALSE);
892 }
893
894 static int
895 acpi_ibm_thermal_sysctl(SYSCTL_HANDLER_ARGS)
896 {
897         struct acpi_ibm_softc   *sc;
898         int                     error = 0;
899         char                    temp_cmd[] = "TMP0";
900         int                     temp[8];
901
902         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
903
904         sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
905
906         ACPI_SERIAL_BEGIN(ibm);
907
908         for (int i = 0; i < 8; ++i) {
909                 temp_cmd[3] = '0' + i;
910                 
911                 /* 
912                  * The TMPx methods seem to return +/- 128 or 0
913                  * when the respecting sensor is not available 
914                  */
915                 if (ACPI_FAILURE(acpi_GetInteger(sc->ec_handle, temp_cmd,
916                     &temp[i])) || ABS(temp[i]) == 128 || temp[i] == 0)
917                         temp[i] = -1;
918                 else if (sc->thermal_updt_supported)
919                         /* Temperature is reported in tenth of Kelvin */
920                         temp[i] = (temp[i] - 2732 + 5) / 10;
921         }
922
923         error = sysctl_handle_opaque(oidp, &temp, 8*sizeof(int), req);
924
925         ACPI_SERIAL_END(ibm);
926         return (error);
927 }
928
929 static void
930 acpi_ibm_notify(ACPI_HANDLE h, UINT32 notify, void *context)
931 {
932         int             event, arg, type;
933         device_t        dev = context;
934         struct acpi_ibm_softc *sc = device_get_softc(dev);
935
936         ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
937
938         if (notify != 0x80)
939                 device_printf(dev, "Unknown notify\n");
940
941         for (;;) {
942                 acpi_GetInteger(acpi_get_handle(dev), IBM_NAME_EVENTS_GET, &event);
943
944                 if (event == 0)
945                         break;
946
947
948                 type = (event >> 12) & 0xf;
949                 arg = event & 0xfff;
950                 switch (type) {
951                 case 1:
952                         if (!(sc->events_availmask & (1 << (arg - 1)))) {
953                                 device_printf(dev, "Unknown key %d\n", arg);
954                                 break;
955                         }
956
957                         /* Notify devd(8) */
958                         acpi_UserNotify("IBM", h, (arg & 0xff));
959                         break;
960                 default:
961                         break;
962                 }
963         }
964 }