1 /* 2 * toshiba_acpi.c - Toshiba Laptop ACPI Extras 3 * 4 * 5 * Copyright (C) 2002-2004 John Belmonte 6 * Copyright (C) 2008 Philip Langdale 7 * Copyright (C) 2010 Pierre Ducroquet 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 * 23 * 24 * The devolpment page for this driver is located at 25 * http://memebeam.org/toys/ToshibaAcpiDriver. 26 * 27 * Credits: 28 * Jonathan A. Buzzard - Toshiba HCI info, and critical tips on reverse 29 * engineering the Windows drivers 30 * Yasushi Nagato - changes for linux kernel 2.4 -> 2.5 31 * Rob Miller - TV out and hotkeys help 32 * 33 * 34 * TODO 35 * 36 */ 37 38 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 39 40 #define TOSHIBA_ACPI_VERSION "0.19" 41 #define PROC_INTERFACE_VERSION 1 42 43 #include <linux/kernel.h> 44 #include <linux/module.h> 45 #include <linux/init.h> 46 #include <linux/types.h> 47 #include <linux/proc_fs.h> 48 #include <linux/seq_file.h> 49 #include <linux/backlight.h> 50 #include <linux/rfkill.h> 51 #include <linux/input.h> 52 #include <linux/input/sparse-keymap.h> 53 #include <linux/leds.h> 54 #include <linux/slab.h> 55 #include <linux/workqueue.h> 56 #include <linux/i8042.h> 57 58 #include <asm/uaccess.h> 59 60 #include <acpi/acpi_drivers.h> 61 62 MODULE_AUTHOR("John Belmonte"); 63 MODULE_DESCRIPTION("Toshiba Laptop ACPI Extras Driver"); 64 MODULE_LICENSE("GPL"); 65 66 #define TOSHIBA_WMI_EVENT_GUID "59142400-C6A3-40FA-BADB-8A2652834100" 67 68 /* Scan code for Fn key on TOS1900 models */ 69 #define TOS1900_FN_SCAN 0x6e 70 71 /* Toshiba ACPI method paths */ 72 #define METHOD_VIDEO_OUT "\\_SB_.VALX.DSSX" 73 74 /* Toshiba HCI interface definitions 75 * 76 * HCI is Toshiba's "Hardware Control Interface" which is supposed to 77 * be uniform across all their models. Ideally we would just call 78 * dedicated ACPI methods instead of using this primitive interface. 79 * However the ACPI methods seem to be incomplete in some areas (for 80 * example they allow setting, but not reading, the LCD brightness value), 81 * so this is still useful. 82 */ 83 84 #define HCI_WORDS 6 85 86 /* operations */ 87 #define HCI_SET 0xff00 88 #define HCI_GET 0xfe00 89 90 /* return codes */ 91 #define HCI_SUCCESS 0x0000 92 #define HCI_FAILURE 0x1000 93 #define HCI_NOT_SUPPORTED 0x8000 94 #define HCI_EMPTY 0x8c00 95 96 /* registers */ 97 #define HCI_FAN 0x0004 98 #define HCI_SYSTEM_EVENT 0x0016 99 #define HCI_VIDEO_OUT 0x001c 100 #define HCI_HOTKEY_EVENT 0x001e 101 #define HCI_LCD_BRIGHTNESS 0x002a 102 #define HCI_WIRELESS 0x0056 103 104 /* field definitions */ 105 #define HCI_HOTKEY_DISABLE 0x0b 106 #define HCI_HOTKEY_ENABLE 0x09 107 #define HCI_LCD_BRIGHTNESS_BITS 3 108 #define HCI_LCD_BRIGHTNESS_SHIFT (16-HCI_LCD_BRIGHTNESS_BITS) 109 #define HCI_LCD_BRIGHTNESS_LEVELS (1 << HCI_LCD_BRIGHTNESS_BITS) 110 #define HCI_VIDEO_OUT_LCD 0x1 111 #define HCI_VIDEO_OUT_CRT 0x2 112 #define HCI_VIDEO_OUT_TV 0x4 113 #define HCI_WIRELESS_KILL_SWITCH 0x01 114 #define HCI_WIRELESS_BT_PRESENT 0x0f 115 #define HCI_WIRELESS_BT_ATTACH 0x40 116 #define HCI_WIRELESS_BT_POWER 0x80 117 118 struct toshiba_acpi_dev { 119 struct acpi_device *acpi_dev; 120 const char *method_hci; 121 struct rfkill *bt_rfk; 122 struct input_dev *hotkey_dev; 123 struct work_struct hotkey_work; 124 struct backlight_device *backlight_dev; 125 struct led_classdev led_dev; 126 127 int force_fan; 128 int last_key_event; 129 int key_event_valid; 130 131 unsigned int illumination_supported:1; 132 unsigned int video_supported:1; 133 unsigned int fan_supported:1; 134 unsigned int system_event_supported:1; 135 unsigned int ntfy_supported:1; 136 unsigned int info_supported:1; 137 138 struct mutex mutex; 139 }; 140 141 static struct toshiba_acpi_dev *toshiba_acpi; 142 143 static const struct acpi_device_id toshiba_device_ids[] = { 144 {"TOS6200", 0}, 145 {"TOS6208", 0}, 146 {"TOS1900", 0}, 147 {"", 0}, 148 }; 149 MODULE_DEVICE_TABLE(acpi, toshiba_device_ids); 150 151 static const struct key_entry toshiba_acpi_keymap[] __devinitconst = { 152 { KE_KEY, 0x101, { KEY_MUTE } }, 153 { KE_KEY, 0x102, { KEY_ZOOMOUT } }, 154 { KE_KEY, 0x103, { KEY_ZOOMIN } }, 155 { KE_KEY, 0x12c, { KEY_KBDILLUMTOGGLE } }, 156 { KE_KEY, 0x139, { KEY_ZOOMRESET } }, 157 { KE_KEY, 0x13b, { KEY_COFFEE } }, 158 { KE_KEY, 0x13c, { KEY_BATTERY } }, 159 { KE_KEY, 0x13d, { KEY_SLEEP } }, 160 { KE_KEY, 0x13e, { KEY_SUSPEND } }, 161 { KE_KEY, 0x13f, { KEY_SWITCHVIDEOMODE } }, 162 { KE_KEY, 0x140, { KEY_BRIGHTNESSDOWN } }, 163 { KE_KEY, 0x141, { KEY_BRIGHTNESSUP } }, 164 { KE_KEY, 0x142, { KEY_WLAN } }, 165 { KE_KEY, 0x143, { KEY_TOUCHPAD_TOGGLE } }, 166 { KE_KEY, 0x17f, { KEY_FN } }, 167 { KE_KEY, 0xb05, { KEY_PROG2 } }, 168 { KE_KEY, 0xb06, { KEY_WWW } }, 169 { KE_KEY, 0xb07, { KEY_MAIL } }, 170 { KE_KEY, 0xb30, { KEY_STOP } }, 171 { KE_KEY, 0xb31, { KEY_PREVIOUSSONG } }, 172 { KE_KEY, 0xb32, { KEY_NEXTSONG } }, 173 { KE_KEY, 0xb33, { KEY_PLAYPAUSE } }, 174 { KE_KEY, 0xb5a, { KEY_MEDIA } }, 175 { KE_IGNORE, 0x1430, { KEY_RESERVED } }, 176 { KE_END, 0 }, 177 }; 178 179 /* utility 180 */ 181 182 static __inline__ void _set_bit(u32 * word, u32 mask, int value) 183 { 184 *word = (*word & ~mask) | (mask * value); 185 } 186 187 /* acpi interface wrappers 188 */ 189 190 static int write_acpi_int(const char *methodName, int val) 191 { 192 struct acpi_object_list params; 193 union acpi_object in_objs[1]; 194 acpi_status status; 195 196 params.count = ARRAY_SIZE(in_objs); 197 params.pointer = in_objs; 198 in_objs[0].type = ACPI_TYPE_INTEGER; 199 in_objs[0].integer.value = val; 200 201 status = acpi_evaluate_object(NULL, (char *)methodName, ¶ms, NULL); 202 return (status == AE_OK) ? 0 : -EIO; 203 } 204 205 /* Perform a raw HCI call. Here we don't care about input or output buffer 206 * format. 207 */ 208 static acpi_status hci_raw(struct toshiba_acpi_dev *dev, 209 const u32 in[HCI_WORDS], u32 out[HCI_WORDS]) 210 { 211 struct acpi_object_list params; 212 union acpi_object in_objs[HCI_WORDS]; 213 struct acpi_buffer results; 214 union acpi_object out_objs[HCI_WORDS + 1]; 215 acpi_status status; 216 int i; 217 218 params.count = HCI_WORDS; 219 params.pointer = in_objs; 220 for (i = 0; i < HCI_WORDS; ++i) { 221 in_objs[i].type = ACPI_TYPE_INTEGER; 222 in_objs[i].integer.value = in[i]; 223 } 224 225 results.length = sizeof(out_objs); 226 results.pointer = out_objs; 227 228 status = acpi_evaluate_object(dev->acpi_dev->handle, 229 (char *)dev->method_hci, ¶ms, 230 &results); 231 if ((status == AE_OK) && (out_objs->package.count <= HCI_WORDS)) { 232 for (i = 0; i < out_objs->package.count; ++i) { 233 out[i] = out_objs->package.elements[i].integer.value; 234 } 235 } 236 237 return status; 238 } 239 240 /* common hci tasks (get or set one or two value) 241 * 242 * In addition to the ACPI status, the HCI system returns a result which 243 * may be useful (such as "not supported"). 244 */ 245 246 static acpi_status hci_write1(struct toshiba_acpi_dev *dev, u32 reg, 247 u32 in1, u32 *result) 248 { 249 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, 0, 0, 0 }; 250 u32 out[HCI_WORDS]; 251 acpi_status status = hci_raw(dev, in, out); 252 *result = (status == AE_OK) ? out[0] : HCI_FAILURE; 253 return status; 254 } 255 256 static acpi_status hci_read1(struct toshiba_acpi_dev *dev, u32 reg, 257 u32 *out1, u32 *result) 258 { 259 u32 in[HCI_WORDS] = { HCI_GET, reg, 0, 0, 0, 0 }; 260 u32 out[HCI_WORDS]; 261 acpi_status status = hci_raw(dev, in, out); 262 *out1 = out[2]; 263 *result = (status == AE_OK) ? out[0] : HCI_FAILURE; 264 return status; 265 } 266 267 static acpi_status hci_write2(struct toshiba_acpi_dev *dev, u32 reg, 268 u32 in1, u32 in2, u32 *result) 269 { 270 u32 in[HCI_WORDS] = { HCI_SET, reg, in1, in2, 0, 0 }; 271 u32 out[HCI_WORDS]; 272 acpi_status status = hci_raw(dev, in, out); 273 *result = (status == AE_OK) ? out[0] : HCI_FAILURE; 274 return status; 275 } 276 277 static acpi_status hci_read2(struct toshiba_acpi_dev *dev, u32 reg, 278 u32 *out1, u32 *out2, u32 *result) 279 { 280 u32 in[HCI_WORDS] = { HCI_GET, reg, *out1, *out2, 0, 0 }; 281 u32 out[HCI_WORDS]; 282 acpi_status status = hci_raw(dev, in, out); 283 *out1 = out[2]; 284 *out2 = out[3]; 285 *result = (status == AE_OK) ? out[0] : HCI_FAILURE; 286 return status; 287 } 288 289 /* Illumination support */ 290 static int toshiba_illumination_available(struct toshiba_acpi_dev *dev) 291 { 292 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 }; 293 u32 out[HCI_WORDS]; 294 acpi_status status; 295 296 in[0] = 0xf100; 297 status = hci_raw(dev, in, out); 298 if (ACPI_FAILURE(status)) { 299 pr_info("Illumination device not available\n"); 300 return 0; 301 } 302 in[0] = 0xf400; 303 status = hci_raw(dev, in, out); 304 return 1; 305 } 306 307 static void toshiba_illumination_set(struct led_classdev *cdev, 308 enum led_brightness brightness) 309 { 310 struct toshiba_acpi_dev *dev = container_of(cdev, 311 struct toshiba_acpi_dev, led_dev); 312 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 }; 313 u32 out[HCI_WORDS]; 314 acpi_status status; 315 316 /* First request : initialize communication. */ 317 in[0] = 0xf100; 318 status = hci_raw(dev, in, out); 319 if (ACPI_FAILURE(status)) { 320 pr_info("Illumination device not available\n"); 321 return; 322 } 323 324 if (brightness) { 325 /* Switch the illumination on */ 326 in[0] = 0xf400; 327 in[1] = 0x14e; 328 in[2] = 1; 329 status = hci_raw(dev, in, out); 330 if (ACPI_FAILURE(status)) { 331 pr_info("ACPI call for illumination failed\n"); 332 return; 333 } 334 } else { 335 /* Switch the illumination off */ 336 in[0] = 0xf400; 337 in[1] = 0x14e; 338 in[2] = 0; 339 status = hci_raw(dev, in, out); 340 if (ACPI_FAILURE(status)) { 341 pr_info("ACPI call for illumination failed.\n"); 342 return; 343 } 344 } 345 346 /* Last request : close communication. */ 347 in[0] = 0xf200; 348 in[1] = 0; 349 in[2] = 0; 350 hci_raw(dev, in, out); 351 } 352 353 static enum led_brightness toshiba_illumination_get(struct led_classdev *cdev) 354 { 355 struct toshiba_acpi_dev *dev = container_of(cdev, 356 struct toshiba_acpi_dev, led_dev); 357 u32 in[HCI_WORDS] = { 0, 0, 0, 0, 0, 0 }; 358 u32 out[HCI_WORDS]; 359 acpi_status status; 360 enum led_brightness result; 361 362 /* First request : initialize communication. */ 363 in[0] = 0xf100; 364 status = hci_raw(dev, in, out); 365 if (ACPI_FAILURE(status)) { 366 pr_info("Illumination device not available\n"); 367 return LED_OFF; 368 } 369 370 /* Check the illumination */ 371 in[0] = 0xf300; 372 in[1] = 0x14e; 373 status = hci_raw(dev, in, out); 374 if (ACPI_FAILURE(status)) { 375 pr_info("ACPI call for illumination failed.\n"); 376 return LED_OFF; 377 } 378 379 result = out[2] ? LED_FULL : LED_OFF; 380 381 /* Last request : close communication. */ 382 in[0] = 0xf200; 383 in[1] = 0; 384 in[2] = 0; 385 hci_raw(dev, in, out); 386 387 return result; 388 } 389 390 /* Bluetooth rfkill handlers */ 391 392 static u32 hci_get_bt_present(struct toshiba_acpi_dev *dev, bool *present) 393 { 394 u32 hci_result; 395 u32 value, value2; 396 397 value = 0; 398 value2 = 0; 399 hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result); 400 if (hci_result == HCI_SUCCESS) 401 *present = (value & HCI_WIRELESS_BT_PRESENT) ? true : false; 402 403 return hci_result; 404 } 405 406 static u32 hci_get_radio_state(struct toshiba_acpi_dev *dev, bool *radio_state) 407 { 408 u32 hci_result; 409 u32 value, value2; 410 411 value = 0; 412 value2 = 0x0001; 413 hci_read2(dev, HCI_WIRELESS, &value, &value2, &hci_result); 414 415 *radio_state = value & HCI_WIRELESS_KILL_SWITCH; 416 return hci_result; 417 } 418 419 static int bt_rfkill_set_block(void *data, bool blocked) 420 { 421 struct toshiba_acpi_dev *dev = data; 422 u32 result1, result2; 423 u32 value; 424 int err; 425 bool radio_state; 426 427 value = (blocked == false); 428 429 mutex_lock(&dev->mutex); 430 if (hci_get_radio_state(dev, &radio_state) != HCI_SUCCESS) { 431 err = -EIO; 432 goto out; 433 } 434 435 if (!radio_state) { 436 err = 0; 437 goto out; 438 } 439 440 hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_POWER, &result1); 441 hci_write2(dev, HCI_WIRELESS, value, HCI_WIRELESS_BT_ATTACH, &result2); 442 443 if (result1 != HCI_SUCCESS || result2 != HCI_SUCCESS) 444 err = -EIO; 445 else 446 err = 0; 447 out: 448 mutex_unlock(&dev->mutex); 449 return err; 450 } 451 452 static void bt_rfkill_poll(struct rfkill *rfkill, void *data) 453 { 454 bool new_rfk_state; 455 bool value; 456 u32 hci_result; 457 struct toshiba_acpi_dev *dev = data; 458 459 mutex_lock(&dev->mutex); 460 461 hci_result = hci_get_radio_state(dev, &value); 462 if (hci_result != HCI_SUCCESS) { 463 /* Can't do anything useful */ 464 mutex_unlock(&dev->mutex); 465 return; 466 } 467 468 new_rfk_state = value; 469 470 mutex_unlock(&dev->mutex); 471 472 if (rfkill_set_hw_state(rfkill, !new_rfk_state)) 473 bt_rfkill_set_block(data, true); 474 } 475 476 static const struct rfkill_ops toshiba_rfk_ops = { 477 .set_block = bt_rfkill_set_block, 478 .poll = bt_rfkill_poll, 479 }; 480 481 static struct proc_dir_entry *toshiba_proc_dir /*= 0*/ ; 482 483 static int get_lcd(struct backlight_device *bd) 484 { 485 struct toshiba_acpi_dev *dev = bl_get_data(bd); 486 u32 hci_result; 487 u32 value; 488 489 hci_read1(dev, HCI_LCD_BRIGHTNESS, &value, &hci_result); 490 if (hci_result == HCI_SUCCESS) 491 return (value >> HCI_LCD_BRIGHTNESS_SHIFT); 492 493 return -EIO; 494 } 495 496 static int lcd_proc_show(struct seq_file *m, void *v) 497 { 498 struct toshiba_acpi_dev *dev = m->private; 499 int value; 500 501 if (!dev->backlight_dev) 502 return -ENODEV; 503 504 value = get_lcd(dev->backlight_dev); 505 if (value >= 0) { 506 seq_printf(m, "brightness: %d\n", value); 507 seq_printf(m, "brightness_levels: %d\n", 508 HCI_LCD_BRIGHTNESS_LEVELS); 509 return 0; 510 } 511 512 pr_err("Error reading LCD brightness\n"); 513 return -EIO; 514 } 515 516 static int lcd_proc_open(struct inode *inode, struct file *file) 517 { 518 return single_open(file, lcd_proc_show, PDE(inode)->data); 519 } 520 521 static int set_lcd(struct toshiba_acpi_dev *dev, int value) 522 { 523 u32 hci_result; 524 525 value = value << HCI_LCD_BRIGHTNESS_SHIFT; 526 hci_write1(dev, HCI_LCD_BRIGHTNESS, value, &hci_result); 527 return hci_result == HCI_SUCCESS ? 0 : -EIO; 528 } 529 530 static int set_lcd_status(struct backlight_device *bd) 531 { 532 struct toshiba_acpi_dev *dev = bl_get_data(bd); 533 return set_lcd(dev, bd->props.brightness); 534 } 535 536 static ssize_t lcd_proc_write(struct file *file, const char __user *buf, 537 size_t count, loff_t *pos) 538 { 539 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data; 540 char cmd[42]; 541 size_t len; 542 int value; 543 int ret; 544 545 len = min(count, sizeof(cmd) - 1); 546 if (copy_from_user(cmd, buf, len)) 547 return -EFAULT; 548 cmd[len] = '\0'; 549 550 if (sscanf(cmd, " brightness : %i", &value) == 1 && 551 value >= 0 && value < HCI_LCD_BRIGHTNESS_LEVELS) { 552 ret = set_lcd(dev, value); 553 if (ret == 0) 554 ret = count; 555 } else { 556 ret = -EINVAL; 557 } 558 return ret; 559 } 560 561 static const struct file_operations lcd_proc_fops = { 562 .owner = THIS_MODULE, 563 .open = lcd_proc_open, 564 .read = seq_read, 565 .llseek = seq_lseek, 566 .release = single_release, 567 .write = lcd_proc_write, 568 }; 569 570 static int get_video_status(struct toshiba_acpi_dev *dev, u32 *status) 571 { 572 u32 hci_result; 573 574 hci_read1(dev, HCI_VIDEO_OUT, status, &hci_result); 575 return hci_result == HCI_SUCCESS ? 0 : -EIO; 576 } 577 578 static int video_proc_show(struct seq_file *m, void *v) 579 { 580 struct toshiba_acpi_dev *dev = m->private; 581 u32 value; 582 int ret; 583 584 ret = get_video_status(dev, &value); 585 if (!ret) { 586 int is_lcd = (value & HCI_VIDEO_OUT_LCD) ? 1 : 0; 587 int is_crt = (value & HCI_VIDEO_OUT_CRT) ? 1 : 0; 588 int is_tv = (value & HCI_VIDEO_OUT_TV) ? 1 : 0; 589 seq_printf(m, "lcd_out: %d\n", is_lcd); 590 seq_printf(m, "crt_out: %d\n", is_crt); 591 seq_printf(m, "tv_out: %d\n", is_tv); 592 } 593 594 return ret; 595 } 596 597 static int video_proc_open(struct inode *inode, struct file *file) 598 { 599 return single_open(file, video_proc_show, PDE(inode)->data); 600 } 601 602 static ssize_t video_proc_write(struct file *file, const char __user *buf, 603 size_t count, loff_t *pos) 604 { 605 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data; 606 char *cmd, *buffer; 607 int ret; 608 int value; 609 int remain = count; 610 int lcd_out = -1; 611 int crt_out = -1; 612 int tv_out = -1; 613 u32 video_out; 614 615 cmd = kmalloc(count + 1, GFP_KERNEL); 616 if (!cmd) 617 return -ENOMEM; 618 if (copy_from_user(cmd, buf, count)) { 619 kfree(cmd); 620 return -EFAULT; 621 } 622 cmd[count] = '\0'; 623 624 buffer = cmd; 625 626 /* scan expression. Multiple expressions may be delimited with ; 627 * 628 * NOTE: to keep scanning simple, invalid fields are ignored 629 */ 630 while (remain) { 631 if (sscanf(buffer, " lcd_out : %i", &value) == 1) 632 lcd_out = value & 1; 633 else if (sscanf(buffer, " crt_out : %i", &value) == 1) 634 crt_out = value & 1; 635 else if (sscanf(buffer, " tv_out : %i", &value) == 1) 636 tv_out = value & 1; 637 /* advance to one character past the next ; */ 638 do { 639 ++buffer; 640 --remain; 641 } 642 while (remain && *(buffer - 1) != ';'); 643 } 644 645 kfree(cmd); 646 647 ret = get_video_status(dev, &video_out); 648 if (!ret) { 649 unsigned int new_video_out = video_out; 650 if (lcd_out != -1) 651 _set_bit(&new_video_out, HCI_VIDEO_OUT_LCD, lcd_out); 652 if (crt_out != -1) 653 _set_bit(&new_video_out, HCI_VIDEO_OUT_CRT, crt_out); 654 if (tv_out != -1) 655 _set_bit(&new_video_out, HCI_VIDEO_OUT_TV, tv_out); 656 /* To avoid unnecessary video disruption, only write the new 657 * video setting if something changed. */ 658 if (new_video_out != video_out) 659 ret = write_acpi_int(METHOD_VIDEO_OUT, new_video_out); 660 } 661 662 return ret ? ret : count; 663 } 664 665 static const struct file_operations video_proc_fops = { 666 .owner = THIS_MODULE, 667 .open = video_proc_open, 668 .read = seq_read, 669 .llseek = seq_lseek, 670 .release = single_release, 671 .write = video_proc_write, 672 }; 673 674 static int get_fan_status(struct toshiba_acpi_dev *dev, u32 *status) 675 { 676 u32 hci_result; 677 678 hci_read1(dev, HCI_FAN, status, &hci_result); 679 return hci_result == HCI_SUCCESS ? 0 : -EIO; 680 } 681 682 static int fan_proc_show(struct seq_file *m, void *v) 683 { 684 struct toshiba_acpi_dev *dev = m->private; 685 int ret; 686 u32 value; 687 688 ret = get_fan_status(dev, &value); 689 if (!ret) { 690 seq_printf(m, "running: %d\n", (value > 0)); 691 seq_printf(m, "force_on: %d\n", dev->force_fan); 692 } 693 694 return ret; 695 } 696 697 static int fan_proc_open(struct inode *inode, struct file *file) 698 { 699 return single_open(file, fan_proc_show, PDE(inode)->data); 700 } 701 702 static ssize_t fan_proc_write(struct file *file, const char __user *buf, 703 size_t count, loff_t *pos) 704 { 705 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data; 706 char cmd[42]; 707 size_t len; 708 int value; 709 u32 hci_result; 710 711 len = min(count, sizeof(cmd) - 1); 712 if (copy_from_user(cmd, buf, len)) 713 return -EFAULT; 714 cmd[len] = '\0'; 715 716 if (sscanf(cmd, " force_on : %i", &value) == 1 && 717 value >= 0 && value <= 1) { 718 hci_write1(dev, HCI_FAN, value, &hci_result); 719 if (hci_result != HCI_SUCCESS) 720 return -EIO; 721 else 722 dev->force_fan = value; 723 } else { 724 return -EINVAL; 725 } 726 727 return count; 728 } 729 730 static const struct file_operations fan_proc_fops = { 731 .owner = THIS_MODULE, 732 .open = fan_proc_open, 733 .read = seq_read, 734 .llseek = seq_lseek, 735 .release = single_release, 736 .write = fan_proc_write, 737 }; 738 739 static int keys_proc_show(struct seq_file *m, void *v) 740 { 741 struct toshiba_acpi_dev *dev = m->private; 742 u32 hci_result; 743 u32 value; 744 745 if (!dev->key_event_valid && dev->system_event_supported) { 746 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result); 747 if (hci_result == HCI_SUCCESS) { 748 dev->key_event_valid = 1; 749 dev->last_key_event = value; 750 } else if (hci_result == HCI_EMPTY) { 751 /* better luck next time */ 752 } else if (hci_result == HCI_NOT_SUPPORTED) { 753 /* This is a workaround for an unresolved issue on 754 * some machines where system events sporadically 755 * become disabled. */ 756 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result); 757 pr_notice("Re-enabled hotkeys\n"); 758 } else { 759 pr_err("Error reading hotkey status\n"); 760 return -EIO; 761 } 762 } 763 764 seq_printf(m, "hotkey_ready: %d\n", dev->key_event_valid); 765 seq_printf(m, "hotkey: 0x%04x\n", dev->last_key_event); 766 return 0; 767 } 768 769 static int keys_proc_open(struct inode *inode, struct file *file) 770 { 771 return single_open(file, keys_proc_show, PDE(inode)->data); 772 } 773 774 static ssize_t keys_proc_write(struct file *file, const char __user *buf, 775 size_t count, loff_t *pos) 776 { 777 struct toshiba_acpi_dev *dev = PDE(file->f_path.dentry->d_inode)->data; 778 char cmd[42]; 779 size_t len; 780 int value; 781 782 len = min(count, sizeof(cmd) - 1); 783 if (copy_from_user(cmd, buf, len)) 784 return -EFAULT; 785 cmd[len] = '\0'; 786 787 if (sscanf(cmd, " hotkey_ready : %i", &value) == 1 && value == 0) { 788 dev->key_event_valid = 0; 789 } else { 790 return -EINVAL; 791 } 792 793 return count; 794 } 795 796 static const struct file_operations keys_proc_fops = { 797 .owner = THIS_MODULE, 798 .open = keys_proc_open, 799 .read = seq_read, 800 .llseek = seq_lseek, 801 .release = single_release, 802 .write = keys_proc_write, 803 }; 804 805 static int version_proc_show(struct seq_file *m, void *v) 806 { 807 seq_printf(m, "driver: %s\n", TOSHIBA_ACPI_VERSION); 808 seq_printf(m, "proc_interface: %d\n", PROC_INTERFACE_VERSION); 809 return 0; 810 } 811 812 static int version_proc_open(struct inode *inode, struct file *file) 813 { 814 return single_open(file, version_proc_show, PDE(inode)->data); 815 } 816 817 static const struct file_operations version_proc_fops = { 818 .owner = THIS_MODULE, 819 .open = version_proc_open, 820 .read = seq_read, 821 .llseek = seq_lseek, 822 .release = single_release, 823 }; 824 825 /* proc and module init 826 */ 827 828 #define PROC_TOSHIBA "toshiba" 829 830 static void __devinit 831 create_toshiba_proc_entries(struct toshiba_acpi_dev *dev) 832 { 833 if (dev->backlight_dev) 834 proc_create_data("lcd", S_IRUGO | S_IWUSR, toshiba_proc_dir, 835 &lcd_proc_fops, dev); 836 if (dev->video_supported) 837 proc_create_data("video", S_IRUGO | S_IWUSR, toshiba_proc_dir, 838 &video_proc_fops, dev); 839 if (dev->fan_supported) 840 proc_create_data("fan", S_IRUGO | S_IWUSR, toshiba_proc_dir, 841 &fan_proc_fops, dev); 842 if (dev->hotkey_dev) 843 proc_create_data("keys", S_IRUGO | S_IWUSR, toshiba_proc_dir, 844 &keys_proc_fops, dev); 845 proc_create_data("version", S_IRUGO, toshiba_proc_dir, 846 &version_proc_fops, dev); 847 } 848 849 static void remove_toshiba_proc_entries(struct toshiba_acpi_dev *dev) 850 { 851 if (dev->backlight_dev) 852 remove_proc_entry("lcd", toshiba_proc_dir); 853 if (dev->video_supported) 854 remove_proc_entry("video", toshiba_proc_dir); 855 if (dev->fan_supported) 856 remove_proc_entry("fan", toshiba_proc_dir); 857 if (dev->hotkey_dev) 858 remove_proc_entry("keys", toshiba_proc_dir); 859 remove_proc_entry("version", toshiba_proc_dir); 860 } 861 862 static const struct backlight_ops toshiba_backlight_data = { 863 .get_brightness = get_lcd, 864 .update_status = set_lcd_status, 865 }; 866 867 static bool toshiba_acpi_i8042_filter(unsigned char data, unsigned char str, 868 struct serio *port) 869 { 870 if (str & 0x20) 871 return false; 872 873 if (unlikely(data == 0xe0)) 874 return false; 875 876 if ((data & 0x7f) == TOS1900_FN_SCAN) { 877 schedule_work(&toshiba_acpi->hotkey_work); 878 return true; 879 } 880 881 return false; 882 } 883 884 static void toshiba_acpi_hotkey_work(struct work_struct *work) 885 { 886 acpi_handle ec_handle = ec_get_handle(); 887 acpi_status status; 888 889 if (!ec_handle) 890 return; 891 892 status = acpi_evaluate_object(ec_handle, "NTFY", NULL, NULL); 893 if (ACPI_FAILURE(status)) 894 pr_err("ACPI NTFY method execution failed\n"); 895 } 896 897 /* 898 * Returns hotkey scancode, or < 0 on failure. 899 */ 900 static int toshiba_acpi_query_hotkey(struct toshiba_acpi_dev *dev) 901 { 902 struct acpi_buffer buf; 903 union acpi_object out_obj; 904 acpi_status status; 905 906 buf.pointer = &out_obj; 907 buf.length = sizeof(out_obj); 908 909 status = acpi_evaluate_object(dev->acpi_dev->handle, "INFO", 910 NULL, &buf); 911 if (ACPI_FAILURE(status) || out_obj.type != ACPI_TYPE_INTEGER) { 912 pr_err("ACPI INFO method execution failed\n"); 913 return -EIO; 914 } 915 916 return out_obj.integer.value; 917 } 918 919 static void toshiba_acpi_report_hotkey(struct toshiba_acpi_dev *dev, 920 int scancode) 921 { 922 if (scancode == 0x100) 923 return; 924 925 /* act on key press; ignore key release */ 926 if (scancode & 0x80) 927 return; 928 929 if (!sparse_keymap_report_event(dev->hotkey_dev, scancode, 1, true)) 930 pr_info("Unknown key %x\n", scancode); 931 } 932 933 static int __devinit toshiba_acpi_setup_keyboard(struct toshiba_acpi_dev *dev) 934 { 935 acpi_status status; 936 acpi_handle ec_handle, handle; 937 int error; 938 u32 hci_result; 939 940 dev->hotkey_dev = input_allocate_device(); 941 if (!dev->hotkey_dev) { 942 pr_info("Unable to register input device\n"); 943 return -ENOMEM; 944 } 945 946 dev->hotkey_dev->name = "Toshiba input device"; 947 dev->hotkey_dev->phys = "toshiba_acpi/input0"; 948 dev->hotkey_dev->id.bustype = BUS_HOST; 949 950 error = sparse_keymap_setup(dev->hotkey_dev, toshiba_acpi_keymap, NULL); 951 if (error) 952 goto err_free_dev; 953 954 /* 955 * For some machines the SCI responsible for providing hotkey 956 * notification doesn't fire. We can trigger the notification 957 * whenever the Fn key is pressed using the NTFY method, if 958 * supported, so if it's present set up an i8042 key filter 959 * for this purpose. 960 */ 961 status = AE_ERROR; 962 ec_handle = ec_get_handle(); 963 if (ec_handle) 964 status = acpi_get_handle(ec_handle, "NTFY", &handle); 965 966 if (ACPI_SUCCESS(status)) { 967 INIT_WORK(&dev->hotkey_work, toshiba_acpi_hotkey_work); 968 969 error = i8042_install_filter(toshiba_acpi_i8042_filter); 970 if (error) { 971 pr_err("Error installing key filter\n"); 972 goto err_free_keymap; 973 } 974 975 dev->ntfy_supported = 1; 976 } 977 978 /* 979 * Determine hotkey query interface. Prefer using the INFO 980 * method when it is available. 981 */ 982 status = acpi_get_handle(dev->acpi_dev->handle, "INFO", &handle); 983 if (ACPI_SUCCESS(status)) { 984 dev->info_supported = 1; 985 } else { 986 hci_write1(dev, HCI_SYSTEM_EVENT, 1, &hci_result); 987 if (hci_result == HCI_SUCCESS) 988 dev->system_event_supported = 1; 989 } 990 991 if (!dev->info_supported && !dev->system_event_supported) { 992 pr_warn("No hotkey query interface found\n"); 993 goto err_remove_filter; 994 } 995 996 status = acpi_evaluate_object(dev->acpi_dev->handle, "ENAB", NULL, NULL); 997 if (ACPI_FAILURE(status)) { 998 pr_info("Unable to enable hotkeys\n"); 999 error = -ENODEV; 1000 goto err_remove_filter; 1001 } 1002 1003 error = input_register_device(dev->hotkey_dev); 1004 if (error) { 1005 pr_info("Unable to register input device\n"); 1006 goto err_remove_filter; 1007 } 1008 1009 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE, &hci_result); 1010 return 0; 1011 1012 err_remove_filter: 1013 if (dev->ntfy_supported) 1014 i8042_remove_filter(toshiba_acpi_i8042_filter); 1015 err_free_keymap: 1016 sparse_keymap_free(dev->hotkey_dev); 1017 err_free_dev: 1018 input_free_device(dev->hotkey_dev); 1019 dev->hotkey_dev = NULL; 1020 return error; 1021 } 1022 1023 static int toshiba_acpi_remove(struct acpi_device *acpi_dev, int type) 1024 { 1025 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev); 1026 1027 remove_toshiba_proc_entries(dev); 1028 1029 if (dev->ntfy_supported) { 1030 i8042_remove_filter(toshiba_acpi_i8042_filter); 1031 cancel_work_sync(&dev->hotkey_work); 1032 } 1033 1034 if (dev->hotkey_dev) { 1035 input_unregister_device(dev->hotkey_dev); 1036 sparse_keymap_free(dev->hotkey_dev); 1037 } 1038 1039 if (dev->bt_rfk) { 1040 rfkill_unregister(dev->bt_rfk); 1041 rfkill_destroy(dev->bt_rfk); 1042 } 1043 1044 if (dev->backlight_dev) 1045 backlight_device_unregister(dev->backlight_dev); 1046 1047 if (dev->illumination_supported) 1048 led_classdev_unregister(&dev->led_dev); 1049 1050 if (toshiba_acpi) 1051 toshiba_acpi = NULL; 1052 1053 kfree(dev); 1054 1055 return 0; 1056 } 1057 1058 static const char * __devinit find_hci_method(acpi_handle handle) 1059 { 1060 acpi_status status; 1061 acpi_handle hci_handle; 1062 1063 status = acpi_get_handle(handle, "GHCI", &hci_handle); 1064 if (ACPI_SUCCESS(status)) 1065 return "GHCI"; 1066 1067 status = acpi_get_handle(handle, "SPFC", &hci_handle); 1068 if (ACPI_SUCCESS(status)) 1069 return "SPFC"; 1070 1071 return NULL; 1072 } 1073 1074 static int __devinit toshiba_acpi_add(struct acpi_device *acpi_dev) 1075 { 1076 struct toshiba_acpi_dev *dev; 1077 const char *hci_method; 1078 u32 dummy; 1079 bool bt_present; 1080 int ret = 0; 1081 struct backlight_properties props; 1082 1083 if (toshiba_acpi) 1084 return -EBUSY; 1085 1086 pr_info("Toshiba Laptop ACPI Extras version %s\n", 1087 TOSHIBA_ACPI_VERSION); 1088 1089 hci_method = find_hci_method(acpi_dev->handle); 1090 if (!hci_method) { 1091 pr_err("HCI interface not found\n"); 1092 return -ENODEV; 1093 } 1094 1095 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1096 if (!dev) 1097 return -ENOMEM; 1098 dev->acpi_dev = acpi_dev; 1099 dev->method_hci = hci_method; 1100 acpi_dev->driver_data = dev; 1101 1102 if (toshiba_acpi_setup_keyboard(dev)) 1103 pr_info("Unable to activate hotkeys\n"); 1104 1105 mutex_init(&dev->mutex); 1106 1107 props.type = BACKLIGHT_PLATFORM; 1108 props.max_brightness = HCI_LCD_BRIGHTNESS_LEVELS - 1; 1109 dev->backlight_dev = backlight_device_register("toshiba", 1110 &acpi_dev->dev, 1111 dev, 1112 &toshiba_backlight_data, 1113 &props); 1114 if (IS_ERR(dev->backlight_dev)) { 1115 ret = PTR_ERR(dev->backlight_dev); 1116 1117 pr_err("Could not register toshiba backlight device\n"); 1118 dev->backlight_dev = NULL; 1119 goto error; 1120 } 1121 dev->backlight_dev->props.brightness = get_lcd(dev->backlight_dev); 1122 1123 /* Register rfkill switch for Bluetooth */ 1124 if (hci_get_bt_present(dev, &bt_present) == HCI_SUCCESS && bt_present) { 1125 dev->bt_rfk = rfkill_alloc("Toshiba Bluetooth", 1126 &acpi_dev->dev, 1127 RFKILL_TYPE_BLUETOOTH, 1128 &toshiba_rfk_ops, 1129 dev); 1130 if (!dev->bt_rfk) { 1131 pr_err("unable to allocate rfkill device\n"); 1132 ret = -ENOMEM; 1133 goto error; 1134 } 1135 1136 ret = rfkill_register(dev->bt_rfk); 1137 if (ret) { 1138 pr_err("unable to register rfkill device\n"); 1139 rfkill_destroy(dev->bt_rfk); 1140 goto error; 1141 } 1142 } 1143 1144 if (toshiba_illumination_available(dev)) { 1145 dev->led_dev.name = "toshiba::illumination"; 1146 dev->led_dev.max_brightness = 1; 1147 dev->led_dev.brightness_set = toshiba_illumination_set; 1148 dev->led_dev.brightness_get = toshiba_illumination_get; 1149 if (!led_classdev_register(&acpi_dev->dev, &dev->led_dev)) 1150 dev->illumination_supported = 1; 1151 } 1152 1153 /* Determine whether or not BIOS supports fan and video interfaces */ 1154 1155 ret = get_video_status(dev, &dummy); 1156 dev->video_supported = !ret; 1157 1158 ret = get_fan_status(dev, &dummy); 1159 dev->fan_supported = !ret; 1160 1161 create_toshiba_proc_entries(dev); 1162 1163 toshiba_acpi = dev; 1164 1165 return 0; 1166 1167 error: 1168 toshiba_acpi_remove(acpi_dev, 0); 1169 return ret; 1170 } 1171 1172 static void toshiba_acpi_notify(struct acpi_device *acpi_dev, u32 event) 1173 { 1174 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev); 1175 u32 hci_result, value; 1176 int retries = 3; 1177 int scancode; 1178 1179 if (event != 0x80) 1180 return; 1181 1182 if (dev->info_supported) { 1183 scancode = toshiba_acpi_query_hotkey(dev); 1184 if (scancode < 0) 1185 pr_err("Failed to query hotkey event\n"); 1186 else if (scancode != 0) 1187 toshiba_acpi_report_hotkey(dev, scancode); 1188 } else if (dev->system_event_supported) { 1189 do { 1190 hci_read1(dev, HCI_SYSTEM_EVENT, &value, &hci_result); 1191 switch (hci_result) { 1192 case HCI_SUCCESS: 1193 toshiba_acpi_report_hotkey(dev, (int)value); 1194 break; 1195 case HCI_NOT_SUPPORTED: 1196 /* 1197 * This is a workaround for an unresolved 1198 * issue on some machines where system events 1199 * sporadically become disabled. 1200 */ 1201 hci_write1(dev, HCI_SYSTEM_EVENT, 1, 1202 &hci_result); 1203 pr_notice("Re-enabled hotkeys\n"); 1204 /* fall through */ 1205 default: 1206 retries--; 1207 break; 1208 } 1209 } while (retries && hci_result != HCI_EMPTY); 1210 } 1211 } 1212 1213 static int toshiba_acpi_suspend(struct acpi_device *acpi_dev, 1214 pm_message_t state) 1215 { 1216 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev); 1217 u32 result; 1218 1219 if (dev->hotkey_dev) 1220 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_DISABLE, &result); 1221 1222 return 0; 1223 } 1224 1225 static int toshiba_acpi_resume(struct acpi_device *acpi_dev) 1226 { 1227 struct toshiba_acpi_dev *dev = acpi_driver_data(acpi_dev); 1228 u32 result; 1229 1230 if (dev->hotkey_dev) 1231 hci_write1(dev, HCI_HOTKEY_EVENT, HCI_HOTKEY_ENABLE, &result); 1232 1233 return 0; 1234 } 1235 1236 static struct acpi_driver toshiba_acpi_driver = { 1237 .name = "Toshiba ACPI driver", 1238 .owner = THIS_MODULE, 1239 .ids = toshiba_device_ids, 1240 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS, 1241 .ops = { 1242 .add = toshiba_acpi_add, 1243 .remove = toshiba_acpi_remove, 1244 .notify = toshiba_acpi_notify, 1245 .suspend = toshiba_acpi_suspend, 1246 .resume = toshiba_acpi_resume, 1247 }, 1248 }; 1249 1250 static int __init toshiba_acpi_init(void) 1251 { 1252 int ret; 1253 1254 /* 1255 * Machines with this WMI guid aren't supported due to bugs in 1256 * their AML. This check relies on wmi initializing before 1257 * toshiba_acpi to guarantee guids have been identified. 1258 */ 1259 if (wmi_has_guid(TOSHIBA_WMI_EVENT_GUID)) 1260 return -ENODEV; 1261 1262 toshiba_proc_dir = proc_mkdir(PROC_TOSHIBA, acpi_root_dir); 1263 if (!toshiba_proc_dir) { 1264 pr_err("Unable to create proc dir " PROC_TOSHIBA "\n"); 1265 return -ENODEV; 1266 } 1267 1268 ret = acpi_bus_register_driver(&toshiba_acpi_driver); 1269 if (ret) { 1270 pr_err("Failed to register ACPI driver: %d\n", ret); 1271 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir); 1272 } 1273 1274 return ret; 1275 } 1276 1277 static void __exit toshiba_acpi_exit(void) 1278 { 1279 acpi_bus_unregister_driver(&toshiba_acpi_driver); 1280 if (toshiba_proc_dir) 1281 remove_proc_entry(PROC_TOSHIBA, acpi_root_dir); 1282 } 1283 1284 module_init(toshiba_acpi_init); 1285 module_exit(toshiba_acpi_exit); 1286