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