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