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 };
141 if (acpi_disabled("panasonic") ||
142 ACPI_ID_PROBE(device_get_parent(dev), dev, mat_ids) == NULL ||
143 device_get_unit(dev) != 0)
146 device_set_desc(dev, "Panasonic Notebook Hotkeys");
151 acpi_panasonic_attach(device_t dev)
153 struct acpi_panasonic_softc *sc;
154 struct acpi_softc *acpi_sc;
158 sc = device_get_softc(dev);
160 sc->handle = acpi_get_handle(dev);
162 acpi_sc = acpi_device_get_parent_softc(dev);
164 /* Build sysctl tree */
165 sysctl_ctx_init(&sc->sysctl_ctx);
166 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
167 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO,
168 "panasonic", CTLFLAG_RD, 0, "");
169 for (i = 0; sysctl_table[i].name != NULL; i++) {
170 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
171 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
172 sysctl_table[i].name,
173 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
174 sc, i, acpi_panasonic_sysctl, "I", "");
178 /* Activate hotkeys */
179 status = AcpiEvaluateObject(sc->handle, "", NULL, NULL);
180 if (ACPI_FAILURE(status)) {
181 device_printf(dev, "enable FN keys failed\n");
182 sysctl_ctx_free(&sc->sysctl_ctx);
187 /* Handle notifies */
188 status = AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
189 acpi_panasonic_notify, sc);
190 if (ACPI_FAILURE(status)) {
191 device_printf(dev, "couldn't install notify handler - %s\n",
192 AcpiFormatException(status));
193 sysctl_ctx_free(&sc->sysctl_ctx);
197 /* Install power profile event handler */
198 sc->power_evh = EVENTHANDLER_REGISTER(power_profile_change,
199 acpi_panasonic_power_profile, sc->handle, 0);
205 acpi_panasonic_detach(device_t dev)
207 struct acpi_panasonic_softc *sc;
209 sc = device_get_softc(dev);
211 /* Remove power profile event handler */
212 EVENTHANDLER_DEREGISTER(power_profile_change, sc->power_evh);
214 /* Remove notify handler */
215 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
216 acpi_panasonic_notify);
218 /* Free sysctl tree */
219 sysctl_ctx_free(&sc->sysctl_ctx);
225 acpi_panasonic_shutdown(device_t dev)
227 struct acpi_panasonic_softc *sc;
230 /* Mute the main audio during reboot to prevent static burst to speaker. */
231 sc = device_get_softc(dev);
233 hkey_sound_mute(sc->handle, HKEY_SET, &mute);
238 acpi_panasonic_sysctl(SYSCTL_HANDLER_ARGS)
240 struct acpi_panasonic_softc *sc;
245 sc = (struct acpi_panasonic_softc *)oidp->oid_arg1;
246 function = oidp->oid_arg2;
247 handler = sysctl_table[function].handler;
249 /* Get the current value from the appropriate function. */
250 ACPI_SERIAL_BEGIN(panasonic);
251 error = handler(sc->handle, HKEY_GET, &arg);
255 /* Send the current value to the user and return if no new value. */
256 error = sysctl_handle_int(oidp, &arg, 0, req);
257 if (error != 0 || req->newptr == NULL)
260 /* Set the new value via the appropriate function. */
261 error = handler(sc->handle, HKEY_SET, &arg);
264 ACPI_SERIAL_END(panasonic);
269 acpi_panasonic_sinf(ACPI_HANDLE h, UINT64 index)
275 ACPI_SERIAL_ASSERT(panasonic);
277 buf.Length = ACPI_ALLOCATE_BUFFER;
279 AcpiEvaluateObject(h, "SINF", NULL, &buf);
280 res = (ACPI_OBJECT *)buf.Pointer;
281 if (res->Type == ACPI_TYPE_PACKAGE)
282 ret = res->Package.Elements[index].Integer.Value;
283 AcpiOsFree(buf.Pointer);
289 acpi_panasonic_sset(ACPI_HANDLE h, UINT64 index, UINT64 val)
291 ACPI_OBJECT_LIST args;
294 ACPI_SERIAL_ASSERT(panasonic);
295 obj[0].Type = ACPI_TYPE_INTEGER;
296 obj[0].Integer.Value = index;
297 obj[1].Type = ACPI_TYPE_INTEGER;
298 obj[1].Integer.Value = val;
301 AcpiEvaluateObject(h, "SSET", &args, NULL);
305 hkey_lcd_brightness_max(ACPI_HANDLE h, int op, UINT32 *val)
309 ACPI_SERIAL_ASSERT(panasonic);
310 reg = (power_profile_get_state() == POWER_PROFILE_PERFORMANCE) ?
311 HKEY_REG_LCD_BRIGHTNESS_MAX_AC : HKEY_REG_LCD_BRIGHTNESS_MAX_DC;
318 *val = acpi_panasonic_sinf(h, reg);
326 hkey_lcd_brightness_min(ACPI_HANDLE h, int op, UINT32 *val)
330 ACPI_SERIAL_ASSERT(panasonic);
331 reg = (power_profile_get_state() == POWER_PROFILE_PERFORMANCE) ?
332 HKEY_REG_LCD_BRIGHTNESS_MIN_AC : HKEY_REG_LCD_BRIGHTNESS_MIN_DC;
339 *val = acpi_panasonic_sinf(h, reg);
347 hkey_lcd_brightness(ACPI_HANDLE h, int op, UINT32 *val)
352 reg = (power_profile_get_state() == POWER_PROFILE_PERFORMANCE) ?
353 HKEY_REG_LCD_BRIGHTNESS_AC : HKEY_REG_LCD_BRIGHTNESS_DC;
355 ACPI_SERIAL_ASSERT(panasonic);
358 hkey_lcd_brightness_max(h, HKEY_GET, &max);
359 hkey_lcd_brightness_min(h, HKEY_GET, &min);
360 if (*val < min || *val > max)
362 acpi_panasonic_sset(h, reg, *val);
365 *val = acpi_panasonic_sinf(h, reg);
373 hkey_sound_mute(ACPI_HANDLE h, int op, UINT32 *val)
376 ACPI_SERIAL_ASSERT(panasonic);
379 if (*val != 0 && *val != 1)
381 acpi_panasonic_sset(h, HKEY_REG_SOUND_MUTE, *val);
384 *val = acpi_panasonic_sinf(h, HKEY_REG_SOUND_MUTE);
392 acpi_panasonic_hkey_event(struct acpi_panasonic_softc *sc, ACPI_HANDLE h,
400 ACPI_SERIAL_ASSERT(panasonic);
403 buf.Length = ACPI_ALLOCATE_BUFFER;
405 AcpiEvaluateObject(h, "HINF", NULL, &buf);
406 res = (ACPI_OBJECT *)buf.Pointer;
407 if (res->Type != ACPI_TYPE_INTEGER) {
408 device_printf(sc->dev, "HINF returned non-integer\n");
411 val = res->Integer.Value;
412 #ifdef ACPI_PANASONIC_DEBUG
413 device_printf(sc->dev, "%s button Fn+F%d\n",
414 (val & 0x80) ? "Pressed" : "Released",
417 if ((val & 0x7f) > 0 && (val & 0x7f) < 11) {
423 AcpiOsFree(buf.Pointer);
429 acpi_panasonic_hkey_action(struct acpi_panasonic_softc *sc, ACPI_HANDLE h,
432 struct acpi_softc *acpi_sc;
435 acpi_sc = acpi_device_get_parent_softc(sc->dev);
437 ACPI_SERIAL_ASSERT(panasonic);
440 /* Decrease LCD brightness. */
441 hkey_lcd_brightness_max(h, HKEY_GET, &max);
442 hkey_lcd_brightness_min(h, HKEY_GET, &min);
443 hkey_lcd_brightness(h, HKEY_GET, &arg);
444 arg -= max / HKEY_LCD_BRIGHTNESS_DIV;
449 hkey_lcd_brightness(h, HKEY_SET, &arg);
452 /* Increase LCD brightness. */
453 hkey_lcd_brightness_max(h, HKEY_GET, &max);
454 hkey_lcd_brightness_min(h, HKEY_GET, &min);
455 hkey_lcd_brightness(h, HKEY_GET, &arg);
456 arg += max / HKEY_LCD_BRIGHTNESS_DIV;
461 hkey_lcd_brightness(h, HKEY_SET, &arg);
464 /* Toggle sound mute. */
465 hkey_sound_mute(h, HKEY_GET, &arg);
470 hkey_sound_mute(h, HKEY_SET, &arg);
474 acpi_event_sleep_button_sleep(acpi_sc);
480 acpi_panasonic_notify(ACPI_HANDLE h, UINT32 notify, void *context)
482 struct acpi_panasonic_softc *sc;
485 sc = (struct acpi_panasonic_softc *)context;
489 ACPI_SERIAL_BEGIN(panasonic);
490 if (acpi_panasonic_hkey_event(sc, h, &key) == 0) {
491 acpi_panasonic_hkey_action(sc, h, key);
492 acpi_UserNotify("Panasonic", h, (uint8_t)key);
494 ACPI_SERIAL_END(panasonic);
497 device_printf(sc->dev, "unknown notify: %#x\n", notify);
503 acpi_panasonic_power_profile(void *arg)
508 handle = (ACPI_HANDLE)arg;
510 /* Reset current brightness according to new power state. */
511 ACPI_SERIAL_BEGIN(panasonic);
512 hkey_lcd_brightness(handle, HKEY_GET, &brightness);
513 hkey_lcd_brightness(handle, HKEY_SET, &brightness);
514 ACPI_SERIAL_END(panasonic);