1 /*- 2 * Copyright (c) 2003 OGAWA Takaya <t-ogawa@triaez.kaisei.org> 3 * Copyright (c) 2004 TAKAHASHI Yoshihiro <nyan@FreeBSD.org> 4 * All rights Reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 14 * 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 25 * SUCH DAMAGE. 26 * 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_acpi.h" 33 #include <sys/param.h> 34 #include <sys/bus.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> 40 41 #include <contrib/dev/acpica/include/acpi.h> 42 43 #include <dev/acpica/acpivar.h> 44 45 #define _COMPONENT ACPI_OEM 46 ACPI_MODULE_NAME("Panasonic") 47 48 /* Debug */ 49 #undef ACPI_PANASONIC_DEBUG 50 51 /* Operations */ 52 #define HKEY_SET 0 53 #define HKEY_GET 1 54 55 /* Functions */ 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 63 64 /* Field definitions */ 65 #define HKEY_LCD_BRIGHTNESS_BITS 4 66 #define HKEY_LCD_BRIGHTNESS_DIV ((1 << HKEY_LCD_BRIGHTNESS_BITS) - 1) 67 68 struct acpi_panasonic_softc { 69 device_t dev; 70 ACPI_HANDLE handle; 71 72 struct sysctl_ctx_list sysctl_ctx; 73 struct sysctl_oid *sysctl_tree; 74 75 eventhandler_tag power_evh; 76 }; 77 78 /* Prototype for HKEY functions for getting/setting a value. */ 79 typedef int hkey_fn_t(ACPI_HANDLE, int, UINT32 *); 80 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, 88 UINT64 val); 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, 94 void *context); 95 static void acpi_panasonic_power_profile(void *arg); 96 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"); 102 103 /* Table of sysctl names and HKEY functions to call. */ 104 static struct { 105 char *name; 106 hkey_fn_t *handler; 107 } sysctl_table[] = { 108 /* name, handler */ 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}, 113 {NULL, NULL} 114 }; 115 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), 121 122 DEVMETHOD_END 123 }; 124 125 static driver_t acpi_panasonic_driver = { 126 "acpi_panasonic", 127 acpi_panasonic_methods, 128 sizeof(struct acpi_panasonic_softc), 129 }; 130 131 DRIVER_MODULE(acpi_panasonic, acpi, acpi_panasonic_driver, 0, 0); 132 MODULE_DEPEND(acpi_panasonic, acpi, 1, 1, 1); 133 134 static int 135 acpi_panasonic_probe(device_t dev) 136 { 137 static char *mat_ids[] = { "MAT0019", NULL }; 138 int rv; 139 140 if (acpi_disabled("panasonic") || 141 device_get_unit(dev) != 0) 142 return (ENXIO); 143 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, mat_ids, NULL); 144 145 if (rv <= 0) 146 device_set_desc(dev, "Panasonic Notebook Hotkeys"); 147 return (rv); 148 } 149 150 static int 151 acpi_panasonic_attach(device_t dev) 152 { 153 struct acpi_panasonic_softc *sc; 154 struct acpi_softc *acpi_sc; 155 ACPI_STATUS status; 156 int i; 157 158 sc = device_get_softc(dev); 159 sc->dev = dev; 160 sc->handle = acpi_get_handle(dev); 161 162 acpi_sc = acpi_device_get_parent_softc(dev); 163 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 | CTLFLAG_MPSAFE, 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 CTLFLAG_MPSAFE, sc, i, acpi_panasonic_sysctl, "I", ""); 175 } 176 177 #if 0 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); 183 return (ENXIO); 184 } 185 #endif 186 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); 194 return (ENXIO); 195 } 196 197 /* Install power profile event handler */ 198 sc->power_evh = EVENTHANDLER_REGISTER(power_profile_change, 199 acpi_panasonic_power_profile, sc->handle, 0); 200 201 return (0); 202 } 203 204 static int 205 acpi_panasonic_detach(device_t dev) 206 { 207 struct acpi_panasonic_softc *sc; 208 209 sc = device_get_softc(dev); 210 211 /* Remove power profile event handler */ 212 EVENTHANDLER_DEREGISTER(power_profile_change, sc->power_evh); 213 214 /* Remove notify handler */ 215 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY, 216 acpi_panasonic_notify); 217 218 /* Free sysctl tree */ 219 sysctl_ctx_free(&sc->sysctl_ctx); 220 221 return (0); 222 } 223 224 static int 225 acpi_panasonic_shutdown(device_t dev) 226 { 227 struct acpi_panasonic_softc *sc; 228 int mute; 229 230 /* Mute the main audio during reboot to prevent static burst to speaker. */ 231 sc = device_get_softc(dev); 232 mute = 1; 233 hkey_sound_mute(sc->handle, HKEY_SET, &mute); 234 return (0); 235 } 236 237 static int 238 acpi_panasonic_sysctl(SYSCTL_HANDLER_ARGS) 239 { 240 struct acpi_panasonic_softc *sc; 241 UINT32 arg; 242 int function, error; 243 hkey_fn_t *handler; 244 245 sc = (struct acpi_panasonic_softc *)oidp->oid_arg1; 246 function = oidp->oid_arg2; 247 handler = sysctl_table[function].handler; 248 249 /* Get the current value from the appropriate function. */ 250 ACPI_SERIAL_BEGIN(panasonic); 251 error = handler(sc->handle, HKEY_GET, &arg); 252 if (error != 0) 253 goto out; 254 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) 258 goto out; 259 260 /* Set the new value via the appropriate function. */ 261 error = handler(sc->handle, HKEY_SET, &arg); 262 263 out: 264 ACPI_SERIAL_END(panasonic); 265 return (error); 266 } 267 268 static UINT64 269 acpi_panasonic_sinf(ACPI_HANDLE h, UINT64 index) 270 { 271 ACPI_BUFFER buf; 272 ACPI_OBJECT *res; 273 UINT64 ret; 274 275 ACPI_SERIAL_ASSERT(panasonic); 276 ret = -1; 277 buf.Length = ACPI_ALLOCATE_BUFFER; 278 buf.Pointer = NULL; 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); 284 285 return (ret); 286 } 287 288 static void 289 acpi_panasonic_sset(ACPI_HANDLE h, UINT64 index, UINT64 val) 290 { 291 ACPI_OBJECT_LIST args; 292 ACPI_OBJECT obj[2]; 293 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; 299 args.Count = 2; 300 args.Pointer = obj; 301 AcpiEvaluateObject(h, "SSET", &args, NULL); 302 } 303 304 static int 305 hkey_lcd_brightness_max(ACPI_HANDLE h, int op, UINT32 *val) 306 { 307 int reg; 308 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; 312 313 switch (op) { 314 case HKEY_SET: 315 return (EPERM); 316 break; 317 case HKEY_GET: 318 *val = acpi_panasonic_sinf(h, reg); 319 break; 320 } 321 322 return (0); 323 } 324 325 static int 326 hkey_lcd_brightness_min(ACPI_HANDLE h, int op, UINT32 *val) 327 { 328 int reg; 329 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; 333 334 switch (op) { 335 case HKEY_SET: 336 return (EPERM); 337 break; 338 case HKEY_GET: 339 *val = acpi_panasonic_sinf(h, reg); 340 break; 341 } 342 343 return (0); 344 } 345 346 static int 347 hkey_lcd_brightness(ACPI_HANDLE h, int op, UINT32 *val) 348 { 349 int reg; 350 UINT32 max, min; 351 352 reg = (power_profile_get_state() == POWER_PROFILE_PERFORMANCE) ? 353 HKEY_REG_LCD_BRIGHTNESS_AC : HKEY_REG_LCD_BRIGHTNESS_DC; 354 355 ACPI_SERIAL_ASSERT(panasonic); 356 switch (op) { 357 case HKEY_SET: 358 hkey_lcd_brightness_max(h, HKEY_GET, &max); 359 hkey_lcd_brightness_min(h, HKEY_GET, &min); 360 if (*val < min || *val > max) 361 return (EINVAL); 362 acpi_panasonic_sset(h, reg, *val); 363 break; 364 case HKEY_GET: 365 *val = acpi_panasonic_sinf(h, reg); 366 break; 367 } 368 369 return (0); 370 } 371 372 static int 373 hkey_sound_mute(ACPI_HANDLE h, int op, UINT32 *val) 374 { 375 376 ACPI_SERIAL_ASSERT(panasonic); 377 switch (op) { 378 case HKEY_SET: 379 if (*val != 0 && *val != 1) 380 return (EINVAL); 381 acpi_panasonic_sset(h, HKEY_REG_SOUND_MUTE, *val); 382 break; 383 case HKEY_GET: 384 *val = acpi_panasonic_sinf(h, HKEY_REG_SOUND_MUTE); 385 break; 386 } 387 388 return (0); 389 } 390 391 static int 392 acpi_panasonic_hkey_event(struct acpi_panasonic_softc *sc, ACPI_HANDLE h, 393 UINT32 *arg) 394 { 395 ACPI_BUFFER buf; 396 ACPI_OBJECT *res; 397 UINT64 val; 398 int status; 399 400 ACPI_SERIAL_ASSERT(panasonic); 401 status = ENXIO; 402 403 buf.Length = ACPI_ALLOCATE_BUFFER; 404 buf.Pointer = NULL; 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"); 409 goto end; 410 } 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", 415 (int)(val & 0x7f)); 416 #endif 417 if ((val & 0x7f) > 0 && (val & 0x7f) < 11) { 418 *arg = val; 419 status = 0; 420 } 421 end: 422 if (buf.Pointer) 423 AcpiOsFree(buf.Pointer); 424 425 return (status); 426 } 427 428 static void 429 acpi_panasonic_hkey_action(struct acpi_panasonic_softc *sc, ACPI_HANDLE h, 430 UINT32 key) 431 { 432 struct acpi_softc *acpi_sc; 433 int arg, max, min; 434 435 acpi_sc = acpi_device_get_parent_softc(sc->dev); 436 437 ACPI_SERIAL_ASSERT(panasonic); 438 switch (key) { 439 case 1: 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; 445 if (arg < min) 446 arg = min; 447 else if (arg > max) 448 arg = max; 449 hkey_lcd_brightness(h, HKEY_SET, &arg); 450 break; 451 case 2: 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; 457 if (arg < min) 458 arg = min; 459 else if (arg > max) 460 arg = max; 461 hkey_lcd_brightness(h, HKEY_SET, &arg); 462 break; 463 case 4: 464 /* Toggle sound mute. */ 465 hkey_sound_mute(h, HKEY_GET, &arg); 466 if (arg) 467 arg = 0; 468 else 469 arg = 1; 470 hkey_sound_mute(h, HKEY_SET, &arg); 471 break; 472 case 7: 473 /* Suspend. */ 474 acpi_event_sleep_button_sleep(acpi_sc); 475 break; 476 } 477 } 478 479 static void 480 acpi_panasonic_notify(ACPI_HANDLE h, UINT32 notify, void *context) 481 { 482 struct acpi_panasonic_softc *sc; 483 UINT32 key = 0; 484 485 sc = (struct acpi_panasonic_softc *)context; 486 487 switch (notify) { 488 case 0x80: 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); 493 } 494 ACPI_SERIAL_END(panasonic); 495 break; 496 case 0x81: 497 if (!bootverbose) 498 break; 499 /* FALLTHROUGH */ 500 default: 501 device_printf(sc->dev, "unknown notify: %#x\n", notify); 502 break; 503 } 504 } 505 506 static void 507 acpi_panasonic_power_profile(void *arg) 508 { 509 ACPI_HANDLE handle; 510 UINT32 brightness; 511 512 handle = (ACPI_HANDLE)arg; 513 514 /* Reset current brightness according to new power state. */ 515 ACPI_SERIAL_BEGIN(panasonic); 516 hkey_lcd_brightness(handle, HKEY_GET, &brightness); 517 hkey_lcd_brightness(handle, HKEY_SET, &brightness); 518 ACPI_SERIAL_END(panasonic); 519 } 520