1 /*- 2 * Copyright (c) 2004 Takanori Watanabe 3 * Copyright (c) 2005 Markus Brueffer <markus@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 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 /* 32 * Driver for extra ACPI-controlled gadgets found on IBM ThinkPad laptops. 33 * Inspired by the ibm-acpi and tpb projects which implement these features 34 * on Linux. 35 * 36 * acpi-ibm: <http://ibm-acpi.sourceforge.net/> 37 * tpb: <http://www.nongnu.org/tpb/> 38 */ 39 40 #include "opt_acpi.h" 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/bus.h> 45 #include <machine/cpufunc.h> 46 47 #include <contrib/dev/acpica/include/acpi.h> 48 #include <contrib/dev/acpica/include/accommon.h> 49 50 #include "acpi_if.h" 51 #include <sys/module.h> 52 #include <dev/acpica/acpivar.h> 53 #include <dev/led/led.h> 54 #include <sys/power.h> 55 #include <sys/sbuf.h> 56 #include <sys/sysctl.h> 57 #include <isa/rtc.h> 58 59 #define _COMPONENT ACPI_OEM 60 ACPI_MODULE_NAME("IBM") 61 62 /* Internal methods */ 63 #define ACPI_IBM_METHOD_EVENTS 1 64 #define ACPI_IBM_METHOD_EVENTMASK 2 65 #define ACPI_IBM_METHOD_HOTKEY 3 66 #define ACPI_IBM_METHOD_BRIGHTNESS 4 67 #define ACPI_IBM_METHOD_VOLUME 5 68 #define ACPI_IBM_METHOD_MUTE 6 69 #define ACPI_IBM_METHOD_THINKLIGHT 7 70 #define ACPI_IBM_METHOD_BLUETOOTH 8 71 #define ACPI_IBM_METHOD_WLAN 9 72 #define ACPI_IBM_METHOD_FANSPEED 10 73 #define ACPI_IBM_METHOD_FANLEVEL 11 74 #define ACPI_IBM_METHOD_FANSTATUS 12 75 #define ACPI_IBM_METHOD_THERMAL 13 76 #define ACPI_IBM_METHOD_HANDLEREVENTS 14 77 #define ACPI_IBM_METHOD_MIC_LED 15 78 79 /* Hotkeys/Buttons */ 80 #define IBM_RTC_HOTKEY1 0x64 81 #define IBM_RTC_MASK_HOME (1 << 0) 82 #define IBM_RTC_MASK_SEARCH (1 << 1) 83 #define IBM_RTC_MASK_MAIL (1 << 2) 84 #define IBM_RTC_MASK_WLAN (1 << 5) 85 #define IBM_RTC_HOTKEY2 0x65 86 #define IBM_RTC_MASK_THINKPAD (1 << 3) 87 #define IBM_RTC_MASK_ZOOM (1 << 5) 88 #define IBM_RTC_MASK_VIDEO (1 << 6) 89 #define IBM_RTC_MASK_HIBERNATE (1 << 7) 90 #define IBM_RTC_THINKLIGHT 0x66 91 #define IBM_RTC_MASK_THINKLIGHT (1 << 4) 92 #define IBM_RTC_SCREENEXPAND 0x67 93 #define IBM_RTC_MASK_SCREENEXPAND (1 << 5) 94 #define IBM_RTC_BRIGHTNESS 0x6c 95 #define IBM_RTC_MASK_BRIGHTNESS (1 << 5) 96 #define IBM_RTC_VOLUME 0x6e 97 #define IBM_RTC_MASK_VOLUME (1 << 7) 98 99 /* Embedded Controller registers */ 100 #define IBM_EC_BRIGHTNESS 0x31 101 #define IBM_EC_MASK_BRI 0x7 102 #define IBM_EC_VOLUME 0x30 103 #define IBM_EC_MASK_VOL 0xf 104 #define IBM_EC_MASK_MUTE (1 << 6) 105 #define IBM_EC_FANSTATUS 0x2F 106 #define IBM_EC_MASK_FANLEVEL 0x3f 107 #define IBM_EC_MASK_FANDISENGAGED (1 << 6) 108 #define IBM_EC_MASK_FANSTATUS (1 << 7) 109 #define IBM_EC_FANSPEED 0x84 110 111 /* CMOS Commands */ 112 #define IBM_CMOS_VOLUME_DOWN 0 113 #define IBM_CMOS_VOLUME_UP 1 114 #define IBM_CMOS_VOLUME_MUTE 2 115 #define IBM_CMOS_BRIGHTNESS_UP 4 116 #define IBM_CMOS_BRIGHTNESS_DOWN 5 117 118 /* ACPI methods */ 119 #define IBM_NAME_KEYLIGHT "KBLT" 120 #define IBM_NAME_WLAN_BT_GET "GBDC" 121 #define IBM_NAME_WLAN_BT_SET "SBDC" 122 #define IBM_NAME_MASK_BT (1 << 1) 123 #define IBM_NAME_MASK_WLAN (1 << 2) 124 #define IBM_NAME_THERMAL_GET "TMP7" 125 #define IBM_NAME_THERMAL_UPDT "UPDT" 126 127 #define IBM_NAME_EVENTS_STATUS_GET "DHKC" 128 #define IBM_NAME_EVENTS_MASK_GET "DHKN" 129 #define IBM_NAME_EVENTS_STATUS_SET "MHKC" 130 #define IBM_NAME_EVENTS_MASK_SET "MHKM" 131 #define IBM_NAME_EVENTS_GET "MHKP" 132 #define IBM_NAME_EVENTS_AVAILMASK "MHKA" 133 134 /* Event Code */ 135 #define IBM_EVENT_LCD_BACKLIGHT 0x03 136 #define IBM_EVENT_SUSPEND_TO_RAM 0x04 137 #define IBM_EVENT_BLUETOOTH 0x05 138 #define IBM_EVENT_SCREEN_EXPAND 0x07 139 #define IBM_EVENT_SUSPEND_TO_DISK 0x0c 140 #define IBM_EVENT_BRIGHTNESS_UP 0x10 141 #define IBM_EVENT_BRIGHTNESS_DOWN 0x11 142 #define IBM_EVENT_THINKLIGHT 0x12 143 #define IBM_EVENT_ZOOM 0x14 144 #define IBM_EVENT_VOLUME_UP 0x15 145 #define IBM_EVENT_VOLUME_DOWN 0x16 146 #define IBM_EVENT_MUTE 0x17 147 #define IBM_EVENT_ACCESS_IBM_BUTTON 0x18 148 149 #define ABS(x) (((x) < 0)? -(x) : (x)) 150 151 struct acpi_ibm_softc { 152 device_t dev; 153 ACPI_HANDLE handle; 154 155 /* Embedded controller */ 156 device_t ec_dev; 157 ACPI_HANDLE ec_handle; 158 159 /* CMOS */ 160 ACPI_HANDLE cmos_handle; 161 162 /* Fan status */ 163 ACPI_HANDLE fan_handle; 164 int fan_levels; 165 166 /* Keylight commands and states */ 167 ACPI_HANDLE light_handle; 168 int light_cmd_on; 169 int light_cmd_off; 170 int light_val; 171 int light_get_supported; 172 int light_set_supported; 173 174 /* led(4) interface */ 175 struct cdev *led_dev; 176 int led_busy; 177 int led_state; 178 179 /* Mic led handle */ 180 ACPI_HANDLE mic_led_handle; 181 int mic_led_state; 182 183 int wlan_bt_flags; 184 int thermal_updt_supported; 185 186 unsigned int events_availmask; 187 unsigned int events_initialmask; 188 int events_mask_supported; 189 int events_enable; 190 191 unsigned int handler_events; 192 193 struct sysctl_ctx_list *sysctl_ctx; 194 struct sysctl_oid *sysctl_tree; 195 }; 196 197 static struct { 198 char *name; 199 int method; 200 char *description; 201 int flag_rdonly; 202 } acpi_ibm_sysctls[] = { 203 { 204 .name = "events", 205 .method = ACPI_IBM_METHOD_EVENTS, 206 .description = "ACPI events enable", 207 }, 208 { 209 .name = "eventmask", 210 .method = ACPI_IBM_METHOD_EVENTMASK, 211 .description = "ACPI eventmask", 212 }, 213 { 214 .name = "hotkey", 215 .method = ACPI_IBM_METHOD_HOTKEY, 216 .description = "Key Status", 217 .flag_rdonly = 1 218 }, 219 { 220 .name = "lcd_brightness", 221 .method = ACPI_IBM_METHOD_BRIGHTNESS, 222 .description = "LCD Brightness", 223 }, 224 { 225 .name = "volume", 226 .method = ACPI_IBM_METHOD_VOLUME, 227 .description = "Volume", 228 }, 229 { 230 .name = "mute", 231 .method = ACPI_IBM_METHOD_MUTE, 232 .description = "Mute", 233 }, 234 { 235 .name = "thinklight", 236 .method = ACPI_IBM_METHOD_THINKLIGHT, 237 .description = "Thinklight enable", 238 }, 239 { 240 .name = "bluetooth", 241 .method = ACPI_IBM_METHOD_BLUETOOTH, 242 .description = "Bluetooth enable", 243 }, 244 { 245 .name = "wlan", 246 .method = ACPI_IBM_METHOD_WLAN, 247 .description = "WLAN enable", 248 .flag_rdonly = 1 249 }, 250 { 251 .name = "fan_speed", 252 .method = ACPI_IBM_METHOD_FANSPEED, 253 .description = "Fan speed", 254 .flag_rdonly = 1 255 }, 256 { 257 .name = "fan_level", 258 .method = ACPI_IBM_METHOD_FANLEVEL, 259 .description = "Fan level", 260 }, 261 { 262 .name = "fan", 263 .method = ACPI_IBM_METHOD_FANSTATUS, 264 .description = "Fan enable", 265 }, 266 { 267 .name = "mic_led", 268 .method = ACPI_IBM_METHOD_MIC_LED, 269 .description = "Mic led", 270 }, 271 { NULL, 0, NULL, 0 } 272 }; 273 274 /* 275 * Per-model default list of event mask. 276 */ 277 #define ACPI_IBM_HKEY_RFKILL_MASK (1 << 4) 278 #define ACPI_IBM_HKEY_DSWITCH_MASK (1 << 6) 279 #define ACPI_IBM_HKEY_BRIGHTNESS_UP_MASK (1 << 15) 280 #define ACPI_IBM_HKEY_BRIGHTNESS_DOWN_MASK (1 << 16) 281 #define ACPI_IBM_HKEY_SEARCH_MASK (1 << 18) 282 #define ACPI_IBM_HKEY_MICMUTE_MASK (1 << 26) 283 #define ACPI_IBM_HKEY_SETTINGS_MASK (1 << 28) 284 #define ACPI_IBM_HKEY_VIEWOPEN_MASK (1 << 30) 285 #define ACPI_IBM_HKEY_VIEWALL_MASK (1 << 31) 286 287 struct acpi_ibm_models { 288 const char *maker; 289 const char *product; 290 uint32_t eventmask; 291 } acpi_ibm_models[] = { 292 { "LENOVO", "20BSCTO1WW", 293 ACPI_IBM_HKEY_RFKILL_MASK | 294 ACPI_IBM_HKEY_DSWITCH_MASK | 295 ACPI_IBM_HKEY_BRIGHTNESS_UP_MASK | 296 ACPI_IBM_HKEY_BRIGHTNESS_DOWN_MASK | 297 ACPI_IBM_HKEY_SEARCH_MASK | 298 ACPI_IBM_HKEY_MICMUTE_MASK | 299 ACPI_IBM_HKEY_SETTINGS_MASK | 300 ACPI_IBM_HKEY_VIEWOPEN_MASK | 301 ACPI_IBM_HKEY_VIEWALL_MASK 302 } 303 }; 304 305 ACPI_SERIAL_DECL(ibm, "ACPI IBM extras"); 306 307 static int acpi_ibm_probe(device_t dev); 308 static int acpi_ibm_attach(device_t dev); 309 static int acpi_ibm_detach(device_t dev); 310 static int acpi_ibm_resume(device_t dev); 311 312 static void ibm_led(void *softc, int onoff); 313 static void ibm_led_task(struct acpi_ibm_softc *sc, int pending __unused); 314 315 static int acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS); 316 static int acpi_ibm_sysctl_init(struct acpi_ibm_softc *sc, int method); 317 static int acpi_ibm_sysctl_get(struct acpi_ibm_softc *sc, int method); 318 static int acpi_ibm_sysctl_set(struct acpi_ibm_softc *sc, int method, int val); 319 320 static int acpi_ibm_eventmask_set(struct acpi_ibm_softc *sc, int val); 321 static int acpi_ibm_thermal_sysctl(SYSCTL_HANDLER_ARGS); 322 static int acpi_ibm_handlerevents_sysctl(SYSCTL_HANDLER_ARGS); 323 static void acpi_ibm_notify(ACPI_HANDLE h, UINT32 notify, void *context); 324 325 static int acpi_ibm_brightness_set(struct acpi_ibm_softc *sc, int arg); 326 static int acpi_ibm_bluetooth_set(struct acpi_ibm_softc *sc, int arg); 327 static int acpi_ibm_thinklight_set(struct acpi_ibm_softc *sc, int arg); 328 static int acpi_ibm_volume_set(struct acpi_ibm_softc *sc, int arg); 329 static int acpi_ibm_mute_set(struct acpi_ibm_softc *sc, int arg); 330 331 static device_method_t acpi_ibm_methods[] = { 332 /* Device interface */ 333 DEVMETHOD(device_probe, acpi_ibm_probe), 334 DEVMETHOD(device_attach, acpi_ibm_attach), 335 DEVMETHOD(device_detach, acpi_ibm_detach), 336 DEVMETHOD(device_resume, acpi_ibm_resume), 337 338 DEVMETHOD_END 339 }; 340 341 static driver_t acpi_ibm_driver = { 342 "acpi_ibm", 343 acpi_ibm_methods, 344 sizeof(struct acpi_ibm_softc), 345 }; 346 347 static devclass_t acpi_ibm_devclass; 348 349 DRIVER_MODULE(acpi_ibm, acpi, acpi_ibm_driver, acpi_ibm_devclass, 350 0, 0); 351 MODULE_DEPEND(acpi_ibm, acpi, 1, 1, 1); 352 static char *ibm_ids[] = {"IBM0068", "LEN0068", "LEN0268", NULL}; 353 354 static void 355 ibm_led(void *softc, int onoff) 356 { 357 struct acpi_ibm_softc* sc = (struct acpi_ibm_softc*) softc; 358 359 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 360 361 if (sc->led_busy) 362 return; 363 364 sc->led_busy = 1; 365 sc->led_state = onoff; 366 367 AcpiOsExecute(OSL_NOTIFY_HANDLER, (void *)ibm_led_task, sc); 368 } 369 370 static void 371 ibm_led_task(struct acpi_ibm_softc *sc, int pending __unused) 372 { 373 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 374 375 ACPI_SERIAL_BEGIN(ibm); 376 acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_THINKLIGHT, sc->led_state); 377 ACPI_SERIAL_END(ibm); 378 379 sc->led_busy = 0; 380 } 381 382 static int 383 acpi_ibm_mic_led_set (struct acpi_ibm_softc *sc, int arg) 384 { 385 ACPI_OBJECT_LIST input; 386 ACPI_OBJECT params[1]; 387 ACPI_STATUS status; 388 389 if (arg < 0 || arg > 1) 390 return (EINVAL); 391 392 if (sc->mic_led_handle) { 393 params[0].Type = ACPI_TYPE_INTEGER; 394 params[0].Integer.Value = 0; 395 /* mic led: 0 off, 2 on */ 396 if (arg == 1) 397 params[0].Integer.Value = 2; 398 399 input.Pointer = params; 400 input.Count = 1; 401 402 status = AcpiEvaluateObject (sc->handle, "MMTS", &input, NULL); 403 if (ACPI_SUCCESS(status)) 404 sc->mic_led_state = arg; 405 return(status); 406 } 407 408 return (0); 409 } 410 411 static int 412 acpi_ibm_probe(device_t dev) 413 { 414 int rv; 415 416 if (acpi_disabled("ibm") || 417 device_get_unit(dev) != 0) 418 return (ENXIO); 419 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, ibm_ids, NULL); 420 421 if (rv <= 0) 422 device_set_desc(dev, "IBM ThinkPad ACPI Extras"); 423 424 return (rv); 425 } 426 427 static int 428 acpi_ibm_attach(device_t dev) 429 { 430 int i; 431 int hkey; 432 struct acpi_ibm_softc *sc; 433 char *maker, *product; 434 ACPI_OBJECT_LIST input; 435 ACPI_OBJECT params[1]; 436 ACPI_OBJECT out_obj; 437 ACPI_BUFFER result; 438 devclass_t ec_devclass; 439 440 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__); 441 442 sc = device_get_softc(dev); 443 sc->dev = dev; 444 sc->handle = acpi_get_handle(dev); 445 446 /* Look for the first embedded controller */ 447 if (!(ec_devclass = devclass_find ("acpi_ec"))) { 448 if (bootverbose) 449 device_printf(dev, "Couldn't find acpi_ec devclass\n"); 450 return (EINVAL); 451 } 452 if (!(sc->ec_dev = devclass_get_device(ec_devclass, 0))) { 453 if (bootverbose) 454 device_printf(dev, "Couldn't find acpi_ec device\n"); 455 return (EINVAL); 456 } 457 sc->ec_handle = acpi_get_handle(sc->ec_dev); 458 459 /* Get the sysctl tree */ 460 sc->sysctl_ctx = device_get_sysctl_ctx(dev); 461 sc->sysctl_tree = device_get_sysctl_tree(dev); 462 463 /* Look for event mask and hook up the nodes */ 464 sc->events_mask_supported = ACPI_SUCCESS(acpi_GetInteger(sc->handle, 465 IBM_NAME_EVENTS_MASK_GET, &sc->events_initialmask)); 466 467 if (sc->events_mask_supported) { 468 SYSCTL_ADD_UINT(sc->sysctl_ctx, 469 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, 470 "initialmask", CTLFLAG_RD, 471 &sc->events_initialmask, 0, "Initial eventmask"); 472 473 if (ACPI_SUCCESS (acpi_GetInteger(sc->handle, "MHKV", &hkey))) { 474 device_printf(dev, "Firmware version is 0x%X\n", hkey); 475 switch(hkey >> 8) 476 { 477 case 1: 478 /* The availmask is the bitmask of supported events */ 479 if (ACPI_FAILURE(acpi_GetInteger(sc->handle, 480 IBM_NAME_EVENTS_AVAILMASK, &sc->events_availmask))) 481 sc->events_availmask = 0xffffffff; 482 break; 483 484 case 2: 485 result.Length = sizeof(out_obj); 486 result.Pointer = &out_obj; 487 params[0].Type = ACPI_TYPE_INTEGER; 488 params[0].Integer.Value = 1; 489 input.Pointer = params; 490 input.Count = 1; 491 492 sc->events_availmask = 0xffffffff; 493 494 if (ACPI_SUCCESS(AcpiEvaluateObject (sc->handle, 495 IBM_NAME_EVENTS_AVAILMASK, &input, &result))) 496 sc->events_availmask = out_obj.Integer.Value; 497 break; 498 default: 499 device_printf(dev, "Unknown firmware version 0x%x\n", hkey); 500 break; 501 } 502 } else 503 sc->events_availmask = 0xffffffff; 504 505 SYSCTL_ADD_UINT(sc->sysctl_ctx, 506 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, 507 "availmask", CTLFLAG_RD, 508 &sc->events_availmask, 0, "Mask of supported events"); 509 } 510 511 /* Hook up proc nodes */ 512 for (int i = 0; acpi_ibm_sysctls[i].name != NULL; i++) { 513 if (!acpi_ibm_sysctl_init(sc, acpi_ibm_sysctls[i].method)) 514 continue; 515 516 if (acpi_ibm_sysctls[i].flag_rdonly != 0) { 517 SYSCTL_ADD_PROC(sc->sysctl_ctx, 518 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, 519 acpi_ibm_sysctls[i].name, CTLTYPE_INT | CTLFLAG_RD, 520 sc, i, acpi_ibm_sysctl, "I", 521 acpi_ibm_sysctls[i].description); 522 } else { 523 SYSCTL_ADD_PROC(sc->sysctl_ctx, 524 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, 525 acpi_ibm_sysctls[i].name, CTLTYPE_INT | CTLFLAG_RW, 526 sc, i, acpi_ibm_sysctl, "I", 527 acpi_ibm_sysctls[i].description); 528 } 529 } 530 531 /* Hook up thermal node */ 532 if (acpi_ibm_sysctl_init(sc, ACPI_IBM_METHOD_THERMAL)) { 533 SYSCTL_ADD_PROC(sc->sysctl_ctx, 534 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, 535 "thermal", CTLTYPE_INT | CTLFLAG_RD, 536 sc, 0, acpi_ibm_thermal_sysctl, "I", 537 "Thermal zones"); 538 } 539 540 /* Hook up handlerevents node */ 541 if (acpi_ibm_sysctl_init(sc, ACPI_IBM_METHOD_HANDLEREVENTS)) { 542 SYSCTL_ADD_PROC(sc->sysctl_ctx, 543 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, 544 "handlerevents", CTLTYPE_STRING | CTLFLAG_RW, 545 sc, 0, acpi_ibm_handlerevents_sysctl, "I", 546 "devd(8) events handled by acpi_ibm"); 547 } 548 549 /* Handle notifies */ 550 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY, 551 acpi_ibm_notify, dev); 552 553 /* Hook up light to led(4) */ 554 if (sc->light_set_supported) 555 sc->led_dev = led_create_state(ibm_led, sc, "thinklight", 556 (sc->light_val ? 1 : 0)); 557 558 /* Enable per-model events. */ 559 maker = kern_getenv("smbios.system.maker"); 560 product = kern_getenv("smbios.system.product"); 561 if (maker == NULL || product == NULL) 562 goto nosmbios; 563 564 for (i = 0; i < nitems(acpi_ibm_models); i++) { 565 if (strcmp(maker, acpi_ibm_models[i].maker) == 0 && 566 strcmp(product, acpi_ibm_models[i].product) == 0) { 567 ACPI_SERIAL_BEGIN(ibm); 568 acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTMASK, 569 acpi_ibm_models[i].eventmask); 570 ACPI_SERIAL_END(ibm); 571 } 572 } 573 574 nosmbios: 575 freeenv(maker); 576 freeenv(product); 577 578 /* Enable events by default. */ 579 ACPI_SERIAL_BEGIN(ibm); 580 acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTS, 1); 581 ACPI_SERIAL_END(ibm); 582 583 584 return (0); 585 } 586 587 static int 588 acpi_ibm_detach(device_t dev) 589 { 590 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__); 591 592 struct acpi_ibm_softc *sc = device_get_softc(dev); 593 594 /* Disable events and restore eventmask */ 595 ACPI_SERIAL_BEGIN(ibm); 596 acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTS, 0); 597 acpi_ibm_sysctl_set(sc, ACPI_IBM_METHOD_EVENTMASK, sc->events_initialmask); 598 ACPI_SERIAL_END(ibm); 599 600 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY, acpi_ibm_notify); 601 602 if (sc->led_dev != NULL) 603 led_destroy(sc->led_dev); 604 605 return (0); 606 } 607 608 static int 609 acpi_ibm_resume(device_t dev) 610 { 611 struct acpi_ibm_softc *sc = device_get_softc(dev); 612 613 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__); 614 615 ACPI_SERIAL_BEGIN(ibm); 616 for (int i = 0; acpi_ibm_sysctls[i].name != NULL; i++) { 617 int val; 618 619 val = acpi_ibm_sysctl_get(sc, i); 620 621 if (acpi_ibm_sysctls[i].flag_rdonly != 0) 622 continue; 623 624 acpi_ibm_sysctl_set(sc, i, val); 625 } 626 ACPI_SERIAL_END(ibm); 627 628 /* The mic led does not turn back on when sysctl_set is called in the above loop */ 629 acpi_ibm_mic_led_set(sc, sc->mic_led_state); 630 631 return (0); 632 } 633 634 static int 635 acpi_ibm_eventmask_set(struct acpi_ibm_softc *sc, int val) 636 { 637 ACPI_OBJECT arg[2]; 638 ACPI_OBJECT_LIST args; 639 ACPI_STATUS status; 640 641 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 642 ACPI_SERIAL_ASSERT(ibm); 643 644 args.Count = 2; 645 args.Pointer = arg; 646 arg[0].Type = ACPI_TYPE_INTEGER; 647 arg[1].Type = ACPI_TYPE_INTEGER; 648 649 for (int i = 0; i < 32; ++i) { 650 arg[0].Integer.Value = i+1; 651 arg[1].Integer.Value = (((1 << i) & val) != 0); 652 status = AcpiEvaluateObject(sc->handle, 653 IBM_NAME_EVENTS_MASK_SET, &args, NULL); 654 655 if (ACPI_FAILURE(status)) 656 return (status); 657 } 658 659 return (0); 660 } 661 662 static int 663 acpi_ibm_sysctl(SYSCTL_HANDLER_ARGS) 664 { 665 struct acpi_ibm_softc *sc; 666 int arg; 667 int error = 0; 668 int function; 669 int method; 670 671 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 672 673 sc = (struct acpi_ibm_softc *)oidp->oid_arg1; 674 function = oidp->oid_arg2; 675 method = acpi_ibm_sysctls[function].method; 676 677 ACPI_SERIAL_BEGIN(ibm); 678 arg = acpi_ibm_sysctl_get(sc, method); 679 error = sysctl_handle_int(oidp, &arg, 0, req); 680 681 /* Sanity check */ 682 if (error != 0 || req->newptr == NULL) 683 goto out; 684 685 /* Update */ 686 error = acpi_ibm_sysctl_set(sc, method, arg); 687 688 out: 689 ACPI_SERIAL_END(ibm); 690 return (error); 691 } 692 693 static int 694 acpi_ibm_sysctl_get(struct acpi_ibm_softc *sc, int method) 695 { 696 UINT64 val_ec; 697 int val = 0, key; 698 699 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 700 ACPI_SERIAL_ASSERT(ibm); 701 702 switch (method) { 703 case ACPI_IBM_METHOD_EVENTS: 704 acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_GET, &val); 705 break; 706 707 case ACPI_IBM_METHOD_EVENTMASK: 708 if (sc->events_mask_supported) 709 acpi_GetInteger(sc->handle, IBM_NAME_EVENTS_MASK_GET, &val); 710 break; 711 712 case ACPI_IBM_METHOD_HOTKEY: 713 /* 714 * Construct the hotkey as a bitmask as illustrated below. 715 * Note that whenever a key was pressed, the respecting bit 716 * toggles and nothing else changes. 717 * +--+--+-+-+-+-+-+-+-+-+-+-+ 718 * |11|10|9|8|7|6|5|4|3|2|1|0| 719 * +--+--+-+-+-+-+-+-+-+-+-+-+ 720 * | | | | | | | | | | | | 721 * | | | | | | | | | | | +- Home Button 722 * | | | | | | | | | | +--- Search Button 723 * | | | | | | | | | +----- Mail Button 724 * | | | | | | | | +------- Thinkpad Button 725 * | | | | | | | +--------- Zoom (Fn + Space) 726 * | | | | | | +----------- WLAN Button 727 * | | | | | +------------- Video Button 728 * | | | | +--------------- Hibernate Button 729 * | | | +----------------- Thinklight Button 730 * | | +------------------- Screen expand (Fn + F8) 731 * | +--------------------- Brightness 732 * +------------------------ Volume/Mute 733 */ 734 key = rtcin(IBM_RTC_HOTKEY1); 735 val = (IBM_RTC_MASK_HOME | IBM_RTC_MASK_SEARCH | IBM_RTC_MASK_MAIL | IBM_RTC_MASK_WLAN) & key; 736 key = rtcin(IBM_RTC_HOTKEY2); 737 val |= (IBM_RTC_MASK_THINKPAD | IBM_RTC_MASK_VIDEO | IBM_RTC_MASK_HIBERNATE) & key; 738 val |= (IBM_RTC_MASK_ZOOM & key) >> 1; 739 key = rtcin(IBM_RTC_THINKLIGHT); 740 val |= (IBM_RTC_MASK_THINKLIGHT & key) << 4; 741 key = rtcin(IBM_RTC_SCREENEXPAND); 742 val |= (IBM_RTC_MASK_THINKLIGHT & key) << 4; 743 key = rtcin(IBM_RTC_BRIGHTNESS); 744 val |= (IBM_RTC_MASK_BRIGHTNESS & key) << 5; 745 key = rtcin(IBM_RTC_VOLUME); 746 val |= (IBM_RTC_MASK_VOLUME & key) << 4; 747 break; 748 749 case ACPI_IBM_METHOD_BRIGHTNESS: 750 ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS, &val_ec, 1); 751 val = val_ec & IBM_EC_MASK_BRI; 752 break; 753 754 case ACPI_IBM_METHOD_VOLUME: 755 ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1); 756 val = val_ec & IBM_EC_MASK_VOL; 757 break; 758 759 case ACPI_IBM_METHOD_MUTE: 760 ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1); 761 val = ((val_ec & IBM_EC_MASK_MUTE) == IBM_EC_MASK_MUTE); 762 break; 763 764 case ACPI_IBM_METHOD_THINKLIGHT: 765 if (sc->light_get_supported) 766 acpi_GetInteger(sc->ec_handle, IBM_NAME_KEYLIGHT, &val); 767 else 768 val = sc->light_val; 769 break; 770 771 case ACPI_IBM_METHOD_BLUETOOTH: 772 acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &val); 773 sc->wlan_bt_flags = val; 774 val = ((val & IBM_NAME_MASK_BT) != 0); 775 break; 776 777 case ACPI_IBM_METHOD_WLAN: 778 acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &val); 779 sc->wlan_bt_flags = val; 780 val = ((val & IBM_NAME_MASK_WLAN) != 0); 781 break; 782 783 case ACPI_IBM_METHOD_FANSPEED: 784 if (sc->fan_handle) { 785 if(ACPI_FAILURE(acpi_GetInteger(sc->fan_handle, NULL, &val))) 786 val = -1; 787 } 788 else { 789 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSPEED, &val_ec, 2); 790 val = val_ec; 791 } 792 break; 793 794 case ACPI_IBM_METHOD_FANLEVEL: 795 /* 796 * The IBM_EC_FANSTATUS register works as follows: 797 * Bit 0-5 indicate the level at which the fan operates. Only 798 * values between 0 and 7 have an effect. Everything 799 * above 7 is treated the same as level 7 800 * Bit 6 overrides the fan speed limit if set to 1 801 * Bit 7 indicates at which mode the fan operates: 802 * manual (0) or automatic (1) 803 */ 804 if (!sc->fan_handle) { 805 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1); 806 val = val_ec & IBM_EC_MASK_FANLEVEL; 807 } 808 break; 809 810 case ACPI_IBM_METHOD_FANSTATUS: 811 if (!sc->fan_handle) { 812 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1); 813 val = (val_ec & IBM_EC_MASK_FANSTATUS) == IBM_EC_MASK_FANSTATUS; 814 } 815 else 816 val = -1; 817 break; 818 case ACPI_IBM_METHOD_MIC_LED: 819 if (sc->mic_led_handle) 820 return sc->mic_led_state; 821 else 822 val = -1; 823 break; 824 } 825 826 return (val); 827 } 828 829 static int 830 acpi_ibm_sysctl_set(struct acpi_ibm_softc *sc, int method, int arg) 831 { 832 int val; 833 UINT64 val_ec; 834 ACPI_STATUS status; 835 836 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 837 ACPI_SERIAL_ASSERT(ibm); 838 839 switch (method) { 840 case ACPI_IBM_METHOD_EVENTS: 841 if (arg < 0 || arg > 1) 842 return (EINVAL); 843 844 status = acpi_SetInteger(sc->handle, IBM_NAME_EVENTS_STATUS_SET, arg); 845 if (ACPI_FAILURE(status)) 846 return (status); 847 if (sc->events_mask_supported) 848 return acpi_ibm_eventmask_set(sc, sc->events_availmask); 849 break; 850 851 case ACPI_IBM_METHOD_EVENTMASK: 852 if (sc->events_mask_supported) 853 return acpi_ibm_eventmask_set(sc, arg); 854 break; 855 856 case ACPI_IBM_METHOD_BRIGHTNESS: 857 return acpi_ibm_brightness_set(sc, arg); 858 break; 859 860 case ACPI_IBM_METHOD_VOLUME: 861 return acpi_ibm_volume_set(sc, arg); 862 break; 863 864 case ACPI_IBM_METHOD_MUTE: 865 return acpi_ibm_mute_set(sc, arg); 866 break; 867 868 case ACPI_IBM_METHOD_MIC_LED: 869 return acpi_ibm_mic_led_set (sc, arg); 870 break; 871 872 case ACPI_IBM_METHOD_THINKLIGHT: 873 return acpi_ibm_thinklight_set(sc, arg); 874 break; 875 876 case ACPI_IBM_METHOD_BLUETOOTH: 877 return acpi_ibm_bluetooth_set(sc, arg); 878 break; 879 880 case ACPI_IBM_METHOD_FANLEVEL: 881 if (arg < 0 || arg > 7) 882 return (EINVAL); 883 884 if (!sc->fan_handle) { 885 /* Read the current fanstatus */ 886 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1); 887 val = val_ec & (~IBM_EC_MASK_FANLEVEL); 888 889 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_FANSTATUS, val | arg, 1); 890 } 891 break; 892 893 case ACPI_IBM_METHOD_FANSTATUS: 894 if (arg < 0 || arg > 1) 895 return (EINVAL); 896 897 if (!sc->fan_handle) { 898 /* Read the current fanstatus */ 899 ACPI_EC_READ(sc->ec_dev, IBM_EC_FANSTATUS, &val_ec, 1); 900 901 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_FANSTATUS, 902 (arg == 1) ? (val_ec | IBM_EC_MASK_FANSTATUS) : (val_ec & (~IBM_EC_MASK_FANSTATUS)), 1); 903 } 904 break; 905 } 906 907 return (0); 908 } 909 910 static int 911 acpi_ibm_sysctl_init(struct acpi_ibm_softc *sc, int method) 912 { 913 int dummy; 914 ACPI_OBJECT_TYPE cmos_t; 915 ACPI_HANDLE ledb_handle; 916 917 switch (method) { 918 case ACPI_IBM_METHOD_EVENTS: 919 return (TRUE); 920 921 case ACPI_IBM_METHOD_EVENTMASK: 922 return (sc->events_mask_supported); 923 924 case ACPI_IBM_METHOD_HOTKEY: 925 case ACPI_IBM_METHOD_BRIGHTNESS: 926 case ACPI_IBM_METHOD_VOLUME: 927 case ACPI_IBM_METHOD_MUTE: 928 /* EC is required here, which was already checked before */ 929 return (TRUE); 930 931 case ACPI_IBM_METHOD_MIC_LED: 932 if (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "MMTS", &sc->mic_led_handle))) 933 { 934 /* Turn off mic led by default */ 935 acpi_ibm_mic_led_set (sc, 0); 936 return(TRUE); 937 } 938 else 939 sc->mic_led_handle = NULL; 940 return (FALSE); 941 942 case ACPI_IBM_METHOD_THINKLIGHT: 943 sc->cmos_handle = NULL; 944 sc->light_get_supported = ACPI_SUCCESS(acpi_GetInteger( 945 sc->ec_handle, IBM_NAME_KEYLIGHT, &sc->light_val)); 946 947 if ((ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\UCMS", &sc->light_handle)) || 948 ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\CMOS", &sc->light_handle)) || 949 ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\CMS", &sc->light_handle))) && 950 ACPI_SUCCESS(AcpiGetType(sc->light_handle, &cmos_t)) && 951 cmos_t == ACPI_TYPE_METHOD) { 952 sc->light_cmd_on = 0x0c; 953 sc->light_cmd_off = 0x0d; 954 sc->cmos_handle = sc->light_handle; 955 } 956 else if (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\LGHT", &sc->light_handle))) { 957 sc->light_cmd_on = 1; 958 sc->light_cmd_off = 0; 959 } 960 else 961 sc->light_handle = NULL; 962 963 sc->light_set_supported = (sc->light_handle && 964 ACPI_FAILURE(AcpiGetHandle(sc->ec_handle, "LEDB", &ledb_handle))); 965 966 if (sc->light_get_supported) 967 return (TRUE); 968 969 if (sc->light_set_supported) { 970 sc->light_val = 0; 971 return (TRUE); 972 } 973 974 return (FALSE); 975 976 case ACPI_IBM_METHOD_BLUETOOTH: 977 case ACPI_IBM_METHOD_WLAN: 978 if (ACPI_SUCCESS(acpi_GetInteger(sc->handle, IBM_NAME_WLAN_BT_GET, &dummy))) 979 return (TRUE); 980 return (FALSE); 981 982 case ACPI_IBM_METHOD_FANSPEED: 983 /* 984 * Some models report the fan speed in levels from 0-7 985 * Newer models report it contiguously 986 */ 987 sc->fan_levels = 988 (ACPI_SUCCESS(AcpiGetHandle(sc->handle, "GFAN", &sc->fan_handle)) || 989 ACPI_SUCCESS(AcpiGetHandle(sc->handle, "\\FSPD", &sc->fan_handle))); 990 return (TRUE); 991 992 case ACPI_IBM_METHOD_FANLEVEL: 993 case ACPI_IBM_METHOD_FANSTATUS: 994 /* 995 * Fan status is only supported on those models, 996 * which report fan RPM contiguously, not in levels 997 */ 998 if (sc->fan_levels) 999 return (FALSE); 1000 return (TRUE); 1001 1002 case ACPI_IBM_METHOD_THERMAL: 1003 if (ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle, IBM_NAME_THERMAL_GET, &dummy))) { 1004 sc->thermal_updt_supported = ACPI_SUCCESS(acpi_GetInteger(sc->ec_handle, IBM_NAME_THERMAL_UPDT, &dummy)); 1005 return (TRUE); 1006 } 1007 return (FALSE); 1008 1009 case ACPI_IBM_METHOD_HANDLEREVENTS: 1010 return (TRUE); 1011 } 1012 return (FALSE); 1013 } 1014 1015 static int 1016 acpi_ibm_thermal_sysctl(SYSCTL_HANDLER_ARGS) 1017 { 1018 struct acpi_ibm_softc *sc; 1019 int error = 0; 1020 char temp_cmd[] = "TMP0"; 1021 int temp[8]; 1022 1023 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1024 1025 sc = (struct acpi_ibm_softc *)oidp->oid_arg1; 1026 1027 ACPI_SERIAL_BEGIN(ibm); 1028 1029 for (int i = 0; i < 8; ++i) { 1030 temp_cmd[3] = '0' + i; 1031 1032 /* 1033 * The TMPx methods seem to return +/- 128 or 0 1034 * when the respecting sensor is not available 1035 */ 1036 if (ACPI_FAILURE(acpi_GetInteger(sc->ec_handle, temp_cmd, 1037 &temp[i])) || ABS(temp[i]) == 128 || temp[i] == 0) 1038 temp[i] = -1; 1039 else if (sc->thermal_updt_supported) 1040 /* Temperature is reported in tenth of Kelvin */ 1041 temp[i] = (temp[i] - 2731 + 5) / 10; 1042 } 1043 1044 error = sysctl_handle_opaque(oidp, &temp, 8*sizeof(int), req); 1045 1046 ACPI_SERIAL_END(ibm); 1047 return (error); 1048 } 1049 1050 static int 1051 acpi_ibm_handlerevents_sysctl(SYSCTL_HANDLER_ARGS) 1052 { 1053 struct acpi_ibm_softc *sc; 1054 int error = 0; 1055 struct sbuf sb; 1056 char *cp, *ep; 1057 int l, val; 1058 unsigned int handler_events; 1059 char temp[128]; 1060 1061 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1062 1063 sc = (struct acpi_ibm_softc *)oidp->oid_arg1; 1064 1065 if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) 1066 return (ENOMEM); 1067 1068 ACPI_SERIAL_BEGIN(ibm); 1069 1070 /* Get old values if this is a get request. */ 1071 if (req->newptr == NULL) { 1072 for (int i = 0; i < 8 * sizeof(sc->handler_events); i++) 1073 if (sc->handler_events & (1 << i)) 1074 sbuf_printf(&sb, "0x%02x ", i + 1); 1075 if (sbuf_len(&sb) == 0) 1076 sbuf_printf(&sb, "NONE"); 1077 } 1078 1079 sbuf_trim(&sb); 1080 sbuf_finish(&sb); 1081 strlcpy(temp, sbuf_data(&sb), sizeof(temp)); 1082 sbuf_delete(&sb); 1083 1084 error = sysctl_handle_string(oidp, temp, sizeof(temp), req); 1085 1086 /* Check for error or no change */ 1087 if (error != 0 || req->newptr == NULL) 1088 goto out; 1089 1090 /* If the user is setting a string, parse it. */ 1091 handler_events = 0; 1092 cp = temp; 1093 while (*cp) { 1094 if (isspace(*cp)) { 1095 cp++; 1096 continue; 1097 } 1098 1099 ep = cp; 1100 1101 while (*ep && !isspace(*ep)) 1102 ep++; 1103 1104 l = ep - cp; 1105 if (l == 0) 1106 break; 1107 1108 if (strncmp(cp, "NONE", 4) == 0) { 1109 cp = ep; 1110 continue; 1111 } 1112 1113 if (l >= 3 && cp[0] == '0' && (cp[1] == 'X' || cp[1] == 'x')) 1114 val = strtoul(cp, &ep, 16); 1115 else 1116 val = strtoul(cp, &ep, 10); 1117 1118 if (val == 0 || ep == cp || val >= 8 * sizeof(handler_events)) { 1119 cp[l] = '\0'; 1120 device_printf(sc->dev, "invalid event code: %s\n", cp); 1121 error = EINVAL; 1122 goto out; 1123 } 1124 1125 handler_events |= 1 << (val - 1); 1126 1127 cp = ep; 1128 } 1129 1130 sc->handler_events = handler_events; 1131 out: 1132 ACPI_SERIAL_END(ibm); 1133 return (error); 1134 } 1135 1136 static int 1137 acpi_ibm_brightness_set(struct acpi_ibm_softc *sc, int arg) 1138 { 1139 int val, step; 1140 UINT64 val_ec; 1141 ACPI_OBJECT Arg; 1142 ACPI_OBJECT_LIST Args; 1143 ACPI_STATUS status; 1144 1145 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1146 ACPI_SERIAL_ASSERT(ibm); 1147 1148 if (arg < 0 || arg > 7) 1149 return (EINVAL); 1150 1151 /* Read the current brightness */ 1152 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS, &val_ec, 1); 1153 if (ACPI_FAILURE(status)) 1154 return (status); 1155 1156 if (sc->cmos_handle) { 1157 val = val_ec & IBM_EC_MASK_BRI; 1158 1159 Args.Count = 1; 1160 Args.Pointer = &Arg; 1161 Arg.Type = ACPI_TYPE_INTEGER; 1162 Arg.Integer.Value = (arg > val) ? IBM_CMOS_BRIGHTNESS_UP : 1163 IBM_CMOS_BRIGHTNESS_DOWN; 1164 1165 step = (arg > val) ? 1 : -1; 1166 for (int i = val; i != arg; i += step) { 1167 status = AcpiEvaluateObject(sc->cmos_handle, NULL, 1168 &Args, NULL); 1169 if (ACPI_FAILURE(status)) { 1170 /* Record the last value */ 1171 if (i != val) { 1172 ACPI_EC_WRITE(sc->ec_dev, 1173 IBM_EC_BRIGHTNESS, i - step, 1); 1174 } 1175 return (status); 1176 } 1177 } 1178 } 1179 1180 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_BRIGHTNESS, arg, 1); 1181 } 1182 1183 static int 1184 acpi_ibm_bluetooth_set(struct acpi_ibm_softc *sc, int arg) 1185 { 1186 int val; 1187 1188 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1189 ACPI_SERIAL_ASSERT(ibm); 1190 1191 if (arg < 0 || arg > 1) 1192 return (EINVAL); 1193 1194 val = (arg == 1) ? sc->wlan_bt_flags | IBM_NAME_MASK_BT : 1195 sc->wlan_bt_flags & (~IBM_NAME_MASK_BT); 1196 return acpi_SetInteger(sc->handle, IBM_NAME_WLAN_BT_SET, val); 1197 } 1198 1199 static int 1200 acpi_ibm_thinklight_set(struct acpi_ibm_softc *sc, int arg) 1201 { 1202 ACPI_OBJECT Arg; 1203 ACPI_OBJECT_LIST Args; 1204 ACPI_STATUS status; 1205 1206 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1207 ACPI_SERIAL_ASSERT(ibm); 1208 1209 if (arg < 0 || arg > 1) 1210 return (EINVAL); 1211 1212 if (sc->light_set_supported) { 1213 Args.Count = 1; 1214 Args.Pointer = &Arg; 1215 Arg.Type = ACPI_TYPE_INTEGER; 1216 Arg.Integer.Value = arg ? sc->light_cmd_on : sc->light_cmd_off; 1217 1218 status = AcpiEvaluateObject(sc->light_handle, NULL, 1219 &Args, NULL); 1220 if (ACPI_SUCCESS(status)) 1221 sc->light_val = arg; 1222 return (status); 1223 } 1224 1225 return (0); 1226 } 1227 1228 static int 1229 acpi_ibm_volume_set(struct acpi_ibm_softc *sc, int arg) 1230 { 1231 int val, step; 1232 UINT64 val_ec; 1233 ACPI_OBJECT Arg; 1234 ACPI_OBJECT_LIST Args; 1235 ACPI_STATUS status; 1236 1237 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1238 ACPI_SERIAL_ASSERT(ibm); 1239 1240 if (arg < 0 || arg > 14) 1241 return (EINVAL); 1242 1243 /* Read the current volume */ 1244 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1); 1245 if (ACPI_FAILURE(status)) 1246 return (status); 1247 1248 if (sc->cmos_handle) { 1249 val = val_ec & IBM_EC_MASK_VOL; 1250 1251 Args.Count = 1; 1252 Args.Pointer = &Arg; 1253 Arg.Type = ACPI_TYPE_INTEGER; 1254 Arg.Integer.Value = (arg > val) ? IBM_CMOS_VOLUME_UP : 1255 IBM_CMOS_VOLUME_DOWN; 1256 1257 step = (arg > val) ? 1 : -1; 1258 for (int i = val; i != arg; i += step) { 1259 status = AcpiEvaluateObject(sc->cmos_handle, NULL, 1260 &Args, NULL); 1261 if (ACPI_FAILURE(status)) { 1262 /* Record the last value */ 1263 if (i != val) { 1264 val_ec = i - step + 1265 (val_ec & (~IBM_EC_MASK_VOL)); 1266 ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME, 1267 val_ec, 1); 1268 } 1269 return (status); 1270 } 1271 } 1272 } 1273 1274 val_ec = arg + (val_ec & (~IBM_EC_MASK_VOL)); 1275 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME, val_ec, 1); 1276 } 1277 1278 static int 1279 acpi_ibm_mute_set(struct acpi_ibm_softc *sc, int arg) 1280 { 1281 UINT64 val_ec; 1282 ACPI_OBJECT Arg; 1283 ACPI_OBJECT_LIST Args; 1284 ACPI_STATUS status; 1285 1286 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1287 ACPI_SERIAL_ASSERT(ibm); 1288 1289 if (arg < 0 || arg > 1) 1290 return (EINVAL); 1291 1292 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1); 1293 if (ACPI_FAILURE(status)) 1294 return (status); 1295 1296 if (sc->cmos_handle) { 1297 Args.Count = 1; 1298 Args.Pointer = &Arg; 1299 Arg.Type = ACPI_TYPE_INTEGER; 1300 Arg.Integer.Value = IBM_CMOS_VOLUME_MUTE; 1301 1302 status = AcpiEvaluateObject(sc->cmos_handle, NULL, &Args, NULL); 1303 if (ACPI_FAILURE(status)) 1304 return (status); 1305 } 1306 1307 val_ec = (arg == 1) ? val_ec | IBM_EC_MASK_MUTE : 1308 val_ec & (~IBM_EC_MASK_MUTE); 1309 return ACPI_EC_WRITE(sc->ec_dev, IBM_EC_VOLUME, val_ec, 1); 1310 } 1311 1312 static void 1313 acpi_ibm_eventhandler(struct acpi_ibm_softc *sc, int arg) 1314 { 1315 int val; 1316 UINT64 val_ec; 1317 ACPI_STATUS status; 1318 1319 ACPI_SERIAL_BEGIN(ibm); 1320 switch (arg) { 1321 case IBM_EVENT_SUSPEND_TO_RAM: 1322 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND); 1323 break; 1324 1325 case IBM_EVENT_BLUETOOTH: 1326 acpi_ibm_bluetooth_set(sc, (sc->wlan_bt_flags == 0)); 1327 break; 1328 1329 case IBM_EVENT_BRIGHTNESS_UP: 1330 case IBM_EVENT_BRIGHTNESS_DOWN: 1331 /* Read the current brightness */ 1332 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_BRIGHTNESS, 1333 &val_ec, 1); 1334 if (ACPI_FAILURE(status)) 1335 return; 1336 1337 val = val_ec & IBM_EC_MASK_BRI; 1338 val = (arg == IBM_EVENT_BRIGHTNESS_UP) ? val + 1 : val - 1; 1339 acpi_ibm_brightness_set(sc, val); 1340 break; 1341 1342 case IBM_EVENT_THINKLIGHT: 1343 acpi_ibm_thinklight_set(sc, (sc->light_val == 0)); 1344 break; 1345 1346 case IBM_EVENT_VOLUME_UP: 1347 case IBM_EVENT_VOLUME_DOWN: 1348 /* Read the current volume */ 1349 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1); 1350 if (ACPI_FAILURE(status)) 1351 return; 1352 1353 val = val_ec & IBM_EC_MASK_VOL; 1354 val = (arg == IBM_EVENT_VOLUME_UP) ? val + 1 : val - 1; 1355 acpi_ibm_volume_set(sc, val); 1356 break; 1357 1358 case IBM_EVENT_MUTE: 1359 /* Read the current value */ 1360 status = ACPI_EC_READ(sc->ec_dev, IBM_EC_VOLUME, &val_ec, 1); 1361 if (ACPI_FAILURE(status)) 1362 return; 1363 1364 val = ((val_ec & IBM_EC_MASK_MUTE) == IBM_EC_MASK_MUTE); 1365 acpi_ibm_mute_set(sc, (val == 0)); 1366 break; 1367 1368 default: 1369 break; 1370 } 1371 ACPI_SERIAL_END(ibm); 1372 } 1373 1374 static void 1375 acpi_ibm_notify(ACPI_HANDLE h, UINT32 notify, void *context) 1376 { 1377 int event, arg, type; 1378 device_t dev = context; 1379 struct acpi_ibm_softc *sc = device_get_softc(dev); 1380 1381 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify); 1382 1383 if (notify != 0x80) 1384 device_printf(dev, "Unknown notify\n"); 1385 1386 for (;;) { 1387 acpi_GetInteger(acpi_get_handle(dev), IBM_NAME_EVENTS_GET, &event); 1388 if (event == 0) 1389 break; 1390 1391 1392 type = (event >> 12) & 0xf; 1393 arg = event & 0xfff; 1394 switch (type) { 1395 case 1: 1396 if (!(sc->events_availmask & (1 << (arg - 1)))) { 1397 device_printf(dev, "Unknown key %d\n", arg); 1398 break; 1399 } 1400 1401 /* Execute event handler */ 1402 if (sc->handler_events & (1 << (arg - 1))) 1403 acpi_ibm_eventhandler(sc, (arg & 0xff)); 1404 1405 /* Notify devd(8) */ 1406 acpi_UserNotify("IBM", h, (arg & 0xff)); 1407 break; 1408 default: 1409 break; 1410 } 1411 } 1412 } 1413