thinkpad-acpi: document backlight level writeback at driver init
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob25b7b9e85eb4513030e3b4f4d42490238e18170f
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 TPACPI_VERSION "0.24"
25 #define TPACPI_SYSFS_VERSION 0x020700
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/seq_file.h>
65 #include <linux/sysfs.h>
66 #include <linux/backlight.h>
67 #include <linux/fb.h>
68 #include <linux/platform_device.h>
69 #include <linux/hwmon.h>
70 #include <linux/hwmon-sysfs.h>
71 #include <linux/input.h>
72 #include <linux/leds.h>
73 #include <linux/rfkill.h>
74 #include <asm/uaccess.h>
76 #include <linux/dmi.h>
77 #include <linux/jiffies.h>
78 #include <linux/workqueue.h>
80 #include <sound/core.h>
81 #include <sound/control.h>
82 #include <sound/initval.h>
84 #include <acpi/acpi_drivers.h>
86 #include <linux/pci_ids.h>
89 /* ThinkPad CMOS commands */
90 #define TP_CMOS_VOLUME_DOWN 0
91 #define TP_CMOS_VOLUME_UP 1
92 #define TP_CMOS_VOLUME_MUTE 2
93 #define TP_CMOS_BRIGHTNESS_UP 4
94 #define TP_CMOS_BRIGHTNESS_DOWN 5
95 #define TP_CMOS_THINKLIGHT_ON 12
96 #define TP_CMOS_THINKLIGHT_OFF 13
98 /* NVRAM Addresses */
99 enum tp_nvram_addr {
100 TP_NVRAM_ADDR_HK2 = 0x57,
101 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
102 TP_NVRAM_ADDR_VIDEO = 0x59,
103 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
104 TP_NVRAM_ADDR_MIXER = 0x60,
107 /* NVRAM bit masks */
108 enum {
109 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
110 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
111 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
112 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
113 TP_NVRAM_MASK_THINKLIGHT = 0x10,
114 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
115 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
116 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
117 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
118 TP_NVRAM_MASK_MUTE = 0x40,
119 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
120 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
121 TP_NVRAM_POS_LEVEL_VOLUME = 0,
124 /* Misc NVRAM-related */
125 enum {
126 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
129 /* ACPI HIDs */
130 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
131 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
132 #define TPACPI_ACPI_EC_HID "PNP0C09"
134 /* Input IDs */
135 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
136 #define TPACPI_HKEY_INPUT_VERSION 0x4101
138 /* ACPI \WGSV commands */
139 enum {
140 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
141 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
142 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
143 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
146 /* TP_ACPI_WGSV_GET_STATE bits */
147 enum {
148 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
149 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
150 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
151 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
152 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
153 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
154 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
155 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
156 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
157 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
160 /* HKEY events */
161 enum tpacpi_hkey_event_t {
162 /* Hotkey-related */
163 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
164 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
165 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
166 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
167 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
168 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
170 /* Reasons for waking up from S3/S4 */
171 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
172 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
173 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
174 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
175 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
176 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
178 /* Auto-sleep after eject request */
179 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
180 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
182 /* Misc bay events */
183 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
185 /* User-interface events */
186 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
187 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
188 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
189 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
190 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
191 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
192 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
194 /* Thermal events */
195 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
196 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
197 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
198 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
199 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
201 /* Misc */
202 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
205 /****************************************************************************
206 * Main driver
209 #define TPACPI_NAME "thinkpad"
210 #define TPACPI_DESC "ThinkPad ACPI Extras"
211 #define TPACPI_FILE TPACPI_NAME "_acpi"
212 #define TPACPI_URL "http://ibm-acpi.sf.net/"
213 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
215 #define TPACPI_PROC_DIR "ibm"
216 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
217 #define TPACPI_DRVR_NAME TPACPI_FILE
218 #define TPACPI_DRVR_SHORTNAME "tpacpi"
219 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
221 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
222 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
224 #define TPACPI_MAX_ACPI_ARGS 3
226 /* printk headers */
227 #define TPACPI_LOG TPACPI_FILE ": "
228 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
229 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
230 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
231 #define TPACPI_ERR KERN_ERR TPACPI_LOG
232 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
233 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
234 #define TPACPI_INFO KERN_INFO TPACPI_LOG
235 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
237 /* Debugging printk groups */
238 #define TPACPI_DBG_ALL 0xffff
239 #define TPACPI_DBG_DISCLOSETASK 0x8000
240 #define TPACPI_DBG_INIT 0x0001
241 #define TPACPI_DBG_EXIT 0x0002
242 #define TPACPI_DBG_RFKILL 0x0004
243 #define TPACPI_DBG_HKEY 0x0008
244 #define TPACPI_DBG_FAN 0x0010
245 #define TPACPI_DBG_BRGHT 0x0020
246 #define TPACPI_DBG_MIXER 0x0040
248 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
249 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
250 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
253 /****************************************************************************
254 * Driver-wide structs and misc. variables
257 struct ibm_struct;
259 struct tp_acpi_drv_struct {
260 const struct acpi_device_id *hid;
261 struct acpi_driver *driver;
263 void (*notify) (struct ibm_struct *, u32);
264 acpi_handle *handle;
265 u32 type;
266 struct acpi_device *device;
269 struct ibm_struct {
270 char *name;
272 int (*read) (struct seq_file *);
273 int (*write) (char *);
274 void (*exit) (void);
275 void (*resume) (void);
276 void (*suspend) (pm_message_t state);
277 void (*shutdown) (void);
279 struct list_head all_drivers;
281 struct tp_acpi_drv_struct *acpi;
283 struct {
284 u8 acpi_driver_registered:1;
285 u8 acpi_notify_installed:1;
286 u8 proc_created:1;
287 u8 init_called:1;
288 u8 experimental:1;
289 } flags;
292 struct ibm_init_struct {
293 char param[32];
295 int (*init) (struct ibm_init_struct *);
296 mode_t base_procfs_mode;
297 struct ibm_struct *data;
300 static struct {
301 u32 bluetooth:1;
302 u32 hotkey:1;
303 u32 hotkey_mask:1;
304 u32 hotkey_wlsw:1;
305 u32 hotkey_tablet:1;
306 u32 light:1;
307 u32 light_status:1;
308 u32 bright_acpimode:1;
309 u32 bright_unkfw:1;
310 u32 wan:1;
311 u32 uwb:1;
312 u32 fan_ctrl_status_undef:1;
313 u32 second_fan:1;
314 u32 beep_needs_two_args:1;
315 u32 mixer_no_level_control:1;
316 u32 input_device_registered:1;
317 u32 platform_drv_registered:1;
318 u32 platform_drv_attrs_registered:1;
319 u32 sensors_pdrv_registered:1;
320 u32 sensors_pdrv_attrs_registered:1;
321 u32 sensors_pdev_attrs_registered:1;
322 u32 hotkey_poll_active:1;
323 } tp_features;
325 static struct {
326 u16 hotkey_mask_ff:1;
327 u16 volume_ctrl_forbidden:1;
328 } tp_warned;
330 struct thinkpad_id_data {
331 unsigned int vendor; /* ThinkPad vendor:
332 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
334 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
335 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
337 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
338 u16 ec_model;
339 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
340 u16 ec_release;
342 char *model_str; /* ThinkPad T43 */
343 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
345 static struct thinkpad_id_data thinkpad_id;
347 static enum {
348 TPACPI_LIFE_INIT = 0,
349 TPACPI_LIFE_RUNNING,
350 TPACPI_LIFE_EXITING,
351 } tpacpi_lifecycle;
353 static int experimental;
354 static u32 dbg_level;
356 static struct workqueue_struct *tpacpi_wq;
358 enum led_status_t {
359 TPACPI_LED_OFF = 0,
360 TPACPI_LED_ON,
361 TPACPI_LED_BLINK,
364 /* Special LED class that can defer work */
365 struct tpacpi_led_classdev {
366 struct led_classdev led_classdev;
367 struct work_struct work;
368 enum led_status_t new_state;
369 unsigned int led;
372 /* brightness level capabilities */
373 static unsigned int bright_maxlvl; /* 0 = unknown */
375 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
376 static int dbg_wlswemul;
377 static int tpacpi_wlsw_emulstate;
378 static int dbg_bluetoothemul;
379 static int tpacpi_bluetooth_emulstate;
380 static int dbg_wwanemul;
381 static int tpacpi_wwan_emulstate;
382 static int dbg_uwbemul;
383 static int tpacpi_uwb_emulstate;
384 #endif
387 /*************************************************************************
388 * Debugging helpers
391 #define dbg_printk(a_dbg_level, format, arg...) \
392 do { if (dbg_level & (a_dbg_level)) \
393 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
394 } while (0)
396 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
397 #define vdbg_printk dbg_printk
398 static const char *str_supported(int is_supported);
399 #else
400 #define vdbg_printk(a_dbg_level, format, arg...) \
401 do { } while (0)
402 #endif
404 static void tpacpi_log_usertask(const char * const what)
406 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
407 what, task_tgid_vnr(current));
410 #define tpacpi_disclose_usertask(what, format, arg...) \
411 do { \
412 if (unlikely( \
413 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
414 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
415 printk(TPACPI_DEBUG "%s: PID %d: " format, \
416 what, task_tgid_vnr(current), ## arg); \
418 } while (0)
421 * Quirk handling helpers
423 * ThinkPad IDs and versions seen in the field so far
424 * are two-characters from the set [0-9A-Z], i.e. base 36.
426 * We use values well outside that range as specials.
429 #define TPACPI_MATCH_ANY 0xffffU
430 #define TPACPI_MATCH_UNKNOWN 0U
432 /* TPID('1', 'Y') == 0x5931 */
433 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
435 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
436 { .vendor = PCI_VENDOR_ID_IBM, \
437 .bios = TPID(__id1, __id2), \
438 .ec = TPACPI_MATCH_ANY, \
439 .quirks = (__quirk) }
441 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
442 { .vendor = PCI_VENDOR_ID_LENOVO, \
443 .bios = TPID(__id1, __id2), \
444 .ec = TPACPI_MATCH_ANY, \
445 .quirks = (__quirk) }
447 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
448 { .vendor = PCI_VENDOR_ID_LENOVO, \
449 .bios = TPACPI_MATCH_ANY, \
450 .ec = TPID(__id1, __id2), \
451 .quirks = (__quirk) }
453 struct tpacpi_quirk {
454 unsigned int vendor;
455 u16 bios;
456 u16 ec;
457 unsigned long quirks;
461 * tpacpi_check_quirks() - search BIOS/EC version on a list
462 * @qlist: array of &struct tpacpi_quirk
463 * @qlist_size: number of elements in @qlist
465 * Iterates over a quirks list until one is found that matches the
466 * ThinkPad's vendor, BIOS and EC model.
468 * Returns 0 if nothing matches, otherwise returns the quirks field of
469 * the matching &struct tpacpi_quirk entry.
471 * The match criteria is: vendor, ec and bios much match.
473 static unsigned long __init tpacpi_check_quirks(
474 const struct tpacpi_quirk *qlist,
475 unsigned int qlist_size)
477 while (qlist_size) {
478 if ((qlist->vendor == thinkpad_id.vendor ||
479 qlist->vendor == TPACPI_MATCH_ANY) &&
480 (qlist->bios == thinkpad_id.bios_model ||
481 qlist->bios == TPACPI_MATCH_ANY) &&
482 (qlist->ec == thinkpad_id.ec_model ||
483 qlist->ec == TPACPI_MATCH_ANY))
484 return qlist->quirks;
486 qlist_size--;
487 qlist++;
489 return 0;
492 static inline bool __pure __init tpacpi_is_lenovo(void)
494 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
497 static inline bool __pure __init tpacpi_is_ibm(void)
499 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
502 /****************************************************************************
503 ****************************************************************************
505 * ACPI Helpers and device model
507 ****************************************************************************
508 ****************************************************************************/
510 /*************************************************************************
511 * ACPI basic handles
514 static acpi_handle root_handle;
515 static acpi_handle ec_handle;
517 #define TPACPI_HANDLE(object, parent, paths...) \
518 static acpi_handle object##_handle; \
519 static const acpi_handle *object##_parent __initdata = \
520 &parent##_handle; \
521 static char *object##_paths[] __initdata = { paths }
523 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
524 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
526 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
527 /* T4x, X31, X40 */
528 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
529 "\\CMS", /* R40, R40e */
530 ); /* all others */
532 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
533 "^HKEY", /* R30, R31 */
534 "HKEY", /* all others */
535 ); /* 570 */
537 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
538 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
539 "\\_SB.PCI0.VID0", /* 770e */
540 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
541 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
542 "\\_SB.PCI0.AGP.VID", /* all others */
543 ); /* R30, R31 */
546 /*************************************************************************
547 * ACPI helpers
550 static int acpi_evalf(acpi_handle handle,
551 void *res, char *method, char *fmt, ...)
553 char *fmt0 = fmt;
554 struct acpi_object_list params;
555 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
556 struct acpi_buffer result, *resultp;
557 union acpi_object out_obj;
558 acpi_status status;
559 va_list ap;
560 char res_type;
561 int success;
562 int quiet;
564 if (!*fmt) {
565 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
566 return 0;
569 if (*fmt == 'q') {
570 quiet = 1;
571 fmt++;
572 } else
573 quiet = 0;
575 res_type = *(fmt++);
577 params.count = 0;
578 params.pointer = &in_objs[0];
580 va_start(ap, fmt);
581 while (*fmt) {
582 char c = *(fmt++);
583 switch (c) {
584 case 'd': /* int */
585 in_objs[params.count].integer.value = va_arg(ap, int);
586 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
587 break;
588 /* add more types as needed */
589 default:
590 printk(TPACPI_ERR "acpi_evalf() called "
591 "with invalid format character '%c'\n", c);
592 return 0;
595 va_end(ap);
597 if (res_type != 'v') {
598 result.length = sizeof(out_obj);
599 result.pointer = &out_obj;
600 resultp = &result;
601 } else
602 resultp = NULL;
604 status = acpi_evaluate_object(handle, method, &params, resultp);
606 switch (res_type) {
607 case 'd': /* int */
608 success = (status == AE_OK &&
609 out_obj.type == ACPI_TYPE_INTEGER);
610 if (success && res)
611 *(int *)res = out_obj.integer.value;
612 break;
613 case 'v': /* void */
614 success = status == AE_OK;
615 break;
616 /* add more types as needed */
617 default:
618 printk(TPACPI_ERR "acpi_evalf() called "
619 "with invalid format character '%c'\n", res_type);
620 return 0;
623 if (!success && !quiet)
624 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %s\n",
625 method, fmt0, acpi_format_exception(status));
627 return success;
630 static int acpi_ec_read(int i, u8 *p)
632 int v;
634 if (ecrd_handle) {
635 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
636 return 0;
637 *p = v;
638 } else {
639 if (ec_read(i, p) < 0)
640 return 0;
643 return 1;
646 static int acpi_ec_write(int i, u8 v)
648 if (ecwr_handle) {
649 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
650 return 0;
651 } else {
652 if (ec_write(i, v) < 0)
653 return 0;
656 return 1;
659 static int issue_thinkpad_cmos_command(int cmos_cmd)
661 if (!cmos_handle)
662 return -ENXIO;
664 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
665 return -EIO;
667 return 0;
670 /*************************************************************************
671 * ACPI device model
674 #define TPACPI_ACPIHANDLE_INIT(object) \
675 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
676 object##_paths, ARRAY_SIZE(object##_paths))
678 static void __init drv_acpi_handle_init(const char *name,
679 acpi_handle *handle, const acpi_handle parent,
680 char **paths, const int num_paths)
682 int i;
683 acpi_status status;
685 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
686 name);
688 for (i = 0; i < num_paths; i++) {
689 status = acpi_get_handle(parent, paths[i], handle);
690 if (ACPI_SUCCESS(status)) {
691 dbg_printk(TPACPI_DBG_INIT,
692 "Found ACPI handle %s for %s\n",
693 paths[i], name);
694 return;
698 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
699 name);
700 *handle = NULL;
703 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
704 u32 level, void *context, void **return_value)
706 *(acpi_handle *)return_value = handle;
708 return AE_CTRL_TERMINATE;
711 static void __init tpacpi_acpi_handle_locate(const char *name,
712 const char *hid,
713 acpi_handle *handle)
715 acpi_status status;
716 acpi_handle device_found;
718 BUG_ON(!name || !hid || !handle);
719 vdbg_printk(TPACPI_DBG_INIT,
720 "trying to locate ACPI handle for %s, using HID %s\n",
721 name, hid);
723 memset(&device_found, 0, sizeof(device_found));
724 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
725 (void *)name, &device_found);
727 *handle = NULL;
729 if (ACPI_SUCCESS(status)) {
730 *handle = device_found;
731 dbg_printk(TPACPI_DBG_INIT,
732 "Found ACPI handle for %s\n", name);
733 } else {
734 vdbg_printk(TPACPI_DBG_INIT,
735 "Could not locate an ACPI handle for %s: %s\n",
736 name, acpi_format_exception(status));
740 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
742 struct ibm_struct *ibm = data;
744 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
745 return;
747 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
748 return;
750 ibm->acpi->notify(ibm, event);
753 static int __init setup_acpi_notify(struct ibm_struct *ibm)
755 acpi_status status;
756 int rc;
758 BUG_ON(!ibm->acpi);
760 if (!*ibm->acpi->handle)
761 return 0;
763 vdbg_printk(TPACPI_DBG_INIT,
764 "setting up ACPI notify for %s\n", ibm->name);
766 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
767 if (rc < 0) {
768 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
769 ibm->name, rc);
770 return -ENODEV;
773 ibm->acpi->device->driver_data = ibm;
774 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
775 TPACPI_ACPI_EVENT_PREFIX,
776 ibm->name);
778 status = acpi_install_notify_handler(*ibm->acpi->handle,
779 ibm->acpi->type, dispatch_acpi_notify, ibm);
780 if (ACPI_FAILURE(status)) {
781 if (status == AE_ALREADY_EXISTS) {
782 printk(TPACPI_NOTICE
783 "another device driver is already "
784 "handling %s events\n", ibm->name);
785 } else {
786 printk(TPACPI_ERR
787 "acpi_install_notify_handler(%s) failed: %s\n",
788 ibm->name, acpi_format_exception(status));
790 return -ENODEV;
792 ibm->flags.acpi_notify_installed = 1;
793 return 0;
796 static int __init tpacpi_device_add(struct acpi_device *device)
798 return 0;
801 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
803 int rc;
805 dbg_printk(TPACPI_DBG_INIT,
806 "registering %s as an ACPI driver\n", ibm->name);
808 BUG_ON(!ibm->acpi);
810 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
811 if (!ibm->acpi->driver) {
812 printk(TPACPI_ERR
813 "failed to allocate memory for ibm->acpi->driver\n");
814 return -ENOMEM;
817 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
818 ibm->acpi->driver->ids = ibm->acpi->hid;
820 ibm->acpi->driver->ops.add = &tpacpi_device_add;
822 rc = acpi_bus_register_driver(ibm->acpi->driver);
823 if (rc < 0) {
824 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
825 ibm->name, rc);
826 kfree(ibm->acpi->driver);
827 ibm->acpi->driver = NULL;
828 } else if (!rc)
829 ibm->flags.acpi_driver_registered = 1;
831 return rc;
835 /****************************************************************************
836 ****************************************************************************
838 * Procfs Helpers
840 ****************************************************************************
841 ****************************************************************************/
843 static int dispatch_proc_show(struct seq_file *m, void *v)
845 struct ibm_struct *ibm = m->private;
847 if (!ibm || !ibm->read)
848 return -EINVAL;
849 return ibm->read(m);
852 static int dispatch_proc_open(struct inode *inode, struct file *file)
854 return single_open(file, dispatch_proc_show, PDE(inode)->data);
857 static ssize_t dispatch_proc_write(struct file *file,
858 const char __user *userbuf,
859 size_t count, loff_t *pos)
861 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
862 char *kernbuf;
863 int ret;
865 if (!ibm || !ibm->write)
866 return -EINVAL;
867 if (count > PAGE_SIZE - 2)
868 return -EINVAL;
870 kernbuf = kmalloc(count + 2, GFP_KERNEL);
871 if (!kernbuf)
872 return -ENOMEM;
874 if (copy_from_user(kernbuf, userbuf, count)) {
875 kfree(kernbuf);
876 return -EFAULT;
879 kernbuf[count] = 0;
880 strcat(kernbuf, ",");
881 ret = ibm->write(kernbuf);
882 if (ret == 0)
883 ret = count;
885 kfree(kernbuf);
887 return ret;
890 static const struct file_operations dispatch_proc_fops = {
891 .owner = THIS_MODULE,
892 .open = dispatch_proc_open,
893 .read = seq_read,
894 .llseek = seq_lseek,
895 .release = single_release,
896 .write = dispatch_proc_write,
899 static char *next_cmd(char **cmds)
901 char *start = *cmds;
902 char *end;
904 while ((end = strchr(start, ',')) && end == start)
905 start = end + 1;
907 if (!end)
908 return NULL;
910 *end = 0;
911 *cmds = end + 1;
912 return start;
916 /****************************************************************************
917 ****************************************************************************
919 * Device model: input, hwmon and platform
921 ****************************************************************************
922 ****************************************************************************/
924 static struct platform_device *tpacpi_pdev;
925 static struct platform_device *tpacpi_sensors_pdev;
926 static struct device *tpacpi_hwmon;
927 static struct input_dev *tpacpi_inputdev;
928 static struct mutex tpacpi_inputdev_send_mutex;
929 static LIST_HEAD(tpacpi_all_drivers);
931 static int tpacpi_suspend_handler(struct platform_device *pdev,
932 pm_message_t state)
934 struct ibm_struct *ibm, *itmp;
936 list_for_each_entry_safe(ibm, itmp,
937 &tpacpi_all_drivers,
938 all_drivers) {
939 if (ibm->suspend)
940 (ibm->suspend)(state);
943 return 0;
946 static int tpacpi_resume_handler(struct platform_device *pdev)
948 struct ibm_struct *ibm, *itmp;
950 list_for_each_entry_safe(ibm, itmp,
951 &tpacpi_all_drivers,
952 all_drivers) {
953 if (ibm->resume)
954 (ibm->resume)();
957 return 0;
960 static void tpacpi_shutdown_handler(struct platform_device *pdev)
962 struct ibm_struct *ibm, *itmp;
964 list_for_each_entry_safe(ibm, itmp,
965 &tpacpi_all_drivers,
966 all_drivers) {
967 if (ibm->shutdown)
968 (ibm->shutdown)();
972 static struct platform_driver tpacpi_pdriver = {
973 .driver = {
974 .name = TPACPI_DRVR_NAME,
975 .owner = THIS_MODULE,
977 .suspend = tpacpi_suspend_handler,
978 .resume = tpacpi_resume_handler,
979 .shutdown = tpacpi_shutdown_handler,
982 static struct platform_driver tpacpi_hwmon_pdriver = {
983 .driver = {
984 .name = TPACPI_HWMON_DRVR_NAME,
985 .owner = THIS_MODULE,
989 /*************************************************************************
990 * sysfs support helpers
993 struct attribute_set {
994 unsigned int members, max_members;
995 struct attribute_group group;
998 struct attribute_set_obj {
999 struct attribute_set s;
1000 struct attribute *a;
1001 } __attribute__((packed));
1003 static struct attribute_set *create_attr_set(unsigned int max_members,
1004 const char *name)
1006 struct attribute_set_obj *sobj;
1008 if (max_members == 0)
1009 return NULL;
1011 /* Allocates space for implicit NULL at the end too */
1012 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1013 max_members * sizeof(struct attribute *),
1014 GFP_KERNEL);
1015 if (!sobj)
1016 return NULL;
1017 sobj->s.max_members = max_members;
1018 sobj->s.group.attrs = &sobj->a;
1019 sobj->s.group.name = name;
1021 return &sobj->s;
1024 #define destroy_attr_set(_set) \
1025 kfree(_set);
1027 /* not multi-threaded safe, use it in a single thread per set */
1028 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1030 if (!s || !attr)
1031 return -EINVAL;
1033 if (s->members >= s->max_members)
1034 return -ENOMEM;
1036 s->group.attrs[s->members] = attr;
1037 s->members++;
1039 return 0;
1042 static int add_many_to_attr_set(struct attribute_set *s,
1043 struct attribute **attr,
1044 unsigned int count)
1046 int i, res;
1048 for (i = 0; i < count; i++) {
1049 res = add_to_attr_set(s, attr[i]);
1050 if (res)
1051 return res;
1054 return 0;
1057 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1059 sysfs_remove_group(kobj, &s->group);
1060 destroy_attr_set(s);
1063 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1064 sysfs_create_group(_kobj, &_attr_set->group)
1066 static int parse_strtoul(const char *buf,
1067 unsigned long max, unsigned long *value)
1069 char *endp;
1071 while (*buf && isspace(*buf))
1072 buf++;
1073 *value = simple_strtoul(buf, &endp, 0);
1074 while (*endp && isspace(*endp))
1075 endp++;
1076 if (*endp || *value > max)
1077 return -EINVAL;
1079 return 0;
1082 static void tpacpi_disable_brightness_delay(void)
1084 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1085 printk(TPACPI_NOTICE
1086 "ACPI backlight control delay disabled\n");
1089 static void printk_deprecated_attribute(const char * const what,
1090 const char * const details)
1092 tpacpi_log_usertask("deprecated sysfs attribute");
1093 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1094 "will be removed. %s\n",
1095 what, details);
1098 /*************************************************************************
1099 * rfkill and radio control support helpers
1103 * ThinkPad-ACPI firmware handling model:
1105 * WLSW (master wireless switch) is event-driven, and is common to all
1106 * firmware-controlled radios. It cannot be controlled, just monitored,
1107 * as expected. It overrides all radio state in firmware
1109 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1110 * (TODO: verify how WLSW interacts with the returned radio state).
1112 * The only time there are shadow radio state changes, is when
1113 * masked-off hotkeys are used.
1117 * Internal driver API for radio state:
1119 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1120 * bool: true means radio blocked (off)
1122 enum tpacpi_rfkill_state {
1123 TPACPI_RFK_RADIO_OFF = 0,
1124 TPACPI_RFK_RADIO_ON
1127 /* rfkill switches */
1128 enum tpacpi_rfk_id {
1129 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1130 TPACPI_RFK_WWAN_SW_ID,
1131 TPACPI_RFK_UWB_SW_ID,
1132 TPACPI_RFK_SW_MAX
1135 static const char *tpacpi_rfkill_names[] = {
1136 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1137 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1138 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1139 [TPACPI_RFK_SW_MAX] = NULL
1142 /* ThinkPad-ACPI rfkill subdriver */
1143 struct tpacpi_rfk {
1144 struct rfkill *rfkill;
1145 enum tpacpi_rfk_id id;
1146 const struct tpacpi_rfk_ops *ops;
1149 struct tpacpi_rfk_ops {
1150 /* firmware interface */
1151 int (*get_status)(void);
1152 int (*set_status)(const enum tpacpi_rfkill_state);
1155 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1157 /* Query FW and update rfkill sw state for a given rfkill switch */
1158 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1160 int status;
1162 if (!tp_rfk)
1163 return -ENODEV;
1165 status = (tp_rfk->ops->get_status)();
1166 if (status < 0)
1167 return status;
1169 rfkill_set_sw_state(tp_rfk->rfkill,
1170 (status == TPACPI_RFK_RADIO_OFF));
1172 return status;
1175 /* Query FW and update rfkill sw state for all rfkill switches */
1176 static void tpacpi_rfk_update_swstate_all(void)
1178 unsigned int i;
1180 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1181 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1185 * Sync the HW-blocking state of all rfkill switches,
1186 * do notice it causes the rfkill core to schedule uevents
1188 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1190 unsigned int i;
1191 struct tpacpi_rfk *tp_rfk;
1193 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1194 tp_rfk = tpacpi_rfkill_switches[i];
1195 if (tp_rfk) {
1196 if (rfkill_set_hw_state(tp_rfk->rfkill,
1197 blocked)) {
1198 /* ignore -- we track sw block */
1204 /* Call to get the WLSW state from the firmware */
1205 static int hotkey_get_wlsw(void);
1207 /* Call to query WLSW state and update all rfkill switches */
1208 static bool tpacpi_rfk_check_hwblock_state(void)
1210 int res = hotkey_get_wlsw();
1211 int hw_blocked;
1213 /* When unknown or unsupported, we have to assume it is unblocked */
1214 if (res < 0)
1215 return false;
1217 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1218 tpacpi_rfk_update_hwblock_state(hw_blocked);
1220 return hw_blocked;
1223 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1225 struct tpacpi_rfk *tp_rfk = data;
1226 int res;
1228 dbg_printk(TPACPI_DBG_RFKILL,
1229 "request to change radio state to %s\n",
1230 blocked ? "blocked" : "unblocked");
1232 /* try to set radio state */
1233 res = (tp_rfk->ops->set_status)(blocked ?
1234 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1236 /* and update the rfkill core with whatever the FW really did */
1237 tpacpi_rfk_update_swstate(tp_rfk);
1239 return (res < 0) ? res : 0;
1242 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1243 .set_block = tpacpi_rfk_hook_set_block,
1246 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1247 const struct tpacpi_rfk_ops *tp_rfkops,
1248 const enum rfkill_type rfktype,
1249 const char *name,
1250 const bool set_default)
1252 struct tpacpi_rfk *atp_rfk;
1253 int res;
1254 bool sw_state = false;
1255 bool hw_state;
1256 int sw_status;
1258 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1260 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1261 if (atp_rfk)
1262 atp_rfk->rfkill = rfkill_alloc(name,
1263 &tpacpi_pdev->dev,
1264 rfktype,
1265 &tpacpi_rfk_rfkill_ops,
1266 atp_rfk);
1267 if (!atp_rfk || !atp_rfk->rfkill) {
1268 printk(TPACPI_ERR
1269 "failed to allocate memory for rfkill class\n");
1270 kfree(atp_rfk);
1271 return -ENOMEM;
1274 atp_rfk->id = id;
1275 atp_rfk->ops = tp_rfkops;
1277 sw_status = (tp_rfkops->get_status)();
1278 if (sw_status < 0) {
1279 printk(TPACPI_ERR
1280 "failed to read initial state for %s, error %d\n",
1281 name, sw_status);
1282 } else {
1283 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1284 if (set_default) {
1285 /* try to keep the initial state, since we ask the
1286 * firmware to preserve it across S5 in NVRAM */
1287 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1290 hw_state = tpacpi_rfk_check_hwblock_state();
1291 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1293 res = rfkill_register(atp_rfk->rfkill);
1294 if (res < 0) {
1295 printk(TPACPI_ERR
1296 "failed to register %s rfkill switch: %d\n",
1297 name, res);
1298 rfkill_destroy(atp_rfk->rfkill);
1299 kfree(atp_rfk);
1300 return res;
1303 tpacpi_rfkill_switches[id] = atp_rfk;
1305 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1306 name, (sw_state || hw_state) ? "" : "un");
1307 return 0;
1310 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1312 struct tpacpi_rfk *tp_rfk;
1314 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1316 tp_rfk = tpacpi_rfkill_switches[id];
1317 if (tp_rfk) {
1318 rfkill_unregister(tp_rfk->rfkill);
1319 rfkill_destroy(tp_rfk->rfkill);
1320 tpacpi_rfkill_switches[id] = NULL;
1321 kfree(tp_rfk);
1325 static void printk_deprecated_rfkill_attribute(const char * const what)
1327 printk_deprecated_attribute(what,
1328 "Please switch to generic rfkill before year 2010");
1331 /* sysfs <radio> enable ------------------------------------------------ */
1332 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1333 struct device_attribute *attr,
1334 char *buf)
1336 int status;
1338 printk_deprecated_rfkill_attribute(attr->attr.name);
1340 /* This is in the ABI... */
1341 if (tpacpi_rfk_check_hwblock_state()) {
1342 status = TPACPI_RFK_RADIO_OFF;
1343 } else {
1344 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1345 if (status < 0)
1346 return status;
1349 return snprintf(buf, PAGE_SIZE, "%d\n",
1350 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1353 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1354 struct device_attribute *attr,
1355 const char *buf, size_t count)
1357 unsigned long t;
1358 int res;
1360 printk_deprecated_rfkill_attribute(attr->attr.name);
1362 if (parse_strtoul(buf, 1, &t))
1363 return -EINVAL;
1365 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1367 /* This is in the ABI... */
1368 if (tpacpi_rfk_check_hwblock_state() && !!t)
1369 return -EPERM;
1371 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1372 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1373 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1375 return (res < 0) ? res : count;
1378 /* procfs -------------------------------------------------------------- */
1379 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id,
1380 struct seq_file *m)
1382 if (id >= TPACPI_RFK_SW_MAX)
1383 seq_printf(m, "status:\t\tnot supported\n");
1384 else {
1385 int status;
1387 /* This is in the ABI... */
1388 if (tpacpi_rfk_check_hwblock_state()) {
1389 status = TPACPI_RFK_RADIO_OFF;
1390 } else {
1391 status = tpacpi_rfk_update_swstate(
1392 tpacpi_rfkill_switches[id]);
1393 if (status < 0)
1394 return status;
1397 seq_printf(m, "status:\t\t%s\n",
1398 (status == TPACPI_RFK_RADIO_ON) ?
1399 "enabled" : "disabled");
1400 seq_printf(m, "commands:\tenable, disable\n");
1403 return 0;
1406 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1408 char *cmd;
1409 int status = -1;
1410 int res = 0;
1412 if (id >= TPACPI_RFK_SW_MAX)
1413 return -ENODEV;
1415 while ((cmd = next_cmd(&buf))) {
1416 if (strlencmp(cmd, "enable") == 0)
1417 status = TPACPI_RFK_RADIO_ON;
1418 else if (strlencmp(cmd, "disable") == 0)
1419 status = TPACPI_RFK_RADIO_OFF;
1420 else
1421 return -EINVAL;
1424 if (status != -1) {
1425 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1426 (status == TPACPI_RFK_RADIO_ON) ?
1427 "enable" : "disable",
1428 tpacpi_rfkill_names[id]);
1429 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1430 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1433 return res;
1436 /*************************************************************************
1437 * thinkpad-acpi driver attributes
1440 /* interface_version --------------------------------------------------- */
1441 static ssize_t tpacpi_driver_interface_version_show(
1442 struct device_driver *drv,
1443 char *buf)
1445 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1448 static DRIVER_ATTR(interface_version, S_IRUGO,
1449 tpacpi_driver_interface_version_show, NULL);
1451 /* debug_level --------------------------------------------------------- */
1452 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1453 char *buf)
1455 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1458 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1459 const char *buf, size_t count)
1461 unsigned long t;
1463 if (parse_strtoul(buf, 0xffff, &t))
1464 return -EINVAL;
1466 dbg_level = t;
1468 return count;
1471 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1472 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1474 /* version ------------------------------------------------------------- */
1475 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1476 char *buf)
1478 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1479 TPACPI_DESC, TPACPI_VERSION);
1482 static DRIVER_ATTR(version, S_IRUGO,
1483 tpacpi_driver_version_show, NULL);
1485 /* --------------------------------------------------------------------- */
1487 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1489 /* wlsw_emulstate ------------------------------------------------------ */
1490 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1491 char *buf)
1493 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1496 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1497 const char *buf, size_t count)
1499 unsigned long t;
1501 if (parse_strtoul(buf, 1, &t))
1502 return -EINVAL;
1504 if (tpacpi_wlsw_emulstate != !!t) {
1505 tpacpi_wlsw_emulstate = !!t;
1506 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1509 return count;
1512 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1513 tpacpi_driver_wlsw_emulstate_show,
1514 tpacpi_driver_wlsw_emulstate_store);
1516 /* bluetooth_emulstate ------------------------------------------------- */
1517 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1518 struct device_driver *drv,
1519 char *buf)
1521 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1524 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1525 struct device_driver *drv,
1526 const char *buf, size_t count)
1528 unsigned long t;
1530 if (parse_strtoul(buf, 1, &t))
1531 return -EINVAL;
1533 tpacpi_bluetooth_emulstate = !!t;
1535 return count;
1538 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1539 tpacpi_driver_bluetooth_emulstate_show,
1540 tpacpi_driver_bluetooth_emulstate_store);
1542 /* wwan_emulstate ------------------------------------------------- */
1543 static ssize_t tpacpi_driver_wwan_emulstate_show(
1544 struct device_driver *drv,
1545 char *buf)
1547 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1550 static ssize_t tpacpi_driver_wwan_emulstate_store(
1551 struct device_driver *drv,
1552 const char *buf, size_t count)
1554 unsigned long t;
1556 if (parse_strtoul(buf, 1, &t))
1557 return -EINVAL;
1559 tpacpi_wwan_emulstate = !!t;
1561 return count;
1564 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1565 tpacpi_driver_wwan_emulstate_show,
1566 tpacpi_driver_wwan_emulstate_store);
1568 /* uwb_emulstate ------------------------------------------------- */
1569 static ssize_t tpacpi_driver_uwb_emulstate_show(
1570 struct device_driver *drv,
1571 char *buf)
1573 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1576 static ssize_t tpacpi_driver_uwb_emulstate_store(
1577 struct device_driver *drv,
1578 const char *buf, size_t count)
1580 unsigned long t;
1582 if (parse_strtoul(buf, 1, &t))
1583 return -EINVAL;
1585 tpacpi_uwb_emulstate = !!t;
1587 return count;
1590 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1591 tpacpi_driver_uwb_emulstate_show,
1592 tpacpi_driver_uwb_emulstate_store);
1593 #endif
1595 /* --------------------------------------------------------------------- */
1597 static struct driver_attribute *tpacpi_driver_attributes[] = {
1598 &driver_attr_debug_level, &driver_attr_version,
1599 &driver_attr_interface_version,
1602 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1604 int i, res;
1606 i = 0;
1607 res = 0;
1608 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1609 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1610 i++;
1613 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1614 if (!res && dbg_wlswemul)
1615 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1616 if (!res && dbg_bluetoothemul)
1617 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1618 if (!res && dbg_wwanemul)
1619 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1620 if (!res && dbg_uwbemul)
1621 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1622 #endif
1624 return res;
1627 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1629 int i;
1631 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1632 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1634 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1635 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1636 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1637 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1638 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1639 #endif
1642 /*************************************************************************
1643 * Firmware Data
1647 * Table of recommended minimum BIOS versions
1649 * Reasons for listing:
1650 * 1. Stable BIOS, listed because the unknown amount of
1651 * bugs and bad ACPI behaviour on older versions
1653 * 2. BIOS or EC fw with known bugs that trigger on Linux
1655 * 3. BIOS with known reduced functionality in older versions
1657 * We recommend the latest BIOS and EC version.
1658 * We only support the latest BIOS and EC fw version as a rule.
1660 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1661 * Information from users in ThinkWiki
1663 * WARNING: we use this table also to detect that the machine is
1664 * a ThinkPad in some cases, so don't remove entries lightly.
1667 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1668 { .vendor = (__v), \
1669 .bios = TPID(__id1, __id2), \
1670 .ec = TPACPI_MATCH_ANY, \
1671 .quirks = TPACPI_MATCH_ANY << 16 \
1672 | (__bv1) << 8 | (__bv2) }
1674 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1675 __eid, __ev1, __ev2) \
1676 { .vendor = (__v), \
1677 .bios = TPID(__bid1, __bid2), \
1678 .ec = __eid, \
1679 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1680 | (__bv1) << 8 | (__bv2) }
1682 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1683 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1685 /* Outdated IBM BIOSes often lack the EC id string */
1686 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1687 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1688 __bv1, __bv2, TPID(__id1, __id2), \
1689 __ev1, __ev2), \
1690 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1691 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1692 __ev1, __ev2)
1694 /* Outdated IBM BIOSes often lack the EC id string */
1695 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1696 __eid1, __eid2, __ev1, __ev2) \
1697 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1698 __bv1, __bv2, TPID(__eid1, __eid2), \
1699 __ev1, __ev2), \
1700 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1701 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1702 __ev1, __ev2)
1704 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1705 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1707 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1708 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1709 __bv1, __bv2, TPID(__id1, __id2), \
1710 __ev1, __ev2)
1712 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1713 __eid1, __eid2, __ev1, __ev2) \
1714 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1715 __bv1, __bv2, TPID(__eid1, __eid2), \
1716 __ev1, __ev2)
1718 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1719 /* Numeric models ------------------ */
1720 /* FW MODEL BIOS VERS */
1721 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1722 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1723 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1724 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1725 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1726 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1727 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1728 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1729 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1731 /* A-series ------------------------- */
1732 /* FW MODEL BIOS VERS EC VERS */
1733 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1734 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1735 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1736 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1737 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1738 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1739 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1740 TPV_QI0('1', '3', '2', '0'), /* A22m */
1741 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1742 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1743 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1745 /* G-series ------------------------- */
1746 /* FW MODEL BIOS VERS */
1747 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1748 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1750 /* R-series, T-series --------------- */
1751 /* FW MODEL BIOS VERS EC VERS */
1752 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1753 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1754 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1755 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1756 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1757 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1758 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1759 T40/p, T41/p, T42/p (1) */
1760 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1761 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1762 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1763 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1765 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1766 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1767 TPV_QI0('1', '6', '3', '2'), /* T22 */
1768 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1769 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1770 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1772 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1773 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1774 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1776 /* BIOS FW BIOS VERS EC FW EC VERS */
1777 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1778 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1780 /* X-series ------------------------- */
1781 /* FW MODEL BIOS VERS EC VERS */
1782 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1783 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1784 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1785 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1786 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1787 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1788 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1790 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1791 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1793 /* (0) - older versions lack DMI EC fw string and functionality */
1794 /* (1) - older versions known to lack functionality */
1797 #undef TPV_QL1
1798 #undef TPV_QL0
1799 #undef TPV_QI2
1800 #undef TPV_QI1
1801 #undef TPV_QI0
1802 #undef TPV_Q_X
1803 #undef TPV_Q
1805 static void __init tpacpi_check_outdated_fw(void)
1807 unsigned long fwvers;
1808 u16 ec_version, bios_version;
1810 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1811 ARRAY_SIZE(tpacpi_bios_version_qtable));
1813 if (!fwvers)
1814 return;
1816 bios_version = fwvers & 0xffffU;
1817 ec_version = (fwvers >> 16) & 0xffffU;
1819 /* note that unknown versions are set to 0x0000 and we use that */
1820 if ((bios_version > thinkpad_id.bios_release) ||
1821 (ec_version > thinkpad_id.ec_release &&
1822 ec_version != TPACPI_MATCH_ANY)) {
1824 * The changelogs would let us track down the exact
1825 * reason, but it is just too much of a pain to track
1826 * it. We only list BIOSes that are either really
1827 * broken, or really stable to begin with, so it is
1828 * best if the user upgrades the firmware anyway.
1830 printk(TPACPI_WARN
1831 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1832 printk(TPACPI_WARN
1833 "WARNING: This firmware may be missing critical bug "
1834 "fixes and/or important features\n");
1838 static bool __init tpacpi_is_fw_known(void)
1840 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1841 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1844 /****************************************************************************
1845 ****************************************************************************
1847 * Subdrivers
1849 ****************************************************************************
1850 ****************************************************************************/
1852 /*************************************************************************
1853 * thinkpad-acpi metadata subdriver
1856 static int thinkpad_acpi_driver_read(struct seq_file *m)
1858 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1859 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1860 return 0;
1863 static struct ibm_struct thinkpad_acpi_driver_data = {
1864 .name = "driver",
1865 .read = thinkpad_acpi_driver_read,
1868 /*************************************************************************
1869 * Hotkey subdriver
1873 * ThinkPad firmware event model
1875 * The ThinkPad firmware has two main event interfaces: normal ACPI
1876 * notifications (which follow the ACPI standard), and a private event
1877 * interface.
1879 * The private event interface also issues events for the hotkeys. As
1880 * the driver gained features, the event handling code ended up being
1881 * built around the hotkey subdriver. This will need to be refactored
1882 * to a more formal event API eventually.
1884 * Some "hotkeys" are actually supposed to be used as event reports,
1885 * such as "brightness has changed", "volume has changed", depending on
1886 * the ThinkPad model and how the firmware is operating.
1888 * Unlike other classes, hotkey-class events have mask/unmask control on
1889 * non-ancient firmware. However, how it behaves changes a lot with the
1890 * firmware model and version.
1893 enum { /* hot key scan codes (derived from ACPI DSDT) */
1894 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1895 TP_ACPI_HOTKEYSCAN_FNF2,
1896 TP_ACPI_HOTKEYSCAN_FNF3,
1897 TP_ACPI_HOTKEYSCAN_FNF4,
1898 TP_ACPI_HOTKEYSCAN_FNF5,
1899 TP_ACPI_HOTKEYSCAN_FNF6,
1900 TP_ACPI_HOTKEYSCAN_FNF7,
1901 TP_ACPI_HOTKEYSCAN_FNF8,
1902 TP_ACPI_HOTKEYSCAN_FNF9,
1903 TP_ACPI_HOTKEYSCAN_FNF10,
1904 TP_ACPI_HOTKEYSCAN_FNF11,
1905 TP_ACPI_HOTKEYSCAN_FNF12,
1906 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1907 TP_ACPI_HOTKEYSCAN_FNINSERT,
1908 TP_ACPI_HOTKEYSCAN_FNDELETE,
1909 TP_ACPI_HOTKEYSCAN_FNHOME,
1910 TP_ACPI_HOTKEYSCAN_FNEND,
1911 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1912 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1913 TP_ACPI_HOTKEYSCAN_FNSPACE,
1914 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1915 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1916 TP_ACPI_HOTKEYSCAN_MUTE,
1917 TP_ACPI_HOTKEYSCAN_THINKPAD,
1920 enum { /* Keys/events available through NVRAM polling */
1921 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1922 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1925 enum { /* Positions of some of the keys in hotkey masks */
1926 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1927 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1928 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1929 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1930 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1931 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1932 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1933 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1934 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1935 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1936 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1939 enum { /* NVRAM to ACPI HKEY group map */
1940 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1941 TP_ACPI_HKEY_ZOOM_MASK |
1942 TP_ACPI_HKEY_DISPSWTCH_MASK |
1943 TP_ACPI_HKEY_HIBERNATE_MASK,
1944 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1945 TP_ACPI_HKEY_BRGHTDWN_MASK,
1946 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1947 TP_ACPI_HKEY_VOLDWN_MASK |
1948 TP_ACPI_HKEY_MUTE_MASK,
1951 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1952 struct tp_nvram_state {
1953 u16 thinkpad_toggle:1;
1954 u16 zoom_toggle:1;
1955 u16 display_toggle:1;
1956 u16 thinklight_toggle:1;
1957 u16 hibernate_toggle:1;
1958 u16 displayexp_toggle:1;
1959 u16 display_state:1;
1960 u16 brightness_toggle:1;
1961 u16 volume_toggle:1;
1962 u16 mute:1;
1964 u8 brightness_level;
1965 u8 volume_level;
1968 /* kthread for the hotkey poller */
1969 static struct task_struct *tpacpi_hotkey_task;
1971 /* Acquired while the poller kthread is running, use to sync start/stop */
1972 static struct mutex hotkey_thread_mutex;
1975 * Acquire mutex to write poller control variables as an
1976 * atomic block.
1978 * Increment hotkey_config_change when changing them if you
1979 * want the kthread to forget old state.
1981 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1983 static struct mutex hotkey_thread_data_mutex;
1984 static unsigned int hotkey_config_change;
1987 * hotkey poller control variables
1989 * Must be atomic or readers will also need to acquire mutex
1991 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1992 * should be used only when the changes need to be taken as
1993 * a block, OR when one needs to force the kthread to forget
1994 * old state.
1996 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1997 static unsigned int hotkey_poll_freq = 10; /* Hz */
1999 #define HOTKEY_CONFIG_CRITICAL_START \
2000 do { \
2001 mutex_lock(&hotkey_thread_data_mutex); \
2002 hotkey_config_change++; \
2003 } while (0);
2004 #define HOTKEY_CONFIG_CRITICAL_END \
2005 mutex_unlock(&hotkey_thread_data_mutex);
2007 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2009 #define hotkey_source_mask 0U
2010 #define HOTKEY_CONFIG_CRITICAL_START
2011 #define HOTKEY_CONFIG_CRITICAL_END
2013 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2015 static struct mutex hotkey_mutex;
2017 static enum { /* Reasons for waking up */
2018 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2019 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2020 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2021 } hotkey_wakeup_reason;
2023 static int hotkey_autosleep_ack;
2025 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2026 static u32 hotkey_all_mask; /* all events supported in fw */
2027 static u32 hotkey_reserved_mask; /* events better left disabled */
2028 static u32 hotkey_driver_mask; /* events needed by the driver */
2029 static u32 hotkey_user_mask; /* events visible to userspace */
2030 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2032 static unsigned int hotkey_report_mode;
2034 static u16 *hotkey_keycode_map;
2036 static struct attribute_set *hotkey_dev_attributes;
2038 static void tpacpi_driver_event(const unsigned int hkey_event);
2039 static void hotkey_driver_event(const unsigned int scancode);
2040 static void hotkey_poll_setup(const bool may_warn);
2042 /* HKEY.MHKG() return bits */
2043 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2045 static int hotkey_get_wlsw(void)
2047 int status;
2049 if (!tp_features.hotkey_wlsw)
2050 return -ENODEV;
2052 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2053 if (dbg_wlswemul)
2054 return (tpacpi_wlsw_emulstate) ?
2055 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2056 #endif
2058 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2059 return -EIO;
2061 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2064 static int hotkey_get_tablet_mode(int *status)
2066 int s;
2068 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2069 return -EIO;
2071 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2072 return 0;
2076 * Reads current event mask from firmware, and updates
2077 * hotkey_acpi_mask accordingly. Also resets any bits
2078 * from hotkey_user_mask that are unavailable to be
2079 * delivered (shadow requirement of the userspace ABI).
2081 * Call with hotkey_mutex held
2083 static int hotkey_mask_get(void)
2085 if (tp_features.hotkey_mask) {
2086 u32 m = 0;
2088 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2089 return -EIO;
2091 hotkey_acpi_mask = m;
2092 } else {
2093 /* no mask support doesn't mean no event support... */
2094 hotkey_acpi_mask = hotkey_all_mask;
2097 /* sync userspace-visible mask */
2098 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2100 return 0;
2103 void static hotkey_mask_warn_incomplete_mask(void)
2105 /* log only what the user can fix... */
2106 const u32 wantedmask = hotkey_driver_mask &
2107 ~(hotkey_acpi_mask | hotkey_source_mask) &
2108 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2110 if (wantedmask)
2111 printk(TPACPI_NOTICE
2112 "required events 0x%08x not enabled!\n",
2113 wantedmask);
2117 * Set the firmware mask when supported
2119 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2121 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2123 * Call with hotkey_mutex held
2125 static int hotkey_mask_set(u32 mask)
2127 int i;
2128 int rc = 0;
2130 const u32 fwmask = mask & ~hotkey_source_mask;
2132 if (tp_features.hotkey_mask) {
2133 for (i = 0; i < 32; i++) {
2134 if (!acpi_evalf(hkey_handle,
2135 NULL, "MHKM", "vdd", i + 1,
2136 !!(mask & (1 << i)))) {
2137 rc = -EIO;
2138 break;
2144 * We *must* make an inconditional call to hotkey_mask_get to
2145 * refresh hotkey_acpi_mask and update hotkey_user_mask
2147 * Take the opportunity to also log when we cannot _enable_
2148 * a given event.
2150 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2151 printk(TPACPI_NOTICE
2152 "asked for hotkey mask 0x%08x, but "
2153 "firmware forced it to 0x%08x\n",
2154 fwmask, hotkey_acpi_mask);
2157 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2158 hotkey_mask_warn_incomplete_mask();
2160 return rc;
2164 * Sets hotkey_user_mask and tries to set the firmware mask
2166 * Call with hotkey_mutex held
2168 static int hotkey_user_mask_set(const u32 mask)
2170 int rc;
2172 /* Give people a chance to notice they are doing something that
2173 * is bound to go boom on their users sooner or later */
2174 if (!tp_warned.hotkey_mask_ff &&
2175 (mask == 0xffff || mask == 0xffffff ||
2176 mask == 0xffffffff)) {
2177 tp_warned.hotkey_mask_ff = 1;
2178 printk(TPACPI_NOTICE
2179 "setting the hotkey mask to 0x%08x is likely "
2180 "not the best way to go about it\n", mask);
2181 printk(TPACPI_NOTICE
2182 "please consider using the driver defaults, "
2183 "and refer to up-to-date thinkpad-acpi "
2184 "documentation\n");
2187 /* Try to enable what the user asked for, plus whatever we need.
2188 * this syncs everything but won't enable bits in hotkey_user_mask */
2189 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2191 /* Enable the available bits in hotkey_user_mask */
2192 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2194 return rc;
2198 * Sets the driver hotkey mask.
2200 * Can be called even if the hotkey subdriver is inactive
2202 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2204 int rc;
2206 /* Do the right thing if hotkey_init has not been called yet */
2207 if (!tp_features.hotkey) {
2208 hotkey_driver_mask = mask;
2209 return 0;
2212 mutex_lock(&hotkey_mutex);
2214 HOTKEY_CONFIG_CRITICAL_START
2215 hotkey_driver_mask = mask;
2216 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2217 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2218 #endif
2219 HOTKEY_CONFIG_CRITICAL_END
2221 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2222 ~hotkey_source_mask);
2223 hotkey_poll_setup(true);
2225 mutex_unlock(&hotkey_mutex);
2227 return rc;
2230 static int hotkey_status_get(int *status)
2232 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2233 return -EIO;
2235 return 0;
2238 static int hotkey_status_set(bool enable)
2240 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2241 return -EIO;
2243 return 0;
2246 static void tpacpi_input_send_tabletsw(void)
2248 int state;
2250 if (tp_features.hotkey_tablet &&
2251 !hotkey_get_tablet_mode(&state)) {
2252 mutex_lock(&tpacpi_inputdev_send_mutex);
2254 input_report_switch(tpacpi_inputdev,
2255 SW_TABLET_MODE, !!state);
2256 input_sync(tpacpi_inputdev);
2258 mutex_unlock(&tpacpi_inputdev_send_mutex);
2262 /* Do NOT call without validating scancode first */
2263 static void tpacpi_input_send_key(const unsigned int scancode)
2265 const unsigned int keycode = hotkey_keycode_map[scancode];
2267 if (keycode != KEY_RESERVED) {
2268 mutex_lock(&tpacpi_inputdev_send_mutex);
2270 input_report_key(tpacpi_inputdev, keycode, 1);
2271 if (keycode == KEY_UNKNOWN)
2272 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2273 scancode);
2274 input_sync(tpacpi_inputdev);
2276 input_report_key(tpacpi_inputdev, keycode, 0);
2277 if (keycode == KEY_UNKNOWN)
2278 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2279 scancode);
2280 input_sync(tpacpi_inputdev);
2282 mutex_unlock(&tpacpi_inputdev_send_mutex);
2286 /* Do NOT call without validating scancode first */
2287 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2289 hotkey_driver_event(scancode);
2290 if (hotkey_user_mask & (1 << scancode))
2291 tpacpi_input_send_key(scancode);
2294 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2295 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2297 /* Do NOT call without validating scancode first */
2298 static void tpacpi_hotkey_send_key(unsigned int scancode)
2300 tpacpi_input_send_key_masked(scancode);
2301 if (hotkey_report_mode < 2) {
2302 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2303 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2307 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2309 u8 d;
2311 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2312 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2313 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2314 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2315 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2316 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2318 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2319 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2320 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2322 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2323 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2324 n->displayexp_toggle =
2325 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2327 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2328 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2329 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2330 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2331 n->brightness_toggle =
2332 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2334 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2335 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2336 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2337 >> TP_NVRAM_POS_LEVEL_VOLUME;
2338 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2339 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2343 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2344 struct tp_nvram_state *newn,
2345 const u32 event_mask)
2348 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2349 do { \
2350 if ((event_mask & (1 << __scancode)) && \
2351 oldn->__member != newn->__member) \
2352 tpacpi_hotkey_send_key(__scancode); \
2353 } while (0)
2355 #define TPACPI_MAY_SEND_KEY(__scancode) \
2356 do { \
2357 if (event_mask & (1 << __scancode)) \
2358 tpacpi_hotkey_send_key(__scancode); \
2359 } while (0)
2361 void issue_volchange(const unsigned int oldvol,
2362 const unsigned int newvol)
2364 unsigned int i = oldvol;
2366 while (i > newvol) {
2367 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2368 i--;
2370 while (i < newvol) {
2371 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2372 i++;
2376 void issue_brightnesschange(const unsigned int oldbrt,
2377 const unsigned int newbrt)
2379 unsigned int i = oldbrt;
2381 while (i > newbrt) {
2382 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2383 i--;
2385 while (i < newbrt) {
2386 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2387 i++;
2391 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2392 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2393 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2394 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2396 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2398 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2401 * Handle volume
2403 * This code is supposed to duplicate the IBM firmware behaviour:
2404 * - Pressing MUTE issues mute hotkey message, even when already mute
2405 * - Pressing Volume up/down issues volume up/down hotkey messages,
2406 * even when already at maximum or minumum volume
2407 * - The act of unmuting issues volume up/down notification,
2408 * depending which key was used to unmute
2410 * We are constrained to what the NVRAM can tell us, which is not much
2411 * and certainly not enough if more than one volume hotkey was pressed
2412 * since the last poll cycle.
2414 * Just to make our life interesting, some newer Lenovo ThinkPads have
2415 * bugs in the BIOS and may fail to update volume_toggle properly.
2417 if (newn->mute) {
2418 /* muted */
2419 if (!oldn->mute ||
2420 oldn->volume_toggle != newn->volume_toggle ||
2421 oldn->volume_level != newn->volume_level) {
2422 /* recently muted, or repeated mute keypress, or
2423 * multiple presses ending in mute */
2424 issue_volchange(oldn->volume_level, newn->volume_level);
2425 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2427 } else {
2428 /* unmute */
2429 if (oldn->mute) {
2430 /* recently unmuted, issue 'unmute' keypress */
2431 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2433 if (oldn->volume_level != newn->volume_level) {
2434 issue_volchange(oldn->volume_level, newn->volume_level);
2435 } else if (oldn->volume_toggle != newn->volume_toggle) {
2436 /* repeated vol up/down keypress at end of scale ? */
2437 if (newn->volume_level == 0)
2438 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2439 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2440 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2444 /* handle brightness */
2445 if (oldn->brightness_level != newn->brightness_level) {
2446 issue_brightnesschange(oldn->brightness_level,
2447 newn->brightness_level);
2448 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2449 /* repeated key presses that didn't change state */
2450 if (newn->brightness_level == 0)
2451 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2452 else if (newn->brightness_level >= bright_maxlvl
2453 && !tp_features.bright_unkfw)
2454 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2457 #undef TPACPI_COMPARE_KEY
2458 #undef TPACPI_MAY_SEND_KEY
2462 * Polling driver
2464 * We track all events in hotkey_source_mask all the time, since
2465 * most of them are edge-based. We only issue those requested by
2466 * hotkey_user_mask or hotkey_driver_mask, though.
2468 static int hotkey_kthread(void *data)
2470 struct tp_nvram_state s[2];
2471 u32 poll_mask, event_mask;
2472 unsigned int si, so;
2473 unsigned long t;
2474 unsigned int change_detector, must_reset;
2475 unsigned int poll_freq;
2477 mutex_lock(&hotkey_thread_mutex);
2479 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2480 goto exit;
2482 set_freezable();
2484 so = 0;
2485 si = 1;
2486 t = 0;
2488 /* Initial state for compares */
2489 mutex_lock(&hotkey_thread_data_mutex);
2490 change_detector = hotkey_config_change;
2491 poll_mask = hotkey_source_mask;
2492 event_mask = hotkey_source_mask &
2493 (hotkey_driver_mask | hotkey_user_mask);
2494 poll_freq = hotkey_poll_freq;
2495 mutex_unlock(&hotkey_thread_data_mutex);
2496 hotkey_read_nvram(&s[so], poll_mask);
2498 while (!kthread_should_stop()) {
2499 if (t == 0) {
2500 if (likely(poll_freq))
2501 t = 1000/poll_freq;
2502 else
2503 t = 100; /* should never happen... */
2505 t = msleep_interruptible(t);
2506 if (unlikely(kthread_should_stop()))
2507 break;
2508 must_reset = try_to_freeze();
2509 if (t > 0 && !must_reset)
2510 continue;
2512 mutex_lock(&hotkey_thread_data_mutex);
2513 if (must_reset || hotkey_config_change != change_detector) {
2514 /* forget old state on thaw or config change */
2515 si = so;
2516 t = 0;
2517 change_detector = hotkey_config_change;
2519 poll_mask = hotkey_source_mask;
2520 event_mask = hotkey_source_mask &
2521 (hotkey_driver_mask | hotkey_user_mask);
2522 poll_freq = hotkey_poll_freq;
2523 mutex_unlock(&hotkey_thread_data_mutex);
2525 if (likely(poll_mask)) {
2526 hotkey_read_nvram(&s[si], poll_mask);
2527 if (likely(si != so)) {
2528 hotkey_compare_and_issue_event(&s[so], &s[si],
2529 event_mask);
2533 so = si;
2534 si ^= 1;
2537 exit:
2538 mutex_unlock(&hotkey_thread_mutex);
2539 return 0;
2542 /* call with hotkey_mutex held */
2543 static void hotkey_poll_stop_sync(void)
2545 if (tpacpi_hotkey_task) {
2546 if (frozen(tpacpi_hotkey_task) ||
2547 freezing(tpacpi_hotkey_task))
2548 thaw_process(tpacpi_hotkey_task);
2550 kthread_stop(tpacpi_hotkey_task);
2551 tpacpi_hotkey_task = NULL;
2552 mutex_lock(&hotkey_thread_mutex);
2553 /* at this point, the thread did exit */
2554 mutex_unlock(&hotkey_thread_mutex);
2558 /* call with hotkey_mutex held */
2559 static void hotkey_poll_setup(const bool may_warn)
2561 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2562 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2564 if (hotkey_poll_freq > 0 &&
2565 (poll_driver_mask ||
2566 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2567 if (!tpacpi_hotkey_task) {
2568 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2569 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2570 if (IS_ERR(tpacpi_hotkey_task)) {
2571 tpacpi_hotkey_task = NULL;
2572 printk(TPACPI_ERR
2573 "could not create kernel thread "
2574 "for hotkey polling\n");
2577 } else {
2578 hotkey_poll_stop_sync();
2579 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2580 hotkey_poll_freq == 0) {
2581 printk(TPACPI_NOTICE
2582 "hot keys 0x%08x and/or events 0x%08x "
2583 "require polling, which is currently "
2584 "disabled\n",
2585 poll_user_mask, poll_driver_mask);
2590 static void hotkey_poll_setup_safe(const bool may_warn)
2592 mutex_lock(&hotkey_mutex);
2593 hotkey_poll_setup(may_warn);
2594 mutex_unlock(&hotkey_mutex);
2597 /* call with hotkey_mutex held */
2598 static void hotkey_poll_set_freq(unsigned int freq)
2600 if (!freq)
2601 hotkey_poll_stop_sync();
2603 hotkey_poll_freq = freq;
2606 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2608 static void hotkey_poll_setup(const bool __unused)
2612 static void hotkey_poll_setup_safe(const bool __unused)
2616 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2618 static int hotkey_inputdev_open(struct input_dev *dev)
2620 switch (tpacpi_lifecycle) {
2621 case TPACPI_LIFE_INIT:
2622 case TPACPI_LIFE_RUNNING:
2623 hotkey_poll_setup_safe(false);
2624 return 0;
2625 case TPACPI_LIFE_EXITING:
2626 return -EBUSY;
2629 /* Should only happen if tpacpi_lifecycle is corrupt */
2630 BUG();
2631 return -EBUSY;
2634 static void hotkey_inputdev_close(struct input_dev *dev)
2636 /* disable hotkey polling when possible */
2637 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2638 !(hotkey_source_mask & hotkey_driver_mask))
2639 hotkey_poll_setup_safe(false);
2642 /* sysfs hotkey enable ------------------------------------------------- */
2643 static ssize_t hotkey_enable_show(struct device *dev,
2644 struct device_attribute *attr,
2645 char *buf)
2647 int res, status;
2649 printk_deprecated_attribute("hotkey_enable",
2650 "Hotkey reporting is always enabled");
2652 res = hotkey_status_get(&status);
2653 if (res)
2654 return res;
2656 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2659 static ssize_t hotkey_enable_store(struct device *dev,
2660 struct device_attribute *attr,
2661 const char *buf, size_t count)
2663 unsigned long t;
2665 printk_deprecated_attribute("hotkey_enable",
2666 "Hotkeys can be disabled through hotkey_mask");
2668 if (parse_strtoul(buf, 1, &t))
2669 return -EINVAL;
2671 if (t == 0)
2672 return -EPERM;
2674 return count;
2677 static struct device_attribute dev_attr_hotkey_enable =
2678 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2679 hotkey_enable_show, hotkey_enable_store);
2681 /* sysfs hotkey mask --------------------------------------------------- */
2682 static ssize_t hotkey_mask_show(struct device *dev,
2683 struct device_attribute *attr,
2684 char *buf)
2686 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2689 static ssize_t hotkey_mask_store(struct device *dev,
2690 struct device_attribute *attr,
2691 const char *buf, size_t count)
2693 unsigned long t;
2694 int res;
2696 if (parse_strtoul(buf, 0xffffffffUL, &t))
2697 return -EINVAL;
2699 if (mutex_lock_killable(&hotkey_mutex))
2700 return -ERESTARTSYS;
2702 res = hotkey_user_mask_set(t);
2704 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2705 hotkey_poll_setup(true);
2706 #endif
2708 mutex_unlock(&hotkey_mutex);
2710 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2712 return (res) ? res : count;
2715 static struct device_attribute dev_attr_hotkey_mask =
2716 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2717 hotkey_mask_show, hotkey_mask_store);
2719 /* sysfs hotkey bios_enabled ------------------------------------------- */
2720 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2721 struct device_attribute *attr,
2722 char *buf)
2724 return sprintf(buf, "0\n");
2727 static struct device_attribute dev_attr_hotkey_bios_enabled =
2728 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2730 /* sysfs hotkey bios_mask ---------------------------------------------- */
2731 static ssize_t hotkey_bios_mask_show(struct device *dev,
2732 struct device_attribute *attr,
2733 char *buf)
2735 printk_deprecated_attribute("hotkey_bios_mask",
2736 "This attribute is useless.");
2737 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2740 static struct device_attribute dev_attr_hotkey_bios_mask =
2741 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2743 /* sysfs hotkey all_mask ----------------------------------------------- */
2744 static ssize_t hotkey_all_mask_show(struct device *dev,
2745 struct device_attribute *attr,
2746 char *buf)
2748 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2749 hotkey_all_mask | hotkey_source_mask);
2752 static struct device_attribute dev_attr_hotkey_all_mask =
2753 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2755 /* sysfs hotkey recommended_mask --------------------------------------- */
2756 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2757 struct device_attribute *attr,
2758 char *buf)
2760 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2761 (hotkey_all_mask | hotkey_source_mask)
2762 & ~hotkey_reserved_mask);
2765 static struct device_attribute dev_attr_hotkey_recommended_mask =
2766 __ATTR(hotkey_recommended_mask, S_IRUGO,
2767 hotkey_recommended_mask_show, NULL);
2769 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2771 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2772 static ssize_t hotkey_source_mask_show(struct device *dev,
2773 struct device_attribute *attr,
2774 char *buf)
2776 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2779 static ssize_t hotkey_source_mask_store(struct device *dev,
2780 struct device_attribute *attr,
2781 const char *buf, size_t count)
2783 unsigned long t;
2784 u32 r_ev;
2785 int rc;
2787 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2788 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2789 return -EINVAL;
2791 if (mutex_lock_killable(&hotkey_mutex))
2792 return -ERESTARTSYS;
2794 HOTKEY_CONFIG_CRITICAL_START
2795 hotkey_source_mask = t;
2796 HOTKEY_CONFIG_CRITICAL_END
2798 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2799 ~hotkey_source_mask);
2800 hotkey_poll_setup(true);
2802 /* check if events needed by the driver got disabled */
2803 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2804 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2806 mutex_unlock(&hotkey_mutex);
2808 if (rc < 0)
2809 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2810 "firmware event mask!\n");
2812 if (r_ev)
2813 printk(TPACPI_NOTICE "hotkey_source_mask: "
2814 "some important events were disabled: "
2815 "0x%04x\n", r_ev);
2817 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2819 return (rc < 0) ? rc : count;
2822 static struct device_attribute dev_attr_hotkey_source_mask =
2823 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2824 hotkey_source_mask_show, hotkey_source_mask_store);
2826 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2827 static ssize_t hotkey_poll_freq_show(struct device *dev,
2828 struct device_attribute *attr,
2829 char *buf)
2831 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2834 static ssize_t hotkey_poll_freq_store(struct device *dev,
2835 struct device_attribute *attr,
2836 const char *buf, size_t count)
2838 unsigned long t;
2840 if (parse_strtoul(buf, 25, &t))
2841 return -EINVAL;
2843 if (mutex_lock_killable(&hotkey_mutex))
2844 return -ERESTARTSYS;
2846 hotkey_poll_set_freq(t);
2847 hotkey_poll_setup(true);
2849 mutex_unlock(&hotkey_mutex);
2851 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2853 return count;
2856 static struct device_attribute dev_attr_hotkey_poll_freq =
2857 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2858 hotkey_poll_freq_show, hotkey_poll_freq_store);
2860 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2862 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2863 static ssize_t hotkey_radio_sw_show(struct device *dev,
2864 struct device_attribute *attr,
2865 char *buf)
2867 int res;
2868 res = hotkey_get_wlsw();
2869 if (res < 0)
2870 return res;
2872 /* Opportunistic update */
2873 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2875 return snprintf(buf, PAGE_SIZE, "%d\n",
2876 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2879 static struct device_attribute dev_attr_hotkey_radio_sw =
2880 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2882 static void hotkey_radio_sw_notify_change(void)
2884 if (tp_features.hotkey_wlsw)
2885 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2886 "hotkey_radio_sw");
2889 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2890 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2891 struct device_attribute *attr,
2892 char *buf)
2894 int res, s;
2895 res = hotkey_get_tablet_mode(&s);
2896 if (res < 0)
2897 return res;
2899 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2902 static struct device_attribute dev_attr_hotkey_tablet_mode =
2903 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2905 static void hotkey_tablet_mode_notify_change(void)
2907 if (tp_features.hotkey_tablet)
2908 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2909 "hotkey_tablet_mode");
2912 /* sysfs hotkey report_mode -------------------------------------------- */
2913 static ssize_t hotkey_report_mode_show(struct device *dev,
2914 struct device_attribute *attr,
2915 char *buf)
2917 return snprintf(buf, PAGE_SIZE, "%d\n",
2918 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2921 static struct device_attribute dev_attr_hotkey_report_mode =
2922 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2924 /* sysfs wakeup reason (pollable) -------------------------------------- */
2925 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2926 struct device_attribute *attr,
2927 char *buf)
2929 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2932 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2933 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2935 static void hotkey_wakeup_reason_notify_change(void)
2937 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2938 "wakeup_reason");
2941 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2942 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2943 struct device_attribute *attr,
2944 char *buf)
2946 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2949 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2950 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2951 hotkey_wakeup_hotunplug_complete_show, NULL);
2953 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2955 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2956 "wakeup_hotunplug_complete");
2959 /* --------------------------------------------------------------------- */
2961 static struct attribute *hotkey_attributes[] __initdata = {
2962 &dev_attr_hotkey_enable.attr,
2963 &dev_attr_hotkey_bios_enabled.attr,
2964 &dev_attr_hotkey_bios_mask.attr,
2965 &dev_attr_hotkey_report_mode.attr,
2966 &dev_attr_hotkey_wakeup_reason.attr,
2967 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2968 &dev_attr_hotkey_mask.attr,
2969 &dev_attr_hotkey_all_mask.attr,
2970 &dev_attr_hotkey_recommended_mask.attr,
2971 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2972 &dev_attr_hotkey_source_mask.attr,
2973 &dev_attr_hotkey_poll_freq.attr,
2974 #endif
2978 * Sync both the hw and sw blocking state of all switches
2980 static void tpacpi_send_radiosw_update(void)
2982 int wlsw;
2985 * We must sync all rfkill controllers *before* issuing any
2986 * rfkill input events, or we will race the rfkill core input
2987 * handler.
2989 * tpacpi_inputdev_send_mutex works as a syncronization point
2990 * for the above.
2992 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2995 wlsw = hotkey_get_wlsw();
2997 /* Sync hw blocking state first if it is hw-blocked */
2998 if (wlsw == TPACPI_RFK_RADIO_OFF)
2999 tpacpi_rfk_update_hwblock_state(true);
3001 /* Sync sw blocking state */
3002 tpacpi_rfk_update_swstate_all();
3004 /* Sync hw blocking state last if it is hw-unblocked */
3005 if (wlsw == TPACPI_RFK_RADIO_ON)
3006 tpacpi_rfk_update_hwblock_state(false);
3008 /* Issue rfkill input event for WLSW switch */
3009 if (!(wlsw < 0)) {
3010 mutex_lock(&tpacpi_inputdev_send_mutex);
3012 input_report_switch(tpacpi_inputdev,
3013 SW_RFKILL_ALL, (wlsw > 0));
3014 input_sync(tpacpi_inputdev);
3016 mutex_unlock(&tpacpi_inputdev_send_mutex);
3020 * this can be unconditional, as we will poll state again
3021 * if userspace uses the notify to read data
3023 hotkey_radio_sw_notify_change();
3026 static void hotkey_exit(void)
3028 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3029 mutex_lock(&hotkey_mutex);
3030 hotkey_poll_stop_sync();
3031 mutex_unlock(&hotkey_mutex);
3032 #endif
3034 if (hotkey_dev_attributes)
3035 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3037 kfree(hotkey_keycode_map);
3039 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3040 "restoring original HKEY status and mask\n");
3041 /* yes, there is a bitwise or below, we want the
3042 * functions to be called even if one of them fail */
3043 if (((tp_features.hotkey_mask &&
3044 hotkey_mask_set(hotkey_orig_mask)) |
3045 hotkey_status_set(false)) != 0)
3046 printk(TPACPI_ERR
3047 "failed to restore hot key mask "
3048 "to BIOS defaults\n");
3051 static void __init hotkey_unmap(const unsigned int scancode)
3053 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3054 clear_bit(hotkey_keycode_map[scancode],
3055 tpacpi_inputdev->keybit);
3056 hotkey_keycode_map[scancode] = KEY_RESERVED;
3061 * HKEY quirks:
3062 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3065 #define TPACPI_HK_Q_INIMASK 0x0001
3067 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3068 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3069 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3070 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3071 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3072 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3073 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3074 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3075 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3076 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3077 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3078 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3079 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3080 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3081 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3082 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3083 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3084 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3085 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3086 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3089 static int __init hotkey_init(struct ibm_init_struct *iibm)
3091 /* Requirements for changing the default keymaps:
3093 * 1. Many of the keys are mapped to KEY_RESERVED for very
3094 * good reasons. Do not change them unless you have deep
3095 * knowledge on the IBM and Lenovo ThinkPad firmware for
3096 * the various ThinkPad models. The driver behaves
3097 * differently for KEY_RESERVED: such keys have their
3098 * hot key mask *unset* in mask_recommended, and also
3099 * in the initial hot key mask programmed into the
3100 * firmware at driver load time, which means the firm-
3101 * ware may react very differently if you change them to
3102 * something else;
3104 * 2. You must be subscribed to the linux-thinkpad and
3105 * ibm-acpi-devel mailing lists, and you should read the
3106 * list archives since 2007 if you want to change the
3107 * keymaps. This requirement exists so that you will
3108 * know the past history of problems with the thinkpad-
3109 * acpi driver keymaps, and also that you will be
3110 * listening to any bug reports;
3112 * 3. Do not send thinkpad-acpi specific patches directly to
3113 * for merging, *ever*. Send them to the linux-acpi
3114 * mailinglist for comments. Merging is to be done only
3115 * through acpi-test and the ACPI maintainer.
3117 * If the above is too much to ask, don't change the keymap.
3118 * Ask the thinkpad-acpi maintainer to do it, instead.
3120 static u16 ibm_keycode_map[] __initdata = {
3121 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3122 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3123 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3124 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3126 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3127 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3128 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3129 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3131 /* brightness: firmware always reacts to them */
3132 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3133 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3135 /* Thinklight: firmware always react to it */
3136 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3138 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3139 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3141 /* Volume: firmware always react to it and reprograms
3142 * the built-in *extra* mixer. Never map it to control
3143 * another mixer by default. */
3144 KEY_RESERVED, /* 0x14: VOLUME UP */
3145 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3146 KEY_RESERVED, /* 0x16: MUTE */
3148 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3150 /* (assignments unknown, please report if found) */
3151 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3152 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3154 static u16 lenovo_keycode_map[] __initdata = {
3155 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3156 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3157 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3158 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3160 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3161 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3162 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3163 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3165 /* These should be enabled --only-- when ACPI video
3166 * is disabled (i.e. in "vendor" mode), and are handled
3167 * in a special way by the init code */
3168 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3169 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3171 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3173 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3174 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3176 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3177 * react to it and reprograms the built-in *extra* mixer.
3178 * Never map it to control another mixer by default.
3180 * T60?, T61, R60?, R61: firmware and EC tries to send
3181 * these over the regular keyboard, so these are no-ops,
3182 * but there are still weird bugs re. MUTE, so do not
3183 * change unless you get test reports from all Lenovo
3184 * models. May cause the BIOS to interfere with the
3185 * HDA mixer.
3187 KEY_RESERVED, /* 0x14: VOLUME UP */
3188 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3189 KEY_RESERVED, /* 0x16: MUTE */
3191 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3193 /* (assignments unknown, please report if found) */
3194 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3195 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3198 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3199 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3200 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3202 int res, i;
3203 int status;
3204 int hkeyv;
3205 bool radiosw_state = false;
3206 bool tabletsw_state = false;
3208 unsigned long quirks;
3210 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3211 "initializing hotkey subdriver\n");
3213 BUG_ON(!tpacpi_inputdev);
3214 BUG_ON(tpacpi_inputdev->open != NULL ||
3215 tpacpi_inputdev->close != NULL);
3217 TPACPI_ACPIHANDLE_INIT(hkey);
3218 mutex_init(&hotkey_mutex);
3220 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3221 mutex_init(&hotkey_thread_mutex);
3222 mutex_init(&hotkey_thread_data_mutex);
3223 #endif
3225 /* hotkey not supported on 570 */
3226 tp_features.hotkey = hkey_handle != NULL;
3228 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3229 "hotkeys are %s\n",
3230 str_supported(tp_features.hotkey));
3232 if (!tp_features.hotkey)
3233 return 1;
3235 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3236 ARRAY_SIZE(tpacpi_hotkey_qtable));
3238 tpacpi_disable_brightness_delay();
3240 /* MUST have enough space for all attributes to be added to
3241 * hotkey_dev_attributes */
3242 hotkey_dev_attributes = create_attr_set(
3243 ARRAY_SIZE(hotkey_attributes) + 2,
3244 NULL);
3245 if (!hotkey_dev_attributes)
3246 return -ENOMEM;
3247 res = add_many_to_attr_set(hotkey_dev_attributes,
3248 hotkey_attributes,
3249 ARRAY_SIZE(hotkey_attributes));
3250 if (res)
3251 goto err_exit;
3253 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3254 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3255 for HKEY interface version 0x100 */
3256 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3257 if ((hkeyv >> 8) != 1) {
3258 printk(TPACPI_ERR "unknown version of the "
3259 "HKEY interface: 0x%x\n", hkeyv);
3260 printk(TPACPI_ERR "please report this to %s\n",
3261 TPACPI_MAIL);
3262 } else {
3264 * MHKV 0x100 in A31, R40, R40e,
3265 * T4x, X31, and later
3267 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3268 "firmware HKEY interface version: 0x%x\n",
3269 hkeyv);
3271 /* Paranoia check AND init hotkey_all_mask */
3272 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3273 "MHKA", "qd")) {
3274 printk(TPACPI_ERR
3275 "missing MHKA handler, "
3276 "please report this to %s\n",
3277 TPACPI_MAIL);
3278 /* Fallback: pre-init for FN+F3,F4,F12 */
3279 hotkey_all_mask = 0x080cU;
3280 } else {
3281 tp_features.hotkey_mask = 1;
3286 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3287 "hotkey masks are %s\n",
3288 str_supported(tp_features.hotkey_mask));
3290 /* Init hotkey_all_mask if not initialized yet */
3291 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3292 (quirks & TPACPI_HK_Q_INIMASK))
3293 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3295 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3296 if (tp_features.hotkey_mask) {
3297 /* hotkey_source_mask *must* be zero for
3298 * the first hotkey_mask_get to return hotkey_orig_mask */
3299 res = hotkey_mask_get();
3300 if (res)
3301 goto err_exit;
3303 hotkey_orig_mask = hotkey_acpi_mask;
3304 } else {
3305 hotkey_orig_mask = hotkey_all_mask;
3306 hotkey_acpi_mask = hotkey_all_mask;
3309 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3310 if (dbg_wlswemul) {
3311 tp_features.hotkey_wlsw = 1;
3312 radiosw_state = !!tpacpi_wlsw_emulstate;
3313 printk(TPACPI_INFO
3314 "radio switch emulation enabled\n");
3315 } else
3316 #endif
3317 /* Not all thinkpads have a hardware radio switch */
3318 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3319 tp_features.hotkey_wlsw = 1;
3320 radiosw_state = !!status;
3321 printk(TPACPI_INFO
3322 "radio switch found; radios are %s\n",
3323 enabled(status, 0));
3325 if (tp_features.hotkey_wlsw)
3326 res = add_to_attr_set(hotkey_dev_attributes,
3327 &dev_attr_hotkey_radio_sw.attr);
3329 /* For X41t, X60t, X61t Tablets... */
3330 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3331 tp_features.hotkey_tablet = 1;
3332 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3333 printk(TPACPI_INFO
3334 "possible tablet mode switch found; "
3335 "ThinkPad in %s mode\n",
3336 (tabletsw_state) ? "tablet" : "laptop");
3337 res = add_to_attr_set(hotkey_dev_attributes,
3338 &dev_attr_hotkey_tablet_mode.attr);
3341 if (!res)
3342 res = register_attr_set_with_sysfs(
3343 hotkey_dev_attributes,
3344 &tpacpi_pdev->dev.kobj);
3345 if (res)
3346 goto err_exit;
3348 /* Set up key map */
3350 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3351 GFP_KERNEL);
3352 if (!hotkey_keycode_map) {
3353 printk(TPACPI_ERR
3354 "failed to allocate memory for key map\n");
3355 res = -ENOMEM;
3356 goto err_exit;
3359 if (tpacpi_is_lenovo()) {
3360 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3361 "using Lenovo default hot key map\n");
3362 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3363 TPACPI_HOTKEY_MAP_SIZE);
3364 } else {
3365 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3366 "using IBM default hot key map\n");
3367 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3368 TPACPI_HOTKEY_MAP_SIZE);
3371 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3372 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3373 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3374 tpacpi_inputdev->keycode = hotkey_keycode_map;
3375 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3376 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3377 input_set_capability(tpacpi_inputdev, EV_KEY,
3378 hotkey_keycode_map[i]);
3379 } else {
3380 if (i < sizeof(hotkey_reserved_mask)*8)
3381 hotkey_reserved_mask |= 1 << i;
3385 if (tp_features.hotkey_wlsw) {
3386 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3387 input_report_switch(tpacpi_inputdev,
3388 SW_RFKILL_ALL, radiosw_state);
3390 if (tp_features.hotkey_tablet) {
3391 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3392 input_report_switch(tpacpi_inputdev,
3393 SW_TABLET_MODE, tabletsw_state);
3396 /* Do not issue duplicate brightness change events to
3397 * userspace. tpacpi_detect_brightness_capabilities() must have
3398 * been called before this point */
3399 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3400 printk(TPACPI_INFO
3401 "This ThinkPad has standard ACPI backlight "
3402 "brightness control, supported by the ACPI "
3403 "video driver\n");
3404 printk(TPACPI_NOTICE
3405 "Disabling thinkpad-acpi brightness events "
3406 "by default...\n");
3408 /* Disable brightness up/down on Lenovo thinkpads when
3409 * ACPI is handling them, otherwise it is plain impossible
3410 * for userspace to do something even remotely sane */
3411 hotkey_reserved_mask |=
3412 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3413 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3414 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3415 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3418 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3419 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3420 & ~hotkey_all_mask
3421 & ~hotkey_reserved_mask;
3423 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3424 "hotkey source mask 0x%08x, polling freq %u\n",
3425 hotkey_source_mask, hotkey_poll_freq);
3426 #endif
3428 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3429 "enabling firmware HKEY event interface...\n");
3430 res = hotkey_status_set(true);
3431 if (res) {
3432 hotkey_exit();
3433 return res;
3435 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3436 | hotkey_driver_mask)
3437 & ~hotkey_source_mask);
3438 if (res < 0 && res != -ENXIO) {
3439 hotkey_exit();
3440 return res;
3442 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3443 & ~hotkey_reserved_mask;
3444 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3445 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3446 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3448 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3449 "legacy ibm/hotkey event reporting over procfs %s\n",
3450 (hotkey_report_mode < 2) ?
3451 "enabled" : "disabled");
3453 tpacpi_inputdev->open = &hotkey_inputdev_open;
3454 tpacpi_inputdev->close = &hotkey_inputdev_close;
3456 hotkey_poll_setup_safe(true);
3458 return 0;
3460 err_exit:
3461 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3462 hotkey_dev_attributes = NULL;
3464 return (res < 0)? res : 1;
3467 static bool hotkey_notify_hotkey(const u32 hkey,
3468 bool *send_acpi_ev,
3469 bool *ignore_acpi_ev)
3471 /* 0x1000-0x1FFF: key presses */
3472 unsigned int scancode = hkey & 0xfff;
3473 *send_acpi_ev = true;
3474 *ignore_acpi_ev = false;
3476 if (scancode > 0 && scancode < 0x21) {
3477 scancode--;
3478 if (!(hotkey_source_mask & (1 << scancode))) {
3479 tpacpi_input_send_key_masked(scancode);
3480 *send_acpi_ev = false;
3481 } else {
3482 *ignore_acpi_ev = true;
3484 return true;
3486 return false;
3489 static bool hotkey_notify_wakeup(const u32 hkey,
3490 bool *send_acpi_ev,
3491 bool *ignore_acpi_ev)
3493 /* 0x2000-0x2FFF: Wakeup reason */
3494 *send_acpi_ev = true;
3495 *ignore_acpi_ev = false;
3497 switch (hkey) {
3498 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3499 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3500 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3501 *ignore_acpi_ev = true;
3502 break;
3504 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3505 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3506 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3507 *ignore_acpi_ev = true;
3508 break;
3510 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3511 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3512 printk(TPACPI_ALERT
3513 "EMERGENCY WAKEUP: battery almost empty\n");
3514 /* how to auto-heal: */
3515 /* 2313: woke up from S3, go to S4/S5 */
3516 /* 2413: woke up from S4, go to S5 */
3517 break;
3519 default:
3520 return false;
3523 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3524 printk(TPACPI_INFO
3525 "woke up due to a hot-unplug "
3526 "request...\n");
3527 hotkey_wakeup_reason_notify_change();
3529 return true;
3532 static bool hotkey_notify_usrevent(const u32 hkey,
3533 bool *send_acpi_ev,
3534 bool *ignore_acpi_ev)
3536 /* 0x5000-0x5FFF: human interface helpers */
3537 *send_acpi_ev = true;
3538 *ignore_acpi_ev = false;
3540 switch (hkey) {
3541 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3542 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3543 return true;
3545 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3546 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3547 tpacpi_input_send_tabletsw();
3548 hotkey_tablet_mode_notify_change();
3549 *send_acpi_ev = false;
3550 return true;
3552 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3553 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3554 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3555 /* do not propagate these events */
3556 *ignore_acpi_ev = true;
3557 return true;
3559 default:
3560 return false;
3564 static void thermal_dump_all_sensors(void);
3566 static bool hotkey_notify_thermal(const u32 hkey,
3567 bool *send_acpi_ev,
3568 bool *ignore_acpi_ev)
3570 bool known = true;
3572 /* 0x6000-0x6FFF: thermal alarms */
3573 *send_acpi_ev = true;
3574 *ignore_acpi_ev = false;
3576 switch (hkey) {
3577 case TP_HKEY_EV_THM_TABLE_CHANGED:
3578 printk(TPACPI_INFO
3579 "EC reports that Thermal Table has changed\n");
3580 /* recommended action: do nothing, we don't have
3581 * Lenovo ATM information */
3582 return true;
3583 case TP_HKEY_EV_ALARM_BAT_HOT:
3584 printk(TPACPI_CRIT
3585 "THERMAL ALARM: battery is too hot!\n");
3586 /* recommended action: warn user through gui */
3587 break;
3588 case TP_HKEY_EV_ALARM_BAT_XHOT:
3589 printk(TPACPI_ALERT
3590 "THERMAL EMERGENCY: battery is extremely hot!\n");
3591 /* recommended action: immediate sleep/hibernate */
3592 break;
3593 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3594 printk(TPACPI_CRIT
3595 "THERMAL ALARM: "
3596 "a sensor reports something is too hot!\n");
3597 /* recommended action: warn user through gui, that */
3598 /* some internal component is too hot */
3599 break;
3600 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3601 printk(TPACPI_ALERT
3602 "THERMAL EMERGENCY: "
3603 "a sensor reports something is extremely hot!\n");
3604 /* recommended action: immediate sleep/hibernate */
3605 break;
3606 default:
3607 printk(TPACPI_ALERT
3608 "THERMAL ALERT: unknown thermal alarm received\n");
3609 known = false;
3612 thermal_dump_all_sensors();
3614 return known;
3617 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3619 u32 hkey;
3620 bool send_acpi_ev;
3621 bool ignore_acpi_ev;
3622 bool known_ev;
3624 if (event != 0x80) {
3625 printk(TPACPI_ERR
3626 "unknown HKEY notification event %d\n", event);
3627 /* forward it to userspace, maybe it knows how to handle it */
3628 acpi_bus_generate_netlink_event(
3629 ibm->acpi->device->pnp.device_class,
3630 dev_name(&ibm->acpi->device->dev),
3631 event, 0);
3632 return;
3635 while (1) {
3636 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3637 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3638 return;
3641 if (hkey == 0) {
3642 /* queue empty */
3643 return;
3646 send_acpi_ev = true;
3647 ignore_acpi_ev = false;
3649 switch (hkey >> 12) {
3650 case 1:
3651 /* 0x1000-0x1FFF: key presses */
3652 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3653 &ignore_acpi_ev);
3654 break;
3655 case 2:
3656 /* 0x2000-0x2FFF: Wakeup reason */
3657 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3658 &ignore_acpi_ev);
3659 break;
3660 case 3:
3661 /* 0x3000-0x3FFF: bay-related wakeups */
3662 switch (hkey) {
3663 case TP_HKEY_EV_BAYEJ_ACK:
3664 hotkey_autosleep_ack = 1;
3665 printk(TPACPI_INFO
3666 "bay ejected\n");
3667 hotkey_wakeup_hotunplug_complete_notify_change();
3668 known_ev = true;
3669 break;
3670 case TP_HKEY_EV_OPTDRV_EJ:
3671 /* FIXME: kick libata if SATA link offline */
3672 known_ev = true;
3673 break;
3674 default:
3675 known_ev = false;
3677 break;
3678 case 4:
3679 /* 0x4000-0x4FFF: dock-related wakeups */
3680 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3681 hotkey_autosleep_ack = 1;
3682 printk(TPACPI_INFO
3683 "undocked\n");
3684 hotkey_wakeup_hotunplug_complete_notify_change();
3685 known_ev = true;
3686 } else {
3687 known_ev = false;
3689 break;
3690 case 5:
3691 /* 0x5000-0x5FFF: human interface helpers */
3692 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3693 &ignore_acpi_ev);
3694 break;
3695 case 6:
3696 /* 0x6000-0x6FFF: thermal alarms */
3697 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3698 &ignore_acpi_ev);
3699 break;
3700 case 7:
3701 /* 0x7000-0x7FFF: misc */
3702 if (tp_features.hotkey_wlsw &&
3703 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3704 tpacpi_send_radiosw_update();
3705 send_acpi_ev = 0;
3706 known_ev = true;
3707 break;
3709 /* fallthrough to default */
3710 default:
3711 known_ev = false;
3713 if (!known_ev) {
3714 printk(TPACPI_NOTICE
3715 "unhandled HKEY event 0x%04x\n", hkey);
3716 printk(TPACPI_NOTICE
3717 "please report the conditions when this "
3718 "event happened to %s\n", TPACPI_MAIL);
3721 /* Legacy events */
3722 if (!ignore_acpi_ev &&
3723 (send_acpi_ev || hotkey_report_mode < 2)) {
3724 acpi_bus_generate_proc_event(ibm->acpi->device,
3725 event, hkey);
3728 /* netlink events */
3729 if (!ignore_acpi_ev && send_acpi_ev) {
3730 acpi_bus_generate_netlink_event(
3731 ibm->acpi->device->pnp.device_class,
3732 dev_name(&ibm->acpi->device->dev),
3733 event, hkey);
3738 static void hotkey_suspend(pm_message_t state)
3740 /* Do these on suspend, we get the events on early resume! */
3741 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3742 hotkey_autosleep_ack = 0;
3745 static void hotkey_resume(void)
3747 tpacpi_disable_brightness_delay();
3749 if (hotkey_status_set(true) < 0 ||
3750 hotkey_mask_set(hotkey_acpi_mask) < 0)
3751 printk(TPACPI_ERR
3752 "error while attempting to reset the event "
3753 "firmware interface\n");
3755 tpacpi_send_radiosw_update();
3756 hotkey_tablet_mode_notify_change();
3757 hotkey_wakeup_reason_notify_change();
3758 hotkey_wakeup_hotunplug_complete_notify_change();
3759 hotkey_poll_setup_safe(false);
3762 /* procfs -------------------------------------------------------------- */
3763 static int hotkey_read(struct seq_file *m)
3765 int res, status;
3767 if (!tp_features.hotkey) {
3768 seq_printf(m, "status:\t\tnot supported\n");
3769 return 0;
3772 if (mutex_lock_killable(&hotkey_mutex))
3773 return -ERESTARTSYS;
3774 res = hotkey_status_get(&status);
3775 if (!res)
3776 res = hotkey_mask_get();
3777 mutex_unlock(&hotkey_mutex);
3778 if (res)
3779 return res;
3781 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3782 if (hotkey_all_mask) {
3783 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3784 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3785 } else {
3786 seq_printf(m, "mask:\t\tnot supported\n");
3787 seq_printf(m, "commands:\tenable, disable, reset\n");
3790 return 0;
3793 static void hotkey_enabledisable_warn(bool enable)
3795 tpacpi_log_usertask("procfs hotkey enable/disable");
3796 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3797 TPACPI_WARN
3798 "hotkey enable/disable functionality has been "
3799 "removed from the driver. Hotkeys are always "
3800 "enabled\n"))
3801 printk(TPACPI_ERR
3802 "Please remove the hotkey=enable module "
3803 "parameter, it is deprecated. Hotkeys are always "
3804 "enabled\n");
3807 static int hotkey_write(char *buf)
3809 int res;
3810 u32 mask;
3811 char *cmd;
3813 if (!tp_features.hotkey)
3814 return -ENODEV;
3816 if (mutex_lock_killable(&hotkey_mutex))
3817 return -ERESTARTSYS;
3819 mask = hotkey_user_mask;
3821 res = 0;
3822 while ((cmd = next_cmd(&buf))) {
3823 if (strlencmp(cmd, "enable") == 0) {
3824 hotkey_enabledisable_warn(1);
3825 } else if (strlencmp(cmd, "disable") == 0) {
3826 hotkey_enabledisable_warn(0);
3827 res = -EPERM;
3828 } else if (strlencmp(cmd, "reset") == 0) {
3829 mask = (hotkey_all_mask | hotkey_source_mask)
3830 & ~hotkey_reserved_mask;
3831 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3832 /* mask set */
3833 } else if (sscanf(cmd, "%x", &mask) == 1) {
3834 /* mask set */
3835 } else {
3836 res = -EINVAL;
3837 goto errexit;
3841 if (!res) {
3842 tpacpi_disclose_usertask("procfs hotkey",
3843 "set mask to 0x%08x\n", mask);
3844 res = hotkey_user_mask_set(mask);
3847 errexit:
3848 mutex_unlock(&hotkey_mutex);
3849 return res;
3852 static const struct acpi_device_id ibm_htk_device_ids[] = {
3853 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3854 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
3855 {"", 0},
3858 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3859 .hid = ibm_htk_device_ids,
3860 .notify = hotkey_notify,
3861 .handle = &hkey_handle,
3862 .type = ACPI_DEVICE_NOTIFY,
3865 static struct ibm_struct hotkey_driver_data = {
3866 .name = "hotkey",
3867 .read = hotkey_read,
3868 .write = hotkey_write,
3869 .exit = hotkey_exit,
3870 .resume = hotkey_resume,
3871 .suspend = hotkey_suspend,
3872 .acpi = &ibm_hotkey_acpidriver,
3875 /*************************************************************************
3876 * Bluetooth subdriver
3879 enum {
3880 /* ACPI GBDC/SBDC bits */
3881 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3882 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3883 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3884 0 = disable, 1 = enable */
3887 enum {
3888 /* ACPI \BLTH commands */
3889 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3890 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3891 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3892 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3893 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3896 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3898 static int bluetooth_get_status(void)
3900 int status;
3902 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3903 if (dbg_bluetoothemul)
3904 return (tpacpi_bluetooth_emulstate) ?
3905 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3906 #endif
3908 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3909 return -EIO;
3911 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3912 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3915 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3917 int status;
3919 vdbg_printk(TPACPI_DBG_RFKILL,
3920 "will attempt to %s bluetooth\n",
3921 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3923 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3924 if (dbg_bluetoothemul) {
3925 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3926 return 0;
3928 #endif
3930 if (state == TPACPI_RFK_RADIO_ON)
3931 status = TP_ACPI_BLUETOOTH_RADIOSSW
3932 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3933 else
3934 status = 0;
3936 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3937 return -EIO;
3939 return 0;
3942 /* sysfs bluetooth enable ---------------------------------------------- */
3943 static ssize_t bluetooth_enable_show(struct device *dev,
3944 struct device_attribute *attr,
3945 char *buf)
3947 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3948 attr, buf);
3951 static ssize_t bluetooth_enable_store(struct device *dev,
3952 struct device_attribute *attr,
3953 const char *buf, size_t count)
3955 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3956 attr, buf, count);
3959 static struct device_attribute dev_attr_bluetooth_enable =
3960 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3961 bluetooth_enable_show, bluetooth_enable_store);
3963 /* --------------------------------------------------------------------- */
3965 static struct attribute *bluetooth_attributes[] = {
3966 &dev_attr_bluetooth_enable.attr,
3967 NULL
3970 static const struct attribute_group bluetooth_attr_group = {
3971 .attrs = bluetooth_attributes,
3974 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3975 .get_status = bluetooth_get_status,
3976 .set_status = bluetooth_set_status,
3979 static void bluetooth_shutdown(void)
3981 /* Order firmware to save current state to NVRAM */
3982 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3983 TP_ACPI_BLTH_SAVE_STATE))
3984 printk(TPACPI_NOTICE
3985 "failed to save bluetooth state to NVRAM\n");
3986 else
3987 vdbg_printk(TPACPI_DBG_RFKILL,
3988 "bluestooth state saved to NVRAM\n");
3991 static void bluetooth_exit(void)
3993 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3994 &bluetooth_attr_group);
3996 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3998 bluetooth_shutdown();
4001 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4003 int res;
4004 int status = 0;
4006 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4007 "initializing bluetooth subdriver\n");
4009 TPACPI_ACPIHANDLE_INIT(hkey);
4011 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4012 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4013 tp_features.bluetooth = hkey_handle &&
4014 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4016 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4017 "bluetooth is %s, status 0x%02x\n",
4018 str_supported(tp_features.bluetooth),
4019 status);
4021 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4022 if (dbg_bluetoothemul) {
4023 tp_features.bluetooth = 1;
4024 printk(TPACPI_INFO
4025 "bluetooth switch emulation enabled\n");
4026 } else
4027 #endif
4028 if (tp_features.bluetooth &&
4029 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4030 /* no bluetooth hardware present in system */
4031 tp_features.bluetooth = 0;
4032 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4033 "bluetooth hardware not installed\n");
4036 if (!tp_features.bluetooth)
4037 return 1;
4039 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4040 &bluetooth_tprfk_ops,
4041 RFKILL_TYPE_BLUETOOTH,
4042 TPACPI_RFK_BLUETOOTH_SW_NAME,
4043 true);
4044 if (res)
4045 return res;
4047 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4048 &bluetooth_attr_group);
4049 if (res) {
4050 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4051 return res;
4054 return 0;
4057 /* procfs -------------------------------------------------------------- */
4058 static int bluetooth_read(struct seq_file *m)
4060 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4063 static int bluetooth_write(char *buf)
4065 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4068 static struct ibm_struct bluetooth_driver_data = {
4069 .name = "bluetooth",
4070 .read = bluetooth_read,
4071 .write = bluetooth_write,
4072 .exit = bluetooth_exit,
4073 .shutdown = bluetooth_shutdown,
4076 /*************************************************************************
4077 * Wan subdriver
4080 enum {
4081 /* ACPI GWAN/SWAN bits */
4082 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4083 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4084 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4085 0 = disable, 1 = enable */
4088 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4090 static int wan_get_status(void)
4092 int status;
4094 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4095 if (dbg_wwanemul)
4096 return (tpacpi_wwan_emulstate) ?
4097 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4098 #endif
4100 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4101 return -EIO;
4103 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4104 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4107 static int wan_set_status(enum tpacpi_rfkill_state state)
4109 int status;
4111 vdbg_printk(TPACPI_DBG_RFKILL,
4112 "will attempt to %s wwan\n",
4113 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4115 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4116 if (dbg_wwanemul) {
4117 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4118 return 0;
4120 #endif
4122 if (state == TPACPI_RFK_RADIO_ON)
4123 status = TP_ACPI_WANCARD_RADIOSSW
4124 | TP_ACPI_WANCARD_RESUMECTRL;
4125 else
4126 status = 0;
4128 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4129 return -EIO;
4131 return 0;
4134 /* sysfs wan enable ---------------------------------------------------- */
4135 static ssize_t wan_enable_show(struct device *dev,
4136 struct device_attribute *attr,
4137 char *buf)
4139 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4140 attr, buf);
4143 static ssize_t wan_enable_store(struct device *dev,
4144 struct device_attribute *attr,
4145 const char *buf, size_t count)
4147 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4148 attr, buf, count);
4151 static struct device_attribute dev_attr_wan_enable =
4152 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4153 wan_enable_show, wan_enable_store);
4155 /* --------------------------------------------------------------------- */
4157 static struct attribute *wan_attributes[] = {
4158 &dev_attr_wan_enable.attr,
4159 NULL
4162 static const struct attribute_group wan_attr_group = {
4163 .attrs = wan_attributes,
4166 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4167 .get_status = wan_get_status,
4168 .set_status = wan_set_status,
4171 static void wan_shutdown(void)
4173 /* Order firmware to save current state to NVRAM */
4174 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4175 TP_ACPI_WGSV_SAVE_STATE))
4176 printk(TPACPI_NOTICE
4177 "failed to save WWAN state to NVRAM\n");
4178 else
4179 vdbg_printk(TPACPI_DBG_RFKILL,
4180 "WWAN state saved to NVRAM\n");
4183 static void wan_exit(void)
4185 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4186 &wan_attr_group);
4188 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4190 wan_shutdown();
4193 static int __init wan_init(struct ibm_init_struct *iibm)
4195 int res;
4196 int status = 0;
4198 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4199 "initializing wan subdriver\n");
4201 TPACPI_ACPIHANDLE_INIT(hkey);
4203 tp_features.wan = hkey_handle &&
4204 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4206 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4207 "wan is %s, status 0x%02x\n",
4208 str_supported(tp_features.wan),
4209 status);
4211 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4212 if (dbg_wwanemul) {
4213 tp_features.wan = 1;
4214 printk(TPACPI_INFO
4215 "wwan switch emulation enabled\n");
4216 } else
4217 #endif
4218 if (tp_features.wan &&
4219 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4220 /* no wan hardware present in system */
4221 tp_features.wan = 0;
4222 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4223 "wan hardware not installed\n");
4226 if (!tp_features.wan)
4227 return 1;
4229 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4230 &wan_tprfk_ops,
4231 RFKILL_TYPE_WWAN,
4232 TPACPI_RFK_WWAN_SW_NAME,
4233 true);
4234 if (res)
4235 return res;
4237 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4238 &wan_attr_group);
4240 if (res) {
4241 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4242 return res;
4245 return 0;
4248 /* procfs -------------------------------------------------------------- */
4249 static int wan_read(struct seq_file *m)
4251 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4254 static int wan_write(char *buf)
4256 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4259 static struct ibm_struct wan_driver_data = {
4260 .name = "wan",
4261 .read = wan_read,
4262 .write = wan_write,
4263 .exit = wan_exit,
4264 .shutdown = wan_shutdown,
4267 /*************************************************************************
4268 * UWB subdriver
4271 enum {
4272 /* ACPI GUWB/SUWB bits */
4273 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4274 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4277 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4279 static int uwb_get_status(void)
4281 int status;
4283 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4284 if (dbg_uwbemul)
4285 return (tpacpi_uwb_emulstate) ?
4286 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4287 #endif
4289 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4290 return -EIO;
4292 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4293 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4296 static int uwb_set_status(enum tpacpi_rfkill_state state)
4298 int status;
4300 vdbg_printk(TPACPI_DBG_RFKILL,
4301 "will attempt to %s UWB\n",
4302 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4304 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4305 if (dbg_uwbemul) {
4306 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4307 return 0;
4309 #endif
4311 if (state == TPACPI_RFK_RADIO_ON)
4312 status = TP_ACPI_UWB_RADIOSSW;
4313 else
4314 status = 0;
4316 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4317 return -EIO;
4319 return 0;
4322 /* --------------------------------------------------------------------- */
4324 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4325 .get_status = uwb_get_status,
4326 .set_status = uwb_set_status,
4329 static void uwb_exit(void)
4331 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4334 static int __init uwb_init(struct ibm_init_struct *iibm)
4336 int res;
4337 int status = 0;
4339 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4340 "initializing uwb subdriver\n");
4342 TPACPI_ACPIHANDLE_INIT(hkey);
4344 tp_features.uwb = hkey_handle &&
4345 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4347 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4348 "uwb is %s, status 0x%02x\n",
4349 str_supported(tp_features.uwb),
4350 status);
4352 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4353 if (dbg_uwbemul) {
4354 tp_features.uwb = 1;
4355 printk(TPACPI_INFO
4356 "uwb switch emulation enabled\n");
4357 } else
4358 #endif
4359 if (tp_features.uwb &&
4360 !(status & TP_ACPI_UWB_HWPRESENT)) {
4361 /* no uwb hardware present in system */
4362 tp_features.uwb = 0;
4363 dbg_printk(TPACPI_DBG_INIT,
4364 "uwb hardware not installed\n");
4367 if (!tp_features.uwb)
4368 return 1;
4370 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4371 &uwb_tprfk_ops,
4372 RFKILL_TYPE_UWB,
4373 TPACPI_RFK_UWB_SW_NAME,
4374 false);
4375 return res;
4378 static struct ibm_struct uwb_driver_data = {
4379 .name = "uwb",
4380 .exit = uwb_exit,
4381 .flags.experimental = 1,
4384 /*************************************************************************
4385 * Video subdriver
4388 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4390 enum video_access_mode {
4391 TPACPI_VIDEO_NONE = 0,
4392 TPACPI_VIDEO_570, /* 570 */
4393 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4394 TPACPI_VIDEO_NEW, /* all others */
4397 enum { /* video status flags, based on VIDEO_570 */
4398 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4399 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4400 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4403 enum { /* TPACPI_VIDEO_570 constants */
4404 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4405 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4406 * video_status_flags */
4407 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4408 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4411 static enum video_access_mode video_supported;
4412 static int video_orig_autosw;
4414 static int video_autosw_get(void);
4415 static int video_autosw_set(int enable);
4417 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4419 static int __init video_init(struct ibm_init_struct *iibm)
4421 int ivga;
4423 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4425 TPACPI_ACPIHANDLE_INIT(vid);
4426 if (tpacpi_is_ibm())
4427 TPACPI_ACPIHANDLE_INIT(vid2);
4429 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4430 /* G41, assume IVGA doesn't change */
4431 vid_handle = vid2_handle;
4433 if (!vid_handle)
4434 /* video switching not supported on R30, R31 */
4435 video_supported = TPACPI_VIDEO_NONE;
4436 else if (tpacpi_is_ibm() &&
4437 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4438 /* 570 */
4439 video_supported = TPACPI_VIDEO_570;
4440 else if (tpacpi_is_ibm() &&
4441 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4442 /* 600e/x, 770e, 770x */
4443 video_supported = TPACPI_VIDEO_770;
4444 else
4445 /* all others */
4446 video_supported = TPACPI_VIDEO_NEW;
4448 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4449 str_supported(video_supported != TPACPI_VIDEO_NONE),
4450 video_supported);
4452 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4455 static void video_exit(void)
4457 dbg_printk(TPACPI_DBG_EXIT,
4458 "restoring original video autoswitch mode\n");
4459 if (video_autosw_set(video_orig_autosw))
4460 printk(TPACPI_ERR "error while trying to restore original "
4461 "video autoswitch mode\n");
4464 static int video_outputsw_get(void)
4466 int status = 0;
4467 int i;
4469 switch (video_supported) {
4470 case TPACPI_VIDEO_570:
4471 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4472 TP_ACPI_VIDEO_570_PHSCMD))
4473 return -EIO;
4474 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4475 break;
4476 case TPACPI_VIDEO_770:
4477 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4478 return -EIO;
4479 if (i)
4480 status |= TP_ACPI_VIDEO_S_LCD;
4481 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4482 return -EIO;
4483 if (i)
4484 status |= TP_ACPI_VIDEO_S_CRT;
4485 break;
4486 case TPACPI_VIDEO_NEW:
4487 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4488 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4489 return -EIO;
4490 if (i)
4491 status |= TP_ACPI_VIDEO_S_CRT;
4493 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4494 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4495 return -EIO;
4496 if (i)
4497 status |= TP_ACPI_VIDEO_S_LCD;
4498 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4499 return -EIO;
4500 if (i)
4501 status |= TP_ACPI_VIDEO_S_DVI;
4502 break;
4503 default:
4504 return -ENOSYS;
4507 return status;
4510 static int video_outputsw_set(int status)
4512 int autosw;
4513 int res = 0;
4515 switch (video_supported) {
4516 case TPACPI_VIDEO_570:
4517 res = acpi_evalf(NULL, NULL,
4518 "\\_SB.PHS2", "vdd",
4519 TP_ACPI_VIDEO_570_PHS2CMD,
4520 status | TP_ACPI_VIDEO_570_PHS2SET);
4521 break;
4522 case TPACPI_VIDEO_770:
4523 autosw = video_autosw_get();
4524 if (autosw < 0)
4525 return autosw;
4527 res = video_autosw_set(1);
4528 if (res)
4529 return res;
4530 res = acpi_evalf(vid_handle, NULL,
4531 "ASWT", "vdd", status * 0x100, 0);
4532 if (!autosw && video_autosw_set(autosw)) {
4533 printk(TPACPI_ERR
4534 "video auto-switch left enabled due to error\n");
4535 return -EIO;
4537 break;
4538 case TPACPI_VIDEO_NEW:
4539 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4540 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4541 break;
4542 default:
4543 return -ENOSYS;
4546 return (res)? 0 : -EIO;
4549 static int video_autosw_get(void)
4551 int autosw = 0;
4553 switch (video_supported) {
4554 case TPACPI_VIDEO_570:
4555 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4556 return -EIO;
4557 break;
4558 case TPACPI_VIDEO_770:
4559 case TPACPI_VIDEO_NEW:
4560 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4561 return -EIO;
4562 break;
4563 default:
4564 return -ENOSYS;
4567 return autosw & 1;
4570 static int video_autosw_set(int enable)
4572 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4573 return -EIO;
4574 return 0;
4577 static int video_outputsw_cycle(void)
4579 int autosw = video_autosw_get();
4580 int res;
4582 if (autosw < 0)
4583 return autosw;
4585 switch (video_supported) {
4586 case TPACPI_VIDEO_570:
4587 res = video_autosw_set(1);
4588 if (res)
4589 return res;
4590 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4591 break;
4592 case TPACPI_VIDEO_770:
4593 case TPACPI_VIDEO_NEW:
4594 res = video_autosw_set(1);
4595 if (res)
4596 return res;
4597 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4598 break;
4599 default:
4600 return -ENOSYS;
4602 if (!autosw && video_autosw_set(autosw)) {
4603 printk(TPACPI_ERR
4604 "video auto-switch left enabled due to error\n");
4605 return -EIO;
4608 return (res)? 0 : -EIO;
4611 static int video_expand_toggle(void)
4613 switch (video_supported) {
4614 case TPACPI_VIDEO_570:
4615 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4616 0 : -EIO;
4617 case TPACPI_VIDEO_770:
4618 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4619 0 : -EIO;
4620 case TPACPI_VIDEO_NEW:
4621 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4622 0 : -EIO;
4623 default:
4624 return -ENOSYS;
4626 /* not reached */
4629 static int video_read(struct seq_file *m)
4631 int status, autosw;
4633 if (video_supported == TPACPI_VIDEO_NONE) {
4634 seq_printf(m, "status:\t\tnot supported\n");
4635 return 0;
4638 /* Even reads can crash X.org, so... */
4639 if (!capable(CAP_SYS_ADMIN))
4640 return -EPERM;
4642 status = video_outputsw_get();
4643 if (status < 0)
4644 return status;
4646 autosw = video_autosw_get();
4647 if (autosw < 0)
4648 return autosw;
4650 seq_printf(m, "status:\t\tsupported\n");
4651 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4652 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4653 if (video_supported == TPACPI_VIDEO_NEW)
4654 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4655 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4656 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4657 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4658 if (video_supported == TPACPI_VIDEO_NEW)
4659 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4660 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4661 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4663 return 0;
4666 static int video_write(char *buf)
4668 char *cmd;
4669 int enable, disable, status;
4670 int res;
4672 if (video_supported == TPACPI_VIDEO_NONE)
4673 return -ENODEV;
4675 /* Even reads can crash X.org, let alone writes... */
4676 if (!capable(CAP_SYS_ADMIN))
4677 return -EPERM;
4679 enable = 0;
4680 disable = 0;
4682 while ((cmd = next_cmd(&buf))) {
4683 if (strlencmp(cmd, "lcd_enable") == 0) {
4684 enable |= TP_ACPI_VIDEO_S_LCD;
4685 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4686 disable |= TP_ACPI_VIDEO_S_LCD;
4687 } else if (strlencmp(cmd, "crt_enable") == 0) {
4688 enable |= TP_ACPI_VIDEO_S_CRT;
4689 } else if (strlencmp(cmd, "crt_disable") == 0) {
4690 disable |= TP_ACPI_VIDEO_S_CRT;
4691 } else if (video_supported == TPACPI_VIDEO_NEW &&
4692 strlencmp(cmd, "dvi_enable") == 0) {
4693 enable |= TP_ACPI_VIDEO_S_DVI;
4694 } else if (video_supported == TPACPI_VIDEO_NEW &&
4695 strlencmp(cmd, "dvi_disable") == 0) {
4696 disable |= TP_ACPI_VIDEO_S_DVI;
4697 } else if (strlencmp(cmd, "auto_enable") == 0) {
4698 res = video_autosw_set(1);
4699 if (res)
4700 return res;
4701 } else if (strlencmp(cmd, "auto_disable") == 0) {
4702 res = video_autosw_set(0);
4703 if (res)
4704 return res;
4705 } else if (strlencmp(cmd, "video_switch") == 0) {
4706 res = video_outputsw_cycle();
4707 if (res)
4708 return res;
4709 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4710 res = video_expand_toggle();
4711 if (res)
4712 return res;
4713 } else
4714 return -EINVAL;
4717 if (enable || disable) {
4718 status = video_outputsw_get();
4719 if (status < 0)
4720 return status;
4721 res = video_outputsw_set((status & ~disable) | enable);
4722 if (res)
4723 return res;
4726 return 0;
4729 static struct ibm_struct video_driver_data = {
4730 .name = "video",
4731 .read = video_read,
4732 .write = video_write,
4733 .exit = video_exit,
4736 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4738 /*************************************************************************
4739 * Light (thinklight) subdriver
4742 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4743 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4745 static int light_get_status(void)
4747 int status = 0;
4749 if (tp_features.light_status) {
4750 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4751 return -EIO;
4752 return (!!status);
4755 return -ENXIO;
4758 static int light_set_status(int status)
4760 int rc;
4762 if (tp_features.light) {
4763 if (cmos_handle) {
4764 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4765 (status)?
4766 TP_CMOS_THINKLIGHT_ON :
4767 TP_CMOS_THINKLIGHT_OFF);
4768 } else {
4769 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4770 (status)? 1 : 0);
4772 return (rc)? 0 : -EIO;
4775 return -ENXIO;
4778 static void light_set_status_worker(struct work_struct *work)
4780 struct tpacpi_led_classdev *data =
4781 container_of(work, struct tpacpi_led_classdev, work);
4783 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4784 light_set_status((data->new_state != TPACPI_LED_OFF));
4787 static void light_sysfs_set(struct led_classdev *led_cdev,
4788 enum led_brightness brightness)
4790 struct tpacpi_led_classdev *data =
4791 container_of(led_cdev,
4792 struct tpacpi_led_classdev,
4793 led_classdev);
4794 data->new_state = (brightness != LED_OFF) ?
4795 TPACPI_LED_ON : TPACPI_LED_OFF;
4796 queue_work(tpacpi_wq, &data->work);
4799 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4801 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4804 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4805 .led_classdev = {
4806 .name = "tpacpi::thinklight",
4807 .brightness_set = &light_sysfs_set,
4808 .brightness_get = &light_sysfs_get,
4812 static int __init light_init(struct ibm_init_struct *iibm)
4814 int rc;
4816 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4818 if (tpacpi_is_ibm()) {
4819 TPACPI_ACPIHANDLE_INIT(ledb);
4820 TPACPI_ACPIHANDLE_INIT(lght);
4822 TPACPI_ACPIHANDLE_INIT(cmos);
4823 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4825 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4826 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4828 if (tp_features.light)
4829 /* light status not supported on
4830 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4831 tp_features.light_status =
4832 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4834 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4835 str_supported(tp_features.light),
4836 str_supported(tp_features.light_status));
4838 if (!tp_features.light)
4839 return 1;
4841 rc = led_classdev_register(&tpacpi_pdev->dev,
4842 &tpacpi_led_thinklight.led_classdev);
4844 if (rc < 0) {
4845 tp_features.light = 0;
4846 tp_features.light_status = 0;
4847 } else {
4848 rc = 0;
4851 return rc;
4854 static void light_exit(void)
4856 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4857 if (work_pending(&tpacpi_led_thinklight.work))
4858 flush_workqueue(tpacpi_wq);
4861 static int light_read(struct seq_file *m)
4863 int status;
4865 if (!tp_features.light) {
4866 seq_printf(m, "status:\t\tnot supported\n");
4867 } else if (!tp_features.light_status) {
4868 seq_printf(m, "status:\t\tunknown\n");
4869 seq_printf(m, "commands:\ton, off\n");
4870 } else {
4871 status = light_get_status();
4872 if (status < 0)
4873 return status;
4874 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4875 seq_printf(m, "commands:\ton, off\n");
4878 return 0;
4881 static int light_write(char *buf)
4883 char *cmd;
4884 int newstatus = 0;
4886 if (!tp_features.light)
4887 return -ENODEV;
4889 while ((cmd = next_cmd(&buf))) {
4890 if (strlencmp(cmd, "on") == 0) {
4891 newstatus = 1;
4892 } else if (strlencmp(cmd, "off") == 0) {
4893 newstatus = 0;
4894 } else
4895 return -EINVAL;
4898 return light_set_status(newstatus);
4901 static struct ibm_struct light_driver_data = {
4902 .name = "light",
4903 .read = light_read,
4904 .write = light_write,
4905 .exit = light_exit,
4908 /*************************************************************************
4909 * CMOS subdriver
4912 /* sysfs cmos_command -------------------------------------------------- */
4913 static ssize_t cmos_command_store(struct device *dev,
4914 struct device_attribute *attr,
4915 const char *buf, size_t count)
4917 unsigned long cmos_cmd;
4918 int res;
4920 if (parse_strtoul(buf, 21, &cmos_cmd))
4921 return -EINVAL;
4923 res = issue_thinkpad_cmos_command(cmos_cmd);
4924 return (res)? res : count;
4927 static struct device_attribute dev_attr_cmos_command =
4928 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4930 /* --------------------------------------------------------------------- */
4932 static int __init cmos_init(struct ibm_init_struct *iibm)
4934 int res;
4936 vdbg_printk(TPACPI_DBG_INIT,
4937 "initializing cmos commands subdriver\n");
4939 TPACPI_ACPIHANDLE_INIT(cmos);
4941 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4942 str_supported(cmos_handle != NULL));
4944 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4945 if (res)
4946 return res;
4948 return (cmos_handle)? 0 : 1;
4951 static void cmos_exit(void)
4953 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4956 static int cmos_read(struct seq_file *m)
4958 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4959 R30, R31, T20-22, X20-21 */
4960 if (!cmos_handle)
4961 seq_printf(m, "status:\t\tnot supported\n");
4962 else {
4963 seq_printf(m, "status:\t\tsupported\n");
4964 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4967 return 0;
4970 static int cmos_write(char *buf)
4972 char *cmd;
4973 int cmos_cmd, res;
4975 while ((cmd = next_cmd(&buf))) {
4976 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4977 cmos_cmd >= 0 && cmos_cmd <= 21) {
4978 /* cmos_cmd set */
4979 } else
4980 return -EINVAL;
4982 res = issue_thinkpad_cmos_command(cmos_cmd);
4983 if (res)
4984 return res;
4987 return 0;
4990 static struct ibm_struct cmos_driver_data = {
4991 .name = "cmos",
4992 .read = cmos_read,
4993 .write = cmos_write,
4994 .exit = cmos_exit,
4997 /*************************************************************************
4998 * LED subdriver
5001 enum led_access_mode {
5002 TPACPI_LED_NONE = 0,
5003 TPACPI_LED_570, /* 570 */
5004 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5005 TPACPI_LED_NEW, /* all others */
5008 enum { /* For TPACPI_LED_OLD */
5009 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5010 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5011 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5014 static enum led_access_mode led_supported;
5016 static acpi_handle led_handle;
5018 #define TPACPI_LED_NUMLEDS 16
5019 static struct tpacpi_led_classdev *tpacpi_leds;
5020 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5021 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5022 /* there's a limit of 19 chars + NULL before 2.6.26 */
5023 "tpacpi::power",
5024 "tpacpi:orange:batt",
5025 "tpacpi:green:batt",
5026 "tpacpi::dock_active",
5027 "tpacpi::bay_active",
5028 "tpacpi::dock_batt",
5029 "tpacpi::unknown_led",
5030 "tpacpi::standby",
5031 "tpacpi::dock_status1",
5032 "tpacpi::dock_status2",
5033 "tpacpi::unknown_led2",
5034 "tpacpi::unknown_led3",
5035 "tpacpi::thinkvantage",
5037 #define TPACPI_SAFE_LEDS 0x1081U
5039 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5041 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5042 return false;
5043 #else
5044 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5045 #endif
5048 static int led_get_status(const unsigned int led)
5050 int status;
5051 enum led_status_t led_s;
5053 switch (led_supported) {
5054 case TPACPI_LED_570:
5055 if (!acpi_evalf(ec_handle,
5056 &status, "GLED", "dd", 1 << led))
5057 return -EIO;
5058 led_s = (status == 0)?
5059 TPACPI_LED_OFF :
5060 ((status == 1)?
5061 TPACPI_LED_ON :
5062 TPACPI_LED_BLINK);
5063 tpacpi_led_state_cache[led] = led_s;
5064 return led_s;
5065 default:
5066 return -ENXIO;
5069 /* not reached */
5072 static int led_set_status(const unsigned int led,
5073 const enum led_status_t ledstatus)
5075 /* off, on, blink. Index is led_status_t */
5076 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5077 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5079 int rc = 0;
5081 switch (led_supported) {
5082 case TPACPI_LED_570:
5083 /* 570 */
5084 if (unlikely(led > 7))
5085 return -EINVAL;
5086 if (unlikely(tpacpi_is_led_restricted(led)))
5087 return -EPERM;
5088 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5089 (1 << led), led_sled_arg1[ledstatus]))
5090 rc = -EIO;
5091 break;
5092 case TPACPI_LED_OLD:
5093 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5094 if (unlikely(led > 7))
5095 return -EINVAL;
5096 if (unlikely(tpacpi_is_led_restricted(led)))
5097 return -EPERM;
5098 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5099 if (rc >= 0)
5100 rc = ec_write(TPACPI_LED_EC_HLBL,
5101 (ledstatus == TPACPI_LED_BLINK) << led);
5102 if (rc >= 0)
5103 rc = ec_write(TPACPI_LED_EC_HLCL,
5104 (ledstatus != TPACPI_LED_OFF) << led);
5105 break;
5106 case TPACPI_LED_NEW:
5107 /* all others */
5108 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5109 return -EINVAL;
5110 if (unlikely(tpacpi_is_led_restricted(led)))
5111 return -EPERM;
5112 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5113 led, led_led_arg1[ledstatus]))
5114 rc = -EIO;
5115 break;
5116 default:
5117 rc = -ENXIO;
5120 if (!rc)
5121 tpacpi_led_state_cache[led] = ledstatus;
5123 return rc;
5126 static void led_set_status_worker(struct work_struct *work)
5128 struct tpacpi_led_classdev *data =
5129 container_of(work, struct tpacpi_led_classdev, work);
5131 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5132 led_set_status(data->led, data->new_state);
5135 static void led_sysfs_set(struct led_classdev *led_cdev,
5136 enum led_brightness brightness)
5138 struct tpacpi_led_classdev *data = container_of(led_cdev,
5139 struct tpacpi_led_classdev, led_classdev);
5141 if (brightness == LED_OFF)
5142 data->new_state = TPACPI_LED_OFF;
5143 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5144 data->new_state = TPACPI_LED_ON;
5145 else
5146 data->new_state = TPACPI_LED_BLINK;
5148 queue_work(tpacpi_wq, &data->work);
5151 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5152 unsigned long *delay_on, unsigned long *delay_off)
5154 struct tpacpi_led_classdev *data = container_of(led_cdev,
5155 struct tpacpi_led_classdev, led_classdev);
5157 /* Can we choose the flash rate? */
5158 if (*delay_on == 0 && *delay_off == 0) {
5159 /* yes. set them to the hardware blink rate (1 Hz) */
5160 *delay_on = 500; /* ms */
5161 *delay_off = 500; /* ms */
5162 } else if ((*delay_on != 500) || (*delay_off != 500))
5163 return -EINVAL;
5165 data->new_state = TPACPI_LED_BLINK;
5166 queue_work(tpacpi_wq, &data->work);
5168 return 0;
5171 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5173 int rc;
5175 struct tpacpi_led_classdev *data = container_of(led_cdev,
5176 struct tpacpi_led_classdev, led_classdev);
5178 rc = led_get_status(data->led);
5180 if (rc == TPACPI_LED_OFF || rc < 0)
5181 rc = LED_OFF; /* no error handling in led class :( */
5182 else
5183 rc = LED_FULL;
5185 return rc;
5188 static void led_exit(void)
5190 unsigned int i;
5192 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5193 if (tpacpi_leds[i].led_classdev.name)
5194 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5197 kfree(tpacpi_leds);
5200 static int __init tpacpi_init_led(unsigned int led)
5202 int rc;
5204 tpacpi_leds[led].led = led;
5206 /* LEDs with no name don't get registered */
5207 if (!tpacpi_led_names[led])
5208 return 0;
5210 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5211 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5212 if (led_supported == TPACPI_LED_570)
5213 tpacpi_leds[led].led_classdev.brightness_get =
5214 &led_sysfs_get;
5216 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5218 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5220 rc = led_classdev_register(&tpacpi_pdev->dev,
5221 &tpacpi_leds[led].led_classdev);
5222 if (rc < 0)
5223 tpacpi_leds[led].led_classdev.name = NULL;
5225 return rc;
5228 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5229 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5230 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5231 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5233 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5234 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5235 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5236 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5237 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5238 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5239 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5240 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5242 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5243 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5244 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5245 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5246 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5248 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5249 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5250 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5251 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5253 /* (1) - may have excess leds enabled on MSB */
5255 /* Defaults (order matters, keep last, don't reorder!) */
5256 { /* Lenovo */
5257 .vendor = PCI_VENDOR_ID_LENOVO,
5258 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5259 .quirks = 0x1fffU,
5261 { /* IBM ThinkPads with no EC version string */
5262 .vendor = PCI_VENDOR_ID_IBM,
5263 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5264 .quirks = 0x00ffU,
5266 { /* IBM ThinkPads with EC version string */
5267 .vendor = PCI_VENDOR_ID_IBM,
5268 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5269 .quirks = 0x00bfU,
5273 #undef TPACPI_LEDQ_IBM
5274 #undef TPACPI_LEDQ_LNV
5276 static enum led_access_mode __init led_init_detect_mode(void)
5278 acpi_status status;
5280 if (tpacpi_is_ibm()) {
5281 /* 570 */
5282 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5283 if (ACPI_SUCCESS(status))
5284 return TPACPI_LED_570;
5286 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5287 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5288 if (ACPI_SUCCESS(status))
5289 return TPACPI_LED_OLD;
5292 /* most others */
5293 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5294 if (ACPI_SUCCESS(status))
5295 return TPACPI_LED_NEW;
5297 /* R30, R31, and unknown firmwares */
5298 led_handle = NULL;
5299 return TPACPI_LED_NONE;
5302 static int __init led_init(struct ibm_init_struct *iibm)
5304 unsigned int i;
5305 int rc;
5306 unsigned long useful_leds;
5308 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5310 led_supported = led_init_detect_mode();
5312 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5313 str_supported(led_supported), led_supported);
5315 if (led_supported == TPACPI_LED_NONE)
5316 return 1;
5318 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5319 GFP_KERNEL);
5320 if (!tpacpi_leds) {
5321 printk(TPACPI_ERR "Out of memory for LED data\n");
5322 return -ENOMEM;
5325 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5326 ARRAY_SIZE(led_useful_qtable));
5328 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5329 if (!tpacpi_is_led_restricted(i) &&
5330 test_bit(i, &useful_leds)) {
5331 rc = tpacpi_init_led(i);
5332 if (rc < 0) {
5333 led_exit();
5334 return rc;
5339 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5340 printk(TPACPI_NOTICE
5341 "warning: userspace override of important "
5342 "firmware LEDs is enabled\n");
5343 #endif
5344 return 0;
5347 #define str_led_status(s) \
5348 ((s) == TPACPI_LED_OFF ? "off" : \
5349 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5351 static int led_read(struct seq_file *m)
5353 if (!led_supported) {
5354 seq_printf(m, "status:\t\tnot supported\n");
5355 return 0;
5357 seq_printf(m, "status:\t\tsupported\n");
5359 if (led_supported == TPACPI_LED_570) {
5360 /* 570 */
5361 int i, status;
5362 for (i = 0; i < 8; i++) {
5363 status = led_get_status(i);
5364 if (status < 0)
5365 return -EIO;
5366 seq_printf(m, "%d:\t\t%s\n",
5367 i, str_led_status(status));
5371 seq_printf(m, "commands:\t"
5372 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5374 return 0;
5377 static int led_write(char *buf)
5379 char *cmd;
5380 int led, rc;
5381 enum led_status_t s;
5383 if (!led_supported)
5384 return -ENODEV;
5386 while ((cmd = next_cmd(&buf))) {
5387 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5388 return -EINVAL;
5390 if (strstr(cmd, "off")) {
5391 s = TPACPI_LED_OFF;
5392 } else if (strstr(cmd, "on")) {
5393 s = TPACPI_LED_ON;
5394 } else if (strstr(cmd, "blink")) {
5395 s = TPACPI_LED_BLINK;
5396 } else {
5397 return -EINVAL;
5400 rc = led_set_status(led, s);
5401 if (rc < 0)
5402 return rc;
5405 return 0;
5408 static struct ibm_struct led_driver_data = {
5409 .name = "led",
5410 .read = led_read,
5411 .write = led_write,
5412 .exit = led_exit,
5415 /*************************************************************************
5416 * Beep subdriver
5419 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5421 #define TPACPI_BEEP_Q1 0x0001
5423 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5424 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5425 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5428 static int __init beep_init(struct ibm_init_struct *iibm)
5430 unsigned long quirks;
5432 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5434 TPACPI_ACPIHANDLE_INIT(beep);
5436 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5437 str_supported(beep_handle != NULL));
5439 quirks = tpacpi_check_quirks(beep_quirk_table,
5440 ARRAY_SIZE(beep_quirk_table));
5442 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5444 return (beep_handle)? 0 : 1;
5447 static int beep_read(struct seq_file *m)
5449 if (!beep_handle)
5450 seq_printf(m, "status:\t\tnot supported\n");
5451 else {
5452 seq_printf(m, "status:\t\tsupported\n");
5453 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5456 return 0;
5459 static int beep_write(char *buf)
5461 char *cmd;
5462 int beep_cmd;
5464 if (!beep_handle)
5465 return -ENODEV;
5467 while ((cmd = next_cmd(&buf))) {
5468 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5469 beep_cmd >= 0 && beep_cmd <= 17) {
5470 /* beep_cmd set */
5471 } else
5472 return -EINVAL;
5473 if (tp_features.beep_needs_two_args) {
5474 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5475 beep_cmd, 0))
5476 return -EIO;
5477 } else {
5478 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5479 beep_cmd))
5480 return -EIO;
5484 return 0;
5487 static struct ibm_struct beep_driver_data = {
5488 .name = "beep",
5489 .read = beep_read,
5490 .write = beep_write,
5493 /*************************************************************************
5494 * Thermal subdriver
5497 enum thermal_access_mode {
5498 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5499 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5500 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5501 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5502 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5505 enum { /* TPACPI_THERMAL_TPEC_* */
5506 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5507 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5508 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5510 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5514 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5515 struct ibm_thermal_sensors_struct {
5516 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5519 static enum thermal_access_mode thermal_read_mode;
5521 /* idx is zero-based */
5522 static int thermal_get_sensor(int idx, s32 *value)
5524 int t;
5525 s8 tmp;
5526 char tmpi[5];
5528 t = TP_EC_THERMAL_TMP0;
5530 switch (thermal_read_mode) {
5531 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5532 case TPACPI_THERMAL_TPEC_16:
5533 if (idx >= 8 && idx <= 15) {
5534 t = TP_EC_THERMAL_TMP8;
5535 idx -= 8;
5537 /* fallthrough */
5538 #endif
5539 case TPACPI_THERMAL_TPEC_8:
5540 if (idx <= 7) {
5541 if (!acpi_ec_read(t + idx, &tmp))
5542 return -EIO;
5543 *value = tmp * 1000;
5544 return 0;
5546 break;
5548 case TPACPI_THERMAL_ACPI_UPDT:
5549 if (idx <= 7) {
5550 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5551 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5552 return -EIO;
5553 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5554 return -EIO;
5555 *value = (t - 2732) * 100;
5556 return 0;
5558 break;
5560 case TPACPI_THERMAL_ACPI_TMP07:
5561 if (idx <= 7) {
5562 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5563 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5564 return -EIO;
5565 if (t > 127 || t < -127)
5566 t = TP_EC_THERMAL_TMP_NA;
5567 *value = t * 1000;
5568 return 0;
5570 break;
5572 case TPACPI_THERMAL_NONE:
5573 default:
5574 return -ENOSYS;
5577 return -EINVAL;
5580 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5582 int res, i;
5583 int n;
5585 n = 8;
5586 i = 0;
5588 if (!s)
5589 return -EINVAL;
5591 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5592 n = 16;
5594 for (i = 0 ; i < n; i++) {
5595 res = thermal_get_sensor(i, &s->temp[i]);
5596 if (res)
5597 return res;
5600 return n;
5603 static void thermal_dump_all_sensors(void)
5605 int n, i;
5606 struct ibm_thermal_sensors_struct t;
5608 n = thermal_get_sensors(&t);
5609 if (n <= 0)
5610 return;
5612 printk(TPACPI_NOTICE
5613 "temperatures (Celsius):");
5615 for (i = 0; i < n; i++) {
5616 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5617 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5618 else
5619 printk(KERN_CONT " N/A");
5622 printk(KERN_CONT "\n");
5625 /* sysfs temp##_input -------------------------------------------------- */
5627 static ssize_t thermal_temp_input_show(struct device *dev,
5628 struct device_attribute *attr,
5629 char *buf)
5631 struct sensor_device_attribute *sensor_attr =
5632 to_sensor_dev_attr(attr);
5633 int idx = sensor_attr->index;
5634 s32 value;
5635 int res;
5637 res = thermal_get_sensor(idx, &value);
5638 if (res)
5639 return res;
5640 if (value == TPACPI_THERMAL_SENSOR_NA)
5641 return -ENXIO;
5643 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5646 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5647 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5648 thermal_temp_input_show, NULL, _idxB)
5650 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5651 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5652 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5653 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5654 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5655 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5656 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5657 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5658 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5659 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5660 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5661 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5662 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5663 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5664 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5665 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5666 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5669 #define THERMAL_ATTRS(X) \
5670 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5672 static struct attribute *thermal_temp_input_attr[] = {
5673 THERMAL_ATTRS(8),
5674 THERMAL_ATTRS(9),
5675 THERMAL_ATTRS(10),
5676 THERMAL_ATTRS(11),
5677 THERMAL_ATTRS(12),
5678 THERMAL_ATTRS(13),
5679 THERMAL_ATTRS(14),
5680 THERMAL_ATTRS(15),
5681 THERMAL_ATTRS(0),
5682 THERMAL_ATTRS(1),
5683 THERMAL_ATTRS(2),
5684 THERMAL_ATTRS(3),
5685 THERMAL_ATTRS(4),
5686 THERMAL_ATTRS(5),
5687 THERMAL_ATTRS(6),
5688 THERMAL_ATTRS(7),
5689 NULL
5692 static const struct attribute_group thermal_temp_input16_group = {
5693 .attrs = thermal_temp_input_attr
5696 static const struct attribute_group thermal_temp_input8_group = {
5697 .attrs = &thermal_temp_input_attr[8]
5700 #undef THERMAL_SENSOR_ATTR_TEMP
5701 #undef THERMAL_ATTRS
5703 /* --------------------------------------------------------------------- */
5705 static int __init thermal_init(struct ibm_init_struct *iibm)
5707 u8 t, ta1, ta2;
5708 int i;
5709 int acpi_tmp7;
5710 int res;
5712 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5714 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5716 if (thinkpad_id.ec_model) {
5718 * Direct EC access mode: sensors at registers
5719 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5720 * non-implemented, thermal sensors return 0x80 when
5721 * not available
5724 ta1 = ta2 = 0;
5725 for (i = 0; i < 8; i++) {
5726 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5727 ta1 |= t;
5728 } else {
5729 ta1 = 0;
5730 break;
5732 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5733 ta2 |= t;
5734 } else {
5735 ta1 = 0;
5736 break;
5739 if (ta1 == 0) {
5740 /* This is sheer paranoia, but we handle it anyway */
5741 if (acpi_tmp7) {
5742 printk(TPACPI_ERR
5743 "ThinkPad ACPI EC access misbehaving, "
5744 "falling back to ACPI TMPx access "
5745 "mode\n");
5746 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5747 } else {
5748 printk(TPACPI_ERR
5749 "ThinkPad ACPI EC access misbehaving, "
5750 "disabling thermal sensors access\n");
5751 thermal_read_mode = TPACPI_THERMAL_NONE;
5753 } else {
5754 thermal_read_mode =
5755 (ta2 != 0) ?
5756 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5758 } else if (acpi_tmp7) {
5759 if (tpacpi_is_ibm() &&
5760 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5761 /* 600e/x, 770e, 770x */
5762 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5763 } else {
5764 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
5765 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5767 } else {
5768 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5769 thermal_read_mode = TPACPI_THERMAL_NONE;
5772 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5773 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5774 thermal_read_mode);
5776 switch (thermal_read_mode) {
5777 case TPACPI_THERMAL_TPEC_16:
5778 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5779 &thermal_temp_input16_group);
5780 if (res)
5781 return res;
5782 break;
5783 case TPACPI_THERMAL_TPEC_8:
5784 case TPACPI_THERMAL_ACPI_TMP07:
5785 case TPACPI_THERMAL_ACPI_UPDT:
5786 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5787 &thermal_temp_input8_group);
5788 if (res)
5789 return res;
5790 break;
5791 case TPACPI_THERMAL_NONE:
5792 default:
5793 return 1;
5796 return 0;
5799 static void thermal_exit(void)
5801 switch (thermal_read_mode) {
5802 case TPACPI_THERMAL_TPEC_16:
5803 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5804 &thermal_temp_input16_group);
5805 break;
5806 case TPACPI_THERMAL_TPEC_8:
5807 case TPACPI_THERMAL_ACPI_TMP07:
5808 case TPACPI_THERMAL_ACPI_UPDT:
5809 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5810 &thermal_temp_input8_group);
5811 break;
5812 case TPACPI_THERMAL_NONE:
5813 default:
5814 break;
5818 static int thermal_read(struct seq_file *m)
5820 int n, i;
5821 struct ibm_thermal_sensors_struct t;
5823 n = thermal_get_sensors(&t);
5824 if (unlikely(n < 0))
5825 return n;
5827 seq_printf(m, "temperatures:\t");
5829 if (n > 0) {
5830 for (i = 0; i < (n - 1); i++)
5831 seq_printf(m, "%d ", t.temp[i] / 1000);
5832 seq_printf(m, "%d\n", t.temp[i] / 1000);
5833 } else
5834 seq_printf(m, "not supported\n");
5836 return 0;
5839 static struct ibm_struct thermal_driver_data = {
5840 .name = "thermal",
5841 .read = thermal_read,
5842 .exit = thermal_exit,
5845 /*************************************************************************
5846 * EC Dump subdriver
5849 static u8 ecdump_regs[256];
5851 static int ecdump_read(struct seq_file *m)
5853 int i, j;
5854 u8 v;
5856 seq_printf(m, "EC "
5857 " +00 +01 +02 +03 +04 +05 +06 +07"
5858 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5859 for (i = 0; i < 256; i += 16) {
5860 seq_printf(m, "EC 0x%02x:", i);
5861 for (j = 0; j < 16; j++) {
5862 if (!acpi_ec_read(i + j, &v))
5863 break;
5864 if (v != ecdump_regs[i + j])
5865 seq_printf(m, " *%02x", v);
5866 else
5867 seq_printf(m, " %02x", v);
5868 ecdump_regs[i + j] = v;
5870 seq_putc(m, '\n');
5871 if (j != 16)
5872 break;
5875 /* These are way too dangerous to advertise openly... */
5876 #if 0
5877 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5878 " (<offset> is 00-ff, <value> is 00-ff)\n");
5879 seq_printf(m, "commands:\t0x<offset> <value> "
5880 " (<offset> is 00-ff, <value> is 0-255)\n");
5881 #endif
5882 return 0;
5885 static int ecdump_write(char *buf)
5887 char *cmd;
5888 int i, v;
5890 while ((cmd = next_cmd(&buf))) {
5891 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5892 /* i and v set */
5893 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5894 /* i and v set */
5895 } else
5896 return -EINVAL;
5897 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5898 if (!acpi_ec_write(i, v))
5899 return -EIO;
5900 } else
5901 return -EINVAL;
5904 return 0;
5907 static struct ibm_struct ecdump_driver_data = {
5908 .name = "ecdump",
5909 .read = ecdump_read,
5910 .write = ecdump_write,
5911 .flags.experimental = 1,
5914 /*************************************************************************
5915 * Backlight/brightness subdriver
5918 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5921 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5922 * CMOS NVRAM byte 0x5E, bits 0-3.
5924 * EC HBRV (0x31) has the following layout
5925 * Bit 7: unknown function
5926 * Bit 6: unknown function
5927 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5928 * Bit 4: must be set to zero to avoid problems
5929 * Bit 3-0: backlight brightness level
5931 * brightness_get_raw returns status data in the HBRV layout
5933 * WARNING: The X61 has been verified to use HBRV for something else, so
5934 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5935 * testing on the very early *60 Lenovo models...
5938 enum {
5939 TP_EC_BACKLIGHT = 0x31,
5941 /* TP_EC_BACKLIGHT bitmasks */
5942 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5943 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5944 TP_EC_BACKLIGHT_MAPSW = 0x20,
5947 enum tpacpi_brightness_access_mode {
5948 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5949 TPACPI_BRGHT_MODE_EC, /* EC control */
5950 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5951 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5952 TPACPI_BRGHT_MODE_MAX
5955 static struct backlight_device *ibm_backlight_device;
5957 static enum tpacpi_brightness_access_mode brightness_mode =
5958 TPACPI_BRGHT_MODE_MAX;
5960 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5962 static struct mutex brightness_mutex;
5964 /* NVRAM brightness access,
5965 * call with brightness_mutex held! */
5966 static unsigned int tpacpi_brightness_nvram_get(void)
5968 u8 lnvram;
5970 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5971 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5972 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5973 lnvram &= bright_maxlvl;
5975 return lnvram;
5978 static void tpacpi_brightness_checkpoint_nvram(void)
5980 u8 lec = 0;
5981 u8 b_nvram;
5983 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5984 return;
5986 vdbg_printk(TPACPI_DBG_BRGHT,
5987 "trying to checkpoint backlight level to NVRAM...\n");
5989 if (mutex_lock_killable(&brightness_mutex) < 0)
5990 return;
5992 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5993 goto unlock;
5994 lec &= TP_EC_BACKLIGHT_LVLMSK;
5995 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5997 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5998 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5999 /* NVRAM needs update */
6000 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6001 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6002 b_nvram |= lec;
6003 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6004 dbg_printk(TPACPI_DBG_BRGHT,
6005 "updated NVRAM backlight level to %u (0x%02x)\n",
6006 (unsigned int) lec, (unsigned int) b_nvram);
6007 } else
6008 vdbg_printk(TPACPI_DBG_BRGHT,
6009 "NVRAM backlight level already is %u (0x%02x)\n",
6010 (unsigned int) lec, (unsigned int) b_nvram);
6012 unlock:
6013 mutex_unlock(&brightness_mutex);
6017 /* call with brightness_mutex held! */
6018 static int tpacpi_brightness_get_raw(int *status)
6020 u8 lec = 0;
6022 switch (brightness_mode) {
6023 case TPACPI_BRGHT_MODE_UCMS_STEP:
6024 *status = tpacpi_brightness_nvram_get();
6025 return 0;
6026 case TPACPI_BRGHT_MODE_EC:
6027 case TPACPI_BRGHT_MODE_ECNVRAM:
6028 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6029 return -EIO;
6030 *status = lec;
6031 return 0;
6032 default:
6033 return -ENXIO;
6037 /* call with brightness_mutex held! */
6038 /* do NOT call with illegal backlight level value */
6039 static int tpacpi_brightness_set_ec(unsigned int value)
6041 u8 lec = 0;
6043 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6044 return -EIO;
6046 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6047 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6048 (value & TP_EC_BACKLIGHT_LVLMSK))))
6049 return -EIO;
6051 return 0;
6054 /* call with brightness_mutex held! */
6055 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6057 int cmos_cmd, inc;
6058 unsigned int current_value, i;
6060 current_value = tpacpi_brightness_nvram_get();
6062 if (value == current_value)
6063 return 0;
6065 cmos_cmd = (value > current_value) ?
6066 TP_CMOS_BRIGHTNESS_UP :
6067 TP_CMOS_BRIGHTNESS_DOWN;
6068 inc = (value > current_value) ? 1 : -1;
6070 for (i = current_value; i != value; i += inc)
6071 if (issue_thinkpad_cmos_command(cmos_cmd))
6072 return -EIO;
6074 return 0;
6077 /* May return EINTR which can always be mapped to ERESTARTSYS */
6078 static int brightness_set(unsigned int value)
6080 int res;
6082 if (value > bright_maxlvl || value < 0)
6083 return -EINVAL;
6085 vdbg_printk(TPACPI_DBG_BRGHT,
6086 "set backlight level to %d\n", value);
6088 res = mutex_lock_killable(&brightness_mutex);
6089 if (res < 0)
6090 return res;
6092 switch (brightness_mode) {
6093 case TPACPI_BRGHT_MODE_EC:
6094 case TPACPI_BRGHT_MODE_ECNVRAM:
6095 res = tpacpi_brightness_set_ec(value);
6096 break;
6097 case TPACPI_BRGHT_MODE_UCMS_STEP:
6098 res = tpacpi_brightness_set_ucmsstep(value);
6099 break;
6100 default:
6101 res = -ENXIO;
6104 mutex_unlock(&brightness_mutex);
6105 return res;
6108 /* sysfs backlight class ----------------------------------------------- */
6110 static int brightness_update_status(struct backlight_device *bd)
6112 unsigned int level =
6113 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6114 bd->props.power == FB_BLANK_UNBLANK) ?
6115 bd->props.brightness : 0;
6117 dbg_printk(TPACPI_DBG_BRGHT,
6118 "backlight: attempt to set level to %d\n",
6119 level);
6121 /* it is the backlight class's job (caller) to handle
6122 * EINTR and other errors properly */
6123 return brightness_set(level);
6126 static int brightness_get(struct backlight_device *bd)
6128 int status, res;
6130 res = mutex_lock_killable(&brightness_mutex);
6131 if (res < 0)
6132 return 0;
6134 res = tpacpi_brightness_get_raw(&status);
6136 mutex_unlock(&brightness_mutex);
6138 if (res < 0)
6139 return 0;
6141 return status & TP_EC_BACKLIGHT_LVLMSK;
6144 static void tpacpi_brightness_notify_change(void)
6146 backlight_force_update(ibm_backlight_device,
6147 BACKLIGHT_UPDATE_HOTKEY);
6150 static struct backlight_ops ibm_backlight_data = {
6151 .get_brightness = brightness_get,
6152 .update_status = brightness_update_status,
6155 /* --------------------------------------------------------------------- */
6157 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6159 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6160 union acpi_object *obj;
6161 int rc;
6163 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
6164 obj = (union acpi_object *)buffer.pointer;
6165 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6166 printk(TPACPI_ERR "Unknown _BCL data, "
6167 "please report this to %s\n", TPACPI_MAIL);
6168 rc = 0;
6169 } else {
6170 rc = obj->package.count;
6172 } else {
6173 return 0;
6176 kfree(buffer.pointer);
6177 return rc;
6180 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
6181 u32 lvl, void *context, void **rv)
6183 char name[ACPI_PATH_SEGMENT_LENGTH];
6184 struct acpi_buffer buffer = { sizeof(name), &name };
6186 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
6187 !strncmp("_BCL", name, sizeof(name) - 1)) {
6188 BUG_ON(!rv || !*rv);
6189 **(int **)rv = tpacpi_query_bcl_levels(handle);
6190 return AE_CTRL_TERMINATE;
6191 } else {
6192 return AE_OK;
6197 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6199 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6201 int status;
6202 int bcl_levels = 0;
6203 void *bcl_ptr = &bcl_levels;
6205 if (!vid_handle)
6206 TPACPI_ACPIHANDLE_INIT(vid);
6208 if (!vid_handle)
6209 return 0;
6212 * Search for a _BCL method, and execute it. This is safe on all
6213 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
6214 * BIOS in ACPI backlight control mode. We do NOT have to care
6215 * about calling the _BCL method in an enabled video device, any
6216 * will do for our purposes.
6219 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
6220 tpacpi_acpi_walk_find_bcl, NULL,
6221 &bcl_ptr);
6223 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
6224 tp_features.bright_acpimode = 1;
6225 return bcl_levels - 2;
6228 return 0;
6232 * These are only useful for models that have only one possibility
6233 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6234 * these quirks.
6236 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6237 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6238 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6240 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6241 /* Models with ATI GPUs known to require ECNVRAM mode */
6242 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6244 /* Models with ATI GPUs that can use ECNVRAM */
6245 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6246 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6247 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6248 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6250 /* Models with Intel Extreme Graphics 2 */
6251 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6252 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6253 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6255 /* Models with Intel GMA900 */
6256 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6257 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6258 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6262 * Returns < 0 for error, otherwise sets tp_features.bright_*
6263 * and bright_maxlvl.
6265 static void __init tpacpi_detect_brightness_capabilities(void)
6267 unsigned int b;
6269 vdbg_printk(TPACPI_DBG_INIT,
6270 "detecting firmware brightness interface capabilities\n");
6272 /* we could run a quirks check here (same table used by
6273 * brightness_init) if needed */
6276 * We always attempt to detect acpi support, so as to switch
6277 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6278 * going to publish a backlight interface
6280 b = tpacpi_check_std_acpi_brightness_support();
6281 switch (b) {
6282 case 16:
6283 bright_maxlvl = 15;
6284 printk(TPACPI_INFO
6285 "detected a 16-level brightness capable ThinkPad\n");
6286 break;
6287 case 8:
6288 case 0:
6289 bright_maxlvl = 7;
6290 printk(TPACPI_INFO
6291 "detected a 8-level brightness capable ThinkPad\n");
6292 break;
6293 default:
6294 printk(TPACPI_ERR
6295 "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 (tp_features.bright_acpimode) {
6321 if (acpi_video_backlight_support()) {
6322 if (brightness_enable > 1) {
6323 printk(TPACPI_NOTICE
6324 "Standard ACPI backlight interface "
6325 "available, not loading native one.\n");
6326 return 1;
6327 } else if (brightness_enable == 1) {
6328 printk(TPACPI_NOTICE
6329 "Backlight control force enabled, even if standard "
6330 "ACPI backlight interface is available\n");
6332 } else {
6333 if (brightness_enable > 1) {
6334 printk(TPACPI_NOTICE
6335 "Standard ACPI backlight interface not "
6336 "available, thinkpad_acpi native "
6337 "brightness control enabled\n");
6342 if (!brightness_enable) {
6343 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6344 "brightness support disabled by "
6345 "module parameter\n");
6346 return 1;
6350 * Check for module parameter bogosity, note that we
6351 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6352 * able to detect "unspecified"
6354 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6355 return -EINVAL;
6357 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6358 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6359 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6360 if (quirks & TPACPI_BRGHT_Q_EC)
6361 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6362 else
6363 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6365 dbg_printk(TPACPI_DBG_BRGHT,
6366 "driver auto-selected brightness_mode=%d\n",
6367 brightness_mode);
6370 /* Safety */
6371 if (!tpacpi_is_ibm() &&
6372 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6373 brightness_mode == TPACPI_BRGHT_MODE_EC))
6374 return -EINVAL;
6376 if (tpacpi_brightness_get_raw(&b) < 0)
6377 return 1;
6379 ibm_backlight_device = backlight_device_register(
6380 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6381 &ibm_backlight_data);
6382 if (IS_ERR(ibm_backlight_device)) {
6383 int rc = PTR_ERR(ibm_backlight_device);
6384 ibm_backlight_device = NULL;
6385 printk(TPACPI_ERR "Could not register backlight device\n");
6386 return rc;
6388 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6389 "brightness is supported\n");
6391 if (quirks & TPACPI_BRGHT_Q_ASK) {
6392 printk(TPACPI_NOTICE
6393 "brightness: will use unverified default: "
6394 "brightness_mode=%d\n", brightness_mode);
6395 printk(TPACPI_NOTICE
6396 "brightness: please report to %s whether it works well "
6397 "or not on your ThinkPad\n", TPACPI_MAIL);
6400 ibm_backlight_device->props.max_brightness = bright_maxlvl;
6401 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6403 /* Added by mistake in early 2007. Probably useless, but it could
6404 * be working around some unknown firmware problem where the value
6405 * read at startup doesn't match the real hardware state... so leave
6406 * it in place just in case */
6407 backlight_update_status(ibm_backlight_device);
6409 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6410 "brightness: registering brightness hotkeys "
6411 "as change notification\n");
6412 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6413 | TP_ACPI_HKEY_BRGHTUP_MASK
6414 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6415 return 0;
6418 static void brightness_suspend(pm_message_t state)
6420 tpacpi_brightness_checkpoint_nvram();
6423 static void brightness_shutdown(void)
6425 tpacpi_brightness_checkpoint_nvram();
6428 static void brightness_exit(void)
6430 if (ibm_backlight_device) {
6431 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6432 "calling backlight_device_unregister()\n");
6433 backlight_device_unregister(ibm_backlight_device);
6436 tpacpi_brightness_checkpoint_nvram();
6439 static int brightness_read(struct seq_file *m)
6441 int level;
6443 level = brightness_get(NULL);
6444 if (level < 0) {
6445 seq_printf(m, "level:\t\tunreadable\n");
6446 } else {
6447 seq_printf(m, "level:\t\t%d\n", level);
6448 seq_printf(m, "commands:\tup, down\n");
6449 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6450 bright_maxlvl);
6453 return 0;
6456 static int brightness_write(char *buf)
6458 int level;
6459 int rc;
6460 char *cmd;
6462 level = brightness_get(NULL);
6463 if (level < 0)
6464 return level;
6466 while ((cmd = next_cmd(&buf))) {
6467 if (strlencmp(cmd, "up") == 0) {
6468 if (level < bright_maxlvl)
6469 level++;
6470 } else if (strlencmp(cmd, "down") == 0) {
6471 if (level > 0)
6472 level--;
6473 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6474 level >= 0 && level <= bright_maxlvl) {
6475 /* new level set */
6476 } else
6477 return -EINVAL;
6480 tpacpi_disclose_usertask("procfs brightness",
6481 "set level to %d\n", level);
6484 * Now we know what the final level should be, so we try to set it.
6485 * Doing it this way makes the syscall restartable in case of EINTR
6487 rc = brightness_set(level);
6488 if (!rc && ibm_backlight_device)
6489 backlight_force_update(ibm_backlight_device,
6490 BACKLIGHT_UPDATE_SYSFS);
6491 return (rc == -EINTR)? -ERESTARTSYS : rc;
6494 static struct ibm_struct brightness_driver_data = {
6495 .name = "brightness",
6496 .read = brightness_read,
6497 .write = brightness_write,
6498 .exit = brightness_exit,
6499 .suspend = brightness_suspend,
6500 .shutdown = brightness_shutdown,
6503 /*************************************************************************
6504 * Volume subdriver
6508 * IBM ThinkPads have a simple volume controller with MUTE gating.
6509 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6511 * Since the *61 series (and probably also the later *60 series), Lenovo
6512 * ThinkPads only implement the MUTE gate.
6514 * EC register 0x30
6515 * Bit 6: MUTE (1 mutes sound)
6516 * Bit 3-0: Volume
6517 * Other bits should be zero as far as we know.
6519 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6520 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6521 * such as bit 7 which is used to detect repeated presses of MUTE,
6522 * and we leave them unchanged.
6525 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6527 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6528 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6529 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6531 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6532 static char *alsa_id = "ThinkPadEC";
6533 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6535 struct tpacpi_alsa_data {
6536 struct snd_card *card;
6537 struct snd_ctl_elem_id *ctl_mute_id;
6538 struct snd_ctl_elem_id *ctl_vol_id;
6541 static struct snd_card *alsa_card;
6543 enum {
6544 TP_EC_AUDIO = 0x30,
6546 /* TP_EC_AUDIO bits */
6547 TP_EC_AUDIO_MUTESW = 6,
6549 /* TP_EC_AUDIO bitmasks */
6550 TP_EC_AUDIO_LVL_MSK = 0x0F,
6551 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6553 /* Maximum volume */
6554 TP_EC_VOLUME_MAX = 14,
6557 enum tpacpi_volume_access_mode {
6558 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6559 TPACPI_VOL_MODE_EC, /* Pure EC control */
6560 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6561 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6562 TPACPI_VOL_MODE_MAX
6565 enum tpacpi_volume_capabilities {
6566 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6567 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6568 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6569 TPACPI_VOL_CAP_MAX
6572 static enum tpacpi_volume_access_mode volume_mode =
6573 TPACPI_VOL_MODE_MAX;
6575 static enum tpacpi_volume_capabilities volume_capabilities;
6576 static int volume_control_allowed;
6579 * Used to syncronize writers to TP_EC_AUDIO and
6580 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6582 static struct mutex volume_mutex;
6584 static void tpacpi_volume_checkpoint_nvram(void)
6586 u8 lec = 0;
6587 u8 b_nvram;
6588 u8 ec_mask;
6590 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6591 return;
6592 if (!volume_control_allowed)
6593 return;
6595 vdbg_printk(TPACPI_DBG_MIXER,
6596 "trying to checkpoint mixer state to NVRAM...\n");
6598 if (tp_features.mixer_no_level_control)
6599 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6600 else
6601 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6603 if (mutex_lock_killable(&volume_mutex) < 0)
6604 return;
6606 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6607 goto unlock;
6608 lec &= ec_mask;
6609 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6611 if (lec != (b_nvram & ec_mask)) {
6612 /* NVRAM needs update */
6613 b_nvram &= ~ec_mask;
6614 b_nvram |= lec;
6615 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6616 dbg_printk(TPACPI_DBG_MIXER,
6617 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6618 (unsigned int) lec, (unsigned int) b_nvram);
6619 } else {
6620 vdbg_printk(TPACPI_DBG_MIXER,
6621 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6622 (unsigned int) lec, (unsigned int) b_nvram);
6625 unlock:
6626 mutex_unlock(&volume_mutex);
6629 static int volume_get_status_ec(u8 *status)
6631 u8 s;
6633 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6634 return -EIO;
6636 *status = s;
6638 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6640 return 0;
6643 static int volume_get_status(u8 *status)
6645 return volume_get_status_ec(status);
6648 static int volume_set_status_ec(const u8 status)
6650 if (!acpi_ec_write(TP_EC_AUDIO, status))
6651 return -EIO;
6653 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6655 return 0;
6658 static int volume_set_status(const u8 status)
6660 return volume_set_status_ec(status);
6663 /* returns < 0 on error, 0 on no change, 1 on change */
6664 static int __volume_set_mute_ec(const bool mute)
6666 int rc;
6667 u8 s, n;
6669 if (mutex_lock_killable(&volume_mutex) < 0)
6670 return -EINTR;
6672 rc = volume_get_status_ec(&s);
6673 if (rc)
6674 goto unlock;
6676 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6677 s & ~TP_EC_AUDIO_MUTESW_MSK;
6679 if (n != s) {
6680 rc = volume_set_status_ec(n);
6681 if (!rc)
6682 rc = 1;
6685 unlock:
6686 mutex_unlock(&volume_mutex);
6687 return rc;
6690 static int volume_alsa_set_mute(const bool mute)
6692 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6693 (mute) ? "" : "un");
6694 return __volume_set_mute_ec(mute);
6697 static int volume_set_mute(const bool mute)
6699 int rc;
6701 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6702 (mute) ? "" : "un");
6704 rc = __volume_set_mute_ec(mute);
6705 return (rc < 0) ? rc : 0;
6708 /* returns < 0 on error, 0 on no change, 1 on change */
6709 static int __volume_set_volume_ec(const u8 vol)
6711 int rc;
6712 u8 s, n;
6714 if (vol > TP_EC_VOLUME_MAX)
6715 return -EINVAL;
6717 if (mutex_lock_killable(&volume_mutex) < 0)
6718 return -EINTR;
6720 rc = volume_get_status_ec(&s);
6721 if (rc)
6722 goto unlock;
6724 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6726 if (n != s) {
6727 rc = volume_set_status_ec(n);
6728 if (!rc)
6729 rc = 1;
6732 unlock:
6733 mutex_unlock(&volume_mutex);
6734 return rc;
6737 static int volume_alsa_set_volume(const u8 vol)
6739 dbg_printk(TPACPI_DBG_MIXER,
6740 "ALSA: trying to set volume level to %hu\n", vol);
6741 return __volume_set_volume_ec(vol);
6744 static void volume_alsa_notify_change(void)
6746 struct tpacpi_alsa_data *d;
6748 if (alsa_card && alsa_card->private_data) {
6749 d = alsa_card->private_data;
6750 if (d->ctl_mute_id)
6751 snd_ctl_notify(alsa_card,
6752 SNDRV_CTL_EVENT_MASK_VALUE,
6753 d->ctl_mute_id);
6754 if (d->ctl_vol_id)
6755 snd_ctl_notify(alsa_card,
6756 SNDRV_CTL_EVENT_MASK_VALUE,
6757 d->ctl_vol_id);
6761 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6762 struct snd_ctl_elem_info *uinfo)
6764 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6765 uinfo->count = 1;
6766 uinfo->value.integer.min = 0;
6767 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6768 return 0;
6771 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6772 struct snd_ctl_elem_value *ucontrol)
6774 u8 s;
6775 int rc;
6777 rc = volume_get_status(&s);
6778 if (rc < 0)
6779 return rc;
6781 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6782 return 0;
6785 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6786 struct snd_ctl_elem_value *ucontrol)
6788 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6789 ucontrol->value.integer.value[0]);
6790 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6793 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6795 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6796 struct snd_ctl_elem_value *ucontrol)
6798 u8 s;
6799 int rc;
6801 rc = volume_get_status(&s);
6802 if (rc < 0)
6803 return rc;
6805 ucontrol->value.integer.value[0] =
6806 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6807 return 0;
6810 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6811 struct snd_ctl_elem_value *ucontrol)
6813 tpacpi_disclose_usertask("ALSA", "%smute\n",
6814 ucontrol->value.integer.value[0] ?
6815 "un" : "");
6816 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6819 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6820 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6821 .name = "Console Playback Volume",
6822 .index = 0,
6823 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6824 .info = volume_alsa_vol_info,
6825 .get = volume_alsa_vol_get,
6828 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6829 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6830 .name = "Console Playback Switch",
6831 .index = 0,
6832 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6833 .info = volume_alsa_mute_info,
6834 .get = volume_alsa_mute_get,
6837 static void volume_suspend(pm_message_t state)
6839 tpacpi_volume_checkpoint_nvram();
6842 static void volume_resume(void)
6844 volume_alsa_notify_change();
6847 static void volume_shutdown(void)
6849 tpacpi_volume_checkpoint_nvram();
6852 static void volume_exit(void)
6854 if (alsa_card) {
6855 snd_card_free(alsa_card);
6856 alsa_card = NULL;
6859 tpacpi_volume_checkpoint_nvram();
6862 static int __init volume_create_alsa_mixer(void)
6864 struct snd_card *card;
6865 struct tpacpi_alsa_data *data;
6866 struct snd_kcontrol *ctl_vol;
6867 struct snd_kcontrol *ctl_mute;
6868 int rc;
6870 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6871 sizeof(struct tpacpi_alsa_data), &card);
6872 if (rc < 0 || !card) {
6873 printk(TPACPI_ERR
6874 "Failed to create ALSA card structures: %d\n", rc);
6875 return 1;
6878 BUG_ON(!card->private_data);
6879 data = card->private_data;
6880 data->card = card;
6882 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6883 sizeof(card->driver));
6884 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6885 sizeof(card->shortname));
6886 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6887 (thinkpad_id.ec_version_str) ?
6888 thinkpad_id.ec_version_str : "(unknown)");
6889 snprintf(card->longname, sizeof(card->longname),
6890 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6891 (thinkpad_id.ec_version_str) ?
6892 thinkpad_id.ec_version_str : "unknown");
6894 if (volume_control_allowed) {
6895 volume_alsa_control_vol.put = volume_alsa_vol_put;
6896 volume_alsa_control_vol.access =
6897 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6899 volume_alsa_control_mute.put = volume_alsa_mute_put;
6900 volume_alsa_control_mute.access =
6901 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6904 if (!tp_features.mixer_no_level_control) {
6905 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6906 rc = snd_ctl_add(card, ctl_vol);
6907 if (rc < 0) {
6908 printk(TPACPI_ERR
6909 "Failed to create ALSA volume control: %d\n",
6910 rc);
6911 goto err_exit;
6913 data->ctl_vol_id = &ctl_vol->id;
6916 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6917 rc = snd_ctl_add(card, ctl_mute);
6918 if (rc < 0) {
6919 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6920 rc);
6921 goto err_exit;
6923 data->ctl_mute_id = &ctl_mute->id;
6925 snd_card_set_dev(card, &tpacpi_pdev->dev);
6926 rc = snd_card_register(card);
6927 if (rc < 0) {
6928 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6929 goto err_exit;
6932 alsa_card = card;
6933 return 0;
6935 err_exit:
6936 snd_card_free(card);
6937 return 1;
6940 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6941 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6943 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6944 /* Whitelist volume level on all IBM by default */
6945 { .vendor = PCI_VENDOR_ID_IBM,
6946 .bios = TPACPI_MATCH_ANY,
6947 .ec = TPACPI_MATCH_ANY,
6948 .quirks = TPACPI_VOL_Q_LEVEL },
6950 /* Lenovo models with volume control (needs confirmation) */
6951 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6952 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6953 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6954 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6955 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6956 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6957 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6959 /* Whitelist mute-only on all Lenovo by default */
6960 { .vendor = PCI_VENDOR_ID_LENOVO,
6961 .bios = TPACPI_MATCH_ANY,
6962 .ec = TPACPI_MATCH_ANY,
6963 .quirks = TPACPI_VOL_Q_MUTEONLY }
6966 static int __init volume_init(struct ibm_init_struct *iibm)
6968 unsigned long quirks;
6969 int rc;
6971 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6973 mutex_init(&volume_mutex);
6976 * Check for module parameter bogosity, note that we
6977 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6978 * able to detect "unspecified"
6980 if (volume_mode > TPACPI_VOL_MODE_MAX)
6981 return -EINVAL;
6983 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6984 printk(TPACPI_ERR
6985 "UCMS step volume mode not implemented, "
6986 "please contact %s\n", TPACPI_MAIL);
6987 return 1;
6990 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6991 return -EINVAL;
6994 * The ALSA mixer is our primary interface.
6995 * When disabled, don't install the subdriver at all
6997 if (!alsa_enable) {
6998 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6999 "ALSA mixer disabled by parameter, "
7000 "not loading volume subdriver...\n");
7001 return 1;
7004 quirks = tpacpi_check_quirks(volume_quirk_table,
7005 ARRAY_SIZE(volume_quirk_table));
7007 switch (volume_capabilities) {
7008 case TPACPI_VOL_CAP_AUTO:
7009 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7010 tp_features.mixer_no_level_control = 1;
7011 else if (quirks & TPACPI_VOL_Q_LEVEL)
7012 tp_features.mixer_no_level_control = 0;
7013 else
7014 return 1; /* no mixer */
7015 break;
7016 case TPACPI_VOL_CAP_VOLMUTE:
7017 tp_features.mixer_no_level_control = 0;
7018 break;
7019 case TPACPI_VOL_CAP_MUTEONLY:
7020 tp_features.mixer_no_level_control = 1;
7021 break;
7022 default:
7023 return 1;
7026 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7027 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7028 "using user-supplied volume_capabilities=%d\n",
7029 volume_capabilities);
7031 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7032 volume_mode == TPACPI_VOL_MODE_MAX) {
7033 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7035 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7036 "driver auto-selected volume_mode=%d\n",
7037 volume_mode);
7038 } else {
7039 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7040 "using user-supplied volume_mode=%d\n",
7041 volume_mode);
7044 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7045 "mute is supported, volume control is %s\n",
7046 str_supported(!tp_features.mixer_no_level_control));
7048 rc = volume_create_alsa_mixer();
7049 if (rc) {
7050 printk(TPACPI_ERR
7051 "Could not create the ALSA mixer interface\n");
7052 return rc;
7055 printk(TPACPI_INFO
7056 "Console audio control enabled, mode: %s\n",
7057 (volume_control_allowed) ?
7058 "override (read/write)" :
7059 "monitor (read only)");
7061 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7062 "registering volume hotkeys as change notification\n");
7063 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7064 | TP_ACPI_HKEY_VOLUP_MASK
7065 | TP_ACPI_HKEY_VOLDWN_MASK
7066 | TP_ACPI_HKEY_MUTE_MASK);
7068 return 0;
7071 static int volume_read(struct seq_file *m)
7073 u8 status;
7075 if (volume_get_status(&status) < 0) {
7076 seq_printf(m, "level:\t\tunreadable\n");
7077 } else {
7078 if (tp_features.mixer_no_level_control)
7079 seq_printf(m, "level:\t\tunsupported\n");
7080 else
7081 seq_printf(m, "level:\t\t%d\n",
7082 status & TP_EC_AUDIO_LVL_MSK);
7084 seq_printf(m, "mute:\t\t%s\n",
7085 onoff(status, TP_EC_AUDIO_MUTESW));
7087 if (volume_control_allowed) {
7088 seq_printf(m, "commands:\tunmute, mute\n");
7089 if (!tp_features.mixer_no_level_control) {
7090 seq_printf(m,
7091 "commands:\tup, down\n");
7092 seq_printf(m,
7093 "commands:\tlevel <level>"
7094 " (<level> is 0-%d)\n",
7095 TP_EC_VOLUME_MAX);
7100 return 0;
7103 static int volume_write(char *buf)
7105 u8 s;
7106 u8 new_level, new_mute;
7107 int l;
7108 char *cmd;
7109 int rc;
7112 * We do allow volume control at driver startup, so that the
7113 * user can set initial state through the volume=... parameter hack.
7115 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7116 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7117 tp_warned.volume_ctrl_forbidden = 1;
7118 printk(TPACPI_NOTICE
7119 "Console audio control in monitor mode, "
7120 "changes are not allowed.\n");
7121 printk(TPACPI_NOTICE
7122 "Use the volume_control=1 module parameter "
7123 "to enable volume control\n");
7125 return -EPERM;
7128 rc = volume_get_status(&s);
7129 if (rc < 0)
7130 return rc;
7132 new_level = s & TP_EC_AUDIO_LVL_MSK;
7133 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7135 while ((cmd = next_cmd(&buf))) {
7136 if (!tp_features.mixer_no_level_control) {
7137 if (strlencmp(cmd, "up") == 0) {
7138 if (new_mute)
7139 new_mute = 0;
7140 else if (new_level < TP_EC_VOLUME_MAX)
7141 new_level++;
7142 continue;
7143 } else if (strlencmp(cmd, "down") == 0) {
7144 if (new_mute)
7145 new_mute = 0;
7146 else if (new_level > 0)
7147 new_level--;
7148 continue;
7149 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7150 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7151 new_level = l;
7152 continue;
7155 if (strlencmp(cmd, "mute") == 0)
7156 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7157 else if (strlencmp(cmd, "unmute") == 0)
7158 new_mute = 0;
7159 else
7160 return -EINVAL;
7163 if (tp_features.mixer_no_level_control) {
7164 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7165 new_mute ? "" : "un");
7166 rc = volume_set_mute(!!new_mute);
7167 } else {
7168 tpacpi_disclose_usertask("procfs volume",
7169 "%smute and set level to %d\n",
7170 new_mute ? "" : "un", new_level);
7171 rc = volume_set_status(new_mute | new_level);
7173 volume_alsa_notify_change();
7175 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7178 static struct ibm_struct volume_driver_data = {
7179 .name = "volume",
7180 .read = volume_read,
7181 .write = volume_write,
7182 .exit = volume_exit,
7183 .suspend = volume_suspend,
7184 .resume = volume_resume,
7185 .shutdown = volume_shutdown,
7188 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7190 #define alsa_card NULL
7192 static void inline volume_alsa_notify_change(void)
7196 static int __init volume_init(struct ibm_init_struct *iibm)
7198 printk(TPACPI_INFO
7199 "volume: disabled as there is no ALSA support in this kernel\n");
7201 return 1;
7204 static struct ibm_struct volume_driver_data = {
7205 .name = "volume",
7208 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7210 /*************************************************************************
7211 * Fan subdriver
7215 * FAN ACCESS MODES
7217 * TPACPI_FAN_RD_ACPI_GFAN:
7218 * ACPI GFAN method: returns fan level
7220 * see TPACPI_FAN_WR_ACPI_SFAN
7221 * EC 0x2f (HFSP) not available if GFAN exists
7223 * TPACPI_FAN_WR_ACPI_SFAN:
7224 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7226 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7227 * it for writing.
7229 * TPACPI_FAN_WR_TPEC:
7230 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7231 * Supported on almost all ThinkPads
7233 * Fan speed changes of any sort (including those caused by the
7234 * disengaged mode) are usually done slowly by the firmware as the
7235 * maximum amount of fan duty cycle change per second seems to be
7236 * limited.
7238 * Reading is not available if GFAN exists.
7239 * Writing is not available if SFAN exists.
7241 * Bits
7242 * 7 automatic mode engaged;
7243 * (default operation mode of the ThinkPad)
7244 * fan level is ignored in this mode.
7245 * 6 full speed mode (takes precedence over bit 7);
7246 * not available on all thinkpads. May disable
7247 * the tachometer while the fan controller ramps up
7248 * the speed (which can take up to a few *minutes*).
7249 * Speeds up fan to 100% duty-cycle, which is far above
7250 * the standard RPM levels. It is not impossible that
7251 * it could cause hardware damage.
7252 * 5-3 unused in some models. Extra bits for fan level
7253 * in others, but still useless as all values above
7254 * 7 map to the same speed as level 7 in these models.
7255 * 2-0 fan level (0..7 usually)
7256 * 0x00 = stop
7257 * 0x07 = max (set when temperatures critical)
7258 * Some ThinkPads may have other levels, see
7259 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7261 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7262 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7263 * does so, its initial value is meaningless (0x07).
7265 * For firmware bugs, refer to:
7266 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7268 * ----
7270 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7271 * Main fan tachometer reading (in RPM)
7273 * This register is present on all ThinkPads with a new-style EC, and
7274 * it is known not to be present on the A21m/e, and T22, as there is
7275 * something else in offset 0x84 according to the ACPI DSDT. Other
7276 * ThinkPads from this same time period (and earlier) probably lack the
7277 * tachometer as well.
7279 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7280 * was never fixed by IBM to report the EC firmware version string
7281 * probably support the tachometer (like the early X models), so
7282 * detecting it is quite hard. We need more data to know for sure.
7284 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7285 * might result.
7287 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7288 * mode.
7290 * For firmware bugs, refer to:
7291 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7293 * ----
7295 * ThinkPad EC register 0x31 bit 0 (only on select models)
7297 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7298 * show the speed of the main fan. When bit 0 of EC register 0x31
7299 * is one, the tachometer registers show the speed of the auxiliary
7300 * fan.
7302 * Fan control seems to affect both fans, regardless of the state
7303 * of this bit.
7305 * So far, only the firmware for the X60/X61 non-tablet versions
7306 * seem to support this (firmware TP-7M).
7308 * TPACPI_FAN_WR_ACPI_FANS:
7309 * ThinkPad X31, X40, X41. Not available in the X60.
7311 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7312 * high speed. ACPI DSDT seems to map these three speeds to levels
7313 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7314 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7316 * The speeds are stored on handles
7317 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7319 * There are three default speed sets, accessible as handles:
7320 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7322 * ACPI DSDT switches which set is in use depending on various
7323 * factors.
7325 * TPACPI_FAN_WR_TPEC is also available and should be used to
7326 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7327 * but the ACPI tables just mention level 7.
7330 enum { /* Fan control constants */
7331 fan_status_offset = 0x2f, /* EC register 0x2f */
7332 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7333 * 0x84 must be read before 0x85 */
7334 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7335 bit 0 selects which fan is active */
7337 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7338 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7340 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7343 enum fan_status_access_mode {
7344 TPACPI_FAN_NONE = 0, /* No fan status or control */
7345 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7346 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7349 enum fan_control_access_mode {
7350 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7351 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7352 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7353 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7356 enum fan_control_commands {
7357 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7358 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7359 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7360 * and also watchdog cmd */
7363 static int fan_control_allowed;
7365 static enum fan_status_access_mode fan_status_access_mode;
7366 static enum fan_control_access_mode fan_control_access_mode;
7367 static enum fan_control_commands fan_control_commands;
7369 static u8 fan_control_initial_status;
7370 static u8 fan_control_desired_level;
7371 static u8 fan_control_resume_level;
7372 static int fan_watchdog_maxinterval;
7374 static struct mutex fan_mutex;
7376 static void fan_watchdog_fire(struct work_struct *ignored);
7377 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7379 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7380 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7381 "\\FSPD", /* 600e/x, 770e, 770x */
7382 ); /* all others */
7383 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7384 "JFNS", /* 770x-JL */
7385 ); /* all others */
7388 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7389 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7390 * be in auto mode (0x80).
7392 * This is corrected by any write to HFSP either by the driver, or
7393 * by the firmware.
7395 * We assume 0x07 really means auto mode while this quirk is active,
7396 * as this is far more likely than the ThinkPad being in level 7,
7397 * which is only used by the firmware during thermal emergencies.
7399 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7400 * TP-70 (T43, R52), which are known to be buggy.
7403 static void fan_quirk1_setup(void)
7405 if (fan_control_initial_status == 0x07) {
7406 printk(TPACPI_NOTICE
7407 "fan_init: initial fan status is unknown, "
7408 "assuming it is in auto mode\n");
7409 tp_features.fan_ctrl_status_undef = 1;
7413 static void fan_quirk1_handle(u8 *fan_status)
7415 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7416 if (*fan_status != fan_control_initial_status) {
7417 /* something changed the HFSP regisnter since
7418 * driver init time, so it is not undefined
7419 * anymore */
7420 tp_features.fan_ctrl_status_undef = 0;
7421 } else {
7422 /* Return most likely status. In fact, it
7423 * might be the only possible status */
7424 *fan_status = TP_EC_FAN_AUTO;
7429 /* Select main fan on X60/X61, NOOP on others */
7430 static bool fan_select_fan1(void)
7432 if (tp_features.second_fan) {
7433 u8 val;
7435 if (ec_read(fan_select_offset, &val) < 0)
7436 return false;
7437 val &= 0xFEU;
7438 if (ec_write(fan_select_offset, val) < 0)
7439 return false;
7441 return true;
7444 /* Select secondary fan on X60/X61 */
7445 static bool fan_select_fan2(void)
7447 u8 val;
7449 if (!tp_features.second_fan)
7450 return false;
7452 if (ec_read(fan_select_offset, &val) < 0)
7453 return false;
7454 val |= 0x01U;
7455 if (ec_write(fan_select_offset, val) < 0)
7456 return false;
7458 return true;
7462 * Call with fan_mutex held
7464 static void fan_update_desired_level(u8 status)
7466 if ((status &
7467 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7468 if (status > 7)
7469 fan_control_desired_level = 7;
7470 else
7471 fan_control_desired_level = status;
7475 static int fan_get_status(u8 *status)
7477 u8 s;
7479 /* TODO:
7480 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7482 switch (fan_status_access_mode) {
7483 case TPACPI_FAN_RD_ACPI_GFAN:
7484 /* 570, 600e/x, 770e, 770x */
7486 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7487 return -EIO;
7489 if (likely(status))
7490 *status = s & 0x07;
7492 break;
7494 case TPACPI_FAN_RD_TPEC:
7495 /* all except 570, 600e/x, 770e, 770x */
7496 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7497 return -EIO;
7499 if (likely(status)) {
7500 *status = s;
7501 fan_quirk1_handle(status);
7504 break;
7506 default:
7507 return -ENXIO;
7510 return 0;
7513 static int fan_get_status_safe(u8 *status)
7515 int rc;
7516 u8 s;
7518 if (mutex_lock_killable(&fan_mutex))
7519 return -ERESTARTSYS;
7520 rc = fan_get_status(&s);
7521 if (!rc)
7522 fan_update_desired_level(s);
7523 mutex_unlock(&fan_mutex);
7525 if (status)
7526 *status = s;
7528 return rc;
7531 static int fan_get_speed(unsigned int *speed)
7533 u8 hi, lo;
7535 switch (fan_status_access_mode) {
7536 case TPACPI_FAN_RD_TPEC:
7537 /* all except 570, 600e/x, 770e, 770x */
7538 if (unlikely(!fan_select_fan1()))
7539 return -EIO;
7540 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7541 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7542 return -EIO;
7544 if (likely(speed))
7545 *speed = (hi << 8) | lo;
7547 break;
7549 default:
7550 return -ENXIO;
7553 return 0;
7556 static int fan2_get_speed(unsigned int *speed)
7558 u8 hi, lo;
7559 bool rc;
7561 switch (fan_status_access_mode) {
7562 case TPACPI_FAN_RD_TPEC:
7563 /* all except 570, 600e/x, 770e, 770x */
7564 if (unlikely(!fan_select_fan2()))
7565 return -EIO;
7566 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7567 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7568 fan_select_fan1(); /* play it safe */
7569 if (rc)
7570 return -EIO;
7572 if (likely(speed))
7573 *speed = (hi << 8) | lo;
7575 break;
7577 default:
7578 return -ENXIO;
7581 return 0;
7584 static int fan_set_level(int level)
7586 if (!fan_control_allowed)
7587 return -EPERM;
7589 switch (fan_control_access_mode) {
7590 case TPACPI_FAN_WR_ACPI_SFAN:
7591 if (level >= 0 && level <= 7) {
7592 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7593 return -EIO;
7594 } else
7595 return -EINVAL;
7596 break;
7598 case TPACPI_FAN_WR_ACPI_FANS:
7599 case TPACPI_FAN_WR_TPEC:
7600 if (!(level & TP_EC_FAN_AUTO) &&
7601 !(level & TP_EC_FAN_FULLSPEED) &&
7602 ((level < 0) || (level > 7)))
7603 return -EINVAL;
7605 /* safety net should the EC not support AUTO
7606 * or FULLSPEED mode bits and just ignore them */
7607 if (level & TP_EC_FAN_FULLSPEED)
7608 level |= 7; /* safety min speed 7 */
7609 else if (level & TP_EC_FAN_AUTO)
7610 level |= 4; /* safety min speed 4 */
7612 if (!acpi_ec_write(fan_status_offset, level))
7613 return -EIO;
7614 else
7615 tp_features.fan_ctrl_status_undef = 0;
7616 break;
7618 default:
7619 return -ENXIO;
7622 vdbg_printk(TPACPI_DBG_FAN,
7623 "fan control: set fan control register to 0x%02x\n", level);
7624 return 0;
7627 static int fan_set_level_safe(int level)
7629 int rc;
7631 if (!fan_control_allowed)
7632 return -EPERM;
7634 if (mutex_lock_killable(&fan_mutex))
7635 return -ERESTARTSYS;
7637 if (level == TPACPI_FAN_LAST_LEVEL)
7638 level = fan_control_desired_level;
7640 rc = fan_set_level(level);
7641 if (!rc)
7642 fan_update_desired_level(level);
7644 mutex_unlock(&fan_mutex);
7645 return rc;
7648 static int fan_set_enable(void)
7650 u8 s;
7651 int rc;
7653 if (!fan_control_allowed)
7654 return -EPERM;
7656 if (mutex_lock_killable(&fan_mutex))
7657 return -ERESTARTSYS;
7659 switch (fan_control_access_mode) {
7660 case TPACPI_FAN_WR_ACPI_FANS:
7661 case TPACPI_FAN_WR_TPEC:
7662 rc = fan_get_status(&s);
7663 if (rc < 0)
7664 break;
7666 /* Don't go out of emergency fan mode */
7667 if (s != 7) {
7668 s &= 0x07;
7669 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7672 if (!acpi_ec_write(fan_status_offset, s))
7673 rc = -EIO;
7674 else {
7675 tp_features.fan_ctrl_status_undef = 0;
7676 rc = 0;
7678 break;
7680 case TPACPI_FAN_WR_ACPI_SFAN:
7681 rc = fan_get_status(&s);
7682 if (rc < 0)
7683 break;
7685 s &= 0x07;
7687 /* Set fan to at least level 4 */
7688 s |= 4;
7690 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7691 rc = -EIO;
7692 else
7693 rc = 0;
7694 break;
7696 default:
7697 rc = -ENXIO;
7700 mutex_unlock(&fan_mutex);
7702 if (!rc)
7703 vdbg_printk(TPACPI_DBG_FAN,
7704 "fan control: set fan control register to 0x%02x\n",
7706 return rc;
7709 static int fan_set_disable(void)
7711 int rc;
7713 if (!fan_control_allowed)
7714 return -EPERM;
7716 if (mutex_lock_killable(&fan_mutex))
7717 return -ERESTARTSYS;
7719 rc = 0;
7720 switch (fan_control_access_mode) {
7721 case TPACPI_FAN_WR_ACPI_FANS:
7722 case TPACPI_FAN_WR_TPEC:
7723 if (!acpi_ec_write(fan_status_offset, 0x00))
7724 rc = -EIO;
7725 else {
7726 fan_control_desired_level = 0;
7727 tp_features.fan_ctrl_status_undef = 0;
7729 break;
7731 case TPACPI_FAN_WR_ACPI_SFAN:
7732 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7733 rc = -EIO;
7734 else
7735 fan_control_desired_level = 0;
7736 break;
7738 default:
7739 rc = -ENXIO;
7742 if (!rc)
7743 vdbg_printk(TPACPI_DBG_FAN,
7744 "fan control: set fan control register to 0\n");
7746 mutex_unlock(&fan_mutex);
7747 return rc;
7750 static int fan_set_speed(int speed)
7752 int rc;
7754 if (!fan_control_allowed)
7755 return -EPERM;
7757 if (mutex_lock_killable(&fan_mutex))
7758 return -ERESTARTSYS;
7760 rc = 0;
7761 switch (fan_control_access_mode) {
7762 case TPACPI_FAN_WR_ACPI_FANS:
7763 if (speed >= 0 && speed <= 65535) {
7764 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7765 speed, speed, speed))
7766 rc = -EIO;
7767 } else
7768 rc = -EINVAL;
7769 break;
7771 default:
7772 rc = -ENXIO;
7775 mutex_unlock(&fan_mutex);
7776 return rc;
7779 static void fan_watchdog_reset(void)
7781 static int fan_watchdog_active;
7783 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7784 return;
7786 if (fan_watchdog_active)
7787 cancel_delayed_work(&fan_watchdog_task);
7789 if (fan_watchdog_maxinterval > 0 &&
7790 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7791 fan_watchdog_active = 1;
7792 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7793 msecs_to_jiffies(fan_watchdog_maxinterval
7794 * 1000))) {
7795 printk(TPACPI_ERR
7796 "failed to queue the fan watchdog, "
7797 "watchdog will not trigger\n");
7799 } else
7800 fan_watchdog_active = 0;
7803 static void fan_watchdog_fire(struct work_struct *ignored)
7805 int rc;
7807 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7808 return;
7810 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7811 rc = fan_set_enable();
7812 if (rc < 0) {
7813 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7814 "will try again later...\n", -rc);
7815 /* reschedule for later */
7816 fan_watchdog_reset();
7821 * SYSFS fan layout: hwmon compatible (device)
7823 * pwm*_enable:
7824 * 0: "disengaged" mode
7825 * 1: manual mode
7826 * 2: native EC "auto" mode (recommended, hardware default)
7828 * pwm*: set speed in manual mode, ignored otherwise.
7829 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7830 * interpolation.
7832 * fan*_input: tachometer reading, RPM
7835 * SYSFS fan layout: extensions
7837 * fan_watchdog (driver):
7838 * fan watchdog interval in seconds, 0 disables (default), max 120
7841 /* sysfs fan pwm1_enable ----------------------------------------------- */
7842 static ssize_t fan_pwm1_enable_show(struct device *dev,
7843 struct device_attribute *attr,
7844 char *buf)
7846 int res, mode;
7847 u8 status;
7849 res = fan_get_status_safe(&status);
7850 if (res)
7851 return res;
7853 if (status & TP_EC_FAN_FULLSPEED) {
7854 mode = 0;
7855 } else if (status & TP_EC_FAN_AUTO) {
7856 mode = 2;
7857 } else
7858 mode = 1;
7860 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7863 static ssize_t fan_pwm1_enable_store(struct device *dev,
7864 struct device_attribute *attr,
7865 const char *buf, size_t count)
7867 unsigned long t;
7868 int res, level;
7870 if (parse_strtoul(buf, 2, &t))
7871 return -EINVAL;
7873 tpacpi_disclose_usertask("hwmon pwm1_enable",
7874 "set fan mode to %lu\n", t);
7876 switch (t) {
7877 case 0:
7878 level = TP_EC_FAN_FULLSPEED;
7879 break;
7880 case 1:
7881 level = TPACPI_FAN_LAST_LEVEL;
7882 break;
7883 case 2:
7884 level = TP_EC_FAN_AUTO;
7885 break;
7886 case 3:
7887 /* reserved for software-controlled auto mode */
7888 return -ENOSYS;
7889 default:
7890 return -EINVAL;
7893 res = fan_set_level_safe(level);
7894 if (res == -ENXIO)
7895 return -EINVAL;
7896 else if (res < 0)
7897 return res;
7899 fan_watchdog_reset();
7901 return count;
7904 static struct device_attribute dev_attr_fan_pwm1_enable =
7905 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7906 fan_pwm1_enable_show, fan_pwm1_enable_store);
7908 /* sysfs fan pwm1 ------------------------------------------------------ */
7909 static ssize_t fan_pwm1_show(struct device *dev,
7910 struct device_attribute *attr,
7911 char *buf)
7913 int res;
7914 u8 status;
7916 res = fan_get_status_safe(&status);
7917 if (res)
7918 return res;
7920 if ((status &
7921 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7922 status = fan_control_desired_level;
7924 if (status > 7)
7925 status = 7;
7927 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7930 static ssize_t fan_pwm1_store(struct device *dev,
7931 struct device_attribute *attr,
7932 const char *buf, size_t count)
7934 unsigned long s;
7935 int rc;
7936 u8 status, newlevel;
7938 if (parse_strtoul(buf, 255, &s))
7939 return -EINVAL;
7941 tpacpi_disclose_usertask("hwmon pwm1",
7942 "set fan speed to %lu\n", s);
7944 /* scale down from 0-255 to 0-7 */
7945 newlevel = (s >> 5) & 0x07;
7947 if (mutex_lock_killable(&fan_mutex))
7948 return -ERESTARTSYS;
7950 rc = fan_get_status(&status);
7951 if (!rc && (status &
7952 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7953 rc = fan_set_level(newlevel);
7954 if (rc == -ENXIO)
7955 rc = -EINVAL;
7956 else if (!rc) {
7957 fan_update_desired_level(newlevel);
7958 fan_watchdog_reset();
7962 mutex_unlock(&fan_mutex);
7963 return (rc)? rc : count;
7966 static struct device_attribute dev_attr_fan_pwm1 =
7967 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7968 fan_pwm1_show, fan_pwm1_store);
7970 /* sysfs fan fan1_input ------------------------------------------------ */
7971 static ssize_t fan_fan1_input_show(struct device *dev,
7972 struct device_attribute *attr,
7973 char *buf)
7975 int res;
7976 unsigned int speed;
7978 res = fan_get_speed(&speed);
7979 if (res < 0)
7980 return res;
7982 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7985 static struct device_attribute dev_attr_fan_fan1_input =
7986 __ATTR(fan1_input, S_IRUGO,
7987 fan_fan1_input_show, NULL);
7989 /* sysfs fan fan2_input ------------------------------------------------ */
7990 static ssize_t fan_fan2_input_show(struct device *dev,
7991 struct device_attribute *attr,
7992 char *buf)
7994 int res;
7995 unsigned int speed;
7997 res = fan2_get_speed(&speed);
7998 if (res < 0)
7999 return res;
8001 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8004 static struct device_attribute dev_attr_fan_fan2_input =
8005 __ATTR(fan2_input, S_IRUGO,
8006 fan_fan2_input_show, NULL);
8008 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8009 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
8010 char *buf)
8012 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8015 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
8016 const char *buf, size_t count)
8018 unsigned long t;
8020 if (parse_strtoul(buf, 120, &t))
8021 return -EINVAL;
8023 if (!fan_control_allowed)
8024 return -EPERM;
8026 fan_watchdog_maxinterval = t;
8027 fan_watchdog_reset();
8029 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8031 return count;
8034 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8035 fan_fan_watchdog_show, fan_fan_watchdog_store);
8037 /* --------------------------------------------------------------------- */
8038 static struct attribute *fan_attributes[] = {
8039 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
8040 &dev_attr_fan_fan1_input.attr,
8041 NULL, /* for fan2_input */
8042 NULL
8045 static const struct attribute_group fan_attr_group = {
8046 .attrs = fan_attributes,
8049 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8050 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8052 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8053 { .vendor = PCI_VENDOR_ID_IBM, \
8054 .bios = TPACPI_MATCH_ANY, \
8055 .ec = TPID(__id1, __id2), \
8056 .quirks = __quirks }
8058 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8059 { .vendor = PCI_VENDOR_ID_LENOVO, \
8060 .bios = TPACPI_MATCH_ANY, \
8061 .ec = TPID(__id1, __id2), \
8062 .quirks = __quirks }
8064 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8065 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8066 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8067 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8068 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8069 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8072 #undef TPACPI_FAN_QL
8073 #undef TPACPI_FAN_QI
8075 static int __init fan_init(struct ibm_init_struct *iibm)
8077 int rc;
8078 unsigned long quirks;
8080 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8081 "initializing fan subdriver\n");
8083 mutex_init(&fan_mutex);
8084 fan_status_access_mode = TPACPI_FAN_NONE;
8085 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8086 fan_control_commands = 0;
8087 fan_watchdog_maxinterval = 0;
8088 tp_features.fan_ctrl_status_undef = 0;
8089 tp_features.second_fan = 0;
8090 fan_control_desired_level = 7;
8092 if (tpacpi_is_ibm()) {
8093 TPACPI_ACPIHANDLE_INIT(fans);
8094 TPACPI_ACPIHANDLE_INIT(gfan);
8095 TPACPI_ACPIHANDLE_INIT(sfan);
8098 quirks = tpacpi_check_quirks(fan_quirk_table,
8099 ARRAY_SIZE(fan_quirk_table));
8101 if (gfan_handle) {
8102 /* 570, 600e/x, 770e, 770x */
8103 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8104 } else {
8105 /* all other ThinkPads: note that even old-style
8106 * ThinkPad ECs supports the fan control register */
8107 if (likely(acpi_ec_read(fan_status_offset,
8108 &fan_control_initial_status))) {
8109 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8110 if (quirks & TPACPI_FAN_Q1)
8111 fan_quirk1_setup();
8112 if (quirks & TPACPI_FAN_2FAN) {
8113 tp_features.second_fan = 1;
8114 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8115 "secondary fan support enabled\n");
8117 } else {
8118 printk(TPACPI_ERR
8119 "ThinkPad ACPI EC access misbehaving, "
8120 "fan status and control unavailable\n");
8121 return 1;
8125 if (sfan_handle) {
8126 /* 570, 770x-JL */
8127 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8128 fan_control_commands |=
8129 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8130 } else {
8131 if (!gfan_handle) {
8132 /* gfan without sfan means no fan control */
8133 /* all other models implement TP EC 0x2f control */
8135 if (fans_handle) {
8136 /* X31, X40, X41 */
8137 fan_control_access_mode =
8138 TPACPI_FAN_WR_ACPI_FANS;
8139 fan_control_commands |=
8140 TPACPI_FAN_CMD_SPEED |
8141 TPACPI_FAN_CMD_LEVEL |
8142 TPACPI_FAN_CMD_ENABLE;
8143 } else {
8144 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8145 fan_control_commands |=
8146 TPACPI_FAN_CMD_LEVEL |
8147 TPACPI_FAN_CMD_ENABLE;
8152 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8153 "fan is %s, modes %d, %d\n",
8154 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8155 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8156 fan_status_access_mode, fan_control_access_mode);
8158 /* fan control master switch */
8159 if (!fan_control_allowed) {
8160 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8161 fan_control_commands = 0;
8162 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8163 "fan control features disabled by parameter\n");
8166 /* update fan_control_desired_level */
8167 if (fan_status_access_mode != TPACPI_FAN_NONE)
8168 fan_get_status_safe(NULL);
8170 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8171 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8172 if (tp_features.second_fan) {
8173 /* attach second fan tachometer */
8174 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8175 &dev_attr_fan_fan2_input.attr;
8177 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8178 &fan_attr_group);
8179 if (rc < 0)
8180 return rc;
8182 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8183 &driver_attr_fan_watchdog);
8184 if (rc < 0) {
8185 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8186 &fan_attr_group);
8187 return rc;
8189 return 0;
8190 } else
8191 return 1;
8194 static void fan_exit(void)
8196 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8197 "cancelling any pending fan watchdog tasks\n");
8199 /* FIXME: can we really do this unconditionally? */
8200 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8201 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8202 &driver_attr_fan_watchdog);
8204 cancel_delayed_work(&fan_watchdog_task);
8205 flush_workqueue(tpacpi_wq);
8208 static void fan_suspend(pm_message_t state)
8210 int rc;
8212 if (!fan_control_allowed)
8213 return;
8215 /* Store fan status in cache */
8216 fan_control_resume_level = 0;
8217 rc = fan_get_status_safe(&fan_control_resume_level);
8218 if (rc < 0)
8219 printk(TPACPI_NOTICE
8220 "failed to read fan level for later "
8221 "restore during resume: %d\n", rc);
8223 /* if it is undefined, don't attempt to restore it.
8224 * KEEP THIS LAST */
8225 if (tp_features.fan_ctrl_status_undef)
8226 fan_control_resume_level = 0;
8229 static void fan_resume(void)
8231 u8 current_level = 7;
8232 bool do_set = false;
8233 int rc;
8235 /* DSDT *always* updates status on resume */
8236 tp_features.fan_ctrl_status_undef = 0;
8238 if (!fan_control_allowed ||
8239 !fan_control_resume_level ||
8240 (fan_get_status_safe(&current_level) < 0))
8241 return;
8243 switch (fan_control_access_mode) {
8244 case TPACPI_FAN_WR_ACPI_SFAN:
8245 /* never decrease fan level */
8246 do_set = (fan_control_resume_level > current_level);
8247 break;
8248 case TPACPI_FAN_WR_ACPI_FANS:
8249 case TPACPI_FAN_WR_TPEC:
8250 /* never decrease fan level, scale is:
8251 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8253 * We expect the firmware to set either 7 or AUTO, but we
8254 * handle FULLSPEED out of paranoia.
8256 * So, we can safely only restore FULLSPEED or 7, anything
8257 * else could slow the fan. Restoring AUTO is useless, at
8258 * best that's exactly what the DSDT already set (it is the
8259 * slower it uses).
8261 * Always keep in mind that the DSDT *will* have set the
8262 * fans to what the vendor supposes is the best level. We
8263 * muck with it only to speed the fan up.
8265 if (fan_control_resume_level != 7 &&
8266 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8267 return;
8268 else
8269 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8270 (current_level != fan_control_resume_level);
8271 break;
8272 default:
8273 return;
8275 if (do_set) {
8276 printk(TPACPI_NOTICE
8277 "restoring fan level to 0x%02x\n",
8278 fan_control_resume_level);
8279 rc = fan_set_level_safe(fan_control_resume_level);
8280 if (rc < 0)
8281 printk(TPACPI_NOTICE
8282 "failed to restore fan level: %d\n", rc);
8286 static int fan_read(struct seq_file *m)
8288 int rc;
8289 u8 status;
8290 unsigned int speed = 0;
8292 switch (fan_status_access_mode) {
8293 case TPACPI_FAN_RD_ACPI_GFAN:
8294 /* 570, 600e/x, 770e, 770x */
8295 rc = fan_get_status_safe(&status);
8296 if (rc < 0)
8297 return rc;
8299 seq_printf(m, "status:\t\t%s\n"
8300 "level:\t\t%d\n",
8301 (status != 0) ? "enabled" : "disabled", status);
8302 break;
8304 case TPACPI_FAN_RD_TPEC:
8305 /* all except 570, 600e/x, 770e, 770x */
8306 rc = fan_get_status_safe(&status);
8307 if (rc < 0)
8308 return rc;
8310 seq_printf(m, "status:\t\t%s\n",
8311 (status != 0) ? "enabled" : "disabled");
8313 rc = fan_get_speed(&speed);
8314 if (rc < 0)
8315 return rc;
8317 seq_printf(m, "speed:\t\t%d\n", speed);
8319 if (status & TP_EC_FAN_FULLSPEED)
8320 /* Disengaged mode takes precedence */
8321 seq_printf(m, "level:\t\tdisengaged\n");
8322 else if (status & TP_EC_FAN_AUTO)
8323 seq_printf(m, "level:\t\tauto\n");
8324 else
8325 seq_printf(m, "level:\t\t%d\n", status);
8326 break;
8328 case TPACPI_FAN_NONE:
8329 default:
8330 seq_printf(m, "status:\t\tnot supported\n");
8333 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8334 seq_printf(m, "commands:\tlevel <level>");
8336 switch (fan_control_access_mode) {
8337 case TPACPI_FAN_WR_ACPI_SFAN:
8338 seq_printf(m, " (<level> is 0-7)\n");
8339 break;
8341 default:
8342 seq_printf(m, " (<level> is 0-7, "
8343 "auto, disengaged, full-speed)\n");
8344 break;
8348 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8349 seq_printf(m, "commands:\tenable, disable\n"
8350 "commands:\twatchdog <timeout> (<timeout> "
8351 "is 0 (off), 1-120 (seconds))\n");
8353 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8354 seq_printf(m, "commands:\tspeed <speed>"
8355 " (<speed> is 0-65535)\n");
8357 return 0;
8360 static int fan_write_cmd_level(const char *cmd, int *rc)
8362 int level;
8364 if (strlencmp(cmd, "level auto") == 0)
8365 level = TP_EC_FAN_AUTO;
8366 else if ((strlencmp(cmd, "level disengaged") == 0) |
8367 (strlencmp(cmd, "level full-speed") == 0))
8368 level = TP_EC_FAN_FULLSPEED;
8369 else if (sscanf(cmd, "level %d", &level) != 1)
8370 return 0;
8372 *rc = fan_set_level_safe(level);
8373 if (*rc == -ENXIO)
8374 printk(TPACPI_ERR "level command accepted for unsupported "
8375 "access mode %d", fan_control_access_mode);
8376 else if (!*rc)
8377 tpacpi_disclose_usertask("procfs fan",
8378 "set level to %d\n", level);
8380 return 1;
8383 static int fan_write_cmd_enable(const char *cmd, int *rc)
8385 if (strlencmp(cmd, "enable") != 0)
8386 return 0;
8388 *rc = fan_set_enable();
8389 if (*rc == -ENXIO)
8390 printk(TPACPI_ERR "enable command accepted for unsupported "
8391 "access mode %d", fan_control_access_mode);
8392 else if (!*rc)
8393 tpacpi_disclose_usertask("procfs fan", "enable\n");
8395 return 1;
8398 static int fan_write_cmd_disable(const char *cmd, int *rc)
8400 if (strlencmp(cmd, "disable") != 0)
8401 return 0;
8403 *rc = fan_set_disable();
8404 if (*rc == -ENXIO)
8405 printk(TPACPI_ERR "disable command accepted for unsupported "
8406 "access mode %d", fan_control_access_mode);
8407 else if (!*rc)
8408 tpacpi_disclose_usertask("procfs fan", "disable\n");
8410 return 1;
8413 static int fan_write_cmd_speed(const char *cmd, int *rc)
8415 int speed;
8417 /* TODO:
8418 * Support speed <low> <medium> <high> ? */
8420 if (sscanf(cmd, "speed %d", &speed) != 1)
8421 return 0;
8423 *rc = fan_set_speed(speed);
8424 if (*rc == -ENXIO)
8425 printk(TPACPI_ERR "speed command accepted for unsupported "
8426 "access mode %d", fan_control_access_mode);
8427 else if (!*rc)
8428 tpacpi_disclose_usertask("procfs fan",
8429 "set speed to %d\n", speed);
8431 return 1;
8434 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8436 int interval;
8438 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8439 return 0;
8441 if (interval < 0 || interval > 120)
8442 *rc = -EINVAL;
8443 else {
8444 fan_watchdog_maxinterval = interval;
8445 tpacpi_disclose_usertask("procfs fan",
8446 "set watchdog timer to %d\n",
8447 interval);
8450 return 1;
8453 static int fan_write(char *buf)
8455 char *cmd;
8456 int rc = 0;
8458 while (!rc && (cmd = next_cmd(&buf))) {
8459 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8460 fan_write_cmd_level(cmd, &rc)) &&
8461 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8462 (fan_write_cmd_enable(cmd, &rc) ||
8463 fan_write_cmd_disable(cmd, &rc) ||
8464 fan_write_cmd_watchdog(cmd, &rc))) &&
8465 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8466 fan_write_cmd_speed(cmd, &rc))
8468 rc = -EINVAL;
8469 else if (!rc)
8470 fan_watchdog_reset();
8473 return rc;
8476 static struct ibm_struct fan_driver_data = {
8477 .name = "fan",
8478 .read = fan_read,
8479 .write = fan_write,
8480 .exit = fan_exit,
8481 .suspend = fan_suspend,
8482 .resume = fan_resume,
8485 /****************************************************************************
8486 ****************************************************************************
8488 * Infrastructure
8490 ****************************************************************************
8491 ****************************************************************************/
8494 * HKEY event callout for other subdrivers go here
8495 * (yes, it is ugly, but it is quick, safe, and gets the job done
8497 static void tpacpi_driver_event(const unsigned int hkey_event)
8499 if (ibm_backlight_device) {
8500 switch (hkey_event) {
8501 case TP_HKEY_EV_BRGHT_UP:
8502 case TP_HKEY_EV_BRGHT_DOWN:
8503 tpacpi_brightness_notify_change();
8506 if (alsa_card) {
8507 switch (hkey_event) {
8508 case TP_HKEY_EV_VOL_UP:
8509 case TP_HKEY_EV_VOL_DOWN:
8510 case TP_HKEY_EV_VOL_MUTE:
8511 volume_alsa_notify_change();
8516 static void hotkey_driver_event(const unsigned int scancode)
8518 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8521 /* sysfs name ---------------------------------------------------------- */
8522 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8523 struct device_attribute *attr,
8524 char *buf)
8526 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8529 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8530 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8532 /* --------------------------------------------------------------------- */
8534 /* /proc support */
8535 static struct proc_dir_entry *proc_dir;
8538 * Module and infrastructure proble, init and exit handling
8541 static int force_load;
8543 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8544 static const char * __init str_supported(int is_supported)
8546 static char text_unsupported[] __initdata = "not supported";
8548 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8550 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8552 static void ibm_exit(struct ibm_struct *ibm)
8554 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8556 list_del_init(&ibm->all_drivers);
8558 if (ibm->flags.acpi_notify_installed) {
8559 dbg_printk(TPACPI_DBG_EXIT,
8560 "%s: acpi_remove_notify_handler\n", ibm->name);
8561 BUG_ON(!ibm->acpi);
8562 acpi_remove_notify_handler(*ibm->acpi->handle,
8563 ibm->acpi->type,
8564 dispatch_acpi_notify);
8565 ibm->flags.acpi_notify_installed = 0;
8566 ibm->flags.acpi_notify_installed = 0;
8569 if (ibm->flags.proc_created) {
8570 dbg_printk(TPACPI_DBG_EXIT,
8571 "%s: remove_proc_entry\n", ibm->name);
8572 remove_proc_entry(ibm->name, proc_dir);
8573 ibm->flags.proc_created = 0;
8576 if (ibm->flags.acpi_driver_registered) {
8577 dbg_printk(TPACPI_DBG_EXIT,
8578 "%s: acpi_bus_unregister_driver\n", ibm->name);
8579 BUG_ON(!ibm->acpi);
8580 acpi_bus_unregister_driver(ibm->acpi->driver);
8581 kfree(ibm->acpi->driver);
8582 ibm->acpi->driver = NULL;
8583 ibm->flags.acpi_driver_registered = 0;
8586 if (ibm->flags.init_called && ibm->exit) {
8587 ibm->exit();
8588 ibm->flags.init_called = 0;
8591 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8594 static int __init ibm_init(struct ibm_init_struct *iibm)
8596 int ret;
8597 struct ibm_struct *ibm = iibm->data;
8598 struct proc_dir_entry *entry;
8600 BUG_ON(ibm == NULL);
8602 INIT_LIST_HEAD(&ibm->all_drivers);
8604 if (ibm->flags.experimental && !experimental)
8605 return 0;
8607 dbg_printk(TPACPI_DBG_INIT,
8608 "probing for %s\n", ibm->name);
8610 if (iibm->init) {
8611 ret = iibm->init(iibm);
8612 if (ret > 0)
8613 return 0; /* probe failed */
8614 if (ret)
8615 return ret;
8617 ibm->flags.init_called = 1;
8620 if (ibm->acpi) {
8621 if (ibm->acpi->hid) {
8622 ret = register_tpacpi_subdriver(ibm);
8623 if (ret)
8624 goto err_out;
8627 if (ibm->acpi->notify) {
8628 ret = setup_acpi_notify(ibm);
8629 if (ret == -ENODEV) {
8630 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8631 ibm->name);
8632 ret = 0;
8633 goto err_out;
8635 if (ret < 0)
8636 goto err_out;
8640 dbg_printk(TPACPI_DBG_INIT,
8641 "%s installed\n", ibm->name);
8643 if (ibm->read) {
8644 mode_t mode = iibm->base_procfs_mode;
8646 if (!mode)
8647 mode = S_IRUGO;
8648 if (ibm->write)
8649 mode |= S_IWUSR;
8650 entry = proc_create_data(ibm->name, mode, proc_dir,
8651 &dispatch_proc_fops, ibm);
8652 if (!entry) {
8653 printk(TPACPI_ERR "unable to create proc entry %s\n",
8654 ibm->name);
8655 ret = -ENODEV;
8656 goto err_out;
8658 ibm->flags.proc_created = 1;
8661 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8663 return 0;
8665 err_out:
8666 dbg_printk(TPACPI_DBG_INIT,
8667 "%s: at error exit path with result %d\n",
8668 ibm->name, ret);
8670 ibm_exit(ibm);
8671 return (ret < 0)? ret : 0;
8674 /* Probing */
8676 static bool __pure __init tpacpi_is_fw_digit(const char c)
8678 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8681 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8682 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8683 const char t)
8685 return s && strlen(s) >= 8 &&
8686 tpacpi_is_fw_digit(s[0]) &&
8687 tpacpi_is_fw_digit(s[1]) &&
8688 s[2] == t && s[3] == 'T' &&
8689 tpacpi_is_fw_digit(s[4]) &&
8690 tpacpi_is_fw_digit(s[5]) &&
8691 s[6] == 'W' && s[7] == 'W';
8694 /* returns 0 - probe ok, or < 0 - probe error.
8695 * Probe ok doesn't mean thinkpad found.
8696 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8697 static int __must_check __init get_thinkpad_model_data(
8698 struct thinkpad_id_data *tp)
8700 const struct dmi_device *dev = NULL;
8701 char ec_fw_string[18];
8702 char const *s;
8704 if (!tp)
8705 return -EINVAL;
8707 memset(tp, 0, sizeof(*tp));
8709 if (dmi_name_in_vendors("IBM"))
8710 tp->vendor = PCI_VENDOR_ID_IBM;
8711 else if (dmi_name_in_vendors("LENOVO"))
8712 tp->vendor = PCI_VENDOR_ID_LENOVO;
8713 else
8714 return 0;
8716 s = dmi_get_system_info(DMI_BIOS_VERSION);
8717 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8718 if (s && !tp->bios_version_str)
8719 return -ENOMEM;
8721 /* Really ancient ThinkPad 240X will fail this, which is fine */
8722 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8723 return 0;
8725 tp->bios_model = tp->bios_version_str[0]
8726 | (tp->bios_version_str[1] << 8);
8727 tp->bios_release = (tp->bios_version_str[4] << 8)
8728 | tp->bios_version_str[5];
8731 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8732 * X32 or newer, all Z series; Some models must have an
8733 * up-to-date BIOS or they will not be detected.
8735 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8737 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8738 if (sscanf(dev->name,
8739 "IBM ThinkPad Embedded Controller -[%17c",
8740 ec_fw_string) == 1) {
8741 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8742 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8744 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8745 if (!tp->ec_version_str)
8746 return -ENOMEM;
8748 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8749 tp->ec_model = ec_fw_string[0]
8750 | (ec_fw_string[1] << 8);
8751 tp->ec_release = (ec_fw_string[4] << 8)
8752 | ec_fw_string[5];
8753 } else {
8754 printk(TPACPI_NOTICE
8755 "ThinkPad firmware release %s "
8756 "doesn't match the known patterns\n",
8757 ec_fw_string);
8758 printk(TPACPI_NOTICE
8759 "please report this to %s\n",
8760 TPACPI_MAIL);
8762 break;
8766 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8767 if (s && !strnicmp(s, "ThinkPad", 8)) {
8768 tp->model_str = kstrdup(s, GFP_KERNEL);
8769 if (!tp->model_str)
8770 return -ENOMEM;
8773 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8774 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8775 if (s && !tp->nummodel_str)
8776 return -ENOMEM;
8778 return 0;
8781 static int __init probe_for_thinkpad(void)
8783 int is_thinkpad;
8785 if (acpi_disabled)
8786 return -ENODEV;
8788 /* It would be dangerous to run the driver in this case */
8789 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8790 return -ENODEV;
8793 * Non-ancient models have better DMI tagging, but very old models
8794 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8796 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8797 (thinkpad_id.ec_model != 0) ||
8798 tpacpi_is_fw_known();
8800 /* The EC handler is required */
8801 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
8802 if (!ec_handle) {
8803 if (is_thinkpad)
8804 printk(TPACPI_ERR
8805 "Not yet supported ThinkPad detected!\n");
8806 return -ENODEV;
8809 if (!is_thinkpad && !force_load)
8810 return -ENODEV;
8812 return 0;
8815 static void __init thinkpad_acpi_init_banner(void)
8817 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8818 printk(TPACPI_INFO "%s\n", TPACPI_URL);
8820 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
8821 (thinkpad_id.bios_version_str) ?
8822 thinkpad_id.bios_version_str : "unknown",
8823 (thinkpad_id.ec_version_str) ?
8824 thinkpad_id.ec_version_str : "unknown");
8826 BUG_ON(!thinkpad_id.vendor);
8828 if (thinkpad_id.model_str)
8829 printk(TPACPI_INFO "%s %s, model %s\n",
8830 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8831 "IBM" : ((thinkpad_id.vendor ==
8832 PCI_VENDOR_ID_LENOVO) ?
8833 "Lenovo" : "Unknown vendor"),
8834 thinkpad_id.model_str,
8835 (thinkpad_id.nummodel_str) ?
8836 thinkpad_id.nummodel_str : "unknown");
8839 /* Module init, exit, parameters */
8841 static struct ibm_init_struct ibms_init[] __initdata = {
8843 .data = &thinkpad_acpi_driver_data,
8846 .init = hotkey_init,
8847 .data = &hotkey_driver_data,
8850 .init = bluetooth_init,
8851 .data = &bluetooth_driver_data,
8854 .init = wan_init,
8855 .data = &wan_driver_data,
8858 .init = uwb_init,
8859 .data = &uwb_driver_data,
8861 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8863 .init = video_init,
8864 .base_procfs_mode = S_IRUSR,
8865 .data = &video_driver_data,
8867 #endif
8869 .init = light_init,
8870 .data = &light_driver_data,
8873 .init = cmos_init,
8874 .data = &cmos_driver_data,
8877 .init = led_init,
8878 .data = &led_driver_data,
8881 .init = beep_init,
8882 .data = &beep_driver_data,
8885 .init = thermal_init,
8886 .data = &thermal_driver_data,
8889 .data = &ecdump_driver_data,
8892 .init = brightness_init,
8893 .data = &brightness_driver_data,
8896 .init = volume_init,
8897 .data = &volume_driver_data,
8900 .init = fan_init,
8901 .data = &fan_driver_data,
8905 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8907 unsigned int i;
8908 struct ibm_struct *ibm;
8910 if (!kp || !kp->name || !val)
8911 return -EINVAL;
8913 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8914 ibm = ibms_init[i].data;
8915 WARN_ON(ibm == NULL);
8917 if (!ibm || !ibm->name)
8918 continue;
8920 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8921 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8922 return -ENOSPC;
8923 strcpy(ibms_init[i].param, val);
8924 strcat(ibms_init[i].param, ",");
8925 return 0;
8929 return -EINVAL;
8932 module_param(experimental, int, 0444);
8933 MODULE_PARM_DESC(experimental,
8934 "Enables experimental features when non-zero");
8936 module_param_named(debug, dbg_level, uint, 0);
8937 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8939 module_param(force_load, bool, 0444);
8940 MODULE_PARM_DESC(force_load,
8941 "Attempts to load the driver even on a "
8942 "mis-identified ThinkPad when true");
8944 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8945 MODULE_PARM_DESC(fan_control,
8946 "Enables setting fan parameters features when true");
8948 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8949 MODULE_PARM_DESC(brightness_mode,
8950 "Selects brightness control strategy: "
8951 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8953 module_param(brightness_enable, uint, 0444);
8954 MODULE_PARM_DESC(brightness_enable,
8955 "Enables backlight control when 1, disables when 0");
8957 module_param(hotkey_report_mode, uint, 0444);
8958 MODULE_PARM_DESC(hotkey_report_mode,
8959 "used for backwards compatibility with userspace, "
8960 "see documentation");
8962 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8963 module_param_named(volume_mode, volume_mode, uint, 0444);
8964 MODULE_PARM_DESC(volume_mode,
8965 "Selects volume control strategy: "
8966 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8968 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8969 MODULE_PARM_DESC(volume_capabilities,
8970 "Selects the mixer capabilites: "
8971 "0=auto, 1=volume and mute, 2=mute only");
8973 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8974 MODULE_PARM_DESC(volume_control,
8975 "Enables software override for the console audio "
8976 "control when true");
8978 /* ALSA module API parameters */
8979 module_param_named(index, alsa_index, int, 0444);
8980 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8981 module_param_named(id, alsa_id, charp, 0444);
8982 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8983 module_param_named(enable, alsa_enable, bool, 0444);
8984 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8985 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8987 #define TPACPI_PARAM(feature) \
8988 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8989 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8990 "at module load, see documentation")
8992 TPACPI_PARAM(hotkey);
8993 TPACPI_PARAM(bluetooth);
8994 TPACPI_PARAM(video);
8995 TPACPI_PARAM(light);
8996 TPACPI_PARAM(cmos);
8997 TPACPI_PARAM(led);
8998 TPACPI_PARAM(beep);
8999 TPACPI_PARAM(ecdump);
9000 TPACPI_PARAM(brightness);
9001 TPACPI_PARAM(volume);
9002 TPACPI_PARAM(fan);
9004 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
9005 module_param(dbg_wlswemul, uint, 0444);
9006 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9007 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9008 MODULE_PARM_DESC(wlsw_state,
9009 "Initial state of the emulated WLSW switch");
9011 module_param(dbg_bluetoothemul, uint, 0444);
9012 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9013 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9014 MODULE_PARM_DESC(bluetooth_state,
9015 "Initial state of the emulated bluetooth switch");
9017 module_param(dbg_wwanemul, uint, 0444);
9018 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9019 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9020 MODULE_PARM_DESC(wwan_state,
9021 "Initial state of the emulated WWAN switch");
9023 module_param(dbg_uwbemul, uint, 0444);
9024 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9025 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9026 MODULE_PARM_DESC(uwb_state,
9027 "Initial state of the emulated UWB switch");
9028 #endif
9030 static void thinkpad_acpi_module_exit(void)
9032 struct ibm_struct *ibm, *itmp;
9034 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9036 list_for_each_entry_safe_reverse(ibm, itmp,
9037 &tpacpi_all_drivers,
9038 all_drivers) {
9039 ibm_exit(ibm);
9042 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9044 if (tpacpi_inputdev) {
9045 if (tp_features.input_device_registered)
9046 input_unregister_device(tpacpi_inputdev);
9047 else
9048 input_free_device(tpacpi_inputdev);
9051 if (tpacpi_hwmon)
9052 hwmon_device_unregister(tpacpi_hwmon);
9054 if (tp_features.sensors_pdev_attrs_registered)
9055 device_remove_file(&tpacpi_sensors_pdev->dev,
9056 &dev_attr_thinkpad_acpi_pdev_name);
9057 if (tpacpi_sensors_pdev)
9058 platform_device_unregister(tpacpi_sensors_pdev);
9059 if (tpacpi_pdev)
9060 platform_device_unregister(tpacpi_pdev);
9062 if (tp_features.sensors_pdrv_attrs_registered)
9063 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9064 if (tp_features.platform_drv_attrs_registered)
9065 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9067 if (tp_features.sensors_pdrv_registered)
9068 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9070 if (tp_features.platform_drv_registered)
9071 platform_driver_unregister(&tpacpi_pdriver);
9073 if (proc_dir)
9074 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9076 if (tpacpi_wq)
9077 destroy_workqueue(tpacpi_wq);
9079 kfree(thinkpad_id.bios_version_str);
9080 kfree(thinkpad_id.ec_version_str);
9081 kfree(thinkpad_id.model_str);
9085 static int __init thinkpad_acpi_module_init(void)
9087 int ret, i;
9089 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9091 /* Parameter checking */
9092 if (hotkey_report_mode > 2)
9093 return -EINVAL;
9095 /* Driver-level probe */
9097 ret = get_thinkpad_model_data(&thinkpad_id);
9098 if (ret) {
9099 printk(TPACPI_ERR
9100 "unable to get DMI data: %d\n", ret);
9101 thinkpad_acpi_module_exit();
9102 return ret;
9104 ret = probe_for_thinkpad();
9105 if (ret) {
9106 thinkpad_acpi_module_exit();
9107 return ret;
9110 /* Driver initialization */
9112 thinkpad_acpi_init_banner();
9113 tpacpi_check_outdated_fw();
9115 TPACPI_ACPIHANDLE_INIT(ecrd);
9116 TPACPI_ACPIHANDLE_INIT(ecwr);
9118 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9119 if (!tpacpi_wq) {
9120 thinkpad_acpi_module_exit();
9121 return -ENOMEM;
9124 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9125 if (!proc_dir) {
9126 printk(TPACPI_ERR
9127 "unable to create proc dir " TPACPI_PROC_DIR);
9128 thinkpad_acpi_module_exit();
9129 return -ENODEV;
9132 ret = platform_driver_register(&tpacpi_pdriver);
9133 if (ret) {
9134 printk(TPACPI_ERR
9135 "unable to register main platform driver\n");
9136 thinkpad_acpi_module_exit();
9137 return ret;
9139 tp_features.platform_drv_registered = 1;
9141 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9142 if (ret) {
9143 printk(TPACPI_ERR
9144 "unable to register hwmon platform driver\n");
9145 thinkpad_acpi_module_exit();
9146 return ret;
9148 tp_features.sensors_pdrv_registered = 1;
9150 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9151 if (!ret) {
9152 tp_features.platform_drv_attrs_registered = 1;
9153 ret = tpacpi_create_driver_attributes(
9154 &tpacpi_hwmon_pdriver.driver);
9156 if (ret) {
9157 printk(TPACPI_ERR
9158 "unable to create sysfs driver attributes\n");
9159 thinkpad_acpi_module_exit();
9160 return ret;
9162 tp_features.sensors_pdrv_attrs_registered = 1;
9165 /* Device initialization */
9166 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9167 NULL, 0);
9168 if (IS_ERR(tpacpi_pdev)) {
9169 ret = PTR_ERR(tpacpi_pdev);
9170 tpacpi_pdev = NULL;
9171 printk(TPACPI_ERR "unable to register platform device\n");
9172 thinkpad_acpi_module_exit();
9173 return ret;
9175 tpacpi_sensors_pdev = platform_device_register_simple(
9176 TPACPI_HWMON_DRVR_NAME,
9177 -1, NULL, 0);
9178 if (IS_ERR(tpacpi_sensors_pdev)) {
9179 ret = PTR_ERR(tpacpi_sensors_pdev);
9180 tpacpi_sensors_pdev = NULL;
9181 printk(TPACPI_ERR
9182 "unable to register hwmon platform device\n");
9183 thinkpad_acpi_module_exit();
9184 return ret;
9186 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9187 &dev_attr_thinkpad_acpi_pdev_name);
9188 if (ret) {
9189 printk(TPACPI_ERR
9190 "unable to create sysfs hwmon device attributes\n");
9191 thinkpad_acpi_module_exit();
9192 return ret;
9194 tp_features.sensors_pdev_attrs_registered = 1;
9195 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9196 if (IS_ERR(tpacpi_hwmon)) {
9197 ret = PTR_ERR(tpacpi_hwmon);
9198 tpacpi_hwmon = NULL;
9199 printk(TPACPI_ERR "unable to register hwmon device\n");
9200 thinkpad_acpi_module_exit();
9201 return ret;
9203 mutex_init(&tpacpi_inputdev_send_mutex);
9204 tpacpi_inputdev = input_allocate_device();
9205 if (!tpacpi_inputdev) {
9206 printk(TPACPI_ERR "unable to allocate input device\n");
9207 thinkpad_acpi_module_exit();
9208 return -ENOMEM;
9209 } else {
9210 /* Prepare input device, but don't register */
9211 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9212 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9213 tpacpi_inputdev->id.bustype = BUS_HOST;
9214 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9215 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9216 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9217 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9220 /* Init subdriver dependencies */
9221 tpacpi_detect_brightness_capabilities();
9223 /* Init subdrivers */
9224 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9225 ret = ibm_init(&ibms_init[i]);
9226 if (ret >= 0 && *ibms_init[i].param)
9227 ret = ibms_init[i].data->write(ibms_init[i].param);
9228 if (ret < 0) {
9229 thinkpad_acpi_module_exit();
9230 return ret;
9234 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9236 ret = input_register_device(tpacpi_inputdev);
9237 if (ret < 0) {
9238 printk(TPACPI_ERR "unable to register input device\n");
9239 thinkpad_acpi_module_exit();
9240 return ret;
9241 } else {
9242 tp_features.input_device_registered = 1;
9245 return 0;
9248 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9251 * This will autoload the driver in almost every ThinkPad
9252 * in widespread use.
9254 * Only _VERY_ old models, like the 240, 240x and 570 lack
9255 * the HKEY event interface.
9257 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9260 * DMI matching for module autoloading
9262 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9263 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9265 * Only models listed in thinkwiki will be supported, so add yours
9266 * if it is not there yet.
9268 #define IBM_BIOS_MODULE_ALIAS(__type) \
9269 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9271 /* Ancient thinkpad BIOSes have to be identified by
9272 * BIOS type or model number, and there are far less
9273 * BIOS types than model numbers... */
9274 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9276 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9277 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9278 MODULE_DESCRIPTION(TPACPI_DESC);
9279 MODULE_VERSION(TPACPI_VERSION);
9280 MODULE_LICENSE("GPL");
9282 module_init(thinkpad_acpi_module_init);
9283 module_exit(thinkpad_acpi_module_exit);