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 }; 4502 4503 #ifdef CONFIG_SUSPEND 4504 /* 4505 * Lenovo laptops from a variety of generations run a SMI handler during the D3->D0 4506 * transition that occurs specifically when exiting suspend to idle which can cause 4507 * large delays during resume when the IOMMU translation layer is enabled (the default 4508 * behavior) for NVME devices: 4509 * 4510 * To avoid this firmware problem, skip the SMI handler on these machines before the 4511 * D0 transition occurs. 4512 */ 4513 static void thinkpad_acpi_amd_s2idle_restore(void) 4514 { 4515 struct resource *res; 4516 void __iomem *addr; 4517 u8 val; 4518 4519 res = request_mem_region_muxed(tp_features.quirks->s2idle_bug_mmio, 1, 4520 "thinkpad_acpi_pm80"); 4521 if (!res) 4522 return; 4523 4524 addr = ioremap(tp_features.quirks->s2idle_bug_mmio, 1); 4525 if (!addr) 4526 goto cleanup_resource; 4527 4528 val = ioread8(addr); 4529 iowrite8(val & ~BIT(0), addr); 4530 4531 iounmap(addr); 4532 cleanup_resource: 4533 release_resource(res); 4534 kfree(res); 4535 } 4536 4537 static struct acpi_s2idle_dev_ops thinkpad_acpi_s2idle_dev_ops = { 4538 .restore = thinkpad_acpi_amd_s2idle_restore, 4539 }; 4540 #endif 4541 4542 static const struct pci_device_id fwbug_cards_ids[] __initconst = { 4543 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24F3) }, 4544 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x24FD) }, 4545 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2526) }, 4546 {} 4547 }; 4548 4549 4550 static int __init have_bt_fwbug(void) 4551 { 4552 /* 4553 * Some AMD based ThinkPads have a firmware bug that calling 4554 * "GBDC" will cause bluetooth on Intel wireless cards blocked 4555 */ 4556 if (tp_features.quirks && tp_features.quirks->btusb_bug && 4557 pci_dev_present(fwbug_cards_ids)) { 4558 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, 4559 FW_BUG "disable bluetooth subdriver for Intel cards\n"); 4560 return 1; 4561 } else 4562 return 0; 4563 } 4564 4565 static int __init bluetooth_init(struct ibm_init_struct *iibm) 4566 { 4567 int res; 4568 int status = 0; 4569 4570 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, 4571 "initializing bluetooth subdriver\n"); 4572 4573 TPACPI_ACPIHANDLE_INIT(hkey); 4574 4575 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p, 4576 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */ 4577 tp_features.bluetooth = !have_bt_fwbug() && hkey_handle && 4578 acpi_evalf(hkey_handle, &status, "GBDC", "qd"); 4579 4580 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, 4581 "bluetooth is %s, status 0x%02x\n", 4582 str_supported(tp_features.bluetooth), 4583 status); 4584 4585 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 4586 if (dbg_bluetoothemul) { 4587 tp_features.bluetooth = 1; 4588 pr_info("bluetooth switch emulation enabled\n"); 4589 } else 4590 #endif 4591 if (tp_features.bluetooth && 4592 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) { 4593 /* no bluetooth hardware present in system */ 4594 tp_features.bluetooth = 0; 4595 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, 4596 "bluetooth hardware not installed\n"); 4597 } 4598 4599 if (!tp_features.bluetooth) 4600 return -ENODEV; 4601 4602 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID, 4603 &bluetooth_tprfk_ops, 4604 RFKILL_TYPE_BLUETOOTH, 4605 TPACPI_RFK_BLUETOOTH_SW_NAME, 4606 true); 4607 return res; 4608 } 4609 4610 /* procfs -------------------------------------------------------------- */ 4611 static int bluetooth_read(struct seq_file *m) 4612 { 4613 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m); 4614 } 4615 4616 static int bluetooth_write(char *buf) 4617 { 4618 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf); 4619 } 4620 4621 static struct ibm_struct bluetooth_driver_data = { 4622 .name = "bluetooth", 4623 .read = bluetooth_read, 4624 .write = bluetooth_write, 4625 .exit = bluetooth_exit, 4626 .shutdown = bluetooth_shutdown, 4627 }; 4628 4629 /************************************************************************* 4630 * Wan subdriver 4631 */ 4632 4633 enum { 4634 /* ACPI GWAN/SWAN bits */ 4635 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */ 4636 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */ 4637 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume: 4638 0 = disable, 1 = enable */ 4639 }; 4640 4641 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw" 4642 4643 static int wan_get_status(void) 4644 { 4645 int status; 4646 4647 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 4648 if (dbg_wwanemul) 4649 return (tpacpi_wwan_emulstate) ? 4650 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF; 4651 #endif 4652 4653 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d")) 4654 return -EIO; 4655 4656 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ? 4657 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF; 4658 } 4659 4660 static int wan_set_status(enum tpacpi_rfkill_state state) 4661 { 4662 int status; 4663 4664 vdbg_printk(TPACPI_DBG_RFKILL, "will attempt to %s wwan\n", 4665 str_enable_disable(state == TPACPI_RFK_RADIO_ON)); 4666 4667 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 4668 if (dbg_wwanemul) { 4669 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON); 4670 return 0; 4671 } 4672 #endif 4673 4674 if (state == TPACPI_RFK_RADIO_ON) 4675 status = TP_ACPI_WANCARD_RADIOSSW 4676 | TP_ACPI_WANCARD_RESUMECTRL; 4677 else 4678 status = 0; 4679 4680 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status)) 4681 return -EIO; 4682 4683 return 0; 4684 } 4685 4686 /* sysfs wan enable ---------------------------------------------------- */ 4687 static ssize_t wan_enable_show(struct device *dev, 4688 struct device_attribute *attr, 4689 char *buf) 4690 { 4691 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID, 4692 attr, buf); 4693 } 4694 4695 static ssize_t wan_enable_store(struct device *dev, 4696 struct device_attribute *attr, 4697 const char *buf, size_t count) 4698 { 4699 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID, 4700 attr, buf, count); 4701 } 4702 4703 static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO, 4704 wan_enable_show, wan_enable_store); 4705 4706 /* --------------------------------------------------------------------- */ 4707 4708 static struct attribute *wan_attributes[] = { 4709 &dev_attr_wwan_enable.attr, 4710 NULL 4711 }; 4712 4713 static umode_t wan_attr_is_visible(struct kobject *kobj, struct attribute *attr, 4714 int n) 4715 { 4716 return tp_features.wan ? attr->mode : 0; 4717 } 4718 4719 static const struct attribute_group wan_attr_group = { 4720 .is_visible = wan_attr_is_visible, 4721 .attrs = wan_attributes, 4722 }; 4723 4724 static const struct tpacpi_rfk_ops wan_tprfk_ops = { 4725 .get_status = wan_get_status, 4726 .set_status = wan_set_status, 4727 }; 4728 4729 static void wan_shutdown(void) 4730 { 4731 /* Order firmware to save current state to NVRAM */ 4732 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd", 4733 TP_ACPI_WGSV_SAVE_STATE)) 4734 pr_notice("failed to save WWAN state to NVRAM\n"); 4735 else 4736 vdbg_printk(TPACPI_DBG_RFKILL, 4737 "WWAN state saved to NVRAM\n"); 4738 } 4739 4740 static void wan_exit(void) 4741 { 4742 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID); 4743 wan_shutdown(); 4744 } 4745 4746 static int __init wan_init(struct ibm_init_struct *iibm) 4747 { 4748 int res; 4749 int status = 0; 4750 4751 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, 4752 "initializing wan subdriver\n"); 4753 4754 TPACPI_ACPIHANDLE_INIT(hkey); 4755 4756 tp_features.wan = hkey_handle && 4757 acpi_evalf(hkey_handle, &status, "GWAN", "qd"); 4758 4759 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, 4760 "wan is %s, status 0x%02x\n", 4761 str_supported(tp_features.wan), 4762 status); 4763 4764 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 4765 if (dbg_wwanemul) { 4766 tp_features.wan = 1; 4767 pr_info("wwan switch emulation enabled\n"); 4768 } else 4769 #endif 4770 if (tp_features.wan && 4771 !(status & TP_ACPI_WANCARD_HWPRESENT)) { 4772 /* no wan hardware present in system */ 4773 tp_features.wan = 0; 4774 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, 4775 "wan hardware not installed\n"); 4776 } 4777 4778 if (!tp_features.wan) 4779 return -ENODEV; 4780 4781 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID, 4782 &wan_tprfk_ops, 4783 RFKILL_TYPE_WWAN, 4784 TPACPI_RFK_WWAN_SW_NAME, 4785 true); 4786 return res; 4787 } 4788 4789 /* procfs -------------------------------------------------------------- */ 4790 static int wan_read(struct seq_file *m) 4791 { 4792 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m); 4793 } 4794 4795 static int wan_write(char *buf) 4796 { 4797 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf); 4798 } 4799 4800 static struct ibm_struct wan_driver_data = { 4801 .name = "wan", 4802 .read = wan_read, 4803 .write = wan_write, 4804 .exit = wan_exit, 4805 .shutdown = wan_shutdown, 4806 }; 4807 4808 /************************************************************************* 4809 * UWB subdriver 4810 */ 4811 4812 enum { 4813 /* ACPI GUWB/SUWB bits */ 4814 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */ 4815 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */ 4816 }; 4817 4818 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw" 4819 4820 static int uwb_get_status(void) 4821 { 4822 int status; 4823 4824 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 4825 if (dbg_uwbemul) 4826 return (tpacpi_uwb_emulstate) ? 4827 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF; 4828 #endif 4829 4830 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d")) 4831 return -EIO; 4832 4833 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ? 4834 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF; 4835 } 4836 4837 static int uwb_set_status(enum tpacpi_rfkill_state state) 4838 { 4839 int status; 4840 4841 vdbg_printk(TPACPI_DBG_RFKILL, "will attempt to %s UWB\n", 4842 str_enable_disable(state == TPACPI_RFK_RADIO_ON)); 4843 4844 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 4845 if (dbg_uwbemul) { 4846 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON); 4847 return 0; 4848 } 4849 #endif 4850 4851 if (state == TPACPI_RFK_RADIO_ON) 4852 status = TP_ACPI_UWB_RADIOSSW; 4853 else 4854 status = 0; 4855 4856 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status)) 4857 return -EIO; 4858 4859 return 0; 4860 } 4861 4862 /* --------------------------------------------------------------------- */ 4863 4864 static const struct tpacpi_rfk_ops uwb_tprfk_ops = { 4865 .get_status = uwb_get_status, 4866 .set_status = uwb_set_status, 4867 }; 4868 4869 static void uwb_exit(void) 4870 { 4871 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID); 4872 } 4873 4874 static int __init uwb_init(struct ibm_init_struct *iibm) 4875 { 4876 int res; 4877 int status = 0; 4878 4879 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, 4880 "initializing uwb subdriver\n"); 4881 4882 TPACPI_ACPIHANDLE_INIT(hkey); 4883 4884 tp_features.uwb = hkey_handle && 4885 acpi_evalf(hkey_handle, &status, "GUWB", "qd"); 4886 4887 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL, 4888 "uwb is %s, status 0x%02x\n", 4889 str_supported(tp_features.uwb), 4890 status); 4891 4892 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 4893 if (dbg_uwbemul) { 4894 tp_features.uwb = 1; 4895 pr_info("uwb switch emulation enabled\n"); 4896 } else 4897 #endif 4898 if (tp_features.uwb && 4899 !(status & TP_ACPI_UWB_HWPRESENT)) { 4900 /* no uwb hardware present in system */ 4901 tp_features.uwb = 0; 4902 dbg_printk(TPACPI_DBG_INIT, 4903 "uwb hardware not installed\n"); 4904 } 4905 4906 if (!tp_features.uwb) 4907 return -ENODEV; 4908 4909 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID, 4910 &uwb_tprfk_ops, 4911 RFKILL_TYPE_UWB, 4912 TPACPI_RFK_UWB_SW_NAME, 4913 false); 4914 return res; 4915 } 4916 4917 static struct ibm_struct uwb_driver_data = { 4918 .name = "uwb", 4919 .exit = uwb_exit, 4920 .flags.experimental = 1, 4921 }; 4922 4923 /************************************************************************* 4924 * Video subdriver 4925 */ 4926 4927 #ifdef CONFIG_THINKPAD_ACPI_VIDEO 4928 4929 enum video_access_mode { 4930 TPACPI_VIDEO_NONE = 0, 4931 TPACPI_VIDEO_570, /* 570 */ 4932 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */ 4933 TPACPI_VIDEO_NEW, /* all others */ 4934 }; 4935 4936 enum { /* video status flags, based on VIDEO_570 */ 4937 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */ 4938 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */ 4939 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */ 4940 }; 4941 4942 enum { /* TPACPI_VIDEO_570 constants */ 4943 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */ 4944 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to 4945 * video_status_flags */ 4946 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */ 4947 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */ 4948 }; 4949 4950 static enum video_access_mode video_supported; 4951 static int video_orig_autosw; 4952 4953 static int video_autosw_get(void); 4954 static int video_autosw_set(int enable); 4955 4956 TPACPI_HANDLE(vid, root, 4957 "\\_SB.PCI.AGP.VGA", /* 570 */ 4958 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */ 4959 "\\_SB.PCI0.VID0", /* 770e */ 4960 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */ 4961 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */ 4962 "\\_SB.PCI0.AGP.VID", /* all others */ 4963 ); /* R30, R31 */ 4964 4965 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */ 4966 4967 static int __init video_init(struct ibm_init_struct *iibm) 4968 { 4969 int ivga; 4970 4971 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n"); 4972 4973 TPACPI_ACPIHANDLE_INIT(vid); 4974 if (tpacpi_is_ibm()) 4975 TPACPI_ACPIHANDLE_INIT(vid2); 4976 4977 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga) 4978 /* G41, assume IVGA doesn't change */ 4979 vid_handle = vid2_handle; 4980 4981 if (!vid_handle) 4982 /* video switching not supported on R30, R31 */ 4983 video_supported = TPACPI_VIDEO_NONE; 4984 else if (tpacpi_is_ibm() && 4985 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd")) 4986 /* 570 */ 4987 video_supported = TPACPI_VIDEO_570; 4988 else if (tpacpi_is_ibm() && 4989 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd")) 4990 /* 600e/x, 770e, 770x */ 4991 video_supported = TPACPI_VIDEO_770; 4992 else 4993 /* all others */ 4994 video_supported = TPACPI_VIDEO_NEW; 4995 4996 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n", 4997 str_supported(video_supported != TPACPI_VIDEO_NONE), 4998 video_supported); 4999 5000 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : -ENODEV; 5001 } 5002 5003 static void video_exit(void) 5004 { 5005 dbg_printk(TPACPI_DBG_EXIT, 5006 "restoring original video autoswitch mode\n"); 5007 if (video_autosw_set(video_orig_autosw)) 5008 pr_err("error while trying to restore original video autoswitch mode\n"); 5009 } 5010 5011 static int video_outputsw_get(void) 5012 { 5013 int status = 0; 5014 int i; 5015 5016 switch (video_supported) { 5017 case TPACPI_VIDEO_570: 5018 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd", 5019 TP_ACPI_VIDEO_570_PHSCMD)) 5020 return -EIO; 5021 status = i & TP_ACPI_VIDEO_570_PHSMASK; 5022 break; 5023 case TPACPI_VIDEO_770: 5024 if (!acpi_evalf(NULL, &i, "\\VCDL", "d")) 5025 return -EIO; 5026 if (i) 5027 status |= TP_ACPI_VIDEO_S_LCD; 5028 if (!acpi_evalf(NULL, &i, "\\VCDC", "d")) 5029 return -EIO; 5030 if (i) 5031 status |= TP_ACPI_VIDEO_S_CRT; 5032 break; 5033 case TPACPI_VIDEO_NEW: 5034 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) || 5035 !acpi_evalf(NULL, &i, "\\VCDC", "d")) 5036 return -EIO; 5037 if (i) 5038 status |= TP_ACPI_VIDEO_S_CRT; 5039 5040 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) || 5041 !acpi_evalf(NULL, &i, "\\VCDL", "d")) 5042 return -EIO; 5043 if (i) 5044 status |= TP_ACPI_VIDEO_S_LCD; 5045 if (!acpi_evalf(NULL, &i, "\\VCDD", "d")) 5046 return -EIO; 5047 if (i) 5048 status |= TP_ACPI_VIDEO_S_DVI; 5049 break; 5050 default: 5051 return -ENOSYS; 5052 } 5053 5054 return status; 5055 } 5056 5057 static int video_outputsw_set(int status) 5058 { 5059 int autosw; 5060 int res = 0; 5061 5062 switch (video_supported) { 5063 case TPACPI_VIDEO_570: 5064 res = acpi_evalf(NULL, NULL, 5065 "\\_SB.PHS2", "vdd", 5066 TP_ACPI_VIDEO_570_PHS2CMD, 5067 status | TP_ACPI_VIDEO_570_PHS2SET); 5068 break; 5069 case TPACPI_VIDEO_770: 5070 autosw = video_autosw_get(); 5071 if (autosw < 0) 5072 return autosw; 5073 5074 res = video_autosw_set(1); 5075 if (res) 5076 return res; 5077 res = acpi_evalf(vid_handle, NULL, 5078 "ASWT", "vdd", status * 0x100, 0); 5079 if (!autosw && video_autosw_set(autosw)) { 5080 pr_err("video auto-switch left enabled due to error\n"); 5081 return -EIO; 5082 } 5083 break; 5084 case TPACPI_VIDEO_NEW: 5085 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) && 5086 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1); 5087 break; 5088 default: 5089 return -ENOSYS; 5090 } 5091 5092 return (res) ? 0 : -EIO; 5093 } 5094 5095 static int video_autosw_get(void) 5096 { 5097 int autosw = 0; 5098 5099 switch (video_supported) { 5100 case TPACPI_VIDEO_570: 5101 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d")) 5102 return -EIO; 5103 break; 5104 case TPACPI_VIDEO_770: 5105 case TPACPI_VIDEO_NEW: 5106 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d")) 5107 return -EIO; 5108 break; 5109 default: 5110 return -ENOSYS; 5111 } 5112 5113 return autosw & 1; 5114 } 5115 5116 static int video_autosw_set(int enable) 5117 { 5118 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0)) 5119 return -EIO; 5120 return 0; 5121 } 5122 5123 static int video_outputsw_cycle(void) 5124 { 5125 int autosw = video_autosw_get(); 5126 int res; 5127 5128 if (autosw < 0) 5129 return autosw; 5130 5131 switch (video_supported) { 5132 case TPACPI_VIDEO_570: 5133 res = video_autosw_set(1); 5134 if (res) 5135 return res; 5136 res = acpi_evalf(ec_handle, NULL, "_Q16", "v"); 5137 break; 5138 case TPACPI_VIDEO_770: 5139 case TPACPI_VIDEO_NEW: 5140 res = video_autosw_set(1); 5141 if (res) 5142 return res; 5143 res = acpi_evalf(vid_handle, NULL, "VSWT", "v"); 5144 break; 5145 default: 5146 return -ENOSYS; 5147 } 5148 if (!autosw && video_autosw_set(autosw)) { 5149 pr_err("video auto-switch left enabled due to error\n"); 5150 return -EIO; 5151 } 5152 5153 return (res) ? 0 : -EIO; 5154 } 5155 5156 static int video_expand_toggle(void) 5157 { 5158 switch (video_supported) { 5159 case TPACPI_VIDEO_570: 5160 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ? 5161 0 : -EIO; 5162 case TPACPI_VIDEO_770: 5163 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ? 5164 0 : -EIO; 5165 case TPACPI_VIDEO_NEW: 5166 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ? 5167 0 : -EIO; 5168 default: 5169 return -ENOSYS; 5170 } 5171 /* not reached */ 5172 } 5173 5174 static int video_read(struct seq_file *m) 5175 { 5176 int status, autosw; 5177 5178 if (video_supported == TPACPI_VIDEO_NONE) { 5179 seq_printf(m, "status:\t\tnot supported\n"); 5180 return 0; 5181 } 5182 5183 /* Even reads can crash X.org, so... */ 5184 if (!capable(CAP_SYS_ADMIN)) 5185 return -EPERM; 5186 5187 status = video_outputsw_get(); 5188 if (status < 0) 5189 return status; 5190 5191 autosw = video_autosw_get(); 5192 if (autosw < 0) 5193 return autosw; 5194 5195 seq_printf(m, "status:\t\tsupported\n"); 5196 seq_printf(m, "lcd:\t\t%s\n", str_enabled_disabled(status & BIT(0))); 5197 seq_printf(m, "crt:\t\t%s\n", str_enabled_disabled(status & BIT(1))); 5198 if (video_supported == TPACPI_VIDEO_NEW) 5199 seq_printf(m, "dvi:\t\t%s\n", str_enabled_disabled(status & BIT(3))); 5200 seq_printf(m, "auto:\t\t%s\n", str_enabled_disabled(autosw & BIT(0))); 5201 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n"); 5202 seq_printf(m, "commands:\tcrt_enable, crt_disable\n"); 5203 if (video_supported == TPACPI_VIDEO_NEW) 5204 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n"); 5205 seq_printf(m, "commands:\tauto_enable, auto_disable\n"); 5206 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n"); 5207 5208 return 0; 5209 } 5210 5211 static int video_write(char *buf) 5212 { 5213 char *cmd; 5214 int enable, disable, status; 5215 int res; 5216 5217 if (video_supported == TPACPI_VIDEO_NONE) 5218 return -ENODEV; 5219 5220 /* Even reads can crash X.org, let alone writes... */ 5221 if (!capable(CAP_SYS_ADMIN)) 5222 return -EPERM; 5223 5224 enable = 0; 5225 disable = 0; 5226 5227 while ((cmd = strsep(&buf, ","))) { 5228 if (strlencmp(cmd, "lcd_enable") == 0) { 5229 enable |= TP_ACPI_VIDEO_S_LCD; 5230 } else if (strlencmp(cmd, "lcd_disable") == 0) { 5231 disable |= TP_ACPI_VIDEO_S_LCD; 5232 } else if (strlencmp(cmd, "crt_enable") == 0) { 5233 enable |= TP_ACPI_VIDEO_S_CRT; 5234 } else if (strlencmp(cmd, "crt_disable") == 0) { 5235 disable |= TP_ACPI_VIDEO_S_CRT; 5236 } else if (video_supported == TPACPI_VIDEO_NEW && 5237 strlencmp(cmd, "dvi_enable") == 0) { 5238 enable |= TP_ACPI_VIDEO_S_DVI; 5239 } else if (video_supported == TPACPI_VIDEO_NEW && 5240 strlencmp(cmd, "dvi_disable") == 0) { 5241 disable |= TP_ACPI_VIDEO_S_DVI; 5242 } else if (strlencmp(cmd, "auto_enable") == 0) { 5243 res = video_autosw_set(1); 5244 if (res) 5245 return res; 5246 } else if (strlencmp(cmd, "auto_disable") == 0) { 5247 res = video_autosw_set(0); 5248 if (res) 5249 return res; 5250 } else if (strlencmp(cmd, "video_switch") == 0) { 5251 res = video_outputsw_cycle(); 5252 if (res) 5253 return res; 5254 } else if (strlencmp(cmd, "expand_toggle") == 0) { 5255 res = video_expand_toggle(); 5256 if (res) 5257 return res; 5258 } else 5259 return -EINVAL; 5260 } 5261 5262 if (enable || disable) { 5263 status = video_outputsw_get(); 5264 if (status < 0) 5265 return status; 5266 res = video_outputsw_set((status & ~disable) | enable); 5267 if (res) 5268 return res; 5269 } 5270 5271 return 0; 5272 } 5273 5274 static struct ibm_struct video_driver_data = { 5275 .name = "video", 5276 .read = video_read, 5277 .write = video_write, 5278 .exit = video_exit, 5279 }; 5280 5281 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */ 5282 5283 /************************************************************************* 5284 * Keyboard backlight subdriver 5285 */ 5286 5287 static enum led_brightness kbdlight_brightness; 5288 static DEFINE_MUTEX(kbdlight_mutex); 5289 5290 static int kbdlight_set_level(int level) 5291 { 5292 int ret = 0; 5293 5294 if (!hkey_handle) 5295 return -ENXIO; 5296 5297 mutex_lock(&kbdlight_mutex); 5298 5299 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level)) 5300 ret = -EIO; 5301 else 5302 kbdlight_brightness = level; 5303 5304 mutex_unlock(&kbdlight_mutex); 5305 5306 return ret; 5307 } 5308 5309 static int kbdlight_get_level(void) 5310 { 5311 int status = 0; 5312 5313 if (!hkey_handle) 5314 return -ENXIO; 5315 5316 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0)) 5317 return -EIO; 5318 5319 if (status < 0) 5320 return status; 5321 5322 return status & 0x3; 5323 } 5324 5325 static bool kbdlight_is_supported(void) 5326 { 5327 int status = 0; 5328 5329 if (!hkey_handle) 5330 return false; 5331 5332 if (!acpi_has_method(hkey_handle, "MLCG")) { 5333 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n"); 5334 return false; 5335 } 5336 5337 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) { 5338 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n"); 5339 return false; 5340 } 5341 5342 if (status < 0) { 5343 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status); 5344 return false; 5345 } 5346 5347 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status); 5348 /* 5349 * Guessed test for keyboard backlight: 5350 * 5351 * Machines with backlight keyboard return: 5352 * b010100000010000000XX - ThinkPad X1 Carbon 3rd 5353 * b110100010010000000XX - ThinkPad x230 5354 * b010100000010000000XX - ThinkPad x240 5355 * b010100000010000000XX - ThinkPad W541 5356 * (XX is current backlight level) 5357 * 5358 * Machines without backlight keyboard return: 5359 * b10100001000000000000 - ThinkPad x230 5360 * b10110001000000000000 - ThinkPad E430 5361 * b00000000000000000000 - ThinkPad E450 5362 * 5363 * Candidate BITs for detection test (XOR): 5364 * b01000000001000000000 5365 * ^ 5366 */ 5367 return status & BIT(9); 5368 } 5369 5370 static int kbdlight_sysfs_set(struct led_classdev *led_cdev, 5371 enum led_brightness brightness) 5372 { 5373 return kbdlight_set_level(brightness); 5374 } 5375 5376 static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev) 5377 { 5378 int level; 5379 5380 level = kbdlight_get_level(); 5381 if (level < 0) 5382 return 0; 5383 5384 return level; 5385 } 5386 5387 static struct tpacpi_led_classdev tpacpi_led_kbdlight = { 5388 .led_classdev = { 5389 .name = "tpacpi::kbd_backlight", 5390 .max_brightness = 2, 5391 .flags = LED_BRIGHT_HW_CHANGED, 5392 .brightness_set_blocking = &kbdlight_sysfs_set, 5393 .brightness_get = &kbdlight_sysfs_get, 5394 } 5395 }; 5396 5397 static int __init kbdlight_init(struct ibm_init_struct *iibm) 5398 { 5399 int rc; 5400 5401 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n"); 5402 5403 TPACPI_ACPIHANDLE_INIT(hkey); 5404 5405 if (!kbdlight_is_supported()) { 5406 tp_features.kbdlight = 0; 5407 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n"); 5408 return -ENODEV; 5409 } 5410 5411 kbdlight_brightness = kbdlight_sysfs_get(NULL); 5412 tp_features.kbdlight = 1; 5413 5414 rc = led_classdev_register(&tpacpi_pdev->dev, 5415 &tpacpi_led_kbdlight.led_classdev); 5416 if (rc < 0) { 5417 tp_features.kbdlight = 0; 5418 return rc; 5419 } 5420 5421 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask | 5422 TP_ACPI_HKEY_KBD_LIGHT_MASK); 5423 return 0; 5424 } 5425 5426 static void kbdlight_exit(void) 5427 { 5428 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev); 5429 } 5430 5431 static int kbdlight_set_level_and_update(int level) 5432 { 5433 int ret; 5434 struct led_classdev *led_cdev; 5435 5436 ret = kbdlight_set_level(level); 5437 led_cdev = &tpacpi_led_kbdlight.led_classdev; 5438 5439 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED)) 5440 led_cdev->brightness = level; 5441 5442 return ret; 5443 } 5444 5445 static int kbdlight_read(struct seq_file *m) 5446 { 5447 int level; 5448 5449 if (!tp_features.kbdlight) { 5450 seq_printf(m, "status:\t\tnot supported\n"); 5451 } else { 5452 level = kbdlight_get_level(); 5453 if (level < 0) 5454 seq_printf(m, "status:\t\terror %d\n", level); 5455 else 5456 seq_printf(m, "status:\t\t%d\n", level); 5457 seq_printf(m, "commands:\t0, 1, 2\n"); 5458 } 5459 5460 return 0; 5461 } 5462 5463 static int kbdlight_write(char *buf) 5464 { 5465 char *cmd; 5466 int res, level = -EINVAL; 5467 5468 if (!tp_features.kbdlight) 5469 return -ENODEV; 5470 5471 while ((cmd = strsep(&buf, ","))) { 5472 res = kstrtoint(cmd, 10, &level); 5473 if (res < 0) 5474 return res; 5475 } 5476 5477 if (level >= 3 || level < 0) 5478 return -EINVAL; 5479 5480 return kbdlight_set_level_and_update(level); 5481 } 5482 5483 static void kbdlight_suspend(void) 5484 { 5485 struct led_classdev *led_cdev; 5486 5487 if (!tp_features.kbdlight) 5488 return; 5489 5490 led_cdev = &tpacpi_led_kbdlight.led_classdev; 5491 led_update_brightness(led_cdev); 5492 led_classdev_suspend(led_cdev); 5493 } 5494 5495 static void kbdlight_resume(void) 5496 { 5497 if (!tp_features.kbdlight) 5498 return; 5499 5500 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev); 5501 } 5502 5503 static struct ibm_struct kbdlight_driver_data = { 5504 .name = "kbdlight", 5505 .read = kbdlight_read, 5506 .write = kbdlight_write, 5507 .suspend = kbdlight_suspend, 5508 .resume = kbdlight_resume, 5509 .exit = kbdlight_exit, 5510 }; 5511 5512 /************************************************************************* 5513 * Light (thinklight) subdriver 5514 */ 5515 5516 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */ 5517 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */ 5518 5519 static int light_get_status(void) 5520 { 5521 int status = 0; 5522 5523 if (tp_features.light_status) { 5524 if (!acpi_evalf(ec_handle, &status, "KBLT", "d")) 5525 return -EIO; 5526 return (!!status); 5527 } 5528 5529 return -ENXIO; 5530 } 5531 5532 static int light_set_status(int status) 5533 { 5534 int rc; 5535 5536 if (tp_features.light) { 5537 if (cmos_handle) { 5538 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd", 5539 (status) ? 5540 TP_CMOS_THINKLIGHT_ON : 5541 TP_CMOS_THINKLIGHT_OFF); 5542 } else { 5543 rc = acpi_evalf(lght_handle, NULL, NULL, "vd", 5544 (status) ? 1 : 0); 5545 } 5546 return (rc) ? 0 : -EIO; 5547 } 5548 5549 return -ENXIO; 5550 } 5551 5552 static int light_sysfs_set(struct led_classdev *led_cdev, 5553 enum led_brightness brightness) 5554 { 5555 return light_set_status((brightness != LED_OFF) ? 5556 TPACPI_LED_ON : TPACPI_LED_OFF); 5557 } 5558 5559 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev) 5560 { 5561 return (light_get_status() == 1) ? LED_FULL : LED_OFF; 5562 } 5563 5564 static struct tpacpi_led_classdev tpacpi_led_thinklight = { 5565 .led_classdev = { 5566 .name = "tpacpi::thinklight", 5567 .brightness_set_blocking = &light_sysfs_set, 5568 .brightness_get = &light_sysfs_get, 5569 } 5570 }; 5571 5572 static int __init light_init(struct ibm_init_struct *iibm) 5573 { 5574 int rc; 5575 5576 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n"); 5577 5578 if (tpacpi_is_ibm()) { 5579 TPACPI_ACPIHANDLE_INIT(ledb); 5580 TPACPI_ACPIHANDLE_INIT(lght); 5581 } 5582 TPACPI_ACPIHANDLE_INIT(cmos); 5583 5584 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */ 5585 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle; 5586 5587 if (tp_features.light) 5588 /* light status not supported on 5589 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */ 5590 tp_features.light_status = 5591 acpi_evalf(ec_handle, NULL, "KBLT", "qv"); 5592 5593 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n", 5594 str_supported(tp_features.light), 5595 str_supported(tp_features.light_status)); 5596 5597 if (!tp_features.light) 5598 return -ENODEV; 5599 5600 rc = led_classdev_register(&tpacpi_pdev->dev, 5601 &tpacpi_led_thinklight.led_classdev); 5602 5603 if (rc < 0) { 5604 tp_features.light = 0; 5605 tp_features.light_status = 0; 5606 } else { 5607 rc = 0; 5608 } 5609 5610 return rc; 5611 } 5612 5613 static void light_exit(void) 5614 { 5615 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev); 5616 } 5617 5618 static int light_read(struct seq_file *m) 5619 { 5620 int status; 5621 5622 if (!tp_features.light) { 5623 seq_printf(m, "status:\t\tnot supported\n"); 5624 } else if (!tp_features.light_status) { 5625 seq_printf(m, "status:\t\tunknown\n"); 5626 seq_printf(m, "commands:\ton, off\n"); 5627 } else { 5628 status = light_get_status(); 5629 if (status < 0) 5630 return status; 5631 seq_printf(m, "status:\t\t%s\n", str_on_off(status & BIT(0))); 5632 seq_printf(m, "commands:\ton, off\n"); 5633 } 5634 5635 return 0; 5636 } 5637 5638 static int light_write(char *buf) 5639 { 5640 char *cmd; 5641 int newstatus = 0; 5642 5643 if (!tp_features.light) 5644 return -ENODEV; 5645 5646 while ((cmd = strsep(&buf, ","))) { 5647 if (strlencmp(cmd, "on") == 0) { 5648 newstatus = 1; 5649 } else if (strlencmp(cmd, "off") == 0) { 5650 newstatus = 0; 5651 } else 5652 return -EINVAL; 5653 } 5654 5655 return light_set_status(newstatus); 5656 } 5657 5658 static struct ibm_struct light_driver_data = { 5659 .name = "light", 5660 .read = light_read, 5661 .write = light_write, 5662 .exit = light_exit, 5663 }; 5664 5665 /************************************************************************* 5666 * CMOS subdriver 5667 */ 5668 5669 /* sysfs cmos_command -------------------------------------------------- */ 5670 static ssize_t cmos_command_store(struct device *dev, 5671 struct device_attribute *attr, 5672 const char *buf, size_t count) 5673 { 5674 unsigned long cmos_cmd; 5675 int res; 5676 5677 if (parse_strtoul(buf, 21, &cmos_cmd)) 5678 return -EINVAL; 5679 5680 res = issue_thinkpad_cmos_command(cmos_cmd); 5681 return (res) ? res : count; 5682 } 5683 5684 static DEVICE_ATTR_WO(cmos_command); 5685 5686 static struct attribute *cmos_attributes[] = { 5687 &dev_attr_cmos_command.attr, 5688 NULL 5689 }; 5690 5691 static umode_t cmos_attr_is_visible(struct kobject *kobj, 5692 struct attribute *attr, int n) 5693 { 5694 return cmos_handle ? attr->mode : 0; 5695 } 5696 5697 static const struct attribute_group cmos_attr_group = { 5698 .is_visible = cmos_attr_is_visible, 5699 .attrs = cmos_attributes, 5700 }; 5701 5702 /* --------------------------------------------------------------------- */ 5703 5704 static int __init cmos_init(struct ibm_init_struct *iibm) 5705 { 5706 vdbg_printk(TPACPI_DBG_INIT, 5707 "initializing cmos commands subdriver\n"); 5708 5709 TPACPI_ACPIHANDLE_INIT(cmos); 5710 5711 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n", 5712 str_supported(cmos_handle != NULL)); 5713 5714 return cmos_handle ? 0 : -ENODEV; 5715 } 5716 5717 static int cmos_read(struct seq_file *m) 5718 { 5719 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p, 5720 R30, R31, T20-22, X20-21 */ 5721 if (!cmos_handle) 5722 seq_printf(m, "status:\t\tnot supported\n"); 5723 else { 5724 seq_printf(m, "status:\t\tsupported\n"); 5725 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n"); 5726 } 5727 5728 return 0; 5729 } 5730 5731 static int cmos_write(char *buf) 5732 { 5733 char *cmd; 5734 int cmos_cmd, res; 5735 5736 while ((cmd = strsep(&buf, ","))) { 5737 if (sscanf(cmd, "%u", &cmos_cmd) == 1 && 5738 cmos_cmd >= 0 && cmos_cmd <= 21) { 5739 /* cmos_cmd set */ 5740 } else 5741 return -EINVAL; 5742 5743 res = issue_thinkpad_cmos_command(cmos_cmd); 5744 if (res) 5745 return res; 5746 } 5747 5748 return 0; 5749 } 5750 5751 static struct ibm_struct cmos_driver_data = { 5752 .name = "cmos", 5753 .read = cmos_read, 5754 .write = cmos_write, 5755 }; 5756 5757 /************************************************************************* 5758 * LED subdriver 5759 */ 5760 5761 enum led_access_mode { 5762 TPACPI_LED_NONE = 0, 5763 TPACPI_LED_570, /* 570 */ 5764 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */ 5765 TPACPI_LED_NEW, /* all others */ 5766 }; 5767 5768 enum { /* For TPACPI_LED_OLD */ 5769 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */ 5770 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */ 5771 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */ 5772 }; 5773 5774 static enum led_access_mode led_supported; 5775 5776 static acpi_handle led_handle; 5777 5778 #define TPACPI_LED_NUMLEDS 16 5779 static struct tpacpi_led_classdev *tpacpi_leds; 5780 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS]; 5781 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = { 5782 /* there's a limit of 19 chars + NULL before 2.6.26 */ 5783 "tpacpi::power", 5784 "tpacpi:orange:batt", 5785 "tpacpi:green:batt", 5786 "tpacpi::dock_active", 5787 "tpacpi::bay_active", 5788 "tpacpi::dock_batt", 5789 "tpacpi::unknown_led", 5790 "tpacpi::standby", 5791 "tpacpi::dock_status1", 5792 "tpacpi::dock_status2", 5793 "tpacpi::lid_logo_dot", 5794 "tpacpi::unknown_led3", 5795 "tpacpi::thinkvantage", 5796 }; 5797 #define TPACPI_SAFE_LEDS 0x1481U 5798 5799 static inline bool tpacpi_is_led_restricted(const unsigned int led) 5800 { 5801 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS 5802 return false; 5803 #else 5804 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0; 5805 #endif 5806 } 5807 5808 static int led_get_status(const unsigned int led) 5809 { 5810 int status; 5811 enum led_status_t led_s; 5812 5813 switch (led_supported) { 5814 case TPACPI_LED_570: 5815 if (!acpi_evalf(ec_handle, 5816 &status, "GLED", "dd", 1 << led)) 5817 return -EIO; 5818 led_s = (status == 0) ? 5819 TPACPI_LED_OFF : 5820 ((status == 1) ? 5821 TPACPI_LED_ON : 5822 TPACPI_LED_BLINK); 5823 tpacpi_led_state_cache[led] = led_s; 5824 return led_s; 5825 default: 5826 return -ENXIO; 5827 } 5828 5829 /* not reached */ 5830 } 5831 5832 static int led_set_status(const unsigned int led, 5833 const enum led_status_t ledstatus) 5834 { 5835 /* off, on, blink. Index is led_status_t */ 5836 static const unsigned int led_sled_arg1[] = { 0, 1, 3 }; 5837 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 }; 5838 5839 int rc = 0; 5840 5841 switch (led_supported) { 5842 case TPACPI_LED_570: 5843 /* 570 */ 5844 if (unlikely(led > 7)) 5845 return -EINVAL; 5846 if (unlikely(tpacpi_is_led_restricted(led))) 5847 return -EPERM; 5848 if (!acpi_evalf(led_handle, NULL, NULL, "vdd", 5849 (1 << led), led_sled_arg1[ledstatus])) 5850 return -EIO; 5851 break; 5852 case TPACPI_LED_OLD: 5853 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */ 5854 if (unlikely(led > 7)) 5855 return -EINVAL; 5856 if (unlikely(tpacpi_is_led_restricted(led))) 5857 return -EPERM; 5858 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led)); 5859 if (rc >= 0) 5860 rc = ec_write(TPACPI_LED_EC_HLBL, 5861 (ledstatus == TPACPI_LED_BLINK) << led); 5862 if (rc >= 0) 5863 rc = ec_write(TPACPI_LED_EC_HLCL, 5864 (ledstatus != TPACPI_LED_OFF) << led); 5865 break; 5866 case TPACPI_LED_NEW: 5867 /* all others */ 5868 if (unlikely(led >= TPACPI_LED_NUMLEDS)) 5869 return -EINVAL; 5870 if (unlikely(tpacpi_is_led_restricted(led))) 5871 return -EPERM; 5872 if (!acpi_evalf(led_handle, NULL, NULL, "vdd", 5873 led, led_led_arg1[ledstatus])) 5874 return -EIO; 5875 break; 5876 default: 5877 return -ENXIO; 5878 } 5879 5880 if (!rc) 5881 tpacpi_led_state_cache[led] = ledstatus; 5882 5883 return rc; 5884 } 5885 5886 static int led_sysfs_set(struct led_classdev *led_cdev, 5887 enum led_brightness brightness) 5888 { 5889 struct tpacpi_led_classdev *data = container_of(led_cdev, 5890 struct tpacpi_led_classdev, led_classdev); 5891 enum led_status_t new_state; 5892 5893 if (brightness == LED_OFF) 5894 new_state = TPACPI_LED_OFF; 5895 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK) 5896 new_state = TPACPI_LED_ON; 5897 else 5898 new_state = TPACPI_LED_BLINK; 5899 5900 return led_set_status(data->led, new_state); 5901 } 5902 5903 static int led_sysfs_blink_set(struct led_classdev *led_cdev, 5904 unsigned long *delay_on, unsigned long *delay_off) 5905 { 5906 struct tpacpi_led_classdev *data = container_of(led_cdev, 5907 struct tpacpi_led_classdev, led_classdev); 5908 5909 /* Can we choose the flash rate? */ 5910 if (*delay_on == 0 && *delay_off == 0) { 5911 /* yes. set them to the hardware blink rate (1 Hz) */ 5912 *delay_on = 500; /* ms */ 5913 *delay_off = 500; /* ms */ 5914 } else if ((*delay_on != 500) || (*delay_off != 500)) 5915 return -EINVAL; 5916 5917 return led_set_status(data->led, TPACPI_LED_BLINK); 5918 } 5919 5920 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev) 5921 { 5922 int rc; 5923 5924 struct tpacpi_led_classdev *data = container_of(led_cdev, 5925 struct tpacpi_led_classdev, led_classdev); 5926 5927 rc = led_get_status(data->led); 5928 5929 if (rc == TPACPI_LED_OFF || rc < 0) 5930 rc = LED_OFF; /* no error handling in led class :( */ 5931 else 5932 rc = LED_FULL; 5933 5934 return rc; 5935 } 5936 5937 static void led_exit(void) 5938 { 5939 unsigned int i; 5940 5941 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) 5942 led_classdev_unregister(&tpacpi_leds[i].led_classdev); 5943 5944 kfree(tpacpi_leds); 5945 } 5946 5947 static int __init tpacpi_init_led(unsigned int led) 5948 { 5949 /* LEDs with no name don't get registered */ 5950 if (!tpacpi_led_names[led]) 5951 return 0; 5952 5953 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set; 5954 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set; 5955 if (led_supported == TPACPI_LED_570) 5956 tpacpi_leds[led].led_classdev.brightness_get = &led_sysfs_get; 5957 5958 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led]; 5959 tpacpi_leds[led].led_classdev.flags = LED_RETAIN_AT_SHUTDOWN; 5960 tpacpi_leds[led].led = led; 5961 5962 return led_classdev_register(&tpacpi_pdev->dev, &tpacpi_leds[led].led_classdev); 5963 } 5964 5965 static const struct tpacpi_quirk led_useful_qtable[] __initconst = { 5966 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */ 5967 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */ 5968 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */ 5969 5970 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */ 5971 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */ 5972 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */ 5973 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */ 5974 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */ 5975 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */ 5976 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */ 5977 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */ 5978 5979 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */ 5980 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */ 5981 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */ 5982 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */ 5983 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */ 5984 5985 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */ 5986 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */ 5987 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */ 5988 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */ 5989 5990 /* (1) - may have excess leds enabled on MSB */ 5991 5992 /* Defaults (order matters, keep last, don't reorder!) */ 5993 { /* Lenovo */ 5994 .vendor = PCI_VENDOR_ID_LENOVO, 5995 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY, 5996 .quirks = 0x1fffU, 5997 }, 5998 { /* IBM ThinkPads with no EC version string */ 5999 .vendor = PCI_VENDOR_ID_IBM, 6000 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN, 6001 .quirks = 0x00ffU, 6002 }, 6003 { /* IBM ThinkPads with EC version string */ 6004 .vendor = PCI_VENDOR_ID_IBM, 6005 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY, 6006 .quirks = 0x00bfU, 6007 }, 6008 }; 6009 6010 static enum led_access_mode __init led_init_detect_mode(void) 6011 { 6012 acpi_status status; 6013 6014 if (tpacpi_is_ibm()) { 6015 /* 570 */ 6016 status = acpi_get_handle(ec_handle, "SLED", &led_handle); 6017 if (ACPI_SUCCESS(status)) 6018 return TPACPI_LED_570; 6019 6020 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */ 6021 status = acpi_get_handle(ec_handle, "SYSL", &led_handle); 6022 if (ACPI_SUCCESS(status)) 6023 return TPACPI_LED_OLD; 6024 } 6025 6026 /* most others */ 6027 status = acpi_get_handle(ec_handle, "LED", &led_handle); 6028 if (ACPI_SUCCESS(status)) 6029 return TPACPI_LED_NEW; 6030 6031 /* R30, R31, and unknown firmwares */ 6032 led_handle = NULL; 6033 return TPACPI_LED_NONE; 6034 } 6035 6036 static int __init led_init(struct ibm_init_struct *iibm) 6037 { 6038 unsigned int i; 6039 int rc; 6040 unsigned long useful_leds; 6041 6042 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n"); 6043 6044 led_supported = led_init_detect_mode(); 6045 6046 if (led_supported != TPACPI_LED_NONE) { 6047 useful_leds = tpacpi_check_quirks(led_useful_qtable, 6048 ARRAY_SIZE(led_useful_qtable)); 6049 6050 if (!useful_leds) { 6051 led_handle = NULL; 6052 led_supported = TPACPI_LED_NONE; 6053 } 6054 } 6055 6056 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n", 6057 str_supported(led_supported), led_supported); 6058 6059 if (led_supported == TPACPI_LED_NONE) 6060 return -ENODEV; 6061 6062 tpacpi_leds = kcalloc(TPACPI_LED_NUMLEDS, sizeof(*tpacpi_leds), 6063 GFP_KERNEL); 6064 if (!tpacpi_leds) { 6065 pr_err("Out of memory for LED data\n"); 6066 return -ENOMEM; 6067 } 6068 6069 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) { 6070 tpacpi_leds[i].led = -1; 6071 6072 if (!tpacpi_is_led_restricted(i) && test_bit(i, &useful_leds)) { 6073 rc = tpacpi_init_led(i); 6074 if (rc < 0) { 6075 led_exit(); 6076 return rc; 6077 } 6078 } 6079 } 6080 6081 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS 6082 pr_notice("warning: userspace override of important firmware LEDs is enabled\n"); 6083 #endif 6084 return 0; 6085 } 6086 6087 #define str_led_status(s) ((s) >= TPACPI_LED_BLINK ? "blinking" : str_on_off(s)) 6088 6089 static int led_read(struct seq_file *m) 6090 { 6091 if (!led_supported) { 6092 seq_printf(m, "status:\t\tnot supported\n"); 6093 return 0; 6094 } 6095 seq_printf(m, "status:\t\tsupported\n"); 6096 6097 if (led_supported == TPACPI_LED_570) { 6098 /* 570 */ 6099 int i, status; 6100 for (i = 0; i < 8; i++) { 6101 status = led_get_status(i); 6102 if (status < 0) 6103 return -EIO; 6104 seq_printf(m, "%d:\t\t%s\n", i, str_led_status(status)); 6105 } 6106 } 6107 6108 seq_printf(m, "commands:\t<led> on, <led> off, <led> blink (<led> is 0-15)\n"); 6109 6110 return 0; 6111 } 6112 6113 static int led_write(char *buf) 6114 { 6115 char *cmd; 6116 int led, rc; 6117 enum led_status_t s; 6118 6119 if (!led_supported) 6120 return -ENODEV; 6121 6122 while ((cmd = strsep(&buf, ","))) { 6123 if (sscanf(cmd, "%d", &led) != 1) 6124 return -EINVAL; 6125 6126 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1)) 6127 return -ENODEV; 6128 6129 if (tpacpi_leds[led].led < 0) 6130 return -ENODEV; 6131 6132 if (strstr(cmd, "off")) { 6133 s = TPACPI_LED_OFF; 6134 } else if (strstr(cmd, "on")) { 6135 s = TPACPI_LED_ON; 6136 } else if (strstr(cmd, "blink")) { 6137 s = TPACPI_LED_BLINK; 6138 } else { 6139 return -EINVAL; 6140 } 6141 6142 rc = led_set_status(led, s); 6143 if (rc < 0) 6144 return rc; 6145 } 6146 6147 return 0; 6148 } 6149 6150 static struct ibm_struct led_driver_data = { 6151 .name = "led", 6152 .read = led_read, 6153 .write = led_write, 6154 .exit = led_exit, 6155 }; 6156 6157 /************************************************************************* 6158 * Beep subdriver 6159 */ 6160 6161 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */ 6162 6163 #define TPACPI_BEEP_Q1 0x0001 6164 6165 static const struct tpacpi_quirk beep_quirk_table[] __initconst = { 6166 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */ 6167 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */ 6168 }; 6169 6170 static int __init beep_init(struct ibm_init_struct *iibm) 6171 { 6172 unsigned long quirks; 6173 6174 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n"); 6175 6176 TPACPI_ACPIHANDLE_INIT(beep); 6177 6178 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n", 6179 str_supported(beep_handle != NULL)); 6180 6181 quirks = tpacpi_check_quirks(beep_quirk_table, 6182 ARRAY_SIZE(beep_quirk_table)); 6183 6184 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1); 6185 6186 return (beep_handle) ? 0 : -ENODEV; 6187 } 6188 6189 static int beep_read(struct seq_file *m) 6190 { 6191 if (!beep_handle) 6192 seq_printf(m, "status:\t\tnot supported\n"); 6193 else { 6194 seq_printf(m, "status:\t\tsupported\n"); 6195 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n"); 6196 } 6197 6198 return 0; 6199 } 6200 6201 static int beep_write(char *buf) 6202 { 6203 char *cmd; 6204 int beep_cmd; 6205 6206 if (!beep_handle) 6207 return -ENODEV; 6208 6209 while ((cmd = strsep(&buf, ","))) { 6210 if (sscanf(cmd, "%u", &beep_cmd) == 1 && 6211 beep_cmd >= 0 && beep_cmd <= 17) { 6212 /* beep_cmd set */ 6213 } else 6214 return -EINVAL; 6215 if (tp_features.beep_needs_two_args) { 6216 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", 6217 beep_cmd, 0)) 6218 return -EIO; 6219 } else { 6220 if (!acpi_evalf(beep_handle, NULL, NULL, "vd", 6221 beep_cmd)) 6222 return -EIO; 6223 } 6224 } 6225 6226 return 0; 6227 } 6228 6229 static struct ibm_struct beep_driver_data = { 6230 .name = "beep", 6231 .read = beep_read, 6232 .write = beep_write, 6233 }; 6234 6235 /************************************************************************* 6236 * Thermal subdriver 6237 */ 6238 6239 enum thermal_access_mode { 6240 TPACPI_THERMAL_NONE = 0, /* No thermal support */ 6241 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */ 6242 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */ 6243 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */ 6244 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */ 6245 }; 6246 6247 enum { /* TPACPI_THERMAL_TPEC_* */ 6248 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */ 6249 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */ 6250 TP_EC_FUNCREV = 0xEF, /* ACPI EC Functional revision */ 6251 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */ 6252 6253 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */ 6254 }; 6255 6256 6257 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */ 6258 struct ibm_thermal_sensors_struct { 6259 s32 temp[TPACPI_MAX_THERMAL_SENSORS]; 6260 }; 6261 6262 static enum thermal_access_mode thermal_read_mode; 6263 static bool thermal_use_labels; 6264 6265 /* idx is zero-based */ 6266 static int thermal_get_sensor(int idx, s32 *value) 6267 { 6268 int t; 6269 s8 tmp; 6270 char tmpi[5]; 6271 6272 t = TP_EC_THERMAL_TMP0; 6273 6274 switch (thermal_read_mode) { 6275 #if TPACPI_MAX_THERMAL_SENSORS >= 16 6276 case TPACPI_THERMAL_TPEC_16: 6277 if (idx >= 8 && idx <= 15) { 6278 t = TP_EC_THERMAL_TMP8; 6279 idx -= 8; 6280 } 6281 #endif 6282 fallthrough; 6283 case TPACPI_THERMAL_TPEC_8: 6284 if (idx <= 7) { 6285 if (!acpi_ec_read(t + idx, &tmp)) 6286 return -EIO; 6287 *value = tmp * 1000; 6288 return 0; 6289 } 6290 break; 6291 6292 case TPACPI_THERMAL_ACPI_UPDT: 6293 if (idx <= 7) { 6294 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx); 6295 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v")) 6296 return -EIO; 6297 if (!acpi_evalf(ec_handle, &t, tmpi, "d")) 6298 return -EIO; 6299 *value = (t - 2732) * 100; 6300 return 0; 6301 } 6302 break; 6303 6304 case TPACPI_THERMAL_ACPI_TMP07: 6305 if (idx <= 7) { 6306 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx); 6307 if (!acpi_evalf(ec_handle, &t, tmpi, "d")) 6308 return -EIO; 6309 if (t > 127 || t < -127) 6310 t = TP_EC_THERMAL_TMP_NA; 6311 *value = t * 1000; 6312 return 0; 6313 } 6314 break; 6315 6316 case TPACPI_THERMAL_NONE: 6317 default: 6318 return -ENOSYS; 6319 } 6320 6321 return -EINVAL; 6322 } 6323 6324 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s) 6325 { 6326 int res, i; 6327 int n; 6328 6329 n = 8; 6330 i = 0; 6331 6332 if (!s) 6333 return -EINVAL; 6334 6335 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16) 6336 n = 16; 6337 6338 for (i = 0 ; i < n; i++) { 6339 res = thermal_get_sensor(i, &s->temp[i]); 6340 if (res) 6341 return res; 6342 } 6343 6344 return n; 6345 } 6346 6347 static void thermal_dump_all_sensors(void) 6348 { 6349 int n, i; 6350 struct ibm_thermal_sensors_struct t; 6351 6352 n = thermal_get_sensors(&t); 6353 if (n <= 0) 6354 return; 6355 6356 pr_notice("temperatures (Celsius):"); 6357 6358 for (i = 0; i < n; i++) { 6359 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA) 6360 pr_cont(" %d", (int)(t.temp[i] / 1000)); 6361 else 6362 pr_cont(" N/A"); 6363 } 6364 6365 pr_cont("\n"); 6366 } 6367 6368 /* sysfs temp##_input -------------------------------------------------- */ 6369 6370 static ssize_t thermal_temp_input_show(struct device *dev, 6371 struct device_attribute *attr, 6372 char *buf) 6373 { 6374 struct sensor_device_attribute *sensor_attr = 6375 to_sensor_dev_attr(attr); 6376 int idx = sensor_attr->index; 6377 s32 value; 6378 int res; 6379 6380 res = thermal_get_sensor(idx, &value); 6381 if (res) 6382 return res; 6383 if (value == TPACPI_THERMAL_SENSOR_NA) 6384 return -ENXIO; 6385 6386 return sysfs_emit(buf, "%d\n", value); 6387 } 6388 6389 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \ 6390 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \ 6391 thermal_temp_input_show, NULL, _idxB) 6392 6393 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = { 6394 THERMAL_SENSOR_ATTR_TEMP(1, 0), 6395 THERMAL_SENSOR_ATTR_TEMP(2, 1), 6396 THERMAL_SENSOR_ATTR_TEMP(3, 2), 6397 THERMAL_SENSOR_ATTR_TEMP(4, 3), 6398 THERMAL_SENSOR_ATTR_TEMP(5, 4), 6399 THERMAL_SENSOR_ATTR_TEMP(6, 5), 6400 THERMAL_SENSOR_ATTR_TEMP(7, 6), 6401 THERMAL_SENSOR_ATTR_TEMP(8, 7), 6402 THERMAL_SENSOR_ATTR_TEMP(9, 8), 6403 THERMAL_SENSOR_ATTR_TEMP(10, 9), 6404 THERMAL_SENSOR_ATTR_TEMP(11, 10), 6405 THERMAL_SENSOR_ATTR_TEMP(12, 11), 6406 THERMAL_SENSOR_ATTR_TEMP(13, 12), 6407 THERMAL_SENSOR_ATTR_TEMP(14, 13), 6408 THERMAL_SENSOR_ATTR_TEMP(15, 14), 6409 THERMAL_SENSOR_ATTR_TEMP(16, 15), 6410 }; 6411 6412 #define THERMAL_ATTRS(X) \ 6413 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr 6414 6415 static struct attribute *thermal_temp_input_attr[] = { 6416 THERMAL_ATTRS(0), 6417 THERMAL_ATTRS(1), 6418 THERMAL_ATTRS(2), 6419 THERMAL_ATTRS(3), 6420 THERMAL_ATTRS(4), 6421 THERMAL_ATTRS(5), 6422 THERMAL_ATTRS(6), 6423 THERMAL_ATTRS(7), 6424 THERMAL_ATTRS(8), 6425 THERMAL_ATTRS(9), 6426 THERMAL_ATTRS(10), 6427 THERMAL_ATTRS(11), 6428 THERMAL_ATTRS(12), 6429 THERMAL_ATTRS(13), 6430 THERMAL_ATTRS(14), 6431 THERMAL_ATTRS(15), 6432 NULL 6433 }; 6434 6435 static umode_t thermal_attr_is_visible(struct kobject *kobj, 6436 struct attribute *attr, int n) 6437 { 6438 if (thermal_read_mode == TPACPI_THERMAL_NONE) 6439 return 0; 6440 6441 if (attr == THERMAL_ATTRS(8) || attr == THERMAL_ATTRS(9) || 6442 attr == THERMAL_ATTRS(10) || attr == THERMAL_ATTRS(11) || 6443 attr == THERMAL_ATTRS(12) || attr == THERMAL_ATTRS(13) || 6444 attr == THERMAL_ATTRS(14) || attr == THERMAL_ATTRS(15)) { 6445 if (thermal_read_mode != TPACPI_THERMAL_TPEC_16) 6446 return 0; 6447 } 6448 6449 return attr->mode; 6450 } 6451 6452 static const struct attribute_group thermal_attr_group = { 6453 .is_visible = thermal_attr_is_visible, 6454 .attrs = thermal_temp_input_attr, 6455 }; 6456 6457 #undef THERMAL_SENSOR_ATTR_TEMP 6458 #undef THERMAL_ATTRS 6459 6460 static ssize_t temp1_label_show(struct device *dev, struct device_attribute *attr, char *buf) 6461 { 6462 return sysfs_emit(buf, "CPU\n"); 6463 } 6464 static DEVICE_ATTR_RO(temp1_label); 6465 6466 static ssize_t temp2_label_show(struct device *dev, struct device_attribute *attr, char *buf) 6467 { 6468 return sysfs_emit(buf, "GPU\n"); 6469 } 6470 static DEVICE_ATTR_RO(temp2_label); 6471 6472 static struct attribute *temp_label_attributes[] = { 6473 &dev_attr_temp1_label.attr, 6474 &dev_attr_temp2_label.attr, 6475 NULL 6476 }; 6477 6478 static umode_t temp_label_attr_is_visible(struct kobject *kobj, 6479 struct attribute *attr, int n) 6480 { 6481 return thermal_use_labels ? attr->mode : 0; 6482 } 6483 6484 static const struct attribute_group temp_label_attr_group = { 6485 .is_visible = temp_label_attr_is_visible, 6486 .attrs = temp_label_attributes, 6487 }; 6488 6489 /* --------------------------------------------------------------------- */ 6490 6491 static int __init thermal_init(struct ibm_init_struct *iibm) 6492 { 6493 u8 t, ta1, ta2, ver = 0; 6494 int i; 6495 int acpi_tmp7; 6496 6497 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n"); 6498 6499 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv"); 6500 6501 if (thinkpad_id.ec_model) { 6502 /* 6503 * Direct EC access mode: sensors at registers 6504 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for 6505 * non-implemented, thermal sensors return 0x80 when 6506 * not available 6507 * The above rule is unfortunately flawed. This has been seen with 6508 * 0xC2 (power supply ID) causing thermal control problems. 6509 * The EC version can be determined by offset 0xEF and at least for 6510 * version 3 the Lenovo firmware team confirmed that registers 0xC0-0xC7 6511 * are not thermal registers. 6512 */ 6513 if (!acpi_ec_read(TP_EC_FUNCREV, &ver)) 6514 pr_warn("Thinkpad ACPI EC unable to access EC version\n"); 6515 6516 ta1 = ta2 = 0; 6517 for (i = 0; i < 8; i++) { 6518 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) { 6519 ta1 |= t; 6520 } else { 6521 ta1 = 0; 6522 break; 6523 } 6524 if (ver < 3) { 6525 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) { 6526 ta2 |= t; 6527 } else { 6528 ta1 = 0; 6529 break; 6530 } 6531 } 6532 } 6533 if (ta1 == 0) { 6534 /* This is sheer paranoia, but we handle it anyway */ 6535 if (acpi_tmp7) { 6536 pr_err("ThinkPad ACPI EC access misbehaving, falling back to ACPI TMPx access mode\n"); 6537 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07; 6538 } else { 6539 pr_err("ThinkPad ACPI EC access misbehaving, disabling thermal sensors access\n"); 6540 thermal_read_mode = TPACPI_THERMAL_NONE; 6541 } 6542 } else { 6543 if (ver >= 3) { 6544 thermal_read_mode = TPACPI_THERMAL_TPEC_8; 6545 thermal_use_labels = true; 6546 } else { 6547 thermal_read_mode = 6548 (ta2 != 0) ? 6549 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8; 6550 } 6551 } 6552 } else if (acpi_tmp7) { 6553 if (tpacpi_is_ibm() && 6554 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) { 6555 /* 600e/x, 770e, 770x */ 6556 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT; 6557 } else { 6558 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */ 6559 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07; 6560 } 6561 } else { 6562 /* temperatures not supported on 570, G4x, R30, R31, R32 */ 6563 thermal_read_mode = TPACPI_THERMAL_NONE; 6564 } 6565 6566 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n", 6567 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE), 6568 thermal_read_mode); 6569 6570 return thermal_read_mode != TPACPI_THERMAL_NONE ? 0 : -ENODEV; 6571 } 6572 6573 static int thermal_read(struct seq_file *m) 6574 { 6575 int n, i; 6576 struct ibm_thermal_sensors_struct t; 6577 6578 n = thermal_get_sensors(&t); 6579 if (unlikely(n < 0)) 6580 return n; 6581 6582 seq_printf(m, "temperatures:\t"); 6583 6584 if (n > 0) { 6585 for (i = 0; i < (n - 1); i++) 6586 seq_printf(m, "%d ", t.temp[i] / 1000); 6587 seq_printf(m, "%d\n", t.temp[i] / 1000); 6588 } else 6589 seq_printf(m, "not supported\n"); 6590 6591 return 0; 6592 } 6593 6594 static struct ibm_struct thermal_driver_data = { 6595 .name = "thermal", 6596 .read = thermal_read, 6597 }; 6598 6599 /************************************************************************* 6600 * Backlight/brightness subdriver 6601 */ 6602 6603 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen" 6604 6605 /* 6606 * ThinkPads can read brightness from two places: EC HBRV (0x31), or 6607 * CMOS NVRAM byte 0x5E, bits 0-3. 6608 * 6609 * EC HBRV (0x31) has the following layout 6610 * Bit 7: unknown function 6611 * Bit 6: unknown function 6612 * Bit 5: Z: honour scale changes, NZ: ignore scale changes 6613 * Bit 4: must be set to zero to avoid problems 6614 * Bit 3-0: backlight brightness level 6615 * 6616 * brightness_get_raw returns status data in the HBRV layout 6617 * 6618 * WARNING: The X61 has been verified to use HBRV for something else, so 6619 * this should be used _only_ on IBM ThinkPads, and maybe with some careful 6620 * testing on the very early *60 Lenovo models... 6621 */ 6622 6623 enum { 6624 TP_EC_BACKLIGHT = 0x31, 6625 6626 /* TP_EC_BACKLIGHT bitmasks */ 6627 TP_EC_BACKLIGHT_LVLMSK = 0x1F, 6628 TP_EC_BACKLIGHT_CMDMSK = 0xE0, 6629 TP_EC_BACKLIGHT_MAPSW = 0x20, 6630 }; 6631 6632 enum tpacpi_brightness_access_mode { 6633 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */ 6634 TPACPI_BRGHT_MODE_EC, /* EC control */ 6635 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */ 6636 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */ 6637 TPACPI_BRGHT_MODE_MAX 6638 }; 6639 6640 static struct backlight_device *ibm_backlight_device; 6641 6642 static enum tpacpi_brightness_access_mode brightness_mode = 6643 TPACPI_BRGHT_MODE_MAX; 6644 6645 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */ 6646 6647 static struct mutex brightness_mutex; 6648 6649 /* NVRAM brightness access, 6650 * call with brightness_mutex held! */ 6651 static unsigned int tpacpi_brightness_nvram_get(void) 6652 { 6653 u8 lnvram; 6654 6655 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS) 6656 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS) 6657 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS; 6658 lnvram &= bright_maxlvl; 6659 6660 return lnvram; 6661 } 6662 6663 static void tpacpi_brightness_checkpoint_nvram(void) 6664 { 6665 u8 lec = 0; 6666 u8 b_nvram; 6667 6668 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM) 6669 return; 6670 6671 vdbg_printk(TPACPI_DBG_BRGHT, 6672 "trying to checkpoint backlight level to NVRAM...\n"); 6673 6674 if (mutex_lock_killable(&brightness_mutex) < 0) 6675 return; 6676 6677 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec))) 6678 goto unlock; 6679 lec &= TP_EC_BACKLIGHT_LVLMSK; 6680 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS); 6681 6682 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS) 6683 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) { 6684 /* NVRAM needs update */ 6685 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS << 6686 TP_NVRAM_POS_LEVEL_BRIGHTNESS); 6687 b_nvram |= lec; 6688 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS); 6689 dbg_printk(TPACPI_DBG_BRGHT, 6690 "updated NVRAM backlight level to %u (0x%02x)\n", 6691 (unsigned int) lec, (unsigned int) b_nvram); 6692 } else 6693 vdbg_printk(TPACPI_DBG_BRGHT, 6694 "NVRAM backlight level already is %u (0x%02x)\n", 6695 (unsigned int) lec, (unsigned int) b_nvram); 6696 6697 unlock: 6698 mutex_unlock(&brightness_mutex); 6699 } 6700 6701 6702 /* call with brightness_mutex held! */ 6703 static int tpacpi_brightness_get_raw(int *status) 6704 { 6705 u8 lec = 0; 6706 6707 switch (brightness_mode) { 6708 case TPACPI_BRGHT_MODE_UCMS_STEP: 6709 *status = tpacpi_brightness_nvram_get(); 6710 return 0; 6711 case TPACPI_BRGHT_MODE_EC: 6712 case TPACPI_BRGHT_MODE_ECNVRAM: 6713 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec))) 6714 return -EIO; 6715 *status = lec; 6716 return 0; 6717 default: 6718 return -ENXIO; 6719 } 6720 } 6721 6722 /* call with brightness_mutex held! */ 6723 /* do NOT call with illegal backlight level value */ 6724 static int tpacpi_brightness_set_ec(unsigned int value) 6725 { 6726 u8 lec = 0; 6727 6728 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec))) 6729 return -EIO; 6730 6731 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT, 6732 (lec & TP_EC_BACKLIGHT_CMDMSK) | 6733 (value & TP_EC_BACKLIGHT_LVLMSK)))) 6734 return -EIO; 6735 6736 return 0; 6737 } 6738 6739 /* call with brightness_mutex held! */ 6740 static int tpacpi_brightness_set_ucmsstep(unsigned int value) 6741 { 6742 int cmos_cmd, inc; 6743 unsigned int current_value, i; 6744 6745 current_value = tpacpi_brightness_nvram_get(); 6746 6747 if (value == current_value) 6748 return 0; 6749 6750 cmos_cmd = (value > current_value) ? 6751 TP_CMOS_BRIGHTNESS_UP : 6752 TP_CMOS_BRIGHTNESS_DOWN; 6753 inc = (value > current_value) ? 1 : -1; 6754 6755 for (i = current_value; i != value; i += inc) 6756 if (issue_thinkpad_cmos_command(cmos_cmd)) 6757 return -EIO; 6758 6759 return 0; 6760 } 6761 6762 /* May return EINTR which can always be mapped to ERESTARTSYS */ 6763 static int brightness_set(unsigned int value) 6764 { 6765 int res; 6766 6767 if (value > bright_maxlvl) 6768 return -EINVAL; 6769 6770 vdbg_printk(TPACPI_DBG_BRGHT, 6771 "set backlight level to %d\n", value); 6772 6773 res = mutex_lock_killable(&brightness_mutex); 6774 if (res < 0) 6775 return res; 6776 6777 switch (brightness_mode) { 6778 case TPACPI_BRGHT_MODE_EC: 6779 case TPACPI_BRGHT_MODE_ECNVRAM: 6780 res = tpacpi_brightness_set_ec(value); 6781 break; 6782 case TPACPI_BRGHT_MODE_UCMS_STEP: 6783 res = tpacpi_brightness_set_ucmsstep(value); 6784 break; 6785 default: 6786 res = -ENXIO; 6787 } 6788 6789 mutex_unlock(&brightness_mutex); 6790 return res; 6791 } 6792 6793 /* sysfs backlight class ----------------------------------------------- */ 6794 6795 static int brightness_update_status(struct backlight_device *bd) 6796 { 6797 int level = backlight_get_brightness(bd); 6798 6799 dbg_printk(TPACPI_DBG_BRGHT, 6800 "backlight: attempt to set level to %d\n", 6801 level); 6802 6803 /* it is the backlight class's job (caller) to handle 6804 * EINTR and other errors properly */ 6805 return brightness_set(level); 6806 } 6807 6808 static int brightness_get(struct backlight_device *bd) 6809 { 6810 int status, res; 6811 6812 res = mutex_lock_killable(&brightness_mutex); 6813 if (res < 0) 6814 return 0; 6815 6816 res = tpacpi_brightness_get_raw(&status); 6817 6818 mutex_unlock(&brightness_mutex); 6819 6820 if (res < 0) 6821 return 0; 6822 6823 return status & TP_EC_BACKLIGHT_LVLMSK; 6824 } 6825 6826 static void tpacpi_brightness_notify_change(void) 6827 { 6828 backlight_force_update(ibm_backlight_device, 6829 BACKLIGHT_UPDATE_HOTKEY); 6830 } 6831 6832 static const struct backlight_ops ibm_backlight_data = { 6833 .get_brightness = brightness_get, 6834 .update_status = brightness_update_status, 6835 }; 6836 6837 /* --------------------------------------------------------------------- */ 6838 6839 static int __init tpacpi_evaluate_bcl(struct acpi_device *adev, void *not_used) 6840 { 6841 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 6842 union acpi_object *obj; 6843 acpi_status status; 6844 int rc; 6845 6846 status = acpi_evaluate_object(adev->handle, "_BCL", NULL, &buffer); 6847 if (ACPI_FAILURE(status)) 6848 return 0; 6849 6850 obj = buffer.pointer; 6851 if (!obj || obj->type != ACPI_TYPE_PACKAGE) { 6852 acpi_handle_info(adev->handle, 6853 "Unknown _BCL data, please report this to %s\n", 6854 TPACPI_MAIL); 6855 rc = 0; 6856 } else { 6857 rc = obj->package.count; 6858 } 6859 kfree(obj); 6860 6861 return rc; 6862 } 6863 6864 /* 6865 * Call _BCL method of video device. On some ThinkPads this will 6866 * switch the firmware to the ACPI brightness control mode. 6867 */ 6868 6869 static int __init tpacpi_query_bcl_levels(acpi_handle handle) 6870 { 6871 struct acpi_device *device; 6872 6873 device = acpi_fetch_acpi_dev(handle); 6874 if (!device) 6875 return 0; 6876 6877 return acpi_dev_for_each_child(device, tpacpi_evaluate_bcl, NULL); 6878 } 6879 6880 6881 /* 6882 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map 6883 */ 6884 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void) 6885 { 6886 acpi_handle video_device; 6887 int bcl_levels = 0; 6888 6889 tpacpi_acpi_handle_locate("video", NULL, &video_device); 6890 if (video_device) 6891 bcl_levels = tpacpi_query_bcl_levels(video_device); 6892 6893 tp_features.bright_acpimode = (bcl_levels > 0); 6894 6895 return (bcl_levels > 2) ? (bcl_levels - 2) : 0; 6896 } 6897 6898 /* 6899 * These are only useful for models that have only one possibility 6900 * of GPU. If the BIOS model handles both ATI and Intel, don't use 6901 * these quirks. 6902 */ 6903 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */ 6904 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */ 6905 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */ 6906 6907 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = { 6908 /* Models with ATI GPUs known to require ECNVRAM mode */ 6909 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */ 6910 6911 /* Models with ATI GPUs that can use ECNVRAM */ 6912 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */ 6913 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC), 6914 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */ 6915 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC), 6916 6917 /* Models with Intel Extreme Graphics 2 */ 6918 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */ 6919 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC), 6920 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC), 6921 6922 /* Models with Intel GMA900 */ 6923 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */ 6924 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */ 6925 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */ 6926 }; 6927 6928 /* 6929 * Returns < 0 for error, otherwise sets tp_features.bright_* 6930 * and bright_maxlvl. 6931 */ 6932 static void __init tpacpi_detect_brightness_capabilities(void) 6933 { 6934 unsigned int b; 6935 6936 vdbg_printk(TPACPI_DBG_INIT, 6937 "detecting firmware brightness interface capabilities\n"); 6938 6939 /* we could run a quirks check here (same table used by 6940 * brightness_init) if needed */ 6941 6942 /* 6943 * We always attempt to detect acpi support, so as to switch 6944 * Lenovo Vista BIOS to ACPI brightness mode even if we are not 6945 * going to publish a backlight interface 6946 */ 6947 b = tpacpi_check_std_acpi_brightness_support(); 6948 switch (b) { 6949 case 16: 6950 bright_maxlvl = 15; 6951 break; 6952 case 8: 6953 case 0: 6954 bright_maxlvl = 7; 6955 break; 6956 default: 6957 tp_features.bright_unkfw = 1; 6958 bright_maxlvl = b - 1; 6959 } 6960 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1); 6961 } 6962 6963 static int __init brightness_init(struct ibm_init_struct *iibm) 6964 { 6965 struct backlight_properties props; 6966 int b; 6967 unsigned long quirks; 6968 6969 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n"); 6970 6971 mutex_init(&brightness_mutex); 6972 6973 quirks = tpacpi_check_quirks(brightness_quirk_table, 6974 ARRAY_SIZE(brightness_quirk_table)); 6975 6976 /* tpacpi_detect_brightness_capabilities() must have run already */ 6977 6978 /* if it is unknown, we don't handle it: it wouldn't be safe */ 6979 if (tp_features.bright_unkfw) 6980 return -ENODEV; 6981 6982 if (!brightness_enable) { 6983 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT, 6984 "brightness support disabled by module parameter\n"); 6985 return -ENODEV; 6986 } 6987 6988 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) { 6989 if (brightness_enable > 1) { 6990 pr_info("Standard ACPI backlight interface available, not loading native one\n"); 6991 return -ENODEV; 6992 } else if (brightness_enable == 1) { 6993 pr_warn("Cannot enable backlight brightness support, ACPI is already handling it. Refer to the acpi_backlight kernel parameter.\n"); 6994 return -ENODEV; 6995 } 6996 } else if (!tp_features.bright_acpimode) { 6997 pr_notice("ACPI backlight interface not available\n"); 6998 return -ENODEV; 6999 } 7000 7001 pr_notice("ACPI native brightness control enabled\n"); 7002 7003 /* 7004 * Check for module parameter bogosity, note that we 7005 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be 7006 * able to detect "unspecified" 7007 */ 7008 if (brightness_mode > TPACPI_BRGHT_MODE_MAX) 7009 return -EINVAL; 7010 7011 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */ 7012 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO || 7013 brightness_mode == TPACPI_BRGHT_MODE_MAX) { 7014 if (quirks & TPACPI_BRGHT_Q_EC) 7015 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM; 7016 else 7017 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP; 7018 7019 dbg_printk(TPACPI_DBG_BRGHT, 7020 "driver auto-selected brightness_mode=%d\n", 7021 brightness_mode); 7022 } 7023 7024 /* Safety */ 7025 if (!tpacpi_is_ibm() && 7026 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM || 7027 brightness_mode == TPACPI_BRGHT_MODE_EC)) 7028 return -EINVAL; 7029 7030 if (tpacpi_brightness_get_raw(&b) < 0) 7031 return -ENODEV; 7032 7033 memset(&props, 0, sizeof(struct backlight_properties)); 7034 props.type = BACKLIGHT_PLATFORM; 7035 props.max_brightness = bright_maxlvl; 7036 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK; 7037 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME, 7038 NULL, NULL, 7039 &ibm_backlight_data, 7040 &props); 7041 if (IS_ERR(ibm_backlight_device)) { 7042 int rc = PTR_ERR(ibm_backlight_device); 7043 ibm_backlight_device = NULL; 7044 pr_err("Could not register backlight device\n"); 7045 return rc; 7046 } 7047 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT, 7048 "brightness is supported\n"); 7049 7050 if (quirks & TPACPI_BRGHT_Q_ASK) { 7051 pr_notice("brightness: will use unverified default: brightness_mode=%d\n", 7052 brightness_mode); 7053 pr_notice("brightness: please report to %s whether it works well or not on your ThinkPad\n", 7054 TPACPI_MAIL); 7055 } 7056 7057 /* Added by mistake in early 2007. Probably useless, but it could 7058 * be working around some unknown firmware problem where the value 7059 * read at startup doesn't match the real hardware state... so leave 7060 * it in place just in case */ 7061 backlight_update_status(ibm_backlight_device); 7062 7063 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT, 7064 "brightness: registering brightness hotkeys as change notification\n"); 7065 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask 7066 | TP_ACPI_HKEY_BRGHTUP_MASK 7067 | TP_ACPI_HKEY_BRGHTDWN_MASK); 7068 return 0; 7069 } 7070 7071 static void brightness_suspend(void) 7072 { 7073 tpacpi_brightness_checkpoint_nvram(); 7074 } 7075 7076 static void brightness_shutdown(void) 7077 { 7078 tpacpi_brightness_checkpoint_nvram(); 7079 } 7080 7081 static void brightness_exit(void) 7082 { 7083 if (ibm_backlight_device) { 7084 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT, 7085 "calling backlight_device_unregister()\n"); 7086 backlight_device_unregister(ibm_backlight_device); 7087 } 7088 7089 tpacpi_brightness_checkpoint_nvram(); 7090 } 7091 7092 static int brightness_read(struct seq_file *m) 7093 { 7094 int level; 7095 7096 level = brightness_get(NULL); 7097 if (level < 0) { 7098 seq_printf(m, "level:\t\tunreadable\n"); 7099 } else { 7100 seq_printf(m, "level:\t\t%d\n", level); 7101 seq_printf(m, "commands:\tup, down\n"); 7102 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n", 7103 bright_maxlvl); 7104 } 7105 7106 return 0; 7107 } 7108 7109 static int brightness_write(char *buf) 7110 { 7111 int level; 7112 int rc; 7113 char *cmd; 7114 7115 level = brightness_get(NULL); 7116 if (level < 0) 7117 return level; 7118 7119 while ((cmd = strsep(&buf, ","))) { 7120 if (strlencmp(cmd, "up") == 0) { 7121 if (level < bright_maxlvl) 7122 level++; 7123 } else if (strlencmp(cmd, "down") == 0) { 7124 if (level > 0) 7125 level--; 7126 } else if (sscanf(cmd, "level %d", &level) == 1 && 7127 level >= 0 && level <= bright_maxlvl) { 7128 /* new level set */ 7129 } else 7130 return -EINVAL; 7131 } 7132 7133 tpacpi_disclose_usertask("procfs brightness", 7134 "set level to %d\n", level); 7135 7136 /* 7137 * Now we know what the final level should be, so we try to set it. 7138 * Doing it this way makes the syscall restartable in case of EINTR 7139 */ 7140 rc = brightness_set(level); 7141 if (!rc && ibm_backlight_device) 7142 backlight_force_update(ibm_backlight_device, 7143 BACKLIGHT_UPDATE_SYSFS); 7144 return (rc == -EINTR) ? -ERESTARTSYS : rc; 7145 } 7146 7147 static struct ibm_struct brightness_driver_data = { 7148 .name = "brightness", 7149 .read = brightness_read, 7150 .write = brightness_write, 7151 .exit = brightness_exit, 7152 .suspend = brightness_suspend, 7153 .shutdown = brightness_shutdown, 7154 }; 7155 7156 /************************************************************************* 7157 * Volume subdriver 7158 */ 7159 7160 /* 7161 * IBM ThinkPads have a simple volume controller with MUTE gating. 7162 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec. 7163 * 7164 * Since the *61 series (and probably also the later *60 series), Lenovo 7165 * ThinkPads only implement the MUTE gate. 7166 * 7167 * EC register 0x30 7168 * Bit 6: MUTE (1 mutes sound) 7169 * Bit 3-0: Volume 7170 * Other bits should be zero as far as we know. 7171 * 7172 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and 7173 * bits 3-0 (volume). Other bits in NVRAM may have other functions, 7174 * such as bit 7 which is used to detect repeated presses of MUTE, 7175 * and we leave them unchanged. 7176 * 7177 * On newer Lenovo ThinkPads, the EC can automatically change the volume 7178 * in response to user input. Unfortunately, this rarely works well. 7179 * The laptop changes the state of its internal MUTE gate and, on some 7180 * models, sends KEY_MUTE, causing any user code that responds to the 7181 * mute button to get confused. The hardware MUTE gate is also 7182 * unnecessary, since user code can handle the mute button without 7183 * kernel or EC help. 7184 * 7185 * To avoid confusing userspace, we simply disable all EC-based mute 7186 * and volume controls when possible. 7187 */ 7188 7189 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT 7190 7191 #define TPACPI_ALSA_DRVNAME "ThinkPad EC" 7192 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control" 7193 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME 7194 7195 #if SNDRV_CARDS <= 32 7196 #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1) 7197 #else 7198 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1) 7199 #endif 7200 static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */ 7201 static char *alsa_id = "ThinkPadEC"; 7202 static bool alsa_enable = SNDRV_DEFAULT_ENABLE1; 7203 7204 struct tpacpi_alsa_data { 7205 struct snd_card *card; 7206 struct snd_ctl_elem_id *ctl_mute_id; 7207 struct snd_ctl_elem_id *ctl_vol_id; 7208 }; 7209 7210 static struct snd_card *alsa_card; 7211 7212 enum { 7213 TP_EC_AUDIO = 0x30, 7214 7215 /* TP_EC_AUDIO bits */ 7216 TP_EC_AUDIO_MUTESW = 6, 7217 7218 /* TP_EC_AUDIO bitmasks */ 7219 TP_EC_AUDIO_LVL_MSK = 0x0F, 7220 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW), 7221 7222 /* Maximum volume */ 7223 TP_EC_VOLUME_MAX = 14, 7224 }; 7225 7226 enum tpacpi_volume_access_mode { 7227 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */ 7228 TPACPI_VOL_MODE_EC, /* Pure EC control */ 7229 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */ 7230 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */ 7231 TPACPI_VOL_MODE_MAX 7232 }; 7233 7234 enum tpacpi_volume_capabilities { 7235 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */ 7236 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */ 7237 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */ 7238 TPACPI_VOL_CAP_MAX 7239 }; 7240 7241 enum tpacpi_mute_btn_mode { 7242 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */ 7243 /* We don't know what mode 1 is. */ 7244 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */ 7245 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */ 7246 }; 7247 7248 static enum tpacpi_volume_access_mode volume_mode = 7249 TPACPI_VOL_MODE_MAX; 7250 7251 static enum tpacpi_volume_capabilities volume_capabilities; 7252 static bool volume_control_allowed; 7253 static bool software_mute_requested = true; 7254 static bool software_mute_active; 7255 static int software_mute_orig_mode; 7256 7257 /* 7258 * Used to syncronize writers to TP_EC_AUDIO and 7259 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write 7260 */ 7261 static struct mutex volume_mutex; 7262 7263 static void tpacpi_volume_checkpoint_nvram(void) 7264 { 7265 u8 lec = 0; 7266 u8 b_nvram; 7267 u8 ec_mask; 7268 7269 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM) 7270 return; 7271 if (!volume_control_allowed) 7272 return; 7273 if (software_mute_active) 7274 return; 7275 7276 vdbg_printk(TPACPI_DBG_MIXER, 7277 "trying to checkpoint mixer state to NVRAM...\n"); 7278 7279 if (tp_features.mixer_no_level_control) 7280 ec_mask = TP_EC_AUDIO_MUTESW_MSK; 7281 else 7282 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK; 7283 7284 if (mutex_lock_killable(&volume_mutex) < 0) 7285 return; 7286 7287 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec))) 7288 goto unlock; 7289 lec &= ec_mask; 7290 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER); 7291 7292 if (lec != (b_nvram & ec_mask)) { 7293 /* NVRAM needs update */ 7294 b_nvram &= ~ec_mask; 7295 b_nvram |= lec; 7296 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER); 7297 dbg_printk(TPACPI_DBG_MIXER, 7298 "updated NVRAM mixer status to 0x%02x (0x%02x)\n", 7299 (unsigned int) lec, (unsigned int) b_nvram); 7300 } else { 7301 vdbg_printk(TPACPI_DBG_MIXER, 7302 "NVRAM mixer status already is 0x%02x (0x%02x)\n", 7303 (unsigned int) lec, (unsigned int) b_nvram); 7304 } 7305 7306 unlock: 7307 mutex_unlock(&volume_mutex); 7308 } 7309 7310 static int volume_get_status_ec(u8 *status) 7311 { 7312 u8 s; 7313 7314 if (!acpi_ec_read(TP_EC_AUDIO, &s)) 7315 return -EIO; 7316 7317 *status = s; 7318 7319 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s); 7320 7321 return 0; 7322 } 7323 7324 static int volume_get_status(u8 *status) 7325 { 7326 return volume_get_status_ec(status); 7327 } 7328 7329 static int volume_set_status_ec(const u8 status) 7330 { 7331 if (!acpi_ec_write(TP_EC_AUDIO, status)) 7332 return -EIO; 7333 7334 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status); 7335 7336 /* 7337 * On X200s, and possibly on others, it can take a while for 7338 * reads to become correct. 7339 */ 7340 msleep(1); 7341 7342 return 0; 7343 } 7344 7345 static int volume_set_status(const u8 status) 7346 { 7347 return volume_set_status_ec(status); 7348 } 7349 7350 /* returns < 0 on error, 0 on no change, 1 on change */ 7351 static int __volume_set_mute_ec(const bool mute) 7352 { 7353 int rc; 7354 u8 s, n; 7355 7356 if (mutex_lock_killable(&volume_mutex) < 0) 7357 return -EINTR; 7358 7359 rc = volume_get_status_ec(&s); 7360 if (rc) 7361 goto unlock; 7362 7363 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK : 7364 s & ~TP_EC_AUDIO_MUTESW_MSK; 7365 7366 if (n != s) { 7367 rc = volume_set_status_ec(n); 7368 if (!rc) 7369 rc = 1; 7370 } 7371 7372 unlock: 7373 mutex_unlock(&volume_mutex); 7374 return rc; 7375 } 7376 7377 static int volume_alsa_set_mute(const bool mute) 7378 { 7379 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n", 7380 (mute) ? "" : "un"); 7381 return __volume_set_mute_ec(mute); 7382 } 7383 7384 static int volume_set_mute(const bool mute) 7385 { 7386 int rc; 7387 7388 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n", 7389 (mute) ? "" : "un"); 7390 7391 rc = __volume_set_mute_ec(mute); 7392 return (rc < 0) ? rc : 0; 7393 } 7394 7395 /* returns < 0 on error, 0 on no change, 1 on change */ 7396 static int __volume_set_volume_ec(const u8 vol) 7397 { 7398 int rc; 7399 u8 s, n; 7400 7401 if (vol > TP_EC_VOLUME_MAX) 7402 return -EINVAL; 7403 7404 if (mutex_lock_killable(&volume_mutex) < 0) 7405 return -EINTR; 7406 7407 rc = volume_get_status_ec(&s); 7408 if (rc) 7409 goto unlock; 7410 7411 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol; 7412 7413 if (n != s) { 7414 rc = volume_set_status_ec(n); 7415 if (!rc) 7416 rc = 1; 7417 } 7418 7419 unlock: 7420 mutex_unlock(&volume_mutex); 7421 return rc; 7422 } 7423 7424 static int volume_set_software_mute(bool startup) 7425 { 7426 int result; 7427 7428 if (!tpacpi_is_lenovo()) 7429 return -ENODEV; 7430 7431 if (startup) { 7432 if (!acpi_evalf(ec_handle, &software_mute_orig_mode, 7433 "HAUM", "qd")) 7434 return -EIO; 7435 7436 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER, 7437 "Initial HAUM setting was %d\n", 7438 software_mute_orig_mode); 7439 } 7440 7441 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd", 7442 (int)TP_EC_MUTE_BTN_NONE)) 7443 return -EIO; 7444 7445 if (result != TP_EC_MUTE_BTN_NONE) 7446 pr_warn("Unexpected SAUM result %d\n", 7447 result); 7448 7449 /* 7450 * In software mute mode, the standard codec controls take 7451 * precendence, so we unmute the ThinkPad HW switch at 7452 * startup. Just on case there are SAUM-capable ThinkPads 7453 * with level controls, set max HW volume as well. 7454 */ 7455 if (tp_features.mixer_no_level_control) 7456 result = volume_set_mute(false); 7457 else 7458 result = volume_set_status(TP_EC_VOLUME_MAX); 7459 7460 if (result != 0) 7461 pr_warn("Failed to unmute the HW mute switch\n"); 7462 7463 return 0; 7464 } 7465 7466 static void volume_exit_software_mute(void) 7467 { 7468 int r; 7469 7470 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode) 7471 || r != software_mute_orig_mode) 7472 pr_warn("Failed to restore mute mode\n"); 7473 } 7474 7475 static int volume_alsa_set_volume(const u8 vol) 7476 { 7477 dbg_printk(TPACPI_DBG_MIXER, 7478 "ALSA: trying to set volume level to %hu\n", vol); 7479 return __volume_set_volume_ec(vol); 7480 } 7481 7482 static void volume_alsa_notify_change(void) 7483 { 7484 struct tpacpi_alsa_data *d; 7485 7486 if (alsa_card && alsa_card->private_data) { 7487 d = alsa_card->private_data; 7488 if (d->ctl_mute_id) 7489 snd_ctl_notify(alsa_card, 7490 SNDRV_CTL_EVENT_MASK_VALUE, 7491 d->ctl_mute_id); 7492 if (d->ctl_vol_id) 7493 snd_ctl_notify(alsa_card, 7494 SNDRV_CTL_EVENT_MASK_VALUE, 7495 d->ctl_vol_id); 7496 } 7497 } 7498 7499 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol, 7500 struct snd_ctl_elem_info *uinfo) 7501 { 7502 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 7503 uinfo->count = 1; 7504 uinfo->value.integer.min = 0; 7505 uinfo->value.integer.max = TP_EC_VOLUME_MAX; 7506 return 0; 7507 } 7508 7509 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol, 7510 struct snd_ctl_elem_value *ucontrol) 7511 { 7512 u8 s; 7513 int rc; 7514 7515 rc = volume_get_status(&s); 7516 if (rc < 0) 7517 return rc; 7518 7519 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK; 7520 return 0; 7521 } 7522 7523 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol, 7524 struct snd_ctl_elem_value *ucontrol) 7525 { 7526 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n", 7527 ucontrol->value.integer.value[0]); 7528 return volume_alsa_set_volume(ucontrol->value.integer.value[0]); 7529 } 7530 7531 #define volume_alsa_mute_info snd_ctl_boolean_mono_info 7532 7533 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol, 7534 struct snd_ctl_elem_value *ucontrol) 7535 { 7536 u8 s; 7537 int rc; 7538 7539 rc = volume_get_status(&s); 7540 if (rc < 0) 7541 return rc; 7542 7543 ucontrol->value.integer.value[0] = 7544 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1; 7545 return 0; 7546 } 7547 7548 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol, 7549 struct snd_ctl_elem_value *ucontrol) 7550 { 7551 tpacpi_disclose_usertask("ALSA", "%smute\n", 7552 ucontrol->value.integer.value[0] ? 7553 "un" : ""); 7554 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]); 7555 } 7556 7557 static struct snd_kcontrol_new volume_alsa_control_vol __initdata = { 7558 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 7559 .name = "Console Playback Volume", 7560 .index = 0, 7561 .access = SNDRV_CTL_ELEM_ACCESS_READ, 7562 .info = volume_alsa_vol_info, 7563 .get = volume_alsa_vol_get, 7564 }; 7565 7566 static struct snd_kcontrol_new volume_alsa_control_mute __initdata = { 7567 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 7568 .name = "Console Playback Switch", 7569 .index = 0, 7570 .access = SNDRV_CTL_ELEM_ACCESS_READ, 7571 .info = volume_alsa_mute_info, 7572 .get = volume_alsa_mute_get, 7573 }; 7574 7575 static void volume_suspend(void) 7576 { 7577 tpacpi_volume_checkpoint_nvram(); 7578 } 7579 7580 static void volume_resume(void) 7581 { 7582 if (software_mute_active) { 7583 if (volume_set_software_mute(false) < 0) 7584 pr_warn("Failed to restore software mute\n"); 7585 } else { 7586 volume_alsa_notify_change(); 7587 } 7588 } 7589 7590 static void volume_shutdown(void) 7591 { 7592 tpacpi_volume_checkpoint_nvram(); 7593 } 7594 7595 static void volume_exit(void) 7596 { 7597 if (alsa_card) { 7598 snd_card_free(alsa_card); 7599 alsa_card = NULL; 7600 } 7601 7602 tpacpi_volume_checkpoint_nvram(); 7603 7604 if (software_mute_active) 7605 volume_exit_software_mute(); 7606 } 7607 7608 static int __init volume_create_alsa_mixer(void) 7609 { 7610 struct snd_card *card; 7611 struct tpacpi_alsa_data *data; 7612 struct snd_kcontrol *ctl_vol; 7613 struct snd_kcontrol *ctl_mute; 7614 int rc; 7615 7616 rc = snd_card_new(&tpacpi_pdev->dev, 7617 alsa_index, alsa_id, THIS_MODULE, 7618 sizeof(struct tpacpi_alsa_data), &card); 7619 if (rc < 0 || !card) { 7620 pr_err("Failed to create ALSA card structures: %d\n", rc); 7621 return -ENODEV; 7622 } 7623 7624 BUG_ON(!card->private_data); 7625 data = card->private_data; 7626 data->card = card; 7627 7628 strscpy(card->driver, TPACPI_ALSA_DRVNAME, 7629 sizeof(card->driver)); 7630 strscpy(card->shortname, TPACPI_ALSA_SHRTNAME, 7631 sizeof(card->shortname)); 7632 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s", 7633 (thinkpad_id.ec_version_str) ? 7634 thinkpad_id.ec_version_str : "(unknown)"); 7635 snprintf(card->longname, sizeof(card->longname), 7636 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO, 7637 (thinkpad_id.ec_version_str) ? 7638 thinkpad_id.ec_version_str : "unknown"); 7639 7640 if (volume_control_allowed) { 7641 volume_alsa_control_vol.put = volume_alsa_vol_put; 7642 volume_alsa_control_vol.access = 7643 SNDRV_CTL_ELEM_ACCESS_READWRITE; 7644 7645 volume_alsa_control_mute.put = volume_alsa_mute_put; 7646 volume_alsa_control_mute.access = 7647 SNDRV_CTL_ELEM_ACCESS_READWRITE; 7648 } 7649 7650 if (!tp_features.mixer_no_level_control) { 7651 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL); 7652 rc = snd_ctl_add(card, ctl_vol); 7653 if (rc < 0) { 7654 pr_err("Failed to create ALSA volume control: %d\n", 7655 rc); 7656 goto err_exit; 7657 } 7658 data->ctl_vol_id = &ctl_vol->id; 7659 } 7660 7661 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL); 7662 rc = snd_ctl_add(card, ctl_mute); 7663 if (rc < 0) { 7664 pr_err("Failed to create ALSA mute control: %d\n", rc); 7665 goto err_exit; 7666 } 7667 data->ctl_mute_id = &ctl_mute->id; 7668 7669 rc = snd_card_register(card); 7670 if (rc < 0) { 7671 pr_err("Failed to register ALSA card: %d\n", rc); 7672 goto err_exit; 7673 } 7674 7675 alsa_card = card; 7676 return 0; 7677 7678 err_exit: 7679 snd_card_free(card); 7680 return -ENODEV; 7681 } 7682 7683 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */ 7684 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */ 7685 7686 static const struct tpacpi_quirk volume_quirk_table[] __initconst = { 7687 /* Whitelist volume level on all IBM by default */ 7688 { .vendor = PCI_VENDOR_ID_IBM, 7689 .bios = TPACPI_MATCH_ANY, 7690 .ec = TPACPI_MATCH_ANY, 7691 .quirks = TPACPI_VOL_Q_LEVEL }, 7692 7693 /* Lenovo models with volume control (needs confirmation) */ 7694 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */ 7695 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */ 7696 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */ 7697 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */ 7698 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */ 7699 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */ 7700 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */ 7701 7702 /* Whitelist mute-only on all Lenovo by default */ 7703 { .vendor = PCI_VENDOR_ID_LENOVO, 7704 .bios = TPACPI_MATCH_ANY, 7705 .ec = TPACPI_MATCH_ANY, 7706 .quirks = TPACPI_VOL_Q_MUTEONLY } 7707 }; 7708 7709 static int __init volume_init(struct ibm_init_struct *iibm) 7710 { 7711 unsigned long quirks; 7712 int rc; 7713 7714 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n"); 7715 7716 mutex_init(&volume_mutex); 7717 7718 /* 7719 * Check for module parameter bogosity, note that we 7720 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be 7721 * able to detect "unspecified" 7722 */ 7723 if (volume_mode > TPACPI_VOL_MODE_MAX) 7724 return -EINVAL; 7725 7726 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) { 7727 pr_err("UCMS step volume mode not implemented, please contact %s\n", 7728 TPACPI_MAIL); 7729 return -ENODEV; 7730 } 7731 7732 if (volume_capabilities >= TPACPI_VOL_CAP_MAX) 7733 return -EINVAL; 7734 7735 /* 7736 * The ALSA mixer is our primary interface. 7737 * When disabled, don't install the subdriver at all 7738 */ 7739 if (!alsa_enable) { 7740 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER, 7741 "ALSA mixer disabled by parameter, not loading volume subdriver...\n"); 7742 return -ENODEV; 7743 } 7744 7745 quirks = tpacpi_check_quirks(volume_quirk_table, 7746 ARRAY_SIZE(volume_quirk_table)); 7747 7748 switch (volume_capabilities) { 7749 case TPACPI_VOL_CAP_AUTO: 7750 if (quirks & TPACPI_VOL_Q_MUTEONLY) 7751 tp_features.mixer_no_level_control = 1; 7752 else if (quirks & TPACPI_VOL_Q_LEVEL) 7753 tp_features.mixer_no_level_control = 0; 7754 else 7755 return -ENODEV; /* no mixer */ 7756 break; 7757 case TPACPI_VOL_CAP_VOLMUTE: 7758 tp_features.mixer_no_level_control = 0; 7759 break; 7760 case TPACPI_VOL_CAP_MUTEONLY: 7761 tp_features.mixer_no_level_control = 1; 7762 break; 7763 default: 7764 return -ENODEV; 7765 } 7766 7767 if (volume_capabilities != TPACPI_VOL_CAP_AUTO) 7768 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER, 7769 "using user-supplied volume_capabilities=%d\n", 7770 volume_capabilities); 7771 7772 if (volume_mode == TPACPI_VOL_MODE_AUTO || 7773 volume_mode == TPACPI_VOL_MODE_MAX) { 7774 volume_mode = TPACPI_VOL_MODE_ECNVRAM; 7775 7776 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER, 7777 "driver auto-selected volume_mode=%d\n", 7778 volume_mode); 7779 } else { 7780 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER, 7781 "using user-supplied volume_mode=%d\n", 7782 volume_mode); 7783 } 7784 7785 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER, 7786 "mute is supported, volume control is %s\n", 7787 str_supported(!tp_features.mixer_no_level_control)); 7788 7789 if (software_mute_requested && volume_set_software_mute(true) == 0) { 7790 software_mute_active = true; 7791 } else { 7792 rc = volume_create_alsa_mixer(); 7793 if (rc) { 7794 pr_err("Could not create the ALSA mixer interface\n"); 7795 return rc; 7796 } 7797 7798 pr_info("Console audio control enabled, mode: %s\n", 7799 (volume_control_allowed) ? 7800 "override (read/write)" : 7801 "monitor (read only)"); 7802 } 7803 7804 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER, 7805 "registering volume hotkeys as change notification\n"); 7806 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask 7807 | TP_ACPI_HKEY_VOLUP_MASK 7808 | TP_ACPI_HKEY_VOLDWN_MASK 7809 | TP_ACPI_HKEY_MUTE_MASK); 7810 7811 return 0; 7812 } 7813 7814 static int volume_read(struct seq_file *m) 7815 { 7816 u8 status; 7817 7818 if (volume_get_status(&status) < 0) { 7819 seq_printf(m, "level:\t\tunreadable\n"); 7820 } else { 7821 if (tp_features.mixer_no_level_control) 7822 seq_printf(m, "level:\t\tunsupported\n"); 7823 else 7824 seq_printf(m, "level:\t\t%d\n", 7825 status & TP_EC_AUDIO_LVL_MSK); 7826 7827 seq_printf(m, "mute:\t\t%s\n", str_on_off(status & BIT(TP_EC_AUDIO_MUTESW))); 7828 7829 if (volume_control_allowed) { 7830 seq_printf(m, "commands:\tunmute, mute\n"); 7831 if (!tp_features.mixer_no_level_control) { 7832 seq_printf(m, "commands:\tup, down\n"); 7833 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n", 7834 TP_EC_VOLUME_MAX); 7835 } 7836 } 7837 } 7838 7839 return 0; 7840 } 7841 7842 static int volume_write(char *buf) 7843 { 7844 u8 s; 7845 u8 new_level, new_mute; 7846 int l; 7847 char *cmd; 7848 int rc; 7849 7850 /* 7851 * We do allow volume control at driver startup, so that the 7852 * user can set initial state through the volume=... parameter hack. 7853 */ 7854 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) { 7855 if (unlikely(!tp_warned.volume_ctrl_forbidden)) { 7856 tp_warned.volume_ctrl_forbidden = 1; 7857 pr_notice("Console audio control in monitor mode, changes are not allowed\n"); 7858 pr_notice("Use the volume_control=1 module parameter to enable volume control\n"); 7859 } 7860 return -EPERM; 7861 } 7862 7863 rc = volume_get_status(&s); 7864 if (rc < 0) 7865 return rc; 7866 7867 new_level = s & TP_EC_AUDIO_LVL_MSK; 7868 new_mute = s & TP_EC_AUDIO_MUTESW_MSK; 7869 7870 while ((cmd = strsep(&buf, ","))) { 7871 if (!tp_features.mixer_no_level_control) { 7872 if (strlencmp(cmd, "up") == 0) { 7873 if (new_mute) 7874 new_mute = 0; 7875 else if (new_level < TP_EC_VOLUME_MAX) 7876 new_level++; 7877 continue; 7878 } else if (strlencmp(cmd, "down") == 0) { 7879 if (new_mute) 7880 new_mute = 0; 7881 else if (new_level > 0) 7882 new_level--; 7883 continue; 7884 } else if (sscanf(cmd, "level %u", &l) == 1 && 7885 l >= 0 && l <= TP_EC_VOLUME_MAX) { 7886 new_level = l; 7887 continue; 7888 } 7889 } 7890 if (strlencmp(cmd, "mute") == 0) 7891 new_mute = TP_EC_AUDIO_MUTESW_MSK; 7892 else if (strlencmp(cmd, "unmute") == 0) 7893 new_mute = 0; 7894 else 7895 return -EINVAL; 7896 } 7897 7898 if (tp_features.mixer_no_level_control) { 7899 tpacpi_disclose_usertask("procfs volume", "%smute\n", 7900 new_mute ? "" : "un"); 7901 rc = volume_set_mute(!!new_mute); 7902 } else { 7903 tpacpi_disclose_usertask("procfs volume", 7904 "%smute and set level to %d\n", 7905 new_mute ? "" : "un", new_level); 7906 rc = volume_set_status(new_mute | new_level); 7907 } 7908 volume_alsa_notify_change(); 7909 7910 return (rc == -EINTR) ? -ERESTARTSYS : rc; 7911 } 7912 7913 static struct ibm_struct volume_driver_data = { 7914 .name = "volume", 7915 .read = volume_read, 7916 .write = volume_write, 7917 .exit = volume_exit, 7918 .suspend = volume_suspend, 7919 .resume = volume_resume, 7920 .shutdown = volume_shutdown, 7921 }; 7922 7923 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */ 7924 7925 #define alsa_card NULL 7926 7927 static inline void volume_alsa_notify_change(void) 7928 { 7929 } 7930 7931 static int __init volume_init(struct ibm_init_struct *iibm) 7932 { 7933 pr_info("volume: disabled as there is no ALSA support in this kernel\n"); 7934 7935 return -ENODEV; 7936 } 7937 7938 static struct ibm_struct volume_driver_data = { 7939 .name = "volume", 7940 }; 7941 7942 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */ 7943 7944 /************************************************************************* 7945 * Fan subdriver 7946 */ 7947 7948 /* 7949 * FAN ACCESS MODES 7950 * 7951 * TPACPI_FAN_RD_ACPI_GFAN: 7952 * ACPI GFAN method: returns fan level 7953 * 7954 * see TPACPI_FAN_WR_ACPI_SFAN 7955 * EC 0x2f (HFSP) not available if GFAN exists 7956 * 7957 * TPACPI_FAN_WR_ACPI_SFAN: 7958 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max) 7959 * 7960 * EC 0x2f (HFSP) might be available *for reading*, but do not use 7961 * it for writing. 7962 * 7963 * TPACPI_FAN_WR_TPEC: 7964 * ThinkPad EC register 0x2f (HFSP): fan control loop mode 7965 * Supported on almost all ThinkPads 7966 * 7967 * Fan speed changes of any sort (including those caused by the 7968 * disengaged mode) are usually done slowly by the firmware as the 7969 * maximum amount of fan duty cycle change per second seems to be 7970 * limited. 7971 * 7972 * Reading is not available if GFAN exists. 7973 * Writing is not available if SFAN exists. 7974 * 7975 * Bits 7976 * 7 automatic mode engaged; 7977 * (default operation mode of the ThinkPad) 7978 * fan level is ignored in this mode. 7979 * 6 full speed mode (takes precedence over bit 7); 7980 * not available on all thinkpads. May disable 7981 * the tachometer while the fan controller ramps up 7982 * the speed (which can take up to a few *minutes*). 7983 * Speeds up fan to 100% duty-cycle, which is far above 7984 * the standard RPM levels. It is not impossible that 7985 * it could cause hardware damage. 7986 * 5-3 unused in some models. Extra bits for fan level 7987 * in others, but still useless as all values above 7988 * 7 map to the same speed as level 7 in these models. 7989 * 2-0 fan level (0..7 usually) 7990 * 0x00 = stop 7991 * 0x07 = max (set when temperatures critical) 7992 * Some ThinkPads may have other levels, see 7993 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41) 7994 * 7995 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at 7996 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT 7997 * does so, its initial value is meaningless (0x07). 7998 * 7999 * For firmware bugs, refer to: 8000 * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues 8001 * 8002 * ---- 8003 * 8004 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB): 8005 * Main fan tachometer reading (in RPM) 8006 * 8007 * This register is present on all ThinkPads with a new-style EC, and 8008 * it is known not to be present on the A21m/e, and T22, as there is 8009 * something else in offset 0x84 according to the ACPI DSDT. Other 8010 * ThinkPads from this same time period (and earlier) probably lack the 8011 * tachometer as well. 8012 * 8013 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware 8014 * was never fixed by IBM to report the EC firmware version string 8015 * probably support the tachometer (like the early X models), so 8016 * detecting it is quite hard. We need more data to know for sure. 8017 * 8018 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings 8019 * might result. 8020 * 8021 * FIRMWARE BUG: may go stale while the EC is switching to full speed 8022 * mode. 8023 * 8024 * For firmware bugs, refer to: 8025 * https://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues 8026 * 8027 * ---- 8028 * 8029 * ThinkPad EC register 0x31 bit 0 (only on select models) 8030 * 8031 * When bit 0 of EC register 0x31 is zero, the tachometer registers 8032 * show the speed of the main fan. When bit 0 of EC register 0x31 8033 * is one, the tachometer registers show the speed of the auxiliary 8034 * fan. 8035 * 8036 * Fan control seems to affect both fans, regardless of the state 8037 * of this bit. 8038 * 8039 * So far, only the firmware for the X60/X61 non-tablet versions 8040 * seem to support this (firmware TP-7M). 8041 * 8042 * TPACPI_FAN_WR_ACPI_FANS: 8043 * ThinkPad X31, X40, X41. Not available in the X60. 8044 * 8045 * FANS ACPI handle: takes three arguments: low speed, medium speed, 8046 * high speed. ACPI DSDT seems to map these three speeds to levels 8047 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH 8048 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3") 8049 * 8050 * The speeds are stored on handles 8051 * (FANA:FAN9), (FANC:FANB), (FANE:FAND). 8052 * 8053 * There are three default speed sets, accessible as handles: 8054 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H 8055 * 8056 * ACPI DSDT switches which set is in use depending on various 8057 * factors. 8058 * 8059 * TPACPI_FAN_WR_TPEC is also available and should be used to 8060 * command the fan. The X31/X40/X41 seems to have 8 fan levels, 8061 * but the ACPI tables just mention level 7. 8062 */ 8063 8064 enum { /* Fan control constants */ 8065 fan_status_offset = 0x2f, /* EC register 0x2f */ 8066 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM) 8067 * 0x84 must be read before 0x85 */ 8068 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M) 8069 bit 0 selects which fan is active */ 8070 8071 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */ 8072 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */ 8073 8074 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */ 8075 }; 8076 8077 enum fan_status_access_mode { 8078 TPACPI_FAN_NONE = 0, /* No fan status or control */ 8079 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */ 8080 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */ 8081 }; 8082 8083 enum fan_control_access_mode { 8084 TPACPI_FAN_WR_NONE = 0, /* No fan control */ 8085 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */ 8086 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */ 8087 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */ 8088 }; 8089 8090 enum fan_control_commands { 8091 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */ 8092 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */ 8093 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd, 8094 * and also watchdog cmd */ 8095 }; 8096 8097 static bool fan_control_allowed; 8098 8099 static enum fan_status_access_mode fan_status_access_mode; 8100 static enum fan_control_access_mode fan_control_access_mode; 8101 static enum fan_control_commands fan_control_commands; 8102 8103 static u8 fan_control_initial_status; 8104 static u8 fan_control_desired_level; 8105 static u8 fan_control_resume_level; 8106 static int fan_watchdog_maxinterval; 8107 8108 static struct mutex fan_mutex; 8109 8110 static void fan_watchdog_fire(struct work_struct *ignored); 8111 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire); 8112 8113 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */ 8114 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */ 8115 "\\FSPD", /* 600e/x, 770e, 770x */ 8116 ); /* all others */ 8117 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */ 8118 "JFNS", /* 770x-JL */ 8119 ); /* all others */ 8120 8121 /* 8122 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the 8123 * HFSP register at boot, so it contains 0x07 but the Thinkpad could 8124 * be in auto mode (0x80). 8125 * 8126 * This is corrected by any write to HFSP either by the driver, or 8127 * by the firmware. 8128 * 8129 * We assume 0x07 really means auto mode while this quirk is active, 8130 * as this is far more likely than the ThinkPad being in level 7, 8131 * which is only used by the firmware during thermal emergencies. 8132 * 8133 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52), 8134 * TP-70 (T43, R52), which are known to be buggy. 8135 */ 8136 8137 static void fan_quirk1_setup(void) 8138 { 8139 if (fan_control_initial_status == 0x07) { 8140 pr_notice("fan_init: initial fan status is unknown, assuming it is in auto mode\n"); 8141 tp_features.fan_ctrl_status_undef = 1; 8142 } 8143 } 8144 8145 static void fan_quirk1_handle(u8 *fan_status) 8146 { 8147 if (unlikely(tp_features.fan_ctrl_status_undef)) { 8148 if (*fan_status != fan_control_initial_status) { 8149 /* something changed the HFSP regisnter since 8150 * driver init time, so it is not undefined 8151 * anymore */ 8152 tp_features.fan_ctrl_status_undef = 0; 8153 } else { 8154 /* Return most likely status. In fact, it 8155 * might be the only possible status */ 8156 *fan_status = TP_EC_FAN_AUTO; 8157 } 8158 } 8159 } 8160 8161 /* Select main fan on X60/X61, NOOP on others */ 8162 static bool fan_select_fan1(void) 8163 { 8164 if (tp_features.second_fan) { 8165 u8 val; 8166 8167 if (ec_read(fan_select_offset, &val) < 0) 8168 return false; 8169 val &= 0xFEU; 8170 if (ec_write(fan_select_offset, val) < 0) 8171 return false; 8172 } 8173 return true; 8174 } 8175 8176 /* Select secondary fan on X60/X61 */ 8177 static bool fan_select_fan2(void) 8178 { 8179 u8 val; 8180 8181 if (!tp_features.second_fan) 8182 return false; 8183 8184 if (ec_read(fan_select_offset, &val) < 0) 8185 return false; 8186 val |= 0x01U; 8187 if (ec_write(fan_select_offset, val) < 0) 8188 return false; 8189 8190 return true; 8191 } 8192 8193 /* 8194 * Call with fan_mutex held 8195 */ 8196 static void fan_update_desired_level(u8 status) 8197 { 8198 if ((status & 8199 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) { 8200 if (status > 7) 8201 fan_control_desired_level = 7; 8202 else 8203 fan_control_desired_level = status; 8204 } 8205 } 8206 8207 static int fan_get_status(u8 *status) 8208 { 8209 u8 s; 8210 8211 /* TODO: 8212 * Add TPACPI_FAN_RD_ACPI_FANS ? */ 8213 8214 switch (fan_status_access_mode) { 8215 case TPACPI_FAN_RD_ACPI_GFAN: { 8216 /* 570, 600e/x, 770e, 770x */ 8217 int res; 8218 8219 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d"))) 8220 return -EIO; 8221 8222 if (likely(status)) 8223 *status = res & 0x07; 8224 8225 break; 8226 } 8227 case TPACPI_FAN_RD_TPEC: 8228 /* all except 570, 600e/x, 770e, 770x */ 8229 if (unlikely(!acpi_ec_read(fan_status_offset, &s))) 8230 return -EIO; 8231 8232 if (likely(status)) { 8233 *status = s; 8234 fan_quirk1_handle(status); 8235 } 8236 8237 break; 8238 8239 default: 8240 return -ENXIO; 8241 } 8242 8243 return 0; 8244 } 8245 8246 static int fan_get_status_safe(u8 *status) 8247 { 8248 int rc; 8249 u8 s; 8250 8251 if (mutex_lock_killable(&fan_mutex)) 8252 return -ERESTARTSYS; 8253 rc = fan_get_status(&s); 8254 if (!rc) 8255 fan_update_desired_level(s); 8256 mutex_unlock(&fan_mutex); 8257 8258 if (rc) 8259 return rc; 8260 if (status) 8261 *status = s; 8262 8263 return 0; 8264 } 8265 8266 static int fan_get_speed(unsigned int *speed) 8267 { 8268 u8 hi, lo; 8269 8270 switch (fan_status_access_mode) { 8271 case TPACPI_FAN_RD_TPEC: 8272 /* all except 570, 600e/x, 770e, 770x */ 8273 if (unlikely(!fan_select_fan1())) 8274 return -EIO; 8275 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) || 8276 !acpi_ec_read(fan_rpm_offset + 1, &hi))) 8277 return -EIO; 8278 8279 if (likely(speed)) 8280 *speed = (hi << 8) | lo; 8281 8282 break; 8283 8284 default: 8285 return -ENXIO; 8286 } 8287 8288 return 0; 8289 } 8290 8291 static int fan2_get_speed(unsigned int *speed) 8292 { 8293 u8 hi, lo; 8294 bool rc; 8295 8296 switch (fan_status_access_mode) { 8297 case TPACPI_FAN_RD_TPEC: 8298 /* all except 570, 600e/x, 770e, 770x */ 8299 if (unlikely(!fan_select_fan2())) 8300 return -EIO; 8301 rc = !acpi_ec_read(fan_rpm_offset, &lo) || 8302 !acpi_ec_read(fan_rpm_offset + 1, &hi); 8303 fan_select_fan1(); /* play it safe */ 8304 if (rc) 8305 return -EIO; 8306 8307 if (likely(speed)) 8308 *speed = (hi << 8) | lo; 8309 8310 break; 8311 8312 default: 8313 return -ENXIO; 8314 } 8315 8316 return 0; 8317 } 8318 8319 static int fan_set_level(int level) 8320 { 8321 if (!fan_control_allowed) 8322 return -EPERM; 8323 8324 switch (fan_control_access_mode) { 8325 case TPACPI_FAN_WR_ACPI_SFAN: 8326 if ((level < 0) || (level > 7)) 8327 return -EINVAL; 8328 8329 if (tp_features.second_fan_ctl) { 8330 if (!fan_select_fan2() || 8331 !acpi_evalf(sfan_handle, NULL, NULL, "vd", level)) { 8332 pr_warn("Couldn't set 2nd fan level, disabling support\n"); 8333 tp_features.second_fan_ctl = 0; 8334 } 8335 fan_select_fan1(); 8336 } 8337 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level)) 8338 return -EIO; 8339 break; 8340 8341 case TPACPI_FAN_WR_ACPI_FANS: 8342 case TPACPI_FAN_WR_TPEC: 8343 if (!(level & TP_EC_FAN_AUTO) && 8344 !(level & TP_EC_FAN_FULLSPEED) && 8345 ((level < 0) || (level > 7))) 8346 return -EINVAL; 8347 8348 /* safety net should the EC not support AUTO 8349 * or FULLSPEED mode bits and just ignore them */ 8350 if (level & TP_EC_FAN_FULLSPEED) 8351 level |= 7; /* safety min speed 7 */ 8352 else if (level & TP_EC_FAN_AUTO) 8353 level |= 4; /* safety min speed 4 */ 8354 8355 if (tp_features.second_fan_ctl) { 8356 if (!fan_select_fan2() || 8357 !acpi_ec_write(fan_status_offset, level)) { 8358 pr_warn("Couldn't set 2nd fan level, disabling support\n"); 8359 tp_features.second_fan_ctl = 0; 8360 } 8361 fan_select_fan1(); 8362 8363 } 8364 if (!acpi_ec_write(fan_status_offset, level)) 8365 return -EIO; 8366 else 8367 tp_features.fan_ctrl_status_undef = 0; 8368 break; 8369 8370 default: 8371 return -ENXIO; 8372 } 8373 8374 vdbg_printk(TPACPI_DBG_FAN, 8375 "fan control: set fan control register to 0x%02x\n", level); 8376 return 0; 8377 } 8378 8379 static int fan_set_level_safe(int level) 8380 { 8381 int rc; 8382 8383 if (!fan_control_allowed) 8384 return -EPERM; 8385 8386 if (mutex_lock_killable(&fan_mutex)) 8387 return -ERESTARTSYS; 8388 8389 if (level == TPACPI_FAN_LAST_LEVEL) 8390 level = fan_control_desired_level; 8391 8392 rc = fan_set_level(level); 8393 if (!rc) 8394 fan_update_desired_level(level); 8395 8396 mutex_unlock(&fan_mutex); 8397 return rc; 8398 } 8399 8400 static int fan_set_enable(void) 8401 { 8402 u8 s; 8403 int rc; 8404 8405 if (!fan_control_allowed) 8406 return -EPERM; 8407 8408 if (mutex_lock_killable(&fan_mutex)) 8409 return -ERESTARTSYS; 8410 8411 switch (fan_control_access_mode) { 8412 case TPACPI_FAN_WR_ACPI_FANS: 8413 case TPACPI_FAN_WR_TPEC: 8414 rc = fan_get_status(&s); 8415 if (rc) 8416 break; 8417 8418 /* Don't go out of emergency fan mode */ 8419 if (s != 7) { 8420 s &= 0x07; 8421 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */ 8422 } 8423 8424 if (!acpi_ec_write(fan_status_offset, s)) 8425 rc = -EIO; 8426 else { 8427 tp_features.fan_ctrl_status_undef = 0; 8428 rc = 0; 8429 } 8430 break; 8431 8432 case TPACPI_FAN_WR_ACPI_SFAN: 8433 rc = fan_get_status(&s); 8434 if (rc) 8435 break; 8436 8437 s &= 0x07; 8438 8439 /* Set fan to at least level 4 */ 8440 s |= 4; 8441 8442 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s)) 8443 rc = -EIO; 8444 else 8445 rc = 0; 8446 break; 8447 8448 default: 8449 rc = -ENXIO; 8450 } 8451 8452 mutex_unlock(&fan_mutex); 8453 8454 if (!rc) 8455 vdbg_printk(TPACPI_DBG_FAN, 8456 "fan control: set fan control register to 0x%02x\n", 8457 s); 8458 return rc; 8459 } 8460 8461 static int fan_set_disable(void) 8462 { 8463 int rc; 8464 8465 if (!fan_control_allowed) 8466 return -EPERM; 8467 8468 if (mutex_lock_killable(&fan_mutex)) 8469 return -ERESTARTSYS; 8470 8471 rc = 0; 8472 switch (fan_control_access_mode) { 8473 case TPACPI_FAN_WR_ACPI_FANS: 8474 case TPACPI_FAN_WR_TPEC: 8475 if (!acpi_ec_write(fan_status_offset, 0x00)) 8476 rc = -EIO; 8477 else { 8478 fan_control_desired_level = 0; 8479 tp_features.fan_ctrl_status_undef = 0; 8480 } 8481 break; 8482 8483 case TPACPI_FAN_WR_ACPI_SFAN: 8484 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00)) 8485 rc = -EIO; 8486 else 8487 fan_control_desired_level = 0; 8488 break; 8489 8490 default: 8491 rc = -ENXIO; 8492 } 8493 8494 if (!rc) 8495 vdbg_printk(TPACPI_DBG_FAN, 8496 "fan control: set fan control register to 0\n"); 8497 8498 mutex_unlock(&fan_mutex); 8499 return rc; 8500 } 8501 8502 static int fan_set_speed(int speed) 8503 { 8504 int rc; 8505 8506 if (!fan_control_allowed) 8507 return -EPERM; 8508 8509 if (mutex_lock_killable(&fan_mutex)) 8510 return -ERESTARTSYS; 8511 8512 rc = 0; 8513 switch (fan_control_access_mode) { 8514 case TPACPI_FAN_WR_ACPI_FANS: 8515 if (speed >= 0 && speed <= 65535) { 8516 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd", 8517 speed, speed, speed)) 8518 rc = -EIO; 8519 } else 8520 rc = -EINVAL; 8521 break; 8522 8523 default: 8524 rc = -ENXIO; 8525 } 8526 8527 mutex_unlock(&fan_mutex); 8528 return rc; 8529 } 8530 8531 static void fan_watchdog_reset(void) 8532 { 8533 if (fan_control_access_mode == TPACPI_FAN_WR_NONE) 8534 return; 8535 8536 if (fan_watchdog_maxinterval > 0 && 8537 tpacpi_lifecycle != TPACPI_LIFE_EXITING) 8538 mod_delayed_work(tpacpi_wq, &fan_watchdog_task, 8539 msecs_to_jiffies(fan_watchdog_maxinterval * 1000)); 8540 else 8541 cancel_delayed_work(&fan_watchdog_task); 8542 } 8543 8544 static void fan_watchdog_fire(struct work_struct *ignored) 8545 { 8546 int rc; 8547 8548 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING) 8549 return; 8550 8551 pr_notice("fan watchdog: enabling fan\n"); 8552 rc = fan_set_enable(); 8553 if (rc < 0) { 8554 pr_err("fan watchdog: error %d while enabling fan, will try again later...\n", 8555 rc); 8556 /* reschedule for later */ 8557 fan_watchdog_reset(); 8558 } 8559 } 8560 8561 /* 8562 * SYSFS fan layout: hwmon compatible (device) 8563 * 8564 * pwm*_enable: 8565 * 0: "disengaged" mode 8566 * 1: manual mode 8567 * 2: native EC "auto" mode (recommended, hardware default) 8568 * 8569 * pwm*: set speed in manual mode, ignored otherwise. 8570 * 0 is level 0; 255 is level 7. Intermediate points done with linear 8571 * interpolation. 8572 * 8573 * fan*_input: tachometer reading, RPM 8574 * 8575 * 8576 * SYSFS fan layout: extensions 8577 * 8578 * fan_watchdog (driver): 8579 * fan watchdog interval in seconds, 0 disables (default), max 120 8580 */ 8581 8582 /* sysfs fan pwm1_enable ----------------------------------------------- */ 8583 static ssize_t fan_pwm1_enable_show(struct device *dev, 8584 struct device_attribute *attr, 8585 char *buf) 8586 { 8587 int res, mode; 8588 u8 status; 8589 8590 res = fan_get_status_safe(&status); 8591 if (res) 8592 return res; 8593 8594 if (status & TP_EC_FAN_FULLSPEED) { 8595 mode = 0; 8596 } else if (status & TP_EC_FAN_AUTO) { 8597 mode = 2; 8598 } else 8599 mode = 1; 8600 8601 return sysfs_emit(buf, "%d\n", mode); 8602 } 8603 8604 static ssize_t fan_pwm1_enable_store(struct device *dev, 8605 struct device_attribute *attr, 8606 const char *buf, size_t count) 8607 { 8608 unsigned long t; 8609 int res, level; 8610 8611 if (parse_strtoul(buf, 2, &t)) 8612 return -EINVAL; 8613 8614 tpacpi_disclose_usertask("hwmon pwm1_enable", 8615 "set fan mode to %lu\n", t); 8616 8617 switch (t) { 8618 case 0: 8619 level = TP_EC_FAN_FULLSPEED; 8620 break; 8621 case 1: 8622 level = TPACPI_FAN_LAST_LEVEL; 8623 break; 8624 case 2: 8625 level = TP_EC_FAN_AUTO; 8626 break; 8627 case 3: 8628 /* reserved for software-controlled auto mode */ 8629 return -ENOSYS; 8630 default: 8631 return -EINVAL; 8632 } 8633 8634 res = fan_set_level_safe(level); 8635 if (res == -ENXIO) 8636 return -EINVAL; 8637 else if (res < 0) 8638 return res; 8639 8640 fan_watchdog_reset(); 8641 8642 return count; 8643 } 8644 8645 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, 8646 fan_pwm1_enable_show, fan_pwm1_enable_store); 8647 8648 /* sysfs fan pwm1 ------------------------------------------------------ */ 8649 static ssize_t fan_pwm1_show(struct device *dev, 8650 struct device_attribute *attr, 8651 char *buf) 8652 { 8653 int res; 8654 u8 status; 8655 8656 res = fan_get_status_safe(&status); 8657 if (res) 8658 return res; 8659 8660 if ((status & 8661 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0) 8662 status = fan_control_desired_level; 8663 8664 if (status > 7) 8665 status = 7; 8666 8667 return sysfs_emit(buf, "%u\n", (status * 255) / 7); 8668 } 8669 8670 static ssize_t fan_pwm1_store(struct device *dev, 8671 struct device_attribute *attr, 8672 const char *buf, size_t count) 8673 { 8674 unsigned long s; 8675 int rc; 8676 u8 status, newlevel; 8677 8678 if (parse_strtoul(buf, 255, &s)) 8679 return -EINVAL; 8680 8681 tpacpi_disclose_usertask("hwmon pwm1", 8682 "set fan speed to %lu\n", s); 8683 8684 /* scale down from 0-255 to 0-7 */ 8685 newlevel = (s >> 5) & 0x07; 8686 8687 if (mutex_lock_killable(&fan_mutex)) 8688 return -ERESTARTSYS; 8689 8690 rc = fan_get_status(&status); 8691 if (!rc && (status & 8692 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) { 8693 rc = fan_set_level(newlevel); 8694 if (rc == -ENXIO) 8695 rc = -EINVAL; 8696 else if (!rc) { 8697 fan_update_desired_level(newlevel); 8698 fan_watchdog_reset(); 8699 } 8700 } 8701 8702 mutex_unlock(&fan_mutex); 8703 return (rc) ? rc : count; 8704 } 8705 8706 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store); 8707 8708 /* sysfs fan fan1_input ------------------------------------------------ */ 8709 static ssize_t fan_fan1_input_show(struct device *dev, 8710 struct device_attribute *attr, 8711 char *buf) 8712 { 8713 int res; 8714 unsigned int speed; 8715 8716 res = fan_get_speed(&speed); 8717 if (res < 0) 8718 return res; 8719 8720 return sysfs_emit(buf, "%u\n", speed); 8721 } 8722 8723 static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL); 8724 8725 /* sysfs fan fan2_input ------------------------------------------------ */ 8726 static ssize_t fan_fan2_input_show(struct device *dev, 8727 struct device_attribute *attr, 8728 char *buf) 8729 { 8730 int res; 8731 unsigned int speed; 8732 8733 res = fan2_get_speed(&speed); 8734 if (res < 0) 8735 return res; 8736 8737 return sysfs_emit(buf, "%u\n", speed); 8738 } 8739 8740 static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL); 8741 8742 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */ 8743 static ssize_t fan_watchdog_show(struct device_driver *drv, char *buf) 8744 { 8745 return sysfs_emit(buf, "%u\n", fan_watchdog_maxinterval); 8746 } 8747 8748 static ssize_t fan_watchdog_store(struct device_driver *drv, const char *buf, 8749 size_t count) 8750 { 8751 unsigned long t; 8752 8753 if (parse_strtoul(buf, 120, &t)) 8754 return -EINVAL; 8755 8756 if (!fan_control_allowed) 8757 return -EPERM; 8758 8759 fan_watchdog_maxinterval = t; 8760 fan_watchdog_reset(); 8761 8762 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t); 8763 8764 return count; 8765 } 8766 static DRIVER_ATTR_RW(fan_watchdog); 8767 8768 /* --------------------------------------------------------------------- */ 8769 8770 static struct attribute *fan_attributes[] = { 8771 &dev_attr_pwm1_enable.attr, 8772 &dev_attr_pwm1.attr, 8773 &dev_attr_fan1_input.attr, 8774 &dev_attr_fan2_input.attr, 8775 NULL 8776 }; 8777 8778 static umode_t fan_attr_is_visible(struct kobject *kobj, struct attribute *attr, 8779 int n) 8780 { 8781 if (fan_status_access_mode == TPACPI_FAN_NONE && 8782 fan_control_access_mode == TPACPI_FAN_WR_NONE) 8783 return 0; 8784 8785 if (attr == &dev_attr_fan2_input.attr) { 8786 if (!tp_features.second_fan) 8787 return 0; 8788 } 8789 8790 return attr->mode; 8791 } 8792 8793 static const struct attribute_group fan_attr_group = { 8794 .is_visible = fan_attr_is_visible, 8795 .attrs = fan_attributes, 8796 }; 8797 8798 static struct attribute *fan_driver_attributes[] = { 8799 &driver_attr_fan_watchdog.attr, 8800 NULL 8801 }; 8802 8803 static const struct attribute_group fan_driver_attr_group = { 8804 .is_visible = fan_attr_is_visible, 8805 .attrs = fan_driver_attributes, 8806 }; 8807 8808 #define TPACPI_FAN_Q1 0x0001 /* Uninitialized HFSP */ 8809 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */ 8810 #define TPACPI_FAN_2CTL 0x0004 /* selects fan2 control */ 8811 #define TPACPI_FAN_NOFAN 0x0008 /* no fan available */ 8812 8813 static const struct tpacpi_quirk fan_quirk_table[] __initconst = { 8814 TPACPI_QEC_IBM('1', 'Y', TPACPI_FAN_Q1), 8815 TPACPI_QEC_IBM('7', '8', TPACPI_FAN_Q1), 8816 TPACPI_QEC_IBM('7', '6', TPACPI_FAN_Q1), 8817 TPACPI_QEC_IBM('7', '0', TPACPI_FAN_Q1), 8818 TPACPI_QEC_LNV('7', 'M', TPACPI_FAN_2FAN), 8819 TPACPI_Q_LNV('N', '1', TPACPI_FAN_2FAN), 8820 TPACPI_Q_LNV3('N', '1', 'D', TPACPI_FAN_2CTL), /* P70 */ 8821 TPACPI_Q_LNV3('N', '1', 'E', TPACPI_FAN_2CTL), /* P50 */ 8822 TPACPI_Q_LNV3('N', '1', 'T', TPACPI_FAN_2CTL), /* P71 */ 8823 TPACPI_Q_LNV3('N', '1', 'U', TPACPI_FAN_2CTL), /* P51 */ 8824 TPACPI_Q_LNV3('N', '2', 'C', TPACPI_FAN_2CTL), /* P52 / P72 */ 8825 TPACPI_Q_LNV3('N', '2', 'N', TPACPI_FAN_2CTL), /* P53 / P73 */ 8826 TPACPI_Q_LNV3('N', '2', 'E', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (1st gen) */ 8827 TPACPI_Q_LNV3('N', '2', 'O', TPACPI_FAN_2CTL), /* P1 / X1 Extreme (2nd gen) */ 8828 TPACPI_Q_LNV3('N', '3', '0', TPACPI_FAN_2CTL), /* P15 (1st gen) / P15v (1st gen) */ 8829 TPACPI_Q_LNV3('N', '3', '7', TPACPI_FAN_2CTL), /* T15g (2nd gen) */ 8830 TPACPI_Q_LNV3('N', '1', 'O', TPACPI_FAN_NOFAN), /* X1 Tablet (2nd gen) */ 8831 }; 8832 8833 static int __init fan_init(struct ibm_init_struct *iibm) 8834 { 8835 unsigned long quirks; 8836 8837 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN, 8838 "initializing fan subdriver\n"); 8839 8840 mutex_init(&fan_mutex); 8841 fan_status_access_mode = TPACPI_FAN_NONE; 8842 fan_control_access_mode = TPACPI_FAN_WR_NONE; 8843 fan_control_commands = 0; 8844 fan_watchdog_maxinterval = 0; 8845 tp_features.fan_ctrl_status_undef = 0; 8846 tp_features.second_fan = 0; 8847 tp_features.second_fan_ctl = 0; 8848 fan_control_desired_level = 7; 8849 8850 if (tpacpi_is_ibm()) { 8851 TPACPI_ACPIHANDLE_INIT(fans); 8852 TPACPI_ACPIHANDLE_INIT(gfan); 8853 TPACPI_ACPIHANDLE_INIT(sfan); 8854 } 8855 8856 quirks = tpacpi_check_quirks(fan_quirk_table, 8857 ARRAY_SIZE(fan_quirk_table)); 8858 8859 if (quirks & TPACPI_FAN_NOFAN) { 8860 pr_info("No integrated ThinkPad fan available\n"); 8861 return -ENODEV; 8862 } 8863 8864 if (gfan_handle) { 8865 /* 570, 600e/x, 770e, 770x */ 8866 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN; 8867 } else { 8868 /* all other ThinkPads: note that even old-style 8869 * ThinkPad ECs supports the fan control register */ 8870 if (likely(acpi_ec_read(fan_status_offset, 8871 &fan_control_initial_status))) { 8872 int res; 8873 unsigned int speed; 8874 8875 fan_status_access_mode = TPACPI_FAN_RD_TPEC; 8876 if (quirks & TPACPI_FAN_Q1) 8877 fan_quirk1_setup(); 8878 /* Try and probe the 2nd fan */ 8879 tp_features.second_fan = 1; /* needed for get_speed to work */ 8880 res = fan2_get_speed(&speed); 8881 if (res >= 0 && speed != FAN_NOT_PRESENT) { 8882 /* It responded - so let's assume it's there */ 8883 tp_features.second_fan = 1; 8884 tp_features.second_fan_ctl = 1; 8885 pr_info("secondary fan control detected & enabled\n"); 8886 } else { 8887 /* Fan not auto-detected */ 8888 tp_features.second_fan = 0; 8889 if (quirks & TPACPI_FAN_2FAN) { 8890 tp_features.second_fan = 1; 8891 pr_info("secondary fan support enabled\n"); 8892 } 8893 if (quirks & TPACPI_FAN_2CTL) { 8894 tp_features.second_fan = 1; 8895 tp_features.second_fan_ctl = 1; 8896 pr_info("secondary fan control enabled\n"); 8897 } 8898 } 8899 } else { 8900 pr_err("ThinkPad ACPI EC access misbehaving, fan status and control unavailable\n"); 8901 return -ENODEV; 8902 } 8903 } 8904 8905 if (sfan_handle) { 8906 /* 570, 770x-JL */ 8907 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN; 8908 fan_control_commands |= 8909 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE; 8910 } else { 8911 if (!gfan_handle) { 8912 /* gfan without sfan means no fan control */ 8913 /* all other models implement TP EC 0x2f control */ 8914 8915 if (fans_handle) { 8916 /* X31, X40, X41 */ 8917 fan_control_access_mode = 8918 TPACPI_FAN_WR_ACPI_FANS; 8919 fan_control_commands |= 8920 TPACPI_FAN_CMD_SPEED | 8921 TPACPI_FAN_CMD_LEVEL | 8922 TPACPI_FAN_CMD_ENABLE; 8923 } else { 8924 fan_control_access_mode = TPACPI_FAN_WR_TPEC; 8925 fan_control_commands |= 8926 TPACPI_FAN_CMD_LEVEL | 8927 TPACPI_FAN_CMD_ENABLE; 8928 } 8929 } 8930 } 8931 8932 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN, 8933 "fan is %s, modes %d, %d\n", 8934 str_supported(fan_status_access_mode != TPACPI_FAN_NONE || 8935 fan_control_access_mode != TPACPI_FAN_WR_NONE), 8936 fan_status_access_mode, fan_control_access_mode); 8937 8938 /* fan control master switch */ 8939 if (!fan_control_allowed) { 8940 fan_control_access_mode = TPACPI_FAN_WR_NONE; 8941 fan_control_commands = 0; 8942 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN, 8943 "fan control features disabled by parameter\n"); 8944 } 8945 8946 /* update fan_control_desired_level */ 8947 if (fan_status_access_mode != TPACPI_FAN_NONE) 8948 fan_get_status_safe(NULL); 8949 8950 if (fan_status_access_mode == TPACPI_FAN_NONE && 8951 fan_control_access_mode == TPACPI_FAN_WR_NONE) 8952 return -ENODEV; 8953 8954 return 0; 8955 } 8956 8957 static void fan_exit(void) 8958 { 8959 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN, 8960 "cancelling any pending fan watchdog tasks\n"); 8961 8962 cancel_delayed_work(&fan_watchdog_task); 8963 flush_workqueue(tpacpi_wq); 8964 } 8965 8966 static void fan_suspend(void) 8967 { 8968 int rc; 8969 8970 if (!fan_control_allowed) 8971 return; 8972 8973 /* Store fan status in cache */ 8974 fan_control_resume_level = 0; 8975 rc = fan_get_status_safe(&fan_control_resume_level); 8976 if (rc) 8977 pr_notice("failed to read fan level for later restore during resume: %d\n", 8978 rc); 8979 8980 /* if it is undefined, don't attempt to restore it. 8981 * KEEP THIS LAST */ 8982 if (tp_features.fan_ctrl_status_undef) 8983 fan_control_resume_level = 0; 8984 } 8985 8986 static void fan_resume(void) 8987 { 8988 u8 current_level = 7; 8989 bool do_set = false; 8990 int rc; 8991 8992 /* DSDT *always* updates status on resume */ 8993 tp_features.fan_ctrl_status_undef = 0; 8994 8995 if (!fan_control_allowed || 8996 !fan_control_resume_level || 8997 fan_get_status_safe(¤t_level)) 8998 return; 8999 9000 switch (fan_control_access_mode) { 9001 case TPACPI_FAN_WR_ACPI_SFAN: 9002 /* never decrease fan level */ 9003 do_set = (fan_control_resume_level > current_level); 9004 break; 9005 case TPACPI_FAN_WR_ACPI_FANS: 9006 case TPACPI_FAN_WR_TPEC: 9007 /* never decrease fan level, scale is: 9008 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO 9009 * 9010 * We expect the firmware to set either 7 or AUTO, but we 9011 * handle FULLSPEED out of paranoia. 9012 * 9013 * So, we can safely only restore FULLSPEED or 7, anything 9014 * else could slow the fan. Restoring AUTO is useless, at 9015 * best that's exactly what the DSDT already set (it is the 9016 * slower it uses). 9017 * 9018 * Always keep in mind that the DSDT *will* have set the 9019 * fans to what the vendor supposes is the best level. We 9020 * muck with it only to speed the fan up. 9021 */ 9022 if (fan_control_resume_level != 7 && 9023 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED)) 9024 return; 9025 else 9026 do_set = !(current_level & TP_EC_FAN_FULLSPEED) && 9027 (current_level != fan_control_resume_level); 9028 break; 9029 default: 9030 return; 9031 } 9032 if (do_set) { 9033 pr_notice("restoring fan level to 0x%02x\n", 9034 fan_control_resume_level); 9035 rc = fan_set_level_safe(fan_control_resume_level); 9036 if (rc < 0) 9037 pr_notice("failed to restore fan level: %d\n", rc); 9038 } 9039 } 9040 9041 static int fan_read(struct seq_file *m) 9042 { 9043 int rc; 9044 u8 status; 9045 unsigned int speed = 0; 9046 9047 switch (fan_status_access_mode) { 9048 case TPACPI_FAN_RD_ACPI_GFAN: 9049 /* 570, 600e/x, 770e, 770x */ 9050 rc = fan_get_status_safe(&status); 9051 if (rc) 9052 return rc; 9053 9054 seq_printf(m, "status:\t\t%s\n" 9055 "level:\t\t%d\n", 9056 str_enabled_disabled(status), status); 9057 break; 9058 9059 case TPACPI_FAN_RD_TPEC: 9060 /* all except 570, 600e/x, 770e, 770x */ 9061 rc = fan_get_status_safe(&status); 9062 if (rc) 9063 return rc; 9064 9065 seq_printf(m, "status:\t\t%s\n", str_enabled_disabled(status)); 9066 9067 rc = fan_get_speed(&speed); 9068 if (rc < 0) 9069 return rc; 9070 9071 seq_printf(m, "speed:\t\t%d\n", speed); 9072 9073 if (status & TP_EC_FAN_FULLSPEED) 9074 /* Disengaged mode takes precedence */ 9075 seq_printf(m, "level:\t\tdisengaged\n"); 9076 else if (status & TP_EC_FAN_AUTO) 9077 seq_printf(m, "level:\t\tauto\n"); 9078 else 9079 seq_printf(m, "level:\t\t%d\n", status); 9080 break; 9081 9082 case TPACPI_FAN_NONE: 9083 default: 9084 seq_printf(m, "status:\t\tnot supported\n"); 9085 } 9086 9087 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) { 9088 seq_printf(m, "commands:\tlevel <level>"); 9089 9090 switch (fan_control_access_mode) { 9091 case TPACPI_FAN_WR_ACPI_SFAN: 9092 seq_printf(m, " (<level> is 0-7)\n"); 9093 break; 9094 9095 default: 9096 seq_printf(m, " (<level> is 0-7, auto, disengaged, full-speed)\n"); 9097 break; 9098 } 9099 } 9100 9101 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE) 9102 seq_printf(m, "commands:\tenable, disable\n" 9103 "commands:\twatchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))\n"); 9104 9105 if (fan_control_commands & TPACPI_FAN_CMD_SPEED) 9106 seq_printf(m, "commands:\tspeed <speed> (<speed> is 0-65535)\n"); 9107 9108 return 0; 9109 } 9110 9111 static int fan_write_cmd_level(const char *cmd, int *rc) 9112 { 9113 int level; 9114 9115 if (strlencmp(cmd, "level auto") == 0) 9116 level = TP_EC_FAN_AUTO; 9117 else if ((strlencmp(cmd, "level disengaged") == 0) || 9118 (strlencmp(cmd, "level full-speed") == 0)) 9119 level = TP_EC_FAN_FULLSPEED; 9120 else if (sscanf(cmd, "level %d", &level) != 1) 9121 return 0; 9122 9123 *rc = fan_set_level_safe(level); 9124 if (*rc == -ENXIO) 9125 pr_err("level command accepted for unsupported access mode %d\n", 9126 fan_control_access_mode); 9127 else if (!*rc) 9128 tpacpi_disclose_usertask("procfs fan", 9129 "set level to %d\n", level); 9130 9131 return 1; 9132 } 9133 9134 static int fan_write_cmd_enable(const char *cmd, int *rc) 9135 { 9136 if (strlencmp(cmd, "enable") != 0) 9137 return 0; 9138 9139 *rc = fan_set_enable(); 9140 if (*rc == -ENXIO) 9141 pr_err("enable command accepted for unsupported access mode %d\n", 9142 fan_control_access_mode); 9143 else if (!*rc) 9144 tpacpi_disclose_usertask("procfs fan", "enable\n"); 9145 9146 return 1; 9147 } 9148 9149 static int fan_write_cmd_disable(const char *cmd, int *rc) 9150 { 9151 if (strlencmp(cmd, "disable") != 0) 9152 return 0; 9153 9154 *rc = fan_set_disable(); 9155 if (*rc == -ENXIO) 9156 pr_err("disable command accepted for unsupported access mode %d\n", 9157 fan_control_access_mode); 9158 else if (!*rc) 9159 tpacpi_disclose_usertask("procfs fan", "disable\n"); 9160 9161 return 1; 9162 } 9163 9164 static int fan_write_cmd_speed(const char *cmd, int *rc) 9165 { 9166 int speed; 9167 9168 /* TODO: 9169 * Support speed <low> <medium> <high> ? */ 9170 9171 if (sscanf(cmd, "speed %d", &speed) != 1) 9172 return 0; 9173 9174 *rc = fan_set_speed(speed); 9175 if (*rc == -ENXIO) 9176 pr_err("speed command accepted for unsupported access mode %d\n", 9177 fan_control_access_mode); 9178 else if (!*rc) 9179 tpacpi_disclose_usertask("procfs fan", 9180 "set speed to %d\n", speed); 9181 9182 return 1; 9183 } 9184 9185 static int fan_write_cmd_watchdog(const char *cmd, int *rc) 9186 { 9187 int interval; 9188 9189 if (sscanf(cmd, "watchdog %d", &interval) != 1) 9190 return 0; 9191 9192 if (interval < 0 || interval > 120) 9193 *rc = -EINVAL; 9194 else { 9195 fan_watchdog_maxinterval = interval; 9196 tpacpi_disclose_usertask("procfs fan", 9197 "set watchdog timer to %d\n", 9198 interval); 9199 } 9200 9201 return 1; 9202 } 9203 9204 static int fan_write(char *buf) 9205 { 9206 char *cmd; 9207 int rc = 0; 9208 9209 while (!rc && (cmd = strsep(&buf, ","))) { 9210 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) && 9211 fan_write_cmd_level(cmd, &rc)) && 9212 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) && 9213 (fan_write_cmd_enable(cmd, &rc) || 9214 fan_write_cmd_disable(cmd, &rc) || 9215 fan_write_cmd_watchdog(cmd, &rc))) && 9216 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) && 9217 fan_write_cmd_speed(cmd, &rc)) 9218 ) 9219 rc = -EINVAL; 9220 else if (!rc) 9221 fan_watchdog_reset(); 9222 } 9223 9224 return rc; 9225 } 9226 9227 static struct ibm_struct fan_driver_data = { 9228 .name = "fan", 9229 .read = fan_read, 9230 .write = fan_write, 9231 .exit = fan_exit, 9232 .suspend = fan_suspend, 9233 .resume = fan_resume, 9234 }; 9235 9236 /************************************************************************* 9237 * Mute LED subdriver 9238 */ 9239 9240 #define TPACPI_LED_MAX 2 9241 9242 struct tp_led_table { 9243 acpi_string name; 9244 int on_value; 9245 int off_value; 9246 int state; 9247 }; 9248 9249 static struct tp_led_table led_tables[TPACPI_LED_MAX] = { 9250 [LED_AUDIO_MUTE] = { 9251 .name = "SSMS", 9252 .on_value = 1, 9253 .off_value = 0, 9254 }, 9255 [LED_AUDIO_MICMUTE] = { 9256 .name = "MMTS", 9257 .on_value = 2, 9258 .off_value = 0, 9259 }, 9260 }; 9261 9262 static int mute_led_on_off(struct tp_led_table *t, bool state) 9263 { 9264 acpi_handle temp; 9265 int output; 9266 9267 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) { 9268 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name); 9269 return -EIO; 9270 } 9271 9272 if (!acpi_evalf(hkey_handle, &output, t->name, "dd", 9273 state ? t->on_value : t->off_value)) 9274 return -EIO; 9275 9276 t->state = state; 9277 return state; 9278 } 9279 9280 static int tpacpi_led_set(int whichled, bool on) 9281 { 9282 struct tp_led_table *t; 9283 9284 t = &led_tables[whichled]; 9285 if (t->state < 0 || t->state == on) 9286 return t->state; 9287 return mute_led_on_off(t, on); 9288 } 9289 9290 static int tpacpi_led_mute_set(struct led_classdev *led_cdev, 9291 enum led_brightness brightness) 9292 { 9293 return tpacpi_led_set(LED_AUDIO_MUTE, brightness != LED_OFF); 9294 } 9295 9296 static int tpacpi_led_micmute_set(struct led_classdev *led_cdev, 9297 enum led_brightness brightness) 9298 { 9299 return tpacpi_led_set(LED_AUDIO_MICMUTE, brightness != LED_OFF); 9300 } 9301 9302 static struct led_classdev mute_led_cdev[TPACPI_LED_MAX] = { 9303 [LED_AUDIO_MUTE] = { 9304 .name = "platform::mute", 9305 .max_brightness = 1, 9306 .brightness_set_blocking = tpacpi_led_mute_set, 9307 .default_trigger = "audio-mute", 9308 }, 9309 [LED_AUDIO_MICMUTE] = { 9310 .name = "platform::micmute", 9311 .max_brightness = 1, 9312 .brightness_set_blocking = tpacpi_led_micmute_set, 9313 .default_trigger = "audio-micmute", 9314 }, 9315 }; 9316 9317 static int mute_led_init(struct ibm_init_struct *iibm) 9318 { 9319 acpi_handle temp; 9320 int i, err; 9321 9322 for (i = 0; i < TPACPI_LED_MAX; i++) { 9323 struct tp_led_table *t = &led_tables[i]; 9324 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) { 9325 t->state = -ENODEV; 9326 continue; 9327 } 9328 9329 mute_led_cdev[i].brightness = ledtrig_audio_get(i); 9330 err = led_classdev_register(&tpacpi_pdev->dev, &mute_led_cdev[i]); 9331 if (err < 0) { 9332 while (i--) 9333 led_classdev_unregister(&mute_led_cdev[i]); 9334 return err; 9335 } 9336 } 9337 return 0; 9338 } 9339 9340 static void mute_led_exit(void) 9341 { 9342 int i; 9343 9344 for (i = 0; i < TPACPI_LED_MAX; i++) { 9345 led_classdev_unregister(&mute_led_cdev[i]); 9346 tpacpi_led_set(i, false); 9347 } 9348 } 9349 9350 static void mute_led_resume(void) 9351 { 9352 int i; 9353 9354 for (i = 0; i < TPACPI_LED_MAX; i++) { 9355 struct tp_led_table *t = &led_tables[i]; 9356 if (t->state >= 0) 9357 mute_led_on_off(t, t->state); 9358 } 9359 } 9360 9361 static struct ibm_struct mute_led_driver_data = { 9362 .name = "mute_led", 9363 .exit = mute_led_exit, 9364 .resume = mute_led_resume, 9365 }; 9366 9367 /* 9368 * Battery Wear Control Driver 9369 * Contact: Ognjen Galic <smclt30p@gmail.com> 9370 */ 9371 9372 /* Metadata */ 9373 9374 #define GET_START "BCTG" 9375 #define SET_START "BCCS" 9376 #define GET_STOP "BCSG" 9377 #define SET_STOP "BCSS" 9378 #define GET_DISCHARGE "BDSG" 9379 #define SET_DISCHARGE "BDSS" 9380 #define GET_INHIBIT "BICG" 9381 #define SET_INHIBIT "BICS" 9382 9383 enum { 9384 BAT_ANY = 0, 9385 BAT_PRIMARY = 1, 9386 BAT_SECONDARY = 2 9387 }; 9388 9389 enum { 9390 /* Error condition bit */ 9391 METHOD_ERR = BIT(31), 9392 }; 9393 9394 enum { 9395 /* This is used in the get/set helpers */ 9396 THRESHOLD_START, 9397 THRESHOLD_STOP, 9398 FORCE_DISCHARGE, 9399 INHIBIT_CHARGE, 9400 }; 9401 9402 struct tpacpi_battery_data { 9403 int charge_start; 9404 int start_support; 9405 int charge_stop; 9406 int stop_support; 9407 unsigned int charge_behaviours; 9408 }; 9409 9410 struct tpacpi_battery_driver_data { 9411 struct tpacpi_battery_data batteries[3]; 9412 int individual_addressing; 9413 }; 9414 9415 static struct tpacpi_battery_driver_data battery_info; 9416 9417 /* ACPI helpers/functions/probes */ 9418 9419 /** 9420 * This evaluates a ACPI method call specific to the battery 9421 * ACPI extension. The specifics are that an error is marked 9422 * in the 32rd bit of the response, so we just check that here. 9423 */ 9424 static acpi_status tpacpi_battery_acpi_eval(char *method, int *ret, int param) 9425 { 9426 int response; 9427 9428 if (!acpi_evalf(hkey_handle, &response, method, "dd", param)) { 9429 acpi_handle_err(hkey_handle, "%s: evaluate failed", method); 9430 return AE_ERROR; 9431 } 9432 if (response & METHOD_ERR) { 9433 acpi_handle_err(hkey_handle, 9434 "%s evaluated but flagged as error", method); 9435 return AE_ERROR; 9436 } 9437 *ret = response; 9438 return AE_OK; 9439 } 9440 9441 static int tpacpi_battery_get(int what, int battery, int *ret) 9442 { 9443 switch (what) { 9444 case THRESHOLD_START: 9445 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, ret, battery)) 9446 return -ENODEV; 9447 9448 /* The value is in the low 8 bits of the response */ 9449 *ret = *ret & 0xFF; 9450 return 0; 9451 case THRESHOLD_STOP: 9452 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, ret, battery)) 9453 return -ENODEV; 9454 /* Value is in lower 8 bits */ 9455 *ret = *ret & 0xFF; 9456 /* 9457 * On the stop value, if we return 0 that 9458 * does not make any sense. 0 means Default, which 9459 * means that charging stops at 100%, so we return 9460 * that. 9461 */ 9462 if (*ret == 0) 9463 *ret = 100; 9464 return 0; 9465 case FORCE_DISCHARGE: 9466 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, ret, battery)) 9467 return -ENODEV; 9468 /* The force discharge status is in bit 0 */ 9469 *ret = *ret & 0x01; 9470 return 0; 9471 case INHIBIT_CHARGE: 9472 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, ret, battery)) 9473 return -ENODEV; 9474 /* The inhibit charge status is in bit 0 */ 9475 *ret = *ret & 0x01; 9476 return 0; 9477 default: 9478 pr_crit("wrong parameter: %d", what); 9479 return -EINVAL; 9480 } 9481 } 9482 9483 static int tpacpi_battery_set(int what, int battery, int value) 9484 { 9485 int param, ret; 9486 /* The first 8 bits are the value of the threshold */ 9487 param = value; 9488 /* The battery ID is in bits 8-9, 2 bits */ 9489 param |= battery << 8; 9490 9491 switch (what) { 9492 case THRESHOLD_START: 9493 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_START, &ret, param)) { 9494 pr_err("failed to set charge threshold on battery %d", 9495 battery); 9496 return -ENODEV; 9497 } 9498 return 0; 9499 case THRESHOLD_STOP: 9500 if ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_STOP, &ret, param)) { 9501 pr_err("failed to set stop threshold: %d", battery); 9502 return -ENODEV; 9503 } 9504 return 0; 9505 case FORCE_DISCHARGE: 9506 /* Force discharge is in bit 0, 9507 * break on AC attach is in bit 1 (won't work on some ThinkPads), 9508 * battery ID is in bits 8-9, 2 bits. 9509 */ 9510 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_DISCHARGE, &ret, param))) { 9511 pr_err("failed to set force discharge on %d", battery); 9512 return -ENODEV; 9513 } 9514 return 0; 9515 case INHIBIT_CHARGE: 9516 /* When setting inhibit charge, we set a default value of 9517 * always breaking on AC detach and the effective time is set to 9518 * be permanent. 9519 * The battery ID is in bits 4-5, 2 bits, 9520 * the effective time is in bits 8-23, 2 bytes. 9521 * A time of FFFF indicates forever. 9522 */ 9523 param = value; 9524 param |= battery << 4; 9525 param |= 0xFFFF << 8; 9526 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(SET_INHIBIT, &ret, param))) { 9527 pr_err("failed to set inhibit charge on %d", battery); 9528 return -ENODEV; 9529 } 9530 return 0; 9531 default: 9532 pr_crit("wrong parameter: %d", what); 9533 return -EINVAL; 9534 } 9535 } 9536 9537 static int tpacpi_battery_set_validate(int what, int battery, int value) 9538 { 9539 int ret, v; 9540 9541 ret = tpacpi_battery_set(what, battery, value); 9542 if (ret < 0) 9543 return ret; 9544 9545 ret = tpacpi_battery_get(what, battery, &v); 9546 if (ret < 0) 9547 return ret; 9548 9549 if (v == value) 9550 return 0; 9551 9552 msleep(500); 9553 9554 ret = tpacpi_battery_get(what, battery, &v); 9555 if (ret < 0) 9556 return ret; 9557 9558 if (v == value) 9559 return 0; 9560 9561 return -EIO; 9562 } 9563 9564 static int tpacpi_battery_probe(int battery) 9565 { 9566 int ret = 0; 9567 9568 memset(&battery_info.batteries[battery], 0, 9569 sizeof(battery_info.batteries[battery])); 9570 9571 /* 9572 * 1) Get the current start threshold 9573 * 2) Check for support 9574 * 3) Get the current stop threshold 9575 * 4) Check for support 9576 * 5) Get the current force discharge status 9577 * 6) Check for support 9578 * 7) Get the current inhibit charge status 9579 * 8) Check for support 9580 */ 9581 if (acpi_has_method(hkey_handle, GET_START)) { 9582 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_START, &ret, battery)) { 9583 pr_err("Error probing battery %d\n", battery); 9584 return -ENODEV; 9585 } 9586 /* Individual addressing is in bit 9 */ 9587 if (ret & BIT(9)) 9588 battery_info.individual_addressing = true; 9589 /* Support is marked in bit 8 */ 9590 if (ret & BIT(8)) 9591 battery_info.batteries[battery].start_support = 1; 9592 else 9593 return -ENODEV; 9594 if (tpacpi_battery_get(THRESHOLD_START, battery, 9595 &battery_info.batteries[battery].charge_start)) { 9596 pr_err("Error probing battery %d\n", battery); 9597 return -ENODEV; 9598 } 9599 } 9600 if (acpi_has_method(hkey_handle, GET_STOP)) { 9601 if ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_STOP, &ret, battery)) { 9602 pr_err("Error probing battery stop; %d\n", battery); 9603 return -ENODEV; 9604 } 9605 /* Support is marked in bit 8 */ 9606 if (ret & BIT(8)) 9607 battery_info.batteries[battery].stop_support = 1; 9608 else 9609 return -ENODEV; 9610 if (tpacpi_battery_get(THRESHOLD_STOP, battery, 9611 &battery_info.batteries[battery].charge_stop)) { 9612 pr_err("Error probing battery stop: %d\n", battery); 9613 return -ENODEV; 9614 } 9615 } 9616 if (acpi_has_method(hkey_handle, GET_DISCHARGE)) { 9617 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_DISCHARGE, &ret, battery))) { 9618 pr_err("Error probing battery discharge; %d\n", battery); 9619 return -ENODEV; 9620 } 9621 /* Support is marked in bit 8 */ 9622 if (ret & BIT(8)) 9623 battery_info.batteries[battery].charge_behaviours |= 9624 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE); 9625 } 9626 if (acpi_has_method(hkey_handle, GET_INHIBIT)) { 9627 if (ACPI_FAILURE(tpacpi_battery_acpi_eval(GET_INHIBIT, &ret, battery))) { 9628 pr_err("Error probing battery inhibit charge; %d\n", battery); 9629 return -ENODEV; 9630 } 9631 /* Support is marked in bit 5 */ 9632 if (ret & BIT(5)) 9633 battery_info.batteries[battery].charge_behaviours |= 9634 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE); 9635 } 9636 9637 battery_info.batteries[battery].charge_behaviours |= 9638 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO); 9639 9640 pr_info("battery %d registered (start %d, stop %d, behaviours: 0x%x)\n", 9641 battery, 9642 battery_info.batteries[battery].charge_start, 9643 battery_info.batteries[battery].charge_stop, 9644 battery_info.batteries[battery].charge_behaviours); 9645 9646 return 0; 9647 } 9648 9649 /* General helper functions */ 9650 9651 static int tpacpi_battery_get_id(const char *battery_name) 9652 { 9653 9654 if (strcmp(battery_name, "BAT0") == 0 || 9655 tp_features.battery_force_primary) 9656 return BAT_PRIMARY; 9657 if (strcmp(battery_name, "BAT1") == 0) 9658 return BAT_SECONDARY; 9659 /* 9660 * If for some reason the battery is not BAT0 nor is it 9661 * BAT1, we will assume it's the default, first battery, 9662 * AKA primary. 9663 */ 9664 pr_warn("unknown battery %s, assuming primary", battery_name); 9665 return BAT_PRIMARY; 9666 } 9667 9668 /* sysfs interface */ 9669 9670 static ssize_t tpacpi_battery_store(int what, 9671 struct device *dev, 9672 const char *buf, size_t count) 9673 { 9674 struct power_supply *supply = to_power_supply(dev); 9675 unsigned long value; 9676 int battery, rval; 9677 /* 9678 * Some systems have support for more than 9679 * one battery. If that is the case, 9680 * tpacpi_battery_probe marked that addressing 9681 * them individually is supported, so we do that 9682 * based on the device struct. 9683 * 9684 * On systems that are not supported, we assume 9685 * the primary as most of the ACPI calls fail 9686 * with "Any Battery" as the parameter. 9687 */ 9688 if (battery_info.individual_addressing) 9689 /* BAT_PRIMARY or BAT_SECONDARY */ 9690 battery = tpacpi_battery_get_id(supply->desc->name); 9691 else 9692 battery = BAT_PRIMARY; 9693 9694 rval = kstrtoul(buf, 10, &value); 9695 if (rval) 9696 return rval; 9697 9698 switch (what) { 9699 case THRESHOLD_START: 9700 if (!battery_info.batteries[battery].start_support) 9701 return -ENODEV; 9702 /* valid values are [0, 99] */ 9703 if (value > 99) 9704 return -EINVAL; 9705 if (value > battery_info.batteries[battery].charge_stop) 9706 return -EINVAL; 9707 if (tpacpi_battery_set(THRESHOLD_START, battery, value)) 9708 return -ENODEV; 9709 battery_info.batteries[battery].charge_start = value; 9710 return count; 9711 9712 case THRESHOLD_STOP: 9713 if (!battery_info.batteries[battery].stop_support) 9714 return -ENODEV; 9715 /* valid values are [1, 100] */ 9716 if (value < 1 || value > 100) 9717 return -EINVAL; 9718 if (value < battery_info.batteries[battery].charge_start) 9719 return -EINVAL; 9720 battery_info.batteries[battery].charge_stop = value; 9721 /* 9722 * When 100 is passed to stop, we need to flip 9723 * it to 0 as that the EC understands that as 9724 * "Default", which will charge to 100% 9725 */ 9726 if (value == 100) 9727 value = 0; 9728 if (tpacpi_battery_set(THRESHOLD_STOP, battery, value)) 9729 return -EINVAL; 9730 return count; 9731 default: 9732 pr_crit("Wrong parameter: %d", what); 9733 return -EINVAL; 9734 } 9735 return count; 9736 } 9737 9738 static ssize_t tpacpi_battery_show(int what, 9739 struct device *dev, 9740 char *buf) 9741 { 9742 struct power_supply *supply = to_power_supply(dev); 9743 int ret, battery; 9744 /* 9745 * Some systems have support for more than 9746 * one battery. If that is the case, 9747 * tpacpi_battery_probe marked that addressing 9748 * them individually is supported, so we; 9749 * based on the device struct. 9750 * 9751 * On systems that are not supported, we assume 9752 * the primary as most of the ACPI calls fail 9753 * with "Any Battery" as the parameter. 9754 */ 9755 if (battery_info.individual_addressing) 9756 /* BAT_PRIMARY or BAT_SECONDARY */ 9757 battery = tpacpi_battery_get_id(supply->desc->name); 9758 else 9759 battery = BAT_PRIMARY; 9760 if (tpacpi_battery_get(what, battery, &ret)) 9761 return -ENODEV; 9762 return sprintf(buf, "%d\n", ret); 9763 } 9764 9765 static ssize_t charge_control_start_threshold_show(struct device *device, 9766 struct device_attribute *attr, 9767 char *buf) 9768 { 9769 return tpacpi_battery_show(THRESHOLD_START, device, buf); 9770 } 9771 9772 static ssize_t charge_control_end_threshold_show(struct device *device, 9773 struct device_attribute *attr, 9774 char *buf) 9775 { 9776 return tpacpi_battery_show(THRESHOLD_STOP, device, buf); 9777 } 9778 9779 static ssize_t charge_behaviour_show(struct device *dev, 9780 struct device_attribute *attr, 9781 char *buf) 9782 { 9783 enum power_supply_charge_behaviour active = POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO; 9784 struct power_supply *supply = to_power_supply(dev); 9785 unsigned int available; 9786 int ret, battery; 9787 9788 battery = tpacpi_battery_get_id(supply->desc->name); 9789 available = battery_info.batteries[battery].charge_behaviours; 9790 9791 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE)) { 9792 if (tpacpi_battery_get(FORCE_DISCHARGE, battery, &ret)) 9793 return -ENODEV; 9794 if (ret) { 9795 active = POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE; 9796 goto out; 9797 } 9798 } 9799 9800 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE)) { 9801 if (tpacpi_battery_get(INHIBIT_CHARGE, battery, &ret)) 9802 return -ENODEV; 9803 if (ret) { 9804 active = POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE; 9805 goto out; 9806 } 9807 } 9808 9809 out: 9810 return power_supply_charge_behaviour_show(dev, available, active, buf); 9811 } 9812 9813 static ssize_t charge_control_start_threshold_store(struct device *dev, 9814 struct device_attribute *attr, 9815 const char *buf, size_t count) 9816 { 9817 return tpacpi_battery_store(THRESHOLD_START, dev, buf, count); 9818 } 9819 9820 static ssize_t charge_control_end_threshold_store(struct device *dev, 9821 struct device_attribute *attr, 9822 const char *buf, size_t count) 9823 { 9824 return tpacpi_battery_store(THRESHOLD_STOP, dev, buf, count); 9825 } 9826 9827 static ssize_t charge_behaviour_store(struct device *dev, 9828 struct device_attribute *attr, 9829 const char *buf, size_t count) 9830 { 9831 struct power_supply *supply = to_power_supply(dev); 9832 int selected, battery, ret = 0; 9833 unsigned int available; 9834 9835 battery = tpacpi_battery_get_id(supply->desc->name); 9836 available = battery_info.batteries[battery].charge_behaviours; 9837 selected = power_supply_charge_behaviour_parse(available, buf); 9838 9839 if (selected < 0) 9840 return selected; 9841 9842 switch (selected) { 9843 case POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO: 9844 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE)) 9845 ret = tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 0); 9846 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE)) 9847 ret = min(ret, tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 0)); 9848 if (ret < 0) 9849 return ret; 9850 break; 9851 case POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE: 9852 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE)) 9853 ret = tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 0); 9854 ret = min(ret, tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 1)); 9855 if (ret < 0) 9856 return ret; 9857 break; 9858 case POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE: 9859 if (available & BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE)) 9860 ret = tpacpi_battery_set_validate(FORCE_DISCHARGE, battery, 0); 9861 ret = min(ret, tpacpi_battery_set_validate(INHIBIT_CHARGE, battery, 1)); 9862 if (ret < 0) 9863 return ret; 9864 break; 9865 default: 9866 dev_err(dev, "Unexpected charge behaviour: %d\n", selected); 9867 return -EINVAL; 9868 } 9869 9870 return count; 9871 } 9872 9873 static DEVICE_ATTR_RW(charge_control_start_threshold); 9874 static DEVICE_ATTR_RW(charge_control_end_threshold); 9875 static DEVICE_ATTR_RW(charge_behaviour); 9876 static struct device_attribute dev_attr_charge_start_threshold = __ATTR( 9877 charge_start_threshold, 9878 0644, 9879 charge_control_start_threshold_show, 9880 charge_control_start_threshold_store 9881 ); 9882 static struct device_attribute dev_attr_charge_stop_threshold = __ATTR( 9883 charge_stop_threshold, 9884 0644, 9885 charge_control_end_threshold_show, 9886 charge_control_end_threshold_store 9887 ); 9888 9889 static struct attribute *tpacpi_battery_attrs[] = { 9890 &dev_attr_charge_control_start_threshold.attr, 9891 &dev_attr_charge_control_end_threshold.attr, 9892 &dev_attr_charge_start_threshold.attr, 9893 &dev_attr_charge_stop_threshold.attr, 9894 &dev_attr_charge_behaviour.attr, 9895 NULL, 9896 }; 9897 9898 ATTRIBUTE_GROUPS(tpacpi_battery); 9899 9900 /* ACPI battery hooking */ 9901 9902 static int tpacpi_battery_add(struct power_supply *battery) 9903 { 9904 int batteryid = tpacpi_battery_get_id(battery->desc->name); 9905 9906 if (tpacpi_battery_probe(batteryid)) 9907 return -ENODEV; 9908 if (device_add_groups(&battery->dev, tpacpi_battery_groups)) 9909 return -ENODEV; 9910 return 0; 9911 } 9912 9913 static int tpacpi_battery_remove(struct power_supply *battery) 9914 { 9915 device_remove_groups(&battery->dev, tpacpi_battery_groups); 9916 return 0; 9917 } 9918 9919 static struct acpi_battery_hook battery_hook = { 9920 .add_battery = tpacpi_battery_add, 9921 .remove_battery = tpacpi_battery_remove, 9922 .name = "ThinkPad Battery Extension", 9923 }; 9924 9925 /* Subdriver init/exit */ 9926 9927 static const struct tpacpi_quirk battery_quirk_table[] __initconst = { 9928 /* 9929 * Individual addressing is broken on models that expose the 9930 * primary battery as BAT1. 9931 */ 9932 TPACPI_Q_LNV('J', '7', true), /* B5400 */ 9933 TPACPI_Q_LNV('J', 'I', true), /* Thinkpad 11e */ 9934 TPACPI_Q_LNV3('R', '0', 'B', true), /* Thinkpad 11e gen 3 */ 9935 TPACPI_Q_LNV3('R', '0', 'C', true), /* Thinkpad 13 */ 9936 TPACPI_Q_LNV3('R', '0', 'J', true), /* Thinkpad 13 gen 2 */ 9937 TPACPI_Q_LNV3('R', '0', 'K', true), /* Thinkpad 11e gen 4 celeron BIOS */ 9938 }; 9939 9940 static int __init tpacpi_battery_init(struct ibm_init_struct *ibm) 9941 { 9942 memset(&battery_info, 0, sizeof(battery_info)); 9943 9944 tp_features.battery_force_primary = tpacpi_check_quirks( 9945 battery_quirk_table, 9946 ARRAY_SIZE(battery_quirk_table)); 9947 9948 battery_hook_register(&battery_hook); 9949 return 0; 9950 } 9951 9952 static void tpacpi_battery_exit(void) 9953 { 9954 battery_hook_unregister(&battery_hook); 9955 } 9956 9957 static struct ibm_struct battery_driver_data = { 9958 .name = "battery", 9959 .exit = tpacpi_battery_exit, 9960 }; 9961 9962 /************************************************************************* 9963 * LCD Shadow subdriver, for the Lenovo PrivacyGuard feature 9964 */ 9965 9966 static struct drm_privacy_screen *lcdshadow_dev; 9967 static acpi_handle lcdshadow_get_handle; 9968 static acpi_handle lcdshadow_set_handle; 9969 9970 static int lcdshadow_set_sw_state(struct drm_privacy_screen *priv, 9971 enum drm_privacy_screen_status state) 9972 { 9973 int output; 9974 9975 if (WARN_ON(!mutex_is_locked(&priv->lock))) 9976 return -EIO; 9977 9978 if (!acpi_evalf(lcdshadow_set_handle, &output, NULL, "dd", (int)state)) 9979 return -EIO; 9980 9981 priv->hw_state = priv->sw_state = state; 9982 return 0; 9983 } 9984 9985 static void lcdshadow_get_hw_state(struct drm_privacy_screen *priv) 9986 { 9987 int output; 9988 9989 if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0)) 9990 return; 9991 9992 priv->hw_state = priv->sw_state = output & 0x1; 9993 } 9994 9995 static const struct drm_privacy_screen_ops lcdshadow_ops = { 9996 .set_sw_state = lcdshadow_set_sw_state, 9997 .get_hw_state = lcdshadow_get_hw_state, 9998 }; 9999 10000 static int tpacpi_lcdshadow_init(struct ibm_init_struct *iibm) 10001 { 10002 acpi_status status1, status2; 10003 int output; 10004 10005 status1 = acpi_get_handle(hkey_handle, "GSSS", &lcdshadow_get_handle); 10006 status2 = acpi_get_handle(hkey_handle, "SSSS", &lcdshadow_set_handle); 10007 if (ACPI_FAILURE(status1) || ACPI_FAILURE(status2)) 10008 return 0; 10009 10010 if (!acpi_evalf(lcdshadow_get_handle, &output, NULL, "dd", 0)) 10011 return -EIO; 10012 10013 if (!(output & 0x10000)) 10014 return 0; 10015 10016 lcdshadow_dev = drm_privacy_screen_register(&tpacpi_pdev->dev, 10017 &lcdshadow_ops, NULL); 10018 if (IS_ERR(lcdshadow_dev)) 10019 return PTR_ERR(lcdshadow_dev); 10020 10021 return 0; 10022 } 10023 10024 static void lcdshadow_exit(void) 10025 { 10026 drm_privacy_screen_unregister(lcdshadow_dev); 10027 } 10028 10029 static void lcdshadow_resume(void) 10030 { 10031 if (!lcdshadow_dev) 10032 return; 10033 10034 mutex_lock(&lcdshadow_dev->lock); 10035 lcdshadow_set_sw_state(lcdshadow_dev, lcdshadow_dev->sw_state); 10036 mutex_unlock(&lcdshadow_dev->lock); 10037 } 10038 10039 static int lcdshadow_read(struct seq_file *m) 10040 { 10041 if (!lcdshadow_dev) { 10042 seq_puts(m, "status:\t\tnot supported\n"); 10043 } else { 10044 seq_printf(m, "status:\t\t%d\n", lcdshadow_dev->hw_state); 10045 seq_puts(m, "commands:\t0, 1\n"); 10046 } 10047 10048 return 0; 10049 } 10050 10051 static int lcdshadow_write(char *buf) 10052 { 10053 char *cmd; 10054 int res, state = -EINVAL; 10055 10056 if (!lcdshadow_dev) 10057 return -ENODEV; 10058 10059 while ((cmd = strsep(&buf, ","))) { 10060 res = kstrtoint(cmd, 10, &state); 10061 if (res < 0) 10062 return res; 10063 } 10064 10065 if (state >= 2 || state < 0) 10066 return -EINVAL; 10067 10068 mutex_lock(&lcdshadow_dev->lock); 10069 res = lcdshadow_set_sw_state(lcdshadow_dev, state); 10070 mutex_unlock(&lcdshadow_dev->lock); 10071 10072 drm_privacy_screen_call_notifier_chain(lcdshadow_dev); 10073 10074 return res; 10075 } 10076 10077 static struct ibm_struct lcdshadow_driver_data = { 10078 .name = "lcdshadow", 10079 .exit = lcdshadow_exit, 10080 .resume = lcdshadow_resume, 10081 .read = lcdshadow_read, 10082 .write = lcdshadow_write, 10083 }; 10084 10085 /************************************************************************* 10086 * Thinkpad sensor interfaces 10087 */ 10088 10089 #define DYTC_CMD_QUERY 0 /* To get DYTC status - enable/revision */ 10090 #define DYTC_QUERY_ENABLE_BIT 8 /* Bit 8 - 0 = disabled, 1 = enabled */ 10091 #define DYTC_QUERY_SUBREV_BIT 16 /* Bits 16 - 27 - sub revision */ 10092 #define DYTC_QUERY_REV_BIT 28 /* Bits 28 - 31 - revision */ 10093 10094 #define DYTC_CMD_GET 2 /* To get current IC function and mode */ 10095 #define DYTC_GET_LAPMODE_BIT 17 /* Set when in lapmode */ 10096 10097 #define PALMSENSOR_PRESENT_BIT 0 /* Determine if psensor present */ 10098 #define PALMSENSOR_ON_BIT 1 /* psensor status */ 10099 10100 static bool has_palmsensor; 10101 static bool has_lapsensor; 10102 static bool palm_state; 10103 static bool lap_state; 10104 static int dytc_version; 10105 10106 static int dytc_command(int command, int *output) 10107 { 10108 acpi_handle dytc_handle; 10109 10110 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DYTC", &dytc_handle))) { 10111 /* Platform doesn't support DYTC */ 10112 return -ENODEV; 10113 } 10114 if (!acpi_evalf(dytc_handle, output, NULL, "dd", command)) 10115 return -EIO; 10116 return 0; 10117 } 10118 10119 static int lapsensor_get(bool *present, bool *state) 10120 { 10121 int output, err; 10122 10123 *present = false; 10124 err = dytc_command(DYTC_CMD_GET, &output); 10125 if (err) 10126 return err; 10127 10128 *present = true; /*If we get his far, we have lapmode support*/ 10129 *state = output & BIT(DYTC_GET_LAPMODE_BIT) ? true : false; 10130 return 0; 10131 } 10132 10133 static int palmsensor_get(bool *present, bool *state) 10134 { 10135 acpi_handle psensor_handle; 10136 int output; 10137 10138 *present = false; 10139 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GPSS", &psensor_handle))) 10140 return -ENODEV; 10141 if (!acpi_evalf(psensor_handle, &output, NULL, "d")) 10142 return -EIO; 10143 10144 *present = output & BIT(PALMSENSOR_PRESENT_BIT) ? true : false; 10145 *state = output & BIT(PALMSENSOR_ON_BIT) ? true : false; 10146 return 0; 10147 } 10148 10149 static void lapsensor_refresh(void) 10150 { 10151 bool state; 10152 int err; 10153 10154 if (has_lapsensor) { 10155 err = lapsensor_get(&has_lapsensor, &state); 10156 if (err) 10157 return; 10158 if (lap_state != state) { 10159 lap_state = state; 10160 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "dytc_lapmode"); 10161 } 10162 } 10163 } 10164 10165 static void palmsensor_refresh(void) 10166 { 10167 bool state; 10168 int err; 10169 10170 if (has_palmsensor) { 10171 err = palmsensor_get(&has_palmsensor, &state); 10172 if (err) 10173 return; 10174 if (palm_state != state) { 10175 palm_state = state; 10176 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "palmsensor"); 10177 } 10178 } 10179 } 10180 10181 static ssize_t dytc_lapmode_show(struct device *dev, 10182 struct device_attribute *attr, 10183 char *buf) 10184 { 10185 if (has_lapsensor) 10186 return sysfs_emit(buf, "%d\n", lap_state); 10187 return sysfs_emit(buf, "\n"); 10188 } 10189 static DEVICE_ATTR_RO(dytc_lapmode); 10190 10191 static ssize_t palmsensor_show(struct device *dev, 10192 struct device_attribute *attr, 10193 char *buf) 10194 { 10195 if (has_palmsensor) 10196 return sysfs_emit(buf, "%d\n", palm_state); 10197 return sysfs_emit(buf, "\n"); 10198 } 10199 static DEVICE_ATTR_RO(palmsensor); 10200 10201 static struct attribute *proxsensor_attributes[] = { 10202 &dev_attr_dytc_lapmode.attr, 10203 &dev_attr_palmsensor.attr, 10204 NULL 10205 }; 10206 10207 static umode_t proxsensor_attr_is_visible(struct kobject *kobj, 10208 struct attribute *attr, int n) 10209 { 10210 if (attr == &dev_attr_dytc_lapmode.attr) { 10211 /* 10212 * Platforms before DYTC version 5 claim to have a lap sensor, 10213 * but it doesn't work, so we ignore them. 10214 */ 10215 if (!has_lapsensor || dytc_version < 5) 10216 return 0; 10217 } else if (attr == &dev_attr_palmsensor.attr) { 10218 if (!has_palmsensor) 10219 return 0; 10220 } 10221 10222 return attr->mode; 10223 } 10224 10225 static const struct attribute_group proxsensor_attr_group = { 10226 .is_visible = proxsensor_attr_is_visible, 10227 .attrs = proxsensor_attributes, 10228 }; 10229 10230 static int tpacpi_proxsensor_init(struct ibm_init_struct *iibm) 10231 { 10232 int palm_err, lap_err; 10233 10234 palm_err = palmsensor_get(&has_palmsensor, &palm_state); 10235 lap_err = lapsensor_get(&has_lapsensor, &lap_state); 10236 /* If support isn't available for both devices return -ENODEV */ 10237 if ((palm_err == -ENODEV) && (lap_err == -ENODEV)) 10238 return -ENODEV; 10239 /* Otherwise, if there was an error return it */ 10240 if (palm_err && (palm_err != -ENODEV)) 10241 return palm_err; 10242 if (lap_err && (lap_err != -ENODEV)) 10243 return lap_err; 10244 10245 return 0; 10246 } 10247 10248 static struct ibm_struct proxsensor_driver_data = { 10249 .name = "proximity-sensor", 10250 }; 10251 10252 /************************************************************************* 10253 * DYTC Platform Profile interface 10254 */ 10255 10256 #define DYTC_CMD_SET 1 /* To enable/disable IC function mode */ 10257 #define DYTC_CMD_MMC_GET 8 /* To get current MMC function and mode */ 10258 #define DYTC_CMD_RESET 0x1ff /* To reset back to default */ 10259 10260 #define DYTC_CMD_FUNC_CAP 3 /* To get DYTC capabilities */ 10261 #define DYTC_FC_MMC 27 /* MMC Mode supported */ 10262 #define DYTC_FC_PSC 29 /* PSC Mode supported */ 10263 #define DYTC_FC_AMT 31 /* AMT mode supported */ 10264 10265 #define DYTC_GET_FUNCTION_BIT 8 /* Bits 8-11 - function setting */ 10266 #define DYTC_GET_MODE_BIT 12 /* Bits 12-15 - mode setting */ 10267 10268 #define DYTC_SET_FUNCTION_BIT 12 /* Bits 12-15 - function setting */ 10269 #define DYTC_SET_MODE_BIT 16 /* Bits 16-19 - mode setting */ 10270 #define DYTC_SET_VALID_BIT 20 /* Bit 20 - 1 = on, 0 = off */ 10271 10272 #define DYTC_FUNCTION_STD 0 /* Function = 0, standard mode */ 10273 #define DYTC_FUNCTION_CQL 1 /* Function = 1, lap mode */ 10274 #define DYTC_FUNCTION_MMC 11 /* Function = 11, MMC mode */ 10275 #define DYTC_FUNCTION_PSC 13 /* Function = 13, PSC mode */ 10276 #define DYTC_FUNCTION_AMT 15 /* Function = 15, AMT mode */ 10277 10278 #define DYTC_MODE_AMT_ENABLE 0x1 /* Enable AMT (in balanced mode) */ 10279 #define DYTC_MODE_AMT_DISABLE 0xF /* Disable AMT (in other modes) */ 10280 10281 #define DYTC_MODE_MMC_PERFORM 2 /* High power mode aka performance */ 10282 #define DYTC_MODE_MMC_LOWPOWER 3 /* Low power mode */ 10283 #define DYTC_MODE_MMC_BALANCE 0xF /* Default mode aka balanced */ 10284 #define DYTC_MODE_MMC_DEFAULT 0 /* Default mode from MMC_GET, aka balanced */ 10285 10286 #define DYTC_MODE_PSC_LOWPOWER 3 /* Low power mode */ 10287 #define DYTC_MODE_PSC_BALANCE 5 /* Default mode aka balanced */ 10288 #define DYTC_MODE_PSC_PERFORM 7 /* High power mode aka performance */ 10289 10290 #define DYTC_ERR_MASK 0xF /* Bits 0-3 in cmd result are the error result */ 10291 #define DYTC_ERR_SUCCESS 1 /* CMD completed successful */ 10292 10293 #define DYTC_SET_COMMAND(function, mode, on) \ 10294 (DYTC_CMD_SET | (function) << DYTC_SET_FUNCTION_BIT | \ 10295 (mode) << DYTC_SET_MODE_BIT | \ 10296 (on) << DYTC_SET_VALID_BIT) 10297 10298 #define DYTC_DISABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_MMC_BALANCE, 0) 10299 #define DYTC_ENABLE_CQL DYTC_SET_COMMAND(DYTC_FUNCTION_CQL, DYTC_MODE_MMC_BALANCE, 1) 10300 static int dytc_control_amt(bool enable); 10301 static bool dytc_amt_active; 10302 10303 static enum platform_profile_option dytc_current_profile; 10304 static atomic_t dytc_ignore_event = ATOMIC_INIT(0); 10305 static DEFINE_MUTEX(dytc_mutex); 10306 static int dytc_capabilities; 10307 static bool dytc_mmc_get_available; 10308 10309 static int convert_dytc_to_profile(int dytcmode, enum platform_profile_option *profile) 10310 { 10311 if (dytc_capabilities & BIT(DYTC_FC_MMC)) { 10312 switch (dytcmode) { 10313 case DYTC_MODE_MMC_LOWPOWER: 10314 *profile = PLATFORM_PROFILE_LOW_POWER; 10315 break; 10316 case DYTC_MODE_MMC_DEFAULT: 10317 case DYTC_MODE_MMC_BALANCE: 10318 *profile = PLATFORM_PROFILE_BALANCED; 10319 break; 10320 case DYTC_MODE_MMC_PERFORM: 10321 *profile = PLATFORM_PROFILE_PERFORMANCE; 10322 break; 10323 default: /* Unknown mode */ 10324 return -EINVAL; 10325 } 10326 return 0; 10327 } 10328 if (dytc_capabilities & BIT(DYTC_FC_PSC)) { 10329 switch (dytcmode) { 10330 case DYTC_MODE_PSC_LOWPOWER: 10331 *profile = PLATFORM_PROFILE_LOW_POWER; 10332 break; 10333 case DYTC_MODE_PSC_BALANCE: 10334 *profile = PLATFORM_PROFILE_BALANCED; 10335 break; 10336 case DYTC_MODE_PSC_PERFORM: 10337 *profile = PLATFORM_PROFILE_PERFORMANCE; 10338 break; 10339 default: /* Unknown mode */ 10340 return -EINVAL; 10341 } 10342 } 10343 return 0; 10344 } 10345 10346 static int convert_profile_to_dytc(enum platform_profile_option profile, int *perfmode) 10347 { 10348 switch (profile) { 10349 case PLATFORM_PROFILE_LOW_POWER: 10350 if (dytc_capabilities & BIT(DYTC_FC_MMC)) 10351 *perfmode = DYTC_MODE_MMC_LOWPOWER; 10352 else if (dytc_capabilities & BIT(DYTC_FC_PSC)) 10353 *perfmode = DYTC_MODE_PSC_LOWPOWER; 10354 break; 10355 case PLATFORM_PROFILE_BALANCED: 10356 if (dytc_capabilities & BIT(DYTC_FC_MMC)) 10357 *perfmode = DYTC_MODE_MMC_BALANCE; 10358 else if (dytc_capabilities & BIT(DYTC_FC_PSC)) 10359 *perfmode = DYTC_MODE_PSC_BALANCE; 10360 break; 10361 case PLATFORM_PROFILE_PERFORMANCE: 10362 if (dytc_capabilities & BIT(DYTC_FC_MMC)) 10363 *perfmode = DYTC_MODE_MMC_PERFORM; 10364 else if (dytc_capabilities & BIT(DYTC_FC_PSC)) 10365 *perfmode = DYTC_MODE_PSC_PERFORM; 10366 break; 10367 default: /* Unknown profile */ 10368 return -EOPNOTSUPP; 10369 } 10370 return 0; 10371 } 10372 10373 /* 10374 * dytc_profile_get: Function to register with platform_profile 10375 * handler. Returns current platform profile. 10376 */ 10377 static int dytc_profile_get(struct platform_profile_handler *pprof, 10378 enum platform_profile_option *profile) 10379 { 10380 *profile = dytc_current_profile; 10381 return 0; 10382 } 10383 10384 static int dytc_control_amt(bool enable) 10385 { 10386 int dummy; 10387 int err; 10388 int cmd; 10389 10390 if (!(dytc_capabilities & BIT(DYTC_FC_AMT))) { 10391 pr_warn("Attempting to toggle AMT on a system that doesn't advertise support\n"); 10392 return -ENODEV; 10393 } 10394 10395 if (enable) 10396 cmd = DYTC_SET_COMMAND(DYTC_FUNCTION_AMT, DYTC_MODE_AMT_ENABLE, enable); 10397 else 10398 cmd = DYTC_SET_COMMAND(DYTC_FUNCTION_AMT, DYTC_MODE_AMT_DISABLE, enable); 10399 10400 pr_debug("%sabling AMT (cmd 0x%x)", enable ? "en":"dis", cmd); 10401 err = dytc_command(cmd, &dummy); 10402 if (err) 10403 return err; 10404 dytc_amt_active = enable; 10405 return 0; 10406 } 10407 10408 /* 10409 * Helper function - check if we are in CQL mode and if we are 10410 * - disable CQL, 10411 * - run the command 10412 * - enable CQL 10413 * If not in CQL mode, just run the command 10414 */ 10415 static int dytc_cql_command(int command, int *output) 10416 { 10417 int err, cmd_err, dummy; 10418 int cur_funcmode; 10419 10420 /* Determine if we are in CQL mode. This alters the commands we do */ 10421 err = dytc_command(DYTC_CMD_GET, output); 10422 if (err) 10423 return err; 10424 10425 cur_funcmode = (*output >> DYTC_GET_FUNCTION_BIT) & 0xF; 10426 /* Check if we're OK to return immediately */ 10427 if ((command == DYTC_CMD_GET) && (cur_funcmode != DYTC_FUNCTION_CQL)) 10428 return 0; 10429 10430 if (cur_funcmode == DYTC_FUNCTION_CQL) { 10431 atomic_inc(&dytc_ignore_event); 10432 err = dytc_command(DYTC_DISABLE_CQL, &dummy); 10433 if (err) 10434 return err; 10435 } 10436 10437 cmd_err = dytc_command(command, output); 10438 /* Check return condition after we've restored CQL state */ 10439 10440 if (cur_funcmode == DYTC_FUNCTION_CQL) { 10441 err = dytc_command(DYTC_ENABLE_CQL, &dummy); 10442 if (err) 10443 return err; 10444 } 10445 return cmd_err; 10446 } 10447 10448 /* 10449 * dytc_profile_set: Function to register with platform_profile 10450 * handler. Sets current platform profile. 10451 */ 10452 static int dytc_profile_set(struct platform_profile_handler *pprof, 10453 enum platform_profile_option profile) 10454 { 10455 int perfmode; 10456 int output; 10457 int err; 10458 10459 err = mutex_lock_interruptible(&dytc_mutex); 10460 if (err) 10461 return err; 10462 10463 err = convert_profile_to_dytc(profile, &perfmode); 10464 if (err) 10465 goto unlock; 10466 10467 if (dytc_capabilities & BIT(DYTC_FC_MMC)) { 10468 if (profile == PLATFORM_PROFILE_BALANCED) { 10469 /* 10470 * To get back to balanced mode we need to issue a reset command. 10471 * Note we still need to disable CQL mode before hand and re-enable 10472 * it afterwards, otherwise dytc_lapmode gets reset to 0 and stays 10473 * stuck at 0 for aprox. 30 minutes. 10474 */ 10475 err = dytc_cql_command(DYTC_CMD_RESET, &output); 10476 if (err) 10477 goto unlock; 10478 } else { 10479 /* Determine if we are in CQL mode. This alters the commands we do */ 10480 err = dytc_cql_command(DYTC_SET_COMMAND(DYTC_FUNCTION_MMC, perfmode, 1), 10481 &output); 10482 if (err) 10483 goto unlock; 10484 } 10485 } 10486 if (dytc_capabilities & BIT(DYTC_FC_PSC)) { 10487 err = dytc_command(DYTC_SET_COMMAND(DYTC_FUNCTION_PSC, perfmode, 1), &output); 10488 if (err) 10489 goto unlock; 10490 /* system supports AMT, activate it when on balanced */ 10491 if (dytc_capabilities & BIT(DYTC_FC_AMT)) 10492 dytc_control_amt(profile == PLATFORM_PROFILE_BALANCED); 10493 } 10494 /* Success - update current profile */ 10495 dytc_current_profile = profile; 10496 unlock: 10497 mutex_unlock(&dytc_mutex); 10498 return err; 10499 } 10500 10501 static void dytc_profile_refresh(void) 10502 { 10503 enum platform_profile_option profile; 10504 int output, err = 0; 10505 int perfmode; 10506 10507 mutex_lock(&dytc_mutex); 10508 if (dytc_capabilities & BIT(DYTC_FC_MMC)) { 10509 if (dytc_mmc_get_available) 10510 err = dytc_command(DYTC_CMD_MMC_GET, &output); 10511 else 10512 err = dytc_cql_command(DYTC_CMD_GET, &output); 10513 } else if (dytc_capabilities & BIT(DYTC_FC_PSC)) 10514 err = dytc_command(DYTC_CMD_GET, &output); 10515 10516 mutex_unlock(&dytc_mutex); 10517 if (err) 10518 return; 10519 10520 perfmode = (output >> DYTC_GET_MODE_BIT) & 0xF; 10521 convert_dytc_to_profile(perfmode, &profile); 10522 if (profile != dytc_current_profile) { 10523 dytc_current_profile = profile; 10524 platform_profile_notify(); 10525 } 10526 } 10527 10528 static struct platform_profile_handler dytc_profile = { 10529 .profile_get = dytc_profile_get, 10530 .profile_set = dytc_profile_set, 10531 }; 10532 10533 static int tpacpi_dytc_profile_init(struct ibm_init_struct *iibm) 10534 { 10535 int err, output; 10536 10537 /* Setup supported modes */ 10538 set_bit(PLATFORM_PROFILE_LOW_POWER, dytc_profile.choices); 10539 set_bit(PLATFORM_PROFILE_BALANCED, dytc_profile.choices); 10540 set_bit(PLATFORM_PROFILE_PERFORMANCE, dytc_profile.choices); 10541 10542 err = dytc_command(DYTC_CMD_QUERY, &output); 10543 if (err) 10544 return err; 10545 10546 if (output & BIT(DYTC_QUERY_ENABLE_BIT)) 10547 dytc_version = (output >> DYTC_QUERY_REV_BIT) & 0xF; 10548 10549 /* Check DYTC is enabled and supports mode setting */ 10550 if (dytc_version < 5) 10551 return -ENODEV; 10552 10553 /* Check what capabilities are supported */ 10554 err = dytc_command(DYTC_CMD_FUNC_CAP, &dytc_capabilities); 10555 if (err) 10556 return err; 10557 10558 if (dytc_capabilities & BIT(DYTC_FC_MMC)) { /* MMC MODE */ 10559 pr_debug("MMC is supported\n"); 10560 /* 10561 * Check if MMC_GET functionality available 10562 * Version > 6 and return success from MMC_GET command 10563 */ 10564 dytc_mmc_get_available = false; 10565 if (dytc_version >= 6) { 10566 err = dytc_command(DYTC_CMD_MMC_GET, &output); 10567 if (!err && ((output & DYTC_ERR_MASK) == DYTC_ERR_SUCCESS)) 10568 dytc_mmc_get_available = true; 10569 } 10570 } else if (dytc_capabilities & BIT(DYTC_FC_PSC)) { /* PSC MODE */ 10571 /* Support for this only works on AMD platforms */ 10572 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) { 10573 dbg_printk(TPACPI_DBG_INIT, "PSC not support on Intel platforms\n"); 10574 return -ENODEV; 10575 } 10576 pr_debug("PSC is supported\n"); 10577 } else { 10578 dbg_printk(TPACPI_DBG_INIT, "No DYTC support available\n"); 10579 return -ENODEV; 10580 } 10581 10582 dbg_printk(TPACPI_DBG_INIT, 10583 "DYTC version %d: thermal mode available\n", dytc_version); 10584 10585 /* Create platform_profile structure and register */ 10586 err = platform_profile_register(&dytc_profile); 10587 /* 10588 * If for some reason platform_profiles aren't enabled 10589 * don't quit terminally. 10590 */ 10591 if (err) 10592 return -ENODEV; 10593 10594 /* Ensure initial values are correct */ 10595 dytc_profile_refresh(); 10596 10597 /* Workaround for https://bugzilla.kernel.org/show_bug.cgi?id=216347 */ 10598 if (dytc_capabilities & BIT(DYTC_FC_PSC)) 10599 dytc_profile_set(NULL, PLATFORM_PROFILE_BALANCED); 10600 10601 return 0; 10602 } 10603 10604 static void dytc_profile_exit(void) 10605 { 10606 platform_profile_remove(); 10607 } 10608 10609 static struct ibm_struct dytc_profile_driver_data = { 10610 .name = "dytc-profile", 10611 .exit = dytc_profile_exit, 10612 }; 10613 10614 /************************************************************************* 10615 * Keyboard language interface 10616 */ 10617 10618 struct keyboard_lang_data { 10619 const char *lang_str; 10620 int lang_code; 10621 }; 10622 10623 static const struct keyboard_lang_data keyboard_lang_data[] = { 10624 {"be", 0x080c}, 10625 {"cz", 0x0405}, 10626 {"da", 0x0406}, 10627 {"de", 0x0c07}, 10628 {"en", 0x0000}, 10629 {"es", 0x2c0a}, 10630 {"et", 0x0425}, 10631 {"fr", 0x040c}, 10632 {"fr-ch", 0x100c}, 10633 {"hu", 0x040e}, 10634 {"it", 0x0410}, 10635 {"jp", 0x0411}, 10636 {"nl", 0x0413}, 10637 {"nn", 0x0414}, 10638 {"pl", 0x0415}, 10639 {"pt", 0x0816}, 10640 {"sl", 0x041b}, 10641 {"sv", 0x081d}, 10642 {"tr", 0x041f}, 10643 }; 10644 10645 static int set_keyboard_lang_command(int command) 10646 { 10647 acpi_handle sskl_handle; 10648 int output; 10649 10650 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "SSKL", &sskl_handle))) { 10651 /* Platform doesn't support SSKL */ 10652 return -ENODEV; 10653 } 10654 10655 if (!acpi_evalf(sskl_handle, &output, NULL, "dd", command)) 10656 return -EIO; 10657 10658 return 0; 10659 } 10660 10661 static int get_keyboard_lang(int *output) 10662 { 10663 acpi_handle gskl_handle; 10664 int kbd_lang; 10665 10666 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "GSKL", &gskl_handle))) { 10667 /* Platform doesn't support GSKL */ 10668 return -ENODEV; 10669 } 10670 10671 if (!acpi_evalf(gskl_handle, &kbd_lang, NULL, "dd", 0x02000000)) 10672 return -EIO; 10673 10674 /* 10675 * METHOD_ERR gets returned on devices where there are no special (e.g. '=', 10676 * '(' and ')') keys which use layout dependent key-press emulation. 10677 */ 10678 if (kbd_lang & METHOD_ERR) 10679 return -ENODEV; 10680 10681 *output = kbd_lang; 10682 10683 return 0; 10684 } 10685 10686 /* sysfs keyboard language entry */ 10687 static ssize_t keyboard_lang_show(struct device *dev, 10688 struct device_attribute *attr, 10689 char *buf) 10690 { 10691 int output, err, i, len = 0; 10692 10693 err = get_keyboard_lang(&output); 10694 if (err) 10695 return err; 10696 10697 for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) { 10698 if (i) 10699 len += sysfs_emit_at(buf, len, "%s", " "); 10700 10701 if (output == keyboard_lang_data[i].lang_code) { 10702 len += sysfs_emit_at(buf, len, "[%s]", keyboard_lang_data[i].lang_str); 10703 } else { 10704 len += sysfs_emit_at(buf, len, "%s", keyboard_lang_data[i].lang_str); 10705 } 10706 } 10707 len += sysfs_emit_at(buf, len, "\n"); 10708 10709 return len; 10710 } 10711 10712 static ssize_t keyboard_lang_store(struct device *dev, 10713 struct device_attribute *attr, 10714 const char *buf, size_t count) 10715 { 10716 int err, i; 10717 bool lang_found = false; 10718 int lang_code = 0; 10719 10720 for (i = 0; i < ARRAY_SIZE(keyboard_lang_data); i++) { 10721 if (sysfs_streq(buf, keyboard_lang_data[i].lang_str)) { 10722 lang_code = keyboard_lang_data[i].lang_code; 10723 lang_found = true; 10724 break; 10725 } 10726 } 10727 10728 if (lang_found) { 10729 lang_code = lang_code | 1 << 24; 10730 10731 /* Set language code */ 10732 err = set_keyboard_lang_command(lang_code); 10733 if (err) 10734 return err; 10735 } else { 10736 dev_err(&tpacpi_pdev->dev, "Unknown Keyboard language. Ignoring\n"); 10737 return -EINVAL; 10738 } 10739 10740 tpacpi_disclose_usertask(attr->attr.name, 10741 "keyboard language is set to %s\n", buf); 10742 10743 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL, "keyboard_lang"); 10744 10745 return count; 10746 } 10747 static DEVICE_ATTR_RW(keyboard_lang); 10748 10749 static struct attribute *kbdlang_attributes[] = { 10750 &dev_attr_keyboard_lang.attr, 10751 NULL 10752 }; 10753 10754 static umode_t kbdlang_attr_is_visible(struct kobject *kobj, 10755 struct attribute *attr, int n) 10756 { 10757 return tp_features.kbd_lang ? attr->mode : 0; 10758 } 10759 10760 static const struct attribute_group kbdlang_attr_group = { 10761 .is_visible = kbdlang_attr_is_visible, 10762 .attrs = kbdlang_attributes, 10763 }; 10764 10765 static int tpacpi_kbdlang_init(struct ibm_init_struct *iibm) 10766 { 10767 int err, output; 10768 10769 err = get_keyboard_lang(&output); 10770 tp_features.kbd_lang = !err; 10771 return err; 10772 } 10773 10774 static struct ibm_struct kbdlang_driver_data = { 10775 .name = "kbdlang", 10776 }; 10777 10778 /************************************************************************* 10779 * DPRC(Dynamic Power Reduction Control) subdriver, for the Lenovo WWAN 10780 * and WLAN feature. 10781 */ 10782 #define DPRC_GET_WWAN_ANTENNA_TYPE 0x40000 10783 #define DPRC_WWAN_ANTENNA_TYPE_A_BIT BIT(4) 10784 #define DPRC_WWAN_ANTENNA_TYPE_B_BIT BIT(8) 10785 static bool has_antennatype; 10786 static int wwan_antennatype; 10787 10788 static int dprc_command(int command, int *output) 10789 { 10790 acpi_handle dprc_handle; 10791 10792 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, "DPRC", &dprc_handle))) { 10793 /* Platform doesn't support DPRC */ 10794 return -ENODEV; 10795 } 10796 10797 if (!acpi_evalf(dprc_handle, output, NULL, "dd", command)) 10798 return -EIO; 10799 10800 /* 10801 * METHOD_ERR gets returned on devices where few commands are not supported 10802 * for example command to get WWAN Antenna type command is not supported on 10803 * some devices. 10804 */ 10805 if (*output & METHOD_ERR) 10806 return -ENODEV; 10807 10808 return 0; 10809 } 10810 10811 static int get_wwan_antenna(int *wwan_antennatype) 10812 { 10813 int output, err; 10814 10815 /* Get current Antenna type */ 10816 err = dprc_command(DPRC_GET_WWAN_ANTENNA_TYPE, &output); 10817 if (err) 10818 return err; 10819 10820 if (output & DPRC_WWAN_ANTENNA_TYPE_A_BIT) 10821 *wwan_antennatype = 1; 10822 else if (output & DPRC_WWAN_ANTENNA_TYPE_B_BIT) 10823 *wwan_antennatype = 2; 10824 else 10825 return -ENODEV; 10826 10827 return 0; 10828 } 10829 10830 /* sysfs wwan antenna type entry */ 10831 static ssize_t wwan_antenna_type_show(struct device *dev, 10832 struct device_attribute *attr, 10833 char *buf) 10834 { 10835 switch (wwan_antennatype) { 10836 case 1: 10837 return sysfs_emit(buf, "type a\n"); 10838 case 2: 10839 return sysfs_emit(buf, "type b\n"); 10840 default: 10841 return -ENODATA; 10842 } 10843 } 10844 static DEVICE_ATTR_RO(wwan_antenna_type); 10845 10846 static struct attribute *dprc_attributes[] = { 10847 &dev_attr_wwan_antenna_type.attr, 10848 NULL 10849 }; 10850 10851 static umode_t dprc_attr_is_visible(struct kobject *kobj, 10852 struct attribute *attr, int n) 10853 { 10854 return has_antennatype ? attr->mode : 0; 10855 } 10856 10857 static const struct attribute_group dprc_attr_group = { 10858 .is_visible = dprc_attr_is_visible, 10859 .attrs = dprc_attributes, 10860 }; 10861 10862 static int tpacpi_dprc_init(struct ibm_init_struct *iibm) 10863 { 10864 int err; 10865 10866 err = get_wwan_antenna(&wwan_antennatype); 10867 if (err) 10868 return err; 10869 10870 has_antennatype = true; 10871 return 0; 10872 } 10873 10874 static struct ibm_struct dprc_driver_data = { 10875 .name = "dprc", 10876 }; 10877 10878 /* --------------------------------------------------------------------- */ 10879 10880 static struct attribute *tpacpi_driver_attributes[] = { 10881 &driver_attr_debug_level.attr, 10882 &driver_attr_version.attr, 10883 &driver_attr_interface_version.attr, 10884 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 10885 &driver_attr_wlsw_emulstate.attr, 10886 &driver_attr_bluetooth_emulstate.attr, 10887 &driver_attr_wwan_emulstate.attr, 10888 &driver_attr_uwb_emulstate.attr, 10889 #endif 10890 NULL 10891 }; 10892 10893 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 10894 static umode_t tpacpi_attr_is_visible(struct kobject *kobj, 10895 struct attribute *attr, int n) 10896 { 10897 if (attr == &driver_attr_wlsw_emulstate.attr) { 10898 if (!dbg_wlswemul) 10899 return 0; 10900 } else if (attr == &driver_attr_bluetooth_emulstate.attr) { 10901 if (!dbg_bluetoothemul) 10902 return 0; 10903 } else if (attr == &driver_attr_wwan_emulstate.attr) { 10904 if (!dbg_wwanemul) 10905 return 0; 10906 } else if (attr == &driver_attr_uwb_emulstate.attr) { 10907 if (!dbg_uwbemul) 10908 return 0; 10909 } 10910 10911 return attr->mode; 10912 } 10913 #endif 10914 10915 static const struct attribute_group tpacpi_driver_attr_group = { 10916 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 10917 .is_visible = tpacpi_attr_is_visible, 10918 #endif 10919 .attrs = tpacpi_driver_attributes, 10920 }; 10921 10922 static const struct attribute_group *tpacpi_driver_groups[] = { 10923 &tpacpi_driver_attr_group, 10924 NULL, 10925 }; 10926 10927 static const struct attribute_group *tpacpi_groups[] = { 10928 &adaptive_kbd_attr_group, 10929 &hotkey_attr_group, 10930 &bluetooth_attr_group, 10931 &wan_attr_group, 10932 &cmos_attr_group, 10933 &proxsensor_attr_group, 10934 &kbdlang_attr_group, 10935 &dprc_attr_group, 10936 NULL, 10937 }; 10938 10939 static const struct attribute_group *tpacpi_hwmon_groups[] = { 10940 &thermal_attr_group, 10941 &temp_label_attr_group, 10942 &fan_attr_group, 10943 NULL, 10944 }; 10945 10946 static const struct attribute_group *tpacpi_hwmon_driver_groups[] = { 10947 &fan_driver_attr_group, 10948 NULL, 10949 }; 10950 10951 /**************************************************************************** 10952 **************************************************************************** 10953 * 10954 * Platform drivers 10955 * 10956 **************************************************************************** 10957 ****************************************************************************/ 10958 10959 static struct platform_driver tpacpi_pdriver = { 10960 .driver = { 10961 .name = TPACPI_DRVR_NAME, 10962 .pm = &tpacpi_pm, 10963 .groups = tpacpi_driver_groups, 10964 .dev_groups = tpacpi_groups, 10965 }, 10966 .shutdown = tpacpi_shutdown_handler, 10967 }; 10968 10969 static struct platform_driver tpacpi_hwmon_pdriver = { 10970 .driver = { 10971 .name = TPACPI_HWMON_DRVR_NAME, 10972 .groups = tpacpi_hwmon_driver_groups, 10973 }, 10974 }; 10975 10976 /**************************************************************************** 10977 **************************************************************************** 10978 * 10979 * Infrastructure 10980 * 10981 **************************************************************************** 10982 ****************************************************************************/ 10983 10984 /* 10985 * HKEY event callout for other subdrivers go here 10986 * (yes, it is ugly, but it is quick, safe, and gets the job done 10987 */ 10988 static void tpacpi_driver_event(const unsigned int hkey_event) 10989 { 10990 if (ibm_backlight_device) { 10991 switch (hkey_event) { 10992 case TP_HKEY_EV_BRGHT_UP: 10993 case TP_HKEY_EV_BRGHT_DOWN: 10994 tpacpi_brightness_notify_change(); 10995 } 10996 } 10997 if (alsa_card) { 10998 switch (hkey_event) { 10999 case TP_HKEY_EV_VOL_UP: 11000 case TP_HKEY_EV_VOL_DOWN: 11001 case TP_HKEY_EV_VOL_MUTE: 11002 volume_alsa_notify_change(); 11003 } 11004 } 11005 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) { 11006 enum led_brightness brightness; 11007 11008 mutex_lock(&kbdlight_mutex); 11009 11010 /* 11011 * Check the brightness actually changed, setting the brightness 11012 * through kbdlight_set_level() also triggers this event. 11013 */ 11014 brightness = kbdlight_sysfs_get(NULL); 11015 if (kbdlight_brightness != brightness) { 11016 kbdlight_brightness = brightness; 11017 led_classdev_notify_brightness_hw_changed( 11018 &tpacpi_led_kbdlight.led_classdev, brightness); 11019 } 11020 11021 mutex_unlock(&kbdlight_mutex); 11022 } 11023 11024 if (hkey_event == TP_HKEY_EV_THM_CSM_COMPLETED) { 11025 lapsensor_refresh(); 11026 /* If we are already accessing DYTC then skip dytc update */ 11027 if (!atomic_add_unless(&dytc_ignore_event, -1, 0)) 11028 dytc_profile_refresh(); 11029 } 11030 11031 if (lcdshadow_dev && hkey_event == TP_HKEY_EV_PRIVACYGUARD_TOGGLE) { 11032 enum drm_privacy_screen_status old_hw_state; 11033 bool changed; 11034 11035 mutex_lock(&lcdshadow_dev->lock); 11036 old_hw_state = lcdshadow_dev->hw_state; 11037 lcdshadow_get_hw_state(lcdshadow_dev); 11038 changed = lcdshadow_dev->hw_state != old_hw_state; 11039 mutex_unlock(&lcdshadow_dev->lock); 11040 11041 if (changed) 11042 drm_privacy_screen_call_notifier_chain(lcdshadow_dev); 11043 } 11044 if (hkey_event == TP_HKEY_EV_AMT_TOGGLE) { 11045 /* If we're enabling AMT we need to force balanced mode */ 11046 if (!dytc_amt_active) 11047 /* This will also set AMT mode enabled */ 11048 dytc_profile_set(NULL, PLATFORM_PROFILE_BALANCED); 11049 else 11050 dytc_control_amt(!dytc_amt_active); 11051 } 11052 11053 } 11054 11055 static void hotkey_driver_event(const unsigned int scancode) 11056 { 11057 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode); 11058 } 11059 11060 /* --------------------------------------------------------------------- */ 11061 11062 /* /proc support */ 11063 static struct proc_dir_entry *proc_dir; 11064 11065 /* 11066 * Module and infrastructure proble, init and exit handling 11067 */ 11068 11069 static bool force_load; 11070 11071 #ifdef CONFIG_THINKPAD_ACPI_DEBUG 11072 static const char * __init str_supported(int is_supported) 11073 { 11074 static char text_unsupported[] __initdata = "not supported"; 11075 11076 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0]; 11077 } 11078 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */ 11079 11080 static void ibm_exit(struct ibm_struct *ibm) 11081 { 11082 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name); 11083 11084 list_del_init(&ibm->all_drivers); 11085 11086 if (ibm->flags.acpi_notify_installed) { 11087 dbg_printk(TPACPI_DBG_EXIT, 11088 "%s: acpi_remove_notify_handler\n", ibm->name); 11089 BUG_ON(!ibm->acpi); 11090 acpi_remove_notify_handler(*ibm->acpi->handle, 11091 ibm->acpi->type, 11092 dispatch_acpi_notify); 11093 ibm->flags.acpi_notify_installed = 0; 11094 } 11095 11096 if (ibm->flags.proc_created) { 11097 dbg_printk(TPACPI_DBG_EXIT, 11098 "%s: remove_proc_entry\n", ibm->name); 11099 remove_proc_entry(ibm->name, proc_dir); 11100 ibm->flags.proc_created = 0; 11101 } 11102 11103 if (ibm->flags.acpi_driver_registered) { 11104 dbg_printk(TPACPI_DBG_EXIT, 11105 "%s: acpi_bus_unregister_driver\n", ibm->name); 11106 BUG_ON(!ibm->acpi); 11107 acpi_bus_unregister_driver(ibm->acpi->driver); 11108 kfree(ibm->acpi->driver); 11109 ibm->acpi->driver = NULL; 11110 ibm->flags.acpi_driver_registered = 0; 11111 } 11112 11113 if (ibm->flags.init_called && ibm->exit) { 11114 ibm->exit(); 11115 ibm->flags.init_called = 0; 11116 } 11117 11118 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name); 11119 } 11120 11121 static int __init ibm_init(struct ibm_init_struct *iibm) 11122 { 11123 int ret; 11124 struct ibm_struct *ibm = iibm->data; 11125 struct proc_dir_entry *entry; 11126 11127 BUG_ON(ibm == NULL); 11128 11129 INIT_LIST_HEAD(&ibm->all_drivers); 11130 11131 if (ibm->flags.experimental && !experimental) 11132 return 0; 11133 11134 dbg_printk(TPACPI_DBG_INIT, 11135 "probing for %s\n", ibm->name); 11136 11137 if (iibm->init) { 11138 ret = iibm->init(iibm); 11139 if (ret > 0 || ret == -ENODEV) 11140 return 0; /* subdriver functionality not available */ 11141 if (ret) 11142 return ret; 11143 11144 ibm->flags.init_called = 1; 11145 } 11146 11147 if (ibm->acpi) { 11148 if (ibm->acpi->hid) { 11149 ret = register_tpacpi_subdriver(ibm); 11150 if (ret) 11151 goto err_out; 11152 } 11153 11154 if (ibm->acpi->notify) { 11155 ret = setup_acpi_notify(ibm); 11156 if (ret == -ENODEV) { 11157 pr_notice("disabling subdriver %s\n", 11158 ibm->name); 11159 ret = 0; 11160 goto err_out; 11161 } 11162 if (ret < 0) 11163 goto err_out; 11164 } 11165 } 11166 11167 dbg_printk(TPACPI_DBG_INIT, 11168 "%s installed\n", ibm->name); 11169 11170 if (ibm->read) { 11171 umode_t mode = iibm->base_procfs_mode; 11172 11173 if (!mode) 11174 mode = S_IRUGO; 11175 if (ibm->write) 11176 mode |= S_IWUSR; 11177 entry = proc_create_data(ibm->name, mode, proc_dir, 11178 &dispatch_proc_ops, ibm); 11179 if (!entry) { 11180 pr_err("unable to create proc entry %s\n", ibm->name); 11181 ret = -ENODEV; 11182 goto err_out; 11183 } 11184 ibm->flags.proc_created = 1; 11185 } 11186 11187 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers); 11188 11189 return 0; 11190 11191 err_out: 11192 dbg_printk(TPACPI_DBG_INIT, 11193 "%s: at error exit path with result %d\n", 11194 ibm->name, ret); 11195 11196 ibm_exit(ibm); 11197 return (ret < 0) ? ret : 0; 11198 } 11199 11200 /* Probing */ 11201 11202 static char __init tpacpi_parse_fw_id(const char * const s, 11203 u32 *model, u16 *release) 11204 { 11205 int i; 11206 11207 if (!s || strlen(s) < 8) 11208 goto invalid; 11209 11210 for (i = 0; i < 8; i++) 11211 if (!((s[i] >= '0' && s[i] <= '9') || 11212 (s[i] >= 'A' && s[i] <= 'Z'))) 11213 goto invalid; 11214 11215 /* 11216 * Most models: xxyTkkWW (#.##c) 11217 * Ancient 570/600 and -SL lacks (#.##c) 11218 */ 11219 if (s[3] == 'T' || s[3] == 'N') { 11220 *model = TPID(s[0], s[1]); 11221 *release = TPVER(s[4], s[5]); 11222 return s[2]; 11223 11224 /* New models: xxxyTkkW (#.##c); T550 and some others */ 11225 } else if (s[4] == 'T' || s[4] == 'N') { 11226 *model = TPID3(s[0], s[1], s[2]); 11227 *release = TPVER(s[5], s[6]); 11228 return s[3]; 11229 } 11230 11231 invalid: 11232 return '\0'; 11233 } 11234 11235 static void find_new_ec_fwstr(const struct dmi_header *dm, void *private) 11236 { 11237 char *ec_fw_string = (char *) private; 11238 const char *dmi_data = (const char *)dm; 11239 /* 11240 * ThinkPad Embedded Controller Program Table on newer models 11241 * 11242 * Offset | Name | Width | Description 11243 * ---------------------------------------------------- 11244 * 0x00 | Type | BYTE | 0x8C 11245 * 0x01 | Length | BYTE | 11246 * 0x02 | Handle | WORD | Varies 11247 * 0x04 | Signature | BYTEx6 | ASCII for "LENOVO" 11248 * 0x0A | OEM struct offset | BYTE | 0x0B 11249 * 0x0B | OEM struct number | BYTE | 0x07, for this structure 11250 * 0x0C | OEM struct revision | BYTE | 0x01, for this format 11251 * 0x0D | ECP version ID | STR ID | 11252 * 0x0E | ECP release date | STR ID | 11253 */ 11254 11255 /* Return if data structure not match */ 11256 if (dm->type != 140 || dm->length < 0x0F || 11257 memcmp(dmi_data + 4, "LENOVO", 6) != 0 || 11258 dmi_data[0x0A] != 0x0B || dmi_data[0x0B] != 0x07 || 11259 dmi_data[0x0C] != 0x01) 11260 return; 11261 11262 /* fwstr is the first 8byte string */ 11263 strncpy(ec_fw_string, dmi_data + 0x0F, 8); 11264 } 11265 11266 /* returns 0 - probe ok, or < 0 - probe error. 11267 * Probe ok doesn't mean thinkpad found. 11268 * On error, kfree() cleanup on tp->* is not performed, caller must do it */ 11269 static int __must_check __init get_thinkpad_model_data( 11270 struct thinkpad_id_data *tp) 11271 { 11272 const struct dmi_device *dev = NULL; 11273 char ec_fw_string[18] = {0}; 11274 char const *s; 11275 char t; 11276 11277 if (!tp) 11278 return -EINVAL; 11279 11280 memset(tp, 0, sizeof(*tp)); 11281 11282 if (dmi_name_in_vendors("IBM")) 11283 tp->vendor = PCI_VENDOR_ID_IBM; 11284 else if (dmi_name_in_vendors("LENOVO")) 11285 tp->vendor = PCI_VENDOR_ID_LENOVO; 11286 else 11287 return 0; 11288 11289 s = dmi_get_system_info(DMI_BIOS_VERSION); 11290 tp->bios_version_str = kstrdup(s, GFP_KERNEL); 11291 if (s && !tp->bios_version_str) 11292 return -ENOMEM; 11293 11294 /* Really ancient ThinkPad 240X will fail this, which is fine */ 11295 t = tpacpi_parse_fw_id(tp->bios_version_str, 11296 &tp->bios_model, &tp->bios_release); 11297 if (t != 'E' && t != 'C') 11298 return 0; 11299 11300 /* 11301 * ThinkPad T23 or newer, A31 or newer, R50e or newer, 11302 * X32 or newer, all Z series; Some models must have an 11303 * up-to-date BIOS or they will not be detected. 11304 * 11305 * See https://thinkwiki.org/wiki/List_of_DMI_IDs 11306 */ 11307 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) { 11308 if (sscanf(dev->name, 11309 "IBM ThinkPad Embedded Controller -[%17c", 11310 ec_fw_string) == 1) { 11311 ec_fw_string[sizeof(ec_fw_string) - 1] = 0; 11312 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0; 11313 break; 11314 } 11315 } 11316 11317 /* Newer ThinkPads have different EC program info table */ 11318 if (!ec_fw_string[0]) 11319 dmi_walk(find_new_ec_fwstr, &ec_fw_string); 11320 11321 if (ec_fw_string[0]) { 11322 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL); 11323 if (!tp->ec_version_str) 11324 return -ENOMEM; 11325 11326 t = tpacpi_parse_fw_id(ec_fw_string, 11327 &tp->ec_model, &tp->ec_release); 11328 if (t != 'H') { 11329 pr_notice("ThinkPad firmware release %s doesn't match the known patterns\n", 11330 ec_fw_string); 11331 pr_notice("please report this to %s\n", TPACPI_MAIL); 11332 } 11333 } 11334 11335 s = dmi_get_system_info(DMI_PRODUCT_VERSION); 11336 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) { 11337 tp->model_str = kstrdup(s, GFP_KERNEL); 11338 if (!tp->model_str) 11339 return -ENOMEM; 11340 } else { 11341 s = dmi_get_system_info(DMI_BIOS_VENDOR); 11342 if (s && !(strncasecmp(s, "Lenovo", 6))) { 11343 tp->model_str = kstrdup(s, GFP_KERNEL); 11344 if (!tp->model_str) 11345 return -ENOMEM; 11346 } 11347 } 11348 11349 s = dmi_get_system_info(DMI_PRODUCT_NAME); 11350 tp->nummodel_str = kstrdup(s, GFP_KERNEL); 11351 if (s && !tp->nummodel_str) 11352 return -ENOMEM; 11353 11354 return 0; 11355 } 11356 11357 static int __init probe_for_thinkpad(void) 11358 { 11359 int is_thinkpad; 11360 11361 if (acpi_disabled) 11362 return -ENODEV; 11363 11364 /* It would be dangerous to run the driver in this case */ 11365 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo()) 11366 return -ENODEV; 11367 11368 /* 11369 * Non-ancient models have better DMI tagging, but very old models 11370 * don't. tpacpi_is_fw_known() is a cheat to help in that case. 11371 */ 11372 is_thinkpad = (thinkpad_id.model_str != NULL) || 11373 (thinkpad_id.ec_model != 0) || 11374 tpacpi_is_fw_known(); 11375 11376 /* The EC handler is required */ 11377 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle); 11378 if (!ec_handle) { 11379 if (is_thinkpad) 11380 pr_err("Not yet supported ThinkPad detected!\n"); 11381 return -ENODEV; 11382 } 11383 11384 if (!is_thinkpad && !force_load) 11385 return -ENODEV; 11386 11387 return 0; 11388 } 11389 11390 static void __init thinkpad_acpi_init_banner(void) 11391 { 11392 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION); 11393 pr_info("%s\n", TPACPI_URL); 11394 11395 pr_info("ThinkPad BIOS %s, EC %s\n", 11396 (thinkpad_id.bios_version_str) ? 11397 thinkpad_id.bios_version_str : "unknown", 11398 (thinkpad_id.ec_version_str) ? 11399 thinkpad_id.ec_version_str : "unknown"); 11400 11401 BUG_ON(!thinkpad_id.vendor); 11402 11403 if (thinkpad_id.model_str) 11404 pr_info("%s %s, model %s\n", 11405 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ? 11406 "IBM" : ((thinkpad_id.vendor == 11407 PCI_VENDOR_ID_LENOVO) ? 11408 "Lenovo" : "Unknown vendor"), 11409 thinkpad_id.model_str, 11410 (thinkpad_id.nummodel_str) ? 11411 thinkpad_id.nummodel_str : "unknown"); 11412 } 11413 11414 /* Module init, exit, parameters */ 11415 11416 static struct ibm_init_struct ibms_init[] __initdata = { 11417 { 11418 .data = &thinkpad_acpi_driver_data, 11419 }, 11420 { 11421 .init = hotkey_init, 11422 .data = &hotkey_driver_data, 11423 }, 11424 { 11425 .init = bluetooth_init, 11426 .data = &bluetooth_driver_data, 11427 }, 11428 { 11429 .init = wan_init, 11430 .data = &wan_driver_data, 11431 }, 11432 { 11433 .init = uwb_init, 11434 .data = &uwb_driver_data, 11435 }, 11436 #ifdef CONFIG_THINKPAD_ACPI_VIDEO 11437 { 11438 .init = video_init, 11439 .base_procfs_mode = S_IRUSR, 11440 .data = &video_driver_data, 11441 }, 11442 #endif 11443 { 11444 .init = kbdlight_init, 11445 .data = &kbdlight_driver_data, 11446 }, 11447 { 11448 .init = light_init, 11449 .data = &light_driver_data, 11450 }, 11451 { 11452 .init = cmos_init, 11453 .data = &cmos_driver_data, 11454 }, 11455 { 11456 .init = led_init, 11457 .data = &led_driver_data, 11458 }, 11459 { 11460 .init = beep_init, 11461 .data = &beep_driver_data, 11462 }, 11463 { 11464 .init = thermal_init, 11465 .data = &thermal_driver_data, 11466 }, 11467 { 11468 .init = brightness_init, 11469 .data = &brightness_driver_data, 11470 }, 11471 { 11472 .init = volume_init, 11473 .data = &volume_driver_data, 11474 }, 11475 { 11476 .init = fan_init, 11477 .data = &fan_driver_data, 11478 }, 11479 { 11480 .init = mute_led_init, 11481 .data = &mute_led_driver_data, 11482 }, 11483 { 11484 .init = tpacpi_battery_init, 11485 .data = &battery_driver_data, 11486 }, 11487 { 11488 .init = tpacpi_lcdshadow_init, 11489 .data = &lcdshadow_driver_data, 11490 }, 11491 { 11492 .init = tpacpi_proxsensor_init, 11493 .data = &proxsensor_driver_data, 11494 }, 11495 { 11496 .init = tpacpi_dytc_profile_init, 11497 .data = &dytc_profile_driver_data, 11498 }, 11499 { 11500 .init = tpacpi_kbdlang_init, 11501 .data = &kbdlang_driver_data, 11502 }, 11503 { 11504 .init = tpacpi_dprc_init, 11505 .data = &dprc_driver_data, 11506 }, 11507 }; 11508 11509 static int __init set_ibm_param(const char *val, const struct kernel_param *kp) 11510 { 11511 unsigned int i; 11512 struct ibm_struct *ibm; 11513 11514 if (!kp || !kp->name || !val) 11515 return -EINVAL; 11516 11517 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) { 11518 ibm = ibms_init[i].data; 11519 if (!ibm || !ibm->name) 11520 continue; 11521 11522 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) { 11523 if (strlen(val) > sizeof(ibms_init[i].param) - 1) 11524 return -ENOSPC; 11525 strcpy(ibms_init[i].param, val); 11526 return 0; 11527 } 11528 } 11529 11530 return -EINVAL; 11531 } 11532 11533 module_param(experimental, int, 0444); 11534 MODULE_PARM_DESC(experimental, 11535 "Enables experimental features when non-zero"); 11536 11537 module_param_named(debug, dbg_level, uint, 0); 11538 MODULE_PARM_DESC(debug, "Sets debug level bit-mask"); 11539 11540 module_param(force_load, bool, 0444); 11541 MODULE_PARM_DESC(force_load, 11542 "Attempts to load the driver even on a mis-identified ThinkPad when true"); 11543 11544 module_param_named(fan_control, fan_control_allowed, bool, 0444); 11545 MODULE_PARM_DESC(fan_control, 11546 "Enables setting fan parameters features when true"); 11547 11548 module_param_named(brightness_mode, brightness_mode, uint, 0444); 11549 MODULE_PARM_DESC(brightness_mode, 11550 "Selects brightness control strategy: 0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM"); 11551 11552 module_param(brightness_enable, uint, 0444); 11553 MODULE_PARM_DESC(brightness_enable, 11554 "Enables backlight control when 1, disables when 0"); 11555 11556 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT 11557 module_param_named(volume_mode, volume_mode, uint, 0444); 11558 MODULE_PARM_DESC(volume_mode, 11559 "Selects volume control strategy: 0=auto, 1=EC, 2=N/A, 3=EC+NVRAM"); 11560 11561 module_param_named(volume_capabilities, volume_capabilities, uint, 0444); 11562 MODULE_PARM_DESC(volume_capabilities, 11563 "Selects the mixer capabilities: 0=auto, 1=volume and mute, 2=mute only"); 11564 11565 module_param_named(volume_control, volume_control_allowed, bool, 0444); 11566 MODULE_PARM_DESC(volume_control, 11567 "Enables software override for the console audio control when true"); 11568 11569 module_param_named(software_mute, software_mute_requested, bool, 0444); 11570 MODULE_PARM_DESC(software_mute, 11571 "Request full software mute control"); 11572 11573 /* ALSA module API parameters */ 11574 module_param_named(index, alsa_index, int, 0444); 11575 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer"); 11576 module_param_named(id, alsa_id, charp, 0444); 11577 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer"); 11578 module_param_named(enable, alsa_enable, bool, 0444); 11579 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer"); 11580 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */ 11581 11582 /* The module parameter can't be read back, that's why 0 is used here */ 11583 #define TPACPI_PARAM(feature) \ 11584 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \ 11585 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command at module load, see documentation") 11586 11587 TPACPI_PARAM(hotkey); 11588 TPACPI_PARAM(bluetooth); 11589 TPACPI_PARAM(video); 11590 TPACPI_PARAM(light); 11591 TPACPI_PARAM(cmos); 11592 TPACPI_PARAM(led); 11593 TPACPI_PARAM(beep); 11594 TPACPI_PARAM(brightness); 11595 TPACPI_PARAM(volume); 11596 TPACPI_PARAM(fan); 11597 11598 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES 11599 module_param(dbg_wlswemul, uint, 0444); 11600 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation"); 11601 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0); 11602 MODULE_PARM_DESC(wlsw_state, 11603 "Initial state of the emulated WLSW switch"); 11604 11605 module_param(dbg_bluetoothemul, uint, 0444); 11606 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation"); 11607 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0); 11608 MODULE_PARM_DESC(bluetooth_state, 11609 "Initial state of the emulated bluetooth switch"); 11610 11611 module_param(dbg_wwanemul, uint, 0444); 11612 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation"); 11613 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0); 11614 MODULE_PARM_DESC(wwan_state, 11615 "Initial state of the emulated WWAN switch"); 11616 11617 module_param(dbg_uwbemul, uint, 0444); 11618 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation"); 11619 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0); 11620 MODULE_PARM_DESC(uwb_state, 11621 "Initial state of the emulated UWB switch"); 11622 #endif 11623 11624 static void thinkpad_acpi_module_exit(void) 11625 { 11626 struct ibm_struct *ibm, *itmp; 11627 11628 tpacpi_lifecycle = TPACPI_LIFE_EXITING; 11629 11630 #ifdef CONFIG_SUSPEND 11631 if (tp_features.quirks && tp_features.quirks->s2idle_bug_mmio) 11632 acpi_unregister_lps0_dev(&thinkpad_acpi_s2idle_dev_ops); 11633 #endif 11634 if (tpacpi_hwmon) 11635 hwmon_device_unregister(tpacpi_hwmon); 11636 if (tp_features.sensors_pdrv_registered) 11637 platform_driver_unregister(&tpacpi_hwmon_pdriver); 11638 if (tp_features.platform_drv_registered) 11639 platform_driver_unregister(&tpacpi_pdriver); 11640 11641 list_for_each_entry_safe_reverse(ibm, itmp, 11642 &tpacpi_all_drivers, 11643 all_drivers) { 11644 ibm_exit(ibm); 11645 } 11646 11647 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n"); 11648 11649 if (tpacpi_inputdev) { 11650 if (tp_features.input_device_registered) 11651 input_unregister_device(tpacpi_inputdev); 11652 else 11653 input_free_device(tpacpi_inputdev); 11654 kfree(hotkey_keycode_map); 11655 } 11656 11657 if (tpacpi_sensors_pdev) 11658 platform_device_unregister(tpacpi_sensors_pdev); 11659 if (tpacpi_pdev) 11660 platform_device_unregister(tpacpi_pdev); 11661 if (proc_dir) 11662 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir); 11663 if (tpacpi_wq) 11664 destroy_workqueue(tpacpi_wq); 11665 11666 kfree(thinkpad_id.bios_version_str); 11667 kfree(thinkpad_id.ec_version_str); 11668 kfree(thinkpad_id.model_str); 11669 kfree(thinkpad_id.nummodel_str); 11670 } 11671 11672 11673 static int __init thinkpad_acpi_module_init(void) 11674 { 11675 const struct dmi_system_id *dmi_id; 11676 int ret, i; 11677 11678 tpacpi_lifecycle = TPACPI_LIFE_INIT; 11679 11680 /* Driver-level probe */ 11681 11682 ret = get_thinkpad_model_data(&thinkpad_id); 11683 if (ret) { 11684 pr_err("unable to get DMI data: %d\n", ret); 11685 thinkpad_acpi_module_exit(); 11686 return ret; 11687 } 11688 ret = probe_for_thinkpad(); 11689 if (ret) { 11690 thinkpad_acpi_module_exit(); 11691 return ret; 11692 } 11693 11694 /* Driver initialization */ 11695 11696 thinkpad_acpi_init_banner(); 11697 tpacpi_check_outdated_fw(); 11698 11699 TPACPI_ACPIHANDLE_INIT(ecrd); 11700 TPACPI_ACPIHANDLE_INIT(ecwr); 11701 11702 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME); 11703 if (!tpacpi_wq) { 11704 thinkpad_acpi_module_exit(); 11705 return -ENOMEM; 11706 } 11707 11708 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir); 11709 if (!proc_dir) { 11710 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n"); 11711 thinkpad_acpi_module_exit(); 11712 return -ENODEV; 11713 } 11714 11715 dmi_id = dmi_first_match(fwbug_list); 11716 if (dmi_id) 11717 tp_features.quirks = dmi_id->driver_data; 11718 11719 /* Device initialization */ 11720 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, PLATFORM_DEVID_NONE, 11721 NULL, 0); 11722 if (IS_ERR(tpacpi_pdev)) { 11723 ret = PTR_ERR(tpacpi_pdev); 11724 tpacpi_pdev = NULL; 11725 pr_err("unable to register platform device\n"); 11726 thinkpad_acpi_module_exit(); 11727 return ret; 11728 } 11729 tpacpi_sensors_pdev = platform_device_register_simple( 11730 TPACPI_HWMON_DRVR_NAME, 11731 PLATFORM_DEVID_NONE, NULL, 0); 11732 if (IS_ERR(tpacpi_sensors_pdev)) { 11733 ret = PTR_ERR(tpacpi_sensors_pdev); 11734 tpacpi_sensors_pdev = NULL; 11735 pr_err("unable to register hwmon platform device\n"); 11736 thinkpad_acpi_module_exit(); 11737 return ret; 11738 } 11739 11740 mutex_init(&tpacpi_inputdev_send_mutex); 11741 tpacpi_inputdev = input_allocate_device(); 11742 if (!tpacpi_inputdev) { 11743 thinkpad_acpi_module_exit(); 11744 return -ENOMEM; 11745 } else { 11746 /* Prepare input device, but don't register */ 11747 tpacpi_inputdev->name = "ThinkPad Extra Buttons"; 11748 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0"; 11749 tpacpi_inputdev->id.bustype = BUS_HOST; 11750 tpacpi_inputdev->id.vendor = thinkpad_id.vendor; 11751 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT; 11752 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION; 11753 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev; 11754 } 11755 11756 /* Init subdriver dependencies */ 11757 tpacpi_detect_brightness_capabilities(); 11758 11759 /* Init subdrivers */ 11760 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) { 11761 ret = ibm_init(&ibms_init[i]); 11762 if (ret >= 0 && *ibms_init[i].param) 11763 ret = ibms_init[i].data->write(ibms_init[i].param); 11764 if (ret < 0) { 11765 thinkpad_acpi_module_exit(); 11766 return ret; 11767 } 11768 } 11769 11770 tpacpi_lifecycle = TPACPI_LIFE_RUNNING; 11771 11772 ret = platform_driver_register(&tpacpi_pdriver); 11773 if (ret) { 11774 pr_err("unable to register main platform driver\n"); 11775 thinkpad_acpi_module_exit(); 11776 return ret; 11777 } 11778 tp_features.platform_drv_registered = 1; 11779 11780 ret = platform_driver_register(&tpacpi_hwmon_pdriver); 11781 if (ret) { 11782 pr_err("unable to register hwmon platform driver\n"); 11783 thinkpad_acpi_module_exit(); 11784 return ret; 11785 } 11786 tp_features.sensors_pdrv_registered = 1; 11787 11788 tpacpi_hwmon = hwmon_device_register_with_groups( 11789 &tpacpi_sensors_pdev->dev, TPACPI_NAME, NULL, tpacpi_hwmon_groups); 11790 if (IS_ERR(tpacpi_hwmon)) { 11791 ret = PTR_ERR(tpacpi_hwmon); 11792 tpacpi_hwmon = NULL; 11793 pr_err("unable to register hwmon device\n"); 11794 thinkpad_acpi_module_exit(); 11795 return ret; 11796 } 11797 11798 ret = input_register_device(tpacpi_inputdev); 11799 if (ret < 0) { 11800 pr_err("unable to register input device\n"); 11801 thinkpad_acpi_module_exit(); 11802 return ret; 11803 } else { 11804 tp_features.input_device_registered = 1; 11805 } 11806 11807 #ifdef CONFIG_SUSPEND 11808 if (tp_features.quirks && tp_features.quirks->s2idle_bug_mmio) { 11809 if (!acpi_register_lps0_dev(&thinkpad_acpi_s2idle_dev_ops)) 11810 pr_info("Using s2idle quirk to avoid %s platform firmware bug\n", 11811 (dmi_id && dmi_id->ident) ? dmi_id->ident : ""); 11812 } 11813 #endif 11814 return 0; 11815 } 11816 11817 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME); 11818 11819 /* 11820 * This will autoload the driver in almost every ThinkPad 11821 * in widespread use. 11822 * 11823 * Only _VERY_ old models, like the 240, 240x and 570 lack 11824 * the HKEY event interface. 11825 */ 11826 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids); 11827 11828 /* 11829 * DMI matching for module autoloading 11830 * 11831 * See https://thinkwiki.org/wiki/List_of_DMI_IDs 11832 * See https://thinkwiki.org/wiki/BIOS_Upgrade_Downloads 11833 * 11834 * Only models listed in thinkwiki will be supported, so add yours 11835 * if it is not there yet. 11836 */ 11837 #define IBM_BIOS_MODULE_ALIAS(__type) \ 11838 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*") 11839 11840 /* Ancient thinkpad BIOSes have to be identified by 11841 * BIOS type or model number, and there are far less 11842 * BIOS types than model numbers... */ 11843 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */ 11844 11845 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>"); 11846 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>"); 11847 MODULE_DESCRIPTION(TPACPI_DESC); 11848 MODULE_VERSION(TPACPI_VERSION); 11849 MODULE_LICENSE("GPL"); 11850 11851 module_init(thinkpad_acpi_module_init); 11852 module_exit(thinkpad_acpi_module_exit); 11853