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