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