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