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>
35 #include <sys/eventhandler.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/power.h>
41 #include <contrib/dev/acpica/include/acpi.h>
43 #include <dev/acpica/acpivar.h>
45 #define _COMPONENT ACPI_OEM
46 ACPI_MODULE_NAME("Panasonic")
49 #undef ACPI_PANASONIC_DEBUG
56 #define HKEY_REG_LCD_BRIGHTNESS_MAX_AC 0x02
57 #define HKEY_REG_LCD_BRIGHTNESS_MIN_AC 0x03
58 #define HKEY_REG_LCD_BRIGHTNESS_AC 0x04
59 #define HKEY_REG_LCD_BRIGHTNESS_MAX_DC 0x05
60 #define HKEY_REG_LCD_BRIGHTNESS_MIN_DC 0x06
61 #define HKEY_REG_LCD_BRIGHTNESS_DC 0x07
62 #define HKEY_REG_SOUND_MUTE 0x08
64 /* Field definitions */
65 #define HKEY_LCD_BRIGHTNESS_BITS 4
66 #define HKEY_LCD_BRIGHTNESS_DIV ((1 << HKEY_LCD_BRIGHTNESS_BITS) - 1)
68 struct acpi_panasonic_softc {
72 struct sysctl_ctx_list sysctl_ctx;
73 struct sysctl_oid *sysctl_tree;
75 eventhandler_tag power_evh;
78 /* Prototype for HKEY functions for getting/setting a value. */
79 typedef int hkey_fn_t(ACPI_HANDLE, int, UINT32 *);
81 static int acpi_panasonic_probe(device_t dev);
82 static int acpi_panasonic_attach(device_t dev);
83 static int acpi_panasonic_detach(device_t dev);
84 static int acpi_panasonic_shutdown(device_t dev);
85 static int acpi_panasonic_sysctl(SYSCTL_HANDLER_ARGS);
86 static UINT64 acpi_panasonic_sinf(ACPI_HANDLE h, UINT64 index);
87 static void acpi_panasonic_sset(ACPI_HANDLE h, UINT64 index,
89 static int acpi_panasonic_hkey_event(struct acpi_panasonic_softc *sc,
90 ACPI_HANDLE h, UINT32 *arg);
91 static void acpi_panasonic_hkey_action(struct acpi_panasonic_softc *sc,
92 ACPI_HANDLE h, UINT32 key);
93 static void acpi_panasonic_notify(ACPI_HANDLE h, UINT32 notify,
95 static void acpi_panasonic_power_profile(void *arg);
97 static hkey_fn_t hkey_lcd_brightness_max;
98 static hkey_fn_t hkey_lcd_brightness_min;
99 static hkey_fn_t hkey_lcd_brightness;
100 static hkey_fn_t hkey_sound_mute;
101 ACPI_SERIAL_DECL(panasonic, "ACPI Panasonic extras");
103 /* Table of sysctl names and HKEY functions to call. */
109 {"lcd_brightness_max", hkey_lcd_brightness_max},
110 {"lcd_brightness_min", hkey_lcd_brightness_min},
111 {"lcd_brightness", hkey_lcd_brightness},
112 {"sound_mute", hkey_sound_mute},
116 static device_method_t acpi_panasonic_methods[] = {
117 DEVMETHOD(device_probe, acpi_panasonic_probe),
118 DEVMETHOD(device_attach, acpi_panasonic_attach),
119 DEVMETHOD(device_detach, acpi_panasonic_detach),
120 DEVMETHOD(device_shutdown, acpi_panasonic_shutdown),
125 static driver_t acpi_panasonic_driver = {
127 acpi_panasonic_methods,
128 sizeof(struct acpi_panasonic_softc),
131 static devclass_t acpi_panasonic_devclass;
133 DRIVER_MODULE(acpi_panasonic, acpi, acpi_panasonic_driver,
134 acpi_panasonic_devclass, 0, 0);
135 MODULE_DEPEND(acpi_panasonic, acpi, 1, 1, 1);
138 acpi_panasonic_probe(device_t dev)
140 static char *mat_ids[] = { "MAT0019", NULL };
143 if (acpi_disabled("panasonic") ||
144 device_get_unit(dev) != 0)
146 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, mat_ids, NULL);
149 device_set_desc(dev, "Panasonic Notebook Hotkeys");
154 acpi_panasonic_attach(device_t dev)
156 struct acpi_panasonic_softc *sc;
157 struct acpi_softc *acpi_sc;
161 sc = device_get_softc(dev);
163 sc->handle = acpi_get_handle(dev);
165 acpi_sc = acpi_device_get_parent_softc(dev);
167 /* Build sysctl tree */
168 sysctl_ctx_init(&sc->sysctl_ctx);
169 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
170 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO,
171 "panasonic", CTLFLAG_RD, 0, "");
172 for (i = 0; sysctl_table[i].name != NULL; i++) {
173 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
174 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
175 sysctl_table[i].name,
176 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
177 sc, i, acpi_panasonic_sysctl, "I", "");
181 /* Activate hotkeys */
182 status = AcpiEvaluateObject(sc->handle, "", NULL, NULL);
183 if (ACPI_FAILURE(status)) {
184 device_printf(dev, "enable FN keys failed\n");
185 sysctl_ctx_free(&sc->sysctl_ctx);
190 /* Handle notifies */
191 status = AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
192 acpi_panasonic_notify, sc);
193 if (ACPI_FAILURE(status)) {
194 device_printf(dev, "couldn't install notify handler - %s\n",
195 AcpiFormatException(status));
196 sysctl_ctx_free(&sc->sysctl_ctx);
200 /* Install power profile event handler */
201 sc->power_evh = EVENTHANDLER_REGISTER(power_profile_change,
202 acpi_panasonic_power_profile, sc->handle, 0);
208 acpi_panasonic_detach(device_t dev)
210 struct acpi_panasonic_softc *sc;
212 sc = device_get_softc(dev);
214 /* Remove power profile event handler */
215 EVENTHANDLER_DEREGISTER(power_profile_change, sc->power_evh);
217 /* Remove notify handler */
218 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
219 acpi_panasonic_notify);
221 /* Free sysctl tree */
222 sysctl_ctx_free(&sc->sysctl_ctx);
228 acpi_panasonic_shutdown(device_t dev)
230 struct acpi_panasonic_softc *sc;
233 /* Mute the main audio during reboot to prevent static burst to speaker. */
234 sc = device_get_softc(dev);
236 hkey_sound_mute(sc->handle, HKEY_SET, &mute);
241 acpi_panasonic_sysctl(SYSCTL_HANDLER_ARGS)
243 struct acpi_panasonic_softc *sc;
248 sc = (struct acpi_panasonic_softc *)oidp->oid_arg1;
249 function = oidp->oid_arg2;
250 handler = sysctl_table[function].handler;
252 /* Get the current value from the appropriate function. */
253 ACPI_SERIAL_BEGIN(panasonic);
254 error = handler(sc->handle, HKEY_GET, &arg);
258 /* Send the current value to the user and return if no new value. */
259 error = sysctl_handle_int(oidp, &arg, 0, req);
260 if (error != 0 || req->newptr == NULL)
263 /* Set the new value via the appropriate function. */
264 error = handler(sc->handle, HKEY_SET, &arg);
267 ACPI_SERIAL_END(panasonic);
272 acpi_panasonic_sinf(ACPI_HANDLE h, UINT64 index)
278 ACPI_SERIAL_ASSERT(panasonic);
280 buf.Length = ACPI_ALLOCATE_BUFFER;
282 AcpiEvaluateObject(h, "SINF", NULL, &buf);
283 res = (ACPI_OBJECT *)buf.Pointer;
284 if (res->Type == ACPI_TYPE_PACKAGE)
285 ret = res->Package.Elements[index].Integer.Value;
286 AcpiOsFree(buf.Pointer);
292 acpi_panasonic_sset(ACPI_HANDLE h, UINT64 index, UINT64 val)
294 ACPI_OBJECT_LIST args;
297 ACPI_SERIAL_ASSERT(panasonic);
298 obj[0].Type = ACPI_TYPE_INTEGER;
299 obj[0].Integer.Value = index;
300 obj[1].Type = ACPI_TYPE_INTEGER;
301 obj[1].Integer.Value = val;
304 AcpiEvaluateObject(h, "SSET", &args, NULL);
308 hkey_lcd_brightness_max(ACPI_HANDLE h, int op, UINT32 *val)
312 ACPI_SERIAL_ASSERT(panasonic);
313 reg = (power_profile_get_state() == POWER_PROFILE_PERFORMANCE) ?
314 HKEY_REG_LCD_BRIGHTNESS_MAX_AC : HKEY_REG_LCD_BRIGHTNESS_MAX_DC;
321 *val = acpi_panasonic_sinf(h, reg);
329 hkey_lcd_brightness_min(ACPI_HANDLE h, int op, UINT32 *val)
333 ACPI_SERIAL_ASSERT(panasonic);
334 reg = (power_profile_get_state() == POWER_PROFILE_PERFORMANCE) ?
335 HKEY_REG_LCD_BRIGHTNESS_MIN_AC : HKEY_REG_LCD_BRIGHTNESS_MIN_DC;
342 *val = acpi_panasonic_sinf(h, reg);
350 hkey_lcd_brightness(ACPI_HANDLE h, int op, UINT32 *val)
355 reg = (power_profile_get_state() == POWER_PROFILE_PERFORMANCE) ?
356 HKEY_REG_LCD_BRIGHTNESS_AC : HKEY_REG_LCD_BRIGHTNESS_DC;
358 ACPI_SERIAL_ASSERT(panasonic);
361 hkey_lcd_brightness_max(h, HKEY_GET, &max);
362 hkey_lcd_brightness_min(h, HKEY_GET, &min);
363 if (*val < min || *val > max)
365 acpi_panasonic_sset(h, reg, *val);
368 *val = acpi_panasonic_sinf(h, reg);
376 hkey_sound_mute(ACPI_HANDLE h, int op, UINT32 *val)
379 ACPI_SERIAL_ASSERT(panasonic);
382 if (*val != 0 && *val != 1)
384 acpi_panasonic_sset(h, HKEY_REG_SOUND_MUTE, *val);
387 *val = acpi_panasonic_sinf(h, HKEY_REG_SOUND_MUTE);
395 acpi_panasonic_hkey_event(struct acpi_panasonic_softc *sc, ACPI_HANDLE h,
403 ACPI_SERIAL_ASSERT(panasonic);
406 buf.Length = ACPI_ALLOCATE_BUFFER;
408 AcpiEvaluateObject(h, "HINF", NULL, &buf);
409 res = (ACPI_OBJECT *)buf.Pointer;
410 if (res->Type != ACPI_TYPE_INTEGER) {
411 device_printf(sc->dev, "HINF returned non-integer\n");
414 val = res->Integer.Value;
415 #ifdef ACPI_PANASONIC_DEBUG
416 device_printf(sc->dev, "%s button Fn+F%d\n",
417 (val & 0x80) ? "Pressed" : "Released",
420 if ((val & 0x7f) > 0 && (val & 0x7f) < 11) {
426 AcpiOsFree(buf.Pointer);
432 acpi_panasonic_hkey_action(struct acpi_panasonic_softc *sc, ACPI_HANDLE h,
435 struct acpi_softc *acpi_sc;
438 acpi_sc = acpi_device_get_parent_softc(sc->dev);
440 ACPI_SERIAL_ASSERT(panasonic);
443 /* Decrease LCD brightness. */
444 hkey_lcd_brightness_max(h, HKEY_GET, &max);
445 hkey_lcd_brightness_min(h, HKEY_GET, &min);
446 hkey_lcd_brightness(h, HKEY_GET, &arg);
447 arg -= max / HKEY_LCD_BRIGHTNESS_DIV;
452 hkey_lcd_brightness(h, HKEY_SET, &arg);
455 /* Increase LCD brightness. */
456 hkey_lcd_brightness_max(h, HKEY_GET, &max);
457 hkey_lcd_brightness_min(h, HKEY_GET, &min);
458 hkey_lcd_brightness(h, HKEY_GET, &arg);
459 arg += max / HKEY_LCD_BRIGHTNESS_DIV;
464 hkey_lcd_brightness(h, HKEY_SET, &arg);
467 /* Toggle sound mute. */
468 hkey_sound_mute(h, HKEY_GET, &arg);
473 hkey_sound_mute(h, HKEY_SET, &arg);
477 acpi_event_sleep_button_sleep(acpi_sc);
483 acpi_panasonic_notify(ACPI_HANDLE h, UINT32 notify, void *context)
485 struct acpi_panasonic_softc *sc;
488 sc = (struct acpi_panasonic_softc *)context;
492 ACPI_SERIAL_BEGIN(panasonic);
493 if (acpi_panasonic_hkey_event(sc, h, &key) == 0) {
494 acpi_panasonic_hkey_action(sc, h, key);
495 acpi_UserNotify("Panasonic", h, (uint8_t)key);
497 ACPI_SERIAL_END(panasonic);
504 device_printf(sc->dev, "unknown notify: %#x\n", notify);
510 acpi_panasonic_power_profile(void *arg)
515 handle = (ACPI_HANDLE)arg;
517 /* Reset current brightness according to new power state. */
518 ACPI_SERIAL_BEGIN(panasonic);
519 hkey_lcd_brightness(handle, HKEY_GET, &brightness);
520 hkey_lcd_brightness(handle, HKEY_SET, &brightness);
521 ACPI_SERIAL_END(panasonic);