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/acpi.h>
41 #include <dev/acpica/acpivar.h>
43 #define _COMPONENT ACPI_OEM
44 ACPI_MODULE_NAME("Panasonic")
47 #undef ACPI_PANASONIC_DEBUG
54 #define HKEY_REG_LCD_BRIGHTNESS_MAX_AC 0x02
55 #define HKEY_REG_LCD_BRIGHTNESS_MIN_AC 0x03
56 #define HKEY_REG_LCD_BRIGHTNESS_AC 0x04
57 #define HKEY_REG_LCD_BRIGHTNESS_MAX_DC 0x05
58 #define HKEY_REG_LCD_BRIGHTNESS_MIN_DC 0x06
59 #define HKEY_REG_LCD_BRIGHTNESS_DC 0x07
60 #define HKEY_REG_SOUND_MUTE 0x08
62 /* Field definitions */
63 #define HKEY_LCD_BRIGHTNESS_BITS 4
64 #define HKEY_LCD_BRIGHTNESS_DIV ((1 << HKEY_LCD_BRIGHTNESS_BITS) - 1)
66 struct acpi_panasonic_softc {
70 struct sysctl_ctx_list sysctl_ctx;
71 struct sysctl_oid *sysctl_tree;
73 eventhandler_tag power_evh;
76 /* Prototype for HKEY functions for getting/setting a value. */
77 typedef int hkey_fn_t(ACPI_HANDLE, int, UINT32 *);
79 static int acpi_panasonic_probe(device_t dev);
80 static int acpi_panasonic_attach(device_t dev);
81 static int acpi_panasonic_detach(device_t dev);
82 static int acpi_panasonic_sysctl(SYSCTL_HANDLER_ARGS);
83 static ACPI_INTEGER acpi_panasonic_sinf(ACPI_HANDLE h, ACPI_INTEGER index);
84 static void acpi_panasonic_sset(ACPI_HANDLE h, ACPI_INTEGER index,
86 static int acpi_panasonic_hkey_event(struct acpi_panasonic_softc *sc,
87 ACPI_HANDLE h, UINT32 *arg);
88 static void acpi_panasonic_hkey_action(struct acpi_panasonic_softc *sc,
89 ACPI_HANDLE h, UINT32 key);
90 static void acpi_panasonic_notify(ACPI_HANDLE h, UINT32 notify,
92 static void acpi_panasonic_power_profile(void *arg);
94 static hkey_fn_t hkey_lcd_brightness_max;
95 static hkey_fn_t hkey_lcd_brightness_min;
96 static hkey_fn_t hkey_lcd_brightness;
97 static hkey_fn_t hkey_sound_mute;
98 ACPI_SERIAL_DECL(panasonic, "ACPI Panasonic extras");
100 /* Table of sysctl names and HKEY functions to call. */
106 {"lcd_brightness_max", hkey_lcd_brightness_max},
107 {"lcd_brightness_min", hkey_lcd_brightness_min},
108 {"lcd_brightness", hkey_lcd_brightness},
109 {"sound_mute", hkey_sound_mute},
113 static device_method_t acpi_panasonic_methods[] = {
114 DEVMETHOD(device_probe, acpi_panasonic_probe),
115 DEVMETHOD(device_attach, acpi_panasonic_attach),
116 DEVMETHOD(device_detach, acpi_panasonic_detach),
121 static driver_t acpi_panasonic_driver = {
123 acpi_panasonic_methods,
124 sizeof(struct acpi_panasonic_softc),
127 static devclass_t acpi_panasonic_devclass;
129 DRIVER_MODULE(acpi_panasonic, acpi, acpi_panasonic_driver,
130 acpi_panasonic_devclass, 0, 0);
131 MODULE_DEPEND(acpi_panasonic, acpi, 1, 1, 1);
134 acpi_panasonic_probe(device_t dev)
136 static char *mat_ids[] = { "MAT0019", NULL };
138 if (acpi_disabled("panasonic") ||
139 ACPI_ID_PROBE(device_get_parent(dev), dev, mat_ids) == NULL ||
140 device_get_unit(dev) != 0)
143 device_set_desc(dev, "Panasonic Notebook Hotkeys");
148 acpi_panasonic_attach(device_t dev)
150 struct acpi_panasonic_softc *sc;
151 struct acpi_softc *acpi_sc;
155 sc = device_get_softc(dev);
157 sc->handle = acpi_get_handle(dev);
159 acpi_sc = acpi_device_get_parent_softc(dev);
161 /* Build sysctl tree */
162 sysctl_ctx_init(&sc->sysctl_ctx);
163 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
164 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO,
165 "panasonic", CTLFLAG_RD, 0, "");
166 for (i = 0; sysctl_table[i].name != NULL; i++) {
167 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
168 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
169 sysctl_table[i].name,
170 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
171 sc, i, acpi_panasonic_sysctl, "I", "");
175 /* Activate hotkeys */
176 status = AcpiEvaluateObject(sc->handle, "", NULL, NULL);
177 if (ACPI_FAILURE(status)) {
178 device_printf(dev, "enable FN keys failed\n");
179 sysctl_ctx_free(&sc->sysctl_ctx);
184 /* Handle notifies */
185 status = AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
186 acpi_panasonic_notify, sc);
187 if (ACPI_FAILURE(status)) {
188 device_printf(dev, "couldn't install notify handler - %s\n",
189 AcpiFormatException(status));
190 sysctl_ctx_free(&sc->sysctl_ctx);
194 /* Install power profile event handler */
195 sc->power_evh = EVENTHANDLER_REGISTER(power_profile_change,
196 acpi_panasonic_power_profile, sc->handle, 0);
202 acpi_panasonic_detach(device_t dev)
204 struct acpi_panasonic_softc *sc;
206 sc = device_get_softc(dev);
208 /* Remove power profile event handler */
209 EVENTHANDLER_DEREGISTER(power_profile_change, sc->power_evh);
211 /* Remove notify handler */
212 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
213 acpi_panasonic_notify);
215 /* Free sysctl tree */
216 sysctl_ctx_free(&sc->sysctl_ctx);
222 acpi_panasonic_sysctl(SYSCTL_HANDLER_ARGS)
224 struct acpi_panasonic_softc *sc;
229 sc = (struct acpi_panasonic_softc *)oidp->oid_arg1;
230 function = oidp->oid_arg2;
231 handler = sysctl_table[function].handler;
233 /* Get the current value from the appropriate function. */
234 ACPI_SERIAL_BEGIN(panasonic);
235 error = handler(sc->handle, HKEY_GET, &arg);
239 /* Send the current value to the user and return if no new value. */
240 error = sysctl_handle_int(oidp, &arg, 0, req);
241 if (error != 0 || req->newptr == NULL)
244 /* Set the new value via the appropriate function. */
245 error = handler(sc->handle, HKEY_SET, &arg);
248 ACPI_SERIAL_END(panasonic);
253 acpi_panasonic_sinf(ACPI_HANDLE h, ACPI_INTEGER index)
259 ACPI_SERIAL_ASSERT(panasonic);
261 buf.Length = ACPI_ALLOCATE_BUFFER;
263 AcpiEvaluateObject(h, "SINF", NULL, &buf);
264 res = (ACPI_OBJECT *)buf.Pointer;
265 if (res->Type == ACPI_TYPE_PACKAGE)
266 ret = res->Package.Elements[index].Integer.Value;
267 AcpiOsFree(buf.Pointer);
273 acpi_panasonic_sset(ACPI_HANDLE h, ACPI_INTEGER index, ACPI_INTEGER val)
275 ACPI_OBJECT_LIST args;
278 ACPI_SERIAL_ASSERT(panasonic);
279 obj[0].Type = ACPI_TYPE_INTEGER;
280 obj[0].Integer.Value = index;
281 obj[1].Type = ACPI_TYPE_INTEGER;
282 obj[1].Integer.Value = val;
285 AcpiEvaluateObject(h, "SSET", &args, NULL);
289 hkey_lcd_brightness_max(ACPI_HANDLE h, int op, UINT32 *val)
293 ACPI_SERIAL_ASSERT(panasonic);
294 reg = (power_profile_get_state() == POWER_PROFILE_PERFORMANCE) ?
295 HKEY_REG_LCD_BRIGHTNESS_MAX_AC : HKEY_REG_LCD_BRIGHTNESS_MAX_DC;
302 *val = acpi_panasonic_sinf(h, reg);
310 hkey_lcd_brightness_min(ACPI_HANDLE h, int op, UINT32 *val)
314 ACPI_SERIAL_ASSERT(panasonic);
315 reg = (power_profile_get_state() == POWER_PROFILE_PERFORMANCE) ?
316 HKEY_REG_LCD_BRIGHTNESS_MIN_AC : HKEY_REG_LCD_BRIGHTNESS_MIN_DC;
323 *val = acpi_panasonic_sinf(h, reg);
331 hkey_lcd_brightness(ACPI_HANDLE h, int op, UINT32 *val)
336 reg = (power_profile_get_state() == POWER_PROFILE_PERFORMANCE) ?
337 HKEY_REG_LCD_BRIGHTNESS_AC : HKEY_REG_LCD_BRIGHTNESS_DC;
339 ACPI_SERIAL_ASSERT(panasonic);
342 hkey_lcd_brightness_max(h, HKEY_GET, &max);
343 hkey_lcd_brightness_min(h, HKEY_GET, &min);
344 if (*val < min || *val > max)
346 acpi_panasonic_sset(h, reg, *val);
349 *val = acpi_panasonic_sinf(h, reg);
357 hkey_sound_mute(ACPI_HANDLE h, int op, UINT32 *val)
360 ACPI_SERIAL_ASSERT(panasonic);
363 if (*val != 0 && *val != 1)
365 acpi_panasonic_sset(h, HKEY_REG_SOUND_MUTE, *val);
368 *val = acpi_panasonic_sinf(h, HKEY_REG_SOUND_MUTE);
376 acpi_panasonic_hkey_event(struct acpi_panasonic_softc *sc, ACPI_HANDLE h,
384 ACPI_SERIAL_ASSERT(panasonic);
387 buf.Length = ACPI_ALLOCATE_BUFFER;
389 AcpiEvaluateObject(h, "HINF", NULL, &buf);
390 res = (ACPI_OBJECT *)buf.Pointer;
391 if (res->Type != ACPI_TYPE_INTEGER) {
392 device_printf(sc->dev, "HINF returned non-integer\n");
395 val = res->Integer.Value;
396 #ifdef ACPI_PANASONIC_DEBUG
397 device_printf(sc->dev, "%s button Fn+F%d\n",
398 (val & 0x80) ? "Pressed" : "Released",
401 if ((val & 0x7f) > 0 && (val & 0x7f) < 11) {
407 AcpiOsFree(buf.Pointer);
413 acpi_panasonic_hkey_action(struct acpi_panasonic_softc *sc, ACPI_HANDLE h,
418 ACPI_SERIAL_ASSERT(panasonic);
421 /* Decrease LCD brightness. */
422 hkey_lcd_brightness_max(h, HKEY_GET, &max);
423 hkey_lcd_brightness_min(h, HKEY_GET, &min);
424 hkey_lcd_brightness(h, HKEY_GET, &arg);
425 arg -= max / HKEY_LCD_BRIGHTNESS_DIV;
430 hkey_lcd_brightness(h, HKEY_SET, &arg);
433 /* Increase LCD brightness. */
434 hkey_lcd_brightness_max(h, HKEY_GET, &max);
435 hkey_lcd_brightness_min(h, HKEY_GET, &min);
436 hkey_lcd_brightness(h, HKEY_GET, &arg);
437 arg += max / HKEY_LCD_BRIGHTNESS_DIV;
442 hkey_lcd_brightness(h, HKEY_SET, &arg);
445 /* Toggle sound mute. */
446 hkey_sound_mute(h, HKEY_GET, &arg);
451 hkey_sound_mute(h, HKEY_SET, &arg);
457 acpi_panasonic_notify(ACPI_HANDLE h, UINT32 notify, void *context)
459 struct acpi_panasonic_softc *sc;
462 sc = (struct acpi_panasonic_softc *)context;
466 ACPI_SERIAL_BEGIN(panasonic);
467 if (acpi_panasonic_hkey_event(sc, h, &key) == 0) {
468 acpi_panasonic_hkey_action(sc, h, key);
469 acpi_UserNotify("Panasonic", h, (uint8_t)key);
471 ACPI_SERIAL_END(panasonic);
474 device_printf(sc->dev, "unknown notify: %#x\n", notify);
480 acpi_panasonic_power_profile(void *arg)
485 handle = (ACPI_HANDLE)arg;
487 /* Reset current brightness according to new power state. */
488 ACPI_SERIAL_BEGIN(panasonic);
489 hkey_lcd_brightness(handle, HKEY_GET, &brightness);
490 hkey_lcd_brightness(handle, HKEY_SET, &brightness);
491 ACPI_SERIAL_END(panasonic);