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