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