thinkpad-acpi: don't fail to load the entire module due to ALSA problems
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob9b7da9c96e7691c54a7536d3823137f8696062a4
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.24"
25 #define TPACPI_SYSFS_VERSION 0x020700
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/seq_file.h>
65 #include <linux/sysfs.h>
66 #include <linux/backlight.h>
67 #include <linux/fb.h>
68 #include <linux/platform_device.h>
69 #include <linux/hwmon.h>
70 #include <linux/hwmon-sysfs.h>
71 #include <linux/input.h>
72 #include <linux/leds.h>
73 #include <linux/rfkill.h>
74 #include <asm/uaccess.h>
76 #include <linux/dmi.h>
77 #include <linux/jiffies.h>
78 #include <linux/workqueue.h>
80 #include <sound/core.h>
81 #include <sound/control.h>
82 #include <sound/initval.h>
84 #include <acpi/acpi_drivers.h>
86 #include <linux/pci_ids.h>
89 /* ThinkPad CMOS commands */
90 #define TP_CMOS_VOLUME_DOWN 0
91 #define TP_CMOS_VOLUME_UP 1
92 #define TP_CMOS_VOLUME_MUTE 2
93 #define TP_CMOS_BRIGHTNESS_UP 4
94 #define TP_CMOS_BRIGHTNESS_DOWN 5
95 #define TP_CMOS_THINKLIGHT_ON 12
96 #define TP_CMOS_THINKLIGHT_OFF 13
98 /* NVRAM Addresses */
99 enum tp_nvram_addr {
100 TP_NVRAM_ADDR_HK2 = 0x57,
101 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
102 TP_NVRAM_ADDR_VIDEO = 0x59,
103 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
104 TP_NVRAM_ADDR_MIXER = 0x60,
107 /* NVRAM bit masks */
108 enum {
109 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
110 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
111 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
112 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
113 TP_NVRAM_MASK_THINKLIGHT = 0x10,
114 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
115 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
116 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
117 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
118 TP_NVRAM_MASK_MUTE = 0x40,
119 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
120 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
121 TP_NVRAM_POS_LEVEL_VOLUME = 0,
124 /* ACPI HIDs */
125 #define TPACPI_ACPI_HKEY_HID "IBM0068"
127 /* Input IDs */
128 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
129 #define TPACPI_HKEY_INPUT_VERSION 0x4101
131 /* ACPI \WGSV commands */
132 enum {
133 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
134 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
135 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
136 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
139 /* TP_ACPI_WGSV_GET_STATE bits */
140 enum {
141 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
142 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
143 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
144 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
145 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
146 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
147 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
148 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
149 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
150 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
153 /* HKEY events */
154 enum tpacpi_hkey_event_t {
155 /* Hotkey-related */
156 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
157 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
158 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
159 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
160 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
161 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
163 /* Reasons for waking up from S3/S4 */
164 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
165 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
166 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
167 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
168 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
169 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
171 /* Auto-sleep after eject request */
172 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
173 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
175 /* Misc bay events */
176 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
178 /* User-interface events */
179 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
180 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
181 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
182 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
183 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
184 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
185 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
187 /* Thermal events */
188 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
189 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
190 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
191 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
192 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
194 /* Misc */
195 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
198 /****************************************************************************
199 * Main driver
202 #define TPACPI_NAME "thinkpad"
203 #define TPACPI_DESC "ThinkPad ACPI Extras"
204 #define TPACPI_FILE TPACPI_NAME "_acpi"
205 #define TPACPI_URL "http://ibm-acpi.sf.net/"
206 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
208 #define TPACPI_PROC_DIR "ibm"
209 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
210 #define TPACPI_DRVR_NAME TPACPI_FILE
211 #define TPACPI_DRVR_SHORTNAME "tpacpi"
212 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
214 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
215 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
217 #define TPACPI_MAX_ACPI_ARGS 3
219 /* printk headers */
220 #define TPACPI_LOG TPACPI_FILE ": "
221 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
222 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
223 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
224 #define TPACPI_ERR KERN_ERR TPACPI_LOG
225 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
226 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
227 #define TPACPI_INFO KERN_INFO TPACPI_LOG
228 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
230 /* Debugging printk groups */
231 #define TPACPI_DBG_ALL 0xffff
232 #define TPACPI_DBG_DISCLOSETASK 0x8000
233 #define TPACPI_DBG_INIT 0x0001
234 #define TPACPI_DBG_EXIT 0x0002
235 #define TPACPI_DBG_RFKILL 0x0004
236 #define TPACPI_DBG_HKEY 0x0008
237 #define TPACPI_DBG_FAN 0x0010
238 #define TPACPI_DBG_BRGHT 0x0020
239 #define TPACPI_DBG_MIXER 0x0040
241 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
242 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
243 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
246 /****************************************************************************
247 * Driver-wide structs and misc. variables
250 struct ibm_struct;
252 struct tp_acpi_drv_struct {
253 const struct acpi_device_id *hid;
254 struct acpi_driver *driver;
256 void (*notify) (struct ibm_struct *, u32);
257 acpi_handle *handle;
258 u32 type;
259 struct acpi_device *device;
262 struct ibm_struct {
263 char *name;
265 int (*read) (struct seq_file *);
266 int (*write) (char *);
267 void (*exit) (void);
268 void (*resume) (void);
269 void (*suspend) (pm_message_t state);
270 void (*shutdown) (void);
272 struct list_head all_drivers;
274 struct tp_acpi_drv_struct *acpi;
276 struct {
277 u8 acpi_driver_registered:1;
278 u8 acpi_notify_installed:1;
279 u8 proc_created:1;
280 u8 init_called:1;
281 u8 experimental:1;
282 } flags;
285 struct ibm_init_struct {
286 char param[32];
288 int (*init) (struct ibm_init_struct *);
289 struct ibm_struct *data;
292 static struct {
293 u32 bluetooth:1;
294 u32 hotkey:1;
295 u32 hotkey_mask:1;
296 u32 hotkey_wlsw:1;
297 u32 hotkey_tablet:1;
298 u32 light:1;
299 u32 light_status:1;
300 u32 bright_16levels:1;
301 u32 bright_acpimode:1;
302 u32 wan:1;
303 u32 uwb:1;
304 u32 fan_ctrl_status_undef:1;
305 u32 second_fan:1;
306 u32 beep_needs_two_args:1;
307 u32 mixer_no_level_control:1;
308 u32 input_device_registered:1;
309 u32 platform_drv_registered:1;
310 u32 platform_drv_attrs_registered:1;
311 u32 sensors_pdrv_registered:1;
312 u32 sensors_pdrv_attrs_registered:1;
313 u32 sensors_pdev_attrs_registered:1;
314 u32 hotkey_poll_active:1;
315 } tp_features;
317 static struct {
318 u16 hotkey_mask_ff:1;
319 u16 volume_ctrl_forbidden:1;
320 } tp_warned;
322 struct thinkpad_id_data {
323 unsigned int vendor; /* ThinkPad vendor:
324 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
326 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
327 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
329 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
330 u16 ec_model;
331 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
332 u16 ec_release;
334 char *model_str; /* ThinkPad T43 */
335 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
337 static struct thinkpad_id_data thinkpad_id;
339 static enum {
340 TPACPI_LIFE_INIT = 0,
341 TPACPI_LIFE_RUNNING,
342 TPACPI_LIFE_EXITING,
343 } tpacpi_lifecycle;
345 static int experimental;
346 static u32 dbg_level;
348 static struct workqueue_struct *tpacpi_wq;
350 enum led_status_t {
351 TPACPI_LED_OFF = 0,
352 TPACPI_LED_ON,
353 TPACPI_LED_BLINK,
356 /* Special LED class that can defer work */
357 struct tpacpi_led_classdev {
358 struct led_classdev led_classdev;
359 struct work_struct work;
360 enum led_status_t new_state;
361 unsigned int led;
364 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
365 static int dbg_wlswemul;
366 static int tpacpi_wlsw_emulstate;
367 static int dbg_bluetoothemul;
368 static int tpacpi_bluetooth_emulstate;
369 static int dbg_wwanemul;
370 static int tpacpi_wwan_emulstate;
371 static int dbg_uwbemul;
372 static int tpacpi_uwb_emulstate;
373 #endif
376 /*************************************************************************
377 * Debugging helpers
380 #define dbg_printk(a_dbg_level, format, arg...) \
381 do { if (dbg_level & (a_dbg_level)) \
382 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
383 } while (0)
385 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
386 #define vdbg_printk dbg_printk
387 static const char *str_supported(int is_supported);
388 #else
389 #define vdbg_printk(a_dbg_level, format, arg...) \
390 do { } while (0)
391 #endif
393 static void tpacpi_log_usertask(const char * const what)
395 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
396 what, task_tgid_vnr(current));
399 #define tpacpi_disclose_usertask(what, format, arg...) \
400 do { \
401 if (unlikely( \
402 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
403 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
404 printk(TPACPI_DEBUG "%s: PID %d: " format, \
405 what, task_tgid_vnr(current), ## arg); \
407 } while (0)
410 * Quirk handling helpers
412 * ThinkPad IDs and versions seen in the field so far
413 * are two-characters from the set [0-9A-Z], i.e. base 36.
415 * We use values well outside that range as specials.
418 #define TPACPI_MATCH_ANY 0xffffU
419 #define TPACPI_MATCH_UNKNOWN 0U
421 /* TPID('1', 'Y') == 0x5931 */
422 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
424 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
425 { .vendor = PCI_VENDOR_ID_IBM, \
426 .bios = TPID(__id1, __id2), \
427 .ec = TPACPI_MATCH_ANY, \
428 .quirks = (__quirk) }
430 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
431 { .vendor = PCI_VENDOR_ID_LENOVO, \
432 .bios = TPID(__id1, __id2), \
433 .ec = TPACPI_MATCH_ANY, \
434 .quirks = (__quirk) }
436 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
437 { .vendor = PCI_VENDOR_ID_LENOVO, \
438 .bios = TPACPI_MATCH_ANY, \
439 .ec = TPID(__id1, __id2), \
440 .quirks = (__quirk) }
442 struct tpacpi_quirk {
443 unsigned int vendor;
444 u16 bios;
445 u16 ec;
446 unsigned long quirks;
450 * tpacpi_check_quirks() - search BIOS/EC version on a list
451 * @qlist: array of &struct tpacpi_quirk
452 * @qlist_size: number of elements in @qlist
454 * Iterates over a quirks list until one is found that matches the
455 * ThinkPad's vendor, BIOS and EC model.
457 * Returns 0 if nothing matches, otherwise returns the quirks field of
458 * the matching &struct tpacpi_quirk entry.
460 * The match criteria is: vendor, ec and bios much match.
462 static unsigned long __init tpacpi_check_quirks(
463 const struct tpacpi_quirk *qlist,
464 unsigned int qlist_size)
466 while (qlist_size) {
467 if ((qlist->vendor == thinkpad_id.vendor ||
468 qlist->vendor == TPACPI_MATCH_ANY) &&
469 (qlist->bios == thinkpad_id.bios_model ||
470 qlist->bios == TPACPI_MATCH_ANY) &&
471 (qlist->ec == thinkpad_id.ec_model ||
472 qlist->ec == TPACPI_MATCH_ANY))
473 return qlist->quirks;
475 qlist_size--;
476 qlist++;
478 return 0;
482 /****************************************************************************
483 ****************************************************************************
485 * ACPI Helpers and device model
487 ****************************************************************************
488 ****************************************************************************/
490 /*************************************************************************
491 * ACPI basic handles
494 static acpi_handle root_handle;
496 #define TPACPI_HANDLE(object, parent, paths...) \
497 static acpi_handle object##_handle; \
498 static acpi_handle *object##_parent = &parent##_handle; \
499 static char *object##_path; \
500 static char *object##_paths[] = { paths }
502 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
503 "\\_SB.PCI.ISA.EC", /* 570 */
504 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
505 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
506 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
507 "\\_SB.PCI0.ICH3.EC0", /* R31 */
508 "\\_SB.PCI0.LPC.EC", /* all others */
511 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
512 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
514 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
515 /* T4x, X31, X40 */
516 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
517 "\\CMS", /* R40, R40e */
518 ); /* all others */
520 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
521 "^HKEY", /* R30, R31 */
522 "HKEY", /* all others */
523 ); /* 570 */
525 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
526 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
527 "\\_SB.PCI0.VID0", /* 770e */
528 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
529 "\\_SB.PCI0.AGP.VID", /* all others */
530 ); /* R30, R31 */
533 /*************************************************************************
534 * ACPI helpers
537 static int acpi_evalf(acpi_handle handle,
538 void *res, char *method, char *fmt, ...)
540 char *fmt0 = fmt;
541 struct acpi_object_list params;
542 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
543 struct acpi_buffer result, *resultp;
544 union acpi_object out_obj;
545 acpi_status status;
546 va_list ap;
547 char res_type;
548 int success;
549 int quiet;
551 if (!*fmt) {
552 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
553 return 0;
556 if (*fmt == 'q') {
557 quiet = 1;
558 fmt++;
559 } else
560 quiet = 0;
562 res_type = *(fmt++);
564 params.count = 0;
565 params.pointer = &in_objs[0];
567 va_start(ap, fmt);
568 while (*fmt) {
569 char c = *(fmt++);
570 switch (c) {
571 case 'd': /* int */
572 in_objs[params.count].integer.value = va_arg(ap, int);
573 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
574 break;
575 /* add more types as needed */
576 default:
577 printk(TPACPI_ERR "acpi_evalf() called "
578 "with invalid format character '%c'\n", c);
579 return 0;
582 va_end(ap);
584 if (res_type != 'v') {
585 result.length = sizeof(out_obj);
586 result.pointer = &out_obj;
587 resultp = &result;
588 } else
589 resultp = NULL;
591 status = acpi_evaluate_object(handle, method, &params, resultp);
593 switch (res_type) {
594 case 'd': /* int */
595 if (res)
596 *(int *)res = out_obj.integer.value;
597 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
598 break;
599 case 'v': /* void */
600 success = status == AE_OK;
601 break;
602 /* add more types as needed */
603 default:
604 printk(TPACPI_ERR "acpi_evalf() called "
605 "with invalid format character '%c'\n", res_type);
606 return 0;
609 if (!success && !quiet)
610 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
611 method, fmt0, status);
613 return success;
616 static int acpi_ec_read(int i, u8 *p)
618 int v;
620 if (ecrd_handle) {
621 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
622 return 0;
623 *p = v;
624 } else {
625 if (ec_read(i, p) < 0)
626 return 0;
629 return 1;
632 static int acpi_ec_write(int i, u8 v)
634 if (ecwr_handle) {
635 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
636 return 0;
637 } else {
638 if (ec_write(i, v) < 0)
639 return 0;
642 return 1;
645 static int issue_thinkpad_cmos_command(int cmos_cmd)
647 if (!cmos_handle)
648 return -ENXIO;
650 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
651 return -EIO;
653 return 0;
656 /*************************************************************************
657 * ACPI device model
660 #define TPACPI_ACPIHANDLE_INIT(object) \
661 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
662 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
664 static void drv_acpi_handle_init(char *name,
665 acpi_handle *handle, acpi_handle parent,
666 char **paths, int num_paths, char **path)
668 int i;
669 acpi_status status;
671 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
672 name);
674 for (i = 0; i < num_paths; i++) {
675 status = acpi_get_handle(parent, paths[i], handle);
676 if (ACPI_SUCCESS(status)) {
677 *path = paths[i];
678 dbg_printk(TPACPI_DBG_INIT,
679 "Found ACPI handle %s for %s\n",
680 *path, name);
681 return;
685 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
686 name);
687 *handle = NULL;
690 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
692 struct ibm_struct *ibm = data;
694 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
695 return;
697 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
698 return;
700 ibm->acpi->notify(ibm, event);
703 static int __init setup_acpi_notify(struct ibm_struct *ibm)
705 acpi_status status;
706 int rc;
708 BUG_ON(!ibm->acpi);
710 if (!*ibm->acpi->handle)
711 return 0;
713 vdbg_printk(TPACPI_DBG_INIT,
714 "setting up ACPI notify for %s\n", ibm->name);
716 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
717 if (rc < 0) {
718 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
719 ibm->name, rc);
720 return -ENODEV;
723 ibm->acpi->device->driver_data = ibm;
724 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
725 TPACPI_ACPI_EVENT_PREFIX,
726 ibm->name);
728 status = acpi_install_notify_handler(*ibm->acpi->handle,
729 ibm->acpi->type, dispatch_acpi_notify, ibm);
730 if (ACPI_FAILURE(status)) {
731 if (status == AE_ALREADY_EXISTS) {
732 printk(TPACPI_NOTICE
733 "another device driver is already "
734 "handling %s events\n", ibm->name);
735 } else {
736 printk(TPACPI_ERR
737 "acpi_install_notify_handler(%s) failed: %d\n",
738 ibm->name, status);
740 return -ENODEV;
742 ibm->flags.acpi_notify_installed = 1;
743 return 0;
746 static int __init tpacpi_device_add(struct acpi_device *device)
748 return 0;
751 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
753 int rc;
755 dbg_printk(TPACPI_DBG_INIT,
756 "registering %s as an ACPI driver\n", ibm->name);
758 BUG_ON(!ibm->acpi);
760 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
761 if (!ibm->acpi->driver) {
762 printk(TPACPI_ERR
763 "failed to allocate memory for ibm->acpi->driver\n");
764 return -ENOMEM;
767 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
768 ibm->acpi->driver->ids = ibm->acpi->hid;
770 ibm->acpi->driver->ops.add = &tpacpi_device_add;
772 rc = acpi_bus_register_driver(ibm->acpi->driver);
773 if (rc < 0) {
774 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
775 ibm->name, rc);
776 kfree(ibm->acpi->driver);
777 ibm->acpi->driver = NULL;
778 } else if (!rc)
779 ibm->flags.acpi_driver_registered = 1;
781 return rc;
785 /****************************************************************************
786 ****************************************************************************
788 * Procfs Helpers
790 ****************************************************************************
791 ****************************************************************************/
793 static int dispatch_proc_show(struct seq_file *m, void *v)
795 struct ibm_struct *ibm = m->private;
797 if (!ibm || !ibm->read)
798 return -EINVAL;
799 return ibm->read(m);
802 static int dispatch_proc_open(struct inode *inode, struct file *file)
804 return single_open(file, dispatch_proc_show, PDE(inode)->data);
807 static ssize_t dispatch_proc_write(struct file *file,
808 const char __user *userbuf,
809 size_t count, loff_t *pos)
811 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
812 char *kernbuf;
813 int ret;
815 if (!ibm || !ibm->write)
816 return -EINVAL;
817 if (count > PAGE_SIZE - 2)
818 return -EINVAL;
820 kernbuf = kmalloc(count + 2, GFP_KERNEL);
821 if (!kernbuf)
822 return -ENOMEM;
824 if (copy_from_user(kernbuf, userbuf, count)) {
825 kfree(kernbuf);
826 return -EFAULT;
829 kernbuf[count] = 0;
830 strcat(kernbuf, ",");
831 ret = ibm->write(kernbuf);
832 if (ret == 0)
833 ret = count;
835 kfree(kernbuf);
837 return ret;
840 static const struct file_operations dispatch_proc_fops = {
841 .owner = THIS_MODULE,
842 .open = dispatch_proc_open,
843 .read = seq_read,
844 .llseek = seq_lseek,
845 .release = single_release,
846 .write = dispatch_proc_write,
849 static char *next_cmd(char **cmds)
851 char *start = *cmds;
852 char *end;
854 while ((end = strchr(start, ',')) && end == start)
855 start = end + 1;
857 if (!end)
858 return NULL;
860 *end = 0;
861 *cmds = end + 1;
862 return start;
866 /****************************************************************************
867 ****************************************************************************
869 * Device model: input, hwmon and platform
871 ****************************************************************************
872 ****************************************************************************/
874 static struct platform_device *tpacpi_pdev;
875 static struct platform_device *tpacpi_sensors_pdev;
876 static struct device *tpacpi_hwmon;
877 static struct input_dev *tpacpi_inputdev;
878 static struct mutex tpacpi_inputdev_send_mutex;
879 static LIST_HEAD(tpacpi_all_drivers);
881 static int tpacpi_suspend_handler(struct platform_device *pdev,
882 pm_message_t state)
884 struct ibm_struct *ibm, *itmp;
886 list_for_each_entry_safe(ibm, itmp,
887 &tpacpi_all_drivers,
888 all_drivers) {
889 if (ibm->suspend)
890 (ibm->suspend)(state);
893 return 0;
896 static int tpacpi_resume_handler(struct platform_device *pdev)
898 struct ibm_struct *ibm, *itmp;
900 list_for_each_entry_safe(ibm, itmp,
901 &tpacpi_all_drivers,
902 all_drivers) {
903 if (ibm->resume)
904 (ibm->resume)();
907 return 0;
910 static void tpacpi_shutdown_handler(struct platform_device *pdev)
912 struct ibm_struct *ibm, *itmp;
914 list_for_each_entry_safe(ibm, itmp,
915 &tpacpi_all_drivers,
916 all_drivers) {
917 if (ibm->shutdown)
918 (ibm->shutdown)();
922 static struct platform_driver tpacpi_pdriver = {
923 .driver = {
924 .name = TPACPI_DRVR_NAME,
925 .owner = THIS_MODULE,
927 .suspend = tpacpi_suspend_handler,
928 .resume = tpacpi_resume_handler,
929 .shutdown = tpacpi_shutdown_handler,
932 static struct platform_driver tpacpi_hwmon_pdriver = {
933 .driver = {
934 .name = TPACPI_HWMON_DRVR_NAME,
935 .owner = THIS_MODULE,
939 /*************************************************************************
940 * sysfs support helpers
943 struct attribute_set {
944 unsigned int members, max_members;
945 struct attribute_group group;
948 struct attribute_set_obj {
949 struct attribute_set s;
950 struct attribute *a;
951 } __attribute__((packed));
953 static struct attribute_set *create_attr_set(unsigned int max_members,
954 const char *name)
956 struct attribute_set_obj *sobj;
958 if (max_members == 0)
959 return NULL;
961 /* Allocates space for implicit NULL at the end too */
962 sobj = kzalloc(sizeof(struct attribute_set_obj) +
963 max_members * sizeof(struct attribute *),
964 GFP_KERNEL);
965 if (!sobj)
966 return NULL;
967 sobj->s.max_members = max_members;
968 sobj->s.group.attrs = &sobj->a;
969 sobj->s.group.name = name;
971 return &sobj->s;
974 #define destroy_attr_set(_set) \
975 kfree(_set);
977 /* not multi-threaded safe, use it in a single thread per set */
978 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
980 if (!s || !attr)
981 return -EINVAL;
983 if (s->members >= s->max_members)
984 return -ENOMEM;
986 s->group.attrs[s->members] = attr;
987 s->members++;
989 return 0;
992 static int add_many_to_attr_set(struct attribute_set *s,
993 struct attribute **attr,
994 unsigned int count)
996 int i, res;
998 for (i = 0; i < count; i++) {
999 res = add_to_attr_set(s, attr[i]);
1000 if (res)
1001 return res;
1004 return 0;
1007 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1009 sysfs_remove_group(kobj, &s->group);
1010 destroy_attr_set(s);
1013 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1014 sysfs_create_group(_kobj, &_attr_set->group)
1016 static int parse_strtoul(const char *buf,
1017 unsigned long max, unsigned long *value)
1019 char *endp;
1021 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1022 endp = skip_spaces(endp);
1023 if (*endp || *value > max)
1024 return -EINVAL;
1026 return 0;
1029 static void tpacpi_disable_brightness_delay(void)
1031 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1032 printk(TPACPI_NOTICE
1033 "ACPI backlight control delay disabled\n");
1036 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1038 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1039 union acpi_object *obj;
1040 int rc;
1042 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1043 obj = (union acpi_object *)buffer.pointer;
1044 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1045 printk(TPACPI_ERR "Unknown _BCL data, "
1046 "please report this to %s\n", TPACPI_MAIL);
1047 rc = 0;
1048 } else {
1049 rc = obj->package.count;
1051 } else {
1052 return 0;
1055 kfree(buffer.pointer);
1056 return rc;
1059 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1060 u32 lvl, void *context, void **rv)
1062 char name[ACPI_PATH_SEGMENT_LENGTH];
1063 struct acpi_buffer buffer = { sizeof(name), &name };
1065 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1066 !strncmp("_BCL", name, sizeof(name) - 1)) {
1067 BUG_ON(!rv || !*rv);
1068 **(int **)rv = tpacpi_query_bcl_levels(handle);
1069 return AE_CTRL_TERMINATE;
1070 } else {
1071 return AE_OK;
1076 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1078 static int __init tpacpi_check_std_acpi_brightness_support(void)
1080 int status;
1081 int bcl_levels = 0;
1082 void *bcl_ptr = &bcl_levels;
1084 if (!vid_handle) {
1085 TPACPI_ACPIHANDLE_INIT(vid);
1087 if (!vid_handle)
1088 return 0;
1091 * Search for a _BCL method, and execute it. This is safe on all
1092 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1093 * BIOS in ACPI backlight control mode. We do NOT have to care
1094 * about calling the _BCL method in an enabled video device, any
1095 * will do for our purposes.
1098 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1099 tpacpi_acpi_walk_find_bcl, NULL, NULL,
1100 &bcl_ptr);
1102 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1103 tp_features.bright_acpimode = 1;
1104 return (bcl_levels - 2);
1107 return 0;
1110 static void printk_deprecated_attribute(const char * const what,
1111 const char * const details)
1113 tpacpi_log_usertask("deprecated sysfs attribute");
1114 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1115 "will be removed. %s\n",
1116 what, details);
1119 /*************************************************************************
1120 * rfkill and radio control support helpers
1124 * ThinkPad-ACPI firmware handling model:
1126 * WLSW (master wireless switch) is event-driven, and is common to all
1127 * firmware-controlled radios. It cannot be controlled, just monitored,
1128 * as expected. It overrides all radio state in firmware
1130 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1131 * (TODO: verify how WLSW interacts with the returned radio state).
1133 * The only time there are shadow radio state changes, is when
1134 * masked-off hotkeys are used.
1138 * Internal driver API for radio state:
1140 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1141 * bool: true means radio blocked (off)
1143 enum tpacpi_rfkill_state {
1144 TPACPI_RFK_RADIO_OFF = 0,
1145 TPACPI_RFK_RADIO_ON
1148 /* rfkill switches */
1149 enum tpacpi_rfk_id {
1150 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1151 TPACPI_RFK_WWAN_SW_ID,
1152 TPACPI_RFK_UWB_SW_ID,
1153 TPACPI_RFK_SW_MAX
1156 static const char *tpacpi_rfkill_names[] = {
1157 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1158 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1159 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1160 [TPACPI_RFK_SW_MAX] = NULL
1163 /* ThinkPad-ACPI rfkill subdriver */
1164 struct tpacpi_rfk {
1165 struct rfkill *rfkill;
1166 enum tpacpi_rfk_id id;
1167 const struct tpacpi_rfk_ops *ops;
1170 struct tpacpi_rfk_ops {
1171 /* firmware interface */
1172 int (*get_status)(void);
1173 int (*set_status)(const enum tpacpi_rfkill_state);
1176 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1178 /* Query FW and update rfkill sw state for a given rfkill switch */
1179 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1181 int status;
1183 if (!tp_rfk)
1184 return -ENODEV;
1186 status = (tp_rfk->ops->get_status)();
1187 if (status < 0)
1188 return status;
1190 rfkill_set_sw_state(tp_rfk->rfkill,
1191 (status == TPACPI_RFK_RADIO_OFF));
1193 return status;
1196 /* Query FW and update rfkill sw state for all rfkill switches */
1197 static void tpacpi_rfk_update_swstate_all(void)
1199 unsigned int i;
1201 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1202 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1206 * Sync the HW-blocking state of all rfkill switches,
1207 * do notice it causes the rfkill core to schedule uevents
1209 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1211 unsigned int i;
1212 struct tpacpi_rfk *tp_rfk;
1214 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1215 tp_rfk = tpacpi_rfkill_switches[i];
1216 if (tp_rfk) {
1217 if (rfkill_set_hw_state(tp_rfk->rfkill,
1218 blocked)) {
1219 /* ignore -- we track sw block */
1225 /* Call to get the WLSW state from the firmware */
1226 static int hotkey_get_wlsw(void);
1228 /* Call to query WLSW state and update all rfkill switches */
1229 static bool tpacpi_rfk_check_hwblock_state(void)
1231 int res = hotkey_get_wlsw();
1232 int hw_blocked;
1234 /* When unknown or unsupported, we have to assume it is unblocked */
1235 if (res < 0)
1236 return false;
1238 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1239 tpacpi_rfk_update_hwblock_state(hw_blocked);
1241 return hw_blocked;
1244 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1246 struct tpacpi_rfk *tp_rfk = data;
1247 int res;
1249 dbg_printk(TPACPI_DBG_RFKILL,
1250 "request to change radio state to %s\n",
1251 blocked ? "blocked" : "unblocked");
1253 /* try to set radio state */
1254 res = (tp_rfk->ops->set_status)(blocked ?
1255 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1257 /* and update the rfkill core with whatever the FW really did */
1258 tpacpi_rfk_update_swstate(tp_rfk);
1260 return (res < 0) ? res : 0;
1263 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1264 .set_block = tpacpi_rfk_hook_set_block,
1267 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1268 const struct tpacpi_rfk_ops *tp_rfkops,
1269 const enum rfkill_type rfktype,
1270 const char *name,
1271 const bool set_default)
1273 struct tpacpi_rfk *atp_rfk;
1274 int res;
1275 bool sw_state = false;
1276 bool hw_state;
1277 int sw_status;
1279 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1281 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1282 if (atp_rfk)
1283 atp_rfk->rfkill = rfkill_alloc(name,
1284 &tpacpi_pdev->dev,
1285 rfktype,
1286 &tpacpi_rfk_rfkill_ops,
1287 atp_rfk);
1288 if (!atp_rfk || !atp_rfk->rfkill) {
1289 printk(TPACPI_ERR
1290 "failed to allocate memory for rfkill class\n");
1291 kfree(atp_rfk);
1292 return -ENOMEM;
1295 atp_rfk->id = id;
1296 atp_rfk->ops = tp_rfkops;
1298 sw_status = (tp_rfkops->get_status)();
1299 if (sw_status < 0) {
1300 printk(TPACPI_ERR
1301 "failed to read initial state for %s, error %d\n",
1302 name, sw_status);
1303 } else {
1304 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1305 if (set_default) {
1306 /* try to keep the initial state, since we ask the
1307 * firmware to preserve it across S5 in NVRAM */
1308 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1311 hw_state = tpacpi_rfk_check_hwblock_state();
1312 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1314 res = rfkill_register(atp_rfk->rfkill);
1315 if (res < 0) {
1316 printk(TPACPI_ERR
1317 "failed to register %s rfkill switch: %d\n",
1318 name, res);
1319 rfkill_destroy(atp_rfk->rfkill);
1320 kfree(atp_rfk);
1321 return res;
1324 tpacpi_rfkill_switches[id] = atp_rfk;
1326 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1327 name, (sw_state || hw_state) ? "" : "un");
1328 return 0;
1331 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1333 struct tpacpi_rfk *tp_rfk;
1335 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1337 tp_rfk = tpacpi_rfkill_switches[id];
1338 if (tp_rfk) {
1339 rfkill_unregister(tp_rfk->rfkill);
1340 rfkill_destroy(tp_rfk->rfkill);
1341 tpacpi_rfkill_switches[id] = NULL;
1342 kfree(tp_rfk);
1346 static void printk_deprecated_rfkill_attribute(const char * const what)
1348 printk_deprecated_attribute(what,
1349 "Please switch to generic rfkill before year 2010");
1352 /* sysfs <radio> enable ------------------------------------------------ */
1353 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1354 struct device_attribute *attr,
1355 char *buf)
1357 int status;
1359 printk_deprecated_rfkill_attribute(attr->attr.name);
1361 /* This is in the ABI... */
1362 if (tpacpi_rfk_check_hwblock_state()) {
1363 status = TPACPI_RFK_RADIO_OFF;
1364 } else {
1365 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1366 if (status < 0)
1367 return status;
1370 return snprintf(buf, PAGE_SIZE, "%d\n",
1371 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1374 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1375 struct device_attribute *attr,
1376 const char *buf, size_t count)
1378 unsigned long t;
1379 int res;
1381 printk_deprecated_rfkill_attribute(attr->attr.name);
1383 if (parse_strtoul(buf, 1, &t))
1384 return -EINVAL;
1386 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1388 /* This is in the ABI... */
1389 if (tpacpi_rfk_check_hwblock_state() && !!t)
1390 return -EPERM;
1392 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1393 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1394 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1396 return (res < 0) ? res : count;
1399 /* procfs -------------------------------------------------------------- */
1400 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, 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);
2086 /* HKEY.MHKG() return bits */
2087 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2089 static int hotkey_get_wlsw(void)
2091 int status;
2093 if (!tp_features.hotkey_wlsw)
2094 return -ENODEV;
2096 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2097 if (dbg_wlswemul)
2098 return (tpacpi_wlsw_emulstate) ?
2099 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2100 #endif
2102 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2103 return -EIO;
2105 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2108 static int hotkey_get_tablet_mode(int *status)
2110 int s;
2112 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2113 return -EIO;
2115 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2116 return 0;
2120 * Reads current event mask from firmware, and updates
2121 * hotkey_acpi_mask accordingly. Also resets any bits
2122 * from hotkey_user_mask that are unavailable to be
2123 * delivered (shadow requirement of the userspace ABI).
2125 * Call with hotkey_mutex held
2127 static int hotkey_mask_get(void)
2129 if (tp_features.hotkey_mask) {
2130 u32 m = 0;
2132 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2133 return -EIO;
2135 hotkey_acpi_mask = m;
2136 } else {
2137 /* no mask support doesn't mean no event support... */
2138 hotkey_acpi_mask = hotkey_all_mask;
2141 /* sync userspace-visible mask */
2142 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2144 return 0;
2147 void static hotkey_mask_warn_incomplete_mask(void)
2149 /* log only what the user can fix... */
2150 const u32 wantedmask = hotkey_driver_mask &
2151 ~(hotkey_acpi_mask | hotkey_source_mask) &
2152 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2154 if (wantedmask)
2155 printk(TPACPI_NOTICE
2156 "required events 0x%08x not enabled!\n",
2157 wantedmask);
2161 * Set the firmware mask when supported
2163 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2165 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2167 * Call with hotkey_mutex held
2169 static int hotkey_mask_set(u32 mask)
2171 int i;
2172 int rc = 0;
2174 const u32 fwmask = mask & ~hotkey_source_mask;
2176 if (tp_features.hotkey_mask) {
2177 for (i = 0; i < 32; i++) {
2178 if (!acpi_evalf(hkey_handle,
2179 NULL, "MHKM", "vdd", i + 1,
2180 !!(mask & (1 << i)))) {
2181 rc = -EIO;
2182 break;
2188 * We *must* make an inconditional call to hotkey_mask_get to
2189 * refresh hotkey_acpi_mask and update hotkey_user_mask
2191 * Take the opportunity to also log when we cannot _enable_
2192 * a given event.
2194 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2195 printk(TPACPI_NOTICE
2196 "asked for hotkey mask 0x%08x, but "
2197 "firmware forced it to 0x%08x\n",
2198 fwmask, hotkey_acpi_mask);
2201 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2202 hotkey_mask_warn_incomplete_mask();
2204 return rc;
2208 * Sets hotkey_user_mask and tries to set the firmware mask
2210 * Call with hotkey_mutex held
2212 static int hotkey_user_mask_set(const u32 mask)
2214 int rc;
2216 /* Give people a chance to notice they are doing something that
2217 * is bound to go boom on their users sooner or later */
2218 if (!tp_warned.hotkey_mask_ff &&
2219 (mask == 0xffff || mask == 0xffffff ||
2220 mask == 0xffffffff)) {
2221 tp_warned.hotkey_mask_ff = 1;
2222 printk(TPACPI_NOTICE
2223 "setting the hotkey mask to 0x%08x is likely "
2224 "not the best way to go about it\n", mask);
2225 printk(TPACPI_NOTICE
2226 "please consider using the driver defaults, "
2227 "and refer to up-to-date thinkpad-acpi "
2228 "documentation\n");
2231 /* Try to enable what the user asked for, plus whatever we need.
2232 * this syncs everything but won't enable bits in hotkey_user_mask */
2233 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2235 /* Enable the available bits in hotkey_user_mask */
2236 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2238 return rc;
2242 * Sets the driver hotkey mask.
2244 * Can be called even if the hotkey subdriver is inactive
2246 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2248 int rc;
2250 /* Do the right thing if hotkey_init has not been called yet */
2251 if (!tp_features.hotkey) {
2252 hotkey_driver_mask = mask;
2253 return 0;
2256 mutex_lock(&hotkey_mutex);
2258 HOTKEY_CONFIG_CRITICAL_START
2259 hotkey_driver_mask = mask;
2260 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2261 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2262 #endif
2263 HOTKEY_CONFIG_CRITICAL_END
2265 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2266 ~hotkey_source_mask);
2267 mutex_unlock(&hotkey_mutex);
2269 return rc;
2272 static int hotkey_status_get(int *status)
2274 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2275 return -EIO;
2277 return 0;
2280 static int hotkey_status_set(bool enable)
2282 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2283 return -EIO;
2285 return 0;
2288 static void tpacpi_input_send_tabletsw(void)
2290 int state;
2292 if (tp_features.hotkey_tablet &&
2293 !hotkey_get_tablet_mode(&state)) {
2294 mutex_lock(&tpacpi_inputdev_send_mutex);
2296 input_report_switch(tpacpi_inputdev,
2297 SW_TABLET_MODE, !!state);
2298 input_sync(tpacpi_inputdev);
2300 mutex_unlock(&tpacpi_inputdev_send_mutex);
2304 /* Do NOT call without validating scancode first */
2305 static void tpacpi_input_send_key(const unsigned int scancode)
2307 const unsigned int keycode = hotkey_keycode_map[scancode];
2309 if (keycode != KEY_RESERVED) {
2310 mutex_lock(&tpacpi_inputdev_send_mutex);
2312 input_report_key(tpacpi_inputdev, keycode, 1);
2313 if (keycode == KEY_UNKNOWN)
2314 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2315 scancode);
2316 input_sync(tpacpi_inputdev);
2318 input_report_key(tpacpi_inputdev, keycode, 0);
2319 if (keycode == KEY_UNKNOWN)
2320 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2321 scancode);
2322 input_sync(tpacpi_inputdev);
2324 mutex_unlock(&tpacpi_inputdev_send_mutex);
2328 /* Do NOT call without validating scancode first */
2329 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2331 hotkey_driver_event(scancode);
2332 if (hotkey_user_mask & (1 << scancode))
2333 tpacpi_input_send_key(scancode);
2336 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2337 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2339 /* Do NOT call without validating scancode first */
2340 static void tpacpi_hotkey_send_key(unsigned int scancode)
2342 tpacpi_input_send_key_masked(scancode);
2343 if (hotkey_report_mode < 2) {
2344 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2345 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2349 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2351 u8 d;
2353 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2354 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2355 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2356 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2357 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2358 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2360 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2361 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2362 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2364 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2365 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2366 n->displayexp_toggle =
2367 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2369 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2370 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2371 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2372 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2373 n->brightness_toggle =
2374 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2376 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2377 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2378 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2379 >> TP_NVRAM_POS_LEVEL_VOLUME;
2380 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2381 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2385 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2386 struct tp_nvram_state *newn,
2387 const u32 event_mask)
2390 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2391 do { \
2392 if ((event_mask & (1 << __scancode)) && \
2393 oldn->__member != newn->__member) \
2394 tpacpi_hotkey_send_key(__scancode); \
2395 } while (0)
2397 #define TPACPI_MAY_SEND_KEY(__scancode) \
2398 do { \
2399 if (event_mask & (1 << __scancode)) \
2400 tpacpi_hotkey_send_key(__scancode); \
2401 } while (0)
2403 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2404 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2405 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2406 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2408 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2410 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2412 /* handle volume */
2413 if (oldn->volume_toggle != newn->volume_toggle) {
2414 if (oldn->mute != newn->mute) {
2415 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2417 if (oldn->volume_level > newn->volume_level) {
2418 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2419 } else if (oldn->volume_level < newn->volume_level) {
2420 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2421 } else if (oldn->mute == newn->mute) {
2422 /* repeated key presses that didn't change state */
2423 if (newn->mute) {
2424 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2425 } else if (newn->volume_level != 0) {
2426 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2427 } else {
2428 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2433 /* handle brightness */
2434 if (oldn->brightness_toggle != newn->brightness_toggle) {
2435 if (oldn->brightness_level < newn->brightness_level) {
2436 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2437 } else if (oldn->brightness_level > newn->brightness_level) {
2438 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2439 } else {
2440 /* repeated key presses that didn't change state */
2441 if (newn->brightness_level != 0) {
2442 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2443 } else {
2444 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2449 #undef TPACPI_COMPARE_KEY
2450 #undef TPACPI_MAY_SEND_KEY
2454 * Polling driver
2456 * We track all events in hotkey_source_mask all the time, since
2457 * most of them are edge-based. We only issue those requested by
2458 * hotkey_user_mask or hotkey_driver_mask, though.
2460 static int hotkey_kthread(void *data)
2462 struct tp_nvram_state s[2];
2463 u32 poll_mask, event_mask;
2464 unsigned int si, so;
2465 unsigned long t;
2466 unsigned int change_detector, must_reset;
2467 unsigned int poll_freq;
2469 mutex_lock(&hotkey_thread_mutex);
2471 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2472 goto exit;
2474 set_freezable();
2476 so = 0;
2477 si = 1;
2478 t = 0;
2480 /* Initial state for compares */
2481 mutex_lock(&hotkey_thread_data_mutex);
2482 change_detector = hotkey_config_change;
2483 poll_mask = hotkey_source_mask;
2484 event_mask = hotkey_source_mask &
2485 (hotkey_driver_mask | hotkey_user_mask);
2486 poll_freq = hotkey_poll_freq;
2487 mutex_unlock(&hotkey_thread_data_mutex);
2488 hotkey_read_nvram(&s[so], poll_mask);
2490 while (!kthread_should_stop()) {
2491 if (t == 0) {
2492 if (likely(poll_freq))
2493 t = 1000/poll_freq;
2494 else
2495 t = 100; /* should never happen... */
2497 t = msleep_interruptible(t);
2498 if (unlikely(kthread_should_stop()))
2499 break;
2500 must_reset = try_to_freeze();
2501 if (t > 0 && !must_reset)
2502 continue;
2504 mutex_lock(&hotkey_thread_data_mutex);
2505 if (must_reset || hotkey_config_change != change_detector) {
2506 /* forget old state on thaw or config change */
2507 si = so;
2508 t = 0;
2509 change_detector = hotkey_config_change;
2511 poll_mask = hotkey_source_mask;
2512 event_mask = hotkey_source_mask &
2513 (hotkey_driver_mask | hotkey_user_mask);
2514 poll_freq = hotkey_poll_freq;
2515 mutex_unlock(&hotkey_thread_data_mutex);
2517 if (likely(poll_mask)) {
2518 hotkey_read_nvram(&s[si], poll_mask);
2519 if (likely(si != so)) {
2520 hotkey_compare_and_issue_event(&s[so], &s[si],
2521 event_mask);
2525 so = si;
2526 si ^= 1;
2529 exit:
2530 mutex_unlock(&hotkey_thread_mutex);
2531 return 0;
2534 /* call with hotkey_mutex held */
2535 static void hotkey_poll_stop_sync(void)
2537 if (tpacpi_hotkey_task) {
2538 if (frozen(tpacpi_hotkey_task) ||
2539 freezing(tpacpi_hotkey_task))
2540 thaw_process(tpacpi_hotkey_task);
2542 kthread_stop(tpacpi_hotkey_task);
2543 tpacpi_hotkey_task = NULL;
2544 mutex_lock(&hotkey_thread_mutex);
2545 /* at this point, the thread did exit */
2546 mutex_unlock(&hotkey_thread_mutex);
2550 /* call with hotkey_mutex held */
2551 static void hotkey_poll_setup(bool may_warn)
2553 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2554 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2556 if (hotkey_poll_freq > 0 &&
2557 (poll_driver_mask ||
2558 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2559 if (!tpacpi_hotkey_task) {
2560 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2561 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2562 if (IS_ERR(tpacpi_hotkey_task)) {
2563 tpacpi_hotkey_task = NULL;
2564 printk(TPACPI_ERR
2565 "could not create kernel thread "
2566 "for hotkey polling\n");
2569 } else {
2570 hotkey_poll_stop_sync();
2571 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2572 hotkey_poll_freq == 0) {
2573 printk(TPACPI_NOTICE
2574 "hot keys 0x%08x and/or events 0x%08x "
2575 "require polling, which is currently "
2576 "disabled\n",
2577 poll_user_mask, poll_driver_mask);
2582 static void hotkey_poll_setup_safe(bool may_warn)
2584 mutex_lock(&hotkey_mutex);
2585 hotkey_poll_setup(may_warn);
2586 mutex_unlock(&hotkey_mutex);
2589 /* call with hotkey_mutex held */
2590 static void hotkey_poll_set_freq(unsigned int freq)
2592 if (!freq)
2593 hotkey_poll_stop_sync();
2595 hotkey_poll_freq = freq;
2598 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2600 static void hotkey_poll_setup_safe(bool __unused)
2604 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2606 static int hotkey_inputdev_open(struct input_dev *dev)
2608 switch (tpacpi_lifecycle) {
2609 case TPACPI_LIFE_INIT:
2611 * hotkey_init will call hotkey_poll_setup_safe
2612 * at the appropriate moment
2614 return 0;
2615 case TPACPI_LIFE_EXITING:
2616 return -EBUSY;
2617 case TPACPI_LIFE_RUNNING:
2618 hotkey_poll_setup_safe(false);
2619 return 0;
2622 /* Should only happen if tpacpi_lifecycle is corrupt */
2623 BUG();
2624 return -EBUSY;
2627 static void hotkey_inputdev_close(struct input_dev *dev)
2629 /* disable hotkey polling when possible */
2630 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING &&
2631 !(hotkey_source_mask & hotkey_driver_mask))
2632 hotkey_poll_setup_safe(false);
2635 /* sysfs hotkey enable ------------------------------------------------- */
2636 static ssize_t hotkey_enable_show(struct device *dev,
2637 struct device_attribute *attr,
2638 char *buf)
2640 int res, status;
2642 printk_deprecated_attribute("hotkey_enable",
2643 "Hotkey reporting is always enabled");
2645 res = hotkey_status_get(&status);
2646 if (res)
2647 return res;
2649 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2652 static ssize_t hotkey_enable_store(struct device *dev,
2653 struct device_attribute *attr,
2654 const char *buf, size_t count)
2656 unsigned long t;
2658 printk_deprecated_attribute("hotkey_enable",
2659 "Hotkeys can be disabled through hotkey_mask");
2661 if (parse_strtoul(buf, 1, &t))
2662 return -EINVAL;
2664 if (t == 0)
2665 return -EPERM;
2667 return count;
2670 static struct device_attribute dev_attr_hotkey_enable =
2671 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2672 hotkey_enable_show, hotkey_enable_store);
2674 /* sysfs hotkey mask --------------------------------------------------- */
2675 static ssize_t hotkey_mask_show(struct device *dev,
2676 struct device_attribute *attr,
2677 char *buf)
2679 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2682 static ssize_t hotkey_mask_store(struct device *dev,
2683 struct device_attribute *attr,
2684 const char *buf, size_t count)
2686 unsigned long t;
2687 int res;
2689 if (parse_strtoul(buf, 0xffffffffUL, &t))
2690 return -EINVAL;
2692 if (mutex_lock_killable(&hotkey_mutex))
2693 return -ERESTARTSYS;
2695 res = hotkey_user_mask_set(t);
2697 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2698 hotkey_poll_setup(true);
2699 #endif
2701 mutex_unlock(&hotkey_mutex);
2703 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2705 return (res) ? res : count;
2708 static struct device_attribute dev_attr_hotkey_mask =
2709 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2710 hotkey_mask_show, hotkey_mask_store);
2712 /* sysfs hotkey bios_enabled ------------------------------------------- */
2713 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2714 struct device_attribute *attr,
2715 char *buf)
2717 return sprintf(buf, "0\n");
2720 static struct device_attribute dev_attr_hotkey_bios_enabled =
2721 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2723 /* sysfs hotkey bios_mask ---------------------------------------------- */
2724 static ssize_t hotkey_bios_mask_show(struct device *dev,
2725 struct device_attribute *attr,
2726 char *buf)
2728 printk_deprecated_attribute("hotkey_bios_mask",
2729 "This attribute is useless.");
2730 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2733 static struct device_attribute dev_attr_hotkey_bios_mask =
2734 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2736 /* sysfs hotkey all_mask ----------------------------------------------- */
2737 static ssize_t hotkey_all_mask_show(struct device *dev,
2738 struct device_attribute *attr,
2739 char *buf)
2741 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2742 hotkey_all_mask | hotkey_source_mask);
2745 static struct device_attribute dev_attr_hotkey_all_mask =
2746 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2748 /* sysfs hotkey recommended_mask --------------------------------------- */
2749 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2750 struct device_attribute *attr,
2751 char *buf)
2753 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2754 (hotkey_all_mask | hotkey_source_mask)
2755 & ~hotkey_reserved_mask);
2758 static struct device_attribute dev_attr_hotkey_recommended_mask =
2759 __ATTR(hotkey_recommended_mask, S_IRUGO,
2760 hotkey_recommended_mask_show, NULL);
2762 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2764 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2765 static ssize_t hotkey_source_mask_show(struct device *dev,
2766 struct device_attribute *attr,
2767 char *buf)
2769 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2772 static ssize_t hotkey_source_mask_store(struct device *dev,
2773 struct device_attribute *attr,
2774 const char *buf, size_t count)
2776 unsigned long t;
2777 u32 r_ev;
2778 int rc;
2780 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2781 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2782 return -EINVAL;
2784 if (mutex_lock_killable(&hotkey_mutex))
2785 return -ERESTARTSYS;
2787 HOTKEY_CONFIG_CRITICAL_START
2788 hotkey_source_mask = t;
2789 HOTKEY_CONFIG_CRITICAL_END
2791 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2792 ~hotkey_source_mask);
2793 hotkey_poll_setup(true);
2795 /* check if events needed by the driver got disabled */
2796 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2797 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2799 mutex_unlock(&hotkey_mutex);
2801 if (rc < 0)
2802 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2803 "firmware event mask!\n");
2805 if (r_ev)
2806 printk(TPACPI_NOTICE "hotkey_source_mask: "
2807 "some important events were disabled: "
2808 "0x%04x\n", r_ev);
2810 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2812 return (rc < 0) ? rc : count;
2815 static struct device_attribute dev_attr_hotkey_source_mask =
2816 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2817 hotkey_source_mask_show, hotkey_source_mask_store);
2819 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2820 static ssize_t hotkey_poll_freq_show(struct device *dev,
2821 struct device_attribute *attr,
2822 char *buf)
2824 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2827 static ssize_t hotkey_poll_freq_store(struct device *dev,
2828 struct device_attribute *attr,
2829 const char *buf, size_t count)
2831 unsigned long t;
2833 if (parse_strtoul(buf, 25, &t))
2834 return -EINVAL;
2836 if (mutex_lock_killable(&hotkey_mutex))
2837 return -ERESTARTSYS;
2839 hotkey_poll_set_freq(t);
2840 hotkey_poll_setup(true);
2842 mutex_unlock(&hotkey_mutex);
2844 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2846 return count;
2849 static struct device_attribute dev_attr_hotkey_poll_freq =
2850 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2851 hotkey_poll_freq_show, hotkey_poll_freq_store);
2853 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2855 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2856 static ssize_t hotkey_radio_sw_show(struct device *dev,
2857 struct device_attribute *attr,
2858 char *buf)
2860 int res;
2861 res = hotkey_get_wlsw();
2862 if (res < 0)
2863 return res;
2865 /* Opportunistic update */
2866 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2868 return snprintf(buf, PAGE_SIZE, "%d\n",
2869 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2872 static struct device_attribute dev_attr_hotkey_radio_sw =
2873 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2875 static void hotkey_radio_sw_notify_change(void)
2877 if (tp_features.hotkey_wlsw)
2878 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2879 "hotkey_radio_sw");
2882 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2883 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2884 struct device_attribute *attr,
2885 char *buf)
2887 int res, s;
2888 res = hotkey_get_tablet_mode(&s);
2889 if (res < 0)
2890 return res;
2892 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2895 static struct device_attribute dev_attr_hotkey_tablet_mode =
2896 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2898 static void hotkey_tablet_mode_notify_change(void)
2900 if (tp_features.hotkey_tablet)
2901 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2902 "hotkey_tablet_mode");
2905 /* sysfs hotkey report_mode -------------------------------------------- */
2906 static ssize_t hotkey_report_mode_show(struct device *dev,
2907 struct device_attribute *attr,
2908 char *buf)
2910 return snprintf(buf, PAGE_SIZE, "%d\n",
2911 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2914 static struct device_attribute dev_attr_hotkey_report_mode =
2915 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2917 /* sysfs wakeup reason (pollable) -------------------------------------- */
2918 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2919 struct device_attribute *attr,
2920 char *buf)
2922 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2925 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2926 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2928 static void hotkey_wakeup_reason_notify_change(void)
2930 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2931 "wakeup_reason");
2934 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2935 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2936 struct device_attribute *attr,
2937 char *buf)
2939 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2942 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2943 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2944 hotkey_wakeup_hotunplug_complete_show, NULL);
2946 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2948 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2949 "wakeup_hotunplug_complete");
2952 /* --------------------------------------------------------------------- */
2954 static struct attribute *hotkey_attributes[] __initdata = {
2955 &dev_attr_hotkey_enable.attr,
2956 &dev_attr_hotkey_bios_enabled.attr,
2957 &dev_attr_hotkey_bios_mask.attr,
2958 &dev_attr_hotkey_report_mode.attr,
2959 &dev_attr_hotkey_wakeup_reason.attr,
2960 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2961 &dev_attr_hotkey_mask.attr,
2962 &dev_attr_hotkey_all_mask.attr,
2963 &dev_attr_hotkey_recommended_mask.attr,
2964 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2965 &dev_attr_hotkey_source_mask.attr,
2966 &dev_attr_hotkey_poll_freq.attr,
2967 #endif
2971 * Sync both the hw and sw blocking state of all switches
2973 static void tpacpi_send_radiosw_update(void)
2975 int wlsw;
2978 * We must sync all rfkill controllers *before* issuing any
2979 * rfkill input events, or we will race the rfkill core input
2980 * handler.
2982 * tpacpi_inputdev_send_mutex works as a syncronization point
2983 * for the above.
2985 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2988 wlsw = hotkey_get_wlsw();
2990 /* Sync hw blocking state first if it is hw-blocked */
2991 if (wlsw == TPACPI_RFK_RADIO_OFF)
2992 tpacpi_rfk_update_hwblock_state(true);
2994 /* Sync sw blocking state */
2995 tpacpi_rfk_update_swstate_all();
2997 /* Sync hw blocking state last if it is hw-unblocked */
2998 if (wlsw == TPACPI_RFK_RADIO_ON)
2999 tpacpi_rfk_update_hwblock_state(false);
3001 /* Issue rfkill input event for WLSW switch */
3002 if (!(wlsw < 0)) {
3003 mutex_lock(&tpacpi_inputdev_send_mutex);
3005 input_report_switch(tpacpi_inputdev,
3006 SW_RFKILL_ALL, (wlsw > 0));
3007 input_sync(tpacpi_inputdev);
3009 mutex_unlock(&tpacpi_inputdev_send_mutex);
3013 * this can be unconditional, as we will poll state again
3014 * if userspace uses the notify to read data
3016 hotkey_radio_sw_notify_change();
3019 static void hotkey_exit(void)
3021 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3022 mutex_lock(&hotkey_mutex);
3023 hotkey_poll_stop_sync();
3024 mutex_unlock(&hotkey_mutex);
3025 #endif
3027 if (hotkey_dev_attributes)
3028 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3030 kfree(hotkey_keycode_map);
3032 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3033 "restoring original HKEY status and mask\n");
3034 /* yes, there is a bitwise or below, we want the
3035 * functions to be called even if one of them fail */
3036 if (((tp_features.hotkey_mask &&
3037 hotkey_mask_set(hotkey_orig_mask)) |
3038 hotkey_status_set(false)) != 0)
3039 printk(TPACPI_ERR
3040 "failed to restore hot key mask "
3041 "to BIOS defaults\n");
3044 static void __init hotkey_unmap(const unsigned int scancode)
3046 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3047 clear_bit(hotkey_keycode_map[scancode],
3048 tpacpi_inputdev->keybit);
3049 hotkey_keycode_map[scancode] = KEY_RESERVED;
3054 * HKEY quirks:
3055 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3058 #define TPACPI_HK_Q_INIMASK 0x0001
3060 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3061 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3062 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3063 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3064 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3065 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3066 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3067 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3068 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3069 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3070 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3071 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3072 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3073 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3074 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3075 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3076 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3077 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3078 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3079 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3082 static int __init hotkey_init(struct ibm_init_struct *iibm)
3084 /* Requirements for changing the default keymaps:
3086 * 1. Many of the keys are mapped to KEY_RESERVED for very
3087 * good reasons. Do not change them unless you have deep
3088 * knowledge on the IBM and Lenovo ThinkPad firmware for
3089 * the various ThinkPad models. The driver behaves
3090 * differently for KEY_RESERVED: such keys have their
3091 * hot key mask *unset* in mask_recommended, and also
3092 * in the initial hot key mask programmed into the
3093 * firmware at driver load time, which means the firm-
3094 * ware may react very differently if you change them to
3095 * something else;
3097 * 2. You must be subscribed to the linux-thinkpad and
3098 * ibm-acpi-devel mailing lists, and you should read the
3099 * list archives since 2007 if you want to change the
3100 * keymaps. This requirement exists so that you will
3101 * know the past history of problems with the thinkpad-
3102 * acpi driver keymaps, and also that you will be
3103 * listening to any bug reports;
3105 * 3. Do not send thinkpad-acpi specific patches directly to
3106 * for merging, *ever*. Send them to the linux-acpi
3107 * mailinglist for comments. Merging is to be done only
3108 * through acpi-test and the ACPI maintainer.
3110 * If the above is too much to ask, don't change the keymap.
3111 * Ask the thinkpad-acpi maintainer to do it, instead.
3113 static u16 ibm_keycode_map[] __initdata = {
3114 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3115 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3116 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3117 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3119 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3120 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3121 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3122 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3124 /* brightness: firmware always reacts to them */
3125 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3126 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3128 /* Thinklight: firmware always react to it */
3129 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3131 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3132 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3134 /* Volume: firmware always react to it and reprograms
3135 * the built-in *extra* mixer. Never map it to control
3136 * another mixer by default. */
3137 KEY_RESERVED, /* 0x14: VOLUME UP */
3138 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3139 KEY_RESERVED, /* 0x16: MUTE */
3141 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3143 /* (assignments unknown, please report if found) */
3144 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3145 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3147 static u16 lenovo_keycode_map[] __initdata = {
3148 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3149 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3150 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3151 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3153 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3154 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3155 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3156 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3158 /* These should be enabled --only-- when ACPI video
3159 * is disabled (i.e. in "vendor" mode), and are handled
3160 * in a special way by the init code */
3161 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3162 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3164 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3166 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3167 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3169 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3170 * react to it and reprograms the built-in *extra* mixer.
3171 * Never map it to control another mixer by default.
3173 * T60?, T61, R60?, R61: firmware and EC tries to send
3174 * these over the regular keyboard, so these are no-ops,
3175 * but there are still weird bugs re. MUTE, so do not
3176 * change unless you get test reports from all Lenovo
3177 * models. May cause the BIOS to interfere with the
3178 * HDA mixer.
3180 KEY_RESERVED, /* 0x14: VOLUME UP */
3181 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3182 KEY_RESERVED, /* 0x16: MUTE */
3184 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3186 /* (assignments unknown, please report if found) */
3187 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3188 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3191 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3192 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3193 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3195 int res, i;
3196 int status;
3197 int hkeyv;
3198 bool radiosw_state = false;
3199 bool tabletsw_state = false;
3201 unsigned long quirks;
3203 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3204 "initializing hotkey subdriver\n");
3206 BUG_ON(!tpacpi_inputdev);
3207 BUG_ON(tpacpi_inputdev->open != NULL ||
3208 tpacpi_inputdev->close != NULL);
3210 TPACPI_ACPIHANDLE_INIT(hkey);
3211 mutex_init(&hotkey_mutex);
3213 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3214 mutex_init(&hotkey_thread_mutex);
3215 mutex_init(&hotkey_thread_data_mutex);
3216 #endif
3218 /* hotkey not supported on 570 */
3219 tp_features.hotkey = hkey_handle != NULL;
3221 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3222 "hotkeys are %s\n",
3223 str_supported(tp_features.hotkey));
3225 if (!tp_features.hotkey)
3226 return 1;
3228 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3229 ARRAY_SIZE(tpacpi_hotkey_qtable));
3231 tpacpi_disable_brightness_delay();
3233 /* MUST have enough space for all attributes to be added to
3234 * hotkey_dev_attributes */
3235 hotkey_dev_attributes = create_attr_set(
3236 ARRAY_SIZE(hotkey_attributes) + 2,
3237 NULL);
3238 if (!hotkey_dev_attributes)
3239 return -ENOMEM;
3240 res = add_many_to_attr_set(hotkey_dev_attributes,
3241 hotkey_attributes,
3242 ARRAY_SIZE(hotkey_attributes));
3243 if (res)
3244 goto err_exit;
3246 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3247 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3248 for HKEY interface version 0x100 */
3249 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3250 if ((hkeyv >> 8) != 1) {
3251 printk(TPACPI_ERR "unknown version of the "
3252 "HKEY interface: 0x%x\n", hkeyv);
3253 printk(TPACPI_ERR "please report this to %s\n",
3254 TPACPI_MAIL);
3255 } else {
3257 * MHKV 0x100 in A31, R40, R40e,
3258 * T4x, X31, and later
3260 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3261 "firmware HKEY interface version: 0x%x\n",
3262 hkeyv);
3264 /* Paranoia check AND init hotkey_all_mask */
3265 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3266 "MHKA", "qd")) {
3267 printk(TPACPI_ERR
3268 "missing MHKA handler, "
3269 "please report this to %s\n",
3270 TPACPI_MAIL);
3271 /* Fallback: pre-init for FN+F3,F4,F12 */
3272 hotkey_all_mask = 0x080cU;
3273 } else {
3274 tp_features.hotkey_mask = 1;
3279 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3280 "hotkey masks are %s\n",
3281 str_supported(tp_features.hotkey_mask));
3283 /* Init hotkey_all_mask if not initialized yet */
3284 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3285 (quirks & TPACPI_HK_Q_INIMASK))
3286 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3288 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3289 if (tp_features.hotkey_mask) {
3290 /* hotkey_source_mask *must* be zero for
3291 * the first hotkey_mask_get to return hotkey_orig_mask */
3292 res = hotkey_mask_get();
3293 if (res)
3294 goto err_exit;
3296 hotkey_orig_mask = hotkey_acpi_mask;
3297 } else {
3298 hotkey_orig_mask = hotkey_all_mask;
3299 hotkey_acpi_mask = hotkey_all_mask;
3302 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3303 if (dbg_wlswemul) {
3304 tp_features.hotkey_wlsw = 1;
3305 radiosw_state = !!tpacpi_wlsw_emulstate;
3306 printk(TPACPI_INFO
3307 "radio switch emulation enabled\n");
3308 } else
3309 #endif
3310 /* Not all thinkpads have a hardware radio switch */
3311 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3312 tp_features.hotkey_wlsw = 1;
3313 radiosw_state = !!status;
3314 printk(TPACPI_INFO
3315 "radio switch found; radios are %s\n",
3316 enabled(status, 0));
3318 if (tp_features.hotkey_wlsw)
3319 res = add_to_attr_set(hotkey_dev_attributes,
3320 &dev_attr_hotkey_radio_sw.attr);
3322 /* For X41t, X60t, X61t Tablets... */
3323 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3324 tp_features.hotkey_tablet = 1;
3325 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3326 printk(TPACPI_INFO
3327 "possible tablet mode switch found; "
3328 "ThinkPad in %s mode\n",
3329 (tabletsw_state) ? "tablet" : "laptop");
3330 res = add_to_attr_set(hotkey_dev_attributes,
3331 &dev_attr_hotkey_tablet_mode.attr);
3334 if (!res)
3335 res = register_attr_set_with_sysfs(
3336 hotkey_dev_attributes,
3337 &tpacpi_pdev->dev.kobj);
3338 if (res)
3339 goto err_exit;
3341 /* Set up key map */
3343 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3344 GFP_KERNEL);
3345 if (!hotkey_keycode_map) {
3346 printk(TPACPI_ERR
3347 "failed to allocate memory for key map\n");
3348 res = -ENOMEM;
3349 goto err_exit;
3352 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
3353 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3354 "using Lenovo default hot key map\n");
3355 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3356 TPACPI_HOTKEY_MAP_SIZE);
3357 } else {
3358 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3359 "using IBM default hot key map\n");
3360 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3361 TPACPI_HOTKEY_MAP_SIZE);
3364 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3365 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3366 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3367 tpacpi_inputdev->keycode = hotkey_keycode_map;
3368 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3369 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3370 input_set_capability(tpacpi_inputdev, EV_KEY,
3371 hotkey_keycode_map[i]);
3372 } else {
3373 if (i < sizeof(hotkey_reserved_mask)*8)
3374 hotkey_reserved_mask |= 1 << i;
3378 if (tp_features.hotkey_wlsw) {
3379 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3380 input_report_switch(tpacpi_inputdev,
3381 SW_RFKILL_ALL, radiosw_state);
3383 if (tp_features.hotkey_tablet) {
3384 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3385 input_report_switch(tpacpi_inputdev,
3386 SW_TABLET_MODE, tabletsw_state);
3389 /* Do not issue duplicate brightness change events to
3390 * userspace */
3391 if (!tp_features.bright_acpimode)
3392 /* update bright_acpimode... */
3393 tpacpi_check_std_acpi_brightness_support();
3395 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3396 printk(TPACPI_INFO
3397 "This ThinkPad has standard ACPI backlight "
3398 "brightness control, supported by the ACPI "
3399 "video driver\n");
3400 printk(TPACPI_NOTICE
3401 "Disabling thinkpad-acpi brightness events "
3402 "by default...\n");
3404 /* Disable brightness up/down on Lenovo thinkpads when
3405 * ACPI is handling them, otherwise it is plain impossible
3406 * for userspace to do something even remotely sane */
3407 hotkey_reserved_mask |=
3408 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3409 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3410 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3411 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3414 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3415 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3416 & ~hotkey_all_mask
3417 & ~hotkey_reserved_mask;
3419 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3420 "hotkey source mask 0x%08x, polling freq %u\n",
3421 hotkey_source_mask, hotkey_poll_freq);
3422 #endif
3424 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3425 "enabling firmware HKEY event interface...\n");
3426 res = hotkey_status_set(true);
3427 if (res) {
3428 hotkey_exit();
3429 return res;
3431 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3432 | hotkey_driver_mask)
3433 & ~hotkey_source_mask);
3434 if (res < 0 && res != -ENXIO) {
3435 hotkey_exit();
3436 return res;
3438 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3439 & ~hotkey_reserved_mask;
3440 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3441 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3442 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3444 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3445 "legacy ibm/hotkey event reporting over procfs %s\n",
3446 (hotkey_report_mode < 2) ?
3447 "enabled" : "disabled");
3449 tpacpi_inputdev->open = &hotkey_inputdev_open;
3450 tpacpi_inputdev->close = &hotkey_inputdev_close;
3452 hotkey_poll_setup_safe(true);
3454 return 0;
3456 err_exit:
3457 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3458 hotkey_dev_attributes = NULL;
3460 return (res < 0)? res : 1;
3463 static bool hotkey_notify_hotkey(const u32 hkey,
3464 bool *send_acpi_ev,
3465 bool *ignore_acpi_ev)
3467 /* 0x1000-0x1FFF: key presses */
3468 unsigned int scancode = hkey & 0xfff;
3469 *send_acpi_ev = true;
3470 *ignore_acpi_ev = false;
3472 if (scancode > 0 && scancode < 0x21) {
3473 scancode--;
3474 if (!(hotkey_source_mask & (1 << scancode))) {
3475 tpacpi_input_send_key_masked(scancode);
3476 *send_acpi_ev = false;
3477 } else {
3478 *ignore_acpi_ev = true;
3480 return true;
3482 return false;
3485 static bool hotkey_notify_wakeup(const u32 hkey,
3486 bool *send_acpi_ev,
3487 bool *ignore_acpi_ev)
3489 /* 0x2000-0x2FFF: Wakeup reason */
3490 *send_acpi_ev = true;
3491 *ignore_acpi_ev = false;
3493 switch (hkey) {
3494 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3495 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3496 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3497 *ignore_acpi_ev = true;
3498 break;
3500 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3501 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3502 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3503 *ignore_acpi_ev = true;
3504 break;
3506 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3507 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3508 printk(TPACPI_ALERT
3509 "EMERGENCY WAKEUP: battery almost empty\n");
3510 /* how to auto-heal: */
3511 /* 2313: woke up from S3, go to S4/S5 */
3512 /* 2413: woke up from S4, go to S5 */
3513 break;
3515 default:
3516 return false;
3519 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3520 printk(TPACPI_INFO
3521 "woke up due to a hot-unplug "
3522 "request...\n");
3523 hotkey_wakeup_reason_notify_change();
3525 return true;
3528 static bool hotkey_notify_usrevent(const u32 hkey,
3529 bool *send_acpi_ev,
3530 bool *ignore_acpi_ev)
3532 /* 0x5000-0x5FFF: human interface helpers */
3533 *send_acpi_ev = true;
3534 *ignore_acpi_ev = false;
3536 switch (hkey) {
3537 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3538 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3539 return true;
3541 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3542 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3543 tpacpi_input_send_tabletsw();
3544 hotkey_tablet_mode_notify_change();
3545 *send_acpi_ev = false;
3546 return true;
3548 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3549 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3550 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3551 /* do not propagate these events */
3552 *ignore_acpi_ev = true;
3553 return true;
3555 default:
3556 return false;
3560 static void thermal_dump_all_sensors(void);
3562 static bool hotkey_notify_thermal(const u32 hkey,
3563 bool *send_acpi_ev,
3564 bool *ignore_acpi_ev)
3566 bool known = true;
3568 /* 0x6000-0x6FFF: thermal alarms */
3569 *send_acpi_ev = true;
3570 *ignore_acpi_ev = false;
3572 switch (hkey) {
3573 case TP_HKEY_EV_THM_TABLE_CHANGED:
3574 printk(TPACPI_INFO
3575 "EC reports that Thermal Table has changed\n");
3576 /* recommended action: do nothing, we don't have
3577 * Lenovo ATM information */
3578 return true;
3579 case TP_HKEY_EV_ALARM_BAT_HOT:
3580 printk(TPACPI_CRIT
3581 "THERMAL ALARM: battery is too hot!\n");
3582 /* recommended action: warn user through gui */
3583 break;
3584 case TP_HKEY_EV_ALARM_BAT_XHOT:
3585 printk(TPACPI_ALERT
3586 "THERMAL EMERGENCY: battery is extremely hot!\n");
3587 /* recommended action: immediate sleep/hibernate */
3588 break;
3589 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3590 printk(TPACPI_CRIT
3591 "THERMAL ALARM: "
3592 "a sensor reports something is too hot!\n");
3593 /* recommended action: warn user through gui, that */
3594 /* some internal component is too hot */
3595 break;
3596 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3597 printk(TPACPI_ALERT
3598 "THERMAL EMERGENCY: "
3599 "a sensor reports something is extremely hot!\n");
3600 /* recommended action: immediate sleep/hibernate */
3601 break;
3602 default:
3603 printk(TPACPI_ALERT
3604 "THERMAL ALERT: unknown thermal alarm received\n");
3605 known = false;
3608 thermal_dump_all_sensors();
3610 return known;
3613 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3615 u32 hkey;
3616 bool send_acpi_ev;
3617 bool ignore_acpi_ev;
3618 bool known_ev;
3620 if (event != 0x80) {
3621 printk(TPACPI_ERR
3622 "unknown HKEY notification event %d\n", event);
3623 /* forward it to userspace, maybe it knows how to handle it */
3624 acpi_bus_generate_netlink_event(
3625 ibm->acpi->device->pnp.device_class,
3626 dev_name(&ibm->acpi->device->dev),
3627 event, 0);
3628 return;
3631 while (1) {
3632 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3633 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3634 return;
3637 if (hkey == 0) {
3638 /* queue empty */
3639 return;
3642 send_acpi_ev = true;
3643 ignore_acpi_ev = false;
3645 switch (hkey >> 12) {
3646 case 1:
3647 /* 0x1000-0x1FFF: key presses */
3648 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3649 &ignore_acpi_ev);
3650 break;
3651 case 2:
3652 /* 0x2000-0x2FFF: Wakeup reason */
3653 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3654 &ignore_acpi_ev);
3655 break;
3656 case 3:
3657 /* 0x3000-0x3FFF: bay-related wakeups */
3658 if (hkey == TP_HKEY_EV_BAYEJ_ACK) {
3659 hotkey_autosleep_ack = 1;
3660 printk(TPACPI_INFO
3661 "bay ejected\n");
3662 hotkey_wakeup_hotunplug_complete_notify_change();
3663 known_ev = true;
3664 } else {
3665 known_ev = false;
3667 break;
3668 case 4:
3669 /* 0x4000-0x4FFF: dock-related wakeups */
3670 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3671 hotkey_autosleep_ack = 1;
3672 printk(TPACPI_INFO
3673 "undocked\n");
3674 hotkey_wakeup_hotunplug_complete_notify_change();
3675 known_ev = true;
3676 } else {
3677 known_ev = false;
3679 break;
3680 case 5:
3681 /* 0x5000-0x5FFF: human interface helpers */
3682 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3683 &ignore_acpi_ev);
3684 break;
3685 case 6:
3686 /* 0x6000-0x6FFF: thermal alarms */
3687 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3688 &ignore_acpi_ev);
3689 break;
3690 case 7:
3691 /* 0x7000-0x7FFF: misc */
3692 if (tp_features.hotkey_wlsw &&
3693 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3694 tpacpi_send_radiosw_update();
3695 send_acpi_ev = 0;
3696 known_ev = true;
3697 break;
3699 /* fallthrough to default */
3700 default:
3701 known_ev = false;
3703 if (!known_ev) {
3704 printk(TPACPI_NOTICE
3705 "unhandled HKEY event 0x%04x\n", hkey);
3706 printk(TPACPI_NOTICE
3707 "please report the conditions when this "
3708 "event happened to %s\n", TPACPI_MAIL);
3711 /* Legacy events */
3712 if (!ignore_acpi_ev &&
3713 (send_acpi_ev || hotkey_report_mode < 2)) {
3714 acpi_bus_generate_proc_event(ibm->acpi->device,
3715 event, hkey);
3718 /* netlink events */
3719 if (!ignore_acpi_ev && send_acpi_ev) {
3720 acpi_bus_generate_netlink_event(
3721 ibm->acpi->device->pnp.device_class,
3722 dev_name(&ibm->acpi->device->dev),
3723 event, hkey);
3728 static void hotkey_suspend(pm_message_t state)
3730 /* Do these on suspend, we get the events on early resume! */
3731 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3732 hotkey_autosleep_ack = 0;
3735 static void hotkey_resume(void)
3737 tpacpi_disable_brightness_delay();
3739 if (hotkey_status_set(true) < 0 ||
3740 hotkey_mask_set(hotkey_acpi_mask) < 0)
3741 printk(TPACPI_ERR
3742 "error while attempting to reset the event "
3743 "firmware interface\n");
3745 tpacpi_send_radiosw_update();
3746 hotkey_tablet_mode_notify_change();
3747 hotkey_wakeup_reason_notify_change();
3748 hotkey_wakeup_hotunplug_complete_notify_change();
3749 hotkey_poll_setup_safe(false);
3752 /* procfs -------------------------------------------------------------- */
3753 static int hotkey_read(struct seq_file *m)
3755 int res, status;
3757 if (!tp_features.hotkey) {
3758 seq_printf(m, "status:\t\tnot supported\n");
3759 return 0;
3762 if (mutex_lock_killable(&hotkey_mutex))
3763 return -ERESTARTSYS;
3764 res = hotkey_status_get(&status);
3765 if (!res)
3766 res = hotkey_mask_get();
3767 mutex_unlock(&hotkey_mutex);
3768 if (res)
3769 return res;
3771 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3772 if (hotkey_all_mask) {
3773 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3774 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3775 } else {
3776 seq_printf(m, "mask:\t\tnot supported\n");
3777 seq_printf(m, "commands:\tenable, disable, reset\n");
3780 return 0;
3783 static void hotkey_enabledisable_warn(bool enable)
3785 tpacpi_log_usertask("procfs hotkey enable/disable");
3786 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3787 TPACPI_WARN
3788 "hotkey enable/disable functionality has been "
3789 "removed from the driver. Hotkeys are always "
3790 "enabled\n"))
3791 printk(TPACPI_ERR
3792 "Please remove the hotkey=enable module "
3793 "parameter, it is deprecated. Hotkeys are always "
3794 "enabled\n");
3797 static int hotkey_write(char *buf)
3799 int res;
3800 u32 mask;
3801 char *cmd;
3803 if (!tp_features.hotkey)
3804 return -ENODEV;
3806 if (mutex_lock_killable(&hotkey_mutex))
3807 return -ERESTARTSYS;
3809 mask = hotkey_user_mask;
3811 res = 0;
3812 while ((cmd = next_cmd(&buf))) {
3813 if (strlencmp(cmd, "enable") == 0) {
3814 hotkey_enabledisable_warn(1);
3815 } else if (strlencmp(cmd, "disable") == 0) {
3816 hotkey_enabledisable_warn(0);
3817 res = -EPERM;
3818 } else if (strlencmp(cmd, "reset") == 0) {
3819 mask = (hotkey_all_mask | hotkey_source_mask)
3820 & ~hotkey_reserved_mask;
3821 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3822 /* mask set */
3823 } else if (sscanf(cmd, "%x", &mask) == 1) {
3824 /* mask set */
3825 } else {
3826 res = -EINVAL;
3827 goto errexit;
3831 if (!res) {
3832 tpacpi_disclose_usertask("procfs hotkey",
3833 "set mask to 0x%08x\n", mask);
3834 res = hotkey_user_mask_set(mask);
3837 errexit:
3838 mutex_unlock(&hotkey_mutex);
3839 return res;
3842 static const struct acpi_device_id ibm_htk_device_ids[] = {
3843 {TPACPI_ACPI_HKEY_HID, 0},
3844 {"", 0},
3847 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3848 .hid = ibm_htk_device_ids,
3849 .notify = hotkey_notify,
3850 .handle = &hkey_handle,
3851 .type = ACPI_DEVICE_NOTIFY,
3854 static struct ibm_struct hotkey_driver_data = {
3855 .name = "hotkey",
3856 .read = hotkey_read,
3857 .write = hotkey_write,
3858 .exit = hotkey_exit,
3859 .resume = hotkey_resume,
3860 .suspend = hotkey_suspend,
3861 .acpi = &ibm_hotkey_acpidriver,
3864 /*************************************************************************
3865 * Bluetooth subdriver
3868 enum {
3869 /* ACPI GBDC/SBDC bits */
3870 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3871 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3872 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3873 off / last state */
3876 enum {
3877 /* ACPI \BLTH commands */
3878 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3879 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3880 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3881 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3882 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3885 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3887 static int bluetooth_get_status(void)
3889 int status;
3891 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3892 if (dbg_bluetoothemul)
3893 return (tpacpi_bluetooth_emulstate) ?
3894 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3895 #endif
3897 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3898 return -EIO;
3900 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3901 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3904 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3906 int status;
3908 vdbg_printk(TPACPI_DBG_RFKILL,
3909 "will attempt to %s bluetooth\n",
3910 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3912 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3913 if (dbg_bluetoothemul) {
3914 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3915 return 0;
3917 #endif
3919 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3920 status = TP_ACPI_BLUETOOTH_RESUMECTRL;
3921 if (state == TPACPI_RFK_RADIO_ON)
3922 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
3924 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3925 return -EIO;
3927 return 0;
3930 /* sysfs bluetooth enable ---------------------------------------------- */
3931 static ssize_t bluetooth_enable_show(struct device *dev,
3932 struct device_attribute *attr,
3933 char *buf)
3935 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3936 attr, buf);
3939 static ssize_t bluetooth_enable_store(struct device *dev,
3940 struct device_attribute *attr,
3941 const char *buf, size_t count)
3943 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3944 attr, buf, count);
3947 static struct device_attribute dev_attr_bluetooth_enable =
3948 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3949 bluetooth_enable_show, bluetooth_enable_store);
3951 /* --------------------------------------------------------------------- */
3953 static struct attribute *bluetooth_attributes[] = {
3954 &dev_attr_bluetooth_enable.attr,
3955 NULL
3958 static const struct attribute_group bluetooth_attr_group = {
3959 .attrs = bluetooth_attributes,
3962 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3963 .get_status = bluetooth_get_status,
3964 .set_status = bluetooth_set_status,
3967 static void bluetooth_shutdown(void)
3969 /* Order firmware to save current state to NVRAM */
3970 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3971 TP_ACPI_BLTH_SAVE_STATE))
3972 printk(TPACPI_NOTICE
3973 "failed to save bluetooth state to NVRAM\n");
3974 else
3975 vdbg_printk(TPACPI_DBG_RFKILL,
3976 "bluestooth state saved to NVRAM\n");
3979 static void bluetooth_exit(void)
3981 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3982 &bluetooth_attr_group);
3984 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3986 bluetooth_shutdown();
3989 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3991 int res;
3992 int status = 0;
3994 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3995 "initializing bluetooth subdriver\n");
3997 TPACPI_ACPIHANDLE_INIT(hkey);
3999 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4000 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4001 tp_features.bluetooth = hkey_handle &&
4002 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4004 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4005 "bluetooth is %s, status 0x%02x\n",
4006 str_supported(tp_features.bluetooth),
4007 status);
4009 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4010 if (dbg_bluetoothemul) {
4011 tp_features.bluetooth = 1;
4012 printk(TPACPI_INFO
4013 "bluetooth switch emulation enabled\n");
4014 } else
4015 #endif
4016 if (tp_features.bluetooth &&
4017 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4018 /* no bluetooth hardware present in system */
4019 tp_features.bluetooth = 0;
4020 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4021 "bluetooth hardware not installed\n");
4024 if (!tp_features.bluetooth)
4025 return 1;
4027 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4028 &bluetooth_tprfk_ops,
4029 RFKILL_TYPE_BLUETOOTH,
4030 TPACPI_RFK_BLUETOOTH_SW_NAME,
4031 true);
4032 if (res)
4033 return res;
4035 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4036 &bluetooth_attr_group);
4037 if (res) {
4038 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4039 return res;
4042 return 0;
4045 /* procfs -------------------------------------------------------------- */
4046 static int bluetooth_read(struct seq_file *m)
4048 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4051 static int bluetooth_write(char *buf)
4053 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4056 static struct ibm_struct bluetooth_driver_data = {
4057 .name = "bluetooth",
4058 .read = bluetooth_read,
4059 .write = bluetooth_write,
4060 .exit = bluetooth_exit,
4061 .shutdown = bluetooth_shutdown,
4064 /*************************************************************************
4065 * Wan subdriver
4068 enum {
4069 /* ACPI GWAN/SWAN bits */
4070 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4071 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4072 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4073 off / last state */
4076 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4078 static int wan_get_status(void)
4080 int status;
4082 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4083 if (dbg_wwanemul)
4084 return (tpacpi_wwan_emulstate) ?
4085 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4086 #endif
4088 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4089 return -EIO;
4091 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4092 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4095 static int wan_set_status(enum tpacpi_rfkill_state state)
4097 int status;
4099 vdbg_printk(TPACPI_DBG_RFKILL,
4100 "will attempt to %s wwan\n",
4101 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4103 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4104 if (dbg_wwanemul) {
4105 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4106 return 0;
4108 #endif
4110 /* We make sure to set TP_ACPI_WANCARD_RESUMECTRL */
4111 status = TP_ACPI_WANCARD_RESUMECTRL;
4112 if (state == TPACPI_RFK_RADIO_ON)
4113 status |= TP_ACPI_WANCARD_RADIOSSW;
4115 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4116 return -EIO;
4118 return 0;
4121 /* sysfs wan enable ---------------------------------------------------- */
4122 static ssize_t wan_enable_show(struct device *dev,
4123 struct device_attribute *attr,
4124 char *buf)
4126 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4127 attr, buf);
4130 static ssize_t wan_enable_store(struct device *dev,
4131 struct device_attribute *attr,
4132 const char *buf, size_t count)
4134 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4135 attr, buf, count);
4138 static struct device_attribute dev_attr_wan_enable =
4139 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4140 wan_enable_show, wan_enable_store);
4142 /* --------------------------------------------------------------------- */
4144 static struct attribute *wan_attributes[] = {
4145 &dev_attr_wan_enable.attr,
4146 NULL
4149 static const struct attribute_group wan_attr_group = {
4150 .attrs = wan_attributes,
4153 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4154 .get_status = wan_get_status,
4155 .set_status = wan_set_status,
4158 static void wan_shutdown(void)
4160 /* Order firmware to save current state to NVRAM */
4161 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4162 TP_ACPI_WGSV_SAVE_STATE))
4163 printk(TPACPI_NOTICE
4164 "failed to save WWAN state to NVRAM\n");
4165 else
4166 vdbg_printk(TPACPI_DBG_RFKILL,
4167 "WWAN state saved to NVRAM\n");
4170 static void wan_exit(void)
4172 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4173 &wan_attr_group);
4175 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4177 wan_shutdown();
4180 static int __init wan_init(struct ibm_init_struct *iibm)
4182 int res;
4183 int status = 0;
4185 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4186 "initializing wan subdriver\n");
4188 TPACPI_ACPIHANDLE_INIT(hkey);
4190 tp_features.wan = hkey_handle &&
4191 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4193 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4194 "wan is %s, status 0x%02x\n",
4195 str_supported(tp_features.wan),
4196 status);
4198 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4199 if (dbg_wwanemul) {
4200 tp_features.wan = 1;
4201 printk(TPACPI_INFO
4202 "wwan switch emulation enabled\n");
4203 } else
4204 #endif
4205 if (tp_features.wan &&
4206 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4207 /* no wan hardware present in system */
4208 tp_features.wan = 0;
4209 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4210 "wan hardware not installed\n");
4213 if (!tp_features.wan)
4214 return 1;
4216 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4217 &wan_tprfk_ops,
4218 RFKILL_TYPE_WWAN,
4219 TPACPI_RFK_WWAN_SW_NAME,
4220 true);
4221 if (res)
4222 return res;
4224 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4225 &wan_attr_group);
4227 if (res) {
4228 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4229 return res;
4232 return 0;
4235 /* procfs -------------------------------------------------------------- */
4236 static int wan_read(struct seq_file *m)
4238 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4241 static int wan_write(char *buf)
4243 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4246 static struct ibm_struct wan_driver_data = {
4247 .name = "wan",
4248 .read = wan_read,
4249 .write = wan_write,
4250 .exit = wan_exit,
4251 .shutdown = wan_shutdown,
4254 /*************************************************************************
4255 * UWB subdriver
4258 enum {
4259 /* ACPI GUWB/SUWB bits */
4260 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4261 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4264 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4266 static int uwb_get_status(void)
4268 int status;
4270 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4271 if (dbg_uwbemul)
4272 return (tpacpi_uwb_emulstate) ?
4273 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4274 #endif
4276 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4277 return -EIO;
4279 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4280 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4283 static int uwb_set_status(enum tpacpi_rfkill_state state)
4285 int status;
4287 vdbg_printk(TPACPI_DBG_RFKILL,
4288 "will attempt to %s UWB\n",
4289 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4291 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4292 if (dbg_uwbemul) {
4293 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4294 return 0;
4296 #endif
4298 if (state == TPACPI_RFK_RADIO_ON)
4299 status = TP_ACPI_UWB_RADIOSSW;
4300 else
4301 status = 0;
4303 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4304 return -EIO;
4306 return 0;
4309 /* --------------------------------------------------------------------- */
4311 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4312 .get_status = uwb_get_status,
4313 .set_status = uwb_set_status,
4316 static void uwb_exit(void)
4318 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4321 static int __init uwb_init(struct ibm_init_struct *iibm)
4323 int res;
4324 int status = 0;
4326 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4327 "initializing uwb subdriver\n");
4329 TPACPI_ACPIHANDLE_INIT(hkey);
4331 tp_features.uwb = hkey_handle &&
4332 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4334 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4335 "uwb is %s, status 0x%02x\n",
4336 str_supported(tp_features.uwb),
4337 status);
4339 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4340 if (dbg_uwbemul) {
4341 tp_features.uwb = 1;
4342 printk(TPACPI_INFO
4343 "uwb switch emulation enabled\n");
4344 } else
4345 #endif
4346 if (tp_features.uwb &&
4347 !(status & TP_ACPI_UWB_HWPRESENT)) {
4348 /* no uwb hardware present in system */
4349 tp_features.uwb = 0;
4350 dbg_printk(TPACPI_DBG_INIT,
4351 "uwb hardware not installed\n");
4354 if (!tp_features.uwb)
4355 return 1;
4357 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4358 &uwb_tprfk_ops,
4359 RFKILL_TYPE_UWB,
4360 TPACPI_RFK_UWB_SW_NAME,
4361 false);
4362 return res;
4365 static struct ibm_struct uwb_driver_data = {
4366 .name = "uwb",
4367 .exit = uwb_exit,
4368 .flags.experimental = 1,
4371 /*************************************************************************
4372 * Video subdriver
4375 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4377 enum video_access_mode {
4378 TPACPI_VIDEO_NONE = 0,
4379 TPACPI_VIDEO_570, /* 570 */
4380 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4381 TPACPI_VIDEO_NEW, /* all others */
4384 enum { /* video status flags, based on VIDEO_570 */
4385 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4386 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4387 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4390 enum { /* TPACPI_VIDEO_570 constants */
4391 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4392 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4393 * video_status_flags */
4394 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4395 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4398 static enum video_access_mode video_supported;
4399 static int video_orig_autosw;
4401 static int video_autosw_get(void);
4402 static int video_autosw_set(int enable);
4404 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4406 static int __init video_init(struct ibm_init_struct *iibm)
4408 int ivga;
4410 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4412 TPACPI_ACPIHANDLE_INIT(vid);
4413 TPACPI_ACPIHANDLE_INIT(vid2);
4415 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4416 /* G41, assume IVGA doesn't change */
4417 vid_handle = vid2_handle;
4419 if (!vid_handle)
4420 /* video switching not supported on R30, R31 */
4421 video_supported = TPACPI_VIDEO_NONE;
4422 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4423 /* 570 */
4424 video_supported = TPACPI_VIDEO_570;
4425 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4426 /* 600e/x, 770e, 770x */
4427 video_supported = TPACPI_VIDEO_770;
4428 else
4429 /* all others */
4430 video_supported = TPACPI_VIDEO_NEW;
4432 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4433 str_supported(video_supported != TPACPI_VIDEO_NONE),
4434 video_supported);
4436 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4439 static void video_exit(void)
4441 dbg_printk(TPACPI_DBG_EXIT,
4442 "restoring original video autoswitch mode\n");
4443 if (video_autosw_set(video_orig_autosw))
4444 printk(TPACPI_ERR "error while trying to restore original "
4445 "video autoswitch mode\n");
4448 static int video_outputsw_get(void)
4450 int status = 0;
4451 int i;
4453 switch (video_supported) {
4454 case TPACPI_VIDEO_570:
4455 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4456 TP_ACPI_VIDEO_570_PHSCMD))
4457 return -EIO;
4458 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4459 break;
4460 case TPACPI_VIDEO_770:
4461 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4462 return -EIO;
4463 if (i)
4464 status |= TP_ACPI_VIDEO_S_LCD;
4465 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4466 return -EIO;
4467 if (i)
4468 status |= TP_ACPI_VIDEO_S_CRT;
4469 break;
4470 case TPACPI_VIDEO_NEW:
4471 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4472 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4473 return -EIO;
4474 if (i)
4475 status |= TP_ACPI_VIDEO_S_CRT;
4477 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4478 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4479 return -EIO;
4480 if (i)
4481 status |= TP_ACPI_VIDEO_S_LCD;
4482 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4483 return -EIO;
4484 if (i)
4485 status |= TP_ACPI_VIDEO_S_DVI;
4486 break;
4487 default:
4488 return -ENOSYS;
4491 return status;
4494 static int video_outputsw_set(int status)
4496 int autosw;
4497 int res = 0;
4499 switch (video_supported) {
4500 case TPACPI_VIDEO_570:
4501 res = acpi_evalf(NULL, NULL,
4502 "\\_SB.PHS2", "vdd",
4503 TP_ACPI_VIDEO_570_PHS2CMD,
4504 status | TP_ACPI_VIDEO_570_PHS2SET);
4505 break;
4506 case TPACPI_VIDEO_770:
4507 autosw = video_autosw_get();
4508 if (autosw < 0)
4509 return autosw;
4511 res = video_autosw_set(1);
4512 if (res)
4513 return res;
4514 res = acpi_evalf(vid_handle, NULL,
4515 "ASWT", "vdd", status * 0x100, 0);
4516 if (!autosw && video_autosw_set(autosw)) {
4517 printk(TPACPI_ERR
4518 "video auto-switch left enabled due to error\n");
4519 return -EIO;
4521 break;
4522 case TPACPI_VIDEO_NEW:
4523 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4524 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4525 break;
4526 default:
4527 return -ENOSYS;
4530 return (res)? 0 : -EIO;
4533 static int video_autosw_get(void)
4535 int autosw = 0;
4537 switch (video_supported) {
4538 case TPACPI_VIDEO_570:
4539 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4540 return -EIO;
4541 break;
4542 case TPACPI_VIDEO_770:
4543 case TPACPI_VIDEO_NEW:
4544 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4545 return -EIO;
4546 break;
4547 default:
4548 return -ENOSYS;
4551 return autosw & 1;
4554 static int video_autosw_set(int enable)
4556 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4557 return -EIO;
4558 return 0;
4561 static int video_outputsw_cycle(void)
4563 int autosw = video_autosw_get();
4564 int res;
4566 if (autosw < 0)
4567 return autosw;
4569 switch (video_supported) {
4570 case TPACPI_VIDEO_570:
4571 res = video_autosw_set(1);
4572 if (res)
4573 return res;
4574 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4575 break;
4576 case TPACPI_VIDEO_770:
4577 case TPACPI_VIDEO_NEW:
4578 res = video_autosw_set(1);
4579 if (res)
4580 return res;
4581 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4582 break;
4583 default:
4584 return -ENOSYS;
4586 if (!autosw && video_autosw_set(autosw)) {
4587 printk(TPACPI_ERR
4588 "video auto-switch left enabled due to error\n");
4589 return -EIO;
4592 return (res)? 0 : -EIO;
4595 static int video_expand_toggle(void)
4597 switch (video_supported) {
4598 case TPACPI_VIDEO_570:
4599 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4600 0 : -EIO;
4601 case TPACPI_VIDEO_770:
4602 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4603 0 : -EIO;
4604 case TPACPI_VIDEO_NEW:
4605 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4606 0 : -EIO;
4607 default:
4608 return -ENOSYS;
4610 /* not reached */
4613 static int video_read(struct seq_file *m)
4615 int status, autosw;
4617 if (video_supported == TPACPI_VIDEO_NONE) {
4618 seq_printf(m, "status:\t\tnot supported\n");
4619 return 0;
4622 status = video_outputsw_get();
4623 if (status < 0)
4624 return status;
4626 autosw = video_autosw_get();
4627 if (autosw < 0)
4628 return autosw;
4630 seq_printf(m, "status:\t\tsupported\n");
4631 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4632 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4633 if (video_supported == TPACPI_VIDEO_NEW)
4634 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4635 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4636 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4637 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4638 if (video_supported == TPACPI_VIDEO_NEW)
4639 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4640 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4641 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4643 return 0;
4646 static int video_write(char *buf)
4648 char *cmd;
4649 int enable, disable, status;
4650 int res;
4652 if (video_supported == TPACPI_VIDEO_NONE)
4653 return -ENODEV;
4655 enable = 0;
4656 disable = 0;
4658 while ((cmd = next_cmd(&buf))) {
4659 if (strlencmp(cmd, "lcd_enable") == 0) {
4660 enable |= TP_ACPI_VIDEO_S_LCD;
4661 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4662 disable |= TP_ACPI_VIDEO_S_LCD;
4663 } else if (strlencmp(cmd, "crt_enable") == 0) {
4664 enable |= TP_ACPI_VIDEO_S_CRT;
4665 } else if (strlencmp(cmd, "crt_disable") == 0) {
4666 disable |= TP_ACPI_VIDEO_S_CRT;
4667 } else if (video_supported == TPACPI_VIDEO_NEW &&
4668 strlencmp(cmd, "dvi_enable") == 0) {
4669 enable |= TP_ACPI_VIDEO_S_DVI;
4670 } else if (video_supported == TPACPI_VIDEO_NEW &&
4671 strlencmp(cmd, "dvi_disable") == 0) {
4672 disable |= TP_ACPI_VIDEO_S_DVI;
4673 } else if (strlencmp(cmd, "auto_enable") == 0) {
4674 res = video_autosw_set(1);
4675 if (res)
4676 return res;
4677 } else if (strlencmp(cmd, "auto_disable") == 0) {
4678 res = video_autosw_set(0);
4679 if (res)
4680 return res;
4681 } else if (strlencmp(cmd, "video_switch") == 0) {
4682 res = video_outputsw_cycle();
4683 if (res)
4684 return res;
4685 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4686 res = video_expand_toggle();
4687 if (res)
4688 return res;
4689 } else
4690 return -EINVAL;
4693 if (enable || disable) {
4694 status = video_outputsw_get();
4695 if (status < 0)
4696 return status;
4697 res = video_outputsw_set((status & ~disable) | enable);
4698 if (res)
4699 return res;
4702 return 0;
4705 static struct ibm_struct video_driver_data = {
4706 .name = "video",
4707 .read = video_read,
4708 .write = video_write,
4709 .exit = video_exit,
4712 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4714 /*************************************************************************
4715 * Light (thinklight) subdriver
4718 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4719 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4721 static int light_get_status(void)
4723 int status = 0;
4725 if (tp_features.light_status) {
4726 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4727 return -EIO;
4728 return (!!status);
4731 return -ENXIO;
4734 static int light_set_status(int status)
4736 int rc;
4738 if (tp_features.light) {
4739 if (cmos_handle) {
4740 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4741 (status)?
4742 TP_CMOS_THINKLIGHT_ON :
4743 TP_CMOS_THINKLIGHT_OFF);
4744 } else {
4745 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4746 (status)? 1 : 0);
4748 return (rc)? 0 : -EIO;
4751 return -ENXIO;
4754 static void light_set_status_worker(struct work_struct *work)
4756 struct tpacpi_led_classdev *data =
4757 container_of(work, struct tpacpi_led_classdev, work);
4759 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4760 light_set_status((data->new_state != TPACPI_LED_OFF));
4763 static void light_sysfs_set(struct led_classdev *led_cdev,
4764 enum led_brightness brightness)
4766 struct tpacpi_led_classdev *data =
4767 container_of(led_cdev,
4768 struct tpacpi_led_classdev,
4769 led_classdev);
4770 data->new_state = (brightness != LED_OFF) ?
4771 TPACPI_LED_ON : TPACPI_LED_OFF;
4772 queue_work(tpacpi_wq, &data->work);
4775 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4777 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4780 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4781 .led_classdev = {
4782 .name = "tpacpi::thinklight",
4783 .brightness_set = &light_sysfs_set,
4784 .brightness_get = &light_sysfs_get,
4788 static int __init light_init(struct ibm_init_struct *iibm)
4790 int rc;
4792 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4794 TPACPI_ACPIHANDLE_INIT(ledb);
4795 TPACPI_ACPIHANDLE_INIT(lght);
4796 TPACPI_ACPIHANDLE_INIT(cmos);
4797 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4799 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4800 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4802 if (tp_features.light)
4803 /* light status not supported on
4804 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4805 tp_features.light_status =
4806 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4808 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4809 str_supported(tp_features.light),
4810 str_supported(tp_features.light_status));
4812 if (!tp_features.light)
4813 return 1;
4815 rc = led_classdev_register(&tpacpi_pdev->dev,
4816 &tpacpi_led_thinklight.led_classdev);
4818 if (rc < 0) {
4819 tp_features.light = 0;
4820 tp_features.light_status = 0;
4821 } else {
4822 rc = 0;
4825 return rc;
4828 static void light_exit(void)
4830 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4831 if (work_pending(&tpacpi_led_thinklight.work))
4832 flush_workqueue(tpacpi_wq);
4835 static int light_read(struct seq_file *m)
4837 int status;
4839 if (!tp_features.light) {
4840 seq_printf(m, "status:\t\tnot supported\n");
4841 } else if (!tp_features.light_status) {
4842 seq_printf(m, "status:\t\tunknown\n");
4843 seq_printf(m, "commands:\ton, off\n");
4844 } else {
4845 status = light_get_status();
4846 if (status < 0)
4847 return status;
4848 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4849 seq_printf(m, "commands:\ton, off\n");
4852 return 0;
4855 static int light_write(char *buf)
4857 char *cmd;
4858 int newstatus = 0;
4860 if (!tp_features.light)
4861 return -ENODEV;
4863 while ((cmd = next_cmd(&buf))) {
4864 if (strlencmp(cmd, "on") == 0) {
4865 newstatus = 1;
4866 } else if (strlencmp(cmd, "off") == 0) {
4867 newstatus = 0;
4868 } else
4869 return -EINVAL;
4872 return light_set_status(newstatus);
4875 static struct ibm_struct light_driver_data = {
4876 .name = "light",
4877 .read = light_read,
4878 .write = light_write,
4879 .exit = light_exit,
4882 /*************************************************************************
4883 * CMOS subdriver
4886 /* sysfs cmos_command -------------------------------------------------- */
4887 static ssize_t cmos_command_store(struct device *dev,
4888 struct device_attribute *attr,
4889 const char *buf, size_t count)
4891 unsigned long cmos_cmd;
4892 int res;
4894 if (parse_strtoul(buf, 21, &cmos_cmd))
4895 return -EINVAL;
4897 res = issue_thinkpad_cmos_command(cmos_cmd);
4898 return (res)? res : count;
4901 static struct device_attribute dev_attr_cmos_command =
4902 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4904 /* --------------------------------------------------------------------- */
4906 static int __init cmos_init(struct ibm_init_struct *iibm)
4908 int res;
4910 vdbg_printk(TPACPI_DBG_INIT,
4911 "initializing cmos commands subdriver\n");
4913 TPACPI_ACPIHANDLE_INIT(cmos);
4915 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4916 str_supported(cmos_handle != NULL));
4918 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4919 if (res)
4920 return res;
4922 return (cmos_handle)? 0 : 1;
4925 static void cmos_exit(void)
4927 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4930 static int cmos_read(struct seq_file *m)
4932 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4933 R30, R31, T20-22, X20-21 */
4934 if (!cmos_handle)
4935 seq_printf(m, "status:\t\tnot supported\n");
4936 else {
4937 seq_printf(m, "status:\t\tsupported\n");
4938 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4941 return 0;
4944 static int cmos_write(char *buf)
4946 char *cmd;
4947 int cmos_cmd, res;
4949 while ((cmd = next_cmd(&buf))) {
4950 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4951 cmos_cmd >= 0 && cmos_cmd <= 21) {
4952 /* cmos_cmd set */
4953 } else
4954 return -EINVAL;
4956 res = issue_thinkpad_cmos_command(cmos_cmd);
4957 if (res)
4958 return res;
4961 return 0;
4964 static struct ibm_struct cmos_driver_data = {
4965 .name = "cmos",
4966 .read = cmos_read,
4967 .write = cmos_write,
4968 .exit = cmos_exit,
4971 /*************************************************************************
4972 * LED subdriver
4975 enum led_access_mode {
4976 TPACPI_LED_NONE = 0,
4977 TPACPI_LED_570, /* 570 */
4978 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4979 TPACPI_LED_NEW, /* all others */
4982 enum { /* For TPACPI_LED_OLD */
4983 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4984 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4985 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4988 static enum led_access_mode led_supported;
4990 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4991 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4992 /* T20-22, X20-21 */
4993 "LED", /* all others */
4994 ); /* R30, R31 */
4996 #define TPACPI_LED_NUMLEDS 16
4997 static struct tpacpi_led_classdev *tpacpi_leds;
4998 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4999 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5000 /* there's a limit of 19 chars + NULL before 2.6.26 */
5001 "tpacpi::power",
5002 "tpacpi:orange:batt",
5003 "tpacpi:green:batt",
5004 "tpacpi::dock_active",
5005 "tpacpi::bay_active",
5006 "tpacpi::dock_batt",
5007 "tpacpi::unknown_led",
5008 "tpacpi::standby",
5009 "tpacpi::dock_status1",
5010 "tpacpi::dock_status2",
5011 "tpacpi::unknown_led2",
5012 "tpacpi::unknown_led3",
5013 "tpacpi::thinkvantage",
5015 #define TPACPI_SAFE_LEDS 0x1081U
5017 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5019 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5020 return false;
5021 #else
5022 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5023 #endif
5026 static int led_get_status(const unsigned int led)
5028 int status;
5029 enum led_status_t led_s;
5031 switch (led_supported) {
5032 case TPACPI_LED_570:
5033 if (!acpi_evalf(ec_handle,
5034 &status, "GLED", "dd", 1 << led))
5035 return -EIO;
5036 led_s = (status == 0)?
5037 TPACPI_LED_OFF :
5038 ((status == 1)?
5039 TPACPI_LED_ON :
5040 TPACPI_LED_BLINK);
5041 tpacpi_led_state_cache[led] = led_s;
5042 return led_s;
5043 default:
5044 return -ENXIO;
5047 /* not reached */
5050 static int led_set_status(const unsigned int led,
5051 const enum led_status_t ledstatus)
5053 /* off, on, blink. Index is led_status_t */
5054 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5055 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5057 int rc = 0;
5059 switch (led_supported) {
5060 case TPACPI_LED_570:
5061 /* 570 */
5062 if (unlikely(led > 7))
5063 return -EINVAL;
5064 if (unlikely(tpacpi_is_led_restricted(led)))
5065 return -EPERM;
5066 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5067 (1 << led), led_sled_arg1[ledstatus]))
5068 rc = -EIO;
5069 break;
5070 case TPACPI_LED_OLD:
5071 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5072 if (unlikely(led > 7))
5073 return -EINVAL;
5074 if (unlikely(tpacpi_is_led_restricted(led)))
5075 return -EPERM;
5076 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5077 if (rc >= 0)
5078 rc = ec_write(TPACPI_LED_EC_HLBL,
5079 (ledstatus == TPACPI_LED_BLINK) << led);
5080 if (rc >= 0)
5081 rc = ec_write(TPACPI_LED_EC_HLCL,
5082 (ledstatus != TPACPI_LED_OFF) << led);
5083 break;
5084 case TPACPI_LED_NEW:
5085 /* all others */
5086 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5087 return -EINVAL;
5088 if (unlikely(tpacpi_is_led_restricted(led)))
5089 return -EPERM;
5090 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5091 led, led_led_arg1[ledstatus]))
5092 rc = -EIO;
5093 break;
5094 default:
5095 rc = -ENXIO;
5098 if (!rc)
5099 tpacpi_led_state_cache[led] = ledstatus;
5101 return rc;
5104 static void led_set_status_worker(struct work_struct *work)
5106 struct tpacpi_led_classdev *data =
5107 container_of(work, struct tpacpi_led_classdev, work);
5109 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5110 led_set_status(data->led, data->new_state);
5113 static void led_sysfs_set(struct led_classdev *led_cdev,
5114 enum led_brightness brightness)
5116 struct tpacpi_led_classdev *data = container_of(led_cdev,
5117 struct tpacpi_led_classdev, led_classdev);
5119 if (brightness == LED_OFF)
5120 data->new_state = TPACPI_LED_OFF;
5121 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5122 data->new_state = TPACPI_LED_ON;
5123 else
5124 data->new_state = TPACPI_LED_BLINK;
5126 queue_work(tpacpi_wq, &data->work);
5129 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5130 unsigned long *delay_on, unsigned long *delay_off)
5132 struct tpacpi_led_classdev *data = container_of(led_cdev,
5133 struct tpacpi_led_classdev, led_classdev);
5135 /* Can we choose the flash rate? */
5136 if (*delay_on == 0 && *delay_off == 0) {
5137 /* yes. set them to the hardware blink rate (1 Hz) */
5138 *delay_on = 500; /* ms */
5139 *delay_off = 500; /* ms */
5140 } else if ((*delay_on != 500) || (*delay_off != 500))
5141 return -EINVAL;
5143 data->new_state = TPACPI_LED_BLINK;
5144 queue_work(tpacpi_wq, &data->work);
5146 return 0;
5149 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5151 int rc;
5153 struct tpacpi_led_classdev *data = container_of(led_cdev,
5154 struct tpacpi_led_classdev, led_classdev);
5156 rc = led_get_status(data->led);
5158 if (rc == TPACPI_LED_OFF || rc < 0)
5159 rc = LED_OFF; /* no error handling in led class :( */
5160 else
5161 rc = LED_FULL;
5163 return rc;
5166 static void led_exit(void)
5168 unsigned int i;
5170 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5171 if (tpacpi_leds[i].led_classdev.name)
5172 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5175 kfree(tpacpi_leds);
5178 static int __init tpacpi_init_led(unsigned int led)
5180 int rc;
5182 tpacpi_leds[led].led = led;
5184 /* LEDs with no name don't get registered */
5185 if (!tpacpi_led_names[led])
5186 return 0;
5188 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5189 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5190 if (led_supported == TPACPI_LED_570)
5191 tpacpi_leds[led].led_classdev.brightness_get =
5192 &led_sysfs_get;
5194 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5196 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5198 rc = led_classdev_register(&tpacpi_pdev->dev,
5199 &tpacpi_leds[led].led_classdev);
5200 if (rc < 0)
5201 tpacpi_leds[led].led_classdev.name = NULL;
5203 return rc;
5206 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5207 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5208 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5209 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5211 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5212 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5213 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5214 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5215 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5216 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5217 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5218 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5220 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5221 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5222 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5223 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5224 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5226 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5227 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5228 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5229 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5231 /* (1) - may have excess leds enabled on MSB */
5233 /* Defaults (order matters, keep last, don't reorder!) */
5234 { /* Lenovo */
5235 .vendor = PCI_VENDOR_ID_LENOVO,
5236 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5237 .quirks = 0x1fffU,
5239 { /* IBM ThinkPads with no EC version string */
5240 .vendor = PCI_VENDOR_ID_IBM,
5241 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5242 .quirks = 0x00ffU,
5244 { /* IBM ThinkPads with EC version string */
5245 .vendor = PCI_VENDOR_ID_IBM,
5246 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5247 .quirks = 0x00bfU,
5251 #undef TPACPI_LEDQ_IBM
5252 #undef TPACPI_LEDQ_LNV
5254 static int __init led_init(struct ibm_init_struct *iibm)
5256 unsigned int i;
5257 int rc;
5258 unsigned long useful_leds;
5260 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5262 TPACPI_ACPIHANDLE_INIT(led);
5264 if (!led_handle)
5265 /* led not supported on R30, R31 */
5266 led_supported = TPACPI_LED_NONE;
5267 else if (strlencmp(led_path, "SLED") == 0)
5268 /* 570 */
5269 led_supported = TPACPI_LED_570;
5270 else if (strlencmp(led_path, "SYSL") == 0)
5271 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5272 led_supported = TPACPI_LED_OLD;
5273 else
5274 /* all others */
5275 led_supported = TPACPI_LED_NEW;
5277 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5278 str_supported(led_supported), led_supported);
5280 if (led_supported == TPACPI_LED_NONE)
5281 return 1;
5283 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5284 GFP_KERNEL);
5285 if (!tpacpi_leds) {
5286 printk(TPACPI_ERR "Out of memory for LED data\n");
5287 return -ENOMEM;
5290 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5291 ARRAY_SIZE(led_useful_qtable));
5293 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5294 if (!tpacpi_is_led_restricted(i) &&
5295 test_bit(i, &useful_leds)) {
5296 rc = tpacpi_init_led(i);
5297 if (rc < 0) {
5298 led_exit();
5299 return rc;
5304 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5305 printk(TPACPI_NOTICE
5306 "warning: userspace override of important "
5307 "firmware LEDs is enabled\n");
5308 #endif
5309 return 0;
5312 #define str_led_status(s) \
5313 ((s) == TPACPI_LED_OFF ? "off" : \
5314 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5316 static int led_read(struct seq_file *m)
5318 if (!led_supported) {
5319 seq_printf(m, "status:\t\tnot supported\n");
5320 return 0;
5322 seq_printf(m, "status:\t\tsupported\n");
5324 if (led_supported == TPACPI_LED_570) {
5325 /* 570 */
5326 int i, status;
5327 for (i = 0; i < 8; i++) {
5328 status = led_get_status(i);
5329 if (status < 0)
5330 return -EIO;
5331 seq_printf(m, "%d:\t\t%s\n",
5332 i, str_led_status(status));
5336 seq_printf(m, "commands:\t"
5337 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5339 return 0;
5342 static int led_write(char *buf)
5344 char *cmd;
5345 int led, rc;
5346 enum led_status_t s;
5348 if (!led_supported)
5349 return -ENODEV;
5351 while ((cmd = next_cmd(&buf))) {
5352 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5353 return -EINVAL;
5355 if (strstr(cmd, "off")) {
5356 s = TPACPI_LED_OFF;
5357 } else if (strstr(cmd, "on")) {
5358 s = TPACPI_LED_ON;
5359 } else if (strstr(cmd, "blink")) {
5360 s = TPACPI_LED_BLINK;
5361 } else {
5362 return -EINVAL;
5365 rc = led_set_status(led, s);
5366 if (rc < 0)
5367 return rc;
5370 return 0;
5373 static struct ibm_struct led_driver_data = {
5374 .name = "led",
5375 .read = led_read,
5376 .write = led_write,
5377 .exit = led_exit,
5380 /*************************************************************************
5381 * Beep subdriver
5384 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5386 #define TPACPI_BEEP_Q1 0x0001
5388 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5389 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5390 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5393 static int __init beep_init(struct ibm_init_struct *iibm)
5395 unsigned long quirks;
5397 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5399 TPACPI_ACPIHANDLE_INIT(beep);
5401 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5402 str_supported(beep_handle != NULL));
5404 quirks = tpacpi_check_quirks(beep_quirk_table,
5405 ARRAY_SIZE(beep_quirk_table));
5407 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5409 return (beep_handle)? 0 : 1;
5412 static int beep_read(struct seq_file *m)
5414 if (!beep_handle)
5415 seq_printf(m, "status:\t\tnot supported\n");
5416 else {
5417 seq_printf(m, "status:\t\tsupported\n");
5418 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5421 return 0;
5424 static int beep_write(char *buf)
5426 char *cmd;
5427 int beep_cmd;
5429 if (!beep_handle)
5430 return -ENODEV;
5432 while ((cmd = next_cmd(&buf))) {
5433 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5434 beep_cmd >= 0 && beep_cmd <= 17) {
5435 /* beep_cmd set */
5436 } else
5437 return -EINVAL;
5438 if (tp_features.beep_needs_two_args) {
5439 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5440 beep_cmd, 0))
5441 return -EIO;
5442 } else {
5443 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5444 beep_cmd))
5445 return -EIO;
5449 return 0;
5452 static struct ibm_struct beep_driver_data = {
5453 .name = "beep",
5454 .read = beep_read,
5455 .write = beep_write,
5458 /*************************************************************************
5459 * Thermal subdriver
5462 enum thermal_access_mode {
5463 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5464 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5465 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5466 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5467 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5470 enum { /* TPACPI_THERMAL_TPEC_* */
5471 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5472 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5473 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5475 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5479 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5480 struct ibm_thermal_sensors_struct {
5481 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5484 static enum thermal_access_mode thermal_read_mode;
5486 /* idx is zero-based */
5487 static int thermal_get_sensor(int idx, s32 *value)
5489 int t;
5490 s8 tmp;
5491 char tmpi[5];
5493 t = TP_EC_THERMAL_TMP0;
5495 switch (thermal_read_mode) {
5496 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5497 case TPACPI_THERMAL_TPEC_16:
5498 if (idx >= 8 && idx <= 15) {
5499 t = TP_EC_THERMAL_TMP8;
5500 idx -= 8;
5502 /* fallthrough */
5503 #endif
5504 case TPACPI_THERMAL_TPEC_8:
5505 if (idx <= 7) {
5506 if (!acpi_ec_read(t + idx, &tmp))
5507 return -EIO;
5508 *value = tmp * 1000;
5509 return 0;
5511 break;
5513 case TPACPI_THERMAL_ACPI_UPDT:
5514 if (idx <= 7) {
5515 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5516 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5517 return -EIO;
5518 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5519 return -EIO;
5520 *value = (t - 2732) * 100;
5521 return 0;
5523 break;
5525 case TPACPI_THERMAL_ACPI_TMP07:
5526 if (idx <= 7) {
5527 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5528 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5529 return -EIO;
5530 if (t > 127 || t < -127)
5531 t = TP_EC_THERMAL_TMP_NA;
5532 *value = t * 1000;
5533 return 0;
5535 break;
5537 case TPACPI_THERMAL_NONE:
5538 default:
5539 return -ENOSYS;
5542 return -EINVAL;
5545 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5547 int res, i;
5548 int n;
5550 n = 8;
5551 i = 0;
5553 if (!s)
5554 return -EINVAL;
5556 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5557 n = 16;
5559 for (i = 0 ; i < n; i++) {
5560 res = thermal_get_sensor(i, &s->temp[i]);
5561 if (res)
5562 return res;
5565 return n;
5568 static void thermal_dump_all_sensors(void)
5570 int n, i;
5571 struct ibm_thermal_sensors_struct t;
5573 n = thermal_get_sensors(&t);
5574 if (n <= 0)
5575 return;
5577 printk(TPACPI_NOTICE
5578 "temperatures (Celsius):");
5580 for (i = 0; i < n; i++) {
5581 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5582 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5583 else
5584 printk(KERN_CONT " N/A");
5587 printk(KERN_CONT "\n");
5590 /* sysfs temp##_input -------------------------------------------------- */
5592 static ssize_t thermal_temp_input_show(struct device *dev,
5593 struct device_attribute *attr,
5594 char *buf)
5596 struct sensor_device_attribute *sensor_attr =
5597 to_sensor_dev_attr(attr);
5598 int idx = sensor_attr->index;
5599 s32 value;
5600 int res;
5602 res = thermal_get_sensor(idx, &value);
5603 if (res)
5604 return res;
5605 if (value == TPACPI_THERMAL_SENSOR_NA)
5606 return -ENXIO;
5608 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5611 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5612 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5613 thermal_temp_input_show, NULL, _idxB)
5615 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5616 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5617 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5618 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5619 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5620 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5621 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5622 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5623 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5624 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5625 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5626 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5627 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5628 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5629 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5630 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5631 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5634 #define THERMAL_ATTRS(X) \
5635 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5637 static struct attribute *thermal_temp_input_attr[] = {
5638 THERMAL_ATTRS(8),
5639 THERMAL_ATTRS(9),
5640 THERMAL_ATTRS(10),
5641 THERMAL_ATTRS(11),
5642 THERMAL_ATTRS(12),
5643 THERMAL_ATTRS(13),
5644 THERMAL_ATTRS(14),
5645 THERMAL_ATTRS(15),
5646 THERMAL_ATTRS(0),
5647 THERMAL_ATTRS(1),
5648 THERMAL_ATTRS(2),
5649 THERMAL_ATTRS(3),
5650 THERMAL_ATTRS(4),
5651 THERMAL_ATTRS(5),
5652 THERMAL_ATTRS(6),
5653 THERMAL_ATTRS(7),
5654 NULL
5657 static const struct attribute_group thermal_temp_input16_group = {
5658 .attrs = thermal_temp_input_attr
5661 static const struct attribute_group thermal_temp_input8_group = {
5662 .attrs = &thermal_temp_input_attr[8]
5665 #undef THERMAL_SENSOR_ATTR_TEMP
5666 #undef THERMAL_ATTRS
5668 /* --------------------------------------------------------------------- */
5670 static int __init thermal_init(struct ibm_init_struct *iibm)
5672 u8 t, ta1, ta2;
5673 int i;
5674 int acpi_tmp7;
5675 int res;
5677 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5679 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5681 if (thinkpad_id.ec_model) {
5683 * Direct EC access mode: sensors at registers
5684 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5685 * non-implemented, thermal sensors return 0x80 when
5686 * not available
5689 ta1 = ta2 = 0;
5690 for (i = 0; i < 8; i++) {
5691 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5692 ta1 |= t;
5693 } else {
5694 ta1 = 0;
5695 break;
5697 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5698 ta2 |= t;
5699 } else {
5700 ta1 = 0;
5701 break;
5704 if (ta1 == 0) {
5705 /* This is sheer paranoia, but we handle it anyway */
5706 if (acpi_tmp7) {
5707 printk(TPACPI_ERR
5708 "ThinkPad ACPI EC access misbehaving, "
5709 "falling back to ACPI TMPx access "
5710 "mode\n");
5711 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5712 } else {
5713 printk(TPACPI_ERR
5714 "ThinkPad ACPI EC access misbehaving, "
5715 "disabling thermal sensors access\n");
5716 thermal_read_mode = TPACPI_THERMAL_NONE;
5718 } else {
5719 thermal_read_mode =
5720 (ta2 != 0) ?
5721 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5723 } else if (acpi_tmp7) {
5724 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5725 /* 600e/x, 770e, 770x */
5726 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5727 } else {
5728 /* Standard ACPI TMPx access, max 8 sensors */
5729 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5731 } else {
5732 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5733 thermal_read_mode = TPACPI_THERMAL_NONE;
5736 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5737 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5738 thermal_read_mode);
5740 switch (thermal_read_mode) {
5741 case TPACPI_THERMAL_TPEC_16:
5742 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5743 &thermal_temp_input16_group);
5744 if (res)
5745 return res;
5746 break;
5747 case TPACPI_THERMAL_TPEC_8:
5748 case TPACPI_THERMAL_ACPI_TMP07:
5749 case TPACPI_THERMAL_ACPI_UPDT:
5750 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5751 &thermal_temp_input8_group);
5752 if (res)
5753 return res;
5754 break;
5755 case TPACPI_THERMAL_NONE:
5756 default:
5757 return 1;
5760 return 0;
5763 static void thermal_exit(void)
5765 switch (thermal_read_mode) {
5766 case TPACPI_THERMAL_TPEC_16:
5767 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5768 &thermal_temp_input16_group);
5769 break;
5770 case TPACPI_THERMAL_TPEC_8:
5771 case TPACPI_THERMAL_ACPI_TMP07:
5772 case TPACPI_THERMAL_ACPI_UPDT:
5773 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5774 &thermal_temp_input16_group);
5775 break;
5776 case TPACPI_THERMAL_NONE:
5777 default:
5778 break;
5782 static int thermal_read(struct seq_file *m)
5784 int n, i;
5785 struct ibm_thermal_sensors_struct t;
5787 n = thermal_get_sensors(&t);
5788 if (unlikely(n < 0))
5789 return n;
5791 seq_printf(m, "temperatures:\t");
5793 if (n > 0) {
5794 for (i = 0; i < (n - 1); i++)
5795 seq_printf(m, "%d ", t.temp[i] / 1000);
5796 seq_printf(m, "%d\n", t.temp[i] / 1000);
5797 } else
5798 seq_printf(m, "not supported\n");
5800 return 0;
5803 static struct ibm_struct thermal_driver_data = {
5804 .name = "thermal",
5805 .read = thermal_read,
5806 .exit = thermal_exit,
5809 /*************************************************************************
5810 * EC Dump subdriver
5813 static u8 ecdump_regs[256];
5815 static int ecdump_read(struct seq_file *m)
5817 int i, j;
5818 u8 v;
5820 seq_printf(m, "EC "
5821 " +00 +01 +02 +03 +04 +05 +06 +07"
5822 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5823 for (i = 0; i < 256; i += 16) {
5824 seq_printf(m, "EC 0x%02x:", i);
5825 for (j = 0; j < 16; j++) {
5826 if (!acpi_ec_read(i + j, &v))
5827 break;
5828 if (v != ecdump_regs[i + j])
5829 seq_printf(m, " *%02x", v);
5830 else
5831 seq_printf(m, " %02x", v);
5832 ecdump_regs[i + j] = v;
5834 seq_putc(m, '\n');
5835 if (j != 16)
5836 break;
5839 /* These are way too dangerous to advertise openly... */
5840 #if 0
5841 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5842 " (<offset> is 00-ff, <value> is 00-ff)\n");
5843 seq_printf(m, "commands:\t0x<offset> <value> "
5844 " (<offset> is 00-ff, <value> is 0-255)\n");
5845 #endif
5846 return 0;
5849 static int ecdump_write(char *buf)
5851 char *cmd;
5852 int i, v;
5854 while ((cmd = next_cmd(&buf))) {
5855 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5856 /* i and v set */
5857 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5858 /* i and v set */
5859 } else
5860 return -EINVAL;
5861 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5862 if (!acpi_ec_write(i, v))
5863 return -EIO;
5864 } else
5865 return -EINVAL;
5868 return 0;
5871 static struct ibm_struct ecdump_driver_data = {
5872 .name = "ecdump",
5873 .read = ecdump_read,
5874 .write = ecdump_write,
5875 .flags.experimental = 1,
5878 /*************************************************************************
5879 * Backlight/brightness subdriver
5882 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5885 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5886 * CMOS NVRAM byte 0x5E, bits 0-3.
5888 * EC HBRV (0x31) has the following layout
5889 * Bit 7: unknown function
5890 * Bit 6: unknown function
5891 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5892 * Bit 4: must be set to zero to avoid problems
5893 * Bit 3-0: backlight brightness level
5895 * brightness_get_raw returns status data in the HBRV layout
5897 * WARNING: The X61 has been verified to use HBRV for something else, so
5898 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5899 * testing on the very early *60 Lenovo models...
5902 enum {
5903 TP_EC_BACKLIGHT = 0x31,
5905 /* TP_EC_BACKLIGHT bitmasks */
5906 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5907 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5908 TP_EC_BACKLIGHT_MAPSW = 0x20,
5911 enum tpacpi_brightness_access_mode {
5912 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5913 TPACPI_BRGHT_MODE_EC, /* EC control */
5914 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5915 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5916 TPACPI_BRGHT_MODE_MAX
5919 static struct backlight_device *ibm_backlight_device;
5921 static enum tpacpi_brightness_access_mode brightness_mode =
5922 TPACPI_BRGHT_MODE_MAX;
5924 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5926 static struct mutex brightness_mutex;
5928 /* NVRAM brightness access,
5929 * call with brightness_mutex held! */
5930 static unsigned int tpacpi_brightness_nvram_get(void)
5932 u8 lnvram;
5934 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5935 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5936 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5937 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5939 return lnvram;
5942 static void tpacpi_brightness_checkpoint_nvram(void)
5944 u8 lec = 0;
5945 u8 b_nvram;
5947 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5948 return;
5950 vdbg_printk(TPACPI_DBG_BRGHT,
5951 "trying to checkpoint backlight level to NVRAM...\n");
5953 if (mutex_lock_killable(&brightness_mutex) < 0)
5954 return;
5956 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5957 goto unlock;
5958 lec &= TP_EC_BACKLIGHT_LVLMSK;
5959 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5961 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5962 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5963 /* NVRAM needs update */
5964 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5965 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5966 b_nvram |= lec;
5967 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5968 dbg_printk(TPACPI_DBG_BRGHT,
5969 "updated NVRAM backlight level to %u (0x%02x)\n",
5970 (unsigned int) lec, (unsigned int) b_nvram);
5971 } else
5972 vdbg_printk(TPACPI_DBG_BRGHT,
5973 "NVRAM backlight level already is %u (0x%02x)\n",
5974 (unsigned int) lec, (unsigned int) b_nvram);
5976 unlock:
5977 mutex_unlock(&brightness_mutex);
5981 /* call with brightness_mutex held! */
5982 static int tpacpi_brightness_get_raw(int *status)
5984 u8 lec = 0;
5986 switch (brightness_mode) {
5987 case TPACPI_BRGHT_MODE_UCMS_STEP:
5988 *status = tpacpi_brightness_nvram_get();
5989 return 0;
5990 case TPACPI_BRGHT_MODE_EC:
5991 case TPACPI_BRGHT_MODE_ECNVRAM:
5992 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5993 return -EIO;
5994 *status = lec;
5995 return 0;
5996 default:
5997 return -ENXIO;
6001 /* call with brightness_mutex held! */
6002 /* do NOT call with illegal backlight level value */
6003 static int tpacpi_brightness_set_ec(unsigned int value)
6005 u8 lec = 0;
6007 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6008 return -EIO;
6010 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6011 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6012 (value & TP_EC_BACKLIGHT_LVLMSK))))
6013 return -EIO;
6015 return 0;
6018 /* call with brightness_mutex held! */
6019 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6021 int cmos_cmd, inc;
6022 unsigned int current_value, i;
6024 current_value = tpacpi_brightness_nvram_get();
6026 if (value == current_value)
6027 return 0;
6029 cmos_cmd = (value > current_value) ?
6030 TP_CMOS_BRIGHTNESS_UP :
6031 TP_CMOS_BRIGHTNESS_DOWN;
6032 inc = (value > current_value) ? 1 : -1;
6034 for (i = current_value; i != value; i += inc)
6035 if (issue_thinkpad_cmos_command(cmos_cmd))
6036 return -EIO;
6038 return 0;
6041 /* May return EINTR which can always be mapped to ERESTARTSYS */
6042 static int brightness_set(unsigned int value)
6044 int res;
6046 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6047 value < 0)
6048 return -EINVAL;
6050 vdbg_printk(TPACPI_DBG_BRGHT,
6051 "set backlight level to %d\n", value);
6053 res = mutex_lock_killable(&brightness_mutex);
6054 if (res < 0)
6055 return res;
6057 switch (brightness_mode) {
6058 case TPACPI_BRGHT_MODE_EC:
6059 case TPACPI_BRGHT_MODE_ECNVRAM:
6060 res = tpacpi_brightness_set_ec(value);
6061 break;
6062 case TPACPI_BRGHT_MODE_UCMS_STEP:
6063 res = tpacpi_brightness_set_ucmsstep(value);
6064 break;
6065 default:
6066 res = -ENXIO;
6069 mutex_unlock(&brightness_mutex);
6070 return res;
6073 /* sysfs backlight class ----------------------------------------------- */
6075 static int brightness_update_status(struct backlight_device *bd)
6077 unsigned int level =
6078 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6079 bd->props.power == FB_BLANK_UNBLANK) ?
6080 bd->props.brightness : 0;
6082 dbg_printk(TPACPI_DBG_BRGHT,
6083 "backlight: attempt to set level to %d\n",
6084 level);
6086 /* it is the backlight class's job (caller) to handle
6087 * EINTR and other errors properly */
6088 return brightness_set(level);
6091 static int brightness_get(struct backlight_device *bd)
6093 int status, res;
6095 res = mutex_lock_killable(&brightness_mutex);
6096 if (res < 0)
6097 return 0;
6099 res = tpacpi_brightness_get_raw(&status);
6101 mutex_unlock(&brightness_mutex);
6103 if (res < 0)
6104 return 0;
6106 return status & TP_EC_BACKLIGHT_LVLMSK;
6109 static void tpacpi_brightness_notify_change(void)
6111 backlight_force_update(ibm_backlight_device,
6112 BACKLIGHT_UPDATE_HOTKEY);
6115 static struct backlight_ops ibm_backlight_data = {
6116 .get_brightness = brightness_get,
6117 .update_status = brightness_update_status,
6120 /* --------------------------------------------------------------------- */
6123 * These are only useful for models that have only one possibility
6124 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6125 * these quirks.
6127 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6128 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6129 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6131 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6132 /* Models with ATI GPUs known to require ECNVRAM mode */
6133 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6135 /* Models with ATI GPUs that can use ECNVRAM */
6136 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC),
6137 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6138 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6139 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6141 /* Models with Intel Extreme Graphics 2 */
6142 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC),
6143 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6144 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6146 /* Models with Intel GMA900 */
6147 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6148 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6149 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6152 static int __init brightness_init(struct ibm_init_struct *iibm)
6154 int b;
6155 unsigned long quirks;
6157 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6159 mutex_init(&brightness_mutex);
6161 quirks = tpacpi_check_quirks(brightness_quirk_table,
6162 ARRAY_SIZE(brightness_quirk_table));
6165 * We always attempt to detect acpi support, so as to switch
6166 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6167 * going to publish a backlight interface
6169 b = tpacpi_check_std_acpi_brightness_support();
6170 if (b > 0) {
6172 if (acpi_video_backlight_support()) {
6173 if (brightness_enable > 1) {
6174 printk(TPACPI_NOTICE
6175 "Standard ACPI backlight interface "
6176 "available, not loading native one.\n");
6177 return 1;
6178 } else if (brightness_enable == 1) {
6179 printk(TPACPI_NOTICE
6180 "Backlight control force enabled, even if standard "
6181 "ACPI backlight interface is available\n");
6183 } else {
6184 if (brightness_enable > 1) {
6185 printk(TPACPI_NOTICE
6186 "Standard ACPI backlight interface not "
6187 "available, thinkpad_acpi native "
6188 "brightness control enabled\n");
6193 if (!brightness_enable) {
6194 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6195 "brightness support disabled by "
6196 "module parameter\n");
6197 return 1;
6200 if (b > 16) {
6201 printk(TPACPI_ERR
6202 "Unsupported brightness interface, "
6203 "please contact %s\n", TPACPI_MAIL);
6204 return 1;
6206 if (b == 16)
6207 tp_features.bright_16levels = 1;
6210 * Check for module parameter bogosity, note that we
6211 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6212 * able to detect "unspecified"
6214 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6215 return -EINVAL;
6217 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6218 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6219 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6220 if (quirks & TPACPI_BRGHT_Q_EC)
6221 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6222 else
6223 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6225 dbg_printk(TPACPI_DBG_BRGHT,
6226 "driver auto-selected brightness_mode=%d\n",
6227 brightness_mode);
6230 /* Safety */
6231 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6232 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6233 brightness_mode == TPACPI_BRGHT_MODE_EC))
6234 return -EINVAL;
6236 if (tpacpi_brightness_get_raw(&b) < 0)
6237 return 1;
6239 if (tp_features.bright_16levels)
6240 printk(TPACPI_INFO
6241 "detected a 16-level brightness capable ThinkPad\n");
6243 ibm_backlight_device = backlight_device_register(
6244 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6245 &ibm_backlight_data);
6246 if (IS_ERR(ibm_backlight_device)) {
6247 int rc = PTR_ERR(ibm_backlight_device);
6248 ibm_backlight_device = NULL;
6249 printk(TPACPI_ERR "Could not register backlight device\n");
6250 return rc;
6252 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6253 "brightness is supported\n");
6255 if (quirks & TPACPI_BRGHT_Q_ASK) {
6256 printk(TPACPI_NOTICE
6257 "brightness: will use unverified default: "
6258 "brightness_mode=%d\n", brightness_mode);
6259 printk(TPACPI_NOTICE
6260 "brightness: please report to %s whether it works well "
6261 "or not on your ThinkPad\n", TPACPI_MAIL);
6264 ibm_backlight_device->props.max_brightness =
6265 (tp_features.bright_16levels)? 15 : 7;
6266 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6267 backlight_update_status(ibm_backlight_device);
6269 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6270 "brightness: registering brightness hotkeys "
6271 "as change notification\n");
6272 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6273 | TP_ACPI_HKEY_BRGHTUP_MASK
6274 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6275 return 0;
6278 static void brightness_suspend(pm_message_t state)
6280 tpacpi_brightness_checkpoint_nvram();
6283 static void brightness_shutdown(void)
6285 tpacpi_brightness_checkpoint_nvram();
6288 static void brightness_exit(void)
6290 if (ibm_backlight_device) {
6291 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6292 "calling backlight_device_unregister()\n");
6293 backlight_device_unregister(ibm_backlight_device);
6296 tpacpi_brightness_checkpoint_nvram();
6299 static int brightness_read(struct seq_file *m)
6301 int level;
6303 level = brightness_get(NULL);
6304 if (level < 0) {
6305 seq_printf(m, "level:\t\tunreadable\n");
6306 } else {
6307 seq_printf(m, "level:\t\t%d\n", level);
6308 seq_printf(m, "commands:\tup, down\n");
6309 seq_printf(m, "commands:\tlevel <level>"
6310 " (<level> is 0-%d)\n",
6311 (tp_features.bright_16levels) ? 15 : 7);
6314 return 0;
6317 static int brightness_write(char *buf)
6319 int level;
6320 int rc;
6321 char *cmd;
6322 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6324 level = brightness_get(NULL);
6325 if (level < 0)
6326 return level;
6328 while ((cmd = next_cmd(&buf))) {
6329 if (strlencmp(cmd, "up") == 0) {
6330 if (level < max_level)
6331 level++;
6332 } else if (strlencmp(cmd, "down") == 0) {
6333 if (level > 0)
6334 level--;
6335 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6336 level >= 0 && level <= max_level) {
6337 /* new level set */
6338 } else
6339 return -EINVAL;
6342 tpacpi_disclose_usertask("procfs brightness",
6343 "set level to %d\n", level);
6346 * Now we know what the final level should be, so we try to set it.
6347 * Doing it this way makes the syscall restartable in case of EINTR
6349 rc = brightness_set(level);
6350 if (!rc && ibm_backlight_device)
6351 backlight_force_update(ibm_backlight_device,
6352 BACKLIGHT_UPDATE_SYSFS);
6353 return (rc == -EINTR)? -ERESTARTSYS : rc;
6356 static struct ibm_struct brightness_driver_data = {
6357 .name = "brightness",
6358 .read = brightness_read,
6359 .write = brightness_write,
6360 .exit = brightness_exit,
6361 .suspend = brightness_suspend,
6362 .shutdown = brightness_shutdown,
6365 /*************************************************************************
6366 * Volume subdriver
6370 * IBM ThinkPads have a simple volume controller with MUTE gating.
6371 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6373 * Since the *61 series (and probably also the later *60 series), Lenovo
6374 * ThinkPads only implement the MUTE gate.
6376 * EC register 0x30
6377 * Bit 6: MUTE (1 mutes sound)
6378 * Bit 3-0: Volume
6379 * Other bits should be zero as far as we know.
6381 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6382 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6383 * such as bit 7 which is used to detect repeated presses of MUTE,
6384 * and we leave them unchanged.
6387 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6388 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6389 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6391 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6392 static char *alsa_id = "ThinkPadEC";
6393 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6395 struct tpacpi_alsa_data {
6396 struct snd_card *card;
6397 struct snd_ctl_elem_id *ctl_mute_id;
6398 struct snd_ctl_elem_id *ctl_vol_id;
6401 static struct snd_card *alsa_card;
6403 enum {
6404 TP_EC_AUDIO = 0x30,
6406 /* TP_EC_AUDIO bits */
6407 TP_EC_AUDIO_MUTESW = 6,
6409 /* TP_EC_AUDIO bitmasks */
6410 TP_EC_AUDIO_LVL_MSK = 0x0F,
6411 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6413 /* Maximum volume */
6414 TP_EC_VOLUME_MAX = 14,
6417 enum tpacpi_volume_access_mode {
6418 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6419 TPACPI_VOL_MODE_EC, /* Pure EC control */
6420 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6421 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6422 TPACPI_VOL_MODE_MAX
6425 enum tpacpi_volume_capabilities {
6426 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6427 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6428 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6429 TPACPI_VOL_CAP_MAX
6432 static enum tpacpi_volume_access_mode volume_mode =
6433 TPACPI_VOL_MODE_MAX;
6435 static enum tpacpi_volume_capabilities volume_capabilities;
6436 static int volume_control_allowed;
6439 * Used to syncronize writers to TP_EC_AUDIO and
6440 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6442 static struct mutex volume_mutex;
6444 static void tpacpi_volume_checkpoint_nvram(void)
6446 u8 lec = 0;
6447 u8 b_nvram;
6448 u8 ec_mask;
6450 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6451 return;
6452 if (!volume_control_allowed)
6453 return;
6455 vdbg_printk(TPACPI_DBG_MIXER,
6456 "trying to checkpoint mixer state to NVRAM...\n");
6458 if (tp_features.mixer_no_level_control)
6459 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6460 else
6461 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6463 if (mutex_lock_killable(&volume_mutex) < 0)
6464 return;
6466 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6467 goto unlock;
6468 lec &= ec_mask;
6469 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6471 if (lec != (b_nvram & ec_mask)) {
6472 /* NVRAM needs update */
6473 b_nvram &= ~ec_mask;
6474 b_nvram |= lec;
6475 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6476 dbg_printk(TPACPI_DBG_MIXER,
6477 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6478 (unsigned int) lec, (unsigned int) b_nvram);
6479 } else {
6480 vdbg_printk(TPACPI_DBG_MIXER,
6481 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6482 (unsigned int) lec, (unsigned int) b_nvram);
6485 unlock:
6486 mutex_unlock(&volume_mutex);
6489 static int volume_get_status_ec(u8 *status)
6491 u8 s;
6493 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6494 return -EIO;
6496 *status = s;
6498 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6500 return 0;
6503 static int volume_get_status(u8 *status)
6505 return volume_get_status_ec(status);
6508 static int volume_set_status_ec(const u8 status)
6510 if (!acpi_ec_write(TP_EC_AUDIO, status))
6511 return -EIO;
6513 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6515 return 0;
6518 static int volume_set_status(const u8 status)
6520 return volume_set_status_ec(status);
6523 static int volume_set_mute_ec(const bool mute)
6525 int rc;
6526 u8 s, n;
6528 if (mutex_lock_killable(&volume_mutex) < 0)
6529 return -EINTR;
6531 rc = volume_get_status_ec(&s);
6532 if (rc)
6533 goto unlock;
6535 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6536 s & ~TP_EC_AUDIO_MUTESW_MSK;
6538 if (n != s)
6539 rc = volume_set_status_ec(n);
6541 unlock:
6542 mutex_unlock(&volume_mutex);
6543 return rc;
6546 static int volume_set_mute(const bool mute)
6548 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6549 (mute) ? "" : "un");
6550 return volume_set_mute_ec(mute);
6553 static int volume_set_volume_ec(const u8 vol)
6555 int rc;
6556 u8 s, n;
6558 if (vol > TP_EC_VOLUME_MAX)
6559 return -EINVAL;
6561 if (mutex_lock_killable(&volume_mutex) < 0)
6562 return -EINTR;
6564 rc = volume_get_status_ec(&s);
6565 if (rc)
6566 goto unlock;
6568 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6570 if (n != s)
6571 rc = volume_set_status_ec(n);
6573 unlock:
6574 mutex_unlock(&volume_mutex);
6575 return rc;
6578 static int volume_set_volume(const u8 vol)
6580 dbg_printk(TPACPI_DBG_MIXER,
6581 "trying to set volume level to %hu\n", vol);
6582 return volume_set_volume_ec(vol);
6585 static void volume_alsa_notify_change(void)
6587 struct tpacpi_alsa_data *d;
6589 if (alsa_card && alsa_card->private_data) {
6590 d = alsa_card->private_data;
6591 if (d->ctl_mute_id)
6592 snd_ctl_notify(alsa_card,
6593 SNDRV_CTL_EVENT_MASK_VALUE,
6594 d->ctl_mute_id);
6595 if (d->ctl_vol_id)
6596 snd_ctl_notify(alsa_card,
6597 SNDRV_CTL_EVENT_MASK_VALUE,
6598 d->ctl_vol_id);
6602 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6603 struct snd_ctl_elem_info *uinfo)
6605 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6606 uinfo->count = 1;
6607 uinfo->value.integer.min = 0;
6608 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6609 return 0;
6612 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6613 struct snd_ctl_elem_value *ucontrol)
6615 u8 s;
6616 int rc;
6618 rc = volume_get_status(&s);
6619 if (rc < 0)
6620 return rc;
6622 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6623 return 0;
6626 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6627 struct snd_ctl_elem_value *ucontrol)
6629 return volume_set_volume(ucontrol->value.integer.value[0]);
6632 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6634 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6635 struct snd_ctl_elem_value *ucontrol)
6637 u8 s;
6638 int rc;
6640 rc = volume_get_status(&s);
6641 if (rc < 0)
6642 return rc;
6644 ucontrol->value.integer.value[0] =
6645 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6646 return 0;
6649 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6650 struct snd_ctl_elem_value *ucontrol)
6652 return volume_set_mute(!ucontrol->value.integer.value[0]);
6655 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6656 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6657 .name = "Console Playback Volume",
6658 .index = 0,
6659 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6660 .info = volume_alsa_vol_info,
6661 .get = volume_alsa_vol_get,
6664 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6665 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6666 .name = "Console Playback Switch",
6667 .index = 0,
6668 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6669 .info = volume_alsa_mute_info,
6670 .get = volume_alsa_mute_get,
6673 static void volume_suspend(pm_message_t state)
6675 tpacpi_volume_checkpoint_nvram();
6678 static void volume_resume(void)
6680 volume_alsa_notify_change();
6683 static void volume_shutdown(void)
6685 tpacpi_volume_checkpoint_nvram();
6688 static void volume_exit(void)
6690 if (alsa_card) {
6691 snd_card_free(alsa_card);
6692 alsa_card = NULL;
6695 tpacpi_volume_checkpoint_nvram();
6698 static int __init volume_create_alsa_mixer(void)
6700 struct snd_card *card;
6701 struct tpacpi_alsa_data *data;
6702 struct snd_kcontrol *ctl_vol;
6703 struct snd_kcontrol *ctl_mute;
6704 int rc;
6706 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6707 sizeof(struct tpacpi_alsa_data), &card);
6708 if (rc < 0 || !card) {
6709 printk(TPACPI_ERR
6710 "Failed to create ALSA card structures: %d\n", rc);
6711 return 1;
6714 BUG_ON(!card->private_data);
6715 data = card->private_data;
6716 data->card = card;
6718 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6719 sizeof(card->driver));
6720 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6721 sizeof(card->shortname));
6722 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6723 (thinkpad_id.ec_version_str) ?
6724 thinkpad_id.ec_version_str : "(unknown)");
6725 snprintf(card->longname, sizeof(card->longname),
6726 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6727 (thinkpad_id.ec_version_str) ?
6728 thinkpad_id.ec_version_str : "unknown");
6730 if (volume_control_allowed) {
6731 volume_alsa_control_vol.put = volume_alsa_vol_put;
6732 volume_alsa_control_vol.access =
6733 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6735 volume_alsa_control_mute.put = volume_alsa_mute_put;
6736 volume_alsa_control_mute.access =
6737 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6740 if (!tp_features.mixer_no_level_control) {
6741 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6742 rc = snd_ctl_add(card, ctl_vol);
6743 if (rc < 0) {
6744 printk(TPACPI_ERR
6745 "Failed to create ALSA volume control: %d\n",
6746 rc);
6747 goto err_exit;
6749 data->ctl_vol_id = &ctl_vol->id;
6752 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6753 rc = snd_ctl_add(card, ctl_mute);
6754 if (rc < 0) {
6755 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6756 rc);
6757 goto err_exit;
6759 data->ctl_mute_id = &ctl_mute->id;
6761 snd_card_set_dev(card, &tpacpi_pdev->dev);
6762 rc = snd_card_register(card);
6763 if (rc < 0) {
6764 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6765 goto err_exit;
6768 alsa_card = card;
6769 return 0;
6771 err_exit:
6772 snd_card_free(card);
6773 return 1;
6776 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6777 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6779 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6780 /* Whitelist volume level on all IBM by default */
6781 { .vendor = PCI_VENDOR_ID_IBM,
6782 .bios = TPACPI_MATCH_ANY,
6783 .ec = TPACPI_MATCH_ANY,
6784 .quirks = TPACPI_VOL_Q_LEVEL },
6786 /* Lenovo models with volume control (needs confirmation) */
6787 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6788 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6789 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6790 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6791 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6792 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6793 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6795 /* Whitelist mute-only on all Lenovo by default */
6796 { .vendor = PCI_VENDOR_ID_LENOVO,
6797 .bios = TPACPI_MATCH_ANY,
6798 .ec = TPACPI_MATCH_ANY,
6799 .quirks = TPACPI_VOL_Q_MUTEONLY }
6802 static int __init volume_init(struct ibm_init_struct *iibm)
6804 unsigned long quirks;
6805 int rc;
6807 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6809 mutex_init(&volume_mutex);
6812 * Check for module parameter bogosity, note that we
6813 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6814 * able to detect "unspecified"
6816 if (volume_mode > TPACPI_VOL_MODE_MAX)
6817 return -EINVAL;
6819 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6820 printk(TPACPI_ERR
6821 "UCMS step volume mode not implemented, "
6822 "please contact %s\n", TPACPI_MAIL);
6823 return 1;
6826 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6827 return -EINVAL;
6830 * The ALSA mixer is our primary interface.
6831 * When disabled, don't install the subdriver at all
6833 if (!alsa_enable) {
6834 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6835 "ALSA mixer disabled by parameter, "
6836 "not loading volume subdriver...\n");
6837 return 1;
6840 quirks = tpacpi_check_quirks(volume_quirk_table,
6841 ARRAY_SIZE(volume_quirk_table));
6843 switch (volume_capabilities) {
6844 case TPACPI_VOL_CAP_AUTO:
6845 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6846 tp_features.mixer_no_level_control = 1;
6847 else if (quirks & TPACPI_VOL_Q_LEVEL)
6848 tp_features.mixer_no_level_control = 0;
6849 else
6850 return 1; /* no mixer */
6851 break;
6852 case TPACPI_VOL_CAP_VOLMUTE:
6853 tp_features.mixer_no_level_control = 0;
6854 break;
6855 case TPACPI_VOL_CAP_MUTEONLY:
6856 tp_features.mixer_no_level_control = 1;
6857 break;
6858 default:
6859 return 1;
6862 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6863 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6864 "using user-supplied volume_capabilities=%d\n",
6865 volume_capabilities);
6867 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6868 volume_mode == TPACPI_VOL_MODE_MAX) {
6869 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6871 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6872 "driver auto-selected volume_mode=%d\n",
6873 volume_mode);
6874 } else {
6875 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6876 "using user-supplied volume_mode=%d\n",
6877 volume_mode);
6880 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6881 "mute is supported, volume control is %s\n",
6882 str_supported(!tp_features.mixer_no_level_control));
6884 rc = volume_create_alsa_mixer();
6885 if (rc) {
6886 printk(TPACPI_ERR
6887 "Could not create the ALSA mixer interface\n");
6888 return rc;
6891 printk(TPACPI_INFO
6892 "Console audio control enabled, mode: %s\n",
6893 (volume_control_allowed) ?
6894 "override (read/write)" :
6895 "monitor (read only)");
6897 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6898 "registering volume hotkeys as change notification\n");
6899 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6900 | TP_ACPI_HKEY_VOLUP_MASK
6901 | TP_ACPI_HKEY_VOLDWN_MASK
6902 | TP_ACPI_HKEY_MUTE_MASK);
6904 return 0;
6907 static int volume_read(struct seq_file *m)
6909 u8 status;
6911 if (volume_get_status(&status) < 0) {
6912 seq_printf(m, "level:\t\tunreadable\n");
6913 } else {
6914 if (tp_features.mixer_no_level_control)
6915 seq_printf(m, "level:\t\tunsupported\n");
6916 else
6917 seq_printf(m, "level:\t\t%d\n",
6918 status & TP_EC_AUDIO_LVL_MSK);
6920 seq_printf(m, "mute:\t\t%s\n",
6921 onoff(status, TP_EC_AUDIO_MUTESW));
6923 if (volume_control_allowed) {
6924 seq_printf(m, "commands:\tunmute, mute\n");
6925 if (!tp_features.mixer_no_level_control) {
6926 seq_printf(m,
6927 "commands:\tup, down\n");
6928 seq_printf(m,
6929 "commands:\tlevel <level>"
6930 " (<level> is 0-%d)\n",
6931 TP_EC_VOLUME_MAX);
6936 return 0;
6939 static int volume_write(char *buf)
6941 u8 s;
6942 u8 new_level, new_mute;
6943 int l;
6944 char *cmd;
6945 int rc;
6948 * We do allow volume control at driver startup, so that the
6949 * user can set initial state through the volume=... parameter hack.
6951 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
6952 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
6953 tp_warned.volume_ctrl_forbidden = 1;
6954 printk(TPACPI_NOTICE
6955 "Console audio control in monitor mode, "
6956 "changes are not allowed.\n");
6957 printk(TPACPI_NOTICE
6958 "Use the volume_control=1 module parameter "
6959 "to enable volume control\n");
6961 return -EPERM;
6964 rc = volume_get_status(&s);
6965 if (rc < 0)
6966 return rc;
6968 new_level = s & TP_EC_AUDIO_LVL_MSK;
6969 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
6971 while ((cmd = next_cmd(&buf))) {
6972 if (!tp_features.mixer_no_level_control) {
6973 if (strlencmp(cmd, "up") == 0) {
6974 if (new_mute)
6975 new_mute = 0;
6976 else if (new_level < TP_EC_VOLUME_MAX)
6977 new_level++;
6978 continue;
6979 } else if (strlencmp(cmd, "down") == 0) {
6980 if (new_mute)
6981 new_mute = 0;
6982 else if (new_level > 0)
6983 new_level--;
6984 continue;
6985 } else if (sscanf(cmd, "level %u", &l) == 1 &&
6986 l >= 0 && l <= TP_EC_VOLUME_MAX) {
6987 new_level = l;
6988 continue;
6991 if (strlencmp(cmd, "mute") == 0)
6992 new_mute = TP_EC_AUDIO_MUTESW_MSK;
6993 else if (strlencmp(cmd, "unmute") == 0)
6994 new_mute = 0;
6995 else
6996 return -EINVAL;
6999 if (tp_features.mixer_no_level_control) {
7000 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7001 new_mute ? "" : "un");
7002 rc = volume_set_mute(!!new_mute);
7003 } else {
7004 tpacpi_disclose_usertask("procfs volume",
7005 "%smute and set level to %d\n",
7006 new_mute ? "" : "un", new_level);
7007 rc = volume_set_status(new_mute | new_level);
7009 volume_alsa_notify_change();
7011 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7014 static struct ibm_struct volume_driver_data = {
7015 .name = "volume",
7016 .read = volume_read,
7017 .write = volume_write,
7018 .exit = volume_exit,
7019 .suspend = volume_suspend,
7020 .resume = volume_resume,
7021 .shutdown = volume_shutdown,
7024 /*************************************************************************
7025 * Fan subdriver
7029 * FAN ACCESS MODES
7031 * TPACPI_FAN_RD_ACPI_GFAN:
7032 * ACPI GFAN method: returns fan level
7034 * see TPACPI_FAN_WR_ACPI_SFAN
7035 * EC 0x2f (HFSP) not available if GFAN exists
7037 * TPACPI_FAN_WR_ACPI_SFAN:
7038 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7040 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7041 * it for writing.
7043 * TPACPI_FAN_WR_TPEC:
7044 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7045 * Supported on almost all ThinkPads
7047 * Fan speed changes of any sort (including those caused by the
7048 * disengaged mode) are usually done slowly by the firmware as the
7049 * maximum ammount of fan duty cycle change per second seems to be
7050 * limited.
7052 * Reading is not available if GFAN exists.
7053 * Writing is not available if SFAN exists.
7055 * Bits
7056 * 7 automatic mode engaged;
7057 * (default operation mode of the ThinkPad)
7058 * fan level is ignored in this mode.
7059 * 6 full speed mode (takes precedence over bit 7);
7060 * not available on all thinkpads. May disable
7061 * the tachometer while the fan controller ramps up
7062 * the speed (which can take up to a few *minutes*).
7063 * Speeds up fan to 100% duty-cycle, which is far above
7064 * the standard RPM levels. It is not impossible that
7065 * it could cause hardware damage.
7066 * 5-3 unused in some models. Extra bits for fan level
7067 * in others, but still useless as all values above
7068 * 7 map to the same speed as level 7 in these models.
7069 * 2-0 fan level (0..7 usually)
7070 * 0x00 = stop
7071 * 0x07 = max (set when temperatures critical)
7072 * Some ThinkPads may have other levels, see
7073 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7075 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7076 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7077 * does so, its initial value is meaningless (0x07).
7079 * For firmware bugs, refer to:
7080 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7082 * ----
7084 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7085 * Main fan tachometer reading (in RPM)
7087 * This register is present on all ThinkPads with a new-style EC, and
7088 * it is known not to be present on the A21m/e, and T22, as there is
7089 * something else in offset 0x84 according to the ACPI DSDT. Other
7090 * ThinkPads from this same time period (and earlier) probably lack the
7091 * tachometer as well.
7093 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7094 * was never fixed by IBM to report the EC firmware version string
7095 * probably support the tachometer (like the early X models), so
7096 * detecting it is quite hard. We need more data to know for sure.
7098 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7099 * might result.
7101 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7102 * mode.
7104 * For firmware bugs, refer to:
7105 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7107 * ----
7109 * ThinkPad EC register 0x31 bit 0 (only on select models)
7111 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7112 * show the speed of the main fan. When bit 0 of EC register 0x31
7113 * is one, the tachometer registers show the speed of the auxiliary
7114 * fan.
7116 * Fan control seems to affect both fans, regardless of the state
7117 * of this bit.
7119 * So far, only the firmware for the X60/X61 non-tablet versions
7120 * seem to support this (firmware TP-7M).
7122 * TPACPI_FAN_WR_ACPI_FANS:
7123 * ThinkPad X31, X40, X41. Not available in the X60.
7125 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7126 * high speed. ACPI DSDT seems to map these three speeds to levels
7127 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7128 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7130 * The speeds are stored on handles
7131 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7133 * There are three default speed sets, accessible as handles:
7134 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7136 * ACPI DSDT switches which set is in use depending on various
7137 * factors.
7139 * TPACPI_FAN_WR_TPEC is also available and should be used to
7140 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7141 * but the ACPI tables just mention level 7.
7144 enum { /* Fan control constants */
7145 fan_status_offset = 0x2f, /* EC register 0x2f */
7146 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7147 * 0x84 must be read before 0x85 */
7148 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7149 bit 0 selects which fan is active */
7151 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7152 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7154 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7157 enum fan_status_access_mode {
7158 TPACPI_FAN_NONE = 0, /* No fan status or control */
7159 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7160 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7163 enum fan_control_access_mode {
7164 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7165 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7166 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7167 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7170 enum fan_control_commands {
7171 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7172 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7173 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7174 * and also watchdog cmd */
7177 static int fan_control_allowed;
7179 static enum fan_status_access_mode fan_status_access_mode;
7180 static enum fan_control_access_mode fan_control_access_mode;
7181 static enum fan_control_commands fan_control_commands;
7183 static u8 fan_control_initial_status;
7184 static u8 fan_control_desired_level;
7185 static u8 fan_control_resume_level;
7186 static int fan_watchdog_maxinterval;
7188 static struct mutex fan_mutex;
7190 static void fan_watchdog_fire(struct work_struct *ignored);
7191 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7193 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7194 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7195 "\\FSPD", /* 600e/x, 770e, 770x */
7196 ); /* all others */
7197 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7198 "JFNS", /* 770x-JL */
7199 ); /* all others */
7202 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7203 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7204 * be in auto mode (0x80).
7206 * This is corrected by any write to HFSP either by the driver, or
7207 * by the firmware.
7209 * We assume 0x07 really means auto mode while this quirk is active,
7210 * as this is far more likely than the ThinkPad being in level 7,
7211 * which is only used by the firmware during thermal emergencies.
7213 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7214 * TP-70 (T43, R52), which are known to be buggy.
7217 static void fan_quirk1_setup(void)
7219 if (fan_control_initial_status == 0x07) {
7220 printk(TPACPI_NOTICE
7221 "fan_init: initial fan status is unknown, "
7222 "assuming it is in auto mode\n");
7223 tp_features.fan_ctrl_status_undef = 1;
7227 static void fan_quirk1_handle(u8 *fan_status)
7229 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7230 if (*fan_status != fan_control_initial_status) {
7231 /* something changed the HFSP regisnter since
7232 * driver init time, so it is not undefined
7233 * anymore */
7234 tp_features.fan_ctrl_status_undef = 0;
7235 } else {
7236 /* Return most likely status. In fact, it
7237 * might be the only possible status */
7238 *fan_status = TP_EC_FAN_AUTO;
7243 /* Select main fan on X60/X61, NOOP on others */
7244 static bool fan_select_fan1(void)
7246 if (tp_features.second_fan) {
7247 u8 val;
7249 if (ec_read(fan_select_offset, &val) < 0)
7250 return false;
7251 val &= 0xFEU;
7252 if (ec_write(fan_select_offset, val) < 0)
7253 return false;
7255 return true;
7258 /* Select secondary fan on X60/X61 */
7259 static bool fan_select_fan2(void)
7261 u8 val;
7263 if (!tp_features.second_fan)
7264 return false;
7266 if (ec_read(fan_select_offset, &val) < 0)
7267 return false;
7268 val |= 0x01U;
7269 if (ec_write(fan_select_offset, val) < 0)
7270 return false;
7272 return true;
7276 * Call with fan_mutex held
7278 static void fan_update_desired_level(u8 status)
7280 if ((status &
7281 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7282 if (status > 7)
7283 fan_control_desired_level = 7;
7284 else
7285 fan_control_desired_level = status;
7289 static int fan_get_status(u8 *status)
7291 u8 s;
7293 /* TODO:
7294 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7296 switch (fan_status_access_mode) {
7297 case TPACPI_FAN_RD_ACPI_GFAN:
7298 /* 570, 600e/x, 770e, 770x */
7300 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7301 return -EIO;
7303 if (likely(status))
7304 *status = s & 0x07;
7306 break;
7308 case TPACPI_FAN_RD_TPEC:
7309 /* all except 570, 600e/x, 770e, 770x */
7310 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7311 return -EIO;
7313 if (likely(status)) {
7314 *status = s;
7315 fan_quirk1_handle(status);
7318 break;
7320 default:
7321 return -ENXIO;
7324 return 0;
7327 static int fan_get_status_safe(u8 *status)
7329 int rc;
7330 u8 s;
7332 if (mutex_lock_killable(&fan_mutex))
7333 return -ERESTARTSYS;
7334 rc = fan_get_status(&s);
7335 if (!rc)
7336 fan_update_desired_level(s);
7337 mutex_unlock(&fan_mutex);
7339 if (status)
7340 *status = s;
7342 return rc;
7345 static int fan_get_speed(unsigned int *speed)
7347 u8 hi, lo;
7349 switch (fan_status_access_mode) {
7350 case TPACPI_FAN_RD_TPEC:
7351 /* all except 570, 600e/x, 770e, 770x */
7352 if (unlikely(!fan_select_fan1()))
7353 return -EIO;
7354 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7355 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7356 return -EIO;
7358 if (likely(speed))
7359 *speed = (hi << 8) | lo;
7361 break;
7363 default:
7364 return -ENXIO;
7367 return 0;
7370 static int fan2_get_speed(unsigned int *speed)
7372 u8 hi, lo;
7373 bool rc;
7375 switch (fan_status_access_mode) {
7376 case TPACPI_FAN_RD_TPEC:
7377 /* all except 570, 600e/x, 770e, 770x */
7378 if (unlikely(!fan_select_fan2()))
7379 return -EIO;
7380 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7381 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7382 fan_select_fan1(); /* play it safe */
7383 if (rc)
7384 return -EIO;
7386 if (likely(speed))
7387 *speed = (hi << 8) | lo;
7389 break;
7391 default:
7392 return -ENXIO;
7395 return 0;
7398 static int fan_set_level(int level)
7400 if (!fan_control_allowed)
7401 return -EPERM;
7403 switch (fan_control_access_mode) {
7404 case TPACPI_FAN_WR_ACPI_SFAN:
7405 if (level >= 0 && level <= 7) {
7406 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7407 return -EIO;
7408 } else
7409 return -EINVAL;
7410 break;
7412 case TPACPI_FAN_WR_ACPI_FANS:
7413 case TPACPI_FAN_WR_TPEC:
7414 if (!(level & TP_EC_FAN_AUTO) &&
7415 !(level & TP_EC_FAN_FULLSPEED) &&
7416 ((level < 0) || (level > 7)))
7417 return -EINVAL;
7419 /* safety net should the EC not support AUTO
7420 * or FULLSPEED mode bits and just ignore them */
7421 if (level & TP_EC_FAN_FULLSPEED)
7422 level |= 7; /* safety min speed 7 */
7423 else if (level & TP_EC_FAN_AUTO)
7424 level |= 4; /* safety min speed 4 */
7426 if (!acpi_ec_write(fan_status_offset, level))
7427 return -EIO;
7428 else
7429 tp_features.fan_ctrl_status_undef = 0;
7430 break;
7432 default:
7433 return -ENXIO;
7436 vdbg_printk(TPACPI_DBG_FAN,
7437 "fan control: set fan control register to 0x%02x\n", level);
7438 return 0;
7441 static int fan_set_level_safe(int level)
7443 int rc;
7445 if (!fan_control_allowed)
7446 return -EPERM;
7448 if (mutex_lock_killable(&fan_mutex))
7449 return -ERESTARTSYS;
7451 if (level == TPACPI_FAN_LAST_LEVEL)
7452 level = fan_control_desired_level;
7454 rc = fan_set_level(level);
7455 if (!rc)
7456 fan_update_desired_level(level);
7458 mutex_unlock(&fan_mutex);
7459 return rc;
7462 static int fan_set_enable(void)
7464 u8 s;
7465 int rc;
7467 if (!fan_control_allowed)
7468 return -EPERM;
7470 if (mutex_lock_killable(&fan_mutex))
7471 return -ERESTARTSYS;
7473 switch (fan_control_access_mode) {
7474 case TPACPI_FAN_WR_ACPI_FANS:
7475 case TPACPI_FAN_WR_TPEC:
7476 rc = fan_get_status(&s);
7477 if (rc < 0)
7478 break;
7480 /* Don't go out of emergency fan mode */
7481 if (s != 7) {
7482 s &= 0x07;
7483 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7486 if (!acpi_ec_write(fan_status_offset, s))
7487 rc = -EIO;
7488 else {
7489 tp_features.fan_ctrl_status_undef = 0;
7490 rc = 0;
7492 break;
7494 case TPACPI_FAN_WR_ACPI_SFAN:
7495 rc = fan_get_status(&s);
7496 if (rc < 0)
7497 break;
7499 s &= 0x07;
7501 /* Set fan to at least level 4 */
7502 s |= 4;
7504 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7505 rc = -EIO;
7506 else
7507 rc = 0;
7508 break;
7510 default:
7511 rc = -ENXIO;
7514 mutex_unlock(&fan_mutex);
7516 if (!rc)
7517 vdbg_printk(TPACPI_DBG_FAN,
7518 "fan control: set fan control register to 0x%02x\n",
7520 return rc;
7523 static int fan_set_disable(void)
7525 int rc;
7527 if (!fan_control_allowed)
7528 return -EPERM;
7530 if (mutex_lock_killable(&fan_mutex))
7531 return -ERESTARTSYS;
7533 rc = 0;
7534 switch (fan_control_access_mode) {
7535 case TPACPI_FAN_WR_ACPI_FANS:
7536 case TPACPI_FAN_WR_TPEC:
7537 if (!acpi_ec_write(fan_status_offset, 0x00))
7538 rc = -EIO;
7539 else {
7540 fan_control_desired_level = 0;
7541 tp_features.fan_ctrl_status_undef = 0;
7543 break;
7545 case TPACPI_FAN_WR_ACPI_SFAN:
7546 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7547 rc = -EIO;
7548 else
7549 fan_control_desired_level = 0;
7550 break;
7552 default:
7553 rc = -ENXIO;
7556 if (!rc)
7557 vdbg_printk(TPACPI_DBG_FAN,
7558 "fan control: set fan control register to 0\n");
7560 mutex_unlock(&fan_mutex);
7561 return rc;
7564 static int fan_set_speed(int speed)
7566 int rc;
7568 if (!fan_control_allowed)
7569 return -EPERM;
7571 if (mutex_lock_killable(&fan_mutex))
7572 return -ERESTARTSYS;
7574 rc = 0;
7575 switch (fan_control_access_mode) {
7576 case TPACPI_FAN_WR_ACPI_FANS:
7577 if (speed >= 0 && speed <= 65535) {
7578 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7579 speed, speed, speed))
7580 rc = -EIO;
7581 } else
7582 rc = -EINVAL;
7583 break;
7585 default:
7586 rc = -ENXIO;
7589 mutex_unlock(&fan_mutex);
7590 return rc;
7593 static void fan_watchdog_reset(void)
7595 static int fan_watchdog_active;
7597 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7598 return;
7600 if (fan_watchdog_active)
7601 cancel_delayed_work(&fan_watchdog_task);
7603 if (fan_watchdog_maxinterval > 0 &&
7604 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7605 fan_watchdog_active = 1;
7606 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7607 msecs_to_jiffies(fan_watchdog_maxinterval
7608 * 1000))) {
7609 printk(TPACPI_ERR
7610 "failed to queue the fan watchdog, "
7611 "watchdog will not trigger\n");
7613 } else
7614 fan_watchdog_active = 0;
7617 static void fan_watchdog_fire(struct work_struct *ignored)
7619 int rc;
7621 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7622 return;
7624 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7625 rc = fan_set_enable();
7626 if (rc < 0) {
7627 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7628 "will try again later...\n", -rc);
7629 /* reschedule for later */
7630 fan_watchdog_reset();
7635 * SYSFS fan layout: hwmon compatible (device)
7637 * pwm*_enable:
7638 * 0: "disengaged" mode
7639 * 1: manual mode
7640 * 2: native EC "auto" mode (recommended, hardware default)
7642 * pwm*: set speed in manual mode, ignored otherwise.
7643 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7644 * interpolation.
7646 * fan*_input: tachometer reading, RPM
7649 * SYSFS fan layout: extensions
7651 * fan_watchdog (driver):
7652 * fan watchdog interval in seconds, 0 disables (default), max 120
7655 /* sysfs fan pwm1_enable ----------------------------------------------- */
7656 static ssize_t fan_pwm1_enable_show(struct device *dev,
7657 struct device_attribute *attr,
7658 char *buf)
7660 int res, mode;
7661 u8 status;
7663 res = fan_get_status_safe(&status);
7664 if (res)
7665 return res;
7667 if (status & TP_EC_FAN_FULLSPEED) {
7668 mode = 0;
7669 } else if (status & TP_EC_FAN_AUTO) {
7670 mode = 2;
7671 } else
7672 mode = 1;
7674 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7677 static ssize_t fan_pwm1_enable_store(struct device *dev,
7678 struct device_attribute *attr,
7679 const char *buf, size_t count)
7681 unsigned long t;
7682 int res, level;
7684 if (parse_strtoul(buf, 2, &t))
7685 return -EINVAL;
7687 tpacpi_disclose_usertask("hwmon pwm1_enable",
7688 "set fan mode to %lu\n", t);
7690 switch (t) {
7691 case 0:
7692 level = TP_EC_FAN_FULLSPEED;
7693 break;
7694 case 1:
7695 level = TPACPI_FAN_LAST_LEVEL;
7696 break;
7697 case 2:
7698 level = TP_EC_FAN_AUTO;
7699 break;
7700 case 3:
7701 /* reserved for software-controlled auto mode */
7702 return -ENOSYS;
7703 default:
7704 return -EINVAL;
7707 res = fan_set_level_safe(level);
7708 if (res == -ENXIO)
7709 return -EINVAL;
7710 else if (res < 0)
7711 return res;
7713 fan_watchdog_reset();
7715 return count;
7718 static struct device_attribute dev_attr_fan_pwm1_enable =
7719 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7720 fan_pwm1_enable_show, fan_pwm1_enable_store);
7722 /* sysfs fan pwm1 ------------------------------------------------------ */
7723 static ssize_t fan_pwm1_show(struct device *dev,
7724 struct device_attribute *attr,
7725 char *buf)
7727 int res;
7728 u8 status;
7730 res = fan_get_status_safe(&status);
7731 if (res)
7732 return res;
7734 if ((status &
7735 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7736 status = fan_control_desired_level;
7738 if (status > 7)
7739 status = 7;
7741 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7744 static ssize_t fan_pwm1_store(struct device *dev,
7745 struct device_attribute *attr,
7746 const char *buf, size_t count)
7748 unsigned long s;
7749 int rc;
7750 u8 status, newlevel;
7752 if (parse_strtoul(buf, 255, &s))
7753 return -EINVAL;
7755 tpacpi_disclose_usertask("hwmon pwm1",
7756 "set fan speed to %lu\n", s);
7758 /* scale down from 0-255 to 0-7 */
7759 newlevel = (s >> 5) & 0x07;
7761 if (mutex_lock_killable(&fan_mutex))
7762 return -ERESTARTSYS;
7764 rc = fan_get_status(&status);
7765 if (!rc && (status &
7766 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7767 rc = fan_set_level(newlevel);
7768 if (rc == -ENXIO)
7769 rc = -EINVAL;
7770 else if (!rc) {
7771 fan_update_desired_level(newlevel);
7772 fan_watchdog_reset();
7776 mutex_unlock(&fan_mutex);
7777 return (rc)? rc : count;
7780 static struct device_attribute dev_attr_fan_pwm1 =
7781 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7782 fan_pwm1_show, fan_pwm1_store);
7784 /* sysfs fan fan1_input ------------------------------------------------ */
7785 static ssize_t fan_fan1_input_show(struct device *dev,
7786 struct device_attribute *attr,
7787 char *buf)
7789 int res;
7790 unsigned int speed;
7792 res = fan_get_speed(&speed);
7793 if (res < 0)
7794 return res;
7796 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7799 static struct device_attribute dev_attr_fan_fan1_input =
7800 __ATTR(fan1_input, S_IRUGO,
7801 fan_fan1_input_show, NULL);
7803 /* sysfs fan fan2_input ------------------------------------------------ */
7804 static ssize_t fan_fan2_input_show(struct device *dev,
7805 struct device_attribute *attr,
7806 char *buf)
7808 int res;
7809 unsigned int speed;
7811 res = fan2_get_speed(&speed);
7812 if (res < 0)
7813 return res;
7815 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7818 static struct device_attribute dev_attr_fan_fan2_input =
7819 __ATTR(fan2_input, S_IRUGO,
7820 fan_fan2_input_show, NULL);
7822 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7823 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7824 char *buf)
7826 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7829 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7830 const char *buf, size_t count)
7832 unsigned long t;
7834 if (parse_strtoul(buf, 120, &t))
7835 return -EINVAL;
7837 if (!fan_control_allowed)
7838 return -EPERM;
7840 fan_watchdog_maxinterval = t;
7841 fan_watchdog_reset();
7843 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7845 return count;
7848 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7849 fan_fan_watchdog_show, fan_fan_watchdog_store);
7851 /* --------------------------------------------------------------------- */
7852 static struct attribute *fan_attributes[] = {
7853 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7854 &dev_attr_fan_fan1_input.attr,
7855 NULL, /* for fan2_input */
7856 NULL
7859 static const struct attribute_group fan_attr_group = {
7860 .attrs = fan_attributes,
7863 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7864 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7866 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7867 { .vendor = PCI_VENDOR_ID_IBM, \
7868 .bios = TPACPI_MATCH_ANY, \
7869 .ec = TPID(__id1, __id2), \
7870 .quirks = __quirks }
7872 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7873 { .vendor = PCI_VENDOR_ID_LENOVO, \
7874 .bios = TPACPI_MATCH_ANY, \
7875 .ec = TPID(__id1, __id2), \
7876 .quirks = __quirks }
7878 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7879 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7880 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7881 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7882 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7883 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7886 #undef TPACPI_FAN_QL
7887 #undef TPACPI_FAN_QI
7889 static int __init fan_init(struct ibm_init_struct *iibm)
7891 int rc;
7892 unsigned long quirks;
7894 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7895 "initializing fan subdriver\n");
7897 mutex_init(&fan_mutex);
7898 fan_status_access_mode = TPACPI_FAN_NONE;
7899 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7900 fan_control_commands = 0;
7901 fan_watchdog_maxinterval = 0;
7902 tp_features.fan_ctrl_status_undef = 0;
7903 tp_features.second_fan = 0;
7904 fan_control_desired_level = 7;
7906 TPACPI_ACPIHANDLE_INIT(fans);
7907 TPACPI_ACPIHANDLE_INIT(gfan);
7908 TPACPI_ACPIHANDLE_INIT(sfan);
7910 quirks = tpacpi_check_quirks(fan_quirk_table,
7911 ARRAY_SIZE(fan_quirk_table));
7913 if (gfan_handle) {
7914 /* 570, 600e/x, 770e, 770x */
7915 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7916 } else {
7917 /* all other ThinkPads: note that even old-style
7918 * ThinkPad ECs supports the fan control register */
7919 if (likely(acpi_ec_read(fan_status_offset,
7920 &fan_control_initial_status))) {
7921 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7922 if (quirks & TPACPI_FAN_Q1)
7923 fan_quirk1_setup();
7924 if (quirks & TPACPI_FAN_2FAN) {
7925 tp_features.second_fan = 1;
7926 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7927 "secondary fan support enabled\n");
7929 } else {
7930 printk(TPACPI_ERR
7931 "ThinkPad ACPI EC access misbehaving, "
7932 "fan status and control unavailable\n");
7933 return 1;
7937 if (sfan_handle) {
7938 /* 570, 770x-JL */
7939 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7940 fan_control_commands |=
7941 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7942 } else {
7943 if (!gfan_handle) {
7944 /* gfan without sfan means no fan control */
7945 /* all other models implement TP EC 0x2f control */
7947 if (fans_handle) {
7948 /* X31, X40, X41 */
7949 fan_control_access_mode =
7950 TPACPI_FAN_WR_ACPI_FANS;
7951 fan_control_commands |=
7952 TPACPI_FAN_CMD_SPEED |
7953 TPACPI_FAN_CMD_LEVEL |
7954 TPACPI_FAN_CMD_ENABLE;
7955 } else {
7956 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7957 fan_control_commands |=
7958 TPACPI_FAN_CMD_LEVEL |
7959 TPACPI_FAN_CMD_ENABLE;
7964 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7965 "fan is %s, modes %d, %d\n",
7966 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7967 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7968 fan_status_access_mode, fan_control_access_mode);
7970 /* fan control master switch */
7971 if (!fan_control_allowed) {
7972 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7973 fan_control_commands = 0;
7974 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7975 "fan control features disabled by parameter\n");
7978 /* update fan_control_desired_level */
7979 if (fan_status_access_mode != TPACPI_FAN_NONE)
7980 fan_get_status_safe(NULL);
7982 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7983 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
7984 if (tp_features.second_fan) {
7985 /* attach second fan tachometer */
7986 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7987 &dev_attr_fan_fan2_input.attr;
7989 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
7990 &fan_attr_group);
7991 if (rc < 0)
7992 return rc;
7994 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7995 &driver_attr_fan_watchdog);
7996 if (rc < 0) {
7997 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7998 &fan_attr_group);
7999 return rc;
8001 return 0;
8002 } else
8003 return 1;
8006 static void fan_exit(void)
8008 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8009 "cancelling any pending fan watchdog tasks\n");
8011 /* FIXME: can we really do this unconditionally? */
8012 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8013 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8014 &driver_attr_fan_watchdog);
8016 cancel_delayed_work(&fan_watchdog_task);
8017 flush_workqueue(tpacpi_wq);
8020 static void fan_suspend(pm_message_t state)
8022 int rc;
8024 if (!fan_control_allowed)
8025 return;
8027 /* Store fan status in cache */
8028 fan_control_resume_level = 0;
8029 rc = fan_get_status_safe(&fan_control_resume_level);
8030 if (rc < 0)
8031 printk(TPACPI_NOTICE
8032 "failed to read fan level for later "
8033 "restore during resume: %d\n", rc);
8035 /* if it is undefined, don't attempt to restore it.
8036 * KEEP THIS LAST */
8037 if (tp_features.fan_ctrl_status_undef)
8038 fan_control_resume_level = 0;
8041 static void fan_resume(void)
8043 u8 current_level = 7;
8044 bool do_set = false;
8045 int rc;
8047 /* DSDT *always* updates status on resume */
8048 tp_features.fan_ctrl_status_undef = 0;
8050 if (!fan_control_allowed ||
8051 !fan_control_resume_level ||
8052 (fan_get_status_safe(&current_level) < 0))
8053 return;
8055 switch (fan_control_access_mode) {
8056 case TPACPI_FAN_WR_ACPI_SFAN:
8057 /* never decrease fan level */
8058 do_set = (fan_control_resume_level > current_level);
8059 break;
8060 case TPACPI_FAN_WR_ACPI_FANS:
8061 case TPACPI_FAN_WR_TPEC:
8062 /* never decrease fan level, scale is:
8063 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8065 * We expect the firmware to set either 7 or AUTO, but we
8066 * handle FULLSPEED out of paranoia.
8068 * So, we can safely only restore FULLSPEED or 7, anything
8069 * else could slow the fan. Restoring AUTO is useless, at
8070 * best that's exactly what the DSDT already set (it is the
8071 * slower it uses).
8073 * Always keep in mind that the DSDT *will* have set the
8074 * fans to what the vendor supposes is the best level. We
8075 * muck with it only to speed the fan up.
8077 if (fan_control_resume_level != 7 &&
8078 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8079 return;
8080 else
8081 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8082 (current_level != fan_control_resume_level);
8083 break;
8084 default:
8085 return;
8087 if (do_set) {
8088 printk(TPACPI_NOTICE
8089 "restoring fan level to 0x%02x\n",
8090 fan_control_resume_level);
8091 rc = fan_set_level_safe(fan_control_resume_level);
8092 if (rc < 0)
8093 printk(TPACPI_NOTICE
8094 "failed to restore fan level: %d\n", rc);
8098 static int fan_read(struct seq_file *m)
8100 int rc;
8101 u8 status;
8102 unsigned int speed = 0;
8104 switch (fan_status_access_mode) {
8105 case TPACPI_FAN_RD_ACPI_GFAN:
8106 /* 570, 600e/x, 770e, 770x */
8107 rc = fan_get_status_safe(&status);
8108 if (rc < 0)
8109 return rc;
8111 seq_printf(m, "status:\t\t%s\n"
8112 "level:\t\t%d\n",
8113 (status != 0) ? "enabled" : "disabled", status);
8114 break;
8116 case TPACPI_FAN_RD_TPEC:
8117 /* all except 570, 600e/x, 770e, 770x */
8118 rc = fan_get_status_safe(&status);
8119 if (rc < 0)
8120 return rc;
8122 seq_printf(m, "status:\t\t%s\n",
8123 (status != 0) ? "enabled" : "disabled");
8125 rc = fan_get_speed(&speed);
8126 if (rc < 0)
8127 return rc;
8129 seq_printf(m, "speed:\t\t%d\n", speed);
8131 if (status & TP_EC_FAN_FULLSPEED)
8132 /* Disengaged mode takes precedence */
8133 seq_printf(m, "level:\t\tdisengaged\n");
8134 else if (status & TP_EC_FAN_AUTO)
8135 seq_printf(m, "level:\t\tauto\n");
8136 else
8137 seq_printf(m, "level:\t\t%d\n", status);
8138 break;
8140 case TPACPI_FAN_NONE:
8141 default:
8142 seq_printf(m, "status:\t\tnot supported\n");
8145 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8146 seq_printf(m, "commands:\tlevel <level>");
8148 switch (fan_control_access_mode) {
8149 case TPACPI_FAN_WR_ACPI_SFAN:
8150 seq_printf(m, " (<level> is 0-7)\n");
8151 break;
8153 default:
8154 seq_printf(m, " (<level> is 0-7, "
8155 "auto, disengaged, full-speed)\n");
8156 break;
8160 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8161 seq_printf(m, "commands:\tenable, disable\n"
8162 "commands:\twatchdog <timeout> (<timeout> "
8163 "is 0 (off), 1-120 (seconds))\n");
8165 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8166 seq_printf(m, "commands:\tspeed <speed>"
8167 " (<speed> is 0-65535)\n");
8169 return 0;
8172 static int fan_write_cmd_level(const char *cmd, int *rc)
8174 int level;
8176 if (strlencmp(cmd, "level auto") == 0)
8177 level = TP_EC_FAN_AUTO;
8178 else if ((strlencmp(cmd, "level disengaged") == 0) |
8179 (strlencmp(cmd, "level full-speed") == 0))
8180 level = TP_EC_FAN_FULLSPEED;
8181 else if (sscanf(cmd, "level %d", &level) != 1)
8182 return 0;
8184 *rc = fan_set_level_safe(level);
8185 if (*rc == -ENXIO)
8186 printk(TPACPI_ERR "level command accepted for unsupported "
8187 "access mode %d", fan_control_access_mode);
8188 else if (!*rc)
8189 tpacpi_disclose_usertask("procfs fan",
8190 "set level to %d\n", level);
8192 return 1;
8195 static int fan_write_cmd_enable(const char *cmd, int *rc)
8197 if (strlencmp(cmd, "enable") != 0)
8198 return 0;
8200 *rc = fan_set_enable();
8201 if (*rc == -ENXIO)
8202 printk(TPACPI_ERR "enable command accepted for unsupported "
8203 "access mode %d", fan_control_access_mode);
8204 else if (!*rc)
8205 tpacpi_disclose_usertask("procfs fan", "enable\n");
8207 return 1;
8210 static int fan_write_cmd_disable(const char *cmd, int *rc)
8212 if (strlencmp(cmd, "disable") != 0)
8213 return 0;
8215 *rc = fan_set_disable();
8216 if (*rc == -ENXIO)
8217 printk(TPACPI_ERR "disable command accepted for unsupported "
8218 "access mode %d", fan_control_access_mode);
8219 else if (!*rc)
8220 tpacpi_disclose_usertask("procfs fan", "disable\n");
8222 return 1;
8225 static int fan_write_cmd_speed(const char *cmd, int *rc)
8227 int speed;
8229 /* TODO:
8230 * Support speed <low> <medium> <high> ? */
8232 if (sscanf(cmd, "speed %d", &speed) != 1)
8233 return 0;
8235 *rc = fan_set_speed(speed);
8236 if (*rc == -ENXIO)
8237 printk(TPACPI_ERR "speed command accepted for unsupported "
8238 "access mode %d", fan_control_access_mode);
8239 else if (!*rc)
8240 tpacpi_disclose_usertask("procfs fan",
8241 "set speed to %d\n", speed);
8243 return 1;
8246 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8248 int interval;
8250 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8251 return 0;
8253 if (interval < 0 || interval > 120)
8254 *rc = -EINVAL;
8255 else {
8256 fan_watchdog_maxinterval = interval;
8257 tpacpi_disclose_usertask("procfs fan",
8258 "set watchdog timer to %d\n",
8259 interval);
8262 return 1;
8265 static int fan_write(char *buf)
8267 char *cmd;
8268 int rc = 0;
8270 while (!rc && (cmd = next_cmd(&buf))) {
8271 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8272 fan_write_cmd_level(cmd, &rc)) &&
8273 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8274 (fan_write_cmd_enable(cmd, &rc) ||
8275 fan_write_cmd_disable(cmd, &rc) ||
8276 fan_write_cmd_watchdog(cmd, &rc))) &&
8277 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8278 fan_write_cmd_speed(cmd, &rc))
8280 rc = -EINVAL;
8281 else if (!rc)
8282 fan_watchdog_reset();
8285 return rc;
8288 static struct ibm_struct fan_driver_data = {
8289 .name = "fan",
8290 .read = fan_read,
8291 .write = fan_write,
8292 .exit = fan_exit,
8293 .suspend = fan_suspend,
8294 .resume = fan_resume,
8297 /****************************************************************************
8298 ****************************************************************************
8300 * Infrastructure
8302 ****************************************************************************
8303 ****************************************************************************/
8306 * HKEY event callout for other subdrivers go here
8307 * (yes, it is ugly, but it is quick, safe, and gets the job done
8309 static void tpacpi_driver_event(const unsigned int hkey_event)
8311 if (ibm_backlight_device) {
8312 switch (hkey_event) {
8313 case TP_HKEY_EV_BRGHT_UP:
8314 case TP_HKEY_EV_BRGHT_DOWN:
8315 tpacpi_brightness_notify_change();
8318 if (alsa_card) {
8319 switch (hkey_event) {
8320 case TP_HKEY_EV_VOL_UP:
8321 case TP_HKEY_EV_VOL_DOWN:
8322 case TP_HKEY_EV_VOL_MUTE:
8323 volume_alsa_notify_change();
8328 static void hotkey_driver_event(const unsigned int scancode)
8330 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8333 /* sysfs name ---------------------------------------------------------- */
8334 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8335 struct device_attribute *attr,
8336 char *buf)
8338 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8341 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8342 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8344 /* --------------------------------------------------------------------- */
8346 /* /proc support */
8347 static struct proc_dir_entry *proc_dir;
8350 * Module and infrastructure proble, init and exit handling
8353 static int force_load;
8355 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8356 static const char * __init str_supported(int is_supported)
8358 static char text_unsupported[] __initdata = "not supported";
8360 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8362 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8364 static void ibm_exit(struct ibm_struct *ibm)
8366 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8368 list_del_init(&ibm->all_drivers);
8370 if (ibm->flags.acpi_notify_installed) {
8371 dbg_printk(TPACPI_DBG_EXIT,
8372 "%s: acpi_remove_notify_handler\n", ibm->name);
8373 BUG_ON(!ibm->acpi);
8374 acpi_remove_notify_handler(*ibm->acpi->handle,
8375 ibm->acpi->type,
8376 dispatch_acpi_notify);
8377 ibm->flags.acpi_notify_installed = 0;
8378 ibm->flags.acpi_notify_installed = 0;
8381 if (ibm->flags.proc_created) {
8382 dbg_printk(TPACPI_DBG_EXIT,
8383 "%s: remove_proc_entry\n", ibm->name);
8384 remove_proc_entry(ibm->name, proc_dir);
8385 ibm->flags.proc_created = 0;
8388 if (ibm->flags.acpi_driver_registered) {
8389 dbg_printk(TPACPI_DBG_EXIT,
8390 "%s: acpi_bus_unregister_driver\n", ibm->name);
8391 BUG_ON(!ibm->acpi);
8392 acpi_bus_unregister_driver(ibm->acpi->driver);
8393 kfree(ibm->acpi->driver);
8394 ibm->acpi->driver = NULL;
8395 ibm->flags.acpi_driver_registered = 0;
8398 if (ibm->flags.init_called && ibm->exit) {
8399 ibm->exit();
8400 ibm->flags.init_called = 0;
8403 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8406 static int __init ibm_init(struct ibm_init_struct *iibm)
8408 int ret;
8409 struct ibm_struct *ibm = iibm->data;
8410 struct proc_dir_entry *entry;
8412 BUG_ON(ibm == NULL);
8414 INIT_LIST_HEAD(&ibm->all_drivers);
8416 if (ibm->flags.experimental && !experimental)
8417 return 0;
8419 dbg_printk(TPACPI_DBG_INIT,
8420 "probing for %s\n", ibm->name);
8422 if (iibm->init) {
8423 ret = iibm->init(iibm);
8424 if (ret > 0)
8425 return 0; /* probe failed */
8426 if (ret)
8427 return ret;
8429 ibm->flags.init_called = 1;
8432 if (ibm->acpi) {
8433 if (ibm->acpi->hid) {
8434 ret = register_tpacpi_subdriver(ibm);
8435 if (ret)
8436 goto err_out;
8439 if (ibm->acpi->notify) {
8440 ret = setup_acpi_notify(ibm);
8441 if (ret == -ENODEV) {
8442 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8443 ibm->name);
8444 ret = 0;
8445 goto err_out;
8447 if (ret < 0)
8448 goto err_out;
8452 dbg_printk(TPACPI_DBG_INIT,
8453 "%s installed\n", ibm->name);
8455 if (ibm->read) {
8456 mode_t mode;
8458 mode = S_IRUGO;
8459 if (ibm->write)
8460 mode |= S_IWUSR;
8461 entry = proc_create_data(ibm->name, mode, proc_dir,
8462 &dispatch_proc_fops, ibm);
8463 if (!entry) {
8464 printk(TPACPI_ERR "unable to create proc entry %s\n",
8465 ibm->name);
8466 ret = -ENODEV;
8467 goto err_out;
8469 ibm->flags.proc_created = 1;
8472 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8474 return 0;
8476 err_out:
8477 dbg_printk(TPACPI_DBG_INIT,
8478 "%s: at error exit path with result %d\n",
8479 ibm->name, ret);
8481 ibm_exit(ibm);
8482 return (ret < 0)? ret : 0;
8485 /* Probing */
8487 static bool __pure __init tpacpi_is_fw_digit(const char c)
8489 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8492 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8493 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8494 const char t)
8496 return s && strlen(s) >= 8 &&
8497 tpacpi_is_fw_digit(s[0]) &&
8498 tpacpi_is_fw_digit(s[1]) &&
8499 s[2] == t && s[3] == 'T' &&
8500 tpacpi_is_fw_digit(s[4]) &&
8501 tpacpi_is_fw_digit(s[5]) &&
8502 s[6] == 'W' && s[7] == 'W';
8505 /* returns 0 - probe ok, or < 0 - probe error.
8506 * Probe ok doesn't mean thinkpad found.
8507 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8508 static int __must_check __init get_thinkpad_model_data(
8509 struct thinkpad_id_data *tp)
8511 const struct dmi_device *dev = NULL;
8512 char ec_fw_string[18];
8513 char const *s;
8515 if (!tp)
8516 return -EINVAL;
8518 memset(tp, 0, sizeof(*tp));
8520 if (dmi_name_in_vendors("IBM"))
8521 tp->vendor = PCI_VENDOR_ID_IBM;
8522 else if (dmi_name_in_vendors("LENOVO"))
8523 tp->vendor = PCI_VENDOR_ID_LENOVO;
8524 else
8525 return 0;
8527 s = dmi_get_system_info(DMI_BIOS_VERSION);
8528 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8529 if (s && !tp->bios_version_str)
8530 return -ENOMEM;
8532 /* Really ancient ThinkPad 240X will fail this, which is fine */
8533 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8534 return 0;
8536 tp->bios_model = tp->bios_version_str[0]
8537 | (tp->bios_version_str[1] << 8);
8538 tp->bios_release = (tp->bios_version_str[4] << 8)
8539 | tp->bios_version_str[5];
8542 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8543 * X32 or newer, all Z series; Some models must have an
8544 * up-to-date BIOS or they will not be detected.
8546 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8548 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8549 if (sscanf(dev->name,
8550 "IBM ThinkPad Embedded Controller -[%17c",
8551 ec_fw_string) == 1) {
8552 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8553 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8555 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8556 if (!tp->ec_version_str)
8557 return -ENOMEM;
8559 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8560 tp->ec_model = ec_fw_string[0]
8561 | (ec_fw_string[1] << 8);
8562 tp->ec_release = (ec_fw_string[4] << 8)
8563 | ec_fw_string[5];
8564 } else {
8565 printk(TPACPI_NOTICE
8566 "ThinkPad firmware release %s "
8567 "doesn't match the known patterns\n",
8568 ec_fw_string);
8569 printk(TPACPI_NOTICE
8570 "please report this to %s\n",
8571 TPACPI_MAIL);
8573 break;
8577 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8578 if (s && !strnicmp(s, "ThinkPad", 8)) {
8579 tp->model_str = kstrdup(s, GFP_KERNEL);
8580 if (!tp->model_str)
8581 return -ENOMEM;
8584 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8585 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8586 if (s && !tp->nummodel_str)
8587 return -ENOMEM;
8589 return 0;
8592 static int __init probe_for_thinkpad(void)
8594 int is_thinkpad;
8596 if (acpi_disabled)
8597 return -ENODEV;
8600 * Non-ancient models have better DMI tagging, but very old models
8601 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8603 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8604 (thinkpad_id.ec_model != 0) ||
8605 tpacpi_is_fw_known();
8607 /* ec is required because many other handles are relative to it */
8608 TPACPI_ACPIHANDLE_INIT(ec);
8609 if (!ec_handle) {
8610 if (is_thinkpad)
8611 printk(TPACPI_ERR
8612 "Not yet supported ThinkPad detected!\n");
8613 return -ENODEV;
8616 if (!is_thinkpad && !force_load)
8617 return -ENODEV;
8619 return 0;
8623 /* Module init, exit, parameters */
8625 static struct ibm_init_struct ibms_init[] __initdata = {
8627 .init = thinkpad_acpi_driver_init,
8628 .data = &thinkpad_acpi_driver_data,
8631 .init = hotkey_init,
8632 .data = &hotkey_driver_data,
8635 .init = bluetooth_init,
8636 .data = &bluetooth_driver_data,
8639 .init = wan_init,
8640 .data = &wan_driver_data,
8643 .init = uwb_init,
8644 .data = &uwb_driver_data,
8646 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8648 .init = video_init,
8649 .data = &video_driver_data,
8651 #endif
8653 .init = light_init,
8654 .data = &light_driver_data,
8657 .init = cmos_init,
8658 .data = &cmos_driver_data,
8661 .init = led_init,
8662 .data = &led_driver_data,
8665 .init = beep_init,
8666 .data = &beep_driver_data,
8669 .init = thermal_init,
8670 .data = &thermal_driver_data,
8673 .data = &ecdump_driver_data,
8676 .init = brightness_init,
8677 .data = &brightness_driver_data,
8680 .init = volume_init,
8681 .data = &volume_driver_data,
8684 .init = fan_init,
8685 .data = &fan_driver_data,
8689 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8691 unsigned int i;
8692 struct ibm_struct *ibm;
8694 if (!kp || !kp->name || !val)
8695 return -EINVAL;
8697 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8698 ibm = ibms_init[i].data;
8699 WARN_ON(ibm == NULL);
8701 if (!ibm || !ibm->name)
8702 continue;
8704 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8705 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8706 return -ENOSPC;
8707 strcpy(ibms_init[i].param, val);
8708 strcat(ibms_init[i].param, ",");
8709 return 0;
8713 return -EINVAL;
8716 module_param(experimental, int, 0444);
8717 MODULE_PARM_DESC(experimental,
8718 "Enables experimental features when non-zero");
8720 module_param_named(debug, dbg_level, uint, 0);
8721 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8723 module_param(force_load, bool, 0444);
8724 MODULE_PARM_DESC(force_load,
8725 "Attempts to load the driver even on a "
8726 "mis-identified ThinkPad when true");
8728 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8729 MODULE_PARM_DESC(fan_control,
8730 "Enables setting fan parameters features when true");
8732 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8733 MODULE_PARM_DESC(brightness_mode,
8734 "Selects brightness control strategy: "
8735 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8737 module_param(brightness_enable, uint, 0444);
8738 MODULE_PARM_DESC(brightness_enable,
8739 "Enables backlight control when 1, disables when 0");
8741 module_param(hotkey_report_mode, uint, 0444);
8742 MODULE_PARM_DESC(hotkey_report_mode,
8743 "used for backwards compatibility with userspace, "
8744 "see documentation");
8746 module_param_named(volume_mode, volume_mode, uint, 0444);
8747 MODULE_PARM_DESC(volume_mode,
8748 "Selects volume control strategy: "
8749 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8751 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8752 MODULE_PARM_DESC(volume_capabilities,
8753 "Selects the mixer capabilites: "
8754 "0=auto, 1=volume and mute, 2=mute only");
8756 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8757 MODULE_PARM_DESC(volume_control,
8758 "Enables software override for the console audio "
8759 "control when true");
8761 /* ALSA module API parameters */
8762 module_param_named(index, alsa_index, int, 0444);
8763 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8764 module_param_named(id, alsa_id, charp, 0444);
8765 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8766 module_param_named(enable, alsa_enable, bool, 0444);
8767 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8769 #define TPACPI_PARAM(feature) \
8770 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8771 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8772 "at module load, see documentation")
8774 TPACPI_PARAM(hotkey);
8775 TPACPI_PARAM(bluetooth);
8776 TPACPI_PARAM(video);
8777 TPACPI_PARAM(light);
8778 TPACPI_PARAM(cmos);
8779 TPACPI_PARAM(led);
8780 TPACPI_PARAM(beep);
8781 TPACPI_PARAM(ecdump);
8782 TPACPI_PARAM(brightness);
8783 TPACPI_PARAM(volume);
8784 TPACPI_PARAM(fan);
8786 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8787 module_param(dbg_wlswemul, uint, 0444);
8788 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8789 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8790 MODULE_PARM_DESC(wlsw_state,
8791 "Initial state of the emulated WLSW switch");
8793 module_param(dbg_bluetoothemul, uint, 0444);
8794 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8795 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8796 MODULE_PARM_DESC(bluetooth_state,
8797 "Initial state of the emulated bluetooth switch");
8799 module_param(dbg_wwanemul, uint, 0444);
8800 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8801 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8802 MODULE_PARM_DESC(wwan_state,
8803 "Initial state of the emulated WWAN switch");
8805 module_param(dbg_uwbemul, uint, 0444);
8806 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8807 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8808 MODULE_PARM_DESC(uwb_state,
8809 "Initial state of the emulated UWB switch");
8810 #endif
8812 static void thinkpad_acpi_module_exit(void)
8814 struct ibm_struct *ibm, *itmp;
8816 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8818 list_for_each_entry_safe_reverse(ibm, itmp,
8819 &tpacpi_all_drivers,
8820 all_drivers) {
8821 ibm_exit(ibm);
8824 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8826 if (tpacpi_inputdev) {
8827 if (tp_features.input_device_registered)
8828 input_unregister_device(tpacpi_inputdev);
8829 else
8830 input_free_device(tpacpi_inputdev);
8833 if (tpacpi_hwmon)
8834 hwmon_device_unregister(tpacpi_hwmon);
8836 if (tp_features.sensors_pdev_attrs_registered)
8837 device_remove_file(&tpacpi_sensors_pdev->dev,
8838 &dev_attr_thinkpad_acpi_pdev_name);
8839 if (tpacpi_sensors_pdev)
8840 platform_device_unregister(tpacpi_sensors_pdev);
8841 if (tpacpi_pdev)
8842 platform_device_unregister(tpacpi_pdev);
8844 if (tp_features.sensors_pdrv_attrs_registered)
8845 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8846 if (tp_features.platform_drv_attrs_registered)
8847 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8849 if (tp_features.sensors_pdrv_registered)
8850 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8852 if (tp_features.platform_drv_registered)
8853 platform_driver_unregister(&tpacpi_pdriver);
8855 if (proc_dir)
8856 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8858 if (tpacpi_wq)
8859 destroy_workqueue(tpacpi_wq);
8861 kfree(thinkpad_id.bios_version_str);
8862 kfree(thinkpad_id.ec_version_str);
8863 kfree(thinkpad_id.model_str);
8867 static int __init thinkpad_acpi_module_init(void)
8869 int ret, i;
8871 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8873 /* Parameter checking */
8874 if (hotkey_report_mode > 2)
8875 return -EINVAL;
8877 /* Driver-level probe */
8879 ret = get_thinkpad_model_data(&thinkpad_id);
8880 if (ret) {
8881 printk(TPACPI_ERR
8882 "unable to get DMI data: %d\n", ret);
8883 thinkpad_acpi_module_exit();
8884 return ret;
8886 ret = probe_for_thinkpad();
8887 if (ret) {
8888 thinkpad_acpi_module_exit();
8889 return ret;
8892 /* Driver initialization */
8894 TPACPI_ACPIHANDLE_INIT(ecrd);
8895 TPACPI_ACPIHANDLE_INIT(ecwr);
8897 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8898 if (!tpacpi_wq) {
8899 thinkpad_acpi_module_exit();
8900 return -ENOMEM;
8903 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8904 if (!proc_dir) {
8905 printk(TPACPI_ERR
8906 "unable to create proc dir " TPACPI_PROC_DIR);
8907 thinkpad_acpi_module_exit();
8908 return -ENODEV;
8911 ret = platform_driver_register(&tpacpi_pdriver);
8912 if (ret) {
8913 printk(TPACPI_ERR
8914 "unable to register main platform driver\n");
8915 thinkpad_acpi_module_exit();
8916 return ret;
8918 tp_features.platform_drv_registered = 1;
8920 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8921 if (ret) {
8922 printk(TPACPI_ERR
8923 "unable to register hwmon platform driver\n");
8924 thinkpad_acpi_module_exit();
8925 return ret;
8927 tp_features.sensors_pdrv_registered = 1;
8929 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8930 if (!ret) {
8931 tp_features.platform_drv_attrs_registered = 1;
8932 ret = tpacpi_create_driver_attributes(
8933 &tpacpi_hwmon_pdriver.driver);
8935 if (ret) {
8936 printk(TPACPI_ERR
8937 "unable to create sysfs driver attributes\n");
8938 thinkpad_acpi_module_exit();
8939 return ret;
8941 tp_features.sensors_pdrv_attrs_registered = 1;
8944 /* Device initialization */
8945 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8946 NULL, 0);
8947 if (IS_ERR(tpacpi_pdev)) {
8948 ret = PTR_ERR(tpacpi_pdev);
8949 tpacpi_pdev = NULL;
8950 printk(TPACPI_ERR "unable to register platform device\n");
8951 thinkpad_acpi_module_exit();
8952 return ret;
8954 tpacpi_sensors_pdev = platform_device_register_simple(
8955 TPACPI_HWMON_DRVR_NAME,
8956 -1, NULL, 0);
8957 if (IS_ERR(tpacpi_sensors_pdev)) {
8958 ret = PTR_ERR(tpacpi_sensors_pdev);
8959 tpacpi_sensors_pdev = NULL;
8960 printk(TPACPI_ERR
8961 "unable to register hwmon platform device\n");
8962 thinkpad_acpi_module_exit();
8963 return ret;
8965 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8966 &dev_attr_thinkpad_acpi_pdev_name);
8967 if (ret) {
8968 printk(TPACPI_ERR
8969 "unable to create sysfs hwmon device attributes\n");
8970 thinkpad_acpi_module_exit();
8971 return ret;
8973 tp_features.sensors_pdev_attrs_registered = 1;
8974 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
8975 if (IS_ERR(tpacpi_hwmon)) {
8976 ret = PTR_ERR(tpacpi_hwmon);
8977 tpacpi_hwmon = NULL;
8978 printk(TPACPI_ERR "unable to register hwmon device\n");
8979 thinkpad_acpi_module_exit();
8980 return ret;
8982 mutex_init(&tpacpi_inputdev_send_mutex);
8983 tpacpi_inputdev = input_allocate_device();
8984 if (!tpacpi_inputdev) {
8985 printk(TPACPI_ERR "unable to allocate input device\n");
8986 thinkpad_acpi_module_exit();
8987 return -ENOMEM;
8988 } else {
8989 /* Prepare input device, but don't register */
8990 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
8991 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
8992 tpacpi_inputdev->id.bustype = BUS_HOST;
8993 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8994 thinkpad_id.vendor :
8995 PCI_VENDOR_ID_IBM;
8996 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8997 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8998 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9000 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9001 ret = ibm_init(&ibms_init[i]);
9002 if (ret >= 0 && *ibms_init[i].param)
9003 ret = ibms_init[i].data->write(ibms_init[i].param);
9004 if (ret < 0) {
9005 thinkpad_acpi_module_exit();
9006 return ret;
9009 ret = input_register_device(tpacpi_inputdev);
9010 if (ret < 0) {
9011 printk(TPACPI_ERR "unable to register input device\n");
9012 thinkpad_acpi_module_exit();
9013 return ret;
9014 } else {
9015 tp_features.input_device_registered = 1;
9018 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9019 return 0;
9022 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9025 * This will autoload the driver in almost every ThinkPad
9026 * in widespread use.
9028 * Only _VERY_ old models, like the 240, 240x and 570 lack
9029 * the HKEY event interface.
9031 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9034 * DMI matching for module autoloading
9036 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9037 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9039 * Only models listed in thinkwiki will be supported, so add yours
9040 * if it is not there yet.
9042 #define IBM_BIOS_MODULE_ALIAS(__type) \
9043 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9045 /* Ancient thinkpad BIOSes have to be identified by
9046 * BIOS type or model number, and there are far less
9047 * BIOS types than model numbers... */
9048 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9050 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9051 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9052 MODULE_DESCRIPTION(TPACPI_DESC);
9053 MODULE_VERSION(TPACPI_VERSION);
9054 MODULE_LICENSE("GPL");
9056 module_init(thinkpad_acpi_module_init);
9057 module_exit(thinkpad_acpi_module_exit);