thinkpad_acpi.c revision e675abafcc0df38125e6e94a9ba91c92fe774f52
1/* 2 * thinkpad_acpi.c - ThinkPad ACPI Extras 3 * 4 * 5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net> 6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 21 * 02110-1301, USA. 22 */ 23 24#define TPACPI_VERSION "0.23" 25#define TPACPI_SYSFS_VERSION 0x020400 26 27/* 28 * Changelog: 29 * 2007-10-20 changelog trimmed down 30 * 31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to 32 * drivers/misc. 33 * 34 * 2006-11-22 0.13 new maintainer 35 * changelog now lives in git commit history, and will 36 * not be updated further in-file. 37 * 38 * 2005-03-17 0.11 support for 600e, 770x 39 * thanks to Jamie Lentin <lentinj@dial.pipex.com> 40 * 41 * 2005-01-16 0.9 use MODULE_VERSION 42 * thanks to Henrik Brix Andersen <brix@gentoo.org> 43 * fix parameter passing on module loading 44 * thanks to Rusty Russell <rusty@rustcorp.com.au> 45 * thanks to Jim Radford <radford@blackbean.org> 46 * 2004-11-08 0.8 fix init error case, don't return from a macro 47 * thanks to Chris Wright <chrisw@osdl.org> 48 */ 49 50#include <linux/kernel.h> 51#include <linux/module.h> 52#include <linux/init.h> 53#include <linux/types.h> 54#include <linux/string.h> 55#include <linux/list.h> 56#include <linux/mutex.h> 57#include <linux/sched.h> 58#include <linux/kthread.h> 59#include <linux/freezer.h> 60#include <linux/delay.h> 61 62#include <linux/nvram.h> 63#include <linux/proc_fs.h> 64#include <linux/sysfs.h> 65#include <linux/backlight.h> 66#include <linux/fb.h> 67#include <linux/platform_device.h> 68#include <linux/hwmon.h> 69#include <linux/hwmon-sysfs.h> 70#include <linux/input.h> 71#include <linux/leds.h> 72#include <linux/rfkill.h> 73#include <asm/uaccess.h> 74 75#include <linux/dmi.h> 76#include <linux/jiffies.h> 77#include <linux/workqueue.h> 78 79#include <acpi/acpi_drivers.h> 80 81#include <linux/pci_ids.h> 82 83 84/* ThinkPad CMOS commands */ 85#define TP_CMOS_VOLUME_DOWN 0 86#define TP_CMOS_VOLUME_UP 1 87#define TP_CMOS_VOLUME_MUTE 2 88#define TP_CMOS_BRIGHTNESS_UP 4 89#define TP_CMOS_BRIGHTNESS_DOWN 5 90#define TP_CMOS_THINKLIGHT_ON 12 91#define TP_CMOS_THINKLIGHT_OFF 13 92 93/* NVRAM Addresses */ 94enum tp_nvram_addr { 95 TP_NVRAM_ADDR_HK2 = 0x57, 96 TP_NVRAM_ADDR_THINKLIGHT = 0x58, 97 TP_NVRAM_ADDR_VIDEO = 0x59, 98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e, 99 TP_NVRAM_ADDR_MIXER = 0x60, 100}; 101 102/* NVRAM bit masks */ 103enum { 104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08, 105 TP_NVRAM_MASK_HKT_ZOOM = 0x20, 106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40, 107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80, 108 TP_NVRAM_MASK_THINKLIGHT = 0x10, 109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30, 110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20, 111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f, 112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0, 113 TP_NVRAM_MASK_MUTE = 0x40, 114 TP_NVRAM_MASK_HKT_VOLUME = 0x80, 115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f, 116 TP_NVRAM_POS_LEVEL_VOLUME = 0, 117}; 118 119/* ACPI HIDs */ 120#define TPACPI_ACPI_HKEY_HID "IBM0068" 121 122/* Input IDs */ 123#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */ 124#define TPACPI_HKEY_INPUT_VERSION 0x4101 125 126/* ACPI \WGSV commands */ 127enum { 128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */ 129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */ 130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */ 131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */ 132}; 133 134/* TP_ACPI_WGSV_GET_STATE bits */ 135enum { 136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */ 137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */ 138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */ 139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */ 140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */ 141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */ 142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */ 143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */ 144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */ 145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */ 146}; 147 148/**************************************************************************** 149 * Main driver 150 */ 151 152#define TPACPI_NAME "thinkpad" 153#define TPACPI_DESC "ThinkPad ACPI Extras" 154#define TPACPI_FILE TPACPI_NAME "_acpi" 155#define TPACPI_URL "http://ibm-acpi.sf.net/" 156#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net" 157 158#define TPACPI_PROC_DIR "ibm" 159#define TPACPI_ACPI_EVENT_PREFIX "ibm" 160#define TPACPI_DRVR_NAME TPACPI_FILE 161#define TPACPI_DRVR_SHORTNAME "tpacpi" 162#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon" 163 164#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd" 165#define TPACPI_WORKQUEUE_NAME "ktpacpid" 166 167#define TPACPI_MAX_ACPI_ARGS 3 168 169/* printk headers */ 170#define TPACPI_LOG TPACPI_FILE ": " 171#define TPACPI_EMERG KERN_EMERG TPACPI_LOG 172#define TPACPI_ALERT KERN_ALERT TPACPI_LOG 173#define TPACPI_CRIT KERN_CRIT TPACPI_LOG 174#define TPACPI_ERR KERN_ERR TPACPI_LOG 175#define TPACPI_WARN KERN_WARNING TPACPI_LOG 176#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG 177#define TPACPI_INFO KERN_INFO TPACPI_LOG 178#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG 179 180/* Debugging printk groups */ 181#define TPACPI_DBG_ALL 0xffff 182#define TPACPI_DBG_DISCLOSETASK 0x8000 183#define TPACPI_DBG_INIT 0x0001 184#define TPACPI_DBG_EXIT 0x0002 185#define TPACPI_DBG_RFKILL 0x0004 186#define TPACPI_DBG_HKEY 0x0008 187#define TPACPI_DBG_FAN 0x0010 188#define TPACPI_DBG_BRGHT 0x0020 189 190#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off") 191#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled") 192#define strlencmp(a, b) (strncmp((a), (b), strlen(b))) 193 194 195/**************************************************************************** 196 * Driver-wide structs and misc. variables 197 */ 198 199struct ibm_struct; 200 201struct tp_acpi_drv_struct { 202 const struct acpi_device_id *hid; 203 struct acpi_driver *driver; 204 205 void (*notify) (struct ibm_struct *, u32); 206 acpi_handle *handle; 207 u32 type; 208 struct acpi_device *device; 209}; 210 211struct ibm_struct { 212 char *name; 213 214 int (*read) (char *); 215 int (*write) (char *); 216 void (*exit) (void); 217 void (*resume) (void); 218 void (*suspend) (pm_message_t state); 219 void (*shutdown) (void); 220 221 struct list_head all_drivers; 222 223 struct tp_acpi_drv_struct *acpi; 224 225 struct { 226 u8 acpi_driver_registered:1; 227 u8 acpi_notify_installed:1; 228 u8 proc_created:1; 229 u8 init_called:1; 230 u8 experimental:1; 231 } flags; 232}; 233 234struct ibm_init_struct { 235 char param[32]; 236 237 int (*init) (struct ibm_init_struct *); 238 struct ibm_struct *data; 239}; 240 241static struct { 242 u32 bluetooth:1; 243 u32 hotkey:1; 244 u32 hotkey_mask:1; 245 u32 hotkey_wlsw:1; 246 u32 hotkey_tablet:1; 247 u32 light:1; 248 u32 light_status:1; 249 u32 bright_16levels:1; 250 u32 bright_acpimode:1; 251 u32 wan:1; 252 u32 uwb:1; 253 u32 fan_ctrl_status_undef:1; 254 u32 second_fan:1; 255 u32 beep_needs_two_args:1; 256 u32 input_device_registered:1; 257 u32 platform_drv_registered:1; 258 u32 platform_drv_attrs_registered:1; 259 u32 sensors_pdrv_registered:1; 260 u32 sensors_pdrv_attrs_registered:1; 261 u32 sensors_pdev_attrs_registered:1; 262 u32 hotkey_poll_active:1; 263} tp_features; 264 265static struct { 266 u16 hotkey_mask_ff:1; 267} tp_warned; 268 269struct thinkpad_id_data { 270 unsigned int vendor; /* ThinkPad vendor: 271 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */ 272 273 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */ 274 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */ 275 276 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */ 277 u16 ec_model; 278 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */ 279 u16 ec_release; 280 281 char *model_str; /* ThinkPad T43 */ 282 char *nummodel_str; /* 9384A9C for a 9384-A9C model */ 283}; 284static struct thinkpad_id_data thinkpad_id; 285 286static enum { 287 TPACPI_LIFE_INIT = 0, 288 TPACPI_LIFE_RUNNING, 289 TPACPI_LIFE_EXITING, 290} tpacpi_lifecycle; 291 292static int experimental; 293static u32 dbg_level; 294 295static struct workqueue_struct *tpacpi_wq; 296 297enum led_status_t { 298 TPACPI_LED_OFF = 0, 299 TPACPI_LED_ON, 300 TPACPI_LED_BLINK, 301}; 302 303/* Special LED class that can defer work */ 304struct tpacpi_led_classdev { 305 struct led_classdev led_classdev; 306 struct work_struct work; 307 enum led_status_t new_state; 308 unsigned int led; 309}; 310 311#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 312static int dbg_wlswemul; 313static int tpacpi_wlsw_emulstate; 314static int dbg_bluetoothemul; 315static int tpacpi_bluetooth_emulstate; 316static int dbg_wwanemul; 317static int tpacpi_wwan_emulstate; 318static int dbg_uwbemul; 319static int tpacpi_uwb_emulstate; 320#endif 321 322 323/************************************************************************* 324 * Debugging helpers 325 */ 326 327#define dbg_printk(a_dbg_level, format, arg...) \ 328 do { if (dbg_level & (a_dbg_level)) \ 329 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \ 330 } while (0) 331 332#ifdef CONFIG_THINKPAD_ACPI_DEBUG 333#define vdbg_printk dbg_printk 334static const char *str_supported(int is_supported); 335#else 336#define vdbg_printk(a_dbg_level, format, arg...) \ 337 do { } while (0) 338#endif 339 340static void tpacpi_log_usertask(const char * const what) 341{ 342 printk(TPACPI_DEBUG "%s: access by process with PID %d\n", 343 what, task_tgid_vnr(current)); 344} 345 346#define tpacpi_disclose_usertask(what, format, arg...) \ 347 do { \ 348 if (unlikely( \ 349 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \ 350 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \ 351 printk(TPACPI_DEBUG "%s: PID %d: " format, \ 352 what, task_tgid_vnr(current), ## arg); \ 353 } \ 354 } while (0) 355 356/* 357 * Quirk handling helpers 358 * 359 * ThinkPad IDs and versions seen in the field so far 360 * are two-characters from the set [0-9A-Z], i.e. base 36. 361 * 362 * We use values well outside that range as specials. 363 */ 364 365#define TPACPI_MATCH_ANY 0xffffU 366#define TPACPI_MATCH_UNKNOWN 0U 367 368/* TPID('1', 'Y') == 0x5931 */ 369#define TPID(__c1, __c2) (((__c2) << 8) | (__c1)) 370 371#define TPACPI_Q_IBM(__id1, __id2, __quirk) \ 372 { .vendor = PCI_VENDOR_ID_IBM, \ 373 .bios = TPID(__id1, __id2), \ 374 .ec = TPACPI_MATCH_ANY, \ 375 .quirks = (__quirk) } 376 377#define TPACPI_Q_LNV(__id1, __id2, __quirk) \ 378 { .vendor = PCI_VENDOR_ID_LENOVO, \ 379 .bios = TPID(__id1, __id2), \ 380 .ec = TPACPI_MATCH_ANY, \ 381 .quirks = (__quirk) } 382 383struct tpacpi_quirk { 384 unsigned int vendor; 385 u16 bios; 386 u16 ec; 387 unsigned long quirks; 388}; 389 390/** 391 * tpacpi_check_quirks() - search BIOS/EC version on a list 392 * @qlist: array of &struct tpacpi_quirk 393 * @qlist_size: number of elements in @qlist 394 * 395 * Iterates over a quirks list until one is found that matches the 396 * ThinkPad's vendor, BIOS and EC model. 397 * 398 * Returns 0 if nothing matches, otherwise returns the quirks field of 399 * the matching &struct tpacpi_quirk entry. 400 * 401 * The match criteria is: vendor, ec and bios much match. 402 */ 403static unsigned long __init tpacpi_check_quirks( 404 const struct tpacpi_quirk *qlist, 405 unsigned int qlist_size) 406{ 407 while (qlist_size) { 408 if ((qlist->vendor == thinkpad_id.vendor || 409 qlist->vendor == TPACPI_MATCH_ANY) && 410 (qlist->bios == thinkpad_id.bios_model || 411 qlist->bios == TPACPI_MATCH_ANY) && 412 (qlist->ec == thinkpad_id.ec_model || 413 qlist->ec == TPACPI_MATCH_ANY)) 414 return qlist->quirks; 415 416 qlist_size--; 417 qlist++; 418 } 419 return 0; 420} 421 422 423/**************************************************************************** 424 **************************************************************************** 425 * 426 * ACPI Helpers and device model 427 * 428 **************************************************************************** 429 ****************************************************************************/ 430 431/************************************************************************* 432 * ACPI basic handles 433 */ 434 435static acpi_handle root_handle; 436 437#define TPACPI_HANDLE(object, parent, paths...) \ 438 static acpi_handle object##_handle; \ 439 static acpi_handle *object##_parent = &parent##_handle; \ 440 static char *object##_path; \ 441 static char *object##_paths[] = { paths } 442 443TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */ 444 "\\_SB.PCI.ISA.EC", /* 570 */ 445 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */ 446 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */ 447 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */ 448 "\\_SB.PCI0.ICH3.EC0", /* R31 */ 449 "\\_SB.PCI0.LPC.EC", /* all others */ 450 ); 451 452TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */ 453TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */ 454 455TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */ 456 /* T4x, X31, X40 */ 457 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */ 458 "\\CMS", /* R40, R40e */ 459 ); /* all others */ 460 461TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */ 462 "^HKEY", /* R30, R31 */ 463 "HKEY", /* all others */ 464 ); /* 570 */ 465 466TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */ 467 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */ 468 "\\_SB.PCI0.VID0", /* 770e */ 469 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */ 470 "\\_SB.PCI0.AGP.VID", /* all others */ 471 ); /* R30, R31 */ 472 473 474/************************************************************************* 475 * ACPI helpers 476 */ 477 478static int acpi_evalf(acpi_handle handle, 479 void *res, char *method, char *fmt, ...) 480{ 481 char *fmt0 = fmt; 482 struct acpi_object_list params; 483 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS]; 484 struct acpi_buffer result, *resultp; 485 union acpi_object out_obj; 486 acpi_status status; 487 va_list ap; 488 char res_type; 489 int success; 490 int quiet; 491 492 if (!*fmt) { 493 printk(TPACPI_ERR "acpi_evalf() called with empty format\n"); 494 return 0; 495 } 496 497 if (*fmt == 'q') { 498 quiet = 1; 499 fmt++; 500 } else 501 quiet = 0; 502 503 res_type = *(fmt++); 504 505 params.count = 0; 506 params.pointer = &in_objs[0]; 507 508 va_start(ap, fmt); 509 while (*fmt) { 510 char c = *(fmt++); 511 switch (c) { 512 case 'd': /* int */ 513 in_objs[params.count].integer.value = va_arg(ap, int); 514 in_objs[params.count++].type = ACPI_TYPE_INTEGER; 515 break; 516 /* add more types as needed */ 517 default: 518 printk(TPACPI_ERR "acpi_evalf() called " 519 "with invalid format character '%c'\n", c); 520 return 0; 521 } 522 } 523 va_end(ap); 524 525 if (res_type != 'v') { 526 result.length = sizeof(out_obj); 527 result.pointer = &out_obj; 528 resultp = &result; 529 } else 530 resultp = NULL; 531 532 status = acpi_evaluate_object(handle, method, ¶ms, resultp); 533 534 switch (res_type) { 535 case 'd': /* int */ 536 if (res) 537 *(int *)res = out_obj.integer.value; 538 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER; 539 break; 540 case 'v': /* void */ 541 success = status == AE_OK; 542 break; 543 /* add more types as needed */ 544 default: 545 printk(TPACPI_ERR "acpi_evalf() called " 546 "with invalid format character '%c'\n", res_type); 547 return 0; 548 } 549 550 if (!success && !quiet) 551 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n", 552 method, fmt0, status); 553 554 return success; 555} 556 557static int acpi_ec_read(int i, u8 *p) 558{ 559 int v; 560 561 if (ecrd_handle) { 562 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i)) 563 return 0; 564 *p = v; 565 } else { 566 if (ec_read(i, p) < 0) 567 return 0; 568 } 569 570 return 1; 571} 572 573static int acpi_ec_write(int i, u8 v) 574{ 575 if (ecwr_handle) { 576 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v)) 577 return 0; 578 } else { 579 if (ec_write(i, v) < 0) 580 return 0; 581 } 582 583 return 1; 584} 585 586static int issue_thinkpad_cmos_command(int cmos_cmd) 587{ 588 if (!cmos_handle) 589 return -ENXIO; 590 591 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd)) 592 return -EIO; 593 594 return 0; 595} 596 597/************************************************************************* 598 * ACPI device model 599 */ 600 601#define TPACPI_ACPIHANDLE_INIT(object) \ 602 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \ 603 object##_paths, ARRAY_SIZE(object##_paths), &object##_path) 604 605static void drv_acpi_handle_init(char *name, 606 acpi_handle *handle, acpi_handle parent, 607 char **paths, int num_paths, char **path) 608{ 609 int i; 610 acpi_status status; 611 612 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n", 613 name); 614 615 for (i = 0; i < num_paths; i++) { 616 status = acpi_get_handle(parent, paths[i], handle); 617 if (ACPI_SUCCESS(status)) { 618 *path = paths[i]; 619 dbg_printk(TPACPI_DBG_INIT, 620 "Found ACPI handle %s for %s\n", 621 *path, name); 622 return; 623 } 624 } 625 626 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n", 627 name); 628 *handle = NULL; 629} 630 631static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data) 632{ 633 struct ibm_struct *ibm = data; 634 635 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING) 636 return; 637 638 if (!ibm || !ibm->acpi || !ibm->acpi->notify) 639 return; 640 641 ibm->acpi->notify(ibm, event); 642} 643 644static int __init setup_acpi_notify(struct ibm_struct *ibm) 645{ 646 acpi_status status; 647 int rc; 648 649 BUG_ON(!ibm->acpi); 650 651 if (!*ibm->acpi->handle) 652 return 0; 653 654 vdbg_printk(TPACPI_DBG_INIT, 655 "setting up ACPI notify for %s\n", ibm->name); 656 657 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device); 658 if (rc < 0) { 659 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n", 660 ibm->name, rc); 661 return -ENODEV; 662 } 663 664 ibm->acpi->device->driver_data = ibm; 665 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s", 666 TPACPI_ACPI_EVENT_PREFIX, 667 ibm->name); 668 669 status = acpi_install_notify_handler(*ibm->acpi->handle, 670 ibm->acpi->type, dispatch_acpi_notify, ibm); 671 if (ACPI_FAILURE(status)) { 672 if (status == AE_ALREADY_EXISTS) { 673 printk(TPACPI_NOTICE 674 "another device driver is already " 675 "handling %s events\n", ibm->name); 676 } else { 677 printk(TPACPI_ERR 678 "acpi_install_notify_handler(%s) failed: %d\n", 679 ibm->name, status); 680 } 681 return -ENODEV; 682 } 683 ibm->flags.acpi_notify_installed = 1; 684 return 0; 685} 686 687static int __init tpacpi_device_add(struct acpi_device *device) 688{ 689 return 0; 690} 691 692static int __init register_tpacpi_subdriver(struct ibm_struct *ibm) 693{ 694 int rc; 695 696 dbg_printk(TPACPI_DBG_INIT, 697 "registering %s as an ACPI driver\n", ibm->name); 698 699 BUG_ON(!ibm->acpi); 700 701 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL); 702 if (!ibm->acpi->driver) { 703 printk(TPACPI_ERR 704 "failed to allocate memory for ibm->acpi->driver\n"); 705 return -ENOMEM; 706 } 707 708 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name); 709 ibm->acpi->driver->ids = ibm->acpi->hid; 710 711 ibm->acpi->driver->ops.add = &tpacpi_device_add; 712 713 rc = acpi_bus_register_driver(ibm->acpi->driver); 714 if (rc < 0) { 715 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n", 716 ibm->name, rc); 717 kfree(ibm->acpi->driver); 718 ibm->acpi->driver = NULL; 719 } else if (!rc) 720 ibm->flags.acpi_driver_registered = 1; 721 722 return rc; 723} 724 725 726/**************************************************************************** 727 **************************************************************************** 728 * 729 * Procfs Helpers 730 * 731 **************************************************************************** 732 ****************************************************************************/ 733 734static int dispatch_procfs_read(char *page, char **start, off_t off, 735 int count, int *eof, void *data) 736{ 737 struct ibm_struct *ibm = data; 738 int len; 739 740 if (!ibm || !ibm->read) 741 return -EINVAL; 742 743 len = ibm->read(page); 744 if (len < 0) 745 return len; 746 747 if (len <= off + count) 748 *eof = 1; 749 *start = page + off; 750 len -= off; 751 if (len > count) 752 len = count; 753 if (len < 0) 754 len = 0; 755 756 return len; 757} 758 759static int dispatch_procfs_write(struct file *file, 760 const char __user *userbuf, 761 unsigned long count, void *data) 762{ 763 struct ibm_struct *ibm = data; 764 char *kernbuf; 765 int ret; 766 767 if (!ibm || !ibm->write) 768 return -EINVAL; 769 if (count > PAGE_SIZE - 2) 770 return -EINVAL; 771 772 kernbuf = kmalloc(count + 2, GFP_KERNEL); 773 if (!kernbuf) 774 return -ENOMEM; 775 776 if (copy_from_user(kernbuf, userbuf, count)) { 777 kfree(kernbuf); 778 return -EFAULT; 779 } 780 781 kernbuf[count] = 0; 782 strcat(kernbuf, ","); 783 ret = ibm->write(kernbuf); 784 if (ret == 0) 785 ret = count; 786 787 kfree(kernbuf); 788 789 return ret; 790} 791 792static char *next_cmd(char **cmds) 793{ 794 char *start = *cmds; 795 char *end; 796 797 while ((end = strchr(start, ',')) && end == start) 798 start = end + 1; 799 800 if (!end) 801 return NULL; 802 803 *end = 0; 804 *cmds = end + 1; 805 return start; 806} 807 808 809/**************************************************************************** 810 **************************************************************************** 811 * 812 * Device model: input, hwmon and platform 813 * 814 **************************************************************************** 815 ****************************************************************************/ 816 817static struct platform_device *tpacpi_pdev; 818static struct platform_device *tpacpi_sensors_pdev; 819static struct device *tpacpi_hwmon; 820static struct input_dev *tpacpi_inputdev; 821static struct mutex tpacpi_inputdev_send_mutex; 822static LIST_HEAD(tpacpi_all_drivers); 823 824static int tpacpi_suspend_handler(struct platform_device *pdev, 825 pm_message_t state) 826{ 827 struct ibm_struct *ibm, *itmp; 828 829 list_for_each_entry_safe(ibm, itmp, 830 &tpacpi_all_drivers, 831 all_drivers) { 832 if (ibm->suspend) 833 (ibm->suspend)(state); 834 } 835 836 return 0; 837} 838 839static int tpacpi_resume_handler(struct platform_device *pdev) 840{ 841 struct ibm_struct *ibm, *itmp; 842 843 list_for_each_entry_safe(ibm, itmp, 844 &tpacpi_all_drivers, 845 all_drivers) { 846 if (ibm->resume) 847 (ibm->resume)(); 848 } 849 850 return 0; 851} 852 853static void tpacpi_shutdown_handler(struct platform_device *pdev) 854{ 855 struct ibm_struct *ibm, *itmp; 856 857 list_for_each_entry_safe(ibm, itmp, 858 &tpacpi_all_drivers, 859 all_drivers) { 860 if (ibm->shutdown) 861 (ibm->shutdown)(); 862 } 863} 864 865static struct platform_driver tpacpi_pdriver = { 866 .driver = { 867 .name = TPACPI_DRVR_NAME, 868 .owner = THIS_MODULE, 869 }, 870 .suspend = tpacpi_suspend_handler, 871 .resume = tpacpi_resume_handler, 872 .shutdown = tpacpi_shutdown_handler, 873}; 874 875static struct platform_driver tpacpi_hwmon_pdriver = { 876 .driver = { 877 .name = TPACPI_HWMON_DRVR_NAME, 878 .owner = THIS_MODULE, 879 }, 880}; 881 882/************************************************************************* 883 * sysfs support helpers 884 */ 885 886struct attribute_set { 887 unsigned int members, max_members; 888 struct attribute_group group; 889}; 890 891struct attribute_set_obj { 892 struct attribute_set s; 893 struct attribute *a; 894} __attribute__((packed)); 895 896static struct attribute_set *create_attr_set(unsigned int max_members, 897 const char *name) 898{ 899 struct attribute_set_obj *sobj; 900 901 if (max_members == 0) 902 return NULL; 903 904 /* Allocates space for implicit NULL at the end too */ 905 sobj = kzalloc(sizeof(struct attribute_set_obj) + 906 max_members * sizeof(struct attribute *), 907 GFP_KERNEL); 908 if (!sobj) 909 return NULL; 910 sobj->s.max_members = max_members; 911 sobj->s.group.attrs = &sobj->a; 912 sobj->s.group.name = name; 913 914 return &sobj->s; 915} 916 917#define destroy_attr_set(_set) \ 918 kfree(_set); 919 920/* not multi-threaded safe, use it in a single thread per set */ 921static int add_to_attr_set(struct attribute_set *s, struct attribute *attr) 922{ 923 if (!s || !attr) 924 return -EINVAL; 925 926 if (s->members >= s->max_members) 927 return -ENOMEM; 928 929 s->group.attrs[s->members] = attr; 930 s->members++; 931 932 return 0; 933} 934 935static int add_many_to_attr_set(struct attribute_set *s, 936 struct attribute **attr, 937 unsigned int count) 938{ 939 int i, res; 940 941 for (i = 0; i < count; i++) { 942 res = add_to_attr_set(s, attr[i]); 943 if (res) 944 return res; 945 } 946 947 return 0; 948} 949 950static void delete_attr_set(struct attribute_set *s, struct kobject *kobj) 951{ 952 sysfs_remove_group(kobj, &s->group); 953 destroy_attr_set(s); 954} 955 956#define register_attr_set_with_sysfs(_attr_set, _kobj) \ 957 sysfs_create_group(_kobj, &_attr_set->group) 958 959static int parse_strtoul(const char *buf, 960 unsigned long max, unsigned long *value) 961{ 962 char *endp; 963 964 while (*buf && isspace(*buf)) 965 buf++; 966 *value = simple_strtoul(buf, &endp, 0); 967 while (*endp && isspace(*endp)) 968 endp++; 969 if (*endp || *value > max) 970 return -EINVAL; 971 972 return 0; 973} 974 975static void tpacpi_disable_brightness_delay(void) 976{ 977 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0)) 978 printk(TPACPI_NOTICE 979 "ACPI backlight control delay disabled\n"); 980} 981 982static int __init tpacpi_query_bcl_levels(acpi_handle handle) 983{ 984 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 985 union acpi_object *obj; 986 int rc; 987 988 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) { 989 obj = (union acpi_object *)buffer.pointer; 990 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) { 991 printk(TPACPI_ERR "Unknown _BCL data, " 992 "please report this to %s\n", TPACPI_MAIL); 993 rc = 0; 994 } else { 995 rc = obj->package.count; 996 } 997 } else { 998 return 0; 999 } 1000 1001 kfree(buffer.pointer); 1002 return rc; 1003} 1004 1005static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle, 1006 u32 lvl, void *context, void **rv) 1007{ 1008 char name[ACPI_PATH_SEGMENT_LENGTH]; 1009 struct acpi_buffer buffer = { sizeof(name), &name }; 1010 1011 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) && 1012 !strncmp("_BCL", name, sizeof(name) - 1)) { 1013 BUG_ON(!rv || !*rv); 1014 **(int **)rv = tpacpi_query_bcl_levels(handle); 1015 return AE_CTRL_TERMINATE; 1016 } else { 1017 return AE_OK; 1018 } 1019} 1020 1021/* 1022 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map 1023 */ 1024static int __init tpacpi_check_std_acpi_brightness_support(void) 1025{ 1026 int status; 1027 int bcl_levels = 0; 1028 void *bcl_ptr = &bcl_levels; 1029 1030 if (!vid_handle) { 1031 TPACPI_ACPIHANDLE_INIT(vid); 1032 } 1033 if (!vid_handle) 1034 return 0; 1035 1036 /* 1037 * Search for a _BCL method, and execute it. This is safe on all 1038 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista 1039 * BIOS in ACPI backlight control mode. We do NOT have to care 1040 * about calling the _BCL method in an enabled video device, any 1041 * will do for our purposes. 1042 */ 1043 1044 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3, 1045 tpacpi_acpi_walk_find_bcl, NULL, 1046 &bcl_ptr); 1047 1048 if (ACPI_SUCCESS(status) && bcl_levels > 2) { 1049 tp_features.bright_acpimode = 1; 1050 return (bcl_levels - 2); 1051 } 1052 1053 return 0; 1054} 1055 1056static void printk_deprecated_attribute(const char * const what, 1057 const char * const details) 1058{ 1059 tpacpi_log_usertask("deprecated sysfs attribute"); 1060 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and " 1061 "will be removed. %s\n", 1062 what, details); 1063} 1064 1065/************************************************************************* 1066 * rfkill and radio control support helpers 1067 */ 1068 1069/* 1070 * ThinkPad-ACPI firmware handling model: 1071 * 1072 * WLSW (master wireless switch) is event-driven, and is common to all 1073 * firmware-controlled radios. It cannot be controlled, just monitored, 1074 * as expected. It overrides all radio state in firmware 1075 * 1076 * The kernel, a masked-off hotkey, and WLSW can change the radio state 1077 * (TODO: verify how WLSW interacts with the returned radio state). 1078 * 1079 * The only time there are shadow radio state changes, is when 1080 * masked-off hotkeys are used. 1081 */ 1082 1083/* 1084 * Internal driver API for radio state: 1085 * 1086 * int: < 0 = error, otherwise enum tpacpi_rfkill_state 1087 * bool: true means radio blocked (off) 1088 */ 1089enum tpacpi_rfkill_state { 1090 TPACPI_RFK_RADIO_OFF = 0, 1091 TPACPI_RFK_RADIO_ON 1092}; 1093 1094/* rfkill switches */ 1095enum tpacpi_rfk_id { 1096 TPACPI_RFK_BLUETOOTH_SW_ID = 0, 1097 TPACPI_RFK_WWAN_SW_ID, 1098 TPACPI_RFK_UWB_SW_ID, 1099 TPACPI_RFK_SW_MAX 1100}; 1101 1102static const char *tpacpi_rfkill_names[] = { 1103 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth", 1104 [TPACPI_RFK_WWAN_SW_ID] = "wwan", 1105 [TPACPI_RFK_UWB_SW_ID] = "uwb", 1106 [TPACPI_RFK_SW_MAX] = NULL 1107}; 1108 1109/* ThinkPad-ACPI rfkill subdriver */ 1110struct tpacpi_rfk { 1111 struct rfkill *rfkill; 1112 enum tpacpi_rfk_id id; 1113 const struct tpacpi_rfk_ops *ops; 1114}; 1115 1116struct tpacpi_rfk_ops { 1117 /* firmware interface */ 1118 int (*get_status)(void); 1119 int (*set_status)(const enum tpacpi_rfkill_state); 1120}; 1121 1122static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX]; 1123 1124/* Query FW and update rfkill sw state for a given rfkill switch */ 1125static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk) 1126{ 1127 int status; 1128 1129 if (!tp_rfk) 1130 return -ENODEV; 1131 1132 status = (tp_rfk->ops->get_status)(); 1133 if (status < 0) 1134 return status; 1135 1136 rfkill_set_sw_state(tp_rfk->rfkill, 1137 (status == TPACPI_RFK_RADIO_OFF)); 1138 1139 return status; 1140} 1141 1142/* Query FW and update rfkill sw state for all rfkill switches */ 1143static void tpacpi_rfk_update_swstate_all(void) 1144{ 1145 unsigned int i; 1146 1147 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) 1148 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]); 1149} 1150 1151/* 1152 * Sync the HW-blocking state of all rfkill switches, 1153 * do notice it causes the rfkill core to schedule uevents 1154 */ 1155static void tpacpi_rfk_update_hwblock_state(bool blocked) 1156{ 1157 unsigned int i; 1158 struct tpacpi_rfk *tp_rfk; 1159 1160 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) { 1161 tp_rfk = tpacpi_rfkill_switches[i]; 1162 if (tp_rfk) { 1163 if (rfkill_set_hw_state(tp_rfk->rfkill, 1164 blocked)) { 1165 /* ignore -- we track sw block */ 1166 } 1167 } 1168 } 1169} 1170 1171/* Call to get the WLSW state from the firmware */ 1172static int hotkey_get_wlsw(void); 1173 1174/* Call to query WLSW state and update all rfkill switches */ 1175static bool tpacpi_rfk_check_hwblock_state(void) 1176{ 1177 int res = hotkey_get_wlsw(); 1178 int hw_blocked; 1179 1180 /* When unknown or unsupported, we have to assume it is unblocked */ 1181 if (res < 0) 1182 return false; 1183 1184 hw_blocked = (res == TPACPI_RFK_RADIO_OFF); 1185 tpacpi_rfk_update_hwblock_state(hw_blocked); 1186 1187 return hw_blocked; 1188} 1189 1190static int tpacpi_rfk_hook_set_block(void *data, bool blocked) 1191{ 1192 struct tpacpi_rfk *tp_rfk = data; 1193 int res; 1194 1195 dbg_printk(TPACPI_DBG_RFKILL, 1196 "request to change radio state to %s\n", 1197 blocked ? "blocked" : "unblocked"); 1198 1199 /* try to set radio state */ 1200 res = (tp_rfk->ops->set_status)(blocked ? 1201 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON); 1202 1203 /* and update the rfkill core with whatever the FW really did */ 1204 tpacpi_rfk_update_swstate(tp_rfk); 1205 1206 return (res < 0) ? res : 0; 1207} 1208 1209static const struct rfkill_ops tpacpi_rfk_rfkill_ops = { 1210 .set_block = tpacpi_rfk_hook_set_block, 1211}; 1212 1213static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id, 1214 const struct tpacpi_rfk_ops *tp_rfkops, 1215 const enum rfkill_type rfktype, 1216 const char *name, 1217 const bool set_default) 1218{ 1219 struct tpacpi_rfk *atp_rfk; 1220 int res; 1221 bool sw_state = false; 1222 int sw_status; 1223 1224 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]); 1225 1226 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL); 1227 if (atp_rfk) 1228 atp_rfk->rfkill = rfkill_alloc(name, 1229 &tpacpi_pdev->dev, 1230 rfktype, 1231 &tpacpi_rfk_rfkill_ops, 1232 atp_rfk); 1233 if (!atp_rfk || !atp_rfk->rfkill) { 1234 printk(TPACPI_ERR 1235 "failed to allocate memory for rfkill class\n"); 1236 kfree(atp_rfk); 1237 return -ENOMEM; 1238 } 1239 1240 atp_rfk->id = id; 1241 atp_rfk->ops = tp_rfkops; 1242 1243 sw_status = (tp_rfkops->get_status)(); 1244 if (sw_status < 0) { 1245 printk(TPACPI_ERR 1246 "failed to read initial state for %s, error %d\n", 1247 name, sw_status); 1248 } else { 1249 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF); 1250 if (set_default) { 1251 /* try to keep the initial state, since we ask the 1252 * firmware to preserve it across S5 in NVRAM */ 1253 rfkill_init_sw_state(atp_rfk->rfkill, sw_state); 1254 } 1255 } 1256 rfkill_set_hw_state(atp_rfk->rfkill, tpacpi_rfk_check_hwblock_state()); 1257 1258 res = rfkill_register(atp_rfk->rfkill); 1259 if (res < 0) { 1260 printk(TPACPI_ERR 1261 "failed to register %s rfkill switch: %d\n", 1262 name, res); 1263 rfkill_destroy(atp_rfk->rfkill); 1264 kfree(atp_rfk); 1265 return res; 1266 } 1267 1268 tpacpi_rfkill_switches[id] = atp_rfk; 1269 return 0; 1270} 1271 1272static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id) 1273{ 1274 struct tpacpi_rfk *tp_rfk; 1275 1276 BUG_ON(id >= TPACPI_RFK_SW_MAX); 1277 1278 tp_rfk = tpacpi_rfkill_switches[id]; 1279 if (tp_rfk) { 1280 rfkill_unregister(tp_rfk->rfkill); 1281 tpacpi_rfkill_switches[id] = NULL; 1282 kfree(tp_rfk); 1283 } 1284} 1285 1286static void printk_deprecated_rfkill_attribute(const char * const what) 1287{ 1288 printk_deprecated_attribute(what, 1289 "Please switch to generic rfkill before year 2010"); 1290} 1291 1292/* sysfs <radio> enable ------------------------------------------------ */ 1293static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id, 1294 struct device_attribute *attr, 1295 char *buf) 1296{ 1297 int status; 1298 1299 printk_deprecated_rfkill_attribute(attr->attr.name); 1300 1301 /* This is in the ABI... */ 1302 if (tpacpi_rfk_check_hwblock_state()) { 1303 status = TPACPI_RFK_RADIO_OFF; 1304 } else { 1305 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]); 1306 if (status < 0) 1307 return status; 1308 } 1309 1310 return snprintf(buf, PAGE_SIZE, "%d\n", 1311 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0); 1312} 1313 1314static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id, 1315 struct device_attribute *attr, 1316 const char *buf, size_t count) 1317{ 1318 unsigned long t; 1319 int res; 1320 1321 printk_deprecated_rfkill_attribute(attr->attr.name); 1322 1323 if (parse_strtoul(buf, 1, &t)) 1324 return -EINVAL; 1325 1326 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t); 1327 1328 /* This is in the ABI... */ 1329 if (tpacpi_rfk_check_hwblock_state() && !!t) 1330 return -EPERM; 1331 1332 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ? 1333 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF); 1334 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]); 1335 1336 return (res < 0) ? res : count; 1337} 1338 1339/* procfs -------------------------------------------------------------- */ 1340static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, char *p) 1341{ 1342 int len = 0; 1343 1344 if (id >= TPACPI_RFK_SW_MAX) 1345 len += sprintf(p + len, "status:\t\tnot supported\n"); 1346 else { 1347 int status; 1348 1349 /* This is in the ABI... */ 1350 if (tpacpi_rfk_check_hwblock_state()) { 1351 status = TPACPI_RFK_RADIO_OFF; 1352 } else { 1353 status = tpacpi_rfk_update_swstate( 1354 tpacpi_rfkill_switches[id]); 1355 if (status < 0) 1356 return status; 1357 } 1358 1359 len += sprintf(p + len, "status:\t\t%s\n", 1360 (status == TPACPI_RFK_RADIO_ON) ? 1361 "enabled" : "disabled"); 1362 len += sprintf(p + len, "commands:\tenable, disable\n"); 1363 } 1364 1365 return len; 1366} 1367 1368static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf) 1369{ 1370 char *cmd; 1371 int status = -1; 1372 int res = 0; 1373 1374 if (id >= TPACPI_RFK_SW_MAX) 1375 return -ENODEV; 1376 1377 while ((cmd = next_cmd(&buf))) { 1378 if (strlencmp(cmd, "enable") == 0) 1379 status = TPACPI_RFK_RADIO_ON; 1380 else if (strlencmp(cmd, "disable") == 0) 1381 status = TPACPI_RFK_RADIO_OFF; 1382 else 1383 return -EINVAL; 1384 } 1385 1386 if (status != -1) { 1387 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n", 1388 (status == TPACPI_RFK_RADIO_ON) ? 1389 "enable" : "disable", 1390 tpacpi_rfkill_names[id]); 1391 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status); 1392 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]); 1393 } 1394 1395 return res; 1396} 1397 1398/************************************************************************* 1399 * thinkpad-acpi driver attributes 1400 */ 1401 1402/* interface_version --------------------------------------------------- */ 1403static ssize_t tpacpi_driver_interface_version_show( 1404 struct device_driver *drv, 1405 char *buf) 1406{ 1407 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION); 1408} 1409 1410static DRIVER_ATTR(interface_version, S_IRUGO, 1411 tpacpi_driver_interface_version_show, NULL); 1412 1413/* debug_level --------------------------------------------------------- */ 1414static ssize_t tpacpi_driver_debug_show(struct device_driver *drv, 1415 char *buf) 1416{ 1417 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level); 1418} 1419 1420static ssize_t tpacpi_driver_debug_store(struct device_driver *drv, 1421 const char *buf, size_t count) 1422{ 1423 unsigned long t; 1424 1425 if (parse_strtoul(buf, 0xffff, &t)) 1426 return -EINVAL; 1427 1428 dbg_level = t; 1429 1430 return count; 1431} 1432 1433static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO, 1434 tpacpi_driver_debug_show, tpacpi_driver_debug_store); 1435 1436/* version ------------------------------------------------------------- */ 1437static ssize_t tpacpi_driver_version_show(struct device_driver *drv, 1438 char *buf) 1439{ 1440 return snprintf(buf, PAGE_SIZE, "%s v%s\n", 1441 TPACPI_DESC, TPACPI_VERSION); 1442} 1443 1444static DRIVER_ATTR(version, S_IRUGO, 1445 tpacpi_driver_version_show, NULL); 1446 1447/* --------------------------------------------------------------------- */ 1448 1449#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 1450 1451/* wlsw_emulstate ------------------------------------------------------ */ 1452static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv, 1453 char *buf) 1454{ 1455 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate); 1456} 1457 1458static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv, 1459 const char *buf, size_t count) 1460{ 1461 unsigned long t; 1462 1463 if (parse_strtoul(buf, 1, &t)) 1464 return -EINVAL; 1465 1466 if (tpacpi_wlsw_emulstate != !!t) { 1467 tpacpi_wlsw_emulstate = !!t; 1468 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */ 1469 } 1470 1471 return count; 1472} 1473 1474static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO, 1475 tpacpi_driver_wlsw_emulstate_show, 1476 tpacpi_driver_wlsw_emulstate_store); 1477 1478/* bluetooth_emulstate ------------------------------------------------- */ 1479static ssize_t tpacpi_driver_bluetooth_emulstate_show( 1480 struct device_driver *drv, 1481 char *buf) 1482{ 1483 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate); 1484} 1485 1486static ssize_t tpacpi_driver_bluetooth_emulstate_store( 1487 struct device_driver *drv, 1488 const char *buf, size_t count) 1489{ 1490 unsigned long t; 1491 1492 if (parse_strtoul(buf, 1, &t)) 1493 return -EINVAL; 1494 1495 tpacpi_bluetooth_emulstate = !!t; 1496 1497 return count; 1498} 1499 1500static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO, 1501 tpacpi_driver_bluetooth_emulstate_show, 1502 tpacpi_driver_bluetooth_emulstate_store); 1503 1504/* wwan_emulstate ------------------------------------------------- */ 1505static ssize_t tpacpi_driver_wwan_emulstate_show( 1506 struct device_driver *drv, 1507 char *buf) 1508{ 1509 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate); 1510} 1511 1512static ssize_t tpacpi_driver_wwan_emulstate_store( 1513 struct device_driver *drv, 1514 const char *buf, size_t count) 1515{ 1516 unsigned long t; 1517 1518 if (parse_strtoul(buf, 1, &t)) 1519 return -EINVAL; 1520 1521 tpacpi_wwan_emulstate = !!t; 1522 1523 return count; 1524} 1525 1526static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO, 1527 tpacpi_driver_wwan_emulstate_show, 1528 tpacpi_driver_wwan_emulstate_store); 1529 1530/* uwb_emulstate ------------------------------------------------- */ 1531static ssize_t tpacpi_driver_uwb_emulstate_show( 1532 struct device_driver *drv, 1533 char *buf) 1534{ 1535 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate); 1536} 1537 1538static ssize_t tpacpi_driver_uwb_emulstate_store( 1539 struct device_driver *drv, 1540 const char *buf, size_t count) 1541{ 1542 unsigned long t; 1543 1544 if (parse_strtoul(buf, 1, &t)) 1545 return -EINVAL; 1546 1547 tpacpi_uwb_emulstate = !!t; 1548 1549 return count; 1550} 1551 1552static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO, 1553 tpacpi_driver_uwb_emulstate_show, 1554 tpacpi_driver_uwb_emulstate_store); 1555#endif 1556 1557/* --------------------------------------------------------------------- */ 1558 1559static struct driver_attribute *tpacpi_driver_attributes[] = { 1560 &driver_attr_debug_level, &driver_attr_version, 1561 &driver_attr_interface_version, 1562}; 1563 1564static int __init tpacpi_create_driver_attributes(struct device_driver *drv) 1565{ 1566 int i, res; 1567 1568 i = 0; 1569 res = 0; 1570 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) { 1571 res = driver_create_file(drv, tpacpi_driver_attributes[i]); 1572 i++; 1573 } 1574 1575#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 1576 if (!res && dbg_wlswemul) 1577 res = driver_create_file(drv, &driver_attr_wlsw_emulstate); 1578 if (!res && dbg_bluetoothemul) 1579 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate); 1580 if (!res && dbg_wwanemul) 1581 res = driver_create_file(drv, &driver_attr_wwan_emulstate); 1582 if (!res && dbg_uwbemul) 1583 res = driver_create_file(drv, &driver_attr_uwb_emulstate); 1584#endif 1585 1586 return res; 1587} 1588 1589static void tpacpi_remove_driver_attributes(struct device_driver *drv) 1590{ 1591 int i; 1592 1593 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++) 1594 driver_remove_file(drv, tpacpi_driver_attributes[i]); 1595 1596#ifdef THINKPAD_ACPI_DEBUGFACILITIES 1597 driver_remove_file(drv, &driver_attr_wlsw_emulstate); 1598 driver_remove_file(drv, &driver_attr_bluetooth_emulstate); 1599 driver_remove_file(drv, &driver_attr_wwan_emulstate); 1600 driver_remove_file(drv, &driver_attr_uwb_emulstate); 1601#endif 1602} 1603 1604/************************************************************************* 1605 * Firmware Data 1606 */ 1607 1608/* 1609 * Table of recommended minimum BIOS versions 1610 * 1611 * Reasons for listing: 1612 * 1. Stable BIOS, listed because the unknown ammount of 1613 * bugs and bad ACPI behaviour on older versions 1614 * 1615 * 2. BIOS or EC fw with known bugs that trigger on Linux 1616 * 1617 * 3. BIOS with known reduced functionality in older versions 1618 * 1619 * We recommend the latest BIOS and EC version. 1620 * We only support the latest BIOS and EC fw version as a rule. 1621 * 1622 * Sources: IBM ThinkPad Public Web Documents (update changelogs), 1623 * Information from users in ThinkWiki 1624 * 1625 * WARNING: we use this table also to detect that the machine is 1626 * a ThinkPad in some cases, so don't remove entries lightly. 1627 */ 1628 1629#define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \ 1630 { .vendor = (__v), \ 1631 .bios = TPID(__id1, __id2), \ 1632 .ec = TPACPI_MATCH_ANY, \ 1633 .quirks = TPACPI_MATCH_ANY << 16 \ 1634 | (__bv1) << 8 | (__bv2) } 1635 1636#define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \ 1637 __eid1, __eid2, __ev1, __ev2) \ 1638 { .vendor = (__v), \ 1639 .bios = TPID(__bid1, __bid2), \ 1640 .ec = TPID(__eid1, __eid2), \ 1641 .quirks = (__ev1) << 24 | (__ev2) << 16 \ 1642 | (__bv1) << 8 | (__bv2) } 1643 1644#define TPV_QI0(__id1, __id2, __bv1, __bv2) \ 1645 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2) 1646 1647#define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \ 1648 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \ 1649 __bv1, __bv2, __id1, __id2, __ev1, __ev2) 1650 1651#define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \ 1652 __eid1, __eid2, __ev1, __ev2) \ 1653 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \ 1654 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2) 1655 1656#define TPV_QL0(__id1, __id2, __bv1, __bv2) \ 1657 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2) 1658 1659#define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \ 1660 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \ 1661 __bv1, __bv2, __id1, __id2, __ev1, __ev2) 1662 1663#define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \ 1664 __eid1, __eid2, __ev1, __ev2) \ 1665 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \ 1666 __bv1, __bv2, __eid1, __eid2, __ev1, __ev2) 1667 1668static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = { 1669 /* Numeric models ------------------ */ 1670 /* FW MODEL BIOS VERS */ 1671 TPV_QI0('I', 'M', '6', '5'), /* 570 */ 1672 TPV_QI0('I', 'U', '2', '6'), /* 570E */ 1673 TPV_QI0('I', 'B', '5', '4'), /* 600 */ 1674 TPV_QI0('I', 'H', '4', '7'), /* 600E */ 1675 TPV_QI0('I', 'N', '3', '6'), /* 600E */ 1676 TPV_QI0('I', 'T', '5', '5'), /* 600X */ 1677 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */ 1678 TPV_QI0('I', 'I', '4', '2'), /* 770X */ 1679 TPV_QI0('I', 'O', '2', '3'), /* 770Z */ 1680 1681 /* A-series ------------------------- */ 1682 /* FW MODEL BIOS VERS EC VERS */ 1683 TPV_QI0('I', 'W', '5', '9'), /* A20m */ 1684 TPV_QI0('I', 'V', '6', '9'), /* A20p */ 1685 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */ 1686 TPV_QI0('K', 'U', '3', '6'), /* A21e */ 1687 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */ 1688 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */ 1689 TPV_QI0('1', 'B', '1', '7'), /* A22e */ 1690 TPV_QI0('1', '3', '2', '0'), /* A22m */ 1691 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */ 1692 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */ 1693 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */ 1694 1695 /* G-series ------------------------- */ 1696 /* FW MODEL BIOS VERS */ 1697 TPV_QI0('1', 'T', 'A', '6'), /* G40 */ 1698 TPV_QI0('1', 'X', '5', '7'), /* G41 */ 1699 1700 /* R-series, T-series --------------- */ 1701 /* FW MODEL BIOS VERS EC VERS */ 1702 TPV_QI0('1', 'C', 'F', '0'), /* R30 */ 1703 TPV_QI0('1', 'F', 'F', '1'), /* R31 */ 1704 TPV_QI0('1', 'M', '9', '7'), /* R32 */ 1705 TPV_QI0('1', 'O', '6', '1'), /* R40 */ 1706 TPV_QI0('1', 'P', '6', '5'), /* R40 */ 1707 TPV_QI0('1', 'S', '7', '0'), /* R40e */ 1708 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51, 1709 T40/p, T41/p, T42/p (1) */ 1710 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */ 1711 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */ 1712 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */ 1713 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */ 1714 1715 TPV_QI0('I', 'Y', '6', '1'), /* T20 */ 1716 TPV_QI0('K', 'Z', '3', '4'), /* T21 */ 1717 TPV_QI0('1', '6', '3', '2'), /* T22 */ 1718 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */ 1719 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */ 1720 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */ 1721 1722 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */ 1723 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */ 1724 TPV_QL0('7', 'E', 'D', '0'), /* R60e, R60i */ 1725 1726 /* BIOS FW BIOS VERS EC FW EC VERS */ 1727 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */ 1728 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */ 1729 1730 /* X-series ------------------------- */ 1731 /* FW MODEL BIOS VERS EC VERS */ 1732 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */ 1733 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */ 1734 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */ 1735 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */ 1736 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */ 1737 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */ 1738 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */ 1739 1740 TPV_QL0('7', 'B', 'D', '7'), /* X60/s */ 1741 TPV_QL0('7', 'J', '3', '0'), /* X60t */ 1742 1743 /* (0) - older versions lack DMI EC fw string and functionality */ 1744 /* (1) - older versions known to lack functionality */ 1745}; 1746 1747#undef TPV_QL1 1748#undef TPV_QL0 1749#undef TPV_QI2 1750#undef TPV_QI1 1751#undef TPV_QI0 1752#undef TPV_Q_X 1753#undef TPV_Q 1754 1755static void __init tpacpi_check_outdated_fw(void) 1756{ 1757 unsigned long fwvers; 1758 u16 ec_version, bios_version; 1759 1760 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable, 1761 ARRAY_SIZE(tpacpi_bios_version_qtable)); 1762 1763 if (!fwvers) 1764 return; 1765 1766 bios_version = fwvers & 0xffffU; 1767 ec_version = (fwvers >> 16) & 0xffffU; 1768 1769 /* note that unknown versions are set to 0x0000 and we use that */ 1770 if ((bios_version > thinkpad_id.bios_release) || 1771 (ec_version > thinkpad_id.ec_release && 1772 ec_version != TPACPI_MATCH_ANY)) { 1773 /* 1774 * The changelogs would let us track down the exact 1775 * reason, but it is just too much of a pain to track 1776 * it. We only list BIOSes that are either really 1777 * broken, or really stable to begin with, so it is 1778 * best if the user upgrades the firmware anyway. 1779 */ 1780 printk(TPACPI_WARN 1781 "WARNING: Outdated ThinkPad BIOS/EC firmware\n"); 1782 printk(TPACPI_WARN 1783 "WARNING: This firmware may be missing critical bug " 1784 "fixes and/or important features\n"); 1785 } 1786} 1787 1788static bool __init tpacpi_is_fw_known(void) 1789{ 1790 return tpacpi_check_quirks(tpacpi_bios_version_qtable, 1791 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0; 1792} 1793 1794/**************************************************************************** 1795 **************************************************************************** 1796 * 1797 * Subdrivers 1798 * 1799 **************************************************************************** 1800 ****************************************************************************/ 1801 1802/************************************************************************* 1803 * thinkpad-acpi init subdriver 1804 */ 1805 1806static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm) 1807{ 1808 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION); 1809 printk(TPACPI_INFO "%s\n", TPACPI_URL); 1810 1811 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n", 1812 (thinkpad_id.bios_version_str) ? 1813 thinkpad_id.bios_version_str : "unknown", 1814 (thinkpad_id.ec_version_str) ? 1815 thinkpad_id.ec_version_str : "unknown"); 1816 1817 if (thinkpad_id.vendor && thinkpad_id.model_str) 1818 printk(TPACPI_INFO "%s %s, model %s\n", 1819 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ? 1820 "IBM" : ((thinkpad_id.vendor == 1821 PCI_VENDOR_ID_LENOVO) ? 1822 "Lenovo" : "Unknown vendor"), 1823 thinkpad_id.model_str, 1824 (thinkpad_id.nummodel_str) ? 1825 thinkpad_id.nummodel_str : "unknown"); 1826 1827 tpacpi_check_outdated_fw(); 1828 return 0; 1829} 1830 1831static int thinkpad_acpi_driver_read(char *p) 1832{ 1833 int len = 0; 1834 1835 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC); 1836 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION); 1837 1838 return len; 1839} 1840 1841static struct ibm_struct thinkpad_acpi_driver_data = { 1842 .name = "driver", 1843 .read = thinkpad_acpi_driver_read, 1844}; 1845 1846/************************************************************************* 1847 * Hotkey subdriver 1848 */ 1849 1850enum { /* hot key scan codes (derived from ACPI DSDT) */ 1851 TP_ACPI_HOTKEYSCAN_FNF1 = 0, 1852 TP_ACPI_HOTKEYSCAN_FNF2, 1853 TP_ACPI_HOTKEYSCAN_FNF3, 1854 TP_ACPI_HOTKEYSCAN_FNF4, 1855 TP_ACPI_HOTKEYSCAN_FNF5, 1856 TP_ACPI_HOTKEYSCAN_FNF6, 1857 TP_ACPI_HOTKEYSCAN_FNF7, 1858 TP_ACPI_HOTKEYSCAN_FNF8, 1859 TP_ACPI_HOTKEYSCAN_FNF9, 1860 TP_ACPI_HOTKEYSCAN_FNF10, 1861 TP_ACPI_HOTKEYSCAN_FNF11, 1862 TP_ACPI_HOTKEYSCAN_FNF12, 1863 TP_ACPI_HOTKEYSCAN_FNBACKSPACE, 1864 TP_ACPI_HOTKEYSCAN_FNINSERT, 1865 TP_ACPI_HOTKEYSCAN_FNDELETE, 1866 TP_ACPI_HOTKEYSCAN_FNHOME, 1867 TP_ACPI_HOTKEYSCAN_FNEND, 1868 TP_ACPI_HOTKEYSCAN_FNPAGEUP, 1869 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN, 1870 TP_ACPI_HOTKEYSCAN_FNSPACE, 1871 TP_ACPI_HOTKEYSCAN_VOLUMEUP, 1872 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN, 1873 TP_ACPI_HOTKEYSCAN_MUTE, 1874 TP_ACPI_HOTKEYSCAN_THINKPAD, 1875}; 1876 1877enum { /* Keys available through NVRAM polling */ 1878 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U, 1879 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U, 1880}; 1881 1882enum { /* Positions of some of the keys in hotkey masks */ 1883 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7, 1884 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8, 1885 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12, 1886 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME, 1887 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND, 1888 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP, 1889 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE, 1890 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP, 1891 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN, 1892 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE, 1893 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD, 1894}; 1895 1896enum { /* NVRAM to ACPI HKEY group map */ 1897 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK | 1898 TP_ACPI_HKEY_ZOOM_MASK | 1899 TP_ACPI_HKEY_DISPSWTCH_MASK | 1900 TP_ACPI_HKEY_HIBERNATE_MASK, 1901 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK | 1902 TP_ACPI_HKEY_BRGHTDWN_MASK, 1903 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK | 1904 TP_ACPI_HKEY_VOLDWN_MASK | 1905 TP_ACPI_HKEY_MUTE_MASK, 1906}; 1907 1908#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL 1909struct tp_nvram_state { 1910 u16 thinkpad_toggle:1; 1911 u16 zoom_toggle:1; 1912 u16 display_toggle:1; 1913 u16 thinklight_toggle:1; 1914 u16 hibernate_toggle:1; 1915 u16 displayexp_toggle:1; 1916 u16 display_state:1; 1917 u16 brightness_toggle:1; 1918 u16 volume_toggle:1; 1919 u16 mute:1; 1920 1921 u8 brightness_level; 1922 u8 volume_level; 1923}; 1924 1925static struct task_struct *tpacpi_hotkey_task; 1926static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */ 1927static int hotkey_poll_freq = 10; /* Hz */ 1928static struct mutex hotkey_thread_mutex; 1929static struct mutex hotkey_thread_data_mutex; 1930static unsigned int hotkey_config_change; 1931 1932#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */ 1933 1934#define hotkey_source_mask 0U 1935 1936#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */ 1937 1938static struct mutex hotkey_mutex; 1939 1940static enum { /* Reasons for waking up */ 1941 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */ 1942 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */ 1943 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */ 1944} hotkey_wakeup_reason; 1945 1946static int hotkey_autosleep_ack; 1947 1948static u32 hotkey_orig_mask; 1949static u32 hotkey_all_mask; 1950static u32 hotkey_reserved_mask; 1951static u32 hotkey_mask; 1952 1953static unsigned int hotkey_report_mode; 1954 1955static u16 *hotkey_keycode_map; 1956 1957static struct attribute_set *hotkey_dev_attributes; 1958 1959#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL 1960#define HOTKEY_CONFIG_CRITICAL_START \ 1961 do { \ 1962 mutex_lock(&hotkey_thread_data_mutex); \ 1963 hotkey_config_change++; \ 1964 } while (0); 1965#define HOTKEY_CONFIG_CRITICAL_END \ 1966 mutex_unlock(&hotkey_thread_data_mutex); 1967#else 1968#define HOTKEY_CONFIG_CRITICAL_START 1969#define HOTKEY_CONFIG_CRITICAL_END 1970#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */ 1971 1972/* HKEY.MHKG() return bits */ 1973#define TP_HOTKEY_TABLET_MASK (1 << 3) 1974 1975static int hotkey_get_wlsw(void) 1976{ 1977 int status; 1978 1979 if (!tp_features.hotkey_wlsw) 1980 return -ENODEV; 1981 1982#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 1983 if (dbg_wlswemul) 1984 return (tpacpi_wlsw_emulstate) ? 1985 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF; 1986#endif 1987 1988 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d")) 1989 return -EIO; 1990 1991 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF; 1992} 1993 1994static int hotkey_get_tablet_mode(int *status) 1995{ 1996 int s; 1997 1998 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d")) 1999 return -EIO; 2000 2001 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0); 2002 return 0; 2003} 2004 2005/* 2006 * Call with hotkey_mutex held 2007 */ 2008static int hotkey_mask_get(void) 2009{ 2010 u32 m = 0; 2011 2012 if (tp_features.hotkey_mask) { 2013 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d")) 2014 return -EIO; 2015 } 2016 hotkey_mask = m | (hotkey_source_mask & hotkey_mask); 2017 2018 return 0; 2019} 2020 2021/* 2022 * Call with hotkey_mutex held 2023 */ 2024static int hotkey_mask_set(u32 mask) 2025{ 2026 int i; 2027 int rc = 0; 2028 2029 if (tp_features.hotkey_mask) { 2030 if (!tp_warned.hotkey_mask_ff && 2031 (mask == 0xffff || mask == 0xffffff || 2032 mask == 0xffffffff)) { 2033 tp_warned.hotkey_mask_ff = 1; 2034 printk(TPACPI_NOTICE 2035 "setting the hotkey mask to 0x%08x is likely " 2036 "not the best way to go about it\n", mask); 2037 printk(TPACPI_NOTICE 2038 "please consider using the driver defaults, " 2039 "and refer to up-to-date thinkpad-acpi " 2040 "documentation\n"); 2041 } 2042 2043 HOTKEY_CONFIG_CRITICAL_START 2044 for (i = 0; i < 32; i++) { 2045 u32 m = 1 << i; 2046 /* enable in firmware mask only keys not in NVRAM 2047 * mode, but enable the key in the cached hotkey_mask 2048 * regardless of mode, or the key will end up 2049 * disabled by hotkey_mask_get() */ 2050 if (!acpi_evalf(hkey_handle, 2051 NULL, "MHKM", "vdd", i + 1, 2052 !!((mask & ~hotkey_source_mask) & m))) { 2053 rc = -EIO; 2054 break; 2055 } else { 2056 hotkey_mask = (hotkey_mask & ~m) | (mask & m); 2057 } 2058 } 2059 HOTKEY_CONFIG_CRITICAL_END 2060 2061 /* hotkey_mask_get must be called unconditionally below */ 2062 if (!hotkey_mask_get() && !rc && 2063 (hotkey_mask & ~hotkey_source_mask) != 2064 (mask & ~hotkey_source_mask)) { 2065 printk(TPACPI_NOTICE 2066 "requested hot key mask 0x%08x, but " 2067 "firmware forced it to 0x%08x\n", 2068 mask, hotkey_mask); 2069 } 2070 } else { 2071#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL 2072 HOTKEY_CONFIG_CRITICAL_START 2073 hotkey_mask = mask & hotkey_source_mask; 2074 HOTKEY_CONFIG_CRITICAL_END 2075 hotkey_mask_get(); 2076 if (hotkey_mask != mask) { 2077 printk(TPACPI_NOTICE 2078 "requested hot key mask 0x%08x, " 2079 "forced to 0x%08x (NVRAM poll mask is " 2080 "0x%08x): no firmware mask support\n", 2081 mask, hotkey_mask, hotkey_source_mask); 2082 } 2083#else 2084 hotkey_mask_get(); 2085 rc = -ENXIO; 2086#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */ 2087 } 2088 2089 return rc; 2090} 2091 2092static int hotkey_status_get(int *status) 2093{ 2094 if (!acpi_evalf(hkey_handle, status, "DHKC", "d")) 2095 return -EIO; 2096 2097 return 0; 2098} 2099 2100static int hotkey_status_set(bool enable) 2101{ 2102 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0)) 2103 return -EIO; 2104 2105 return 0; 2106} 2107 2108static void tpacpi_input_send_tabletsw(void) 2109{ 2110 int state; 2111 2112 if (tp_features.hotkey_tablet && 2113 !hotkey_get_tablet_mode(&state)) { 2114 mutex_lock(&tpacpi_inputdev_send_mutex); 2115 2116 input_report_switch(tpacpi_inputdev, 2117 SW_TABLET_MODE, !!state); 2118 input_sync(tpacpi_inputdev); 2119 2120 mutex_unlock(&tpacpi_inputdev_send_mutex); 2121 } 2122} 2123 2124static void tpacpi_input_send_key(unsigned int scancode) 2125{ 2126 unsigned int keycode; 2127 2128 keycode = hotkey_keycode_map[scancode]; 2129 2130 if (keycode != KEY_RESERVED) { 2131 mutex_lock(&tpacpi_inputdev_send_mutex); 2132 2133 input_report_key(tpacpi_inputdev, keycode, 1); 2134 if (keycode == KEY_UNKNOWN) 2135 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, 2136 scancode); 2137 input_sync(tpacpi_inputdev); 2138 2139 input_report_key(tpacpi_inputdev, keycode, 0); 2140 if (keycode == KEY_UNKNOWN) 2141 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, 2142 scancode); 2143 input_sync(tpacpi_inputdev); 2144 2145 mutex_unlock(&tpacpi_inputdev_send_mutex); 2146 } 2147} 2148 2149#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL 2150static struct tp_acpi_drv_struct ibm_hotkey_acpidriver; 2151 2152static void tpacpi_hotkey_send_key(unsigned int scancode) 2153{ 2154 tpacpi_input_send_key(scancode); 2155 if (hotkey_report_mode < 2) { 2156 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device, 2157 0x80, 0x1001 + scancode); 2158 } 2159} 2160 2161static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m) 2162{ 2163 u8 d; 2164 2165 if (m & TP_NVRAM_HKEY_GROUP_HK2) { 2166 d = nvram_read_byte(TP_NVRAM_ADDR_HK2); 2167 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD); 2168 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM); 2169 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY); 2170 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE); 2171 } 2172 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) { 2173 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT); 2174 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT); 2175 } 2176 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) { 2177 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO); 2178 n->displayexp_toggle = 2179 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND); 2180 } 2181 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) { 2182 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS); 2183 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS) 2184 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS; 2185 n->brightness_toggle = 2186 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS); 2187 } 2188 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) { 2189 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER); 2190 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME) 2191 >> TP_NVRAM_POS_LEVEL_VOLUME; 2192 n->mute = !!(d & TP_NVRAM_MASK_MUTE); 2193 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME); 2194 } 2195} 2196 2197#define TPACPI_COMPARE_KEY(__scancode, __member) \ 2198 do { \ 2199 if ((mask & (1 << __scancode)) && \ 2200 oldn->__member != newn->__member) \ 2201 tpacpi_hotkey_send_key(__scancode); \ 2202 } while (0) 2203 2204#define TPACPI_MAY_SEND_KEY(__scancode) \ 2205 do { if (mask & (1 << __scancode)) \ 2206 tpacpi_hotkey_send_key(__scancode); } while (0) 2207 2208static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn, 2209 struct tp_nvram_state *newn, 2210 u32 mask) 2211{ 2212 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle); 2213 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle); 2214 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle); 2215 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle); 2216 2217 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle); 2218 2219 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle); 2220 2221 /* handle volume */ 2222 if (oldn->volume_toggle != newn->volume_toggle) { 2223 if (oldn->mute != newn->mute) { 2224 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE); 2225 } 2226 if (oldn->volume_level > newn->volume_level) { 2227 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN); 2228 } else if (oldn->volume_level < newn->volume_level) { 2229 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP); 2230 } else if (oldn->mute == newn->mute) { 2231 /* repeated key presses that didn't change state */ 2232 if (newn->mute) { 2233 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE); 2234 } else if (newn->volume_level != 0) { 2235 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP); 2236 } else { 2237 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN); 2238 } 2239 } 2240 } 2241 2242 /* handle brightness */ 2243 if (oldn->brightness_toggle != newn->brightness_toggle) { 2244 if (oldn->brightness_level < newn->brightness_level) { 2245 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME); 2246 } else if (oldn->brightness_level > newn->brightness_level) { 2247 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND); 2248 } else { 2249 /* repeated key presses that didn't change state */ 2250 if (newn->brightness_level != 0) { 2251 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME); 2252 } else { 2253 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND); 2254 } 2255 } 2256 } 2257} 2258 2259#undef TPACPI_COMPARE_KEY 2260#undef TPACPI_MAY_SEND_KEY 2261 2262static int hotkey_kthread(void *data) 2263{ 2264 struct tp_nvram_state s[2]; 2265 u32 mask; 2266 unsigned int si, so; 2267 unsigned long t; 2268 unsigned int change_detector, must_reset; 2269 2270 mutex_lock(&hotkey_thread_mutex); 2271 2272 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING) 2273 goto exit; 2274 2275 set_freezable(); 2276 2277 so = 0; 2278 si = 1; 2279 t = 0; 2280 2281 /* Initial state for compares */ 2282 mutex_lock(&hotkey_thread_data_mutex); 2283 change_detector = hotkey_config_change; 2284 mask = hotkey_source_mask & hotkey_mask; 2285 mutex_unlock(&hotkey_thread_data_mutex); 2286 hotkey_read_nvram(&s[so], mask); 2287 2288 while (!kthread_should_stop() && hotkey_poll_freq) { 2289 if (t == 0) 2290 t = 1000/hotkey_poll_freq; 2291 t = msleep_interruptible(t); 2292 if (unlikely(kthread_should_stop())) 2293 break; 2294 must_reset = try_to_freeze(); 2295 if (t > 0 && !must_reset) 2296 continue; 2297 2298 mutex_lock(&hotkey_thread_data_mutex); 2299 if (must_reset || hotkey_config_change != change_detector) { 2300 /* forget old state on thaw or config change */ 2301 si = so; 2302 t = 0; 2303 change_detector = hotkey_config_change; 2304 } 2305 mask = hotkey_source_mask & hotkey_mask; 2306 mutex_unlock(&hotkey_thread_data_mutex); 2307 2308 if (likely(mask)) { 2309 hotkey_read_nvram(&s[si], mask); 2310 if (likely(si != so)) { 2311 hotkey_compare_and_issue_event(&s[so], &s[si], 2312 mask); 2313 } 2314 } 2315 2316 so = si; 2317 si ^= 1; 2318 } 2319 2320exit: 2321 mutex_unlock(&hotkey_thread_mutex); 2322 return 0; 2323} 2324 2325static void hotkey_poll_stop_sync(void) 2326{ 2327 if (tpacpi_hotkey_task) { 2328 if (frozen(tpacpi_hotkey_task) || 2329 freezing(tpacpi_hotkey_task)) 2330 thaw_process(tpacpi_hotkey_task); 2331 2332 kthread_stop(tpacpi_hotkey_task); 2333 tpacpi_hotkey_task = NULL; 2334 mutex_lock(&hotkey_thread_mutex); 2335 /* at this point, the thread did exit */ 2336 mutex_unlock(&hotkey_thread_mutex); 2337 } 2338} 2339 2340/* call with hotkey_mutex held */ 2341static void hotkey_poll_setup(int may_warn) 2342{ 2343 if ((hotkey_source_mask & hotkey_mask) != 0 && 2344 hotkey_poll_freq > 0 && 2345 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) { 2346 if (!tpacpi_hotkey_task) { 2347 tpacpi_hotkey_task = kthread_run(hotkey_kthread, 2348 NULL, TPACPI_NVRAM_KTHREAD_NAME); 2349 if (IS_ERR(tpacpi_hotkey_task)) { 2350 tpacpi_hotkey_task = NULL; 2351 printk(TPACPI_ERR 2352 "could not create kernel thread " 2353 "for hotkey polling\n"); 2354 } 2355 } 2356 } else { 2357 hotkey_poll_stop_sync(); 2358 if (may_warn && 2359 hotkey_source_mask != 0 && hotkey_poll_freq == 0) { 2360 printk(TPACPI_NOTICE 2361 "hot keys 0x%08x require polling, " 2362 "which is currently disabled\n", 2363 hotkey_source_mask); 2364 } 2365 } 2366} 2367 2368static void hotkey_poll_setup_safe(int may_warn) 2369{ 2370 mutex_lock(&hotkey_mutex); 2371 hotkey_poll_setup(may_warn); 2372 mutex_unlock(&hotkey_mutex); 2373} 2374 2375#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */ 2376 2377static void hotkey_poll_setup_safe(int __unused) 2378{ 2379} 2380 2381#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */ 2382 2383static int hotkey_inputdev_open(struct input_dev *dev) 2384{ 2385 switch (tpacpi_lifecycle) { 2386 case TPACPI_LIFE_INIT: 2387 /* 2388 * hotkey_init will call hotkey_poll_setup_safe 2389 * at the appropriate moment 2390 */ 2391 return 0; 2392 case TPACPI_LIFE_EXITING: 2393 return -EBUSY; 2394 case TPACPI_LIFE_RUNNING: 2395 hotkey_poll_setup_safe(0); 2396 return 0; 2397 } 2398 2399 /* Should only happen if tpacpi_lifecycle is corrupt */ 2400 BUG(); 2401 return -EBUSY; 2402} 2403 2404static void hotkey_inputdev_close(struct input_dev *dev) 2405{ 2406 /* disable hotkey polling when possible */ 2407 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING) 2408 hotkey_poll_setup_safe(0); 2409} 2410 2411/* sysfs hotkey enable ------------------------------------------------- */ 2412static ssize_t hotkey_enable_show(struct device *dev, 2413 struct device_attribute *attr, 2414 char *buf) 2415{ 2416 int res, status; 2417 2418 printk_deprecated_attribute("hotkey_enable", 2419 "Hotkey reporting is always enabled"); 2420 2421 res = hotkey_status_get(&status); 2422 if (res) 2423 return res; 2424 2425 return snprintf(buf, PAGE_SIZE, "%d\n", status); 2426} 2427 2428static ssize_t hotkey_enable_store(struct device *dev, 2429 struct device_attribute *attr, 2430 const char *buf, size_t count) 2431{ 2432 unsigned long t; 2433 2434 printk_deprecated_attribute("hotkey_enable", 2435 "Hotkeys can be disabled through hotkey_mask"); 2436 2437 if (parse_strtoul(buf, 1, &t)) 2438 return -EINVAL; 2439 2440 if (t == 0) 2441 return -EPERM; 2442 2443 return count; 2444} 2445 2446static struct device_attribute dev_attr_hotkey_enable = 2447 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO, 2448 hotkey_enable_show, hotkey_enable_store); 2449 2450/* sysfs hotkey mask --------------------------------------------------- */ 2451static ssize_t hotkey_mask_show(struct device *dev, 2452 struct device_attribute *attr, 2453 char *buf) 2454{ 2455 int res; 2456 2457 if (mutex_lock_killable(&hotkey_mutex)) 2458 return -ERESTARTSYS; 2459 res = hotkey_mask_get(); 2460 mutex_unlock(&hotkey_mutex); 2461 2462 return (res)? 2463 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask); 2464} 2465 2466static ssize_t hotkey_mask_store(struct device *dev, 2467 struct device_attribute *attr, 2468 const char *buf, size_t count) 2469{ 2470 unsigned long t; 2471 int res; 2472 2473 if (parse_strtoul(buf, 0xffffffffUL, &t)) 2474 return -EINVAL; 2475 2476 if (mutex_lock_killable(&hotkey_mutex)) 2477 return -ERESTARTSYS; 2478 2479 res = hotkey_mask_set(t); 2480 2481#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL 2482 hotkey_poll_setup(1); 2483#endif 2484 2485 mutex_unlock(&hotkey_mutex); 2486 2487 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t); 2488 2489 return (res) ? res : count; 2490} 2491 2492static struct device_attribute dev_attr_hotkey_mask = 2493 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO, 2494 hotkey_mask_show, hotkey_mask_store); 2495 2496/* sysfs hotkey bios_enabled ------------------------------------------- */ 2497static ssize_t hotkey_bios_enabled_show(struct device *dev, 2498 struct device_attribute *attr, 2499 char *buf) 2500{ 2501 return sprintf(buf, "0\n"); 2502} 2503 2504static struct device_attribute dev_attr_hotkey_bios_enabled = 2505 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL); 2506 2507/* sysfs hotkey bios_mask ---------------------------------------------- */ 2508static ssize_t hotkey_bios_mask_show(struct device *dev, 2509 struct device_attribute *attr, 2510 char *buf) 2511{ 2512 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask); 2513} 2514 2515static struct device_attribute dev_attr_hotkey_bios_mask = 2516 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL); 2517 2518/* sysfs hotkey all_mask ----------------------------------------------- */ 2519static ssize_t hotkey_all_mask_show(struct device *dev, 2520 struct device_attribute *attr, 2521 char *buf) 2522{ 2523 return snprintf(buf, PAGE_SIZE, "0x%08x\n", 2524 hotkey_all_mask | hotkey_source_mask); 2525} 2526 2527static struct device_attribute dev_attr_hotkey_all_mask = 2528 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL); 2529 2530/* sysfs hotkey recommended_mask --------------------------------------- */ 2531static ssize_t hotkey_recommended_mask_show(struct device *dev, 2532 struct device_attribute *attr, 2533 char *buf) 2534{ 2535 return snprintf(buf, PAGE_SIZE, "0x%08x\n", 2536 (hotkey_all_mask | hotkey_source_mask) 2537 & ~hotkey_reserved_mask); 2538} 2539 2540static struct device_attribute dev_attr_hotkey_recommended_mask = 2541 __ATTR(hotkey_recommended_mask, S_IRUGO, 2542 hotkey_recommended_mask_show, NULL); 2543 2544#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL 2545 2546/* sysfs hotkey hotkey_source_mask ------------------------------------- */ 2547static ssize_t hotkey_source_mask_show(struct device *dev, 2548 struct device_attribute *attr, 2549 char *buf) 2550{ 2551 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask); 2552} 2553 2554static ssize_t hotkey_source_mask_store(struct device *dev, 2555 struct device_attribute *attr, 2556 const char *buf, size_t count) 2557{ 2558 unsigned long t; 2559 2560 if (parse_strtoul(buf, 0xffffffffUL, &t) || 2561 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0)) 2562 return -EINVAL; 2563 2564 if (mutex_lock_killable(&hotkey_mutex)) 2565 return -ERESTARTSYS; 2566 2567 HOTKEY_CONFIG_CRITICAL_START 2568 hotkey_source_mask = t; 2569 HOTKEY_CONFIG_CRITICAL_END 2570 2571 hotkey_poll_setup(1); 2572 2573 mutex_unlock(&hotkey_mutex); 2574 2575 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t); 2576 2577 return count; 2578} 2579 2580static struct device_attribute dev_attr_hotkey_source_mask = 2581 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO, 2582 hotkey_source_mask_show, hotkey_source_mask_store); 2583 2584/* sysfs hotkey hotkey_poll_freq --------------------------------------- */ 2585static ssize_t hotkey_poll_freq_show(struct device *dev, 2586 struct device_attribute *attr, 2587 char *buf) 2588{ 2589 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq); 2590} 2591 2592static ssize_t hotkey_poll_freq_store(struct device *dev, 2593 struct device_attribute *attr, 2594 const char *buf, size_t count) 2595{ 2596 unsigned long t; 2597 2598 if (parse_strtoul(buf, 25, &t)) 2599 return -EINVAL; 2600 2601 if (mutex_lock_killable(&hotkey_mutex)) 2602 return -ERESTARTSYS; 2603 2604 hotkey_poll_freq = t; 2605 2606 hotkey_poll_setup(1); 2607 mutex_unlock(&hotkey_mutex); 2608 2609 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t); 2610 2611 return count; 2612} 2613 2614static struct device_attribute dev_attr_hotkey_poll_freq = 2615 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO, 2616 hotkey_poll_freq_show, hotkey_poll_freq_store); 2617 2618#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */ 2619 2620/* sysfs hotkey radio_sw (pollable) ------------------------------------ */ 2621static ssize_t hotkey_radio_sw_show(struct device *dev, 2622 struct device_attribute *attr, 2623 char *buf) 2624{ 2625 int res; 2626 res = hotkey_get_wlsw(); 2627 if (res < 0) 2628 return res; 2629 2630 /* Opportunistic update */ 2631 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF)); 2632 2633 return snprintf(buf, PAGE_SIZE, "%d\n", 2634 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1); 2635} 2636 2637static struct device_attribute dev_attr_hotkey_radio_sw = 2638 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL); 2639 2640static void hotkey_radio_sw_notify_change(void) 2641{ 2642 if (tp_features.hotkey_wlsw) 2643 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, 2644 "hotkey_radio_sw"); 2645} 2646 2647/* sysfs hotkey tablet mode (pollable) --------------------------------- */ 2648static ssize_t hotkey_tablet_mode_show(struct device *dev, 2649 struct device_attribute *attr, 2650 char *buf) 2651{ 2652 int res, s; 2653 res = hotkey_get_tablet_mode(&s); 2654 if (res < 0) 2655 return res; 2656 2657 return snprintf(buf, PAGE_SIZE, "%d\n", !!s); 2658} 2659 2660static struct device_attribute dev_attr_hotkey_tablet_mode = 2661 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL); 2662 2663static void hotkey_tablet_mode_notify_change(void) 2664{ 2665 if (tp_features.hotkey_tablet) 2666 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, 2667 "hotkey_tablet_mode"); 2668} 2669 2670/* sysfs hotkey report_mode -------------------------------------------- */ 2671static ssize_t hotkey_report_mode_show(struct device *dev, 2672 struct device_attribute *attr, 2673 char *buf) 2674{ 2675 return snprintf(buf, PAGE_SIZE, "%d\n", 2676 (hotkey_report_mode != 0) ? hotkey_report_mode : 1); 2677} 2678 2679static struct device_attribute dev_attr_hotkey_report_mode = 2680 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL); 2681 2682/* sysfs wakeup reason (pollable) -------------------------------------- */ 2683static ssize_t hotkey_wakeup_reason_show(struct device *dev, 2684 struct device_attribute *attr, 2685 char *buf) 2686{ 2687 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason); 2688} 2689 2690static struct device_attribute dev_attr_hotkey_wakeup_reason = 2691 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL); 2692 2693static void hotkey_wakeup_reason_notify_change(void) 2694{ 2695 if (tp_features.hotkey_mask) 2696 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, 2697 "wakeup_reason"); 2698} 2699 2700/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */ 2701static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev, 2702 struct device_attribute *attr, 2703 char *buf) 2704{ 2705 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack); 2706} 2707 2708static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete = 2709 __ATTR(wakeup_hotunplug_complete, S_IRUGO, 2710 hotkey_wakeup_hotunplug_complete_show, NULL); 2711 2712static void hotkey_wakeup_hotunplug_complete_notify_change(void) 2713{ 2714 if (tp_features.hotkey_mask) 2715 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, 2716 "wakeup_hotunplug_complete"); 2717} 2718 2719/* --------------------------------------------------------------------- */ 2720 2721static struct attribute *hotkey_attributes[] __initdata = { 2722 &dev_attr_hotkey_enable.attr, 2723 &dev_attr_hotkey_bios_enabled.attr, 2724 &dev_attr_hotkey_report_mode.attr, 2725#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL 2726 &dev_attr_hotkey_mask.attr, 2727 &dev_attr_hotkey_all_mask.attr, 2728 &dev_attr_hotkey_recommended_mask.attr, 2729 &dev_attr_hotkey_source_mask.attr, 2730 &dev_attr_hotkey_poll_freq.attr, 2731#endif 2732}; 2733 2734static struct attribute *hotkey_mask_attributes[] __initdata = { 2735 &dev_attr_hotkey_bios_mask.attr, 2736#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL 2737 &dev_attr_hotkey_mask.attr, 2738 &dev_attr_hotkey_all_mask.attr, 2739 &dev_attr_hotkey_recommended_mask.attr, 2740#endif 2741 &dev_attr_hotkey_wakeup_reason.attr, 2742 &dev_attr_hotkey_wakeup_hotunplug_complete.attr, 2743}; 2744 2745/* 2746 * Sync both the hw and sw blocking state of all switches 2747 */ 2748static void tpacpi_send_radiosw_update(void) 2749{ 2750 int wlsw; 2751 2752 /* 2753 * We must sync all rfkill controllers *before* issuing any 2754 * rfkill input events, or we will race the rfkill core input 2755 * handler. 2756 * 2757 * tpacpi_inputdev_send_mutex works as a syncronization point 2758 * for the above. 2759 * 2760 * We optimize to avoid numerous calls to hotkey_get_wlsw. 2761 */ 2762 2763 wlsw = hotkey_get_wlsw(); 2764 2765 /* Sync hw blocking state first if it is hw-blocked */ 2766 if (wlsw == TPACPI_RFK_RADIO_OFF) 2767 tpacpi_rfk_update_hwblock_state(true); 2768 2769 /* Sync sw blocking state */ 2770 tpacpi_rfk_update_swstate_all(); 2771 2772 /* Sync hw blocking state last if it is hw-unblocked */ 2773 if (wlsw == TPACPI_RFK_RADIO_ON) 2774 tpacpi_rfk_update_hwblock_state(false); 2775 2776 /* Issue rfkill input event for WLSW switch */ 2777 if (!(wlsw < 0)) { 2778 mutex_lock(&tpacpi_inputdev_send_mutex); 2779 2780 input_report_switch(tpacpi_inputdev, 2781 SW_RFKILL_ALL, (wlsw > 0)); 2782 input_sync(tpacpi_inputdev); 2783 2784 mutex_unlock(&tpacpi_inputdev_send_mutex); 2785 } 2786 2787 /* 2788 * this can be unconditional, as we will poll state again 2789 * if userspace uses the notify to read data 2790 */ 2791 hotkey_radio_sw_notify_change(); 2792} 2793 2794static void hotkey_exit(void) 2795{ 2796#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL 2797 hotkey_poll_stop_sync(); 2798#endif 2799 2800 if (hotkey_dev_attributes) 2801 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj); 2802 2803 kfree(hotkey_keycode_map); 2804 2805 if (tp_features.hotkey) { 2806 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY, 2807 "restoring original hot key mask\n"); 2808 /* no short-circuit boolean operator below! */ 2809 if ((hotkey_mask_set(hotkey_orig_mask) | 2810 hotkey_status_set(false)) != 0) 2811 printk(TPACPI_ERR 2812 "failed to restore hot key mask " 2813 "to BIOS defaults\n"); 2814 } 2815} 2816 2817static int __init hotkey_init(struct ibm_init_struct *iibm) 2818{ 2819 /* Requirements for changing the default keymaps: 2820 * 2821 * 1. Many of the keys are mapped to KEY_RESERVED for very 2822 * good reasons. Do not change them unless you have deep 2823 * knowledge on the IBM and Lenovo ThinkPad firmware for 2824 * the various ThinkPad models. The driver behaves 2825 * differently for KEY_RESERVED: such keys have their 2826 * hot key mask *unset* in mask_recommended, and also 2827 * in the initial hot key mask programmed into the 2828 * firmware at driver load time, which means the firm- 2829 * ware may react very differently if you change them to 2830 * something else; 2831 * 2832 * 2. You must be subscribed to the linux-thinkpad and 2833 * ibm-acpi-devel mailing lists, and you should read the 2834 * list archives since 2007 if you want to change the 2835 * keymaps. This requirement exists so that you will 2836 * know the past history of problems with the thinkpad- 2837 * acpi driver keymaps, and also that you will be 2838 * listening to any bug reports; 2839 * 2840 * 3. Do not send thinkpad-acpi specific patches directly to 2841 * for merging, *ever*. Send them to the linux-acpi 2842 * mailinglist for comments. Merging is to be done only 2843 * through acpi-test and the ACPI maintainer. 2844 * 2845 * If the above is too much to ask, don't change the keymap. 2846 * Ask the thinkpad-acpi maintainer to do it, instead. 2847 */ 2848 static u16 ibm_keycode_map[] __initdata = { 2849 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */ 2850 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP, 2851 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8, 2852 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND, 2853 2854 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */ 2855 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */ 2856 KEY_UNKNOWN, /* 0x0D: FN+INSERT */ 2857 KEY_UNKNOWN, /* 0x0E: FN+DELETE */ 2858 2859 /* brightness: firmware always reacts to them, unless 2860 * X.org did some tricks in the radeon BIOS scratch 2861 * registers of *some* models */ 2862 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */ 2863 KEY_RESERVED, /* 0x10: FN+END (brightness down) */ 2864 2865 /* Thinklight: firmware always react to it */ 2866 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */ 2867 2868 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */ 2869 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */ 2870 2871 /* Volume: firmware always react to it and reprograms 2872 * the built-in *extra* mixer. Never map it to control 2873 * another mixer by default. */ 2874 KEY_RESERVED, /* 0x14: VOLUME UP */ 2875 KEY_RESERVED, /* 0x15: VOLUME DOWN */ 2876 KEY_RESERVED, /* 0x16: MUTE */ 2877 2878 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */ 2879 2880 /* (assignments unknown, please report if found) */ 2881 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, 2882 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, 2883 }; 2884 static u16 lenovo_keycode_map[] __initdata = { 2885 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */ 2886 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP, 2887 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8, 2888 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND, 2889 2890 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */ 2891 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */ 2892 KEY_UNKNOWN, /* 0x0D: FN+INSERT */ 2893 KEY_UNKNOWN, /* 0x0E: FN+DELETE */ 2894 2895 /* These either have to go through ACPI video, or 2896 * act like in the IBM ThinkPads, so don't ever 2897 * enable them by default */ 2898 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */ 2899 KEY_RESERVED, /* 0x10: FN+END (brightness down) */ 2900 2901 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */ 2902 2903 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */ 2904 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */ 2905 2906 /* Volume: z60/z61, T60 (BIOS version?): firmware always 2907 * react to it and reprograms the built-in *extra* mixer. 2908 * Never map it to control another mixer by default. 2909 * 2910 * T60?, T61, R60?, R61: firmware and EC tries to send 2911 * these over the regular keyboard, so these are no-ops, 2912 * but there are still weird bugs re. MUTE, so do not 2913 * change unless you get test reports from all Lenovo 2914 * models. May cause the BIOS to interfere with the 2915 * HDA mixer. 2916 */ 2917 KEY_RESERVED, /* 0x14: VOLUME UP */ 2918 KEY_RESERVED, /* 0x15: VOLUME DOWN */ 2919 KEY_RESERVED, /* 0x16: MUTE */ 2920 2921 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */ 2922 2923 /* (assignments unknown, please report if found) */ 2924 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, 2925 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, 2926 }; 2927 2928#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map) 2929#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map) 2930#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0]) 2931 2932 int res, i; 2933 int status; 2934 int hkeyv; 2935 2936 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY, 2937 "initializing hotkey subdriver\n"); 2938 2939 BUG_ON(!tpacpi_inputdev); 2940 BUG_ON(tpacpi_inputdev->open != NULL || 2941 tpacpi_inputdev->close != NULL); 2942 2943 TPACPI_ACPIHANDLE_INIT(hkey); 2944 mutex_init(&hotkey_mutex); 2945 2946#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL 2947 mutex_init(&hotkey_thread_mutex); 2948 mutex_init(&hotkey_thread_data_mutex); 2949#endif 2950 2951 /* hotkey not supported on 570 */ 2952 tp_features.hotkey = hkey_handle != NULL; 2953 2954 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY, 2955 "hotkeys are %s\n", 2956 str_supported(tp_features.hotkey)); 2957 2958 if (!tp_features.hotkey) 2959 return 1; 2960 2961 tpacpi_disable_brightness_delay(); 2962 2963 hotkey_dev_attributes = create_attr_set(13, NULL); 2964 if (!hotkey_dev_attributes) 2965 return -ENOMEM; 2966 res = add_many_to_attr_set(hotkey_dev_attributes, 2967 hotkey_attributes, 2968 ARRAY_SIZE(hotkey_attributes)); 2969 if (res) 2970 goto err_exit; 2971 2972 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p, 2973 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking 2974 for HKEY interface version 0x100 */ 2975 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) { 2976 if ((hkeyv >> 8) != 1) { 2977 printk(TPACPI_ERR "unknown version of the " 2978 "HKEY interface: 0x%x\n", hkeyv); 2979 printk(TPACPI_ERR "please report this to %s\n", 2980 TPACPI_MAIL); 2981 } else { 2982 /* 2983 * MHKV 0x100 in A31, R40, R40e, 2984 * T4x, X31, and later 2985 */ 2986 tp_features.hotkey_mask = 1; 2987 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY, 2988 "firmware HKEY interface version: 0x%x\n", 2989 hkeyv); 2990 } 2991 } 2992 2993 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY, 2994 "hotkey masks are %s\n", 2995 str_supported(tp_features.hotkey_mask)); 2996 2997 if (tp_features.hotkey_mask) { 2998 if (!acpi_evalf(hkey_handle, &hotkey_all_mask, 2999 "MHKA", "qd")) { 3000 printk(TPACPI_ERR 3001 "missing MHKA handler, " 3002 "please report this to %s\n", 3003 TPACPI_MAIL); 3004 /* FN+F12, FN+F4, FN+F3 */ 3005 hotkey_all_mask = 0x080cU; 3006 } 3007 } 3008 3009 /* hotkey_source_mask *must* be zero for 3010 * the first hotkey_mask_get */ 3011 if (tp_features.hotkey_mask) { 3012 res = hotkey_mask_get(); 3013 if (res) 3014 goto err_exit; 3015 3016 hotkey_orig_mask = hotkey_mask; 3017 res = add_many_to_attr_set( 3018 hotkey_dev_attributes, 3019 hotkey_mask_attributes, 3020 ARRAY_SIZE(hotkey_mask_attributes)); 3021 if (res) 3022 goto err_exit; 3023 } 3024 3025#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL 3026 if (tp_features.hotkey_mask) { 3027 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK 3028 & ~hotkey_all_mask; 3029 } else { 3030 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK; 3031 } 3032 3033 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY, 3034 "hotkey source mask 0x%08x, polling freq %d\n", 3035 hotkey_source_mask, hotkey_poll_freq); 3036#endif 3037 3038#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 3039 if (dbg_wlswemul) { 3040 tp_features.hotkey_wlsw = 1; 3041 printk(TPACPI_INFO 3042 "radio switch emulation enabled\n"); 3043 } else 3044#endif 3045 /* Not all thinkpads have a hardware radio switch */ 3046 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) { 3047 tp_features.hotkey_wlsw = 1; 3048 printk(TPACPI_INFO 3049 "radio switch found; radios are %s\n", 3050 enabled(status, 0)); 3051 } 3052 if (tp_features.hotkey_wlsw) 3053 res = add_to_attr_set(hotkey_dev_attributes, 3054 &dev_attr_hotkey_radio_sw.attr); 3055 3056 /* For X41t, X60t, X61t Tablets... */ 3057 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) { 3058 tp_features.hotkey_tablet = 1; 3059 printk(TPACPI_INFO 3060 "possible tablet mode switch found; " 3061 "ThinkPad in %s mode\n", 3062 (status & TP_HOTKEY_TABLET_MASK)? 3063 "tablet" : "laptop"); 3064 res = add_to_attr_set(hotkey_dev_attributes, 3065 &dev_attr_hotkey_tablet_mode.attr); 3066 } 3067 3068 if (!res) 3069 res = register_attr_set_with_sysfs( 3070 hotkey_dev_attributes, 3071 &tpacpi_pdev->dev.kobj); 3072 if (res) 3073 goto err_exit; 3074 3075 /* Set up key map */ 3076 3077 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE, 3078 GFP_KERNEL); 3079 if (!hotkey_keycode_map) { 3080 printk(TPACPI_ERR 3081 "failed to allocate memory for key map\n"); 3082 res = -ENOMEM; 3083 goto err_exit; 3084 } 3085 3086 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) { 3087 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY, 3088 "using Lenovo default hot key map\n"); 3089 memcpy(hotkey_keycode_map, &lenovo_keycode_map, 3090 TPACPI_HOTKEY_MAP_SIZE); 3091 } else { 3092 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY, 3093 "using IBM default hot key map\n"); 3094 memcpy(hotkey_keycode_map, &ibm_keycode_map, 3095 TPACPI_HOTKEY_MAP_SIZE); 3096 } 3097 3098 set_bit(EV_KEY, tpacpi_inputdev->evbit); 3099 set_bit(EV_MSC, tpacpi_inputdev->evbit); 3100 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit); 3101 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE; 3102 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN; 3103 tpacpi_inputdev->keycode = hotkey_keycode_map; 3104 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) { 3105 if (hotkey_keycode_map[i] != KEY_RESERVED) { 3106 set_bit(hotkey_keycode_map[i], 3107 tpacpi_inputdev->keybit); 3108 } else { 3109 if (i < sizeof(hotkey_reserved_mask)*8) 3110 hotkey_reserved_mask |= 1 << i; 3111 } 3112 } 3113 3114 if (tp_features.hotkey_wlsw) { 3115 set_bit(EV_SW, tpacpi_inputdev->evbit); 3116 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit); 3117 } 3118 if (tp_features.hotkey_tablet) { 3119 set_bit(EV_SW, tpacpi_inputdev->evbit); 3120 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit); 3121 } 3122 3123 /* Do not issue duplicate brightness change events to 3124 * userspace */ 3125 if (!tp_features.bright_acpimode) 3126 /* update bright_acpimode... */ 3127 tpacpi_check_std_acpi_brightness_support(); 3128 3129 if (tp_features.bright_acpimode && acpi_video_backlight_support()) { 3130 printk(TPACPI_INFO 3131 "This ThinkPad has standard ACPI backlight " 3132 "brightness control, supported by the ACPI " 3133 "video driver\n"); 3134 printk(TPACPI_NOTICE 3135 "Disabling thinkpad-acpi brightness events " 3136 "by default...\n"); 3137 3138 /* The hotkey_reserved_mask change below is not 3139 * necessary while the keys are at KEY_RESERVED in the 3140 * default map, but better safe than sorry, leave it 3141 * here as a marker of what we have to do, especially 3142 * when we finally become able to set this at runtime 3143 * on response to X.org requests */ 3144 hotkey_reserved_mask |= 3145 (1 << TP_ACPI_HOTKEYSCAN_FNHOME) 3146 | (1 << TP_ACPI_HOTKEYSCAN_FNEND); 3147 } 3148 3149 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY, 3150 "enabling firmware HKEY event interface...\n"); 3151 res = hotkey_status_set(true); 3152 if (res) { 3153 hotkey_exit(); 3154 return res; 3155 } 3156 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask) 3157 & ~hotkey_reserved_mask) 3158 | hotkey_orig_mask); 3159 if (res < 0 && res != -ENXIO) { 3160 hotkey_exit(); 3161 return res; 3162 } 3163 3164 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY, 3165 "legacy ibm/hotkey event reporting over procfs %s\n", 3166 (hotkey_report_mode < 2) ? 3167 "enabled" : "disabled"); 3168 3169 tpacpi_inputdev->open = &hotkey_inputdev_open; 3170 tpacpi_inputdev->close = &hotkey_inputdev_close; 3171 3172 hotkey_poll_setup_safe(1); 3173 tpacpi_send_radiosw_update(); 3174 tpacpi_input_send_tabletsw(); 3175 3176 return 0; 3177 3178err_exit: 3179 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj); 3180 hotkey_dev_attributes = NULL; 3181 3182 return (res < 0)? res : 1; 3183} 3184 3185static bool hotkey_notify_hotkey(const u32 hkey, 3186 bool *send_acpi_ev, 3187 bool *ignore_acpi_ev) 3188{ 3189 /* 0x1000-0x1FFF: key presses */ 3190 unsigned int scancode = hkey & 0xfff; 3191 *send_acpi_ev = true; 3192 *ignore_acpi_ev = false; 3193 3194 if (scancode > 0 && scancode < 0x21) { 3195 scancode--; 3196 if (!(hotkey_source_mask & (1 << scancode))) { 3197 tpacpi_input_send_key(scancode); 3198 *send_acpi_ev = false; 3199 } else { 3200 *ignore_acpi_ev = true; 3201 } 3202 return true; 3203 } 3204 return false; 3205} 3206 3207static bool hotkey_notify_wakeup(const u32 hkey, 3208 bool *send_acpi_ev, 3209 bool *ignore_acpi_ev) 3210{ 3211 /* 0x2000-0x2FFF: Wakeup reason */ 3212 *send_acpi_ev = true; 3213 *ignore_acpi_ev = false; 3214 3215 switch (hkey) { 3216 case 0x2304: /* suspend, undock */ 3217 case 0x2404: /* hibernation, undock */ 3218 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK; 3219 *ignore_acpi_ev = true; 3220 break; 3221 3222 case 0x2305: /* suspend, bay eject */ 3223 case 0x2405: /* hibernation, bay eject */ 3224 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ; 3225 *ignore_acpi_ev = true; 3226 break; 3227 3228 case 0x2313: /* Battery on critical low level (S3) */ 3229 case 0x2413: /* Battery on critical low level (S4) */ 3230 printk(TPACPI_ALERT 3231 "EMERGENCY WAKEUP: battery almost empty\n"); 3232 /* how to auto-heal: */ 3233 /* 2313: woke up from S3, go to S4/S5 */ 3234 /* 2413: woke up from S4, go to S5 */ 3235 break; 3236 3237 default: 3238 return false; 3239 } 3240 3241 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) { 3242 printk(TPACPI_INFO 3243 "woke up due to a hot-unplug " 3244 "request...\n"); 3245 hotkey_wakeup_reason_notify_change(); 3246 } 3247 return true; 3248} 3249 3250static bool hotkey_notify_usrevent(const u32 hkey, 3251 bool *send_acpi_ev, 3252 bool *ignore_acpi_ev) 3253{ 3254 /* 0x5000-0x5FFF: human interface helpers */ 3255 *send_acpi_ev = true; 3256 *ignore_acpi_ev = false; 3257 3258 switch (hkey) { 3259 case 0x5010: /* Lenovo new BIOS: brightness changed */ 3260 case 0x500b: /* X61t: tablet pen inserted into bay */ 3261 case 0x500c: /* X61t: tablet pen removed from bay */ 3262 return true; 3263 3264 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */ 3265 case 0x500a: /* X41t-X61t: swivel down (normal mode) */ 3266 tpacpi_input_send_tabletsw(); 3267 hotkey_tablet_mode_notify_change(); 3268 *send_acpi_ev = false; 3269 return true; 3270 3271 case 0x5001: 3272 case 0x5002: 3273 /* LID switch events. Do not propagate */ 3274 *ignore_acpi_ev = true; 3275 return true; 3276 3277 default: 3278 return false; 3279 } 3280} 3281 3282static bool hotkey_notify_thermal(const u32 hkey, 3283 bool *send_acpi_ev, 3284 bool *ignore_acpi_ev) 3285{ 3286 /* 0x6000-0x6FFF: thermal alarms */ 3287 *send_acpi_ev = true; 3288 *ignore_acpi_ev = false; 3289 3290 switch (hkey) { 3291 case 0x6011: 3292 printk(TPACPI_CRIT 3293 "THERMAL ALARM: battery is too hot!\n"); 3294 /* recommended action: warn user through gui */ 3295 return true; 3296 case 0x6012: 3297 printk(TPACPI_ALERT 3298 "THERMAL EMERGENCY: battery is extremely hot!\n"); 3299 /* recommended action: immediate sleep/hibernate */ 3300 return true; 3301 case 0x6021: 3302 printk(TPACPI_CRIT 3303 "THERMAL ALARM: " 3304 "a sensor reports something is too hot!\n"); 3305 /* recommended action: warn user through gui, that */ 3306 /* some internal component is too hot */ 3307 return true; 3308 case 0x6022: 3309 printk(TPACPI_ALERT 3310 "THERMAL EMERGENCY: " 3311 "a sensor reports something is extremely hot!\n"); 3312 /* recommended action: immediate sleep/hibernate */ 3313 return true; 3314 case 0x6030: 3315 printk(TPACPI_INFO 3316 "EC reports that Thermal Table has changed\n"); 3317 /* recommended action: do nothing, we don't have 3318 * Lenovo ATM information */ 3319 return true; 3320 default: 3321 printk(TPACPI_ALERT 3322 "THERMAL ALERT: unknown thermal alarm received\n"); 3323 return false; 3324 } 3325} 3326 3327static void hotkey_notify(struct ibm_struct *ibm, u32 event) 3328{ 3329 u32 hkey; 3330 bool send_acpi_ev; 3331 bool ignore_acpi_ev; 3332 bool known_ev; 3333 3334 if (event != 0x80) { 3335 printk(TPACPI_ERR 3336 "unknown HKEY notification event %d\n", event); 3337 /* forward it to userspace, maybe it knows how to handle it */ 3338 acpi_bus_generate_netlink_event( 3339 ibm->acpi->device->pnp.device_class, 3340 dev_name(&ibm->acpi->device->dev), 3341 event, 0); 3342 return; 3343 } 3344 3345 while (1) { 3346 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) { 3347 printk(TPACPI_ERR "failed to retrieve HKEY event\n"); 3348 return; 3349 } 3350 3351 if (hkey == 0) { 3352 /* queue empty */ 3353 return; 3354 } 3355 3356 send_acpi_ev = true; 3357 ignore_acpi_ev = false; 3358 3359 switch (hkey >> 12) { 3360 case 1: 3361 /* 0x1000-0x1FFF: key presses */ 3362 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev, 3363 &ignore_acpi_ev); 3364 break; 3365 case 2: 3366 /* 0x2000-0x2FFF: Wakeup reason */ 3367 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev, 3368 &ignore_acpi_ev); 3369 break; 3370 case 3: 3371 /* 0x3000-0x3FFF: bay-related wakeups */ 3372 if (hkey == 0x3003) { 3373 hotkey_autosleep_ack = 1; 3374 printk(TPACPI_INFO 3375 "bay ejected\n"); 3376 hotkey_wakeup_hotunplug_complete_notify_change(); 3377 known_ev = true; 3378 } else { 3379 known_ev = false; 3380 } 3381 break; 3382 case 4: 3383 /* 0x4000-0x4FFF: dock-related wakeups */ 3384 if (hkey == 0x4003) { 3385 hotkey_autosleep_ack = 1; 3386 printk(TPACPI_INFO 3387 "undocked\n"); 3388 hotkey_wakeup_hotunplug_complete_notify_change(); 3389 known_ev = true; 3390 } else { 3391 known_ev = false; 3392 } 3393 break; 3394 case 5: 3395 /* 0x5000-0x5FFF: human interface helpers */ 3396 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev, 3397 &ignore_acpi_ev); 3398 break; 3399 case 6: 3400 /* 0x6000-0x6FFF: thermal alarms */ 3401 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev, 3402 &ignore_acpi_ev); 3403 break; 3404 case 7: 3405 /* 0x7000-0x7FFF: misc */ 3406 if (tp_features.hotkey_wlsw && hkey == 0x7000) { 3407 tpacpi_send_radiosw_update(); 3408 send_acpi_ev = 0; 3409 known_ev = true; 3410 break; 3411 } 3412 /* fallthrough to default */ 3413 default: 3414 known_ev = false; 3415 } 3416 if (!known_ev) { 3417 printk(TPACPI_NOTICE 3418 "unhandled HKEY event 0x%04x\n", hkey); 3419 printk(TPACPI_NOTICE 3420 "please report the conditions when this " 3421 "event happened to %s\n", TPACPI_MAIL); 3422 } 3423 3424 /* Legacy events */ 3425 if (!ignore_acpi_ev && 3426 (send_acpi_ev || hotkey_report_mode < 2)) { 3427 acpi_bus_generate_proc_event(ibm->acpi->device, 3428 event, hkey); 3429 } 3430 3431 /* netlink events */ 3432 if (!ignore_acpi_ev && send_acpi_ev) { 3433 acpi_bus_generate_netlink_event( 3434 ibm->acpi->device->pnp.device_class, 3435 dev_name(&ibm->acpi->device->dev), 3436 event, hkey); 3437 } 3438 } 3439} 3440 3441static void hotkey_suspend(pm_message_t state) 3442{ 3443 /* Do these on suspend, we get the events on early resume! */ 3444 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE; 3445 hotkey_autosleep_ack = 0; 3446} 3447 3448static void hotkey_resume(void) 3449{ 3450 tpacpi_disable_brightness_delay(); 3451 3452 if (hotkey_mask_get()) 3453 printk(TPACPI_ERR 3454 "error while trying to read hot key mask " 3455 "from firmware\n"); 3456 tpacpi_send_radiosw_update(); 3457 hotkey_tablet_mode_notify_change(); 3458 hotkey_wakeup_reason_notify_change(); 3459 hotkey_wakeup_hotunplug_complete_notify_change(); 3460 hotkey_poll_setup_safe(0); 3461} 3462 3463/* procfs -------------------------------------------------------------- */ 3464static int hotkey_read(char *p) 3465{ 3466 int res, status; 3467 int len = 0; 3468 3469 if (!tp_features.hotkey) { 3470 len += sprintf(p + len, "status:\t\tnot supported\n"); 3471 return len; 3472 } 3473 3474 if (mutex_lock_killable(&hotkey_mutex)) 3475 return -ERESTARTSYS; 3476 res = hotkey_status_get(&status); 3477 if (!res) 3478 res = hotkey_mask_get(); 3479 mutex_unlock(&hotkey_mutex); 3480 if (res) 3481 return res; 3482 3483 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0)); 3484 if (tp_features.hotkey_mask) { 3485 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask); 3486 len += sprintf(p + len, 3487 "commands:\tenable, disable, reset, <mask>\n"); 3488 } else { 3489 len += sprintf(p + len, "mask:\t\tnot supported\n"); 3490 len += sprintf(p + len, "commands:\tenable, disable, reset\n"); 3491 } 3492 3493 return len; 3494} 3495 3496static void hotkey_enabledisable_warn(bool enable) 3497{ 3498 tpacpi_log_usertask("procfs hotkey enable/disable"); 3499 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable), 3500 TPACPI_WARN 3501 "hotkey enable/disable functionality has been " 3502 "removed from the driver. Hotkeys are always " 3503 "enabled\n")) 3504 printk(TPACPI_ERR 3505 "Please remove the hotkey=enable module " 3506 "parameter, it is deprecated. Hotkeys are always " 3507 "enabled\n"); 3508} 3509 3510static int hotkey_write(char *buf) 3511{ 3512 int res; 3513 u32 mask; 3514 char *cmd; 3515 3516 if (!tp_features.hotkey) 3517 return -ENODEV; 3518 3519 if (mutex_lock_killable(&hotkey_mutex)) 3520 return -ERESTARTSYS; 3521 3522 mask = hotkey_mask; 3523 3524 res = 0; 3525 while ((cmd = next_cmd(&buf))) { 3526 if (strlencmp(cmd, "enable") == 0) { 3527 hotkey_enabledisable_warn(1); 3528 } else if (strlencmp(cmd, "disable") == 0) { 3529 hotkey_enabledisable_warn(0); 3530 res = -EPERM; 3531 } else if (strlencmp(cmd, "reset") == 0) { 3532 mask = hotkey_orig_mask; 3533 } else if (sscanf(cmd, "0x%x", &mask) == 1) { 3534 /* mask set */ 3535 } else if (sscanf(cmd, "%x", &mask) == 1) { 3536 /* mask set */ 3537 } else { 3538 res = -EINVAL; 3539 goto errexit; 3540 } 3541 } 3542 3543 if (!res) 3544 tpacpi_disclose_usertask("procfs hotkey", 3545 "set mask to 0x%08x\n", mask); 3546 3547 if (!res && mask != hotkey_mask) 3548 res = hotkey_mask_set(mask); 3549 3550errexit: 3551 mutex_unlock(&hotkey_mutex); 3552 return res; 3553} 3554 3555static const struct acpi_device_id ibm_htk_device_ids[] = { 3556 {TPACPI_ACPI_HKEY_HID, 0}, 3557 {"", 0}, 3558}; 3559 3560static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = { 3561 .hid = ibm_htk_device_ids, 3562 .notify = hotkey_notify, 3563 .handle = &hkey_handle, 3564 .type = ACPI_DEVICE_NOTIFY, 3565}; 3566 3567static struct ibm_struct hotkey_driver_data = { 3568 .name = "hotkey", 3569 .read = hotkey_read, 3570 .write = hotkey_write, 3571 .exit = hotkey_exit, 3572 .resume = hotkey_resume, 3573 .suspend = hotkey_suspend, 3574 .acpi = &ibm_hotkey_acpidriver, 3575}; 3576 3577/************************************************************************* 3578 * Bluetooth subdriver 3579 */ 3580 3581enum { 3582 /* ACPI GBDC/SBDC bits */ 3583 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */ 3584 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */ 3585 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume: 3586 off / last state */ 3587}; 3588 3589enum { 3590 /* ACPI \BLTH commands */ 3591 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */ 3592 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */ 3593 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */ 3594 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */ 3595 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */ 3596}; 3597 3598#define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw" 3599 3600static void bluetooth_suspend(pm_message_t state) 3601{ 3602 /* Try to make sure radio will resume powered off */ 3603 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd", 3604 TP_ACPI_BLTH_PWR_OFF_ON_RESUME)) 3605 vdbg_printk(TPACPI_DBG_RFKILL, 3606 "bluetooth power down on resume request failed\n"); 3607} 3608 3609static int bluetooth_get_status(void) 3610{ 3611 int status; 3612 3613#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 3614 if (dbg_bluetoothemul) 3615 return (tpacpi_bluetooth_emulstate) ? 3616 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF; 3617#endif 3618 3619 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d")) 3620 return -EIO; 3621 3622 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ? 3623 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF; 3624} 3625 3626static int bluetooth_set_status(enum tpacpi_rfkill_state state) 3627{ 3628 int status; 3629 3630 vdbg_printk(TPACPI_DBG_RFKILL, 3631 "will attempt to %s bluetooth\n", 3632 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable"); 3633 3634#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 3635 if (dbg_bluetoothemul) { 3636 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON); 3637 return 0; 3638 } 3639#endif 3640 3641 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */ 3642 if (state == TPACPI_RFK_RADIO_ON) 3643 status = TP_ACPI_BLUETOOTH_RADIOSSW; 3644 else 3645 status = 0; 3646 3647 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status)) 3648 return -EIO; 3649 3650 return 0; 3651} 3652 3653/* sysfs bluetooth enable ---------------------------------------------- */ 3654static ssize_t bluetooth_enable_show(struct device *dev, 3655 struct device_attribute *attr, 3656 char *buf) 3657{ 3658 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID, 3659 attr, buf); 3660} 3661 3662static ssize_t bluetooth_enable_store(struct device *dev, 3663 struct device_attribute *attr, 3664 const char *buf, size_t count) 3665{ 3666 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID, 3667 attr, buf, count); 3668} 3669 3670static struct device_attribute dev_attr_bluetooth_enable = 3671 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO, 3672 bluetooth_enable_show, bluetooth_enable_store); 3673 3674/* --------------------------------------------------------------------- */ 3675 3676static struct attribute *bluetooth_attributes[] = { 3677 &dev_attr_bluetooth_enable.attr, 3678 NULL 3679}; 3680 3681static const struct attribute_group bluetooth_attr_group = { 3682 .attrs = bluetooth_attributes, 3683}; 3684 3685static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = { 3686 .get_status = bluetooth_get_status, 3687 .set_status = bluetooth_set_status, 3688}; 3689 3690static void bluetooth_shutdown(void) 3691{ 3692 /* Order firmware to save current state to NVRAM */ 3693 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd", 3694 TP_ACPI_BLTH_SAVE_STATE)) 3695 printk(TPACPI_NOTICE 3696 "failed to save bluetooth state to NVRAM\n"); 3697 else 3698 vdbg_printk(TPACPI_DBG_RFKILL, 3699 "bluestooth state saved to NVRAM\n"); 3700} 3701 3702static void bluetooth_exit(void) 3703{ 3704 sysfs_remove_group(&tpacpi_pdev->dev.kobj, 3705 &bluetooth_attr_group); 3706 3707 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID); 3708 3709 bluetooth_shutdown(); 3710} 3711 3712static int __init bluetooth_init(struct ibm_init_struct *iibm) 3713{ 3714 int res; 3715 int status = 0; 3716 3717 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, 3718 "initializing bluetooth subdriver\n"); 3719 3720 TPACPI_ACPIHANDLE_INIT(hkey); 3721 3722 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p, 3723 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */ 3724 tp_features.bluetooth = hkey_handle && 3725 acpi_evalf(hkey_handle, &status, "GBDC", "qd"); 3726 3727 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, 3728 "bluetooth is %s, status 0x%02x\n", 3729 str_supported(tp_features.bluetooth), 3730 status); 3731 3732#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 3733 if (dbg_bluetoothemul) { 3734 tp_features.bluetooth = 1; 3735 printk(TPACPI_INFO 3736 "bluetooth switch emulation enabled\n"); 3737 } else 3738#endif 3739 if (tp_features.bluetooth && 3740 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) { 3741 /* no bluetooth hardware present in system */ 3742 tp_features.bluetooth = 0; 3743 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, 3744 "bluetooth hardware not installed\n"); 3745 } 3746 3747 if (!tp_features.bluetooth) 3748 return 1; 3749 3750 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID, 3751 &bluetooth_tprfk_ops, 3752 RFKILL_TYPE_BLUETOOTH, 3753 TPACPI_RFK_BLUETOOTH_SW_NAME, 3754 true); 3755 if (res) 3756 return res; 3757 3758 res = sysfs_create_group(&tpacpi_pdev->dev.kobj, 3759 &bluetooth_attr_group); 3760 if (res) { 3761 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID); 3762 return res; 3763 } 3764 3765 return 0; 3766} 3767 3768/* procfs -------------------------------------------------------------- */ 3769static int bluetooth_read(char *p) 3770{ 3771 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, p); 3772} 3773 3774static int bluetooth_write(char *buf) 3775{ 3776 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf); 3777} 3778 3779static struct ibm_struct bluetooth_driver_data = { 3780 .name = "bluetooth", 3781 .read = bluetooth_read, 3782 .write = bluetooth_write, 3783 .exit = bluetooth_exit, 3784 .suspend = bluetooth_suspend, 3785 .shutdown = bluetooth_shutdown, 3786}; 3787 3788/************************************************************************* 3789 * Wan subdriver 3790 */ 3791 3792enum { 3793 /* ACPI GWAN/SWAN bits */ 3794 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */ 3795 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */ 3796 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume: 3797 off / last state */ 3798}; 3799 3800#define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw" 3801 3802static void wan_suspend(pm_message_t state) 3803{ 3804 /* Try to make sure radio will resume powered off */ 3805 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd", 3806 TP_ACPI_WGSV_PWR_OFF_ON_RESUME)) 3807 vdbg_printk(TPACPI_DBG_RFKILL, 3808 "WWAN power down on resume request failed\n"); 3809} 3810 3811static int wan_get_status(void) 3812{ 3813 int status; 3814 3815#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 3816 if (dbg_wwanemul) 3817 return (tpacpi_wwan_emulstate) ? 3818 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF; 3819#endif 3820 3821 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d")) 3822 return -EIO; 3823 3824 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ? 3825 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF; 3826} 3827 3828static int wan_set_status(enum tpacpi_rfkill_state state) 3829{ 3830 int status; 3831 3832 vdbg_printk(TPACPI_DBG_RFKILL, 3833 "will attempt to %s wwan\n", 3834 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable"); 3835 3836#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 3837 if (dbg_wwanemul) { 3838 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON); 3839 return 0; 3840 } 3841#endif 3842 3843 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */ 3844 if (state == TPACPI_RFK_RADIO_ON) 3845 status = TP_ACPI_WANCARD_RADIOSSW; 3846 else 3847 status = 0; 3848 3849 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status)) 3850 return -EIO; 3851 3852 return 0; 3853} 3854 3855/* sysfs wan enable ---------------------------------------------------- */ 3856static ssize_t wan_enable_show(struct device *dev, 3857 struct device_attribute *attr, 3858 char *buf) 3859{ 3860 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID, 3861 attr, buf); 3862} 3863 3864static ssize_t wan_enable_store(struct device *dev, 3865 struct device_attribute *attr, 3866 const char *buf, size_t count) 3867{ 3868 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID, 3869 attr, buf, count); 3870} 3871 3872static struct device_attribute dev_attr_wan_enable = 3873 __ATTR(wwan_enable, S_IWUSR | S_IRUGO, 3874 wan_enable_show, wan_enable_store); 3875 3876/* --------------------------------------------------------------------- */ 3877 3878static struct attribute *wan_attributes[] = { 3879 &dev_attr_wan_enable.attr, 3880 NULL 3881}; 3882 3883static const struct attribute_group wan_attr_group = { 3884 .attrs = wan_attributes, 3885}; 3886 3887static const struct tpacpi_rfk_ops wan_tprfk_ops = { 3888 .get_status = wan_get_status, 3889 .set_status = wan_set_status, 3890}; 3891 3892static void wan_shutdown(void) 3893{ 3894 /* Order firmware to save current state to NVRAM */ 3895 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd", 3896 TP_ACPI_WGSV_SAVE_STATE)) 3897 printk(TPACPI_NOTICE 3898 "failed to save WWAN state to NVRAM\n"); 3899 else 3900 vdbg_printk(TPACPI_DBG_RFKILL, 3901 "WWAN state saved to NVRAM\n"); 3902} 3903 3904static void wan_exit(void) 3905{ 3906 sysfs_remove_group(&tpacpi_pdev->dev.kobj, 3907 &wan_attr_group); 3908 3909 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID); 3910 3911 wan_shutdown(); 3912} 3913 3914static int __init wan_init(struct ibm_init_struct *iibm) 3915{ 3916 int res; 3917 int status = 0; 3918 3919 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, 3920 "initializing wan subdriver\n"); 3921 3922 TPACPI_ACPIHANDLE_INIT(hkey); 3923 3924 tp_features.wan = hkey_handle && 3925 acpi_evalf(hkey_handle, &status, "GWAN", "qd"); 3926 3927 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, 3928 "wan is %s, status 0x%02x\n", 3929 str_supported(tp_features.wan), 3930 status); 3931 3932#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 3933 if (dbg_wwanemul) { 3934 tp_features.wan = 1; 3935 printk(TPACPI_INFO 3936 "wwan switch emulation enabled\n"); 3937 } else 3938#endif 3939 if (tp_features.wan && 3940 !(status & TP_ACPI_WANCARD_HWPRESENT)) { 3941 /* no wan hardware present in system */ 3942 tp_features.wan = 0; 3943 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, 3944 "wan hardware not installed\n"); 3945 } 3946 3947 if (!tp_features.wan) 3948 return 1; 3949 3950 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID, 3951 &wan_tprfk_ops, 3952 RFKILL_TYPE_WWAN, 3953 TPACPI_RFK_WWAN_SW_NAME, 3954 true); 3955 if (res) 3956 return res; 3957 3958 res = sysfs_create_group(&tpacpi_pdev->dev.kobj, 3959 &wan_attr_group); 3960 3961 if (res) { 3962 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID); 3963 return res; 3964 } 3965 3966 return 0; 3967} 3968 3969/* procfs -------------------------------------------------------------- */ 3970static int wan_read(char *p) 3971{ 3972 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, p); 3973} 3974 3975static int wan_write(char *buf) 3976{ 3977 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf); 3978} 3979 3980static struct ibm_struct wan_driver_data = { 3981 .name = "wan", 3982 .read = wan_read, 3983 .write = wan_write, 3984 .exit = wan_exit, 3985 .suspend = wan_suspend, 3986 .shutdown = wan_shutdown, 3987}; 3988 3989/************************************************************************* 3990 * UWB subdriver 3991 */ 3992 3993enum { 3994 /* ACPI GUWB/SUWB bits */ 3995 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */ 3996 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */ 3997}; 3998 3999#define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw" 4000 4001static int uwb_get_status(void) 4002{ 4003 int status; 4004 4005#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 4006 if (dbg_uwbemul) 4007 return (tpacpi_uwb_emulstate) ? 4008 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF; 4009#endif 4010 4011 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d")) 4012 return -EIO; 4013 4014 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ? 4015 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF; 4016} 4017 4018static int uwb_set_status(enum tpacpi_rfkill_state state) 4019{ 4020 int status; 4021 4022 vdbg_printk(TPACPI_DBG_RFKILL, 4023 "will attempt to %s UWB\n", 4024 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable"); 4025 4026#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 4027 if (dbg_uwbemul) { 4028 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON); 4029 return 0; 4030 } 4031#endif 4032 4033 if (state == TPACPI_RFK_RADIO_ON) 4034 status = TP_ACPI_UWB_RADIOSSW; 4035 else 4036 status = 0; 4037 4038 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status)) 4039 return -EIO; 4040 4041 return 0; 4042} 4043 4044/* --------------------------------------------------------------------- */ 4045 4046static const struct tpacpi_rfk_ops uwb_tprfk_ops = { 4047 .get_status = uwb_get_status, 4048 .set_status = uwb_set_status, 4049}; 4050 4051static void uwb_exit(void) 4052{ 4053 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID); 4054} 4055 4056static int __init uwb_init(struct ibm_init_struct *iibm) 4057{ 4058 int res; 4059 int status = 0; 4060 4061 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, 4062 "initializing uwb subdriver\n"); 4063 4064 TPACPI_ACPIHANDLE_INIT(hkey); 4065 4066 tp_features.uwb = hkey_handle && 4067 acpi_evalf(hkey_handle, &status, "GUWB", "qd"); 4068 4069 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, 4070 "uwb is %s, status 0x%02x\n", 4071 str_supported(tp_features.uwb), 4072 status); 4073 4074#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 4075 if (dbg_uwbemul) { 4076 tp_features.uwb = 1; 4077 printk(TPACPI_INFO 4078 "uwb switch emulation enabled\n"); 4079 } else 4080#endif 4081 if (tp_features.uwb && 4082 !(status & TP_ACPI_UWB_HWPRESENT)) { 4083 /* no uwb hardware present in system */ 4084 tp_features.uwb = 0; 4085 dbg_printk(TPACPI_DBG_INIT, 4086 "uwb hardware not installed\n"); 4087 } 4088 4089 if (!tp_features.uwb) 4090 return 1; 4091 4092 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID, 4093 &uwb_tprfk_ops, 4094 RFKILL_TYPE_UWB, 4095 TPACPI_RFK_UWB_SW_NAME, 4096 false); 4097 return res; 4098} 4099 4100static struct ibm_struct uwb_driver_data = { 4101 .name = "uwb", 4102 .exit = uwb_exit, 4103 .flags.experimental = 1, 4104}; 4105 4106/************************************************************************* 4107 * Video subdriver 4108 */ 4109 4110#ifdef CONFIG_THINKPAD_ACPI_VIDEO 4111 4112enum video_access_mode { 4113 TPACPI_VIDEO_NONE = 0, 4114 TPACPI_VIDEO_570, /* 570 */ 4115 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */ 4116 TPACPI_VIDEO_NEW, /* all others */ 4117}; 4118 4119enum { /* video status flags, based on VIDEO_570 */ 4120 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */ 4121 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */ 4122 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */ 4123}; 4124 4125enum { /* TPACPI_VIDEO_570 constants */ 4126 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */ 4127 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to 4128 * video_status_flags */ 4129 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */ 4130 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */ 4131}; 4132 4133static enum video_access_mode video_supported; 4134static int video_orig_autosw; 4135 4136static int video_autosw_get(void); 4137static int video_autosw_set(int enable); 4138 4139TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */ 4140 4141static int __init video_init(struct ibm_init_struct *iibm) 4142{ 4143 int ivga; 4144 4145 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n"); 4146 4147 TPACPI_ACPIHANDLE_INIT(vid); 4148 TPACPI_ACPIHANDLE_INIT(vid2); 4149 4150 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga) 4151 /* G41, assume IVGA doesn't change */ 4152 vid_handle = vid2_handle; 4153 4154 if (!vid_handle) 4155 /* video switching not supported on R30, R31 */ 4156 video_supported = TPACPI_VIDEO_NONE; 4157 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd")) 4158 /* 570 */ 4159 video_supported = TPACPI_VIDEO_570; 4160 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd")) 4161 /* 600e/x, 770e, 770x */ 4162 video_supported = TPACPI_VIDEO_770; 4163 else 4164 /* all others */ 4165 video_supported = TPACPI_VIDEO_NEW; 4166 4167 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n", 4168 str_supported(video_supported != TPACPI_VIDEO_NONE), 4169 video_supported); 4170 4171 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1; 4172} 4173 4174static void video_exit(void) 4175{ 4176 dbg_printk(TPACPI_DBG_EXIT, 4177 "restoring original video autoswitch mode\n"); 4178 if (video_autosw_set(video_orig_autosw)) 4179 printk(TPACPI_ERR "error while trying to restore original " 4180 "video autoswitch mode\n"); 4181} 4182 4183static int video_outputsw_get(void) 4184{ 4185 int status = 0; 4186 int i; 4187 4188 switch (video_supported) { 4189 case TPACPI_VIDEO_570: 4190 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 4191 TP_ACPI_VIDEO_570_PHSCMD)) 4192 return -EIO; 4193 status = i & TP_ACPI_VIDEO_570_PHSMASK; 4194 break; 4195 case TPACPI_VIDEO_770: 4196 if (!acpi_evalf(NULL, &i, "\\VCDL", "d")) 4197 return -EIO; 4198 if (i) 4199 status |= TP_ACPI_VIDEO_S_LCD; 4200 if (!acpi_evalf(NULL, &i, "\\VCDC", "d")) 4201 return -EIO; 4202 if (i) 4203 status |= TP_ACPI_VIDEO_S_CRT; 4204 break; 4205 case TPACPI_VIDEO_NEW: 4206 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) || 4207 !acpi_evalf(NULL, &i, "\\VCDC", "d")) 4208 return -EIO; 4209 if (i) 4210 status |= TP_ACPI_VIDEO_S_CRT; 4211 4212 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) || 4213 !acpi_evalf(NULL, &i, "\\VCDL", "d")) 4214 return -EIO; 4215 if (i) 4216 status |= TP_ACPI_VIDEO_S_LCD; 4217 if (!acpi_evalf(NULL, &i, "\\VCDD", "d")) 4218 return -EIO; 4219 if (i) 4220 status |= TP_ACPI_VIDEO_S_DVI; 4221 break; 4222 default: 4223 return -ENOSYS; 4224 } 4225 4226 return status; 4227} 4228 4229static int video_outputsw_set(int status) 4230{ 4231 int autosw; 4232 int res = 0; 4233 4234 switch (video_supported) { 4235 case TPACPI_VIDEO_570: 4236 res = acpi_evalf(NULL, NULL, 4237 "\\_SB.PHS2", "vdd", 4238 TP_ACPI_VIDEO_570_PHS2CMD, 4239 status | TP_ACPI_VIDEO_570_PHS2SET); 4240 break; 4241 case TPACPI_VIDEO_770: 4242 autosw = video_autosw_get(); 4243 if (autosw < 0) 4244 return autosw; 4245 4246 res = video_autosw_set(1); 4247 if (res) 4248 return res; 4249 res = acpi_evalf(vid_handle, NULL, 4250 "ASWT", "vdd", status * 0x100, 0); 4251 if (!autosw && video_autosw_set(autosw)) { 4252 printk(TPACPI_ERR 4253 "video auto-switch left enabled due to error\n"); 4254 return -EIO; 4255 } 4256 break; 4257 case TPACPI_VIDEO_NEW: 4258 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) && 4259 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1); 4260 break; 4261 default: 4262 return -ENOSYS; 4263 } 4264 4265 return (res)? 0 : -EIO; 4266} 4267 4268static int video_autosw_get(void) 4269{ 4270 int autosw = 0; 4271 4272 switch (video_supported) { 4273 case TPACPI_VIDEO_570: 4274 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d")) 4275 return -EIO; 4276 break; 4277 case TPACPI_VIDEO_770: 4278 case TPACPI_VIDEO_NEW: 4279 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d")) 4280 return -EIO; 4281 break; 4282 default: 4283 return -ENOSYS; 4284 } 4285 4286 return autosw & 1; 4287} 4288 4289static int video_autosw_set(int enable) 4290{ 4291 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0)) 4292 return -EIO; 4293 return 0; 4294} 4295 4296static int video_outputsw_cycle(void) 4297{ 4298 int autosw = video_autosw_get(); 4299 int res; 4300 4301 if (autosw < 0) 4302 return autosw; 4303 4304 switch (video_supported) { 4305 case TPACPI_VIDEO_570: 4306 res = video_autosw_set(1); 4307 if (res) 4308 return res; 4309 res = acpi_evalf(ec_handle, NULL, "_Q16", "v"); 4310 break; 4311 case TPACPI_VIDEO_770: 4312 case TPACPI_VIDEO_NEW: 4313 res = video_autosw_set(1); 4314 if (res) 4315 return res; 4316 res = acpi_evalf(vid_handle, NULL, "VSWT", "v"); 4317 break; 4318 default: 4319 return -ENOSYS; 4320 } 4321 if (!autosw && video_autosw_set(autosw)) { 4322 printk(TPACPI_ERR 4323 "video auto-switch left enabled due to error\n"); 4324 return -EIO; 4325 } 4326 4327 return (res)? 0 : -EIO; 4328} 4329 4330static int video_expand_toggle(void) 4331{ 4332 switch (video_supported) { 4333 case TPACPI_VIDEO_570: 4334 return acpi_evalf(ec_handle, NULL, "_Q17", "v")? 4335 0 : -EIO; 4336 case TPACPI_VIDEO_770: 4337 return acpi_evalf(vid_handle, NULL, "VEXP", "v")? 4338 0 : -EIO; 4339 case TPACPI_VIDEO_NEW: 4340 return acpi_evalf(NULL, NULL, "\\VEXP", "v")? 4341 0 : -EIO; 4342 default: 4343 return -ENOSYS; 4344 } 4345 /* not reached */ 4346} 4347 4348static int video_read(char *p) 4349{ 4350 int status, autosw; 4351 int len = 0; 4352 4353 if (video_supported == TPACPI_VIDEO_NONE) { 4354 len += sprintf(p + len, "status:\t\tnot supported\n"); 4355 return len; 4356 } 4357 4358 status = video_outputsw_get(); 4359 if (status < 0) 4360 return status; 4361 4362 autosw = video_autosw_get(); 4363 if (autosw < 0) 4364 return autosw; 4365 4366 len += sprintf(p + len, "status:\t\tsupported\n"); 4367 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0)); 4368 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1)); 4369 if (video_supported == TPACPI_VIDEO_NEW) 4370 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3)); 4371 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0)); 4372 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n"); 4373 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n"); 4374 if (video_supported == TPACPI_VIDEO_NEW) 4375 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n"); 4376 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n"); 4377 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n"); 4378 4379 return len; 4380} 4381 4382static int video_write(char *buf) 4383{ 4384 char *cmd; 4385 int enable, disable, status; 4386 int res; 4387 4388 if (video_supported == TPACPI_VIDEO_NONE) 4389 return -ENODEV; 4390 4391 enable = 0; 4392 disable = 0; 4393 4394 while ((cmd = next_cmd(&buf))) { 4395 if (strlencmp(cmd, "lcd_enable") == 0) { 4396 enable |= TP_ACPI_VIDEO_S_LCD; 4397 } else if (strlencmp(cmd, "lcd_disable") == 0) { 4398 disable |= TP_ACPI_VIDEO_S_LCD; 4399 } else if (strlencmp(cmd, "crt_enable") == 0) { 4400 enable |= TP_ACPI_VIDEO_S_CRT; 4401 } else if (strlencmp(cmd, "crt_disable") == 0) { 4402 disable |= TP_ACPI_VIDEO_S_CRT; 4403 } else if (video_supported == TPACPI_VIDEO_NEW && 4404 strlencmp(cmd, "dvi_enable") == 0) { 4405 enable |= TP_ACPI_VIDEO_S_DVI; 4406 } else if (video_supported == TPACPI_VIDEO_NEW && 4407 strlencmp(cmd, "dvi_disable") == 0) { 4408 disable |= TP_ACPI_VIDEO_S_DVI; 4409 } else if (strlencmp(cmd, "auto_enable") == 0) { 4410 res = video_autosw_set(1); 4411 if (res) 4412 return res; 4413 } else if (strlencmp(cmd, "auto_disable") == 0) { 4414 res = video_autosw_set(0); 4415 if (res) 4416 return res; 4417 } else if (strlencmp(cmd, "video_switch") == 0) { 4418 res = video_outputsw_cycle(); 4419 if (res) 4420 return res; 4421 } else if (strlencmp(cmd, "expand_toggle") == 0) { 4422 res = video_expand_toggle(); 4423 if (res) 4424 return res; 4425 } else 4426 return -EINVAL; 4427 } 4428 4429 if (enable || disable) { 4430 status = video_outputsw_get(); 4431 if (status < 0) 4432 return status; 4433 res = video_outputsw_set((status & ~disable) | enable); 4434 if (res) 4435 return res; 4436 } 4437 4438 return 0; 4439} 4440 4441static struct ibm_struct video_driver_data = { 4442 .name = "video", 4443 .read = video_read, 4444 .write = video_write, 4445 .exit = video_exit, 4446}; 4447 4448#endif /* CONFIG_THINKPAD_ACPI_VIDEO */ 4449 4450/************************************************************************* 4451 * Light (thinklight) subdriver 4452 */ 4453 4454TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */ 4455TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */ 4456 4457static int light_get_status(void) 4458{ 4459 int status = 0; 4460 4461 if (tp_features.light_status) { 4462 if (!acpi_evalf(ec_handle, &status, "KBLT", "d")) 4463 return -EIO; 4464 return (!!status); 4465 } 4466 4467 return -ENXIO; 4468} 4469 4470static int light_set_status(int status) 4471{ 4472 int rc; 4473 4474 if (tp_features.light) { 4475 if (cmos_handle) { 4476 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd", 4477 (status)? 4478 TP_CMOS_THINKLIGHT_ON : 4479 TP_CMOS_THINKLIGHT_OFF); 4480 } else { 4481 rc = acpi_evalf(lght_handle, NULL, NULL, "vd", 4482 (status)? 1 : 0); 4483 } 4484 return (rc)? 0 : -EIO; 4485 } 4486 4487 return -ENXIO; 4488} 4489 4490static void light_set_status_worker(struct work_struct *work) 4491{ 4492 struct tpacpi_led_classdev *data = 4493 container_of(work, struct tpacpi_led_classdev, work); 4494 4495 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING)) 4496 light_set_status((data->new_state != TPACPI_LED_OFF)); 4497} 4498 4499static void light_sysfs_set(struct led_classdev *led_cdev, 4500 enum led_brightness brightness) 4501{ 4502 struct tpacpi_led_classdev *data = 4503 container_of(led_cdev, 4504 struct tpacpi_led_classdev, 4505 led_classdev); 4506 data->new_state = (brightness != LED_OFF) ? 4507 TPACPI_LED_ON : TPACPI_LED_OFF; 4508 queue_work(tpacpi_wq, &data->work); 4509} 4510 4511static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev) 4512{ 4513 return (light_get_status() == 1)? LED_FULL : LED_OFF; 4514} 4515 4516static struct tpacpi_led_classdev tpacpi_led_thinklight = { 4517 .led_classdev = { 4518 .name = "tpacpi::thinklight", 4519 .brightness_set = &light_sysfs_set, 4520 .brightness_get = &light_sysfs_get, 4521 } 4522}; 4523 4524static int __init light_init(struct ibm_init_struct *iibm) 4525{ 4526 int rc; 4527 4528 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n"); 4529 4530 TPACPI_ACPIHANDLE_INIT(ledb); 4531 TPACPI_ACPIHANDLE_INIT(lght); 4532 TPACPI_ACPIHANDLE_INIT(cmos); 4533 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker); 4534 4535 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */ 4536 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle; 4537 4538 if (tp_features.light) 4539 /* light status not supported on 4540 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */ 4541 tp_features.light_status = 4542 acpi_evalf(ec_handle, NULL, "KBLT", "qv"); 4543 4544 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n", 4545 str_supported(tp_features.light), 4546 str_supported(tp_features.light_status)); 4547 4548 if (!tp_features.light) 4549 return 1; 4550 4551 rc = led_classdev_register(&tpacpi_pdev->dev, 4552 &tpacpi_led_thinklight.led_classdev); 4553 4554 if (rc < 0) { 4555 tp_features.light = 0; 4556 tp_features.light_status = 0; 4557 } else { 4558 rc = 0; 4559 } 4560 4561 return rc; 4562} 4563 4564static void light_exit(void) 4565{ 4566 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev); 4567 if (work_pending(&tpacpi_led_thinklight.work)) 4568 flush_workqueue(tpacpi_wq); 4569} 4570 4571static int light_read(char *p) 4572{ 4573 int len = 0; 4574 int status; 4575 4576 if (!tp_features.light) { 4577 len += sprintf(p + len, "status:\t\tnot supported\n"); 4578 } else if (!tp_features.light_status) { 4579 len += sprintf(p + len, "status:\t\tunknown\n"); 4580 len += sprintf(p + len, "commands:\ton, off\n"); 4581 } else { 4582 status = light_get_status(); 4583 if (status < 0) 4584 return status; 4585 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0)); 4586 len += sprintf(p + len, "commands:\ton, off\n"); 4587 } 4588 4589 return len; 4590} 4591 4592static int light_write(char *buf) 4593{ 4594 char *cmd; 4595 int newstatus = 0; 4596 4597 if (!tp_features.light) 4598 return -ENODEV; 4599 4600 while ((cmd = next_cmd(&buf))) { 4601 if (strlencmp(cmd, "on") == 0) { 4602 newstatus = 1; 4603 } else if (strlencmp(cmd, "off") == 0) { 4604 newstatus = 0; 4605 } else 4606 return -EINVAL; 4607 } 4608 4609 return light_set_status(newstatus); 4610} 4611 4612static struct ibm_struct light_driver_data = { 4613 .name = "light", 4614 .read = light_read, 4615 .write = light_write, 4616 .exit = light_exit, 4617}; 4618 4619/************************************************************************* 4620 * CMOS subdriver 4621 */ 4622 4623/* sysfs cmos_command -------------------------------------------------- */ 4624static ssize_t cmos_command_store(struct device *dev, 4625 struct device_attribute *attr, 4626 const char *buf, size_t count) 4627{ 4628 unsigned long cmos_cmd; 4629 int res; 4630 4631 if (parse_strtoul(buf, 21, &cmos_cmd)) 4632 return -EINVAL; 4633 4634 res = issue_thinkpad_cmos_command(cmos_cmd); 4635 return (res)? res : count; 4636} 4637 4638static struct device_attribute dev_attr_cmos_command = 4639 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store); 4640 4641/* --------------------------------------------------------------------- */ 4642 4643static int __init cmos_init(struct ibm_init_struct *iibm) 4644{ 4645 int res; 4646 4647 vdbg_printk(TPACPI_DBG_INIT, 4648 "initializing cmos commands subdriver\n"); 4649 4650 TPACPI_ACPIHANDLE_INIT(cmos); 4651 4652 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n", 4653 str_supported(cmos_handle != NULL)); 4654 4655 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command); 4656 if (res) 4657 return res; 4658 4659 return (cmos_handle)? 0 : 1; 4660} 4661 4662static void cmos_exit(void) 4663{ 4664 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command); 4665} 4666 4667static int cmos_read(char *p) 4668{ 4669 int len = 0; 4670 4671 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p, 4672 R30, R31, T20-22, X20-21 */ 4673 if (!cmos_handle) 4674 len += sprintf(p + len, "status:\t\tnot supported\n"); 4675 else { 4676 len += sprintf(p + len, "status:\t\tsupported\n"); 4677 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n"); 4678 } 4679 4680 return len; 4681} 4682 4683static int cmos_write(char *buf) 4684{ 4685 char *cmd; 4686 int cmos_cmd, res; 4687 4688 while ((cmd = next_cmd(&buf))) { 4689 if (sscanf(cmd, "%u", &cmos_cmd) == 1 && 4690 cmos_cmd >= 0 && cmos_cmd <= 21) { 4691 /* cmos_cmd set */ 4692 } else 4693 return -EINVAL; 4694 4695 res = issue_thinkpad_cmos_command(cmos_cmd); 4696 if (res) 4697 return res; 4698 } 4699 4700 return 0; 4701} 4702 4703static struct ibm_struct cmos_driver_data = { 4704 .name = "cmos", 4705 .read = cmos_read, 4706 .write = cmos_write, 4707 .exit = cmos_exit, 4708}; 4709 4710/************************************************************************* 4711 * LED subdriver 4712 */ 4713 4714enum led_access_mode { 4715 TPACPI_LED_NONE = 0, 4716 TPACPI_LED_570, /* 570 */ 4717 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */ 4718 TPACPI_LED_NEW, /* all others */ 4719}; 4720 4721enum { /* For TPACPI_LED_OLD */ 4722 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */ 4723 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */ 4724 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */ 4725}; 4726 4727static enum led_access_mode led_supported; 4728 4729TPACPI_HANDLE(led, ec, "SLED", /* 570 */ 4730 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */ 4731 /* T20-22, X20-21 */ 4732 "LED", /* all others */ 4733 ); /* R30, R31 */ 4734 4735#define TPACPI_LED_NUMLEDS 16 4736static struct tpacpi_led_classdev *tpacpi_leds; 4737static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS]; 4738static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = { 4739 /* there's a limit of 19 chars + NULL before 2.6.26 */ 4740 "tpacpi::power", 4741 "tpacpi:orange:batt", 4742 "tpacpi:green:batt", 4743 "tpacpi::dock_active", 4744 "tpacpi::bay_active", 4745 "tpacpi::dock_batt", 4746 "tpacpi::unknown_led", 4747 "tpacpi::standby", 4748 "tpacpi::dock_status1", 4749 "tpacpi::dock_status2", 4750 "tpacpi::unknown_led2", 4751 "tpacpi::unknown_led3", 4752 "tpacpi::thinkvantage", 4753}; 4754#define TPACPI_SAFE_LEDS 0x1081U 4755 4756static inline bool tpacpi_is_led_restricted(const unsigned int led) 4757{ 4758#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS 4759 return false; 4760#else 4761 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0; 4762#endif 4763} 4764 4765static int led_get_status(const unsigned int led) 4766{ 4767 int status; 4768 enum led_status_t led_s; 4769 4770 switch (led_supported) { 4771 case TPACPI_LED_570: 4772 if (!acpi_evalf(ec_handle, 4773 &status, "GLED", "dd", 1 << led)) 4774 return -EIO; 4775 led_s = (status == 0)? 4776 TPACPI_LED_OFF : 4777 ((status == 1)? 4778 TPACPI_LED_ON : 4779 TPACPI_LED_BLINK); 4780 tpacpi_led_state_cache[led] = led_s; 4781 return led_s; 4782 default: 4783 return -ENXIO; 4784 } 4785 4786 /* not reached */ 4787} 4788 4789static int led_set_status(const unsigned int led, 4790 const enum led_status_t ledstatus) 4791{ 4792 /* off, on, blink. Index is led_status_t */ 4793 static const unsigned int led_sled_arg1[] = { 0, 1, 3 }; 4794 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 }; 4795 4796 int rc = 0; 4797 4798 switch (led_supported) { 4799 case TPACPI_LED_570: 4800 /* 570 */ 4801 if (unlikely(led > 7)) 4802 return -EINVAL; 4803 if (unlikely(tpacpi_is_led_restricted(led))) 4804 return -EPERM; 4805 if (!acpi_evalf(led_handle, NULL, NULL, "vdd", 4806 (1 << led), led_sled_arg1[ledstatus])) 4807 rc = -EIO; 4808 break; 4809 case TPACPI_LED_OLD: 4810 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */ 4811 if (unlikely(led > 7)) 4812 return -EINVAL; 4813 if (unlikely(tpacpi_is_led_restricted(led))) 4814 return -EPERM; 4815 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led)); 4816 if (rc >= 0) 4817 rc = ec_write(TPACPI_LED_EC_HLBL, 4818 (ledstatus == TPACPI_LED_BLINK) << led); 4819 if (rc >= 0) 4820 rc = ec_write(TPACPI_LED_EC_HLCL, 4821 (ledstatus != TPACPI_LED_OFF) << led); 4822 break; 4823 case TPACPI_LED_NEW: 4824 /* all others */ 4825 if (unlikely(led >= TPACPI_LED_NUMLEDS)) 4826 return -EINVAL; 4827 if (unlikely(tpacpi_is_led_restricted(led))) 4828 return -EPERM; 4829 if (!acpi_evalf(led_handle, NULL, NULL, "vdd", 4830 led, led_led_arg1[ledstatus])) 4831 rc = -EIO; 4832 break; 4833 default: 4834 rc = -ENXIO; 4835 } 4836 4837 if (!rc) 4838 tpacpi_led_state_cache[led] = ledstatus; 4839 4840 return rc; 4841} 4842 4843static void led_set_status_worker(struct work_struct *work) 4844{ 4845 struct tpacpi_led_classdev *data = 4846 container_of(work, struct tpacpi_led_classdev, work); 4847 4848 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING)) 4849 led_set_status(data->led, data->new_state); 4850} 4851 4852static void led_sysfs_set(struct led_classdev *led_cdev, 4853 enum led_brightness brightness) 4854{ 4855 struct tpacpi_led_classdev *data = container_of(led_cdev, 4856 struct tpacpi_led_classdev, led_classdev); 4857 4858 if (brightness == LED_OFF) 4859 data->new_state = TPACPI_LED_OFF; 4860 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK) 4861 data->new_state = TPACPI_LED_ON; 4862 else 4863 data->new_state = TPACPI_LED_BLINK; 4864 4865 queue_work(tpacpi_wq, &data->work); 4866} 4867 4868static int led_sysfs_blink_set(struct led_classdev *led_cdev, 4869 unsigned long *delay_on, unsigned long *delay_off) 4870{ 4871 struct tpacpi_led_classdev *data = container_of(led_cdev, 4872 struct tpacpi_led_classdev, led_classdev); 4873 4874 /* Can we choose the flash rate? */ 4875 if (*delay_on == 0 && *delay_off == 0) { 4876 /* yes. set them to the hardware blink rate (1 Hz) */ 4877 *delay_on = 500; /* ms */ 4878 *delay_off = 500; /* ms */ 4879 } else if ((*delay_on != 500) || (*delay_off != 500)) 4880 return -EINVAL; 4881 4882 data->new_state = TPACPI_LED_BLINK; 4883 queue_work(tpacpi_wq, &data->work); 4884 4885 return 0; 4886} 4887 4888static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev) 4889{ 4890 int rc; 4891 4892 struct tpacpi_led_classdev *data = container_of(led_cdev, 4893 struct tpacpi_led_classdev, led_classdev); 4894 4895 rc = led_get_status(data->led); 4896 4897 if (rc == TPACPI_LED_OFF || rc < 0) 4898 rc = LED_OFF; /* no error handling in led class :( */ 4899 else 4900 rc = LED_FULL; 4901 4902 return rc; 4903} 4904 4905static void led_exit(void) 4906{ 4907 unsigned int i; 4908 4909 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) { 4910 if (tpacpi_leds[i].led_classdev.name) 4911 led_classdev_unregister(&tpacpi_leds[i].led_classdev); 4912 } 4913 4914 kfree(tpacpi_leds); 4915} 4916 4917static int __init tpacpi_init_led(unsigned int led) 4918{ 4919 int rc; 4920 4921 tpacpi_leds[led].led = led; 4922 4923 /* LEDs with no name don't get registered */ 4924 if (!tpacpi_led_names[led]) 4925 return 0; 4926 4927 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set; 4928 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set; 4929 if (led_supported == TPACPI_LED_570) 4930 tpacpi_leds[led].led_classdev.brightness_get = 4931 &led_sysfs_get; 4932 4933 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led]; 4934 4935 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker); 4936 4937 rc = led_classdev_register(&tpacpi_pdev->dev, 4938 &tpacpi_leds[led].led_classdev); 4939 if (rc < 0) 4940 tpacpi_leds[led].led_classdev.name = NULL; 4941 4942 return rc; 4943} 4944 4945static const struct tpacpi_quirk led_useful_qtable[] __initconst = { 4946 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */ 4947 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */ 4948 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */ 4949 4950 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */ 4951 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */ 4952 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */ 4953 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */ 4954 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */ 4955 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */ 4956 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */ 4957 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */ 4958 4959 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */ 4960 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */ 4961 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */ 4962 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */ 4963 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */ 4964 4965 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */ 4966 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */ 4967 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */ 4968 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */ 4969 4970 /* (1) - may have excess leds enabled on MSB */ 4971 4972 /* Defaults (order matters, keep last, don't reorder!) */ 4973 { /* Lenovo */ 4974 .vendor = PCI_VENDOR_ID_LENOVO, 4975 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY, 4976 .quirks = 0x1fffU, 4977 }, 4978 { /* IBM ThinkPads with no EC version string */ 4979 .vendor = PCI_VENDOR_ID_IBM, 4980 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN, 4981 .quirks = 0x00ffU, 4982 }, 4983 { /* IBM ThinkPads with EC version string */ 4984 .vendor = PCI_VENDOR_ID_IBM, 4985 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY, 4986 .quirks = 0x00bfU, 4987 }, 4988}; 4989 4990#undef TPACPI_LEDQ_IBM 4991#undef TPACPI_LEDQ_LNV 4992 4993static int __init led_init(struct ibm_init_struct *iibm) 4994{ 4995 unsigned int i; 4996 int rc; 4997 unsigned long useful_leds; 4998 4999 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n"); 5000 5001 TPACPI_ACPIHANDLE_INIT(led); 5002 5003 if (!led_handle) 5004 /* led not supported on R30, R31 */ 5005 led_supported = TPACPI_LED_NONE; 5006 else if (strlencmp(led_path, "SLED") == 0) 5007 /* 570 */ 5008 led_supported = TPACPI_LED_570; 5009 else if (strlencmp(led_path, "SYSL") == 0) 5010 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */ 5011 led_supported = TPACPI_LED_OLD; 5012 else 5013 /* all others */ 5014 led_supported = TPACPI_LED_NEW; 5015 5016 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n", 5017 str_supported(led_supported), led_supported); 5018 5019 if (led_supported == TPACPI_LED_NONE) 5020 return 1; 5021 5022 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS, 5023 GFP_KERNEL); 5024 if (!tpacpi_leds) { 5025 printk(TPACPI_ERR "Out of memory for LED data\n"); 5026 return -ENOMEM; 5027 } 5028 5029 useful_leds = tpacpi_check_quirks(led_useful_qtable, 5030 ARRAY_SIZE(led_useful_qtable)); 5031 5032 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) { 5033 if (!tpacpi_is_led_restricted(i) && 5034 test_bit(i, &useful_leds)) { 5035 rc = tpacpi_init_led(i); 5036 if (rc < 0) { 5037 led_exit(); 5038 return rc; 5039 } 5040 } 5041 } 5042 5043#ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS 5044 printk(TPACPI_NOTICE 5045 "warning: userspace override of important " 5046 "firmware LEDs is enabled\n"); 5047#endif 5048 return 0; 5049} 5050 5051#define str_led_status(s) \ 5052 ((s) == TPACPI_LED_OFF ? "off" : \ 5053 ((s) == TPACPI_LED_ON ? "on" : "blinking")) 5054 5055static int led_read(char *p) 5056{ 5057 int len = 0; 5058 5059 if (!led_supported) { 5060 len += sprintf(p + len, "status:\t\tnot supported\n"); 5061 return len; 5062 } 5063 len += sprintf(p + len, "status:\t\tsupported\n"); 5064 5065 if (led_supported == TPACPI_LED_570) { 5066 /* 570 */ 5067 int i, status; 5068 for (i = 0; i < 8; i++) { 5069 status = led_get_status(i); 5070 if (status < 0) 5071 return -EIO; 5072 len += sprintf(p + len, "%d:\t\t%s\n", 5073 i, str_led_status(status)); 5074 } 5075 } 5076 5077 len += sprintf(p + len, "commands:\t" 5078 "<led> on, <led> off, <led> blink (<led> is 0-15)\n"); 5079 5080 return len; 5081} 5082 5083static int led_write(char *buf) 5084{ 5085 char *cmd; 5086 int led, rc; 5087 enum led_status_t s; 5088 5089 if (!led_supported) 5090 return -ENODEV; 5091 5092 while ((cmd = next_cmd(&buf))) { 5093 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15) 5094 return -EINVAL; 5095 5096 if (strstr(cmd, "off")) { 5097 s = TPACPI_LED_OFF; 5098 } else if (strstr(cmd, "on")) { 5099 s = TPACPI_LED_ON; 5100 } else if (strstr(cmd, "blink")) { 5101 s = TPACPI_LED_BLINK; 5102 } else { 5103 return -EINVAL; 5104 } 5105 5106 rc = led_set_status(led, s); 5107 if (rc < 0) 5108 return rc; 5109 } 5110 5111 return 0; 5112} 5113 5114static struct ibm_struct led_driver_data = { 5115 .name = "led", 5116 .read = led_read, 5117 .write = led_write, 5118 .exit = led_exit, 5119}; 5120 5121/************************************************************************* 5122 * Beep subdriver 5123 */ 5124 5125TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */ 5126 5127#define TPACPI_BEEP_Q1 0x0001 5128 5129static const struct tpacpi_quirk beep_quirk_table[] __initconst = { 5130 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */ 5131 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */ 5132}; 5133 5134static int __init beep_init(struct ibm_init_struct *iibm) 5135{ 5136 unsigned long quirks; 5137 5138 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n"); 5139 5140 TPACPI_ACPIHANDLE_INIT(beep); 5141 5142 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n", 5143 str_supported(beep_handle != NULL)); 5144 5145 quirks = tpacpi_check_quirks(beep_quirk_table, 5146 ARRAY_SIZE(beep_quirk_table)); 5147 5148 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1); 5149 5150 return (beep_handle)? 0 : 1; 5151} 5152 5153static int beep_read(char *p) 5154{ 5155 int len = 0; 5156 5157 if (!beep_handle) 5158 len += sprintf(p + len, "status:\t\tnot supported\n"); 5159 else { 5160 len += sprintf(p + len, "status:\t\tsupported\n"); 5161 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n"); 5162 } 5163 5164 return len; 5165} 5166 5167static int beep_write(char *buf) 5168{ 5169 char *cmd; 5170 int beep_cmd; 5171 5172 if (!beep_handle) 5173 return -ENODEV; 5174 5175 while ((cmd = next_cmd(&buf))) { 5176 if (sscanf(cmd, "%u", &beep_cmd) == 1 && 5177 beep_cmd >= 0 && beep_cmd <= 17) { 5178 /* beep_cmd set */ 5179 } else 5180 return -EINVAL; 5181 if (tp_features.beep_needs_two_args) { 5182 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", 5183 beep_cmd, 0)) 5184 return -EIO; 5185 } else { 5186 if (!acpi_evalf(beep_handle, NULL, NULL, "vd", 5187 beep_cmd)) 5188 return -EIO; 5189 } 5190 } 5191 5192 return 0; 5193} 5194 5195static struct ibm_struct beep_driver_data = { 5196 .name = "beep", 5197 .read = beep_read, 5198 .write = beep_write, 5199}; 5200 5201/************************************************************************* 5202 * Thermal subdriver 5203 */ 5204 5205enum thermal_access_mode { 5206 TPACPI_THERMAL_NONE = 0, /* No thermal support */ 5207 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */ 5208 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */ 5209 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */ 5210 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */ 5211}; 5212 5213enum { /* TPACPI_THERMAL_TPEC_* */ 5214 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */ 5215 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */ 5216 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */ 5217}; 5218 5219#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */ 5220struct ibm_thermal_sensors_struct { 5221 s32 temp[TPACPI_MAX_THERMAL_SENSORS]; 5222}; 5223 5224static enum thermal_access_mode thermal_read_mode; 5225 5226/* idx is zero-based */ 5227static int thermal_get_sensor(int idx, s32 *value) 5228{ 5229 int t; 5230 s8 tmp; 5231 char tmpi[5]; 5232 5233 t = TP_EC_THERMAL_TMP0; 5234 5235 switch (thermal_read_mode) { 5236#if TPACPI_MAX_THERMAL_SENSORS >= 16 5237 case TPACPI_THERMAL_TPEC_16: 5238 if (idx >= 8 && idx <= 15) { 5239 t = TP_EC_THERMAL_TMP8; 5240 idx -= 8; 5241 } 5242 /* fallthrough */ 5243#endif 5244 case TPACPI_THERMAL_TPEC_8: 5245 if (idx <= 7) { 5246 if (!acpi_ec_read(t + idx, &tmp)) 5247 return -EIO; 5248 *value = tmp * 1000; 5249 return 0; 5250 } 5251 break; 5252 5253 case TPACPI_THERMAL_ACPI_UPDT: 5254 if (idx <= 7) { 5255 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx); 5256 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v")) 5257 return -EIO; 5258 if (!acpi_evalf(ec_handle, &t, tmpi, "d")) 5259 return -EIO; 5260 *value = (t - 2732) * 100; 5261 return 0; 5262 } 5263 break; 5264 5265 case TPACPI_THERMAL_ACPI_TMP07: 5266 if (idx <= 7) { 5267 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx); 5268 if (!acpi_evalf(ec_handle, &t, tmpi, "d")) 5269 return -EIO; 5270 if (t > 127 || t < -127) 5271 t = TP_EC_THERMAL_TMP_NA; 5272 *value = t * 1000; 5273 return 0; 5274 } 5275 break; 5276 5277 case TPACPI_THERMAL_NONE: 5278 default: 5279 return -ENOSYS; 5280 } 5281 5282 return -EINVAL; 5283} 5284 5285static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s) 5286{ 5287 int res, i; 5288 int n; 5289 5290 n = 8; 5291 i = 0; 5292 5293 if (!s) 5294 return -EINVAL; 5295 5296 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16) 5297 n = 16; 5298 5299 for (i = 0 ; i < n; i++) { 5300 res = thermal_get_sensor(i, &s->temp[i]); 5301 if (res) 5302 return res; 5303 } 5304 5305 return n; 5306} 5307 5308/* sysfs temp##_input -------------------------------------------------- */ 5309 5310static ssize_t thermal_temp_input_show(struct device *dev, 5311 struct device_attribute *attr, 5312 char *buf) 5313{ 5314 struct sensor_device_attribute *sensor_attr = 5315 to_sensor_dev_attr(attr); 5316 int idx = sensor_attr->index; 5317 s32 value; 5318 int res; 5319 5320 res = thermal_get_sensor(idx, &value); 5321 if (res) 5322 return res; 5323 if (value == TP_EC_THERMAL_TMP_NA * 1000) 5324 return -ENXIO; 5325 5326 return snprintf(buf, PAGE_SIZE, "%d\n", value); 5327} 5328 5329#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \ 5330 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \ 5331 thermal_temp_input_show, NULL, _idxB) 5332 5333static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = { 5334 THERMAL_SENSOR_ATTR_TEMP(1, 0), 5335 THERMAL_SENSOR_ATTR_TEMP(2, 1), 5336 THERMAL_SENSOR_ATTR_TEMP(3, 2), 5337 THERMAL_SENSOR_ATTR_TEMP(4, 3), 5338 THERMAL_SENSOR_ATTR_TEMP(5, 4), 5339 THERMAL_SENSOR_ATTR_TEMP(6, 5), 5340 THERMAL_SENSOR_ATTR_TEMP(7, 6), 5341 THERMAL_SENSOR_ATTR_TEMP(8, 7), 5342 THERMAL_SENSOR_ATTR_TEMP(9, 8), 5343 THERMAL_SENSOR_ATTR_TEMP(10, 9), 5344 THERMAL_SENSOR_ATTR_TEMP(11, 10), 5345 THERMAL_SENSOR_ATTR_TEMP(12, 11), 5346 THERMAL_SENSOR_ATTR_TEMP(13, 12), 5347 THERMAL_SENSOR_ATTR_TEMP(14, 13), 5348 THERMAL_SENSOR_ATTR_TEMP(15, 14), 5349 THERMAL_SENSOR_ATTR_TEMP(16, 15), 5350}; 5351 5352#define THERMAL_ATTRS(X) \ 5353 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr 5354 5355static struct attribute *thermal_temp_input_attr[] = { 5356 THERMAL_ATTRS(8), 5357 THERMAL_ATTRS(9), 5358 THERMAL_ATTRS(10), 5359 THERMAL_ATTRS(11), 5360 THERMAL_ATTRS(12), 5361 THERMAL_ATTRS(13), 5362 THERMAL_ATTRS(14), 5363 THERMAL_ATTRS(15), 5364 THERMAL_ATTRS(0), 5365 THERMAL_ATTRS(1), 5366 THERMAL_ATTRS(2), 5367 THERMAL_ATTRS(3), 5368 THERMAL_ATTRS(4), 5369 THERMAL_ATTRS(5), 5370 THERMAL_ATTRS(6), 5371 THERMAL_ATTRS(7), 5372 NULL 5373}; 5374 5375static const struct attribute_group thermal_temp_input16_group = { 5376 .attrs = thermal_temp_input_attr 5377}; 5378 5379static const struct attribute_group thermal_temp_input8_group = { 5380 .attrs = &thermal_temp_input_attr[8] 5381}; 5382 5383#undef THERMAL_SENSOR_ATTR_TEMP 5384#undef THERMAL_ATTRS 5385 5386/* --------------------------------------------------------------------- */ 5387 5388static int __init thermal_init(struct ibm_init_struct *iibm) 5389{ 5390 u8 t, ta1, ta2; 5391 int i; 5392 int acpi_tmp7; 5393 int res; 5394 5395 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n"); 5396 5397 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv"); 5398 5399 if (thinkpad_id.ec_model) { 5400 /* 5401 * Direct EC access mode: sensors at registers 5402 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for 5403 * non-implemented, thermal sensors return 0x80 when 5404 * not available 5405 */ 5406 5407 ta1 = ta2 = 0; 5408 for (i = 0; i < 8; i++) { 5409 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) { 5410 ta1 |= t; 5411 } else { 5412 ta1 = 0; 5413 break; 5414 } 5415 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) { 5416 ta2 |= t; 5417 } else { 5418 ta1 = 0; 5419 break; 5420 } 5421 } 5422 if (ta1 == 0) { 5423 /* This is sheer paranoia, but we handle it anyway */ 5424 if (acpi_tmp7) { 5425 printk(TPACPI_ERR 5426 "ThinkPad ACPI EC access misbehaving, " 5427 "falling back to ACPI TMPx access " 5428 "mode\n"); 5429 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07; 5430 } else { 5431 printk(TPACPI_ERR 5432 "ThinkPad ACPI EC access misbehaving, " 5433 "disabling thermal sensors access\n"); 5434 thermal_read_mode = TPACPI_THERMAL_NONE; 5435 } 5436 } else { 5437 thermal_read_mode = 5438 (ta2 != 0) ? 5439 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8; 5440 } 5441 } else if (acpi_tmp7) { 5442 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) { 5443 /* 600e/x, 770e, 770x */ 5444 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT; 5445 } else { 5446 /* Standard ACPI TMPx access, max 8 sensors */ 5447 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07; 5448 } 5449 } else { 5450 /* temperatures not supported on 570, G4x, R30, R31, R32 */ 5451 thermal_read_mode = TPACPI_THERMAL_NONE; 5452 } 5453 5454 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n", 5455 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE), 5456 thermal_read_mode); 5457 5458 switch (thermal_read_mode) { 5459 case TPACPI_THERMAL_TPEC_16: 5460 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj, 5461 &thermal_temp_input16_group); 5462 if (res) 5463 return res; 5464 break; 5465 case TPACPI_THERMAL_TPEC_8: 5466 case TPACPI_THERMAL_ACPI_TMP07: 5467 case TPACPI_THERMAL_ACPI_UPDT: 5468 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj, 5469 &thermal_temp_input8_group); 5470 if (res) 5471 return res; 5472 break; 5473 case TPACPI_THERMAL_NONE: 5474 default: 5475 return 1; 5476 } 5477 5478 return 0; 5479} 5480 5481static void thermal_exit(void) 5482{ 5483 switch (thermal_read_mode) { 5484 case TPACPI_THERMAL_TPEC_16: 5485 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, 5486 &thermal_temp_input16_group); 5487 break; 5488 case TPACPI_THERMAL_TPEC_8: 5489 case TPACPI_THERMAL_ACPI_TMP07: 5490 case TPACPI_THERMAL_ACPI_UPDT: 5491 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, 5492 &thermal_temp_input16_group); 5493 break; 5494 case TPACPI_THERMAL_NONE: 5495 default: 5496 break; 5497 } 5498} 5499 5500static int thermal_read(char *p) 5501{ 5502 int len = 0; 5503 int n, i; 5504 struct ibm_thermal_sensors_struct t; 5505 5506 n = thermal_get_sensors(&t); 5507 if (unlikely(n < 0)) 5508 return n; 5509 5510 len += sprintf(p + len, "temperatures:\t"); 5511 5512 if (n > 0) { 5513 for (i = 0; i < (n - 1); i++) 5514 len += sprintf(p + len, "%d ", t.temp[i] / 1000); 5515 len += sprintf(p + len, "%d\n", t.temp[i] / 1000); 5516 } else 5517 len += sprintf(p + len, "not supported\n"); 5518 5519 return len; 5520} 5521 5522static struct ibm_struct thermal_driver_data = { 5523 .name = "thermal", 5524 .read = thermal_read, 5525 .exit = thermal_exit, 5526}; 5527 5528/************************************************************************* 5529 * EC Dump subdriver 5530 */ 5531 5532static u8 ecdump_regs[256]; 5533 5534static int ecdump_read(char *p) 5535{ 5536 int len = 0; 5537 int i, j; 5538 u8 v; 5539 5540 len += sprintf(p + len, "EC " 5541 " +00 +01 +02 +03 +04 +05 +06 +07" 5542 " +08 +09 +0a +0b +0c +0d +0e +0f\n"); 5543 for (i = 0; i < 256; i += 16) { 5544 len += sprintf(p + len, "EC 0x%02x:", i); 5545 for (j = 0; j < 16; j++) { 5546 if (!acpi_ec_read(i + j, &v)) 5547 break; 5548 if (v != ecdump_regs[i + j]) 5549 len += sprintf(p + len, " *%02x", v); 5550 else 5551 len += sprintf(p + len, " %02x", v); 5552 ecdump_regs[i + j] = v; 5553 } 5554 len += sprintf(p + len, "\n"); 5555 if (j != 16) 5556 break; 5557 } 5558 5559 /* These are way too dangerous to advertise openly... */ 5560#if 0 5561 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>" 5562 " (<offset> is 00-ff, <value> is 00-ff)\n"); 5563 len += sprintf(p + len, "commands:\t0x<offset> <value> " 5564 " (<offset> is 00-ff, <value> is 0-255)\n"); 5565#endif 5566 return len; 5567} 5568 5569static int ecdump_write(char *buf) 5570{ 5571 char *cmd; 5572 int i, v; 5573 5574 while ((cmd = next_cmd(&buf))) { 5575 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) { 5576 /* i and v set */ 5577 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) { 5578 /* i and v set */ 5579 } else 5580 return -EINVAL; 5581 if (i >= 0 && i < 256 && v >= 0 && v < 256) { 5582 if (!acpi_ec_write(i, v)) 5583 return -EIO; 5584 } else 5585 return -EINVAL; 5586 } 5587 5588 return 0; 5589} 5590 5591static struct ibm_struct ecdump_driver_data = { 5592 .name = "ecdump", 5593 .read = ecdump_read, 5594 .write = ecdump_write, 5595 .flags.experimental = 1, 5596}; 5597 5598/************************************************************************* 5599 * Backlight/brightness subdriver 5600 */ 5601 5602#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen" 5603 5604/* 5605 * ThinkPads can read brightness from two places: EC HBRV (0x31), or 5606 * CMOS NVRAM byte 0x5E, bits 0-3. 5607 * 5608 * EC HBRV (0x31) has the following layout 5609 * Bit 7: unknown function 5610 * Bit 6: unknown function 5611 * Bit 5: Z: honour scale changes, NZ: ignore scale changes 5612 * Bit 4: must be set to zero to avoid problems 5613 * Bit 3-0: backlight brightness level 5614 * 5615 * brightness_get_raw returns status data in the HBRV layout 5616 * 5617 * WARNING: The X61 has been verified to use HBRV for something else, so 5618 * this should be used _only_ on IBM ThinkPads, and maybe with some careful 5619 * testing on the very early *60 Lenovo models... 5620 */ 5621 5622enum { 5623 TP_EC_BACKLIGHT = 0x31, 5624 5625 /* TP_EC_BACKLIGHT bitmasks */ 5626 TP_EC_BACKLIGHT_LVLMSK = 0x1F, 5627 TP_EC_BACKLIGHT_CMDMSK = 0xE0, 5628 TP_EC_BACKLIGHT_MAPSW = 0x20, 5629}; 5630 5631enum tpacpi_brightness_access_mode { 5632 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */ 5633 TPACPI_BRGHT_MODE_EC, /* EC control */ 5634 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */ 5635 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */ 5636 TPACPI_BRGHT_MODE_MAX 5637}; 5638 5639static struct backlight_device *ibm_backlight_device; 5640 5641static enum tpacpi_brightness_access_mode brightness_mode = 5642 TPACPI_BRGHT_MODE_MAX; 5643 5644static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */ 5645 5646static struct mutex brightness_mutex; 5647 5648/* NVRAM brightness access, 5649 * call with brightness_mutex held! */ 5650static unsigned int tpacpi_brightness_nvram_get(void) 5651{ 5652 u8 lnvram; 5653 5654 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS) 5655 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS) 5656 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS; 5657 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07; 5658 5659 return lnvram; 5660} 5661 5662static void tpacpi_brightness_checkpoint_nvram(void) 5663{ 5664 u8 lec = 0; 5665 u8 b_nvram; 5666 5667 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM) 5668 return; 5669 5670 vdbg_printk(TPACPI_DBG_BRGHT, 5671 "trying to checkpoint backlight level to NVRAM...\n"); 5672 5673 if (mutex_lock_killable(&brightness_mutex) < 0) 5674 return; 5675 5676 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec))) 5677 goto unlock; 5678 lec &= TP_EC_BACKLIGHT_LVLMSK; 5679 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS); 5680 5681 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS) 5682 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) { 5683 /* NVRAM needs update */ 5684 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS << 5685 TP_NVRAM_POS_LEVEL_BRIGHTNESS); 5686 b_nvram |= lec; 5687 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS); 5688 dbg_printk(TPACPI_DBG_BRGHT, 5689 "updated NVRAM backlight level to %u (0x%02x)\n", 5690 (unsigned int) lec, (unsigned int) b_nvram); 5691 } else 5692 vdbg_printk(TPACPI_DBG_BRGHT, 5693 "NVRAM backlight level already is %u (0x%02x)\n", 5694 (unsigned int) lec, (unsigned int) b_nvram); 5695 5696unlock: 5697 mutex_unlock(&brightness_mutex); 5698} 5699 5700 5701/* call with brightness_mutex held! */ 5702static int tpacpi_brightness_get_raw(int *status) 5703{ 5704 u8 lec = 0; 5705 5706 switch (brightness_mode) { 5707 case TPACPI_BRGHT_MODE_UCMS_STEP: 5708 *status = tpacpi_brightness_nvram_get(); 5709 return 0; 5710 case TPACPI_BRGHT_MODE_EC: 5711 case TPACPI_BRGHT_MODE_ECNVRAM: 5712 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec))) 5713 return -EIO; 5714 *status = lec; 5715 return 0; 5716 default: 5717 return -ENXIO; 5718 } 5719} 5720 5721/* call with brightness_mutex held! */ 5722/* do NOT call with illegal backlight level value */ 5723static int tpacpi_brightness_set_ec(unsigned int value) 5724{ 5725 u8 lec = 0; 5726 5727 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec))) 5728 return -EIO; 5729 5730 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT, 5731 (lec & TP_EC_BACKLIGHT_CMDMSK) | 5732 (value & TP_EC_BACKLIGHT_LVLMSK)))) 5733 return -EIO; 5734 5735 return 0; 5736} 5737 5738/* call with brightness_mutex held! */ 5739static int tpacpi_brightness_set_ucmsstep(unsigned int value) 5740{ 5741 int cmos_cmd, inc; 5742 unsigned int current_value, i; 5743 5744 current_value = tpacpi_brightness_nvram_get(); 5745 5746 if (value == current_value) 5747 return 0; 5748 5749 cmos_cmd = (value > current_value) ? 5750 TP_CMOS_BRIGHTNESS_UP : 5751 TP_CMOS_BRIGHTNESS_DOWN; 5752 inc = (value > current_value) ? 1 : -1; 5753 5754 for (i = current_value; i != value; i += inc) 5755 if (issue_thinkpad_cmos_command(cmos_cmd)) 5756 return -EIO; 5757 5758 return 0; 5759} 5760 5761/* May return EINTR which can always be mapped to ERESTARTSYS */ 5762static int brightness_set(unsigned int value) 5763{ 5764 int res; 5765 5766 if (value > ((tp_features.bright_16levels)? 15 : 7) || 5767 value < 0) 5768 return -EINVAL; 5769 5770 vdbg_printk(TPACPI_DBG_BRGHT, 5771 "set backlight level to %d\n", value); 5772 5773 res = mutex_lock_killable(&brightness_mutex); 5774 if (res < 0) 5775 return res; 5776 5777 switch (brightness_mode) { 5778 case TPACPI_BRGHT_MODE_EC: 5779 case TPACPI_BRGHT_MODE_ECNVRAM: 5780 res = tpacpi_brightness_set_ec(value); 5781 break; 5782 case TPACPI_BRGHT_MODE_UCMS_STEP: 5783 res = tpacpi_brightness_set_ucmsstep(value); 5784 break; 5785 default: 5786 res = -ENXIO; 5787 } 5788 5789 mutex_unlock(&brightness_mutex); 5790 return res; 5791} 5792 5793/* sysfs backlight class ----------------------------------------------- */ 5794 5795static int brightness_update_status(struct backlight_device *bd) 5796{ 5797 unsigned int level = 5798 (bd->props.fb_blank == FB_BLANK_UNBLANK && 5799 bd->props.power == FB_BLANK_UNBLANK) ? 5800 bd->props.brightness : 0; 5801 5802 dbg_printk(TPACPI_DBG_BRGHT, 5803 "backlight: attempt to set level to %d\n", 5804 level); 5805 5806 /* it is the backlight class's job (caller) to handle 5807 * EINTR and other errors properly */ 5808 return brightness_set(level); 5809} 5810 5811static int brightness_get(struct backlight_device *bd) 5812{ 5813 int status, res; 5814 5815 res = mutex_lock_killable(&brightness_mutex); 5816 if (res < 0) 5817 return 0; 5818 5819 res = tpacpi_brightness_get_raw(&status); 5820 5821 mutex_unlock(&brightness_mutex); 5822 5823 if (res < 0) 5824 return 0; 5825 5826 return status & TP_EC_BACKLIGHT_LVLMSK; 5827} 5828 5829static struct backlight_ops ibm_backlight_data = { 5830 .get_brightness = brightness_get, 5831 .update_status = brightness_update_status, 5832}; 5833 5834/* --------------------------------------------------------------------- */ 5835 5836/* 5837 * These are only useful for models that have only one possibility 5838 * of GPU. If the BIOS model handles both ATI and Intel, don't use 5839 * these quirks. 5840 */ 5841#define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */ 5842#define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */ 5843#define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */ 5844 5845static const struct tpacpi_quirk brightness_quirk_table[] __initconst = { 5846 /* Models with ATI GPUs known to require ECNVRAM mode */ 5847 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */ 5848 5849 /* Models with ATI GPUs that can use ECNVRAM */ 5850 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), 5851 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC), 5852 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC), 5853 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC), 5854 5855 /* Models with Intel Extreme Graphics 2 */ 5856 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), 5857 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC), 5858 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC), 5859 5860 /* Models with Intel GMA900 */ 5861 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */ 5862 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */ 5863 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */ 5864}; 5865 5866static int __init brightness_init(struct ibm_init_struct *iibm) 5867{ 5868 int b; 5869 unsigned long quirks; 5870 5871 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n"); 5872 5873 mutex_init(&brightness_mutex); 5874 5875 quirks = tpacpi_check_quirks(brightness_quirk_table, 5876 ARRAY_SIZE(brightness_quirk_table)); 5877 5878 /* 5879 * We always attempt to detect acpi support, so as to switch 5880 * Lenovo Vista BIOS to ACPI brightness mode even if we are not 5881 * going to publish a backlight interface 5882 */ 5883 b = tpacpi_check_std_acpi_brightness_support(); 5884 if (b > 0) { 5885 5886 if (acpi_video_backlight_support()) { 5887 if (brightness_enable > 1) { 5888 printk(TPACPI_NOTICE 5889 "Standard ACPI backlight interface " 5890 "available, not loading native one.\n"); 5891 return 1; 5892 } else if (brightness_enable == 1) { 5893 printk(TPACPI_NOTICE 5894 "Backlight control force enabled, even if standard " 5895 "ACPI backlight interface is available\n"); 5896 } 5897 } else { 5898 if (brightness_enable > 1) { 5899 printk(TPACPI_NOTICE 5900 "Standard ACPI backlight interface not " 5901 "available, thinkpad_acpi native " 5902 "brightness control enabled\n"); 5903 } 5904 } 5905 } 5906 5907 if (!brightness_enable) { 5908 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT, 5909 "brightness support disabled by " 5910 "module parameter\n"); 5911 return 1; 5912 } 5913 5914 if (b > 16) { 5915 printk(TPACPI_ERR 5916 "Unsupported brightness interface, " 5917 "please contact %s\n", TPACPI_MAIL); 5918 return 1; 5919 } 5920 if (b == 16) 5921 tp_features.bright_16levels = 1; 5922 5923 /* 5924 * Check for module parameter bogosity, note that we 5925 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be 5926 * able to detect "unspecified" 5927 */ 5928 if (brightness_mode > TPACPI_BRGHT_MODE_MAX) 5929 return -EINVAL; 5930 5931 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */ 5932 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO || 5933 brightness_mode == TPACPI_BRGHT_MODE_MAX) { 5934 if (quirks & TPACPI_BRGHT_Q_EC) 5935 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM; 5936 else 5937 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP; 5938 5939 dbg_printk(TPACPI_DBG_BRGHT, 5940 "driver auto-selected brightness_mode=%d\n", 5941 brightness_mode); 5942 } 5943 5944 /* Safety */ 5945 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM && 5946 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM || 5947 brightness_mode == TPACPI_BRGHT_MODE_EC)) 5948 return -EINVAL; 5949 5950 if (tpacpi_brightness_get_raw(&b) < 0) 5951 return 1; 5952 5953 if (tp_features.bright_16levels) 5954 printk(TPACPI_INFO 5955 "detected a 16-level brightness capable ThinkPad\n"); 5956 5957 ibm_backlight_device = backlight_device_register( 5958 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL, 5959 &ibm_backlight_data); 5960 if (IS_ERR(ibm_backlight_device)) { 5961 printk(TPACPI_ERR "Could not register backlight device\n"); 5962 return PTR_ERR(ibm_backlight_device); 5963 } 5964 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT, 5965 "brightness is supported\n"); 5966 5967 if (quirks & TPACPI_BRGHT_Q_ASK) { 5968 printk(TPACPI_NOTICE 5969 "brightness: will use unverified default: " 5970 "brightness_mode=%d\n", brightness_mode); 5971 printk(TPACPI_NOTICE 5972 "brightness: please report to %s whether it works well " 5973 "or not on your ThinkPad\n", TPACPI_MAIL); 5974 } 5975 5976 ibm_backlight_device->props.max_brightness = 5977 (tp_features.bright_16levels)? 15 : 7; 5978 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK; 5979 backlight_update_status(ibm_backlight_device); 5980 5981 return 0; 5982} 5983 5984static void brightness_suspend(pm_message_t state) 5985{ 5986 tpacpi_brightness_checkpoint_nvram(); 5987} 5988 5989static void brightness_shutdown(void) 5990{ 5991 tpacpi_brightness_checkpoint_nvram(); 5992} 5993 5994static void brightness_exit(void) 5995{ 5996 if (ibm_backlight_device) { 5997 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT, 5998 "calling backlight_device_unregister()\n"); 5999 backlight_device_unregister(ibm_backlight_device); 6000 } 6001 6002 tpacpi_brightness_checkpoint_nvram(); 6003} 6004 6005static int brightness_read(char *p) 6006{ 6007 int len = 0; 6008 int level; 6009 6010 level = brightness_get(NULL); 6011 if (level < 0) { 6012 len += sprintf(p + len, "level:\t\tunreadable\n"); 6013 } else { 6014 len += sprintf(p + len, "level:\t\t%d\n", level); 6015 len += sprintf(p + len, "commands:\tup, down\n"); 6016 len += sprintf(p + len, "commands:\tlevel <level>" 6017 " (<level> is 0-%d)\n", 6018 (tp_features.bright_16levels) ? 15 : 7); 6019 } 6020 6021 return len; 6022} 6023 6024static int brightness_write(char *buf) 6025{ 6026 int level; 6027 int rc; 6028 char *cmd; 6029 int max_level = (tp_features.bright_16levels) ? 15 : 7; 6030 6031 level = brightness_get(NULL); 6032 if (level < 0) 6033 return level; 6034 6035 while ((cmd = next_cmd(&buf))) { 6036 if (strlencmp(cmd, "up") == 0) { 6037 if (level < max_level) 6038 level++; 6039 } else if (strlencmp(cmd, "down") == 0) { 6040 if (level > 0) 6041 level--; 6042 } else if (sscanf(cmd, "level %d", &level) == 1 && 6043 level >= 0 && level <= max_level) { 6044 /* new level set */ 6045 } else 6046 return -EINVAL; 6047 } 6048 6049 tpacpi_disclose_usertask("procfs brightness", 6050 "set level to %d\n", level); 6051 6052 /* 6053 * Now we know what the final level should be, so we try to set it. 6054 * Doing it this way makes the syscall restartable in case of EINTR 6055 */ 6056 rc = brightness_set(level); 6057 return (rc == -EINTR)? ERESTARTSYS : rc; 6058} 6059 6060static struct ibm_struct brightness_driver_data = { 6061 .name = "brightness", 6062 .read = brightness_read, 6063 .write = brightness_write, 6064 .exit = brightness_exit, 6065 .suspend = brightness_suspend, 6066 .shutdown = brightness_shutdown, 6067}; 6068 6069/************************************************************************* 6070 * Volume subdriver 6071 */ 6072 6073static int volume_offset = 0x30; 6074 6075static int volume_read(char *p) 6076{ 6077 int len = 0; 6078 u8 level; 6079 6080 if (!acpi_ec_read(volume_offset, &level)) { 6081 len += sprintf(p + len, "level:\t\tunreadable\n"); 6082 } else { 6083 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf); 6084 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6)); 6085 len += sprintf(p + len, "commands:\tup, down, mute\n"); 6086 len += sprintf(p + len, "commands:\tlevel <level>" 6087 " (<level> is 0-15)\n"); 6088 } 6089 6090 return len; 6091} 6092 6093static int volume_write(char *buf) 6094{ 6095 int cmos_cmd, inc, i; 6096 u8 level, mute; 6097 int new_level, new_mute; 6098 char *cmd; 6099 6100 while ((cmd = next_cmd(&buf))) { 6101 if (!acpi_ec_read(volume_offset, &level)) 6102 return -EIO; 6103 new_mute = mute = level & 0x40; 6104 new_level = level = level & 0xf; 6105 6106 if (strlencmp(cmd, "up") == 0) { 6107 if (mute) 6108 new_mute = 0; 6109 else 6110 new_level = level == 15 ? 15 : level + 1; 6111 } else if (strlencmp(cmd, "down") == 0) { 6112 if (mute) 6113 new_mute = 0; 6114 else 6115 new_level = level == 0 ? 0 : level - 1; 6116 } else if (sscanf(cmd, "level %d", &new_level) == 1 && 6117 new_level >= 0 && new_level <= 15) { 6118 /* new_level set */ 6119 } else if (strlencmp(cmd, "mute") == 0) { 6120 new_mute = 0x40; 6121 } else 6122 return -EINVAL; 6123 6124 if (new_level != level) { 6125 /* mute doesn't change */ 6126 6127 cmos_cmd = (new_level > level) ? 6128 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN; 6129 inc = new_level > level ? 1 : -1; 6130 6131 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) || 6132 !acpi_ec_write(volume_offset, level))) 6133 return -EIO; 6134 6135 for (i = level; i != new_level; i += inc) 6136 if (issue_thinkpad_cmos_command(cmos_cmd) || 6137 !acpi_ec_write(volume_offset, i + inc)) 6138 return -EIO; 6139 6140 if (mute && 6141 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) || 6142 !acpi_ec_write(volume_offset, new_level + mute))) { 6143 return -EIO; 6144 } 6145 } 6146 6147 if (new_mute != mute) { 6148 /* level doesn't change */ 6149 6150 cmos_cmd = (new_mute) ? 6151 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP; 6152 6153 if (issue_thinkpad_cmos_command(cmos_cmd) || 6154 !acpi_ec_write(volume_offset, level + new_mute)) 6155 return -EIO; 6156 } 6157 } 6158 6159 return 0; 6160} 6161 6162static struct ibm_struct volume_driver_data = { 6163 .name = "volume", 6164 .read = volume_read, 6165 .write = volume_write, 6166}; 6167 6168/************************************************************************* 6169 * Fan subdriver 6170 */ 6171 6172/* 6173 * FAN ACCESS MODES 6174 * 6175 * TPACPI_FAN_RD_ACPI_GFAN: 6176 * ACPI GFAN method: returns fan level 6177 * 6178 * see TPACPI_FAN_WR_ACPI_SFAN 6179 * EC 0x2f (HFSP) not available if GFAN exists 6180 * 6181 * TPACPI_FAN_WR_ACPI_SFAN: 6182 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max) 6183 * 6184 * EC 0x2f (HFSP) might be available *for reading*, but do not use 6185 * it for writing. 6186 * 6187 * TPACPI_FAN_WR_TPEC: 6188 * ThinkPad EC register 0x2f (HFSP): fan control loop mode 6189 * Supported on almost all ThinkPads 6190 * 6191 * Fan speed changes of any sort (including those caused by the 6192 * disengaged mode) are usually done slowly by the firmware as the 6193 * maximum ammount of fan duty cycle change per second seems to be 6194 * limited. 6195 * 6196 * Reading is not available if GFAN exists. 6197 * Writing is not available if SFAN exists. 6198 * 6199 * Bits 6200 * 7 automatic mode engaged; 6201 * (default operation mode of the ThinkPad) 6202 * fan level is ignored in this mode. 6203 * 6 full speed mode (takes precedence over bit 7); 6204 * not available on all thinkpads. May disable 6205 * the tachometer while the fan controller ramps up 6206 * the speed (which can take up to a few *minutes*). 6207 * Speeds up fan to 100% duty-cycle, which is far above 6208 * the standard RPM levels. It is not impossible that 6209 * it could cause hardware damage. 6210 * 5-3 unused in some models. Extra bits for fan level 6211 * in others, but still useless as all values above 6212 * 7 map to the same speed as level 7 in these models. 6213 * 2-0 fan level (0..7 usually) 6214 * 0x00 = stop 6215 * 0x07 = max (set when temperatures critical) 6216 * Some ThinkPads may have other levels, see 6217 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41) 6218 * 6219 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at 6220 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT 6221 * does so, its initial value is meaningless (0x07). 6222 * 6223 * For firmware bugs, refer to: 6224 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues 6225 * 6226 * ---- 6227 * 6228 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB): 6229 * Main fan tachometer reading (in RPM) 6230 * 6231 * This register is present on all ThinkPads with a new-style EC, and 6232 * it is known not to be present on the A21m/e, and T22, as there is 6233 * something else in offset 0x84 according to the ACPI DSDT. Other 6234 * ThinkPads from this same time period (and earlier) probably lack the 6235 * tachometer as well. 6236 * 6237 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware 6238 * was never fixed by IBM to report the EC firmware version string 6239 * probably support the tachometer (like the early X models), so 6240 * detecting it is quite hard. We need more data to know for sure. 6241 * 6242 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings 6243 * might result. 6244 * 6245 * FIRMWARE BUG: may go stale while the EC is switching to full speed 6246 * mode. 6247 * 6248 * For firmware bugs, refer to: 6249 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues 6250 * 6251 * ---- 6252 * 6253 * ThinkPad EC register 0x31 bit 0 (only on select models) 6254 * 6255 * When bit 0 of EC register 0x31 is zero, the tachometer registers 6256 * show the speed of the main fan. When bit 0 of EC register 0x31 6257 * is one, the tachometer registers show the speed of the auxiliary 6258 * fan. 6259 * 6260 * Fan control seems to affect both fans, regardless of the state 6261 * of this bit. 6262 * 6263 * So far, only the firmware for the X60/X61 non-tablet versions 6264 * seem to support this (firmware TP-7M). 6265 * 6266 * TPACPI_FAN_WR_ACPI_FANS: 6267 * ThinkPad X31, X40, X41. Not available in the X60. 6268 * 6269 * FANS ACPI handle: takes three arguments: low speed, medium speed, 6270 * high speed. ACPI DSDT seems to map these three speeds to levels 6271 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH 6272 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3") 6273 * 6274 * The speeds are stored on handles 6275 * (FANA:FAN9), (FANC:FANB), (FANE:FAND). 6276 * 6277 * There are three default speed sets, acessible as handles: 6278 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H 6279 * 6280 * ACPI DSDT switches which set is in use depending on various 6281 * factors. 6282 * 6283 * TPACPI_FAN_WR_TPEC is also available and should be used to 6284 * command the fan. The X31/X40/X41 seems to have 8 fan levels, 6285 * but the ACPI tables just mention level 7. 6286 */ 6287 6288enum { /* Fan control constants */ 6289 fan_status_offset = 0x2f, /* EC register 0x2f */ 6290 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM) 6291 * 0x84 must be read before 0x85 */ 6292 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M) 6293 bit 0 selects which fan is active */ 6294 6295 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */ 6296 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */ 6297 6298 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */ 6299}; 6300 6301enum fan_status_access_mode { 6302 TPACPI_FAN_NONE = 0, /* No fan status or control */ 6303 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */ 6304 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */ 6305}; 6306 6307enum fan_control_access_mode { 6308 TPACPI_FAN_WR_NONE = 0, /* No fan control */ 6309 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */ 6310 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */ 6311 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */ 6312}; 6313 6314enum fan_control_commands { 6315 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */ 6316 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */ 6317 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd, 6318 * and also watchdog cmd */ 6319}; 6320 6321static int fan_control_allowed; 6322 6323static enum fan_status_access_mode fan_status_access_mode; 6324static enum fan_control_access_mode fan_control_access_mode; 6325static enum fan_control_commands fan_control_commands; 6326 6327static u8 fan_control_initial_status; 6328static u8 fan_control_desired_level; 6329static u8 fan_control_resume_level; 6330static int fan_watchdog_maxinterval; 6331 6332static struct mutex fan_mutex; 6333 6334static void fan_watchdog_fire(struct work_struct *ignored); 6335static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire); 6336 6337TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */ 6338TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */ 6339 "\\FSPD", /* 600e/x, 770e, 770x */ 6340 ); /* all others */ 6341TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */ 6342 "JFNS", /* 770x-JL */ 6343 ); /* all others */ 6344 6345/* 6346 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the 6347 * HFSP register at boot, so it contains 0x07 but the Thinkpad could 6348 * be in auto mode (0x80). 6349 * 6350 * This is corrected by any write to HFSP either by the driver, or 6351 * by the firmware. 6352 * 6353 * We assume 0x07 really means auto mode while this quirk is active, 6354 * as this is far more likely than the ThinkPad being in level 7, 6355 * which is only used by the firmware during thermal emergencies. 6356 * 6357 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52), 6358 * TP-70 (T43, R52), which are known to be buggy. 6359 */ 6360 6361static void fan_quirk1_setup(void) 6362{ 6363 if (fan_control_initial_status == 0x07) { 6364 printk(TPACPI_NOTICE 6365 "fan_init: initial fan status is unknown, " 6366 "assuming it is in auto mode\n"); 6367 tp_features.fan_ctrl_status_undef = 1; 6368 } 6369} 6370 6371static void fan_quirk1_handle(u8 *fan_status) 6372{ 6373 if (unlikely(tp_features.fan_ctrl_status_undef)) { 6374 if (*fan_status != fan_control_initial_status) { 6375 /* something changed the HFSP regisnter since 6376 * driver init time, so it is not undefined 6377 * anymore */ 6378 tp_features.fan_ctrl_status_undef = 0; 6379 } else { 6380 /* Return most likely status. In fact, it 6381 * might be the only possible status */ 6382 *fan_status = TP_EC_FAN_AUTO; 6383 } 6384 } 6385} 6386 6387/* Select main fan on X60/X61, NOOP on others */ 6388static bool fan_select_fan1(void) 6389{ 6390 if (tp_features.second_fan) { 6391 u8 val; 6392 6393 if (ec_read(fan_select_offset, &val) < 0) 6394 return false; 6395 val &= 0xFEU; 6396 if (ec_write(fan_select_offset, val) < 0) 6397 return false; 6398 } 6399 return true; 6400} 6401 6402/* Select secondary fan on X60/X61 */ 6403static bool fan_select_fan2(void) 6404{ 6405 u8 val; 6406 6407 if (!tp_features.second_fan) 6408 return false; 6409 6410 if (ec_read(fan_select_offset, &val) < 0) 6411 return false; 6412 val |= 0x01U; 6413 if (ec_write(fan_select_offset, val) < 0) 6414 return false; 6415 6416 return true; 6417} 6418 6419/* 6420 * Call with fan_mutex held 6421 */ 6422static void fan_update_desired_level(u8 status) 6423{ 6424 if ((status & 6425 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) { 6426 if (status > 7) 6427 fan_control_desired_level = 7; 6428 else 6429 fan_control_desired_level = status; 6430 } 6431} 6432 6433static int fan_get_status(u8 *status) 6434{ 6435 u8 s; 6436 6437 /* TODO: 6438 * Add TPACPI_FAN_RD_ACPI_FANS ? */ 6439 6440 switch (fan_status_access_mode) { 6441 case TPACPI_FAN_RD_ACPI_GFAN: 6442 /* 570, 600e/x, 770e, 770x */ 6443 6444 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d"))) 6445 return -EIO; 6446 6447 if (likely(status)) 6448 *status = s & 0x07; 6449 6450 break; 6451 6452 case TPACPI_FAN_RD_TPEC: 6453 /* all except 570, 600e/x, 770e, 770x */ 6454 if (unlikely(!acpi_ec_read(fan_status_offset, &s))) 6455 return -EIO; 6456 6457 if (likely(status)) { 6458 *status = s; 6459 fan_quirk1_handle(status); 6460 } 6461 6462 break; 6463 6464 default: 6465 return -ENXIO; 6466 } 6467 6468 return 0; 6469} 6470 6471static int fan_get_status_safe(u8 *status) 6472{ 6473 int rc; 6474 u8 s; 6475 6476 if (mutex_lock_killable(&fan_mutex)) 6477 return -ERESTARTSYS; 6478 rc = fan_get_status(&s); 6479 if (!rc) 6480 fan_update_desired_level(s); 6481 mutex_unlock(&fan_mutex); 6482 6483 if (status) 6484 *status = s; 6485 6486 return rc; 6487} 6488 6489static int fan_get_speed(unsigned int *speed) 6490{ 6491 u8 hi, lo; 6492 6493 switch (fan_status_access_mode) { 6494 case TPACPI_FAN_RD_TPEC: 6495 /* all except 570, 600e/x, 770e, 770x */ 6496 if (unlikely(!fan_select_fan1())) 6497 return -EIO; 6498 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) || 6499 !acpi_ec_read(fan_rpm_offset + 1, &hi))) 6500 return -EIO; 6501 6502 if (likely(speed)) 6503 *speed = (hi << 8) | lo; 6504 6505 break; 6506 6507 default: 6508 return -ENXIO; 6509 } 6510 6511 return 0; 6512} 6513 6514static int fan2_get_speed(unsigned int *speed) 6515{ 6516 u8 hi, lo; 6517 bool rc; 6518 6519 switch (fan_status_access_mode) { 6520 case TPACPI_FAN_RD_TPEC: 6521 /* all except 570, 600e/x, 770e, 770x */ 6522 if (unlikely(!fan_select_fan2())) 6523 return -EIO; 6524 rc = !acpi_ec_read(fan_rpm_offset, &lo) || 6525 !acpi_ec_read(fan_rpm_offset + 1, &hi); 6526 fan_select_fan1(); /* play it safe */ 6527 if (rc) 6528 return -EIO; 6529 6530 if (likely(speed)) 6531 *speed = (hi << 8) | lo; 6532 6533 break; 6534 6535 default: 6536 return -ENXIO; 6537 } 6538 6539 return 0; 6540} 6541 6542static int fan_set_level(int level) 6543{ 6544 if (!fan_control_allowed) 6545 return -EPERM; 6546 6547 switch (fan_control_access_mode) { 6548 case TPACPI_FAN_WR_ACPI_SFAN: 6549 if (level >= 0 && level <= 7) { 6550 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level)) 6551 return -EIO; 6552 } else 6553 return -EINVAL; 6554 break; 6555 6556 case TPACPI_FAN_WR_ACPI_FANS: 6557 case TPACPI_FAN_WR_TPEC: 6558 if (!(level & TP_EC_FAN_AUTO) && 6559 !(level & TP_EC_FAN_FULLSPEED) && 6560 ((level < 0) || (level > 7))) 6561 return -EINVAL; 6562 6563 /* safety net should the EC not support AUTO 6564 * or FULLSPEED mode bits and just ignore them */ 6565 if (level & TP_EC_FAN_FULLSPEED) 6566 level |= 7; /* safety min speed 7 */ 6567 else if (level & TP_EC_FAN_AUTO) 6568 level |= 4; /* safety min speed 4 */ 6569 6570 if (!acpi_ec_write(fan_status_offset, level)) 6571 return -EIO; 6572 else 6573 tp_features.fan_ctrl_status_undef = 0; 6574 break; 6575 6576 default: 6577 return -ENXIO; 6578 } 6579 6580 vdbg_printk(TPACPI_DBG_FAN, 6581 "fan control: set fan control register to 0x%02x\n", level); 6582 return 0; 6583} 6584 6585static int fan_set_level_safe(int level) 6586{ 6587 int rc; 6588 6589 if (!fan_control_allowed) 6590 return -EPERM; 6591 6592 if (mutex_lock_killable(&fan_mutex)) 6593 return -ERESTARTSYS; 6594 6595 if (level == TPACPI_FAN_LAST_LEVEL) 6596 level = fan_control_desired_level; 6597 6598 rc = fan_set_level(level); 6599 if (!rc) 6600 fan_update_desired_level(level); 6601 6602 mutex_unlock(&fan_mutex); 6603 return rc; 6604} 6605 6606static int fan_set_enable(void) 6607{ 6608 u8 s; 6609 int rc; 6610 6611 if (!fan_control_allowed) 6612 return -EPERM; 6613 6614 if (mutex_lock_killable(&fan_mutex)) 6615 return -ERESTARTSYS; 6616 6617 switch (fan_control_access_mode) { 6618 case TPACPI_FAN_WR_ACPI_FANS: 6619 case TPACPI_FAN_WR_TPEC: 6620 rc = fan_get_status(&s); 6621 if (rc < 0) 6622 break; 6623 6624 /* Don't go out of emergency fan mode */ 6625 if (s != 7) { 6626 s &= 0x07; 6627 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */ 6628 } 6629 6630 if (!acpi_ec_write(fan_status_offset, s)) 6631 rc = -EIO; 6632 else { 6633 tp_features.fan_ctrl_status_undef = 0; 6634 rc = 0; 6635 } 6636 break; 6637 6638 case TPACPI_FAN_WR_ACPI_SFAN: 6639 rc = fan_get_status(&s); 6640 if (rc < 0) 6641 break; 6642 6643 s &= 0x07; 6644 6645 /* Set fan to at least level 4 */ 6646 s |= 4; 6647 6648 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s)) 6649 rc = -EIO; 6650 else 6651 rc = 0; 6652 break; 6653 6654 default: 6655 rc = -ENXIO; 6656 } 6657 6658 mutex_unlock(&fan_mutex); 6659 6660 if (!rc) 6661 vdbg_printk(TPACPI_DBG_FAN, 6662 "fan control: set fan control register to 0x%02x\n", 6663 s); 6664 return rc; 6665} 6666 6667static int fan_set_disable(void) 6668{ 6669 int rc; 6670 6671 if (!fan_control_allowed) 6672 return -EPERM; 6673 6674 if (mutex_lock_killable(&fan_mutex)) 6675 return -ERESTARTSYS; 6676 6677 rc = 0; 6678 switch (fan_control_access_mode) { 6679 case TPACPI_FAN_WR_ACPI_FANS: 6680 case TPACPI_FAN_WR_TPEC: 6681 if (!acpi_ec_write(fan_status_offset, 0x00)) 6682 rc = -EIO; 6683 else { 6684 fan_control_desired_level = 0; 6685 tp_features.fan_ctrl_status_undef = 0; 6686 } 6687 break; 6688 6689 case TPACPI_FAN_WR_ACPI_SFAN: 6690 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00)) 6691 rc = -EIO; 6692 else 6693 fan_control_desired_level = 0; 6694 break; 6695 6696 default: 6697 rc = -ENXIO; 6698 } 6699 6700 if (!rc) 6701 vdbg_printk(TPACPI_DBG_FAN, 6702 "fan control: set fan control register to 0\n"); 6703 6704 mutex_unlock(&fan_mutex); 6705 return rc; 6706} 6707 6708static int fan_set_speed(int speed) 6709{ 6710 int rc; 6711 6712 if (!fan_control_allowed) 6713 return -EPERM; 6714 6715 if (mutex_lock_killable(&fan_mutex)) 6716 return -ERESTARTSYS; 6717 6718 rc = 0; 6719 switch (fan_control_access_mode) { 6720 case TPACPI_FAN_WR_ACPI_FANS: 6721 if (speed >= 0 && speed <= 65535) { 6722 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd", 6723 speed, speed, speed)) 6724 rc = -EIO; 6725 } else 6726 rc = -EINVAL; 6727 break; 6728 6729 default: 6730 rc = -ENXIO; 6731 } 6732 6733 mutex_unlock(&fan_mutex); 6734 return rc; 6735} 6736 6737static void fan_watchdog_reset(void) 6738{ 6739 static int fan_watchdog_active; 6740 6741 if (fan_control_access_mode == TPACPI_FAN_WR_NONE) 6742 return; 6743 6744 if (fan_watchdog_active) 6745 cancel_delayed_work(&fan_watchdog_task); 6746 6747 if (fan_watchdog_maxinterval > 0 && 6748 tpacpi_lifecycle != TPACPI_LIFE_EXITING) { 6749 fan_watchdog_active = 1; 6750 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task, 6751 msecs_to_jiffies(fan_watchdog_maxinterval 6752 * 1000))) { 6753 printk(TPACPI_ERR 6754 "failed to queue the fan watchdog, " 6755 "watchdog will not trigger\n"); 6756 } 6757 } else 6758 fan_watchdog_active = 0; 6759} 6760 6761static void fan_watchdog_fire(struct work_struct *ignored) 6762{ 6763 int rc; 6764 6765 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING) 6766 return; 6767 6768 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n"); 6769 rc = fan_set_enable(); 6770 if (rc < 0) { 6771 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, " 6772 "will try again later...\n", -rc); 6773 /* reschedule for later */ 6774 fan_watchdog_reset(); 6775 } 6776} 6777 6778/* 6779 * SYSFS fan layout: hwmon compatible (device) 6780 * 6781 * pwm*_enable: 6782 * 0: "disengaged" mode 6783 * 1: manual mode 6784 * 2: native EC "auto" mode (recommended, hardware default) 6785 * 6786 * pwm*: set speed in manual mode, ignored otherwise. 6787 * 0 is level 0; 255 is level 7. Intermediate points done with linear 6788 * interpolation. 6789 * 6790 * fan*_input: tachometer reading, RPM 6791 * 6792 * 6793 * SYSFS fan layout: extensions 6794 * 6795 * fan_watchdog (driver): 6796 * fan watchdog interval in seconds, 0 disables (default), max 120 6797 */ 6798 6799/* sysfs fan pwm1_enable ----------------------------------------------- */ 6800static ssize_t fan_pwm1_enable_show(struct device *dev, 6801 struct device_attribute *attr, 6802 char *buf) 6803{ 6804 int res, mode; 6805 u8 status; 6806 6807 res = fan_get_status_safe(&status); 6808 if (res) 6809 return res; 6810 6811 if (status & TP_EC_FAN_FULLSPEED) { 6812 mode = 0; 6813 } else if (status & TP_EC_FAN_AUTO) { 6814 mode = 2; 6815 } else 6816 mode = 1; 6817 6818 return snprintf(buf, PAGE_SIZE, "%d\n", mode); 6819} 6820 6821static ssize_t fan_pwm1_enable_store(struct device *dev, 6822 struct device_attribute *attr, 6823 const char *buf, size_t count) 6824{ 6825 unsigned long t; 6826 int res, level; 6827 6828 if (parse_strtoul(buf, 2, &t)) 6829 return -EINVAL; 6830 6831 tpacpi_disclose_usertask("hwmon pwm1_enable", 6832 "set fan mode to %lu\n", t); 6833 6834 switch (t) { 6835 case 0: 6836 level = TP_EC_FAN_FULLSPEED; 6837 break; 6838 case 1: 6839 level = TPACPI_FAN_LAST_LEVEL; 6840 break; 6841 case 2: 6842 level = TP_EC_FAN_AUTO; 6843 break; 6844 case 3: 6845 /* reserved for software-controlled auto mode */ 6846 return -ENOSYS; 6847 default: 6848 return -EINVAL; 6849 } 6850 6851 res = fan_set_level_safe(level); 6852 if (res == -ENXIO) 6853 return -EINVAL; 6854 else if (res < 0) 6855 return res; 6856 6857 fan_watchdog_reset(); 6858 6859 return count; 6860} 6861 6862static struct device_attribute dev_attr_fan_pwm1_enable = 6863 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO, 6864 fan_pwm1_enable_show, fan_pwm1_enable_store); 6865 6866/* sysfs fan pwm1 ------------------------------------------------------ */ 6867static ssize_t fan_pwm1_show(struct device *dev, 6868 struct device_attribute *attr, 6869 char *buf) 6870{ 6871 int res; 6872 u8 status; 6873 6874 res = fan_get_status_safe(&status); 6875 if (res) 6876 return res; 6877 6878 if ((status & 6879 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0) 6880 status = fan_control_desired_level; 6881 6882 if (status > 7) 6883 status = 7; 6884 6885 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7); 6886} 6887 6888static ssize_t fan_pwm1_store(struct device *dev, 6889 struct device_attribute *attr, 6890 const char *buf, size_t count) 6891{ 6892 unsigned long s; 6893 int rc; 6894 u8 status, newlevel; 6895 6896 if (parse_strtoul(buf, 255, &s)) 6897 return -EINVAL; 6898 6899 tpacpi_disclose_usertask("hwmon pwm1", 6900 "set fan speed to %lu\n", s); 6901 6902 /* scale down from 0-255 to 0-7 */ 6903 newlevel = (s >> 5) & 0x07; 6904 6905 if (mutex_lock_killable(&fan_mutex)) 6906 return -ERESTARTSYS; 6907 6908 rc = fan_get_status(&status); 6909 if (!rc && (status & 6910 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) { 6911 rc = fan_set_level(newlevel); 6912 if (rc == -ENXIO) 6913 rc = -EINVAL; 6914 else if (!rc) { 6915 fan_update_desired_level(newlevel); 6916 fan_watchdog_reset(); 6917 } 6918 } 6919 6920 mutex_unlock(&fan_mutex); 6921 return (rc)? rc : count; 6922} 6923 6924static struct device_attribute dev_attr_fan_pwm1 = 6925 __ATTR(pwm1, S_IWUSR | S_IRUGO, 6926 fan_pwm1_show, fan_pwm1_store); 6927 6928/* sysfs fan fan1_input ------------------------------------------------ */ 6929static ssize_t fan_fan1_input_show(struct device *dev, 6930 struct device_attribute *attr, 6931 char *buf) 6932{ 6933 int res; 6934 unsigned int speed; 6935 6936 res = fan_get_speed(&speed); 6937 if (res < 0) 6938 return res; 6939 6940 return snprintf(buf, PAGE_SIZE, "%u\n", speed); 6941} 6942 6943static struct device_attribute dev_attr_fan_fan1_input = 6944 __ATTR(fan1_input, S_IRUGO, 6945 fan_fan1_input_show, NULL); 6946 6947/* sysfs fan fan2_input ------------------------------------------------ */ 6948static ssize_t fan_fan2_input_show(struct device *dev, 6949 struct device_attribute *attr, 6950 char *buf) 6951{ 6952 int res; 6953 unsigned int speed; 6954 6955 res = fan2_get_speed(&speed); 6956 if (res < 0) 6957 return res; 6958 6959 return snprintf(buf, PAGE_SIZE, "%u\n", speed); 6960} 6961 6962static struct device_attribute dev_attr_fan_fan2_input = 6963 __ATTR(fan2_input, S_IRUGO, 6964 fan_fan2_input_show, NULL); 6965 6966/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */ 6967static ssize_t fan_fan_watchdog_show(struct device_driver *drv, 6968 char *buf) 6969{ 6970 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval); 6971} 6972 6973static ssize_t fan_fan_watchdog_store(struct device_driver *drv, 6974 const char *buf, size_t count) 6975{ 6976 unsigned long t; 6977 6978 if (parse_strtoul(buf, 120, &t)) 6979 return -EINVAL; 6980 6981 if (!fan_control_allowed) 6982 return -EPERM; 6983 6984 fan_watchdog_maxinterval = t; 6985 fan_watchdog_reset(); 6986 6987 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t); 6988 6989 return count; 6990} 6991 6992static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO, 6993 fan_fan_watchdog_show, fan_fan_watchdog_store); 6994 6995/* --------------------------------------------------------------------- */ 6996static struct attribute *fan_attributes[] = { 6997 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr, 6998 &dev_attr_fan_fan1_input.attr, 6999 NULL, /* for fan2_input */ 7000 NULL 7001}; 7002 7003static const struct attribute_group fan_attr_group = { 7004 .attrs = fan_attributes, 7005}; 7006 7007#define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */ 7008#define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */ 7009 7010#define TPACPI_FAN_QI(__id1, __id2, __quirks) \ 7011 { .vendor = PCI_VENDOR_ID_IBM, \ 7012 .bios = TPACPI_MATCH_ANY, \ 7013 .ec = TPID(__id1, __id2), \ 7014 .quirks = __quirks } 7015 7016#define TPACPI_FAN_QL(__id1, __id2, __quirks) \ 7017 { .vendor = PCI_VENDOR_ID_LENOVO, \ 7018 .bios = TPACPI_MATCH_ANY, \ 7019 .ec = TPID(__id1, __id2), \ 7020 .quirks = __quirks } 7021 7022static const struct tpacpi_quirk fan_quirk_table[] __initconst = { 7023 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1), 7024 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1), 7025 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1), 7026 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1), 7027 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN), 7028}; 7029 7030#undef TPACPI_FAN_QL 7031#undef TPACPI_FAN_QI 7032 7033static int __init fan_init(struct ibm_init_struct *iibm) 7034{ 7035 int rc; 7036 unsigned long quirks; 7037 7038 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN, 7039 "initializing fan subdriver\n"); 7040 7041 mutex_init(&fan_mutex); 7042 fan_status_access_mode = TPACPI_FAN_NONE; 7043 fan_control_access_mode = TPACPI_FAN_WR_NONE; 7044 fan_control_commands = 0; 7045 fan_watchdog_maxinterval = 0; 7046 tp_features.fan_ctrl_status_undef = 0; 7047 tp_features.second_fan = 0; 7048 fan_control_desired_level = 7; 7049 7050 TPACPI_ACPIHANDLE_INIT(fans); 7051 TPACPI_ACPIHANDLE_INIT(gfan); 7052 TPACPI_ACPIHANDLE_INIT(sfan); 7053 7054 quirks = tpacpi_check_quirks(fan_quirk_table, 7055 ARRAY_SIZE(fan_quirk_table)); 7056 7057 if (gfan_handle) { 7058 /* 570, 600e/x, 770e, 770x */ 7059 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN; 7060 } else { 7061 /* all other ThinkPads: note that even old-style 7062 * ThinkPad ECs supports the fan control register */ 7063 if (likely(acpi_ec_read(fan_status_offset, 7064 &fan_control_initial_status))) { 7065 fan_status_access_mode = TPACPI_FAN_RD_TPEC; 7066 if (quirks & TPACPI_FAN_Q1) 7067 fan_quirk1_setup(); 7068 if (quirks & TPACPI_FAN_2FAN) { 7069 tp_features.second_fan = 1; 7070 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN, 7071 "secondary fan support enabled\n"); 7072 } 7073 } else { 7074 printk(TPACPI_ERR 7075 "ThinkPad ACPI EC access misbehaving, " 7076 "fan status and control unavailable\n"); 7077 return 1; 7078 } 7079 } 7080 7081 if (sfan_handle) { 7082 /* 570, 770x-JL */ 7083 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN; 7084 fan_control_commands |= 7085 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE; 7086 } else { 7087 if (!gfan_handle) { 7088 /* gfan without sfan means no fan control */ 7089 /* all other models implement TP EC 0x2f control */ 7090 7091 if (fans_handle) { 7092 /* X31, X40, X41 */ 7093 fan_control_access_mode = 7094 TPACPI_FAN_WR_ACPI_FANS; 7095 fan_control_commands |= 7096 TPACPI_FAN_CMD_SPEED | 7097 TPACPI_FAN_CMD_LEVEL | 7098 TPACPI_FAN_CMD_ENABLE; 7099 } else { 7100 fan_control_access_mode = TPACPI_FAN_WR_TPEC; 7101 fan_control_commands |= 7102 TPACPI_FAN_CMD_LEVEL | 7103 TPACPI_FAN_CMD_ENABLE; 7104 } 7105 } 7106 } 7107 7108 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN, 7109 "fan is %s, modes %d, %d\n", 7110 str_supported(fan_status_access_mode != TPACPI_FAN_NONE || 7111 fan_control_access_mode != TPACPI_FAN_WR_NONE), 7112 fan_status_access_mode, fan_control_access_mode); 7113 7114 /* fan control master switch */ 7115 if (!fan_control_allowed) { 7116 fan_control_access_mode = TPACPI_FAN_WR_NONE; 7117 fan_control_commands = 0; 7118 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN, 7119 "fan control features disabled by parameter\n"); 7120 } 7121 7122 /* update fan_control_desired_level */ 7123 if (fan_status_access_mode != TPACPI_FAN_NONE) 7124 fan_get_status_safe(NULL); 7125 7126 if (fan_status_access_mode != TPACPI_FAN_NONE || 7127 fan_control_access_mode != TPACPI_FAN_WR_NONE) { 7128 if (tp_features.second_fan) { 7129 /* attach second fan tachometer */ 7130 fan_attributes[ARRAY_SIZE(fan_attributes)-2] = 7131 &dev_attr_fan_fan2_input.attr; 7132 } 7133 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj, 7134 &fan_attr_group); 7135 if (rc < 0) 7136 return rc; 7137 7138 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver, 7139 &driver_attr_fan_watchdog); 7140 if (rc < 0) { 7141 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, 7142 &fan_attr_group); 7143 return rc; 7144 } 7145 return 0; 7146 } else 7147 return 1; 7148} 7149 7150static void fan_exit(void) 7151{ 7152 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN, 7153 "cancelling any pending fan watchdog tasks\n"); 7154 7155 /* FIXME: can we really do this unconditionally? */ 7156 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group); 7157 driver_remove_file(&tpacpi_hwmon_pdriver.driver, 7158 &driver_attr_fan_watchdog); 7159 7160 cancel_delayed_work(&fan_watchdog_task); 7161 flush_workqueue(tpacpi_wq); 7162} 7163 7164static void fan_suspend(pm_message_t state) 7165{ 7166 int rc; 7167 7168 if (!fan_control_allowed) 7169 return; 7170 7171 /* Store fan status in cache */ 7172 fan_control_resume_level = 0; 7173 rc = fan_get_status_safe(&fan_control_resume_level); 7174 if (rc < 0) 7175 printk(TPACPI_NOTICE 7176 "failed to read fan level for later " 7177 "restore during resume: %d\n", rc); 7178 7179 /* if it is undefined, don't attempt to restore it. 7180 * KEEP THIS LAST */ 7181 if (tp_features.fan_ctrl_status_undef) 7182 fan_control_resume_level = 0; 7183} 7184 7185static void fan_resume(void) 7186{ 7187 u8 current_level = 7; 7188 bool do_set = false; 7189 int rc; 7190 7191 /* DSDT *always* updates status on resume */ 7192 tp_features.fan_ctrl_status_undef = 0; 7193 7194 if (!fan_control_allowed || 7195 !fan_control_resume_level || 7196 (fan_get_status_safe(¤t_level) < 0)) 7197 return; 7198 7199 switch (fan_control_access_mode) { 7200 case TPACPI_FAN_WR_ACPI_SFAN: 7201 /* never decrease fan level */ 7202 do_set = (fan_control_resume_level > current_level); 7203 break; 7204 case TPACPI_FAN_WR_ACPI_FANS: 7205 case TPACPI_FAN_WR_TPEC: 7206 /* never decrease fan level, scale is: 7207 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO 7208 * 7209 * We expect the firmware to set either 7 or AUTO, but we 7210 * handle FULLSPEED out of paranoia. 7211 * 7212 * So, we can safely only restore FULLSPEED or 7, anything 7213 * else could slow the fan. Restoring AUTO is useless, at 7214 * best that's exactly what the DSDT already set (it is the 7215 * slower it uses). 7216 * 7217 * Always keep in mind that the DSDT *will* have set the 7218 * fans to what the vendor supposes is the best level. We 7219 * muck with it only to speed the fan up. 7220 */ 7221 if (fan_control_resume_level != 7 && 7222 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED)) 7223 return; 7224 else 7225 do_set = !(current_level & TP_EC_FAN_FULLSPEED) && 7226 (current_level != fan_control_resume_level); 7227 break; 7228 default: 7229 return; 7230 } 7231 if (do_set) { 7232 printk(TPACPI_NOTICE 7233 "restoring fan level to 0x%02x\n", 7234 fan_control_resume_level); 7235 rc = fan_set_level_safe(fan_control_resume_level); 7236 if (rc < 0) 7237 printk(TPACPI_NOTICE 7238 "failed to restore fan level: %d\n", rc); 7239 } 7240} 7241 7242static int fan_read(char *p) 7243{ 7244 int len = 0; 7245 int rc; 7246 u8 status; 7247 unsigned int speed = 0; 7248 7249 switch (fan_status_access_mode) { 7250 case TPACPI_FAN_RD_ACPI_GFAN: 7251 /* 570, 600e/x, 770e, 770x */ 7252 rc = fan_get_status_safe(&status); 7253 if (rc < 0) 7254 return rc; 7255 7256 len += sprintf(p + len, "status:\t\t%s\n" 7257 "level:\t\t%d\n", 7258 (status != 0) ? "enabled" : "disabled", status); 7259 break; 7260 7261 case TPACPI_FAN_RD_TPEC: 7262 /* all except 570, 600e/x, 770e, 770x */ 7263 rc = fan_get_status_safe(&status); 7264 if (rc < 0) 7265 return rc; 7266 7267 len += sprintf(p + len, "status:\t\t%s\n", 7268 (status != 0) ? "enabled" : "disabled"); 7269 7270 rc = fan_get_speed(&speed); 7271 if (rc < 0) 7272 return rc; 7273 7274 len += sprintf(p + len, "speed:\t\t%d\n", speed); 7275 7276 if (status & TP_EC_FAN_FULLSPEED) 7277 /* Disengaged mode takes precedence */ 7278 len += sprintf(p + len, "level:\t\tdisengaged\n"); 7279 else if (status & TP_EC_FAN_AUTO) 7280 len += sprintf(p + len, "level:\t\tauto\n"); 7281 else 7282 len += sprintf(p + len, "level:\t\t%d\n", status); 7283 break; 7284 7285 case TPACPI_FAN_NONE: 7286 default: 7287 len += sprintf(p + len, "status:\t\tnot supported\n"); 7288 } 7289 7290 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) { 7291 len += sprintf(p + len, "commands:\tlevel <level>"); 7292 7293 switch (fan_control_access_mode) { 7294 case TPACPI_FAN_WR_ACPI_SFAN: 7295 len += sprintf(p + len, " (<level> is 0-7)\n"); 7296 break; 7297 7298 default: 7299 len += sprintf(p + len, " (<level> is 0-7, " 7300 "auto, disengaged, full-speed)\n"); 7301 break; 7302 } 7303 } 7304 7305 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE) 7306 len += sprintf(p + len, "commands:\tenable, disable\n" 7307 "commands:\twatchdog <timeout> (<timeout> " 7308 "is 0 (off), 1-120 (seconds))\n"); 7309 7310 if (fan_control_commands & TPACPI_FAN_CMD_SPEED) 7311 len += sprintf(p + len, "commands:\tspeed <speed>" 7312 " (<speed> is 0-65535)\n"); 7313 7314 return len; 7315} 7316 7317static int fan_write_cmd_level(const char *cmd, int *rc) 7318{ 7319 int level; 7320 7321 if (strlencmp(cmd, "level auto") == 0) 7322 level = TP_EC_FAN_AUTO; 7323 else if ((strlencmp(cmd, "level disengaged") == 0) | 7324 (strlencmp(cmd, "level full-speed") == 0)) 7325 level = TP_EC_FAN_FULLSPEED; 7326 else if (sscanf(cmd, "level %d", &level) != 1) 7327 return 0; 7328 7329 *rc = fan_set_level_safe(level); 7330 if (*rc == -ENXIO) 7331 printk(TPACPI_ERR "level command accepted for unsupported " 7332 "access mode %d", fan_control_access_mode); 7333 else if (!*rc) 7334 tpacpi_disclose_usertask("procfs fan", 7335 "set level to %d\n", level); 7336 7337 return 1; 7338} 7339 7340static int fan_write_cmd_enable(const char *cmd, int *rc) 7341{ 7342 if (strlencmp(cmd, "enable") != 0) 7343 return 0; 7344 7345 *rc = fan_set_enable(); 7346 if (*rc == -ENXIO) 7347 printk(TPACPI_ERR "enable command accepted for unsupported " 7348 "access mode %d", fan_control_access_mode); 7349 else if (!*rc) 7350 tpacpi_disclose_usertask("procfs fan", "enable\n"); 7351 7352 return 1; 7353} 7354 7355static int fan_write_cmd_disable(const char *cmd, int *rc) 7356{ 7357 if (strlencmp(cmd, "disable") != 0) 7358 return 0; 7359 7360 *rc = fan_set_disable(); 7361 if (*rc == -ENXIO) 7362 printk(TPACPI_ERR "disable command accepted for unsupported " 7363 "access mode %d", fan_control_access_mode); 7364 else if (!*rc) 7365 tpacpi_disclose_usertask("procfs fan", "disable\n"); 7366 7367 return 1; 7368} 7369 7370static int fan_write_cmd_speed(const char *cmd, int *rc) 7371{ 7372 int speed; 7373 7374 /* TODO: 7375 * Support speed <low> <medium> <high> ? */ 7376 7377 if (sscanf(cmd, "speed %d", &speed) != 1) 7378 return 0; 7379 7380 *rc = fan_set_speed(speed); 7381 if (*rc == -ENXIO) 7382 printk(TPACPI_ERR "speed command accepted for unsupported " 7383 "access mode %d", fan_control_access_mode); 7384 else if (!*rc) 7385 tpacpi_disclose_usertask("procfs fan", 7386 "set speed to %d\n", speed); 7387 7388 return 1; 7389} 7390 7391static int fan_write_cmd_watchdog(const char *cmd, int *rc) 7392{ 7393 int interval; 7394 7395 if (sscanf(cmd, "watchdog %d", &interval) != 1) 7396 return 0; 7397 7398 if (interval < 0 || interval > 120) 7399 *rc = -EINVAL; 7400 else { 7401 fan_watchdog_maxinterval = interval; 7402 tpacpi_disclose_usertask("procfs fan", 7403 "set watchdog timer to %d\n", 7404 interval); 7405 } 7406 7407 return 1; 7408} 7409 7410static int fan_write(char *buf) 7411{ 7412 char *cmd; 7413 int rc = 0; 7414 7415 while (!rc && (cmd = next_cmd(&buf))) { 7416 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) && 7417 fan_write_cmd_level(cmd, &rc)) && 7418 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) && 7419 (fan_write_cmd_enable(cmd, &rc) || 7420 fan_write_cmd_disable(cmd, &rc) || 7421 fan_write_cmd_watchdog(cmd, &rc))) && 7422 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) && 7423 fan_write_cmd_speed(cmd, &rc)) 7424 ) 7425 rc = -EINVAL; 7426 else if (!rc) 7427 fan_watchdog_reset(); 7428 } 7429 7430 return rc; 7431} 7432 7433static struct ibm_struct fan_driver_data = { 7434 .name = "fan", 7435 .read = fan_read, 7436 .write = fan_write, 7437 .exit = fan_exit, 7438 .suspend = fan_suspend, 7439 .resume = fan_resume, 7440}; 7441 7442/**************************************************************************** 7443 **************************************************************************** 7444 * 7445 * Infrastructure 7446 * 7447 **************************************************************************** 7448 ****************************************************************************/ 7449 7450/* sysfs name ---------------------------------------------------------- */ 7451static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev, 7452 struct device_attribute *attr, 7453 char *buf) 7454{ 7455 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME); 7456} 7457 7458static struct device_attribute dev_attr_thinkpad_acpi_pdev_name = 7459 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL); 7460 7461/* --------------------------------------------------------------------- */ 7462 7463/* /proc support */ 7464static struct proc_dir_entry *proc_dir; 7465 7466/* 7467 * Module and infrastructure proble, init and exit handling 7468 */ 7469 7470static int force_load; 7471 7472#ifdef CONFIG_THINKPAD_ACPI_DEBUG 7473static const char * __init str_supported(int is_supported) 7474{ 7475 static char text_unsupported[] __initdata = "not supported"; 7476 7477 return (is_supported)? &text_unsupported[4] : &text_unsupported[0]; 7478} 7479#endif /* CONFIG_THINKPAD_ACPI_DEBUG */ 7480 7481static void ibm_exit(struct ibm_struct *ibm) 7482{ 7483 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name); 7484 7485 list_del_init(&ibm->all_drivers); 7486 7487 if (ibm->flags.acpi_notify_installed) { 7488 dbg_printk(TPACPI_DBG_EXIT, 7489 "%s: acpi_remove_notify_handler\n", ibm->name); 7490 BUG_ON(!ibm->acpi); 7491 acpi_remove_notify_handler(*ibm->acpi->handle, 7492 ibm->acpi->type, 7493 dispatch_acpi_notify); 7494 ibm->flags.acpi_notify_installed = 0; 7495 ibm->flags.acpi_notify_installed = 0; 7496 } 7497 7498 if (ibm->flags.proc_created) { 7499 dbg_printk(TPACPI_DBG_EXIT, 7500 "%s: remove_proc_entry\n", ibm->name); 7501 remove_proc_entry(ibm->name, proc_dir); 7502 ibm->flags.proc_created = 0; 7503 } 7504 7505 if (ibm->flags.acpi_driver_registered) { 7506 dbg_printk(TPACPI_DBG_EXIT, 7507 "%s: acpi_bus_unregister_driver\n", ibm->name); 7508 BUG_ON(!ibm->acpi); 7509 acpi_bus_unregister_driver(ibm->acpi->driver); 7510 kfree(ibm->acpi->driver); 7511 ibm->acpi->driver = NULL; 7512 ibm->flags.acpi_driver_registered = 0; 7513 } 7514 7515 if (ibm->flags.init_called && ibm->exit) { 7516 ibm->exit(); 7517 ibm->flags.init_called = 0; 7518 } 7519 7520 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name); 7521} 7522 7523static int __init ibm_init(struct ibm_init_struct *iibm) 7524{ 7525 int ret; 7526 struct ibm_struct *ibm = iibm->data; 7527 struct proc_dir_entry *entry; 7528 7529 BUG_ON(ibm == NULL); 7530 7531 INIT_LIST_HEAD(&ibm->all_drivers); 7532 7533 if (ibm->flags.experimental && !experimental) 7534 return 0; 7535 7536 dbg_printk(TPACPI_DBG_INIT, 7537 "probing for %s\n", ibm->name); 7538 7539 if (iibm->init) { 7540 ret = iibm->init(iibm); 7541 if (ret > 0) 7542 return 0; /* probe failed */ 7543 if (ret) 7544 return ret; 7545 7546 ibm->flags.init_called = 1; 7547 } 7548 7549 if (ibm->acpi) { 7550 if (ibm->acpi->hid) { 7551 ret = register_tpacpi_subdriver(ibm); 7552 if (ret) 7553 goto err_out; 7554 } 7555 7556 if (ibm->acpi->notify) { 7557 ret = setup_acpi_notify(ibm); 7558 if (ret == -ENODEV) { 7559 printk(TPACPI_NOTICE "disabling subdriver %s\n", 7560 ibm->name); 7561 ret = 0; 7562 goto err_out; 7563 } 7564 if (ret < 0) 7565 goto err_out; 7566 } 7567 } 7568 7569 dbg_printk(TPACPI_DBG_INIT, 7570 "%s installed\n", ibm->name); 7571 7572 if (ibm->read) { 7573 entry = create_proc_entry(ibm->name, 7574 S_IFREG | S_IRUGO | S_IWUSR, 7575 proc_dir); 7576 if (!entry) { 7577 printk(TPACPI_ERR "unable to create proc entry %s\n", 7578 ibm->name); 7579 ret = -ENODEV; 7580 goto err_out; 7581 } 7582 entry->data = ibm; 7583 entry->read_proc = &dispatch_procfs_read; 7584 if (ibm->write) 7585 entry->write_proc = &dispatch_procfs_write; 7586 ibm->flags.proc_created = 1; 7587 } 7588 7589 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers); 7590 7591 return 0; 7592 7593err_out: 7594 dbg_printk(TPACPI_DBG_INIT, 7595 "%s: at error exit path with result %d\n", 7596 ibm->name, ret); 7597 7598 ibm_exit(ibm); 7599 return (ret < 0)? ret : 0; 7600} 7601 7602/* Probing */ 7603 7604static bool __pure __init tpacpi_is_fw_digit(const char c) 7605{ 7606 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z'); 7607} 7608 7609/* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */ 7610static bool __pure __init tpacpi_is_valid_fw_id(const char* const s, 7611 const char t) 7612{ 7613 return s && strlen(s) >= 8 && 7614 tpacpi_is_fw_digit(s[0]) && 7615 tpacpi_is_fw_digit(s[1]) && 7616 s[2] == t && s[3] == 'T' && 7617 tpacpi_is_fw_digit(s[4]) && 7618 tpacpi_is_fw_digit(s[5]) && 7619 s[6] == 'W' && s[7] == 'W'; 7620} 7621 7622/* returns 0 - probe ok, or < 0 - probe error. 7623 * Probe ok doesn't mean thinkpad found. 7624 * On error, kfree() cleanup on tp->* is not performed, caller must do it */ 7625static int __must_check __init get_thinkpad_model_data( 7626 struct thinkpad_id_data *tp) 7627{ 7628 const struct dmi_device *dev = NULL; 7629 char ec_fw_string[18]; 7630 char const *s; 7631 7632 if (!tp) 7633 return -EINVAL; 7634 7635 memset(tp, 0, sizeof(*tp)); 7636 7637 if (dmi_name_in_vendors("IBM")) 7638 tp->vendor = PCI_VENDOR_ID_IBM; 7639 else if (dmi_name_in_vendors("LENOVO")) 7640 tp->vendor = PCI_VENDOR_ID_LENOVO; 7641 else 7642 return 0; 7643 7644 s = dmi_get_system_info(DMI_BIOS_VERSION); 7645 tp->bios_version_str = kstrdup(s, GFP_KERNEL); 7646 if (s && !tp->bios_version_str) 7647 return -ENOMEM; 7648 7649 /* Really ancient ThinkPad 240X will fail this, which is fine */ 7650 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E')) 7651 return 0; 7652 7653 tp->bios_model = tp->bios_version_str[0] 7654 | (tp->bios_version_str[1] << 8); 7655 tp->bios_release = (tp->bios_version_str[4] << 8) 7656 | tp->bios_version_str[5]; 7657 7658 /* 7659 * ThinkPad T23 or newer, A31 or newer, R50e or newer, 7660 * X32 or newer, all Z series; Some models must have an 7661 * up-to-date BIOS or they will not be detected. 7662 * 7663 * See http://thinkwiki.org/wiki/List_of_DMI_IDs 7664 */ 7665 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) { 7666 if (sscanf(dev->name, 7667 "IBM ThinkPad Embedded Controller -[%17c", 7668 ec_fw_string) == 1) { 7669 ec_fw_string[sizeof(ec_fw_string) - 1] = 0; 7670 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0; 7671 7672 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL); 7673 if (!tp->ec_version_str) 7674 return -ENOMEM; 7675 7676 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) { 7677 tp->ec_model = ec_fw_string[0] 7678 | (ec_fw_string[1] << 8); 7679 tp->ec_release = (ec_fw_string[4] << 8) 7680 | ec_fw_string[5]; 7681 } else { 7682 printk(TPACPI_NOTICE 7683 "ThinkPad firmware release %s " 7684 "doesn't match the known patterns\n", 7685 ec_fw_string); 7686 printk(TPACPI_NOTICE 7687 "please report this to %s\n", 7688 TPACPI_MAIL); 7689 } 7690 break; 7691 } 7692 } 7693 7694 s = dmi_get_system_info(DMI_PRODUCT_VERSION); 7695 if (s && !strnicmp(s, "ThinkPad", 8)) { 7696 tp->model_str = kstrdup(s, GFP_KERNEL); 7697 if (!tp->model_str) 7698 return -ENOMEM; 7699 } 7700 7701 s = dmi_get_system_info(DMI_PRODUCT_NAME); 7702 tp->nummodel_str = kstrdup(s, GFP_KERNEL); 7703 if (s && !tp->nummodel_str) 7704 return -ENOMEM; 7705 7706 return 0; 7707} 7708 7709static int __init probe_for_thinkpad(void) 7710{ 7711 int is_thinkpad; 7712 7713 if (acpi_disabled) 7714 return -ENODEV; 7715 7716 /* 7717 * Non-ancient models have better DMI tagging, but very old models 7718 * don't. tpacpi_is_fw_known() is a cheat to help in that case. 7719 */ 7720 is_thinkpad = (thinkpad_id.model_str != NULL) || 7721 (thinkpad_id.ec_model != 0) || 7722 tpacpi_is_fw_known(); 7723 7724 /* ec is required because many other handles are relative to it */ 7725 TPACPI_ACPIHANDLE_INIT(ec); 7726 if (!ec_handle) { 7727 if (is_thinkpad) 7728 printk(TPACPI_ERR 7729 "Not yet supported ThinkPad detected!\n"); 7730 return -ENODEV; 7731 } 7732 7733 if (!is_thinkpad && !force_load) 7734 return -ENODEV; 7735 7736 return 0; 7737} 7738 7739 7740/* Module init, exit, parameters */ 7741 7742static struct ibm_init_struct ibms_init[] __initdata = { 7743 { 7744 .init = thinkpad_acpi_driver_init, 7745 .data = &thinkpad_acpi_driver_data, 7746 }, 7747 { 7748 .init = hotkey_init, 7749 .data = &hotkey_driver_data, 7750 }, 7751 { 7752 .init = bluetooth_init, 7753 .data = &bluetooth_driver_data, 7754 }, 7755 { 7756 .init = wan_init, 7757 .data = &wan_driver_data, 7758 }, 7759 { 7760 .init = uwb_init, 7761 .data = &uwb_driver_data, 7762 }, 7763#ifdef CONFIG_THINKPAD_ACPI_VIDEO 7764 { 7765 .init = video_init, 7766 .data = &video_driver_data, 7767 }, 7768#endif 7769 { 7770 .init = light_init, 7771 .data = &light_driver_data, 7772 }, 7773 { 7774 .init = cmos_init, 7775 .data = &cmos_driver_data, 7776 }, 7777 { 7778 .init = led_init, 7779 .data = &led_driver_data, 7780 }, 7781 { 7782 .init = beep_init, 7783 .data = &beep_driver_data, 7784 }, 7785 { 7786 .init = thermal_init, 7787 .data = &thermal_driver_data, 7788 }, 7789 { 7790 .data = &ecdump_driver_data, 7791 }, 7792 { 7793 .init = brightness_init, 7794 .data = &brightness_driver_data, 7795 }, 7796 { 7797 .data = &volume_driver_data, 7798 }, 7799 { 7800 .init = fan_init, 7801 .data = &fan_driver_data, 7802 }, 7803}; 7804 7805static int __init set_ibm_param(const char *val, struct kernel_param *kp) 7806{ 7807 unsigned int i; 7808 struct ibm_struct *ibm; 7809 7810 if (!kp || !kp->name || !val) 7811 return -EINVAL; 7812 7813 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) { 7814 ibm = ibms_init[i].data; 7815 WARN_ON(ibm == NULL); 7816 7817 if (!ibm || !ibm->name) 7818 continue; 7819 7820 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) { 7821 if (strlen(val) > sizeof(ibms_init[i].param) - 2) 7822 return -ENOSPC; 7823 strcpy(ibms_init[i].param, val); 7824 strcat(ibms_init[i].param, ","); 7825 return 0; 7826 } 7827 } 7828 7829 return -EINVAL; 7830} 7831 7832module_param(experimental, int, 0); 7833MODULE_PARM_DESC(experimental, 7834 "Enables experimental features when non-zero"); 7835 7836module_param_named(debug, dbg_level, uint, 0); 7837MODULE_PARM_DESC(debug, "Sets debug level bit-mask"); 7838 7839module_param(force_load, bool, 0); 7840MODULE_PARM_DESC(force_load, 7841 "Attempts to load the driver even on a " 7842 "mis-identified ThinkPad when true"); 7843 7844module_param_named(fan_control, fan_control_allowed, bool, 0); 7845MODULE_PARM_DESC(fan_control, 7846 "Enables setting fan parameters features when true"); 7847 7848module_param_named(brightness_mode, brightness_mode, uint, 0); 7849MODULE_PARM_DESC(brightness_mode, 7850 "Selects brightness control strategy: " 7851 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM"); 7852 7853module_param(brightness_enable, uint, 0); 7854MODULE_PARM_DESC(brightness_enable, 7855 "Enables backlight control when 1, disables when 0"); 7856 7857module_param(hotkey_report_mode, uint, 0); 7858MODULE_PARM_DESC(hotkey_report_mode, 7859 "used for backwards compatibility with userspace, " 7860 "see documentation"); 7861 7862#define TPACPI_PARAM(feature) \ 7863 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \ 7864 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \ 7865 "at module load, see documentation") 7866 7867TPACPI_PARAM(hotkey); 7868TPACPI_PARAM(bluetooth); 7869TPACPI_PARAM(video); 7870TPACPI_PARAM(light); 7871TPACPI_PARAM(cmos); 7872TPACPI_PARAM(led); 7873TPACPI_PARAM(beep); 7874TPACPI_PARAM(ecdump); 7875TPACPI_PARAM(brightness); 7876TPACPI_PARAM(volume); 7877TPACPI_PARAM(fan); 7878 7879#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 7880module_param(dbg_wlswemul, uint, 0); 7881MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation"); 7882module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0); 7883MODULE_PARM_DESC(wlsw_state, 7884 "Initial state of the emulated WLSW switch"); 7885 7886module_param(dbg_bluetoothemul, uint, 0); 7887MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation"); 7888module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0); 7889MODULE_PARM_DESC(bluetooth_state, 7890 "Initial state of the emulated bluetooth switch"); 7891 7892module_param(dbg_wwanemul, uint, 0); 7893MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation"); 7894module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0); 7895MODULE_PARM_DESC(wwan_state, 7896 "Initial state of the emulated WWAN switch"); 7897 7898module_param(dbg_uwbemul, uint, 0); 7899MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation"); 7900module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0); 7901MODULE_PARM_DESC(uwb_state, 7902 "Initial state of the emulated UWB switch"); 7903#endif 7904 7905static void thinkpad_acpi_module_exit(void) 7906{ 7907 struct ibm_struct *ibm, *itmp; 7908 7909 tpacpi_lifecycle = TPACPI_LIFE_EXITING; 7910 7911 list_for_each_entry_safe_reverse(ibm, itmp, 7912 &tpacpi_all_drivers, 7913 all_drivers) { 7914 ibm_exit(ibm); 7915 } 7916 7917 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n"); 7918 7919 if (tpacpi_inputdev) { 7920 if (tp_features.input_device_registered) 7921 input_unregister_device(tpacpi_inputdev); 7922 else 7923 input_free_device(tpacpi_inputdev); 7924 } 7925 7926 if (tpacpi_hwmon) 7927 hwmon_device_unregister(tpacpi_hwmon); 7928 7929 if (tp_features.sensors_pdev_attrs_registered) 7930 device_remove_file(&tpacpi_sensors_pdev->dev, 7931 &dev_attr_thinkpad_acpi_pdev_name); 7932 if (tpacpi_sensors_pdev) 7933 platform_device_unregister(tpacpi_sensors_pdev); 7934 if (tpacpi_pdev) 7935 platform_device_unregister(tpacpi_pdev); 7936 7937 if (tp_features.sensors_pdrv_attrs_registered) 7938 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver); 7939 if (tp_features.platform_drv_attrs_registered) 7940 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver); 7941 7942 if (tp_features.sensors_pdrv_registered) 7943 platform_driver_unregister(&tpacpi_hwmon_pdriver); 7944 7945 if (tp_features.platform_drv_registered) 7946 platform_driver_unregister(&tpacpi_pdriver); 7947 7948 if (proc_dir) 7949 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir); 7950 7951 if (tpacpi_wq) 7952 destroy_workqueue(tpacpi_wq); 7953 7954 kfree(thinkpad_id.bios_version_str); 7955 kfree(thinkpad_id.ec_version_str); 7956 kfree(thinkpad_id.model_str); 7957} 7958 7959 7960static int __init thinkpad_acpi_module_init(void) 7961{ 7962 int ret, i; 7963 7964 tpacpi_lifecycle = TPACPI_LIFE_INIT; 7965 7966 /* Parameter checking */ 7967 if (hotkey_report_mode > 2) 7968 return -EINVAL; 7969 7970 /* Driver-level probe */ 7971 7972 ret = get_thinkpad_model_data(&thinkpad_id); 7973 if (ret) { 7974 printk(TPACPI_ERR 7975 "unable to get DMI data: %d\n", ret); 7976 thinkpad_acpi_module_exit(); 7977 return ret; 7978 } 7979 ret = probe_for_thinkpad(); 7980 if (ret) { 7981 thinkpad_acpi_module_exit(); 7982 return ret; 7983 } 7984 7985 /* Driver initialization */ 7986 7987 TPACPI_ACPIHANDLE_INIT(ecrd); 7988 TPACPI_ACPIHANDLE_INIT(ecwr); 7989 7990 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME); 7991 if (!tpacpi_wq) { 7992 thinkpad_acpi_module_exit(); 7993 return -ENOMEM; 7994 } 7995 7996 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir); 7997 if (!proc_dir) { 7998 printk(TPACPI_ERR 7999 "unable to create proc dir " TPACPI_PROC_DIR); 8000 thinkpad_acpi_module_exit(); 8001 return -ENODEV; 8002 } 8003 8004 ret = platform_driver_register(&tpacpi_pdriver); 8005 if (ret) { 8006 printk(TPACPI_ERR 8007 "unable to register main platform driver\n"); 8008 thinkpad_acpi_module_exit(); 8009 return ret; 8010 } 8011 tp_features.platform_drv_registered = 1; 8012 8013 ret = platform_driver_register(&tpacpi_hwmon_pdriver); 8014 if (ret) { 8015 printk(TPACPI_ERR 8016 "unable to register hwmon platform driver\n"); 8017 thinkpad_acpi_module_exit(); 8018 return ret; 8019 } 8020 tp_features.sensors_pdrv_registered = 1; 8021 8022 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver); 8023 if (!ret) { 8024 tp_features.platform_drv_attrs_registered = 1; 8025 ret = tpacpi_create_driver_attributes( 8026 &tpacpi_hwmon_pdriver.driver); 8027 } 8028 if (ret) { 8029 printk(TPACPI_ERR 8030 "unable to create sysfs driver attributes\n"); 8031 thinkpad_acpi_module_exit(); 8032 return ret; 8033 } 8034 tp_features.sensors_pdrv_attrs_registered = 1; 8035 8036 8037 /* Device initialization */ 8038 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1, 8039 NULL, 0); 8040 if (IS_ERR(tpacpi_pdev)) { 8041 ret = PTR_ERR(tpacpi_pdev); 8042 tpacpi_pdev = NULL; 8043 printk(TPACPI_ERR "unable to register platform device\n"); 8044 thinkpad_acpi_module_exit(); 8045 return ret; 8046 } 8047 tpacpi_sensors_pdev = platform_device_register_simple( 8048 TPACPI_HWMON_DRVR_NAME, 8049 -1, NULL, 0); 8050 if (IS_ERR(tpacpi_sensors_pdev)) { 8051 ret = PTR_ERR(tpacpi_sensors_pdev); 8052 tpacpi_sensors_pdev = NULL; 8053 printk(TPACPI_ERR 8054 "unable to register hwmon platform device\n"); 8055 thinkpad_acpi_module_exit(); 8056 return ret; 8057 } 8058 ret = device_create_file(&tpacpi_sensors_pdev->dev, 8059 &dev_attr_thinkpad_acpi_pdev_name); 8060 if (ret) { 8061 printk(TPACPI_ERR 8062 "unable to create sysfs hwmon device attributes\n"); 8063 thinkpad_acpi_module_exit(); 8064 return ret; 8065 } 8066 tp_features.sensors_pdev_attrs_registered = 1; 8067 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev); 8068 if (IS_ERR(tpacpi_hwmon)) { 8069 ret = PTR_ERR(tpacpi_hwmon); 8070 tpacpi_hwmon = NULL; 8071 printk(TPACPI_ERR "unable to register hwmon device\n"); 8072 thinkpad_acpi_module_exit(); 8073 return ret; 8074 } 8075 mutex_init(&tpacpi_inputdev_send_mutex); 8076 tpacpi_inputdev = input_allocate_device(); 8077 if (!tpacpi_inputdev) { 8078 printk(TPACPI_ERR "unable to allocate input device\n"); 8079 thinkpad_acpi_module_exit(); 8080 return -ENOMEM; 8081 } else { 8082 /* Prepare input device, but don't register */ 8083 tpacpi_inputdev->name = "ThinkPad Extra Buttons"; 8084 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0"; 8085 tpacpi_inputdev->id.bustype = BUS_HOST; 8086 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ? 8087 thinkpad_id.vendor : 8088 PCI_VENDOR_ID_IBM; 8089 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT; 8090 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION; 8091 } 8092 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) { 8093 ret = ibm_init(&ibms_init[i]); 8094 if (ret >= 0 && *ibms_init[i].param) 8095 ret = ibms_init[i].data->write(ibms_init[i].param); 8096 if (ret < 0) { 8097 thinkpad_acpi_module_exit(); 8098 return ret; 8099 } 8100 } 8101 ret = input_register_device(tpacpi_inputdev); 8102 if (ret < 0) { 8103 printk(TPACPI_ERR "unable to register input device\n"); 8104 thinkpad_acpi_module_exit(); 8105 return ret; 8106 } else { 8107 tp_features.input_device_registered = 1; 8108 } 8109 8110 tpacpi_lifecycle = TPACPI_LIFE_RUNNING; 8111 return 0; 8112} 8113 8114MODULE_ALIAS(TPACPI_DRVR_SHORTNAME); 8115 8116/* 8117 * This will autoload the driver in almost every ThinkPad 8118 * in widespread use. 8119 * 8120 * Only _VERY_ old models, like the 240, 240x and 570 lack 8121 * the HKEY event interface. 8122 */ 8123MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids); 8124 8125/* 8126 * DMI matching for module autoloading 8127 * 8128 * See http://thinkwiki.org/wiki/List_of_DMI_IDs 8129 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads 8130 * 8131 * Only models listed in thinkwiki will be supported, so add yours 8132 * if it is not there yet. 8133 */ 8134#define IBM_BIOS_MODULE_ALIAS(__type) \ 8135 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*") 8136 8137/* Ancient thinkpad BIOSes have to be identified by 8138 * BIOS type or model number, and there are far less 8139 * BIOS types than model numbers... */ 8140IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */ 8141 8142MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>"); 8143MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>"); 8144MODULE_DESCRIPTION(TPACPI_DESC); 8145MODULE_VERSION(TPACPI_VERSION); 8146MODULE_LICENSE("GPL"); 8147 8148module_init(thinkpad_acpi_module_init); 8149module_exit(thinkpad_acpi_module_exit); 8150