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