thinkpad-acpi: untangle ACPI/vendor backlight selection
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobe99582f33cea63c33221b66c909f01782a2f080d
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.24"
25 #define TPACPI_SYSFS_VERSION 0x020700
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/seq_file.h>
65 #include <linux/sysfs.h>
66 #include <linux/backlight.h>
67 #include <linux/fb.h>
68 #include <linux/platform_device.h>
69 #include <linux/hwmon.h>
70 #include <linux/hwmon-sysfs.h>
71 #include <linux/input.h>
72 #include <linux/leds.h>
73 #include <linux/rfkill.h>
74 #include <asm/uaccess.h>
76 #include <linux/dmi.h>
77 #include <linux/jiffies.h>
78 #include <linux/workqueue.h>
80 #include <sound/core.h>
81 #include <sound/control.h>
82 #include <sound/initval.h>
84 #include <acpi/acpi_drivers.h>
86 #include <linux/pci_ids.h>
89 /* ThinkPad CMOS commands */
90 #define TP_CMOS_VOLUME_DOWN 0
91 #define TP_CMOS_VOLUME_UP 1
92 #define TP_CMOS_VOLUME_MUTE 2
93 #define TP_CMOS_BRIGHTNESS_UP 4
94 #define TP_CMOS_BRIGHTNESS_DOWN 5
95 #define TP_CMOS_THINKLIGHT_ON 12
96 #define TP_CMOS_THINKLIGHT_OFF 13
98 /* NVRAM Addresses */
99 enum tp_nvram_addr {
100 TP_NVRAM_ADDR_HK2 = 0x57,
101 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
102 TP_NVRAM_ADDR_VIDEO = 0x59,
103 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
104 TP_NVRAM_ADDR_MIXER = 0x60,
107 /* NVRAM bit masks */
108 enum {
109 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
110 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
111 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
112 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
113 TP_NVRAM_MASK_THINKLIGHT = 0x10,
114 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
115 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
116 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
117 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
118 TP_NVRAM_MASK_MUTE = 0x40,
119 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
120 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
121 TP_NVRAM_POS_LEVEL_VOLUME = 0,
124 /* Misc NVRAM-related */
125 enum {
126 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
129 /* ACPI HIDs */
130 #define TPACPI_ACPI_HKEY_HID "IBM0068"
131 #define TPACPI_ACPI_EC_HID "PNP0C09"
133 /* Input IDs */
134 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
135 #define TPACPI_HKEY_INPUT_VERSION 0x4101
137 /* ACPI \WGSV commands */
138 enum {
139 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
140 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
141 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
142 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
145 /* TP_ACPI_WGSV_GET_STATE bits */
146 enum {
147 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
148 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
149 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
150 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
151 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
152 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
153 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
154 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
155 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
156 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
159 /* HKEY events */
160 enum tpacpi_hkey_event_t {
161 /* Hotkey-related */
162 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
163 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
164 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
165 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
166 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
167 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
169 /* Reasons for waking up from S3/S4 */
170 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
171 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
172 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
173 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
174 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
175 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
177 /* Auto-sleep after eject request */
178 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
179 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
181 /* Misc bay events */
182 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
184 /* User-interface events */
185 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
186 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
187 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
188 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
189 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
190 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
191 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
193 /* Thermal events */
194 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
195 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
196 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
197 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
198 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
200 /* Misc */
201 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
204 /****************************************************************************
205 * Main driver
208 #define TPACPI_NAME "thinkpad"
209 #define TPACPI_DESC "ThinkPad ACPI Extras"
210 #define TPACPI_FILE TPACPI_NAME "_acpi"
211 #define TPACPI_URL "http://ibm-acpi.sf.net/"
212 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
214 #define TPACPI_PROC_DIR "ibm"
215 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
216 #define TPACPI_DRVR_NAME TPACPI_FILE
217 #define TPACPI_DRVR_SHORTNAME "tpacpi"
218 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
220 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
221 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
223 #define TPACPI_MAX_ACPI_ARGS 3
225 /* printk headers */
226 #define TPACPI_LOG TPACPI_FILE ": "
227 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
228 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
229 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
230 #define TPACPI_ERR KERN_ERR TPACPI_LOG
231 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
232 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
233 #define TPACPI_INFO KERN_INFO TPACPI_LOG
234 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
236 /* Debugging printk groups */
237 #define TPACPI_DBG_ALL 0xffff
238 #define TPACPI_DBG_DISCLOSETASK 0x8000
239 #define TPACPI_DBG_INIT 0x0001
240 #define TPACPI_DBG_EXIT 0x0002
241 #define TPACPI_DBG_RFKILL 0x0004
242 #define TPACPI_DBG_HKEY 0x0008
243 #define TPACPI_DBG_FAN 0x0010
244 #define TPACPI_DBG_BRGHT 0x0020
245 #define TPACPI_DBG_MIXER 0x0040
247 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
248 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
249 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
252 /****************************************************************************
253 * Driver-wide structs and misc. variables
256 struct ibm_struct;
258 struct tp_acpi_drv_struct {
259 const struct acpi_device_id *hid;
260 struct acpi_driver *driver;
262 void (*notify) (struct ibm_struct *, u32);
263 acpi_handle *handle;
264 u32 type;
265 struct acpi_device *device;
268 struct ibm_struct {
269 char *name;
271 int (*read) (struct seq_file *);
272 int (*write) (char *);
273 void (*exit) (void);
274 void (*resume) (void);
275 void (*suspend) (pm_message_t state);
276 void (*shutdown) (void);
278 struct list_head all_drivers;
280 struct tp_acpi_drv_struct *acpi;
282 struct {
283 u8 acpi_driver_registered:1;
284 u8 acpi_notify_installed:1;
285 u8 proc_created:1;
286 u8 init_called:1;
287 u8 experimental:1;
288 } flags;
291 struct ibm_init_struct {
292 char param[32];
294 int (*init) (struct ibm_init_struct *);
295 mode_t base_procfs_mode;
296 struct ibm_struct *data;
299 static struct {
300 u32 bluetooth:1;
301 u32 hotkey:1;
302 u32 hotkey_mask:1;
303 u32 hotkey_wlsw:1;
304 u32 hotkey_tablet:1;
305 u32 light:1;
306 u32 light_status:1;
307 u32 bright_acpimode:1;
308 u32 bright_unkfw:1;
309 u32 wan:1;
310 u32 uwb:1;
311 u32 fan_ctrl_status_undef:1;
312 u32 second_fan:1;
313 u32 beep_needs_two_args:1;
314 u32 mixer_no_level_control:1;
315 u32 input_device_registered:1;
316 u32 platform_drv_registered:1;
317 u32 platform_drv_attrs_registered:1;
318 u32 sensors_pdrv_registered:1;
319 u32 sensors_pdrv_attrs_registered:1;
320 u32 sensors_pdev_attrs_registered:1;
321 u32 hotkey_poll_active:1;
322 } tp_features;
324 static struct {
325 u16 hotkey_mask_ff:1;
326 u16 volume_ctrl_forbidden:1;
327 } tp_warned;
329 struct thinkpad_id_data {
330 unsigned int vendor; /* ThinkPad vendor:
331 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
333 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
334 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
336 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
337 u16 ec_model;
338 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
339 u16 ec_release;
341 char *model_str; /* ThinkPad T43 */
342 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
344 static struct thinkpad_id_data thinkpad_id;
346 static enum {
347 TPACPI_LIFE_INIT = 0,
348 TPACPI_LIFE_RUNNING,
349 TPACPI_LIFE_EXITING,
350 } tpacpi_lifecycle;
352 static int experimental;
353 static u32 dbg_level;
355 static struct workqueue_struct *tpacpi_wq;
357 enum led_status_t {
358 TPACPI_LED_OFF = 0,
359 TPACPI_LED_ON,
360 TPACPI_LED_BLINK,
363 /* Special LED class that can defer work */
364 struct tpacpi_led_classdev {
365 struct led_classdev led_classdev;
366 struct work_struct work;
367 enum led_status_t new_state;
368 unsigned int led;
371 /* brightness level capabilities */
372 static unsigned int bright_maxlvl; /* 0 = unknown */
374 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
375 static int dbg_wlswemul;
376 static int tpacpi_wlsw_emulstate;
377 static int dbg_bluetoothemul;
378 static int tpacpi_bluetooth_emulstate;
379 static int dbg_wwanemul;
380 static int tpacpi_wwan_emulstate;
381 static int dbg_uwbemul;
382 static int tpacpi_uwb_emulstate;
383 #endif
386 /*************************************************************************
387 * Debugging helpers
390 #define dbg_printk(a_dbg_level, format, arg...) \
391 do { if (dbg_level & (a_dbg_level)) \
392 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
393 } while (0)
395 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
396 #define vdbg_printk dbg_printk
397 static const char *str_supported(int is_supported);
398 #else
399 #define vdbg_printk(a_dbg_level, format, arg...) \
400 do { } while (0)
401 #endif
403 static void tpacpi_log_usertask(const char * const what)
405 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
406 what, task_tgid_vnr(current));
409 #define tpacpi_disclose_usertask(what, format, arg...) \
410 do { \
411 if (unlikely( \
412 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
413 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
414 printk(TPACPI_DEBUG "%s: PID %d: " format, \
415 what, task_tgid_vnr(current), ## arg); \
417 } while (0)
420 * Quirk handling helpers
422 * ThinkPad IDs and versions seen in the field so far
423 * are two-characters from the set [0-9A-Z], i.e. base 36.
425 * We use values well outside that range as specials.
428 #define TPACPI_MATCH_ANY 0xffffU
429 #define TPACPI_MATCH_UNKNOWN 0U
431 /* TPID('1', 'Y') == 0x5931 */
432 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
434 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
435 { .vendor = PCI_VENDOR_ID_IBM, \
436 .bios = TPID(__id1, __id2), \
437 .ec = TPACPI_MATCH_ANY, \
438 .quirks = (__quirk) }
440 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
441 { .vendor = PCI_VENDOR_ID_LENOVO, \
442 .bios = TPID(__id1, __id2), \
443 .ec = TPACPI_MATCH_ANY, \
444 .quirks = (__quirk) }
446 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
447 { .vendor = PCI_VENDOR_ID_LENOVO, \
448 .bios = TPACPI_MATCH_ANY, \
449 .ec = TPID(__id1, __id2), \
450 .quirks = (__quirk) }
452 struct tpacpi_quirk {
453 unsigned int vendor;
454 u16 bios;
455 u16 ec;
456 unsigned long quirks;
460 * tpacpi_check_quirks() - search BIOS/EC version on a list
461 * @qlist: array of &struct tpacpi_quirk
462 * @qlist_size: number of elements in @qlist
464 * Iterates over a quirks list until one is found that matches the
465 * ThinkPad's vendor, BIOS and EC model.
467 * Returns 0 if nothing matches, otherwise returns the quirks field of
468 * the matching &struct tpacpi_quirk entry.
470 * The match criteria is: vendor, ec and bios much match.
472 static unsigned long __init tpacpi_check_quirks(
473 const struct tpacpi_quirk *qlist,
474 unsigned int qlist_size)
476 while (qlist_size) {
477 if ((qlist->vendor == thinkpad_id.vendor ||
478 qlist->vendor == TPACPI_MATCH_ANY) &&
479 (qlist->bios == thinkpad_id.bios_model ||
480 qlist->bios == TPACPI_MATCH_ANY) &&
481 (qlist->ec == thinkpad_id.ec_model ||
482 qlist->ec == TPACPI_MATCH_ANY))
483 return qlist->quirks;
485 qlist_size--;
486 qlist++;
488 return 0;
491 static inline bool __pure __init tpacpi_is_lenovo(void)
493 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
496 static inline bool __pure __init tpacpi_is_ibm(void)
498 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
501 /****************************************************************************
502 ****************************************************************************
504 * ACPI Helpers and device model
506 ****************************************************************************
507 ****************************************************************************/
509 /*************************************************************************
510 * ACPI basic handles
513 static acpi_handle root_handle;
514 static acpi_handle ec_handle;
516 #define TPACPI_HANDLE(object, parent, paths...) \
517 static acpi_handle object##_handle; \
518 static const acpi_handle *object##_parent __initdata = \
519 &parent##_handle; \
520 static char *object##_paths[] __initdata = { paths }
522 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
523 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
525 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
526 /* T4x, X31, X40 */
527 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
528 "\\CMS", /* R40, R40e */
529 ); /* all others */
531 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
532 "^HKEY", /* R30, R31 */
533 "HKEY", /* all others */
534 ); /* 570 */
536 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
537 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
538 "\\_SB.PCI0.VID0", /* 770e */
539 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
540 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
541 "\\_SB.PCI0.AGP.VID", /* all others */
542 ); /* R30, R31 */
545 /*************************************************************************
546 * ACPI helpers
549 static int acpi_evalf(acpi_handle handle,
550 void *res, char *method, char *fmt, ...)
552 char *fmt0 = fmt;
553 struct acpi_object_list params;
554 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
555 struct acpi_buffer result, *resultp;
556 union acpi_object out_obj;
557 acpi_status status;
558 va_list ap;
559 char res_type;
560 int success;
561 int quiet;
563 if (!*fmt) {
564 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
565 return 0;
568 if (*fmt == 'q') {
569 quiet = 1;
570 fmt++;
571 } else
572 quiet = 0;
574 res_type = *(fmt++);
576 params.count = 0;
577 params.pointer = &in_objs[0];
579 va_start(ap, fmt);
580 while (*fmt) {
581 char c = *(fmt++);
582 switch (c) {
583 case 'd': /* int */
584 in_objs[params.count].integer.value = va_arg(ap, int);
585 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
586 break;
587 /* add more types as needed */
588 default:
589 printk(TPACPI_ERR "acpi_evalf() called "
590 "with invalid format character '%c'\n", c);
591 return 0;
594 va_end(ap);
596 if (res_type != 'v') {
597 result.length = sizeof(out_obj);
598 result.pointer = &out_obj;
599 resultp = &result;
600 } else
601 resultp = NULL;
603 status = acpi_evaluate_object(handle, method, &params, resultp);
605 switch (res_type) {
606 case 'd': /* int */
607 success = (status == AE_OK &&
608 out_obj.type == ACPI_TYPE_INTEGER);
609 if (success && res)
610 *(int *)res = out_obj.integer.value;
611 break;
612 case 'v': /* void */
613 success = status == AE_OK;
614 break;
615 /* add more types as needed */
616 default:
617 printk(TPACPI_ERR "acpi_evalf() called "
618 "with invalid format character '%c'\n", res_type);
619 return 0;
622 if (!success && !quiet)
623 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %s\n",
624 method, fmt0, acpi_format_exception(status));
626 return success;
629 static int acpi_ec_read(int i, u8 *p)
631 int v;
633 if (ecrd_handle) {
634 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
635 return 0;
636 *p = v;
637 } else {
638 if (ec_read(i, p) < 0)
639 return 0;
642 return 1;
645 static int acpi_ec_write(int i, u8 v)
647 if (ecwr_handle) {
648 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
649 return 0;
650 } else {
651 if (ec_write(i, v) < 0)
652 return 0;
655 return 1;
658 static int issue_thinkpad_cmos_command(int cmos_cmd)
660 if (!cmos_handle)
661 return -ENXIO;
663 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
664 return -EIO;
666 return 0;
669 /*************************************************************************
670 * ACPI device model
673 #define TPACPI_ACPIHANDLE_INIT(object) \
674 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
675 object##_paths, ARRAY_SIZE(object##_paths))
677 static void __init drv_acpi_handle_init(const char *name,
678 acpi_handle *handle, const acpi_handle parent,
679 char **paths, const int num_paths)
681 int i;
682 acpi_status status;
684 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
685 name);
687 for (i = 0; i < num_paths; i++) {
688 status = acpi_get_handle(parent, paths[i], handle);
689 if (ACPI_SUCCESS(status)) {
690 dbg_printk(TPACPI_DBG_INIT,
691 "Found ACPI handle %s for %s\n",
692 paths[i], name);
693 return;
697 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
698 name);
699 *handle = NULL;
702 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
703 u32 level, void *context, void **return_value)
705 *(acpi_handle *)return_value = handle;
707 return AE_CTRL_TERMINATE;
710 static void __init tpacpi_acpi_handle_locate(const char *name,
711 const char *hid,
712 acpi_handle *handle)
714 acpi_status status;
715 acpi_handle device_found;
717 BUG_ON(!name || !hid || !handle);
718 vdbg_printk(TPACPI_DBG_INIT,
719 "trying to locate ACPI handle for %s, using HID %s\n",
720 name, hid);
722 memset(&device_found, 0, sizeof(device_found));
723 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
724 (void *)name, &device_found);
726 *handle = NULL;
728 if (ACPI_SUCCESS(status)) {
729 *handle = device_found;
730 dbg_printk(TPACPI_DBG_INIT,
731 "Found ACPI handle for %s\n", name);
732 } else {
733 vdbg_printk(TPACPI_DBG_INIT,
734 "Could not locate an ACPI handle for %s: %s\n",
735 name, acpi_format_exception(status));
739 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
741 struct ibm_struct *ibm = data;
743 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
744 return;
746 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
747 return;
749 ibm->acpi->notify(ibm, event);
752 static int __init setup_acpi_notify(struct ibm_struct *ibm)
754 acpi_status status;
755 int rc;
757 BUG_ON(!ibm->acpi);
759 if (!*ibm->acpi->handle)
760 return 0;
762 vdbg_printk(TPACPI_DBG_INIT,
763 "setting up ACPI notify for %s\n", ibm->name);
765 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
766 if (rc < 0) {
767 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
768 ibm->name, rc);
769 return -ENODEV;
772 ibm->acpi->device->driver_data = ibm;
773 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
774 TPACPI_ACPI_EVENT_PREFIX,
775 ibm->name);
777 status = acpi_install_notify_handler(*ibm->acpi->handle,
778 ibm->acpi->type, dispatch_acpi_notify, ibm);
779 if (ACPI_FAILURE(status)) {
780 if (status == AE_ALREADY_EXISTS) {
781 printk(TPACPI_NOTICE
782 "another device driver is already "
783 "handling %s events\n", ibm->name);
784 } else {
785 printk(TPACPI_ERR
786 "acpi_install_notify_handler(%s) failed: %s\n",
787 ibm->name, acpi_format_exception(status));
789 return -ENODEV;
791 ibm->flags.acpi_notify_installed = 1;
792 return 0;
795 static int __init tpacpi_device_add(struct acpi_device *device)
797 return 0;
800 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
802 int rc;
804 dbg_printk(TPACPI_DBG_INIT,
805 "registering %s as an ACPI driver\n", ibm->name);
807 BUG_ON(!ibm->acpi);
809 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
810 if (!ibm->acpi->driver) {
811 printk(TPACPI_ERR
812 "failed to allocate memory for ibm->acpi->driver\n");
813 return -ENOMEM;
816 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
817 ibm->acpi->driver->ids = ibm->acpi->hid;
819 ibm->acpi->driver->ops.add = &tpacpi_device_add;
821 rc = acpi_bus_register_driver(ibm->acpi->driver);
822 if (rc < 0) {
823 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
824 ibm->name, rc);
825 kfree(ibm->acpi->driver);
826 ibm->acpi->driver = NULL;
827 } else if (!rc)
828 ibm->flags.acpi_driver_registered = 1;
830 return rc;
834 /****************************************************************************
835 ****************************************************************************
837 * Procfs Helpers
839 ****************************************************************************
840 ****************************************************************************/
842 static int dispatch_proc_show(struct seq_file *m, void *v)
844 struct ibm_struct *ibm = m->private;
846 if (!ibm || !ibm->read)
847 return -EINVAL;
848 return ibm->read(m);
851 static int dispatch_proc_open(struct inode *inode, struct file *file)
853 return single_open(file, dispatch_proc_show, PDE(inode)->data);
856 static ssize_t dispatch_proc_write(struct file *file,
857 const char __user *userbuf,
858 size_t count, loff_t *pos)
860 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
861 char *kernbuf;
862 int ret;
864 if (!ibm || !ibm->write)
865 return -EINVAL;
866 if (count > PAGE_SIZE - 2)
867 return -EINVAL;
869 kernbuf = kmalloc(count + 2, GFP_KERNEL);
870 if (!kernbuf)
871 return -ENOMEM;
873 if (copy_from_user(kernbuf, userbuf, count)) {
874 kfree(kernbuf);
875 return -EFAULT;
878 kernbuf[count] = 0;
879 strcat(kernbuf, ",");
880 ret = ibm->write(kernbuf);
881 if (ret == 0)
882 ret = count;
884 kfree(kernbuf);
886 return ret;
889 static const struct file_operations dispatch_proc_fops = {
890 .owner = THIS_MODULE,
891 .open = dispatch_proc_open,
892 .read = seq_read,
893 .llseek = seq_lseek,
894 .release = single_release,
895 .write = dispatch_proc_write,
898 static char *next_cmd(char **cmds)
900 char *start = *cmds;
901 char *end;
903 while ((end = strchr(start, ',')) && end == start)
904 start = end + 1;
906 if (!end)
907 return NULL;
909 *end = 0;
910 *cmds = end + 1;
911 return start;
915 /****************************************************************************
916 ****************************************************************************
918 * Device model: input, hwmon and platform
920 ****************************************************************************
921 ****************************************************************************/
923 static struct platform_device *tpacpi_pdev;
924 static struct platform_device *tpacpi_sensors_pdev;
925 static struct device *tpacpi_hwmon;
926 static struct input_dev *tpacpi_inputdev;
927 static struct mutex tpacpi_inputdev_send_mutex;
928 static LIST_HEAD(tpacpi_all_drivers);
930 static int tpacpi_suspend_handler(struct platform_device *pdev,
931 pm_message_t state)
933 struct ibm_struct *ibm, *itmp;
935 list_for_each_entry_safe(ibm, itmp,
936 &tpacpi_all_drivers,
937 all_drivers) {
938 if (ibm->suspend)
939 (ibm->suspend)(state);
942 return 0;
945 static int tpacpi_resume_handler(struct platform_device *pdev)
947 struct ibm_struct *ibm, *itmp;
949 list_for_each_entry_safe(ibm, itmp,
950 &tpacpi_all_drivers,
951 all_drivers) {
952 if (ibm->resume)
953 (ibm->resume)();
956 return 0;
959 static void tpacpi_shutdown_handler(struct platform_device *pdev)
961 struct ibm_struct *ibm, *itmp;
963 list_for_each_entry_safe(ibm, itmp,
964 &tpacpi_all_drivers,
965 all_drivers) {
966 if (ibm->shutdown)
967 (ibm->shutdown)();
971 static struct platform_driver tpacpi_pdriver = {
972 .driver = {
973 .name = TPACPI_DRVR_NAME,
974 .owner = THIS_MODULE,
976 .suspend = tpacpi_suspend_handler,
977 .resume = tpacpi_resume_handler,
978 .shutdown = tpacpi_shutdown_handler,
981 static struct platform_driver tpacpi_hwmon_pdriver = {
982 .driver = {
983 .name = TPACPI_HWMON_DRVR_NAME,
984 .owner = THIS_MODULE,
988 /*************************************************************************
989 * sysfs support helpers
992 struct attribute_set {
993 unsigned int members, max_members;
994 struct attribute_group group;
997 struct attribute_set_obj {
998 struct attribute_set s;
999 struct attribute *a;
1000 } __attribute__((packed));
1002 static struct attribute_set *create_attr_set(unsigned int max_members,
1003 const char *name)
1005 struct attribute_set_obj *sobj;
1007 if (max_members == 0)
1008 return NULL;
1010 /* Allocates space for implicit NULL at the end too */
1011 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1012 max_members * sizeof(struct attribute *),
1013 GFP_KERNEL);
1014 if (!sobj)
1015 return NULL;
1016 sobj->s.max_members = max_members;
1017 sobj->s.group.attrs = &sobj->a;
1018 sobj->s.group.name = name;
1020 return &sobj->s;
1023 #define destroy_attr_set(_set) \
1024 kfree(_set);
1026 /* not multi-threaded safe, use it in a single thread per set */
1027 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1029 if (!s || !attr)
1030 return -EINVAL;
1032 if (s->members >= s->max_members)
1033 return -ENOMEM;
1035 s->group.attrs[s->members] = attr;
1036 s->members++;
1038 return 0;
1041 static int add_many_to_attr_set(struct attribute_set *s,
1042 struct attribute **attr,
1043 unsigned int count)
1045 int i, res;
1047 for (i = 0; i < count; i++) {
1048 res = add_to_attr_set(s, attr[i]);
1049 if (res)
1050 return res;
1053 return 0;
1056 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1058 sysfs_remove_group(kobj, &s->group);
1059 destroy_attr_set(s);
1062 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1063 sysfs_create_group(_kobj, &_attr_set->group)
1065 static int parse_strtoul(const char *buf,
1066 unsigned long max, unsigned long *value)
1068 char *endp;
1070 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1071 endp = skip_spaces(endp);
1072 if (*endp || *value > max)
1073 return -EINVAL;
1075 return 0;
1078 static void tpacpi_disable_brightness_delay(void)
1080 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1081 printk(TPACPI_NOTICE
1082 "ACPI backlight control delay disabled\n");
1085 static void printk_deprecated_attribute(const char * const what,
1086 const char * const details)
1088 tpacpi_log_usertask("deprecated sysfs attribute");
1089 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1090 "will be removed. %s\n",
1091 what, details);
1094 /*************************************************************************
1095 * rfkill and radio control support helpers
1099 * ThinkPad-ACPI firmware handling model:
1101 * WLSW (master wireless switch) is event-driven, and is common to all
1102 * firmware-controlled radios. It cannot be controlled, just monitored,
1103 * as expected. It overrides all radio state in firmware
1105 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1106 * (TODO: verify how WLSW interacts with the returned radio state).
1108 * The only time there are shadow radio state changes, is when
1109 * masked-off hotkeys are used.
1113 * Internal driver API for radio state:
1115 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1116 * bool: true means radio blocked (off)
1118 enum tpacpi_rfkill_state {
1119 TPACPI_RFK_RADIO_OFF = 0,
1120 TPACPI_RFK_RADIO_ON
1123 /* rfkill switches */
1124 enum tpacpi_rfk_id {
1125 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1126 TPACPI_RFK_WWAN_SW_ID,
1127 TPACPI_RFK_UWB_SW_ID,
1128 TPACPI_RFK_SW_MAX
1131 static const char *tpacpi_rfkill_names[] = {
1132 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1133 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1134 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1135 [TPACPI_RFK_SW_MAX] = NULL
1138 /* ThinkPad-ACPI rfkill subdriver */
1139 struct tpacpi_rfk {
1140 struct rfkill *rfkill;
1141 enum tpacpi_rfk_id id;
1142 const struct tpacpi_rfk_ops *ops;
1145 struct tpacpi_rfk_ops {
1146 /* firmware interface */
1147 int (*get_status)(void);
1148 int (*set_status)(const enum tpacpi_rfkill_state);
1151 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1153 /* Query FW and update rfkill sw state for a given rfkill switch */
1154 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1156 int status;
1158 if (!tp_rfk)
1159 return -ENODEV;
1161 status = (tp_rfk->ops->get_status)();
1162 if (status < 0)
1163 return status;
1165 rfkill_set_sw_state(tp_rfk->rfkill,
1166 (status == TPACPI_RFK_RADIO_OFF));
1168 return status;
1171 /* Query FW and update rfkill sw state for all rfkill switches */
1172 static void tpacpi_rfk_update_swstate_all(void)
1174 unsigned int i;
1176 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1177 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1181 * Sync the HW-blocking state of all rfkill switches,
1182 * do notice it causes the rfkill core to schedule uevents
1184 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1186 unsigned int i;
1187 struct tpacpi_rfk *tp_rfk;
1189 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1190 tp_rfk = tpacpi_rfkill_switches[i];
1191 if (tp_rfk) {
1192 if (rfkill_set_hw_state(tp_rfk->rfkill,
1193 blocked)) {
1194 /* ignore -- we track sw block */
1200 /* Call to get the WLSW state from the firmware */
1201 static int hotkey_get_wlsw(void);
1203 /* Call to query WLSW state and update all rfkill switches */
1204 static bool tpacpi_rfk_check_hwblock_state(void)
1206 int res = hotkey_get_wlsw();
1207 int hw_blocked;
1209 /* When unknown or unsupported, we have to assume it is unblocked */
1210 if (res < 0)
1211 return false;
1213 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1214 tpacpi_rfk_update_hwblock_state(hw_blocked);
1216 return hw_blocked;
1219 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1221 struct tpacpi_rfk *tp_rfk = data;
1222 int res;
1224 dbg_printk(TPACPI_DBG_RFKILL,
1225 "request to change radio state to %s\n",
1226 blocked ? "blocked" : "unblocked");
1228 /* try to set radio state */
1229 res = (tp_rfk->ops->set_status)(blocked ?
1230 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1232 /* and update the rfkill core with whatever the FW really did */
1233 tpacpi_rfk_update_swstate(tp_rfk);
1235 return (res < 0) ? res : 0;
1238 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1239 .set_block = tpacpi_rfk_hook_set_block,
1242 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1243 const struct tpacpi_rfk_ops *tp_rfkops,
1244 const enum rfkill_type rfktype,
1245 const char *name,
1246 const bool set_default)
1248 struct tpacpi_rfk *atp_rfk;
1249 int res;
1250 bool sw_state = false;
1251 bool hw_state;
1252 int sw_status;
1254 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1256 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1257 if (atp_rfk)
1258 atp_rfk->rfkill = rfkill_alloc(name,
1259 &tpacpi_pdev->dev,
1260 rfktype,
1261 &tpacpi_rfk_rfkill_ops,
1262 atp_rfk);
1263 if (!atp_rfk || !atp_rfk->rfkill) {
1264 printk(TPACPI_ERR
1265 "failed to allocate memory for rfkill class\n");
1266 kfree(atp_rfk);
1267 return -ENOMEM;
1270 atp_rfk->id = id;
1271 atp_rfk->ops = tp_rfkops;
1273 sw_status = (tp_rfkops->get_status)();
1274 if (sw_status < 0) {
1275 printk(TPACPI_ERR
1276 "failed to read initial state for %s, error %d\n",
1277 name, sw_status);
1278 } else {
1279 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1280 if (set_default) {
1281 /* try to keep the initial state, since we ask the
1282 * firmware to preserve it across S5 in NVRAM */
1283 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1286 hw_state = tpacpi_rfk_check_hwblock_state();
1287 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1289 res = rfkill_register(atp_rfk->rfkill);
1290 if (res < 0) {
1291 printk(TPACPI_ERR
1292 "failed to register %s rfkill switch: %d\n",
1293 name, res);
1294 rfkill_destroy(atp_rfk->rfkill);
1295 kfree(atp_rfk);
1296 return res;
1299 tpacpi_rfkill_switches[id] = atp_rfk;
1301 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1302 name, (sw_state || hw_state) ? "" : "un");
1303 return 0;
1306 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1308 struct tpacpi_rfk *tp_rfk;
1310 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1312 tp_rfk = tpacpi_rfkill_switches[id];
1313 if (tp_rfk) {
1314 rfkill_unregister(tp_rfk->rfkill);
1315 rfkill_destroy(tp_rfk->rfkill);
1316 tpacpi_rfkill_switches[id] = NULL;
1317 kfree(tp_rfk);
1321 static void printk_deprecated_rfkill_attribute(const char * const what)
1323 printk_deprecated_attribute(what,
1324 "Please switch to generic rfkill before year 2010");
1327 /* sysfs <radio> enable ------------------------------------------------ */
1328 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1329 struct device_attribute *attr,
1330 char *buf)
1332 int status;
1334 printk_deprecated_rfkill_attribute(attr->attr.name);
1336 /* This is in the ABI... */
1337 if (tpacpi_rfk_check_hwblock_state()) {
1338 status = TPACPI_RFK_RADIO_OFF;
1339 } else {
1340 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1341 if (status < 0)
1342 return status;
1345 return snprintf(buf, PAGE_SIZE, "%d\n",
1346 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1349 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1350 struct device_attribute *attr,
1351 const char *buf, size_t count)
1353 unsigned long t;
1354 int res;
1356 printk_deprecated_rfkill_attribute(attr->attr.name);
1358 if (parse_strtoul(buf, 1, &t))
1359 return -EINVAL;
1361 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1363 /* This is in the ABI... */
1364 if (tpacpi_rfk_check_hwblock_state() && !!t)
1365 return -EPERM;
1367 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1368 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1369 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1371 return (res < 0) ? res : count;
1374 /* procfs -------------------------------------------------------------- */
1375 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1377 if (id >= TPACPI_RFK_SW_MAX)
1378 seq_printf(m, "status:\t\tnot supported\n");
1379 else {
1380 int status;
1382 /* This is in the ABI... */
1383 if (tpacpi_rfk_check_hwblock_state()) {
1384 status = TPACPI_RFK_RADIO_OFF;
1385 } else {
1386 status = tpacpi_rfk_update_swstate(
1387 tpacpi_rfkill_switches[id]);
1388 if (status < 0)
1389 return status;
1392 seq_printf(m, "status:\t\t%s\n",
1393 (status == TPACPI_RFK_RADIO_ON) ?
1394 "enabled" : "disabled");
1395 seq_printf(m, "commands:\tenable, disable\n");
1398 return 0;
1401 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1403 char *cmd;
1404 int status = -1;
1405 int res = 0;
1407 if (id >= TPACPI_RFK_SW_MAX)
1408 return -ENODEV;
1410 while ((cmd = next_cmd(&buf))) {
1411 if (strlencmp(cmd, "enable") == 0)
1412 status = TPACPI_RFK_RADIO_ON;
1413 else if (strlencmp(cmd, "disable") == 0)
1414 status = TPACPI_RFK_RADIO_OFF;
1415 else
1416 return -EINVAL;
1419 if (status != -1) {
1420 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1421 (status == TPACPI_RFK_RADIO_ON) ?
1422 "enable" : "disable",
1423 tpacpi_rfkill_names[id]);
1424 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1425 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1428 return res;
1431 /*************************************************************************
1432 * thinkpad-acpi driver attributes
1435 /* interface_version --------------------------------------------------- */
1436 static ssize_t tpacpi_driver_interface_version_show(
1437 struct device_driver *drv,
1438 char *buf)
1440 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1443 static DRIVER_ATTR(interface_version, S_IRUGO,
1444 tpacpi_driver_interface_version_show, NULL);
1446 /* debug_level --------------------------------------------------------- */
1447 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1448 char *buf)
1450 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1453 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1454 const char *buf, size_t count)
1456 unsigned long t;
1458 if (parse_strtoul(buf, 0xffff, &t))
1459 return -EINVAL;
1461 dbg_level = t;
1463 return count;
1466 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1467 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1469 /* version ------------------------------------------------------------- */
1470 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1471 char *buf)
1473 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1474 TPACPI_DESC, TPACPI_VERSION);
1477 static DRIVER_ATTR(version, S_IRUGO,
1478 tpacpi_driver_version_show, NULL);
1480 /* --------------------------------------------------------------------- */
1482 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1484 /* wlsw_emulstate ------------------------------------------------------ */
1485 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1486 char *buf)
1488 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1491 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1492 const char *buf, size_t count)
1494 unsigned long t;
1496 if (parse_strtoul(buf, 1, &t))
1497 return -EINVAL;
1499 if (tpacpi_wlsw_emulstate != !!t) {
1500 tpacpi_wlsw_emulstate = !!t;
1501 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1504 return count;
1507 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1508 tpacpi_driver_wlsw_emulstate_show,
1509 tpacpi_driver_wlsw_emulstate_store);
1511 /* bluetooth_emulstate ------------------------------------------------- */
1512 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1513 struct device_driver *drv,
1514 char *buf)
1516 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1519 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1520 struct device_driver *drv,
1521 const char *buf, size_t count)
1523 unsigned long t;
1525 if (parse_strtoul(buf, 1, &t))
1526 return -EINVAL;
1528 tpacpi_bluetooth_emulstate = !!t;
1530 return count;
1533 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1534 tpacpi_driver_bluetooth_emulstate_show,
1535 tpacpi_driver_bluetooth_emulstate_store);
1537 /* wwan_emulstate ------------------------------------------------- */
1538 static ssize_t tpacpi_driver_wwan_emulstate_show(
1539 struct device_driver *drv,
1540 char *buf)
1542 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1545 static ssize_t tpacpi_driver_wwan_emulstate_store(
1546 struct device_driver *drv,
1547 const char *buf, size_t count)
1549 unsigned long t;
1551 if (parse_strtoul(buf, 1, &t))
1552 return -EINVAL;
1554 tpacpi_wwan_emulstate = !!t;
1556 return count;
1559 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1560 tpacpi_driver_wwan_emulstate_show,
1561 tpacpi_driver_wwan_emulstate_store);
1563 /* uwb_emulstate ------------------------------------------------- */
1564 static ssize_t tpacpi_driver_uwb_emulstate_show(
1565 struct device_driver *drv,
1566 char *buf)
1568 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1571 static ssize_t tpacpi_driver_uwb_emulstate_store(
1572 struct device_driver *drv,
1573 const char *buf, size_t count)
1575 unsigned long t;
1577 if (parse_strtoul(buf, 1, &t))
1578 return -EINVAL;
1580 tpacpi_uwb_emulstate = !!t;
1582 return count;
1585 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1586 tpacpi_driver_uwb_emulstate_show,
1587 tpacpi_driver_uwb_emulstate_store);
1588 #endif
1590 /* --------------------------------------------------------------------- */
1592 static struct driver_attribute *tpacpi_driver_attributes[] = {
1593 &driver_attr_debug_level, &driver_attr_version,
1594 &driver_attr_interface_version,
1597 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1599 int i, res;
1601 i = 0;
1602 res = 0;
1603 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1604 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1605 i++;
1608 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1609 if (!res && dbg_wlswemul)
1610 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1611 if (!res && dbg_bluetoothemul)
1612 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1613 if (!res && dbg_wwanemul)
1614 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1615 if (!res && dbg_uwbemul)
1616 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1617 #endif
1619 return res;
1622 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1624 int i;
1626 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1627 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1629 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1630 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1631 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1632 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1633 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1634 #endif
1637 /*************************************************************************
1638 * Firmware Data
1642 * Table of recommended minimum BIOS versions
1644 * Reasons for listing:
1645 * 1. Stable BIOS, listed because the unknown amount of
1646 * bugs and bad ACPI behaviour on older versions
1648 * 2. BIOS or EC fw with known bugs that trigger on Linux
1650 * 3. BIOS with known reduced functionality in older versions
1652 * We recommend the latest BIOS and EC version.
1653 * We only support the latest BIOS and EC fw version as a rule.
1655 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1656 * Information from users in ThinkWiki
1658 * WARNING: we use this table also to detect that the machine is
1659 * a ThinkPad in some cases, so don't remove entries lightly.
1662 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1663 { .vendor = (__v), \
1664 .bios = TPID(__id1, __id2), \
1665 .ec = TPACPI_MATCH_ANY, \
1666 .quirks = TPACPI_MATCH_ANY << 16 \
1667 | (__bv1) << 8 | (__bv2) }
1669 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1670 __eid, __ev1, __ev2) \
1671 { .vendor = (__v), \
1672 .bios = TPID(__bid1, __bid2), \
1673 .ec = __eid, \
1674 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1675 | (__bv1) << 8 | (__bv2) }
1677 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1678 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1680 /* Outdated IBM BIOSes often lack the EC id string */
1681 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1682 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1683 __bv1, __bv2, TPID(__id1, __id2), \
1684 __ev1, __ev2), \
1685 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1686 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1687 __ev1, __ev2)
1689 /* Outdated IBM BIOSes often lack the EC id string */
1690 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1691 __eid1, __eid2, __ev1, __ev2) \
1692 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1693 __bv1, __bv2, TPID(__eid1, __eid2), \
1694 __ev1, __ev2), \
1695 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1696 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1697 __ev1, __ev2)
1699 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1700 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1702 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1703 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1704 __bv1, __bv2, TPID(__id1, __id2), \
1705 __ev1, __ev2)
1707 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1708 __eid1, __eid2, __ev1, __ev2) \
1709 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1710 __bv1, __bv2, TPID(__eid1, __eid2), \
1711 __ev1, __ev2)
1713 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1714 /* Numeric models ------------------ */
1715 /* FW MODEL BIOS VERS */
1716 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1717 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1718 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1719 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1720 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1721 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1722 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1723 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1724 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1726 /* A-series ------------------------- */
1727 /* FW MODEL BIOS VERS EC VERS */
1728 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1729 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1730 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1731 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1732 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1733 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1734 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1735 TPV_QI0('1', '3', '2', '0'), /* A22m */
1736 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1737 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1738 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1740 /* G-series ------------------------- */
1741 /* FW MODEL BIOS VERS */
1742 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1743 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1745 /* R-series, T-series --------------- */
1746 /* FW MODEL BIOS VERS EC VERS */
1747 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1748 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1749 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1750 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1751 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1752 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1753 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1754 T40/p, T41/p, T42/p (1) */
1755 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1756 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1757 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1758 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1760 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1761 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1762 TPV_QI0('1', '6', '3', '2'), /* T22 */
1763 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1764 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1765 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1767 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1768 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1769 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1771 /* BIOS FW BIOS VERS EC FW EC VERS */
1772 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1773 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1775 /* X-series ------------------------- */
1776 /* FW MODEL BIOS VERS EC VERS */
1777 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1778 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1779 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1780 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1781 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1782 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1783 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1785 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1786 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1788 /* (0) - older versions lack DMI EC fw string and functionality */
1789 /* (1) - older versions known to lack functionality */
1792 #undef TPV_QL1
1793 #undef TPV_QL0
1794 #undef TPV_QI2
1795 #undef TPV_QI1
1796 #undef TPV_QI0
1797 #undef TPV_Q_X
1798 #undef TPV_Q
1800 static void __init tpacpi_check_outdated_fw(void)
1802 unsigned long fwvers;
1803 u16 ec_version, bios_version;
1805 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1806 ARRAY_SIZE(tpacpi_bios_version_qtable));
1808 if (!fwvers)
1809 return;
1811 bios_version = fwvers & 0xffffU;
1812 ec_version = (fwvers >> 16) & 0xffffU;
1814 /* note that unknown versions are set to 0x0000 and we use that */
1815 if ((bios_version > thinkpad_id.bios_release) ||
1816 (ec_version > thinkpad_id.ec_release &&
1817 ec_version != TPACPI_MATCH_ANY)) {
1819 * The changelogs would let us track down the exact
1820 * reason, but it is just too much of a pain to track
1821 * it. We only list BIOSes that are either really
1822 * broken, or really stable to begin with, so it is
1823 * best if the user upgrades the firmware anyway.
1825 printk(TPACPI_WARN
1826 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1827 printk(TPACPI_WARN
1828 "WARNING: This firmware may be missing critical bug "
1829 "fixes and/or important features\n");
1833 static bool __init tpacpi_is_fw_known(void)
1835 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1836 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1839 /****************************************************************************
1840 ****************************************************************************
1842 * Subdrivers
1844 ****************************************************************************
1845 ****************************************************************************/
1847 /*************************************************************************
1848 * thinkpad-acpi metadata subdriver
1851 static int thinkpad_acpi_driver_read(struct seq_file *m)
1853 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1854 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1855 return 0;
1858 static struct ibm_struct thinkpad_acpi_driver_data = {
1859 .name = "driver",
1860 .read = thinkpad_acpi_driver_read,
1863 /*************************************************************************
1864 * Hotkey subdriver
1868 * ThinkPad firmware event model
1870 * The ThinkPad firmware has two main event interfaces: normal ACPI
1871 * notifications (which follow the ACPI standard), and a private event
1872 * interface.
1874 * The private event interface also issues events for the hotkeys. As
1875 * the driver gained features, the event handling code ended up being
1876 * built around the hotkey subdriver. This will need to be refactored
1877 * to a more formal event API eventually.
1879 * Some "hotkeys" are actually supposed to be used as event reports,
1880 * such as "brightness has changed", "volume has changed", depending on
1881 * the ThinkPad model and how the firmware is operating.
1883 * Unlike other classes, hotkey-class events have mask/unmask control on
1884 * non-ancient firmware. However, how it behaves changes a lot with the
1885 * firmware model and version.
1888 enum { /* hot key scan codes (derived from ACPI DSDT) */
1889 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1890 TP_ACPI_HOTKEYSCAN_FNF2,
1891 TP_ACPI_HOTKEYSCAN_FNF3,
1892 TP_ACPI_HOTKEYSCAN_FNF4,
1893 TP_ACPI_HOTKEYSCAN_FNF5,
1894 TP_ACPI_HOTKEYSCAN_FNF6,
1895 TP_ACPI_HOTKEYSCAN_FNF7,
1896 TP_ACPI_HOTKEYSCAN_FNF8,
1897 TP_ACPI_HOTKEYSCAN_FNF9,
1898 TP_ACPI_HOTKEYSCAN_FNF10,
1899 TP_ACPI_HOTKEYSCAN_FNF11,
1900 TP_ACPI_HOTKEYSCAN_FNF12,
1901 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1902 TP_ACPI_HOTKEYSCAN_FNINSERT,
1903 TP_ACPI_HOTKEYSCAN_FNDELETE,
1904 TP_ACPI_HOTKEYSCAN_FNHOME,
1905 TP_ACPI_HOTKEYSCAN_FNEND,
1906 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1907 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1908 TP_ACPI_HOTKEYSCAN_FNSPACE,
1909 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1910 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1911 TP_ACPI_HOTKEYSCAN_MUTE,
1912 TP_ACPI_HOTKEYSCAN_THINKPAD,
1915 enum { /* Keys/events available through NVRAM polling */
1916 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1917 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1920 enum { /* Positions of some of the keys in hotkey masks */
1921 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1922 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1923 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1924 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1925 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1926 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1927 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1928 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1929 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1930 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1931 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1934 enum { /* NVRAM to ACPI HKEY group map */
1935 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1936 TP_ACPI_HKEY_ZOOM_MASK |
1937 TP_ACPI_HKEY_DISPSWTCH_MASK |
1938 TP_ACPI_HKEY_HIBERNATE_MASK,
1939 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1940 TP_ACPI_HKEY_BRGHTDWN_MASK,
1941 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1942 TP_ACPI_HKEY_VOLDWN_MASK |
1943 TP_ACPI_HKEY_MUTE_MASK,
1946 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1947 struct tp_nvram_state {
1948 u16 thinkpad_toggle:1;
1949 u16 zoom_toggle:1;
1950 u16 display_toggle:1;
1951 u16 thinklight_toggle:1;
1952 u16 hibernate_toggle:1;
1953 u16 displayexp_toggle:1;
1954 u16 display_state:1;
1955 u16 brightness_toggle:1;
1956 u16 volume_toggle:1;
1957 u16 mute:1;
1959 u8 brightness_level;
1960 u8 volume_level;
1963 /* kthread for the hotkey poller */
1964 static struct task_struct *tpacpi_hotkey_task;
1966 /* Acquired while the poller kthread is running, use to sync start/stop */
1967 static struct mutex hotkey_thread_mutex;
1970 * Acquire mutex to write poller control variables as an
1971 * atomic block.
1973 * Increment hotkey_config_change when changing them if you
1974 * want the kthread to forget old state.
1976 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1978 static struct mutex hotkey_thread_data_mutex;
1979 static unsigned int hotkey_config_change;
1982 * hotkey poller control variables
1984 * Must be atomic or readers will also need to acquire mutex
1986 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1987 * should be used only when the changes need to be taken as
1988 * a block, OR when one needs to force the kthread to forget
1989 * old state.
1991 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1992 static unsigned int hotkey_poll_freq = 10; /* Hz */
1994 #define HOTKEY_CONFIG_CRITICAL_START \
1995 do { \
1996 mutex_lock(&hotkey_thread_data_mutex); \
1997 hotkey_config_change++; \
1998 } while (0);
1999 #define HOTKEY_CONFIG_CRITICAL_END \
2000 mutex_unlock(&hotkey_thread_data_mutex);
2002 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2004 #define hotkey_source_mask 0U
2005 #define HOTKEY_CONFIG_CRITICAL_START
2006 #define HOTKEY_CONFIG_CRITICAL_END
2008 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2010 static struct mutex hotkey_mutex;
2012 static enum { /* Reasons for waking up */
2013 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2014 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2015 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2016 } hotkey_wakeup_reason;
2018 static int hotkey_autosleep_ack;
2020 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2021 static u32 hotkey_all_mask; /* all events supported in fw */
2022 static u32 hotkey_reserved_mask; /* events better left disabled */
2023 static u32 hotkey_driver_mask; /* events needed by the driver */
2024 static u32 hotkey_user_mask; /* events visible to userspace */
2025 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2027 static unsigned int hotkey_report_mode;
2029 static u16 *hotkey_keycode_map;
2031 static struct attribute_set *hotkey_dev_attributes;
2033 static void tpacpi_driver_event(const unsigned int hkey_event);
2034 static void hotkey_driver_event(const unsigned int scancode);
2035 static void hotkey_poll_setup(const bool may_warn);
2037 /* HKEY.MHKG() return bits */
2038 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2040 static int hotkey_get_wlsw(void)
2042 int status;
2044 if (!tp_features.hotkey_wlsw)
2045 return -ENODEV;
2047 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2048 if (dbg_wlswemul)
2049 return (tpacpi_wlsw_emulstate) ?
2050 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2051 #endif
2053 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2054 return -EIO;
2056 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2059 static int hotkey_get_tablet_mode(int *status)
2061 int s;
2063 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2064 return -EIO;
2066 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2067 return 0;
2071 * Reads current event mask from firmware, and updates
2072 * hotkey_acpi_mask accordingly. Also resets any bits
2073 * from hotkey_user_mask that are unavailable to be
2074 * delivered (shadow requirement of the userspace ABI).
2076 * Call with hotkey_mutex held
2078 static int hotkey_mask_get(void)
2080 if (tp_features.hotkey_mask) {
2081 u32 m = 0;
2083 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2084 return -EIO;
2086 hotkey_acpi_mask = m;
2087 } else {
2088 /* no mask support doesn't mean no event support... */
2089 hotkey_acpi_mask = hotkey_all_mask;
2092 /* sync userspace-visible mask */
2093 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2095 return 0;
2098 void static hotkey_mask_warn_incomplete_mask(void)
2100 /* log only what the user can fix... */
2101 const u32 wantedmask = hotkey_driver_mask &
2102 ~(hotkey_acpi_mask | hotkey_source_mask) &
2103 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2105 if (wantedmask)
2106 printk(TPACPI_NOTICE
2107 "required events 0x%08x not enabled!\n",
2108 wantedmask);
2112 * Set the firmware mask when supported
2114 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2116 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2118 * Call with hotkey_mutex held
2120 static int hotkey_mask_set(u32 mask)
2122 int i;
2123 int rc = 0;
2125 const u32 fwmask = mask & ~hotkey_source_mask;
2127 if (tp_features.hotkey_mask) {
2128 for (i = 0; i < 32; i++) {
2129 if (!acpi_evalf(hkey_handle,
2130 NULL, "MHKM", "vdd", i + 1,
2131 !!(mask & (1 << i)))) {
2132 rc = -EIO;
2133 break;
2139 * We *must* make an inconditional call to hotkey_mask_get to
2140 * refresh hotkey_acpi_mask and update hotkey_user_mask
2142 * Take the opportunity to also log when we cannot _enable_
2143 * a given event.
2145 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2146 printk(TPACPI_NOTICE
2147 "asked for hotkey mask 0x%08x, but "
2148 "firmware forced it to 0x%08x\n",
2149 fwmask, hotkey_acpi_mask);
2152 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2153 hotkey_mask_warn_incomplete_mask();
2155 return rc;
2159 * Sets hotkey_user_mask and tries to set the firmware mask
2161 * Call with hotkey_mutex held
2163 static int hotkey_user_mask_set(const u32 mask)
2165 int rc;
2167 /* Give people a chance to notice they are doing something that
2168 * is bound to go boom on their users sooner or later */
2169 if (!tp_warned.hotkey_mask_ff &&
2170 (mask == 0xffff || mask == 0xffffff ||
2171 mask == 0xffffffff)) {
2172 tp_warned.hotkey_mask_ff = 1;
2173 printk(TPACPI_NOTICE
2174 "setting the hotkey mask to 0x%08x is likely "
2175 "not the best way to go about it\n", mask);
2176 printk(TPACPI_NOTICE
2177 "please consider using the driver defaults, "
2178 "and refer to up-to-date thinkpad-acpi "
2179 "documentation\n");
2182 /* Try to enable what the user asked for, plus whatever we need.
2183 * this syncs everything but won't enable bits in hotkey_user_mask */
2184 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2186 /* Enable the available bits in hotkey_user_mask */
2187 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2189 return rc;
2193 * Sets the driver hotkey mask.
2195 * Can be called even if the hotkey subdriver is inactive
2197 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2199 int rc;
2201 /* Do the right thing if hotkey_init has not been called yet */
2202 if (!tp_features.hotkey) {
2203 hotkey_driver_mask = mask;
2204 return 0;
2207 mutex_lock(&hotkey_mutex);
2209 HOTKEY_CONFIG_CRITICAL_START
2210 hotkey_driver_mask = mask;
2211 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2212 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2213 #endif
2214 HOTKEY_CONFIG_CRITICAL_END
2216 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2217 ~hotkey_source_mask);
2218 hotkey_poll_setup(true);
2220 mutex_unlock(&hotkey_mutex);
2222 return rc;
2225 static int hotkey_status_get(int *status)
2227 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2228 return -EIO;
2230 return 0;
2233 static int hotkey_status_set(bool enable)
2235 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2236 return -EIO;
2238 return 0;
2241 static void tpacpi_input_send_tabletsw(void)
2243 int state;
2245 if (tp_features.hotkey_tablet &&
2246 !hotkey_get_tablet_mode(&state)) {
2247 mutex_lock(&tpacpi_inputdev_send_mutex);
2249 input_report_switch(tpacpi_inputdev,
2250 SW_TABLET_MODE, !!state);
2251 input_sync(tpacpi_inputdev);
2253 mutex_unlock(&tpacpi_inputdev_send_mutex);
2257 /* Do NOT call without validating scancode first */
2258 static void tpacpi_input_send_key(const unsigned int scancode)
2260 const unsigned int keycode = hotkey_keycode_map[scancode];
2262 if (keycode != KEY_RESERVED) {
2263 mutex_lock(&tpacpi_inputdev_send_mutex);
2265 input_report_key(tpacpi_inputdev, keycode, 1);
2266 if (keycode == KEY_UNKNOWN)
2267 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2268 scancode);
2269 input_sync(tpacpi_inputdev);
2271 input_report_key(tpacpi_inputdev, keycode, 0);
2272 if (keycode == KEY_UNKNOWN)
2273 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2274 scancode);
2275 input_sync(tpacpi_inputdev);
2277 mutex_unlock(&tpacpi_inputdev_send_mutex);
2281 /* Do NOT call without validating scancode first */
2282 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2284 hotkey_driver_event(scancode);
2285 if (hotkey_user_mask & (1 << scancode))
2286 tpacpi_input_send_key(scancode);
2289 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2290 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2292 /* Do NOT call without validating scancode first */
2293 static void tpacpi_hotkey_send_key(unsigned int scancode)
2295 tpacpi_input_send_key_masked(scancode);
2296 if (hotkey_report_mode < 2) {
2297 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2298 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2302 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2304 u8 d;
2306 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2307 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2308 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2309 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2310 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2311 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2313 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2314 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2315 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2317 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2318 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2319 n->displayexp_toggle =
2320 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2322 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2323 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2324 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2325 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2326 n->brightness_toggle =
2327 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2329 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2330 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2331 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2332 >> TP_NVRAM_POS_LEVEL_VOLUME;
2333 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2334 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2338 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2339 struct tp_nvram_state *newn,
2340 const u32 event_mask)
2343 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2344 do { \
2345 if ((event_mask & (1 << __scancode)) && \
2346 oldn->__member != newn->__member) \
2347 tpacpi_hotkey_send_key(__scancode); \
2348 } while (0)
2350 #define TPACPI_MAY_SEND_KEY(__scancode) \
2351 do { \
2352 if (event_mask & (1 << __scancode)) \
2353 tpacpi_hotkey_send_key(__scancode); \
2354 } while (0)
2356 void issue_volchange(const unsigned int oldvol,
2357 const unsigned int newvol)
2359 unsigned int i = oldvol;
2361 while (i > newvol) {
2362 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2363 i--;
2365 while (i < newvol) {
2366 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2367 i++;
2371 void issue_brightnesschange(const unsigned int oldbrt,
2372 const unsigned int newbrt)
2374 unsigned int i = oldbrt;
2376 while (i > newbrt) {
2377 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2378 i--;
2380 while (i < newbrt) {
2381 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2382 i++;
2386 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2387 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2388 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2389 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2391 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2393 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2396 * Handle volume
2398 * This code is supposed to duplicate the IBM firmware behaviour:
2399 * - Pressing MUTE issues mute hotkey message, even when already mute
2400 * - Pressing Volume up/down issues volume up/down hotkey messages,
2401 * even when already at maximum or minumum volume
2402 * - The act of unmuting issues volume up/down notification,
2403 * depending which key was used to unmute
2405 * We are constrained to what the NVRAM can tell us, which is not much
2406 * and certainly not enough if more than one volume hotkey was pressed
2407 * since the last poll cycle.
2409 * Just to make our life interesting, some newer Lenovo ThinkPads have
2410 * bugs in the BIOS and may fail to update volume_toggle properly.
2412 if (newn->mute) {
2413 /* muted */
2414 if (!oldn->mute ||
2415 oldn->volume_toggle != newn->volume_toggle ||
2416 oldn->volume_level != newn->volume_level) {
2417 /* recently muted, or repeated mute keypress, or
2418 * multiple presses ending in mute */
2419 issue_volchange(oldn->volume_level, newn->volume_level);
2420 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2422 } else {
2423 /* unmute */
2424 if (oldn->mute) {
2425 /* recently unmuted, issue 'unmute' keypress */
2426 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2428 if (oldn->volume_level != newn->volume_level) {
2429 issue_volchange(oldn->volume_level, newn->volume_level);
2430 } else if (oldn->volume_toggle != newn->volume_toggle) {
2431 /* repeated vol up/down keypress at end of scale ? */
2432 if (newn->volume_level == 0)
2433 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2434 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2435 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2439 /* handle brightness */
2440 if (oldn->brightness_level != newn->brightness_level) {
2441 issue_brightnesschange(oldn->brightness_level,
2442 newn->brightness_level);
2443 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2444 /* repeated key presses that didn't change state */
2445 if (newn->brightness_level == 0)
2446 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2447 else if (newn->brightness_level >= bright_maxlvl
2448 && !tp_features.bright_unkfw)
2449 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2452 #undef TPACPI_COMPARE_KEY
2453 #undef TPACPI_MAY_SEND_KEY
2457 * Polling driver
2459 * We track all events in hotkey_source_mask all the time, since
2460 * most of them are edge-based. We only issue those requested by
2461 * hotkey_user_mask or hotkey_driver_mask, though.
2463 static int hotkey_kthread(void *data)
2465 struct tp_nvram_state s[2];
2466 u32 poll_mask, event_mask;
2467 unsigned int si, so;
2468 unsigned long t;
2469 unsigned int change_detector, must_reset;
2470 unsigned int poll_freq;
2472 mutex_lock(&hotkey_thread_mutex);
2474 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2475 goto exit;
2477 set_freezable();
2479 so = 0;
2480 si = 1;
2481 t = 0;
2483 /* Initial state for compares */
2484 mutex_lock(&hotkey_thread_data_mutex);
2485 change_detector = hotkey_config_change;
2486 poll_mask = hotkey_source_mask;
2487 event_mask = hotkey_source_mask &
2488 (hotkey_driver_mask | hotkey_user_mask);
2489 poll_freq = hotkey_poll_freq;
2490 mutex_unlock(&hotkey_thread_data_mutex);
2491 hotkey_read_nvram(&s[so], poll_mask);
2493 while (!kthread_should_stop()) {
2494 if (t == 0) {
2495 if (likely(poll_freq))
2496 t = 1000/poll_freq;
2497 else
2498 t = 100; /* should never happen... */
2500 t = msleep_interruptible(t);
2501 if (unlikely(kthread_should_stop()))
2502 break;
2503 must_reset = try_to_freeze();
2504 if (t > 0 && !must_reset)
2505 continue;
2507 mutex_lock(&hotkey_thread_data_mutex);
2508 if (must_reset || hotkey_config_change != change_detector) {
2509 /* forget old state on thaw or config change */
2510 si = so;
2511 t = 0;
2512 change_detector = hotkey_config_change;
2514 poll_mask = hotkey_source_mask;
2515 event_mask = hotkey_source_mask &
2516 (hotkey_driver_mask | hotkey_user_mask);
2517 poll_freq = hotkey_poll_freq;
2518 mutex_unlock(&hotkey_thread_data_mutex);
2520 if (likely(poll_mask)) {
2521 hotkey_read_nvram(&s[si], poll_mask);
2522 if (likely(si != so)) {
2523 hotkey_compare_and_issue_event(&s[so], &s[si],
2524 event_mask);
2528 so = si;
2529 si ^= 1;
2532 exit:
2533 mutex_unlock(&hotkey_thread_mutex);
2534 return 0;
2537 /* call with hotkey_mutex held */
2538 static void hotkey_poll_stop_sync(void)
2540 if (tpacpi_hotkey_task) {
2541 if (frozen(tpacpi_hotkey_task) ||
2542 freezing(tpacpi_hotkey_task))
2543 thaw_process(tpacpi_hotkey_task);
2545 kthread_stop(tpacpi_hotkey_task);
2546 tpacpi_hotkey_task = NULL;
2547 mutex_lock(&hotkey_thread_mutex);
2548 /* at this point, the thread did exit */
2549 mutex_unlock(&hotkey_thread_mutex);
2553 /* call with hotkey_mutex held */
2554 static void hotkey_poll_setup(const bool may_warn)
2556 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2557 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2559 if (hotkey_poll_freq > 0 &&
2560 (poll_driver_mask ||
2561 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2562 if (!tpacpi_hotkey_task) {
2563 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2564 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2565 if (IS_ERR(tpacpi_hotkey_task)) {
2566 tpacpi_hotkey_task = NULL;
2567 printk(TPACPI_ERR
2568 "could not create kernel thread "
2569 "for hotkey polling\n");
2572 } else {
2573 hotkey_poll_stop_sync();
2574 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2575 hotkey_poll_freq == 0) {
2576 printk(TPACPI_NOTICE
2577 "hot keys 0x%08x and/or events 0x%08x "
2578 "require polling, which is currently "
2579 "disabled\n",
2580 poll_user_mask, poll_driver_mask);
2585 static void hotkey_poll_setup_safe(const bool may_warn)
2587 mutex_lock(&hotkey_mutex);
2588 hotkey_poll_setup(may_warn);
2589 mutex_unlock(&hotkey_mutex);
2592 /* call with hotkey_mutex held */
2593 static void hotkey_poll_set_freq(unsigned int freq)
2595 if (!freq)
2596 hotkey_poll_stop_sync();
2598 hotkey_poll_freq = freq;
2601 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2603 static void hotkey_poll_setup(const bool __unused)
2607 static void hotkey_poll_setup_safe(const bool __unused)
2611 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2613 static int hotkey_inputdev_open(struct input_dev *dev)
2615 switch (tpacpi_lifecycle) {
2616 case TPACPI_LIFE_INIT:
2617 case TPACPI_LIFE_RUNNING:
2618 hotkey_poll_setup_safe(false);
2619 return 0;
2620 case TPACPI_LIFE_EXITING:
2621 return -EBUSY;
2624 /* Should only happen if tpacpi_lifecycle is corrupt */
2625 BUG();
2626 return -EBUSY;
2629 static void hotkey_inputdev_close(struct input_dev *dev)
2631 /* disable hotkey polling when possible */
2632 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2633 !(hotkey_source_mask & hotkey_driver_mask))
2634 hotkey_poll_setup_safe(false);
2637 /* sysfs hotkey enable ------------------------------------------------- */
2638 static ssize_t hotkey_enable_show(struct device *dev,
2639 struct device_attribute *attr,
2640 char *buf)
2642 int res, status;
2644 printk_deprecated_attribute("hotkey_enable",
2645 "Hotkey reporting is always enabled");
2647 res = hotkey_status_get(&status);
2648 if (res)
2649 return res;
2651 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2654 static ssize_t hotkey_enable_store(struct device *dev,
2655 struct device_attribute *attr,
2656 const char *buf, size_t count)
2658 unsigned long t;
2660 printk_deprecated_attribute("hotkey_enable",
2661 "Hotkeys can be disabled through hotkey_mask");
2663 if (parse_strtoul(buf, 1, &t))
2664 return -EINVAL;
2666 if (t == 0)
2667 return -EPERM;
2669 return count;
2672 static struct device_attribute dev_attr_hotkey_enable =
2673 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2674 hotkey_enable_show, hotkey_enable_store);
2676 /* sysfs hotkey mask --------------------------------------------------- */
2677 static ssize_t hotkey_mask_show(struct device *dev,
2678 struct device_attribute *attr,
2679 char *buf)
2681 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2684 static ssize_t hotkey_mask_store(struct device *dev,
2685 struct device_attribute *attr,
2686 const char *buf, size_t count)
2688 unsigned long t;
2689 int res;
2691 if (parse_strtoul(buf, 0xffffffffUL, &t))
2692 return -EINVAL;
2694 if (mutex_lock_killable(&hotkey_mutex))
2695 return -ERESTARTSYS;
2697 res = hotkey_user_mask_set(t);
2699 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2700 hotkey_poll_setup(true);
2701 #endif
2703 mutex_unlock(&hotkey_mutex);
2705 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2707 return (res) ? res : count;
2710 static struct device_attribute dev_attr_hotkey_mask =
2711 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2712 hotkey_mask_show, hotkey_mask_store);
2714 /* sysfs hotkey bios_enabled ------------------------------------------- */
2715 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2716 struct device_attribute *attr,
2717 char *buf)
2719 return sprintf(buf, "0\n");
2722 static struct device_attribute dev_attr_hotkey_bios_enabled =
2723 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2725 /* sysfs hotkey bios_mask ---------------------------------------------- */
2726 static ssize_t hotkey_bios_mask_show(struct device *dev,
2727 struct device_attribute *attr,
2728 char *buf)
2730 printk_deprecated_attribute("hotkey_bios_mask",
2731 "This attribute is useless.");
2732 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2735 static struct device_attribute dev_attr_hotkey_bios_mask =
2736 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2738 /* sysfs hotkey all_mask ----------------------------------------------- */
2739 static ssize_t hotkey_all_mask_show(struct device *dev,
2740 struct device_attribute *attr,
2741 char *buf)
2743 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2744 hotkey_all_mask | hotkey_source_mask);
2747 static struct device_attribute dev_attr_hotkey_all_mask =
2748 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2750 /* sysfs hotkey recommended_mask --------------------------------------- */
2751 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2752 struct device_attribute *attr,
2753 char *buf)
2755 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2756 (hotkey_all_mask | hotkey_source_mask)
2757 & ~hotkey_reserved_mask);
2760 static struct device_attribute dev_attr_hotkey_recommended_mask =
2761 __ATTR(hotkey_recommended_mask, S_IRUGO,
2762 hotkey_recommended_mask_show, NULL);
2764 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2766 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2767 static ssize_t hotkey_source_mask_show(struct device *dev,
2768 struct device_attribute *attr,
2769 char *buf)
2771 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2774 static ssize_t hotkey_source_mask_store(struct device *dev,
2775 struct device_attribute *attr,
2776 const char *buf, size_t count)
2778 unsigned long t;
2779 u32 r_ev;
2780 int rc;
2782 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2783 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2784 return -EINVAL;
2786 if (mutex_lock_killable(&hotkey_mutex))
2787 return -ERESTARTSYS;
2789 HOTKEY_CONFIG_CRITICAL_START
2790 hotkey_source_mask = t;
2791 HOTKEY_CONFIG_CRITICAL_END
2793 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2794 ~hotkey_source_mask);
2795 hotkey_poll_setup(true);
2797 /* check if events needed by the driver got disabled */
2798 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2799 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2801 mutex_unlock(&hotkey_mutex);
2803 if (rc < 0)
2804 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2805 "firmware event mask!\n");
2807 if (r_ev)
2808 printk(TPACPI_NOTICE "hotkey_source_mask: "
2809 "some important events were disabled: "
2810 "0x%04x\n", r_ev);
2812 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2814 return (rc < 0) ? rc : count;
2817 static struct device_attribute dev_attr_hotkey_source_mask =
2818 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2819 hotkey_source_mask_show, hotkey_source_mask_store);
2821 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2822 static ssize_t hotkey_poll_freq_show(struct device *dev,
2823 struct device_attribute *attr,
2824 char *buf)
2826 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2829 static ssize_t hotkey_poll_freq_store(struct device *dev,
2830 struct device_attribute *attr,
2831 const char *buf, size_t count)
2833 unsigned long t;
2835 if (parse_strtoul(buf, 25, &t))
2836 return -EINVAL;
2838 if (mutex_lock_killable(&hotkey_mutex))
2839 return -ERESTARTSYS;
2841 hotkey_poll_set_freq(t);
2842 hotkey_poll_setup(true);
2844 mutex_unlock(&hotkey_mutex);
2846 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2848 return count;
2851 static struct device_attribute dev_attr_hotkey_poll_freq =
2852 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2853 hotkey_poll_freq_show, hotkey_poll_freq_store);
2855 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2857 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2858 static ssize_t hotkey_radio_sw_show(struct device *dev,
2859 struct device_attribute *attr,
2860 char *buf)
2862 int res;
2863 res = hotkey_get_wlsw();
2864 if (res < 0)
2865 return res;
2867 /* Opportunistic update */
2868 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2870 return snprintf(buf, PAGE_SIZE, "%d\n",
2871 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2874 static struct device_attribute dev_attr_hotkey_radio_sw =
2875 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2877 static void hotkey_radio_sw_notify_change(void)
2879 if (tp_features.hotkey_wlsw)
2880 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2881 "hotkey_radio_sw");
2884 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2885 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2886 struct device_attribute *attr,
2887 char *buf)
2889 int res, s;
2890 res = hotkey_get_tablet_mode(&s);
2891 if (res < 0)
2892 return res;
2894 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2897 static struct device_attribute dev_attr_hotkey_tablet_mode =
2898 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2900 static void hotkey_tablet_mode_notify_change(void)
2902 if (tp_features.hotkey_tablet)
2903 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2904 "hotkey_tablet_mode");
2907 /* sysfs hotkey report_mode -------------------------------------------- */
2908 static ssize_t hotkey_report_mode_show(struct device *dev,
2909 struct device_attribute *attr,
2910 char *buf)
2912 return snprintf(buf, PAGE_SIZE, "%d\n",
2913 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2916 static struct device_attribute dev_attr_hotkey_report_mode =
2917 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2919 /* sysfs wakeup reason (pollable) -------------------------------------- */
2920 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2921 struct device_attribute *attr,
2922 char *buf)
2924 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2927 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2928 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2930 static void hotkey_wakeup_reason_notify_change(void)
2932 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2933 "wakeup_reason");
2936 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2937 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2938 struct device_attribute *attr,
2939 char *buf)
2941 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2944 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2945 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2946 hotkey_wakeup_hotunplug_complete_show, NULL);
2948 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2950 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2951 "wakeup_hotunplug_complete");
2954 /* --------------------------------------------------------------------- */
2956 static struct attribute *hotkey_attributes[] __initdata = {
2957 &dev_attr_hotkey_enable.attr,
2958 &dev_attr_hotkey_bios_enabled.attr,
2959 &dev_attr_hotkey_bios_mask.attr,
2960 &dev_attr_hotkey_report_mode.attr,
2961 &dev_attr_hotkey_wakeup_reason.attr,
2962 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2963 &dev_attr_hotkey_mask.attr,
2964 &dev_attr_hotkey_all_mask.attr,
2965 &dev_attr_hotkey_recommended_mask.attr,
2966 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2967 &dev_attr_hotkey_source_mask.attr,
2968 &dev_attr_hotkey_poll_freq.attr,
2969 #endif
2973 * Sync both the hw and sw blocking state of all switches
2975 static void tpacpi_send_radiosw_update(void)
2977 int wlsw;
2980 * We must sync all rfkill controllers *before* issuing any
2981 * rfkill input events, or we will race the rfkill core input
2982 * handler.
2984 * tpacpi_inputdev_send_mutex works as a syncronization point
2985 * for the above.
2987 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2990 wlsw = hotkey_get_wlsw();
2992 /* Sync hw blocking state first if it is hw-blocked */
2993 if (wlsw == TPACPI_RFK_RADIO_OFF)
2994 tpacpi_rfk_update_hwblock_state(true);
2996 /* Sync sw blocking state */
2997 tpacpi_rfk_update_swstate_all();
2999 /* Sync hw blocking state last if it is hw-unblocked */
3000 if (wlsw == TPACPI_RFK_RADIO_ON)
3001 tpacpi_rfk_update_hwblock_state(false);
3003 /* Issue rfkill input event for WLSW switch */
3004 if (!(wlsw < 0)) {
3005 mutex_lock(&tpacpi_inputdev_send_mutex);
3007 input_report_switch(tpacpi_inputdev,
3008 SW_RFKILL_ALL, (wlsw > 0));
3009 input_sync(tpacpi_inputdev);
3011 mutex_unlock(&tpacpi_inputdev_send_mutex);
3015 * this can be unconditional, as we will poll state again
3016 * if userspace uses the notify to read data
3018 hotkey_radio_sw_notify_change();
3021 static void hotkey_exit(void)
3023 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3024 mutex_lock(&hotkey_mutex);
3025 hotkey_poll_stop_sync();
3026 mutex_unlock(&hotkey_mutex);
3027 #endif
3029 if (hotkey_dev_attributes)
3030 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3032 kfree(hotkey_keycode_map);
3034 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3035 "restoring original HKEY status and mask\n");
3036 /* yes, there is a bitwise or below, we want the
3037 * functions to be called even if one of them fail */
3038 if (((tp_features.hotkey_mask &&
3039 hotkey_mask_set(hotkey_orig_mask)) |
3040 hotkey_status_set(false)) != 0)
3041 printk(TPACPI_ERR
3042 "failed to restore hot key mask "
3043 "to BIOS defaults\n");
3046 static void __init hotkey_unmap(const unsigned int scancode)
3048 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3049 clear_bit(hotkey_keycode_map[scancode],
3050 tpacpi_inputdev->keybit);
3051 hotkey_keycode_map[scancode] = KEY_RESERVED;
3056 * HKEY quirks:
3057 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3060 #define TPACPI_HK_Q_INIMASK 0x0001
3062 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3063 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3064 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3065 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3066 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3067 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3068 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3069 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3070 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3071 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3072 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3073 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3074 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3075 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3076 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3077 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3078 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3079 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3080 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3081 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3084 static int __init hotkey_init(struct ibm_init_struct *iibm)
3086 /* Requirements for changing the default keymaps:
3088 * 1. Many of the keys are mapped to KEY_RESERVED for very
3089 * good reasons. Do not change them unless you have deep
3090 * knowledge on the IBM and Lenovo ThinkPad firmware for
3091 * the various ThinkPad models. The driver behaves
3092 * differently for KEY_RESERVED: such keys have their
3093 * hot key mask *unset* in mask_recommended, and also
3094 * in the initial hot key mask programmed into the
3095 * firmware at driver load time, which means the firm-
3096 * ware may react very differently if you change them to
3097 * something else;
3099 * 2. You must be subscribed to the linux-thinkpad and
3100 * ibm-acpi-devel mailing lists, and you should read the
3101 * list archives since 2007 if you want to change the
3102 * keymaps. This requirement exists so that you will
3103 * know the past history of problems with the thinkpad-
3104 * acpi driver keymaps, and also that you will be
3105 * listening to any bug reports;
3107 * 3. Do not send thinkpad-acpi specific patches directly to
3108 * for merging, *ever*. Send them to the linux-acpi
3109 * mailinglist for comments. Merging is to be done only
3110 * through acpi-test and the ACPI maintainer.
3112 * If the above is too much to ask, don't change the keymap.
3113 * Ask the thinkpad-acpi maintainer to do it, instead.
3115 static u16 ibm_keycode_map[] __initdata = {
3116 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3117 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3118 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3119 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3121 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3122 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3123 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3124 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3126 /* brightness: firmware always reacts to them */
3127 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3128 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3130 /* Thinklight: firmware always react to it */
3131 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3133 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3134 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3136 /* Volume: firmware always react to it and reprograms
3137 * the built-in *extra* mixer. Never map it to control
3138 * another mixer by default. */
3139 KEY_RESERVED, /* 0x14: VOLUME UP */
3140 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3141 KEY_RESERVED, /* 0x16: MUTE */
3143 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3145 /* (assignments unknown, please report if found) */
3146 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3147 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3149 static u16 lenovo_keycode_map[] __initdata = {
3150 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3151 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3152 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3153 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3155 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3156 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3157 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3158 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3160 /* These should be enabled --only-- when ACPI video
3161 * is disabled (i.e. in "vendor" mode), and are handled
3162 * in a special way by the init code */
3163 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3164 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3166 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3168 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3169 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3171 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3172 * react to it and reprograms the built-in *extra* mixer.
3173 * Never map it to control another mixer by default.
3175 * T60?, T61, R60?, R61: firmware and EC tries to send
3176 * these over the regular keyboard, so these are no-ops,
3177 * but there are still weird bugs re. MUTE, so do not
3178 * change unless you get test reports from all Lenovo
3179 * models. May cause the BIOS to interfere with the
3180 * HDA mixer.
3182 KEY_RESERVED, /* 0x14: VOLUME UP */
3183 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3184 KEY_RESERVED, /* 0x16: MUTE */
3186 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3188 /* (assignments unknown, please report if found) */
3189 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3190 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3193 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3194 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3195 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3197 int res, i;
3198 int status;
3199 int hkeyv;
3200 bool radiosw_state = false;
3201 bool tabletsw_state = false;
3203 unsigned long quirks;
3205 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3206 "initializing hotkey subdriver\n");
3208 BUG_ON(!tpacpi_inputdev);
3209 BUG_ON(tpacpi_inputdev->open != NULL ||
3210 tpacpi_inputdev->close != NULL);
3212 TPACPI_ACPIHANDLE_INIT(hkey);
3213 mutex_init(&hotkey_mutex);
3215 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3216 mutex_init(&hotkey_thread_mutex);
3217 mutex_init(&hotkey_thread_data_mutex);
3218 #endif
3220 /* hotkey not supported on 570 */
3221 tp_features.hotkey = hkey_handle != NULL;
3223 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3224 "hotkeys are %s\n",
3225 str_supported(tp_features.hotkey));
3227 if (!tp_features.hotkey)
3228 return 1;
3230 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3231 ARRAY_SIZE(tpacpi_hotkey_qtable));
3233 tpacpi_disable_brightness_delay();
3235 /* MUST have enough space for all attributes to be added to
3236 * hotkey_dev_attributes */
3237 hotkey_dev_attributes = create_attr_set(
3238 ARRAY_SIZE(hotkey_attributes) + 2,
3239 NULL);
3240 if (!hotkey_dev_attributes)
3241 return -ENOMEM;
3242 res = add_many_to_attr_set(hotkey_dev_attributes,
3243 hotkey_attributes,
3244 ARRAY_SIZE(hotkey_attributes));
3245 if (res)
3246 goto err_exit;
3248 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3249 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3250 for HKEY interface version 0x100 */
3251 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3252 if ((hkeyv >> 8) != 1) {
3253 printk(TPACPI_ERR "unknown version of the "
3254 "HKEY interface: 0x%x\n", hkeyv);
3255 printk(TPACPI_ERR "please report this to %s\n",
3256 TPACPI_MAIL);
3257 } else {
3259 * MHKV 0x100 in A31, R40, R40e,
3260 * T4x, X31, and later
3262 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3263 "firmware HKEY interface version: 0x%x\n",
3264 hkeyv);
3266 /* Paranoia check AND init hotkey_all_mask */
3267 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3268 "MHKA", "qd")) {
3269 printk(TPACPI_ERR
3270 "missing MHKA handler, "
3271 "please report this to %s\n",
3272 TPACPI_MAIL);
3273 /* Fallback: pre-init for FN+F3,F4,F12 */
3274 hotkey_all_mask = 0x080cU;
3275 } else {
3276 tp_features.hotkey_mask = 1;
3281 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3282 "hotkey masks are %s\n",
3283 str_supported(tp_features.hotkey_mask));
3285 /* Init hotkey_all_mask if not initialized yet */
3286 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3287 (quirks & TPACPI_HK_Q_INIMASK))
3288 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3290 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3291 if (tp_features.hotkey_mask) {
3292 /* hotkey_source_mask *must* be zero for
3293 * the first hotkey_mask_get to return hotkey_orig_mask */
3294 res = hotkey_mask_get();
3295 if (res)
3296 goto err_exit;
3298 hotkey_orig_mask = hotkey_acpi_mask;
3299 } else {
3300 hotkey_orig_mask = hotkey_all_mask;
3301 hotkey_acpi_mask = hotkey_all_mask;
3304 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3305 if (dbg_wlswemul) {
3306 tp_features.hotkey_wlsw = 1;
3307 radiosw_state = !!tpacpi_wlsw_emulstate;
3308 printk(TPACPI_INFO
3309 "radio switch emulation enabled\n");
3310 } else
3311 #endif
3312 /* Not all thinkpads have a hardware radio switch */
3313 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3314 tp_features.hotkey_wlsw = 1;
3315 radiosw_state = !!status;
3316 printk(TPACPI_INFO
3317 "radio switch found; radios are %s\n",
3318 enabled(status, 0));
3320 if (tp_features.hotkey_wlsw)
3321 res = add_to_attr_set(hotkey_dev_attributes,
3322 &dev_attr_hotkey_radio_sw.attr);
3324 /* For X41t, X60t, X61t Tablets... */
3325 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3326 tp_features.hotkey_tablet = 1;
3327 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3328 printk(TPACPI_INFO
3329 "possible tablet mode switch found; "
3330 "ThinkPad in %s mode\n",
3331 (tabletsw_state) ? "tablet" : "laptop");
3332 res = add_to_attr_set(hotkey_dev_attributes,
3333 &dev_attr_hotkey_tablet_mode.attr);
3336 if (!res)
3337 res = register_attr_set_with_sysfs(
3338 hotkey_dev_attributes,
3339 &tpacpi_pdev->dev.kobj);
3340 if (res)
3341 goto err_exit;
3343 /* Set up key map */
3345 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3346 GFP_KERNEL);
3347 if (!hotkey_keycode_map) {
3348 printk(TPACPI_ERR
3349 "failed to allocate memory for key map\n");
3350 res = -ENOMEM;
3351 goto err_exit;
3354 if (tpacpi_is_lenovo()) {
3355 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3356 "using Lenovo default hot key map\n");
3357 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3358 TPACPI_HOTKEY_MAP_SIZE);
3359 } else {
3360 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3361 "using IBM default hot key map\n");
3362 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3363 TPACPI_HOTKEY_MAP_SIZE);
3366 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3367 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3368 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3369 tpacpi_inputdev->keycode = hotkey_keycode_map;
3370 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3371 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3372 input_set_capability(tpacpi_inputdev, EV_KEY,
3373 hotkey_keycode_map[i]);
3374 } else {
3375 if (i < sizeof(hotkey_reserved_mask)*8)
3376 hotkey_reserved_mask |= 1 << i;
3380 if (tp_features.hotkey_wlsw) {
3381 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3382 input_report_switch(tpacpi_inputdev,
3383 SW_RFKILL_ALL, radiosw_state);
3385 if (tp_features.hotkey_tablet) {
3386 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3387 input_report_switch(tpacpi_inputdev,
3388 SW_TABLET_MODE, tabletsw_state);
3391 /* Do not issue duplicate brightness change events to
3392 * userspace. tpacpi_detect_brightness_capabilities() must have
3393 * been called before this point */
3394 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3395 printk(TPACPI_INFO
3396 "This ThinkPad has standard ACPI backlight "
3397 "brightness control, supported by the ACPI "
3398 "video driver\n");
3399 printk(TPACPI_NOTICE
3400 "Disabling thinkpad-acpi brightness events "
3401 "by default...\n");
3403 /* Disable brightness up/down on Lenovo thinkpads when
3404 * ACPI is handling them, otherwise it is plain impossible
3405 * for userspace to do something even remotely sane */
3406 hotkey_reserved_mask |=
3407 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3408 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3409 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3410 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3413 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3414 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3415 & ~hotkey_all_mask
3416 & ~hotkey_reserved_mask;
3418 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3419 "hotkey source mask 0x%08x, polling freq %u\n",
3420 hotkey_source_mask, hotkey_poll_freq);
3421 #endif
3423 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3424 "enabling firmware HKEY event interface...\n");
3425 res = hotkey_status_set(true);
3426 if (res) {
3427 hotkey_exit();
3428 return res;
3430 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3431 | hotkey_driver_mask)
3432 & ~hotkey_source_mask);
3433 if (res < 0 && res != -ENXIO) {
3434 hotkey_exit();
3435 return res;
3437 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3438 & ~hotkey_reserved_mask;
3439 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3440 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3441 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3443 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3444 "legacy ibm/hotkey event reporting over procfs %s\n",
3445 (hotkey_report_mode < 2) ?
3446 "enabled" : "disabled");
3448 tpacpi_inputdev->open = &hotkey_inputdev_open;
3449 tpacpi_inputdev->close = &hotkey_inputdev_close;
3451 hotkey_poll_setup_safe(true);
3453 return 0;
3455 err_exit:
3456 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3457 hotkey_dev_attributes = NULL;
3459 return (res < 0)? res : 1;
3462 static bool hotkey_notify_hotkey(const u32 hkey,
3463 bool *send_acpi_ev,
3464 bool *ignore_acpi_ev)
3466 /* 0x1000-0x1FFF: key presses */
3467 unsigned int scancode = hkey & 0xfff;
3468 *send_acpi_ev = true;
3469 *ignore_acpi_ev = false;
3471 if (scancode > 0 && scancode < 0x21) {
3472 scancode--;
3473 if (!(hotkey_source_mask & (1 << scancode))) {
3474 tpacpi_input_send_key_masked(scancode);
3475 *send_acpi_ev = false;
3476 } else {
3477 *ignore_acpi_ev = true;
3479 return true;
3481 return false;
3484 static bool hotkey_notify_wakeup(const u32 hkey,
3485 bool *send_acpi_ev,
3486 bool *ignore_acpi_ev)
3488 /* 0x2000-0x2FFF: Wakeup reason */
3489 *send_acpi_ev = true;
3490 *ignore_acpi_ev = false;
3492 switch (hkey) {
3493 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3494 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3495 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3496 *ignore_acpi_ev = true;
3497 break;
3499 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3500 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3501 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3502 *ignore_acpi_ev = true;
3503 break;
3505 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3506 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3507 printk(TPACPI_ALERT
3508 "EMERGENCY WAKEUP: battery almost empty\n");
3509 /* how to auto-heal: */
3510 /* 2313: woke up from S3, go to S4/S5 */
3511 /* 2413: woke up from S4, go to S5 */
3512 break;
3514 default:
3515 return false;
3518 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3519 printk(TPACPI_INFO
3520 "woke up due to a hot-unplug "
3521 "request...\n");
3522 hotkey_wakeup_reason_notify_change();
3524 return true;
3527 static bool hotkey_notify_usrevent(const u32 hkey,
3528 bool *send_acpi_ev,
3529 bool *ignore_acpi_ev)
3531 /* 0x5000-0x5FFF: human interface helpers */
3532 *send_acpi_ev = true;
3533 *ignore_acpi_ev = false;
3535 switch (hkey) {
3536 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3537 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3538 return true;
3540 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3541 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3542 tpacpi_input_send_tabletsw();
3543 hotkey_tablet_mode_notify_change();
3544 *send_acpi_ev = false;
3545 return true;
3547 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3548 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3549 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3550 /* do not propagate these events */
3551 *ignore_acpi_ev = true;
3552 return true;
3554 default:
3555 return false;
3559 static void thermal_dump_all_sensors(void);
3561 static bool hotkey_notify_thermal(const u32 hkey,
3562 bool *send_acpi_ev,
3563 bool *ignore_acpi_ev)
3565 bool known = true;
3567 /* 0x6000-0x6FFF: thermal alarms */
3568 *send_acpi_ev = true;
3569 *ignore_acpi_ev = false;
3571 switch (hkey) {
3572 case TP_HKEY_EV_THM_TABLE_CHANGED:
3573 printk(TPACPI_INFO
3574 "EC reports that Thermal Table has changed\n");
3575 /* recommended action: do nothing, we don't have
3576 * Lenovo ATM information */
3577 return true;
3578 case TP_HKEY_EV_ALARM_BAT_HOT:
3579 printk(TPACPI_CRIT
3580 "THERMAL ALARM: battery is too hot!\n");
3581 /* recommended action: warn user through gui */
3582 break;
3583 case TP_HKEY_EV_ALARM_BAT_XHOT:
3584 printk(TPACPI_ALERT
3585 "THERMAL EMERGENCY: battery is extremely hot!\n");
3586 /* recommended action: immediate sleep/hibernate */
3587 break;
3588 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3589 printk(TPACPI_CRIT
3590 "THERMAL ALARM: "
3591 "a sensor reports something is too hot!\n");
3592 /* recommended action: warn user through gui, that */
3593 /* some internal component is too hot */
3594 break;
3595 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3596 printk(TPACPI_ALERT
3597 "THERMAL EMERGENCY: "
3598 "a sensor reports something is extremely hot!\n");
3599 /* recommended action: immediate sleep/hibernate */
3600 break;
3601 default:
3602 printk(TPACPI_ALERT
3603 "THERMAL ALERT: unknown thermal alarm received\n");
3604 known = false;
3607 thermal_dump_all_sensors();
3609 return known;
3612 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3614 u32 hkey;
3615 bool send_acpi_ev;
3616 bool ignore_acpi_ev;
3617 bool known_ev;
3619 if (event != 0x80) {
3620 printk(TPACPI_ERR
3621 "unknown HKEY notification event %d\n", event);
3622 /* forward it to userspace, maybe it knows how to handle it */
3623 acpi_bus_generate_netlink_event(
3624 ibm->acpi->device->pnp.device_class,
3625 dev_name(&ibm->acpi->device->dev),
3626 event, 0);
3627 return;
3630 while (1) {
3631 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3632 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3633 return;
3636 if (hkey == 0) {
3637 /* queue empty */
3638 return;
3641 send_acpi_ev = true;
3642 ignore_acpi_ev = false;
3644 switch (hkey >> 12) {
3645 case 1:
3646 /* 0x1000-0x1FFF: key presses */
3647 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3648 &ignore_acpi_ev);
3649 break;
3650 case 2:
3651 /* 0x2000-0x2FFF: Wakeup reason */
3652 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3653 &ignore_acpi_ev);
3654 break;
3655 case 3:
3656 /* 0x3000-0x3FFF: bay-related wakeups */
3657 switch (hkey) {
3658 case TP_HKEY_EV_BAYEJ_ACK:
3659 hotkey_autosleep_ack = 1;
3660 printk(TPACPI_INFO
3661 "bay ejected\n");
3662 hotkey_wakeup_hotunplug_complete_notify_change();
3663 known_ev = true;
3664 break;
3665 case TP_HKEY_EV_OPTDRV_EJ:
3666 /* FIXME: kick libata if SATA link offline */
3667 known_ev = true;
3668 break;
3669 default:
3670 known_ev = false;
3672 break;
3673 case 4:
3674 /* 0x4000-0x4FFF: dock-related wakeups */
3675 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3676 hotkey_autosleep_ack = 1;
3677 printk(TPACPI_INFO
3678 "undocked\n");
3679 hotkey_wakeup_hotunplug_complete_notify_change();
3680 known_ev = true;
3681 } else {
3682 known_ev = false;
3684 break;
3685 case 5:
3686 /* 0x5000-0x5FFF: human interface helpers */
3687 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3688 &ignore_acpi_ev);
3689 break;
3690 case 6:
3691 /* 0x6000-0x6FFF: thermal alarms */
3692 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3693 &ignore_acpi_ev);
3694 break;
3695 case 7:
3696 /* 0x7000-0x7FFF: misc */
3697 if (tp_features.hotkey_wlsw &&
3698 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3699 tpacpi_send_radiosw_update();
3700 send_acpi_ev = 0;
3701 known_ev = true;
3702 break;
3704 /* fallthrough to default */
3705 default:
3706 known_ev = false;
3708 if (!known_ev) {
3709 printk(TPACPI_NOTICE
3710 "unhandled HKEY event 0x%04x\n", hkey);
3711 printk(TPACPI_NOTICE
3712 "please report the conditions when this "
3713 "event happened to %s\n", TPACPI_MAIL);
3716 /* Legacy events */
3717 if (!ignore_acpi_ev &&
3718 (send_acpi_ev || hotkey_report_mode < 2)) {
3719 acpi_bus_generate_proc_event(ibm->acpi->device,
3720 event, hkey);
3723 /* netlink events */
3724 if (!ignore_acpi_ev && send_acpi_ev) {
3725 acpi_bus_generate_netlink_event(
3726 ibm->acpi->device->pnp.device_class,
3727 dev_name(&ibm->acpi->device->dev),
3728 event, hkey);
3733 static void hotkey_suspend(pm_message_t state)
3735 /* Do these on suspend, we get the events on early resume! */
3736 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3737 hotkey_autosleep_ack = 0;
3740 static void hotkey_resume(void)
3742 tpacpi_disable_brightness_delay();
3744 if (hotkey_status_set(true) < 0 ||
3745 hotkey_mask_set(hotkey_acpi_mask) < 0)
3746 printk(TPACPI_ERR
3747 "error while attempting to reset the event "
3748 "firmware interface\n");
3750 tpacpi_send_radiosw_update();
3751 hotkey_tablet_mode_notify_change();
3752 hotkey_wakeup_reason_notify_change();
3753 hotkey_wakeup_hotunplug_complete_notify_change();
3754 hotkey_poll_setup_safe(false);
3757 /* procfs -------------------------------------------------------------- */
3758 static int hotkey_read(struct seq_file *m)
3760 int res, status;
3762 if (!tp_features.hotkey) {
3763 seq_printf(m, "status:\t\tnot supported\n");
3764 return 0;
3767 if (mutex_lock_killable(&hotkey_mutex))
3768 return -ERESTARTSYS;
3769 res = hotkey_status_get(&status);
3770 if (!res)
3771 res = hotkey_mask_get();
3772 mutex_unlock(&hotkey_mutex);
3773 if (res)
3774 return res;
3776 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3777 if (hotkey_all_mask) {
3778 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3779 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3780 } else {
3781 seq_printf(m, "mask:\t\tnot supported\n");
3782 seq_printf(m, "commands:\tenable, disable, reset\n");
3785 return 0;
3788 static void hotkey_enabledisable_warn(bool enable)
3790 tpacpi_log_usertask("procfs hotkey enable/disable");
3791 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3792 TPACPI_WARN
3793 "hotkey enable/disable functionality has been "
3794 "removed from the driver. Hotkeys are always "
3795 "enabled\n"))
3796 printk(TPACPI_ERR
3797 "Please remove the hotkey=enable module "
3798 "parameter, it is deprecated. Hotkeys are always "
3799 "enabled\n");
3802 static int hotkey_write(char *buf)
3804 int res;
3805 u32 mask;
3806 char *cmd;
3808 if (!tp_features.hotkey)
3809 return -ENODEV;
3811 if (mutex_lock_killable(&hotkey_mutex))
3812 return -ERESTARTSYS;
3814 mask = hotkey_user_mask;
3816 res = 0;
3817 while ((cmd = next_cmd(&buf))) {
3818 if (strlencmp(cmd, "enable") == 0) {
3819 hotkey_enabledisable_warn(1);
3820 } else if (strlencmp(cmd, "disable") == 0) {
3821 hotkey_enabledisable_warn(0);
3822 res = -EPERM;
3823 } else if (strlencmp(cmd, "reset") == 0) {
3824 mask = (hotkey_all_mask | hotkey_source_mask)
3825 & ~hotkey_reserved_mask;
3826 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3827 /* mask set */
3828 } else if (sscanf(cmd, "%x", &mask) == 1) {
3829 /* mask set */
3830 } else {
3831 res = -EINVAL;
3832 goto errexit;
3836 if (!res) {
3837 tpacpi_disclose_usertask("procfs hotkey",
3838 "set mask to 0x%08x\n", mask);
3839 res = hotkey_user_mask_set(mask);
3842 errexit:
3843 mutex_unlock(&hotkey_mutex);
3844 return res;
3847 static const struct acpi_device_id ibm_htk_device_ids[] = {
3848 {TPACPI_ACPI_HKEY_HID, 0},
3849 {"", 0},
3852 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3853 .hid = ibm_htk_device_ids,
3854 .notify = hotkey_notify,
3855 .handle = &hkey_handle,
3856 .type = ACPI_DEVICE_NOTIFY,
3859 static struct ibm_struct hotkey_driver_data = {
3860 .name = "hotkey",
3861 .read = hotkey_read,
3862 .write = hotkey_write,
3863 .exit = hotkey_exit,
3864 .resume = hotkey_resume,
3865 .suspend = hotkey_suspend,
3866 .acpi = &ibm_hotkey_acpidriver,
3869 /*************************************************************************
3870 * Bluetooth subdriver
3873 enum {
3874 /* ACPI GBDC/SBDC bits */
3875 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3876 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3877 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3878 0 = disable, 1 = enable */
3881 enum {
3882 /* ACPI \BLTH commands */
3883 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3884 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3885 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3886 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3887 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3890 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3892 static int bluetooth_get_status(void)
3894 int status;
3896 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3897 if (dbg_bluetoothemul)
3898 return (tpacpi_bluetooth_emulstate) ?
3899 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3900 #endif
3902 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3903 return -EIO;
3905 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3906 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3909 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3911 int status;
3913 vdbg_printk(TPACPI_DBG_RFKILL,
3914 "will attempt to %s bluetooth\n",
3915 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3917 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3918 if (dbg_bluetoothemul) {
3919 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3920 return 0;
3922 #endif
3924 if (state == TPACPI_RFK_RADIO_ON)
3925 status = TP_ACPI_BLUETOOTH_RADIOSSW
3926 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3927 else
3928 status = 0;
3930 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3931 return -EIO;
3933 return 0;
3936 /* sysfs bluetooth enable ---------------------------------------------- */
3937 static ssize_t bluetooth_enable_show(struct device *dev,
3938 struct device_attribute *attr,
3939 char *buf)
3941 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3942 attr, buf);
3945 static ssize_t bluetooth_enable_store(struct device *dev,
3946 struct device_attribute *attr,
3947 const char *buf, size_t count)
3949 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3950 attr, buf, count);
3953 static struct device_attribute dev_attr_bluetooth_enable =
3954 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3955 bluetooth_enable_show, bluetooth_enable_store);
3957 /* --------------------------------------------------------------------- */
3959 static struct attribute *bluetooth_attributes[] = {
3960 &dev_attr_bluetooth_enable.attr,
3961 NULL
3964 static const struct attribute_group bluetooth_attr_group = {
3965 .attrs = bluetooth_attributes,
3968 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3969 .get_status = bluetooth_get_status,
3970 .set_status = bluetooth_set_status,
3973 static void bluetooth_shutdown(void)
3975 /* Order firmware to save current state to NVRAM */
3976 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3977 TP_ACPI_BLTH_SAVE_STATE))
3978 printk(TPACPI_NOTICE
3979 "failed to save bluetooth state to NVRAM\n");
3980 else
3981 vdbg_printk(TPACPI_DBG_RFKILL,
3982 "bluestooth state saved to NVRAM\n");
3985 static void bluetooth_exit(void)
3987 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3988 &bluetooth_attr_group);
3990 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3992 bluetooth_shutdown();
3995 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3997 int res;
3998 int status = 0;
4000 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4001 "initializing bluetooth subdriver\n");
4003 TPACPI_ACPIHANDLE_INIT(hkey);
4005 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4006 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4007 tp_features.bluetooth = hkey_handle &&
4008 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4010 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4011 "bluetooth is %s, status 0x%02x\n",
4012 str_supported(tp_features.bluetooth),
4013 status);
4015 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4016 if (dbg_bluetoothemul) {
4017 tp_features.bluetooth = 1;
4018 printk(TPACPI_INFO
4019 "bluetooth switch emulation enabled\n");
4020 } else
4021 #endif
4022 if (tp_features.bluetooth &&
4023 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4024 /* no bluetooth hardware present in system */
4025 tp_features.bluetooth = 0;
4026 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4027 "bluetooth hardware not installed\n");
4030 if (!tp_features.bluetooth)
4031 return 1;
4033 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4034 &bluetooth_tprfk_ops,
4035 RFKILL_TYPE_BLUETOOTH,
4036 TPACPI_RFK_BLUETOOTH_SW_NAME,
4037 true);
4038 if (res)
4039 return res;
4041 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4042 &bluetooth_attr_group);
4043 if (res) {
4044 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4045 return res;
4048 return 0;
4051 /* procfs -------------------------------------------------------------- */
4052 static int bluetooth_read(struct seq_file *m)
4054 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4057 static int bluetooth_write(char *buf)
4059 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4062 static struct ibm_struct bluetooth_driver_data = {
4063 .name = "bluetooth",
4064 .read = bluetooth_read,
4065 .write = bluetooth_write,
4066 .exit = bluetooth_exit,
4067 .shutdown = bluetooth_shutdown,
4070 /*************************************************************************
4071 * Wan subdriver
4074 enum {
4075 /* ACPI GWAN/SWAN bits */
4076 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4077 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4078 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4079 0 = disable, 1 = enable */
4082 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4084 static int wan_get_status(void)
4086 int status;
4088 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4089 if (dbg_wwanemul)
4090 return (tpacpi_wwan_emulstate) ?
4091 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4092 #endif
4094 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4095 return -EIO;
4097 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4098 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4101 static int wan_set_status(enum tpacpi_rfkill_state state)
4103 int status;
4105 vdbg_printk(TPACPI_DBG_RFKILL,
4106 "will attempt to %s wwan\n",
4107 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4109 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4110 if (dbg_wwanemul) {
4111 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4112 return 0;
4114 #endif
4116 if (state == TPACPI_RFK_RADIO_ON)
4117 status = TP_ACPI_WANCARD_RADIOSSW
4118 | TP_ACPI_WANCARD_RESUMECTRL;
4119 else
4120 status = 0;
4122 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4123 return -EIO;
4125 return 0;
4128 /* sysfs wan enable ---------------------------------------------------- */
4129 static ssize_t wan_enable_show(struct device *dev,
4130 struct device_attribute *attr,
4131 char *buf)
4133 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4134 attr, buf);
4137 static ssize_t wan_enable_store(struct device *dev,
4138 struct device_attribute *attr,
4139 const char *buf, size_t count)
4141 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4142 attr, buf, count);
4145 static struct device_attribute dev_attr_wan_enable =
4146 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4147 wan_enable_show, wan_enable_store);
4149 /* --------------------------------------------------------------------- */
4151 static struct attribute *wan_attributes[] = {
4152 &dev_attr_wan_enable.attr,
4153 NULL
4156 static const struct attribute_group wan_attr_group = {
4157 .attrs = wan_attributes,
4160 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4161 .get_status = wan_get_status,
4162 .set_status = wan_set_status,
4165 static void wan_shutdown(void)
4167 /* Order firmware to save current state to NVRAM */
4168 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4169 TP_ACPI_WGSV_SAVE_STATE))
4170 printk(TPACPI_NOTICE
4171 "failed to save WWAN state to NVRAM\n");
4172 else
4173 vdbg_printk(TPACPI_DBG_RFKILL,
4174 "WWAN state saved to NVRAM\n");
4177 static void wan_exit(void)
4179 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4180 &wan_attr_group);
4182 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4184 wan_shutdown();
4187 static int __init wan_init(struct ibm_init_struct *iibm)
4189 int res;
4190 int status = 0;
4192 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4193 "initializing wan subdriver\n");
4195 TPACPI_ACPIHANDLE_INIT(hkey);
4197 tp_features.wan = hkey_handle &&
4198 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4200 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4201 "wan is %s, status 0x%02x\n",
4202 str_supported(tp_features.wan),
4203 status);
4205 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4206 if (dbg_wwanemul) {
4207 tp_features.wan = 1;
4208 printk(TPACPI_INFO
4209 "wwan switch emulation enabled\n");
4210 } else
4211 #endif
4212 if (tp_features.wan &&
4213 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4214 /* no wan hardware present in system */
4215 tp_features.wan = 0;
4216 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4217 "wan hardware not installed\n");
4220 if (!tp_features.wan)
4221 return 1;
4223 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4224 &wan_tprfk_ops,
4225 RFKILL_TYPE_WWAN,
4226 TPACPI_RFK_WWAN_SW_NAME,
4227 true);
4228 if (res)
4229 return res;
4231 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4232 &wan_attr_group);
4234 if (res) {
4235 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4236 return res;
4239 return 0;
4242 /* procfs -------------------------------------------------------------- */
4243 static int wan_read(struct seq_file *m)
4245 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4248 static int wan_write(char *buf)
4250 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4253 static struct ibm_struct wan_driver_data = {
4254 .name = "wan",
4255 .read = wan_read,
4256 .write = wan_write,
4257 .exit = wan_exit,
4258 .shutdown = wan_shutdown,
4261 /*************************************************************************
4262 * UWB subdriver
4265 enum {
4266 /* ACPI GUWB/SUWB bits */
4267 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4268 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4271 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4273 static int uwb_get_status(void)
4275 int status;
4277 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4278 if (dbg_uwbemul)
4279 return (tpacpi_uwb_emulstate) ?
4280 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4281 #endif
4283 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4284 return -EIO;
4286 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4287 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4290 static int uwb_set_status(enum tpacpi_rfkill_state state)
4292 int status;
4294 vdbg_printk(TPACPI_DBG_RFKILL,
4295 "will attempt to %s UWB\n",
4296 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4298 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4299 if (dbg_uwbemul) {
4300 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4301 return 0;
4303 #endif
4305 if (state == TPACPI_RFK_RADIO_ON)
4306 status = TP_ACPI_UWB_RADIOSSW;
4307 else
4308 status = 0;
4310 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4311 return -EIO;
4313 return 0;
4316 /* --------------------------------------------------------------------- */
4318 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4319 .get_status = uwb_get_status,
4320 .set_status = uwb_set_status,
4323 static void uwb_exit(void)
4325 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4328 static int __init uwb_init(struct ibm_init_struct *iibm)
4330 int res;
4331 int status = 0;
4333 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4334 "initializing uwb subdriver\n");
4336 TPACPI_ACPIHANDLE_INIT(hkey);
4338 tp_features.uwb = hkey_handle &&
4339 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4341 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4342 "uwb is %s, status 0x%02x\n",
4343 str_supported(tp_features.uwb),
4344 status);
4346 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4347 if (dbg_uwbemul) {
4348 tp_features.uwb = 1;
4349 printk(TPACPI_INFO
4350 "uwb switch emulation enabled\n");
4351 } else
4352 #endif
4353 if (tp_features.uwb &&
4354 !(status & TP_ACPI_UWB_HWPRESENT)) {
4355 /* no uwb hardware present in system */
4356 tp_features.uwb = 0;
4357 dbg_printk(TPACPI_DBG_INIT,
4358 "uwb hardware not installed\n");
4361 if (!tp_features.uwb)
4362 return 1;
4364 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4365 &uwb_tprfk_ops,
4366 RFKILL_TYPE_UWB,
4367 TPACPI_RFK_UWB_SW_NAME,
4368 false);
4369 return res;
4372 static struct ibm_struct uwb_driver_data = {
4373 .name = "uwb",
4374 .exit = uwb_exit,
4375 .flags.experimental = 1,
4378 /*************************************************************************
4379 * Video subdriver
4382 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4384 enum video_access_mode {
4385 TPACPI_VIDEO_NONE = 0,
4386 TPACPI_VIDEO_570, /* 570 */
4387 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4388 TPACPI_VIDEO_NEW, /* all others */
4391 enum { /* video status flags, based on VIDEO_570 */
4392 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4393 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4394 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4397 enum { /* TPACPI_VIDEO_570 constants */
4398 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4399 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4400 * video_status_flags */
4401 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4402 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4405 static enum video_access_mode video_supported;
4406 static int video_orig_autosw;
4408 static int video_autosw_get(void);
4409 static int video_autosw_set(int enable);
4411 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4413 static int __init video_init(struct ibm_init_struct *iibm)
4415 int ivga;
4417 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4419 TPACPI_ACPIHANDLE_INIT(vid);
4420 if (tpacpi_is_ibm())
4421 TPACPI_ACPIHANDLE_INIT(vid2);
4423 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4424 /* G41, assume IVGA doesn't change */
4425 vid_handle = vid2_handle;
4427 if (!vid_handle)
4428 /* video switching not supported on R30, R31 */
4429 video_supported = TPACPI_VIDEO_NONE;
4430 else if (tpacpi_is_ibm() &&
4431 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4432 /* 570 */
4433 video_supported = TPACPI_VIDEO_570;
4434 else if (tpacpi_is_ibm() &&
4435 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4436 /* 600e/x, 770e, 770x */
4437 video_supported = TPACPI_VIDEO_770;
4438 else
4439 /* all others */
4440 video_supported = TPACPI_VIDEO_NEW;
4442 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4443 str_supported(video_supported != TPACPI_VIDEO_NONE),
4444 video_supported);
4446 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4449 static void video_exit(void)
4451 dbg_printk(TPACPI_DBG_EXIT,
4452 "restoring original video autoswitch mode\n");
4453 if (video_autosw_set(video_orig_autosw))
4454 printk(TPACPI_ERR "error while trying to restore original "
4455 "video autoswitch mode\n");
4458 static int video_outputsw_get(void)
4460 int status = 0;
4461 int i;
4463 switch (video_supported) {
4464 case TPACPI_VIDEO_570:
4465 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4466 TP_ACPI_VIDEO_570_PHSCMD))
4467 return -EIO;
4468 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4469 break;
4470 case TPACPI_VIDEO_770:
4471 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4472 return -EIO;
4473 if (i)
4474 status |= TP_ACPI_VIDEO_S_LCD;
4475 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4476 return -EIO;
4477 if (i)
4478 status |= TP_ACPI_VIDEO_S_CRT;
4479 break;
4480 case TPACPI_VIDEO_NEW:
4481 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4482 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4483 return -EIO;
4484 if (i)
4485 status |= TP_ACPI_VIDEO_S_CRT;
4487 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4488 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4489 return -EIO;
4490 if (i)
4491 status |= TP_ACPI_VIDEO_S_LCD;
4492 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4493 return -EIO;
4494 if (i)
4495 status |= TP_ACPI_VIDEO_S_DVI;
4496 break;
4497 default:
4498 return -ENOSYS;
4501 return status;
4504 static int video_outputsw_set(int status)
4506 int autosw;
4507 int res = 0;
4509 switch (video_supported) {
4510 case TPACPI_VIDEO_570:
4511 res = acpi_evalf(NULL, NULL,
4512 "\\_SB.PHS2", "vdd",
4513 TP_ACPI_VIDEO_570_PHS2CMD,
4514 status | TP_ACPI_VIDEO_570_PHS2SET);
4515 break;
4516 case TPACPI_VIDEO_770:
4517 autosw = video_autosw_get();
4518 if (autosw < 0)
4519 return autosw;
4521 res = video_autosw_set(1);
4522 if (res)
4523 return res;
4524 res = acpi_evalf(vid_handle, NULL,
4525 "ASWT", "vdd", status * 0x100, 0);
4526 if (!autosw && video_autosw_set(autosw)) {
4527 printk(TPACPI_ERR
4528 "video auto-switch left enabled due to error\n");
4529 return -EIO;
4531 break;
4532 case TPACPI_VIDEO_NEW:
4533 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4534 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4535 break;
4536 default:
4537 return -ENOSYS;
4540 return (res)? 0 : -EIO;
4543 static int video_autosw_get(void)
4545 int autosw = 0;
4547 switch (video_supported) {
4548 case TPACPI_VIDEO_570:
4549 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4550 return -EIO;
4551 break;
4552 case TPACPI_VIDEO_770:
4553 case TPACPI_VIDEO_NEW:
4554 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4555 return -EIO;
4556 break;
4557 default:
4558 return -ENOSYS;
4561 return autosw & 1;
4564 static int video_autosw_set(int enable)
4566 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4567 return -EIO;
4568 return 0;
4571 static int video_outputsw_cycle(void)
4573 int autosw = video_autosw_get();
4574 int res;
4576 if (autosw < 0)
4577 return autosw;
4579 switch (video_supported) {
4580 case TPACPI_VIDEO_570:
4581 res = video_autosw_set(1);
4582 if (res)
4583 return res;
4584 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4585 break;
4586 case TPACPI_VIDEO_770:
4587 case TPACPI_VIDEO_NEW:
4588 res = video_autosw_set(1);
4589 if (res)
4590 return res;
4591 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4592 break;
4593 default:
4594 return -ENOSYS;
4596 if (!autosw && video_autosw_set(autosw)) {
4597 printk(TPACPI_ERR
4598 "video auto-switch left enabled due to error\n");
4599 return -EIO;
4602 return (res)? 0 : -EIO;
4605 static int video_expand_toggle(void)
4607 switch (video_supported) {
4608 case TPACPI_VIDEO_570:
4609 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4610 0 : -EIO;
4611 case TPACPI_VIDEO_770:
4612 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4613 0 : -EIO;
4614 case TPACPI_VIDEO_NEW:
4615 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4616 0 : -EIO;
4617 default:
4618 return -ENOSYS;
4620 /* not reached */
4623 static int video_read(struct seq_file *m)
4625 int status, autosw;
4627 if (video_supported == TPACPI_VIDEO_NONE) {
4628 seq_printf(m, "status:\t\tnot supported\n");
4629 return 0;
4632 /* Even reads can crash X.org, so... */
4633 if (!capable(CAP_SYS_ADMIN))
4634 return -EPERM;
4636 status = video_outputsw_get();
4637 if (status < 0)
4638 return status;
4640 autosw = video_autosw_get();
4641 if (autosw < 0)
4642 return autosw;
4644 seq_printf(m, "status:\t\tsupported\n");
4645 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4646 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4647 if (video_supported == TPACPI_VIDEO_NEW)
4648 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4649 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4650 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4651 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4652 if (video_supported == TPACPI_VIDEO_NEW)
4653 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4654 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4655 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4657 return 0;
4660 static int video_write(char *buf)
4662 char *cmd;
4663 int enable, disable, status;
4664 int res;
4666 if (video_supported == TPACPI_VIDEO_NONE)
4667 return -ENODEV;
4669 /* Even reads can crash X.org, let alone writes... */
4670 if (!capable(CAP_SYS_ADMIN))
4671 return -EPERM;
4673 enable = 0;
4674 disable = 0;
4676 while ((cmd = next_cmd(&buf))) {
4677 if (strlencmp(cmd, "lcd_enable") == 0) {
4678 enable |= TP_ACPI_VIDEO_S_LCD;
4679 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4680 disable |= TP_ACPI_VIDEO_S_LCD;
4681 } else if (strlencmp(cmd, "crt_enable") == 0) {
4682 enable |= TP_ACPI_VIDEO_S_CRT;
4683 } else if (strlencmp(cmd, "crt_disable") == 0) {
4684 disable |= TP_ACPI_VIDEO_S_CRT;
4685 } else if (video_supported == TPACPI_VIDEO_NEW &&
4686 strlencmp(cmd, "dvi_enable") == 0) {
4687 enable |= TP_ACPI_VIDEO_S_DVI;
4688 } else if (video_supported == TPACPI_VIDEO_NEW &&
4689 strlencmp(cmd, "dvi_disable") == 0) {
4690 disable |= TP_ACPI_VIDEO_S_DVI;
4691 } else if (strlencmp(cmd, "auto_enable") == 0) {
4692 res = video_autosw_set(1);
4693 if (res)
4694 return res;
4695 } else if (strlencmp(cmd, "auto_disable") == 0) {
4696 res = video_autosw_set(0);
4697 if (res)
4698 return res;
4699 } else if (strlencmp(cmd, "video_switch") == 0) {
4700 res = video_outputsw_cycle();
4701 if (res)
4702 return res;
4703 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4704 res = video_expand_toggle();
4705 if (res)
4706 return res;
4707 } else
4708 return -EINVAL;
4711 if (enable || disable) {
4712 status = video_outputsw_get();
4713 if (status < 0)
4714 return status;
4715 res = video_outputsw_set((status & ~disable) | enable);
4716 if (res)
4717 return res;
4720 return 0;
4723 static struct ibm_struct video_driver_data = {
4724 .name = "video",
4725 .read = video_read,
4726 .write = video_write,
4727 .exit = video_exit,
4730 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4732 /*************************************************************************
4733 * Light (thinklight) subdriver
4736 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4737 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4739 static int light_get_status(void)
4741 int status = 0;
4743 if (tp_features.light_status) {
4744 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4745 return -EIO;
4746 return (!!status);
4749 return -ENXIO;
4752 static int light_set_status(int status)
4754 int rc;
4756 if (tp_features.light) {
4757 if (cmos_handle) {
4758 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4759 (status)?
4760 TP_CMOS_THINKLIGHT_ON :
4761 TP_CMOS_THINKLIGHT_OFF);
4762 } else {
4763 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4764 (status)? 1 : 0);
4766 return (rc)? 0 : -EIO;
4769 return -ENXIO;
4772 static void light_set_status_worker(struct work_struct *work)
4774 struct tpacpi_led_classdev *data =
4775 container_of(work, struct tpacpi_led_classdev, work);
4777 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4778 light_set_status((data->new_state != TPACPI_LED_OFF));
4781 static void light_sysfs_set(struct led_classdev *led_cdev,
4782 enum led_brightness brightness)
4784 struct tpacpi_led_classdev *data =
4785 container_of(led_cdev,
4786 struct tpacpi_led_classdev,
4787 led_classdev);
4788 data->new_state = (brightness != LED_OFF) ?
4789 TPACPI_LED_ON : TPACPI_LED_OFF;
4790 queue_work(tpacpi_wq, &data->work);
4793 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4795 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4798 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4799 .led_classdev = {
4800 .name = "tpacpi::thinklight",
4801 .brightness_set = &light_sysfs_set,
4802 .brightness_get = &light_sysfs_get,
4806 static int __init light_init(struct ibm_init_struct *iibm)
4808 int rc;
4810 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4812 if (tpacpi_is_ibm()) {
4813 TPACPI_ACPIHANDLE_INIT(ledb);
4814 TPACPI_ACPIHANDLE_INIT(lght);
4816 TPACPI_ACPIHANDLE_INIT(cmos);
4817 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4819 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4820 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4822 if (tp_features.light)
4823 /* light status not supported on
4824 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4825 tp_features.light_status =
4826 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4828 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4829 str_supported(tp_features.light),
4830 str_supported(tp_features.light_status));
4832 if (!tp_features.light)
4833 return 1;
4835 rc = led_classdev_register(&tpacpi_pdev->dev,
4836 &tpacpi_led_thinklight.led_classdev);
4838 if (rc < 0) {
4839 tp_features.light = 0;
4840 tp_features.light_status = 0;
4841 } else {
4842 rc = 0;
4845 return rc;
4848 static void light_exit(void)
4850 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4851 if (work_pending(&tpacpi_led_thinklight.work))
4852 flush_workqueue(tpacpi_wq);
4855 static int light_read(struct seq_file *m)
4857 int status;
4859 if (!tp_features.light) {
4860 seq_printf(m, "status:\t\tnot supported\n");
4861 } else if (!tp_features.light_status) {
4862 seq_printf(m, "status:\t\tunknown\n");
4863 seq_printf(m, "commands:\ton, off\n");
4864 } else {
4865 status = light_get_status();
4866 if (status < 0)
4867 return status;
4868 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4869 seq_printf(m, "commands:\ton, off\n");
4872 return 0;
4875 static int light_write(char *buf)
4877 char *cmd;
4878 int newstatus = 0;
4880 if (!tp_features.light)
4881 return -ENODEV;
4883 while ((cmd = next_cmd(&buf))) {
4884 if (strlencmp(cmd, "on") == 0) {
4885 newstatus = 1;
4886 } else if (strlencmp(cmd, "off") == 0) {
4887 newstatus = 0;
4888 } else
4889 return -EINVAL;
4892 return light_set_status(newstatus);
4895 static struct ibm_struct light_driver_data = {
4896 .name = "light",
4897 .read = light_read,
4898 .write = light_write,
4899 .exit = light_exit,
4902 /*************************************************************************
4903 * CMOS subdriver
4906 /* sysfs cmos_command -------------------------------------------------- */
4907 static ssize_t cmos_command_store(struct device *dev,
4908 struct device_attribute *attr,
4909 const char *buf, size_t count)
4911 unsigned long cmos_cmd;
4912 int res;
4914 if (parse_strtoul(buf, 21, &cmos_cmd))
4915 return -EINVAL;
4917 res = issue_thinkpad_cmos_command(cmos_cmd);
4918 return (res)? res : count;
4921 static struct device_attribute dev_attr_cmos_command =
4922 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4924 /* --------------------------------------------------------------------- */
4926 static int __init cmos_init(struct ibm_init_struct *iibm)
4928 int res;
4930 vdbg_printk(TPACPI_DBG_INIT,
4931 "initializing cmos commands subdriver\n");
4933 TPACPI_ACPIHANDLE_INIT(cmos);
4935 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4936 str_supported(cmos_handle != NULL));
4938 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4939 if (res)
4940 return res;
4942 return (cmos_handle)? 0 : 1;
4945 static void cmos_exit(void)
4947 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4950 static int cmos_read(struct seq_file *m)
4952 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4953 R30, R31, T20-22, X20-21 */
4954 if (!cmos_handle)
4955 seq_printf(m, "status:\t\tnot supported\n");
4956 else {
4957 seq_printf(m, "status:\t\tsupported\n");
4958 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4961 return 0;
4964 static int cmos_write(char *buf)
4966 char *cmd;
4967 int cmos_cmd, res;
4969 while ((cmd = next_cmd(&buf))) {
4970 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4971 cmos_cmd >= 0 && cmos_cmd <= 21) {
4972 /* cmos_cmd set */
4973 } else
4974 return -EINVAL;
4976 res = issue_thinkpad_cmos_command(cmos_cmd);
4977 if (res)
4978 return res;
4981 return 0;
4984 static struct ibm_struct cmos_driver_data = {
4985 .name = "cmos",
4986 .read = cmos_read,
4987 .write = cmos_write,
4988 .exit = cmos_exit,
4991 /*************************************************************************
4992 * LED subdriver
4995 enum led_access_mode {
4996 TPACPI_LED_NONE = 0,
4997 TPACPI_LED_570, /* 570 */
4998 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4999 TPACPI_LED_NEW, /* all others */
5002 enum { /* For TPACPI_LED_OLD */
5003 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5004 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5005 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5008 static enum led_access_mode led_supported;
5010 static acpi_handle led_handle;
5012 #define TPACPI_LED_NUMLEDS 16
5013 static struct tpacpi_led_classdev *tpacpi_leds;
5014 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5015 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5016 /* there's a limit of 19 chars + NULL before 2.6.26 */
5017 "tpacpi::power",
5018 "tpacpi:orange:batt",
5019 "tpacpi:green:batt",
5020 "tpacpi::dock_active",
5021 "tpacpi::bay_active",
5022 "tpacpi::dock_batt",
5023 "tpacpi::unknown_led",
5024 "tpacpi::standby",
5025 "tpacpi::dock_status1",
5026 "tpacpi::dock_status2",
5027 "tpacpi::unknown_led2",
5028 "tpacpi::unknown_led3",
5029 "tpacpi::thinkvantage",
5031 #define TPACPI_SAFE_LEDS 0x1081U
5033 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5035 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5036 return false;
5037 #else
5038 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5039 #endif
5042 static int led_get_status(const unsigned int led)
5044 int status;
5045 enum led_status_t led_s;
5047 switch (led_supported) {
5048 case TPACPI_LED_570:
5049 if (!acpi_evalf(ec_handle,
5050 &status, "GLED", "dd", 1 << led))
5051 return -EIO;
5052 led_s = (status == 0)?
5053 TPACPI_LED_OFF :
5054 ((status == 1)?
5055 TPACPI_LED_ON :
5056 TPACPI_LED_BLINK);
5057 tpacpi_led_state_cache[led] = led_s;
5058 return led_s;
5059 default:
5060 return -ENXIO;
5063 /* not reached */
5066 static int led_set_status(const unsigned int led,
5067 const enum led_status_t ledstatus)
5069 /* off, on, blink. Index is led_status_t */
5070 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5071 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5073 int rc = 0;
5075 switch (led_supported) {
5076 case TPACPI_LED_570:
5077 /* 570 */
5078 if (unlikely(led > 7))
5079 return -EINVAL;
5080 if (unlikely(tpacpi_is_led_restricted(led)))
5081 return -EPERM;
5082 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5083 (1 << led), led_sled_arg1[ledstatus]))
5084 rc = -EIO;
5085 break;
5086 case TPACPI_LED_OLD:
5087 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5088 if (unlikely(led > 7))
5089 return -EINVAL;
5090 if (unlikely(tpacpi_is_led_restricted(led)))
5091 return -EPERM;
5092 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5093 if (rc >= 0)
5094 rc = ec_write(TPACPI_LED_EC_HLBL,
5095 (ledstatus == TPACPI_LED_BLINK) << led);
5096 if (rc >= 0)
5097 rc = ec_write(TPACPI_LED_EC_HLCL,
5098 (ledstatus != TPACPI_LED_OFF) << led);
5099 break;
5100 case TPACPI_LED_NEW:
5101 /* all others */
5102 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5103 return -EINVAL;
5104 if (unlikely(tpacpi_is_led_restricted(led)))
5105 return -EPERM;
5106 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5107 led, led_led_arg1[ledstatus]))
5108 rc = -EIO;
5109 break;
5110 default:
5111 rc = -ENXIO;
5114 if (!rc)
5115 tpacpi_led_state_cache[led] = ledstatus;
5117 return rc;
5120 static void led_set_status_worker(struct work_struct *work)
5122 struct tpacpi_led_classdev *data =
5123 container_of(work, struct tpacpi_led_classdev, work);
5125 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5126 led_set_status(data->led, data->new_state);
5129 static void led_sysfs_set(struct led_classdev *led_cdev,
5130 enum led_brightness brightness)
5132 struct tpacpi_led_classdev *data = container_of(led_cdev,
5133 struct tpacpi_led_classdev, led_classdev);
5135 if (brightness == LED_OFF)
5136 data->new_state = TPACPI_LED_OFF;
5137 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5138 data->new_state = TPACPI_LED_ON;
5139 else
5140 data->new_state = TPACPI_LED_BLINK;
5142 queue_work(tpacpi_wq, &data->work);
5145 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5146 unsigned long *delay_on, unsigned long *delay_off)
5148 struct tpacpi_led_classdev *data = container_of(led_cdev,
5149 struct tpacpi_led_classdev, led_classdev);
5151 /* Can we choose the flash rate? */
5152 if (*delay_on == 0 && *delay_off == 0) {
5153 /* yes. set them to the hardware blink rate (1 Hz) */
5154 *delay_on = 500; /* ms */
5155 *delay_off = 500; /* ms */
5156 } else if ((*delay_on != 500) || (*delay_off != 500))
5157 return -EINVAL;
5159 data->new_state = TPACPI_LED_BLINK;
5160 queue_work(tpacpi_wq, &data->work);
5162 return 0;
5165 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5167 int rc;
5169 struct tpacpi_led_classdev *data = container_of(led_cdev,
5170 struct tpacpi_led_classdev, led_classdev);
5172 rc = led_get_status(data->led);
5174 if (rc == TPACPI_LED_OFF || rc < 0)
5175 rc = LED_OFF; /* no error handling in led class :( */
5176 else
5177 rc = LED_FULL;
5179 return rc;
5182 static void led_exit(void)
5184 unsigned int i;
5186 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5187 if (tpacpi_leds[i].led_classdev.name)
5188 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5191 kfree(tpacpi_leds);
5194 static int __init tpacpi_init_led(unsigned int led)
5196 int rc;
5198 tpacpi_leds[led].led = led;
5200 /* LEDs with no name don't get registered */
5201 if (!tpacpi_led_names[led])
5202 return 0;
5204 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5205 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5206 if (led_supported == TPACPI_LED_570)
5207 tpacpi_leds[led].led_classdev.brightness_get =
5208 &led_sysfs_get;
5210 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5212 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5214 rc = led_classdev_register(&tpacpi_pdev->dev,
5215 &tpacpi_leds[led].led_classdev);
5216 if (rc < 0)
5217 tpacpi_leds[led].led_classdev.name = NULL;
5219 return rc;
5222 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5223 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5224 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5225 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5227 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5228 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5229 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5230 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5231 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5232 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5233 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5234 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5236 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5237 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5238 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5239 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5240 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5242 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5243 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5244 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5245 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5247 /* (1) - may have excess leds enabled on MSB */
5249 /* Defaults (order matters, keep last, don't reorder!) */
5250 { /* Lenovo */
5251 .vendor = PCI_VENDOR_ID_LENOVO,
5252 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5253 .quirks = 0x1fffU,
5255 { /* IBM ThinkPads with no EC version string */
5256 .vendor = PCI_VENDOR_ID_IBM,
5257 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5258 .quirks = 0x00ffU,
5260 { /* IBM ThinkPads with EC version string */
5261 .vendor = PCI_VENDOR_ID_IBM,
5262 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5263 .quirks = 0x00bfU,
5267 #undef TPACPI_LEDQ_IBM
5268 #undef TPACPI_LEDQ_LNV
5270 static enum led_access_mode __init led_init_detect_mode(void)
5272 acpi_status status;
5274 if (tpacpi_is_ibm()) {
5275 /* 570 */
5276 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5277 if (ACPI_SUCCESS(status))
5278 return TPACPI_LED_570;
5280 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5281 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5282 if (ACPI_SUCCESS(status))
5283 return TPACPI_LED_OLD;
5286 /* most others */
5287 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5288 if (ACPI_SUCCESS(status))
5289 return TPACPI_LED_NEW;
5291 /* R30, R31, and unknown firmwares */
5292 led_handle = NULL;
5293 return TPACPI_LED_NONE;
5296 static int __init led_init(struct ibm_init_struct *iibm)
5298 unsigned int i;
5299 int rc;
5300 unsigned long useful_leds;
5302 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5304 led_supported = led_init_detect_mode();
5306 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5307 str_supported(led_supported), led_supported);
5309 if (led_supported == TPACPI_LED_NONE)
5310 return 1;
5312 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5313 GFP_KERNEL);
5314 if (!tpacpi_leds) {
5315 printk(TPACPI_ERR "Out of memory for LED data\n");
5316 return -ENOMEM;
5319 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5320 ARRAY_SIZE(led_useful_qtable));
5322 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5323 if (!tpacpi_is_led_restricted(i) &&
5324 test_bit(i, &useful_leds)) {
5325 rc = tpacpi_init_led(i);
5326 if (rc < 0) {
5327 led_exit();
5328 return rc;
5333 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5334 printk(TPACPI_NOTICE
5335 "warning: userspace override of important "
5336 "firmware LEDs is enabled\n");
5337 #endif
5338 return 0;
5341 #define str_led_status(s) \
5342 ((s) == TPACPI_LED_OFF ? "off" : \
5343 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5345 static int led_read(struct seq_file *m)
5347 if (!led_supported) {
5348 seq_printf(m, "status:\t\tnot supported\n");
5349 return 0;
5351 seq_printf(m, "status:\t\tsupported\n");
5353 if (led_supported == TPACPI_LED_570) {
5354 /* 570 */
5355 int i, status;
5356 for (i = 0; i < 8; i++) {
5357 status = led_get_status(i);
5358 if (status < 0)
5359 return -EIO;
5360 seq_printf(m, "%d:\t\t%s\n",
5361 i, str_led_status(status));
5365 seq_printf(m, "commands:\t"
5366 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5368 return 0;
5371 static int led_write(char *buf)
5373 char *cmd;
5374 int led, rc;
5375 enum led_status_t s;
5377 if (!led_supported)
5378 return -ENODEV;
5380 while ((cmd = next_cmd(&buf))) {
5381 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5382 return -EINVAL;
5384 if (strstr(cmd, "off")) {
5385 s = TPACPI_LED_OFF;
5386 } else if (strstr(cmd, "on")) {
5387 s = TPACPI_LED_ON;
5388 } else if (strstr(cmd, "blink")) {
5389 s = TPACPI_LED_BLINK;
5390 } else {
5391 return -EINVAL;
5394 rc = led_set_status(led, s);
5395 if (rc < 0)
5396 return rc;
5399 return 0;
5402 static struct ibm_struct led_driver_data = {
5403 .name = "led",
5404 .read = led_read,
5405 .write = led_write,
5406 .exit = led_exit,
5409 /*************************************************************************
5410 * Beep subdriver
5413 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5415 #define TPACPI_BEEP_Q1 0x0001
5417 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5418 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5419 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5422 static int __init beep_init(struct ibm_init_struct *iibm)
5424 unsigned long quirks;
5426 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5428 TPACPI_ACPIHANDLE_INIT(beep);
5430 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5431 str_supported(beep_handle != NULL));
5433 quirks = tpacpi_check_quirks(beep_quirk_table,
5434 ARRAY_SIZE(beep_quirk_table));
5436 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5438 return (beep_handle)? 0 : 1;
5441 static int beep_read(struct seq_file *m)
5443 if (!beep_handle)
5444 seq_printf(m, "status:\t\tnot supported\n");
5445 else {
5446 seq_printf(m, "status:\t\tsupported\n");
5447 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5450 return 0;
5453 static int beep_write(char *buf)
5455 char *cmd;
5456 int beep_cmd;
5458 if (!beep_handle)
5459 return -ENODEV;
5461 while ((cmd = next_cmd(&buf))) {
5462 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5463 beep_cmd >= 0 && beep_cmd <= 17) {
5464 /* beep_cmd set */
5465 } else
5466 return -EINVAL;
5467 if (tp_features.beep_needs_two_args) {
5468 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5469 beep_cmd, 0))
5470 return -EIO;
5471 } else {
5472 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5473 beep_cmd))
5474 return -EIO;
5478 return 0;
5481 static struct ibm_struct beep_driver_data = {
5482 .name = "beep",
5483 .read = beep_read,
5484 .write = beep_write,
5487 /*************************************************************************
5488 * Thermal subdriver
5491 enum thermal_access_mode {
5492 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5493 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5494 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5495 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5496 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5499 enum { /* TPACPI_THERMAL_TPEC_* */
5500 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5501 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5502 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5504 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5508 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5509 struct ibm_thermal_sensors_struct {
5510 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5513 static enum thermal_access_mode thermal_read_mode;
5515 /* idx is zero-based */
5516 static int thermal_get_sensor(int idx, s32 *value)
5518 int t;
5519 s8 tmp;
5520 char tmpi[5];
5522 t = TP_EC_THERMAL_TMP0;
5524 switch (thermal_read_mode) {
5525 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5526 case TPACPI_THERMAL_TPEC_16:
5527 if (idx >= 8 && idx <= 15) {
5528 t = TP_EC_THERMAL_TMP8;
5529 idx -= 8;
5531 /* fallthrough */
5532 #endif
5533 case TPACPI_THERMAL_TPEC_8:
5534 if (idx <= 7) {
5535 if (!acpi_ec_read(t + idx, &tmp))
5536 return -EIO;
5537 *value = tmp * 1000;
5538 return 0;
5540 break;
5542 case TPACPI_THERMAL_ACPI_UPDT:
5543 if (idx <= 7) {
5544 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5545 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5546 return -EIO;
5547 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5548 return -EIO;
5549 *value = (t - 2732) * 100;
5550 return 0;
5552 break;
5554 case TPACPI_THERMAL_ACPI_TMP07:
5555 if (idx <= 7) {
5556 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5557 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5558 return -EIO;
5559 if (t > 127 || t < -127)
5560 t = TP_EC_THERMAL_TMP_NA;
5561 *value = t * 1000;
5562 return 0;
5564 break;
5566 case TPACPI_THERMAL_NONE:
5567 default:
5568 return -ENOSYS;
5571 return -EINVAL;
5574 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5576 int res, i;
5577 int n;
5579 n = 8;
5580 i = 0;
5582 if (!s)
5583 return -EINVAL;
5585 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5586 n = 16;
5588 for (i = 0 ; i < n; i++) {
5589 res = thermal_get_sensor(i, &s->temp[i]);
5590 if (res)
5591 return res;
5594 return n;
5597 static void thermal_dump_all_sensors(void)
5599 int n, i;
5600 struct ibm_thermal_sensors_struct t;
5602 n = thermal_get_sensors(&t);
5603 if (n <= 0)
5604 return;
5606 printk(TPACPI_NOTICE
5607 "temperatures (Celsius):");
5609 for (i = 0; i < n; i++) {
5610 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5611 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5612 else
5613 printk(KERN_CONT " N/A");
5616 printk(KERN_CONT "\n");
5619 /* sysfs temp##_input -------------------------------------------------- */
5621 static ssize_t thermal_temp_input_show(struct device *dev,
5622 struct device_attribute *attr,
5623 char *buf)
5625 struct sensor_device_attribute *sensor_attr =
5626 to_sensor_dev_attr(attr);
5627 int idx = sensor_attr->index;
5628 s32 value;
5629 int res;
5631 res = thermal_get_sensor(idx, &value);
5632 if (res)
5633 return res;
5634 if (value == TPACPI_THERMAL_SENSOR_NA)
5635 return -ENXIO;
5637 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5640 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5641 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5642 thermal_temp_input_show, NULL, _idxB)
5644 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5645 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5646 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5647 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5648 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5649 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5650 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5651 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5652 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5653 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5654 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5655 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5656 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5657 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5658 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5659 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5660 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5663 #define THERMAL_ATTRS(X) \
5664 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5666 static struct attribute *thermal_temp_input_attr[] = {
5667 THERMAL_ATTRS(8),
5668 THERMAL_ATTRS(9),
5669 THERMAL_ATTRS(10),
5670 THERMAL_ATTRS(11),
5671 THERMAL_ATTRS(12),
5672 THERMAL_ATTRS(13),
5673 THERMAL_ATTRS(14),
5674 THERMAL_ATTRS(15),
5675 THERMAL_ATTRS(0),
5676 THERMAL_ATTRS(1),
5677 THERMAL_ATTRS(2),
5678 THERMAL_ATTRS(3),
5679 THERMAL_ATTRS(4),
5680 THERMAL_ATTRS(5),
5681 THERMAL_ATTRS(6),
5682 THERMAL_ATTRS(7),
5683 NULL
5686 static const struct attribute_group thermal_temp_input16_group = {
5687 .attrs = thermal_temp_input_attr
5690 static const struct attribute_group thermal_temp_input8_group = {
5691 .attrs = &thermal_temp_input_attr[8]
5694 #undef THERMAL_SENSOR_ATTR_TEMP
5695 #undef THERMAL_ATTRS
5697 /* --------------------------------------------------------------------- */
5699 static int __init thermal_init(struct ibm_init_struct *iibm)
5701 u8 t, ta1, ta2;
5702 int i;
5703 int acpi_tmp7;
5704 int res;
5706 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5708 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5710 if (thinkpad_id.ec_model) {
5712 * Direct EC access mode: sensors at registers
5713 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5714 * non-implemented, thermal sensors return 0x80 when
5715 * not available
5718 ta1 = ta2 = 0;
5719 for (i = 0; i < 8; i++) {
5720 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5721 ta1 |= t;
5722 } else {
5723 ta1 = 0;
5724 break;
5726 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5727 ta2 |= t;
5728 } else {
5729 ta1 = 0;
5730 break;
5733 if (ta1 == 0) {
5734 /* This is sheer paranoia, but we handle it anyway */
5735 if (acpi_tmp7) {
5736 printk(TPACPI_ERR
5737 "ThinkPad ACPI EC access misbehaving, "
5738 "falling back to ACPI TMPx access "
5739 "mode\n");
5740 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5741 } else {
5742 printk(TPACPI_ERR
5743 "ThinkPad ACPI EC access misbehaving, "
5744 "disabling thermal sensors access\n");
5745 thermal_read_mode = TPACPI_THERMAL_NONE;
5747 } else {
5748 thermal_read_mode =
5749 (ta2 != 0) ?
5750 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5752 } else if (acpi_tmp7) {
5753 if (tpacpi_is_ibm() &&
5754 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5755 /* 600e/x, 770e, 770x */
5756 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5757 } else {
5758 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
5759 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5761 } else {
5762 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5763 thermal_read_mode = TPACPI_THERMAL_NONE;
5766 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5767 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5768 thermal_read_mode);
5770 switch (thermal_read_mode) {
5771 case TPACPI_THERMAL_TPEC_16:
5772 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5773 &thermal_temp_input16_group);
5774 if (res)
5775 return res;
5776 break;
5777 case TPACPI_THERMAL_TPEC_8:
5778 case TPACPI_THERMAL_ACPI_TMP07:
5779 case TPACPI_THERMAL_ACPI_UPDT:
5780 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5781 &thermal_temp_input8_group);
5782 if (res)
5783 return res;
5784 break;
5785 case TPACPI_THERMAL_NONE:
5786 default:
5787 return 1;
5790 return 0;
5793 static void thermal_exit(void)
5795 switch (thermal_read_mode) {
5796 case TPACPI_THERMAL_TPEC_16:
5797 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5798 &thermal_temp_input16_group);
5799 break;
5800 case TPACPI_THERMAL_TPEC_8:
5801 case TPACPI_THERMAL_ACPI_TMP07:
5802 case TPACPI_THERMAL_ACPI_UPDT:
5803 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5804 &thermal_temp_input8_group);
5805 break;
5806 case TPACPI_THERMAL_NONE:
5807 default:
5808 break;
5812 static int thermal_read(struct seq_file *m)
5814 int n, i;
5815 struct ibm_thermal_sensors_struct t;
5817 n = thermal_get_sensors(&t);
5818 if (unlikely(n < 0))
5819 return n;
5821 seq_printf(m, "temperatures:\t");
5823 if (n > 0) {
5824 for (i = 0; i < (n - 1); i++)
5825 seq_printf(m, "%d ", t.temp[i] / 1000);
5826 seq_printf(m, "%d\n", t.temp[i] / 1000);
5827 } else
5828 seq_printf(m, "not supported\n");
5830 return 0;
5833 static struct ibm_struct thermal_driver_data = {
5834 .name = "thermal",
5835 .read = thermal_read,
5836 .exit = thermal_exit,
5839 /*************************************************************************
5840 * EC Dump subdriver
5843 static u8 ecdump_regs[256];
5845 static int ecdump_read(struct seq_file *m)
5847 int i, j;
5848 u8 v;
5850 seq_printf(m, "EC "
5851 " +00 +01 +02 +03 +04 +05 +06 +07"
5852 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5853 for (i = 0; i < 256; i += 16) {
5854 seq_printf(m, "EC 0x%02x:", i);
5855 for (j = 0; j < 16; j++) {
5856 if (!acpi_ec_read(i + j, &v))
5857 break;
5858 if (v != ecdump_regs[i + j])
5859 seq_printf(m, " *%02x", v);
5860 else
5861 seq_printf(m, " %02x", v);
5862 ecdump_regs[i + j] = v;
5864 seq_putc(m, '\n');
5865 if (j != 16)
5866 break;
5869 /* These are way too dangerous to advertise openly... */
5870 #if 0
5871 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5872 " (<offset> is 00-ff, <value> is 00-ff)\n");
5873 seq_printf(m, "commands:\t0x<offset> <value> "
5874 " (<offset> is 00-ff, <value> is 0-255)\n");
5875 #endif
5876 return 0;
5879 static int ecdump_write(char *buf)
5881 char *cmd;
5882 int i, v;
5884 while ((cmd = next_cmd(&buf))) {
5885 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5886 /* i and v set */
5887 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5888 /* i and v set */
5889 } else
5890 return -EINVAL;
5891 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5892 if (!acpi_ec_write(i, v))
5893 return -EIO;
5894 } else
5895 return -EINVAL;
5898 return 0;
5901 static struct ibm_struct ecdump_driver_data = {
5902 .name = "ecdump",
5903 .read = ecdump_read,
5904 .write = ecdump_write,
5905 .flags.experimental = 1,
5908 /*************************************************************************
5909 * Backlight/brightness subdriver
5912 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5915 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5916 * CMOS NVRAM byte 0x5E, bits 0-3.
5918 * EC HBRV (0x31) has the following layout
5919 * Bit 7: unknown function
5920 * Bit 6: unknown function
5921 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5922 * Bit 4: must be set to zero to avoid problems
5923 * Bit 3-0: backlight brightness level
5925 * brightness_get_raw returns status data in the HBRV layout
5927 * WARNING: The X61 has been verified to use HBRV for something else, so
5928 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5929 * testing on the very early *60 Lenovo models...
5932 enum {
5933 TP_EC_BACKLIGHT = 0x31,
5935 /* TP_EC_BACKLIGHT bitmasks */
5936 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5937 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5938 TP_EC_BACKLIGHT_MAPSW = 0x20,
5941 enum tpacpi_brightness_access_mode {
5942 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5943 TPACPI_BRGHT_MODE_EC, /* EC control */
5944 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5945 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5946 TPACPI_BRGHT_MODE_MAX
5949 static struct backlight_device *ibm_backlight_device;
5951 static enum tpacpi_brightness_access_mode brightness_mode =
5952 TPACPI_BRGHT_MODE_MAX;
5954 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5956 static struct mutex brightness_mutex;
5958 /* NVRAM brightness access,
5959 * call with brightness_mutex held! */
5960 static unsigned int tpacpi_brightness_nvram_get(void)
5962 u8 lnvram;
5964 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5965 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5966 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5967 lnvram &= bright_maxlvl;
5969 return lnvram;
5972 static void tpacpi_brightness_checkpoint_nvram(void)
5974 u8 lec = 0;
5975 u8 b_nvram;
5977 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5978 return;
5980 vdbg_printk(TPACPI_DBG_BRGHT,
5981 "trying to checkpoint backlight level to NVRAM...\n");
5983 if (mutex_lock_killable(&brightness_mutex) < 0)
5984 return;
5986 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5987 goto unlock;
5988 lec &= TP_EC_BACKLIGHT_LVLMSK;
5989 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5991 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5992 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5993 /* NVRAM needs update */
5994 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5995 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5996 b_nvram |= lec;
5997 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5998 dbg_printk(TPACPI_DBG_BRGHT,
5999 "updated NVRAM backlight level to %u (0x%02x)\n",
6000 (unsigned int) lec, (unsigned int) b_nvram);
6001 } else
6002 vdbg_printk(TPACPI_DBG_BRGHT,
6003 "NVRAM backlight level already is %u (0x%02x)\n",
6004 (unsigned int) lec, (unsigned int) b_nvram);
6006 unlock:
6007 mutex_unlock(&brightness_mutex);
6011 /* call with brightness_mutex held! */
6012 static int tpacpi_brightness_get_raw(int *status)
6014 u8 lec = 0;
6016 switch (brightness_mode) {
6017 case TPACPI_BRGHT_MODE_UCMS_STEP:
6018 *status = tpacpi_brightness_nvram_get();
6019 return 0;
6020 case TPACPI_BRGHT_MODE_EC:
6021 case TPACPI_BRGHT_MODE_ECNVRAM:
6022 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6023 return -EIO;
6024 *status = lec;
6025 return 0;
6026 default:
6027 return -ENXIO;
6031 /* call with brightness_mutex held! */
6032 /* do NOT call with illegal backlight level value */
6033 static int tpacpi_brightness_set_ec(unsigned int value)
6035 u8 lec = 0;
6037 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6038 return -EIO;
6040 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6041 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6042 (value & TP_EC_BACKLIGHT_LVLMSK))))
6043 return -EIO;
6045 return 0;
6048 /* call with brightness_mutex held! */
6049 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6051 int cmos_cmd, inc;
6052 unsigned int current_value, i;
6054 current_value = tpacpi_brightness_nvram_get();
6056 if (value == current_value)
6057 return 0;
6059 cmos_cmd = (value > current_value) ?
6060 TP_CMOS_BRIGHTNESS_UP :
6061 TP_CMOS_BRIGHTNESS_DOWN;
6062 inc = (value > current_value) ? 1 : -1;
6064 for (i = current_value; i != value; i += inc)
6065 if (issue_thinkpad_cmos_command(cmos_cmd))
6066 return -EIO;
6068 return 0;
6071 /* May return EINTR which can always be mapped to ERESTARTSYS */
6072 static int brightness_set(unsigned int value)
6074 int res;
6076 if (value > bright_maxlvl || value < 0)
6077 return -EINVAL;
6079 vdbg_printk(TPACPI_DBG_BRGHT,
6080 "set backlight level to %d\n", value);
6082 res = mutex_lock_killable(&brightness_mutex);
6083 if (res < 0)
6084 return res;
6086 switch (brightness_mode) {
6087 case TPACPI_BRGHT_MODE_EC:
6088 case TPACPI_BRGHT_MODE_ECNVRAM:
6089 res = tpacpi_brightness_set_ec(value);
6090 break;
6091 case TPACPI_BRGHT_MODE_UCMS_STEP:
6092 res = tpacpi_brightness_set_ucmsstep(value);
6093 break;
6094 default:
6095 res = -ENXIO;
6098 mutex_unlock(&brightness_mutex);
6099 return res;
6102 /* sysfs backlight class ----------------------------------------------- */
6104 static int brightness_update_status(struct backlight_device *bd)
6106 unsigned int level =
6107 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6108 bd->props.power == FB_BLANK_UNBLANK) ?
6109 bd->props.brightness : 0;
6111 dbg_printk(TPACPI_DBG_BRGHT,
6112 "backlight: attempt to set level to %d\n",
6113 level);
6115 /* it is the backlight class's job (caller) to handle
6116 * EINTR and other errors properly */
6117 return brightness_set(level);
6120 static int brightness_get(struct backlight_device *bd)
6122 int status, res;
6124 res = mutex_lock_killable(&brightness_mutex);
6125 if (res < 0)
6126 return 0;
6128 res = tpacpi_brightness_get_raw(&status);
6130 mutex_unlock(&brightness_mutex);
6132 if (res < 0)
6133 return 0;
6135 return status & TP_EC_BACKLIGHT_LVLMSK;
6138 static void tpacpi_brightness_notify_change(void)
6140 backlight_force_update(ibm_backlight_device,
6141 BACKLIGHT_UPDATE_HOTKEY);
6144 static struct backlight_ops ibm_backlight_data = {
6145 .get_brightness = brightness_get,
6146 .update_status = brightness_update_status,
6149 /* --------------------------------------------------------------------- */
6152 * Call _BCL method of video device. On some ThinkPads this will
6153 * switch the firmware to the ACPI brightness control mode.
6156 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6158 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6159 union acpi_object *obj;
6160 int rc;
6162 if (ACPI_SUCCESS(acpi_evaluate_object(handle, "_BCL", NULL, &buffer))) {
6163 obj = (union acpi_object *)buffer.pointer;
6164 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6165 printk(TPACPI_ERR "Unknown _BCL data, "
6166 "please report this to %s\n", TPACPI_MAIL);
6167 rc = 0;
6168 } else {
6169 rc = obj->package.count;
6171 } else {
6172 return 0;
6175 kfree(buffer.pointer);
6176 return rc;
6181 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6183 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6185 acpi_handle video_device;
6186 int bcl_levels = 0;
6188 tpacpi_acpi_handle_locate("video", ACPI_VIDEO_HID, &video_device);
6189 if (video_device)
6190 bcl_levels = tpacpi_query_bcl_levels(video_device);
6192 tp_features.bright_acpimode = (bcl_levels > 0);
6194 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6198 * These are only useful for models that have only one possibility
6199 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6200 * these quirks.
6202 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6203 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6204 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6206 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6207 /* Models with ATI GPUs known to require ECNVRAM mode */
6208 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6210 /* Models with ATI GPUs that can use ECNVRAM */
6211 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6212 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6213 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6214 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6216 /* Models with Intel Extreme Graphics 2 */
6217 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6218 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6219 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6221 /* Models with Intel GMA900 */
6222 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6223 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6224 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6228 * Returns < 0 for error, otherwise sets tp_features.bright_*
6229 * and bright_maxlvl.
6231 static void __init tpacpi_detect_brightness_capabilities(void)
6233 unsigned int b;
6235 vdbg_printk(TPACPI_DBG_INIT,
6236 "detecting firmware brightness interface capabilities\n");
6238 /* we could run a quirks check here (same table used by
6239 * brightness_init) if needed */
6242 * We always attempt to detect acpi support, so as to switch
6243 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6244 * going to publish a backlight interface
6246 b = tpacpi_check_std_acpi_brightness_support();
6247 switch (b) {
6248 case 16:
6249 bright_maxlvl = 15;
6250 printk(TPACPI_INFO
6251 "detected a 16-level brightness capable ThinkPad\n");
6252 break;
6253 case 8:
6254 case 0:
6255 bright_maxlvl = 7;
6256 printk(TPACPI_INFO
6257 "detected a 8-level brightness capable ThinkPad\n");
6258 break;
6259 default:
6260 printk(TPACPI_ERR
6261 "Unsupported brightness interface, "
6262 "please contact %s\n", TPACPI_MAIL);
6263 tp_features.bright_unkfw = 1;
6264 bright_maxlvl = b - 1;
6268 static int __init brightness_init(struct ibm_init_struct *iibm)
6270 int b;
6271 unsigned long quirks;
6273 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6275 mutex_init(&brightness_mutex);
6277 quirks = tpacpi_check_quirks(brightness_quirk_table,
6278 ARRAY_SIZE(brightness_quirk_table));
6280 /* tpacpi_detect_brightness_capabilities() must have run already */
6282 /* if it is unknown, we don't handle it: it wouldn't be safe */
6283 if (tp_features.bright_unkfw)
6284 return 1;
6286 if (!brightness_enable) {
6287 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6288 "brightness support disabled by "
6289 "module parameter\n");
6290 return 1;
6293 if (acpi_video_backlight_support()) {
6294 if (brightness_enable > 1) {
6295 printk(TPACPI_INFO
6296 "Standard ACPI backlight interface "
6297 "available, not loading native one.\n");
6298 return 1;
6299 } else if (brightness_enable == 1) {
6300 printk(TPACPI_WARN
6301 "Cannot enable backlight brightness support, "
6302 "ACPI is already handling it. Refer to the "
6303 "acpi_backlight kernel parameter\n");
6304 return 1;
6306 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
6307 printk(TPACPI_NOTICE
6308 "Standard ACPI backlight interface not "
6309 "available, thinkpad_acpi native "
6310 "brightness control enabled\n");
6314 * Check for module parameter bogosity, note that we
6315 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6316 * able to detect "unspecified"
6318 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6319 return -EINVAL;
6321 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6322 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6323 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6324 if (quirks & TPACPI_BRGHT_Q_EC)
6325 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6326 else
6327 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6329 dbg_printk(TPACPI_DBG_BRGHT,
6330 "driver auto-selected brightness_mode=%d\n",
6331 brightness_mode);
6334 /* Safety */
6335 if (!tpacpi_is_ibm() &&
6336 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6337 brightness_mode == TPACPI_BRGHT_MODE_EC))
6338 return -EINVAL;
6340 if (tpacpi_brightness_get_raw(&b) < 0)
6341 return 1;
6343 ibm_backlight_device = backlight_device_register(
6344 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6345 &ibm_backlight_data);
6346 if (IS_ERR(ibm_backlight_device)) {
6347 int rc = PTR_ERR(ibm_backlight_device);
6348 ibm_backlight_device = NULL;
6349 printk(TPACPI_ERR "Could not register backlight device\n");
6350 return rc;
6352 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6353 "brightness is supported\n");
6355 if (quirks & TPACPI_BRGHT_Q_ASK) {
6356 printk(TPACPI_NOTICE
6357 "brightness: will use unverified default: "
6358 "brightness_mode=%d\n", brightness_mode);
6359 printk(TPACPI_NOTICE
6360 "brightness: please report to %s whether it works well "
6361 "or not on your ThinkPad\n", TPACPI_MAIL);
6364 ibm_backlight_device->props.max_brightness = bright_maxlvl;
6365 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6367 /* Added by mistake in early 2007. Probably useless, but it could
6368 * be working around some unknown firmware problem where the value
6369 * read at startup doesn't match the real hardware state... so leave
6370 * it in place just in case */
6371 backlight_update_status(ibm_backlight_device);
6373 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6374 "brightness: registering brightness hotkeys "
6375 "as change notification\n");
6376 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6377 | TP_ACPI_HKEY_BRGHTUP_MASK
6378 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6379 return 0;
6382 static void brightness_suspend(pm_message_t state)
6384 tpacpi_brightness_checkpoint_nvram();
6387 static void brightness_shutdown(void)
6389 tpacpi_brightness_checkpoint_nvram();
6392 static void brightness_exit(void)
6394 if (ibm_backlight_device) {
6395 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6396 "calling backlight_device_unregister()\n");
6397 backlight_device_unregister(ibm_backlight_device);
6400 tpacpi_brightness_checkpoint_nvram();
6403 static int brightness_read(struct seq_file *m)
6405 int level;
6407 level = brightness_get(NULL);
6408 if (level < 0) {
6409 seq_printf(m, "level:\t\tunreadable\n");
6410 } else {
6411 seq_printf(m, "level:\t\t%d\n", level);
6412 seq_printf(m, "commands:\tup, down\n");
6413 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6414 bright_maxlvl);
6417 return 0;
6420 static int brightness_write(char *buf)
6422 int level;
6423 int rc;
6424 char *cmd;
6426 level = brightness_get(NULL);
6427 if (level < 0)
6428 return level;
6430 while ((cmd = next_cmd(&buf))) {
6431 if (strlencmp(cmd, "up") == 0) {
6432 if (level < bright_maxlvl)
6433 level++;
6434 } else if (strlencmp(cmd, "down") == 0) {
6435 if (level > 0)
6436 level--;
6437 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6438 level >= 0 && level <= bright_maxlvl) {
6439 /* new level set */
6440 } else
6441 return -EINVAL;
6444 tpacpi_disclose_usertask("procfs brightness",
6445 "set level to %d\n", level);
6448 * Now we know what the final level should be, so we try to set it.
6449 * Doing it this way makes the syscall restartable in case of EINTR
6451 rc = brightness_set(level);
6452 if (!rc && ibm_backlight_device)
6453 backlight_force_update(ibm_backlight_device,
6454 BACKLIGHT_UPDATE_SYSFS);
6455 return (rc == -EINTR)? -ERESTARTSYS : rc;
6458 static struct ibm_struct brightness_driver_data = {
6459 .name = "brightness",
6460 .read = brightness_read,
6461 .write = brightness_write,
6462 .exit = brightness_exit,
6463 .suspend = brightness_suspend,
6464 .shutdown = brightness_shutdown,
6467 /*************************************************************************
6468 * Volume subdriver
6472 * IBM ThinkPads have a simple volume controller with MUTE gating.
6473 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6475 * Since the *61 series (and probably also the later *60 series), Lenovo
6476 * ThinkPads only implement the MUTE gate.
6478 * EC register 0x30
6479 * Bit 6: MUTE (1 mutes sound)
6480 * Bit 3-0: Volume
6481 * Other bits should be zero as far as we know.
6483 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6484 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6485 * such as bit 7 which is used to detect repeated presses of MUTE,
6486 * and we leave them unchanged.
6489 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6491 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6492 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6493 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6495 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6496 static char *alsa_id = "ThinkPadEC";
6497 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6499 struct tpacpi_alsa_data {
6500 struct snd_card *card;
6501 struct snd_ctl_elem_id *ctl_mute_id;
6502 struct snd_ctl_elem_id *ctl_vol_id;
6505 static struct snd_card *alsa_card;
6507 enum {
6508 TP_EC_AUDIO = 0x30,
6510 /* TP_EC_AUDIO bits */
6511 TP_EC_AUDIO_MUTESW = 6,
6513 /* TP_EC_AUDIO bitmasks */
6514 TP_EC_AUDIO_LVL_MSK = 0x0F,
6515 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6517 /* Maximum volume */
6518 TP_EC_VOLUME_MAX = 14,
6521 enum tpacpi_volume_access_mode {
6522 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6523 TPACPI_VOL_MODE_EC, /* Pure EC control */
6524 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6525 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6526 TPACPI_VOL_MODE_MAX
6529 enum tpacpi_volume_capabilities {
6530 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6531 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6532 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6533 TPACPI_VOL_CAP_MAX
6536 static enum tpacpi_volume_access_mode volume_mode =
6537 TPACPI_VOL_MODE_MAX;
6539 static enum tpacpi_volume_capabilities volume_capabilities;
6540 static int volume_control_allowed;
6543 * Used to syncronize writers to TP_EC_AUDIO and
6544 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6546 static struct mutex volume_mutex;
6548 static void tpacpi_volume_checkpoint_nvram(void)
6550 u8 lec = 0;
6551 u8 b_nvram;
6552 u8 ec_mask;
6554 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6555 return;
6556 if (!volume_control_allowed)
6557 return;
6559 vdbg_printk(TPACPI_DBG_MIXER,
6560 "trying to checkpoint mixer state to NVRAM...\n");
6562 if (tp_features.mixer_no_level_control)
6563 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6564 else
6565 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6567 if (mutex_lock_killable(&volume_mutex) < 0)
6568 return;
6570 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6571 goto unlock;
6572 lec &= ec_mask;
6573 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6575 if (lec != (b_nvram & ec_mask)) {
6576 /* NVRAM needs update */
6577 b_nvram &= ~ec_mask;
6578 b_nvram |= lec;
6579 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6580 dbg_printk(TPACPI_DBG_MIXER,
6581 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6582 (unsigned int) lec, (unsigned int) b_nvram);
6583 } else {
6584 vdbg_printk(TPACPI_DBG_MIXER,
6585 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6586 (unsigned int) lec, (unsigned int) b_nvram);
6589 unlock:
6590 mutex_unlock(&volume_mutex);
6593 static int volume_get_status_ec(u8 *status)
6595 u8 s;
6597 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6598 return -EIO;
6600 *status = s;
6602 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6604 return 0;
6607 static int volume_get_status(u8 *status)
6609 return volume_get_status_ec(status);
6612 static int volume_set_status_ec(const u8 status)
6614 if (!acpi_ec_write(TP_EC_AUDIO, status))
6615 return -EIO;
6617 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6619 return 0;
6622 static int volume_set_status(const u8 status)
6624 return volume_set_status_ec(status);
6627 /* returns < 0 on error, 0 on no change, 1 on change */
6628 static int __volume_set_mute_ec(const bool mute)
6630 int rc;
6631 u8 s, n;
6633 if (mutex_lock_killable(&volume_mutex) < 0)
6634 return -EINTR;
6636 rc = volume_get_status_ec(&s);
6637 if (rc)
6638 goto unlock;
6640 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6641 s & ~TP_EC_AUDIO_MUTESW_MSK;
6643 if (n != s) {
6644 rc = volume_set_status_ec(n);
6645 if (!rc)
6646 rc = 1;
6649 unlock:
6650 mutex_unlock(&volume_mutex);
6651 return rc;
6654 static int volume_alsa_set_mute(const bool mute)
6656 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6657 (mute) ? "" : "un");
6658 return __volume_set_mute_ec(mute);
6661 static int volume_set_mute(const bool mute)
6663 int rc;
6665 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6666 (mute) ? "" : "un");
6668 rc = __volume_set_mute_ec(mute);
6669 return (rc < 0) ? rc : 0;
6672 /* returns < 0 on error, 0 on no change, 1 on change */
6673 static int __volume_set_volume_ec(const u8 vol)
6675 int rc;
6676 u8 s, n;
6678 if (vol > TP_EC_VOLUME_MAX)
6679 return -EINVAL;
6681 if (mutex_lock_killable(&volume_mutex) < 0)
6682 return -EINTR;
6684 rc = volume_get_status_ec(&s);
6685 if (rc)
6686 goto unlock;
6688 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6690 if (n != s) {
6691 rc = volume_set_status_ec(n);
6692 if (!rc)
6693 rc = 1;
6696 unlock:
6697 mutex_unlock(&volume_mutex);
6698 return rc;
6701 static int volume_alsa_set_volume(const u8 vol)
6703 dbg_printk(TPACPI_DBG_MIXER,
6704 "ALSA: trying to set volume level to %hu\n", vol);
6705 return __volume_set_volume_ec(vol);
6708 static void volume_alsa_notify_change(void)
6710 struct tpacpi_alsa_data *d;
6712 if (alsa_card && alsa_card->private_data) {
6713 d = alsa_card->private_data;
6714 if (d->ctl_mute_id)
6715 snd_ctl_notify(alsa_card,
6716 SNDRV_CTL_EVENT_MASK_VALUE,
6717 d->ctl_mute_id);
6718 if (d->ctl_vol_id)
6719 snd_ctl_notify(alsa_card,
6720 SNDRV_CTL_EVENT_MASK_VALUE,
6721 d->ctl_vol_id);
6725 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6726 struct snd_ctl_elem_info *uinfo)
6728 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6729 uinfo->count = 1;
6730 uinfo->value.integer.min = 0;
6731 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6732 return 0;
6735 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6736 struct snd_ctl_elem_value *ucontrol)
6738 u8 s;
6739 int rc;
6741 rc = volume_get_status(&s);
6742 if (rc < 0)
6743 return rc;
6745 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6746 return 0;
6749 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6750 struct snd_ctl_elem_value *ucontrol)
6752 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6753 ucontrol->value.integer.value[0]);
6754 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6757 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6759 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6760 struct snd_ctl_elem_value *ucontrol)
6762 u8 s;
6763 int rc;
6765 rc = volume_get_status(&s);
6766 if (rc < 0)
6767 return rc;
6769 ucontrol->value.integer.value[0] =
6770 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6771 return 0;
6774 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6775 struct snd_ctl_elem_value *ucontrol)
6777 tpacpi_disclose_usertask("ALSA", "%smute\n",
6778 ucontrol->value.integer.value[0] ?
6779 "un" : "");
6780 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6783 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6784 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6785 .name = "Console Playback Volume",
6786 .index = 0,
6787 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6788 .info = volume_alsa_vol_info,
6789 .get = volume_alsa_vol_get,
6792 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6793 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6794 .name = "Console Playback Switch",
6795 .index = 0,
6796 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6797 .info = volume_alsa_mute_info,
6798 .get = volume_alsa_mute_get,
6801 static void volume_suspend(pm_message_t state)
6803 tpacpi_volume_checkpoint_nvram();
6806 static void volume_resume(void)
6808 volume_alsa_notify_change();
6811 static void volume_shutdown(void)
6813 tpacpi_volume_checkpoint_nvram();
6816 static void volume_exit(void)
6818 if (alsa_card) {
6819 snd_card_free(alsa_card);
6820 alsa_card = NULL;
6823 tpacpi_volume_checkpoint_nvram();
6826 static int __init volume_create_alsa_mixer(void)
6828 struct snd_card *card;
6829 struct tpacpi_alsa_data *data;
6830 struct snd_kcontrol *ctl_vol;
6831 struct snd_kcontrol *ctl_mute;
6832 int rc;
6834 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6835 sizeof(struct tpacpi_alsa_data), &card);
6836 if (rc < 0 || !card) {
6837 printk(TPACPI_ERR
6838 "Failed to create ALSA card structures: %d\n", rc);
6839 return 1;
6842 BUG_ON(!card->private_data);
6843 data = card->private_data;
6844 data->card = card;
6846 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6847 sizeof(card->driver));
6848 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6849 sizeof(card->shortname));
6850 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6851 (thinkpad_id.ec_version_str) ?
6852 thinkpad_id.ec_version_str : "(unknown)");
6853 snprintf(card->longname, sizeof(card->longname),
6854 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6855 (thinkpad_id.ec_version_str) ?
6856 thinkpad_id.ec_version_str : "unknown");
6858 if (volume_control_allowed) {
6859 volume_alsa_control_vol.put = volume_alsa_vol_put;
6860 volume_alsa_control_vol.access =
6861 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6863 volume_alsa_control_mute.put = volume_alsa_mute_put;
6864 volume_alsa_control_mute.access =
6865 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6868 if (!tp_features.mixer_no_level_control) {
6869 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6870 rc = snd_ctl_add(card, ctl_vol);
6871 if (rc < 0) {
6872 printk(TPACPI_ERR
6873 "Failed to create ALSA volume control: %d\n",
6874 rc);
6875 goto err_exit;
6877 data->ctl_vol_id = &ctl_vol->id;
6880 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6881 rc = snd_ctl_add(card, ctl_mute);
6882 if (rc < 0) {
6883 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6884 rc);
6885 goto err_exit;
6887 data->ctl_mute_id = &ctl_mute->id;
6889 snd_card_set_dev(card, &tpacpi_pdev->dev);
6890 rc = snd_card_register(card);
6891 if (rc < 0) {
6892 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6893 goto err_exit;
6896 alsa_card = card;
6897 return 0;
6899 err_exit:
6900 snd_card_free(card);
6901 return 1;
6904 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6905 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6907 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6908 /* Whitelist volume level on all IBM by default */
6909 { .vendor = PCI_VENDOR_ID_IBM,
6910 .bios = TPACPI_MATCH_ANY,
6911 .ec = TPACPI_MATCH_ANY,
6912 .quirks = TPACPI_VOL_Q_LEVEL },
6914 /* Lenovo models with volume control (needs confirmation) */
6915 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6916 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6917 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6918 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6919 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6920 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6921 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6923 /* Whitelist mute-only on all Lenovo by default */
6924 { .vendor = PCI_VENDOR_ID_LENOVO,
6925 .bios = TPACPI_MATCH_ANY,
6926 .ec = TPACPI_MATCH_ANY,
6927 .quirks = TPACPI_VOL_Q_MUTEONLY }
6930 static int __init volume_init(struct ibm_init_struct *iibm)
6932 unsigned long quirks;
6933 int rc;
6935 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6937 mutex_init(&volume_mutex);
6940 * Check for module parameter bogosity, note that we
6941 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6942 * able to detect "unspecified"
6944 if (volume_mode > TPACPI_VOL_MODE_MAX)
6945 return -EINVAL;
6947 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6948 printk(TPACPI_ERR
6949 "UCMS step volume mode not implemented, "
6950 "please contact %s\n", TPACPI_MAIL);
6951 return 1;
6954 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6955 return -EINVAL;
6958 * The ALSA mixer is our primary interface.
6959 * When disabled, don't install the subdriver at all
6961 if (!alsa_enable) {
6962 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6963 "ALSA mixer disabled by parameter, "
6964 "not loading volume subdriver...\n");
6965 return 1;
6968 quirks = tpacpi_check_quirks(volume_quirk_table,
6969 ARRAY_SIZE(volume_quirk_table));
6971 switch (volume_capabilities) {
6972 case TPACPI_VOL_CAP_AUTO:
6973 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6974 tp_features.mixer_no_level_control = 1;
6975 else if (quirks & TPACPI_VOL_Q_LEVEL)
6976 tp_features.mixer_no_level_control = 0;
6977 else
6978 return 1; /* no mixer */
6979 break;
6980 case TPACPI_VOL_CAP_VOLMUTE:
6981 tp_features.mixer_no_level_control = 0;
6982 break;
6983 case TPACPI_VOL_CAP_MUTEONLY:
6984 tp_features.mixer_no_level_control = 1;
6985 break;
6986 default:
6987 return 1;
6990 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6991 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6992 "using user-supplied volume_capabilities=%d\n",
6993 volume_capabilities);
6995 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6996 volume_mode == TPACPI_VOL_MODE_MAX) {
6997 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6999 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7000 "driver auto-selected volume_mode=%d\n",
7001 volume_mode);
7002 } else {
7003 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7004 "using user-supplied volume_mode=%d\n",
7005 volume_mode);
7008 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7009 "mute is supported, volume control is %s\n",
7010 str_supported(!tp_features.mixer_no_level_control));
7012 rc = volume_create_alsa_mixer();
7013 if (rc) {
7014 printk(TPACPI_ERR
7015 "Could not create the ALSA mixer interface\n");
7016 return rc;
7019 printk(TPACPI_INFO
7020 "Console audio control enabled, mode: %s\n",
7021 (volume_control_allowed) ?
7022 "override (read/write)" :
7023 "monitor (read only)");
7025 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7026 "registering volume hotkeys as change notification\n");
7027 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7028 | TP_ACPI_HKEY_VOLUP_MASK
7029 | TP_ACPI_HKEY_VOLDWN_MASK
7030 | TP_ACPI_HKEY_MUTE_MASK);
7032 return 0;
7035 static int volume_read(struct seq_file *m)
7037 u8 status;
7039 if (volume_get_status(&status) < 0) {
7040 seq_printf(m, "level:\t\tunreadable\n");
7041 } else {
7042 if (tp_features.mixer_no_level_control)
7043 seq_printf(m, "level:\t\tunsupported\n");
7044 else
7045 seq_printf(m, "level:\t\t%d\n",
7046 status & TP_EC_AUDIO_LVL_MSK);
7048 seq_printf(m, "mute:\t\t%s\n",
7049 onoff(status, TP_EC_AUDIO_MUTESW));
7051 if (volume_control_allowed) {
7052 seq_printf(m, "commands:\tunmute, mute\n");
7053 if (!tp_features.mixer_no_level_control) {
7054 seq_printf(m,
7055 "commands:\tup, down\n");
7056 seq_printf(m,
7057 "commands:\tlevel <level>"
7058 " (<level> is 0-%d)\n",
7059 TP_EC_VOLUME_MAX);
7064 return 0;
7067 static int volume_write(char *buf)
7069 u8 s;
7070 u8 new_level, new_mute;
7071 int l;
7072 char *cmd;
7073 int rc;
7076 * We do allow volume control at driver startup, so that the
7077 * user can set initial state through the volume=... parameter hack.
7079 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7080 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7081 tp_warned.volume_ctrl_forbidden = 1;
7082 printk(TPACPI_NOTICE
7083 "Console audio control in monitor mode, "
7084 "changes are not allowed.\n");
7085 printk(TPACPI_NOTICE
7086 "Use the volume_control=1 module parameter "
7087 "to enable volume control\n");
7089 return -EPERM;
7092 rc = volume_get_status(&s);
7093 if (rc < 0)
7094 return rc;
7096 new_level = s & TP_EC_AUDIO_LVL_MSK;
7097 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7099 while ((cmd = next_cmd(&buf))) {
7100 if (!tp_features.mixer_no_level_control) {
7101 if (strlencmp(cmd, "up") == 0) {
7102 if (new_mute)
7103 new_mute = 0;
7104 else if (new_level < TP_EC_VOLUME_MAX)
7105 new_level++;
7106 continue;
7107 } else if (strlencmp(cmd, "down") == 0) {
7108 if (new_mute)
7109 new_mute = 0;
7110 else if (new_level > 0)
7111 new_level--;
7112 continue;
7113 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7114 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7115 new_level = l;
7116 continue;
7119 if (strlencmp(cmd, "mute") == 0)
7120 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7121 else if (strlencmp(cmd, "unmute") == 0)
7122 new_mute = 0;
7123 else
7124 return -EINVAL;
7127 if (tp_features.mixer_no_level_control) {
7128 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7129 new_mute ? "" : "un");
7130 rc = volume_set_mute(!!new_mute);
7131 } else {
7132 tpacpi_disclose_usertask("procfs volume",
7133 "%smute and set level to %d\n",
7134 new_mute ? "" : "un", new_level);
7135 rc = volume_set_status(new_mute | new_level);
7137 volume_alsa_notify_change();
7139 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7142 static struct ibm_struct volume_driver_data = {
7143 .name = "volume",
7144 .read = volume_read,
7145 .write = volume_write,
7146 .exit = volume_exit,
7147 .suspend = volume_suspend,
7148 .resume = volume_resume,
7149 .shutdown = volume_shutdown,
7152 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7154 #define alsa_card NULL
7156 static void inline volume_alsa_notify_change(void)
7160 static int __init volume_init(struct ibm_init_struct *iibm)
7162 printk(TPACPI_INFO
7163 "volume: disabled as there is no ALSA support in this kernel\n");
7165 return 1;
7168 static struct ibm_struct volume_driver_data = {
7169 .name = "volume",
7172 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7174 /*************************************************************************
7175 * Fan subdriver
7179 * FAN ACCESS MODES
7181 * TPACPI_FAN_RD_ACPI_GFAN:
7182 * ACPI GFAN method: returns fan level
7184 * see TPACPI_FAN_WR_ACPI_SFAN
7185 * EC 0x2f (HFSP) not available if GFAN exists
7187 * TPACPI_FAN_WR_ACPI_SFAN:
7188 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7190 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7191 * it for writing.
7193 * TPACPI_FAN_WR_TPEC:
7194 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7195 * Supported on almost all ThinkPads
7197 * Fan speed changes of any sort (including those caused by the
7198 * disengaged mode) are usually done slowly by the firmware as the
7199 * maximum amount of fan duty cycle change per second seems to be
7200 * limited.
7202 * Reading is not available if GFAN exists.
7203 * Writing is not available if SFAN exists.
7205 * Bits
7206 * 7 automatic mode engaged;
7207 * (default operation mode of the ThinkPad)
7208 * fan level is ignored in this mode.
7209 * 6 full speed mode (takes precedence over bit 7);
7210 * not available on all thinkpads. May disable
7211 * the tachometer while the fan controller ramps up
7212 * the speed (which can take up to a few *minutes*).
7213 * Speeds up fan to 100% duty-cycle, which is far above
7214 * the standard RPM levels. It is not impossible that
7215 * it could cause hardware damage.
7216 * 5-3 unused in some models. Extra bits for fan level
7217 * in others, but still useless as all values above
7218 * 7 map to the same speed as level 7 in these models.
7219 * 2-0 fan level (0..7 usually)
7220 * 0x00 = stop
7221 * 0x07 = max (set when temperatures critical)
7222 * Some ThinkPads may have other levels, see
7223 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7225 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7226 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7227 * does so, its initial value is meaningless (0x07).
7229 * For firmware bugs, refer to:
7230 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7232 * ----
7234 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7235 * Main fan tachometer reading (in RPM)
7237 * This register is present on all ThinkPads with a new-style EC, and
7238 * it is known not to be present on the A21m/e, and T22, as there is
7239 * something else in offset 0x84 according to the ACPI DSDT. Other
7240 * ThinkPads from this same time period (and earlier) probably lack the
7241 * tachometer as well.
7243 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7244 * was never fixed by IBM to report the EC firmware version string
7245 * probably support the tachometer (like the early X models), so
7246 * detecting it is quite hard. We need more data to know for sure.
7248 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7249 * might result.
7251 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7252 * mode.
7254 * For firmware bugs, refer to:
7255 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7257 * ----
7259 * ThinkPad EC register 0x31 bit 0 (only on select models)
7261 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7262 * show the speed of the main fan. When bit 0 of EC register 0x31
7263 * is one, the tachometer registers show the speed of the auxiliary
7264 * fan.
7266 * Fan control seems to affect both fans, regardless of the state
7267 * of this bit.
7269 * So far, only the firmware for the X60/X61 non-tablet versions
7270 * seem to support this (firmware TP-7M).
7272 * TPACPI_FAN_WR_ACPI_FANS:
7273 * ThinkPad X31, X40, X41. Not available in the X60.
7275 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7276 * high speed. ACPI DSDT seems to map these three speeds to levels
7277 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7278 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7280 * The speeds are stored on handles
7281 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7283 * There are three default speed sets, accessible as handles:
7284 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7286 * ACPI DSDT switches which set is in use depending on various
7287 * factors.
7289 * TPACPI_FAN_WR_TPEC is also available and should be used to
7290 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7291 * but the ACPI tables just mention level 7.
7294 enum { /* Fan control constants */
7295 fan_status_offset = 0x2f, /* EC register 0x2f */
7296 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7297 * 0x84 must be read before 0x85 */
7298 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7299 bit 0 selects which fan is active */
7301 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7302 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7304 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7307 enum fan_status_access_mode {
7308 TPACPI_FAN_NONE = 0, /* No fan status or control */
7309 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7310 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7313 enum fan_control_access_mode {
7314 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7315 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7316 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7317 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7320 enum fan_control_commands {
7321 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7322 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7323 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7324 * and also watchdog cmd */
7327 static int fan_control_allowed;
7329 static enum fan_status_access_mode fan_status_access_mode;
7330 static enum fan_control_access_mode fan_control_access_mode;
7331 static enum fan_control_commands fan_control_commands;
7333 static u8 fan_control_initial_status;
7334 static u8 fan_control_desired_level;
7335 static u8 fan_control_resume_level;
7336 static int fan_watchdog_maxinterval;
7338 static struct mutex fan_mutex;
7340 static void fan_watchdog_fire(struct work_struct *ignored);
7341 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7343 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7344 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7345 "\\FSPD", /* 600e/x, 770e, 770x */
7346 ); /* all others */
7347 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7348 "JFNS", /* 770x-JL */
7349 ); /* all others */
7352 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7353 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7354 * be in auto mode (0x80).
7356 * This is corrected by any write to HFSP either by the driver, or
7357 * by the firmware.
7359 * We assume 0x07 really means auto mode while this quirk is active,
7360 * as this is far more likely than the ThinkPad being in level 7,
7361 * which is only used by the firmware during thermal emergencies.
7363 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7364 * TP-70 (T43, R52), which are known to be buggy.
7367 static void fan_quirk1_setup(void)
7369 if (fan_control_initial_status == 0x07) {
7370 printk(TPACPI_NOTICE
7371 "fan_init: initial fan status is unknown, "
7372 "assuming it is in auto mode\n");
7373 tp_features.fan_ctrl_status_undef = 1;
7377 static void fan_quirk1_handle(u8 *fan_status)
7379 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7380 if (*fan_status != fan_control_initial_status) {
7381 /* something changed the HFSP regisnter since
7382 * driver init time, so it is not undefined
7383 * anymore */
7384 tp_features.fan_ctrl_status_undef = 0;
7385 } else {
7386 /* Return most likely status. In fact, it
7387 * might be the only possible status */
7388 *fan_status = TP_EC_FAN_AUTO;
7393 /* Select main fan on X60/X61, NOOP on others */
7394 static bool fan_select_fan1(void)
7396 if (tp_features.second_fan) {
7397 u8 val;
7399 if (ec_read(fan_select_offset, &val) < 0)
7400 return false;
7401 val &= 0xFEU;
7402 if (ec_write(fan_select_offset, val) < 0)
7403 return false;
7405 return true;
7408 /* Select secondary fan on X60/X61 */
7409 static bool fan_select_fan2(void)
7411 u8 val;
7413 if (!tp_features.second_fan)
7414 return false;
7416 if (ec_read(fan_select_offset, &val) < 0)
7417 return false;
7418 val |= 0x01U;
7419 if (ec_write(fan_select_offset, val) < 0)
7420 return false;
7422 return true;
7426 * Call with fan_mutex held
7428 static void fan_update_desired_level(u8 status)
7430 if ((status &
7431 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7432 if (status > 7)
7433 fan_control_desired_level = 7;
7434 else
7435 fan_control_desired_level = status;
7439 static int fan_get_status(u8 *status)
7441 u8 s;
7443 /* TODO:
7444 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7446 switch (fan_status_access_mode) {
7447 case TPACPI_FAN_RD_ACPI_GFAN:
7448 /* 570, 600e/x, 770e, 770x */
7450 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7451 return -EIO;
7453 if (likely(status))
7454 *status = s & 0x07;
7456 break;
7458 case TPACPI_FAN_RD_TPEC:
7459 /* all except 570, 600e/x, 770e, 770x */
7460 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7461 return -EIO;
7463 if (likely(status)) {
7464 *status = s;
7465 fan_quirk1_handle(status);
7468 break;
7470 default:
7471 return -ENXIO;
7474 return 0;
7477 static int fan_get_status_safe(u8 *status)
7479 int rc;
7480 u8 s;
7482 if (mutex_lock_killable(&fan_mutex))
7483 return -ERESTARTSYS;
7484 rc = fan_get_status(&s);
7485 if (!rc)
7486 fan_update_desired_level(s);
7487 mutex_unlock(&fan_mutex);
7489 if (status)
7490 *status = s;
7492 return rc;
7495 static int fan_get_speed(unsigned int *speed)
7497 u8 hi, lo;
7499 switch (fan_status_access_mode) {
7500 case TPACPI_FAN_RD_TPEC:
7501 /* all except 570, 600e/x, 770e, 770x */
7502 if (unlikely(!fan_select_fan1()))
7503 return -EIO;
7504 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7505 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7506 return -EIO;
7508 if (likely(speed))
7509 *speed = (hi << 8) | lo;
7511 break;
7513 default:
7514 return -ENXIO;
7517 return 0;
7520 static int fan2_get_speed(unsigned int *speed)
7522 u8 hi, lo;
7523 bool rc;
7525 switch (fan_status_access_mode) {
7526 case TPACPI_FAN_RD_TPEC:
7527 /* all except 570, 600e/x, 770e, 770x */
7528 if (unlikely(!fan_select_fan2()))
7529 return -EIO;
7530 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7531 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7532 fan_select_fan1(); /* play it safe */
7533 if (rc)
7534 return -EIO;
7536 if (likely(speed))
7537 *speed = (hi << 8) | lo;
7539 break;
7541 default:
7542 return -ENXIO;
7545 return 0;
7548 static int fan_set_level(int level)
7550 if (!fan_control_allowed)
7551 return -EPERM;
7553 switch (fan_control_access_mode) {
7554 case TPACPI_FAN_WR_ACPI_SFAN:
7555 if (level >= 0 && level <= 7) {
7556 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7557 return -EIO;
7558 } else
7559 return -EINVAL;
7560 break;
7562 case TPACPI_FAN_WR_ACPI_FANS:
7563 case TPACPI_FAN_WR_TPEC:
7564 if (!(level & TP_EC_FAN_AUTO) &&
7565 !(level & TP_EC_FAN_FULLSPEED) &&
7566 ((level < 0) || (level > 7)))
7567 return -EINVAL;
7569 /* safety net should the EC not support AUTO
7570 * or FULLSPEED mode bits and just ignore them */
7571 if (level & TP_EC_FAN_FULLSPEED)
7572 level |= 7; /* safety min speed 7 */
7573 else if (level & TP_EC_FAN_AUTO)
7574 level |= 4; /* safety min speed 4 */
7576 if (!acpi_ec_write(fan_status_offset, level))
7577 return -EIO;
7578 else
7579 tp_features.fan_ctrl_status_undef = 0;
7580 break;
7582 default:
7583 return -ENXIO;
7586 vdbg_printk(TPACPI_DBG_FAN,
7587 "fan control: set fan control register to 0x%02x\n", level);
7588 return 0;
7591 static int fan_set_level_safe(int level)
7593 int rc;
7595 if (!fan_control_allowed)
7596 return -EPERM;
7598 if (mutex_lock_killable(&fan_mutex))
7599 return -ERESTARTSYS;
7601 if (level == TPACPI_FAN_LAST_LEVEL)
7602 level = fan_control_desired_level;
7604 rc = fan_set_level(level);
7605 if (!rc)
7606 fan_update_desired_level(level);
7608 mutex_unlock(&fan_mutex);
7609 return rc;
7612 static int fan_set_enable(void)
7614 u8 s;
7615 int rc;
7617 if (!fan_control_allowed)
7618 return -EPERM;
7620 if (mutex_lock_killable(&fan_mutex))
7621 return -ERESTARTSYS;
7623 switch (fan_control_access_mode) {
7624 case TPACPI_FAN_WR_ACPI_FANS:
7625 case TPACPI_FAN_WR_TPEC:
7626 rc = fan_get_status(&s);
7627 if (rc < 0)
7628 break;
7630 /* Don't go out of emergency fan mode */
7631 if (s != 7) {
7632 s &= 0x07;
7633 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7636 if (!acpi_ec_write(fan_status_offset, s))
7637 rc = -EIO;
7638 else {
7639 tp_features.fan_ctrl_status_undef = 0;
7640 rc = 0;
7642 break;
7644 case TPACPI_FAN_WR_ACPI_SFAN:
7645 rc = fan_get_status(&s);
7646 if (rc < 0)
7647 break;
7649 s &= 0x07;
7651 /* Set fan to at least level 4 */
7652 s |= 4;
7654 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7655 rc = -EIO;
7656 else
7657 rc = 0;
7658 break;
7660 default:
7661 rc = -ENXIO;
7664 mutex_unlock(&fan_mutex);
7666 if (!rc)
7667 vdbg_printk(TPACPI_DBG_FAN,
7668 "fan control: set fan control register to 0x%02x\n",
7670 return rc;
7673 static int fan_set_disable(void)
7675 int rc;
7677 if (!fan_control_allowed)
7678 return -EPERM;
7680 if (mutex_lock_killable(&fan_mutex))
7681 return -ERESTARTSYS;
7683 rc = 0;
7684 switch (fan_control_access_mode) {
7685 case TPACPI_FAN_WR_ACPI_FANS:
7686 case TPACPI_FAN_WR_TPEC:
7687 if (!acpi_ec_write(fan_status_offset, 0x00))
7688 rc = -EIO;
7689 else {
7690 fan_control_desired_level = 0;
7691 tp_features.fan_ctrl_status_undef = 0;
7693 break;
7695 case TPACPI_FAN_WR_ACPI_SFAN:
7696 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7697 rc = -EIO;
7698 else
7699 fan_control_desired_level = 0;
7700 break;
7702 default:
7703 rc = -ENXIO;
7706 if (!rc)
7707 vdbg_printk(TPACPI_DBG_FAN,
7708 "fan control: set fan control register to 0\n");
7710 mutex_unlock(&fan_mutex);
7711 return rc;
7714 static int fan_set_speed(int speed)
7716 int rc;
7718 if (!fan_control_allowed)
7719 return -EPERM;
7721 if (mutex_lock_killable(&fan_mutex))
7722 return -ERESTARTSYS;
7724 rc = 0;
7725 switch (fan_control_access_mode) {
7726 case TPACPI_FAN_WR_ACPI_FANS:
7727 if (speed >= 0 && speed <= 65535) {
7728 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7729 speed, speed, speed))
7730 rc = -EIO;
7731 } else
7732 rc = -EINVAL;
7733 break;
7735 default:
7736 rc = -ENXIO;
7739 mutex_unlock(&fan_mutex);
7740 return rc;
7743 static void fan_watchdog_reset(void)
7745 static int fan_watchdog_active;
7747 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7748 return;
7750 if (fan_watchdog_active)
7751 cancel_delayed_work(&fan_watchdog_task);
7753 if (fan_watchdog_maxinterval > 0 &&
7754 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7755 fan_watchdog_active = 1;
7756 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7757 msecs_to_jiffies(fan_watchdog_maxinterval
7758 * 1000))) {
7759 printk(TPACPI_ERR
7760 "failed to queue the fan watchdog, "
7761 "watchdog will not trigger\n");
7763 } else
7764 fan_watchdog_active = 0;
7767 static void fan_watchdog_fire(struct work_struct *ignored)
7769 int rc;
7771 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7772 return;
7774 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7775 rc = fan_set_enable();
7776 if (rc < 0) {
7777 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7778 "will try again later...\n", -rc);
7779 /* reschedule for later */
7780 fan_watchdog_reset();
7785 * SYSFS fan layout: hwmon compatible (device)
7787 * pwm*_enable:
7788 * 0: "disengaged" mode
7789 * 1: manual mode
7790 * 2: native EC "auto" mode (recommended, hardware default)
7792 * pwm*: set speed in manual mode, ignored otherwise.
7793 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7794 * interpolation.
7796 * fan*_input: tachometer reading, RPM
7799 * SYSFS fan layout: extensions
7801 * fan_watchdog (driver):
7802 * fan watchdog interval in seconds, 0 disables (default), max 120
7805 /* sysfs fan pwm1_enable ----------------------------------------------- */
7806 static ssize_t fan_pwm1_enable_show(struct device *dev,
7807 struct device_attribute *attr,
7808 char *buf)
7810 int res, mode;
7811 u8 status;
7813 res = fan_get_status_safe(&status);
7814 if (res)
7815 return res;
7817 if (status & TP_EC_FAN_FULLSPEED) {
7818 mode = 0;
7819 } else if (status & TP_EC_FAN_AUTO) {
7820 mode = 2;
7821 } else
7822 mode = 1;
7824 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7827 static ssize_t fan_pwm1_enable_store(struct device *dev,
7828 struct device_attribute *attr,
7829 const char *buf, size_t count)
7831 unsigned long t;
7832 int res, level;
7834 if (parse_strtoul(buf, 2, &t))
7835 return -EINVAL;
7837 tpacpi_disclose_usertask("hwmon pwm1_enable",
7838 "set fan mode to %lu\n", t);
7840 switch (t) {
7841 case 0:
7842 level = TP_EC_FAN_FULLSPEED;
7843 break;
7844 case 1:
7845 level = TPACPI_FAN_LAST_LEVEL;
7846 break;
7847 case 2:
7848 level = TP_EC_FAN_AUTO;
7849 break;
7850 case 3:
7851 /* reserved for software-controlled auto mode */
7852 return -ENOSYS;
7853 default:
7854 return -EINVAL;
7857 res = fan_set_level_safe(level);
7858 if (res == -ENXIO)
7859 return -EINVAL;
7860 else if (res < 0)
7861 return res;
7863 fan_watchdog_reset();
7865 return count;
7868 static struct device_attribute dev_attr_fan_pwm1_enable =
7869 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7870 fan_pwm1_enable_show, fan_pwm1_enable_store);
7872 /* sysfs fan pwm1 ------------------------------------------------------ */
7873 static ssize_t fan_pwm1_show(struct device *dev,
7874 struct device_attribute *attr,
7875 char *buf)
7877 int res;
7878 u8 status;
7880 res = fan_get_status_safe(&status);
7881 if (res)
7882 return res;
7884 if ((status &
7885 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7886 status = fan_control_desired_level;
7888 if (status > 7)
7889 status = 7;
7891 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7894 static ssize_t fan_pwm1_store(struct device *dev,
7895 struct device_attribute *attr,
7896 const char *buf, size_t count)
7898 unsigned long s;
7899 int rc;
7900 u8 status, newlevel;
7902 if (parse_strtoul(buf, 255, &s))
7903 return -EINVAL;
7905 tpacpi_disclose_usertask("hwmon pwm1",
7906 "set fan speed to %lu\n", s);
7908 /* scale down from 0-255 to 0-7 */
7909 newlevel = (s >> 5) & 0x07;
7911 if (mutex_lock_killable(&fan_mutex))
7912 return -ERESTARTSYS;
7914 rc = fan_get_status(&status);
7915 if (!rc && (status &
7916 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7917 rc = fan_set_level(newlevel);
7918 if (rc == -ENXIO)
7919 rc = -EINVAL;
7920 else if (!rc) {
7921 fan_update_desired_level(newlevel);
7922 fan_watchdog_reset();
7926 mutex_unlock(&fan_mutex);
7927 return (rc)? rc : count;
7930 static struct device_attribute dev_attr_fan_pwm1 =
7931 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7932 fan_pwm1_show, fan_pwm1_store);
7934 /* sysfs fan fan1_input ------------------------------------------------ */
7935 static ssize_t fan_fan1_input_show(struct device *dev,
7936 struct device_attribute *attr,
7937 char *buf)
7939 int res;
7940 unsigned int speed;
7942 res = fan_get_speed(&speed);
7943 if (res < 0)
7944 return res;
7946 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7949 static struct device_attribute dev_attr_fan_fan1_input =
7950 __ATTR(fan1_input, S_IRUGO,
7951 fan_fan1_input_show, NULL);
7953 /* sysfs fan fan2_input ------------------------------------------------ */
7954 static ssize_t fan_fan2_input_show(struct device *dev,
7955 struct device_attribute *attr,
7956 char *buf)
7958 int res;
7959 unsigned int speed;
7961 res = fan2_get_speed(&speed);
7962 if (res < 0)
7963 return res;
7965 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7968 static struct device_attribute dev_attr_fan_fan2_input =
7969 __ATTR(fan2_input, S_IRUGO,
7970 fan_fan2_input_show, NULL);
7972 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7973 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7974 char *buf)
7976 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7979 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7980 const char *buf, size_t count)
7982 unsigned long t;
7984 if (parse_strtoul(buf, 120, &t))
7985 return -EINVAL;
7987 if (!fan_control_allowed)
7988 return -EPERM;
7990 fan_watchdog_maxinterval = t;
7991 fan_watchdog_reset();
7993 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7995 return count;
7998 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7999 fan_fan_watchdog_show, fan_fan_watchdog_store);
8001 /* --------------------------------------------------------------------- */
8002 static struct attribute *fan_attributes[] = {
8003 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
8004 &dev_attr_fan_fan1_input.attr,
8005 NULL, /* for fan2_input */
8006 NULL
8009 static const struct attribute_group fan_attr_group = {
8010 .attrs = fan_attributes,
8013 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8014 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8016 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8017 { .vendor = PCI_VENDOR_ID_IBM, \
8018 .bios = TPACPI_MATCH_ANY, \
8019 .ec = TPID(__id1, __id2), \
8020 .quirks = __quirks }
8022 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8023 { .vendor = PCI_VENDOR_ID_LENOVO, \
8024 .bios = TPACPI_MATCH_ANY, \
8025 .ec = TPID(__id1, __id2), \
8026 .quirks = __quirks }
8028 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8029 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8030 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8031 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8032 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8033 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8036 #undef TPACPI_FAN_QL
8037 #undef TPACPI_FAN_QI
8039 static int __init fan_init(struct ibm_init_struct *iibm)
8041 int rc;
8042 unsigned long quirks;
8044 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8045 "initializing fan subdriver\n");
8047 mutex_init(&fan_mutex);
8048 fan_status_access_mode = TPACPI_FAN_NONE;
8049 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8050 fan_control_commands = 0;
8051 fan_watchdog_maxinterval = 0;
8052 tp_features.fan_ctrl_status_undef = 0;
8053 tp_features.second_fan = 0;
8054 fan_control_desired_level = 7;
8056 if (tpacpi_is_ibm()) {
8057 TPACPI_ACPIHANDLE_INIT(fans);
8058 TPACPI_ACPIHANDLE_INIT(gfan);
8059 TPACPI_ACPIHANDLE_INIT(sfan);
8062 quirks = tpacpi_check_quirks(fan_quirk_table,
8063 ARRAY_SIZE(fan_quirk_table));
8065 if (gfan_handle) {
8066 /* 570, 600e/x, 770e, 770x */
8067 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8068 } else {
8069 /* all other ThinkPads: note that even old-style
8070 * ThinkPad ECs supports the fan control register */
8071 if (likely(acpi_ec_read(fan_status_offset,
8072 &fan_control_initial_status))) {
8073 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8074 if (quirks & TPACPI_FAN_Q1)
8075 fan_quirk1_setup();
8076 if (quirks & TPACPI_FAN_2FAN) {
8077 tp_features.second_fan = 1;
8078 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8079 "secondary fan support enabled\n");
8081 } else {
8082 printk(TPACPI_ERR
8083 "ThinkPad ACPI EC access misbehaving, "
8084 "fan status and control unavailable\n");
8085 return 1;
8089 if (sfan_handle) {
8090 /* 570, 770x-JL */
8091 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8092 fan_control_commands |=
8093 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8094 } else {
8095 if (!gfan_handle) {
8096 /* gfan without sfan means no fan control */
8097 /* all other models implement TP EC 0x2f control */
8099 if (fans_handle) {
8100 /* X31, X40, X41 */
8101 fan_control_access_mode =
8102 TPACPI_FAN_WR_ACPI_FANS;
8103 fan_control_commands |=
8104 TPACPI_FAN_CMD_SPEED |
8105 TPACPI_FAN_CMD_LEVEL |
8106 TPACPI_FAN_CMD_ENABLE;
8107 } else {
8108 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8109 fan_control_commands |=
8110 TPACPI_FAN_CMD_LEVEL |
8111 TPACPI_FAN_CMD_ENABLE;
8116 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8117 "fan is %s, modes %d, %d\n",
8118 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8119 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8120 fan_status_access_mode, fan_control_access_mode);
8122 /* fan control master switch */
8123 if (!fan_control_allowed) {
8124 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8125 fan_control_commands = 0;
8126 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8127 "fan control features disabled by parameter\n");
8130 /* update fan_control_desired_level */
8131 if (fan_status_access_mode != TPACPI_FAN_NONE)
8132 fan_get_status_safe(NULL);
8134 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8135 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8136 if (tp_features.second_fan) {
8137 /* attach second fan tachometer */
8138 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8139 &dev_attr_fan_fan2_input.attr;
8141 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8142 &fan_attr_group);
8143 if (rc < 0)
8144 return rc;
8146 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8147 &driver_attr_fan_watchdog);
8148 if (rc < 0) {
8149 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8150 &fan_attr_group);
8151 return rc;
8153 return 0;
8154 } else
8155 return 1;
8158 static void fan_exit(void)
8160 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8161 "cancelling any pending fan watchdog tasks\n");
8163 /* FIXME: can we really do this unconditionally? */
8164 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8165 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8166 &driver_attr_fan_watchdog);
8168 cancel_delayed_work(&fan_watchdog_task);
8169 flush_workqueue(tpacpi_wq);
8172 static void fan_suspend(pm_message_t state)
8174 int rc;
8176 if (!fan_control_allowed)
8177 return;
8179 /* Store fan status in cache */
8180 fan_control_resume_level = 0;
8181 rc = fan_get_status_safe(&fan_control_resume_level);
8182 if (rc < 0)
8183 printk(TPACPI_NOTICE
8184 "failed to read fan level for later "
8185 "restore during resume: %d\n", rc);
8187 /* if it is undefined, don't attempt to restore it.
8188 * KEEP THIS LAST */
8189 if (tp_features.fan_ctrl_status_undef)
8190 fan_control_resume_level = 0;
8193 static void fan_resume(void)
8195 u8 current_level = 7;
8196 bool do_set = false;
8197 int rc;
8199 /* DSDT *always* updates status on resume */
8200 tp_features.fan_ctrl_status_undef = 0;
8202 if (!fan_control_allowed ||
8203 !fan_control_resume_level ||
8204 (fan_get_status_safe(&current_level) < 0))
8205 return;
8207 switch (fan_control_access_mode) {
8208 case TPACPI_FAN_WR_ACPI_SFAN:
8209 /* never decrease fan level */
8210 do_set = (fan_control_resume_level > current_level);
8211 break;
8212 case TPACPI_FAN_WR_ACPI_FANS:
8213 case TPACPI_FAN_WR_TPEC:
8214 /* never decrease fan level, scale is:
8215 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8217 * We expect the firmware to set either 7 or AUTO, but we
8218 * handle FULLSPEED out of paranoia.
8220 * So, we can safely only restore FULLSPEED or 7, anything
8221 * else could slow the fan. Restoring AUTO is useless, at
8222 * best that's exactly what the DSDT already set (it is the
8223 * slower it uses).
8225 * Always keep in mind that the DSDT *will* have set the
8226 * fans to what the vendor supposes is the best level. We
8227 * muck with it only to speed the fan up.
8229 if (fan_control_resume_level != 7 &&
8230 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8231 return;
8232 else
8233 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8234 (current_level != fan_control_resume_level);
8235 break;
8236 default:
8237 return;
8239 if (do_set) {
8240 printk(TPACPI_NOTICE
8241 "restoring fan level to 0x%02x\n",
8242 fan_control_resume_level);
8243 rc = fan_set_level_safe(fan_control_resume_level);
8244 if (rc < 0)
8245 printk(TPACPI_NOTICE
8246 "failed to restore fan level: %d\n", rc);
8250 static int fan_read(struct seq_file *m)
8252 int rc;
8253 u8 status;
8254 unsigned int speed = 0;
8256 switch (fan_status_access_mode) {
8257 case TPACPI_FAN_RD_ACPI_GFAN:
8258 /* 570, 600e/x, 770e, 770x */
8259 rc = fan_get_status_safe(&status);
8260 if (rc < 0)
8261 return rc;
8263 seq_printf(m, "status:\t\t%s\n"
8264 "level:\t\t%d\n",
8265 (status != 0) ? "enabled" : "disabled", status);
8266 break;
8268 case TPACPI_FAN_RD_TPEC:
8269 /* all except 570, 600e/x, 770e, 770x */
8270 rc = fan_get_status_safe(&status);
8271 if (rc < 0)
8272 return rc;
8274 seq_printf(m, "status:\t\t%s\n",
8275 (status != 0) ? "enabled" : "disabled");
8277 rc = fan_get_speed(&speed);
8278 if (rc < 0)
8279 return rc;
8281 seq_printf(m, "speed:\t\t%d\n", speed);
8283 if (status & TP_EC_FAN_FULLSPEED)
8284 /* Disengaged mode takes precedence */
8285 seq_printf(m, "level:\t\tdisengaged\n");
8286 else if (status & TP_EC_FAN_AUTO)
8287 seq_printf(m, "level:\t\tauto\n");
8288 else
8289 seq_printf(m, "level:\t\t%d\n", status);
8290 break;
8292 case TPACPI_FAN_NONE:
8293 default:
8294 seq_printf(m, "status:\t\tnot supported\n");
8297 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8298 seq_printf(m, "commands:\tlevel <level>");
8300 switch (fan_control_access_mode) {
8301 case TPACPI_FAN_WR_ACPI_SFAN:
8302 seq_printf(m, " (<level> is 0-7)\n");
8303 break;
8305 default:
8306 seq_printf(m, " (<level> is 0-7, "
8307 "auto, disengaged, full-speed)\n");
8308 break;
8312 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8313 seq_printf(m, "commands:\tenable, disable\n"
8314 "commands:\twatchdog <timeout> (<timeout> "
8315 "is 0 (off), 1-120 (seconds))\n");
8317 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8318 seq_printf(m, "commands:\tspeed <speed>"
8319 " (<speed> is 0-65535)\n");
8321 return 0;
8324 static int fan_write_cmd_level(const char *cmd, int *rc)
8326 int level;
8328 if (strlencmp(cmd, "level auto") == 0)
8329 level = TP_EC_FAN_AUTO;
8330 else if ((strlencmp(cmd, "level disengaged") == 0) |
8331 (strlencmp(cmd, "level full-speed") == 0))
8332 level = TP_EC_FAN_FULLSPEED;
8333 else if (sscanf(cmd, "level %d", &level) != 1)
8334 return 0;
8336 *rc = fan_set_level_safe(level);
8337 if (*rc == -ENXIO)
8338 printk(TPACPI_ERR "level command accepted for unsupported "
8339 "access mode %d", fan_control_access_mode);
8340 else if (!*rc)
8341 tpacpi_disclose_usertask("procfs fan",
8342 "set level to %d\n", level);
8344 return 1;
8347 static int fan_write_cmd_enable(const char *cmd, int *rc)
8349 if (strlencmp(cmd, "enable") != 0)
8350 return 0;
8352 *rc = fan_set_enable();
8353 if (*rc == -ENXIO)
8354 printk(TPACPI_ERR "enable command accepted for unsupported "
8355 "access mode %d", fan_control_access_mode);
8356 else if (!*rc)
8357 tpacpi_disclose_usertask("procfs fan", "enable\n");
8359 return 1;
8362 static int fan_write_cmd_disable(const char *cmd, int *rc)
8364 if (strlencmp(cmd, "disable") != 0)
8365 return 0;
8367 *rc = fan_set_disable();
8368 if (*rc == -ENXIO)
8369 printk(TPACPI_ERR "disable command accepted for unsupported "
8370 "access mode %d", fan_control_access_mode);
8371 else if (!*rc)
8372 tpacpi_disclose_usertask("procfs fan", "disable\n");
8374 return 1;
8377 static int fan_write_cmd_speed(const char *cmd, int *rc)
8379 int speed;
8381 /* TODO:
8382 * Support speed <low> <medium> <high> ? */
8384 if (sscanf(cmd, "speed %d", &speed) != 1)
8385 return 0;
8387 *rc = fan_set_speed(speed);
8388 if (*rc == -ENXIO)
8389 printk(TPACPI_ERR "speed command accepted for unsupported "
8390 "access mode %d", fan_control_access_mode);
8391 else if (!*rc)
8392 tpacpi_disclose_usertask("procfs fan",
8393 "set speed to %d\n", speed);
8395 return 1;
8398 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8400 int interval;
8402 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8403 return 0;
8405 if (interval < 0 || interval > 120)
8406 *rc = -EINVAL;
8407 else {
8408 fan_watchdog_maxinterval = interval;
8409 tpacpi_disclose_usertask("procfs fan",
8410 "set watchdog timer to %d\n",
8411 interval);
8414 return 1;
8417 static int fan_write(char *buf)
8419 char *cmd;
8420 int rc = 0;
8422 while (!rc && (cmd = next_cmd(&buf))) {
8423 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8424 fan_write_cmd_level(cmd, &rc)) &&
8425 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8426 (fan_write_cmd_enable(cmd, &rc) ||
8427 fan_write_cmd_disable(cmd, &rc) ||
8428 fan_write_cmd_watchdog(cmd, &rc))) &&
8429 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8430 fan_write_cmd_speed(cmd, &rc))
8432 rc = -EINVAL;
8433 else if (!rc)
8434 fan_watchdog_reset();
8437 return rc;
8440 static struct ibm_struct fan_driver_data = {
8441 .name = "fan",
8442 .read = fan_read,
8443 .write = fan_write,
8444 .exit = fan_exit,
8445 .suspend = fan_suspend,
8446 .resume = fan_resume,
8449 /****************************************************************************
8450 ****************************************************************************
8452 * Infrastructure
8454 ****************************************************************************
8455 ****************************************************************************/
8458 * HKEY event callout for other subdrivers go here
8459 * (yes, it is ugly, but it is quick, safe, and gets the job done
8461 static void tpacpi_driver_event(const unsigned int hkey_event)
8463 if (ibm_backlight_device) {
8464 switch (hkey_event) {
8465 case TP_HKEY_EV_BRGHT_UP:
8466 case TP_HKEY_EV_BRGHT_DOWN:
8467 tpacpi_brightness_notify_change();
8470 if (alsa_card) {
8471 switch (hkey_event) {
8472 case TP_HKEY_EV_VOL_UP:
8473 case TP_HKEY_EV_VOL_DOWN:
8474 case TP_HKEY_EV_VOL_MUTE:
8475 volume_alsa_notify_change();
8480 static void hotkey_driver_event(const unsigned int scancode)
8482 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8485 /* sysfs name ---------------------------------------------------------- */
8486 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8487 struct device_attribute *attr,
8488 char *buf)
8490 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8493 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8494 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8496 /* --------------------------------------------------------------------- */
8498 /* /proc support */
8499 static struct proc_dir_entry *proc_dir;
8502 * Module and infrastructure proble, init and exit handling
8505 static int force_load;
8507 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8508 static const char * __init str_supported(int is_supported)
8510 static char text_unsupported[] __initdata = "not supported";
8512 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8514 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8516 static void ibm_exit(struct ibm_struct *ibm)
8518 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8520 list_del_init(&ibm->all_drivers);
8522 if (ibm->flags.acpi_notify_installed) {
8523 dbg_printk(TPACPI_DBG_EXIT,
8524 "%s: acpi_remove_notify_handler\n", ibm->name);
8525 BUG_ON(!ibm->acpi);
8526 acpi_remove_notify_handler(*ibm->acpi->handle,
8527 ibm->acpi->type,
8528 dispatch_acpi_notify);
8529 ibm->flags.acpi_notify_installed = 0;
8530 ibm->flags.acpi_notify_installed = 0;
8533 if (ibm->flags.proc_created) {
8534 dbg_printk(TPACPI_DBG_EXIT,
8535 "%s: remove_proc_entry\n", ibm->name);
8536 remove_proc_entry(ibm->name, proc_dir);
8537 ibm->flags.proc_created = 0;
8540 if (ibm->flags.acpi_driver_registered) {
8541 dbg_printk(TPACPI_DBG_EXIT,
8542 "%s: acpi_bus_unregister_driver\n", ibm->name);
8543 BUG_ON(!ibm->acpi);
8544 acpi_bus_unregister_driver(ibm->acpi->driver);
8545 kfree(ibm->acpi->driver);
8546 ibm->acpi->driver = NULL;
8547 ibm->flags.acpi_driver_registered = 0;
8550 if (ibm->flags.init_called && ibm->exit) {
8551 ibm->exit();
8552 ibm->flags.init_called = 0;
8555 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8558 static int __init ibm_init(struct ibm_init_struct *iibm)
8560 int ret;
8561 struct ibm_struct *ibm = iibm->data;
8562 struct proc_dir_entry *entry;
8564 BUG_ON(ibm == NULL);
8566 INIT_LIST_HEAD(&ibm->all_drivers);
8568 if (ibm->flags.experimental && !experimental)
8569 return 0;
8571 dbg_printk(TPACPI_DBG_INIT,
8572 "probing for %s\n", ibm->name);
8574 if (iibm->init) {
8575 ret = iibm->init(iibm);
8576 if (ret > 0)
8577 return 0; /* probe failed */
8578 if (ret)
8579 return ret;
8581 ibm->flags.init_called = 1;
8584 if (ibm->acpi) {
8585 if (ibm->acpi->hid) {
8586 ret = register_tpacpi_subdriver(ibm);
8587 if (ret)
8588 goto err_out;
8591 if (ibm->acpi->notify) {
8592 ret = setup_acpi_notify(ibm);
8593 if (ret == -ENODEV) {
8594 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8595 ibm->name);
8596 ret = 0;
8597 goto err_out;
8599 if (ret < 0)
8600 goto err_out;
8604 dbg_printk(TPACPI_DBG_INIT,
8605 "%s installed\n", ibm->name);
8607 if (ibm->read) {
8608 mode_t mode = iibm->base_procfs_mode;
8610 if (!mode)
8611 mode = S_IRUGO;
8612 if (ibm->write)
8613 mode |= S_IWUSR;
8614 entry = proc_create_data(ibm->name, mode, proc_dir,
8615 &dispatch_proc_fops, ibm);
8616 if (!entry) {
8617 printk(TPACPI_ERR "unable to create proc entry %s\n",
8618 ibm->name);
8619 ret = -ENODEV;
8620 goto err_out;
8622 ibm->flags.proc_created = 1;
8625 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8627 return 0;
8629 err_out:
8630 dbg_printk(TPACPI_DBG_INIT,
8631 "%s: at error exit path with result %d\n",
8632 ibm->name, ret);
8634 ibm_exit(ibm);
8635 return (ret < 0)? ret : 0;
8638 /* Probing */
8640 static bool __pure __init tpacpi_is_fw_digit(const char c)
8642 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8645 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8646 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8647 const char t)
8649 return s && strlen(s) >= 8 &&
8650 tpacpi_is_fw_digit(s[0]) &&
8651 tpacpi_is_fw_digit(s[1]) &&
8652 s[2] == t && s[3] == 'T' &&
8653 tpacpi_is_fw_digit(s[4]) &&
8654 tpacpi_is_fw_digit(s[5]) &&
8655 s[6] == 'W' && s[7] == 'W';
8658 /* returns 0 - probe ok, or < 0 - probe error.
8659 * Probe ok doesn't mean thinkpad found.
8660 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8661 static int __must_check __init get_thinkpad_model_data(
8662 struct thinkpad_id_data *tp)
8664 const struct dmi_device *dev = NULL;
8665 char ec_fw_string[18];
8666 char const *s;
8668 if (!tp)
8669 return -EINVAL;
8671 memset(tp, 0, sizeof(*tp));
8673 if (dmi_name_in_vendors("IBM"))
8674 tp->vendor = PCI_VENDOR_ID_IBM;
8675 else if (dmi_name_in_vendors("LENOVO"))
8676 tp->vendor = PCI_VENDOR_ID_LENOVO;
8677 else
8678 return 0;
8680 s = dmi_get_system_info(DMI_BIOS_VERSION);
8681 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8682 if (s && !tp->bios_version_str)
8683 return -ENOMEM;
8685 /* Really ancient ThinkPad 240X will fail this, which is fine */
8686 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8687 return 0;
8689 tp->bios_model = tp->bios_version_str[0]
8690 | (tp->bios_version_str[1] << 8);
8691 tp->bios_release = (tp->bios_version_str[4] << 8)
8692 | tp->bios_version_str[5];
8695 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8696 * X32 or newer, all Z series; Some models must have an
8697 * up-to-date BIOS or they will not be detected.
8699 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8701 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8702 if (sscanf(dev->name,
8703 "IBM ThinkPad Embedded Controller -[%17c",
8704 ec_fw_string) == 1) {
8705 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8706 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8708 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8709 if (!tp->ec_version_str)
8710 return -ENOMEM;
8712 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8713 tp->ec_model = ec_fw_string[0]
8714 | (ec_fw_string[1] << 8);
8715 tp->ec_release = (ec_fw_string[4] << 8)
8716 | ec_fw_string[5];
8717 } else {
8718 printk(TPACPI_NOTICE
8719 "ThinkPad firmware release %s "
8720 "doesn't match the known patterns\n",
8721 ec_fw_string);
8722 printk(TPACPI_NOTICE
8723 "please report this to %s\n",
8724 TPACPI_MAIL);
8726 break;
8730 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8731 if (s && !strnicmp(s, "ThinkPad", 8)) {
8732 tp->model_str = kstrdup(s, GFP_KERNEL);
8733 if (!tp->model_str)
8734 return -ENOMEM;
8737 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8738 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8739 if (s && !tp->nummodel_str)
8740 return -ENOMEM;
8742 return 0;
8745 static int __init probe_for_thinkpad(void)
8747 int is_thinkpad;
8749 if (acpi_disabled)
8750 return -ENODEV;
8752 /* It would be dangerous to run the driver in this case */
8753 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8754 return -ENODEV;
8757 * Non-ancient models have better DMI tagging, but very old models
8758 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8760 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8761 (thinkpad_id.ec_model != 0) ||
8762 tpacpi_is_fw_known();
8764 /* The EC handler is required */
8765 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
8766 if (!ec_handle) {
8767 if (is_thinkpad)
8768 printk(TPACPI_ERR
8769 "Not yet supported ThinkPad detected!\n");
8770 return -ENODEV;
8773 if (!is_thinkpad && !force_load)
8774 return -ENODEV;
8776 return 0;
8779 static void __init thinkpad_acpi_init_banner(void)
8781 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8782 printk(TPACPI_INFO "%s\n", TPACPI_URL);
8784 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
8785 (thinkpad_id.bios_version_str) ?
8786 thinkpad_id.bios_version_str : "unknown",
8787 (thinkpad_id.ec_version_str) ?
8788 thinkpad_id.ec_version_str : "unknown");
8790 BUG_ON(!thinkpad_id.vendor);
8792 if (thinkpad_id.model_str)
8793 printk(TPACPI_INFO "%s %s, model %s\n",
8794 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8795 "IBM" : ((thinkpad_id.vendor ==
8796 PCI_VENDOR_ID_LENOVO) ?
8797 "Lenovo" : "Unknown vendor"),
8798 thinkpad_id.model_str,
8799 (thinkpad_id.nummodel_str) ?
8800 thinkpad_id.nummodel_str : "unknown");
8803 /* Module init, exit, parameters */
8805 static struct ibm_init_struct ibms_init[] __initdata = {
8807 .data = &thinkpad_acpi_driver_data,
8810 .init = hotkey_init,
8811 .data = &hotkey_driver_data,
8814 .init = bluetooth_init,
8815 .data = &bluetooth_driver_data,
8818 .init = wan_init,
8819 .data = &wan_driver_data,
8822 .init = uwb_init,
8823 .data = &uwb_driver_data,
8825 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8827 .init = video_init,
8828 .base_procfs_mode = S_IRUSR,
8829 .data = &video_driver_data,
8831 #endif
8833 .init = light_init,
8834 .data = &light_driver_data,
8837 .init = cmos_init,
8838 .data = &cmos_driver_data,
8841 .init = led_init,
8842 .data = &led_driver_data,
8845 .init = beep_init,
8846 .data = &beep_driver_data,
8849 .init = thermal_init,
8850 .data = &thermal_driver_data,
8853 .data = &ecdump_driver_data,
8856 .init = brightness_init,
8857 .data = &brightness_driver_data,
8860 .init = volume_init,
8861 .data = &volume_driver_data,
8864 .init = fan_init,
8865 .data = &fan_driver_data,
8869 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8871 unsigned int i;
8872 struct ibm_struct *ibm;
8874 if (!kp || !kp->name || !val)
8875 return -EINVAL;
8877 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8878 ibm = ibms_init[i].data;
8879 WARN_ON(ibm == NULL);
8881 if (!ibm || !ibm->name)
8882 continue;
8884 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8885 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8886 return -ENOSPC;
8887 strcpy(ibms_init[i].param, val);
8888 strcat(ibms_init[i].param, ",");
8889 return 0;
8893 return -EINVAL;
8896 module_param(experimental, int, 0444);
8897 MODULE_PARM_DESC(experimental,
8898 "Enables experimental features when non-zero");
8900 module_param_named(debug, dbg_level, uint, 0);
8901 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8903 module_param(force_load, bool, 0444);
8904 MODULE_PARM_DESC(force_load,
8905 "Attempts to load the driver even on a "
8906 "mis-identified ThinkPad when true");
8908 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8909 MODULE_PARM_DESC(fan_control,
8910 "Enables setting fan parameters features when true");
8912 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8913 MODULE_PARM_DESC(brightness_mode,
8914 "Selects brightness control strategy: "
8915 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8917 module_param(brightness_enable, uint, 0444);
8918 MODULE_PARM_DESC(brightness_enable,
8919 "Enables backlight control when 1, disables when 0");
8921 module_param(hotkey_report_mode, uint, 0444);
8922 MODULE_PARM_DESC(hotkey_report_mode,
8923 "used for backwards compatibility with userspace, "
8924 "see documentation");
8926 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8927 module_param_named(volume_mode, volume_mode, uint, 0444);
8928 MODULE_PARM_DESC(volume_mode,
8929 "Selects volume control strategy: "
8930 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8932 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8933 MODULE_PARM_DESC(volume_capabilities,
8934 "Selects the mixer capabilites: "
8935 "0=auto, 1=volume and mute, 2=mute only");
8937 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8938 MODULE_PARM_DESC(volume_control,
8939 "Enables software override for the console audio "
8940 "control when true");
8942 /* ALSA module API parameters */
8943 module_param_named(index, alsa_index, int, 0444);
8944 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8945 module_param_named(id, alsa_id, charp, 0444);
8946 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8947 module_param_named(enable, alsa_enable, bool, 0444);
8948 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8949 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8951 #define TPACPI_PARAM(feature) \
8952 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8953 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8954 "at module load, see documentation")
8956 TPACPI_PARAM(hotkey);
8957 TPACPI_PARAM(bluetooth);
8958 TPACPI_PARAM(video);
8959 TPACPI_PARAM(light);
8960 TPACPI_PARAM(cmos);
8961 TPACPI_PARAM(led);
8962 TPACPI_PARAM(beep);
8963 TPACPI_PARAM(ecdump);
8964 TPACPI_PARAM(brightness);
8965 TPACPI_PARAM(volume);
8966 TPACPI_PARAM(fan);
8968 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8969 module_param(dbg_wlswemul, uint, 0444);
8970 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8971 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8972 MODULE_PARM_DESC(wlsw_state,
8973 "Initial state of the emulated WLSW switch");
8975 module_param(dbg_bluetoothemul, uint, 0444);
8976 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8977 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8978 MODULE_PARM_DESC(bluetooth_state,
8979 "Initial state of the emulated bluetooth switch");
8981 module_param(dbg_wwanemul, uint, 0444);
8982 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8983 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8984 MODULE_PARM_DESC(wwan_state,
8985 "Initial state of the emulated WWAN switch");
8987 module_param(dbg_uwbemul, uint, 0444);
8988 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8989 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8990 MODULE_PARM_DESC(uwb_state,
8991 "Initial state of the emulated UWB switch");
8992 #endif
8994 static void thinkpad_acpi_module_exit(void)
8996 struct ibm_struct *ibm, *itmp;
8998 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9000 list_for_each_entry_safe_reverse(ibm, itmp,
9001 &tpacpi_all_drivers,
9002 all_drivers) {
9003 ibm_exit(ibm);
9006 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9008 if (tpacpi_inputdev) {
9009 if (tp_features.input_device_registered)
9010 input_unregister_device(tpacpi_inputdev);
9011 else
9012 input_free_device(tpacpi_inputdev);
9015 if (tpacpi_hwmon)
9016 hwmon_device_unregister(tpacpi_hwmon);
9018 if (tp_features.sensors_pdev_attrs_registered)
9019 device_remove_file(&tpacpi_sensors_pdev->dev,
9020 &dev_attr_thinkpad_acpi_pdev_name);
9021 if (tpacpi_sensors_pdev)
9022 platform_device_unregister(tpacpi_sensors_pdev);
9023 if (tpacpi_pdev)
9024 platform_device_unregister(tpacpi_pdev);
9026 if (tp_features.sensors_pdrv_attrs_registered)
9027 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9028 if (tp_features.platform_drv_attrs_registered)
9029 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9031 if (tp_features.sensors_pdrv_registered)
9032 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9034 if (tp_features.platform_drv_registered)
9035 platform_driver_unregister(&tpacpi_pdriver);
9037 if (proc_dir)
9038 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9040 if (tpacpi_wq)
9041 destroy_workqueue(tpacpi_wq);
9043 kfree(thinkpad_id.bios_version_str);
9044 kfree(thinkpad_id.ec_version_str);
9045 kfree(thinkpad_id.model_str);
9049 static int __init thinkpad_acpi_module_init(void)
9051 int ret, i;
9053 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9055 /* Parameter checking */
9056 if (hotkey_report_mode > 2)
9057 return -EINVAL;
9059 /* Driver-level probe */
9061 ret = get_thinkpad_model_data(&thinkpad_id);
9062 if (ret) {
9063 printk(TPACPI_ERR
9064 "unable to get DMI data: %d\n", ret);
9065 thinkpad_acpi_module_exit();
9066 return ret;
9068 ret = probe_for_thinkpad();
9069 if (ret) {
9070 thinkpad_acpi_module_exit();
9071 return ret;
9074 /* Driver initialization */
9076 thinkpad_acpi_init_banner();
9077 tpacpi_check_outdated_fw();
9079 TPACPI_ACPIHANDLE_INIT(ecrd);
9080 TPACPI_ACPIHANDLE_INIT(ecwr);
9082 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9083 if (!tpacpi_wq) {
9084 thinkpad_acpi_module_exit();
9085 return -ENOMEM;
9088 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9089 if (!proc_dir) {
9090 printk(TPACPI_ERR
9091 "unable to create proc dir " TPACPI_PROC_DIR);
9092 thinkpad_acpi_module_exit();
9093 return -ENODEV;
9096 ret = platform_driver_register(&tpacpi_pdriver);
9097 if (ret) {
9098 printk(TPACPI_ERR
9099 "unable to register main platform driver\n");
9100 thinkpad_acpi_module_exit();
9101 return ret;
9103 tp_features.platform_drv_registered = 1;
9105 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9106 if (ret) {
9107 printk(TPACPI_ERR
9108 "unable to register hwmon platform driver\n");
9109 thinkpad_acpi_module_exit();
9110 return ret;
9112 tp_features.sensors_pdrv_registered = 1;
9114 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9115 if (!ret) {
9116 tp_features.platform_drv_attrs_registered = 1;
9117 ret = tpacpi_create_driver_attributes(
9118 &tpacpi_hwmon_pdriver.driver);
9120 if (ret) {
9121 printk(TPACPI_ERR
9122 "unable to create sysfs driver attributes\n");
9123 thinkpad_acpi_module_exit();
9124 return ret;
9126 tp_features.sensors_pdrv_attrs_registered = 1;
9129 /* Device initialization */
9130 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9131 NULL, 0);
9132 if (IS_ERR(tpacpi_pdev)) {
9133 ret = PTR_ERR(tpacpi_pdev);
9134 tpacpi_pdev = NULL;
9135 printk(TPACPI_ERR "unable to register platform device\n");
9136 thinkpad_acpi_module_exit();
9137 return ret;
9139 tpacpi_sensors_pdev = platform_device_register_simple(
9140 TPACPI_HWMON_DRVR_NAME,
9141 -1, NULL, 0);
9142 if (IS_ERR(tpacpi_sensors_pdev)) {
9143 ret = PTR_ERR(tpacpi_sensors_pdev);
9144 tpacpi_sensors_pdev = NULL;
9145 printk(TPACPI_ERR
9146 "unable to register hwmon platform device\n");
9147 thinkpad_acpi_module_exit();
9148 return ret;
9150 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9151 &dev_attr_thinkpad_acpi_pdev_name);
9152 if (ret) {
9153 printk(TPACPI_ERR
9154 "unable to create sysfs hwmon device attributes\n");
9155 thinkpad_acpi_module_exit();
9156 return ret;
9158 tp_features.sensors_pdev_attrs_registered = 1;
9159 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9160 if (IS_ERR(tpacpi_hwmon)) {
9161 ret = PTR_ERR(tpacpi_hwmon);
9162 tpacpi_hwmon = NULL;
9163 printk(TPACPI_ERR "unable to register hwmon device\n");
9164 thinkpad_acpi_module_exit();
9165 return ret;
9167 mutex_init(&tpacpi_inputdev_send_mutex);
9168 tpacpi_inputdev = input_allocate_device();
9169 if (!tpacpi_inputdev) {
9170 printk(TPACPI_ERR "unable to allocate input device\n");
9171 thinkpad_acpi_module_exit();
9172 return -ENOMEM;
9173 } else {
9174 /* Prepare input device, but don't register */
9175 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9176 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9177 tpacpi_inputdev->id.bustype = BUS_HOST;
9178 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9179 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9180 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9181 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9184 /* Init subdriver dependencies */
9185 tpacpi_detect_brightness_capabilities();
9187 /* Init subdrivers */
9188 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9189 ret = ibm_init(&ibms_init[i]);
9190 if (ret >= 0 && *ibms_init[i].param)
9191 ret = ibms_init[i].data->write(ibms_init[i].param);
9192 if (ret < 0) {
9193 thinkpad_acpi_module_exit();
9194 return ret;
9198 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9200 ret = input_register_device(tpacpi_inputdev);
9201 if (ret < 0) {
9202 printk(TPACPI_ERR "unable to register input device\n");
9203 thinkpad_acpi_module_exit();
9204 return ret;
9205 } else {
9206 tp_features.input_device_registered = 1;
9209 return 0;
9212 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9215 * This will autoload the driver in almost every ThinkPad
9216 * in widespread use.
9218 * Only _VERY_ old models, like the 240, 240x and 570 lack
9219 * the HKEY event interface.
9221 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9224 * DMI matching for module autoloading
9226 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9227 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9229 * Only models listed in thinkwiki will be supported, so add yours
9230 * if it is not there yet.
9232 #define IBM_BIOS_MODULE_ALIAS(__type) \
9233 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9235 /* Ancient thinkpad BIOSes have to be identified by
9236 * BIOS type or model number, and there are far less
9237 * BIOS types than model numbers... */
9238 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9240 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9241 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9242 MODULE_DESCRIPTION(TPACPI_DESC);
9243 MODULE_VERSION(TPACPI_VERSION);
9244 MODULE_LICENSE("GPL");
9246 module_init(thinkpad_acpi_module_init);
9247 module_exit(thinkpad_acpi_module_exit);