thinkpad-acpi: let other subdrivers know backlight level range
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob943e526133f59e8654ace80016638634aa6d24e5
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;
516 #define TPACPI_HANDLE(object, parent, paths...) \
517 static acpi_handle object##_handle; \
518 static acpi_handle *object##_parent = &parent##_handle; \
519 static char *object##_path; \
520 static char *object##_paths[] = { paths }
522 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
523 "\\_SB.PCI.ISA.EC", /* 570 */
524 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
525 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
526 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
527 "\\_SB.PCI0.ICH3.EC0", /* R31 */
528 "\\_SB.PCI0.LPC0.EC", /* X100e and a few others */
529 "\\_SB.PCI0.LPC.EC", /* all others */
532 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
533 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
535 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
536 /* T4x, X31, X40 */
537 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
538 "\\CMS", /* R40, R40e */
539 ); /* all others */
541 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
542 "^HKEY", /* R30, R31 */
543 "HKEY", /* all others */
544 ); /* 570 */
546 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
547 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
548 "\\_SB.PCI0.VID0", /* 770e */
549 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
550 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
551 "\\_SB.PCI0.AGP.VID", /* all others */
552 ); /* R30, R31 */
555 /*************************************************************************
556 * ACPI helpers
559 static int acpi_evalf(acpi_handle handle,
560 void *res, char *method, char *fmt, ...)
562 char *fmt0 = fmt;
563 struct acpi_object_list params;
564 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
565 struct acpi_buffer result, *resultp;
566 union acpi_object out_obj;
567 acpi_status status;
568 va_list ap;
569 char res_type;
570 int success;
571 int quiet;
573 if (!*fmt) {
574 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
575 return 0;
578 if (*fmt == 'q') {
579 quiet = 1;
580 fmt++;
581 } else
582 quiet = 0;
584 res_type = *(fmt++);
586 params.count = 0;
587 params.pointer = &in_objs[0];
589 va_start(ap, fmt);
590 while (*fmt) {
591 char c = *(fmt++);
592 switch (c) {
593 case 'd': /* int */
594 in_objs[params.count].integer.value = va_arg(ap, int);
595 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
596 break;
597 /* add more types as needed */
598 default:
599 printk(TPACPI_ERR "acpi_evalf() called "
600 "with invalid format character '%c'\n", c);
601 return 0;
604 va_end(ap);
606 if (res_type != 'v') {
607 result.length = sizeof(out_obj);
608 result.pointer = &out_obj;
609 resultp = &result;
610 } else
611 resultp = NULL;
613 status = acpi_evaluate_object(handle, method, &params, resultp);
615 switch (res_type) {
616 case 'd': /* int */
617 if (res)
618 *(int *)res = out_obj.integer.value;
619 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
620 break;
621 case 'v': /* void */
622 success = status == AE_OK;
623 break;
624 /* add more types as needed */
625 default:
626 printk(TPACPI_ERR "acpi_evalf() called "
627 "with invalid format character '%c'\n", res_type);
628 return 0;
631 if (!success && !quiet)
632 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
633 method, fmt0, status);
635 return success;
638 static int acpi_ec_read(int i, u8 *p)
640 int v;
642 if (ecrd_handle) {
643 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
644 return 0;
645 *p = v;
646 } else {
647 if (ec_read(i, p) < 0)
648 return 0;
651 return 1;
654 static int acpi_ec_write(int i, u8 v)
656 if (ecwr_handle) {
657 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
658 return 0;
659 } else {
660 if (ec_write(i, v) < 0)
661 return 0;
664 return 1;
667 static int issue_thinkpad_cmos_command(int cmos_cmd)
669 if (!cmos_handle)
670 return -ENXIO;
672 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
673 return -EIO;
675 return 0;
678 /*************************************************************************
679 * ACPI device model
682 #define TPACPI_ACPIHANDLE_INIT(object) \
683 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
684 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
686 static void drv_acpi_handle_init(char *name,
687 acpi_handle *handle, acpi_handle parent,
688 char **paths, int num_paths, char **path)
690 int i;
691 acpi_status status;
693 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
694 name);
696 for (i = 0; i < num_paths; i++) {
697 status = acpi_get_handle(parent, paths[i], handle);
698 if (ACPI_SUCCESS(status)) {
699 *path = paths[i];
700 dbg_printk(TPACPI_DBG_INIT,
701 "Found ACPI handle %s for %s\n",
702 *path, name);
703 return;
707 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
708 name);
709 *handle = NULL;
712 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
714 struct ibm_struct *ibm = data;
716 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
717 return;
719 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
720 return;
722 ibm->acpi->notify(ibm, event);
725 static int __init setup_acpi_notify(struct ibm_struct *ibm)
727 acpi_status status;
728 int rc;
730 BUG_ON(!ibm->acpi);
732 if (!*ibm->acpi->handle)
733 return 0;
735 vdbg_printk(TPACPI_DBG_INIT,
736 "setting up ACPI notify for %s\n", ibm->name);
738 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
739 if (rc < 0) {
740 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
741 ibm->name, rc);
742 return -ENODEV;
745 ibm->acpi->device->driver_data = ibm;
746 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
747 TPACPI_ACPI_EVENT_PREFIX,
748 ibm->name);
750 status = acpi_install_notify_handler(*ibm->acpi->handle,
751 ibm->acpi->type, dispatch_acpi_notify, ibm);
752 if (ACPI_FAILURE(status)) {
753 if (status == AE_ALREADY_EXISTS) {
754 printk(TPACPI_NOTICE
755 "another device driver is already "
756 "handling %s events\n", ibm->name);
757 } else {
758 printk(TPACPI_ERR
759 "acpi_install_notify_handler(%s) failed: %d\n",
760 ibm->name, status);
762 return -ENODEV;
764 ibm->flags.acpi_notify_installed = 1;
765 return 0;
768 static int __init tpacpi_device_add(struct acpi_device *device)
770 return 0;
773 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
775 int rc;
777 dbg_printk(TPACPI_DBG_INIT,
778 "registering %s as an ACPI driver\n", ibm->name);
780 BUG_ON(!ibm->acpi);
782 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
783 if (!ibm->acpi->driver) {
784 printk(TPACPI_ERR
785 "failed to allocate memory for ibm->acpi->driver\n");
786 return -ENOMEM;
789 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
790 ibm->acpi->driver->ids = ibm->acpi->hid;
792 ibm->acpi->driver->ops.add = &tpacpi_device_add;
794 rc = acpi_bus_register_driver(ibm->acpi->driver);
795 if (rc < 0) {
796 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
797 ibm->name, rc);
798 kfree(ibm->acpi->driver);
799 ibm->acpi->driver = NULL;
800 } else if (!rc)
801 ibm->flags.acpi_driver_registered = 1;
803 return rc;
807 /****************************************************************************
808 ****************************************************************************
810 * Procfs Helpers
812 ****************************************************************************
813 ****************************************************************************/
815 static int dispatch_proc_show(struct seq_file *m, void *v)
817 struct ibm_struct *ibm = m->private;
819 if (!ibm || !ibm->read)
820 return -EINVAL;
821 return ibm->read(m);
824 static int dispatch_proc_open(struct inode *inode, struct file *file)
826 return single_open(file, dispatch_proc_show, PDE(inode)->data);
829 static ssize_t dispatch_proc_write(struct file *file,
830 const char __user *userbuf,
831 size_t count, loff_t *pos)
833 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
834 char *kernbuf;
835 int ret;
837 if (!ibm || !ibm->write)
838 return -EINVAL;
839 if (count > PAGE_SIZE - 2)
840 return -EINVAL;
842 kernbuf = kmalloc(count + 2, GFP_KERNEL);
843 if (!kernbuf)
844 return -ENOMEM;
846 if (copy_from_user(kernbuf, userbuf, count)) {
847 kfree(kernbuf);
848 return -EFAULT;
851 kernbuf[count] = 0;
852 strcat(kernbuf, ",");
853 ret = ibm->write(kernbuf);
854 if (ret == 0)
855 ret = count;
857 kfree(kernbuf);
859 return ret;
862 static const struct file_operations dispatch_proc_fops = {
863 .owner = THIS_MODULE,
864 .open = dispatch_proc_open,
865 .read = seq_read,
866 .llseek = seq_lseek,
867 .release = single_release,
868 .write = dispatch_proc_write,
871 static char *next_cmd(char **cmds)
873 char *start = *cmds;
874 char *end;
876 while ((end = strchr(start, ',')) && end == start)
877 start = end + 1;
879 if (!end)
880 return NULL;
882 *end = 0;
883 *cmds = end + 1;
884 return start;
888 /****************************************************************************
889 ****************************************************************************
891 * Device model: input, hwmon and platform
893 ****************************************************************************
894 ****************************************************************************/
896 static struct platform_device *tpacpi_pdev;
897 static struct platform_device *tpacpi_sensors_pdev;
898 static struct device *tpacpi_hwmon;
899 static struct input_dev *tpacpi_inputdev;
900 static struct mutex tpacpi_inputdev_send_mutex;
901 static LIST_HEAD(tpacpi_all_drivers);
903 static int tpacpi_suspend_handler(struct platform_device *pdev,
904 pm_message_t state)
906 struct ibm_struct *ibm, *itmp;
908 list_for_each_entry_safe(ibm, itmp,
909 &tpacpi_all_drivers,
910 all_drivers) {
911 if (ibm->suspend)
912 (ibm->suspend)(state);
915 return 0;
918 static int tpacpi_resume_handler(struct platform_device *pdev)
920 struct ibm_struct *ibm, *itmp;
922 list_for_each_entry_safe(ibm, itmp,
923 &tpacpi_all_drivers,
924 all_drivers) {
925 if (ibm->resume)
926 (ibm->resume)();
929 return 0;
932 static void tpacpi_shutdown_handler(struct platform_device *pdev)
934 struct ibm_struct *ibm, *itmp;
936 list_for_each_entry_safe(ibm, itmp,
937 &tpacpi_all_drivers,
938 all_drivers) {
939 if (ibm->shutdown)
940 (ibm->shutdown)();
944 static struct platform_driver tpacpi_pdriver = {
945 .driver = {
946 .name = TPACPI_DRVR_NAME,
947 .owner = THIS_MODULE,
949 .suspend = tpacpi_suspend_handler,
950 .resume = tpacpi_resume_handler,
951 .shutdown = tpacpi_shutdown_handler,
954 static struct platform_driver tpacpi_hwmon_pdriver = {
955 .driver = {
956 .name = TPACPI_HWMON_DRVR_NAME,
957 .owner = THIS_MODULE,
961 /*************************************************************************
962 * sysfs support helpers
965 struct attribute_set {
966 unsigned int members, max_members;
967 struct attribute_group group;
970 struct attribute_set_obj {
971 struct attribute_set s;
972 struct attribute *a;
973 } __attribute__((packed));
975 static struct attribute_set *create_attr_set(unsigned int max_members,
976 const char *name)
978 struct attribute_set_obj *sobj;
980 if (max_members == 0)
981 return NULL;
983 /* Allocates space for implicit NULL at the end too */
984 sobj = kzalloc(sizeof(struct attribute_set_obj) +
985 max_members * sizeof(struct attribute *),
986 GFP_KERNEL);
987 if (!sobj)
988 return NULL;
989 sobj->s.max_members = max_members;
990 sobj->s.group.attrs = &sobj->a;
991 sobj->s.group.name = name;
993 return &sobj->s;
996 #define destroy_attr_set(_set) \
997 kfree(_set);
999 /* not multi-threaded safe, use it in a single thread per set */
1000 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1002 if (!s || !attr)
1003 return -EINVAL;
1005 if (s->members >= s->max_members)
1006 return -ENOMEM;
1008 s->group.attrs[s->members] = attr;
1009 s->members++;
1011 return 0;
1014 static int add_many_to_attr_set(struct attribute_set *s,
1015 struct attribute **attr,
1016 unsigned int count)
1018 int i, res;
1020 for (i = 0; i < count; i++) {
1021 res = add_to_attr_set(s, attr[i]);
1022 if (res)
1023 return res;
1026 return 0;
1029 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1031 sysfs_remove_group(kobj, &s->group);
1032 destroy_attr_set(s);
1035 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1036 sysfs_create_group(_kobj, &_attr_set->group)
1038 static int parse_strtoul(const char *buf,
1039 unsigned long max, unsigned long *value)
1041 char *endp;
1043 while (*buf && isspace(*buf))
1044 buf++;
1045 *value = simple_strtoul(buf, &endp, 0);
1046 while (*endp && isspace(*endp))
1047 endp++;
1048 if (*endp || *value > max)
1049 return -EINVAL;
1051 return 0;
1054 static void tpacpi_disable_brightness_delay(void)
1056 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1057 printk(TPACPI_NOTICE
1058 "ACPI backlight control delay disabled\n");
1061 static void printk_deprecated_attribute(const char * const what,
1062 const char * const details)
1064 tpacpi_log_usertask("deprecated sysfs attribute");
1065 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1066 "will be removed. %s\n",
1067 what, details);
1070 /*************************************************************************
1071 * rfkill and radio control support helpers
1075 * ThinkPad-ACPI firmware handling model:
1077 * WLSW (master wireless switch) is event-driven, and is common to all
1078 * firmware-controlled radios. It cannot be controlled, just monitored,
1079 * as expected. It overrides all radio state in firmware
1081 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1082 * (TODO: verify how WLSW interacts with the returned radio state).
1084 * The only time there are shadow radio state changes, is when
1085 * masked-off hotkeys are used.
1089 * Internal driver API for radio state:
1091 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1092 * bool: true means radio blocked (off)
1094 enum tpacpi_rfkill_state {
1095 TPACPI_RFK_RADIO_OFF = 0,
1096 TPACPI_RFK_RADIO_ON
1099 /* rfkill switches */
1100 enum tpacpi_rfk_id {
1101 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1102 TPACPI_RFK_WWAN_SW_ID,
1103 TPACPI_RFK_UWB_SW_ID,
1104 TPACPI_RFK_SW_MAX
1107 static const char *tpacpi_rfkill_names[] = {
1108 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1109 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1110 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1111 [TPACPI_RFK_SW_MAX] = NULL
1114 /* ThinkPad-ACPI rfkill subdriver */
1115 struct tpacpi_rfk {
1116 struct rfkill *rfkill;
1117 enum tpacpi_rfk_id id;
1118 const struct tpacpi_rfk_ops *ops;
1121 struct tpacpi_rfk_ops {
1122 /* firmware interface */
1123 int (*get_status)(void);
1124 int (*set_status)(const enum tpacpi_rfkill_state);
1127 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1129 /* Query FW and update rfkill sw state for a given rfkill switch */
1130 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1132 int status;
1134 if (!tp_rfk)
1135 return -ENODEV;
1137 status = (tp_rfk->ops->get_status)();
1138 if (status < 0)
1139 return status;
1141 rfkill_set_sw_state(tp_rfk->rfkill,
1142 (status == TPACPI_RFK_RADIO_OFF));
1144 return status;
1147 /* Query FW and update rfkill sw state for all rfkill switches */
1148 static void tpacpi_rfk_update_swstate_all(void)
1150 unsigned int i;
1152 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1153 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1157 * Sync the HW-blocking state of all rfkill switches,
1158 * do notice it causes the rfkill core to schedule uevents
1160 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1162 unsigned int i;
1163 struct tpacpi_rfk *tp_rfk;
1165 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1166 tp_rfk = tpacpi_rfkill_switches[i];
1167 if (tp_rfk) {
1168 if (rfkill_set_hw_state(tp_rfk->rfkill,
1169 blocked)) {
1170 /* ignore -- we track sw block */
1176 /* Call to get the WLSW state from the firmware */
1177 static int hotkey_get_wlsw(void);
1179 /* Call to query WLSW state and update all rfkill switches */
1180 static bool tpacpi_rfk_check_hwblock_state(void)
1182 int res = hotkey_get_wlsw();
1183 int hw_blocked;
1185 /* When unknown or unsupported, we have to assume it is unblocked */
1186 if (res < 0)
1187 return false;
1189 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1190 tpacpi_rfk_update_hwblock_state(hw_blocked);
1192 return hw_blocked;
1195 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1197 struct tpacpi_rfk *tp_rfk = data;
1198 int res;
1200 dbg_printk(TPACPI_DBG_RFKILL,
1201 "request to change radio state to %s\n",
1202 blocked ? "blocked" : "unblocked");
1204 /* try to set radio state */
1205 res = (tp_rfk->ops->set_status)(blocked ?
1206 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1208 /* and update the rfkill core with whatever the FW really did */
1209 tpacpi_rfk_update_swstate(tp_rfk);
1211 return (res < 0) ? res : 0;
1214 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1215 .set_block = tpacpi_rfk_hook_set_block,
1218 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1219 const struct tpacpi_rfk_ops *tp_rfkops,
1220 const enum rfkill_type rfktype,
1221 const char *name,
1222 const bool set_default)
1224 struct tpacpi_rfk *atp_rfk;
1225 int res;
1226 bool sw_state = false;
1227 bool hw_state;
1228 int sw_status;
1230 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1232 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1233 if (atp_rfk)
1234 atp_rfk->rfkill = rfkill_alloc(name,
1235 &tpacpi_pdev->dev,
1236 rfktype,
1237 &tpacpi_rfk_rfkill_ops,
1238 atp_rfk);
1239 if (!atp_rfk || !atp_rfk->rfkill) {
1240 printk(TPACPI_ERR
1241 "failed to allocate memory for rfkill class\n");
1242 kfree(atp_rfk);
1243 return -ENOMEM;
1246 atp_rfk->id = id;
1247 atp_rfk->ops = tp_rfkops;
1249 sw_status = (tp_rfkops->get_status)();
1250 if (sw_status < 0) {
1251 printk(TPACPI_ERR
1252 "failed to read initial state for %s, error %d\n",
1253 name, sw_status);
1254 } else {
1255 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1256 if (set_default) {
1257 /* try to keep the initial state, since we ask the
1258 * firmware to preserve it across S5 in NVRAM */
1259 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1262 hw_state = tpacpi_rfk_check_hwblock_state();
1263 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1265 res = rfkill_register(atp_rfk->rfkill);
1266 if (res < 0) {
1267 printk(TPACPI_ERR
1268 "failed to register %s rfkill switch: %d\n",
1269 name, res);
1270 rfkill_destroy(atp_rfk->rfkill);
1271 kfree(atp_rfk);
1272 return res;
1275 tpacpi_rfkill_switches[id] = atp_rfk;
1277 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1278 name, (sw_state || hw_state) ? "" : "un");
1279 return 0;
1282 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1284 struct tpacpi_rfk *tp_rfk;
1286 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1288 tp_rfk = tpacpi_rfkill_switches[id];
1289 if (tp_rfk) {
1290 rfkill_unregister(tp_rfk->rfkill);
1291 rfkill_destroy(tp_rfk->rfkill);
1292 tpacpi_rfkill_switches[id] = NULL;
1293 kfree(tp_rfk);
1297 static void printk_deprecated_rfkill_attribute(const char * const what)
1299 printk_deprecated_attribute(what,
1300 "Please switch to generic rfkill before year 2010");
1303 /* sysfs <radio> enable ------------------------------------------------ */
1304 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1305 struct device_attribute *attr,
1306 char *buf)
1308 int status;
1310 printk_deprecated_rfkill_attribute(attr->attr.name);
1312 /* This is in the ABI... */
1313 if (tpacpi_rfk_check_hwblock_state()) {
1314 status = TPACPI_RFK_RADIO_OFF;
1315 } else {
1316 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1317 if (status < 0)
1318 return status;
1321 return snprintf(buf, PAGE_SIZE, "%d\n",
1322 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1325 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1326 struct device_attribute *attr,
1327 const char *buf, size_t count)
1329 unsigned long t;
1330 int res;
1332 printk_deprecated_rfkill_attribute(attr->attr.name);
1334 if (parse_strtoul(buf, 1, &t))
1335 return -EINVAL;
1337 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1339 /* This is in the ABI... */
1340 if (tpacpi_rfk_check_hwblock_state() && !!t)
1341 return -EPERM;
1343 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1344 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1345 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1347 return (res < 0) ? res : count;
1350 /* procfs -------------------------------------------------------------- */
1351 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id,
1352 struct seq_file *m)
1354 if (id >= TPACPI_RFK_SW_MAX)
1355 seq_printf(m, "status:\t\tnot supported\n");
1356 else {
1357 int status;
1359 /* This is in the ABI... */
1360 if (tpacpi_rfk_check_hwblock_state()) {
1361 status = TPACPI_RFK_RADIO_OFF;
1362 } else {
1363 status = tpacpi_rfk_update_swstate(
1364 tpacpi_rfkill_switches[id]);
1365 if (status < 0)
1366 return status;
1369 seq_printf(m, "status:\t\t%s\n",
1370 (status == TPACPI_RFK_RADIO_ON) ?
1371 "enabled" : "disabled");
1372 seq_printf(m, "commands:\tenable, disable\n");
1375 return 0;
1378 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1380 char *cmd;
1381 int status = -1;
1382 int res = 0;
1384 if (id >= TPACPI_RFK_SW_MAX)
1385 return -ENODEV;
1387 while ((cmd = next_cmd(&buf))) {
1388 if (strlencmp(cmd, "enable") == 0)
1389 status = TPACPI_RFK_RADIO_ON;
1390 else if (strlencmp(cmd, "disable") == 0)
1391 status = TPACPI_RFK_RADIO_OFF;
1392 else
1393 return -EINVAL;
1396 if (status != -1) {
1397 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1398 (status == TPACPI_RFK_RADIO_ON) ?
1399 "enable" : "disable",
1400 tpacpi_rfkill_names[id]);
1401 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1402 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1405 return res;
1408 /*************************************************************************
1409 * thinkpad-acpi driver attributes
1412 /* interface_version --------------------------------------------------- */
1413 static ssize_t tpacpi_driver_interface_version_show(
1414 struct device_driver *drv,
1415 char *buf)
1417 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1420 static DRIVER_ATTR(interface_version, S_IRUGO,
1421 tpacpi_driver_interface_version_show, NULL);
1423 /* debug_level --------------------------------------------------------- */
1424 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1425 char *buf)
1427 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1430 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1431 const char *buf, size_t count)
1433 unsigned long t;
1435 if (parse_strtoul(buf, 0xffff, &t))
1436 return -EINVAL;
1438 dbg_level = t;
1440 return count;
1443 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1444 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1446 /* version ------------------------------------------------------------- */
1447 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1448 char *buf)
1450 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1451 TPACPI_DESC, TPACPI_VERSION);
1454 static DRIVER_ATTR(version, S_IRUGO,
1455 tpacpi_driver_version_show, NULL);
1457 /* --------------------------------------------------------------------- */
1459 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1461 /* wlsw_emulstate ------------------------------------------------------ */
1462 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1463 char *buf)
1465 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1468 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1469 const char *buf, size_t count)
1471 unsigned long t;
1473 if (parse_strtoul(buf, 1, &t))
1474 return -EINVAL;
1476 if (tpacpi_wlsw_emulstate != !!t) {
1477 tpacpi_wlsw_emulstate = !!t;
1478 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1481 return count;
1484 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1485 tpacpi_driver_wlsw_emulstate_show,
1486 tpacpi_driver_wlsw_emulstate_store);
1488 /* bluetooth_emulstate ------------------------------------------------- */
1489 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1490 struct device_driver *drv,
1491 char *buf)
1493 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1496 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1497 struct device_driver *drv,
1498 const char *buf, size_t count)
1500 unsigned long t;
1502 if (parse_strtoul(buf, 1, &t))
1503 return -EINVAL;
1505 tpacpi_bluetooth_emulstate = !!t;
1507 return count;
1510 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1511 tpacpi_driver_bluetooth_emulstate_show,
1512 tpacpi_driver_bluetooth_emulstate_store);
1514 /* wwan_emulstate ------------------------------------------------- */
1515 static ssize_t tpacpi_driver_wwan_emulstate_show(
1516 struct device_driver *drv,
1517 char *buf)
1519 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1522 static ssize_t tpacpi_driver_wwan_emulstate_store(
1523 struct device_driver *drv,
1524 const char *buf, size_t count)
1526 unsigned long t;
1528 if (parse_strtoul(buf, 1, &t))
1529 return -EINVAL;
1531 tpacpi_wwan_emulstate = !!t;
1533 return count;
1536 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1537 tpacpi_driver_wwan_emulstate_show,
1538 tpacpi_driver_wwan_emulstate_store);
1540 /* uwb_emulstate ------------------------------------------------- */
1541 static ssize_t tpacpi_driver_uwb_emulstate_show(
1542 struct device_driver *drv,
1543 char *buf)
1545 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1548 static ssize_t tpacpi_driver_uwb_emulstate_store(
1549 struct device_driver *drv,
1550 const char *buf, size_t count)
1552 unsigned long t;
1554 if (parse_strtoul(buf, 1, &t))
1555 return -EINVAL;
1557 tpacpi_uwb_emulstate = !!t;
1559 return count;
1562 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1563 tpacpi_driver_uwb_emulstate_show,
1564 tpacpi_driver_uwb_emulstate_store);
1565 #endif
1567 /* --------------------------------------------------------------------- */
1569 static struct driver_attribute *tpacpi_driver_attributes[] = {
1570 &driver_attr_debug_level, &driver_attr_version,
1571 &driver_attr_interface_version,
1574 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1576 int i, res;
1578 i = 0;
1579 res = 0;
1580 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1581 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1582 i++;
1585 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1586 if (!res && dbg_wlswemul)
1587 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1588 if (!res && dbg_bluetoothemul)
1589 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1590 if (!res && dbg_wwanemul)
1591 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1592 if (!res && dbg_uwbemul)
1593 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1594 #endif
1596 return res;
1599 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1601 int i;
1603 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1604 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1606 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1607 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1608 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1609 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1610 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1611 #endif
1614 /*************************************************************************
1615 * Firmware Data
1619 * Table of recommended minimum BIOS versions
1621 * Reasons for listing:
1622 * 1. Stable BIOS, listed because the unknown amount of
1623 * bugs and bad ACPI behaviour on older versions
1625 * 2. BIOS or EC fw with known bugs that trigger on Linux
1627 * 3. BIOS with known reduced functionality in older versions
1629 * We recommend the latest BIOS and EC version.
1630 * We only support the latest BIOS and EC fw version as a rule.
1632 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1633 * Information from users in ThinkWiki
1635 * WARNING: we use this table also to detect that the machine is
1636 * a ThinkPad in some cases, so don't remove entries lightly.
1639 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1640 { .vendor = (__v), \
1641 .bios = TPID(__id1, __id2), \
1642 .ec = TPACPI_MATCH_ANY, \
1643 .quirks = TPACPI_MATCH_ANY << 16 \
1644 | (__bv1) << 8 | (__bv2) }
1646 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1647 __eid, __ev1, __ev2) \
1648 { .vendor = (__v), \
1649 .bios = TPID(__bid1, __bid2), \
1650 .ec = __eid, \
1651 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1652 | (__bv1) << 8 | (__bv2) }
1654 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1655 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1657 /* Outdated IBM BIOSes often lack the EC id string */
1658 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1659 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1660 __bv1, __bv2, TPID(__id1, __id2), \
1661 __ev1, __ev2), \
1662 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1663 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1664 __ev1, __ev2)
1666 /* Outdated IBM BIOSes often lack the EC id string */
1667 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1668 __eid1, __eid2, __ev1, __ev2) \
1669 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1670 __bv1, __bv2, TPID(__eid1, __eid2), \
1671 __ev1, __ev2), \
1672 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1673 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1674 __ev1, __ev2)
1676 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1677 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1679 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1680 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1681 __bv1, __bv2, TPID(__id1, __id2), \
1682 __ev1, __ev2)
1684 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1685 __eid1, __eid2, __ev1, __ev2) \
1686 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1687 __bv1, __bv2, TPID(__eid1, __eid2), \
1688 __ev1, __ev2)
1690 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1691 /* Numeric models ------------------ */
1692 /* FW MODEL BIOS VERS */
1693 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1694 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1695 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1696 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1697 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1698 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1699 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1700 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1701 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1703 /* A-series ------------------------- */
1704 /* FW MODEL BIOS VERS EC VERS */
1705 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1706 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1707 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1708 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1709 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1710 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1711 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1712 TPV_QI0('1', '3', '2', '0'), /* A22m */
1713 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1714 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1715 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1717 /* G-series ------------------------- */
1718 /* FW MODEL BIOS VERS */
1719 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1720 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1722 /* R-series, T-series --------------- */
1723 /* FW MODEL BIOS VERS EC VERS */
1724 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1725 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1726 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1727 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1728 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1729 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1730 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1731 T40/p, T41/p, T42/p (1) */
1732 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1733 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1734 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1735 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1737 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1738 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1739 TPV_QI0('1', '6', '3', '2'), /* T22 */
1740 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1741 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1742 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1744 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1745 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1746 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1748 /* BIOS FW BIOS VERS EC FW EC VERS */
1749 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1750 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1752 /* X-series ------------------------- */
1753 /* FW MODEL BIOS VERS EC VERS */
1754 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1755 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1756 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1757 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1758 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1759 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1760 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1762 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1763 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1765 /* (0) - older versions lack DMI EC fw string and functionality */
1766 /* (1) - older versions known to lack functionality */
1769 #undef TPV_QL1
1770 #undef TPV_QL0
1771 #undef TPV_QI2
1772 #undef TPV_QI1
1773 #undef TPV_QI0
1774 #undef TPV_Q_X
1775 #undef TPV_Q
1777 static void __init tpacpi_check_outdated_fw(void)
1779 unsigned long fwvers;
1780 u16 ec_version, bios_version;
1782 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1783 ARRAY_SIZE(tpacpi_bios_version_qtable));
1785 if (!fwvers)
1786 return;
1788 bios_version = fwvers & 0xffffU;
1789 ec_version = (fwvers >> 16) & 0xffffU;
1791 /* note that unknown versions are set to 0x0000 and we use that */
1792 if ((bios_version > thinkpad_id.bios_release) ||
1793 (ec_version > thinkpad_id.ec_release &&
1794 ec_version != TPACPI_MATCH_ANY)) {
1796 * The changelogs would let us track down the exact
1797 * reason, but it is just too much of a pain to track
1798 * it. We only list BIOSes that are either really
1799 * broken, or really stable to begin with, so it is
1800 * best if the user upgrades the firmware anyway.
1802 printk(TPACPI_WARN
1803 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1804 printk(TPACPI_WARN
1805 "WARNING: This firmware may be missing critical bug "
1806 "fixes and/or important features\n");
1810 static bool __init tpacpi_is_fw_known(void)
1812 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1813 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1816 /****************************************************************************
1817 ****************************************************************************
1819 * Subdrivers
1821 ****************************************************************************
1822 ****************************************************************************/
1824 /*************************************************************************
1825 * thinkpad-acpi metadata subdriver
1828 static int thinkpad_acpi_driver_read(struct seq_file *m)
1830 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1831 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1832 return 0;
1835 static struct ibm_struct thinkpad_acpi_driver_data = {
1836 .name = "driver",
1837 .read = thinkpad_acpi_driver_read,
1840 /*************************************************************************
1841 * Hotkey subdriver
1845 * ThinkPad firmware event model
1847 * The ThinkPad firmware has two main event interfaces: normal ACPI
1848 * notifications (which follow the ACPI standard), and a private event
1849 * interface.
1851 * The private event interface also issues events for the hotkeys. As
1852 * the driver gained features, the event handling code ended up being
1853 * built around the hotkey subdriver. This will need to be refactored
1854 * to a more formal event API eventually.
1856 * Some "hotkeys" are actually supposed to be used as event reports,
1857 * such as "brightness has changed", "volume has changed", depending on
1858 * the ThinkPad model and how the firmware is operating.
1860 * Unlike other classes, hotkey-class events have mask/unmask control on
1861 * non-ancient firmware. However, how it behaves changes a lot with the
1862 * firmware model and version.
1865 enum { /* hot key scan codes (derived from ACPI DSDT) */
1866 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1867 TP_ACPI_HOTKEYSCAN_FNF2,
1868 TP_ACPI_HOTKEYSCAN_FNF3,
1869 TP_ACPI_HOTKEYSCAN_FNF4,
1870 TP_ACPI_HOTKEYSCAN_FNF5,
1871 TP_ACPI_HOTKEYSCAN_FNF6,
1872 TP_ACPI_HOTKEYSCAN_FNF7,
1873 TP_ACPI_HOTKEYSCAN_FNF8,
1874 TP_ACPI_HOTKEYSCAN_FNF9,
1875 TP_ACPI_HOTKEYSCAN_FNF10,
1876 TP_ACPI_HOTKEYSCAN_FNF11,
1877 TP_ACPI_HOTKEYSCAN_FNF12,
1878 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1879 TP_ACPI_HOTKEYSCAN_FNINSERT,
1880 TP_ACPI_HOTKEYSCAN_FNDELETE,
1881 TP_ACPI_HOTKEYSCAN_FNHOME,
1882 TP_ACPI_HOTKEYSCAN_FNEND,
1883 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1884 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1885 TP_ACPI_HOTKEYSCAN_FNSPACE,
1886 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1887 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1888 TP_ACPI_HOTKEYSCAN_MUTE,
1889 TP_ACPI_HOTKEYSCAN_THINKPAD,
1892 enum { /* Keys/events available through NVRAM polling */
1893 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1894 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1897 enum { /* Positions of some of the keys in hotkey masks */
1898 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1899 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1900 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1901 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1902 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1903 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1904 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1905 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1906 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1907 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1908 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1911 enum { /* NVRAM to ACPI HKEY group map */
1912 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1913 TP_ACPI_HKEY_ZOOM_MASK |
1914 TP_ACPI_HKEY_DISPSWTCH_MASK |
1915 TP_ACPI_HKEY_HIBERNATE_MASK,
1916 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1917 TP_ACPI_HKEY_BRGHTDWN_MASK,
1918 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1919 TP_ACPI_HKEY_VOLDWN_MASK |
1920 TP_ACPI_HKEY_MUTE_MASK,
1923 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1924 struct tp_nvram_state {
1925 u16 thinkpad_toggle:1;
1926 u16 zoom_toggle:1;
1927 u16 display_toggle:1;
1928 u16 thinklight_toggle:1;
1929 u16 hibernate_toggle:1;
1930 u16 displayexp_toggle:1;
1931 u16 display_state:1;
1932 u16 brightness_toggle:1;
1933 u16 volume_toggle:1;
1934 u16 mute:1;
1936 u8 brightness_level;
1937 u8 volume_level;
1940 /* kthread for the hotkey poller */
1941 static struct task_struct *tpacpi_hotkey_task;
1943 /* Acquired while the poller kthread is running, use to sync start/stop */
1944 static struct mutex hotkey_thread_mutex;
1947 * Acquire mutex to write poller control variables as an
1948 * atomic block.
1950 * Increment hotkey_config_change when changing them if you
1951 * want the kthread to forget old state.
1953 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1955 static struct mutex hotkey_thread_data_mutex;
1956 static unsigned int hotkey_config_change;
1959 * hotkey poller control variables
1961 * Must be atomic or readers will also need to acquire mutex
1963 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1964 * should be used only when the changes need to be taken as
1965 * a block, OR when one needs to force the kthread to forget
1966 * old state.
1968 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1969 static unsigned int hotkey_poll_freq = 10; /* Hz */
1971 #define HOTKEY_CONFIG_CRITICAL_START \
1972 do { \
1973 mutex_lock(&hotkey_thread_data_mutex); \
1974 hotkey_config_change++; \
1975 } while (0);
1976 #define HOTKEY_CONFIG_CRITICAL_END \
1977 mutex_unlock(&hotkey_thread_data_mutex);
1979 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1981 #define hotkey_source_mask 0U
1982 #define HOTKEY_CONFIG_CRITICAL_START
1983 #define HOTKEY_CONFIG_CRITICAL_END
1985 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1987 static struct mutex hotkey_mutex;
1989 static enum { /* Reasons for waking up */
1990 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1991 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1992 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1993 } hotkey_wakeup_reason;
1995 static int hotkey_autosleep_ack;
1997 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1998 static u32 hotkey_all_mask; /* all events supported in fw */
1999 static u32 hotkey_reserved_mask; /* events better left disabled */
2000 static u32 hotkey_driver_mask; /* events needed by the driver */
2001 static u32 hotkey_user_mask; /* events visible to userspace */
2002 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2004 static unsigned int hotkey_report_mode;
2006 static u16 *hotkey_keycode_map;
2008 static struct attribute_set *hotkey_dev_attributes;
2010 static void tpacpi_driver_event(const unsigned int hkey_event);
2011 static void hotkey_driver_event(const unsigned int scancode);
2012 static void hotkey_poll_setup(const bool may_warn);
2014 /* HKEY.MHKG() return bits */
2015 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2017 static int hotkey_get_wlsw(void)
2019 int status;
2021 if (!tp_features.hotkey_wlsw)
2022 return -ENODEV;
2024 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2025 if (dbg_wlswemul)
2026 return (tpacpi_wlsw_emulstate) ?
2027 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2028 #endif
2030 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2031 return -EIO;
2033 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2036 static int hotkey_get_tablet_mode(int *status)
2038 int s;
2040 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2041 return -EIO;
2043 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2044 return 0;
2048 * Reads current event mask from firmware, and updates
2049 * hotkey_acpi_mask accordingly. Also resets any bits
2050 * from hotkey_user_mask that are unavailable to be
2051 * delivered (shadow requirement of the userspace ABI).
2053 * Call with hotkey_mutex held
2055 static int hotkey_mask_get(void)
2057 if (tp_features.hotkey_mask) {
2058 u32 m = 0;
2060 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2061 return -EIO;
2063 hotkey_acpi_mask = m;
2064 } else {
2065 /* no mask support doesn't mean no event support... */
2066 hotkey_acpi_mask = hotkey_all_mask;
2069 /* sync userspace-visible mask */
2070 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2072 return 0;
2075 void static hotkey_mask_warn_incomplete_mask(void)
2077 /* log only what the user can fix... */
2078 const u32 wantedmask = hotkey_driver_mask &
2079 ~(hotkey_acpi_mask | hotkey_source_mask) &
2080 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2082 if (wantedmask)
2083 printk(TPACPI_NOTICE
2084 "required events 0x%08x not enabled!\n",
2085 wantedmask);
2089 * Set the firmware mask when supported
2091 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2093 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2095 * Call with hotkey_mutex held
2097 static int hotkey_mask_set(u32 mask)
2099 int i;
2100 int rc = 0;
2102 const u32 fwmask = mask & ~hotkey_source_mask;
2104 if (tp_features.hotkey_mask) {
2105 for (i = 0; i < 32; i++) {
2106 if (!acpi_evalf(hkey_handle,
2107 NULL, "MHKM", "vdd", i + 1,
2108 !!(mask & (1 << i)))) {
2109 rc = -EIO;
2110 break;
2116 * We *must* make an inconditional call to hotkey_mask_get to
2117 * refresh hotkey_acpi_mask and update hotkey_user_mask
2119 * Take the opportunity to also log when we cannot _enable_
2120 * a given event.
2122 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2123 printk(TPACPI_NOTICE
2124 "asked for hotkey mask 0x%08x, but "
2125 "firmware forced it to 0x%08x\n",
2126 fwmask, hotkey_acpi_mask);
2129 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2130 hotkey_mask_warn_incomplete_mask();
2132 return rc;
2136 * Sets hotkey_user_mask and tries to set the firmware mask
2138 * Call with hotkey_mutex held
2140 static int hotkey_user_mask_set(const u32 mask)
2142 int rc;
2144 /* Give people a chance to notice they are doing something that
2145 * is bound to go boom on their users sooner or later */
2146 if (!tp_warned.hotkey_mask_ff &&
2147 (mask == 0xffff || mask == 0xffffff ||
2148 mask == 0xffffffff)) {
2149 tp_warned.hotkey_mask_ff = 1;
2150 printk(TPACPI_NOTICE
2151 "setting the hotkey mask to 0x%08x is likely "
2152 "not the best way to go about it\n", mask);
2153 printk(TPACPI_NOTICE
2154 "please consider using the driver defaults, "
2155 "and refer to up-to-date thinkpad-acpi "
2156 "documentation\n");
2159 /* Try to enable what the user asked for, plus whatever we need.
2160 * this syncs everything but won't enable bits in hotkey_user_mask */
2161 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2163 /* Enable the available bits in hotkey_user_mask */
2164 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2166 return rc;
2170 * Sets the driver hotkey mask.
2172 * Can be called even if the hotkey subdriver is inactive
2174 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2176 int rc;
2178 /* Do the right thing if hotkey_init has not been called yet */
2179 if (!tp_features.hotkey) {
2180 hotkey_driver_mask = mask;
2181 return 0;
2184 mutex_lock(&hotkey_mutex);
2186 HOTKEY_CONFIG_CRITICAL_START
2187 hotkey_driver_mask = mask;
2188 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2189 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2190 #endif
2191 HOTKEY_CONFIG_CRITICAL_END
2193 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2194 ~hotkey_source_mask);
2195 hotkey_poll_setup(true);
2197 mutex_unlock(&hotkey_mutex);
2199 return rc;
2202 static int hotkey_status_get(int *status)
2204 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2205 return -EIO;
2207 return 0;
2210 static int hotkey_status_set(bool enable)
2212 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2213 return -EIO;
2215 return 0;
2218 static void tpacpi_input_send_tabletsw(void)
2220 int state;
2222 if (tp_features.hotkey_tablet &&
2223 !hotkey_get_tablet_mode(&state)) {
2224 mutex_lock(&tpacpi_inputdev_send_mutex);
2226 input_report_switch(tpacpi_inputdev,
2227 SW_TABLET_MODE, !!state);
2228 input_sync(tpacpi_inputdev);
2230 mutex_unlock(&tpacpi_inputdev_send_mutex);
2234 /* Do NOT call without validating scancode first */
2235 static void tpacpi_input_send_key(const unsigned int scancode)
2237 const unsigned int keycode = hotkey_keycode_map[scancode];
2239 if (keycode != KEY_RESERVED) {
2240 mutex_lock(&tpacpi_inputdev_send_mutex);
2242 input_report_key(tpacpi_inputdev, keycode, 1);
2243 if (keycode == KEY_UNKNOWN)
2244 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2245 scancode);
2246 input_sync(tpacpi_inputdev);
2248 input_report_key(tpacpi_inputdev, keycode, 0);
2249 if (keycode == KEY_UNKNOWN)
2250 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2251 scancode);
2252 input_sync(tpacpi_inputdev);
2254 mutex_unlock(&tpacpi_inputdev_send_mutex);
2258 /* Do NOT call without validating scancode first */
2259 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2261 hotkey_driver_event(scancode);
2262 if (hotkey_user_mask & (1 << scancode))
2263 tpacpi_input_send_key(scancode);
2266 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2267 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2269 /* Do NOT call without validating scancode first */
2270 static void tpacpi_hotkey_send_key(unsigned int scancode)
2272 tpacpi_input_send_key_masked(scancode);
2273 if (hotkey_report_mode < 2) {
2274 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2275 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2279 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2281 u8 d;
2283 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2284 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2285 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2286 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2287 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2288 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2290 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2291 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2292 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2294 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2295 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2296 n->displayexp_toggle =
2297 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2299 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2300 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2301 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2302 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2303 n->brightness_toggle =
2304 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2306 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2307 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2308 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2309 >> TP_NVRAM_POS_LEVEL_VOLUME;
2310 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2311 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2315 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2316 struct tp_nvram_state *newn,
2317 const u32 event_mask)
2320 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2321 do { \
2322 if ((event_mask & (1 << __scancode)) && \
2323 oldn->__member != newn->__member) \
2324 tpacpi_hotkey_send_key(__scancode); \
2325 } while (0)
2327 #define TPACPI_MAY_SEND_KEY(__scancode) \
2328 do { \
2329 if (event_mask & (1 << __scancode)) \
2330 tpacpi_hotkey_send_key(__scancode); \
2331 } while (0)
2333 void issue_volchange(const unsigned int oldvol,
2334 const unsigned int newvol)
2336 unsigned int i = oldvol;
2338 while (i > newvol) {
2339 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2340 i--;
2342 while (i < newvol) {
2343 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2344 i++;
2348 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2349 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2350 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2351 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2353 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2355 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2358 * Handle volume
2360 * This code is supposed to duplicate the IBM firmware behaviour:
2361 * - Pressing MUTE issues mute hotkey message, even when already mute
2362 * - Pressing Volume up/down issues volume up/down hotkey messages,
2363 * even when already at maximum or minumum volume
2364 * - The act of unmuting issues volume up/down notification,
2365 * depending which key was used to unmute
2367 * We are constrained to what the NVRAM can tell us, which is not much
2368 * and certainly not enough if more than one volume hotkey was pressed
2369 * since the last poll cycle.
2371 * Just to make our life interesting, some newer Lenovo ThinkPads have
2372 * bugs in the BIOS and may fail to update volume_toggle properly.
2374 if (newn->mute) {
2375 /* muted */
2376 if (!oldn->mute ||
2377 oldn->volume_toggle != newn->volume_toggle ||
2378 oldn->volume_level != newn->volume_level) {
2379 /* recently muted, or repeated mute keypress, or
2380 * multiple presses ending in mute */
2381 issue_volchange(oldn->volume_level, newn->volume_level);
2382 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2384 } else {
2385 /* unmute */
2386 if (oldn->mute) {
2387 /* recently unmuted, issue 'unmute' keypress */
2388 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2390 if (oldn->volume_level != newn->volume_level) {
2391 issue_volchange(oldn->volume_level, newn->volume_level);
2392 } else if (oldn->volume_toggle != newn->volume_toggle) {
2393 /* repeated vol up/down keypress at end of scale ? */
2394 if (newn->volume_level == 0)
2395 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2396 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2397 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2401 /* handle brightness */
2402 if (oldn->brightness_toggle != newn->brightness_toggle) {
2403 if (oldn->brightness_level < newn->brightness_level) {
2404 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2405 } else if (oldn->brightness_level > newn->brightness_level) {
2406 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2407 } else {
2408 /* repeated key presses that didn't change state */
2409 if (newn->brightness_level != 0) {
2410 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2411 } else {
2412 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2417 #undef TPACPI_COMPARE_KEY
2418 #undef TPACPI_MAY_SEND_KEY
2422 * Polling driver
2424 * We track all events in hotkey_source_mask all the time, since
2425 * most of them are edge-based. We only issue those requested by
2426 * hotkey_user_mask or hotkey_driver_mask, though.
2428 static int hotkey_kthread(void *data)
2430 struct tp_nvram_state s[2];
2431 u32 poll_mask, event_mask;
2432 unsigned int si, so;
2433 unsigned long t;
2434 unsigned int change_detector, must_reset;
2435 unsigned int poll_freq;
2437 mutex_lock(&hotkey_thread_mutex);
2439 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2440 goto exit;
2442 set_freezable();
2444 so = 0;
2445 si = 1;
2446 t = 0;
2448 /* Initial state for compares */
2449 mutex_lock(&hotkey_thread_data_mutex);
2450 change_detector = hotkey_config_change;
2451 poll_mask = hotkey_source_mask;
2452 event_mask = hotkey_source_mask &
2453 (hotkey_driver_mask | hotkey_user_mask);
2454 poll_freq = hotkey_poll_freq;
2455 mutex_unlock(&hotkey_thread_data_mutex);
2456 hotkey_read_nvram(&s[so], poll_mask);
2458 while (!kthread_should_stop()) {
2459 if (t == 0) {
2460 if (likely(poll_freq))
2461 t = 1000/poll_freq;
2462 else
2463 t = 100; /* should never happen... */
2465 t = msleep_interruptible(t);
2466 if (unlikely(kthread_should_stop()))
2467 break;
2468 must_reset = try_to_freeze();
2469 if (t > 0 && !must_reset)
2470 continue;
2472 mutex_lock(&hotkey_thread_data_mutex);
2473 if (must_reset || hotkey_config_change != change_detector) {
2474 /* forget old state on thaw or config change */
2475 si = so;
2476 t = 0;
2477 change_detector = hotkey_config_change;
2479 poll_mask = hotkey_source_mask;
2480 event_mask = hotkey_source_mask &
2481 (hotkey_driver_mask | hotkey_user_mask);
2482 poll_freq = hotkey_poll_freq;
2483 mutex_unlock(&hotkey_thread_data_mutex);
2485 if (likely(poll_mask)) {
2486 hotkey_read_nvram(&s[si], poll_mask);
2487 if (likely(si != so)) {
2488 hotkey_compare_and_issue_event(&s[so], &s[si],
2489 event_mask);
2493 so = si;
2494 si ^= 1;
2497 exit:
2498 mutex_unlock(&hotkey_thread_mutex);
2499 return 0;
2502 /* call with hotkey_mutex held */
2503 static void hotkey_poll_stop_sync(void)
2505 if (tpacpi_hotkey_task) {
2506 if (frozen(tpacpi_hotkey_task) ||
2507 freezing(tpacpi_hotkey_task))
2508 thaw_process(tpacpi_hotkey_task);
2510 kthread_stop(tpacpi_hotkey_task);
2511 tpacpi_hotkey_task = NULL;
2512 mutex_lock(&hotkey_thread_mutex);
2513 /* at this point, the thread did exit */
2514 mutex_unlock(&hotkey_thread_mutex);
2518 /* call with hotkey_mutex held */
2519 static void hotkey_poll_setup(const bool may_warn)
2521 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2522 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2524 if (hotkey_poll_freq > 0 &&
2525 (poll_driver_mask ||
2526 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2527 if (!tpacpi_hotkey_task) {
2528 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2529 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2530 if (IS_ERR(tpacpi_hotkey_task)) {
2531 tpacpi_hotkey_task = NULL;
2532 printk(TPACPI_ERR
2533 "could not create kernel thread "
2534 "for hotkey polling\n");
2537 } else {
2538 hotkey_poll_stop_sync();
2539 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2540 hotkey_poll_freq == 0) {
2541 printk(TPACPI_NOTICE
2542 "hot keys 0x%08x and/or events 0x%08x "
2543 "require polling, which is currently "
2544 "disabled\n",
2545 poll_user_mask, poll_driver_mask);
2550 static void hotkey_poll_setup_safe(const bool may_warn)
2552 mutex_lock(&hotkey_mutex);
2553 hotkey_poll_setup(may_warn);
2554 mutex_unlock(&hotkey_mutex);
2557 /* call with hotkey_mutex held */
2558 static void hotkey_poll_set_freq(unsigned int freq)
2560 if (!freq)
2561 hotkey_poll_stop_sync();
2563 hotkey_poll_freq = freq;
2566 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2568 static void hotkey_poll_setup(const bool __unused)
2572 static void hotkey_poll_setup_safe(const bool __unused)
2576 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2578 static int hotkey_inputdev_open(struct input_dev *dev)
2580 switch (tpacpi_lifecycle) {
2581 case TPACPI_LIFE_INIT:
2582 case TPACPI_LIFE_RUNNING:
2583 hotkey_poll_setup_safe(false);
2584 return 0;
2585 case TPACPI_LIFE_EXITING:
2586 return -EBUSY;
2589 /* Should only happen if tpacpi_lifecycle is corrupt */
2590 BUG();
2591 return -EBUSY;
2594 static void hotkey_inputdev_close(struct input_dev *dev)
2596 /* disable hotkey polling when possible */
2597 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2598 !(hotkey_source_mask & hotkey_driver_mask))
2599 hotkey_poll_setup_safe(false);
2602 /* sysfs hotkey enable ------------------------------------------------- */
2603 static ssize_t hotkey_enable_show(struct device *dev,
2604 struct device_attribute *attr,
2605 char *buf)
2607 int res, status;
2609 printk_deprecated_attribute("hotkey_enable",
2610 "Hotkey reporting is always enabled");
2612 res = hotkey_status_get(&status);
2613 if (res)
2614 return res;
2616 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2619 static ssize_t hotkey_enable_store(struct device *dev,
2620 struct device_attribute *attr,
2621 const char *buf, size_t count)
2623 unsigned long t;
2625 printk_deprecated_attribute("hotkey_enable",
2626 "Hotkeys can be disabled through hotkey_mask");
2628 if (parse_strtoul(buf, 1, &t))
2629 return -EINVAL;
2631 if (t == 0)
2632 return -EPERM;
2634 return count;
2637 static struct device_attribute dev_attr_hotkey_enable =
2638 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2639 hotkey_enable_show, hotkey_enable_store);
2641 /* sysfs hotkey mask --------------------------------------------------- */
2642 static ssize_t hotkey_mask_show(struct device *dev,
2643 struct device_attribute *attr,
2644 char *buf)
2646 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2649 static ssize_t hotkey_mask_store(struct device *dev,
2650 struct device_attribute *attr,
2651 const char *buf, size_t count)
2653 unsigned long t;
2654 int res;
2656 if (parse_strtoul(buf, 0xffffffffUL, &t))
2657 return -EINVAL;
2659 if (mutex_lock_killable(&hotkey_mutex))
2660 return -ERESTARTSYS;
2662 res = hotkey_user_mask_set(t);
2664 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2665 hotkey_poll_setup(true);
2666 #endif
2668 mutex_unlock(&hotkey_mutex);
2670 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2672 return (res) ? res : count;
2675 static struct device_attribute dev_attr_hotkey_mask =
2676 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2677 hotkey_mask_show, hotkey_mask_store);
2679 /* sysfs hotkey bios_enabled ------------------------------------------- */
2680 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2681 struct device_attribute *attr,
2682 char *buf)
2684 return sprintf(buf, "0\n");
2687 static struct device_attribute dev_attr_hotkey_bios_enabled =
2688 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2690 /* sysfs hotkey bios_mask ---------------------------------------------- */
2691 static ssize_t hotkey_bios_mask_show(struct device *dev,
2692 struct device_attribute *attr,
2693 char *buf)
2695 printk_deprecated_attribute("hotkey_bios_mask",
2696 "This attribute is useless.");
2697 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2700 static struct device_attribute dev_attr_hotkey_bios_mask =
2701 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2703 /* sysfs hotkey all_mask ----------------------------------------------- */
2704 static ssize_t hotkey_all_mask_show(struct device *dev,
2705 struct device_attribute *attr,
2706 char *buf)
2708 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2709 hotkey_all_mask | hotkey_source_mask);
2712 static struct device_attribute dev_attr_hotkey_all_mask =
2713 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2715 /* sysfs hotkey recommended_mask --------------------------------------- */
2716 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2717 struct device_attribute *attr,
2718 char *buf)
2720 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2721 (hotkey_all_mask | hotkey_source_mask)
2722 & ~hotkey_reserved_mask);
2725 static struct device_attribute dev_attr_hotkey_recommended_mask =
2726 __ATTR(hotkey_recommended_mask, S_IRUGO,
2727 hotkey_recommended_mask_show, NULL);
2729 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2731 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2732 static ssize_t hotkey_source_mask_show(struct device *dev,
2733 struct device_attribute *attr,
2734 char *buf)
2736 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2739 static ssize_t hotkey_source_mask_store(struct device *dev,
2740 struct device_attribute *attr,
2741 const char *buf, size_t count)
2743 unsigned long t;
2744 u32 r_ev;
2745 int rc;
2747 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2748 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2749 return -EINVAL;
2751 if (mutex_lock_killable(&hotkey_mutex))
2752 return -ERESTARTSYS;
2754 HOTKEY_CONFIG_CRITICAL_START
2755 hotkey_source_mask = t;
2756 HOTKEY_CONFIG_CRITICAL_END
2758 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2759 ~hotkey_source_mask);
2760 hotkey_poll_setup(true);
2762 /* check if events needed by the driver got disabled */
2763 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2764 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2766 mutex_unlock(&hotkey_mutex);
2768 if (rc < 0)
2769 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2770 "firmware event mask!\n");
2772 if (r_ev)
2773 printk(TPACPI_NOTICE "hotkey_source_mask: "
2774 "some important events were disabled: "
2775 "0x%04x\n", r_ev);
2777 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2779 return (rc < 0) ? rc : count;
2782 static struct device_attribute dev_attr_hotkey_source_mask =
2783 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2784 hotkey_source_mask_show, hotkey_source_mask_store);
2786 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2787 static ssize_t hotkey_poll_freq_show(struct device *dev,
2788 struct device_attribute *attr,
2789 char *buf)
2791 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2794 static ssize_t hotkey_poll_freq_store(struct device *dev,
2795 struct device_attribute *attr,
2796 const char *buf, size_t count)
2798 unsigned long t;
2800 if (parse_strtoul(buf, 25, &t))
2801 return -EINVAL;
2803 if (mutex_lock_killable(&hotkey_mutex))
2804 return -ERESTARTSYS;
2806 hotkey_poll_set_freq(t);
2807 hotkey_poll_setup(true);
2809 mutex_unlock(&hotkey_mutex);
2811 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2813 return count;
2816 static struct device_attribute dev_attr_hotkey_poll_freq =
2817 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2818 hotkey_poll_freq_show, hotkey_poll_freq_store);
2820 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2822 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2823 static ssize_t hotkey_radio_sw_show(struct device *dev,
2824 struct device_attribute *attr,
2825 char *buf)
2827 int res;
2828 res = hotkey_get_wlsw();
2829 if (res < 0)
2830 return res;
2832 /* Opportunistic update */
2833 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2835 return snprintf(buf, PAGE_SIZE, "%d\n",
2836 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2839 static struct device_attribute dev_attr_hotkey_radio_sw =
2840 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2842 static void hotkey_radio_sw_notify_change(void)
2844 if (tp_features.hotkey_wlsw)
2845 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2846 "hotkey_radio_sw");
2849 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2850 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2851 struct device_attribute *attr,
2852 char *buf)
2854 int res, s;
2855 res = hotkey_get_tablet_mode(&s);
2856 if (res < 0)
2857 return res;
2859 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2862 static struct device_attribute dev_attr_hotkey_tablet_mode =
2863 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2865 static void hotkey_tablet_mode_notify_change(void)
2867 if (tp_features.hotkey_tablet)
2868 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2869 "hotkey_tablet_mode");
2872 /* sysfs hotkey report_mode -------------------------------------------- */
2873 static ssize_t hotkey_report_mode_show(struct device *dev,
2874 struct device_attribute *attr,
2875 char *buf)
2877 return snprintf(buf, PAGE_SIZE, "%d\n",
2878 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2881 static struct device_attribute dev_attr_hotkey_report_mode =
2882 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2884 /* sysfs wakeup reason (pollable) -------------------------------------- */
2885 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2886 struct device_attribute *attr,
2887 char *buf)
2889 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2892 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2893 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2895 static void hotkey_wakeup_reason_notify_change(void)
2897 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2898 "wakeup_reason");
2901 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2902 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2903 struct device_attribute *attr,
2904 char *buf)
2906 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2909 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2910 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2911 hotkey_wakeup_hotunplug_complete_show, NULL);
2913 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2915 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2916 "wakeup_hotunplug_complete");
2919 /* --------------------------------------------------------------------- */
2921 static struct attribute *hotkey_attributes[] __initdata = {
2922 &dev_attr_hotkey_enable.attr,
2923 &dev_attr_hotkey_bios_enabled.attr,
2924 &dev_attr_hotkey_bios_mask.attr,
2925 &dev_attr_hotkey_report_mode.attr,
2926 &dev_attr_hotkey_wakeup_reason.attr,
2927 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2928 &dev_attr_hotkey_mask.attr,
2929 &dev_attr_hotkey_all_mask.attr,
2930 &dev_attr_hotkey_recommended_mask.attr,
2931 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2932 &dev_attr_hotkey_source_mask.attr,
2933 &dev_attr_hotkey_poll_freq.attr,
2934 #endif
2938 * Sync both the hw and sw blocking state of all switches
2940 static void tpacpi_send_radiosw_update(void)
2942 int wlsw;
2945 * We must sync all rfkill controllers *before* issuing any
2946 * rfkill input events, or we will race the rfkill core input
2947 * handler.
2949 * tpacpi_inputdev_send_mutex works as a syncronization point
2950 * for the above.
2952 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2955 wlsw = hotkey_get_wlsw();
2957 /* Sync hw blocking state first if it is hw-blocked */
2958 if (wlsw == TPACPI_RFK_RADIO_OFF)
2959 tpacpi_rfk_update_hwblock_state(true);
2961 /* Sync sw blocking state */
2962 tpacpi_rfk_update_swstate_all();
2964 /* Sync hw blocking state last if it is hw-unblocked */
2965 if (wlsw == TPACPI_RFK_RADIO_ON)
2966 tpacpi_rfk_update_hwblock_state(false);
2968 /* Issue rfkill input event for WLSW switch */
2969 if (!(wlsw < 0)) {
2970 mutex_lock(&tpacpi_inputdev_send_mutex);
2972 input_report_switch(tpacpi_inputdev,
2973 SW_RFKILL_ALL, (wlsw > 0));
2974 input_sync(tpacpi_inputdev);
2976 mutex_unlock(&tpacpi_inputdev_send_mutex);
2980 * this can be unconditional, as we will poll state again
2981 * if userspace uses the notify to read data
2983 hotkey_radio_sw_notify_change();
2986 static void hotkey_exit(void)
2988 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2989 mutex_lock(&hotkey_mutex);
2990 hotkey_poll_stop_sync();
2991 mutex_unlock(&hotkey_mutex);
2992 #endif
2994 if (hotkey_dev_attributes)
2995 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2997 kfree(hotkey_keycode_map);
2999 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3000 "restoring original HKEY status and mask\n");
3001 /* yes, there is a bitwise or below, we want the
3002 * functions to be called even if one of them fail */
3003 if (((tp_features.hotkey_mask &&
3004 hotkey_mask_set(hotkey_orig_mask)) |
3005 hotkey_status_set(false)) != 0)
3006 printk(TPACPI_ERR
3007 "failed to restore hot key mask "
3008 "to BIOS defaults\n");
3011 static void __init hotkey_unmap(const unsigned int scancode)
3013 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3014 clear_bit(hotkey_keycode_map[scancode],
3015 tpacpi_inputdev->keybit);
3016 hotkey_keycode_map[scancode] = KEY_RESERVED;
3021 * HKEY quirks:
3022 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3025 #define TPACPI_HK_Q_INIMASK 0x0001
3027 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3028 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3029 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3030 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3031 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3032 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3033 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3034 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3035 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3036 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3037 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3038 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3039 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3040 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3041 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3042 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3043 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3044 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3045 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3046 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3049 static int __init hotkey_init(struct ibm_init_struct *iibm)
3051 /* Requirements for changing the default keymaps:
3053 * 1. Many of the keys are mapped to KEY_RESERVED for very
3054 * good reasons. Do not change them unless you have deep
3055 * knowledge on the IBM and Lenovo ThinkPad firmware for
3056 * the various ThinkPad models. The driver behaves
3057 * differently for KEY_RESERVED: such keys have their
3058 * hot key mask *unset* in mask_recommended, and also
3059 * in the initial hot key mask programmed into the
3060 * firmware at driver load time, which means the firm-
3061 * ware may react very differently if you change them to
3062 * something else;
3064 * 2. You must be subscribed to the linux-thinkpad and
3065 * ibm-acpi-devel mailing lists, and you should read the
3066 * list archives since 2007 if you want to change the
3067 * keymaps. This requirement exists so that you will
3068 * know the past history of problems with the thinkpad-
3069 * acpi driver keymaps, and also that you will be
3070 * listening to any bug reports;
3072 * 3. Do not send thinkpad-acpi specific patches directly to
3073 * for merging, *ever*. Send them to the linux-acpi
3074 * mailinglist for comments. Merging is to be done only
3075 * through acpi-test and the ACPI maintainer.
3077 * If the above is too much to ask, don't change the keymap.
3078 * Ask the thinkpad-acpi maintainer to do it, instead.
3080 static u16 ibm_keycode_map[] __initdata = {
3081 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3082 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3083 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3084 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3086 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3087 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3088 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3089 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3091 /* brightness: firmware always reacts to them */
3092 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3093 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3095 /* Thinklight: firmware always react to it */
3096 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3098 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3099 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3101 /* Volume: firmware always react to it and reprograms
3102 * the built-in *extra* mixer. Never map it to control
3103 * another mixer by default. */
3104 KEY_RESERVED, /* 0x14: VOLUME UP */
3105 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3106 KEY_RESERVED, /* 0x16: MUTE */
3108 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3110 /* (assignments unknown, please report if found) */
3111 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3112 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3114 static u16 lenovo_keycode_map[] __initdata = {
3115 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3116 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3117 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3118 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3120 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3121 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3122 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3123 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3125 /* These should be enabled --only-- when ACPI video
3126 * is disabled (i.e. in "vendor" mode), and are handled
3127 * in a special way by the init code */
3128 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3129 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3131 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3133 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3134 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3136 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3137 * react to it and reprograms the built-in *extra* mixer.
3138 * Never map it to control another mixer by default.
3140 * T60?, T61, R60?, R61: firmware and EC tries to send
3141 * these over the regular keyboard, so these are no-ops,
3142 * but there are still weird bugs re. MUTE, so do not
3143 * change unless you get test reports from all Lenovo
3144 * models. May cause the BIOS to interfere with the
3145 * HDA mixer.
3147 KEY_RESERVED, /* 0x14: VOLUME UP */
3148 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3149 KEY_RESERVED, /* 0x16: MUTE */
3151 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3153 /* (assignments unknown, please report if found) */
3154 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3155 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3158 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3159 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3160 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3162 int res, i;
3163 int status;
3164 int hkeyv;
3165 bool radiosw_state = false;
3166 bool tabletsw_state = false;
3168 unsigned long quirks;
3170 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3171 "initializing hotkey subdriver\n");
3173 BUG_ON(!tpacpi_inputdev);
3174 BUG_ON(tpacpi_inputdev->open != NULL ||
3175 tpacpi_inputdev->close != NULL);
3177 TPACPI_ACPIHANDLE_INIT(hkey);
3178 mutex_init(&hotkey_mutex);
3180 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3181 mutex_init(&hotkey_thread_mutex);
3182 mutex_init(&hotkey_thread_data_mutex);
3183 #endif
3185 /* hotkey not supported on 570 */
3186 tp_features.hotkey = hkey_handle != NULL;
3188 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3189 "hotkeys are %s\n",
3190 str_supported(tp_features.hotkey));
3192 if (!tp_features.hotkey)
3193 return 1;
3195 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3196 ARRAY_SIZE(tpacpi_hotkey_qtable));
3198 tpacpi_disable_brightness_delay();
3200 /* MUST have enough space for all attributes to be added to
3201 * hotkey_dev_attributes */
3202 hotkey_dev_attributes = create_attr_set(
3203 ARRAY_SIZE(hotkey_attributes) + 2,
3204 NULL);
3205 if (!hotkey_dev_attributes)
3206 return -ENOMEM;
3207 res = add_many_to_attr_set(hotkey_dev_attributes,
3208 hotkey_attributes,
3209 ARRAY_SIZE(hotkey_attributes));
3210 if (res)
3211 goto err_exit;
3213 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3214 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3215 for HKEY interface version 0x100 */
3216 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3217 if ((hkeyv >> 8) != 1) {
3218 printk(TPACPI_ERR "unknown version of the "
3219 "HKEY interface: 0x%x\n", hkeyv);
3220 printk(TPACPI_ERR "please report this to %s\n",
3221 TPACPI_MAIL);
3222 } else {
3224 * MHKV 0x100 in A31, R40, R40e,
3225 * T4x, X31, and later
3227 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3228 "firmware HKEY interface version: 0x%x\n",
3229 hkeyv);
3231 /* Paranoia check AND init hotkey_all_mask */
3232 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3233 "MHKA", "qd")) {
3234 printk(TPACPI_ERR
3235 "missing MHKA handler, "
3236 "please report this to %s\n",
3237 TPACPI_MAIL);
3238 /* Fallback: pre-init for FN+F3,F4,F12 */
3239 hotkey_all_mask = 0x080cU;
3240 } else {
3241 tp_features.hotkey_mask = 1;
3246 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3247 "hotkey masks are %s\n",
3248 str_supported(tp_features.hotkey_mask));
3250 /* Init hotkey_all_mask if not initialized yet */
3251 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3252 (quirks & TPACPI_HK_Q_INIMASK))
3253 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3255 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3256 if (tp_features.hotkey_mask) {
3257 /* hotkey_source_mask *must* be zero for
3258 * the first hotkey_mask_get to return hotkey_orig_mask */
3259 res = hotkey_mask_get();
3260 if (res)
3261 goto err_exit;
3263 hotkey_orig_mask = hotkey_acpi_mask;
3264 } else {
3265 hotkey_orig_mask = hotkey_all_mask;
3266 hotkey_acpi_mask = hotkey_all_mask;
3269 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3270 if (dbg_wlswemul) {
3271 tp_features.hotkey_wlsw = 1;
3272 radiosw_state = !!tpacpi_wlsw_emulstate;
3273 printk(TPACPI_INFO
3274 "radio switch emulation enabled\n");
3275 } else
3276 #endif
3277 /* Not all thinkpads have a hardware radio switch */
3278 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3279 tp_features.hotkey_wlsw = 1;
3280 radiosw_state = !!status;
3281 printk(TPACPI_INFO
3282 "radio switch found; radios are %s\n",
3283 enabled(status, 0));
3285 if (tp_features.hotkey_wlsw)
3286 res = add_to_attr_set(hotkey_dev_attributes,
3287 &dev_attr_hotkey_radio_sw.attr);
3289 /* For X41t, X60t, X61t Tablets... */
3290 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3291 tp_features.hotkey_tablet = 1;
3292 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3293 printk(TPACPI_INFO
3294 "possible tablet mode switch found; "
3295 "ThinkPad in %s mode\n",
3296 (tabletsw_state) ? "tablet" : "laptop");
3297 res = add_to_attr_set(hotkey_dev_attributes,
3298 &dev_attr_hotkey_tablet_mode.attr);
3301 if (!res)
3302 res = register_attr_set_with_sysfs(
3303 hotkey_dev_attributes,
3304 &tpacpi_pdev->dev.kobj);
3305 if (res)
3306 goto err_exit;
3308 /* Set up key map */
3310 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3311 GFP_KERNEL);
3312 if (!hotkey_keycode_map) {
3313 printk(TPACPI_ERR
3314 "failed to allocate memory for key map\n");
3315 res = -ENOMEM;
3316 goto err_exit;
3319 if (tpacpi_is_lenovo()) {
3320 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3321 "using Lenovo default hot key map\n");
3322 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3323 TPACPI_HOTKEY_MAP_SIZE);
3324 } else {
3325 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3326 "using IBM default hot key map\n");
3327 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3328 TPACPI_HOTKEY_MAP_SIZE);
3331 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3332 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3333 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3334 tpacpi_inputdev->keycode = hotkey_keycode_map;
3335 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3336 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3337 input_set_capability(tpacpi_inputdev, EV_KEY,
3338 hotkey_keycode_map[i]);
3339 } else {
3340 if (i < sizeof(hotkey_reserved_mask)*8)
3341 hotkey_reserved_mask |= 1 << i;
3345 if (tp_features.hotkey_wlsw) {
3346 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3347 input_report_switch(tpacpi_inputdev,
3348 SW_RFKILL_ALL, radiosw_state);
3350 if (tp_features.hotkey_tablet) {
3351 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3352 input_report_switch(tpacpi_inputdev,
3353 SW_TABLET_MODE, tabletsw_state);
3356 /* Do not issue duplicate brightness change events to
3357 * userspace. tpacpi_detect_brightness_capabilities() must have
3358 * been called before this point */
3359 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3360 printk(TPACPI_INFO
3361 "This ThinkPad has standard ACPI backlight "
3362 "brightness control, supported by the ACPI "
3363 "video driver\n");
3364 printk(TPACPI_NOTICE
3365 "Disabling thinkpad-acpi brightness events "
3366 "by default...\n");
3368 /* Disable brightness up/down on Lenovo thinkpads when
3369 * ACPI is handling them, otherwise it is plain impossible
3370 * for userspace to do something even remotely sane */
3371 hotkey_reserved_mask |=
3372 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3373 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3374 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3375 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3378 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3379 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3380 & ~hotkey_all_mask
3381 & ~hotkey_reserved_mask;
3383 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3384 "hotkey source mask 0x%08x, polling freq %u\n",
3385 hotkey_source_mask, hotkey_poll_freq);
3386 #endif
3388 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3389 "enabling firmware HKEY event interface...\n");
3390 res = hotkey_status_set(true);
3391 if (res) {
3392 hotkey_exit();
3393 return res;
3395 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3396 | hotkey_driver_mask)
3397 & ~hotkey_source_mask);
3398 if (res < 0 && res != -ENXIO) {
3399 hotkey_exit();
3400 return res;
3402 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3403 & ~hotkey_reserved_mask;
3404 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3405 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3406 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3408 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3409 "legacy ibm/hotkey event reporting over procfs %s\n",
3410 (hotkey_report_mode < 2) ?
3411 "enabled" : "disabled");
3413 tpacpi_inputdev->open = &hotkey_inputdev_open;
3414 tpacpi_inputdev->close = &hotkey_inputdev_close;
3416 hotkey_poll_setup_safe(true);
3418 return 0;
3420 err_exit:
3421 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3422 hotkey_dev_attributes = NULL;
3424 return (res < 0)? res : 1;
3427 static bool hotkey_notify_hotkey(const u32 hkey,
3428 bool *send_acpi_ev,
3429 bool *ignore_acpi_ev)
3431 /* 0x1000-0x1FFF: key presses */
3432 unsigned int scancode = hkey & 0xfff;
3433 *send_acpi_ev = true;
3434 *ignore_acpi_ev = false;
3436 if (scancode > 0 && scancode < 0x21) {
3437 scancode--;
3438 if (!(hotkey_source_mask & (1 << scancode))) {
3439 tpacpi_input_send_key_masked(scancode);
3440 *send_acpi_ev = false;
3441 } else {
3442 *ignore_acpi_ev = true;
3444 return true;
3446 return false;
3449 static bool hotkey_notify_wakeup(const u32 hkey,
3450 bool *send_acpi_ev,
3451 bool *ignore_acpi_ev)
3453 /* 0x2000-0x2FFF: Wakeup reason */
3454 *send_acpi_ev = true;
3455 *ignore_acpi_ev = false;
3457 switch (hkey) {
3458 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3459 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3460 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3461 *ignore_acpi_ev = true;
3462 break;
3464 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3465 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3466 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3467 *ignore_acpi_ev = true;
3468 break;
3470 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3471 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3472 printk(TPACPI_ALERT
3473 "EMERGENCY WAKEUP: battery almost empty\n");
3474 /* how to auto-heal: */
3475 /* 2313: woke up from S3, go to S4/S5 */
3476 /* 2413: woke up from S4, go to S5 */
3477 break;
3479 default:
3480 return false;
3483 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3484 printk(TPACPI_INFO
3485 "woke up due to a hot-unplug "
3486 "request...\n");
3487 hotkey_wakeup_reason_notify_change();
3489 return true;
3492 static bool hotkey_notify_usrevent(const u32 hkey,
3493 bool *send_acpi_ev,
3494 bool *ignore_acpi_ev)
3496 /* 0x5000-0x5FFF: human interface helpers */
3497 *send_acpi_ev = true;
3498 *ignore_acpi_ev = false;
3500 switch (hkey) {
3501 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3502 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3503 return true;
3505 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3506 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3507 tpacpi_input_send_tabletsw();
3508 hotkey_tablet_mode_notify_change();
3509 *send_acpi_ev = false;
3510 return true;
3512 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3513 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3514 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3515 /* do not propagate these events */
3516 *ignore_acpi_ev = true;
3517 return true;
3519 default:
3520 return false;
3524 static void thermal_dump_all_sensors(void);
3526 static bool hotkey_notify_thermal(const u32 hkey,
3527 bool *send_acpi_ev,
3528 bool *ignore_acpi_ev)
3530 bool known = true;
3532 /* 0x6000-0x6FFF: thermal alarms */
3533 *send_acpi_ev = true;
3534 *ignore_acpi_ev = false;
3536 switch (hkey) {
3537 case TP_HKEY_EV_THM_TABLE_CHANGED:
3538 printk(TPACPI_INFO
3539 "EC reports that Thermal Table has changed\n");
3540 /* recommended action: do nothing, we don't have
3541 * Lenovo ATM information */
3542 return true;
3543 case TP_HKEY_EV_ALARM_BAT_HOT:
3544 printk(TPACPI_CRIT
3545 "THERMAL ALARM: battery is too hot!\n");
3546 /* recommended action: warn user through gui */
3547 break;
3548 case TP_HKEY_EV_ALARM_BAT_XHOT:
3549 printk(TPACPI_ALERT
3550 "THERMAL EMERGENCY: battery is extremely hot!\n");
3551 /* recommended action: immediate sleep/hibernate */
3552 break;
3553 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3554 printk(TPACPI_CRIT
3555 "THERMAL ALARM: "
3556 "a sensor reports something is too hot!\n");
3557 /* recommended action: warn user through gui, that */
3558 /* some internal component is too hot */
3559 break;
3560 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3561 printk(TPACPI_ALERT
3562 "THERMAL EMERGENCY: "
3563 "a sensor reports something is extremely hot!\n");
3564 /* recommended action: immediate sleep/hibernate */
3565 break;
3566 default:
3567 printk(TPACPI_ALERT
3568 "THERMAL ALERT: unknown thermal alarm received\n");
3569 known = false;
3572 thermal_dump_all_sensors();
3574 return known;
3577 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3579 u32 hkey;
3580 bool send_acpi_ev;
3581 bool ignore_acpi_ev;
3582 bool known_ev;
3584 if (event != 0x80) {
3585 printk(TPACPI_ERR
3586 "unknown HKEY notification event %d\n", event);
3587 /* forward it to userspace, maybe it knows how to handle it */
3588 acpi_bus_generate_netlink_event(
3589 ibm->acpi->device->pnp.device_class,
3590 dev_name(&ibm->acpi->device->dev),
3591 event, 0);
3592 return;
3595 while (1) {
3596 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3597 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3598 return;
3601 if (hkey == 0) {
3602 /* queue empty */
3603 return;
3606 send_acpi_ev = true;
3607 ignore_acpi_ev = false;
3609 switch (hkey >> 12) {
3610 case 1:
3611 /* 0x1000-0x1FFF: key presses */
3612 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3613 &ignore_acpi_ev);
3614 break;
3615 case 2:
3616 /* 0x2000-0x2FFF: Wakeup reason */
3617 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3618 &ignore_acpi_ev);
3619 break;
3620 case 3:
3621 /* 0x3000-0x3FFF: bay-related wakeups */
3622 switch (hkey) {
3623 case TP_HKEY_EV_BAYEJ_ACK:
3624 hotkey_autosleep_ack = 1;
3625 printk(TPACPI_INFO
3626 "bay ejected\n");
3627 hotkey_wakeup_hotunplug_complete_notify_change();
3628 known_ev = true;
3629 break;
3630 case TP_HKEY_EV_OPTDRV_EJ:
3631 /* FIXME: kick libata if SATA link offline */
3632 known_ev = true;
3633 break;
3634 default:
3635 known_ev = false;
3637 break;
3638 case 4:
3639 /* 0x4000-0x4FFF: dock-related wakeups */
3640 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3641 hotkey_autosleep_ack = 1;
3642 printk(TPACPI_INFO
3643 "undocked\n");
3644 hotkey_wakeup_hotunplug_complete_notify_change();
3645 known_ev = true;
3646 } else {
3647 known_ev = false;
3649 break;
3650 case 5:
3651 /* 0x5000-0x5FFF: human interface helpers */
3652 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3653 &ignore_acpi_ev);
3654 break;
3655 case 6:
3656 /* 0x6000-0x6FFF: thermal alarms */
3657 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3658 &ignore_acpi_ev);
3659 break;
3660 case 7:
3661 /* 0x7000-0x7FFF: misc */
3662 if (tp_features.hotkey_wlsw &&
3663 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3664 tpacpi_send_radiosw_update();
3665 send_acpi_ev = 0;
3666 known_ev = true;
3667 break;
3669 /* fallthrough to default */
3670 default:
3671 known_ev = false;
3673 if (!known_ev) {
3674 printk(TPACPI_NOTICE
3675 "unhandled HKEY event 0x%04x\n", hkey);
3676 printk(TPACPI_NOTICE
3677 "please report the conditions when this "
3678 "event happened to %s\n", TPACPI_MAIL);
3681 /* Legacy events */
3682 if (!ignore_acpi_ev &&
3683 (send_acpi_ev || hotkey_report_mode < 2)) {
3684 acpi_bus_generate_proc_event(ibm->acpi->device,
3685 event, hkey);
3688 /* netlink events */
3689 if (!ignore_acpi_ev && send_acpi_ev) {
3690 acpi_bus_generate_netlink_event(
3691 ibm->acpi->device->pnp.device_class,
3692 dev_name(&ibm->acpi->device->dev),
3693 event, hkey);
3698 static void hotkey_suspend(pm_message_t state)
3700 /* Do these on suspend, we get the events on early resume! */
3701 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3702 hotkey_autosleep_ack = 0;
3705 static void hotkey_resume(void)
3707 tpacpi_disable_brightness_delay();
3709 if (hotkey_status_set(true) < 0 ||
3710 hotkey_mask_set(hotkey_acpi_mask) < 0)
3711 printk(TPACPI_ERR
3712 "error while attempting to reset the event "
3713 "firmware interface\n");
3715 tpacpi_send_radiosw_update();
3716 hotkey_tablet_mode_notify_change();
3717 hotkey_wakeup_reason_notify_change();
3718 hotkey_wakeup_hotunplug_complete_notify_change();
3719 hotkey_poll_setup_safe(false);
3722 /* procfs -------------------------------------------------------------- */
3723 static int hotkey_read(struct seq_file *m)
3725 int res, status;
3727 if (!tp_features.hotkey) {
3728 seq_printf(m, "status:\t\tnot supported\n");
3729 return 0;
3732 if (mutex_lock_killable(&hotkey_mutex))
3733 return -ERESTARTSYS;
3734 res = hotkey_status_get(&status);
3735 if (!res)
3736 res = hotkey_mask_get();
3737 mutex_unlock(&hotkey_mutex);
3738 if (res)
3739 return res;
3741 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3742 if (hotkey_all_mask) {
3743 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3744 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3745 } else {
3746 seq_printf(m, "mask:\t\tnot supported\n");
3747 seq_printf(m, "commands:\tenable, disable, reset\n");
3750 return 0;
3753 static void hotkey_enabledisable_warn(bool enable)
3755 tpacpi_log_usertask("procfs hotkey enable/disable");
3756 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3757 TPACPI_WARN
3758 "hotkey enable/disable functionality has been "
3759 "removed from the driver. Hotkeys are always "
3760 "enabled\n"))
3761 printk(TPACPI_ERR
3762 "Please remove the hotkey=enable module "
3763 "parameter, it is deprecated. Hotkeys are always "
3764 "enabled\n");
3767 static int hotkey_write(char *buf)
3769 int res;
3770 u32 mask;
3771 char *cmd;
3773 if (!tp_features.hotkey)
3774 return -ENODEV;
3776 if (mutex_lock_killable(&hotkey_mutex))
3777 return -ERESTARTSYS;
3779 mask = hotkey_user_mask;
3781 res = 0;
3782 while ((cmd = next_cmd(&buf))) {
3783 if (strlencmp(cmd, "enable") == 0) {
3784 hotkey_enabledisable_warn(1);
3785 } else if (strlencmp(cmd, "disable") == 0) {
3786 hotkey_enabledisable_warn(0);
3787 res = -EPERM;
3788 } else if (strlencmp(cmd, "reset") == 0) {
3789 mask = (hotkey_all_mask | hotkey_source_mask)
3790 & ~hotkey_reserved_mask;
3791 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3792 /* mask set */
3793 } else if (sscanf(cmd, "%x", &mask) == 1) {
3794 /* mask set */
3795 } else {
3796 res = -EINVAL;
3797 goto errexit;
3801 if (!res) {
3802 tpacpi_disclose_usertask("procfs hotkey",
3803 "set mask to 0x%08x\n", mask);
3804 res = hotkey_user_mask_set(mask);
3807 errexit:
3808 mutex_unlock(&hotkey_mutex);
3809 return res;
3812 static const struct acpi_device_id ibm_htk_device_ids[] = {
3813 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3814 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
3815 {"", 0},
3818 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3819 .hid = ibm_htk_device_ids,
3820 .notify = hotkey_notify,
3821 .handle = &hkey_handle,
3822 .type = ACPI_DEVICE_NOTIFY,
3825 static struct ibm_struct hotkey_driver_data = {
3826 .name = "hotkey",
3827 .read = hotkey_read,
3828 .write = hotkey_write,
3829 .exit = hotkey_exit,
3830 .resume = hotkey_resume,
3831 .suspend = hotkey_suspend,
3832 .acpi = &ibm_hotkey_acpidriver,
3835 /*************************************************************************
3836 * Bluetooth subdriver
3839 enum {
3840 /* ACPI GBDC/SBDC bits */
3841 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3842 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3843 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3844 0 = disable, 1 = enable */
3847 enum {
3848 /* ACPI \BLTH commands */
3849 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3850 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3851 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3852 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3853 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3856 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3858 static int bluetooth_get_status(void)
3860 int status;
3862 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3863 if (dbg_bluetoothemul)
3864 return (tpacpi_bluetooth_emulstate) ?
3865 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3866 #endif
3868 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3869 return -EIO;
3871 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3872 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3875 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3877 int status;
3879 vdbg_printk(TPACPI_DBG_RFKILL,
3880 "will attempt to %s bluetooth\n",
3881 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3883 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3884 if (dbg_bluetoothemul) {
3885 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3886 return 0;
3888 #endif
3890 if (state == TPACPI_RFK_RADIO_ON)
3891 status = TP_ACPI_BLUETOOTH_RADIOSSW
3892 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3893 else
3894 status = 0;
3896 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3897 return -EIO;
3899 return 0;
3902 /* sysfs bluetooth enable ---------------------------------------------- */
3903 static ssize_t bluetooth_enable_show(struct device *dev,
3904 struct device_attribute *attr,
3905 char *buf)
3907 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3908 attr, buf);
3911 static ssize_t bluetooth_enable_store(struct device *dev,
3912 struct device_attribute *attr,
3913 const char *buf, size_t count)
3915 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3916 attr, buf, count);
3919 static struct device_attribute dev_attr_bluetooth_enable =
3920 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3921 bluetooth_enable_show, bluetooth_enable_store);
3923 /* --------------------------------------------------------------------- */
3925 static struct attribute *bluetooth_attributes[] = {
3926 &dev_attr_bluetooth_enable.attr,
3927 NULL
3930 static const struct attribute_group bluetooth_attr_group = {
3931 .attrs = bluetooth_attributes,
3934 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3935 .get_status = bluetooth_get_status,
3936 .set_status = bluetooth_set_status,
3939 static void bluetooth_shutdown(void)
3941 /* Order firmware to save current state to NVRAM */
3942 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3943 TP_ACPI_BLTH_SAVE_STATE))
3944 printk(TPACPI_NOTICE
3945 "failed to save bluetooth state to NVRAM\n");
3946 else
3947 vdbg_printk(TPACPI_DBG_RFKILL,
3948 "bluestooth state saved to NVRAM\n");
3951 static void bluetooth_exit(void)
3953 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3954 &bluetooth_attr_group);
3956 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3958 bluetooth_shutdown();
3961 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3963 int res;
3964 int status = 0;
3966 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3967 "initializing bluetooth subdriver\n");
3969 TPACPI_ACPIHANDLE_INIT(hkey);
3971 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3972 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3973 tp_features.bluetooth = hkey_handle &&
3974 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3976 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3977 "bluetooth is %s, status 0x%02x\n",
3978 str_supported(tp_features.bluetooth),
3979 status);
3981 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3982 if (dbg_bluetoothemul) {
3983 tp_features.bluetooth = 1;
3984 printk(TPACPI_INFO
3985 "bluetooth switch emulation enabled\n");
3986 } else
3987 #endif
3988 if (tp_features.bluetooth &&
3989 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3990 /* no bluetooth hardware present in system */
3991 tp_features.bluetooth = 0;
3992 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3993 "bluetooth hardware not installed\n");
3996 if (!tp_features.bluetooth)
3997 return 1;
3999 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4000 &bluetooth_tprfk_ops,
4001 RFKILL_TYPE_BLUETOOTH,
4002 TPACPI_RFK_BLUETOOTH_SW_NAME,
4003 true);
4004 if (res)
4005 return res;
4007 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4008 &bluetooth_attr_group);
4009 if (res) {
4010 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4011 return res;
4014 return 0;
4017 /* procfs -------------------------------------------------------------- */
4018 static int bluetooth_read(struct seq_file *m)
4020 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4023 static int bluetooth_write(char *buf)
4025 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4028 static struct ibm_struct bluetooth_driver_data = {
4029 .name = "bluetooth",
4030 .read = bluetooth_read,
4031 .write = bluetooth_write,
4032 .exit = bluetooth_exit,
4033 .shutdown = bluetooth_shutdown,
4036 /*************************************************************************
4037 * Wan subdriver
4040 enum {
4041 /* ACPI GWAN/SWAN bits */
4042 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4043 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4044 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4045 0 = disable, 1 = enable */
4048 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4050 static int wan_get_status(void)
4052 int status;
4054 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4055 if (dbg_wwanemul)
4056 return (tpacpi_wwan_emulstate) ?
4057 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4058 #endif
4060 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4061 return -EIO;
4063 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4064 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4067 static int wan_set_status(enum tpacpi_rfkill_state state)
4069 int status;
4071 vdbg_printk(TPACPI_DBG_RFKILL,
4072 "will attempt to %s wwan\n",
4073 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4075 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4076 if (dbg_wwanemul) {
4077 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4078 return 0;
4080 #endif
4082 if (state == TPACPI_RFK_RADIO_ON)
4083 status = TP_ACPI_WANCARD_RADIOSSW
4084 | TP_ACPI_WANCARD_RESUMECTRL;
4085 else
4086 status = 0;
4088 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4089 return -EIO;
4091 return 0;
4094 /* sysfs wan enable ---------------------------------------------------- */
4095 static ssize_t wan_enable_show(struct device *dev,
4096 struct device_attribute *attr,
4097 char *buf)
4099 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4100 attr, buf);
4103 static ssize_t wan_enable_store(struct device *dev,
4104 struct device_attribute *attr,
4105 const char *buf, size_t count)
4107 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4108 attr, buf, count);
4111 static struct device_attribute dev_attr_wan_enable =
4112 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4113 wan_enable_show, wan_enable_store);
4115 /* --------------------------------------------------------------------- */
4117 static struct attribute *wan_attributes[] = {
4118 &dev_attr_wan_enable.attr,
4119 NULL
4122 static const struct attribute_group wan_attr_group = {
4123 .attrs = wan_attributes,
4126 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4127 .get_status = wan_get_status,
4128 .set_status = wan_set_status,
4131 static void wan_shutdown(void)
4133 /* Order firmware to save current state to NVRAM */
4134 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4135 TP_ACPI_WGSV_SAVE_STATE))
4136 printk(TPACPI_NOTICE
4137 "failed to save WWAN state to NVRAM\n");
4138 else
4139 vdbg_printk(TPACPI_DBG_RFKILL,
4140 "WWAN state saved to NVRAM\n");
4143 static void wan_exit(void)
4145 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4146 &wan_attr_group);
4148 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4150 wan_shutdown();
4153 static int __init wan_init(struct ibm_init_struct *iibm)
4155 int res;
4156 int status = 0;
4158 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4159 "initializing wan subdriver\n");
4161 TPACPI_ACPIHANDLE_INIT(hkey);
4163 tp_features.wan = hkey_handle &&
4164 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4166 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4167 "wan is %s, status 0x%02x\n",
4168 str_supported(tp_features.wan),
4169 status);
4171 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4172 if (dbg_wwanemul) {
4173 tp_features.wan = 1;
4174 printk(TPACPI_INFO
4175 "wwan switch emulation enabled\n");
4176 } else
4177 #endif
4178 if (tp_features.wan &&
4179 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4180 /* no wan hardware present in system */
4181 tp_features.wan = 0;
4182 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4183 "wan hardware not installed\n");
4186 if (!tp_features.wan)
4187 return 1;
4189 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4190 &wan_tprfk_ops,
4191 RFKILL_TYPE_WWAN,
4192 TPACPI_RFK_WWAN_SW_NAME,
4193 true);
4194 if (res)
4195 return res;
4197 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4198 &wan_attr_group);
4200 if (res) {
4201 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4202 return res;
4205 return 0;
4208 /* procfs -------------------------------------------------------------- */
4209 static int wan_read(struct seq_file *m)
4211 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4214 static int wan_write(char *buf)
4216 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4219 static struct ibm_struct wan_driver_data = {
4220 .name = "wan",
4221 .read = wan_read,
4222 .write = wan_write,
4223 .exit = wan_exit,
4224 .shutdown = wan_shutdown,
4227 /*************************************************************************
4228 * UWB subdriver
4231 enum {
4232 /* ACPI GUWB/SUWB bits */
4233 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4234 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4237 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4239 static int uwb_get_status(void)
4241 int status;
4243 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4244 if (dbg_uwbemul)
4245 return (tpacpi_uwb_emulstate) ?
4246 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4247 #endif
4249 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4250 return -EIO;
4252 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4253 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4256 static int uwb_set_status(enum tpacpi_rfkill_state state)
4258 int status;
4260 vdbg_printk(TPACPI_DBG_RFKILL,
4261 "will attempt to %s UWB\n",
4262 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4264 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4265 if (dbg_uwbemul) {
4266 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4267 return 0;
4269 #endif
4271 if (state == TPACPI_RFK_RADIO_ON)
4272 status = TP_ACPI_UWB_RADIOSSW;
4273 else
4274 status = 0;
4276 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4277 return -EIO;
4279 return 0;
4282 /* --------------------------------------------------------------------- */
4284 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4285 .get_status = uwb_get_status,
4286 .set_status = uwb_set_status,
4289 static void uwb_exit(void)
4291 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4294 static int __init uwb_init(struct ibm_init_struct *iibm)
4296 int res;
4297 int status = 0;
4299 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4300 "initializing uwb subdriver\n");
4302 TPACPI_ACPIHANDLE_INIT(hkey);
4304 tp_features.uwb = hkey_handle &&
4305 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4307 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4308 "uwb is %s, status 0x%02x\n",
4309 str_supported(tp_features.uwb),
4310 status);
4312 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4313 if (dbg_uwbemul) {
4314 tp_features.uwb = 1;
4315 printk(TPACPI_INFO
4316 "uwb switch emulation enabled\n");
4317 } else
4318 #endif
4319 if (tp_features.uwb &&
4320 !(status & TP_ACPI_UWB_HWPRESENT)) {
4321 /* no uwb hardware present in system */
4322 tp_features.uwb = 0;
4323 dbg_printk(TPACPI_DBG_INIT,
4324 "uwb hardware not installed\n");
4327 if (!tp_features.uwb)
4328 return 1;
4330 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4331 &uwb_tprfk_ops,
4332 RFKILL_TYPE_UWB,
4333 TPACPI_RFK_UWB_SW_NAME,
4334 false);
4335 return res;
4338 static struct ibm_struct uwb_driver_data = {
4339 .name = "uwb",
4340 .exit = uwb_exit,
4341 .flags.experimental = 1,
4344 /*************************************************************************
4345 * Video subdriver
4348 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4350 enum video_access_mode {
4351 TPACPI_VIDEO_NONE = 0,
4352 TPACPI_VIDEO_570, /* 570 */
4353 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4354 TPACPI_VIDEO_NEW, /* all others */
4357 enum { /* video status flags, based on VIDEO_570 */
4358 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4359 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4360 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4363 enum { /* TPACPI_VIDEO_570 constants */
4364 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4365 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4366 * video_status_flags */
4367 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4368 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4371 static enum video_access_mode video_supported;
4372 static int video_orig_autosw;
4374 static int video_autosw_get(void);
4375 static int video_autosw_set(int enable);
4377 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4379 static int __init video_init(struct ibm_init_struct *iibm)
4381 int ivga;
4383 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4385 TPACPI_ACPIHANDLE_INIT(vid);
4386 if (tpacpi_is_ibm())
4387 TPACPI_ACPIHANDLE_INIT(vid2);
4389 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4390 /* G41, assume IVGA doesn't change */
4391 vid_handle = vid2_handle;
4393 if (!vid_handle)
4394 /* video switching not supported on R30, R31 */
4395 video_supported = TPACPI_VIDEO_NONE;
4396 else if (tpacpi_is_ibm() &&
4397 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4398 /* 570 */
4399 video_supported = TPACPI_VIDEO_570;
4400 else if (tpacpi_is_ibm() &&
4401 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4402 /* 600e/x, 770e, 770x */
4403 video_supported = TPACPI_VIDEO_770;
4404 else
4405 /* all others */
4406 video_supported = TPACPI_VIDEO_NEW;
4408 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4409 str_supported(video_supported != TPACPI_VIDEO_NONE),
4410 video_supported);
4412 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4415 static void video_exit(void)
4417 dbg_printk(TPACPI_DBG_EXIT,
4418 "restoring original video autoswitch mode\n");
4419 if (video_autosw_set(video_orig_autosw))
4420 printk(TPACPI_ERR "error while trying to restore original "
4421 "video autoswitch mode\n");
4424 static int video_outputsw_get(void)
4426 int status = 0;
4427 int i;
4429 switch (video_supported) {
4430 case TPACPI_VIDEO_570:
4431 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4432 TP_ACPI_VIDEO_570_PHSCMD))
4433 return -EIO;
4434 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4435 break;
4436 case TPACPI_VIDEO_770:
4437 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4438 return -EIO;
4439 if (i)
4440 status |= TP_ACPI_VIDEO_S_LCD;
4441 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4442 return -EIO;
4443 if (i)
4444 status |= TP_ACPI_VIDEO_S_CRT;
4445 break;
4446 case TPACPI_VIDEO_NEW:
4447 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4448 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4449 return -EIO;
4450 if (i)
4451 status |= TP_ACPI_VIDEO_S_CRT;
4453 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4454 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4455 return -EIO;
4456 if (i)
4457 status |= TP_ACPI_VIDEO_S_LCD;
4458 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4459 return -EIO;
4460 if (i)
4461 status |= TP_ACPI_VIDEO_S_DVI;
4462 break;
4463 default:
4464 return -ENOSYS;
4467 return status;
4470 static int video_outputsw_set(int status)
4472 int autosw;
4473 int res = 0;
4475 switch (video_supported) {
4476 case TPACPI_VIDEO_570:
4477 res = acpi_evalf(NULL, NULL,
4478 "\\_SB.PHS2", "vdd",
4479 TP_ACPI_VIDEO_570_PHS2CMD,
4480 status | TP_ACPI_VIDEO_570_PHS2SET);
4481 break;
4482 case TPACPI_VIDEO_770:
4483 autosw = video_autosw_get();
4484 if (autosw < 0)
4485 return autosw;
4487 res = video_autosw_set(1);
4488 if (res)
4489 return res;
4490 res = acpi_evalf(vid_handle, NULL,
4491 "ASWT", "vdd", status * 0x100, 0);
4492 if (!autosw && video_autosw_set(autosw)) {
4493 printk(TPACPI_ERR
4494 "video auto-switch left enabled due to error\n");
4495 return -EIO;
4497 break;
4498 case TPACPI_VIDEO_NEW:
4499 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4500 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4501 break;
4502 default:
4503 return -ENOSYS;
4506 return (res)? 0 : -EIO;
4509 static int video_autosw_get(void)
4511 int autosw = 0;
4513 switch (video_supported) {
4514 case TPACPI_VIDEO_570:
4515 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4516 return -EIO;
4517 break;
4518 case TPACPI_VIDEO_770:
4519 case TPACPI_VIDEO_NEW:
4520 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4521 return -EIO;
4522 break;
4523 default:
4524 return -ENOSYS;
4527 return autosw & 1;
4530 static int video_autosw_set(int enable)
4532 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4533 return -EIO;
4534 return 0;
4537 static int video_outputsw_cycle(void)
4539 int autosw = video_autosw_get();
4540 int res;
4542 if (autosw < 0)
4543 return autosw;
4545 switch (video_supported) {
4546 case TPACPI_VIDEO_570:
4547 res = video_autosw_set(1);
4548 if (res)
4549 return res;
4550 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4551 break;
4552 case TPACPI_VIDEO_770:
4553 case TPACPI_VIDEO_NEW:
4554 res = video_autosw_set(1);
4555 if (res)
4556 return res;
4557 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4558 break;
4559 default:
4560 return -ENOSYS;
4562 if (!autosw && video_autosw_set(autosw)) {
4563 printk(TPACPI_ERR
4564 "video auto-switch left enabled due to error\n");
4565 return -EIO;
4568 return (res)? 0 : -EIO;
4571 static int video_expand_toggle(void)
4573 switch (video_supported) {
4574 case TPACPI_VIDEO_570:
4575 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4576 0 : -EIO;
4577 case TPACPI_VIDEO_770:
4578 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4579 0 : -EIO;
4580 case TPACPI_VIDEO_NEW:
4581 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4582 0 : -EIO;
4583 default:
4584 return -ENOSYS;
4586 /* not reached */
4589 static int video_read(struct seq_file *m)
4591 int status, autosw;
4593 if (video_supported == TPACPI_VIDEO_NONE) {
4594 seq_printf(m, "status:\t\tnot supported\n");
4595 return 0;
4598 /* Even reads can crash X.org, so... */
4599 if (!capable(CAP_SYS_ADMIN))
4600 return -EPERM;
4602 status = video_outputsw_get();
4603 if (status < 0)
4604 return status;
4606 autosw = video_autosw_get();
4607 if (autosw < 0)
4608 return autosw;
4610 seq_printf(m, "status:\t\tsupported\n");
4611 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4612 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4613 if (video_supported == TPACPI_VIDEO_NEW)
4614 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4615 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4616 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4617 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4618 if (video_supported == TPACPI_VIDEO_NEW)
4619 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4620 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4621 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4623 return 0;
4626 static int video_write(char *buf)
4628 char *cmd;
4629 int enable, disable, status;
4630 int res;
4632 if (video_supported == TPACPI_VIDEO_NONE)
4633 return -ENODEV;
4635 /* Even reads can crash X.org, let alone writes... */
4636 if (!capable(CAP_SYS_ADMIN))
4637 return -EPERM;
4639 enable = 0;
4640 disable = 0;
4642 while ((cmd = next_cmd(&buf))) {
4643 if (strlencmp(cmd, "lcd_enable") == 0) {
4644 enable |= TP_ACPI_VIDEO_S_LCD;
4645 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4646 disable |= TP_ACPI_VIDEO_S_LCD;
4647 } else if (strlencmp(cmd, "crt_enable") == 0) {
4648 enable |= TP_ACPI_VIDEO_S_CRT;
4649 } else if (strlencmp(cmd, "crt_disable") == 0) {
4650 disable |= TP_ACPI_VIDEO_S_CRT;
4651 } else if (video_supported == TPACPI_VIDEO_NEW &&
4652 strlencmp(cmd, "dvi_enable") == 0) {
4653 enable |= TP_ACPI_VIDEO_S_DVI;
4654 } else if (video_supported == TPACPI_VIDEO_NEW &&
4655 strlencmp(cmd, "dvi_disable") == 0) {
4656 disable |= TP_ACPI_VIDEO_S_DVI;
4657 } else if (strlencmp(cmd, "auto_enable") == 0) {
4658 res = video_autosw_set(1);
4659 if (res)
4660 return res;
4661 } else if (strlencmp(cmd, "auto_disable") == 0) {
4662 res = video_autosw_set(0);
4663 if (res)
4664 return res;
4665 } else if (strlencmp(cmd, "video_switch") == 0) {
4666 res = video_outputsw_cycle();
4667 if (res)
4668 return res;
4669 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4670 res = video_expand_toggle();
4671 if (res)
4672 return res;
4673 } else
4674 return -EINVAL;
4677 if (enable || disable) {
4678 status = video_outputsw_get();
4679 if (status < 0)
4680 return status;
4681 res = video_outputsw_set((status & ~disable) | enable);
4682 if (res)
4683 return res;
4686 return 0;
4689 static struct ibm_struct video_driver_data = {
4690 .name = "video",
4691 .read = video_read,
4692 .write = video_write,
4693 .exit = video_exit,
4696 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4698 /*************************************************************************
4699 * Light (thinklight) subdriver
4702 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4703 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4705 static int light_get_status(void)
4707 int status = 0;
4709 if (tp_features.light_status) {
4710 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4711 return -EIO;
4712 return (!!status);
4715 return -ENXIO;
4718 static int light_set_status(int status)
4720 int rc;
4722 if (tp_features.light) {
4723 if (cmos_handle) {
4724 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4725 (status)?
4726 TP_CMOS_THINKLIGHT_ON :
4727 TP_CMOS_THINKLIGHT_OFF);
4728 } else {
4729 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4730 (status)? 1 : 0);
4732 return (rc)? 0 : -EIO;
4735 return -ENXIO;
4738 static void light_set_status_worker(struct work_struct *work)
4740 struct tpacpi_led_classdev *data =
4741 container_of(work, struct tpacpi_led_classdev, work);
4743 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4744 light_set_status((data->new_state != TPACPI_LED_OFF));
4747 static void light_sysfs_set(struct led_classdev *led_cdev,
4748 enum led_brightness brightness)
4750 struct tpacpi_led_classdev *data =
4751 container_of(led_cdev,
4752 struct tpacpi_led_classdev,
4753 led_classdev);
4754 data->new_state = (brightness != LED_OFF) ?
4755 TPACPI_LED_ON : TPACPI_LED_OFF;
4756 queue_work(tpacpi_wq, &data->work);
4759 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4761 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4764 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4765 .led_classdev = {
4766 .name = "tpacpi::thinklight",
4767 .brightness_set = &light_sysfs_set,
4768 .brightness_get = &light_sysfs_get,
4772 static int __init light_init(struct ibm_init_struct *iibm)
4774 int rc;
4776 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4778 if (tpacpi_is_ibm()) {
4779 TPACPI_ACPIHANDLE_INIT(ledb);
4780 TPACPI_ACPIHANDLE_INIT(lght);
4782 TPACPI_ACPIHANDLE_INIT(cmos);
4783 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4785 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4786 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4788 if (tp_features.light)
4789 /* light status not supported on
4790 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4791 tp_features.light_status =
4792 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4794 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4795 str_supported(tp_features.light),
4796 str_supported(tp_features.light_status));
4798 if (!tp_features.light)
4799 return 1;
4801 rc = led_classdev_register(&tpacpi_pdev->dev,
4802 &tpacpi_led_thinklight.led_classdev);
4804 if (rc < 0) {
4805 tp_features.light = 0;
4806 tp_features.light_status = 0;
4807 } else {
4808 rc = 0;
4811 return rc;
4814 static void light_exit(void)
4816 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4817 if (work_pending(&tpacpi_led_thinklight.work))
4818 flush_workqueue(tpacpi_wq);
4821 static int light_read(struct seq_file *m)
4823 int status;
4825 if (!tp_features.light) {
4826 seq_printf(m, "status:\t\tnot supported\n");
4827 } else if (!tp_features.light_status) {
4828 seq_printf(m, "status:\t\tunknown\n");
4829 seq_printf(m, "commands:\ton, off\n");
4830 } else {
4831 status = light_get_status();
4832 if (status < 0)
4833 return status;
4834 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4835 seq_printf(m, "commands:\ton, off\n");
4838 return 0;
4841 static int light_write(char *buf)
4843 char *cmd;
4844 int newstatus = 0;
4846 if (!tp_features.light)
4847 return -ENODEV;
4849 while ((cmd = next_cmd(&buf))) {
4850 if (strlencmp(cmd, "on") == 0) {
4851 newstatus = 1;
4852 } else if (strlencmp(cmd, "off") == 0) {
4853 newstatus = 0;
4854 } else
4855 return -EINVAL;
4858 return light_set_status(newstatus);
4861 static struct ibm_struct light_driver_data = {
4862 .name = "light",
4863 .read = light_read,
4864 .write = light_write,
4865 .exit = light_exit,
4868 /*************************************************************************
4869 * CMOS subdriver
4872 /* sysfs cmos_command -------------------------------------------------- */
4873 static ssize_t cmos_command_store(struct device *dev,
4874 struct device_attribute *attr,
4875 const char *buf, size_t count)
4877 unsigned long cmos_cmd;
4878 int res;
4880 if (parse_strtoul(buf, 21, &cmos_cmd))
4881 return -EINVAL;
4883 res = issue_thinkpad_cmos_command(cmos_cmd);
4884 return (res)? res : count;
4887 static struct device_attribute dev_attr_cmos_command =
4888 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4890 /* --------------------------------------------------------------------- */
4892 static int __init cmos_init(struct ibm_init_struct *iibm)
4894 int res;
4896 vdbg_printk(TPACPI_DBG_INIT,
4897 "initializing cmos commands subdriver\n");
4899 TPACPI_ACPIHANDLE_INIT(cmos);
4901 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4902 str_supported(cmos_handle != NULL));
4904 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4905 if (res)
4906 return res;
4908 return (cmos_handle)? 0 : 1;
4911 static void cmos_exit(void)
4913 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4916 static int cmos_read(struct seq_file *m)
4918 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4919 R30, R31, T20-22, X20-21 */
4920 if (!cmos_handle)
4921 seq_printf(m, "status:\t\tnot supported\n");
4922 else {
4923 seq_printf(m, "status:\t\tsupported\n");
4924 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4927 return 0;
4930 static int cmos_write(char *buf)
4932 char *cmd;
4933 int cmos_cmd, res;
4935 while ((cmd = next_cmd(&buf))) {
4936 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4937 cmos_cmd >= 0 && cmos_cmd <= 21) {
4938 /* cmos_cmd set */
4939 } else
4940 return -EINVAL;
4942 res = issue_thinkpad_cmos_command(cmos_cmd);
4943 if (res)
4944 return res;
4947 return 0;
4950 static struct ibm_struct cmos_driver_data = {
4951 .name = "cmos",
4952 .read = cmos_read,
4953 .write = cmos_write,
4954 .exit = cmos_exit,
4957 /*************************************************************************
4958 * LED subdriver
4961 enum led_access_mode {
4962 TPACPI_LED_NONE = 0,
4963 TPACPI_LED_570, /* 570 */
4964 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4965 TPACPI_LED_NEW, /* all others */
4968 enum { /* For TPACPI_LED_OLD */
4969 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4970 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4971 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4974 static enum led_access_mode led_supported;
4976 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4977 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4978 /* T20-22, X20-21 */
4979 "LED", /* all others */
4980 ); /* R30, R31 */
4982 #define TPACPI_LED_NUMLEDS 16
4983 static struct tpacpi_led_classdev *tpacpi_leds;
4984 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4985 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4986 /* there's a limit of 19 chars + NULL before 2.6.26 */
4987 "tpacpi::power",
4988 "tpacpi:orange:batt",
4989 "tpacpi:green:batt",
4990 "tpacpi::dock_active",
4991 "tpacpi::bay_active",
4992 "tpacpi::dock_batt",
4993 "tpacpi::unknown_led",
4994 "tpacpi::standby",
4995 "tpacpi::dock_status1",
4996 "tpacpi::dock_status2",
4997 "tpacpi::unknown_led2",
4998 "tpacpi::unknown_led3",
4999 "tpacpi::thinkvantage",
5001 #define TPACPI_SAFE_LEDS 0x1081U
5003 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5005 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5006 return false;
5007 #else
5008 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5009 #endif
5012 static int led_get_status(const unsigned int led)
5014 int status;
5015 enum led_status_t led_s;
5017 switch (led_supported) {
5018 case TPACPI_LED_570:
5019 if (!acpi_evalf(ec_handle,
5020 &status, "GLED", "dd", 1 << led))
5021 return -EIO;
5022 led_s = (status == 0)?
5023 TPACPI_LED_OFF :
5024 ((status == 1)?
5025 TPACPI_LED_ON :
5026 TPACPI_LED_BLINK);
5027 tpacpi_led_state_cache[led] = led_s;
5028 return led_s;
5029 default:
5030 return -ENXIO;
5033 /* not reached */
5036 static int led_set_status(const unsigned int led,
5037 const enum led_status_t ledstatus)
5039 /* off, on, blink. Index is led_status_t */
5040 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5041 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5043 int rc = 0;
5045 switch (led_supported) {
5046 case TPACPI_LED_570:
5047 /* 570 */
5048 if (unlikely(led > 7))
5049 return -EINVAL;
5050 if (unlikely(tpacpi_is_led_restricted(led)))
5051 return -EPERM;
5052 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5053 (1 << led), led_sled_arg1[ledstatus]))
5054 rc = -EIO;
5055 break;
5056 case TPACPI_LED_OLD:
5057 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5058 if (unlikely(led > 7))
5059 return -EINVAL;
5060 if (unlikely(tpacpi_is_led_restricted(led)))
5061 return -EPERM;
5062 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5063 if (rc >= 0)
5064 rc = ec_write(TPACPI_LED_EC_HLBL,
5065 (ledstatus == TPACPI_LED_BLINK) << led);
5066 if (rc >= 0)
5067 rc = ec_write(TPACPI_LED_EC_HLCL,
5068 (ledstatus != TPACPI_LED_OFF) << led);
5069 break;
5070 case TPACPI_LED_NEW:
5071 /* all others */
5072 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5073 return -EINVAL;
5074 if (unlikely(tpacpi_is_led_restricted(led)))
5075 return -EPERM;
5076 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5077 led, led_led_arg1[ledstatus]))
5078 rc = -EIO;
5079 break;
5080 default:
5081 rc = -ENXIO;
5084 if (!rc)
5085 tpacpi_led_state_cache[led] = ledstatus;
5087 return rc;
5090 static void led_set_status_worker(struct work_struct *work)
5092 struct tpacpi_led_classdev *data =
5093 container_of(work, struct tpacpi_led_classdev, work);
5095 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5096 led_set_status(data->led, data->new_state);
5099 static void led_sysfs_set(struct led_classdev *led_cdev,
5100 enum led_brightness brightness)
5102 struct tpacpi_led_classdev *data = container_of(led_cdev,
5103 struct tpacpi_led_classdev, led_classdev);
5105 if (brightness == LED_OFF)
5106 data->new_state = TPACPI_LED_OFF;
5107 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5108 data->new_state = TPACPI_LED_ON;
5109 else
5110 data->new_state = TPACPI_LED_BLINK;
5112 queue_work(tpacpi_wq, &data->work);
5115 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5116 unsigned long *delay_on, unsigned long *delay_off)
5118 struct tpacpi_led_classdev *data = container_of(led_cdev,
5119 struct tpacpi_led_classdev, led_classdev);
5121 /* Can we choose the flash rate? */
5122 if (*delay_on == 0 && *delay_off == 0) {
5123 /* yes. set them to the hardware blink rate (1 Hz) */
5124 *delay_on = 500; /* ms */
5125 *delay_off = 500; /* ms */
5126 } else if ((*delay_on != 500) || (*delay_off != 500))
5127 return -EINVAL;
5129 data->new_state = TPACPI_LED_BLINK;
5130 queue_work(tpacpi_wq, &data->work);
5132 return 0;
5135 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5137 int rc;
5139 struct tpacpi_led_classdev *data = container_of(led_cdev,
5140 struct tpacpi_led_classdev, led_classdev);
5142 rc = led_get_status(data->led);
5144 if (rc == TPACPI_LED_OFF || rc < 0)
5145 rc = LED_OFF; /* no error handling in led class :( */
5146 else
5147 rc = LED_FULL;
5149 return rc;
5152 static void led_exit(void)
5154 unsigned int i;
5156 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5157 if (tpacpi_leds[i].led_classdev.name)
5158 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5161 kfree(tpacpi_leds);
5164 static int __init tpacpi_init_led(unsigned int led)
5166 int rc;
5168 tpacpi_leds[led].led = led;
5170 /* LEDs with no name don't get registered */
5171 if (!tpacpi_led_names[led])
5172 return 0;
5174 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5175 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5176 if (led_supported == TPACPI_LED_570)
5177 tpacpi_leds[led].led_classdev.brightness_get =
5178 &led_sysfs_get;
5180 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5182 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5184 rc = led_classdev_register(&tpacpi_pdev->dev,
5185 &tpacpi_leds[led].led_classdev);
5186 if (rc < 0)
5187 tpacpi_leds[led].led_classdev.name = NULL;
5189 return rc;
5192 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5193 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5194 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5195 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5197 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5198 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5199 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5200 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5201 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5202 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5203 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5204 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5206 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5207 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5208 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5209 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5210 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5212 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5213 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5214 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5215 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5217 /* (1) - may have excess leds enabled on MSB */
5219 /* Defaults (order matters, keep last, don't reorder!) */
5220 { /* Lenovo */
5221 .vendor = PCI_VENDOR_ID_LENOVO,
5222 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5223 .quirks = 0x1fffU,
5225 { /* IBM ThinkPads with no EC version string */
5226 .vendor = PCI_VENDOR_ID_IBM,
5227 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5228 .quirks = 0x00ffU,
5230 { /* IBM ThinkPads with EC version string */
5231 .vendor = PCI_VENDOR_ID_IBM,
5232 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5233 .quirks = 0x00bfU,
5237 #undef TPACPI_LEDQ_IBM
5238 #undef TPACPI_LEDQ_LNV
5240 static int __init led_init(struct ibm_init_struct *iibm)
5242 unsigned int i;
5243 int rc;
5244 unsigned long useful_leds;
5246 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5248 TPACPI_ACPIHANDLE_INIT(led);
5250 if (!led_handle)
5251 /* led not supported on R30, R31 */
5252 led_supported = TPACPI_LED_NONE;
5253 else if (tpacpi_is_ibm() && strlencmp(led_path, "SLED") == 0)
5254 /* 570 */
5255 led_supported = TPACPI_LED_570;
5256 else if (tpacpi_is_ibm() && strlencmp(led_path, "SYSL") == 0)
5257 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5258 led_supported = TPACPI_LED_OLD;
5259 else
5260 /* all others */
5261 led_supported = TPACPI_LED_NEW;
5263 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5264 str_supported(led_supported), led_supported);
5266 if (led_supported == TPACPI_LED_NONE)
5267 return 1;
5269 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5270 GFP_KERNEL);
5271 if (!tpacpi_leds) {
5272 printk(TPACPI_ERR "Out of memory for LED data\n");
5273 return -ENOMEM;
5276 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5277 ARRAY_SIZE(led_useful_qtable));
5279 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5280 if (!tpacpi_is_led_restricted(i) &&
5281 test_bit(i, &useful_leds)) {
5282 rc = tpacpi_init_led(i);
5283 if (rc < 0) {
5284 led_exit();
5285 return rc;
5290 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5291 printk(TPACPI_NOTICE
5292 "warning: userspace override of important "
5293 "firmware LEDs is enabled\n");
5294 #endif
5295 return 0;
5298 #define str_led_status(s) \
5299 ((s) == TPACPI_LED_OFF ? "off" : \
5300 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5302 static int led_read(struct seq_file *m)
5304 if (!led_supported) {
5305 seq_printf(m, "status:\t\tnot supported\n");
5306 return 0;
5308 seq_printf(m, "status:\t\tsupported\n");
5310 if (led_supported == TPACPI_LED_570) {
5311 /* 570 */
5312 int i, status;
5313 for (i = 0; i < 8; i++) {
5314 status = led_get_status(i);
5315 if (status < 0)
5316 return -EIO;
5317 seq_printf(m, "%d:\t\t%s\n",
5318 i, str_led_status(status));
5322 seq_printf(m, "commands:\t"
5323 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5325 return 0;
5328 static int led_write(char *buf)
5330 char *cmd;
5331 int led, rc;
5332 enum led_status_t s;
5334 if (!led_supported)
5335 return -ENODEV;
5337 while ((cmd = next_cmd(&buf))) {
5338 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5339 return -EINVAL;
5341 if (strstr(cmd, "off")) {
5342 s = TPACPI_LED_OFF;
5343 } else if (strstr(cmd, "on")) {
5344 s = TPACPI_LED_ON;
5345 } else if (strstr(cmd, "blink")) {
5346 s = TPACPI_LED_BLINK;
5347 } else {
5348 return -EINVAL;
5351 rc = led_set_status(led, s);
5352 if (rc < 0)
5353 return rc;
5356 return 0;
5359 static struct ibm_struct led_driver_data = {
5360 .name = "led",
5361 .read = led_read,
5362 .write = led_write,
5363 .exit = led_exit,
5366 /*************************************************************************
5367 * Beep subdriver
5370 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5372 #define TPACPI_BEEP_Q1 0x0001
5374 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5375 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5376 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5379 static int __init beep_init(struct ibm_init_struct *iibm)
5381 unsigned long quirks;
5383 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5385 TPACPI_ACPIHANDLE_INIT(beep);
5387 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5388 str_supported(beep_handle != NULL));
5390 quirks = tpacpi_check_quirks(beep_quirk_table,
5391 ARRAY_SIZE(beep_quirk_table));
5393 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5395 return (beep_handle)? 0 : 1;
5398 static int beep_read(struct seq_file *m)
5400 if (!beep_handle)
5401 seq_printf(m, "status:\t\tnot supported\n");
5402 else {
5403 seq_printf(m, "status:\t\tsupported\n");
5404 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5407 return 0;
5410 static int beep_write(char *buf)
5412 char *cmd;
5413 int beep_cmd;
5415 if (!beep_handle)
5416 return -ENODEV;
5418 while ((cmd = next_cmd(&buf))) {
5419 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5420 beep_cmd >= 0 && beep_cmd <= 17) {
5421 /* beep_cmd set */
5422 } else
5423 return -EINVAL;
5424 if (tp_features.beep_needs_two_args) {
5425 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5426 beep_cmd, 0))
5427 return -EIO;
5428 } else {
5429 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5430 beep_cmd))
5431 return -EIO;
5435 return 0;
5438 static struct ibm_struct beep_driver_data = {
5439 .name = "beep",
5440 .read = beep_read,
5441 .write = beep_write,
5444 /*************************************************************************
5445 * Thermal subdriver
5448 enum thermal_access_mode {
5449 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5450 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5451 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5452 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5453 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5456 enum { /* TPACPI_THERMAL_TPEC_* */
5457 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5458 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5459 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5461 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5465 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5466 struct ibm_thermal_sensors_struct {
5467 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5470 static enum thermal_access_mode thermal_read_mode;
5472 /* idx is zero-based */
5473 static int thermal_get_sensor(int idx, s32 *value)
5475 int t;
5476 s8 tmp;
5477 char tmpi[5];
5479 t = TP_EC_THERMAL_TMP0;
5481 switch (thermal_read_mode) {
5482 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5483 case TPACPI_THERMAL_TPEC_16:
5484 if (idx >= 8 && idx <= 15) {
5485 t = TP_EC_THERMAL_TMP8;
5486 idx -= 8;
5488 /* fallthrough */
5489 #endif
5490 case TPACPI_THERMAL_TPEC_8:
5491 if (idx <= 7) {
5492 if (!acpi_ec_read(t + idx, &tmp))
5493 return -EIO;
5494 *value = tmp * 1000;
5495 return 0;
5497 break;
5499 case TPACPI_THERMAL_ACPI_UPDT:
5500 if (idx <= 7) {
5501 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5502 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5503 return -EIO;
5504 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5505 return -EIO;
5506 *value = (t - 2732) * 100;
5507 return 0;
5509 break;
5511 case TPACPI_THERMAL_ACPI_TMP07:
5512 if (idx <= 7) {
5513 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5514 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5515 return -EIO;
5516 if (t > 127 || t < -127)
5517 t = TP_EC_THERMAL_TMP_NA;
5518 *value = t * 1000;
5519 return 0;
5521 break;
5523 case TPACPI_THERMAL_NONE:
5524 default:
5525 return -ENOSYS;
5528 return -EINVAL;
5531 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5533 int res, i;
5534 int n;
5536 n = 8;
5537 i = 0;
5539 if (!s)
5540 return -EINVAL;
5542 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5543 n = 16;
5545 for (i = 0 ; i < n; i++) {
5546 res = thermal_get_sensor(i, &s->temp[i]);
5547 if (res)
5548 return res;
5551 return n;
5554 static void thermal_dump_all_sensors(void)
5556 int n, i;
5557 struct ibm_thermal_sensors_struct t;
5559 n = thermal_get_sensors(&t);
5560 if (n <= 0)
5561 return;
5563 printk(TPACPI_NOTICE
5564 "temperatures (Celsius):");
5566 for (i = 0; i < n; i++) {
5567 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5568 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5569 else
5570 printk(KERN_CONT " N/A");
5573 printk(KERN_CONT "\n");
5576 /* sysfs temp##_input -------------------------------------------------- */
5578 static ssize_t thermal_temp_input_show(struct device *dev,
5579 struct device_attribute *attr,
5580 char *buf)
5582 struct sensor_device_attribute *sensor_attr =
5583 to_sensor_dev_attr(attr);
5584 int idx = sensor_attr->index;
5585 s32 value;
5586 int res;
5588 res = thermal_get_sensor(idx, &value);
5589 if (res)
5590 return res;
5591 if (value == TPACPI_THERMAL_SENSOR_NA)
5592 return -ENXIO;
5594 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5597 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5598 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5599 thermal_temp_input_show, NULL, _idxB)
5601 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5602 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5603 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5604 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5605 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5606 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5607 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5608 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5609 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5610 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5611 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5612 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5613 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5614 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5615 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5616 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5617 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5620 #define THERMAL_ATTRS(X) \
5621 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5623 static struct attribute *thermal_temp_input_attr[] = {
5624 THERMAL_ATTRS(8),
5625 THERMAL_ATTRS(9),
5626 THERMAL_ATTRS(10),
5627 THERMAL_ATTRS(11),
5628 THERMAL_ATTRS(12),
5629 THERMAL_ATTRS(13),
5630 THERMAL_ATTRS(14),
5631 THERMAL_ATTRS(15),
5632 THERMAL_ATTRS(0),
5633 THERMAL_ATTRS(1),
5634 THERMAL_ATTRS(2),
5635 THERMAL_ATTRS(3),
5636 THERMAL_ATTRS(4),
5637 THERMAL_ATTRS(5),
5638 THERMAL_ATTRS(6),
5639 THERMAL_ATTRS(7),
5640 NULL
5643 static const struct attribute_group thermal_temp_input16_group = {
5644 .attrs = thermal_temp_input_attr
5647 static const struct attribute_group thermal_temp_input8_group = {
5648 .attrs = &thermal_temp_input_attr[8]
5651 #undef THERMAL_SENSOR_ATTR_TEMP
5652 #undef THERMAL_ATTRS
5654 /* --------------------------------------------------------------------- */
5656 static int __init thermal_init(struct ibm_init_struct *iibm)
5658 u8 t, ta1, ta2;
5659 int i;
5660 int acpi_tmp7;
5661 int res;
5663 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5665 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5667 if (thinkpad_id.ec_model) {
5669 * Direct EC access mode: sensors at registers
5670 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5671 * non-implemented, thermal sensors return 0x80 when
5672 * not available
5675 ta1 = ta2 = 0;
5676 for (i = 0; i < 8; i++) {
5677 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5678 ta1 |= t;
5679 } else {
5680 ta1 = 0;
5681 break;
5683 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5684 ta2 |= t;
5685 } else {
5686 ta1 = 0;
5687 break;
5690 if (ta1 == 0) {
5691 /* This is sheer paranoia, but we handle it anyway */
5692 if (acpi_tmp7) {
5693 printk(TPACPI_ERR
5694 "ThinkPad ACPI EC access misbehaving, "
5695 "falling back to ACPI TMPx access "
5696 "mode\n");
5697 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5698 } else {
5699 printk(TPACPI_ERR
5700 "ThinkPad ACPI EC access misbehaving, "
5701 "disabling thermal sensors access\n");
5702 thermal_read_mode = TPACPI_THERMAL_NONE;
5704 } else {
5705 thermal_read_mode =
5706 (ta2 != 0) ?
5707 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5709 } else if (acpi_tmp7) {
5710 if (tpacpi_is_ibm() &&
5711 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5712 /* 600e/x, 770e, 770x */
5713 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5714 } else {
5715 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
5716 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5718 } else {
5719 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5720 thermal_read_mode = TPACPI_THERMAL_NONE;
5723 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5724 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5725 thermal_read_mode);
5727 switch (thermal_read_mode) {
5728 case TPACPI_THERMAL_TPEC_16:
5729 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5730 &thermal_temp_input16_group);
5731 if (res)
5732 return res;
5733 break;
5734 case TPACPI_THERMAL_TPEC_8:
5735 case TPACPI_THERMAL_ACPI_TMP07:
5736 case TPACPI_THERMAL_ACPI_UPDT:
5737 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5738 &thermal_temp_input8_group);
5739 if (res)
5740 return res;
5741 break;
5742 case TPACPI_THERMAL_NONE:
5743 default:
5744 return 1;
5747 return 0;
5750 static void thermal_exit(void)
5752 switch (thermal_read_mode) {
5753 case TPACPI_THERMAL_TPEC_16:
5754 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5755 &thermal_temp_input16_group);
5756 break;
5757 case TPACPI_THERMAL_TPEC_8:
5758 case TPACPI_THERMAL_ACPI_TMP07:
5759 case TPACPI_THERMAL_ACPI_UPDT:
5760 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5761 &thermal_temp_input8_group);
5762 break;
5763 case TPACPI_THERMAL_NONE:
5764 default:
5765 break;
5769 static int thermal_read(struct seq_file *m)
5771 int n, i;
5772 struct ibm_thermal_sensors_struct t;
5774 n = thermal_get_sensors(&t);
5775 if (unlikely(n < 0))
5776 return n;
5778 seq_printf(m, "temperatures:\t");
5780 if (n > 0) {
5781 for (i = 0; i < (n - 1); i++)
5782 seq_printf(m, "%d ", t.temp[i] / 1000);
5783 seq_printf(m, "%d\n", t.temp[i] / 1000);
5784 } else
5785 seq_printf(m, "not supported\n");
5787 return 0;
5790 static struct ibm_struct thermal_driver_data = {
5791 .name = "thermal",
5792 .read = thermal_read,
5793 .exit = thermal_exit,
5796 /*************************************************************************
5797 * EC Dump subdriver
5800 static u8 ecdump_regs[256];
5802 static int ecdump_read(struct seq_file *m)
5804 int i, j;
5805 u8 v;
5807 seq_printf(m, "EC "
5808 " +00 +01 +02 +03 +04 +05 +06 +07"
5809 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5810 for (i = 0; i < 256; i += 16) {
5811 seq_printf(m, "EC 0x%02x:", i);
5812 for (j = 0; j < 16; j++) {
5813 if (!acpi_ec_read(i + j, &v))
5814 break;
5815 if (v != ecdump_regs[i + j])
5816 seq_printf(m, " *%02x", v);
5817 else
5818 seq_printf(m, " %02x", v);
5819 ecdump_regs[i + j] = v;
5821 seq_putc(m, '\n');
5822 if (j != 16)
5823 break;
5826 /* These are way too dangerous to advertise openly... */
5827 #if 0
5828 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5829 " (<offset> is 00-ff, <value> is 00-ff)\n");
5830 seq_printf(m, "commands:\t0x<offset> <value> "
5831 " (<offset> is 00-ff, <value> is 0-255)\n");
5832 #endif
5833 return 0;
5836 static int ecdump_write(char *buf)
5838 char *cmd;
5839 int i, v;
5841 while ((cmd = next_cmd(&buf))) {
5842 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5843 /* i and v set */
5844 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5845 /* i and v set */
5846 } else
5847 return -EINVAL;
5848 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5849 if (!acpi_ec_write(i, v))
5850 return -EIO;
5851 } else
5852 return -EINVAL;
5855 return 0;
5858 static struct ibm_struct ecdump_driver_data = {
5859 .name = "ecdump",
5860 .read = ecdump_read,
5861 .write = ecdump_write,
5862 .flags.experimental = 1,
5865 /*************************************************************************
5866 * Backlight/brightness subdriver
5869 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5872 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5873 * CMOS NVRAM byte 0x5E, bits 0-3.
5875 * EC HBRV (0x31) has the following layout
5876 * Bit 7: unknown function
5877 * Bit 6: unknown function
5878 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5879 * Bit 4: must be set to zero to avoid problems
5880 * Bit 3-0: backlight brightness level
5882 * brightness_get_raw returns status data in the HBRV layout
5884 * WARNING: The X61 has been verified to use HBRV for something else, so
5885 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5886 * testing on the very early *60 Lenovo models...
5889 enum {
5890 TP_EC_BACKLIGHT = 0x31,
5892 /* TP_EC_BACKLIGHT bitmasks */
5893 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5894 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5895 TP_EC_BACKLIGHT_MAPSW = 0x20,
5898 enum tpacpi_brightness_access_mode {
5899 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5900 TPACPI_BRGHT_MODE_EC, /* EC control */
5901 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5902 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5903 TPACPI_BRGHT_MODE_MAX
5906 static struct backlight_device *ibm_backlight_device;
5908 static enum tpacpi_brightness_access_mode brightness_mode =
5909 TPACPI_BRGHT_MODE_MAX;
5911 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5913 static struct mutex brightness_mutex;
5915 /* NVRAM brightness access,
5916 * call with brightness_mutex held! */
5917 static unsigned int tpacpi_brightness_nvram_get(void)
5919 u8 lnvram;
5921 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5922 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5923 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5924 lnvram &= bright_maxlvl;
5926 return lnvram;
5929 static void tpacpi_brightness_checkpoint_nvram(void)
5931 u8 lec = 0;
5932 u8 b_nvram;
5934 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5935 return;
5937 vdbg_printk(TPACPI_DBG_BRGHT,
5938 "trying to checkpoint backlight level to NVRAM...\n");
5940 if (mutex_lock_killable(&brightness_mutex) < 0)
5941 return;
5943 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5944 goto unlock;
5945 lec &= TP_EC_BACKLIGHT_LVLMSK;
5946 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5948 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5949 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5950 /* NVRAM needs update */
5951 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5952 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5953 b_nvram |= lec;
5954 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5955 dbg_printk(TPACPI_DBG_BRGHT,
5956 "updated NVRAM backlight level to %u (0x%02x)\n",
5957 (unsigned int) lec, (unsigned int) b_nvram);
5958 } else
5959 vdbg_printk(TPACPI_DBG_BRGHT,
5960 "NVRAM backlight level already is %u (0x%02x)\n",
5961 (unsigned int) lec, (unsigned int) b_nvram);
5963 unlock:
5964 mutex_unlock(&brightness_mutex);
5968 /* call with brightness_mutex held! */
5969 static int tpacpi_brightness_get_raw(int *status)
5971 u8 lec = 0;
5973 switch (brightness_mode) {
5974 case TPACPI_BRGHT_MODE_UCMS_STEP:
5975 *status = tpacpi_brightness_nvram_get();
5976 return 0;
5977 case TPACPI_BRGHT_MODE_EC:
5978 case TPACPI_BRGHT_MODE_ECNVRAM:
5979 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5980 return -EIO;
5981 *status = lec;
5982 return 0;
5983 default:
5984 return -ENXIO;
5988 /* call with brightness_mutex held! */
5989 /* do NOT call with illegal backlight level value */
5990 static int tpacpi_brightness_set_ec(unsigned int value)
5992 u8 lec = 0;
5994 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5995 return -EIO;
5997 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5998 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5999 (value & TP_EC_BACKLIGHT_LVLMSK))))
6000 return -EIO;
6002 return 0;
6005 /* call with brightness_mutex held! */
6006 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6008 int cmos_cmd, inc;
6009 unsigned int current_value, i;
6011 current_value = tpacpi_brightness_nvram_get();
6013 if (value == current_value)
6014 return 0;
6016 cmos_cmd = (value > current_value) ?
6017 TP_CMOS_BRIGHTNESS_UP :
6018 TP_CMOS_BRIGHTNESS_DOWN;
6019 inc = (value > current_value) ? 1 : -1;
6021 for (i = current_value; i != value; i += inc)
6022 if (issue_thinkpad_cmos_command(cmos_cmd))
6023 return -EIO;
6025 return 0;
6028 /* May return EINTR which can always be mapped to ERESTARTSYS */
6029 static int brightness_set(unsigned int value)
6031 int res;
6033 if (value > bright_maxlvl || value < 0)
6034 return -EINVAL;
6036 vdbg_printk(TPACPI_DBG_BRGHT,
6037 "set backlight level to %d\n", value);
6039 res = mutex_lock_killable(&brightness_mutex);
6040 if (res < 0)
6041 return res;
6043 switch (brightness_mode) {
6044 case TPACPI_BRGHT_MODE_EC:
6045 case TPACPI_BRGHT_MODE_ECNVRAM:
6046 res = tpacpi_brightness_set_ec(value);
6047 break;
6048 case TPACPI_BRGHT_MODE_UCMS_STEP:
6049 res = tpacpi_brightness_set_ucmsstep(value);
6050 break;
6051 default:
6052 res = -ENXIO;
6055 mutex_unlock(&brightness_mutex);
6056 return res;
6059 /* sysfs backlight class ----------------------------------------------- */
6061 static int brightness_update_status(struct backlight_device *bd)
6063 unsigned int level =
6064 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6065 bd->props.power == FB_BLANK_UNBLANK) ?
6066 bd->props.brightness : 0;
6068 dbg_printk(TPACPI_DBG_BRGHT,
6069 "backlight: attempt to set level to %d\n",
6070 level);
6072 /* it is the backlight class's job (caller) to handle
6073 * EINTR and other errors properly */
6074 return brightness_set(level);
6077 static int brightness_get(struct backlight_device *bd)
6079 int status, res;
6081 res = mutex_lock_killable(&brightness_mutex);
6082 if (res < 0)
6083 return 0;
6085 res = tpacpi_brightness_get_raw(&status);
6087 mutex_unlock(&brightness_mutex);
6089 if (res < 0)
6090 return 0;
6092 return status & TP_EC_BACKLIGHT_LVLMSK;
6095 static void tpacpi_brightness_notify_change(void)
6097 backlight_force_update(ibm_backlight_device,
6098 BACKLIGHT_UPDATE_HOTKEY);
6101 static struct backlight_ops ibm_backlight_data = {
6102 .get_brightness = brightness_get,
6103 .update_status = brightness_update_status,
6106 /* --------------------------------------------------------------------- */
6108 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6110 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6111 union acpi_object *obj;
6112 int rc;
6114 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
6115 obj = (union acpi_object *)buffer.pointer;
6116 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6117 printk(TPACPI_ERR "Unknown _BCL data, "
6118 "please report this to %s\n", TPACPI_MAIL);
6119 rc = 0;
6120 } else {
6121 rc = obj->package.count;
6123 } else {
6124 return 0;
6127 kfree(buffer.pointer);
6128 return rc;
6131 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
6132 u32 lvl, void *context, void **rv)
6134 char name[ACPI_PATH_SEGMENT_LENGTH];
6135 struct acpi_buffer buffer = { sizeof(name), &name };
6137 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
6138 !strncmp("_BCL", name, sizeof(name) - 1)) {
6139 BUG_ON(!rv || !*rv);
6140 **(int **)rv = tpacpi_query_bcl_levels(handle);
6141 return AE_CTRL_TERMINATE;
6142 } else {
6143 return AE_OK;
6148 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6150 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6152 int status;
6153 int bcl_levels = 0;
6154 void *bcl_ptr = &bcl_levels;
6156 if (!vid_handle)
6157 TPACPI_ACPIHANDLE_INIT(vid);
6159 if (!vid_handle)
6160 return 0;
6163 * Search for a _BCL method, and execute it. This is safe on all
6164 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
6165 * BIOS in ACPI backlight control mode. We do NOT have to care
6166 * about calling the _BCL method in an enabled video device, any
6167 * will do for our purposes.
6170 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
6171 tpacpi_acpi_walk_find_bcl, NULL,
6172 &bcl_ptr);
6174 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
6175 tp_features.bright_acpimode = 1;
6176 return bcl_levels - 2;
6179 return 0;
6183 * These are only useful for models that have only one possibility
6184 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6185 * these quirks.
6187 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6188 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6189 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6191 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6192 /* Models with ATI GPUs known to require ECNVRAM mode */
6193 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6195 /* Models with ATI GPUs that can use ECNVRAM */
6196 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6197 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6198 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6199 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6201 /* Models with Intel Extreme Graphics 2 */
6202 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6203 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6204 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6206 /* Models with Intel GMA900 */
6207 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6208 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6209 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6213 * Returns < 0 for error, otherwise sets tp_features.bright_*
6214 * and bright_maxlvl.
6216 static void __init tpacpi_detect_brightness_capabilities(void)
6218 unsigned int b;
6220 vdbg_printk(TPACPI_DBG_INIT,
6221 "detecting firmware brightness interface capabilities\n");
6223 /* we could run a quirks check here (same table used by
6224 * brightness_init) if needed */
6227 * We always attempt to detect acpi support, so as to switch
6228 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6229 * going to publish a backlight interface
6231 b = tpacpi_check_std_acpi_brightness_support();
6232 switch (b) {
6233 case 16:
6234 bright_maxlvl = 15;
6235 printk(TPACPI_INFO
6236 "detected a 16-level brightness capable ThinkPad\n");
6237 break;
6238 case 8:
6239 case 0:
6240 bright_maxlvl = 7;
6241 printk(TPACPI_INFO
6242 "detected a 8-level brightness capable ThinkPad\n");
6243 break;
6244 default:
6245 printk(TPACPI_ERR
6246 "Unsupported brightness interface, "
6247 "please contact %s\n", TPACPI_MAIL);
6248 tp_features.bright_unkfw = 1;
6249 bright_maxlvl = b - 1;
6253 static int __init brightness_init(struct ibm_init_struct *iibm)
6255 int b;
6256 unsigned long quirks;
6258 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6260 mutex_init(&brightness_mutex);
6262 quirks = tpacpi_check_quirks(brightness_quirk_table,
6263 ARRAY_SIZE(brightness_quirk_table));
6265 /* tpacpi_detect_brightness_capabilities() must have run already */
6267 /* if it is unknown, we don't handle it: it wouldn't be safe */
6268 if (tp_features.bright_unkfw)
6269 return 1;
6271 if (tp_features.bright_acpimode) {
6272 if (acpi_video_backlight_support()) {
6273 if (brightness_enable > 1) {
6274 printk(TPACPI_NOTICE
6275 "Standard ACPI backlight interface "
6276 "available, not loading native one.\n");
6277 return 1;
6278 } else if (brightness_enable == 1) {
6279 printk(TPACPI_NOTICE
6280 "Backlight control force enabled, even if standard "
6281 "ACPI backlight interface is available\n");
6283 } else {
6284 if (brightness_enable > 1) {
6285 printk(TPACPI_NOTICE
6286 "Standard ACPI backlight interface not "
6287 "available, thinkpad_acpi native "
6288 "brightness control enabled\n");
6293 if (!brightness_enable) {
6294 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6295 "brightness support disabled by "
6296 "module parameter\n");
6297 return 1;
6301 * Check for module parameter bogosity, note that we
6302 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6303 * able to detect "unspecified"
6305 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6306 return -EINVAL;
6308 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6309 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6310 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6311 if (quirks & TPACPI_BRGHT_Q_EC)
6312 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6313 else
6314 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6316 dbg_printk(TPACPI_DBG_BRGHT,
6317 "driver auto-selected brightness_mode=%d\n",
6318 brightness_mode);
6321 /* Safety */
6322 if (!tpacpi_is_ibm() &&
6323 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6324 brightness_mode == TPACPI_BRGHT_MODE_EC))
6325 return -EINVAL;
6327 if (tpacpi_brightness_get_raw(&b) < 0)
6328 return 1;
6330 ibm_backlight_device = backlight_device_register(
6331 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6332 &ibm_backlight_data);
6333 if (IS_ERR(ibm_backlight_device)) {
6334 int rc = PTR_ERR(ibm_backlight_device);
6335 ibm_backlight_device = NULL;
6336 printk(TPACPI_ERR "Could not register backlight device\n");
6337 return rc;
6339 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6340 "brightness is supported\n");
6342 if (quirks & TPACPI_BRGHT_Q_ASK) {
6343 printk(TPACPI_NOTICE
6344 "brightness: will use unverified default: "
6345 "brightness_mode=%d\n", brightness_mode);
6346 printk(TPACPI_NOTICE
6347 "brightness: please report to %s whether it works well "
6348 "or not on your ThinkPad\n", TPACPI_MAIL);
6351 ibm_backlight_device->props.max_brightness = bright_maxlvl;
6352 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6353 backlight_update_status(ibm_backlight_device);
6355 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6356 "brightness: registering brightness hotkeys "
6357 "as change notification\n");
6358 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6359 | TP_ACPI_HKEY_BRGHTUP_MASK
6360 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6361 return 0;
6364 static void brightness_suspend(pm_message_t state)
6366 tpacpi_brightness_checkpoint_nvram();
6369 static void brightness_shutdown(void)
6371 tpacpi_brightness_checkpoint_nvram();
6374 static void brightness_exit(void)
6376 if (ibm_backlight_device) {
6377 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6378 "calling backlight_device_unregister()\n");
6379 backlight_device_unregister(ibm_backlight_device);
6382 tpacpi_brightness_checkpoint_nvram();
6385 static int brightness_read(struct seq_file *m)
6387 int level;
6389 level = brightness_get(NULL);
6390 if (level < 0) {
6391 seq_printf(m, "level:\t\tunreadable\n");
6392 } else {
6393 seq_printf(m, "level:\t\t%d\n", level);
6394 seq_printf(m, "commands:\tup, down\n");
6395 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6396 bright_maxlvl);
6399 return 0;
6402 static int brightness_write(char *buf)
6404 int level;
6405 int rc;
6406 char *cmd;
6408 level = brightness_get(NULL);
6409 if (level < 0)
6410 return level;
6412 while ((cmd = next_cmd(&buf))) {
6413 if (strlencmp(cmd, "up") == 0) {
6414 if (level < bright_maxlvl)
6415 level++;
6416 } else if (strlencmp(cmd, "down") == 0) {
6417 if (level > 0)
6418 level--;
6419 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6420 level >= 0 && level <= bright_maxlvl) {
6421 /* new level set */
6422 } else
6423 return -EINVAL;
6426 tpacpi_disclose_usertask("procfs brightness",
6427 "set level to %d\n", level);
6430 * Now we know what the final level should be, so we try to set it.
6431 * Doing it this way makes the syscall restartable in case of EINTR
6433 rc = brightness_set(level);
6434 if (!rc && ibm_backlight_device)
6435 backlight_force_update(ibm_backlight_device,
6436 BACKLIGHT_UPDATE_SYSFS);
6437 return (rc == -EINTR)? -ERESTARTSYS : rc;
6440 static struct ibm_struct brightness_driver_data = {
6441 .name = "brightness",
6442 .read = brightness_read,
6443 .write = brightness_write,
6444 .exit = brightness_exit,
6445 .suspend = brightness_suspend,
6446 .shutdown = brightness_shutdown,
6449 /*************************************************************************
6450 * Volume subdriver
6454 * IBM ThinkPads have a simple volume controller with MUTE gating.
6455 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6457 * Since the *61 series (and probably also the later *60 series), Lenovo
6458 * ThinkPads only implement the MUTE gate.
6460 * EC register 0x30
6461 * Bit 6: MUTE (1 mutes sound)
6462 * Bit 3-0: Volume
6463 * Other bits should be zero as far as we know.
6465 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6466 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6467 * such as bit 7 which is used to detect repeated presses of MUTE,
6468 * and we leave them unchanged.
6471 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6473 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6474 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6475 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6477 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6478 static char *alsa_id = "ThinkPadEC";
6479 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6481 struct tpacpi_alsa_data {
6482 struct snd_card *card;
6483 struct snd_ctl_elem_id *ctl_mute_id;
6484 struct snd_ctl_elem_id *ctl_vol_id;
6487 static struct snd_card *alsa_card;
6489 enum {
6490 TP_EC_AUDIO = 0x30,
6492 /* TP_EC_AUDIO bits */
6493 TP_EC_AUDIO_MUTESW = 6,
6495 /* TP_EC_AUDIO bitmasks */
6496 TP_EC_AUDIO_LVL_MSK = 0x0F,
6497 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6499 /* Maximum volume */
6500 TP_EC_VOLUME_MAX = 14,
6503 enum tpacpi_volume_access_mode {
6504 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6505 TPACPI_VOL_MODE_EC, /* Pure EC control */
6506 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6507 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6508 TPACPI_VOL_MODE_MAX
6511 enum tpacpi_volume_capabilities {
6512 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6513 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6514 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6515 TPACPI_VOL_CAP_MAX
6518 static enum tpacpi_volume_access_mode volume_mode =
6519 TPACPI_VOL_MODE_MAX;
6521 static enum tpacpi_volume_capabilities volume_capabilities;
6522 static int volume_control_allowed;
6525 * Used to syncronize writers to TP_EC_AUDIO and
6526 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6528 static struct mutex volume_mutex;
6530 static void tpacpi_volume_checkpoint_nvram(void)
6532 u8 lec = 0;
6533 u8 b_nvram;
6534 u8 ec_mask;
6536 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6537 return;
6538 if (!volume_control_allowed)
6539 return;
6541 vdbg_printk(TPACPI_DBG_MIXER,
6542 "trying to checkpoint mixer state to NVRAM...\n");
6544 if (tp_features.mixer_no_level_control)
6545 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6546 else
6547 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6549 if (mutex_lock_killable(&volume_mutex) < 0)
6550 return;
6552 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6553 goto unlock;
6554 lec &= ec_mask;
6555 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6557 if (lec != (b_nvram & ec_mask)) {
6558 /* NVRAM needs update */
6559 b_nvram &= ~ec_mask;
6560 b_nvram |= lec;
6561 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6562 dbg_printk(TPACPI_DBG_MIXER,
6563 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6564 (unsigned int) lec, (unsigned int) b_nvram);
6565 } else {
6566 vdbg_printk(TPACPI_DBG_MIXER,
6567 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6568 (unsigned int) lec, (unsigned int) b_nvram);
6571 unlock:
6572 mutex_unlock(&volume_mutex);
6575 static int volume_get_status_ec(u8 *status)
6577 u8 s;
6579 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6580 return -EIO;
6582 *status = s;
6584 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6586 return 0;
6589 static int volume_get_status(u8 *status)
6591 return volume_get_status_ec(status);
6594 static int volume_set_status_ec(const u8 status)
6596 if (!acpi_ec_write(TP_EC_AUDIO, status))
6597 return -EIO;
6599 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6601 return 0;
6604 static int volume_set_status(const u8 status)
6606 return volume_set_status_ec(status);
6609 /* returns < 0 on error, 0 on no change, 1 on change */
6610 static int __volume_set_mute_ec(const bool mute)
6612 int rc;
6613 u8 s, n;
6615 if (mutex_lock_killable(&volume_mutex) < 0)
6616 return -EINTR;
6618 rc = volume_get_status_ec(&s);
6619 if (rc)
6620 goto unlock;
6622 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6623 s & ~TP_EC_AUDIO_MUTESW_MSK;
6625 if (n != s) {
6626 rc = volume_set_status_ec(n);
6627 if (!rc)
6628 rc = 1;
6631 unlock:
6632 mutex_unlock(&volume_mutex);
6633 return rc;
6636 static int volume_alsa_set_mute(const bool mute)
6638 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6639 (mute) ? "" : "un");
6640 return __volume_set_mute_ec(mute);
6643 static int volume_set_mute(const bool mute)
6645 int rc;
6647 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6648 (mute) ? "" : "un");
6650 rc = __volume_set_mute_ec(mute);
6651 return (rc < 0) ? rc : 0;
6654 /* returns < 0 on error, 0 on no change, 1 on change */
6655 static int __volume_set_volume_ec(const u8 vol)
6657 int rc;
6658 u8 s, n;
6660 if (vol > TP_EC_VOLUME_MAX)
6661 return -EINVAL;
6663 if (mutex_lock_killable(&volume_mutex) < 0)
6664 return -EINTR;
6666 rc = volume_get_status_ec(&s);
6667 if (rc)
6668 goto unlock;
6670 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6672 if (n != s) {
6673 rc = volume_set_status_ec(n);
6674 if (!rc)
6675 rc = 1;
6678 unlock:
6679 mutex_unlock(&volume_mutex);
6680 return rc;
6683 static int volume_alsa_set_volume(const u8 vol)
6685 dbg_printk(TPACPI_DBG_MIXER,
6686 "ALSA: trying to set volume level to %hu\n", vol);
6687 return __volume_set_volume_ec(vol);
6690 static void volume_alsa_notify_change(void)
6692 struct tpacpi_alsa_data *d;
6694 if (alsa_card && alsa_card->private_data) {
6695 d = alsa_card->private_data;
6696 if (d->ctl_mute_id)
6697 snd_ctl_notify(alsa_card,
6698 SNDRV_CTL_EVENT_MASK_VALUE,
6699 d->ctl_mute_id);
6700 if (d->ctl_vol_id)
6701 snd_ctl_notify(alsa_card,
6702 SNDRV_CTL_EVENT_MASK_VALUE,
6703 d->ctl_vol_id);
6707 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6708 struct snd_ctl_elem_info *uinfo)
6710 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6711 uinfo->count = 1;
6712 uinfo->value.integer.min = 0;
6713 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6714 return 0;
6717 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6718 struct snd_ctl_elem_value *ucontrol)
6720 u8 s;
6721 int rc;
6723 rc = volume_get_status(&s);
6724 if (rc < 0)
6725 return rc;
6727 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6728 return 0;
6731 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6732 struct snd_ctl_elem_value *ucontrol)
6734 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6737 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6739 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6740 struct snd_ctl_elem_value *ucontrol)
6742 u8 s;
6743 int rc;
6745 rc = volume_get_status(&s);
6746 if (rc < 0)
6747 return rc;
6749 ucontrol->value.integer.value[0] =
6750 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6751 return 0;
6754 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6755 struct snd_ctl_elem_value *ucontrol)
6757 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6760 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6761 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6762 .name = "Console Playback Volume",
6763 .index = 0,
6764 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6765 .info = volume_alsa_vol_info,
6766 .get = volume_alsa_vol_get,
6769 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6770 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6771 .name = "Console Playback Switch",
6772 .index = 0,
6773 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6774 .info = volume_alsa_mute_info,
6775 .get = volume_alsa_mute_get,
6778 static void volume_suspend(pm_message_t state)
6780 tpacpi_volume_checkpoint_nvram();
6783 static void volume_resume(void)
6785 volume_alsa_notify_change();
6788 static void volume_shutdown(void)
6790 tpacpi_volume_checkpoint_nvram();
6793 static void volume_exit(void)
6795 if (alsa_card) {
6796 snd_card_free(alsa_card);
6797 alsa_card = NULL;
6800 tpacpi_volume_checkpoint_nvram();
6803 static int __init volume_create_alsa_mixer(void)
6805 struct snd_card *card;
6806 struct tpacpi_alsa_data *data;
6807 struct snd_kcontrol *ctl_vol;
6808 struct snd_kcontrol *ctl_mute;
6809 int rc;
6811 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6812 sizeof(struct tpacpi_alsa_data), &card);
6813 if (rc < 0 || !card) {
6814 printk(TPACPI_ERR
6815 "Failed to create ALSA card structures: %d\n", rc);
6816 return 1;
6819 BUG_ON(!card->private_data);
6820 data = card->private_data;
6821 data->card = card;
6823 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6824 sizeof(card->driver));
6825 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6826 sizeof(card->shortname));
6827 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6828 (thinkpad_id.ec_version_str) ?
6829 thinkpad_id.ec_version_str : "(unknown)");
6830 snprintf(card->longname, sizeof(card->longname),
6831 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6832 (thinkpad_id.ec_version_str) ?
6833 thinkpad_id.ec_version_str : "unknown");
6835 if (volume_control_allowed) {
6836 volume_alsa_control_vol.put = volume_alsa_vol_put;
6837 volume_alsa_control_vol.access =
6838 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6840 volume_alsa_control_mute.put = volume_alsa_mute_put;
6841 volume_alsa_control_mute.access =
6842 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6845 if (!tp_features.mixer_no_level_control) {
6846 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6847 rc = snd_ctl_add(card, ctl_vol);
6848 if (rc < 0) {
6849 printk(TPACPI_ERR
6850 "Failed to create ALSA volume control: %d\n",
6851 rc);
6852 goto err_exit;
6854 data->ctl_vol_id = &ctl_vol->id;
6857 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6858 rc = snd_ctl_add(card, ctl_mute);
6859 if (rc < 0) {
6860 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6861 rc);
6862 goto err_exit;
6864 data->ctl_mute_id = &ctl_mute->id;
6866 snd_card_set_dev(card, &tpacpi_pdev->dev);
6867 rc = snd_card_register(card);
6868 if (rc < 0) {
6869 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6870 goto err_exit;
6873 alsa_card = card;
6874 return 0;
6876 err_exit:
6877 snd_card_free(card);
6878 return 1;
6881 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6882 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6884 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6885 /* Whitelist volume level on all IBM by default */
6886 { .vendor = PCI_VENDOR_ID_IBM,
6887 .bios = TPACPI_MATCH_ANY,
6888 .ec = TPACPI_MATCH_ANY,
6889 .quirks = TPACPI_VOL_Q_LEVEL },
6891 /* Lenovo models with volume control (needs confirmation) */
6892 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6893 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6894 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6895 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6896 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6897 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6898 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6900 /* Whitelist mute-only on all Lenovo by default */
6901 { .vendor = PCI_VENDOR_ID_LENOVO,
6902 .bios = TPACPI_MATCH_ANY,
6903 .ec = TPACPI_MATCH_ANY,
6904 .quirks = TPACPI_VOL_Q_MUTEONLY }
6907 static int __init volume_init(struct ibm_init_struct *iibm)
6909 unsigned long quirks;
6910 int rc;
6912 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6914 mutex_init(&volume_mutex);
6917 * Check for module parameter bogosity, note that we
6918 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6919 * able to detect "unspecified"
6921 if (volume_mode > TPACPI_VOL_MODE_MAX)
6922 return -EINVAL;
6924 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6925 printk(TPACPI_ERR
6926 "UCMS step volume mode not implemented, "
6927 "please contact %s\n", TPACPI_MAIL);
6928 return 1;
6931 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6932 return -EINVAL;
6935 * The ALSA mixer is our primary interface.
6936 * When disabled, don't install the subdriver at all
6938 if (!alsa_enable) {
6939 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6940 "ALSA mixer disabled by parameter, "
6941 "not loading volume subdriver...\n");
6942 return 1;
6945 quirks = tpacpi_check_quirks(volume_quirk_table,
6946 ARRAY_SIZE(volume_quirk_table));
6948 switch (volume_capabilities) {
6949 case TPACPI_VOL_CAP_AUTO:
6950 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6951 tp_features.mixer_no_level_control = 1;
6952 else if (quirks & TPACPI_VOL_Q_LEVEL)
6953 tp_features.mixer_no_level_control = 0;
6954 else
6955 return 1; /* no mixer */
6956 break;
6957 case TPACPI_VOL_CAP_VOLMUTE:
6958 tp_features.mixer_no_level_control = 0;
6959 break;
6960 case TPACPI_VOL_CAP_MUTEONLY:
6961 tp_features.mixer_no_level_control = 1;
6962 break;
6963 default:
6964 return 1;
6967 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6968 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6969 "using user-supplied volume_capabilities=%d\n",
6970 volume_capabilities);
6972 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6973 volume_mode == TPACPI_VOL_MODE_MAX) {
6974 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6976 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6977 "driver auto-selected volume_mode=%d\n",
6978 volume_mode);
6979 } else {
6980 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6981 "using user-supplied volume_mode=%d\n",
6982 volume_mode);
6985 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6986 "mute is supported, volume control is %s\n",
6987 str_supported(!tp_features.mixer_no_level_control));
6989 rc = volume_create_alsa_mixer();
6990 if (rc) {
6991 printk(TPACPI_ERR
6992 "Could not create the ALSA mixer interface\n");
6993 return rc;
6996 printk(TPACPI_INFO
6997 "Console audio control enabled, mode: %s\n",
6998 (volume_control_allowed) ?
6999 "override (read/write)" :
7000 "monitor (read only)");
7002 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7003 "registering volume hotkeys as change notification\n");
7004 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7005 | TP_ACPI_HKEY_VOLUP_MASK
7006 | TP_ACPI_HKEY_VOLDWN_MASK
7007 | TP_ACPI_HKEY_MUTE_MASK);
7009 return 0;
7012 static int volume_read(struct seq_file *m)
7014 u8 status;
7016 if (volume_get_status(&status) < 0) {
7017 seq_printf(m, "level:\t\tunreadable\n");
7018 } else {
7019 if (tp_features.mixer_no_level_control)
7020 seq_printf(m, "level:\t\tunsupported\n");
7021 else
7022 seq_printf(m, "level:\t\t%d\n",
7023 status & TP_EC_AUDIO_LVL_MSK);
7025 seq_printf(m, "mute:\t\t%s\n",
7026 onoff(status, TP_EC_AUDIO_MUTESW));
7028 if (volume_control_allowed) {
7029 seq_printf(m, "commands:\tunmute, mute\n");
7030 if (!tp_features.mixer_no_level_control) {
7031 seq_printf(m,
7032 "commands:\tup, down\n");
7033 seq_printf(m,
7034 "commands:\tlevel <level>"
7035 " (<level> is 0-%d)\n",
7036 TP_EC_VOLUME_MAX);
7041 return 0;
7044 static int volume_write(char *buf)
7046 u8 s;
7047 u8 new_level, new_mute;
7048 int l;
7049 char *cmd;
7050 int rc;
7053 * We do allow volume control at driver startup, so that the
7054 * user can set initial state through the volume=... parameter hack.
7056 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7057 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7058 tp_warned.volume_ctrl_forbidden = 1;
7059 printk(TPACPI_NOTICE
7060 "Console audio control in monitor mode, "
7061 "changes are not allowed.\n");
7062 printk(TPACPI_NOTICE
7063 "Use the volume_control=1 module parameter "
7064 "to enable volume control\n");
7066 return -EPERM;
7069 rc = volume_get_status(&s);
7070 if (rc < 0)
7071 return rc;
7073 new_level = s & TP_EC_AUDIO_LVL_MSK;
7074 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7076 while ((cmd = next_cmd(&buf))) {
7077 if (!tp_features.mixer_no_level_control) {
7078 if (strlencmp(cmd, "up") == 0) {
7079 if (new_mute)
7080 new_mute = 0;
7081 else if (new_level < TP_EC_VOLUME_MAX)
7082 new_level++;
7083 continue;
7084 } else if (strlencmp(cmd, "down") == 0) {
7085 if (new_mute)
7086 new_mute = 0;
7087 else if (new_level > 0)
7088 new_level--;
7089 continue;
7090 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7091 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7092 new_level = l;
7093 continue;
7096 if (strlencmp(cmd, "mute") == 0)
7097 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7098 else if (strlencmp(cmd, "unmute") == 0)
7099 new_mute = 0;
7100 else
7101 return -EINVAL;
7104 if (tp_features.mixer_no_level_control) {
7105 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7106 new_mute ? "" : "un");
7107 rc = volume_set_mute(!!new_mute);
7108 } else {
7109 tpacpi_disclose_usertask("procfs volume",
7110 "%smute and set level to %d\n",
7111 new_mute ? "" : "un", new_level);
7112 rc = volume_set_status(new_mute | new_level);
7114 volume_alsa_notify_change();
7116 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7119 static struct ibm_struct volume_driver_data = {
7120 .name = "volume",
7121 .read = volume_read,
7122 .write = volume_write,
7123 .exit = volume_exit,
7124 .suspend = volume_suspend,
7125 .resume = volume_resume,
7126 .shutdown = volume_shutdown,
7129 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7131 #define alsa_card NULL
7133 static void inline volume_alsa_notify_change(void)
7137 static int __init volume_init(struct ibm_init_struct *iibm)
7139 printk(TPACPI_INFO
7140 "volume: disabled as there is no ALSA support in this kernel\n");
7142 return 1;
7145 static struct ibm_struct volume_driver_data = {
7146 .name = "volume",
7149 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7151 /*************************************************************************
7152 * Fan subdriver
7156 * FAN ACCESS MODES
7158 * TPACPI_FAN_RD_ACPI_GFAN:
7159 * ACPI GFAN method: returns fan level
7161 * see TPACPI_FAN_WR_ACPI_SFAN
7162 * EC 0x2f (HFSP) not available if GFAN exists
7164 * TPACPI_FAN_WR_ACPI_SFAN:
7165 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7167 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7168 * it for writing.
7170 * TPACPI_FAN_WR_TPEC:
7171 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7172 * Supported on almost all ThinkPads
7174 * Fan speed changes of any sort (including those caused by the
7175 * disengaged mode) are usually done slowly by the firmware as the
7176 * maximum amount of fan duty cycle change per second seems to be
7177 * limited.
7179 * Reading is not available if GFAN exists.
7180 * Writing is not available if SFAN exists.
7182 * Bits
7183 * 7 automatic mode engaged;
7184 * (default operation mode of the ThinkPad)
7185 * fan level is ignored in this mode.
7186 * 6 full speed mode (takes precedence over bit 7);
7187 * not available on all thinkpads. May disable
7188 * the tachometer while the fan controller ramps up
7189 * the speed (which can take up to a few *minutes*).
7190 * Speeds up fan to 100% duty-cycle, which is far above
7191 * the standard RPM levels. It is not impossible that
7192 * it could cause hardware damage.
7193 * 5-3 unused in some models. Extra bits for fan level
7194 * in others, but still useless as all values above
7195 * 7 map to the same speed as level 7 in these models.
7196 * 2-0 fan level (0..7 usually)
7197 * 0x00 = stop
7198 * 0x07 = max (set when temperatures critical)
7199 * Some ThinkPads may have other levels, see
7200 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7202 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7203 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7204 * does so, its initial value is meaningless (0x07).
7206 * For firmware bugs, refer to:
7207 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7209 * ----
7211 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7212 * Main fan tachometer reading (in RPM)
7214 * This register is present on all ThinkPads with a new-style EC, and
7215 * it is known not to be present on the A21m/e, and T22, as there is
7216 * something else in offset 0x84 according to the ACPI DSDT. Other
7217 * ThinkPads from this same time period (and earlier) probably lack the
7218 * tachometer as well.
7220 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7221 * was never fixed by IBM to report the EC firmware version string
7222 * probably support the tachometer (like the early X models), so
7223 * detecting it is quite hard. We need more data to know for sure.
7225 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7226 * might result.
7228 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7229 * mode.
7231 * For firmware bugs, refer to:
7232 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7234 * ----
7236 * ThinkPad EC register 0x31 bit 0 (only on select models)
7238 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7239 * show the speed of the main fan. When bit 0 of EC register 0x31
7240 * is one, the tachometer registers show the speed of the auxiliary
7241 * fan.
7243 * Fan control seems to affect both fans, regardless of the state
7244 * of this bit.
7246 * So far, only the firmware for the X60/X61 non-tablet versions
7247 * seem to support this (firmware TP-7M).
7249 * TPACPI_FAN_WR_ACPI_FANS:
7250 * ThinkPad X31, X40, X41. Not available in the X60.
7252 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7253 * high speed. ACPI DSDT seems to map these three speeds to levels
7254 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7255 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7257 * The speeds are stored on handles
7258 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7260 * There are three default speed sets, accessible as handles:
7261 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7263 * ACPI DSDT switches which set is in use depending on various
7264 * factors.
7266 * TPACPI_FAN_WR_TPEC is also available and should be used to
7267 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7268 * but the ACPI tables just mention level 7.
7271 enum { /* Fan control constants */
7272 fan_status_offset = 0x2f, /* EC register 0x2f */
7273 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7274 * 0x84 must be read before 0x85 */
7275 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7276 bit 0 selects which fan is active */
7278 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7279 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7281 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7284 enum fan_status_access_mode {
7285 TPACPI_FAN_NONE = 0, /* No fan status or control */
7286 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7287 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7290 enum fan_control_access_mode {
7291 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7292 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7293 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7294 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7297 enum fan_control_commands {
7298 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7299 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7300 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7301 * and also watchdog cmd */
7304 static int fan_control_allowed;
7306 static enum fan_status_access_mode fan_status_access_mode;
7307 static enum fan_control_access_mode fan_control_access_mode;
7308 static enum fan_control_commands fan_control_commands;
7310 static u8 fan_control_initial_status;
7311 static u8 fan_control_desired_level;
7312 static u8 fan_control_resume_level;
7313 static int fan_watchdog_maxinterval;
7315 static struct mutex fan_mutex;
7317 static void fan_watchdog_fire(struct work_struct *ignored);
7318 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7320 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7321 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7322 "\\FSPD", /* 600e/x, 770e, 770x */
7323 ); /* all others */
7324 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7325 "JFNS", /* 770x-JL */
7326 ); /* all others */
7329 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7330 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7331 * be in auto mode (0x80).
7333 * This is corrected by any write to HFSP either by the driver, or
7334 * by the firmware.
7336 * We assume 0x07 really means auto mode while this quirk is active,
7337 * as this is far more likely than the ThinkPad being in level 7,
7338 * which is only used by the firmware during thermal emergencies.
7340 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7341 * TP-70 (T43, R52), which are known to be buggy.
7344 static void fan_quirk1_setup(void)
7346 if (fan_control_initial_status == 0x07) {
7347 printk(TPACPI_NOTICE
7348 "fan_init: initial fan status is unknown, "
7349 "assuming it is in auto mode\n");
7350 tp_features.fan_ctrl_status_undef = 1;
7354 static void fan_quirk1_handle(u8 *fan_status)
7356 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7357 if (*fan_status != fan_control_initial_status) {
7358 /* something changed the HFSP regisnter since
7359 * driver init time, so it is not undefined
7360 * anymore */
7361 tp_features.fan_ctrl_status_undef = 0;
7362 } else {
7363 /* Return most likely status. In fact, it
7364 * might be the only possible status */
7365 *fan_status = TP_EC_FAN_AUTO;
7370 /* Select main fan on X60/X61, NOOP on others */
7371 static bool fan_select_fan1(void)
7373 if (tp_features.second_fan) {
7374 u8 val;
7376 if (ec_read(fan_select_offset, &val) < 0)
7377 return false;
7378 val &= 0xFEU;
7379 if (ec_write(fan_select_offset, val) < 0)
7380 return false;
7382 return true;
7385 /* Select secondary fan on X60/X61 */
7386 static bool fan_select_fan2(void)
7388 u8 val;
7390 if (!tp_features.second_fan)
7391 return false;
7393 if (ec_read(fan_select_offset, &val) < 0)
7394 return false;
7395 val |= 0x01U;
7396 if (ec_write(fan_select_offset, val) < 0)
7397 return false;
7399 return true;
7403 * Call with fan_mutex held
7405 static void fan_update_desired_level(u8 status)
7407 if ((status &
7408 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7409 if (status > 7)
7410 fan_control_desired_level = 7;
7411 else
7412 fan_control_desired_level = status;
7416 static int fan_get_status(u8 *status)
7418 u8 s;
7420 /* TODO:
7421 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7423 switch (fan_status_access_mode) {
7424 case TPACPI_FAN_RD_ACPI_GFAN:
7425 /* 570, 600e/x, 770e, 770x */
7427 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7428 return -EIO;
7430 if (likely(status))
7431 *status = s & 0x07;
7433 break;
7435 case TPACPI_FAN_RD_TPEC:
7436 /* all except 570, 600e/x, 770e, 770x */
7437 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7438 return -EIO;
7440 if (likely(status)) {
7441 *status = s;
7442 fan_quirk1_handle(status);
7445 break;
7447 default:
7448 return -ENXIO;
7451 return 0;
7454 static int fan_get_status_safe(u8 *status)
7456 int rc;
7457 u8 s;
7459 if (mutex_lock_killable(&fan_mutex))
7460 return -ERESTARTSYS;
7461 rc = fan_get_status(&s);
7462 if (!rc)
7463 fan_update_desired_level(s);
7464 mutex_unlock(&fan_mutex);
7466 if (status)
7467 *status = s;
7469 return rc;
7472 static int fan_get_speed(unsigned int *speed)
7474 u8 hi, lo;
7476 switch (fan_status_access_mode) {
7477 case TPACPI_FAN_RD_TPEC:
7478 /* all except 570, 600e/x, 770e, 770x */
7479 if (unlikely(!fan_select_fan1()))
7480 return -EIO;
7481 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7482 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7483 return -EIO;
7485 if (likely(speed))
7486 *speed = (hi << 8) | lo;
7488 break;
7490 default:
7491 return -ENXIO;
7494 return 0;
7497 static int fan2_get_speed(unsigned int *speed)
7499 u8 hi, lo;
7500 bool rc;
7502 switch (fan_status_access_mode) {
7503 case TPACPI_FAN_RD_TPEC:
7504 /* all except 570, 600e/x, 770e, 770x */
7505 if (unlikely(!fan_select_fan2()))
7506 return -EIO;
7507 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7508 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7509 fan_select_fan1(); /* play it safe */
7510 if (rc)
7511 return -EIO;
7513 if (likely(speed))
7514 *speed = (hi << 8) | lo;
7516 break;
7518 default:
7519 return -ENXIO;
7522 return 0;
7525 static int fan_set_level(int level)
7527 if (!fan_control_allowed)
7528 return -EPERM;
7530 switch (fan_control_access_mode) {
7531 case TPACPI_FAN_WR_ACPI_SFAN:
7532 if (level >= 0 && level <= 7) {
7533 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7534 return -EIO;
7535 } else
7536 return -EINVAL;
7537 break;
7539 case TPACPI_FAN_WR_ACPI_FANS:
7540 case TPACPI_FAN_WR_TPEC:
7541 if (!(level & TP_EC_FAN_AUTO) &&
7542 !(level & TP_EC_FAN_FULLSPEED) &&
7543 ((level < 0) || (level > 7)))
7544 return -EINVAL;
7546 /* safety net should the EC not support AUTO
7547 * or FULLSPEED mode bits and just ignore them */
7548 if (level & TP_EC_FAN_FULLSPEED)
7549 level |= 7; /* safety min speed 7 */
7550 else if (level & TP_EC_FAN_AUTO)
7551 level |= 4; /* safety min speed 4 */
7553 if (!acpi_ec_write(fan_status_offset, level))
7554 return -EIO;
7555 else
7556 tp_features.fan_ctrl_status_undef = 0;
7557 break;
7559 default:
7560 return -ENXIO;
7563 vdbg_printk(TPACPI_DBG_FAN,
7564 "fan control: set fan control register to 0x%02x\n", level);
7565 return 0;
7568 static int fan_set_level_safe(int level)
7570 int rc;
7572 if (!fan_control_allowed)
7573 return -EPERM;
7575 if (mutex_lock_killable(&fan_mutex))
7576 return -ERESTARTSYS;
7578 if (level == TPACPI_FAN_LAST_LEVEL)
7579 level = fan_control_desired_level;
7581 rc = fan_set_level(level);
7582 if (!rc)
7583 fan_update_desired_level(level);
7585 mutex_unlock(&fan_mutex);
7586 return rc;
7589 static int fan_set_enable(void)
7591 u8 s;
7592 int rc;
7594 if (!fan_control_allowed)
7595 return -EPERM;
7597 if (mutex_lock_killable(&fan_mutex))
7598 return -ERESTARTSYS;
7600 switch (fan_control_access_mode) {
7601 case TPACPI_FAN_WR_ACPI_FANS:
7602 case TPACPI_FAN_WR_TPEC:
7603 rc = fan_get_status(&s);
7604 if (rc < 0)
7605 break;
7607 /* Don't go out of emergency fan mode */
7608 if (s != 7) {
7609 s &= 0x07;
7610 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7613 if (!acpi_ec_write(fan_status_offset, s))
7614 rc = -EIO;
7615 else {
7616 tp_features.fan_ctrl_status_undef = 0;
7617 rc = 0;
7619 break;
7621 case TPACPI_FAN_WR_ACPI_SFAN:
7622 rc = fan_get_status(&s);
7623 if (rc < 0)
7624 break;
7626 s &= 0x07;
7628 /* Set fan to at least level 4 */
7629 s |= 4;
7631 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7632 rc = -EIO;
7633 else
7634 rc = 0;
7635 break;
7637 default:
7638 rc = -ENXIO;
7641 mutex_unlock(&fan_mutex);
7643 if (!rc)
7644 vdbg_printk(TPACPI_DBG_FAN,
7645 "fan control: set fan control register to 0x%02x\n",
7647 return rc;
7650 static int fan_set_disable(void)
7652 int rc;
7654 if (!fan_control_allowed)
7655 return -EPERM;
7657 if (mutex_lock_killable(&fan_mutex))
7658 return -ERESTARTSYS;
7660 rc = 0;
7661 switch (fan_control_access_mode) {
7662 case TPACPI_FAN_WR_ACPI_FANS:
7663 case TPACPI_FAN_WR_TPEC:
7664 if (!acpi_ec_write(fan_status_offset, 0x00))
7665 rc = -EIO;
7666 else {
7667 fan_control_desired_level = 0;
7668 tp_features.fan_ctrl_status_undef = 0;
7670 break;
7672 case TPACPI_FAN_WR_ACPI_SFAN:
7673 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7674 rc = -EIO;
7675 else
7676 fan_control_desired_level = 0;
7677 break;
7679 default:
7680 rc = -ENXIO;
7683 if (!rc)
7684 vdbg_printk(TPACPI_DBG_FAN,
7685 "fan control: set fan control register to 0\n");
7687 mutex_unlock(&fan_mutex);
7688 return rc;
7691 static int fan_set_speed(int speed)
7693 int rc;
7695 if (!fan_control_allowed)
7696 return -EPERM;
7698 if (mutex_lock_killable(&fan_mutex))
7699 return -ERESTARTSYS;
7701 rc = 0;
7702 switch (fan_control_access_mode) {
7703 case TPACPI_FAN_WR_ACPI_FANS:
7704 if (speed >= 0 && speed <= 65535) {
7705 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7706 speed, speed, speed))
7707 rc = -EIO;
7708 } else
7709 rc = -EINVAL;
7710 break;
7712 default:
7713 rc = -ENXIO;
7716 mutex_unlock(&fan_mutex);
7717 return rc;
7720 static void fan_watchdog_reset(void)
7722 static int fan_watchdog_active;
7724 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7725 return;
7727 if (fan_watchdog_active)
7728 cancel_delayed_work(&fan_watchdog_task);
7730 if (fan_watchdog_maxinterval > 0 &&
7731 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7732 fan_watchdog_active = 1;
7733 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7734 msecs_to_jiffies(fan_watchdog_maxinterval
7735 * 1000))) {
7736 printk(TPACPI_ERR
7737 "failed to queue the fan watchdog, "
7738 "watchdog will not trigger\n");
7740 } else
7741 fan_watchdog_active = 0;
7744 static void fan_watchdog_fire(struct work_struct *ignored)
7746 int rc;
7748 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7749 return;
7751 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7752 rc = fan_set_enable();
7753 if (rc < 0) {
7754 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7755 "will try again later...\n", -rc);
7756 /* reschedule for later */
7757 fan_watchdog_reset();
7762 * SYSFS fan layout: hwmon compatible (device)
7764 * pwm*_enable:
7765 * 0: "disengaged" mode
7766 * 1: manual mode
7767 * 2: native EC "auto" mode (recommended, hardware default)
7769 * pwm*: set speed in manual mode, ignored otherwise.
7770 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7771 * interpolation.
7773 * fan*_input: tachometer reading, RPM
7776 * SYSFS fan layout: extensions
7778 * fan_watchdog (driver):
7779 * fan watchdog interval in seconds, 0 disables (default), max 120
7782 /* sysfs fan pwm1_enable ----------------------------------------------- */
7783 static ssize_t fan_pwm1_enable_show(struct device *dev,
7784 struct device_attribute *attr,
7785 char *buf)
7787 int res, mode;
7788 u8 status;
7790 res = fan_get_status_safe(&status);
7791 if (res)
7792 return res;
7794 if (status & TP_EC_FAN_FULLSPEED) {
7795 mode = 0;
7796 } else if (status & TP_EC_FAN_AUTO) {
7797 mode = 2;
7798 } else
7799 mode = 1;
7801 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7804 static ssize_t fan_pwm1_enable_store(struct device *dev,
7805 struct device_attribute *attr,
7806 const char *buf, size_t count)
7808 unsigned long t;
7809 int res, level;
7811 if (parse_strtoul(buf, 2, &t))
7812 return -EINVAL;
7814 tpacpi_disclose_usertask("hwmon pwm1_enable",
7815 "set fan mode to %lu\n", t);
7817 switch (t) {
7818 case 0:
7819 level = TP_EC_FAN_FULLSPEED;
7820 break;
7821 case 1:
7822 level = TPACPI_FAN_LAST_LEVEL;
7823 break;
7824 case 2:
7825 level = TP_EC_FAN_AUTO;
7826 break;
7827 case 3:
7828 /* reserved for software-controlled auto mode */
7829 return -ENOSYS;
7830 default:
7831 return -EINVAL;
7834 res = fan_set_level_safe(level);
7835 if (res == -ENXIO)
7836 return -EINVAL;
7837 else if (res < 0)
7838 return res;
7840 fan_watchdog_reset();
7842 return count;
7845 static struct device_attribute dev_attr_fan_pwm1_enable =
7846 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7847 fan_pwm1_enable_show, fan_pwm1_enable_store);
7849 /* sysfs fan pwm1 ------------------------------------------------------ */
7850 static ssize_t fan_pwm1_show(struct device *dev,
7851 struct device_attribute *attr,
7852 char *buf)
7854 int res;
7855 u8 status;
7857 res = fan_get_status_safe(&status);
7858 if (res)
7859 return res;
7861 if ((status &
7862 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7863 status = fan_control_desired_level;
7865 if (status > 7)
7866 status = 7;
7868 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7871 static ssize_t fan_pwm1_store(struct device *dev,
7872 struct device_attribute *attr,
7873 const char *buf, size_t count)
7875 unsigned long s;
7876 int rc;
7877 u8 status, newlevel;
7879 if (parse_strtoul(buf, 255, &s))
7880 return -EINVAL;
7882 tpacpi_disclose_usertask("hwmon pwm1",
7883 "set fan speed to %lu\n", s);
7885 /* scale down from 0-255 to 0-7 */
7886 newlevel = (s >> 5) & 0x07;
7888 if (mutex_lock_killable(&fan_mutex))
7889 return -ERESTARTSYS;
7891 rc = fan_get_status(&status);
7892 if (!rc && (status &
7893 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7894 rc = fan_set_level(newlevel);
7895 if (rc == -ENXIO)
7896 rc = -EINVAL;
7897 else if (!rc) {
7898 fan_update_desired_level(newlevel);
7899 fan_watchdog_reset();
7903 mutex_unlock(&fan_mutex);
7904 return (rc)? rc : count;
7907 static struct device_attribute dev_attr_fan_pwm1 =
7908 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7909 fan_pwm1_show, fan_pwm1_store);
7911 /* sysfs fan fan1_input ------------------------------------------------ */
7912 static ssize_t fan_fan1_input_show(struct device *dev,
7913 struct device_attribute *attr,
7914 char *buf)
7916 int res;
7917 unsigned int speed;
7919 res = fan_get_speed(&speed);
7920 if (res < 0)
7921 return res;
7923 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7926 static struct device_attribute dev_attr_fan_fan1_input =
7927 __ATTR(fan1_input, S_IRUGO,
7928 fan_fan1_input_show, NULL);
7930 /* sysfs fan fan2_input ------------------------------------------------ */
7931 static ssize_t fan_fan2_input_show(struct device *dev,
7932 struct device_attribute *attr,
7933 char *buf)
7935 int res;
7936 unsigned int speed;
7938 res = fan2_get_speed(&speed);
7939 if (res < 0)
7940 return res;
7942 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7945 static struct device_attribute dev_attr_fan_fan2_input =
7946 __ATTR(fan2_input, S_IRUGO,
7947 fan_fan2_input_show, NULL);
7949 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7950 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7951 char *buf)
7953 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7956 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7957 const char *buf, size_t count)
7959 unsigned long t;
7961 if (parse_strtoul(buf, 120, &t))
7962 return -EINVAL;
7964 if (!fan_control_allowed)
7965 return -EPERM;
7967 fan_watchdog_maxinterval = t;
7968 fan_watchdog_reset();
7970 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7972 return count;
7975 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7976 fan_fan_watchdog_show, fan_fan_watchdog_store);
7978 /* --------------------------------------------------------------------- */
7979 static struct attribute *fan_attributes[] = {
7980 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7981 &dev_attr_fan_fan1_input.attr,
7982 NULL, /* for fan2_input */
7983 NULL
7986 static const struct attribute_group fan_attr_group = {
7987 .attrs = fan_attributes,
7990 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7991 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7993 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7994 { .vendor = PCI_VENDOR_ID_IBM, \
7995 .bios = TPACPI_MATCH_ANY, \
7996 .ec = TPID(__id1, __id2), \
7997 .quirks = __quirks }
7999 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8000 { .vendor = PCI_VENDOR_ID_LENOVO, \
8001 .bios = TPACPI_MATCH_ANY, \
8002 .ec = TPID(__id1, __id2), \
8003 .quirks = __quirks }
8005 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8006 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8007 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8008 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8009 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8010 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8013 #undef TPACPI_FAN_QL
8014 #undef TPACPI_FAN_QI
8016 static int __init fan_init(struct ibm_init_struct *iibm)
8018 int rc;
8019 unsigned long quirks;
8021 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8022 "initializing fan subdriver\n");
8024 mutex_init(&fan_mutex);
8025 fan_status_access_mode = TPACPI_FAN_NONE;
8026 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8027 fan_control_commands = 0;
8028 fan_watchdog_maxinterval = 0;
8029 tp_features.fan_ctrl_status_undef = 0;
8030 tp_features.second_fan = 0;
8031 fan_control_desired_level = 7;
8033 if (tpacpi_is_ibm()) {
8034 TPACPI_ACPIHANDLE_INIT(fans);
8035 TPACPI_ACPIHANDLE_INIT(gfan);
8036 TPACPI_ACPIHANDLE_INIT(sfan);
8039 quirks = tpacpi_check_quirks(fan_quirk_table,
8040 ARRAY_SIZE(fan_quirk_table));
8042 if (gfan_handle) {
8043 /* 570, 600e/x, 770e, 770x */
8044 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8045 } else {
8046 /* all other ThinkPads: note that even old-style
8047 * ThinkPad ECs supports the fan control register */
8048 if (likely(acpi_ec_read(fan_status_offset,
8049 &fan_control_initial_status))) {
8050 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8051 if (quirks & TPACPI_FAN_Q1)
8052 fan_quirk1_setup();
8053 if (quirks & TPACPI_FAN_2FAN) {
8054 tp_features.second_fan = 1;
8055 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8056 "secondary fan support enabled\n");
8058 } else {
8059 printk(TPACPI_ERR
8060 "ThinkPad ACPI EC access misbehaving, "
8061 "fan status and control unavailable\n");
8062 return 1;
8066 if (sfan_handle) {
8067 /* 570, 770x-JL */
8068 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8069 fan_control_commands |=
8070 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8071 } else {
8072 if (!gfan_handle) {
8073 /* gfan without sfan means no fan control */
8074 /* all other models implement TP EC 0x2f control */
8076 if (fans_handle) {
8077 /* X31, X40, X41 */
8078 fan_control_access_mode =
8079 TPACPI_FAN_WR_ACPI_FANS;
8080 fan_control_commands |=
8081 TPACPI_FAN_CMD_SPEED |
8082 TPACPI_FAN_CMD_LEVEL |
8083 TPACPI_FAN_CMD_ENABLE;
8084 } else {
8085 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8086 fan_control_commands |=
8087 TPACPI_FAN_CMD_LEVEL |
8088 TPACPI_FAN_CMD_ENABLE;
8093 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8094 "fan is %s, modes %d, %d\n",
8095 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8096 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8097 fan_status_access_mode, fan_control_access_mode);
8099 /* fan control master switch */
8100 if (!fan_control_allowed) {
8101 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8102 fan_control_commands = 0;
8103 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8104 "fan control features disabled by parameter\n");
8107 /* update fan_control_desired_level */
8108 if (fan_status_access_mode != TPACPI_FAN_NONE)
8109 fan_get_status_safe(NULL);
8111 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8112 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8113 if (tp_features.second_fan) {
8114 /* attach second fan tachometer */
8115 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8116 &dev_attr_fan_fan2_input.attr;
8118 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8119 &fan_attr_group);
8120 if (rc < 0)
8121 return rc;
8123 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8124 &driver_attr_fan_watchdog);
8125 if (rc < 0) {
8126 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8127 &fan_attr_group);
8128 return rc;
8130 return 0;
8131 } else
8132 return 1;
8135 static void fan_exit(void)
8137 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8138 "cancelling any pending fan watchdog tasks\n");
8140 /* FIXME: can we really do this unconditionally? */
8141 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8142 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8143 &driver_attr_fan_watchdog);
8145 cancel_delayed_work(&fan_watchdog_task);
8146 flush_workqueue(tpacpi_wq);
8149 static void fan_suspend(pm_message_t state)
8151 int rc;
8153 if (!fan_control_allowed)
8154 return;
8156 /* Store fan status in cache */
8157 fan_control_resume_level = 0;
8158 rc = fan_get_status_safe(&fan_control_resume_level);
8159 if (rc < 0)
8160 printk(TPACPI_NOTICE
8161 "failed to read fan level for later "
8162 "restore during resume: %d\n", rc);
8164 /* if it is undefined, don't attempt to restore it.
8165 * KEEP THIS LAST */
8166 if (tp_features.fan_ctrl_status_undef)
8167 fan_control_resume_level = 0;
8170 static void fan_resume(void)
8172 u8 current_level = 7;
8173 bool do_set = false;
8174 int rc;
8176 /* DSDT *always* updates status on resume */
8177 tp_features.fan_ctrl_status_undef = 0;
8179 if (!fan_control_allowed ||
8180 !fan_control_resume_level ||
8181 (fan_get_status_safe(&current_level) < 0))
8182 return;
8184 switch (fan_control_access_mode) {
8185 case TPACPI_FAN_WR_ACPI_SFAN:
8186 /* never decrease fan level */
8187 do_set = (fan_control_resume_level > current_level);
8188 break;
8189 case TPACPI_FAN_WR_ACPI_FANS:
8190 case TPACPI_FAN_WR_TPEC:
8191 /* never decrease fan level, scale is:
8192 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8194 * We expect the firmware to set either 7 or AUTO, but we
8195 * handle FULLSPEED out of paranoia.
8197 * So, we can safely only restore FULLSPEED or 7, anything
8198 * else could slow the fan. Restoring AUTO is useless, at
8199 * best that's exactly what the DSDT already set (it is the
8200 * slower it uses).
8202 * Always keep in mind that the DSDT *will* have set the
8203 * fans to what the vendor supposes is the best level. We
8204 * muck with it only to speed the fan up.
8206 if (fan_control_resume_level != 7 &&
8207 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8208 return;
8209 else
8210 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8211 (current_level != fan_control_resume_level);
8212 break;
8213 default:
8214 return;
8216 if (do_set) {
8217 printk(TPACPI_NOTICE
8218 "restoring fan level to 0x%02x\n",
8219 fan_control_resume_level);
8220 rc = fan_set_level_safe(fan_control_resume_level);
8221 if (rc < 0)
8222 printk(TPACPI_NOTICE
8223 "failed to restore fan level: %d\n", rc);
8227 static int fan_read(struct seq_file *m)
8229 int rc;
8230 u8 status;
8231 unsigned int speed = 0;
8233 switch (fan_status_access_mode) {
8234 case TPACPI_FAN_RD_ACPI_GFAN:
8235 /* 570, 600e/x, 770e, 770x */
8236 rc = fan_get_status_safe(&status);
8237 if (rc < 0)
8238 return rc;
8240 seq_printf(m, "status:\t\t%s\n"
8241 "level:\t\t%d\n",
8242 (status != 0) ? "enabled" : "disabled", status);
8243 break;
8245 case TPACPI_FAN_RD_TPEC:
8246 /* all except 570, 600e/x, 770e, 770x */
8247 rc = fan_get_status_safe(&status);
8248 if (rc < 0)
8249 return rc;
8251 seq_printf(m, "status:\t\t%s\n",
8252 (status != 0) ? "enabled" : "disabled");
8254 rc = fan_get_speed(&speed);
8255 if (rc < 0)
8256 return rc;
8258 seq_printf(m, "speed:\t\t%d\n", speed);
8260 if (status & TP_EC_FAN_FULLSPEED)
8261 /* Disengaged mode takes precedence */
8262 seq_printf(m, "level:\t\tdisengaged\n");
8263 else if (status & TP_EC_FAN_AUTO)
8264 seq_printf(m, "level:\t\tauto\n");
8265 else
8266 seq_printf(m, "level:\t\t%d\n", status);
8267 break;
8269 case TPACPI_FAN_NONE:
8270 default:
8271 seq_printf(m, "status:\t\tnot supported\n");
8274 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8275 seq_printf(m, "commands:\tlevel <level>");
8277 switch (fan_control_access_mode) {
8278 case TPACPI_FAN_WR_ACPI_SFAN:
8279 seq_printf(m, " (<level> is 0-7)\n");
8280 break;
8282 default:
8283 seq_printf(m, " (<level> is 0-7, "
8284 "auto, disengaged, full-speed)\n");
8285 break;
8289 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8290 seq_printf(m, "commands:\tenable, disable\n"
8291 "commands:\twatchdog <timeout> (<timeout> "
8292 "is 0 (off), 1-120 (seconds))\n");
8294 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8295 seq_printf(m, "commands:\tspeed <speed>"
8296 " (<speed> is 0-65535)\n");
8298 return 0;
8301 static int fan_write_cmd_level(const char *cmd, int *rc)
8303 int level;
8305 if (strlencmp(cmd, "level auto") == 0)
8306 level = TP_EC_FAN_AUTO;
8307 else if ((strlencmp(cmd, "level disengaged") == 0) |
8308 (strlencmp(cmd, "level full-speed") == 0))
8309 level = TP_EC_FAN_FULLSPEED;
8310 else if (sscanf(cmd, "level %d", &level) != 1)
8311 return 0;
8313 *rc = fan_set_level_safe(level);
8314 if (*rc == -ENXIO)
8315 printk(TPACPI_ERR "level command accepted for unsupported "
8316 "access mode %d", fan_control_access_mode);
8317 else if (!*rc)
8318 tpacpi_disclose_usertask("procfs fan",
8319 "set level to %d\n", level);
8321 return 1;
8324 static int fan_write_cmd_enable(const char *cmd, int *rc)
8326 if (strlencmp(cmd, "enable") != 0)
8327 return 0;
8329 *rc = fan_set_enable();
8330 if (*rc == -ENXIO)
8331 printk(TPACPI_ERR "enable command accepted for unsupported "
8332 "access mode %d", fan_control_access_mode);
8333 else if (!*rc)
8334 tpacpi_disclose_usertask("procfs fan", "enable\n");
8336 return 1;
8339 static int fan_write_cmd_disable(const char *cmd, int *rc)
8341 if (strlencmp(cmd, "disable") != 0)
8342 return 0;
8344 *rc = fan_set_disable();
8345 if (*rc == -ENXIO)
8346 printk(TPACPI_ERR "disable command accepted for unsupported "
8347 "access mode %d", fan_control_access_mode);
8348 else if (!*rc)
8349 tpacpi_disclose_usertask("procfs fan", "disable\n");
8351 return 1;
8354 static int fan_write_cmd_speed(const char *cmd, int *rc)
8356 int speed;
8358 /* TODO:
8359 * Support speed <low> <medium> <high> ? */
8361 if (sscanf(cmd, "speed %d", &speed) != 1)
8362 return 0;
8364 *rc = fan_set_speed(speed);
8365 if (*rc == -ENXIO)
8366 printk(TPACPI_ERR "speed command accepted for unsupported "
8367 "access mode %d", fan_control_access_mode);
8368 else if (!*rc)
8369 tpacpi_disclose_usertask("procfs fan",
8370 "set speed to %d\n", speed);
8372 return 1;
8375 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8377 int interval;
8379 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8380 return 0;
8382 if (interval < 0 || interval > 120)
8383 *rc = -EINVAL;
8384 else {
8385 fan_watchdog_maxinterval = interval;
8386 tpacpi_disclose_usertask("procfs fan",
8387 "set watchdog timer to %d\n",
8388 interval);
8391 return 1;
8394 static int fan_write(char *buf)
8396 char *cmd;
8397 int rc = 0;
8399 while (!rc && (cmd = next_cmd(&buf))) {
8400 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8401 fan_write_cmd_level(cmd, &rc)) &&
8402 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8403 (fan_write_cmd_enable(cmd, &rc) ||
8404 fan_write_cmd_disable(cmd, &rc) ||
8405 fan_write_cmd_watchdog(cmd, &rc))) &&
8406 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8407 fan_write_cmd_speed(cmd, &rc))
8409 rc = -EINVAL;
8410 else if (!rc)
8411 fan_watchdog_reset();
8414 return rc;
8417 static struct ibm_struct fan_driver_data = {
8418 .name = "fan",
8419 .read = fan_read,
8420 .write = fan_write,
8421 .exit = fan_exit,
8422 .suspend = fan_suspend,
8423 .resume = fan_resume,
8426 /****************************************************************************
8427 ****************************************************************************
8429 * Infrastructure
8431 ****************************************************************************
8432 ****************************************************************************/
8435 * HKEY event callout for other subdrivers go here
8436 * (yes, it is ugly, but it is quick, safe, and gets the job done
8438 static void tpacpi_driver_event(const unsigned int hkey_event)
8440 if (ibm_backlight_device) {
8441 switch (hkey_event) {
8442 case TP_HKEY_EV_BRGHT_UP:
8443 case TP_HKEY_EV_BRGHT_DOWN:
8444 tpacpi_brightness_notify_change();
8447 if (alsa_card) {
8448 switch (hkey_event) {
8449 case TP_HKEY_EV_VOL_UP:
8450 case TP_HKEY_EV_VOL_DOWN:
8451 case TP_HKEY_EV_VOL_MUTE:
8452 volume_alsa_notify_change();
8457 static void hotkey_driver_event(const unsigned int scancode)
8459 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8462 /* sysfs name ---------------------------------------------------------- */
8463 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8464 struct device_attribute *attr,
8465 char *buf)
8467 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8470 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8471 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8473 /* --------------------------------------------------------------------- */
8475 /* /proc support */
8476 static struct proc_dir_entry *proc_dir;
8479 * Module and infrastructure proble, init and exit handling
8482 static int force_load;
8484 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8485 static const char * __init str_supported(int is_supported)
8487 static char text_unsupported[] __initdata = "not supported";
8489 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8491 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8493 static void ibm_exit(struct ibm_struct *ibm)
8495 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8497 list_del_init(&ibm->all_drivers);
8499 if (ibm->flags.acpi_notify_installed) {
8500 dbg_printk(TPACPI_DBG_EXIT,
8501 "%s: acpi_remove_notify_handler\n", ibm->name);
8502 BUG_ON(!ibm->acpi);
8503 acpi_remove_notify_handler(*ibm->acpi->handle,
8504 ibm->acpi->type,
8505 dispatch_acpi_notify);
8506 ibm->flags.acpi_notify_installed = 0;
8507 ibm->flags.acpi_notify_installed = 0;
8510 if (ibm->flags.proc_created) {
8511 dbg_printk(TPACPI_DBG_EXIT,
8512 "%s: remove_proc_entry\n", ibm->name);
8513 remove_proc_entry(ibm->name, proc_dir);
8514 ibm->flags.proc_created = 0;
8517 if (ibm->flags.acpi_driver_registered) {
8518 dbg_printk(TPACPI_DBG_EXIT,
8519 "%s: acpi_bus_unregister_driver\n", ibm->name);
8520 BUG_ON(!ibm->acpi);
8521 acpi_bus_unregister_driver(ibm->acpi->driver);
8522 kfree(ibm->acpi->driver);
8523 ibm->acpi->driver = NULL;
8524 ibm->flags.acpi_driver_registered = 0;
8527 if (ibm->flags.init_called && ibm->exit) {
8528 ibm->exit();
8529 ibm->flags.init_called = 0;
8532 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8535 static int __init ibm_init(struct ibm_init_struct *iibm)
8537 int ret;
8538 struct ibm_struct *ibm = iibm->data;
8539 struct proc_dir_entry *entry;
8541 BUG_ON(ibm == NULL);
8543 INIT_LIST_HEAD(&ibm->all_drivers);
8545 if (ibm->flags.experimental && !experimental)
8546 return 0;
8548 dbg_printk(TPACPI_DBG_INIT,
8549 "probing for %s\n", ibm->name);
8551 if (iibm->init) {
8552 ret = iibm->init(iibm);
8553 if (ret > 0)
8554 return 0; /* probe failed */
8555 if (ret)
8556 return ret;
8558 ibm->flags.init_called = 1;
8561 if (ibm->acpi) {
8562 if (ibm->acpi->hid) {
8563 ret = register_tpacpi_subdriver(ibm);
8564 if (ret)
8565 goto err_out;
8568 if (ibm->acpi->notify) {
8569 ret = setup_acpi_notify(ibm);
8570 if (ret == -ENODEV) {
8571 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8572 ibm->name);
8573 ret = 0;
8574 goto err_out;
8576 if (ret < 0)
8577 goto err_out;
8581 dbg_printk(TPACPI_DBG_INIT,
8582 "%s installed\n", ibm->name);
8584 if (ibm->read) {
8585 mode_t mode = iibm->base_procfs_mode;
8587 if (!mode)
8588 mode = S_IRUGO;
8589 if (ibm->write)
8590 mode |= S_IWUSR;
8591 entry = proc_create_data(ibm->name, mode, proc_dir,
8592 &dispatch_proc_fops, ibm);
8593 if (!entry) {
8594 printk(TPACPI_ERR "unable to create proc entry %s\n",
8595 ibm->name);
8596 ret = -ENODEV;
8597 goto err_out;
8599 ibm->flags.proc_created = 1;
8602 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8604 return 0;
8606 err_out:
8607 dbg_printk(TPACPI_DBG_INIT,
8608 "%s: at error exit path with result %d\n",
8609 ibm->name, ret);
8611 ibm_exit(ibm);
8612 return (ret < 0)? ret : 0;
8615 /* Probing */
8617 static bool __pure __init tpacpi_is_fw_digit(const char c)
8619 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8622 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8623 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8624 const char t)
8626 return s && strlen(s) >= 8 &&
8627 tpacpi_is_fw_digit(s[0]) &&
8628 tpacpi_is_fw_digit(s[1]) &&
8629 s[2] == t && s[3] == 'T' &&
8630 tpacpi_is_fw_digit(s[4]) &&
8631 tpacpi_is_fw_digit(s[5]) &&
8632 s[6] == 'W' && s[7] == 'W';
8635 /* returns 0 - probe ok, or < 0 - probe error.
8636 * Probe ok doesn't mean thinkpad found.
8637 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8638 static int __must_check __init get_thinkpad_model_data(
8639 struct thinkpad_id_data *tp)
8641 const struct dmi_device *dev = NULL;
8642 char ec_fw_string[18];
8643 char const *s;
8645 if (!tp)
8646 return -EINVAL;
8648 memset(tp, 0, sizeof(*tp));
8650 if (dmi_name_in_vendors("IBM"))
8651 tp->vendor = PCI_VENDOR_ID_IBM;
8652 else if (dmi_name_in_vendors("LENOVO"))
8653 tp->vendor = PCI_VENDOR_ID_LENOVO;
8654 else
8655 return 0;
8657 s = dmi_get_system_info(DMI_BIOS_VERSION);
8658 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8659 if (s && !tp->bios_version_str)
8660 return -ENOMEM;
8662 /* Really ancient ThinkPad 240X will fail this, which is fine */
8663 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8664 return 0;
8666 tp->bios_model = tp->bios_version_str[0]
8667 | (tp->bios_version_str[1] << 8);
8668 tp->bios_release = (tp->bios_version_str[4] << 8)
8669 | tp->bios_version_str[5];
8672 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8673 * X32 or newer, all Z series; Some models must have an
8674 * up-to-date BIOS or they will not be detected.
8676 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8678 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8679 if (sscanf(dev->name,
8680 "IBM ThinkPad Embedded Controller -[%17c",
8681 ec_fw_string) == 1) {
8682 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8683 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8685 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8686 if (!tp->ec_version_str)
8687 return -ENOMEM;
8689 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8690 tp->ec_model = ec_fw_string[0]
8691 | (ec_fw_string[1] << 8);
8692 tp->ec_release = (ec_fw_string[4] << 8)
8693 | ec_fw_string[5];
8694 } else {
8695 printk(TPACPI_NOTICE
8696 "ThinkPad firmware release %s "
8697 "doesn't match the known patterns\n",
8698 ec_fw_string);
8699 printk(TPACPI_NOTICE
8700 "please report this to %s\n",
8701 TPACPI_MAIL);
8703 break;
8707 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8708 if (s && !strnicmp(s, "ThinkPad", 8)) {
8709 tp->model_str = kstrdup(s, GFP_KERNEL);
8710 if (!tp->model_str)
8711 return -ENOMEM;
8714 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8715 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8716 if (s && !tp->nummodel_str)
8717 return -ENOMEM;
8719 return 0;
8722 static int __init probe_for_thinkpad(void)
8724 int is_thinkpad;
8726 if (acpi_disabled)
8727 return -ENODEV;
8729 /* It would be dangerous to run the driver in this case */
8730 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8731 return -ENODEV;
8734 * Non-ancient models have better DMI tagging, but very old models
8735 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8737 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8738 (thinkpad_id.ec_model != 0) ||
8739 tpacpi_is_fw_known();
8741 /* ec is required because many other handles are relative to it */
8742 TPACPI_ACPIHANDLE_INIT(ec);
8743 if (!ec_handle) {
8744 if (is_thinkpad)
8745 printk(TPACPI_ERR
8746 "Not yet supported ThinkPad detected!\n");
8747 return -ENODEV;
8750 if (!is_thinkpad && !force_load)
8751 return -ENODEV;
8753 return 0;
8756 static void __init thinkpad_acpi_init_banner(void)
8758 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8759 printk(TPACPI_INFO "%s\n", TPACPI_URL);
8761 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
8762 (thinkpad_id.bios_version_str) ?
8763 thinkpad_id.bios_version_str : "unknown",
8764 (thinkpad_id.ec_version_str) ?
8765 thinkpad_id.ec_version_str : "unknown");
8767 BUG_ON(!thinkpad_id.vendor);
8769 if (thinkpad_id.model_str)
8770 printk(TPACPI_INFO "%s %s, model %s\n",
8771 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8772 "IBM" : ((thinkpad_id.vendor ==
8773 PCI_VENDOR_ID_LENOVO) ?
8774 "Lenovo" : "Unknown vendor"),
8775 thinkpad_id.model_str,
8776 (thinkpad_id.nummodel_str) ?
8777 thinkpad_id.nummodel_str : "unknown");
8780 /* Module init, exit, parameters */
8782 static struct ibm_init_struct ibms_init[] __initdata = {
8784 .data = &thinkpad_acpi_driver_data,
8787 .init = hotkey_init,
8788 .data = &hotkey_driver_data,
8791 .init = bluetooth_init,
8792 .data = &bluetooth_driver_data,
8795 .init = wan_init,
8796 .data = &wan_driver_data,
8799 .init = uwb_init,
8800 .data = &uwb_driver_data,
8802 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8804 .init = video_init,
8805 .base_procfs_mode = S_IRUSR,
8806 .data = &video_driver_data,
8808 #endif
8810 .init = light_init,
8811 .data = &light_driver_data,
8814 .init = cmos_init,
8815 .data = &cmos_driver_data,
8818 .init = led_init,
8819 .data = &led_driver_data,
8822 .init = beep_init,
8823 .data = &beep_driver_data,
8826 .init = thermal_init,
8827 .data = &thermal_driver_data,
8830 .data = &ecdump_driver_data,
8833 .init = brightness_init,
8834 .data = &brightness_driver_data,
8837 .init = volume_init,
8838 .data = &volume_driver_data,
8841 .init = fan_init,
8842 .data = &fan_driver_data,
8846 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8848 unsigned int i;
8849 struct ibm_struct *ibm;
8851 if (!kp || !kp->name || !val)
8852 return -EINVAL;
8854 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8855 ibm = ibms_init[i].data;
8856 WARN_ON(ibm == NULL);
8858 if (!ibm || !ibm->name)
8859 continue;
8861 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8862 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8863 return -ENOSPC;
8864 strcpy(ibms_init[i].param, val);
8865 strcat(ibms_init[i].param, ",");
8866 return 0;
8870 return -EINVAL;
8873 module_param(experimental, int, 0444);
8874 MODULE_PARM_DESC(experimental,
8875 "Enables experimental features when non-zero");
8877 module_param_named(debug, dbg_level, uint, 0);
8878 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8880 module_param(force_load, bool, 0444);
8881 MODULE_PARM_DESC(force_load,
8882 "Attempts to load the driver even on a "
8883 "mis-identified ThinkPad when true");
8885 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8886 MODULE_PARM_DESC(fan_control,
8887 "Enables setting fan parameters features when true");
8889 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8890 MODULE_PARM_DESC(brightness_mode,
8891 "Selects brightness control strategy: "
8892 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8894 module_param(brightness_enable, uint, 0444);
8895 MODULE_PARM_DESC(brightness_enable,
8896 "Enables backlight control when 1, disables when 0");
8898 module_param(hotkey_report_mode, uint, 0444);
8899 MODULE_PARM_DESC(hotkey_report_mode,
8900 "used for backwards compatibility with userspace, "
8901 "see documentation");
8903 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8904 module_param_named(volume_mode, volume_mode, uint, 0444);
8905 MODULE_PARM_DESC(volume_mode,
8906 "Selects volume control strategy: "
8907 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8909 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8910 MODULE_PARM_DESC(volume_capabilities,
8911 "Selects the mixer capabilites: "
8912 "0=auto, 1=volume and mute, 2=mute only");
8914 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8915 MODULE_PARM_DESC(volume_control,
8916 "Enables software override for the console audio "
8917 "control when true");
8919 /* ALSA module API parameters */
8920 module_param_named(index, alsa_index, int, 0444);
8921 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8922 module_param_named(id, alsa_id, charp, 0444);
8923 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8924 module_param_named(enable, alsa_enable, bool, 0444);
8925 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8926 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8928 #define TPACPI_PARAM(feature) \
8929 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8930 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8931 "at module load, see documentation")
8933 TPACPI_PARAM(hotkey);
8934 TPACPI_PARAM(bluetooth);
8935 TPACPI_PARAM(video);
8936 TPACPI_PARAM(light);
8937 TPACPI_PARAM(cmos);
8938 TPACPI_PARAM(led);
8939 TPACPI_PARAM(beep);
8940 TPACPI_PARAM(ecdump);
8941 TPACPI_PARAM(brightness);
8942 TPACPI_PARAM(volume);
8943 TPACPI_PARAM(fan);
8945 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8946 module_param(dbg_wlswemul, uint, 0444);
8947 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8948 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8949 MODULE_PARM_DESC(wlsw_state,
8950 "Initial state of the emulated WLSW switch");
8952 module_param(dbg_bluetoothemul, uint, 0444);
8953 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8954 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8955 MODULE_PARM_DESC(bluetooth_state,
8956 "Initial state of the emulated bluetooth switch");
8958 module_param(dbg_wwanemul, uint, 0444);
8959 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8960 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8961 MODULE_PARM_DESC(wwan_state,
8962 "Initial state of the emulated WWAN switch");
8964 module_param(dbg_uwbemul, uint, 0444);
8965 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8966 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8967 MODULE_PARM_DESC(uwb_state,
8968 "Initial state of the emulated UWB switch");
8969 #endif
8971 static void thinkpad_acpi_module_exit(void)
8973 struct ibm_struct *ibm, *itmp;
8975 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8977 list_for_each_entry_safe_reverse(ibm, itmp,
8978 &tpacpi_all_drivers,
8979 all_drivers) {
8980 ibm_exit(ibm);
8983 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8985 if (tpacpi_inputdev) {
8986 if (tp_features.input_device_registered)
8987 input_unregister_device(tpacpi_inputdev);
8988 else
8989 input_free_device(tpacpi_inputdev);
8992 if (tpacpi_hwmon)
8993 hwmon_device_unregister(tpacpi_hwmon);
8995 if (tp_features.sensors_pdev_attrs_registered)
8996 device_remove_file(&tpacpi_sensors_pdev->dev,
8997 &dev_attr_thinkpad_acpi_pdev_name);
8998 if (tpacpi_sensors_pdev)
8999 platform_device_unregister(tpacpi_sensors_pdev);
9000 if (tpacpi_pdev)
9001 platform_device_unregister(tpacpi_pdev);
9003 if (tp_features.sensors_pdrv_attrs_registered)
9004 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9005 if (tp_features.platform_drv_attrs_registered)
9006 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9008 if (tp_features.sensors_pdrv_registered)
9009 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9011 if (tp_features.platform_drv_registered)
9012 platform_driver_unregister(&tpacpi_pdriver);
9014 if (proc_dir)
9015 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9017 if (tpacpi_wq)
9018 destroy_workqueue(tpacpi_wq);
9020 kfree(thinkpad_id.bios_version_str);
9021 kfree(thinkpad_id.ec_version_str);
9022 kfree(thinkpad_id.model_str);
9026 static int __init thinkpad_acpi_module_init(void)
9028 int ret, i;
9030 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9032 /* Parameter checking */
9033 if (hotkey_report_mode > 2)
9034 return -EINVAL;
9036 /* Driver-level probe */
9038 ret = get_thinkpad_model_data(&thinkpad_id);
9039 if (ret) {
9040 printk(TPACPI_ERR
9041 "unable to get DMI data: %d\n", ret);
9042 thinkpad_acpi_module_exit();
9043 return ret;
9045 ret = probe_for_thinkpad();
9046 if (ret) {
9047 thinkpad_acpi_module_exit();
9048 return ret;
9051 /* Driver initialization */
9053 thinkpad_acpi_init_banner();
9054 tpacpi_check_outdated_fw();
9056 TPACPI_ACPIHANDLE_INIT(ecrd);
9057 TPACPI_ACPIHANDLE_INIT(ecwr);
9059 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9060 if (!tpacpi_wq) {
9061 thinkpad_acpi_module_exit();
9062 return -ENOMEM;
9065 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9066 if (!proc_dir) {
9067 printk(TPACPI_ERR
9068 "unable to create proc dir " TPACPI_PROC_DIR);
9069 thinkpad_acpi_module_exit();
9070 return -ENODEV;
9073 ret = platform_driver_register(&tpacpi_pdriver);
9074 if (ret) {
9075 printk(TPACPI_ERR
9076 "unable to register main platform driver\n");
9077 thinkpad_acpi_module_exit();
9078 return ret;
9080 tp_features.platform_drv_registered = 1;
9082 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9083 if (ret) {
9084 printk(TPACPI_ERR
9085 "unable to register hwmon platform driver\n");
9086 thinkpad_acpi_module_exit();
9087 return ret;
9089 tp_features.sensors_pdrv_registered = 1;
9091 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9092 if (!ret) {
9093 tp_features.platform_drv_attrs_registered = 1;
9094 ret = tpacpi_create_driver_attributes(
9095 &tpacpi_hwmon_pdriver.driver);
9097 if (ret) {
9098 printk(TPACPI_ERR
9099 "unable to create sysfs driver attributes\n");
9100 thinkpad_acpi_module_exit();
9101 return ret;
9103 tp_features.sensors_pdrv_attrs_registered = 1;
9106 /* Device initialization */
9107 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9108 NULL, 0);
9109 if (IS_ERR(tpacpi_pdev)) {
9110 ret = PTR_ERR(tpacpi_pdev);
9111 tpacpi_pdev = NULL;
9112 printk(TPACPI_ERR "unable to register platform device\n");
9113 thinkpad_acpi_module_exit();
9114 return ret;
9116 tpacpi_sensors_pdev = platform_device_register_simple(
9117 TPACPI_HWMON_DRVR_NAME,
9118 -1, NULL, 0);
9119 if (IS_ERR(tpacpi_sensors_pdev)) {
9120 ret = PTR_ERR(tpacpi_sensors_pdev);
9121 tpacpi_sensors_pdev = NULL;
9122 printk(TPACPI_ERR
9123 "unable to register hwmon platform device\n");
9124 thinkpad_acpi_module_exit();
9125 return ret;
9127 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9128 &dev_attr_thinkpad_acpi_pdev_name);
9129 if (ret) {
9130 printk(TPACPI_ERR
9131 "unable to create sysfs hwmon device attributes\n");
9132 thinkpad_acpi_module_exit();
9133 return ret;
9135 tp_features.sensors_pdev_attrs_registered = 1;
9136 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9137 if (IS_ERR(tpacpi_hwmon)) {
9138 ret = PTR_ERR(tpacpi_hwmon);
9139 tpacpi_hwmon = NULL;
9140 printk(TPACPI_ERR "unable to register hwmon device\n");
9141 thinkpad_acpi_module_exit();
9142 return ret;
9144 mutex_init(&tpacpi_inputdev_send_mutex);
9145 tpacpi_inputdev = input_allocate_device();
9146 if (!tpacpi_inputdev) {
9147 printk(TPACPI_ERR "unable to allocate input device\n");
9148 thinkpad_acpi_module_exit();
9149 return -ENOMEM;
9150 } else {
9151 /* Prepare input device, but don't register */
9152 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9153 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9154 tpacpi_inputdev->id.bustype = BUS_HOST;
9155 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9156 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9157 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9158 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9161 /* Init subdriver dependencies */
9162 tpacpi_detect_brightness_capabilities();
9164 /* Init subdrivers */
9165 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9166 ret = ibm_init(&ibms_init[i]);
9167 if (ret >= 0 && *ibms_init[i].param)
9168 ret = ibms_init[i].data->write(ibms_init[i].param);
9169 if (ret < 0) {
9170 thinkpad_acpi_module_exit();
9171 return ret;
9175 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9177 ret = input_register_device(tpacpi_inputdev);
9178 if (ret < 0) {
9179 printk(TPACPI_ERR "unable to register input device\n");
9180 thinkpad_acpi_module_exit();
9181 return ret;
9182 } else {
9183 tp_features.input_device_registered = 1;
9186 return 0;
9189 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9192 * This will autoload the driver in almost every ThinkPad
9193 * in widespread use.
9195 * Only _VERY_ old models, like the 240, 240x and 570 lack
9196 * the HKEY event interface.
9198 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9201 * DMI matching for module autoloading
9203 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9204 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9206 * Only models listed in thinkwiki will be supported, so add yours
9207 * if it is not there yet.
9209 #define IBM_BIOS_MODULE_ALIAS(__type) \
9210 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9212 /* Ancient thinkpad BIOSes have to be identified by
9213 * BIOS type or model number, and there are far less
9214 * BIOS types than model numbers... */
9215 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9217 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9218 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9219 MODULE_DESCRIPTION(TPACPI_DESC);
9220 MODULE_VERSION(TPACPI_VERSION);
9221 MODULE_LICENSE("GPL");
9223 module_init(thinkpad_acpi_module_init);
9224 module_exit(thinkpad_acpi_module_exit);