thinkpad_acpi.c revision 1.9.8.2 1 /* $NetBSD: thinkpad_acpi.c,v 1.9.8.2 2008/01/09 01:52:22 matt 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.8.2 2008/01/09 01:52:22 matt 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 return;
273 }
274
275 static void
276 thinkpad_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
277 {
278 thinkpad_softc_t *sc = (thinkpad_softc_t *)opaque;
279 device_t self = sc->sc_dev;
280 ACPI_STATUS rv;
281
282 if (notify != 0x80) {
283 aprint_debug_dev(self, "unknown notify 0x%02x\n", notify);
284 return;
285 }
286
287 rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, thinkpad_get_hotkeys, sc);
288 if (ACPI_FAILURE(rv))
289 aprint_error_dev(self, "couldn't queue hotkey handler: %s\n",
290 AcpiFormatException(rv));
291 }
292
293 static void
294 thinkpad_get_hotkeys(void *opaque)
295 {
296 thinkpad_softc_t *sc = (thinkpad_softc_t *)opaque;
297 device_t self = sc->sc_dev;
298 ACPI_STATUS rv;
299 ACPI_INTEGER val;
300 int type, event;
301
302 for (;;) {
303 rv = acpi_eval_integer(sc->sc_node->ad_handle, "MHKP", &val);
304 if (ACPI_FAILURE(rv)) {
305 aprint_error_dev(self, "couldn't evaluate MHKP: %s\n",
306 AcpiFormatException(rv));
307 return;
308 }
309
310 if (val == 0)
311 return;
312
313 type = (val & 0xf000) >> 12;
314 event = val & 0x0fff;
315
316 if (type != 1)
317 /* Only type 1 events are supported for now */
318 continue;
319
320 switch (event) {
321 case THINKPAD_NOTIFY_BrightnessUp:
322 thinkpad_brightness_up(self);
323 break;
324 case THINKPAD_NOTIFY_BrightnessDown:
325 thinkpad_brightness_down(self);
326 break;
327 case THINKPAD_NOTIFY_WirelessSwitch:
328 thinkpad_wireless_toggle(sc);
329 break;
330 case THINKPAD_NOTIFY_SleepButton:
331 if (sc->sc_smpsw_valid == false)
332 break;
333 sysmon_pswitch_event(&sc->sc_smpsw[TP_PSW_SLEEP],
334 PSWITCH_EVENT_PRESSED);
335 break;
336 case THINKPAD_NOTIFY_HibernateButton:
337 #if notyet
338 if (sc->sc_smpsw_valid == false)
339 break;
340 sysmon_pswitch_event(&sc->sc_smpsw[TP_PSW_HIBERNATE],
341 PSWITCH_EVENT_PRESSED);
342 #endif
343 break;
344 case THINKPAD_NOTIFY_DisplayCycle:
345 if (sc->sc_smpsw_valid == false)
346 break;
347 sysmon_pswitch_event(
348 &sc->sc_smpsw[TP_PSW_DISPLAY_CYCLE],
349 PSWITCH_EVENT_PRESSED);
350 break;
351 case THINKPAD_NOTIFY_LockScreen:
352 if (sc->sc_smpsw_valid == false)
353 break;
354 sysmon_pswitch_event(
355 &sc->sc_smpsw[TP_PSW_LOCK_SCREEN],
356 PSWITCH_EVENT_PRESSED);
357 break;
358 case THINKPAD_NOTIFY_BatteryInfo:
359 if (sc->sc_smpsw_valid == false)
360 break;
361 sysmon_pswitch_event(
362 &sc->sc_smpsw[TP_PSW_BATTERY_INFO],
363 PSWITCH_EVENT_PRESSED);
364 break;
365 case THINKPAD_NOTIFY_EjectButton:
366 if (sc->sc_smpsw_valid == false)
367 break;
368 sysmon_pswitch_event(
369 &sc->sc_smpsw[TP_PSW_EJECT_BUTTON],
370 PSWITCH_EVENT_PRESSED);
371 break;
372 case THINKPAD_NOTIFY_Zoom:
373 if (sc->sc_smpsw_valid == false)
374 break;
375 sysmon_pswitch_event(
376 &sc->sc_smpsw[TP_PSW_ZOOM_BUTTON],
377 PSWITCH_EVENT_PRESSED);
378 break;
379 case THINKPAD_NOTIFY_ThinkVantage:
380 if (sc->sc_smpsw_valid == false)
381 break;
382 sysmon_pswitch_event(
383 &sc->sc_smpsw[TP_PSW_VENDOR_BUTTON],
384 PSWITCH_EVENT_PRESSED);
385 break;
386 case THINKPAD_NOTIFY_FnF1:
387 case THINKPAD_NOTIFY_FnF6:
388 case THINKPAD_NOTIFY_PointerSwitch:
389 case THINKPAD_NOTIFY_FnF10:
390 case THINKPAD_NOTIFY_FnF11:
391 case THINKPAD_NOTIFY_ThinkLight:
392 /* XXXJDM we should deliver hotkeys as keycodes */
393 break;
394 default:
395 aprint_debug_dev(self, "notify event 0x%03x\n", event);
396 break;
397 }
398 }
399
400 return;
401 }
402
403 static ACPI_STATUS
404 thinkpad_mask_init(thinkpad_softc_t *sc, uint32_t mask)
405 {
406 ACPI_OBJECT param[2];
407 ACPI_OBJECT_LIST params;
408 ACPI_STATUS rv;
409 int i;
410
411 /* Update hotkey mask */
412 params.Count = 2;
413 params.Pointer = param;
414 param[0].Type = param[1].Type = ACPI_TYPE_INTEGER;
415
416 for (i = 0; i < 32; i++) {
417 param[0].Integer.Value = i + 1;
418 param[1].Integer.Value = (((1 << i) & mask) != 0);
419
420 rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "MHKM",
421 ¶ms, NULL);
422 if (ACPI_FAILURE(rv))
423 return rv;
424 }
425
426 /* Enable hotkey events */
427 params.Count = 1;
428 param[0].Integer.Value = 1;
429 rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "MHKC", ¶ms, NULL);
430 if (ACPI_FAILURE(rv)) {
431 aprint_error_dev(sc->sc_dev, "couldn't enable hotkeys: %s\n",
432 AcpiFormatException(rv));
433 return rv;
434 }
435
436 /* Claim ownership of brightness control */
437 param[0].Integer.Value = 0;
438 (void)AcpiEvaluateObject(sc->sc_node->ad_handle, "PWMS", ¶ms, NULL);
439
440 return AE_OK;
441 }
442
443 static void
444 thinkpad_temp_init(thinkpad_softc_t *sc)
445 {
446 char sname[5] = "TMP?";
447 int i, err;
448
449 if (sc->sc_ecdev == NULL)
450 return; /* no chance of this working */
451
452 sc->sc_sme = sysmon_envsys_create();
453 for (i = 0; i < THINKPAD_NSENSORS; i++) {
454 sname[3] = '0' + i;
455 strcpy(sc->sc_sensor[i].desc, sname);
456 sc->sc_sensor[i].units = ENVSYS_STEMP;
457
458 if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[i]))
459 aprint_error_dev(sc->sc_dev,
460 "couldn't attach sensor %s\n", sname);
461 }
462
463 sc->sc_sme->sme_name = device_xname(sc->sc_dev);
464 sc->sc_sme->sme_cookie = sc;
465 sc->sc_sme->sme_refresh = thinkpad_temp_refresh;
466
467 err = sysmon_envsys_register(sc->sc_sme);
468 if (err) {
469 aprint_error_dev(sc->sc_dev,
470 "couldn't register with sysmon: %d\n", err);
471 sysmon_envsys_destroy(sc->sc_sme);
472 }
473 }
474
475 static void
476 thinkpad_temp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
477 {
478 thinkpad_softc_t *sc = sme->sme_cookie;
479 char sname[5] = "TMP?";
480 ACPI_INTEGER val;
481 ACPI_STATUS rv;
482 int temp;
483
484 sname[3] = '0' + edata->sensor;
485 rv = acpi_eval_integer(acpiec_get_handle(sc->sc_ecdev), sname, &val);
486 if (ACPI_FAILURE(rv)) {
487 edata->state = ENVSYS_SINVALID;
488 return;
489 }
490 temp = (int)val;
491 if (temp > 127 || temp < -127) {
492 edata->state = ENVSYS_SINVALID;
493 return;
494 }
495
496 edata->value_cur = temp * 1000000 + 273150000;
497 edata->state = ENVSYS_SVALID;
498 }
499
500 static void
501 thinkpad_wireless_toggle(thinkpad_softc_t *sc)
502 {
503 /* Ignore return value, as the hardware may not support bluetooth */
504 (void)AcpiEvaluateObject(sc->sc_node->ad_handle, "BTGL", NULL, NULL);
505 }
506
507 static uint8_t
508 thinkpad_brightness_read(thinkpad_softc_t *sc)
509 {
510 #if defined(__i386__) || defined(__amd64__)
511 /*
512 * We have two ways to get the current brightness -- either via
513 * magic RTC registers, or using the EC. Since I don't dare mess
514 * with the EC, and Thinkpads are x86-only, this will have to do
515 * for now.
516 */
517 outb(0x70, 0x6c);
518 return inb(0x71) & 7;
519 #else
520 return 0;
521 #endif
522 }
523
524 static void
525 thinkpad_brightness_up(device_t self)
526 {
527 thinkpad_softc_t *sc = device_private(self);
528
529 if (thinkpad_brightness_read(sc) == 7)
530 return;
531 thinkpad_cmos(sc, THINKPAD_CMOS_BRIGHTNESS_UP);
532 }
533
534 static void
535 thinkpad_brightness_down(device_t self)
536 {
537 thinkpad_softc_t *sc = device_private(self);
538
539 if (thinkpad_brightness_read(sc) == 0)
540 return;
541 thinkpad_cmos(sc, THINKPAD_CMOS_BRIGHTNESS_DOWN);
542 }
543
544 static void
545 thinkpad_cmos(thinkpad_softc_t *sc, uint8_t cmd)
546 {
547 ACPI_OBJECT param;
548 ACPI_OBJECT_LIST params;
549 ACPI_STATUS rv;
550
551 if (sc->sc_cmoshdl_valid == false)
552 return;
553
554 params.Count = 1;
555 params.Pointer = ¶m;
556 param.Type = ACPI_TYPE_INTEGER;
557 param.Integer.Value = cmd;
558 rv = AcpiEvaluateObject(sc->sc_cmoshdl, NULL, ¶ms, NULL);
559 if (ACPI_FAILURE(rv))
560 aprint_error_dev(sc->sc_dev, "couldn't evalute CMOS: %s\n",
561 AcpiFormatException(rv));
562 }
563