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