1 /*- 2 * Copyright (c) 2003 Hiroyuki Aizu <aizu@navi.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include "opt_acpi.h" 32 #include <sys/param.h> 33 #include <sys/kernel.h> 34 #include <sys/module.h> 35 #include <sys/bus.h> 36 37 #include "acpi.h" 38 #include <dev/acpica/acpivar.h> 39 40 /* 41 * Toshiba HCI interface definitions 42 * 43 * HCI is Toshiba's "Hardware Control Interface" which is supposed to 44 * be uniform across all their models. Ideally we would just call 45 * dedicated ACPI methods instead of using this primitive interface. 46 * However, the ACPI methods seem to be incomplete in some areas (for 47 * example they allow setting, but not reading, the LCD brightness 48 * value), so this is still useful. 49 */ 50 51 #define METHOD_HCI "GHCI" 52 #define METHOD_HCI_ENABLE "ENAB" 53 #define METHOD_VIDEO "DSSX" 54 55 /* Operations */ 56 #define HCI_SET 0xFF00 57 #define HCI_GET 0xFE00 58 59 /* Return codes */ 60 #define HCI_SUCCESS 0x0000 61 #define HCI_FAILURE 0x1000 62 #define HCI_NOT_SUPPORTED 0x8000 63 #define HCI_EMPTY 0x8C00 64 65 /* Functions */ 66 #define HCI_REG_LCD_BACKLIGHT 0x0002 67 #define HCI_REG_FAN 0x0004 68 #define HCI_REG_SYSTEM_EVENT 0x0016 69 #define HCI_REG_VIDEO_OUTPUT 0x001C 70 #define HCI_REG_HOTKEY_EVENT 0x001E 71 #define HCI_REG_LCD_BRIGHTNESS 0x002A 72 #define HCI_REG_CPU_SPEED 0x0032 73 74 /* Field definitions */ 75 #define HCI_FAN_SHIFT 7 76 #define HCI_LCD_BRIGHTNESS_BITS 3 77 #define HCI_LCD_BRIGHTNESS_SHIFT (16 - HCI_LCD_BRIGHTNESS_BITS) 78 #define HCI_LCD_BRIGHTNESS_MAX ((1 << HCI_LCD_BRIGHTNESS_BITS) - 1) 79 #define HCI_VIDEO_OUTPUT_FLAG 0x0100 80 #define HCI_VIDEO_OUTPUT_LCD 0x1 81 #define HCI_VIDEO_OUTPUT_CRT 0x2 82 #define HCI_VIDEO_OUTPUT_TV 0x4 83 #define HCI_CPU_SPEED_BITS 3 84 #define HCI_CPU_SPEED_SHIFT (16 - HCI_CPU_SPEED_BITS) 85 #define HCI_CPU_SPEED_MAX ((1 << HCI_CPU_SPEED_BITS) - 1) 86 87 /* Key press/release events. */ 88 #define FN_F1_PRESS 0x013B 89 #define FN_F1_RELEASE 0x01BB 90 #define FN_F2_PRESS 0x013C 91 #define FN_F2_RELEASE 0x01BC 92 #define FN_F3_PRESS 0x013D 93 #define FN_F3_RELEASE 0x01BD 94 #define FN_F4_PRESS 0x013E 95 #define FN_F4_RELEASE 0x01BE 96 #define FN_F5_PRESS 0x013F 97 #define FN_F5_RELEASE 0x01BF 98 #define FN_F6_PRESS 0x0140 99 #define FN_F6_RELEASE 0x01C0 100 #define FN_F7_PRESS 0x0141 101 #define FN_F7_RELEASE 0x01C1 102 #define FN_F8_PRESS 0x0142 103 #define FN_F8_RELEASE 0x01C2 104 #define FN_F9_PRESS 0x0143 105 #define FN_F9_RELEASE 0x01C3 106 #define FN_BS_PRESS 0x010E 107 #define FN_BS_RELEASE 0x018E 108 #define FN_ESC_PRESS 0x0101 109 #define FN_ESC_RELEASE 0x0181 110 #define FN_KNJ_PRESS 0x0129 111 #define FN_KNJ_RELEASE 0x01A9 112 113 /* HCI register definitions. */ 114 #define HCI_WORDS 6 /* Number of registers */ 115 #define HCI_REG_AX 0 /* Operation, then return value */ 116 #define HCI_REG_BX 1 /* Function */ 117 #define HCI_REG_CX 2 /* Argument (in or out) */ 118 #define HCI_REG_DX 3 /* Unused? */ 119 #define HCI_REG_SI 4 /* Unused? */ 120 #define HCI_REG_DI 5 /* Unused? */ 121 122 struct acpi_toshiba_softc { 123 device_t dev; 124 ACPI_HANDLE handle; 125 ACPI_HANDLE video_handle; 126 struct sysctl_ctx_list sysctl_ctx; 127 struct sysctl_oid *sysctl_tree; 128 }; 129 130 /* Prototype for HCI functions for getting/setting a value. */ 131 typedef int hci_fn_t(ACPI_HANDLE, int, UINT32 *); 132 133 static int acpi_toshiba_probe(device_t dev); 134 static int acpi_toshiba_attach(device_t dev); 135 static int acpi_toshiba_detach(device_t dev); 136 static int acpi_toshiba_sysctl(SYSCTL_HANDLER_ARGS); 137 static hci_fn_t hci_force_fan; 138 static hci_fn_t hci_video_output; 139 static hci_fn_t hci_lcd_brightness; 140 static hci_fn_t hci_lcd_backlight; 141 static hci_fn_t hci_cpu_speed; 142 static int hci_call(ACPI_HANDLE h, int op, int function, UINT32 *arg); 143 static void hci_key_action(struct acpi_toshiba_softc *sc, ACPI_HANDLE h, 144 UINT32 key); 145 static void acpi_toshiba_notify(ACPI_HANDLE h, UINT32 notify, 146 void *context); 147 static int acpi_toshiba_video_probe(device_t dev); 148 static int acpi_toshiba_video_attach(device_t dev); 149 150 /* Table of sysctl names and HCI functions to call. */ 151 static struct { 152 char *name; 153 hci_fn_t *handler; 154 } sysctl_table[] = { 155 /* name, handler */ 156 {"force_fan", hci_force_fan}, 157 {"video_output", hci_video_output}, 158 {"lcd_brightness", hci_lcd_brightness}, 159 {"lcd_backlight", hci_lcd_backlight}, 160 {"cpu_speed", hci_cpu_speed}, 161 {NULL, NULL} 162 }; 163 164 static device_method_t acpi_toshiba_methods[] = { 165 DEVMETHOD(device_probe, acpi_toshiba_probe), 166 DEVMETHOD(device_attach, acpi_toshiba_attach), 167 DEVMETHOD(device_detach, acpi_toshiba_detach), 168 169 {0, 0} 170 }; 171 172 static driver_t acpi_toshiba_driver = { 173 "acpi_toshiba", 174 acpi_toshiba_methods, 175 sizeof(struct acpi_toshiba_softc), 176 }; 177 178 static devclass_t acpi_toshiba_devclass; 179 DRIVER_MODULE(acpi_toshiba, acpi, acpi_toshiba_driver, acpi_toshiba_devclass, 180 0, 0); 181 MODULE_DEPEND(acpi_toshiba, acpi, 1, 1, 1); 182 183 static device_method_t acpi_toshiba_video_methods[] = { 184 DEVMETHOD(device_probe, acpi_toshiba_video_probe), 185 DEVMETHOD(device_attach, acpi_toshiba_video_attach), 186 187 {0, 0} 188 }; 189 190 static driver_t acpi_toshiba_video_driver = { 191 "acpi_toshiba_video", 192 acpi_toshiba_video_methods, 193 0, 194 }; 195 196 static devclass_t acpi_toshiba_video_devclass; 197 DRIVER_MODULE(acpi_toshiba_video, acpi, acpi_toshiba_video_driver, 198 acpi_toshiba_video_devclass, 0, 0); 199 MODULE_DEPEND(acpi_toshiba_video, acpi, 1, 1, 1); 200 201 static int enable_fn_keys = 1; 202 TUNABLE_INT("hw.acpi.toshiba.enable_fn_keys", &enable_fn_keys); 203 204 /* 205 * HID Model 206 * ------------------------------------- 207 * TOS6200 Libretto L Series 208 * Dynabook Satellite 2455 209 * Dynabook SS 3500 210 * TOS6207 Dynabook SS2110 Series 211 */ 212 static int 213 acpi_toshiba_probe(device_t dev) 214 { 215 static char *tosh_ids[] = { "TOS6200", "TOS6207", NULL }; 216 217 if (acpi_disabled("toshiba") || 218 ACPI_ID_PROBE(device_get_parent(dev), dev, tosh_ids) == NULL || 219 device_get_unit(dev) != 0) 220 return (ENXIO); 221 222 device_set_desc(dev, "Toshiba HCI Extras"); 223 return (0); 224 } 225 226 static int 227 acpi_toshiba_attach(device_t dev) 228 { 229 struct acpi_toshiba_softc *sc; 230 struct acpi_softc *acpi_sc; 231 ACPI_STATUS status; 232 int i; 233 234 sc = device_get_softc(dev); 235 sc->dev = dev; 236 sc->handle = acpi_get_handle(dev); 237 238 acpi_sc = acpi_device_get_parent_softc(dev); 239 sysctl_ctx_init(&sc->sysctl_ctx); 240 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx, 241 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO, 242 "toshiba", CTLFLAG_RD, 0, ""); 243 244 for (i = 0; sysctl_table[i].name != NULL; i++) { 245 SYSCTL_ADD_PROC(&sc->sysctl_ctx, 246 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, 247 sysctl_table[i].name, 248 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY, 249 sc, i, acpi_toshiba_sysctl, "I", ""); 250 } 251 252 if (enable_fn_keys != 0) { 253 status = AcpiEvaluateObject(sc->handle, METHOD_HCI_ENABLE, 254 NULL, NULL); 255 if (ACPI_FAILURE(status)) { 256 device_printf(dev, "enable FN keys failed\n"); 257 sysctl_ctx_free(&sc->sysctl_ctx); 258 return (ENXIO); 259 } 260 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY, 261 acpi_toshiba_notify, sc); 262 } 263 264 return (0); 265 } 266 267 static int 268 acpi_toshiba_detach(device_t dev) 269 { 270 struct acpi_toshiba_softc *sc; 271 272 sc = device_get_softc(dev); 273 if (enable_fn_keys != 0) { 274 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY, 275 acpi_toshiba_notify); 276 } 277 sysctl_ctx_free(&sc->sysctl_ctx); 278 279 return (0); 280 } 281 282 static int 283 acpi_toshiba_sysctl(SYSCTL_HANDLER_ARGS) 284 { 285 struct acpi_toshiba_softc *sc; 286 UINT32 arg; 287 int function, error = 0; 288 hci_fn_t *handler; 289 290 sc = (struct acpi_toshiba_softc *)oidp->oid_arg1; 291 function = oidp->oid_arg2; 292 handler = sysctl_table[function].handler; 293 294 /* Get the current value from the appropriate function. */ 295 error = handler(sc->handle, HCI_GET, &arg); 296 if (error != 0) 297 return (error); 298 299 /* Send the current value to the user and return if no new value. */ 300 error = sysctl_handle_int(oidp, &arg, 0, req); 301 if (error != 0 || req->newptr == NULL) 302 return (error); 303 304 /* Set the new value via the appropriate function. */ 305 error = handler(sc->handle, HCI_SET, &arg); 306 307 return (error); 308 } 309 310 static int 311 hci_force_fan(ACPI_HANDLE h, int op, UINT32 *state) 312 { 313 int ret; 314 315 if (op == HCI_SET) { 316 if (*state < 0 || *state > 1) 317 return (EINVAL); 318 *state <<= HCI_FAN_SHIFT; 319 } 320 ret = hci_call(h, op, HCI_REG_FAN, state); 321 if (ret == 0 && op == HCI_GET) 322 *state >>= HCI_FAN_SHIFT; 323 return (ret); 324 } 325 326 static int 327 hci_video_output(ACPI_HANDLE h, int op, UINT32 *video_output) 328 { 329 int ret; 330 ACPI_STATUS status; 331 332 if (op == HCI_SET) { 333 if (*video_output < 1 || *video_output > 7) 334 return (EINVAL); 335 if (h == NULL) 336 return (ENXIO); 337 *video_output |= HCI_VIDEO_OUTPUT_FLAG; 338 status = acpi_SetInteger(h, METHOD_VIDEO, *video_output); 339 if (ACPI_SUCCESS(status)) 340 ret = 0; 341 else 342 ret = ENXIO; 343 } else { 344 ret = hci_call(h, op, HCI_REG_VIDEO_OUTPUT, video_output); 345 if (ret == 0) 346 *video_output &= 0xff; 347 } 348 349 return (ret); 350 } 351 352 static int 353 hci_lcd_brightness(ACPI_HANDLE h, int op, UINT32 *brightness) 354 { 355 int ret; 356 357 if (op == HCI_SET) { 358 if (*brightness < 0 || *brightness > HCI_LCD_BRIGHTNESS_MAX) 359 return (EINVAL); 360 *brightness <<= HCI_LCD_BRIGHTNESS_SHIFT; 361 } 362 ret = hci_call(h, op, HCI_REG_LCD_BRIGHTNESS, brightness); 363 if (ret == 0 && op == HCI_GET) 364 *brightness >>= HCI_LCD_BRIGHTNESS_SHIFT; 365 return (ret); 366 } 367 368 static int 369 hci_lcd_backlight(ACPI_HANDLE h, int op, UINT32 *backlight) 370 { 371 if (op == HCI_SET) { 372 if (*backlight < 0 || *backlight > 1) 373 return (EINVAL); 374 } 375 return (hci_call(h, op, HCI_REG_LCD_BACKLIGHT, backlight)); 376 } 377 378 static int 379 hci_cpu_speed(ACPI_HANDLE h, int op, UINT32 *speed) 380 { 381 int ret; 382 383 if (op == HCI_SET) { 384 if (*speed < 0 || *speed > HCI_CPU_SPEED_MAX) 385 return (EINVAL); 386 *speed <<= HCI_CPU_SPEED_SHIFT; 387 } 388 ret = hci_call(h, op, HCI_REG_CPU_SPEED, speed); 389 if (ret == 0 && op == HCI_GET) 390 *speed >>= HCI_CPU_SPEED_SHIFT; 391 return (ret); 392 } 393 394 static int 395 hci_call(ACPI_HANDLE h, int op, int function, UINT32 *arg) 396 { 397 ACPI_OBJECT_LIST args; 398 ACPI_BUFFER results; 399 ACPI_OBJECT obj[HCI_WORDS]; 400 ACPI_OBJECT *res; 401 int status, i, ret; 402 403 status = ENXIO; 404 405 for (i = 0; i < HCI_WORDS; i++) { 406 obj[i].Type = ACPI_TYPE_INTEGER; 407 obj[i].Integer.Value = 0; 408 } 409 obj[HCI_REG_AX].Integer.Value = op; 410 obj[HCI_REG_BX].Integer.Value = function; 411 if (op == HCI_SET) 412 obj[HCI_REG_CX].Integer.Value = *arg; 413 414 args.Count = HCI_WORDS; 415 args.Pointer = obj; 416 results.Pointer = NULL; 417 results.Length = ACPI_ALLOCATE_BUFFER; 418 if (ACPI_FAILURE(AcpiEvaluateObject(h, METHOD_HCI, &args, &results))) 419 goto end; 420 res = (ACPI_OBJECT *)results.Pointer; 421 if (!ACPI_PKG_VALID(res, HCI_WORDS)) { 422 printf("toshiba: invalid package!\n"); 423 return (ENXIO); 424 } 425 426 acpi_PkgInt32(res, HCI_REG_AX, &ret); 427 if (ret == HCI_SUCCESS) { 428 if (op == HCI_GET) 429 acpi_PkgInt32(res, HCI_REG_CX, arg); 430 status = 0; 431 } else if (function == HCI_REG_SYSTEM_EVENT && op == HCI_GET && 432 ret == HCI_NOT_SUPPORTED) { 433 /* 434 * Sometimes system events are disabled without us requesting 435 * it. This workaround attempts to re-enable them. 436 */ 437 i = 1; 438 hci_call(h, HCI_SET, HCI_REG_SYSTEM_EVENT, &i); 439 } 440 441 end: 442 if (results.Pointer != NULL) 443 AcpiOsFree(results.Pointer); 444 445 return (status); 446 } 447 448 /* 449 * Perform a few actions based on the keypress. Users can extend this 450 * functionality by reading the keystrokes we send to devd(8). 451 */ 452 static void 453 hci_key_action(struct acpi_toshiba_softc *sc, ACPI_HANDLE h, UINT32 key) 454 { 455 UINT32 arg; 456 457 switch (key) { 458 case FN_F6_RELEASE: 459 /* Decrease LCD brightness. */ 460 hci_lcd_brightness(h, HCI_GET, &arg); 461 if (arg-- == 0) 462 arg = 0; 463 else 464 hci_lcd_brightness(h, HCI_SET, &arg); 465 break; 466 case FN_F7_RELEASE: 467 /* Increase LCD brightness. */ 468 hci_lcd_brightness(h, HCI_GET, &arg); 469 if (arg++ == 7) 470 arg = 7; 471 else 472 hci_lcd_brightness(h, HCI_SET, &arg); 473 break; 474 case FN_F5_RELEASE: 475 /* Cycle through video outputs. */ 476 hci_video_output(h, HCI_GET, &arg); 477 arg = (arg + 1) % 7; 478 hci_video_output(sc->video_handle, HCI_SET, &arg); 479 break; 480 case FN_F8_RELEASE: 481 /* Toggle LCD backlight. */ 482 hci_lcd_backlight(h, HCI_GET, &arg); 483 arg = (arg != 0) ? 0 : 1; 484 hci_lcd_backlight(h, HCI_SET, &arg); 485 break; 486 case FN_ESC_RELEASE: 487 /* Toggle forcing fan on. */ 488 hci_force_fan(h, HCI_GET, &arg); 489 arg = (arg != 0) ? 0 : 1; 490 hci_force_fan(h, HCI_SET, &arg); 491 break; 492 } 493 } 494 495 static void 496 acpi_toshiba_notify(ACPI_HANDLE h, UINT32 notify, void *context) 497 { 498 struct acpi_toshiba_softc *sc; 499 UINT32 key; 500 501 sc = (struct acpi_toshiba_softc *)context; 502 503 if (notify == 0x80) { 504 while (hci_call(h, HCI_GET, HCI_REG_SYSTEM_EVENT, &key) == 0) { 505 hci_key_action(sc, h, key); 506 acpi_UserNotify("TOSHIBA", h, (uint8_t)key); 507 } 508 } else 509 device_printf(sc->dev, "unknown notify: 0x%x\n", notify); 510 } 511 512 /* 513 * Toshiba video pseudo-device to provide the DSSX method. 514 * 515 * HID Model 516 * ------------------------------------- 517 * TOS6201 Libretto L Series 518 */ 519 static int 520 acpi_toshiba_video_probe(device_t dev) 521 { 522 static char *vid_ids[] = { "TOS6201", NULL }; 523 524 if (acpi_disabled("toshiba") || 525 ACPI_ID_PROBE(device_get_parent(dev), dev, vid_ids) == NULL || 526 device_get_unit(dev) != 0) 527 return (ENXIO); 528 529 device_quiet(dev); 530 device_set_desc(dev, "Toshiba Video"); 531 return (0); 532 } 533 534 static int 535 acpi_toshiba_video_attach(device_t dev) 536 { 537 struct acpi_toshiba_softc *sc; 538 539 sc = devclass_get_softc(acpi_toshiba_devclass, 0); 540 if (sc == NULL) 541 return (ENXIO); 542 sc->video_handle = acpi_get_handle(dev); 543 return (0); 544 } 545