thinkpad_acpi.c revision 1.4 1 /* $NetBSD: thinkpad_acpi.c,v 1.4 2007/12/21 18:43:39 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.4 2007/12/21 18:43:39 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
51 #if defined(__i386__) || defined(__amd64__)
52 #include <machine/pio.h>
53 #endif
54
55 typedef struct thinkpad_softc {
56 device_t sc_dev;
57 struct acpi_devnode *sc_node;
58 ACPI_HANDLE sc_cmoshdl;
59 bool sc_cmoshdl_valid;
60
61 struct sysmon_pswitch sc_smpsw[2];
62 #define TP_PSW_SLEEP 0
63 #define TP_PSW_HIBERNATE 1
64 bool sc_smpsw_valid;
65 } thinkpad_softc_t;
66
67 /* Hotkey events */
68 #define THINKPAD_NOTIFY_FnF1 0x001
69 #define THINKPAD_NOTIFY_LockScreen 0x002
70 #define THINKPAD_NOTIFY_BatteryInfo 0x003
71 #define THINKPAD_NOTIFY_SleepButton 0x004
72 #define THINKPAD_NOTIFY_WirelessSwitch 0x005
73 #define THINKPAD_NOTIFY_FnF6 0x006
74 #define THINKPAD_NOTIFY_DisplayCycle 0x007
75 #define THINKPAD_NOTIFY_PointerSwitch 0x008
76 #define THINKPAD_NOTIFY_EjectButton 0x009
77 #define THINKPAD_NOTIFY_FnF10 0x00a
78 #define THINKPAD_NOTIFY_FnF11 0x00b
79 #define THINKPAD_NOTIFY_HibernateButton 0x00c
80 #define THINKPAD_NOTIFY_BrightnessUp 0x010
81 #define THINKPAD_NOTIFY_BrightnessDown 0x011
82 #define THINKPAD_NOTIFY_ThinkLight 0x012
83 #define THINKPAD_NOTIFY_Zoom 0x014
84 #define THINKPAD_NOTIFY_ThinkVantage 0x018
85
86 #define THINKPAD_CMOS_BRIGHTNESS_UP 0x04
87 #define THINKPAD_CMOS_BRIGHTNESS_DOWN 0x05
88
89 #define THINKPAD_HKEY_VERSION 0x0100
90
91 static int thinkpad_match(device_t, struct cfdata *, void *);
92 static void thinkpad_attach(device_t, device_t, void *);
93
94 static ACPI_STATUS thinkpad_mask_init(thinkpad_softc_t *, uint32_t);
95 static void thinkpad_notify_handler(ACPI_HANDLE, UINT32, void *);
96 static void thinkpad_get_hotkeys(void *);
97
98 static void thinkpad_brightness_up(device_t);
99 static void thinkpad_brightness_down(device_t);
100 static uint8_t thinkpad_brightness_read(thinkpad_softc_t *sc);
101 static void thinkpad_cmos(thinkpad_softc_t *, uint8_t);
102
103 CFATTACH_DECL_NEW(thinkpad, sizeof(thinkpad_softc_t),
104 thinkpad_match, thinkpad_attach, NULL, NULL);
105
106 static const char * const thinkpad_ids[] = {
107 "IBM0068",
108 NULL
109 };
110
111 static int
112 thinkpad_match(device_t parent, struct cfdata *match, void *opaque)
113 {
114 struct acpi_attach_args *aa = (struct acpi_attach_args *)opaque;
115 ACPI_HANDLE hdl;
116 ACPI_INTEGER ver;
117
118 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
119 return 0;
120
121 if (!acpi_match_hid(aa->aa_node->ad_devinfo, thinkpad_ids))
122 return 0;
123
124 /* No point in attaching if we can't find the CMOS method */
125 if (ACPI_FAILURE(AcpiGetHandle(NULL, "\\UCMS", &hdl)))
126 return 0;
127
128 /* We only support hotkey version 0x0100 */
129 if (ACPI_FAILURE(acpi_eval_integer(aa->aa_node->ad_handle, "MHKV",
130 &ver)))
131 return 0;
132
133 if (ver != THINKPAD_HKEY_VERSION)
134 return 0;
135
136 /* Cool, looks like we're good to go */
137 return 1;
138 }
139
140 static void
141 thinkpad_attach(device_t parent, device_t self, void *opaque)
142 {
143 thinkpad_softc_t *sc = device_private(self);
144 struct acpi_attach_args *aa = (struct acpi_attach_args *)opaque;
145 ACPI_STATUS rv;
146 ACPI_INTEGER val;
147
148 sc->sc_node = aa->aa_node;
149 sc->sc_dev = self;
150
151 aprint_naive("\n");
152 aprint_normal("\n");
153
154 /* T61 uses \UCMS method for issuing CMOS commands */
155 rv = AcpiGetHandle(NULL, "\\UCMS", &sc->sc_cmoshdl);
156 if (ACPI_FAILURE(rv))
157 sc->sc_cmoshdl_valid = false;
158 else {
159 aprint_verbose_dev(self, "using CMOS at \\UCMS\n");
160 sc->sc_cmoshdl_valid = true;
161 }
162
163 /* Get the supported event mask */
164 rv = acpi_eval_integer(sc->sc_node->ad_handle, "MHKA", &val);
165 if (ACPI_FAILURE(rv)) {
166 aprint_error_dev(self, "couldn't evaluate MHKA: %s\n",
167 AcpiFormatException(rv));
168 goto fail;
169 }
170
171 /* Enable all supported events */
172 rv = thinkpad_mask_init(sc, val);
173 if (ACPI_FAILURE(rv)) {
174 aprint_error_dev(self, "couldn't set event mask: %s\n",
175 AcpiFormatException(rv));
176 goto fail;
177 }
178
179 /* Install notify handler for events */
180 rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
181 ACPI_DEVICE_NOTIFY, thinkpad_notify_handler, sc);
182 if (ACPI_FAILURE(rv))
183 aprint_error_dev(self, "couldn't install notify handler: %s\n",
184 AcpiFormatException(rv));
185
186 /* Register with sysmon */
187 sc->sc_smpsw_valid = true;
188
189 sc->sc_smpsw[TP_PSW_SLEEP].smpsw_name = device_xname(self);
190 sc->sc_smpsw[TP_PSW_SLEEP].smpsw_type = PSWITCH_TYPE_SLEEP;
191 #if notyet
192 sc->sc_smpsw[TP_PSW_HIBERNATE].smpsw_name = device_xname(self);
193 sc->sc_smpsw[TP_PSW_HIBERNATE].smpsw_type = PSWITCH_TYPE_HIBERNATE;
194 #endif
195
196 if (sysmon_pswitch_register(&sc->sc_smpsw[TP_PSW_SLEEP]) != 0) {
197 aprint_error_dev(self, "couldn't register with sysmon\n");
198 sc->sc_smpsw_valid = false;
199 }
200 #if notyet
201 if (sysmon_pswitch_register(&sc->sc_smpsw[TP_PSW_HIBERNATE]) != 0) {
202 aprint_error_dev(self, "couldn't register with sysmon\n");
203 sc->sc_smpsw_valid = false;
204 }
205 #endif
206
207 fail:
208 if (!pmf_device_register(self, NULL, NULL))
209 aprint_error_dev(self, "couldn't establish power handler\n");
210 if (!pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_UP,
211 thinkpad_brightness_up, true))
212 aprint_error_dev(self, "couldn't register event handler\n");
213 if (!pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_DOWN,
214 thinkpad_brightness_down, true))
215 aprint_error_dev(self, "couldn't register event handler\n");
216
217 return;
218 }
219
220 static void
221 thinkpad_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
222 {
223 thinkpad_softc_t *sc = (thinkpad_softc_t *)opaque;
224 device_t self = sc->sc_dev;
225 ACPI_STATUS rv;
226
227 if (notify != 0x80) {
228 aprint_debug_dev(self, "unknown notify 0x%02x\n", notify);
229 return;
230 }
231
232 rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, thinkpad_get_hotkeys, sc);
233 if (ACPI_FAILURE(rv))
234 aprint_error_dev(self, "couldn't queue hotkey handler: %s\n",
235 AcpiFormatException(rv));
236 }
237
238 static void
239 thinkpad_get_hotkeys(void *opaque)
240 {
241 thinkpad_softc_t *sc = (thinkpad_softc_t *)opaque;
242 device_t self = sc->sc_dev;
243 ACPI_STATUS rv;
244 ACPI_INTEGER val;
245 int type, event;
246
247 for (;;) {
248 rv = acpi_eval_integer(sc->sc_node->ad_handle, "MHKP", &val);
249 if (ACPI_FAILURE(rv)) {
250 aprint_error_dev(self, "couldn't evaluate MHKP: %s\n",
251 AcpiFormatException(rv));
252 return;
253 }
254
255 if (val == 0)
256 return;
257
258 type = (val & 0xf000) >> 12;
259 event = val & 0x0fff;
260
261 if (type != 1)
262 /* Only type 1 events are supported for now */
263 continue;
264
265 switch (event) {
266 case THINKPAD_NOTIFY_BrightnessUp:
267 thinkpad_brightness_up(self);
268 break;
269 case THINKPAD_NOTIFY_BrightnessDown:
270 thinkpad_brightness_down(self);
271 break;
272 case THINKPAD_NOTIFY_DisplayCycle:
273 #if notyet
274 pmf_event_inject(NULL, PMFE_DISPLAY_CYCLE);
275 #endif
276 break;
277 case THINKPAD_NOTIFY_SleepButton:
278 if (sc->sc_smpsw_valid == false)
279 break;
280 sysmon_pswitch_event(&sc->sc_smpsw[TP_PSW_SLEEP],
281 PSWITCH_EVENT_PRESSED);
282 break;
283 case THINKPAD_NOTIFY_HibernateButton:
284 #if notyet
285 if (sc->sc_smpsw_valid == false)
286 break;
287 sysmon_pswitch_event(&sc->sc_smpsw[TP_PSW_HIBERNATE],
288 PSWITCH_EVENT_PRESSED);
289 break;
290 #endif
291 case THINKPAD_NOTIFY_FnF1:
292 case THINKPAD_NOTIFY_LockScreen:
293 case THINKPAD_NOTIFY_BatteryInfo:
294 case THINKPAD_NOTIFY_WirelessSwitch:
295 case THINKPAD_NOTIFY_FnF6:
296 case THINKPAD_NOTIFY_PointerSwitch:
297 case THINKPAD_NOTIFY_EjectButton:
298 case THINKPAD_NOTIFY_FnF10:
299 case THINKPAD_NOTIFY_FnF11:
300 case THINKPAD_NOTIFY_ThinkLight:
301 case THINKPAD_NOTIFY_Zoom:
302 case THINKPAD_NOTIFY_ThinkVantage:
303 /* XXXJDM we should deliver hotkeys as keycodes */
304 break;
305 default:
306 aprint_debug_dev(self, "notify event 0x%03x\n", event);
307 break;
308 }
309 }
310
311 return;
312 }
313
314 static ACPI_STATUS
315 thinkpad_mask_init(thinkpad_softc_t *sc, uint32_t mask)
316 {
317 ACPI_OBJECT param[2];
318 ACPI_OBJECT_LIST params;
319 ACPI_STATUS rv;
320 int i;
321
322 /* Update hotkey mask */
323 params.Count = 2;
324 params.Pointer = param;
325 param[0].Type = param[1].Type = ACPI_TYPE_INTEGER;
326
327 for (i = 0; i < 32; i++) {
328 param[0].Integer.Value = i + 1;
329 param[1].Integer.Value = (((1 << i) & mask) != 0);
330
331 rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "MHKM",
332 ¶ms, NULL);
333 if (ACPI_FAILURE(rv))
334 return rv;
335 }
336
337 /* Enable hotkey events */
338 params.Count = 1;
339 param[0].Integer.Value = 1;
340 rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "MHKC", ¶ms, NULL);
341 if (ACPI_FAILURE(rv)) {
342 aprint_error_dev(sc->sc_dev, "couldn't enable hotkeys: %s\n",
343 AcpiFormatException(rv));
344 return rv;
345 }
346
347 /* Claim ownership of brightness control */
348 param[0].Integer.Value = 0;
349 (void)AcpiEvaluateObject(sc->sc_node->ad_handle, "PWMS", ¶ms, NULL);
350
351 return AE_OK;
352 }
353
354 static uint8_t
355 thinkpad_brightness_read(thinkpad_softc_t *sc)
356 {
357 #if defined(__i386__) || defined(__amd64__)
358 /*
359 * We have two ways to get the current brightness -- either via
360 * magic RTC registers, or using the EC. Since I don't dare mess
361 * with the EC, and Thinkpads are x86-only, this will have to do
362 * for now.
363 */
364 outb(0x70, 0x6c);
365 return inb(0x71) & 7;
366 #else
367 return 0;
368 #endif
369 }
370
371 static void
372 thinkpad_brightness_up(device_t self)
373 {
374 thinkpad_softc_t *sc = device_private(self);
375
376 if (thinkpad_brightness_read(sc) == 7)
377 return;
378 thinkpad_cmos(sc, THINKPAD_CMOS_BRIGHTNESS_UP);
379 }
380
381 static void
382 thinkpad_brightness_down(device_t self)
383 {
384 thinkpad_softc_t *sc = device_private(self);
385
386 if (thinkpad_brightness_read(sc) == 0)
387 return;
388 thinkpad_cmos(sc, THINKPAD_CMOS_BRIGHTNESS_DOWN);
389 }
390
391 static void
392 thinkpad_cmos(thinkpad_softc_t *sc, uint8_t cmd)
393 {
394 ACPI_OBJECT param;
395 ACPI_OBJECT_LIST params;
396 ACPI_STATUS rv;
397
398 if (sc->sc_cmoshdl_valid == false)
399 return;
400
401 params.Count = 1;
402 params.Pointer = ¶m;
403 param.Type = ACPI_TYPE_INTEGER;
404 param.Integer.Value = cmd;
405 rv = AcpiEvaluateObject(sc->sc_cmoshdl, NULL, ¶ms, NULL);
406 if (ACPI_FAILURE(rv))
407 aprint_error_dev(sc->sc_dev, "couldn't evalute CMOS: %s\n",
408 AcpiFormatException(rv));
409 }
410