thinkpad_acpi.c revision 1.9.6.3 1 /* $NetBSD: thinkpad_acpi.c,v 1.9.6.3 2008/01/10 23:44:14 bouyer Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Jared D. McNeill.
18 * 4. Neither the name of The NetBSD Foundation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: thinkpad_acpi.c,v 1.9.6.3 2008/01/10 23:44:14 bouyer Exp $");
37
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/malloc.h>
41 #include <sys/buf.h>
42 #include <sys/callout.h>
43 #include <sys/kernel.h>
44 #include <sys/device.h>
45 #include <sys/pmf.h>
46 #include <sys/queue.h>
47 #include <sys/kmem.h>
48
49 #include <dev/acpi/acpivar.h>
50 #include <dev/acpi/acpi_ecvar.h>
51
52 #if defined(__i386__) || defined(__amd64__)
53 #include <machine/pio.h>
54 #endif
55
56 #define THINKPAD_NSENSORS 8
57
58 typedef struct thinkpad_softc {
59 device_t sc_dev;
60 device_t sc_ecdev;
61 struct acpi_devnode *sc_node;
62 ACPI_HANDLE sc_cmoshdl;
63 bool sc_cmoshdl_valid;
64
65 #define TP_PSW_SLEEP 0
66 #define TP_PSW_HIBERNATE 1
67 #define TP_PSW_DISPLAY_CYCLE 2
68 #define TP_PSW_LOCK_SCREEN 3
69 #define TP_PSW_BATTERY_INFO 4
70 #define TP_PSW_EJECT_BUTTON 5
71 #define TP_PSW_ZOOM_BUTTON 6
72 #define TP_PSW_VENDOR_BUTTON 7
73 #define TP_PSW_LAST 8
74 struct sysmon_pswitch sc_smpsw[TP_PSW_LAST];
75 bool sc_smpsw_valid;
76
77 struct sysmon_envsys *sc_sme;
78 envsys_data_t sc_sensor[THINKPAD_NSENSORS];
79
80 int sc_display_state;
81 } thinkpad_softc_t;
82
83 /* Hotkey events */
84 #define THINKPAD_NOTIFY_FnF1 0x001
85 #define THINKPAD_NOTIFY_LockScreen 0x002
86 #define THINKPAD_NOTIFY_BatteryInfo 0x003
87 #define THINKPAD_NOTIFY_SleepButton 0x004
88 #define THINKPAD_NOTIFY_WirelessSwitch 0x005
89 #define THINKPAD_NOTIFY_FnF6 0x006
90 #define THINKPAD_NOTIFY_DisplayCycle 0x007
91 #define THINKPAD_NOTIFY_PointerSwitch 0x008
92 #define THINKPAD_NOTIFY_EjectButton 0x009
93 #define THINKPAD_NOTIFY_FnF10 0x00a
94 #define THINKPAD_NOTIFY_FnF11 0x00b
95 #define THINKPAD_NOTIFY_HibernateButton 0x00c
96 #define THINKPAD_NOTIFY_BrightnessUp 0x010
97 #define THINKPAD_NOTIFY_BrightnessDown 0x011
98 #define THINKPAD_NOTIFY_ThinkLight 0x012
99 #define THINKPAD_NOTIFY_Zoom 0x014
100 #define THINKPAD_NOTIFY_ThinkVantage 0x018
101
102 #define THINKPAD_CMOS_BRIGHTNESS_UP 0x04
103 #define THINKPAD_CMOS_BRIGHTNESS_DOWN 0x05
104
105 #define THINKPAD_HKEY_VERSION 0x0100
106
107 #define THINKPAD_DISPLAY_LCD 0x01
108 #define THINKPAD_DISPLAY_CRT 0x02
109 #define THINKPAD_DISPLAY_DVI 0x08
110 #define THINKPAD_DISPLAY_ALL \
111 (THINKPAD_DISPLAY_LCD | THINKPAD_DISPLAY_CRT | THINKPAD_DISPLAY_DVI)
112
113 static int thinkpad_match(device_t, struct cfdata *, void *);
114 static void thinkpad_attach(device_t, device_t, void *);
115
116 static ACPI_STATUS thinkpad_mask_init(thinkpad_softc_t *, uint32_t);
117 static void thinkpad_notify_handler(ACPI_HANDLE, UINT32, void *);
118 static void thinkpad_get_hotkeys(void *);
119
120 static void thinkpad_temp_init(thinkpad_softc_t *);
121 static void thinkpad_temp_refresh(struct sysmon_envsys *, envsys_data_t *);
122
123 static void thinkpad_wireless_toggle(thinkpad_softc_t *);
124
125 static void thinkpad_brightness_up(device_t);
126 static void thinkpad_brightness_down(device_t);
127 static uint8_t thinkpad_brightness_read(thinkpad_softc_t *sc);
128 static void thinkpad_cmos(thinkpad_softc_t *, uint8_t);
129
130 CFATTACH_DECL_NEW(thinkpad, sizeof(thinkpad_softc_t),
131 thinkpad_match, thinkpad_attach, NULL, NULL);
132
133 static const char * const thinkpad_ids[] = {
134 "IBM0068",
135 NULL
136 };
137
138 static int
139 thinkpad_match(device_t parent, struct cfdata *match, void *opaque)
140 {
141 struct acpi_attach_args *aa = (struct acpi_attach_args *)opaque;
142 ACPI_HANDLE hdl;
143 ACPI_INTEGER ver;
144
145 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
146 return 0;
147
148 if (!acpi_match_hid(aa->aa_node->ad_devinfo, thinkpad_ids))
149 return 0;
150
151 /* No point in attaching if we can't find the CMOS method */
152 if (ACPI_FAILURE(AcpiGetHandle(NULL, "\\UCMS", &hdl)))
153 return 0;
154
155 /* We only support hotkey version 0x0100 */
156 if (ACPI_FAILURE(acpi_eval_integer(aa->aa_node->ad_handle, "MHKV",
157 &ver)))
158 return 0;
159
160 if (ver != THINKPAD_HKEY_VERSION)
161 return 0;
162
163 /* Cool, looks like we're good to go */
164 return 1;
165 }
166
167 static void
168 thinkpad_attach(device_t parent, device_t self, void *opaque)
169 {
170 thinkpad_softc_t *sc = device_private(self);
171 struct acpi_attach_args *aa = (struct acpi_attach_args *)opaque;
172 struct sysmon_pswitch *psw;
173 device_t curdev;
174 ACPI_STATUS rv;
175 ACPI_INTEGER val;
176 int i;
177
178 sc->sc_node = aa->aa_node;
179 sc->sc_dev = self;
180 sc->sc_display_state = THINKPAD_DISPLAY_LCD;
181
182 aprint_naive("\n");
183 aprint_normal("\n");
184
185 /* T61 uses \UCMS method for issuing CMOS commands */
186 rv = AcpiGetHandle(NULL, "\\UCMS", &sc->sc_cmoshdl);
187 if (ACPI_FAILURE(rv))
188 sc->sc_cmoshdl_valid = false;
189 else {
190 aprint_verbose_dev(self, "using CMOS at \\UCMS\n");
191 sc->sc_cmoshdl_valid = true;
192 }
193
194 sc->sc_ecdev = NULL;
195 TAILQ_FOREACH(curdev, &alldevs, dv_list)
196 if (device_is_a(curdev, "acpiecdt") ||
197 device_is_a(curdev, "acpiec")) {
198 sc->sc_ecdev = curdev;
199 break;
200 }
201 if (sc->sc_ecdev)
202 aprint_verbose_dev(self, "using EC at %s\n",
203 device_xname(sc->sc_ecdev));
204
205 /* Get the supported event mask */
206 rv = acpi_eval_integer(sc->sc_node->ad_handle, "MHKA", &val);
207 if (ACPI_FAILURE(rv)) {
208 aprint_error_dev(self, "couldn't evaluate MHKA: %s\n",
209 AcpiFormatException(rv));
210 goto fail;
211 }
212
213 /* Enable all supported events */
214 rv = thinkpad_mask_init(sc, val);
215 if (ACPI_FAILURE(rv)) {
216 aprint_error_dev(self, "couldn't set event mask: %s\n",
217 AcpiFormatException(rv));
218 goto fail;
219 }
220
221 /* Install notify handler for events */
222 rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
223 ACPI_DEVICE_NOTIFY, thinkpad_notify_handler, sc);
224 if (ACPI_FAILURE(rv))
225 aprint_error_dev(self, "couldn't install notify handler: %s\n",
226 AcpiFormatException(rv));
227
228 /* Register power switches with sysmon */
229 psw = sc->sc_smpsw;
230 sc->sc_smpsw_valid = true;
231
232 psw[TP_PSW_SLEEP].smpsw_name = device_xname(self);
233 psw[TP_PSW_SLEEP].smpsw_type = PSWITCH_TYPE_SLEEP;
234 #if notyet
235 psw[TP_PSW_HIBERNATE].smpsw_name = device_xname(self);
236 mpsw[TP_PSW_HIBERNATE].smpsw_type = PSWITCH_TYPE_HIBERNATE;
237 #endif
238 for (i = TP_PSW_DISPLAY_CYCLE; i < TP_PSW_LAST; i++)
239 sc->sc_smpsw[i].smpsw_type = PSWITCH_TYPE_HOTKEY;
240 psw[TP_PSW_DISPLAY_CYCLE].smpsw_name = PSWITCH_HK_DISPLAY_CYCLE;
241 psw[TP_PSW_LOCK_SCREEN].smpsw_name = PSWITCH_HK_LOCK_SCREEN;
242 psw[TP_PSW_BATTERY_INFO].smpsw_name = PSWITCH_HK_BATTERY_INFO;
243 psw[TP_PSW_EJECT_BUTTON].smpsw_name = PSWITCH_HK_EJECT_BUTTON;
244 psw[TP_PSW_ZOOM_BUTTON].smpsw_name = PSWITCH_HK_ZOOM_BUTTON;
245 psw[TP_PSW_VENDOR_BUTTON].smpsw_name = PSWITCH_HK_VENDOR_BUTTON;
246
247 for (i = 0; i < TP_PSW_LAST; i++) {
248 /* not supported yet */
249 if (i == TP_PSW_HIBERNATE)
250 continue;
251 if (sysmon_pswitch_register(&sc->sc_smpsw[i]) != 0) {
252 aprint_error_dev(self,
253 "couldn't register with sysmon\n");
254 sc->sc_smpsw_valid = false;
255 break;
256 }
257 }
258
259 /* Register temperature sensors with envsys */
260 thinkpad_temp_init(sc);
261
262 fail:
263 if (!pmf_device_register(self, NULL, NULL))
264 aprint_error_dev(self, "couldn't establish power handler\n");
265 if (!pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_UP,
266 thinkpad_brightness_up, true))
267 aprint_error_dev(self, "couldn't register event handler\n");
268 if (!pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_DOWN,
269 thinkpad_brightness_down, true))
270 aprint_error_dev(self, "couldn't register event handler\n");
271 }
272
273 static void
274 thinkpad_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
275 {
276 thinkpad_softc_t *sc = (thinkpad_softc_t *)opaque;
277 device_t self = sc->sc_dev;
278 ACPI_STATUS rv;
279
280 if (notify != 0x80) {
281 aprint_debug_dev(self, "unknown notify 0x%02x\n", notify);
282 return;
283 }
284
285 rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, thinkpad_get_hotkeys, sc);
286 if (ACPI_FAILURE(rv))
287 aprint_error_dev(self, "couldn't queue hotkey handler: %s\n",
288 AcpiFormatException(rv));
289 }
290
291 static void
292 thinkpad_get_hotkeys(void *opaque)
293 {
294 thinkpad_softc_t *sc = (thinkpad_softc_t *)opaque;
295 device_t self = sc->sc_dev;
296 ACPI_STATUS rv;
297 ACPI_INTEGER val;
298 int type, event;
299
300 for (;;) {
301 rv = acpi_eval_integer(sc->sc_node->ad_handle, "MHKP", &val);
302 if (ACPI_FAILURE(rv)) {
303 aprint_error_dev(self, "couldn't evaluate MHKP: %s\n",
304 AcpiFormatException(rv));
305 return;
306 }
307
308 if (val == 0)
309 return;
310
311 type = (val & 0xf000) >> 12;
312 event = val & 0x0fff;
313
314 if (type != 1)
315 /* Only type 1 events are supported for now */
316 continue;
317
318 switch (event) {
319 case THINKPAD_NOTIFY_BrightnessUp:
320 thinkpad_brightness_up(self);
321 break;
322 case THINKPAD_NOTIFY_BrightnessDown:
323 thinkpad_brightness_down(self);
324 break;
325 case THINKPAD_NOTIFY_WirelessSwitch:
326 thinkpad_wireless_toggle(sc);
327 break;
328 case THINKPAD_NOTIFY_SleepButton:
329 if (sc->sc_smpsw_valid == false)
330 break;
331 sysmon_pswitch_event(&sc->sc_smpsw[TP_PSW_SLEEP],
332 PSWITCH_EVENT_PRESSED);
333 break;
334 case THINKPAD_NOTIFY_HibernateButton:
335 #if notyet
336 if (sc->sc_smpsw_valid == false)
337 break;
338 sysmon_pswitch_event(&sc->sc_smpsw[TP_PSW_HIBERNATE],
339 PSWITCH_EVENT_PRESSED);
340 #endif
341 break;
342 case THINKPAD_NOTIFY_DisplayCycle:
343 if (sc->sc_smpsw_valid == false)
344 break;
345 sysmon_pswitch_event(
346 &sc->sc_smpsw[TP_PSW_DISPLAY_CYCLE],
347 PSWITCH_EVENT_PRESSED);
348 break;
349 case THINKPAD_NOTIFY_LockScreen:
350 if (sc->sc_smpsw_valid == false)
351 break;
352 sysmon_pswitch_event(
353 &sc->sc_smpsw[TP_PSW_LOCK_SCREEN],
354 PSWITCH_EVENT_PRESSED);
355 break;
356 case THINKPAD_NOTIFY_BatteryInfo:
357 if (sc->sc_smpsw_valid == false)
358 break;
359 sysmon_pswitch_event(
360 &sc->sc_smpsw[TP_PSW_BATTERY_INFO],
361 PSWITCH_EVENT_PRESSED);
362 break;
363 case THINKPAD_NOTIFY_EjectButton:
364 if (sc->sc_smpsw_valid == false)
365 break;
366 sysmon_pswitch_event(
367 &sc->sc_smpsw[TP_PSW_EJECT_BUTTON],
368 PSWITCH_EVENT_PRESSED);
369 break;
370 case THINKPAD_NOTIFY_Zoom:
371 if (sc->sc_smpsw_valid == false)
372 break;
373 sysmon_pswitch_event(
374 &sc->sc_smpsw[TP_PSW_ZOOM_BUTTON],
375 PSWITCH_EVENT_PRESSED);
376 break;
377 case THINKPAD_NOTIFY_ThinkVantage:
378 if (sc->sc_smpsw_valid == false)
379 break;
380 sysmon_pswitch_event(
381 &sc->sc_smpsw[TP_PSW_VENDOR_BUTTON],
382 PSWITCH_EVENT_PRESSED);
383 break;
384 case THINKPAD_NOTIFY_FnF1:
385 case THINKPAD_NOTIFY_FnF6:
386 case THINKPAD_NOTIFY_PointerSwitch:
387 case THINKPAD_NOTIFY_FnF10:
388 case THINKPAD_NOTIFY_FnF11:
389 case THINKPAD_NOTIFY_ThinkLight:
390 /* XXXJDM we should deliver hotkeys as keycodes */
391 break;
392 default:
393 aprint_debug_dev(self, "notify event 0x%03x\n", event);
394 break;
395 }
396 }
397 }
398
399 static ACPI_STATUS
400 thinkpad_mask_init(thinkpad_softc_t *sc, uint32_t mask)
401 {
402 ACPI_OBJECT param[2];
403 ACPI_OBJECT_LIST params;
404 ACPI_STATUS rv;
405 int i;
406
407 /* Update hotkey mask */
408 params.Count = 2;
409 params.Pointer = param;
410 param[0].Type = param[1].Type = ACPI_TYPE_INTEGER;
411
412 for (i = 0; i < 32; i++) {
413 param[0].Integer.Value = i + 1;
414 param[1].Integer.Value = (((1 << i) & mask) != 0);
415
416 rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "MHKM",
417 ¶ms, NULL);
418 if (ACPI_FAILURE(rv))
419 return rv;
420 }
421
422 /* Enable hotkey events */
423 params.Count = 1;
424 param[0].Integer.Value = 1;
425 rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "MHKC", ¶ms, NULL);
426 if (ACPI_FAILURE(rv)) {
427 aprint_error_dev(sc->sc_dev, "couldn't enable hotkeys: %s\n",
428 AcpiFormatException(rv));
429 return rv;
430 }
431
432 /* Claim ownership of brightness control */
433 param[0].Integer.Value = 0;
434 (void)AcpiEvaluateObject(sc->sc_node->ad_handle, "PWMS", ¶ms, NULL);
435
436 return AE_OK;
437 }
438
439 static void
440 thinkpad_temp_init(thinkpad_softc_t *sc)
441 {
442 char sname[5] = "TMP?";
443 int i, err;
444
445 if (sc->sc_ecdev == NULL)
446 return; /* no chance of this working */
447
448 sc->sc_sme = sysmon_envsys_create();
449 for (i = 0; i < THINKPAD_NSENSORS; i++) {
450 sname[3] = '0' + i;
451 strcpy(sc->sc_sensor[i].desc, sname);
452 sc->sc_sensor[i].units = ENVSYS_STEMP;
453
454 if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[i]))
455 aprint_error_dev(sc->sc_dev,
456 "couldn't attach sensor %s\n", sname);
457 }
458
459 sc->sc_sme->sme_name = device_xname(sc->sc_dev);
460 sc->sc_sme->sme_cookie = sc;
461 sc->sc_sme->sme_refresh = thinkpad_temp_refresh;
462
463 err = sysmon_envsys_register(sc->sc_sme);
464 if (err) {
465 aprint_error_dev(sc->sc_dev,
466 "couldn't register with sysmon: %d\n", err);
467 sysmon_envsys_destroy(sc->sc_sme);
468 }
469 }
470
471 static void
472 thinkpad_temp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
473 {
474 thinkpad_softc_t *sc = sme->sme_cookie;
475 char sname[5] = "TMP?";
476 ACPI_INTEGER val;
477 ACPI_STATUS rv;
478 int temp;
479
480 sname[3] = '0' + edata->sensor;
481 rv = acpi_eval_integer(acpiec_get_handle(sc->sc_ecdev), sname, &val);
482 if (ACPI_FAILURE(rv)) {
483 edata->state = ENVSYS_SINVALID;
484 return;
485 }
486 temp = (int)val;
487 if (temp > 127 || temp < -127) {
488 edata->state = ENVSYS_SINVALID;
489 return;
490 }
491
492 edata->value_cur = temp * 1000000 + 273150000;
493 edata->state = ENVSYS_SVALID;
494 }
495
496 static void
497 thinkpad_wireless_toggle(thinkpad_softc_t *sc)
498 {
499 /* Ignore return value, as the hardware may not support bluetooth */
500 (void)AcpiEvaluateObject(sc->sc_node->ad_handle, "BTGL", NULL, NULL);
501 }
502
503 static uint8_t
504 thinkpad_brightness_read(thinkpad_softc_t *sc)
505 {
506 #if defined(__i386__) || defined(__amd64__)
507 /*
508 * We have two ways to get the current brightness -- either via
509 * magic RTC registers, or using the EC. Since I don't dare mess
510 * with the EC, and Thinkpads are x86-only, this will have to do
511 * for now.
512 */
513 outb(0x70, 0x6c);
514 return inb(0x71) & 7;
515 #else
516 return 0;
517 #endif
518 }
519
520 static void
521 thinkpad_brightness_up(device_t self)
522 {
523 thinkpad_softc_t *sc = device_private(self);
524
525 if (thinkpad_brightness_read(sc) == 7)
526 return;
527 thinkpad_cmos(sc, THINKPAD_CMOS_BRIGHTNESS_UP);
528 }
529
530 static void
531 thinkpad_brightness_down(device_t self)
532 {
533 thinkpad_softc_t *sc = device_private(self);
534
535 if (thinkpad_brightness_read(sc) == 0)
536 return;
537 thinkpad_cmos(sc, THINKPAD_CMOS_BRIGHTNESS_DOWN);
538 }
539
540 static void
541 thinkpad_cmos(thinkpad_softc_t *sc, uint8_t cmd)
542 {
543 ACPI_OBJECT param;
544 ACPI_OBJECT_LIST params;
545 ACPI_STATUS rv;
546
547 if (sc->sc_cmoshdl_valid == false)
548 return;
549
550 params.Count = 1;
551 params.Pointer = ¶m;
552 param.Type = ACPI_TYPE_INTEGER;
553 param.Integer.Value = cmd;
554 rv = AcpiEvaluateObject(sc->sc_cmoshdl, NULL, ¶ms, NULL);
555 if (ACPI_FAILURE(rv))
556 aprint_error_dev(sc->sc_dev, "couldn't evalute CMOS: %s\n",
557 AcpiFormatException(rv));
558 }
559