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