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