2 * Copyright (c) 2003 OGAWA Takaya <t-ogawa@triaez.kaisei.org>
3 * Copyright (c) 2004 TAKAHASHI Yoshihiro <nyan@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
38 #include <sys/power.h>
40 #include <contrib/dev/acpica/include/acpi.h>
42 #include <dev/acpica/acpivar.h>
44 #define _COMPONENT ACPI_OEM
45 ACPI_MODULE_NAME("Panasonic")
48 #undef ACPI_PANASONIC_DEBUG
55 #define HKEY_REG_LCD_BRIGHTNESS_MAX_AC 0x02
56 #define HKEY_REG_LCD_BRIGHTNESS_MIN_AC 0x03
57 #define HKEY_REG_LCD_BRIGHTNESS_AC 0x04
58 #define HKEY_REG_LCD_BRIGHTNESS_MAX_DC 0x05
59 #define HKEY_REG_LCD_BRIGHTNESS_MIN_DC 0x06
60 #define HKEY_REG_LCD_BRIGHTNESS_DC 0x07
61 #define HKEY_REG_SOUND_MUTE 0x08
63 /* Field definitions */
64 #define HKEY_LCD_BRIGHTNESS_BITS 4
65 #define HKEY_LCD_BRIGHTNESS_DIV ((1 << HKEY_LCD_BRIGHTNESS_BITS) - 1)
67 struct acpi_panasonic_softc {
71 struct sysctl_ctx_list sysctl_ctx;
72 struct sysctl_oid *sysctl_tree;
74 eventhandler_tag power_evh;
77 /* Prototype for HKEY functions for getting/setting a value. */
78 typedef int hkey_fn_t(ACPI_HANDLE, int, UINT32 *);
80 static int acpi_panasonic_probe(device_t dev);
81 static int acpi_panasonic_attach(device_t dev);
82 static int acpi_panasonic_detach(device_t dev);
83 static int acpi_panasonic_shutdown(device_t dev);
84 static int acpi_panasonic_sysctl(SYSCTL_HANDLER_ARGS);
85 static UINT64 acpi_panasonic_sinf(ACPI_HANDLE h, UINT64 index);
86 static void acpi_panasonic_sset(ACPI_HANDLE h, UINT64 index,
88 static int acpi_panasonic_hkey_event(struct acpi_panasonic_softc *sc,
89 ACPI_HANDLE h, UINT32 *arg);
90 static void acpi_panasonic_hkey_action(struct acpi_panasonic_softc *sc,
91 ACPI_HANDLE h, UINT32 key);
92 static void acpi_panasonic_notify(ACPI_HANDLE h, UINT32 notify,
94 static void acpi_panasonic_power_profile(void *arg);
96 static hkey_fn_t hkey_lcd_brightness_max;
97 static hkey_fn_t hkey_lcd_brightness_min;
98 static hkey_fn_t hkey_lcd_brightness;
99 static hkey_fn_t hkey_sound_mute;
100 ACPI_SERIAL_DECL(panasonic, "ACPI Panasonic extras");
102 /* Table of sysctl names and HKEY functions to call. */
108 {"lcd_brightness_max", hkey_lcd_brightness_max},
109 {"lcd_brightness_min", hkey_lcd_brightness_min},
110 {"lcd_brightness", hkey_lcd_brightness},
111 {"sound_mute", hkey_sound_mute},
115 static device_method_t acpi_panasonic_methods[] = {
116 DEVMETHOD(device_probe, acpi_panasonic_probe),
117 DEVMETHOD(device_attach, acpi_panasonic_attach),
118 DEVMETHOD(device_detach, acpi_panasonic_detach),
119 DEVMETHOD(device_shutdown, acpi_panasonic_shutdown),
124 static driver_t acpi_panasonic_driver = {
126 acpi_panasonic_methods,
127 sizeof(struct acpi_panasonic_softc),
130 static devclass_t acpi_panasonic_devclass;
132 DRIVER_MODULE(acpi_panasonic, acpi, acpi_panasonic_driver,
133 acpi_panasonic_devclass, 0, 0);
134 MODULE_DEPEND(acpi_panasonic, acpi, 1, 1, 1);
137 acpi_panasonic_probe(device_t dev)
139 static char *mat_ids[] = { "MAT0019", NULL };
142 if (acpi_disabled("panasonic") ||
143 device_get_unit(dev) != 0)
145 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, mat_ids, NULL);
148 device_set_desc(dev, "Panasonic Notebook Hotkeys");
153 acpi_panasonic_attach(device_t dev)
155 struct acpi_panasonic_softc *sc;
156 struct acpi_softc *acpi_sc;
160 sc = device_get_softc(dev);
162 sc->handle = acpi_get_handle(dev);
164 acpi_sc = acpi_device_get_parent_softc(dev);
166 /* Build sysctl tree */
167 sysctl_ctx_init(&sc->sysctl_ctx);
168 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
169 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO,
170 "panasonic", CTLFLAG_RD, 0, "");
171 for (i = 0; sysctl_table[i].name != NULL; i++) {
172 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
173 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
174 sysctl_table[i].name,
175 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
176 sc, i, acpi_panasonic_sysctl, "I", "");
180 /* Activate hotkeys */
181 status = AcpiEvaluateObject(sc->handle, "", NULL, NULL);
182 if (ACPI_FAILURE(status)) {
183 device_printf(dev, "enable FN keys failed\n");
184 sysctl_ctx_free(&sc->sysctl_ctx);
189 /* Handle notifies */
190 status = AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
191 acpi_panasonic_notify, sc);
192 if (ACPI_FAILURE(status)) {
193 device_printf(dev, "couldn't install notify handler - %s\n",
194 AcpiFormatException(status));
195 sysctl_ctx_free(&sc->sysctl_ctx);
199 /* Install power profile event handler */
200 sc->power_evh = EVENTHANDLER_REGISTER(power_profile_change,
201 acpi_panasonic_power_profile, sc->handle, 0);
207 acpi_panasonic_detach(device_t dev)
209 struct acpi_panasonic_softc *sc;
211 sc = device_get_softc(dev);
213 /* Remove power profile event handler */
214 EVENTHANDLER_DEREGISTER(power_profile_change, sc->power_evh);
216 /* Remove notify handler */
217 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
218 acpi_panasonic_notify);
220 /* Free sysctl tree */
221 sysctl_ctx_free(&sc->sysctl_ctx);
227 acpi_panasonic_shutdown(device_t dev)
229 struct acpi_panasonic_softc *sc;
232 /* Mute the main audio during reboot to prevent static burst to speaker. */
233 sc = device_get_softc(dev);
235 hkey_sound_mute(sc->handle, HKEY_SET, &mute);
240 acpi_panasonic_sysctl(SYSCTL_HANDLER_ARGS)
242 struct acpi_panasonic_softc *sc;
247 sc = (struct acpi_panasonic_softc *)oidp->oid_arg1;
248 function = oidp->oid_arg2;
249 handler = sysctl_table[function].handler;
251 /* Get the current value from the appropriate function. */
252 ACPI_SERIAL_BEGIN(panasonic);
253 error = handler(sc->handle, HKEY_GET, &arg);
257 /* Send the current value to the user and return if no new value. */
258 error = sysctl_handle_int(oidp, &arg, 0, req);
259 if (error != 0 || req->newptr == NULL)
262 /* Set the new value via the appropriate function. */
263 error = handler(sc->handle, HKEY_SET, &arg);
266 ACPI_SERIAL_END(panasonic);
271 acpi_panasonic_sinf(ACPI_HANDLE h, UINT64 index)
277 ACPI_SERIAL_ASSERT(panasonic);
279 buf.Length = ACPI_ALLOCATE_BUFFER;
281 AcpiEvaluateObject(h, "SINF", NULL, &buf);
282 res = (ACPI_OBJECT *)buf.Pointer;
283 if (res->Type == ACPI_TYPE_PACKAGE)
284 ret = res->Package.Elements[index].Integer.Value;
285 AcpiOsFree(buf.Pointer);
291 acpi_panasonic_sset(ACPI_HANDLE h, UINT64 index, UINT64 val)
293 ACPI_OBJECT_LIST args;
296 ACPI_SERIAL_ASSERT(panasonic);
297 obj[0].Type = ACPI_TYPE_INTEGER;
298 obj[0].Integer.Value = index;
299 obj[1].Type = ACPI_TYPE_INTEGER;
300 obj[1].Integer.Value = val;
303 AcpiEvaluateObject(h, "SSET", &args, NULL);
307 hkey_lcd_brightness_max(ACPI_HANDLE h, int op, UINT32 *val)
311 ACPI_SERIAL_ASSERT(panasonic);
312 reg = (power_profile_get_state() == POWER_PROFILE_PERFORMANCE) ?
313 HKEY_REG_LCD_BRIGHTNESS_MAX_AC : HKEY_REG_LCD_BRIGHTNESS_MAX_DC;
320 *val = acpi_panasonic_sinf(h, reg);
328 hkey_lcd_brightness_min(ACPI_HANDLE h, int op, UINT32 *val)
332 ACPI_SERIAL_ASSERT(panasonic);
333 reg = (power_profile_get_state() == POWER_PROFILE_PERFORMANCE) ?
334 HKEY_REG_LCD_BRIGHTNESS_MIN_AC : HKEY_REG_LCD_BRIGHTNESS_MIN_DC;
341 *val = acpi_panasonic_sinf(h, reg);
349 hkey_lcd_brightness(ACPI_HANDLE h, int op, UINT32 *val)
354 reg = (power_profile_get_state() == POWER_PROFILE_PERFORMANCE) ?
355 HKEY_REG_LCD_BRIGHTNESS_AC : HKEY_REG_LCD_BRIGHTNESS_DC;
357 ACPI_SERIAL_ASSERT(panasonic);
360 hkey_lcd_brightness_max(h, HKEY_GET, &max);
361 hkey_lcd_brightness_min(h, HKEY_GET, &min);
362 if (*val < min || *val > max)
364 acpi_panasonic_sset(h, reg, *val);
367 *val = acpi_panasonic_sinf(h, reg);
375 hkey_sound_mute(ACPI_HANDLE h, int op, UINT32 *val)
378 ACPI_SERIAL_ASSERT(panasonic);
381 if (*val != 0 && *val != 1)
383 acpi_panasonic_sset(h, HKEY_REG_SOUND_MUTE, *val);
386 *val = acpi_panasonic_sinf(h, HKEY_REG_SOUND_MUTE);
394 acpi_panasonic_hkey_event(struct acpi_panasonic_softc *sc, ACPI_HANDLE h,
402 ACPI_SERIAL_ASSERT(panasonic);
405 buf.Length = ACPI_ALLOCATE_BUFFER;
407 AcpiEvaluateObject(h, "HINF", NULL, &buf);
408 res = (ACPI_OBJECT *)buf.Pointer;
409 if (res->Type != ACPI_TYPE_INTEGER) {
410 device_printf(sc->dev, "HINF returned non-integer\n");
413 val = res->Integer.Value;
414 #ifdef ACPI_PANASONIC_DEBUG
415 device_printf(sc->dev, "%s button Fn+F%d\n",
416 (val & 0x80) ? "Pressed" : "Released",
419 if ((val & 0x7f) > 0 && (val & 0x7f) < 11) {
425 AcpiOsFree(buf.Pointer);
431 acpi_panasonic_hkey_action(struct acpi_panasonic_softc *sc, ACPI_HANDLE h,
434 struct acpi_softc *acpi_sc;
437 acpi_sc = acpi_device_get_parent_softc(sc->dev);
439 ACPI_SERIAL_ASSERT(panasonic);
442 /* Decrease LCD brightness. */
443 hkey_lcd_brightness_max(h, HKEY_GET, &max);
444 hkey_lcd_brightness_min(h, HKEY_GET, &min);
445 hkey_lcd_brightness(h, HKEY_GET, &arg);
446 arg -= max / HKEY_LCD_BRIGHTNESS_DIV;
451 hkey_lcd_brightness(h, HKEY_SET, &arg);
454 /* Increase LCD brightness. */
455 hkey_lcd_brightness_max(h, HKEY_GET, &max);
456 hkey_lcd_brightness_min(h, HKEY_GET, &min);
457 hkey_lcd_brightness(h, HKEY_GET, &arg);
458 arg += max / HKEY_LCD_BRIGHTNESS_DIV;
463 hkey_lcd_brightness(h, HKEY_SET, &arg);
466 /* Toggle sound mute. */
467 hkey_sound_mute(h, HKEY_GET, &arg);
472 hkey_sound_mute(h, HKEY_SET, &arg);
476 acpi_event_sleep_button_sleep(acpi_sc);
482 acpi_panasonic_notify(ACPI_HANDLE h, UINT32 notify, void *context)
484 struct acpi_panasonic_softc *sc;
487 sc = (struct acpi_panasonic_softc *)context;
491 ACPI_SERIAL_BEGIN(panasonic);
492 if (acpi_panasonic_hkey_event(sc, h, &key) == 0) {
493 acpi_panasonic_hkey_action(sc, h, key);
494 acpi_UserNotify("Panasonic", h, (uint8_t)key);
496 ACPI_SERIAL_END(panasonic);
503 device_printf(sc->dev, "unknown notify: %#x\n", notify);
509 acpi_panasonic_power_profile(void *arg)
514 handle = (ACPI_HANDLE)arg;
516 /* Reset current brightness according to new power state. */
517 ACPI_SERIAL_BEGIN(panasonic);
518 hkey_lcd_brightness(handle, HKEY_GET, &brightness);
519 hkey_lcd_brightness(handle, HKEY_SET, &brightness);
520 ACPI_SERIAL_END(panasonic);