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