acpi_thinkpad: bring back sensors framework
[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 <sys/sensors.h>
45 #include <machine/cpufunc.h>
46 #include <machine/clock.h>
47
48 #include "acpi.h"
49 #include "accommon.h"
50
51 #include "acpi_if.h"
52 #include <sys/module.h>
53 #include <dev/acpica5/acpivar.h>
54 #if defined(__FreeBSD__)
55 #include <dev/led/led.h>
56 #endif
57 #include <sys/sysctl.h>
58 #include <bus/isa/rtc.h>
59
60 #define _COMPONENT      ACPI_OEM
61 ACPI_MODULE_NAME("IBM")
62
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
77
78 /* Hotkeys/Buttons */
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)
97
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
109
110 /* CMOS Commands */
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
116
117 /* ACPI methods */
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"
125
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"
132
133 #define IBM_NUM_SENSORS                 9
134 #define IBM_THERMAL_SENSORS             8
135
136 #define ABS(x) (((x) < 0)? -(x) : (x))
137
138 struct acpi_ibm_softc {
139         device_t        dev;
140         ACPI_HANDLE     handle;
141
142         /* Embedded controller */
143         device_t        ec_dev;
144         ACPI_HANDLE     ec_handle;
145
146         /* CMOS */
147         ACPI_HANDLE     cmos_handle;
148
149         /* Fan status */
150         ACPI_HANDLE     fan_handle;
151         int             fan_levels;
152
153         /* Keylight commands and states */
154         ACPI_HANDLE     light_handle;
155         int             light_cmd_on;
156         int             light_cmd_off;
157         int             light_val;
158         int             light_get_supported;
159         int             light_set_supported;
160
161         /* led(4) interface */
162         struct cdev     *led_dev;
163         int             led_busy;
164         int             led_state;
165
166         int             wlan_bt_flags;
167         int             thermal_updt_supported;
168
169         unsigned int    events_availmask;
170         unsigned int    events_initialmask;
171         int             events_mask_supported;
172         int             events_enable;
173
174         /* sensors(9) related */
175         struct ksensordev sensordev;
176         struct ksensor sensors[IBM_NUM_SENSORS];
177
178         struct sysctl_ctx_list  sysctl_ctx;
179         struct sysctl_oid       *sysctl_tree;
180 };
181
182 static struct {
183         char    *name;
184         int     method;
185         char    *description;
186         int     access;
187 } acpi_ibm_sysctls[] = {
188         {
189                 .name           = "events",
190                 .method         = ACPI_IBM_METHOD_EVENTS,
191                 .description    = "ACPI events enable",
192                 .access         = CTLTYPE_INT | CTLFLAG_RW
193         },
194         {
195                 .name           = "eventmask",
196                 .method         = ACPI_IBM_METHOD_EVENTMASK,
197                 .description    = "ACPI eventmask",
198                 .access         = CTLTYPE_INT | CTLFLAG_RW
199         },
200         {
201                 .name           = "hotkey",
202                 .method         = ACPI_IBM_METHOD_HOTKEY,
203                 .description    = "Key Status",
204                 .access         = CTLTYPE_INT | CTLFLAG_RD
205         },
206         {
207                 .name           = "lcd_brightness",
208                 .method         = ACPI_IBM_METHOD_BRIGHTNESS,
209                 .description    = "LCD Brightness",
210                 .access         = CTLTYPE_INT | CTLFLAG_RW
211         },
212         {
213                 .name           = "volume",
214                 .method         = ACPI_IBM_METHOD_VOLUME,
215                 .description    = "Volume",
216                 .access         = CTLTYPE_INT | CTLFLAG_RW
217         },
218         {
219                 .name           = "mute",
220                 .method         = ACPI_IBM_METHOD_MUTE,
221                 .description    = "Mute",
222                 .access         = CTLTYPE_INT | CTLFLAG_RW
223         },
224         {
225                 .name           = "thinklight",
226                 .method         = ACPI_IBM_METHOD_THINKLIGHT,
227                 .description    = "Thinklight enable",
228                 .access         = CTLTYPE_INT | CTLFLAG_RW
229         },
230         {
231                 .name           = "bluetooth",
232                 .method         = ACPI_IBM_METHOD_BLUETOOTH,
233                 .description    = "Bluetooth enable",
234                 .access         = CTLTYPE_INT | CTLFLAG_RW
235         },
236         {
237                 .name           = "wlan",
238                 .method         = ACPI_IBM_METHOD_WLAN,
239                 .description    = "WLAN enable",
240                 .access         = CTLTYPE_INT | CTLFLAG_RD
241         },
242         {
243                 .name           = "fan_speed",
244                 .method         = ACPI_IBM_METHOD_FANSPEED,
245                 .description    = "Fan speed",
246                 .access         = CTLTYPE_INT | CTLFLAG_RD
247         },
248         {
249                 .name           = "fan_level",
250                 .method         = ACPI_IBM_METHOD_FANLEVEL,
251                 .description    = "Fan level",
252                 .access         = CTLTYPE_INT | CTLFLAG_RW
253         },
254         {
255                 .name           = "fan",
256                 .method         = ACPI_IBM_METHOD_FANSTATUS,
257                 .description    = "Fan enable",
258                 .access         = CTLTYPE_INT | CTLFLAG_RW
259         },
260
261         { NULL, 0, NULL, 0 }
262 };
263
264 ACPI_SERIAL_DECL(ibm, "ACPI IBM extras");
265
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);
269
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);
273 #endif
274
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);
279
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);
282
283 static void     acpi_ibm_refresh(void *);
284
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),
290
291         {0, 0}
292 };
293
294 static driver_t acpi_ibm_driver = {
295         "acpi_ibm",
296         acpi_ibm_methods,
297         sizeof(struct acpi_ibm_softc),
298 };
299
300 static devclass_t acpi_ibm_devclass;
301
302 DRIVER_MODULE(acpi_ibm, acpi, acpi_ibm_driver, acpi_ibm_devclass,
303               0, 0);
304 MODULE_DEPEND(acpi_ibm, acpi, 1, 1, 1);
305 static char    *ibm_ids[] = {"IBM0057", "IBM0068", NULL};
306
307 #if defined(__FreeBSD__)
308 static void
309 ibm_led(void *softc, int onoff)
310 {
311         struct acpi_ibm_softc* sc = (struct acpi_ibm_softc*) softc;
312
313         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
314
315         if (sc->led_busy)
316                 return;
317
318         sc->led_busy = 1;
319         sc->led_state = onoff;
320
321         AcpiOsExecute(OSL_NOTIFY_HANDLER, (void *)ibm_led_task, sc);
322 }
323
324 static void
325 ibm_led_task(struct acpi_ibm_softc *sc, int pending __unused)
326 {
327         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
328
329         ACPI_SERIAL_BEGIN(ibm);
330         acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_THINKLIGHT, sc->led_state);
331         ACPI_SERIAL_END(ibm);
332
333         sc->led_busy = 0;
334 }
335 #endif
336
337 static int
338 acpi_ibm_probe(device_t dev)
339 {
340         if (acpi_disabled("ibm") ||
341             ACPI_ID_PROBE(device_get_parent(dev), dev, ibm_ids) == NULL ||
342             device_get_unit(dev) != 0)
343                 return (ENXIO);
344
345         device_set_desc(dev, "IBM ThinkPad ACPI Extras");
346
347         return (0);
348 }
349
350 static int
351 acpi_ibm_attach(device_t dev)
352 {
353         struct acpi_ibm_softc   *sc;
354         devclass_t              ec_devclass;
355         struct acpi_softc       *acpi_sc;
356         int i;
357
358         ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
359
360         sc = device_get_softc(dev);
361         sc->dev = dev;
362         sc->handle = acpi_get_handle(dev);
363
364         /* Look for the first embedded controller */
365         if (!(ec_devclass = devclass_find ("acpi_ec"))) {
366                 if (bootverbose)
367                         device_printf(dev, "Couldn't find acpi_ec devclass\n");
368                 return (EINVAL);
369         }
370         if (!(sc->ec_dev = devclass_get_device(ec_devclass, 0))) {
371                 if (bootverbose)
372                         device_printf(dev, "Couldn't find acpi_ec device\n");
373                 return (EINVAL);
374         }
375         sc->ec_handle = acpi_get_handle(sc->ec_dev);
376         acpi_sc = acpi_device_get_parent_softc(dev);
377
378         ACPI_SERIAL_BEGIN(ibm);
379
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, "");
385
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));
389
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");
395
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;
400
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");
405         }
406
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))
410                         continue;
411
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);
417         }
418         ACPI_SERIAL_END(ibm);
419
420         /* Handle notifies */
421         AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
422             acpi_ibm_notify, dev);
423
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");
427                 return 1;
428         }
429         strlcpy(sc->sensordev.xname, device_get_nameunit(sc->dev),
430             sizeof(sc->sensordev.xname));
431
432         for (i = 0; i < IBM_THERMAL_SENSORS; i++) {
433                 sc->sensors[i].type = SENSOR_TEMP;
434                 sensor_attach(&sc->sensordev, &sc->sensors[i]);
435         }
436         
437         sc->sensors[i].type = SENSOR_FANRPM;
438         sensor_attach(&sc->sensordev, &sc->sensors[i]);
439
440         sensordev_install(&sc->sensordev);
441
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);
446 #endif
447
448         return (0);
449 }
450
451 static int
452 acpi_ibm_detach(device_t dev)
453 {
454         int i;
455         ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
456
457         struct acpi_ibm_softc *sc = device_get_softc(dev);
458
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);
464
465         AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY, acpi_ibm_notify);
466
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);
471
472 #if defined(__FreeBSD__)
473         if (sc->led_dev != NULL)
474                 led_destroy(sc->led_dev);
475 #endif
476
477         return (0);
478 }
479
480 static int
481 acpi_ibm_eventmask_set(struct acpi_ibm_softc *sc, int val)
482 {
483         ACPI_OBJECT             arg[2];
484         ACPI_OBJECT_LIST        args;
485         ACPI_STATUS             status;
486
487         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
488         ACPI_SERIAL_ASSERT(ibm);
489
490         args.Count = 2;
491         args.Pointer = arg;
492         arg[0].Type = ACPI_TYPE_INTEGER;
493         arg[1].Type = ACPI_TYPE_INTEGER;
494
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);
500
501                 if (ACPI_FAILURE(status))
502                         return (status);
503         }
504
505         return (0);
506 }
507
508 static int
509 acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS)
510 {
511         struct acpi_ibm_softc   *sc;
512         int                     arg;
513         int                     error = 0;
514         int                     function;
515         int                     method;
516         
517         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
518
519         sc = (struct acpi_ibm_softc *)oidp->oid_arg1;
520         function = oidp->oid_arg2;
521         method = acpi_ibm_sysctls[function].method;
522
523         ACPI_SERIAL_BEGIN(ibm);
524         arg = acpi_ibm_sysctl_get(sc, method);
525         error = sysctl_handle_int(oidp, &arg, 0, req);
526
527         /* Sanity check */
528         if (error != 0 || req->newptr == NULL)
529                 goto out;
530
531         /* Update */
532         error = acpi_ibm_sysctl_set(sc, method, arg);
533
534 out:
535         ACPI_SERIAL_END(ibm);
536         return (error);
537 }
538
539 static int
540 acpi_ibm_sysctl_get(struct acpi_ibm_softc *sc, int method)
541 {
542         ACPI_INTEGER    val_ec;
543         int             val = 0, key;
544
545         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
546         ACPI_SERIAL_ASSERT(ibm);
547
548         switch (method) {
549         case ACPI_IBM_METHOD_EVENTS:
550                 acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_GET, &val);
551                 break;
552
553         case ACPI_IBM_METHOD_EVENTMASK:
554                 if (sc->events_mask_supported)
555                         acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_MASK_GET, &val);
556                 break;
557
558         case ACPI_IBM_METHOD_HOTKEY:
559                 /*
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
579                  */
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;
593                 break;
594
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;
598                 break;
599
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;
603                 break;
604
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);
608                 break;
609
610         case ACPI_IBM_METHOD_THINKLIGHT:
611                 if (sc->light_get_supported)
612                         acpi_GetInteger(sc->ec_handle, IBM_NAME_KEYLIGHT, &val);
613                 else
614                         val = sc->light_val;
615                 break;
616
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);
621                 break;
622
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);
627                 break;
628
629         case ACPI_IBM_METHOD_FANSPEED:
630                 if (sc->fan_handle) {
631                         if(ACPI_FAILURE(acpi_GetInteger(sc->fan_handle, NULL, &val)))
632                                 val = -1;
633                 }
634                 else {
635                         ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSPEED, &val_ec, 2);
636                         val = val_ec;
637                 }
638                 break;
639
640         case ACPI_IBM_METHOD_FANLEVEL:
641                 /*
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)
649                  */
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;
653                 }
654                 break;
655
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;
660                 }
661                 else
662                         val = -1;
663                 break;
664         }
665
666         return (val);
667 }
668
669 static int
670 acpi_ibm_sysctl_set(struct acpi_ibm_softc *sc, int method, int arg)
671 {
672         int                     val, step;
673         ACPI_INTEGER            val_ec;
674         ACPI_OBJECT             Arg;
675         ACPI_OBJECT_LIST        Args;
676         ACPI_STATUS             status;
677
678         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
679         ACPI_SERIAL_ASSERT(ibm);
680
681         switch (method) {
682         case ACPI_IBM_METHOD_EVENTS:
683                 if (arg < 0 || arg > 1)
684                         return (EINVAL);
685
686                 status = acpi_SetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_SET, arg);
687                 if (ACPI_FAILURE(status))
688                         return (status);
689                 if (sc->events_mask_supported)
690                         return acpi_ibm_eventmask_set(sc, sc->events_availmask);
691                 break;
692
693         case ACPI_IBM_METHOD_EVENTMASK:
694                 if (sc->events_mask_supported)
695                         return acpi_ibm_eventmask_set(sc, arg);
696                 break;
697
698         case ACPI_IBM_METHOD_BRIGHTNESS:
699                 if (arg < 0 || arg > 7)
700                         return (EINVAL);
701
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))
706                                 return (status);
707                         val = val_ec & IBM_EC_MASK_BRI;
708
709                         Args.Count = 1;
710                         Args.Pointer = &Arg;
711                         Arg.Type = ACPI_TYPE_INTEGER;
712                         Arg.Integer.Value = (arg > val) ? IBM_CMOS_BRIGHTNESS_UP : IBM_CMOS_BRIGHTNESS_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_BRIGHTNESS, arg, 1);
722                 break;
723
724         case ACPI_IBM_METHOD_VOLUME:
725                 if (arg < 0 || arg > 14)
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 = (arg > val) ? IBM_CMOS_VOLUME_UP : IBM_CMOS_VOLUME_DOWN;
739
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))
744                                         break;
745                         }
746                 }
747                 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME, arg + (val_ec & (~IBM_EC_MASK_VOL)), 1);
748                 break;
749
750         case ACPI_IBM_METHOD_MUTE:
751                 if (arg < 0 || arg > 1)
752                         return (EINVAL);
753
754                 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1);
755                 if (ACPI_FAILURE(status))
756                         return (status);
757
758                 if (sc->cmos_handle) {
759                         val = val_ec & IBM_EC_MASK_VOL;
760
761                         Args.Count = 1;
762                         Args.Pointer = &Arg;
763                         Arg.Type = ACPI_TYPE_INTEGER;
764                         Arg.Integer.Value = IBM_CMOS_VOLUME_MUTE;
765
766                         status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
767                         if (ACPI_FAILURE(status))
768                                 break;
769                 }
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);
771                 break;
772
773         case ACPI_IBM_METHOD_THINKLIGHT:
774                 if (arg < 0 || arg > 1)
775                         return (EINVAL);
776
777                 if (sc->light_set_supported) {
778                         Args.Count = 1;
779                         Args.Pointer = &Arg;
780                         Arg.Type = ACPI_TYPE_INTEGER;
781                         Arg.Integer.Value = arg ? sc->light_cmd_on : sc->light_cmd_off;
782
783                         status = AcpiEvaluateObject(sc->light_handle, NULL, &Args, NULL);
784                         if (ACPI_SUCCESS(status))
785                                 sc->light_val = arg;
786                         return (status);
787                 }
788                 break;
789
790         case ACPI_IBM_METHOD_BLUETOOTH:
791                 if (arg < 0 || arg > 1)
792                         return (EINVAL);
793
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);
796                 break;
797
798         case ACPI_IBM_METHOD_FANLEVEL:
799                 if (arg < 0 || arg > 7)
800                         return (EINVAL);
801
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);
806
807                         return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_FANSTATUS, val | arg, 1);
808                 }
809                 break;
810
811         case ACPI_IBM_METHOD_FANSTATUS:
812                 if (arg < 0 || arg > 1)
813                         return (EINVAL);
814
815                 if (!sc->fan_handle) {
816                         /* Read the current fanstatus */
817                         ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1);
818
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);
821                 }
822                 break;
823         }
824
825         return (0);
826 }
827
828 static int
829 acpi_ibm_sysctl_init(struct acpi_ibm_softc *sc, int method)
830 {
831         int                     dummy;
832         ACPI_OBJECT_TYPE        cmos_t;
833         ACPI_HANDLE             ledb_handle;
834
835         switch (method) {
836         case ACPI_IBM_METHOD_EVENTS:
837                 /* Events are disabled by default */
838                 return (TRUE);
839
840         case ACPI_IBM_METHOD_EVENTMASK:
841                 return (sc->events_mask_supported);
842
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 */
848                 return (TRUE);
849
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));
854
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;
863                 }
864                 else if (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\LGHT", &sc->light_handle))) {
865                         sc->light_cmd_on = 1;
866                         sc->light_cmd_off = 0;
867                 }
868                 else
869                         sc->light_handle = NULL;
870
871                 sc->light_set_supported = (sc->light_handle &&
872                     ACPI_FAILURE(AcpiGetHandle(sc->ec_handle, "LEDB", &ledb_handle)));
873
874                 if (sc->light_get_supported)
875                         return (TRUE);
876
877                 if (sc->light_set_supported) {
878                         sc->light_val = 0;
879                         return (TRUE);
880                 }
881
882                 return (FALSE);
883
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)))
887                         return (TRUE);
888                 return (FALSE);
889
890         case ACPI_IBM_METHOD_FANSPEED:
891                 /* 
892                  * Some models report the fan speed in levels from 0-7
893                  * Newer models report it contiguously
894                  */
895                 sc->fan_levels =
896                     (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "GFAN", &sc->fan_handle)) ||
897                      ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\FSPD", &sc->fan_handle)));
898                 return (TRUE);
899
900         case ACPI_IBM_METHOD_FANLEVEL:
901         case ACPI_IBM_METHOD_FANSTATUS:
902                 /* 
903                  * Fan status is only supported on those models,
904                  * which report fan RPM contiguously, not in levels
905                  */
906                 if (sc->fan_levels)
907                         return (FALSE);
908                 return (TRUE);
909
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));
913                         return (TRUE);
914                 }
915                 return (FALSE);
916         }
917         return (FALSE);
918 }
919
920 void
921 acpi_ibm_refresh(void *arg)
922 {
923         struct acpi_ibm_softc   *sc;
924         char                    temp_cmd[] = "TMP0";
925         int                     i, data, temp[8], speed;
926
927         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
928
929         sc = (struct acpi_ibm_softc *)arg;
930
931         ACPI_SERIAL_BEGIN(ibm);
932
933         for (i = 0; i < IBM_THERMAL_SENSORS; ++i) {
934                 temp_cmd[3] = '0' + i;
935                 
936                 /* 
937                  * The TMPx methods seem to return +/- 128 or 0
938                  * when the respecting sensor is not available 
939                  */
940                 if (ACPI_FAILURE(acpi_GetInteger(sc->ec_handle, temp_cmd,
941                     &temp[i])) || ABS(temp[i]) == 128 || temp[i] == 0) { 
942                         sc->sensors[i].flags |= SENSOR_FINVALID;
943                         data = 0;
944                 }
945                 else if (sc->thermal_updt_supported) {
946                         /* Temperature is reported in tenth of Kelvin */
947                         sc->sensors[i].value = data * 100000;
948                 }
949                 sc->sensors[i].value = data * 1000000 + 273150000;
950         }
951         sc->sensors[i].flags &= ~SENSOR_FINVALID;
952         if (sc->fan_handle) {
953                 if (ACPI_FAILURE(acpi_GetInteger(sc->fan_handle,
954                     NULL, &data)))
955                         sc->sensors[i].flags |= SENSOR_FINVALID;
956                         data = -1;
957         }
958         else {  
959                 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSPEED, &speed, 2);
960                 data = speed;
961         }
962
963         sc->sensors[i].value = data;
964
965         ACPI_SERIAL_END(ibm);
966 }
967
968 static void
969 acpi_ibm_notify(ACPI_HANDLE h, UINT32 notify, void *context)
970 {
971         int             event, arg, type;
972         device_t        dev = context;
973         struct acpi_ibm_softc *sc = device_get_softc(dev);
974
975         ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
976
977         if (notify != 0x80)
978                 device_printf(dev, "Unknown notify\n");
979
980         for (;;) {
981                 acpi_GetInteger(acpi_get_handle(dev), IBM_NAME_EVENTS_GET, &event);
982
983                 if (event == 0)
984                         break;
985
986
987                 type = (event >> 12) & 0xf;
988                 arg = event & 0xfff;
989                 switch (type) {
990                 case 1:
991                         if (!(sc->events_availmask & (1 << (arg - 1)))) {
992                                 device_printf(dev, "Unknown key %d\n", arg);
993                                 break;
994                         }
995
996                         /* Notify devd(8) */
997                         acpi_UserNotify("IBM", h, (arg & 0xff));
998                         break;
999                 default:
1000                         break;
1001                 }
1002         }
1003 }