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