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