thinkpad-acpi: acpi_evalf fixes
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob10be7369410a2e6f28389288b368eeffee94f362
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 acpi_handle *object##_parent = &parent##_handle; \
519 static char *object##_path; \
520 static char *object##_paths[] = { 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), &object##_path)
677 static void drv_acpi_handle_init(char *name,
678 acpi_handle *handle, acpi_handle parent,
679 char **paths, int num_paths, char **path)
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 *path = paths[i];
691 dbg_printk(TPACPI_DBG_INIT,
692 "Found ACPI handle %s for %s\n",
693 *path, name);
694 return;
698 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
699 name);
700 *handle = NULL;
703 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
704 u32 level, void *context, void **return_value)
706 *(acpi_handle *)return_value = handle;
708 return AE_CTRL_TERMINATE;
711 static void __init tpacpi_acpi_handle_locate(const char *name,
712 const char *hid,
713 acpi_handle *handle)
715 acpi_status status;
716 acpi_handle device_found;
718 BUG_ON(!name || !hid || !handle);
719 vdbg_printk(TPACPI_DBG_INIT,
720 "trying to locate ACPI handle for %s, using HID %s\n",
721 name, hid);
723 memset(&device_found, 0, sizeof(device_found));
724 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
725 (void *)name, &device_found);
727 *handle = NULL;
729 if (ACPI_SUCCESS(status)) {
730 *handle = device_found;
731 dbg_printk(TPACPI_DBG_INIT,
732 "Found ACPI handle for %s\n", name);
733 } else {
734 vdbg_printk(TPACPI_DBG_INIT,
735 "Could not locate an ACPI handle for %s: %s\n",
736 name, acpi_format_exception(status));
740 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
742 struct ibm_struct *ibm = data;
744 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
745 return;
747 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
748 return;
750 ibm->acpi->notify(ibm, event);
753 static int __init setup_acpi_notify(struct ibm_struct *ibm)
755 acpi_status status;
756 int rc;
758 BUG_ON(!ibm->acpi);
760 if (!*ibm->acpi->handle)
761 return 0;
763 vdbg_printk(TPACPI_DBG_INIT,
764 "setting up ACPI notify for %s\n", ibm->name);
766 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
767 if (rc < 0) {
768 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
769 ibm->name, rc);
770 return -ENODEV;
773 ibm->acpi->device->driver_data = ibm;
774 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
775 TPACPI_ACPI_EVENT_PREFIX,
776 ibm->name);
778 status = acpi_install_notify_handler(*ibm->acpi->handle,
779 ibm->acpi->type, dispatch_acpi_notify, ibm);
780 if (ACPI_FAILURE(status)) {
781 if (status == AE_ALREADY_EXISTS) {
782 printk(TPACPI_NOTICE
783 "another device driver is already "
784 "handling %s events\n", ibm->name);
785 } else {
786 printk(TPACPI_ERR
787 "acpi_install_notify_handler(%s) failed: %d\n",
788 ibm->name, status);
790 return -ENODEV;
792 ibm->flags.acpi_notify_installed = 1;
793 return 0;
796 static int __init tpacpi_device_add(struct acpi_device *device)
798 return 0;
801 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
803 int rc;
805 dbg_printk(TPACPI_DBG_INIT,
806 "registering %s as an ACPI driver\n", ibm->name);
808 BUG_ON(!ibm->acpi);
810 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
811 if (!ibm->acpi->driver) {
812 printk(TPACPI_ERR
813 "failed to allocate memory for ibm->acpi->driver\n");
814 return -ENOMEM;
817 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
818 ibm->acpi->driver->ids = ibm->acpi->hid;
820 ibm->acpi->driver->ops.add = &tpacpi_device_add;
822 rc = acpi_bus_register_driver(ibm->acpi->driver);
823 if (rc < 0) {
824 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
825 ibm->name, rc);
826 kfree(ibm->acpi->driver);
827 ibm->acpi->driver = NULL;
828 } else if (!rc)
829 ibm->flags.acpi_driver_registered = 1;
831 return rc;
835 /****************************************************************************
836 ****************************************************************************
838 * Procfs Helpers
840 ****************************************************************************
841 ****************************************************************************/
843 static int dispatch_proc_show(struct seq_file *m, void *v)
845 struct ibm_struct *ibm = m->private;
847 if (!ibm || !ibm->read)
848 return -EINVAL;
849 return ibm->read(m);
852 static int dispatch_proc_open(struct inode *inode, struct file *file)
854 return single_open(file, dispatch_proc_show, PDE(inode)->data);
857 static ssize_t dispatch_proc_write(struct file *file,
858 const char __user *userbuf,
859 size_t count, loff_t *pos)
861 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
862 char *kernbuf;
863 int ret;
865 if (!ibm || !ibm->write)
866 return -EINVAL;
867 if (count > PAGE_SIZE - 2)
868 return -EINVAL;
870 kernbuf = kmalloc(count + 2, GFP_KERNEL);
871 if (!kernbuf)
872 return -ENOMEM;
874 if (copy_from_user(kernbuf, userbuf, count)) {
875 kfree(kernbuf);
876 return -EFAULT;
879 kernbuf[count] = 0;
880 strcat(kernbuf, ",");
881 ret = ibm->write(kernbuf);
882 if (ret == 0)
883 ret = count;
885 kfree(kernbuf);
887 return ret;
890 static const struct file_operations dispatch_proc_fops = {
891 .owner = THIS_MODULE,
892 .open = dispatch_proc_open,
893 .read = seq_read,
894 .llseek = seq_lseek,
895 .release = single_release,
896 .write = dispatch_proc_write,
899 static char *next_cmd(char **cmds)
901 char *start = *cmds;
902 char *end;
904 while ((end = strchr(start, ',')) && end == start)
905 start = end + 1;
907 if (!end)
908 return NULL;
910 *end = 0;
911 *cmds = end + 1;
912 return start;
916 /****************************************************************************
917 ****************************************************************************
919 * Device model: input, hwmon and platform
921 ****************************************************************************
922 ****************************************************************************/
924 static struct platform_device *tpacpi_pdev;
925 static struct platform_device *tpacpi_sensors_pdev;
926 static struct device *tpacpi_hwmon;
927 static struct input_dev *tpacpi_inputdev;
928 static struct mutex tpacpi_inputdev_send_mutex;
929 static LIST_HEAD(tpacpi_all_drivers);
931 static int tpacpi_suspend_handler(struct platform_device *pdev,
932 pm_message_t state)
934 struct ibm_struct *ibm, *itmp;
936 list_for_each_entry_safe(ibm, itmp,
937 &tpacpi_all_drivers,
938 all_drivers) {
939 if (ibm->suspend)
940 (ibm->suspend)(state);
943 return 0;
946 static int tpacpi_resume_handler(struct platform_device *pdev)
948 struct ibm_struct *ibm, *itmp;
950 list_for_each_entry_safe(ibm, itmp,
951 &tpacpi_all_drivers,
952 all_drivers) {
953 if (ibm->resume)
954 (ibm->resume)();
957 return 0;
960 static void tpacpi_shutdown_handler(struct platform_device *pdev)
962 struct ibm_struct *ibm, *itmp;
964 list_for_each_entry_safe(ibm, itmp,
965 &tpacpi_all_drivers,
966 all_drivers) {
967 if (ibm->shutdown)
968 (ibm->shutdown)();
972 static struct platform_driver tpacpi_pdriver = {
973 .driver = {
974 .name = TPACPI_DRVR_NAME,
975 .owner = THIS_MODULE,
977 .suspend = tpacpi_suspend_handler,
978 .resume = tpacpi_resume_handler,
979 .shutdown = tpacpi_shutdown_handler,
982 static struct platform_driver tpacpi_hwmon_pdriver = {
983 .driver = {
984 .name = TPACPI_HWMON_DRVR_NAME,
985 .owner = THIS_MODULE,
989 /*************************************************************************
990 * sysfs support helpers
993 struct attribute_set {
994 unsigned int members, max_members;
995 struct attribute_group group;
998 struct attribute_set_obj {
999 struct attribute_set s;
1000 struct attribute *a;
1001 } __attribute__((packed));
1003 static struct attribute_set *create_attr_set(unsigned int max_members,
1004 const char *name)
1006 struct attribute_set_obj *sobj;
1008 if (max_members == 0)
1009 return NULL;
1011 /* Allocates space for implicit NULL at the end too */
1012 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1013 max_members * sizeof(struct attribute *),
1014 GFP_KERNEL);
1015 if (!sobj)
1016 return NULL;
1017 sobj->s.max_members = max_members;
1018 sobj->s.group.attrs = &sobj->a;
1019 sobj->s.group.name = name;
1021 return &sobj->s;
1024 #define destroy_attr_set(_set) \
1025 kfree(_set);
1027 /* not multi-threaded safe, use it in a single thread per set */
1028 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1030 if (!s || !attr)
1031 return -EINVAL;
1033 if (s->members >= s->max_members)
1034 return -ENOMEM;
1036 s->group.attrs[s->members] = attr;
1037 s->members++;
1039 return 0;
1042 static int add_many_to_attr_set(struct attribute_set *s,
1043 struct attribute **attr,
1044 unsigned int count)
1046 int i, res;
1048 for (i = 0; i < count; i++) {
1049 res = add_to_attr_set(s, attr[i]);
1050 if (res)
1051 return res;
1054 return 0;
1057 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1059 sysfs_remove_group(kobj, &s->group);
1060 destroy_attr_set(s);
1063 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1064 sysfs_create_group(_kobj, &_attr_set->group)
1066 static int parse_strtoul(const char *buf,
1067 unsigned long max, unsigned long *value)
1069 char *endp;
1071 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1072 endp = skip_spaces(endp);
1073 if (*endp || *value > max)
1074 return -EINVAL;
1076 return 0;
1079 static void tpacpi_disable_brightness_delay(void)
1081 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1082 printk(TPACPI_NOTICE
1083 "ACPI backlight control delay disabled\n");
1086 static void printk_deprecated_attribute(const char * const what,
1087 const char * const details)
1089 tpacpi_log_usertask("deprecated sysfs attribute");
1090 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1091 "will be removed. %s\n",
1092 what, details);
1095 /*************************************************************************
1096 * rfkill and radio control support helpers
1100 * ThinkPad-ACPI firmware handling model:
1102 * WLSW (master wireless switch) is event-driven, and is common to all
1103 * firmware-controlled radios. It cannot be controlled, just monitored,
1104 * as expected. It overrides all radio state in firmware
1106 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1107 * (TODO: verify how WLSW interacts with the returned radio state).
1109 * The only time there are shadow radio state changes, is when
1110 * masked-off hotkeys are used.
1114 * Internal driver API for radio state:
1116 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1117 * bool: true means radio blocked (off)
1119 enum tpacpi_rfkill_state {
1120 TPACPI_RFK_RADIO_OFF = 0,
1121 TPACPI_RFK_RADIO_ON
1124 /* rfkill switches */
1125 enum tpacpi_rfk_id {
1126 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1127 TPACPI_RFK_WWAN_SW_ID,
1128 TPACPI_RFK_UWB_SW_ID,
1129 TPACPI_RFK_SW_MAX
1132 static const char *tpacpi_rfkill_names[] = {
1133 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1134 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1135 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1136 [TPACPI_RFK_SW_MAX] = NULL
1139 /* ThinkPad-ACPI rfkill subdriver */
1140 struct tpacpi_rfk {
1141 struct rfkill *rfkill;
1142 enum tpacpi_rfk_id id;
1143 const struct tpacpi_rfk_ops *ops;
1146 struct tpacpi_rfk_ops {
1147 /* firmware interface */
1148 int (*get_status)(void);
1149 int (*set_status)(const enum tpacpi_rfkill_state);
1152 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1154 /* Query FW and update rfkill sw state for a given rfkill switch */
1155 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1157 int status;
1159 if (!tp_rfk)
1160 return -ENODEV;
1162 status = (tp_rfk->ops->get_status)();
1163 if (status < 0)
1164 return status;
1166 rfkill_set_sw_state(tp_rfk->rfkill,
1167 (status == TPACPI_RFK_RADIO_OFF));
1169 return status;
1172 /* Query FW and update rfkill sw state for all rfkill switches */
1173 static void tpacpi_rfk_update_swstate_all(void)
1175 unsigned int i;
1177 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1178 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1182 * Sync the HW-blocking state of all rfkill switches,
1183 * do notice it causes the rfkill core to schedule uevents
1185 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1187 unsigned int i;
1188 struct tpacpi_rfk *tp_rfk;
1190 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1191 tp_rfk = tpacpi_rfkill_switches[i];
1192 if (tp_rfk) {
1193 if (rfkill_set_hw_state(tp_rfk->rfkill,
1194 blocked)) {
1195 /* ignore -- we track sw block */
1201 /* Call to get the WLSW state from the firmware */
1202 static int hotkey_get_wlsw(void);
1204 /* Call to query WLSW state and update all rfkill switches */
1205 static bool tpacpi_rfk_check_hwblock_state(void)
1207 int res = hotkey_get_wlsw();
1208 int hw_blocked;
1210 /* When unknown or unsupported, we have to assume it is unblocked */
1211 if (res < 0)
1212 return false;
1214 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1215 tpacpi_rfk_update_hwblock_state(hw_blocked);
1217 return hw_blocked;
1220 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1222 struct tpacpi_rfk *tp_rfk = data;
1223 int res;
1225 dbg_printk(TPACPI_DBG_RFKILL,
1226 "request to change radio state to %s\n",
1227 blocked ? "blocked" : "unblocked");
1229 /* try to set radio state */
1230 res = (tp_rfk->ops->set_status)(blocked ?
1231 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1233 /* and update the rfkill core with whatever the FW really did */
1234 tpacpi_rfk_update_swstate(tp_rfk);
1236 return (res < 0) ? res : 0;
1239 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1240 .set_block = tpacpi_rfk_hook_set_block,
1243 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1244 const struct tpacpi_rfk_ops *tp_rfkops,
1245 const enum rfkill_type rfktype,
1246 const char *name,
1247 const bool set_default)
1249 struct tpacpi_rfk *atp_rfk;
1250 int res;
1251 bool sw_state = false;
1252 bool hw_state;
1253 int sw_status;
1255 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1257 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1258 if (atp_rfk)
1259 atp_rfk->rfkill = rfkill_alloc(name,
1260 &tpacpi_pdev->dev,
1261 rfktype,
1262 &tpacpi_rfk_rfkill_ops,
1263 atp_rfk);
1264 if (!atp_rfk || !atp_rfk->rfkill) {
1265 printk(TPACPI_ERR
1266 "failed to allocate memory for rfkill class\n");
1267 kfree(atp_rfk);
1268 return -ENOMEM;
1271 atp_rfk->id = id;
1272 atp_rfk->ops = tp_rfkops;
1274 sw_status = (tp_rfkops->get_status)();
1275 if (sw_status < 0) {
1276 printk(TPACPI_ERR
1277 "failed to read initial state for %s, error %d\n",
1278 name, sw_status);
1279 } else {
1280 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1281 if (set_default) {
1282 /* try to keep the initial state, since we ask the
1283 * firmware to preserve it across S5 in NVRAM */
1284 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1287 hw_state = tpacpi_rfk_check_hwblock_state();
1288 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1290 res = rfkill_register(atp_rfk->rfkill);
1291 if (res < 0) {
1292 printk(TPACPI_ERR
1293 "failed to register %s rfkill switch: %d\n",
1294 name, res);
1295 rfkill_destroy(atp_rfk->rfkill);
1296 kfree(atp_rfk);
1297 return res;
1300 tpacpi_rfkill_switches[id] = atp_rfk;
1302 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1303 name, (sw_state || hw_state) ? "" : "un");
1304 return 0;
1307 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1309 struct tpacpi_rfk *tp_rfk;
1311 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1313 tp_rfk = tpacpi_rfkill_switches[id];
1314 if (tp_rfk) {
1315 rfkill_unregister(tp_rfk->rfkill);
1316 rfkill_destroy(tp_rfk->rfkill);
1317 tpacpi_rfkill_switches[id] = NULL;
1318 kfree(tp_rfk);
1322 static void printk_deprecated_rfkill_attribute(const char * const what)
1324 printk_deprecated_attribute(what,
1325 "Please switch to generic rfkill before year 2010");
1328 /* sysfs <radio> enable ------------------------------------------------ */
1329 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1330 struct device_attribute *attr,
1331 char *buf)
1333 int status;
1335 printk_deprecated_rfkill_attribute(attr->attr.name);
1337 /* This is in the ABI... */
1338 if (tpacpi_rfk_check_hwblock_state()) {
1339 status = TPACPI_RFK_RADIO_OFF;
1340 } else {
1341 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1342 if (status < 0)
1343 return status;
1346 return snprintf(buf, PAGE_SIZE, "%d\n",
1347 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1350 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1351 struct device_attribute *attr,
1352 const char *buf, size_t count)
1354 unsigned long t;
1355 int res;
1357 printk_deprecated_rfkill_attribute(attr->attr.name);
1359 if (parse_strtoul(buf, 1, &t))
1360 return -EINVAL;
1362 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1364 /* This is in the ABI... */
1365 if (tpacpi_rfk_check_hwblock_state() && !!t)
1366 return -EPERM;
1368 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1369 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1370 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1372 return (res < 0) ? res : count;
1375 /* procfs -------------------------------------------------------------- */
1376 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1378 if (id >= TPACPI_RFK_SW_MAX)
1379 seq_printf(m, "status:\t\tnot supported\n");
1380 else {
1381 int status;
1383 /* This is in the ABI... */
1384 if (tpacpi_rfk_check_hwblock_state()) {
1385 status = TPACPI_RFK_RADIO_OFF;
1386 } else {
1387 status = tpacpi_rfk_update_swstate(
1388 tpacpi_rfkill_switches[id]);
1389 if (status < 0)
1390 return status;
1393 seq_printf(m, "status:\t\t%s\n",
1394 (status == TPACPI_RFK_RADIO_ON) ?
1395 "enabled" : "disabled");
1396 seq_printf(m, "commands:\tenable, disable\n");
1399 return 0;
1402 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1404 char *cmd;
1405 int status = -1;
1406 int res = 0;
1408 if (id >= TPACPI_RFK_SW_MAX)
1409 return -ENODEV;
1411 while ((cmd = next_cmd(&buf))) {
1412 if (strlencmp(cmd, "enable") == 0)
1413 status = TPACPI_RFK_RADIO_ON;
1414 else if (strlencmp(cmd, "disable") == 0)
1415 status = TPACPI_RFK_RADIO_OFF;
1416 else
1417 return -EINVAL;
1420 if (status != -1) {
1421 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1422 (status == TPACPI_RFK_RADIO_ON) ?
1423 "enable" : "disable",
1424 tpacpi_rfkill_names[id]);
1425 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1426 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1429 return res;
1432 /*************************************************************************
1433 * thinkpad-acpi driver attributes
1436 /* interface_version --------------------------------------------------- */
1437 static ssize_t tpacpi_driver_interface_version_show(
1438 struct device_driver *drv,
1439 char *buf)
1441 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1444 static DRIVER_ATTR(interface_version, S_IRUGO,
1445 tpacpi_driver_interface_version_show, NULL);
1447 /* debug_level --------------------------------------------------------- */
1448 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1449 char *buf)
1451 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1454 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1455 const char *buf, size_t count)
1457 unsigned long t;
1459 if (parse_strtoul(buf, 0xffff, &t))
1460 return -EINVAL;
1462 dbg_level = t;
1464 return count;
1467 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1468 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1470 /* version ------------------------------------------------------------- */
1471 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1472 char *buf)
1474 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1475 TPACPI_DESC, TPACPI_VERSION);
1478 static DRIVER_ATTR(version, S_IRUGO,
1479 tpacpi_driver_version_show, NULL);
1481 /* --------------------------------------------------------------------- */
1483 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1485 /* wlsw_emulstate ------------------------------------------------------ */
1486 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1487 char *buf)
1489 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1492 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1493 const char *buf, size_t count)
1495 unsigned long t;
1497 if (parse_strtoul(buf, 1, &t))
1498 return -EINVAL;
1500 if (tpacpi_wlsw_emulstate != !!t) {
1501 tpacpi_wlsw_emulstate = !!t;
1502 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1505 return count;
1508 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1509 tpacpi_driver_wlsw_emulstate_show,
1510 tpacpi_driver_wlsw_emulstate_store);
1512 /* bluetooth_emulstate ------------------------------------------------- */
1513 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1514 struct device_driver *drv,
1515 char *buf)
1517 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1520 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1521 struct device_driver *drv,
1522 const char *buf, size_t count)
1524 unsigned long t;
1526 if (parse_strtoul(buf, 1, &t))
1527 return -EINVAL;
1529 tpacpi_bluetooth_emulstate = !!t;
1531 return count;
1534 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1535 tpacpi_driver_bluetooth_emulstate_show,
1536 tpacpi_driver_bluetooth_emulstate_store);
1538 /* wwan_emulstate ------------------------------------------------- */
1539 static ssize_t tpacpi_driver_wwan_emulstate_show(
1540 struct device_driver *drv,
1541 char *buf)
1543 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1546 static ssize_t tpacpi_driver_wwan_emulstate_store(
1547 struct device_driver *drv,
1548 const char *buf, size_t count)
1550 unsigned long t;
1552 if (parse_strtoul(buf, 1, &t))
1553 return -EINVAL;
1555 tpacpi_wwan_emulstate = !!t;
1557 return count;
1560 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1561 tpacpi_driver_wwan_emulstate_show,
1562 tpacpi_driver_wwan_emulstate_store);
1564 /* uwb_emulstate ------------------------------------------------- */
1565 static ssize_t tpacpi_driver_uwb_emulstate_show(
1566 struct device_driver *drv,
1567 char *buf)
1569 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1572 static ssize_t tpacpi_driver_uwb_emulstate_store(
1573 struct device_driver *drv,
1574 const char *buf, size_t count)
1576 unsigned long t;
1578 if (parse_strtoul(buf, 1, &t))
1579 return -EINVAL;
1581 tpacpi_uwb_emulstate = !!t;
1583 return count;
1586 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1587 tpacpi_driver_uwb_emulstate_show,
1588 tpacpi_driver_uwb_emulstate_store);
1589 #endif
1591 /* --------------------------------------------------------------------- */
1593 static struct driver_attribute *tpacpi_driver_attributes[] = {
1594 &driver_attr_debug_level, &driver_attr_version,
1595 &driver_attr_interface_version,
1598 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1600 int i, res;
1602 i = 0;
1603 res = 0;
1604 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1605 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1606 i++;
1609 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1610 if (!res && dbg_wlswemul)
1611 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1612 if (!res && dbg_bluetoothemul)
1613 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1614 if (!res && dbg_wwanemul)
1615 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1616 if (!res && dbg_uwbemul)
1617 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1618 #endif
1620 return res;
1623 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1625 int i;
1627 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1628 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1630 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1631 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1632 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1633 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1634 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1635 #endif
1638 /*************************************************************************
1639 * Firmware Data
1643 * Table of recommended minimum BIOS versions
1645 * Reasons for listing:
1646 * 1. Stable BIOS, listed because the unknown amount of
1647 * bugs and bad ACPI behaviour on older versions
1649 * 2. BIOS or EC fw with known bugs that trigger on Linux
1651 * 3. BIOS with known reduced functionality in older versions
1653 * We recommend the latest BIOS and EC version.
1654 * We only support the latest BIOS and EC fw version as a rule.
1656 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1657 * Information from users in ThinkWiki
1659 * WARNING: we use this table also to detect that the machine is
1660 * a ThinkPad in some cases, so don't remove entries lightly.
1663 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1664 { .vendor = (__v), \
1665 .bios = TPID(__id1, __id2), \
1666 .ec = TPACPI_MATCH_ANY, \
1667 .quirks = TPACPI_MATCH_ANY << 16 \
1668 | (__bv1) << 8 | (__bv2) }
1670 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1671 __eid, __ev1, __ev2) \
1672 { .vendor = (__v), \
1673 .bios = TPID(__bid1, __bid2), \
1674 .ec = __eid, \
1675 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1676 | (__bv1) << 8 | (__bv2) }
1678 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1679 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1681 /* Outdated IBM BIOSes often lack the EC id string */
1682 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1683 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1684 __bv1, __bv2, TPID(__id1, __id2), \
1685 __ev1, __ev2), \
1686 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1687 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1688 __ev1, __ev2)
1690 /* Outdated IBM BIOSes often lack the EC id string */
1691 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1692 __eid1, __eid2, __ev1, __ev2) \
1693 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1694 __bv1, __bv2, TPID(__eid1, __eid2), \
1695 __ev1, __ev2), \
1696 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1697 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1698 __ev1, __ev2)
1700 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1701 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1703 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1704 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1705 __bv1, __bv2, TPID(__id1, __id2), \
1706 __ev1, __ev2)
1708 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1709 __eid1, __eid2, __ev1, __ev2) \
1710 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1711 __bv1, __bv2, TPID(__eid1, __eid2), \
1712 __ev1, __ev2)
1714 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1715 /* Numeric models ------------------ */
1716 /* FW MODEL BIOS VERS */
1717 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1718 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1719 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1720 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1721 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1722 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1723 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1724 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1725 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1727 /* A-series ------------------------- */
1728 /* FW MODEL BIOS VERS EC VERS */
1729 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1730 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1731 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1732 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1733 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1734 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1735 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1736 TPV_QI0('1', '3', '2', '0'), /* A22m */
1737 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1738 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1739 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1741 /* G-series ------------------------- */
1742 /* FW MODEL BIOS VERS */
1743 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1744 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1746 /* R-series, T-series --------------- */
1747 /* FW MODEL BIOS VERS EC VERS */
1748 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1749 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1750 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1751 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1752 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1753 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1754 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1755 T40/p, T41/p, T42/p (1) */
1756 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1757 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1758 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1759 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1761 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1762 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1763 TPV_QI0('1', '6', '3', '2'), /* T22 */
1764 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1765 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1766 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1768 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1769 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1770 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1772 /* BIOS FW BIOS VERS EC FW EC VERS */
1773 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1774 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1776 /* X-series ------------------------- */
1777 /* FW MODEL BIOS VERS EC VERS */
1778 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1779 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1780 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1781 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1782 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1783 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1784 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1786 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1787 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1789 /* (0) - older versions lack DMI EC fw string and functionality */
1790 /* (1) - older versions known to lack functionality */
1793 #undef TPV_QL1
1794 #undef TPV_QL0
1795 #undef TPV_QI2
1796 #undef TPV_QI1
1797 #undef TPV_QI0
1798 #undef TPV_Q_X
1799 #undef TPV_Q
1801 static void __init tpacpi_check_outdated_fw(void)
1803 unsigned long fwvers;
1804 u16 ec_version, bios_version;
1806 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1807 ARRAY_SIZE(tpacpi_bios_version_qtable));
1809 if (!fwvers)
1810 return;
1812 bios_version = fwvers & 0xffffU;
1813 ec_version = (fwvers >> 16) & 0xffffU;
1815 /* note that unknown versions are set to 0x0000 and we use that */
1816 if ((bios_version > thinkpad_id.bios_release) ||
1817 (ec_version > thinkpad_id.ec_release &&
1818 ec_version != TPACPI_MATCH_ANY)) {
1820 * The changelogs would let us track down the exact
1821 * reason, but it is just too much of a pain to track
1822 * it. We only list BIOSes that are either really
1823 * broken, or really stable to begin with, so it is
1824 * best if the user upgrades the firmware anyway.
1826 printk(TPACPI_WARN
1827 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1828 printk(TPACPI_WARN
1829 "WARNING: This firmware may be missing critical bug "
1830 "fixes and/or important features\n");
1834 static bool __init tpacpi_is_fw_known(void)
1836 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1837 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1840 /****************************************************************************
1841 ****************************************************************************
1843 * Subdrivers
1845 ****************************************************************************
1846 ****************************************************************************/
1848 /*************************************************************************
1849 * thinkpad-acpi metadata subdriver
1852 static int thinkpad_acpi_driver_read(struct seq_file *m)
1854 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1855 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1856 return 0;
1859 static struct ibm_struct thinkpad_acpi_driver_data = {
1860 .name = "driver",
1861 .read = thinkpad_acpi_driver_read,
1864 /*************************************************************************
1865 * Hotkey subdriver
1869 * ThinkPad firmware event model
1871 * The ThinkPad firmware has two main event interfaces: normal ACPI
1872 * notifications (which follow the ACPI standard), and a private event
1873 * interface.
1875 * The private event interface also issues events for the hotkeys. As
1876 * the driver gained features, the event handling code ended up being
1877 * built around the hotkey subdriver. This will need to be refactored
1878 * to a more formal event API eventually.
1880 * Some "hotkeys" are actually supposed to be used as event reports,
1881 * such as "brightness has changed", "volume has changed", depending on
1882 * the ThinkPad model and how the firmware is operating.
1884 * Unlike other classes, hotkey-class events have mask/unmask control on
1885 * non-ancient firmware. However, how it behaves changes a lot with the
1886 * firmware model and version.
1889 enum { /* hot key scan codes (derived from ACPI DSDT) */
1890 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1891 TP_ACPI_HOTKEYSCAN_FNF2,
1892 TP_ACPI_HOTKEYSCAN_FNF3,
1893 TP_ACPI_HOTKEYSCAN_FNF4,
1894 TP_ACPI_HOTKEYSCAN_FNF5,
1895 TP_ACPI_HOTKEYSCAN_FNF6,
1896 TP_ACPI_HOTKEYSCAN_FNF7,
1897 TP_ACPI_HOTKEYSCAN_FNF8,
1898 TP_ACPI_HOTKEYSCAN_FNF9,
1899 TP_ACPI_HOTKEYSCAN_FNF10,
1900 TP_ACPI_HOTKEYSCAN_FNF11,
1901 TP_ACPI_HOTKEYSCAN_FNF12,
1902 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1903 TP_ACPI_HOTKEYSCAN_FNINSERT,
1904 TP_ACPI_HOTKEYSCAN_FNDELETE,
1905 TP_ACPI_HOTKEYSCAN_FNHOME,
1906 TP_ACPI_HOTKEYSCAN_FNEND,
1907 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1908 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1909 TP_ACPI_HOTKEYSCAN_FNSPACE,
1910 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1911 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1912 TP_ACPI_HOTKEYSCAN_MUTE,
1913 TP_ACPI_HOTKEYSCAN_THINKPAD,
1916 enum { /* Keys/events available through NVRAM polling */
1917 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1918 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1921 enum { /* Positions of some of the keys in hotkey masks */
1922 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1923 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1924 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1925 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1926 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1927 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1928 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1929 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1930 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1931 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1932 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1935 enum { /* NVRAM to ACPI HKEY group map */
1936 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1937 TP_ACPI_HKEY_ZOOM_MASK |
1938 TP_ACPI_HKEY_DISPSWTCH_MASK |
1939 TP_ACPI_HKEY_HIBERNATE_MASK,
1940 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1941 TP_ACPI_HKEY_BRGHTDWN_MASK,
1942 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1943 TP_ACPI_HKEY_VOLDWN_MASK |
1944 TP_ACPI_HKEY_MUTE_MASK,
1947 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1948 struct tp_nvram_state {
1949 u16 thinkpad_toggle:1;
1950 u16 zoom_toggle:1;
1951 u16 display_toggle:1;
1952 u16 thinklight_toggle:1;
1953 u16 hibernate_toggle:1;
1954 u16 displayexp_toggle:1;
1955 u16 display_state:1;
1956 u16 brightness_toggle:1;
1957 u16 volume_toggle:1;
1958 u16 mute:1;
1960 u8 brightness_level;
1961 u8 volume_level;
1964 /* kthread for the hotkey poller */
1965 static struct task_struct *tpacpi_hotkey_task;
1967 /* Acquired while the poller kthread is running, use to sync start/stop */
1968 static struct mutex hotkey_thread_mutex;
1971 * Acquire mutex to write poller control variables as an
1972 * atomic block.
1974 * Increment hotkey_config_change when changing them if you
1975 * want the kthread to forget old state.
1977 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1979 static struct mutex hotkey_thread_data_mutex;
1980 static unsigned int hotkey_config_change;
1983 * hotkey poller control variables
1985 * Must be atomic or readers will also need to acquire mutex
1987 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1988 * should be used only when the changes need to be taken as
1989 * a block, OR when one needs to force the kthread to forget
1990 * old state.
1992 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1993 static unsigned int hotkey_poll_freq = 10; /* Hz */
1995 #define HOTKEY_CONFIG_CRITICAL_START \
1996 do { \
1997 mutex_lock(&hotkey_thread_data_mutex); \
1998 hotkey_config_change++; \
1999 } while (0);
2000 #define HOTKEY_CONFIG_CRITICAL_END \
2001 mutex_unlock(&hotkey_thread_data_mutex);
2003 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2005 #define hotkey_source_mask 0U
2006 #define HOTKEY_CONFIG_CRITICAL_START
2007 #define HOTKEY_CONFIG_CRITICAL_END
2009 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2011 static struct mutex hotkey_mutex;
2013 static enum { /* Reasons for waking up */
2014 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2015 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2016 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2017 } hotkey_wakeup_reason;
2019 static int hotkey_autosleep_ack;
2021 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2022 static u32 hotkey_all_mask; /* all events supported in fw */
2023 static u32 hotkey_reserved_mask; /* events better left disabled */
2024 static u32 hotkey_driver_mask; /* events needed by the driver */
2025 static u32 hotkey_user_mask; /* events visible to userspace */
2026 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2028 static unsigned int hotkey_report_mode;
2030 static u16 *hotkey_keycode_map;
2032 static struct attribute_set *hotkey_dev_attributes;
2034 static void tpacpi_driver_event(const unsigned int hkey_event);
2035 static void hotkey_driver_event(const unsigned int scancode);
2036 static void hotkey_poll_setup(const bool may_warn);
2038 /* HKEY.MHKG() return bits */
2039 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2041 static int hotkey_get_wlsw(void)
2043 int status;
2045 if (!tp_features.hotkey_wlsw)
2046 return -ENODEV;
2048 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2049 if (dbg_wlswemul)
2050 return (tpacpi_wlsw_emulstate) ?
2051 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2052 #endif
2054 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2055 return -EIO;
2057 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2060 static int hotkey_get_tablet_mode(int *status)
2062 int s;
2064 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2065 return -EIO;
2067 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2068 return 0;
2072 * Reads current event mask from firmware, and updates
2073 * hotkey_acpi_mask accordingly. Also resets any bits
2074 * from hotkey_user_mask that are unavailable to be
2075 * delivered (shadow requirement of the userspace ABI).
2077 * Call with hotkey_mutex held
2079 static int hotkey_mask_get(void)
2081 if (tp_features.hotkey_mask) {
2082 u32 m = 0;
2084 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2085 return -EIO;
2087 hotkey_acpi_mask = m;
2088 } else {
2089 /* no mask support doesn't mean no event support... */
2090 hotkey_acpi_mask = hotkey_all_mask;
2093 /* sync userspace-visible mask */
2094 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2096 return 0;
2099 void static hotkey_mask_warn_incomplete_mask(void)
2101 /* log only what the user can fix... */
2102 const u32 wantedmask = hotkey_driver_mask &
2103 ~(hotkey_acpi_mask | hotkey_source_mask) &
2104 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2106 if (wantedmask)
2107 printk(TPACPI_NOTICE
2108 "required events 0x%08x not enabled!\n",
2109 wantedmask);
2113 * Set the firmware mask when supported
2115 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2117 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2119 * Call with hotkey_mutex held
2121 static int hotkey_mask_set(u32 mask)
2123 int i;
2124 int rc = 0;
2126 const u32 fwmask = mask & ~hotkey_source_mask;
2128 if (tp_features.hotkey_mask) {
2129 for (i = 0; i < 32; i++) {
2130 if (!acpi_evalf(hkey_handle,
2131 NULL, "MHKM", "vdd", i + 1,
2132 !!(mask & (1 << i)))) {
2133 rc = -EIO;
2134 break;
2140 * We *must* make an inconditional call to hotkey_mask_get to
2141 * refresh hotkey_acpi_mask and update hotkey_user_mask
2143 * Take the opportunity to also log when we cannot _enable_
2144 * a given event.
2146 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2147 printk(TPACPI_NOTICE
2148 "asked for hotkey mask 0x%08x, but "
2149 "firmware forced it to 0x%08x\n",
2150 fwmask, hotkey_acpi_mask);
2153 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2154 hotkey_mask_warn_incomplete_mask();
2156 return rc;
2160 * Sets hotkey_user_mask and tries to set the firmware mask
2162 * Call with hotkey_mutex held
2164 static int hotkey_user_mask_set(const u32 mask)
2166 int rc;
2168 /* Give people a chance to notice they are doing something that
2169 * is bound to go boom on their users sooner or later */
2170 if (!tp_warned.hotkey_mask_ff &&
2171 (mask == 0xffff || mask == 0xffffff ||
2172 mask == 0xffffffff)) {
2173 tp_warned.hotkey_mask_ff = 1;
2174 printk(TPACPI_NOTICE
2175 "setting the hotkey mask to 0x%08x is likely "
2176 "not the best way to go about it\n", mask);
2177 printk(TPACPI_NOTICE
2178 "please consider using the driver defaults, "
2179 "and refer to up-to-date thinkpad-acpi "
2180 "documentation\n");
2183 /* Try to enable what the user asked for, plus whatever we need.
2184 * this syncs everything but won't enable bits in hotkey_user_mask */
2185 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2187 /* Enable the available bits in hotkey_user_mask */
2188 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2190 return rc;
2194 * Sets the driver hotkey mask.
2196 * Can be called even if the hotkey subdriver is inactive
2198 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2200 int rc;
2202 /* Do the right thing if hotkey_init has not been called yet */
2203 if (!tp_features.hotkey) {
2204 hotkey_driver_mask = mask;
2205 return 0;
2208 mutex_lock(&hotkey_mutex);
2210 HOTKEY_CONFIG_CRITICAL_START
2211 hotkey_driver_mask = mask;
2212 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2213 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2214 #endif
2215 HOTKEY_CONFIG_CRITICAL_END
2217 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2218 ~hotkey_source_mask);
2219 hotkey_poll_setup(true);
2221 mutex_unlock(&hotkey_mutex);
2223 return rc;
2226 static int hotkey_status_get(int *status)
2228 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2229 return -EIO;
2231 return 0;
2234 static int hotkey_status_set(bool enable)
2236 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2237 return -EIO;
2239 return 0;
2242 static void tpacpi_input_send_tabletsw(void)
2244 int state;
2246 if (tp_features.hotkey_tablet &&
2247 !hotkey_get_tablet_mode(&state)) {
2248 mutex_lock(&tpacpi_inputdev_send_mutex);
2250 input_report_switch(tpacpi_inputdev,
2251 SW_TABLET_MODE, !!state);
2252 input_sync(tpacpi_inputdev);
2254 mutex_unlock(&tpacpi_inputdev_send_mutex);
2258 /* Do NOT call without validating scancode first */
2259 static void tpacpi_input_send_key(const unsigned int scancode)
2261 const unsigned int keycode = hotkey_keycode_map[scancode];
2263 if (keycode != KEY_RESERVED) {
2264 mutex_lock(&tpacpi_inputdev_send_mutex);
2266 input_report_key(tpacpi_inputdev, keycode, 1);
2267 if (keycode == KEY_UNKNOWN)
2268 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2269 scancode);
2270 input_sync(tpacpi_inputdev);
2272 input_report_key(tpacpi_inputdev, keycode, 0);
2273 if (keycode == KEY_UNKNOWN)
2274 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2275 scancode);
2276 input_sync(tpacpi_inputdev);
2278 mutex_unlock(&tpacpi_inputdev_send_mutex);
2282 /* Do NOT call without validating scancode first */
2283 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2285 hotkey_driver_event(scancode);
2286 if (hotkey_user_mask & (1 << scancode))
2287 tpacpi_input_send_key(scancode);
2290 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2291 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2293 /* Do NOT call without validating scancode first */
2294 static void tpacpi_hotkey_send_key(unsigned int scancode)
2296 tpacpi_input_send_key_masked(scancode);
2297 if (hotkey_report_mode < 2) {
2298 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2299 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2303 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2305 u8 d;
2307 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2308 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2309 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2310 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2311 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2312 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2314 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2315 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2316 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2318 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2319 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2320 n->displayexp_toggle =
2321 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2323 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2324 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2325 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2326 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2327 n->brightness_toggle =
2328 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2330 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2331 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2332 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2333 >> TP_NVRAM_POS_LEVEL_VOLUME;
2334 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2335 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2339 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2340 struct tp_nvram_state *newn,
2341 const u32 event_mask)
2344 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2345 do { \
2346 if ((event_mask & (1 << __scancode)) && \
2347 oldn->__member != newn->__member) \
2348 tpacpi_hotkey_send_key(__scancode); \
2349 } while (0)
2351 #define TPACPI_MAY_SEND_KEY(__scancode) \
2352 do { \
2353 if (event_mask & (1 << __scancode)) \
2354 tpacpi_hotkey_send_key(__scancode); \
2355 } while (0)
2357 void issue_volchange(const unsigned int oldvol,
2358 const unsigned int newvol)
2360 unsigned int i = oldvol;
2362 while (i > newvol) {
2363 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2364 i--;
2366 while (i < newvol) {
2367 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2368 i++;
2372 void issue_brightnesschange(const unsigned int oldbrt,
2373 const unsigned int newbrt)
2375 unsigned int i = oldbrt;
2377 while (i > newbrt) {
2378 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2379 i--;
2381 while (i < newbrt) {
2382 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2383 i++;
2387 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2388 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2389 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2390 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2392 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2394 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2397 * Handle volume
2399 * This code is supposed to duplicate the IBM firmware behaviour:
2400 * - Pressing MUTE issues mute hotkey message, even when already mute
2401 * - Pressing Volume up/down issues volume up/down hotkey messages,
2402 * even when already at maximum or minumum volume
2403 * - The act of unmuting issues volume up/down notification,
2404 * depending which key was used to unmute
2406 * We are constrained to what the NVRAM can tell us, which is not much
2407 * and certainly not enough if more than one volume hotkey was pressed
2408 * since the last poll cycle.
2410 * Just to make our life interesting, some newer Lenovo ThinkPads have
2411 * bugs in the BIOS and may fail to update volume_toggle properly.
2413 if (newn->mute) {
2414 /* muted */
2415 if (!oldn->mute ||
2416 oldn->volume_toggle != newn->volume_toggle ||
2417 oldn->volume_level != newn->volume_level) {
2418 /* recently muted, or repeated mute keypress, or
2419 * multiple presses ending in mute */
2420 issue_volchange(oldn->volume_level, newn->volume_level);
2421 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2423 } else {
2424 /* unmute */
2425 if (oldn->mute) {
2426 /* recently unmuted, issue 'unmute' keypress */
2427 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2429 if (oldn->volume_level != newn->volume_level) {
2430 issue_volchange(oldn->volume_level, newn->volume_level);
2431 } else if (oldn->volume_toggle != newn->volume_toggle) {
2432 /* repeated vol up/down keypress at end of scale ? */
2433 if (newn->volume_level == 0)
2434 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2435 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2436 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2440 /* handle brightness */
2441 if (oldn->brightness_level != newn->brightness_level) {
2442 issue_brightnesschange(oldn->brightness_level,
2443 newn->brightness_level);
2444 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2445 /* repeated key presses that didn't change state */
2446 if (newn->brightness_level == 0)
2447 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2448 else if (newn->brightness_level >= bright_maxlvl
2449 && !tp_features.bright_unkfw)
2450 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2453 #undef TPACPI_COMPARE_KEY
2454 #undef TPACPI_MAY_SEND_KEY
2458 * Polling driver
2460 * We track all events in hotkey_source_mask all the time, since
2461 * most of them are edge-based. We only issue those requested by
2462 * hotkey_user_mask or hotkey_driver_mask, though.
2464 static int hotkey_kthread(void *data)
2466 struct tp_nvram_state s[2];
2467 u32 poll_mask, event_mask;
2468 unsigned int si, so;
2469 unsigned long t;
2470 unsigned int change_detector, must_reset;
2471 unsigned int poll_freq;
2473 mutex_lock(&hotkey_thread_mutex);
2475 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2476 goto exit;
2478 set_freezable();
2480 so = 0;
2481 si = 1;
2482 t = 0;
2484 /* Initial state for compares */
2485 mutex_lock(&hotkey_thread_data_mutex);
2486 change_detector = hotkey_config_change;
2487 poll_mask = hotkey_source_mask;
2488 event_mask = hotkey_source_mask &
2489 (hotkey_driver_mask | hotkey_user_mask);
2490 poll_freq = hotkey_poll_freq;
2491 mutex_unlock(&hotkey_thread_data_mutex);
2492 hotkey_read_nvram(&s[so], poll_mask);
2494 while (!kthread_should_stop()) {
2495 if (t == 0) {
2496 if (likely(poll_freq))
2497 t = 1000/poll_freq;
2498 else
2499 t = 100; /* should never happen... */
2501 t = msleep_interruptible(t);
2502 if (unlikely(kthread_should_stop()))
2503 break;
2504 must_reset = try_to_freeze();
2505 if (t > 0 && !must_reset)
2506 continue;
2508 mutex_lock(&hotkey_thread_data_mutex);
2509 if (must_reset || hotkey_config_change != change_detector) {
2510 /* forget old state on thaw or config change */
2511 si = so;
2512 t = 0;
2513 change_detector = hotkey_config_change;
2515 poll_mask = hotkey_source_mask;
2516 event_mask = hotkey_source_mask &
2517 (hotkey_driver_mask | hotkey_user_mask);
2518 poll_freq = hotkey_poll_freq;
2519 mutex_unlock(&hotkey_thread_data_mutex);
2521 if (likely(poll_mask)) {
2522 hotkey_read_nvram(&s[si], poll_mask);
2523 if (likely(si != so)) {
2524 hotkey_compare_and_issue_event(&s[so], &s[si],
2525 event_mask);
2529 so = si;
2530 si ^= 1;
2533 exit:
2534 mutex_unlock(&hotkey_thread_mutex);
2535 return 0;
2538 /* call with hotkey_mutex held */
2539 static void hotkey_poll_stop_sync(void)
2541 if (tpacpi_hotkey_task) {
2542 if (frozen(tpacpi_hotkey_task) ||
2543 freezing(tpacpi_hotkey_task))
2544 thaw_process(tpacpi_hotkey_task);
2546 kthread_stop(tpacpi_hotkey_task);
2547 tpacpi_hotkey_task = NULL;
2548 mutex_lock(&hotkey_thread_mutex);
2549 /* at this point, the thread did exit */
2550 mutex_unlock(&hotkey_thread_mutex);
2554 /* call with hotkey_mutex held */
2555 static void hotkey_poll_setup(const bool may_warn)
2557 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2558 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2560 if (hotkey_poll_freq > 0 &&
2561 (poll_driver_mask ||
2562 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2563 if (!tpacpi_hotkey_task) {
2564 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2565 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2566 if (IS_ERR(tpacpi_hotkey_task)) {
2567 tpacpi_hotkey_task = NULL;
2568 printk(TPACPI_ERR
2569 "could not create kernel thread "
2570 "for hotkey polling\n");
2573 } else {
2574 hotkey_poll_stop_sync();
2575 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2576 hotkey_poll_freq == 0) {
2577 printk(TPACPI_NOTICE
2578 "hot keys 0x%08x and/or events 0x%08x "
2579 "require polling, which is currently "
2580 "disabled\n",
2581 poll_user_mask, poll_driver_mask);
2586 static void hotkey_poll_setup_safe(const bool may_warn)
2588 mutex_lock(&hotkey_mutex);
2589 hotkey_poll_setup(may_warn);
2590 mutex_unlock(&hotkey_mutex);
2593 /* call with hotkey_mutex held */
2594 static void hotkey_poll_set_freq(unsigned int freq)
2596 if (!freq)
2597 hotkey_poll_stop_sync();
2599 hotkey_poll_freq = freq;
2602 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2604 static void hotkey_poll_setup(const bool __unused)
2608 static void hotkey_poll_setup_safe(const bool __unused)
2612 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2614 static int hotkey_inputdev_open(struct input_dev *dev)
2616 switch (tpacpi_lifecycle) {
2617 case TPACPI_LIFE_INIT:
2618 case TPACPI_LIFE_RUNNING:
2619 hotkey_poll_setup_safe(false);
2620 return 0;
2621 case TPACPI_LIFE_EXITING:
2622 return -EBUSY;
2625 /* Should only happen if tpacpi_lifecycle is corrupt */
2626 BUG();
2627 return -EBUSY;
2630 static void hotkey_inputdev_close(struct input_dev *dev)
2632 /* disable hotkey polling when possible */
2633 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2634 !(hotkey_source_mask & hotkey_driver_mask))
2635 hotkey_poll_setup_safe(false);
2638 /* sysfs hotkey enable ------------------------------------------------- */
2639 static ssize_t hotkey_enable_show(struct device *dev,
2640 struct device_attribute *attr,
2641 char *buf)
2643 int res, status;
2645 printk_deprecated_attribute("hotkey_enable",
2646 "Hotkey reporting is always enabled");
2648 res = hotkey_status_get(&status);
2649 if (res)
2650 return res;
2652 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2655 static ssize_t hotkey_enable_store(struct device *dev,
2656 struct device_attribute *attr,
2657 const char *buf, size_t count)
2659 unsigned long t;
2661 printk_deprecated_attribute("hotkey_enable",
2662 "Hotkeys can be disabled through hotkey_mask");
2664 if (parse_strtoul(buf, 1, &t))
2665 return -EINVAL;
2667 if (t == 0)
2668 return -EPERM;
2670 return count;
2673 static struct device_attribute dev_attr_hotkey_enable =
2674 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2675 hotkey_enable_show, hotkey_enable_store);
2677 /* sysfs hotkey mask --------------------------------------------------- */
2678 static ssize_t hotkey_mask_show(struct device *dev,
2679 struct device_attribute *attr,
2680 char *buf)
2682 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2685 static ssize_t hotkey_mask_store(struct device *dev,
2686 struct device_attribute *attr,
2687 const char *buf, size_t count)
2689 unsigned long t;
2690 int res;
2692 if (parse_strtoul(buf, 0xffffffffUL, &t))
2693 return -EINVAL;
2695 if (mutex_lock_killable(&hotkey_mutex))
2696 return -ERESTARTSYS;
2698 res = hotkey_user_mask_set(t);
2700 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2701 hotkey_poll_setup(true);
2702 #endif
2704 mutex_unlock(&hotkey_mutex);
2706 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2708 return (res) ? res : count;
2711 static struct device_attribute dev_attr_hotkey_mask =
2712 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2713 hotkey_mask_show, hotkey_mask_store);
2715 /* sysfs hotkey bios_enabled ------------------------------------------- */
2716 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2717 struct device_attribute *attr,
2718 char *buf)
2720 return sprintf(buf, "0\n");
2723 static struct device_attribute dev_attr_hotkey_bios_enabled =
2724 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2726 /* sysfs hotkey bios_mask ---------------------------------------------- */
2727 static ssize_t hotkey_bios_mask_show(struct device *dev,
2728 struct device_attribute *attr,
2729 char *buf)
2731 printk_deprecated_attribute("hotkey_bios_mask",
2732 "This attribute is useless.");
2733 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2736 static struct device_attribute dev_attr_hotkey_bios_mask =
2737 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2739 /* sysfs hotkey all_mask ----------------------------------------------- */
2740 static ssize_t hotkey_all_mask_show(struct device *dev,
2741 struct device_attribute *attr,
2742 char *buf)
2744 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2745 hotkey_all_mask | hotkey_source_mask);
2748 static struct device_attribute dev_attr_hotkey_all_mask =
2749 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2751 /* sysfs hotkey recommended_mask --------------------------------------- */
2752 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2753 struct device_attribute *attr,
2754 char *buf)
2756 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2757 (hotkey_all_mask | hotkey_source_mask)
2758 & ~hotkey_reserved_mask);
2761 static struct device_attribute dev_attr_hotkey_recommended_mask =
2762 __ATTR(hotkey_recommended_mask, S_IRUGO,
2763 hotkey_recommended_mask_show, NULL);
2765 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2767 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2768 static ssize_t hotkey_source_mask_show(struct device *dev,
2769 struct device_attribute *attr,
2770 char *buf)
2772 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2775 static ssize_t hotkey_source_mask_store(struct device *dev,
2776 struct device_attribute *attr,
2777 const char *buf, size_t count)
2779 unsigned long t;
2780 u32 r_ev;
2781 int rc;
2783 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2784 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2785 return -EINVAL;
2787 if (mutex_lock_killable(&hotkey_mutex))
2788 return -ERESTARTSYS;
2790 HOTKEY_CONFIG_CRITICAL_START
2791 hotkey_source_mask = t;
2792 HOTKEY_CONFIG_CRITICAL_END
2794 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2795 ~hotkey_source_mask);
2796 hotkey_poll_setup(true);
2798 /* check if events needed by the driver got disabled */
2799 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2800 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2802 mutex_unlock(&hotkey_mutex);
2804 if (rc < 0)
2805 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2806 "firmware event mask!\n");
2808 if (r_ev)
2809 printk(TPACPI_NOTICE "hotkey_source_mask: "
2810 "some important events were disabled: "
2811 "0x%04x\n", r_ev);
2813 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2815 return (rc < 0) ? rc : count;
2818 static struct device_attribute dev_attr_hotkey_source_mask =
2819 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2820 hotkey_source_mask_show, hotkey_source_mask_store);
2822 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2823 static ssize_t hotkey_poll_freq_show(struct device *dev,
2824 struct device_attribute *attr,
2825 char *buf)
2827 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2830 static ssize_t hotkey_poll_freq_store(struct device *dev,
2831 struct device_attribute *attr,
2832 const char *buf, size_t count)
2834 unsigned long t;
2836 if (parse_strtoul(buf, 25, &t))
2837 return -EINVAL;
2839 if (mutex_lock_killable(&hotkey_mutex))
2840 return -ERESTARTSYS;
2842 hotkey_poll_set_freq(t);
2843 hotkey_poll_setup(true);
2845 mutex_unlock(&hotkey_mutex);
2847 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2849 return count;
2852 static struct device_attribute dev_attr_hotkey_poll_freq =
2853 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2854 hotkey_poll_freq_show, hotkey_poll_freq_store);
2856 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2858 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2859 static ssize_t hotkey_radio_sw_show(struct device *dev,
2860 struct device_attribute *attr,
2861 char *buf)
2863 int res;
2864 res = hotkey_get_wlsw();
2865 if (res < 0)
2866 return res;
2868 /* Opportunistic update */
2869 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2871 return snprintf(buf, PAGE_SIZE, "%d\n",
2872 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2875 static struct device_attribute dev_attr_hotkey_radio_sw =
2876 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2878 static void hotkey_radio_sw_notify_change(void)
2880 if (tp_features.hotkey_wlsw)
2881 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2882 "hotkey_radio_sw");
2885 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2886 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2887 struct device_attribute *attr,
2888 char *buf)
2890 int res, s;
2891 res = hotkey_get_tablet_mode(&s);
2892 if (res < 0)
2893 return res;
2895 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2898 static struct device_attribute dev_attr_hotkey_tablet_mode =
2899 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2901 static void hotkey_tablet_mode_notify_change(void)
2903 if (tp_features.hotkey_tablet)
2904 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2905 "hotkey_tablet_mode");
2908 /* sysfs hotkey report_mode -------------------------------------------- */
2909 static ssize_t hotkey_report_mode_show(struct device *dev,
2910 struct device_attribute *attr,
2911 char *buf)
2913 return snprintf(buf, PAGE_SIZE, "%d\n",
2914 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2917 static struct device_attribute dev_attr_hotkey_report_mode =
2918 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2920 /* sysfs wakeup reason (pollable) -------------------------------------- */
2921 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2922 struct device_attribute *attr,
2923 char *buf)
2925 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2928 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2929 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2931 static void hotkey_wakeup_reason_notify_change(void)
2933 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2934 "wakeup_reason");
2937 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2938 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2939 struct device_attribute *attr,
2940 char *buf)
2942 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2945 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2946 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2947 hotkey_wakeup_hotunplug_complete_show, NULL);
2949 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2951 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2952 "wakeup_hotunplug_complete");
2955 /* --------------------------------------------------------------------- */
2957 static struct attribute *hotkey_attributes[] __initdata = {
2958 &dev_attr_hotkey_enable.attr,
2959 &dev_attr_hotkey_bios_enabled.attr,
2960 &dev_attr_hotkey_bios_mask.attr,
2961 &dev_attr_hotkey_report_mode.attr,
2962 &dev_attr_hotkey_wakeup_reason.attr,
2963 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2964 &dev_attr_hotkey_mask.attr,
2965 &dev_attr_hotkey_all_mask.attr,
2966 &dev_attr_hotkey_recommended_mask.attr,
2967 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2968 &dev_attr_hotkey_source_mask.attr,
2969 &dev_attr_hotkey_poll_freq.attr,
2970 #endif
2974 * Sync both the hw and sw blocking state of all switches
2976 static void tpacpi_send_radiosw_update(void)
2978 int wlsw;
2981 * We must sync all rfkill controllers *before* issuing any
2982 * rfkill input events, or we will race the rfkill core input
2983 * handler.
2985 * tpacpi_inputdev_send_mutex works as a syncronization point
2986 * for the above.
2988 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2991 wlsw = hotkey_get_wlsw();
2993 /* Sync hw blocking state first if it is hw-blocked */
2994 if (wlsw == TPACPI_RFK_RADIO_OFF)
2995 tpacpi_rfk_update_hwblock_state(true);
2997 /* Sync sw blocking state */
2998 tpacpi_rfk_update_swstate_all();
3000 /* Sync hw blocking state last if it is hw-unblocked */
3001 if (wlsw == TPACPI_RFK_RADIO_ON)
3002 tpacpi_rfk_update_hwblock_state(false);
3004 /* Issue rfkill input event for WLSW switch */
3005 if (!(wlsw < 0)) {
3006 mutex_lock(&tpacpi_inputdev_send_mutex);
3008 input_report_switch(tpacpi_inputdev,
3009 SW_RFKILL_ALL, (wlsw > 0));
3010 input_sync(tpacpi_inputdev);
3012 mutex_unlock(&tpacpi_inputdev_send_mutex);
3016 * this can be unconditional, as we will poll state again
3017 * if userspace uses the notify to read data
3019 hotkey_radio_sw_notify_change();
3022 static void hotkey_exit(void)
3024 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3025 mutex_lock(&hotkey_mutex);
3026 hotkey_poll_stop_sync();
3027 mutex_unlock(&hotkey_mutex);
3028 #endif
3030 if (hotkey_dev_attributes)
3031 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3033 kfree(hotkey_keycode_map);
3035 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3036 "restoring original HKEY status and mask\n");
3037 /* yes, there is a bitwise or below, we want the
3038 * functions to be called even if one of them fail */
3039 if (((tp_features.hotkey_mask &&
3040 hotkey_mask_set(hotkey_orig_mask)) |
3041 hotkey_status_set(false)) != 0)
3042 printk(TPACPI_ERR
3043 "failed to restore hot key mask "
3044 "to BIOS defaults\n");
3047 static void __init hotkey_unmap(const unsigned int scancode)
3049 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3050 clear_bit(hotkey_keycode_map[scancode],
3051 tpacpi_inputdev->keybit);
3052 hotkey_keycode_map[scancode] = KEY_RESERVED;
3057 * HKEY quirks:
3058 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3061 #define TPACPI_HK_Q_INIMASK 0x0001
3063 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3064 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3065 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3066 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3067 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3068 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3069 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3070 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3071 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3072 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3073 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3074 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3075 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3076 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3077 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3078 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3079 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3080 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3081 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3082 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3085 static int __init hotkey_init(struct ibm_init_struct *iibm)
3087 /* Requirements for changing the default keymaps:
3089 * 1. Many of the keys are mapped to KEY_RESERVED for very
3090 * good reasons. Do not change them unless you have deep
3091 * knowledge on the IBM and Lenovo ThinkPad firmware for
3092 * the various ThinkPad models. The driver behaves
3093 * differently for KEY_RESERVED: such keys have their
3094 * hot key mask *unset* in mask_recommended, and also
3095 * in the initial hot key mask programmed into the
3096 * firmware at driver load time, which means the firm-
3097 * ware may react very differently if you change them to
3098 * something else;
3100 * 2. You must be subscribed to the linux-thinkpad and
3101 * ibm-acpi-devel mailing lists, and you should read the
3102 * list archives since 2007 if you want to change the
3103 * keymaps. This requirement exists so that you will
3104 * know the past history of problems with the thinkpad-
3105 * acpi driver keymaps, and also that you will be
3106 * listening to any bug reports;
3108 * 3. Do not send thinkpad-acpi specific patches directly to
3109 * for merging, *ever*. Send them to the linux-acpi
3110 * mailinglist for comments. Merging is to be done only
3111 * through acpi-test and the ACPI maintainer.
3113 * If the above is too much to ask, don't change the keymap.
3114 * Ask the thinkpad-acpi maintainer to do it, instead.
3116 static u16 ibm_keycode_map[] __initdata = {
3117 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3118 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3119 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3120 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3122 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3123 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3124 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3125 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3127 /* brightness: firmware always reacts to them */
3128 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3129 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3131 /* Thinklight: firmware always react to it */
3132 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3134 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3135 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3137 /* Volume: firmware always react to it and reprograms
3138 * the built-in *extra* mixer. Never map it to control
3139 * another mixer by default. */
3140 KEY_RESERVED, /* 0x14: VOLUME UP */
3141 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3142 KEY_RESERVED, /* 0x16: MUTE */
3144 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3146 /* (assignments unknown, please report if found) */
3147 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3148 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3150 static u16 lenovo_keycode_map[] __initdata = {
3151 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3152 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3153 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3154 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3156 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3157 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3158 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3159 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3161 /* These should be enabled --only-- when ACPI video
3162 * is disabled (i.e. in "vendor" mode), and are handled
3163 * in a special way by the init code */
3164 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3165 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3167 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3169 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3170 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3172 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3173 * react to it and reprograms the built-in *extra* mixer.
3174 * Never map it to control another mixer by default.
3176 * T60?, T61, R60?, R61: firmware and EC tries to send
3177 * these over the regular keyboard, so these are no-ops,
3178 * but there are still weird bugs re. MUTE, so do not
3179 * change unless you get test reports from all Lenovo
3180 * models. May cause the BIOS to interfere with the
3181 * HDA mixer.
3183 KEY_RESERVED, /* 0x14: VOLUME UP */
3184 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3185 KEY_RESERVED, /* 0x16: MUTE */
3187 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3189 /* (assignments unknown, please report if found) */
3190 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3191 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3194 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3195 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3196 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3198 int res, i;
3199 int status;
3200 int hkeyv;
3201 bool radiosw_state = false;
3202 bool tabletsw_state = false;
3204 unsigned long quirks;
3206 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3207 "initializing hotkey subdriver\n");
3209 BUG_ON(!tpacpi_inputdev);
3210 BUG_ON(tpacpi_inputdev->open != NULL ||
3211 tpacpi_inputdev->close != NULL);
3213 TPACPI_ACPIHANDLE_INIT(hkey);
3214 mutex_init(&hotkey_mutex);
3216 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3217 mutex_init(&hotkey_thread_mutex);
3218 mutex_init(&hotkey_thread_data_mutex);
3219 #endif
3221 /* hotkey not supported on 570 */
3222 tp_features.hotkey = hkey_handle != NULL;
3224 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3225 "hotkeys are %s\n",
3226 str_supported(tp_features.hotkey));
3228 if (!tp_features.hotkey)
3229 return 1;
3231 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3232 ARRAY_SIZE(tpacpi_hotkey_qtable));
3234 tpacpi_disable_brightness_delay();
3236 /* MUST have enough space for all attributes to be added to
3237 * hotkey_dev_attributes */
3238 hotkey_dev_attributes = create_attr_set(
3239 ARRAY_SIZE(hotkey_attributes) + 2,
3240 NULL);
3241 if (!hotkey_dev_attributes)
3242 return -ENOMEM;
3243 res = add_many_to_attr_set(hotkey_dev_attributes,
3244 hotkey_attributes,
3245 ARRAY_SIZE(hotkey_attributes));
3246 if (res)
3247 goto err_exit;
3249 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3250 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3251 for HKEY interface version 0x100 */
3252 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3253 if ((hkeyv >> 8) != 1) {
3254 printk(TPACPI_ERR "unknown version of the "
3255 "HKEY interface: 0x%x\n", hkeyv);
3256 printk(TPACPI_ERR "please report this to %s\n",
3257 TPACPI_MAIL);
3258 } else {
3260 * MHKV 0x100 in A31, R40, R40e,
3261 * T4x, X31, and later
3263 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3264 "firmware HKEY interface version: 0x%x\n",
3265 hkeyv);
3267 /* Paranoia check AND init hotkey_all_mask */
3268 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3269 "MHKA", "qd")) {
3270 printk(TPACPI_ERR
3271 "missing MHKA handler, "
3272 "please report this to %s\n",
3273 TPACPI_MAIL);
3274 /* Fallback: pre-init for FN+F3,F4,F12 */
3275 hotkey_all_mask = 0x080cU;
3276 } else {
3277 tp_features.hotkey_mask = 1;
3282 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3283 "hotkey masks are %s\n",
3284 str_supported(tp_features.hotkey_mask));
3286 /* Init hotkey_all_mask if not initialized yet */
3287 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3288 (quirks & TPACPI_HK_Q_INIMASK))
3289 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3291 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3292 if (tp_features.hotkey_mask) {
3293 /* hotkey_source_mask *must* be zero for
3294 * the first hotkey_mask_get to return hotkey_orig_mask */
3295 res = hotkey_mask_get();
3296 if (res)
3297 goto err_exit;
3299 hotkey_orig_mask = hotkey_acpi_mask;
3300 } else {
3301 hotkey_orig_mask = hotkey_all_mask;
3302 hotkey_acpi_mask = hotkey_all_mask;
3305 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3306 if (dbg_wlswemul) {
3307 tp_features.hotkey_wlsw = 1;
3308 radiosw_state = !!tpacpi_wlsw_emulstate;
3309 printk(TPACPI_INFO
3310 "radio switch emulation enabled\n");
3311 } else
3312 #endif
3313 /* Not all thinkpads have a hardware radio switch */
3314 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3315 tp_features.hotkey_wlsw = 1;
3316 radiosw_state = !!status;
3317 printk(TPACPI_INFO
3318 "radio switch found; radios are %s\n",
3319 enabled(status, 0));
3321 if (tp_features.hotkey_wlsw)
3322 res = add_to_attr_set(hotkey_dev_attributes,
3323 &dev_attr_hotkey_radio_sw.attr);
3325 /* For X41t, X60t, X61t Tablets... */
3326 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3327 tp_features.hotkey_tablet = 1;
3328 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3329 printk(TPACPI_INFO
3330 "possible tablet mode switch found; "
3331 "ThinkPad in %s mode\n",
3332 (tabletsw_state) ? "tablet" : "laptop");
3333 res = add_to_attr_set(hotkey_dev_attributes,
3334 &dev_attr_hotkey_tablet_mode.attr);
3337 if (!res)
3338 res = register_attr_set_with_sysfs(
3339 hotkey_dev_attributes,
3340 &tpacpi_pdev->dev.kobj);
3341 if (res)
3342 goto err_exit;
3344 /* Set up key map */
3346 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3347 GFP_KERNEL);
3348 if (!hotkey_keycode_map) {
3349 printk(TPACPI_ERR
3350 "failed to allocate memory for key map\n");
3351 res = -ENOMEM;
3352 goto err_exit;
3355 if (tpacpi_is_lenovo()) {
3356 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3357 "using Lenovo default hot key map\n");
3358 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3359 TPACPI_HOTKEY_MAP_SIZE);
3360 } else {
3361 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3362 "using IBM default hot key map\n");
3363 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3364 TPACPI_HOTKEY_MAP_SIZE);
3367 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3368 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3369 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3370 tpacpi_inputdev->keycode = hotkey_keycode_map;
3371 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3372 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3373 input_set_capability(tpacpi_inputdev, EV_KEY,
3374 hotkey_keycode_map[i]);
3375 } else {
3376 if (i < sizeof(hotkey_reserved_mask)*8)
3377 hotkey_reserved_mask |= 1 << i;
3381 if (tp_features.hotkey_wlsw) {
3382 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3383 input_report_switch(tpacpi_inputdev,
3384 SW_RFKILL_ALL, radiosw_state);
3386 if (tp_features.hotkey_tablet) {
3387 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3388 input_report_switch(tpacpi_inputdev,
3389 SW_TABLET_MODE, tabletsw_state);
3392 /* Do not issue duplicate brightness change events to
3393 * userspace. tpacpi_detect_brightness_capabilities() must have
3394 * been called before this point */
3395 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3396 printk(TPACPI_INFO
3397 "This ThinkPad has standard ACPI backlight "
3398 "brightness control, supported by the ACPI "
3399 "video driver\n");
3400 printk(TPACPI_NOTICE
3401 "Disabling thinkpad-acpi brightness events "
3402 "by default...\n");
3404 /* Disable brightness up/down on Lenovo thinkpads when
3405 * ACPI is handling them, otherwise it is plain impossible
3406 * for userspace to do something even remotely sane */
3407 hotkey_reserved_mask |=
3408 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3409 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3410 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3411 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3414 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3415 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3416 & ~hotkey_all_mask
3417 & ~hotkey_reserved_mask;
3419 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3420 "hotkey source mask 0x%08x, polling freq %u\n",
3421 hotkey_source_mask, hotkey_poll_freq);
3422 #endif
3424 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3425 "enabling firmware HKEY event interface...\n");
3426 res = hotkey_status_set(true);
3427 if (res) {
3428 hotkey_exit();
3429 return res;
3431 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3432 | hotkey_driver_mask)
3433 & ~hotkey_source_mask);
3434 if (res < 0 && res != -ENXIO) {
3435 hotkey_exit();
3436 return res;
3438 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3439 & ~hotkey_reserved_mask;
3440 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3441 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3442 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3444 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3445 "legacy ibm/hotkey event reporting over procfs %s\n",
3446 (hotkey_report_mode < 2) ?
3447 "enabled" : "disabled");
3449 tpacpi_inputdev->open = &hotkey_inputdev_open;
3450 tpacpi_inputdev->close = &hotkey_inputdev_close;
3452 hotkey_poll_setup_safe(true);
3454 return 0;
3456 err_exit:
3457 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3458 hotkey_dev_attributes = NULL;
3460 return (res < 0)? res : 1;
3463 static bool hotkey_notify_hotkey(const u32 hkey,
3464 bool *send_acpi_ev,
3465 bool *ignore_acpi_ev)
3467 /* 0x1000-0x1FFF: key presses */
3468 unsigned int scancode = hkey & 0xfff;
3469 *send_acpi_ev = true;
3470 *ignore_acpi_ev = false;
3472 if (scancode > 0 && scancode < 0x21) {
3473 scancode--;
3474 if (!(hotkey_source_mask & (1 << scancode))) {
3475 tpacpi_input_send_key_masked(scancode);
3476 *send_acpi_ev = false;
3477 } else {
3478 *ignore_acpi_ev = true;
3480 return true;
3482 return false;
3485 static bool hotkey_notify_wakeup(const u32 hkey,
3486 bool *send_acpi_ev,
3487 bool *ignore_acpi_ev)
3489 /* 0x2000-0x2FFF: Wakeup reason */
3490 *send_acpi_ev = true;
3491 *ignore_acpi_ev = false;
3493 switch (hkey) {
3494 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3495 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3496 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3497 *ignore_acpi_ev = true;
3498 break;
3500 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3501 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3502 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3503 *ignore_acpi_ev = true;
3504 break;
3506 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3507 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3508 printk(TPACPI_ALERT
3509 "EMERGENCY WAKEUP: battery almost empty\n");
3510 /* how to auto-heal: */
3511 /* 2313: woke up from S3, go to S4/S5 */
3512 /* 2413: woke up from S4, go to S5 */
3513 break;
3515 default:
3516 return false;
3519 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3520 printk(TPACPI_INFO
3521 "woke up due to a hot-unplug "
3522 "request...\n");
3523 hotkey_wakeup_reason_notify_change();
3525 return true;
3528 static bool hotkey_notify_usrevent(const u32 hkey,
3529 bool *send_acpi_ev,
3530 bool *ignore_acpi_ev)
3532 /* 0x5000-0x5FFF: human interface helpers */
3533 *send_acpi_ev = true;
3534 *ignore_acpi_ev = false;
3536 switch (hkey) {
3537 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3538 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3539 return true;
3541 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3542 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3543 tpacpi_input_send_tabletsw();
3544 hotkey_tablet_mode_notify_change();
3545 *send_acpi_ev = false;
3546 return true;
3548 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3549 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3550 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3551 /* do not propagate these events */
3552 *ignore_acpi_ev = true;
3553 return true;
3555 default:
3556 return false;
3560 static void thermal_dump_all_sensors(void);
3562 static bool hotkey_notify_thermal(const u32 hkey,
3563 bool *send_acpi_ev,
3564 bool *ignore_acpi_ev)
3566 bool known = true;
3568 /* 0x6000-0x6FFF: thermal alarms */
3569 *send_acpi_ev = true;
3570 *ignore_acpi_ev = false;
3572 switch (hkey) {
3573 case TP_HKEY_EV_THM_TABLE_CHANGED:
3574 printk(TPACPI_INFO
3575 "EC reports that Thermal Table has changed\n");
3576 /* recommended action: do nothing, we don't have
3577 * Lenovo ATM information */
3578 return true;
3579 case TP_HKEY_EV_ALARM_BAT_HOT:
3580 printk(TPACPI_CRIT
3581 "THERMAL ALARM: battery is too hot!\n");
3582 /* recommended action: warn user through gui */
3583 break;
3584 case TP_HKEY_EV_ALARM_BAT_XHOT:
3585 printk(TPACPI_ALERT
3586 "THERMAL EMERGENCY: battery is extremely hot!\n");
3587 /* recommended action: immediate sleep/hibernate */
3588 break;
3589 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3590 printk(TPACPI_CRIT
3591 "THERMAL ALARM: "
3592 "a sensor reports something is too hot!\n");
3593 /* recommended action: warn user through gui, that */
3594 /* some internal component is too hot */
3595 break;
3596 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3597 printk(TPACPI_ALERT
3598 "THERMAL EMERGENCY: "
3599 "a sensor reports something is extremely hot!\n");
3600 /* recommended action: immediate sleep/hibernate */
3601 break;
3602 default:
3603 printk(TPACPI_ALERT
3604 "THERMAL ALERT: unknown thermal alarm received\n");
3605 known = false;
3608 thermal_dump_all_sensors();
3610 return known;
3613 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3615 u32 hkey;
3616 bool send_acpi_ev;
3617 bool ignore_acpi_ev;
3618 bool known_ev;
3620 if (event != 0x80) {
3621 printk(TPACPI_ERR
3622 "unknown HKEY notification event %d\n", event);
3623 /* forward it to userspace, maybe it knows how to handle it */
3624 acpi_bus_generate_netlink_event(
3625 ibm->acpi->device->pnp.device_class,
3626 dev_name(&ibm->acpi->device->dev),
3627 event, 0);
3628 return;
3631 while (1) {
3632 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3633 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3634 return;
3637 if (hkey == 0) {
3638 /* queue empty */
3639 return;
3642 send_acpi_ev = true;
3643 ignore_acpi_ev = false;
3645 switch (hkey >> 12) {
3646 case 1:
3647 /* 0x1000-0x1FFF: key presses */
3648 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3649 &ignore_acpi_ev);
3650 break;
3651 case 2:
3652 /* 0x2000-0x2FFF: Wakeup reason */
3653 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3654 &ignore_acpi_ev);
3655 break;
3656 case 3:
3657 /* 0x3000-0x3FFF: bay-related wakeups */
3658 switch (hkey) {
3659 case TP_HKEY_EV_BAYEJ_ACK:
3660 hotkey_autosleep_ack = 1;
3661 printk(TPACPI_INFO
3662 "bay ejected\n");
3663 hotkey_wakeup_hotunplug_complete_notify_change();
3664 known_ev = true;
3665 break;
3666 case TP_HKEY_EV_OPTDRV_EJ:
3667 /* FIXME: kick libata if SATA link offline */
3668 known_ev = true;
3669 break;
3670 default:
3671 known_ev = false;
3673 break;
3674 case 4:
3675 /* 0x4000-0x4FFF: dock-related wakeups */
3676 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3677 hotkey_autosleep_ack = 1;
3678 printk(TPACPI_INFO
3679 "undocked\n");
3680 hotkey_wakeup_hotunplug_complete_notify_change();
3681 known_ev = true;
3682 } else {
3683 known_ev = false;
3685 break;
3686 case 5:
3687 /* 0x5000-0x5FFF: human interface helpers */
3688 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3689 &ignore_acpi_ev);
3690 break;
3691 case 6:
3692 /* 0x6000-0x6FFF: thermal alarms */
3693 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3694 &ignore_acpi_ev);
3695 break;
3696 case 7:
3697 /* 0x7000-0x7FFF: misc */
3698 if (tp_features.hotkey_wlsw &&
3699 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3700 tpacpi_send_radiosw_update();
3701 send_acpi_ev = 0;
3702 known_ev = true;
3703 break;
3705 /* fallthrough to default */
3706 default:
3707 known_ev = false;
3709 if (!known_ev) {
3710 printk(TPACPI_NOTICE
3711 "unhandled HKEY event 0x%04x\n", hkey);
3712 printk(TPACPI_NOTICE
3713 "please report the conditions when this "
3714 "event happened to %s\n", TPACPI_MAIL);
3717 /* Legacy events */
3718 if (!ignore_acpi_ev &&
3719 (send_acpi_ev || hotkey_report_mode < 2)) {
3720 acpi_bus_generate_proc_event(ibm->acpi->device,
3721 event, hkey);
3724 /* netlink events */
3725 if (!ignore_acpi_ev && send_acpi_ev) {
3726 acpi_bus_generate_netlink_event(
3727 ibm->acpi->device->pnp.device_class,
3728 dev_name(&ibm->acpi->device->dev),
3729 event, hkey);
3734 static void hotkey_suspend(pm_message_t state)
3736 /* Do these on suspend, we get the events on early resume! */
3737 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3738 hotkey_autosleep_ack = 0;
3741 static void hotkey_resume(void)
3743 tpacpi_disable_brightness_delay();
3745 if (hotkey_status_set(true) < 0 ||
3746 hotkey_mask_set(hotkey_acpi_mask) < 0)
3747 printk(TPACPI_ERR
3748 "error while attempting to reset the event "
3749 "firmware interface\n");
3751 tpacpi_send_radiosw_update();
3752 hotkey_tablet_mode_notify_change();
3753 hotkey_wakeup_reason_notify_change();
3754 hotkey_wakeup_hotunplug_complete_notify_change();
3755 hotkey_poll_setup_safe(false);
3758 /* procfs -------------------------------------------------------------- */
3759 static int hotkey_read(struct seq_file *m)
3761 int res, status;
3763 if (!tp_features.hotkey) {
3764 seq_printf(m, "status:\t\tnot supported\n");
3765 return 0;
3768 if (mutex_lock_killable(&hotkey_mutex))
3769 return -ERESTARTSYS;
3770 res = hotkey_status_get(&status);
3771 if (!res)
3772 res = hotkey_mask_get();
3773 mutex_unlock(&hotkey_mutex);
3774 if (res)
3775 return res;
3777 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3778 if (hotkey_all_mask) {
3779 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3780 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3781 } else {
3782 seq_printf(m, "mask:\t\tnot supported\n");
3783 seq_printf(m, "commands:\tenable, disable, reset\n");
3786 return 0;
3789 static void hotkey_enabledisable_warn(bool enable)
3791 tpacpi_log_usertask("procfs hotkey enable/disable");
3792 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3793 TPACPI_WARN
3794 "hotkey enable/disable functionality has been "
3795 "removed from the driver. Hotkeys are always "
3796 "enabled\n"))
3797 printk(TPACPI_ERR
3798 "Please remove the hotkey=enable module "
3799 "parameter, it is deprecated. Hotkeys are always "
3800 "enabled\n");
3803 static int hotkey_write(char *buf)
3805 int res;
3806 u32 mask;
3807 char *cmd;
3809 if (!tp_features.hotkey)
3810 return -ENODEV;
3812 if (mutex_lock_killable(&hotkey_mutex))
3813 return -ERESTARTSYS;
3815 mask = hotkey_user_mask;
3817 res = 0;
3818 while ((cmd = next_cmd(&buf))) {
3819 if (strlencmp(cmd, "enable") == 0) {
3820 hotkey_enabledisable_warn(1);
3821 } else if (strlencmp(cmd, "disable") == 0) {
3822 hotkey_enabledisable_warn(0);
3823 res = -EPERM;
3824 } else if (strlencmp(cmd, "reset") == 0) {
3825 mask = (hotkey_all_mask | hotkey_source_mask)
3826 & ~hotkey_reserved_mask;
3827 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3828 /* mask set */
3829 } else if (sscanf(cmd, "%x", &mask) == 1) {
3830 /* mask set */
3831 } else {
3832 res = -EINVAL;
3833 goto errexit;
3837 if (!res) {
3838 tpacpi_disclose_usertask("procfs hotkey",
3839 "set mask to 0x%08x\n", mask);
3840 res = hotkey_user_mask_set(mask);
3843 errexit:
3844 mutex_unlock(&hotkey_mutex);
3845 return res;
3848 static const struct acpi_device_id ibm_htk_device_ids[] = {
3849 {TPACPI_ACPI_HKEY_HID, 0},
3850 {"", 0},
3853 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3854 .hid = ibm_htk_device_ids,
3855 .notify = hotkey_notify,
3856 .handle = &hkey_handle,
3857 .type = ACPI_DEVICE_NOTIFY,
3860 static struct ibm_struct hotkey_driver_data = {
3861 .name = "hotkey",
3862 .read = hotkey_read,
3863 .write = hotkey_write,
3864 .exit = hotkey_exit,
3865 .resume = hotkey_resume,
3866 .suspend = hotkey_suspend,
3867 .acpi = &ibm_hotkey_acpidriver,
3870 /*************************************************************************
3871 * Bluetooth subdriver
3874 enum {
3875 /* ACPI GBDC/SBDC bits */
3876 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3877 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3878 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3879 0 = disable, 1 = enable */
3882 enum {
3883 /* ACPI \BLTH commands */
3884 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3885 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3886 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3887 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3888 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3891 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3893 static int bluetooth_get_status(void)
3895 int status;
3897 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3898 if (dbg_bluetoothemul)
3899 return (tpacpi_bluetooth_emulstate) ?
3900 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3901 #endif
3903 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3904 return -EIO;
3906 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3907 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3910 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3912 int status;
3914 vdbg_printk(TPACPI_DBG_RFKILL,
3915 "will attempt to %s bluetooth\n",
3916 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3918 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3919 if (dbg_bluetoothemul) {
3920 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3921 return 0;
3923 #endif
3925 if (state == TPACPI_RFK_RADIO_ON)
3926 status = TP_ACPI_BLUETOOTH_RADIOSSW
3927 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3928 else
3929 status = 0;
3931 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3932 return -EIO;
3934 return 0;
3937 /* sysfs bluetooth enable ---------------------------------------------- */
3938 static ssize_t bluetooth_enable_show(struct device *dev,
3939 struct device_attribute *attr,
3940 char *buf)
3942 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3943 attr, buf);
3946 static ssize_t bluetooth_enable_store(struct device *dev,
3947 struct device_attribute *attr,
3948 const char *buf, size_t count)
3950 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3951 attr, buf, count);
3954 static struct device_attribute dev_attr_bluetooth_enable =
3955 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3956 bluetooth_enable_show, bluetooth_enable_store);
3958 /* --------------------------------------------------------------------- */
3960 static struct attribute *bluetooth_attributes[] = {
3961 &dev_attr_bluetooth_enable.attr,
3962 NULL
3965 static const struct attribute_group bluetooth_attr_group = {
3966 .attrs = bluetooth_attributes,
3969 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3970 .get_status = bluetooth_get_status,
3971 .set_status = bluetooth_set_status,
3974 static void bluetooth_shutdown(void)
3976 /* Order firmware to save current state to NVRAM */
3977 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3978 TP_ACPI_BLTH_SAVE_STATE))
3979 printk(TPACPI_NOTICE
3980 "failed to save bluetooth state to NVRAM\n");
3981 else
3982 vdbg_printk(TPACPI_DBG_RFKILL,
3983 "bluestooth state saved to NVRAM\n");
3986 static void bluetooth_exit(void)
3988 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3989 &bluetooth_attr_group);
3991 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3993 bluetooth_shutdown();
3996 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3998 int res;
3999 int status = 0;
4001 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4002 "initializing bluetooth subdriver\n");
4004 TPACPI_ACPIHANDLE_INIT(hkey);
4006 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4007 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4008 tp_features.bluetooth = hkey_handle &&
4009 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4011 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4012 "bluetooth is %s, status 0x%02x\n",
4013 str_supported(tp_features.bluetooth),
4014 status);
4016 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4017 if (dbg_bluetoothemul) {
4018 tp_features.bluetooth = 1;
4019 printk(TPACPI_INFO
4020 "bluetooth switch emulation enabled\n");
4021 } else
4022 #endif
4023 if (tp_features.bluetooth &&
4024 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4025 /* no bluetooth hardware present in system */
4026 tp_features.bluetooth = 0;
4027 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4028 "bluetooth hardware not installed\n");
4031 if (!tp_features.bluetooth)
4032 return 1;
4034 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4035 &bluetooth_tprfk_ops,
4036 RFKILL_TYPE_BLUETOOTH,
4037 TPACPI_RFK_BLUETOOTH_SW_NAME,
4038 true);
4039 if (res)
4040 return res;
4042 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4043 &bluetooth_attr_group);
4044 if (res) {
4045 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4046 return res;
4049 return 0;
4052 /* procfs -------------------------------------------------------------- */
4053 static int bluetooth_read(struct seq_file *m)
4055 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4058 static int bluetooth_write(char *buf)
4060 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4063 static struct ibm_struct bluetooth_driver_data = {
4064 .name = "bluetooth",
4065 .read = bluetooth_read,
4066 .write = bluetooth_write,
4067 .exit = bluetooth_exit,
4068 .shutdown = bluetooth_shutdown,
4071 /*************************************************************************
4072 * Wan subdriver
4075 enum {
4076 /* ACPI GWAN/SWAN bits */
4077 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4078 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4079 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4080 0 = disable, 1 = enable */
4083 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4085 static int wan_get_status(void)
4087 int status;
4089 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4090 if (dbg_wwanemul)
4091 return (tpacpi_wwan_emulstate) ?
4092 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4093 #endif
4095 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4096 return -EIO;
4098 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4099 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4102 static int wan_set_status(enum tpacpi_rfkill_state state)
4104 int status;
4106 vdbg_printk(TPACPI_DBG_RFKILL,
4107 "will attempt to %s wwan\n",
4108 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4110 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4111 if (dbg_wwanemul) {
4112 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4113 return 0;
4115 #endif
4117 if (state == TPACPI_RFK_RADIO_ON)
4118 status = TP_ACPI_WANCARD_RADIOSSW
4119 | TP_ACPI_WANCARD_RESUMECTRL;
4120 else
4121 status = 0;
4123 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4124 return -EIO;
4126 return 0;
4129 /* sysfs wan enable ---------------------------------------------------- */
4130 static ssize_t wan_enable_show(struct device *dev,
4131 struct device_attribute *attr,
4132 char *buf)
4134 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4135 attr, buf);
4138 static ssize_t wan_enable_store(struct device *dev,
4139 struct device_attribute *attr,
4140 const char *buf, size_t count)
4142 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4143 attr, buf, count);
4146 static struct device_attribute dev_attr_wan_enable =
4147 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4148 wan_enable_show, wan_enable_store);
4150 /* --------------------------------------------------------------------- */
4152 static struct attribute *wan_attributes[] = {
4153 &dev_attr_wan_enable.attr,
4154 NULL
4157 static const struct attribute_group wan_attr_group = {
4158 .attrs = wan_attributes,
4161 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4162 .get_status = wan_get_status,
4163 .set_status = wan_set_status,
4166 static void wan_shutdown(void)
4168 /* Order firmware to save current state to NVRAM */
4169 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4170 TP_ACPI_WGSV_SAVE_STATE))
4171 printk(TPACPI_NOTICE
4172 "failed to save WWAN state to NVRAM\n");
4173 else
4174 vdbg_printk(TPACPI_DBG_RFKILL,
4175 "WWAN state saved to NVRAM\n");
4178 static void wan_exit(void)
4180 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4181 &wan_attr_group);
4183 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4185 wan_shutdown();
4188 static int __init wan_init(struct ibm_init_struct *iibm)
4190 int res;
4191 int status = 0;
4193 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4194 "initializing wan subdriver\n");
4196 TPACPI_ACPIHANDLE_INIT(hkey);
4198 tp_features.wan = hkey_handle &&
4199 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4201 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4202 "wan is %s, status 0x%02x\n",
4203 str_supported(tp_features.wan),
4204 status);
4206 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4207 if (dbg_wwanemul) {
4208 tp_features.wan = 1;
4209 printk(TPACPI_INFO
4210 "wwan switch emulation enabled\n");
4211 } else
4212 #endif
4213 if (tp_features.wan &&
4214 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4215 /* no wan hardware present in system */
4216 tp_features.wan = 0;
4217 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4218 "wan hardware not installed\n");
4221 if (!tp_features.wan)
4222 return 1;
4224 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4225 &wan_tprfk_ops,
4226 RFKILL_TYPE_WWAN,
4227 TPACPI_RFK_WWAN_SW_NAME,
4228 true);
4229 if (res)
4230 return res;
4232 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4233 &wan_attr_group);
4235 if (res) {
4236 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4237 return res;
4240 return 0;
4243 /* procfs -------------------------------------------------------------- */
4244 static int wan_read(struct seq_file *m)
4246 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4249 static int wan_write(char *buf)
4251 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4254 static struct ibm_struct wan_driver_data = {
4255 .name = "wan",
4256 .read = wan_read,
4257 .write = wan_write,
4258 .exit = wan_exit,
4259 .shutdown = wan_shutdown,
4262 /*************************************************************************
4263 * UWB subdriver
4266 enum {
4267 /* ACPI GUWB/SUWB bits */
4268 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4269 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4272 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4274 static int uwb_get_status(void)
4276 int status;
4278 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4279 if (dbg_uwbemul)
4280 return (tpacpi_uwb_emulstate) ?
4281 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4282 #endif
4284 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4285 return -EIO;
4287 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4288 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4291 static int uwb_set_status(enum tpacpi_rfkill_state state)
4293 int status;
4295 vdbg_printk(TPACPI_DBG_RFKILL,
4296 "will attempt to %s UWB\n",
4297 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4299 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4300 if (dbg_uwbemul) {
4301 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4302 return 0;
4304 #endif
4306 if (state == TPACPI_RFK_RADIO_ON)
4307 status = TP_ACPI_UWB_RADIOSSW;
4308 else
4309 status = 0;
4311 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4312 return -EIO;
4314 return 0;
4317 /* --------------------------------------------------------------------- */
4319 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4320 .get_status = uwb_get_status,
4321 .set_status = uwb_set_status,
4324 static void uwb_exit(void)
4326 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4329 static int __init uwb_init(struct ibm_init_struct *iibm)
4331 int res;
4332 int status = 0;
4334 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4335 "initializing uwb subdriver\n");
4337 TPACPI_ACPIHANDLE_INIT(hkey);
4339 tp_features.uwb = hkey_handle &&
4340 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4342 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4343 "uwb is %s, status 0x%02x\n",
4344 str_supported(tp_features.uwb),
4345 status);
4347 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4348 if (dbg_uwbemul) {
4349 tp_features.uwb = 1;
4350 printk(TPACPI_INFO
4351 "uwb switch emulation enabled\n");
4352 } else
4353 #endif
4354 if (tp_features.uwb &&
4355 !(status & TP_ACPI_UWB_HWPRESENT)) {
4356 /* no uwb hardware present in system */
4357 tp_features.uwb = 0;
4358 dbg_printk(TPACPI_DBG_INIT,
4359 "uwb hardware not installed\n");
4362 if (!tp_features.uwb)
4363 return 1;
4365 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4366 &uwb_tprfk_ops,
4367 RFKILL_TYPE_UWB,
4368 TPACPI_RFK_UWB_SW_NAME,
4369 false);
4370 return res;
4373 static struct ibm_struct uwb_driver_data = {
4374 .name = "uwb",
4375 .exit = uwb_exit,
4376 .flags.experimental = 1,
4379 /*************************************************************************
4380 * Video subdriver
4383 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4385 enum video_access_mode {
4386 TPACPI_VIDEO_NONE = 0,
4387 TPACPI_VIDEO_570, /* 570 */
4388 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4389 TPACPI_VIDEO_NEW, /* all others */
4392 enum { /* video status flags, based on VIDEO_570 */
4393 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4394 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4395 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4398 enum { /* TPACPI_VIDEO_570 constants */
4399 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4400 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4401 * video_status_flags */
4402 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4403 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4406 static enum video_access_mode video_supported;
4407 static int video_orig_autosw;
4409 static int video_autosw_get(void);
4410 static int video_autosw_set(int enable);
4412 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4414 static int __init video_init(struct ibm_init_struct *iibm)
4416 int ivga;
4418 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4420 TPACPI_ACPIHANDLE_INIT(vid);
4421 if (tpacpi_is_ibm())
4422 TPACPI_ACPIHANDLE_INIT(vid2);
4424 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4425 /* G41, assume IVGA doesn't change */
4426 vid_handle = vid2_handle;
4428 if (!vid_handle)
4429 /* video switching not supported on R30, R31 */
4430 video_supported = TPACPI_VIDEO_NONE;
4431 else if (tpacpi_is_ibm() &&
4432 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4433 /* 570 */
4434 video_supported = TPACPI_VIDEO_570;
4435 else if (tpacpi_is_ibm() &&
4436 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4437 /* 600e/x, 770e, 770x */
4438 video_supported = TPACPI_VIDEO_770;
4439 else
4440 /* all others */
4441 video_supported = TPACPI_VIDEO_NEW;
4443 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4444 str_supported(video_supported != TPACPI_VIDEO_NONE),
4445 video_supported);
4447 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4450 static void video_exit(void)
4452 dbg_printk(TPACPI_DBG_EXIT,
4453 "restoring original video autoswitch mode\n");
4454 if (video_autosw_set(video_orig_autosw))
4455 printk(TPACPI_ERR "error while trying to restore original "
4456 "video autoswitch mode\n");
4459 static int video_outputsw_get(void)
4461 int status = 0;
4462 int i;
4464 switch (video_supported) {
4465 case TPACPI_VIDEO_570:
4466 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4467 TP_ACPI_VIDEO_570_PHSCMD))
4468 return -EIO;
4469 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4470 break;
4471 case TPACPI_VIDEO_770:
4472 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4473 return -EIO;
4474 if (i)
4475 status |= TP_ACPI_VIDEO_S_LCD;
4476 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4477 return -EIO;
4478 if (i)
4479 status |= TP_ACPI_VIDEO_S_CRT;
4480 break;
4481 case TPACPI_VIDEO_NEW:
4482 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4483 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4484 return -EIO;
4485 if (i)
4486 status |= TP_ACPI_VIDEO_S_CRT;
4488 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4489 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4490 return -EIO;
4491 if (i)
4492 status |= TP_ACPI_VIDEO_S_LCD;
4493 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4494 return -EIO;
4495 if (i)
4496 status |= TP_ACPI_VIDEO_S_DVI;
4497 break;
4498 default:
4499 return -ENOSYS;
4502 return status;
4505 static int video_outputsw_set(int status)
4507 int autosw;
4508 int res = 0;
4510 switch (video_supported) {
4511 case TPACPI_VIDEO_570:
4512 res = acpi_evalf(NULL, NULL,
4513 "\\_SB.PHS2", "vdd",
4514 TP_ACPI_VIDEO_570_PHS2CMD,
4515 status | TP_ACPI_VIDEO_570_PHS2SET);
4516 break;
4517 case TPACPI_VIDEO_770:
4518 autosw = video_autosw_get();
4519 if (autosw < 0)
4520 return autosw;
4522 res = video_autosw_set(1);
4523 if (res)
4524 return res;
4525 res = acpi_evalf(vid_handle, NULL,
4526 "ASWT", "vdd", status * 0x100, 0);
4527 if (!autosw && video_autosw_set(autosw)) {
4528 printk(TPACPI_ERR
4529 "video auto-switch left enabled due to error\n");
4530 return -EIO;
4532 break;
4533 case TPACPI_VIDEO_NEW:
4534 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4535 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4536 break;
4537 default:
4538 return -ENOSYS;
4541 return (res)? 0 : -EIO;
4544 static int video_autosw_get(void)
4546 int autosw = 0;
4548 switch (video_supported) {
4549 case TPACPI_VIDEO_570:
4550 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4551 return -EIO;
4552 break;
4553 case TPACPI_VIDEO_770:
4554 case TPACPI_VIDEO_NEW:
4555 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4556 return -EIO;
4557 break;
4558 default:
4559 return -ENOSYS;
4562 return autosw & 1;
4565 static int video_autosw_set(int enable)
4567 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4568 return -EIO;
4569 return 0;
4572 static int video_outputsw_cycle(void)
4574 int autosw = video_autosw_get();
4575 int res;
4577 if (autosw < 0)
4578 return autosw;
4580 switch (video_supported) {
4581 case TPACPI_VIDEO_570:
4582 res = video_autosw_set(1);
4583 if (res)
4584 return res;
4585 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4586 break;
4587 case TPACPI_VIDEO_770:
4588 case TPACPI_VIDEO_NEW:
4589 res = video_autosw_set(1);
4590 if (res)
4591 return res;
4592 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4593 break;
4594 default:
4595 return -ENOSYS;
4597 if (!autosw && video_autosw_set(autosw)) {
4598 printk(TPACPI_ERR
4599 "video auto-switch left enabled due to error\n");
4600 return -EIO;
4603 return (res)? 0 : -EIO;
4606 static int video_expand_toggle(void)
4608 switch (video_supported) {
4609 case TPACPI_VIDEO_570:
4610 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4611 0 : -EIO;
4612 case TPACPI_VIDEO_770:
4613 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4614 0 : -EIO;
4615 case TPACPI_VIDEO_NEW:
4616 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4617 0 : -EIO;
4618 default:
4619 return -ENOSYS;
4621 /* not reached */
4624 static int video_read(struct seq_file *m)
4626 int status, autosw;
4628 if (video_supported == TPACPI_VIDEO_NONE) {
4629 seq_printf(m, "status:\t\tnot supported\n");
4630 return 0;
4633 /* Even reads can crash X.org, so... */
4634 if (!capable(CAP_SYS_ADMIN))
4635 return -EPERM;
4637 status = video_outputsw_get();
4638 if (status < 0)
4639 return status;
4641 autosw = video_autosw_get();
4642 if (autosw < 0)
4643 return autosw;
4645 seq_printf(m, "status:\t\tsupported\n");
4646 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4647 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4648 if (video_supported == TPACPI_VIDEO_NEW)
4649 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4650 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4651 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4652 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4653 if (video_supported == TPACPI_VIDEO_NEW)
4654 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4655 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4656 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4658 return 0;
4661 static int video_write(char *buf)
4663 char *cmd;
4664 int enable, disable, status;
4665 int res;
4667 if (video_supported == TPACPI_VIDEO_NONE)
4668 return -ENODEV;
4670 /* Even reads can crash X.org, let alone writes... */
4671 if (!capable(CAP_SYS_ADMIN))
4672 return -EPERM;
4674 enable = 0;
4675 disable = 0;
4677 while ((cmd = next_cmd(&buf))) {
4678 if (strlencmp(cmd, "lcd_enable") == 0) {
4679 enable |= TP_ACPI_VIDEO_S_LCD;
4680 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4681 disable |= TP_ACPI_VIDEO_S_LCD;
4682 } else if (strlencmp(cmd, "crt_enable") == 0) {
4683 enable |= TP_ACPI_VIDEO_S_CRT;
4684 } else if (strlencmp(cmd, "crt_disable") == 0) {
4685 disable |= TP_ACPI_VIDEO_S_CRT;
4686 } else if (video_supported == TPACPI_VIDEO_NEW &&
4687 strlencmp(cmd, "dvi_enable") == 0) {
4688 enable |= TP_ACPI_VIDEO_S_DVI;
4689 } else if (video_supported == TPACPI_VIDEO_NEW &&
4690 strlencmp(cmd, "dvi_disable") == 0) {
4691 disable |= TP_ACPI_VIDEO_S_DVI;
4692 } else if (strlencmp(cmd, "auto_enable") == 0) {
4693 res = video_autosw_set(1);
4694 if (res)
4695 return res;
4696 } else if (strlencmp(cmd, "auto_disable") == 0) {
4697 res = video_autosw_set(0);
4698 if (res)
4699 return res;
4700 } else if (strlencmp(cmd, "video_switch") == 0) {
4701 res = video_outputsw_cycle();
4702 if (res)
4703 return res;
4704 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4705 res = video_expand_toggle();
4706 if (res)
4707 return res;
4708 } else
4709 return -EINVAL;
4712 if (enable || disable) {
4713 status = video_outputsw_get();
4714 if (status < 0)
4715 return status;
4716 res = video_outputsw_set((status & ~disable) | enable);
4717 if (res)
4718 return res;
4721 return 0;
4724 static struct ibm_struct video_driver_data = {
4725 .name = "video",
4726 .read = video_read,
4727 .write = video_write,
4728 .exit = video_exit,
4731 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4733 /*************************************************************************
4734 * Light (thinklight) subdriver
4737 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4738 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4740 static int light_get_status(void)
4742 int status = 0;
4744 if (tp_features.light_status) {
4745 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4746 return -EIO;
4747 return (!!status);
4750 return -ENXIO;
4753 static int light_set_status(int status)
4755 int rc;
4757 if (tp_features.light) {
4758 if (cmos_handle) {
4759 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4760 (status)?
4761 TP_CMOS_THINKLIGHT_ON :
4762 TP_CMOS_THINKLIGHT_OFF);
4763 } else {
4764 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4765 (status)? 1 : 0);
4767 return (rc)? 0 : -EIO;
4770 return -ENXIO;
4773 static void light_set_status_worker(struct work_struct *work)
4775 struct tpacpi_led_classdev *data =
4776 container_of(work, struct tpacpi_led_classdev, work);
4778 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4779 light_set_status((data->new_state != TPACPI_LED_OFF));
4782 static void light_sysfs_set(struct led_classdev *led_cdev,
4783 enum led_brightness brightness)
4785 struct tpacpi_led_classdev *data =
4786 container_of(led_cdev,
4787 struct tpacpi_led_classdev,
4788 led_classdev);
4789 data->new_state = (brightness != LED_OFF) ?
4790 TPACPI_LED_ON : TPACPI_LED_OFF;
4791 queue_work(tpacpi_wq, &data->work);
4794 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4796 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4799 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4800 .led_classdev = {
4801 .name = "tpacpi::thinklight",
4802 .brightness_set = &light_sysfs_set,
4803 .brightness_get = &light_sysfs_get,
4807 static int __init light_init(struct ibm_init_struct *iibm)
4809 int rc;
4811 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4813 if (tpacpi_is_ibm()) {
4814 TPACPI_ACPIHANDLE_INIT(ledb);
4815 TPACPI_ACPIHANDLE_INIT(lght);
4817 TPACPI_ACPIHANDLE_INIT(cmos);
4818 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4820 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4821 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4823 if (tp_features.light)
4824 /* light status not supported on
4825 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4826 tp_features.light_status =
4827 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4829 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4830 str_supported(tp_features.light),
4831 str_supported(tp_features.light_status));
4833 if (!tp_features.light)
4834 return 1;
4836 rc = led_classdev_register(&tpacpi_pdev->dev,
4837 &tpacpi_led_thinklight.led_classdev);
4839 if (rc < 0) {
4840 tp_features.light = 0;
4841 tp_features.light_status = 0;
4842 } else {
4843 rc = 0;
4846 return rc;
4849 static void light_exit(void)
4851 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4852 if (work_pending(&tpacpi_led_thinklight.work))
4853 flush_workqueue(tpacpi_wq);
4856 static int light_read(struct seq_file *m)
4858 int status;
4860 if (!tp_features.light) {
4861 seq_printf(m, "status:\t\tnot supported\n");
4862 } else if (!tp_features.light_status) {
4863 seq_printf(m, "status:\t\tunknown\n");
4864 seq_printf(m, "commands:\ton, off\n");
4865 } else {
4866 status = light_get_status();
4867 if (status < 0)
4868 return status;
4869 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4870 seq_printf(m, "commands:\ton, off\n");
4873 return 0;
4876 static int light_write(char *buf)
4878 char *cmd;
4879 int newstatus = 0;
4881 if (!tp_features.light)
4882 return -ENODEV;
4884 while ((cmd = next_cmd(&buf))) {
4885 if (strlencmp(cmd, "on") == 0) {
4886 newstatus = 1;
4887 } else if (strlencmp(cmd, "off") == 0) {
4888 newstatus = 0;
4889 } else
4890 return -EINVAL;
4893 return light_set_status(newstatus);
4896 static struct ibm_struct light_driver_data = {
4897 .name = "light",
4898 .read = light_read,
4899 .write = light_write,
4900 .exit = light_exit,
4903 /*************************************************************************
4904 * CMOS subdriver
4907 /* sysfs cmos_command -------------------------------------------------- */
4908 static ssize_t cmos_command_store(struct device *dev,
4909 struct device_attribute *attr,
4910 const char *buf, size_t count)
4912 unsigned long cmos_cmd;
4913 int res;
4915 if (parse_strtoul(buf, 21, &cmos_cmd))
4916 return -EINVAL;
4918 res = issue_thinkpad_cmos_command(cmos_cmd);
4919 return (res)? res : count;
4922 static struct device_attribute dev_attr_cmos_command =
4923 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4925 /* --------------------------------------------------------------------- */
4927 static int __init cmos_init(struct ibm_init_struct *iibm)
4929 int res;
4931 vdbg_printk(TPACPI_DBG_INIT,
4932 "initializing cmos commands subdriver\n");
4934 TPACPI_ACPIHANDLE_INIT(cmos);
4936 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4937 str_supported(cmos_handle != NULL));
4939 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4940 if (res)
4941 return res;
4943 return (cmos_handle)? 0 : 1;
4946 static void cmos_exit(void)
4948 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4951 static int cmos_read(struct seq_file *m)
4953 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4954 R30, R31, T20-22, X20-21 */
4955 if (!cmos_handle)
4956 seq_printf(m, "status:\t\tnot supported\n");
4957 else {
4958 seq_printf(m, "status:\t\tsupported\n");
4959 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4962 return 0;
4965 static int cmos_write(char *buf)
4967 char *cmd;
4968 int cmos_cmd, res;
4970 while ((cmd = next_cmd(&buf))) {
4971 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4972 cmos_cmd >= 0 && cmos_cmd <= 21) {
4973 /* cmos_cmd set */
4974 } else
4975 return -EINVAL;
4977 res = issue_thinkpad_cmos_command(cmos_cmd);
4978 if (res)
4979 return res;
4982 return 0;
4985 static struct ibm_struct cmos_driver_data = {
4986 .name = "cmos",
4987 .read = cmos_read,
4988 .write = cmos_write,
4989 .exit = cmos_exit,
4992 /*************************************************************************
4993 * LED subdriver
4996 enum led_access_mode {
4997 TPACPI_LED_NONE = 0,
4998 TPACPI_LED_570, /* 570 */
4999 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5000 TPACPI_LED_NEW, /* all others */
5003 enum { /* For TPACPI_LED_OLD */
5004 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5005 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5006 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5009 static enum led_access_mode led_supported;
5011 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
5012 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
5013 /* T20-22, X20-21 */
5014 "LED", /* all others */
5015 ); /* R30, R31 */
5017 #define TPACPI_LED_NUMLEDS 16
5018 static struct tpacpi_led_classdev *tpacpi_leds;
5019 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5020 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5021 /* there's a limit of 19 chars + NULL before 2.6.26 */
5022 "tpacpi::power",
5023 "tpacpi:orange:batt",
5024 "tpacpi:green:batt",
5025 "tpacpi::dock_active",
5026 "tpacpi::bay_active",
5027 "tpacpi::dock_batt",
5028 "tpacpi::unknown_led",
5029 "tpacpi::standby",
5030 "tpacpi::dock_status1",
5031 "tpacpi::dock_status2",
5032 "tpacpi::unknown_led2",
5033 "tpacpi::unknown_led3",
5034 "tpacpi::thinkvantage",
5036 #define TPACPI_SAFE_LEDS 0x1081U
5038 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5040 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5041 return false;
5042 #else
5043 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5044 #endif
5047 static int led_get_status(const unsigned int led)
5049 int status;
5050 enum led_status_t led_s;
5052 switch (led_supported) {
5053 case TPACPI_LED_570:
5054 if (!acpi_evalf(ec_handle,
5055 &status, "GLED", "dd", 1 << led))
5056 return -EIO;
5057 led_s = (status == 0)?
5058 TPACPI_LED_OFF :
5059 ((status == 1)?
5060 TPACPI_LED_ON :
5061 TPACPI_LED_BLINK);
5062 tpacpi_led_state_cache[led] = led_s;
5063 return led_s;
5064 default:
5065 return -ENXIO;
5068 /* not reached */
5071 static int led_set_status(const unsigned int led,
5072 const enum led_status_t ledstatus)
5074 /* off, on, blink. Index is led_status_t */
5075 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5076 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5078 int rc = 0;
5080 switch (led_supported) {
5081 case TPACPI_LED_570:
5082 /* 570 */
5083 if (unlikely(led > 7))
5084 return -EINVAL;
5085 if (unlikely(tpacpi_is_led_restricted(led)))
5086 return -EPERM;
5087 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5088 (1 << led), led_sled_arg1[ledstatus]))
5089 rc = -EIO;
5090 break;
5091 case TPACPI_LED_OLD:
5092 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5093 if (unlikely(led > 7))
5094 return -EINVAL;
5095 if (unlikely(tpacpi_is_led_restricted(led)))
5096 return -EPERM;
5097 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5098 if (rc >= 0)
5099 rc = ec_write(TPACPI_LED_EC_HLBL,
5100 (ledstatus == TPACPI_LED_BLINK) << led);
5101 if (rc >= 0)
5102 rc = ec_write(TPACPI_LED_EC_HLCL,
5103 (ledstatus != TPACPI_LED_OFF) << led);
5104 break;
5105 case TPACPI_LED_NEW:
5106 /* all others */
5107 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5108 return -EINVAL;
5109 if (unlikely(tpacpi_is_led_restricted(led)))
5110 return -EPERM;
5111 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5112 led, led_led_arg1[ledstatus]))
5113 rc = -EIO;
5114 break;
5115 default:
5116 rc = -ENXIO;
5119 if (!rc)
5120 tpacpi_led_state_cache[led] = ledstatus;
5122 return rc;
5125 static void led_set_status_worker(struct work_struct *work)
5127 struct tpacpi_led_classdev *data =
5128 container_of(work, struct tpacpi_led_classdev, work);
5130 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5131 led_set_status(data->led, data->new_state);
5134 static void led_sysfs_set(struct led_classdev *led_cdev,
5135 enum led_brightness brightness)
5137 struct tpacpi_led_classdev *data = container_of(led_cdev,
5138 struct tpacpi_led_classdev, led_classdev);
5140 if (brightness == LED_OFF)
5141 data->new_state = TPACPI_LED_OFF;
5142 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5143 data->new_state = TPACPI_LED_ON;
5144 else
5145 data->new_state = TPACPI_LED_BLINK;
5147 queue_work(tpacpi_wq, &data->work);
5150 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5151 unsigned long *delay_on, unsigned long *delay_off)
5153 struct tpacpi_led_classdev *data = container_of(led_cdev,
5154 struct tpacpi_led_classdev, led_classdev);
5156 /* Can we choose the flash rate? */
5157 if (*delay_on == 0 && *delay_off == 0) {
5158 /* yes. set them to the hardware blink rate (1 Hz) */
5159 *delay_on = 500; /* ms */
5160 *delay_off = 500; /* ms */
5161 } else if ((*delay_on != 500) || (*delay_off != 500))
5162 return -EINVAL;
5164 data->new_state = TPACPI_LED_BLINK;
5165 queue_work(tpacpi_wq, &data->work);
5167 return 0;
5170 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5172 int rc;
5174 struct tpacpi_led_classdev *data = container_of(led_cdev,
5175 struct tpacpi_led_classdev, led_classdev);
5177 rc = led_get_status(data->led);
5179 if (rc == TPACPI_LED_OFF || rc < 0)
5180 rc = LED_OFF; /* no error handling in led class :( */
5181 else
5182 rc = LED_FULL;
5184 return rc;
5187 static void led_exit(void)
5189 unsigned int i;
5191 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5192 if (tpacpi_leds[i].led_classdev.name)
5193 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5196 kfree(tpacpi_leds);
5199 static int __init tpacpi_init_led(unsigned int led)
5201 int rc;
5203 tpacpi_leds[led].led = led;
5205 /* LEDs with no name don't get registered */
5206 if (!tpacpi_led_names[led])
5207 return 0;
5209 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5210 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5211 if (led_supported == TPACPI_LED_570)
5212 tpacpi_leds[led].led_classdev.brightness_get =
5213 &led_sysfs_get;
5215 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5217 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5219 rc = led_classdev_register(&tpacpi_pdev->dev,
5220 &tpacpi_leds[led].led_classdev);
5221 if (rc < 0)
5222 tpacpi_leds[led].led_classdev.name = NULL;
5224 return rc;
5227 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5228 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5229 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5230 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5232 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5233 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5234 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5235 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5236 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5237 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5238 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5239 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5241 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5242 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5243 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5244 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5245 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5247 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5248 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5249 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5250 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5252 /* (1) - may have excess leds enabled on MSB */
5254 /* Defaults (order matters, keep last, don't reorder!) */
5255 { /* Lenovo */
5256 .vendor = PCI_VENDOR_ID_LENOVO,
5257 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5258 .quirks = 0x1fffU,
5260 { /* IBM ThinkPads with no EC version string */
5261 .vendor = PCI_VENDOR_ID_IBM,
5262 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5263 .quirks = 0x00ffU,
5265 { /* IBM ThinkPads with EC version string */
5266 .vendor = PCI_VENDOR_ID_IBM,
5267 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5268 .quirks = 0x00bfU,
5272 #undef TPACPI_LEDQ_IBM
5273 #undef TPACPI_LEDQ_LNV
5275 static int __init led_init(struct ibm_init_struct *iibm)
5277 unsigned int i;
5278 int rc;
5279 unsigned long useful_leds;
5281 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5283 TPACPI_ACPIHANDLE_INIT(led);
5285 if (!led_handle)
5286 /* led not supported on R30, R31 */
5287 led_supported = TPACPI_LED_NONE;
5288 else if (tpacpi_is_ibm() && strlencmp(led_path, "SLED") == 0)
5289 /* 570 */
5290 led_supported = TPACPI_LED_570;
5291 else if (tpacpi_is_ibm() && strlencmp(led_path, "SYSL") == 0)
5292 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5293 led_supported = TPACPI_LED_OLD;
5294 else
5295 /* all others */
5296 led_supported = TPACPI_LED_NEW;
5298 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5299 str_supported(led_supported), led_supported);
5301 if (led_supported == TPACPI_LED_NONE)
5302 return 1;
5304 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5305 GFP_KERNEL);
5306 if (!tpacpi_leds) {
5307 printk(TPACPI_ERR "Out of memory for LED data\n");
5308 return -ENOMEM;
5311 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5312 ARRAY_SIZE(led_useful_qtable));
5314 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5315 if (!tpacpi_is_led_restricted(i) &&
5316 test_bit(i, &useful_leds)) {
5317 rc = tpacpi_init_led(i);
5318 if (rc < 0) {
5319 led_exit();
5320 return rc;
5325 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5326 printk(TPACPI_NOTICE
5327 "warning: userspace override of important "
5328 "firmware LEDs is enabled\n");
5329 #endif
5330 return 0;
5333 #define str_led_status(s) \
5334 ((s) == TPACPI_LED_OFF ? "off" : \
5335 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5337 static int led_read(struct seq_file *m)
5339 if (!led_supported) {
5340 seq_printf(m, "status:\t\tnot supported\n");
5341 return 0;
5343 seq_printf(m, "status:\t\tsupported\n");
5345 if (led_supported == TPACPI_LED_570) {
5346 /* 570 */
5347 int i, status;
5348 for (i = 0; i < 8; i++) {
5349 status = led_get_status(i);
5350 if (status < 0)
5351 return -EIO;
5352 seq_printf(m, "%d:\t\t%s\n",
5353 i, str_led_status(status));
5357 seq_printf(m, "commands:\t"
5358 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5360 return 0;
5363 static int led_write(char *buf)
5365 char *cmd;
5366 int led, rc;
5367 enum led_status_t s;
5369 if (!led_supported)
5370 return -ENODEV;
5372 while ((cmd = next_cmd(&buf))) {
5373 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5374 return -EINVAL;
5376 if (strstr(cmd, "off")) {
5377 s = TPACPI_LED_OFF;
5378 } else if (strstr(cmd, "on")) {
5379 s = TPACPI_LED_ON;
5380 } else if (strstr(cmd, "blink")) {
5381 s = TPACPI_LED_BLINK;
5382 } else {
5383 return -EINVAL;
5386 rc = led_set_status(led, s);
5387 if (rc < 0)
5388 return rc;
5391 return 0;
5394 static struct ibm_struct led_driver_data = {
5395 .name = "led",
5396 .read = led_read,
5397 .write = led_write,
5398 .exit = led_exit,
5401 /*************************************************************************
5402 * Beep subdriver
5405 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5407 #define TPACPI_BEEP_Q1 0x0001
5409 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5410 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5411 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5414 static int __init beep_init(struct ibm_init_struct *iibm)
5416 unsigned long quirks;
5418 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5420 TPACPI_ACPIHANDLE_INIT(beep);
5422 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5423 str_supported(beep_handle != NULL));
5425 quirks = tpacpi_check_quirks(beep_quirk_table,
5426 ARRAY_SIZE(beep_quirk_table));
5428 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5430 return (beep_handle)? 0 : 1;
5433 static int beep_read(struct seq_file *m)
5435 if (!beep_handle)
5436 seq_printf(m, "status:\t\tnot supported\n");
5437 else {
5438 seq_printf(m, "status:\t\tsupported\n");
5439 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5442 return 0;
5445 static int beep_write(char *buf)
5447 char *cmd;
5448 int beep_cmd;
5450 if (!beep_handle)
5451 return -ENODEV;
5453 while ((cmd = next_cmd(&buf))) {
5454 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5455 beep_cmd >= 0 && beep_cmd <= 17) {
5456 /* beep_cmd set */
5457 } else
5458 return -EINVAL;
5459 if (tp_features.beep_needs_two_args) {
5460 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5461 beep_cmd, 0))
5462 return -EIO;
5463 } else {
5464 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5465 beep_cmd))
5466 return -EIO;
5470 return 0;
5473 static struct ibm_struct beep_driver_data = {
5474 .name = "beep",
5475 .read = beep_read,
5476 .write = beep_write,
5479 /*************************************************************************
5480 * Thermal subdriver
5483 enum thermal_access_mode {
5484 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5485 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5486 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5487 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5488 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5491 enum { /* TPACPI_THERMAL_TPEC_* */
5492 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5493 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5494 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5496 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5500 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5501 struct ibm_thermal_sensors_struct {
5502 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5505 static enum thermal_access_mode thermal_read_mode;
5507 /* idx is zero-based */
5508 static int thermal_get_sensor(int idx, s32 *value)
5510 int t;
5511 s8 tmp;
5512 char tmpi[5];
5514 t = TP_EC_THERMAL_TMP0;
5516 switch (thermal_read_mode) {
5517 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5518 case TPACPI_THERMAL_TPEC_16:
5519 if (idx >= 8 && idx <= 15) {
5520 t = TP_EC_THERMAL_TMP8;
5521 idx -= 8;
5523 /* fallthrough */
5524 #endif
5525 case TPACPI_THERMAL_TPEC_8:
5526 if (idx <= 7) {
5527 if (!acpi_ec_read(t + idx, &tmp))
5528 return -EIO;
5529 *value = tmp * 1000;
5530 return 0;
5532 break;
5534 case TPACPI_THERMAL_ACPI_UPDT:
5535 if (idx <= 7) {
5536 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5537 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5538 return -EIO;
5539 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5540 return -EIO;
5541 *value = (t - 2732) * 100;
5542 return 0;
5544 break;
5546 case TPACPI_THERMAL_ACPI_TMP07:
5547 if (idx <= 7) {
5548 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5549 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5550 return -EIO;
5551 if (t > 127 || t < -127)
5552 t = TP_EC_THERMAL_TMP_NA;
5553 *value = t * 1000;
5554 return 0;
5556 break;
5558 case TPACPI_THERMAL_NONE:
5559 default:
5560 return -ENOSYS;
5563 return -EINVAL;
5566 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5568 int res, i;
5569 int n;
5571 n = 8;
5572 i = 0;
5574 if (!s)
5575 return -EINVAL;
5577 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5578 n = 16;
5580 for (i = 0 ; i < n; i++) {
5581 res = thermal_get_sensor(i, &s->temp[i]);
5582 if (res)
5583 return res;
5586 return n;
5589 static void thermal_dump_all_sensors(void)
5591 int n, i;
5592 struct ibm_thermal_sensors_struct t;
5594 n = thermal_get_sensors(&t);
5595 if (n <= 0)
5596 return;
5598 printk(TPACPI_NOTICE
5599 "temperatures (Celsius):");
5601 for (i = 0; i < n; i++) {
5602 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5603 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5604 else
5605 printk(KERN_CONT " N/A");
5608 printk(KERN_CONT "\n");
5611 /* sysfs temp##_input -------------------------------------------------- */
5613 static ssize_t thermal_temp_input_show(struct device *dev,
5614 struct device_attribute *attr,
5615 char *buf)
5617 struct sensor_device_attribute *sensor_attr =
5618 to_sensor_dev_attr(attr);
5619 int idx = sensor_attr->index;
5620 s32 value;
5621 int res;
5623 res = thermal_get_sensor(idx, &value);
5624 if (res)
5625 return res;
5626 if (value == TPACPI_THERMAL_SENSOR_NA)
5627 return -ENXIO;
5629 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5632 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5633 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5634 thermal_temp_input_show, NULL, _idxB)
5636 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5637 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5638 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5639 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5640 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5641 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5642 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5643 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5644 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5645 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5646 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5647 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5648 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5649 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5650 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5651 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5652 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5655 #define THERMAL_ATTRS(X) \
5656 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5658 static struct attribute *thermal_temp_input_attr[] = {
5659 THERMAL_ATTRS(8),
5660 THERMAL_ATTRS(9),
5661 THERMAL_ATTRS(10),
5662 THERMAL_ATTRS(11),
5663 THERMAL_ATTRS(12),
5664 THERMAL_ATTRS(13),
5665 THERMAL_ATTRS(14),
5666 THERMAL_ATTRS(15),
5667 THERMAL_ATTRS(0),
5668 THERMAL_ATTRS(1),
5669 THERMAL_ATTRS(2),
5670 THERMAL_ATTRS(3),
5671 THERMAL_ATTRS(4),
5672 THERMAL_ATTRS(5),
5673 THERMAL_ATTRS(6),
5674 THERMAL_ATTRS(7),
5675 NULL
5678 static const struct attribute_group thermal_temp_input16_group = {
5679 .attrs = thermal_temp_input_attr
5682 static const struct attribute_group thermal_temp_input8_group = {
5683 .attrs = &thermal_temp_input_attr[8]
5686 #undef THERMAL_SENSOR_ATTR_TEMP
5687 #undef THERMAL_ATTRS
5689 /* --------------------------------------------------------------------- */
5691 static int __init thermal_init(struct ibm_init_struct *iibm)
5693 u8 t, ta1, ta2;
5694 int i;
5695 int acpi_tmp7;
5696 int res;
5698 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5700 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5702 if (thinkpad_id.ec_model) {
5704 * Direct EC access mode: sensors at registers
5705 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5706 * non-implemented, thermal sensors return 0x80 when
5707 * not available
5710 ta1 = ta2 = 0;
5711 for (i = 0; i < 8; i++) {
5712 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5713 ta1 |= t;
5714 } else {
5715 ta1 = 0;
5716 break;
5718 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5719 ta2 |= t;
5720 } else {
5721 ta1 = 0;
5722 break;
5725 if (ta1 == 0) {
5726 /* This is sheer paranoia, but we handle it anyway */
5727 if (acpi_tmp7) {
5728 printk(TPACPI_ERR
5729 "ThinkPad ACPI EC access misbehaving, "
5730 "falling back to ACPI TMPx access "
5731 "mode\n");
5732 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5733 } else {
5734 printk(TPACPI_ERR
5735 "ThinkPad ACPI EC access misbehaving, "
5736 "disabling thermal sensors access\n");
5737 thermal_read_mode = TPACPI_THERMAL_NONE;
5739 } else {
5740 thermal_read_mode =
5741 (ta2 != 0) ?
5742 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5744 } else if (acpi_tmp7) {
5745 if (tpacpi_is_ibm() &&
5746 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5747 /* 600e/x, 770e, 770x */
5748 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5749 } else {
5750 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
5751 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5753 } else {
5754 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5755 thermal_read_mode = TPACPI_THERMAL_NONE;
5758 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5759 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5760 thermal_read_mode);
5762 switch (thermal_read_mode) {
5763 case TPACPI_THERMAL_TPEC_16:
5764 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5765 &thermal_temp_input16_group);
5766 if (res)
5767 return res;
5768 break;
5769 case TPACPI_THERMAL_TPEC_8:
5770 case TPACPI_THERMAL_ACPI_TMP07:
5771 case TPACPI_THERMAL_ACPI_UPDT:
5772 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5773 &thermal_temp_input8_group);
5774 if (res)
5775 return res;
5776 break;
5777 case TPACPI_THERMAL_NONE:
5778 default:
5779 return 1;
5782 return 0;
5785 static void thermal_exit(void)
5787 switch (thermal_read_mode) {
5788 case TPACPI_THERMAL_TPEC_16:
5789 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5790 &thermal_temp_input16_group);
5791 break;
5792 case TPACPI_THERMAL_TPEC_8:
5793 case TPACPI_THERMAL_ACPI_TMP07:
5794 case TPACPI_THERMAL_ACPI_UPDT:
5795 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5796 &thermal_temp_input8_group);
5797 break;
5798 case TPACPI_THERMAL_NONE:
5799 default:
5800 break;
5804 static int thermal_read(struct seq_file *m)
5806 int n, i;
5807 struct ibm_thermal_sensors_struct t;
5809 n = thermal_get_sensors(&t);
5810 if (unlikely(n < 0))
5811 return n;
5813 seq_printf(m, "temperatures:\t");
5815 if (n > 0) {
5816 for (i = 0; i < (n - 1); i++)
5817 seq_printf(m, "%d ", t.temp[i] / 1000);
5818 seq_printf(m, "%d\n", t.temp[i] / 1000);
5819 } else
5820 seq_printf(m, "not supported\n");
5822 return 0;
5825 static struct ibm_struct thermal_driver_data = {
5826 .name = "thermal",
5827 .read = thermal_read,
5828 .exit = thermal_exit,
5831 /*************************************************************************
5832 * EC Dump subdriver
5835 static u8 ecdump_regs[256];
5837 static int ecdump_read(struct seq_file *m)
5839 int i, j;
5840 u8 v;
5842 seq_printf(m, "EC "
5843 " +00 +01 +02 +03 +04 +05 +06 +07"
5844 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5845 for (i = 0; i < 256; i += 16) {
5846 seq_printf(m, "EC 0x%02x:", i);
5847 for (j = 0; j < 16; j++) {
5848 if (!acpi_ec_read(i + j, &v))
5849 break;
5850 if (v != ecdump_regs[i + j])
5851 seq_printf(m, " *%02x", v);
5852 else
5853 seq_printf(m, " %02x", v);
5854 ecdump_regs[i + j] = v;
5856 seq_putc(m, '\n');
5857 if (j != 16)
5858 break;
5861 /* These are way too dangerous to advertise openly... */
5862 #if 0
5863 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5864 " (<offset> is 00-ff, <value> is 00-ff)\n");
5865 seq_printf(m, "commands:\t0x<offset> <value> "
5866 " (<offset> is 00-ff, <value> is 0-255)\n");
5867 #endif
5868 return 0;
5871 static int ecdump_write(char *buf)
5873 char *cmd;
5874 int i, v;
5876 while ((cmd = next_cmd(&buf))) {
5877 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5878 /* i and v set */
5879 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5880 /* i and v set */
5881 } else
5882 return -EINVAL;
5883 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5884 if (!acpi_ec_write(i, v))
5885 return -EIO;
5886 } else
5887 return -EINVAL;
5890 return 0;
5893 static struct ibm_struct ecdump_driver_data = {
5894 .name = "ecdump",
5895 .read = ecdump_read,
5896 .write = ecdump_write,
5897 .flags.experimental = 1,
5900 /*************************************************************************
5901 * Backlight/brightness subdriver
5904 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5907 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5908 * CMOS NVRAM byte 0x5E, bits 0-3.
5910 * EC HBRV (0x31) has the following layout
5911 * Bit 7: unknown function
5912 * Bit 6: unknown function
5913 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5914 * Bit 4: must be set to zero to avoid problems
5915 * Bit 3-0: backlight brightness level
5917 * brightness_get_raw returns status data in the HBRV layout
5919 * WARNING: The X61 has been verified to use HBRV for something else, so
5920 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5921 * testing on the very early *60 Lenovo models...
5924 enum {
5925 TP_EC_BACKLIGHT = 0x31,
5927 /* TP_EC_BACKLIGHT bitmasks */
5928 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5929 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5930 TP_EC_BACKLIGHT_MAPSW = 0x20,
5933 enum tpacpi_brightness_access_mode {
5934 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5935 TPACPI_BRGHT_MODE_EC, /* EC control */
5936 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5937 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5938 TPACPI_BRGHT_MODE_MAX
5941 static struct backlight_device *ibm_backlight_device;
5943 static enum tpacpi_brightness_access_mode brightness_mode =
5944 TPACPI_BRGHT_MODE_MAX;
5946 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5948 static struct mutex brightness_mutex;
5950 /* NVRAM brightness access,
5951 * call with brightness_mutex held! */
5952 static unsigned int tpacpi_brightness_nvram_get(void)
5954 u8 lnvram;
5956 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5957 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5958 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5959 lnvram &= bright_maxlvl;
5961 return lnvram;
5964 static void tpacpi_brightness_checkpoint_nvram(void)
5966 u8 lec = 0;
5967 u8 b_nvram;
5969 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5970 return;
5972 vdbg_printk(TPACPI_DBG_BRGHT,
5973 "trying to checkpoint backlight level to NVRAM...\n");
5975 if (mutex_lock_killable(&brightness_mutex) < 0)
5976 return;
5978 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5979 goto unlock;
5980 lec &= TP_EC_BACKLIGHT_LVLMSK;
5981 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5983 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5984 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5985 /* NVRAM needs update */
5986 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5987 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5988 b_nvram |= lec;
5989 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5990 dbg_printk(TPACPI_DBG_BRGHT,
5991 "updated NVRAM backlight level to %u (0x%02x)\n",
5992 (unsigned int) lec, (unsigned int) b_nvram);
5993 } else
5994 vdbg_printk(TPACPI_DBG_BRGHT,
5995 "NVRAM backlight level already is %u (0x%02x)\n",
5996 (unsigned int) lec, (unsigned int) b_nvram);
5998 unlock:
5999 mutex_unlock(&brightness_mutex);
6003 /* call with brightness_mutex held! */
6004 static int tpacpi_brightness_get_raw(int *status)
6006 u8 lec = 0;
6008 switch (brightness_mode) {
6009 case TPACPI_BRGHT_MODE_UCMS_STEP:
6010 *status = tpacpi_brightness_nvram_get();
6011 return 0;
6012 case TPACPI_BRGHT_MODE_EC:
6013 case TPACPI_BRGHT_MODE_ECNVRAM:
6014 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6015 return -EIO;
6016 *status = lec;
6017 return 0;
6018 default:
6019 return -ENXIO;
6023 /* call with brightness_mutex held! */
6024 /* do NOT call with illegal backlight level value */
6025 static int tpacpi_brightness_set_ec(unsigned int value)
6027 u8 lec = 0;
6029 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6030 return -EIO;
6032 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6033 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6034 (value & TP_EC_BACKLIGHT_LVLMSK))))
6035 return -EIO;
6037 return 0;
6040 /* call with brightness_mutex held! */
6041 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6043 int cmos_cmd, inc;
6044 unsigned int current_value, i;
6046 current_value = tpacpi_brightness_nvram_get();
6048 if (value == current_value)
6049 return 0;
6051 cmos_cmd = (value > current_value) ?
6052 TP_CMOS_BRIGHTNESS_UP :
6053 TP_CMOS_BRIGHTNESS_DOWN;
6054 inc = (value > current_value) ? 1 : -1;
6056 for (i = current_value; i != value; i += inc)
6057 if (issue_thinkpad_cmos_command(cmos_cmd))
6058 return -EIO;
6060 return 0;
6063 /* May return EINTR which can always be mapped to ERESTARTSYS */
6064 static int brightness_set(unsigned int value)
6066 int res;
6068 if (value > bright_maxlvl || value < 0)
6069 return -EINVAL;
6071 vdbg_printk(TPACPI_DBG_BRGHT,
6072 "set backlight level to %d\n", value);
6074 res = mutex_lock_killable(&brightness_mutex);
6075 if (res < 0)
6076 return res;
6078 switch (brightness_mode) {
6079 case TPACPI_BRGHT_MODE_EC:
6080 case TPACPI_BRGHT_MODE_ECNVRAM:
6081 res = tpacpi_brightness_set_ec(value);
6082 break;
6083 case TPACPI_BRGHT_MODE_UCMS_STEP:
6084 res = tpacpi_brightness_set_ucmsstep(value);
6085 break;
6086 default:
6087 res = -ENXIO;
6090 mutex_unlock(&brightness_mutex);
6091 return res;
6094 /* sysfs backlight class ----------------------------------------------- */
6096 static int brightness_update_status(struct backlight_device *bd)
6098 unsigned int level =
6099 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6100 bd->props.power == FB_BLANK_UNBLANK) ?
6101 bd->props.brightness : 0;
6103 dbg_printk(TPACPI_DBG_BRGHT,
6104 "backlight: attempt to set level to %d\n",
6105 level);
6107 /* it is the backlight class's job (caller) to handle
6108 * EINTR and other errors properly */
6109 return brightness_set(level);
6112 static int brightness_get(struct backlight_device *bd)
6114 int status, res;
6116 res = mutex_lock_killable(&brightness_mutex);
6117 if (res < 0)
6118 return 0;
6120 res = tpacpi_brightness_get_raw(&status);
6122 mutex_unlock(&brightness_mutex);
6124 if (res < 0)
6125 return 0;
6127 return status & TP_EC_BACKLIGHT_LVLMSK;
6130 static void tpacpi_brightness_notify_change(void)
6132 backlight_force_update(ibm_backlight_device,
6133 BACKLIGHT_UPDATE_HOTKEY);
6136 static struct backlight_ops ibm_backlight_data = {
6137 .get_brightness = brightness_get,
6138 .update_status = brightness_update_status,
6141 /* --------------------------------------------------------------------- */
6143 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6145 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6146 union acpi_object *obj;
6147 int rc;
6149 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
6150 obj = (union acpi_object *)buffer.pointer;
6151 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6152 printk(TPACPI_ERR "Unknown _BCL data, "
6153 "please report this to %s\n", TPACPI_MAIL);
6154 rc = 0;
6155 } else {
6156 rc = obj->package.count;
6158 } else {
6159 return 0;
6162 kfree(buffer.pointer);
6163 return rc;
6166 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
6167 u32 lvl, void *context, void **rv)
6169 char name[ACPI_PATH_SEGMENT_LENGTH];
6170 struct acpi_buffer buffer = { sizeof(name), &name };
6172 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
6173 !strncmp("_BCL", name, sizeof(name) - 1)) {
6174 BUG_ON(!rv || !*rv);
6175 **(int **)rv = tpacpi_query_bcl_levels(handle);
6176 return AE_CTRL_TERMINATE;
6177 } else {
6178 return AE_OK;
6183 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6185 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6187 int status;
6188 int bcl_levels = 0;
6189 void *bcl_ptr = &bcl_levels;
6191 if (!vid_handle)
6192 TPACPI_ACPIHANDLE_INIT(vid);
6194 if (!vid_handle)
6195 return 0;
6198 * Search for a _BCL method, and execute it. This is safe on all
6199 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
6200 * BIOS in ACPI backlight control mode. We do NOT have to care
6201 * about calling the _BCL method in an enabled video device, any
6202 * will do for our purposes.
6205 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
6206 tpacpi_acpi_walk_find_bcl, NULL, NULL,
6207 &bcl_ptr);
6209 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
6210 tp_features.bright_acpimode = 1;
6211 return bcl_levels - 2;
6214 return 0;
6218 * These are only useful for models that have only one possibility
6219 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6220 * these quirks.
6222 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6223 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6224 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6226 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6227 /* Models with ATI GPUs known to require ECNVRAM mode */
6228 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6230 /* Models with ATI GPUs that can use ECNVRAM */
6231 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6232 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6233 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6234 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6236 /* Models with Intel Extreme Graphics 2 */
6237 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6238 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6239 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6241 /* Models with Intel GMA900 */
6242 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6243 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6244 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6248 * Returns < 0 for error, otherwise sets tp_features.bright_*
6249 * and bright_maxlvl.
6251 static void __init tpacpi_detect_brightness_capabilities(void)
6253 unsigned int b;
6255 vdbg_printk(TPACPI_DBG_INIT,
6256 "detecting firmware brightness interface capabilities\n");
6258 /* we could run a quirks check here (same table used by
6259 * brightness_init) if needed */
6262 * We always attempt to detect acpi support, so as to switch
6263 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6264 * going to publish a backlight interface
6266 b = tpacpi_check_std_acpi_brightness_support();
6267 switch (b) {
6268 case 16:
6269 bright_maxlvl = 15;
6270 printk(TPACPI_INFO
6271 "detected a 16-level brightness capable ThinkPad\n");
6272 break;
6273 case 8:
6274 case 0:
6275 bright_maxlvl = 7;
6276 printk(TPACPI_INFO
6277 "detected a 8-level brightness capable ThinkPad\n");
6278 break;
6279 default:
6280 printk(TPACPI_ERR
6281 "Unsupported brightness interface, "
6282 "please contact %s\n", TPACPI_MAIL);
6283 tp_features.bright_unkfw = 1;
6284 bright_maxlvl = b - 1;
6288 static int __init brightness_init(struct ibm_init_struct *iibm)
6290 int b;
6291 unsigned long quirks;
6293 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6295 mutex_init(&brightness_mutex);
6297 quirks = tpacpi_check_quirks(brightness_quirk_table,
6298 ARRAY_SIZE(brightness_quirk_table));
6300 /* tpacpi_detect_brightness_capabilities() must have run already */
6302 /* if it is unknown, we don't handle it: it wouldn't be safe */
6303 if (tp_features.bright_unkfw)
6304 return 1;
6306 if (tp_features.bright_acpimode) {
6307 if (acpi_video_backlight_support()) {
6308 if (brightness_enable > 1) {
6309 printk(TPACPI_NOTICE
6310 "Standard ACPI backlight interface "
6311 "available, not loading native one.\n");
6312 return 1;
6313 } else if (brightness_enable == 1) {
6314 printk(TPACPI_NOTICE
6315 "Backlight control force enabled, even if standard "
6316 "ACPI backlight interface is available\n");
6318 } else {
6319 if (brightness_enable > 1) {
6320 printk(TPACPI_NOTICE
6321 "Standard ACPI backlight interface not "
6322 "available, thinkpad_acpi native "
6323 "brightness control enabled\n");
6328 if (!brightness_enable) {
6329 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6330 "brightness support disabled by "
6331 "module parameter\n");
6332 return 1;
6336 * Check for module parameter bogosity, note that we
6337 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6338 * able to detect "unspecified"
6340 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6341 return -EINVAL;
6343 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6344 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6345 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6346 if (quirks & TPACPI_BRGHT_Q_EC)
6347 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6348 else
6349 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6351 dbg_printk(TPACPI_DBG_BRGHT,
6352 "driver auto-selected brightness_mode=%d\n",
6353 brightness_mode);
6356 /* Safety */
6357 if (!tpacpi_is_ibm() &&
6358 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6359 brightness_mode == TPACPI_BRGHT_MODE_EC))
6360 return -EINVAL;
6362 if (tpacpi_brightness_get_raw(&b) < 0)
6363 return 1;
6365 ibm_backlight_device = backlight_device_register(
6366 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6367 &ibm_backlight_data);
6368 if (IS_ERR(ibm_backlight_device)) {
6369 int rc = PTR_ERR(ibm_backlight_device);
6370 ibm_backlight_device = NULL;
6371 printk(TPACPI_ERR "Could not register backlight device\n");
6372 return rc;
6374 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6375 "brightness is supported\n");
6377 if (quirks & TPACPI_BRGHT_Q_ASK) {
6378 printk(TPACPI_NOTICE
6379 "brightness: will use unverified default: "
6380 "brightness_mode=%d\n", brightness_mode);
6381 printk(TPACPI_NOTICE
6382 "brightness: please report to %s whether it works well "
6383 "or not on your ThinkPad\n", TPACPI_MAIL);
6386 ibm_backlight_device->props.max_brightness = bright_maxlvl;
6387 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6388 backlight_update_status(ibm_backlight_device);
6390 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6391 "brightness: registering brightness hotkeys "
6392 "as change notification\n");
6393 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6394 | TP_ACPI_HKEY_BRGHTUP_MASK
6395 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6396 return 0;
6399 static void brightness_suspend(pm_message_t state)
6401 tpacpi_brightness_checkpoint_nvram();
6404 static void brightness_shutdown(void)
6406 tpacpi_brightness_checkpoint_nvram();
6409 static void brightness_exit(void)
6411 if (ibm_backlight_device) {
6412 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6413 "calling backlight_device_unregister()\n");
6414 backlight_device_unregister(ibm_backlight_device);
6417 tpacpi_brightness_checkpoint_nvram();
6420 static int brightness_read(struct seq_file *m)
6422 int level;
6424 level = brightness_get(NULL);
6425 if (level < 0) {
6426 seq_printf(m, "level:\t\tunreadable\n");
6427 } else {
6428 seq_printf(m, "level:\t\t%d\n", level);
6429 seq_printf(m, "commands:\tup, down\n");
6430 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6431 bright_maxlvl);
6434 return 0;
6437 static int brightness_write(char *buf)
6439 int level;
6440 int rc;
6441 char *cmd;
6443 level = brightness_get(NULL);
6444 if (level < 0)
6445 return level;
6447 while ((cmd = next_cmd(&buf))) {
6448 if (strlencmp(cmd, "up") == 0) {
6449 if (level < bright_maxlvl)
6450 level++;
6451 } else if (strlencmp(cmd, "down") == 0) {
6452 if (level > 0)
6453 level--;
6454 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6455 level >= 0 && level <= bright_maxlvl) {
6456 /* new level set */
6457 } else
6458 return -EINVAL;
6461 tpacpi_disclose_usertask("procfs brightness",
6462 "set level to %d\n", level);
6465 * Now we know what the final level should be, so we try to set it.
6466 * Doing it this way makes the syscall restartable in case of EINTR
6468 rc = brightness_set(level);
6469 if (!rc && ibm_backlight_device)
6470 backlight_force_update(ibm_backlight_device,
6471 BACKLIGHT_UPDATE_SYSFS);
6472 return (rc == -EINTR)? -ERESTARTSYS : rc;
6475 static struct ibm_struct brightness_driver_data = {
6476 .name = "brightness",
6477 .read = brightness_read,
6478 .write = brightness_write,
6479 .exit = brightness_exit,
6480 .suspend = brightness_suspend,
6481 .shutdown = brightness_shutdown,
6484 /*************************************************************************
6485 * Volume subdriver
6489 * IBM ThinkPads have a simple volume controller with MUTE gating.
6490 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6492 * Since the *61 series (and probably also the later *60 series), Lenovo
6493 * ThinkPads only implement the MUTE gate.
6495 * EC register 0x30
6496 * Bit 6: MUTE (1 mutes sound)
6497 * Bit 3-0: Volume
6498 * Other bits should be zero as far as we know.
6500 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6501 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6502 * such as bit 7 which is used to detect repeated presses of MUTE,
6503 * and we leave them unchanged.
6506 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6508 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6509 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6510 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6512 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6513 static char *alsa_id = "ThinkPadEC";
6514 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6516 struct tpacpi_alsa_data {
6517 struct snd_card *card;
6518 struct snd_ctl_elem_id *ctl_mute_id;
6519 struct snd_ctl_elem_id *ctl_vol_id;
6522 static struct snd_card *alsa_card;
6524 enum {
6525 TP_EC_AUDIO = 0x30,
6527 /* TP_EC_AUDIO bits */
6528 TP_EC_AUDIO_MUTESW = 6,
6530 /* TP_EC_AUDIO bitmasks */
6531 TP_EC_AUDIO_LVL_MSK = 0x0F,
6532 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6534 /* Maximum volume */
6535 TP_EC_VOLUME_MAX = 14,
6538 enum tpacpi_volume_access_mode {
6539 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6540 TPACPI_VOL_MODE_EC, /* Pure EC control */
6541 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6542 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6543 TPACPI_VOL_MODE_MAX
6546 enum tpacpi_volume_capabilities {
6547 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6548 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6549 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6550 TPACPI_VOL_CAP_MAX
6553 static enum tpacpi_volume_access_mode volume_mode =
6554 TPACPI_VOL_MODE_MAX;
6556 static enum tpacpi_volume_capabilities volume_capabilities;
6557 static int volume_control_allowed;
6560 * Used to syncronize writers to TP_EC_AUDIO and
6561 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6563 static struct mutex volume_mutex;
6565 static void tpacpi_volume_checkpoint_nvram(void)
6567 u8 lec = 0;
6568 u8 b_nvram;
6569 u8 ec_mask;
6571 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6572 return;
6573 if (!volume_control_allowed)
6574 return;
6576 vdbg_printk(TPACPI_DBG_MIXER,
6577 "trying to checkpoint mixer state to NVRAM...\n");
6579 if (tp_features.mixer_no_level_control)
6580 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6581 else
6582 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6584 if (mutex_lock_killable(&volume_mutex) < 0)
6585 return;
6587 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6588 goto unlock;
6589 lec &= ec_mask;
6590 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6592 if (lec != (b_nvram & ec_mask)) {
6593 /* NVRAM needs update */
6594 b_nvram &= ~ec_mask;
6595 b_nvram |= lec;
6596 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6597 dbg_printk(TPACPI_DBG_MIXER,
6598 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6599 (unsigned int) lec, (unsigned int) b_nvram);
6600 } else {
6601 vdbg_printk(TPACPI_DBG_MIXER,
6602 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6603 (unsigned int) lec, (unsigned int) b_nvram);
6606 unlock:
6607 mutex_unlock(&volume_mutex);
6610 static int volume_get_status_ec(u8 *status)
6612 u8 s;
6614 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6615 return -EIO;
6617 *status = s;
6619 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6621 return 0;
6624 static int volume_get_status(u8 *status)
6626 return volume_get_status_ec(status);
6629 static int volume_set_status_ec(const u8 status)
6631 if (!acpi_ec_write(TP_EC_AUDIO, status))
6632 return -EIO;
6634 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6636 return 0;
6639 static int volume_set_status(const u8 status)
6641 return volume_set_status_ec(status);
6644 /* returns < 0 on error, 0 on no change, 1 on change */
6645 static int __volume_set_mute_ec(const bool mute)
6647 int rc;
6648 u8 s, n;
6650 if (mutex_lock_killable(&volume_mutex) < 0)
6651 return -EINTR;
6653 rc = volume_get_status_ec(&s);
6654 if (rc)
6655 goto unlock;
6657 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6658 s & ~TP_EC_AUDIO_MUTESW_MSK;
6660 if (n != s) {
6661 rc = volume_set_status_ec(n);
6662 if (!rc)
6663 rc = 1;
6666 unlock:
6667 mutex_unlock(&volume_mutex);
6668 return rc;
6671 static int volume_alsa_set_mute(const bool mute)
6673 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6674 (mute) ? "" : "un");
6675 return __volume_set_mute_ec(mute);
6678 static int volume_set_mute(const bool mute)
6680 int rc;
6682 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6683 (mute) ? "" : "un");
6685 rc = __volume_set_mute_ec(mute);
6686 return (rc < 0) ? rc : 0;
6689 /* returns < 0 on error, 0 on no change, 1 on change */
6690 static int __volume_set_volume_ec(const u8 vol)
6692 int rc;
6693 u8 s, n;
6695 if (vol > TP_EC_VOLUME_MAX)
6696 return -EINVAL;
6698 if (mutex_lock_killable(&volume_mutex) < 0)
6699 return -EINTR;
6701 rc = volume_get_status_ec(&s);
6702 if (rc)
6703 goto unlock;
6705 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6707 if (n != s) {
6708 rc = volume_set_status_ec(n);
6709 if (!rc)
6710 rc = 1;
6713 unlock:
6714 mutex_unlock(&volume_mutex);
6715 return rc;
6718 static int volume_alsa_set_volume(const u8 vol)
6720 dbg_printk(TPACPI_DBG_MIXER,
6721 "ALSA: trying to set volume level to %hu\n", vol);
6722 return __volume_set_volume_ec(vol);
6725 static void volume_alsa_notify_change(void)
6727 struct tpacpi_alsa_data *d;
6729 if (alsa_card && alsa_card->private_data) {
6730 d = alsa_card->private_data;
6731 if (d->ctl_mute_id)
6732 snd_ctl_notify(alsa_card,
6733 SNDRV_CTL_EVENT_MASK_VALUE,
6734 d->ctl_mute_id);
6735 if (d->ctl_vol_id)
6736 snd_ctl_notify(alsa_card,
6737 SNDRV_CTL_EVENT_MASK_VALUE,
6738 d->ctl_vol_id);
6742 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6743 struct snd_ctl_elem_info *uinfo)
6745 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6746 uinfo->count = 1;
6747 uinfo->value.integer.min = 0;
6748 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6749 return 0;
6752 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6753 struct snd_ctl_elem_value *ucontrol)
6755 u8 s;
6756 int rc;
6758 rc = volume_get_status(&s);
6759 if (rc < 0)
6760 return rc;
6762 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6763 return 0;
6766 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6767 struct snd_ctl_elem_value *ucontrol)
6769 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6770 ucontrol->value.integer.value[0]);
6771 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6774 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6776 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6777 struct snd_ctl_elem_value *ucontrol)
6779 u8 s;
6780 int rc;
6782 rc = volume_get_status(&s);
6783 if (rc < 0)
6784 return rc;
6786 ucontrol->value.integer.value[0] =
6787 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6788 return 0;
6791 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6792 struct snd_ctl_elem_value *ucontrol)
6794 tpacpi_disclose_usertask("ALSA", "%smute\n",
6795 ucontrol->value.integer.value[0] ?
6796 "un" : "");
6797 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6800 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6801 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6802 .name = "Console Playback Volume",
6803 .index = 0,
6804 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6805 .info = volume_alsa_vol_info,
6806 .get = volume_alsa_vol_get,
6809 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6810 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6811 .name = "Console Playback Switch",
6812 .index = 0,
6813 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6814 .info = volume_alsa_mute_info,
6815 .get = volume_alsa_mute_get,
6818 static void volume_suspend(pm_message_t state)
6820 tpacpi_volume_checkpoint_nvram();
6823 static void volume_resume(void)
6825 volume_alsa_notify_change();
6828 static void volume_shutdown(void)
6830 tpacpi_volume_checkpoint_nvram();
6833 static void volume_exit(void)
6835 if (alsa_card) {
6836 snd_card_free(alsa_card);
6837 alsa_card = NULL;
6840 tpacpi_volume_checkpoint_nvram();
6843 static int __init volume_create_alsa_mixer(void)
6845 struct snd_card *card;
6846 struct tpacpi_alsa_data *data;
6847 struct snd_kcontrol *ctl_vol;
6848 struct snd_kcontrol *ctl_mute;
6849 int rc;
6851 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6852 sizeof(struct tpacpi_alsa_data), &card);
6853 if (rc < 0 || !card) {
6854 printk(TPACPI_ERR
6855 "Failed to create ALSA card structures: %d\n", rc);
6856 return 1;
6859 BUG_ON(!card->private_data);
6860 data = card->private_data;
6861 data->card = card;
6863 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6864 sizeof(card->driver));
6865 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6866 sizeof(card->shortname));
6867 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6868 (thinkpad_id.ec_version_str) ?
6869 thinkpad_id.ec_version_str : "(unknown)");
6870 snprintf(card->longname, sizeof(card->longname),
6871 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6872 (thinkpad_id.ec_version_str) ?
6873 thinkpad_id.ec_version_str : "unknown");
6875 if (volume_control_allowed) {
6876 volume_alsa_control_vol.put = volume_alsa_vol_put;
6877 volume_alsa_control_vol.access =
6878 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6880 volume_alsa_control_mute.put = volume_alsa_mute_put;
6881 volume_alsa_control_mute.access =
6882 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6885 if (!tp_features.mixer_no_level_control) {
6886 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6887 rc = snd_ctl_add(card, ctl_vol);
6888 if (rc < 0) {
6889 printk(TPACPI_ERR
6890 "Failed to create ALSA volume control: %d\n",
6891 rc);
6892 goto err_exit;
6894 data->ctl_vol_id = &ctl_vol->id;
6897 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6898 rc = snd_ctl_add(card, ctl_mute);
6899 if (rc < 0) {
6900 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6901 rc);
6902 goto err_exit;
6904 data->ctl_mute_id = &ctl_mute->id;
6906 snd_card_set_dev(card, &tpacpi_pdev->dev);
6907 rc = snd_card_register(card);
6908 if (rc < 0) {
6909 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6910 goto err_exit;
6913 alsa_card = card;
6914 return 0;
6916 err_exit:
6917 snd_card_free(card);
6918 return 1;
6921 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6922 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6924 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6925 /* Whitelist volume level on all IBM by default */
6926 { .vendor = PCI_VENDOR_ID_IBM,
6927 .bios = TPACPI_MATCH_ANY,
6928 .ec = TPACPI_MATCH_ANY,
6929 .quirks = TPACPI_VOL_Q_LEVEL },
6931 /* Lenovo models with volume control (needs confirmation) */
6932 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6933 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6934 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6935 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6936 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6937 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6938 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6940 /* Whitelist mute-only on all Lenovo by default */
6941 { .vendor = PCI_VENDOR_ID_LENOVO,
6942 .bios = TPACPI_MATCH_ANY,
6943 .ec = TPACPI_MATCH_ANY,
6944 .quirks = TPACPI_VOL_Q_MUTEONLY }
6947 static int __init volume_init(struct ibm_init_struct *iibm)
6949 unsigned long quirks;
6950 int rc;
6952 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6954 mutex_init(&volume_mutex);
6957 * Check for module parameter bogosity, note that we
6958 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6959 * able to detect "unspecified"
6961 if (volume_mode > TPACPI_VOL_MODE_MAX)
6962 return -EINVAL;
6964 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6965 printk(TPACPI_ERR
6966 "UCMS step volume mode not implemented, "
6967 "please contact %s\n", TPACPI_MAIL);
6968 return 1;
6971 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6972 return -EINVAL;
6975 * The ALSA mixer is our primary interface.
6976 * When disabled, don't install the subdriver at all
6978 if (!alsa_enable) {
6979 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6980 "ALSA mixer disabled by parameter, "
6981 "not loading volume subdriver...\n");
6982 return 1;
6985 quirks = tpacpi_check_quirks(volume_quirk_table,
6986 ARRAY_SIZE(volume_quirk_table));
6988 switch (volume_capabilities) {
6989 case TPACPI_VOL_CAP_AUTO:
6990 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6991 tp_features.mixer_no_level_control = 1;
6992 else if (quirks & TPACPI_VOL_Q_LEVEL)
6993 tp_features.mixer_no_level_control = 0;
6994 else
6995 return 1; /* no mixer */
6996 break;
6997 case TPACPI_VOL_CAP_VOLMUTE:
6998 tp_features.mixer_no_level_control = 0;
6999 break;
7000 case TPACPI_VOL_CAP_MUTEONLY:
7001 tp_features.mixer_no_level_control = 1;
7002 break;
7003 default:
7004 return 1;
7007 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7008 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7009 "using user-supplied volume_capabilities=%d\n",
7010 volume_capabilities);
7012 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7013 volume_mode == TPACPI_VOL_MODE_MAX) {
7014 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7016 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7017 "driver auto-selected volume_mode=%d\n",
7018 volume_mode);
7019 } else {
7020 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7021 "using user-supplied volume_mode=%d\n",
7022 volume_mode);
7025 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7026 "mute is supported, volume control is %s\n",
7027 str_supported(!tp_features.mixer_no_level_control));
7029 rc = volume_create_alsa_mixer();
7030 if (rc) {
7031 printk(TPACPI_ERR
7032 "Could not create the ALSA mixer interface\n");
7033 return rc;
7036 printk(TPACPI_INFO
7037 "Console audio control enabled, mode: %s\n",
7038 (volume_control_allowed) ?
7039 "override (read/write)" :
7040 "monitor (read only)");
7042 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7043 "registering volume hotkeys as change notification\n");
7044 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7045 | TP_ACPI_HKEY_VOLUP_MASK
7046 | TP_ACPI_HKEY_VOLDWN_MASK
7047 | TP_ACPI_HKEY_MUTE_MASK);
7049 return 0;
7052 static int volume_read(struct seq_file *m)
7054 u8 status;
7056 if (volume_get_status(&status) < 0) {
7057 seq_printf(m, "level:\t\tunreadable\n");
7058 } else {
7059 if (tp_features.mixer_no_level_control)
7060 seq_printf(m, "level:\t\tunsupported\n");
7061 else
7062 seq_printf(m, "level:\t\t%d\n",
7063 status & TP_EC_AUDIO_LVL_MSK);
7065 seq_printf(m, "mute:\t\t%s\n",
7066 onoff(status, TP_EC_AUDIO_MUTESW));
7068 if (volume_control_allowed) {
7069 seq_printf(m, "commands:\tunmute, mute\n");
7070 if (!tp_features.mixer_no_level_control) {
7071 seq_printf(m,
7072 "commands:\tup, down\n");
7073 seq_printf(m,
7074 "commands:\tlevel <level>"
7075 " (<level> is 0-%d)\n",
7076 TP_EC_VOLUME_MAX);
7081 return 0;
7084 static int volume_write(char *buf)
7086 u8 s;
7087 u8 new_level, new_mute;
7088 int l;
7089 char *cmd;
7090 int rc;
7093 * We do allow volume control at driver startup, so that the
7094 * user can set initial state through the volume=... parameter hack.
7096 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7097 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7098 tp_warned.volume_ctrl_forbidden = 1;
7099 printk(TPACPI_NOTICE
7100 "Console audio control in monitor mode, "
7101 "changes are not allowed.\n");
7102 printk(TPACPI_NOTICE
7103 "Use the volume_control=1 module parameter "
7104 "to enable volume control\n");
7106 return -EPERM;
7109 rc = volume_get_status(&s);
7110 if (rc < 0)
7111 return rc;
7113 new_level = s & TP_EC_AUDIO_LVL_MSK;
7114 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7116 while ((cmd = next_cmd(&buf))) {
7117 if (!tp_features.mixer_no_level_control) {
7118 if (strlencmp(cmd, "up") == 0) {
7119 if (new_mute)
7120 new_mute = 0;
7121 else if (new_level < TP_EC_VOLUME_MAX)
7122 new_level++;
7123 continue;
7124 } else if (strlencmp(cmd, "down") == 0) {
7125 if (new_mute)
7126 new_mute = 0;
7127 else if (new_level > 0)
7128 new_level--;
7129 continue;
7130 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7131 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7132 new_level = l;
7133 continue;
7136 if (strlencmp(cmd, "mute") == 0)
7137 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7138 else if (strlencmp(cmd, "unmute") == 0)
7139 new_mute = 0;
7140 else
7141 return -EINVAL;
7144 if (tp_features.mixer_no_level_control) {
7145 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7146 new_mute ? "" : "un");
7147 rc = volume_set_mute(!!new_mute);
7148 } else {
7149 tpacpi_disclose_usertask("procfs volume",
7150 "%smute and set level to %d\n",
7151 new_mute ? "" : "un", new_level);
7152 rc = volume_set_status(new_mute | new_level);
7154 volume_alsa_notify_change();
7156 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7159 static struct ibm_struct volume_driver_data = {
7160 .name = "volume",
7161 .read = volume_read,
7162 .write = volume_write,
7163 .exit = volume_exit,
7164 .suspend = volume_suspend,
7165 .resume = volume_resume,
7166 .shutdown = volume_shutdown,
7169 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7171 #define alsa_card NULL
7173 static void inline volume_alsa_notify_change(void)
7177 static int __init volume_init(struct ibm_init_struct *iibm)
7179 printk(TPACPI_INFO
7180 "volume: disabled as there is no ALSA support in this kernel\n");
7182 return 1;
7185 static struct ibm_struct volume_driver_data = {
7186 .name = "volume",
7189 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7191 /*************************************************************************
7192 * Fan subdriver
7196 * FAN ACCESS MODES
7198 * TPACPI_FAN_RD_ACPI_GFAN:
7199 * ACPI GFAN method: returns fan level
7201 * see TPACPI_FAN_WR_ACPI_SFAN
7202 * EC 0x2f (HFSP) not available if GFAN exists
7204 * TPACPI_FAN_WR_ACPI_SFAN:
7205 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7207 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7208 * it for writing.
7210 * TPACPI_FAN_WR_TPEC:
7211 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7212 * Supported on almost all ThinkPads
7214 * Fan speed changes of any sort (including those caused by the
7215 * disengaged mode) are usually done slowly by the firmware as the
7216 * maximum amount of fan duty cycle change per second seems to be
7217 * limited.
7219 * Reading is not available if GFAN exists.
7220 * Writing is not available if SFAN exists.
7222 * Bits
7223 * 7 automatic mode engaged;
7224 * (default operation mode of the ThinkPad)
7225 * fan level is ignored in this mode.
7226 * 6 full speed mode (takes precedence over bit 7);
7227 * not available on all thinkpads. May disable
7228 * the tachometer while the fan controller ramps up
7229 * the speed (which can take up to a few *minutes*).
7230 * Speeds up fan to 100% duty-cycle, which is far above
7231 * the standard RPM levels. It is not impossible that
7232 * it could cause hardware damage.
7233 * 5-3 unused in some models. Extra bits for fan level
7234 * in others, but still useless as all values above
7235 * 7 map to the same speed as level 7 in these models.
7236 * 2-0 fan level (0..7 usually)
7237 * 0x00 = stop
7238 * 0x07 = max (set when temperatures critical)
7239 * Some ThinkPads may have other levels, see
7240 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7242 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7243 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7244 * does so, its initial value is meaningless (0x07).
7246 * For firmware bugs, refer to:
7247 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7249 * ----
7251 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7252 * Main fan tachometer reading (in RPM)
7254 * This register is present on all ThinkPads with a new-style EC, and
7255 * it is known not to be present on the A21m/e, and T22, as there is
7256 * something else in offset 0x84 according to the ACPI DSDT. Other
7257 * ThinkPads from this same time period (and earlier) probably lack the
7258 * tachometer as well.
7260 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7261 * was never fixed by IBM to report the EC firmware version string
7262 * probably support the tachometer (like the early X models), so
7263 * detecting it is quite hard. We need more data to know for sure.
7265 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7266 * might result.
7268 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7269 * mode.
7271 * For firmware bugs, refer to:
7272 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7274 * ----
7276 * ThinkPad EC register 0x31 bit 0 (only on select models)
7278 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7279 * show the speed of the main fan. When bit 0 of EC register 0x31
7280 * is one, the tachometer registers show the speed of the auxiliary
7281 * fan.
7283 * Fan control seems to affect both fans, regardless of the state
7284 * of this bit.
7286 * So far, only the firmware for the X60/X61 non-tablet versions
7287 * seem to support this (firmware TP-7M).
7289 * TPACPI_FAN_WR_ACPI_FANS:
7290 * ThinkPad X31, X40, X41. Not available in the X60.
7292 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7293 * high speed. ACPI DSDT seems to map these three speeds to levels
7294 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7295 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7297 * The speeds are stored on handles
7298 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7300 * There are three default speed sets, accessible as handles:
7301 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7303 * ACPI DSDT switches which set is in use depending on various
7304 * factors.
7306 * TPACPI_FAN_WR_TPEC is also available and should be used to
7307 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7308 * but the ACPI tables just mention level 7.
7311 enum { /* Fan control constants */
7312 fan_status_offset = 0x2f, /* EC register 0x2f */
7313 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7314 * 0x84 must be read before 0x85 */
7315 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7316 bit 0 selects which fan is active */
7318 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7319 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7321 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7324 enum fan_status_access_mode {
7325 TPACPI_FAN_NONE = 0, /* No fan status or control */
7326 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7327 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7330 enum fan_control_access_mode {
7331 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7332 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7333 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7334 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7337 enum fan_control_commands {
7338 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7339 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7340 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7341 * and also watchdog cmd */
7344 static int fan_control_allowed;
7346 static enum fan_status_access_mode fan_status_access_mode;
7347 static enum fan_control_access_mode fan_control_access_mode;
7348 static enum fan_control_commands fan_control_commands;
7350 static u8 fan_control_initial_status;
7351 static u8 fan_control_desired_level;
7352 static u8 fan_control_resume_level;
7353 static int fan_watchdog_maxinterval;
7355 static struct mutex fan_mutex;
7357 static void fan_watchdog_fire(struct work_struct *ignored);
7358 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7360 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7361 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7362 "\\FSPD", /* 600e/x, 770e, 770x */
7363 ); /* all others */
7364 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7365 "JFNS", /* 770x-JL */
7366 ); /* all others */
7369 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7370 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7371 * be in auto mode (0x80).
7373 * This is corrected by any write to HFSP either by the driver, or
7374 * by the firmware.
7376 * We assume 0x07 really means auto mode while this quirk is active,
7377 * as this is far more likely than the ThinkPad being in level 7,
7378 * which is only used by the firmware during thermal emergencies.
7380 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7381 * TP-70 (T43, R52), which are known to be buggy.
7384 static void fan_quirk1_setup(void)
7386 if (fan_control_initial_status == 0x07) {
7387 printk(TPACPI_NOTICE
7388 "fan_init: initial fan status is unknown, "
7389 "assuming it is in auto mode\n");
7390 tp_features.fan_ctrl_status_undef = 1;
7394 static void fan_quirk1_handle(u8 *fan_status)
7396 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7397 if (*fan_status != fan_control_initial_status) {
7398 /* something changed the HFSP regisnter since
7399 * driver init time, so it is not undefined
7400 * anymore */
7401 tp_features.fan_ctrl_status_undef = 0;
7402 } else {
7403 /* Return most likely status. In fact, it
7404 * might be the only possible status */
7405 *fan_status = TP_EC_FAN_AUTO;
7410 /* Select main fan on X60/X61, NOOP on others */
7411 static bool fan_select_fan1(void)
7413 if (tp_features.second_fan) {
7414 u8 val;
7416 if (ec_read(fan_select_offset, &val) < 0)
7417 return false;
7418 val &= 0xFEU;
7419 if (ec_write(fan_select_offset, val) < 0)
7420 return false;
7422 return true;
7425 /* Select secondary fan on X60/X61 */
7426 static bool fan_select_fan2(void)
7428 u8 val;
7430 if (!tp_features.second_fan)
7431 return false;
7433 if (ec_read(fan_select_offset, &val) < 0)
7434 return false;
7435 val |= 0x01U;
7436 if (ec_write(fan_select_offset, val) < 0)
7437 return false;
7439 return true;
7443 * Call with fan_mutex held
7445 static void fan_update_desired_level(u8 status)
7447 if ((status &
7448 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7449 if (status > 7)
7450 fan_control_desired_level = 7;
7451 else
7452 fan_control_desired_level = status;
7456 static int fan_get_status(u8 *status)
7458 u8 s;
7460 /* TODO:
7461 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7463 switch (fan_status_access_mode) {
7464 case TPACPI_FAN_RD_ACPI_GFAN:
7465 /* 570, 600e/x, 770e, 770x */
7467 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7468 return -EIO;
7470 if (likely(status))
7471 *status = s & 0x07;
7473 break;
7475 case TPACPI_FAN_RD_TPEC:
7476 /* all except 570, 600e/x, 770e, 770x */
7477 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7478 return -EIO;
7480 if (likely(status)) {
7481 *status = s;
7482 fan_quirk1_handle(status);
7485 break;
7487 default:
7488 return -ENXIO;
7491 return 0;
7494 static int fan_get_status_safe(u8 *status)
7496 int rc;
7497 u8 s;
7499 if (mutex_lock_killable(&fan_mutex))
7500 return -ERESTARTSYS;
7501 rc = fan_get_status(&s);
7502 if (!rc)
7503 fan_update_desired_level(s);
7504 mutex_unlock(&fan_mutex);
7506 if (status)
7507 *status = s;
7509 return rc;
7512 static int fan_get_speed(unsigned int *speed)
7514 u8 hi, lo;
7516 switch (fan_status_access_mode) {
7517 case TPACPI_FAN_RD_TPEC:
7518 /* all except 570, 600e/x, 770e, 770x */
7519 if (unlikely(!fan_select_fan1()))
7520 return -EIO;
7521 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7522 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7523 return -EIO;
7525 if (likely(speed))
7526 *speed = (hi << 8) | lo;
7528 break;
7530 default:
7531 return -ENXIO;
7534 return 0;
7537 static int fan2_get_speed(unsigned int *speed)
7539 u8 hi, lo;
7540 bool rc;
7542 switch (fan_status_access_mode) {
7543 case TPACPI_FAN_RD_TPEC:
7544 /* all except 570, 600e/x, 770e, 770x */
7545 if (unlikely(!fan_select_fan2()))
7546 return -EIO;
7547 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7548 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7549 fan_select_fan1(); /* play it safe */
7550 if (rc)
7551 return -EIO;
7553 if (likely(speed))
7554 *speed = (hi << 8) | lo;
7556 break;
7558 default:
7559 return -ENXIO;
7562 return 0;
7565 static int fan_set_level(int level)
7567 if (!fan_control_allowed)
7568 return -EPERM;
7570 switch (fan_control_access_mode) {
7571 case TPACPI_FAN_WR_ACPI_SFAN:
7572 if (level >= 0 && level <= 7) {
7573 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7574 return -EIO;
7575 } else
7576 return -EINVAL;
7577 break;
7579 case TPACPI_FAN_WR_ACPI_FANS:
7580 case TPACPI_FAN_WR_TPEC:
7581 if (!(level & TP_EC_FAN_AUTO) &&
7582 !(level & TP_EC_FAN_FULLSPEED) &&
7583 ((level < 0) || (level > 7)))
7584 return -EINVAL;
7586 /* safety net should the EC not support AUTO
7587 * or FULLSPEED mode bits and just ignore them */
7588 if (level & TP_EC_FAN_FULLSPEED)
7589 level |= 7; /* safety min speed 7 */
7590 else if (level & TP_EC_FAN_AUTO)
7591 level |= 4; /* safety min speed 4 */
7593 if (!acpi_ec_write(fan_status_offset, level))
7594 return -EIO;
7595 else
7596 tp_features.fan_ctrl_status_undef = 0;
7597 break;
7599 default:
7600 return -ENXIO;
7603 vdbg_printk(TPACPI_DBG_FAN,
7604 "fan control: set fan control register to 0x%02x\n", level);
7605 return 0;
7608 static int fan_set_level_safe(int level)
7610 int rc;
7612 if (!fan_control_allowed)
7613 return -EPERM;
7615 if (mutex_lock_killable(&fan_mutex))
7616 return -ERESTARTSYS;
7618 if (level == TPACPI_FAN_LAST_LEVEL)
7619 level = fan_control_desired_level;
7621 rc = fan_set_level(level);
7622 if (!rc)
7623 fan_update_desired_level(level);
7625 mutex_unlock(&fan_mutex);
7626 return rc;
7629 static int fan_set_enable(void)
7631 u8 s;
7632 int rc;
7634 if (!fan_control_allowed)
7635 return -EPERM;
7637 if (mutex_lock_killable(&fan_mutex))
7638 return -ERESTARTSYS;
7640 switch (fan_control_access_mode) {
7641 case TPACPI_FAN_WR_ACPI_FANS:
7642 case TPACPI_FAN_WR_TPEC:
7643 rc = fan_get_status(&s);
7644 if (rc < 0)
7645 break;
7647 /* Don't go out of emergency fan mode */
7648 if (s != 7) {
7649 s &= 0x07;
7650 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7653 if (!acpi_ec_write(fan_status_offset, s))
7654 rc = -EIO;
7655 else {
7656 tp_features.fan_ctrl_status_undef = 0;
7657 rc = 0;
7659 break;
7661 case TPACPI_FAN_WR_ACPI_SFAN:
7662 rc = fan_get_status(&s);
7663 if (rc < 0)
7664 break;
7666 s &= 0x07;
7668 /* Set fan to at least level 4 */
7669 s |= 4;
7671 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7672 rc = -EIO;
7673 else
7674 rc = 0;
7675 break;
7677 default:
7678 rc = -ENXIO;
7681 mutex_unlock(&fan_mutex);
7683 if (!rc)
7684 vdbg_printk(TPACPI_DBG_FAN,
7685 "fan control: set fan control register to 0x%02x\n",
7687 return rc;
7690 static int fan_set_disable(void)
7692 int rc;
7694 if (!fan_control_allowed)
7695 return -EPERM;
7697 if (mutex_lock_killable(&fan_mutex))
7698 return -ERESTARTSYS;
7700 rc = 0;
7701 switch (fan_control_access_mode) {
7702 case TPACPI_FAN_WR_ACPI_FANS:
7703 case TPACPI_FAN_WR_TPEC:
7704 if (!acpi_ec_write(fan_status_offset, 0x00))
7705 rc = -EIO;
7706 else {
7707 fan_control_desired_level = 0;
7708 tp_features.fan_ctrl_status_undef = 0;
7710 break;
7712 case TPACPI_FAN_WR_ACPI_SFAN:
7713 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7714 rc = -EIO;
7715 else
7716 fan_control_desired_level = 0;
7717 break;
7719 default:
7720 rc = -ENXIO;
7723 if (!rc)
7724 vdbg_printk(TPACPI_DBG_FAN,
7725 "fan control: set fan control register to 0\n");
7727 mutex_unlock(&fan_mutex);
7728 return rc;
7731 static int fan_set_speed(int speed)
7733 int rc;
7735 if (!fan_control_allowed)
7736 return -EPERM;
7738 if (mutex_lock_killable(&fan_mutex))
7739 return -ERESTARTSYS;
7741 rc = 0;
7742 switch (fan_control_access_mode) {
7743 case TPACPI_FAN_WR_ACPI_FANS:
7744 if (speed >= 0 && speed <= 65535) {
7745 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7746 speed, speed, speed))
7747 rc = -EIO;
7748 } else
7749 rc = -EINVAL;
7750 break;
7752 default:
7753 rc = -ENXIO;
7756 mutex_unlock(&fan_mutex);
7757 return rc;
7760 static void fan_watchdog_reset(void)
7762 static int fan_watchdog_active;
7764 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7765 return;
7767 if (fan_watchdog_active)
7768 cancel_delayed_work(&fan_watchdog_task);
7770 if (fan_watchdog_maxinterval > 0 &&
7771 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7772 fan_watchdog_active = 1;
7773 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7774 msecs_to_jiffies(fan_watchdog_maxinterval
7775 * 1000))) {
7776 printk(TPACPI_ERR
7777 "failed to queue the fan watchdog, "
7778 "watchdog will not trigger\n");
7780 } else
7781 fan_watchdog_active = 0;
7784 static void fan_watchdog_fire(struct work_struct *ignored)
7786 int rc;
7788 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7789 return;
7791 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7792 rc = fan_set_enable();
7793 if (rc < 0) {
7794 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7795 "will try again later...\n", -rc);
7796 /* reschedule for later */
7797 fan_watchdog_reset();
7802 * SYSFS fan layout: hwmon compatible (device)
7804 * pwm*_enable:
7805 * 0: "disengaged" mode
7806 * 1: manual mode
7807 * 2: native EC "auto" mode (recommended, hardware default)
7809 * pwm*: set speed in manual mode, ignored otherwise.
7810 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7811 * interpolation.
7813 * fan*_input: tachometer reading, RPM
7816 * SYSFS fan layout: extensions
7818 * fan_watchdog (driver):
7819 * fan watchdog interval in seconds, 0 disables (default), max 120
7822 /* sysfs fan pwm1_enable ----------------------------------------------- */
7823 static ssize_t fan_pwm1_enable_show(struct device *dev,
7824 struct device_attribute *attr,
7825 char *buf)
7827 int res, mode;
7828 u8 status;
7830 res = fan_get_status_safe(&status);
7831 if (res)
7832 return res;
7834 if (status & TP_EC_FAN_FULLSPEED) {
7835 mode = 0;
7836 } else if (status & TP_EC_FAN_AUTO) {
7837 mode = 2;
7838 } else
7839 mode = 1;
7841 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7844 static ssize_t fan_pwm1_enable_store(struct device *dev,
7845 struct device_attribute *attr,
7846 const char *buf, size_t count)
7848 unsigned long t;
7849 int res, level;
7851 if (parse_strtoul(buf, 2, &t))
7852 return -EINVAL;
7854 tpacpi_disclose_usertask("hwmon pwm1_enable",
7855 "set fan mode to %lu\n", t);
7857 switch (t) {
7858 case 0:
7859 level = TP_EC_FAN_FULLSPEED;
7860 break;
7861 case 1:
7862 level = TPACPI_FAN_LAST_LEVEL;
7863 break;
7864 case 2:
7865 level = TP_EC_FAN_AUTO;
7866 break;
7867 case 3:
7868 /* reserved for software-controlled auto mode */
7869 return -ENOSYS;
7870 default:
7871 return -EINVAL;
7874 res = fan_set_level_safe(level);
7875 if (res == -ENXIO)
7876 return -EINVAL;
7877 else if (res < 0)
7878 return res;
7880 fan_watchdog_reset();
7882 return count;
7885 static struct device_attribute dev_attr_fan_pwm1_enable =
7886 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7887 fan_pwm1_enable_show, fan_pwm1_enable_store);
7889 /* sysfs fan pwm1 ------------------------------------------------------ */
7890 static ssize_t fan_pwm1_show(struct device *dev,
7891 struct device_attribute *attr,
7892 char *buf)
7894 int res;
7895 u8 status;
7897 res = fan_get_status_safe(&status);
7898 if (res)
7899 return res;
7901 if ((status &
7902 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7903 status = fan_control_desired_level;
7905 if (status > 7)
7906 status = 7;
7908 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7911 static ssize_t fan_pwm1_store(struct device *dev,
7912 struct device_attribute *attr,
7913 const char *buf, size_t count)
7915 unsigned long s;
7916 int rc;
7917 u8 status, newlevel;
7919 if (parse_strtoul(buf, 255, &s))
7920 return -EINVAL;
7922 tpacpi_disclose_usertask("hwmon pwm1",
7923 "set fan speed to %lu\n", s);
7925 /* scale down from 0-255 to 0-7 */
7926 newlevel = (s >> 5) & 0x07;
7928 if (mutex_lock_killable(&fan_mutex))
7929 return -ERESTARTSYS;
7931 rc = fan_get_status(&status);
7932 if (!rc && (status &
7933 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7934 rc = fan_set_level(newlevel);
7935 if (rc == -ENXIO)
7936 rc = -EINVAL;
7937 else if (!rc) {
7938 fan_update_desired_level(newlevel);
7939 fan_watchdog_reset();
7943 mutex_unlock(&fan_mutex);
7944 return (rc)? rc : count;
7947 static struct device_attribute dev_attr_fan_pwm1 =
7948 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7949 fan_pwm1_show, fan_pwm1_store);
7951 /* sysfs fan fan1_input ------------------------------------------------ */
7952 static ssize_t fan_fan1_input_show(struct device *dev,
7953 struct device_attribute *attr,
7954 char *buf)
7956 int res;
7957 unsigned int speed;
7959 res = fan_get_speed(&speed);
7960 if (res < 0)
7961 return res;
7963 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7966 static struct device_attribute dev_attr_fan_fan1_input =
7967 __ATTR(fan1_input, S_IRUGO,
7968 fan_fan1_input_show, NULL);
7970 /* sysfs fan fan2_input ------------------------------------------------ */
7971 static ssize_t fan_fan2_input_show(struct device *dev,
7972 struct device_attribute *attr,
7973 char *buf)
7975 int res;
7976 unsigned int speed;
7978 res = fan2_get_speed(&speed);
7979 if (res < 0)
7980 return res;
7982 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7985 static struct device_attribute dev_attr_fan_fan2_input =
7986 __ATTR(fan2_input, S_IRUGO,
7987 fan_fan2_input_show, NULL);
7989 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7990 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7991 char *buf)
7993 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7996 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7997 const char *buf, size_t count)
7999 unsigned long t;
8001 if (parse_strtoul(buf, 120, &t))
8002 return -EINVAL;
8004 if (!fan_control_allowed)
8005 return -EPERM;
8007 fan_watchdog_maxinterval = t;
8008 fan_watchdog_reset();
8010 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8012 return count;
8015 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8016 fan_fan_watchdog_show, fan_fan_watchdog_store);
8018 /* --------------------------------------------------------------------- */
8019 static struct attribute *fan_attributes[] = {
8020 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
8021 &dev_attr_fan_fan1_input.attr,
8022 NULL, /* for fan2_input */
8023 NULL
8026 static const struct attribute_group fan_attr_group = {
8027 .attrs = fan_attributes,
8030 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8031 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8033 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8034 { .vendor = PCI_VENDOR_ID_IBM, \
8035 .bios = TPACPI_MATCH_ANY, \
8036 .ec = TPID(__id1, __id2), \
8037 .quirks = __quirks }
8039 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8040 { .vendor = PCI_VENDOR_ID_LENOVO, \
8041 .bios = TPACPI_MATCH_ANY, \
8042 .ec = TPID(__id1, __id2), \
8043 .quirks = __quirks }
8045 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8046 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8047 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8048 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8049 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8050 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8053 #undef TPACPI_FAN_QL
8054 #undef TPACPI_FAN_QI
8056 static int __init fan_init(struct ibm_init_struct *iibm)
8058 int rc;
8059 unsigned long quirks;
8061 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8062 "initializing fan subdriver\n");
8064 mutex_init(&fan_mutex);
8065 fan_status_access_mode = TPACPI_FAN_NONE;
8066 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8067 fan_control_commands = 0;
8068 fan_watchdog_maxinterval = 0;
8069 tp_features.fan_ctrl_status_undef = 0;
8070 tp_features.second_fan = 0;
8071 fan_control_desired_level = 7;
8073 if (tpacpi_is_ibm()) {
8074 TPACPI_ACPIHANDLE_INIT(fans);
8075 TPACPI_ACPIHANDLE_INIT(gfan);
8076 TPACPI_ACPIHANDLE_INIT(sfan);
8079 quirks = tpacpi_check_quirks(fan_quirk_table,
8080 ARRAY_SIZE(fan_quirk_table));
8082 if (gfan_handle) {
8083 /* 570, 600e/x, 770e, 770x */
8084 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8085 } else {
8086 /* all other ThinkPads: note that even old-style
8087 * ThinkPad ECs supports the fan control register */
8088 if (likely(acpi_ec_read(fan_status_offset,
8089 &fan_control_initial_status))) {
8090 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8091 if (quirks & TPACPI_FAN_Q1)
8092 fan_quirk1_setup();
8093 if (quirks & TPACPI_FAN_2FAN) {
8094 tp_features.second_fan = 1;
8095 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8096 "secondary fan support enabled\n");
8098 } else {
8099 printk(TPACPI_ERR
8100 "ThinkPad ACPI EC access misbehaving, "
8101 "fan status and control unavailable\n");
8102 return 1;
8106 if (sfan_handle) {
8107 /* 570, 770x-JL */
8108 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8109 fan_control_commands |=
8110 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8111 } else {
8112 if (!gfan_handle) {
8113 /* gfan without sfan means no fan control */
8114 /* all other models implement TP EC 0x2f control */
8116 if (fans_handle) {
8117 /* X31, X40, X41 */
8118 fan_control_access_mode =
8119 TPACPI_FAN_WR_ACPI_FANS;
8120 fan_control_commands |=
8121 TPACPI_FAN_CMD_SPEED |
8122 TPACPI_FAN_CMD_LEVEL |
8123 TPACPI_FAN_CMD_ENABLE;
8124 } else {
8125 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8126 fan_control_commands |=
8127 TPACPI_FAN_CMD_LEVEL |
8128 TPACPI_FAN_CMD_ENABLE;
8133 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8134 "fan is %s, modes %d, %d\n",
8135 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8136 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8137 fan_status_access_mode, fan_control_access_mode);
8139 /* fan control master switch */
8140 if (!fan_control_allowed) {
8141 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8142 fan_control_commands = 0;
8143 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8144 "fan control features disabled by parameter\n");
8147 /* update fan_control_desired_level */
8148 if (fan_status_access_mode != TPACPI_FAN_NONE)
8149 fan_get_status_safe(NULL);
8151 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8152 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8153 if (tp_features.second_fan) {
8154 /* attach second fan tachometer */
8155 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8156 &dev_attr_fan_fan2_input.attr;
8158 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8159 &fan_attr_group);
8160 if (rc < 0)
8161 return rc;
8163 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8164 &driver_attr_fan_watchdog);
8165 if (rc < 0) {
8166 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8167 &fan_attr_group);
8168 return rc;
8170 return 0;
8171 } else
8172 return 1;
8175 static void fan_exit(void)
8177 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8178 "cancelling any pending fan watchdog tasks\n");
8180 /* FIXME: can we really do this unconditionally? */
8181 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8182 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8183 &driver_attr_fan_watchdog);
8185 cancel_delayed_work(&fan_watchdog_task);
8186 flush_workqueue(tpacpi_wq);
8189 static void fan_suspend(pm_message_t state)
8191 int rc;
8193 if (!fan_control_allowed)
8194 return;
8196 /* Store fan status in cache */
8197 fan_control_resume_level = 0;
8198 rc = fan_get_status_safe(&fan_control_resume_level);
8199 if (rc < 0)
8200 printk(TPACPI_NOTICE
8201 "failed to read fan level for later "
8202 "restore during resume: %d\n", rc);
8204 /* if it is undefined, don't attempt to restore it.
8205 * KEEP THIS LAST */
8206 if (tp_features.fan_ctrl_status_undef)
8207 fan_control_resume_level = 0;
8210 static void fan_resume(void)
8212 u8 current_level = 7;
8213 bool do_set = false;
8214 int rc;
8216 /* DSDT *always* updates status on resume */
8217 tp_features.fan_ctrl_status_undef = 0;
8219 if (!fan_control_allowed ||
8220 !fan_control_resume_level ||
8221 (fan_get_status_safe(&current_level) < 0))
8222 return;
8224 switch (fan_control_access_mode) {
8225 case TPACPI_FAN_WR_ACPI_SFAN:
8226 /* never decrease fan level */
8227 do_set = (fan_control_resume_level > current_level);
8228 break;
8229 case TPACPI_FAN_WR_ACPI_FANS:
8230 case TPACPI_FAN_WR_TPEC:
8231 /* never decrease fan level, scale is:
8232 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8234 * We expect the firmware to set either 7 or AUTO, but we
8235 * handle FULLSPEED out of paranoia.
8237 * So, we can safely only restore FULLSPEED or 7, anything
8238 * else could slow the fan. Restoring AUTO is useless, at
8239 * best that's exactly what the DSDT already set (it is the
8240 * slower it uses).
8242 * Always keep in mind that the DSDT *will* have set the
8243 * fans to what the vendor supposes is the best level. We
8244 * muck with it only to speed the fan up.
8246 if (fan_control_resume_level != 7 &&
8247 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8248 return;
8249 else
8250 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8251 (current_level != fan_control_resume_level);
8252 break;
8253 default:
8254 return;
8256 if (do_set) {
8257 printk(TPACPI_NOTICE
8258 "restoring fan level to 0x%02x\n",
8259 fan_control_resume_level);
8260 rc = fan_set_level_safe(fan_control_resume_level);
8261 if (rc < 0)
8262 printk(TPACPI_NOTICE
8263 "failed to restore fan level: %d\n", rc);
8267 static int fan_read(struct seq_file *m)
8269 int rc;
8270 u8 status;
8271 unsigned int speed = 0;
8273 switch (fan_status_access_mode) {
8274 case TPACPI_FAN_RD_ACPI_GFAN:
8275 /* 570, 600e/x, 770e, 770x */
8276 rc = fan_get_status_safe(&status);
8277 if (rc < 0)
8278 return rc;
8280 seq_printf(m, "status:\t\t%s\n"
8281 "level:\t\t%d\n",
8282 (status != 0) ? "enabled" : "disabled", status);
8283 break;
8285 case TPACPI_FAN_RD_TPEC:
8286 /* all except 570, 600e/x, 770e, 770x */
8287 rc = fan_get_status_safe(&status);
8288 if (rc < 0)
8289 return rc;
8291 seq_printf(m, "status:\t\t%s\n",
8292 (status != 0) ? "enabled" : "disabled");
8294 rc = fan_get_speed(&speed);
8295 if (rc < 0)
8296 return rc;
8298 seq_printf(m, "speed:\t\t%d\n", speed);
8300 if (status & TP_EC_FAN_FULLSPEED)
8301 /* Disengaged mode takes precedence */
8302 seq_printf(m, "level:\t\tdisengaged\n");
8303 else if (status & TP_EC_FAN_AUTO)
8304 seq_printf(m, "level:\t\tauto\n");
8305 else
8306 seq_printf(m, "level:\t\t%d\n", status);
8307 break;
8309 case TPACPI_FAN_NONE:
8310 default:
8311 seq_printf(m, "status:\t\tnot supported\n");
8314 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8315 seq_printf(m, "commands:\tlevel <level>");
8317 switch (fan_control_access_mode) {
8318 case TPACPI_FAN_WR_ACPI_SFAN:
8319 seq_printf(m, " (<level> is 0-7)\n");
8320 break;
8322 default:
8323 seq_printf(m, " (<level> is 0-7, "
8324 "auto, disengaged, full-speed)\n");
8325 break;
8329 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8330 seq_printf(m, "commands:\tenable, disable\n"
8331 "commands:\twatchdog <timeout> (<timeout> "
8332 "is 0 (off), 1-120 (seconds))\n");
8334 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8335 seq_printf(m, "commands:\tspeed <speed>"
8336 " (<speed> is 0-65535)\n");
8338 return 0;
8341 static int fan_write_cmd_level(const char *cmd, int *rc)
8343 int level;
8345 if (strlencmp(cmd, "level auto") == 0)
8346 level = TP_EC_FAN_AUTO;
8347 else if ((strlencmp(cmd, "level disengaged") == 0) |
8348 (strlencmp(cmd, "level full-speed") == 0))
8349 level = TP_EC_FAN_FULLSPEED;
8350 else if (sscanf(cmd, "level %d", &level) != 1)
8351 return 0;
8353 *rc = fan_set_level_safe(level);
8354 if (*rc == -ENXIO)
8355 printk(TPACPI_ERR "level command accepted for unsupported "
8356 "access mode %d", fan_control_access_mode);
8357 else if (!*rc)
8358 tpacpi_disclose_usertask("procfs fan",
8359 "set level to %d\n", level);
8361 return 1;
8364 static int fan_write_cmd_enable(const char *cmd, int *rc)
8366 if (strlencmp(cmd, "enable") != 0)
8367 return 0;
8369 *rc = fan_set_enable();
8370 if (*rc == -ENXIO)
8371 printk(TPACPI_ERR "enable command accepted for unsupported "
8372 "access mode %d", fan_control_access_mode);
8373 else if (!*rc)
8374 tpacpi_disclose_usertask("procfs fan", "enable\n");
8376 return 1;
8379 static int fan_write_cmd_disable(const char *cmd, int *rc)
8381 if (strlencmp(cmd, "disable") != 0)
8382 return 0;
8384 *rc = fan_set_disable();
8385 if (*rc == -ENXIO)
8386 printk(TPACPI_ERR "disable command accepted for unsupported "
8387 "access mode %d", fan_control_access_mode);
8388 else if (!*rc)
8389 tpacpi_disclose_usertask("procfs fan", "disable\n");
8391 return 1;
8394 static int fan_write_cmd_speed(const char *cmd, int *rc)
8396 int speed;
8398 /* TODO:
8399 * Support speed <low> <medium> <high> ? */
8401 if (sscanf(cmd, "speed %d", &speed) != 1)
8402 return 0;
8404 *rc = fan_set_speed(speed);
8405 if (*rc == -ENXIO)
8406 printk(TPACPI_ERR "speed command accepted for unsupported "
8407 "access mode %d", fan_control_access_mode);
8408 else if (!*rc)
8409 tpacpi_disclose_usertask("procfs fan",
8410 "set speed to %d\n", speed);
8412 return 1;
8415 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8417 int interval;
8419 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8420 return 0;
8422 if (interval < 0 || interval > 120)
8423 *rc = -EINVAL;
8424 else {
8425 fan_watchdog_maxinterval = interval;
8426 tpacpi_disclose_usertask("procfs fan",
8427 "set watchdog timer to %d\n",
8428 interval);
8431 return 1;
8434 static int fan_write(char *buf)
8436 char *cmd;
8437 int rc = 0;
8439 while (!rc && (cmd = next_cmd(&buf))) {
8440 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8441 fan_write_cmd_level(cmd, &rc)) &&
8442 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8443 (fan_write_cmd_enable(cmd, &rc) ||
8444 fan_write_cmd_disable(cmd, &rc) ||
8445 fan_write_cmd_watchdog(cmd, &rc))) &&
8446 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8447 fan_write_cmd_speed(cmd, &rc))
8449 rc = -EINVAL;
8450 else if (!rc)
8451 fan_watchdog_reset();
8454 return rc;
8457 static struct ibm_struct fan_driver_data = {
8458 .name = "fan",
8459 .read = fan_read,
8460 .write = fan_write,
8461 .exit = fan_exit,
8462 .suspend = fan_suspend,
8463 .resume = fan_resume,
8466 /****************************************************************************
8467 ****************************************************************************
8469 * Infrastructure
8471 ****************************************************************************
8472 ****************************************************************************/
8475 * HKEY event callout for other subdrivers go here
8476 * (yes, it is ugly, but it is quick, safe, and gets the job done
8478 static void tpacpi_driver_event(const unsigned int hkey_event)
8480 if (ibm_backlight_device) {
8481 switch (hkey_event) {
8482 case TP_HKEY_EV_BRGHT_UP:
8483 case TP_HKEY_EV_BRGHT_DOWN:
8484 tpacpi_brightness_notify_change();
8487 if (alsa_card) {
8488 switch (hkey_event) {
8489 case TP_HKEY_EV_VOL_UP:
8490 case TP_HKEY_EV_VOL_DOWN:
8491 case TP_HKEY_EV_VOL_MUTE:
8492 volume_alsa_notify_change();
8497 static void hotkey_driver_event(const unsigned int scancode)
8499 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8502 /* sysfs name ---------------------------------------------------------- */
8503 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8504 struct device_attribute *attr,
8505 char *buf)
8507 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8510 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8511 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8513 /* --------------------------------------------------------------------- */
8515 /* /proc support */
8516 static struct proc_dir_entry *proc_dir;
8519 * Module and infrastructure proble, init and exit handling
8522 static int force_load;
8524 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8525 static const char * __init str_supported(int is_supported)
8527 static char text_unsupported[] __initdata = "not supported";
8529 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8531 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8533 static void ibm_exit(struct ibm_struct *ibm)
8535 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8537 list_del_init(&ibm->all_drivers);
8539 if (ibm->flags.acpi_notify_installed) {
8540 dbg_printk(TPACPI_DBG_EXIT,
8541 "%s: acpi_remove_notify_handler\n", ibm->name);
8542 BUG_ON(!ibm->acpi);
8543 acpi_remove_notify_handler(*ibm->acpi->handle,
8544 ibm->acpi->type,
8545 dispatch_acpi_notify);
8546 ibm->flags.acpi_notify_installed = 0;
8547 ibm->flags.acpi_notify_installed = 0;
8550 if (ibm->flags.proc_created) {
8551 dbg_printk(TPACPI_DBG_EXIT,
8552 "%s: remove_proc_entry\n", ibm->name);
8553 remove_proc_entry(ibm->name, proc_dir);
8554 ibm->flags.proc_created = 0;
8557 if (ibm->flags.acpi_driver_registered) {
8558 dbg_printk(TPACPI_DBG_EXIT,
8559 "%s: acpi_bus_unregister_driver\n", ibm->name);
8560 BUG_ON(!ibm->acpi);
8561 acpi_bus_unregister_driver(ibm->acpi->driver);
8562 kfree(ibm->acpi->driver);
8563 ibm->acpi->driver = NULL;
8564 ibm->flags.acpi_driver_registered = 0;
8567 if (ibm->flags.init_called && ibm->exit) {
8568 ibm->exit();
8569 ibm->flags.init_called = 0;
8572 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8575 static int __init ibm_init(struct ibm_init_struct *iibm)
8577 int ret;
8578 struct ibm_struct *ibm = iibm->data;
8579 struct proc_dir_entry *entry;
8581 BUG_ON(ibm == NULL);
8583 INIT_LIST_HEAD(&ibm->all_drivers);
8585 if (ibm->flags.experimental && !experimental)
8586 return 0;
8588 dbg_printk(TPACPI_DBG_INIT,
8589 "probing for %s\n", ibm->name);
8591 if (iibm->init) {
8592 ret = iibm->init(iibm);
8593 if (ret > 0)
8594 return 0; /* probe failed */
8595 if (ret)
8596 return ret;
8598 ibm->flags.init_called = 1;
8601 if (ibm->acpi) {
8602 if (ibm->acpi->hid) {
8603 ret = register_tpacpi_subdriver(ibm);
8604 if (ret)
8605 goto err_out;
8608 if (ibm->acpi->notify) {
8609 ret = setup_acpi_notify(ibm);
8610 if (ret == -ENODEV) {
8611 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8612 ibm->name);
8613 ret = 0;
8614 goto err_out;
8616 if (ret < 0)
8617 goto err_out;
8621 dbg_printk(TPACPI_DBG_INIT,
8622 "%s installed\n", ibm->name);
8624 if (ibm->read) {
8625 mode_t mode = iibm->base_procfs_mode;
8627 if (!mode)
8628 mode = S_IRUGO;
8629 if (ibm->write)
8630 mode |= S_IWUSR;
8631 entry = proc_create_data(ibm->name, mode, proc_dir,
8632 &dispatch_proc_fops, ibm);
8633 if (!entry) {
8634 printk(TPACPI_ERR "unable to create proc entry %s\n",
8635 ibm->name);
8636 ret = -ENODEV;
8637 goto err_out;
8639 ibm->flags.proc_created = 1;
8642 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8644 return 0;
8646 err_out:
8647 dbg_printk(TPACPI_DBG_INIT,
8648 "%s: at error exit path with result %d\n",
8649 ibm->name, ret);
8651 ibm_exit(ibm);
8652 return (ret < 0)? ret : 0;
8655 /* Probing */
8657 static bool __pure __init tpacpi_is_fw_digit(const char c)
8659 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8662 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8663 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8664 const char t)
8666 return s && strlen(s) >= 8 &&
8667 tpacpi_is_fw_digit(s[0]) &&
8668 tpacpi_is_fw_digit(s[1]) &&
8669 s[2] == t && s[3] == 'T' &&
8670 tpacpi_is_fw_digit(s[4]) &&
8671 tpacpi_is_fw_digit(s[5]) &&
8672 s[6] == 'W' && s[7] == 'W';
8675 /* returns 0 - probe ok, or < 0 - probe error.
8676 * Probe ok doesn't mean thinkpad found.
8677 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8678 static int __must_check __init get_thinkpad_model_data(
8679 struct thinkpad_id_data *tp)
8681 const struct dmi_device *dev = NULL;
8682 char ec_fw_string[18];
8683 char const *s;
8685 if (!tp)
8686 return -EINVAL;
8688 memset(tp, 0, sizeof(*tp));
8690 if (dmi_name_in_vendors("IBM"))
8691 tp->vendor = PCI_VENDOR_ID_IBM;
8692 else if (dmi_name_in_vendors("LENOVO"))
8693 tp->vendor = PCI_VENDOR_ID_LENOVO;
8694 else
8695 return 0;
8697 s = dmi_get_system_info(DMI_BIOS_VERSION);
8698 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8699 if (s && !tp->bios_version_str)
8700 return -ENOMEM;
8702 /* Really ancient ThinkPad 240X will fail this, which is fine */
8703 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8704 return 0;
8706 tp->bios_model = tp->bios_version_str[0]
8707 | (tp->bios_version_str[1] << 8);
8708 tp->bios_release = (tp->bios_version_str[4] << 8)
8709 | tp->bios_version_str[5];
8712 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8713 * X32 or newer, all Z series; Some models must have an
8714 * up-to-date BIOS or they will not be detected.
8716 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8718 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8719 if (sscanf(dev->name,
8720 "IBM ThinkPad Embedded Controller -[%17c",
8721 ec_fw_string) == 1) {
8722 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8723 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8725 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8726 if (!tp->ec_version_str)
8727 return -ENOMEM;
8729 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8730 tp->ec_model = ec_fw_string[0]
8731 | (ec_fw_string[1] << 8);
8732 tp->ec_release = (ec_fw_string[4] << 8)
8733 | ec_fw_string[5];
8734 } else {
8735 printk(TPACPI_NOTICE
8736 "ThinkPad firmware release %s "
8737 "doesn't match the known patterns\n",
8738 ec_fw_string);
8739 printk(TPACPI_NOTICE
8740 "please report this to %s\n",
8741 TPACPI_MAIL);
8743 break;
8747 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8748 if (s && !strnicmp(s, "ThinkPad", 8)) {
8749 tp->model_str = kstrdup(s, GFP_KERNEL);
8750 if (!tp->model_str)
8751 return -ENOMEM;
8754 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8755 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8756 if (s && !tp->nummodel_str)
8757 return -ENOMEM;
8759 return 0;
8762 static int __init probe_for_thinkpad(void)
8764 int is_thinkpad;
8766 if (acpi_disabled)
8767 return -ENODEV;
8769 /* It would be dangerous to run the driver in this case */
8770 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8771 return -ENODEV;
8774 * Non-ancient models have better DMI tagging, but very old models
8775 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8777 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8778 (thinkpad_id.ec_model != 0) ||
8779 tpacpi_is_fw_known();
8781 /* The EC handler is required */
8782 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
8783 if (!ec_handle) {
8784 if (is_thinkpad)
8785 printk(TPACPI_ERR
8786 "Not yet supported ThinkPad detected!\n");
8787 return -ENODEV;
8790 if (!is_thinkpad && !force_load)
8791 return -ENODEV;
8793 return 0;
8796 static void __init thinkpad_acpi_init_banner(void)
8798 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8799 printk(TPACPI_INFO "%s\n", TPACPI_URL);
8801 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
8802 (thinkpad_id.bios_version_str) ?
8803 thinkpad_id.bios_version_str : "unknown",
8804 (thinkpad_id.ec_version_str) ?
8805 thinkpad_id.ec_version_str : "unknown");
8807 BUG_ON(!thinkpad_id.vendor);
8809 if (thinkpad_id.model_str)
8810 printk(TPACPI_INFO "%s %s, model %s\n",
8811 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8812 "IBM" : ((thinkpad_id.vendor ==
8813 PCI_VENDOR_ID_LENOVO) ?
8814 "Lenovo" : "Unknown vendor"),
8815 thinkpad_id.model_str,
8816 (thinkpad_id.nummodel_str) ?
8817 thinkpad_id.nummodel_str : "unknown");
8820 /* Module init, exit, parameters */
8822 static struct ibm_init_struct ibms_init[] __initdata = {
8824 .data = &thinkpad_acpi_driver_data,
8827 .init = hotkey_init,
8828 .data = &hotkey_driver_data,
8831 .init = bluetooth_init,
8832 .data = &bluetooth_driver_data,
8835 .init = wan_init,
8836 .data = &wan_driver_data,
8839 .init = uwb_init,
8840 .data = &uwb_driver_data,
8842 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8844 .init = video_init,
8845 .base_procfs_mode = S_IRUSR,
8846 .data = &video_driver_data,
8848 #endif
8850 .init = light_init,
8851 .data = &light_driver_data,
8854 .init = cmos_init,
8855 .data = &cmos_driver_data,
8858 .init = led_init,
8859 .data = &led_driver_data,
8862 .init = beep_init,
8863 .data = &beep_driver_data,
8866 .init = thermal_init,
8867 .data = &thermal_driver_data,
8870 .data = &ecdump_driver_data,
8873 .init = brightness_init,
8874 .data = &brightness_driver_data,
8877 .init = volume_init,
8878 .data = &volume_driver_data,
8881 .init = fan_init,
8882 .data = &fan_driver_data,
8886 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8888 unsigned int i;
8889 struct ibm_struct *ibm;
8891 if (!kp || !kp->name || !val)
8892 return -EINVAL;
8894 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8895 ibm = ibms_init[i].data;
8896 WARN_ON(ibm == NULL);
8898 if (!ibm || !ibm->name)
8899 continue;
8901 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8902 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8903 return -ENOSPC;
8904 strcpy(ibms_init[i].param, val);
8905 strcat(ibms_init[i].param, ",");
8906 return 0;
8910 return -EINVAL;
8913 module_param(experimental, int, 0444);
8914 MODULE_PARM_DESC(experimental,
8915 "Enables experimental features when non-zero");
8917 module_param_named(debug, dbg_level, uint, 0);
8918 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8920 module_param(force_load, bool, 0444);
8921 MODULE_PARM_DESC(force_load,
8922 "Attempts to load the driver even on a "
8923 "mis-identified ThinkPad when true");
8925 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8926 MODULE_PARM_DESC(fan_control,
8927 "Enables setting fan parameters features when true");
8929 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8930 MODULE_PARM_DESC(brightness_mode,
8931 "Selects brightness control strategy: "
8932 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8934 module_param(brightness_enable, uint, 0444);
8935 MODULE_PARM_DESC(brightness_enable,
8936 "Enables backlight control when 1, disables when 0");
8938 module_param(hotkey_report_mode, uint, 0444);
8939 MODULE_PARM_DESC(hotkey_report_mode,
8940 "used for backwards compatibility with userspace, "
8941 "see documentation");
8943 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8944 module_param_named(volume_mode, volume_mode, uint, 0444);
8945 MODULE_PARM_DESC(volume_mode,
8946 "Selects volume control strategy: "
8947 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8949 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8950 MODULE_PARM_DESC(volume_capabilities,
8951 "Selects the mixer capabilites: "
8952 "0=auto, 1=volume and mute, 2=mute only");
8954 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8955 MODULE_PARM_DESC(volume_control,
8956 "Enables software override for the console audio "
8957 "control when true");
8959 /* ALSA module API parameters */
8960 module_param_named(index, alsa_index, int, 0444);
8961 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8962 module_param_named(id, alsa_id, charp, 0444);
8963 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8964 module_param_named(enable, alsa_enable, bool, 0444);
8965 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8966 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8968 #define TPACPI_PARAM(feature) \
8969 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8970 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8971 "at module load, see documentation")
8973 TPACPI_PARAM(hotkey);
8974 TPACPI_PARAM(bluetooth);
8975 TPACPI_PARAM(video);
8976 TPACPI_PARAM(light);
8977 TPACPI_PARAM(cmos);
8978 TPACPI_PARAM(led);
8979 TPACPI_PARAM(beep);
8980 TPACPI_PARAM(ecdump);
8981 TPACPI_PARAM(brightness);
8982 TPACPI_PARAM(volume);
8983 TPACPI_PARAM(fan);
8985 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8986 module_param(dbg_wlswemul, uint, 0444);
8987 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8988 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8989 MODULE_PARM_DESC(wlsw_state,
8990 "Initial state of the emulated WLSW switch");
8992 module_param(dbg_bluetoothemul, uint, 0444);
8993 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8994 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8995 MODULE_PARM_DESC(bluetooth_state,
8996 "Initial state of the emulated bluetooth switch");
8998 module_param(dbg_wwanemul, uint, 0444);
8999 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9000 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9001 MODULE_PARM_DESC(wwan_state,
9002 "Initial state of the emulated WWAN switch");
9004 module_param(dbg_uwbemul, uint, 0444);
9005 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9006 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9007 MODULE_PARM_DESC(uwb_state,
9008 "Initial state of the emulated UWB switch");
9009 #endif
9011 static void thinkpad_acpi_module_exit(void)
9013 struct ibm_struct *ibm, *itmp;
9015 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9017 list_for_each_entry_safe_reverse(ibm, itmp,
9018 &tpacpi_all_drivers,
9019 all_drivers) {
9020 ibm_exit(ibm);
9023 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9025 if (tpacpi_inputdev) {
9026 if (tp_features.input_device_registered)
9027 input_unregister_device(tpacpi_inputdev);
9028 else
9029 input_free_device(tpacpi_inputdev);
9032 if (tpacpi_hwmon)
9033 hwmon_device_unregister(tpacpi_hwmon);
9035 if (tp_features.sensors_pdev_attrs_registered)
9036 device_remove_file(&tpacpi_sensors_pdev->dev,
9037 &dev_attr_thinkpad_acpi_pdev_name);
9038 if (tpacpi_sensors_pdev)
9039 platform_device_unregister(tpacpi_sensors_pdev);
9040 if (tpacpi_pdev)
9041 platform_device_unregister(tpacpi_pdev);
9043 if (tp_features.sensors_pdrv_attrs_registered)
9044 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9045 if (tp_features.platform_drv_attrs_registered)
9046 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9048 if (tp_features.sensors_pdrv_registered)
9049 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9051 if (tp_features.platform_drv_registered)
9052 platform_driver_unregister(&tpacpi_pdriver);
9054 if (proc_dir)
9055 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9057 if (tpacpi_wq)
9058 destroy_workqueue(tpacpi_wq);
9060 kfree(thinkpad_id.bios_version_str);
9061 kfree(thinkpad_id.ec_version_str);
9062 kfree(thinkpad_id.model_str);
9066 static int __init thinkpad_acpi_module_init(void)
9068 int ret, i;
9070 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9072 /* Parameter checking */
9073 if (hotkey_report_mode > 2)
9074 return -EINVAL;
9076 /* Driver-level probe */
9078 ret = get_thinkpad_model_data(&thinkpad_id);
9079 if (ret) {
9080 printk(TPACPI_ERR
9081 "unable to get DMI data: %d\n", ret);
9082 thinkpad_acpi_module_exit();
9083 return ret;
9085 ret = probe_for_thinkpad();
9086 if (ret) {
9087 thinkpad_acpi_module_exit();
9088 return ret;
9091 /* Driver initialization */
9093 thinkpad_acpi_init_banner();
9094 tpacpi_check_outdated_fw();
9096 TPACPI_ACPIHANDLE_INIT(ecrd);
9097 TPACPI_ACPIHANDLE_INIT(ecwr);
9099 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9100 if (!tpacpi_wq) {
9101 thinkpad_acpi_module_exit();
9102 return -ENOMEM;
9105 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9106 if (!proc_dir) {
9107 printk(TPACPI_ERR
9108 "unable to create proc dir " TPACPI_PROC_DIR);
9109 thinkpad_acpi_module_exit();
9110 return -ENODEV;
9113 ret = platform_driver_register(&tpacpi_pdriver);
9114 if (ret) {
9115 printk(TPACPI_ERR
9116 "unable to register main platform driver\n");
9117 thinkpad_acpi_module_exit();
9118 return ret;
9120 tp_features.platform_drv_registered = 1;
9122 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9123 if (ret) {
9124 printk(TPACPI_ERR
9125 "unable to register hwmon platform driver\n");
9126 thinkpad_acpi_module_exit();
9127 return ret;
9129 tp_features.sensors_pdrv_registered = 1;
9131 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9132 if (!ret) {
9133 tp_features.platform_drv_attrs_registered = 1;
9134 ret = tpacpi_create_driver_attributes(
9135 &tpacpi_hwmon_pdriver.driver);
9137 if (ret) {
9138 printk(TPACPI_ERR
9139 "unable to create sysfs driver attributes\n");
9140 thinkpad_acpi_module_exit();
9141 return ret;
9143 tp_features.sensors_pdrv_attrs_registered = 1;
9146 /* Device initialization */
9147 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9148 NULL, 0);
9149 if (IS_ERR(tpacpi_pdev)) {
9150 ret = PTR_ERR(tpacpi_pdev);
9151 tpacpi_pdev = NULL;
9152 printk(TPACPI_ERR "unable to register platform device\n");
9153 thinkpad_acpi_module_exit();
9154 return ret;
9156 tpacpi_sensors_pdev = platform_device_register_simple(
9157 TPACPI_HWMON_DRVR_NAME,
9158 -1, NULL, 0);
9159 if (IS_ERR(tpacpi_sensors_pdev)) {
9160 ret = PTR_ERR(tpacpi_sensors_pdev);
9161 tpacpi_sensors_pdev = NULL;
9162 printk(TPACPI_ERR
9163 "unable to register hwmon platform device\n");
9164 thinkpad_acpi_module_exit();
9165 return ret;
9167 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9168 &dev_attr_thinkpad_acpi_pdev_name);
9169 if (ret) {
9170 printk(TPACPI_ERR
9171 "unable to create sysfs hwmon device attributes\n");
9172 thinkpad_acpi_module_exit();
9173 return ret;
9175 tp_features.sensors_pdev_attrs_registered = 1;
9176 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9177 if (IS_ERR(tpacpi_hwmon)) {
9178 ret = PTR_ERR(tpacpi_hwmon);
9179 tpacpi_hwmon = NULL;
9180 printk(TPACPI_ERR "unable to register hwmon device\n");
9181 thinkpad_acpi_module_exit();
9182 return ret;
9184 mutex_init(&tpacpi_inputdev_send_mutex);
9185 tpacpi_inputdev = input_allocate_device();
9186 if (!tpacpi_inputdev) {
9187 printk(TPACPI_ERR "unable to allocate input device\n");
9188 thinkpad_acpi_module_exit();
9189 return -ENOMEM;
9190 } else {
9191 /* Prepare input device, but don't register */
9192 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9193 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9194 tpacpi_inputdev->id.bustype = BUS_HOST;
9195 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9196 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9197 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9198 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9201 /* Init subdriver dependencies */
9202 tpacpi_detect_brightness_capabilities();
9204 /* Init subdrivers */
9205 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9206 ret = ibm_init(&ibms_init[i]);
9207 if (ret >= 0 && *ibms_init[i].param)
9208 ret = ibms_init[i].data->write(ibms_init[i].param);
9209 if (ret < 0) {
9210 thinkpad_acpi_module_exit();
9211 return ret;
9215 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9217 ret = input_register_device(tpacpi_inputdev);
9218 if (ret < 0) {
9219 printk(TPACPI_ERR "unable to register input device\n");
9220 thinkpad_acpi_module_exit();
9221 return ret;
9222 } else {
9223 tp_features.input_device_registered = 1;
9226 return 0;
9229 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9232 * This will autoload the driver in almost every ThinkPad
9233 * in widespread use.
9235 * Only _VERY_ old models, like the 240, 240x and 570 lack
9236 * the HKEY event interface.
9238 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9241 * DMI matching for module autoloading
9243 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9244 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9246 * Only models listed in thinkwiki will be supported, so add yours
9247 * if it is not there yet.
9249 #define IBM_BIOS_MODULE_ALIAS(__type) \
9250 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9252 /* Ancient thinkpad BIOSes have to be identified by
9253 * BIOS type or model number, and there are far less
9254 * BIOS types than model numbers... */
9255 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9257 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9258 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9259 MODULE_DESCRIPTION(TPACPI_DESC);
9260 MODULE_VERSION(TPACPI_VERSION);
9261 MODULE_LICENSE("GPL");
9263 module_init(thinkpad_acpi_module_init);
9264 module_exit(thinkpad_acpi_module_exit);