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