thinkpad_acpi.c revision 1.8 1 /* $NetBSD: thinkpad_acpi.c,v 1.8 2007/12/22 13:06:30 jmcneill 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.8 2007/12/22 13:06:30 jmcneill 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 struct sysmon_pswitch sc_smpsw[2];
66 #define TP_PSW_SLEEP 0
67 #define TP_PSW_HIBERNATE 1
68 bool sc_smpsw_valid;
69
70 struct sysmon_envsys *sc_sme;
71 envsys_data_t sc_sensor[THINKPAD_NSENSORS];
72
73 int sc_display_state;
74 } thinkpad_softc_t;
75
76 /* Hotkey events */
77 #define THINKPAD_NOTIFY_FnF1 0x001
78 #define THINKPAD_NOTIFY_LockScreen 0x002
79 #define THINKPAD_NOTIFY_BatteryInfo 0x003
80 #define THINKPAD_NOTIFY_SleepButton 0x004
81 #define THINKPAD_NOTIFY_WirelessSwitch 0x005
82 #define THINKPAD_NOTIFY_FnF6 0x006
83 #define THINKPAD_NOTIFY_DisplayCycle 0x007
84 #define THINKPAD_NOTIFY_PointerSwitch 0x008
85 #define THINKPAD_NOTIFY_EjectButton 0x009
86 #define THINKPAD_NOTIFY_FnF10 0x00a
87 #define THINKPAD_NOTIFY_FnF11 0x00b
88 #define THINKPAD_NOTIFY_HibernateButton 0x00c
89 #define THINKPAD_NOTIFY_BrightnessUp 0x010
90 #define THINKPAD_NOTIFY_BrightnessDown 0x011
91 #define THINKPAD_NOTIFY_ThinkLight 0x012
92 #define THINKPAD_NOTIFY_Zoom 0x014
93 #define THINKPAD_NOTIFY_ThinkVantage 0x018
94
95 #define THINKPAD_CMOS_BRIGHTNESS_UP 0x04
96 #define THINKPAD_CMOS_BRIGHTNESS_DOWN 0x05
97
98 #define THINKPAD_HKEY_VERSION 0x0100
99
100 #define THINKPAD_DISPLAY_LCD 0x01
101 #define THINKPAD_DISPLAY_CRT 0x02
102 #define THINKPAD_DISPLAY_DVI 0x08
103 #define THINKPAD_DISPLAY_ALL \
104 (THINKPAD_DISPLAY_LCD | THINKPAD_DISPLAY_CRT | THINKPAD_DISPLAY_DVI)
105
106 static int thinkpad_match(device_t, struct cfdata *, void *);
107 static void thinkpad_attach(device_t, device_t, void *);
108
109 static ACPI_STATUS thinkpad_mask_init(thinkpad_softc_t *, uint32_t);
110 static void thinkpad_notify_handler(ACPI_HANDLE, UINT32, void *);
111 static void thinkpad_get_hotkeys(void *);
112
113 static void thinkpad_temp_init(thinkpad_softc_t *);
114 static void thinkpad_temp_refresh(struct sysmon_envsys *, envsys_data_t *);
115
116 static void thinkpad_wireless_toggle(thinkpad_softc_t *);
117
118 static void thinkpad_display_cycle(thinkpad_softc_t *);
119
120 static void thinkpad_brightness_up(device_t);
121 static void thinkpad_brightness_down(device_t);
122 static uint8_t thinkpad_brightness_read(thinkpad_softc_t *sc);
123 static void thinkpad_cmos(thinkpad_softc_t *, uint8_t);
124
125 CFATTACH_DECL_NEW(thinkpad, sizeof(thinkpad_softc_t),
126 thinkpad_match, thinkpad_attach, NULL, NULL);
127
128 static const char * const thinkpad_ids[] = {
129 "IBM0068",
130 NULL
131 };
132
133 static int
134 thinkpad_match(device_t parent, struct cfdata *match, void *opaque)
135 {
136 struct acpi_attach_args *aa = (struct acpi_attach_args *)opaque;
137 ACPI_HANDLE hdl;
138 ACPI_INTEGER ver;
139
140 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
141 return 0;
142
143 if (!acpi_match_hid(aa->aa_node->ad_devinfo, thinkpad_ids))
144 return 0;
145
146 /* No point in attaching if we can't find the CMOS method */
147 if (ACPI_FAILURE(AcpiGetHandle(NULL, "\\UCMS", &hdl)))
148 return 0;
149
150 /* We only support hotkey version 0x0100 */
151 if (ACPI_FAILURE(acpi_eval_integer(aa->aa_node->ad_handle, "MHKV",
152 &ver)))
153 return 0;
154
155 if (ver != THINKPAD_HKEY_VERSION)
156 return 0;
157
158 /* Cool, looks like we're good to go */
159 return 1;
160 }
161
162 static void
163 thinkpad_attach(device_t parent, device_t self, void *opaque)
164 {
165 thinkpad_softc_t *sc = device_private(self);
166 struct acpi_attach_args *aa = (struct acpi_attach_args *)opaque;
167 device_t curdev;
168 ACPI_STATUS rv;
169 ACPI_INTEGER val;
170
171 sc->sc_node = aa->aa_node;
172 sc->sc_dev = self;
173 sc->sc_display_state = THINKPAD_DISPLAY_LCD;
174
175 aprint_naive("\n");
176 aprint_normal("\n");
177
178 /* T61 uses \UCMS method for issuing CMOS commands */
179 rv = AcpiGetHandle(NULL, "\\UCMS", &sc->sc_cmoshdl);
180 if (ACPI_FAILURE(rv))
181 sc->sc_cmoshdl_valid = false;
182 else {
183 aprint_verbose_dev(self, "using CMOS at \\UCMS\n");
184 sc->sc_cmoshdl_valid = true;
185 }
186
187 sc->sc_ecdev = NULL;
188 TAILQ_FOREACH(curdev, &alldevs, dv_list)
189 if (device_is_a(curdev, "acpiecdt") ||
190 device_is_a(curdev, "acpiec")) {
191 sc->sc_ecdev = curdev;
192 break;
193 }
194 if (sc->sc_ecdev)
195 aprint_verbose_dev(self, "using EC at %s\n",
196 device_xname(sc->sc_ecdev));
197
198 /* Get the supported event mask */
199 rv = acpi_eval_integer(sc->sc_node->ad_handle, "MHKA", &val);
200 if (ACPI_FAILURE(rv)) {
201 aprint_error_dev(self, "couldn't evaluate MHKA: %s\n",
202 AcpiFormatException(rv));
203 goto fail;
204 }
205
206 /* Enable all supported events */
207 rv = thinkpad_mask_init(sc, val);
208 if (ACPI_FAILURE(rv)) {
209 aprint_error_dev(self, "couldn't set event mask: %s\n",
210 AcpiFormatException(rv));
211 goto fail;
212 }
213
214 /* Install notify handler for events */
215 rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
216 ACPI_DEVICE_NOTIFY, thinkpad_notify_handler, sc);
217 if (ACPI_FAILURE(rv))
218 aprint_error_dev(self, "couldn't install notify handler: %s\n",
219 AcpiFormatException(rv));
220
221 /* Register power switches with sysmon */
222 sc->sc_smpsw_valid = true;
223
224 sc->sc_smpsw[TP_PSW_SLEEP].smpsw_name = device_xname(self);
225 sc->sc_smpsw[TP_PSW_SLEEP].smpsw_type = PSWITCH_TYPE_SLEEP;
226 #if notyet
227 sc->sc_smpsw[TP_PSW_HIBERNATE].smpsw_name = device_xname(self);
228 sc->sc_smpsw[TP_PSW_HIBERNATE].smpsw_type = PSWITCH_TYPE_HIBERNATE;
229 #endif
230
231 if (sysmon_pswitch_register(&sc->sc_smpsw[TP_PSW_SLEEP]) != 0) {
232 aprint_error_dev(self, "couldn't register with sysmon\n");
233 sc->sc_smpsw_valid = false;
234 }
235 #if notyet
236 if (sysmon_pswitch_register(&sc->sc_smpsw[TP_PSW_HIBERNATE]) != 0) {
237 aprint_error_dev(self, "couldn't register with sysmon\n");
238 sc->sc_smpsw_valid = false;
239 }
240 #endif
241
242 /* Register temperature sensors with envsys */
243 thinkpad_temp_init(sc);
244
245 fail:
246 if (!pmf_device_register(self, NULL, NULL))
247 aprint_error_dev(self, "couldn't establish power handler\n");
248 if (!pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_UP,
249 thinkpad_brightness_up, true))
250 aprint_error_dev(self, "couldn't register event handler\n");
251 if (!pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_DOWN,
252 thinkpad_brightness_down, true))
253 aprint_error_dev(self, "couldn't register event handler\n");
254
255 return;
256 }
257
258 static void
259 thinkpad_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
260 {
261 thinkpad_softc_t *sc = (thinkpad_softc_t *)opaque;
262 device_t self = sc->sc_dev;
263 ACPI_STATUS rv;
264
265 if (notify != 0x80) {
266 aprint_debug_dev(self, "unknown notify 0x%02x\n", notify);
267 return;
268 }
269
270 rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, thinkpad_get_hotkeys, sc);
271 if (ACPI_FAILURE(rv))
272 aprint_error_dev(self, "couldn't queue hotkey handler: %s\n",
273 AcpiFormatException(rv));
274 }
275
276 static void
277 thinkpad_get_hotkeys(void *opaque)
278 {
279 thinkpad_softc_t *sc = (thinkpad_softc_t *)opaque;
280 device_t self = sc->sc_dev;
281 ACPI_STATUS rv;
282 ACPI_INTEGER val;
283 int type, event;
284
285 for (;;) {
286 rv = acpi_eval_integer(sc->sc_node->ad_handle, "MHKP", &val);
287 if (ACPI_FAILURE(rv)) {
288 aprint_error_dev(self, "couldn't evaluate MHKP: %s\n",
289 AcpiFormatException(rv));
290 return;
291 }
292
293 if (val == 0)
294 return;
295
296 type = (val & 0xf000) >> 12;
297 event = val & 0x0fff;
298
299 if (type != 1)
300 /* Only type 1 events are supported for now */
301 continue;
302
303 switch (event) {
304 case THINKPAD_NOTIFY_BrightnessUp:
305 thinkpad_brightness_up(self);
306 break;
307 case THINKPAD_NOTIFY_BrightnessDown:
308 thinkpad_brightness_down(self);
309 break;
310 case THINKPAD_NOTIFY_DisplayCycle:
311 thinkpad_display_cycle(sc);
312 break;
313 case THINKPAD_NOTIFY_WirelessSwitch:
314 thinkpad_wireless_toggle(sc);
315 break;
316 case THINKPAD_NOTIFY_SleepButton:
317 if (sc->sc_smpsw_valid == false)
318 break;
319 sysmon_pswitch_event(&sc->sc_smpsw[TP_PSW_SLEEP],
320 PSWITCH_EVENT_PRESSED);
321 break;
322 case THINKPAD_NOTIFY_HibernateButton:
323 #if notyet
324 if (sc->sc_smpsw_valid == false)
325 break;
326 sysmon_pswitch_event(&sc->sc_smpsw[TP_PSW_HIBERNATE],
327 PSWITCH_EVENT_PRESSED);
328 #endif
329 break;
330 case THINKPAD_NOTIFY_FnF1:
331 case THINKPAD_NOTIFY_LockScreen:
332 case THINKPAD_NOTIFY_BatteryInfo:
333 case THINKPAD_NOTIFY_FnF6:
334 case THINKPAD_NOTIFY_PointerSwitch:
335 case THINKPAD_NOTIFY_EjectButton:
336 case THINKPAD_NOTIFY_FnF10:
337 case THINKPAD_NOTIFY_FnF11:
338 case THINKPAD_NOTIFY_ThinkLight:
339 case THINKPAD_NOTIFY_Zoom:
340 case THINKPAD_NOTIFY_ThinkVantage:
341 /* XXXJDM we should deliver hotkeys as keycodes */
342 break;
343 default:
344 aprint_debug_dev(self, "notify event 0x%03x\n", event);
345 break;
346 }
347 }
348
349 return;
350 }
351
352 static ACPI_STATUS
353 thinkpad_mask_init(thinkpad_softc_t *sc, uint32_t mask)
354 {
355 ACPI_OBJECT param[2];
356 ACPI_OBJECT_LIST params;
357 ACPI_STATUS rv;
358 int i;
359
360 /* Update hotkey mask */
361 params.Count = 2;
362 params.Pointer = param;
363 param[0].Type = param[1].Type = ACPI_TYPE_INTEGER;
364
365 for (i = 0; i < 32; i++) {
366 param[0].Integer.Value = i + 1;
367 param[1].Integer.Value = (((1 << i) & mask) != 0);
368
369 rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "MHKM",
370 ¶ms, NULL);
371 if (ACPI_FAILURE(rv))
372 return rv;
373 }
374
375 /* Enable hotkey events */
376 params.Count = 1;
377 param[0].Integer.Value = 1;
378 rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "MHKC", ¶ms, NULL);
379 if (ACPI_FAILURE(rv)) {
380 aprint_error_dev(sc->sc_dev, "couldn't enable hotkeys: %s\n",
381 AcpiFormatException(rv));
382 return rv;
383 }
384
385 /* Claim ownership of brightness control */
386 param[0].Integer.Value = 0;
387 (void)AcpiEvaluateObject(sc->sc_node->ad_handle, "PWMS", ¶ms, NULL);
388
389 return AE_OK;
390 }
391
392 static void
393 thinkpad_temp_init(thinkpad_softc_t *sc)
394 {
395 char sname[5] = "TMP?";
396 int i, err;
397
398 if (sc->sc_ecdev == NULL)
399 return; /* no chance of this working */
400
401 sc->sc_sme = sysmon_envsys_create();
402 for (i = 0; i < THINKPAD_NSENSORS; i++) {
403 sname[3] = '0' + i;
404 strcpy(sc->sc_sensor[i].desc, sname);
405 sc->sc_sensor[i].units = ENVSYS_STEMP;
406
407 if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[i]))
408 aprint_error_dev(sc->sc_dev,
409 "couldn't attach sensor %s\n", sname);
410 }
411
412 sc->sc_sme->sme_name = device_xname(sc->sc_dev);
413 sc->sc_sme->sme_cookie = sc;
414 sc->sc_sme->sme_refresh = thinkpad_temp_refresh;
415
416 err = sysmon_envsys_register(sc->sc_sme);
417 if (err) {
418 aprint_error_dev(sc->sc_dev,
419 "couldn't register with sysmon: %d\n", err);
420 sysmon_envsys_destroy(sc->sc_sme);
421 }
422 }
423
424 static void
425 thinkpad_temp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
426 {
427 thinkpad_softc_t *sc = sme->sme_cookie;
428 char sname[5] = "TMP?";
429 ACPI_INTEGER val;
430 ACPI_STATUS rv;
431 int temp;
432
433 sname[3] = '0' + edata->sensor;
434 rv = acpi_eval_integer(acpiec_get_handle(sc->sc_ecdev), sname, &val);
435 if (ACPI_FAILURE(rv)) {
436 edata->state = ENVSYS_SINVALID;
437 return;
438 }
439 temp = (int)val;
440 if (temp > 127 || temp < -127) {
441 edata->state = ENVSYS_SINVALID;
442 return;
443 }
444
445 edata->value_cur = temp * 1000000 + 273150000;
446 edata->state = ENVSYS_SVALID;
447 }
448
449 static void
450 thinkpad_wireless_toggle(thinkpad_softc_t *sc)
451 {
452 /* Ignore return value, as the hardware may not support bluetooth */
453 (void)AcpiEvaluateObject(sc->sc_node->ad_handle, "BTGL", NULL, NULL);
454 }
455
456 static void
457 thinkpad_display_cycle(thinkpad_softc_t *sc)
458 {
459 ACPI_OBJECT param[2];
460 ACPI_OBJECT_LIST params;
461 ACPI_STATUS rv;
462
463 /* Select the next display state */
464 switch (sc->sc_display_state) {
465 case THINKPAD_DISPLAY_LCD:
466 sc->sc_display_state = THINKPAD_DISPLAY_CRT;
467 aprint_normal_dev(sc->sc_dev, "switching to CRT\n");
468 break;
469 case THINKPAD_DISPLAY_CRT:
470 sc->sc_display_state = THINKPAD_DISPLAY_DVI;
471 aprint_normal_dev(sc->sc_dev, "switching to DVI\n");
472 break;
473 case THINKPAD_DISPLAY_DVI:
474 sc->sc_display_state = THINKPAD_DISPLAY_ALL;
475 aprint_normal_dev(sc->sc_dev, "switching to LCD,CRT,DVI\n");
476 break;
477 case THINKPAD_DISPLAY_ALL:
478 sc->sc_display_state = THINKPAD_DISPLAY_LCD;
479 aprint_normal_dev(sc->sc_dev, "switching to LCD\n");
480 break;
481 }
482
483 param[0].Type = param[1].Type = ACPI_TYPE_INTEGER;
484 params.Pointer = param;
485
486 params.Count = 1;
487 param[0].Integer.Value = 0x80;
488 rv = AcpiEvaluateObject(NULL, "\\VUPS", ¶ms, NULL);
489 if (ACPI_FAILURE(rv))
490 aprint_error_dev(sc->sc_dev, "couldn't evaluate \\VUPS: %s\n",
491 AcpiFormatException(rv));
492
493 params.Count = 2;
494 param[0].Integer.Value = sc->sc_display_state;
495 param[1].Integer.Value = 1;
496 rv = AcpiEvaluateObject(NULL, "\\VSDS", ¶ms, NULL);
497 if (ACPI_FAILURE(rv))
498 aprint_error_dev(sc->sc_dev, "couldn't evaluate \\VSDS: %s\n",
499 AcpiFormatException(rv));
500 }
501
502 static uint8_t
503 thinkpad_brightness_read(thinkpad_softc_t *sc)
504 {
505 #if defined(__i386__) || defined(__amd64__)
506 /*
507 * We have two ways to get the current brightness -- either via
508 * magic RTC registers, or using the EC. Since I don't dare mess
509 * with the EC, and Thinkpads are x86-only, this will have to do
510 * for now.
511 */
512 outb(0x70, 0x6c);
513 return inb(0x71) & 7;
514 #else
515 return 0;
516 #endif
517 }
518
519 static void
520 thinkpad_brightness_up(device_t self)
521 {
522 thinkpad_softc_t *sc = device_private(self);
523
524 if (thinkpad_brightness_read(sc) == 7)
525 return;
526 thinkpad_cmos(sc, THINKPAD_CMOS_BRIGHTNESS_UP);
527 }
528
529 static void
530 thinkpad_brightness_down(device_t self)
531 {
532 thinkpad_softc_t *sc = device_private(self);
533
534 if (thinkpad_brightness_read(sc) == 0)
535 return;
536 thinkpad_cmos(sc, THINKPAD_CMOS_BRIGHTNESS_DOWN);
537 }
538
539 static void
540 thinkpad_cmos(thinkpad_softc_t *sc, uint8_t cmd)
541 {
542 ACPI_OBJECT param;
543 ACPI_OBJECT_LIST params;
544 ACPI_STATUS rv;
545
546 if (sc->sc_cmoshdl_valid == false)
547 return;
548
549 params.Count = 1;
550 params.Pointer = ¶m;
551 param.Type = ACPI_TYPE_INTEGER;
552 param.Integer.Value = cmd;
553 rv = AcpiEvaluateObject(sc->sc_cmoshdl, NULL, ¶ms, NULL);
554 if (ACPI_FAILURE(rv))
555 aprint_error_dev(sc->sc_dev, "couldn't evalute CMOS: %s\n",
556 AcpiFormatException(rv));
557 }
558