Merge branch 'vendor/GCC'
[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  *
27  * $FreeBSD: src/sys/dev/acpi_support/acpi_ibm.c,v 1.15 2007/10/25 17:30:18 jhb Exp $
28  * $DragonFly: src/sys/dev/acpica5/acpi_thinkpad/acpi_thinkpad.c,v 1.2 2008/10/03 00:47:36 hasso Exp $
29  */
30
31 /*
32  * Driver for extra ACPI-controlled gadgets found on IBM and Lenovo ThinkPad
33  * laptops. Inspired by the ibm-acpi and tpb projects which implement these
34  * features 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 <sys/module.h>
46 #include <sys/sensors.h>
47 #include <sys/sysctl.h>
48 #include <sys/lock.h>
49 #include <sys/thread2.h>
50 #include <machine/clock.h>
51
52 #include "acpi.h"
53 #include "accommon.h"
54 #include "acpivar.h"
55 #include "acpi_if.h"
56
57 #define _COMPONENT      ACPI_OEM
58 ACPI_MODULE_NAME("THINKPAD")
59
60 /* Internal methods */
61 #define ACPI_THINKPAD_METHOD_EVENTS             1
62 #define ACPI_THINKPAD_METHOD_EVENTMASK          2
63 #define ACPI_THINKPAD_METHOD_HOTKEY             3
64 #define ACPI_THINKPAD_METHOD_BRIGHTNESS         4
65 #define ACPI_THINKPAD_METHOD_VOLUME             5
66 #define ACPI_THINKPAD_METHOD_MUTE               6
67 #define ACPI_THINKPAD_METHOD_THINKLIGHT         7
68 #define ACPI_THINKPAD_METHOD_BLUETOOTH          8
69 #define ACPI_THINKPAD_METHOD_WLAN               9
70 #define ACPI_THINKPAD_METHOD_FANSPEED           10
71 #define ACPI_THINKPAD_METHOD_FANLEVEL           11
72 #define ACPI_THINKPAD_METHOD_FANSTATUS          12
73 #define ACPI_THINKPAD_METHOD_THERMAL            13
74
75 /* Hotkeys/Buttons */
76 #define THINKPAD_RTC_HOTKEY1                    0x64
77 #define   THINKPAD_RTC_MASK_HOME                (1 << 0)
78 #define   THINKPAD_RTC_MASK_SEARCH              (1 << 1)
79 #define   THINKPAD_RTC_MASK_MAIL                (1 << 2)
80 #define   THINKPAD_RTC_MASK_WLAN                (1 << 5)
81 #define THINKPAD_RTC_HOTKEY2                    0x65
82 #define   THINKPAD_RTC_MASK_THINKPAD            (1 << 3)
83 #define   THINKPAD_RTC_MASK_ZOOM                (1 << 5)
84 #define   THINKPAD_RTC_MASK_VIDEO               (1 << 6)
85 #define   THINKPAD_RTC_MASK_HIBERNATE           (1 << 7)
86 #define THINKPAD_RTC_THINKLIGHT                 0x66
87 #define   THINKPAD_RTC_MASK_THINKLIGHT          (1 << 4)
88 #define THINKPAD_RTC_SCREENEXPAND               0x67
89 #define   THINKPAD_RTC_MASK_SCREENEXPAND        (1 << 5)
90 #define THINKPAD_RTC_BRIGHTNESS                 0x6c
91 #define   THINKPAD_RTC_MASK_BRIGHTNESS          (1 << 5)
92 #define THINKPAD_RTC_VOLUME                     0x6e
93 #define   THINKPAD_RTC_MASK_VOLUME              (1 << 7)
94
95 /* Embedded Controller registers */
96 #define THINKPAD_EC_BRIGHTNESS                  0x31
97 #define   THINKPAD_EC_MASK_BRI                  0x7
98 #define THINKPAD_EC_VOLUME                      0x30
99 #define   THINKPAD_EC_MASK_VOL                  0xf
100 #define   THINKPAD_EC_MASK_MUTE                 (1 << 6)
101 #define THINKPAD_EC_FANSTATUS                   0x2F
102 #define   THINKPAD_EC_MASK_FANLEVEL             0x3f
103 #define   THINKPAD_EC_MASK_FANDISENGAGED        (1 << 6)
104 #define   THINKPAD_EC_MASK_FANSTATUS            (1 << 7)
105 #define THINKPAD_EC_FANSPEED                    0x84
106
107 /* CMOS Commands */
108 #define THINKPAD_CMOS_VOLUME_DOWN               0
109 #define THINKPAD_CMOS_VOLUME_UP                 1
110 #define THINKPAD_CMOS_VOLUME_MUTE               2
111 #define THINKPAD_CMOS_BRIGHTNESS_UP             4
112 #define THINKPAD_CMOS_BRIGHTNESS_DOWN           5
113
114 /* ACPI methods */
115 #define THINKPAD_NAME_KEYLIGHT                  "KBLT"
116 #define THINKPAD_NAME_WLAN_BT_GET               "GBDC"
117 #define THINKPAD_NAME_WLAN_BT_SET               "SBDC"
118 #define   THINKPAD_NAME_MASK_BT                 (1 << 1)
119 #define   THINKPAD_NAME_MASK_WLAN               (1 << 2)
120 #define THINKPAD_NAME_THERMAL_GET               "TMP7"
121 #define THINKPAD_NAME_THERMAL_UPDT              "UPDT"
122
123 #define THINKPAD_NAME_EVENTS_STATUS_GET         "DHKC"
124 #define THINKPAD_NAME_EVENTS_MASK_GET           "DHKN"
125 #define THINKPAD_NAME_EVENTS_STATUS_SET         "MHKC"
126 #define THINKPAD_NAME_EVENTS_MASK_SET           "MHKM"
127 #define THINKPAD_NAME_EVENTS_GET                "MHKP"
128 #define THINKPAD_NAME_EVENTS_AVAILMASK          "MHKA"
129
130 #define THINKPAD_NUM_SENSORS                    9
131 #define THINKPAD_TEMP_SENSORS                   8
132
133 #define ABS(x) (((x) < 0)? -(x) : (x))
134
135 struct acpi_thinkpad_softc {
136         device_t        dev;
137         ACPI_HANDLE     handle;
138
139         /* Embedded controller */
140         device_t        ec_dev;
141         ACPI_HANDLE     ec_handle;
142
143         /* CMOS */
144         ACPI_HANDLE     cmos_handle;
145
146         /* Fan status */
147         ACPI_HANDLE     fan_handle;
148         int             fan_levels;
149
150         /* Keylight commands and states */
151         ACPI_HANDLE     light_handle;
152         int             light_cmd_on;
153         int             light_cmd_off;
154         int             light_val;
155         int             light_get_supported;
156         int             light_set_supported;
157
158         /* led(4) interface */
159         struct cdev     *led_dev;
160         int             led_busy;
161         int             led_state;
162
163         int             wlan_bt_flags;
164         int             thermal_updt_supported;
165
166         unsigned int    events_availmask;
167         unsigned int    events_initialmask;
168         int             events_mask_supported;
169         int             events_enable;
170
171         /* sensors(9) related */
172         struct ksensordev sensordev;
173         struct ksensor sensors[THINKPAD_NUM_SENSORS];
174
175         struct sysctl_ctx_list   sysctl_ctx;
176         struct sysctl_oid       *sysctl_tree;
177 };
178
179 static struct {
180         char    *name;
181         int     method;
182         char    *description;
183         int     access;
184 } acpi_thinkpad_sysctls[] = {
185         {
186                 .name           = "events",
187                 .method         = ACPI_THINKPAD_METHOD_EVENTS,
188                 .description    = "ACPI events enable",
189                 .access         = CTLTYPE_INT | CTLFLAG_RW
190         },
191         {
192                 .name           = "eventmask",
193                 .method         = ACPI_THINKPAD_METHOD_EVENTMASK,
194                 .description    = "ACPI eventmask",
195                 .access         = CTLTYPE_INT | CTLFLAG_RW
196         },
197         {
198                 .name           = "hotkey",
199                 .method         = ACPI_THINKPAD_METHOD_HOTKEY,
200                 .description    = "Key Status",
201                 .access         = CTLTYPE_INT | CTLFLAG_RD
202         },
203         {
204                 .name           = "lcd_brightness",
205                 .method         = ACPI_THINKPAD_METHOD_BRIGHTNESS,
206                 .description    = "LCD Brightness",
207                 .access         = CTLTYPE_INT | CTLFLAG_RW
208         },
209         {
210                 .name           = "volume",
211                 .method         = ACPI_THINKPAD_METHOD_VOLUME,
212                 .description    = "Volume",
213                 .access         = CTLTYPE_INT | CTLFLAG_RW
214         },
215         {
216                 .name           = "mute",
217                 .method         = ACPI_THINKPAD_METHOD_MUTE,
218                 .description    = "Mute",
219                 .access         = CTLTYPE_INT | CTLFLAG_RW
220         },
221         {
222                 .name           = "thinklight",
223                 .method         = ACPI_THINKPAD_METHOD_THINKLIGHT,
224                 .description    = "Thinklight enable",
225                 .access         = CTLTYPE_INT | CTLFLAG_RW
226         },
227         {
228                 .name           = "bluetooth",
229                 .method         = ACPI_THINKPAD_METHOD_BLUETOOTH,
230                 .description    = "Bluetooth enable",
231                 .access         = CTLTYPE_INT | CTLFLAG_RW
232         },
233         {
234                 .name           = "wlan",
235                 .method         = ACPI_THINKPAD_METHOD_WLAN,
236                 .description    = "WLAN enable",
237                 .access         = CTLTYPE_INT | CTLFLAG_RD
238         },
239         {
240                 .name           = "fan_level",
241                 .method         = ACPI_THINKPAD_METHOD_FANLEVEL,
242                 .description    = "Fan level",
243                 .access         = CTLTYPE_INT | CTLFLAG_RW
244         },
245         {
246                 .name           = "fan",
247                 .method         = ACPI_THINKPAD_METHOD_FANSTATUS,
248                 .description    = "Fan enable",
249                 .access         = CTLTYPE_INT | CTLFLAG_RW
250         },
251
252         { NULL, 0, NULL, 0 }
253 };
254
255 static struct lock tplock;
256
257 static int      acpi_thinkpad_probe(device_t dev);
258 static int      acpi_thinkpad_attach(device_t dev);
259 static int      acpi_thinkpad_detach(device_t dev);
260
261 static int      acpi_thinkpad_sysctl(SYSCTL_HANDLER_ARGS);
262 static int      acpi_thinkpad_sysctl_init(struct acpi_thinkpad_softc *sc,
263                 int method);
264 static int      acpi_thinkpad_sysctl_get(struct acpi_thinkpad_softc *sc,
265                 int method);
266 static int      acpi_thinkpad_sysctl_set(struct acpi_thinkpad_softc *sc,
267                 int method, int val);
268
269 static int      acpi_thinkpad_eventmask_set(struct acpi_thinkpad_softc *sc,
270                 int val);
271 static void     acpi_thinkpad_notify(ACPI_HANDLE h, UINT32 notify,
272                 void *context);
273 static void     acpi_thinkpad_refresh(void *);
274
275 static device_method_t acpi_thinkpad_methods[] = {
276         /* Device interface */
277         DEVMETHOD(device_probe, acpi_thinkpad_probe),
278         DEVMETHOD(device_attach, acpi_thinkpad_attach),
279         DEVMETHOD(device_detach, acpi_thinkpad_detach),
280         {0, 0}
281 };
282
283 static driver_t acpi_thinkpad_driver = {
284         "acpi_thinkpad",
285         acpi_thinkpad_methods,
286         sizeof(struct acpi_thinkpad_softc),
287 };
288
289 static devclass_t acpi_thinkpad_devclass;
290
291 DRIVER_MODULE(acpi_thinkpad, acpi, acpi_thinkpad_driver,
292     acpi_thinkpad_devclass, 0, 0);
293 MODULE_DEPEND(acpi_thinkpad, acpi, 1, 1, 1);
294 static char    *thinkpad_ids[] = {"IBM0068", NULL};
295
296 static int
297 acpi_thinkpad_probe(device_t dev)
298 {
299         if (acpi_disabled("thinkpad") ||
300             ACPI_ID_PROBE(device_get_parent(dev), dev, thinkpad_ids) == NULL ||
301             device_get_unit(dev) != 0) 
302                 return (ENXIO);
303
304         device_set_desc(dev, "IBM/Lenovo ThinkPad ACPI Extras");
305         return (0);
306 }
307
308 static int
309 acpi_thinkpad_attach(device_t dev)
310 {
311         struct acpi_thinkpad_softc      *sc;
312         struct acpi_softc       *acpi_sc;
313         devclass_t              ec_devclass;
314         int                     i;
315
316         ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
317
318         sc = device_get_softc(dev);
319         sc->dev = dev;
320         sc->handle = acpi_get_handle(dev);
321
322         acpi_sc = acpi_device_get_parent_softc(dev);
323
324         /* Look for the first embedded controller */
325         if (!(ec_devclass = devclass_find ("acpi_ec"))) {
326                 if (bootverbose)
327                         device_printf(dev, "Couldn't find acpi_ec devclass\n");
328                 return (EINVAL);
329         }
330         if (!(sc->ec_dev = devclass_get_device(ec_devclass, 0))) {
331                 if (bootverbose)
332                         device_printf(dev, "Couldn't find acpi_ec device\n");
333                 return (EINVAL);
334         }
335         sc->ec_handle = acpi_get_handle(sc->ec_dev);
336         
337         lockinit(&tplock, "thinkpad", 0, 0);
338         lockmgr(&tplock, LK_EXCLUSIVE);
339
340         sysctl_ctx_init(&sc->sysctl_ctx);
341         sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
342             SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO,
343             "thinkpad", CTLFLAG_RD, 0, "");
344
345         /* Look for event mask and hook up the nodes */
346         sc->events_mask_supported = ACPI_SUCCESS(acpi_GetInteger(sc->handle,
347             THINKPAD_NAME_EVENTS_MASK_GET, &sc->events_initialmask));
348
349         if (sc->events_mask_supported) {
350                 SYSCTL_ADD_INT(&sc->sysctl_ctx,
351                     SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
352                     "initialmask", CTLFLAG_RD,
353                     &sc->events_initialmask, 0, "Initial eventmask");
354
355                 /* The availmask is the bitmask of supported events */
356                 if (ACPI_FAILURE(acpi_GetInteger(sc->handle,
357                     THINKPAD_NAME_EVENTS_AVAILMASK, &sc->events_availmask)))
358                         sc->events_availmask = 0xffffffff;
359
360                 SYSCTL_ADD_INT(&sc->sysctl_ctx,
361                     SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
362                     "availmask", CTLFLAG_RD,
363                     &sc->events_availmask, 0, "Mask of supported events");
364         }
365
366         /* Hook up proc nodes */
367         for (i = 0; acpi_thinkpad_sysctls[i].name != NULL; i++) {
368                 if (!acpi_thinkpad_sysctl_init(sc,
369                     acpi_thinkpad_sysctls[i].method))
370                         continue;
371
372                 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
373                     SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
374                     acpi_thinkpad_sysctls[i].name,
375                     acpi_thinkpad_sysctls[i].access,
376                     sc, i, acpi_thinkpad_sysctl, "I",
377                     acpi_thinkpad_sysctls[i].description);
378         }
379
380         lockmgr(&tplock, LK_RELEASE);
381
382         /* Handle notifies */
383         AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
384             acpi_thinkpad_notify, dev);
385
386         /* Attach sensors(9). */
387         if (sensor_task_register(sc, acpi_thinkpad_refresh, 5)) {
388                 device_printf(sc->dev, "unable to register update task\n");
389                 return 1;
390         }
391
392         strlcpy(sc->sensordev.xname, device_get_nameunit(sc->dev),
393             sizeof(sc->sensordev.xname));
394
395         for (i = 0; i < THINKPAD_TEMP_SENSORS; i++) {
396                 sc->sensors[i].type = SENSOR_TEMP;
397                 sensor_attach(&sc->sensordev, &sc->sensors[i]);
398         }
399         
400         sc->sensors[i].type = SENSOR_FANRPM;
401         sensor_attach(&sc->sensordev, &sc->sensors[i]);
402
403         sensordev_install(&sc->sensordev);
404
405         return (0);
406 }
407
408 static int
409 acpi_thinkpad_detach(device_t dev)
410 {
411         int i;
412
413         ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
414
415         struct acpi_thinkpad_softc *sc = device_get_softc(dev);
416
417         /* Disable events and restore eventmask */
418         lockmgr(&tplock, LK_EXCLUSIVE);
419         acpi_thinkpad_sysctl_set(sc, ACPI_THINKPAD_METHOD_EVENTS, 0);
420         acpi_thinkpad_sysctl_set(sc, ACPI_THINKPAD_METHOD_EVENTMASK,
421             sc->events_initialmask);
422         lockmgr(&tplock, LK_RELEASE);
423
424         AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
425             acpi_thinkpad_notify);
426
427         if (sc->sysctl_tree != NULL)
428                 sysctl_ctx_free(&sc->sysctl_ctx);
429
430         sensordev_deinstall(&sc->sensordev);
431         for (i = 0; i < THINKPAD_NUM_SENSORS; i++)
432                 sensor_detach(&sc->sensordev, &sc->sensors[i]);
433         sensor_task_unregister(sc);
434
435         return (0);
436 }
437
438 static int
439 acpi_thinkpad_eventmask_set(struct acpi_thinkpad_softc *sc, int val)
440 {
441         int i;
442         ACPI_OBJECT             arg[2];
443         ACPI_OBJECT_LIST        args;
444         ACPI_STATUS             status;
445
446         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
447         KKASSERT(lockstatus(&tplock, curthread) != 0);
448
449         args.Count = 2;
450         args.Pointer = arg;
451         arg[0].Type = ACPI_TYPE_INTEGER;
452         arg[1].Type = ACPI_TYPE_INTEGER;
453
454         for (i = 0; i < 32; ++i) {
455                 arg[0].Integer.Value = i+1;
456                 arg[1].Integer.Value = (((1 << i) & val) != 0);
457                 status = AcpiEvaluateObject(sc->handle,
458                     THINKPAD_NAME_EVENTS_MASK_SET, &args, NULL);
459
460                 if (ACPI_FAILURE(status))
461                         return (status);
462         }
463
464         return (0);
465 }
466
467 static int
468 acpi_thinkpad_sysctl(SYSCTL_HANDLER_ARGS)
469 {
470         struct acpi_thinkpad_softc      *sc;
471         int                     arg;
472         int                     error = 0;
473         int                     function;
474         int                     method;
475         
476         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
477
478         sc = (struct acpi_thinkpad_softc *)oidp->oid_arg1;
479         function = oidp->oid_arg2;
480         method = acpi_thinkpad_sysctls[function].method;
481
482         lockmgr(&tplock, LK_EXCLUSIVE);
483         arg = acpi_thinkpad_sysctl_get(sc, method);
484         error = sysctl_handle_int(oidp, &arg, 0, req);
485
486         /* Sanity check */
487         if (error != 0 || req->newptr == NULL)
488                 goto out;
489
490         /* Update */
491         error = acpi_thinkpad_sysctl_set(sc, method, arg);
492
493 out:
494         lockmgr(&tplock, LK_RELEASE);
495         return (error);
496 }
497
498 static int
499 acpi_thinkpad_sysctl_get(struct acpi_thinkpad_softc *sc, int method)
500 {
501         ACPI_INTEGER    val_ec;
502         int             val = 0, key;
503
504         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
505         KKASSERT(lockstatus(&tplock, curthread) != 0);
506
507         switch (method) {
508         case ACPI_THINKPAD_METHOD_EVENTS:
509                 acpi_GetInteger(sc->handle, THINKPAD_NAME_EVENTS_STATUS_GET,
510                     &val);
511                 break;
512
513         case ACPI_THINKPAD_METHOD_EVENTMASK:
514                 if (sc->events_mask_supported)
515                         acpi_GetInteger(sc->handle,
516                             THINKPAD_NAME_EVENTS_MASK_GET, &val);
517                 break;
518
519         case ACPI_THINKPAD_METHOD_HOTKEY:
520                 /*
521                  * Construct the hotkey as a bitmask as illustrated below.
522                  * Note that whenever a key was pressed, the respecting bit
523                  * toggles and nothing else changes.
524                  * +--+--+-+-+-+-+-+-+-+-+-+-+
525                  * |11|10|9|8|7|6|5|4|3|2|1|0|
526                  * +--+--+-+-+-+-+-+-+-+-+-+-+
527                  *   |  | | | | | | | | | | |
528                  *   |  | | | | | | | | | | +- Home Button
529                  *   |  | | | | | | | | | +--- Search Button
530                  *   |  | | | | | | | | +----- Mail Button
531                  *   |  | | | | | | | +------- Thinkpad Button
532                  *   |  | | | | | | +--------- Zoom (Fn + Space)
533                  *   |  | | | | | +----------- WLAN Button
534                  *   |  | | | | +------------- Video Button
535                  *   |  | | | +--------------- Hibernate Button
536                  *   |  | | +----------------- Thinklight Button
537                  *   |  | +------------------- Screen expand (Fn + F8)
538                  *   |  +--------------------- Brightness
539                  *   +------------------------ Volume/Mute
540                  */
541                 key = rtcin(THINKPAD_RTC_HOTKEY1);
542                 val = (THINKPAD_RTC_MASK_HOME | THINKPAD_RTC_MASK_SEARCH |
543                     THINKPAD_RTC_MASK_MAIL | THINKPAD_RTC_MASK_WLAN) & key;
544                 key = rtcin(THINKPAD_RTC_HOTKEY2);
545                 val |= (THINKPAD_RTC_MASK_THINKPAD | THINKPAD_RTC_MASK_VIDEO |
546                     THINKPAD_RTC_MASK_HIBERNATE) & key;
547                 val |= (THINKPAD_RTC_MASK_ZOOM & key) >> 1;
548                 key = rtcin(THINKPAD_RTC_THINKLIGHT);
549                 val |= (THINKPAD_RTC_MASK_THINKLIGHT & key) << 4;
550                 key = rtcin(THINKPAD_RTC_SCREENEXPAND);
551                 val |= (THINKPAD_RTC_MASK_THINKLIGHT & key) << 4;
552                 key = rtcin(THINKPAD_RTC_BRIGHTNESS);
553                 val |= (THINKPAD_RTC_MASK_BRIGHTNESS & key) << 5;
554                 key = rtcin(THINKPAD_RTC_VOLUME);
555                 val |= (THINKPAD_RTC_MASK_VOLUME & key) << 4;
556                 break;
557
558         case ACPI_THINKPAD_METHOD_BRIGHTNESS:
559                 ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_BRIGHTNESS, &val_ec, 1);
560                 val = val_ec & THINKPAD_EC_MASK_BRI;
561                 break;
562
563         case ACPI_THINKPAD_METHOD_VOLUME:
564                 ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_VOLUME, &val_ec, 1);
565                 val = val_ec & THINKPAD_EC_MASK_VOL;
566                 break;
567
568         case ACPI_THINKPAD_METHOD_MUTE:
569                 ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_VOLUME, &val_ec, 1);
570                 val = ((val_ec & THINKPAD_EC_MASK_MUTE) ==
571                     THINKPAD_EC_MASK_MUTE);
572                 break;
573
574         case ACPI_THINKPAD_METHOD_THINKLIGHT:
575                 if (sc->light_get_supported)
576                         acpi_GetInteger(sc->ec_handle, THINKPAD_NAME_KEYLIGHT,
577                             &val);
578                 else
579                         val = sc->light_val;
580                 break;
581
582         case ACPI_THINKPAD_METHOD_BLUETOOTH:
583                 acpi_GetInteger(sc->handle, THINKPAD_NAME_WLAN_BT_GET, &val);
584                 sc->wlan_bt_flags = val;
585                 val = ((val & THINKPAD_NAME_MASK_BT) != 0);
586                 break;
587
588         case ACPI_THINKPAD_METHOD_WLAN:
589                 acpi_GetInteger(sc->handle, THINKPAD_NAME_WLAN_BT_GET, &val);
590                 sc->wlan_bt_flags = val;
591                 val = ((val & THINKPAD_NAME_MASK_WLAN) != 0);
592                 break;
593
594         case ACPI_THINKPAD_METHOD_FANSPEED:
595                 if (sc->fan_handle) {
596                         if (ACPI_FAILURE(acpi_GetInteger(sc->fan_handle,
597                             NULL, &val)))
598                                 val = -1;
599                 }
600                 else {
601                         ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_FANSPEED,
602                             &val_ec, 2);
603                         val = val_ec;
604                 }
605                 break;
606
607         case ACPI_THINKPAD_METHOD_FANLEVEL:
608                 /*
609                  * The THINKPAD_EC_FANSTATUS register works as follows:
610                  * Bit 0-5 indicate the level at which the fan operates. Only
611                  *       values between 0 and 7 have an effect. Everything
612                  *       above 7 is treated the same as level 7
613                  * Bit 6 overrides the fan speed limit if set to 1
614                  * Bit 7 indicates at which mode the fan operates:
615                  *       manual (0) or automatic (1)
616                  */
617                 if (!sc->fan_handle) {
618                         ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_FANSTATUS,
619                             &val_ec, 1);
620                         val = val_ec & THINKPAD_EC_MASK_FANLEVEL;
621                 }
622                 break;
623
624         case ACPI_THINKPAD_METHOD_FANSTATUS:
625                 if (!sc->fan_handle) {
626                         ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_FANSTATUS,
627                             &val_ec, 1);
628                         val = (val_ec & THINKPAD_EC_MASK_FANSTATUS) ==
629                             THINKPAD_EC_MASK_FANSTATUS;
630                 }
631                 else
632                         val = -1;
633                 break;
634         }
635
636         return (val);
637 }
638
639 static int
640 acpi_thinkpad_sysctl_set(struct acpi_thinkpad_softc *sc, int method, int arg)
641 {
642         int                     val, step, i;
643         ACPI_INTEGER            val_ec;
644         ACPI_OBJECT             Arg;
645         ACPI_OBJECT_LIST        Args;
646         ACPI_STATUS             status;
647
648         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
649         KKASSERT(lockstatus(&tplock, curthread) != 0);
650
651         switch (method) {
652         case ACPI_THINKPAD_METHOD_EVENTS:
653                 if (arg < 0 || arg > 1)
654                         return (EINVAL);
655
656                 status = acpi_SetInteger(sc->handle,
657                     THINKPAD_NAME_EVENTS_STATUS_SET, arg);
658                 if (ACPI_FAILURE(status))
659                         return (status);
660                 if (sc->events_mask_supported)
661                         return acpi_thinkpad_eventmask_set(sc,
662                             sc->events_availmask);
663                 break;
664
665         case ACPI_THINKPAD_METHOD_EVENTMASK:
666                 if (sc->events_mask_supported)
667                         return acpi_thinkpad_eventmask_set(sc, arg);
668                 break;
669
670         case ACPI_THINKPAD_METHOD_BRIGHTNESS:
671                 if (arg < 0 || arg > 7)
672                         return (EINVAL);
673
674                 if (sc->cmos_handle) {
675                         /* Read the current brightness */
676                         status = ACPI_EC_READ(sc->ec_dev,
677                             THINKPAD_EC_BRIGHTNESS, &val_ec, 1);
678                         if (ACPI_FAILURE(status))
679                                 return (status);
680                         val = val_ec & THINKPAD_EC_MASK_BRI;
681
682                         Args.Count = 1;
683                         Args.Pointer = &Arg;
684                         Arg.Type = ACPI_TYPE_INTEGER;
685                         Arg.Integer.Value = (arg > val) ?
686                             THINKPAD_CMOS_BRIGHTNESS_UP :
687                             THINKPAD_CMOS_BRIGHTNESS_DOWN;
688
689                         step = (arg > val) ? 1 : -1;
690                         for (i = val; i != arg; i += step) {
691                                 status = AcpiEvaluateObject(sc->cmos_handle,
692                                     NULL, &Args, NULL);
693                                 if (ACPI_FAILURE(status))
694                                         break;
695                         }
696                 }
697                 return ACPI_EC_WRITE(sc->ec_dev, THINKPAD_EC_BRIGHTNESS,
698                     arg, 1);
699                 break;
700
701         case ACPI_THINKPAD_METHOD_VOLUME:
702                 if (arg < 0 || arg > 14)
703                         return (EINVAL);
704
705                 status = ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_VOLUME,
706                     &val_ec, 1);
707                 if (ACPI_FAILURE(status))
708                         return (status);
709
710                 if (sc->cmos_handle) {
711                         val = val_ec & THINKPAD_EC_MASK_VOL;
712
713                         Args.Count = 1;
714                         Args.Pointer = &Arg;
715                         Arg.Type = ACPI_TYPE_INTEGER;
716                         Arg.Integer.Value = (arg > val) ?
717                             THINKPAD_CMOS_VOLUME_UP : THINKPAD_CMOS_VOLUME_DOWN;
718
719                         step = (arg > val) ? 1 : -1;
720                         for (i = val; i != arg; i += step) {
721                                 status = AcpiEvaluateObject(sc->cmos_handle,
722                                     NULL, &Args, NULL);
723                                 if (ACPI_FAILURE(status))
724                                         break;
725                         }
726                 }
727                 return ACPI_EC_WRITE(sc->ec_dev, THINKPAD_EC_VOLUME, arg +
728                     (val_ec & (~THINKPAD_EC_MASK_VOL)), 1);
729                 break;
730
731         case ACPI_THINKPAD_METHOD_MUTE:
732                 if (arg < 0 || arg > 1)
733                         return (EINVAL);
734
735                 status = ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_VOLUME,
736                     &val_ec, 1);
737                 if (ACPI_FAILURE(status))
738                         return (status);
739
740                 if (sc->cmos_handle) {
741                         val = val_ec & THINKPAD_EC_MASK_VOL;
742
743                         Args.Count = 1;
744                         Args.Pointer = &Arg;
745                         Arg.Type = ACPI_TYPE_INTEGER;
746                         Arg.Integer.Value = THINKPAD_CMOS_VOLUME_MUTE;
747
748                         status = AcpiEvaluateObject(sc->cmos_handle, NULL,
749                             &Args, NULL);
750                         if (ACPI_FAILURE(status))
751                                 break;
752                 }
753                 return ACPI_EC_WRITE(sc->ec_dev, THINKPAD_EC_VOLUME, (arg==1) ?
754                    val_ec | THINKPAD_EC_MASK_MUTE :
755                    val_ec & (~THINKPAD_EC_MASK_MUTE), 1);
756                 break;
757
758         case ACPI_THINKPAD_METHOD_THINKLIGHT:
759                 if (arg < 0 || arg > 1)
760                         return (EINVAL);
761
762                 if (sc->light_set_supported) {
763                         Args.Count = 1;
764                         Args.Pointer = &Arg;
765                         Arg.Type = ACPI_TYPE_INTEGER;
766                         Arg.Integer.Value = arg ?
767                             sc->light_cmd_on : sc->light_cmd_off;
768
769                         status = AcpiEvaluateObject(sc->light_handle, NULL,
770                             &Args, NULL);
771                         if (ACPI_SUCCESS(status))
772                                 sc->light_val = arg;
773                         return (status);
774                 }
775                 break;
776
777         case ACPI_THINKPAD_METHOD_BLUETOOTH:
778                 if (arg < 0 || arg > 1)
779                         return (EINVAL);
780
781                 val = (arg == 1) ? sc->wlan_bt_flags |
782                     THINKPAD_NAME_MASK_BT :
783                     sc->wlan_bt_flags & (~THINKPAD_NAME_MASK_BT);
784                 return acpi_SetInteger(sc->handle, THINKPAD_NAME_WLAN_BT_SET,
785                     val);
786                 break;
787
788         case ACPI_THINKPAD_METHOD_FANLEVEL:
789                 if (arg < 0 || arg > 7)
790                         return (EINVAL);
791
792                 if (!sc->fan_handle) {
793                         /* Read the current fanstatus */
794                         ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_FANSTATUS,
795                             &val_ec, 1);
796                         val = val_ec & (~THINKPAD_EC_MASK_FANLEVEL);
797
798                         return ACPI_EC_WRITE(sc->ec_dev, THINKPAD_EC_FANSTATUS,
799                             val | arg, 1);
800                 }
801                 break;
802
803         case ACPI_THINKPAD_METHOD_FANSTATUS:
804                 if (arg < 0 || arg > 1)
805                         return (EINVAL);
806
807                 if (!sc->fan_handle) {
808                         /* Read the current fanstatus */
809                         ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_FANSTATUS,
810                             &val_ec, 1);
811
812                         return ACPI_EC_WRITE(sc->ec_dev, THINKPAD_EC_FANSTATUS,
813                             (arg == 1) ? (val_ec | THINKPAD_EC_MASK_FANSTATUS) :
814                             (val_ec & (~THINKPAD_EC_MASK_FANSTATUS)), 1);
815                 }
816                 break;
817         }
818
819         return (0);
820 }
821
822 static int
823 acpi_thinkpad_sysctl_init(struct acpi_thinkpad_softc *sc, int method)
824 {
825         int                     dummy;
826         ACPI_OBJECT_TYPE        cmos_t;
827         ACPI_HANDLE             ledb_handle;
828
829         switch (method) {
830         case ACPI_THINKPAD_METHOD_EVENTS:
831                 /* Events are disabled by default */
832                 return (TRUE);
833
834         case ACPI_THINKPAD_METHOD_EVENTMASK:
835                 return (sc->events_mask_supported);
836
837         case ACPI_THINKPAD_METHOD_HOTKEY:
838         case ACPI_THINKPAD_METHOD_BRIGHTNESS:
839         case ACPI_THINKPAD_METHOD_VOLUME:
840         case ACPI_THINKPAD_METHOD_MUTE:
841                 /* EC is required here, which was already checked before */
842                 return (TRUE);
843
844         case ACPI_THINKPAD_METHOD_THINKLIGHT:
845                 sc->cmos_handle = NULL;
846                 sc->light_get_supported = ACPI_SUCCESS(acpi_GetInteger(
847                     sc->ec_handle, THINKPAD_NAME_KEYLIGHT, &sc->light_val));
848
849                 if ((ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\UCMS",
850                     &sc->light_handle)) ||
851                     ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\CMOS",
852                     &sc->light_handle)) ||
853                     ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\CMS",
854                     &sc->light_handle))) &&
855                     ACPI_SUCCESS(AcpiGetType(sc->light_handle, &cmos_t)) &&
856                     cmos_t == ACPI_TYPE_METHOD) {
857                         sc->light_cmd_on = 0x0c;
858                         sc->light_cmd_off = 0x0d;
859                         sc->cmos_handle = sc->light_handle;
860                 }
861                 else if (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\LGHT",
862                     &sc->light_handle))) {
863                         sc->light_cmd_on = 1;
864                         sc->light_cmd_off = 0;
865                 }
866                 else
867                         sc->light_handle = NULL;
868
869                 sc->light_set_supported = (sc->light_handle &&
870                     ACPI_FAILURE(AcpiGetHandle(sc->ec_handle, "LEDB",
871                     &ledb_handle)));
872
873                 if (sc->light_get_supported)
874                         return (TRUE);
875
876                 if (sc->light_set_supported) {
877                         sc->light_val = 0;
878                         return (TRUE);
879                 }
880
881                 return (FALSE);
882
883         case ACPI_THINKPAD_METHOD_BLUETOOTH:
884         case ACPI_THINKPAD_METHOD_WLAN:
885                 if (ACPI_SUCCESS(acpi_GetInteger(sc->handle,
886                     THINKPAD_NAME_WLAN_BT_GET, &dummy)))
887                         return (TRUE);
888                 return (FALSE);
889
890         case ACPI_THINKPAD_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 = (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "GFAN",
896                     &sc->fan_handle)) ||
897                     ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\FSPD",
898                     &sc->fan_handle)));
899                 return (TRUE);
900
901         case ACPI_THINKPAD_METHOD_FANLEVEL:
902         case ACPI_THINKPAD_METHOD_FANSTATUS:
903                 /* 
904                  * Fan status is only supported on those models,
905                  * which report fan RPM contiguously, not in levels
906                  */
907                 if (sc->fan_levels)
908                         return (FALSE);
909                 return (TRUE);
910
911         case ACPI_THINKPAD_METHOD_THERMAL:
912                 if (ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle,
913                     THINKPAD_NAME_THERMAL_GET, &dummy))) {
914                         sc->thermal_updt_supported =
915                             ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle,
916                             THINKPAD_NAME_THERMAL_UPDT, &dummy));
917                         return (TRUE);
918                 }
919                 return (FALSE);
920         }
921         return (FALSE);
922 }
923
924 static void
925 acpi_thinkpad_notify(ACPI_HANDLE h, UINT32 notify, void *context)
926 {
927         int             event, arg, type;
928         device_t        dev = context;
929         struct acpi_thinkpad_softc *sc = device_get_softc(dev);
930
931         ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
932
933         if (notify != 0x80)
934                 device_printf(dev, "Unknown notify\n");
935
936         for (;;) {
937                 acpi_GetInteger(acpi_get_handle(dev), THINKPAD_NAME_EVENTS_GET,
938                     &event);
939
940                 if (event == 0)
941                         break;
942
943                 type = (event >> 12) & 0xf;
944                 arg = event & 0xfff;
945                 switch (type) {
946                 case 1:
947                         if (!(sc->events_availmask & (1 << (arg - 1)))) {
948                                 device_printf(dev, "Unknown key %d\n", arg);
949                                 break;
950                         }
951
952                         /* Notify devd(8) */
953                         acpi_UserNotify("THINKPAD", h, (arg & 0xff));
954                         break;
955                 default:
956                         break;
957                 }
958         }
959 }
960
961 static void
962 acpi_thinkpad_refresh(void *arg)
963 {
964         struct acpi_thinkpad_softc *sc = (struct acpi_thinkpad_softc *)arg;
965         char temp_cmd[] = "TMP0";
966         int data, i;
967         ACPI_INTEGER speed;
968
969         for (i = 0; i < THINKPAD_TEMP_SENSORS; i++) {
970                 temp_cmd[3] = '0' + i;
971                 
972                 /*
973                  * The TMPx methods seem to return +/- 128 or 0
974                  * when the respecting sensor is not available
975                  */
976                 sc->sensors[i].flags &= ~SENSOR_FINVALID;
977                 if (ACPI_FAILURE(acpi_GetInteger(sc->ec_handle, temp_cmd,
978                     &data)) || ABS(data) == 128 || data == 0) {
979                         sc->sensors[i].flags |= SENSOR_FINVALID;
980                         data = 0;
981                 }
982                 else if (sc->thermal_updt_supported) {
983                         /* Temperature is reported in tenth of Kelvin */
984                         sc->sensors[i].value = data * 100000;
985                 }
986                 sc->sensors[i].value = data * 1000000 + 273150000;
987         }
988         
989         sc->sensors[i].flags &= ~SENSOR_FINVALID;
990         if (sc->fan_handle) {
991                 if (ACPI_FAILURE(acpi_GetInteger(sc->fan_handle,
992                     NULL, &data)))
993                         sc->sensors[i].flags |= SENSOR_FINVALID;
994                         data = -1;
995         }
996         else {
997                 ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_FANSPEED, &speed, 2);
998                 data = speed;
999         }
1000
1001         sc->sensors[i].value = data;
1002 }