thinkpad-acpi: fix brightness hotkey poll handling
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobe5330639f58d8b9bc1bf9caa062e98f4515c0a09
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_HKEY_HID "IBM0068"
132 /* Input IDs */
133 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
134 #define TPACPI_HKEY_INPUT_VERSION 0x4101
136 /* ACPI \WGSV commands */
137 enum {
138 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
139 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
140 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
141 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
144 /* TP_ACPI_WGSV_GET_STATE bits */
145 enum {
146 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
147 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
148 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
149 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
150 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
151 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
152 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
153 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
154 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
155 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
158 /* HKEY events */
159 enum tpacpi_hkey_event_t {
160 /* Hotkey-related */
161 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
162 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
163 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
164 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
165 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
166 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
168 /* Reasons for waking up from S3/S4 */
169 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
170 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
171 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
172 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
173 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
174 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
176 /* Auto-sleep after eject request */
177 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
178 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
180 /* Misc bay events */
181 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
183 /* User-interface events */
184 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
185 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
186 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
187 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
188 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
189 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
190 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
192 /* Thermal events */
193 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
194 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
195 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
196 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
197 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
199 /* Misc */
200 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
203 /****************************************************************************
204 * Main driver
207 #define TPACPI_NAME "thinkpad"
208 #define TPACPI_DESC "ThinkPad ACPI Extras"
209 #define TPACPI_FILE TPACPI_NAME "_acpi"
210 #define TPACPI_URL "http://ibm-acpi.sf.net/"
211 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
213 #define TPACPI_PROC_DIR "ibm"
214 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
215 #define TPACPI_DRVR_NAME TPACPI_FILE
216 #define TPACPI_DRVR_SHORTNAME "tpacpi"
217 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
219 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
220 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
222 #define TPACPI_MAX_ACPI_ARGS 3
224 /* printk headers */
225 #define TPACPI_LOG TPACPI_FILE ": "
226 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
227 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
228 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
229 #define TPACPI_ERR KERN_ERR TPACPI_LOG
230 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
231 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
232 #define TPACPI_INFO KERN_INFO TPACPI_LOG
233 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
235 /* Debugging printk groups */
236 #define TPACPI_DBG_ALL 0xffff
237 #define TPACPI_DBG_DISCLOSETASK 0x8000
238 #define TPACPI_DBG_INIT 0x0001
239 #define TPACPI_DBG_EXIT 0x0002
240 #define TPACPI_DBG_RFKILL 0x0004
241 #define TPACPI_DBG_HKEY 0x0008
242 #define TPACPI_DBG_FAN 0x0010
243 #define TPACPI_DBG_BRGHT 0x0020
244 #define TPACPI_DBG_MIXER 0x0040
246 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
247 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
248 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
251 /****************************************************************************
252 * Driver-wide structs and misc. variables
255 struct ibm_struct;
257 struct tp_acpi_drv_struct {
258 const struct acpi_device_id *hid;
259 struct acpi_driver *driver;
261 void (*notify) (struct ibm_struct *, u32);
262 acpi_handle *handle;
263 u32 type;
264 struct acpi_device *device;
267 struct ibm_struct {
268 char *name;
270 int (*read) (struct seq_file *);
271 int (*write) (char *);
272 void (*exit) (void);
273 void (*resume) (void);
274 void (*suspend) (pm_message_t state);
275 void (*shutdown) (void);
277 struct list_head all_drivers;
279 struct tp_acpi_drv_struct *acpi;
281 struct {
282 u8 acpi_driver_registered:1;
283 u8 acpi_notify_installed:1;
284 u8 proc_created:1;
285 u8 init_called:1;
286 u8 experimental:1;
287 } flags;
290 struct ibm_init_struct {
291 char param[32];
293 int (*init) (struct ibm_init_struct *);
294 mode_t base_procfs_mode;
295 struct ibm_struct *data;
298 static struct {
299 u32 bluetooth:1;
300 u32 hotkey:1;
301 u32 hotkey_mask:1;
302 u32 hotkey_wlsw:1;
303 u32 hotkey_tablet:1;
304 u32 light:1;
305 u32 light_status:1;
306 u32 bright_acpimode:1;
307 u32 bright_unkfw:1;
308 u32 wan:1;
309 u32 uwb:1;
310 u32 fan_ctrl_status_undef:1;
311 u32 second_fan:1;
312 u32 beep_needs_two_args:1;
313 u32 mixer_no_level_control:1;
314 u32 input_device_registered:1;
315 u32 platform_drv_registered:1;
316 u32 platform_drv_attrs_registered:1;
317 u32 sensors_pdrv_registered:1;
318 u32 sensors_pdrv_attrs_registered:1;
319 u32 sensors_pdev_attrs_registered:1;
320 u32 hotkey_poll_active:1;
321 } tp_features;
323 static struct {
324 u16 hotkey_mask_ff:1;
325 u16 volume_ctrl_forbidden:1;
326 } tp_warned;
328 struct thinkpad_id_data {
329 unsigned int vendor; /* ThinkPad vendor:
330 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
332 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
333 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
335 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
336 u16 ec_model;
337 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
338 u16 ec_release;
340 char *model_str; /* ThinkPad T43 */
341 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
343 static struct thinkpad_id_data thinkpad_id;
345 static enum {
346 TPACPI_LIFE_INIT = 0,
347 TPACPI_LIFE_RUNNING,
348 TPACPI_LIFE_EXITING,
349 } tpacpi_lifecycle;
351 static int experimental;
352 static u32 dbg_level;
354 static struct workqueue_struct *tpacpi_wq;
356 enum led_status_t {
357 TPACPI_LED_OFF = 0,
358 TPACPI_LED_ON,
359 TPACPI_LED_BLINK,
362 /* Special LED class that can defer work */
363 struct tpacpi_led_classdev {
364 struct led_classdev led_classdev;
365 struct work_struct work;
366 enum led_status_t new_state;
367 unsigned int led;
370 /* brightness level capabilities */
371 static unsigned int bright_maxlvl; /* 0 = unknown */
373 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
374 static int dbg_wlswemul;
375 static int tpacpi_wlsw_emulstate;
376 static int dbg_bluetoothemul;
377 static int tpacpi_bluetooth_emulstate;
378 static int dbg_wwanemul;
379 static int tpacpi_wwan_emulstate;
380 static int dbg_uwbemul;
381 static int tpacpi_uwb_emulstate;
382 #endif
385 /*************************************************************************
386 * Debugging helpers
389 #define dbg_printk(a_dbg_level, format, arg...) \
390 do { if (dbg_level & (a_dbg_level)) \
391 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
392 } while (0)
394 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
395 #define vdbg_printk dbg_printk
396 static const char *str_supported(int is_supported);
397 #else
398 #define vdbg_printk(a_dbg_level, format, arg...) \
399 do { } while (0)
400 #endif
402 static void tpacpi_log_usertask(const char * const what)
404 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
405 what, task_tgid_vnr(current));
408 #define tpacpi_disclose_usertask(what, format, arg...) \
409 do { \
410 if (unlikely( \
411 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
412 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
413 printk(TPACPI_DEBUG "%s: PID %d: " format, \
414 what, task_tgid_vnr(current), ## arg); \
416 } while (0)
419 * Quirk handling helpers
421 * ThinkPad IDs and versions seen in the field so far
422 * are two-characters from the set [0-9A-Z], i.e. base 36.
424 * We use values well outside that range as specials.
427 #define TPACPI_MATCH_ANY 0xffffU
428 #define TPACPI_MATCH_UNKNOWN 0U
430 /* TPID('1', 'Y') == 0x5931 */
431 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
433 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
434 { .vendor = PCI_VENDOR_ID_IBM, \
435 .bios = TPID(__id1, __id2), \
436 .ec = TPACPI_MATCH_ANY, \
437 .quirks = (__quirk) }
439 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
440 { .vendor = PCI_VENDOR_ID_LENOVO, \
441 .bios = TPID(__id1, __id2), \
442 .ec = TPACPI_MATCH_ANY, \
443 .quirks = (__quirk) }
445 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
446 { .vendor = PCI_VENDOR_ID_LENOVO, \
447 .bios = TPACPI_MATCH_ANY, \
448 .ec = TPID(__id1, __id2), \
449 .quirks = (__quirk) }
451 struct tpacpi_quirk {
452 unsigned int vendor;
453 u16 bios;
454 u16 ec;
455 unsigned long quirks;
459 * tpacpi_check_quirks() - search BIOS/EC version on a list
460 * @qlist: array of &struct tpacpi_quirk
461 * @qlist_size: number of elements in @qlist
463 * Iterates over a quirks list until one is found that matches the
464 * ThinkPad's vendor, BIOS and EC model.
466 * Returns 0 if nothing matches, otherwise returns the quirks field of
467 * the matching &struct tpacpi_quirk entry.
469 * The match criteria is: vendor, ec and bios much match.
471 static unsigned long __init tpacpi_check_quirks(
472 const struct tpacpi_quirk *qlist,
473 unsigned int qlist_size)
475 while (qlist_size) {
476 if ((qlist->vendor == thinkpad_id.vendor ||
477 qlist->vendor == TPACPI_MATCH_ANY) &&
478 (qlist->bios == thinkpad_id.bios_model ||
479 qlist->bios == TPACPI_MATCH_ANY) &&
480 (qlist->ec == thinkpad_id.ec_model ||
481 qlist->ec == TPACPI_MATCH_ANY))
482 return qlist->quirks;
484 qlist_size--;
485 qlist++;
487 return 0;
490 static inline bool __pure __init tpacpi_is_lenovo(void)
492 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
495 static inline bool __pure __init tpacpi_is_ibm(void)
497 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
500 /****************************************************************************
501 ****************************************************************************
503 * ACPI Helpers and device model
505 ****************************************************************************
506 ****************************************************************************/
508 /*************************************************************************
509 * ACPI basic handles
512 static acpi_handle root_handle;
514 #define TPACPI_HANDLE(object, parent, paths...) \
515 static acpi_handle object##_handle; \
516 static acpi_handle *object##_parent = &parent##_handle; \
517 static char *object##_path; \
518 static char *object##_paths[] = { paths }
520 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
521 "\\_SB.PCI.ISA.EC", /* 570 */
522 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
523 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
524 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
525 "\\_SB.PCI0.ICH3.EC0", /* R31 */
526 "\\_SB.PCI0.LPC0.EC", /* X100e and a few others */
527 "\\_SB.PCI0.LPC.EC", /* all others */
530 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
531 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
533 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
534 /* T4x, X31, X40 */
535 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
536 "\\CMS", /* R40, R40e */
537 ); /* all others */
539 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
540 "^HKEY", /* R30, R31 */
541 "HKEY", /* all others */
542 ); /* 570 */
544 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
545 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
546 "\\_SB.PCI0.VID0", /* 770e */
547 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
548 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
549 "\\_SB.PCI0.AGP.VID", /* all others */
550 ); /* R30, R31 */
553 /*************************************************************************
554 * ACPI helpers
557 static int acpi_evalf(acpi_handle handle,
558 void *res, char *method, char *fmt, ...)
560 char *fmt0 = fmt;
561 struct acpi_object_list params;
562 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
563 struct acpi_buffer result, *resultp;
564 union acpi_object out_obj;
565 acpi_status status;
566 va_list ap;
567 char res_type;
568 int success;
569 int quiet;
571 if (!*fmt) {
572 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
573 return 0;
576 if (*fmt == 'q') {
577 quiet = 1;
578 fmt++;
579 } else
580 quiet = 0;
582 res_type = *(fmt++);
584 params.count = 0;
585 params.pointer = &in_objs[0];
587 va_start(ap, fmt);
588 while (*fmt) {
589 char c = *(fmt++);
590 switch (c) {
591 case 'd': /* int */
592 in_objs[params.count].integer.value = va_arg(ap, int);
593 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
594 break;
595 /* add more types as needed */
596 default:
597 printk(TPACPI_ERR "acpi_evalf() called "
598 "with invalid format character '%c'\n", c);
599 return 0;
602 va_end(ap);
604 if (res_type != 'v') {
605 result.length = sizeof(out_obj);
606 result.pointer = &out_obj;
607 resultp = &result;
608 } else
609 resultp = NULL;
611 status = acpi_evaluate_object(handle, method, &params, resultp);
613 switch (res_type) {
614 case 'd': /* int */
615 if (res)
616 *(int *)res = out_obj.integer.value;
617 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
618 break;
619 case 'v': /* void */
620 success = status == AE_OK;
621 break;
622 /* add more types as needed */
623 default:
624 printk(TPACPI_ERR "acpi_evalf() called "
625 "with invalid format character '%c'\n", res_type);
626 return 0;
629 if (!success && !quiet)
630 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
631 method, fmt0, status);
633 return success;
636 static int acpi_ec_read(int i, u8 *p)
638 int v;
640 if (ecrd_handle) {
641 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
642 return 0;
643 *p = v;
644 } else {
645 if (ec_read(i, p) < 0)
646 return 0;
649 return 1;
652 static int acpi_ec_write(int i, u8 v)
654 if (ecwr_handle) {
655 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
656 return 0;
657 } else {
658 if (ec_write(i, v) < 0)
659 return 0;
662 return 1;
665 static int issue_thinkpad_cmos_command(int cmos_cmd)
667 if (!cmos_handle)
668 return -ENXIO;
670 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
671 return -EIO;
673 return 0;
676 /*************************************************************************
677 * ACPI device model
680 #define TPACPI_ACPIHANDLE_INIT(object) \
681 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
682 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
684 static void drv_acpi_handle_init(char *name,
685 acpi_handle *handle, acpi_handle parent,
686 char **paths, int num_paths, char **path)
688 int i;
689 acpi_status status;
691 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
692 name);
694 for (i = 0; i < num_paths; i++) {
695 status = acpi_get_handle(parent, paths[i], handle);
696 if (ACPI_SUCCESS(status)) {
697 *path = paths[i];
698 dbg_printk(TPACPI_DBG_INIT,
699 "Found ACPI handle %s for %s\n",
700 *path, name);
701 return;
705 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
706 name);
707 *handle = NULL;
710 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
712 struct ibm_struct *ibm = data;
714 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
715 return;
717 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
718 return;
720 ibm->acpi->notify(ibm, event);
723 static int __init setup_acpi_notify(struct ibm_struct *ibm)
725 acpi_status status;
726 int rc;
728 BUG_ON(!ibm->acpi);
730 if (!*ibm->acpi->handle)
731 return 0;
733 vdbg_printk(TPACPI_DBG_INIT,
734 "setting up ACPI notify for %s\n", ibm->name);
736 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
737 if (rc < 0) {
738 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
739 ibm->name, rc);
740 return -ENODEV;
743 ibm->acpi->device->driver_data = ibm;
744 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
745 TPACPI_ACPI_EVENT_PREFIX,
746 ibm->name);
748 status = acpi_install_notify_handler(*ibm->acpi->handle,
749 ibm->acpi->type, dispatch_acpi_notify, ibm);
750 if (ACPI_FAILURE(status)) {
751 if (status == AE_ALREADY_EXISTS) {
752 printk(TPACPI_NOTICE
753 "another device driver is already "
754 "handling %s events\n", ibm->name);
755 } else {
756 printk(TPACPI_ERR
757 "acpi_install_notify_handler(%s) failed: %d\n",
758 ibm->name, status);
760 return -ENODEV;
762 ibm->flags.acpi_notify_installed = 1;
763 return 0;
766 static int __init tpacpi_device_add(struct acpi_device *device)
768 return 0;
771 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
773 int rc;
775 dbg_printk(TPACPI_DBG_INIT,
776 "registering %s as an ACPI driver\n", ibm->name);
778 BUG_ON(!ibm->acpi);
780 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
781 if (!ibm->acpi->driver) {
782 printk(TPACPI_ERR
783 "failed to allocate memory for ibm->acpi->driver\n");
784 return -ENOMEM;
787 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
788 ibm->acpi->driver->ids = ibm->acpi->hid;
790 ibm->acpi->driver->ops.add = &tpacpi_device_add;
792 rc = acpi_bus_register_driver(ibm->acpi->driver);
793 if (rc < 0) {
794 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
795 ibm->name, rc);
796 kfree(ibm->acpi->driver);
797 ibm->acpi->driver = NULL;
798 } else if (!rc)
799 ibm->flags.acpi_driver_registered = 1;
801 return rc;
805 /****************************************************************************
806 ****************************************************************************
808 * Procfs Helpers
810 ****************************************************************************
811 ****************************************************************************/
813 static int dispatch_proc_show(struct seq_file *m, void *v)
815 struct ibm_struct *ibm = m->private;
817 if (!ibm || !ibm->read)
818 return -EINVAL;
819 return ibm->read(m);
822 static int dispatch_proc_open(struct inode *inode, struct file *file)
824 return single_open(file, dispatch_proc_show, PDE(inode)->data);
827 static ssize_t dispatch_proc_write(struct file *file,
828 const char __user *userbuf,
829 size_t count, loff_t *pos)
831 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
832 char *kernbuf;
833 int ret;
835 if (!ibm || !ibm->write)
836 return -EINVAL;
837 if (count > PAGE_SIZE - 2)
838 return -EINVAL;
840 kernbuf = kmalloc(count + 2, GFP_KERNEL);
841 if (!kernbuf)
842 return -ENOMEM;
844 if (copy_from_user(kernbuf, userbuf, count)) {
845 kfree(kernbuf);
846 return -EFAULT;
849 kernbuf[count] = 0;
850 strcat(kernbuf, ",");
851 ret = ibm->write(kernbuf);
852 if (ret == 0)
853 ret = count;
855 kfree(kernbuf);
857 return ret;
860 static const struct file_operations dispatch_proc_fops = {
861 .owner = THIS_MODULE,
862 .open = dispatch_proc_open,
863 .read = seq_read,
864 .llseek = seq_lseek,
865 .release = single_release,
866 .write = dispatch_proc_write,
869 static char *next_cmd(char **cmds)
871 char *start = *cmds;
872 char *end;
874 while ((end = strchr(start, ',')) && end == start)
875 start = end + 1;
877 if (!end)
878 return NULL;
880 *end = 0;
881 *cmds = end + 1;
882 return start;
886 /****************************************************************************
887 ****************************************************************************
889 * Device model: input, hwmon and platform
891 ****************************************************************************
892 ****************************************************************************/
894 static struct platform_device *tpacpi_pdev;
895 static struct platform_device *tpacpi_sensors_pdev;
896 static struct device *tpacpi_hwmon;
897 static struct input_dev *tpacpi_inputdev;
898 static struct mutex tpacpi_inputdev_send_mutex;
899 static LIST_HEAD(tpacpi_all_drivers);
901 static int tpacpi_suspend_handler(struct platform_device *pdev,
902 pm_message_t state)
904 struct ibm_struct *ibm, *itmp;
906 list_for_each_entry_safe(ibm, itmp,
907 &tpacpi_all_drivers,
908 all_drivers) {
909 if (ibm->suspend)
910 (ibm->suspend)(state);
913 return 0;
916 static int tpacpi_resume_handler(struct platform_device *pdev)
918 struct ibm_struct *ibm, *itmp;
920 list_for_each_entry_safe(ibm, itmp,
921 &tpacpi_all_drivers,
922 all_drivers) {
923 if (ibm->resume)
924 (ibm->resume)();
927 return 0;
930 static void tpacpi_shutdown_handler(struct platform_device *pdev)
932 struct ibm_struct *ibm, *itmp;
934 list_for_each_entry_safe(ibm, itmp,
935 &tpacpi_all_drivers,
936 all_drivers) {
937 if (ibm->shutdown)
938 (ibm->shutdown)();
942 static struct platform_driver tpacpi_pdriver = {
943 .driver = {
944 .name = TPACPI_DRVR_NAME,
945 .owner = THIS_MODULE,
947 .suspend = tpacpi_suspend_handler,
948 .resume = tpacpi_resume_handler,
949 .shutdown = tpacpi_shutdown_handler,
952 static struct platform_driver tpacpi_hwmon_pdriver = {
953 .driver = {
954 .name = TPACPI_HWMON_DRVR_NAME,
955 .owner = THIS_MODULE,
959 /*************************************************************************
960 * sysfs support helpers
963 struct attribute_set {
964 unsigned int members, max_members;
965 struct attribute_group group;
968 struct attribute_set_obj {
969 struct attribute_set s;
970 struct attribute *a;
971 } __attribute__((packed));
973 static struct attribute_set *create_attr_set(unsigned int max_members,
974 const char *name)
976 struct attribute_set_obj *sobj;
978 if (max_members == 0)
979 return NULL;
981 /* Allocates space for implicit NULL at the end too */
982 sobj = kzalloc(sizeof(struct attribute_set_obj) +
983 max_members * sizeof(struct attribute *),
984 GFP_KERNEL);
985 if (!sobj)
986 return NULL;
987 sobj->s.max_members = max_members;
988 sobj->s.group.attrs = &sobj->a;
989 sobj->s.group.name = name;
991 return &sobj->s;
994 #define destroy_attr_set(_set) \
995 kfree(_set);
997 /* not multi-threaded safe, use it in a single thread per set */
998 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1000 if (!s || !attr)
1001 return -EINVAL;
1003 if (s->members >= s->max_members)
1004 return -ENOMEM;
1006 s->group.attrs[s->members] = attr;
1007 s->members++;
1009 return 0;
1012 static int add_many_to_attr_set(struct attribute_set *s,
1013 struct attribute **attr,
1014 unsigned int count)
1016 int i, res;
1018 for (i = 0; i < count; i++) {
1019 res = add_to_attr_set(s, attr[i]);
1020 if (res)
1021 return res;
1024 return 0;
1027 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1029 sysfs_remove_group(kobj, &s->group);
1030 destroy_attr_set(s);
1033 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1034 sysfs_create_group(_kobj, &_attr_set->group)
1036 static int parse_strtoul(const char *buf,
1037 unsigned long max, unsigned long *value)
1039 char *endp;
1041 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1042 endp = skip_spaces(endp);
1043 if (*endp || *value > max)
1044 return -EINVAL;
1046 return 0;
1049 static void tpacpi_disable_brightness_delay(void)
1051 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1052 printk(TPACPI_NOTICE
1053 "ACPI backlight control delay disabled\n");
1056 static void printk_deprecated_attribute(const char * const what,
1057 const char * const details)
1059 tpacpi_log_usertask("deprecated sysfs attribute");
1060 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1061 "will be removed. %s\n",
1062 what, details);
1065 /*************************************************************************
1066 * rfkill and radio control support helpers
1070 * ThinkPad-ACPI firmware handling model:
1072 * WLSW (master wireless switch) is event-driven, and is common to all
1073 * firmware-controlled radios. It cannot be controlled, just monitored,
1074 * as expected. It overrides all radio state in firmware
1076 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1077 * (TODO: verify how WLSW interacts with the returned radio state).
1079 * The only time there are shadow radio state changes, is when
1080 * masked-off hotkeys are used.
1084 * Internal driver API for radio state:
1086 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1087 * bool: true means radio blocked (off)
1089 enum tpacpi_rfkill_state {
1090 TPACPI_RFK_RADIO_OFF = 0,
1091 TPACPI_RFK_RADIO_ON
1094 /* rfkill switches */
1095 enum tpacpi_rfk_id {
1096 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1097 TPACPI_RFK_WWAN_SW_ID,
1098 TPACPI_RFK_UWB_SW_ID,
1099 TPACPI_RFK_SW_MAX
1102 static const char *tpacpi_rfkill_names[] = {
1103 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1104 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1105 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1106 [TPACPI_RFK_SW_MAX] = NULL
1109 /* ThinkPad-ACPI rfkill subdriver */
1110 struct tpacpi_rfk {
1111 struct rfkill *rfkill;
1112 enum tpacpi_rfk_id id;
1113 const struct tpacpi_rfk_ops *ops;
1116 struct tpacpi_rfk_ops {
1117 /* firmware interface */
1118 int (*get_status)(void);
1119 int (*set_status)(const enum tpacpi_rfkill_state);
1122 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1124 /* Query FW and update rfkill sw state for a given rfkill switch */
1125 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1127 int status;
1129 if (!tp_rfk)
1130 return -ENODEV;
1132 status = (tp_rfk->ops->get_status)();
1133 if (status < 0)
1134 return status;
1136 rfkill_set_sw_state(tp_rfk->rfkill,
1137 (status == TPACPI_RFK_RADIO_OFF));
1139 return status;
1142 /* Query FW and update rfkill sw state for all rfkill switches */
1143 static void tpacpi_rfk_update_swstate_all(void)
1145 unsigned int i;
1147 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1148 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1152 * Sync the HW-blocking state of all rfkill switches,
1153 * do notice it causes the rfkill core to schedule uevents
1155 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1157 unsigned int i;
1158 struct tpacpi_rfk *tp_rfk;
1160 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1161 tp_rfk = tpacpi_rfkill_switches[i];
1162 if (tp_rfk) {
1163 if (rfkill_set_hw_state(tp_rfk->rfkill,
1164 blocked)) {
1165 /* ignore -- we track sw block */
1171 /* Call to get the WLSW state from the firmware */
1172 static int hotkey_get_wlsw(void);
1174 /* Call to query WLSW state and update all rfkill switches */
1175 static bool tpacpi_rfk_check_hwblock_state(void)
1177 int res = hotkey_get_wlsw();
1178 int hw_blocked;
1180 /* When unknown or unsupported, we have to assume it is unblocked */
1181 if (res < 0)
1182 return false;
1184 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1185 tpacpi_rfk_update_hwblock_state(hw_blocked);
1187 return hw_blocked;
1190 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1192 struct tpacpi_rfk *tp_rfk = data;
1193 int res;
1195 dbg_printk(TPACPI_DBG_RFKILL,
1196 "request to change radio state to %s\n",
1197 blocked ? "blocked" : "unblocked");
1199 /* try to set radio state */
1200 res = (tp_rfk->ops->set_status)(blocked ?
1201 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1203 /* and update the rfkill core with whatever the FW really did */
1204 tpacpi_rfk_update_swstate(tp_rfk);
1206 return (res < 0) ? res : 0;
1209 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1210 .set_block = tpacpi_rfk_hook_set_block,
1213 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1214 const struct tpacpi_rfk_ops *tp_rfkops,
1215 const enum rfkill_type rfktype,
1216 const char *name,
1217 const bool set_default)
1219 struct tpacpi_rfk *atp_rfk;
1220 int res;
1221 bool sw_state = false;
1222 bool hw_state;
1223 int sw_status;
1225 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1227 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1228 if (atp_rfk)
1229 atp_rfk->rfkill = rfkill_alloc(name,
1230 &tpacpi_pdev->dev,
1231 rfktype,
1232 &tpacpi_rfk_rfkill_ops,
1233 atp_rfk);
1234 if (!atp_rfk || !atp_rfk->rfkill) {
1235 printk(TPACPI_ERR
1236 "failed to allocate memory for rfkill class\n");
1237 kfree(atp_rfk);
1238 return -ENOMEM;
1241 atp_rfk->id = id;
1242 atp_rfk->ops = tp_rfkops;
1244 sw_status = (tp_rfkops->get_status)();
1245 if (sw_status < 0) {
1246 printk(TPACPI_ERR
1247 "failed to read initial state for %s, error %d\n",
1248 name, sw_status);
1249 } else {
1250 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1251 if (set_default) {
1252 /* try to keep the initial state, since we ask the
1253 * firmware to preserve it across S5 in NVRAM */
1254 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1257 hw_state = tpacpi_rfk_check_hwblock_state();
1258 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1260 res = rfkill_register(atp_rfk->rfkill);
1261 if (res < 0) {
1262 printk(TPACPI_ERR
1263 "failed to register %s rfkill switch: %d\n",
1264 name, res);
1265 rfkill_destroy(atp_rfk->rfkill);
1266 kfree(atp_rfk);
1267 return res;
1270 tpacpi_rfkill_switches[id] = atp_rfk;
1272 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1273 name, (sw_state || hw_state) ? "" : "un");
1274 return 0;
1277 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1279 struct tpacpi_rfk *tp_rfk;
1281 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1283 tp_rfk = tpacpi_rfkill_switches[id];
1284 if (tp_rfk) {
1285 rfkill_unregister(tp_rfk->rfkill);
1286 rfkill_destroy(tp_rfk->rfkill);
1287 tpacpi_rfkill_switches[id] = NULL;
1288 kfree(tp_rfk);
1292 static void printk_deprecated_rfkill_attribute(const char * const what)
1294 printk_deprecated_attribute(what,
1295 "Please switch to generic rfkill before year 2010");
1298 /* sysfs <radio> enable ------------------------------------------------ */
1299 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1300 struct device_attribute *attr,
1301 char *buf)
1303 int status;
1305 printk_deprecated_rfkill_attribute(attr->attr.name);
1307 /* This is in the ABI... */
1308 if (tpacpi_rfk_check_hwblock_state()) {
1309 status = TPACPI_RFK_RADIO_OFF;
1310 } else {
1311 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1312 if (status < 0)
1313 return status;
1316 return snprintf(buf, PAGE_SIZE, "%d\n",
1317 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1320 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1321 struct device_attribute *attr,
1322 const char *buf, size_t count)
1324 unsigned long t;
1325 int res;
1327 printk_deprecated_rfkill_attribute(attr->attr.name);
1329 if (parse_strtoul(buf, 1, &t))
1330 return -EINVAL;
1332 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1334 /* This is in the ABI... */
1335 if (tpacpi_rfk_check_hwblock_state() && !!t)
1336 return -EPERM;
1338 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1339 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1340 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1342 return (res < 0) ? res : count;
1345 /* procfs -------------------------------------------------------------- */
1346 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1348 if (id >= TPACPI_RFK_SW_MAX)
1349 seq_printf(m, "status:\t\tnot supported\n");
1350 else {
1351 int status;
1353 /* This is in the ABI... */
1354 if (tpacpi_rfk_check_hwblock_state()) {
1355 status = TPACPI_RFK_RADIO_OFF;
1356 } else {
1357 status = tpacpi_rfk_update_swstate(
1358 tpacpi_rfkill_switches[id]);
1359 if (status < 0)
1360 return status;
1363 seq_printf(m, "status:\t\t%s\n",
1364 (status == TPACPI_RFK_RADIO_ON) ?
1365 "enabled" : "disabled");
1366 seq_printf(m, "commands:\tenable, disable\n");
1369 return 0;
1372 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1374 char *cmd;
1375 int status = -1;
1376 int res = 0;
1378 if (id >= TPACPI_RFK_SW_MAX)
1379 return -ENODEV;
1381 while ((cmd = next_cmd(&buf))) {
1382 if (strlencmp(cmd, "enable") == 0)
1383 status = TPACPI_RFK_RADIO_ON;
1384 else if (strlencmp(cmd, "disable") == 0)
1385 status = TPACPI_RFK_RADIO_OFF;
1386 else
1387 return -EINVAL;
1390 if (status != -1) {
1391 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1392 (status == TPACPI_RFK_RADIO_ON) ?
1393 "enable" : "disable",
1394 tpacpi_rfkill_names[id]);
1395 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1396 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1399 return res;
1402 /*************************************************************************
1403 * thinkpad-acpi driver attributes
1406 /* interface_version --------------------------------------------------- */
1407 static ssize_t tpacpi_driver_interface_version_show(
1408 struct device_driver *drv,
1409 char *buf)
1411 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1414 static DRIVER_ATTR(interface_version, S_IRUGO,
1415 tpacpi_driver_interface_version_show, NULL);
1417 /* debug_level --------------------------------------------------------- */
1418 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1419 char *buf)
1421 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1424 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1425 const char *buf, size_t count)
1427 unsigned long t;
1429 if (parse_strtoul(buf, 0xffff, &t))
1430 return -EINVAL;
1432 dbg_level = t;
1434 return count;
1437 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1438 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1440 /* version ------------------------------------------------------------- */
1441 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1442 char *buf)
1444 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1445 TPACPI_DESC, TPACPI_VERSION);
1448 static DRIVER_ATTR(version, S_IRUGO,
1449 tpacpi_driver_version_show, NULL);
1451 /* --------------------------------------------------------------------- */
1453 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1455 /* wlsw_emulstate ------------------------------------------------------ */
1456 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1457 char *buf)
1459 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1462 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1463 const char *buf, size_t count)
1465 unsigned long t;
1467 if (parse_strtoul(buf, 1, &t))
1468 return -EINVAL;
1470 if (tpacpi_wlsw_emulstate != !!t) {
1471 tpacpi_wlsw_emulstate = !!t;
1472 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1475 return count;
1478 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1479 tpacpi_driver_wlsw_emulstate_show,
1480 tpacpi_driver_wlsw_emulstate_store);
1482 /* bluetooth_emulstate ------------------------------------------------- */
1483 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1484 struct device_driver *drv,
1485 char *buf)
1487 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1490 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1491 struct device_driver *drv,
1492 const char *buf, size_t count)
1494 unsigned long t;
1496 if (parse_strtoul(buf, 1, &t))
1497 return -EINVAL;
1499 tpacpi_bluetooth_emulstate = !!t;
1501 return count;
1504 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1505 tpacpi_driver_bluetooth_emulstate_show,
1506 tpacpi_driver_bluetooth_emulstate_store);
1508 /* wwan_emulstate ------------------------------------------------- */
1509 static ssize_t tpacpi_driver_wwan_emulstate_show(
1510 struct device_driver *drv,
1511 char *buf)
1513 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1516 static ssize_t tpacpi_driver_wwan_emulstate_store(
1517 struct device_driver *drv,
1518 const char *buf, size_t count)
1520 unsigned long t;
1522 if (parse_strtoul(buf, 1, &t))
1523 return -EINVAL;
1525 tpacpi_wwan_emulstate = !!t;
1527 return count;
1530 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1531 tpacpi_driver_wwan_emulstate_show,
1532 tpacpi_driver_wwan_emulstate_store);
1534 /* uwb_emulstate ------------------------------------------------- */
1535 static ssize_t tpacpi_driver_uwb_emulstate_show(
1536 struct device_driver *drv,
1537 char *buf)
1539 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1542 static ssize_t tpacpi_driver_uwb_emulstate_store(
1543 struct device_driver *drv,
1544 const char *buf, size_t count)
1546 unsigned long t;
1548 if (parse_strtoul(buf, 1, &t))
1549 return -EINVAL;
1551 tpacpi_uwb_emulstate = !!t;
1553 return count;
1556 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1557 tpacpi_driver_uwb_emulstate_show,
1558 tpacpi_driver_uwb_emulstate_store);
1559 #endif
1561 /* --------------------------------------------------------------------- */
1563 static struct driver_attribute *tpacpi_driver_attributes[] = {
1564 &driver_attr_debug_level, &driver_attr_version,
1565 &driver_attr_interface_version,
1568 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1570 int i, res;
1572 i = 0;
1573 res = 0;
1574 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1575 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1576 i++;
1579 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1580 if (!res && dbg_wlswemul)
1581 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1582 if (!res && dbg_bluetoothemul)
1583 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1584 if (!res && dbg_wwanemul)
1585 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1586 if (!res && dbg_uwbemul)
1587 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1588 #endif
1590 return res;
1593 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1595 int i;
1597 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1598 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1600 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1601 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1602 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1603 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1604 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1605 #endif
1608 /*************************************************************************
1609 * Firmware Data
1613 * Table of recommended minimum BIOS versions
1615 * Reasons for listing:
1616 * 1. Stable BIOS, listed because the unknown amount of
1617 * bugs and bad ACPI behaviour on older versions
1619 * 2. BIOS or EC fw with known bugs that trigger on Linux
1621 * 3. BIOS with known reduced functionality in older versions
1623 * We recommend the latest BIOS and EC version.
1624 * We only support the latest BIOS and EC fw version as a rule.
1626 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1627 * Information from users in ThinkWiki
1629 * WARNING: we use this table also to detect that the machine is
1630 * a ThinkPad in some cases, so don't remove entries lightly.
1633 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1634 { .vendor = (__v), \
1635 .bios = TPID(__id1, __id2), \
1636 .ec = TPACPI_MATCH_ANY, \
1637 .quirks = TPACPI_MATCH_ANY << 16 \
1638 | (__bv1) << 8 | (__bv2) }
1640 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1641 __eid, __ev1, __ev2) \
1642 { .vendor = (__v), \
1643 .bios = TPID(__bid1, __bid2), \
1644 .ec = __eid, \
1645 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1646 | (__bv1) << 8 | (__bv2) }
1648 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1649 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1651 /* Outdated IBM BIOSes often lack the EC id string */
1652 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1653 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1654 __bv1, __bv2, TPID(__id1, __id2), \
1655 __ev1, __ev2), \
1656 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1657 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1658 __ev1, __ev2)
1660 /* Outdated IBM BIOSes often lack the EC id string */
1661 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1662 __eid1, __eid2, __ev1, __ev2) \
1663 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1664 __bv1, __bv2, TPID(__eid1, __eid2), \
1665 __ev1, __ev2), \
1666 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1667 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1668 __ev1, __ev2)
1670 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1671 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1673 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1674 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1675 __bv1, __bv2, TPID(__id1, __id2), \
1676 __ev1, __ev2)
1678 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1679 __eid1, __eid2, __ev1, __ev2) \
1680 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1681 __bv1, __bv2, TPID(__eid1, __eid2), \
1682 __ev1, __ev2)
1684 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1685 /* Numeric models ------------------ */
1686 /* FW MODEL BIOS VERS */
1687 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1688 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1689 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1690 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1691 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1692 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1693 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1694 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1695 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1697 /* A-series ------------------------- */
1698 /* FW MODEL BIOS VERS EC VERS */
1699 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1700 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1701 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1702 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1703 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1704 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1705 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1706 TPV_QI0('1', '3', '2', '0'), /* A22m */
1707 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1708 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1709 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1711 /* G-series ------------------------- */
1712 /* FW MODEL BIOS VERS */
1713 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1714 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1716 /* R-series, T-series --------------- */
1717 /* FW MODEL BIOS VERS EC VERS */
1718 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1719 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1720 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1721 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1722 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1723 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1724 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1725 T40/p, T41/p, T42/p (1) */
1726 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1727 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1728 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1729 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1731 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1732 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1733 TPV_QI0('1', '6', '3', '2'), /* T22 */
1734 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1735 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1736 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1738 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1739 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1740 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1742 /* BIOS FW BIOS VERS EC FW EC VERS */
1743 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1744 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1746 /* X-series ------------------------- */
1747 /* FW MODEL BIOS VERS EC VERS */
1748 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1749 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1750 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1751 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1752 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1753 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1754 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1756 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1757 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1759 /* (0) - older versions lack DMI EC fw string and functionality */
1760 /* (1) - older versions known to lack functionality */
1763 #undef TPV_QL1
1764 #undef TPV_QL0
1765 #undef TPV_QI2
1766 #undef TPV_QI1
1767 #undef TPV_QI0
1768 #undef TPV_Q_X
1769 #undef TPV_Q
1771 static void __init tpacpi_check_outdated_fw(void)
1773 unsigned long fwvers;
1774 u16 ec_version, bios_version;
1776 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1777 ARRAY_SIZE(tpacpi_bios_version_qtable));
1779 if (!fwvers)
1780 return;
1782 bios_version = fwvers & 0xffffU;
1783 ec_version = (fwvers >> 16) & 0xffffU;
1785 /* note that unknown versions are set to 0x0000 and we use that */
1786 if ((bios_version > thinkpad_id.bios_release) ||
1787 (ec_version > thinkpad_id.ec_release &&
1788 ec_version != TPACPI_MATCH_ANY)) {
1790 * The changelogs would let us track down the exact
1791 * reason, but it is just too much of a pain to track
1792 * it. We only list BIOSes that are either really
1793 * broken, or really stable to begin with, so it is
1794 * best if the user upgrades the firmware anyway.
1796 printk(TPACPI_WARN
1797 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1798 printk(TPACPI_WARN
1799 "WARNING: This firmware may be missing critical bug "
1800 "fixes and/or important features\n");
1804 static bool __init tpacpi_is_fw_known(void)
1806 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1807 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1810 /****************************************************************************
1811 ****************************************************************************
1813 * Subdrivers
1815 ****************************************************************************
1816 ****************************************************************************/
1818 /*************************************************************************
1819 * thinkpad-acpi metadata subdriver
1822 static int thinkpad_acpi_driver_read(struct seq_file *m)
1824 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1825 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1826 return 0;
1829 static struct ibm_struct thinkpad_acpi_driver_data = {
1830 .name = "driver",
1831 .read = thinkpad_acpi_driver_read,
1834 /*************************************************************************
1835 * Hotkey subdriver
1839 * ThinkPad firmware event model
1841 * The ThinkPad firmware has two main event interfaces: normal ACPI
1842 * notifications (which follow the ACPI standard), and a private event
1843 * interface.
1845 * The private event interface also issues events for the hotkeys. As
1846 * the driver gained features, the event handling code ended up being
1847 * built around the hotkey subdriver. This will need to be refactored
1848 * to a more formal event API eventually.
1850 * Some "hotkeys" are actually supposed to be used as event reports,
1851 * such as "brightness has changed", "volume has changed", depending on
1852 * the ThinkPad model and how the firmware is operating.
1854 * Unlike other classes, hotkey-class events have mask/unmask control on
1855 * non-ancient firmware. However, how it behaves changes a lot with the
1856 * firmware model and version.
1859 enum { /* hot key scan codes (derived from ACPI DSDT) */
1860 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1861 TP_ACPI_HOTKEYSCAN_FNF2,
1862 TP_ACPI_HOTKEYSCAN_FNF3,
1863 TP_ACPI_HOTKEYSCAN_FNF4,
1864 TP_ACPI_HOTKEYSCAN_FNF5,
1865 TP_ACPI_HOTKEYSCAN_FNF6,
1866 TP_ACPI_HOTKEYSCAN_FNF7,
1867 TP_ACPI_HOTKEYSCAN_FNF8,
1868 TP_ACPI_HOTKEYSCAN_FNF9,
1869 TP_ACPI_HOTKEYSCAN_FNF10,
1870 TP_ACPI_HOTKEYSCAN_FNF11,
1871 TP_ACPI_HOTKEYSCAN_FNF12,
1872 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1873 TP_ACPI_HOTKEYSCAN_FNINSERT,
1874 TP_ACPI_HOTKEYSCAN_FNDELETE,
1875 TP_ACPI_HOTKEYSCAN_FNHOME,
1876 TP_ACPI_HOTKEYSCAN_FNEND,
1877 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1878 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1879 TP_ACPI_HOTKEYSCAN_FNSPACE,
1880 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1881 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1882 TP_ACPI_HOTKEYSCAN_MUTE,
1883 TP_ACPI_HOTKEYSCAN_THINKPAD,
1886 enum { /* Keys/events available through NVRAM polling */
1887 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1888 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1891 enum { /* Positions of some of the keys in hotkey masks */
1892 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1893 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1894 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1895 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1896 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1897 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1898 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1899 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1900 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1901 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1902 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1905 enum { /* NVRAM to ACPI HKEY group map */
1906 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1907 TP_ACPI_HKEY_ZOOM_MASK |
1908 TP_ACPI_HKEY_DISPSWTCH_MASK |
1909 TP_ACPI_HKEY_HIBERNATE_MASK,
1910 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1911 TP_ACPI_HKEY_BRGHTDWN_MASK,
1912 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1913 TP_ACPI_HKEY_VOLDWN_MASK |
1914 TP_ACPI_HKEY_MUTE_MASK,
1917 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1918 struct tp_nvram_state {
1919 u16 thinkpad_toggle:1;
1920 u16 zoom_toggle:1;
1921 u16 display_toggle:1;
1922 u16 thinklight_toggle:1;
1923 u16 hibernate_toggle:1;
1924 u16 displayexp_toggle:1;
1925 u16 display_state:1;
1926 u16 brightness_toggle:1;
1927 u16 volume_toggle:1;
1928 u16 mute:1;
1930 u8 brightness_level;
1931 u8 volume_level;
1934 /* kthread for the hotkey poller */
1935 static struct task_struct *tpacpi_hotkey_task;
1937 /* Acquired while the poller kthread is running, use to sync start/stop */
1938 static struct mutex hotkey_thread_mutex;
1941 * Acquire mutex to write poller control variables as an
1942 * atomic block.
1944 * Increment hotkey_config_change when changing them if you
1945 * want the kthread to forget old state.
1947 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1949 static struct mutex hotkey_thread_data_mutex;
1950 static unsigned int hotkey_config_change;
1953 * hotkey poller control variables
1955 * Must be atomic or readers will also need to acquire mutex
1957 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1958 * should be used only when the changes need to be taken as
1959 * a block, OR when one needs to force the kthread to forget
1960 * old state.
1962 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1963 static unsigned int hotkey_poll_freq = 10; /* Hz */
1965 #define HOTKEY_CONFIG_CRITICAL_START \
1966 do { \
1967 mutex_lock(&hotkey_thread_data_mutex); \
1968 hotkey_config_change++; \
1969 } while (0);
1970 #define HOTKEY_CONFIG_CRITICAL_END \
1971 mutex_unlock(&hotkey_thread_data_mutex);
1973 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1975 #define hotkey_source_mask 0U
1976 #define HOTKEY_CONFIG_CRITICAL_START
1977 #define HOTKEY_CONFIG_CRITICAL_END
1979 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1981 static struct mutex hotkey_mutex;
1983 static enum { /* Reasons for waking up */
1984 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1985 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1986 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1987 } hotkey_wakeup_reason;
1989 static int hotkey_autosleep_ack;
1991 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1992 static u32 hotkey_all_mask; /* all events supported in fw */
1993 static u32 hotkey_reserved_mask; /* events better left disabled */
1994 static u32 hotkey_driver_mask; /* events needed by the driver */
1995 static u32 hotkey_user_mask; /* events visible to userspace */
1996 static u32 hotkey_acpi_mask; /* events enabled in firmware */
1998 static unsigned int hotkey_report_mode;
2000 static u16 *hotkey_keycode_map;
2002 static struct attribute_set *hotkey_dev_attributes;
2004 static void tpacpi_driver_event(const unsigned int hkey_event);
2005 static void hotkey_driver_event(const unsigned int scancode);
2006 static void hotkey_poll_setup(const bool may_warn);
2008 /* HKEY.MHKG() return bits */
2009 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2011 static int hotkey_get_wlsw(void)
2013 int status;
2015 if (!tp_features.hotkey_wlsw)
2016 return -ENODEV;
2018 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2019 if (dbg_wlswemul)
2020 return (tpacpi_wlsw_emulstate) ?
2021 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2022 #endif
2024 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2025 return -EIO;
2027 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2030 static int hotkey_get_tablet_mode(int *status)
2032 int s;
2034 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2035 return -EIO;
2037 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2038 return 0;
2042 * Reads current event mask from firmware, and updates
2043 * hotkey_acpi_mask accordingly. Also resets any bits
2044 * from hotkey_user_mask that are unavailable to be
2045 * delivered (shadow requirement of the userspace ABI).
2047 * Call with hotkey_mutex held
2049 static int hotkey_mask_get(void)
2051 if (tp_features.hotkey_mask) {
2052 u32 m = 0;
2054 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2055 return -EIO;
2057 hotkey_acpi_mask = m;
2058 } else {
2059 /* no mask support doesn't mean no event support... */
2060 hotkey_acpi_mask = hotkey_all_mask;
2063 /* sync userspace-visible mask */
2064 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2066 return 0;
2069 void static hotkey_mask_warn_incomplete_mask(void)
2071 /* log only what the user can fix... */
2072 const u32 wantedmask = hotkey_driver_mask &
2073 ~(hotkey_acpi_mask | hotkey_source_mask) &
2074 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2076 if (wantedmask)
2077 printk(TPACPI_NOTICE
2078 "required events 0x%08x not enabled!\n",
2079 wantedmask);
2083 * Set the firmware mask when supported
2085 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2087 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2089 * Call with hotkey_mutex held
2091 static int hotkey_mask_set(u32 mask)
2093 int i;
2094 int rc = 0;
2096 const u32 fwmask = mask & ~hotkey_source_mask;
2098 if (tp_features.hotkey_mask) {
2099 for (i = 0; i < 32; i++) {
2100 if (!acpi_evalf(hkey_handle,
2101 NULL, "MHKM", "vdd", i + 1,
2102 !!(mask & (1 << i)))) {
2103 rc = -EIO;
2104 break;
2110 * We *must* make an inconditional call to hotkey_mask_get to
2111 * refresh hotkey_acpi_mask and update hotkey_user_mask
2113 * Take the opportunity to also log when we cannot _enable_
2114 * a given event.
2116 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2117 printk(TPACPI_NOTICE
2118 "asked for hotkey mask 0x%08x, but "
2119 "firmware forced it to 0x%08x\n",
2120 fwmask, hotkey_acpi_mask);
2123 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2124 hotkey_mask_warn_incomplete_mask();
2126 return rc;
2130 * Sets hotkey_user_mask and tries to set the firmware mask
2132 * Call with hotkey_mutex held
2134 static int hotkey_user_mask_set(const u32 mask)
2136 int rc;
2138 /* Give people a chance to notice they are doing something that
2139 * is bound to go boom on their users sooner or later */
2140 if (!tp_warned.hotkey_mask_ff &&
2141 (mask == 0xffff || mask == 0xffffff ||
2142 mask == 0xffffffff)) {
2143 tp_warned.hotkey_mask_ff = 1;
2144 printk(TPACPI_NOTICE
2145 "setting the hotkey mask to 0x%08x is likely "
2146 "not the best way to go about it\n", mask);
2147 printk(TPACPI_NOTICE
2148 "please consider using the driver defaults, "
2149 "and refer to up-to-date thinkpad-acpi "
2150 "documentation\n");
2153 /* Try to enable what the user asked for, plus whatever we need.
2154 * this syncs everything but won't enable bits in hotkey_user_mask */
2155 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2157 /* Enable the available bits in hotkey_user_mask */
2158 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2160 return rc;
2164 * Sets the driver hotkey mask.
2166 * Can be called even if the hotkey subdriver is inactive
2168 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2170 int rc;
2172 /* Do the right thing if hotkey_init has not been called yet */
2173 if (!tp_features.hotkey) {
2174 hotkey_driver_mask = mask;
2175 return 0;
2178 mutex_lock(&hotkey_mutex);
2180 HOTKEY_CONFIG_CRITICAL_START
2181 hotkey_driver_mask = mask;
2182 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2183 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2184 #endif
2185 HOTKEY_CONFIG_CRITICAL_END
2187 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2188 ~hotkey_source_mask);
2189 hotkey_poll_setup(true);
2191 mutex_unlock(&hotkey_mutex);
2193 return rc;
2196 static int hotkey_status_get(int *status)
2198 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2199 return -EIO;
2201 return 0;
2204 static int hotkey_status_set(bool enable)
2206 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2207 return -EIO;
2209 return 0;
2212 static void tpacpi_input_send_tabletsw(void)
2214 int state;
2216 if (tp_features.hotkey_tablet &&
2217 !hotkey_get_tablet_mode(&state)) {
2218 mutex_lock(&tpacpi_inputdev_send_mutex);
2220 input_report_switch(tpacpi_inputdev,
2221 SW_TABLET_MODE, !!state);
2222 input_sync(tpacpi_inputdev);
2224 mutex_unlock(&tpacpi_inputdev_send_mutex);
2228 /* Do NOT call without validating scancode first */
2229 static void tpacpi_input_send_key(const unsigned int scancode)
2231 const unsigned int keycode = hotkey_keycode_map[scancode];
2233 if (keycode != KEY_RESERVED) {
2234 mutex_lock(&tpacpi_inputdev_send_mutex);
2236 input_report_key(tpacpi_inputdev, keycode, 1);
2237 if (keycode == KEY_UNKNOWN)
2238 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2239 scancode);
2240 input_sync(tpacpi_inputdev);
2242 input_report_key(tpacpi_inputdev, keycode, 0);
2243 if (keycode == KEY_UNKNOWN)
2244 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2245 scancode);
2246 input_sync(tpacpi_inputdev);
2248 mutex_unlock(&tpacpi_inputdev_send_mutex);
2252 /* Do NOT call without validating scancode first */
2253 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2255 hotkey_driver_event(scancode);
2256 if (hotkey_user_mask & (1 << scancode))
2257 tpacpi_input_send_key(scancode);
2260 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2261 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2263 /* Do NOT call without validating scancode first */
2264 static void tpacpi_hotkey_send_key(unsigned int scancode)
2266 tpacpi_input_send_key_masked(scancode);
2267 if (hotkey_report_mode < 2) {
2268 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2269 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2273 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2275 u8 d;
2277 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2278 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2279 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2280 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2281 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2282 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2284 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2285 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2286 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2288 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2289 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2290 n->displayexp_toggle =
2291 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2293 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2294 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2295 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2296 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2297 n->brightness_toggle =
2298 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2300 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2301 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2302 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2303 >> TP_NVRAM_POS_LEVEL_VOLUME;
2304 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2305 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2309 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2310 struct tp_nvram_state *newn,
2311 const u32 event_mask)
2314 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2315 do { \
2316 if ((event_mask & (1 << __scancode)) && \
2317 oldn->__member != newn->__member) \
2318 tpacpi_hotkey_send_key(__scancode); \
2319 } while (0)
2321 #define TPACPI_MAY_SEND_KEY(__scancode) \
2322 do { \
2323 if (event_mask & (1 << __scancode)) \
2324 tpacpi_hotkey_send_key(__scancode); \
2325 } while (0)
2327 void issue_volchange(const unsigned int oldvol,
2328 const unsigned int newvol)
2330 unsigned int i = oldvol;
2332 while (i > newvol) {
2333 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2334 i--;
2336 while (i < newvol) {
2337 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2338 i++;
2342 void issue_brightnesschange(const unsigned int oldbrt,
2343 const unsigned int newbrt)
2345 unsigned int i = oldbrt;
2347 while (i > newbrt) {
2348 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2349 i--;
2351 while (i < newbrt) {
2352 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2353 i++;
2357 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2358 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2359 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2360 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2362 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2364 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2367 * Handle volume
2369 * This code is supposed to duplicate the IBM firmware behaviour:
2370 * - Pressing MUTE issues mute hotkey message, even when already mute
2371 * - Pressing Volume up/down issues volume up/down hotkey messages,
2372 * even when already at maximum or minumum volume
2373 * - The act of unmuting issues volume up/down notification,
2374 * depending which key was used to unmute
2376 * We are constrained to what the NVRAM can tell us, which is not much
2377 * and certainly not enough if more than one volume hotkey was pressed
2378 * since the last poll cycle.
2380 * Just to make our life interesting, some newer Lenovo ThinkPads have
2381 * bugs in the BIOS and may fail to update volume_toggle properly.
2383 if (newn->mute) {
2384 /* muted */
2385 if (!oldn->mute ||
2386 oldn->volume_toggle != newn->volume_toggle ||
2387 oldn->volume_level != newn->volume_level) {
2388 /* recently muted, or repeated mute keypress, or
2389 * multiple presses ending in mute */
2390 issue_volchange(oldn->volume_level, newn->volume_level);
2391 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2393 } else {
2394 /* unmute */
2395 if (oldn->mute) {
2396 /* recently unmuted, issue 'unmute' keypress */
2397 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2399 if (oldn->volume_level != newn->volume_level) {
2400 issue_volchange(oldn->volume_level, newn->volume_level);
2401 } else if (oldn->volume_toggle != newn->volume_toggle) {
2402 /* repeated vol up/down keypress at end of scale ? */
2403 if (newn->volume_level == 0)
2404 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2405 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2406 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2410 /* handle brightness */
2411 if (oldn->brightness_level != newn->brightness_level) {
2412 issue_brightnesschange(oldn->brightness_level,
2413 newn->brightness_level);
2414 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2415 /* repeated key presses that didn't change state */
2416 if (newn->brightness_level == 0)
2417 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2418 else if (newn->brightness_level >= bright_maxlvl
2419 && !tp_features.bright_unkfw)
2420 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2423 #undef TPACPI_COMPARE_KEY
2424 #undef TPACPI_MAY_SEND_KEY
2428 * Polling driver
2430 * We track all events in hotkey_source_mask all the time, since
2431 * most of them are edge-based. We only issue those requested by
2432 * hotkey_user_mask or hotkey_driver_mask, though.
2434 static int hotkey_kthread(void *data)
2436 struct tp_nvram_state s[2];
2437 u32 poll_mask, event_mask;
2438 unsigned int si, so;
2439 unsigned long t;
2440 unsigned int change_detector, must_reset;
2441 unsigned int poll_freq;
2443 mutex_lock(&hotkey_thread_mutex);
2445 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2446 goto exit;
2448 set_freezable();
2450 so = 0;
2451 si = 1;
2452 t = 0;
2454 /* Initial state for compares */
2455 mutex_lock(&hotkey_thread_data_mutex);
2456 change_detector = hotkey_config_change;
2457 poll_mask = hotkey_source_mask;
2458 event_mask = hotkey_source_mask &
2459 (hotkey_driver_mask | hotkey_user_mask);
2460 poll_freq = hotkey_poll_freq;
2461 mutex_unlock(&hotkey_thread_data_mutex);
2462 hotkey_read_nvram(&s[so], poll_mask);
2464 while (!kthread_should_stop()) {
2465 if (t == 0) {
2466 if (likely(poll_freq))
2467 t = 1000/poll_freq;
2468 else
2469 t = 100; /* should never happen... */
2471 t = msleep_interruptible(t);
2472 if (unlikely(kthread_should_stop()))
2473 break;
2474 must_reset = try_to_freeze();
2475 if (t > 0 && !must_reset)
2476 continue;
2478 mutex_lock(&hotkey_thread_data_mutex);
2479 if (must_reset || hotkey_config_change != change_detector) {
2480 /* forget old state on thaw or config change */
2481 si = so;
2482 t = 0;
2483 change_detector = hotkey_config_change;
2485 poll_mask = hotkey_source_mask;
2486 event_mask = hotkey_source_mask &
2487 (hotkey_driver_mask | hotkey_user_mask);
2488 poll_freq = hotkey_poll_freq;
2489 mutex_unlock(&hotkey_thread_data_mutex);
2491 if (likely(poll_mask)) {
2492 hotkey_read_nvram(&s[si], poll_mask);
2493 if (likely(si != so)) {
2494 hotkey_compare_and_issue_event(&s[so], &s[si],
2495 event_mask);
2499 so = si;
2500 si ^= 1;
2503 exit:
2504 mutex_unlock(&hotkey_thread_mutex);
2505 return 0;
2508 /* call with hotkey_mutex held */
2509 static void hotkey_poll_stop_sync(void)
2511 if (tpacpi_hotkey_task) {
2512 if (frozen(tpacpi_hotkey_task) ||
2513 freezing(tpacpi_hotkey_task))
2514 thaw_process(tpacpi_hotkey_task);
2516 kthread_stop(tpacpi_hotkey_task);
2517 tpacpi_hotkey_task = NULL;
2518 mutex_lock(&hotkey_thread_mutex);
2519 /* at this point, the thread did exit */
2520 mutex_unlock(&hotkey_thread_mutex);
2524 /* call with hotkey_mutex held */
2525 static void hotkey_poll_setup(const bool may_warn)
2527 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2528 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2530 if (hotkey_poll_freq > 0 &&
2531 (poll_driver_mask ||
2532 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2533 if (!tpacpi_hotkey_task) {
2534 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2535 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2536 if (IS_ERR(tpacpi_hotkey_task)) {
2537 tpacpi_hotkey_task = NULL;
2538 printk(TPACPI_ERR
2539 "could not create kernel thread "
2540 "for hotkey polling\n");
2543 } else {
2544 hotkey_poll_stop_sync();
2545 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2546 hotkey_poll_freq == 0) {
2547 printk(TPACPI_NOTICE
2548 "hot keys 0x%08x and/or events 0x%08x "
2549 "require polling, which is currently "
2550 "disabled\n",
2551 poll_user_mask, poll_driver_mask);
2556 static void hotkey_poll_setup_safe(const bool may_warn)
2558 mutex_lock(&hotkey_mutex);
2559 hotkey_poll_setup(may_warn);
2560 mutex_unlock(&hotkey_mutex);
2563 /* call with hotkey_mutex held */
2564 static void hotkey_poll_set_freq(unsigned int freq)
2566 if (!freq)
2567 hotkey_poll_stop_sync();
2569 hotkey_poll_freq = freq;
2572 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2574 static void hotkey_poll_setup(const bool __unused)
2578 static void hotkey_poll_setup_safe(const bool __unused)
2582 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2584 static int hotkey_inputdev_open(struct input_dev *dev)
2586 switch (tpacpi_lifecycle) {
2587 case TPACPI_LIFE_INIT:
2588 case TPACPI_LIFE_RUNNING:
2589 hotkey_poll_setup_safe(false);
2590 return 0;
2591 case TPACPI_LIFE_EXITING:
2592 return -EBUSY;
2595 /* Should only happen if tpacpi_lifecycle is corrupt */
2596 BUG();
2597 return -EBUSY;
2600 static void hotkey_inputdev_close(struct input_dev *dev)
2602 /* disable hotkey polling when possible */
2603 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2604 !(hotkey_source_mask & hotkey_driver_mask))
2605 hotkey_poll_setup_safe(false);
2608 /* sysfs hotkey enable ------------------------------------------------- */
2609 static ssize_t hotkey_enable_show(struct device *dev,
2610 struct device_attribute *attr,
2611 char *buf)
2613 int res, status;
2615 printk_deprecated_attribute("hotkey_enable",
2616 "Hotkey reporting is always enabled");
2618 res = hotkey_status_get(&status);
2619 if (res)
2620 return res;
2622 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2625 static ssize_t hotkey_enable_store(struct device *dev,
2626 struct device_attribute *attr,
2627 const char *buf, size_t count)
2629 unsigned long t;
2631 printk_deprecated_attribute("hotkey_enable",
2632 "Hotkeys can be disabled through hotkey_mask");
2634 if (parse_strtoul(buf, 1, &t))
2635 return -EINVAL;
2637 if (t == 0)
2638 return -EPERM;
2640 return count;
2643 static struct device_attribute dev_attr_hotkey_enable =
2644 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2645 hotkey_enable_show, hotkey_enable_store);
2647 /* sysfs hotkey mask --------------------------------------------------- */
2648 static ssize_t hotkey_mask_show(struct device *dev,
2649 struct device_attribute *attr,
2650 char *buf)
2652 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2655 static ssize_t hotkey_mask_store(struct device *dev,
2656 struct device_attribute *attr,
2657 const char *buf, size_t count)
2659 unsigned long t;
2660 int res;
2662 if (parse_strtoul(buf, 0xffffffffUL, &t))
2663 return -EINVAL;
2665 if (mutex_lock_killable(&hotkey_mutex))
2666 return -ERESTARTSYS;
2668 res = hotkey_user_mask_set(t);
2670 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2671 hotkey_poll_setup(true);
2672 #endif
2674 mutex_unlock(&hotkey_mutex);
2676 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2678 return (res) ? res : count;
2681 static struct device_attribute dev_attr_hotkey_mask =
2682 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2683 hotkey_mask_show, hotkey_mask_store);
2685 /* sysfs hotkey bios_enabled ------------------------------------------- */
2686 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2687 struct device_attribute *attr,
2688 char *buf)
2690 return sprintf(buf, "0\n");
2693 static struct device_attribute dev_attr_hotkey_bios_enabled =
2694 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2696 /* sysfs hotkey bios_mask ---------------------------------------------- */
2697 static ssize_t hotkey_bios_mask_show(struct device *dev,
2698 struct device_attribute *attr,
2699 char *buf)
2701 printk_deprecated_attribute("hotkey_bios_mask",
2702 "This attribute is useless.");
2703 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2706 static struct device_attribute dev_attr_hotkey_bios_mask =
2707 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2709 /* sysfs hotkey all_mask ----------------------------------------------- */
2710 static ssize_t hotkey_all_mask_show(struct device *dev,
2711 struct device_attribute *attr,
2712 char *buf)
2714 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2715 hotkey_all_mask | hotkey_source_mask);
2718 static struct device_attribute dev_attr_hotkey_all_mask =
2719 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2721 /* sysfs hotkey recommended_mask --------------------------------------- */
2722 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2723 struct device_attribute *attr,
2724 char *buf)
2726 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2727 (hotkey_all_mask | hotkey_source_mask)
2728 & ~hotkey_reserved_mask);
2731 static struct device_attribute dev_attr_hotkey_recommended_mask =
2732 __ATTR(hotkey_recommended_mask, S_IRUGO,
2733 hotkey_recommended_mask_show, NULL);
2735 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2737 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2738 static ssize_t hotkey_source_mask_show(struct device *dev,
2739 struct device_attribute *attr,
2740 char *buf)
2742 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2745 static ssize_t hotkey_source_mask_store(struct device *dev,
2746 struct device_attribute *attr,
2747 const char *buf, size_t count)
2749 unsigned long t;
2750 u32 r_ev;
2751 int rc;
2753 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2754 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2755 return -EINVAL;
2757 if (mutex_lock_killable(&hotkey_mutex))
2758 return -ERESTARTSYS;
2760 HOTKEY_CONFIG_CRITICAL_START
2761 hotkey_source_mask = t;
2762 HOTKEY_CONFIG_CRITICAL_END
2764 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2765 ~hotkey_source_mask);
2766 hotkey_poll_setup(true);
2768 /* check if events needed by the driver got disabled */
2769 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2770 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2772 mutex_unlock(&hotkey_mutex);
2774 if (rc < 0)
2775 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2776 "firmware event mask!\n");
2778 if (r_ev)
2779 printk(TPACPI_NOTICE "hotkey_source_mask: "
2780 "some important events were disabled: "
2781 "0x%04x\n", r_ev);
2783 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2785 return (rc < 0) ? rc : count;
2788 static struct device_attribute dev_attr_hotkey_source_mask =
2789 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2790 hotkey_source_mask_show, hotkey_source_mask_store);
2792 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2793 static ssize_t hotkey_poll_freq_show(struct device *dev,
2794 struct device_attribute *attr,
2795 char *buf)
2797 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2800 static ssize_t hotkey_poll_freq_store(struct device *dev,
2801 struct device_attribute *attr,
2802 const char *buf, size_t count)
2804 unsigned long t;
2806 if (parse_strtoul(buf, 25, &t))
2807 return -EINVAL;
2809 if (mutex_lock_killable(&hotkey_mutex))
2810 return -ERESTARTSYS;
2812 hotkey_poll_set_freq(t);
2813 hotkey_poll_setup(true);
2815 mutex_unlock(&hotkey_mutex);
2817 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2819 return count;
2822 static struct device_attribute dev_attr_hotkey_poll_freq =
2823 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2824 hotkey_poll_freq_show, hotkey_poll_freq_store);
2826 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2828 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2829 static ssize_t hotkey_radio_sw_show(struct device *dev,
2830 struct device_attribute *attr,
2831 char *buf)
2833 int res;
2834 res = hotkey_get_wlsw();
2835 if (res < 0)
2836 return res;
2838 /* Opportunistic update */
2839 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2841 return snprintf(buf, PAGE_SIZE, "%d\n",
2842 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2845 static struct device_attribute dev_attr_hotkey_radio_sw =
2846 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2848 static void hotkey_radio_sw_notify_change(void)
2850 if (tp_features.hotkey_wlsw)
2851 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2852 "hotkey_radio_sw");
2855 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2856 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2857 struct device_attribute *attr,
2858 char *buf)
2860 int res, s;
2861 res = hotkey_get_tablet_mode(&s);
2862 if (res < 0)
2863 return res;
2865 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2868 static struct device_attribute dev_attr_hotkey_tablet_mode =
2869 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2871 static void hotkey_tablet_mode_notify_change(void)
2873 if (tp_features.hotkey_tablet)
2874 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2875 "hotkey_tablet_mode");
2878 /* sysfs hotkey report_mode -------------------------------------------- */
2879 static ssize_t hotkey_report_mode_show(struct device *dev,
2880 struct device_attribute *attr,
2881 char *buf)
2883 return snprintf(buf, PAGE_SIZE, "%d\n",
2884 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2887 static struct device_attribute dev_attr_hotkey_report_mode =
2888 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2890 /* sysfs wakeup reason (pollable) -------------------------------------- */
2891 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2892 struct device_attribute *attr,
2893 char *buf)
2895 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2898 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2899 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2901 static void hotkey_wakeup_reason_notify_change(void)
2903 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2904 "wakeup_reason");
2907 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2908 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2909 struct device_attribute *attr,
2910 char *buf)
2912 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2915 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2916 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2917 hotkey_wakeup_hotunplug_complete_show, NULL);
2919 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2921 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2922 "wakeup_hotunplug_complete");
2925 /* --------------------------------------------------------------------- */
2927 static struct attribute *hotkey_attributes[] __initdata = {
2928 &dev_attr_hotkey_enable.attr,
2929 &dev_attr_hotkey_bios_enabled.attr,
2930 &dev_attr_hotkey_bios_mask.attr,
2931 &dev_attr_hotkey_report_mode.attr,
2932 &dev_attr_hotkey_wakeup_reason.attr,
2933 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2934 &dev_attr_hotkey_mask.attr,
2935 &dev_attr_hotkey_all_mask.attr,
2936 &dev_attr_hotkey_recommended_mask.attr,
2937 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2938 &dev_attr_hotkey_source_mask.attr,
2939 &dev_attr_hotkey_poll_freq.attr,
2940 #endif
2944 * Sync both the hw and sw blocking state of all switches
2946 static void tpacpi_send_radiosw_update(void)
2948 int wlsw;
2951 * We must sync all rfkill controllers *before* issuing any
2952 * rfkill input events, or we will race the rfkill core input
2953 * handler.
2955 * tpacpi_inputdev_send_mutex works as a syncronization point
2956 * for the above.
2958 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2961 wlsw = hotkey_get_wlsw();
2963 /* Sync hw blocking state first if it is hw-blocked */
2964 if (wlsw == TPACPI_RFK_RADIO_OFF)
2965 tpacpi_rfk_update_hwblock_state(true);
2967 /* Sync sw blocking state */
2968 tpacpi_rfk_update_swstate_all();
2970 /* Sync hw blocking state last if it is hw-unblocked */
2971 if (wlsw == TPACPI_RFK_RADIO_ON)
2972 tpacpi_rfk_update_hwblock_state(false);
2974 /* Issue rfkill input event for WLSW switch */
2975 if (!(wlsw < 0)) {
2976 mutex_lock(&tpacpi_inputdev_send_mutex);
2978 input_report_switch(tpacpi_inputdev,
2979 SW_RFKILL_ALL, (wlsw > 0));
2980 input_sync(tpacpi_inputdev);
2982 mutex_unlock(&tpacpi_inputdev_send_mutex);
2986 * this can be unconditional, as we will poll state again
2987 * if userspace uses the notify to read data
2989 hotkey_radio_sw_notify_change();
2992 static void hotkey_exit(void)
2994 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2995 mutex_lock(&hotkey_mutex);
2996 hotkey_poll_stop_sync();
2997 mutex_unlock(&hotkey_mutex);
2998 #endif
3000 if (hotkey_dev_attributes)
3001 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3003 kfree(hotkey_keycode_map);
3005 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3006 "restoring original HKEY status and mask\n");
3007 /* yes, there is a bitwise or below, we want the
3008 * functions to be called even if one of them fail */
3009 if (((tp_features.hotkey_mask &&
3010 hotkey_mask_set(hotkey_orig_mask)) |
3011 hotkey_status_set(false)) != 0)
3012 printk(TPACPI_ERR
3013 "failed to restore hot key mask "
3014 "to BIOS defaults\n");
3017 static void __init hotkey_unmap(const unsigned int scancode)
3019 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3020 clear_bit(hotkey_keycode_map[scancode],
3021 tpacpi_inputdev->keybit);
3022 hotkey_keycode_map[scancode] = KEY_RESERVED;
3027 * HKEY quirks:
3028 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3031 #define TPACPI_HK_Q_INIMASK 0x0001
3033 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3034 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3035 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3036 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3037 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3038 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3039 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3040 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3041 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3042 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3043 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3044 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3045 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3046 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3047 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3048 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3049 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3050 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3051 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3052 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3055 static int __init hotkey_init(struct ibm_init_struct *iibm)
3057 /* Requirements for changing the default keymaps:
3059 * 1. Many of the keys are mapped to KEY_RESERVED for very
3060 * good reasons. Do not change them unless you have deep
3061 * knowledge on the IBM and Lenovo ThinkPad firmware for
3062 * the various ThinkPad models. The driver behaves
3063 * differently for KEY_RESERVED: such keys have their
3064 * hot key mask *unset* in mask_recommended, and also
3065 * in the initial hot key mask programmed into the
3066 * firmware at driver load time, which means the firm-
3067 * ware may react very differently if you change them to
3068 * something else;
3070 * 2. You must be subscribed to the linux-thinkpad and
3071 * ibm-acpi-devel mailing lists, and you should read the
3072 * list archives since 2007 if you want to change the
3073 * keymaps. This requirement exists so that you will
3074 * know the past history of problems with the thinkpad-
3075 * acpi driver keymaps, and also that you will be
3076 * listening to any bug reports;
3078 * 3. Do not send thinkpad-acpi specific patches directly to
3079 * for merging, *ever*. Send them to the linux-acpi
3080 * mailinglist for comments. Merging is to be done only
3081 * through acpi-test and the ACPI maintainer.
3083 * If the above is too much to ask, don't change the keymap.
3084 * Ask the thinkpad-acpi maintainer to do it, instead.
3086 static u16 ibm_keycode_map[] __initdata = {
3087 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3088 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3089 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3090 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3092 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3093 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3094 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3095 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3097 /* brightness: firmware always reacts to them */
3098 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3099 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3101 /* Thinklight: firmware always react to it */
3102 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3104 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3105 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3107 /* Volume: firmware always react to it and reprograms
3108 * the built-in *extra* mixer. Never map it to control
3109 * another mixer by default. */
3110 KEY_RESERVED, /* 0x14: VOLUME UP */
3111 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3112 KEY_RESERVED, /* 0x16: MUTE */
3114 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3116 /* (assignments unknown, please report if found) */
3117 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3118 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3120 static u16 lenovo_keycode_map[] __initdata = {
3121 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3122 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3123 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3124 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3126 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3127 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3128 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3129 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3131 /* These should be enabled --only-- when ACPI video
3132 * is disabled (i.e. in "vendor" mode), and are handled
3133 * in a special way by the init code */
3134 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3135 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3137 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3139 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3140 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3142 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3143 * react to it and reprograms the built-in *extra* mixer.
3144 * Never map it to control another mixer by default.
3146 * T60?, T61, R60?, R61: firmware and EC tries to send
3147 * these over the regular keyboard, so these are no-ops,
3148 * but there are still weird bugs re. MUTE, so do not
3149 * change unless you get test reports from all Lenovo
3150 * models. May cause the BIOS to interfere with the
3151 * HDA mixer.
3153 KEY_RESERVED, /* 0x14: VOLUME UP */
3154 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3155 KEY_RESERVED, /* 0x16: MUTE */
3157 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3159 /* (assignments unknown, please report if found) */
3160 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3161 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3164 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3165 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3166 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3168 int res, i;
3169 int status;
3170 int hkeyv;
3171 bool radiosw_state = false;
3172 bool tabletsw_state = false;
3174 unsigned long quirks;
3176 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3177 "initializing hotkey subdriver\n");
3179 BUG_ON(!tpacpi_inputdev);
3180 BUG_ON(tpacpi_inputdev->open != NULL ||
3181 tpacpi_inputdev->close != NULL);
3183 TPACPI_ACPIHANDLE_INIT(hkey);
3184 mutex_init(&hotkey_mutex);
3186 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3187 mutex_init(&hotkey_thread_mutex);
3188 mutex_init(&hotkey_thread_data_mutex);
3189 #endif
3191 /* hotkey not supported on 570 */
3192 tp_features.hotkey = hkey_handle != NULL;
3194 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3195 "hotkeys are %s\n",
3196 str_supported(tp_features.hotkey));
3198 if (!tp_features.hotkey)
3199 return 1;
3201 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3202 ARRAY_SIZE(tpacpi_hotkey_qtable));
3204 tpacpi_disable_brightness_delay();
3206 /* MUST have enough space for all attributes to be added to
3207 * hotkey_dev_attributes */
3208 hotkey_dev_attributes = create_attr_set(
3209 ARRAY_SIZE(hotkey_attributes) + 2,
3210 NULL);
3211 if (!hotkey_dev_attributes)
3212 return -ENOMEM;
3213 res = add_many_to_attr_set(hotkey_dev_attributes,
3214 hotkey_attributes,
3215 ARRAY_SIZE(hotkey_attributes));
3216 if (res)
3217 goto err_exit;
3219 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3220 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3221 for HKEY interface version 0x100 */
3222 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3223 if ((hkeyv >> 8) != 1) {
3224 printk(TPACPI_ERR "unknown version of the "
3225 "HKEY interface: 0x%x\n", hkeyv);
3226 printk(TPACPI_ERR "please report this to %s\n",
3227 TPACPI_MAIL);
3228 } else {
3230 * MHKV 0x100 in A31, R40, R40e,
3231 * T4x, X31, and later
3233 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3234 "firmware HKEY interface version: 0x%x\n",
3235 hkeyv);
3237 /* Paranoia check AND init hotkey_all_mask */
3238 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3239 "MHKA", "qd")) {
3240 printk(TPACPI_ERR
3241 "missing MHKA handler, "
3242 "please report this to %s\n",
3243 TPACPI_MAIL);
3244 /* Fallback: pre-init for FN+F3,F4,F12 */
3245 hotkey_all_mask = 0x080cU;
3246 } else {
3247 tp_features.hotkey_mask = 1;
3252 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3253 "hotkey masks are %s\n",
3254 str_supported(tp_features.hotkey_mask));
3256 /* Init hotkey_all_mask if not initialized yet */
3257 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3258 (quirks & TPACPI_HK_Q_INIMASK))
3259 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3261 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3262 if (tp_features.hotkey_mask) {
3263 /* hotkey_source_mask *must* be zero for
3264 * the first hotkey_mask_get to return hotkey_orig_mask */
3265 res = hotkey_mask_get();
3266 if (res)
3267 goto err_exit;
3269 hotkey_orig_mask = hotkey_acpi_mask;
3270 } else {
3271 hotkey_orig_mask = hotkey_all_mask;
3272 hotkey_acpi_mask = hotkey_all_mask;
3275 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3276 if (dbg_wlswemul) {
3277 tp_features.hotkey_wlsw = 1;
3278 radiosw_state = !!tpacpi_wlsw_emulstate;
3279 printk(TPACPI_INFO
3280 "radio switch emulation enabled\n");
3281 } else
3282 #endif
3283 /* Not all thinkpads have a hardware radio switch */
3284 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3285 tp_features.hotkey_wlsw = 1;
3286 radiosw_state = !!status;
3287 printk(TPACPI_INFO
3288 "radio switch found; radios are %s\n",
3289 enabled(status, 0));
3291 if (tp_features.hotkey_wlsw)
3292 res = add_to_attr_set(hotkey_dev_attributes,
3293 &dev_attr_hotkey_radio_sw.attr);
3295 /* For X41t, X60t, X61t Tablets... */
3296 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3297 tp_features.hotkey_tablet = 1;
3298 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3299 printk(TPACPI_INFO
3300 "possible tablet mode switch found; "
3301 "ThinkPad in %s mode\n",
3302 (tabletsw_state) ? "tablet" : "laptop");
3303 res = add_to_attr_set(hotkey_dev_attributes,
3304 &dev_attr_hotkey_tablet_mode.attr);
3307 if (!res)
3308 res = register_attr_set_with_sysfs(
3309 hotkey_dev_attributes,
3310 &tpacpi_pdev->dev.kobj);
3311 if (res)
3312 goto err_exit;
3314 /* Set up key map */
3316 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3317 GFP_KERNEL);
3318 if (!hotkey_keycode_map) {
3319 printk(TPACPI_ERR
3320 "failed to allocate memory for key map\n");
3321 res = -ENOMEM;
3322 goto err_exit;
3325 if (tpacpi_is_lenovo()) {
3326 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3327 "using Lenovo default hot key map\n");
3328 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3329 TPACPI_HOTKEY_MAP_SIZE);
3330 } else {
3331 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3332 "using IBM default hot key map\n");
3333 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3334 TPACPI_HOTKEY_MAP_SIZE);
3337 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3338 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3339 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3340 tpacpi_inputdev->keycode = hotkey_keycode_map;
3341 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3342 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3343 input_set_capability(tpacpi_inputdev, EV_KEY,
3344 hotkey_keycode_map[i]);
3345 } else {
3346 if (i < sizeof(hotkey_reserved_mask)*8)
3347 hotkey_reserved_mask |= 1 << i;
3351 if (tp_features.hotkey_wlsw) {
3352 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3353 input_report_switch(tpacpi_inputdev,
3354 SW_RFKILL_ALL, radiosw_state);
3356 if (tp_features.hotkey_tablet) {
3357 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3358 input_report_switch(tpacpi_inputdev,
3359 SW_TABLET_MODE, tabletsw_state);
3362 /* Do not issue duplicate brightness change events to
3363 * userspace. tpacpi_detect_brightness_capabilities() must have
3364 * been called before this point */
3365 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3366 printk(TPACPI_INFO
3367 "This ThinkPad has standard ACPI backlight "
3368 "brightness control, supported by the ACPI "
3369 "video driver\n");
3370 printk(TPACPI_NOTICE
3371 "Disabling thinkpad-acpi brightness events "
3372 "by default...\n");
3374 /* Disable brightness up/down on Lenovo thinkpads when
3375 * ACPI is handling them, otherwise it is plain impossible
3376 * for userspace to do something even remotely sane */
3377 hotkey_reserved_mask |=
3378 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3379 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3380 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3381 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3384 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3385 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3386 & ~hotkey_all_mask
3387 & ~hotkey_reserved_mask;
3389 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3390 "hotkey source mask 0x%08x, polling freq %u\n",
3391 hotkey_source_mask, hotkey_poll_freq);
3392 #endif
3394 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3395 "enabling firmware HKEY event interface...\n");
3396 res = hotkey_status_set(true);
3397 if (res) {
3398 hotkey_exit();
3399 return res;
3401 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3402 | hotkey_driver_mask)
3403 & ~hotkey_source_mask);
3404 if (res < 0 && res != -ENXIO) {
3405 hotkey_exit();
3406 return res;
3408 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3409 & ~hotkey_reserved_mask;
3410 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3411 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3412 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3414 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3415 "legacy ibm/hotkey event reporting over procfs %s\n",
3416 (hotkey_report_mode < 2) ?
3417 "enabled" : "disabled");
3419 tpacpi_inputdev->open = &hotkey_inputdev_open;
3420 tpacpi_inputdev->close = &hotkey_inputdev_close;
3422 hotkey_poll_setup_safe(true);
3424 return 0;
3426 err_exit:
3427 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3428 hotkey_dev_attributes = NULL;
3430 return (res < 0)? res : 1;
3433 static bool hotkey_notify_hotkey(const u32 hkey,
3434 bool *send_acpi_ev,
3435 bool *ignore_acpi_ev)
3437 /* 0x1000-0x1FFF: key presses */
3438 unsigned int scancode = hkey & 0xfff;
3439 *send_acpi_ev = true;
3440 *ignore_acpi_ev = false;
3442 if (scancode > 0 && scancode < 0x21) {
3443 scancode--;
3444 if (!(hotkey_source_mask & (1 << scancode))) {
3445 tpacpi_input_send_key_masked(scancode);
3446 *send_acpi_ev = false;
3447 } else {
3448 *ignore_acpi_ev = true;
3450 return true;
3452 return false;
3455 static bool hotkey_notify_wakeup(const u32 hkey,
3456 bool *send_acpi_ev,
3457 bool *ignore_acpi_ev)
3459 /* 0x2000-0x2FFF: Wakeup reason */
3460 *send_acpi_ev = true;
3461 *ignore_acpi_ev = false;
3463 switch (hkey) {
3464 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3465 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3466 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3467 *ignore_acpi_ev = true;
3468 break;
3470 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3471 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3472 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3473 *ignore_acpi_ev = true;
3474 break;
3476 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3477 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3478 printk(TPACPI_ALERT
3479 "EMERGENCY WAKEUP: battery almost empty\n");
3480 /* how to auto-heal: */
3481 /* 2313: woke up from S3, go to S4/S5 */
3482 /* 2413: woke up from S4, go to S5 */
3483 break;
3485 default:
3486 return false;
3489 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3490 printk(TPACPI_INFO
3491 "woke up due to a hot-unplug "
3492 "request...\n");
3493 hotkey_wakeup_reason_notify_change();
3495 return true;
3498 static bool hotkey_notify_usrevent(const u32 hkey,
3499 bool *send_acpi_ev,
3500 bool *ignore_acpi_ev)
3502 /* 0x5000-0x5FFF: human interface helpers */
3503 *send_acpi_ev = true;
3504 *ignore_acpi_ev = false;
3506 switch (hkey) {
3507 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3508 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3509 return true;
3511 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3512 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3513 tpacpi_input_send_tabletsw();
3514 hotkey_tablet_mode_notify_change();
3515 *send_acpi_ev = false;
3516 return true;
3518 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3519 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3520 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3521 /* do not propagate these events */
3522 *ignore_acpi_ev = true;
3523 return true;
3525 default:
3526 return false;
3530 static void thermal_dump_all_sensors(void);
3532 static bool hotkey_notify_thermal(const u32 hkey,
3533 bool *send_acpi_ev,
3534 bool *ignore_acpi_ev)
3536 bool known = true;
3538 /* 0x6000-0x6FFF: thermal alarms */
3539 *send_acpi_ev = true;
3540 *ignore_acpi_ev = false;
3542 switch (hkey) {
3543 case TP_HKEY_EV_THM_TABLE_CHANGED:
3544 printk(TPACPI_INFO
3545 "EC reports that Thermal Table has changed\n");
3546 /* recommended action: do nothing, we don't have
3547 * Lenovo ATM information */
3548 return true;
3549 case TP_HKEY_EV_ALARM_BAT_HOT:
3550 printk(TPACPI_CRIT
3551 "THERMAL ALARM: battery is too hot!\n");
3552 /* recommended action: warn user through gui */
3553 break;
3554 case TP_HKEY_EV_ALARM_BAT_XHOT:
3555 printk(TPACPI_ALERT
3556 "THERMAL EMERGENCY: battery is extremely hot!\n");
3557 /* recommended action: immediate sleep/hibernate */
3558 break;
3559 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3560 printk(TPACPI_CRIT
3561 "THERMAL ALARM: "
3562 "a sensor reports something is too hot!\n");
3563 /* recommended action: warn user through gui, that */
3564 /* some internal component is too hot */
3565 break;
3566 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3567 printk(TPACPI_ALERT
3568 "THERMAL EMERGENCY: "
3569 "a sensor reports something is extremely hot!\n");
3570 /* recommended action: immediate sleep/hibernate */
3571 break;
3572 default:
3573 printk(TPACPI_ALERT
3574 "THERMAL ALERT: unknown thermal alarm received\n");
3575 known = false;
3578 thermal_dump_all_sensors();
3580 return known;
3583 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3585 u32 hkey;
3586 bool send_acpi_ev;
3587 bool ignore_acpi_ev;
3588 bool known_ev;
3590 if (event != 0x80) {
3591 printk(TPACPI_ERR
3592 "unknown HKEY notification event %d\n", event);
3593 /* forward it to userspace, maybe it knows how to handle it */
3594 acpi_bus_generate_netlink_event(
3595 ibm->acpi->device->pnp.device_class,
3596 dev_name(&ibm->acpi->device->dev),
3597 event, 0);
3598 return;
3601 while (1) {
3602 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3603 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3604 return;
3607 if (hkey == 0) {
3608 /* queue empty */
3609 return;
3612 send_acpi_ev = true;
3613 ignore_acpi_ev = false;
3615 switch (hkey >> 12) {
3616 case 1:
3617 /* 0x1000-0x1FFF: key presses */
3618 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3619 &ignore_acpi_ev);
3620 break;
3621 case 2:
3622 /* 0x2000-0x2FFF: Wakeup reason */
3623 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3624 &ignore_acpi_ev);
3625 break;
3626 case 3:
3627 /* 0x3000-0x3FFF: bay-related wakeups */
3628 switch (hkey) {
3629 case TP_HKEY_EV_BAYEJ_ACK:
3630 hotkey_autosleep_ack = 1;
3631 printk(TPACPI_INFO
3632 "bay ejected\n");
3633 hotkey_wakeup_hotunplug_complete_notify_change();
3634 known_ev = true;
3635 break;
3636 case TP_HKEY_EV_OPTDRV_EJ:
3637 /* FIXME: kick libata if SATA link offline */
3638 known_ev = true;
3639 break;
3640 default:
3641 known_ev = false;
3643 break;
3644 case 4:
3645 /* 0x4000-0x4FFF: dock-related wakeups */
3646 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3647 hotkey_autosleep_ack = 1;
3648 printk(TPACPI_INFO
3649 "undocked\n");
3650 hotkey_wakeup_hotunplug_complete_notify_change();
3651 known_ev = true;
3652 } else {
3653 known_ev = false;
3655 break;
3656 case 5:
3657 /* 0x5000-0x5FFF: human interface helpers */
3658 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3659 &ignore_acpi_ev);
3660 break;
3661 case 6:
3662 /* 0x6000-0x6FFF: thermal alarms */
3663 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3664 &ignore_acpi_ev);
3665 break;
3666 case 7:
3667 /* 0x7000-0x7FFF: misc */
3668 if (tp_features.hotkey_wlsw &&
3669 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3670 tpacpi_send_radiosw_update();
3671 send_acpi_ev = 0;
3672 known_ev = true;
3673 break;
3675 /* fallthrough to default */
3676 default:
3677 known_ev = false;
3679 if (!known_ev) {
3680 printk(TPACPI_NOTICE
3681 "unhandled HKEY event 0x%04x\n", hkey);
3682 printk(TPACPI_NOTICE
3683 "please report the conditions when this "
3684 "event happened to %s\n", TPACPI_MAIL);
3687 /* Legacy events */
3688 if (!ignore_acpi_ev &&
3689 (send_acpi_ev || hotkey_report_mode < 2)) {
3690 acpi_bus_generate_proc_event(ibm->acpi->device,
3691 event, hkey);
3694 /* netlink events */
3695 if (!ignore_acpi_ev && send_acpi_ev) {
3696 acpi_bus_generate_netlink_event(
3697 ibm->acpi->device->pnp.device_class,
3698 dev_name(&ibm->acpi->device->dev),
3699 event, hkey);
3704 static void hotkey_suspend(pm_message_t state)
3706 /* Do these on suspend, we get the events on early resume! */
3707 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3708 hotkey_autosleep_ack = 0;
3711 static void hotkey_resume(void)
3713 tpacpi_disable_brightness_delay();
3715 if (hotkey_status_set(true) < 0 ||
3716 hotkey_mask_set(hotkey_acpi_mask) < 0)
3717 printk(TPACPI_ERR
3718 "error while attempting to reset the event "
3719 "firmware interface\n");
3721 tpacpi_send_radiosw_update();
3722 hotkey_tablet_mode_notify_change();
3723 hotkey_wakeup_reason_notify_change();
3724 hotkey_wakeup_hotunplug_complete_notify_change();
3725 hotkey_poll_setup_safe(false);
3728 /* procfs -------------------------------------------------------------- */
3729 static int hotkey_read(struct seq_file *m)
3731 int res, status;
3733 if (!tp_features.hotkey) {
3734 seq_printf(m, "status:\t\tnot supported\n");
3735 return 0;
3738 if (mutex_lock_killable(&hotkey_mutex))
3739 return -ERESTARTSYS;
3740 res = hotkey_status_get(&status);
3741 if (!res)
3742 res = hotkey_mask_get();
3743 mutex_unlock(&hotkey_mutex);
3744 if (res)
3745 return res;
3747 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3748 if (hotkey_all_mask) {
3749 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3750 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3751 } else {
3752 seq_printf(m, "mask:\t\tnot supported\n");
3753 seq_printf(m, "commands:\tenable, disable, reset\n");
3756 return 0;
3759 static void hotkey_enabledisable_warn(bool enable)
3761 tpacpi_log_usertask("procfs hotkey enable/disable");
3762 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3763 TPACPI_WARN
3764 "hotkey enable/disable functionality has been "
3765 "removed from the driver. Hotkeys are always "
3766 "enabled\n"))
3767 printk(TPACPI_ERR
3768 "Please remove the hotkey=enable module "
3769 "parameter, it is deprecated. Hotkeys are always "
3770 "enabled\n");
3773 static int hotkey_write(char *buf)
3775 int res;
3776 u32 mask;
3777 char *cmd;
3779 if (!tp_features.hotkey)
3780 return -ENODEV;
3782 if (mutex_lock_killable(&hotkey_mutex))
3783 return -ERESTARTSYS;
3785 mask = hotkey_user_mask;
3787 res = 0;
3788 while ((cmd = next_cmd(&buf))) {
3789 if (strlencmp(cmd, "enable") == 0) {
3790 hotkey_enabledisable_warn(1);
3791 } else if (strlencmp(cmd, "disable") == 0) {
3792 hotkey_enabledisable_warn(0);
3793 res = -EPERM;
3794 } else if (strlencmp(cmd, "reset") == 0) {
3795 mask = (hotkey_all_mask | hotkey_source_mask)
3796 & ~hotkey_reserved_mask;
3797 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3798 /* mask set */
3799 } else if (sscanf(cmd, "%x", &mask) == 1) {
3800 /* mask set */
3801 } else {
3802 res = -EINVAL;
3803 goto errexit;
3807 if (!res) {
3808 tpacpi_disclose_usertask("procfs hotkey",
3809 "set mask to 0x%08x\n", mask);
3810 res = hotkey_user_mask_set(mask);
3813 errexit:
3814 mutex_unlock(&hotkey_mutex);
3815 return res;
3818 static const struct acpi_device_id ibm_htk_device_ids[] = {
3819 {TPACPI_ACPI_HKEY_HID, 0},
3820 {"", 0},
3823 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3824 .hid = ibm_htk_device_ids,
3825 .notify = hotkey_notify,
3826 .handle = &hkey_handle,
3827 .type = ACPI_DEVICE_NOTIFY,
3830 static struct ibm_struct hotkey_driver_data = {
3831 .name = "hotkey",
3832 .read = hotkey_read,
3833 .write = hotkey_write,
3834 .exit = hotkey_exit,
3835 .resume = hotkey_resume,
3836 .suspend = hotkey_suspend,
3837 .acpi = &ibm_hotkey_acpidriver,
3840 /*************************************************************************
3841 * Bluetooth subdriver
3844 enum {
3845 /* ACPI GBDC/SBDC bits */
3846 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3847 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3848 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3849 0 = disable, 1 = enable */
3852 enum {
3853 /* ACPI \BLTH commands */
3854 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3855 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3856 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3857 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3858 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3861 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3863 static int bluetooth_get_status(void)
3865 int status;
3867 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3868 if (dbg_bluetoothemul)
3869 return (tpacpi_bluetooth_emulstate) ?
3870 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3871 #endif
3873 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3874 return -EIO;
3876 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3877 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3880 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3882 int status;
3884 vdbg_printk(TPACPI_DBG_RFKILL,
3885 "will attempt to %s bluetooth\n",
3886 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3888 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3889 if (dbg_bluetoothemul) {
3890 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3891 return 0;
3893 #endif
3895 if (state == TPACPI_RFK_RADIO_ON)
3896 status = TP_ACPI_BLUETOOTH_RADIOSSW
3897 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3898 else
3899 status = 0;
3901 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3902 return -EIO;
3904 return 0;
3907 /* sysfs bluetooth enable ---------------------------------------------- */
3908 static ssize_t bluetooth_enable_show(struct device *dev,
3909 struct device_attribute *attr,
3910 char *buf)
3912 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3913 attr, buf);
3916 static ssize_t bluetooth_enable_store(struct device *dev,
3917 struct device_attribute *attr,
3918 const char *buf, size_t count)
3920 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3921 attr, buf, count);
3924 static struct device_attribute dev_attr_bluetooth_enable =
3925 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3926 bluetooth_enable_show, bluetooth_enable_store);
3928 /* --------------------------------------------------------------------- */
3930 static struct attribute *bluetooth_attributes[] = {
3931 &dev_attr_bluetooth_enable.attr,
3932 NULL
3935 static const struct attribute_group bluetooth_attr_group = {
3936 .attrs = bluetooth_attributes,
3939 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3940 .get_status = bluetooth_get_status,
3941 .set_status = bluetooth_set_status,
3944 static void bluetooth_shutdown(void)
3946 /* Order firmware to save current state to NVRAM */
3947 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3948 TP_ACPI_BLTH_SAVE_STATE))
3949 printk(TPACPI_NOTICE
3950 "failed to save bluetooth state to NVRAM\n");
3951 else
3952 vdbg_printk(TPACPI_DBG_RFKILL,
3953 "bluestooth state saved to NVRAM\n");
3956 static void bluetooth_exit(void)
3958 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3959 &bluetooth_attr_group);
3961 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3963 bluetooth_shutdown();
3966 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3968 int res;
3969 int status = 0;
3971 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3972 "initializing bluetooth subdriver\n");
3974 TPACPI_ACPIHANDLE_INIT(hkey);
3976 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3977 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3978 tp_features.bluetooth = hkey_handle &&
3979 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3981 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3982 "bluetooth is %s, status 0x%02x\n",
3983 str_supported(tp_features.bluetooth),
3984 status);
3986 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3987 if (dbg_bluetoothemul) {
3988 tp_features.bluetooth = 1;
3989 printk(TPACPI_INFO
3990 "bluetooth switch emulation enabled\n");
3991 } else
3992 #endif
3993 if (tp_features.bluetooth &&
3994 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3995 /* no bluetooth hardware present in system */
3996 tp_features.bluetooth = 0;
3997 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3998 "bluetooth hardware not installed\n");
4001 if (!tp_features.bluetooth)
4002 return 1;
4004 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4005 &bluetooth_tprfk_ops,
4006 RFKILL_TYPE_BLUETOOTH,
4007 TPACPI_RFK_BLUETOOTH_SW_NAME,
4008 true);
4009 if (res)
4010 return res;
4012 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4013 &bluetooth_attr_group);
4014 if (res) {
4015 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4016 return res;
4019 return 0;
4022 /* procfs -------------------------------------------------------------- */
4023 static int bluetooth_read(struct seq_file *m)
4025 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4028 static int bluetooth_write(char *buf)
4030 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4033 static struct ibm_struct bluetooth_driver_data = {
4034 .name = "bluetooth",
4035 .read = bluetooth_read,
4036 .write = bluetooth_write,
4037 .exit = bluetooth_exit,
4038 .shutdown = bluetooth_shutdown,
4041 /*************************************************************************
4042 * Wan subdriver
4045 enum {
4046 /* ACPI GWAN/SWAN bits */
4047 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4048 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4049 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4050 0 = disable, 1 = enable */
4053 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4055 static int wan_get_status(void)
4057 int status;
4059 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4060 if (dbg_wwanemul)
4061 return (tpacpi_wwan_emulstate) ?
4062 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4063 #endif
4065 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4066 return -EIO;
4068 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4069 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4072 static int wan_set_status(enum tpacpi_rfkill_state state)
4074 int status;
4076 vdbg_printk(TPACPI_DBG_RFKILL,
4077 "will attempt to %s wwan\n",
4078 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4080 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4081 if (dbg_wwanemul) {
4082 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4083 return 0;
4085 #endif
4087 if (state == TPACPI_RFK_RADIO_ON)
4088 status = TP_ACPI_WANCARD_RADIOSSW
4089 | TP_ACPI_WANCARD_RESUMECTRL;
4090 else
4091 status = 0;
4093 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4094 return -EIO;
4096 return 0;
4099 /* sysfs wan enable ---------------------------------------------------- */
4100 static ssize_t wan_enable_show(struct device *dev,
4101 struct device_attribute *attr,
4102 char *buf)
4104 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4105 attr, buf);
4108 static ssize_t wan_enable_store(struct device *dev,
4109 struct device_attribute *attr,
4110 const char *buf, size_t count)
4112 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4113 attr, buf, count);
4116 static struct device_attribute dev_attr_wan_enable =
4117 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4118 wan_enable_show, wan_enable_store);
4120 /* --------------------------------------------------------------------- */
4122 static struct attribute *wan_attributes[] = {
4123 &dev_attr_wan_enable.attr,
4124 NULL
4127 static const struct attribute_group wan_attr_group = {
4128 .attrs = wan_attributes,
4131 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4132 .get_status = wan_get_status,
4133 .set_status = wan_set_status,
4136 static void wan_shutdown(void)
4138 /* Order firmware to save current state to NVRAM */
4139 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4140 TP_ACPI_WGSV_SAVE_STATE))
4141 printk(TPACPI_NOTICE
4142 "failed to save WWAN state to NVRAM\n");
4143 else
4144 vdbg_printk(TPACPI_DBG_RFKILL,
4145 "WWAN state saved to NVRAM\n");
4148 static void wan_exit(void)
4150 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4151 &wan_attr_group);
4153 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4155 wan_shutdown();
4158 static int __init wan_init(struct ibm_init_struct *iibm)
4160 int res;
4161 int status = 0;
4163 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4164 "initializing wan subdriver\n");
4166 TPACPI_ACPIHANDLE_INIT(hkey);
4168 tp_features.wan = hkey_handle &&
4169 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4171 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4172 "wan is %s, status 0x%02x\n",
4173 str_supported(tp_features.wan),
4174 status);
4176 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4177 if (dbg_wwanemul) {
4178 tp_features.wan = 1;
4179 printk(TPACPI_INFO
4180 "wwan switch emulation enabled\n");
4181 } else
4182 #endif
4183 if (tp_features.wan &&
4184 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4185 /* no wan hardware present in system */
4186 tp_features.wan = 0;
4187 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4188 "wan hardware not installed\n");
4191 if (!tp_features.wan)
4192 return 1;
4194 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4195 &wan_tprfk_ops,
4196 RFKILL_TYPE_WWAN,
4197 TPACPI_RFK_WWAN_SW_NAME,
4198 true);
4199 if (res)
4200 return res;
4202 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4203 &wan_attr_group);
4205 if (res) {
4206 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4207 return res;
4210 return 0;
4213 /* procfs -------------------------------------------------------------- */
4214 static int wan_read(struct seq_file *m)
4216 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4219 static int wan_write(char *buf)
4221 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4224 static struct ibm_struct wan_driver_data = {
4225 .name = "wan",
4226 .read = wan_read,
4227 .write = wan_write,
4228 .exit = wan_exit,
4229 .shutdown = wan_shutdown,
4232 /*************************************************************************
4233 * UWB subdriver
4236 enum {
4237 /* ACPI GUWB/SUWB bits */
4238 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4239 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4242 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4244 static int uwb_get_status(void)
4246 int status;
4248 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4249 if (dbg_uwbemul)
4250 return (tpacpi_uwb_emulstate) ?
4251 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4252 #endif
4254 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4255 return -EIO;
4257 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4258 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4261 static int uwb_set_status(enum tpacpi_rfkill_state state)
4263 int status;
4265 vdbg_printk(TPACPI_DBG_RFKILL,
4266 "will attempt to %s UWB\n",
4267 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4269 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4270 if (dbg_uwbemul) {
4271 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4272 return 0;
4274 #endif
4276 if (state == TPACPI_RFK_RADIO_ON)
4277 status = TP_ACPI_UWB_RADIOSSW;
4278 else
4279 status = 0;
4281 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4282 return -EIO;
4284 return 0;
4287 /* --------------------------------------------------------------------- */
4289 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4290 .get_status = uwb_get_status,
4291 .set_status = uwb_set_status,
4294 static void uwb_exit(void)
4296 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4299 static int __init uwb_init(struct ibm_init_struct *iibm)
4301 int res;
4302 int status = 0;
4304 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4305 "initializing uwb subdriver\n");
4307 TPACPI_ACPIHANDLE_INIT(hkey);
4309 tp_features.uwb = hkey_handle &&
4310 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4312 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4313 "uwb is %s, status 0x%02x\n",
4314 str_supported(tp_features.uwb),
4315 status);
4317 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4318 if (dbg_uwbemul) {
4319 tp_features.uwb = 1;
4320 printk(TPACPI_INFO
4321 "uwb switch emulation enabled\n");
4322 } else
4323 #endif
4324 if (tp_features.uwb &&
4325 !(status & TP_ACPI_UWB_HWPRESENT)) {
4326 /* no uwb hardware present in system */
4327 tp_features.uwb = 0;
4328 dbg_printk(TPACPI_DBG_INIT,
4329 "uwb hardware not installed\n");
4332 if (!tp_features.uwb)
4333 return 1;
4335 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4336 &uwb_tprfk_ops,
4337 RFKILL_TYPE_UWB,
4338 TPACPI_RFK_UWB_SW_NAME,
4339 false);
4340 return res;
4343 static struct ibm_struct uwb_driver_data = {
4344 .name = "uwb",
4345 .exit = uwb_exit,
4346 .flags.experimental = 1,
4349 /*************************************************************************
4350 * Video subdriver
4353 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4355 enum video_access_mode {
4356 TPACPI_VIDEO_NONE = 0,
4357 TPACPI_VIDEO_570, /* 570 */
4358 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4359 TPACPI_VIDEO_NEW, /* all others */
4362 enum { /* video status flags, based on VIDEO_570 */
4363 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4364 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4365 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4368 enum { /* TPACPI_VIDEO_570 constants */
4369 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4370 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4371 * video_status_flags */
4372 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4373 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4376 static enum video_access_mode video_supported;
4377 static int video_orig_autosw;
4379 static int video_autosw_get(void);
4380 static int video_autosw_set(int enable);
4382 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4384 static int __init video_init(struct ibm_init_struct *iibm)
4386 int ivga;
4388 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4390 TPACPI_ACPIHANDLE_INIT(vid);
4391 if (tpacpi_is_ibm())
4392 TPACPI_ACPIHANDLE_INIT(vid2);
4394 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4395 /* G41, assume IVGA doesn't change */
4396 vid_handle = vid2_handle;
4398 if (!vid_handle)
4399 /* video switching not supported on R30, R31 */
4400 video_supported = TPACPI_VIDEO_NONE;
4401 else if (tpacpi_is_ibm() &&
4402 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4403 /* 570 */
4404 video_supported = TPACPI_VIDEO_570;
4405 else if (tpacpi_is_ibm() &&
4406 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4407 /* 600e/x, 770e, 770x */
4408 video_supported = TPACPI_VIDEO_770;
4409 else
4410 /* all others */
4411 video_supported = TPACPI_VIDEO_NEW;
4413 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4414 str_supported(video_supported != TPACPI_VIDEO_NONE),
4415 video_supported);
4417 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4420 static void video_exit(void)
4422 dbg_printk(TPACPI_DBG_EXIT,
4423 "restoring original video autoswitch mode\n");
4424 if (video_autosw_set(video_orig_autosw))
4425 printk(TPACPI_ERR "error while trying to restore original "
4426 "video autoswitch mode\n");
4429 static int video_outputsw_get(void)
4431 int status = 0;
4432 int i;
4434 switch (video_supported) {
4435 case TPACPI_VIDEO_570:
4436 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4437 TP_ACPI_VIDEO_570_PHSCMD))
4438 return -EIO;
4439 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4440 break;
4441 case TPACPI_VIDEO_770:
4442 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4443 return -EIO;
4444 if (i)
4445 status |= TP_ACPI_VIDEO_S_LCD;
4446 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4447 return -EIO;
4448 if (i)
4449 status |= TP_ACPI_VIDEO_S_CRT;
4450 break;
4451 case TPACPI_VIDEO_NEW:
4452 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4453 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4454 return -EIO;
4455 if (i)
4456 status |= TP_ACPI_VIDEO_S_CRT;
4458 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4459 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4460 return -EIO;
4461 if (i)
4462 status |= TP_ACPI_VIDEO_S_LCD;
4463 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4464 return -EIO;
4465 if (i)
4466 status |= TP_ACPI_VIDEO_S_DVI;
4467 break;
4468 default:
4469 return -ENOSYS;
4472 return status;
4475 static int video_outputsw_set(int status)
4477 int autosw;
4478 int res = 0;
4480 switch (video_supported) {
4481 case TPACPI_VIDEO_570:
4482 res = acpi_evalf(NULL, NULL,
4483 "\\_SB.PHS2", "vdd",
4484 TP_ACPI_VIDEO_570_PHS2CMD,
4485 status | TP_ACPI_VIDEO_570_PHS2SET);
4486 break;
4487 case TPACPI_VIDEO_770:
4488 autosw = video_autosw_get();
4489 if (autosw < 0)
4490 return autosw;
4492 res = video_autosw_set(1);
4493 if (res)
4494 return res;
4495 res = acpi_evalf(vid_handle, NULL,
4496 "ASWT", "vdd", status * 0x100, 0);
4497 if (!autosw && video_autosw_set(autosw)) {
4498 printk(TPACPI_ERR
4499 "video auto-switch left enabled due to error\n");
4500 return -EIO;
4502 break;
4503 case TPACPI_VIDEO_NEW:
4504 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4505 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4506 break;
4507 default:
4508 return -ENOSYS;
4511 return (res)? 0 : -EIO;
4514 static int video_autosw_get(void)
4516 int autosw = 0;
4518 switch (video_supported) {
4519 case TPACPI_VIDEO_570:
4520 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4521 return -EIO;
4522 break;
4523 case TPACPI_VIDEO_770:
4524 case TPACPI_VIDEO_NEW:
4525 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4526 return -EIO;
4527 break;
4528 default:
4529 return -ENOSYS;
4532 return autosw & 1;
4535 static int video_autosw_set(int enable)
4537 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4538 return -EIO;
4539 return 0;
4542 static int video_outputsw_cycle(void)
4544 int autosw = video_autosw_get();
4545 int res;
4547 if (autosw < 0)
4548 return autosw;
4550 switch (video_supported) {
4551 case TPACPI_VIDEO_570:
4552 res = video_autosw_set(1);
4553 if (res)
4554 return res;
4555 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4556 break;
4557 case TPACPI_VIDEO_770:
4558 case TPACPI_VIDEO_NEW:
4559 res = video_autosw_set(1);
4560 if (res)
4561 return res;
4562 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4563 break;
4564 default:
4565 return -ENOSYS;
4567 if (!autosw && video_autosw_set(autosw)) {
4568 printk(TPACPI_ERR
4569 "video auto-switch left enabled due to error\n");
4570 return -EIO;
4573 return (res)? 0 : -EIO;
4576 static int video_expand_toggle(void)
4578 switch (video_supported) {
4579 case TPACPI_VIDEO_570:
4580 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4581 0 : -EIO;
4582 case TPACPI_VIDEO_770:
4583 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4584 0 : -EIO;
4585 case TPACPI_VIDEO_NEW:
4586 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4587 0 : -EIO;
4588 default:
4589 return -ENOSYS;
4591 /* not reached */
4594 static int video_read(struct seq_file *m)
4596 int status, autosw;
4598 if (video_supported == TPACPI_VIDEO_NONE) {
4599 seq_printf(m, "status:\t\tnot supported\n");
4600 return 0;
4603 /* Even reads can crash X.org, so... */
4604 if (!capable(CAP_SYS_ADMIN))
4605 return -EPERM;
4607 status = video_outputsw_get();
4608 if (status < 0)
4609 return status;
4611 autosw = video_autosw_get();
4612 if (autosw < 0)
4613 return autosw;
4615 seq_printf(m, "status:\t\tsupported\n");
4616 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4617 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4618 if (video_supported == TPACPI_VIDEO_NEW)
4619 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4620 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4621 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4622 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4623 if (video_supported == TPACPI_VIDEO_NEW)
4624 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4625 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4626 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4628 return 0;
4631 static int video_write(char *buf)
4633 char *cmd;
4634 int enable, disable, status;
4635 int res;
4637 if (video_supported == TPACPI_VIDEO_NONE)
4638 return -ENODEV;
4640 /* Even reads can crash X.org, let alone writes... */
4641 if (!capable(CAP_SYS_ADMIN))
4642 return -EPERM;
4644 enable = 0;
4645 disable = 0;
4647 while ((cmd = next_cmd(&buf))) {
4648 if (strlencmp(cmd, "lcd_enable") == 0) {
4649 enable |= TP_ACPI_VIDEO_S_LCD;
4650 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4651 disable |= TP_ACPI_VIDEO_S_LCD;
4652 } else if (strlencmp(cmd, "crt_enable") == 0) {
4653 enable |= TP_ACPI_VIDEO_S_CRT;
4654 } else if (strlencmp(cmd, "crt_disable") == 0) {
4655 disable |= TP_ACPI_VIDEO_S_CRT;
4656 } else if (video_supported == TPACPI_VIDEO_NEW &&
4657 strlencmp(cmd, "dvi_enable") == 0) {
4658 enable |= TP_ACPI_VIDEO_S_DVI;
4659 } else if (video_supported == TPACPI_VIDEO_NEW &&
4660 strlencmp(cmd, "dvi_disable") == 0) {
4661 disable |= TP_ACPI_VIDEO_S_DVI;
4662 } else if (strlencmp(cmd, "auto_enable") == 0) {
4663 res = video_autosw_set(1);
4664 if (res)
4665 return res;
4666 } else if (strlencmp(cmd, "auto_disable") == 0) {
4667 res = video_autosw_set(0);
4668 if (res)
4669 return res;
4670 } else if (strlencmp(cmd, "video_switch") == 0) {
4671 res = video_outputsw_cycle();
4672 if (res)
4673 return res;
4674 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4675 res = video_expand_toggle();
4676 if (res)
4677 return res;
4678 } else
4679 return -EINVAL;
4682 if (enable || disable) {
4683 status = video_outputsw_get();
4684 if (status < 0)
4685 return status;
4686 res = video_outputsw_set((status & ~disable) | enable);
4687 if (res)
4688 return res;
4691 return 0;
4694 static struct ibm_struct video_driver_data = {
4695 .name = "video",
4696 .read = video_read,
4697 .write = video_write,
4698 .exit = video_exit,
4701 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4703 /*************************************************************************
4704 * Light (thinklight) subdriver
4707 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4708 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4710 static int light_get_status(void)
4712 int status = 0;
4714 if (tp_features.light_status) {
4715 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4716 return -EIO;
4717 return (!!status);
4720 return -ENXIO;
4723 static int light_set_status(int status)
4725 int rc;
4727 if (tp_features.light) {
4728 if (cmos_handle) {
4729 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4730 (status)?
4731 TP_CMOS_THINKLIGHT_ON :
4732 TP_CMOS_THINKLIGHT_OFF);
4733 } else {
4734 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4735 (status)? 1 : 0);
4737 return (rc)? 0 : -EIO;
4740 return -ENXIO;
4743 static void light_set_status_worker(struct work_struct *work)
4745 struct tpacpi_led_classdev *data =
4746 container_of(work, struct tpacpi_led_classdev, work);
4748 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4749 light_set_status((data->new_state != TPACPI_LED_OFF));
4752 static void light_sysfs_set(struct led_classdev *led_cdev,
4753 enum led_brightness brightness)
4755 struct tpacpi_led_classdev *data =
4756 container_of(led_cdev,
4757 struct tpacpi_led_classdev,
4758 led_classdev);
4759 data->new_state = (brightness != LED_OFF) ?
4760 TPACPI_LED_ON : TPACPI_LED_OFF;
4761 queue_work(tpacpi_wq, &data->work);
4764 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4766 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4769 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4770 .led_classdev = {
4771 .name = "tpacpi::thinklight",
4772 .brightness_set = &light_sysfs_set,
4773 .brightness_get = &light_sysfs_get,
4777 static int __init light_init(struct ibm_init_struct *iibm)
4779 int rc;
4781 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4783 if (tpacpi_is_ibm()) {
4784 TPACPI_ACPIHANDLE_INIT(ledb);
4785 TPACPI_ACPIHANDLE_INIT(lght);
4787 TPACPI_ACPIHANDLE_INIT(cmos);
4788 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4790 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4791 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4793 if (tp_features.light)
4794 /* light status not supported on
4795 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4796 tp_features.light_status =
4797 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4799 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4800 str_supported(tp_features.light),
4801 str_supported(tp_features.light_status));
4803 if (!tp_features.light)
4804 return 1;
4806 rc = led_classdev_register(&tpacpi_pdev->dev,
4807 &tpacpi_led_thinklight.led_classdev);
4809 if (rc < 0) {
4810 tp_features.light = 0;
4811 tp_features.light_status = 0;
4812 } else {
4813 rc = 0;
4816 return rc;
4819 static void light_exit(void)
4821 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4822 if (work_pending(&tpacpi_led_thinklight.work))
4823 flush_workqueue(tpacpi_wq);
4826 static int light_read(struct seq_file *m)
4828 int status;
4830 if (!tp_features.light) {
4831 seq_printf(m, "status:\t\tnot supported\n");
4832 } else if (!tp_features.light_status) {
4833 seq_printf(m, "status:\t\tunknown\n");
4834 seq_printf(m, "commands:\ton, off\n");
4835 } else {
4836 status = light_get_status();
4837 if (status < 0)
4838 return status;
4839 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4840 seq_printf(m, "commands:\ton, off\n");
4843 return 0;
4846 static int light_write(char *buf)
4848 char *cmd;
4849 int newstatus = 0;
4851 if (!tp_features.light)
4852 return -ENODEV;
4854 while ((cmd = next_cmd(&buf))) {
4855 if (strlencmp(cmd, "on") == 0) {
4856 newstatus = 1;
4857 } else if (strlencmp(cmd, "off") == 0) {
4858 newstatus = 0;
4859 } else
4860 return -EINVAL;
4863 return light_set_status(newstatus);
4866 static struct ibm_struct light_driver_data = {
4867 .name = "light",
4868 .read = light_read,
4869 .write = light_write,
4870 .exit = light_exit,
4873 /*************************************************************************
4874 * CMOS subdriver
4877 /* sysfs cmos_command -------------------------------------------------- */
4878 static ssize_t cmos_command_store(struct device *dev,
4879 struct device_attribute *attr,
4880 const char *buf, size_t count)
4882 unsigned long cmos_cmd;
4883 int res;
4885 if (parse_strtoul(buf, 21, &cmos_cmd))
4886 return -EINVAL;
4888 res = issue_thinkpad_cmos_command(cmos_cmd);
4889 return (res)? res : count;
4892 static struct device_attribute dev_attr_cmos_command =
4893 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4895 /* --------------------------------------------------------------------- */
4897 static int __init cmos_init(struct ibm_init_struct *iibm)
4899 int res;
4901 vdbg_printk(TPACPI_DBG_INIT,
4902 "initializing cmos commands subdriver\n");
4904 TPACPI_ACPIHANDLE_INIT(cmos);
4906 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4907 str_supported(cmos_handle != NULL));
4909 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4910 if (res)
4911 return res;
4913 return (cmos_handle)? 0 : 1;
4916 static void cmos_exit(void)
4918 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4921 static int cmos_read(struct seq_file *m)
4923 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4924 R30, R31, T20-22, X20-21 */
4925 if (!cmos_handle)
4926 seq_printf(m, "status:\t\tnot supported\n");
4927 else {
4928 seq_printf(m, "status:\t\tsupported\n");
4929 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4932 return 0;
4935 static int cmos_write(char *buf)
4937 char *cmd;
4938 int cmos_cmd, res;
4940 while ((cmd = next_cmd(&buf))) {
4941 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4942 cmos_cmd >= 0 && cmos_cmd <= 21) {
4943 /* cmos_cmd set */
4944 } else
4945 return -EINVAL;
4947 res = issue_thinkpad_cmos_command(cmos_cmd);
4948 if (res)
4949 return res;
4952 return 0;
4955 static struct ibm_struct cmos_driver_data = {
4956 .name = "cmos",
4957 .read = cmos_read,
4958 .write = cmos_write,
4959 .exit = cmos_exit,
4962 /*************************************************************************
4963 * LED subdriver
4966 enum led_access_mode {
4967 TPACPI_LED_NONE = 0,
4968 TPACPI_LED_570, /* 570 */
4969 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4970 TPACPI_LED_NEW, /* all others */
4973 enum { /* For TPACPI_LED_OLD */
4974 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4975 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4976 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4979 static enum led_access_mode led_supported;
4981 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4982 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4983 /* T20-22, X20-21 */
4984 "LED", /* all others */
4985 ); /* R30, R31 */
4987 #define TPACPI_LED_NUMLEDS 16
4988 static struct tpacpi_led_classdev *tpacpi_leds;
4989 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4990 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4991 /* there's a limit of 19 chars + NULL before 2.6.26 */
4992 "tpacpi::power",
4993 "tpacpi:orange:batt",
4994 "tpacpi:green:batt",
4995 "tpacpi::dock_active",
4996 "tpacpi::bay_active",
4997 "tpacpi::dock_batt",
4998 "tpacpi::unknown_led",
4999 "tpacpi::standby",
5000 "tpacpi::dock_status1",
5001 "tpacpi::dock_status2",
5002 "tpacpi::unknown_led2",
5003 "tpacpi::unknown_led3",
5004 "tpacpi::thinkvantage",
5006 #define TPACPI_SAFE_LEDS 0x1081U
5008 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5010 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5011 return false;
5012 #else
5013 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5014 #endif
5017 static int led_get_status(const unsigned int led)
5019 int status;
5020 enum led_status_t led_s;
5022 switch (led_supported) {
5023 case TPACPI_LED_570:
5024 if (!acpi_evalf(ec_handle,
5025 &status, "GLED", "dd", 1 << led))
5026 return -EIO;
5027 led_s = (status == 0)?
5028 TPACPI_LED_OFF :
5029 ((status == 1)?
5030 TPACPI_LED_ON :
5031 TPACPI_LED_BLINK);
5032 tpacpi_led_state_cache[led] = led_s;
5033 return led_s;
5034 default:
5035 return -ENXIO;
5038 /* not reached */
5041 static int led_set_status(const unsigned int led,
5042 const enum led_status_t ledstatus)
5044 /* off, on, blink. Index is led_status_t */
5045 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5046 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5048 int rc = 0;
5050 switch (led_supported) {
5051 case TPACPI_LED_570:
5052 /* 570 */
5053 if (unlikely(led > 7))
5054 return -EINVAL;
5055 if (unlikely(tpacpi_is_led_restricted(led)))
5056 return -EPERM;
5057 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5058 (1 << led), led_sled_arg1[ledstatus]))
5059 rc = -EIO;
5060 break;
5061 case TPACPI_LED_OLD:
5062 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5063 if (unlikely(led > 7))
5064 return -EINVAL;
5065 if (unlikely(tpacpi_is_led_restricted(led)))
5066 return -EPERM;
5067 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5068 if (rc >= 0)
5069 rc = ec_write(TPACPI_LED_EC_HLBL,
5070 (ledstatus == TPACPI_LED_BLINK) << led);
5071 if (rc >= 0)
5072 rc = ec_write(TPACPI_LED_EC_HLCL,
5073 (ledstatus != TPACPI_LED_OFF) << led);
5074 break;
5075 case TPACPI_LED_NEW:
5076 /* all others */
5077 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5078 return -EINVAL;
5079 if (unlikely(tpacpi_is_led_restricted(led)))
5080 return -EPERM;
5081 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5082 led, led_led_arg1[ledstatus]))
5083 rc = -EIO;
5084 break;
5085 default:
5086 rc = -ENXIO;
5089 if (!rc)
5090 tpacpi_led_state_cache[led] = ledstatus;
5092 return rc;
5095 static void led_set_status_worker(struct work_struct *work)
5097 struct tpacpi_led_classdev *data =
5098 container_of(work, struct tpacpi_led_classdev, work);
5100 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5101 led_set_status(data->led, data->new_state);
5104 static void led_sysfs_set(struct led_classdev *led_cdev,
5105 enum led_brightness brightness)
5107 struct tpacpi_led_classdev *data = container_of(led_cdev,
5108 struct tpacpi_led_classdev, led_classdev);
5110 if (brightness == LED_OFF)
5111 data->new_state = TPACPI_LED_OFF;
5112 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5113 data->new_state = TPACPI_LED_ON;
5114 else
5115 data->new_state = TPACPI_LED_BLINK;
5117 queue_work(tpacpi_wq, &data->work);
5120 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5121 unsigned long *delay_on, unsigned long *delay_off)
5123 struct tpacpi_led_classdev *data = container_of(led_cdev,
5124 struct tpacpi_led_classdev, led_classdev);
5126 /* Can we choose the flash rate? */
5127 if (*delay_on == 0 && *delay_off == 0) {
5128 /* yes. set them to the hardware blink rate (1 Hz) */
5129 *delay_on = 500; /* ms */
5130 *delay_off = 500; /* ms */
5131 } else if ((*delay_on != 500) || (*delay_off != 500))
5132 return -EINVAL;
5134 data->new_state = TPACPI_LED_BLINK;
5135 queue_work(tpacpi_wq, &data->work);
5137 return 0;
5140 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5142 int rc;
5144 struct tpacpi_led_classdev *data = container_of(led_cdev,
5145 struct tpacpi_led_classdev, led_classdev);
5147 rc = led_get_status(data->led);
5149 if (rc == TPACPI_LED_OFF || rc < 0)
5150 rc = LED_OFF; /* no error handling in led class :( */
5151 else
5152 rc = LED_FULL;
5154 return rc;
5157 static void led_exit(void)
5159 unsigned int i;
5161 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5162 if (tpacpi_leds[i].led_classdev.name)
5163 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5166 kfree(tpacpi_leds);
5169 static int __init tpacpi_init_led(unsigned int led)
5171 int rc;
5173 tpacpi_leds[led].led = led;
5175 /* LEDs with no name don't get registered */
5176 if (!tpacpi_led_names[led])
5177 return 0;
5179 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5180 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5181 if (led_supported == TPACPI_LED_570)
5182 tpacpi_leds[led].led_classdev.brightness_get =
5183 &led_sysfs_get;
5185 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5187 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5189 rc = led_classdev_register(&tpacpi_pdev->dev,
5190 &tpacpi_leds[led].led_classdev);
5191 if (rc < 0)
5192 tpacpi_leds[led].led_classdev.name = NULL;
5194 return rc;
5197 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5198 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5199 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5200 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5202 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5203 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5204 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5205 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5206 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5207 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5208 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5209 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5211 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5212 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5213 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5214 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5215 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5217 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5218 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5219 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5220 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5222 /* (1) - may have excess leds enabled on MSB */
5224 /* Defaults (order matters, keep last, don't reorder!) */
5225 { /* Lenovo */
5226 .vendor = PCI_VENDOR_ID_LENOVO,
5227 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5228 .quirks = 0x1fffU,
5230 { /* IBM ThinkPads with no EC version string */
5231 .vendor = PCI_VENDOR_ID_IBM,
5232 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5233 .quirks = 0x00ffU,
5235 { /* IBM ThinkPads with EC version string */
5236 .vendor = PCI_VENDOR_ID_IBM,
5237 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5238 .quirks = 0x00bfU,
5242 #undef TPACPI_LEDQ_IBM
5243 #undef TPACPI_LEDQ_LNV
5245 static int __init led_init(struct ibm_init_struct *iibm)
5247 unsigned int i;
5248 int rc;
5249 unsigned long useful_leds;
5251 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5253 TPACPI_ACPIHANDLE_INIT(led);
5255 if (!led_handle)
5256 /* led not supported on R30, R31 */
5257 led_supported = TPACPI_LED_NONE;
5258 else if (tpacpi_is_ibm() && strlencmp(led_path, "SLED") == 0)
5259 /* 570 */
5260 led_supported = TPACPI_LED_570;
5261 else if (tpacpi_is_ibm() && strlencmp(led_path, "SYSL") == 0)
5262 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5263 led_supported = TPACPI_LED_OLD;
5264 else
5265 /* all others */
5266 led_supported = TPACPI_LED_NEW;
5268 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5269 str_supported(led_supported), led_supported);
5271 if (led_supported == TPACPI_LED_NONE)
5272 return 1;
5274 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5275 GFP_KERNEL);
5276 if (!tpacpi_leds) {
5277 printk(TPACPI_ERR "Out of memory for LED data\n");
5278 return -ENOMEM;
5281 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5282 ARRAY_SIZE(led_useful_qtable));
5284 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5285 if (!tpacpi_is_led_restricted(i) &&
5286 test_bit(i, &useful_leds)) {
5287 rc = tpacpi_init_led(i);
5288 if (rc < 0) {
5289 led_exit();
5290 return rc;
5295 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5296 printk(TPACPI_NOTICE
5297 "warning: userspace override of important "
5298 "firmware LEDs is enabled\n");
5299 #endif
5300 return 0;
5303 #define str_led_status(s) \
5304 ((s) == TPACPI_LED_OFF ? "off" : \
5305 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5307 static int led_read(struct seq_file *m)
5309 if (!led_supported) {
5310 seq_printf(m, "status:\t\tnot supported\n");
5311 return 0;
5313 seq_printf(m, "status:\t\tsupported\n");
5315 if (led_supported == TPACPI_LED_570) {
5316 /* 570 */
5317 int i, status;
5318 for (i = 0; i < 8; i++) {
5319 status = led_get_status(i);
5320 if (status < 0)
5321 return -EIO;
5322 seq_printf(m, "%d:\t\t%s\n",
5323 i, str_led_status(status));
5327 seq_printf(m, "commands:\t"
5328 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5330 return 0;
5333 static int led_write(char *buf)
5335 char *cmd;
5336 int led, rc;
5337 enum led_status_t s;
5339 if (!led_supported)
5340 return -ENODEV;
5342 while ((cmd = next_cmd(&buf))) {
5343 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5344 return -EINVAL;
5346 if (strstr(cmd, "off")) {
5347 s = TPACPI_LED_OFF;
5348 } else if (strstr(cmd, "on")) {
5349 s = TPACPI_LED_ON;
5350 } else if (strstr(cmd, "blink")) {
5351 s = TPACPI_LED_BLINK;
5352 } else {
5353 return -EINVAL;
5356 rc = led_set_status(led, s);
5357 if (rc < 0)
5358 return rc;
5361 return 0;
5364 static struct ibm_struct led_driver_data = {
5365 .name = "led",
5366 .read = led_read,
5367 .write = led_write,
5368 .exit = led_exit,
5371 /*************************************************************************
5372 * Beep subdriver
5375 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5377 #define TPACPI_BEEP_Q1 0x0001
5379 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5380 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5381 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5384 static int __init beep_init(struct ibm_init_struct *iibm)
5386 unsigned long quirks;
5388 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5390 TPACPI_ACPIHANDLE_INIT(beep);
5392 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5393 str_supported(beep_handle != NULL));
5395 quirks = tpacpi_check_quirks(beep_quirk_table,
5396 ARRAY_SIZE(beep_quirk_table));
5398 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5400 return (beep_handle)? 0 : 1;
5403 static int beep_read(struct seq_file *m)
5405 if (!beep_handle)
5406 seq_printf(m, "status:\t\tnot supported\n");
5407 else {
5408 seq_printf(m, "status:\t\tsupported\n");
5409 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5412 return 0;
5415 static int beep_write(char *buf)
5417 char *cmd;
5418 int beep_cmd;
5420 if (!beep_handle)
5421 return -ENODEV;
5423 while ((cmd = next_cmd(&buf))) {
5424 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5425 beep_cmd >= 0 && beep_cmd <= 17) {
5426 /* beep_cmd set */
5427 } else
5428 return -EINVAL;
5429 if (tp_features.beep_needs_two_args) {
5430 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5431 beep_cmd, 0))
5432 return -EIO;
5433 } else {
5434 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5435 beep_cmd))
5436 return -EIO;
5440 return 0;
5443 static struct ibm_struct beep_driver_data = {
5444 .name = "beep",
5445 .read = beep_read,
5446 .write = beep_write,
5449 /*************************************************************************
5450 * Thermal subdriver
5453 enum thermal_access_mode {
5454 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5455 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5456 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5457 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5458 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5461 enum { /* TPACPI_THERMAL_TPEC_* */
5462 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5463 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5464 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5466 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5470 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5471 struct ibm_thermal_sensors_struct {
5472 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5475 static enum thermal_access_mode thermal_read_mode;
5477 /* idx is zero-based */
5478 static int thermal_get_sensor(int idx, s32 *value)
5480 int t;
5481 s8 tmp;
5482 char tmpi[5];
5484 t = TP_EC_THERMAL_TMP0;
5486 switch (thermal_read_mode) {
5487 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5488 case TPACPI_THERMAL_TPEC_16:
5489 if (idx >= 8 && idx <= 15) {
5490 t = TP_EC_THERMAL_TMP8;
5491 idx -= 8;
5493 /* fallthrough */
5494 #endif
5495 case TPACPI_THERMAL_TPEC_8:
5496 if (idx <= 7) {
5497 if (!acpi_ec_read(t + idx, &tmp))
5498 return -EIO;
5499 *value = tmp * 1000;
5500 return 0;
5502 break;
5504 case TPACPI_THERMAL_ACPI_UPDT:
5505 if (idx <= 7) {
5506 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5507 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5508 return -EIO;
5509 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5510 return -EIO;
5511 *value = (t - 2732) * 100;
5512 return 0;
5514 break;
5516 case TPACPI_THERMAL_ACPI_TMP07:
5517 if (idx <= 7) {
5518 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5519 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5520 return -EIO;
5521 if (t > 127 || t < -127)
5522 t = TP_EC_THERMAL_TMP_NA;
5523 *value = t * 1000;
5524 return 0;
5526 break;
5528 case TPACPI_THERMAL_NONE:
5529 default:
5530 return -ENOSYS;
5533 return -EINVAL;
5536 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5538 int res, i;
5539 int n;
5541 n = 8;
5542 i = 0;
5544 if (!s)
5545 return -EINVAL;
5547 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5548 n = 16;
5550 for (i = 0 ; i < n; i++) {
5551 res = thermal_get_sensor(i, &s->temp[i]);
5552 if (res)
5553 return res;
5556 return n;
5559 static void thermal_dump_all_sensors(void)
5561 int n, i;
5562 struct ibm_thermal_sensors_struct t;
5564 n = thermal_get_sensors(&t);
5565 if (n <= 0)
5566 return;
5568 printk(TPACPI_NOTICE
5569 "temperatures (Celsius):");
5571 for (i = 0; i < n; i++) {
5572 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5573 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5574 else
5575 printk(KERN_CONT " N/A");
5578 printk(KERN_CONT "\n");
5581 /* sysfs temp##_input -------------------------------------------------- */
5583 static ssize_t thermal_temp_input_show(struct device *dev,
5584 struct device_attribute *attr,
5585 char *buf)
5587 struct sensor_device_attribute *sensor_attr =
5588 to_sensor_dev_attr(attr);
5589 int idx = sensor_attr->index;
5590 s32 value;
5591 int res;
5593 res = thermal_get_sensor(idx, &value);
5594 if (res)
5595 return res;
5596 if (value == TPACPI_THERMAL_SENSOR_NA)
5597 return -ENXIO;
5599 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5602 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5603 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5604 thermal_temp_input_show, NULL, _idxB)
5606 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5607 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5608 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5609 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5610 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5611 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5612 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5613 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5614 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5615 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5616 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5617 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5618 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5619 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5620 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5621 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5622 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5625 #define THERMAL_ATTRS(X) \
5626 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5628 static struct attribute *thermal_temp_input_attr[] = {
5629 THERMAL_ATTRS(8),
5630 THERMAL_ATTRS(9),
5631 THERMAL_ATTRS(10),
5632 THERMAL_ATTRS(11),
5633 THERMAL_ATTRS(12),
5634 THERMAL_ATTRS(13),
5635 THERMAL_ATTRS(14),
5636 THERMAL_ATTRS(15),
5637 THERMAL_ATTRS(0),
5638 THERMAL_ATTRS(1),
5639 THERMAL_ATTRS(2),
5640 THERMAL_ATTRS(3),
5641 THERMAL_ATTRS(4),
5642 THERMAL_ATTRS(5),
5643 THERMAL_ATTRS(6),
5644 THERMAL_ATTRS(7),
5645 NULL
5648 static const struct attribute_group thermal_temp_input16_group = {
5649 .attrs = thermal_temp_input_attr
5652 static const struct attribute_group thermal_temp_input8_group = {
5653 .attrs = &thermal_temp_input_attr[8]
5656 #undef THERMAL_SENSOR_ATTR_TEMP
5657 #undef THERMAL_ATTRS
5659 /* --------------------------------------------------------------------- */
5661 static int __init thermal_init(struct ibm_init_struct *iibm)
5663 u8 t, ta1, ta2;
5664 int i;
5665 int acpi_tmp7;
5666 int res;
5668 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5670 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5672 if (thinkpad_id.ec_model) {
5674 * Direct EC access mode: sensors at registers
5675 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5676 * non-implemented, thermal sensors return 0x80 when
5677 * not available
5680 ta1 = ta2 = 0;
5681 for (i = 0; i < 8; i++) {
5682 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5683 ta1 |= t;
5684 } else {
5685 ta1 = 0;
5686 break;
5688 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5689 ta2 |= t;
5690 } else {
5691 ta1 = 0;
5692 break;
5695 if (ta1 == 0) {
5696 /* This is sheer paranoia, but we handle it anyway */
5697 if (acpi_tmp7) {
5698 printk(TPACPI_ERR
5699 "ThinkPad ACPI EC access misbehaving, "
5700 "falling back to ACPI TMPx access "
5701 "mode\n");
5702 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5703 } else {
5704 printk(TPACPI_ERR
5705 "ThinkPad ACPI EC access misbehaving, "
5706 "disabling thermal sensors access\n");
5707 thermal_read_mode = TPACPI_THERMAL_NONE;
5709 } else {
5710 thermal_read_mode =
5711 (ta2 != 0) ?
5712 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5714 } else if (acpi_tmp7) {
5715 if (tpacpi_is_ibm() &&
5716 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5717 /* 600e/x, 770e, 770x */
5718 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5719 } else {
5720 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
5721 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5723 } else {
5724 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5725 thermal_read_mode = TPACPI_THERMAL_NONE;
5728 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5729 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5730 thermal_read_mode);
5732 switch (thermal_read_mode) {
5733 case TPACPI_THERMAL_TPEC_16:
5734 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5735 &thermal_temp_input16_group);
5736 if (res)
5737 return res;
5738 break;
5739 case TPACPI_THERMAL_TPEC_8:
5740 case TPACPI_THERMAL_ACPI_TMP07:
5741 case TPACPI_THERMAL_ACPI_UPDT:
5742 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5743 &thermal_temp_input8_group);
5744 if (res)
5745 return res;
5746 break;
5747 case TPACPI_THERMAL_NONE:
5748 default:
5749 return 1;
5752 return 0;
5755 static void thermal_exit(void)
5757 switch (thermal_read_mode) {
5758 case TPACPI_THERMAL_TPEC_16:
5759 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5760 &thermal_temp_input16_group);
5761 break;
5762 case TPACPI_THERMAL_TPEC_8:
5763 case TPACPI_THERMAL_ACPI_TMP07:
5764 case TPACPI_THERMAL_ACPI_UPDT:
5765 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5766 &thermal_temp_input8_group);
5767 break;
5768 case TPACPI_THERMAL_NONE:
5769 default:
5770 break;
5774 static int thermal_read(struct seq_file *m)
5776 int n, i;
5777 struct ibm_thermal_sensors_struct t;
5779 n = thermal_get_sensors(&t);
5780 if (unlikely(n < 0))
5781 return n;
5783 seq_printf(m, "temperatures:\t");
5785 if (n > 0) {
5786 for (i = 0; i < (n - 1); i++)
5787 seq_printf(m, "%d ", t.temp[i] / 1000);
5788 seq_printf(m, "%d\n", t.temp[i] / 1000);
5789 } else
5790 seq_printf(m, "not supported\n");
5792 return 0;
5795 static struct ibm_struct thermal_driver_data = {
5796 .name = "thermal",
5797 .read = thermal_read,
5798 .exit = thermal_exit,
5801 /*************************************************************************
5802 * EC Dump subdriver
5805 static u8 ecdump_regs[256];
5807 static int ecdump_read(struct seq_file *m)
5809 int i, j;
5810 u8 v;
5812 seq_printf(m, "EC "
5813 " +00 +01 +02 +03 +04 +05 +06 +07"
5814 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5815 for (i = 0; i < 256; i += 16) {
5816 seq_printf(m, "EC 0x%02x:", i);
5817 for (j = 0; j < 16; j++) {
5818 if (!acpi_ec_read(i + j, &v))
5819 break;
5820 if (v != ecdump_regs[i + j])
5821 seq_printf(m, " *%02x", v);
5822 else
5823 seq_printf(m, " %02x", v);
5824 ecdump_regs[i + j] = v;
5826 seq_putc(m, '\n');
5827 if (j != 16)
5828 break;
5831 /* These are way too dangerous to advertise openly... */
5832 #if 0
5833 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5834 " (<offset> is 00-ff, <value> is 00-ff)\n");
5835 seq_printf(m, "commands:\t0x<offset> <value> "
5836 " (<offset> is 00-ff, <value> is 0-255)\n");
5837 #endif
5838 return 0;
5841 static int ecdump_write(char *buf)
5843 char *cmd;
5844 int i, v;
5846 while ((cmd = next_cmd(&buf))) {
5847 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5848 /* i and v set */
5849 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5850 /* i and v set */
5851 } else
5852 return -EINVAL;
5853 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5854 if (!acpi_ec_write(i, v))
5855 return -EIO;
5856 } else
5857 return -EINVAL;
5860 return 0;
5863 static struct ibm_struct ecdump_driver_data = {
5864 .name = "ecdump",
5865 .read = ecdump_read,
5866 .write = ecdump_write,
5867 .flags.experimental = 1,
5870 /*************************************************************************
5871 * Backlight/brightness subdriver
5874 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5877 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5878 * CMOS NVRAM byte 0x5E, bits 0-3.
5880 * EC HBRV (0x31) has the following layout
5881 * Bit 7: unknown function
5882 * Bit 6: unknown function
5883 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5884 * Bit 4: must be set to zero to avoid problems
5885 * Bit 3-0: backlight brightness level
5887 * brightness_get_raw returns status data in the HBRV layout
5889 * WARNING: The X61 has been verified to use HBRV for something else, so
5890 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5891 * testing on the very early *60 Lenovo models...
5894 enum {
5895 TP_EC_BACKLIGHT = 0x31,
5897 /* TP_EC_BACKLIGHT bitmasks */
5898 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5899 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5900 TP_EC_BACKLIGHT_MAPSW = 0x20,
5903 enum tpacpi_brightness_access_mode {
5904 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5905 TPACPI_BRGHT_MODE_EC, /* EC control */
5906 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5907 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5908 TPACPI_BRGHT_MODE_MAX
5911 static struct backlight_device *ibm_backlight_device;
5913 static enum tpacpi_brightness_access_mode brightness_mode =
5914 TPACPI_BRGHT_MODE_MAX;
5916 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5918 static struct mutex brightness_mutex;
5920 /* NVRAM brightness access,
5921 * call with brightness_mutex held! */
5922 static unsigned int tpacpi_brightness_nvram_get(void)
5924 u8 lnvram;
5926 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5927 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5928 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5929 lnvram &= bright_maxlvl;
5931 return lnvram;
5934 static void tpacpi_brightness_checkpoint_nvram(void)
5936 u8 lec = 0;
5937 u8 b_nvram;
5939 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5940 return;
5942 vdbg_printk(TPACPI_DBG_BRGHT,
5943 "trying to checkpoint backlight level to NVRAM...\n");
5945 if (mutex_lock_killable(&brightness_mutex) < 0)
5946 return;
5948 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5949 goto unlock;
5950 lec &= TP_EC_BACKLIGHT_LVLMSK;
5951 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5953 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5954 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5955 /* NVRAM needs update */
5956 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5957 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5958 b_nvram |= lec;
5959 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5960 dbg_printk(TPACPI_DBG_BRGHT,
5961 "updated NVRAM backlight level to %u (0x%02x)\n",
5962 (unsigned int) lec, (unsigned int) b_nvram);
5963 } else
5964 vdbg_printk(TPACPI_DBG_BRGHT,
5965 "NVRAM backlight level already is %u (0x%02x)\n",
5966 (unsigned int) lec, (unsigned int) b_nvram);
5968 unlock:
5969 mutex_unlock(&brightness_mutex);
5973 /* call with brightness_mutex held! */
5974 static int tpacpi_brightness_get_raw(int *status)
5976 u8 lec = 0;
5978 switch (brightness_mode) {
5979 case TPACPI_BRGHT_MODE_UCMS_STEP:
5980 *status = tpacpi_brightness_nvram_get();
5981 return 0;
5982 case TPACPI_BRGHT_MODE_EC:
5983 case TPACPI_BRGHT_MODE_ECNVRAM:
5984 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5985 return -EIO;
5986 *status = lec;
5987 return 0;
5988 default:
5989 return -ENXIO;
5993 /* call with brightness_mutex held! */
5994 /* do NOT call with illegal backlight level value */
5995 static int tpacpi_brightness_set_ec(unsigned int value)
5997 u8 lec = 0;
5999 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6000 return -EIO;
6002 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6003 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6004 (value & TP_EC_BACKLIGHT_LVLMSK))))
6005 return -EIO;
6007 return 0;
6010 /* call with brightness_mutex held! */
6011 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6013 int cmos_cmd, inc;
6014 unsigned int current_value, i;
6016 current_value = tpacpi_brightness_nvram_get();
6018 if (value == current_value)
6019 return 0;
6021 cmos_cmd = (value > current_value) ?
6022 TP_CMOS_BRIGHTNESS_UP :
6023 TP_CMOS_BRIGHTNESS_DOWN;
6024 inc = (value > current_value) ? 1 : -1;
6026 for (i = current_value; i != value; i += inc)
6027 if (issue_thinkpad_cmos_command(cmos_cmd))
6028 return -EIO;
6030 return 0;
6033 /* May return EINTR which can always be mapped to ERESTARTSYS */
6034 static int brightness_set(unsigned int value)
6036 int res;
6038 if (value > bright_maxlvl || value < 0)
6039 return -EINVAL;
6041 vdbg_printk(TPACPI_DBG_BRGHT,
6042 "set backlight level to %d\n", value);
6044 res = mutex_lock_killable(&brightness_mutex);
6045 if (res < 0)
6046 return res;
6048 switch (brightness_mode) {
6049 case TPACPI_BRGHT_MODE_EC:
6050 case TPACPI_BRGHT_MODE_ECNVRAM:
6051 res = tpacpi_brightness_set_ec(value);
6052 break;
6053 case TPACPI_BRGHT_MODE_UCMS_STEP:
6054 res = tpacpi_brightness_set_ucmsstep(value);
6055 break;
6056 default:
6057 res = -ENXIO;
6060 mutex_unlock(&brightness_mutex);
6061 return res;
6064 /* sysfs backlight class ----------------------------------------------- */
6066 static int brightness_update_status(struct backlight_device *bd)
6068 unsigned int level =
6069 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6070 bd->props.power == FB_BLANK_UNBLANK) ?
6071 bd->props.brightness : 0;
6073 dbg_printk(TPACPI_DBG_BRGHT,
6074 "backlight: attempt to set level to %d\n",
6075 level);
6077 /* it is the backlight class's job (caller) to handle
6078 * EINTR and other errors properly */
6079 return brightness_set(level);
6082 static int brightness_get(struct backlight_device *bd)
6084 int status, res;
6086 res = mutex_lock_killable(&brightness_mutex);
6087 if (res < 0)
6088 return 0;
6090 res = tpacpi_brightness_get_raw(&status);
6092 mutex_unlock(&brightness_mutex);
6094 if (res < 0)
6095 return 0;
6097 return status & TP_EC_BACKLIGHT_LVLMSK;
6100 static void tpacpi_brightness_notify_change(void)
6102 backlight_force_update(ibm_backlight_device,
6103 BACKLIGHT_UPDATE_HOTKEY);
6106 static struct backlight_ops ibm_backlight_data = {
6107 .get_brightness = brightness_get,
6108 .update_status = brightness_update_status,
6111 /* --------------------------------------------------------------------- */
6113 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6115 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6116 union acpi_object *obj;
6117 int rc;
6119 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
6120 obj = (union acpi_object *)buffer.pointer;
6121 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6122 printk(TPACPI_ERR "Unknown _BCL data, "
6123 "please report this to %s\n", TPACPI_MAIL);
6124 rc = 0;
6125 } else {
6126 rc = obj->package.count;
6128 } else {
6129 return 0;
6132 kfree(buffer.pointer);
6133 return rc;
6136 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
6137 u32 lvl, void *context, void **rv)
6139 char name[ACPI_PATH_SEGMENT_LENGTH];
6140 struct acpi_buffer buffer = { sizeof(name), &name };
6142 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
6143 !strncmp("_BCL", name, sizeof(name) - 1)) {
6144 BUG_ON(!rv || !*rv);
6145 **(int **)rv = tpacpi_query_bcl_levels(handle);
6146 return AE_CTRL_TERMINATE;
6147 } else {
6148 return AE_OK;
6153 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6155 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6157 int status;
6158 int bcl_levels = 0;
6159 void *bcl_ptr = &bcl_levels;
6161 if (!vid_handle)
6162 TPACPI_ACPIHANDLE_INIT(vid);
6164 if (!vid_handle)
6165 return 0;
6168 * Search for a _BCL method, and execute it. This is safe on all
6169 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
6170 * BIOS in ACPI backlight control mode. We do NOT have to care
6171 * about calling the _BCL method in an enabled video device, any
6172 * will do for our purposes.
6175 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
6176 tpacpi_acpi_walk_find_bcl, NULL, NULL,
6177 &bcl_ptr);
6179 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
6180 tp_features.bright_acpimode = 1;
6181 return bcl_levels - 2;
6184 return 0;
6188 * These are only useful for models that have only one possibility
6189 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6190 * these quirks.
6192 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6193 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6194 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6196 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6197 /* Models with ATI GPUs known to require ECNVRAM mode */
6198 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6200 /* Models with ATI GPUs that can use ECNVRAM */
6201 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6202 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6203 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6204 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6206 /* Models with Intel Extreme Graphics 2 */
6207 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6208 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6209 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6211 /* Models with Intel GMA900 */
6212 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6213 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6214 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6218 * Returns < 0 for error, otherwise sets tp_features.bright_*
6219 * and bright_maxlvl.
6221 static void __init tpacpi_detect_brightness_capabilities(void)
6223 unsigned int b;
6225 vdbg_printk(TPACPI_DBG_INIT,
6226 "detecting firmware brightness interface capabilities\n");
6228 /* we could run a quirks check here (same table used by
6229 * brightness_init) if needed */
6232 * We always attempt to detect acpi support, so as to switch
6233 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6234 * going to publish a backlight interface
6236 b = tpacpi_check_std_acpi_brightness_support();
6237 switch (b) {
6238 case 16:
6239 bright_maxlvl = 15;
6240 printk(TPACPI_INFO
6241 "detected a 16-level brightness capable ThinkPad\n");
6242 break;
6243 case 8:
6244 case 0:
6245 bright_maxlvl = 7;
6246 printk(TPACPI_INFO
6247 "detected a 8-level brightness capable ThinkPad\n");
6248 break;
6249 default:
6250 printk(TPACPI_ERR
6251 "Unsupported brightness interface, "
6252 "please contact %s\n", TPACPI_MAIL);
6253 tp_features.bright_unkfw = 1;
6254 bright_maxlvl = b - 1;
6258 static int __init brightness_init(struct ibm_init_struct *iibm)
6260 int b;
6261 unsigned long quirks;
6263 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6265 mutex_init(&brightness_mutex);
6267 quirks = tpacpi_check_quirks(brightness_quirk_table,
6268 ARRAY_SIZE(brightness_quirk_table));
6270 /* tpacpi_detect_brightness_capabilities() must have run already */
6272 /* if it is unknown, we don't handle it: it wouldn't be safe */
6273 if (tp_features.bright_unkfw)
6274 return 1;
6276 if (tp_features.bright_acpimode) {
6277 if (acpi_video_backlight_support()) {
6278 if (brightness_enable > 1) {
6279 printk(TPACPI_NOTICE
6280 "Standard ACPI backlight interface "
6281 "available, not loading native one.\n");
6282 return 1;
6283 } else if (brightness_enable == 1) {
6284 printk(TPACPI_NOTICE
6285 "Backlight control force enabled, even if standard "
6286 "ACPI backlight interface is available\n");
6288 } else {
6289 if (brightness_enable > 1) {
6290 printk(TPACPI_NOTICE
6291 "Standard ACPI backlight interface not "
6292 "available, thinkpad_acpi native "
6293 "brightness control enabled\n");
6298 if (!brightness_enable) {
6299 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6300 "brightness support disabled by "
6301 "module parameter\n");
6302 return 1;
6306 * Check for module parameter bogosity, note that we
6307 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6308 * able to detect "unspecified"
6310 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6311 return -EINVAL;
6313 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6314 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6315 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6316 if (quirks & TPACPI_BRGHT_Q_EC)
6317 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6318 else
6319 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6321 dbg_printk(TPACPI_DBG_BRGHT,
6322 "driver auto-selected brightness_mode=%d\n",
6323 brightness_mode);
6326 /* Safety */
6327 if (!tpacpi_is_ibm() &&
6328 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6329 brightness_mode == TPACPI_BRGHT_MODE_EC))
6330 return -EINVAL;
6332 if (tpacpi_brightness_get_raw(&b) < 0)
6333 return 1;
6335 ibm_backlight_device = backlight_device_register(
6336 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6337 &ibm_backlight_data);
6338 if (IS_ERR(ibm_backlight_device)) {
6339 int rc = PTR_ERR(ibm_backlight_device);
6340 ibm_backlight_device = NULL;
6341 printk(TPACPI_ERR "Could not register backlight device\n");
6342 return rc;
6344 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6345 "brightness is supported\n");
6347 if (quirks & TPACPI_BRGHT_Q_ASK) {
6348 printk(TPACPI_NOTICE
6349 "brightness: will use unverified default: "
6350 "brightness_mode=%d\n", brightness_mode);
6351 printk(TPACPI_NOTICE
6352 "brightness: please report to %s whether it works well "
6353 "or not on your ThinkPad\n", TPACPI_MAIL);
6356 ibm_backlight_device->props.max_brightness = bright_maxlvl;
6357 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6358 backlight_update_status(ibm_backlight_device);
6360 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6361 "brightness: registering brightness hotkeys "
6362 "as change notification\n");
6363 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6364 | TP_ACPI_HKEY_BRGHTUP_MASK
6365 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6366 return 0;
6369 static void brightness_suspend(pm_message_t state)
6371 tpacpi_brightness_checkpoint_nvram();
6374 static void brightness_shutdown(void)
6376 tpacpi_brightness_checkpoint_nvram();
6379 static void brightness_exit(void)
6381 if (ibm_backlight_device) {
6382 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6383 "calling backlight_device_unregister()\n");
6384 backlight_device_unregister(ibm_backlight_device);
6387 tpacpi_brightness_checkpoint_nvram();
6390 static int brightness_read(struct seq_file *m)
6392 int level;
6394 level = brightness_get(NULL);
6395 if (level < 0) {
6396 seq_printf(m, "level:\t\tunreadable\n");
6397 } else {
6398 seq_printf(m, "level:\t\t%d\n", level);
6399 seq_printf(m, "commands:\tup, down\n");
6400 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6401 bright_maxlvl);
6404 return 0;
6407 static int brightness_write(char *buf)
6409 int level;
6410 int rc;
6411 char *cmd;
6413 level = brightness_get(NULL);
6414 if (level < 0)
6415 return level;
6417 while ((cmd = next_cmd(&buf))) {
6418 if (strlencmp(cmd, "up") == 0) {
6419 if (level < bright_maxlvl)
6420 level++;
6421 } else if (strlencmp(cmd, "down") == 0) {
6422 if (level > 0)
6423 level--;
6424 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6425 level >= 0 && level <= bright_maxlvl) {
6426 /* new level set */
6427 } else
6428 return -EINVAL;
6431 tpacpi_disclose_usertask("procfs brightness",
6432 "set level to %d\n", level);
6435 * Now we know what the final level should be, so we try to set it.
6436 * Doing it this way makes the syscall restartable in case of EINTR
6438 rc = brightness_set(level);
6439 if (!rc && ibm_backlight_device)
6440 backlight_force_update(ibm_backlight_device,
6441 BACKLIGHT_UPDATE_SYSFS);
6442 return (rc == -EINTR)? -ERESTARTSYS : rc;
6445 static struct ibm_struct brightness_driver_data = {
6446 .name = "brightness",
6447 .read = brightness_read,
6448 .write = brightness_write,
6449 .exit = brightness_exit,
6450 .suspend = brightness_suspend,
6451 .shutdown = brightness_shutdown,
6454 /*************************************************************************
6455 * Volume subdriver
6459 * IBM ThinkPads have a simple volume controller with MUTE gating.
6460 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6462 * Since the *61 series (and probably also the later *60 series), Lenovo
6463 * ThinkPads only implement the MUTE gate.
6465 * EC register 0x30
6466 * Bit 6: MUTE (1 mutes sound)
6467 * Bit 3-0: Volume
6468 * Other bits should be zero as far as we know.
6470 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6471 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6472 * such as bit 7 which is used to detect repeated presses of MUTE,
6473 * and we leave them unchanged.
6476 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6478 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6479 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6480 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6482 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6483 static char *alsa_id = "ThinkPadEC";
6484 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6486 struct tpacpi_alsa_data {
6487 struct snd_card *card;
6488 struct snd_ctl_elem_id *ctl_mute_id;
6489 struct snd_ctl_elem_id *ctl_vol_id;
6492 static struct snd_card *alsa_card;
6494 enum {
6495 TP_EC_AUDIO = 0x30,
6497 /* TP_EC_AUDIO bits */
6498 TP_EC_AUDIO_MUTESW = 6,
6500 /* TP_EC_AUDIO bitmasks */
6501 TP_EC_AUDIO_LVL_MSK = 0x0F,
6502 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6504 /* Maximum volume */
6505 TP_EC_VOLUME_MAX = 14,
6508 enum tpacpi_volume_access_mode {
6509 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6510 TPACPI_VOL_MODE_EC, /* Pure EC control */
6511 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6512 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6513 TPACPI_VOL_MODE_MAX
6516 enum tpacpi_volume_capabilities {
6517 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6518 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6519 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6520 TPACPI_VOL_CAP_MAX
6523 static enum tpacpi_volume_access_mode volume_mode =
6524 TPACPI_VOL_MODE_MAX;
6526 static enum tpacpi_volume_capabilities volume_capabilities;
6527 static int volume_control_allowed;
6530 * Used to syncronize writers to TP_EC_AUDIO and
6531 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6533 static struct mutex volume_mutex;
6535 static void tpacpi_volume_checkpoint_nvram(void)
6537 u8 lec = 0;
6538 u8 b_nvram;
6539 u8 ec_mask;
6541 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6542 return;
6543 if (!volume_control_allowed)
6544 return;
6546 vdbg_printk(TPACPI_DBG_MIXER,
6547 "trying to checkpoint mixer state to NVRAM...\n");
6549 if (tp_features.mixer_no_level_control)
6550 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6551 else
6552 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6554 if (mutex_lock_killable(&volume_mutex) < 0)
6555 return;
6557 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6558 goto unlock;
6559 lec &= ec_mask;
6560 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6562 if (lec != (b_nvram & ec_mask)) {
6563 /* NVRAM needs update */
6564 b_nvram &= ~ec_mask;
6565 b_nvram |= lec;
6566 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6567 dbg_printk(TPACPI_DBG_MIXER,
6568 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6569 (unsigned int) lec, (unsigned int) b_nvram);
6570 } else {
6571 vdbg_printk(TPACPI_DBG_MIXER,
6572 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6573 (unsigned int) lec, (unsigned int) b_nvram);
6576 unlock:
6577 mutex_unlock(&volume_mutex);
6580 static int volume_get_status_ec(u8 *status)
6582 u8 s;
6584 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6585 return -EIO;
6587 *status = s;
6589 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6591 return 0;
6594 static int volume_get_status(u8 *status)
6596 return volume_get_status_ec(status);
6599 static int volume_set_status_ec(const u8 status)
6601 if (!acpi_ec_write(TP_EC_AUDIO, status))
6602 return -EIO;
6604 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6606 return 0;
6609 static int volume_set_status(const u8 status)
6611 return volume_set_status_ec(status);
6614 /* returns < 0 on error, 0 on no change, 1 on change */
6615 static int __volume_set_mute_ec(const bool mute)
6617 int rc;
6618 u8 s, n;
6620 if (mutex_lock_killable(&volume_mutex) < 0)
6621 return -EINTR;
6623 rc = volume_get_status_ec(&s);
6624 if (rc)
6625 goto unlock;
6627 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6628 s & ~TP_EC_AUDIO_MUTESW_MSK;
6630 if (n != s) {
6631 rc = volume_set_status_ec(n);
6632 if (!rc)
6633 rc = 1;
6636 unlock:
6637 mutex_unlock(&volume_mutex);
6638 return rc;
6641 static int volume_alsa_set_mute(const bool mute)
6643 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6644 (mute) ? "" : "un");
6645 return __volume_set_mute_ec(mute);
6648 static int volume_set_mute(const bool mute)
6650 int rc;
6652 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6653 (mute) ? "" : "un");
6655 rc = __volume_set_mute_ec(mute);
6656 return (rc < 0) ? rc : 0;
6659 /* returns < 0 on error, 0 on no change, 1 on change */
6660 static int __volume_set_volume_ec(const u8 vol)
6662 int rc;
6663 u8 s, n;
6665 if (vol > TP_EC_VOLUME_MAX)
6666 return -EINVAL;
6668 if (mutex_lock_killable(&volume_mutex) < 0)
6669 return -EINTR;
6671 rc = volume_get_status_ec(&s);
6672 if (rc)
6673 goto unlock;
6675 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6677 if (n != s) {
6678 rc = volume_set_status_ec(n);
6679 if (!rc)
6680 rc = 1;
6683 unlock:
6684 mutex_unlock(&volume_mutex);
6685 return rc;
6688 static int volume_alsa_set_volume(const u8 vol)
6690 dbg_printk(TPACPI_DBG_MIXER,
6691 "ALSA: trying to set volume level to %hu\n", vol);
6692 return __volume_set_volume_ec(vol);
6695 static void volume_alsa_notify_change(void)
6697 struct tpacpi_alsa_data *d;
6699 if (alsa_card && alsa_card->private_data) {
6700 d = alsa_card->private_data;
6701 if (d->ctl_mute_id)
6702 snd_ctl_notify(alsa_card,
6703 SNDRV_CTL_EVENT_MASK_VALUE,
6704 d->ctl_mute_id);
6705 if (d->ctl_vol_id)
6706 snd_ctl_notify(alsa_card,
6707 SNDRV_CTL_EVENT_MASK_VALUE,
6708 d->ctl_vol_id);
6712 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6713 struct snd_ctl_elem_info *uinfo)
6715 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6716 uinfo->count = 1;
6717 uinfo->value.integer.min = 0;
6718 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6719 return 0;
6722 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6723 struct snd_ctl_elem_value *ucontrol)
6725 u8 s;
6726 int rc;
6728 rc = volume_get_status(&s);
6729 if (rc < 0)
6730 return rc;
6732 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6733 return 0;
6736 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6737 struct snd_ctl_elem_value *ucontrol)
6739 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6742 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6744 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6745 struct snd_ctl_elem_value *ucontrol)
6747 u8 s;
6748 int rc;
6750 rc = volume_get_status(&s);
6751 if (rc < 0)
6752 return rc;
6754 ucontrol->value.integer.value[0] =
6755 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6756 return 0;
6759 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6760 struct snd_ctl_elem_value *ucontrol)
6762 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6765 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6766 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6767 .name = "Console Playback Volume",
6768 .index = 0,
6769 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6770 .info = volume_alsa_vol_info,
6771 .get = volume_alsa_vol_get,
6774 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6775 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6776 .name = "Console Playback Switch",
6777 .index = 0,
6778 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6779 .info = volume_alsa_mute_info,
6780 .get = volume_alsa_mute_get,
6783 static void volume_suspend(pm_message_t state)
6785 tpacpi_volume_checkpoint_nvram();
6788 static void volume_resume(void)
6790 volume_alsa_notify_change();
6793 static void volume_shutdown(void)
6795 tpacpi_volume_checkpoint_nvram();
6798 static void volume_exit(void)
6800 if (alsa_card) {
6801 snd_card_free(alsa_card);
6802 alsa_card = NULL;
6805 tpacpi_volume_checkpoint_nvram();
6808 static int __init volume_create_alsa_mixer(void)
6810 struct snd_card *card;
6811 struct tpacpi_alsa_data *data;
6812 struct snd_kcontrol *ctl_vol;
6813 struct snd_kcontrol *ctl_mute;
6814 int rc;
6816 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6817 sizeof(struct tpacpi_alsa_data), &card);
6818 if (rc < 0 || !card) {
6819 printk(TPACPI_ERR
6820 "Failed to create ALSA card structures: %d\n", rc);
6821 return 1;
6824 BUG_ON(!card->private_data);
6825 data = card->private_data;
6826 data->card = card;
6828 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6829 sizeof(card->driver));
6830 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6831 sizeof(card->shortname));
6832 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6833 (thinkpad_id.ec_version_str) ?
6834 thinkpad_id.ec_version_str : "(unknown)");
6835 snprintf(card->longname, sizeof(card->longname),
6836 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6837 (thinkpad_id.ec_version_str) ?
6838 thinkpad_id.ec_version_str : "unknown");
6840 if (volume_control_allowed) {
6841 volume_alsa_control_vol.put = volume_alsa_vol_put;
6842 volume_alsa_control_vol.access =
6843 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6845 volume_alsa_control_mute.put = volume_alsa_mute_put;
6846 volume_alsa_control_mute.access =
6847 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6850 if (!tp_features.mixer_no_level_control) {
6851 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6852 rc = snd_ctl_add(card, ctl_vol);
6853 if (rc < 0) {
6854 printk(TPACPI_ERR
6855 "Failed to create ALSA volume control: %d\n",
6856 rc);
6857 goto err_exit;
6859 data->ctl_vol_id = &ctl_vol->id;
6862 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6863 rc = snd_ctl_add(card, ctl_mute);
6864 if (rc < 0) {
6865 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6866 rc);
6867 goto err_exit;
6869 data->ctl_mute_id = &ctl_mute->id;
6871 snd_card_set_dev(card, &tpacpi_pdev->dev);
6872 rc = snd_card_register(card);
6873 if (rc < 0) {
6874 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6875 goto err_exit;
6878 alsa_card = card;
6879 return 0;
6881 err_exit:
6882 snd_card_free(card);
6883 return 1;
6886 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6887 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6889 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6890 /* Whitelist volume level on all IBM by default */
6891 { .vendor = PCI_VENDOR_ID_IBM,
6892 .bios = TPACPI_MATCH_ANY,
6893 .ec = TPACPI_MATCH_ANY,
6894 .quirks = TPACPI_VOL_Q_LEVEL },
6896 /* Lenovo models with volume control (needs confirmation) */
6897 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6898 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6899 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6900 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6901 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6902 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6903 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6905 /* Whitelist mute-only on all Lenovo by default */
6906 { .vendor = PCI_VENDOR_ID_LENOVO,
6907 .bios = TPACPI_MATCH_ANY,
6908 .ec = TPACPI_MATCH_ANY,
6909 .quirks = TPACPI_VOL_Q_MUTEONLY }
6912 static int __init volume_init(struct ibm_init_struct *iibm)
6914 unsigned long quirks;
6915 int rc;
6917 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6919 mutex_init(&volume_mutex);
6922 * Check for module parameter bogosity, note that we
6923 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6924 * able to detect "unspecified"
6926 if (volume_mode > TPACPI_VOL_MODE_MAX)
6927 return -EINVAL;
6929 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6930 printk(TPACPI_ERR
6931 "UCMS step volume mode not implemented, "
6932 "please contact %s\n", TPACPI_MAIL);
6933 return 1;
6936 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6937 return -EINVAL;
6940 * The ALSA mixer is our primary interface.
6941 * When disabled, don't install the subdriver at all
6943 if (!alsa_enable) {
6944 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6945 "ALSA mixer disabled by parameter, "
6946 "not loading volume subdriver...\n");
6947 return 1;
6950 quirks = tpacpi_check_quirks(volume_quirk_table,
6951 ARRAY_SIZE(volume_quirk_table));
6953 switch (volume_capabilities) {
6954 case TPACPI_VOL_CAP_AUTO:
6955 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6956 tp_features.mixer_no_level_control = 1;
6957 else if (quirks & TPACPI_VOL_Q_LEVEL)
6958 tp_features.mixer_no_level_control = 0;
6959 else
6960 return 1; /* no mixer */
6961 break;
6962 case TPACPI_VOL_CAP_VOLMUTE:
6963 tp_features.mixer_no_level_control = 0;
6964 break;
6965 case TPACPI_VOL_CAP_MUTEONLY:
6966 tp_features.mixer_no_level_control = 1;
6967 break;
6968 default:
6969 return 1;
6972 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6973 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6974 "using user-supplied volume_capabilities=%d\n",
6975 volume_capabilities);
6977 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6978 volume_mode == TPACPI_VOL_MODE_MAX) {
6979 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6981 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6982 "driver auto-selected volume_mode=%d\n",
6983 volume_mode);
6984 } else {
6985 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6986 "using user-supplied volume_mode=%d\n",
6987 volume_mode);
6990 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6991 "mute is supported, volume control is %s\n",
6992 str_supported(!tp_features.mixer_no_level_control));
6994 rc = volume_create_alsa_mixer();
6995 if (rc) {
6996 printk(TPACPI_ERR
6997 "Could not create the ALSA mixer interface\n");
6998 return rc;
7001 printk(TPACPI_INFO
7002 "Console audio control enabled, mode: %s\n",
7003 (volume_control_allowed) ?
7004 "override (read/write)" :
7005 "monitor (read only)");
7007 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7008 "registering volume hotkeys as change notification\n");
7009 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7010 | TP_ACPI_HKEY_VOLUP_MASK
7011 | TP_ACPI_HKEY_VOLDWN_MASK
7012 | TP_ACPI_HKEY_MUTE_MASK);
7014 return 0;
7017 static int volume_read(struct seq_file *m)
7019 u8 status;
7021 if (volume_get_status(&status) < 0) {
7022 seq_printf(m, "level:\t\tunreadable\n");
7023 } else {
7024 if (tp_features.mixer_no_level_control)
7025 seq_printf(m, "level:\t\tunsupported\n");
7026 else
7027 seq_printf(m, "level:\t\t%d\n",
7028 status & TP_EC_AUDIO_LVL_MSK);
7030 seq_printf(m, "mute:\t\t%s\n",
7031 onoff(status, TP_EC_AUDIO_MUTESW));
7033 if (volume_control_allowed) {
7034 seq_printf(m, "commands:\tunmute, mute\n");
7035 if (!tp_features.mixer_no_level_control) {
7036 seq_printf(m,
7037 "commands:\tup, down\n");
7038 seq_printf(m,
7039 "commands:\tlevel <level>"
7040 " (<level> is 0-%d)\n",
7041 TP_EC_VOLUME_MAX);
7046 return 0;
7049 static int volume_write(char *buf)
7051 u8 s;
7052 u8 new_level, new_mute;
7053 int l;
7054 char *cmd;
7055 int rc;
7058 * We do allow volume control at driver startup, so that the
7059 * user can set initial state through the volume=... parameter hack.
7061 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7062 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7063 tp_warned.volume_ctrl_forbidden = 1;
7064 printk(TPACPI_NOTICE
7065 "Console audio control in monitor mode, "
7066 "changes are not allowed.\n");
7067 printk(TPACPI_NOTICE
7068 "Use the volume_control=1 module parameter "
7069 "to enable volume control\n");
7071 return -EPERM;
7074 rc = volume_get_status(&s);
7075 if (rc < 0)
7076 return rc;
7078 new_level = s & TP_EC_AUDIO_LVL_MSK;
7079 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7081 while ((cmd = next_cmd(&buf))) {
7082 if (!tp_features.mixer_no_level_control) {
7083 if (strlencmp(cmd, "up") == 0) {
7084 if (new_mute)
7085 new_mute = 0;
7086 else if (new_level < TP_EC_VOLUME_MAX)
7087 new_level++;
7088 continue;
7089 } else if (strlencmp(cmd, "down") == 0) {
7090 if (new_mute)
7091 new_mute = 0;
7092 else if (new_level > 0)
7093 new_level--;
7094 continue;
7095 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7096 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7097 new_level = l;
7098 continue;
7101 if (strlencmp(cmd, "mute") == 0)
7102 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7103 else if (strlencmp(cmd, "unmute") == 0)
7104 new_mute = 0;
7105 else
7106 return -EINVAL;
7109 if (tp_features.mixer_no_level_control) {
7110 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7111 new_mute ? "" : "un");
7112 rc = volume_set_mute(!!new_mute);
7113 } else {
7114 tpacpi_disclose_usertask("procfs volume",
7115 "%smute and set level to %d\n",
7116 new_mute ? "" : "un", new_level);
7117 rc = volume_set_status(new_mute | new_level);
7119 volume_alsa_notify_change();
7121 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7124 static struct ibm_struct volume_driver_data = {
7125 .name = "volume",
7126 .read = volume_read,
7127 .write = volume_write,
7128 .exit = volume_exit,
7129 .suspend = volume_suspend,
7130 .resume = volume_resume,
7131 .shutdown = volume_shutdown,
7134 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7136 #define alsa_card NULL
7138 static void inline volume_alsa_notify_change(void)
7142 static int __init volume_init(struct ibm_init_struct *iibm)
7144 printk(TPACPI_INFO
7145 "volume: disabled as there is no ALSA support in this kernel\n");
7147 return 1;
7150 static struct ibm_struct volume_driver_data = {
7151 .name = "volume",
7154 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7156 /*************************************************************************
7157 * Fan subdriver
7161 * FAN ACCESS MODES
7163 * TPACPI_FAN_RD_ACPI_GFAN:
7164 * ACPI GFAN method: returns fan level
7166 * see TPACPI_FAN_WR_ACPI_SFAN
7167 * EC 0x2f (HFSP) not available if GFAN exists
7169 * TPACPI_FAN_WR_ACPI_SFAN:
7170 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7172 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7173 * it for writing.
7175 * TPACPI_FAN_WR_TPEC:
7176 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7177 * Supported on almost all ThinkPads
7179 * Fan speed changes of any sort (including those caused by the
7180 * disengaged mode) are usually done slowly by the firmware as the
7181 * maximum amount of fan duty cycle change per second seems to be
7182 * limited.
7184 * Reading is not available if GFAN exists.
7185 * Writing is not available if SFAN exists.
7187 * Bits
7188 * 7 automatic mode engaged;
7189 * (default operation mode of the ThinkPad)
7190 * fan level is ignored in this mode.
7191 * 6 full speed mode (takes precedence over bit 7);
7192 * not available on all thinkpads. May disable
7193 * the tachometer while the fan controller ramps up
7194 * the speed (which can take up to a few *minutes*).
7195 * Speeds up fan to 100% duty-cycle, which is far above
7196 * the standard RPM levels. It is not impossible that
7197 * it could cause hardware damage.
7198 * 5-3 unused in some models. Extra bits for fan level
7199 * in others, but still useless as all values above
7200 * 7 map to the same speed as level 7 in these models.
7201 * 2-0 fan level (0..7 usually)
7202 * 0x00 = stop
7203 * 0x07 = max (set when temperatures critical)
7204 * Some ThinkPads may have other levels, see
7205 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7207 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7208 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7209 * does so, its initial value is meaningless (0x07).
7211 * For firmware bugs, refer to:
7212 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7214 * ----
7216 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7217 * Main fan tachometer reading (in RPM)
7219 * This register is present on all ThinkPads with a new-style EC, and
7220 * it is known not to be present on the A21m/e, and T22, as there is
7221 * something else in offset 0x84 according to the ACPI DSDT. Other
7222 * ThinkPads from this same time period (and earlier) probably lack the
7223 * tachometer as well.
7225 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7226 * was never fixed by IBM to report the EC firmware version string
7227 * probably support the tachometer (like the early X models), so
7228 * detecting it is quite hard. We need more data to know for sure.
7230 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7231 * might result.
7233 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7234 * mode.
7236 * For firmware bugs, refer to:
7237 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7239 * ----
7241 * ThinkPad EC register 0x31 bit 0 (only on select models)
7243 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7244 * show the speed of the main fan. When bit 0 of EC register 0x31
7245 * is one, the tachometer registers show the speed of the auxiliary
7246 * fan.
7248 * Fan control seems to affect both fans, regardless of the state
7249 * of this bit.
7251 * So far, only the firmware for the X60/X61 non-tablet versions
7252 * seem to support this (firmware TP-7M).
7254 * TPACPI_FAN_WR_ACPI_FANS:
7255 * ThinkPad X31, X40, X41. Not available in the X60.
7257 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7258 * high speed. ACPI DSDT seems to map these three speeds to levels
7259 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7260 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7262 * The speeds are stored on handles
7263 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7265 * There are three default speed sets, accessible as handles:
7266 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7268 * ACPI DSDT switches which set is in use depending on various
7269 * factors.
7271 * TPACPI_FAN_WR_TPEC is also available and should be used to
7272 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7273 * but the ACPI tables just mention level 7.
7276 enum { /* Fan control constants */
7277 fan_status_offset = 0x2f, /* EC register 0x2f */
7278 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7279 * 0x84 must be read before 0x85 */
7280 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7281 bit 0 selects which fan is active */
7283 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7284 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7286 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7289 enum fan_status_access_mode {
7290 TPACPI_FAN_NONE = 0, /* No fan status or control */
7291 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7292 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7295 enum fan_control_access_mode {
7296 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7297 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7298 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7299 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7302 enum fan_control_commands {
7303 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7304 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7305 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7306 * and also watchdog cmd */
7309 static int fan_control_allowed;
7311 static enum fan_status_access_mode fan_status_access_mode;
7312 static enum fan_control_access_mode fan_control_access_mode;
7313 static enum fan_control_commands fan_control_commands;
7315 static u8 fan_control_initial_status;
7316 static u8 fan_control_desired_level;
7317 static u8 fan_control_resume_level;
7318 static int fan_watchdog_maxinterval;
7320 static struct mutex fan_mutex;
7322 static void fan_watchdog_fire(struct work_struct *ignored);
7323 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7325 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7326 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7327 "\\FSPD", /* 600e/x, 770e, 770x */
7328 ); /* all others */
7329 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7330 "JFNS", /* 770x-JL */
7331 ); /* all others */
7334 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7335 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7336 * be in auto mode (0x80).
7338 * This is corrected by any write to HFSP either by the driver, or
7339 * by the firmware.
7341 * We assume 0x07 really means auto mode while this quirk is active,
7342 * as this is far more likely than the ThinkPad being in level 7,
7343 * which is only used by the firmware during thermal emergencies.
7345 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7346 * TP-70 (T43, R52), which are known to be buggy.
7349 static void fan_quirk1_setup(void)
7351 if (fan_control_initial_status == 0x07) {
7352 printk(TPACPI_NOTICE
7353 "fan_init: initial fan status is unknown, "
7354 "assuming it is in auto mode\n");
7355 tp_features.fan_ctrl_status_undef = 1;
7359 static void fan_quirk1_handle(u8 *fan_status)
7361 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7362 if (*fan_status != fan_control_initial_status) {
7363 /* something changed the HFSP regisnter since
7364 * driver init time, so it is not undefined
7365 * anymore */
7366 tp_features.fan_ctrl_status_undef = 0;
7367 } else {
7368 /* Return most likely status. In fact, it
7369 * might be the only possible status */
7370 *fan_status = TP_EC_FAN_AUTO;
7375 /* Select main fan on X60/X61, NOOP on others */
7376 static bool fan_select_fan1(void)
7378 if (tp_features.second_fan) {
7379 u8 val;
7381 if (ec_read(fan_select_offset, &val) < 0)
7382 return false;
7383 val &= 0xFEU;
7384 if (ec_write(fan_select_offset, val) < 0)
7385 return false;
7387 return true;
7390 /* Select secondary fan on X60/X61 */
7391 static bool fan_select_fan2(void)
7393 u8 val;
7395 if (!tp_features.second_fan)
7396 return false;
7398 if (ec_read(fan_select_offset, &val) < 0)
7399 return false;
7400 val |= 0x01U;
7401 if (ec_write(fan_select_offset, val) < 0)
7402 return false;
7404 return true;
7408 * Call with fan_mutex held
7410 static void fan_update_desired_level(u8 status)
7412 if ((status &
7413 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7414 if (status > 7)
7415 fan_control_desired_level = 7;
7416 else
7417 fan_control_desired_level = status;
7421 static int fan_get_status(u8 *status)
7423 u8 s;
7425 /* TODO:
7426 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7428 switch (fan_status_access_mode) {
7429 case TPACPI_FAN_RD_ACPI_GFAN:
7430 /* 570, 600e/x, 770e, 770x */
7432 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7433 return -EIO;
7435 if (likely(status))
7436 *status = s & 0x07;
7438 break;
7440 case TPACPI_FAN_RD_TPEC:
7441 /* all except 570, 600e/x, 770e, 770x */
7442 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7443 return -EIO;
7445 if (likely(status)) {
7446 *status = s;
7447 fan_quirk1_handle(status);
7450 break;
7452 default:
7453 return -ENXIO;
7456 return 0;
7459 static int fan_get_status_safe(u8 *status)
7461 int rc;
7462 u8 s;
7464 if (mutex_lock_killable(&fan_mutex))
7465 return -ERESTARTSYS;
7466 rc = fan_get_status(&s);
7467 if (!rc)
7468 fan_update_desired_level(s);
7469 mutex_unlock(&fan_mutex);
7471 if (status)
7472 *status = s;
7474 return rc;
7477 static int fan_get_speed(unsigned int *speed)
7479 u8 hi, lo;
7481 switch (fan_status_access_mode) {
7482 case TPACPI_FAN_RD_TPEC:
7483 /* all except 570, 600e/x, 770e, 770x */
7484 if (unlikely(!fan_select_fan1()))
7485 return -EIO;
7486 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7487 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7488 return -EIO;
7490 if (likely(speed))
7491 *speed = (hi << 8) | lo;
7493 break;
7495 default:
7496 return -ENXIO;
7499 return 0;
7502 static int fan2_get_speed(unsigned int *speed)
7504 u8 hi, lo;
7505 bool rc;
7507 switch (fan_status_access_mode) {
7508 case TPACPI_FAN_RD_TPEC:
7509 /* all except 570, 600e/x, 770e, 770x */
7510 if (unlikely(!fan_select_fan2()))
7511 return -EIO;
7512 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7513 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7514 fan_select_fan1(); /* play it safe */
7515 if (rc)
7516 return -EIO;
7518 if (likely(speed))
7519 *speed = (hi << 8) | lo;
7521 break;
7523 default:
7524 return -ENXIO;
7527 return 0;
7530 static int fan_set_level(int level)
7532 if (!fan_control_allowed)
7533 return -EPERM;
7535 switch (fan_control_access_mode) {
7536 case TPACPI_FAN_WR_ACPI_SFAN:
7537 if (level >= 0 && level <= 7) {
7538 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7539 return -EIO;
7540 } else
7541 return -EINVAL;
7542 break;
7544 case TPACPI_FAN_WR_ACPI_FANS:
7545 case TPACPI_FAN_WR_TPEC:
7546 if (!(level & TP_EC_FAN_AUTO) &&
7547 !(level & TP_EC_FAN_FULLSPEED) &&
7548 ((level < 0) || (level > 7)))
7549 return -EINVAL;
7551 /* safety net should the EC not support AUTO
7552 * or FULLSPEED mode bits and just ignore them */
7553 if (level & TP_EC_FAN_FULLSPEED)
7554 level |= 7; /* safety min speed 7 */
7555 else if (level & TP_EC_FAN_AUTO)
7556 level |= 4; /* safety min speed 4 */
7558 if (!acpi_ec_write(fan_status_offset, level))
7559 return -EIO;
7560 else
7561 tp_features.fan_ctrl_status_undef = 0;
7562 break;
7564 default:
7565 return -ENXIO;
7568 vdbg_printk(TPACPI_DBG_FAN,
7569 "fan control: set fan control register to 0x%02x\n", level);
7570 return 0;
7573 static int fan_set_level_safe(int level)
7575 int rc;
7577 if (!fan_control_allowed)
7578 return -EPERM;
7580 if (mutex_lock_killable(&fan_mutex))
7581 return -ERESTARTSYS;
7583 if (level == TPACPI_FAN_LAST_LEVEL)
7584 level = fan_control_desired_level;
7586 rc = fan_set_level(level);
7587 if (!rc)
7588 fan_update_desired_level(level);
7590 mutex_unlock(&fan_mutex);
7591 return rc;
7594 static int fan_set_enable(void)
7596 u8 s;
7597 int rc;
7599 if (!fan_control_allowed)
7600 return -EPERM;
7602 if (mutex_lock_killable(&fan_mutex))
7603 return -ERESTARTSYS;
7605 switch (fan_control_access_mode) {
7606 case TPACPI_FAN_WR_ACPI_FANS:
7607 case TPACPI_FAN_WR_TPEC:
7608 rc = fan_get_status(&s);
7609 if (rc < 0)
7610 break;
7612 /* Don't go out of emergency fan mode */
7613 if (s != 7) {
7614 s &= 0x07;
7615 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7618 if (!acpi_ec_write(fan_status_offset, s))
7619 rc = -EIO;
7620 else {
7621 tp_features.fan_ctrl_status_undef = 0;
7622 rc = 0;
7624 break;
7626 case TPACPI_FAN_WR_ACPI_SFAN:
7627 rc = fan_get_status(&s);
7628 if (rc < 0)
7629 break;
7631 s &= 0x07;
7633 /* Set fan to at least level 4 */
7634 s |= 4;
7636 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7637 rc = -EIO;
7638 else
7639 rc = 0;
7640 break;
7642 default:
7643 rc = -ENXIO;
7646 mutex_unlock(&fan_mutex);
7648 if (!rc)
7649 vdbg_printk(TPACPI_DBG_FAN,
7650 "fan control: set fan control register to 0x%02x\n",
7652 return rc;
7655 static int fan_set_disable(void)
7657 int rc;
7659 if (!fan_control_allowed)
7660 return -EPERM;
7662 if (mutex_lock_killable(&fan_mutex))
7663 return -ERESTARTSYS;
7665 rc = 0;
7666 switch (fan_control_access_mode) {
7667 case TPACPI_FAN_WR_ACPI_FANS:
7668 case TPACPI_FAN_WR_TPEC:
7669 if (!acpi_ec_write(fan_status_offset, 0x00))
7670 rc = -EIO;
7671 else {
7672 fan_control_desired_level = 0;
7673 tp_features.fan_ctrl_status_undef = 0;
7675 break;
7677 case TPACPI_FAN_WR_ACPI_SFAN:
7678 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7679 rc = -EIO;
7680 else
7681 fan_control_desired_level = 0;
7682 break;
7684 default:
7685 rc = -ENXIO;
7688 if (!rc)
7689 vdbg_printk(TPACPI_DBG_FAN,
7690 "fan control: set fan control register to 0\n");
7692 mutex_unlock(&fan_mutex);
7693 return rc;
7696 static int fan_set_speed(int speed)
7698 int rc;
7700 if (!fan_control_allowed)
7701 return -EPERM;
7703 if (mutex_lock_killable(&fan_mutex))
7704 return -ERESTARTSYS;
7706 rc = 0;
7707 switch (fan_control_access_mode) {
7708 case TPACPI_FAN_WR_ACPI_FANS:
7709 if (speed >= 0 && speed <= 65535) {
7710 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7711 speed, speed, speed))
7712 rc = -EIO;
7713 } else
7714 rc = -EINVAL;
7715 break;
7717 default:
7718 rc = -ENXIO;
7721 mutex_unlock(&fan_mutex);
7722 return rc;
7725 static void fan_watchdog_reset(void)
7727 static int fan_watchdog_active;
7729 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7730 return;
7732 if (fan_watchdog_active)
7733 cancel_delayed_work(&fan_watchdog_task);
7735 if (fan_watchdog_maxinterval > 0 &&
7736 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7737 fan_watchdog_active = 1;
7738 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7739 msecs_to_jiffies(fan_watchdog_maxinterval
7740 * 1000))) {
7741 printk(TPACPI_ERR
7742 "failed to queue the fan watchdog, "
7743 "watchdog will not trigger\n");
7745 } else
7746 fan_watchdog_active = 0;
7749 static void fan_watchdog_fire(struct work_struct *ignored)
7751 int rc;
7753 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7754 return;
7756 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7757 rc = fan_set_enable();
7758 if (rc < 0) {
7759 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7760 "will try again later...\n", -rc);
7761 /* reschedule for later */
7762 fan_watchdog_reset();
7767 * SYSFS fan layout: hwmon compatible (device)
7769 * pwm*_enable:
7770 * 0: "disengaged" mode
7771 * 1: manual mode
7772 * 2: native EC "auto" mode (recommended, hardware default)
7774 * pwm*: set speed in manual mode, ignored otherwise.
7775 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7776 * interpolation.
7778 * fan*_input: tachometer reading, RPM
7781 * SYSFS fan layout: extensions
7783 * fan_watchdog (driver):
7784 * fan watchdog interval in seconds, 0 disables (default), max 120
7787 /* sysfs fan pwm1_enable ----------------------------------------------- */
7788 static ssize_t fan_pwm1_enable_show(struct device *dev,
7789 struct device_attribute *attr,
7790 char *buf)
7792 int res, mode;
7793 u8 status;
7795 res = fan_get_status_safe(&status);
7796 if (res)
7797 return res;
7799 if (status & TP_EC_FAN_FULLSPEED) {
7800 mode = 0;
7801 } else if (status & TP_EC_FAN_AUTO) {
7802 mode = 2;
7803 } else
7804 mode = 1;
7806 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7809 static ssize_t fan_pwm1_enable_store(struct device *dev,
7810 struct device_attribute *attr,
7811 const char *buf, size_t count)
7813 unsigned long t;
7814 int res, level;
7816 if (parse_strtoul(buf, 2, &t))
7817 return -EINVAL;
7819 tpacpi_disclose_usertask("hwmon pwm1_enable",
7820 "set fan mode to %lu\n", t);
7822 switch (t) {
7823 case 0:
7824 level = TP_EC_FAN_FULLSPEED;
7825 break;
7826 case 1:
7827 level = TPACPI_FAN_LAST_LEVEL;
7828 break;
7829 case 2:
7830 level = TP_EC_FAN_AUTO;
7831 break;
7832 case 3:
7833 /* reserved for software-controlled auto mode */
7834 return -ENOSYS;
7835 default:
7836 return -EINVAL;
7839 res = fan_set_level_safe(level);
7840 if (res == -ENXIO)
7841 return -EINVAL;
7842 else if (res < 0)
7843 return res;
7845 fan_watchdog_reset();
7847 return count;
7850 static struct device_attribute dev_attr_fan_pwm1_enable =
7851 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7852 fan_pwm1_enable_show, fan_pwm1_enable_store);
7854 /* sysfs fan pwm1 ------------------------------------------------------ */
7855 static ssize_t fan_pwm1_show(struct device *dev,
7856 struct device_attribute *attr,
7857 char *buf)
7859 int res;
7860 u8 status;
7862 res = fan_get_status_safe(&status);
7863 if (res)
7864 return res;
7866 if ((status &
7867 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7868 status = fan_control_desired_level;
7870 if (status > 7)
7871 status = 7;
7873 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7876 static ssize_t fan_pwm1_store(struct device *dev,
7877 struct device_attribute *attr,
7878 const char *buf, size_t count)
7880 unsigned long s;
7881 int rc;
7882 u8 status, newlevel;
7884 if (parse_strtoul(buf, 255, &s))
7885 return -EINVAL;
7887 tpacpi_disclose_usertask("hwmon pwm1",
7888 "set fan speed to %lu\n", s);
7890 /* scale down from 0-255 to 0-7 */
7891 newlevel = (s >> 5) & 0x07;
7893 if (mutex_lock_killable(&fan_mutex))
7894 return -ERESTARTSYS;
7896 rc = fan_get_status(&status);
7897 if (!rc && (status &
7898 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7899 rc = fan_set_level(newlevel);
7900 if (rc == -ENXIO)
7901 rc = -EINVAL;
7902 else if (!rc) {
7903 fan_update_desired_level(newlevel);
7904 fan_watchdog_reset();
7908 mutex_unlock(&fan_mutex);
7909 return (rc)? rc : count;
7912 static struct device_attribute dev_attr_fan_pwm1 =
7913 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7914 fan_pwm1_show, fan_pwm1_store);
7916 /* sysfs fan fan1_input ------------------------------------------------ */
7917 static ssize_t fan_fan1_input_show(struct device *dev,
7918 struct device_attribute *attr,
7919 char *buf)
7921 int res;
7922 unsigned int speed;
7924 res = fan_get_speed(&speed);
7925 if (res < 0)
7926 return res;
7928 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7931 static struct device_attribute dev_attr_fan_fan1_input =
7932 __ATTR(fan1_input, S_IRUGO,
7933 fan_fan1_input_show, NULL);
7935 /* sysfs fan fan2_input ------------------------------------------------ */
7936 static ssize_t fan_fan2_input_show(struct device *dev,
7937 struct device_attribute *attr,
7938 char *buf)
7940 int res;
7941 unsigned int speed;
7943 res = fan2_get_speed(&speed);
7944 if (res < 0)
7945 return res;
7947 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7950 static struct device_attribute dev_attr_fan_fan2_input =
7951 __ATTR(fan2_input, S_IRUGO,
7952 fan_fan2_input_show, NULL);
7954 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7955 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7956 char *buf)
7958 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7961 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7962 const char *buf, size_t count)
7964 unsigned long t;
7966 if (parse_strtoul(buf, 120, &t))
7967 return -EINVAL;
7969 if (!fan_control_allowed)
7970 return -EPERM;
7972 fan_watchdog_maxinterval = t;
7973 fan_watchdog_reset();
7975 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7977 return count;
7980 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7981 fan_fan_watchdog_show, fan_fan_watchdog_store);
7983 /* --------------------------------------------------------------------- */
7984 static struct attribute *fan_attributes[] = {
7985 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7986 &dev_attr_fan_fan1_input.attr,
7987 NULL, /* for fan2_input */
7988 NULL
7991 static const struct attribute_group fan_attr_group = {
7992 .attrs = fan_attributes,
7995 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7996 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7998 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7999 { .vendor = PCI_VENDOR_ID_IBM, \
8000 .bios = TPACPI_MATCH_ANY, \
8001 .ec = TPID(__id1, __id2), \
8002 .quirks = __quirks }
8004 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8005 { .vendor = PCI_VENDOR_ID_LENOVO, \
8006 .bios = TPACPI_MATCH_ANY, \
8007 .ec = TPID(__id1, __id2), \
8008 .quirks = __quirks }
8010 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8011 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8012 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8013 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8014 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8015 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8018 #undef TPACPI_FAN_QL
8019 #undef TPACPI_FAN_QI
8021 static int __init fan_init(struct ibm_init_struct *iibm)
8023 int rc;
8024 unsigned long quirks;
8026 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8027 "initializing fan subdriver\n");
8029 mutex_init(&fan_mutex);
8030 fan_status_access_mode = TPACPI_FAN_NONE;
8031 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8032 fan_control_commands = 0;
8033 fan_watchdog_maxinterval = 0;
8034 tp_features.fan_ctrl_status_undef = 0;
8035 tp_features.second_fan = 0;
8036 fan_control_desired_level = 7;
8038 if (tpacpi_is_ibm()) {
8039 TPACPI_ACPIHANDLE_INIT(fans);
8040 TPACPI_ACPIHANDLE_INIT(gfan);
8041 TPACPI_ACPIHANDLE_INIT(sfan);
8044 quirks = tpacpi_check_quirks(fan_quirk_table,
8045 ARRAY_SIZE(fan_quirk_table));
8047 if (gfan_handle) {
8048 /* 570, 600e/x, 770e, 770x */
8049 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8050 } else {
8051 /* all other ThinkPads: note that even old-style
8052 * ThinkPad ECs supports the fan control register */
8053 if (likely(acpi_ec_read(fan_status_offset,
8054 &fan_control_initial_status))) {
8055 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8056 if (quirks & TPACPI_FAN_Q1)
8057 fan_quirk1_setup();
8058 if (quirks & TPACPI_FAN_2FAN) {
8059 tp_features.second_fan = 1;
8060 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8061 "secondary fan support enabled\n");
8063 } else {
8064 printk(TPACPI_ERR
8065 "ThinkPad ACPI EC access misbehaving, "
8066 "fan status and control unavailable\n");
8067 return 1;
8071 if (sfan_handle) {
8072 /* 570, 770x-JL */
8073 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8074 fan_control_commands |=
8075 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8076 } else {
8077 if (!gfan_handle) {
8078 /* gfan without sfan means no fan control */
8079 /* all other models implement TP EC 0x2f control */
8081 if (fans_handle) {
8082 /* X31, X40, X41 */
8083 fan_control_access_mode =
8084 TPACPI_FAN_WR_ACPI_FANS;
8085 fan_control_commands |=
8086 TPACPI_FAN_CMD_SPEED |
8087 TPACPI_FAN_CMD_LEVEL |
8088 TPACPI_FAN_CMD_ENABLE;
8089 } else {
8090 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8091 fan_control_commands |=
8092 TPACPI_FAN_CMD_LEVEL |
8093 TPACPI_FAN_CMD_ENABLE;
8098 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8099 "fan is %s, modes %d, %d\n",
8100 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8101 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8102 fan_status_access_mode, fan_control_access_mode);
8104 /* fan control master switch */
8105 if (!fan_control_allowed) {
8106 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8107 fan_control_commands = 0;
8108 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8109 "fan control features disabled by parameter\n");
8112 /* update fan_control_desired_level */
8113 if (fan_status_access_mode != TPACPI_FAN_NONE)
8114 fan_get_status_safe(NULL);
8116 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8117 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8118 if (tp_features.second_fan) {
8119 /* attach second fan tachometer */
8120 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8121 &dev_attr_fan_fan2_input.attr;
8123 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8124 &fan_attr_group);
8125 if (rc < 0)
8126 return rc;
8128 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8129 &driver_attr_fan_watchdog);
8130 if (rc < 0) {
8131 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8132 &fan_attr_group);
8133 return rc;
8135 return 0;
8136 } else
8137 return 1;
8140 static void fan_exit(void)
8142 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8143 "cancelling any pending fan watchdog tasks\n");
8145 /* FIXME: can we really do this unconditionally? */
8146 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8147 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8148 &driver_attr_fan_watchdog);
8150 cancel_delayed_work(&fan_watchdog_task);
8151 flush_workqueue(tpacpi_wq);
8154 static void fan_suspend(pm_message_t state)
8156 int rc;
8158 if (!fan_control_allowed)
8159 return;
8161 /* Store fan status in cache */
8162 fan_control_resume_level = 0;
8163 rc = fan_get_status_safe(&fan_control_resume_level);
8164 if (rc < 0)
8165 printk(TPACPI_NOTICE
8166 "failed to read fan level for later "
8167 "restore during resume: %d\n", rc);
8169 /* if it is undefined, don't attempt to restore it.
8170 * KEEP THIS LAST */
8171 if (tp_features.fan_ctrl_status_undef)
8172 fan_control_resume_level = 0;
8175 static void fan_resume(void)
8177 u8 current_level = 7;
8178 bool do_set = false;
8179 int rc;
8181 /* DSDT *always* updates status on resume */
8182 tp_features.fan_ctrl_status_undef = 0;
8184 if (!fan_control_allowed ||
8185 !fan_control_resume_level ||
8186 (fan_get_status_safe(&current_level) < 0))
8187 return;
8189 switch (fan_control_access_mode) {
8190 case TPACPI_FAN_WR_ACPI_SFAN:
8191 /* never decrease fan level */
8192 do_set = (fan_control_resume_level > current_level);
8193 break;
8194 case TPACPI_FAN_WR_ACPI_FANS:
8195 case TPACPI_FAN_WR_TPEC:
8196 /* never decrease fan level, scale is:
8197 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8199 * We expect the firmware to set either 7 or AUTO, but we
8200 * handle FULLSPEED out of paranoia.
8202 * So, we can safely only restore FULLSPEED or 7, anything
8203 * else could slow the fan. Restoring AUTO is useless, at
8204 * best that's exactly what the DSDT already set (it is the
8205 * slower it uses).
8207 * Always keep in mind that the DSDT *will* have set the
8208 * fans to what the vendor supposes is the best level. We
8209 * muck with it only to speed the fan up.
8211 if (fan_control_resume_level != 7 &&
8212 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8213 return;
8214 else
8215 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8216 (current_level != fan_control_resume_level);
8217 break;
8218 default:
8219 return;
8221 if (do_set) {
8222 printk(TPACPI_NOTICE
8223 "restoring fan level to 0x%02x\n",
8224 fan_control_resume_level);
8225 rc = fan_set_level_safe(fan_control_resume_level);
8226 if (rc < 0)
8227 printk(TPACPI_NOTICE
8228 "failed to restore fan level: %d\n", rc);
8232 static int fan_read(struct seq_file *m)
8234 int rc;
8235 u8 status;
8236 unsigned int speed = 0;
8238 switch (fan_status_access_mode) {
8239 case TPACPI_FAN_RD_ACPI_GFAN:
8240 /* 570, 600e/x, 770e, 770x */
8241 rc = fan_get_status_safe(&status);
8242 if (rc < 0)
8243 return rc;
8245 seq_printf(m, "status:\t\t%s\n"
8246 "level:\t\t%d\n",
8247 (status != 0) ? "enabled" : "disabled", status);
8248 break;
8250 case TPACPI_FAN_RD_TPEC:
8251 /* all except 570, 600e/x, 770e, 770x */
8252 rc = fan_get_status_safe(&status);
8253 if (rc < 0)
8254 return rc;
8256 seq_printf(m, "status:\t\t%s\n",
8257 (status != 0) ? "enabled" : "disabled");
8259 rc = fan_get_speed(&speed);
8260 if (rc < 0)
8261 return rc;
8263 seq_printf(m, "speed:\t\t%d\n", speed);
8265 if (status & TP_EC_FAN_FULLSPEED)
8266 /* Disengaged mode takes precedence */
8267 seq_printf(m, "level:\t\tdisengaged\n");
8268 else if (status & TP_EC_FAN_AUTO)
8269 seq_printf(m, "level:\t\tauto\n");
8270 else
8271 seq_printf(m, "level:\t\t%d\n", status);
8272 break;
8274 case TPACPI_FAN_NONE:
8275 default:
8276 seq_printf(m, "status:\t\tnot supported\n");
8279 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8280 seq_printf(m, "commands:\tlevel <level>");
8282 switch (fan_control_access_mode) {
8283 case TPACPI_FAN_WR_ACPI_SFAN:
8284 seq_printf(m, " (<level> is 0-7)\n");
8285 break;
8287 default:
8288 seq_printf(m, " (<level> is 0-7, "
8289 "auto, disengaged, full-speed)\n");
8290 break;
8294 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8295 seq_printf(m, "commands:\tenable, disable\n"
8296 "commands:\twatchdog <timeout> (<timeout> "
8297 "is 0 (off), 1-120 (seconds))\n");
8299 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8300 seq_printf(m, "commands:\tspeed <speed>"
8301 " (<speed> is 0-65535)\n");
8303 return 0;
8306 static int fan_write_cmd_level(const char *cmd, int *rc)
8308 int level;
8310 if (strlencmp(cmd, "level auto") == 0)
8311 level = TP_EC_FAN_AUTO;
8312 else if ((strlencmp(cmd, "level disengaged") == 0) |
8313 (strlencmp(cmd, "level full-speed") == 0))
8314 level = TP_EC_FAN_FULLSPEED;
8315 else if (sscanf(cmd, "level %d", &level) != 1)
8316 return 0;
8318 *rc = fan_set_level_safe(level);
8319 if (*rc == -ENXIO)
8320 printk(TPACPI_ERR "level command accepted for unsupported "
8321 "access mode %d", fan_control_access_mode);
8322 else if (!*rc)
8323 tpacpi_disclose_usertask("procfs fan",
8324 "set level to %d\n", level);
8326 return 1;
8329 static int fan_write_cmd_enable(const char *cmd, int *rc)
8331 if (strlencmp(cmd, "enable") != 0)
8332 return 0;
8334 *rc = fan_set_enable();
8335 if (*rc == -ENXIO)
8336 printk(TPACPI_ERR "enable command accepted for unsupported "
8337 "access mode %d", fan_control_access_mode);
8338 else if (!*rc)
8339 tpacpi_disclose_usertask("procfs fan", "enable\n");
8341 return 1;
8344 static int fan_write_cmd_disable(const char *cmd, int *rc)
8346 if (strlencmp(cmd, "disable") != 0)
8347 return 0;
8349 *rc = fan_set_disable();
8350 if (*rc == -ENXIO)
8351 printk(TPACPI_ERR "disable command accepted for unsupported "
8352 "access mode %d", fan_control_access_mode);
8353 else if (!*rc)
8354 tpacpi_disclose_usertask("procfs fan", "disable\n");
8356 return 1;
8359 static int fan_write_cmd_speed(const char *cmd, int *rc)
8361 int speed;
8363 /* TODO:
8364 * Support speed <low> <medium> <high> ? */
8366 if (sscanf(cmd, "speed %d", &speed) != 1)
8367 return 0;
8369 *rc = fan_set_speed(speed);
8370 if (*rc == -ENXIO)
8371 printk(TPACPI_ERR "speed command accepted for unsupported "
8372 "access mode %d", fan_control_access_mode);
8373 else if (!*rc)
8374 tpacpi_disclose_usertask("procfs fan",
8375 "set speed to %d\n", speed);
8377 return 1;
8380 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8382 int interval;
8384 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8385 return 0;
8387 if (interval < 0 || interval > 120)
8388 *rc = -EINVAL;
8389 else {
8390 fan_watchdog_maxinterval = interval;
8391 tpacpi_disclose_usertask("procfs fan",
8392 "set watchdog timer to %d\n",
8393 interval);
8396 return 1;
8399 static int fan_write(char *buf)
8401 char *cmd;
8402 int rc = 0;
8404 while (!rc && (cmd = next_cmd(&buf))) {
8405 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8406 fan_write_cmd_level(cmd, &rc)) &&
8407 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8408 (fan_write_cmd_enable(cmd, &rc) ||
8409 fan_write_cmd_disable(cmd, &rc) ||
8410 fan_write_cmd_watchdog(cmd, &rc))) &&
8411 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8412 fan_write_cmd_speed(cmd, &rc))
8414 rc = -EINVAL;
8415 else if (!rc)
8416 fan_watchdog_reset();
8419 return rc;
8422 static struct ibm_struct fan_driver_data = {
8423 .name = "fan",
8424 .read = fan_read,
8425 .write = fan_write,
8426 .exit = fan_exit,
8427 .suspend = fan_suspend,
8428 .resume = fan_resume,
8431 /****************************************************************************
8432 ****************************************************************************
8434 * Infrastructure
8436 ****************************************************************************
8437 ****************************************************************************/
8440 * HKEY event callout for other subdrivers go here
8441 * (yes, it is ugly, but it is quick, safe, and gets the job done
8443 static void tpacpi_driver_event(const unsigned int hkey_event)
8445 if (ibm_backlight_device) {
8446 switch (hkey_event) {
8447 case TP_HKEY_EV_BRGHT_UP:
8448 case TP_HKEY_EV_BRGHT_DOWN:
8449 tpacpi_brightness_notify_change();
8452 if (alsa_card) {
8453 switch (hkey_event) {
8454 case TP_HKEY_EV_VOL_UP:
8455 case TP_HKEY_EV_VOL_DOWN:
8456 case TP_HKEY_EV_VOL_MUTE:
8457 volume_alsa_notify_change();
8462 static void hotkey_driver_event(const unsigned int scancode)
8464 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8467 /* sysfs name ---------------------------------------------------------- */
8468 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8469 struct device_attribute *attr,
8470 char *buf)
8472 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8475 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8476 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8478 /* --------------------------------------------------------------------- */
8480 /* /proc support */
8481 static struct proc_dir_entry *proc_dir;
8484 * Module and infrastructure proble, init and exit handling
8487 static int force_load;
8489 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8490 static const char * __init str_supported(int is_supported)
8492 static char text_unsupported[] __initdata = "not supported";
8494 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8496 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8498 static void ibm_exit(struct ibm_struct *ibm)
8500 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8502 list_del_init(&ibm->all_drivers);
8504 if (ibm->flags.acpi_notify_installed) {
8505 dbg_printk(TPACPI_DBG_EXIT,
8506 "%s: acpi_remove_notify_handler\n", ibm->name);
8507 BUG_ON(!ibm->acpi);
8508 acpi_remove_notify_handler(*ibm->acpi->handle,
8509 ibm->acpi->type,
8510 dispatch_acpi_notify);
8511 ibm->flags.acpi_notify_installed = 0;
8512 ibm->flags.acpi_notify_installed = 0;
8515 if (ibm->flags.proc_created) {
8516 dbg_printk(TPACPI_DBG_EXIT,
8517 "%s: remove_proc_entry\n", ibm->name);
8518 remove_proc_entry(ibm->name, proc_dir);
8519 ibm->flags.proc_created = 0;
8522 if (ibm->flags.acpi_driver_registered) {
8523 dbg_printk(TPACPI_DBG_EXIT,
8524 "%s: acpi_bus_unregister_driver\n", ibm->name);
8525 BUG_ON(!ibm->acpi);
8526 acpi_bus_unregister_driver(ibm->acpi->driver);
8527 kfree(ibm->acpi->driver);
8528 ibm->acpi->driver = NULL;
8529 ibm->flags.acpi_driver_registered = 0;
8532 if (ibm->flags.init_called && ibm->exit) {
8533 ibm->exit();
8534 ibm->flags.init_called = 0;
8537 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8540 static int __init ibm_init(struct ibm_init_struct *iibm)
8542 int ret;
8543 struct ibm_struct *ibm = iibm->data;
8544 struct proc_dir_entry *entry;
8546 BUG_ON(ibm == NULL);
8548 INIT_LIST_HEAD(&ibm->all_drivers);
8550 if (ibm->flags.experimental && !experimental)
8551 return 0;
8553 dbg_printk(TPACPI_DBG_INIT,
8554 "probing for %s\n", ibm->name);
8556 if (iibm->init) {
8557 ret = iibm->init(iibm);
8558 if (ret > 0)
8559 return 0; /* probe failed */
8560 if (ret)
8561 return ret;
8563 ibm->flags.init_called = 1;
8566 if (ibm->acpi) {
8567 if (ibm->acpi->hid) {
8568 ret = register_tpacpi_subdriver(ibm);
8569 if (ret)
8570 goto err_out;
8573 if (ibm->acpi->notify) {
8574 ret = setup_acpi_notify(ibm);
8575 if (ret == -ENODEV) {
8576 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8577 ibm->name);
8578 ret = 0;
8579 goto err_out;
8581 if (ret < 0)
8582 goto err_out;
8586 dbg_printk(TPACPI_DBG_INIT,
8587 "%s installed\n", ibm->name);
8589 if (ibm->read) {
8590 mode_t mode = iibm->base_procfs_mode;
8592 if (!mode)
8593 mode = S_IRUGO;
8594 if (ibm->write)
8595 mode |= S_IWUSR;
8596 entry = proc_create_data(ibm->name, mode, proc_dir,
8597 &dispatch_proc_fops, ibm);
8598 if (!entry) {
8599 printk(TPACPI_ERR "unable to create proc entry %s\n",
8600 ibm->name);
8601 ret = -ENODEV;
8602 goto err_out;
8604 ibm->flags.proc_created = 1;
8607 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8609 return 0;
8611 err_out:
8612 dbg_printk(TPACPI_DBG_INIT,
8613 "%s: at error exit path with result %d\n",
8614 ibm->name, ret);
8616 ibm_exit(ibm);
8617 return (ret < 0)? ret : 0;
8620 /* Probing */
8622 static bool __pure __init tpacpi_is_fw_digit(const char c)
8624 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8627 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8628 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8629 const char t)
8631 return s && strlen(s) >= 8 &&
8632 tpacpi_is_fw_digit(s[0]) &&
8633 tpacpi_is_fw_digit(s[1]) &&
8634 s[2] == t && s[3] == 'T' &&
8635 tpacpi_is_fw_digit(s[4]) &&
8636 tpacpi_is_fw_digit(s[5]) &&
8637 s[6] == 'W' && s[7] == 'W';
8640 /* returns 0 - probe ok, or < 0 - probe error.
8641 * Probe ok doesn't mean thinkpad found.
8642 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8643 static int __must_check __init get_thinkpad_model_data(
8644 struct thinkpad_id_data *tp)
8646 const struct dmi_device *dev = NULL;
8647 char ec_fw_string[18];
8648 char const *s;
8650 if (!tp)
8651 return -EINVAL;
8653 memset(tp, 0, sizeof(*tp));
8655 if (dmi_name_in_vendors("IBM"))
8656 tp->vendor = PCI_VENDOR_ID_IBM;
8657 else if (dmi_name_in_vendors("LENOVO"))
8658 tp->vendor = PCI_VENDOR_ID_LENOVO;
8659 else
8660 return 0;
8662 s = dmi_get_system_info(DMI_BIOS_VERSION);
8663 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8664 if (s && !tp->bios_version_str)
8665 return -ENOMEM;
8667 /* Really ancient ThinkPad 240X will fail this, which is fine */
8668 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8669 return 0;
8671 tp->bios_model = tp->bios_version_str[0]
8672 | (tp->bios_version_str[1] << 8);
8673 tp->bios_release = (tp->bios_version_str[4] << 8)
8674 | tp->bios_version_str[5];
8677 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8678 * X32 or newer, all Z series; Some models must have an
8679 * up-to-date BIOS or they will not be detected.
8681 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8683 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8684 if (sscanf(dev->name,
8685 "IBM ThinkPad Embedded Controller -[%17c",
8686 ec_fw_string) == 1) {
8687 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8688 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8690 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8691 if (!tp->ec_version_str)
8692 return -ENOMEM;
8694 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8695 tp->ec_model = ec_fw_string[0]
8696 | (ec_fw_string[1] << 8);
8697 tp->ec_release = (ec_fw_string[4] << 8)
8698 | ec_fw_string[5];
8699 } else {
8700 printk(TPACPI_NOTICE
8701 "ThinkPad firmware release %s "
8702 "doesn't match the known patterns\n",
8703 ec_fw_string);
8704 printk(TPACPI_NOTICE
8705 "please report this to %s\n",
8706 TPACPI_MAIL);
8708 break;
8712 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8713 if (s && !strnicmp(s, "ThinkPad", 8)) {
8714 tp->model_str = kstrdup(s, GFP_KERNEL);
8715 if (!tp->model_str)
8716 return -ENOMEM;
8719 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8720 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8721 if (s && !tp->nummodel_str)
8722 return -ENOMEM;
8724 return 0;
8727 static int __init probe_for_thinkpad(void)
8729 int is_thinkpad;
8731 if (acpi_disabled)
8732 return -ENODEV;
8734 /* It would be dangerous to run the driver in this case */
8735 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8736 return -ENODEV;
8739 * Non-ancient models have better DMI tagging, but very old models
8740 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8742 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8743 (thinkpad_id.ec_model != 0) ||
8744 tpacpi_is_fw_known();
8746 /* ec is required because many other handles are relative to it */
8747 TPACPI_ACPIHANDLE_INIT(ec);
8748 if (!ec_handle) {
8749 if (is_thinkpad)
8750 printk(TPACPI_ERR
8751 "Not yet supported ThinkPad detected!\n");
8752 return -ENODEV;
8755 if (!is_thinkpad && !force_load)
8756 return -ENODEV;
8758 return 0;
8761 static void __init thinkpad_acpi_init_banner(void)
8763 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8764 printk(TPACPI_INFO "%s\n", TPACPI_URL);
8766 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
8767 (thinkpad_id.bios_version_str) ?
8768 thinkpad_id.bios_version_str : "unknown",
8769 (thinkpad_id.ec_version_str) ?
8770 thinkpad_id.ec_version_str : "unknown");
8772 BUG_ON(!thinkpad_id.vendor);
8774 if (thinkpad_id.model_str)
8775 printk(TPACPI_INFO "%s %s, model %s\n",
8776 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8777 "IBM" : ((thinkpad_id.vendor ==
8778 PCI_VENDOR_ID_LENOVO) ?
8779 "Lenovo" : "Unknown vendor"),
8780 thinkpad_id.model_str,
8781 (thinkpad_id.nummodel_str) ?
8782 thinkpad_id.nummodel_str : "unknown");
8785 /* Module init, exit, parameters */
8787 static struct ibm_init_struct ibms_init[] __initdata = {
8789 .data = &thinkpad_acpi_driver_data,
8792 .init = hotkey_init,
8793 .data = &hotkey_driver_data,
8796 .init = bluetooth_init,
8797 .data = &bluetooth_driver_data,
8800 .init = wan_init,
8801 .data = &wan_driver_data,
8804 .init = uwb_init,
8805 .data = &uwb_driver_data,
8807 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8809 .init = video_init,
8810 .base_procfs_mode = S_IRUSR,
8811 .data = &video_driver_data,
8813 #endif
8815 .init = light_init,
8816 .data = &light_driver_data,
8819 .init = cmos_init,
8820 .data = &cmos_driver_data,
8823 .init = led_init,
8824 .data = &led_driver_data,
8827 .init = beep_init,
8828 .data = &beep_driver_data,
8831 .init = thermal_init,
8832 .data = &thermal_driver_data,
8835 .data = &ecdump_driver_data,
8838 .init = brightness_init,
8839 .data = &brightness_driver_data,
8842 .init = volume_init,
8843 .data = &volume_driver_data,
8846 .init = fan_init,
8847 .data = &fan_driver_data,
8851 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8853 unsigned int i;
8854 struct ibm_struct *ibm;
8856 if (!kp || !kp->name || !val)
8857 return -EINVAL;
8859 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8860 ibm = ibms_init[i].data;
8861 WARN_ON(ibm == NULL);
8863 if (!ibm || !ibm->name)
8864 continue;
8866 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8867 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8868 return -ENOSPC;
8869 strcpy(ibms_init[i].param, val);
8870 strcat(ibms_init[i].param, ",");
8871 return 0;
8875 return -EINVAL;
8878 module_param(experimental, int, 0444);
8879 MODULE_PARM_DESC(experimental,
8880 "Enables experimental features when non-zero");
8882 module_param_named(debug, dbg_level, uint, 0);
8883 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8885 module_param(force_load, bool, 0444);
8886 MODULE_PARM_DESC(force_load,
8887 "Attempts to load the driver even on a "
8888 "mis-identified ThinkPad when true");
8890 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8891 MODULE_PARM_DESC(fan_control,
8892 "Enables setting fan parameters features when true");
8894 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8895 MODULE_PARM_DESC(brightness_mode,
8896 "Selects brightness control strategy: "
8897 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8899 module_param(brightness_enable, uint, 0444);
8900 MODULE_PARM_DESC(brightness_enable,
8901 "Enables backlight control when 1, disables when 0");
8903 module_param(hotkey_report_mode, uint, 0444);
8904 MODULE_PARM_DESC(hotkey_report_mode,
8905 "used for backwards compatibility with userspace, "
8906 "see documentation");
8908 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8909 module_param_named(volume_mode, volume_mode, uint, 0444);
8910 MODULE_PARM_DESC(volume_mode,
8911 "Selects volume control strategy: "
8912 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8914 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8915 MODULE_PARM_DESC(volume_capabilities,
8916 "Selects the mixer capabilites: "
8917 "0=auto, 1=volume and mute, 2=mute only");
8919 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8920 MODULE_PARM_DESC(volume_control,
8921 "Enables software override for the console audio "
8922 "control when true");
8924 /* ALSA module API parameters */
8925 module_param_named(index, alsa_index, int, 0444);
8926 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8927 module_param_named(id, alsa_id, charp, 0444);
8928 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8929 module_param_named(enable, alsa_enable, bool, 0444);
8930 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8931 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8933 #define TPACPI_PARAM(feature) \
8934 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8935 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8936 "at module load, see documentation")
8938 TPACPI_PARAM(hotkey);
8939 TPACPI_PARAM(bluetooth);
8940 TPACPI_PARAM(video);
8941 TPACPI_PARAM(light);
8942 TPACPI_PARAM(cmos);
8943 TPACPI_PARAM(led);
8944 TPACPI_PARAM(beep);
8945 TPACPI_PARAM(ecdump);
8946 TPACPI_PARAM(brightness);
8947 TPACPI_PARAM(volume);
8948 TPACPI_PARAM(fan);
8950 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8951 module_param(dbg_wlswemul, uint, 0444);
8952 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8953 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8954 MODULE_PARM_DESC(wlsw_state,
8955 "Initial state of the emulated WLSW switch");
8957 module_param(dbg_bluetoothemul, uint, 0444);
8958 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8959 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8960 MODULE_PARM_DESC(bluetooth_state,
8961 "Initial state of the emulated bluetooth switch");
8963 module_param(dbg_wwanemul, uint, 0444);
8964 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8965 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8966 MODULE_PARM_DESC(wwan_state,
8967 "Initial state of the emulated WWAN switch");
8969 module_param(dbg_uwbemul, uint, 0444);
8970 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8971 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8972 MODULE_PARM_DESC(uwb_state,
8973 "Initial state of the emulated UWB switch");
8974 #endif
8976 static void thinkpad_acpi_module_exit(void)
8978 struct ibm_struct *ibm, *itmp;
8980 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8982 list_for_each_entry_safe_reverse(ibm, itmp,
8983 &tpacpi_all_drivers,
8984 all_drivers) {
8985 ibm_exit(ibm);
8988 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8990 if (tpacpi_inputdev) {
8991 if (tp_features.input_device_registered)
8992 input_unregister_device(tpacpi_inputdev);
8993 else
8994 input_free_device(tpacpi_inputdev);
8997 if (tpacpi_hwmon)
8998 hwmon_device_unregister(tpacpi_hwmon);
9000 if (tp_features.sensors_pdev_attrs_registered)
9001 device_remove_file(&tpacpi_sensors_pdev->dev,
9002 &dev_attr_thinkpad_acpi_pdev_name);
9003 if (tpacpi_sensors_pdev)
9004 platform_device_unregister(tpacpi_sensors_pdev);
9005 if (tpacpi_pdev)
9006 platform_device_unregister(tpacpi_pdev);
9008 if (tp_features.sensors_pdrv_attrs_registered)
9009 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9010 if (tp_features.platform_drv_attrs_registered)
9011 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9013 if (tp_features.sensors_pdrv_registered)
9014 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9016 if (tp_features.platform_drv_registered)
9017 platform_driver_unregister(&tpacpi_pdriver);
9019 if (proc_dir)
9020 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9022 if (tpacpi_wq)
9023 destroy_workqueue(tpacpi_wq);
9025 kfree(thinkpad_id.bios_version_str);
9026 kfree(thinkpad_id.ec_version_str);
9027 kfree(thinkpad_id.model_str);
9031 static int __init thinkpad_acpi_module_init(void)
9033 int ret, i;
9035 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9037 /* Parameter checking */
9038 if (hotkey_report_mode > 2)
9039 return -EINVAL;
9041 /* Driver-level probe */
9043 ret = get_thinkpad_model_data(&thinkpad_id);
9044 if (ret) {
9045 printk(TPACPI_ERR
9046 "unable to get DMI data: %d\n", ret);
9047 thinkpad_acpi_module_exit();
9048 return ret;
9050 ret = probe_for_thinkpad();
9051 if (ret) {
9052 thinkpad_acpi_module_exit();
9053 return ret;
9056 /* Driver initialization */
9058 thinkpad_acpi_init_banner();
9059 tpacpi_check_outdated_fw();
9061 TPACPI_ACPIHANDLE_INIT(ecrd);
9062 TPACPI_ACPIHANDLE_INIT(ecwr);
9064 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9065 if (!tpacpi_wq) {
9066 thinkpad_acpi_module_exit();
9067 return -ENOMEM;
9070 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9071 if (!proc_dir) {
9072 printk(TPACPI_ERR
9073 "unable to create proc dir " TPACPI_PROC_DIR);
9074 thinkpad_acpi_module_exit();
9075 return -ENODEV;
9078 ret = platform_driver_register(&tpacpi_pdriver);
9079 if (ret) {
9080 printk(TPACPI_ERR
9081 "unable to register main platform driver\n");
9082 thinkpad_acpi_module_exit();
9083 return ret;
9085 tp_features.platform_drv_registered = 1;
9087 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9088 if (ret) {
9089 printk(TPACPI_ERR
9090 "unable to register hwmon platform driver\n");
9091 thinkpad_acpi_module_exit();
9092 return ret;
9094 tp_features.sensors_pdrv_registered = 1;
9096 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9097 if (!ret) {
9098 tp_features.platform_drv_attrs_registered = 1;
9099 ret = tpacpi_create_driver_attributes(
9100 &tpacpi_hwmon_pdriver.driver);
9102 if (ret) {
9103 printk(TPACPI_ERR
9104 "unable to create sysfs driver attributes\n");
9105 thinkpad_acpi_module_exit();
9106 return ret;
9108 tp_features.sensors_pdrv_attrs_registered = 1;
9111 /* Device initialization */
9112 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9113 NULL, 0);
9114 if (IS_ERR(tpacpi_pdev)) {
9115 ret = PTR_ERR(tpacpi_pdev);
9116 tpacpi_pdev = NULL;
9117 printk(TPACPI_ERR "unable to register platform device\n");
9118 thinkpad_acpi_module_exit();
9119 return ret;
9121 tpacpi_sensors_pdev = platform_device_register_simple(
9122 TPACPI_HWMON_DRVR_NAME,
9123 -1, NULL, 0);
9124 if (IS_ERR(tpacpi_sensors_pdev)) {
9125 ret = PTR_ERR(tpacpi_sensors_pdev);
9126 tpacpi_sensors_pdev = NULL;
9127 printk(TPACPI_ERR
9128 "unable to register hwmon platform device\n");
9129 thinkpad_acpi_module_exit();
9130 return ret;
9132 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9133 &dev_attr_thinkpad_acpi_pdev_name);
9134 if (ret) {
9135 printk(TPACPI_ERR
9136 "unable to create sysfs hwmon device attributes\n");
9137 thinkpad_acpi_module_exit();
9138 return ret;
9140 tp_features.sensors_pdev_attrs_registered = 1;
9141 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9142 if (IS_ERR(tpacpi_hwmon)) {
9143 ret = PTR_ERR(tpacpi_hwmon);
9144 tpacpi_hwmon = NULL;
9145 printk(TPACPI_ERR "unable to register hwmon device\n");
9146 thinkpad_acpi_module_exit();
9147 return ret;
9149 mutex_init(&tpacpi_inputdev_send_mutex);
9150 tpacpi_inputdev = input_allocate_device();
9151 if (!tpacpi_inputdev) {
9152 printk(TPACPI_ERR "unable to allocate input device\n");
9153 thinkpad_acpi_module_exit();
9154 return -ENOMEM;
9155 } else {
9156 /* Prepare input device, but don't register */
9157 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9158 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9159 tpacpi_inputdev->id.bustype = BUS_HOST;
9160 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9161 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9162 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9163 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9166 /* Init subdriver dependencies */
9167 tpacpi_detect_brightness_capabilities();
9169 /* Init subdrivers */
9170 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9171 ret = ibm_init(&ibms_init[i]);
9172 if (ret >= 0 && *ibms_init[i].param)
9173 ret = ibms_init[i].data->write(ibms_init[i].param);
9174 if (ret < 0) {
9175 thinkpad_acpi_module_exit();
9176 return ret;
9180 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9182 ret = input_register_device(tpacpi_inputdev);
9183 if (ret < 0) {
9184 printk(TPACPI_ERR "unable to register input device\n");
9185 thinkpad_acpi_module_exit();
9186 return ret;
9187 } else {
9188 tp_features.input_device_registered = 1;
9191 return 0;
9194 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9197 * This will autoload the driver in almost every ThinkPad
9198 * in widespread use.
9200 * Only _VERY_ old models, like the 240, 240x and 570 lack
9201 * the HKEY event interface.
9203 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9206 * DMI matching for module autoloading
9208 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9209 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9211 * Only models listed in thinkwiki will be supported, so add yours
9212 * if it is not there yet.
9214 #define IBM_BIOS_MODULE_ALIAS(__type) \
9215 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9217 /* Ancient thinkpad BIOSes have to be identified by
9218 * BIOS type or model number, and there are far less
9219 * BIOS types than model numbers... */
9220 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9222 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9223 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9224 MODULE_DESCRIPTION(TPACPI_DESC);
9225 MODULE_VERSION(TPACPI_VERSION);
9226 MODULE_LICENSE("GPL");
9228 module_init(thinkpad_acpi_module_init);
9229 module_exit(thinkpad_acpi_module_exit);