ACPI: thinkpad-acpi: add development version tag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobdbdcdad7857089139b46363cfcde9fad0cb1e827
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
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.
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.
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.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #ifndef pr_warn
27 #define pr_warn pr_warning
28 #endif
30 #ifndef no_printk
32 * Dummy printk for disabled debugging statements to use whilst maintaining
33 * gcc's format and side-effect checking.
35 static inline __attribute__ ((format (printf, 1, 2)))
36 int no_printk(const char *fmt, ...)
38 return 0;
40 #endif
42 #define TPACPI_VERSION "0.24-20110604"
43 #define TPACPI_SYSFS_VERSION 0x020700
46 * Changelog:
47 * 2007-10-20 changelog trimmed down
49 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
50 * drivers/misc.
52 * 2006-11-22 0.13 new maintainer
53 * changelog now lives in git commit history, and will
54 * not be updated further in-file.
56 * 2005-03-17 0.11 support for 600e, 770x
57 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
59 * 2005-01-16 0.9 use MODULE_VERSION
60 * thanks to Henrik Brix Andersen <brix@gentoo.org>
61 * fix parameter passing on module loading
62 * thanks to Rusty Russell <rusty@rustcorp.com.au>
63 * thanks to Jim Radford <radford@blackbean.org>
64 * 2004-11-08 0.8 fix init error case, don't return from a macro
65 * thanks to Chris Wright <chrisw@osdl.org>
68 #include <linux/kernel.h>
69 #include <linux/module.h>
70 #include <linux/init.h>
71 #include <linux/types.h>
72 #include <linux/string.h>
73 #include <linux/list.h>
74 #include <linux/mutex.h>
75 #include <linux/sched.h>
76 #include <linux/kthread.h>
77 #include <linux/freezer.h>
78 #include <linux/delay.h>
80 #include <linux/nvram.h>
81 #include <linux/proc_fs.h>
82 #include <linux/seq_file.h>
83 #include <linux/sysfs.h>
84 #include <linux/backlight.h>
85 #include <linux/fb.h>
86 #include <linux/platform_device.h>
87 #include <linux/hwmon.h>
88 #include <linux/hwmon-sysfs.h>
89 #include <linux/input.h>
90 #include <linux/leds.h>
91 #include <linux/rfkill.h>
92 #include <asm/uaccess.h>
94 #include <linux/dmi.h>
95 #include <linux/jiffies.h>
96 #include <linux/workqueue.h>
98 #include <sound/core.h>
99 #include <sound/control.h>
100 #include <sound/initval.h>
102 #include <acpi/acpi_drivers.h>
104 #include <linux/pci_ids.h>
107 /* ThinkPad CMOS commands */
108 #define TP_CMOS_VOLUME_DOWN 0
109 #define TP_CMOS_VOLUME_UP 1
110 #define TP_CMOS_VOLUME_MUTE 2
111 #define TP_CMOS_BRIGHTNESS_UP 4
112 #define TP_CMOS_BRIGHTNESS_DOWN 5
113 #define TP_CMOS_THINKLIGHT_ON 12
114 #define TP_CMOS_THINKLIGHT_OFF 13
116 /* NVRAM Addresses */
117 enum tp_nvram_addr {
118 TP_NVRAM_ADDR_HK2 = 0x57,
119 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
120 TP_NVRAM_ADDR_VIDEO = 0x59,
121 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
122 TP_NVRAM_ADDR_MIXER = 0x60,
125 /* NVRAM bit masks */
126 enum {
127 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
128 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
129 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
130 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
131 TP_NVRAM_MASK_THINKLIGHT = 0x10,
132 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
133 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
134 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
135 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
136 TP_NVRAM_MASK_MUTE = 0x40,
137 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
138 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
139 TP_NVRAM_POS_LEVEL_VOLUME = 0,
142 /* Misc NVRAM-related */
143 enum {
144 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
147 /* ACPI HIDs */
148 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
149 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
150 #define TPACPI_ACPI_EC_HID "PNP0C09"
152 /* Input IDs */
153 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
154 #define TPACPI_HKEY_INPUT_VERSION 0x4101
156 /* ACPI \WGSV commands */
157 enum {
158 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
159 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
160 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
161 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
164 /* TP_ACPI_WGSV_GET_STATE bits */
165 enum {
166 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
167 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
168 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
169 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
170 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
171 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
172 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
173 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
174 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
175 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
178 /* HKEY events */
179 enum tpacpi_hkey_event_t {
180 /* Hotkey-related */
181 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
182 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
183 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
184 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
185 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
186 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
188 /* Reasons for waking up from S3/S4 */
189 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
190 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
191 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
192 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
193 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
194 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
196 /* Auto-sleep after eject request */
197 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
198 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
200 /* Misc bay events */
201 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
202 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
203 or port replicator */
204 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
205 dock or port replicator */
207 /* User-interface events */
208 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
209 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
210 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
211 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
212 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
213 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
214 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
216 /* Key-related user-interface events */
217 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
218 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
220 /* Thermal events */
221 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
222 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
223 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
224 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
225 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
227 TP_HKEY_EV_UNK_6040 = 0x6040, /* Related to AC change?
228 some sort of APM hint,
229 W520 */
231 /* Misc */
232 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
235 /****************************************************************************
236 * Main driver
239 #define TPACPI_NAME "thinkpad"
240 #define TPACPI_DESC "ThinkPad ACPI Extras"
241 #define TPACPI_FILE TPACPI_NAME "_acpi"
242 #define TPACPI_URL "http://ibm-acpi.sf.net/"
243 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
245 #define TPACPI_PROC_DIR "ibm"
246 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
247 #define TPACPI_DRVR_NAME TPACPI_FILE
248 #define TPACPI_DRVR_SHORTNAME "tpacpi"
249 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
251 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
252 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
254 #define TPACPI_MAX_ACPI_ARGS 3
256 /* Debugging printk groups */
257 #define TPACPI_DBG_ALL 0xffff
258 #define TPACPI_DBG_DISCLOSETASK 0x8000
259 #define TPACPI_DBG_INIT 0x0001
260 #define TPACPI_DBG_EXIT 0x0002
261 #define TPACPI_DBG_RFKILL 0x0004
262 #define TPACPI_DBG_HKEY 0x0008
263 #define TPACPI_DBG_FAN 0x0010
264 #define TPACPI_DBG_BRGHT 0x0020
265 #define TPACPI_DBG_MIXER 0x0040
267 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
268 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
269 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
272 /****************************************************************************
273 * Driver-wide structs and misc. variables
276 struct ibm_struct;
278 struct tp_acpi_drv_struct {
279 const struct acpi_device_id *hid;
280 struct acpi_driver *driver;
282 void (*notify) (struct ibm_struct *, u32);
283 acpi_handle *handle;
284 u32 type;
285 struct acpi_device *device;
288 struct ibm_struct {
289 char *name;
291 int (*read) (struct seq_file *);
292 int (*write) (char *);
293 void (*exit) (void);
294 void (*resume) (void);
295 void (*suspend) (pm_message_t state);
296 void (*shutdown) (void);
298 struct list_head all_drivers;
300 struct tp_acpi_drv_struct *acpi;
302 struct {
303 u8 acpi_driver_registered:1;
304 u8 acpi_notify_installed:1;
305 u8 proc_created:1;
306 u8 init_called:1;
307 u8 experimental:1;
308 } flags;
311 struct ibm_init_struct {
312 char param[32];
314 int (*init) (struct ibm_init_struct *);
315 mode_t base_procfs_mode;
316 struct ibm_struct *data;
319 static struct {
320 u32 bluetooth:1;
321 u32 hotkey:1;
322 u32 hotkey_mask:1;
323 u32 hotkey_wlsw:1;
324 u32 hotkey_tablet:1;
325 u32 light:1;
326 u32 light_status:1;
327 u32 bright_acpimode:1;
328 u32 bright_unkfw:1;
329 u32 wan:1;
330 u32 uwb:1;
331 u32 fan_ctrl_status_undef:1;
332 u32 second_fan:1;
333 u32 beep_needs_two_args:1;
334 u32 mixer_no_level_control:1;
335 u32 input_device_registered:1;
336 u32 platform_drv_registered:1;
337 u32 platform_drv_attrs_registered:1;
338 u32 sensors_pdrv_registered:1;
339 u32 sensors_pdrv_attrs_registered:1;
340 u32 sensors_pdev_attrs_registered:1;
341 u32 hotkey_poll_active:1;
342 } tp_features;
344 static struct {
345 u16 hotkey_mask_ff:1;
346 u16 volume_ctrl_forbidden:1;
347 } tp_warned;
349 struct thinkpad_id_data {
350 unsigned int vendor; /* ThinkPad vendor:
351 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
353 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
354 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
356 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
357 u16 ec_model;
358 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
359 u16 ec_release;
361 char *model_str; /* ThinkPad T43 */
362 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
364 static struct thinkpad_id_data thinkpad_id;
366 static enum {
367 TPACPI_LIFE_INIT = 0,
368 TPACPI_LIFE_RUNNING,
369 TPACPI_LIFE_EXITING,
370 } tpacpi_lifecycle;
372 static int experimental;
373 static u32 dbg_level;
375 static struct workqueue_struct *tpacpi_wq;
377 enum led_status_t {
378 TPACPI_LED_OFF = 0,
379 TPACPI_LED_ON,
380 TPACPI_LED_BLINK,
383 /* Special LED class that can defer work */
384 struct tpacpi_led_classdev {
385 struct led_classdev led_classdev;
386 struct work_struct work;
387 enum led_status_t new_state;
388 unsigned int led;
391 /* brightness level capabilities */
392 static unsigned int bright_maxlvl; /* 0 = unknown */
394 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
395 static int dbg_wlswemul;
396 static int tpacpi_wlsw_emulstate;
397 static int dbg_bluetoothemul;
398 static int tpacpi_bluetooth_emulstate;
399 static int dbg_wwanemul;
400 static int tpacpi_wwan_emulstate;
401 static int dbg_uwbemul;
402 static int tpacpi_uwb_emulstate;
403 #endif
406 /*************************************************************************
407 * Debugging helpers
410 #define dbg_printk(a_dbg_level, format, arg...) \
411 do { \
412 if (dbg_level & (a_dbg_level)) \
413 printk(KERN_DEBUG pr_fmt("%s: " format), \
414 __func__, ##arg); \
415 } while (0)
417 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
418 #define vdbg_printk dbg_printk
419 static const char *str_supported(int is_supported);
420 #else
421 static inline const char *str_supported(int is_supported) { return ""; }
422 #define vdbg_printk(a_dbg_level, format, arg...) \
423 no_printk(format, ##arg)
424 #endif
426 static void tpacpi_log_usertask(const char * const what)
428 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
429 what, task_tgid_vnr(current));
432 #define tpacpi_disclose_usertask(what, format, arg...) \
433 do { \
434 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
435 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
436 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
437 what, task_tgid_vnr(current), ## arg); \
439 } while (0)
442 * Quirk handling helpers
444 * ThinkPad IDs and versions seen in the field so far
445 * are two-characters from the set [0-9A-Z], i.e. base 36.
447 * We use values well outside that range as specials.
450 #define TPACPI_MATCH_ANY 0xffffU
451 #define TPACPI_MATCH_UNKNOWN 0U
453 /* TPID('1', 'Y') == 0x5931 */
454 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
456 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
457 { .vendor = PCI_VENDOR_ID_IBM, \
458 .bios = TPID(__id1, __id2), \
459 .ec = TPACPI_MATCH_ANY, \
460 .quirks = (__quirk) }
462 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
463 { .vendor = PCI_VENDOR_ID_LENOVO, \
464 .bios = TPID(__id1, __id2), \
465 .ec = TPACPI_MATCH_ANY, \
466 .quirks = (__quirk) }
468 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
469 { .vendor = PCI_VENDOR_ID_LENOVO, \
470 .bios = TPACPI_MATCH_ANY, \
471 .ec = TPID(__id1, __id2), \
472 .quirks = (__quirk) }
474 struct tpacpi_quirk {
475 unsigned int vendor;
476 u16 bios;
477 u16 ec;
478 unsigned long quirks;
482 * tpacpi_check_quirks() - search BIOS/EC version on a list
483 * @qlist: array of &struct tpacpi_quirk
484 * @qlist_size: number of elements in @qlist
486 * Iterates over a quirks list until one is found that matches the
487 * ThinkPad's vendor, BIOS and EC model.
489 * Returns 0 if nothing matches, otherwise returns the quirks field of
490 * the matching &struct tpacpi_quirk entry.
492 * The match criteria is: vendor, ec and bios much match.
494 static unsigned long __init tpacpi_check_quirks(
495 const struct tpacpi_quirk *qlist,
496 unsigned int qlist_size)
498 while (qlist_size) {
499 if ((qlist->vendor == thinkpad_id.vendor ||
500 qlist->vendor == TPACPI_MATCH_ANY) &&
501 (qlist->bios == thinkpad_id.bios_model ||
502 qlist->bios == TPACPI_MATCH_ANY) &&
503 (qlist->ec == thinkpad_id.ec_model ||
504 qlist->ec == TPACPI_MATCH_ANY))
505 return qlist->quirks;
507 qlist_size--;
508 qlist++;
510 return 0;
513 static inline bool __pure __init tpacpi_is_lenovo(void)
515 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
518 static inline bool __pure __init tpacpi_is_ibm(void)
520 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
523 /****************************************************************************
524 ****************************************************************************
526 * ACPI Helpers and device model
528 ****************************************************************************
529 ****************************************************************************/
531 /*************************************************************************
532 * ACPI basic handles
535 static acpi_handle root_handle;
536 static acpi_handle ec_handle;
538 #define TPACPI_HANDLE(object, parent, paths...) \
539 static acpi_handle object##_handle; \
540 static const acpi_handle *object##_parent __initdata = \
541 &parent##_handle; \
542 static char *object##_paths[] __initdata = { paths }
544 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
545 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
547 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
548 /* T4x, X31, X40 */
549 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
550 "\\CMS", /* R40, R40e */
551 ); /* all others */
553 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
554 "^HKEY", /* R30, R31 */
555 "HKEY", /* all others */
556 ); /* 570 */
558 /*************************************************************************
559 * ACPI helpers
562 static int acpi_evalf(acpi_handle handle,
563 void *res, char *method, char *fmt, ...)
565 char *fmt0 = fmt;
566 struct acpi_object_list params;
567 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
568 struct acpi_buffer result, *resultp;
569 union acpi_object out_obj;
570 acpi_status status;
571 va_list ap;
572 char res_type;
573 int success;
574 int quiet;
576 if (!*fmt) {
577 pr_err("acpi_evalf() called with empty format\n");
578 return 0;
581 if (*fmt == 'q') {
582 quiet = 1;
583 fmt++;
584 } else
585 quiet = 0;
587 res_type = *(fmt++);
589 params.count = 0;
590 params.pointer = &in_objs[0];
592 va_start(ap, fmt);
593 while (*fmt) {
594 char c = *(fmt++);
595 switch (c) {
596 case 'd': /* int */
597 in_objs[params.count].integer.value = va_arg(ap, int);
598 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
599 break;
600 /* add more types as needed */
601 default:
602 pr_err("acpi_evalf() called "
603 "with invalid format character '%c'\n", c);
604 va_end(ap);
605 return 0;
608 va_end(ap);
610 if (res_type != 'v') {
611 result.length = sizeof(out_obj);
612 result.pointer = &out_obj;
613 resultp = &result;
614 } else
615 resultp = NULL;
617 status = acpi_evaluate_object(handle, method, &params, resultp);
619 switch (res_type) {
620 case 'd': /* int */
621 success = (status == AE_OK &&
622 out_obj.type == ACPI_TYPE_INTEGER);
623 if (success && res)
624 *(int *)res = out_obj.integer.value;
625 break;
626 case 'v': /* void */
627 success = status == AE_OK;
628 break;
629 /* add more types as needed */
630 default:
631 pr_err("acpi_evalf() called "
632 "with invalid format character '%c'\n", res_type);
633 return 0;
636 if (!success && !quiet)
637 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
638 method, fmt0, acpi_format_exception(status));
640 return success;
643 static int acpi_ec_read(int i, u8 *p)
645 int v;
647 if (ecrd_handle) {
648 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
649 return 0;
650 *p = v;
651 } else {
652 if (ec_read(i, p) < 0)
653 return 0;
656 return 1;
659 static int acpi_ec_write(int i, u8 v)
661 if (ecwr_handle) {
662 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
663 return 0;
664 } else {
665 if (ec_write(i, v) < 0)
666 return 0;
669 return 1;
672 static int issue_thinkpad_cmos_command(int cmos_cmd)
674 if (!cmos_handle)
675 return -ENXIO;
677 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
678 return -EIO;
680 return 0;
683 /*************************************************************************
684 * ACPI device model
687 #define TPACPI_ACPIHANDLE_INIT(object) \
688 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
689 object##_paths, ARRAY_SIZE(object##_paths))
691 static void __init drv_acpi_handle_init(const char *name,
692 acpi_handle *handle, const acpi_handle parent,
693 char **paths, const int num_paths)
695 int i;
696 acpi_status status;
698 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
699 name);
701 for (i = 0; i < num_paths; i++) {
702 status = acpi_get_handle(parent, paths[i], handle);
703 if (ACPI_SUCCESS(status)) {
704 dbg_printk(TPACPI_DBG_INIT,
705 "Found ACPI handle %s for %s\n",
706 paths[i], name);
707 return;
711 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
712 name);
713 *handle = NULL;
716 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
717 u32 level, void *context, void **return_value)
719 *(acpi_handle *)return_value = handle;
721 return AE_CTRL_TERMINATE;
724 static void __init tpacpi_acpi_handle_locate(const char *name,
725 const char *hid,
726 acpi_handle *handle)
728 acpi_status status;
729 acpi_handle device_found;
731 BUG_ON(!name || !hid || !handle);
732 vdbg_printk(TPACPI_DBG_INIT,
733 "trying to locate ACPI handle for %s, using HID %s\n",
734 name, hid);
736 memset(&device_found, 0, sizeof(device_found));
737 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
738 (void *)name, &device_found);
740 *handle = NULL;
742 if (ACPI_SUCCESS(status)) {
743 *handle = device_found;
744 dbg_printk(TPACPI_DBG_INIT,
745 "Found ACPI handle for %s\n", name);
746 } else {
747 vdbg_printk(TPACPI_DBG_INIT,
748 "Could not locate an ACPI handle for %s: %s\n",
749 name, acpi_format_exception(status));
753 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
755 struct ibm_struct *ibm = data;
757 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
758 return;
760 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
761 return;
763 ibm->acpi->notify(ibm, event);
766 static int __init setup_acpi_notify(struct ibm_struct *ibm)
768 acpi_status status;
769 int rc;
771 BUG_ON(!ibm->acpi);
773 if (!*ibm->acpi->handle)
774 return 0;
776 vdbg_printk(TPACPI_DBG_INIT,
777 "setting up ACPI notify for %s\n", ibm->name);
779 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
780 if (rc < 0) {
781 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
782 return -ENODEV;
785 ibm->acpi->device->driver_data = ibm;
786 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
787 TPACPI_ACPI_EVENT_PREFIX,
788 ibm->name);
790 status = acpi_install_notify_handler(*ibm->acpi->handle,
791 ibm->acpi->type, dispatch_acpi_notify, ibm);
792 if (ACPI_FAILURE(status)) {
793 if (status == AE_ALREADY_EXISTS) {
794 pr_notice("another device driver is already "
795 "handling %s events\n", ibm->name);
796 } else {
797 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
798 ibm->name, acpi_format_exception(status));
800 return -ENODEV;
802 ibm->flags.acpi_notify_installed = 1;
803 return 0;
806 static int __init tpacpi_device_add(struct acpi_device *device)
808 return 0;
811 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
813 int rc;
815 dbg_printk(TPACPI_DBG_INIT,
816 "registering %s as an ACPI driver\n", ibm->name);
818 BUG_ON(!ibm->acpi);
820 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
821 if (!ibm->acpi->driver) {
822 pr_err("failed to allocate memory for ibm->acpi->driver\n");
823 return -ENOMEM;
826 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
827 ibm->acpi->driver->ids = ibm->acpi->hid;
829 ibm->acpi->driver->ops.add = &tpacpi_device_add;
831 rc = acpi_bus_register_driver(ibm->acpi->driver);
832 if (rc < 0) {
833 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
834 ibm->name, rc);
835 kfree(ibm->acpi->driver);
836 ibm->acpi->driver = NULL;
837 } else if (!rc)
838 ibm->flags.acpi_driver_registered = 1;
840 return rc;
844 /****************************************************************************
845 ****************************************************************************
847 * Procfs Helpers
849 ****************************************************************************
850 ****************************************************************************/
852 static int dispatch_proc_show(struct seq_file *m, void *v)
854 struct ibm_struct *ibm = m->private;
856 if (!ibm || !ibm->read)
857 return -EINVAL;
858 return ibm->read(m);
861 static int dispatch_proc_open(struct inode *inode, struct file *file)
863 return single_open(file, dispatch_proc_show, PDE(inode)->data);
866 static ssize_t dispatch_proc_write(struct file *file,
867 const char __user *userbuf,
868 size_t count, loff_t *pos)
870 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
871 char *kernbuf;
872 int ret;
874 if (!ibm || !ibm->write)
875 return -EINVAL;
876 if (count > PAGE_SIZE - 2)
877 return -EINVAL;
879 kernbuf = kmalloc(count + 2, GFP_KERNEL);
880 if (!kernbuf)
881 return -ENOMEM;
883 if (copy_from_user(kernbuf, userbuf, count)) {
884 kfree(kernbuf);
885 return -EFAULT;
888 kernbuf[count] = 0;
889 strcat(kernbuf, ",");
890 ret = ibm->write(kernbuf);
891 if (ret == 0)
892 ret = count;
894 kfree(kernbuf);
896 return ret;
899 static const struct file_operations dispatch_proc_fops = {
900 .owner = THIS_MODULE,
901 .open = dispatch_proc_open,
902 .read = seq_read,
903 .llseek = seq_lseek,
904 .release = single_release,
905 .write = dispatch_proc_write,
908 static char *next_cmd(char **cmds)
910 char *start = *cmds;
911 char *end;
913 while ((end = strchr(start, ',')) && end == start)
914 start = end + 1;
916 if (!end)
917 return NULL;
919 *end = 0;
920 *cmds = end + 1;
921 return start;
925 /****************************************************************************
926 ****************************************************************************
928 * Device model: input, hwmon and platform
930 ****************************************************************************
931 ****************************************************************************/
933 static struct platform_device *tpacpi_pdev;
934 static struct platform_device *tpacpi_sensors_pdev;
935 static struct device *tpacpi_hwmon;
936 static struct input_dev *tpacpi_inputdev;
937 static struct mutex tpacpi_inputdev_send_mutex;
938 static LIST_HEAD(tpacpi_all_drivers);
940 static int tpacpi_suspend_handler(struct platform_device *pdev,
941 pm_message_t state)
943 struct ibm_struct *ibm, *itmp;
945 list_for_each_entry_safe(ibm, itmp,
946 &tpacpi_all_drivers,
947 all_drivers) {
948 if (ibm->suspend)
949 (ibm->suspend)(state);
952 return 0;
955 static int tpacpi_resume_handler(struct platform_device *pdev)
957 struct ibm_struct *ibm, *itmp;
959 list_for_each_entry_safe(ibm, itmp,
960 &tpacpi_all_drivers,
961 all_drivers) {
962 if (ibm->resume)
963 (ibm->resume)();
966 return 0;
969 static void tpacpi_shutdown_handler(struct platform_device *pdev)
971 struct ibm_struct *ibm, *itmp;
973 list_for_each_entry_safe(ibm, itmp,
974 &tpacpi_all_drivers,
975 all_drivers) {
976 if (ibm->shutdown)
977 (ibm->shutdown)();
981 static struct platform_driver tpacpi_pdriver = {
982 .driver = {
983 .name = TPACPI_DRVR_NAME,
984 .owner = THIS_MODULE,
986 .suspend = tpacpi_suspend_handler,
987 .resume = tpacpi_resume_handler,
988 .shutdown = tpacpi_shutdown_handler,
991 static struct platform_driver tpacpi_hwmon_pdriver = {
992 .driver = {
993 .name = TPACPI_HWMON_DRVR_NAME,
994 .owner = THIS_MODULE,
998 /*************************************************************************
999 * sysfs support helpers
1002 struct attribute_set {
1003 unsigned int members, max_members;
1004 struct attribute_group group;
1007 struct attribute_set_obj {
1008 struct attribute_set s;
1009 struct attribute *a;
1010 } __attribute__((packed));
1012 static struct attribute_set *create_attr_set(unsigned int max_members,
1013 const char *name)
1015 struct attribute_set_obj *sobj;
1017 if (max_members == 0)
1018 return NULL;
1020 /* Allocates space for implicit NULL at the end too */
1021 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1022 max_members * sizeof(struct attribute *),
1023 GFP_KERNEL);
1024 if (!sobj)
1025 return NULL;
1026 sobj->s.max_members = max_members;
1027 sobj->s.group.attrs = &sobj->a;
1028 sobj->s.group.name = name;
1030 return &sobj->s;
1033 #define destroy_attr_set(_set) \
1034 kfree(_set);
1036 /* not multi-threaded safe, use it in a single thread per set */
1037 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1039 if (!s || !attr)
1040 return -EINVAL;
1042 if (s->members >= s->max_members)
1043 return -ENOMEM;
1045 s->group.attrs[s->members] = attr;
1046 s->members++;
1048 return 0;
1051 static int add_many_to_attr_set(struct attribute_set *s,
1052 struct attribute **attr,
1053 unsigned int count)
1055 int i, res;
1057 for (i = 0; i < count; i++) {
1058 res = add_to_attr_set(s, attr[i]);
1059 if (res)
1060 return res;
1063 return 0;
1066 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1068 sysfs_remove_group(kobj, &s->group);
1069 destroy_attr_set(s);
1072 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1073 sysfs_create_group(_kobj, &_attr_set->group)
1075 static int parse_strtoul(const char *buf,
1076 unsigned long max, unsigned long *value)
1078 char *endp;
1080 while (*buf && isspace(*buf))
1081 buf++;
1082 *value = simple_strtoul(buf, &endp, 0);
1083 while (*endp && isspace(*endp))
1084 endp++;
1085 if (*endp || *value > max)
1086 return -EINVAL;
1088 return 0;
1091 static void tpacpi_disable_brightness_delay(void)
1093 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1094 pr_notice("ACPI backlight control delay disabled\n");
1097 static void printk_deprecated_attribute(const char * const what,
1098 const char * const details)
1100 tpacpi_log_usertask("deprecated sysfs attribute");
1101 pr_warn("WARNING: sysfs attribute %s is deprecated and "
1102 "will be removed. %s\n",
1103 what, details);
1106 /*************************************************************************
1107 * rfkill and radio control support helpers
1111 * ThinkPad-ACPI firmware handling model:
1113 * WLSW (master wireless switch) is event-driven, and is common to all
1114 * firmware-controlled radios. It cannot be controlled, just monitored,
1115 * as expected. It overrides all radio state in firmware
1117 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1118 * (TODO: verify how WLSW interacts with the returned radio state).
1120 * The only time there are shadow radio state changes, is when
1121 * masked-off hotkeys are used.
1125 * Internal driver API for radio state:
1127 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1128 * bool: true means radio blocked (off)
1130 enum tpacpi_rfkill_state {
1131 TPACPI_RFK_RADIO_OFF = 0,
1132 TPACPI_RFK_RADIO_ON
1135 /* rfkill switches */
1136 enum tpacpi_rfk_id {
1137 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1138 TPACPI_RFK_WWAN_SW_ID,
1139 TPACPI_RFK_UWB_SW_ID,
1140 TPACPI_RFK_SW_MAX
1143 static const char *tpacpi_rfkill_names[] = {
1144 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1145 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1146 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1147 [TPACPI_RFK_SW_MAX] = NULL
1150 /* ThinkPad-ACPI rfkill subdriver */
1151 struct tpacpi_rfk {
1152 struct rfkill *rfkill;
1153 enum tpacpi_rfk_id id;
1154 const struct tpacpi_rfk_ops *ops;
1157 struct tpacpi_rfk_ops {
1158 /* firmware interface */
1159 int (*get_status)(void);
1160 int (*set_status)(const enum tpacpi_rfkill_state);
1163 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1165 /* Query FW and update rfkill sw state for a given rfkill switch */
1166 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1168 int status;
1170 if (!tp_rfk)
1171 return -ENODEV;
1173 status = (tp_rfk->ops->get_status)();
1174 if (status < 0)
1175 return status;
1177 rfkill_set_sw_state(tp_rfk->rfkill,
1178 (status == TPACPI_RFK_RADIO_OFF));
1180 return status;
1183 /* Query FW and update rfkill sw state for all rfkill switches */
1184 static void tpacpi_rfk_update_swstate_all(void)
1186 unsigned int i;
1188 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1189 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1193 * Sync the HW-blocking state of all rfkill switches,
1194 * do notice it causes the rfkill core to schedule uevents
1196 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1198 unsigned int i;
1199 struct tpacpi_rfk *tp_rfk;
1201 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1202 tp_rfk = tpacpi_rfkill_switches[i];
1203 if (tp_rfk) {
1204 if (rfkill_set_hw_state(tp_rfk->rfkill,
1205 blocked)) {
1206 /* ignore -- we track sw block */
1212 /* Call to get the WLSW state from the firmware */
1213 static int hotkey_get_wlsw(void);
1215 /* Call to query WLSW state and update all rfkill switches */
1216 static bool tpacpi_rfk_check_hwblock_state(void)
1218 int res = hotkey_get_wlsw();
1219 int hw_blocked;
1221 /* When unknown or unsupported, we have to assume it is unblocked */
1222 if (res < 0)
1223 return false;
1225 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1226 tpacpi_rfk_update_hwblock_state(hw_blocked);
1228 return hw_blocked;
1231 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1233 struct tpacpi_rfk *tp_rfk = data;
1234 int res;
1236 dbg_printk(TPACPI_DBG_RFKILL,
1237 "request to change radio state to %s\n",
1238 blocked ? "blocked" : "unblocked");
1240 /* try to set radio state */
1241 res = (tp_rfk->ops->set_status)(blocked ?
1242 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1244 /* and update the rfkill core with whatever the FW really did */
1245 tpacpi_rfk_update_swstate(tp_rfk);
1247 return (res < 0) ? res : 0;
1250 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1251 .set_block = tpacpi_rfk_hook_set_block,
1254 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1255 const struct tpacpi_rfk_ops *tp_rfkops,
1256 const enum rfkill_type rfktype,
1257 const char *name,
1258 const bool set_default)
1260 struct tpacpi_rfk *atp_rfk;
1261 int res;
1262 bool sw_state = false;
1263 bool hw_state;
1264 int sw_status;
1266 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1268 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1269 if (atp_rfk)
1270 atp_rfk->rfkill = rfkill_alloc(name,
1271 &tpacpi_pdev->dev,
1272 rfktype,
1273 &tpacpi_rfk_rfkill_ops,
1274 atp_rfk);
1275 if (!atp_rfk || !atp_rfk->rfkill) {
1276 pr_err("failed to allocate memory for rfkill class\n");
1277 kfree(atp_rfk);
1278 return -ENOMEM;
1281 atp_rfk->id = id;
1282 atp_rfk->ops = tp_rfkops;
1284 sw_status = (tp_rfkops->get_status)();
1285 if (sw_status < 0) {
1286 pr_err("failed to read initial state for %s, error %d\n",
1287 name, sw_status);
1288 } else {
1289 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1290 if (set_default) {
1291 /* try to keep the initial state, since we ask the
1292 * firmware to preserve it across S5 in NVRAM */
1293 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1296 hw_state = tpacpi_rfk_check_hwblock_state();
1297 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1299 res = rfkill_register(atp_rfk->rfkill);
1300 if (res < 0) {
1301 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1302 rfkill_destroy(atp_rfk->rfkill);
1303 kfree(atp_rfk);
1304 return res;
1307 tpacpi_rfkill_switches[id] = atp_rfk;
1309 pr_info("rfkill switch %s: radio is %sblocked\n",
1310 name, (sw_state || hw_state) ? "" : "un");
1311 return 0;
1314 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1316 struct tpacpi_rfk *tp_rfk;
1318 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1320 tp_rfk = tpacpi_rfkill_switches[id];
1321 if (tp_rfk) {
1322 rfkill_unregister(tp_rfk->rfkill);
1323 rfkill_destroy(tp_rfk->rfkill);
1324 tpacpi_rfkill_switches[id] = NULL;
1325 kfree(tp_rfk);
1329 static void printk_deprecated_rfkill_attribute(const char * const what)
1331 printk_deprecated_attribute(what,
1332 "Please switch to generic rfkill before year 2010");
1335 /* sysfs <radio> enable ------------------------------------------------ */
1336 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1337 struct device_attribute *attr,
1338 char *buf)
1340 int status;
1342 printk_deprecated_rfkill_attribute(attr->attr.name);
1344 /* This is in the ABI... */
1345 if (tpacpi_rfk_check_hwblock_state()) {
1346 status = TPACPI_RFK_RADIO_OFF;
1347 } else {
1348 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1349 if (status < 0)
1350 return status;
1353 return snprintf(buf, PAGE_SIZE, "%d\n",
1354 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1357 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1358 struct device_attribute *attr,
1359 const char *buf, size_t count)
1361 unsigned long t;
1362 int res;
1364 printk_deprecated_rfkill_attribute(attr->attr.name);
1366 if (parse_strtoul(buf, 1, &t))
1367 return -EINVAL;
1369 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1371 /* This is in the ABI... */
1372 if (tpacpi_rfk_check_hwblock_state() && !!t)
1373 return -EPERM;
1375 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1376 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1377 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1379 return (res < 0) ? res : count;
1382 /* procfs -------------------------------------------------------------- */
1383 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id,
1384 struct seq_file *m)
1386 if (id >= TPACPI_RFK_SW_MAX)
1387 seq_printf(m, "status:\t\tnot supported\n");
1388 else {
1389 int status;
1391 /* This is in the ABI... */
1392 if (tpacpi_rfk_check_hwblock_state()) {
1393 status = TPACPI_RFK_RADIO_OFF;
1394 } else {
1395 status = tpacpi_rfk_update_swstate(
1396 tpacpi_rfkill_switches[id]);
1397 if (status < 0)
1398 return status;
1401 seq_printf(m, "status:\t\t%s\n",
1402 (status == TPACPI_RFK_RADIO_ON) ?
1403 "enabled" : "disabled");
1404 seq_printf(m, "commands:\tenable, disable\n");
1407 return 0;
1410 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1412 char *cmd;
1413 int status = -1;
1414 int res = 0;
1416 if (id >= TPACPI_RFK_SW_MAX)
1417 return -ENODEV;
1419 while ((cmd = next_cmd(&buf))) {
1420 if (strlencmp(cmd, "enable") == 0)
1421 status = TPACPI_RFK_RADIO_ON;
1422 else if (strlencmp(cmd, "disable") == 0)
1423 status = TPACPI_RFK_RADIO_OFF;
1424 else
1425 return -EINVAL;
1428 if (status != -1) {
1429 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1430 (status == TPACPI_RFK_RADIO_ON) ?
1431 "enable" : "disable",
1432 tpacpi_rfkill_names[id]);
1433 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1434 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1437 return res;
1440 /*************************************************************************
1441 * thinkpad-acpi driver attributes
1444 /* interface_version --------------------------------------------------- */
1445 static ssize_t tpacpi_driver_interface_version_show(
1446 struct device_driver *drv,
1447 char *buf)
1449 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1452 static DRIVER_ATTR(interface_version, S_IRUGO,
1453 tpacpi_driver_interface_version_show, NULL);
1455 /* debug_level --------------------------------------------------------- */
1456 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1457 char *buf)
1459 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1462 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1463 const char *buf, size_t count)
1465 unsigned long t;
1467 if (parse_strtoul(buf, 0xffff, &t))
1468 return -EINVAL;
1470 dbg_level = t;
1472 return count;
1475 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1476 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1478 /* version ------------------------------------------------------------- */
1479 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1480 char *buf)
1482 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1483 TPACPI_DESC, TPACPI_VERSION);
1486 static DRIVER_ATTR(version, S_IRUGO,
1487 tpacpi_driver_version_show, NULL);
1489 /* --------------------------------------------------------------------- */
1491 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1493 /* wlsw_emulstate ------------------------------------------------------ */
1494 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1495 char *buf)
1497 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1500 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1501 const char *buf, size_t count)
1503 unsigned long t;
1505 if (parse_strtoul(buf, 1, &t))
1506 return -EINVAL;
1508 if (tpacpi_wlsw_emulstate != !!t) {
1509 tpacpi_wlsw_emulstate = !!t;
1510 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1513 return count;
1516 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1517 tpacpi_driver_wlsw_emulstate_show,
1518 tpacpi_driver_wlsw_emulstate_store);
1520 /* bluetooth_emulstate ------------------------------------------------- */
1521 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1522 struct device_driver *drv,
1523 char *buf)
1525 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1528 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1529 struct device_driver *drv,
1530 const char *buf, size_t count)
1532 unsigned long t;
1534 if (parse_strtoul(buf, 1, &t))
1535 return -EINVAL;
1537 tpacpi_bluetooth_emulstate = !!t;
1539 return count;
1542 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1543 tpacpi_driver_bluetooth_emulstate_show,
1544 tpacpi_driver_bluetooth_emulstate_store);
1546 /* wwan_emulstate ------------------------------------------------- */
1547 static ssize_t tpacpi_driver_wwan_emulstate_show(
1548 struct device_driver *drv,
1549 char *buf)
1551 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1554 static ssize_t tpacpi_driver_wwan_emulstate_store(
1555 struct device_driver *drv,
1556 const char *buf, size_t count)
1558 unsigned long t;
1560 if (parse_strtoul(buf, 1, &t))
1561 return -EINVAL;
1563 tpacpi_wwan_emulstate = !!t;
1565 return count;
1568 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1569 tpacpi_driver_wwan_emulstate_show,
1570 tpacpi_driver_wwan_emulstate_store);
1572 /* uwb_emulstate ------------------------------------------------- */
1573 static ssize_t tpacpi_driver_uwb_emulstate_show(
1574 struct device_driver *drv,
1575 char *buf)
1577 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1580 static ssize_t tpacpi_driver_uwb_emulstate_store(
1581 struct device_driver *drv,
1582 const char *buf, size_t count)
1584 unsigned long t;
1586 if (parse_strtoul(buf, 1, &t))
1587 return -EINVAL;
1589 tpacpi_uwb_emulstate = !!t;
1591 return count;
1594 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1595 tpacpi_driver_uwb_emulstate_show,
1596 tpacpi_driver_uwb_emulstate_store);
1597 #endif
1599 /* --------------------------------------------------------------------- */
1601 static struct driver_attribute *tpacpi_driver_attributes[] = {
1602 &driver_attr_debug_level, &driver_attr_version,
1603 &driver_attr_interface_version,
1606 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1608 int i, res;
1610 i = 0;
1611 res = 0;
1612 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1613 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1614 i++;
1617 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1618 if (!res && dbg_wlswemul)
1619 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1620 if (!res && dbg_bluetoothemul)
1621 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1622 if (!res && dbg_wwanemul)
1623 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1624 if (!res && dbg_uwbemul)
1625 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1626 #endif
1628 return res;
1631 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1633 int i;
1635 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1636 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1638 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1639 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1640 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1641 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1642 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1643 #endif
1646 /*************************************************************************
1647 * Firmware Data
1651 * Table of recommended minimum BIOS versions
1653 * Reasons for listing:
1654 * 1. Stable BIOS, listed because the unknown amount of
1655 * bugs and bad ACPI behaviour on older versions
1657 * 2. BIOS or EC fw with known bugs that trigger on Linux
1659 * 3. BIOS with known reduced functionality in older versions
1661 * We recommend the latest BIOS and EC version.
1662 * We only support the latest BIOS and EC fw version as a rule.
1664 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1665 * Information from users in ThinkWiki
1667 * WARNING: we use this table also to detect that the machine is
1668 * a ThinkPad in some cases, so don't remove entries lightly.
1671 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1672 { .vendor = (__v), \
1673 .bios = TPID(__id1, __id2), \
1674 .ec = TPACPI_MATCH_ANY, \
1675 .quirks = TPACPI_MATCH_ANY << 16 \
1676 | (__bv1) << 8 | (__bv2) }
1678 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1679 __eid, __ev1, __ev2) \
1680 { .vendor = (__v), \
1681 .bios = TPID(__bid1, __bid2), \
1682 .ec = __eid, \
1683 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1684 | (__bv1) << 8 | (__bv2) }
1686 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1687 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1689 /* Outdated IBM BIOSes often lack the EC id string */
1690 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1691 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1692 __bv1, __bv2, TPID(__id1, __id2), \
1693 __ev1, __ev2), \
1694 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1695 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1696 __ev1, __ev2)
1698 /* Outdated IBM BIOSes often lack the EC id string */
1699 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1700 __eid1, __eid2, __ev1, __ev2) \
1701 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1702 __bv1, __bv2, TPID(__eid1, __eid2), \
1703 __ev1, __ev2), \
1704 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1705 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1706 __ev1, __ev2)
1708 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1709 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1711 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1712 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1713 __bv1, __bv2, TPID(__id1, __id2), \
1714 __ev1, __ev2)
1716 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1717 __eid1, __eid2, __ev1, __ev2) \
1718 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1719 __bv1, __bv2, TPID(__eid1, __eid2), \
1720 __ev1, __ev2)
1722 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1723 /* Numeric models ------------------ */
1724 /* FW MODEL BIOS VERS */
1725 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1726 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1727 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1728 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1729 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1730 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1731 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1732 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1733 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1735 /* A-series ------------------------- */
1736 /* FW MODEL BIOS VERS EC VERS */
1737 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1738 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1739 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1740 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1741 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1742 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1743 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1744 TPV_QI0('1', '3', '2', '0'), /* A22m */
1745 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1746 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1747 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1749 /* G-series ------------------------- */
1750 /* FW MODEL BIOS VERS */
1751 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1752 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1754 /* R-series, T-series --------------- */
1755 /* FW MODEL BIOS VERS EC VERS */
1756 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1757 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1758 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1759 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1760 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1761 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1762 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1763 T40/p, T41/p, T42/p (1) */
1764 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1765 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1766 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1767 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1769 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1770 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1771 TPV_QI0('1', '6', '3', '2'), /* T22 */
1772 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1773 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1774 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1776 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1777 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1778 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1780 /* BIOS FW BIOS VERS EC FW EC VERS */
1781 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1782 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1784 /* X-series ------------------------- */
1785 /* FW MODEL BIOS VERS EC VERS */
1786 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1787 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1788 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1789 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1790 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1791 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1792 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1794 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1795 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1797 /* (0) - older versions lack DMI EC fw string and functionality */
1798 /* (1) - older versions known to lack functionality */
1801 #undef TPV_QL1
1802 #undef TPV_QL0
1803 #undef TPV_QI2
1804 #undef TPV_QI1
1805 #undef TPV_QI0
1806 #undef TPV_Q_X
1807 #undef TPV_Q
1809 static void __init tpacpi_check_outdated_fw(void)
1811 unsigned long fwvers;
1812 u16 ec_version, bios_version;
1814 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1815 ARRAY_SIZE(tpacpi_bios_version_qtable));
1817 if (!fwvers)
1818 return;
1820 bios_version = fwvers & 0xffffU;
1821 ec_version = (fwvers >> 16) & 0xffffU;
1823 /* note that unknown versions are set to 0x0000 and we use that */
1824 if ((bios_version > thinkpad_id.bios_release) ||
1825 (ec_version > thinkpad_id.ec_release &&
1826 ec_version != TPACPI_MATCH_ANY)) {
1828 * The changelogs would let us track down the exact
1829 * reason, but it is just too much of a pain to track
1830 * it. We only list BIOSes that are either really
1831 * broken, or really stable to begin with, so it is
1832 * best if the user upgrades the firmware anyway.
1834 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1835 pr_warn("WARNING: This firmware may be missing critical bug "
1836 "fixes and/or important features\n");
1840 static bool __init tpacpi_is_fw_known(void)
1842 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1843 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1846 /****************************************************************************
1847 ****************************************************************************
1849 * Subdrivers
1851 ****************************************************************************
1852 ****************************************************************************/
1854 /*************************************************************************
1855 * thinkpad-acpi metadata subdriver
1858 static int thinkpad_acpi_driver_read(struct seq_file *m)
1860 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1861 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1862 return 0;
1865 static struct ibm_struct thinkpad_acpi_driver_data = {
1866 .name = "driver",
1867 .read = thinkpad_acpi_driver_read,
1870 /*************************************************************************
1871 * Hotkey subdriver
1875 * ThinkPad firmware event model
1877 * The ThinkPad firmware has two main event interfaces: normal ACPI
1878 * notifications (which follow the ACPI standard), and a private event
1879 * interface.
1881 * The private event interface also issues events for the hotkeys. As
1882 * the driver gained features, the event handling code ended up being
1883 * built around the hotkey subdriver. This will need to be refactored
1884 * to a more formal event API eventually.
1886 * Some "hotkeys" are actually supposed to be used as event reports,
1887 * such as "brightness has changed", "volume has changed", depending on
1888 * the ThinkPad model and how the firmware is operating.
1890 * Unlike other classes, hotkey-class events have mask/unmask control on
1891 * non-ancient firmware. However, how it behaves changes a lot with the
1892 * firmware model and version.
1895 enum { /* hot key scan codes (derived from ACPI DSDT) */
1896 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1897 TP_ACPI_HOTKEYSCAN_FNF2,
1898 TP_ACPI_HOTKEYSCAN_FNF3,
1899 TP_ACPI_HOTKEYSCAN_FNF4,
1900 TP_ACPI_HOTKEYSCAN_FNF5,
1901 TP_ACPI_HOTKEYSCAN_FNF6,
1902 TP_ACPI_HOTKEYSCAN_FNF7,
1903 TP_ACPI_HOTKEYSCAN_FNF8,
1904 TP_ACPI_HOTKEYSCAN_FNF9,
1905 TP_ACPI_HOTKEYSCAN_FNF10,
1906 TP_ACPI_HOTKEYSCAN_FNF11,
1907 TP_ACPI_HOTKEYSCAN_FNF12,
1908 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1909 TP_ACPI_HOTKEYSCAN_FNINSERT,
1910 TP_ACPI_HOTKEYSCAN_FNDELETE,
1911 TP_ACPI_HOTKEYSCAN_FNHOME,
1912 TP_ACPI_HOTKEYSCAN_FNEND,
1913 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1914 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1915 TP_ACPI_HOTKEYSCAN_FNSPACE,
1916 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1917 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1918 TP_ACPI_HOTKEYSCAN_MUTE,
1919 TP_ACPI_HOTKEYSCAN_THINKPAD,
1920 TP_ACPI_HOTKEYSCAN_UNK1,
1921 TP_ACPI_HOTKEYSCAN_UNK2,
1922 TP_ACPI_HOTKEYSCAN_UNK3,
1923 TP_ACPI_HOTKEYSCAN_UNK4,
1924 TP_ACPI_HOTKEYSCAN_UNK5,
1925 TP_ACPI_HOTKEYSCAN_UNK6,
1926 TP_ACPI_HOTKEYSCAN_UNK7,
1927 TP_ACPI_HOTKEYSCAN_UNK8,
1929 /* Hotkey keymap size */
1930 TPACPI_HOTKEY_MAP_LEN
1933 enum { /* Keys/events available through NVRAM polling */
1934 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1935 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1938 enum { /* Positions of some of the keys in hotkey masks */
1939 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1940 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1941 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1942 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1943 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1944 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1945 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1946 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1947 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1948 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1949 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1952 enum { /* NVRAM to ACPI HKEY group map */
1953 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1954 TP_ACPI_HKEY_ZOOM_MASK |
1955 TP_ACPI_HKEY_DISPSWTCH_MASK |
1956 TP_ACPI_HKEY_HIBERNATE_MASK,
1957 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1958 TP_ACPI_HKEY_BRGHTDWN_MASK,
1959 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1960 TP_ACPI_HKEY_VOLDWN_MASK |
1961 TP_ACPI_HKEY_MUTE_MASK,
1964 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1965 struct tp_nvram_state {
1966 u16 thinkpad_toggle:1;
1967 u16 zoom_toggle:1;
1968 u16 display_toggle:1;
1969 u16 thinklight_toggle:1;
1970 u16 hibernate_toggle:1;
1971 u16 displayexp_toggle:1;
1972 u16 display_state:1;
1973 u16 brightness_toggle:1;
1974 u16 volume_toggle:1;
1975 u16 mute:1;
1977 u8 brightness_level;
1978 u8 volume_level;
1981 /* kthread for the hotkey poller */
1982 static struct task_struct *tpacpi_hotkey_task;
1984 /* Acquired while the poller kthread is running, use to sync start/stop */
1985 static struct mutex hotkey_thread_mutex;
1988 * Acquire mutex to write poller control variables as an
1989 * atomic block.
1991 * Increment hotkey_config_change when changing them if you
1992 * want the kthread to forget old state.
1994 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1996 static struct mutex hotkey_thread_data_mutex;
1997 static unsigned int hotkey_config_change;
2000 * hotkey poller control variables
2002 * Must be atomic or readers will also need to acquire mutex
2004 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2005 * should be used only when the changes need to be taken as
2006 * a block, OR when one needs to force the kthread to forget
2007 * old state.
2009 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2010 static unsigned int hotkey_poll_freq = 10; /* Hz */
2012 #define HOTKEY_CONFIG_CRITICAL_START \
2013 do { \
2014 mutex_lock(&hotkey_thread_data_mutex); \
2015 hotkey_config_change++; \
2016 } while (0);
2017 #define HOTKEY_CONFIG_CRITICAL_END \
2018 mutex_unlock(&hotkey_thread_data_mutex);
2020 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2022 #define hotkey_source_mask 0U
2023 #define HOTKEY_CONFIG_CRITICAL_START
2024 #define HOTKEY_CONFIG_CRITICAL_END
2026 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2028 static struct mutex hotkey_mutex;
2030 static enum { /* Reasons for waking up */
2031 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2032 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2033 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2034 } hotkey_wakeup_reason;
2036 static int hotkey_autosleep_ack;
2038 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2039 static u32 hotkey_all_mask; /* all events supported in fw */
2040 static u32 hotkey_reserved_mask; /* events better left disabled */
2041 static u32 hotkey_driver_mask; /* events needed by the driver */
2042 static u32 hotkey_user_mask; /* events visible to userspace */
2043 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2045 static unsigned int hotkey_report_mode;
2047 static u16 *hotkey_keycode_map;
2049 static struct attribute_set *hotkey_dev_attributes;
2051 static void tpacpi_driver_event(const unsigned int hkey_event);
2052 static void hotkey_driver_event(const unsigned int scancode);
2053 static void hotkey_poll_setup(const bool may_warn);
2055 /* HKEY.MHKG() return bits */
2056 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2058 static int hotkey_get_wlsw(void)
2060 int status;
2062 if (!tp_features.hotkey_wlsw)
2063 return -ENODEV;
2065 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2066 if (dbg_wlswemul)
2067 return (tpacpi_wlsw_emulstate) ?
2068 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2069 #endif
2071 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2072 return -EIO;
2074 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2077 static int hotkey_get_tablet_mode(int *status)
2079 int s;
2081 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2082 return -EIO;
2084 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2085 return 0;
2089 * Reads current event mask from firmware, and updates
2090 * hotkey_acpi_mask accordingly. Also resets any bits
2091 * from hotkey_user_mask that are unavailable to be
2092 * delivered (shadow requirement of the userspace ABI).
2094 * Call with hotkey_mutex held
2096 static int hotkey_mask_get(void)
2098 if (tp_features.hotkey_mask) {
2099 u32 m = 0;
2101 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2102 return -EIO;
2104 hotkey_acpi_mask = m;
2105 } else {
2106 /* no mask support doesn't mean no event support... */
2107 hotkey_acpi_mask = hotkey_all_mask;
2110 /* sync userspace-visible mask */
2111 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2113 return 0;
2116 void static hotkey_mask_warn_incomplete_mask(void)
2118 /* log only what the user can fix... */
2119 const u32 wantedmask = hotkey_driver_mask &
2120 ~(hotkey_acpi_mask | hotkey_source_mask) &
2121 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2123 if (wantedmask)
2124 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2128 * Set the firmware mask when supported
2130 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2132 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2134 * Call with hotkey_mutex held
2136 static int hotkey_mask_set(u32 mask)
2138 int i;
2139 int rc = 0;
2141 const u32 fwmask = mask & ~hotkey_source_mask;
2143 if (tp_features.hotkey_mask) {
2144 for (i = 0; i < 32; i++) {
2145 if (!acpi_evalf(hkey_handle,
2146 NULL, "MHKM", "vdd", i + 1,
2147 !!(mask & (1 << i)))) {
2148 rc = -EIO;
2149 break;
2155 * We *must* make an inconditional call to hotkey_mask_get to
2156 * refresh hotkey_acpi_mask and update hotkey_user_mask
2158 * Take the opportunity to also log when we cannot _enable_
2159 * a given event.
2161 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2162 pr_notice("asked for hotkey mask 0x%08x, but "
2163 "firmware forced it to 0x%08x\n",
2164 fwmask, hotkey_acpi_mask);
2167 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2168 hotkey_mask_warn_incomplete_mask();
2170 return rc;
2174 * Sets hotkey_user_mask and tries to set the firmware mask
2176 * Call with hotkey_mutex held
2178 static int hotkey_user_mask_set(const u32 mask)
2180 int rc;
2182 /* Give people a chance to notice they are doing something that
2183 * is bound to go boom on their users sooner or later */
2184 if (!tp_warned.hotkey_mask_ff &&
2185 (mask == 0xffff || mask == 0xffffff ||
2186 mask == 0xffffffff)) {
2187 tp_warned.hotkey_mask_ff = 1;
2188 pr_notice("setting the hotkey mask to 0x%08x is likely "
2189 "not the best way to go about it\n", mask);
2190 pr_notice("please consider using the driver defaults, "
2191 "and refer to up-to-date thinkpad-acpi "
2192 "documentation\n");
2195 /* Try to enable what the user asked for, plus whatever we need.
2196 * this syncs everything but won't enable bits in hotkey_user_mask */
2197 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2199 /* Enable the available bits in hotkey_user_mask */
2200 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2202 return rc;
2206 * Sets the driver hotkey mask.
2208 * Can be called even if the hotkey subdriver is inactive
2210 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2212 int rc;
2214 /* Do the right thing if hotkey_init has not been called yet */
2215 if (!tp_features.hotkey) {
2216 hotkey_driver_mask = mask;
2217 return 0;
2220 mutex_lock(&hotkey_mutex);
2222 HOTKEY_CONFIG_CRITICAL_START
2223 hotkey_driver_mask = mask;
2224 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2225 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2226 #endif
2227 HOTKEY_CONFIG_CRITICAL_END
2229 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2230 ~hotkey_source_mask);
2231 hotkey_poll_setup(true);
2233 mutex_unlock(&hotkey_mutex);
2235 return rc;
2238 static int hotkey_status_get(int *status)
2240 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2241 return -EIO;
2243 return 0;
2246 static int hotkey_status_set(bool enable)
2248 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2249 return -EIO;
2251 return 0;
2254 static void tpacpi_input_send_tabletsw(void)
2256 int state;
2258 if (tp_features.hotkey_tablet &&
2259 !hotkey_get_tablet_mode(&state)) {
2260 mutex_lock(&tpacpi_inputdev_send_mutex);
2262 input_report_switch(tpacpi_inputdev,
2263 SW_TABLET_MODE, !!state);
2264 input_sync(tpacpi_inputdev);
2266 mutex_unlock(&tpacpi_inputdev_send_mutex);
2270 /* Do NOT call without validating scancode first */
2271 static void tpacpi_input_send_key(const unsigned int scancode)
2273 const unsigned int keycode = hotkey_keycode_map[scancode];
2275 if (keycode != KEY_RESERVED) {
2276 mutex_lock(&tpacpi_inputdev_send_mutex);
2278 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2279 input_report_key(tpacpi_inputdev, keycode, 1);
2280 input_sync(tpacpi_inputdev);
2282 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2283 input_report_key(tpacpi_inputdev, keycode, 0);
2284 input_sync(tpacpi_inputdev);
2286 mutex_unlock(&tpacpi_inputdev_send_mutex);
2290 /* Do NOT call without validating scancode first */
2291 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2293 hotkey_driver_event(scancode);
2294 if (hotkey_user_mask & (1 << scancode))
2295 tpacpi_input_send_key(scancode);
2298 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2299 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2301 /* Do NOT call without validating scancode first */
2302 static void tpacpi_hotkey_send_key(unsigned int scancode)
2304 tpacpi_input_send_key_masked(scancode);
2305 if (hotkey_report_mode < 2) {
2306 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2307 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2311 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2313 u8 d;
2315 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2316 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2317 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2318 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2319 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2320 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2322 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2323 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2324 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2326 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2327 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2328 n->displayexp_toggle =
2329 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2331 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2332 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2333 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2334 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2335 n->brightness_toggle =
2336 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2338 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2339 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2340 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2341 >> TP_NVRAM_POS_LEVEL_VOLUME;
2342 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2343 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2347 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2348 struct tp_nvram_state *newn,
2349 const u32 event_mask)
2352 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2353 do { \
2354 if ((event_mask & (1 << __scancode)) && \
2355 oldn->__member != newn->__member) \
2356 tpacpi_hotkey_send_key(__scancode); \
2357 } while (0)
2359 #define TPACPI_MAY_SEND_KEY(__scancode) \
2360 do { \
2361 if (event_mask & (1 << __scancode)) \
2362 tpacpi_hotkey_send_key(__scancode); \
2363 } while (0)
2365 void issue_volchange(const unsigned int oldvol,
2366 const unsigned int newvol)
2368 unsigned int i = oldvol;
2370 while (i > newvol) {
2371 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2372 i--;
2374 while (i < newvol) {
2375 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2376 i++;
2380 void issue_brightnesschange(const unsigned int oldbrt,
2381 const unsigned int newbrt)
2383 unsigned int i = oldbrt;
2385 while (i > newbrt) {
2386 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2387 i--;
2389 while (i < newbrt) {
2390 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2391 i++;
2395 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2396 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2397 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2398 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2400 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2402 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2405 * Handle volume
2407 * This code is supposed to duplicate the IBM firmware behaviour:
2408 * - Pressing MUTE issues mute hotkey message, even when already mute
2409 * - Pressing Volume up/down issues volume up/down hotkey messages,
2410 * even when already at maximum or minimum volume
2411 * - The act of unmuting issues volume up/down notification,
2412 * depending which key was used to unmute
2414 * We are constrained to what the NVRAM can tell us, which is not much
2415 * and certainly not enough if more than one volume hotkey was pressed
2416 * since the last poll cycle.
2418 * Just to make our life interesting, some newer Lenovo ThinkPads have
2419 * bugs in the BIOS and may fail to update volume_toggle properly.
2421 if (newn->mute) {
2422 /* muted */
2423 if (!oldn->mute ||
2424 oldn->volume_toggle != newn->volume_toggle ||
2425 oldn->volume_level != newn->volume_level) {
2426 /* recently muted, or repeated mute keypress, or
2427 * multiple presses ending in mute */
2428 issue_volchange(oldn->volume_level, newn->volume_level);
2429 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2431 } else {
2432 /* unmute */
2433 if (oldn->mute) {
2434 /* recently unmuted, issue 'unmute' keypress */
2435 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2437 if (oldn->volume_level != newn->volume_level) {
2438 issue_volchange(oldn->volume_level, newn->volume_level);
2439 } else if (oldn->volume_toggle != newn->volume_toggle) {
2440 /* repeated vol up/down keypress at end of scale ? */
2441 if (newn->volume_level == 0)
2442 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2443 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2444 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2448 /* handle brightness */
2449 if (oldn->brightness_level != newn->brightness_level) {
2450 issue_brightnesschange(oldn->brightness_level,
2451 newn->brightness_level);
2452 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2453 /* repeated key presses that didn't change state */
2454 if (newn->brightness_level == 0)
2455 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2456 else if (newn->brightness_level >= bright_maxlvl
2457 && !tp_features.bright_unkfw)
2458 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2461 #undef TPACPI_COMPARE_KEY
2462 #undef TPACPI_MAY_SEND_KEY
2466 * Polling driver
2468 * We track all events in hotkey_source_mask all the time, since
2469 * most of them are edge-based. We only issue those requested by
2470 * hotkey_user_mask or hotkey_driver_mask, though.
2472 static int hotkey_kthread(void *data)
2474 struct tp_nvram_state s[2];
2475 u32 poll_mask, event_mask;
2476 unsigned int si, so;
2477 unsigned long t;
2478 unsigned int change_detector, must_reset;
2479 unsigned int poll_freq;
2481 mutex_lock(&hotkey_thread_mutex);
2483 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2484 goto exit;
2486 set_freezable();
2488 so = 0;
2489 si = 1;
2490 t = 0;
2492 /* Initial state for compares */
2493 mutex_lock(&hotkey_thread_data_mutex);
2494 change_detector = hotkey_config_change;
2495 poll_mask = hotkey_source_mask;
2496 event_mask = hotkey_source_mask &
2497 (hotkey_driver_mask | hotkey_user_mask);
2498 poll_freq = hotkey_poll_freq;
2499 mutex_unlock(&hotkey_thread_data_mutex);
2500 hotkey_read_nvram(&s[so], poll_mask);
2502 while (!kthread_should_stop()) {
2503 if (t == 0) {
2504 if (likely(poll_freq))
2505 t = 1000/poll_freq;
2506 else
2507 t = 100; /* should never happen... */
2509 t = msleep_interruptible(t);
2510 if (unlikely(kthread_should_stop()))
2511 break;
2512 must_reset = try_to_freeze();
2513 if (t > 0 && !must_reset)
2514 continue;
2516 mutex_lock(&hotkey_thread_data_mutex);
2517 if (must_reset || hotkey_config_change != change_detector) {
2518 /* forget old state on thaw or config change */
2519 si = so;
2520 t = 0;
2521 change_detector = hotkey_config_change;
2523 poll_mask = hotkey_source_mask;
2524 event_mask = hotkey_source_mask &
2525 (hotkey_driver_mask | hotkey_user_mask);
2526 poll_freq = hotkey_poll_freq;
2527 mutex_unlock(&hotkey_thread_data_mutex);
2529 if (likely(poll_mask)) {
2530 hotkey_read_nvram(&s[si], poll_mask);
2531 if (likely(si != so)) {
2532 hotkey_compare_and_issue_event(&s[so], &s[si],
2533 event_mask);
2537 so = si;
2538 si ^= 1;
2541 exit:
2542 mutex_unlock(&hotkey_thread_mutex);
2543 return 0;
2546 /* call with hotkey_mutex held */
2547 static void hotkey_poll_stop_sync(void)
2549 if (tpacpi_hotkey_task) {
2550 if (frozen(tpacpi_hotkey_task) ||
2551 freezing(tpacpi_hotkey_task))
2552 thaw_process(tpacpi_hotkey_task);
2554 kthread_stop(tpacpi_hotkey_task);
2555 tpacpi_hotkey_task = NULL;
2556 mutex_lock(&hotkey_thread_mutex);
2557 /* at this point, the thread did exit */
2558 mutex_unlock(&hotkey_thread_mutex);
2562 /* call with hotkey_mutex held */
2563 static void hotkey_poll_setup(const bool may_warn)
2565 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2566 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2568 if (hotkey_poll_freq > 0 &&
2569 (poll_driver_mask ||
2570 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2571 if (!tpacpi_hotkey_task) {
2572 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2573 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2574 if (IS_ERR(tpacpi_hotkey_task)) {
2575 tpacpi_hotkey_task = NULL;
2576 pr_err("could not create kernel thread "
2577 "for hotkey polling\n");
2580 } else {
2581 hotkey_poll_stop_sync();
2582 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2583 hotkey_poll_freq == 0) {
2584 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2585 "require polling, which is currently "
2586 "disabled\n",
2587 poll_user_mask, poll_driver_mask);
2592 static void hotkey_poll_setup_safe(const bool may_warn)
2594 mutex_lock(&hotkey_mutex);
2595 hotkey_poll_setup(may_warn);
2596 mutex_unlock(&hotkey_mutex);
2599 /* call with hotkey_mutex held */
2600 static void hotkey_poll_set_freq(unsigned int freq)
2602 if (!freq)
2603 hotkey_poll_stop_sync();
2605 hotkey_poll_freq = freq;
2608 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2610 static void hotkey_poll_setup(const bool __unused)
2614 static void hotkey_poll_setup_safe(const bool __unused)
2618 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2620 static int hotkey_inputdev_open(struct input_dev *dev)
2622 switch (tpacpi_lifecycle) {
2623 case TPACPI_LIFE_INIT:
2624 case TPACPI_LIFE_RUNNING:
2625 hotkey_poll_setup_safe(false);
2626 return 0;
2627 case TPACPI_LIFE_EXITING:
2628 return -EBUSY;
2631 /* Should only happen if tpacpi_lifecycle is corrupt */
2632 BUG();
2633 return -EBUSY;
2636 static void hotkey_inputdev_close(struct input_dev *dev)
2638 /* disable hotkey polling when possible */
2639 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2640 !(hotkey_source_mask & hotkey_driver_mask))
2641 hotkey_poll_setup_safe(false);
2644 /* sysfs hotkey enable ------------------------------------------------- */
2645 static ssize_t hotkey_enable_show(struct device *dev,
2646 struct device_attribute *attr,
2647 char *buf)
2649 int res, status;
2651 printk_deprecated_attribute("hotkey_enable",
2652 "Hotkey reporting is always enabled");
2654 res = hotkey_status_get(&status);
2655 if (res)
2656 return res;
2658 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2661 static ssize_t hotkey_enable_store(struct device *dev,
2662 struct device_attribute *attr,
2663 const char *buf, size_t count)
2665 unsigned long t;
2667 printk_deprecated_attribute("hotkey_enable",
2668 "Hotkeys can be disabled through hotkey_mask");
2670 if (parse_strtoul(buf, 1, &t))
2671 return -EINVAL;
2673 if (t == 0)
2674 return -EPERM;
2676 return count;
2679 static struct device_attribute dev_attr_hotkey_enable =
2680 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2681 hotkey_enable_show, hotkey_enable_store);
2683 /* sysfs hotkey mask --------------------------------------------------- */
2684 static ssize_t hotkey_mask_show(struct device *dev,
2685 struct device_attribute *attr,
2686 char *buf)
2688 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2691 static ssize_t hotkey_mask_store(struct device *dev,
2692 struct device_attribute *attr,
2693 const char *buf, size_t count)
2695 unsigned long t;
2696 int res;
2698 if (parse_strtoul(buf, 0xffffffffUL, &t))
2699 return -EINVAL;
2701 if (mutex_lock_killable(&hotkey_mutex))
2702 return -ERESTARTSYS;
2704 res = hotkey_user_mask_set(t);
2706 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2707 hotkey_poll_setup(true);
2708 #endif
2710 mutex_unlock(&hotkey_mutex);
2712 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2714 return (res) ? res : count;
2717 static struct device_attribute dev_attr_hotkey_mask =
2718 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2719 hotkey_mask_show, hotkey_mask_store);
2721 /* sysfs hotkey bios_enabled ------------------------------------------- */
2722 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2723 struct device_attribute *attr,
2724 char *buf)
2726 return sprintf(buf, "0\n");
2729 static struct device_attribute dev_attr_hotkey_bios_enabled =
2730 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2732 /* sysfs hotkey bios_mask ---------------------------------------------- */
2733 static ssize_t hotkey_bios_mask_show(struct device *dev,
2734 struct device_attribute *attr,
2735 char *buf)
2737 printk_deprecated_attribute("hotkey_bios_mask",
2738 "This attribute is useless.");
2739 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2742 static struct device_attribute dev_attr_hotkey_bios_mask =
2743 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2745 /* sysfs hotkey all_mask ----------------------------------------------- */
2746 static ssize_t hotkey_all_mask_show(struct device *dev,
2747 struct device_attribute *attr,
2748 char *buf)
2750 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2751 hotkey_all_mask | hotkey_source_mask);
2754 static struct device_attribute dev_attr_hotkey_all_mask =
2755 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2757 /* sysfs hotkey recommended_mask --------------------------------------- */
2758 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2759 struct device_attribute *attr,
2760 char *buf)
2762 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2763 (hotkey_all_mask | hotkey_source_mask)
2764 & ~hotkey_reserved_mask);
2767 static struct device_attribute dev_attr_hotkey_recommended_mask =
2768 __ATTR(hotkey_recommended_mask, S_IRUGO,
2769 hotkey_recommended_mask_show, NULL);
2771 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2773 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2774 static ssize_t hotkey_source_mask_show(struct device *dev,
2775 struct device_attribute *attr,
2776 char *buf)
2778 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2781 static ssize_t hotkey_source_mask_store(struct device *dev,
2782 struct device_attribute *attr,
2783 const char *buf, size_t count)
2785 unsigned long t;
2786 u32 r_ev;
2787 int rc;
2789 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2790 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2791 return -EINVAL;
2793 if (mutex_lock_killable(&hotkey_mutex))
2794 return -ERESTARTSYS;
2796 HOTKEY_CONFIG_CRITICAL_START
2797 hotkey_source_mask = t;
2798 HOTKEY_CONFIG_CRITICAL_END
2800 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2801 ~hotkey_source_mask);
2802 hotkey_poll_setup(true);
2804 /* check if events needed by the driver got disabled */
2805 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2806 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2808 mutex_unlock(&hotkey_mutex);
2810 if (rc < 0)
2811 pr_err("hotkey_source_mask: "
2812 "failed to update the firmware event mask!\n");
2814 if (r_ev)
2815 pr_notice("hotkey_source_mask: "
2816 "some important events were disabled: 0x%04x\n",
2817 r_ev);
2819 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2821 return (rc < 0) ? rc : count;
2824 static struct device_attribute dev_attr_hotkey_source_mask =
2825 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2826 hotkey_source_mask_show, hotkey_source_mask_store);
2828 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2829 static ssize_t hotkey_poll_freq_show(struct device *dev,
2830 struct device_attribute *attr,
2831 char *buf)
2833 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2836 static ssize_t hotkey_poll_freq_store(struct device *dev,
2837 struct device_attribute *attr,
2838 const char *buf, size_t count)
2840 unsigned long t;
2842 if (parse_strtoul(buf, 25, &t))
2843 return -EINVAL;
2845 if (mutex_lock_killable(&hotkey_mutex))
2846 return -ERESTARTSYS;
2848 hotkey_poll_set_freq(t);
2849 hotkey_poll_setup(true);
2851 mutex_unlock(&hotkey_mutex);
2853 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2855 return count;
2858 static struct device_attribute dev_attr_hotkey_poll_freq =
2859 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2860 hotkey_poll_freq_show, hotkey_poll_freq_store);
2862 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2864 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2865 static ssize_t hotkey_radio_sw_show(struct device *dev,
2866 struct device_attribute *attr,
2867 char *buf)
2869 int res;
2870 res = hotkey_get_wlsw();
2871 if (res < 0)
2872 return res;
2874 /* Opportunistic update */
2875 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2877 return snprintf(buf, PAGE_SIZE, "%d\n",
2878 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2881 static struct device_attribute dev_attr_hotkey_radio_sw =
2882 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2884 static void hotkey_radio_sw_notify_change(void)
2886 if (tp_features.hotkey_wlsw)
2887 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2888 "hotkey_radio_sw");
2891 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2892 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2893 struct device_attribute *attr,
2894 char *buf)
2896 int res, s;
2897 res = hotkey_get_tablet_mode(&s);
2898 if (res < 0)
2899 return res;
2901 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2904 static struct device_attribute dev_attr_hotkey_tablet_mode =
2905 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2907 static void hotkey_tablet_mode_notify_change(void)
2909 if (tp_features.hotkey_tablet)
2910 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2911 "hotkey_tablet_mode");
2914 /* sysfs hotkey report_mode -------------------------------------------- */
2915 static ssize_t hotkey_report_mode_show(struct device *dev,
2916 struct device_attribute *attr,
2917 char *buf)
2919 return snprintf(buf, PAGE_SIZE, "%d\n",
2920 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2923 static struct device_attribute dev_attr_hotkey_report_mode =
2924 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2926 /* sysfs wakeup reason (pollable) -------------------------------------- */
2927 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2928 struct device_attribute *attr,
2929 char *buf)
2931 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2934 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2935 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2937 static void hotkey_wakeup_reason_notify_change(void)
2939 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2940 "wakeup_reason");
2943 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2944 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2945 struct device_attribute *attr,
2946 char *buf)
2948 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2951 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2952 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2953 hotkey_wakeup_hotunplug_complete_show, NULL);
2955 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2957 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2958 "wakeup_hotunplug_complete");
2961 /* --------------------------------------------------------------------- */
2963 static struct attribute *hotkey_attributes[] __initdata = {
2964 &dev_attr_hotkey_enable.attr,
2965 &dev_attr_hotkey_bios_enabled.attr,
2966 &dev_attr_hotkey_bios_mask.attr,
2967 &dev_attr_hotkey_report_mode.attr,
2968 &dev_attr_hotkey_wakeup_reason.attr,
2969 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2970 &dev_attr_hotkey_mask.attr,
2971 &dev_attr_hotkey_all_mask.attr,
2972 &dev_attr_hotkey_recommended_mask.attr,
2973 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2974 &dev_attr_hotkey_source_mask.attr,
2975 &dev_attr_hotkey_poll_freq.attr,
2976 #endif
2980 * Sync both the hw and sw blocking state of all switches
2982 static void tpacpi_send_radiosw_update(void)
2984 int wlsw;
2987 * We must sync all rfkill controllers *before* issuing any
2988 * rfkill input events, or we will race the rfkill core input
2989 * handler.
2991 * tpacpi_inputdev_send_mutex works as a synchronization point
2992 * for the above.
2994 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2997 wlsw = hotkey_get_wlsw();
2999 /* Sync hw blocking state first if it is hw-blocked */
3000 if (wlsw == TPACPI_RFK_RADIO_OFF)
3001 tpacpi_rfk_update_hwblock_state(true);
3003 /* Sync sw blocking state */
3004 tpacpi_rfk_update_swstate_all();
3006 /* Sync hw blocking state last if it is hw-unblocked */
3007 if (wlsw == TPACPI_RFK_RADIO_ON)
3008 tpacpi_rfk_update_hwblock_state(false);
3010 /* Issue rfkill input event for WLSW switch */
3011 if (!(wlsw < 0)) {
3012 mutex_lock(&tpacpi_inputdev_send_mutex);
3014 input_report_switch(tpacpi_inputdev,
3015 SW_RFKILL_ALL, (wlsw > 0));
3016 input_sync(tpacpi_inputdev);
3018 mutex_unlock(&tpacpi_inputdev_send_mutex);
3022 * this can be unconditional, as we will poll state again
3023 * if userspace uses the notify to read data
3025 hotkey_radio_sw_notify_change();
3028 static void hotkey_exit(void)
3030 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3031 mutex_lock(&hotkey_mutex);
3032 hotkey_poll_stop_sync();
3033 mutex_unlock(&hotkey_mutex);
3034 #endif
3036 if (hotkey_dev_attributes)
3037 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3039 kfree(hotkey_keycode_map);
3041 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3042 "restoring original HKEY status and mask\n");
3043 /* yes, there is a bitwise or below, we want the
3044 * functions to be called even if one of them fail */
3045 if (((tp_features.hotkey_mask &&
3046 hotkey_mask_set(hotkey_orig_mask)) |
3047 hotkey_status_set(false)) != 0)
3048 pr_err("failed to restore hot key mask "
3049 "to BIOS defaults\n");
3052 static void __init hotkey_unmap(const unsigned int scancode)
3054 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3055 clear_bit(hotkey_keycode_map[scancode],
3056 tpacpi_inputdev->keybit);
3057 hotkey_keycode_map[scancode] = KEY_RESERVED;
3062 * HKEY quirks:
3063 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3066 #define TPACPI_HK_Q_INIMASK 0x0001
3068 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3069 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3070 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3071 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3072 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3073 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3074 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3075 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3076 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3077 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3078 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3079 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3080 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3081 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3082 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3083 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3084 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3085 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3086 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3087 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3090 typedef u16 tpacpi_keymap_entry_t;
3091 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3093 static int __init hotkey_init(struct ibm_init_struct *iibm)
3095 /* Requirements for changing the default keymaps:
3097 * 1. Many of the keys are mapped to KEY_RESERVED for very
3098 * good reasons. Do not change them unless you have deep
3099 * knowledge on the IBM and Lenovo ThinkPad firmware for
3100 * the various ThinkPad models. The driver behaves
3101 * differently for KEY_RESERVED: such keys have their
3102 * hot key mask *unset* in mask_recommended, and also
3103 * in the initial hot key mask programmed into the
3104 * firmware at driver load time, which means the firm-
3105 * ware may react very differently if you change them to
3106 * something else;
3108 * 2. You must be subscribed to the linux-thinkpad and
3109 * ibm-acpi-devel mailing lists, and you should read the
3110 * list archives since 2007 if you want to change the
3111 * keymaps. This requirement exists so that you will
3112 * know the past history of problems with the thinkpad-
3113 * acpi driver keymaps, and also that you will be
3114 * listening to any bug reports;
3116 * 3. Do not send thinkpad-acpi specific patches directly to
3117 * for merging, *ever*. Send them to the linux-acpi
3118 * mailinglist for comments. Merging is to be done only
3119 * through acpi-test and the ACPI maintainer.
3121 * If the above is too much to ask, don't change the keymap.
3122 * Ask the thinkpad-acpi maintainer to do it, instead.
3125 enum keymap_index {
3126 TPACPI_KEYMAP_IBM_GENERIC = 0,
3127 TPACPI_KEYMAP_LENOVO_GENERIC,
3130 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3131 /* Generic keymap for IBM ThinkPads */
3132 [TPACPI_KEYMAP_IBM_GENERIC] = {
3133 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3134 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3135 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3136 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3138 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3139 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3140 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3141 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3143 /* brightness: firmware always reacts to them */
3144 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3145 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3147 /* Thinklight: firmware always react to it */
3148 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3150 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3151 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3153 /* Volume: firmware always react to it and reprograms
3154 * the built-in *extra* mixer. Never map it to control
3155 * another mixer by default. */
3156 KEY_RESERVED, /* 0x14: VOLUME UP */
3157 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3158 KEY_RESERVED, /* 0x16: MUTE */
3160 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3162 /* (assignments unknown, please report if found) */
3163 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3164 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3167 /* Generic keymap for Lenovo ThinkPads */
3168 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3169 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3170 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3171 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3172 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3174 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3175 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3176 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3177 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3179 /* These should be enabled --only-- when ACPI video
3180 * is disabled (i.e. in "vendor" mode), and are handled
3181 * in a special way by the init code */
3182 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3183 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3185 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3187 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3188 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3190 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3191 * react to it and reprograms the built-in *extra* mixer.
3192 * Never map it to control another mixer by default.
3194 * T60?, T61, R60?, R61: firmware and EC tries to send
3195 * these over the regular keyboard, so these are no-ops,
3196 * but there are still weird bugs re. MUTE, so do not
3197 * change unless you get test reports from all Lenovo
3198 * models. May cause the BIOS to interfere with the
3199 * HDA mixer.
3201 KEY_RESERVED, /* 0x14: VOLUME UP */
3202 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3203 KEY_RESERVED, /* 0x16: MUTE */
3205 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3207 /* (assignments unknown, please report if found) */
3208 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3209 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3213 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3214 /* Generic maps (fallback) */
3216 .vendor = PCI_VENDOR_ID_IBM,
3217 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3218 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3221 .vendor = PCI_VENDOR_ID_LENOVO,
3222 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3223 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3227 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3228 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3230 int res, i;
3231 int status;
3232 int hkeyv;
3233 bool radiosw_state = false;
3234 bool tabletsw_state = false;
3236 unsigned long quirks;
3237 unsigned long keymap_id;
3239 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3240 "initializing hotkey subdriver\n");
3242 BUG_ON(!tpacpi_inputdev);
3243 BUG_ON(tpacpi_inputdev->open != NULL ||
3244 tpacpi_inputdev->close != NULL);
3246 TPACPI_ACPIHANDLE_INIT(hkey);
3247 mutex_init(&hotkey_mutex);
3249 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3250 mutex_init(&hotkey_thread_mutex);
3251 mutex_init(&hotkey_thread_data_mutex);
3252 #endif
3254 /* hotkey not supported on 570 */
3255 tp_features.hotkey = hkey_handle != NULL;
3257 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3258 "hotkeys are %s\n",
3259 str_supported(tp_features.hotkey));
3261 if (!tp_features.hotkey)
3262 return 1;
3264 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3265 ARRAY_SIZE(tpacpi_hotkey_qtable));
3267 tpacpi_disable_brightness_delay();
3269 /* MUST have enough space for all attributes to be added to
3270 * hotkey_dev_attributes */
3271 hotkey_dev_attributes = create_attr_set(
3272 ARRAY_SIZE(hotkey_attributes) + 2,
3273 NULL);
3274 if (!hotkey_dev_attributes)
3275 return -ENOMEM;
3276 res = add_many_to_attr_set(hotkey_dev_attributes,
3277 hotkey_attributes,
3278 ARRAY_SIZE(hotkey_attributes));
3279 if (res)
3280 goto err_exit;
3282 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3283 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3284 for HKEY interface version 0x100 */
3285 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3286 if ((hkeyv >> 8) != 1) {
3287 pr_err("unknown version of the HKEY interface: 0x%x\n",
3288 hkeyv);
3289 pr_err("please report this to %s\n", TPACPI_MAIL);
3290 } else {
3292 * MHKV 0x100 in A31, R40, R40e,
3293 * T4x, X31, and later
3295 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3296 "firmware HKEY interface version: 0x%x\n",
3297 hkeyv);
3299 /* Paranoia check AND init hotkey_all_mask */
3300 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3301 "MHKA", "qd")) {
3302 pr_err("missing MHKA handler, "
3303 "please report this to %s\n",
3304 TPACPI_MAIL);
3305 /* Fallback: pre-init for FN+F3,F4,F12 */
3306 hotkey_all_mask = 0x080cU;
3307 } else {
3308 tp_features.hotkey_mask = 1;
3313 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3314 "hotkey masks are %s\n",
3315 str_supported(tp_features.hotkey_mask));
3317 /* Init hotkey_all_mask if not initialized yet */
3318 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3319 (quirks & TPACPI_HK_Q_INIMASK))
3320 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3322 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3323 if (tp_features.hotkey_mask) {
3324 /* hotkey_source_mask *must* be zero for
3325 * the first hotkey_mask_get to return hotkey_orig_mask */
3326 res = hotkey_mask_get();
3327 if (res)
3328 goto err_exit;
3330 hotkey_orig_mask = hotkey_acpi_mask;
3331 } else {
3332 hotkey_orig_mask = hotkey_all_mask;
3333 hotkey_acpi_mask = hotkey_all_mask;
3336 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3337 if (dbg_wlswemul) {
3338 tp_features.hotkey_wlsw = 1;
3339 radiosw_state = !!tpacpi_wlsw_emulstate;
3340 pr_info("radio switch emulation enabled\n");
3341 } else
3342 #endif
3343 /* Not all thinkpads have a hardware radio switch */
3344 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3345 tp_features.hotkey_wlsw = 1;
3346 radiosw_state = !!status;
3347 pr_info("radio switch found; radios are %s\n",
3348 enabled(status, 0));
3350 if (tp_features.hotkey_wlsw)
3351 res = add_to_attr_set(hotkey_dev_attributes,
3352 &dev_attr_hotkey_radio_sw.attr);
3354 /* For X41t, X60t, X61t Tablets... */
3355 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3356 tp_features.hotkey_tablet = 1;
3357 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3358 pr_info("possible tablet mode switch found; "
3359 "ThinkPad in %s mode\n",
3360 (tabletsw_state) ? "tablet" : "laptop");
3361 res = add_to_attr_set(hotkey_dev_attributes,
3362 &dev_attr_hotkey_tablet_mode.attr);
3365 if (!res)
3366 res = register_attr_set_with_sysfs(
3367 hotkey_dev_attributes,
3368 &tpacpi_pdev->dev.kobj);
3369 if (res)
3370 goto err_exit;
3372 /* Set up key map */
3373 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3374 GFP_KERNEL);
3375 if (!hotkey_keycode_map) {
3376 pr_err("failed to allocate memory for key map\n");
3377 res = -ENOMEM;
3378 goto err_exit;
3381 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3382 ARRAY_SIZE(tpacpi_keymap_qtable));
3383 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3384 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3385 "using keymap number %lu\n", keymap_id);
3387 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3388 TPACPI_HOTKEY_MAP_SIZE);
3390 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3391 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3392 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3393 tpacpi_inputdev->keycode = hotkey_keycode_map;
3394 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3395 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3396 input_set_capability(tpacpi_inputdev, EV_KEY,
3397 hotkey_keycode_map[i]);
3398 } else {
3399 if (i < sizeof(hotkey_reserved_mask)*8)
3400 hotkey_reserved_mask |= 1 << i;
3404 if (tp_features.hotkey_wlsw) {
3405 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3406 input_report_switch(tpacpi_inputdev,
3407 SW_RFKILL_ALL, radiosw_state);
3409 if (tp_features.hotkey_tablet) {
3410 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3411 input_report_switch(tpacpi_inputdev,
3412 SW_TABLET_MODE, tabletsw_state);
3415 /* Do not issue duplicate brightness change events to
3416 * userspace. tpacpi_detect_brightness_capabilities() must have
3417 * been called before this point */
3418 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3419 pr_info("This ThinkPad has standard ACPI backlight "
3420 "brightness control, supported by the ACPI "
3421 "video driver\n");
3422 pr_notice("Disabling thinkpad-acpi brightness events "
3423 "by default...\n");
3425 /* Disable brightness up/down on Lenovo thinkpads when
3426 * ACPI is handling them, otherwise it is plain impossible
3427 * for userspace to do something even remotely sane */
3428 hotkey_reserved_mask |=
3429 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3430 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3431 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3432 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3435 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3436 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3437 & ~hotkey_all_mask
3438 & ~hotkey_reserved_mask;
3440 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3441 "hotkey source mask 0x%08x, polling freq %u\n",
3442 hotkey_source_mask, hotkey_poll_freq);
3443 #endif
3445 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3446 "enabling firmware HKEY event interface...\n");
3447 res = hotkey_status_set(true);
3448 if (res) {
3449 hotkey_exit();
3450 return res;
3452 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3453 | hotkey_driver_mask)
3454 & ~hotkey_source_mask);
3455 if (res < 0 && res != -ENXIO) {
3456 hotkey_exit();
3457 return res;
3459 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3460 & ~hotkey_reserved_mask;
3461 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3462 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3463 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3465 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3466 "legacy ibm/hotkey event reporting over procfs %s\n",
3467 (hotkey_report_mode < 2) ?
3468 "enabled" : "disabled");
3470 tpacpi_inputdev->open = &hotkey_inputdev_open;
3471 tpacpi_inputdev->close = &hotkey_inputdev_close;
3473 hotkey_poll_setup_safe(true);
3475 return 0;
3477 err_exit:
3478 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3479 hotkey_dev_attributes = NULL;
3481 return (res < 0)? res : 1;
3484 static bool hotkey_notify_hotkey(const u32 hkey,
3485 bool *send_acpi_ev,
3486 bool *ignore_acpi_ev)
3488 /* 0x1000-0x1FFF: key presses */
3489 unsigned int scancode = hkey & 0xfff;
3490 *send_acpi_ev = true;
3491 *ignore_acpi_ev = false;
3493 /* HKEY event 0x1001 is scancode 0x00 */
3494 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
3495 scancode--;
3496 if (!(hotkey_source_mask & (1 << scancode))) {
3497 tpacpi_input_send_key_masked(scancode);
3498 *send_acpi_ev = false;
3499 } else {
3500 *ignore_acpi_ev = true;
3502 return true;
3504 return false;
3507 static bool hotkey_notify_wakeup(const u32 hkey,
3508 bool *send_acpi_ev,
3509 bool *ignore_acpi_ev)
3511 /* 0x2000-0x2FFF: Wakeup reason */
3512 *send_acpi_ev = true;
3513 *ignore_acpi_ev = false;
3515 switch (hkey) {
3516 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3517 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3518 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3519 *ignore_acpi_ev = true;
3520 break;
3522 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3523 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3524 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3525 *ignore_acpi_ev = true;
3526 break;
3528 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3529 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3530 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3531 /* how to auto-heal: */
3532 /* 2313: woke up from S3, go to S4/S5 */
3533 /* 2413: woke up from S4, go to S5 */
3534 break;
3536 default:
3537 return false;
3540 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3541 pr_info("woke up due to a hot-unplug request...\n");
3542 hotkey_wakeup_reason_notify_change();
3544 return true;
3547 static bool hotkey_notify_dockevent(const u32 hkey,
3548 bool *send_acpi_ev,
3549 bool *ignore_acpi_ev)
3551 /* 0x4000-0x4FFF: dock-related events */
3552 *send_acpi_ev = true;
3553 *ignore_acpi_ev = false;
3555 switch (hkey) {
3556 case TP_HKEY_EV_UNDOCK_ACK:
3557 /* ACPI undock operation completed after wakeup */
3558 hotkey_autosleep_ack = 1;
3559 pr_info("undocked\n");
3560 hotkey_wakeup_hotunplug_complete_notify_change();
3561 return true;
3563 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3564 pr_info("docked into hotplug port replicator\n");
3565 return true;
3566 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3567 pr_info("undocked from hotplug port replicator\n");
3568 return true;
3570 default:
3571 return false;
3575 static bool hotkey_notify_usrevent(const u32 hkey,
3576 bool *send_acpi_ev,
3577 bool *ignore_acpi_ev)
3579 /* 0x5000-0x5FFF: human interface helpers */
3580 *send_acpi_ev = true;
3581 *ignore_acpi_ev = false;
3583 switch (hkey) {
3584 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3585 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3586 return true;
3588 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3589 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3590 tpacpi_input_send_tabletsw();
3591 hotkey_tablet_mode_notify_change();
3592 *send_acpi_ev = false;
3593 return true;
3595 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3596 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3597 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3598 /* do not propagate these events */
3599 *ignore_acpi_ev = true;
3600 return true;
3602 default:
3603 return false;
3607 static void thermal_dump_all_sensors(void);
3609 static bool hotkey_notify_6xxx(const u32 hkey,
3610 bool *send_acpi_ev,
3611 bool *ignore_acpi_ev)
3613 bool known = true;
3615 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
3616 *send_acpi_ev = true;
3617 *ignore_acpi_ev = false;
3619 switch (hkey) {
3620 case TP_HKEY_EV_THM_TABLE_CHANGED:
3621 pr_info("EC reports that Thermal Table has changed\n");
3622 /* recommended action: do nothing, we don't have
3623 * Lenovo ATM information */
3624 return true;
3625 case TP_HKEY_EV_ALARM_BAT_HOT:
3626 pr_crit("THERMAL ALARM: battery is too hot!\n");
3627 /* recommended action: warn user through gui */
3628 break;
3629 case TP_HKEY_EV_ALARM_BAT_XHOT:
3630 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
3631 /* recommended action: immediate sleep/hibernate */
3632 break;
3633 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3634 pr_crit("THERMAL ALARM: "
3635 "a sensor reports something is too hot!\n");
3636 /* recommended action: warn user through gui, that */
3637 /* some internal component is too hot */
3638 break;
3639 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3640 pr_alert("THERMAL EMERGENCY: "
3641 "a sensor reports something is extremely hot!\n");
3642 /* recommended action: immediate sleep/hibernate */
3643 break;
3645 case TP_HKEY_EV_KEY_NUMLOCK:
3646 case TP_HKEY_EV_KEY_FN:
3647 /* key press events, we just ignore them as long as the EC
3648 * is still reporting them in the normal keyboard stream */
3649 *send_acpi_ev = false;
3650 *ignore_acpi_ev = true;
3651 return true;
3653 default:
3654 pr_warn("unknown possible thermal alarm or keyboard event received\n");
3655 known = false;
3658 thermal_dump_all_sensors();
3660 return known;
3663 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3665 u32 hkey;
3666 bool send_acpi_ev;
3667 bool ignore_acpi_ev;
3668 bool known_ev;
3670 if (event != 0x80) {
3671 pr_err("unknown HKEY notification event %d\n", event);
3672 /* forward it to userspace, maybe it knows how to handle it */
3673 acpi_bus_generate_netlink_event(
3674 ibm->acpi->device->pnp.device_class,
3675 dev_name(&ibm->acpi->device->dev),
3676 event, 0);
3677 return;
3680 while (1) {
3681 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3682 pr_err("failed to retrieve HKEY event\n");
3683 return;
3686 if (hkey == 0) {
3687 /* queue empty */
3688 return;
3691 send_acpi_ev = true;
3692 ignore_acpi_ev = false;
3694 switch (hkey >> 12) {
3695 case 1:
3696 /* 0x1000-0x1FFF: key presses */
3697 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3698 &ignore_acpi_ev);
3699 break;
3700 case 2:
3701 /* 0x2000-0x2FFF: Wakeup reason */
3702 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3703 &ignore_acpi_ev);
3704 break;
3705 case 3:
3706 /* 0x3000-0x3FFF: bay-related wakeups */
3707 switch (hkey) {
3708 case TP_HKEY_EV_BAYEJ_ACK:
3709 hotkey_autosleep_ack = 1;
3710 pr_info("bay ejected\n");
3711 hotkey_wakeup_hotunplug_complete_notify_change();
3712 known_ev = true;
3713 break;
3714 case TP_HKEY_EV_OPTDRV_EJ:
3715 /* FIXME: kick libata if SATA link offline */
3716 known_ev = true;
3717 break;
3718 default:
3719 known_ev = false;
3721 break;
3722 case 4:
3723 /* 0x4000-0x4FFF: dock-related events */
3724 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
3725 &ignore_acpi_ev);
3726 break;
3727 case 5:
3728 /* 0x5000-0x5FFF: human interface helpers */
3729 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3730 &ignore_acpi_ev);
3731 break;
3732 case 6:
3733 /* 0x6000-0x6FFF: thermal alarms/notices and
3734 * keyboard events */
3735 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
3736 &ignore_acpi_ev);
3737 break;
3738 case 7:
3739 /* 0x7000-0x7FFF: misc */
3740 if (tp_features.hotkey_wlsw &&
3741 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3742 tpacpi_send_radiosw_update();
3743 send_acpi_ev = 0;
3744 known_ev = true;
3745 break;
3747 /* fallthrough to default */
3748 default:
3749 known_ev = false;
3751 if (!known_ev) {
3752 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
3753 pr_notice("please report the conditions when this "
3754 "event happened to %s\n", TPACPI_MAIL);
3757 /* Legacy events */
3758 if (!ignore_acpi_ev &&
3759 (send_acpi_ev || hotkey_report_mode < 2)) {
3760 acpi_bus_generate_proc_event(ibm->acpi->device,
3761 event, hkey);
3764 /* netlink events */
3765 if (!ignore_acpi_ev && send_acpi_ev) {
3766 acpi_bus_generate_netlink_event(
3767 ibm->acpi->device->pnp.device_class,
3768 dev_name(&ibm->acpi->device->dev),
3769 event, hkey);
3774 static void hotkey_suspend(pm_message_t state)
3776 /* Do these on suspend, we get the events on early resume! */
3777 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3778 hotkey_autosleep_ack = 0;
3781 static void hotkey_resume(void)
3783 tpacpi_disable_brightness_delay();
3785 if (hotkey_status_set(true) < 0 ||
3786 hotkey_mask_set(hotkey_acpi_mask) < 0)
3787 pr_err("error while attempting to reset the event "
3788 "firmware interface\n");
3790 tpacpi_send_radiosw_update();
3791 hotkey_tablet_mode_notify_change();
3792 hotkey_wakeup_reason_notify_change();
3793 hotkey_wakeup_hotunplug_complete_notify_change();
3794 hotkey_poll_setup_safe(false);
3797 /* procfs -------------------------------------------------------------- */
3798 static int hotkey_read(struct seq_file *m)
3800 int res, status;
3802 if (!tp_features.hotkey) {
3803 seq_printf(m, "status:\t\tnot supported\n");
3804 return 0;
3807 if (mutex_lock_killable(&hotkey_mutex))
3808 return -ERESTARTSYS;
3809 res = hotkey_status_get(&status);
3810 if (!res)
3811 res = hotkey_mask_get();
3812 mutex_unlock(&hotkey_mutex);
3813 if (res)
3814 return res;
3816 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3817 if (hotkey_all_mask) {
3818 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3819 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3820 } else {
3821 seq_printf(m, "mask:\t\tnot supported\n");
3822 seq_printf(m, "commands:\tenable, disable, reset\n");
3825 return 0;
3828 static void hotkey_enabledisable_warn(bool enable)
3830 tpacpi_log_usertask("procfs hotkey enable/disable");
3831 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3832 pr_fmt("hotkey enable/disable functionality has been "
3833 "removed from the driver. "
3834 "Hotkeys are always enabled.\n")))
3835 pr_err("Please remove the hotkey=enable module "
3836 "parameter, it is deprecated. "
3837 "Hotkeys are always enabled.\n");
3840 static int hotkey_write(char *buf)
3842 int res;
3843 u32 mask;
3844 char *cmd;
3846 if (!tp_features.hotkey)
3847 return -ENODEV;
3849 if (mutex_lock_killable(&hotkey_mutex))
3850 return -ERESTARTSYS;
3852 mask = hotkey_user_mask;
3854 res = 0;
3855 while ((cmd = next_cmd(&buf))) {
3856 if (strlencmp(cmd, "enable") == 0) {
3857 hotkey_enabledisable_warn(1);
3858 } else if (strlencmp(cmd, "disable") == 0) {
3859 hotkey_enabledisable_warn(0);
3860 res = -EPERM;
3861 } else if (strlencmp(cmd, "reset") == 0) {
3862 mask = (hotkey_all_mask | hotkey_source_mask)
3863 & ~hotkey_reserved_mask;
3864 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3865 /* mask set */
3866 } else if (sscanf(cmd, "%x", &mask) == 1) {
3867 /* mask set */
3868 } else {
3869 res = -EINVAL;
3870 goto errexit;
3874 if (!res) {
3875 tpacpi_disclose_usertask("procfs hotkey",
3876 "set mask to 0x%08x\n", mask);
3877 res = hotkey_user_mask_set(mask);
3880 errexit:
3881 mutex_unlock(&hotkey_mutex);
3882 return res;
3885 static const struct acpi_device_id ibm_htk_device_ids[] = {
3886 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3887 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
3888 {"", 0},
3891 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3892 .hid = ibm_htk_device_ids,
3893 .notify = hotkey_notify,
3894 .handle = &hkey_handle,
3895 .type = ACPI_DEVICE_NOTIFY,
3898 static struct ibm_struct hotkey_driver_data = {
3899 .name = "hotkey",
3900 .read = hotkey_read,
3901 .write = hotkey_write,
3902 .exit = hotkey_exit,
3903 .resume = hotkey_resume,
3904 .suspend = hotkey_suspend,
3905 .acpi = &ibm_hotkey_acpidriver,
3908 /*************************************************************************
3909 * Bluetooth subdriver
3912 enum {
3913 /* ACPI GBDC/SBDC bits */
3914 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3915 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3916 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3917 0 = disable, 1 = enable */
3920 enum {
3921 /* ACPI \BLTH commands */
3922 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3923 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3924 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3925 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3926 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3929 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3931 static int bluetooth_get_status(void)
3933 int status;
3935 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3936 if (dbg_bluetoothemul)
3937 return (tpacpi_bluetooth_emulstate) ?
3938 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3939 #endif
3941 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3942 return -EIO;
3944 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3945 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3948 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3950 int status;
3952 vdbg_printk(TPACPI_DBG_RFKILL,
3953 "will attempt to %s bluetooth\n",
3954 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3956 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3957 if (dbg_bluetoothemul) {
3958 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3959 return 0;
3961 #endif
3963 if (state == TPACPI_RFK_RADIO_ON)
3964 status = TP_ACPI_BLUETOOTH_RADIOSSW
3965 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3966 else
3967 status = 0;
3969 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3970 return -EIO;
3972 return 0;
3975 /* sysfs bluetooth enable ---------------------------------------------- */
3976 static ssize_t bluetooth_enable_show(struct device *dev,
3977 struct device_attribute *attr,
3978 char *buf)
3980 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3981 attr, buf);
3984 static ssize_t bluetooth_enable_store(struct device *dev,
3985 struct device_attribute *attr,
3986 const char *buf, size_t count)
3988 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3989 attr, buf, count);
3992 static struct device_attribute dev_attr_bluetooth_enable =
3993 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3994 bluetooth_enable_show, bluetooth_enable_store);
3996 /* --------------------------------------------------------------------- */
3998 static struct attribute *bluetooth_attributes[] = {
3999 &dev_attr_bluetooth_enable.attr,
4000 NULL
4003 static const struct attribute_group bluetooth_attr_group = {
4004 .attrs = bluetooth_attributes,
4007 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4008 .get_status = bluetooth_get_status,
4009 .set_status = bluetooth_set_status,
4012 static void bluetooth_shutdown(void)
4014 /* Order firmware to save current state to NVRAM */
4015 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4016 TP_ACPI_BLTH_SAVE_STATE))
4017 pr_notice("failed to save bluetooth state to NVRAM\n");
4018 else
4019 vdbg_printk(TPACPI_DBG_RFKILL,
4020 "bluestooth state saved to NVRAM\n");
4023 static void bluetooth_exit(void)
4025 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4026 &bluetooth_attr_group);
4028 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4030 bluetooth_shutdown();
4033 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4035 int res;
4036 int status = 0;
4038 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4039 "initializing bluetooth subdriver\n");
4041 TPACPI_ACPIHANDLE_INIT(hkey);
4043 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4044 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4045 tp_features.bluetooth = hkey_handle &&
4046 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4048 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4049 "bluetooth is %s, status 0x%02x\n",
4050 str_supported(tp_features.bluetooth),
4051 status);
4053 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4054 if (dbg_bluetoothemul) {
4055 tp_features.bluetooth = 1;
4056 pr_info("bluetooth switch emulation enabled\n");
4057 } else
4058 #endif
4059 if (tp_features.bluetooth &&
4060 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4061 /* no bluetooth hardware present in system */
4062 tp_features.bluetooth = 0;
4063 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4064 "bluetooth hardware not installed\n");
4067 if (!tp_features.bluetooth)
4068 return 1;
4070 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4071 &bluetooth_tprfk_ops,
4072 RFKILL_TYPE_BLUETOOTH,
4073 TPACPI_RFK_BLUETOOTH_SW_NAME,
4074 true);
4075 if (res)
4076 return res;
4078 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4079 &bluetooth_attr_group);
4080 if (res) {
4081 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4082 return res;
4085 return 0;
4088 /* procfs -------------------------------------------------------------- */
4089 static int bluetooth_read(struct seq_file *m)
4091 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4094 static int bluetooth_write(char *buf)
4096 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4099 static struct ibm_struct bluetooth_driver_data = {
4100 .name = "bluetooth",
4101 .read = bluetooth_read,
4102 .write = bluetooth_write,
4103 .exit = bluetooth_exit,
4104 .shutdown = bluetooth_shutdown,
4107 /*************************************************************************
4108 * Wan subdriver
4111 enum {
4112 /* ACPI GWAN/SWAN bits */
4113 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4114 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4115 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4116 0 = disable, 1 = enable */
4119 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4121 static int wan_get_status(void)
4123 int status;
4125 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4126 if (dbg_wwanemul)
4127 return (tpacpi_wwan_emulstate) ?
4128 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4129 #endif
4131 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4132 return -EIO;
4134 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4135 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4138 static int wan_set_status(enum tpacpi_rfkill_state state)
4140 int status;
4142 vdbg_printk(TPACPI_DBG_RFKILL,
4143 "will attempt to %s wwan\n",
4144 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4146 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4147 if (dbg_wwanemul) {
4148 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4149 return 0;
4151 #endif
4153 if (state == TPACPI_RFK_RADIO_ON)
4154 status = TP_ACPI_WANCARD_RADIOSSW
4155 | TP_ACPI_WANCARD_RESUMECTRL;
4156 else
4157 status = 0;
4159 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4160 return -EIO;
4162 return 0;
4165 /* sysfs wan enable ---------------------------------------------------- */
4166 static ssize_t wan_enable_show(struct device *dev,
4167 struct device_attribute *attr,
4168 char *buf)
4170 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4171 attr, buf);
4174 static ssize_t wan_enable_store(struct device *dev,
4175 struct device_attribute *attr,
4176 const char *buf, size_t count)
4178 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4179 attr, buf, count);
4182 static struct device_attribute dev_attr_wan_enable =
4183 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4184 wan_enable_show, wan_enable_store);
4186 /* --------------------------------------------------------------------- */
4188 static struct attribute *wan_attributes[] = {
4189 &dev_attr_wan_enable.attr,
4190 NULL
4193 static const struct attribute_group wan_attr_group = {
4194 .attrs = wan_attributes,
4197 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4198 .get_status = wan_get_status,
4199 .set_status = wan_set_status,
4202 static void wan_shutdown(void)
4204 /* Order firmware to save current state to NVRAM */
4205 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4206 TP_ACPI_WGSV_SAVE_STATE))
4207 pr_notice("failed to save WWAN state to NVRAM\n");
4208 else
4209 vdbg_printk(TPACPI_DBG_RFKILL,
4210 "WWAN state saved to NVRAM\n");
4213 static void wan_exit(void)
4215 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4216 &wan_attr_group);
4218 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4220 wan_shutdown();
4223 static int __init wan_init(struct ibm_init_struct *iibm)
4225 int res;
4226 int status = 0;
4228 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4229 "initializing wan subdriver\n");
4231 TPACPI_ACPIHANDLE_INIT(hkey);
4233 tp_features.wan = hkey_handle &&
4234 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4236 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4237 "wan is %s, status 0x%02x\n",
4238 str_supported(tp_features.wan),
4239 status);
4241 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4242 if (dbg_wwanemul) {
4243 tp_features.wan = 1;
4244 pr_info("wwan switch emulation enabled\n");
4245 } else
4246 #endif
4247 if (tp_features.wan &&
4248 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4249 /* no wan hardware present in system */
4250 tp_features.wan = 0;
4251 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4252 "wan hardware not installed\n");
4255 if (!tp_features.wan)
4256 return 1;
4258 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4259 &wan_tprfk_ops,
4260 RFKILL_TYPE_WWAN,
4261 TPACPI_RFK_WWAN_SW_NAME,
4262 true);
4263 if (res)
4264 return res;
4266 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4267 &wan_attr_group);
4269 if (res) {
4270 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4271 return res;
4274 return 0;
4277 /* procfs -------------------------------------------------------------- */
4278 static int wan_read(struct seq_file *m)
4280 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4283 static int wan_write(char *buf)
4285 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4288 static struct ibm_struct wan_driver_data = {
4289 .name = "wan",
4290 .read = wan_read,
4291 .write = wan_write,
4292 .exit = wan_exit,
4293 .shutdown = wan_shutdown,
4296 /*************************************************************************
4297 * UWB subdriver
4300 enum {
4301 /* ACPI GUWB/SUWB bits */
4302 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4303 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4306 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4308 static int uwb_get_status(void)
4310 int status;
4312 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4313 if (dbg_uwbemul)
4314 return (tpacpi_uwb_emulstate) ?
4315 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4316 #endif
4318 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4319 return -EIO;
4321 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4322 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4325 static int uwb_set_status(enum tpacpi_rfkill_state state)
4327 int status;
4329 vdbg_printk(TPACPI_DBG_RFKILL,
4330 "will attempt to %s UWB\n",
4331 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4333 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4334 if (dbg_uwbemul) {
4335 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4336 return 0;
4338 #endif
4340 if (state == TPACPI_RFK_RADIO_ON)
4341 status = TP_ACPI_UWB_RADIOSSW;
4342 else
4343 status = 0;
4345 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4346 return -EIO;
4348 return 0;
4351 /* --------------------------------------------------------------------- */
4353 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4354 .get_status = uwb_get_status,
4355 .set_status = uwb_set_status,
4358 static void uwb_exit(void)
4360 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4363 static int __init uwb_init(struct ibm_init_struct *iibm)
4365 int res;
4366 int status = 0;
4368 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4369 "initializing uwb subdriver\n");
4371 TPACPI_ACPIHANDLE_INIT(hkey);
4373 tp_features.uwb = hkey_handle &&
4374 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4376 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4377 "uwb is %s, status 0x%02x\n",
4378 str_supported(tp_features.uwb),
4379 status);
4381 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4382 if (dbg_uwbemul) {
4383 tp_features.uwb = 1;
4384 pr_info("uwb switch emulation enabled\n");
4385 } else
4386 #endif
4387 if (tp_features.uwb &&
4388 !(status & TP_ACPI_UWB_HWPRESENT)) {
4389 /* no uwb hardware present in system */
4390 tp_features.uwb = 0;
4391 dbg_printk(TPACPI_DBG_INIT,
4392 "uwb hardware not installed\n");
4395 if (!tp_features.uwb)
4396 return 1;
4398 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4399 &uwb_tprfk_ops,
4400 RFKILL_TYPE_UWB,
4401 TPACPI_RFK_UWB_SW_NAME,
4402 false);
4403 return res;
4406 static struct ibm_struct uwb_driver_data = {
4407 .name = "uwb",
4408 .exit = uwb_exit,
4409 .flags.experimental = 1,
4412 /*************************************************************************
4413 * Video subdriver
4416 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4418 enum video_access_mode {
4419 TPACPI_VIDEO_NONE = 0,
4420 TPACPI_VIDEO_570, /* 570 */
4421 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4422 TPACPI_VIDEO_NEW, /* all others */
4425 enum { /* video status flags, based on VIDEO_570 */
4426 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4427 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4428 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4431 enum { /* TPACPI_VIDEO_570 constants */
4432 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4433 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4434 * video_status_flags */
4435 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4436 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4439 static enum video_access_mode video_supported;
4440 static int video_orig_autosw;
4442 static int video_autosw_get(void);
4443 static int video_autosw_set(int enable);
4445 TPACPI_HANDLE(vid, root,
4446 "\\_SB.PCI.AGP.VGA", /* 570 */
4447 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4448 "\\_SB.PCI0.VID0", /* 770e */
4449 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4450 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4451 "\\_SB.PCI0.AGP.VID", /* all others */
4452 ); /* R30, R31 */
4454 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4456 static int __init video_init(struct ibm_init_struct *iibm)
4458 int ivga;
4460 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4462 TPACPI_ACPIHANDLE_INIT(vid);
4463 if (tpacpi_is_ibm())
4464 TPACPI_ACPIHANDLE_INIT(vid2);
4466 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4467 /* G41, assume IVGA doesn't change */
4468 vid_handle = vid2_handle;
4470 if (!vid_handle)
4471 /* video switching not supported on R30, R31 */
4472 video_supported = TPACPI_VIDEO_NONE;
4473 else if (tpacpi_is_ibm() &&
4474 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4475 /* 570 */
4476 video_supported = TPACPI_VIDEO_570;
4477 else if (tpacpi_is_ibm() &&
4478 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4479 /* 600e/x, 770e, 770x */
4480 video_supported = TPACPI_VIDEO_770;
4481 else
4482 /* all others */
4483 video_supported = TPACPI_VIDEO_NEW;
4485 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4486 str_supported(video_supported != TPACPI_VIDEO_NONE),
4487 video_supported);
4489 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4492 static void video_exit(void)
4494 dbg_printk(TPACPI_DBG_EXIT,
4495 "restoring original video autoswitch mode\n");
4496 if (video_autosw_set(video_orig_autosw))
4497 pr_err("error while trying to restore original "
4498 "video autoswitch mode\n");
4501 static int video_outputsw_get(void)
4503 int status = 0;
4504 int i;
4506 switch (video_supported) {
4507 case TPACPI_VIDEO_570:
4508 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4509 TP_ACPI_VIDEO_570_PHSCMD))
4510 return -EIO;
4511 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4512 break;
4513 case TPACPI_VIDEO_770:
4514 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4515 return -EIO;
4516 if (i)
4517 status |= TP_ACPI_VIDEO_S_LCD;
4518 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4519 return -EIO;
4520 if (i)
4521 status |= TP_ACPI_VIDEO_S_CRT;
4522 break;
4523 case TPACPI_VIDEO_NEW:
4524 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4525 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4526 return -EIO;
4527 if (i)
4528 status |= TP_ACPI_VIDEO_S_CRT;
4530 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4531 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4532 return -EIO;
4533 if (i)
4534 status |= TP_ACPI_VIDEO_S_LCD;
4535 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4536 return -EIO;
4537 if (i)
4538 status |= TP_ACPI_VIDEO_S_DVI;
4539 break;
4540 default:
4541 return -ENOSYS;
4544 return status;
4547 static int video_outputsw_set(int status)
4549 int autosw;
4550 int res = 0;
4552 switch (video_supported) {
4553 case TPACPI_VIDEO_570:
4554 res = acpi_evalf(NULL, NULL,
4555 "\\_SB.PHS2", "vdd",
4556 TP_ACPI_VIDEO_570_PHS2CMD,
4557 status | TP_ACPI_VIDEO_570_PHS2SET);
4558 break;
4559 case TPACPI_VIDEO_770:
4560 autosw = video_autosw_get();
4561 if (autosw < 0)
4562 return autosw;
4564 res = video_autosw_set(1);
4565 if (res)
4566 return res;
4567 res = acpi_evalf(vid_handle, NULL,
4568 "ASWT", "vdd", status * 0x100, 0);
4569 if (!autosw && video_autosw_set(autosw)) {
4570 pr_err("video auto-switch left enabled due to error\n");
4571 return -EIO;
4573 break;
4574 case TPACPI_VIDEO_NEW:
4575 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4576 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4577 break;
4578 default:
4579 return -ENOSYS;
4582 return (res)? 0 : -EIO;
4585 static int video_autosw_get(void)
4587 int autosw = 0;
4589 switch (video_supported) {
4590 case TPACPI_VIDEO_570:
4591 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4592 return -EIO;
4593 break;
4594 case TPACPI_VIDEO_770:
4595 case TPACPI_VIDEO_NEW:
4596 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4597 return -EIO;
4598 break;
4599 default:
4600 return -ENOSYS;
4603 return autosw & 1;
4606 static int video_autosw_set(int enable)
4608 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4609 return -EIO;
4610 return 0;
4613 static int video_outputsw_cycle(void)
4615 int autosw = video_autosw_get();
4616 int res;
4618 if (autosw < 0)
4619 return autosw;
4621 switch (video_supported) {
4622 case TPACPI_VIDEO_570:
4623 res = video_autosw_set(1);
4624 if (res)
4625 return res;
4626 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4627 break;
4628 case TPACPI_VIDEO_770:
4629 case TPACPI_VIDEO_NEW:
4630 res = video_autosw_set(1);
4631 if (res)
4632 return res;
4633 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4634 break;
4635 default:
4636 return -ENOSYS;
4638 if (!autosw && video_autosw_set(autosw)) {
4639 pr_err("video auto-switch left enabled due to error\n");
4640 return -EIO;
4643 return (res)? 0 : -EIO;
4646 static int video_expand_toggle(void)
4648 switch (video_supported) {
4649 case TPACPI_VIDEO_570:
4650 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4651 0 : -EIO;
4652 case TPACPI_VIDEO_770:
4653 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4654 0 : -EIO;
4655 case TPACPI_VIDEO_NEW:
4656 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4657 0 : -EIO;
4658 default:
4659 return -ENOSYS;
4661 /* not reached */
4664 static int video_read(struct seq_file *m)
4666 int status, autosw;
4668 if (video_supported == TPACPI_VIDEO_NONE) {
4669 seq_printf(m, "status:\t\tnot supported\n");
4670 return 0;
4673 /* Even reads can crash X.org, so... */
4674 if (!capable(CAP_SYS_ADMIN))
4675 return -EPERM;
4677 status = video_outputsw_get();
4678 if (status < 0)
4679 return status;
4681 autosw = video_autosw_get();
4682 if (autosw < 0)
4683 return autosw;
4685 seq_printf(m, "status:\t\tsupported\n");
4686 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4687 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4688 if (video_supported == TPACPI_VIDEO_NEW)
4689 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4690 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4691 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4692 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4693 if (video_supported == TPACPI_VIDEO_NEW)
4694 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4695 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4696 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4698 return 0;
4701 static int video_write(char *buf)
4703 char *cmd;
4704 int enable, disable, status;
4705 int res;
4707 if (video_supported == TPACPI_VIDEO_NONE)
4708 return -ENODEV;
4710 /* Even reads can crash X.org, let alone writes... */
4711 if (!capable(CAP_SYS_ADMIN))
4712 return -EPERM;
4714 enable = 0;
4715 disable = 0;
4717 while ((cmd = next_cmd(&buf))) {
4718 if (strlencmp(cmd, "lcd_enable") == 0) {
4719 enable |= TP_ACPI_VIDEO_S_LCD;
4720 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4721 disable |= TP_ACPI_VIDEO_S_LCD;
4722 } else if (strlencmp(cmd, "crt_enable") == 0) {
4723 enable |= TP_ACPI_VIDEO_S_CRT;
4724 } else if (strlencmp(cmd, "crt_disable") == 0) {
4725 disable |= TP_ACPI_VIDEO_S_CRT;
4726 } else if (video_supported == TPACPI_VIDEO_NEW &&
4727 strlencmp(cmd, "dvi_enable") == 0) {
4728 enable |= TP_ACPI_VIDEO_S_DVI;
4729 } else if (video_supported == TPACPI_VIDEO_NEW &&
4730 strlencmp(cmd, "dvi_disable") == 0) {
4731 disable |= TP_ACPI_VIDEO_S_DVI;
4732 } else if (strlencmp(cmd, "auto_enable") == 0) {
4733 res = video_autosw_set(1);
4734 if (res)
4735 return res;
4736 } else if (strlencmp(cmd, "auto_disable") == 0) {
4737 res = video_autosw_set(0);
4738 if (res)
4739 return res;
4740 } else if (strlencmp(cmd, "video_switch") == 0) {
4741 res = video_outputsw_cycle();
4742 if (res)
4743 return res;
4744 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4745 res = video_expand_toggle();
4746 if (res)
4747 return res;
4748 } else
4749 return -EINVAL;
4752 if (enable || disable) {
4753 status = video_outputsw_get();
4754 if (status < 0)
4755 return status;
4756 res = video_outputsw_set((status & ~disable) | enable);
4757 if (res)
4758 return res;
4761 return 0;
4764 static struct ibm_struct video_driver_data = {
4765 .name = "video",
4766 .read = video_read,
4767 .write = video_write,
4768 .exit = video_exit,
4771 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4773 /*************************************************************************
4774 * Light (thinklight) subdriver
4777 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4778 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4780 static int light_get_status(void)
4782 int status = 0;
4784 if (tp_features.light_status) {
4785 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4786 return -EIO;
4787 return (!!status);
4790 return -ENXIO;
4793 static int light_set_status(int status)
4795 int rc;
4797 if (tp_features.light) {
4798 if (cmos_handle) {
4799 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4800 (status)?
4801 TP_CMOS_THINKLIGHT_ON :
4802 TP_CMOS_THINKLIGHT_OFF);
4803 } else {
4804 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4805 (status)? 1 : 0);
4807 return (rc)? 0 : -EIO;
4810 return -ENXIO;
4813 static void light_set_status_worker(struct work_struct *work)
4815 struct tpacpi_led_classdev *data =
4816 container_of(work, struct tpacpi_led_classdev, work);
4818 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4819 light_set_status((data->new_state != TPACPI_LED_OFF));
4822 static void light_sysfs_set(struct led_classdev *led_cdev,
4823 enum led_brightness brightness)
4825 struct tpacpi_led_classdev *data =
4826 container_of(led_cdev,
4827 struct tpacpi_led_classdev,
4828 led_classdev);
4829 data->new_state = (brightness != LED_OFF) ?
4830 TPACPI_LED_ON : TPACPI_LED_OFF;
4831 queue_work(tpacpi_wq, &data->work);
4834 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4836 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4839 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4840 .led_classdev = {
4841 .name = "tpacpi::thinklight",
4842 .brightness_set = &light_sysfs_set,
4843 .brightness_get = &light_sysfs_get,
4847 static int __init light_init(struct ibm_init_struct *iibm)
4849 int rc;
4851 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4853 if (tpacpi_is_ibm()) {
4854 TPACPI_ACPIHANDLE_INIT(ledb);
4855 TPACPI_ACPIHANDLE_INIT(lght);
4857 TPACPI_ACPIHANDLE_INIT(cmos);
4858 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4860 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4861 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4863 if (tp_features.light)
4864 /* light status not supported on
4865 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4866 tp_features.light_status =
4867 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4869 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4870 str_supported(tp_features.light),
4871 str_supported(tp_features.light_status));
4873 if (!tp_features.light)
4874 return 1;
4876 rc = led_classdev_register(&tpacpi_pdev->dev,
4877 &tpacpi_led_thinklight.led_classdev);
4879 if (rc < 0) {
4880 tp_features.light = 0;
4881 tp_features.light_status = 0;
4882 } else {
4883 rc = 0;
4886 return rc;
4889 static void light_exit(void)
4891 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4892 if (work_pending(&tpacpi_led_thinklight.work))
4893 flush_workqueue(tpacpi_wq);
4896 static int light_read(struct seq_file *m)
4898 int status;
4900 if (!tp_features.light) {
4901 seq_printf(m, "status:\t\tnot supported\n");
4902 } else if (!tp_features.light_status) {
4903 seq_printf(m, "status:\t\tunknown\n");
4904 seq_printf(m, "commands:\ton, off\n");
4905 } else {
4906 status = light_get_status();
4907 if (status < 0)
4908 return status;
4909 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4910 seq_printf(m, "commands:\ton, off\n");
4913 return 0;
4916 static int light_write(char *buf)
4918 char *cmd;
4919 int newstatus = 0;
4921 if (!tp_features.light)
4922 return -ENODEV;
4924 while ((cmd = next_cmd(&buf))) {
4925 if (strlencmp(cmd, "on") == 0) {
4926 newstatus = 1;
4927 } else if (strlencmp(cmd, "off") == 0) {
4928 newstatus = 0;
4929 } else
4930 return -EINVAL;
4933 return light_set_status(newstatus);
4936 static struct ibm_struct light_driver_data = {
4937 .name = "light",
4938 .read = light_read,
4939 .write = light_write,
4940 .exit = light_exit,
4943 /*************************************************************************
4944 * CMOS subdriver
4947 /* sysfs cmos_command -------------------------------------------------- */
4948 static ssize_t cmos_command_store(struct device *dev,
4949 struct device_attribute *attr,
4950 const char *buf, size_t count)
4952 unsigned long cmos_cmd;
4953 int res;
4955 if (parse_strtoul(buf, 21, &cmos_cmd))
4956 return -EINVAL;
4958 res = issue_thinkpad_cmos_command(cmos_cmd);
4959 return (res)? res : count;
4962 static struct device_attribute dev_attr_cmos_command =
4963 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4965 /* --------------------------------------------------------------------- */
4967 static int __init cmos_init(struct ibm_init_struct *iibm)
4969 int res;
4971 vdbg_printk(TPACPI_DBG_INIT,
4972 "initializing cmos commands subdriver\n");
4974 TPACPI_ACPIHANDLE_INIT(cmos);
4976 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4977 str_supported(cmos_handle != NULL));
4979 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4980 if (res)
4981 return res;
4983 return (cmos_handle)? 0 : 1;
4986 static void cmos_exit(void)
4988 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4991 static int cmos_read(struct seq_file *m)
4993 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4994 R30, R31, T20-22, X20-21 */
4995 if (!cmos_handle)
4996 seq_printf(m, "status:\t\tnot supported\n");
4997 else {
4998 seq_printf(m, "status:\t\tsupported\n");
4999 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5002 return 0;
5005 static int cmos_write(char *buf)
5007 char *cmd;
5008 int cmos_cmd, res;
5010 while ((cmd = next_cmd(&buf))) {
5011 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5012 cmos_cmd >= 0 && cmos_cmd <= 21) {
5013 /* cmos_cmd set */
5014 } else
5015 return -EINVAL;
5017 res = issue_thinkpad_cmos_command(cmos_cmd);
5018 if (res)
5019 return res;
5022 return 0;
5025 static struct ibm_struct cmos_driver_data = {
5026 .name = "cmos",
5027 .read = cmos_read,
5028 .write = cmos_write,
5029 .exit = cmos_exit,
5032 /*************************************************************************
5033 * LED subdriver
5036 enum led_access_mode {
5037 TPACPI_LED_NONE = 0,
5038 TPACPI_LED_570, /* 570 */
5039 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5040 TPACPI_LED_NEW, /* all others */
5043 enum { /* For TPACPI_LED_OLD */
5044 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5045 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5046 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5049 static enum led_access_mode led_supported;
5051 static acpi_handle led_handle;
5053 #define TPACPI_LED_NUMLEDS 16
5054 static struct tpacpi_led_classdev *tpacpi_leds;
5055 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5056 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5057 /* there's a limit of 19 chars + NULL before 2.6.26 */
5058 "tpacpi::power",
5059 "tpacpi:orange:batt",
5060 "tpacpi:green:batt",
5061 "tpacpi::dock_active",
5062 "tpacpi::bay_active",
5063 "tpacpi::dock_batt",
5064 "tpacpi::unknown_led",
5065 "tpacpi::standby",
5066 "tpacpi::dock_status1",
5067 "tpacpi::dock_status2",
5068 "tpacpi::unknown_led2",
5069 "tpacpi::unknown_led3",
5070 "tpacpi::thinkvantage",
5072 #define TPACPI_SAFE_LEDS 0x1081U
5074 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5076 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5077 return false;
5078 #else
5079 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5080 #endif
5083 static int led_get_status(const unsigned int led)
5085 int status;
5086 enum led_status_t led_s;
5088 switch (led_supported) {
5089 case TPACPI_LED_570:
5090 if (!acpi_evalf(ec_handle,
5091 &status, "GLED", "dd", 1 << led))
5092 return -EIO;
5093 led_s = (status == 0)?
5094 TPACPI_LED_OFF :
5095 ((status == 1)?
5096 TPACPI_LED_ON :
5097 TPACPI_LED_BLINK);
5098 tpacpi_led_state_cache[led] = led_s;
5099 return led_s;
5100 default:
5101 return -ENXIO;
5104 /* not reached */
5107 static int led_set_status(const unsigned int led,
5108 const enum led_status_t ledstatus)
5110 /* off, on, blink. Index is led_status_t */
5111 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5112 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5114 int rc = 0;
5116 switch (led_supported) {
5117 case TPACPI_LED_570:
5118 /* 570 */
5119 if (unlikely(led > 7))
5120 return -EINVAL;
5121 if (unlikely(tpacpi_is_led_restricted(led)))
5122 return -EPERM;
5123 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5124 (1 << led), led_sled_arg1[ledstatus]))
5125 rc = -EIO;
5126 break;
5127 case TPACPI_LED_OLD:
5128 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5129 if (unlikely(led > 7))
5130 return -EINVAL;
5131 if (unlikely(tpacpi_is_led_restricted(led)))
5132 return -EPERM;
5133 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5134 if (rc >= 0)
5135 rc = ec_write(TPACPI_LED_EC_HLBL,
5136 (ledstatus == TPACPI_LED_BLINK) << led);
5137 if (rc >= 0)
5138 rc = ec_write(TPACPI_LED_EC_HLCL,
5139 (ledstatus != TPACPI_LED_OFF) << led);
5140 break;
5141 case TPACPI_LED_NEW:
5142 /* all others */
5143 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5144 return -EINVAL;
5145 if (unlikely(tpacpi_is_led_restricted(led)))
5146 return -EPERM;
5147 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5148 led, led_led_arg1[ledstatus]))
5149 rc = -EIO;
5150 break;
5151 default:
5152 rc = -ENXIO;
5155 if (!rc)
5156 tpacpi_led_state_cache[led] = ledstatus;
5158 return rc;
5161 static void led_set_status_worker(struct work_struct *work)
5163 struct tpacpi_led_classdev *data =
5164 container_of(work, struct tpacpi_led_classdev, work);
5166 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5167 led_set_status(data->led, data->new_state);
5170 static void led_sysfs_set(struct led_classdev *led_cdev,
5171 enum led_brightness brightness)
5173 struct tpacpi_led_classdev *data = container_of(led_cdev,
5174 struct tpacpi_led_classdev, led_classdev);
5176 if (brightness == LED_OFF)
5177 data->new_state = TPACPI_LED_OFF;
5178 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5179 data->new_state = TPACPI_LED_ON;
5180 else
5181 data->new_state = TPACPI_LED_BLINK;
5183 queue_work(tpacpi_wq, &data->work);
5186 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5187 unsigned long *delay_on, unsigned long *delay_off)
5189 struct tpacpi_led_classdev *data = container_of(led_cdev,
5190 struct tpacpi_led_classdev, led_classdev);
5192 /* Can we choose the flash rate? */
5193 if (*delay_on == 0 && *delay_off == 0) {
5194 /* yes. set them to the hardware blink rate (1 Hz) */
5195 *delay_on = 500; /* ms */
5196 *delay_off = 500; /* ms */
5197 } else if ((*delay_on != 500) || (*delay_off != 500))
5198 return -EINVAL;
5200 data->new_state = TPACPI_LED_BLINK;
5201 queue_work(tpacpi_wq, &data->work);
5203 return 0;
5206 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5208 int rc;
5210 struct tpacpi_led_classdev *data = container_of(led_cdev,
5211 struct tpacpi_led_classdev, led_classdev);
5213 rc = led_get_status(data->led);
5215 if (rc == TPACPI_LED_OFF || rc < 0)
5216 rc = LED_OFF; /* no error handling in led class :( */
5217 else
5218 rc = LED_FULL;
5220 return rc;
5223 static void led_exit(void)
5225 unsigned int i;
5227 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5228 if (tpacpi_leds[i].led_classdev.name)
5229 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5232 kfree(tpacpi_leds);
5235 static int __init tpacpi_init_led(unsigned int led)
5237 int rc;
5239 tpacpi_leds[led].led = led;
5241 /* LEDs with no name don't get registered */
5242 if (!tpacpi_led_names[led])
5243 return 0;
5245 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5246 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5247 if (led_supported == TPACPI_LED_570)
5248 tpacpi_leds[led].led_classdev.brightness_get =
5249 &led_sysfs_get;
5251 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5253 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5255 rc = led_classdev_register(&tpacpi_pdev->dev,
5256 &tpacpi_leds[led].led_classdev);
5257 if (rc < 0)
5258 tpacpi_leds[led].led_classdev.name = NULL;
5260 return rc;
5263 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5264 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5265 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5266 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5268 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5269 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5270 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5271 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5272 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5273 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5274 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5275 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5277 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5278 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5279 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5280 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5281 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5283 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5284 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5285 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5286 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5288 /* (1) - may have excess leds enabled on MSB */
5290 /* Defaults (order matters, keep last, don't reorder!) */
5291 { /* Lenovo */
5292 .vendor = PCI_VENDOR_ID_LENOVO,
5293 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5294 .quirks = 0x1fffU,
5296 { /* IBM ThinkPads with no EC version string */
5297 .vendor = PCI_VENDOR_ID_IBM,
5298 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5299 .quirks = 0x00ffU,
5301 { /* IBM ThinkPads with EC version string */
5302 .vendor = PCI_VENDOR_ID_IBM,
5303 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5304 .quirks = 0x00bfU,
5308 #undef TPACPI_LEDQ_IBM
5309 #undef TPACPI_LEDQ_LNV
5311 static enum led_access_mode __init led_init_detect_mode(void)
5313 acpi_status status;
5315 if (tpacpi_is_ibm()) {
5316 /* 570 */
5317 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5318 if (ACPI_SUCCESS(status))
5319 return TPACPI_LED_570;
5321 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5322 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5323 if (ACPI_SUCCESS(status))
5324 return TPACPI_LED_OLD;
5327 /* most others */
5328 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5329 if (ACPI_SUCCESS(status))
5330 return TPACPI_LED_NEW;
5332 /* R30, R31, and unknown firmwares */
5333 led_handle = NULL;
5334 return TPACPI_LED_NONE;
5337 static int __init led_init(struct ibm_init_struct *iibm)
5339 unsigned int i;
5340 int rc;
5341 unsigned long useful_leds;
5343 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5345 led_supported = led_init_detect_mode();
5347 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5348 str_supported(led_supported), led_supported);
5350 if (led_supported == TPACPI_LED_NONE)
5351 return 1;
5353 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5354 GFP_KERNEL);
5355 if (!tpacpi_leds) {
5356 pr_err("Out of memory for LED data\n");
5357 return -ENOMEM;
5360 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5361 ARRAY_SIZE(led_useful_qtable));
5363 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5364 if (!tpacpi_is_led_restricted(i) &&
5365 test_bit(i, &useful_leds)) {
5366 rc = tpacpi_init_led(i);
5367 if (rc < 0) {
5368 led_exit();
5369 return rc;
5374 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5375 pr_notice("warning: userspace override of important "
5376 "firmware LEDs is enabled\n");
5377 #endif
5378 return 0;
5381 #define str_led_status(s) \
5382 ((s) == TPACPI_LED_OFF ? "off" : \
5383 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5385 static int led_read(struct seq_file *m)
5387 if (!led_supported) {
5388 seq_printf(m, "status:\t\tnot supported\n");
5389 return 0;
5391 seq_printf(m, "status:\t\tsupported\n");
5393 if (led_supported == TPACPI_LED_570) {
5394 /* 570 */
5395 int i, status;
5396 for (i = 0; i < 8; i++) {
5397 status = led_get_status(i);
5398 if (status < 0)
5399 return -EIO;
5400 seq_printf(m, "%d:\t\t%s\n",
5401 i, str_led_status(status));
5405 seq_printf(m, "commands:\t"
5406 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5408 return 0;
5411 static int led_write(char *buf)
5413 char *cmd;
5414 int led, rc;
5415 enum led_status_t s;
5417 if (!led_supported)
5418 return -ENODEV;
5420 while ((cmd = next_cmd(&buf))) {
5421 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5422 return -EINVAL;
5424 if (strstr(cmd, "off")) {
5425 s = TPACPI_LED_OFF;
5426 } else if (strstr(cmd, "on")) {
5427 s = TPACPI_LED_ON;
5428 } else if (strstr(cmd, "blink")) {
5429 s = TPACPI_LED_BLINK;
5430 } else {
5431 return -EINVAL;
5434 rc = led_set_status(led, s);
5435 if (rc < 0)
5436 return rc;
5439 return 0;
5442 static struct ibm_struct led_driver_data = {
5443 .name = "led",
5444 .read = led_read,
5445 .write = led_write,
5446 .exit = led_exit,
5449 /*************************************************************************
5450 * Beep subdriver
5453 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5455 #define TPACPI_BEEP_Q1 0x0001
5457 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5458 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5459 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5462 static int __init beep_init(struct ibm_init_struct *iibm)
5464 unsigned long quirks;
5466 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5468 TPACPI_ACPIHANDLE_INIT(beep);
5470 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5471 str_supported(beep_handle != NULL));
5473 quirks = tpacpi_check_quirks(beep_quirk_table,
5474 ARRAY_SIZE(beep_quirk_table));
5476 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5478 return (beep_handle)? 0 : 1;
5481 static int beep_read(struct seq_file *m)
5483 if (!beep_handle)
5484 seq_printf(m, "status:\t\tnot supported\n");
5485 else {
5486 seq_printf(m, "status:\t\tsupported\n");
5487 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5490 return 0;
5493 static int beep_write(char *buf)
5495 char *cmd;
5496 int beep_cmd;
5498 if (!beep_handle)
5499 return -ENODEV;
5501 while ((cmd = next_cmd(&buf))) {
5502 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5503 beep_cmd >= 0 && beep_cmd <= 17) {
5504 /* beep_cmd set */
5505 } else
5506 return -EINVAL;
5507 if (tp_features.beep_needs_two_args) {
5508 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5509 beep_cmd, 0))
5510 return -EIO;
5511 } else {
5512 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5513 beep_cmd))
5514 return -EIO;
5518 return 0;
5521 static struct ibm_struct beep_driver_data = {
5522 .name = "beep",
5523 .read = beep_read,
5524 .write = beep_write,
5527 /*************************************************************************
5528 * Thermal subdriver
5531 enum thermal_access_mode {
5532 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5533 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5534 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5535 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5536 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5539 enum { /* TPACPI_THERMAL_TPEC_* */
5540 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5541 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5542 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5544 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5548 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5549 struct ibm_thermal_sensors_struct {
5550 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5553 static enum thermal_access_mode thermal_read_mode;
5555 /* idx is zero-based */
5556 static int thermal_get_sensor(int idx, s32 *value)
5558 int t;
5559 s8 tmp;
5560 char tmpi[5];
5562 t = TP_EC_THERMAL_TMP0;
5564 switch (thermal_read_mode) {
5565 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5566 case TPACPI_THERMAL_TPEC_16:
5567 if (idx >= 8 && idx <= 15) {
5568 t = TP_EC_THERMAL_TMP8;
5569 idx -= 8;
5571 /* fallthrough */
5572 #endif
5573 case TPACPI_THERMAL_TPEC_8:
5574 if (idx <= 7) {
5575 if (!acpi_ec_read(t + idx, &tmp))
5576 return -EIO;
5577 *value = tmp * 1000;
5578 return 0;
5580 break;
5582 case TPACPI_THERMAL_ACPI_UPDT:
5583 if (idx <= 7) {
5584 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5585 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5586 return -EIO;
5587 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5588 return -EIO;
5589 *value = (t - 2732) * 100;
5590 return 0;
5592 break;
5594 case TPACPI_THERMAL_ACPI_TMP07:
5595 if (idx <= 7) {
5596 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5597 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5598 return -EIO;
5599 if (t > 127 || t < -127)
5600 t = TP_EC_THERMAL_TMP_NA;
5601 *value = t * 1000;
5602 return 0;
5604 break;
5606 case TPACPI_THERMAL_NONE:
5607 default:
5608 return -ENOSYS;
5611 return -EINVAL;
5614 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5616 int res, i;
5617 int n;
5619 n = 8;
5620 i = 0;
5622 if (!s)
5623 return -EINVAL;
5625 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5626 n = 16;
5628 for (i = 0 ; i < n; i++) {
5629 res = thermal_get_sensor(i, &s->temp[i]);
5630 if (res)
5631 return res;
5634 return n;
5637 static void thermal_dump_all_sensors(void)
5639 int n, i;
5640 struct ibm_thermal_sensors_struct t;
5642 n = thermal_get_sensors(&t);
5643 if (n <= 0)
5644 return;
5646 pr_notice("temperatures (Celsius):");
5648 for (i = 0; i < n; i++) {
5649 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5650 pr_cont(" %d", (int)(t.temp[i] / 1000));
5651 else
5652 pr_cont(" N/A");
5655 pr_cont("\n");
5658 /* sysfs temp##_input -------------------------------------------------- */
5660 static ssize_t thermal_temp_input_show(struct device *dev,
5661 struct device_attribute *attr,
5662 char *buf)
5664 struct sensor_device_attribute *sensor_attr =
5665 to_sensor_dev_attr(attr);
5666 int idx = sensor_attr->index;
5667 s32 value;
5668 int res;
5670 res = thermal_get_sensor(idx, &value);
5671 if (res)
5672 return res;
5673 if (value == TPACPI_THERMAL_SENSOR_NA)
5674 return -ENXIO;
5676 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5679 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5680 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5681 thermal_temp_input_show, NULL, _idxB)
5683 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5684 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5685 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5686 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5687 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5688 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5689 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5690 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5691 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5692 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5693 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5694 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5695 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5696 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5697 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5698 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5699 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5702 #define THERMAL_ATTRS(X) \
5703 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5705 static struct attribute *thermal_temp_input_attr[] = {
5706 THERMAL_ATTRS(8),
5707 THERMAL_ATTRS(9),
5708 THERMAL_ATTRS(10),
5709 THERMAL_ATTRS(11),
5710 THERMAL_ATTRS(12),
5711 THERMAL_ATTRS(13),
5712 THERMAL_ATTRS(14),
5713 THERMAL_ATTRS(15),
5714 THERMAL_ATTRS(0),
5715 THERMAL_ATTRS(1),
5716 THERMAL_ATTRS(2),
5717 THERMAL_ATTRS(3),
5718 THERMAL_ATTRS(4),
5719 THERMAL_ATTRS(5),
5720 THERMAL_ATTRS(6),
5721 THERMAL_ATTRS(7),
5722 NULL
5725 static const struct attribute_group thermal_temp_input16_group = {
5726 .attrs = thermal_temp_input_attr
5729 static const struct attribute_group thermal_temp_input8_group = {
5730 .attrs = &thermal_temp_input_attr[8]
5733 #undef THERMAL_SENSOR_ATTR_TEMP
5734 #undef THERMAL_ATTRS
5736 /* --------------------------------------------------------------------- */
5738 static int __init thermal_init(struct ibm_init_struct *iibm)
5740 u8 t, ta1, ta2;
5741 int i;
5742 int acpi_tmp7;
5743 int res;
5745 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5747 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5749 if (thinkpad_id.ec_model) {
5751 * Direct EC access mode: sensors at registers
5752 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5753 * non-implemented, thermal sensors return 0x80 when
5754 * not available
5757 ta1 = ta2 = 0;
5758 for (i = 0; i < 8; i++) {
5759 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5760 ta1 |= t;
5761 } else {
5762 ta1 = 0;
5763 break;
5765 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5766 ta2 |= t;
5767 } else {
5768 ta1 = 0;
5769 break;
5772 if (ta1 == 0) {
5773 /* This is sheer paranoia, but we handle it anyway */
5774 if (acpi_tmp7) {
5775 pr_err("ThinkPad ACPI EC access misbehaving, "
5776 "falling back to ACPI TMPx access "
5777 "mode\n");
5778 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5779 } else {
5780 pr_err("ThinkPad ACPI EC access misbehaving, "
5781 "disabling thermal sensors access\n");
5782 thermal_read_mode = TPACPI_THERMAL_NONE;
5784 } else {
5785 thermal_read_mode =
5786 (ta2 != 0) ?
5787 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5789 } else if (acpi_tmp7) {
5790 if (tpacpi_is_ibm() &&
5791 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5792 /* 600e/x, 770e, 770x */
5793 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5794 } else {
5795 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
5796 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5798 } else {
5799 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5800 thermal_read_mode = TPACPI_THERMAL_NONE;
5803 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5804 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5805 thermal_read_mode);
5807 switch (thermal_read_mode) {
5808 case TPACPI_THERMAL_TPEC_16:
5809 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5810 &thermal_temp_input16_group);
5811 if (res)
5812 return res;
5813 break;
5814 case TPACPI_THERMAL_TPEC_8:
5815 case TPACPI_THERMAL_ACPI_TMP07:
5816 case TPACPI_THERMAL_ACPI_UPDT:
5817 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5818 &thermal_temp_input8_group);
5819 if (res)
5820 return res;
5821 break;
5822 case TPACPI_THERMAL_NONE:
5823 default:
5824 return 1;
5827 return 0;
5830 static void thermal_exit(void)
5832 switch (thermal_read_mode) {
5833 case TPACPI_THERMAL_TPEC_16:
5834 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5835 &thermal_temp_input16_group);
5836 break;
5837 case TPACPI_THERMAL_TPEC_8:
5838 case TPACPI_THERMAL_ACPI_TMP07:
5839 case TPACPI_THERMAL_ACPI_UPDT:
5840 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5841 &thermal_temp_input8_group);
5842 break;
5843 case TPACPI_THERMAL_NONE:
5844 default:
5845 break;
5849 static int thermal_read(struct seq_file *m)
5851 int n, i;
5852 struct ibm_thermal_sensors_struct t;
5854 n = thermal_get_sensors(&t);
5855 if (unlikely(n < 0))
5856 return n;
5858 seq_printf(m, "temperatures:\t");
5860 if (n > 0) {
5861 for (i = 0; i < (n - 1); i++)
5862 seq_printf(m, "%d ", t.temp[i] / 1000);
5863 seq_printf(m, "%d\n", t.temp[i] / 1000);
5864 } else
5865 seq_printf(m, "not supported\n");
5867 return 0;
5870 static struct ibm_struct thermal_driver_data = {
5871 .name = "thermal",
5872 .read = thermal_read,
5873 .exit = thermal_exit,
5876 /*************************************************************************
5877 * EC Dump subdriver
5880 static u8 ecdump_regs[256];
5882 static int ecdump_read(struct seq_file *m)
5884 int i, j;
5885 u8 v;
5887 seq_printf(m, "EC "
5888 " +00 +01 +02 +03 +04 +05 +06 +07"
5889 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5890 for (i = 0; i < 256; i += 16) {
5891 seq_printf(m, "EC 0x%02x:", i);
5892 for (j = 0; j < 16; j++) {
5893 if (!acpi_ec_read(i + j, &v))
5894 break;
5895 if (v != ecdump_regs[i + j])
5896 seq_printf(m, " *%02x", v);
5897 else
5898 seq_printf(m, " %02x", v);
5899 ecdump_regs[i + j] = v;
5901 seq_putc(m, '\n');
5902 if (j != 16)
5903 break;
5906 /* These are way too dangerous to advertise openly... */
5907 #if 0
5908 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5909 " (<offset> is 00-ff, <value> is 00-ff)\n");
5910 seq_printf(m, "commands:\t0x<offset> <value> "
5911 " (<offset> is 00-ff, <value> is 0-255)\n");
5912 #endif
5913 return 0;
5916 static int ecdump_write(char *buf)
5918 char *cmd;
5919 int i, v;
5921 while ((cmd = next_cmd(&buf))) {
5922 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5923 /* i and v set */
5924 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5925 /* i and v set */
5926 } else
5927 return -EINVAL;
5928 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5929 if (!acpi_ec_write(i, v))
5930 return -EIO;
5931 } else
5932 return -EINVAL;
5935 return 0;
5938 static struct ibm_struct ecdump_driver_data = {
5939 .name = "ecdump",
5940 .read = ecdump_read,
5941 .write = ecdump_write,
5942 .flags.experimental = 1,
5945 /*************************************************************************
5946 * Backlight/brightness subdriver
5949 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5952 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5953 * CMOS NVRAM byte 0x5E, bits 0-3.
5955 * EC HBRV (0x31) has the following layout
5956 * Bit 7: unknown function
5957 * Bit 6: unknown function
5958 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5959 * Bit 4: must be set to zero to avoid problems
5960 * Bit 3-0: backlight brightness level
5962 * brightness_get_raw returns status data in the HBRV layout
5964 * WARNING: The X61 has been verified to use HBRV for something else, so
5965 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5966 * testing on the very early *60 Lenovo models...
5969 enum {
5970 TP_EC_BACKLIGHT = 0x31,
5972 /* TP_EC_BACKLIGHT bitmasks */
5973 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5974 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5975 TP_EC_BACKLIGHT_MAPSW = 0x20,
5978 enum tpacpi_brightness_access_mode {
5979 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5980 TPACPI_BRGHT_MODE_EC, /* EC control */
5981 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5982 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5983 TPACPI_BRGHT_MODE_MAX
5986 static struct backlight_device *ibm_backlight_device;
5988 static enum tpacpi_brightness_access_mode brightness_mode =
5989 TPACPI_BRGHT_MODE_MAX;
5991 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5993 static struct mutex brightness_mutex;
5995 /* NVRAM brightness access,
5996 * call with brightness_mutex held! */
5997 static unsigned int tpacpi_brightness_nvram_get(void)
5999 u8 lnvram;
6001 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6002 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6003 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6004 lnvram &= bright_maxlvl;
6006 return lnvram;
6009 static void tpacpi_brightness_checkpoint_nvram(void)
6011 u8 lec = 0;
6012 u8 b_nvram;
6014 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6015 return;
6017 vdbg_printk(TPACPI_DBG_BRGHT,
6018 "trying to checkpoint backlight level to NVRAM...\n");
6020 if (mutex_lock_killable(&brightness_mutex) < 0)
6021 return;
6023 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6024 goto unlock;
6025 lec &= TP_EC_BACKLIGHT_LVLMSK;
6026 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6028 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6029 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6030 /* NVRAM needs update */
6031 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6032 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6033 b_nvram |= lec;
6034 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6035 dbg_printk(TPACPI_DBG_BRGHT,
6036 "updated NVRAM backlight level to %u (0x%02x)\n",
6037 (unsigned int) lec, (unsigned int) b_nvram);
6038 } else
6039 vdbg_printk(TPACPI_DBG_BRGHT,
6040 "NVRAM backlight level already is %u (0x%02x)\n",
6041 (unsigned int) lec, (unsigned int) b_nvram);
6043 unlock:
6044 mutex_unlock(&brightness_mutex);
6048 /* call with brightness_mutex held! */
6049 static int tpacpi_brightness_get_raw(int *status)
6051 u8 lec = 0;
6053 switch (brightness_mode) {
6054 case TPACPI_BRGHT_MODE_UCMS_STEP:
6055 *status = tpacpi_brightness_nvram_get();
6056 return 0;
6057 case TPACPI_BRGHT_MODE_EC:
6058 case TPACPI_BRGHT_MODE_ECNVRAM:
6059 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6060 return -EIO;
6061 *status = lec;
6062 return 0;
6063 default:
6064 return -ENXIO;
6068 /* call with brightness_mutex held! */
6069 /* do NOT call with illegal backlight level value */
6070 static int tpacpi_brightness_set_ec(unsigned int value)
6072 u8 lec = 0;
6074 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6075 return -EIO;
6077 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6078 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6079 (value & TP_EC_BACKLIGHT_LVLMSK))))
6080 return -EIO;
6082 return 0;
6085 /* call with brightness_mutex held! */
6086 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6088 int cmos_cmd, inc;
6089 unsigned int current_value, i;
6091 current_value = tpacpi_brightness_nvram_get();
6093 if (value == current_value)
6094 return 0;
6096 cmos_cmd = (value > current_value) ?
6097 TP_CMOS_BRIGHTNESS_UP :
6098 TP_CMOS_BRIGHTNESS_DOWN;
6099 inc = (value > current_value) ? 1 : -1;
6101 for (i = current_value; i != value; i += inc)
6102 if (issue_thinkpad_cmos_command(cmos_cmd))
6103 return -EIO;
6105 return 0;
6108 /* May return EINTR which can always be mapped to ERESTARTSYS */
6109 static int brightness_set(unsigned int value)
6111 int res;
6113 if (value > bright_maxlvl || value < 0)
6114 return -EINVAL;
6116 vdbg_printk(TPACPI_DBG_BRGHT,
6117 "set backlight level to %d\n", value);
6119 res = mutex_lock_killable(&brightness_mutex);
6120 if (res < 0)
6121 return res;
6123 switch (brightness_mode) {
6124 case TPACPI_BRGHT_MODE_EC:
6125 case TPACPI_BRGHT_MODE_ECNVRAM:
6126 res = tpacpi_brightness_set_ec(value);
6127 break;
6128 case TPACPI_BRGHT_MODE_UCMS_STEP:
6129 res = tpacpi_brightness_set_ucmsstep(value);
6130 break;
6131 default:
6132 res = -ENXIO;
6135 mutex_unlock(&brightness_mutex);
6136 return res;
6139 /* sysfs backlight class ----------------------------------------------- */
6141 static int brightness_update_status(struct backlight_device *bd)
6143 unsigned int level =
6144 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6145 bd->props.power == FB_BLANK_UNBLANK) ?
6146 bd->props.brightness : 0;
6148 dbg_printk(TPACPI_DBG_BRGHT,
6149 "backlight: attempt to set level to %d\n",
6150 level);
6152 /* it is the backlight class's job (caller) to handle
6153 * EINTR and other errors properly */
6154 return brightness_set(level);
6157 static int brightness_get(struct backlight_device *bd)
6159 int status, res;
6161 res = mutex_lock_killable(&brightness_mutex);
6162 if (res < 0)
6163 return 0;
6165 res = tpacpi_brightness_get_raw(&status);
6167 mutex_unlock(&brightness_mutex);
6169 if (res < 0)
6170 return 0;
6172 return status & TP_EC_BACKLIGHT_LVLMSK;
6175 static void tpacpi_brightness_notify_change(void)
6177 backlight_force_update(ibm_backlight_device,
6178 BACKLIGHT_UPDATE_HOTKEY);
6181 static struct backlight_ops ibm_backlight_data = {
6182 .get_brightness = brightness_get,
6183 .update_status = brightness_update_status,
6186 /* --------------------------------------------------------------------- */
6189 * Call _BCL method of video device. On some ThinkPads this will
6190 * switch the firmware to the ACPI brightness control mode.
6193 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6195 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6196 union acpi_object *obj;
6197 int rc;
6199 if (ACPI_SUCCESS(acpi_evaluate_object(handle, "_BCL", NULL, &buffer))) {
6200 obj = (union acpi_object *)buffer.pointer;
6201 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6202 pr_err("Unknown _BCL data, please report this to %s\n",
6203 TPACPI_MAIL);
6204 rc = 0;
6205 } else {
6206 rc = obj->package.count;
6208 } else {
6209 return 0;
6212 kfree(buffer.pointer);
6213 return rc;
6218 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6220 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6222 acpi_handle video_device;
6223 int bcl_levels = 0;
6225 tpacpi_acpi_handle_locate("video", ACPI_VIDEO_HID, &video_device);
6226 if (video_device)
6227 bcl_levels = tpacpi_query_bcl_levels(video_device);
6229 tp_features.bright_acpimode = (bcl_levels > 0);
6231 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6235 * These are only useful for models that have only one possibility
6236 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6237 * these quirks.
6239 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6240 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6241 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6243 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6244 /* Models with ATI GPUs known to require ECNVRAM mode */
6245 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6247 /* Models with ATI GPUs that can use ECNVRAM */
6248 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6249 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6250 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6251 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6253 /* Models with Intel Extreme Graphics 2 */
6254 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6255 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6256 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6258 /* Models with Intel GMA900 */
6259 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6260 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6261 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6265 * Returns < 0 for error, otherwise sets tp_features.bright_*
6266 * and bright_maxlvl.
6268 static void __init tpacpi_detect_brightness_capabilities(void)
6270 unsigned int b;
6272 vdbg_printk(TPACPI_DBG_INIT,
6273 "detecting firmware brightness interface capabilities\n");
6275 /* we could run a quirks check here (same table used by
6276 * brightness_init) if needed */
6279 * We always attempt to detect acpi support, so as to switch
6280 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6281 * going to publish a backlight interface
6283 b = tpacpi_check_std_acpi_brightness_support();
6284 switch (b) {
6285 case 16:
6286 bright_maxlvl = 15;
6287 pr_info("detected a 16-level brightness capable ThinkPad\n");
6288 break;
6289 case 8:
6290 case 0:
6291 bright_maxlvl = 7;
6292 pr_info("detected a 8-level brightness capable ThinkPad\n");
6293 break;
6294 default:
6295 pr_err("Unsupported brightness interface, "
6296 "please contact %s\n", TPACPI_MAIL);
6297 tp_features.bright_unkfw = 1;
6298 bright_maxlvl = b - 1;
6302 static int __init brightness_init(struct ibm_init_struct *iibm)
6304 int b;
6305 unsigned long quirks;
6307 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6309 mutex_init(&brightness_mutex);
6311 quirks = tpacpi_check_quirks(brightness_quirk_table,
6312 ARRAY_SIZE(brightness_quirk_table));
6314 /* tpacpi_detect_brightness_capabilities() must have run already */
6316 /* if it is unknown, we don't handle it: it wouldn't be safe */
6317 if (tp_features.bright_unkfw)
6318 return 1;
6320 if (!brightness_enable) {
6321 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6322 "brightness support disabled by "
6323 "module parameter\n");
6324 return 1;
6327 if (acpi_video_backlight_support()) {
6328 if (brightness_enable > 1) {
6329 pr_info("Standard ACPI backlight interface "
6330 "available, not loading native one\n");
6331 return 1;
6332 } else if (brightness_enable == 1) {
6333 pr_warn("Cannot enable backlight brightness support, "
6334 "ACPI is already handling it. Refer to the "
6335 "acpi_backlight kernel parameter.\n");
6336 return 1;
6338 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
6339 pr_notice("Standard ACPI backlight interface not "
6340 "available, thinkpad_acpi native "
6341 "brightness control enabled\n");
6345 * Check for module parameter bogosity, note that we
6346 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6347 * able to detect "unspecified"
6349 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6350 return -EINVAL;
6352 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6353 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6354 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6355 if (quirks & TPACPI_BRGHT_Q_EC)
6356 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6357 else
6358 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6360 dbg_printk(TPACPI_DBG_BRGHT,
6361 "driver auto-selected brightness_mode=%d\n",
6362 brightness_mode);
6365 /* Safety */
6366 if (!tpacpi_is_ibm() &&
6367 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6368 brightness_mode == TPACPI_BRGHT_MODE_EC))
6369 return -EINVAL;
6371 if (tpacpi_brightness_get_raw(&b) < 0)
6372 return 1;
6374 ibm_backlight_device = backlight_device_register(
6375 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6376 &ibm_backlight_data);
6377 if (IS_ERR(ibm_backlight_device)) {
6378 int rc = PTR_ERR(ibm_backlight_device);
6379 ibm_backlight_device = NULL;
6380 pr_err("Could not register backlight device\n");
6381 return rc;
6383 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6384 "brightness is supported\n");
6386 if (quirks & TPACPI_BRGHT_Q_ASK) {
6387 pr_notice("brightness: will use unverified default: "
6388 "brightness_mode=%d\n", brightness_mode);
6389 pr_notice("brightness: please report to %s whether it works well "
6390 "or not on your ThinkPad\n", TPACPI_MAIL);
6393 ibm_backlight_device->props.max_brightness = bright_maxlvl;
6394 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6396 /* Added by mistake in early 2007. Probably useless, but it could
6397 * be working around some unknown firmware problem where the value
6398 * read at startup doesn't match the real hardware state... so leave
6399 * it in place just in case */
6400 backlight_update_status(ibm_backlight_device);
6402 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6403 "brightness: registering brightness hotkeys "
6404 "as change notification\n");
6405 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6406 | TP_ACPI_HKEY_BRGHTUP_MASK
6407 | TP_ACPI_HKEY_BRGHTDWN_MASK);
6408 return 0;
6411 static void brightness_suspend(pm_message_t state)
6413 tpacpi_brightness_checkpoint_nvram();
6416 static void brightness_shutdown(void)
6418 tpacpi_brightness_checkpoint_nvram();
6421 static void brightness_exit(void)
6423 if (ibm_backlight_device) {
6424 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6425 "calling backlight_device_unregister()\n");
6426 backlight_device_unregister(ibm_backlight_device);
6429 tpacpi_brightness_checkpoint_nvram();
6432 static int brightness_read(struct seq_file *m)
6434 int level;
6436 level = brightness_get(NULL);
6437 if (level < 0) {
6438 seq_printf(m, "level:\t\tunreadable\n");
6439 } else {
6440 seq_printf(m, "level:\t\t%d\n", level);
6441 seq_printf(m, "commands:\tup, down\n");
6442 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6443 bright_maxlvl);
6446 return 0;
6449 static int brightness_write(char *buf)
6451 int level;
6452 int rc;
6453 char *cmd;
6455 level = brightness_get(NULL);
6456 if (level < 0)
6457 return level;
6459 while ((cmd = next_cmd(&buf))) {
6460 if (strlencmp(cmd, "up") == 0) {
6461 if (level < bright_maxlvl)
6462 level++;
6463 } else if (strlencmp(cmd, "down") == 0) {
6464 if (level > 0)
6465 level--;
6466 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6467 level >= 0 && level <= bright_maxlvl) {
6468 /* new level set */
6469 } else
6470 return -EINVAL;
6473 tpacpi_disclose_usertask("procfs brightness",
6474 "set level to %d\n", level);
6477 * Now we know what the final level should be, so we try to set it.
6478 * Doing it this way makes the syscall restartable in case of EINTR
6480 rc = brightness_set(level);
6481 if (!rc && ibm_backlight_device)
6482 backlight_force_update(ibm_backlight_device,
6483 BACKLIGHT_UPDATE_SYSFS);
6484 return (rc == -EINTR)? -ERESTARTSYS : rc;
6487 static struct ibm_struct brightness_driver_data = {
6488 .name = "brightness",
6489 .read = brightness_read,
6490 .write = brightness_write,
6491 .exit = brightness_exit,
6492 .suspend = brightness_suspend,
6493 .shutdown = brightness_shutdown,
6496 /*************************************************************************
6497 * Volume subdriver
6501 * IBM ThinkPads have a simple volume controller with MUTE gating.
6502 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6504 * Since the *61 series (and probably also the later *60 series), Lenovo
6505 * ThinkPads only implement the MUTE gate.
6507 * EC register 0x30
6508 * Bit 6: MUTE (1 mutes sound)
6509 * Bit 3-0: Volume
6510 * Other bits should be zero as far as we know.
6512 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6513 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6514 * such as bit 7 which is used to detect repeated presses of MUTE,
6515 * and we leave them unchanged.
6518 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6520 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6521 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6522 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6524 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6525 static char *alsa_id = "ThinkPadEC";
6526 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6528 struct tpacpi_alsa_data {
6529 struct snd_card *card;
6530 struct snd_ctl_elem_id *ctl_mute_id;
6531 struct snd_ctl_elem_id *ctl_vol_id;
6534 static struct snd_card *alsa_card;
6536 enum {
6537 TP_EC_AUDIO = 0x30,
6539 /* TP_EC_AUDIO bits */
6540 TP_EC_AUDIO_MUTESW = 6,
6542 /* TP_EC_AUDIO bitmasks */
6543 TP_EC_AUDIO_LVL_MSK = 0x0F,
6544 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6546 /* Maximum volume */
6547 TP_EC_VOLUME_MAX = 14,
6550 enum tpacpi_volume_access_mode {
6551 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6552 TPACPI_VOL_MODE_EC, /* Pure EC control */
6553 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6554 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6555 TPACPI_VOL_MODE_MAX
6558 enum tpacpi_volume_capabilities {
6559 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6560 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6561 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6562 TPACPI_VOL_CAP_MAX
6565 static enum tpacpi_volume_access_mode volume_mode =
6566 TPACPI_VOL_MODE_MAX;
6568 static enum tpacpi_volume_capabilities volume_capabilities;
6569 static int volume_control_allowed;
6572 * Used to syncronize writers to TP_EC_AUDIO and
6573 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6575 static struct mutex volume_mutex;
6577 static void tpacpi_volume_checkpoint_nvram(void)
6579 u8 lec = 0;
6580 u8 b_nvram;
6581 u8 ec_mask;
6583 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6584 return;
6585 if (!volume_control_allowed)
6586 return;
6588 vdbg_printk(TPACPI_DBG_MIXER,
6589 "trying to checkpoint mixer state to NVRAM...\n");
6591 if (tp_features.mixer_no_level_control)
6592 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6593 else
6594 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6596 if (mutex_lock_killable(&volume_mutex) < 0)
6597 return;
6599 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6600 goto unlock;
6601 lec &= ec_mask;
6602 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6604 if (lec != (b_nvram & ec_mask)) {
6605 /* NVRAM needs update */
6606 b_nvram &= ~ec_mask;
6607 b_nvram |= lec;
6608 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6609 dbg_printk(TPACPI_DBG_MIXER,
6610 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6611 (unsigned int) lec, (unsigned int) b_nvram);
6612 } else {
6613 vdbg_printk(TPACPI_DBG_MIXER,
6614 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6615 (unsigned int) lec, (unsigned int) b_nvram);
6618 unlock:
6619 mutex_unlock(&volume_mutex);
6622 static int volume_get_status_ec(u8 *status)
6624 u8 s;
6626 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6627 return -EIO;
6629 *status = s;
6631 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6633 return 0;
6636 static int volume_get_status(u8 *status)
6638 return volume_get_status_ec(status);
6641 static int volume_set_status_ec(const u8 status)
6643 if (!acpi_ec_write(TP_EC_AUDIO, status))
6644 return -EIO;
6646 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6648 return 0;
6651 static int volume_set_status(const u8 status)
6653 return volume_set_status_ec(status);
6656 /* returns < 0 on error, 0 on no change, 1 on change */
6657 static int __volume_set_mute_ec(const bool mute)
6659 int rc;
6660 u8 s, n;
6662 if (mutex_lock_killable(&volume_mutex) < 0)
6663 return -EINTR;
6665 rc = volume_get_status_ec(&s);
6666 if (rc)
6667 goto unlock;
6669 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6670 s & ~TP_EC_AUDIO_MUTESW_MSK;
6672 if (n != s) {
6673 rc = volume_set_status_ec(n);
6674 if (!rc)
6675 rc = 1;
6678 unlock:
6679 mutex_unlock(&volume_mutex);
6680 return rc;
6683 static int volume_alsa_set_mute(const bool mute)
6685 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6686 (mute) ? "" : "un");
6687 return __volume_set_mute_ec(mute);
6690 static int volume_set_mute(const bool mute)
6692 int rc;
6694 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6695 (mute) ? "" : "un");
6697 rc = __volume_set_mute_ec(mute);
6698 return (rc < 0) ? rc : 0;
6701 /* returns < 0 on error, 0 on no change, 1 on change */
6702 static int __volume_set_volume_ec(const u8 vol)
6704 int rc;
6705 u8 s, n;
6707 if (vol > TP_EC_VOLUME_MAX)
6708 return -EINVAL;
6710 if (mutex_lock_killable(&volume_mutex) < 0)
6711 return -EINTR;
6713 rc = volume_get_status_ec(&s);
6714 if (rc)
6715 goto unlock;
6717 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6719 if (n != s) {
6720 rc = volume_set_status_ec(n);
6721 if (!rc)
6722 rc = 1;
6725 unlock:
6726 mutex_unlock(&volume_mutex);
6727 return rc;
6730 static int volume_alsa_set_volume(const u8 vol)
6732 dbg_printk(TPACPI_DBG_MIXER,
6733 "ALSA: trying to set volume level to %hu\n", vol);
6734 return __volume_set_volume_ec(vol);
6737 static void volume_alsa_notify_change(void)
6739 struct tpacpi_alsa_data *d;
6741 if (alsa_card && alsa_card->private_data) {
6742 d = alsa_card->private_data;
6743 if (d->ctl_mute_id)
6744 snd_ctl_notify(alsa_card,
6745 SNDRV_CTL_EVENT_MASK_VALUE,
6746 d->ctl_mute_id);
6747 if (d->ctl_vol_id)
6748 snd_ctl_notify(alsa_card,
6749 SNDRV_CTL_EVENT_MASK_VALUE,
6750 d->ctl_vol_id);
6754 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6755 struct snd_ctl_elem_info *uinfo)
6757 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6758 uinfo->count = 1;
6759 uinfo->value.integer.min = 0;
6760 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6761 return 0;
6764 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6765 struct snd_ctl_elem_value *ucontrol)
6767 u8 s;
6768 int rc;
6770 rc = volume_get_status(&s);
6771 if (rc < 0)
6772 return rc;
6774 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6775 return 0;
6778 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6779 struct snd_ctl_elem_value *ucontrol)
6781 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6782 ucontrol->value.integer.value[0]);
6783 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6786 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6788 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6789 struct snd_ctl_elem_value *ucontrol)
6791 u8 s;
6792 int rc;
6794 rc = volume_get_status(&s);
6795 if (rc < 0)
6796 return rc;
6798 ucontrol->value.integer.value[0] =
6799 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6800 return 0;
6803 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6804 struct snd_ctl_elem_value *ucontrol)
6806 tpacpi_disclose_usertask("ALSA", "%smute\n",
6807 ucontrol->value.integer.value[0] ?
6808 "un" : "");
6809 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6812 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6813 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6814 .name = "Console Playback Volume",
6815 .index = 0,
6816 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6817 .info = volume_alsa_vol_info,
6818 .get = volume_alsa_vol_get,
6821 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6822 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6823 .name = "Console Playback Switch",
6824 .index = 0,
6825 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6826 .info = volume_alsa_mute_info,
6827 .get = volume_alsa_mute_get,
6830 static void volume_suspend(pm_message_t state)
6832 tpacpi_volume_checkpoint_nvram();
6835 static void volume_resume(void)
6837 volume_alsa_notify_change();
6840 static void volume_shutdown(void)
6842 tpacpi_volume_checkpoint_nvram();
6845 static void volume_exit(void)
6847 if (alsa_card) {
6848 snd_card_free(alsa_card);
6849 alsa_card = NULL;
6852 tpacpi_volume_checkpoint_nvram();
6855 static int __init volume_create_alsa_mixer(void)
6857 struct snd_card *card;
6858 struct tpacpi_alsa_data *data;
6859 struct snd_kcontrol *ctl_vol;
6860 struct snd_kcontrol *ctl_mute;
6861 int rc;
6863 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6864 sizeof(struct tpacpi_alsa_data), &card);
6865 if (rc < 0 || !card) {
6866 pr_err("Failed to create ALSA card structures: %d\n", rc);
6867 return 1;
6870 BUG_ON(!card->private_data);
6871 data = card->private_data;
6872 data->card = card;
6874 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6875 sizeof(card->driver));
6876 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6877 sizeof(card->shortname));
6878 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6879 (thinkpad_id.ec_version_str) ?
6880 thinkpad_id.ec_version_str : "(unknown)");
6881 snprintf(card->longname, sizeof(card->longname),
6882 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6883 (thinkpad_id.ec_version_str) ?
6884 thinkpad_id.ec_version_str : "unknown");
6886 if (volume_control_allowed) {
6887 volume_alsa_control_vol.put = volume_alsa_vol_put;
6888 volume_alsa_control_vol.access =
6889 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6891 volume_alsa_control_mute.put = volume_alsa_mute_put;
6892 volume_alsa_control_mute.access =
6893 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6896 if (!tp_features.mixer_no_level_control) {
6897 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6898 rc = snd_ctl_add(card, ctl_vol);
6899 if (rc < 0) {
6900 pr_err("Failed to create ALSA volume control: %d\n",
6901 rc);
6902 goto err_exit;
6904 data->ctl_vol_id = &ctl_vol->id;
6907 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6908 rc = snd_ctl_add(card, ctl_mute);
6909 if (rc < 0) {
6910 pr_err("Failed to create ALSA mute control: %d\n", rc);
6911 goto err_exit;
6913 data->ctl_mute_id = &ctl_mute->id;
6915 snd_card_set_dev(card, &tpacpi_pdev->dev);
6916 rc = snd_card_register(card);
6917 if (rc < 0) {
6918 pr_err("Failed to register ALSA card: %d\n", rc);
6919 goto err_exit;
6922 alsa_card = card;
6923 return 0;
6925 err_exit:
6926 snd_card_free(card);
6927 return 1;
6930 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6931 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6933 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6934 /* Whitelist volume level on all IBM by default */
6935 { .vendor = PCI_VENDOR_ID_IBM,
6936 .bios = TPACPI_MATCH_ANY,
6937 .ec = TPACPI_MATCH_ANY,
6938 .quirks = TPACPI_VOL_Q_LEVEL },
6940 /* Lenovo models with volume control (needs confirmation) */
6941 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6942 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6943 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6944 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6945 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6946 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6947 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6949 /* Whitelist mute-only on all Lenovo by default */
6950 { .vendor = PCI_VENDOR_ID_LENOVO,
6951 .bios = TPACPI_MATCH_ANY,
6952 .ec = TPACPI_MATCH_ANY,
6953 .quirks = TPACPI_VOL_Q_MUTEONLY }
6956 static int __init volume_init(struct ibm_init_struct *iibm)
6958 unsigned long quirks;
6959 int rc;
6961 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6963 mutex_init(&volume_mutex);
6966 * Check for module parameter bogosity, note that we
6967 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6968 * able to detect "unspecified"
6970 if (volume_mode > TPACPI_VOL_MODE_MAX)
6971 return -EINVAL;
6973 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6974 pr_err("UCMS step volume mode not implemented, "
6975 "please contact %s\n", TPACPI_MAIL);
6976 return 1;
6979 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6980 return -EINVAL;
6983 * The ALSA mixer is our primary interface.
6984 * When disabled, don't install the subdriver at all
6986 if (!alsa_enable) {
6987 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6988 "ALSA mixer disabled by parameter, "
6989 "not loading volume subdriver...\n");
6990 return 1;
6993 quirks = tpacpi_check_quirks(volume_quirk_table,
6994 ARRAY_SIZE(volume_quirk_table));
6996 switch (volume_capabilities) {
6997 case TPACPI_VOL_CAP_AUTO:
6998 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6999 tp_features.mixer_no_level_control = 1;
7000 else if (quirks & TPACPI_VOL_Q_LEVEL)
7001 tp_features.mixer_no_level_control = 0;
7002 else
7003 return 1; /* no mixer */
7004 break;
7005 case TPACPI_VOL_CAP_VOLMUTE:
7006 tp_features.mixer_no_level_control = 0;
7007 break;
7008 case TPACPI_VOL_CAP_MUTEONLY:
7009 tp_features.mixer_no_level_control = 1;
7010 break;
7011 default:
7012 return 1;
7015 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7016 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7017 "using user-supplied volume_capabilities=%d\n",
7018 volume_capabilities);
7020 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7021 volume_mode == TPACPI_VOL_MODE_MAX) {
7022 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7024 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7025 "driver auto-selected volume_mode=%d\n",
7026 volume_mode);
7027 } else {
7028 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7029 "using user-supplied volume_mode=%d\n",
7030 volume_mode);
7033 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7034 "mute is supported, volume control is %s\n",
7035 str_supported(!tp_features.mixer_no_level_control));
7037 rc = volume_create_alsa_mixer();
7038 if (rc) {
7039 pr_err("Could not create the ALSA mixer interface\n");
7040 return rc;
7043 pr_info("Console audio control enabled, mode: %s\n",
7044 (volume_control_allowed) ?
7045 "override (read/write)" :
7046 "monitor (read only)");
7048 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7049 "registering volume hotkeys as change notification\n");
7050 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7051 | TP_ACPI_HKEY_VOLUP_MASK
7052 | TP_ACPI_HKEY_VOLDWN_MASK
7053 | TP_ACPI_HKEY_MUTE_MASK);
7055 return 0;
7058 static int volume_read(struct seq_file *m)
7060 u8 status;
7062 if (volume_get_status(&status) < 0) {
7063 seq_printf(m, "level:\t\tunreadable\n");
7064 } else {
7065 if (tp_features.mixer_no_level_control)
7066 seq_printf(m, "level:\t\tunsupported\n");
7067 else
7068 seq_printf(m, "level:\t\t%d\n",
7069 status & TP_EC_AUDIO_LVL_MSK);
7071 seq_printf(m, "mute:\t\t%s\n",
7072 onoff(status, TP_EC_AUDIO_MUTESW));
7074 if (volume_control_allowed) {
7075 seq_printf(m, "commands:\tunmute, mute\n");
7076 if (!tp_features.mixer_no_level_control) {
7077 seq_printf(m,
7078 "commands:\tup, down\n");
7079 seq_printf(m,
7080 "commands:\tlevel <level>"
7081 " (<level> is 0-%d)\n",
7082 TP_EC_VOLUME_MAX);
7087 return 0;
7090 static int volume_write(char *buf)
7092 u8 s;
7093 u8 new_level, new_mute;
7094 int l;
7095 char *cmd;
7096 int rc;
7099 * We do allow volume control at driver startup, so that the
7100 * user can set initial state through the volume=... parameter hack.
7102 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7103 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7104 tp_warned.volume_ctrl_forbidden = 1;
7105 pr_notice("Console audio control in monitor mode, "
7106 "changes are not allowed\n");
7107 pr_notice("Use the volume_control=1 module parameter "
7108 "to enable volume control\n");
7110 return -EPERM;
7113 rc = volume_get_status(&s);
7114 if (rc < 0)
7115 return rc;
7117 new_level = s & TP_EC_AUDIO_LVL_MSK;
7118 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7120 while ((cmd = next_cmd(&buf))) {
7121 if (!tp_features.mixer_no_level_control) {
7122 if (strlencmp(cmd, "up") == 0) {
7123 if (new_mute)
7124 new_mute = 0;
7125 else if (new_level < TP_EC_VOLUME_MAX)
7126 new_level++;
7127 continue;
7128 } else if (strlencmp(cmd, "down") == 0) {
7129 if (new_mute)
7130 new_mute = 0;
7131 else if (new_level > 0)
7132 new_level--;
7133 continue;
7134 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7135 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7136 new_level = l;
7137 continue;
7140 if (strlencmp(cmd, "mute") == 0)
7141 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7142 else if (strlencmp(cmd, "unmute") == 0)
7143 new_mute = 0;
7144 else
7145 return -EINVAL;
7148 if (tp_features.mixer_no_level_control) {
7149 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7150 new_mute ? "" : "un");
7151 rc = volume_set_mute(!!new_mute);
7152 } else {
7153 tpacpi_disclose_usertask("procfs volume",
7154 "%smute and set level to %d\n",
7155 new_mute ? "" : "un", new_level);
7156 rc = volume_set_status(new_mute | new_level);
7158 volume_alsa_notify_change();
7160 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7163 static struct ibm_struct volume_driver_data = {
7164 .name = "volume",
7165 .read = volume_read,
7166 .write = volume_write,
7167 .exit = volume_exit,
7168 .suspend = volume_suspend,
7169 .resume = volume_resume,
7170 .shutdown = volume_shutdown,
7173 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7175 #define alsa_card NULL
7177 static void inline volume_alsa_notify_change(void)
7181 static int __init volume_init(struct ibm_init_struct *iibm)
7183 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7185 return 1;
7188 static struct ibm_struct volume_driver_data = {
7189 .name = "volume",
7192 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7194 /*************************************************************************
7195 * Fan subdriver
7199 * FAN ACCESS MODES
7201 * TPACPI_FAN_RD_ACPI_GFAN:
7202 * ACPI GFAN method: returns fan level
7204 * see TPACPI_FAN_WR_ACPI_SFAN
7205 * EC 0x2f (HFSP) not available if GFAN exists
7207 * TPACPI_FAN_WR_ACPI_SFAN:
7208 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7210 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7211 * it for writing.
7213 * TPACPI_FAN_WR_TPEC:
7214 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7215 * Supported on almost all ThinkPads
7217 * Fan speed changes of any sort (including those caused by the
7218 * disengaged mode) are usually done slowly by the firmware as the
7219 * maximum amount of fan duty cycle change per second seems to be
7220 * limited.
7222 * Reading is not available if GFAN exists.
7223 * Writing is not available if SFAN exists.
7225 * Bits
7226 * 7 automatic mode engaged;
7227 * (default operation mode of the ThinkPad)
7228 * fan level is ignored in this mode.
7229 * 6 full speed mode (takes precedence over bit 7);
7230 * not available on all thinkpads. May disable
7231 * the tachometer while the fan controller ramps up
7232 * the speed (which can take up to a few *minutes*).
7233 * Speeds up fan to 100% duty-cycle, which is far above
7234 * the standard RPM levels. It is not impossible that
7235 * it could cause hardware damage.
7236 * 5-3 unused in some models. Extra bits for fan level
7237 * in others, but still useless as all values above
7238 * 7 map to the same speed as level 7 in these models.
7239 * 2-0 fan level (0..7 usually)
7240 * 0x00 = stop
7241 * 0x07 = max (set when temperatures critical)
7242 * Some ThinkPads may have other levels, see
7243 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7245 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7246 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7247 * does so, its initial value is meaningless (0x07).
7249 * For firmware bugs, refer to:
7250 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7252 * ----
7254 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7255 * Main fan tachometer reading (in RPM)
7257 * This register is present on all ThinkPads with a new-style EC, and
7258 * it is known not to be present on the A21m/e, and T22, as there is
7259 * something else in offset 0x84 according to the ACPI DSDT. Other
7260 * ThinkPads from this same time period (and earlier) probably lack the
7261 * tachometer as well.
7263 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7264 * was never fixed by IBM to report the EC firmware version string
7265 * probably support the tachometer (like the early X models), so
7266 * detecting it is quite hard. We need more data to know for sure.
7268 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7269 * might result.
7271 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7272 * mode.
7274 * For firmware bugs, refer to:
7275 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7277 * ----
7279 * ThinkPad EC register 0x31 bit 0 (only on select models)
7281 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7282 * show the speed of the main fan. When bit 0 of EC register 0x31
7283 * is one, the tachometer registers show the speed of the auxiliary
7284 * fan.
7286 * Fan control seems to affect both fans, regardless of the state
7287 * of this bit.
7289 * So far, only the firmware for the X60/X61 non-tablet versions
7290 * seem to support this (firmware TP-7M).
7292 * TPACPI_FAN_WR_ACPI_FANS:
7293 * ThinkPad X31, X40, X41. Not available in the X60.
7295 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7296 * high speed. ACPI DSDT seems to map these three speeds to levels
7297 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7298 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7300 * The speeds are stored on handles
7301 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7303 * There are three default speed sets, accessible as handles:
7304 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7306 * ACPI DSDT switches which set is in use depending on various
7307 * factors.
7309 * TPACPI_FAN_WR_TPEC is also available and should be used to
7310 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7311 * but the ACPI tables just mention level 7.
7314 enum { /* Fan control constants */
7315 fan_status_offset = 0x2f, /* EC register 0x2f */
7316 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7317 * 0x84 must be read before 0x85 */
7318 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7319 bit 0 selects which fan is active */
7321 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7322 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7324 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7327 enum fan_status_access_mode {
7328 TPACPI_FAN_NONE = 0, /* No fan status or control */
7329 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7330 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7333 enum fan_control_access_mode {
7334 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7335 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7336 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7337 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7340 enum fan_control_commands {
7341 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7342 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7343 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7344 * and also watchdog cmd */
7347 static int fan_control_allowed;
7349 static enum fan_status_access_mode fan_status_access_mode;
7350 static enum fan_control_access_mode fan_control_access_mode;
7351 static enum fan_control_commands fan_control_commands;
7353 static u8 fan_control_initial_status;
7354 static u8 fan_control_desired_level;
7355 static u8 fan_control_resume_level;
7356 static int fan_watchdog_maxinterval;
7358 static struct mutex fan_mutex;
7360 static void fan_watchdog_fire(struct work_struct *ignored);
7361 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7363 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7364 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7365 "\\FSPD", /* 600e/x, 770e, 770x */
7366 ); /* all others */
7367 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7368 "JFNS", /* 770x-JL */
7369 ); /* all others */
7372 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7373 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7374 * be in auto mode (0x80).
7376 * This is corrected by any write to HFSP either by the driver, or
7377 * by the firmware.
7379 * We assume 0x07 really means auto mode while this quirk is active,
7380 * as this is far more likely than the ThinkPad being in level 7,
7381 * which is only used by the firmware during thermal emergencies.
7383 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7384 * TP-70 (T43, R52), which are known to be buggy.
7387 static void fan_quirk1_setup(void)
7389 if (fan_control_initial_status == 0x07) {
7390 pr_notice("fan_init: initial fan status is unknown, "
7391 "assuming it is in auto mode\n");
7392 tp_features.fan_ctrl_status_undef = 1;
7396 static void fan_quirk1_handle(u8 *fan_status)
7398 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7399 if (*fan_status != fan_control_initial_status) {
7400 /* something changed the HFSP regisnter since
7401 * driver init time, so it is not undefined
7402 * anymore */
7403 tp_features.fan_ctrl_status_undef = 0;
7404 } else {
7405 /* Return most likely status. In fact, it
7406 * might be the only possible status */
7407 *fan_status = TP_EC_FAN_AUTO;
7412 /* Select main fan on X60/X61, NOOP on others */
7413 static bool fan_select_fan1(void)
7415 if (tp_features.second_fan) {
7416 u8 val;
7418 if (ec_read(fan_select_offset, &val) < 0)
7419 return false;
7420 val &= 0xFEU;
7421 if (ec_write(fan_select_offset, val) < 0)
7422 return false;
7424 return true;
7427 /* Select secondary fan on X60/X61 */
7428 static bool fan_select_fan2(void)
7430 u8 val;
7432 if (!tp_features.second_fan)
7433 return false;
7435 if (ec_read(fan_select_offset, &val) < 0)
7436 return false;
7437 val |= 0x01U;
7438 if (ec_write(fan_select_offset, val) < 0)
7439 return false;
7441 return true;
7445 * Call with fan_mutex held
7447 static void fan_update_desired_level(u8 status)
7449 if ((status &
7450 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7451 if (status > 7)
7452 fan_control_desired_level = 7;
7453 else
7454 fan_control_desired_level = status;
7458 static int fan_get_status(u8 *status)
7460 u8 s;
7462 /* TODO:
7463 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7465 switch (fan_status_access_mode) {
7466 case TPACPI_FAN_RD_ACPI_GFAN:
7467 /* 570, 600e/x, 770e, 770x */
7469 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7470 return -EIO;
7472 if (likely(status))
7473 *status = s & 0x07;
7475 break;
7477 case TPACPI_FAN_RD_TPEC:
7478 /* all except 570, 600e/x, 770e, 770x */
7479 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7480 return -EIO;
7482 if (likely(status)) {
7483 *status = s;
7484 fan_quirk1_handle(status);
7487 break;
7489 default:
7490 return -ENXIO;
7493 return 0;
7496 static int fan_get_status_safe(u8 *status)
7498 int rc;
7499 u8 s;
7501 if (mutex_lock_killable(&fan_mutex))
7502 return -ERESTARTSYS;
7503 rc = fan_get_status(&s);
7504 if (!rc)
7505 fan_update_desired_level(s);
7506 mutex_unlock(&fan_mutex);
7508 if (status)
7509 *status = s;
7511 return rc;
7514 static int fan_get_speed(unsigned int *speed)
7516 u8 hi, lo;
7518 switch (fan_status_access_mode) {
7519 case TPACPI_FAN_RD_TPEC:
7520 /* all except 570, 600e/x, 770e, 770x */
7521 if (unlikely(!fan_select_fan1()))
7522 return -EIO;
7523 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7524 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7525 return -EIO;
7527 if (likely(speed))
7528 *speed = (hi << 8) | lo;
7530 break;
7532 default:
7533 return -ENXIO;
7536 return 0;
7539 static int fan2_get_speed(unsigned int *speed)
7541 u8 hi, lo;
7542 bool rc;
7544 switch (fan_status_access_mode) {
7545 case TPACPI_FAN_RD_TPEC:
7546 /* all except 570, 600e/x, 770e, 770x */
7547 if (unlikely(!fan_select_fan2()))
7548 return -EIO;
7549 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7550 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7551 fan_select_fan1(); /* play it safe */
7552 if (rc)
7553 return -EIO;
7555 if (likely(speed))
7556 *speed = (hi << 8) | lo;
7558 break;
7560 default:
7561 return -ENXIO;
7564 return 0;
7567 static int fan_set_level(int level)
7569 if (!fan_control_allowed)
7570 return -EPERM;
7572 switch (fan_control_access_mode) {
7573 case TPACPI_FAN_WR_ACPI_SFAN:
7574 if (level >= 0 && level <= 7) {
7575 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7576 return -EIO;
7577 } else
7578 return -EINVAL;
7579 break;
7581 case TPACPI_FAN_WR_ACPI_FANS:
7582 case TPACPI_FAN_WR_TPEC:
7583 if (!(level & TP_EC_FAN_AUTO) &&
7584 !(level & TP_EC_FAN_FULLSPEED) &&
7585 ((level < 0) || (level > 7)))
7586 return -EINVAL;
7588 /* safety net should the EC not support AUTO
7589 * or FULLSPEED mode bits and just ignore them */
7590 if (level & TP_EC_FAN_FULLSPEED)
7591 level |= 7; /* safety min speed 7 */
7592 else if (level & TP_EC_FAN_AUTO)
7593 level |= 4; /* safety min speed 4 */
7595 if (!acpi_ec_write(fan_status_offset, level))
7596 return -EIO;
7597 else
7598 tp_features.fan_ctrl_status_undef = 0;
7599 break;
7601 default:
7602 return -ENXIO;
7605 vdbg_printk(TPACPI_DBG_FAN,
7606 "fan control: set fan control register to 0x%02x\n", level);
7607 return 0;
7610 static int fan_set_level_safe(int level)
7612 int rc;
7614 if (!fan_control_allowed)
7615 return -EPERM;
7617 if (mutex_lock_killable(&fan_mutex))
7618 return -ERESTARTSYS;
7620 if (level == TPACPI_FAN_LAST_LEVEL)
7621 level = fan_control_desired_level;
7623 rc = fan_set_level(level);
7624 if (!rc)
7625 fan_update_desired_level(level);
7627 mutex_unlock(&fan_mutex);
7628 return rc;
7631 static int fan_set_enable(void)
7633 u8 s;
7634 int rc;
7636 if (!fan_control_allowed)
7637 return -EPERM;
7639 if (mutex_lock_killable(&fan_mutex))
7640 return -ERESTARTSYS;
7642 switch (fan_control_access_mode) {
7643 case TPACPI_FAN_WR_ACPI_FANS:
7644 case TPACPI_FAN_WR_TPEC:
7645 rc = fan_get_status(&s);
7646 if (rc < 0)
7647 break;
7649 /* Don't go out of emergency fan mode */
7650 if (s != 7) {
7651 s &= 0x07;
7652 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7655 if (!acpi_ec_write(fan_status_offset, s))
7656 rc = -EIO;
7657 else {
7658 tp_features.fan_ctrl_status_undef = 0;
7659 rc = 0;
7661 break;
7663 case TPACPI_FAN_WR_ACPI_SFAN:
7664 rc = fan_get_status(&s);
7665 if (rc < 0)
7666 break;
7668 s &= 0x07;
7670 /* Set fan to at least level 4 */
7671 s |= 4;
7673 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7674 rc = -EIO;
7675 else
7676 rc = 0;
7677 break;
7679 default:
7680 rc = -ENXIO;
7683 mutex_unlock(&fan_mutex);
7685 if (!rc)
7686 vdbg_printk(TPACPI_DBG_FAN,
7687 "fan control: set fan control register to 0x%02x\n",
7689 return rc;
7692 static int fan_set_disable(void)
7694 int rc;
7696 if (!fan_control_allowed)
7697 return -EPERM;
7699 if (mutex_lock_killable(&fan_mutex))
7700 return -ERESTARTSYS;
7702 rc = 0;
7703 switch (fan_control_access_mode) {
7704 case TPACPI_FAN_WR_ACPI_FANS:
7705 case TPACPI_FAN_WR_TPEC:
7706 if (!acpi_ec_write(fan_status_offset, 0x00))
7707 rc = -EIO;
7708 else {
7709 fan_control_desired_level = 0;
7710 tp_features.fan_ctrl_status_undef = 0;
7712 break;
7714 case TPACPI_FAN_WR_ACPI_SFAN:
7715 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7716 rc = -EIO;
7717 else
7718 fan_control_desired_level = 0;
7719 break;
7721 default:
7722 rc = -ENXIO;
7725 if (!rc)
7726 vdbg_printk(TPACPI_DBG_FAN,
7727 "fan control: set fan control register to 0\n");
7729 mutex_unlock(&fan_mutex);
7730 return rc;
7733 static int fan_set_speed(int speed)
7735 int rc;
7737 if (!fan_control_allowed)
7738 return -EPERM;
7740 if (mutex_lock_killable(&fan_mutex))
7741 return -ERESTARTSYS;
7743 rc = 0;
7744 switch (fan_control_access_mode) {
7745 case TPACPI_FAN_WR_ACPI_FANS:
7746 if (speed >= 0 && speed <= 65535) {
7747 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7748 speed, speed, speed))
7749 rc = -EIO;
7750 } else
7751 rc = -EINVAL;
7752 break;
7754 default:
7755 rc = -ENXIO;
7758 mutex_unlock(&fan_mutex);
7759 return rc;
7762 static void fan_watchdog_reset(void)
7764 static int fan_watchdog_active;
7766 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7767 return;
7769 if (fan_watchdog_active)
7770 cancel_delayed_work(&fan_watchdog_task);
7772 if (fan_watchdog_maxinterval > 0 &&
7773 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7774 fan_watchdog_active = 1;
7775 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7776 msecs_to_jiffies(fan_watchdog_maxinterval
7777 * 1000))) {
7778 pr_err("failed to queue the fan watchdog, "
7779 "watchdog will not trigger\n");
7781 } else
7782 fan_watchdog_active = 0;
7785 static void fan_watchdog_fire(struct work_struct *ignored)
7787 int rc;
7789 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7790 return;
7792 pr_notice("fan watchdog: enabling fan\n");
7793 rc = fan_set_enable();
7794 if (rc < 0) {
7795 pr_err("fan watchdog: error %d while enabling fan, "
7796 "will try again later...\n", -rc);
7797 /* reschedule for later */
7798 fan_watchdog_reset();
7803 * SYSFS fan layout: hwmon compatible (device)
7805 * pwm*_enable:
7806 * 0: "disengaged" mode
7807 * 1: manual mode
7808 * 2: native EC "auto" mode (recommended, hardware default)
7810 * pwm*: set speed in manual mode, ignored otherwise.
7811 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7812 * interpolation.
7814 * fan*_input: tachometer reading, RPM
7817 * SYSFS fan layout: extensions
7819 * fan_watchdog (driver):
7820 * fan watchdog interval in seconds, 0 disables (default), max 120
7823 /* sysfs fan pwm1_enable ----------------------------------------------- */
7824 static ssize_t fan_pwm1_enable_show(struct device *dev,
7825 struct device_attribute *attr,
7826 char *buf)
7828 int res, mode;
7829 u8 status;
7831 res = fan_get_status_safe(&status);
7832 if (res)
7833 return res;
7835 if (status & TP_EC_FAN_FULLSPEED) {
7836 mode = 0;
7837 } else if (status & TP_EC_FAN_AUTO) {
7838 mode = 2;
7839 } else
7840 mode = 1;
7842 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7845 static ssize_t fan_pwm1_enable_store(struct device *dev,
7846 struct device_attribute *attr,
7847 const char *buf, size_t count)
7849 unsigned long t;
7850 int res, level;
7852 if (parse_strtoul(buf, 2, &t))
7853 return -EINVAL;
7855 tpacpi_disclose_usertask("hwmon pwm1_enable",
7856 "set fan mode to %lu\n", t);
7858 switch (t) {
7859 case 0:
7860 level = TP_EC_FAN_FULLSPEED;
7861 break;
7862 case 1:
7863 level = TPACPI_FAN_LAST_LEVEL;
7864 break;
7865 case 2:
7866 level = TP_EC_FAN_AUTO;
7867 break;
7868 case 3:
7869 /* reserved for software-controlled auto mode */
7870 return -ENOSYS;
7871 default:
7872 return -EINVAL;
7875 res = fan_set_level_safe(level);
7876 if (res == -ENXIO)
7877 return -EINVAL;
7878 else if (res < 0)
7879 return res;
7881 fan_watchdog_reset();
7883 return count;
7886 static struct device_attribute dev_attr_fan_pwm1_enable =
7887 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7888 fan_pwm1_enable_show, fan_pwm1_enable_store);
7890 /* sysfs fan pwm1 ------------------------------------------------------ */
7891 static ssize_t fan_pwm1_show(struct device *dev,
7892 struct device_attribute *attr,
7893 char *buf)
7895 int res;
7896 u8 status;
7898 res = fan_get_status_safe(&status);
7899 if (res)
7900 return res;
7902 if ((status &
7903 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7904 status = fan_control_desired_level;
7906 if (status > 7)
7907 status = 7;
7909 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7912 static ssize_t fan_pwm1_store(struct device *dev,
7913 struct device_attribute *attr,
7914 const char *buf, size_t count)
7916 unsigned long s;
7917 int rc;
7918 u8 status, newlevel;
7920 if (parse_strtoul(buf, 255, &s))
7921 return -EINVAL;
7923 tpacpi_disclose_usertask("hwmon pwm1",
7924 "set fan speed to %lu\n", s);
7926 /* scale down from 0-255 to 0-7 */
7927 newlevel = (s >> 5) & 0x07;
7929 if (mutex_lock_killable(&fan_mutex))
7930 return -ERESTARTSYS;
7932 rc = fan_get_status(&status);
7933 if (!rc && (status &
7934 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7935 rc = fan_set_level(newlevel);
7936 if (rc == -ENXIO)
7937 rc = -EINVAL;
7938 else if (!rc) {
7939 fan_update_desired_level(newlevel);
7940 fan_watchdog_reset();
7944 mutex_unlock(&fan_mutex);
7945 return (rc)? rc : count;
7948 static struct device_attribute dev_attr_fan_pwm1 =
7949 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7950 fan_pwm1_show, fan_pwm1_store);
7952 /* sysfs fan fan1_input ------------------------------------------------ */
7953 static ssize_t fan_fan1_input_show(struct device *dev,
7954 struct device_attribute *attr,
7955 char *buf)
7957 int res;
7958 unsigned int speed;
7960 res = fan_get_speed(&speed);
7961 if (res < 0)
7962 return res;
7964 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7967 static struct device_attribute dev_attr_fan_fan1_input =
7968 __ATTR(fan1_input, S_IRUGO,
7969 fan_fan1_input_show, NULL);
7971 /* sysfs fan fan2_input ------------------------------------------------ */
7972 static ssize_t fan_fan2_input_show(struct device *dev,
7973 struct device_attribute *attr,
7974 char *buf)
7976 int res;
7977 unsigned int speed;
7979 res = fan2_get_speed(&speed);
7980 if (res < 0)
7981 return res;
7983 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7986 static struct device_attribute dev_attr_fan_fan2_input =
7987 __ATTR(fan2_input, S_IRUGO,
7988 fan_fan2_input_show, NULL);
7990 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7991 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7992 char *buf)
7994 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7997 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7998 const char *buf, size_t count)
8000 unsigned long t;
8002 if (parse_strtoul(buf, 120, &t))
8003 return -EINVAL;
8005 if (!fan_control_allowed)
8006 return -EPERM;
8008 fan_watchdog_maxinterval = t;
8009 fan_watchdog_reset();
8011 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8013 return count;
8016 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8017 fan_fan_watchdog_show, fan_fan_watchdog_store);
8019 /* --------------------------------------------------------------------- */
8020 static struct attribute *fan_attributes[] = {
8021 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
8022 &dev_attr_fan_fan1_input.attr,
8023 NULL, /* for fan2_input */
8024 NULL
8027 static const struct attribute_group fan_attr_group = {
8028 .attrs = fan_attributes,
8031 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8032 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8034 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8035 { .vendor = PCI_VENDOR_ID_IBM, \
8036 .bios = TPACPI_MATCH_ANY, \
8037 .ec = TPID(__id1, __id2), \
8038 .quirks = __quirks }
8040 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8041 { .vendor = PCI_VENDOR_ID_LENOVO, \
8042 .bios = TPACPI_MATCH_ANY, \
8043 .ec = TPID(__id1, __id2), \
8044 .quirks = __quirks }
8046 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8047 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8048 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8049 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8050 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8051 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8054 #undef TPACPI_FAN_QL
8055 #undef TPACPI_FAN_QI
8057 static int __init fan_init(struct ibm_init_struct *iibm)
8059 int rc;
8060 unsigned long quirks;
8062 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8063 "initializing fan subdriver\n");
8065 mutex_init(&fan_mutex);
8066 fan_status_access_mode = TPACPI_FAN_NONE;
8067 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8068 fan_control_commands = 0;
8069 fan_watchdog_maxinterval = 0;
8070 tp_features.fan_ctrl_status_undef = 0;
8071 tp_features.second_fan = 0;
8072 fan_control_desired_level = 7;
8074 if (tpacpi_is_ibm()) {
8075 TPACPI_ACPIHANDLE_INIT(fans);
8076 TPACPI_ACPIHANDLE_INIT(gfan);
8077 TPACPI_ACPIHANDLE_INIT(sfan);
8080 quirks = tpacpi_check_quirks(fan_quirk_table,
8081 ARRAY_SIZE(fan_quirk_table));
8083 if (gfan_handle) {
8084 /* 570, 600e/x, 770e, 770x */
8085 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8086 } else {
8087 /* all other ThinkPads: note that even old-style
8088 * ThinkPad ECs supports the fan control register */
8089 if (likely(acpi_ec_read(fan_status_offset,
8090 &fan_control_initial_status))) {
8091 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8092 if (quirks & TPACPI_FAN_Q1)
8093 fan_quirk1_setup();
8094 if (quirks & TPACPI_FAN_2FAN) {
8095 tp_features.second_fan = 1;
8096 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8097 "secondary fan support enabled\n");
8099 } else {
8100 pr_err("ThinkPad ACPI EC access misbehaving, "
8101 "fan status and control unavailable\n");
8102 return 1;
8106 if (sfan_handle) {
8107 /* 570, 770x-JL */
8108 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8109 fan_control_commands |=
8110 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8111 } else {
8112 if (!gfan_handle) {
8113 /* gfan without sfan means no fan control */
8114 /* all other models implement TP EC 0x2f control */
8116 if (fans_handle) {
8117 /* X31, X40, X41 */
8118 fan_control_access_mode =
8119 TPACPI_FAN_WR_ACPI_FANS;
8120 fan_control_commands |=
8121 TPACPI_FAN_CMD_SPEED |
8122 TPACPI_FAN_CMD_LEVEL |
8123 TPACPI_FAN_CMD_ENABLE;
8124 } else {
8125 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8126 fan_control_commands |=
8127 TPACPI_FAN_CMD_LEVEL |
8128 TPACPI_FAN_CMD_ENABLE;
8133 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8134 "fan is %s, modes %d, %d\n",
8135 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8136 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8137 fan_status_access_mode, fan_control_access_mode);
8139 /* fan control master switch */
8140 if (!fan_control_allowed) {
8141 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8142 fan_control_commands = 0;
8143 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8144 "fan control features disabled by parameter\n");
8147 /* update fan_control_desired_level */
8148 if (fan_status_access_mode != TPACPI_FAN_NONE)
8149 fan_get_status_safe(NULL);
8151 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8152 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8153 if (tp_features.second_fan) {
8154 /* attach second fan tachometer */
8155 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8156 &dev_attr_fan_fan2_input.attr;
8158 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8159 &fan_attr_group);
8160 if (rc < 0)
8161 return rc;
8163 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8164 &driver_attr_fan_watchdog);
8165 if (rc < 0) {
8166 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8167 &fan_attr_group);
8168 return rc;
8170 return 0;
8171 } else
8172 return 1;
8175 static void fan_exit(void)
8177 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8178 "cancelling any pending fan watchdog tasks\n");
8180 /* FIXME: can we really do this unconditionally? */
8181 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8182 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8183 &driver_attr_fan_watchdog);
8185 cancel_delayed_work(&fan_watchdog_task);
8186 flush_workqueue(tpacpi_wq);
8189 static void fan_suspend(pm_message_t state)
8191 int rc;
8193 if (!fan_control_allowed)
8194 return;
8196 /* Store fan status in cache */
8197 fan_control_resume_level = 0;
8198 rc = fan_get_status_safe(&fan_control_resume_level);
8199 if (rc < 0)
8200 pr_notice("failed to read fan level for later "
8201 "restore during resume: %d\n", rc);
8203 /* if it is undefined, don't attempt to restore it.
8204 * KEEP THIS LAST */
8205 if (tp_features.fan_ctrl_status_undef)
8206 fan_control_resume_level = 0;
8209 static void fan_resume(void)
8211 u8 current_level = 7;
8212 bool do_set = false;
8213 int rc;
8215 /* DSDT *always* updates status on resume */
8216 tp_features.fan_ctrl_status_undef = 0;
8218 if (!fan_control_allowed ||
8219 !fan_control_resume_level ||
8220 (fan_get_status_safe(&current_level) < 0))
8221 return;
8223 switch (fan_control_access_mode) {
8224 case TPACPI_FAN_WR_ACPI_SFAN:
8225 /* never decrease fan level */
8226 do_set = (fan_control_resume_level > current_level);
8227 break;
8228 case TPACPI_FAN_WR_ACPI_FANS:
8229 case TPACPI_FAN_WR_TPEC:
8230 /* never decrease fan level, scale is:
8231 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8233 * We expect the firmware to set either 7 or AUTO, but we
8234 * handle FULLSPEED out of paranoia.
8236 * So, we can safely only restore FULLSPEED or 7, anything
8237 * else could slow the fan. Restoring AUTO is useless, at
8238 * best that's exactly what the DSDT already set (it is the
8239 * slower it uses).
8241 * Always keep in mind that the DSDT *will* have set the
8242 * fans to what the vendor supposes is the best level. We
8243 * muck with it only to speed the fan up.
8245 if (fan_control_resume_level != 7 &&
8246 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8247 return;
8248 else
8249 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8250 (current_level != fan_control_resume_level);
8251 break;
8252 default:
8253 return;
8255 if (do_set) {
8256 pr_notice("restoring fan level to 0x%02x\n",
8257 fan_control_resume_level);
8258 rc = fan_set_level_safe(fan_control_resume_level);
8259 if (rc < 0)
8260 pr_notice("failed to restore fan level: %d\n", rc);
8264 static int fan_read(struct seq_file *m)
8266 int rc;
8267 u8 status;
8268 unsigned int speed = 0;
8270 switch (fan_status_access_mode) {
8271 case TPACPI_FAN_RD_ACPI_GFAN:
8272 /* 570, 600e/x, 770e, 770x */
8273 rc = fan_get_status_safe(&status);
8274 if (rc < 0)
8275 return rc;
8277 seq_printf(m, "status:\t\t%s\n"
8278 "level:\t\t%d\n",
8279 (status != 0) ? "enabled" : "disabled", status);
8280 break;
8282 case TPACPI_FAN_RD_TPEC:
8283 /* all except 570, 600e/x, 770e, 770x */
8284 rc = fan_get_status_safe(&status);
8285 if (rc < 0)
8286 return rc;
8288 seq_printf(m, "status:\t\t%s\n",
8289 (status != 0) ? "enabled" : "disabled");
8291 rc = fan_get_speed(&speed);
8292 if (rc < 0)
8293 return rc;
8295 seq_printf(m, "speed:\t\t%d\n", speed);
8297 if (status & TP_EC_FAN_FULLSPEED)
8298 /* Disengaged mode takes precedence */
8299 seq_printf(m, "level:\t\tdisengaged\n");
8300 else if (status & TP_EC_FAN_AUTO)
8301 seq_printf(m, "level:\t\tauto\n");
8302 else
8303 seq_printf(m, "level:\t\t%d\n", status);
8304 break;
8306 case TPACPI_FAN_NONE:
8307 default:
8308 seq_printf(m, "status:\t\tnot supported\n");
8311 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8312 seq_printf(m, "commands:\tlevel <level>");
8314 switch (fan_control_access_mode) {
8315 case TPACPI_FAN_WR_ACPI_SFAN:
8316 seq_printf(m, " (<level> is 0-7)\n");
8317 break;
8319 default:
8320 seq_printf(m, " (<level> is 0-7, "
8321 "auto, disengaged, full-speed)\n");
8322 break;
8326 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8327 seq_printf(m, "commands:\tenable, disable\n"
8328 "commands:\twatchdog <timeout> (<timeout> "
8329 "is 0 (off), 1-120 (seconds))\n");
8331 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8332 seq_printf(m, "commands:\tspeed <speed>"
8333 " (<speed> is 0-65535)\n");
8335 return 0;
8338 static int fan_write_cmd_level(const char *cmd, int *rc)
8340 int level;
8342 if (strlencmp(cmd, "level auto") == 0)
8343 level = TP_EC_FAN_AUTO;
8344 else if ((strlencmp(cmd, "level disengaged") == 0) |
8345 (strlencmp(cmd, "level full-speed") == 0))
8346 level = TP_EC_FAN_FULLSPEED;
8347 else if (sscanf(cmd, "level %d", &level) != 1)
8348 return 0;
8350 *rc = fan_set_level_safe(level);
8351 if (*rc == -ENXIO)
8352 pr_err("level command accepted for unsupported access mode %d\n",
8353 fan_control_access_mode);
8354 else if (!*rc)
8355 tpacpi_disclose_usertask("procfs fan",
8356 "set level to %d\n", level);
8358 return 1;
8361 static int fan_write_cmd_enable(const char *cmd, int *rc)
8363 if (strlencmp(cmd, "enable") != 0)
8364 return 0;
8366 *rc = fan_set_enable();
8367 if (*rc == -ENXIO)
8368 pr_err("enable command accepted for unsupported access mode %d\n",
8369 fan_control_access_mode);
8370 else if (!*rc)
8371 tpacpi_disclose_usertask("procfs fan", "enable\n");
8373 return 1;
8376 static int fan_write_cmd_disable(const char *cmd, int *rc)
8378 if (strlencmp(cmd, "disable") != 0)
8379 return 0;
8381 *rc = fan_set_disable();
8382 if (*rc == -ENXIO)
8383 pr_err("disable command accepted for unsupported access mode %d\n",
8384 fan_control_access_mode);
8385 else if (!*rc)
8386 tpacpi_disclose_usertask("procfs fan", "disable\n");
8388 return 1;
8391 static int fan_write_cmd_speed(const char *cmd, int *rc)
8393 int speed;
8395 /* TODO:
8396 * Support speed <low> <medium> <high> ? */
8398 if (sscanf(cmd, "speed %d", &speed) != 1)
8399 return 0;
8401 *rc = fan_set_speed(speed);
8402 if (*rc == -ENXIO)
8403 pr_err("speed command accepted for unsupported access mode %d\n",
8404 fan_control_access_mode);
8405 else if (!*rc)
8406 tpacpi_disclose_usertask("procfs fan",
8407 "set speed to %d\n", speed);
8409 return 1;
8412 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8414 int interval;
8416 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8417 return 0;
8419 if (interval < 0 || interval > 120)
8420 *rc = -EINVAL;
8421 else {
8422 fan_watchdog_maxinterval = interval;
8423 tpacpi_disclose_usertask("procfs fan",
8424 "set watchdog timer to %d\n",
8425 interval);
8428 return 1;
8431 static int fan_write(char *buf)
8433 char *cmd;
8434 int rc = 0;
8436 while (!rc && (cmd = next_cmd(&buf))) {
8437 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8438 fan_write_cmd_level(cmd, &rc)) &&
8439 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8440 (fan_write_cmd_enable(cmd, &rc) ||
8441 fan_write_cmd_disable(cmd, &rc) ||
8442 fan_write_cmd_watchdog(cmd, &rc))) &&
8443 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8444 fan_write_cmd_speed(cmd, &rc))
8446 rc = -EINVAL;
8447 else if (!rc)
8448 fan_watchdog_reset();
8451 return rc;
8454 static struct ibm_struct fan_driver_data = {
8455 .name = "fan",
8456 .read = fan_read,
8457 .write = fan_write,
8458 .exit = fan_exit,
8459 .suspend = fan_suspend,
8460 .resume = fan_resume,
8463 /****************************************************************************
8464 ****************************************************************************
8466 * Infrastructure
8468 ****************************************************************************
8469 ****************************************************************************/
8472 * HKEY event callout for other subdrivers go here
8473 * (yes, it is ugly, but it is quick, safe, and gets the job done
8475 static void tpacpi_driver_event(const unsigned int hkey_event)
8477 if (ibm_backlight_device) {
8478 switch (hkey_event) {
8479 case TP_HKEY_EV_BRGHT_UP:
8480 case TP_HKEY_EV_BRGHT_DOWN:
8481 tpacpi_brightness_notify_change();
8484 if (alsa_card) {
8485 switch (hkey_event) {
8486 case TP_HKEY_EV_VOL_UP:
8487 case TP_HKEY_EV_VOL_DOWN:
8488 case TP_HKEY_EV_VOL_MUTE:
8489 volume_alsa_notify_change();
8494 static void hotkey_driver_event(const unsigned int scancode)
8496 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8499 /* sysfs name ---------------------------------------------------------- */
8500 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8501 struct device_attribute *attr,
8502 char *buf)
8504 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8507 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8508 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8510 /* --------------------------------------------------------------------- */
8512 /* /proc support */
8513 static struct proc_dir_entry *proc_dir;
8516 * Module and infrastructure proble, init and exit handling
8519 static int force_load;
8521 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8522 static const char * __init str_supported(int is_supported)
8524 static char text_unsupported[] __initdata = "not supported";
8526 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8528 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8530 static void ibm_exit(struct ibm_struct *ibm)
8532 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8534 list_del_init(&ibm->all_drivers);
8536 if (ibm->flags.acpi_notify_installed) {
8537 dbg_printk(TPACPI_DBG_EXIT,
8538 "%s: acpi_remove_notify_handler\n", ibm->name);
8539 BUG_ON(!ibm->acpi);
8540 acpi_remove_notify_handler(*ibm->acpi->handle,
8541 ibm->acpi->type,
8542 dispatch_acpi_notify);
8543 ibm->flags.acpi_notify_installed = 0;
8546 if (ibm->flags.proc_created) {
8547 dbg_printk(TPACPI_DBG_EXIT,
8548 "%s: remove_proc_entry\n", ibm->name);
8549 remove_proc_entry(ibm->name, proc_dir);
8550 ibm->flags.proc_created = 0;
8553 if (ibm->flags.acpi_driver_registered) {
8554 dbg_printk(TPACPI_DBG_EXIT,
8555 "%s: acpi_bus_unregister_driver\n", ibm->name);
8556 BUG_ON(!ibm->acpi);
8557 acpi_bus_unregister_driver(ibm->acpi->driver);
8558 kfree(ibm->acpi->driver);
8559 ibm->acpi->driver = NULL;
8560 ibm->flags.acpi_driver_registered = 0;
8563 if (ibm->flags.init_called && ibm->exit) {
8564 ibm->exit();
8565 ibm->flags.init_called = 0;
8568 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8571 static int __init ibm_init(struct ibm_init_struct *iibm)
8573 int ret;
8574 struct ibm_struct *ibm = iibm->data;
8575 struct proc_dir_entry *entry;
8577 BUG_ON(ibm == NULL);
8579 INIT_LIST_HEAD(&ibm->all_drivers);
8581 if (ibm->flags.experimental && !experimental)
8582 return 0;
8584 dbg_printk(TPACPI_DBG_INIT,
8585 "probing for %s\n", ibm->name);
8587 if (iibm->init) {
8588 ret = iibm->init(iibm);
8589 if (ret > 0)
8590 return 0; /* probe failed */
8591 if (ret)
8592 return ret;
8594 ibm->flags.init_called = 1;
8597 if (ibm->acpi) {
8598 if (ibm->acpi->hid) {
8599 ret = register_tpacpi_subdriver(ibm);
8600 if (ret)
8601 goto err_out;
8604 if (ibm->acpi->notify) {
8605 ret = setup_acpi_notify(ibm);
8606 if (ret == -ENODEV) {
8607 pr_notice("disabling subdriver %s\n",
8608 ibm->name);
8609 ret = 0;
8610 goto err_out;
8612 if (ret < 0)
8613 goto err_out;
8617 dbg_printk(TPACPI_DBG_INIT,
8618 "%s installed\n", ibm->name);
8620 if (ibm->read) {
8621 mode_t mode = iibm->base_procfs_mode;
8623 if (!mode)
8624 mode = S_IRUGO;
8625 if (ibm->write)
8626 mode |= S_IWUSR;
8627 entry = proc_create_data(ibm->name, mode, proc_dir,
8628 &dispatch_proc_fops, ibm);
8629 if (!entry) {
8630 pr_err("unable to create proc entry %s\n", ibm->name);
8631 ret = -ENODEV;
8632 goto err_out;
8634 ibm->flags.proc_created = 1;
8637 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8639 return 0;
8641 err_out:
8642 dbg_printk(TPACPI_DBG_INIT,
8643 "%s: at error exit path with result %d\n",
8644 ibm->name, ret);
8646 ibm_exit(ibm);
8647 return (ret < 0)? ret : 0;
8650 /* Probing */
8652 static bool __pure __init tpacpi_is_fw_digit(const char c)
8654 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8657 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8658 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8659 const char t)
8661 return s && strlen(s) >= 8 &&
8662 tpacpi_is_fw_digit(s[0]) &&
8663 tpacpi_is_fw_digit(s[1]) &&
8664 s[2] == t && s[3] == 'T' &&
8665 tpacpi_is_fw_digit(s[4]) &&
8666 tpacpi_is_fw_digit(s[5]);
8669 /* returns 0 - probe ok, or < 0 - probe error.
8670 * Probe ok doesn't mean thinkpad found.
8671 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8672 static int __must_check __init get_thinkpad_model_data(
8673 struct thinkpad_id_data *tp)
8675 const struct dmi_device *dev = NULL;
8676 char ec_fw_string[18];
8677 char const *s;
8679 if (!tp)
8680 return -EINVAL;
8682 memset(tp, 0, sizeof(*tp));
8684 if (dmi_name_in_vendors("IBM"))
8685 tp->vendor = PCI_VENDOR_ID_IBM;
8686 else if (dmi_name_in_vendors("LENOVO"))
8687 tp->vendor = PCI_VENDOR_ID_LENOVO;
8688 else
8689 return 0;
8691 s = dmi_get_system_info(DMI_BIOS_VERSION);
8692 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8693 if (s && !tp->bios_version_str)
8694 return -ENOMEM;
8696 /* Really ancient ThinkPad 240X will fail this, which is fine */
8697 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8698 return 0;
8700 tp->bios_model = tp->bios_version_str[0]
8701 | (tp->bios_version_str[1] << 8);
8702 tp->bios_release = (tp->bios_version_str[4] << 8)
8703 | tp->bios_version_str[5];
8706 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8707 * X32 or newer, all Z series; Some models must have an
8708 * up-to-date BIOS or they will not be detected.
8710 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8712 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8713 if (sscanf(dev->name,
8714 "IBM ThinkPad Embedded Controller -[%17c",
8715 ec_fw_string) == 1) {
8716 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8717 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8719 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8720 if (!tp->ec_version_str)
8721 return -ENOMEM;
8723 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8724 tp->ec_model = ec_fw_string[0]
8725 | (ec_fw_string[1] << 8);
8726 tp->ec_release = (ec_fw_string[4] << 8)
8727 | ec_fw_string[5];
8728 } else {
8729 pr_notice("ThinkPad firmware release %s "
8730 "doesn't match the known patterns\n",
8731 ec_fw_string);
8732 pr_notice("please report this to %s\n",
8733 TPACPI_MAIL);
8735 break;
8739 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8740 if (s && !strnicmp(s, "ThinkPad", 8)) {
8741 tp->model_str = kstrdup(s, GFP_KERNEL);
8742 if (!tp->model_str)
8743 return -ENOMEM;
8746 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8747 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8748 if (s && !tp->nummodel_str)
8749 return -ENOMEM;
8751 return 0;
8754 static int __init probe_for_thinkpad(void)
8756 int is_thinkpad;
8758 if (acpi_disabled)
8759 return -ENODEV;
8761 /* It would be dangerous to run the driver in this case */
8762 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8763 return -ENODEV;
8766 * Non-ancient models have better DMI tagging, but very old models
8767 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8769 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8770 (thinkpad_id.ec_model != 0) ||
8771 tpacpi_is_fw_known();
8773 /* The EC handler is required */
8774 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
8775 if (!ec_handle) {
8776 if (is_thinkpad)
8777 pr_err("Not yet supported ThinkPad detected!\n");
8778 return -ENODEV;
8781 if (!is_thinkpad && !force_load)
8782 return -ENODEV;
8784 return 0;
8787 static void __init thinkpad_acpi_init_banner(void)
8789 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8790 pr_info("%s\n", TPACPI_URL);
8792 pr_info("ThinkPad BIOS %s, EC %s\n",
8793 (thinkpad_id.bios_version_str) ?
8794 thinkpad_id.bios_version_str : "unknown",
8795 (thinkpad_id.ec_version_str) ?
8796 thinkpad_id.ec_version_str : "unknown");
8798 BUG_ON(!thinkpad_id.vendor);
8800 if (thinkpad_id.model_str)
8801 pr_info("%s %s, model %s\n",
8802 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8803 "IBM" : ((thinkpad_id.vendor ==
8804 PCI_VENDOR_ID_LENOVO) ?
8805 "Lenovo" : "Unknown vendor"),
8806 thinkpad_id.model_str,
8807 (thinkpad_id.nummodel_str) ?
8808 thinkpad_id.nummodel_str : "unknown");
8811 /* Module init, exit, parameters */
8813 static struct ibm_init_struct ibms_init[] __initdata = {
8815 .data = &thinkpad_acpi_driver_data,
8818 .init = hotkey_init,
8819 .data = &hotkey_driver_data,
8822 .init = bluetooth_init,
8823 .data = &bluetooth_driver_data,
8826 .init = wan_init,
8827 .data = &wan_driver_data,
8830 .init = uwb_init,
8831 .data = &uwb_driver_data,
8833 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8835 .init = video_init,
8836 .base_procfs_mode = S_IRUSR,
8837 .data = &video_driver_data,
8839 #endif
8841 .init = light_init,
8842 .data = &light_driver_data,
8845 .init = cmos_init,
8846 .data = &cmos_driver_data,
8849 .init = led_init,
8850 .data = &led_driver_data,
8853 .init = beep_init,
8854 .data = &beep_driver_data,
8857 .init = thermal_init,
8858 .data = &thermal_driver_data,
8861 .data = &ecdump_driver_data,
8864 .init = brightness_init,
8865 .data = &brightness_driver_data,
8868 .init = volume_init,
8869 .data = &volume_driver_data,
8872 .init = fan_init,
8873 .data = &fan_driver_data,
8877 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8879 unsigned int i;
8880 struct ibm_struct *ibm;
8882 if (!kp || !kp->name || !val)
8883 return -EINVAL;
8885 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8886 ibm = ibms_init[i].data;
8887 WARN_ON(ibm == NULL);
8889 if (!ibm || !ibm->name)
8890 continue;
8892 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8893 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8894 return -ENOSPC;
8895 strcpy(ibms_init[i].param, val);
8896 strcat(ibms_init[i].param, ",");
8897 return 0;
8901 return -EINVAL;
8904 module_param(experimental, int, 0444);
8905 MODULE_PARM_DESC(experimental,
8906 "Enables experimental features when non-zero");
8908 module_param_named(debug, dbg_level, uint, 0);
8909 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8911 module_param(force_load, bool, 0444);
8912 MODULE_PARM_DESC(force_load,
8913 "Attempts to load the driver even on a "
8914 "mis-identified ThinkPad when true");
8916 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8917 MODULE_PARM_DESC(fan_control,
8918 "Enables setting fan parameters features when true");
8920 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8921 MODULE_PARM_DESC(brightness_mode,
8922 "Selects brightness control strategy: "
8923 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8925 module_param(brightness_enable, uint, 0444);
8926 MODULE_PARM_DESC(brightness_enable,
8927 "Enables backlight control when 1, disables when 0");
8929 module_param(hotkey_report_mode, uint, 0444);
8930 MODULE_PARM_DESC(hotkey_report_mode,
8931 "used for backwards compatibility with userspace, "
8932 "see documentation");
8934 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8935 module_param_named(volume_mode, volume_mode, uint, 0444);
8936 MODULE_PARM_DESC(volume_mode,
8937 "Selects volume control strategy: "
8938 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8940 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8941 MODULE_PARM_DESC(volume_capabilities,
8942 "Selects the mixer capabilites: "
8943 "0=auto, 1=volume and mute, 2=mute only");
8945 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8946 MODULE_PARM_DESC(volume_control,
8947 "Enables software override for the console audio "
8948 "control when true");
8950 /* ALSA module API parameters */
8951 module_param_named(index, alsa_index, int, 0444);
8952 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8953 module_param_named(id, alsa_id, charp, 0444);
8954 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8955 module_param_named(enable, alsa_enable, bool, 0444);
8956 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8957 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8959 #define TPACPI_PARAM(feature) \
8960 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8961 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8962 "at module load, see documentation")
8964 TPACPI_PARAM(hotkey);
8965 TPACPI_PARAM(bluetooth);
8966 TPACPI_PARAM(video);
8967 TPACPI_PARAM(light);
8968 TPACPI_PARAM(cmos);
8969 TPACPI_PARAM(led);
8970 TPACPI_PARAM(beep);
8971 TPACPI_PARAM(ecdump);
8972 TPACPI_PARAM(brightness);
8973 TPACPI_PARAM(volume);
8974 TPACPI_PARAM(fan);
8976 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8977 module_param(dbg_wlswemul, uint, 0444);
8978 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8979 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8980 MODULE_PARM_DESC(wlsw_state,
8981 "Initial state of the emulated WLSW switch");
8983 module_param(dbg_bluetoothemul, uint, 0444);
8984 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8985 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8986 MODULE_PARM_DESC(bluetooth_state,
8987 "Initial state of the emulated bluetooth switch");
8989 module_param(dbg_wwanemul, uint, 0444);
8990 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8991 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8992 MODULE_PARM_DESC(wwan_state,
8993 "Initial state of the emulated WWAN switch");
8995 module_param(dbg_uwbemul, uint, 0444);
8996 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8997 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8998 MODULE_PARM_DESC(uwb_state,
8999 "Initial state of the emulated UWB switch");
9000 #endif
9002 static void thinkpad_acpi_module_exit(void)
9004 struct ibm_struct *ibm, *itmp;
9006 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9008 list_for_each_entry_safe_reverse(ibm, itmp,
9009 &tpacpi_all_drivers,
9010 all_drivers) {
9011 ibm_exit(ibm);
9014 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9016 if (tpacpi_inputdev) {
9017 if (tp_features.input_device_registered)
9018 input_unregister_device(tpacpi_inputdev);
9019 else
9020 input_free_device(tpacpi_inputdev);
9023 if (tpacpi_hwmon)
9024 hwmon_device_unregister(tpacpi_hwmon);
9026 if (tp_features.sensors_pdev_attrs_registered)
9027 device_remove_file(&tpacpi_sensors_pdev->dev,
9028 &dev_attr_thinkpad_acpi_pdev_name);
9029 if (tpacpi_sensors_pdev)
9030 platform_device_unregister(tpacpi_sensors_pdev);
9031 if (tpacpi_pdev)
9032 platform_device_unregister(tpacpi_pdev);
9034 if (tp_features.sensors_pdrv_attrs_registered)
9035 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9036 if (tp_features.platform_drv_attrs_registered)
9037 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9039 if (tp_features.sensors_pdrv_registered)
9040 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9042 if (tp_features.platform_drv_registered)
9043 platform_driver_unregister(&tpacpi_pdriver);
9045 if (proc_dir)
9046 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9048 if (tpacpi_wq)
9049 destroy_workqueue(tpacpi_wq);
9051 kfree(thinkpad_id.bios_version_str);
9052 kfree(thinkpad_id.ec_version_str);
9053 kfree(thinkpad_id.model_str);
9057 static int __init thinkpad_acpi_module_init(void)
9059 int ret, i;
9061 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9063 /* Parameter checking */
9064 if (hotkey_report_mode > 2)
9065 return -EINVAL;
9067 /* Driver-level probe */
9069 ret = get_thinkpad_model_data(&thinkpad_id);
9070 if (ret) {
9071 pr_err("unable to get DMI data: %d\n", ret);
9072 thinkpad_acpi_module_exit();
9073 return ret;
9075 ret = probe_for_thinkpad();
9076 if (ret) {
9077 thinkpad_acpi_module_exit();
9078 return ret;
9081 /* Driver initialization */
9083 thinkpad_acpi_init_banner();
9084 tpacpi_check_outdated_fw();
9086 TPACPI_ACPIHANDLE_INIT(ecrd);
9087 TPACPI_ACPIHANDLE_INIT(ecwr);
9089 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9090 if (!tpacpi_wq) {
9091 thinkpad_acpi_module_exit();
9092 return -ENOMEM;
9095 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9096 if (!proc_dir) {
9097 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
9098 thinkpad_acpi_module_exit();
9099 return -ENODEV;
9102 ret = platform_driver_register(&tpacpi_pdriver);
9103 if (ret) {
9104 pr_err("unable to register main platform driver\n");
9105 thinkpad_acpi_module_exit();
9106 return ret;
9108 tp_features.platform_drv_registered = 1;
9110 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9111 if (ret) {
9112 pr_err("unable to register hwmon platform driver\n");
9113 thinkpad_acpi_module_exit();
9114 return ret;
9116 tp_features.sensors_pdrv_registered = 1;
9118 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9119 if (!ret) {
9120 tp_features.platform_drv_attrs_registered = 1;
9121 ret = tpacpi_create_driver_attributes(
9122 &tpacpi_hwmon_pdriver.driver);
9124 if (ret) {
9125 pr_err("unable to create sysfs driver attributes\n");
9126 thinkpad_acpi_module_exit();
9127 return ret;
9129 tp_features.sensors_pdrv_attrs_registered = 1;
9132 /* Device initialization */
9133 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9134 NULL, 0);
9135 if (IS_ERR(tpacpi_pdev)) {
9136 ret = PTR_ERR(tpacpi_pdev);
9137 tpacpi_pdev = NULL;
9138 pr_err("unable to register platform device\n");
9139 thinkpad_acpi_module_exit();
9140 return ret;
9142 tpacpi_sensors_pdev = platform_device_register_simple(
9143 TPACPI_HWMON_DRVR_NAME,
9144 -1, NULL, 0);
9145 if (IS_ERR(tpacpi_sensors_pdev)) {
9146 ret = PTR_ERR(tpacpi_sensors_pdev);
9147 tpacpi_sensors_pdev = NULL;
9148 pr_err("unable to register hwmon platform device\n");
9149 thinkpad_acpi_module_exit();
9150 return ret;
9152 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9153 &dev_attr_thinkpad_acpi_pdev_name);
9154 if (ret) {
9155 pr_err("unable to create sysfs hwmon device attributes\n");
9156 thinkpad_acpi_module_exit();
9157 return ret;
9159 tp_features.sensors_pdev_attrs_registered = 1;
9160 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9161 if (IS_ERR(tpacpi_hwmon)) {
9162 ret = PTR_ERR(tpacpi_hwmon);
9163 tpacpi_hwmon = NULL;
9164 pr_err("unable to register hwmon device\n");
9165 thinkpad_acpi_module_exit();
9166 return ret;
9168 mutex_init(&tpacpi_inputdev_send_mutex);
9169 tpacpi_inputdev = input_allocate_device();
9170 if (!tpacpi_inputdev) {
9171 pr_err("unable to allocate input device\n");
9172 thinkpad_acpi_module_exit();
9173 return -ENOMEM;
9174 } else {
9175 /* Prepare input device, but don't register */
9176 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9177 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9178 tpacpi_inputdev->id.bustype = BUS_HOST;
9179 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9180 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9181 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9182 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9185 /* Init subdriver dependencies */
9186 tpacpi_detect_brightness_capabilities();
9188 /* Init subdrivers */
9189 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9190 ret = ibm_init(&ibms_init[i]);
9191 if (ret >= 0 && *ibms_init[i].param)
9192 ret = ibms_init[i].data->write(ibms_init[i].param);
9193 if (ret < 0) {
9194 thinkpad_acpi_module_exit();
9195 return ret;
9199 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9201 ret = input_register_device(tpacpi_inputdev);
9202 if (ret < 0) {
9203 pr_err("unable to register input device\n");
9204 thinkpad_acpi_module_exit();
9205 return ret;
9206 } else {
9207 tp_features.input_device_registered = 1;
9210 return 0;
9213 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9216 * This will autoload the driver in almost every ThinkPad
9217 * in widespread use.
9219 * Only _VERY_ old models, like the 240, 240x and 570 lack
9220 * the HKEY event interface.
9222 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9225 * DMI matching for module autoloading
9227 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9228 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9230 * Only models listed in thinkwiki will be supported, so add yours
9231 * if it is not there yet.
9233 #define IBM_BIOS_MODULE_ALIAS(__type) \
9234 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9236 /* Ancient thinkpad BIOSes have to be identified by
9237 * BIOS type or model number, and there are far less
9238 * BIOS types than model numbers... */
9239 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9241 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9242 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9243 MODULE_DESCRIPTION(TPACPI_DESC);
9244 MODULE_VERSION(TPACPI_VERSION);
9245 MODULE_LICENSE("GPL");
9247 module_init(thinkpad_acpi_module_init);
9248 module_exit(thinkpad_acpi_module_exit);