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