thinkpad-acpi: clean up ACPI handles handling
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob9256e54414832b46c413ddc6c86ba36f3f402166
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 /* --------------------------------------------------------------------- */
6151 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6153 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6154 union acpi_object *obj;
6155 int rc;
6157 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
6158 obj = (union acpi_object *)buffer.pointer;
6159 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6160 printk(TPACPI_ERR "Unknown _BCL data, "
6161 "please report this to %s\n", TPACPI_MAIL);
6162 rc = 0;
6163 } else {
6164 rc = obj->package.count;
6166 } else {
6167 return 0;
6170 kfree(buffer.pointer);
6171 return rc;
6174 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
6175 u32 lvl, void *context, void **rv)
6177 char name[ACPI_PATH_SEGMENT_LENGTH];
6178 struct acpi_buffer buffer = { sizeof(name), &name };
6180 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
6181 !strncmp("_BCL", name, sizeof(name) - 1)) {
6182 BUG_ON(!rv || !*rv);
6183 **(int **)rv = tpacpi_query_bcl_levels(handle);
6184 return AE_CTRL_TERMINATE;
6185 } else {
6186 return AE_OK;
6191 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6193 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6195 int status;
6196 int bcl_levels = 0;
6197 void *bcl_ptr = &bcl_levels;
6199 if (!vid_handle)
6200 TPACPI_ACPIHANDLE_INIT(vid);
6202 if (!vid_handle)
6203 return 0;
6206 * Search for a _BCL method, and execute it. This is safe on all
6207 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
6208 * BIOS in ACPI backlight control mode. We do NOT have to care
6209 * about calling the _BCL method in an enabled video device, any
6210 * will do for our purposes.
6213 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
6214 tpacpi_acpi_walk_find_bcl, NULL, NULL,
6215 &bcl_ptr);
6217 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
6218 tp_features.bright_acpimode = 1;
6219 return bcl_levels - 2;
6222 return 0;
6226 * These are only useful for models that have only one possibility
6227 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6228 * these quirks.
6230 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6231 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6232 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6234 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6235 /* Models with ATI GPUs known to require ECNVRAM mode */
6236 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6238 /* Models with ATI GPUs that can use ECNVRAM */
6239 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6240 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6241 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6242 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6244 /* Models with Intel Extreme Graphics 2 */
6245 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6246 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6247 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6249 /* Models with Intel GMA900 */
6250 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6251 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6252 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6256 * Returns < 0 for error, otherwise sets tp_features.bright_*
6257 * and bright_maxlvl.
6259 static void __init tpacpi_detect_brightness_capabilities(void)
6261 unsigned int b;
6263 vdbg_printk(TPACPI_DBG_INIT,
6264 "detecting firmware brightness interface capabilities\n");
6266 /* we could run a quirks check here (same table used by
6267 * brightness_init) if needed */
6270 * We always attempt to detect acpi support, so as to switch
6271 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6272 * going to publish a backlight interface
6274 b = tpacpi_check_std_acpi_brightness_support();
6275 switch (b) {
6276 case 16:
6277 bright_maxlvl = 15;
6278 printk(TPACPI_INFO
6279 "detected a 16-level brightness capable ThinkPad\n");
6280 break;
6281 case 8:
6282 case 0:
6283 bright_maxlvl = 7;
6284 printk(TPACPI_INFO
6285 "detected a 8-level brightness capable ThinkPad\n");
6286 break;
6287 default:
6288 printk(TPACPI_ERR
6289 "Unsupported brightness interface, "
6290 "please contact %s\n", TPACPI_MAIL);
6291 tp_features.bright_unkfw = 1;
6292 bright_maxlvl = b - 1;
6296 static int __init brightness_init(struct ibm_init_struct *iibm)
6298 int b;
6299 unsigned long quirks;
6301 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6303 mutex_init(&brightness_mutex);
6305 quirks = tpacpi_check_quirks(brightness_quirk_table,
6306 ARRAY_SIZE(brightness_quirk_table));
6308 /* tpacpi_detect_brightness_capabilities() must have run already */
6310 /* if it is unknown, we don't handle it: it wouldn't be safe */
6311 if (tp_features.bright_unkfw)
6312 return 1;
6314 if (tp_features.bright_acpimode) {
6315 if (acpi_video_backlight_support()) {
6316 if (brightness_enable > 1) {
6317 printk(TPACPI_NOTICE
6318 "Standard ACPI backlight interface "
6319 "available, not loading native one.\n");
6320 return 1;
6321 } else if (brightness_enable == 1) {
6322 printk(TPACPI_NOTICE
6323 "Backlight control force enabled, even if standard "
6324 "ACPI backlight interface is available\n");
6326 } else {
6327 if (brightness_enable > 1) {
6328 printk(TPACPI_NOTICE
6329 "Standard ACPI backlight interface not "
6330 "available, thinkpad_acpi native "
6331 "brightness control enabled\n");
6336 if (!brightness_enable) {
6337 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6338 "brightness support disabled by "
6339 "module parameter\n");
6340 return 1;
6344 * Check for module parameter bogosity, note that we
6345 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6346 * able to detect "unspecified"
6348 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6349 return -EINVAL;
6351 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6352 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6353 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6354 if (quirks & TPACPI_BRGHT_Q_EC)
6355 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6356 else
6357 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6359 dbg_printk(TPACPI_DBG_BRGHT,
6360 "driver auto-selected brightness_mode=%d\n",
6361 brightness_mode);
6364 /* Safety */
6365 if (!tpacpi_is_ibm() &&
6366 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6367 brightness_mode == TPACPI_BRGHT_MODE_EC))
6368 return -EINVAL;
6370 if (tpacpi_brightness_get_raw(&b) < 0)
6371 return 1;
6373 ibm_backlight_device = backlight_device_register(
6374 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6375 &ibm_backlight_data);
6376 if (IS_ERR(ibm_backlight_device)) {
6377 int rc = PTR_ERR(ibm_backlight_device);
6378 ibm_backlight_device = NULL;
6379 printk(TPACPI_ERR "Could not register backlight device\n");
6380 return rc;
6382 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6383 "brightness is supported\n");
6385 if (quirks & TPACPI_BRGHT_Q_ASK) {
6386 printk(TPACPI_NOTICE
6387 "brightness: will use unverified default: "
6388 "brightness_mode=%d\n", brightness_mode);
6389 printk(TPACPI_NOTICE
6390 "brightness: please report to %s whether it works well "
6391 "or not on your ThinkPad\n", TPACPI_MAIL);
6394 ibm_backlight_device->props.max_brightness = bright_maxlvl;
6395 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6396 backlight_update_status(ibm_backlight_device);
6398 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6399 "brightness: registering brightness hotkeys "
6400 "as change notification\n");
6401 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6402 | TP_ACPI_HKEY_BRGHTUP_MASK
6403 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6404 return 0;
6407 static void brightness_suspend(pm_message_t state)
6409 tpacpi_brightness_checkpoint_nvram();
6412 static void brightness_shutdown(void)
6414 tpacpi_brightness_checkpoint_nvram();
6417 static void brightness_exit(void)
6419 if (ibm_backlight_device) {
6420 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6421 "calling backlight_device_unregister()\n");
6422 backlight_device_unregister(ibm_backlight_device);
6425 tpacpi_brightness_checkpoint_nvram();
6428 static int brightness_read(struct seq_file *m)
6430 int level;
6432 level = brightness_get(NULL);
6433 if (level < 0) {
6434 seq_printf(m, "level:\t\tunreadable\n");
6435 } else {
6436 seq_printf(m, "level:\t\t%d\n", level);
6437 seq_printf(m, "commands:\tup, down\n");
6438 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6439 bright_maxlvl);
6442 return 0;
6445 static int brightness_write(char *buf)
6447 int level;
6448 int rc;
6449 char *cmd;
6451 level = brightness_get(NULL);
6452 if (level < 0)
6453 return level;
6455 while ((cmd = next_cmd(&buf))) {
6456 if (strlencmp(cmd, "up") == 0) {
6457 if (level < bright_maxlvl)
6458 level++;
6459 } else if (strlencmp(cmd, "down") == 0) {
6460 if (level > 0)
6461 level--;
6462 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6463 level >= 0 && level <= bright_maxlvl) {
6464 /* new level set */
6465 } else
6466 return -EINVAL;
6469 tpacpi_disclose_usertask("procfs brightness",
6470 "set level to %d\n", level);
6473 * Now we know what the final level should be, so we try to set it.
6474 * Doing it this way makes the syscall restartable in case of EINTR
6476 rc = brightness_set(level);
6477 if (!rc && ibm_backlight_device)
6478 backlight_force_update(ibm_backlight_device,
6479 BACKLIGHT_UPDATE_SYSFS);
6480 return (rc == -EINTR)? -ERESTARTSYS : rc;
6483 static struct ibm_struct brightness_driver_data = {
6484 .name = "brightness",
6485 .read = brightness_read,
6486 .write = brightness_write,
6487 .exit = brightness_exit,
6488 .suspend = brightness_suspend,
6489 .shutdown = brightness_shutdown,
6492 /*************************************************************************
6493 * Volume subdriver
6497 * IBM ThinkPads have a simple volume controller with MUTE gating.
6498 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6500 * Since the *61 series (and probably also the later *60 series), Lenovo
6501 * ThinkPads only implement the MUTE gate.
6503 * EC register 0x30
6504 * Bit 6: MUTE (1 mutes sound)
6505 * Bit 3-0: Volume
6506 * Other bits should be zero as far as we know.
6508 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6509 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6510 * such as bit 7 which is used to detect repeated presses of MUTE,
6511 * and we leave them unchanged.
6514 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6516 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6517 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6518 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6520 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6521 static char *alsa_id = "ThinkPadEC";
6522 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6524 struct tpacpi_alsa_data {
6525 struct snd_card *card;
6526 struct snd_ctl_elem_id *ctl_mute_id;
6527 struct snd_ctl_elem_id *ctl_vol_id;
6530 static struct snd_card *alsa_card;
6532 enum {
6533 TP_EC_AUDIO = 0x30,
6535 /* TP_EC_AUDIO bits */
6536 TP_EC_AUDIO_MUTESW = 6,
6538 /* TP_EC_AUDIO bitmasks */
6539 TP_EC_AUDIO_LVL_MSK = 0x0F,
6540 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6542 /* Maximum volume */
6543 TP_EC_VOLUME_MAX = 14,
6546 enum tpacpi_volume_access_mode {
6547 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6548 TPACPI_VOL_MODE_EC, /* Pure EC control */
6549 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6550 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6551 TPACPI_VOL_MODE_MAX
6554 enum tpacpi_volume_capabilities {
6555 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6556 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6557 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6558 TPACPI_VOL_CAP_MAX
6561 static enum tpacpi_volume_access_mode volume_mode =
6562 TPACPI_VOL_MODE_MAX;
6564 static enum tpacpi_volume_capabilities volume_capabilities;
6565 static int volume_control_allowed;
6568 * Used to syncronize writers to TP_EC_AUDIO and
6569 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6571 static struct mutex volume_mutex;
6573 static void tpacpi_volume_checkpoint_nvram(void)
6575 u8 lec = 0;
6576 u8 b_nvram;
6577 u8 ec_mask;
6579 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6580 return;
6581 if (!volume_control_allowed)
6582 return;
6584 vdbg_printk(TPACPI_DBG_MIXER,
6585 "trying to checkpoint mixer state to NVRAM...\n");
6587 if (tp_features.mixer_no_level_control)
6588 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6589 else
6590 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6592 if (mutex_lock_killable(&volume_mutex) < 0)
6593 return;
6595 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6596 goto unlock;
6597 lec &= ec_mask;
6598 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6600 if (lec != (b_nvram & ec_mask)) {
6601 /* NVRAM needs update */
6602 b_nvram &= ~ec_mask;
6603 b_nvram |= lec;
6604 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6605 dbg_printk(TPACPI_DBG_MIXER,
6606 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6607 (unsigned int) lec, (unsigned int) b_nvram);
6608 } else {
6609 vdbg_printk(TPACPI_DBG_MIXER,
6610 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6611 (unsigned int) lec, (unsigned int) b_nvram);
6614 unlock:
6615 mutex_unlock(&volume_mutex);
6618 static int volume_get_status_ec(u8 *status)
6620 u8 s;
6622 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6623 return -EIO;
6625 *status = s;
6627 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6629 return 0;
6632 static int volume_get_status(u8 *status)
6634 return volume_get_status_ec(status);
6637 static int volume_set_status_ec(const u8 status)
6639 if (!acpi_ec_write(TP_EC_AUDIO, status))
6640 return -EIO;
6642 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6644 return 0;
6647 static int volume_set_status(const u8 status)
6649 return volume_set_status_ec(status);
6652 /* returns < 0 on error, 0 on no change, 1 on change */
6653 static int __volume_set_mute_ec(const bool mute)
6655 int rc;
6656 u8 s, n;
6658 if (mutex_lock_killable(&volume_mutex) < 0)
6659 return -EINTR;
6661 rc = volume_get_status_ec(&s);
6662 if (rc)
6663 goto unlock;
6665 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6666 s & ~TP_EC_AUDIO_MUTESW_MSK;
6668 if (n != s) {
6669 rc = volume_set_status_ec(n);
6670 if (!rc)
6671 rc = 1;
6674 unlock:
6675 mutex_unlock(&volume_mutex);
6676 return rc;
6679 static int volume_alsa_set_mute(const bool mute)
6681 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6682 (mute) ? "" : "un");
6683 return __volume_set_mute_ec(mute);
6686 static int volume_set_mute(const bool mute)
6688 int rc;
6690 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6691 (mute) ? "" : "un");
6693 rc = __volume_set_mute_ec(mute);
6694 return (rc < 0) ? rc : 0;
6697 /* returns < 0 on error, 0 on no change, 1 on change */
6698 static int __volume_set_volume_ec(const u8 vol)
6700 int rc;
6701 u8 s, n;
6703 if (vol > TP_EC_VOLUME_MAX)
6704 return -EINVAL;
6706 if (mutex_lock_killable(&volume_mutex) < 0)
6707 return -EINTR;
6709 rc = volume_get_status_ec(&s);
6710 if (rc)
6711 goto unlock;
6713 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6715 if (n != s) {
6716 rc = volume_set_status_ec(n);
6717 if (!rc)
6718 rc = 1;
6721 unlock:
6722 mutex_unlock(&volume_mutex);
6723 return rc;
6726 static int volume_alsa_set_volume(const u8 vol)
6728 dbg_printk(TPACPI_DBG_MIXER,
6729 "ALSA: trying to set volume level to %hu\n", vol);
6730 return __volume_set_volume_ec(vol);
6733 static void volume_alsa_notify_change(void)
6735 struct tpacpi_alsa_data *d;
6737 if (alsa_card && alsa_card->private_data) {
6738 d = alsa_card->private_data;
6739 if (d->ctl_mute_id)
6740 snd_ctl_notify(alsa_card,
6741 SNDRV_CTL_EVENT_MASK_VALUE,
6742 d->ctl_mute_id);
6743 if (d->ctl_vol_id)
6744 snd_ctl_notify(alsa_card,
6745 SNDRV_CTL_EVENT_MASK_VALUE,
6746 d->ctl_vol_id);
6750 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6751 struct snd_ctl_elem_info *uinfo)
6753 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6754 uinfo->count = 1;
6755 uinfo->value.integer.min = 0;
6756 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6757 return 0;
6760 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6761 struct snd_ctl_elem_value *ucontrol)
6763 u8 s;
6764 int rc;
6766 rc = volume_get_status(&s);
6767 if (rc < 0)
6768 return rc;
6770 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6771 return 0;
6774 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6775 struct snd_ctl_elem_value *ucontrol)
6777 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6778 ucontrol->value.integer.value[0]);
6779 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6782 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6784 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6785 struct snd_ctl_elem_value *ucontrol)
6787 u8 s;
6788 int rc;
6790 rc = volume_get_status(&s);
6791 if (rc < 0)
6792 return rc;
6794 ucontrol->value.integer.value[0] =
6795 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6796 return 0;
6799 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6800 struct snd_ctl_elem_value *ucontrol)
6802 tpacpi_disclose_usertask("ALSA", "%smute\n",
6803 ucontrol->value.integer.value[0] ?
6804 "un" : "");
6805 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6808 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6809 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6810 .name = "Console Playback Volume",
6811 .index = 0,
6812 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6813 .info = volume_alsa_vol_info,
6814 .get = volume_alsa_vol_get,
6817 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6818 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6819 .name = "Console Playback Switch",
6820 .index = 0,
6821 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6822 .info = volume_alsa_mute_info,
6823 .get = volume_alsa_mute_get,
6826 static void volume_suspend(pm_message_t state)
6828 tpacpi_volume_checkpoint_nvram();
6831 static void volume_resume(void)
6833 volume_alsa_notify_change();
6836 static void volume_shutdown(void)
6838 tpacpi_volume_checkpoint_nvram();
6841 static void volume_exit(void)
6843 if (alsa_card) {
6844 snd_card_free(alsa_card);
6845 alsa_card = NULL;
6848 tpacpi_volume_checkpoint_nvram();
6851 static int __init volume_create_alsa_mixer(void)
6853 struct snd_card *card;
6854 struct tpacpi_alsa_data *data;
6855 struct snd_kcontrol *ctl_vol;
6856 struct snd_kcontrol *ctl_mute;
6857 int rc;
6859 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6860 sizeof(struct tpacpi_alsa_data), &card);
6861 if (rc < 0 || !card) {
6862 printk(TPACPI_ERR
6863 "Failed to create ALSA card structures: %d\n", rc);
6864 return 1;
6867 BUG_ON(!card->private_data);
6868 data = card->private_data;
6869 data->card = card;
6871 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6872 sizeof(card->driver));
6873 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6874 sizeof(card->shortname));
6875 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6876 (thinkpad_id.ec_version_str) ?
6877 thinkpad_id.ec_version_str : "(unknown)");
6878 snprintf(card->longname, sizeof(card->longname),
6879 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6880 (thinkpad_id.ec_version_str) ?
6881 thinkpad_id.ec_version_str : "unknown");
6883 if (volume_control_allowed) {
6884 volume_alsa_control_vol.put = volume_alsa_vol_put;
6885 volume_alsa_control_vol.access =
6886 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6888 volume_alsa_control_mute.put = volume_alsa_mute_put;
6889 volume_alsa_control_mute.access =
6890 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6893 if (!tp_features.mixer_no_level_control) {
6894 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6895 rc = snd_ctl_add(card, ctl_vol);
6896 if (rc < 0) {
6897 printk(TPACPI_ERR
6898 "Failed to create ALSA volume control: %d\n",
6899 rc);
6900 goto err_exit;
6902 data->ctl_vol_id = &ctl_vol->id;
6905 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6906 rc = snd_ctl_add(card, ctl_mute);
6907 if (rc < 0) {
6908 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6909 rc);
6910 goto err_exit;
6912 data->ctl_mute_id = &ctl_mute->id;
6914 snd_card_set_dev(card, &tpacpi_pdev->dev);
6915 rc = snd_card_register(card);
6916 if (rc < 0) {
6917 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6918 goto err_exit;
6921 alsa_card = card;
6922 return 0;
6924 err_exit:
6925 snd_card_free(card);
6926 return 1;
6929 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6930 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6932 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6933 /* Whitelist volume level on all IBM by default */
6934 { .vendor = PCI_VENDOR_ID_IBM,
6935 .bios = TPACPI_MATCH_ANY,
6936 .ec = TPACPI_MATCH_ANY,
6937 .quirks = TPACPI_VOL_Q_LEVEL },
6939 /* Lenovo models with volume control (needs confirmation) */
6940 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6941 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6942 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6943 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6944 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6945 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6946 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6948 /* Whitelist mute-only on all Lenovo by default */
6949 { .vendor = PCI_VENDOR_ID_LENOVO,
6950 .bios = TPACPI_MATCH_ANY,
6951 .ec = TPACPI_MATCH_ANY,
6952 .quirks = TPACPI_VOL_Q_MUTEONLY }
6955 static int __init volume_init(struct ibm_init_struct *iibm)
6957 unsigned long quirks;
6958 int rc;
6960 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6962 mutex_init(&volume_mutex);
6965 * Check for module parameter bogosity, note that we
6966 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6967 * able to detect "unspecified"
6969 if (volume_mode > TPACPI_VOL_MODE_MAX)
6970 return -EINVAL;
6972 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6973 printk(TPACPI_ERR
6974 "UCMS step volume mode not implemented, "
6975 "please contact %s\n", TPACPI_MAIL);
6976 return 1;
6979 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6980 return -EINVAL;
6983 * The ALSA mixer is our primary interface.
6984 * When disabled, don't install the subdriver at all
6986 if (!alsa_enable) {
6987 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6988 "ALSA mixer disabled by parameter, "
6989 "not loading volume subdriver...\n");
6990 return 1;
6993 quirks = tpacpi_check_quirks(volume_quirk_table,
6994 ARRAY_SIZE(volume_quirk_table));
6996 switch (volume_capabilities) {
6997 case TPACPI_VOL_CAP_AUTO:
6998 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6999 tp_features.mixer_no_level_control = 1;
7000 else if (quirks & TPACPI_VOL_Q_LEVEL)
7001 tp_features.mixer_no_level_control = 0;
7002 else
7003 return 1; /* no mixer */
7004 break;
7005 case TPACPI_VOL_CAP_VOLMUTE:
7006 tp_features.mixer_no_level_control = 0;
7007 break;
7008 case TPACPI_VOL_CAP_MUTEONLY:
7009 tp_features.mixer_no_level_control = 1;
7010 break;
7011 default:
7012 return 1;
7015 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7016 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7017 "using user-supplied volume_capabilities=%d\n",
7018 volume_capabilities);
7020 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7021 volume_mode == TPACPI_VOL_MODE_MAX) {
7022 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7024 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7025 "driver auto-selected volume_mode=%d\n",
7026 volume_mode);
7027 } else {
7028 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7029 "using user-supplied volume_mode=%d\n",
7030 volume_mode);
7033 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7034 "mute is supported, volume control is %s\n",
7035 str_supported(!tp_features.mixer_no_level_control));
7037 rc = volume_create_alsa_mixer();
7038 if (rc) {
7039 printk(TPACPI_ERR
7040 "Could not create the ALSA mixer interface\n");
7041 return rc;
7044 printk(TPACPI_INFO
7045 "Console audio control enabled, mode: %s\n",
7046 (volume_control_allowed) ?
7047 "override (read/write)" :
7048 "monitor (read only)");
7050 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7051 "registering volume hotkeys as change notification\n");
7052 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7053 | TP_ACPI_HKEY_VOLUP_MASK
7054 | TP_ACPI_HKEY_VOLDWN_MASK
7055 | TP_ACPI_HKEY_MUTE_MASK);
7057 return 0;
7060 static int volume_read(struct seq_file *m)
7062 u8 status;
7064 if (volume_get_status(&status) < 0) {
7065 seq_printf(m, "level:\t\tunreadable\n");
7066 } else {
7067 if (tp_features.mixer_no_level_control)
7068 seq_printf(m, "level:\t\tunsupported\n");
7069 else
7070 seq_printf(m, "level:\t\t%d\n",
7071 status & TP_EC_AUDIO_LVL_MSK);
7073 seq_printf(m, "mute:\t\t%s\n",
7074 onoff(status, TP_EC_AUDIO_MUTESW));
7076 if (volume_control_allowed) {
7077 seq_printf(m, "commands:\tunmute, mute\n");
7078 if (!tp_features.mixer_no_level_control) {
7079 seq_printf(m,
7080 "commands:\tup, down\n");
7081 seq_printf(m,
7082 "commands:\tlevel <level>"
7083 " (<level> is 0-%d)\n",
7084 TP_EC_VOLUME_MAX);
7089 return 0;
7092 static int volume_write(char *buf)
7094 u8 s;
7095 u8 new_level, new_mute;
7096 int l;
7097 char *cmd;
7098 int rc;
7101 * We do allow volume control at driver startup, so that the
7102 * user can set initial state through the volume=... parameter hack.
7104 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7105 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7106 tp_warned.volume_ctrl_forbidden = 1;
7107 printk(TPACPI_NOTICE
7108 "Console audio control in monitor mode, "
7109 "changes are not allowed.\n");
7110 printk(TPACPI_NOTICE
7111 "Use the volume_control=1 module parameter "
7112 "to enable volume control\n");
7114 return -EPERM;
7117 rc = volume_get_status(&s);
7118 if (rc < 0)
7119 return rc;
7121 new_level = s & TP_EC_AUDIO_LVL_MSK;
7122 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7124 while ((cmd = next_cmd(&buf))) {
7125 if (!tp_features.mixer_no_level_control) {
7126 if (strlencmp(cmd, "up") == 0) {
7127 if (new_mute)
7128 new_mute = 0;
7129 else if (new_level < TP_EC_VOLUME_MAX)
7130 new_level++;
7131 continue;
7132 } else if (strlencmp(cmd, "down") == 0) {
7133 if (new_mute)
7134 new_mute = 0;
7135 else if (new_level > 0)
7136 new_level--;
7137 continue;
7138 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7139 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7140 new_level = l;
7141 continue;
7144 if (strlencmp(cmd, "mute") == 0)
7145 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7146 else if (strlencmp(cmd, "unmute") == 0)
7147 new_mute = 0;
7148 else
7149 return -EINVAL;
7152 if (tp_features.mixer_no_level_control) {
7153 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7154 new_mute ? "" : "un");
7155 rc = volume_set_mute(!!new_mute);
7156 } else {
7157 tpacpi_disclose_usertask("procfs volume",
7158 "%smute and set level to %d\n",
7159 new_mute ? "" : "un", new_level);
7160 rc = volume_set_status(new_mute | new_level);
7162 volume_alsa_notify_change();
7164 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7167 static struct ibm_struct volume_driver_data = {
7168 .name = "volume",
7169 .read = volume_read,
7170 .write = volume_write,
7171 .exit = volume_exit,
7172 .suspend = volume_suspend,
7173 .resume = volume_resume,
7174 .shutdown = volume_shutdown,
7177 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7179 #define alsa_card NULL
7181 static void inline volume_alsa_notify_change(void)
7185 static int __init volume_init(struct ibm_init_struct *iibm)
7187 printk(TPACPI_INFO
7188 "volume: disabled as there is no ALSA support in this kernel\n");
7190 return 1;
7193 static struct ibm_struct volume_driver_data = {
7194 .name = "volume",
7197 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7199 /*************************************************************************
7200 * Fan subdriver
7204 * FAN ACCESS MODES
7206 * TPACPI_FAN_RD_ACPI_GFAN:
7207 * ACPI GFAN method: returns fan level
7209 * see TPACPI_FAN_WR_ACPI_SFAN
7210 * EC 0x2f (HFSP) not available if GFAN exists
7212 * TPACPI_FAN_WR_ACPI_SFAN:
7213 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7215 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7216 * it for writing.
7218 * TPACPI_FAN_WR_TPEC:
7219 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7220 * Supported on almost all ThinkPads
7222 * Fan speed changes of any sort (including those caused by the
7223 * disengaged mode) are usually done slowly by the firmware as the
7224 * maximum amount of fan duty cycle change per second seems to be
7225 * limited.
7227 * Reading is not available if GFAN exists.
7228 * Writing is not available if SFAN exists.
7230 * Bits
7231 * 7 automatic mode engaged;
7232 * (default operation mode of the ThinkPad)
7233 * fan level is ignored in this mode.
7234 * 6 full speed mode (takes precedence over bit 7);
7235 * not available on all thinkpads. May disable
7236 * the tachometer while the fan controller ramps up
7237 * the speed (which can take up to a few *minutes*).
7238 * Speeds up fan to 100% duty-cycle, which is far above
7239 * the standard RPM levels. It is not impossible that
7240 * it could cause hardware damage.
7241 * 5-3 unused in some models. Extra bits for fan level
7242 * in others, but still useless as all values above
7243 * 7 map to the same speed as level 7 in these models.
7244 * 2-0 fan level (0..7 usually)
7245 * 0x00 = stop
7246 * 0x07 = max (set when temperatures critical)
7247 * Some ThinkPads may have other levels, see
7248 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7250 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7251 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7252 * does so, its initial value is meaningless (0x07).
7254 * For firmware bugs, refer to:
7255 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7257 * ----
7259 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7260 * Main fan tachometer reading (in RPM)
7262 * This register is present on all ThinkPads with a new-style EC, and
7263 * it is known not to be present on the A21m/e, and T22, as there is
7264 * something else in offset 0x84 according to the ACPI DSDT. Other
7265 * ThinkPads from this same time period (and earlier) probably lack the
7266 * tachometer as well.
7268 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7269 * was never fixed by IBM to report the EC firmware version string
7270 * probably support the tachometer (like the early X models), so
7271 * detecting it is quite hard. We need more data to know for sure.
7273 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7274 * might result.
7276 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7277 * mode.
7279 * For firmware bugs, refer to:
7280 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7282 * ----
7284 * ThinkPad EC register 0x31 bit 0 (only on select models)
7286 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7287 * show the speed of the main fan. When bit 0 of EC register 0x31
7288 * is one, the tachometer registers show the speed of the auxiliary
7289 * fan.
7291 * Fan control seems to affect both fans, regardless of the state
7292 * of this bit.
7294 * So far, only the firmware for the X60/X61 non-tablet versions
7295 * seem to support this (firmware TP-7M).
7297 * TPACPI_FAN_WR_ACPI_FANS:
7298 * ThinkPad X31, X40, X41. Not available in the X60.
7300 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7301 * high speed. ACPI DSDT seems to map these three speeds to levels
7302 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7303 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7305 * The speeds are stored on handles
7306 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7308 * There are three default speed sets, accessible as handles:
7309 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7311 * ACPI DSDT switches which set is in use depending on various
7312 * factors.
7314 * TPACPI_FAN_WR_TPEC is also available and should be used to
7315 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7316 * but the ACPI tables just mention level 7.
7319 enum { /* Fan control constants */
7320 fan_status_offset = 0x2f, /* EC register 0x2f */
7321 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7322 * 0x84 must be read before 0x85 */
7323 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7324 bit 0 selects which fan is active */
7326 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7327 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7329 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7332 enum fan_status_access_mode {
7333 TPACPI_FAN_NONE = 0, /* No fan status or control */
7334 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7335 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7338 enum fan_control_access_mode {
7339 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7340 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7341 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7342 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7345 enum fan_control_commands {
7346 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7347 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7348 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7349 * and also watchdog cmd */
7352 static int fan_control_allowed;
7354 static enum fan_status_access_mode fan_status_access_mode;
7355 static enum fan_control_access_mode fan_control_access_mode;
7356 static enum fan_control_commands fan_control_commands;
7358 static u8 fan_control_initial_status;
7359 static u8 fan_control_desired_level;
7360 static u8 fan_control_resume_level;
7361 static int fan_watchdog_maxinterval;
7363 static struct mutex fan_mutex;
7365 static void fan_watchdog_fire(struct work_struct *ignored);
7366 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7368 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7369 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7370 "\\FSPD", /* 600e/x, 770e, 770x */
7371 ); /* all others */
7372 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7373 "JFNS", /* 770x-JL */
7374 ); /* all others */
7377 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7378 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7379 * be in auto mode (0x80).
7381 * This is corrected by any write to HFSP either by the driver, or
7382 * by the firmware.
7384 * We assume 0x07 really means auto mode while this quirk is active,
7385 * as this is far more likely than the ThinkPad being in level 7,
7386 * which is only used by the firmware during thermal emergencies.
7388 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7389 * TP-70 (T43, R52), which are known to be buggy.
7392 static void fan_quirk1_setup(void)
7394 if (fan_control_initial_status == 0x07) {
7395 printk(TPACPI_NOTICE
7396 "fan_init: initial fan status is unknown, "
7397 "assuming it is in auto mode\n");
7398 tp_features.fan_ctrl_status_undef = 1;
7402 static void fan_quirk1_handle(u8 *fan_status)
7404 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7405 if (*fan_status != fan_control_initial_status) {
7406 /* something changed the HFSP regisnter since
7407 * driver init time, so it is not undefined
7408 * anymore */
7409 tp_features.fan_ctrl_status_undef = 0;
7410 } else {
7411 /* Return most likely status. In fact, it
7412 * might be the only possible status */
7413 *fan_status = TP_EC_FAN_AUTO;
7418 /* Select main fan on X60/X61, NOOP on others */
7419 static bool fan_select_fan1(void)
7421 if (tp_features.second_fan) {
7422 u8 val;
7424 if (ec_read(fan_select_offset, &val) < 0)
7425 return false;
7426 val &= 0xFEU;
7427 if (ec_write(fan_select_offset, val) < 0)
7428 return false;
7430 return true;
7433 /* Select secondary fan on X60/X61 */
7434 static bool fan_select_fan2(void)
7436 u8 val;
7438 if (!tp_features.second_fan)
7439 return false;
7441 if (ec_read(fan_select_offset, &val) < 0)
7442 return false;
7443 val |= 0x01U;
7444 if (ec_write(fan_select_offset, val) < 0)
7445 return false;
7447 return true;
7451 * Call with fan_mutex held
7453 static void fan_update_desired_level(u8 status)
7455 if ((status &
7456 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7457 if (status > 7)
7458 fan_control_desired_level = 7;
7459 else
7460 fan_control_desired_level = status;
7464 static int fan_get_status(u8 *status)
7466 u8 s;
7468 /* TODO:
7469 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7471 switch (fan_status_access_mode) {
7472 case TPACPI_FAN_RD_ACPI_GFAN:
7473 /* 570, 600e/x, 770e, 770x */
7475 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7476 return -EIO;
7478 if (likely(status))
7479 *status = s & 0x07;
7481 break;
7483 case TPACPI_FAN_RD_TPEC:
7484 /* all except 570, 600e/x, 770e, 770x */
7485 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7486 return -EIO;
7488 if (likely(status)) {
7489 *status = s;
7490 fan_quirk1_handle(status);
7493 break;
7495 default:
7496 return -ENXIO;
7499 return 0;
7502 static int fan_get_status_safe(u8 *status)
7504 int rc;
7505 u8 s;
7507 if (mutex_lock_killable(&fan_mutex))
7508 return -ERESTARTSYS;
7509 rc = fan_get_status(&s);
7510 if (!rc)
7511 fan_update_desired_level(s);
7512 mutex_unlock(&fan_mutex);
7514 if (status)
7515 *status = s;
7517 return rc;
7520 static int fan_get_speed(unsigned int *speed)
7522 u8 hi, lo;
7524 switch (fan_status_access_mode) {
7525 case TPACPI_FAN_RD_TPEC:
7526 /* all except 570, 600e/x, 770e, 770x */
7527 if (unlikely(!fan_select_fan1()))
7528 return -EIO;
7529 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7530 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7531 return -EIO;
7533 if (likely(speed))
7534 *speed = (hi << 8) | lo;
7536 break;
7538 default:
7539 return -ENXIO;
7542 return 0;
7545 static int fan2_get_speed(unsigned int *speed)
7547 u8 hi, lo;
7548 bool rc;
7550 switch (fan_status_access_mode) {
7551 case TPACPI_FAN_RD_TPEC:
7552 /* all except 570, 600e/x, 770e, 770x */
7553 if (unlikely(!fan_select_fan2()))
7554 return -EIO;
7555 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7556 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7557 fan_select_fan1(); /* play it safe */
7558 if (rc)
7559 return -EIO;
7561 if (likely(speed))
7562 *speed = (hi << 8) | lo;
7564 break;
7566 default:
7567 return -ENXIO;
7570 return 0;
7573 static int fan_set_level(int level)
7575 if (!fan_control_allowed)
7576 return -EPERM;
7578 switch (fan_control_access_mode) {
7579 case TPACPI_FAN_WR_ACPI_SFAN:
7580 if (level >= 0 && level <= 7) {
7581 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7582 return -EIO;
7583 } else
7584 return -EINVAL;
7585 break;
7587 case TPACPI_FAN_WR_ACPI_FANS:
7588 case TPACPI_FAN_WR_TPEC:
7589 if (!(level & TP_EC_FAN_AUTO) &&
7590 !(level & TP_EC_FAN_FULLSPEED) &&
7591 ((level < 0) || (level > 7)))
7592 return -EINVAL;
7594 /* safety net should the EC not support AUTO
7595 * or FULLSPEED mode bits and just ignore them */
7596 if (level & TP_EC_FAN_FULLSPEED)
7597 level |= 7; /* safety min speed 7 */
7598 else if (level & TP_EC_FAN_AUTO)
7599 level |= 4; /* safety min speed 4 */
7601 if (!acpi_ec_write(fan_status_offset, level))
7602 return -EIO;
7603 else
7604 tp_features.fan_ctrl_status_undef = 0;
7605 break;
7607 default:
7608 return -ENXIO;
7611 vdbg_printk(TPACPI_DBG_FAN,
7612 "fan control: set fan control register to 0x%02x\n", level);
7613 return 0;
7616 static int fan_set_level_safe(int level)
7618 int rc;
7620 if (!fan_control_allowed)
7621 return -EPERM;
7623 if (mutex_lock_killable(&fan_mutex))
7624 return -ERESTARTSYS;
7626 if (level == TPACPI_FAN_LAST_LEVEL)
7627 level = fan_control_desired_level;
7629 rc = fan_set_level(level);
7630 if (!rc)
7631 fan_update_desired_level(level);
7633 mutex_unlock(&fan_mutex);
7634 return rc;
7637 static int fan_set_enable(void)
7639 u8 s;
7640 int rc;
7642 if (!fan_control_allowed)
7643 return -EPERM;
7645 if (mutex_lock_killable(&fan_mutex))
7646 return -ERESTARTSYS;
7648 switch (fan_control_access_mode) {
7649 case TPACPI_FAN_WR_ACPI_FANS:
7650 case TPACPI_FAN_WR_TPEC:
7651 rc = fan_get_status(&s);
7652 if (rc < 0)
7653 break;
7655 /* Don't go out of emergency fan mode */
7656 if (s != 7) {
7657 s &= 0x07;
7658 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7661 if (!acpi_ec_write(fan_status_offset, s))
7662 rc = -EIO;
7663 else {
7664 tp_features.fan_ctrl_status_undef = 0;
7665 rc = 0;
7667 break;
7669 case TPACPI_FAN_WR_ACPI_SFAN:
7670 rc = fan_get_status(&s);
7671 if (rc < 0)
7672 break;
7674 s &= 0x07;
7676 /* Set fan to at least level 4 */
7677 s |= 4;
7679 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7680 rc = -EIO;
7681 else
7682 rc = 0;
7683 break;
7685 default:
7686 rc = -ENXIO;
7689 mutex_unlock(&fan_mutex);
7691 if (!rc)
7692 vdbg_printk(TPACPI_DBG_FAN,
7693 "fan control: set fan control register to 0x%02x\n",
7695 return rc;
7698 static int fan_set_disable(void)
7700 int rc;
7702 if (!fan_control_allowed)
7703 return -EPERM;
7705 if (mutex_lock_killable(&fan_mutex))
7706 return -ERESTARTSYS;
7708 rc = 0;
7709 switch (fan_control_access_mode) {
7710 case TPACPI_FAN_WR_ACPI_FANS:
7711 case TPACPI_FAN_WR_TPEC:
7712 if (!acpi_ec_write(fan_status_offset, 0x00))
7713 rc = -EIO;
7714 else {
7715 fan_control_desired_level = 0;
7716 tp_features.fan_ctrl_status_undef = 0;
7718 break;
7720 case TPACPI_FAN_WR_ACPI_SFAN:
7721 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7722 rc = -EIO;
7723 else
7724 fan_control_desired_level = 0;
7725 break;
7727 default:
7728 rc = -ENXIO;
7731 if (!rc)
7732 vdbg_printk(TPACPI_DBG_FAN,
7733 "fan control: set fan control register to 0\n");
7735 mutex_unlock(&fan_mutex);
7736 return rc;
7739 static int fan_set_speed(int speed)
7741 int rc;
7743 if (!fan_control_allowed)
7744 return -EPERM;
7746 if (mutex_lock_killable(&fan_mutex))
7747 return -ERESTARTSYS;
7749 rc = 0;
7750 switch (fan_control_access_mode) {
7751 case TPACPI_FAN_WR_ACPI_FANS:
7752 if (speed >= 0 && speed <= 65535) {
7753 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7754 speed, speed, speed))
7755 rc = -EIO;
7756 } else
7757 rc = -EINVAL;
7758 break;
7760 default:
7761 rc = -ENXIO;
7764 mutex_unlock(&fan_mutex);
7765 return rc;
7768 static void fan_watchdog_reset(void)
7770 static int fan_watchdog_active;
7772 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7773 return;
7775 if (fan_watchdog_active)
7776 cancel_delayed_work(&fan_watchdog_task);
7778 if (fan_watchdog_maxinterval > 0 &&
7779 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7780 fan_watchdog_active = 1;
7781 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7782 msecs_to_jiffies(fan_watchdog_maxinterval
7783 * 1000))) {
7784 printk(TPACPI_ERR
7785 "failed to queue the fan watchdog, "
7786 "watchdog will not trigger\n");
7788 } else
7789 fan_watchdog_active = 0;
7792 static void fan_watchdog_fire(struct work_struct *ignored)
7794 int rc;
7796 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7797 return;
7799 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7800 rc = fan_set_enable();
7801 if (rc < 0) {
7802 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7803 "will try again later...\n", -rc);
7804 /* reschedule for later */
7805 fan_watchdog_reset();
7810 * SYSFS fan layout: hwmon compatible (device)
7812 * pwm*_enable:
7813 * 0: "disengaged" mode
7814 * 1: manual mode
7815 * 2: native EC "auto" mode (recommended, hardware default)
7817 * pwm*: set speed in manual mode, ignored otherwise.
7818 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7819 * interpolation.
7821 * fan*_input: tachometer reading, RPM
7824 * SYSFS fan layout: extensions
7826 * fan_watchdog (driver):
7827 * fan watchdog interval in seconds, 0 disables (default), max 120
7830 /* sysfs fan pwm1_enable ----------------------------------------------- */
7831 static ssize_t fan_pwm1_enable_show(struct device *dev,
7832 struct device_attribute *attr,
7833 char *buf)
7835 int res, mode;
7836 u8 status;
7838 res = fan_get_status_safe(&status);
7839 if (res)
7840 return res;
7842 if (status & TP_EC_FAN_FULLSPEED) {
7843 mode = 0;
7844 } else if (status & TP_EC_FAN_AUTO) {
7845 mode = 2;
7846 } else
7847 mode = 1;
7849 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7852 static ssize_t fan_pwm1_enable_store(struct device *dev,
7853 struct device_attribute *attr,
7854 const char *buf, size_t count)
7856 unsigned long t;
7857 int res, level;
7859 if (parse_strtoul(buf, 2, &t))
7860 return -EINVAL;
7862 tpacpi_disclose_usertask("hwmon pwm1_enable",
7863 "set fan mode to %lu\n", t);
7865 switch (t) {
7866 case 0:
7867 level = TP_EC_FAN_FULLSPEED;
7868 break;
7869 case 1:
7870 level = TPACPI_FAN_LAST_LEVEL;
7871 break;
7872 case 2:
7873 level = TP_EC_FAN_AUTO;
7874 break;
7875 case 3:
7876 /* reserved for software-controlled auto mode */
7877 return -ENOSYS;
7878 default:
7879 return -EINVAL;
7882 res = fan_set_level_safe(level);
7883 if (res == -ENXIO)
7884 return -EINVAL;
7885 else if (res < 0)
7886 return res;
7888 fan_watchdog_reset();
7890 return count;
7893 static struct device_attribute dev_attr_fan_pwm1_enable =
7894 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7895 fan_pwm1_enable_show, fan_pwm1_enable_store);
7897 /* sysfs fan pwm1 ------------------------------------------------------ */
7898 static ssize_t fan_pwm1_show(struct device *dev,
7899 struct device_attribute *attr,
7900 char *buf)
7902 int res;
7903 u8 status;
7905 res = fan_get_status_safe(&status);
7906 if (res)
7907 return res;
7909 if ((status &
7910 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7911 status = fan_control_desired_level;
7913 if (status > 7)
7914 status = 7;
7916 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7919 static ssize_t fan_pwm1_store(struct device *dev,
7920 struct device_attribute *attr,
7921 const char *buf, size_t count)
7923 unsigned long s;
7924 int rc;
7925 u8 status, newlevel;
7927 if (parse_strtoul(buf, 255, &s))
7928 return -EINVAL;
7930 tpacpi_disclose_usertask("hwmon pwm1",
7931 "set fan speed to %lu\n", s);
7933 /* scale down from 0-255 to 0-7 */
7934 newlevel = (s >> 5) & 0x07;
7936 if (mutex_lock_killable(&fan_mutex))
7937 return -ERESTARTSYS;
7939 rc = fan_get_status(&status);
7940 if (!rc && (status &
7941 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7942 rc = fan_set_level(newlevel);
7943 if (rc == -ENXIO)
7944 rc = -EINVAL;
7945 else if (!rc) {
7946 fan_update_desired_level(newlevel);
7947 fan_watchdog_reset();
7951 mutex_unlock(&fan_mutex);
7952 return (rc)? rc : count;
7955 static struct device_attribute dev_attr_fan_pwm1 =
7956 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7957 fan_pwm1_show, fan_pwm1_store);
7959 /* sysfs fan fan1_input ------------------------------------------------ */
7960 static ssize_t fan_fan1_input_show(struct device *dev,
7961 struct device_attribute *attr,
7962 char *buf)
7964 int res;
7965 unsigned int speed;
7967 res = fan_get_speed(&speed);
7968 if (res < 0)
7969 return res;
7971 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7974 static struct device_attribute dev_attr_fan_fan1_input =
7975 __ATTR(fan1_input, S_IRUGO,
7976 fan_fan1_input_show, NULL);
7978 /* sysfs fan fan2_input ------------------------------------------------ */
7979 static ssize_t fan_fan2_input_show(struct device *dev,
7980 struct device_attribute *attr,
7981 char *buf)
7983 int res;
7984 unsigned int speed;
7986 res = fan2_get_speed(&speed);
7987 if (res < 0)
7988 return res;
7990 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7993 static struct device_attribute dev_attr_fan_fan2_input =
7994 __ATTR(fan2_input, S_IRUGO,
7995 fan_fan2_input_show, NULL);
7997 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7998 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7999 char *buf)
8001 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8004 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
8005 const char *buf, size_t count)
8007 unsigned long t;
8009 if (parse_strtoul(buf, 120, &t))
8010 return -EINVAL;
8012 if (!fan_control_allowed)
8013 return -EPERM;
8015 fan_watchdog_maxinterval = t;
8016 fan_watchdog_reset();
8018 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8020 return count;
8023 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8024 fan_fan_watchdog_show, fan_fan_watchdog_store);
8026 /* --------------------------------------------------------------------- */
8027 static struct attribute *fan_attributes[] = {
8028 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
8029 &dev_attr_fan_fan1_input.attr,
8030 NULL, /* for fan2_input */
8031 NULL
8034 static const struct attribute_group fan_attr_group = {
8035 .attrs = fan_attributes,
8038 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8039 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8041 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8042 { .vendor = PCI_VENDOR_ID_IBM, \
8043 .bios = TPACPI_MATCH_ANY, \
8044 .ec = TPID(__id1, __id2), \
8045 .quirks = __quirks }
8047 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8048 { .vendor = PCI_VENDOR_ID_LENOVO, \
8049 .bios = TPACPI_MATCH_ANY, \
8050 .ec = TPID(__id1, __id2), \
8051 .quirks = __quirks }
8053 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8054 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8055 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8056 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8057 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8058 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8061 #undef TPACPI_FAN_QL
8062 #undef TPACPI_FAN_QI
8064 static int __init fan_init(struct ibm_init_struct *iibm)
8066 int rc;
8067 unsigned long quirks;
8069 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8070 "initializing fan subdriver\n");
8072 mutex_init(&fan_mutex);
8073 fan_status_access_mode = TPACPI_FAN_NONE;
8074 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8075 fan_control_commands = 0;
8076 fan_watchdog_maxinterval = 0;
8077 tp_features.fan_ctrl_status_undef = 0;
8078 tp_features.second_fan = 0;
8079 fan_control_desired_level = 7;
8081 if (tpacpi_is_ibm()) {
8082 TPACPI_ACPIHANDLE_INIT(fans);
8083 TPACPI_ACPIHANDLE_INIT(gfan);
8084 TPACPI_ACPIHANDLE_INIT(sfan);
8087 quirks = tpacpi_check_quirks(fan_quirk_table,
8088 ARRAY_SIZE(fan_quirk_table));
8090 if (gfan_handle) {
8091 /* 570, 600e/x, 770e, 770x */
8092 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8093 } else {
8094 /* all other ThinkPads: note that even old-style
8095 * ThinkPad ECs supports the fan control register */
8096 if (likely(acpi_ec_read(fan_status_offset,
8097 &fan_control_initial_status))) {
8098 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8099 if (quirks & TPACPI_FAN_Q1)
8100 fan_quirk1_setup();
8101 if (quirks & TPACPI_FAN_2FAN) {
8102 tp_features.second_fan = 1;
8103 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8104 "secondary fan support enabled\n");
8106 } else {
8107 printk(TPACPI_ERR
8108 "ThinkPad ACPI EC access misbehaving, "
8109 "fan status and control unavailable\n");
8110 return 1;
8114 if (sfan_handle) {
8115 /* 570, 770x-JL */
8116 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8117 fan_control_commands |=
8118 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8119 } else {
8120 if (!gfan_handle) {
8121 /* gfan without sfan means no fan control */
8122 /* all other models implement TP EC 0x2f control */
8124 if (fans_handle) {
8125 /* X31, X40, X41 */
8126 fan_control_access_mode =
8127 TPACPI_FAN_WR_ACPI_FANS;
8128 fan_control_commands |=
8129 TPACPI_FAN_CMD_SPEED |
8130 TPACPI_FAN_CMD_LEVEL |
8131 TPACPI_FAN_CMD_ENABLE;
8132 } else {
8133 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8134 fan_control_commands |=
8135 TPACPI_FAN_CMD_LEVEL |
8136 TPACPI_FAN_CMD_ENABLE;
8141 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8142 "fan is %s, modes %d, %d\n",
8143 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8144 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8145 fan_status_access_mode, fan_control_access_mode);
8147 /* fan control master switch */
8148 if (!fan_control_allowed) {
8149 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8150 fan_control_commands = 0;
8151 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8152 "fan control features disabled by parameter\n");
8155 /* update fan_control_desired_level */
8156 if (fan_status_access_mode != TPACPI_FAN_NONE)
8157 fan_get_status_safe(NULL);
8159 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8160 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8161 if (tp_features.second_fan) {
8162 /* attach second fan tachometer */
8163 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8164 &dev_attr_fan_fan2_input.attr;
8166 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8167 &fan_attr_group);
8168 if (rc < 0)
8169 return rc;
8171 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8172 &driver_attr_fan_watchdog);
8173 if (rc < 0) {
8174 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8175 &fan_attr_group);
8176 return rc;
8178 return 0;
8179 } else
8180 return 1;
8183 static void fan_exit(void)
8185 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8186 "cancelling any pending fan watchdog tasks\n");
8188 /* FIXME: can we really do this unconditionally? */
8189 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8190 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8191 &driver_attr_fan_watchdog);
8193 cancel_delayed_work(&fan_watchdog_task);
8194 flush_workqueue(tpacpi_wq);
8197 static void fan_suspend(pm_message_t state)
8199 int rc;
8201 if (!fan_control_allowed)
8202 return;
8204 /* Store fan status in cache */
8205 fan_control_resume_level = 0;
8206 rc = fan_get_status_safe(&fan_control_resume_level);
8207 if (rc < 0)
8208 printk(TPACPI_NOTICE
8209 "failed to read fan level for later "
8210 "restore during resume: %d\n", rc);
8212 /* if it is undefined, don't attempt to restore it.
8213 * KEEP THIS LAST */
8214 if (tp_features.fan_ctrl_status_undef)
8215 fan_control_resume_level = 0;
8218 static void fan_resume(void)
8220 u8 current_level = 7;
8221 bool do_set = false;
8222 int rc;
8224 /* DSDT *always* updates status on resume */
8225 tp_features.fan_ctrl_status_undef = 0;
8227 if (!fan_control_allowed ||
8228 !fan_control_resume_level ||
8229 (fan_get_status_safe(&current_level) < 0))
8230 return;
8232 switch (fan_control_access_mode) {
8233 case TPACPI_FAN_WR_ACPI_SFAN:
8234 /* never decrease fan level */
8235 do_set = (fan_control_resume_level > current_level);
8236 break;
8237 case TPACPI_FAN_WR_ACPI_FANS:
8238 case TPACPI_FAN_WR_TPEC:
8239 /* never decrease fan level, scale is:
8240 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8242 * We expect the firmware to set either 7 or AUTO, but we
8243 * handle FULLSPEED out of paranoia.
8245 * So, we can safely only restore FULLSPEED or 7, anything
8246 * else could slow the fan. Restoring AUTO is useless, at
8247 * best that's exactly what the DSDT already set (it is the
8248 * slower it uses).
8250 * Always keep in mind that the DSDT *will* have set the
8251 * fans to what the vendor supposes is the best level. We
8252 * muck with it only to speed the fan up.
8254 if (fan_control_resume_level != 7 &&
8255 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8256 return;
8257 else
8258 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8259 (current_level != fan_control_resume_level);
8260 break;
8261 default:
8262 return;
8264 if (do_set) {
8265 printk(TPACPI_NOTICE
8266 "restoring fan level to 0x%02x\n",
8267 fan_control_resume_level);
8268 rc = fan_set_level_safe(fan_control_resume_level);
8269 if (rc < 0)
8270 printk(TPACPI_NOTICE
8271 "failed to restore fan level: %d\n", rc);
8275 static int fan_read(struct seq_file *m)
8277 int rc;
8278 u8 status;
8279 unsigned int speed = 0;
8281 switch (fan_status_access_mode) {
8282 case TPACPI_FAN_RD_ACPI_GFAN:
8283 /* 570, 600e/x, 770e, 770x */
8284 rc = fan_get_status_safe(&status);
8285 if (rc < 0)
8286 return rc;
8288 seq_printf(m, "status:\t\t%s\n"
8289 "level:\t\t%d\n",
8290 (status != 0) ? "enabled" : "disabled", status);
8291 break;
8293 case TPACPI_FAN_RD_TPEC:
8294 /* all except 570, 600e/x, 770e, 770x */
8295 rc = fan_get_status_safe(&status);
8296 if (rc < 0)
8297 return rc;
8299 seq_printf(m, "status:\t\t%s\n",
8300 (status != 0) ? "enabled" : "disabled");
8302 rc = fan_get_speed(&speed);
8303 if (rc < 0)
8304 return rc;
8306 seq_printf(m, "speed:\t\t%d\n", speed);
8308 if (status & TP_EC_FAN_FULLSPEED)
8309 /* Disengaged mode takes precedence */
8310 seq_printf(m, "level:\t\tdisengaged\n");
8311 else if (status & TP_EC_FAN_AUTO)
8312 seq_printf(m, "level:\t\tauto\n");
8313 else
8314 seq_printf(m, "level:\t\t%d\n", status);
8315 break;
8317 case TPACPI_FAN_NONE:
8318 default:
8319 seq_printf(m, "status:\t\tnot supported\n");
8322 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8323 seq_printf(m, "commands:\tlevel <level>");
8325 switch (fan_control_access_mode) {
8326 case TPACPI_FAN_WR_ACPI_SFAN:
8327 seq_printf(m, " (<level> is 0-7)\n");
8328 break;
8330 default:
8331 seq_printf(m, " (<level> is 0-7, "
8332 "auto, disengaged, full-speed)\n");
8333 break;
8337 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8338 seq_printf(m, "commands:\tenable, disable\n"
8339 "commands:\twatchdog <timeout> (<timeout> "
8340 "is 0 (off), 1-120 (seconds))\n");
8342 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8343 seq_printf(m, "commands:\tspeed <speed>"
8344 " (<speed> is 0-65535)\n");
8346 return 0;
8349 static int fan_write_cmd_level(const char *cmd, int *rc)
8351 int level;
8353 if (strlencmp(cmd, "level auto") == 0)
8354 level = TP_EC_FAN_AUTO;
8355 else if ((strlencmp(cmd, "level disengaged") == 0) |
8356 (strlencmp(cmd, "level full-speed") == 0))
8357 level = TP_EC_FAN_FULLSPEED;
8358 else if (sscanf(cmd, "level %d", &level) != 1)
8359 return 0;
8361 *rc = fan_set_level_safe(level);
8362 if (*rc == -ENXIO)
8363 printk(TPACPI_ERR "level command accepted for unsupported "
8364 "access mode %d", fan_control_access_mode);
8365 else if (!*rc)
8366 tpacpi_disclose_usertask("procfs fan",
8367 "set level to %d\n", level);
8369 return 1;
8372 static int fan_write_cmd_enable(const char *cmd, int *rc)
8374 if (strlencmp(cmd, "enable") != 0)
8375 return 0;
8377 *rc = fan_set_enable();
8378 if (*rc == -ENXIO)
8379 printk(TPACPI_ERR "enable command accepted for unsupported "
8380 "access mode %d", fan_control_access_mode);
8381 else if (!*rc)
8382 tpacpi_disclose_usertask("procfs fan", "enable\n");
8384 return 1;
8387 static int fan_write_cmd_disable(const char *cmd, int *rc)
8389 if (strlencmp(cmd, "disable") != 0)
8390 return 0;
8392 *rc = fan_set_disable();
8393 if (*rc == -ENXIO)
8394 printk(TPACPI_ERR "disable command accepted for unsupported "
8395 "access mode %d", fan_control_access_mode);
8396 else if (!*rc)
8397 tpacpi_disclose_usertask("procfs fan", "disable\n");
8399 return 1;
8402 static int fan_write_cmd_speed(const char *cmd, int *rc)
8404 int speed;
8406 /* TODO:
8407 * Support speed <low> <medium> <high> ? */
8409 if (sscanf(cmd, "speed %d", &speed) != 1)
8410 return 0;
8412 *rc = fan_set_speed(speed);
8413 if (*rc == -ENXIO)
8414 printk(TPACPI_ERR "speed command accepted for unsupported "
8415 "access mode %d", fan_control_access_mode);
8416 else if (!*rc)
8417 tpacpi_disclose_usertask("procfs fan",
8418 "set speed to %d\n", speed);
8420 return 1;
8423 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8425 int interval;
8427 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8428 return 0;
8430 if (interval < 0 || interval > 120)
8431 *rc = -EINVAL;
8432 else {
8433 fan_watchdog_maxinterval = interval;
8434 tpacpi_disclose_usertask("procfs fan",
8435 "set watchdog timer to %d\n",
8436 interval);
8439 return 1;
8442 static int fan_write(char *buf)
8444 char *cmd;
8445 int rc = 0;
8447 while (!rc && (cmd = next_cmd(&buf))) {
8448 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8449 fan_write_cmd_level(cmd, &rc)) &&
8450 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8451 (fan_write_cmd_enable(cmd, &rc) ||
8452 fan_write_cmd_disable(cmd, &rc) ||
8453 fan_write_cmd_watchdog(cmd, &rc))) &&
8454 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8455 fan_write_cmd_speed(cmd, &rc))
8457 rc = -EINVAL;
8458 else if (!rc)
8459 fan_watchdog_reset();
8462 return rc;
8465 static struct ibm_struct fan_driver_data = {
8466 .name = "fan",
8467 .read = fan_read,
8468 .write = fan_write,
8469 .exit = fan_exit,
8470 .suspend = fan_suspend,
8471 .resume = fan_resume,
8474 /****************************************************************************
8475 ****************************************************************************
8477 * Infrastructure
8479 ****************************************************************************
8480 ****************************************************************************/
8483 * HKEY event callout for other subdrivers go here
8484 * (yes, it is ugly, but it is quick, safe, and gets the job done
8486 static void tpacpi_driver_event(const unsigned int hkey_event)
8488 if (ibm_backlight_device) {
8489 switch (hkey_event) {
8490 case TP_HKEY_EV_BRGHT_UP:
8491 case TP_HKEY_EV_BRGHT_DOWN:
8492 tpacpi_brightness_notify_change();
8495 if (alsa_card) {
8496 switch (hkey_event) {
8497 case TP_HKEY_EV_VOL_UP:
8498 case TP_HKEY_EV_VOL_DOWN:
8499 case TP_HKEY_EV_VOL_MUTE:
8500 volume_alsa_notify_change();
8505 static void hotkey_driver_event(const unsigned int scancode)
8507 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8510 /* sysfs name ---------------------------------------------------------- */
8511 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8512 struct device_attribute *attr,
8513 char *buf)
8515 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8518 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8519 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8521 /* --------------------------------------------------------------------- */
8523 /* /proc support */
8524 static struct proc_dir_entry *proc_dir;
8527 * Module and infrastructure proble, init and exit handling
8530 static int force_load;
8532 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8533 static const char * __init str_supported(int is_supported)
8535 static char text_unsupported[] __initdata = "not supported";
8537 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8539 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8541 static void ibm_exit(struct ibm_struct *ibm)
8543 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8545 list_del_init(&ibm->all_drivers);
8547 if (ibm->flags.acpi_notify_installed) {
8548 dbg_printk(TPACPI_DBG_EXIT,
8549 "%s: acpi_remove_notify_handler\n", ibm->name);
8550 BUG_ON(!ibm->acpi);
8551 acpi_remove_notify_handler(*ibm->acpi->handle,
8552 ibm->acpi->type,
8553 dispatch_acpi_notify);
8554 ibm->flags.acpi_notify_installed = 0;
8555 ibm->flags.acpi_notify_installed = 0;
8558 if (ibm->flags.proc_created) {
8559 dbg_printk(TPACPI_DBG_EXIT,
8560 "%s: remove_proc_entry\n", ibm->name);
8561 remove_proc_entry(ibm->name, proc_dir);
8562 ibm->flags.proc_created = 0;
8565 if (ibm->flags.acpi_driver_registered) {
8566 dbg_printk(TPACPI_DBG_EXIT,
8567 "%s: acpi_bus_unregister_driver\n", ibm->name);
8568 BUG_ON(!ibm->acpi);
8569 acpi_bus_unregister_driver(ibm->acpi->driver);
8570 kfree(ibm->acpi->driver);
8571 ibm->acpi->driver = NULL;
8572 ibm->flags.acpi_driver_registered = 0;
8575 if (ibm->flags.init_called && ibm->exit) {
8576 ibm->exit();
8577 ibm->flags.init_called = 0;
8580 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8583 static int __init ibm_init(struct ibm_init_struct *iibm)
8585 int ret;
8586 struct ibm_struct *ibm = iibm->data;
8587 struct proc_dir_entry *entry;
8589 BUG_ON(ibm == NULL);
8591 INIT_LIST_HEAD(&ibm->all_drivers);
8593 if (ibm->flags.experimental && !experimental)
8594 return 0;
8596 dbg_printk(TPACPI_DBG_INIT,
8597 "probing for %s\n", ibm->name);
8599 if (iibm->init) {
8600 ret = iibm->init(iibm);
8601 if (ret > 0)
8602 return 0; /* probe failed */
8603 if (ret)
8604 return ret;
8606 ibm->flags.init_called = 1;
8609 if (ibm->acpi) {
8610 if (ibm->acpi->hid) {
8611 ret = register_tpacpi_subdriver(ibm);
8612 if (ret)
8613 goto err_out;
8616 if (ibm->acpi->notify) {
8617 ret = setup_acpi_notify(ibm);
8618 if (ret == -ENODEV) {
8619 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8620 ibm->name);
8621 ret = 0;
8622 goto err_out;
8624 if (ret < 0)
8625 goto err_out;
8629 dbg_printk(TPACPI_DBG_INIT,
8630 "%s installed\n", ibm->name);
8632 if (ibm->read) {
8633 mode_t mode = iibm->base_procfs_mode;
8635 if (!mode)
8636 mode = S_IRUGO;
8637 if (ibm->write)
8638 mode |= S_IWUSR;
8639 entry = proc_create_data(ibm->name, mode, proc_dir,
8640 &dispatch_proc_fops, ibm);
8641 if (!entry) {
8642 printk(TPACPI_ERR "unable to create proc entry %s\n",
8643 ibm->name);
8644 ret = -ENODEV;
8645 goto err_out;
8647 ibm->flags.proc_created = 1;
8650 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8652 return 0;
8654 err_out:
8655 dbg_printk(TPACPI_DBG_INIT,
8656 "%s: at error exit path with result %d\n",
8657 ibm->name, ret);
8659 ibm_exit(ibm);
8660 return (ret < 0)? ret : 0;
8663 /* Probing */
8665 static bool __pure __init tpacpi_is_fw_digit(const char c)
8667 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8670 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8671 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8672 const char t)
8674 return s && strlen(s) >= 8 &&
8675 tpacpi_is_fw_digit(s[0]) &&
8676 tpacpi_is_fw_digit(s[1]) &&
8677 s[2] == t && s[3] == 'T' &&
8678 tpacpi_is_fw_digit(s[4]) &&
8679 tpacpi_is_fw_digit(s[5]) &&
8680 s[6] == 'W' && s[7] == 'W';
8683 /* returns 0 - probe ok, or < 0 - probe error.
8684 * Probe ok doesn't mean thinkpad found.
8685 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8686 static int __must_check __init get_thinkpad_model_data(
8687 struct thinkpad_id_data *tp)
8689 const struct dmi_device *dev = NULL;
8690 char ec_fw_string[18];
8691 char const *s;
8693 if (!tp)
8694 return -EINVAL;
8696 memset(tp, 0, sizeof(*tp));
8698 if (dmi_name_in_vendors("IBM"))
8699 tp->vendor = PCI_VENDOR_ID_IBM;
8700 else if (dmi_name_in_vendors("LENOVO"))
8701 tp->vendor = PCI_VENDOR_ID_LENOVO;
8702 else
8703 return 0;
8705 s = dmi_get_system_info(DMI_BIOS_VERSION);
8706 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8707 if (s && !tp->bios_version_str)
8708 return -ENOMEM;
8710 /* Really ancient ThinkPad 240X will fail this, which is fine */
8711 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8712 return 0;
8714 tp->bios_model = tp->bios_version_str[0]
8715 | (tp->bios_version_str[1] << 8);
8716 tp->bios_release = (tp->bios_version_str[4] << 8)
8717 | tp->bios_version_str[5];
8720 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8721 * X32 or newer, all Z series; Some models must have an
8722 * up-to-date BIOS or they will not be detected.
8724 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8726 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8727 if (sscanf(dev->name,
8728 "IBM ThinkPad Embedded Controller -[%17c",
8729 ec_fw_string) == 1) {
8730 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8731 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8733 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8734 if (!tp->ec_version_str)
8735 return -ENOMEM;
8737 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8738 tp->ec_model = ec_fw_string[0]
8739 | (ec_fw_string[1] << 8);
8740 tp->ec_release = (ec_fw_string[4] << 8)
8741 | ec_fw_string[5];
8742 } else {
8743 printk(TPACPI_NOTICE
8744 "ThinkPad firmware release %s "
8745 "doesn't match the known patterns\n",
8746 ec_fw_string);
8747 printk(TPACPI_NOTICE
8748 "please report this to %s\n",
8749 TPACPI_MAIL);
8751 break;
8755 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8756 if (s && !strnicmp(s, "ThinkPad", 8)) {
8757 tp->model_str = kstrdup(s, GFP_KERNEL);
8758 if (!tp->model_str)
8759 return -ENOMEM;
8762 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8763 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8764 if (s && !tp->nummodel_str)
8765 return -ENOMEM;
8767 return 0;
8770 static int __init probe_for_thinkpad(void)
8772 int is_thinkpad;
8774 if (acpi_disabled)
8775 return -ENODEV;
8777 /* It would be dangerous to run the driver in this case */
8778 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8779 return -ENODEV;
8782 * Non-ancient models have better DMI tagging, but very old models
8783 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8785 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8786 (thinkpad_id.ec_model != 0) ||
8787 tpacpi_is_fw_known();
8789 /* The EC handler is required */
8790 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
8791 if (!ec_handle) {
8792 if (is_thinkpad)
8793 printk(TPACPI_ERR
8794 "Not yet supported ThinkPad detected!\n");
8795 return -ENODEV;
8798 if (!is_thinkpad && !force_load)
8799 return -ENODEV;
8801 return 0;
8804 static void __init thinkpad_acpi_init_banner(void)
8806 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8807 printk(TPACPI_INFO "%s\n", TPACPI_URL);
8809 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
8810 (thinkpad_id.bios_version_str) ?
8811 thinkpad_id.bios_version_str : "unknown",
8812 (thinkpad_id.ec_version_str) ?
8813 thinkpad_id.ec_version_str : "unknown");
8815 BUG_ON(!thinkpad_id.vendor);
8817 if (thinkpad_id.model_str)
8818 printk(TPACPI_INFO "%s %s, model %s\n",
8819 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8820 "IBM" : ((thinkpad_id.vendor ==
8821 PCI_VENDOR_ID_LENOVO) ?
8822 "Lenovo" : "Unknown vendor"),
8823 thinkpad_id.model_str,
8824 (thinkpad_id.nummodel_str) ?
8825 thinkpad_id.nummodel_str : "unknown");
8828 /* Module init, exit, parameters */
8830 static struct ibm_init_struct ibms_init[] __initdata = {
8832 .data = &thinkpad_acpi_driver_data,
8835 .init = hotkey_init,
8836 .data = &hotkey_driver_data,
8839 .init = bluetooth_init,
8840 .data = &bluetooth_driver_data,
8843 .init = wan_init,
8844 .data = &wan_driver_data,
8847 .init = uwb_init,
8848 .data = &uwb_driver_data,
8850 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8852 .init = video_init,
8853 .base_procfs_mode = S_IRUSR,
8854 .data = &video_driver_data,
8856 #endif
8858 .init = light_init,
8859 .data = &light_driver_data,
8862 .init = cmos_init,
8863 .data = &cmos_driver_data,
8866 .init = led_init,
8867 .data = &led_driver_data,
8870 .init = beep_init,
8871 .data = &beep_driver_data,
8874 .init = thermal_init,
8875 .data = &thermal_driver_data,
8878 .data = &ecdump_driver_data,
8881 .init = brightness_init,
8882 .data = &brightness_driver_data,
8885 .init = volume_init,
8886 .data = &volume_driver_data,
8889 .init = fan_init,
8890 .data = &fan_driver_data,
8894 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8896 unsigned int i;
8897 struct ibm_struct *ibm;
8899 if (!kp || !kp->name || !val)
8900 return -EINVAL;
8902 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8903 ibm = ibms_init[i].data;
8904 WARN_ON(ibm == NULL);
8906 if (!ibm || !ibm->name)
8907 continue;
8909 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8910 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8911 return -ENOSPC;
8912 strcpy(ibms_init[i].param, val);
8913 strcat(ibms_init[i].param, ",");
8914 return 0;
8918 return -EINVAL;
8921 module_param(experimental, int, 0444);
8922 MODULE_PARM_DESC(experimental,
8923 "Enables experimental features when non-zero");
8925 module_param_named(debug, dbg_level, uint, 0);
8926 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8928 module_param(force_load, bool, 0444);
8929 MODULE_PARM_DESC(force_load,
8930 "Attempts to load the driver even on a "
8931 "mis-identified ThinkPad when true");
8933 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8934 MODULE_PARM_DESC(fan_control,
8935 "Enables setting fan parameters features when true");
8937 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8938 MODULE_PARM_DESC(brightness_mode,
8939 "Selects brightness control strategy: "
8940 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8942 module_param(brightness_enable, uint, 0444);
8943 MODULE_PARM_DESC(brightness_enable,
8944 "Enables backlight control when 1, disables when 0");
8946 module_param(hotkey_report_mode, uint, 0444);
8947 MODULE_PARM_DESC(hotkey_report_mode,
8948 "used for backwards compatibility with userspace, "
8949 "see documentation");
8951 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8952 module_param_named(volume_mode, volume_mode, uint, 0444);
8953 MODULE_PARM_DESC(volume_mode,
8954 "Selects volume control strategy: "
8955 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8957 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8958 MODULE_PARM_DESC(volume_capabilities,
8959 "Selects the mixer capabilites: "
8960 "0=auto, 1=volume and mute, 2=mute only");
8962 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8963 MODULE_PARM_DESC(volume_control,
8964 "Enables software override for the console audio "
8965 "control when true");
8967 /* ALSA module API parameters */
8968 module_param_named(index, alsa_index, int, 0444);
8969 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8970 module_param_named(id, alsa_id, charp, 0444);
8971 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8972 module_param_named(enable, alsa_enable, bool, 0444);
8973 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8974 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8976 #define TPACPI_PARAM(feature) \
8977 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8978 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8979 "at module load, see documentation")
8981 TPACPI_PARAM(hotkey);
8982 TPACPI_PARAM(bluetooth);
8983 TPACPI_PARAM(video);
8984 TPACPI_PARAM(light);
8985 TPACPI_PARAM(cmos);
8986 TPACPI_PARAM(led);
8987 TPACPI_PARAM(beep);
8988 TPACPI_PARAM(ecdump);
8989 TPACPI_PARAM(brightness);
8990 TPACPI_PARAM(volume);
8991 TPACPI_PARAM(fan);
8993 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8994 module_param(dbg_wlswemul, uint, 0444);
8995 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8996 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8997 MODULE_PARM_DESC(wlsw_state,
8998 "Initial state of the emulated WLSW switch");
9000 module_param(dbg_bluetoothemul, uint, 0444);
9001 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9002 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9003 MODULE_PARM_DESC(bluetooth_state,
9004 "Initial state of the emulated bluetooth switch");
9006 module_param(dbg_wwanemul, uint, 0444);
9007 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9008 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9009 MODULE_PARM_DESC(wwan_state,
9010 "Initial state of the emulated WWAN switch");
9012 module_param(dbg_uwbemul, uint, 0444);
9013 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9014 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9015 MODULE_PARM_DESC(uwb_state,
9016 "Initial state of the emulated UWB switch");
9017 #endif
9019 static void thinkpad_acpi_module_exit(void)
9021 struct ibm_struct *ibm, *itmp;
9023 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9025 list_for_each_entry_safe_reverse(ibm, itmp,
9026 &tpacpi_all_drivers,
9027 all_drivers) {
9028 ibm_exit(ibm);
9031 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9033 if (tpacpi_inputdev) {
9034 if (tp_features.input_device_registered)
9035 input_unregister_device(tpacpi_inputdev);
9036 else
9037 input_free_device(tpacpi_inputdev);
9040 if (tpacpi_hwmon)
9041 hwmon_device_unregister(tpacpi_hwmon);
9043 if (tp_features.sensors_pdev_attrs_registered)
9044 device_remove_file(&tpacpi_sensors_pdev->dev,
9045 &dev_attr_thinkpad_acpi_pdev_name);
9046 if (tpacpi_sensors_pdev)
9047 platform_device_unregister(tpacpi_sensors_pdev);
9048 if (tpacpi_pdev)
9049 platform_device_unregister(tpacpi_pdev);
9051 if (tp_features.sensors_pdrv_attrs_registered)
9052 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9053 if (tp_features.platform_drv_attrs_registered)
9054 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9056 if (tp_features.sensors_pdrv_registered)
9057 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9059 if (tp_features.platform_drv_registered)
9060 platform_driver_unregister(&tpacpi_pdriver);
9062 if (proc_dir)
9063 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9065 if (tpacpi_wq)
9066 destroy_workqueue(tpacpi_wq);
9068 kfree(thinkpad_id.bios_version_str);
9069 kfree(thinkpad_id.ec_version_str);
9070 kfree(thinkpad_id.model_str);
9074 static int __init thinkpad_acpi_module_init(void)
9076 int ret, i;
9078 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9080 /* Parameter checking */
9081 if (hotkey_report_mode > 2)
9082 return -EINVAL;
9084 /* Driver-level probe */
9086 ret = get_thinkpad_model_data(&thinkpad_id);
9087 if (ret) {
9088 printk(TPACPI_ERR
9089 "unable to get DMI data: %d\n", ret);
9090 thinkpad_acpi_module_exit();
9091 return ret;
9093 ret = probe_for_thinkpad();
9094 if (ret) {
9095 thinkpad_acpi_module_exit();
9096 return ret;
9099 /* Driver initialization */
9101 thinkpad_acpi_init_banner();
9102 tpacpi_check_outdated_fw();
9104 TPACPI_ACPIHANDLE_INIT(ecrd);
9105 TPACPI_ACPIHANDLE_INIT(ecwr);
9107 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9108 if (!tpacpi_wq) {
9109 thinkpad_acpi_module_exit();
9110 return -ENOMEM;
9113 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9114 if (!proc_dir) {
9115 printk(TPACPI_ERR
9116 "unable to create proc dir " TPACPI_PROC_DIR);
9117 thinkpad_acpi_module_exit();
9118 return -ENODEV;
9121 ret = platform_driver_register(&tpacpi_pdriver);
9122 if (ret) {
9123 printk(TPACPI_ERR
9124 "unable to register main platform driver\n");
9125 thinkpad_acpi_module_exit();
9126 return ret;
9128 tp_features.platform_drv_registered = 1;
9130 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9131 if (ret) {
9132 printk(TPACPI_ERR
9133 "unable to register hwmon platform driver\n");
9134 thinkpad_acpi_module_exit();
9135 return ret;
9137 tp_features.sensors_pdrv_registered = 1;
9139 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9140 if (!ret) {
9141 tp_features.platform_drv_attrs_registered = 1;
9142 ret = tpacpi_create_driver_attributes(
9143 &tpacpi_hwmon_pdriver.driver);
9145 if (ret) {
9146 printk(TPACPI_ERR
9147 "unable to create sysfs driver attributes\n");
9148 thinkpad_acpi_module_exit();
9149 return ret;
9151 tp_features.sensors_pdrv_attrs_registered = 1;
9154 /* Device initialization */
9155 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9156 NULL, 0);
9157 if (IS_ERR(tpacpi_pdev)) {
9158 ret = PTR_ERR(tpacpi_pdev);
9159 tpacpi_pdev = NULL;
9160 printk(TPACPI_ERR "unable to register platform device\n");
9161 thinkpad_acpi_module_exit();
9162 return ret;
9164 tpacpi_sensors_pdev = platform_device_register_simple(
9165 TPACPI_HWMON_DRVR_NAME,
9166 -1, NULL, 0);
9167 if (IS_ERR(tpacpi_sensors_pdev)) {
9168 ret = PTR_ERR(tpacpi_sensors_pdev);
9169 tpacpi_sensors_pdev = NULL;
9170 printk(TPACPI_ERR
9171 "unable to register hwmon platform device\n");
9172 thinkpad_acpi_module_exit();
9173 return ret;
9175 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9176 &dev_attr_thinkpad_acpi_pdev_name);
9177 if (ret) {
9178 printk(TPACPI_ERR
9179 "unable to create sysfs hwmon device attributes\n");
9180 thinkpad_acpi_module_exit();
9181 return ret;
9183 tp_features.sensors_pdev_attrs_registered = 1;
9184 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9185 if (IS_ERR(tpacpi_hwmon)) {
9186 ret = PTR_ERR(tpacpi_hwmon);
9187 tpacpi_hwmon = NULL;
9188 printk(TPACPI_ERR "unable to register hwmon device\n");
9189 thinkpad_acpi_module_exit();
9190 return ret;
9192 mutex_init(&tpacpi_inputdev_send_mutex);
9193 tpacpi_inputdev = input_allocate_device();
9194 if (!tpacpi_inputdev) {
9195 printk(TPACPI_ERR "unable to allocate input device\n");
9196 thinkpad_acpi_module_exit();
9197 return -ENOMEM;
9198 } else {
9199 /* Prepare input device, but don't register */
9200 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9201 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9202 tpacpi_inputdev->id.bustype = BUS_HOST;
9203 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9204 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9205 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9206 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9209 /* Init subdriver dependencies */
9210 tpacpi_detect_brightness_capabilities();
9212 /* Init subdrivers */
9213 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9214 ret = ibm_init(&ibms_init[i]);
9215 if (ret >= 0 && *ibms_init[i].param)
9216 ret = ibms_init[i].data->write(ibms_init[i].param);
9217 if (ret < 0) {
9218 thinkpad_acpi_module_exit();
9219 return ret;
9223 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9225 ret = input_register_device(tpacpi_inputdev);
9226 if (ret < 0) {
9227 printk(TPACPI_ERR "unable to register input device\n");
9228 thinkpad_acpi_module_exit();
9229 return ret;
9230 } else {
9231 tp_features.input_device_registered = 1;
9234 return 0;
9237 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9240 * This will autoload the driver in almost every ThinkPad
9241 * in widespread use.
9243 * Only _VERY_ old models, like the 240, 240x and 570 lack
9244 * the HKEY event interface.
9246 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9249 * DMI matching for module autoloading
9251 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9252 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9254 * Only models listed in thinkwiki will be supported, so add yours
9255 * if it is not there yet.
9257 #define IBM_BIOS_MODULE_ALIAS(__type) \
9258 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9260 /* Ancient thinkpad BIOSes have to be identified by
9261 * BIOS type or model number, and there are far less
9262 * BIOS types than model numbers... */
9263 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9265 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9266 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9267 MODULE_DESCRIPTION(TPACPI_DESC);
9268 MODULE_VERSION(TPACPI_VERSION);
9269 MODULE_LICENSE("GPL");
9271 module_init(thinkpad_acpi_module_init);
9272 module_exit(thinkpad_acpi_module_exit);