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