1 /* $NetBSD: fujhk_acpi.c,v 1.5 2021/01/29 15:49:55 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Gregoire Sutre. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: fujhk_acpi.c,v 1.5 2021/01/29 15:49:55 thorpej Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/device.h> 37 #include <sys/module.h> 38 #include <sys/mutex.h> 39 #include <sys/sysctl.h> 40 41 #include <dev/sysmon/sysmonvar.h> 42 43 #include <dev/acpi/acpireg.h> 44 #include <dev/acpi/acpivar.h> 45 46 #define _COMPONENT ACPI_RESOURCE_COMPONENT 47 ACPI_MODULE_NAME ("fujhk_acpi") 48 49 #define FUJITSU_HK_NOTIFY 0x80 50 #define FUJITSU_HK_MODMASK 0xc0000000 51 52 /* Values returned by the GIRB method. */ 53 #define FUJITSU_HK_IRB_HOTKEY_RELEASE 0x0 54 #define FUJITSU_HK_IRB_HOTKEY_PRESS(x) (0x410 | x) 55 56 /* Hotkey index of a value returned by the GIRB method. */ 57 #define FUJITSU_HK_IRB_HOTKEY_INDEX(x) (x & 0x3) 58 59 /* Values for the first argument of the FUNC method. */ 60 #define FUJITSU_FUNC_TARGET_BACKLIGHT 0x1004 61 62 /* Values for the second argument of the FUNC method. */ 63 #define FUJITSU_FUNC_COMMAND_SET 0x1 64 #define FUJITSU_FUNC_COMMAND_GET 0x2 65 66 /* Backlight values for the FUNC method. */ 67 #define FUJITSU_FUNC_BACKLIGHT_ON 0x0 68 #define FUJITSU_FUNC_BACKLIGHT_REDUCED 0x2 69 #define FUJITSU_FUNC_BACKLIGHT_OFF 0x3 70 71 /* Value returned by FUNC on invalid arguments. */ 72 #define FUJITSU_FUNC_INVALID_ARGS 0x80000000 73 74 /* ACPI Fujitsu hotkeys controller capabilities (methods). */ 75 #define FUJITSU_HK_CAP_GIRB __BIT(0) 76 #define FUJITSU_HK_CAP_FUNC __BIT(1) 77 78 struct fujitsu_hk_softc { 79 device_t sc_dev; 80 struct acpi_devnode *sc_node; 81 struct sysctllog *sc_log; 82 kmutex_t sc_mtx; 83 uint16_t sc_caps; 84 #define FUJITSU_HK_PSW_COUNT 4 85 struct sysmon_pswitch sc_smpsw[FUJITSU_HK_PSW_COUNT]; 86 char sc_smpsw_name[FUJITSU_HK_PSW_COUNT][16]; 87 }; 88 89 static const struct device_compatible_entry compat_data[] = { 90 { .compat = "FUJ02E3" }, 91 DEVICE_COMPAT_EOL 92 }; 93 94 static int fujitsu_hk_match(device_t, cfdata_t, void *); 95 static void fujitsu_hk_attach(device_t, device_t, void *); 96 static int fujitsu_hk_detach(device_t, int); 97 static bool fujitsu_hk_suspend(device_t, const pmf_qual_t *); 98 static bool fujitsu_hk_resume(device_t, const pmf_qual_t *); 99 static uint16_t fujitsu_hk_capabilities(const struct acpi_devnode *); 100 static void fujitsu_hk_notify_handler(ACPI_HANDLE, uint32_t, void *); 101 static void fujitsu_hk_event_callback(void *); 102 static void fujitsu_hk_sysctl_setup(struct fujitsu_hk_softc *); 103 static int fujitsu_hk_sysctl_backlight(SYSCTLFN_PROTO); 104 static int fujitsu_hk_get_irb(struct fujitsu_hk_softc *, uint32_t *); 105 static int fujitsu_hk_get_backlight(struct fujitsu_hk_softc *, bool *); 106 static int fujitsu_hk_set_backlight(struct fujitsu_hk_softc *, bool); 107 static bool fujitsu_hk_cap(ACPI_HANDLE, const char *, ACPI_OBJECT_TYPE); 108 static ACPI_STATUS fujitsu_hk_eval_nary_integer(ACPI_HANDLE, 109 const char *, const 110 ACPI_INTEGER *, uint8_t, ACPI_INTEGER *); 111 112 CFATTACH_DECL_NEW(fujhk, sizeof(struct fujitsu_hk_softc), 113 fujitsu_hk_match, fujitsu_hk_attach, fujitsu_hk_detach, NULL); 114 115 static int 116 fujitsu_hk_match(device_t parent, cfdata_t match, void *aux) 117 { 118 struct acpi_attach_args *aa = aux; 119 120 return acpi_compatible_match(aa, compat_data); 121 } 122 123 static void 124 fujitsu_hk_attach(device_t parent, device_t self, void *aux) 125 { 126 struct fujitsu_hk_softc *sc = device_private(self); 127 struct acpi_attach_args *aa = aux; 128 struct acpi_devnode *ad = aa->aa_node; 129 int i; 130 131 aprint_naive(": Fujitsu Hotkeys\n"); 132 aprint_normal(": Fujitsu Hotkeys\n"); 133 134 sc->sc_dev = self; 135 sc->sc_node = ad; 136 sc->sc_log = NULL; 137 sc->sc_caps = fujitsu_hk_capabilities(ad); 138 139 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE); 140 141 for (i = 0; i < FUJITSU_HK_PSW_COUNT; i++) { 142 (void)snprintf(sc->sc_smpsw_name[i], 143 sizeof(sc->sc_smpsw_name[i]), "%s-%d", 144 device_xname(self), i); 145 sc->sc_smpsw[i].smpsw_name = sc->sc_smpsw_name[i]; 146 sc->sc_smpsw[i].smpsw_type = PSWITCH_TYPE_HOTKEY; 147 (void)sysmon_pswitch_register(&sc->sc_smpsw[i]); 148 } 149 150 fujitsu_hk_sysctl_setup(sc); 151 152 (void)pmf_device_register(self, fujitsu_hk_suspend, fujitsu_hk_resume); 153 (void)acpi_register_notify(sc->sc_node, fujitsu_hk_notify_handler); 154 } 155 156 static int 157 fujitsu_hk_detach(device_t self, int flags) 158 { 159 struct fujitsu_hk_softc *sc = device_private(self); 160 int i; 161 162 pmf_device_deregister(self); 163 164 if (sc->sc_log != NULL) 165 sysctl_teardown(&sc->sc_log); 166 167 acpi_deregister_notify(sc->sc_node); 168 169 for (i = 0; i < FUJITSU_HK_PSW_COUNT; i++) 170 sysmon_pswitch_unregister(&sc->sc_smpsw[i]); 171 172 mutex_destroy(&sc->sc_mtx); 173 174 return 0; 175 } 176 177 /* 178 * On the P7120, the backlight needs to be enabled after resume, since the 179 * laptop wakes up with the backlight off (even if it was on before suspend). 180 */ 181 static bool 182 fujitsu_hk_suspend(device_t self, const pmf_qual_t *qual) 183 { 184 struct fujitsu_hk_softc *sc = device_private(self); 185 186 mutex_enter(&sc->sc_mtx); 187 (void)fujitsu_hk_set_backlight(sc, false); 188 mutex_exit(&sc->sc_mtx); 189 190 return true; 191 } 192 193 static bool 194 fujitsu_hk_resume(device_t self, const pmf_qual_t *qual) 195 { 196 struct fujitsu_hk_softc *sc = device_private(self); 197 198 mutex_enter(&sc->sc_mtx); 199 (void)fujitsu_hk_set_backlight(sc, true); 200 mutex_exit(&sc->sc_mtx); 201 202 return true; 203 } 204 205 static uint16_t 206 fujitsu_hk_capabilities(const struct acpi_devnode *ad) 207 { 208 uint16_t caps; 209 210 caps = 0; 211 212 if (fujitsu_hk_cap(ad->ad_handle, "GIRB", ACPI_TYPE_INTEGER)) 213 caps |= FUJITSU_HK_CAP_GIRB; 214 215 if (fujitsu_hk_cap(ad->ad_handle, "FUNC", ACPI_TYPE_METHOD)) 216 caps |= FUJITSU_HK_CAP_FUNC; 217 218 return caps; 219 } 220 221 static void 222 fujitsu_hk_notify_handler(ACPI_HANDLE handle, uint32_t evt, void *context) 223 { 224 struct fujitsu_hk_softc *sc = device_private(context); 225 static const int handler = OSL_NOTIFY_HANDLER; 226 227 switch (evt) { 228 229 case FUJITSU_HK_NOTIFY: 230 (void)AcpiOsExecute(handler, fujitsu_hk_event_callback, sc); 231 break; 232 233 default: 234 aprint_debug_dev(sc->sc_dev, "unknown notify 0x%02X\n", evt); 235 } 236 } 237 238 static void 239 fujitsu_hk_event_callback(void *arg) 240 { 241 struct fujitsu_hk_softc *sc = arg; 242 const int max_irb_buffer_size = 100; 243 uint32_t irb; 244 int i, index; 245 246 for (i = 0; i < max_irb_buffer_size; i++) { 247 if (fujitsu_hk_get_irb(sc, &irb) || irb == 0) 248 return; 249 250 switch (irb & ~FUJITSU_HK_MODMASK) { 251 case FUJITSU_HK_IRB_HOTKEY_RELEASE: 252 /* Hotkey button release event (nothing to do). */ 253 break; 254 case FUJITSU_HK_IRB_HOTKEY_PRESS(0): 255 case FUJITSU_HK_IRB_HOTKEY_PRESS(1): 256 case FUJITSU_HK_IRB_HOTKEY_PRESS(2): 257 case FUJITSU_HK_IRB_HOTKEY_PRESS(3): 258 /* Hotkey button press event. */ 259 index = FUJITSU_HK_IRB_HOTKEY_INDEX(irb); 260 sysmon_pswitch_event(&sc->sc_smpsw[index], 261 PSWITCH_EVENT_PRESSED); 262 break; 263 default: 264 aprint_error_dev(sc->sc_dev, 265 "unknown GIRB result: 0x%"PRIx32"\n", irb); 266 break; 267 } 268 } 269 } 270 271 static void 272 fujitsu_hk_sysctl_setup(struct fujitsu_hk_softc *sc) 273 { 274 const struct sysctlnode *rnode; 275 bool dummy_state; 276 277 if (fujitsu_hk_get_backlight(sc, &dummy_state) == 0) { 278 if ((sysctl_createv(&sc->sc_log, 0, NULL, &rnode, 279 0, CTLTYPE_NODE, "acpi", NULL, 280 NULL, 0, NULL, 0, 281 CTL_HW, CTL_CREATE, CTL_EOL)) != 0) 282 goto fail; 283 284 if ((sysctl_createv(&sc->sc_log, 0, &rnode, &rnode, 285 0, CTLTYPE_NODE, device_xname(sc->sc_dev), 286 SYSCTL_DESCR("Fujitsu hotkeys controls"), 287 NULL, 0, NULL, 0, 288 CTL_CREATE, CTL_EOL)) != 0) 289 goto fail; 290 291 (void)sysctl_createv(&sc->sc_log, 0, &rnode, NULL, 292 CTLFLAG_READWRITE, CTLTYPE_BOOL, "backlight", 293 SYSCTL_DESCR("Internal DFP backlight switch state"), 294 fujitsu_hk_sysctl_backlight, 0, (void *)sc, 0, 295 CTL_CREATE, CTL_EOL); 296 } 297 298 return; 299 300 fail: 301 aprint_error_dev(sc->sc_dev, "couldn't add sysctl nodes\n"); 302 } 303 304 static int 305 fujitsu_hk_sysctl_backlight(SYSCTLFN_ARGS) 306 { 307 struct sysctlnode node; 308 struct fujitsu_hk_softc *sc; 309 bool val; 310 int error; 311 312 node = *rnode; 313 sc = node.sysctl_data; 314 315 mutex_enter(&sc->sc_mtx); 316 error = fujitsu_hk_get_backlight(sc, &val); 317 mutex_exit(&sc->sc_mtx); 318 319 if (error) 320 return error; 321 322 node.sysctl_data = &val; 323 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 324 if (error || newp == NULL) 325 return error; 326 327 mutex_enter(&sc->sc_mtx); 328 error = fujitsu_hk_set_backlight(sc, val); 329 mutex_exit(&sc->sc_mtx); 330 331 return error; 332 } 333 334 static int 335 fujitsu_hk_get_irb(struct fujitsu_hk_softc *sc, uint32_t *valuep) 336 { 337 ACPI_HANDLE hdl = sc->sc_node->ad_handle; 338 ACPI_INTEGER val; 339 ACPI_STATUS rv; 340 341 if (!(sc->sc_caps & FUJITSU_HK_CAP_GIRB)) 342 return ENODEV; 343 344 rv = acpi_eval_integer(hdl, "GIRB", &val); 345 if (ACPI_FAILURE(rv)) { 346 aprint_error_dev(sc->sc_dev, "failed to evaluate %s.%s: %s\n", 347 acpi_name(hdl), "GIRB", AcpiFormatException(rv)); 348 return EIO; 349 } 350 351 *valuep = (uint32_t)val; 352 353 return 0; 354 } 355 356 static int 357 fujitsu_hk_get_backlight(struct fujitsu_hk_softc *sc, bool *valuep) 358 { 359 ACPI_HANDLE hdl = sc->sc_node->ad_handle; 360 ACPI_INTEGER args[] = { 361 FUJITSU_FUNC_TARGET_BACKLIGHT, 362 FUJITSU_FUNC_COMMAND_GET, 363 0x4, 364 0x0 365 }; 366 ACPI_INTEGER val; 367 ACPI_STATUS rv; 368 369 if (!(sc->sc_caps & FUJITSU_HK_CAP_FUNC)) 370 return ENODEV; 371 372 rv = fujitsu_hk_eval_nary_integer(hdl, "FUNC", args, 4, &val); 373 if (ACPI_FAILURE(rv)) { 374 aprint_error_dev(sc->sc_dev, "failed to evaluate %s.%s: %s\n", 375 acpi_name(hdl), "FUNC", AcpiFormatException(rv)); 376 return EIO; 377 } 378 379 if (val == FUJITSU_FUNC_INVALID_ARGS) 380 return ENODEV; 381 382 if (val == FUJITSU_FUNC_BACKLIGHT_ON) 383 *valuep = true; 384 else if (val == FUJITSU_FUNC_BACKLIGHT_OFF) 385 *valuep = false; 386 else 387 return ERANGE; 388 389 return 0; 390 } 391 392 static int 393 fujitsu_hk_set_backlight(struct fujitsu_hk_softc *sc, bool value) 394 { 395 ACPI_HANDLE hdl = sc->sc_node->ad_handle; 396 ACPI_INTEGER args[] = { 397 FUJITSU_FUNC_TARGET_BACKLIGHT, 398 FUJITSU_FUNC_COMMAND_SET, 399 0x4, 400 0x0 401 }; 402 ACPI_INTEGER val; 403 ACPI_STATUS rv; 404 405 if (!(sc->sc_caps & FUJITSU_HK_CAP_FUNC)) 406 return ENODEV; 407 408 if (value) 409 args[3] = FUJITSU_FUNC_BACKLIGHT_ON; 410 else 411 args[3] = FUJITSU_FUNC_BACKLIGHT_OFF; 412 413 rv = fujitsu_hk_eval_nary_integer(hdl, "FUNC", args, 4, &val); 414 if (ACPI_FAILURE(rv)) { 415 aprint_error_dev(sc->sc_dev, "failed to evaluate %s.%s: %s\n", 416 acpi_name(hdl), "FUNC", AcpiFormatException(rv)); 417 return EIO; 418 } 419 420 if (val == FUJITSU_FUNC_INVALID_ARGS) 421 return ENODEV; 422 423 return 0; 424 } 425 426 /* 427 * fujitsu_hk_cap: 428 * 429 * Returns true if and only if (a) the object handle.path exists and 430 * (b) this object is a method or has the given type. 431 */ 432 static bool 433 fujitsu_hk_cap(ACPI_HANDLE handle, const char *path, ACPI_OBJECT_TYPE type) 434 { 435 ACPI_HANDLE hdl; 436 ACPI_OBJECT_TYPE typ; 437 438 KASSERT(handle != NULL); 439 440 if (ACPI_FAILURE(AcpiGetHandle(handle, path, &hdl))) 441 return false; 442 443 if (ACPI_FAILURE(AcpiGetType(hdl, &typ))) 444 return false; 445 446 if (typ != ACPI_TYPE_METHOD && typ != type) 447 return false; 448 449 return true; 450 } 451 452 /* 453 * fujitsu_hk_eval_nary_integer: 454 * 455 * Evaluate an object that takes as input an arbitrary (possible null) 456 * number of integer parameters. If res is not NULL, then *res is filled 457 * with the result of the evaluation, and AE_NULL_OBJECT is returned if 458 * the evaluation produced no result. 459 */ 460 static ACPI_STATUS 461 fujitsu_hk_eval_nary_integer(ACPI_HANDLE handle, const char *path, const 462 ACPI_INTEGER *args, uint8_t count, ACPI_INTEGER *res) 463 { 464 ACPI_OBJECT_LIST paramlist; 465 ACPI_OBJECT retobj, objpool[4], *argobjs; 466 ACPI_BUFFER buf; 467 ACPI_STATUS rv; 468 uint8_t i; 469 470 /* Require that (args == NULL) if and only if (count == 0). */ 471 KASSERT((args != NULL || count == 0) && (args == NULL || count != 0)); 472 473 /* The object pool should be large enough for our callers. */ 474 KASSERT(count <= __arraycount(objpool)); 475 476 if (handle == NULL) 477 handle = ACPI_ROOT_OBJECT; 478 479 /* Convert the given array args into an array of ACPI objects. */ 480 argobjs = objpool; 481 for (i = 0; i < count; i++) { 482 argobjs[i].Type = ACPI_TYPE_INTEGER; 483 argobjs[i].Integer.Value = args[i]; 484 } 485 486 paramlist.Count = count; 487 paramlist.Pointer = argobjs; 488 489 (void)memset(&retobj, 0, sizeof(retobj)); 490 buf.Pointer = &retobj; 491 buf.Length = sizeof(retobj); 492 493 rv = AcpiEvaluateObject(handle, path, ¶mlist, &buf); 494 495 if (ACPI_FAILURE(rv)) 496 return rv; 497 498 /* 499 * If a return value is expected and desired (i.e. res != NULL), 500 * then copy the result into *res. 501 */ 502 if (res != NULL) { 503 if (buf.Length == 0) 504 return AE_NULL_OBJECT; 505 506 if (retobj.Type != ACPI_TYPE_INTEGER) 507 return AE_TYPE; 508 509 *res = retobj.Integer.Value; 510 } 511 512 return AE_OK; 513 } 514 515 MODULE(MODULE_CLASS_DRIVER, fujhk, "sysmon_power"); 516 517 #ifdef _MODULE 518 #include "ioconf.c" 519 #endif 520 521 static int 522 fujhk_modcmd(modcmd_t cmd, void *aux) 523 { 524 int rv = 0; 525 526 switch (cmd) { 527 528 case MODULE_CMD_INIT: 529 530 #ifdef _MODULE 531 rv = config_init_component(cfdriver_ioconf_fujhk, 532 cfattach_ioconf_fujhk, cfdata_ioconf_fujhk); 533 #endif 534 break; 535 536 case MODULE_CMD_FINI: 537 538 #ifdef _MODULE 539 rv = config_fini_component(cfdriver_ioconf_fujhk, 540 cfattach_ioconf_fujhk, cfdata_ioconf_fujhk); 541 #endif 542 break; 543 544 default: 545 rv = ENOTTY; 546 } 547 548 return rv; 549 } 550