thinkpad-acpi: disclose usertask for ALSA callbacks
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob5b7d8ac93390552da7e71b5eabdb75543d3ba351
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.24"
25 #define TPACPI_SYSFS_VERSION 0x020700
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/seq_file.h>
65 #include <linux/sysfs.h>
66 #include <linux/backlight.h>
67 #include <linux/fb.h>
68 #include <linux/platform_device.h>
69 #include <linux/hwmon.h>
70 #include <linux/hwmon-sysfs.h>
71 #include <linux/input.h>
72 #include <linux/leds.h>
73 #include <linux/rfkill.h>
74 #include <asm/uaccess.h>
76 #include <linux/dmi.h>
77 #include <linux/jiffies.h>
78 #include <linux/workqueue.h>
80 #include <sound/core.h>
81 #include <sound/control.h>
82 #include <sound/initval.h>
84 #include <acpi/acpi_drivers.h>
86 #include <linux/pci_ids.h>
89 /* ThinkPad CMOS commands */
90 #define TP_CMOS_VOLUME_DOWN 0
91 #define TP_CMOS_VOLUME_UP 1
92 #define TP_CMOS_VOLUME_MUTE 2
93 #define TP_CMOS_BRIGHTNESS_UP 4
94 #define TP_CMOS_BRIGHTNESS_DOWN 5
95 #define TP_CMOS_THINKLIGHT_ON 12
96 #define TP_CMOS_THINKLIGHT_OFF 13
98 /* NVRAM Addresses */
99 enum tp_nvram_addr {
100 TP_NVRAM_ADDR_HK2 = 0x57,
101 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
102 TP_NVRAM_ADDR_VIDEO = 0x59,
103 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
104 TP_NVRAM_ADDR_MIXER = 0x60,
107 /* NVRAM bit masks */
108 enum {
109 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
110 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
111 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
112 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
113 TP_NVRAM_MASK_THINKLIGHT = 0x10,
114 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
115 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
116 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
117 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
118 TP_NVRAM_MASK_MUTE = 0x40,
119 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
120 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
121 TP_NVRAM_POS_LEVEL_VOLUME = 0,
124 /* Misc NVRAM-related */
125 enum {
126 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
129 /* ACPI HIDs */
130 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
131 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
132 #define TPACPI_ACPI_EC_HID "PNP0C09"
134 /* Input IDs */
135 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
136 #define TPACPI_HKEY_INPUT_VERSION 0x4101
138 /* ACPI \WGSV commands */
139 enum {
140 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
141 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
142 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
143 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
146 /* TP_ACPI_WGSV_GET_STATE bits */
147 enum {
148 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
149 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
150 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
151 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
152 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
153 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
154 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
155 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
156 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
157 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
160 /* HKEY events */
161 enum tpacpi_hkey_event_t {
162 /* Hotkey-related */
163 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
164 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
165 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
166 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
167 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
168 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
170 /* Reasons for waking up from S3/S4 */
171 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
172 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
173 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
174 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
175 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
176 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
178 /* Auto-sleep after eject request */
179 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
180 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
182 /* Misc bay events */
183 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
185 /* User-interface events */
186 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
187 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
188 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
189 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
190 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
191 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
192 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
194 /* Thermal events */
195 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
196 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
197 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
198 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
199 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
201 /* Misc */
202 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
205 /****************************************************************************
206 * Main driver
209 #define TPACPI_NAME "thinkpad"
210 #define TPACPI_DESC "ThinkPad ACPI Extras"
211 #define TPACPI_FILE TPACPI_NAME "_acpi"
212 #define TPACPI_URL "http://ibm-acpi.sf.net/"
213 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
215 #define TPACPI_PROC_DIR "ibm"
216 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
217 #define TPACPI_DRVR_NAME TPACPI_FILE
218 #define TPACPI_DRVR_SHORTNAME "tpacpi"
219 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
221 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
222 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
224 #define TPACPI_MAX_ACPI_ARGS 3
226 /* printk headers */
227 #define TPACPI_LOG TPACPI_FILE ": "
228 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
229 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
230 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
231 #define TPACPI_ERR KERN_ERR TPACPI_LOG
232 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
233 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
234 #define TPACPI_INFO KERN_INFO TPACPI_LOG
235 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
237 /* Debugging printk groups */
238 #define TPACPI_DBG_ALL 0xffff
239 #define TPACPI_DBG_DISCLOSETASK 0x8000
240 #define TPACPI_DBG_INIT 0x0001
241 #define TPACPI_DBG_EXIT 0x0002
242 #define TPACPI_DBG_RFKILL 0x0004
243 #define TPACPI_DBG_HKEY 0x0008
244 #define TPACPI_DBG_FAN 0x0010
245 #define TPACPI_DBG_BRGHT 0x0020
246 #define TPACPI_DBG_MIXER 0x0040
248 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
249 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
250 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
253 /****************************************************************************
254 * Driver-wide structs and misc. variables
257 struct ibm_struct;
259 struct tp_acpi_drv_struct {
260 const struct acpi_device_id *hid;
261 struct acpi_driver *driver;
263 void (*notify) (struct ibm_struct *, u32);
264 acpi_handle *handle;
265 u32 type;
266 struct acpi_device *device;
269 struct ibm_struct {
270 char *name;
272 int (*read) (struct seq_file *);
273 int (*write) (char *);
274 void (*exit) (void);
275 void (*resume) (void);
276 void (*suspend) (pm_message_t state);
277 void (*shutdown) (void);
279 struct list_head all_drivers;
281 struct tp_acpi_drv_struct *acpi;
283 struct {
284 u8 acpi_driver_registered:1;
285 u8 acpi_notify_installed:1;
286 u8 proc_created:1;
287 u8 init_called:1;
288 u8 experimental:1;
289 } flags;
292 struct ibm_init_struct {
293 char param[32];
295 int (*init) (struct ibm_init_struct *);
296 mode_t base_procfs_mode;
297 struct ibm_struct *data;
300 static struct {
301 u32 bluetooth:1;
302 u32 hotkey:1;
303 u32 hotkey_mask:1;
304 u32 hotkey_wlsw:1;
305 u32 hotkey_tablet:1;
306 u32 light:1;
307 u32 light_status:1;
308 u32 bright_acpimode:1;
309 u32 bright_unkfw:1;
310 u32 wan:1;
311 u32 uwb:1;
312 u32 fan_ctrl_status_undef:1;
313 u32 second_fan:1;
314 u32 beep_needs_two_args:1;
315 u32 mixer_no_level_control:1;
316 u32 input_device_registered:1;
317 u32 platform_drv_registered:1;
318 u32 platform_drv_attrs_registered:1;
319 u32 sensors_pdrv_registered:1;
320 u32 sensors_pdrv_attrs_registered:1;
321 u32 sensors_pdev_attrs_registered:1;
322 u32 hotkey_poll_active:1;
323 } tp_features;
325 static struct {
326 u16 hotkey_mask_ff:1;
327 u16 volume_ctrl_forbidden:1;
328 } tp_warned;
330 struct thinkpad_id_data {
331 unsigned int vendor; /* ThinkPad vendor:
332 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
334 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
335 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
337 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
338 u16 ec_model;
339 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
340 u16 ec_release;
342 char *model_str; /* ThinkPad T43 */
343 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
345 static struct thinkpad_id_data thinkpad_id;
347 static enum {
348 TPACPI_LIFE_INIT = 0,
349 TPACPI_LIFE_RUNNING,
350 TPACPI_LIFE_EXITING,
351 } tpacpi_lifecycle;
353 static int experimental;
354 static u32 dbg_level;
356 static struct workqueue_struct *tpacpi_wq;
358 enum led_status_t {
359 TPACPI_LED_OFF = 0,
360 TPACPI_LED_ON,
361 TPACPI_LED_BLINK,
364 /* Special LED class that can defer work */
365 struct tpacpi_led_classdev {
366 struct led_classdev led_classdev;
367 struct work_struct work;
368 enum led_status_t new_state;
369 unsigned int led;
372 /* brightness level capabilities */
373 static unsigned int bright_maxlvl; /* 0 = unknown */
375 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
376 static int dbg_wlswemul;
377 static int tpacpi_wlsw_emulstate;
378 static int dbg_bluetoothemul;
379 static int tpacpi_bluetooth_emulstate;
380 static int dbg_wwanemul;
381 static int tpacpi_wwan_emulstate;
382 static int dbg_uwbemul;
383 static int tpacpi_uwb_emulstate;
384 #endif
387 /*************************************************************************
388 * Debugging helpers
391 #define dbg_printk(a_dbg_level, format, arg...) \
392 do { if (dbg_level & (a_dbg_level)) \
393 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
394 } while (0)
396 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
397 #define vdbg_printk dbg_printk
398 static const char *str_supported(int is_supported);
399 #else
400 #define vdbg_printk(a_dbg_level, format, arg...) \
401 do { } while (0)
402 #endif
404 static void tpacpi_log_usertask(const char * const what)
406 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
407 what, task_tgid_vnr(current));
410 #define tpacpi_disclose_usertask(what, format, arg...) \
411 do { \
412 if (unlikely( \
413 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
414 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
415 printk(TPACPI_DEBUG "%s: PID %d: " format, \
416 what, task_tgid_vnr(current), ## arg); \
418 } while (0)
421 * Quirk handling helpers
423 * ThinkPad IDs and versions seen in the field so far
424 * are two-characters from the set [0-9A-Z], i.e. base 36.
426 * We use values well outside that range as specials.
429 #define TPACPI_MATCH_ANY 0xffffU
430 #define TPACPI_MATCH_UNKNOWN 0U
432 /* TPID('1', 'Y') == 0x5931 */
433 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
435 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
436 { .vendor = PCI_VENDOR_ID_IBM, \
437 .bios = TPID(__id1, __id2), \
438 .ec = TPACPI_MATCH_ANY, \
439 .quirks = (__quirk) }
441 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
442 { .vendor = PCI_VENDOR_ID_LENOVO, \
443 .bios = TPID(__id1, __id2), \
444 .ec = TPACPI_MATCH_ANY, \
445 .quirks = (__quirk) }
447 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
448 { .vendor = PCI_VENDOR_ID_LENOVO, \
449 .bios = TPACPI_MATCH_ANY, \
450 .ec = TPID(__id1, __id2), \
451 .quirks = (__quirk) }
453 struct tpacpi_quirk {
454 unsigned int vendor;
455 u16 bios;
456 u16 ec;
457 unsigned long quirks;
461 * tpacpi_check_quirks() - search BIOS/EC version on a list
462 * @qlist: array of &struct tpacpi_quirk
463 * @qlist_size: number of elements in @qlist
465 * Iterates over a quirks list until one is found that matches the
466 * ThinkPad's vendor, BIOS and EC model.
468 * Returns 0 if nothing matches, otherwise returns the quirks field of
469 * the matching &struct tpacpi_quirk entry.
471 * The match criteria is: vendor, ec and bios much match.
473 static unsigned long __init tpacpi_check_quirks(
474 const struct tpacpi_quirk *qlist,
475 unsigned int qlist_size)
477 while (qlist_size) {
478 if ((qlist->vendor == thinkpad_id.vendor ||
479 qlist->vendor == TPACPI_MATCH_ANY) &&
480 (qlist->bios == thinkpad_id.bios_model ||
481 qlist->bios == TPACPI_MATCH_ANY) &&
482 (qlist->ec == thinkpad_id.ec_model ||
483 qlist->ec == TPACPI_MATCH_ANY))
484 return qlist->quirks;
486 qlist_size--;
487 qlist++;
489 return 0;
492 static inline bool __pure __init tpacpi_is_lenovo(void)
494 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
497 static inline bool __pure __init tpacpi_is_ibm(void)
499 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
502 /****************************************************************************
503 ****************************************************************************
505 * ACPI Helpers and device model
507 ****************************************************************************
508 ****************************************************************************/
510 /*************************************************************************
511 * ACPI basic handles
514 static acpi_handle root_handle;
516 #define TPACPI_HANDLE(object, parent, paths...) \
517 static acpi_handle object##_handle; \
518 static acpi_handle *object##_parent = &parent##_handle; \
519 static char *object##_path; \
520 static char *object##_paths[] = { paths }
522 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
523 "\\_SB.PCI.ISA.EC", /* 570 */
524 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
525 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
526 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
527 "\\_SB.PCI0.ICH3.EC0", /* R31 */
528 "\\_SB.PCI0.LPC0.EC", /* X100e and a few others */
529 "\\_SB.PCI0.LPC.EC", /* all others */
532 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
533 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
535 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
536 /* T4x, X31, X40 */
537 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
538 "\\CMS", /* R40, R40e */
539 ); /* all others */
541 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
542 "^HKEY", /* R30, R31 */
543 "HKEY", /* all others */
544 ); /* 570 */
546 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
547 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
548 "\\_SB.PCI0.VID0", /* 770e */
549 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
550 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
551 "\\_SB.PCI0.AGP.VID", /* all others */
552 ); /* R30, R31 */
555 /*************************************************************************
556 * ACPI helpers
559 static int acpi_evalf(acpi_handle handle,
560 void *res, char *method, char *fmt, ...)
562 char *fmt0 = fmt;
563 struct acpi_object_list params;
564 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
565 struct acpi_buffer result, *resultp;
566 union acpi_object out_obj;
567 acpi_status status;
568 va_list ap;
569 char res_type;
570 int success;
571 int quiet;
573 if (!*fmt) {
574 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
575 return 0;
578 if (*fmt == 'q') {
579 quiet = 1;
580 fmt++;
581 } else
582 quiet = 0;
584 res_type = *(fmt++);
586 params.count = 0;
587 params.pointer = &in_objs[0];
589 va_start(ap, fmt);
590 while (*fmt) {
591 char c = *(fmt++);
592 switch (c) {
593 case 'd': /* int */
594 in_objs[params.count].integer.value = va_arg(ap, int);
595 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
596 break;
597 /* add more types as needed */
598 default:
599 printk(TPACPI_ERR "acpi_evalf() called "
600 "with invalid format character '%c'\n", c);
601 return 0;
604 va_end(ap);
606 if (res_type != 'v') {
607 result.length = sizeof(out_obj);
608 result.pointer = &out_obj;
609 resultp = &result;
610 } else
611 resultp = NULL;
613 status = acpi_evaluate_object(handle, method, &params, resultp);
615 switch (res_type) {
616 case 'd': /* int */
617 if (res)
618 *(int *)res = out_obj.integer.value;
619 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
620 break;
621 case 'v': /* void */
622 success = status == AE_OK;
623 break;
624 /* add more types as needed */
625 default:
626 printk(TPACPI_ERR "acpi_evalf() called "
627 "with invalid format character '%c'\n", res_type);
628 return 0;
631 if (!success && !quiet)
632 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
633 method, fmt0, status);
635 return success;
638 static int acpi_ec_read(int i, u8 *p)
640 int v;
642 if (ecrd_handle) {
643 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
644 return 0;
645 *p = v;
646 } else {
647 if (ec_read(i, p) < 0)
648 return 0;
651 return 1;
654 static int acpi_ec_write(int i, u8 v)
656 if (ecwr_handle) {
657 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
658 return 0;
659 } else {
660 if (ec_write(i, v) < 0)
661 return 0;
664 return 1;
667 static int issue_thinkpad_cmos_command(int cmos_cmd)
669 if (!cmos_handle)
670 return -ENXIO;
672 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
673 return -EIO;
675 return 0;
678 /*************************************************************************
679 * ACPI device model
682 #define TPACPI_ACPIHANDLE_INIT(object) \
683 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
684 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
686 static void drv_acpi_handle_init(char *name,
687 acpi_handle *handle, acpi_handle parent,
688 char **paths, int num_paths, char **path)
690 int i;
691 acpi_status status;
693 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
694 name);
696 for (i = 0; i < num_paths; i++) {
697 status = acpi_get_handle(parent, paths[i], handle);
698 if (ACPI_SUCCESS(status)) {
699 *path = paths[i];
700 dbg_printk(TPACPI_DBG_INIT,
701 "Found ACPI handle %s for %s\n",
702 *path, name);
703 return;
707 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
708 name);
709 *handle = NULL;
712 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
714 struct ibm_struct *ibm = data;
716 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
717 return;
719 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
720 return;
722 ibm->acpi->notify(ibm, event);
725 static int __init setup_acpi_notify(struct ibm_struct *ibm)
727 acpi_status status;
728 int rc;
730 BUG_ON(!ibm->acpi);
732 if (!*ibm->acpi->handle)
733 return 0;
735 vdbg_printk(TPACPI_DBG_INIT,
736 "setting up ACPI notify for %s\n", ibm->name);
738 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
739 if (rc < 0) {
740 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
741 ibm->name, rc);
742 return -ENODEV;
745 ibm->acpi->device->driver_data = ibm;
746 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
747 TPACPI_ACPI_EVENT_PREFIX,
748 ibm->name);
750 status = acpi_install_notify_handler(*ibm->acpi->handle,
751 ibm->acpi->type, dispatch_acpi_notify, ibm);
752 if (ACPI_FAILURE(status)) {
753 if (status == AE_ALREADY_EXISTS) {
754 printk(TPACPI_NOTICE
755 "another device driver is already "
756 "handling %s events\n", ibm->name);
757 } else {
758 printk(TPACPI_ERR
759 "acpi_install_notify_handler(%s) failed: %d\n",
760 ibm->name, status);
762 return -ENODEV;
764 ibm->flags.acpi_notify_installed = 1;
765 return 0;
768 static int __init tpacpi_device_add(struct acpi_device *device)
770 return 0;
773 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
775 int rc;
777 dbg_printk(TPACPI_DBG_INIT,
778 "registering %s as an ACPI driver\n", ibm->name);
780 BUG_ON(!ibm->acpi);
782 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
783 if (!ibm->acpi->driver) {
784 printk(TPACPI_ERR
785 "failed to allocate memory for ibm->acpi->driver\n");
786 return -ENOMEM;
789 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
790 ibm->acpi->driver->ids = ibm->acpi->hid;
792 ibm->acpi->driver->ops.add = &tpacpi_device_add;
794 rc = acpi_bus_register_driver(ibm->acpi->driver);
795 if (rc < 0) {
796 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
797 ibm->name, rc);
798 kfree(ibm->acpi->driver);
799 ibm->acpi->driver = NULL;
800 } else if (!rc)
801 ibm->flags.acpi_driver_registered = 1;
803 return rc;
807 /****************************************************************************
808 ****************************************************************************
810 * Procfs Helpers
812 ****************************************************************************
813 ****************************************************************************/
815 static int dispatch_proc_show(struct seq_file *m, void *v)
817 struct ibm_struct *ibm = m->private;
819 if (!ibm || !ibm->read)
820 return -EINVAL;
821 return ibm->read(m);
824 static int dispatch_proc_open(struct inode *inode, struct file *file)
826 return single_open(file, dispatch_proc_show, PDE(inode)->data);
829 static ssize_t dispatch_proc_write(struct file *file,
830 const char __user *userbuf,
831 size_t count, loff_t *pos)
833 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
834 char *kernbuf;
835 int ret;
837 if (!ibm || !ibm->write)
838 return -EINVAL;
839 if (count > PAGE_SIZE - 2)
840 return -EINVAL;
842 kernbuf = kmalloc(count + 2, GFP_KERNEL);
843 if (!kernbuf)
844 return -ENOMEM;
846 if (copy_from_user(kernbuf, userbuf, count)) {
847 kfree(kernbuf);
848 return -EFAULT;
851 kernbuf[count] = 0;
852 strcat(kernbuf, ",");
853 ret = ibm->write(kernbuf);
854 if (ret == 0)
855 ret = count;
857 kfree(kernbuf);
859 return ret;
862 static const struct file_operations dispatch_proc_fops = {
863 .owner = THIS_MODULE,
864 .open = dispatch_proc_open,
865 .read = seq_read,
866 .llseek = seq_lseek,
867 .release = single_release,
868 .write = dispatch_proc_write,
871 static char *next_cmd(char **cmds)
873 char *start = *cmds;
874 char *end;
876 while ((end = strchr(start, ',')) && end == start)
877 start = end + 1;
879 if (!end)
880 return NULL;
882 *end = 0;
883 *cmds = end + 1;
884 return start;
888 /****************************************************************************
889 ****************************************************************************
891 * Device model: input, hwmon and platform
893 ****************************************************************************
894 ****************************************************************************/
896 static struct platform_device *tpacpi_pdev;
897 static struct platform_device *tpacpi_sensors_pdev;
898 static struct device *tpacpi_hwmon;
899 static struct input_dev *tpacpi_inputdev;
900 static struct mutex tpacpi_inputdev_send_mutex;
901 static LIST_HEAD(tpacpi_all_drivers);
903 static int tpacpi_suspend_handler(struct platform_device *pdev,
904 pm_message_t state)
906 struct ibm_struct *ibm, *itmp;
908 list_for_each_entry_safe(ibm, itmp,
909 &tpacpi_all_drivers,
910 all_drivers) {
911 if (ibm->suspend)
912 (ibm->suspend)(state);
915 return 0;
918 static int tpacpi_resume_handler(struct platform_device *pdev)
920 struct ibm_struct *ibm, *itmp;
922 list_for_each_entry_safe(ibm, itmp,
923 &tpacpi_all_drivers,
924 all_drivers) {
925 if (ibm->resume)
926 (ibm->resume)();
929 return 0;
932 static void tpacpi_shutdown_handler(struct platform_device *pdev)
934 struct ibm_struct *ibm, *itmp;
936 list_for_each_entry_safe(ibm, itmp,
937 &tpacpi_all_drivers,
938 all_drivers) {
939 if (ibm->shutdown)
940 (ibm->shutdown)();
944 static struct platform_driver tpacpi_pdriver = {
945 .driver = {
946 .name = TPACPI_DRVR_NAME,
947 .owner = THIS_MODULE,
949 .suspend = tpacpi_suspend_handler,
950 .resume = tpacpi_resume_handler,
951 .shutdown = tpacpi_shutdown_handler,
954 static struct platform_driver tpacpi_hwmon_pdriver = {
955 .driver = {
956 .name = TPACPI_HWMON_DRVR_NAME,
957 .owner = THIS_MODULE,
961 /*************************************************************************
962 * sysfs support helpers
965 struct attribute_set {
966 unsigned int members, max_members;
967 struct attribute_group group;
970 struct attribute_set_obj {
971 struct attribute_set s;
972 struct attribute *a;
973 } __attribute__((packed));
975 static struct attribute_set *create_attr_set(unsigned int max_members,
976 const char *name)
978 struct attribute_set_obj *sobj;
980 if (max_members == 0)
981 return NULL;
983 /* Allocates space for implicit NULL at the end too */
984 sobj = kzalloc(sizeof(struct attribute_set_obj) +
985 max_members * sizeof(struct attribute *),
986 GFP_KERNEL);
987 if (!sobj)
988 return NULL;
989 sobj->s.max_members = max_members;
990 sobj->s.group.attrs = &sobj->a;
991 sobj->s.group.name = name;
993 return &sobj->s;
996 #define destroy_attr_set(_set) \
997 kfree(_set);
999 /* not multi-threaded safe, use it in a single thread per set */
1000 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1002 if (!s || !attr)
1003 return -EINVAL;
1005 if (s->members >= s->max_members)
1006 return -ENOMEM;
1008 s->group.attrs[s->members] = attr;
1009 s->members++;
1011 return 0;
1014 static int add_many_to_attr_set(struct attribute_set *s,
1015 struct attribute **attr,
1016 unsigned int count)
1018 int i, res;
1020 for (i = 0; i < count; i++) {
1021 res = add_to_attr_set(s, attr[i]);
1022 if (res)
1023 return res;
1026 return 0;
1029 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1031 sysfs_remove_group(kobj, &s->group);
1032 destroy_attr_set(s);
1035 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1036 sysfs_create_group(_kobj, &_attr_set->group)
1038 static int parse_strtoul(const char *buf,
1039 unsigned long max, unsigned long *value)
1041 char *endp;
1043 while (*buf && isspace(*buf))
1044 buf++;
1045 *value = simple_strtoul(buf, &endp, 0);
1046 while (*endp && isspace(*endp))
1047 endp++;
1048 if (*endp || *value > max)
1049 return -EINVAL;
1051 return 0;
1054 static void tpacpi_disable_brightness_delay(void)
1056 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1057 printk(TPACPI_NOTICE
1058 "ACPI backlight control delay disabled\n");
1061 static void printk_deprecated_attribute(const char * const what,
1062 const char * const details)
1064 tpacpi_log_usertask("deprecated sysfs attribute");
1065 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1066 "will be removed. %s\n",
1067 what, details);
1070 /*************************************************************************
1071 * rfkill and radio control support helpers
1075 * ThinkPad-ACPI firmware handling model:
1077 * WLSW (master wireless switch) is event-driven, and is common to all
1078 * firmware-controlled radios. It cannot be controlled, just monitored,
1079 * as expected. It overrides all radio state in firmware
1081 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1082 * (TODO: verify how WLSW interacts with the returned radio state).
1084 * The only time there are shadow radio state changes, is when
1085 * masked-off hotkeys are used.
1089 * Internal driver API for radio state:
1091 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1092 * bool: true means radio blocked (off)
1094 enum tpacpi_rfkill_state {
1095 TPACPI_RFK_RADIO_OFF = 0,
1096 TPACPI_RFK_RADIO_ON
1099 /* rfkill switches */
1100 enum tpacpi_rfk_id {
1101 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1102 TPACPI_RFK_WWAN_SW_ID,
1103 TPACPI_RFK_UWB_SW_ID,
1104 TPACPI_RFK_SW_MAX
1107 static const char *tpacpi_rfkill_names[] = {
1108 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1109 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1110 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1111 [TPACPI_RFK_SW_MAX] = NULL
1114 /* ThinkPad-ACPI rfkill subdriver */
1115 struct tpacpi_rfk {
1116 struct rfkill *rfkill;
1117 enum tpacpi_rfk_id id;
1118 const struct tpacpi_rfk_ops *ops;
1121 struct tpacpi_rfk_ops {
1122 /* firmware interface */
1123 int (*get_status)(void);
1124 int (*set_status)(const enum tpacpi_rfkill_state);
1127 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1129 /* Query FW and update rfkill sw state for a given rfkill switch */
1130 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1132 int status;
1134 if (!tp_rfk)
1135 return -ENODEV;
1137 status = (tp_rfk->ops->get_status)();
1138 if (status < 0)
1139 return status;
1141 rfkill_set_sw_state(tp_rfk->rfkill,
1142 (status == TPACPI_RFK_RADIO_OFF));
1144 return status;
1147 /* Query FW and update rfkill sw state for all rfkill switches */
1148 static void tpacpi_rfk_update_swstate_all(void)
1150 unsigned int i;
1152 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1153 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1157 * Sync the HW-blocking state of all rfkill switches,
1158 * do notice it causes the rfkill core to schedule uevents
1160 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1162 unsigned int i;
1163 struct tpacpi_rfk *tp_rfk;
1165 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1166 tp_rfk = tpacpi_rfkill_switches[i];
1167 if (tp_rfk) {
1168 if (rfkill_set_hw_state(tp_rfk->rfkill,
1169 blocked)) {
1170 /* ignore -- we track sw block */
1176 /* Call to get the WLSW state from the firmware */
1177 static int hotkey_get_wlsw(void);
1179 /* Call to query WLSW state and update all rfkill switches */
1180 static bool tpacpi_rfk_check_hwblock_state(void)
1182 int res = hotkey_get_wlsw();
1183 int hw_blocked;
1185 /* When unknown or unsupported, we have to assume it is unblocked */
1186 if (res < 0)
1187 return false;
1189 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1190 tpacpi_rfk_update_hwblock_state(hw_blocked);
1192 return hw_blocked;
1195 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1197 struct tpacpi_rfk *tp_rfk = data;
1198 int res;
1200 dbg_printk(TPACPI_DBG_RFKILL,
1201 "request to change radio state to %s\n",
1202 blocked ? "blocked" : "unblocked");
1204 /* try to set radio state */
1205 res = (tp_rfk->ops->set_status)(blocked ?
1206 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1208 /* and update the rfkill core with whatever the FW really did */
1209 tpacpi_rfk_update_swstate(tp_rfk);
1211 return (res < 0) ? res : 0;
1214 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1215 .set_block = tpacpi_rfk_hook_set_block,
1218 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1219 const struct tpacpi_rfk_ops *tp_rfkops,
1220 const enum rfkill_type rfktype,
1221 const char *name,
1222 const bool set_default)
1224 struct tpacpi_rfk *atp_rfk;
1225 int res;
1226 bool sw_state = false;
1227 bool hw_state;
1228 int sw_status;
1230 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1232 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1233 if (atp_rfk)
1234 atp_rfk->rfkill = rfkill_alloc(name,
1235 &tpacpi_pdev->dev,
1236 rfktype,
1237 &tpacpi_rfk_rfkill_ops,
1238 atp_rfk);
1239 if (!atp_rfk || !atp_rfk->rfkill) {
1240 printk(TPACPI_ERR
1241 "failed to allocate memory for rfkill class\n");
1242 kfree(atp_rfk);
1243 return -ENOMEM;
1246 atp_rfk->id = id;
1247 atp_rfk->ops = tp_rfkops;
1249 sw_status = (tp_rfkops->get_status)();
1250 if (sw_status < 0) {
1251 printk(TPACPI_ERR
1252 "failed to read initial state for %s, error %d\n",
1253 name, sw_status);
1254 } else {
1255 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1256 if (set_default) {
1257 /* try to keep the initial state, since we ask the
1258 * firmware to preserve it across S5 in NVRAM */
1259 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1262 hw_state = tpacpi_rfk_check_hwblock_state();
1263 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1265 res = rfkill_register(atp_rfk->rfkill);
1266 if (res < 0) {
1267 printk(TPACPI_ERR
1268 "failed to register %s rfkill switch: %d\n",
1269 name, res);
1270 rfkill_destroy(atp_rfk->rfkill);
1271 kfree(atp_rfk);
1272 return res;
1275 tpacpi_rfkill_switches[id] = atp_rfk;
1277 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1278 name, (sw_state || hw_state) ? "" : "un");
1279 return 0;
1282 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1284 struct tpacpi_rfk *tp_rfk;
1286 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1288 tp_rfk = tpacpi_rfkill_switches[id];
1289 if (tp_rfk) {
1290 rfkill_unregister(tp_rfk->rfkill);
1291 rfkill_destroy(tp_rfk->rfkill);
1292 tpacpi_rfkill_switches[id] = NULL;
1293 kfree(tp_rfk);
1297 static void printk_deprecated_rfkill_attribute(const char * const what)
1299 printk_deprecated_attribute(what,
1300 "Please switch to generic rfkill before year 2010");
1303 /* sysfs <radio> enable ------------------------------------------------ */
1304 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1305 struct device_attribute *attr,
1306 char *buf)
1308 int status;
1310 printk_deprecated_rfkill_attribute(attr->attr.name);
1312 /* This is in the ABI... */
1313 if (tpacpi_rfk_check_hwblock_state()) {
1314 status = TPACPI_RFK_RADIO_OFF;
1315 } else {
1316 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1317 if (status < 0)
1318 return status;
1321 return snprintf(buf, PAGE_SIZE, "%d\n",
1322 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1325 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1326 struct device_attribute *attr,
1327 const char *buf, size_t count)
1329 unsigned long t;
1330 int res;
1332 printk_deprecated_rfkill_attribute(attr->attr.name);
1334 if (parse_strtoul(buf, 1, &t))
1335 return -EINVAL;
1337 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1339 /* This is in the ABI... */
1340 if (tpacpi_rfk_check_hwblock_state() && !!t)
1341 return -EPERM;
1343 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1344 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1345 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1347 return (res < 0) ? res : count;
1350 /* procfs -------------------------------------------------------------- */
1351 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id,
1352 struct seq_file *m)
1354 if (id >= TPACPI_RFK_SW_MAX)
1355 seq_printf(m, "status:\t\tnot supported\n");
1356 else {
1357 int status;
1359 /* This is in the ABI... */
1360 if (tpacpi_rfk_check_hwblock_state()) {
1361 status = TPACPI_RFK_RADIO_OFF;
1362 } else {
1363 status = tpacpi_rfk_update_swstate(
1364 tpacpi_rfkill_switches[id]);
1365 if (status < 0)
1366 return status;
1369 seq_printf(m, "status:\t\t%s\n",
1370 (status == TPACPI_RFK_RADIO_ON) ?
1371 "enabled" : "disabled");
1372 seq_printf(m, "commands:\tenable, disable\n");
1375 return 0;
1378 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1380 char *cmd;
1381 int status = -1;
1382 int res = 0;
1384 if (id >= TPACPI_RFK_SW_MAX)
1385 return -ENODEV;
1387 while ((cmd = next_cmd(&buf))) {
1388 if (strlencmp(cmd, "enable") == 0)
1389 status = TPACPI_RFK_RADIO_ON;
1390 else if (strlencmp(cmd, "disable") == 0)
1391 status = TPACPI_RFK_RADIO_OFF;
1392 else
1393 return -EINVAL;
1396 if (status != -1) {
1397 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1398 (status == TPACPI_RFK_RADIO_ON) ?
1399 "enable" : "disable",
1400 tpacpi_rfkill_names[id]);
1401 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1402 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1405 return res;
1408 /*************************************************************************
1409 * thinkpad-acpi driver attributes
1412 /* interface_version --------------------------------------------------- */
1413 static ssize_t tpacpi_driver_interface_version_show(
1414 struct device_driver *drv,
1415 char *buf)
1417 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1420 static DRIVER_ATTR(interface_version, S_IRUGO,
1421 tpacpi_driver_interface_version_show, NULL);
1423 /* debug_level --------------------------------------------------------- */
1424 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1425 char *buf)
1427 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1430 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1431 const char *buf, size_t count)
1433 unsigned long t;
1435 if (parse_strtoul(buf, 0xffff, &t))
1436 return -EINVAL;
1438 dbg_level = t;
1440 return count;
1443 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1444 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1446 /* version ------------------------------------------------------------- */
1447 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1448 char *buf)
1450 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1451 TPACPI_DESC, TPACPI_VERSION);
1454 static DRIVER_ATTR(version, S_IRUGO,
1455 tpacpi_driver_version_show, NULL);
1457 /* --------------------------------------------------------------------- */
1459 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1461 /* wlsw_emulstate ------------------------------------------------------ */
1462 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1463 char *buf)
1465 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1468 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1469 const char *buf, size_t count)
1471 unsigned long t;
1473 if (parse_strtoul(buf, 1, &t))
1474 return -EINVAL;
1476 if (tpacpi_wlsw_emulstate != !!t) {
1477 tpacpi_wlsw_emulstate = !!t;
1478 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1481 return count;
1484 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1485 tpacpi_driver_wlsw_emulstate_show,
1486 tpacpi_driver_wlsw_emulstate_store);
1488 /* bluetooth_emulstate ------------------------------------------------- */
1489 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1490 struct device_driver *drv,
1491 char *buf)
1493 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1496 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1497 struct device_driver *drv,
1498 const char *buf, size_t count)
1500 unsigned long t;
1502 if (parse_strtoul(buf, 1, &t))
1503 return -EINVAL;
1505 tpacpi_bluetooth_emulstate = !!t;
1507 return count;
1510 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1511 tpacpi_driver_bluetooth_emulstate_show,
1512 tpacpi_driver_bluetooth_emulstate_store);
1514 /* wwan_emulstate ------------------------------------------------- */
1515 static ssize_t tpacpi_driver_wwan_emulstate_show(
1516 struct device_driver *drv,
1517 char *buf)
1519 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1522 static ssize_t tpacpi_driver_wwan_emulstate_store(
1523 struct device_driver *drv,
1524 const char *buf, size_t count)
1526 unsigned long t;
1528 if (parse_strtoul(buf, 1, &t))
1529 return -EINVAL;
1531 tpacpi_wwan_emulstate = !!t;
1533 return count;
1536 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1537 tpacpi_driver_wwan_emulstate_show,
1538 tpacpi_driver_wwan_emulstate_store);
1540 /* uwb_emulstate ------------------------------------------------- */
1541 static ssize_t tpacpi_driver_uwb_emulstate_show(
1542 struct device_driver *drv,
1543 char *buf)
1545 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1548 static ssize_t tpacpi_driver_uwb_emulstate_store(
1549 struct device_driver *drv,
1550 const char *buf, size_t count)
1552 unsigned long t;
1554 if (parse_strtoul(buf, 1, &t))
1555 return -EINVAL;
1557 tpacpi_uwb_emulstate = !!t;
1559 return count;
1562 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1563 tpacpi_driver_uwb_emulstate_show,
1564 tpacpi_driver_uwb_emulstate_store);
1565 #endif
1567 /* --------------------------------------------------------------------- */
1569 static struct driver_attribute *tpacpi_driver_attributes[] = {
1570 &driver_attr_debug_level, &driver_attr_version,
1571 &driver_attr_interface_version,
1574 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1576 int i, res;
1578 i = 0;
1579 res = 0;
1580 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1581 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1582 i++;
1585 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1586 if (!res && dbg_wlswemul)
1587 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1588 if (!res && dbg_bluetoothemul)
1589 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1590 if (!res && dbg_wwanemul)
1591 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1592 if (!res && dbg_uwbemul)
1593 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1594 #endif
1596 return res;
1599 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1601 int i;
1603 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1604 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1606 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1607 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1608 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1609 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1610 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1611 #endif
1614 /*************************************************************************
1615 * Firmware Data
1619 * Table of recommended minimum BIOS versions
1621 * Reasons for listing:
1622 * 1. Stable BIOS, listed because the unknown amount of
1623 * bugs and bad ACPI behaviour on older versions
1625 * 2. BIOS or EC fw with known bugs that trigger on Linux
1627 * 3. BIOS with known reduced functionality in older versions
1629 * We recommend the latest BIOS and EC version.
1630 * We only support the latest BIOS and EC fw version as a rule.
1632 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1633 * Information from users in ThinkWiki
1635 * WARNING: we use this table also to detect that the machine is
1636 * a ThinkPad in some cases, so don't remove entries lightly.
1639 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1640 { .vendor = (__v), \
1641 .bios = TPID(__id1, __id2), \
1642 .ec = TPACPI_MATCH_ANY, \
1643 .quirks = TPACPI_MATCH_ANY << 16 \
1644 | (__bv1) << 8 | (__bv2) }
1646 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1647 __eid, __ev1, __ev2) \
1648 { .vendor = (__v), \
1649 .bios = TPID(__bid1, __bid2), \
1650 .ec = __eid, \
1651 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1652 | (__bv1) << 8 | (__bv2) }
1654 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1655 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1657 /* Outdated IBM BIOSes often lack the EC id string */
1658 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1659 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1660 __bv1, __bv2, TPID(__id1, __id2), \
1661 __ev1, __ev2), \
1662 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1663 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1664 __ev1, __ev2)
1666 /* Outdated IBM BIOSes often lack the EC id string */
1667 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1668 __eid1, __eid2, __ev1, __ev2) \
1669 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1670 __bv1, __bv2, TPID(__eid1, __eid2), \
1671 __ev1, __ev2), \
1672 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1673 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1674 __ev1, __ev2)
1676 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1677 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1679 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1680 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1681 __bv1, __bv2, TPID(__id1, __id2), \
1682 __ev1, __ev2)
1684 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1685 __eid1, __eid2, __ev1, __ev2) \
1686 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1687 __bv1, __bv2, TPID(__eid1, __eid2), \
1688 __ev1, __ev2)
1690 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1691 /* Numeric models ------------------ */
1692 /* FW MODEL BIOS VERS */
1693 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1694 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1695 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1696 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1697 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1698 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1699 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1700 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1701 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1703 /* A-series ------------------------- */
1704 /* FW MODEL BIOS VERS EC VERS */
1705 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1706 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1707 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1708 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1709 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1710 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1711 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1712 TPV_QI0('1', '3', '2', '0'), /* A22m */
1713 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1714 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1715 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1717 /* G-series ------------------------- */
1718 /* FW MODEL BIOS VERS */
1719 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1720 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1722 /* R-series, T-series --------------- */
1723 /* FW MODEL BIOS VERS EC VERS */
1724 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1725 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1726 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1727 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1728 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1729 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1730 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1731 T40/p, T41/p, T42/p (1) */
1732 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1733 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1734 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1735 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1737 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1738 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1739 TPV_QI0('1', '6', '3', '2'), /* T22 */
1740 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1741 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1742 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1744 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1745 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1746 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1748 /* BIOS FW BIOS VERS EC FW EC VERS */
1749 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1750 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1752 /* X-series ------------------------- */
1753 /* FW MODEL BIOS VERS EC VERS */
1754 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1755 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1756 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1757 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1758 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1759 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1760 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1762 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1763 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1765 /* (0) - older versions lack DMI EC fw string and functionality */
1766 /* (1) - older versions known to lack functionality */
1769 #undef TPV_QL1
1770 #undef TPV_QL0
1771 #undef TPV_QI2
1772 #undef TPV_QI1
1773 #undef TPV_QI0
1774 #undef TPV_Q_X
1775 #undef TPV_Q
1777 static void __init tpacpi_check_outdated_fw(void)
1779 unsigned long fwvers;
1780 u16 ec_version, bios_version;
1782 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1783 ARRAY_SIZE(tpacpi_bios_version_qtable));
1785 if (!fwvers)
1786 return;
1788 bios_version = fwvers & 0xffffU;
1789 ec_version = (fwvers >> 16) & 0xffffU;
1791 /* note that unknown versions are set to 0x0000 and we use that */
1792 if ((bios_version > thinkpad_id.bios_release) ||
1793 (ec_version > thinkpad_id.ec_release &&
1794 ec_version != TPACPI_MATCH_ANY)) {
1796 * The changelogs would let us track down the exact
1797 * reason, but it is just too much of a pain to track
1798 * it. We only list BIOSes that are either really
1799 * broken, or really stable to begin with, so it is
1800 * best if the user upgrades the firmware anyway.
1802 printk(TPACPI_WARN
1803 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1804 printk(TPACPI_WARN
1805 "WARNING: This firmware may be missing critical bug "
1806 "fixes and/or important features\n");
1810 static bool __init tpacpi_is_fw_known(void)
1812 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1813 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1816 /****************************************************************************
1817 ****************************************************************************
1819 * Subdrivers
1821 ****************************************************************************
1822 ****************************************************************************/
1824 /*************************************************************************
1825 * thinkpad-acpi metadata subdriver
1828 static int thinkpad_acpi_driver_read(struct seq_file *m)
1830 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1831 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1832 return 0;
1835 static struct ibm_struct thinkpad_acpi_driver_data = {
1836 .name = "driver",
1837 .read = thinkpad_acpi_driver_read,
1840 /*************************************************************************
1841 * Hotkey subdriver
1845 * ThinkPad firmware event model
1847 * The ThinkPad firmware has two main event interfaces: normal ACPI
1848 * notifications (which follow the ACPI standard), and a private event
1849 * interface.
1851 * The private event interface also issues events for the hotkeys. As
1852 * the driver gained features, the event handling code ended up being
1853 * built around the hotkey subdriver. This will need to be refactored
1854 * to a more formal event API eventually.
1856 * Some "hotkeys" are actually supposed to be used as event reports,
1857 * such as "brightness has changed", "volume has changed", depending on
1858 * the ThinkPad model and how the firmware is operating.
1860 * Unlike other classes, hotkey-class events have mask/unmask control on
1861 * non-ancient firmware. However, how it behaves changes a lot with the
1862 * firmware model and version.
1865 enum { /* hot key scan codes (derived from ACPI DSDT) */
1866 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1867 TP_ACPI_HOTKEYSCAN_FNF2,
1868 TP_ACPI_HOTKEYSCAN_FNF3,
1869 TP_ACPI_HOTKEYSCAN_FNF4,
1870 TP_ACPI_HOTKEYSCAN_FNF5,
1871 TP_ACPI_HOTKEYSCAN_FNF6,
1872 TP_ACPI_HOTKEYSCAN_FNF7,
1873 TP_ACPI_HOTKEYSCAN_FNF8,
1874 TP_ACPI_HOTKEYSCAN_FNF9,
1875 TP_ACPI_HOTKEYSCAN_FNF10,
1876 TP_ACPI_HOTKEYSCAN_FNF11,
1877 TP_ACPI_HOTKEYSCAN_FNF12,
1878 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1879 TP_ACPI_HOTKEYSCAN_FNINSERT,
1880 TP_ACPI_HOTKEYSCAN_FNDELETE,
1881 TP_ACPI_HOTKEYSCAN_FNHOME,
1882 TP_ACPI_HOTKEYSCAN_FNEND,
1883 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1884 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1885 TP_ACPI_HOTKEYSCAN_FNSPACE,
1886 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1887 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1888 TP_ACPI_HOTKEYSCAN_MUTE,
1889 TP_ACPI_HOTKEYSCAN_THINKPAD,
1892 enum { /* Keys/events available through NVRAM polling */
1893 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1894 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1897 enum { /* Positions of some of the keys in hotkey masks */
1898 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1899 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1900 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1901 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1902 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1903 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1904 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1905 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1906 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1907 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1908 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1911 enum { /* NVRAM to ACPI HKEY group map */
1912 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1913 TP_ACPI_HKEY_ZOOM_MASK |
1914 TP_ACPI_HKEY_DISPSWTCH_MASK |
1915 TP_ACPI_HKEY_HIBERNATE_MASK,
1916 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1917 TP_ACPI_HKEY_BRGHTDWN_MASK,
1918 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1919 TP_ACPI_HKEY_VOLDWN_MASK |
1920 TP_ACPI_HKEY_MUTE_MASK,
1923 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1924 struct tp_nvram_state {
1925 u16 thinkpad_toggle:1;
1926 u16 zoom_toggle:1;
1927 u16 display_toggle:1;
1928 u16 thinklight_toggle:1;
1929 u16 hibernate_toggle:1;
1930 u16 displayexp_toggle:1;
1931 u16 display_state:1;
1932 u16 brightness_toggle:1;
1933 u16 volume_toggle:1;
1934 u16 mute:1;
1936 u8 brightness_level;
1937 u8 volume_level;
1940 /* kthread for the hotkey poller */
1941 static struct task_struct *tpacpi_hotkey_task;
1943 /* Acquired while the poller kthread is running, use to sync start/stop */
1944 static struct mutex hotkey_thread_mutex;
1947 * Acquire mutex to write poller control variables as an
1948 * atomic block.
1950 * Increment hotkey_config_change when changing them if you
1951 * want the kthread to forget old state.
1953 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1955 static struct mutex hotkey_thread_data_mutex;
1956 static unsigned int hotkey_config_change;
1959 * hotkey poller control variables
1961 * Must be atomic or readers will also need to acquire mutex
1963 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1964 * should be used only when the changes need to be taken as
1965 * a block, OR when one needs to force the kthread to forget
1966 * old state.
1968 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1969 static unsigned int hotkey_poll_freq = 10; /* Hz */
1971 #define HOTKEY_CONFIG_CRITICAL_START \
1972 do { \
1973 mutex_lock(&hotkey_thread_data_mutex); \
1974 hotkey_config_change++; \
1975 } while (0);
1976 #define HOTKEY_CONFIG_CRITICAL_END \
1977 mutex_unlock(&hotkey_thread_data_mutex);
1979 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1981 #define hotkey_source_mask 0U
1982 #define HOTKEY_CONFIG_CRITICAL_START
1983 #define HOTKEY_CONFIG_CRITICAL_END
1985 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1987 static struct mutex hotkey_mutex;
1989 static enum { /* Reasons for waking up */
1990 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1991 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1992 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1993 } hotkey_wakeup_reason;
1995 static int hotkey_autosleep_ack;
1997 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
1998 static u32 hotkey_all_mask; /* all events supported in fw */
1999 static u32 hotkey_reserved_mask; /* events better left disabled */
2000 static u32 hotkey_driver_mask; /* events needed by the driver */
2001 static u32 hotkey_user_mask; /* events visible to userspace */
2002 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2004 static unsigned int hotkey_report_mode;
2006 static u16 *hotkey_keycode_map;
2008 static struct attribute_set *hotkey_dev_attributes;
2010 static void tpacpi_driver_event(const unsigned int hkey_event);
2011 static void hotkey_driver_event(const unsigned int scancode);
2012 static void hotkey_poll_setup(const bool may_warn);
2014 /* HKEY.MHKG() return bits */
2015 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2017 static int hotkey_get_wlsw(void)
2019 int status;
2021 if (!tp_features.hotkey_wlsw)
2022 return -ENODEV;
2024 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2025 if (dbg_wlswemul)
2026 return (tpacpi_wlsw_emulstate) ?
2027 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2028 #endif
2030 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2031 return -EIO;
2033 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2036 static int hotkey_get_tablet_mode(int *status)
2038 int s;
2040 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2041 return -EIO;
2043 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2044 return 0;
2048 * Reads current event mask from firmware, and updates
2049 * hotkey_acpi_mask accordingly. Also resets any bits
2050 * from hotkey_user_mask that are unavailable to be
2051 * delivered (shadow requirement of the userspace ABI).
2053 * Call with hotkey_mutex held
2055 static int hotkey_mask_get(void)
2057 if (tp_features.hotkey_mask) {
2058 u32 m = 0;
2060 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2061 return -EIO;
2063 hotkey_acpi_mask = m;
2064 } else {
2065 /* no mask support doesn't mean no event support... */
2066 hotkey_acpi_mask = hotkey_all_mask;
2069 /* sync userspace-visible mask */
2070 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2072 return 0;
2075 void static hotkey_mask_warn_incomplete_mask(void)
2077 /* log only what the user can fix... */
2078 const u32 wantedmask = hotkey_driver_mask &
2079 ~(hotkey_acpi_mask | hotkey_source_mask) &
2080 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2082 if (wantedmask)
2083 printk(TPACPI_NOTICE
2084 "required events 0x%08x not enabled!\n",
2085 wantedmask);
2089 * Set the firmware mask when supported
2091 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2093 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2095 * Call with hotkey_mutex held
2097 static int hotkey_mask_set(u32 mask)
2099 int i;
2100 int rc = 0;
2102 const u32 fwmask = mask & ~hotkey_source_mask;
2104 if (tp_features.hotkey_mask) {
2105 for (i = 0; i < 32; i++) {
2106 if (!acpi_evalf(hkey_handle,
2107 NULL, "MHKM", "vdd", i + 1,
2108 !!(mask & (1 << i)))) {
2109 rc = -EIO;
2110 break;
2116 * We *must* make an inconditional call to hotkey_mask_get to
2117 * refresh hotkey_acpi_mask and update hotkey_user_mask
2119 * Take the opportunity to also log when we cannot _enable_
2120 * a given event.
2122 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2123 printk(TPACPI_NOTICE
2124 "asked for hotkey mask 0x%08x, but "
2125 "firmware forced it to 0x%08x\n",
2126 fwmask, hotkey_acpi_mask);
2129 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2130 hotkey_mask_warn_incomplete_mask();
2132 return rc;
2136 * Sets hotkey_user_mask and tries to set the firmware mask
2138 * Call with hotkey_mutex held
2140 static int hotkey_user_mask_set(const u32 mask)
2142 int rc;
2144 /* Give people a chance to notice they are doing something that
2145 * is bound to go boom on their users sooner or later */
2146 if (!tp_warned.hotkey_mask_ff &&
2147 (mask == 0xffff || mask == 0xffffff ||
2148 mask == 0xffffffff)) {
2149 tp_warned.hotkey_mask_ff = 1;
2150 printk(TPACPI_NOTICE
2151 "setting the hotkey mask to 0x%08x is likely "
2152 "not the best way to go about it\n", mask);
2153 printk(TPACPI_NOTICE
2154 "please consider using the driver defaults, "
2155 "and refer to up-to-date thinkpad-acpi "
2156 "documentation\n");
2159 /* Try to enable what the user asked for, plus whatever we need.
2160 * this syncs everything but won't enable bits in hotkey_user_mask */
2161 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2163 /* Enable the available bits in hotkey_user_mask */
2164 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2166 return rc;
2170 * Sets the driver hotkey mask.
2172 * Can be called even if the hotkey subdriver is inactive
2174 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2176 int rc;
2178 /* Do the right thing if hotkey_init has not been called yet */
2179 if (!tp_features.hotkey) {
2180 hotkey_driver_mask = mask;
2181 return 0;
2184 mutex_lock(&hotkey_mutex);
2186 HOTKEY_CONFIG_CRITICAL_START
2187 hotkey_driver_mask = mask;
2188 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2189 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2190 #endif
2191 HOTKEY_CONFIG_CRITICAL_END
2193 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2194 ~hotkey_source_mask);
2195 hotkey_poll_setup(true);
2197 mutex_unlock(&hotkey_mutex);
2199 return rc;
2202 static int hotkey_status_get(int *status)
2204 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2205 return -EIO;
2207 return 0;
2210 static int hotkey_status_set(bool enable)
2212 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2213 return -EIO;
2215 return 0;
2218 static void tpacpi_input_send_tabletsw(void)
2220 int state;
2222 if (tp_features.hotkey_tablet &&
2223 !hotkey_get_tablet_mode(&state)) {
2224 mutex_lock(&tpacpi_inputdev_send_mutex);
2226 input_report_switch(tpacpi_inputdev,
2227 SW_TABLET_MODE, !!state);
2228 input_sync(tpacpi_inputdev);
2230 mutex_unlock(&tpacpi_inputdev_send_mutex);
2234 /* Do NOT call without validating scancode first */
2235 static void tpacpi_input_send_key(const unsigned int scancode)
2237 const unsigned int keycode = hotkey_keycode_map[scancode];
2239 if (keycode != KEY_RESERVED) {
2240 mutex_lock(&tpacpi_inputdev_send_mutex);
2242 input_report_key(tpacpi_inputdev, keycode, 1);
2243 if (keycode == KEY_UNKNOWN)
2244 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2245 scancode);
2246 input_sync(tpacpi_inputdev);
2248 input_report_key(tpacpi_inputdev, keycode, 0);
2249 if (keycode == KEY_UNKNOWN)
2250 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2251 scancode);
2252 input_sync(tpacpi_inputdev);
2254 mutex_unlock(&tpacpi_inputdev_send_mutex);
2258 /* Do NOT call without validating scancode first */
2259 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2261 hotkey_driver_event(scancode);
2262 if (hotkey_user_mask & (1 << scancode))
2263 tpacpi_input_send_key(scancode);
2266 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2267 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2269 /* Do NOT call without validating scancode first */
2270 static void tpacpi_hotkey_send_key(unsigned int scancode)
2272 tpacpi_input_send_key_masked(scancode);
2273 if (hotkey_report_mode < 2) {
2274 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2275 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2279 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2281 u8 d;
2283 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2284 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2285 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2286 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2287 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2288 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2290 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2291 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2292 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2294 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2295 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2296 n->displayexp_toggle =
2297 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2299 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2300 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2301 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2302 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2303 n->brightness_toggle =
2304 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2306 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2307 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2308 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2309 >> TP_NVRAM_POS_LEVEL_VOLUME;
2310 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2311 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2315 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2316 struct tp_nvram_state *newn,
2317 const u32 event_mask)
2320 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2321 do { \
2322 if ((event_mask & (1 << __scancode)) && \
2323 oldn->__member != newn->__member) \
2324 tpacpi_hotkey_send_key(__scancode); \
2325 } while (0)
2327 #define TPACPI_MAY_SEND_KEY(__scancode) \
2328 do { \
2329 if (event_mask & (1 << __scancode)) \
2330 tpacpi_hotkey_send_key(__scancode); \
2331 } while (0)
2333 void issue_volchange(const unsigned int oldvol,
2334 const unsigned int newvol)
2336 unsigned int i = oldvol;
2338 while (i > newvol) {
2339 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2340 i--;
2342 while (i < newvol) {
2343 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2344 i++;
2348 void issue_brightnesschange(const unsigned int oldbrt,
2349 const unsigned int newbrt)
2351 unsigned int i = oldbrt;
2353 while (i > newbrt) {
2354 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2355 i--;
2357 while (i < newbrt) {
2358 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2359 i++;
2363 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2364 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2365 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2366 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2368 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2370 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2373 * Handle volume
2375 * This code is supposed to duplicate the IBM firmware behaviour:
2376 * - Pressing MUTE issues mute hotkey message, even when already mute
2377 * - Pressing Volume up/down issues volume up/down hotkey messages,
2378 * even when already at maximum or minumum volume
2379 * - The act of unmuting issues volume up/down notification,
2380 * depending which key was used to unmute
2382 * We are constrained to what the NVRAM can tell us, which is not much
2383 * and certainly not enough if more than one volume hotkey was pressed
2384 * since the last poll cycle.
2386 * Just to make our life interesting, some newer Lenovo ThinkPads have
2387 * bugs in the BIOS and may fail to update volume_toggle properly.
2389 if (newn->mute) {
2390 /* muted */
2391 if (!oldn->mute ||
2392 oldn->volume_toggle != newn->volume_toggle ||
2393 oldn->volume_level != newn->volume_level) {
2394 /* recently muted, or repeated mute keypress, or
2395 * multiple presses ending in mute */
2396 issue_volchange(oldn->volume_level, newn->volume_level);
2397 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2399 } else {
2400 /* unmute */
2401 if (oldn->mute) {
2402 /* recently unmuted, issue 'unmute' keypress */
2403 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2405 if (oldn->volume_level != newn->volume_level) {
2406 issue_volchange(oldn->volume_level, newn->volume_level);
2407 } else if (oldn->volume_toggle != newn->volume_toggle) {
2408 /* repeated vol up/down keypress at end of scale ? */
2409 if (newn->volume_level == 0)
2410 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2411 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2412 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2416 /* handle brightness */
2417 if (oldn->brightness_level != newn->brightness_level) {
2418 issue_brightnesschange(oldn->brightness_level,
2419 newn->brightness_level);
2420 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2421 /* repeated key presses that didn't change state */
2422 if (newn->brightness_level == 0)
2423 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2424 else if (newn->brightness_level >= bright_maxlvl
2425 && !tp_features.bright_unkfw)
2426 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2429 #undef TPACPI_COMPARE_KEY
2430 #undef TPACPI_MAY_SEND_KEY
2434 * Polling driver
2436 * We track all events in hotkey_source_mask all the time, since
2437 * most of them are edge-based. We only issue those requested by
2438 * hotkey_user_mask or hotkey_driver_mask, though.
2440 static int hotkey_kthread(void *data)
2442 struct tp_nvram_state s[2];
2443 u32 poll_mask, event_mask;
2444 unsigned int si, so;
2445 unsigned long t;
2446 unsigned int change_detector, must_reset;
2447 unsigned int poll_freq;
2449 mutex_lock(&hotkey_thread_mutex);
2451 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2452 goto exit;
2454 set_freezable();
2456 so = 0;
2457 si = 1;
2458 t = 0;
2460 /* Initial state for compares */
2461 mutex_lock(&hotkey_thread_data_mutex);
2462 change_detector = hotkey_config_change;
2463 poll_mask = hotkey_source_mask;
2464 event_mask = hotkey_source_mask &
2465 (hotkey_driver_mask | hotkey_user_mask);
2466 poll_freq = hotkey_poll_freq;
2467 mutex_unlock(&hotkey_thread_data_mutex);
2468 hotkey_read_nvram(&s[so], poll_mask);
2470 while (!kthread_should_stop()) {
2471 if (t == 0) {
2472 if (likely(poll_freq))
2473 t = 1000/poll_freq;
2474 else
2475 t = 100; /* should never happen... */
2477 t = msleep_interruptible(t);
2478 if (unlikely(kthread_should_stop()))
2479 break;
2480 must_reset = try_to_freeze();
2481 if (t > 0 && !must_reset)
2482 continue;
2484 mutex_lock(&hotkey_thread_data_mutex);
2485 if (must_reset || hotkey_config_change != change_detector) {
2486 /* forget old state on thaw or config change */
2487 si = so;
2488 t = 0;
2489 change_detector = hotkey_config_change;
2491 poll_mask = hotkey_source_mask;
2492 event_mask = hotkey_source_mask &
2493 (hotkey_driver_mask | hotkey_user_mask);
2494 poll_freq = hotkey_poll_freq;
2495 mutex_unlock(&hotkey_thread_data_mutex);
2497 if (likely(poll_mask)) {
2498 hotkey_read_nvram(&s[si], poll_mask);
2499 if (likely(si != so)) {
2500 hotkey_compare_and_issue_event(&s[so], &s[si],
2501 event_mask);
2505 so = si;
2506 si ^= 1;
2509 exit:
2510 mutex_unlock(&hotkey_thread_mutex);
2511 return 0;
2514 /* call with hotkey_mutex held */
2515 static void hotkey_poll_stop_sync(void)
2517 if (tpacpi_hotkey_task) {
2518 if (frozen(tpacpi_hotkey_task) ||
2519 freezing(tpacpi_hotkey_task))
2520 thaw_process(tpacpi_hotkey_task);
2522 kthread_stop(tpacpi_hotkey_task);
2523 tpacpi_hotkey_task = NULL;
2524 mutex_lock(&hotkey_thread_mutex);
2525 /* at this point, the thread did exit */
2526 mutex_unlock(&hotkey_thread_mutex);
2530 /* call with hotkey_mutex held */
2531 static void hotkey_poll_setup(const bool may_warn)
2533 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2534 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2536 if (hotkey_poll_freq > 0 &&
2537 (poll_driver_mask ||
2538 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2539 if (!tpacpi_hotkey_task) {
2540 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2541 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2542 if (IS_ERR(tpacpi_hotkey_task)) {
2543 tpacpi_hotkey_task = NULL;
2544 printk(TPACPI_ERR
2545 "could not create kernel thread "
2546 "for hotkey polling\n");
2549 } else {
2550 hotkey_poll_stop_sync();
2551 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2552 hotkey_poll_freq == 0) {
2553 printk(TPACPI_NOTICE
2554 "hot keys 0x%08x and/or events 0x%08x "
2555 "require polling, which is currently "
2556 "disabled\n",
2557 poll_user_mask, poll_driver_mask);
2562 static void hotkey_poll_setup_safe(const bool may_warn)
2564 mutex_lock(&hotkey_mutex);
2565 hotkey_poll_setup(may_warn);
2566 mutex_unlock(&hotkey_mutex);
2569 /* call with hotkey_mutex held */
2570 static void hotkey_poll_set_freq(unsigned int freq)
2572 if (!freq)
2573 hotkey_poll_stop_sync();
2575 hotkey_poll_freq = freq;
2578 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2580 static void hotkey_poll_setup(const bool __unused)
2584 static void hotkey_poll_setup_safe(const bool __unused)
2588 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2590 static int hotkey_inputdev_open(struct input_dev *dev)
2592 switch (tpacpi_lifecycle) {
2593 case TPACPI_LIFE_INIT:
2594 case TPACPI_LIFE_RUNNING:
2595 hotkey_poll_setup_safe(false);
2596 return 0;
2597 case TPACPI_LIFE_EXITING:
2598 return -EBUSY;
2601 /* Should only happen if tpacpi_lifecycle is corrupt */
2602 BUG();
2603 return -EBUSY;
2606 static void hotkey_inputdev_close(struct input_dev *dev)
2608 /* disable hotkey polling when possible */
2609 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2610 !(hotkey_source_mask & hotkey_driver_mask))
2611 hotkey_poll_setup_safe(false);
2614 /* sysfs hotkey enable ------------------------------------------------- */
2615 static ssize_t hotkey_enable_show(struct device *dev,
2616 struct device_attribute *attr,
2617 char *buf)
2619 int res, status;
2621 printk_deprecated_attribute("hotkey_enable",
2622 "Hotkey reporting is always enabled");
2624 res = hotkey_status_get(&status);
2625 if (res)
2626 return res;
2628 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2631 static ssize_t hotkey_enable_store(struct device *dev,
2632 struct device_attribute *attr,
2633 const char *buf, size_t count)
2635 unsigned long t;
2637 printk_deprecated_attribute("hotkey_enable",
2638 "Hotkeys can be disabled through hotkey_mask");
2640 if (parse_strtoul(buf, 1, &t))
2641 return -EINVAL;
2643 if (t == 0)
2644 return -EPERM;
2646 return count;
2649 static struct device_attribute dev_attr_hotkey_enable =
2650 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2651 hotkey_enable_show, hotkey_enable_store);
2653 /* sysfs hotkey mask --------------------------------------------------- */
2654 static ssize_t hotkey_mask_show(struct device *dev,
2655 struct device_attribute *attr,
2656 char *buf)
2658 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2661 static ssize_t hotkey_mask_store(struct device *dev,
2662 struct device_attribute *attr,
2663 const char *buf, size_t count)
2665 unsigned long t;
2666 int res;
2668 if (parse_strtoul(buf, 0xffffffffUL, &t))
2669 return -EINVAL;
2671 if (mutex_lock_killable(&hotkey_mutex))
2672 return -ERESTARTSYS;
2674 res = hotkey_user_mask_set(t);
2676 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2677 hotkey_poll_setup(true);
2678 #endif
2680 mutex_unlock(&hotkey_mutex);
2682 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2684 return (res) ? res : count;
2687 static struct device_attribute dev_attr_hotkey_mask =
2688 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2689 hotkey_mask_show, hotkey_mask_store);
2691 /* sysfs hotkey bios_enabled ------------------------------------------- */
2692 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2693 struct device_attribute *attr,
2694 char *buf)
2696 return sprintf(buf, "0\n");
2699 static struct device_attribute dev_attr_hotkey_bios_enabled =
2700 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2702 /* sysfs hotkey bios_mask ---------------------------------------------- */
2703 static ssize_t hotkey_bios_mask_show(struct device *dev,
2704 struct device_attribute *attr,
2705 char *buf)
2707 printk_deprecated_attribute("hotkey_bios_mask",
2708 "This attribute is useless.");
2709 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2712 static struct device_attribute dev_attr_hotkey_bios_mask =
2713 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2715 /* sysfs hotkey all_mask ----------------------------------------------- */
2716 static ssize_t hotkey_all_mask_show(struct device *dev,
2717 struct device_attribute *attr,
2718 char *buf)
2720 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2721 hotkey_all_mask | hotkey_source_mask);
2724 static struct device_attribute dev_attr_hotkey_all_mask =
2725 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2727 /* sysfs hotkey recommended_mask --------------------------------------- */
2728 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2729 struct device_attribute *attr,
2730 char *buf)
2732 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2733 (hotkey_all_mask | hotkey_source_mask)
2734 & ~hotkey_reserved_mask);
2737 static struct device_attribute dev_attr_hotkey_recommended_mask =
2738 __ATTR(hotkey_recommended_mask, S_IRUGO,
2739 hotkey_recommended_mask_show, NULL);
2741 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2743 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2744 static ssize_t hotkey_source_mask_show(struct device *dev,
2745 struct device_attribute *attr,
2746 char *buf)
2748 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2751 static ssize_t hotkey_source_mask_store(struct device *dev,
2752 struct device_attribute *attr,
2753 const char *buf, size_t count)
2755 unsigned long t;
2756 u32 r_ev;
2757 int rc;
2759 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2760 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2761 return -EINVAL;
2763 if (mutex_lock_killable(&hotkey_mutex))
2764 return -ERESTARTSYS;
2766 HOTKEY_CONFIG_CRITICAL_START
2767 hotkey_source_mask = t;
2768 HOTKEY_CONFIG_CRITICAL_END
2770 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2771 ~hotkey_source_mask);
2772 hotkey_poll_setup(true);
2774 /* check if events needed by the driver got disabled */
2775 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2776 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2778 mutex_unlock(&hotkey_mutex);
2780 if (rc < 0)
2781 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2782 "firmware event mask!\n");
2784 if (r_ev)
2785 printk(TPACPI_NOTICE "hotkey_source_mask: "
2786 "some important events were disabled: "
2787 "0x%04x\n", r_ev);
2789 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2791 return (rc < 0) ? rc : count;
2794 static struct device_attribute dev_attr_hotkey_source_mask =
2795 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2796 hotkey_source_mask_show, hotkey_source_mask_store);
2798 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2799 static ssize_t hotkey_poll_freq_show(struct device *dev,
2800 struct device_attribute *attr,
2801 char *buf)
2803 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2806 static ssize_t hotkey_poll_freq_store(struct device *dev,
2807 struct device_attribute *attr,
2808 const char *buf, size_t count)
2810 unsigned long t;
2812 if (parse_strtoul(buf, 25, &t))
2813 return -EINVAL;
2815 if (mutex_lock_killable(&hotkey_mutex))
2816 return -ERESTARTSYS;
2818 hotkey_poll_set_freq(t);
2819 hotkey_poll_setup(true);
2821 mutex_unlock(&hotkey_mutex);
2823 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2825 return count;
2828 static struct device_attribute dev_attr_hotkey_poll_freq =
2829 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2830 hotkey_poll_freq_show, hotkey_poll_freq_store);
2832 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2834 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2835 static ssize_t hotkey_radio_sw_show(struct device *dev,
2836 struct device_attribute *attr,
2837 char *buf)
2839 int res;
2840 res = hotkey_get_wlsw();
2841 if (res < 0)
2842 return res;
2844 /* Opportunistic update */
2845 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2847 return snprintf(buf, PAGE_SIZE, "%d\n",
2848 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2851 static struct device_attribute dev_attr_hotkey_radio_sw =
2852 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2854 static void hotkey_radio_sw_notify_change(void)
2856 if (tp_features.hotkey_wlsw)
2857 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2858 "hotkey_radio_sw");
2861 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2862 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2863 struct device_attribute *attr,
2864 char *buf)
2866 int res, s;
2867 res = hotkey_get_tablet_mode(&s);
2868 if (res < 0)
2869 return res;
2871 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2874 static struct device_attribute dev_attr_hotkey_tablet_mode =
2875 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2877 static void hotkey_tablet_mode_notify_change(void)
2879 if (tp_features.hotkey_tablet)
2880 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2881 "hotkey_tablet_mode");
2884 /* sysfs hotkey report_mode -------------------------------------------- */
2885 static ssize_t hotkey_report_mode_show(struct device *dev,
2886 struct device_attribute *attr,
2887 char *buf)
2889 return snprintf(buf, PAGE_SIZE, "%d\n",
2890 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2893 static struct device_attribute dev_attr_hotkey_report_mode =
2894 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2896 /* sysfs wakeup reason (pollable) -------------------------------------- */
2897 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2898 struct device_attribute *attr,
2899 char *buf)
2901 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2904 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2905 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2907 static void hotkey_wakeup_reason_notify_change(void)
2909 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2910 "wakeup_reason");
2913 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2914 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2915 struct device_attribute *attr,
2916 char *buf)
2918 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2921 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2922 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2923 hotkey_wakeup_hotunplug_complete_show, NULL);
2925 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2927 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2928 "wakeup_hotunplug_complete");
2931 /* --------------------------------------------------------------------- */
2933 static struct attribute *hotkey_attributes[] __initdata = {
2934 &dev_attr_hotkey_enable.attr,
2935 &dev_attr_hotkey_bios_enabled.attr,
2936 &dev_attr_hotkey_bios_mask.attr,
2937 &dev_attr_hotkey_report_mode.attr,
2938 &dev_attr_hotkey_wakeup_reason.attr,
2939 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2940 &dev_attr_hotkey_mask.attr,
2941 &dev_attr_hotkey_all_mask.attr,
2942 &dev_attr_hotkey_recommended_mask.attr,
2943 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2944 &dev_attr_hotkey_source_mask.attr,
2945 &dev_attr_hotkey_poll_freq.attr,
2946 #endif
2950 * Sync both the hw and sw blocking state of all switches
2952 static void tpacpi_send_radiosw_update(void)
2954 int wlsw;
2957 * We must sync all rfkill controllers *before* issuing any
2958 * rfkill input events, or we will race the rfkill core input
2959 * handler.
2961 * tpacpi_inputdev_send_mutex works as a syncronization point
2962 * for the above.
2964 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2967 wlsw = hotkey_get_wlsw();
2969 /* Sync hw blocking state first if it is hw-blocked */
2970 if (wlsw == TPACPI_RFK_RADIO_OFF)
2971 tpacpi_rfk_update_hwblock_state(true);
2973 /* Sync sw blocking state */
2974 tpacpi_rfk_update_swstate_all();
2976 /* Sync hw blocking state last if it is hw-unblocked */
2977 if (wlsw == TPACPI_RFK_RADIO_ON)
2978 tpacpi_rfk_update_hwblock_state(false);
2980 /* Issue rfkill input event for WLSW switch */
2981 if (!(wlsw < 0)) {
2982 mutex_lock(&tpacpi_inputdev_send_mutex);
2984 input_report_switch(tpacpi_inputdev,
2985 SW_RFKILL_ALL, (wlsw > 0));
2986 input_sync(tpacpi_inputdev);
2988 mutex_unlock(&tpacpi_inputdev_send_mutex);
2992 * this can be unconditional, as we will poll state again
2993 * if userspace uses the notify to read data
2995 hotkey_radio_sw_notify_change();
2998 static void hotkey_exit(void)
3000 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3001 mutex_lock(&hotkey_mutex);
3002 hotkey_poll_stop_sync();
3003 mutex_unlock(&hotkey_mutex);
3004 #endif
3006 if (hotkey_dev_attributes)
3007 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3009 kfree(hotkey_keycode_map);
3011 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3012 "restoring original HKEY status and mask\n");
3013 /* yes, there is a bitwise or below, we want the
3014 * functions to be called even if one of them fail */
3015 if (((tp_features.hotkey_mask &&
3016 hotkey_mask_set(hotkey_orig_mask)) |
3017 hotkey_status_set(false)) != 0)
3018 printk(TPACPI_ERR
3019 "failed to restore hot key mask "
3020 "to BIOS defaults\n");
3023 static void __init hotkey_unmap(const unsigned int scancode)
3025 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3026 clear_bit(hotkey_keycode_map[scancode],
3027 tpacpi_inputdev->keybit);
3028 hotkey_keycode_map[scancode] = KEY_RESERVED;
3033 * HKEY quirks:
3034 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3037 #define TPACPI_HK_Q_INIMASK 0x0001
3039 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3040 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3041 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3042 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3043 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3044 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3045 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3046 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3047 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3048 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3049 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3050 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3051 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3052 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3053 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3054 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3055 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3056 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3057 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3058 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3061 static int __init hotkey_init(struct ibm_init_struct *iibm)
3063 /* Requirements for changing the default keymaps:
3065 * 1. Many of the keys are mapped to KEY_RESERVED for very
3066 * good reasons. Do not change them unless you have deep
3067 * knowledge on the IBM and Lenovo ThinkPad firmware for
3068 * the various ThinkPad models. The driver behaves
3069 * differently for KEY_RESERVED: such keys have their
3070 * hot key mask *unset* in mask_recommended, and also
3071 * in the initial hot key mask programmed into the
3072 * firmware at driver load time, which means the firm-
3073 * ware may react very differently if you change them to
3074 * something else;
3076 * 2. You must be subscribed to the linux-thinkpad and
3077 * ibm-acpi-devel mailing lists, and you should read the
3078 * list archives since 2007 if you want to change the
3079 * keymaps. This requirement exists so that you will
3080 * know the past history of problems with the thinkpad-
3081 * acpi driver keymaps, and also that you will be
3082 * listening to any bug reports;
3084 * 3. Do not send thinkpad-acpi specific patches directly to
3085 * for merging, *ever*. Send them to the linux-acpi
3086 * mailinglist for comments. Merging is to be done only
3087 * through acpi-test and the ACPI maintainer.
3089 * If the above is too much to ask, don't change the keymap.
3090 * Ask the thinkpad-acpi maintainer to do it, instead.
3092 static u16 ibm_keycode_map[] __initdata = {
3093 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3094 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3095 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3096 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3098 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3099 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3100 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3101 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3103 /* brightness: firmware always reacts to them */
3104 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3105 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3107 /* Thinklight: firmware always react to it */
3108 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3110 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3111 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3113 /* Volume: firmware always react to it and reprograms
3114 * the built-in *extra* mixer. Never map it to control
3115 * another mixer by default. */
3116 KEY_RESERVED, /* 0x14: VOLUME UP */
3117 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3118 KEY_RESERVED, /* 0x16: MUTE */
3120 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3122 /* (assignments unknown, please report if found) */
3123 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3124 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3126 static u16 lenovo_keycode_map[] __initdata = {
3127 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3128 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3129 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3130 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3132 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3133 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3134 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3135 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3137 /* These should be enabled --only-- when ACPI video
3138 * is disabled (i.e. in "vendor" mode), and are handled
3139 * in a special way by the init code */
3140 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3141 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3143 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3145 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3146 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3148 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3149 * react to it and reprograms the built-in *extra* mixer.
3150 * Never map it to control another mixer by default.
3152 * T60?, T61, R60?, R61: firmware and EC tries to send
3153 * these over the regular keyboard, so these are no-ops,
3154 * but there are still weird bugs re. MUTE, so do not
3155 * change unless you get test reports from all Lenovo
3156 * models. May cause the BIOS to interfere with the
3157 * HDA mixer.
3159 KEY_RESERVED, /* 0x14: VOLUME UP */
3160 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3161 KEY_RESERVED, /* 0x16: MUTE */
3163 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3165 /* (assignments unknown, please report if found) */
3166 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3167 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3170 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3171 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3172 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3174 int res, i;
3175 int status;
3176 int hkeyv;
3177 bool radiosw_state = false;
3178 bool tabletsw_state = false;
3180 unsigned long quirks;
3182 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3183 "initializing hotkey subdriver\n");
3185 BUG_ON(!tpacpi_inputdev);
3186 BUG_ON(tpacpi_inputdev->open != NULL ||
3187 tpacpi_inputdev->close != NULL);
3189 TPACPI_ACPIHANDLE_INIT(hkey);
3190 mutex_init(&hotkey_mutex);
3192 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3193 mutex_init(&hotkey_thread_mutex);
3194 mutex_init(&hotkey_thread_data_mutex);
3195 #endif
3197 /* hotkey not supported on 570 */
3198 tp_features.hotkey = hkey_handle != NULL;
3200 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3201 "hotkeys are %s\n",
3202 str_supported(tp_features.hotkey));
3204 if (!tp_features.hotkey)
3205 return 1;
3207 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3208 ARRAY_SIZE(tpacpi_hotkey_qtable));
3210 tpacpi_disable_brightness_delay();
3212 /* MUST have enough space for all attributes to be added to
3213 * hotkey_dev_attributes */
3214 hotkey_dev_attributes = create_attr_set(
3215 ARRAY_SIZE(hotkey_attributes) + 2,
3216 NULL);
3217 if (!hotkey_dev_attributes)
3218 return -ENOMEM;
3219 res = add_many_to_attr_set(hotkey_dev_attributes,
3220 hotkey_attributes,
3221 ARRAY_SIZE(hotkey_attributes));
3222 if (res)
3223 goto err_exit;
3225 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3226 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3227 for HKEY interface version 0x100 */
3228 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3229 if ((hkeyv >> 8) != 1) {
3230 printk(TPACPI_ERR "unknown version of the "
3231 "HKEY interface: 0x%x\n", hkeyv);
3232 printk(TPACPI_ERR "please report this to %s\n",
3233 TPACPI_MAIL);
3234 } else {
3236 * MHKV 0x100 in A31, R40, R40e,
3237 * T4x, X31, and later
3239 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3240 "firmware HKEY interface version: 0x%x\n",
3241 hkeyv);
3243 /* Paranoia check AND init hotkey_all_mask */
3244 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3245 "MHKA", "qd")) {
3246 printk(TPACPI_ERR
3247 "missing MHKA handler, "
3248 "please report this to %s\n",
3249 TPACPI_MAIL);
3250 /* Fallback: pre-init for FN+F3,F4,F12 */
3251 hotkey_all_mask = 0x080cU;
3252 } else {
3253 tp_features.hotkey_mask = 1;
3258 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3259 "hotkey masks are %s\n",
3260 str_supported(tp_features.hotkey_mask));
3262 /* Init hotkey_all_mask if not initialized yet */
3263 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3264 (quirks & TPACPI_HK_Q_INIMASK))
3265 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3267 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3268 if (tp_features.hotkey_mask) {
3269 /* hotkey_source_mask *must* be zero for
3270 * the first hotkey_mask_get to return hotkey_orig_mask */
3271 res = hotkey_mask_get();
3272 if (res)
3273 goto err_exit;
3275 hotkey_orig_mask = hotkey_acpi_mask;
3276 } else {
3277 hotkey_orig_mask = hotkey_all_mask;
3278 hotkey_acpi_mask = hotkey_all_mask;
3281 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3282 if (dbg_wlswemul) {
3283 tp_features.hotkey_wlsw = 1;
3284 radiosw_state = !!tpacpi_wlsw_emulstate;
3285 printk(TPACPI_INFO
3286 "radio switch emulation enabled\n");
3287 } else
3288 #endif
3289 /* Not all thinkpads have a hardware radio switch */
3290 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3291 tp_features.hotkey_wlsw = 1;
3292 radiosw_state = !!status;
3293 printk(TPACPI_INFO
3294 "radio switch found; radios are %s\n",
3295 enabled(status, 0));
3297 if (tp_features.hotkey_wlsw)
3298 res = add_to_attr_set(hotkey_dev_attributes,
3299 &dev_attr_hotkey_radio_sw.attr);
3301 /* For X41t, X60t, X61t Tablets... */
3302 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3303 tp_features.hotkey_tablet = 1;
3304 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3305 printk(TPACPI_INFO
3306 "possible tablet mode switch found; "
3307 "ThinkPad in %s mode\n",
3308 (tabletsw_state) ? "tablet" : "laptop");
3309 res = add_to_attr_set(hotkey_dev_attributes,
3310 &dev_attr_hotkey_tablet_mode.attr);
3313 if (!res)
3314 res = register_attr_set_with_sysfs(
3315 hotkey_dev_attributes,
3316 &tpacpi_pdev->dev.kobj);
3317 if (res)
3318 goto err_exit;
3320 /* Set up key map */
3322 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3323 GFP_KERNEL);
3324 if (!hotkey_keycode_map) {
3325 printk(TPACPI_ERR
3326 "failed to allocate memory for key map\n");
3327 res = -ENOMEM;
3328 goto err_exit;
3331 if (tpacpi_is_lenovo()) {
3332 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3333 "using Lenovo default hot key map\n");
3334 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3335 TPACPI_HOTKEY_MAP_SIZE);
3336 } else {
3337 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3338 "using IBM default hot key map\n");
3339 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3340 TPACPI_HOTKEY_MAP_SIZE);
3343 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3344 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3345 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3346 tpacpi_inputdev->keycode = hotkey_keycode_map;
3347 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3348 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3349 input_set_capability(tpacpi_inputdev, EV_KEY,
3350 hotkey_keycode_map[i]);
3351 } else {
3352 if (i < sizeof(hotkey_reserved_mask)*8)
3353 hotkey_reserved_mask |= 1 << i;
3357 if (tp_features.hotkey_wlsw) {
3358 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3359 input_report_switch(tpacpi_inputdev,
3360 SW_RFKILL_ALL, radiosw_state);
3362 if (tp_features.hotkey_tablet) {
3363 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3364 input_report_switch(tpacpi_inputdev,
3365 SW_TABLET_MODE, tabletsw_state);
3368 /* Do not issue duplicate brightness change events to
3369 * userspace. tpacpi_detect_brightness_capabilities() must have
3370 * been called before this point */
3371 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3372 printk(TPACPI_INFO
3373 "This ThinkPad has standard ACPI backlight "
3374 "brightness control, supported by the ACPI "
3375 "video driver\n");
3376 printk(TPACPI_NOTICE
3377 "Disabling thinkpad-acpi brightness events "
3378 "by default...\n");
3380 /* Disable brightness up/down on Lenovo thinkpads when
3381 * ACPI is handling them, otherwise it is plain impossible
3382 * for userspace to do something even remotely sane */
3383 hotkey_reserved_mask |=
3384 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3385 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3386 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3387 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3390 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3391 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3392 & ~hotkey_all_mask
3393 & ~hotkey_reserved_mask;
3395 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3396 "hotkey source mask 0x%08x, polling freq %u\n",
3397 hotkey_source_mask, hotkey_poll_freq);
3398 #endif
3400 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3401 "enabling firmware HKEY event interface...\n");
3402 res = hotkey_status_set(true);
3403 if (res) {
3404 hotkey_exit();
3405 return res;
3407 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3408 | hotkey_driver_mask)
3409 & ~hotkey_source_mask);
3410 if (res < 0 && res != -ENXIO) {
3411 hotkey_exit();
3412 return res;
3414 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3415 & ~hotkey_reserved_mask;
3416 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3417 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3418 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3420 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3421 "legacy ibm/hotkey event reporting over procfs %s\n",
3422 (hotkey_report_mode < 2) ?
3423 "enabled" : "disabled");
3425 tpacpi_inputdev->open = &hotkey_inputdev_open;
3426 tpacpi_inputdev->close = &hotkey_inputdev_close;
3428 hotkey_poll_setup_safe(true);
3430 return 0;
3432 err_exit:
3433 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3434 hotkey_dev_attributes = NULL;
3436 return (res < 0)? res : 1;
3439 static bool hotkey_notify_hotkey(const u32 hkey,
3440 bool *send_acpi_ev,
3441 bool *ignore_acpi_ev)
3443 /* 0x1000-0x1FFF: key presses */
3444 unsigned int scancode = hkey & 0xfff;
3445 *send_acpi_ev = true;
3446 *ignore_acpi_ev = false;
3448 if (scancode > 0 && scancode < 0x21) {
3449 scancode--;
3450 if (!(hotkey_source_mask & (1 << scancode))) {
3451 tpacpi_input_send_key_masked(scancode);
3452 *send_acpi_ev = false;
3453 } else {
3454 *ignore_acpi_ev = true;
3456 return true;
3458 return false;
3461 static bool hotkey_notify_wakeup(const u32 hkey,
3462 bool *send_acpi_ev,
3463 bool *ignore_acpi_ev)
3465 /* 0x2000-0x2FFF: Wakeup reason */
3466 *send_acpi_ev = true;
3467 *ignore_acpi_ev = false;
3469 switch (hkey) {
3470 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3471 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3472 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3473 *ignore_acpi_ev = true;
3474 break;
3476 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3477 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3478 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3479 *ignore_acpi_ev = true;
3480 break;
3482 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3483 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3484 printk(TPACPI_ALERT
3485 "EMERGENCY WAKEUP: battery almost empty\n");
3486 /* how to auto-heal: */
3487 /* 2313: woke up from S3, go to S4/S5 */
3488 /* 2413: woke up from S4, go to S5 */
3489 break;
3491 default:
3492 return false;
3495 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3496 printk(TPACPI_INFO
3497 "woke up due to a hot-unplug "
3498 "request...\n");
3499 hotkey_wakeup_reason_notify_change();
3501 return true;
3504 static bool hotkey_notify_usrevent(const u32 hkey,
3505 bool *send_acpi_ev,
3506 bool *ignore_acpi_ev)
3508 /* 0x5000-0x5FFF: human interface helpers */
3509 *send_acpi_ev = true;
3510 *ignore_acpi_ev = false;
3512 switch (hkey) {
3513 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3514 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3515 return true;
3517 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3518 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3519 tpacpi_input_send_tabletsw();
3520 hotkey_tablet_mode_notify_change();
3521 *send_acpi_ev = false;
3522 return true;
3524 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3525 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3526 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3527 /* do not propagate these events */
3528 *ignore_acpi_ev = true;
3529 return true;
3531 default:
3532 return false;
3536 static void thermal_dump_all_sensors(void);
3538 static bool hotkey_notify_thermal(const u32 hkey,
3539 bool *send_acpi_ev,
3540 bool *ignore_acpi_ev)
3542 bool known = true;
3544 /* 0x6000-0x6FFF: thermal alarms */
3545 *send_acpi_ev = true;
3546 *ignore_acpi_ev = false;
3548 switch (hkey) {
3549 case TP_HKEY_EV_THM_TABLE_CHANGED:
3550 printk(TPACPI_INFO
3551 "EC reports that Thermal Table has changed\n");
3552 /* recommended action: do nothing, we don't have
3553 * Lenovo ATM information */
3554 return true;
3555 case TP_HKEY_EV_ALARM_BAT_HOT:
3556 printk(TPACPI_CRIT
3557 "THERMAL ALARM: battery is too hot!\n");
3558 /* recommended action: warn user through gui */
3559 break;
3560 case TP_HKEY_EV_ALARM_BAT_XHOT:
3561 printk(TPACPI_ALERT
3562 "THERMAL EMERGENCY: battery is extremely hot!\n");
3563 /* recommended action: immediate sleep/hibernate */
3564 break;
3565 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3566 printk(TPACPI_CRIT
3567 "THERMAL ALARM: "
3568 "a sensor reports something is too hot!\n");
3569 /* recommended action: warn user through gui, that */
3570 /* some internal component is too hot */
3571 break;
3572 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3573 printk(TPACPI_ALERT
3574 "THERMAL EMERGENCY: "
3575 "a sensor reports something is extremely hot!\n");
3576 /* recommended action: immediate sleep/hibernate */
3577 break;
3578 default:
3579 printk(TPACPI_ALERT
3580 "THERMAL ALERT: unknown thermal alarm received\n");
3581 known = false;
3584 thermal_dump_all_sensors();
3586 return known;
3589 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3591 u32 hkey;
3592 bool send_acpi_ev;
3593 bool ignore_acpi_ev;
3594 bool known_ev;
3596 if (event != 0x80) {
3597 printk(TPACPI_ERR
3598 "unknown HKEY notification event %d\n", event);
3599 /* forward it to userspace, maybe it knows how to handle it */
3600 acpi_bus_generate_netlink_event(
3601 ibm->acpi->device->pnp.device_class,
3602 dev_name(&ibm->acpi->device->dev),
3603 event, 0);
3604 return;
3607 while (1) {
3608 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3609 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3610 return;
3613 if (hkey == 0) {
3614 /* queue empty */
3615 return;
3618 send_acpi_ev = true;
3619 ignore_acpi_ev = false;
3621 switch (hkey >> 12) {
3622 case 1:
3623 /* 0x1000-0x1FFF: key presses */
3624 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3625 &ignore_acpi_ev);
3626 break;
3627 case 2:
3628 /* 0x2000-0x2FFF: Wakeup reason */
3629 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3630 &ignore_acpi_ev);
3631 break;
3632 case 3:
3633 /* 0x3000-0x3FFF: bay-related wakeups */
3634 switch (hkey) {
3635 case TP_HKEY_EV_BAYEJ_ACK:
3636 hotkey_autosleep_ack = 1;
3637 printk(TPACPI_INFO
3638 "bay ejected\n");
3639 hotkey_wakeup_hotunplug_complete_notify_change();
3640 known_ev = true;
3641 break;
3642 case TP_HKEY_EV_OPTDRV_EJ:
3643 /* FIXME: kick libata if SATA link offline */
3644 known_ev = true;
3645 break;
3646 default:
3647 known_ev = false;
3649 break;
3650 case 4:
3651 /* 0x4000-0x4FFF: dock-related wakeups */
3652 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3653 hotkey_autosleep_ack = 1;
3654 printk(TPACPI_INFO
3655 "undocked\n");
3656 hotkey_wakeup_hotunplug_complete_notify_change();
3657 known_ev = true;
3658 } else {
3659 known_ev = false;
3661 break;
3662 case 5:
3663 /* 0x5000-0x5FFF: human interface helpers */
3664 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3665 &ignore_acpi_ev);
3666 break;
3667 case 6:
3668 /* 0x6000-0x6FFF: thermal alarms */
3669 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3670 &ignore_acpi_ev);
3671 break;
3672 case 7:
3673 /* 0x7000-0x7FFF: misc */
3674 if (tp_features.hotkey_wlsw &&
3675 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3676 tpacpi_send_radiosw_update();
3677 send_acpi_ev = 0;
3678 known_ev = true;
3679 break;
3681 /* fallthrough to default */
3682 default:
3683 known_ev = false;
3685 if (!known_ev) {
3686 printk(TPACPI_NOTICE
3687 "unhandled HKEY event 0x%04x\n", hkey);
3688 printk(TPACPI_NOTICE
3689 "please report the conditions when this "
3690 "event happened to %s\n", TPACPI_MAIL);
3693 /* Legacy events */
3694 if (!ignore_acpi_ev &&
3695 (send_acpi_ev || hotkey_report_mode < 2)) {
3696 acpi_bus_generate_proc_event(ibm->acpi->device,
3697 event, hkey);
3700 /* netlink events */
3701 if (!ignore_acpi_ev && send_acpi_ev) {
3702 acpi_bus_generate_netlink_event(
3703 ibm->acpi->device->pnp.device_class,
3704 dev_name(&ibm->acpi->device->dev),
3705 event, hkey);
3710 static void hotkey_suspend(pm_message_t state)
3712 /* Do these on suspend, we get the events on early resume! */
3713 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3714 hotkey_autosleep_ack = 0;
3717 static void hotkey_resume(void)
3719 tpacpi_disable_brightness_delay();
3721 if (hotkey_status_set(true) < 0 ||
3722 hotkey_mask_set(hotkey_acpi_mask) < 0)
3723 printk(TPACPI_ERR
3724 "error while attempting to reset the event "
3725 "firmware interface\n");
3727 tpacpi_send_radiosw_update();
3728 hotkey_tablet_mode_notify_change();
3729 hotkey_wakeup_reason_notify_change();
3730 hotkey_wakeup_hotunplug_complete_notify_change();
3731 hotkey_poll_setup_safe(false);
3734 /* procfs -------------------------------------------------------------- */
3735 static int hotkey_read(struct seq_file *m)
3737 int res, status;
3739 if (!tp_features.hotkey) {
3740 seq_printf(m, "status:\t\tnot supported\n");
3741 return 0;
3744 if (mutex_lock_killable(&hotkey_mutex))
3745 return -ERESTARTSYS;
3746 res = hotkey_status_get(&status);
3747 if (!res)
3748 res = hotkey_mask_get();
3749 mutex_unlock(&hotkey_mutex);
3750 if (res)
3751 return res;
3753 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3754 if (hotkey_all_mask) {
3755 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3756 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3757 } else {
3758 seq_printf(m, "mask:\t\tnot supported\n");
3759 seq_printf(m, "commands:\tenable, disable, reset\n");
3762 return 0;
3765 static void hotkey_enabledisable_warn(bool enable)
3767 tpacpi_log_usertask("procfs hotkey enable/disable");
3768 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3769 TPACPI_WARN
3770 "hotkey enable/disable functionality has been "
3771 "removed from the driver. Hotkeys are always "
3772 "enabled\n"))
3773 printk(TPACPI_ERR
3774 "Please remove the hotkey=enable module "
3775 "parameter, it is deprecated. Hotkeys are always "
3776 "enabled\n");
3779 static int hotkey_write(char *buf)
3781 int res;
3782 u32 mask;
3783 char *cmd;
3785 if (!tp_features.hotkey)
3786 return -ENODEV;
3788 if (mutex_lock_killable(&hotkey_mutex))
3789 return -ERESTARTSYS;
3791 mask = hotkey_user_mask;
3793 res = 0;
3794 while ((cmd = next_cmd(&buf))) {
3795 if (strlencmp(cmd, "enable") == 0) {
3796 hotkey_enabledisable_warn(1);
3797 } else if (strlencmp(cmd, "disable") == 0) {
3798 hotkey_enabledisable_warn(0);
3799 res = -EPERM;
3800 } else if (strlencmp(cmd, "reset") == 0) {
3801 mask = (hotkey_all_mask | hotkey_source_mask)
3802 & ~hotkey_reserved_mask;
3803 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3804 /* mask set */
3805 } else if (sscanf(cmd, "%x", &mask) == 1) {
3806 /* mask set */
3807 } else {
3808 res = -EINVAL;
3809 goto errexit;
3813 if (!res) {
3814 tpacpi_disclose_usertask("procfs hotkey",
3815 "set mask to 0x%08x\n", mask);
3816 res = hotkey_user_mask_set(mask);
3819 errexit:
3820 mutex_unlock(&hotkey_mutex);
3821 return res;
3824 static const struct acpi_device_id ibm_htk_device_ids[] = {
3825 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3826 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
3827 {"", 0},
3830 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3831 .hid = ibm_htk_device_ids,
3832 .notify = hotkey_notify,
3833 .handle = &hkey_handle,
3834 .type = ACPI_DEVICE_NOTIFY,
3837 static struct ibm_struct hotkey_driver_data = {
3838 .name = "hotkey",
3839 .read = hotkey_read,
3840 .write = hotkey_write,
3841 .exit = hotkey_exit,
3842 .resume = hotkey_resume,
3843 .suspend = hotkey_suspend,
3844 .acpi = &ibm_hotkey_acpidriver,
3847 /*************************************************************************
3848 * Bluetooth subdriver
3851 enum {
3852 /* ACPI GBDC/SBDC bits */
3853 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3854 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3855 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3856 0 = disable, 1 = enable */
3859 enum {
3860 /* ACPI \BLTH commands */
3861 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3862 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3863 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3864 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3865 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3868 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3870 static int bluetooth_get_status(void)
3872 int status;
3874 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3875 if (dbg_bluetoothemul)
3876 return (tpacpi_bluetooth_emulstate) ?
3877 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3878 #endif
3880 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3881 return -EIO;
3883 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3884 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3887 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3889 int status;
3891 vdbg_printk(TPACPI_DBG_RFKILL,
3892 "will attempt to %s bluetooth\n",
3893 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3895 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3896 if (dbg_bluetoothemul) {
3897 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3898 return 0;
3900 #endif
3902 if (state == TPACPI_RFK_RADIO_ON)
3903 status = TP_ACPI_BLUETOOTH_RADIOSSW
3904 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3905 else
3906 status = 0;
3908 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3909 return -EIO;
3911 return 0;
3914 /* sysfs bluetooth enable ---------------------------------------------- */
3915 static ssize_t bluetooth_enable_show(struct device *dev,
3916 struct device_attribute *attr,
3917 char *buf)
3919 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3920 attr, buf);
3923 static ssize_t bluetooth_enable_store(struct device *dev,
3924 struct device_attribute *attr,
3925 const char *buf, size_t count)
3927 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3928 attr, buf, count);
3931 static struct device_attribute dev_attr_bluetooth_enable =
3932 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3933 bluetooth_enable_show, bluetooth_enable_store);
3935 /* --------------------------------------------------------------------- */
3937 static struct attribute *bluetooth_attributes[] = {
3938 &dev_attr_bluetooth_enable.attr,
3939 NULL
3942 static const struct attribute_group bluetooth_attr_group = {
3943 .attrs = bluetooth_attributes,
3946 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3947 .get_status = bluetooth_get_status,
3948 .set_status = bluetooth_set_status,
3951 static void bluetooth_shutdown(void)
3953 /* Order firmware to save current state to NVRAM */
3954 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3955 TP_ACPI_BLTH_SAVE_STATE))
3956 printk(TPACPI_NOTICE
3957 "failed to save bluetooth state to NVRAM\n");
3958 else
3959 vdbg_printk(TPACPI_DBG_RFKILL,
3960 "bluestooth state saved to NVRAM\n");
3963 static void bluetooth_exit(void)
3965 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3966 &bluetooth_attr_group);
3968 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3970 bluetooth_shutdown();
3973 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3975 int res;
3976 int status = 0;
3978 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3979 "initializing bluetooth subdriver\n");
3981 TPACPI_ACPIHANDLE_INIT(hkey);
3983 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3984 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3985 tp_features.bluetooth = hkey_handle &&
3986 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3988 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3989 "bluetooth is %s, status 0x%02x\n",
3990 str_supported(tp_features.bluetooth),
3991 status);
3993 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3994 if (dbg_bluetoothemul) {
3995 tp_features.bluetooth = 1;
3996 printk(TPACPI_INFO
3997 "bluetooth switch emulation enabled\n");
3998 } else
3999 #endif
4000 if (tp_features.bluetooth &&
4001 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4002 /* no bluetooth hardware present in system */
4003 tp_features.bluetooth = 0;
4004 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4005 "bluetooth hardware not installed\n");
4008 if (!tp_features.bluetooth)
4009 return 1;
4011 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4012 &bluetooth_tprfk_ops,
4013 RFKILL_TYPE_BLUETOOTH,
4014 TPACPI_RFK_BLUETOOTH_SW_NAME,
4015 true);
4016 if (res)
4017 return res;
4019 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4020 &bluetooth_attr_group);
4021 if (res) {
4022 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4023 return res;
4026 return 0;
4029 /* procfs -------------------------------------------------------------- */
4030 static int bluetooth_read(struct seq_file *m)
4032 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4035 static int bluetooth_write(char *buf)
4037 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4040 static struct ibm_struct bluetooth_driver_data = {
4041 .name = "bluetooth",
4042 .read = bluetooth_read,
4043 .write = bluetooth_write,
4044 .exit = bluetooth_exit,
4045 .shutdown = bluetooth_shutdown,
4048 /*************************************************************************
4049 * Wan subdriver
4052 enum {
4053 /* ACPI GWAN/SWAN bits */
4054 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4055 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4056 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4057 0 = disable, 1 = enable */
4060 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4062 static int wan_get_status(void)
4064 int status;
4066 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4067 if (dbg_wwanemul)
4068 return (tpacpi_wwan_emulstate) ?
4069 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4070 #endif
4072 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4073 return -EIO;
4075 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4076 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4079 static int wan_set_status(enum tpacpi_rfkill_state state)
4081 int status;
4083 vdbg_printk(TPACPI_DBG_RFKILL,
4084 "will attempt to %s wwan\n",
4085 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4087 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4088 if (dbg_wwanemul) {
4089 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4090 return 0;
4092 #endif
4094 if (state == TPACPI_RFK_RADIO_ON)
4095 status = TP_ACPI_WANCARD_RADIOSSW
4096 | TP_ACPI_WANCARD_RESUMECTRL;
4097 else
4098 status = 0;
4100 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4101 return -EIO;
4103 return 0;
4106 /* sysfs wan enable ---------------------------------------------------- */
4107 static ssize_t wan_enable_show(struct device *dev,
4108 struct device_attribute *attr,
4109 char *buf)
4111 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4112 attr, buf);
4115 static ssize_t wan_enable_store(struct device *dev,
4116 struct device_attribute *attr,
4117 const char *buf, size_t count)
4119 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4120 attr, buf, count);
4123 static struct device_attribute dev_attr_wan_enable =
4124 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4125 wan_enable_show, wan_enable_store);
4127 /* --------------------------------------------------------------------- */
4129 static struct attribute *wan_attributes[] = {
4130 &dev_attr_wan_enable.attr,
4131 NULL
4134 static const struct attribute_group wan_attr_group = {
4135 .attrs = wan_attributes,
4138 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4139 .get_status = wan_get_status,
4140 .set_status = wan_set_status,
4143 static void wan_shutdown(void)
4145 /* Order firmware to save current state to NVRAM */
4146 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4147 TP_ACPI_WGSV_SAVE_STATE))
4148 printk(TPACPI_NOTICE
4149 "failed to save WWAN state to NVRAM\n");
4150 else
4151 vdbg_printk(TPACPI_DBG_RFKILL,
4152 "WWAN state saved to NVRAM\n");
4155 static void wan_exit(void)
4157 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4158 &wan_attr_group);
4160 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4162 wan_shutdown();
4165 static int __init wan_init(struct ibm_init_struct *iibm)
4167 int res;
4168 int status = 0;
4170 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4171 "initializing wan subdriver\n");
4173 TPACPI_ACPIHANDLE_INIT(hkey);
4175 tp_features.wan = hkey_handle &&
4176 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4178 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4179 "wan is %s, status 0x%02x\n",
4180 str_supported(tp_features.wan),
4181 status);
4183 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4184 if (dbg_wwanemul) {
4185 tp_features.wan = 1;
4186 printk(TPACPI_INFO
4187 "wwan switch emulation enabled\n");
4188 } else
4189 #endif
4190 if (tp_features.wan &&
4191 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4192 /* no wan hardware present in system */
4193 tp_features.wan = 0;
4194 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4195 "wan hardware not installed\n");
4198 if (!tp_features.wan)
4199 return 1;
4201 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4202 &wan_tprfk_ops,
4203 RFKILL_TYPE_WWAN,
4204 TPACPI_RFK_WWAN_SW_NAME,
4205 true);
4206 if (res)
4207 return res;
4209 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4210 &wan_attr_group);
4212 if (res) {
4213 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4214 return res;
4217 return 0;
4220 /* procfs -------------------------------------------------------------- */
4221 static int wan_read(struct seq_file *m)
4223 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4226 static int wan_write(char *buf)
4228 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4231 static struct ibm_struct wan_driver_data = {
4232 .name = "wan",
4233 .read = wan_read,
4234 .write = wan_write,
4235 .exit = wan_exit,
4236 .shutdown = wan_shutdown,
4239 /*************************************************************************
4240 * UWB subdriver
4243 enum {
4244 /* ACPI GUWB/SUWB bits */
4245 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4246 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4249 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4251 static int uwb_get_status(void)
4253 int status;
4255 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4256 if (dbg_uwbemul)
4257 return (tpacpi_uwb_emulstate) ?
4258 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4259 #endif
4261 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4262 return -EIO;
4264 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4265 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4268 static int uwb_set_status(enum tpacpi_rfkill_state state)
4270 int status;
4272 vdbg_printk(TPACPI_DBG_RFKILL,
4273 "will attempt to %s UWB\n",
4274 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4276 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4277 if (dbg_uwbemul) {
4278 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4279 return 0;
4281 #endif
4283 if (state == TPACPI_RFK_RADIO_ON)
4284 status = TP_ACPI_UWB_RADIOSSW;
4285 else
4286 status = 0;
4288 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4289 return -EIO;
4291 return 0;
4294 /* --------------------------------------------------------------------- */
4296 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4297 .get_status = uwb_get_status,
4298 .set_status = uwb_set_status,
4301 static void uwb_exit(void)
4303 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4306 static int __init uwb_init(struct ibm_init_struct *iibm)
4308 int res;
4309 int status = 0;
4311 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4312 "initializing uwb subdriver\n");
4314 TPACPI_ACPIHANDLE_INIT(hkey);
4316 tp_features.uwb = hkey_handle &&
4317 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4319 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4320 "uwb is %s, status 0x%02x\n",
4321 str_supported(tp_features.uwb),
4322 status);
4324 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4325 if (dbg_uwbemul) {
4326 tp_features.uwb = 1;
4327 printk(TPACPI_INFO
4328 "uwb switch emulation enabled\n");
4329 } else
4330 #endif
4331 if (tp_features.uwb &&
4332 !(status & TP_ACPI_UWB_HWPRESENT)) {
4333 /* no uwb hardware present in system */
4334 tp_features.uwb = 0;
4335 dbg_printk(TPACPI_DBG_INIT,
4336 "uwb hardware not installed\n");
4339 if (!tp_features.uwb)
4340 return 1;
4342 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4343 &uwb_tprfk_ops,
4344 RFKILL_TYPE_UWB,
4345 TPACPI_RFK_UWB_SW_NAME,
4346 false);
4347 return res;
4350 static struct ibm_struct uwb_driver_data = {
4351 .name = "uwb",
4352 .exit = uwb_exit,
4353 .flags.experimental = 1,
4356 /*************************************************************************
4357 * Video subdriver
4360 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4362 enum video_access_mode {
4363 TPACPI_VIDEO_NONE = 0,
4364 TPACPI_VIDEO_570, /* 570 */
4365 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4366 TPACPI_VIDEO_NEW, /* all others */
4369 enum { /* video status flags, based on VIDEO_570 */
4370 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4371 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4372 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4375 enum { /* TPACPI_VIDEO_570 constants */
4376 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4377 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4378 * video_status_flags */
4379 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4380 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4383 static enum video_access_mode video_supported;
4384 static int video_orig_autosw;
4386 static int video_autosw_get(void);
4387 static int video_autosw_set(int enable);
4389 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4391 static int __init video_init(struct ibm_init_struct *iibm)
4393 int ivga;
4395 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4397 TPACPI_ACPIHANDLE_INIT(vid);
4398 if (tpacpi_is_ibm())
4399 TPACPI_ACPIHANDLE_INIT(vid2);
4401 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4402 /* G41, assume IVGA doesn't change */
4403 vid_handle = vid2_handle;
4405 if (!vid_handle)
4406 /* video switching not supported on R30, R31 */
4407 video_supported = TPACPI_VIDEO_NONE;
4408 else if (tpacpi_is_ibm() &&
4409 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4410 /* 570 */
4411 video_supported = TPACPI_VIDEO_570;
4412 else if (tpacpi_is_ibm() &&
4413 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4414 /* 600e/x, 770e, 770x */
4415 video_supported = TPACPI_VIDEO_770;
4416 else
4417 /* all others */
4418 video_supported = TPACPI_VIDEO_NEW;
4420 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4421 str_supported(video_supported != TPACPI_VIDEO_NONE),
4422 video_supported);
4424 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4427 static void video_exit(void)
4429 dbg_printk(TPACPI_DBG_EXIT,
4430 "restoring original video autoswitch mode\n");
4431 if (video_autosw_set(video_orig_autosw))
4432 printk(TPACPI_ERR "error while trying to restore original "
4433 "video autoswitch mode\n");
4436 static int video_outputsw_get(void)
4438 int status = 0;
4439 int i;
4441 switch (video_supported) {
4442 case TPACPI_VIDEO_570:
4443 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4444 TP_ACPI_VIDEO_570_PHSCMD))
4445 return -EIO;
4446 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4447 break;
4448 case TPACPI_VIDEO_770:
4449 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4450 return -EIO;
4451 if (i)
4452 status |= TP_ACPI_VIDEO_S_LCD;
4453 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4454 return -EIO;
4455 if (i)
4456 status |= TP_ACPI_VIDEO_S_CRT;
4457 break;
4458 case TPACPI_VIDEO_NEW:
4459 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4460 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4461 return -EIO;
4462 if (i)
4463 status |= TP_ACPI_VIDEO_S_CRT;
4465 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4466 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4467 return -EIO;
4468 if (i)
4469 status |= TP_ACPI_VIDEO_S_LCD;
4470 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4471 return -EIO;
4472 if (i)
4473 status |= TP_ACPI_VIDEO_S_DVI;
4474 break;
4475 default:
4476 return -ENOSYS;
4479 return status;
4482 static int video_outputsw_set(int status)
4484 int autosw;
4485 int res = 0;
4487 switch (video_supported) {
4488 case TPACPI_VIDEO_570:
4489 res = acpi_evalf(NULL, NULL,
4490 "\\_SB.PHS2", "vdd",
4491 TP_ACPI_VIDEO_570_PHS2CMD,
4492 status | TP_ACPI_VIDEO_570_PHS2SET);
4493 break;
4494 case TPACPI_VIDEO_770:
4495 autosw = video_autosw_get();
4496 if (autosw < 0)
4497 return autosw;
4499 res = video_autosw_set(1);
4500 if (res)
4501 return res;
4502 res = acpi_evalf(vid_handle, NULL,
4503 "ASWT", "vdd", status * 0x100, 0);
4504 if (!autosw && video_autosw_set(autosw)) {
4505 printk(TPACPI_ERR
4506 "video auto-switch left enabled due to error\n");
4507 return -EIO;
4509 break;
4510 case TPACPI_VIDEO_NEW:
4511 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4512 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4513 break;
4514 default:
4515 return -ENOSYS;
4518 return (res)? 0 : -EIO;
4521 static int video_autosw_get(void)
4523 int autosw = 0;
4525 switch (video_supported) {
4526 case TPACPI_VIDEO_570:
4527 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4528 return -EIO;
4529 break;
4530 case TPACPI_VIDEO_770:
4531 case TPACPI_VIDEO_NEW:
4532 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4533 return -EIO;
4534 break;
4535 default:
4536 return -ENOSYS;
4539 return autosw & 1;
4542 static int video_autosw_set(int enable)
4544 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4545 return -EIO;
4546 return 0;
4549 static int video_outputsw_cycle(void)
4551 int autosw = video_autosw_get();
4552 int res;
4554 if (autosw < 0)
4555 return autosw;
4557 switch (video_supported) {
4558 case TPACPI_VIDEO_570:
4559 res = video_autosw_set(1);
4560 if (res)
4561 return res;
4562 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4563 break;
4564 case TPACPI_VIDEO_770:
4565 case TPACPI_VIDEO_NEW:
4566 res = video_autosw_set(1);
4567 if (res)
4568 return res;
4569 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4570 break;
4571 default:
4572 return -ENOSYS;
4574 if (!autosw && video_autosw_set(autosw)) {
4575 printk(TPACPI_ERR
4576 "video auto-switch left enabled due to error\n");
4577 return -EIO;
4580 return (res)? 0 : -EIO;
4583 static int video_expand_toggle(void)
4585 switch (video_supported) {
4586 case TPACPI_VIDEO_570:
4587 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4588 0 : -EIO;
4589 case TPACPI_VIDEO_770:
4590 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4591 0 : -EIO;
4592 case TPACPI_VIDEO_NEW:
4593 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4594 0 : -EIO;
4595 default:
4596 return -ENOSYS;
4598 /* not reached */
4601 static int video_read(struct seq_file *m)
4603 int status, autosw;
4605 if (video_supported == TPACPI_VIDEO_NONE) {
4606 seq_printf(m, "status:\t\tnot supported\n");
4607 return 0;
4610 /* Even reads can crash X.org, so... */
4611 if (!capable(CAP_SYS_ADMIN))
4612 return -EPERM;
4614 status = video_outputsw_get();
4615 if (status < 0)
4616 return status;
4618 autosw = video_autosw_get();
4619 if (autosw < 0)
4620 return autosw;
4622 seq_printf(m, "status:\t\tsupported\n");
4623 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4624 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4625 if (video_supported == TPACPI_VIDEO_NEW)
4626 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4627 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4628 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4629 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4630 if (video_supported == TPACPI_VIDEO_NEW)
4631 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4632 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4633 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4635 return 0;
4638 static int video_write(char *buf)
4640 char *cmd;
4641 int enable, disable, status;
4642 int res;
4644 if (video_supported == TPACPI_VIDEO_NONE)
4645 return -ENODEV;
4647 /* Even reads can crash X.org, let alone writes... */
4648 if (!capable(CAP_SYS_ADMIN))
4649 return -EPERM;
4651 enable = 0;
4652 disable = 0;
4654 while ((cmd = next_cmd(&buf))) {
4655 if (strlencmp(cmd, "lcd_enable") == 0) {
4656 enable |= TP_ACPI_VIDEO_S_LCD;
4657 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4658 disable |= TP_ACPI_VIDEO_S_LCD;
4659 } else if (strlencmp(cmd, "crt_enable") == 0) {
4660 enable |= TP_ACPI_VIDEO_S_CRT;
4661 } else if (strlencmp(cmd, "crt_disable") == 0) {
4662 disable |= TP_ACPI_VIDEO_S_CRT;
4663 } else if (video_supported == TPACPI_VIDEO_NEW &&
4664 strlencmp(cmd, "dvi_enable") == 0) {
4665 enable |= TP_ACPI_VIDEO_S_DVI;
4666 } else if (video_supported == TPACPI_VIDEO_NEW &&
4667 strlencmp(cmd, "dvi_disable") == 0) {
4668 disable |= TP_ACPI_VIDEO_S_DVI;
4669 } else if (strlencmp(cmd, "auto_enable") == 0) {
4670 res = video_autosw_set(1);
4671 if (res)
4672 return res;
4673 } else if (strlencmp(cmd, "auto_disable") == 0) {
4674 res = video_autosw_set(0);
4675 if (res)
4676 return res;
4677 } else if (strlencmp(cmd, "video_switch") == 0) {
4678 res = video_outputsw_cycle();
4679 if (res)
4680 return res;
4681 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4682 res = video_expand_toggle();
4683 if (res)
4684 return res;
4685 } else
4686 return -EINVAL;
4689 if (enable || disable) {
4690 status = video_outputsw_get();
4691 if (status < 0)
4692 return status;
4693 res = video_outputsw_set((status & ~disable) | enable);
4694 if (res)
4695 return res;
4698 return 0;
4701 static struct ibm_struct video_driver_data = {
4702 .name = "video",
4703 .read = video_read,
4704 .write = video_write,
4705 .exit = video_exit,
4708 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4710 /*************************************************************************
4711 * Light (thinklight) subdriver
4714 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4715 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4717 static int light_get_status(void)
4719 int status = 0;
4721 if (tp_features.light_status) {
4722 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4723 return -EIO;
4724 return (!!status);
4727 return -ENXIO;
4730 static int light_set_status(int status)
4732 int rc;
4734 if (tp_features.light) {
4735 if (cmos_handle) {
4736 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4737 (status)?
4738 TP_CMOS_THINKLIGHT_ON :
4739 TP_CMOS_THINKLIGHT_OFF);
4740 } else {
4741 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4742 (status)? 1 : 0);
4744 return (rc)? 0 : -EIO;
4747 return -ENXIO;
4750 static void light_set_status_worker(struct work_struct *work)
4752 struct tpacpi_led_classdev *data =
4753 container_of(work, struct tpacpi_led_classdev, work);
4755 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4756 light_set_status((data->new_state != TPACPI_LED_OFF));
4759 static void light_sysfs_set(struct led_classdev *led_cdev,
4760 enum led_brightness brightness)
4762 struct tpacpi_led_classdev *data =
4763 container_of(led_cdev,
4764 struct tpacpi_led_classdev,
4765 led_classdev);
4766 data->new_state = (brightness != LED_OFF) ?
4767 TPACPI_LED_ON : TPACPI_LED_OFF;
4768 queue_work(tpacpi_wq, &data->work);
4771 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4773 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4776 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4777 .led_classdev = {
4778 .name = "tpacpi::thinklight",
4779 .brightness_set = &light_sysfs_set,
4780 .brightness_get = &light_sysfs_get,
4784 static int __init light_init(struct ibm_init_struct *iibm)
4786 int rc;
4788 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4790 if (tpacpi_is_ibm()) {
4791 TPACPI_ACPIHANDLE_INIT(ledb);
4792 TPACPI_ACPIHANDLE_INIT(lght);
4794 TPACPI_ACPIHANDLE_INIT(cmos);
4795 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4797 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4798 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4800 if (tp_features.light)
4801 /* light status not supported on
4802 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4803 tp_features.light_status =
4804 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4806 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4807 str_supported(tp_features.light),
4808 str_supported(tp_features.light_status));
4810 if (!tp_features.light)
4811 return 1;
4813 rc = led_classdev_register(&tpacpi_pdev->dev,
4814 &tpacpi_led_thinklight.led_classdev);
4816 if (rc < 0) {
4817 tp_features.light = 0;
4818 tp_features.light_status = 0;
4819 } else {
4820 rc = 0;
4823 return rc;
4826 static void light_exit(void)
4828 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4829 if (work_pending(&tpacpi_led_thinklight.work))
4830 flush_workqueue(tpacpi_wq);
4833 static int light_read(struct seq_file *m)
4835 int status;
4837 if (!tp_features.light) {
4838 seq_printf(m, "status:\t\tnot supported\n");
4839 } else if (!tp_features.light_status) {
4840 seq_printf(m, "status:\t\tunknown\n");
4841 seq_printf(m, "commands:\ton, off\n");
4842 } else {
4843 status = light_get_status();
4844 if (status < 0)
4845 return status;
4846 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4847 seq_printf(m, "commands:\ton, off\n");
4850 return 0;
4853 static int light_write(char *buf)
4855 char *cmd;
4856 int newstatus = 0;
4858 if (!tp_features.light)
4859 return -ENODEV;
4861 while ((cmd = next_cmd(&buf))) {
4862 if (strlencmp(cmd, "on") == 0) {
4863 newstatus = 1;
4864 } else if (strlencmp(cmd, "off") == 0) {
4865 newstatus = 0;
4866 } else
4867 return -EINVAL;
4870 return light_set_status(newstatus);
4873 static struct ibm_struct light_driver_data = {
4874 .name = "light",
4875 .read = light_read,
4876 .write = light_write,
4877 .exit = light_exit,
4880 /*************************************************************************
4881 * CMOS subdriver
4884 /* sysfs cmos_command -------------------------------------------------- */
4885 static ssize_t cmos_command_store(struct device *dev,
4886 struct device_attribute *attr,
4887 const char *buf, size_t count)
4889 unsigned long cmos_cmd;
4890 int res;
4892 if (parse_strtoul(buf, 21, &cmos_cmd))
4893 return -EINVAL;
4895 res = issue_thinkpad_cmos_command(cmos_cmd);
4896 return (res)? res : count;
4899 static struct device_attribute dev_attr_cmos_command =
4900 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4902 /* --------------------------------------------------------------------- */
4904 static int __init cmos_init(struct ibm_init_struct *iibm)
4906 int res;
4908 vdbg_printk(TPACPI_DBG_INIT,
4909 "initializing cmos commands subdriver\n");
4911 TPACPI_ACPIHANDLE_INIT(cmos);
4913 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4914 str_supported(cmos_handle != NULL));
4916 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4917 if (res)
4918 return res;
4920 return (cmos_handle)? 0 : 1;
4923 static void cmos_exit(void)
4925 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4928 static int cmos_read(struct seq_file *m)
4930 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4931 R30, R31, T20-22, X20-21 */
4932 if (!cmos_handle)
4933 seq_printf(m, "status:\t\tnot supported\n");
4934 else {
4935 seq_printf(m, "status:\t\tsupported\n");
4936 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4939 return 0;
4942 static int cmos_write(char *buf)
4944 char *cmd;
4945 int cmos_cmd, res;
4947 while ((cmd = next_cmd(&buf))) {
4948 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4949 cmos_cmd >= 0 && cmos_cmd <= 21) {
4950 /* cmos_cmd set */
4951 } else
4952 return -EINVAL;
4954 res = issue_thinkpad_cmos_command(cmos_cmd);
4955 if (res)
4956 return res;
4959 return 0;
4962 static struct ibm_struct cmos_driver_data = {
4963 .name = "cmos",
4964 .read = cmos_read,
4965 .write = cmos_write,
4966 .exit = cmos_exit,
4969 /*************************************************************************
4970 * LED subdriver
4973 enum led_access_mode {
4974 TPACPI_LED_NONE = 0,
4975 TPACPI_LED_570, /* 570 */
4976 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4977 TPACPI_LED_NEW, /* all others */
4980 enum { /* For TPACPI_LED_OLD */
4981 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4982 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4983 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4986 static enum led_access_mode led_supported;
4988 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4989 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4990 /* T20-22, X20-21 */
4991 "LED", /* all others */
4992 ); /* R30, R31 */
4994 #define TPACPI_LED_NUMLEDS 16
4995 static struct tpacpi_led_classdev *tpacpi_leds;
4996 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4997 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4998 /* there's a limit of 19 chars + NULL before 2.6.26 */
4999 "tpacpi::power",
5000 "tpacpi:orange:batt",
5001 "tpacpi:green:batt",
5002 "tpacpi::dock_active",
5003 "tpacpi::bay_active",
5004 "tpacpi::dock_batt",
5005 "tpacpi::unknown_led",
5006 "tpacpi::standby",
5007 "tpacpi::dock_status1",
5008 "tpacpi::dock_status2",
5009 "tpacpi::unknown_led2",
5010 "tpacpi::unknown_led3",
5011 "tpacpi::thinkvantage",
5013 #define TPACPI_SAFE_LEDS 0x1081U
5015 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5017 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5018 return false;
5019 #else
5020 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5021 #endif
5024 static int led_get_status(const unsigned int led)
5026 int status;
5027 enum led_status_t led_s;
5029 switch (led_supported) {
5030 case TPACPI_LED_570:
5031 if (!acpi_evalf(ec_handle,
5032 &status, "GLED", "dd", 1 << led))
5033 return -EIO;
5034 led_s = (status == 0)?
5035 TPACPI_LED_OFF :
5036 ((status == 1)?
5037 TPACPI_LED_ON :
5038 TPACPI_LED_BLINK);
5039 tpacpi_led_state_cache[led] = led_s;
5040 return led_s;
5041 default:
5042 return -ENXIO;
5045 /* not reached */
5048 static int led_set_status(const unsigned int led,
5049 const enum led_status_t ledstatus)
5051 /* off, on, blink. Index is led_status_t */
5052 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5053 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5055 int rc = 0;
5057 switch (led_supported) {
5058 case TPACPI_LED_570:
5059 /* 570 */
5060 if (unlikely(led > 7))
5061 return -EINVAL;
5062 if (unlikely(tpacpi_is_led_restricted(led)))
5063 return -EPERM;
5064 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5065 (1 << led), led_sled_arg1[ledstatus]))
5066 rc = -EIO;
5067 break;
5068 case TPACPI_LED_OLD:
5069 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5070 if (unlikely(led > 7))
5071 return -EINVAL;
5072 if (unlikely(tpacpi_is_led_restricted(led)))
5073 return -EPERM;
5074 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5075 if (rc >= 0)
5076 rc = ec_write(TPACPI_LED_EC_HLBL,
5077 (ledstatus == TPACPI_LED_BLINK) << led);
5078 if (rc >= 0)
5079 rc = ec_write(TPACPI_LED_EC_HLCL,
5080 (ledstatus != TPACPI_LED_OFF) << led);
5081 break;
5082 case TPACPI_LED_NEW:
5083 /* all others */
5084 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5085 return -EINVAL;
5086 if (unlikely(tpacpi_is_led_restricted(led)))
5087 return -EPERM;
5088 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5089 led, led_led_arg1[ledstatus]))
5090 rc = -EIO;
5091 break;
5092 default:
5093 rc = -ENXIO;
5096 if (!rc)
5097 tpacpi_led_state_cache[led] = ledstatus;
5099 return rc;
5102 static void led_set_status_worker(struct work_struct *work)
5104 struct tpacpi_led_classdev *data =
5105 container_of(work, struct tpacpi_led_classdev, work);
5107 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5108 led_set_status(data->led, data->new_state);
5111 static void led_sysfs_set(struct led_classdev *led_cdev,
5112 enum led_brightness brightness)
5114 struct tpacpi_led_classdev *data = container_of(led_cdev,
5115 struct tpacpi_led_classdev, led_classdev);
5117 if (brightness == LED_OFF)
5118 data->new_state = TPACPI_LED_OFF;
5119 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5120 data->new_state = TPACPI_LED_ON;
5121 else
5122 data->new_state = TPACPI_LED_BLINK;
5124 queue_work(tpacpi_wq, &data->work);
5127 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5128 unsigned long *delay_on, unsigned long *delay_off)
5130 struct tpacpi_led_classdev *data = container_of(led_cdev,
5131 struct tpacpi_led_classdev, led_classdev);
5133 /* Can we choose the flash rate? */
5134 if (*delay_on == 0 && *delay_off == 0) {
5135 /* yes. set them to the hardware blink rate (1 Hz) */
5136 *delay_on = 500; /* ms */
5137 *delay_off = 500; /* ms */
5138 } else if ((*delay_on != 500) || (*delay_off != 500))
5139 return -EINVAL;
5141 data->new_state = TPACPI_LED_BLINK;
5142 queue_work(tpacpi_wq, &data->work);
5144 return 0;
5147 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5149 int rc;
5151 struct tpacpi_led_classdev *data = container_of(led_cdev,
5152 struct tpacpi_led_classdev, led_classdev);
5154 rc = led_get_status(data->led);
5156 if (rc == TPACPI_LED_OFF || rc < 0)
5157 rc = LED_OFF; /* no error handling in led class :( */
5158 else
5159 rc = LED_FULL;
5161 return rc;
5164 static void led_exit(void)
5166 unsigned int i;
5168 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5169 if (tpacpi_leds[i].led_classdev.name)
5170 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5173 kfree(tpacpi_leds);
5176 static int __init tpacpi_init_led(unsigned int led)
5178 int rc;
5180 tpacpi_leds[led].led = led;
5182 /* LEDs with no name don't get registered */
5183 if (!tpacpi_led_names[led])
5184 return 0;
5186 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5187 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5188 if (led_supported == TPACPI_LED_570)
5189 tpacpi_leds[led].led_classdev.brightness_get =
5190 &led_sysfs_get;
5192 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5194 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5196 rc = led_classdev_register(&tpacpi_pdev->dev,
5197 &tpacpi_leds[led].led_classdev);
5198 if (rc < 0)
5199 tpacpi_leds[led].led_classdev.name = NULL;
5201 return rc;
5204 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5205 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5206 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5207 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5209 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5210 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5211 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5212 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5213 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5214 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5215 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5216 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5218 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5219 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5220 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5221 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5222 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5224 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5225 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5226 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5227 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5229 /* (1) - may have excess leds enabled on MSB */
5231 /* Defaults (order matters, keep last, don't reorder!) */
5232 { /* Lenovo */
5233 .vendor = PCI_VENDOR_ID_LENOVO,
5234 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5235 .quirks = 0x1fffU,
5237 { /* IBM ThinkPads with no EC version string */
5238 .vendor = PCI_VENDOR_ID_IBM,
5239 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5240 .quirks = 0x00ffU,
5242 { /* IBM ThinkPads with EC version string */
5243 .vendor = PCI_VENDOR_ID_IBM,
5244 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5245 .quirks = 0x00bfU,
5249 #undef TPACPI_LEDQ_IBM
5250 #undef TPACPI_LEDQ_LNV
5252 static int __init led_init(struct ibm_init_struct *iibm)
5254 unsigned int i;
5255 int rc;
5256 unsigned long useful_leds;
5258 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5260 TPACPI_ACPIHANDLE_INIT(led);
5262 if (!led_handle)
5263 /* led not supported on R30, R31 */
5264 led_supported = TPACPI_LED_NONE;
5265 else if (tpacpi_is_ibm() && strlencmp(led_path, "SLED") == 0)
5266 /* 570 */
5267 led_supported = TPACPI_LED_570;
5268 else if (tpacpi_is_ibm() && strlencmp(led_path, "SYSL") == 0)
5269 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5270 led_supported = TPACPI_LED_OLD;
5271 else
5272 /* all others */
5273 led_supported = TPACPI_LED_NEW;
5275 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5276 str_supported(led_supported), led_supported);
5278 if (led_supported == TPACPI_LED_NONE)
5279 return 1;
5281 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5282 GFP_KERNEL);
5283 if (!tpacpi_leds) {
5284 printk(TPACPI_ERR "Out of memory for LED data\n");
5285 return -ENOMEM;
5288 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5289 ARRAY_SIZE(led_useful_qtable));
5291 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5292 if (!tpacpi_is_led_restricted(i) &&
5293 test_bit(i, &useful_leds)) {
5294 rc = tpacpi_init_led(i);
5295 if (rc < 0) {
5296 led_exit();
5297 return rc;
5302 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5303 printk(TPACPI_NOTICE
5304 "warning: userspace override of important "
5305 "firmware LEDs is enabled\n");
5306 #endif
5307 return 0;
5310 #define str_led_status(s) \
5311 ((s) == TPACPI_LED_OFF ? "off" : \
5312 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5314 static int led_read(struct seq_file *m)
5316 if (!led_supported) {
5317 seq_printf(m, "status:\t\tnot supported\n");
5318 return 0;
5320 seq_printf(m, "status:\t\tsupported\n");
5322 if (led_supported == TPACPI_LED_570) {
5323 /* 570 */
5324 int i, status;
5325 for (i = 0; i < 8; i++) {
5326 status = led_get_status(i);
5327 if (status < 0)
5328 return -EIO;
5329 seq_printf(m, "%d:\t\t%s\n",
5330 i, str_led_status(status));
5334 seq_printf(m, "commands:\t"
5335 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5337 return 0;
5340 static int led_write(char *buf)
5342 char *cmd;
5343 int led, rc;
5344 enum led_status_t s;
5346 if (!led_supported)
5347 return -ENODEV;
5349 while ((cmd = next_cmd(&buf))) {
5350 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5351 return -EINVAL;
5353 if (strstr(cmd, "off")) {
5354 s = TPACPI_LED_OFF;
5355 } else if (strstr(cmd, "on")) {
5356 s = TPACPI_LED_ON;
5357 } else if (strstr(cmd, "blink")) {
5358 s = TPACPI_LED_BLINK;
5359 } else {
5360 return -EINVAL;
5363 rc = led_set_status(led, s);
5364 if (rc < 0)
5365 return rc;
5368 return 0;
5371 static struct ibm_struct led_driver_data = {
5372 .name = "led",
5373 .read = led_read,
5374 .write = led_write,
5375 .exit = led_exit,
5378 /*************************************************************************
5379 * Beep subdriver
5382 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5384 #define TPACPI_BEEP_Q1 0x0001
5386 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5387 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5388 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5391 static int __init beep_init(struct ibm_init_struct *iibm)
5393 unsigned long quirks;
5395 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5397 TPACPI_ACPIHANDLE_INIT(beep);
5399 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5400 str_supported(beep_handle != NULL));
5402 quirks = tpacpi_check_quirks(beep_quirk_table,
5403 ARRAY_SIZE(beep_quirk_table));
5405 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5407 return (beep_handle)? 0 : 1;
5410 static int beep_read(struct seq_file *m)
5412 if (!beep_handle)
5413 seq_printf(m, "status:\t\tnot supported\n");
5414 else {
5415 seq_printf(m, "status:\t\tsupported\n");
5416 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5419 return 0;
5422 static int beep_write(char *buf)
5424 char *cmd;
5425 int beep_cmd;
5427 if (!beep_handle)
5428 return -ENODEV;
5430 while ((cmd = next_cmd(&buf))) {
5431 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5432 beep_cmd >= 0 && beep_cmd <= 17) {
5433 /* beep_cmd set */
5434 } else
5435 return -EINVAL;
5436 if (tp_features.beep_needs_two_args) {
5437 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5438 beep_cmd, 0))
5439 return -EIO;
5440 } else {
5441 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5442 beep_cmd))
5443 return -EIO;
5447 return 0;
5450 static struct ibm_struct beep_driver_data = {
5451 .name = "beep",
5452 .read = beep_read,
5453 .write = beep_write,
5456 /*************************************************************************
5457 * Thermal subdriver
5460 enum thermal_access_mode {
5461 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5462 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5463 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5464 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5465 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5468 enum { /* TPACPI_THERMAL_TPEC_* */
5469 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5470 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5471 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5473 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5477 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5478 struct ibm_thermal_sensors_struct {
5479 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5482 static enum thermal_access_mode thermal_read_mode;
5484 /* idx is zero-based */
5485 static int thermal_get_sensor(int idx, s32 *value)
5487 int t;
5488 s8 tmp;
5489 char tmpi[5];
5491 t = TP_EC_THERMAL_TMP0;
5493 switch (thermal_read_mode) {
5494 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5495 case TPACPI_THERMAL_TPEC_16:
5496 if (idx >= 8 && idx <= 15) {
5497 t = TP_EC_THERMAL_TMP8;
5498 idx -= 8;
5500 /* fallthrough */
5501 #endif
5502 case TPACPI_THERMAL_TPEC_8:
5503 if (idx <= 7) {
5504 if (!acpi_ec_read(t + idx, &tmp))
5505 return -EIO;
5506 *value = tmp * 1000;
5507 return 0;
5509 break;
5511 case TPACPI_THERMAL_ACPI_UPDT:
5512 if (idx <= 7) {
5513 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5514 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5515 return -EIO;
5516 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5517 return -EIO;
5518 *value = (t - 2732) * 100;
5519 return 0;
5521 break;
5523 case TPACPI_THERMAL_ACPI_TMP07:
5524 if (idx <= 7) {
5525 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5526 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5527 return -EIO;
5528 if (t > 127 || t < -127)
5529 t = TP_EC_THERMAL_TMP_NA;
5530 *value = t * 1000;
5531 return 0;
5533 break;
5535 case TPACPI_THERMAL_NONE:
5536 default:
5537 return -ENOSYS;
5540 return -EINVAL;
5543 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5545 int res, i;
5546 int n;
5548 n = 8;
5549 i = 0;
5551 if (!s)
5552 return -EINVAL;
5554 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5555 n = 16;
5557 for (i = 0 ; i < n; i++) {
5558 res = thermal_get_sensor(i, &s->temp[i]);
5559 if (res)
5560 return res;
5563 return n;
5566 static void thermal_dump_all_sensors(void)
5568 int n, i;
5569 struct ibm_thermal_sensors_struct t;
5571 n = thermal_get_sensors(&t);
5572 if (n <= 0)
5573 return;
5575 printk(TPACPI_NOTICE
5576 "temperatures (Celsius):");
5578 for (i = 0; i < n; i++) {
5579 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5580 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5581 else
5582 printk(KERN_CONT " N/A");
5585 printk(KERN_CONT "\n");
5588 /* sysfs temp##_input -------------------------------------------------- */
5590 static ssize_t thermal_temp_input_show(struct device *dev,
5591 struct device_attribute *attr,
5592 char *buf)
5594 struct sensor_device_attribute *sensor_attr =
5595 to_sensor_dev_attr(attr);
5596 int idx = sensor_attr->index;
5597 s32 value;
5598 int res;
5600 res = thermal_get_sensor(idx, &value);
5601 if (res)
5602 return res;
5603 if (value == TPACPI_THERMAL_SENSOR_NA)
5604 return -ENXIO;
5606 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5609 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5610 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5611 thermal_temp_input_show, NULL, _idxB)
5613 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5614 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5615 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5616 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5617 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5618 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5619 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5620 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5621 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5622 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5623 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5624 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5625 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5626 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5627 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5628 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5629 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5632 #define THERMAL_ATTRS(X) \
5633 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5635 static struct attribute *thermal_temp_input_attr[] = {
5636 THERMAL_ATTRS(8),
5637 THERMAL_ATTRS(9),
5638 THERMAL_ATTRS(10),
5639 THERMAL_ATTRS(11),
5640 THERMAL_ATTRS(12),
5641 THERMAL_ATTRS(13),
5642 THERMAL_ATTRS(14),
5643 THERMAL_ATTRS(15),
5644 THERMAL_ATTRS(0),
5645 THERMAL_ATTRS(1),
5646 THERMAL_ATTRS(2),
5647 THERMAL_ATTRS(3),
5648 THERMAL_ATTRS(4),
5649 THERMAL_ATTRS(5),
5650 THERMAL_ATTRS(6),
5651 THERMAL_ATTRS(7),
5652 NULL
5655 static const struct attribute_group thermal_temp_input16_group = {
5656 .attrs = thermal_temp_input_attr
5659 static const struct attribute_group thermal_temp_input8_group = {
5660 .attrs = &thermal_temp_input_attr[8]
5663 #undef THERMAL_SENSOR_ATTR_TEMP
5664 #undef THERMAL_ATTRS
5666 /* --------------------------------------------------------------------- */
5668 static int __init thermal_init(struct ibm_init_struct *iibm)
5670 u8 t, ta1, ta2;
5671 int i;
5672 int acpi_tmp7;
5673 int res;
5675 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5677 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5679 if (thinkpad_id.ec_model) {
5681 * Direct EC access mode: sensors at registers
5682 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5683 * non-implemented, thermal sensors return 0x80 when
5684 * not available
5687 ta1 = ta2 = 0;
5688 for (i = 0; i < 8; i++) {
5689 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5690 ta1 |= t;
5691 } else {
5692 ta1 = 0;
5693 break;
5695 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5696 ta2 |= t;
5697 } else {
5698 ta1 = 0;
5699 break;
5702 if (ta1 == 0) {
5703 /* This is sheer paranoia, but we handle it anyway */
5704 if (acpi_tmp7) {
5705 printk(TPACPI_ERR
5706 "ThinkPad ACPI EC access misbehaving, "
5707 "falling back to ACPI TMPx access "
5708 "mode\n");
5709 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5710 } else {
5711 printk(TPACPI_ERR
5712 "ThinkPad ACPI EC access misbehaving, "
5713 "disabling thermal sensors access\n");
5714 thermal_read_mode = TPACPI_THERMAL_NONE;
5716 } else {
5717 thermal_read_mode =
5718 (ta2 != 0) ?
5719 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5721 } else if (acpi_tmp7) {
5722 if (tpacpi_is_ibm() &&
5723 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5724 /* 600e/x, 770e, 770x */
5725 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5726 } else {
5727 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
5728 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5730 } else {
5731 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5732 thermal_read_mode = TPACPI_THERMAL_NONE;
5735 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5736 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5737 thermal_read_mode);
5739 switch (thermal_read_mode) {
5740 case TPACPI_THERMAL_TPEC_16:
5741 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5742 &thermal_temp_input16_group);
5743 if (res)
5744 return res;
5745 break;
5746 case TPACPI_THERMAL_TPEC_8:
5747 case TPACPI_THERMAL_ACPI_TMP07:
5748 case TPACPI_THERMAL_ACPI_UPDT:
5749 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5750 &thermal_temp_input8_group);
5751 if (res)
5752 return res;
5753 break;
5754 case TPACPI_THERMAL_NONE:
5755 default:
5756 return 1;
5759 return 0;
5762 static void thermal_exit(void)
5764 switch (thermal_read_mode) {
5765 case TPACPI_THERMAL_TPEC_16:
5766 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5767 &thermal_temp_input16_group);
5768 break;
5769 case TPACPI_THERMAL_TPEC_8:
5770 case TPACPI_THERMAL_ACPI_TMP07:
5771 case TPACPI_THERMAL_ACPI_UPDT:
5772 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5773 &thermal_temp_input8_group);
5774 break;
5775 case TPACPI_THERMAL_NONE:
5776 default:
5777 break;
5781 static int thermal_read(struct seq_file *m)
5783 int n, i;
5784 struct ibm_thermal_sensors_struct t;
5786 n = thermal_get_sensors(&t);
5787 if (unlikely(n < 0))
5788 return n;
5790 seq_printf(m, "temperatures:\t");
5792 if (n > 0) {
5793 for (i = 0; i < (n - 1); i++)
5794 seq_printf(m, "%d ", t.temp[i] / 1000);
5795 seq_printf(m, "%d\n", t.temp[i] / 1000);
5796 } else
5797 seq_printf(m, "not supported\n");
5799 return 0;
5802 static struct ibm_struct thermal_driver_data = {
5803 .name = "thermal",
5804 .read = thermal_read,
5805 .exit = thermal_exit,
5808 /*************************************************************************
5809 * EC Dump subdriver
5812 static u8 ecdump_regs[256];
5814 static int ecdump_read(struct seq_file *m)
5816 int i, j;
5817 u8 v;
5819 seq_printf(m, "EC "
5820 " +00 +01 +02 +03 +04 +05 +06 +07"
5821 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5822 for (i = 0; i < 256; i += 16) {
5823 seq_printf(m, "EC 0x%02x:", i);
5824 for (j = 0; j < 16; j++) {
5825 if (!acpi_ec_read(i + j, &v))
5826 break;
5827 if (v != ecdump_regs[i + j])
5828 seq_printf(m, " *%02x", v);
5829 else
5830 seq_printf(m, " %02x", v);
5831 ecdump_regs[i + j] = v;
5833 seq_putc(m, '\n');
5834 if (j != 16)
5835 break;
5838 /* These are way too dangerous to advertise openly... */
5839 #if 0
5840 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5841 " (<offset> is 00-ff, <value> is 00-ff)\n");
5842 seq_printf(m, "commands:\t0x<offset> <value> "
5843 " (<offset> is 00-ff, <value> is 0-255)\n");
5844 #endif
5845 return 0;
5848 static int ecdump_write(char *buf)
5850 char *cmd;
5851 int i, v;
5853 while ((cmd = next_cmd(&buf))) {
5854 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5855 /* i and v set */
5856 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5857 /* i and v set */
5858 } else
5859 return -EINVAL;
5860 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5861 if (!acpi_ec_write(i, v))
5862 return -EIO;
5863 } else
5864 return -EINVAL;
5867 return 0;
5870 static struct ibm_struct ecdump_driver_data = {
5871 .name = "ecdump",
5872 .read = ecdump_read,
5873 .write = ecdump_write,
5874 .flags.experimental = 1,
5877 /*************************************************************************
5878 * Backlight/brightness subdriver
5881 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5884 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5885 * CMOS NVRAM byte 0x5E, bits 0-3.
5887 * EC HBRV (0x31) has the following layout
5888 * Bit 7: unknown function
5889 * Bit 6: unknown function
5890 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5891 * Bit 4: must be set to zero to avoid problems
5892 * Bit 3-0: backlight brightness level
5894 * brightness_get_raw returns status data in the HBRV layout
5896 * WARNING: The X61 has been verified to use HBRV for something else, so
5897 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5898 * testing on the very early *60 Lenovo models...
5901 enum {
5902 TP_EC_BACKLIGHT = 0x31,
5904 /* TP_EC_BACKLIGHT bitmasks */
5905 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5906 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5907 TP_EC_BACKLIGHT_MAPSW = 0x20,
5910 enum tpacpi_brightness_access_mode {
5911 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5912 TPACPI_BRGHT_MODE_EC, /* EC control */
5913 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5914 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5915 TPACPI_BRGHT_MODE_MAX
5918 static struct backlight_device *ibm_backlight_device;
5920 static enum tpacpi_brightness_access_mode brightness_mode =
5921 TPACPI_BRGHT_MODE_MAX;
5923 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5925 static struct mutex brightness_mutex;
5927 /* NVRAM brightness access,
5928 * call with brightness_mutex held! */
5929 static unsigned int tpacpi_brightness_nvram_get(void)
5931 u8 lnvram;
5933 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5934 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5935 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5936 lnvram &= bright_maxlvl;
5938 return lnvram;
5941 static void tpacpi_brightness_checkpoint_nvram(void)
5943 u8 lec = 0;
5944 u8 b_nvram;
5946 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5947 return;
5949 vdbg_printk(TPACPI_DBG_BRGHT,
5950 "trying to checkpoint backlight level to NVRAM...\n");
5952 if (mutex_lock_killable(&brightness_mutex) < 0)
5953 return;
5955 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5956 goto unlock;
5957 lec &= TP_EC_BACKLIGHT_LVLMSK;
5958 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5960 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5961 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5962 /* NVRAM needs update */
5963 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5964 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5965 b_nvram |= lec;
5966 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5967 dbg_printk(TPACPI_DBG_BRGHT,
5968 "updated NVRAM backlight level to %u (0x%02x)\n",
5969 (unsigned int) lec, (unsigned int) b_nvram);
5970 } else
5971 vdbg_printk(TPACPI_DBG_BRGHT,
5972 "NVRAM backlight level already is %u (0x%02x)\n",
5973 (unsigned int) lec, (unsigned int) b_nvram);
5975 unlock:
5976 mutex_unlock(&brightness_mutex);
5980 /* call with brightness_mutex held! */
5981 static int tpacpi_brightness_get_raw(int *status)
5983 u8 lec = 0;
5985 switch (brightness_mode) {
5986 case TPACPI_BRGHT_MODE_UCMS_STEP:
5987 *status = tpacpi_brightness_nvram_get();
5988 return 0;
5989 case TPACPI_BRGHT_MODE_EC:
5990 case TPACPI_BRGHT_MODE_ECNVRAM:
5991 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5992 return -EIO;
5993 *status = lec;
5994 return 0;
5995 default:
5996 return -ENXIO;
6000 /* call with brightness_mutex held! */
6001 /* do NOT call with illegal backlight level value */
6002 static int tpacpi_brightness_set_ec(unsigned int value)
6004 u8 lec = 0;
6006 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6007 return -EIO;
6009 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6010 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6011 (value & TP_EC_BACKLIGHT_LVLMSK))))
6012 return -EIO;
6014 return 0;
6017 /* call with brightness_mutex held! */
6018 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6020 int cmos_cmd, inc;
6021 unsigned int current_value, i;
6023 current_value = tpacpi_brightness_nvram_get();
6025 if (value == current_value)
6026 return 0;
6028 cmos_cmd = (value > current_value) ?
6029 TP_CMOS_BRIGHTNESS_UP :
6030 TP_CMOS_BRIGHTNESS_DOWN;
6031 inc = (value > current_value) ? 1 : -1;
6033 for (i = current_value; i != value; i += inc)
6034 if (issue_thinkpad_cmos_command(cmos_cmd))
6035 return -EIO;
6037 return 0;
6040 /* May return EINTR which can always be mapped to ERESTARTSYS */
6041 static int brightness_set(unsigned int value)
6043 int res;
6045 if (value > bright_maxlvl || value < 0)
6046 return -EINVAL;
6048 vdbg_printk(TPACPI_DBG_BRGHT,
6049 "set backlight level to %d\n", value);
6051 res = mutex_lock_killable(&brightness_mutex);
6052 if (res < 0)
6053 return res;
6055 switch (brightness_mode) {
6056 case TPACPI_BRGHT_MODE_EC:
6057 case TPACPI_BRGHT_MODE_ECNVRAM:
6058 res = tpacpi_brightness_set_ec(value);
6059 break;
6060 case TPACPI_BRGHT_MODE_UCMS_STEP:
6061 res = tpacpi_brightness_set_ucmsstep(value);
6062 break;
6063 default:
6064 res = -ENXIO;
6067 mutex_unlock(&brightness_mutex);
6068 return res;
6071 /* sysfs backlight class ----------------------------------------------- */
6073 static int brightness_update_status(struct backlight_device *bd)
6075 unsigned int level =
6076 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6077 bd->props.power == FB_BLANK_UNBLANK) ?
6078 bd->props.brightness : 0;
6080 dbg_printk(TPACPI_DBG_BRGHT,
6081 "backlight: attempt to set level to %d\n",
6082 level);
6084 /* it is the backlight class's job (caller) to handle
6085 * EINTR and other errors properly */
6086 return brightness_set(level);
6089 static int brightness_get(struct backlight_device *bd)
6091 int status, res;
6093 res = mutex_lock_killable(&brightness_mutex);
6094 if (res < 0)
6095 return 0;
6097 res = tpacpi_brightness_get_raw(&status);
6099 mutex_unlock(&brightness_mutex);
6101 if (res < 0)
6102 return 0;
6104 return status & TP_EC_BACKLIGHT_LVLMSK;
6107 static void tpacpi_brightness_notify_change(void)
6109 backlight_force_update(ibm_backlight_device,
6110 BACKLIGHT_UPDATE_HOTKEY);
6113 static struct backlight_ops ibm_backlight_data = {
6114 .get_brightness = brightness_get,
6115 .update_status = brightness_update_status,
6118 /* --------------------------------------------------------------------- */
6120 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6122 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6123 union acpi_object *obj;
6124 int rc;
6126 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
6127 obj = (union acpi_object *)buffer.pointer;
6128 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6129 printk(TPACPI_ERR "Unknown _BCL data, "
6130 "please report this to %s\n", TPACPI_MAIL);
6131 rc = 0;
6132 } else {
6133 rc = obj->package.count;
6135 } else {
6136 return 0;
6139 kfree(buffer.pointer);
6140 return rc;
6143 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
6144 u32 lvl, void *context, void **rv)
6146 char name[ACPI_PATH_SEGMENT_LENGTH];
6147 struct acpi_buffer buffer = { sizeof(name), &name };
6149 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
6150 !strncmp("_BCL", name, sizeof(name) - 1)) {
6151 BUG_ON(!rv || !*rv);
6152 **(int **)rv = tpacpi_query_bcl_levels(handle);
6153 return AE_CTRL_TERMINATE;
6154 } else {
6155 return AE_OK;
6160 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6162 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6164 int status;
6165 int bcl_levels = 0;
6166 void *bcl_ptr = &bcl_levels;
6168 if (!vid_handle)
6169 TPACPI_ACPIHANDLE_INIT(vid);
6171 if (!vid_handle)
6172 return 0;
6175 * Search for a _BCL method, and execute it. This is safe on all
6176 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
6177 * BIOS in ACPI backlight control mode. We do NOT have to care
6178 * about calling the _BCL method in an enabled video device, any
6179 * will do for our purposes.
6182 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
6183 tpacpi_acpi_walk_find_bcl, NULL,
6184 &bcl_ptr);
6186 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
6187 tp_features.bright_acpimode = 1;
6188 return bcl_levels - 2;
6191 return 0;
6195 * These are only useful for models that have only one possibility
6196 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6197 * these quirks.
6199 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6200 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6201 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6203 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6204 /* Models with ATI GPUs known to require ECNVRAM mode */
6205 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6207 /* Models with ATI GPUs that can use ECNVRAM */
6208 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6209 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6210 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6211 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6213 /* Models with Intel Extreme Graphics 2 */
6214 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6215 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6216 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6218 /* Models with Intel GMA900 */
6219 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6220 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6221 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6225 * Returns < 0 for error, otherwise sets tp_features.bright_*
6226 * and bright_maxlvl.
6228 static void __init tpacpi_detect_brightness_capabilities(void)
6230 unsigned int b;
6232 vdbg_printk(TPACPI_DBG_INIT,
6233 "detecting firmware brightness interface capabilities\n");
6235 /* we could run a quirks check here (same table used by
6236 * brightness_init) if needed */
6239 * We always attempt to detect acpi support, so as to switch
6240 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6241 * going to publish a backlight interface
6243 b = tpacpi_check_std_acpi_brightness_support();
6244 switch (b) {
6245 case 16:
6246 bright_maxlvl = 15;
6247 printk(TPACPI_INFO
6248 "detected a 16-level brightness capable ThinkPad\n");
6249 break;
6250 case 8:
6251 case 0:
6252 bright_maxlvl = 7;
6253 printk(TPACPI_INFO
6254 "detected a 8-level brightness capable ThinkPad\n");
6255 break;
6256 default:
6257 printk(TPACPI_ERR
6258 "Unsupported brightness interface, "
6259 "please contact %s\n", TPACPI_MAIL);
6260 tp_features.bright_unkfw = 1;
6261 bright_maxlvl = b - 1;
6265 static int __init brightness_init(struct ibm_init_struct *iibm)
6267 int b;
6268 unsigned long quirks;
6270 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6272 mutex_init(&brightness_mutex);
6274 quirks = tpacpi_check_quirks(brightness_quirk_table,
6275 ARRAY_SIZE(brightness_quirk_table));
6277 /* tpacpi_detect_brightness_capabilities() must have run already */
6279 /* if it is unknown, we don't handle it: it wouldn't be safe */
6280 if (tp_features.bright_unkfw)
6281 return 1;
6283 if (tp_features.bright_acpimode) {
6284 if (acpi_video_backlight_support()) {
6285 if (brightness_enable > 1) {
6286 printk(TPACPI_NOTICE
6287 "Standard ACPI backlight interface "
6288 "available, not loading native one.\n");
6289 return 1;
6290 } else if (brightness_enable == 1) {
6291 printk(TPACPI_NOTICE
6292 "Backlight control force enabled, even if standard "
6293 "ACPI backlight interface is available\n");
6295 } else {
6296 if (brightness_enable > 1) {
6297 printk(TPACPI_NOTICE
6298 "Standard ACPI backlight interface not "
6299 "available, thinkpad_acpi native "
6300 "brightness control enabled\n");
6305 if (!brightness_enable) {
6306 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6307 "brightness support disabled by "
6308 "module parameter\n");
6309 return 1;
6313 * Check for module parameter bogosity, note that we
6314 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6315 * able to detect "unspecified"
6317 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6318 return -EINVAL;
6320 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6321 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6322 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6323 if (quirks & TPACPI_BRGHT_Q_EC)
6324 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6325 else
6326 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6328 dbg_printk(TPACPI_DBG_BRGHT,
6329 "driver auto-selected brightness_mode=%d\n",
6330 brightness_mode);
6333 /* Safety */
6334 if (!tpacpi_is_ibm() &&
6335 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6336 brightness_mode == TPACPI_BRGHT_MODE_EC))
6337 return -EINVAL;
6339 if (tpacpi_brightness_get_raw(&b) < 0)
6340 return 1;
6342 ibm_backlight_device = backlight_device_register(
6343 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6344 &ibm_backlight_data);
6345 if (IS_ERR(ibm_backlight_device)) {
6346 int rc = PTR_ERR(ibm_backlight_device);
6347 ibm_backlight_device = NULL;
6348 printk(TPACPI_ERR "Could not register backlight device\n");
6349 return rc;
6351 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6352 "brightness is supported\n");
6354 if (quirks & TPACPI_BRGHT_Q_ASK) {
6355 printk(TPACPI_NOTICE
6356 "brightness: will use unverified default: "
6357 "brightness_mode=%d\n", brightness_mode);
6358 printk(TPACPI_NOTICE
6359 "brightness: please report to %s whether it works well "
6360 "or not on your ThinkPad\n", TPACPI_MAIL);
6363 ibm_backlight_device->props.max_brightness = bright_maxlvl;
6364 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6365 backlight_update_status(ibm_backlight_device);
6367 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6368 "brightness: registering brightness hotkeys "
6369 "as change notification\n");
6370 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6371 | TP_ACPI_HKEY_BRGHTUP_MASK
6372 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6373 return 0;
6376 static void brightness_suspend(pm_message_t state)
6378 tpacpi_brightness_checkpoint_nvram();
6381 static void brightness_shutdown(void)
6383 tpacpi_brightness_checkpoint_nvram();
6386 static void brightness_exit(void)
6388 if (ibm_backlight_device) {
6389 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6390 "calling backlight_device_unregister()\n");
6391 backlight_device_unregister(ibm_backlight_device);
6394 tpacpi_brightness_checkpoint_nvram();
6397 static int brightness_read(struct seq_file *m)
6399 int level;
6401 level = brightness_get(NULL);
6402 if (level < 0) {
6403 seq_printf(m, "level:\t\tunreadable\n");
6404 } else {
6405 seq_printf(m, "level:\t\t%d\n", level);
6406 seq_printf(m, "commands:\tup, down\n");
6407 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6408 bright_maxlvl);
6411 return 0;
6414 static int brightness_write(char *buf)
6416 int level;
6417 int rc;
6418 char *cmd;
6420 level = brightness_get(NULL);
6421 if (level < 0)
6422 return level;
6424 while ((cmd = next_cmd(&buf))) {
6425 if (strlencmp(cmd, "up") == 0) {
6426 if (level < bright_maxlvl)
6427 level++;
6428 } else if (strlencmp(cmd, "down") == 0) {
6429 if (level > 0)
6430 level--;
6431 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6432 level >= 0 && level <= bright_maxlvl) {
6433 /* new level set */
6434 } else
6435 return -EINVAL;
6438 tpacpi_disclose_usertask("procfs brightness",
6439 "set level to %d\n", level);
6442 * Now we know what the final level should be, so we try to set it.
6443 * Doing it this way makes the syscall restartable in case of EINTR
6445 rc = brightness_set(level);
6446 if (!rc && ibm_backlight_device)
6447 backlight_force_update(ibm_backlight_device,
6448 BACKLIGHT_UPDATE_SYSFS);
6449 return (rc == -EINTR)? -ERESTARTSYS : rc;
6452 static struct ibm_struct brightness_driver_data = {
6453 .name = "brightness",
6454 .read = brightness_read,
6455 .write = brightness_write,
6456 .exit = brightness_exit,
6457 .suspend = brightness_suspend,
6458 .shutdown = brightness_shutdown,
6461 /*************************************************************************
6462 * Volume subdriver
6466 * IBM ThinkPads have a simple volume controller with MUTE gating.
6467 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6469 * Since the *61 series (and probably also the later *60 series), Lenovo
6470 * ThinkPads only implement the MUTE gate.
6472 * EC register 0x30
6473 * Bit 6: MUTE (1 mutes sound)
6474 * Bit 3-0: Volume
6475 * Other bits should be zero as far as we know.
6477 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6478 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6479 * such as bit 7 which is used to detect repeated presses of MUTE,
6480 * and we leave them unchanged.
6483 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6485 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6486 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6487 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6489 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6490 static char *alsa_id = "ThinkPadEC";
6491 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6493 struct tpacpi_alsa_data {
6494 struct snd_card *card;
6495 struct snd_ctl_elem_id *ctl_mute_id;
6496 struct snd_ctl_elem_id *ctl_vol_id;
6499 static struct snd_card *alsa_card;
6501 enum {
6502 TP_EC_AUDIO = 0x30,
6504 /* TP_EC_AUDIO bits */
6505 TP_EC_AUDIO_MUTESW = 6,
6507 /* TP_EC_AUDIO bitmasks */
6508 TP_EC_AUDIO_LVL_MSK = 0x0F,
6509 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6511 /* Maximum volume */
6512 TP_EC_VOLUME_MAX = 14,
6515 enum tpacpi_volume_access_mode {
6516 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6517 TPACPI_VOL_MODE_EC, /* Pure EC control */
6518 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6519 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6520 TPACPI_VOL_MODE_MAX
6523 enum tpacpi_volume_capabilities {
6524 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6525 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6526 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6527 TPACPI_VOL_CAP_MAX
6530 static enum tpacpi_volume_access_mode volume_mode =
6531 TPACPI_VOL_MODE_MAX;
6533 static enum tpacpi_volume_capabilities volume_capabilities;
6534 static int volume_control_allowed;
6537 * Used to syncronize writers to TP_EC_AUDIO and
6538 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6540 static struct mutex volume_mutex;
6542 static void tpacpi_volume_checkpoint_nvram(void)
6544 u8 lec = 0;
6545 u8 b_nvram;
6546 u8 ec_mask;
6548 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6549 return;
6550 if (!volume_control_allowed)
6551 return;
6553 vdbg_printk(TPACPI_DBG_MIXER,
6554 "trying to checkpoint mixer state to NVRAM...\n");
6556 if (tp_features.mixer_no_level_control)
6557 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6558 else
6559 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6561 if (mutex_lock_killable(&volume_mutex) < 0)
6562 return;
6564 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6565 goto unlock;
6566 lec &= ec_mask;
6567 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6569 if (lec != (b_nvram & ec_mask)) {
6570 /* NVRAM needs update */
6571 b_nvram &= ~ec_mask;
6572 b_nvram |= lec;
6573 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6574 dbg_printk(TPACPI_DBG_MIXER,
6575 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6576 (unsigned int) lec, (unsigned int) b_nvram);
6577 } else {
6578 vdbg_printk(TPACPI_DBG_MIXER,
6579 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6580 (unsigned int) lec, (unsigned int) b_nvram);
6583 unlock:
6584 mutex_unlock(&volume_mutex);
6587 static int volume_get_status_ec(u8 *status)
6589 u8 s;
6591 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6592 return -EIO;
6594 *status = s;
6596 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6598 return 0;
6601 static int volume_get_status(u8 *status)
6603 return volume_get_status_ec(status);
6606 static int volume_set_status_ec(const u8 status)
6608 if (!acpi_ec_write(TP_EC_AUDIO, status))
6609 return -EIO;
6611 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6613 return 0;
6616 static int volume_set_status(const u8 status)
6618 return volume_set_status_ec(status);
6621 /* returns < 0 on error, 0 on no change, 1 on change */
6622 static int __volume_set_mute_ec(const bool mute)
6624 int rc;
6625 u8 s, n;
6627 if (mutex_lock_killable(&volume_mutex) < 0)
6628 return -EINTR;
6630 rc = volume_get_status_ec(&s);
6631 if (rc)
6632 goto unlock;
6634 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6635 s & ~TP_EC_AUDIO_MUTESW_MSK;
6637 if (n != s) {
6638 rc = volume_set_status_ec(n);
6639 if (!rc)
6640 rc = 1;
6643 unlock:
6644 mutex_unlock(&volume_mutex);
6645 return rc;
6648 static int volume_alsa_set_mute(const bool mute)
6650 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6651 (mute) ? "" : "un");
6652 return __volume_set_mute_ec(mute);
6655 static int volume_set_mute(const bool mute)
6657 int rc;
6659 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6660 (mute) ? "" : "un");
6662 rc = __volume_set_mute_ec(mute);
6663 return (rc < 0) ? rc : 0;
6666 /* returns < 0 on error, 0 on no change, 1 on change */
6667 static int __volume_set_volume_ec(const u8 vol)
6669 int rc;
6670 u8 s, n;
6672 if (vol > TP_EC_VOLUME_MAX)
6673 return -EINVAL;
6675 if (mutex_lock_killable(&volume_mutex) < 0)
6676 return -EINTR;
6678 rc = volume_get_status_ec(&s);
6679 if (rc)
6680 goto unlock;
6682 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6684 if (n != s) {
6685 rc = volume_set_status_ec(n);
6686 if (!rc)
6687 rc = 1;
6690 unlock:
6691 mutex_unlock(&volume_mutex);
6692 return rc;
6695 static int volume_alsa_set_volume(const u8 vol)
6697 dbg_printk(TPACPI_DBG_MIXER,
6698 "ALSA: trying to set volume level to %hu\n", vol);
6699 return __volume_set_volume_ec(vol);
6702 static void volume_alsa_notify_change(void)
6704 struct tpacpi_alsa_data *d;
6706 if (alsa_card && alsa_card->private_data) {
6707 d = alsa_card->private_data;
6708 if (d->ctl_mute_id)
6709 snd_ctl_notify(alsa_card,
6710 SNDRV_CTL_EVENT_MASK_VALUE,
6711 d->ctl_mute_id);
6712 if (d->ctl_vol_id)
6713 snd_ctl_notify(alsa_card,
6714 SNDRV_CTL_EVENT_MASK_VALUE,
6715 d->ctl_vol_id);
6719 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6720 struct snd_ctl_elem_info *uinfo)
6722 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6723 uinfo->count = 1;
6724 uinfo->value.integer.min = 0;
6725 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6726 return 0;
6729 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6730 struct snd_ctl_elem_value *ucontrol)
6732 u8 s;
6733 int rc;
6735 rc = volume_get_status(&s);
6736 if (rc < 0)
6737 return rc;
6739 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6740 return 0;
6743 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6744 struct snd_ctl_elem_value *ucontrol)
6746 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6747 ucontrol->value.integer.value[0]);
6748 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6751 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6753 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6754 struct snd_ctl_elem_value *ucontrol)
6756 u8 s;
6757 int rc;
6759 rc = volume_get_status(&s);
6760 if (rc < 0)
6761 return rc;
6763 ucontrol->value.integer.value[0] =
6764 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6765 return 0;
6768 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6769 struct snd_ctl_elem_value *ucontrol)
6771 tpacpi_disclose_usertask("ALSA", "%smute\n",
6772 ucontrol->value.integer.value[0] ?
6773 "un" : "");
6774 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6777 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6778 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6779 .name = "Console Playback Volume",
6780 .index = 0,
6781 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6782 .info = volume_alsa_vol_info,
6783 .get = volume_alsa_vol_get,
6786 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6787 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6788 .name = "Console Playback Switch",
6789 .index = 0,
6790 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6791 .info = volume_alsa_mute_info,
6792 .get = volume_alsa_mute_get,
6795 static void volume_suspend(pm_message_t state)
6797 tpacpi_volume_checkpoint_nvram();
6800 static void volume_resume(void)
6802 volume_alsa_notify_change();
6805 static void volume_shutdown(void)
6807 tpacpi_volume_checkpoint_nvram();
6810 static void volume_exit(void)
6812 if (alsa_card) {
6813 snd_card_free(alsa_card);
6814 alsa_card = NULL;
6817 tpacpi_volume_checkpoint_nvram();
6820 static int __init volume_create_alsa_mixer(void)
6822 struct snd_card *card;
6823 struct tpacpi_alsa_data *data;
6824 struct snd_kcontrol *ctl_vol;
6825 struct snd_kcontrol *ctl_mute;
6826 int rc;
6828 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6829 sizeof(struct tpacpi_alsa_data), &card);
6830 if (rc < 0 || !card) {
6831 printk(TPACPI_ERR
6832 "Failed to create ALSA card structures: %d\n", rc);
6833 return 1;
6836 BUG_ON(!card->private_data);
6837 data = card->private_data;
6838 data->card = card;
6840 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6841 sizeof(card->driver));
6842 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6843 sizeof(card->shortname));
6844 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6845 (thinkpad_id.ec_version_str) ?
6846 thinkpad_id.ec_version_str : "(unknown)");
6847 snprintf(card->longname, sizeof(card->longname),
6848 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6849 (thinkpad_id.ec_version_str) ?
6850 thinkpad_id.ec_version_str : "unknown");
6852 if (volume_control_allowed) {
6853 volume_alsa_control_vol.put = volume_alsa_vol_put;
6854 volume_alsa_control_vol.access =
6855 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6857 volume_alsa_control_mute.put = volume_alsa_mute_put;
6858 volume_alsa_control_mute.access =
6859 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6862 if (!tp_features.mixer_no_level_control) {
6863 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6864 rc = snd_ctl_add(card, ctl_vol);
6865 if (rc < 0) {
6866 printk(TPACPI_ERR
6867 "Failed to create ALSA volume control: %d\n",
6868 rc);
6869 goto err_exit;
6871 data->ctl_vol_id = &ctl_vol->id;
6874 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6875 rc = snd_ctl_add(card, ctl_mute);
6876 if (rc < 0) {
6877 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6878 rc);
6879 goto err_exit;
6881 data->ctl_mute_id = &ctl_mute->id;
6883 snd_card_set_dev(card, &tpacpi_pdev->dev);
6884 rc = snd_card_register(card);
6885 if (rc < 0) {
6886 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6887 goto err_exit;
6890 alsa_card = card;
6891 return 0;
6893 err_exit:
6894 snd_card_free(card);
6895 return 1;
6898 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6899 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6901 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6902 /* Whitelist volume level on all IBM by default */
6903 { .vendor = PCI_VENDOR_ID_IBM,
6904 .bios = TPACPI_MATCH_ANY,
6905 .ec = TPACPI_MATCH_ANY,
6906 .quirks = TPACPI_VOL_Q_LEVEL },
6908 /* Lenovo models with volume control (needs confirmation) */
6909 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6910 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6911 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6912 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6913 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6914 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6915 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6917 /* Whitelist mute-only on all Lenovo by default */
6918 { .vendor = PCI_VENDOR_ID_LENOVO,
6919 .bios = TPACPI_MATCH_ANY,
6920 .ec = TPACPI_MATCH_ANY,
6921 .quirks = TPACPI_VOL_Q_MUTEONLY }
6924 static int __init volume_init(struct ibm_init_struct *iibm)
6926 unsigned long quirks;
6927 int rc;
6929 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6931 mutex_init(&volume_mutex);
6934 * Check for module parameter bogosity, note that we
6935 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6936 * able to detect "unspecified"
6938 if (volume_mode > TPACPI_VOL_MODE_MAX)
6939 return -EINVAL;
6941 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6942 printk(TPACPI_ERR
6943 "UCMS step volume mode not implemented, "
6944 "please contact %s\n", TPACPI_MAIL);
6945 return 1;
6948 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6949 return -EINVAL;
6952 * The ALSA mixer is our primary interface.
6953 * When disabled, don't install the subdriver at all
6955 if (!alsa_enable) {
6956 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6957 "ALSA mixer disabled by parameter, "
6958 "not loading volume subdriver...\n");
6959 return 1;
6962 quirks = tpacpi_check_quirks(volume_quirk_table,
6963 ARRAY_SIZE(volume_quirk_table));
6965 switch (volume_capabilities) {
6966 case TPACPI_VOL_CAP_AUTO:
6967 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6968 tp_features.mixer_no_level_control = 1;
6969 else if (quirks & TPACPI_VOL_Q_LEVEL)
6970 tp_features.mixer_no_level_control = 0;
6971 else
6972 return 1; /* no mixer */
6973 break;
6974 case TPACPI_VOL_CAP_VOLMUTE:
6975 tp_features.mixer_no_level_control = 0;
6976 break;
6977 case TPACPI_VOL_CAP_MUTEONLY:
6978 tp_features.mixer_no_level_control = 1;
6979 break;
6980 default:
6981 return 1;
6984 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6985 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6986 "using user-supplied volume_capabilities=%d\n",
6987 volume_capabilities);
6989 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6990 volume_mode == TPACPI_VOL_MODE_MAX) {
6991 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6993 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6994 "driver auto-selected volume_mode=%d\n",
6995 volume_mode);
6996 } else {
6997 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6998 "using user-supplied volume_mode=%d\n",
6999 volume_mode);
7002 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7003 "mute is supported, volume control is %s\n",
7004 str_supported(!tp_features.mixer_no_level_control));
7006 rc = volume_create_alsa_mixer();
7007 if (rc) {
7008 printk(TPACPI_ERR
7009 "Could not create the ALSA mixer interface\n");
7010 return rc;
7013 printk(TPACPI_INFO
7014 "Console audio control enabled, mode: %s\n",
7015 (volume_control_allowed) ?
7016 "override (read/write)" :
7017 "monitor (read only)");
7019 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7020 "registering volume hotkeys as change notification\n");
7021 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7022 | TP_ACPI_HKEY_VOLUP_MASK
7023 | TP_ACPI_HKEY_VOLDWN_MASK
7024 | TP_ACPI_HKEY_MUTE_MASK);
7026 return 0;
7029 static int volume_read(struct seq_file *m)
7031 u8 status;
7033 if (volume_get_status(&status) < 0) {
7034 seq_printf(m, "level:\t\tunreadable\n");
7035 } else {
7036 if (tp_features.mixer_no_level_control)
7037 seq_printf(m, "level:\t\tunsupported\n");
7038 else
7039 seq_printf(m, "level:\t\t%d\n",
7040 status & TP_EC_AUDIO_LVL_MSK);
7042 seq_printf(m, "mute:\t\t%s\n",
7043 onoff(status, TP_EC_AUDIO_MUTESW));
7045 if (volume_control_allowed) {
7046 seq_printf(m, "commands:\tunmute, mute\n");
7047 if (!tp_features.mixer_no_level_control) {
7048 seq_printf(m,
7049 "commands:\tup, down\n");
7050 seq_printf(m,
7051 "commands:\tlevel <level>"
7052 " (<level> is 0-%d)\n",
7053 TP_EC_VOLUME_MAX);
7058 return 0;
7061 static int volume_write(char *buf)
7063 u8 s;
7064 u8 new_level, new_mute;
7065 int l;
7066 char *cmd;
7067 int rc;
7070 * We do allow volume control at driver startup, so that the
7071 * user can set initial state through the volume=... parameter hack.
7073 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7074 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7075 tp_warned.volume_ctrl_forbidden = 1;
7076 printk(TPACPI_NOTICE
7077 "Console audio control in monitor mode, "
7078 "changes are not allowed.\n");
7079 printk(TPACPI_NOTICE
7080 "Use the volume_control=1 module parameter "
7081 "to enable volume control\n");
7083 return -EPERM;
7086 rc = volume_get_status(&s);
7087 if (rc < 0)
7088 return rc;
7090 new_level = s & TP_EC_AUDIO_LVL_MSK;
7091 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7093 while ((cmd = next_cmd(&buf))) {
7094 if (!tp_features.mixer_no_level_control) {
7095 if (strlencmp(cmd, "up") == 0) {
7096 if (new_mute)
7097 new_mute = 0;
7098 else if (new_level < TP_EC_VOLUME_MAX)
7099 new_level++;
7100 continue;
7101 } else if (strlencmp(cmd, "down") == 0) {
7102 if (new_mute)
7103 new_mute = 0;
7104 else if (new_level > 0)
7105 new_level--;
7106 continue;
7107 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7108 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7109 new_level = l;
7110 continue;
7113 if (strlencmp(cmd, "mute") == 0)
7114 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7115 else if (strlencmp(cmd, "unmute") == 0)
7116 new_mute = 0;
7117 else
7118 return -EINVAL;
7121 if (tp_features.mixer_no_level_control) {
7122 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7123 new_mute ? "" : "un");
7124 rc = volume_set_mute(!!new_mute);
7125 } else {
7126 tpacpi_disclose_usertask("procfs volume",
7127 "%smute and set level to %d\n",
7128 new_mute ? "" : "un", new_level);
7129 rc = volume_set_status(new_mute | new_level);
7131 volume_alsa_notify_change();
7133 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7136 static struct ibm_struct volume_driver_data = {
7137 .name = "volume",
7138 .read = volume_read,
7139 .write = volume_write,
7140 .exit = volume_exit,
7141 .suspend = volume_suspend,
7142 .resume = volume_resume,
7143 .shutdown = volume_shutdown,
7146 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7148 #define alsa_card NULL
7150 static void inline volume_alsa_notify_change(void)
7154 static int __init volume_init(struct ibm_init_struct *iibm)
7156 printk(TPACPI_INFO
7157 "volume: disabled as there is no ALSA support in this kernel\n");
7159 return 1;
7162 static struct ibm_struct volume_driver_data = {
7163 .name = "volume",
7166 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7168 /*************************************************************************
7169 * Fan subdriver
7173 * FAN ACCESS MODES
7175 * TPACPI_FAN_RD_ACPI_GFAN:
7176 * ACPI GFAN method: returns fan level
7178 * see TPACPI_FAN_WR_ACPI_SFAN
7179 * EC 0x2f (HFSP) not available if GFAN exists
7181 * TPACPI_FAN_WR_ACPI_SFAN:
7182 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7184 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7185 * it for writing.
7187 * TPACPI_FAN_WR_TPEC:
7188 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7189 * Supported on almost all ThinkPads
7191 * Fan speed changes of any sort (including those caused by the
7192 * disengaged mode) are usually done slowly by the firmware as the
7193 * maximum amount of fan duty cycle change per second seems to be
7194 * limited.
7196 * Reading is not available if GFAN exists.
7197 * Writing is not available if SFAN exists.
7199 * Bits
7200 * 7 automatic mode engaged;
7201 * (default operation mode of the ThinkPad)
7202 * fan level is ignored in this mode.
7203 * 6 full speed mode (takes precedence over bit 7);
7204 * not available on all thinkpads. May disable
7205 * the tachometer while the fan controller ramps up
7206 * the speed (which can take up to a few *minutes*).
7207 * Speeds up fan to 100% duty-cycle, which is far above
7208 * the standard RPM levels. It is not impossible that
7209 * it could cause hardware damage.
7210 * 5-3 unused in some models. Extra bits for fan level
7211 * in others, but still useless as all values above
7212 * 7 map to the same speed as level 7 in these models.
7213 * 2-0 fan level (0..7 usually)
7214 * 0x00 = stop
7215 * 0x07 = max (set when temperatures critical)
7216 * Some ThinkPads may have other levels, see
7217 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7219 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7220 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7221 * does so, its initial value is meaningless (0x07).
7223 * For firmware bugs, refer to:
7224 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7226 * ----
7228 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7229 * Main fan tachometer reading (in RPM)
7231 * This register is present on all ThinkPads with a new-style EC, and
7232 * it is known not to be present on the A21m/e, and T22, as there is
7233 * something else in offset 0x84 according to the ACPI DSDT. Other
7234 * ThinkPads from this same time period (and earlier) probably lack the
7235 * tachometer as well.
7237 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7238 * was never fixed by IBM to report the EC firmware version string
7239 * probably support the tachometer (like the early X models), so
7240 * detecting it is quite hard. We need more data to know for sure.
7242 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7243 * might result.
7245 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7246 * mode.
7248 * For firmware bugs, refer to:
7249 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7251 * ----
7253 * ThinkPad EC register 0x31 bit 0 (only on select models)
7255 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7256 * show the speed of the main fan. When bit 0 of EC register 0x31
7257 * is one, the tachometer registers show the speed of the auxiliary
7258 * fan.
7260 * Fan control seems to affect both fans, regardless of the state
7261 * of this bit.
7263 * So far, only the firmware for the X60/X61 non-tablet versions
7264 * seem to support this (firmware TP-7M).
7266 * TPACPI_FAN_WR_ACPI_FANS:
7267 * ThinkPad X31, X40, X41. Not available in the X60.
7269 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7270 * high speed. ACPI DSDT seems to map these three speeds to levels
7271 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7272 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7274 * The speeds are stored on handles
7275 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7277 * There are three default speed sets, accessible as handles:
7278 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7280 * ACPI DSDT switches which set is in use depending on various
7281 * factors.
7283 * TPACPI_FAN_WR_TPEC is also available and should be used to
7284 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7285 * but the ACPI tables just mention level 7.
7288 enum { /* Fan control constants */
7289 fan_status_offset = 0x2f, /* EC register 0x2f */
7290 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7291 * 0x84 must be read before 0x85 */
7292 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7293 bit 0 selects which fan is active */
7295 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7296 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7298 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7301 enum fan_status_access_mode {
7302 TPACPI_FAN_NONE = 0, /* No fan status or control */
7303 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7304 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7307 enum fan_control_access_mode {
7308 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7309 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7310 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7311 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7314 enum fan_control_commands {
7315 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7316 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7317 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7318 * and also watchdog cmd */
7321 static int fan_control_allowed;
7323 static enum fan_status_access_mode fan_status_access_mode;
7324 static enum fan_control_access_mode fan_control_access_mode;
7325 static enum fan_control_commands fan_control_commands;
7327 static u8 fan_control_initial_status;
7328 static u8 fan_control_desired_level;
7329 static u8 fan_control_resume_level;
7330 static int fan_watchdog_maxinterval;
7332 static struct mutex fan_mutex;
7334 static void fan_watchdog_fire(struct work_struct *ignored);
7335 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7337 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7338 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7339 "\\FSPD", /* 600e/x, 770e, 770x */
7340 ); /* all others */
7341 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7342 "JFNS", /* 770x-JL */
7343 ); /* all others */
7346 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7347 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7348 * be in auto mode (0x80).
7350 * This is corrected by any write to HFSP either by the driver, or
7351 * by the firmware.
7353 * We assume 0x07 really means auto mode while this quirk is active,
7354 * as this is far more likely than the ThinkPad being in level 7,
7355 * which is only used by the firmware during thermal emergencies.
7357 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7358 * TP-70 (T43, R52), which are known to be buggy.
7361 static void fan_quirk1_setup(void)
7363 if (fan_control_initial_status == 0x07) {
7364 printk(TPACPI_NOTICE
7365 "fan_init: initial fan status is unknown, "
7366 "assuming it is in auto mode\n");
7367 tp_features.fan_ctrl_status_undef = 1;
7371 static void fan_quirk1_handle(u8 *fan_status)
7373 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7374 if (*fan_status != fan_control_initial_status) {
7375 /* something changed the HFSP regisnter since
7376 * driver init time, so it is not undefined
7377 * anymore */
7378 tp_features.fan_ctrl_status_undef = 0;
7379 } else {
7380 /* Return most likely status. In fact, it
7381 * might be the only possible status */
7382 *fan_status = TP_EC_FAN_AUTO;
7387 /* Select main fan on X60/X61, NOOP on others */
7388 static bool fan_select_fan1(void)
7390 if (tp_features.second_fan) {
7391 u8 val;
7393 if (ec_read(fan_select_offset, &val) < 0)
7394 return false;
7395 val &= 0xFEU;
7396 if (ec_write(fan_select_offset, val) < 0)
7397 return false;
7399 return true;
7402 /* Select secondary fan on X60/X61 */
7403 static bool fan_select_fan2(void)
7405 u8 val;
7407 if (!tp_features.second_fan)
7408 return false;
7410 if (ec_read(fan_select_offset, &val) < 0)
7411 return false;
7412 val |= 0x01U;
7413 if (ec_write(fan_select_offset, val) < 0)
7414 return false;
7416 return true;
7420 * Call with fan_mutex held
7422 static void fan_update_desired_level(u8 status)
7424 if ((status &
7425 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7426 if (status > 7)
7427 fan_control_desired_level = 7;
7428 else
7429 fan_control_desired_level = status;
7433 static int fan_get_status(u8 *status)
7435 u8 s;
7437 /* TODO:
7438 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7440 switch (fan_status_access_mode) {
7441 case TPACPI_FAN_RD_ACPI_GFAN:
7442 /* 570, 600e/x, 770e, 770x */
7444 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7445 return -EIO;
7447 if (likely(status))
7448 *status = s & 0x07;
7450 break;
7452 case TPACPI_FAN_RD_TPEC:
7453 /* all except 570, 600e/x, 770e, 770x */
7454 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7455 return -EIO;
7457 if (likely(status)) {
7458 *status = s;
7459 fan_quirk1_handle(status);
7462 break;
7464 default:
7465 return -ENXIO;
7468 return 0;
7471 static int fan_get_status_safe(u8 *status)
7473 int rc;
7474 u8 s;
7476 if (mutex_lock_killable(&fan_mutex))
7477 return -ERESTARTSYS;
7478 rc = fan_get_status(&s);
7479 if (!rc)
7480 fan_update_desired_level(s);
7481 mutex_unlock(&fan_mutex);
7483 if (status)
7484 *status = s;
7486 return rc;
7489 static int fan_get_speed(unsigned int *speed)
7491 u8 hi, lo;
7493 switch (fan_status_access_mode) {
7494 case TPACPI_FAN_RD_TPEC:
7495 /* all except 570, 600e/x, 770e, 770x */
7496 if (unlikely(!fan_select_fan1()))
7497 return -EIO;
7498 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7499 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7500 return -EIO;
7502 if (likely(speed))
7503 *speed = (hi << 8) | lo;
7505 break;
7507 default:
7508 return -ENXIO;
7511 return 0;
7514 static int fan2_get_speed(unsigned int *speed)
7516 u8 hi, lo;
7517 bool rc;
7519 switch (fan_status_access_mode) {
7520 case TPACPI_FAN_RD_TPEC:
7521 /* all except 570, 600e/x, 770e, 770x */
7522 if (unlikely(!fan_select_fan2()))
7523 return -EIO;
7524 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7525 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7526 fan_select_fan1(); /* play it safe */
7527 if (rc)
7528 return -EIO;
7530 if (likely(speed))
7531 *speed = (hi << 8) | lo;
7533 break;
7535 default:
7536 return -ENXIO;
7539 return 0;
7542 static int fan_set_level(int level)
7544 if (!fan_control_allowed)
7545 return -EPERM;
7547 switch (fan_control_access_mode) {
7548 case TPACPI_FAN_WR_ACPI_SFAN:
7549 if (level >= 0 && level <= 7) {
7550 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7551 return -EIO;
7552 } else
7553 return -EINVAL;
7554 break;
7556 case TPACPI_FAN_WR_ACPI_FANS:
7557 case TPACPI_FAN_WR_TPEC:
7558 if (!(level & TP_EC_FAN_AUTO) &&
7559 !(level & TP_EC_FAN_FULLSPEED) &&
7560 ((level < 0) || (level > 7)))
7561 return -EINVAL;
7563 /* safety net should the EC not support AUTO
7564 * or FULLSPEED mode bits and just ignore them */
7565 if (level & TP_EC_FAN_FULLSPEED)
7566 level |= 7; /* safety min speed 7 */
7567 else if (level & TP_EC_FAN_AUTO)
7568 level |= 4; /* safety min speed 4 */
7570 if (!acpi_ec_write(fan_status_offset, level))
7571 return -EIO;
7572 else
7573 tp_features.fan_ctrl_status_undef = 0;
7574 break;
7576 default:
7577 return -ENXIO;
7580 vdbg_printk(TPACPI_DBG_FAN,
7581 "fan control: set fan control register to 0x%02x\n", level);
7582 return 0;
7585 static int fan_set_level_safe(int level)
7587 int rc;
7589 if (!fan_control_allowed)
7590 return -EPERM;
7592 if (mutex_lock_killable(&fan_mutex))
7593 return -ERESTARTSYS;
7595 if (level == TPACPI_FAN_LAST_LEVEL)
7596 level = fan_control_desired_level;
7598 rc = fan_set_level(level);
7599 if (!rc)
7600 fan_update_desired_level(level);
7602 mutex_unlock(&fan_mutex);
7603 return rc;
7606 static int fan_set_enable(void)
7608 u8 s;
7609 int rc;
7611 if (!fan_control_allowed)
7612 return -EPERM;
7614 if (mutex_lock_killable(&fan_mutex))
7615 return -ERESTARTSYS;
7617 switch (fan_control_access_mode) {
7618 case TPACPI_FAN_WR_ACPI_FANS:
7619 case TPACPI_FAN_WR_TPEC:
7620 rc = fan_get_status(&s);
7621 if (rc < 0)
7622 break;
7624 /* Don't go out of emergency fan mode */
7625 if (s != 7) {
7626 s &= 0x07;
7627 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7630 if (!acpi_ec_write(fan_status_offset, s))
7631 rc = -EIO;
7632 else {
7633 tp_features.fan_ctrl_status_undef = 0;
7634 rc = 0;
7636 break;
7638 case TPACPI_FAN_WR_ACPI_SFAN:
7639 rc = fan_get_status(&s);
7640 if (rc < 0)
7641 break;
7643 s &= 0x07;
7645 /* Set fan to at least level 4 */
7646 s |= 4;
7648 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7649 rc = -EIO;
7650 else
7651 rc = 0;
7652 break;
7654 default:
7655 rc = -ENXIO;
7658 mutex_unlock(&fan_mutex);
7660 if (!rc)
7661 vdbg_printk(TPACPI_DBG_FAN,
7662 "fan control: set fan control register to 0x%02x\n",
7664 return rc;
7667 static int fan_set_disable(void)
7669 int rc;
7671 if (!fan_control_allowed)
7672 return -EPERM;
7674 if (mutex_lock_killable(&fan_mutex))
7675 return -ERESTARTSYS;
7677 rc = 0;
7678 switch (fan_control_access_mode) {
7679 case TPACPI_FAN_WR_ACPI_FANS:
7680 case TPACPI_FAN_WR_TPEC:
7681 if (!acpi_ec_write(fan_status_offset, 0x00))
7682 rc = -EIO;
7683 else {
7684 fan_control_desired_level = 0;
7685 tp_features.fan_ctrl_status_undef = 0;
7687 break;
7689 case TPACPI_FAN_WR_ACPI_SFAN:
7690 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7691 rc = -EIO;
7692 else
7693 fan_control_desired_level = 0;
7694 break;
7696 default:
7697 rc = -ENXIO;
7700 if (!rc)
7701 vdbg_printk(TPACPI_DBG_FAN,
7702 "fan control: set fan control register to 0\n");
7704 mutex_unlock(&fan_mutex);
7705 return rc;
7708 static int fan_set_speed(int speed)
7710 int rc;
7712 if (!fan_control_allowed)
7713 return -EPERM;
7715 if (mutex_lock_killable(&fan_mutex))
7716 return -ERESTARTSYS;
7718 rc = 0;
7719 switch (fan_control_access_mode) {
7720 case TPACPI_FAN_WR_ACPI_FANS:
7721 if (speed >= 0 && speed <= 65535) {
7722 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7723 speed, speed, speed))
7724 rc = -EIO;
7725 } else
7726 rc = -EINVAL;
7727 break;
7729 default:
7730 rc = -ENXIO;
7733 mutex_unlock(&fan_mutex);
7734 return rc;
7737 static void fan_watchdog_reset(void)
7739 static int fan_watchdog_active;
7741 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7742 return;
7744 if (fan_watchdog_active)
7745 cancel_delayed_work(&fan_watchdog_task);
7747 if (fan_watchdog_maxinterval > 0 &&
7748 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7749 fan_watchdog_active = 1;
7750 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7751 msecs_to_jiffies(fan_watchdog_maxinterval
7752 * 1000))) {
7753 printk(TPACPI_ERR
7754 "failed to queue the fan watchdog, "
7755 "watchdog will not trigger\n");
7757 } else
7758 fan_watchdog_active = 0;
7761 static void fan_watchdog_fire(struct work_struct *ignored)
7763 int rc;
7765 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7766 return;
7768 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7769 rc = fan_set_enable();
7770 if (rc < 0) {
7771 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7772 "will try again later...\n", -rc);
7773 /* reschedule for later */
7774 fan_watchdog_reset();
7779 * SYSFS fan layout: hwmon compatible (device)
7781 * pwm*_enable:
7782 * 0: "disengaged" mode
7783 * 1: manual mode
7784 * 2: native EC "auto" mode (recommended, hardware default)
7786 * pwm*: set speed in manual mode, ignored otherwise.
7787 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7788 * interpolation.
7790 * fan*_input: tachometer reading, RPM
7793 * SYSFS fan layout: extensions
7795 * fan_watchdog (driver):
7796 * fan watchdog interval in seconds, 0 disables (default), max 120
7799 /* sysfs fan pwm1_enable ----------------------------------------------- */
7800 static ssize_t fan_pwm1_enable_show(struct device *dev,
7801 struct device_attribute *attr,
7802 char *buf)
7804 int res, mode;
7805 u8 status;
7807 res = fan_get_status_safe(&status);
7808 if (res)
7809 return res;
7811 if (status & TP_EC_FAN_FULLSPEED) {
7812 mode = 0;
7813 } else if (status & TP_EC_FAN_AUTO) {
7814 mode = 2;
7815 } else
7816 mode = 1;
7818 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7821 static ssize_t fan_pwm1_enable_store(struct device *dev,
7822 struct device_attribute *attr,
7823 const char *buf, size_t count)
7825 unsigned long t;
7826 int res, level;
7828 if (parse_strtoul(buf, 2, &t))
7829 return -EINVAL;
7831 tpacpi_disclose_usertask("hwmon pwm1_enable",
7832 "set fan mode to %lu\n", t);
7834 switch (t) {
7835 case 0:
7836 level = TP_EC_FAN_FULLSPEED;
7837 break;
7838 case 1:
7839 level = TPACPI_FAN_LAST_LEVEL;
7840 break;
7841 case 2:
7842 level = TP_EC_FAN_AUTO;
7843 break;
7844 case 3:
7845 /* reserved for software-controlled auto mode */
7846 return -ENOSYS;
7847 default:
7848 return -EINVAL;
7851 res = fan_set_level_safe(level);
7852 if (res == -ENXIO)
7853 return -EINVAL;
7854 else if (res < 0)
7855 return res;
7857 fan_watchdog_reset();
7859 return count;
7862 static struct device_attribute dev_attr_fan_pwm1_enable =
7863 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7864 fan_pwm1_enable_show, fan_pwm1_enable_store);
7866 /* sysfs fan pwm1 ------------------------------------------------------ */
7867 static ssize_t fan_pwm1_show(struct device *dev,
7868 struct device_attribute *attr,
7869 char *buf)
7871 int res;
7872 u8 status;
7874 res = fan_get_status_safe(&status);
7875 if (res)
7876 return res;
7878 if ((status &
7879 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7880 status = fan_control_desired_level;
7882 if (status > 7)
7883 status = 7;
7885 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7888 static ssize_t fan_pwm1_store(struct device *dev,
7889 struct device_attribute *attr,
7890 const char *buf, size_t count)
7892 unsigned long s;
7893 int rc;
7894 u8 status, newlevel;
7896 if (parse_strtoul(buf, 255, &s))
7897 return -EINVAL;
7899 tpacpi_disclose_usertask("hwmon pwm1",
7900 "set fan speed to %lu\n", s);
7902 /* scale down from 0-255 to 0-7 */
7903 newlevel = (s >> 5) & 0x07;
7905 if (mutex_lock_killable(&fan_mutex))
7906 return -ERESTARTSYS;
7908 rc = fan_get_status(&status);
7909 if (!rc && (status &
7910 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7911 rc = fan_set_level(newlevel);
7912 if (rc == -ENXIO)
7913 rc = -EINVAL;
7914 else if (!rc) {
7915 fan_update_desired_level(newlevel);
7916 fan_watchdog_reset();
7920 mutex_unlock(&fan_mutex);
7921 return (rc)? rc : count;
7924 static struct device_attribute dev_attr_fan_pwm1 =
7925 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7926 fan_pwm1_show, fan_pwm1_store);
7928 /* sysfs fan fan1_input ------------------------------------------------ */
7929 static ssize_t fan_fan1_input_show(struct device *dev,
7930 struct device_attribute *attr,
7931 char *buf)
7933 int res;
7934 unsigned int speed;
7936 res = fan_get_speed(&speed);
7937 if (res < 0)
7938 return res;
7940 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7943 static struct device_attribute dev_attr_fan_fan1_input =
7944 __ATTR(fan1_input, S_IRUGO,
7945 fan_fan1_input_show, NULL);
7947 /* sysfs fan fan2_input ------------------------------------------------ */
7948 static ssize_t fan_fan2_input_show(struct device *dev,
7949 struct device_attribute *attr,
7950 char *buf)
7952 int res;
7953 unsigned int speed;
7955 res = fan2_get_speed(&speed);
7956 if (res < 0)
7957 return res;
7959 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7962 static struct device_attribute dev_attr_fan_fan2_input =
7963 __ATTR(fan2_input, S_IRUGO,
7964 fan_fan2_input_show, NULL);
7966 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7967 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7968 char *buf)
7970 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7973 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7974 const char *buf, size_t count)
7976 unsigned long t;
7978 if (parse_strtoul(buf, 120, &t))
7979 return -EINVAL;
7981 if (!fan_control_allowed)
7982 return -EPERM;
7984 fan_watchdog_maxinterval = t;
7985 fan_watchdog_reset();
7987 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7989 return count;
7992 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7993 fan_fan_watchdog_show, fan_fan_watchdog_store);
7995 /* --------------------------------------------------------------------- */
7996 static struct attribute *fan_attributes[] = {
7997 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7998 &dev_attr_fan_fan1_input.attr,
7999 NULL, /* for fan2_input */
8000 NULL
8003 static const struct attribute_group fan_attr_group = {
8004 .attrs = fan_attributes,
8007 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8008 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8010 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8011 { .vendor = PCI_VENDOR_ID_IBM, \
8012 .bios = TPACPI_MATCH_ANY, \
8013 .ec = TPID(__id1, __id2), \
8014 .quirks = __quirks }
8016 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8017 { .vendor = PCI_VENDOR_ID_LENOVO, \
8018 .bios = TPACPI_MATCH_ANY, \
8019 .ec = TPID(__id1, __id2), \
8020 .quirks = __quirks }
8022 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8023 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8024 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8025 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8026 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8027 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8030 #undef TPACPI_FAN_QL
8031 #undef TPACPI_FAN_QI
8033 static int __init fan_init(struct ibm_init_struct *iibm)
8035 int rc;
8036 unsigned long quirks;
8038 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8039 "initializing fan subdriver\n");
8041 mutex_init(&fan_mutex);
8042 fan_status_access_mode = TPACPI_FAN_NONE;
8043 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8044 fan_control_commands = 0;
8045 fan_watchdog_maxinterval = 0;
8046 tp_features.fan_ctrl_status_undef = 0;
8047 tp_features.second_fan = 0;
8048 fan_control_desired_level = 7;
8050 if (tpacpi_is_ibm()) {
8051 TPACPI_ACPIHANDLE_INIT(fans);
8052 TPACPI_ACPIHANDLE_INIT(gfan);
8053 TPACPI_ACPIHANDLE_INIT(sfan);
8056 quirks = tpacpi_check_quirks(fan_quirk_table,
8057 ARRAY_SIZE(fan_quirk_table));
8059 if (gfan_handle) {
8060 /* 570, 600e/x, 770e, 770x */
8061 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8062 } else {
8063 /* all other ThinkPads: note that even old-style
8064 * ThinkPad ECs supports the fan control register */
8065 if (likely(acpi_ec_read(fan_status_offset,
8066 &fan_control_initial_status))) {
8067 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8068 if (quirks & TPACPI_FAN_Q1)
8069 fan_quirk1_setup();
8070 if (quirks & TPACPI_FAN_2FAN) {
8071 tp_features.second_fan = 1;
8072 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8073 "secondary fan support enabled\n");
8075 } else {
8076 printk(TPACPI_ERR
8077 "ThinkPad ACPI EC access misbehaving, "
8078 "fan status and control unavailable\n");
8079 return 1;
8083 if (sfan_handle) {
8084 /* 570, 770x-JL */
8085 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8086 fan_control_commands |=
8087 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8088 } else {
8089 if (!gfan_handle) {
8090 /* gfan without sfan means no fan control */
8091 /* all other models implement TP EC 0x2f control */
8093 if (fans_handle) {
8094 /* X31, X40, X41 */
8095 fan_control_access_mode =
8096 TPACPI_FAN_WR_ACPI_FANS;
8097 fan_control_commands |=
8098 TPACPI_FAN_CMD_SPEED |
8099 TPACPI_FAN_CMD_LEVEL |
8100 TPACPI_FAN_CMD_ENABLE;
8101 } else {
8102 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8103 fan_control_commands |=
8104 TPACPI_FAN_CMD_LEVEL |
8105 TPACPI_FAN_CMD_ENABLE;
8110 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8111 "fan is %s, modes %d, %d\n",
8112 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8113 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8114 fan_status_access_mode, fan_control_access_mode);
8116 /* fan control master switch */
8117 if (!fan_control_allowed) {
8118 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8119 fan_control_commands = 0;
8120 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8121 "fan control features disabled by parameter\n");
8124 /* update fan_control_desired_level */
8125 if (fan_status_access_mode != TPACPI_FAN_NONE)
8126 fan_get_status_safe(NULL);
8128 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8129 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8130 if (tp_features.second_fan) {
8131 /* attach second fan tachometer */
8132 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8133 &dev_attr_fan_fan2_input.attr;
8135 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8136 &fan_attr_group);
8137 if (rc < 0)
8138 return rc;
8140 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8141 &driver_attr_fan_watchdog);
8142 if (rc < 0) {
8143 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8144 &fan_attr_group);
8145 return rc;
8147 return 0;
8148 } else
8149 return 1;
8152 static void fan_exit(void)
8154 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8155 "cancelling any pending fan watchdog tasks\n");
8157 /* FIXME: can we really do this unconditionally? */
8158 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8159 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8160 &driver_attr_fan_watchdog);
8162 cancel_delayed_work(&fan_watchdog_task);
8163 flush_workqueue(tpacpi_wq);
8166 static void fan_suspend(pm_message_t state)
8168 int rc;
8170 if (!fan_control_allowed)
8171 return;
8173 /* Store fan status in cache */
8174 fan_control_resume_level = 0;
8175 rc = fan_get_status_safe(&fan_control_resume_level);
8176 if (rc < 0)
8177 printk(TPACPI_NOTICE
8178 "failed to read fan level for later "
8179 "restore during resume: %d\n", rc);
8181 /* if it is undefined, don't attempt to restore it.
8182 * KEEP THIS LAST */
8183 if (tp_features.fan_ctrl_status_undef)
8184 fan_control_resume_level = 0;
8187 static void fan_resume(void)
8189 u8 current_level = 7;
8190 bool do_set = false;
8191 int rc;
8193 /* DSDT *always* updates status on resume */
8194 tp_features.fan_ctrl_status_undef = 0;
8196 if (!fan_control_allowed ||
8197 !fan_control_resume_level ||
8198 (fan_get_status_safe(&current_level) < 0))
8199 return;
8201 switch (fan_control_access_mode) {
8202 case TPACPI_FAN_WR_ACPI_SFAN:
8203 /* never decrease fan level */
8204 do_set = (fan_control_resume_level > current_level);
8205 break;
8206 case TPACPI_FAN_WR_ACPI_FANS:
8207 case TPACPI_FAN_WR_TPEC:
8208 /* never decrease fan level, scale is:
8209 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8211 * We expect the firmware to set either 7 or AUTO, but we
8212 * handle FULLSPEED out of paranoia.
8214 * So, we can safely only restore FULLSPEED or 7, anything
8215 * else could slow the fan. Restoring AUTO is useless, at
8216 * best that's exactly what the DSDT already set (it is the
8217 * slower it uses).
8219 * Always keep in mind that the DSDT *will* have set the
8220 * fans to what the vendor supposes is the best level. We
8221 * muck with it only to speed the fan up.
8223 if (fan_control_resume_level != 7 &&
8224 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8225 return;
8226 else
8227 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8228 (current_level != fan_control_resume_level);
8229 break;
8230 default:
8231 return;
8233 if (do_set) {
8234 printk(TPACPI_NOTICE
8235 "restoring fan level to 0x%02x\n",
8236 fan_control_resume_level);
8237 rc = fan_set_level_safe(fan_control_resume_level);
8238 if (rc < 0)
8239 printk(TPACPI_NOTICE
8240 "failed to restore fan level: %d\n", rc);
8244 static int fan_read(struct seq_file *m)
8246 int rc;
8247 u8 status;
8248 unsigned int speed = 0;
8250 switch (fan_status_access_mode) {
8251 case TPACPI_FAN_RD_ACPI_GFAN:
8252 /* 570, 600e/x, 770e, 770x */
8253 rc = fan_get_status_safe(&status);
8254 if (rc < 0)
8255 return rc;
8257 seq_printf(m, "status:\t\t%s\n"
8258 "level:\t\t%d\n",
8259 (status != 0) ? "enabled" : "disabled", status);
8260 break;
8262 case TPACPI_FAN_RD_TPEC:
8263 /* all except 570, 600e/x, 770e, 770x */
8264 rc = fan_get_status_safe(&status);
8265 if (rc < 0)
8266 return rc;
8268 seq_printf(m, "status:\t\t%s\n",
8269 (status != 0) ? "enabled" : "disabled");
8271 rc = fan_get_speed(&speed);
8272 if (rc < 0)
8273 return rc;
8275 seq_printf(m, "speed:\t\t%d\n", speed);
8277 if (status & TP_EC_FAN_FULLSPEED)
8278 /* Disengaged mode takes precedence */
8279 seq_printf(m, "level:\t\tdisengaged\n");
8280 else if (status & TP_EC_FAN_AUTO)
8281 seq_printf(m, "level:\t\tauto\n");
8282 else
8283 seq_printf(m, "level:\t\t%d\n", status);
8284 break;
8286 case TPACPI_FAN_NONE:
8287 default:
8288 seq_printf(m, "status:\t\tnot supported\n");
8291 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8292 seq_printf(m, "commands:\tlevel <level>");
8294 switch (fan_control_access_mode) {
8295 case TPACPI_FAN_WR_ACPI_SFAN:
8296 seq_printf(m, " (<level> is 0-7)\n");
8297 break;
8299 default:
8300 seq_printf(m, " (<level> is 0-7, "
8301 "auto, disengaged, full-speed)\n");
8302 break;
8306 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8307 seq_printf(m, "commands:\tenable, disable\n"
8308 "commands:\twatchdog <timeout> (<timeout> "
8309 "is 0 (off), 1-120 (seconds))\n");
8311 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8312 seq_printf(m, "commands:\tspeed <speed>"
8313 " (<speed> is 0-65535)\n");
8315 return 0;
8318 static int fan_write_cmd_level(const char *cmd, int *rc)
8320 int level;
8322 if (strlencmp(cmd, "level auto") == 0)
8323 level = TP_EC_FAN_AUTO;
8324 else if ((strlencmp(cmd, "level disengaged") == 0) |
8325 (strlencmp(cmd, "level full-speed") == 0))
8326 level = TP_EC_FAN_FULLSPEED;
8327 else if (sscanf(cmd, "level %d", &level) != 1)
8328 return 0;
8330 *rc = fan_set_level_safe(level);
8331 if (*rc == -ENXIO)
8332 printk(TPACPI_ERR "level command accepted for unsupported "
8333 "access mode %d", fan_control_access_mode);
8334 else if (!*rc)
8335 tpacpi_disclose_usertask("procfs fan",
8336 "set level to %d\n", level);
8338 return 1;
8341 static int fan_write_cmd_enable(const char *cmd, int *rc)
8343 if (strlencmp(cmd, "enable") != 0)
8344 return 0;
8346 *rc = fan_set_enable();
8347 if (*rc == -ENXIO)
8348 printk(TPACPI_ERR "enable command accepted for unsupported "
8349 "access mode %d", fan_control_access_mode);
8350 else if (!*rc)
8351 tpacpi_disclose_usertask("procfs fan", "enable\n");
8353 return 1;
8356 static int fan_write_cmd_disable(const char *cmd, int *rc)
8358 if (strlencmp(cmd, "disable") != 0)
8359 return 0;
8361 *rc = fan_set_disable();
8362 if (*rc == -ENXIO)
8363 printk(TPACPI_ERR "disable command accepted for unsupported "
8364 "access mode %d", fan_control_access_mode);
8365 else if (!*rc)
8366 tpacpi_disclose_usertask("procfs fan", "disable\n");
8368 return 1;
8371 static int fan_write_cmd_speed(const char *cmd, int *rc)
8373 int speed;
8375 /* TODO:
8376 * Support speed <low> <medium> <high> ? */
8378 if (sscanf(cmd, "speed %d", &speed) != 1)
8379 return 0;
8381 *rc = fan_set_speed(speed);
8382 if (*rc == -ENXIO)
8383 printk(TPACPI_ERR "speed command accepted for unsupported "
8384 "access mode %d", fan_control_access_mode);
8385 else if (!*rc)
8386 tpacpi_disclose_usertask("procfs fan",
8387 "set speed to %d\n", speed);
8389 return 1;
8392 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8394 int interval;
8396 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8397 return 0;
8399 if (interval < 0 || interval > 120)
8400 *rc = -EINVAL;
8401 else {
8402 fan_watchdog_maxinterval = interval;
8403 tpacpi_disclose_usertask("procfs fan",
8404 "set watchdog timer to %d\n",
8405 interval);
8408 return 1;
8411 static int fan_write(char *buf)
8413 char *cmd;
8414 int rc = 0;
8416 while (!rc && (cmd = next_cmd(&buf))) {
8417 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8418 fan_write_cmd_level(cmd, &rc)) &&
8419 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8420 (fan_write_cmd_enable(cmd, &rc) ||
8421 fan_write_cmd_disable(cmd, &rc) ||
8422 fan_write_cmd_watchdog(cmd, &rc))) &&
8423 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8424 fan_write_cmd_speed(cmd, &rc))
8426 rc = -EINVAL;
8427 else if (!rc)
8428 fan_watchdog_reset();
8431 return rc;
8434 static struct ibm_struct fan_driver_data = {
8435 .name = "fan",
8436 .read = fan_read,
8437 .write = fan_write,
8438 .exit = fan_exit,
8439 .suspend = fan_suspend,
8440 .resume = fan_resume,
8443 /****************************************************************************
8444 ****************************************************************************
8446 * Infrastructure
8448 ****************************************************************************
8449 ****************************************************************************/
8452 * HKEY event callout for other subdrivers go here
8453 * (yes, it is ugly, but it is quick, safe, and gets the job done
8455 static void tpacpi_driver_event(const unsigned int hkey_event)
8457 if (ibm_backlight_device) {
8458 switch (hkey_event) {
8459 case TP_HKEY_EV_BRGHT_UP:
8460 case TP_HKEY_EV_BRGHT_DOWN:
8461 tpacpi_brightness_notify_change();
8464 if (alsa_card) {
8465 switch (hkey_event) {
8466 case TP_HKEY_EV_VOL_UP:
8467 case TP_HKEY_EV_VOL_DOWN:
8468 case TP_HKEY_EV_VOL_MUTE:
8469 volume_alsa_notify_change();
8474 static void hotkey_driver_event(const unsigned int scancode)
8476 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8479 /* sysfs name ---------------------------------------------------------- */
8480 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8481 struct device_attribute *attr,
8482 char *buf)
8484 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8487 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8488 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8490 /* --------------------------------------------------------------------- */
8492 /* /proc support */
8493 static struct proc_dir_entry *proc_dir;
8496 * Module and infrastructure proble, init and exit handling
8499 static int force_load;
8501 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8502 static const char * __init str_supported(int is_supported)
8504 static char text_unsupported[] __initdata = "not supported";
8506 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8508 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8510 static void ibm_exit(struct ibm_struct *ibm)
8512 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8514 list_del_init(&ibm->all_drivers);
8516 if (ibm->flags.acpi_notify_installed) {
8517 dbg_printk(TPACPI_DBG_EXIT,
8518 "%s: acpi_remove_notify_handler\n", ibm->name);
8519 BUG_ON(!ibm->acpi);
8520 acpi_remove_notify_handler(*ibm->acpi->handle,
8521 ibm->acpi->type,
8522 dispatch_acpi_notify);
8523 ibm->flags.acpi_notify_installed = 0;
8524 ibm->flags.acpi_notify_installed = 0;
8527 if (ibm->flags.proc_created) {
8528 dbg_printk(TPACPI_DBG_EXIT,
8529 "%s: remove_proc_entry\n", ibm->name);
8530 remove_proc_entry(ibm->name, proc_dir);
8531 ibm->flags.proc_created = 0;
8534 if (ibm->flags.acpi_driver_registered) {
8535 dbg_printk(TPACPI_DBG_EXIT,
8536 "%s: acpi_bus_unregister_driver\n", ibm->name);
8537 BUG_ON(!ibm->acpi);
8538 acpi_bus_unregister_driver(ibm->acpi->driver);
8539 kfree(ibm->acpi->driver);
8540 ibm->acpi->driver = NULL;
8541 ibm->flags.acpi_driver_registered = 0;
8544 if (ibm->flags.init_called && ibm->exit) {
8545 ibm->exit();
8546 ibm->flags.init_called = 0;
8549 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8552 static int __init ibm_init(struct ibm_init_struct *iibm)
8554 int ret;
8555 struct ibm_struct *ibm = iibm->data;
8556 struct proc_dir_entry *entry;
8558 BUG_ON(ibm == NULL);
8560 INIT_LIST_HEAD(&ibm->all_drivers);
8562 if (ibm->flags.experimental && !experimental)
8563 return 0;
8565 dbg_printk(TPACPI_DBG_INIT,
8566 "probing for %s\n", ibm->name);
8568 if (iibm->init) {
8569 ret = iibm->init(iibm);
8570 if (ret > 0)
8571 return 0; /* probe failed */
8572 if (ret)
8573 return ret;
8575 ibm->flags.init_called = 1;
8578 if (ibm->acpi) {
8579 if (ibm->acpi->hid) {
8580 ret = register_tpacpi_subdriver(ibm);
8581 if (ret)
8582 goto err_out;
8585 if (ibm->acpi->notify) {
8586 ret = setup_acpi_notify(ibm);
8587 if (ret == -ENODEV) {
8588 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8589 ibm->name);
8590 ret = 0;
8591 goto err_out;
8593 if (ret < 0)
8594 goto err_out;
8598 dbg_printk(TPACPI_DBG_INIT,
8599 "%s installed\n", ibm->name);
8601 if (ibm->read) {
8602 mode_t mode = iibm->base_procfs_mode;
8604 if (!mode)
8605 mode = S_IRUGO;
8606 if (ibm->write)
8607 mode |= S_IWUSR;
8608 entry = proc_create_data(ibm->name, mode, proc_dir,
8609 &dispatch_proc_fops, ibm);
8610 if (!entry) {
8611 printk(TPACPI_ERR "unable to create proc entry %s\n",
8612 ibm->name);
8613 ret = -ENODEV;
8614 goto err_out;
8616 ibm->flags.proc_created = 1;
8619 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8621 return 0;
8623 err_out:
8624 dbg_printk(TPACPI_DBG_INIT,
8625 "%s: at error exit path with result %d\n",
8626 ibm->name, ret);
8628 ibm_exit(ibm);
8629 return (ret < 0)? ret : 0;
8632 /* Probing */
8634 static bool __pure __init tpacpi_is_fw_digit(const char c)
8636 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8639 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8640 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8641 const char t)
8643 return s && strlen(s) >= 8 &&
8644 tpacpi_is_fw_digit(s[0]) &&
8645 tpacpi_is_fw_digit(s[1]) &&
8646 s[2] == t && s[3] == 'T' &&
8647 tpacpi_is_fw_digit(s[4]) &&
8648 tpacpi_is_fw_digit(s[5]) &&
8649 s[6] == 'W' && s[7] == 'W';
8652 /* returns 0 - probe ok, or < 0 - probe error.
8653 * Probe ok doesn't mean thinkpad found.
8654 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8655 static int __must_check __init get_thinkpad_model_data(
8656 struct thinkpad_id_data *tp)
8658 const struct dmi_device *dev = NULL;
8659 char ec_fw_string[18];
8660 char const *s;
8662 if (!tp)
8663 return -EINVAL;
8665 memset(tp, 0, sizeof(*tp));
8667 if (dmi_name_in_vendors("IBM"))
8668 tp->vendor = PCI_VENDOR_ID_IBM;
8669 else if (dmi_name_in_vendors("LENOVO"))
8670 tp->vendor = PCI_VENDOR_ID_LENOVO;
8671 else
8672 return 0;
8674 s = dmi_get_system_info(DMI_BIOS_VERSION);
8675 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8676 if (s && !tp->bios_version_str)
8677 return -ENOMEM;
8679 /* Really ancient ThinkPad 240X will fail this, which is fine */
8680 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8681 return 0;
8683 tp->bios_model = tp->bios_version_str[0]
8684 | (tp->bios_version_str[1] << 8);
8685 tp->bios_release = (tp->bios_version_str[4] << 8)
8686 | tp->bios_version_str[5];
8689 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8690 * X32 or newer, all Z series; Some models must have an
8691 * up-to-date BIOS or they will not be detected.
8693 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8695 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8696 if (sscanf(dev->name,
8697 "IBM ThinkPad Embedded Controller -[%17c",
8698 ec_fw_string) == 1) {
8699 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8700 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8702 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8703 if (!tp->ec_version_str)
8704 return -ENOMEM;
8706 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8707 tp->ec_model = ec_fw_string[0]
8708 | (ec_fw_string[1] << 8);
8709 tp->ec_release = (ec_fw_string[4] << 8)
8710 | ec_fw_string[5];
8711 } else {
8712 printk(TPACPI_NOTICE
8713 "ThinkPad firmware release %s "
8714 "doesn't match the known patterns\n",
8715 ec_fw_string);
8716 printk(TPACPI_NOTICE
8717 "please report this to %s\n",
8718 TPACPI_MAIL);
8720 break;
8724 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8725 if (s && !strnicmp(s, "ThinkPad", 8)) {
8726 tp->model_str = kstrdup(s, GFP_KERNEL);
8727 if (!tp->model_str)
8728 return -ENOMEM;
8731 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8732 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8733 if (s && !tp->nummodel_str)
8734 return -ENOMEM;
8736 return 0;
8739 static int __init probe_for_thinkpad(void)
8741 int is_thinkpad;
8743 if (acpi_disabled)
8744 return -ENODEV;
8746 /* It would be dangerous to run the driver in this case */
8747 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8748 return -ENODEV;
8751 * Non-ancient models have better DMI tagging, but very old models
8752 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8754 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8755 (thinkpad_id.ec_model != 0) ||
8756 tpacpi_is_fw_known();
8758 /* ec is required because many other handles are relative to it */
8759 TPACPI_ACPIHANDLE_INIT(ec);
8760 if (!ec_handle) {
8761 if (is_thinkpad)
8762 printk(TPACPI_ERR
8763 "Not yet supported ThinkPad detected!\n");
8764 return -ENODEV;
8767 if (!is_thinkpad && !force_load)
8768 return -ENODEV;
8770 return 0;
8773 static void __init thinkpad_acpi_init_banner(void)
8775 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8776 printk(TPACPI_INFO "%s\n", TPACPI_URL);
8778 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
8779 (thinkpad_id.bios_version_str) ?
8780 thinkpad_id.bios_version_str : "unknown",
8781 (thinkpad_id.ec_version_str) ?
8782 thinkpad_id.ec_version_str : "unknown");
8784 BUG_ON(!thinkpad_id.vendor);
8786 if (thinkpad_id.model_str)
8787 printk(TPACPI_INFO "%s %s, model %s\n",
8788 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8789 "IBM" : ((thinkpad_id.vendor ==
8790 PCI_VENDOR_ID_LENOVO) ?
8791 "Lenovo" : "Unknown vendor"),
8792 thinkpad_id.model_str,
8793 (thinkpad_id.nummodel_str) ?
8794 thinkpad_id.nummodel_str : "unknown");
8797 /* Module init, exit, parameters */
8799 static struct ibm_init_struct ibms_init[] __initdata = {
8801 .data = &thinkpad_acpi_driver_data,
8804 .init = hotkey_init,
8805 .data = &hotkey_driver_data,
8808 .init = bluetooth_init,
8809 .data = &bluetooth_driver_data,
8812 .init = wan_init,
8813 .data = &wan_driver_data,
8816 .init = uwb_init,
8817 .data = &uwb_driver_data,
8819 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8821 .init = video_init,
8822 .base_procfs_mode = S_IRUSR,
8823 .data = &video_driver_data,
8825 #endif
8827 .init = light_init,
8828 .data = &light_driver_data,
8831 .init = cmos_init,
8832 .data = &cmos_driver_data,
8835 .init = led_init,
8836 .data = &led_driver_data,
8839 .init = beep_init,
8840 .data = &beep_driver_data,
8843 .init = thermal_init,
8844 .data = &thermal_driver_data,
8847 .data = &ecdump_driver_data,
8850 .init = brightness_init,
8851 .data = &brightness_driver_data,
8854 .init = volume_init,
8855 .data = &volume_driver_data,
8858 .init = fan_init,
8859 .data = &fan_driver_data,
8863 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8865 unsigned int i;
8866 struct ibm_struct *ibm;
8868 if (!kp || !kp->name || !val)
8869 return -EINVAL;
8871 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8872 ibm = ibms_init[i].data;
8873 WARN_ON(ibm == NULL);
8875 if (!ibm || !ibm->name)
8876 continue;
8878 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8879 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8880 return -ENOSPC;
8881 strcpy(ibms_init[i].param, val);
8882 strcat(ibms_init[i].param, ",");
8883 return 0;
8887 return -EINVAL;
8890 module_param(experimental, int, 0444);
8891 MODULE_PARM_DESC(experimental,
8892 "Enables experimental features when non-zero");
8894 module_param_named(debug, dbg_level, uint, 0);
8895 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8897 module_param(force_load, bool, 0444);
8898 MODULE_PARM_DESC(force_load,
8899 "Attempts to load the driver even on a "
8900 "mis-identified ThinkPad when true");
8902 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8903 MODULE_PARM_DESC(fan_control,
8904 "Enables setting fan parameters features when true");
8906 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8907 MODULE_PARM_DESC(brightness_mode,
8908 "Selects brightness control strategy: "
8909 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8911 module_param(brightness_enable, uint, 0444);
8912 MODULE_PARM_DESC(brightness_enable,
8913 "Enables backlight control when 1, disables when 0");
8915 module_param(hotkey_report_mode, uint, 0444);
8916 MODULE_PARM_DESC(hotkey_report_mode,
8917 "used for backwards compatibility with userspace, "
8918 "see documentation");
8920 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8921 module_param_named(volume_mode, volume_mode, uint, 0444);
8922 MODULE_PARM_DESC(volume_mode,
8923 "Selects volume control strategy: "
8924 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8926 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8927 MODULE_PARM_DESC(volume_capabilities,
8928 "Selects the mixer capabilites: "
8929 "0=auto, 1=volume and mute, 2=mute only");
8931 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8932 MODULE_PARM_DESC(volume_control,
8933 "Enables software override for the console audio "
8934 "control when true");
8936 /* ALSA module API parameters */
8937 module_param_named(index, alsa_index, int, 0444);
8938 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8939 module_param_named(id, alsa_id, charp, 0444);
8940 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8941 module_param_named(enable, alsa_enable, bool, 0444);
8942 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8943 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8945 #define TPACPI_PARAM(feature) \
8946 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8947 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8948 "at module load, see documentation")
8950 TPACPI_PARAM(hotkey);
8951 TPACPI_PARAM(bluetooth);
8952 TPACPI_PARAM(video);
8953 TPACPI_PARAM(light);
8954 TPACPI_PARAM(cmos);
8955 TPACPI_PARAM(led);
8956 TPACPI_PARAM(beep);
8957 TPACPI_PARAM(ecdump);
8958 TPACPI_PARAM(brightness);
8959 TPACPI_PARAM(volume);
8960 TPACPI_PARAM(fan);
8962 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8963 module_param(dbg_wlswemul, uint, 0444);
8964 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8965 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8966 MODULE_PARM_DESC(wlsw_state,
8967 "Initial state of the emulated WLSW switch");
8969 module_param(dbg_bluetoothemul, uint, 0444);
8970 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8971 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8972 MODULE_PARM_DESC(bluetooth_state,
8973 "Initial state of the emulated bluetooth switch");
8975 module_param(dbg_wwanemul, uint, 0444);
8976 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8977 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8978 MODULE_PARM_DESC(wwan_state,
8979 "Initial state of the emulated WWAN switch");
8981 module_param(dbg_uwbemul, uint, 0444);
8982 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8983 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8984 MODULE_PARM_DESC(uwb_state,
8985 "Initial state of the emulated UWB switch");
8986 #endif
8988 static void thinkpad_acpi_module_exit(void)
8990 struct ibm_struct *ibm, *itmp;
8992 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8994 list_for_each_entry_safe_reverse(ibm, itmp,
8995 &tpacpi_all_drivers,
8996 all_drivers) {
8997 ibm_exit(ibm);
9000 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9002 if (tpacpi_inputdev) {
9003 if (tp_features.input_device_registered)
9004 input_unregister_device(tpacpi_inputdev);
9005 else
9006 input_free_device(tpacpi_inputdev);
9009 if (tpacpi_hwmon)
9010 hwmon_device_unregister(tpacpi_hwmon);
9012 if (tp_features.sensors_pdev_attrs_registered)
9013 device_remove_file(&tpacpi_sensors_pdev->dev,
9014 &dev_attr_thinkpad_acpi_pdev_name);
9015 if (tpacpi_sensors_pdev)
9016 platform_device_unregister(tpacpi_sensors_pdev);
9017 if (tpacpi_pdev)
9018 platform_device_unregister(tpacpi_pdev);
9020 if (tp_features.sensors_pdrv_attrs_registered)
9021 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9022 if (tp_features.platform_drv_attrs_registered)
9023 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9025 if (tp_features.sensors_pdrv_registered)
9026 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9028 if (tp_features.platform_drv_registered)
9029 platform_driver_unregister(&tpacpi_pdriver);
9031 if (proc_dir)
9032 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9034 if (tpacpi_wq)
9035 destroy_workqueue(tpacpi_wq);
9037 kfree(thinkpad_id.bios_version_str);
9038 kfree(thinkpad_id.ec_version_str);
9039 kfree(thinkpad_id.model_str);
9043 static int __init thinkpad_acpi_module_init(void)
9045 int ret, i;
9047 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9049 /* Parameter checking */
9050 if (hotkey_report_mode > 2)
9051 return -EINVAL;
9053 /* Driver-level probe */
9055 ret = get_thinkpad_model_data(&thinkpad_id);
9056 if (ret) {
9057 printk(TPACPI_ERR
9058 "unable to get DMI data: %d\n", ret);
9059 thinkpad_acpi_module_exit();
9060 return ret;
9062 ret = probe_for_thinkpad();
9063 if (ret) {
9064 thinkpad_acpi_module_exit();
9065 return ret;
9068 /* Driver initialization */
9070 thinkpad_acpi_init_banner();
9071 tpacpi_check_outdated_fw();
9073 TPACPI_ACPIHANDLE_INIT(ecrd);
9074 TPACPI_ACPIHANDLE_INIT(ecwr);
9076 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9077 if (!tpacpi_wq) {
9078 thinkpad_acpi_module_exit();
9079 return -ENOMEM;
9082 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9083 if (!proc_dir) {
9084 printk(TPACPI_ERR
9085 "unable to create proc dir " TPACPI_PROC_DIR);
9086 thinkpad_acpi_module_exit();
9087 return -ENODEV;
9090 ret = platform_driver_register(&tpacpi_pdriver);
9091 if (ret) {
9092 printk(TPACPI_ERR
9093 "unable to register main platform driver\n");
9094 thinkpad_acpi_module_exit();
9095 return ret;
9097 tp_features.platform_drv_registered = 1;
9099 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9100 if (ret) {
9101 printk(TPACPI_ERR
9102 "unable to register hwmon platform driver\n");
9103 thinkpad_acpi_module_exit();
9104 return ret;
9106 tp_features.sensors_pdrv_registered = 1;
9108 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9109 if (!ret) {
9110 tp_features.platform_drv_attrs_registered = 1;
9111 ret = tpacpi_create_driver_attributes(
9112 &tpacpi_hwmon_pdriver.driver);
9114 if (ret) {
9115 printk(TPACPI_ERR
9116 "unable to create sysfs driver attributes\n");
9117 thinkpad_acpi_module_exit();
9118 return ret;
9120 tp_features.sensors_pdrv_attrs_registered = 1;
9123 /* Device initialization */
9124 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9125 NULL, 0);
9126 if (IS_ERR(tpacpi_pdev)) {
9127 ret = PTR_ERR(tpacpi_pdev);
9128 tpacpi_pdev = NULL;
9129 printk(TPACPI_ERR "unable to register platform device\n");
9130 thinkpad_acpi_module_exit();
9131 return ret;
9133 tpacpi_sensors_pdev = platform_device_register_simple(
9134 TPACPI_HWMON_DRVR_NAME,
9135 -1, NULL, 0);
9136 if (IS_ERR(tpacpi_sensors_pdev)) {
9137 ret = PTR_ERR(tpacpi_sensors_pdev);
9138 tpacpi_sensors_pdev = NULL;
9139 printk(TPACPI_ERR
9140 "unable to register hwmon platform device\n");
9141 thinkpad_acpi_module_exit();
9142 return ret;
9144 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9145 &dev_attr_thinkpad_acpi_pdev_name);
9146 if (ret) {
9147 printk(TPACPI_ERR
9148 "unable to create sysfs hwmon device attributes\n");
9149 thinkpad_acpi_module_exit();
9150 return ret;
9152 tp_features.sensors_pdev_attrs_registered = 1;
9153 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9154 if (IS_ERR(tpacpi_hwmon)) {
9155 ret = PTR_ERR(tpacpi_hwmon);
9156 tpacpi_hwmon = NULL;
9157 printk(TPACPI_ERR "unable to register hwmon device\n");
9158 thinkpad_acpi_module_exit();
9159 return ret;
9161 mutex_init(&tpacpi_inputdev_send_mutex);
9162 tpacpi_inputdev = input_allocate_device();
9163 if (!tpacpi_inputdev) {
9164 printk(TPACPI_ERR "unable to allocate input device\n");
9165 thinkpad_acpi_module_exit();
9166 return -ENOMEM;
9167 } else {
9168 /* Prepare input device, but don't register */
9169 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9170 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9171 tpacpi_inputdev->id.bustype = BUS_HOST;
9172 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9173 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9174 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9175 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9178 /* Init subdriver dependencies */
9179 tpacpi_detect_brightness_capabilities();
9181 /* Init subdrivers */
9182 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9183 ret = ibm_init(&ibms_init[i]);
9184 if (ret >= 0 && *ibms_init[i].param)
9185 ret = ibms_init[i].data->write(ibms_init[i].param);
9186 if (ret < 0) {
9187 thinkpad_acpi_module_exit();
9188 return ret;
9192 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9194 ret = input_register_device(tpacpi_inputdev);
9195 if (ret < 0) {
9196 printk(TPACPI_ERR "unable to register input device\n");
9197 thinkpad_acpi_module_exit();
9198 return ret;
9199 } else {
9200 tp_features.input_device_registered = 1;
9203 return 0;
9206 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9209 * This will autoload the driver in almost every ThinkPad
9210 * in widespread use.
9212 * Only _VERY_ old models, like the 240, 240x and 570 lack
9213 * the HKEY event interface.
9215 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9218 * DMI matching for module autoloading
9220 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9221 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9223 * Only models listed in thinkwiki will be supported, so add yours
9224 * if it is not there yet.
9226 #define IBM_BIOS_MODULE_ALIAS(__type) \
9227 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9229 /* Ancient thinkpad BIOSes have to be identified by
9230 * BIOS type or model number, and there are far less
9231 * BIOS types than model numbers... */
9232 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9234 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9235 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9236 MODULE_DESCRIPTION(TPACPI_DESC);
9237 MODULE_VERSION(TPACPI_VERSION);
9238 MODULE_LICENSE("GPL");
9240 module_init(thinkpad_acpi_module_init);
9241 module_exit(thinkpad_acpi_module_exit);