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