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