sony_acpi.c revision 1.5 1 /* $NetBSD: sony_acpi.c,v 1.5 2008/03/26 18:35:17 xtraeme Exp $ */
2
3 /*-
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: sony_acpi.c,v 1.5 2008/03/26 18:35:17 xtraeme Exp $");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 #include <sys/proc.h>
45 #include <sys/kernel.h>
46 #include <sys/callout.h>
47 #include <sys/sysctl.h>
48
49 #include <machine/bus.h>
50
51 #include <dev/acpi/acpica.h>
52 #include <dev/acpi/acpivar.h>
53
54 #define SONY_NOTIFY_FnKeyEvent 0x92
55 #define SONY_NOTIFY_BrightnessDownPressed 0x85
56 #define SONY_NOTIFY_BrightnessDownReleased 0x05
57 #define SONY_NOTIFY_BrightnessUpPressed 0x86
58 #define SONY_NOTIFY_BrightnessUpReleased 0x06
59 #define SONY_NOTIFY_DisplaySwitchPressed 0x87
60 #define SONY_NOTIFY_DisplaySwitchReleased 0x07
61 #define SONY_NOTIFY_ZoomPressed 0x8a
62 #define SONY_NOTIFY_ZoomReleased 0x0a
63 #define SONY_NOTIFY_SuspendPressed 0x8c
64 #define SONY_NOTIFY_SuspendReleased 0x0c
65
66 struct sony_acpi_softc {
67 device_t sc_dev;
68 struct sysctllog *sc_log;
69 struct acpi_devnode *sc_node;
70
71 #define SONY_PSW_SLEEP 0
72 #define SONY_PSW_DISPLAY_CYCLE 1
73 #define SONY_PSW_ZOOM 2
74 #define SONY_PSW_LAST 3
75 struct sysmon_pswitch sc_smpsw[SONY_PSW_LAST];
76 int sc_smpsw_valid;
77
78 #define SONY_ACPI_QUIRK_FNINIT 0x01
79 int sc_quirks;
80 bool sc_has_pic;
81
82 struct sony_acpi_pmstate {
83 ACPI_INTEGER brt;
84 } sc_pmstate;
85 };
86
87 static const char * const sony_acpi_ids[] = {
88 "SNY5001",
89 NULL
90 };
91
92 static int sony_acpi_match(device_t, cfdata_t, void *);
93 static void sony_acpi_attach(device_t, device_t, void *);
94 static ACPI_STATUS sony_acpi_eval_set_integer(ACPI_HANDLE, const char *,
95 ACPI_INTEGER, ACPI_INTEGER *);
96 static void sony_acpi_quirk_setup(struct sony_acpi_softc *);
97 static void sony_acpi_notify_handler(ACPI_HANDLE, UINT32, void *);
98 static bool sony_acpi_suspend(device_t PMF_FN_PROTO);
99 static bool sony_acpi_resume(device_t PMF_FN_PROTO);
100 static void sony_acpi_brightness_down(device_t);
101 static void sony_acpi_brightness_up(device_t);
102 static ACPI_STATUS sony_acpi_find_pic(ACPI_HANDLE, UINT32, void *, void **);
103
104 CFATTACH_DECL_NEW(sony_acpi, sizeof(struct sony_acpi_softc),
105 sony_acpi_match, sony_acpi_attach, NULL, NULL);
106
107 static int
108 sony_acpi_match(device_t parent, cfdata_t match, void *aux)
109 {
110 struct acpi_attach_args *aa = aux;
111
112 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
113 return 0;
114
115 return acpi_match_hid(aa->aa_node->ad_devinfo, sony_acpi_ids);
116 }
117
118 static int
119 sony_sysctl_helper(SYSCTLFN_ARGS)
120 {
121 struct sysctlnode node;
122 ACPI_INTEGER acpi_val;
123 ACPI_STATUS rv;
124 int val, old_val, error;
125 char buf[SYSCTL_NAMELEN + 1], *ptr;
126 struct sony_acpi_softc *sc = rnode->sysctl_data;
127
128 (void)snprintf(buf, sizeof(buf), "G%s", rnode->sysctl_name);
129 for (ptr = buf; *ptr; ptr++)
130 *ptr = toupper(*ptr);
131
132 rv = acpi_eval_integer(sc->sc_node->ad_handle, buf, &acpi_val);
133 if (ACPI_FAILURE(rv)) {
134 #ifdef DIAGNOSTIC
135 printf("%s: couldn't get `%s'\n", device_xname(sc->sc_dev), buf);
136 #endif
137 return EIO;
138 }
139 val = old_val = acpi_val;
140
141 node = *rnode;
142 node.sysctl_data = &val;
143
144 error = sysctl_lookup(SYSCTLFN_CALL(&node));
145 if (error || newp == NULL)
146 return error;
147
148 (void)snprintf(buf, sizeof(buf), "S%s", rnode->sysctl_name);
149 acpi_val = val;
150 rv = sony_acpi_eval_set_integer(sc->sc_node->ad_handle, buf,
151 acpi_val, NULL);
152 if (ACPI_FAILURE(rv)) {
153 #ifdef DIAGNOSTIC
154 printf("%s: couldn't set `%s' to %d\n",
155 device_xname(sc->sc_dev), buf, val);
156 #endif
157 return EIO;
158 }
159 return 0;
160 }
161
162 static ACPI_STATUS
163 sony_walk_cb(ACPI_HANDLE hnd, UINT32 v, void *context, void **status)
164 {
165 struct sony_acpi_softc *sc = (void *)context;
166 const struct sysctlnode *node, *snode;
167 const char *name = acpi_name(hnd);
168 ACPI_INTEGER acpi_val;
169 char buf[SYSCTL_NAMELEN + 1], *ptr;
170 int rv;
171
172 if ((name = strrchr(name, '.')) == NULL)
173 return AE_OK;
174
175 name++;
176 if ((*name != 'G') && (*name != 'S'))
177 return AE_OK;
178
179 (void)strlcpy(buf, name, sizeof(buf));
180 *buf = 'G';
181
182 /*
183 * We assume that if the 'get' of the name as an integer is
184 * successful it is ok.
185 */
186 if (acpi_eval_integer(sc->sc_node->ad_handle, buf, &acpi_val))
187 return AE_OK;
188
189 for (ptr = buf; *ptr; ptr++)
190 *ptr = tolower(*ptr);
191
192 if ((rv = sysctl_createv(&sc->sc_log, 0, NULL, &node, CTLFLAG_PERMANENT,
193 CTLTYPE_NODE, "hw", NULL, NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
194 goto out;
195
196 if ((rv = sysctl_createv(&sc->sc_log, 0, &node, &snode, 0,
197 CTLTYPE_NODE, device_xname(sc->sc_dev),
198 SYSCTL_DESCR("sony controls"),
199 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
200 goto out;
201
202 if ((rv = sysctl_createv(&sc->sc_log, 0, &snode, &node,
203 CTLFLAG_READWRITE, CTLTYPE_INT, buf + 1, NULL,
204 sony_sysctl_helper, 0, sc, 0, CTL_CREATE, CTL_EOL)) != 0)
205 goto out;
206
207 out:
208 #ifdef DIAGNOSTIC
209 if (rv)
210 printf("%s: sysctl_createv failed (rv = %d)\n",
211 device_xname(sc->sc_dev), rv);
212 #endif
213 return AE_OK;
214 }
215
216 ACPI_STATUS
217 sony_acpi_eval_set_integer(ACPI_HANDLE handle, const char *path,
218 ACPI_INTEGER val, ACPI_INTEGER *valp)
219 {
220 ACPI_STATUS rv;
221 ACPI_BUFFER buf;
222 ACPI_OBJECT param, ret_val;
223 ACPI_OBJECT_LIST params;
224
225 if (handle == NULL)
226 handle = ACPI_ROOT_OBJECT;
227
228 params.Count = 1;
229 params.Pointer = ¶m;
230
231 param.Type = ACPI_TYPE_INTEGER;
232 param.Integer.Value = val;
233
234 buf.Pointer = &ret_val;
235 buf.Length = sizeof(ret_val);
236
237 rv = AcpiEvaluateObjectTyped(handle, path, ¶ms, &buf,
238 ACPI_TYPE_INTEGER);
239
240 if (ACPI_SUCCESS(rv) && valp)
241 *valp = ret_val.Integer.Value;
242
243 return rv;
244 }
245
246 static void
247 sony_acpi_attach(device_t parent, device_t self, void *aux)
248 {
249 struct sony_acpi_softc *sc = device_private(self);
250 struct acpi_attach_args *aa = aux;
251 ACPI_STATUS rv;
252 int i;
253
254 aprint_naive(": Sony Miscellaneous Controller\n");
255 aprint_normal(": Sony Miscellaneous Controller\n");
256
257 sc->sc_node = aa->aa_node;
258 sc->sc_dev = self;
259
260 rv = AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 100,
261 sony_acpi_find_pic, sc, NULL);
262 if (ACPI_FAILURE(rv))
263 aprint_error_dev(self, "couldn't walk namespace: %s\n",
264 AcpiFormatException(rv));
265
266 /*
267 * If we don't find an SNY6001 device, assume that we need the
268 * Fn key initialization sequence.
269 */
270 if (sc->sc_has_pic == false)
271 sc->sc_quirks |= SONY_ACPI_QUIRK_FNINIT;
272
273 sony_acpi_quirk_setup(sc);
274
275 /* Configure suspend button and hotkeys */
276 sc->sc_smpsw[SONY_PSW_SLEEP].smpsw_name = device_xname(self);
277 sc->sc_smpsw[SONY_PSW_SLEEP].smpsw_type = PSWITCH_TYPE_SLEEP;
278 sc->sc_smpsw[SONY_PSW_DISPLAY_CYCLE].smpsw_name =
279 PSWITCH_HK_DISPLAY_CYCLE;
280 sc->sc_smpsw[SONY_PSW_DISPLAY_CYCLE].smpsw_type = PSWITCH_TYPE_HOTKEY;
281 sc->sc_smpsw[SONY_PSW_ZOOM].smpsw_name = PSWITCH_HK_ZOOM_BUTTON;
282 sc->sc_smpsw[SONY_PSW_ZOOM].smpsw_type = PSWITCH_TYPE_HOTKEY;
283 sc->sc_smpsw_valid = 1;
284
285 for (i = 0; i < SONY_PSW_LAST; i++)
286 if (sysmon_pswitch_register(&sc->sc_smpsw[i]) != 0) {
287 aprint_error_dev(self,
288 "couldn't register %s with sysmon\n",
289 sc->sc_smpsw[i].smpsw_name);
290 sc->sc_smpsw_valid = 0;
291 }
292
293 /* Install notify handler */
294 rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
295 ACPI_DEVICE_NOTIFY, sony_acpi_notify_handler, self);
296 if (ACPI_FAILURE(rv))
297 aprint_error_dev(self,
298 "couldn't install notify handler (%d)\n", rv);
299
300 /* Install sysctl handler */
301 rv = AcpiWalkNamespace(ACPI_TYPE_METHOD,
302 sc->sc_node->ad_handle, 1, sony_walk_cb, sc, NULL);
303 #ifdef DIAGNOSTIC
304 if (ACPI_FAILURE(rv))
305 aprint_error_dev(self, "Cannot walk ACPI namespace (%d)\n",
306 rv);
307 #endif
308
309 if (!pmf_device_register(self, sony_acpi_suspend, sony_acpi_resume))
310 aprint_error_dev(self, "couldn't establish power handler\n");
311
312 if (!pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_UP,
313 sony_acpi_brightness_up, true))
314 aprint_error_dev(self, "couldn't register BRIGHTNESS UP handler\n");
315
316 if (!pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_DOWN,
317 sony_acpi_brightness_down, true))
318 aprint_error_dev(self, "couldn't register BRIGHTNESS DOWN handler\n");
319 }
320
321 static void
322 sony_acpi_quirk_setup(struct sony_acpi_softc *sc)
323 {
324 ACPI_HANDLE hdl = sc->sc_node->ad_handle;
325
326 if (sc->sc_quirks & SONY_ACPI_QUIRK_FNINIT) {
327 /* Initialize extra Fn keys */
328 sony_acpi_eval_set_integer(hdl, "SN02", 0x04, NULL);
329 sony_acpi_eval_set_integer(hdl, "SN07", 0x02, NULL);
330 sony_acpi_eval_set_integer(hdl, "SN02", 0x10, NULL);
331 sony_acpi_eval_set_integer(hdl, "SN07", 0x00, NULL);
332 sony_acpi_eval_set_integer(hdl, "SN03", 0x02, NULL);
333 sony_acpi_eval_set_integer(hdl, "SN07", 0x101, NULL);
334 }
335 }
336
337 static void
338 sony_acpi_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
339 {
340 device_t dv = opaque;
341 struct sony_acpi_softc *sc = device_private(dv);
342 ACPI_STATUS rv;
343 ACPI_INTEGER arg;
344 int s;
345
346 if (notify == SONY_NOTIFY_FnKeyEvent) {
347 rv = sony_acpi_eval_set_integer(hdl, "SN07", 0x202, &arg);
348 if (ACPI_FAILURE(rv))
349 return;
350
351 notify = arg & 0xff;
352 }
353
354 s = spltty();
355 switch (notify) {
356 case SONY_NOTIFY_BrightnessDownPressed:
357 sony_acpi_brightness_down(dv);
358 break;
359 case SONY_NOTIFY_BrightnessUpPressed:
360 sony_acpi_brightness_up(dv);
361 break;
362 case SONY_NOTIFY_BrightnessDownReleased:
363 case SONY_NOTIFY_BrightnessUpReleased:
364 break;
365 case SONY_NOTIFY_SuspendPressed:
366 if (!sc->sc_smpsw_valid)
367 break;
368 sysmon_pswitch_event(&sc->sc_smpsw[SONY_PSW_SLEEP],
369 PSWITCH_EVENT_PRESSED);
370 break;
371 case SONY_NOTIFY_SuspendReleased:
372 break;
373 case SONY_NOTIFY_DisplaySwitchPressed:
374 if (!sc->sc_smpsw_valid)
375 break;
376 sysmon_pswitch_event(&sc->sc_smpsw[SONY_PSW_DISPLAY_CYCLE],
377 PSWITCH_EVENT_PRESSED);
378 break;
379 case SONY_NOTIFY_DisplaySwitchReleased:
380 break;
381 case SONY_NOTIFY_ZoomPressed:
382 if (!sc->sc_smpsw_valid)
383 break;
384 sysmon_pswitch_event(&sc->sc_smpsw[SONY_PSW_ZOOM],
385 PSWITCH_EVENT_PRESSED);
386 break;
387 case SONY_NOTIFY_ZoomReleased:
388 break;
389 default:
390 aprint_debug_dev(dv, "unknown notify event 0x%x\n",
391 notify);
392 break;
393 }
394 splx(s);
395 }
396
397 static bool
398 sony_acpi_suspend(device_t dv PMF_FN_ARGS)
399 {
400 struct sony_acpi_softc *sc = device_private(dv);
401
402 acpi_eval_integer(sc->sc_node->ad_handle, "GBRT", &sc->sc_pmstate.brt);
403
404 return true;
405 }
406
407 static bool
408 sony_acpi_resume(device_t dv PMF_FN_ARGS)
409 {
410 struct sony_acpi_softc *sc = device_private(dv);
411
412 sony_acpi_eval_set_integer(sc->sc_node->ad_handle, "SBRT",
413 sc->sc_pmstate.brt, NULL);
414 sony_acpi_quirk_setup(sc);
415
416 return true;
417 }
418
419 static void
420 sony_acpi_brightness_up(device_t dv)
421 {
422 struct sony_acpi_softc *sc = device_private(dv);
423 ACPI_INTEGER arg;
424 ACPI_STATUS rv;
425
426 rv = acpi_eval_integer(sc->sc_node->ad_handle, "GBRT", &arg);
427 if (ACPI_FAILURE(rv))
428 return;
429 if (arg >= 8)
430 arg = 8;
431 else
432 arg++;
433 sony_acpi_eval_set_integer(sc->sc_node->ad_handle, "SBRT", arg, NULL);
434 }
435
436 static void
437 sony_acpi_brightness_down(device_t dv)
438 {
439 struct sony_acpi_softc *sc = device_private(dv);
440 ACPI_INTEGER arg;
441 ACPI_STATUS rv;
442
443 rv = acpi_eval_integer(sc->sc_node->ad_handle, "GBRT", &arg);
444 if (ACPI_FAILURE(rv))
445 return;
446 if (arg <= 0)
447 arg = 0;
448 else
449 arg--;
450 sony_acpi_eval_set_integer(sc->sc_node->ad_handle, "SBRT", arg, NULL);
451 }
452
453 static ACPI_STATUS
454 sony_acpi_find_pic(ACPI_HANDLE hdl, UINT32 level, void *opaque, void **status)
455 {
456 struct sony_acpi_softc *sc = opaque;
457 ACPI_BUFFER buf;
458 ACPI_STATUS rv;
459 ACPI_DEVICE_INFO *devinfo;
460
461 buf.Pointer = NULL;
462 buf.Length = ACPI_ALLOCATE_BUFFER;
463 rv = AcpiGetObjectInfo(hdl, &buf);
464 if (ACPI_FAILURE(rv) || buf.Pointer == NULL)
465 return AE_OK; /* we don't want to stop searching */
466
467 devinfo = buf.Pointer;
468 if (strncmp(devinfo->HardwareId.Value, "SNY6001", 7) == 0)
469 sc->sc_has_pic = true;
470
471 AcpiOsFree(buf.Pointer);
472
473 return AE_OK;
474 }
475