fujhk_acpi.c revision 1.2 1 /* $NetBSD: fujhk_acpi.c,v 1.2 2012/06/02 21:36:43 dsl Exp $ */
2
3 /*-
4 * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Gregoire Sutre.
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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: fujhk_acpi.c,v 1.2 2012/06/02 21:36:43 dsl Exp $");
34
35 #include <sys/param.h>
36 #include <sys/device.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/sysctl.h>
40
41 #include <dev/sysmon/sysmonvar.h>
42
43 #include <dev/acpi/acpireg.h>
44 #include <dev/acpi/acpivar.h>
45
46 #define _COMPONENT ACPI_RESOURCE_COMPONENT
47 ACPI_MODULE_NAME ("fujhk_acpi")
48
49 #define FUJITSU_HK_NOTIFY 0x80
50 #define FUJITSU_HK_MODMASK 0xc0000000
51
52 /* Values returned by the GIRB method. */
53 #define FUJITSU_HK_IRB_HOTKEY_RELEASE 0x0
54 #define FUJITSU_HK_IRB_HOTKEY_PRESS(x) (0x410 | x)
55
56 /* Hotkey index of a value returned by the GIRB method. */
57 #define FUJITSU_HK_IRB_HOTKEY_INDEX(x) (x & 0x3)
58
59 /* Values for the first argument of the FUNC method. */
60 #define FUJITSU_FUNC_TARGET_BACKLIGHT 0x1004
61
62 /* Values for the second argument of the FUNC method. */
63 #define FUJITSU_FUNC_COMMAND_SET 0x1
64 #define FUJITSU_FUNC_COMMAND_GET 0x2
65
66 /* Backlight values for the FUNC method. */
67 #define FUJITSU_FUNC_BACKLIGHT_ON 0x0
68 #define FUJITSU_FUNC_BACKLIGHT_REDUCED 0x2
69 #define FUJITSU_FUNC_BACKLIGHT_OFF 0x3
70
71 /* Value returned by FUNC on invalid arguments. */
72 #define FUJITSU_FUNC_INVALID_ARGS 0x80000000
73
74 /* ACPI Fujitsu hotkeys controller capabilities (methods). */
75 #define FUJITSU_HK_CAP_GIRB __BIT(0)
76 #define FUJITSU_HK_CAP_FUNC __BIT(1)
77
78 struct fujitsu_hk_softc {
79 device_t sc_dev;
80 struct acpi_devnode *sc_node;
81 struct sysctllog *sc_log;
82 kmutex_t sc_mtx;
83 uint16_t sc_caps;
84 #define FUJITSU_HK_PSW_COUNT 4
85 struct sysmon_pswitch sc_smpsw[FUJITSU_HK_PSW_COUNT];
86 char sc_smpsw_name[FUJITSU_HK_PSW_COUNT][16];
87 };
88
89 static const char * const fujitsu_hk_hid[] = {
90 "FUJ02E3",
91 NULL
92 };
93
94 static int fujitsu_hk_match(device_t, cfdata_t, void *);
95 static void fujitsu_hk_attach(device_t, device_t, void *);
96 static int fujitsu_hk_detach(device_t, int);
97 static bool fujitsu_hk_suspend(device_t, const pmf_qual_t *);
98 static bool fujitsu_hk_resume(device_t, const pmf_qual_t *);
99 static uint16_t fujitsu_hk_capabilities(const struct acpi_devnode *);
100 static void fujitsu_hk_notify_handler(ACPI_HANDLE, uint32_t, void *);
101 static void fujitsu_hk_event_callback(void *);
102 static void fujitsu_hk_sysctl_setup(struct fujitsu_hk_softc *);
103 static int fujitsu_hk_sysctl_backlight(SYSCTLFN_PROTO);
104 static int fujitsu_hk_get_irb(struct fujitsu_hk_softc *, uint32_t *);
105 static int fujitsu_hk_get_backlight(struct fujitsu_hk_softc *, bool *);
106 static int fujitsu_hk_set_backlight(struct fujitsu_hk_softc *, bool);
107 static bool fujitsu_hk_cap(ACPI_HANDLE, const char *, ACPI_OBJECT_TYPE);
108 static ACPI_STATUS fujitsu_hk_eval_nary_integer(ACPI_HANDLE,
109 const char *, const
110 ACPI_INTEGER *, uint8_t, ACPI_INTEGER *);
111
112 CFATTACH_DECL_NEW(fujhk, sizeof(struct fujitsu_hk_softc),
113 fujitsu_hk_match, fujitsu_hk_attach, fujitsu_hk_detach, NULL);
114
115 static int
116 fujitsu_hk_match(device_t parent, cfdata_t match, void *aux)
117 {
118 struct acpi_attach_args *aa = aux;
119
120 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
121 return 0;
122
123 return acpi_match_hid(aa->aa_node->ad_devinfo, fujitsu_hk_hid);
124 }
125
126 static void
127 fujitsu_hk_attach(device_t parent, device_t self, void *aux)
128 {
129 struct fujitsu_hk_softc *sc = device_private(self);
130 struct acpi_attach_args *aa = aux;
131 struct acpi_devnode *ad = aa->aa_node;
132 int i;
133
134 aprint_naive(": Fujitsu Hotkeys\n");
135 aprint_normal(": Fujitsu Hotkeys\n");
136
137 sc->sc_dev = self;
138 sc->sc_node = ad;
139 sc->sc_log = NULL;
140 sc->sc_caps = fujitsu_hk_capabilities(ad);
141
142 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
143
144 for (i = 0; i < FUJITSU_HK_PSW_COUNT; i++) {
145 (void)snprintf(sc->sc_smpsw_name[i],
146 sizeof(sc->sc_smpsw_name[i]), "%s-%d",
147 device_xname(self), i);
148 sc->sc_smpsw[i].smpsw_name = sc->sc_smpsw_name[i];
149 sc->sc_smpsw[i].smpsw_type = PSWITCH_TYPE_HOTKEY;
150 (void)sysmon_pswitch_register(&sc->sc_smpsw[i]);
151 }
152
153 fujitsu_hk_sysctl_setup(sc);
154
155 (void)pmf_device_register(self, fujitsu_hk_suspend, fujitsu_hk_resume);
156 (void)acpi_register_notify(sc->sc_node, fujitsu_hk_notify_handler);
157 }
158
159 static int
160 fujitsu_hk_detach(device_t self, int flags)
161 {
162 struct fujitsu_hk_softc *sc = device_private(self);
163 int i;
164
165 pmf_device_deregister(self);
166
167 if (sc->sc_log != NULL)
168 sysctl_teardown(&sc->sc_log);
169
170 acpi_deregister_notify(sc->sc_node);
171
172 for (i = 0; i < FUJITSU_HK_PSW_COUNT; i++)
173 sysmon_pswitch_unregister(&sc->sc_smpsw[i]);
174
175 mutex_destroy(&sc->sc_mtx);
176
177 return 0;
178 }
179
180 /*
181 * On the P7120, the backlight needs to be enabled after resume, since the
182 * laptop wakes up with the backlight off (even if it was on before suspend).
183 */
184 static bool
185 fujitsu_hk_suspend(device_t self, const pmf_qual_t *qual)
186 {
187 struct fujitsu_hk_softc *sc = device_private(self);
188
189 mutex_enter(&sc->sc_mtx);
190 (void)fujitsu_hk_set_backlight(sc, false);
191 mutex_exit(&sc->sc_mtx);
192
193 return true;
194 }
195
196 static bool
197 fujitsu_hk_resume(device_t self, const pmf_qual_t *qual)
198 {
199 struct fujitsu_hk_softc *sc = device_private(self);
200
201 mutex_enter(&sc->sc_mtx);
202 (void)fujitsu_hk_set_backlight(sc, true);
203 mutex_exit(&sc->sc_mtx);
204
205 return true;
206 }
207
208 static uint16_t
209 fujitsu_hk_capabilities(const struct acpi_devnode *ad)
210 {
211 uint16_t caps;
212
213 caps = 0;
214
215 if (fujitsu_hk_cap(ad->ad_handle, "GIRB", ACPI_TYPE_INTEGER))
216 caps |= FUJITSU_HK_CAP_GIRB;
217
218 if (fujitsu_hk_cap(ad->ad_handle, "FUNC", ACPI_TYPE_METHOD))
219 caps |= FUJITSU_HK_CAP_FUNC;
220
221 return caps;
222 }
223
224 static void
225 fujitsu_hk_notify_handler(ACPI_HANDLE handle, uint32_t evt, void *context)
226 {
227 struct fujitsu_hk_softc *sc = device_private(context);
228 static const int handler = OSL_NOTIFY_HANDLER;
229
230 switch (evt) {
231
232 case FUJITSU_HK_NOTIFY:
233 (void)AcpiOsExecute(handler, fujitsu_hk_event_callback, sc);
234 break;
235
236 default:
237 aprint_debug_dev(sc->sc_dev, "unknown notify 0x%02X\n", evt);
238 }
239 }
240
241 static void
242 fujitsu_hk_event_callback(void *arg)
243 {
244 struct fujitsu_hk_softc *sc = arg;
245 const int max_irb_buffer_size = 100;
246 uint32_t irb;
247 int i, index;
248
249 for (i = 0; i < max_irb_buffer_size; i++) {
250 if (fujitsu_hk_get_irb(sc, &irb) || irb == 0)
251 return;
252
253 switch (irb & ~FUJITSU_HK_MODMASK) {
254 case FUJITSU_HK_IRB_HOTKEY_RELEASE:
255 /* Hotkey button release event (nothing to do). */
256 break;
257 case FUJITSU_HK_IRB_HOTKEY_PRESS(0):
258 case FUJITSU_HK_IRB_HOTKEY_PRESS(1):
259 case FUJITSU_HK_IRB_HOTKEY_PRESS(2):
260 case FUJITSU_HK_IRB_HOTKEY_PRESS(3):
261 /* Hotkey button press event. */
262 index = FUJITSU_HK_IRB_HOTKEY_INDEX(irb);
263 sysmon_pswitch_event(&sc->sc_smpsw[index],
264 PSWITCH_EVENT_PRESSED);
265 break;
266 default:
267 aprint_error_dev(sc->sc_dev,
268 "unknown GIRB result: 0x%"PRIx32"\n", irb);
269 break;
270 }
271 }
272 }
273
274 static void
275 fujitsu_hk_sysctl_setup(struct fujitsu_hk_softc *sc)
276 {
277 const struct sysctlnode *rnode;
278 bool dummy_state;
279
280 if (fujitsu_hk_get_backlight(sc, &dummy_state) == 0) {
281 if ((sysctl_createv(&sc->sc_log, 0, NULL, &rnode,
282 0, CTLTYPE_NODE, "hw", NULL,
283 NULL, 0, NULL, 0,
284 CTL_HW, CTL_EOL)) != 0)
285 goto fail;
286
287 if ((sysctl_createv(&sc->sc_log, 0, &rnode, &rnode,
288 0, CTLTYPE_NODE, "acpi", NULL,
289 NULL, 0, NULL, 0,
290 CTL_CREATE, CTL_EOL)) != 0)
291 goto fail;
292
293 if ((sysctl_createv(&sc->sc_log, 0, &rnode, &rnode,
294 0, CTLTYPE_NODE, device_xname(sc->sc_dev),
295 SYSCTL_DESCR("Fujitsu hotkeys controls"),
296 NULL, 0, NULL, 0,
297 CTL_CREATE, CTL_EOL)) != 0)
298 goto fail;
299
300 (void)sysctl_createv(&sc->sc_log, 0, &rnode, NULL,
301 CTLFLAG_READWRITE, CTLTYPE_BOOL, "backlight",
302 SYSCTL_DESCR("Internal DFP backlight switch state"),
303 fujitsu_hk_sysctl_backlight, 0, (void *)sc, 0,
304 CTL_CREATE, CTL_EOL);
305 }
306
307 return;
308
309 fail:
310 aprint_error_dev(sc->sc_dev, "couldn't add sysctl nodes\n");
311 }
312
313 static int
314 fujitsu_hk_sysctl_backlight(SYSCTLFN_ARGS)
315 {
316 struct sysctlnode node;
317 struct fujitsu_hk_softc *sc;
318 bool val;
319 int error;
320
321 node = *rnode;
322 sc = node.sysctl_data;
323
324 mutex_enter(&sc->sc_mtx);
325 error = fujitsu_hk_get_backlight(sc, &val);
326 mutex_exit(&sc->sc_mtx);
327
328 if (error)
329 return error;
330
331 node.sysctl_data = &val;
332 error = sysctl_lookup(SYSCTLFN_CALL(&node));
333 if (error || newp == NULL)
334 return error;
335
336 mutex_enter(&sc->sc_mtx);
337 error = fujitsu_hk_set_backlight(sc, val);
338 mutex_exit(&sc->sc_mtx);
339
340 return error;
341 }
342
343 static int
344 fujitsu_hk_get_irb(struct fujitsu_hk_softc *sc, uint32_t *valuep)
345 {
346 ACPI_HANDLE hdl = sc->sc_node->ad_handle;
347 ACPI_INTEGER val;
348 ACPI_STATUS rv;
349
350 if (!(sc->sc_caps & FUJITSU_HK_CAP_GIRB))
351 return ENODEV;
352
353 rv = acpi_eval_integer(hdl, "GIRB", &val);
354 if (ACPI_FAILURE(rv)) {
355 aprint_error_dev(sc->sc_dev, "failed to evaluate %s.%s: %s\n",
356 acpi_name(hdl), "GIRB", AcpiFormatException(rv));
357 return EIO;
358 }
359
360 *valuep = (uint32_t)val;
361
362 return 0;
363 }
364
365 static int
366 fujitsu_hk_get_backlight(struct fujitsu_hk_softc *sc, bool *valuep)
367 {
368 ACPI_HANDLE hdl = sc->sc_node->ad_handle;
369 ACPI_INTEGER args[] = {
370 FUJITSU_FUNC_TARGET_BACKLIGHT,
371 FUJITSU_FUNC_COMMAND_GET,
372 0x4,
373 0x0
374 };
375 ACPI_INTEGER val;
376 ACPI_STATUS rv;
377
378 if (!(sc->sc_caps & FUJITSU_HK_CAP_FUNC))
379 return ENODEV;
380
381 rv = fujitsu_hk_eval_nary_integer(hdl, "FUNC", args, 4, &val);
382 if (ACPI_FAILURE(rv)) {
383 aprint_error_dev(sc->sc_dev, "failed to evaluate %s.%s: %s\n",
384 acpi_name(hdl), "FUNC", AcpiFormatException(rv));
385 return EIO;
386 }
387
388 if (val == FUJITSU_FUNC_INVALID_ARGS)
389 return ENODEV;
390
391 if (val == FUJITSU_FUNC_BACKLIGHT_ON)
392 *valuep = true;
393 else if (val == FUJITSU_FUNC_BACKLIGHT_OFF)
394 *valuep = false;
395 else
396 return ERANGE;
397
398 return 0;
399 }
400
401 static int
402 fujitsu_hk_set_backlight(struct fujitsu_hk_softc *sc, bool value)
403 {
404 ACPI_HANDLE hdl = sc->sc_node->ad_handle;
405 ACPI_INTEGER args[] = {
406 FUJITSU_FUNC_TARGET_BACKLIGHT,
407 FUJITSU_FUNC_COMMAND_SET,
408 0x4,
409 0x0
410 };
411 ACPI_INTEGER val;
412 ACPI_STATUS rv;
413
414 if (!(sc->sc_caps & FUJITSU_HK_CAP_FUNC))
415 return ENODEV;
416
417 if (value)
418 args[3] = FUJITSU_FUNC_BACKLIGHT_ON;
419 else
420 args[3] = FUJITSU_FUNC_BACKLIGHT_OFF;
421
422 rv = fujitsu_hk_eval_nary_integer(hdl, "FUNC", args, 4, &val);
423 if (ACPI_FAILURE(rv)) {
424 aprint_error_dev(sc->sc_dev, "failed to evaluate %s.%s: %s\n",
425 acpi_name(hdl), "FUNC", AcpiFormatException(rv));
426 return EIO;
427 }
428
429 if (val == FUJITSU_FUNC_INVALID_ARGS)
430 return ENODEV;
431
432 return 0;
433 }
434
435 /*
436 * fujitsu_hk_cap:
437 *
438 * Returns true if and only if (a) the object handle.path exists and
439 * (b) this object is a method or has the given type.
440 */
441 static bool
442 fujitsu_hk_cap(ACPI_HANDLE handle, const char *path, ACPI_OBJECT_TYPE type)
443 {
444 ACPI_HANDLE hdl;
445 ACPI_OBJECT_TYPE typ;
446
447 KASSERT(handle != NULL);
448
449 if (ACPI_FAILURE(AcpiGetHandle(handle, path, &hdl)))
450 return false;
451
452 if (ACPI_FAILURE(AcpiGetType(hdl, &typ)))
453 return false;
454
455 if (typ != ACPI_TYPE_METHOD && typ != type)
456 return false;
457
458 return true;
459 }
460
461 /*
462 * fujitsu_hk_eval_nary_integer:
463 *
464 * Evaluate an object that takes as input an arbitrary (possible null)
465 * number of integer parameters. If res is not NULL, then *res is filled
466 * with the result of the evaluation, and AE_NULL_OBJECT is returned if
467 * the evaluation produced no result.
468 */
469 static ACPI_STATUS
470 fujitsu_hk_eval_nary_integer(ACPI_HANDLE handle, const char *path, const
471 ACPI_INTEGER *args, uint8_t count, ACPI_INTEGER *res)
472 {
473 ACPI_OBJECT_LIST paramlist;
474 ACPI_OBJECT retobj, objpool[4], *argobjs;
475 ACPI_BUFFER buf;
476 ACPI_STATUS rv;
477 uint8_t i;
478
479 /* Require that (args == NULL) if and only if (count == 0). */
480 KASSERT((args != NULL || count == 0) && (args == NULL || count != 0));
481
482 /* The object pool should be large enough for our callers. */
483 KASSERT(count <= __arraycount(objpool));
484
485 if (handle == NULL)
486 handle = ACPI_ROOT_OBJECT;
487
488 /* Convert the given array args into an array of ACPI objects. */
489 argobjs = objpool;
490 for (i = 0; i < count; i++) {
491 argobjs[i].Type = ACPI_TYPE_INTEGER;
492 argobjs[i].Integer.Value = args[i];
493 }
494
495 paramlist.Count = count;
496 paramlist.Pointer = argobjs;
497
498 (void)memset(&retobj, 0, sizeof(retobj));
499 buf.Pointer = &retobj;
500 buf.Length = sizeof(retobj);
501
502 rv = AcpiEvaluateObject(handle, path, ¶mlist, &buf);
503
504 if (ACPI_FAILURE(rv))
505 return rv;
506
507 /*
508 * If a return value is expected and desired (i.e. res != NULL),
509 * then copy the result into *res.
510 */
511 if (res != NULL) {
512 if (buf.Length == 0)
513 return AE_NULL_OBJECT;
514
515 if (retobj.Type != ACPI_TYPE_INTEGER)
516 return AE_TYPE;
517
518 *res = retobj.Integer.Value;
519 }
520
521 return AE_OK;
522 }
523
524 MODULE(MODULE_CLASS_DRIVER, fujhk, NULL);
525
526 #ifdef _MODULE
527 #include "ioconf.c"
528 #endif
529
530 static int
531 fujhk_modcmd(modcmd_t cmd, void *aux)
532 {
533 int rv = 0;
534
535 switch (cmd) {
536
537 case MODULE_CMD_INIT:
538
539 #ifdef _MODULE
540 rv = config_init_component(cfdriver_ioconf_fujhk,
541 cfattach_ioconf_fujhk, cfdata_ioconf_fujhk);
542 #endif
543 break;
544
545 case MODULE_CMD_FINI:
546
547 #ifdef _MODULE
548 rv = config_fini_component(cfdriver_ioconf_fujhk,
549 cfattach_ioconf_fujhk, cfdata_ioconf_fujhk);
550 #endif
551 break;
552
553 default:
554 rv = ENOTTY;
555 }
556
557 return rv;
558 }
559