thinkpad-acpi: support MUTE-only ThinkPads
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobee880f2e4a4a9a4e46c7a3230e487559e2d2fda2
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.23"
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 <acpi/acpi_drivers.h>
82 #include <linux/pci_ids.h>
85 /* ThinkPad CMOS commands */
86 #define TP_CMOS_VOLUME_DOWN 0
87 #define TP_CMOS_VOLUME_UP 1
88 #define TP_CMOS_VOLUME_MUTE 2
89 #define TP_CMOS_BRIGHTNESS_UP 4
90 #define TP_CMOS_BRIGHTNESS_DOWN 5
91 #define TP_CMOS_THINKLIGHT_ON 12
92 #define TP_CMOS_THINKLIGHT_OFF 13
94 /* NVRAM Addresses */
95 enum tp_nvram_addr {
96 TP_NVRAM_ADDR_HK2 = 0x57,
97 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
98 TP_NVRAM_ADDR_VIDEO = 0x59,
99 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
100 TP_NVRAM_ADDR_MIXER = 0x60,
103 /* NVRAM bit masks */
104 enum {
105 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
106 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
107 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
108 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
109 TP_NVRAM_MASK_THINKLIGHT = 0x10,
110 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
111 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
112 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
113 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
114 TP_NVRAM_MASK_MUTE = 0x40,
115 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
116 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
117 TP_NVRAM_POS_LEVEL_VOLUME = 0,
120 /* ACPI HIDs */
121 #define TPACPI_ACPI_HKEY_HID "IBM0068"
123 /* Input IDs */
124 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
125 #define TPACPI_HKEY_INPUT_VERSION 0x4101
127 /* ACPI \WGSV commands */
128 enum {
129 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
130 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
131 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
132 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
135 /* TP_ACPI_WGSV_GET_STATE bits */
136 enum {
137 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
138 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
139 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
140 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
141 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
142 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
143 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
144 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
145 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
146 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
149 /* HKEY events */
150 enum tpacpi_hkey_event_t {
151 /* Hotkey-related */
152 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
153 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
154 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
155 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
156 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
157 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
159 /* Reasons for waking up from S3/S4 */
160 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
161 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
162 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
163 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
164 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
165 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
167 /* Auto-sleep after eject request */
168 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
169 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
171 /* Misc bay events */
172 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
174 /* User-interface events */
175 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
176 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
177 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
178 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
179 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
180 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
181 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
183 /* Thermal events */
184 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
185 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
186 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
187 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
188 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
190 /* Misc */
191 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
194 /****************************************************************************
195 * Main driver
198 #define TPACPI_NAME "thinkpad"
199 #define TPACPI_DESC "ThinkPad ACPI Extras"
200 #define TPACPI_FILE TPACPI_NAME "_acpi"
201 #define TPACPI_URL "http://ibm-acpi.sf.net/"
202 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
204 #define TPACPI_PROC_DIR "ibm"
205 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
206 #define TPACPI_DRVR_NAME TPACPI_FILE
207 #define TPACPI_DRVR_SHORTNAME "tpacpi"
208 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
210 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
211 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
213 #define TPACPI_MAX_ACPI_ARGS 3
215 /* printk headers */
216 #define TPACPI_LOG TPACPI_FILE ": "
217 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
218 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
219 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
220 #define TPACPI_ERR KERN_ERR TPACPI_LOG
221 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
222 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
223 #define TPACPI_INFO KERN_INFO TPACPI_LOG
224 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
226 /* Debugging printk groups */
227 #define TPACPI_DBG_ALL 0xffff
228 #define TPACPI_DBG_DISCLOSETASK 0x8000
229 #define TPACPI_DBG_INIT 0x0001
230 #define TPACPI_DBG_EXIT 0x0002
231 #define TPACPI_DBG_RFKILL 0x0004
232 #define TPACPI_DBG_HKEY 0x0008
233 #define TPACPI_DBG_FAN 0x0010
234 #define TPACPI_DBG_BRGHT 0x0020
235 #define TPACPI_DBG_MIXER 0x0040
237 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
238 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
239 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
242 /****************************************************************************
243 * Driver-wide structs and misc. variables
246 struct ibm_struct;
248 struct tp_acpi_drv_struct {
249 const struct acpi_device_id *hid;
250 struct acpi_driver *driver;
252 void (*notify) (struct ibm_struct *, u32);
253 acpi_handle *handle;
254 u32 type;
255 struct acpi_device *device;
258 struct ibm_struct {
259 char *name;
261 int (*read) (struct seq_file *);
262 int (*write) (char *);
263 void (*exit) (void);
264 void (*resume) (void);
265 void (*suspend) (pm_message_t state);
266 void (*shutdown) (void);
268 struct list_head all_drivers;
270 struct tp_acpi_drv_struct *acpi;
272 struct {
273 u8 acpi_driver_registered:1;
274 u8 acpi_notify_installed:1;
275 u8 proc_created:1;
276 u8 init_called:1;
277 u8 experimental:1;
278 } flags;
281 struct ibm_init_struct {
282 char param[32];
284 int (*init) (struct ibm_init_struct *);
285 mode_t base_procfs_mode;
286 struct ibm_struct *data;
289 static struct {
290 u32 bluetooth:1;
291 u32 hotkey:1;
292 u32 hotkey_mask:1;
293 u32 hotkey_wlsw:1;
294 u32 hotkey_tablet:1;
295 u32 light:1;
296 u32 light_status:1;
297 u32 bright_16levels:1;
298 u32 bright_acpimode:1;
299 u32 wan:1;
300 u32 uwb:1;
301 u32 fan_ctrl_status_undef:1;
302 u32 second_fan:1;
303 u32 beep_needs_two_args:1;
304 u32 mixer_no_level_control:1;
305 u32 input_device_registered:1;
306 u32 platform_drv_registered:1;
307 u32 platform_drv_attrs_registered:1;
308 u32 sensors_pdrv_registered:1;
309 u32 sensors_pdrv_attrs_registered:1;
310 u32 sensors_pdev_attrs_registered:1;
311 u32 hotkey_poll_active:1;
312 } tp_features;
314 static struct {
315 u16 hotkey_mask_ff:1;
316 } tp_warned;
318 struct thinkpad_id_data {
319 unsigned int vendor; /* ThinkPad vendor:
320 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
322 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
323 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
325 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
326 u16 ec_model;
327 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
328 u16 ec_release;
330 char *model_str; /* ThinkPad T43 */
331 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
333 static struct thinkpad_id_data thinkpad_id;
335 static enum {
336 TPACPI_LIFE_INIT = 0,
337 TPACPI_LIFE_RUNNING,
338 TPACPI_LIFE_EXITING,
339 } tpacpi_lifecycle;
341 static int experimental;
342 static u32 dbg_level;
344 static struct workqueue_struct *tpacpi_wq;
346 enum led_status_t {
347 TPACPI_LED_OFF = 0,
348 TPACPI_LED_ON,
349 TPACPI_LED_BLINK,
352 /* Special LED class that can defer work */
353 struct tpacpi_led_classdev {
354 struct led_classdev led_classdev;
355 struct work_struct work;
356 enum led_status_t new_state;
357 unsigned int led;
360 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
361 static int dbg_wlswemul;
362 static int tpacpi_wlsw_emulstate;
363 static int dbg_bluetoothemul;
364 static int tpacpi_bluetooth_emulstate;
365 static int dbg_wwanemul;
366 static int tpacpi_wwan_emulstate;
367 static int dbg_uwbemul;
368 static int tpacpi_uwb_emulstate;
369 #endif
372 /*************************************************************************
373 * Debugging helpers
376 #define dbg_printk(a_dbg_level, format, arg...) \
377 do { if (dbg_level & (a_dbg_level)) \
378 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
379 } while (0)
381 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
382 #define vdbg_printk dbg_printk
383 static const char *str_supported(int is_supported);
384 #else
385 #define vdbg_printk(a_dbg_level, format, arg...) \
386 do { } while (0)
387 #endif
389 static void tpacpi_log_usertask(const char * const what)
391 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
392 what, task_tgid_vnr(current));
395 #define tpacpi_disclose_usertask(what, format, arg...) \
396 do { \
397 if (unlikely( \
398 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
399 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
400 printk(TPACPI_DEBUG "%s: PID %d: " format, \
401 what, task_tgid_vnr(current), ## arg); \
403 } while (0)
406 * Quirk handling helpers
408 * ThinkPad IDs and versions seen in the field so far
409 * are two-characters from the set [0-9A-Z], i.e. base 36.
411 * We use values well outside that range as specials.
414 #define TPACPI_MATCH_ANY 0xffffU
415 #define TPACPI_MATCH_UNKNOWN 0U
417 /* TPID('1', 'Y') == 0x5931 */
418 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
420 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
421 { .vendor = PCI_VENDOR_ID_IBM, \
422 .bios = TPID(__id1, __id2), \
423 .ec = TPACPI_MATCH_ANY, \
424 .quirks = (__quirk) }
426 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
427 { .vendor = PCI_VENDOR_ID_LENOVO, \
428 .bios = TPID(__id1, __id2), \
429 .ec = TPACPI_MATCH_ANY, \
430 .quirks = (__quirk) }
432 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
433 { .vendor = PCI_VENDOR_ID_LENOVO, \
434 .bios = TPACPI_MATCH_ANY, \
435 .ec = TPID(__id1, __id2), \
436 .quirks = (__quirk) }
438 struct tpacpi_quirk {
439 unsigned int vendor;
440 u16 bios;
441 u16 ec;
442 unsigned long quirks;
446 * tpacpi_check_quirks() - search BIOS/EC version on a list
447 * @qlist: array of &struct tpacpi_quirk
448 * @qlist_size: number of elements in @qlist
450 * Iterates over a quirks list until one is found that matches the
451 * ThinkPad's vendor, BIOS and EC model.
453 * Returns 0 if nothing matches, otherwise returns the quirks field of
454 * the matching &struct tpacpi_quirk entry.
456 * The match criteria is: vendor, ec and bios much match.
458 static unsigned long __init tpacpi_check_quirks(
459 const struct tpacpi_quirk *qlist,
460 unsigned int qlist_size)
462 while (qlist_size) {
463 if ((qlist->vendor == thinkpad_id.vendor ||
464 qlist->vendor == TPACPI_MATCH_ANY) &&
465 (qlist->bios == thinkpad_id.bios_model ||
466 qlist->bios == TPACPI_MATCH_ANY) &&
467 (qlist->ec == thinkpad_id.ec_model ||
468 qlist->ec == TPACPI_MATCH_ANY))
469 return qlist->quirks;
471 qlist_size--;
472 qlist++;
474 return 0;
478 /****************************************************************************
479 ****************************************************************************
481 * ACPI Helpers and device model
483 ****************************************************************************
484 ****************************************************************************/
486 /*************************************************************************
487 * ACPI basic handles
490 static acpi_handle root_handle;
492 #define TPACPI_HANDLE(object, parent, paths...) \
493 static acpi_handle object##_handle; \
494 static acpi_handle *object##_parent = &parent##_handle; \
495 static char *object##_path; \
496 static char *object##_paths[] = { paths }
498 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
499 "\\_SB.PCI.ISA.EC", /* 570 */
500 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
501 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
502 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
503 "\\_SB.PCI0.ICH3.EC0", /* R31 */
504 "\\_SB.PCI0.LPC.EC", /* all others */
507 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
508 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
510 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
511 /* T4x, X31, X40 */
512 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
513 "\\CMS", /* R40, R40e */
514 ); /* all others */
516 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
517 "^HKEY", /* R30, R31 */
518 "HKEY", /* all others */
519 ); /* 570 */
521 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
522 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
523 "\\_SB.PCI0.VID0", /* 770e */
524 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
525 "\\_SB.PCI0.AGP.VID", /* all others */
526 ); /* R30, R31 */
529 /*************************************************************************
530 * ACPI helpers
533 static int acpi_evalf(acpi_handle handle,
534 void *res, char *method, char *fmt, ...)
536 char *fmt0 = fmt;
537 struct acpi_object_list params;
538 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
539 struct acpi_buffer result, *resultp;
540 union acpi_object out_obj;
541 acpi_status status;
542 va_list ap;
543 char res_type;
544 int success;
545 int quiet;
547 if (!*fmt) {
548 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
549 return 0;
552 if (*fmt == 'q') {
553 quiet = 1;
554 fmt++;
555 } else
556 quiet = 0;
558 res_type = *(fmt++);
560 params.count = 0;
561 params.pointer = &in_objs[0];
563 va_start(ap, fmt);
564 while (*fmt) {
565 char c = *(fmt++);
566 switch (c) {
567 case 'd': /* int */
568 in_objs[params.count].integer.value = va_arg(ap, int);
569 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
570 break;
571 /* add more types as needed */
572 default:
573 printk(TPACPI_ERR "acpi_evalf() called "
574 "with invalid format character '%c'\n", c);
575 return 0;
578 va_end(ap);
580 if (res_type != 'v') {
581 result.length = sizeof(out_obj);
582 result.pointer = &out_obj;
583 resultp = &result;
584 } else
585 resultp = NULL;
587 status = acpi_evaluate_object(handle, method, &params, resultp);
589 switch (res_type) {
590 case 'd': /* int */
591 if (res)
592 *(int *)res = out_obj.integer.value;
593 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
594 break;
595 case 'v': /* void */
596 success = status == AE_OK;
597 break;
598 /* add more types as needed */
599 default:
600 printk(TPACPI_ERR "acpi_evalf() called "
601 "with invalid format character '%c'\n", res_type);
602 return 0;
605 if (!success && !quiet)
606 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
607 method, fmt0, status);
609 return success;
612 static int acpi_ec_read(int i, u8 *p)
614 int v;
616 if (ecrd_handle) {
617 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
618 return 0;
619 *p = v;
620 } else {
621 if (ec_read(i, p) < 0)
622 return 0;
625 return 1;
628 static int acpi_ec_write(int i, u8 v)
630 if (ecwr_handle) {
631 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
632 return 0;
633 } else {
634 if (ec_write(i, v) < 0)
635 return 0;
638 return 1;
641 static int issue_thinkpad_cmos_command(int cmos_cmd)
643 if (!cmos_handle)
644 return -ENXIO;
646 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
647 return -EIO;
649 return 0;
652 /*************************************************************************
653 * ACPI device model
656 #define TPACPI_ACPIHANDLE_INIT(object) \
657 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
658 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
660 static void drv_acpi_handle_init(char *name,
661 acpi_handle *handle, acpi_handle parent,
662 char **paths, int num_paths, char **path)
664 int i;
665 acpi_status status;
667 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
668 name);
670 for (i = 0; i < num_paths; i++) {
671 status = acpi_get_handle(parent, paths[i], handle);
672 if (ACPI_SUCCESS(status)) {
673 *path = paths[i];
674 dbg_printk(TPACPI_DBG_INIT,
675 "Found ACPI handle %s for %s\n",
676 *path, name);
677 return;
681 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
682 name);
683 *handle = NULL;
686 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
688 struct ibm_struct *ibm = data;
690 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
691 return;
693 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
694 return;
696 ibm->acpi->notify(ibm, event);
699 static int __init setup_acpi_notify(struct ibm_struct *ibm)
701 acpi_status status;
702 int rc;
704 BUG_ON(!ibm->acpi);
706 if (!*ibm->acpi->handle)
707 return 0;
709 vdbg_printk(TPACPI_DBG_INIT,
710 "setting up ACPI notify for %s\n", ibm->name);
712 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
713 if (rc < 0) {
714 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
715 ibm->name, rc);
716 return -ENODEV;
719 ibm->acpi->device->driver_data = ibm;
720 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
721 TPACPI_ACPI_EVENT_PREFIX,
722 ibm->name);
724 status = acpi_install_notify_handler(*ibm->acpi->handle,
725 ibm->acpi->type, dispatch_acpi_notify, ibm);
726 if (ACPI_FAILURE(status)) {
727 if (status == AE_ALREADY_EXISTS) {
728 printk(TPACPI_NOTICE
729 "another device driver is already "
730 "handling %s events\n", ibm->name);
731 } else {
732 printk(TPACPI_ERR
733 "acpi_install_notify_handler(%s) failed: %d\n",
734 ibm->name, status);
736 return -ENODEV;
738 ibm->flags.acpi_notify_installed = 1;
739 return 0;
742 static int __init tpacpi_device_add(struct acpi_device *device)
744 return 0;
747 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
749 int rc;
751 dbg_printk(TPACPI_DBG_INIT,
752 "registering %s as an ACPI driver\n", ibm->name);
754 BUG_ON(!ibm->acpi);
756 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
757 if (!ibm->acpi->driver) {
758 printk(TPACPI_ERR
759 "failed to allocate memory for ibm->acpi->driver\n");
760 return -ENOMEM;
763 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
764 ibm->acpi->driver->ids = ibm->acpi->hid;
766 ibm->acpi->driver->ops.add = &tpacpi_device_add;
768 rc = acpi_bus_register_driver(ibm->acpi->driver);
769 if (rc < 0) {
770 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
771 ibm->name, rc);
772 kfree(ibm->acpi->driver);
773 ibm->acpi->driver = NULL;
774 } else if (!rc)
775 ibm->flags.acpi_driver_registered = 1;
777 return rc;
781 /****************************************************************************
782 ****************************************************************************
784 * Procfs Helpers
786 ****************************************************************************
787 ****************************************************************************/
789 static int dispatch_proc_show(struct seq_file *m, void *v)
791 struct ibm_struct *ibm = m->private;
793 if (!ibm || !ibm->read)
794 return -EINVAL;
795 return ibm->read(m);
798 static int dispatch_proc_open(struct inode *inode, struct file *file)
800 return single_open(file, dispatch_proc_show, PDE(inode)->data);
803 static ssize_t dispatch_proc_write(struct file *file,
804 const char __user *userbuf,
805 size_t count, loff_t *pos)
807 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
808 char *kernbuf;
809 int ret;
811 if (!ibm || !ibm->write)
812 return -EINVAL;
813 if (count > PAGE_SIZE - 2)
814 return -EINVAL;
816 kernbuf = kmalloc(count + 2, GFP_KERNEL);
817 if (!kernbuf)
818 return -ENOMEM;
820 if (copy_from_user(kernbuf, userbuf, count)) {
821 kfree(kernbuf);
822 return -EFAULT;
825 kernbuf[count] = 0;
826 strcat(kernbuf, ",");
827 ret = ibm->write(kernbuf);
828 if (ret == 0)
829 ret = count;
831 kfree(kernbuf);
833 return ret;
836 static const struct file_operations dispatch_proc_fops = {
837 .owner = THIS_MODULE,
838 .open = dispatch_proc_open,
839 .read = seq_read,
840 .llseek = seq_lseek,
841 .release = single_release,
842 .write = dispatch_proc_write,
845 static char *next_cmd(char **cmds)
847 char *start = *cmds;
848 char *end;
850 while ((end = strchr(start, ',')) && end == start)
851 start = end + 1;
853 if (!end)
854 return NULL;
856 *end = 0;
857 *cmds = end + 1;
858 return start;
862 /****************************************************************************
863 ****************************************************************************
865 * Device model: input, hwmon and platform
867 ****************************************************************************
868 ****************************************************************************/
870 static struct platform_device *tpacpi_pdev;
871 static struct platform_device *tpacpi_sensors_pdev;
872 static struct device *tpacpi_hwmon;
873 static struct input_dev *tpacpi_inputdev;
874 static struct mutex tpacpi_inputdev_send_mutex;
875 static LIST_HEAD(tpacpi_all_drivers);
877 static int tpacpi_suspend_handler(struct platform_device *pdev,
878 pm_message_t state)
880 struct ibm_struct *ibm, *itmp;
882 list_for_each_entry_safe(ibm, itmp,
883 &tpacpi_all_drivers,
884 all_drivers) {
885 if (ibm->suspend)
886 (ibm->suspend)(state);
889 return 0;
892 static int tpacpi_resume_handler(struct platform_device *pdev)
894 struct ibm_struct *ibm, *itmp;
896 list_for_each_entry_safe(ibm, itmp,
897 &tpacpi_all_drivers,
898 all_drivers) {
899 if (ibm->resume)
900 (ibm->resume)();
903 return 0;
906 static void tpacpi_shutdown_handler(struct platform_device *pdev)
908 struct ibm_struct *ibm, *itmp;
910 list_for_each_entry_safe(ibm, itmp,
911 &tpacpi_all_drivers,
912 all_drivers) {
913 if (ibm->shutdown)
914 (ibm->shutdown)();
918 static struct platform_driver tpacpi_pdriver = {
919 .driver = {
920 .name = TPACPI_DRVR_NAME,
921 .owner = THIS_MODULE,
923 .suspend = tpacpi_suspend_handler,
924 .resume = tpacpi_resume_handler,
925 .shutdown = tpacpi_shutdown_handler,
928 static struct platform_driver tpacpi_hwmon_pdriver = {
929 .driver = {
930 .name = TPACPI_HWMON_DRVR_NAME,
931 .owner = THIS_MODULE,
935 /*************************************************************************
936 * sysfs support helpers
939 struct attribute_set {
940 unsigned int members, max_members;
941 struct attribute_group group;
944 struct attribute_set_obj {
945 struct attribute_set s;
946 struct attribute *a;
947 } __attribute__((packed));
949 static struct attribute_set *create_attr_set(unsigned int max_members,
950 const char *name)
952 struct attribute_set_obj *sobj;
954 if (max_members == 0)
955 return NULL;
957 /* Allocates space for implicit NULL at the end too */
958 sobj = kzalloc(sizeof(struct attribute_set_obj) +
959 max_members * sizeof(struct attribute *),
960 GFP_KERNEL);
961 if (!sobj)
962 return NULL;
963 sobj->s.max_members = max_members;
964 sobj->s.group.attrs = &sobj->a;
965 sobj->s.group.name = name;
967 return &sobj->s;
970 #define destroy_attr_set(_set) \
971 kfree(_set);
973 /* not multi-threaded safe, use it in a single thread per set */
974 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
976 if (!s || !attr)
977 return -EINVAL;
979 if (s->members >= s->max_members)
980 return -ENOMEM;
982 s->group.attrs[s->members] = attr;
983 s->members++;
985 return 0;
988 static int add_many_to_attr_set(struct attribute_set *s,
989 struct attribute **attr,
990 unsigned int count)
992 int i, res;
994 for (i = 0; i < count; i++) {
995 res = add_to_attr_set(s, attr[i]);
996 if (res)
997 return res;
1000 return 0;
1003 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1005 sysfs_remove_group(kobj, &s->group);
1006 destroy_attr_set(s);
1009 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1010 sysfs_create_group(_kobj, &_attr_set->group)
1012 static int parse_strtoul(const char *buf,
1013 unsigned long max, unsigned long *value)
1015 char *endp;
1017 while (*buf && isspace(*buf))
1018 buf++;
1019 *value = simple_strtoul(buf, &endp, 0);
1020 while (*endp && isspace(*endp))
1021 endp++;
1022 if (*endp || *value > max)
1023 return -EINVAL;
1025 return 0;
1028 static void tpacpi_disable_brightness_delay(void)
1030 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1031 printk(TPACPI_NOTICE
1032 "ACPI backlight control delay disabled\n");
1035 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1037 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1038 union acpi_object *obj;
1039 int rc;
1041 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1042 obj = (union acpi_object *)buffer.pointer;
1043 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1044 printk(TPACPI_ERR "Unknown _BCL data, "
1045 "please report this to %s\n", TPACPI_MAIL);
1046 rc = 0;
1047 } else {
1048 rc = obj->package.count;
1050 } else {
1051 return 0;
1054 kfree(buffer.pointer);
1055 return rc;
1058 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1059 u32 lvl, void *context, void **rv)
1061 char name[ACPI_PATH_SEGMENT_LENGTH];
1062 struct acpi_buffer buffer = { sizeof(name), &name };
1064 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1065 !strncmp("_BCL", name, sizeof(name) - 1)) {
1066 BUG_ON(!rv || !*rv);
1067 **(int **)rv = tpacpi_query_bcl_levels(handle);
1068 return AE_CTRL_TERMINATE;
1069 } else {
1070 return AE_OK;
1075 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1077 static int __init tpacpi_check_std_acpi_brightness_support(void)
1079 int status;
1080 int bcl_levels = 0;
1081 void *bcl_ptr = &bcl_levels;
1083 if (!vid_handle) {
1084 TPACPI_ACPIHANDLE_INIT(vid);
1086 if (!vid_handle)
1087 return 0;
1090 * Search for a _BCL method, and execute it. This is safe on all
1091 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1092 * BIOS in ACPI backlight control mode. We do NOT have to care
1093 * about calling the _BCL method in an enabled video device, any
1094 * will do for our purposes.
1097 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1098 tpacpi_acpi_walk_find_bcl, NULL,
1099 &bcl_ptr);
1101 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1102 tp_features.bright_acpimode = 1;
1103 return (bcl_levels - 2);
1106 return 0;
1109 static void printk_deprecated_attribute(const char * const what,
1110 const char * const details)
1112 tpacpi_log_usertask("deprecated sysfs attribute");
1113 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1114 "will be removed. %s\n",
1115 what, details);
1118 /*************************************************************************
1119 * rfkill and radio control support helpers
1123 * ThinkPad-ACPI firmware handling model:
1125 * WLSW (master wireless switch) is event-driven, and is common to all
1126 * firmware-controlled radios. It cannot be controlled, just monitored,
1127 * as expected. It overrides all radio state in firmware
1129 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1130 * (TODO: verify how WLSW interacts with the returned radio state).
1132 * The only time there are shadow radio state changes, is when
1133 * masked-off hotkeys are used.
1137 * Internal driver API for radio state:
1139 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1140 * bool: true means radio blocked (off)
1142 enum tpacpi_rfkill_state {
1143 TPACPI_RFK_RADIO_OFF = 0,
1144 TPACPI_RFK_RADIO_ON
1147 /* rfkill switches */
1148 enum tpacpi_rfk_id {
1149 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1150 TPACPI_RFK_WWAN_SW_ID,
1151 TPACPI_RFK_UWB_SW_ID,
1152 TPACPI_RFK_SW_MAX
1155 static const char *tpacpi_rfkill_names[] = {
1156 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1157 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1158 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1159 [TPACPI_RFK_SW_MAX] = NULL
1162 /* ThinkPad-ACPI rfkill subdriver */
1163 struct tpacpi_rfk {
1164 struct rfkill *rfkill;
1165 enum tpacpi_rfk_id id;
1166 const struct tpacpi_rfk_ops *ops;
1169 struct tpacpi_rfk_ops {
1170 /* firmware interface */
1171 int (*get_status)(void);
1172 int (*set_status)(const enum tpacpi_rfkill_state);
1175 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1177 /* Query FW and update rfkill sw state for a given rfkill switch */
1178 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1180 int status;
1182 if (!tp_rfk)
1183 return -ENODEV;
1185 status = (tp_rfk->ops->get_status)();
1186 if (status < 0)
1187 return status;
1189 rfkill_set_sw_state(tp_rfk->rfkill,
1190 (status == TPACPI_RFK_RADIO_OFF));
1192 return status;
1195 /* Query FW and update rfkill sw state for all rfkill switches */
1196 static void tpacpi_rfk_update_swstate_all(void)
1198 unsigned int i;
1200 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1201 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1205 * Sync the HW-blocking state of all rfkill switches,
1206 * do notice it causes the rfkill core to schedule uevents
1208 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1210 unsigned int i;
1211 struct tpacpi_rfk *tp_rfk;
1213 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1214 tp_rfk = tpacpi_rfkill_switches[i];
1215 if (tp_rfk) {
1216 if (rfkill_set_hw_state(tp_rfk->rfkill,
1217 blocked)) {
1218 /* ignore -- we track sw block */
1224 /* Call to get the WLSW state from the firmware */
1225 static int hotkey_get_wlsw(void);
1227 /* Call to query WLSW state and update all rfkill switches */
1228 static bool tpacpi_rfk_check_hwblock_state(void)
1230 int res = hotkey_get_wlsw();
1231 int hw_blocked;
1233 /* When unknown or unsupported, we have to assume it is unblocked */
1234 if (res < 0)
1235 return false;
1237 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1238 tpacpi_rfk_update_hwblock_state(hw_blocked);
1240 return hw_blocked;
1243 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1245 struct tpacpi_rfk *tp_rfk = data;
1246 int res;
1248 dbg_printk(TPACPI_DBG_RFKILL,
1249 "request to change radio state to %s\n",
1250 blocked ? "blocked" : "unblocked");
1252 /* try to set radio state */
1253 res = (tp_rfk->ops->set_status)(blocked ?
1254 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1256 /* and update the rfkill core with whatever the FW really did */
1257 tpacpi_rfk_update_swstate(tp_rfk);
1259 return (res < 0) ? res : 0;
1262 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1263 .set_block = tpacpi_rfk_hook_set_block,
1266 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1267 const struct tpacpi_rfk_ops *tp_rfkops,
1268 const enum rfkill_type rfktype,
1269 const char *name,
1270 const bool set_default)
1272 struct tpacpi_rfk *atp_rfk;
1273 int res;
1274 bool sw_state = false;
1275 bool hw_state;
1276 int sw_status;
1278 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1280 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1281 if (atp_rfk)
1282 atp_rfk->rfkill = rfkill_alloc(name,
1283 &tpacpi_pdev->dev,
1284 rfktype,
1285 &tpacpi_rfk_rfkill_ops,
1286 atp_rfk);
1287 if (!atp_rfk || !atp_rfk->rfkill) {
1288 printk(TPACPI_ERR
1289 "failed to allocate memory for rfkill class\n");
1290 kfree(atp_rfk);
1291 return -ENOMEM;
1294 atp_rfk->id = id;
1295 atp_rfk->ops = tp_rfkops;
1297 sw_status = (tp_rfkops->get_status)();
1298 if (sw_status < 0) {
1299 printk(TPACPI_ERR
1300 "failed to read initial state for %s, error %d\n",
1301 name, sw_status);
1302 } else {
1303 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1304 if (set_default) {
1305 /* try to keep the initial state, since we ask the
1306 * firmware to preserve it across S5 in NVRAM */
1307 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1310 hw_state = tpacpi_rfk_check_hwblock_state();
1311 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1313 res = rfkill_register(atp_rfk->rfkill);
1314 if (res < 0) {
1315 printk(TPACPI_ERR
1316 "failed to register %s rfkill switch: %d\n",
1317 name, res);
1318 rfkill_destroy(atp_rfk->rfkill);
1319 kfree(atp_rfk);
1320 return res;
1323 tpacpi_rfkill_switches[id] = atp_rfk;
1325 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1326 name, (sw_state || hw_state) ? "" : "un");
1327 return 0;
1330 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1332 struct tpacpi_rfk *tp_rfk;
1334 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1336 tp_rfk = tpacpi_rfkill_switches[id];
1337 if (tp_rfk) {
1338 rfkill_unregister(tp_rfk->rfkill);
1339 rfkill_destroy(tp_rfk->rfkill);
1340 tpacpi_rfkill_switches[id] = NULL;
1341 kfree(tp_rfk);
1345 static void printk_deprecated_rfkill_attribute(const char * const what)
1347 printk_deprecated_attribute(what,
1348 "Please switch to generic rfkill before year 2010");
1351 /* sysfs <radio> enable ------------------------------------------------ */
1352 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1353 struct device_attribute *attr,
1354 char *buf)
1356 int status;
1358 printk_deprecated_rfkill_attribute(attr->attr.name);
1360 /* This is in the ABI... */
1361 if (tpacpi_rfk_check_hwblock_state()) {
1362 status = TPACPI_RFK_RADIO_OFF;
1363 } else {
1364 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1365 if (status < 0)
1366 return status;
1369 return snprintf(buf, PAGE_SIZE, "%d\n",
1370 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1373 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1374 struct device_attribute *attr,
1375 const char *buf, size_t count)
1377 unsigned long t;
1378 int res;
1380 printk_deprecated_rfkill_attribute(attr->attr.name);
1382 if (parse_strtoul(buf, 1, &t))
1383 return -EINVAL;
1385 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1387 /* This is in the ABI... */
1388 if (tpacpi_rfk_check_hwblock_state() && !!t)
1389 return -EPERM;
1391 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1392 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1393 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1395 return (res < 0) ? res : count;
1398 /* procfs -------------------------------------------------------------- */
1399 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id,
1400 struct seq_file *m)
1402 if (id >= TPACPI_RFK_SW_MAX)
1403 seq_printf(m, "status:\t\tnot supported\n");
1404 else {
1405 int status;
1407 /* This is in the ABI... */
1408 if (tpacpi_rfk_check_hwblock_state()) {
1409 status = TPACPI_RFK_RADIO_OFF;
1410 } else {
1411 status = tpacpi_rfk_update_swstate(
1412 tpacpi_rfkill_switches[id]);
1413 if (status < 0)
1414 return status;
1417 seq_printf(m, "status:\t\t%s\n",
1418 (status == TPACPI_RFK_RADIO_ON) ?
1419 "enabled" : "disabled");
1420 seq_printf(m, "commands:\tenable, disable\n");
1423 return 0;
1426 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1428 char *cmd;
1429 int status = -1;
1430 int res = 0;
1432 if (id >= TPACPI_RFK_SW_MAX)
1433 return -ENODEV;
1435 while ((cmd = next_cmd(&buf))) {
1436 if (strlencmp(cmd, "enable") == 0)
1437 status = TPACPI_RFK_RADIO_ON;
1438 else if (strlencmp(cmd, "disable") == 0)
1439 status = TPACPI_RFK_RADIO_OFF;
1440 else
1441 return -EINVAL;
1444 if (status != -1) {
1445 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1446 (status == TPACPI_RFK_RADIO_ON) ?
1447 "enable" : "disable",
1448 tpacpi_rfkill_names[id]);
1449 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1450 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1453 return res;
1456 /*************************************************************************
1457 * thinkpad-acpi driver attributes
1460 /* interface_version --------------------------------------------------- */
1461 static ssize_t tpacpi_driver_interface_version_show(
1462 struct device_driver *drv,
1463 char *buf)
1465 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1468 static DRIVER_ATTR(interface_version, S_IRUGO,
1469 tpacpi_driver_interface_version_show, NULL);
1471 /* debug_level --------------------------------------------------------- */
1472 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1473 char *buf)
1475 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1478 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1479 const char *buf, size_t count)
1481 unsigned long t;
1483 if (parse_strtoul(buf, 0xffff, &t))
1484 return -EINVAL;
1486 dbg_level = t;
1488 return count;
1491 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1492 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1494 /* version ------------------------------------------------------------- */
1495 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1496 char *buf)
1498 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1499 TPACPI_DESC, TPACPI_VERSION);
1502 static DRIVER_ATTR(version, S_IRUGO,
1503 tpacpi_driver_version_show, NULL);
1505 /* --------------------------------------------------------------------- */
1507 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1509 /* wlsw_emulstate ------------------------------------------------------ */
1510 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1511 char *buf)
1513 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1516 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1517 const char *buf, size_t count)
1519 unsigned long t;
1521 if (parse_strtoul(buf, 1, &t))
1522 return -EINVAL;
1524 if (tpacpi_wlsw_emulstate != !!t) {
1525 tpacpi_wlsw_emulstate = !!t;
1526 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1529 return count;
1532 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1533 tpacpi_driver_wlsw_emulstate_show,
1534 tpacpi_driver_wlsw_emulstate_store);
1536 /* bluetooth_emulstate ------------------------------------------------- */
1537 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1538 struct device_driver *drv,
1539 char *buf)
1541 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1544 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1545 struct device_driver *drv,
1546 const char *buf, size_t count)
1548 unsigned long t;
1550 if (parse_strtoul(buf, 1, &t))
1551 return -EINVAL;
1553 tpacpi_bluetooth_emulstate = !!t;
1555 return count;
1558 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1559 tpacpi_driver_bluetooth_emulstate_show,
1560 tpacpi_driver_bluetooth_emulstate_store);
1562 /* wwan_emulstate ------------------------------------------------- */
1563 static ssize_t tpacpi_driver_wwan_emulstate_show(
1564 struct device_driver *drv,
1565 char *buf)
1567 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1570 static ssize_t tpacpi_driver_wwan_emulstate_store(
1571 struct device_driver *drv,
1572 const char *buf, size_t count)
1574 unsigned long t;
1576 if (parse_strtoul(buf, 1, &t))
1577 return -EINVAL;
1579 tpacpi_wwan_emulstate = !!t;
1581 return count;
1584 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1585 tpacpi_driver_wwan_emulstate_show,
1586 tpacpi_driver_wwan_emulstate_store);
1588 /* uwb_emulstate ------------------------------------------------- */
1589 static ssize_t tpacpi_driver_uwb_emulstate_show(
1590 struct device_driver *drv,
1591 char *buf)
1593 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1596 static ssize_t tpacpi_driver_uwb_emulstate_store(
1597 struct device_driver *drv,
1598 const char *buf, size_t count)
1600 unsigned long t;
1602 if (parse_strtoul(buf, 1, &t))
1603 return -EINVAL;
1605 tpacpi_uwb_emulstate = !!t;
1607 return count;
1610 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1611 tpacpi_driver_uwb_emulstate_show,
1612 tpacpi_driver_uwb_emulstate_store);
1613 #endif
1615 /* --------------------------------------------------------------------- */
1617 static struct driver_attribute *tpacpi_driver_attributes[] = {
1618 &driver_attr_debug_level, &driver_attr_version,
1619 &driver_attr_interface_version,
1622 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1624 int i, res;
1626 i = 0;
1627 res = 0;
1628 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1629 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1630 i++;
1633 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1634 if (!res && dbg_wlswemul)
1635 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1636 if (!res && dbg_bluetoothemul)
1637 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1638 if (!res && dbg_wwanemul)
1639 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1640 if (!res && dbg_uwbemul)
1641 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1642 #endif
1644 return res;
1647 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1649 int i;
1651 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1652 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1654 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1655 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1656 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1657 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1658 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1659 #endif
1662 /*************************************************************************
1663 * Firmware Data
1667 * Table of recommended minimum BIOS versions
1669 * Reasons for listing:
1670 * 1. Stable BIOS, listed because the unknown ammount of
1671 * bugs and bad ACPI behaviour on older versions
1673 * 2. BIOS or EC fw with known bugs that trigger on Linux
1675 * 3. BIOS with known reduced functionality in older versions
1677 * We recommend the latest BIOS and EC version.
1678 * We only support the latest BIOS and EC fw version as a rule.
1680 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1681 * Information from users in ThinkWiki
1683 * WARNING: we use this table also to detect that the machine is
1684 * a ThinkPad in some cases, so don't remove entries lightly.
1687 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1688 { .vendor = (__v), \
1689 .bios = TPID(__id1, __id2), \
1690 .ec = TPACPI_MATCH_ANY, \
1691 .quirks = TPACPI_MATCH_ANY << 16 \
1692 | (__bv1) << 8 | (__bv2) }
1694 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1695 __eid, __ev1, __ev2) \
1696 { .vendor = (__v), \
1697 .bios = TPID(__bid1, __bid2), \
1698 .ec = __eid, \
1699 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1700 | (__bv1) << 8 | (__bv2) }
1702 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1703 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1705 /* Outdated IBM BIOSes often lack the EC id string */
1706 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1707 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1708 __bv1, __bv2, TPID(__id1, __id2), \
1709 __ev1, __ev2), \
1710 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1711 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1712 __ev1, __ev2)
1714 /* Outdated IBM BIOSes often lack the EC id string */
1715 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1716 __eid1, __eid2, __ev1, __ev2) \
1717 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1718 __bv1, __bv2, TPID(__eid1, __eid2), \
1719 __ev1, __ev2), \
1720 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1721 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1722 __ev1, __ev2)
1724 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1725 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1727 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1728 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1729 __bv1, __bv2, TPID(__id1, __id2), \
1730 __ev1, __ev2)
1732 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1733 __eid1, __eid2, __ev1, __ev2) \
1734 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1735 __bv1, __bv2, TPID(__eid1, __eid2), \
1736 __ev1, __ev2)
1738 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1739 /* Numeric models ------------------ */
1740 /* FW MODEL BIOS VERS */
1741 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1742 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1743 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1744 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1745 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1746 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1747 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1748 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1749 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1751 /* A-series ------------------------- */
1752 /* FW MODEL BIOS VERS EC VERS */
1753 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1754 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1755 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1756 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1757 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1758 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1759 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1760 TPV_QI0('1', '3', '2', '0'), /* A22m */
1761 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1762 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1763 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1765 /* G-series ------------------------- */
1766 /* FW MODEL BIOS VERS */
1767 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1768 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1770 /* R-series, T-series --------------- */
1771 /* FW MODEL BIOS VERS EC VERS */
1772 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1773 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1774 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1775 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1776 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1777 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1778 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1779 T40/p, T41/p, T42/p (1) */
1780 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1781 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1782 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1783 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1785 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1786 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1787 TPV_QI0('1', '6', '3', '2'), /* T22 */
1788 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1789 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1790 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1792 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1793 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1794 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1796 /* BIOS FW BIOS VERS EC FW EC VERS */
1797 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1798 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1800 /* X-series ------------------------- */
1801 /* FW MODEL BIOS VERS EC VERS */
1802 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1803 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1804 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1805 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1806 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1807 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1808 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1810 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1811 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1813 /* (0) - older versions lack DMI EC fw string and functionality */
1814 /* (1) - older versions known to lack functionality */
1817 #undef TPV_QL1
1818 #undef TPV_QL0
1819 #undef TPV_QI2
1820 #undef TPV_QI1
1821 #undef TPV_QI0
1822 #undef TPV_Q_X
1823 #undef TPV_Q
1825 static void __init tpacpi_check_outdated_fw(void)
1827 unsigned long fwvers;
1828 u16 ec_version, bios_version;
1830 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1831 ARRAY_SIZE(tpacpi_bios_version_qtable));
1833 if (!fwvers)
1834 return;
1836 bios_version = fwvers & 0xffffU;
1837 ec_version = (fwvers >> 16) & 0xffffU;
1839 /* note that unknown versions are set to 0x0000 and we use that */
1840 if ((bios_version > thinkpad_id.bios_release) ||
1841 (ec_version > thinkpad_id.ec_release &&
1842 ec_version != TPACPI_MATCH_ANY)) {
1844 * The changelogs would let us track down the exact
1845 * reason, but it is just too much of a pain to track
1846 * it. We only list BIOSes that are either really
1847 * broken, or really stable to begin with, so it is
1848 * best if the user upgrades the firmware anyway.
1850 printk(TPACPI_WARN
1851 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1852 printk(TPACPI_WARN
1853 "WARNING: This firmware may be missing critical bug "
1854 "fixes and/or important features\n");
1858 static bool __init tpacpi_is_fw_known(void)
1860 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1861 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1864 /****************************************************************************
1865 ****************************************************************************
1867 * Subdrivers
1869 ****************************************************************************
1870 ****************************************************************************/
1872 /*************************************************************************
1873 * thinkpad-acpi init subdriver
1876 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1878 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1879 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1881 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1882 (thinkpad_id.bios_version_str) ?
1883 thinkpad_id.bios_version_str : "unknown",
1884 (thinkpad_id.ec_version_str) ?
1885 thinkpad_id.ec_version_str : "unknown");
1887 if (thinkpad_id.vendor && thinkpad_id.model_str)
1888 printk(TPACPI_INFO "%s %s, model %s\n",
1889 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1890 "IBM" : ((thinkpad_id.vendor ==
1891 PCI_VENDOR_ID_LENOVO) ?
1892 "Lenovo" : "Unknown vendor"),
1893 thinkpad_id.model_str,
1894 (thinkpad_id.nummodel_str) ?
1895 thinkpad_id.nummodel_str : "unknown");
1897 tpacpi_check_outdated_fw();
1898 return 0;
1901 static int thinkpad_acpi_driver_read(struct seq_file *m)
1903 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1904 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1905 return 0;
1908 static struct ibm_struct thinkpad_acpi_driver_data = {
1909 .name = "driver",
1910 .read = thinkpad_acpi_driver_read,
1913 /*************************************************************************
1914 * Hotkey subdriver
1918 * ThinkPad firmware event model
1920 * The ThinkPad firmware has two main event interfaces: normal ACPI
1921 * notifications (which follow the ACPI standard), and a private event
1922 * interface.
1924 * The private event interface also issues events for the hotkeys. As
1925 * the driver gained features, the event handling code ended up being
1926 * built around the hotkey subdriver. This will need to be refactored
1927 * to a more formal event API eventually.
1929 * Some "hotkeys" are actually supposed to be used as event reports,
1930 * such as "brightness has changed", "volume has changed", depending on
1931 * the ThinkPad model and how the firmware is operating.
1933 * Unlike other classes, hotkey-class events have mask/unmask control on
1934 * non-ancient firmware. However, how it behaves changes a lot with the
1935 * firmware model and version.
1938 enum { /* hot key scan codes (derived from ACPI DSDT) */
1939 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1940 TP_ACPI_HOTKEYSCAN_FNF2,
1941 TP_ACPI_HOTKEYSCAN_FNF3,
1942 TP_ACPI_HOTKEYSCAN_FNF4,
1943 TP_ACPI_HOTKEYSCAN_FNF5,
1944 TP_ACPI_HOTKEYSCAN_FNF6,
1945 TP_ACPI_HOTKEYSCAN_FNF7,
1946 TP_ACPI_HOTKEYSCAN_FNF8,
1947 TP_ACPI_HOTKEYSCAN_FNF9,
1948 TP_ACPI_HOTKEYSCAN_FNF10,
1949 TP_ACPI_HOTKEYSCAN_FNF11,
1950 TP_ACPI_HOTKEYSCAN_FNF12,
1951 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1952 TP_ACPI_HOTKEYSCAN_FNINSERT,
1953 TP_ACPI_HOTKEYSCAN_FNDELETE,
1954 TP_ACPI_HOTKEYSCAN_FNHOME,
1955 TP_ACPI_HOTKEYSCAN_FNEND,
1956 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1957 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1958 TP_ACPI_HOTKEYSCAN_FNSPACE,
1959 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1960 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1961 TP_ACPI_HOTKEYSCAN_MUTE,
1962 TP_ACPI_HOTKEYSCAN_THINKPAD,
1965 enum { /* Keys/events available through NVRAM polling */
1966 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1967 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1970 enum { /* Positions of some of the keys in hotkey masks */
1971 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1972 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1973 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1974 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1975 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1976 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1977 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1978 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1979 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1980 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1981 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1984 enum { /* NVRAM to ACPI HKEY group map */
1985 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1986 TP_ACPI_HKEY_ZOOM_MASK |
1987 TP_ACPI_HKEY_DISPSWTCH_MASK |
1988 TP_ACPI_HKEY_HIBERNATE_MASK,
1989 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1990 TP_ACPI_HKEY_BRGHTDWN_MASK,
1991 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1992 TP_ACPI_HKEY_VOLDWN_MASK |
1993 TP_ACPI_HKEY_MUTE_MASK,
1996 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1997 struct tp_nvram_state {
1998 u16 thinkpad_toggle:1;
1999 u16 zoom_toggle:1;
2000 u16 display_toggle:1;
2001 u16 thinklight_toggle:1;
2002 u16 hibernate_toggle:1;
2003 u16 displayexp_toggle:1;
2004 u16 display_state:1;
2005 u16 brightness_toggle:1;
2006 u16 volume_toggle:1;
2007 u16 mute:1;
2009 u8 brightness_level;
2010 u8 volume_level;
2013 /* kthread for the hotkey poller */
2014 static struct task_struct *tpacpi_hotkey_task;
2016 /* Acquired while the poller kthread is running, use to sync start/stop */
2017 static struct mutex hotkey_thread_mutex;
2020 * Acquire mutex to write poller control variables as an
2021 * atomic block.
2023 * Increment hotkey_config_change when changing them if you
2024 * want the kthread to forget old state.
2026 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2028 static struct mutex hotkey_thread_data_mutex;
2029 static unsigned int hotkey_config_change;
2032 * hotkey poller control variables
2034 * Must be atomic or readers will also need to acquire mutex
2036 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2037 * should be used only when the changes need to be taken as
2038 * a block, OR when one needs to force the kthread to forget
2039 * old state.
2041 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2042 static unsigned int hotkey_poll_freq = 10; /* Hz */
2044 #define HOTKEY_CONFIG_CRITICAL_START \
2045 do { \
2046 mutex_lock(&hotkey_thread_data_mutex); \
2047 hotkey_config_change++; \
2048 } while (0);
2049 #define HOTKEY_CONFIG_CRITICAL_END \
2050 mutex_unlock(&hotkey_thread_data_mutex);
2052 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2054 #define hotkey_source_mask 0U
2055 #define HOTKEY_CONFIG_CRITICAL_START
2056 #define HOTKEY_CONFIG_CRITICAL_END
2058 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2060 static struct mutex hotkey_mutex;
2062 static enum { /* Reasons for waking up */
2063 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2064 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2065 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2066 } hotkey_wakeup_reason;
2068 static int hotkey_autosleep_ack;
2070 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2071 static u32 hotkey_all_mask; /* all events supported in fw */
2072 static u32 hotkey_reserved_mask; /* events better left disabled */
2073 static u32 hotkey_driver_mask; /* events needed by the driver */
2074 static u32 hotkey_user_mask; /* events visible to userspace */
2075 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2077 static unsigned int hotkey_report_mode;
2079 static u16 *hotkey_keycode_map;
2081 static struct attribute_set *hotkey_dev_attributes;
2083 static void tpacpi_driver_event(const unsigned int hkey_event);
2084 static void hotkey_driver_event(const unsigned int scancode);
2085 static void hotkey_poll_setup(const bool may_warn);
2087 /* HKEY.MHKG() return bits */
2088 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2090 static int hotkey_get_wlsw(void)
2092 int status;
2094 if (!tp_features.hotkey_wlsw)
2095 return -ENODEV;
2097 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2098 if (dbg_wlswemul)
2099 return (tpacpi_wlsw_emulstate) ?
2100 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2101 #endif
2103 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2104 return -EIO;
2106 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2109 static int hotkey_get_tablet_mode(int *status)
2111 int s;
2113 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2114 return -EIO;
2116 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2117 return 0;
2121 * Reads current event mask from firmware, and updates
2122 * hotkey_acpi_mask accordingly. Also resets any bits
2123 * from hotkey_user_mask that are unavailable to be
2124 * delivered (shadow requirement of the userspace ABI).
2126 * Call with hotkey_mutex held
2128 static int hotkey_mask_get(void)
2130 if (tp_features.hotkey_mask) {
2131 u32 m = 0;
2133 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2134 return -EIO;
2136 hotkey_acpi_mask = m;
2137 } else {
2138 /* no mask support doesn't mean no event support... */
2139 hotkey_acpi_mask = hotkey_all_mask;
2142 /* sync userspace-visible mask */
2143 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2145 return 0;
2148 void static hotkey_mask_warn_incomplete_mask(void)
2150 /* log only what the user can fix... */
2151 const u32 wantedmask = hotkey_driver_mask &
2152 ~(hotkey_acpi_mask | hotkey_source_mask) &
2153 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2155 if (wantedmask)
2156 printk(TPACPI_NOTICE
2157 "required events 0x%08x not enabled!\n",
2158 wantedmask);
2162 * Set the firmware mask when supported
2164 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2166 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2168 * Call with hotkey_mutex held
2170 static int hotkey_mask_set(u32 mask)
2172 int i;
2173 int rc = 0;
2175 const u32 fwmask = mask & ~hotkey_source_mask;
2177 if (tp_features.hotkey_mask) {
2178 for (i = 0; i < 32; i++) {
2179 if (!acpi_evalf(hkey_handle,
2180 NULL, "MHKM", "vdd", i + 1,
2181 !!(mask & (1 << i)))) {
2182 rc = -EIO;
2183 break;
2189 * We *must* make an inconditional call to hotkey_mask_get to
2190 * refresh hotkey_acpi_mask and update hotkey_user_mask
2192 * Take the opportunity to also log when we cannot _enable_
2193 * a given event.
2195 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2196 printk(TPACPI_NOTICE
2197 "asked for hotkey mask 0x%08x, but "
2198 "firmware forced it to 0x%08x\n",
2199 fwmask, hotkey_acpi_mask);
2202 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2203 hotkey_mask_warn_incomplete_mask();
2205 return rc;
2209 * Sets hotkey_user_mask and tries to set the firmware mask
2211 * Call with hotkey_mutex held
2213 static int hotkey_user_mask_set(const u32 mask)
2215 int rc;
2217 /* Give people a chance to notice they are doing something that
2218 * is bound to go boom on their users sooner or later */
2219 if (!tp_warned.hotkey_mask_ff &&
2220 (mask == 0xffff || mask == 0xffffff ||
2221 mask == 0xffffffff)) {
2222 tp_warned.hotkey_mask_ff = 1;
2223 printk(TPACPI_NOTICE
2224 "setting the hotkey mask to 0x%08x is likely "
2225 "not the best way to go about it\n", mask);
2226 printk(TPACPI_NOTICE
2227 "please consider using the driver defaults, "
2228 "and refer to up-to-date thinkpad-acpi "
2229 "documentation\n");
2232 /* Try to enable what the user asked for, plus whatever we need.
2233 * this syncs everything but won't enable bits in hotkey_user_mask */
2234 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2236 /* Enable the available bits in hotkey_user_mask */
2237 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2239 return rc;
2243 * Sets the driver hotkey mask.
2245 * Can be called even if the hotkey subdriver is inactive
2247 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2249 int rc;
2251 /* Do the right thing if hotkey_init has not been called yet */
2252 if (!tp_features.hotkey) {
2253 hotkey_driver_mask = mask;
2254 return 0;
2257 mutex_lock(&hotkey_mutex);
2259 HOTKEY_CONFIG_CRITICAL_START
2260 hotkey_driver_mask = mask;
2261 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2262 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2263 #endif
2264 HOTKEY_CONFIG_CRITICAL_END
2266 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2267 ~hotkey_source_mask);
2268 hotkey_poll_setup(true);
2270 mutex_unlock(&hotkey_mutex);
2272 return rc;
2275 static int hotkey_status_get(int *status)
2277 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2278 return -EIO;
2280 return 0;
2283 static int hotkey_status_set(bool enable)
2285 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2286 return -EIO;
2288 return 0;
2291 static void tpacpi_input_send_tabletsw(void)
2293 int state;
2295 if (tp_features.hotkey_tablet &&
2296 !hotkey_get_tablet_mode(&state)) {
2297 mutex_lock(&tpacpi_inputdev_send_mutex);
2299 input_report_switch(tpacpi_inputdev,
2300 SW_TABLET_MODE, !!state);
2301 input_sync(tpacpi_inputdev);
2303 mutex_unlock(&tpacpi_inputdev_send_mutex);
2307 /* Do NOT call without validating scancode first */
2308 static void tpacpi_input_send_key(const unsigned int scancode)
2310 const unsigned int keycode = hotkey_keycode_map[scancode];
2312 if (keycode != KEY_RESERVED) {
2313 mutex_lock(&tpacpi_inputdev_send_mutex);
2315 input_report_key(tpacpi_inputdev, keycode, 1);
2316 if (keycode == KEY_UNKNOWN)
2317 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2318 scancode);
2319 input_sync(tpacpi_inputdev);
2321 input_report_key(tpacpi_inputdev, keycode, 0);
2322 if (keycode == KEY_UNKNOWN)
2323 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2324 scancode);
2325 input_sync(tpacpi_inputdev);
2327 mutex_unlock(&tpacpi_inputdev_send_mutex);
2331 /* Do NOT call without validating scancode first */
2332 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2334 hotkey_driver_event(scancode);
2335 if (hotkey_user_mask & (1 << scancode))
2336 tpacpi_input_send_key(scancode);
2339 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2340 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2342 /* Do NOT call without validating scancode first */
2343 static void tpacpi_hotkey_send_key(unsigned int scancode)
2345 tpacpi_input_send_key_masked(scancode);
2346 if (hotkey_report_mode < 2) {
2347 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2348 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2352 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2354 u8 d;
2356 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2357 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2358 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2359 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2360 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2361 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2363 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2364 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2365 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2367 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2368 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2369 n->displayexp_toggle =
2370 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2372 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2373 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2374 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2375 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2376 n->brightness_toggle =
2377 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2379 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2380 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2381 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2382 >> TP_NVRAM_POS_LEVEL_VOLUME;
2383 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2384 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2388 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2389 struct tp_nvram_state *newn,
2390 const u32 event_mask)
2393 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2394 do { \
2395 if ((event_mask & (1 << __scancode)) && \
2396 oldn->__member != newn->__member) \
2397 tpacpi_hotkey_send_key(__scancode); \
2398 } while (0)
2400 #define TPACPI_MAY_SEND_KEY(__scancode) \
2401 do { \
2402 if (event_mask & (1 << __scancode)) \
2403 tpacpi_hotkey_send_key(__scancode); \
2404 } while (0)
2406 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2407 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2408 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2409 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2411 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2413 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2415 /* handle volume */
2416 if (oldn->volume_toggle != newn->volume_toggle) {
2417 if (oldn->mute != newn->mute) {
2418 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2420 if (oldn->volume_level > newn->volume_level) {
2421 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2422 } else if (oldn->volume_level < newn->volume_level) {
2423 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2424 } else if (oldn->mute == newn->mute) {
2425 /* repeated key presses that didn't change state */
2426 if (newn->mute) {
2427 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2428 } else if (newn->volume_level != 0) {
2429 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2430 } else {
2431 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2436 /* handle brightness */
2437 if (oldn->brightness_toggle != newn->brightness_toggle) {
2438 if (oldn->brightness_level < newn->brightness_level) {
2439 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2440 } else if (oldn->brightness_level > newn->brightness_level) {
2441 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2442 } else {
2443 /* repeated key presses that didn't change state */
2444 if (newn->brightness_level != 0) {
2445 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2446 } else {
2447 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2452 #undef TPACPI_COMPARE_KEY
2453 #undef TPACPI_MAY_SEND_KEY
2457 * Polling driver
2459 * We track all events in hotkey_source_mask all the time, since
2460 * most of them are edge-based. We only issue those requested by
2461 * hotkey_user_mask or hotkey_driver_mask, though.
2463 static int hotkey_kthread(void *data)
2465 struct tp_nvram_state s[2];
2466 u32 poll_mask, event_mask;
2467 unsigned int si, so;
2468 unsigned long t;
2469 unsigned int change_detector, must_reset;
2470 unsigned int poll_freq;
2472 mutex_lock(&hotkey_thread_mutex);
2474 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2475 goto exit;
2477 set_freezable();
2479 so = 0;
2480 si = 1;
2481 t = 0;
2483 /* Initial state for compares */
2484 mutex_lock(&hotkey_thread_data_mutex);
2485 change_detector = hotkey_config_change;
2486 poll_mask = hotkey_source_mask;
2487 event_mask = hotkey_source_mask &
2488 (hotkey_driver_mask | hotkey_user_mask);
2489 poll_freq = hotkey_poll_freq;
2490 mutex_unlock(&hotkey_thread_data_mutex);
2491 hotkey_read_nvram(&s[so], poll_mask);
2493 while (!kthread_should_stop()) {
2494 if (t == 0) {
2495 if (likely(poll_freq))
2496 t = 1000/poll_freq;
2497 else
2498 t = 100; /* should never happen... */
2500 t = msleep_interruptible(t);
2501 if (unlikely(kthread_should_stop()))
2502 break;
2503 must_reset = try_to_freeze();
2504 if (t > 0 && !must_reset)
2505 continue;
2507 mutex_lock(&hotkey_thread_data_mutex);
2508 if (must_reset || hotkey_config_change != change_detector) {
2509 /* forget old state on thaw or config change */
2510 si = so;
2511 t = 0;
2512 change_detector = hotkey_config_change;
2514 poll_mask = hotkey_source_mask;
2515 event_mask = hotkey_source_mask &
2516 (hotkey_driver_mask | hotkey_user_mask);
2517 poll_freq = hotkey_poll_freq;
2518 mutex_unlock(&hotkey_thread_data_mutex);
2520 if (likely(poll_mask)) {
2521 hotkey_read_nvram(&s[si], poll_mask);
2522 if (likely(si != so)) {
2523 hotkey_compare_and_issue_event(&s[so], &s[si],
2524 event_mask);
2528 so = si;
2529 si ^= 1;
2532 exit:
2533 mutex_unlock(&hotkey_thread_mutex);
2534 return 0;
2537 /* call with hotkey_mutex held */
2538 static void hotkey_poll_stop_sync(void)
2540 if (tpacpi_hotkey_task) {
2541 if (frozen(tpacpi_hotkey_task) ||
2542 freezing(tpacpi_hotkey_task))
2543 thaw_process(tpacpi_hotkey_task);
2545 kthread_stop(tpacpi_hotkey_task);
2546 tpacpi_hotkey_task = NULL;
2547 mutex_lock(&hotkey_thread_mutex);
2548 /* at this point, the thread did exit */
2549 mutex_unlock(&hotkey_thread_mutex);
2553 /* call with hotkey_mutex held */
2554 static void hotkey_poll_setup(const bool may_warn)
2556 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2557 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2559 if (hotkey_poll_freq > 0 &&
2560 (poll_driver_mask ||
2561 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2562 if (!tpacpi_hotkey_task) {
2563 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2564 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2565 if (IS_ERR(tpacpi_hotkey_task)) {
2566 tpacpi_hotkey_task = NULL;
2567 printk(TPACPI_ERR
2568 "could not create kernel thread "
2569 "for hotkey polling\n");
2572 } else {
2573 hotkey_poll_stop_sync();
2574 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2575 hotkey_poll_freq == 0) {
2576 printk(TPACPI_NOTICE
2577 "hot keys 0x%08x and/or events 0x%08x "
2578 "require polling, which is currently "
2579 "disabled\n",
2580 poll_user_mask, poll_driver_mask);
2585 static void hotkey_poll_setup_safe(const bool may_warn)
2587 mutex_lock(&hotkey_mutex);
2588 hotkey_poll_setup(may_warn);
2589 mutex_unlock(&hotkey_mutex);
2592 /* call with hotkey_mutex held */
2593 static void hotkey_poll_set_freq(unsigned int freq)
2595 if (!freq)
2596 hotkey_poll_stop_sync();
2598 hotkey_poll_freq = freq;
2601 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2603 static void hotkey_poll_setup(const bool __unused)
2607 static void hotkey_poll_setup_safe(const bool __unused)
2611 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2613 static int hotkey_inputdev_open(struct input_dev *dev)
2615 switch (tpacpi_lifecycle) {
2616 case TPACPI_LIFE_INIT:
2617 case TPACPI_LIFE_RUNNING:
2618 hotkey_poll_setup_safe(false);
2619 return 0;
2620 case TPACPI_LIFE_EXITING:
2621 return -EBUSY;
2624 /* Should only happen if tpacpi_lifecycle is corrupt */
2625 BUG();
2626 return -EBUSY;
2629 static void hotkey_inputdev_close(struct input_dev *dev)
2631 /* disable hotkey polling when possible */
2632 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2633 !(hotkey_source_mask & hotkey_driver_mask))
2634 hotkey_poll_setup_safe(false);
2637 /* sysfs hotkey enable ------------------------------------------------- */
2638 static ssize_t hotkey_enable_show(struct device *dev,
2639 struct device_attribute *attr,
2640 char *buf)
2642 int res, status;
2644 printk_deprecated_attribute("hotkey_enable",
2645 "Hotkey reporting is always enabled");
2647 res = hotkey_status_get(&status);
2648 if (res)
2649 return res;
2651 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2654 static ssize_t hotkey_enable_store(struct device *dev,
2655 struct device_attribute *attr,
2656 const char *buf, size_t count)
2658 unsigned long t;
2660 printk_deprecated_attribute("hotkey_enable",
2661 "Hotkeys can be disabled through hotkey_mask");
2663 if (parse_strtoul(buf, 1, &t))
2664 return -EINVAL;
2666 if (t == 0)
2667 return -EPERM;
2669 return count;
2672 static struct device_attribute dev_attr_hotkey_enable =
2673 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2674 hotkey_enable_show, hotkey_enable_store);
2676 /* sysfs hotkey mask --------------------------------------------------- */
2677 static ssize_t hotkey_mask_show(struct device *dev,
2678 struct device_attribute *attr,
2679 char *buf)
2681 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2684 static ssize_t hotkey_mask_store(struct device *dev,
2685 struct device_attribute *attr,
2686 const char *buf, size_t count)
2688 unsigned long t;
2689 int res;
2691 if (parse_strtoul(buf, 0xffffffffUL, &t))
2692 return -EINVAL;
2694 if (mutex_lock_killable(&hotkey_mutex))
2695 return -ERESTARTSYS;
2697 res = hotkey_user_mask_set(t);
2699 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2700 hotkey_poll_setup(true);
2701 #endif
2703 mutex_unlock(&hotkey_mutex);
2705 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2707 return (res) ? res : count;
2710 static struct device_attribute dev_attr_hotkey_mask =
2711 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2712 hotkey_mask_show, hotkey_mask_store);
2714 /* sysfs hotkey bios_enabled ------------------------------------------- */
2715 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2716 struct device_attribute *attr,
2717 char *buf)
2719 return sprintf(buf, "0\n");
2722 static struct device_attribute dev_attr_hotkey_bios_enabled =
2723 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2725 /* sysfs hotkey bios_mask ---------------------------------------------- */
2726 static ssize_t hotkey_bios_mask_show(struct device *dev,
2727 struct device_attribute *attr,
2728 char *buf)
2730 printk_deprecated_attribute("hotkey_bios_mask",
2731 "This attribute is useless.");
2732 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2735 static struct device_attribute dev_attr_hotkey_bios_mask =
2736 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2738 /* sysfs hotkey all_mask ----------------------------------------------- */
2739 static ssize_t hotkey_all_mask_show(struct device *dev,
2740 struct device_attribute *attr,
2741 char *buf)
2743 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2744 hotkey_all_mask | hotkey_source_mask);
2747 static struct device_attribute dev_attr_hotkey_all_mask =
2748 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2750 /* sysfs hotkey recommended_mask --------------------------------------- */
2751 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2752 struct device_attribute *attr,
2753 char *buf)
2755 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2756 (hotkey_all_mask | hotkey_source_mask)
2757 & ~hotkey_reserved_mask);
2760 static struct device_attribute dev_attr_hotkey_recommended_mask =
2761 __ATTR(hotkey_recommended_mask, S_IRUGO,
2762 hotkey_recommended_mask_show, NULL);
2764 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2766 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2767 static ssize_t hotkey_source_mask_show(struct device *dev,
2768 struct device_attribute *attr,
2769 char *buf)
2771 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2774 static ssize_t hotkey_source_mask_store(struct device *dev,
2775 struct device_attribute *attr,
2776 const char *buf, size_t count)
2778 unsigned long t;
2779 u32 r_ev;
2780 int rc;
2782 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2783 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2784 return -EINVAL;
2786 if (mutex_lock_killable(&hotkey_mutex))
2787 return -ERESTARTSYS;
2789 HOTKEY_CONFIG_CRITICAL_START
2790 hotkey_source_mask = t;
2791 HOTKEY_CONFIG_CRITICAL_END
2793 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2794 ~hotkey_source_mask);
2795 hotkey_poll_setup(true);
2797 /* check if events needed by the driver got disabled */
2798 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2799 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2801 mutex_unlock(&hotkey_mutex);
2803 if (rc < 0)
2804 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2805 "firmware event mask!\n");
2807 if (r_ev)
2808 printk(TPACPI_NOTICE "hotkey_source_mask: "
2809 "some important events were disabled: "
2810 "0x%04x\n", r_ev);
2812 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2814 return (rc < 0) ? rc : count;
2817 static struct device_attribute dev_attr_hotkey_source_mask =
2818 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2819 hotkey_source_mask_show, hotkey_source_mask_store);
2821 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2822 static ssize_t hotkey_poll_freq_show(struct device *dev,
2823 struct device_attribute *attr,
2824 char *buf)
2826 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2829 static ssize_t hotkey_poll_freq_store(struct device *dev,
2830 struct device_attribute *attr,
2831 const char *buf, size_t count)
2833 unsigned long t;
2835 if (parse_strtoul(buf, 25, &t))
2836 return -EINVAL;
2838 if (mutex_lock_killable(&hotkey_mutex))
2839 return -ERESTARTSYS;
2841 hotkey_poll_set_freq(t);
2842 hotkey_poll_setup(true);
2844 mutex_unlock(&hotkey_mutex);
2846 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2848 return count;
2851 static struct device_attribute dev_attr_hotkey_poll_freq =
2852 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2853 hotkey_poll_freq_show, hotkey_poll_freq_store);
2855 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2857 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2858 static ssize_t hotkey_radio_sw_show(struct device *dev,
2859 struct device_attribute *attr,
2860 char *buf)
2862 int res;
2863 res = hotkey_get_wlsw();
2864 if (res < 0)
2865 return res;
2867 /* Opportunistic update */
2868 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2870 return snprintf(buf, PAGE_SIZE, "%d\n",
2871 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2874 static struct device_attribute dev_attr_hotkey_radio_sw =
2875 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2877 static void hotkey_radio_sw_notify_change(void)
2879 if (tp_features.hotkey_wlsw)
2880 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2881 "hotkey_radio_sw");
2884 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2885 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2886 struct device_attribute *attr,
2887 char *buf)
2889 int res, s;
2890 res = hotkey_get_tablet_mode(&s);
2891 if (res < 0)
2892 return res;
2894 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2897 static struct device_attribute dev_attr_hotkey_tablet_mode =
2898 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2900 static void hotkey_tablet_mode_notify_change(void)
2902 if (tp_features.hotkey_tablet)
2903 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2904 "hotkey_tablet_mode");
2907 /* sysfs hotkey report_mode -------------------------------------------- */
2908 static ssize_t hotkey_report_mode_show(struct device *dev,
2909 struct device_attribute *attr,
2910 char *buf)
2912 return snprintf(buf, PAGE_SIZE, "%d\n",
2913 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2916 static struct device_attribute dev_attr_hotkey_report_mode =
2917 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2919 /* sysfs wakeup reason (pollable) -------------------------------------- */
2920 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2921 struct device_attribute *attr,
2922 char *buf)
2924 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2927 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2928 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2930 static void hotkey_wakeup_reason_notify_change(void)
2932 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2933 "wakeup_reason");
2936 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2937 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2938 struct device_attribute *attr,
2939 char *buf)
2941 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2944 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2945 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2946 hotkey_wakeup_hotunplug_complete_show, NULL);
2948 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2950 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2951 "wakeup_hotunplug_complete");
2954 /* --------------------------------------------------------------------- */
2956 static struct attribute *hotkey_attributes[] __initdata = {
2957 &dev_attr_hotkey_enable.attr,
2958 &dev_attr_hotkey_bios_enabled.attr,
2959 &dev_attr_hotkey_bios_mask.attr,
2960 &dev_attr_hotkey_report_mode.attr,
2961 &dev_attr_hotkey_wakeup_reason.attr,
2962 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2963 &dev_attr_hotkey_mask.attr,
2964 &dev_attr_hotkey_all_mask.attr,
2965 &dev_attr_hotkey_recommended_mask.attr,
2966 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2967 &dev_attr_hotkey_source_mask.attr,
2968 &dev_attr_hotkey_poll_freq.attr,
2969 #endif
2973 * Sync both the hw and sw blocking state of all switches
2975 static void tpacpi_send_radiosw_update(void)
2977 int wlsw;
2980 * We must sync all rfkill controllers *before* issuing any
2981 * rfkill input events, or we will race the rfkill core input
2982 * handler.
2984 * tpacpi_inputdev_send_mutex works as a syncronization point
2985 * for the above.
2987 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2990 wlsw = hotkey_get_wlsw();
2992 /* Sync hw blocking state first if it is hw-blocked */
2993 if (wlsw == TPACPI_RFK_RADIO_OFF)
2994 tpacpi_rfk_update_hwblock_state(true);
2996 /* Sync sw blocking state */
2997 tpacpi_rfk_update_swstate_all();
2999 /* Sync hw blocking state last if it is hw-unblocked */
3000 if (wlsw == TPACPI_RFK_RADIO_ON)
3001 tpacpi_rfk_update_hwblock_state(false);
3003 /* Issue rfkill input event for WLSW switch */
3004 if (!(wlsw < 0)) {
3005 mutex_lock(&tpacpi_inputdev_send_mutex);
3007 input_report_switch(tpacpi_inputdev,
3008 SW_RFKILL_ALL, (wlsw > 0));
3009 input_sync(tpacpi_inputdev);
3011 mutex_unlock(&tpacpi_inputdev_send_mutex);
3015 * this can be unconditional, as we will poll state again
3016 * if userspace uses the notify to read data
3018 hotkey_radio_sw_notify_change();
3021 static void hotkey_exit(void)
3023 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3024 mutex_lock(&hotkey_mutex);
3025 hotkey_poll_stop_sync();
3026 mutex_unlock(&hotkey_mutex);
3027 #endif
3029 if (hotkey_dev_attributes)
3030 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3032 kfree(hotkey_keycode_map);
3034 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3035 "restoring original HKEY status and mask\n");
3036 /* yes, there is a bitwise or below, we want the
3037 * functions to be called even if one of them fail */
3038 if (((tp_features.hotkey_mask &&
3039 hotkey_mask_set(hotkey_orig_mask)) |
3040 hotkey_status_set(false)) != 0)
3041 printk(TPACPI_ERR
3042 "failed to restore hot key mask "
3043 "to BIOS defaults\n");
3046 static void __init hotkey_unmap(const unsigned int scancode)
3048 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3049 clear_bit(hotkey_keycode_map[scancode],
3050 tpacpi_inputdev->keybit);
3051 hotkey_keycode_map[scancode] = KEY_RESERVED;
3056 * HKEY quirks:
3057 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3060 #define TPACPI_HK_Q_INIMASK 0x0001
3062 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3063 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3064 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3065 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3066 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3067 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3068 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3069 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3070 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3071 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3072 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3073 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3074 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3075 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3076 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3077 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3078 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3079 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3080 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3081 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3084 static int __init hotkey_init(struct ibm_init_struct *iibm)
3086 /* Requirements for changing the default keymaps:
3088 * 1. Many of the keys are mapped to KEY_RESERVED for very
3089 * good reasons. Do not change them unless you have deep
3090 * knowledge on the IBM and Lenovo ThinkPad firmware for
3091 * the various ThinkPad models. The driver behaves
3092 * differently for KEY_RESERVED: such keys have their
3093 * hot key mask *unset* in mask_recommended, and also
3094 * in the initial hot key mask programmed into the
3095 * firmware at driver load time, which means the firm-
3096 * ware may react very differently if you change them to
3097 * something else;
3099 * 2. You must be subscribed to the linux-thinkpad and
3100 * ibm-acpi-devel mailing lists, and you should read the
3101 * list archives since 2007 if you want to change the
3102 * keymaps. This requirement exists so that you will
3103 * know the past history of problems with the thinkpad-
3104 * acpi driver keymaps, and also that you will be
3105 * listening to any bug reports;
3107 * 3. Do not send thinkpad-acpi specific patches directly to
3108 * for merging, *ever*. Send them to the linux-acpi
3109 * mailinglist for comments. Merging is to be done only
3110 * through acpi-test and the ACPI maintainer.
3112 * If the above is too much to ask, don't change the keymap.
3113 * Ask the thinkpad-acpi maintainer to do it, instead.
3115 static u16 ibm_keycode_map[] __initdata = {
3116 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3117 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3118 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3119 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3121 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3122 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3123 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3124 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3126 /* brightness: firmware always reacts to them */
3127 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3128 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3130 /* Thinklight: firmware always react to it */
3131 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3133 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3134 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3136 /* Volume: firmware always react to it and reprograms
3137 * the built-in *extra* mixer. Never map it to control
3138 * another mixer by default. */
3139 KEY_RESERVED, /* 0x14: VOLUME UP */
3140 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3141 KEY_RESERVED, /* 0x16: MUTE */
3143 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3145 /* (assignments unknown, please report if found) */
3146 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3147 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3149 static u16 lenovo_keycode_map[] __initdata = {
3150 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3151 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3152 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3153 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3155 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3156 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3157 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3158 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3160 /* These should be enabled --only-- when ACPI video
3161 * is disabled (i.e. in "vendor" mode), and are handled
3162 * in a special way by the init code */
3163 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3164 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3166 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3168 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3169 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3171 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3172 * react to it and reprograms the built-in *extra* mixer.
3173 * Never map it to control another mixer by default.
3175 * T60?, T61, R60?, R61: firmware and EC tries to send
3176 * these over the regular keyboard, so these are no-ops,
3177 * but there are still weird bugs re. MUTE, so do not
3178 * change unless you get test reports from all Lenovo
3179 * models. May cause the BIOS to interfere with the
3180 * HDA mixer.
3182 KEY_RESERVED, /* 0x14: VOLUME UP */
3183 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3184 KEY_RESERVED, /* 0x16: MUTE */
3186 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3188 /* (assignments unknown, please report if found) */
3189 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3190 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3193 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3194 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3195 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3197 int res, i;
3198 int status;
3199 int hkeyv;
3200 bool radiosw_state = false;
3201 bool tabletsw_state = false;
3203 unsigned long quirks;
3205 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3206 "initializing hotkey subdriver\n");
3208 BUG_ON(!tpacpi_inputdev);
3209 BUG_ON(tpacpi_inputdev->open != NULL ||
3210 tpacpi_inputdev->close != NULL);
3212 TPACPI_ACPIHANDLE_INIT(hkey);
3213 mutex_init(&hotkey_mutex);
3215 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3216 mutex_init(&hotkey_thread_mutex);
3217 mutex_init(&hotkey_thread_data_mutex);
3218 #endif
3220 /* hotkey not supported on 570 */
3221 tp_features.hotkey = hkey_handle != NULL;
3223 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3224 "hotkeys are %s\n",
3225 str_supported(tp_features.hotkey));
3227 if (!tp_features.hotkey)
3228 return 1;
3230 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3231 ARRAY_SIZE(tpacpi_hotkey_qtable));
3233 tpacpi_disable_brightness_delay();
3235 /* MUST have enough space for all attributes to be added to
3236 * hotkey_dev_attributes */
3237 hotkey_dev_attributes = create_attr_set(
3238 ARRAY_SIZE(hotkey_attributes) + 2,
3239 NULL);
3240 if (!hotkey_dev_attributes)
3241 return -ENOMEM;
3242 res = add_many_to_attr_set(hotkey_dev_attributes,
3243 hotkey_attributes,
3244 ARRAY_SIZE(hotkey_attributes));
3245 if (res)
3246 goto err_exit;
3248 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3249 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3250 for HKEY interface version 0x100 */
3251 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3252 if ((hkeyv >> 8) != 1) {
3253 printk(TPACPI_ERR "unknown version of the "
3254 "HKEY interface: 0x%x\n", hkeyv);
3255 printk(TPACPI_ERR "please report this to %s\n",
3256 TPACPI_MAIL);
3257 } else {
3259 * MHKV 0x100 in A31, R40, R40e,
3260 * T4x, X31, and later
3262 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3263 "firmware HKEY interface version: 0x%x\n",
3264 hkeyv);
3266 /* Paranoia check AND init hotkey_all_mask */
3267 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3268 "MHKA", "qd")) {
3269 printk(TPACPI_ERR
3270 "missing MHKA handler, "
3271 "please report this to %s\n",
3272 TPACPI_MAIL);
3273 /* Fallback: pre-init for FN+F3,F4,F12 */
3274 hotkey_all_mask = 0x080cU;
3275 } else {
3276 tp_features.hotkey_mask = 1;
3281 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3282 "hotkey masks are %s\n",
3283 str_supported(tp_features.hotkey_mask));
3285 /* Init hotkey_all_mask if not initialized yet */
3286 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3287 (quirks & TPACPI_HK_Q_INIMASK))
3288 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3290 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3291 if (tp_features.hotkey_mask) {
3292 /* hotkey_source_mask *must* be zero for
3293 * the first hotkey_mask_get to return hotkey_orig_mask */
3294 res = hotkey_mask_get();
3295 if (res)
3296 goto err_exit;
3298 hotkey_orig_mask = hotkey_acpi_mask;
3299 } else {
3300 hotkey_orig_mask = hotkey_all_mask;
3301 hotkey_acpi_mask = hotkey_all_mask;
3304 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3305 if (dbg_wlswemul) {
3306 tp_features.hotkey_wlsw = 1;
3307 radiosw_state = !!tpacpi_wlsw_emulstate;
3308 printk(TPACPI_INFO
3309 "radio switch emulation enabled\n");
3310 } else
3311 #endif
3312 /* Not all thinkpads have a hardware radio switch */
3313 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3314 tp_features.hotkey_wlsw = 1;
3315 radiosw_state = !!status;
3316 printk(TPACPI_INFO
3317 "radio switch found; radios are %s\n",
3318 enabled(status, 0));
3320 if (tp_features.hotkey_wlsw)
3321 res = add_to_attr_set(hotkey_dev_attributes,
3322 &dev_attr_hotkey_radio_sw.attr);
3324 /* For X41t, X60t, X61t Tablets... */
3325 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3326 tp_features.hotkey_tablet = 1;
3327 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3328 printk(TPACPI_INFO
3329 "possible tablet mode switch found; "
3330 "ThinkPad in %s mode\n",
3331 (tabletsw_state) ? "tablet" : "laptop");
3332 res = add_to_attr_set(hotkey_dev_attributes,
3333 &dev_attr_hotkey_tablet_mode.attr);
3336 if (!res)
3337 res = register_attr_set_with_sysfs(
3338 hotkey_dev_attributes,
3339 &tpacpi_pdev->dev.kobj);
3340 if (res)
3341 goto err_exit;
3343 /* Set up key map */
3345 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3346 GFP_KERNEL);
3347 if (!hotkey_keycode_map) {
3348 printk(TPACPI_ERR
3349 "failed to allocate memory for key map\n");
3350 res = -ENOMEM;
3351 goto err_exit;
3354 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
3355 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3356 "using Lenovo default hot key map\n");
3357 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3358 TPACPI_HOTKEY_MAP_SIZE);
3359 } else {
3360 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3361 "using IBM default hot key map\n");
3362 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3363 TPACPI_HOTKEY_MAP_SIZE);
3366 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3367 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3368 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3369 tpacpi_inputdev->keycode = hotkey_keycode_map;
3370 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3371 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3372 input_set_capability(tpacpi_inputdev, EV_KEY,
3373 hotkey_keycode_map[i]);
3374 } else {
3375 if (i < sizeof(hotkey_reserved_mask)*8)
3376 hotkey_reserved_mask |= 1 << i;
3380 if (tp_features.hotkey_wlsw) {
3381 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3382 input_report_switch(tpacpi_inputdev,
3383 SW_RFKILL_ALL, radiosw_state);
3385 if (tp_features.hotkey_tablet) {
3386 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3387 input_report_switch(tpacpi_inputdev,
3388 SW_TABLET_MODE, tabletsw_state);
3391 /* Do not issue duplicate brightness change events to
3392 * userspace */
3393 if (!tp_features.bright_acpimode)
3394 /* update bright_acpimode... */
3395 tpacpi_check_std_acpi_brightness_support();
3397 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3398 printk(TPACPI_INFO
3399 "This ThinkPad has standard ACPI backlight "
3400 "brightness control, supported by the ACPI "
3401 "video driver\n");
3402 printk(TPACPI_NOTICE
3403 "Disabling thinkpad-acpi brightness events "
3404 "by default...\n");
3406 /* Disable brightness up/down on Lenovo thinkpads when
3407 * ACPI is handling them, otherwise it is plain impossible
3408 * for userspace to do something even remotely sane */
3409 hotkey_reserved_mask |=
3410 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3411 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3412 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3413 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3416 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3417 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3418 & ~hotkey_all_mask
3419 & ~hotkey_reserved_mask;
3421 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3422 "hotkey source mask 0x%08x, polling freq %u\n",
3423 hotkey_source_mask, hotkey_poll_freq);
3424 #endif
3426 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3427 "enabling firmware HKEY event interface...\n");
3428 res = hotkey_status_set(true);
3429 if (res) {
3430 hotkey_exit();
3431 return res;
3433 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3434 | hotkey_driver_mask)
3435 & ~hotkey_source_mask);
3436 if (res < 0 && res != -ENXIO) {
3437 hotkey_exit();
3438 return res;
3440 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3441 & ~hotkey_reserved_mask;
3442 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3443 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3444 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3446 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3447 "legacy ibm/hotkey event reporting over procfs %s\n",
3448 (hotkey_report_mode < 2) ?
3449 "enabled" : "disabled");
3451 tpacpi_inputdev->open = &hotkey_inputdev_open;
3452 tpacpi_inputdev->close = &hotkey_inputdev_close;
3454 hotkey_poll_setup_safe(true);
3456 return 0;
3458 err_exit:
3459 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3460 hotkey_dev_attributes = NULL;
3462 return (res < 0)? res : 1;
3465 static bool hotkey_notify_hotkey(const u32 hkey,
3466 bool *send_acpi_ev,
3467 bool *ignore_acpi_ev)
3469 /* 0x1000-0x1FFF: key presses */
3470 unsigned int scancode = hkey & 0xfff;
3471 *send_acpi_ev = true;
3472 *ignore_acpi_ev = false;
3474 if (scancode > 0 && scancode < 0x21) {
3475 scancode--;
3476 if (!(hotkey_source_mask & (1 << scancode))) {
3477 tpacpi_input_send_key_masked(scancode);
3478 *send_acpi_ev = false;
3479 } else {
3480 *ignore_acpi_ev = true;
3482 return true;
3484 return false;
3487 static bool hotkey_notify_wakeup(const u32 hkey,
3488 bool *send_acpi_ev,
3489 bool *ignore_acpi_ev)
3491 /* 0x2000-0x2FFF: Wakeup reason */
3492 *send_acpi_ev = true;
3493 *ignore_acpi_ev = false;
3495 switch (hkey) {
3496 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3497 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3498 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3499 *ignore_acpi_ev = true;
3500 break;
3502 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3503 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3504 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3505 *ignore_acpi_ev = true;
3506 break;
3508 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3509 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3510 printk(TPACPI_ALERT
3511 "EMERGENCY WAKEUP: battery almost empty\n");
3512 /* how to auto-heal: */
3513 /* 2313: woke up from S3, go to S4/S5 */
3514 /* 2413: woke up from S4, go to S5 */
3515 break;
3517 default:
3518 return false;
3521 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3522 printk(TPACPI_INFO
3523 "woke up due to a hot-unplug "
3524 "request...\n");
3525 hotkey_wakeup_reason_notify_change();
3527 return true;
3530 static bool hotkey_notify_usrevent(const u32 hkey,
3531 bool *send_acpi_ev,
3532 bool *ignore_acpi_ev)
3534 /* 0x5000-0x5FFF: human interface helpers */
3535 *send_acpi_ev = true;
3536 *ignore_acpi_ev = false;
3538 switch (hkey) {
3539 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3540 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3541 return true;
3543 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3544 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3545 tpacpi_input_send_tabletsw();
3546 hotkey_tablet_mode_notify_change();
3547 *send_acpi_ev = false;
3548 return true;
3550 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3551 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3552 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3553 /* do not propagate these events */
3554 *ignore_acpi_ev = true;
3555 return true;
3557 default:
3558 return false;
3562 static void thermal_dump_all_sensors(void);
3564 static bool hotkey_notify_thermal(const u32 hkey,
3565 bool *send_acpi_ev,
3566 bool *ignore_acpi_ev)
3568 bool known = true;
3570 /* 0x6000-0x6FFF: thermal alarms */
3571 *send_acpi_ev = true;
3572 *ignore_acpi_ev = false;
3574 switch (hkey) {
3575 case TP_HKEY_EV_THM_TABLE_CHANGED:
3576 printk(TPACPI_INFO
3577 "EC reports that Thermal Table has changed\n");
3578 /* recommended action: do nothing, we don't have
3579 * Lenovo ATM information */
3580 return true;
3581 case TP_HKEY_EV_ALARM_BAT_HOT:
3582 printk(TPACPI_CRIT
3583 "THERMAL ALARM: battery is too hot!\n");
3584 /* recommended action: warn user through gui */
3585 break;
3586 case TP_HKEY_EV_ALARM_BAT_XHOT:
3587 printk(TPACPI_ALERT
3588 "THERMAL EMERGENCY: battery is extremely hot!\n");
3589 /* recommended action: immediate sleep/hibernate */
3590 break;
3591 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3592 printk(TPACPI_CRIT
3593 "THERMAL ALARM: "
3594 "a sensor reports something is too hot!\n");
3595 /* recommended action: warn user through gui, that */
3596 /* some internal component is too hot */
3597 break;
3598 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3599 printk(TPACPI_ALERT
3600 "THERMAL EMERGENCY: "
3601 "a sensor reports something is extremely hot!\n");
3602 /* recommended action: immediate sleep/hibernate */
3603 break;
3604 default:
3605 printk(TPACPI_ALERT
3606 "THERMAL ALERT: unknown thermal alarm received\n");
3607 known = false;
3610 thermal_dump_all_sensors();
3612 return known;
3615 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3617 u32 hkey;
3618 bool send_acpi_ev;
3619 bool ignore_acpi_ev;
3620 bool known_ev;
3622 if (event != 0x80) {
3623 printk(TPACPI_ERR
3624 "unknown HKEY notification event %d\n", event);
3625 /* forward it to userspace, maybe it knows how to handle it */
3626 acpi_bus_generate_netlink_event(
3627 ibm->acpi->device->pnp.device_class,
3628 dev_name(&ibm->acpi->device->dev),
3629 event, 0);
3630 return;
3633 while (1) {
3634 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3635 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3636 return;
3639 if (hkey == 0) {
3640 /* queue empty */
3641 return;
3644 send_acpi_ev = true;
3645 ignore_acpi_ev = false;
3647 switch (hkey >> 12) {
3648 case 1:
3649 /* 0x1000-0x1FFF: key presses */
3650 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3651 &ignore_acpi_ev);
3652 break;
3653 case 2:
3654 /* 0x2000-0x2FFF: Wakeup reason */
3655 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3656 &ignore_acpi_ev);
3657 break;
3658 case 3:
3659 /* 0x3000-0x3FFF: bay-related wakeups */
3660 switch (hkey) {
3661 case TP_HKEY_EV_BAYEJ_ACK:
3662 hotkey_autosleep_ack = 1;
3663 printk(TPACPI_INFO
3664 "bay ejected\n");
3665 hotkey_wakeup_hotunplug_complete_notify_change();
3666 known_ev = true;
3667 break;
3668 case TP_HKEY_EV_OPTDRV_EJ:
3669 /* FIXME: kick libata if SATA link offline */
3670 known_ev = true;
3671 break;
3672 default:
3673 known_ev = false;
3675 break;
3676 case 4:
3677 /* 0x4000-0x4FFF: dock-related wakeups */
3678 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3679 hotkey_autosleep_ack = 1;
3680 printk(TPACPI_INFO
3681 "undocked\n");
3682 hotkey_wakeup_hotunplug_complete_notify_change();
3683 known_ev = true;
3684 } else {
3685 known_ev = false;
3687 break;
3688 case 5:
3689 /* 0x5000-0x5FFF: human interface helpers */
3690 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3691 &ignore_acpi_ev);
3692 break;
3693 case 6:
3694 /* 0x6000-0x6FFF: thermal alarms */
3695 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3696 &ignore_acpi_ev);
3697 break;
3698 case 7:
3699 /* 0x7000-0x7FFF: misc */
3700 if (tp_features.hotkey_wlsw &&
3701 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3702 tpacpi_send_radiosw_update();
3703 send_acpi_ev = 0;
3704 known_ev = true;
3705 break;
3707 /* fallthrough to default */
3708 default:
3709 known_ev = false;
3711 if (!known_ev) {
3712 printk(TPACPI_NOTICE
3713 "unhandled HKEY event 0x%04x\n", hkey);
3714 printk(TPACPI_NOTICE
3715 "please report the conditions when this "
3716 "event happened to %s\n", TPACPI_MAIL);
3719 /* Legacy events */
3720 if (!ignore_acpi_ev &&
3721 (send_acpi_ev || hotkey_report_mode < 2)) {
3722 acpi_bus_generate_proc_event(ibm->acpi->device,
3723 event, hkey);
3726 /* netlink events */
3727 if (!ignore_acpi_ev && send_acpi_ev) {
3728 acpi_bus_generate_netlink_event(
3729 ibm->acpi->device->pnp.device_class,
3730 dev_name(&ibm->acpi->device->dev),
3731 event, hkey);
3736 static void hotkey_suspend(pm_message_t state)
3738 /* Do these on suspend, we get the events on early resume! */
3739 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3740 hotkey_autosleep_ack = 0;
3743 static void hotkey_resume(void)
3745 tpacpi_disable_brightness_delay();
3747 if (hotkey_status_set(true) < 0 ||
3748 hotkey_mask_set(hotkey_acpi_mask) < 0)
3749 printk(TPACPI_ERR
3750 "error while attempting to reset the event "
3751 "firmware interface\n");
3753 tpacpi_send_radiosw_update();
3754 hotkey_tablet_mode_notify_change();
3755 hotkey_wakeup_reason_notify_change();
3756 hotkey_wakeup_hotunplug_complete_notify_change();
3757 hotkey_poll_setup_safe(false);
3760 /* procfs -------------------------------------------------------------- */
3761 static int hotkey_read(struct seq_file *m)
3763 int res, status;
3765 if (!tp_features.hotkey) {
3766 seq_printf(m, "status:\t\tnot supported\n");
3767 return 0;
3770 if (mutex_lock_killable(&hotkey_mutex))
3771 return -ERESTARTSYS;
3772 res = hotkey_status_get(&status);
3773 if (!res)
3774 res = hotkey_mask_get();
3775 mutex_unlock(&hotkey_mutex);
3776 if (res)
3777 return res;
3779 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3780 if (hotkey_all_mask) {
3781 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3782 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3783 } else {
3784 seq_printf(m, "mask:\t\tnot supported\n");
3785 seq_printf(m, "commands:\tenable, disable, reset\n");
3788 return 0;
3791 static void hotkey_enabledisable_warn(bool enable)
3793 tpacpi_log_usertask("procfs hotkey enable/disable");
3794 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3795 TPACPI_WARN
3796 "hotkey enable/disable functionality has been "
3797 "removed from the driver. Hotkeys are always "
3798 "enabled\n"))
3799 printk(TPACPI_ERR
3800 "Please remove the hotkey=enable module "
3801 "parameter, it is deprecated. Hotkeys are always "
3802 "enabled\n");
3805 static int hotkey_write(char *buf)
3807 int res;
3808 u32 mask;
3809 char *cmd;
3811 if (!tp_features.hotkey)
3812 return -ENODEV;
3814 if (mutex_lock_killable(&hotkey_mutex))
3815 return -ERESTARTSYS;
3817 mask = hotkey_user_mask;
3819 res = 0;
3820 while ((cmd = next_cmd(&buf))) {
3821 if (strlencmp(cmd, "enable") == 0) {
3822 hotkey_enabledisable_warn(1);
3823 } else if (strlencmp(cmd, "disable") == 0) {
3824 hotkey_enabledisable_warn(0);
3825 res = -EPERM;
3826 } else if (strlencmp(cmd, "reset") == 0) {
3827 mask = (hotkey_all_mask | hotkey_source_mask)
3828 & ~hotkey_reserved_mask;
3829 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3830 /* mask set */
3831 } else if (sscanf(cmd, "%x", &mask) == 1) {
3832 /* mask set */
3833 } else {
3834 res = -EINVAL;
3835 goto errexit;
3839 if (!res) {
3840 tpacpi_disclose_usertask("procfs hotkey",
3841 "set mask to 0x%08x\n", mask);
3842 res = hotkey_user_mask_set(mask);
3845 errexit:
3846 mutex_unlock(&hotkey_mutex);
3847 return res;
3850 static const struct acpi_device_id ibm_htk_device_ids[] = {
3851 {TPACPI_ACPI_HKEY_HID, 0},
3852 {"", 0},
3855 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3856 .hid = ibm_htk_device_ids,
3857 .notify = hotkey_notify,
3858 .handle = &hkey_handle,
3859 .type = ACPI_DEVICE_NOTIFY,
3862 static struct ibm_struct hotkey_driver_data = {
3863 .name = "hotkey",
3864 .read = hotkey_read,
3865 .write = hotkey_write,
3866 .exit = hotkey_exit,
3867 .resume = hotkey_resume,
3868 .suspend = hotkey_suspend,
3869 .acpi = &ibm_hotkey_acpidriver,
3872 /*************************************************************************
3873 * Bluetooth subdriver
3876 enum {
3877 /* ACPI GBDC/SBDC bits */
3878 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3879 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3880 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3881 0 = disable, 1 = enable */
3884 enum {
3885 /* ACPI \BLTH commands */
3886 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3887 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3888 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3889 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3890 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3893 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3895 static int bluetooth_get_status(void)
3897 int status;
3899 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3900 if (dbg_bluetoothemul)
3901 return (tpacpi_bluetooth_emulstate) ?
3902 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3903 #endif
3905 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3906 return -EIO;
3908 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3909 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3912 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3914 int status;
3916 vdbg_printk(TPACPI_DBG_RFKILL,
3917 "will attempt to %s bluetooth\n",
3918 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3920 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3921 if (dbg_bluetoothemul) {
3922 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3923 return 0;
3925 #endif
3927 if (state == TPACPI_RFK_RADIO_ON)
3928 status = TP_ACPI_BLUETOOTH_RADIOSSW
3929 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3930 else
3931 status = 0;
3933 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3934 return -EIO;
3936 return 0;
3939 /* sysfs bluetooth enable ---------------------------------------------- */
3940 static ssize_t bluetooth_enable_show(struct device *dev,
3941 struct device_attribute *attr,
3942 char *buf)
3944 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3945 attr, buf);
3948 static ssize_t bluetooth_enable_store(struct device *dev,
3949 struct device_attribute *attr,
3950 const char *buf, size_t count)
3952 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3953 attr, buf, count);
3956 static struct device_attribute dev_attr_bluetooth_enable =
3957 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3958 bluetooth_enable_show, bluetooth_enable_store);
3960 /* --------------------------------------------------------------------- */
3962 static struct attribute *bluetooth_attributes[] = {
3963 &dev_attr_bluetooth_enable.attr,
3964 NULL
3967 static const struct attribute_group bluetooth_attr_group = {
3968 .attrs = bluetooth_attributes,
3971 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3972 .get_status = bluetooth_get_status,
3973 .set_status = bluetooth_set_status,
3976 static void bluetooth_shutdown(void)
3978 /* Order firmware to save current state to NVRAM */
3979 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3980 TP_ACPI_BLTH_SAVE_STATE))
3981 printk(TPACPI_NOTICE
3982 "failed to save bluetooth state to NVRAM\n");
3983 else
3984 vdbg_printk(TPACPI_DBG_RFKILL,
3985 "bluestooth state saved to NVRAM\n");
3988 static void bluetooth_exit(void)
3990 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3991 &bluetooth_attr_group);
3993 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3995 bluetooth_shutdown();
3998 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4000 int res;
4001 int status = 0;
4003 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4004 "initializing bluetooth subdriver\n");
4006 TPACPI_ACPIHANDLE_INIT(hkey);
4008 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4009 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4010 tp_features.bluetooth = hkey_handle &&
4011 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4013 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4014 "bluetooth is %s, status 0x%02x\n",
4015 str_supported(tp_features.bluetooth),
4016 status);
4018 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4019 if (dbg_bluetoothemul) {
4020 tp_features.bluetooth = 1;
4021 printk(TPACPI_INFO
4022 "bluetooth switch emulation enabled\n");
4023 } else
4024 #endif
4025 if (tp_features.bluetooth &&
4026 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4027 /* no bluetooth hardware present in system */
4028 tp_features.bluetooth = 0;
4029 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4030 "bluetooth hardware not installed\n");
4033 if (!tp_features.bluetooth)
4034 return 1;
4036 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4037 &bluetooth_tprfk_ops,
4038 RFKILL_TYPE_BLUETOOTH,
4039 TPACPI_RFK_BLUETOOTH_SW_NAME,
4040 true);
4041 if (res)
4042 return res;
4044 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4045 &bluetooth_attr_group);
4046 if (res) {
4047 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4048 return res;
4051 return 0;
4054 /* procfs -------------------------------------------------------------- */
4055 static int bluetooth_read(struct seq_file *m)
4057 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4060 static int bluetooth_write(char *buf)
4062 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4065 static struct ibm_struct bluetooth_driver_data = {
4066 .name = "bluetooth",
4067 .read = bluetooth_read,
4068 .write = bluetooth_write,
4069 .exit = bluetooth_exit,
4070 .shutdown = bluetooth_shutdown,
4073 /*************************************************************************
4074 * Wan subdriver
4077 enum {
4078 /* ACPI GWAN/SWAN bits */
4079 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4080 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4081 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4082 0 = disable, 1 = enable */
4085 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4087 static int wan_get_status(void)
4089 int status;
4091 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4092 if (dbg_wwanemul)
4093 return (tpacpi_wwan_emulstate) ?
4094 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4095 #endif
4097 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4098 return -EIO;
4100 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4101 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4104 static int wan_set_status(enum tpacpi_rfkill_state state)
4106 int status;
4108 vdbg_printk(TPACPI_DBG_RFKILL,
4109 "will attempt to %s wwan\n",
4110 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4112 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4113 if (dbg_wwanemul) {
4114 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4115 return 0;
4117 #endif
4119 if (state == TPACPI_RFK_RADIO_ON)
4120 status = TP_ACPI_WANCARD_RADIOSSW
4121 | TP_ACPI_WANCARD_RESUMECTRL;
4122 else
4123 status = 0;
4125 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4126 return -EIO;
4128 return 0;
4131 /* sysfs wan enable ---------------------------------------------------- */
4132 static ssize_t wan_enable_show(struct device *dev,
4133 struct device_attribute *attr,
4134 char *buf)
4136 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4137 attr, buf);
4140 static ssize_t wan_enable_store(struct device *dev,
4141 struct device_attribute *attr,
4142 const char *buf, size_t count)
4144 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4145 attr, buf, count);
4148 static struct device_attribute dev_attr_wan_enable =
4149 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4150 wan_enable_show, wan_enable_store);
4152 /* --------------------------------------------------------------------- */
4154 static struct attribute *wan_attributes[] = {
4155 &dev_attr_wan_enable.attr,
4156 NULL
4159 static const struct attribute_group wan_attr_group = {
4160 .attrs = wan_attributes,
4163 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4164 .get_status = wan_get_status,
4165 .set_status = wan_set_status,
4168 static void wan_shutdown(void)
4170 /* Order firmware to save current state to NVRAM */
4171 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4172 TP_ACPI_WGSV_SAVE_STATE))
4173 printk(TPACPI_NOTICE
4174 "failed to save WWAN state to NVRAM\n");
4175 else
4176 vdbg_printk(TPACPI_DBG_RFKILL,
4177 "WWAN state saved to NVRAM\n");
4180 static void wan_exit(void)
4182 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4183 &wan_attr_group);
4185 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4187 wan_shutdown();
4190 static int __init wan_init(struct ibm_init_struct *iibm)
4192 int res;
4193 int status = 0;
4195 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4196 "initializing wan subdriver\n");
4198 TPACPI_ACPIHANDLE_INIT(hkey);
4200 tp_features.wan = hkey_handle &&
4201 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4203 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4204 "wan is %s, status 0x%02x\n",
4205 str_supported(tp_features.wan),
4206 status);
4208 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4209 if (dbg_wwanemul) {
4210 tp_features.wan = 1;
4211 printk(TPACPI_INFO
4212 "wwan switch emulation enabled\n");
4213 } else
4214 #endif
4215 if (tp_features.wan &&
4216 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4217 /* no wan hardware present in system */
4218 tp_features.wan = 0;
4219 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4220 "wan hardware not installed\n");
4223 if (!tp_features.wan)
4224 return 1;
4226 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4227 &wan_tprfk_ops,
4228 RFKILL_TYPE_WWAN,
4229 TPACPI_RFK_WWAN_SW_NAME,
4230 true);
4231 if (res)
4232 return res;
4234 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4235 &wan_attr_group);
4237 if (res) {
4238 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4239 return res;
4242 return 0;
4245 /* procfs -------------------------------------------------------------- */
4246 static int wan_read(struct seq_file *m)
4248 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4251 static int wan_write(char *buf)
4253 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4256 static struct ibm_struct wan_driver_data = {
4257 .name = "wan",
4258 .read = wan_read,
4259 .write = wan_write,
4260 .exit = wan_exit,
4261 .shutdown = wan_shutdown,
4264 /*************************************************************************
4265 * UWB subdriver
4268 enum {
4269 /* ACPI GUWB/SUWB bits */
4270 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4271 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4274 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4276 static int uwb_get_status(void)
4278 int status;
4280 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4281 if (dbg_uwbemul)
4282 return (tpacpi_uwb_emulstate) ?
4283 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4284 #endif
4286 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4287 return -EIO;
4289 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4290 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4293 static int uwb_set_status(enum tpacpi_rfkill_state state)
4295 int status;
4297 vdbg_printk(TPACPI_DBG_RFKILL,
4298 "will attempt to %s UWB\n",
4299 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4301 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4302 if (dbg_uwbemul) {
4303 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4304 return 0;
4306 #endif
4308 if (state == TPACPI_RFK_RADIO_ON)
4309 status = TP_ACPI_UWB_RADIOSSW;
4310 else
4311 status = 0;
4313 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4314 return -EIO;
4316 return 0;
4319 /* --------------------------------------------------------------------- */
4321 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4322 .get_status = uwb_get_status,
4323 .set_status = uwb_set_status,
4326 static void uwb_exit(void)
4328 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4331 static int __init uwb_init(struct ibm_init_struct *iibm)
4333 int res;
4334 int status = 0;
4336 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4337 "initializing uwb subdriver\n");
4339 TPACPI_ACPIHANDLE_INIT(hkey);
4341 tp_features.uwb = hkey_handle &&
4342 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4344 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4345 "uwb is %s, status 0x%02x\n",
4346 str_supported(tp_features.uwb),
4347 status);
4349 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4350 if (dbg_uwbemul) {
4351 tp_features.uwb = 1;
4352 printk(TPACPI_INFO
4353 "uwb switch emulation enabled\n");
4354 } else
4355 #endif
4356 if (tp_features.uwb &&
4357 !(status & TP_ACPI_UWB_HWPRESENT)) {
4358 /* no uwb hardware present in system */
4359 tp_features.uwb = 0;
4360 dbg_printk(TPACPI_DBG_INIT,
4361 "uwb hardware not installed\n");
4364 if (!tp_features.uwb)
4365 return 1;
4367 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4368 &uwb_tprfk_ops,
4369 RFKILL_TYPE_UWB,
4370 TPACPI_RFK_UWB_SW_NAME,
4371 false);
4372 return res;
4375 static struct ibm_struct uwb_driver_data = {
4376 .name = "uwb",
4377 .exit = uwb_exit,
4378 .flags.experimental = 1,
4381 /*************************************************************************
4382 * Video subdriver
4385 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4387 enum video_access_mode {
4388 TPACPI_VIDEO_NONE = 0,
4389 TPACPI_VIDEO_570, /* 570 */
4390 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4391 TPACPI_VIDEO_NEW, /* all others */
4394 enum { /* video status flags, based on VIDEO_570 */
4395 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4396 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4397 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4400 enum { /* TPACPI_VIDEO_570 constants */
4401 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4402 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4403 * video_status_flags */
4404 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4405 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4408 static enum video_access_mode video_supported;
4409 static int video_orig_autosw;
4411 static int video_autosw_get(void);
4412 static int video_autosw_set(int enable);
4414 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4416 static int __init video_init(struct ibm_init_struct *iibm)
4418 int ivga;
4420 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4422 TPACPI_ACPIHANDLE_INIT(vid);
4423 TPACPI_ACPIHANDLE_INIT(vid2);
4425 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4426 /* G41, assume IVGA doesn't change */
4427 vid_handle = vid2_handle;
4429 if (!vid_handle)
4430 /* video switching not supported on R30, R31 */
4431 video_supported = TPACPI_VIDEO_NONE;
4432 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4433 /* 570 */
4434 video_supported = TPACPI_VIDEO_570;
4435 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4436 /* 600e/x, 770e, 770x */
4437 video_supported = TPACPI_VIDEO_770;
4438 else
4439 /* all others */
4440 video_supported = TPACPI_VIDEO_NEW;
4442 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4443 str_supported(video_supported != TPACPI_VIDEO_NONE),
4444 video_supported);
4446 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4449 static void video_exit(void)
4451 dbg_printk(TPACPI_DBG_EXIT,
4452 "restoring original video autoswitch mode\n");
4453 if (video_autosw_set(video_orig_autosw))
4454 printk(TPACPI_ERR "error while trying to restore original "
4455 "video autoswitch mode\n");
4458 static int video_outputsw_get(void)
4460 int status = 0;
4461 int i;
4463 switch (video_supported) {
4464 case TPACPI_VIDEO_570:
4465 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4466 TP_ACPI_VIDEO_570_PHSCMD))
4467 return -EIO;
4468 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4469 break;
4470 case TPACPI_VIDEO_770:
4471 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4472 return -EIO;
4473 if (i)
4474 status |= TP_ACPI_VIDEO_S_LCD;
4475 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4476 return -EIO;
4477 if (i)
4478 status |= TP_ACPI_VIDEO_S_CRT;
4479 break;
4480 case TPACPI_VIDEO_NEW:
4481 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4482 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4483 return -EIO;
4484 if (i)
4485 status |= TP_ACPI_VIDEO_S_CRT;
4487 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4488 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4489 return -EIO;
4490 if (i)
4491 status |= TP_ACPI_VIDEO_S_LCD;
4492 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4493 return -EIO;
4494 if (i)
4495 status |= TP_ACPI_VIDEO_S_DVI;
4496 break;
4497 default:
4498 return -ENOSYS;
4501 return status;
4504 static int video_outputsw_set(int status)
4506 int autosw;
4507 int res = 0;
4509 switch (video_supported) {
4510 case TPACPI_VIDEO_570:
4511 res = acpi_evalf(NULL, NULL,
4512 "\\_SB.PHS2", "vdd",
4513 TP_ACPI_VIDEO_570_PHS2CMD,
4514 status | TP_ACPI_VIDEO_570_PHS2SET);
4515 break;
4516 case TPACPI_VIDEO_770:
4517 autosw = video_autosw_get();
4518 if (autosw < 0)
4519 return autosw;
4521 res = video_autosw_set(1);
4522 if (res)
4523 return res;
4524 res = acpi_evalf(vid_handle, NULL,
4525 "ASWT", "vdd", status * 0x100, 0);
4526 if (!autosw && video_autosw_set(autosw)) {
4527 printk(TPACPI_ERR
4528 "video auto-switch left enabled due to error\n");
4529 return -EIO;
4531 break;
4532 case TPACPI_VIDEO_NEW:
4533 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4534 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4535 break;
4536 default:
4537 return -ENOSYS;
4540 return (res)? 0 : -EIO;
4543 static int video_autosw_get(void)
4545 int autosw = 0;
4547 switch (video_supported) {
4548 case TPACPI_VIDEO_570:
4549 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4550 return -EIO;
4551 break;
4552 case TPACPI_VIDEO_770:
4553 case TPACPI_VIDEO_NEW:
4554 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4555 return -EIO;
4556 break;
4557 default:
4558 return -ENOSYS;
4561 return autosw & 1;
4564 static int video_autosw_set(int enable)
4566 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4567 return -EIO;
4568 return 0;
4571 static int video_outputsw_cycle(void)
4573 int autosw = video_autosw_get();
4574 int res;
4576 if (autosw < 0)
4577 return autosw;
4579 switch (video_supported) {
4580 case TPACPI_VIDEO_570:
4581 res = video_autosw_set(1);
4582 if (res)
4583 return res;
4584 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4585 break;
4586 case TPACPI_VIDEO_770:
4587 case TPACPI_VIDEO_NEW:
4588 res = video_autosw_set(1);
4589 if (res)
4590 return res;
4591 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4592 break;
4593 default:
4594 return -ENOSYS;
4596 if (!autosw && video_autosw_set(autosw)) {
4597 printk(TPACPI_ERR
4598 "video auto-switch left enabled due to error\n");
4599 return -EIO;
4602 return (res)? 0 : -EIO;
4605 static int video_expand_toggle(void)
4607 switch (video_supported) {
4608 case TPACPI_VIDEO_570:
4609 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4610 0 : -EIO;
4611 case TPACPI_VIDEO_770:
4612 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4613 0 : -EIO;
4614 case TPACPI_VIDEO_NEW:
4615 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4616 0 : -EIO;
4617 default:
4618 return -ENOSYS;
4620 /* not reached */
4623 static int video_read(struct seq_file *m)
4625 int status, autosw;
4627 if (video_supported == TPACPI_VIDEO_NONE) {
4628 seq_printf(m, "status:\t\tnot supported\n");
4629 return 0;
4632 /* Even reads can crash X.org, so... */
4633 if (!capable(CAP_SYS_ADMIN))
4634 return -EPERM;
4636 status = video_outputsw_get();
4637 if (status < 0)
4638 return status;
4640 autosw = video_autosw_get();
4641 if (autosw < 0)
4642 return autosw;
4644 seq_printf(m, "status:\t\tsupported\n");
4645 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4646 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4647 if (video_supported == TPACPI_VIDEO_NEW)
4648 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4649 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4650 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4651 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4652 if (video_supported == TPACPI_VIDEO_NEW)
4653 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4654 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4655 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4657 return 0;
4660 static int video_write(char *buf)
4662 char *cmd;
4663 int enable, disable, status;
4664 int res;
4666 if (video_supported == TPACPI_VIDEO_NONE)
4667 return -ENODEV;
4669 /* Even reads can crash X.org, let alone writes... */
4670 if (!capable(CAP_SYS_ADMIN))
4671 return -EPERM;
4673 enable = 0;
4674 disable = 0;
4676 while ((cmd = next_cmd(&buf))) {
4677 if (strlencmp(cmd, "lcd_enable") == 0) {
4678 enable |= TP_ACPI_VIDEO_S_LCD;
4679 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4680 disable |= TP_ACPI_VIDEO_S_LCD;
4681 } else if (strlencmp(cmd, "crt_enable") == 0) {
4682 enable |= TP_ACPI_VIDEO_S_CRT;
4683 } else if (strlencmp(cmd, "crt_disable") == 0) {
4684 disable |= TP_ACPI_VIDEO_S_CRT;
4685 } else if (video_supported == TPACPI_VIDEO_NEW &&
4686 strlencmp(cmd, "dvi_enable") == 0) {
4687 enable |= TP_ACPI_VIDEO_S_DVI;
4688 } else if (video_supported == TPACPI_VIDEO_NEW &&
4689 strlencmp(cmd, "dvi_disable") == 0) {
4690 disable |= TP_ACPI_VIDEO_S_DVI;
4691 } else if (strlencmp(cmd, "auto_enable") == 0) {
4692 res = video_autosw_set(1);
4693 if (res)
4694 return res;
4695 } else if (strlencmp(cmd, "auto_disable") == 0) {
4696 res = video_autosw_set(0);
4697 if (res)
4698 return res;
4699 } else if (strlencmp(cmd, "video_switch") == 0) {
4700 res = video_outputsw_cycle();
4701 if (res)
4702 return res;
4703 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4704 res = video_expand_toggle();
4705 if (res)
4706 return res;
4707 } else
4708 return -EINVAL;
4711 if (enable || disable) {
4712 status = video_outputsw_get();
4713 if (status < 0)
4714 return status;
4715 res = video_outputsw_set((status & ~disable) | enable);
4716 if (res)
4717 return res;
4720 return 0;
4723 static struct ibm_struct video_driver_data = {
4724 .name = "video",
4725 .read = video_read,
4726 .write = video_write,
4727 .exit = video_exit,
4730 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4732 /*************************************************************************
4733 * Light (thinklight) subdriver
4736 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4737 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4739 static int light_get_status(void)
4741 int status = 0;
4743 if (tp_features.light_status) {
4744 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4745 return -EIO;
4746 return (!!status);
4749 return -ENXIO;
4752 static int light_set_status(int status)
4754 int rc;
4756 if (tp_features.light) {
4757 if (cmos_handle) {
4758 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4759 (status)?
4760 TP_CMOS_THINKLIGHT_ON :
4761 TP_CMOS_THINKLIGHT_OFF);
4762 } else {
4763 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4764 (status)? 1 : 0);
4766 return (rc)? 0 : -EIO;
4769 return -ENXIO;
4772 static void light_set_status_worker(struct work_struct *work)
4774 struct tpacpi_led_classdev *data =
4775 container_of(work, struct tpacpi_led_classdev, work);
4777 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4778 light_set_status((data->new_state != TPACPI_LED_OFF));
4781 static void light_sysfs_set(struct led_classdev *led_cdev,
4782 enum led_brightness brightness)
4784 struct tpacpi_led_classdev *data =
4785 container_of(led_cdev,
4786 struct tpacpi_led_classdev,
4787 led_classdev);
4788 data->new_state = (brightness != LED_OFF) ?
4789 TPACPI_LED_ON : TPACPI_LED_OFF;
4790 queue_work(tpacpi_wq, &data->work);
4793 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4795 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4798 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4799 .led_classdev = {
4800 .name = "tpacpi::thinklight",
4801 .brightness_set = &light_sysfs_set,
4802 .brightness_get = &light_sysfs_get,
4806 static int __init light_init(struct ibm_init_struct *iibm)
4808 int rc;
4810 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4812 TPACPI_ACPIHANDLE_INIT(ledb);
4813 TPACPI_ACPIHANDLE_INIT(lght);
4814 TPACPI_ACPIHANDLE_INIT(cmos);
4815 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4817 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4818 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4820 if (tp_features.light)
4821 /* light status not supported on
4822 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4823 tp_features.light_status =
4824 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4826 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4827 str_supported(tp_features.light),
4828 str_supported(tp_features.light_status));
4830 if (!tp_features.light)
4831 return 1;
4833 rc = led_classdev_register(&tpacpi_pdev->dev,
4834 &tpacpi_led_thinklight.led_classdev);
4836 if (rc < 0) {
4837 tp_features.light = 0;
4838 tp_features.light_status = 0;
4839 } else {
4840 rc = 0;
4843 return rc;
4846 static void light_exit(void)
4848 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4849 if (work_pending(&tpacpi_led_thinklight.work))
4850 flush_workqueue(tpacpi_wq);
4853 static int light_read(struct seq_file *m)
4855 int status;
4857 if (!tp_features.light) {
4858 seq_printf(m, "status:\t\tnot supported\n");
4859 } else if (!tp_features.light_status) {
4860 seq_printf(m, "status:\t\tunknown\n");
4861 seq_printf(m, "commands:\ton, off\n");
4862 } else {
4863 status = light_get_status();
4864 if (status < 0)
4865 return status;
4866 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4867 seq_printf(m, "commands:\ton, off\n");
4870 return 0;
4873 static int light_write(char *buf)
4875 char *cmd;
4876 int newstatus = 0;
4878 if (!tp_features.light)
4879 return -ENODEV;
4881 while ((cmd = next_cmd(&buf))) {
4882 if (strlencmp(cmd, "on") == 0) {
4883 newstatus = 1;
4884 } else if (strlencmp(cmd, "off") == 0) {
4885 newstatus = 0;
4886 } else
4887 return -EINVAL;
4890 return light_set_status(newstatus);
4893 static struct ibm_struct light_driver_data = {
4894 .name = "light",
4895 .read = light_read,
4896 .write = light_write,
4897 .exit = light_exit,
4900 /*************************************************************************
4901 * CMOS subdriver
4904 /* sysfs cmos_command -------------------------------------------------- */
4905 static ssize_t cmos_command_store(struct device *dev,
4906 struct device_attribute *attr,
4907 const char *buf, size_t count)
4909 unsigned long cmos_cmd;
4910 int res;
4912 if (parse_strtoul(buf, 21, &cmos_cmd))
4913 return -EINVAL;
4915 res = issue_thinkpad_cmos_command(cmos_cmd);
4916 return (res)? res : count;
4919 static struct device_attribute dev_attr_cmos_command =
4920 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4922 /* --------------------------------------------------------------------- */
4924 static int __init cmos_init(struct ibm_init_struct *iibm)
4926 int res;
4928 vdbg_printk(TPACPI_DBG_INIT,
4929 "initializing cmos commands subdriver\n");
4931 TPACPI_ACPIHANDLE_INIT(cmos);
4933 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4934 str_supported(cmos_handle != NULL));
4936 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4937 if (res)
4938 return res;
4940 return (cmos_handle)? 0 : 1;
4943 static void cmos_exit(void)
4945 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4948 static int cmos_read(struct seq_file *m)
4950 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4951 R30, R31, T20-22, X20-21 */
4952 if (!cmos_handle)
4953 seq_printf(m, "status:\t\tnot supported\n");
4954 else {
4955 seq_printf(m, "status:\t\tsupported\n");
4956 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4959 return 0;
4962 static int cmos_write(char *buf)
4964 char *cmd;
4965 int cmos_cmd, res;
4967 while ((cmd = next_cmd(&buf))) {
4968 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4969 cmos_cmd >= 0 && cmos_cmd <= 21) {
4970 /* cmos_cmd set */
4971 } else
4972 return -EINVAL;
4974 res = issue_thinkpad_cmos_command(cmos_cmd);
4975 if (res)
4976 return res;
4979 return 0;
4982 static struct ibm_struct cmos_driver_data = {
4983 .name = "cmos",
4984 .read = cmos_read,
4985 .write = cmos_write,
4986 .exit = cmos_exit,
4989 /*************************************************************************
4990 * LED subdriver
4993 enum led_access_mode {
4994 TPACPI_LED_NONE = 0,
4995 TPACPI_LED_570, /* 570 */
4996 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4997 TPACPI_LED_NEW, /* all others */
5000 enum { /* For TPACPI_LED_OLD */
5001 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5002 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5003 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5006 static enum led_access_mode led_supported;
5008 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
5009 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
5010 /* T20-22, X20-21 */
5011 "LED", /* all others */
5012 ); /* R30, R31 */
5014 #define TPACPI_LED_NUMLEDS 16
5015 static struct tpacpi_led_classdev *tpacpi_leds;
5016 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5017 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5018 /* there's a limit of 19 chars + NULL before 2.6.26 */
5019 "tpacpi::power",
5020 "tpacpi:orange:batt",
5021 "tpacpi:green:batt",
5022 "tpacpi::dock_active",
5023 "tpacpi::bay_active",
5024 "tpacpi::dock_batt",
5025 "tpacpi::unknown_led",
5026 "tpacpi::standby",
5027 "tpacpi::dock_status1",
5028 "tpacpi::dock_status2",
5029 "tpacpi::unknown_led2",
5030 "tpacpi::unknown_led3",
5031 "tpacpi::thinkvantage",
5033 #define TPACPI_SAFE_LEDS 0x1081U
5035 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5037 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5038 return false;
5039 #else
5040 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5041 #endif
5044 static int led_get_status(const unsigned int led)
5046 int status;
5047 enum led_status_t led_s;
5049 switch (led_supported) {
5050 case TPACPI_LED_570:
5051 if (!acpi_evalf(ec_handle,
5052 &status, "GLED", "dd", 1 << led))
5053 return -EIO;
5054 led_s = (status == 0)?
5055 TPACPI_LED_OFF :
5056 ((status == 1)?
5057 TPACPI_LED_ON :
5058 TPACPI_LED_BLINK);
5059 tpacpi_led_state_cache[led] = led_s;
5060 return led_s;
5061 default:
5062 return -ENXIO;
5065 /* not reached */
5068 static int led_set_status(const unsigned int led,
5069 const enum led_status_t ledstatus)
5071 /* off, on, blink. Index is led_status_t */
5072 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5073 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5075 int rc = 0;
5077 switch (led_supported) {
5078 case TPACPI_LED_570:
5079 /* 570 */
5080 if (unlikely(led > 7))
5081 return -EINVAL;
5082 if (unlikely(tpacpi_is_led_restricted(led)))
5083 return -EPERM;
5084 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5085 (1 << led), led_sled_arg1[ledstatus]))
5086 rc = -EIO;
5087 break;
5088 case TPACPI_LED_OLD:
5089 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5090 if (unlikely(led > 7))
5091 return -EINVAL;
5092 if (unlikely(tpacpi_is_led_restricted(led)))
5093 return -EPERM;
5094 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5095 if (rc >= 0)
5096 rc = ec_write(TPACPI_LED_EC_HLBL,
5097 (ledstatus == TPACPI_LED_BLINK) << led);
5098 if (rc >= 0)
5099 rc = ec_write(TPACPI_LED_EC_HLCL,
5100 (ledstatus != TPACPI_LED_OFF) << led);
5101 break;
5102 case TPACPI_LED_NEW:
5103 /* all others */
5104 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5105 return -EINVAL;
5106 if (unlikely(tpacpi_is_led_restricted(led)))
5107 return -EPERM;
5108 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5109 led, led_led_arg1[ledstatus]))
5110 rc = -EIO;
5111 break;
5112 default:
5113 rc = -ENXIO;
5116 if (!rc)
5117 tpacpi_led_state_cache[led] = ledstatus;
5119 return rc;
5122 static void led_set_status_worker(struct work_struct *work)
5124 struct tpacpi_led_classdev *data =
5125 container_of(work, struct tpacpi_led_classdev, work);
5127 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5128 led_set_status(data->led, data->new_state);
5131 static void led_sysfs_set(struct led_classdev *led_cdev,
5132 enum led_brightness brightness)
5134 struct tpacpi_led_classdev *data = container_of(led_cdev,
5135 struct tpacpi_led_classdev, led_classdev);
5137 if (brightness == LED_OFF)
5138 data->new_state = TPACPI_LED_OFF;
5139 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5140 data->new_state = TPACPI_LED_ON;
5141 else
5142 data->new_state = TPACPI_LED_BLINK;
5144 queue_work(tpacpi_wq, &data->work);
5147 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5148 unsigned long *delay_on, unsigned long *delay_off)
5150 struct tpacpi_led_classdev *data = container_of(led_cdev,
5151 struct tpacpi_led_classdev, led_classdev);
5153 /* Can we choose the flash rate? */
5154 if (*delay_on == 0 && *delay_off == 0) {
5155 /* yes. set them to the hardware blink rate (1 Hz) */
5156 *delay_on = 500; /* ms */
5157 *delay_off = 500; /* ms */
5158 } else if ((*delay_on != 500) || (*delay_off != 500))
5159 return -EINVAL;
5161 data->new_state = TPACPI_LED_BLINK;
5162 queue_work(tpacpi_wq, &data->work);
5164 return 0;
5167 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5169 int rc;
5171 struct tpacpi_led_classdev *data = container_of(led_cdev,
5172 struct tpacpi_led_classdev, led_classdev);
5174 rc = led_get_status(data->led);
5176 if (rc == TPACPI_LED_OFF || rc < 0)
5177 rc = LED_OFF; /* no error handling in led class :( */
5178 else
5179 rc = LED_FULL;
5181 return rc;
5184 static void led_exit(void)
5186 unsigned int i;
5188 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5189 if (tpacpi_leds[i].led_classdev.name)
5190 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5193 kfree(tpacpi_leds);
5196 static int __init tpacpi_init_led(unsigned int led)
5198 int rc;
5200 tpacpi_leds[led].led = led;
5202 /* LEDs with no name don't get registered */
5203 if (!tpacpi_led_names[led])
5204 return 0;
5206 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5207 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5208 if (led_supported == TPACPI_LED_570)
5209 tpacpi_leds[led].led_classdev.brightness_get =
5210 &led_sysfs_get;
5212 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5214 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5216 rc = led_classdev_register(&tpacpi_pdev->dev,
5217 &tpacpi_leds[led].led_classdev);
5218 if (rc < 0)
5219 tpacpi_leds[led].led_classdev.name = NULL;
5221 return rc;
5224 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5225 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5226 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5227 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5229 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5230 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5231 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5232 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5233 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5234 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5235 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5236 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5238 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5239 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5240 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5241 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5242 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5244 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5245 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5246 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5247 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5249 /* (1) - may have excess leds enabled on MSB */
5251 /* Defaults (order matters, keep last, don't reorder!) */
5252 { /* Lenovo */
5253 .vendor = PCI_VENDOR_ID_LENOVO,
5254 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5255 .quirks = 0x1fffU,
5257 { /* IBM ThinkPads with no EC version string */
5258 .vendor = PCI_VENDOR_ID_IBM,
5259 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5260 .quirks = 0x00ffU,
5262 { /* IBM ThinkPads with EC version string */
5263 .vendor = PCI_VENDOR_ID_IBM,
5264 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5265 .quirks = 0x00bfU,
5269 #undef TPACPI_LEDQ_IBM
5270 #undef TPACPI_LEDQ_LNV
5272 static int __init led_init(struct ibm_init_struct *iibm)
5274 unsigned int i;
5275 int rc;
5276 unsigned long useful_leds;
5278 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5280 TPACPI_ACPIHANDLE_INIT(led);
5282 if (!led_handle)
5283 /* led not supported on R30, R31 */
5284 led_supported = TPACPI_LED_NONE;
5285 else if (strlencmp(led_path, "SLED") == 0)
5286 /* 570 */
5287 led_supported = TPACPI_LED_570;
5288 else if (strlencmp(led_path, "SYSL") == 0)
5289 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5290 led_supported = TPACPI_LED_OLD;
5291 else
5292 /* all others */
5293 led_supported = TPACPI_LED_NEW;
5295 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5296 str_supported(led_supported), led_supported);
5298 if (led_supported == TPACPI_LED_NONE)
5299 return 1;
5301 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5302 GFP_KERNEL);
5303 if (!tpacpi_leds) {
5304 printk(TPACPI_ERR "Out of memory for LED data\n");
5305 return -ENOMEM;
5308 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5309 ARRAY_SIZE(led_useful_qtable));
5311 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5312 if (!tpacpi_is_led_restricted(i) &&
5313 test_bit(i, &useful_leds)) {
5314 rc = tpacpi_init_led(i);
5315 if (rc < 0) {
5316 led_exit();
5317 return rc;
5322 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5323 printk(TPACPI_NOTICE
5324 "warning: userspace override of important "
5325 "firmware LEDs is enabled\n");
5326 #endif
5327 return 0;
5330 #define str_led_status(s) \
5331 ((s) == TPACPI_LED_OFF ? "off" : \
5332 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5334 static int led_read(struct seq_file *m)
5336 if (!led_supported) {
5337 seq_printf(m, "status:\t\tnot supported\n");
5338 return 0;
5340 seq_printf(m, "status:\t\tsupported\n");
5342 if (led_supported == TPACPI_LED_570) {
5343 /* 570 */
5344 int i, status;
5345 for (i = 0; i < 8; i++) {
5346 status = led_get_status(i);
5347 if (status < 0)
5348 return -EIO;
5349 seq_printf(m, "%d:\t\t%s\n",
5350 i, str_led_status(status));
5354 seq_printf(m, "commands:\t"
5355 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5357 return 0;
5360 static int led_write(char *buf)
5362 char *cmd;
5363 int led, rc;
5364 enum led_status_t s;
5366 if (!led_supported)
5367 return -ENODEV;
5369 while ((cmd = next_cmd(&buf))) {
5370 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5371 return -EINVAL;
5373 if (strstr(cmd, "off")) {
5374 s = TPACPI_LED_OFF;
5375 } else if (strstr(cmd, "on")) {
5376 s = TPACPI_LED_ON;
5377 } else if (strstr(cmd, "blink")) {
5378 s = TPACPI_LED_BLINK;
5379 } else {
5380 return -EINVAL;
5383 rc = led_set_status(led, s);
5384 if (rc < 0)
5385 return rc;
5388 return 0;
5391 static struct ibm_struct led_driver_data = {
5392 .name = "led",
5393 .read = led_read,
5394 .write = led_write,
5395 .exit = led_exit,
5398 /*************************************************************************
5399 * Beep subdriver
5402 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5404 #define TPACPI_BEEP_Q1 0x0001
5406 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5407 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5408 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5411 static int __init beep_init(struct ibm_init_struct *iibm)
5413 unsigned long quirks;
5415 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5417 TPACPI_ACPIHANDLE_INIT(beep);
5419 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5420 str_supported(beep_handle != NULL));
5422 quirks = tpacpi_check_quirks(beep_quirk_table,
5423 ARRAY_SIZE(beep_quirk_table));
5425 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5427 return (beep_handle)? 0 : 1;
5430 static int beep_read(struct seq_file *m)
5432 if (!beep_handle)
5433 seq_printf(m, "status:\t\tnot supported\n");
5434 else {
5435 seq_printf(m, "status:\t\tsupported\n");
5436 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5439 return 0;
5442 static int beep_write(char *buf)
5444 char *cmd;
5445 int beep_cmd;
5447 if (!beep_handle)
5448 return -ENODEV;
5450 while ((cmd = next_cmd(&buf))) {
5451 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5452 beep_cmd >= 0 && beep_cmd <= 17) {
5453 /* beep_cmd set */
5454 } else
5455 return -EINVAL;
5456 if (tp_features.beep_needs_two_args) {
5457 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5458 beep_cmd, 0))
5459 return -EIO;
5460 } else {
5461 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5462 beep_cmd))
5463 return -EIO;
5467 return 0;
5470 static struct ibm_struct beep_driver_data = {
5471 .name = "beep",
5472 .read = beep_read,
5473 .write = beep_write,
5476 /*************************************************************************
5477 * Thermal subdriver
5480 enum thermal_access_mode {
5481 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5482 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5483 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5484 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5485 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5488 enum { /* TPACPI_THERMAL_TPEC_* */
5489 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5490 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5491 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5493 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5497 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5498 struct ibm_thermal_sensors_struct {
5499 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5502 static enum thermal_access_mode thermal_read_mode;
5504 /* idx is zero-based */
5505 static int thermal_get_sensor(int idx, s32 *value)
5507 int t;
5508 s8 tmp;
5509 char tmpi[5];
5511 t = TP_EC_THERMAL_TMP0;
5513 switch (thermal_read_mode) {
5514 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5515 case TPACPI_THERMAL_TPEC_16:
5516 if (idx >= 8 && idx <= 15) {
5517 t = TP_EC_THERMAL_TMP8;
5518 idx -= 8;
5520 /* fallthrough */
5521 #endif
5522 case TPACPI_THERMAL_TPEC_8:
5523 if (idx <= 7) {
5524 if (!acpi_ec_read(t + idx, &tmp))
5525 return -EIO;
5526 *value = tmp * 1000;
5527 return 0;
5529 break;
5531 case TPACPI_THERMAL_ACPI_UPDT:
5532 if (idx <= 7) {
5533 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5534 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5535 return -EIO;
5536 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5537 return -EIO;
5538 *value = (t - 2732) * 100;
5539 return 0;
5541 break;
5543 case TPACPI_THERMAL_ACPI_TMP07:
5544 if (idx <= 7) {
5545 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5546 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5547 return -EIO;
5548 if (t > 127 || t < -127)
5549 t = TP_EC_THERMAL_TMP_NA;
5550 *value = t * 1000;
5551 return 0;
5553 break;
5555 case TPACPI_THERMAL_NONE:
5556 default:
5557 return -ENOSYS;
5560 return -EINVAL;
5563 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5565 int res, i;
5566 int n;
5568 n = 8;
5569 i = 0;
5571 if (!s)
5572 return -EINVAL;
5574 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5575 n = 16;
5577 for (i = 0 ; i < n; i++) {
5578 res = thermal_get_sensor(i, &s->temp[i]);
5579 if (res)
5580 return res;
5583 return n;
5586 static void thermal_dump_all_sensors(void)
5588 int n, i;
5589 struct ibm_thermal_sensors_struct t;
5591 n = thermal_get_sensors(&t);
5592 if (n <= 0)
5593 return;
5595 printk(TPACPI_NOTICE
5596 "temperatures (Celsius):");
5598 for (i = 0; i < n; i++) {
5599 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5600 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5601 else
5602 printk(KERN_CONT " N/A");
5605 printk(KERN_CONT "\n");
5608 /* sysfs temp##_input -------------------------------------------------- */
5610 static ssize_t thermal_temp_input_show(struct device *dev,
5611 struct device_attribute *attr,
5612 char *buf)
5614 struct sensor_device_attribute *sensor_attr =
5615 to_sensor_dev_attr(attr);
5616 int idx = sensor_attr->index;
5617 s32 value;
5618 int res;
5620 res = thermal_get_sensor(idx, &value);
5621 if (res)
5622 return res;
5623 if (value == TPACPI_THERMAL_SENSOR_NA)
5624 return -ENXIO;
5626 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5629 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5630 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5631 thermal_temp_input_show, NULL, _idxB)
5633 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5634 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5635 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5636 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5637 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5638 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5639 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5640 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5641 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5642 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5643 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5644 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5645 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5646 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5647 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5648 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5649 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5652 #define THERMAL_ATTRS(X) \
5653 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5655 static struct attribute *thermal_temp_input_attr[] = {
5656 THERMAL_ATTRS(8),
5657 THERMAL_ATTRS(9),
5658 THERMAL_ATTRS(10),
5659 THERMAL_ATTRS(11),
5660 THERMAL_ATTRS(12),
5661 THERMAL_ATTRS(13),
5662 THERMAL_ATTRS(14),
5663 THERMAL_ATTRS(15),
5664 THERMAL_ATTRS(0),
5665 THERMAL_ATTRS(1),
5666 THERMAL_ATTRS(2),
5667 THERMAL_ATTRS(3),
5668 THERMAL_ATTRS(4),
5669 THERMAL_ATTRS(5),
5670 THERMAL_ATTRS(6),
5671 THERMAL_ATTRS(7),
5672 NULL
5675 static const struct attribute_group thermal_temp_input16_group = {
5676 .attrs = thermal_temp_input_attr
5679 static const struct attribute_group thermal_temp_input8_group = {
5680 .attrs = &thermal_temp_input_attr[8]
5683 #undef THERMAL_SENSOR_ATTR_TEMP
5684 #undef THERMAL_ATTRS
5686 /* --------------------------------------------------------------------- */
5688 static int __init thermal_init(struct ibm_init_struct *iibm)
5690 u8 t, ta1, ta2;
5691 int i;
5692 int acpi_tmp7;
5693 int res;
5695 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5697 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5699 if (thinkpad_id.ec_model) {
5701 * Direct EC access mode: sensors at registers
5702 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5703 * non-implemented, thermal sensors return 0x80 when
5704 * not available
5707 ta1 = ta2 = 0;
5708 for (i = 0; i < 8; i++) {
5709 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5710 ta1 |= t;
5711 } else {
5712 ta1 = 0;
5713 break;
5715 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5716 ta2 |= t;
5717 } else {
5718 ta1 = 0;
5719 break;
5722 if (ta1 == 0) {
5723 /* This is sheer paranoia, but we handle it anyway */
5724 if (acpi_tmp7) {
5725 printk(TPACPI_ERR
5726 "ThinkPad ACPI EC access misbehaving, "
5727 "falling back to ACPI TMPx access "
5728 "mode\n");
5729 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5730 } else {
5731 printk(TPACPI_ERR
5732 "ThinkPad ACPI EC access misbehaving, "
5733 "disabling thermal sensors access\n");
5734 thermal_read_mode = TPACPI_THERMAL_NONE;
5736 } else {
5737 thermal_read_mode =
5738 (ta2 != 0) ?
5739 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5741 } else if (acpi_tmp7) {
5742 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5743 /* 600e/x, 770e, 770x */
5744 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5745 } else {
5746 /* Standard ACPI TMPx access, max 8 sensors */
5747 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5749 } else {
5750 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5751 thermal_read_mode = TPACPI_THERMAL_NONE;
5754 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5755 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5756 thermal_read_mode);
5758 switch (thermal_read_mode) {
5759 case TPACPI_THERMAL_TPEC_16:
5760 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5761 &thermal_temp_input16_group);
5762 if (res)
5763 return res;
5764 break;
5765 case TPACPI_THERMAL_TPEC_8:
5766 case TPACPI_THERMAL_ACPI_TMP07:
5767 case TPACPI_THERMAL_ACPI_UPDT:
5768 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5769 &thermal_temp_input8_group);
5770 if (res)
5771 return res;
5772 break;
5773 case TPACPI_THERMAL_NONE:
5774 default:
5775 return 1;
5778 return 0;
5781 static void thermal_exit(void)
5783 switch (thermal_read_mode) {
5784 case TPACPI_THERMAL_TPEC_16:
5785 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5786 &thermal_temp_input16_group);
5787 break;
5788 case TPACPI_THERMAL_TPEC_8:
5789 case TPACPI_THERMAL_ACPI_TMP07:
5790 case TPACPI_THERMAL_ACPI_UPDT:
5791 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5792 &thermal_temp_input8_group);
5793 break;
5794 case TPACPI_THERMAL_NONE:
5795 default:
5796 break;
5800 static int thermal_read(struct seq_file *m)
5802 int n, i;
5803 struct ibm_thermal_sensors_struct t;
5805 n = thermal_get_sensors(&t);
5806 if (unlikely(n < 0))
5807 return n;
5809 seq_printf(m, "temperatures:\t");
5811 if (n > 0) {
5812 for (i = 0; i < (n - 1); i++)
5813 seq_printf(m, "%d ", t.temp[i] / 1000);
5814 seq_printf(m, "%d\n", t.temp[i] / 1000);
5815 } else
5816 seq_printf(m, "not supported\n");
5818 return 0;
5821 static struct ibm_struct thermal_driver_data = {
5822 .name = "thermal",
5823 .read = thermal_read,
5824 .exit = thermal_exit,
5827 /*************************************************************************
5828 * EC Dump subdriver
5831 static u8 ecdump_regs[256];
5833 static int ecdump_read(struct seq_file *m)
5835 int i, j;
5836 u8 v;
5838 seq_printf(m, "EC "
5839 " +00 +01 +02 +03 +04 +05 +06 +07"
5840 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5841 for (i = 0; i < 256; i += 16) {
5842 seq_printf(m, "EC 0x%02x:", i);
5843 for (j = 0; j < 16; j++) {
5844 if (!acpi_ec_read(i + j, &v))
5845 break;
5846 if (v != ecdump_regs[i + j])
5847 seq_printf(m, " *%02x", v);
5848 else
5849 seq_printf(m, " %02x", v);
5850 ecdump_regs[i + j] = v;
5852 seq_putc(m, '\n');
5853 if (j != 16)
5854 break;
5857 /* These are way too dangerous to advertise openly... */
5858 #if 0
5859 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5860 " (<offset> is 00-ff, <value> is 00-ff)\n");
5861 seq_printf(m, "commands:\t0x<offset> <value> "
5862 " (<offset> is 00-ff, <value> is 0-255)\n");
5863 #endif
5864 return 0;
5867 static int ecdump_write(char *buf)
5869 char *cmd;
5870 int i, v;
5872 while ((cmd = next_cmd(&buf))) {
5873 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5874 /* i and v set */
5875 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5876 /* i and v set */
5877 } else
5878 return -EINVAL;
5879 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5880 if (!acpi_ec_write(i, v))
5881 return -EIO;
5882 } else
5883 return -EINVAL;
5886 return 0;
5889 static struct ibm_struct ecdump_driver_data = {
5890 .name = "ecdump",
5891 .read = ecdump_read,
5892 .write = ecdump_write,
5893 .flags.experimental = 1,
5896 /*************************************************************************
5897 * Backlight/brightness subdriver
5900 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5903 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5904 * CMOS NVRAM byte 0x5E, bits 0-3.
5906 * EC HBRV (0x31) has the following layout
5907 * Bit 7: unknown function
5908 * Bit 6: unknown function
5909 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5910 * Bit 4: must be set to zero to avoid problems
5911 * Bit 3-0: backlight brightness level
5913 * brightness_get_raw returns status data in the HBRV layout
5915 * WARNING: The X61 has been verified to use HBRV for something else, so
5916 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5917 * testing on the very early *60 Lenovo models...
5920 enum {
5921 TP_EC_BACKLIGHT = 0x31,
5923 /* TP_EC_BACKLIGHT bitmasks */
5924 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5925 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5926 TP_EC_BACKLIGHT_MAPSW = 0x20,
5929 enum tpacpi_brightness_access_mode {
5930 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5931 TPACPI_BRGHT_MODE_EC, /* EC control */
5932 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5933 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5934 TPACPI_BRGHT_MODE_MAX
5937 static struct backlight_device *ibm_backlight_device;
5939 static enum tpacpi_brightness_access_mode brightness_mode =
5940 TPACPI_BRGHT_MODE_MAX;
5942 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5944 static struct mutex brightness_mutex;
5946 /* NVRAM brightness access,
5947 * call with brightness_mutex held! */
5948 static unsigned int tpacpi_brightness_nvram_get(void)
5950 u8 lnvram;
5952 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5953 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5954 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5955 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5957 return lnvram;
5960 static void tpacpi_brightness_checkpoint_nvram(void)
5962 u8 lec = 0;
5963 u8 b_nvram;
5965 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5966 return;
5968 vdbg_printk(TPACPI_DBG_BRGHT,
5969 "trying to checkpoint backlight level to NVRAM...\n");
5971 if (mutex_lock_killable(&brightness_mutex) < 0)
5972 return;
5974 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5975 goto unlock;
5976 lec &= TP_EC_BACKLIGHT_LVLMSK;
5977 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5979 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5980 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5981 /* NVRAM needs update */
5982 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5983 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5984 b_nvram |= lec;
5985 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5986 dbg_printk(TPACPI_DBG_BRGHT,
5987 "updated NVRAM backlight level to %u (0x%02x)\n",
5988 (unsigned int) lec, (unsigned int) b_nvram);
5989 } else
5990 vdbg_printk(TPACPI_DBG_BRGHT,
5991 "NVRAM backlight level already is %u (0x%02x)\n",
5992 (unsigned int) lec, (unsigned int) b_nvram);
5994 unlock:
5995 mutex_unlock(&brightness_mutex);
5999 /* call with brightness_mutex held! */
6000 static int tpacpi_brightness_get_raw(int *status)
6002 u8 lec = 0;
6004 switch (brightness_mode) {
6005 case TPACPI_BRGHT_MODE_UCMS_STEP:
6006 *status = tpacpi_brightness_nvram_get();
6007 return 0;
6008 case TPACPI_BRGHT_MODE_EC:
6009 case TPACPI_BRGHT_MODE_ECNVRAM:
6010 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6011 return -EIO;
6012 *status = lec;
6013 return 0;
6014 default:
6015 return -ENXIO;
6019 /* call with brightness_mutex held! */
6020 /* do NOT call with illegal backlight level value */
6021 static int tpacpi_brightness_set_ec(unsigned int value)
6023 u8 lec = 0;
6025 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6026 return -EIO;
6028 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6029 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6030 (value & TP_EC_BACKLIGHT_LVLMSK))))
6031 return -EIO;
6033 return 0;
6036 /* call with brightness_mutex held! */
6037 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6039 int cmos_cmd, inc;
6040 unsigned int current_value, i;
6042 current_value = tpacpi_brightness_nvram_get();
6044 if (value == current_value)
6045 return 0;
6047 cmos_cmd = (value > current_value) ?
6048 TP_CMOS_BRIGHTNESS_UP :
6049 TP_CMOS_BRIGHTNESS_DOWN;
6050 inc = (value > current_value) ? 1 : -1;
6052 for (i = current_value; i != value; i += inc)
6053 if (issue_thinkpad_cmos_command(cmos_cmd))
6054 return -EIO;
6056 return 0;
6059 /* May return EINTR which can always be mapped to ERESTARTSYS */
6060 static int brightness_set(unsigned int value)
6062 int res;
6064 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6065 value < 0)
6066 return -EINVAL;
6068 vdbg_printk(TPACPI_DBG_BRGHT,
6069 "set backlight level to %d\n", value);
6071 res = mutex_lock_killable(&brightness_mutex);
6072 if (res < 0)
6073 return res;
6075 switch (brightness_mode) {
6076 case TPACPI_BRGHT_MODE_EC:
6077 case TPACPI_BRGHT_MODE_ECNVRAM:
6078 res = tpacpi_brightness_set_ec(value);
6079 break;
6080 case TPACPI_BRGHT_MODE_UCMS_STEP:
6081 res = tpacpi_brightness_set_ucmsstep(value);
6082 break;
6083 default:
6084 res = -ENXIO;
6087 mutex_unlock(&brightness_mutex);
6088 return res;
6091 /* sysfs backlight class ----------------------------------------------- */
6093 static int brightness_update_status(struct backlight_device *bd)
6095 unsigned int level =
6096 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6097 bd->props.power == FB_BLANK_UNBLANK) ?
6098 bd->props.brightness : 0;
6100 dbg_printk(TPACPI_DBG_BRGHT,
6101 "backlight: attempt to set level to %d\n",
6102 level);
6104 /* it is the backlight class's job (caller) to handle
6105 * EINTR and other errors properly */
6106 return brightness_set(level);
6109 static int brightness_get(struct backlight_device *bd)
6111 int status, res;
6113 res = mutex_lock_killable(&brightness_mutex);
6114 if (res < 0)
6115 return 0;
6117 res = tpacpi_brightness_get_raw(&status);
6119 mutex_unlock(&brightness_mutex);
6121 if (res < 0)
6122 return 0;
6124 return status & TP_EC_BACKLIGHT_LVLMSK;
6127 static void tpacpi_brightness_notify_change(void)
6129 backlight_force_update(ibm_backlight_device,
6130 BACKLIGHT_UPDATE_HOTKEY);
6133 static struct backlight_ops ibm_backlight_data = {
6134 .get_brightness = brightness_get,
6135 .update_status = brightness_update_status,
6138 /* --------------------------------------------------------------------- */
6141 * These are only useful for models that have only one possibility
6142 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6143 * these quirks.
6145 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6146 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6147 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6149 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6150 /* Models with ATI GPUs known to require ECNVRAM mode */
6151 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6153 /* Models with ATI GPUs that can use ECNVRAM */
6154 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6155 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6156 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6157 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6159 /* Models with Intel Extreme Graphics 2 */
6160 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6161 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6162 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6164 /* Models with Intel GMA900 */
6165 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6166 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6167 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6170 static int __init brightness_init(struct ibm_init_struct *iibm)
6172 int b;
6173 unsigned long quirks;
6175 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6177 mutex_init(&brightness_mutex);
6179 quirks = tpacpi_check_quirks(brightness_quirk_table,
6180 ARRAY_SIZE(brightness_quirk_table));
6183 * We always attempt to detect acpi support, so as to switch
6184 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6185 * going to publish a backlight interface
6187 b = tpacpi_check_std_acpi_brightness_support();
6188 if (b > 0) {
6190 if (acpi_video_backlight_support()) {
6191 if (brightness_enable > 1) {
6192 printk(TPACPI_NOTICE
6193 "Standard ACPI backlight interface "
6194 "available, not loading native one.\n");
6195 return 1;
6196 } else if (brightness_enable == 1) {
6197 printk(TPACPI_NOTICE
6198 "Backlight control force enabled, even if standard "
6199 "ACPI backlight interface is available\n");
6201 } else {
6202 if (brightness_enable > 1) {
6203 printk(TPACPI_NOTICE
6204 "Standard ACPI backlight interface not "
6205 "available, thinkpad_acpi native "
6206 "brightness control enabled\n");
6211 if (!brightness_enable) {
6212 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6213 "brightness support disabled by "
6214 "module parameter\n");
6215 return 1;
6218 if (b > 16) {
6219 printk(TPACPI_ERR
6220 "Unsupported brightness interface, "
6221 "please contact %s\n", TPACPI_MAIL);
6222 return 1;
6224 if (b == 16)
6225 tp_features.bright_16levels = 1;
6228 * Check for module parameter bogosity, note that we
6229 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6230 * able to detect "unspecified"
6232 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6233 return -EINVAL;
6235 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6236 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6237 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6238 if (quirks & TPACPI_BRGHT_Q_EC)
6239 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6240 else
6241 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6243 dbg_printk(TPACPI_DBG_BRGHT,
6244 "driver auto-selected brightness_mode=%d\n",
6245 brightness_mode);
6248 /* Safety */
6249 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6250 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6251 brightness_mode == TPACPI_BRGHT_MODE_EC))
6252 return -EINVAL;
6254 if (tpacpi_brightness_get_raw(&b) < 0)
6255 return 1;
6257 if (tp_features.bright_16levels)
6258 printk(TPACPI_INFO
6259 "detected a 16-level brightness capable ThinkPad\n");
6261 ibm_backlight_device = backlight_device_register(
6262 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6263 &ibm_backlight_data);
6264 if (IS_ERR(ibm_backlight_device)) {
6265 int rc = PTR_ERR(ibm_backlight_device);
6266 ibm_backlight_device = NULL;
6267 printk(TPACPI_ERR "Could not register backlight device\n");
6268 return rc;
6270 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6271 "brightness is supported\n");
6273 if (quirks & TPACPI_BRGHT_Q_ASK) {
6274 printk(TPACPI_NOTICE
6275 "brightness: will use unverified default: "
6276 "brightness_mode=%d\n", brightness_mode);
6277 printk(TPACPI_NOTICE
6278 "brightness: please report to %s whether it works well "
6279 "or not on your ThinkPad\n", TPACPI_MAIL);
6282 ibm_backlight_device->props.max_brightness =
6283 (tp_features.bright_16levels)? 15 : 7;
6284 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6285 backlight_update_status(ibm_backlight_device);
6287 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6288 "brightness: registering brightness hotkeys "
6289 "as change notification\n");
6290 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6291 | TP_ACPI_HKEY_BRGHTUP_MASK
6292 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6293 return 0;
6296 static void brightness_suspend(pm_message_t state)
6298 tpacpi_brightness_checkpoint_nvram();
6301 static void brightness_shutdown(void)
6303 tpacpi_brightness_checkpoint_nvram();
6306 static void brightness_exit(void)
6308 if (ibm_backlight_device) {
6309 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6310 "calling backlight_device_unregister()\n");
6311 backlight_device_unregister(ibm_backlight_device);
6314 tpacpi_brightness_checkpoint_nvram();
6317 static int brightness_read(struct seq_file *m)
6319 int level;
6321 level = brightness_get(NULL);
6322 if (level < 0) {
6323 seq_printf(m, "level:\t\tunreadable\n");
6324 } else {
6325 seq_printf(m, "level:\t\t%d\n", level);
6326 seq_printf(m, "commands:\tup, down\n");
6327 seq_printf(m, "commands:\tlevel <level>"
6328 " (<level> is 0-%d)\n",
6329 (tp_features.bright_16levels) ? 15 : 7);
6332 return 0;
6335 static int brightness_write(char *buf)
6337 int level;
6338 int rc;
6339 char *cmd;
6340 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6342 level = brightness_get(NULL);
6343 if (level < 0)
6344 return level;
6346 while ((cmd = next_cmd(&buf))) {
6347 if (strlencmp(cmd, "up") == 0) {
6348 if (level < max_level)
6349 level++;
6350 } else if (strlencmp(cmd, "down") == 0) {
6351 if (level > 0)
6352 level--;
6353 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6354 level >= 0 && level <= max_level) {
6355 /* new level set */
6356 } else
6357 return -EINVAL;
6360 tpacpi_disclose_usertask("procfs brightness",
6361 "set level to %d\n", level);
6364 * Now we know what the final level should be, so we try to set it.
6365 * Doing it this way makes the syscall restartable in case of EINTR
6367 rc = brightness_set(level);
6368 if (!rc && ibm_backlight_device)
6369 backlight_force_update(ibm_backlight_device,
6370 BACKLIGHT_UPDATE_SYSFS);
6371 return (rc == -EINTR)? -ERESTARTSYS : rc;
6374 static struct ibm_struct brightness_driver_data = {
6375 .name = "brightness",
6376 .read = brightness_read,
6377 .write = brightness_write,
6378 .exit = brightness_exit,
6379 .suspend = brightness_suspend,
6380 .shutdown = brightness_shutdown,
6383 /*************************************************************************
6384 * Volume subdriver
6388 * IBM ThinkPads have a simple volume controller with MUTE gating.
6389 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6391 * Since the *61 series (and probably also the later *60 series), Lenovo
6392 * ThinkPads only implement the MUTE gate.
6394 * EC register 0x30
6395 * Bit 6: MUTE (1 mutes sound)
6396 * Bit 3-0: Volume
6397 * Other bits should be zero as far as we know.
6399 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6400 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6401 * such as bit 7 which is used to detect repeated presses of MUTE,
6402 * and we leave them unchanged.
6405 enum {
6406 TP_EC_AUDIO = 0x30,
6408 /* TP_EC_AUDIO bits */
6409 TP_EC_AUDIO_MUTESW = 6,
6411 /* TP_EC_AUDIO bitmasks */
6412 TP_EC_AUDIO_LVL_MSK = 0x0F,
6413 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6415 /* Maximum volume */
6416 TP_EC_VOLUME_MAX = 14,
6419 enum tpacpi_volume_access_mode {
6420 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6421 TPACPI_VOL_MODE_EC, /* Pure EC control */
6422 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6423 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6424 TPACPI_VOL_MODE_MAX
6427 enum tpacpi_volume_capabilities {
6428 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6429 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6430 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6431 TPACPI_VOL_CAP_MAX
6434 static enum tpacpi_volume_access_mode volume_mode =
6435 TPACPI_VOL_MODE_MAX;
6437 static enum tpacpi_volume_capabilities volume_capabilities;
6440 * Used to syncronize writers to TP_EC_AUDIO and
6441 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6443 static struct mutex volume_mutex;
6445 static void tpacpi_volume_checkpoint_nvram(void)
6447 u8 lec = 0;
6448 u8 b_nvram;
6449 u8 ec_mask;
6451 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6452 return;
6454 vdbg_printk(TPACPI_DBG_MIXER,
6455 "trying to checkpoint mixer state to NVRAM...\n");
6457 if (tp_features.mixer_no_level_control)
6458 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6459 else
6460 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6462 if (mutex_lock_killable(&volume_mutex) < 0)
6463 return;
6465 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6466 goto unlock;
6467 lec &= ec_mask;
6468 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6470 if (lec != (b_nvram & ec_mask)) {
6471 /* NVRAM needs update */
6472 b_nvram &= ~ec_mask;
6473 b_nvram |= lec;
6474 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6475 dbg_printk(TPACPI_DBG_MIXER,
6476 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6477 (unsigned int) lec, (unsigned int) b_nvram);
6478 } else {
6479 vdbg_printk(TPACPI_DBG_MIXER,
6480 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6481 (unsigned int) lec, (unsigned int) b_nvram);
6484 unlock:
6485 mutex_unlock(&volume_mutex);
6488 static int volume_get_status_ec(u8 *status)
6490 u8 s;
6492 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6493 return -EIO;
6495 *status = s;
6497 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6499 return 0;
6502 static int volume_get_status(u8 *status)
6504 return volume_get_status_ec(status);
6507 static int volume_set_status_ec(const u8 status)
6509 if (!acpi_ec_write(TP_EC_AUDIO, status))
6510 return -EIO;
6512 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6514 return 0;
6517 static int volume_set_status(const u8 status)
6519 return volume_set_status_ec(status);
6522 static int volume_set_mute_ec(const bool mute)
6524 int rc;
6525 u8 s, n;
6527 if (mutex_lock_killable(&volume_mutex) < 0)
6528 return -EINTR;
6530 rc = volume_get_status_ec(&s);
6531 if (rc)
6532 goto unlock;
6534 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6535 s & ~TP_EC_AUDIO_MUTESW_MSK;
6537 if (n != s)
6538 rc = volume_set_status_ec(n);
6540 unlock:
6541 mutex_unlock(&volume_mutex);
6542 return rc;
6545 static int volume_set_mute(const bool mute)
6547 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6548 (mute) ? "" : "un");
6549 return volume_set_mute_ec(mute);
6552 static int volume_set_volume_ec(const u8 vol)
6554 int rc;
6555 u8 s, n;
6557 if (vol > TP_EC_VOLUME_MAX)
6558 return -EINVAL;
6560 if (mutex_lock_killable(&volume_mutex) < 0)
6561 return -EINTR;
6563 rc = volume_get_status_ec(&s);
6564 if (rc)
6565 goto unlock;
6567 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6569 if (n != s)
6570 rc = volume_set_status_ec(n);
6572 unlock:
6573 mutex_unlock(&volume_mutex);
6574 return rc;
6577 static int volume_set_volume(const u8 vol)
6579 dbg_printk(TPACPI_DBG_MIXER,
6580 "trying to set volume level to %hu\n", vol);
6581 return volume_set_volume_ec(vol);
6584 static void volume_suspend(pm_message_t state)
6586 tpacpi_volume_checkpoint_nvram();
6589 static void volume_shutdown(void)
6591 tpacpi_volume_checkpoint_nvram();
6594 static void volume_exit(void)
6596 tpacpi_volume_checkpoint_nvram();
6599 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6600 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6602 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6603 /* Whitelist volume level on all IBM by default */
6604 { .vendor = PCI_VENDOR_ID_IBM,
6605 .bios = TPACPI_MATCH_ANY,
6606 .ec = TPACPI_MATCH_ANY,
6607 .quirks = TPACPI_VOL_Q_LEVEL },
6609 /* Lenovo models with volume control (needs confirmation) */
6610 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6611 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6612 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6613 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6614 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6615 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6616 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6618 /* Whitelist mute-only on all Lenovo by default */
6619 { .vendor = PCI_VENDOR_ID_LENOVO,
6620 .bios = TPACPI_MATCH_ANY,
6621 .ec = TPACPI_MATCH_ANY,
6622 .quirks = TPACPI_VOL_Q_MUTEONLY }
6625 static int __init volume_init(struct ibm_init_struct *iibm)
6627 unsigned long quirks;
6629 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6631 mutex_init(&volume_mutex);
6634 * Check for module parameter bogosity, note that we
6635 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6636 * able to detect "unspecified"
6638 if (volume_mode > TPACPI_VOL_MODE_MAX)
6639 return -EINVAL;
6641 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6642 printk(TPACPI_ERR
6643 "UCMS step volume mode not implemented, "
6644 "please contact %s\n", TPACPI_MAIL);
6645 return 1;
6648 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6649 return -EINVAL;
6651 quirks = tpacpi_check_quirks(volume_quirk_table,
6652 ARRAY_SIZE(volume_quirk_table));
6654 switch (volume_capabilities) {
6655 case TPACPI_VOL_CAP_AUTO:
6656 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6657 tp_features.mixer_no_level_control = 1;
6658 else if (quirks & TPACPI_VOL_Q_LEVEL)
6659 tp_features.mixer_no_level_control = 0;
6660 else
6661 return 1; /* no mixer */
6662 break;
6663 case TPACPI_VOL_CAP_VOLMUTE:
6664 tp_features.mixer_no_level_control = 0;
6665 break;
6666 case TPACPI_VOL_CAP_MUTEONLY:
6667 tp_features.mixer_no_level_control = 1;
6668 break;
6669 default:
6670 return 1;
6673 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6674 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6675 "using user-supplied volume_capabilities=%d\n",
6676 volume_capabilities);
6678 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6679 volume_mode == TPACPI_VOL_MODE_MAX) {
6680 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6682 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6683 "driver auto-selected volume_mode=%d\n",
6684 volume_mode);
6685 } else {
6686 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6687 "using user-supplied volume_mode=%d\n",
6688 volume_mode);
6691 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6692 "mute is supported, volume control is %s\n",
6693 str_supported(!tp_features.mixer_no_level_control));
6695 return 0;
6698 static int volume_read(struct seq_file *m)
6700 u8 status;
6702 if (volume_get_status(&status) < 0) {
6703 seq_printf(m, "level:\t\tunreadable\n");
6704 } else {
6705 if (tp_features.mixer_no_level_control)
6706 seq_printf(m, "level:\t\tunsupported\n");
6707 else
6708 seq_printf(m, "level:\t\t%d\n",
6709 status & TP_EC_AUDIO_LVL_MSK);
6711 seq_printf(m, "mute:\t\t%s\n",
6712 onoff(status, TP_EC_AUDIO_MUTESW));
6714 seq_printf(m, "commands:\tunmute, mute\n");
6715 if (!tp_features.mixer_no_level_control) {
6716 seq_printf(m, "commands:\tup, down\n");
6717 seq_printf(m, "commands:\tlevel <level>"
6718 " (<level> is 0-%d)\n", TP_EC_VOLUME_MAX);
6722 return 0;
6725 static int volume_write(char *buf)
6727 u8 s;
6728 u8 new_level, new_mute;
6729 int l;
6730 char *cmd;
6731 int rc;
6733 rc = volume_get_status(&s);
6734 if (rc < 0)
6735 return rc;
6737 new_level = s & TP_EC_AUDIO_LVL_MSK;
6738 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
6740 while ((cmd = next_cmd(&buf))) {
6741 if (!tp_features.mixer_no_level_control) {
6742 if (strlencmp(cmd, "up") == 0) {
6743 if (new_mute)
6744 new_mute = 0;
6745 else if (new_level < TP_EC_VOLUME_MAX)
6746 new_level++;
6747 continue;
6748 } else if (strlencmp(cmd, "down") == 0) {
6749 if (new_mute)
6750 new_mute = 0;
6751 else if (new_level > 0)
6752 new_level--;
6753 continue;
6754 } else if (sscanf(cmd, "level %u", &l) == 1 &&
6755 l >= 0 && l <= TP_EC_VOLUME_MAX) {
6756 new_level = l;
6757 continue;
6760 if (strlencmp(cmd, "mute") == 0)
6761 new_mute = TP_EC_AUDIO_MUTESW_MSK;
6762 else if (strlencmp(cmd, "unmute") == 0)
6763 new_mute = 0;
6764 else
6765 return -EINVAL;
6768 if (tp_features.mixer_no_level_control) {
6769 tpacpi_disclose_usertask("procfs volume", "%smute\n",
6770 new_mute ? "" : "un");
6771 rc = volume_set_mute(!!new_mute);
6772 } else {
6773 tpacpi_disclose_usertask("procfs volume",
6774 "%smute and set level to %d\n",
6775 new_mute ? "" : "un", new_level);
6776 rc = volume_set_status(new_mute | new_level);
6779 return (rc == -EINTR) ? -ERESTARTSYS : rc;
6782 static struct ibm_struct volume_driver_data = {
6783 .name = "volume",
6784 .read = volume_read,
6785 .write = volume_write,
6786 .exit = volume_exit,
6787 .suspend = volume_suspend,
6788 .shutdown = volume_shutdown,
6791 /*************************************************************************
6792 * Fan subdriver
6796 * FAN ACCESS MODES
6798 * TPACPI_FAN_RD_ACPI_GFAN:
6799 * ACPI GFAN method: returns fan level
6801 * see TPACPI_FAN_WR_ACPI_SFAN
6802 * EC 0x2f (HFSP) not available if GFAN exists
6804 * TPACPI_FAN_WR_ACPI_SFAN:
6805 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6807 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6808 * it for writing.
6810 * TPACPI_FAN_WR_TPEC:
6811 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6812 * Supported on almost all ThinkPads
6814 * Fan speed changes of any sort (including those caused by the
6815 * disengaged mode) are usually done slowly by the firmware as the
6816 * maximum ammount of fan duty cycle change per second seems to be
6817 * limited.
6819 * Reading is not available if GFAN exists.
6820 * Writing is not available if SFAN exists.
6822 * Bits
6823 * 7 automatic mode engaged;
6824 * (default operation mode of the ThinkPad)
6825 * fan level is ignored in this mode.
6826 * 6 full speed mode (takes precedence over bit 7);
6827 * not available on all thinkpads. May disable
6828 * the tachometer while the fan controller ramps up
6829 * the speed (which can take up to a few *minutes*).
6830 * Speeds up fan to 100% duty-cycle, which is far above
6831 * the standard RPM levels. It is not impossible that
6832 * it could cause hardware damage.
6833 * 5-3 unused in some models. Extra bits for fan level
6834 * in others, but still useless as all values above
6835 * 7 map to the same speed as level 7 in these models.
6836 * 2-0 fan level (0..7 usually)
6837 * 0x00 = stop
6838 * 0x07 = max (set when temperatures critical)
6839 * Some ThinkPads may have other levels, see
6840 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6842 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6843 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6844 * does so, its initial value is meaningless (0x07).
6846 * For firmware bugs, refer to:
6847 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6849 * ----
6851 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6852 * Main fan tachometer reading (in RPM)
6854 * This register is present on all ThinkPads with a new-style EC, and
6855 * it is known not to be present on the A21m/e, and T22, as there is
6856 * something else in offset 0x84 according to the ACPI DSDT. Other
6857 * ThinkPads from this same time period (and earlier) probably lack the
6858 * tachometer as well.
6860 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6861 * was never fixed by IBM to report the EC firmware version string
6862 * probably support the tachometer (like the early X models), so
6863 * detecting it is quite hard. We need more data to know for sure.
6865 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6866 * might result.
6868 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6869 * mode.
6871 * For firmware bugs, refer to:
6872 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6874 * ----
6876 * ThinkPad EC register 0x31 bit 0 (only on select models)
6878 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6879 * show the speed of the main fan. When bit 0 of EC register 0x31
6880 * is one, the tachometer registers show the speed of the auxiliary
6881 * fan.
6883 * Fan control seems to affect both fans, regardless of the state
6884 * of this bit.
6886 * So far, only the firmware for the X60/X61 non-tablet versions
6887 * seem to support this (firmware TP-7M).
6889 * TPACPI_FAN_WR_ACPI_FANS:
6890 * ThinkPad X31, X40, X41. Not available in the X60.
6892 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6893 * high speed. ACPI DSDT seems to map these three speeds to levels
6894 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6895 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6897 * The speeds are stored on handles
6898 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6900 * There are three default speed sets, acessible as handles:
6901 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6903 * ACPI DSDT switches which set is in use depending on various
6904 * factors.
6906 * TPACPI_FAN_WR_TPEC is also available and should be used to
6907 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6908 * but the ACPI tables just mention level 7.
6911 enum { /* Fan control constants */
6912 fan_status_offset = 0x2f, /* EC register 0x2f */
6913 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6914 * 0x84 must be read before 0x85 */
6915 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6916 bit 0 selects which fan is active */
6918 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6919 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6921 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6924 enum fan_status_access_mode {
6925 TPACPI_FAN_NONE = 0, /* No fan status or control */
6926 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6927 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6930 enum fan_control_access_mode {
6931 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6932 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6933 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6934 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6937 enum fan_control_commands {
6938 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6939 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6940 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6941 * and also watchdog cmd */
6944 static int fan_control_allowed;
6946 static enum fan_status_access_mode fan_status_access_mode;
6947 static enum fan_control_access_mode fan_control_access_mode;
6948 static enum fan_control_commands fan_control_commands;
6950 static u8 fan_control_initial_status;
6951 static u8 fan_control_desired_level;
6952 static u8 fan_control_resume_level;
6953 static int fan_watchdog_maxinterval;
6955 static struct mutex fan_mutex;
6957 static void fan_watchdog_fire(struct work_struct *ignored);
6958 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6960 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6961 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6962 "\\FSPD", /* 600e/x, 770e, 770x */
6963 ); /* all others */
6964 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6965 "JFNS", /* 770x-JL */
6966 ); /* all others */
6969 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6970 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6971 * be in auto mode (0x80).
6973 * This is corrected by any write to HFSP either by the driver, or
6974 * by the firmware.
6976 * We assume 0x07 really means auto mode while this quirk is active,
6977 * as this is far more likely than the ThinkPad being in level 7,
6978 * which is only used by the firmware during thermal emergencies.
6980 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6981 * TP-70 (T43, R52), which are known to be buggy.
6984 static void fan_quirk1_setup(void)
6986 if (fan_control_initial_status == 0x07) {
6987 printk(TPACPI_NOTICE
6988 "fan_init: initial fan status is unknown, "
6989 "assuming it is in auto mode\n");
6990 tp_features.fan_ctrl_status_undef = 1;
6994 static void fan_quirk1_handle(u8 *fan_status)
6996 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6997 if (*fan_status != fan_control_initial_status) {
6998 /* something changed the HFSP regisnter since
6999 * driver init time, so it is not undefined
7000 * anymore */
7001 tp_features.fan_ctrl_status_undef = 0;
7002 } else {
7003 /* Return most likely status. In fact, it
7004 * might be the only possible status */
7005 *fan_status = TP_EC_FAN_AUTO;
7010 /* Select main fan on X60/X61, NOOP on others */
7011 static bool fan_select_fan1(void)
7013 if (tp_features.second_fan) {
7014 u8 val;
7016 if (ec_read(fan_select_offset, &val) < 0)
7017 return false;
7018 val &= 0xFEU;
7019 if (ec_write(fan_select_offset, val) < 0)
7020 return false;
7022 return true;
7025 /* Select secondary fan on X60/X61 */
7026 static bool fan_select_fan2(void)
7028 u8 val;
7030 if (!tp_features.second_fan)
7031 return false;
7033 if (ec_read(fan_select_offset, &val) < 0)
7034 return false;
7035 val |= 0x01U;
7036 if (ec_write(fan_select_offset, val) < 0)
7037 return false;
7039 return true;
7043 * Call with fan_mutex held
7045 static void fan_update_desired_level(u8 status)
7047 if ((status &
7048 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7049 if (status > 7)
7050 fan_control_desired_level = 7;
7051 else
7052 fan_control_desired_level = status;
7056 static int fan_get_status(u8 *status)
7058 u8 s;
7060 /* TODO:
7061 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7063 switch (fan_status_access_mode) {
7064 case TPACPI_FAN_RD_ACPI_GFAN:
7065 /* 570, 600e/x, 770e, 770x */
7067 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7068 return -EIO;
7070 if (likely(status))
7071 *status = s & 0x07;
7073 break;
7075 case TPACPI_FAN_RD_TPEC:
7076 /* all except 570, 600e/x, 770e, 770x */
7077 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7078 return -EIO;
7080 if (likely(status)) {
7081 *status = s;
7082 fan_quirk1_handle(status);
7085 break;
7087 default:
7088 return -ENXIO;
7091 return 0;
7094 static int fan_get_status_safe(u8 *status)
7096 int rc;
7097 u8 s;
7099 if (mutex_lock_killable(&fan_mutex))
7100 return -ERESTARTSYS;
7101 rc = fan_get_status(&s);
7102 if (!rc)
7103 fan_update_desired_level(s);
7104 mutex_unlock(&fan_mutex);
7106 if (status)
7107 *status = s;
7109 return rc;
7112 static int fan_get_speed(unsigned int *speed)
7114 u8 hi, lo;
7116 switch (fan_status_access_mode) {
7117 case TPACPI_FAN_RD_TPEC:
7118 /* all except 570, 600e/x, 770e, 770x */
7119 if (unlikely(!fan_select_fan1()))
7120 return -EIO;
7121 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7122 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7123 return -EIO;
7125 if (likely(speed))
7126 *speed = (hi << 8) | lo;
7128 break;
7130 default:
7131 return -ENXIO;
7134 return 0;
7137 static int fan2_get_speed(unsigned int *speed)
7139 u8 hi, lo;
7140 bool rc;
7142 switch (fan_status_access_mode) {
7143 case TPACPI_FAN_RD_TPEC:
7144 /* all except 570, 600e/x, 770e, 770x */
7145 if (unlikely(!fan_select_fan2()))
7146 return -EIO;
7147 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7148 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7149 fan_select_fan1(); /* play it safe */
7150 if (rc)
7151 return -EIO;
7153 if (likely(speed))
7154 *speed = (hi << 8) | lo;
7156 break;
7158 default:
7159 return -ENXIO;
7162 return 0;
7165 static int fan_set_level(int level)
7167 if (!fan_control_allowed)
7168 return -EPERM;
7170 switch (fan_control_access_mode) {
7171 case TPACPI_FAN_WR_ACPI_SFAN:
7172 if (level >= 0 && level <= 7) {
7173 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7174 return -EIO;
7175 } else
7176 return -EINVAL;
7177 break;
7179 case TPACPI_FAN_WR_ACPI_FANS:
7180 case TPACPI_FAN_WR_TPEC:
7181 if (!(level & TP_EC_FAN_AUTO) &&
7182 !(level & TP_EC_FAN_FULLSPEED) &&
7183 ((level < 0) || (level > 7)))
7184 return -EINVAL;
7186 /* safety net should the EC not support AUTO
7187 * or FULLSPEED mode bits and just ignore them */
7188 if (level & TP_EC_FAN_FULLSPEED)
7189 level |= 7; /* safety min speed 7 */
7190 else if (level & TP_EC_FAN_AUTO)
7191 level |= 4; /* safety min speed 4 */
7193 if (!acpi_ec_write(fan_status_offset, level))
7194 return -EIO;
7195 else
7196 tp_features.fan_ctrl_status_undef = 0;
7197 break;
7199 default:
7200 return -ENXIO;
7203 vdbg_printk(TPACPI_DBG_FAN,
7204 "fan control: set fan control register to 0x%02x\n", level);
7205 return 0;
7208 static int fan_set_level_safe(int level)
7210 int rc;
7212 if (!fan_control_allowed)
7213 return -EPERM;
7215 if (mutex_lock_killable(&fan_mutex))
7216 return -ERESTARTSYS;
7218 if (level == TPACPI_FAN_LAST_LEVEL)
7219 level = fan_control_desired_level;
7221 rc = fan_set_level(level);
7222 if (!rc)
7223 fan_update_desired_level(level);
7225 mutex_unlock(&fan_mutex);
7226 return rc;
7229 static int fan_set_enable(void)
7231 u8 s;
7232 int rc;
7234 if (!fan_control_allowed)
7235 return -EPERM;
7237 if (mutex_lock_killable(&fan_mutex))
7238 return -ERESTARTSYS;
7240 switch (fan_control_access_mode) {
7241 case TPACPI_FAN_WR_ACPI_FANS:
7242 case TPACPI_FAN_WR_TPEC:
7243 rc = fan_get_status(&s);
7244 if (rc < 0)
7245 break;
7247 /* Don't go out of emergency fan mode */
7248 if (s != 7) {
7249 s &= 0x07;
7250 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7253 if (!acpi_ec_write(fan_status_offset, s))
7254 rc = -EIO;
7255 else {
7256 tp_features.fan_ctrl_status_undef = 0;
7257 rc = 0;
7259 break;
7261 case TPACPI_FAN_WR_ACPI_SFAN:
7262 rc = fan_get_status(&s);
7263 if (rc < 0)
7264 break;
7266 s &= 0x07;
7268 /* Set fan to at least level 4 */
7269 s |= 4;
7271 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7272 rc = -EIO;
7273 else
7274 rc = 0;
7275 break;
7277 default:
7278 rc = -ENXIO;
7281 mutex_unlock(&fan_mutex);
7283 if (!rc)
7284 vdbg_printk(TPACPI_DBG_FAN,
7285 "fan control: set fan control register to 0x%02x\n",
7287 return rc;
7290 static int fan_set_disable(void)
7292 int rc;
7294 if (!fan_control_allowed)
7295 return -EPERM;
7297 if (mutex_lock_killable(&fan_mutex))
7298 return -ERESTARTSYS;
7300 rc = 0;
7301 switch (fan_control_access_mode) {
7302 case TPACPI_FAN_WR_ACPI_FANS:
7303 case TPACPI_FAN_WR_TPEC:
7304 if (!acpi_ec_write(fan_status_offset, 0x00))
7305 rc = -EIO;
7306 else {
7307 fan_control_desired_level = 0;
7308 tp_features.fan_ctrl_status_undef = 0;
7310 break;
7312 case TPACPI_FAN_WR_ACPI_SFAN:
7313 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7314 rc = -EIO;
7315 else
7316 fan_control_desired_level = 0;
7317 break;
7319 default:
7320 rc = -ENXIO;
7323 if (!rc)
7324 vdbg_printk(TPACPI_DBG_FAN,
7325 "fan control: set fan control register to 0\n");
7327 mutex_unlock(&fan_mutex);
7328 return rc;
7331 static int fan_set_speed(int speed)
7333 int rc;
7335 if (!fan_control_allowed)
7336 return -EPERM;
7338 if (mutex_lock_killable(&fan_mutex))
7339 return -ERESTARTSYS;
7341 rc = 0;
7342 switch (fan_control_access_mode) {
7343 case TPACPI_FAN_WR_ACPI_FANS:
7344 if (speed >= 0 && speed <= 65535) {
7345 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7346 speed, speed, speed))
7347 rc = -EIO;
7348 } else
7349 rc = -EINVAL;
7350 break;
7352 default:
7353 rc = -ENXIO;
7356 mutex_unlock(&fan_mutex);
7357 return rc;
7360 static void fan_watchdog_reset(void)
7362 static int fan_watchdog_active;
7364 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7365 return;
7367 if (fan_watchdog_active)
7368 cancel_delayed_work(&fan_watchdog_task);
7370 if (fan_watchdog_maxinterval > 0 &&
7371 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7372 fan_watchdog_active = 1;
7373 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7374 msecs_to_jiffies(fan_watchdog_maxinterval
7375 * 1000))) {
7376 printk(TPACPI_ERR
7377 "failed to queue the fan watchdog, "
7378 "watchdog will not trigger\n");
7380 } else
7381 fan_watchdog_active = 0;
7384 static void fan_watchdog_fire(struct work_struct *ignored)
7386 int rc;
7388 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7389 return;
7391 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7392 rc = fan_set_enable();
7393 if (rc < 0) {
7394 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7395 "will try again later...\n", -rc);
7396 /* reschedule for later */
7397 fan_watchdog_reset();
7402 * SYSFS fan layout: hwmon compatible (device)
7404 * pwm*_enable:
7405 * 0: "disengaged" mode
7406 * 1: manual mode
7407 * 2: native EC "auto" mode (recommended, hardware default)
7409 * pwm*: set speed in manual mode, ignored otherwise.
7410 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7411 * interpolation.
7413 * fan*_input: tachometer reading, RPM
7416 * SYSFS fan layout: extensions
7418 * fan_watchdog (driver):
7419 * fan watchdog interval in seconds, 0 disables (default), max 120
7422 /* sysfs fan pwm1_enable ----------------------------------------------- */
7423 static ssize_t fan_pwm1_enable_show(struct device *dev,
7424 struct device_attribute *attr,
7425 char *buf)
7427 int res, mode;
7428 u8 status;
7430 res = fan_get_status_safe(&status);
7431 if (res)
7432 return res;
7434 if (status & TP_EC_FAN_FULLSPEED) {
7435 mode = 0;
7436 } else if (status & TP_EC_FAN_AUTO) {
7437 mode = 2;
7438 } else
7439 mode = 1;
7441 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7444 static ssize_t fan_pwm1_enable_store(struct device *dev,
7445 struct device_attribute *attr,
7446 const char *buf, size_t count)
7448 unsigned long t;
7449 int res, level;
7451 if (parse_strtoul(buf, 2, &t))
7452 return -EINVAL;
7454 tpacpi_disclose_usertask("hwmon pwm1_enable",
7455 "set fan mode to %lu\n", t);
7457 switch (t) {
7458 case 0:
7459 level = TP_EC_FAN_FULLSPEED;
7460 break;
7461 case 1:
7462 level = TPACPI_FAN_LAST_LEVEL;
7463 break;
7464 case 2:
7465 level = TP_EC_FAN_AUTO;
7466 break;
7467 case 3:
7468 /* reserved for software-controlled auto mode */
7469 return -ENOSYS;
7470 default:
7471 return -EINVAL;
7474 res = fan_set_level_safe(level);
7475 if (res == -ENXIO)
7476 return -EINVAL;
7477 else if (res < 0)
7478 return res;
7480 fan_watchdog_reset();
7482 return count;
7485 static struct device_attribute dev_attr_fan_pwm1_enable =
7486 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7487 fan_pwm1_enable_show, fan_pwm1_enable_store);
7489 /* sysfs fan pwm1 ------------------------------------------------------ */
7490 static ssize_t fan_pwm1_show(struct device *dev,
7491 struct device_attribute *attr,
7492 char *buf)
7494 int res;
7495 u8 status;
7497 res = fan_get_status_safe(&status);
7498 if (res)
7499 return res;
7501 if ((status &
7502 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7503 status = fan_control_desired_level;
7505 if (status > 7)
7506 status = 7;
7508 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7511 static ssize_t fan_pwm1_store(struct device *dev,
7512 struct device_attribute *attr,
7513 const char *buf, size_t count)
7515 unsigned long s;
7516 int rc;
7517 u8 status, newlevel;
7519 if (parse_strtoul(buf, 255, &s))
7520 return -EINVAL;
7522 tpacpi_disclose_usertask("hwmon pwm1",
7523 "set fan speed to %lu\n", s);
7525 /* scale down from 0-255 to 0-7 */
7526 newlevel = (s >> 5) & 0x07;
7528 if (mutex_lock_killable(&fan_mutex))
7529 return -ERESTARTSYS;
7531 rc = fan_get_status(&status);
7532 if (!rc && (status &
7533 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7534 rc = fan_set_level(newlevel);
7535 if (rc == -ENXIO)
7536 rc = -EINVAL;
7537 else if (!rc) {
7538 fan_update_desired_level(newlevel);
7539 fan_watchdog_reset();
7543 mutex_unlock(&fan_mutex);
7544 return (rc)? rc : count;
7547 static struct device_attribute dev_attr_fan_pwm1 =
7548 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7549 fan_pwm1_show, fan_pwm1_store);
7551 /* sysfs fan fan1_input ------------------------------------------------ */
7552 static ssize_t fan_fan1_input_show(struct device *dev,
7553 struct device_attribute *attr,
7554 char *buf)
7556 int res;
7557 unsigned int speed;
7559 res = fan_get_speed(&speed);
7560 if (res < 0)
7561 return res;
7563 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7566 static struct device_attribute dev_attr_fan_fan1_input =
7567 __ATTR(fan1_input, S_IRUGO,
7568 fan_fan1_input_show, NULL);
7570 /* sysfs fan fan2_input ------------------------------------------------ */
7571 static ssize_t fan_fan2_input_show(struct device *dev,
7572 struct device_attribute *attr,
7573 char *buf)
7575 int res;
7576 unsigned int speed;
7578 res = fan2_get_speed(&speed);
7579 if (res < 0)
7580 return res;
7582 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7585 static struct device_attribute dev_attr_fan_fan2_input =
7586 __ATTR(fan2_input, S_IRUGO,
7587 fan_fan2_input_show, NULL);
7589 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7590 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7591 char *buf)
7593 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7596 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7597 const char *buf, size_t count)
7599 unsigned long t;
7601 if (parse_strtoul(buf, 120, &t))
7602 return -EINVAL;
7604 if (!fan_control_allowed)
7605 return -EPERM;
7607 fan_watchdog_maxinterval = t;
7608 fan_watchdog_reset();
7610 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7612 return count;
7615 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7616 fan_fan_watchdog_show, fan_fan_watchdog_store);
7618 /* --------------------------------------------------------------------- */
7619 static struct attribute *fan_attributes[] = {
7620 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7621 &dev_attr_fan_fan1_input.attr,
7622 NULL, /* for fan2_input */
7623 NULL
7626 static const struct attribute_group fan_attr_group = {
7627 .attrs = fan_attributes,
7630 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7631 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7633 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7634 { .vendor = PCI_VENDOR_ID_IBM, \
7635 .bios = TPACPI_MATCH_ANY, \
7636 .ec = TPID(__id1, __id2), \
7637 .quirks = __quirks }
7639 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7640 { .vendor = PCI_VENDOR_ID_LENOVO, \
7641 .bios = TPACPI_MATCH_ANY, \
7642 .ec = TPID(__id1, __id2), \
7643 .quirks = __quirks }
7645 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7646 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7647 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7648 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7649 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7650 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7653 #undef TPACPI_FAN_QL
7654 #undef TPACPI_FAN_QI
7656 static int __init fan_init(struct ibm_init_struct *iibm)
7658 int rc;
7659 unsigned long quirks;
7661 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7662 "initializing fan subdriver\n");
7664 mutex_init(&fan_mutex);
7665 fan_status_access_mode = TPACPI_FAN_NONE;
7666 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7667 fan_control_commands = 0;
7668 fan_watchdog_maxinterval = 0;
7669 tp_features.fan_ctrl_status_undef = 0;
7670 tp_features.second_fan = 0;
7671 fan_control_desired_level = 7;
7673 TPACPI_ACPIHANDLE_INIT(fans);
7674 TPACPI_ACPIHANDLE_INIT(gfan);
7675 TPACPI_ACPIHANDLE_INIT(sfan);
7677 quirks = tpacpi_check_quirks(fan_quirk_table,
7678 ARRAY_SIZE(fan_quirk_table));
7680 if (gfan_handle) {
7681 /* 570, 600e/x, 770e, 770x */
7682 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7683 } else {
7684 /* all other ThinkPads: note that even old-style
7685 * ThinkPad ECs supports the fan control register */
7686 if (likely(acpi_ec_read(fan_status_offset,
7687 &fan_control_initial_status))) {
7688 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7689 if (quirks & TPACPI_FAN_Q1)
7690 fan_quirk1_setup();
7691 if (quirks & TPACPI_FAN_2FAN) {
7692 tp_features.second_fan = 1;
7693 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7694 "secondary fan support enabled\n");
7696 } else {
7697 printk(TPACPI_ERR
7698 "ThinkPad ACPI EC access misbehaving, "
7699 "fan status and control unavailable\n");
7700 return 1;
7704 if (sfan_handle) {
7705 /* 570, 770x-JL */
7706 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7707 fan_control_commands |=
7708 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7709 } else {
7710 if (!gfan_handle) {
7711 /* gfan without sfan means no fan control */
7712 /* all other models implement TP EC 0x2f control */
7714 if (fans_handle) {
7715 /* X31, X40, X41 */
7716 fan_control_access_mode =
7717 TPACPI_FAN_WR_ACPI_FANS;
7718 fan_control_commands |=
7719 TPACPI_FAN_CMD_SPEED |
7720 TPACPI_FAN_CMD_LEVEL |
7721 TPACPI_FAN_CMD_ENABLE;
7722 } else {
7723 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7724 fan_control_commands |=
7725 TPACPI_FAN_CMD_LEVEL |
7726 TPACPI_FAN_CMD_ENABLE;
7731 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7732 "fan is %s, modes %d, %d\n",
7733 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7734 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7735 fan_status_access_mode, fan_control_access_mode);
7737 /* fan control master switch */
7738 if (!fan_control_allowed) {
7739 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7740 fan_control_commands = 0;
7741 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7742 "fan control features disabled by parameter\n");
7745 /* update fan_control_desired_level */
7746 if (fan_status_access_mode != TPACPI_FAN_NONE)
7747 fan_get_status_safe(NULL);
7749 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7750 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
7751 if (tp_features.second_fan) {
7752 /* attach second fan tachometer */
7753 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7754 &dev_attr_fan_fan2_input.attr;
7756 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
7757 &fan_attr_group);
7758 if (rc < 0)
7759 return rc;
7761 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7762 &driver_attr_fan_watchdog);
7763 if (rc < 0) {
7764 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7765 &fan_attr_group);
7766 return rc;
7768 return 0;
7769 } else
7770 return 1;
7773 static void fan_exit(void)
7775 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
7776 "cancelling any pending fan watchdog tasks\n");
7778 /* FIXME: can we really do this unconditionally? */
7779 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
7780 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7781 &driver_attr_fan_watchdog);
7783 cancel_delayed_work(&fan_watchdog_task);
7784 flush_workqueue(tpacpi_wq);
7787 static void fan_suspend(pm_message_t state)
7789 int rc;
7791 if (!fan_control_allowed)
7792 return;
7794 /* Store fan status in cache */
7795 fan_control_resume_level = 0;
7796 rc = fan_get_status_safe(&fan_control_resume_level);
7797 if (rc < 0)
7798 printk(TPACPI_NOTICE
7799 "failed to read fan level for later "
7800 "restore during resume: %d\n", rc);
7802 /* if it is undefined, don't attempt to restore it.
7803 * KEEP THIS LAST */
7804 if (tp_features.fan_ctrl_status_undef)
7805 fan_control_resume_level = 0;
7808 static void fan_resume(void)
7810 u8 current_level = 7;
7811 bool do_set = false;
7812 int rc;
7814 /* DSDT *always* updates status on resume */
7815 tp_features.fan_ctrl_status_undef = 0;
7817 if (!fan_control_allowed ||
7818 !fan_control_resume_level ||
7819 (fan_get_status_safe(&current_level) < 0))
7820 return;
7822 switch (fan_control_access_mode) {
7823 case TPACPI_FAN_WR_ACPI_SFAN:
7824 /* never decrease fan level */
7825 do_set = (fan_control_resume_level > current_level);
7826 break;
7827 case TPACPI_FAN_WR_ACPI_FANS:
7828 case TPACPI_FAN_WR_TPEC:
7829 /* never decrease fan level, scale is:
7830 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7832 * We expect the firmware to set either 7 or AUTO, but we
7833 * handle FULLSPEED out of paranoia.
7835 * So, we can safely only restore FULLSPEED or 7, anything
7836 * else could slow the fan. Restoring AUTO is useless, at
7837 * best that's exactly what the DSDT already set (it is the
7838 * slower it uses).
7840 * Always keep in mind that the DSDT *will* have set the
7841 * fans to what the vendor supposes is the best level. We
7842 * muck with it only to speed the fan up.
7844 if (fan_control_resume_level != 7 &&
7845 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7846 return;
7847 else
7848 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7849 (current_level != fan_control_resume_level);
7850 break;
7851 default:
7852 return;
7854 if (do_set) {
7855 printk(TPACPI_NOTICE
7856 "restoring fan level to 0x%02x\n",
7857 fan_control_resume_level);
7858 rc = fan_set_level_safe(fan_control_resume_level);
7859 if (rc < 0)
7860 printk(TPACPI_NOTICE
7861 "failed to restore fan level: %d\n", rc);
7865 static int fan_read(struct seq_file *m)
7867 int rc;
7868 u8 status;
7869 unsigned int speed = 0;
7871 switch (fan_status_access_mode) {
7872 case TPACPI_FAN_RD_ACPI_GFAN:
7873 /* 570, 600e/x, 770e, 770x */
7874 rc = fan_get_status_safe(&status);
7875 if (rc < 0)
7876 return rc;
7878 seq_printf(m, "status:\t\t%s\n"
7879 "level:\t\t%d\n",
7880 (status != 0) ? "enabled" : "disabled", status);
7881 break;
7883 case TPACPI_FAN_RD_TPEC:
7884 /* all except 570, 600e/x, 770e, 770x */
7885 rc = fan_get_status_safe(&status);
7886 if (rc < 0)
7887 return rc;
7889 seq_printf(m, "status:\t\t%s\n",
7890 (status != 0) ? "enabled" : "disabled");
7892 rc = fan_get_speed(&speed);
7893 if (rc < 0)
7894 return rc;
7896 seq_printf(m, "speed:\t\t%d\n", speed);
7898 if (status & TP_EC_FAN_FULLSPEED)
7899 /* Disengaged mode takes precedence */
7900 seq_printf(m, "level:\t\tdisengaged\n");
7901 else if (status & TP_EC_FAN_AUTO)
7902 seq_printf(m, "level:\t\tauto\n");
7903 else
7904 seq_printf(m, "level:\t\t%d\n", status);
7905 break;
7907 case TPACPI_FAN_NONE:
7908 default:
7909 seq_printf(m, "status:\t\tnot supported\n");
7912 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7913 seq_printf(m, "commands:\tlevel <level>");
7915 switch (fan_control_access_mode) {
7916 case TPACPI_FAN_WR_ACPI_SFAN:
7917 seq_printf(m, " (<level> is 0-7)\n");
7918 break;
7920 default:
7921 seq_printf(m, " (<level> is 0-7, "
7922 "auto, disengaged, full-speed)\n");
7923 break;
7927 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7928 seq_printf(m, "commands:\tenable, disable\n"
7929 "commands:\twatchdog <timeout> (<timeout> "
7930 "is 0 (off), 1-120 (seconds))\n");
7932 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7933 seq_printf(m, "commands:\tspeed <speed>"
7934 " (<speed> is 0-65535)\n");
7936 return 0;
7939 static int fan_write_cmd_level(const char *cmd, int *rc)
7941 int level;
7943 if (strlencmp(cmd, "level auto") == 0)
7944 level = TP_EC_FAN_AUTO;
7945 else if ((strlencmp(cmd, "level disengaged") == 0) |
7946 (strlencmp(cmd, "level full-speed") == 0))
7947 level = TP_EC_FAN_FULLSPEED;
7948 else if (sscanf(cmd, "level %d", &level) != 1)
7949 return 0;
7951 *rc = fan_set_level_safe(level);
7952 if (*rc == -ENXIO)
7953 printk(TPACPI_ERR "level command accepted for unsupported "
7954 "access mode %d", fan_control_access_mode);
7955 else if (!*rc)
7956 tpacpi_disclose_usertask("procfs fan",
7957 "set level to %d\n", level);
7959 return 1;
7962 static int fan_write_cmd_enable(const char *cmd, int *rc)
7964 if (strlencmp(cmd, "enable") != 0)
7965 return 0;
7967 *rc = fan_set_enable();
7968 if (*rc == -ENXIO)
7969 printk(TPACPI_ERR "enable command accepted for unsupported "
7970 "access mode %d", fan_control_access_mode);
7971 else if (!*rc)
7972 tpacpi_disclose_usertask("procfs fan", "enable\n");
7974 return 1;
7977 static int fan_write_cmd_disable(const char *cmd, int *rc)
7979 if (strlencmp(cmd, "disable") != 0)
7980 return 0;
7982 *rc = fan_set_disable();
7983 if (*rc == -ENXIO)
7984 printk(TPACPI_ERR "disable command accepted for unsupported "
7985 "access mode %d", fan_control_access_mode);
7986 else if (!*rc)
7987 tpacpi_disclose_usertask("procfs fan", "disable\n");
7989 return 1;
7992 static int fan_write_cmd_speed(const char *cmd, int *rc)
7994 int speed;
7996 /* TODO:
7997 * Support speed <low> <medium> <high> ? */
7999 if (sscanf(cmd, "speed %d", &speed) != 1)
8000 return 0;
8002 *rc = fan_set_speed(speed);
8003 if (*rc == -ENXIO)
8004 printk(TPACPI_ERR "speed command accepted for unsupported "
8005 "access mode %d", fan_control_access_mode);
8006 else if (!*rc)
8007 tpacpi_disclose_usertask("procfs fan",
8008 "set speed to %d\n", speed);
8010 return 1;
8013 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8015 int interval;
8017 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8018 return 0;
8020 if (interval < 0 || interval > 120)
8021 *rc = -EINVAL;
8022 else {
8023 fan_watchdog_maxinterval = interval;
8024 tpacpi_disclose_usertask("procfs fan",
8025 "set watchdog timer to %d\n",
8026 interval);
8029 return 1;
8032 static int fan_write(char *buf)
8034 char *cmd;
8035 int rc = 0;
8037 while (!rc && (cmd = next_cmd(&buf))) {
8038 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8039 fan_write_cmd_level(cmd, &rc)) &&
8040 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8041 (fan_write_cmd_enable(cmd, &rc) ||
8042 fan_write_cmd_disable(cmd, &rc) ||
8043 fan_write_cmd_watchdog(cmd, &rc))) &&
8044 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8045 fan_write_cmd_speed(cmd, &rc))
8047 rc = -EINVAL;
8048 else if (!rc)
8049 fan_watchdog_reset();
8052 return rc;
8055 static struct ibm_struct fan_driver_data = {
8056 .name = "fan",
8057 .read = fan_read,
8058 .write = fan_write,
8059 .exit = fan_exit,
8060 .suspend = fan_suspend,
8061 .resume = fan_resume,
8064 /****************************************************************************
8065 ****************************************************************************
8067 * Infrastructure
8069 ****************************************************************************
8070 ****************************************************************************/
8073 * HKEY event callout for other subdrivers go here
8074 * (yes, it is ugly, but it is quick, safe, and gets the job done
8076 static void tpacpi_driver_event(const unsigned int hkey_event)
8078 if (ibm_backlight_device) {
8079 switch (hkey_event) {
8080 case TP_HKEY_EV_BRGHT_UP:
8081 case TP_HKEY_EV_BRGHT_DOWN:
8082 tpacpi_brightness_notify_change();
8089 static void hotkey_driver_event(const unsigned int scancode)
8091 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8094 /* sysfs name ---------------------------------------------------------- */
8095 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8096 struct device_attribute *attr,
8097 char *buf)
8099 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8102 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8103 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8105 /* --------------------------------------------------------------------- */
8107 /* /proc support */
8108 static struct proc_dir_entry *proc_dir;
8111 * Module and infrastructure proble, init and exit handling
8114 static int force_load;
8116 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8117 static const char * __init str_supported(int is_supported)
8119 static char text_unsupported[] __initdata = "not supported";
8121 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8123 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8125 static void ibm_exit(struct ibm_struct *ibm)
8127 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8129 list_del_init(&ibm->all_drivers);
8131 if (ibm->flags.acpi_notify_installed) {
8132 dbg_printk(TPACPI_DBG_EXIT,
8133 "%s: acpi_remove_notify_handler\n", ibm->name);
8134 BUG_ON(!ibm->acpi);
8135 acpi_remove_notify_handler(*ibm->acpi->handle,
8136 ibm->acpi->type,
8137 dispatch_acpi_notify);
8138 ibm->flags.acpi_notify_installed = 0;
8139 ibm->flags.acpi_notify_installed = 0;
8142 if (ibm->flags.proc_created) {
8143 dbg_printk(TPACPI_DBG_EXIT,
8144 "%s: remove_proc_entry\n", ibm->name);
8145 remove_proc_entry(ibm->name, proc_dir);
8146 ibm->flags.proc_created = 0;
8149 if (ibm->flags.acpi_driver_registered) {
8150 dbg_printk(TPACPI_DBG_EXIT,
8151 "%s: acpi_bus_unregister_driver\n", ibm->name);
8152 BUG_ON(!ibm->acpi);
8153 acpi_bus_unregister_driver(ibm->acpi->driver);
8154 kfree(ibm->acpi->driver);
8155 ibm->acpi->driver = NULL;
8156 ibm->flags.acpi_driver_registered = 0;
8159 if (ibm->flags.init_called && ibm->exit) {
8160 ibm->exit();
8161 ibm->flags.init_called = 0;
8164 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8167 static int __init ibm_init(struct ibm_init_struct *iibm)
8169 int ret;
8170 struct ibm_struct *ibm = iibm->data;
8171 struct proc_dir_entry *entry;
8173 BUG_ON(ibm == NULL);
8175 INIT_LIST_HEAD(&ibm->all_drivers);
8177 if (ibm->flags.experimental && !experimental)
8178 return 0;
8180 dbg_printk(TPACPI_DBG_INIT,
8181 "probing for %s\n", ibm->name);
8183 if (iibm->init) {
8184 ret = iibm->init(iibm);
8185 if (ret > 0)
8186 return 0; /* probe failed */
8187 if (ret)
8188 return ret;
8190 ibm->flags.init_called = 1;
8193 if (ibm->acpi) {
8194 if (ibm->acpi->hid) {
8195 ret = register_tpacpi_subdriver(ibm);
8196 if (ret)
8197 goto err_out;
8200 if (ibm->acpi->notify) {
8201 ret = setup_acpi_notify(ibm);
8202 if (ret == -ENODEV) {
8203 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8204 ibm->name);
8205 ret = 0;
8206 goto err_out;
8208 if (ret < 0)
8209 goto err_out;
8213 dbg_printk(TPACPI_DBG_INIT,
8214 "%s installed\n", ibm->name);
8216 if (ibm->read) {
8217 mode_t mode = iibm->base_procfs_mode;
8219 if (!mode)
8220 mode = S_IRUGO;
8221 if (ibm->write)
8222 mode |= S_IWUSR;
8223 entry = proc_create_data(ibm->name, mode, proc_dir,
8224 &dispatch_proc_fops, ibm);
8225 if (!entry) {
8226 printk(TPACPI_ERR "unable to create proc entry %s\n",
8227 ibm->name);
8228 ret = -ENODEV;
8229 goto err_out;
8231 ibm->flags.proc_created = 1;
8234 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8236 return 0;
8238 err_out:
8239 dbg_printk(TPACPI_DBG_INIT,
8240 "%s: at error exit path with result %d\n",
8241 ibm->name, ret);
8243 ibm_exit(ibm);
8244 return (ret < 0)? ret : 0;
8247 /* Probing */
8249 static bool __pure __init tpacpi_is_fw_digit(const char c)
8251 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8254 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8255 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8256 const char t)
8258 return s && strlen(s) >= 8 &&
8259 tpacpi_is_fw_digit(s[0]) &&
8260 tpacpi_is_fw_digit(s[1]) &&
8261 s[2] == t && s[3] == 'T' &&
8262 tpacpi_is_fw_digit(s[4]) &&
8263 tpacpi_is_fw_digit(s[5]) &&
8264 s[6] == 'W' && s[7] == 'W';
8267 /* returns 0 - probe ok, or < 0 - probe error.
8268 * Probe ok doesn't mean thinkpad found.
8269 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8270 static int __must_check __init get_thinkpad_model_data(
8271 struct thinkpad_id_data *tp)
8273 const struct dmi_device *dev = NULL;
8274 char ec_fw_string[18];
8275 char const *s;
8277 if (!tp)
8278 return -EINVAL;
8280 memset(tp, 0, sizeof(*tp));
8282 if (dmi_name_in_vendors("IBM"))
8283 tp->vendor = PCI_VENDOR_ID_IBM;
8284 else if (dmi_name_in_vendors("LENOVO"))
8285 tp->vendor = PCI_VENDOR_ID_LENOVO;
8286 else
8287 return 0;
8289 s = dmi_get_system_info(DMI_BIOS_VERSION);
8290 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8291 if (s && !tp->bios_version_str)
8292 return -ENOMEM;
8294 /* Really ancient ThinkPad 240X will fail this, which is fine */
8295 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8296 return 0;
8298 tp->bios_model = tp->bios_version_str[0]
8299 | (tp->bios_version_str[1] << 8);
8300 tp->bios_release = (tp->bios_version_str[4] << 8)
8301 | tp->bios_version_str[5];
8304 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8305 * X32 or newer, all Z series; Some models must have an
8306 * up-to-date BIOS or they will not be detected.
8308 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8310 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8311 if (sscanf(dev->name,
8312 "IBM ThinkPad Embedded Controller -[%17c",
8313 ec_fw_string) == 1) {
8314 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8315 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8317 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8318 if (!tp->ec_version_str)
8319 return -ENOMEM;
8321 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8322 tp->ec_model = ec_fw_string[0]
8323 | (ec_fw_string[1] << 8);
8324 tp->ec_release = (ec_fw_string[4] << 8)
8325 | ec_fw_string[5];
8326 } else {
8327 printk(TPACPI_NOTICE
8328 "ThinkPad firmware release %s "
8329 "doesn't match the known patterns\n",
8330 ec_fw_string);
8331 printk(TPACPI_NOTICE
8332 "please report this to %s\n",
8333 TPACPI_MAIL);
8335 break;
8339 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8340 if (s && !strnicmp(s, "ThinkPad", 8)) {
8341 tp->model_str = kstrdup(s, GFP_KERNEL);
8342 if (!tp->model_str)
8343 return -ENOMEM;
8346 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8347 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8348 if (s && !tp->nummodel_str)
8349 return -ENOMEM;
8351 return 0;
8354 static int __init probe_for_thinkpad(void)
8356 int is_thinkpad;
8358 if (acpi_disabled)
8359 return -ENODEV;
8362 * Non-ancient models have better DMI tagging, but very old models
8363 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8365 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8366 (thinkpad_id.ec_model != 0) ||
8367 tpacpi_is_fw_known();
8369 /* ec is required because many other handles are relative to it */
8370 TPACPI_ACPIHANDLE_INIT(ec);
8371 if (!ec_handle) {
8372 if (is_thinkpad)
8373 printk(TPACPI_ERR
8374 "Not yet supported ThinkPad detected!\n");
8375 return -ENODEV;
8378 if (!is_thinkpad && !force_load)
8379 return -ENODEV;
8381 return 0;
8385 /* Module init, exit, parameters */
8387 static struct ibm_init_struct ibms_init[] __initdata = {
8389 .init = thinkpad_acpi_driver_init,
8390 .data = &thinkpad_acpi_driver_data,
8393 .init = hotkey_init,
8394 .data = &hotkey_driver_data,
8397 .init = bluetooth_init,
8398 .data = &bluetooth_driver_data,
8401 .init = wan_init,
8402 .data = &wan_driver_data,
8405 .init = uwb_init,
8406 .data = &uwb_driver_data,
8408 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8410 .init = video_init,
8411 .base_procfs_mode = S_IRUSR,
8412 .data = &video_driver_data,
8414 #endif
8416 .init = light_init,
8417 .data = &light_driver_data,
8420 .init = cmos_init,
8421 .data = &cmos_driver_data,
8424 .init = led_init,
8425 .data = &led_driver_data,
8428 .init = beep_init,
8429 .data = &beep_driver_data,
8432 .init = thermal_init,
8433 .data = &thermal_driver_data,
8436 .data = &ecdump_driver_data,
8439 .init = brightness_init,
8440 .data = &brightness_driver_data,
8443 .init = volume_init,
8444 .data = &volume_driver_data,
8447 .init = fan_init,
8448 .data = &fan_driver_data,
8452 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8454 unsigned int i;
8455 struct ibm_struct *ibm;
8457 if (!kp || !kp->name || !val)
8458 return -EINVAL;
8460 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8461 ibm = ibms_init[i].data;
8462 WARN_ON(ibm == NULL);
8464 if (!ibm || !ibm->name)
8465 continue;
8467 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8468 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8469 return -ENOSPC;
8470 strcpy(ibms_init[i].param, val);
8471 strcat(ibms_init[i].param, ",");
8472 return 0;
8476 return -EINVAL;
8479 module_param(experimental, int, 0444);
8480 MODULE_PARM_DESC(experimental,
8481 "Enables experimental features when non-zero");
8483 module_param_named(debug, dbg_level, uint, 0);
8484 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8486 module_param(force_load, bool, 0444);
8487 MODULE_PARM_DESC(force_load,
8488 "Attempts to load the driver even on a "
8489 "mis-identified ThinkPad when true");
8491 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8492 MODULE_PARM_DESC(fan_control,
8493 "Enables setting fan parameters features when true");
8495 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8496 MODULE_PARM_DESC(brightness_mode,
8497 "Selects brightness control strategy: "
8498 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8500 module_param(brightness_enable, uint, 0444);
8501 MODULE_PARM_DESC(brightness_enable,
8502 "Enables backlight control when 1, disables when 0");
8504 module_param(hotkey_report_mode, uint, 0444);
8505 MODULE_PARM_DESC(hotkey_report_mode,
8506 "used for backwards compatibility with userspace, "
8507 "see documentation");
8509 module_param_named(volume_mode, volume_mode, uint, 0444);
8510 MODULE_PARM_DESC(volume_mode,
8511 "Selects volume control strategy: "
8512 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8514 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8515 MODULE_PARM_DESC(volume_capabilities,
8516 "Selects the mixer capabilites: "
8517 "0=auto, 1=volume and mute, 2=mute only");
8519 #define TPACPI_PARAM(feature) \
8520 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8521 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8522 "at module load, see documentation")
8524 TPACPI_PARAM(hotkey);
8525 TPACPI_PARAM(bluetooth);
8526 TPACPI_PARAM(video);
8527 TPACPI_PARAM(light);
8528 TPACPI_PARAM(cmos);
8529 TPACPI_PARAM(led);
8530 TPACPI_PARAM(beep);
8531 TPACPI_PARAM(ecdump);
8532 TPACPI_PARAM(brightness);
8533 TPACPI_PARAM(volume);
8534 TPACPI_PARAM(fan);
8536 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8537 module_param(dbg_wlswemul, uint, 0444);
8538 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8539 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8540 MODULE_PARM_DESC(wlsw_state,
8541 "Initial state of the emulated WLSW switch");
8543 module_param(dbg_bluetoothemul, uint, 0444);
8544 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8545 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8546 MODULE_PARM_DESC(bluetooth_state,
8547 "Initial state of the emulated bluetooth switch");
8549 module_param(dbg_wwanemul, uint, 0444);
8550 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8551 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8552 MODULE_PARM_DESC(wwan_state,
8553 "Initial state of the emulated WWAN switch");
8555 module_param(dbg_uwbemul, uint, 0444);
8556 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8557 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8558 MODULE_PARM_DESC(uwb_state,
8559 "Initial state of the emulated UWB switch");
8560 #endif
8562 static void thinkpad_acpi_module_exit(void)
8564 struct ibm_struct *ibm, *itmp;
8566 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8568 list_for_each_entry_safe_reverse(ibm, itmp,
8569 &tpacpi_all_drivers,
8570 all_drivers) {
8571 ibm_exit(ibm);
8574 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8576 if (tpacpi_inputdev) {
8577 if (tp_features.input_device_registered)
8578 input_unregister_device(tpacpi_inputdev);
8579 else
8580 input_free_device(tpacpi_inputdev);
8583 if (tpacpi_hwmon)
8584 hwmon_device_unregister(tpacpi_hwmon);
8586 if (tp_features.sensors_pdev_attrs_registered)
8587 device_remove_file(&tpacpi_sensors_pdev->dev,
8588 &dev_attr_thinkpad_acpi_pdev_name);
8589 if (tpacpi_sensors_pdev)
8590 platform_device_unregister(tpacpi_sensors_pdev);
8591 if (tpacpi_pdev)
8592 platform_device_unregister(tpacpi_pdev);
8594 if (tp_features.sensors_pdrv_attrs_registered)
8595 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8596 if (tp_features.platform_drv_attrs_registered)
8597 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8599 if (tp_features.sensors_pdrv_registered)
8600 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8602 if (tp_features.platform_drv_registered)
8603 platform_driver_unregister(&tpacpi_pdriver);
8605 if (proc_dir)
8606 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8608 if (tpacpi_wq)
8609 destroy_workqueue(tpacpi_wq);
8611 kfree(thinkpad_id.bios_version_str);
8612 kfree(thinkpad_id.ec_version_str);
8613 kfree(thinkpad_id.model_str);
8617 static int __init thinkpad_acpi_module_init(void)
8619 int ret, i;
8621 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8623 /* Parameter checking */
8624 if (hotkey_report_mode > 2)
8625 return -EINVAL;
8627 /* Driver-level probe */
8629 ret = get_thinkpad_model_data(&thinkpad_id);
8630 if (ret) {
8631 printk(TPACPI_ERR
8632 "unable to get DMI data: %d\n", ret);
8633 thinkpad_acpi_module_exit();
8634 return ret;
8636 ret = probe_for_thinkpad();
8637 if (ret) {
8638 thinkpad_acpi_module_exit();
8639 return ret;
8642 /* Driver initialization */
8644 TPACPI_ACPIHANDLE_INIT(ecrd);
8645 TPACPI_ACPIHANDLE_INIT(ecwr);
8647 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8648 if (!tpacpi_wq) {
8649 thinkpad_acpi_module_exit();
8650 return -ENOMEM;
8653 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8654 if (!proc_dir) {
8655 printk(TPACPI_ERR
8656 "unable to create proc dir " TPACPI_PROC_DIR);
8657 thinkpad_acpi_module_exit();
8658 return -ENODEV;
8661 ret = platform_driver_register(&tpacpi_pdriver);
8662 if (ret) {
8663 printk(TPACPI_ERR
8664 "unable to register main platform driver\n");
8665 thinkpad_acpi_module_exit();
8666 return ret;
8668 tp_features.platform_drv_registered = 1;
8670 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8671 if (ret) {
8672 printk(TPACPI_ERR
8673 "unable to register hwmon platform driver\n");
8674 thinkpad_acpi_module_exit();
8675 return ret;
8677 tp_features.sensors_pdrv_registered = 1;
8679 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8680 if (!ret) {
8681 tp_features.platform_drv_attrs_registered = 1;
8682 ret = tpacpi_create_driver_attributes(
8683 &tpacpi_hwmon_pdriver.driver);
8685 if (ret) {
8686 printk(TPACPI_ERR
8687 "unable to create sysfs driver attributes\n");
8688 thinkpad_acpi_module_exit();
8689 return ret;
8691 tp_features.sensors_pdrv_attrs_registered = 1;
8694 /* Device initialization */
8695 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8696 NULL, 0);
8697 if (IS_ERR(tpacpi_pdev)) {
8698 ret = PTR_ERR(tpacpi_pdev);
8699 tpacpi_pdev = NULL;
8700 printk(TPACPI_ERR "unable to register platform device\n");
8701 thinkpad_acpi_module_exit();
8702 return ret;
8704 tpacpi_sensors_pdev = platform_device_register_simple(
8705 TPACPI_HWMON_DRVR_NAME,
8706 -1, NULL, 0);
8707 if (IS_ERR(tpacpi_sensors_pdev)) {
8708 ret = PTR_ERR(tpacpi_sensors_pdev);
8709 tpacpi_sensors_pdev = NULL;
8710 printk(TPACPI_ERR
8711 "unable to register hwmon platform device\n");
8712 thinkpad_acpi_module_exit();
8713 return ret;
8715 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8716 &dev_attr_thinkpad_acpi_pdev_name);
8717 if (ret) {
8718 printk(TPACPI_ERR
8719 "unable to create sysfs hwmon device attributes\n");
8720 thinkpad_acpi_module_exit();
8721 return ret;
8723 tp_features.sensors_pdev_attrs_registered = 1;
8724 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
8725 if (IS_ERR(tpacpi_hwmon)) {
8726 ret = PTR_ERR(tpacpi_hwmon);
8727 tpacpi_hwmon = NULL;
8728 printk(TPACPI_ERR "unable to register hwmon device\n");
8729 thinkpad_acpi_module_exit();
8730 return ret;
8732 mutex_init(&tpacpi_inputdev_send_mutex);
8733 tpacpi_inputdev = input_allocate_device();
8734 if (!tpacpi_inputdev) {
8735 printk(TPACPI_ERR "unable to allocate input device\n");
8736 thinkpad_acpi_module_exit();
8737 return -ENOMEM;
8738 } else {
8739 /* Prepare input device, but don't register */
8740 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
8741 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
8742 tpacpi_inputdev->id.bustype = BUS_HOST;
8743 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8744 thinkpad_id.vendor :
8745 PCI_VENDOR_ID_IBM;
8746 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8747 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8748 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
8750 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8751 ret = ibm_init(&ibms_init[i]);
8752 if (ret >= 0 && *ibms_init[i].param)
8753 ret = ibms_init[i].data->write(ibms_init[i].param);
8754 if (ret < 0) {
8755 thinkpad_acpi_module_exit();
8756 return ret;
8760 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
8762 ret = input_register_device(tpacpi_inputdev);
8763 if (ret < 0) {
8764 printk(TPACPI_ERR "unable to register input device\n");
8765 thinkpad_acpi_module_exit();
8766 return ret;
8767 } else {
8768 tp_features.input_device_registered = 1;
8771 return 0;
8774 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8777 * This will autoload the driver in almost every ThinkPad
8778 * in widespread use.
8780 * Only _VERY_ old models, like the 240, 240x and 570 lack
8781 * the HKEY event interface.
8783 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8786 * DMI matching for module autoloading
8788 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8789 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8791 * Only models listed in thinkwiki will be supported, so add yours
8792 * if it is not there yet.
8794 #define IBM_BIOS_MODULE_ALIAS(__type) \
8795 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8797 /* Ancient thinkpad BIOSes have to be identified by
8798 * BIOS type or model number, and there are far less
8799 * BIOS types than model numbers... */
8800 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8802 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8803 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8804 MODULE_DESCRIPTION(TPACPI_DESC);
8805 MODULE_VERSION(TPACPI_VERSION);
8806 MODULE_LICENSE("GPL");
8808 module_init(thinkpad_acpi_module_init);
8809 module_exit(thinkpad_acpi_module_exit);