kernel: FWIW, add FreeBSD's led(4) framework.
[dragonfly.git] / sys / dev / acpica / 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: head/sys/dev/acpi_support/acpi_ibm.c 246128 2013-01-30 18:01:20Z sbz $
28  */
29
30 /*
31  * Driver for extra ACPI-controlled gadgets found on IBM and Lenovo ThinkPad
32  * laptops. Inspired by the ibm-acpi and tpb projects which implement these
33  * features on Linux.
34  *
35  *   acpi-ibm: <http://ibm-acpi.sourceforge.net/>
36  *        tpb: <http://www.nongnu.org/tpb/>
37  */
38
39 #include "opt_acpi.h"
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/bus.h>
43 #include <machine/cpufunc.h>
44 #include <sys/module.h>
45 #include <sys/sensors.h>
46 #include <sys/sbuf.h>
47 #include <sys/sysctl.h>
48 #include <sys/lock.h>
49 #include <sys/thread2.h>
50 #include <machine/clock.h>
51 #include <dev/misc/led/led.h>
52
53 #include "acpi.h"
54 #include "accommon.h"
55 #include "acpivar.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 #define ACPI_THINKPAD_METHOD_HANDLEREVENTS      14
75
76 /* Hotkeys/Buttons */
77 #define THINKPAD_RTC_HOTKEY1                    0x64
78 #define   THINKPAD_RTC_MASK_HOME                (1 << 0)
79 #define   THINKPAD_RTC_MASK_SEARCH              (1 << 1)
80 #define   THINKPAD_RTC_MASK_MAIL                (1 << 2)
81 #define   THINKPAD_RTC_MASK_WLAN                (1 << 5)
82 #define THINKPAD_RTC_HOTKEY2                    0x65
83 #define   THINKPAD_RTC_MASK_THINKPAD            (1 << 3)
84 #define   THINKPAD_RTC_MASK_ZOOM                (1 << 5)
85 #define   THINKPAD_RTC_MASK_VIDEO               (1 << 6)
86 #define   THINKPAD_RTC_MASK_HIBERNATE           (1 << 7)
87 #define THINKPAD_RTC_THINKLIGHT                 0x66
88 #define   THINKPAD_RTC_MASK_THINKLIGHT          (1 << 4)
89 #define THINKPAD_RTC_SCREENEXPAND               0x67
90 #define   THINKPAD_RTC_MASK_SCREENEXPAND        (1 << 5)
91 #define THINKPAD_RTC_BRIGHTNESS                 0x6c
92 #define   THINKPAD_RTC_MASK_BRIGHTNESS          (1 << 5)
93 #define THINKPAD_RTC_VOLUME                     0x6e
94 #define   THINKPAD_RTC_MASK_VOLUME              (1 << 7)
95
96 /* Embedded Controller registers */
97 #define THINKPAD_EC_BRIGHTNESS                  0x31
98 #define   THINKPAD_EC_MASK_BRI                  0x7
99 #define THINKPAD_EC_VOLUME                      0x30
100 #define   THINKPAD_EC_MASK_VOL                  0xf
101 #define   THINKPAD_EC_MASK_MUTE                 (1 << 6)
102 #define THINKPAD_EC_FANSTATUS                   0x2F
103 #define   THINKPAD_EC_MASK_FANLEVEL             0x3f
104 #define   THINKPAD_EC_MASK_FANDISENGAGED        (1 << 6)
105 #define   THINKPAD_EC_MASK_FANSTATUS            (1 << 7)
106 #define THINKPAD_EC_FANSPEED                    0x84
107
108 /* CMOS Commands */
109 #define THINKPAD_CMOS_VOLUME_DOWN               0
110 #define THINKPAD_CMOS_VOLUME_UP                 1
111 #define THINKPAD_CMOS_VOLUME_MUTE               2
112 #define THINKPAD_CMOS_BRIGHTNESS_UP             4
113 #define THINKPAD_CMOS_BRIGHTNESS_DOWN           5
114
115 /* ACPI methods */
116 #define THINKPAD_NAME_KEYLIGHT                  "KBLT"
117 #define THINKPAD_NAME_WLAN_BT_GET               "GBDC"
118 #define THINKPAD_NAME_WLAN_BT_SET               "SBDC"
119 #define   THINKPAD_NAME_MASK_BT                 (1 << 1)
120 #define   THINKPAD_NAME_MASK_WLAN               (1 << 2)
121 #define THINKPAD_NAME_THERMAL_GET               "TMP7"
122 #define THINKPAD_NAME_THERMAL_UPDT              "UPDT"
123
124 #define THINKPAD_NAME_EVENTS_STATUS_GET         "DHKC"
125 #define THINKPAD_NAME_EVENTS_MASK_GET           "DHKN"
126 #define THINKPAD_NAME_EVENTS_STATUS_SET         "MHKC"
127 #define THINKPAD_NAME_EVENTS_MASK_SET           "MHKM"
128 #define THINKPAD_NAME_EVENTS_GET                "MHKP"
129 #define THINKPAD_NAME_EVENTS_AVAILMASK          "MHKA"
130
131 #define THINKPAD_NUM_SENSORS                    9
132 #define THINKPAD_TEMP_SENSORS                   8
133
134 /* Event Code */
135 #define THINKPAD_EVENT_LCD_BACKLIGHT            0x03
136 #define THINKPAD_EVENT_SUSPEND_TO_RAM           0x04
137 #define THINKPAD_EVENT_BLUETOOTH                0x05
138 #define THINKPAD_EVENT_SCREEN_EXPAND            0x07
139 #define THINKPAD_EVENT_SUSPEND_TO_DISK          0x0c
140 #define THINKPAD_EVENT_BRIGHTNESS_UP            0x10
141 #define THINKPAD_EVENT_BRIGHTNESS_DOWN          0x11
142 #define THINKPAD_EVENT_THINKLIGHT               0x12
143 #define THINKPAD_EVENT_ZOOM                     0x14
144 #define THINKPAD_EVENT_VOLUME_UP                0x15
145 #define THINKPAD_EVENT_VOLUME_DOWN              0x16
146 #define THINKPAD_EVENT_MUTE                     0x17
147 #define THINKPAD_EVENT_ACCESS_THINKPAD_BUTTON   0x18
148
149 #define ABS(x) (((x) < 0)? -(x) : (x))
150
151 struct acpi_thinkpad_softc {
152         device_t        dev;
153         ACPI_HANDLE     handle;
154
155         /* Embedded controller */
156         device_t        ec_dev;
157         ACPI_HANDLE     ec_handle;
158
159         /* CMOS */
160         ACPI_HANDLE     cmos_handle;
161
162         /* Fan status */
163         ACPI_HANDLE     fan_handle;
164         int             fan_levels;
165
166         /* Keylight commands and states */
167         ACPI_HANDLE     light_handle;
168         int             light_cmd_on;
169         int             light_cmd_off;
170         int             light_val;
171         int             light_get_supported;
172         int             light_set_supported;
173
174         /* led(4) interface */
175         struct cdev     *led_dev;
176         int             led_busy;
177         int             led_state;
178
179         int             wlan_bt_flags;
180         int             thermal_updt_supported;
181
182         unsigned int    events_availmask;
183         unsigned int    events_initialmask;
184         int             events_mask_supported;
185         int             events_enable;
186
187         /* sensors(9) related */
188         struct ksensordev sensordev;
189         struct ksensor sensors[THINKPAD_NUM_SENSORS];
190
191         unsigned int    handler_events;
192
193         struct sysctl_ctx_list   sysctl_ctx;
194         struct sysctl_oid       *sysctl_tree;
195 };
196
197 static struct {
198         char    *name;
199         int     method;
200         char    *description;
201         int     access;
202 } acpi_thinkpad_sysctls[] = {
203         {
204                 .name           = "events",
205                 .method         = ACPI_THINKPAD_METHOD_EVENTS,
206                 .description    = "ACPI events enable",
207                 .access         = CTLTYPE_INT | CTLFLAG_RW
208         },
209         {
210                 .name           = "eventmask",
211                 .method         = ACPI_THINKPAD_METHOD_EVENTMASK,
212                 .description    = "ACPI eventmask",
213                 .access         = CTLTYPE_INT | CTLFLAG_RW
214         },
215         {
216                 .name           = "hotkey",
217                 .method         = ACPI_THINKPAD_METHOD_HOTKEY,
218                 .description    = "Key Status",
219                 .access         = CTLTYPE_INT | CTLFLAG_RD
220         },
221         {
222                 .name           = "lcd_brightness",
223                 .method         = ACPI_THINKPAD_METHOD_BRIGHTNESS,
224                 .description    = "LCD Brightness",
225                 .access         = CTLTYPE_INT | CTLFLAG_RW
226         },
227         {
228                 .name           = "volume",
229                 .method         = ACPI_THINKPAD_METHOD_VOLUME,
230                 .description    = "Volume",
231                 .access         = CTLTYPE_INT | CTLFLAG_RW
232         },
233         {
234                 .name           = "mute",
235                 .method         = ACPI_THINKPAD_METHOD_MUTE,
236                 .description    = "Mute",
237                 .access         = CTLTYPE_INT | CTLFLAG_RW
238         },
239         {
240                 .name           = "thinklight",
241                 .method         = ACPI_THINKPAD_METHOD_THINKLIGHT,
242                 .description    = "Thinklight enable",
243                 .access         = CTLTYPE_INT | CTLFLAG_RW
244         },
245         {
246                 .name           = "bluetooth",
247                 .method         = ACPI_THINKPAD_METHOD_BLUETOOTH,
248                 .description    = "Bluetooth enable",
249                 .access         = CTLTYPE_INT | CTLFLAG_RW
250         },
251         {
252                 .name           = "wlan",
253                 .method         = ACPI_THINKPAD_METHOD_WLAN,
254                 .description    = "WLAN enable",
255                 .access         = CTLTYPE_INT | CTLFLAG_RD
256         },
257         {
258                 .name           = "fan_speed",
259                 .method         = ACPI_THINKPAD_METHOD_FANSPEED,
260                 .description    = "Fan speed",
261                 .access         = CTLTYPE_INT | CTLFLAG_RD
262         },
263         {
264                 .name           = "fan_level",
265                 .method         = ACPI_THINKPAD_METHOD_FANLEVEL,
266                 .description    = "Fan level",
267                 .access         = CTLTYPE_INT | CTLFLAG_RW
268         },
269         {
270                 .name           = "fan",
271                 .method         = ACPI_THINKPAD_METHOD_FANSTATUS,
272                 .description    = "Fan enable",
273                 .access         = CTLTYPE_INT | CTLFLAG_RW
274         },
275
276         { NULL, 0, NULL, 0 }
277 };
278
279 ACPI_SERIAL_DECL(thinkpad, "ACPI Thinkpad extras");
280
281 static int      acpi_thinkpad_probe(device_t dev);
282 static int      acpi_thinkpad_attach(device_t dev);
283 static int      acpi_thinkpad_detach(device_t dev);
284 static int      acpi_thinkpad_resume(device_t dev);
285
286 static void     thinkpad_led(void *softc, int onoff);
287 static void     thinkpad_led_task(struct acpi_thinkpad_softc *sc, int pending __unused);
288
289 static int      acpi_thinkpad_sysctl(SYSCTL_HANDLER_ARGS);
290 static int      acpi_thinkpad_sysctl_init(struct acpi_thinkpad_softc *sc,
291                 int method);
292 static int      acpi_thinkpad_sysctl_get(struct acpi_thinkpad_softc *sc,
293                 int method);
294 static int      acpi_thinkpad_sysctl_set(struct acpi_thinkpad_softc *sc,
295                 int method, int val);
296
297 static int      acpi_thinkpad_eventmask_set(struct acpi_thinkpad_softc *sc,
298                 int val);
299 static int      acpi_thinkpad_handlerevents_sysctl(SYSCTL_HANDLER_ARGS);
300 static void     acpi_thinkpad_notify(ACPI_HANDLE h, UINT32 notify,
301                 void *context);
302 static void     acpi_thinkpad_refresh(void *);
303
304 static int      acpi_thinkpad_brightness_set(struct acpi_thinkpad_softc *sc, int arg);
305 static int      acpi_thinkpad_bluetooth_set(struct acpi_thinkpad_softc *sc, int arg);
306 static int      acpi_thinkpad_thinklight_set(struct acpi_thinkpad_softc *sc, int arg);
307 static int      acpi_thinkpad_volume_set(struct acpi_thinkpad_softc *sc, int arg);
308 static int      acpi_thinkpad_mute_set(struct acpi_thinkpad_softc *sc, int arg);
309
310 static device_method_t acpi_thinkpad_methods[] = {
311         /* Device interface */
312         DEVMETHOD(device_probe, acpi_thinkpad_probe),
313         DEVMETHOD(device_attach, acpi_thinkpad_attach),
314         DEVMETHOD(device_detach, acpi_thinkpad_detach),
315         DEVMETHOD(device_resume, acpi_thinkpad_resume),
316
317         DEVMETHOD_END
318 };
319
320 static driver_t acpi_thinkpad_driver = {
321         "acpi_thinkpad",
322         acpi_thinkpad_methods,
323         sizeof(struct acpi_thinkpad_softc),
324 };
325
326 static devclass_t acpi_thinkpad_devclass;
327
328 DRIVER_MODULE(acpi_thinkpad, acpi, acpi_thinkpad_driver,
329     acpi_thinkpad_devclass, NULL, NULL);
330 MODULE_DEPEND(acpi_thinkpad, acpi, 1, 1, 1);
331 static char    *thinkpad_ids[] = {"IBM0068", "LEN0068", NULL};
332
333 static void
334 thinkpad_led(void *softc, int onoff)
335 {
336         struct acpi_thinkpad_softc* sc = (struct acpi_thinkpad_softc*) softc;
337
338         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
339
340         if (sc->led_busy)
341                 return;
342
343         sc->led_busy = 1;
344         sc->led_state = onoff;
345
346         AcpiOsExecute(OSL_NOTIFY_HANDLER, (void *)thinkpad_led_task, sc);
347 }
348
349 static void
350 thinkpad_led_task(struct acpi_thinkpad_softc *sc, int pending __unused)
351 {
352         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
353
354         ACPI_SERIAL_BEGIN(thinkpad);
355         acpi_thinkpad_sysctl_set(sc, ACPI_THINKPAD_METHOD_THINKLIGHT, sc->led_state);
356         ACPI_SERIAL_END(thinkpad);
357
358         sc->led_busy = 0;
359 }
360
361 static int
362 acpi_thinkpad_probe(device_t dev)
363 {
364         if (acpi_disabled("thinkpad") ||
365             ACPI_ID_PROBE(device_get_parent(dev), dev, thinkpad_ids) == NULL ||
366             device_get_unit(dev) != 0) 
367                 return (ENXIO);
368
369         device_set_desc(dev, "IBM/Lenovo ThinkPad ACPI Extras");
370         return (0);
371 }
372
373 static int
374 acpi_thinkpad_attach(device_t dev)
375 {
376         struct acpi_thinkpad_softc      *sc;
377         struct acpi_softc       *acpi_sc;
378         devclass_t              ec_devclass;
379         int                     i;
380
381         ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
382
383         sc = device_get_softc(dev);
384         sc->dev = dev;
385         sc->handle = acpi_get_handle(dev);
386
387         acpi_sc = acpi_device_get_parent_softc(dev);
388
389         /* Look for the first embedded controller */
390         if (!(ec_devclass = devclass_find ("acpi_ec"))) {
391                 if (bootverbose)
392                         device_printf(dev, "Couldn't find acpi_ec devclass\n");
393                 return (EINVAL);
394         }
395         if (!(sc->ec_dev = devclass_get_device(ec_devclass, 0))) {
396                 if (bootverbose)
397                         device_printf(dev, "Couldn't find acpi_ec device\n");
398                 return (EINVAL);
399         }
400         sc->ec_handle = acpi_get_handle(sc->ec_dev);
401         
402         sysctl_ctx_init(&sc->sysctl_ctx);
403         sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
404             SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO,
405             "thinkpad", CTLFLAG_RD, 0, "");
406
407         /* Look for event mask and hook up the nodes */
408         sc->events_mask_supported = ACPI_SUCCESS(acpi_GetInteger(sc->handle,
409             THINKPAD_NAME_EVENTS_MASK_GET, &sc->events_initialmask));
410
411         if (sc->events_mask_supported) {
412                 SYSCTL_ADD_UINT(&sc->sysctl_ctx,
413                     SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
414                     "initialmask", CTLFLAG_RD,
415                     &sc->events_initialmask, 0, "Initial eventmask");
416
417                 /* The availmask is the bitmask of supported events */
418                 if (ACPI_FAILURE(acpi_GetInteger(sc->handle,
419                     THINKPAD_NAME_EVENTS_AVAILMASK, &sc->events_availmask)))
420                         sc->events_availmask = 0xffffffff;
421
422                 SYSCTL_ADD_UINT(&sc->sysctl_ctx,
423                     SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
424                     "availmask", CTLFLAG_RD,
425                     &sc->events_availmask, 0, "Mask of supported events");
426         }
427
428         /* Hook up proc nodes */
429         for (i = 0; acpi_thinkpad_sysctls[i].name != NULL; i++) {
430                 if (!acpi_thinkpad_sysctl_init(sc,
431                     acpi_thinkpad_sysctls[i].method))
432                         continue;
433
434                 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
435                     SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
436                     acpi_thinkpad_sysctls[i].name,
437                     acpi_thinkpad_sysctls[i].access,
438                     sc, i, acpi_thinkpad_sysctl, "I",
439                     acpi_thinkpad_sysctls[i].description);
440         }
441
442         /* Hook up handlerevents node */
443         if (acpi_thinkpad_sysctl_init(sc, ACPI_THINKPAD_METHOD_HANDLEREVENTS)) {
444                 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
445                     SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
446                     "handlerevents", CTLTYPE_STRING | CTLFLAG_RW,
447                     sc, 0, acpi_thinkpad_handlerevents_sysctl, "I",
448                     "devd(8) events handled by acpi_thinkpad");
449         }
450  
451         /* Handle notifies */
452         AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
453             acpi_thinkpad_notify, dev);
454
455         /* Attach sensors(9). */
456         if (sensor_task_register(sc, acpi_thinkpad_refresh, 5)) {
457                 device_printf(sc->dev, "unable to register update task\n");
458                 return 1;
459         }
460
461         strlcpy(sc->sensordev.xname, device_get_nameunit(sc->dev),
462             sizeof(sc->sensordev.xname));
463
464         for (i = 0; i < THINKPAD_TEMP_SENSORS; i++) {
465                 sc->sensors[i].type = SENSOR_TEMP;
466                 sensor_attach(&sc->sensordev, &sc->sensors[i]);
467         }
468         
469         sc->sensors[i].type = SENSOR_FANRPM;
470         sensor_attach(&sc->sensordev, &sc->sensors[i]);
471
472         sensordev_install(&sc->sensordev);
473
474         /* Hook up light to led(4) */
475         if (sc->light_set_supported)
476                 sc->led_dev = led_create_state(thinkpad_led, sc, "thinklight", sc->light_val);
477
478         return (0);
479 }
480
481 static int
482 acpi_thinkpad_detach(device_t dev)
483 {
484         int i;
485
486         ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
487
488         struct acpi_thinkpad_softc *sc = device_get_softc(dev);
489
490         /* Disable events and restore eventmask */
491         ACPI_SERIAL_BEGIN(thinkpad);
492         acpi_thinkpad_sysctl_set(sc, ACPI_THINKPAD_METHOD_EVENTS, 0);
493         acpi_thinkpad_sysctl_set(sc, ACPI_THINKPAD_METHOD_EVENTMASK,
494             sc->events_initialmask);
495         ACPI_SERIAL_END(thinkpad);
496
497         AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
498             acpi_thinkpad_notify);
499
500         if (sc->sysctl_tree != NULL)
501                 sysctl_ctx_free(&sc->sysctl_ctx);
502
503         sensordev_deinstall(&sc->sensordev);
504         for (i = 0; i < THINKPAD_NUM_SENSORS; i++)
505                 sensor_detach(&sc->sensordev, &sc->sensors[i]);
506         sensor_task_unregister(sc);
507
508         if (sc->led_dev != NULL)
509                 led_destroy(sc->led_dev);
510
511         return (0);
512 }
513
514 static int
515 acpi_thinkpad_resume(device_t dev)
516 {
517         struct acpi_thinkpad_softc *sc = device_get_softc(dev);
518
519         ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
520
521         ACPI_SERIAL_BEGIN(thinkpad);
522         for (int i = 0; acpi_thinkpad_sysctls[i].name != NULL; i++) {
523                 int val;
524
525                 if ((acpi_thinkpad_sysctls[i].access & CTLFLAG_RD) == 0) {
526                         continue;
527                 }
528
529                 val = acpi_thinkpad_sysctl_get(sc, i);
530
531                 if ((acpi_thinkpad_sysctls[i].access & CTLFLAG_WR) == 0) {
532                         continue;
533                 }
534
535                 acpi_thinkpad_sysctl_set(sc, i, val);
536         }
537         ACPI_SERIAL_END(thinkpad);
538
539         return (0);
540 }
541
542 static int
543 acpi_thinkpad_eventmask_set(struct acpi_thinkpad_softc *sc, int val)
544 {
545         int i;
546         ACPI_OBJECT             arg[2];
547         ACPI_OBJECT_LIST        args;
548         ACPI_STATUS             status;
549
550         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
551         ACPI_SERIAL_ASSERT(thinkpad);
552
553         args.Count = 2;
554         args.Pointer = arg;
555         arg[0].Type = ACPI_TYPE_INTEGER;
556         arg[1].Type = ACPI_TYPE_INTEGER;
557
558         for (i = 0; i < 32; ++i) {
559                 arg[0].Integer.Value = i+1;
560                 arg[1].Integer.Value = (((1 << i) & val) != 0);
561                 status = AcpiEvaluateObject(sc->handle,
562                     THINKPAD_NAME_EVENTS_MASK_SET, &args, NULL);
563
564                 if (ACPI_FAILURE(status))
565                         return (status);
566         }
567
568         return (0);
569 }
570
571 static int
572 acpi_thinkpad_sysctl(SYSCTL_HANDLER_ARGS)
573 {
574         struct acpi_thinkpad_softc      *sc;
575         int                     arg;
576         int                     error = 0;
577         int                     function;
578         int                     method;
579         
580         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
581
582         sc = (struct acpi_thinkpad_softc *)oidp->oid_arg1;
583         function = oidp->oid_arg2;
584         method = acpi_thinkpad_sysctls[function].method;
585
586         ACPI_SERIAL_BEGIN(thinkpad);
587         arg = acpi_thinkpad_sysctl_get(sc, method);
588         error = sysctl_handle_int(oidp, &arg, 0, req);
589
590         /* Sanity check */
591         if (error != 0 || req->newptr == NULL)
592                 goto out;
593
594         /* Update */
595         error = acpi_thinkpad_sysctl_set(sc, method, arg);
596
597 out:
598         ACPI_SERIAL_END(thinkpad);
599         return (error);
600 }
601
602 static int
603 acpi_thinkpad_sysctl_get(struct acpi_thinkpad_softc *sc, int method)
604 {
605         UINT64          val_ec;
606         int             val = 0, key;
607
608         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
609         ACPI_SERIAL_ASSERT(thinkpad);
610
611         switch (method) {
612         case ACPI_THINKPAD_METHOD_EVENTS:
613                 acpi_GetInteger(sc->handle, THINKPAD_NAME_EVENTS_STATUS_GET,
614                     &val);
615                 break;
616
617         case ACPI_THINKPAD_METHOD_EVENTMASK:
618                 if (sc->events_mask_supported)
619                         acpi_GetInteger(sc->handle,
620                             THINKPAD_NAME_EVENTS_MASK_GET, &val);
621                 break;
622
623         case ACPI_THINKPAD_METHOD_HOTKEY:
624                 /*
625                  * Construct the hotkey as a bitmask as illustrated below.
626                  * Note that whenever a key was pressed, the respecting bit
627                  * toggles and nothing else changes.
628                  * +--+--+-+-+-+-+-+-+-+-+-+-+
629                  * |11|10|9|8|7|6|5|4|3|2|1|0|
630                  * +--+--+-+-+-+-+-+-+-+-+-+-+
631                  *   |  | | | | | | | | | | |
632                  *   |  | | | | | | | | | | +- Home Button
633                  *   |  | | | | | | | | | +--- Search Button
634                  *   |  | | | | | | | | +----- Mail Button
635                  *   |  | | | | | | | +------- Thinkpad Button
636                  *   |  | | | | | | +--------- Zoom (Fn + Space)
637                  *   |  | | | | | +----------- WLAN Button
638                  *   |  | | | | +------------- Video Button
639                  *   |  | | | +--------------- Hibernate Button
640                  *   |  | | +----------------- Thinklight Button
641                  *   |  | +------------------- Screen expand (Fn + F8)
642                  *   |  +--------------------- Brightness
643                  *   +------------------------ Volume/Mute
644                  */
645                 key = rtcin(THINKPAD_RTC_HOTKEY1);
646                 val = (THINKPAD_RTC_MASK_HOME | THINKPAD_RTC_MASK_SEARCH |
647                     THINKPAD_RTC_MASK_MAIL | THINKPAD_RTC_MASK_WLAN) & key;
648                 key = rtcin(THINKPAD_RTC_HOTKEY2);
649                 val |= (THINKPAD_RTC_MASK_THINKPAD | THINKPAD_RTC_MASK_VIDEO |
650                     THINKPAD_RTC_MASK_HIBERNATE) & key;
651                 val |= (THINKPAD_RTC_MASK_ZOOM & key) >> 1;
652                 key = rtcin(THINKPAD_RTC_THINKLIGHT);
653                 val |= (THINKPAD_RTC_MASK_THINKLIGHT & key) << 4;
654                 key = rtcin(THINKPAD_RTC_SCREENEXPAND);
655                 val |= (THINKPAD_RTC_MASK_THINKLIGHT & key) << 4;
656                 key = rtcin(THINKPAD_RTC_BRIGHTNESS);
657                 val |= (THINKPAD_RTC_MASK_BRIGHTNESS & key) << 5;
658                 key = rtcin(THINKPAD_RTC_VOLUME);
659                 val |= (THINKPAD_RTC_MASK_VOLUME & key) << 4;
660                 break;
661
662         case ACPI_THINKPAD_METHOD_BRIGHTNESS:
663                 ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_BRIGHTNESS, &val_ec, 1);
664                 val = val_ec & THINKPAD_EC_MASK_BRI;
665                 break;
666
667         case ACPI_THINKPAD_METHOD_VOLUME:
668                 ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_VOLUME, &val_ec, 1);
669                 val = val_ec & THINKPAD_EC_MASK_VOL;
670                 break;
671
672         case ACPI_THINKPAD_METHOD_MUTE:
673                 ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_VOLUME, &val_ec, 1);
674                 val = ((val_ec & THINKPAD_EC_MASK_MUTE) ==
675                     THINKPAD_EC_MASK_MUTE);
676                 break;
677
678         case ACPI_THINKPAD_METHOD_THINKLIGHT:
679                 if (sc->light_get_supported)
680                         acpi_GetInteger(sc->ec_handle, THINKPAD_NAME_KEYLIGHT,
681                             &val);
682                 else
683                         val = sc->light_val;
684                 break;
685
686         case ACPI_THINKPAD_METHOD_BLUETOOTH:
687                 acpi_GetInteger(sc->handle, THINKPAD_NAME_WLAN_BT_GET, &val);
688                 sc->wlan_bt_flags = val;
689                 val = ((val & THINKPAD_NAME_MASK_BT) != 0);
690                 break;
691
692         case ACPI_THINKPAD_METHOD_WLAN:
693                 acpi_GetInteger(sc->handle, THINKPAD_NAME_WLAN_BT_GET, &val);
694                 sc->wlan_bt_flags = val;
695                 val = ((val & THINKPAD_NAME_MASK_WLAN) != 0);
696                 break;
697
698         case ACPI_THINKPAD_METHOD_FANSPEED:
699                 if (sc->fan_handle) {
700                         if (ACPI_FAILURE(acpi_GetInteger(sc->fan_handle,
701                             NULL, &val)))
702                                 val = -1;
703                 }
704                 else {
705                         ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_FANSPEED,
706                             &val_ec, 2);
707                         val = val_ec;
708                 }
709                 break;
710
711         case ACPI_THINKPAD_METHOD_FANLEVEL:
712                 /*
713                  * The THINKPAD_EC_FANSTATUS register works as follows:
714                  * Bit 0-5 indicate the level at which the fan operates. Only
715                  *       values between 0 and 7 have an effect. Everything
716                  *       above 7 is treated the same as level 7
717                  * Bit 6 overrides the fan speed limit if set to 1
718                  * Bit 7 indicates at which mode the fan operates:
719                  *       manual (0) or automatic (1)
720                  */
721                 if (!sc->fan_handle) {
722                         ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_FANSTATUS,
723                             &val_ec, 1);
724                         val = val_ec & THINKPAD_EC_MASK_FANLEVEL;
725                 }
726                 break;
727
728         case ACPI_THINKPAD_METHOD_FANSTATUS:
729                 if (!sc->fan_handle) {
730                         ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_FANSTATUS,
731                             &val_ec, 1);
732                         val = (val_ec & THINKPAD_EC_MASK_FANSTATUS) ==
733                             THINKPAD_EC_MASK_FANSTATUS;
734                 }
735                 else
736                         val = -1;
737                 break;
738         }
739
740         return (val);
741 }
742
743 static int
744 acpi_thinkpad_sysctl_set(struct acpi_thinkpad_softc *sc, int method, int arg)
745 {
746         int                     val;
747         UINT64                  val_ec;
748         ACPI_STATUS             status;
749
750         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
751         ACPI_SERIAL_ASSERT(thinkpad);
752
753         switch (method) {
754         case ACPI_THINKPAD_METHOD_EVENTS:
755                 if (arg < 0 || arg > 1)
756                         return (EINVAL);
757
758                 status = acpi_SetInteger(sc->handle,
759                     THINKPAD_NAME_EVENTS_STATUS_SET, arg);
760                 if (ACPI_FAILURE(status))
761                         return (status);
762                 if (sc->events_mask_supported)
763                         return acpi_thinkpad_eventmask_set(sc,
764                             sc->events_availmask);
765                 break;
766
767         case ACPI_THINKPAD_METHOD_EVENTMASK:
768                 if (sc->events_mask_supported)
769                         return acpi_thinkpad_eventmask_set(sc, arg);
770                 break;
771
772         case ACPI_THINKPAD_METHOD_BRIGHTNESS:
773                 return acpi_thinkpad_brightness_set(sc, arg);
774                 break;
775
776         case ACPI_THINKPAD_METHOD_VOLUME:
777                 return acpi_thinkpad_volume_set(sc, arg);
778                 break;
779
780         case ACPI_THINKPAD_METHOD_MUTE:
781                 return acpi_thinkpad_mute_set(sc, arg);
782                 break;
783
784         case ACPI_THINKPAD_METHOD_THINKLIGHT:
785                 return acpi_thinkpad_thinklight_set(sc, arg);
786                 break;
787
788         case ACPI_THINKPAD_METHOD_BLUETOOTH:
789                 return acpi_thinkpad_bluetooth_set(sc, arg);
790                 break;
791
792         case ACPI_THINKPAD_METHOD_FANLEVEL:
793                 if (arg < 0 || arg > 7)
794                         return (EINVAL);
795
796                 if (!sc->fan_handle) {
797                         /* Read the current fanstatus */
798                         ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_FANSTATUS,
799                             &val_ec, 1);
800                         val = val_ec & (~THINKPAD_EC_MASK_FANLEVEL);
801
802                         return ACPI_EC_WRITE(sc->ec_dev, THINKPAD_EC_FANSTATUS,
803                             val | arg, 1);
804                 }
805                 break;
806
807         case ACPI_THINKPAD_METHOD_FANSTATUS:
808                 if (arg < 0 || arg > 1)
809                         return (EINVAL);
810
811                 if (!sc->fan_handle) {
812                         /* Read the current fanstatus */
813                         ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_FANSTATUS,
814                             &val_ec, 1);
815
816                         return ACPI_EC_WRITE(sc->ec_dev, THINKPAD_EC_FANSTATUS,
817                             (arg == 1) ? (val_ec | THINKPAD_EC_MASK_FANSTATUS) :
818                             (val_ec & (~THINKPAD_EC_MASK_FANSTATUS)), 1);
819                 }
820                 break;
821         }
822
823         return (0);
824 }
825
826 static int
827 acpi_thinkpad_sysctl_init(struct acpi_thinkpad_softc *sc, int method)
828 {
829         int                     dummy;
830         ACPI_OBJECT_TYPE        cmos_t;
831         ACPI_HANDLE             ledb_handle;
832
833         switch (method) {
834         case ACPI_THINKPAD_METHOD_EVENTS:
835                 /* Events are disabled by default */
836                 return (TRUE);
837
838         case ACPI_THINKPAD_METHOD_EVENTMASK:
839                 return (sc->events_mask_supported);
840
841         case ACPI_THINKPAD_METHOD_HOTKEY:
842         case ACPI_THINKPAD_METHOD_BRIGHTNESS:
843         case ACPI_THINKPAD_METHOD_VOLUME:
844         case ACPI_THINKPAD_METHOD_MUTE:
845                 /* EC is required here, which was already checked before */
846                 return (TRUE);
847
848         case ACPI_THINKPAD_METHOD_THINKLIGHT:
849                 sc->cmos_handle = NULL;
850                 sc->light_get_supported = ACPI_SUCCESS(acpi_GetInteger(
851                     sc->ec_handle, THINKPAD_NAME_KEYLIGHT, &sc->light_val));
852
853                 if ((ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\UCMS",
854                     &sc->light_handle)) ||
855                     ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\CMOS",
856                     &sc->light_handle)) ||
857                     ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\CMS",
858                     &sc->light_handle))) &&
859                     ACPI_SUCCESS(AcpiGetType(sc->light_handle, &cmos_t)) &&
860                     cmos_t == ACPI_TYPE_METHOD) {
861                         sc->light_cmd_on = 0x0c;
862                         sc->light_cmd_off = 0x0d;
863                         sc->cmos_handle = sc->light_handle;
864                 }
865                 else if (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\LGHT",
866                     &sc->light_handle))) {
867                         sc->light_cmd_on = 1;
868                         sc->light_cmd_off = 0;
869                 }
870                 else
871                         sc->light_handle = NULL;
872
873                 sc->light_set_supported = (sc->light_handle &&
874                     ACPI_FAILURE(AcpiGetHandle(sc->ec_handle, "LEDB",
875                     &ledb_handle)));
876
877                 if (sc->light_get_supported)
878                         return (TRUE);
879
880                 if (sc->light_set_supported) {
881                         sc->light_val = 0;
882                         return (TRUE);
883                 }
884
885                 return (FALSE);
886
887         case ACPI_THINKPAD_METHOD_BLUETOOTH:
888         case ACPI_THINKPAD_METHOD_WLAN:
889                 if (ACPI_SUCCESS(acpi_GetInteger(sc->handle,
890                     THINKPAD_NAME_WLAN_BT_GET, &dummy)))
891                         return (TRUE);
892                 return (FALSE);
893
894         case ACPI_THINKPAD_METHOD_FANSPEED:
895                 /* 
896                  * Some models report the fan speed in levels from 0-7
897                  * Newer models report it contiguously
898                  */
899                 sc->fan_levels = (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "GFAN",
900                     &sc->fan_handle)) ||
901                     ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\FSPD",
902                     &sc->fan_handle)));
903                 return (TRUE);
904
905         case ACPI_THINKPAD_METHOD_FANLEVEL:
906         case ACPI_THINKPAD_METHOD_FANSTATUS:
907                 /* 
908                  * Fan status is only supported on those models,
909                  * which report fan RPM contiguously, not in levels
910                  */
911                 if (sc->fan_levels)
912                         return (FALSE);
913                 return (TRUE);
914
915         case ACPI_THINKPAD_METHOD_THERMAL:
916                 if (ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle,
917                     THINKPAD_NAME_THERMAL_GET, &dummy))) {
918                         sc->thermal_updt_supported =
919                             ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle,
920                             THINKPAD_NAME_THERMAL_UPDT, &dummy));
921                         return (TRUE);
922                 }
923                 return (FALSE);
924
925         case ACPI_THINKPAD_METHOD_HANDLEREVENTS:
926                 return (TRUE);
927         }
928         return (FALSE);
929 }
930
931 static int
932 acpi_thinkpad_handlerevents_sysctl(SYSCTL_HANDLER_ARGS)
933 {
934         struct acpi_thinkpad_softc      *sc;
935         int                     error = 0;
936         struct sbuf             sb;
937         char                    *cp, *ep;
938         int                     l, val;
939         unsigned int            handler_events;
940
941         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
942
943         sc = (struct acpi_thinkpad_softc *)oidp->oid_arg1;
944
945         if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL)
946                 return (ENOMEM);
947
948         ACPI_SERIAL_BEGIN(thinkpad);
949
950         /* Get old values if this is a get request. */
951         if (req->newptr == NULL) {
952                 for (int i = 0; i < 8 * sizeof(sc->handler_events); i++)
953                         if (sc->handler_events & (1 << i))
954                                 sbuf_printf(&sb, "0x%02x ", i + 1);
955                 if (sbuf_len(&sb) == 0)
956                         sbuf_printf(&sb, "NONE");
957         }
958
959         sbuf_trim(&sb);
960         sbuf_finish(&sb);
961
962         /* Copy out the old values to the user. */
963         error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
964         sbuf_delete(&sb);
965
966         if (error != 0 || req->newptr == NULL)
967                 goto out;
968
969         /* If the user is setting a string, parse it. */
970         handler_events = 0;
971         cp = (char *)req->newptr;
972         while (*cp) {
973                 if (isspace(*cp)) {
974                         cp++;
975                         continue;
976                 }
977
978                 ep = cp;
979
980                 while (*ep && !isspace(*ep))
981                         ep++;
982
983                 l = ep - cp;
984                 if (l == 0)
985                         break;
986
987                 if (strncmp(cp, "NONE", 4) == 0) {
988                         cp = ep;
989                         continue;
990                 }
991
992                 if (l >= 3 && cp[0] == '0' && (cp[1] == 'X' || cp[1] == 'x'))
993                         val = strtoul(cp, &ep, 16);
994                 else
995                         val = strtoul(cp, &ep, 10);
996
997                 if (val == 0 || ep == cp || val >= 8 * sizeof(handler_events)) {
998                         cp[l] = '\0';
999                         device_printf(sc->dev, "invalid event code: %s\n", cp);
1000                         error = EINVAL;
1001                         goto out;
1002                 }
1003
1004                 handler_events |= 1 << (val - 1);
1005
1006                 cp = ep;
1007         }
1008
1009         sc->handler_events = handler_events;
1010 out:
1011         ACPI_SERIAL_END(thinkpad);
1012         return (error);
1013 }
1014
1015 static int
1016 acpi_thinkpad_brightness_set(struct acpi_thinkpad_softc *sc, int arg)
1017 {
1018         int                     val, step;
1019         UINT64                  val_ec;
1020         ACPI_OBJECT             Arg;
1021         ACPI_OBJECT_LIST        Args;
1022         ACPI_STATUS             status;
1023
1024         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1025         ACPI_SERIAL_ASSERT(thinkpad);
1026
1027         if (arg < 0 || arg > 7)
1028                 return (EINVAL);
1029
1030         /* Read the current brightness */
1031         status = ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_BRIGHTNESS, &val_ec, 1);
1032         if (ACPI_FAILURE(status))
1033                 return (status);
1034
1035         if (sc->cmos_handle) {
1036                 val = val_ec & THINKPAD_EC_MASK_BRI;
1037
1038                 Args.Count = 1;
1039                 Args.Pointer = &Arg;
1040                 Arg.Type = ACPI_TYPE_INTEGER;
1041                 Arg.Integer.Value = (arg > val) ? THINKPAD_CMOS_BRIGHTNESS_UP :
1042                                                   THINKPAD_CMOS_BRIGHTNESS_DOWN;
1043
1044                 step = (arg > val) ? 1 : -1;
1045                 for (int i = val; i != arg; i += step) {
1046                         status = AcpiEvaluateObject(sc->cmos_handle, NULL,
1047                                                     &Args, NULL);
1048                         if (ACPI_FAILURE(status)) {
1049                                 /* Record the last value */
1050                                 if (i != val) {
1051                                         ACPI_EC_WRITE(sc->ec_dev,
1052                                             THINKPAD_EC_BRIGHTNESS, i - step, 1);
1053                                 }
1054                                 return (status);
1055                         }
1056                 }
1057         }
1058
1059         return ACPI_EC_WRITE(sc->ec_dev, THINKPAD_EC_BRIGHTNESS, arg, 1);
1060 }
1061
1062 static int
1063 acpi_thinkpad_bluetooth_set(struct acpi_thinkpad_softc *sc, int arg)
1064 {
1065         int                     val;
1066
1067         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1068         ACPI_SERIAL_ASSERT(thinkpad);
1069
1070         if (arg < 0 || arg > 1)
1071                 return (EINVAL);
1072
1073         val = (arg == 1) ? sc->wlan_bt_flags | THINKPAD_NAME_MASK_BT :
1074                            sc->wlan_bt_flags & (~THINKPAD_NAME_MASK_BT);
1075         return acpi_SetInteger(sc->handle, THINKPAD_NAME_WLAN_BT_SET, val);
1076 }
1077
1078 static int
1079 acpi_thinkpad_thinklight_set(struct acpi_thinkpad_softc *sc, int arg)
1080 {
1081         ACPI_OBJECT             Arg;
1082         ACPI_OBJECT_LIST        Args;
1083         ACPI_STATUS             status;
1084
1085         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1086         ACPI_SERIAL_ASSERT(thinkpad);
1087
1088         if (arg < 0 || arg > 1)
1089                 return (EINVAL);
1090
1091         if (sc->light_set_supported) {
1092                 Args.Count = 1;
1093                 Args.Pointer = &Arg;
1094                 Arg.Type = ACPI_TYPE_INTEGER;
1095                 Arg.Integer.Value = arg ? sc->light_cmd_on : sc->light_cmd_off;
1096
1097                 status = AcpiEvaluateObject(sc->light_handle, NULL,
1098                                             &Args, NULL);
1099                 if (ACPI_SUCCESS(status))
1100                         sc->light_val = arg;
1101                 return (status);
1102         }
1103
1104         return (0);
1105 }
1106
1107 static int
1108 acpi_thinkpad_volume_set(struct acpi_thinkpad_softc *sc, int arg)
1109 {
1110         int                     val, step;
1111         UINT64                  val_ec;
1112         ACPI_OBJECT             Arg;
1113         ACPI_OBJECT_LIST        Args;
1114         ACPI_STATUS             status;
1115
1116         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1117         ACPI_SERIAL_ASSERT(thinkpad);
1118
1119         if (arg < 0 || arg > 14)
1120                 return (EINVAL);
1121
1122         /* Read the current volume */
1123         status = ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_VOLUME, &val_ec, 1);
1124         if (ACPI_FAILURE(status))
1125                 return (status);
1126
1127         if (sc->cmos_handle) {
1128                 val = val_ec & THINKPAD_EC_MASK_VOL;
1129
1130                 Args.Count = 1;
1131                 Args.Pointer = &Arg;
1132                 Arg.Type = ACPI_TYPE_INTEGER;
1133                 Arg.Integer.Value = (arg > val) ? THINKPAD_CMOS_VOLUME_UP :
1134                                                   THINKPAD_CMOS_VOLUME_DOWN;
1135
1136                 step = (arg > val) ? 1 : -1;
1137                 for (int i = val; i != arg; i += step) {
1138                         status = AcpiEvaluateObject(sc->cmos_handle, NULL,
1139                                                     &Args, NULL);
1140                         if (ACPI_FAILURE(status)) {
1141                                 /* Record the last value */
1142                                 if (i != val) {
1143                                         val_ec = i - step +
1144                                                  (val_ec & (~THINKPAD_EC_MASK_VOL));
1145                                         ACPI_EC_WRITE(sc->ec_dev, THINKPAD_EC_VOLUME,
1146                                                       val_ec, 1);
1147                                 }
1148                                 return (status);
1149                         }
1150                 }
1151         }
1152
1153         val_ec = arg + (val_ec & (~THINKPAD_EC_MASK_VOL));
1154         return ACPI_EC_WRITE(sc->ec_dev, THINKPAD_EC_VOLUME, val_ec, 1);
1155 }
1156
1157 static int
1158 acpi_thinkpad_mute_set(struct acpi_thinkpad_softc *sc, int arg)
1159 {
1160         UINT64                  val_ec;
1161         ACPI_OBJECT             Arg;
1162         ACPI_OBJECT_LIST        Args;
1163         ACPI_STATUS             status;
1164
1165         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1166         ACPI_SERIAL_ASSERT(thinkpad);
1167
1168         if (arg < 0 || arg > 1)
1169                 return (EINVAL);
1170
1171         status = ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_VOLUME, &val_ec, 1);
1172         if (ACPI_FAILURE(status))
1173                 return (status);
1174
1175         if (sc->cmos_handle) {
1176                 Args.Count = 1;
1177                 Args.Pointer = &Arg;
1178                 Arg.Type = ACPI_TYPE_INTEGER;
1179                 Arg.Integer.Value = THINKPAD_CMOS_VOLUME_MUTE;
1180
1181                 status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL);
1182                 if (ACPI_FAILURE(status))
1183                         return (status);
1184         }
1185
1186         val_ec = (arg == 1) ? val_ec | THINKPAD_EC_MASK_MUTE :
1187                               val_ec & (~THINKPAD_EC_MASK_MUTE);
1188         return ACPI_EC_WRITE(sc->ec_dev, THINKPAD_EC_VOLUME, val_ec, 1);
1189 }
1190
1191 static void
1192 acpi_thinkpad_eventhandler(struct acpi_thinkpad_softc *sc, int arg)
1193 {
1194         int                     val;
1195         UINT64                  val_ec;
1196         ACPI_STATUS             status;
1197
1198         ACPI_SERIAL_BEGIN(thinkpad);
1199         switch (arg) {
1200 #if 0 /* XXX */
1201         case THINKPAD_EVENT_SUSPEND_TO_RAM:
1202                 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
1203                 break;
1204 #endif
1205
1206         case THINKPAD_EVENT_BLUETOOTH:
1207                 acpi_thinkpad_bluetooth_set(sc, (sc->wlan_bt_flags == 0));
1208                 break;
1209
1210         case THINKPAD_EVENT_BRIGHTNESS_UP:
1211         case THINKPAD_EVENT_BRIGHTNESS_DOWN:
1212                 /* Read the current brightness */
1213                 status = ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_BRIGHTNESS,
1214                                       &val_ec, 1);
1215                 if (ACPI_FAILURE(status))
1216                         goto done;
1217
1218                 val = val_ec & THINKPAD_EC_MASK_BRI;
1219                 val = (arg == THINKPAD_EVENT_BRIGHTNESS_UP) ? val + 1 : val - 1;
1220                 acpi_thinkpad_brightness_set(sc, val);
1221                 break;
1222
1223         case THINKPAD_EVENT_THINKLIGHT:
1224                 acpi_thinkpad_thinklight_set(sc, (sc->light_val == 0));
1225                 break;
1226
1227         case THINKPAD_EVENT_VOLUME_UP:
1228         case THINKPAD_EVENT_VOLUME_DOWN:
1229                 /* Read the current volume */
1230                 status = ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_VOLUME, &val_ec, 1);
1231                 if (ACPI_FAILURE(status))
1232                         goto done;
1233
1234                 val = val_ec & THINKPAD_EC_MASK_VOL;
1235                 val = (arg == THINKPAD_EVENT_VOLUME_UP) ? val + 1 : val - 1;
1236                 acpi_thinkpad_volume_set(sc, val);
1237                 break;
1238
1239         case THINKPAD_EVENT_MUTE:
1240                 /* Read the current value */
1241                 status = ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_VOLUME, &val_ec, 1);
1242                 if (ACPI_FAILURE(status))
1243                         goto done;
1244
1245                 val = ((val_ec & THINKPAD_EC_MASK_MUTE) == THINKPAD_EC_MASK_MUTE);
1246                 acpi_thinkpad_mute_set(sc, (val == 0));
1247                 break;
1248
1249         default:
1250                 break;
1251         }
1252 done:
1253         ACPI_SERIAL_END(thinkpad);
1254 }
1255
1256 static void
1257 acpi_thinkpad_notify(ACPI_HANDLE h, UINT32 notify, void *context)
1258 {
1259         int             event, arg, type;
1260         device_t        dev = context;
1261         struct acpi_thinkpad_softc *sc = device_get_softc(dev);
1262
1263         ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
1264
1265         if (notify != 0x80)
1266                 device_printf(dev, "Unknown notify\n");
1267
1268         for (;;) {
1269                 acpi_GetInteger(acpi_get_handle(dev), THINKPAD_NAME_EVENTS_GET,
1270                     &event);
1271
1272                 if (event == 0)
1273                         break;
1274
1275                 type = (event >> 12) & 0xf;
1276                 arg = event & 0xfff;
1277                 switch (type) {
1278                 case 1:
1279                         if (!(sc->events_availmask & (1 << (arg - 1)))) {
1280                                 device_printf(dev, "Unknown key %d\n", arg);
1281                                 break;
1282                         }
1283
1284                         /* Execute event handler */
1285                         if (sc->handler_events & (1 << (arg - 1)))
1286                                 acpi_thinkpad_eventhandler(sc, (arg & 0xff));
1287
1288                         /* Notify devd(8) */
1289                         acpi_UserNotify("THINKPAD", h, (arg & 0xff));
1290                         break;
1291                 default:
1292                         break;
1293                 }
1294         }
1295 }
1296
1297 static void
1298 acpi_thinkpad_refresh(void *arg)
1299 {
1300         struct acpi_thinkpad_softc *sc = (struct acpi_thinkpad_softc *)arg;
1301         int i, data;
1302
1303         for (i = 0; i < THINKPAD_TEMP_SENSORS; i++) {
1304                 char temp_cmd[] = "TMP0";
1305
1306                 temp_cmd[3] = '0' + i;
1307                 /*
1308                  * The TMPx methods seem to return +/- 128 or 0
1309                  * when the respecting sensor is not available
1310                  */
1311                 if (ACPI_FAILURE(acpi_GetInteger(sc->ec_handle, temp_cmd,
1312                     &data)) || ABS(data) == 128 || data == 0) {
1313                         sc->sensors[i].flags |= SENSOR_FINVALID;
1314                         continue;
1315                 }
1316                 if (sc->thermal_updt_supported)
1317                         /* Temperature is reported in tenth of Kelvin */
1318                         sc->sensors[i].value = data * 100000 - 50000;
1319                 else
1320                         sc->sensors[i].value = data * 1000000 + 273150000;
1321                 sc->sensors[i].flags &= ~SENSOR_FINVALID;
1322         }
1323
1324         if (sc->fan_handle) {
1325                 if (ACPI_FAILURE(acpi_GetInteger(sc->fan_handle,
1326                     NULL, &data)))
1327                         sc->sensors[i].flags |= SENSOR_FINVALID;
1328                 sc->sensors[i].value = data;
1329                 sc->sensors[i].flags &= ~SENSOR_FINVALID;
1330         } else {
1331                 UINT64 speed;
1332
1333                 ACPI_EC_READ(sc->ec_dev, THINKPAD_EC_FANSPEED, &speed, 2);
1334                 sc->sensors[i].value = speed;
1335                 sc->sensors[i].flags &= ~SENSOR_FINVALID;
1336         }
1337 }