thinkpad-acpi: acpi_evalf fixes
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobda3a70b280f20db5c338ec728fd06a06396378e1
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>
61 #include <linux/slab.h>
63 #include <linux/nvram.h>
64 #include <linux/proc_fs.h>
65 #include <linux/seq_file.h>
66 #include <linux/sysfs.h>
67 #include <linux/backlight.h>
68 #include <linux/fb.h>
69 #include <linux/platform_device.h>
70 #include <linux/hwmon.h>
71 #include <linux/hwmon-sysfs.h>
72 #include <linux/input.h>
73 #include <linux/leds.h>
74 #include <linux/rfkill.h>
75 #include <asm/uaccess.h>
77 #include <linux/dmi.h>
78 #include <linux/jiffies.h>
79 #include <linux/workqueue.h>
81 #include <sound/core.h>
82 #include <sound/control.h>
83 #include <sound/initval.h>
85 #include <acpi/acpi_drivers.h>
87 #include <linux/pci_ids.h>
90 /* ThinkPad CMOS commands */
91 #define TP_CMOS_VOLUME_DOWN 0
92 #define TP_CMOS_VOLUME_UP 1
93 #define TP_CMOS_VOLUME_MUTE 2
94 #define TP_CMOS_BRIGHTNESS_UP 4
95 #define TP_CMOS_BRIGHTNESS_DOWN 5
96 #define TP_CMOS_THINKLIGHT_ON 12
97 #define TP_CMOS_THINKLIGHT_OFF 13
99 /* NVRAM Addresses */
100 enum tp_nvram_addr {
101 TP_NVRAM_ADDR_HK2 = 0x57,
102 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
103 TP_NVRAM_ADDR_VIDEO = 0x59,
104 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
105 TP_NVRAM_ADDR_MIXER = 0x60,
108 /* NVRAM bit masks */
109 enum {
110 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
111 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
112 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
113 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
114 TP_NVRAM_MASK_THINKLIGHT = 0x10,
115 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
116 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
117 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
118 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
119 TP_NVRAM_MASK_MUTE = 0x40,
120 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
121 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
122 TP_NVRAM_POS_LEVEL_VOLUME = 0,
125 /* Misc NVRAM-related */
126 enum {
127 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
130 /* ACPI HIDs */
131 #define TPACPI_ACPI_HKEY_HID "IBM0068"
132 #define TPACPI_ACPI_EC_HID "PNP0C09"
134 /* Input IDs */
135 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
136 #define TPACPI_HKEY_INPUT_VERSION 0x4101
138 /* ACPI \WGSV commands */
139 enum {
140 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
141 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
142 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
143 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
146 /* TP_ACPI_WGSV_GET_STATE bits */
147 enum {
148 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
149 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
150 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
151 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
152 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
153 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
154 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
155 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
156 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
157 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
160 /* HKEY events */
161 enum tpacpi_hkey_event_t {
162 /* Hotkey-related */
163 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
164 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
165 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
166 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
167 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
168 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
170 /* Reasons for waking up from S3/S4 */
171 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
172 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
173 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
174 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
175 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
176 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
178 /* Auto-sleep after eject request */
179 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
180 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
182 /* Misc bay events */
183 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
185 /* User-interface events */
186 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
187 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
188 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
189 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
190 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
191 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
192 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
194 /* Thermal events */
195 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
196 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
197 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
198 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
199 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
201 /* Misc */
202 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
205 /****************************************************************************
206 * Main driver
209 #define TPACPI_NAME "thinkpad"
210 #define TPACPI_DESC "ThinkPad ACPI Extras"
211 #define TPACPI_FILE TPACPI_NAME "_acpi"
212 #define TPACPI_URL "http://ibm-acpi.sf.net/"
213 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
215 #define TPACPI_PROC_DIR "ibm"
216 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
217 #define TPACPI_DRVR_NAME TPACPI_FILE
218 #define TPACPI_DRVR_SHORTNAME "tpacpi"
219 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
221 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
222 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
224 #define TPACPI_MAX_ACPI_ARGS 3
226 /* printk headers */
227 #define TPACPI_LOG TPACPI_FILE ": "
228 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
229 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
230 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
231 #define TPACPI_ERR KERN_ERR TPACPI_LOG
232 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
233 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
234 #define TPACPI_INFO KERN_INFO TPACPI_LOG
235 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
237 /* Debugging printk groups */
238 #define TPACPI_DBG_ALL 0xffff
239 #define TPACPI_DBG_DISCLOSETASK 0x8000
240 #define TPACPI_DBG_INIT 0x0001
241 #define TPACPI_DBG_EXIT 0x0002
242 #define TPACPI_DBG_RFKILL 0x0004
243 #define TPACPI_DBG_HKEY 0x0008
244 #define TPACPI_DBG_FAN 0x0010
245 #define TPACPI_DBG_BRGHT 0x0020
246 #define TPACPI_DBG_MIXER 0x0040
248 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
249 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
250 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
253 /****************************************************************************
254 * Driver-wide structs and misc. variables
257 struct ibm_struct;
259 struct tp_acpi_drv_struct {
260 const struct acpi_device_id *hid;
261 struct acpi_driver *driver;
263 void (*notify) (struct ibm_struct *, u32);
264 acpi_handle *handle;
265 u32 type;
266 struct acpi_device *device;
269 struct ibm_struct {
270 char *name;
272 int (*read) (struct seq_file *);
273 int (*write) (char *);
274 void (*exit) (void);
275 void (*resume) (void);
276 void (*suspend) (pm_message_t state);
277 void (*shutdown) (void);
279 struct list_head all_drivers;
281 struct tp_acpi_drv_struct *acpi;
283 struct {
284 u8 acpi_driver_registered:1;
285 u8 acpi_notify_installed:1;
286 u8 proc_created:1;
287 u8 init_called:1;
288 u8 experimental:1;
289 } flags;
292 struct ibm_init_struct {
293 char param[32];
295 int (*init) (struct ibm_init_struct *);
296 mode_t base_procfs_mode;
297 struct ibm_struct *data;
300 static struct {
301 u32 bluetooth:1;
302 u32 hotkey:1;
303 u32 hotkey_mask:1;
304 u32 hotkey_wlsw:1;
305 u32 hotkey_tablet:1;
306 u32 light:1;
307 u32 light_status:1;
308 u32 bright_acpimode:1;
309 u32 bright_unkfw:1;
310 u32 wan:1;
311 u32 uwb:1;
312 u32 fan_ctrl_status_undef:1;
313 u32 second_fan:1;
314 u32 beep_needs_two_args:1;
315 u32 mixer_no_level_control:1;
316 u32 input_device_registered:1;
317 u32 platform_drv_registered:1;
318 u32 platform_drv_attrs_registered:1;
319 u32 sensors_pdrv_registered:1;
320 u32 sensors_pdrv_attrs_registered:1;
321 u32 sensors_pdev_attrs_registered:1;
322 u32 hotkey_poll_active:1;
323 } tp_features;
325 static struct {
326 u16 hotkey_mask_ff:1;
327 u16 volume_ctrl_forbidden:1;
328 } tp_warned;
330 struct thinkpad_id_data {
331 unsigned int vendor; /* ThinkPad vendor:
332 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
334 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
335 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
337 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
338 u16 ec_model;
339 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
340 u16 ec_release;
342 char *model_str; /* ThinkPad T43 */
343 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
345 static struct thinkpad_id_data thinkpad_id;
347 static enum {
348 TPACPI_LIFE_INIT = 0,
349 TPACPI_LIFE_RUNNING,
350 TPACPI_LIFE_EXITING,
351 } tpacpi_lifecycle;
353 static int experimental;
354 static u32 dbg_level;
356 static struct workqueue_struct *tpacpi_wq;
358 enum led_status_t {
359 TPACPI_LED_OFF = 0,
360 TPACPI_LED_ON,
361 TPACPI_LED_BLINK,
364 /* Special LED class that can defer work */
365 struct tpacpi_led_classdev {
366 struct led_classdev led_classdev;
367 struct work_struct work;
368 enum led_status_t new_state;
369 unsigned int led;
372 /* brightness level capabilities */
373 static unsigned int bright_maxlvl; /* 0 = unknown */
375 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
376 static int dbg_wlswemul;
377 static int tpacpi_wlsw_emulstate;
378 static int dbg_bluetoothemul;
379 static int tpacpi_bluetooth_emulstate;
380 static int dbg_wwanemul;
381 static int tpacpi_wwan_emulstate;
382 static int dbg_uwbemul;
383 static int tpacpi_uwb_emulstate;
384 #endif
387 /*************************************************************************
388 * Debugging helpers
391 #define dbg_printk(a_dbg_level, format, arg...) \
392 do { if (dbg_level & (a_dbg_level)) \
393 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
394 } while (0)
396 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
397 #define vdbg_printk dbg_printk
398 static const char *str_supported(int is_supported);
399 #else
400 #define vdbg_printk(a_dbg_level, format, arg...) \
401 do { } while (0)
402 #endif
404 static void tpacpi_log_usertask(const char * const what)
406 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
407 what, task_tgid_vnr(current));
410 #define tpacpi_disclose_usertask(what, format, arg...) \
411 do { \
412 if (unlikely( \
413 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
414 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
415 printk(TPACPI_DEBUG "%s: PID %d: " format, \
416 what, task_tgid_vnr(current), ## arg); \
418 } while (0)
421 * Quirk handling helpers
423 * ThinkPad IDs and versions seen in the field so far
424 * are two-characters from the set [0-9A-Z], i.e. base 36.
426 * We use values well outside that range as specials.
429 #define TPACPI_MATCH_ANY 0xffffU
430 #define TPACPI_MATCH_UNKNOWN 0U
432 /* TPID('1', 'Y') == 0x5931 */
433 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
435 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
436 { .vendor = PCI_VENDOR_ID_IBM, \
437 .bios = TPID(__id1, __id2), \
438 .ec = TPACPI_MATCH_ANY, \
439 .quirks = (__quirk) }
441 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
442 { .vendor = PCI_VENDOR_ID_LENOVO, \
443 .bios = TPID(__id1, __id2), \
444 .ec = TPACPI_MATCH_ANY, \
445 .quirks = (__quirk) }
447 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
448 { .vendor = PCI_VENDOR_ID_LENOVO, \
449 .bios = TPACPI_MATCH_ANY, \
450 .ec = TPID(__id1, __id2), \
451 .quirks = (__quirk) }
453 struct tpacpi_quirk {
454 unsigned int vendor;
455 u16 bios;
456 u16 ec;
457 unsigned long quirks;
461 * tpacpi_check_quirks() - search BIOS/EC version on a list
462 * @qlist: array of &struct tpacpi_quirk
463 * @qlist_size: number of elements in @qlist
465 * Iterates over a quirks list until one is found that matches the
466 * ThinkPad's vendor, BIOS and EC model.
468 * Returns 0 if nothing matches, otherwise returns the quirks field of
469 * the matching &struct tpacpi_quirk entry.
471 * The match criteria is: vendor, ec and bios much match.
473 static unsigned long __init tpacpi_check_quirks(
474 const struct tpacpi_quirk *qlist,
475 unsigned int qlist_size)
477 while (qlist_size) {
478 if ((qlist->vendor == thinkpad_id.vendor ||
479 qlist->vendor == TPACPI_MATCH_ANY) &&
480 (qlist->bios == thinkpad_id.bios_model ||
481 qlist->bios == TPACPI_MATCH_ANY) &&
482 (qlist->ec == thinkpad_id.ec_model ||
483 qlist->ec == TPACPI_MATCH_ANY))
484 return qlist->quirks;
486 qlist_size--;
487 qlist++;
489 return 0;
492 static inline bool __pure __init tpacpi_is_lenovo(void)
494 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
497 static inline bool __pure __init tpacpi_is_ibm(void)
499 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
502 /****************************************************************************
503 ****************************************************************************
505 * ACPI Helpers and device model
507 ****************************************************************************
508 ****************************************************************************/
510 /*************************************************************************
511 * ACPI basic handles
514 static acpi_handle root_handle;
515 static acpi_handle ec_handle;
517 #define TPACPI_HANDLE(object, parent, paths...) \
518 static acpi_handle object##_handle; \
519 static acpi_handle *object##_parent = &parent##_handle; \
520 static char *object##_path; \
521 static char *object##_paths[] = { paths }
523 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
524 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
526 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
527 /* T4x, X31, X40 */
528 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
529 "\\CMS", /* R40, R40e */
530 ); /* all others */
532 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
533 "^HKEY", /* R30, R31 */
534 "HKEY", /* all others */
535 ); /* 570 */
537 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
538 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
539 "\\_SB.PCI0.VID0", /* 770e */
540 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
541 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
542 "\\_SB.PCI0.AGP.VID", /* all others */
543 ); /* R30, R31 */
546 /*************************************************************************
547 * ACPI helpers
550 static int acpi_evalf(acpi_handle handle,
551 void *res, char *method, char *fmt, ...)
553 char *fmt0 = fmt;
554 struct acpi_object_list params;
555 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
556 struct acpi_buffer result, *resultp;
557 union acpi_object out_obj;
558 acpi_status status;
559 va_list ap;
560 char res_type;
561 int success;
562 int quiet;
564 if (!*fmt) {
565 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
566 return 0;
569 if (*fmt == 'q') {
570 quiet = 1;
571 fmt++;
572 } else
573 quiet = 0;
575 res_type = *(fmt++);
577 params.count = 0;
578 params.pointer = &in_objs[0];
580 va_start(ap, fmt);
581 while (*fmt) {
582 char c = *(fmt++);
583 switch (c) {
584 case 'd': /* int */
585 in_objs[params.count].integer.value = va_arg(ap, int);
586 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
587 break;
588 /* add more types as needed */
589 default:
590 printk(TPACPI_ERR "acpi_evalf() called "
591 "with invalid format character '%c'\n", c);
592 return 0;
595 va_end(ap);
597 if (res_type != 'v') {
598 result.length = sizeof(out_obj);
599 result.pointer = &out_obj;
600 resultp = &result;
601 } else
602 resultp = NULL;
604 status = acpi_evaluate_object(handle, method, &params, resultp);
606 switch (res_type) {
607 case 'd': /* int */
608 success = (status == AE_OK &&
609 out_obj.type == ACPI_TYPE_INTEGER);
610 if (success && res)
611 *(int *)res = out_obj.integer.value;
612 break;
613 case 'v': /* void */
614 success = status == AE_OK;
615 break;
616 /* add more types as needed */
617 default:
618 printk(TPACPI_ERR "acpi_evalf() called "
619 "with invalid format character '%c'\n", res_type);
620 return 0;
623 if (!success && !quiet)
624 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %s\n",
625 method, fmt0, acpi_format_exception(status));
627 return success;
630 static int acpi_ec_read(int i, u8 *p)
632 int v;
634 if (ecrd_handle) {
635 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
636 return 0;
637 *p = v;
638 } else {
639 if (ec_read(i, p) < 0)
640 return 0;
643 return 1;
646 static int acpi_ec_write(int i, u8 v)
648 if (ecwr_handle) {
649 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
650 return 0;
651 } else {
652 if (ec_write(i, v) < 0)
653 return 0;
656 return 1;
659 static int issue_thinkpad_cmos_command(int cmos_cmd)
661 if (!cmos_handle)
662 return -ENXIO;
664 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
665 return -EIO;
667 return 0;
670 /*************************************************************************
671 * ACPI device model
674 #define TPACPI_ACPIHANDLE_INIT(object) \
675 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
676 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
678 static void drv_acpi_handle_init(char *name,
679 acpi_handle *handle, acpi_handle parent,
680 char **paths, int num_paths, char **path)
682 int i;
683 acpi_status status;
685 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
686 name);
688 for (i = 0; i < num_paths; i++) {
689 status = acpi_get_handle(parent, paths[i], handle);
690 if (ACPI_SUCCESS(status)) {
691 *path = paths[i];
692 dbg_printk(TPACPI_DBG_INIT,
693 "Found ACPI handle %s for %s\n",
694 *path, name);
695 return;
699 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
700 name);
701 *handle = NULL;
704 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
705 u32 level, void *context, void **return_value)
707 *(acpi_handle *)return_value = handle;
709 return AE_CTRL_TERMINATE;
712 static void __init tpacpi_acpi_handle_locate(const char *name,
713 const char *hid,
714 acpi_handle *handle)
716 acpi_status status;
717 acpi_handle device_found;
719 BUG_ON(!name || !hid || !handle);
720 vdbg_printk(TPACPI_DBG_INIT,
721 "trying to locate ACPI handle for %s, using HID %s\n",
722 name, hid);
724 memset(&device_found, 0, sizeof(device_found));
725 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
726 (void *)name, &device_found);
728 *handle = NULL;
730 if (ACPI_SUCCESS(status)) {
731 *handle = device_found;
732 dbg_printk(TPACPI_DBG_INIT,
733 "Found ACPI handle for %s\n", name);
734 } else {
735 vdbg_printk(TPACPI_DBG_INIT,
736 "Could not locate an ACPI handle for %s: %s\n",
737 name, acpi_format_exception(status));
741 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
743 struct ibm_struct *ibm = data;
745 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
746 return;
748 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
749 return;
751 ibm->acpi->notify(ibm, event);
754 static int __init setup_acpi_notify(struct ibm_struct *ibm)
756 acpi_status status;
757 int rc;
759 BUG_ON(!ibm->acpi);
761 if (!*ibm->acpi->handle)
762 return 0;
764 vdbg_printk(TPACPI_DBG_INIT,
765 "setting up ACPI notify for %s\n", ibm->name);
767 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
768 if (rc < 0) {
769 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
770 ibm->name, rc);
771 return -ENODEV;
774 ibm->acpi->device->driver_data = ibm;
775 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
776 TPACPI_ACPI_EVENT_PREFIX,
777 ibm->name);
779 status = acpi_install_notify_handler(*ibm->acpi->handle,
780 ibm->acpi->type, dispatch_acpi_notify, ibm);
781 if (ACPI_FAILURE(status)) {
782 if (status == AE_ALREADY_EXISTS) {
783 printk(TPACPI_NOTICE
784 "another device driver is already "
785 "handling %s events\n", ibm->name);
786 } else {
787 printk(TPACPI_ERR
788 "acpi_install_notify_handler(%s) failed: %d\n",
789 ibm->name, status);
791 return -ENODEV;
793 ibm->flags.acpi_notify_installed = 1;
794 return 0;
797 static int __init tpacpi_device_add(struct acpi_device *device)
799 return 0;
802 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
804 int rc;
806 dbg_printk(TPACPI_DBG_INIT,
807 "registering %s as an ACPI driver\n", ibm->name);
809 BUG_ON(!ibm->acpi);
811 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
812 if (!ibm->acpi->driver) {
813 printk(TPACPI_ERR
814 "failed to allocate memory for ibm->acpi->driver\n");
815 return -ENOMEM;
818 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
819 ibm->acpi->driver->ids = ibm->acpi->hid;
821 ibm->acpi->driver->ops.add = &tpacpi_device_add;
823 rc = acpi_bus_register_driver(ibm->acpi->driver);
824 if (rc < 0) {
825 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
826 ibm->name, rc);
827 kfree(ibm->acpi->driver);
828 ibm->acpi->driver = NULL;
829 } else if (!rc)
830 ibm->flags.acpi_driver_registered = 1;
832 return rc;
836 /****************************************************************************
837 ****************************************************************************
839 * Procfs Helpers
841 ****************************************************************************
842 ****************************************************************************/
844 static int dispatch_proc_show(struct seq_file *m, void *v)
846 struct ibm_struct *ibm = m->private;
848 if (!ibm || !ibm->read)
849 return -EINVAL;
850 return ibm->read(m);
853 static int dispatch_proc_open(struct inode *inode, struct file *file)
855 return single_open(file, dispatch_proc_show, PDE(inode)->data);
858 static ssize_t dispatch_proc_write(struct file *file,
859 const char __user *userbuf,
860 size_t count, loff_t *pos)
862 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
863 char *kernbuf;
864 int ret;
866 if (!ibm || !ibm->write)
867 return -EINVAL;
868 if (count > PAGE_SIZE - 2)
869 return -EINVAL;
871 kernbuf = kmalloc(count + 2, GFP_KERNEL);
872 if (!kernbuf)
873 return -ENOMEM;
875 if (copy_from_user(kernbuf, userbuf, count)) {
876 kfree(kernbuf);
877 return -EFAULT;
880 kernbuf[count] = 0;
881 strcat(kernbuf, ",");
882 ret = ibm->write(kernbuf);
883 if (ret == 0)
884 ret = count;
886 kfree(kernbuf);
888 return ret;
891 static const struct file_operations dispatch_proc_fops = {
892 .owner = THIS_MODULE,
893 .open = dispatch_proc_open,
894 .read = seq_read,
895 .llseek = seq_lseek,
896 .release = single_release,
897 .write = dispatch_proc_write,
900 static char *next_cmd(char **cmds)
902 char *start = *cmds;
903 char *end;
905 while ((end = strchr(start, ',')) && end == start)
906 start = end + 1;
908 if (!end)
909 return NULL;
911 *end = 0;
912 *cmds = end + 1;
913 return start;
917 /****************************************************************************
918 ****************************************************************************
920 * Device model: input, hwmon and platform
922 ****************************************************************************
923 ****************************************************************************/
925 static struct platform_device *tpacpi_pdev;
926 static struct platform_device *tpacpi_sensors_pdev;
927 static struct device *tpacpi_hwmon;
928 static struct input_dev *tpacpi_inputdev;
929 static struct mutex tpacpi_inputdev_send_mutex;
930 static LIST_HEAD(tpacpi_all_drivers);
932 static int tpacpi_suspend_handler(struct platform_device *pdev,
933 pm_message_t state)
935 struct ibm_struct *ibm, *itmp;
937 list_for_each_entry_safe(ibm, itmp,
938 &tpacpi_all_drivers,
939 all_drivers) {
940 if (ibm->suspend)
941 (ibm->suspend)(state);
944 return 0;
947 static int tpacpi_resume_handler(struct platform_device *pdev)
949 struct ibm_struct *ibm, *itmp;
951 list_for_each_entry_safe(ibm, itmp,
952 &tpacpi_all_drivers,
953 all_drivers) {
954 if (ibm->resume)
955 (ibm->resume)();
958 return 0;
961 static void tpacpi_shutdown_handler(struct platform_device *pdev)
963 struct ibm_struct *ibm, *itmp;
965 list_for_each_entry_safe(ibm, itmp,
966 &tpacpi_all_drivers,
967 all_drivers) {
968 if (ibm->shutdown)
969 (ibm->shutdown)();
973 static struct platform_driver tpacpi_pdriver = {
974 .driver = {
975 .name = TPACPI_DRVR_NAME,
976 .owner = THIS_MODULE,
978 .suspend = tpacpi_suspend_handler,
979 .resume = tpacpi_resume_handler,
980 .shutdown = tpacpi_shutdown_handler,
983 static struct platform_driver tpacpi_hwmon_pdriver = {
984 .driver = {
985 .name = TPACPI_HWMON_DRVR_NAME,
986 .owner = THIS_MODULE,
990 /*************************************************************************
991 * sysfs support helpers
994 struct attribute_set {
995 unsigned int members, max_members;
996 struct attribute_group group;
999 struct attribute_set_obj {
1000 struct attribute_set s;
1001 struct attribute *a;
1002 } __attribute__((packed));
1004 static struct attribute_set *create_attr_set(unsigned int max_members,
1005 const char *name)
1007 struct attribute_set_obj *sobj;
1009 if (max_members == 0)
1010 return NULL;
1012 /* Allocates space for implicit NULL at the end too */
1013 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1014 max_members * sizeof(struct attribute *),
1015 GFP_KERNEL);
1016 if (!sobj)
1017 return NULL;
1018 sobj->s.max_members = max_members;
1019 sobj->s.group.attrs = &sobj->a;
1020 sobj->s.group.name = name;
1022 return &sobj->s;
1025 #define destroy_attr_set(_set) \
1026 kfree(_set);
1028 /* not multi-threaded safe, use it in a single thread per set */
1029 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1031 if (!s || !attr)
1032 return -EINVAL;
1034 if (s->members >= s->max_members)
1035 return -ENOMEM;
1037 s->group.attrs[s->members] = attr;
1038 s->members++;
1040 return 0;
1043 static int add_many_to_attr_set(struct attribute_set *s,
1044 struct attribute **attr,
1045 unsigned int count)
1047 int i, res;
1049 for (i = 0; i < count; i++) {
1050 res = add_to_attr_set(s, attr[i]);
1051 if (res)
1052 return res;
1055 return 0;
1058 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1060 sysfs_remove_group(kobj, &s->group);
1061 destroy_attr_set(s);
1064 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1065 sysfs_create_group(_kobj, &_attr_set->group)
1067 static int parse_strtoul(const char *buf,
1068 unsigned long max, unsigned long *value)
1070 char *endp;
1072 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1073 endp = skip_spaces(endp);
1074 if (*endp || *value > max)
1075 return -EINVAL;
1077 return 0;
1080 static void tpacpi_disable_brightness_delay(void)
1082 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1083 printk(TPACPI_NOTICE
1084 "ACPI backlight control delay disabled\n");
1087 static void printk_deprecated_attribute(const char * const what,
1088 const char * const details)
1090 tpacpi_log_usertask("deprecated sysfs attribute");
1091 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1092 "will be removed. %s\n",
1093 what, details);
1096 /*************************************************************************
1097 * rfkill and radio control support helpers
1101 * ThinkPad-ACPI firmware handling model:
1103 * WLSW (master wireless switch) is event-driven, and is common to all
1104 * firmware-controlled radios. It cannot be controlled, just monitored,
1105 * as expected. It overrides all radio state in firmware
1107 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1108 * (TODO: verify how WLSW interacts with the returned radio state).
1110 * The only time there are shadow radio state changes, is when
1111 * masked-off hotkeys are used.
1115 * Internal driver API for radio state:
1117 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1118 * bool: true means radio blocked (off)
1120 enum tpacpi_rfkill_state {
1121 TPACPI_RFK_RADIO_OFF = 0,
1122 TPACPI_RFK_RADIO_ON
1125 /* rfkill switches */
1126 enum tpacpi_rfk_id {
1127 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1128 TPACPI_RFK_WWAN_SW_ID,
1129 TPACPI_RFK_UWB_SW_ID,
1130 TPACPI_RFK_SW_MAX
1133 static const char *tpacpi_rfkill_names[] = {
1134 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1135 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1136 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1137 [TPACPI_RFK_SW_MAX] = NULL
1140 /* ThinkPad-ACPI rfkill subdriver */
1141 struct tpacpi_rfk {
1142 struct rfkill *rfkill;
1143 enum tpacpi_rfk_id id;
1144 const struct tpacpi_rfk_ops *ops;
1147 struct tpacpi_rfk_ops {
1148 /* firmware interface */
1149 int (*get_status)(void);
1150 int (*set_status)(const enum tpacpi_rfkill_state);
1153 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1155 /* Query FW and update rfkill sw state for a given rfkill switch */
1156 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1158 int status;
1160 if (!tp_rfk)
1161 return -ENODEV;
1163 status = (tp_rfk->ops->get_status)();
1164 if (status < 0)
1165 return status;
1167 rfkill_set_sw_state(tp_rfk->rfkill,
1168 (status == TPACPI_RFK_RADIO_OFF));
1170 return status;
1173 /* Query FW and update rfkill sw state for all rfkill switches */
1174 static void tpacpi_rfk_update_swstate_all(void)
1176 unsigned int i;
1178 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1179 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1183 * Sync the HW-blocking state of all rfkill switches,
1184 * do notice it causes the rfkill core to schedule uevents
1186 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1188 unsigned int i;
1189 struct tpacpi_rfk *tp_rfk;
1191 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1192 tp_rfk = tpacpi_rfkill_switches[i];
1193 if (tp_rfk) {
1194 if (rfkill_set_hw_state(tp_rfk->rfkill,
1195 blocked)) {
1196 /* ignore -- we track sw block */
1202 /* Call to get the WLSW state from the firmware */
1203 static int hotkey_get_wlsw(void);
1205 /* Call to query WLSW state and update all rfkill switches */
1206 static bool tpacpi_rfk_check_hwblock_state(void)
1208 int res = hotkey_get_wlsw();
1209 int hw_blocked;
1211 /* When unknown or unsupported, we have to assume it is unblocked */
1212 if (res < 0)
1213 return false;
1215 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1216 tpacpi_rfk_update_hwblock_state(hw_blocked);
1218 return hw_blocked;
1221 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1223 struct tpacpi_rfk *tp_rfk = data;
1224 int res;
1226 dbg_printk(TPACPI_DBG_RFKILL,
1227 "request to change radio state to %s\n",
1228 blocked ? "blocked" : "unblocked");
1230 /* try to set radio state */
1231 res = (tp_rfk->ops->set_status)(blocked ?
1232 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1234 /* and update the rfkill core with whatever the FW really did */
1235 tpacpi_rfk_update_swstate(tp_rfk);
1237 return (res < 0) ? res : 0;
1240 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1241 .set_block = tpacpi_rfk_hook_set_block,
1244 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1245 const struct tpacpi_rfk_ops *tp_rfkops,
1246 const enum rfkill_type rfktype,
1247 const char *name,
1248 const bool set_default)
1250 struct tpacpi_rfk *atp_rfk;
1251 int res;
1252 bool sw_state = false;
1253 bool hw_state;
1254 int sw_status;
1256 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1258 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1259 if (atp_rfk)
1260 atp_rfk->rfkill = rfkill_alloc(name,
1261 &tpacpi_pdev->dev,
1262 rfktype,
1263 &tpacpi_rfk_rfkill_ops,
1264 atp_rfk);
1265 if (!atp_rfk || !atp_rfk->rfkill) {
1266 printk(TPACPI_ERR
1267 "failed to allocate memory for rfkill class\n");
1268 kfree(atp_rfk);
1269 return -ENOMEM;
1272 atp_rfk->id = id;
1273 atp_rfk->ops = tp_rfkops;
1275 sw_status = (tp_rfkops->get_status)();
1276 if (sw_status < 0) {
1277 printk(TPACPI_ERR
1278 "failed to read initial state for %s, error %d\n",
1279 name, sw_status);
1280 } else {
1281 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1282 if (set_default) {
1283 /* try to keep the initial state, since we ask the
1284 * firmware to preserve it across S5 in NVRAM */
1285 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1288 hw_state = tpacpi_rfk_check_hwblock_state();
1289 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1291 res = rfkill_register(atp_rfk->rfkill);
1292 if (res < 0) {
1293 printk(TPACPI_ERR
1294 "failed to register %s rfkill switch: %d\n",
1295 name, res);
1296 rfkill_destroy(atp_rfk->rfkill);
1297 kfree(atp_rfk);
1298 return res;
1301 tpacpi_rfkill_switches[id] = atp_rfk;
1303 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1304 name, (sw_state || hw_state) ? "" : "un");
1305 return 0;
1308 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1310 struct tpacpi_rfk *tp_rfk;
1312 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1314 tp_rfk = tpacpi_rfkill_switches[id];
1315 if (tp_rfk) {
1316 rfkill_unregister(tp_rfk->rfkill);
1317 rfkill_destroy(tp_rfk->rfkill);
1318 tpacpi_rfkill_switches[id] = NULL;
1319 kfree(tp_rfk);
1323 static void printk_deprecated_rfkill_attribute(const char * const what)
1325 printk_deprecated_attribute(what,
1326 "Please switch to generic rfkill before year 2010");
1329 /* sysfs <radio> enable ------------------------------------------------ */
1330 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1331 struct device_attribute *attr,
1332 char *buf)
1334 int status;
1336 printk_deprecated_rfkill_attribute(attr->attr.name);
1338 /* This is in the ABI... */
1339 if (tpacpi_rfk_check_hwblock_state()) {
1340 status = TPACPI_RFK_RADIO_OFF;
1341 } else {
1342 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1343 if (status < 0)
1344 return status;
1347 return snprintf(buf, PAGE_SIZE, "%d\n",
1348 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1351 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1352 struct device_attribute *attr,
1353 const char *buf, size_t count)
1355 unsigned long t;
1356 int res;
1358 printk_deprecated_rfkill_attribute(attr->attr.name);
1360 if (parse_strtoul(buf, 1, &t))
1361 return -EINVAL;
1363 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1365 /* This is in the ABI... */
1366 if (tpacpi_rfk_check_hwblock_state() && !!t)
1367 return -EPERM;
1369 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1370 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1371 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1373 return (res < 0) ? res : count;
1376 /* procfs -------------------------------------------------------------- */
1377 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1379 if (id >= TPACPI_RFK_SW_MAX)
1380 seq_printf(m, "status:\t\tnot supported\n");
1381 else {
1382 int status;
1384 /* This is in the ABI... */
1385 if (tpacpi_rfk_check_hwblock_state()) {
1386 status = TPACPI_RFK_RADIO_OFF;
1387 } else {
1388 status = tpacpi_rfk_update_swstate(
1389 tpacpi_rfkill_switches[id]);
1390 if (status < 0)
1391 return status;
1394 seq_printf(m, "status:\t\t%s\n",
1395 (status == TPACPI_RFK_RADIO_ON) ?
1396 "enabled" : "disabled");
1397 seq_printf(m, "commands:\tenable, disable\n");
1400 return 0;
1403 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1405 char *cmd;
1406 int status = -1;
1407 int res = 0;
1409 if (id >= TPACPI_RFK_SW_MAX)
1410 return -ENODEV;
1412 while ((cmd = next_cmd(&buf))) {
1413 if (strlencmp(cmd, "enable") == 0)
1414 status = TPACPI_RFK_RADIO_ON;
1415 else if (strlencmp(cmd, "disable") == 0)
1416 status = TPACPI_RFK_RADIO_OFF;
1417 else
1418 return -EINVAL;
1421 if (status != -1) {
1422 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1423 (status == TPACPI_RFK_RADIO_ON) ?
1424 "enable" : "disable",
1425 tpacpi_rfkill_names[id]);
1426 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1427 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1430 return res;
1433 /*************************************************************************
1434 * thinkpad-acpi driver attributes
1437 /* interface_version --------------------------------------------------- */
1438 static ssize_t tpacpi_driver_interface_version_show(
1439 struct device_driver *drv,
1440 char *buf)
1442 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1445 static DRIVER_ATTR(interface_version, S_IRUGO,
1446 tpacpi_driver_interface_version_show, NULL);
1448 /* debug_level --------------------------------------------------------- */
1449 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1450 char *buf)
1452 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1455 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1456 const char *buf, size_t count)
1458 unsigned long t;
1460 if (parse_strtoul(buf, 0xffff, &t))
1461 return -EINVAL;
1463 dbg_level = t;
1465 return count;
1468 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1469 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1471 /* version ------------------------------------------------------------- */
1472 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1473 char *buf)
1475 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1476 TPACPI_DESC, TPACPI_VERSION);
1479 static DRIVER_ATTR(version, S_IRUGO,
1480 tpacpi_driver_version_show, NULL);
1482 /* --------------------------------------------------------------------- */
1484 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1486 /* wlsw_emulstate ------------------------------------------------------ */
1487 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1488 char *buf)
1490 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1493 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1494 const char *buf, size_t count)
1496 unsigned long t;
1498 if (parse_strtoul(buf, 1, &t))
1499 return -EINVAL;
1501 if (tpacpi_wlsw_emulstate != !!t) {
1502 tpacpi_wlsw_emulstate = !!t;
1503 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1506 return count;
1509 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1510 tpacpi_driver_wlsw_emulstate_show,
1511 tpacpi_driver_wlsw_emulstate_store);
1513 /* bluetooth_emulstate ------------------------------------------------- */
1514 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1515 struct device_driver *drv,
1516 char *buf)
1518 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1521 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1522 struct device_driver *drv,
1523 const char *buf, size_t count)
1525 unsigned long t;
1527 if (parse_strtoul(buf, 1, &t))
1528 return -EINVAL;
1530 tpacpi_bluetooth_emulstate = !!t;
1532 return count;
1535 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1536 tpacpi_driver_bluetooth_emulstate_show,
1537 tpacpi_driver_bluetooth_emulstate_store);
1539 /* wwan_emulstate ------------------------------------------------- */
1540 static ssize_t tpacpi_driver_wwan_emulstate_show(
1541 struct device_driver *drv,
1542 char *buf)
1544 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1547 static ssize_t tpacpi_driver_wwan_emulstate_store(
1548 struct device_driver *drv,
1549 const char *buf, size_t count)
1551 unsigned long t;
1553 if (parse_strtoul(buf, 1, &t))
1554 return -EINVAL;
1556 tpacpi_wwan_emulstate = !!t;
1558 return count;
1561 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1562 tpacpi_driver_wwan_emulstate_show,
1563 tpacpi_driver_wwan_emulstate_store);
1565 /* uwb_emulstate ------------------------------------------------- */
1566 static ssize_t tpacpi_driver_uwb_emulstate_show(
1567 struct device_driver *drv,
1568 char *buf)
1570 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1573 static ssize_t tpacpi_driver_uwb_emulstate_store(
1574 struct device_driver *drv,
1575 const char *buf, size_t count)
1577 unsigned long t;
1579 if (parse_strtoul(buf, 1, &t))
1580 return -EINVAL;
1582 tpacpi_uwb_emulstate = !!t;
1584 return count;
1587 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1588 tpacpi_driver_uwb_emulstate_show,
1589 tpacpi_driver_uwb_emulstate_store);
1590 #endif
1592 /* --------------------------------------------------------------------- */
1594 static struct driver_attribute *tpacpi_driver_attributes[] = {
1595 &driver_attr_debug_level, &driver_attr_version,
1596 &driver_attr_interface_version,
1599 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1601 int i, res;
1603 i = 0;
1604 res = 0;
1605 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1606 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1607 i++;
1610 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1611 if (!res && dbg_wlswemul)
1612 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1613 if (!res && dbg_bluetoothemul)
1614 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1615 if (!res && dbg_wwanemul)
1616 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1617 if (!res && dbg_uwbemul)
1618 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1619 #endif
1621 return res;
1624 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1626 int i;
1628 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1629 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1631 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1632 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1633 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1634 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1635 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1636 #endif
1639 /*************************************************************************
1640 * Firmware Data
1644 * Table of recommended minimum BIOS versions
1646 * Reasons for listing:
1647 * 1. Stable BIOS, listed because the unknown amount of
1648 * bugs and bad ACPI behaviour on older versions
1650 * 2. BIOS or EC fw with known bugs that trigger on Linux
1652 * 3. BIOS with known reduced functionality in older versions
1654 * We recommend the latest BIOS and EC version.
1655 * We only support the latest BIOS and EC fw version as a rule.
1657 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1658 * Information from users in ThinkWiki
1660 * WARNING: we use this table also to detect that the machine is
1661 * a ThinkPad in some cases, so don't remove entries lightly.
1664 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1665 { .vendor = (__v), \
1666 .bios = TPID(__id1, __id2), \
1667 .ec = TPACPI_MATCH_ANY, \
1668 .quirks = TPACPI_MATCH_ANY << 16 \
1669 | (__bv1) << 8 | (__bv2) }
1671 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1672 __eid, __ev1, __ev2) \
1673 { .vendor = (__v), \
1674 .bios = TPID(__bid1, __bid2), \
1675 .ec = __eid, \
1676 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1677 | (__bv1) << 8 | (__bv2) }
1679 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1680 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1682 /* Outdated IBM BIOSes often lack the EC id string */
1683 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1684 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1685 __bv1, __bv2, TPID(__id1, __id2), \
1686 __ev1, __ev2), \
1687 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1688 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1689 __ev1, __ev2)
1691 /* Outdated IBM BIOSes often lack the EC id string */
1692 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1693 __eid1, __eid2, __ev1, __ev2) \
1694 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1695 __bv1, __bv2, TPID(__eid1, __eid2), \
1696 __ev1, __ev2), \
1697 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1698 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1699 __ev1, __ev2)
1701 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1702 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1704 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1705 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1706 __bv1, __bv2, TPID(__id1, __id2), \
1707 __ev1, __ev2)
1709 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1710 __eid1, __eid2, __ev1, __ev2) \
1711 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1712 __bv1, __bv2, TPID(__eid1, __eid2), \
1713 __ev1, __ev2)
1715 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1716 /* Numeric models ------------------ */
1717 /* FW MODEL BIOS VERS */
1718 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1719 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1720 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1721 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1722 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1723 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1724 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1725 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1726 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1728 /* A-series ------------------------- */
1729 /* FW MODEL BIOS VERS EC VERS */
1730 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1731 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1732 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1733 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1734 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1735 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1736 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1737 TPV_QI0('1', '3', '2', '0'), /* A22m */
1738 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1739 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1740 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1742 /* G-series ------------------------- */
1743 /* FW MODEL BIOS VERS */
1744 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1745 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1747 /* R-series, T-series --------------- */
1748 /* FW MODEL BIOS VERS EC VERS */
1749 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1750 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1751 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1752 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1753 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1754 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1755 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1756 T40/p, T41/p, T42/p (1) */
1757 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1758 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1759 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1760 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1762 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1763 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1764 TPV_QI0('1', '6', '3', '2'), /* T22 */
1765 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1766 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1767 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1769 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1770 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1771 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1773 /* BIOS FW BIOS VERS EC FW EC VERS */
1774 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1775 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1777 /* X-series ------------------------- */
1778 /* FW MODEL BIOS VERS EC VERS */
1779 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1780 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1781 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1782 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1783 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1784 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1785 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1787 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1788 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1790 /* (0) - older versions lack DMI EC fw string and functionality */
1791 /* (1) - older versions known to lack functionality */
1794 #undef TPV_QL1
1795 #undef TPV_QL0
1796 #undef TPV_QI2
1797 #undef TPV_QI1
1798 #undef TPV_QI0
1799 #undef TPV_Q_X
1800 #undef TPV_Q
1802 static void __init tpacpi_check_outdated_fw(void)
1804 unsigned long fwvers;
1805 u16 ec_version, bios_version;
1807 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1808 ARRAY_SIZE(tpacpi_bios_version_qtable));
1810 if (!fwvers)
1811 return;
1813 bios_version = fwvers & 0xffffU;
1814 ec_version = (fwvers >> 16) & 0xffffU;
1816 /* note that unknown versions are set to 0x0000 and we use that */
1817 if ((bios_version > thinkpad_id.bios_release) ||
1818 (ec_version > thinkpad_id.ec_release &&
1819 ec_version != TPACPI_MATCH_ANY)) {
1821 * The changelogs would let us track down the exact
1822 * reason, but it is just too much of a pain to track
1823 * it. We only list BIOSes that are either really
1824 * broken, or really stable to begin with, so it is
1825 * best if the user upgrades the firmware anyway.
1827 printk(TPACPI_WARN
1828 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1829 printk(TPACPI_WARN
1830 "WARNING: This firmware may be missing critical bug "
1831 "fixes and/or important features\n");
1835 static bool __init tpacpi_is_fw_known(void)
1837 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1838 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1841 /****************************************************************************
1842 ****************************************************************************
1844 * Subdrivers
1846 ****************************************************************************
1847 ****************************************************************************/
1849 /*************************************************************************
1850 * thinkpad-acpi metadata subdriver
1853 static int thinkpad_acpi_driver_read(struct seq_file *m)
1855 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1856 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1857 return 0;
1860 static struct ibm_struct thinkpad_acpi_driver_data = {
1861 .name = "driver",
1862 .read = thinkpad_acpi_driver_read,
1865 /*************************************************************************
1866 * Hotkey subdriver
1870 * ThinkPad firmware event model
1872 * The ThinkPad firmware has two main event interfaces: normal ACPI
1873 * notifications (which follow the ACPI standard), and a private event
1874 * interface.
1876 * The private event interface also issues events for the hotkeys. As
1877 * the driver gained features, the event handling code ended up being
1878 * built around the hotkey subdriver. This will need to be refactored
1879 * to a more formal event API eventually.
1881 * Some "hotkeys" are actually supposed to be used as event reports,
1882 * such as "brightness has changed", "volume has changed", depending on
1883 * the ThinkPad model and how the firmware is operating.
1885 * Unlike other classes, hotkey-class events have mask/unmask control on
1886 * non-ancient firmware. However, how it behaves changes a lot with the
1887 * firmware model and version.
1890 enum { /* hot key scan codes (derived from ACPI DSDT) */
1891 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1892 TP_ACPI_HOTKEYSCAN_FNF2,
1893 TP_ACPI_HOTKEYSCAN_FNF3,
1894 TP_ACPI_HOTKEYSCAN_FNF4,
1895 TP_ACPI_HOTKEYSCAN_FNF5,
1896 TP_ACPI_HOTKEYSCAN_FNF6,
1897 TP_ACPI_HOTKEYSCAN_FNF7,
1898 TP_ACPI_HOTKEYSCAN_FNF8,
1899 TP_ACPI_HOTKEYSCAN_FNF9,
1900 TP_ACPI_HOTKEYSCAN_FNF10,
1901 TP_ACPI_HOTKEYSCAN_FNF11,
1902 TP_ACPI_HOTKEYSCAN_FNF12,
1903 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1904 TP_ACPI_HOTKEYSCAN_FNINSERT,
1905 TP_ACPI_HOTKEYSCAN_FNDELETE,
1906 TP_ACPI_HOTKEYSCAN_FNHOME,
1907 TP_ACPI_HOTKEYSCAN_FNEND,
1908 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1909 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1910 TP_ACPI_HOTKEYSCAN_FNSPACE,
1911 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1912 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1913 TP_ACPI_HOTKEYSCAN_MUTE,
1914 TP_ACPI_HOTKEYSCAN_THINKPAD,
1917 enum { /* Keys/events available through NVRAM polling */
1918 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1919 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1922 enum { /* Positions of some of the keys in hotkey masks */
1923 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1924 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1925 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1926 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1927 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1928 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1929 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1930 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1931 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1932 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1933 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1936 enum { /* NVRAM to ACPI HKEY group map */
1937 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1938 TP_ACPI_HKEY_ZOOM_MASK |
1939 TP_ACPI_HKEY_DISPSWTCH_MASK |
1940 TP_ACPI_HKEY_HIBERNATE_MASK,
1941 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1942 TP_ACPI_HKEY_BRGHTDWN_MASK,
1943 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1944 TP_ACPI_HKEY_VOLDWN_MASK |
1945 TP_ACPI_HKEY_MUTE_MASK,
1948 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1949 struct tp_nvram_state {
1950 u16 thinkpad_toggle:1;
1951 u16 zoom_toggle:1;
1952 u16 display_toggle:1;
1953 u16 thinklight_toggle:1;
1954 u16 hibernate_toggle:1;
1955 u16 displayexp_toggle:1;
1956 u16 display_state:1;
1957 u16 brightness_toggle:1;
1958 u16 volume_toggle:1;
1959 u16 mute:1;
1961 u8 brightness_level;
1962 u8 volume_level;
1965 /* kthread for the hotkey poller */
1966 static struct task_struct *tpacpi_hotkey_task;
1968 /* Acquired while the poller kthread is running, use to sync start/stop */
1969 static struct mutex hotkey_thread_mutex;
1972 * Acquire mutex to write poller control variables as an
1973 * atomic block.
1975 * Increment hotkey_config_change when changing them if you
1976 * want the kthread to forget old state.
1978 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1980 static struct mutex hotkey_thread_data_mutex;
1981 static unsigned int hotkey_config_change;
1984 * hotkey poller control variables
1986 * Must be atomic or readers will also need to acquire mutex
1988 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1989 * should be used only when the changes need to be taken as
1990 * a block, OR when one needs to force the kthread to forget
1991 * old state.
1993 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1994 static unsigned int hotkey_poll_freq = 10; /* Hz */
1996 #define HOTKEY_CONFIG_CRITICAL_START \
1997 do { \
1998 mutex_lock(&hotkey_thread_data_mutex); \
1999 hotkey_config_change++; \
2000 } while (0);
2001 #define HOTKEY_CONFIG_CRITICAL_END \
2002 mutex_unlock(&hotkey_thread_data_mutex);
2004 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2006 #define hotkey_source_mask 0U
2007 #define HOTKEY_CONFIG_CRITICAL_START
2008 #define HOTKEY_CONFIG_CRITICAL_END
2010 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2012 static struct mutex hotkey_mutex;
2014 static enum { /* Reasons for waking up */
2015 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2016 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2017 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2018 } hotkey_wakeup_reason;
2020 static int hotkey_autosleep_ack;
2022 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2023 static u32 hotkey_all_mask; /* all events supported in fw */
2024 static u32 hotkey_reserved_mask; /* events better left disabled */
2025 static u32 hotkey_driver_mask; /* events needed by the driver */
2026 static u32 hotkey_user_mask; /* events visible to userspace */
2027 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2029 static unsigned int hotkey_report_mode;
2031 static u16 *hotkey_keycode_map;
2033 static struct attribute_set *hotkey_dev_attributes;
2035 static void tpacpi_driver_event(const unsigned int hkey_event);
2036 static void hotkey_driver_event(const unsigned int scancode);
2037 static void hotkey_poll_setup(const bool may_warn);
2039 /* HKEY.MHKG() return bits */
2040 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2042 static int hotkey_get_wlsw(void)
2044 int status;
2046 if (!tp_features.hotkey_wlsw)
2047 return -ENODEV;
2049 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2050 if (dbg_wlswemul)
2051 return (tpacpi_wlsw_emulstate) ?
2052 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2053 #endif
2055 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2056 return -EIO;
2058 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2061 static int hotkey_get_tablet_mode(int *status)
2063 int s;
2065 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2066 return -EIO;
2068 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2069 return 0;
2073 * Reads current event mask from firmware, and updates
2074 * hotkey_acpi_mask accordingly. Also resets any bits
2075 * from hotkey_user_mask that are unavailable to be
2076 * delivered (shadow requirement of the userspace ABI).
2078 * Call with hotkey_mutex held
2080 static int hotkey_mask_get(void)
2082 if (tp_features.hotkey_mask) {
2083 u32 m = 0;
2085 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2086 return -EIO;
2088 hotkey_acpi_mask = m;
2089 } else {
2090 /* no mask support doesn't mean no event support... */
2091 hotkey_acpi_mask = hotkey_all_mask;
2094 /* sync userspace-visible mask */
2095 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2097 return 0;
2100 void static hotkey_mask_warn_incomplete_mask(void)
2102 /* log only what the user can fix... */
2103 const u32 wantedmask = hotkey_driver_mask &
2104 ~(hotkey_acpi_mask | hotkey_source_mask) &
2105 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2107 if (wantedmask)
2108 printk(TPACPI_NOTICE
2109 "required events 0x%08x not enabled!\n",
2110 wantedmask);
2114 * Set the firmware mask when supported
2116 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2118 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2120 * Call with hotkey_mutex held
2122 static int hotkey_mask_set(u32 mask)
2124 int i;
2125 int rc = 0;
2127 const u32 fwmask = mask & ~hotkey_source_mask;
2129 if (tp_features.hotkey_mask) {
2130 for (i = 0; i < 32; i++) {
2131 if (!acpi_evalf(hkey_handle,
2132 NULL, "MHKM", "vdd", i + 1,
2133 !!(mask & (1 << i)))) {
2134 rc = -EIO;
2135 break;
2141 * We *must* make an inconditional call to hotkey_mask_get to
2142 * refresh hotkey_acpi_mask and update hotkey_user_mask
2144 * Take the opportunity to also log when we cannot _enable_
2145 * a given event.
2147 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2148 printk(TPACPI_NOTICE
2149 "asked for hotkey mask 0x%08x, but "
2150 "firmware forced it to 0x%08x\n",
2151 fwmask, hotkey_acpi_mask);
2154 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2155 hotkey_mask_warn_incomplete_mask();
2157 return rc;
2161 * Sets hotkey_user_mask and tries to set the firmware mask
2163 * Call with hotkey_mutex held
2165 static int hotkey_user_mask_set(const u32 mask)
2167 int rc;
2169 /* Give people a chance to notice they are doing something that
2170 * is bound to go boom on their users sooner or later */
2171 if (!tp_warned.hotkey_mask_ff &&
2172 (mask == 0xffff || mask == 0xffffff ||
2173 mask == 0xffffffff)) {
2174 tp_warned.hotkey_mask_ff = 1;
2175 printk(TPACPI_NOTICE
2176 "setting the hotkey mask to 0x%08x is likely "
2177 "not the best way to go about it\n", mask);
2178 printk(TPACPI_NOTICE
2179 "please consider using the driver defaults, "
2180 "and refer to up-to-date thinkpad-acpi "
2181 "documentation\n");
2184 /* Try to enable what the user asked for, plus whatever we need.
2185 * this syncs everything but won't enable bits in hotkey_user_mask */
2186 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2188 /* Enable the available bits in hotkey_user_mask */
2189 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2191 return rc;
2195 * Sets the driver hotkey mask.
2197 * Can be called even if the hotkey subdriver is inactive
2199 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2201 int rc;
2203 /* Do the right thing if hotkey_init has not been called yet */
2204 if (!tp_features.hotkey) {
2205 hotkey_driver_mask = mask;
2206 return 0;
2209 mutex_lock(&hotkey_mutex);
2211 HOTKEY_CONFIG_CRITICAL_START
2212 hotkey_driver_mask = mask;
2213 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2214 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2215 #endif
2216 HOTKEY_CONFIG_CRITICAL_END
2218 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2219 ~hotkey_source_mask);
2220 hotkey_poll_setup(true);
2222 mutex_unlock(&hotkey_mutex);
2224 return rc;
2227 static int hotkey_status_get(int *status)
2229 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2230 return -EIO;
2232 return 0;
2235 static int hotkey_status_set(bool enable)
2237 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2238 return -EIO;
2240 return 0;
2243 static void tpacpi_input_send_tabletsw(void)
2245 int state;
2247 if (tp_features.hotkey_tablet &&
2248 !hotkey_get_tablet_mode(&state)) {
2249 mutex_lock(&tpacpi_inputdev_send_mutex);
2251 input_report_switch(tpacpi_inputdev,
2252 SW_TABLET_MODE, !!state);
2253 input_sync(tpacpi_inputdev);
2255 mutex_unlock(&tpacpi_inputdev_send_mutex);
2259 /* Do NOT call without validating scancode first */
2260 static void tpacpi_input_send_key(const unsigned int scancode)
2262 const unsigned int keycode = hotkey_keycode_map[scancode];
2264 if (keycode != KEY_RESERVED) {
2265 mutex_lock(&tpacpi_inputdev_send_mutex);
2267 input_report_key(tpacpi_inputdev, keycode, 1);
2268 if (keycode == KEY_UNKNOWN)
2269 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2270 scancode);
2271 input_sync(tpacpi_inputdev);
2273 input_report_key(tpacpi_inputdev, keycode, 0);
2274 if (keycode == KEY_UNKNOWN)
2275 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2276 scancode);
2277 input_sync(tpacpi_inputdev);
2279 mutex_unlock(&tpacpi_inputdev_send_mutex);
2283 /* Do NOT call without validating scancode first */
2284 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2286 hotkey_driver_event(scancode);
2287 if (hotkey_user_mask & (1 << scancode))
2288 tpacpi_input_send_key(scancode);
2291 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2292 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2294 /* Do NOT call without validating scancode first */
2295 static void tpacpi_hotkey_send_key(unsigned int scancode)
2297 tpacpi_input_send_key_masked(scancode);
2298 if (hotkey_report_mode < 2) {
2299 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2300 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2304 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2306 u8 d;
2308 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2309 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2310 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2311 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2312 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2313 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2315 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2316 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2317 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2319 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2320 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2321 n->displayexp_toggle =
2322 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2324 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2325 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2326 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2327 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2328 n->brightness_toggle =
2329 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2331 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2332 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2333 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2334 >> TP_NVRAM_POS_LEVEL_VOLUME;
2335 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2336 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2340 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2341 struct tp_nvram_state *newn,
2342 const u32 event_mask)
2345 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2346 do { \
2347 if ((event_mask & (1 << __scancode)) && \
2348 oldn->__member != newn->__member) \
2349 tpacpi_hotkey_send_key(__scancode); \
2350 } while (0)
2352 #define TPACPI_MAY_SEND_KEY(__scancode) \
2353 do { \
2354 if (event_mask & (1 << __scancode)) \
2355 tpacpi_hotkey_send_key(__scancode); \
2356 } while (0)
2358 void issue_volchange(const unsigned int oldvol,
2359 const unsigned int newvol)
2361 unsigned int i = oldvol;
2363 while (i > newvol) {
2364 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2365 i--;
2367 while (i < newvol) {
2368 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2369 i++;
2373 void issue_brightnesschange(const unsigned int oldbrt,
2374 const unsigned int newbrt)
2376 unsigned int i = oldbrt;
2378 while (i > newbrt) {
2379 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2380 i--;
2382 while (i < newbrt) {
2383 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2384 i++;
2388 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2389 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2390 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2391 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2393 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2395 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2398 * Handle volume
2400 * This code is supposed to duplicate the IBM firmware behaviour:
2401 * - Pressing MUTE issues mute hotkey message, even when already mute
2402 * - Pressing Volume up/down issues volume up/down hotkey messages,
2403 * even when already at maximum or minumum volume
2404 * - The act of unmuting issues volume up/down notification,
2405 * depending which key was used to unmute
2407 * We are constrained to what the NVRAM can tell us, which is not much
2408 * and certainly not enough if more than one volume hotkey was pressed
2409 * since the last poll cycle.
2411 * Just to make our life interesting, some newer Lenovo ThinkPads have
2412 * bugs in the BIOS and may fail to update volume_toggle properly.
2414 if (newn->mute) {
2415 /* muted */
2416 if (!oldn->mute ||
2417 oldn->volume_toggle != newn->volume_toggle ||
2418 oldn->volume_level != newn->volume_level) {
2419 /* recently muted, or repeated mute keypress, or
2420 * multiple presses ending in mute */
2421 issue_volchange(oldn->volume_level, newn->volume_level);
2422 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2424 } else {
2425 /* unmute */
2426 if (oldn->mute) {
2427 /* recently unmuted, issue 'unmute' keypress */
2428 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2430 if (oldn->volume_level != newn->volume_level) {
2431 issue_volchange(oldn->volume_level, newn->volume_level);
2432 } else if (oldn->volume_toggle != newn->volume_toggle) {
2433 /* repeated vol up/down keypress at end of scale ? */
2434 if (newn->volume_level == 0)
2435 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2436 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2437 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2441 /* handle brightness */
2442 if (oldn->brightness_level != newn->brightness_level) {
2443 issue_brightnesschange(oldn->brightness_level,
2444 newn->brightness_level);
2445 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2446 /* repeated key presses that didn't change state */
2447 if (newn->brightness_level == 0)
2448 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2449 else if (newn->brightness_level >= bright_maxlvl
2450 && !tp_features.bright_unkfw)
2451 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2454 #undef TPACPI_COMPARE_KEY
2455 #undef TPACPI_MAY_SEND_KEY
2459 * Polling driver
2461 * We track all events in hotkey_source_mask all the time, since
2462 * most of them are edge-based. We only issue those requested by
2463 * hotkey_user_mask or hotkey_driver_mask, though.
2465 static int hotkey_kthread(void *data)
2467 struct tp_nvram_state s[2];
2468 u32 poll_mask, event_mask;
2469 unsigned int si, so;
2470 unsigned long t;
2471 unsigned int change_detector, must_reset;
2472 unsigned int poll_freq;
2474 mutex_lock(&hotkey_thread_mutex);
2476 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2477 goto exit;
2479 set_freezable();
2481 so = 0;
2482 si = 1;
2483 t = 0;
2485 /* Initial state for compares */
2486 mutex_lock(&hotkey_thread_data_mutex);
2487 change_detector = hotkey_config_change;
2488 poll_mask = hotkey_source_mask;
2489 event_mask = hotkey_source_mask &
2490 (hotkey_driver_mask | hotkey_user_mask);
2491 poll_freq = hotkey_poll_freq;
2492 mutex_unlock(&hotkey_thread_data_mutex);
2493 hotkey_read_nvram(&s[so], poll_mask);
2495 while (!kthread_should_stop()) {
2496 if (t == 0) {
2497 if (likely(poll_freq))
2498 t = 1000/poll_freq;
2499 else
2500 t = 100; /* should never happen... */
2502 t = msleep_interruptible(t);
2503 if (unlikely(kthread_should_stop()))
2504 break;
2505 must_reset = try_to_freeze();
2506 if (t > 0 && !must_reset)
2507 continue;
2509 mutex_lock(&hotkey_thread_data_mutex);
2510 if (must_reset || hotkey_config_change != change_detector) {
2511 /* forget old state on thaw or config change */
2512 si = so;
2513 t = 0;
2514 change_detector = hotkey_config_change;
2516 poll_mask = hotkey_source_mask;
2517 event_mask = hotkey_source_mask &
2518 (hotkey_driver_mask | hotkey_user_mask);
2519 poll_freq = hotkey_poll_freq;
2520 mutex_unlock(&hotkey_thread_data_mutex);
2522 if (likely(poll_mask)) {
2523 hotkey_read_nvram(&s[si], poll_mask);
2524 if (likely(si != so)) {
2525 hotkey_compare_and_issue_event(&s[so], &s[si],
2526 event_mask);
2530 so = si;
2531 si ^= 1;
2534 exit:
2535 mutex_unlock(&hotkey_thread_mutex);
2536 return 0;
2539 /* call with hotkey_mutex held */
2540 static void hotkey_poll_stop_sync(void)
2542 if (tpacpi_hotkey_task) {
2543 if (frozen(tpacpi_hotkey_task) ||
2544 freezing(tpacpi_hotkey_task))
2545 thaw_process(tpacpi_hotkey_task);
2547 kthread_stop(tpacpi_hotkey_task);
2548 tpacpi_hotkey_task = NULL;
2549 mutex_lock(&hotkey_thread_mutex);
2550 /* at this point, the thread did exit */
2551 mutex_unlock(&hotkey_thread_mutex);
2555 /* call with hotkey_mutex held */
2556 static void hotkey_poll_setup(const bool may_warn)
2558 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2559 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2561 if (hotkey_poll_freq > 0 &&
2562 (poll_driver_mask ||
2563 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2564 if (!tpacpi_hotkey_task) {
2565 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2566 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2567 if (IS_ERR(tpacpi_hotkey_task)) {
2568 tpacpi_hotkey_task = NULL;
2569 printk(TPACPI_ERR
2570 "could not create kernel thread "
2571 "for hotkey polling\n");
2574 } else {
2575 hotkey_poll_stop_sync();
2576 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2577 hotkey_poll_freq == 0) {
2578 printk(TPACPI_NOTICE
2579 "hot keys 0x%08x and/or events 0x%08x "
2580 "require polling, which is currently "
2581 "disabled\n",
2582 poll_user_mask, poll_driver_mask);
2587 static void hotkey_poll_setup_safe(const bool may_warn)
2589 mutex_lock(&hotkey_mutex);
2590 hotkey_poll_setup(may_warn);
2591 mutex_unlock(&hotkey_mutex);
2594 /* call with hotkey_mutex held */
2595 static void hotkey_poll_set_freq(unsigned int freq)
2597 if (!freq)
2598 hotkey_poll_stop_sync();
2600 hotkey_poll_freq = freq;
2603 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2605 static void hotkey_poll_setup(const bool __unused)
2609 static void hotkey_poll_setup_safe(const bool __unused)
2613 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2615 static int hotkey_inputdev_open(struct input_dev *dev)
2617 switch (tpacpi_lifecycle) {
2618 case TPACPI_LIFE_INIT:
2619 case TPACPI_LIFE_RUNNING:
2620 hotkey_poll_setup_safe(false);
2621 return 0;
2622 case TPACPI_LIFE_EXITING:
2623 return -EBUSY;
2626 /* Should only happen if tpacpi_lifecycle is corrupt */
2627 BUG();
2628 return -EBUSY;
2631 static void hotkey_inputdev_close(struct input_dev *dev)
2633 /* disable hotkey polling when possible */
2634 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2635 !(hotkey_source_mask & hotkey_driver_mask))
2636 hotkey_poll_setup_safe(false);
2639 /* sysfs hotkey enable ------------------------------------------------- */
2640 static ssize_t hotkey_enable_show(struct device *dev,
2641 struct device_attribute *attr,
2642 char *buf)
2644 int res, status;
2646 printk_deprecated_attribute("hotkey_enable",
2647 "Hotkey reporting is always enabled");
2649 res = hotkey_status_get(&status);
2650 if (res)
2651 return res;
2653 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2656 static ssize_t hotkey_enable_store(struct device *dev,
2657 struct device_attribute *attr,
2658 const char *buf, size_t count)
2660 unsigned long t;
2662 printk_deprecated_attribute("hotkey_enable",
2663 "Hotkeys can be disabled through hotkey_mask");
2665 if (parse_strtoul(buf, 1, &t))
2666 return -EINVAL;
2668 if (t == 0)
2669 return -EPERM;
2671 return count;
2674 static struct device_attribute dev_attr_hotkey_enable =
2675 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2676 hotkey_enable_show, hotkey_enable_store);
2678 /* sysfs hotkey mask --------------------------------------------------- */
2679 static ssize_t hotkey_mask_show(struct device *dev,
2680 struct device_attribute *attr,
2681 char *buf)
2683 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2686 static ssize_t hotkey_mask_store(struct device *dev,
2687 struct device_attribute *attr,
2688 const char *buf, size_t count)
2690 unsigned long t;
2691 int res;
2693 if (parse_strtoul(buf, 0xffffffffUL, &t))
2694 return -EINVAL;
2696 if (mutex_lock_killable(&hotkey_mutex))
2697 return -ERESTARTSYS;
2699 res = hotkey_user_mask_set(t);
2701 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2702 hotkey_poll_setup(true);
2703 #endif
2705 mutex_unlock(&hotkey_mutex);
2707 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2709 return (res) ? res : count;
2712 static struct device_attribute dev_attr_hotkey_mask =
2713 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2714 hotkey_mask_show, hotkey_mask_store);
2716 /* sysfs hotkey bios_enabled ------------------------------------------- */
2717 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2718 struct device_attribute *attr,
2719 char *buf)
2721 return sprintf(buf, "0\n");
2724 static struct device_attribute dev_attr_hotkey_bios_enabled =
2725 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2727 /* sysfs hotkey bios_mask ---------------------------------------------- */
2728 static ssize_t hotkey_bios_mask_show(struct device *dev,
2729 struct device_attribute *attr,
2730 char *buf)
2732 printk_deprecated_attribute("hotkey_bios_mask",
2733 "This attribute is useless.");
2734 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2737 static struct device_attribute dev_attr_hotkey_bios_mask =
2738 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2740 /* sysfs hotkey all_mask ----------------------------------------------- */
2741 static ssize_t hotkey_all_mask_show(struct device *dev,
2742 struct device_attribute *attr,
2743 char *buf)
2745 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2746 hotkey_all_mask | hotkey_source_mask);
2749 static struct device_attribute dev_attr_hotkey_all_mask =
2750 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2752 /* sysfs hotkey recommended_mask --------------------------------------- */
2753 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2754 struct device_attribute *attr,
2755 char *buf)
2757 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2758 (hotkey_all_mask | hotkey_source_mask)
2759 & ~hotkey_reserved_mask);
2762 static struct device_attribute dev_attr_hotkey_recommended_mask =
2763 __ATTR(hotkey_recommended_mask, S_IRUGO,
2764 hotkey_recommended_mask_show, NULL);
2766 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2768 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2769 static ssize_t hotkey_source_mask_show(struct device *dev,
2770 struct device_attribute *attr,
2771 char *buf)
2773 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2776 static ssize_t hotkey_source_mask_store(struct device *dev,
2777 struct device_attribute *attr,
2778 const char *buf, size_t count)
2780 unsigned long t;
2781 u32 r_ev;
2782 int rc;
2784 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2785 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2786 return -EINVAL;
2788 if (mutex_lock_killable(&hotkey_mutex))
2789 return -ERESTARTSYS;
2791 HOTKEY_CONFIG_CRITICAL_START
2792 hotkey_source_mask = t;
2793 HOTKEY_CONFIG_CRITICAL_END
2795 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2796 ~hotkey_source_mask);
2797 hotkey_poll_setup(true);
2799 /* check if events needed by the driver got disabled */
2800 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2801 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2803 mutex_unlock(&hotkey_mutex);
2805 if (rc < 0)
2806 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2807 "firmware event mask!\n");
2809 if (r_ev)
2810 printk(TPACPI_NOTICE "hotkey_source_mask: "
2811 "some important events were disabled: "
2812 "0x%04x\n", r_ev);
2814 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2816 return (rc < 0) ? rc : count;
2819 static struct device_attribute dev_attr_hotkey_source_mask =
2820 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2821 hotkey_source_mask_show, hotkey_source_mask_store);
2823 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2824 static ssize_t hotkey_poll_freq_show(struct device *dev,
2825 struct device_attribute *attr,
2826 char *buf)
2828 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2831 static ssize_t hotkey_poll_freq_store(struct device *dev,
2832 struct device_attribute *attr,
2833 const char *buf, size_t count)
2835 unsigned long t;
2837 if (parse_strtoul(buf, 25, &t))
2838 return -EINVAL;
2840 if (mutex_lock_killable(&hotkey_mutex))
2841 return -ERESTARTSYS;
2843 hotkey_poll_set_freq(t);
2844 hotkey_poll_setup(true);
2846 mutex_unlock(&hotkey_mutex);
2848 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2850 return count;
2853 static struct device_attribute dev_attr_hotkey_poll_freq =
2854 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2855 hotkey_poll_freq_show, hotkey_poll_freq_store);
2857 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2859 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2860 static ssize_t hotkey_radio_sw_show(struct device *dev,
2861 struct device_attribute *attr,
2862 char *buf)
2864 int res;
2865 res = hotkey_get_wlsw();
2866 if (res < 0)
2867 return res;
2869 /* Opportunistic update */
2870 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2872 return snprintf(buf, PAGE_SIZE, "%d\n",
2873 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2876 static struct device_attribute dev_attr_hotkey_radio_sw =
2877 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2879 static void hotkey_radio_sw_notify_change(void)
2881 if (tp_features.hotkey_wlsw)
2882 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2883 "hotkey_radio_sw");
2886 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2887 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2888 struct device_attribute *attr,
2889 char *buf)
2891 int res, s;
2892 res = hotkey_get_tablet_mode(&s);
2893 if (res < 0)
2894 return res;
2896 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2899 static struct device_attribute dev_attr_hotkey_tablet_mode =
2900 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2902 static void hotkey_tablet_mode_notify_change(void)
2904 if (tp_features.hotkey_tablet)
2905 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2906 "hotkey_tablet_mode");
2909 /* sysfs hotkey report_mode -------------------------------------------- */
2910 static ssize_t hotkey_report_mode_show(struct device *dev,
2911 struct device_attribute *attr,
2912 char *buf)
2914 return snprintf(buf, PAGE_SIZE, "%d\n",
2915 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2918 static struct device_attribute dev_attr_hotkey_report_mode =
2919 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2921 /* sysfs wakeup reason (pollable) -------------------------------------- */
2922 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2923 struct device_attribute *attr,
2924 char *buf)
2926 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2929 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2930 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2932 static void hotkey_wakeup_reason_notify_change(void)
2934 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2935 "wakeup_reason");
2938 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2939 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2940 struct device_attribute *attr,
2941 char *buf)
2943 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2946 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2947 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2948 hotkey_wakeup_hotunplug_complete_show, NULL);
2950 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2952 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2953 "wakeup_hotunplug_complete");
2956 /* --------------------------------------------------------------------- */
2958 static struct attribute *hotkey_attributes[] __initdata = {
2959 &dev_attr_hotkey_enable.attr,
2960 &dev_attr_hotkey_bios_enabled.attr,
2961 &dev_attr_hotkey_bios_mask.attr,
2962 &dev_attr_hotkey_report_mode.attr,
2963 &dev_attr_hotkey_wakeup_reason.attr,
2964 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2965 &dev_attr_hotkey_mask.attr,
2966 &dev_attr_hotkey_all_mask.attr,
2967 &dev_attr_hotkey_recommended_mask.attr,
2968 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2969 &dev_attr_hotkey_source_mask.attr,
2970 &dev_attr_hotkey_poll_freq.attr,
2971 #endif
2975 * Sync both the hw and sw blocking state of all switches
2977 static void tpacpi_send_radiosw_update(void)
2979 int wlsw;
2982 * We must sync all rfkill controllers *before* issuing any
2983 * rfkill input events, or we will race the rfkill core input
2984 * handler.
2986 * tpacpi_inputdev_send_mutex works as a syncronization point
2987 * for the above.
2989 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2992 wlsw = hotkey_get_wlsw();
2994 /* Sync hw blocking state first if it is hw-blocked */
2995 if (wlsw == TPACPI_RFK_RADIO_OFF)
2996 tpacpi_rfk_update_hwblock_state(true);
2998 /* Sync sw blocking state */
2999 tpacpi_rfk_update_swstate_all();
3001 /* Sync hw blocking state last if it is hw-unblocked */
3002 if (wlsw == TPACPI_RFK_RADIO_ON)
3003 tpacpi_rfk_update_hwblock_state(false);
3005 /* Issue rfkill input event for WLSW switch */
3006 if (!(wlsw < 0)) {
3007 mutex_lock(&tpacpi_inputdev_send_mutex);
3009 input_report_switch(tpacpi_inputdev,
3010 SW_RFKILL_ALL, (wlsw > 0));
3011 input_sync(tpacpi_inputdev);
3013 mutex_unlock(&tpacpi_inputdev_send_mutex);
3017 * this can be unconditional, as we will poll state again
3018 * if userspace uses the notify to read data
3020 hotkey_radio_sw_notify_change();
3023 static void hotkey_exit(void)
3025 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3026 mutex_lock(&hotkey_mutex);
3027 hotkey_poll_stop_sync();
3028 mutex_unlock(&hotkey_mutex);
3029 #endif
3031 if (hotkey_dev_attributes)
3032 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3034 kfree(hotkey_keycode_map);
3036 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3037 "restoring original HKEY status and mask\n");
3038 /* yes, there is a bitwise or below, we want the
3039 * functions to be called even if one of them fail */
3040 if (((tp_features.hotkey_mask &&
3041 hotkey_mask_set(hotkey_orig_mask)) |
3042 hotkey_status_set(false)) != 0)
3043 printk(TPACPI_ERR
3044 "failed to restore hot key mask "
3045 "to BIOS defaults\n");
3048 static void __init hotkey_unmap(const unsigned int scancode)
3050 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3051 clear_bit(hotkey_keycode_map[scancode],
3052 tpacpi_inputdev->keybit);
3053 hotkey_keycode_map[scancode] = KEY_RESERVED;
3058 * HKEY quirks:
3059 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3062 #define TPACPI_HK_Q_INIMASK 0x0001
3064 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3065 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3066 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3067 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3068 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3069 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3070 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3071 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3072 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3073 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3074 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3075 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3076 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3077 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3078 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3079 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3080 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3081 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3082 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3083 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3086 static int __init hotkey_init(struct ibm_init_struct *iibm)
3088 /* Requirements for changing the default keymaps:
3090 * 1. Many of the keys are mapped to KEY_RESERVED for very
3091 * good reasons. Do not change them unless you have deep
3092 * knowledge on the IBM and Lenovo ThinkPad firmware for
3093 * the various ThinkPad models. The driver behaves
3094 * differently for KEY_RESERVED: such keys have their
3095 * hot key mask *unset* in mask_recommended, and also
3096 * in the initial hot key mask programmed into the
3097 * firmware at driver load time, which means the firm-
3098 * ware may react very differently if you change them to
3099 * something else;
3101 * 2. You must be subscribed to the linux-thinkpad and
3102 * ibm-acpi-devel mailing lists, and you should read the
3103 * list archives since 2007 if you want to change the
3104 * keymaps. This requirement exists so that you will
3105 * know the past history of problems with the thinkpad-
3106 * acpi driver keymaps, and also that you will be
3107 * listening to any bug reports;
3109 * 3. Do not send thinkpad-acpi specific patches directly to
3110 * for merging, *ever*. Send them to the linux-acpi
3111 * mailinglist for comments. Merging is to be done only
3112 * through acpi-test and the ACPI maintainer.
3114 * If the above is too much to ask, don't change the keymap.
3115 * Ask the thinkpad-acpi maintainer to do it, instead.
3117 static u16 ibm_keycode_map[] __initdata = {
3118 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3119 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3120 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3121 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3123 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3124 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3125 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3126 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3128 /* brightness: firmware always reacts to them */
3129 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3130 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3132 /* Thinklight: firmware always react to it */
3133 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3135 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3136 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3138 /* Volume: firmware always react to it and reprograms
3139 * the built-in *extra* mixer. Never map it to control
3140 * another mixer by default. */
3141 KEY_RESERVED, /* 0x14: VOLUME UP */
3142 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3143 KEY_RESERVED, /* 0x16: MUTE */
3145 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3147 /* (assignments unknown, please report if found) */
3148 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3149 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3151 static u16 lenovo_keycode_map[] __initdata = {
3152 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3153 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3154 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3155 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3157 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3158 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3159 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3160 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3162 /* These should be enabled --only-- when ACPI video
3163 * is disabled (i.e. in "vendor" mode), and are handled
3164 * in a special way by the init code */
3165 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3166 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3168 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3170 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3171 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3173 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3174 * react to it and reprograms the built-in *extra* mixer.
3175 * Never map it to control another mixer by default.
3177 * T60?, T61, R60?, R61: firmware and EC tries to send
3178 * these over the regular keyboard, so these are no-ops,
3179 * but there are still weird bugs re. MUTE, so do not
3180 * change unless you get test reports from all Lenovo
3181 * models. May cause the BIOS to interfere with the
3182 * HDA mixer.
3184 KEY_RESERVED, /* 0x14: VOLUME UP */
3185 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3186 KEY_RESERVED, /* 0x16: MUTE */
3188 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3190 /* (assignments unknown, please report if found) */
3191 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3192 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3195 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3196 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3197 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3199 int res, i;
3200 int status;
3201 int hkeyv;
3202 bool radiosw_state = false;
3203 bool tabletsw_state = false;
3205 unsigned long quirks;
3207 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3208 "initializing hotkey subdriver\n");
3210 BUG_ON(!tpacpi_inputdev);
3211 BUG_ON(tpacpi_inputdev->open != NULL ||
3212 tpacpi_inputdev->close != NULL);
3214 TPACPI_ACPIHANDLE_INIT(hkey);
3215 mutex_init(&hotkey_mutex);
3217 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3218 mutex_init(&hotkey_thread_mutex);
3219 mutex_init(&hotkey_thread_data_mutex);
3220 #endif
3222 /* hotkey not supported on 570 */
3223 tp_features.hotkey = hkey_handle != NULL;
3225 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3226 "hotkeys are %s\n",
3227 str_supported(tp_features.hotkey));
3229 if (!tp_features.hotkey)
3230 return 1;
3232 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3233 ARRAY_SIZE(tpacpi_hotkey_qtable));
3235 tpacpi_disable_brightness_delay();
3237 /* MUST have enough space for all attributes to be added to
3238 * hotkey_dev_attributes */
3239 hotkey_dev_attributes = create_attr_set(
3240 ARRAY_SIZE(hotkey_attributes) + 2,
3241 NULL);
3242 if (!hotkey_dev_attributes)
3243 return -ENOMEM;
3244 res = add_many_to_attr_set(hotkey_dev_attributes,
3245 hotkey_attributes,
3246 ARRAY_SIZE(hotkey_attributes));
3247 if (res)
3248 goto err_exit;
3250 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3251 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3252 for HKEY interface version 0x100 */
3253 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3254 if ((hkeyv >> 8) != 1) {
3255 printk(TPACPI_ERR "unknown version of the "
3256 "HKEY interface: 0x%x\n", hkeyv);
3257 printk(TPACPI_ERR "please report this to %s\n",
3258 TPACPI_MAIL);
3259 } else {
3261 * MHKV 0x100 in A31, R40, R40e,
3262 * T4x, X31, and later
3264 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3265 "firmware HKEY interface version: 0x%x\n",
3266 hkeyv);
3268 /* Paranoia check AND init hotkey_all_mask */
3269 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3270 "MHKA", "qd")) {
3271 printk(TPACPI_ERR
3272 "missing MHKA handler, "
3273 "please report this to %s\n",
3274 TPACPI_MAIL);
3275 /* Fallback: pre-init for FN+F3,F4,F12 */
3276 hotkey_all_mask = 0x080cU;
3277 } else {
3278 tp_features.hotkey_mask = 1;
3283 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3284 "hotkey masks are %s\n",
3285 str_supported(tp_features.hotkey_mask));
3287 /* Init hotkey_all_mask if not initialized yet */
3288 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3289 (quirks & TPACPI_HK_Q_INIMASK))
3290 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3292 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3293 if (tp_features.hotkey_mask) {
3294 /* hotkey_source_mask *must* be zero for
3295 * the first hotkey_mask_get to return hotkey_orig_mask */
3296 res = hotkey_mask_get();
3297 if (res)
3298 goto err_exit;
3300 hotkey_orig_mask = hotkey_acpi_mask;
3301 } else {
3302 hotkey_orig_mask = hotkey_all_mask;
3303 hotkey_acpi_mask = hotkey_all_mask;
3306 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3307 if (dbg_wlswemul) {
3308 tp_features.hotkey_wlsw = 1;
3309 radiosw_state = !!tpacpi_wlsw_emulstate;
3310 printk(TPACPI_INFO
3311 "radio switch emulation enabled\n");
3312 } else
3313 #endif
3314 /* Not all thinkpads have a hardware radio switch */
3315 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3316 tp_features.hotkey_wlsw = 1;
3317 radiosw_state = !!status;
3318 printk(TPACPI_INFO
3319 "radio switch found; radios are %s\n",
3320 enabled(status, 0));
3322 if (tp_features.hotkey_wlsw)
3323 res = add_to_attr_set(hotkey_dev_attributes,
3324 &dev_attr_hotkey_radio_sw.attr);
3326 /* For X41t, X60t, X61t Tablets... */
3327 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3328 tp_features.hotkey_tablet = 1;
3329 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3330 printk(TPACPI_INFO
3331 "possible tablet mode switch found; "
3332 "ThinkPad in %s mode\n",
3333 (tabletsw_state) ? "tablet" : "laptop");
3334 res = add_to_attr_set(hotkey_dev_attributes,
3335 &dev_attr_hotkey_tablet_mode.attr);
3338 if (!res)
3339 res = register_attr_set_with_sysfs(
3340 hotkey_dev_attributes,
3341 &tpacpi_pdev->dev.kobj);
3342 if (res)
3343 goto err_exit;
3345 /* Set up key map */
3347 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3348 GFP_KERNEL);
3349 if (!hotkey_keycode_map) {
3350 printk(TPACPI_ERR
3351 "failed to allocate memory for key map\n");
3352 res = -ENOMEM;
3353 goto err_exit;
3356 if (tpacpi_is_lenovo()) {
3357 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3358 "using Lenovo default hot key map\n");
3359 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3360 TPACPI_HOTKEY_MAP_SIZE);
3361 } else {
3362 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3363 "using IBM default hot key map\n");
3364 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3365 TPACPI_HOTKEY_MAP_SIZE);
3368 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3369 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3370 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3371 tpacpi_inputdev->keycode = hotkey_keycode_map;
3372 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3373 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3374 input_set_capability(tpacpi_inputdev, EV_KEY,
3375 hotkey_keycode_map[i]);
3376 } else {
3377 if (i < sizeof(hotkey_reserved_mask)*8)
3378 hotkey_reserved_mask |= 1 << i;
3382 if (tp_features.hotkey_wlsw) {
3383 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3384 input_report_switch(tpacpi_inputdev,
3385 SW_RFKILL_ALL, radiosw_state);
3387 if (tp_features.hotkey_tablet) {
3388 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3389 input_report_switch(tpacpi_inputdev,
3390 SW_TABLET_MODE, tabletsw_state);
3393 /* Do not issue duplicate brightness change events to
3394 * userspace. tpacpi_detect_brightness_capabilities() must have
3395 * been called before this point */
3396 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3397 printk(TPACPI_INFO
3398 "This ThinkPad has standard ACPI backlight "
3399 "brightness control, supported by the ACPI "
3400 "video driver\n");
3401 printk(TPACPI_NOTICE
3402 "Disabling thinkpad-acpi brightness events "
3403 "by default...\n");
3405 /* Disable brightness up/down on Lenovo thinkpads when
3406 * ACPI is handling them, otherwise it is plain impossible
3407 * for userspace to do something even remotely sane */
3408 hotkey_reserved_mask |=
3409 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3410 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3411 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3412 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3415 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3416 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3417 & ~hotkey_all_mask
3418 & ~hotkey_reserved_mask;
3420 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3421 "hotkey source mask 0x%08x, polling freq %u\n",
3422 hotkey_source_mask, hotkey_poll_freq);
3423 #endif
3425 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3426 "enabling firmware HKEY event interface...\n");
3427 res = hotkey_status_set(true);
3428 if (res) {
3429 hotkey_exit();
3430 return res;
3432 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3433 | hotkey_driver_mask)
3434 & ~hotkey_source_mask);
3435 if (res < 0 && res != -ENXIO) {
3436 hotkey_exit();
3437 return res;
3439 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3440 & ~hotkey_reserved_mask;
3441 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3442 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3443 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3445 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3446 "legacy ibm/hotkey event reporting over procfs %s\n",
3447 (hotkey_report_mode < 2) ?
3448 "enabled" : "disabled");
3450 tpacpi_inputdev->open = &hotkey_inputdev_open;
3451 tpacpi_inputdev->close = &hotkey_inputdev_close;
3453 hotkey_poll_setup_safe(true);
3455 return 0;
3457 err_exit:
3458 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3459 hotkey_dev_attributes = NULL;
3461 return (res < 0)? res : 1;
3464 static bool hotkey_notify_hotkey(const u32 hkey,
3465 bool *send_acpi_ev,
3466 bool *ignore_acpi_ev)
3468 /* 0x1000-0x1FFF: key presses */
3469 unsigned int scancode = hkey & 0xfff;
3470 *send_acpi_ev = true;
3471 *ignore_acpi_ev = false;
3473 if (scancode > 0 && scancode < 0x21) {
3474 scancode--;
3475 if (!(hotkey_source_mask & (1 << scancode))) {
3476 tpacpi_input_send_key_masked(scancode);
3477 *send_acpi_ev = false;
3478 } else {
3479 *ignore_acpi_ev = true;
3481 return true;
3483 return false;
3486 static bool hotkey_notify_wakeup(const u32 hkey,
3487 bool *send_acpi_ev,
3488 bool *ignore_acpi_ev)
3490 /* 0x2000-0x2FFF: Wakeup reason */
3491 *send_acpi_ev = true;
3492 *ignore_acpi_ev = false;
3494 switch (hkey) {
3495 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3496 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3497 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3498 *ignore_acpi_ev = true;
3499 break;
3501 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3502 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3503 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3504 *ignore_acpi_ev = true;
3505 break;
3507 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3508 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3509 printk(TPACPI_ALERT
3510 "EMERGENCY WAKEUP: battery almost empty\n");
3511 /* how to auto-heal: */
3512 /* 2313: woke up from S3, go to S4/S5 */
3513 /* 2413: woke up from S4, go to S5 */
3514 break;
3516 default:
3517 return false;
3520 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3521 printk(TPACPI_INFO
3522 "woke up due to a hot-unplug "
3523 "request...\n");
3524 hotkey_wakeup_reason_notify_change();
3526 return true;
3529 static bool hotkey_notify_usrevent(const u32 hkey,
3530 bool *send_acpi_ev,
3531 bool *ignore_acpi_ev)
3533 /* 0x5000-0x5FFF: human interface helpers */
3534 *send_acpi_ev = true;
3535 *ignore_acpi_ev = false;
3537 switch (hkey) {
3538 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3539 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3540 return true;
3542 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3543 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3544 tpacpi_input_send_tabletsw();
3545 hotkey_tablet_mode_notify_change();
3546 *send_acpi_ev = false;
3547 return true;
3549 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3550 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3551 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3552 /* do not propagate these events */
3553 *ignore_acpi_ev = true;
3554 return true;
3556 default:
3557 return false;
3561 static void thermal_dump_all_sensors(void);
3563 static bool hotkey_notify_thermal(const u32 hkey,
3564 bool *send_acpi_ev,
3565 bool *ignore_acpi_ev)
3567 bool known = true;
3569 /* 0x6000-0x6FFF: thermal alarms */
3570 *send_acpi_ev = true;
3571 *ignore_acpi_ev = false;
3573 switch (hkey) {
3574 case TP_HKEY_EV_THM_TABLE_CHANGED:
3575 printk(TPACPI_INFO
3576 "EC reports that Thermal Table has changed\n");
3577 /* recommended action: do nothing, we don't have
3578 * Lenovo ATM information */
3579 return true;
3580 case TP_HKEY_EV_ALARM_BAT_HOT:
3581 printk(TPACPI_CRIT
3582 "THERMAL ALARM: battery is too hot!\n");
3583 /* recommended action: warn user through gui */
3584 break;
3585 case TP_HKEY_EV_ALARM_BAT_XHOT:
3586 printk(TPACPI_ALERT
3587 "THERMAL EMERGENCY: battery is extremely hot!\n");
3588 /* recommended action: immediate sleep/hibernate */
3589 break;
3590 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3591 printk(TPACPI_CRIT
3592 "THERMAL ALARM: "
3593 "a sensor reports something is too hot!\n");
3594 /* recommended action: warn user through gui, that */
3595 /* some internal component is too hot */
3596 break;
3597 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3598 printk(TPACPI_ALERT
3599 "THERMAL EMERGENCY: "
3600 "a sensor reports something is extremely hot!\n");
3601 /* recommended action: immediate sleep/hibernate */
3602 break;
3603 default:
3604 printk(TPACPI_ALERT
3605 "THERMAL ALERT: unknown thermal alarm received\n");
3606 known = false;
3609 thermal_dump_all_sensors();
3611 return known;
3614 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3616 u32 hkey;
3617 bool send_acpi_ev;
3618 bool ignore_acpi_ev;
3619 bool known_ev;
3621 if (event != 0x80) {
3622 printk(TPACPI_ERR
3623 "unknown HKEY notification event %d\n", event);
3624 /* forward it to userspace, maybe it knows how to handle it */
3625 acpi_bus_generate_netlink_event(
3626 ibm->acpi->device->pnp.device_class,
3627 dev_name(&ibm->acpi->device->dev),
3628 event, 0);
3629 return;
3632 while (1) {
3633 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3634 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3635 return;
3638 if (hkey == 0) {
3639 /* queue empty */
3640 return;
3643 send_acpi_ev = true;
3644 ignore_acpi_ev = false;
3646 switch (hkey >> 12) {
3647 case 1:
3648 /* 0x1000-0x1FFF: key presses */
3649 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3650 &ignore_acpi_ev);
3651 break;
3652 case 2:
3653 /* 0x2000-0x2FFF: Wakeup reason */
3654 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3655 &ignore_acpi_ev);
3656 break;
3657 case 3:
3658 /* 0x3000-0x3FFF: bay-related wakeups */
3659 switch (hkey) {
3660 case TP_HKEY_EV_BAYEJ_ACK:
3661 hotkey_autosleep_ack = 1;
3662 printk(TPACPI_INFO
3663 "bay ejected\n");
3664 hotkey_wakeup_hotunplug_complete_notify_change();
3665 known_ev = true;
3666 break;
3667 case TP_HKEY_EV_OPTDRV_EJ:
3668 /* FIXME: kick libata if SATA link offline */
3669 known_ev = true;
3670 break;
3671 default:
3672 known_ev = false;
3674 break;
3675 case 4:
3676 /* 0x4000-0x4FFF: dock-related wakeups */
3677 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3678 hotkey_autosleep_ack = 1;
3679 printk(TPACPI_INFO
3680 "undocked\n");
3681 hotkey_wakeup_hotunplug_complete_notify_change();
3682 known_ev = true;
3683 } else {
3684 known_ev = false;
3686 break;
3687 case 5:
3688 /* 0x5000-0x5FFF: human interface helpers */
3689 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3690 &ignore_acpi_ev);
3691 break;
3692 case 6:
3693 /* 0x6000-0x6FFF: thermal alarms */
3694 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3695 &ignore_acpi_ev);
3696 break;
3697 case 7:
3698 /* 0x7000-0x7FFF: misc */
3699 if (tp_features.hotkey_wlsw &&
3700 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3701 tpacpi_send_radiosw_update();
3702 send_acpi_ev = 0;
3703 known_ev = true;
3704 break;
3706 /* fallthrough to default */
3707 default:
3708 known_ev = false;
3710 if (!known_ev) {
3711 printk(TPACPI_NOTICE
3712 "unhandled HKEY event 0x%04x\n", hkey);
3713 printk(TPACPI_NOTICE
3714 "please report the conditions when this "
3715 "event happened to %s\n", TPACPI_MAIL);
3718 /* Legacy events */
3719 if (!ignore_acpi_ev &&
3720 (send_acpi_ev || hotkey_report_mode < 2)) {
3721 acpi_bus_generate_proc_event(ibm->acpi->device,
3722 event, hkey);
3725 /* netlink events */
3726 if (!ignore_acpi_ev && send_acpi_ev) {
3727 acpi_bus_generate_netlink_event(
3728 ibm->acpi->device->pnp.device_class,
3729 dev_name(&ibm->acpi->device->dev),
3730 event, hkey);
3735 static void hotkey_suspend(pm_message_t state)
3737 /* Do these on suspend, we get the events on early resume! */
3738 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3739 hotkey_autosleep_ack = 0;
3742 static void hotkey_resume(void)
3744 tpacpi_disable_brightness_delay();
3746 if (hotkey_status_set(true) < 0 ||
3747 hotkey_mask_set(hotkey_acpi_mask) < 0)
3748 printk(TPACPI_ERR
3749 "error while attempting to reset the event "
3750 "firmware interface\n");
3752 tpacpi_send_radiosw_update();
3753 hotkey_tablet_mode_notify_change();
3754 hotkey_wakeup_reason_notify_change();
3755 hotkey_wakeup_hotunplug_complete_notify_change();
3756 hotkey_poll_setup_safe(false);
3759 /* procfs -------------------------------------------------------------- */
3760 static int hotkey_read(struct seq_file *m)
3762 int res, status;
3764 if (!tp_features.hotkey) {
3765 seq_printf(m, "status:\t\tnot supported\n");
3766 return 0;
3769 if (mutex_lock_killable(&hotkey_mutex))
3770 return -ERESTARTSYS;
3771 res = hotkey_status_get(&status);
3772 if (!res)
3773 res = hotkey_mask_get();
3774 mutex_unlock(&hotkey_mutex);
3775 if (res)
3776 return res;
3778 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3779 if (hotkey_all_mask) {
3780 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3781 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3782 } else {
3783 seq_printf(m, "mask:\t\tnot supported\n");
3784 seq_printf(m, "commands:\tenable, disable, reset\n");
3787 return 0;
3790 static void hotkey_enabledisable_warn(bool enable)
3792 tpacpi_log_usertask("procfs hotkey enable/disable");
3793 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3794 TPACPI_WARN
3795 "hotkey enable/disable functionality has been "
3796 "removed from the driver. Hotkeys are always "
3797 "enabled\n"))
3798 printk(TPACPI_ERR
3799 "Please remove the hotkey=enable module "
3800 "parameter, it is deprecated. Hotkeys are always "
3801 "enabled\n");
3804 static int hotkey_write(char *buf)
3806 int res;
3807 u32 mask;
3808 char *cmd;
3810 if (!tp_features.hotkey)
3811 return -ENODEV;
3813 if (mutex_lock_killable(&hotkey_mutex))
3814 return -ERESTARTSYS;
3816 mask = hotkey_user_mask;
3818 res = 0;
3819 while ((cmd = next_cmd(&buf))) {
3820 if (strlencmp(cmd, "enable") == 0) {
3821 hotkey_enabledisable_warn(1);
3822 } else if (strlencmp(cmd, "disable") == 0) {
3823 hotkey_enabledisable_warn(0);
3824 res = -EPERM;
3825 } else if (strlencmp(cmd, "reset") == 0) {
3826 mask = (hotkey_all_mask | hotkey_source_mask)
3827 & ~hotkey_reserved_mask;
3828 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3829 /* mask set */
3830 } else if (sscanf(cmd, "%x", &mask) == 1) {
3831 /* mask set */
3832 } else {
3833 res = -EINVAL;
3834 goto errexit;
3838 if (!res) {
3839 tpacpi_disclose_usertask("procfs hotkey",
3840 "set mask to 0x%08x\n", mask);
3841 res = hotkey_user_mask_set(mask);
3844 errexit:
3845 mutex_unlock(&hotkey_mutex);
3846 return res;
3849 static const struct acpi_device_id ibm_htk_device_ids[] = {
3850 {TPACPI_ACPI_HKEY_HID, 0},
3851 {"", 0},
3854 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3855 .hid = ibm_htk_device_ids,
3856 .notify = hotkey_notify,
3857 .handle = &hkey_handle,
3858 .type = ACPI_DEVICE_NOTIFY,
3861 static struct ibm_struct hotkey_driver_data = {
3862 .name = "hotkey",
3863 .read = hotkey_read,
3864 .write = hotkey_write,
3865 .exit = hotkey_exit,
3866 .resume = hotkey_resume,
3867 .suspend = hotkey_suspend,
3868 .acpi = &ibm_hotkey_acpidriver,
3871 /*************************************************************************
3872 * Bluetooth subdriver
3875 enum {
3876 /* ACPI GBDC/SBDC bits */
3877 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3878 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3879 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3880 0 = disable, 1 = enable */
3883 enum {
3884 /* ACPI \BLTH commands */
3885 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3886 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3887 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3888 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3889 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3892 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3894 static int bluetooth_get_status(void)
3896 int status;
3898 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3899 if (dbg_bluetoothemul)
3900 return (tpacpi_bluetooth_emulstate) ?
3901 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3902 #endif
3904 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3905 return -EIO;
3907 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3908 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3911 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3913 int status;
3915 vdbg_printk(TPACPI_DBG_RFKILL,
3916 "will attempt to %s bluetooth\n",
3917 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3919 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3920 if (dbg_bluetoothemul) {
3921 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3922 return 0;
3924 #endif
3926 if (state == TPACPI_RFK_RADIO_ON)
3927 status = TP_ACPI_BLUETOOTH_RADIOSSW
3928 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3929 else
3930 status = 0;
3932 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3933 return -EIO;
3935 return 0;
3938 /* sysfs bluetooth enable ---------------------------------------------- */
3939 static ssize_t bluetooth_enable_show(struct device *dev,
3940 struct device_attribute *attr,
3941 char *buf)
3943 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3944 attr, buf);
3947 static ssize_t bluetooth_enable_store(struct device *dev,
3948 struct device_attribute *attr,
3949 const char *buf, size_t count)
3951 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3952 attr, buf, count);
3955 static struct device_attribute dev_attr_bluetooth_enable =
3956 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3957 bluetooth_enable_show, bluetooth_enable_store);
3959 /* --------------------------------------------------------------------- */
3961 static struct attribute *bluetooth_attributes[] = {
3962 &dev_attr_bluetooth_enable.attr,
3963 NULL
3966 static const struct attribute_group bluetooth_attr_group = {
3967 .attrs = bluetooth_attributes,
3970 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3971 .get_status = bluetooth_get_status,
3972 .set_status = bluetooth_set_status,
3975 static void bluetooth_shutdown(void)
3977 /* Order firmware to save current state to NVRAM */
3978 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3979 TP_ACPI_BLTH_SAVE_STATE))
3980 printk(TPACPI_NOTICE
3981 "failed to save bluetooth state to NVRAM\n");
3982 else
3983 vdbg_printk(TPACPI_DBG_RFKILL,
3984 "bluestooth state saved to NVRAM\n");
3987 static void bluetooth_exit(void)
3989 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3990 &bluetooth_attr_group);
3992 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3994 bluetooth_shutdown();
3997 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3999 int res;
4000 int status = 0;
4002 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4003 "initializing bluetooth subdriver\n");
4005 TPACPI_ACPIHANDLE_INIT(hkey);
4007 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4008 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4009 tp_features.bluetooth = hkey_handle &&
4010 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4012 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4013 "bluetooth is %s, status 0x%02x\n",
4014 str_supported(tp_features.bluetooth),
4015 status);
4017 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4018 if (dbg_bluetoothemul) {
4019 tp_features.bluetooth = 1;
4020 printk(TPACPI_INFO
4021 "bluetooth switch emulation enabled\n");
4022 } else
4023 #endif
4024 if (tp_features.bluetooth &&
4025 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4026 /* no bluetooth hardware present in system */
4027 tp_features.bluetooth = 0;
4028 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4029 "bluetooth hardware not installed\n");
4032 if (!tp_features.bluetooth)
4033 return 1;
4035 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4036 &bluetooth_tprfk_ops,
4037 RFKILL_TYPE_BLUETOOTH,
4038 TPACPI_RFK_BLUETOOTH_SW_NAME,
4039 true);
4040 if (res)
4041 return res;
4043 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4044 &bluetooth_attr_group);
4045 if (res) {
4046 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4047 return res;
4050 return 0;
4053 /* procfs -------------------------------------------------------------- */
4054 static int bluetooth_read(struct seq_file *m)
4056 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4059 static int bluetooth_write(char *buf)
4061 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4064 static struct ibm_struct bluetooth_driver_data = {
4065 .name = "bluetooth",
4066 .read = bluetooth_read,
4067 .write = bluetooth_write,
4068 .exit = bluetooth_exit,
4069 .shutdown = bluetooth_shutdown,
4072 /*************************************************************************
4073 * Wan subdriver
4076 enum {
4077 /* ACPI GWAN/SWAN bits */
4078 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4079 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4080 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4081 0 = disable, 1 = enable */
4084 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4086 static int wan_get_status(void)
4088 int status;
4090 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4091 if (dbg_wwanemul)
4092 return (tpacpi_wwan_emulstate) ?
4093 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4094 #endif
4096 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4097 return -EIO;
4099 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4100 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4103 static int wan_set_status(enum tpacpi_rfkill_state state)
4105 int status;
4107 vdbg_printk(TPACPI_DBG_RFKILL,
4108 "will attempt to %s wwan\n",
4109 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4111 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4112 if (dbg_wwanemul) {
4113 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4114 return 0;
4116 #endif
4118 if (state == TPACPI_RFK_RADIO_ON)
4119 status = TP_ACPI_WANCARD_RADIOSSW
4120 | TP_ACPI_WANCARD_RESUMECTRL;
4121 else
4122 status = 0;
4124 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4125 return -EIO;
4127 return 0;
4130 /* sysfs wan enable ---------------------------------------------------- */
4131 static ssize_t wan_enable_show(struct device *dev,
4132 struct device_attribute *attr,
4133 char *buf)
4135 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4136 attr, buf);
4139 static ssize_t wan_enable_store(struct device *dev,
4140 struct device_attribute *attr,
4141 const char *buf, size_t count)
4143 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4144 attr, buf, count);
4147 static struct device_attribute dev_attr_wan_enable =
4148 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4149 wan_enable_show, wan_enable_store);
4151 /* --------------------------------------------------------------------- */
4153 static struct attribute *wan_attributes[] = {
4154 &dev_attr_wan_enable.attr,
4155 NULL
4158 static const struct attribute_group wan_attr_group = {
4159 .attrs = wan_attributes,
4162 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4163 .get_status = wan_get_status,
4164 .set_status = wan_set_status,
4167 static void wan_shutdown(void)
4169 /* Order firmware to save current state to NVRAM */
4170 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4171 TP_ACPI_WGSV_SAVE_STATE))
4172 printk(TPACPI_NOTICE
4173 "failed to save WWAN state to NVRAM\n");
4174 else
4175 vdbg_printk(TPACPI_DBG_RFKILL,
4176 "WWAN state saved to NVRAM\n");
4179 static void wan_exit(void)
4181 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4182 &wan_attr_group);
4184 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4186 wan_shutdown();
4189 static int __init wan_init(struct ibm_init_struct *iibm)
4191 int res;
4192 int status = 0;
4194 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4195 "initializing wan subdriver\n");
4197 TPACPI_ACPIHANDLE_INIT(hkey);
4199 tp_features.wan = hkey_handle &&
4200 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4202 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4203 "wan is %s, status 0x%02x\n",
4204 str_supported(tp_features.wan),
4205 status);
4207 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4208 if (dbg_wwanemul) {
4209 tp_features.wan = 1;
4210 printk(TPACPI_INFO
4211 "wwan switch emulation enabled\n");
4212 } else
4213 #endif
4214 if (tp_features.wan &&
4215 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4216 /* no wan hardware present in system */
4217 tp_features.wan = 0;
4218 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4219 "wan hardware not installed\n");
4222 if (!tp_features.wan)
4223 return 1;
4225 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4226 &wan_tprfk_ops,
4227 RFKILL_TYPE_WWAN,
4228 TPACPI_RFK_WWAN_SW_NAME,
4229 true);
4230 if (res)
4231 return res;
4233 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4234 &wan_attr_group);
4236 if (res) {
4237 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4238 return res;
4241 return 0;
4244 /* procfs -------------------------------------------------------------- */
4245 static int wan_read(struct seq_file *m)
4247 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4250 static int wan_write(char *buf)
4252 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4255 static struct ibm_struct wan_driver_data = {
4256 .name = "wan",
4257 .read = wan_read,
4258 .write = wan_write,
4259 .exit = wan_exit,
4260 .shutdown = wan_shutdown,
4263 /*************************************************************************
4264 * UWB subdriver
4267 enum {
4268 /* ACPI GUWB/SUWB bits */
4269 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4270 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4273 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4275 static int uwb_get_status(void)
4277 int status;
4279 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4280 if (dbg_uwbemul)
4281 return (tpacpi_uwb_emulstate) ?
4282 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4283 #endif
4285 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4286 return -EIO;
4288 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4289 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4292 static int uwb_set_status(enum tpacpi_rfkill_state state)
4294 int status;
4296 vdbg_printk(TPACPI_DBG_RFKILL,
4297 "will attempt to %s UWB\n",
4298 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4300 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4301 if (dbg_uwbemul) {
4302 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4303 return 0;
4305 #endif
4307 if (state == TPACPI_RFK_RADIO_ON)
4308 status = TP_ACPI_UWB_RADIOSSW;
4309 else
4310 status = 0;
4312 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4313 return -EIO;
4315 return 0;
4318 /* --------------------------------------------------------------------- */
4320 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4321 .get_status = uwb_get_status,
4322 .set_status = uwb_set_status,
4325 static void uwb_exit(void)
4327 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4330 static int __init uwb_init(struct ibm_init_struct *iibm)
4332 int res;
4333 int status = 0;
4335 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4336 "initializing uwb subdriver\n");
4338 TPACPI_ACPIHANDLE_INIT(hkey);
4340 tp_features.uwb = hkey_handle &&
4341 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4343 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4344 "uwb is %s, status 0x%02x\n",
4345 str_supported(tp_features.uwb),
4346 status);
4348 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4349 if (dbg_uwbemul) {
4350 tp_features.uwb = 1;
4351 printk(TPACPI_INFO
4352 "uwb switch emulation enabled\n");
4353 } else
4354 #endif
4355 if (tp_features.uwb &&
4356 !(status & TP_ACPI_UWB_HWPRESENT)) {
4357 /* no uwb hardware present in system */
4358 tp_features.uwb = 0;
4359 dbg_printk(TPACPI_DBG_INIT,
4360 "uwb hardware not installed\n");
4363 if (!tp_features.uwb)
4364 return 1;
4366 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4367 &uwb_tprfk_ops,
4368 RFKILL_TYPE_UWB,
4369 TPACPI_RFK_UWB_SW_NAME,
4370 false);
4371 return res;
4374 static struct ibm_struct uwb_driver_data = {
4375 .name = "uwb",
4376 .exit = uwb_exit,
4377 .flags.experimental = 1,
4380 /*************************************************************************
4381 * Video subdriver
4384 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4386 enum video_access_mode {
4387 TPACPI_VIDEO_NONE = 0,
4388 TPACPI_VIDEO_570, /* 570 */
4389 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4390 TPACPI_VIDEO_NEW, /* all others */
4393 enum { /* video status flags, based on VIDEO_570 */
4394 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4395 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4396 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4399 enum { /* TPACPI_VIDEO_570 constants */
4400 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4401 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4402 * video_status_flags */
4403 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4404 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4407 static enum video_access_mode video_supported;
4408 static int video_orig_autosw;
4410 static int video_autosw_get(void);
4411 static int video_autosw_set(int enable);
4413 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4415 static int __init video_init(struct ibm_init_struct *iibm)
4417 int ivga;
4419 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4421 TPACPI_ACPIHANDLE_INIT(vid);
4422 if (tpacpi_is_ibm())
4423 TPACPI_ACPIHANDLE_INIT(vid2);
4425 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4426 /* G41, assume IVGA doesn't change */
4427 vid_handle = vid2_handle;
4429 if (!vid_handle)
4430 /* video switching not supported on R30, R31 */
4431 video_supported = TPACPI_VIDEO_NONE;
4432 else if (tpacpi_is_ibm() &&
4433 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4434 /* 570 */
4435 video_supported = TPACPI_VIDEO_570;
4436 else if (tpacpi_is_ibm() &&
4437 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4438 /* 600e/x, 770e, 770x */
4439 video_supported = TPACPI_VIDEO_770;
4440 else
4441 /* all others */
4442 video_supported = TPACPI_VIDEO_NEW;
4444 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4445 str_supported(video_supported != TPACPI_VIDEO_NONE),
4446 video_supported);
4448 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4451 static void video_exit(void)
4453 dbg_printk(TPACPI_DBG_EXIT,
4454 "restoring original video autoswitch mode\n");
4455 if (video_autosw_set(video_orig_autosw))
4456 printk(TPACPI_ERR "error while trying to restore original "
4457 "video autoswitch mode\n");
4460 static int video_outputsw_get(void)
4462 int status = 0;
4463 int i;
4465 switch (video_supported) {
4466 case TPACPI_VIDEO_570:
4467 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4468 TP_ACPI_VIDEO_570_PHSCMD))
4469 return -EIO;
4470 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4471 break;
4472 case TPACPI_VIDEO_770:
4473 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4474 return -EIO;
4475 if (i)
4476 status |= TP_ACPI_VIDEO_S_LCD;
4477 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4478 return -EIO;
4479 if (i)
4480 status |= TP_ACPI_VIDEO_S_CRT;
4481 break;
4482 case TPACPI_VIDEO_NEW:
4483 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4484 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4485 return -EIO;
4486 if (i)
4487 status |= TP_ACPI_VIDEO_S_CRT;
4489 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4490 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4491 return -EIO;
4492 if (i)
4493 status |= TP_ACPI_VIDEO_S_LCD;
4494 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4495 return -EIO;
4496 if (i)
4497 status |= TP_ACPI_VIDEO_S_DVI;
4498 break;
4499 default:
4500 return -ENOSYS;
4503 return status;
4506 static int video_outputsw_set(int status)
4508 int autosw;
4509 int res = 0;
4511 switch (video_supported) {
4512 case TPACPI_VIDEO_570:
4513 res = acpi_evalf(NULL, NULL,
4514 "\\_SB.PHS2", "vdd",
4515 TP_ACPI_VIDEO_570_PHS2CMD,
4516 status | TP_ACPI_VIDEO_570_PHS2SET);
4517 break;
4518 case TPACPI_VIDEO_770:
4519 autosw = video_autosw_get();
4520 if (autosw < 0)
4521 return autosw;
4523 res = video_autosw_set(1);
4524 if (res)
4525 return res;
4526 res = acpi_evalf(vid_handle, NULL,
4527 "ASWT", "vdd", status * 0x100, 0);
4528 if (!autosw && video_autosw_set(autosw)) {
4529 printk(TPACPI_ERR
4530 "video auto-switch left enabled due to error\n");
4531 return -EIO;
4533 break;
4534 case TPACPI_VIDEO_NEW:
4535 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4536 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4537 break;
4538 default:
4539 return -ENOSYS;
4542 return (res)? 0 : -EIO;
4545 static int video_autosw_get(void)
4547 int autosw = 0;
4549 switch (video_supported) {
4550 case TPACPI_VIDEO_570:
4551 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4552 return -EIO;
4553 break;
4554 case TPACPI_VIDEO_770:
4555 case TPACPI_VIDEO_NEW:
4556 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4557 return -EIO;
4558 break;
4559 default:
4560 return -ENOSYS;
4563 return autosw & 1;
4566 static int video_autosw_set(int enable)
4568 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4569 return -EIO;
4570 return 0;
4573 static int video_outputsw_cycle(void)
4575 int autosw = video_autosw_get();
4576 int res;
4578 if (autosw < 0)
4579 return autosw;
4581 switch (video_supported) {
4582 case TPACPI_VIDEO_570:
4583 res = video_autosw_set(1);
4584 if (res)
4585 return res;
4586 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4587 break;
4588 case TPACPI_VIDEO_770:
4589 case TPACPI_VIDEO_NEW:
4590 res = video_autosw_set(1);
4591 if (res)
4592 return res;
4593 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4594 break;
4595 default:
4596 return -ENOSYS;
4598 if (!autosw && video_autosw_set(autosw)) {
4599 printk(TPACPI_ERR
4600 "video auto-switch left enabled due to error\n");
4601 return -EIO;
4604 return (res)? 0 : -EIO;
4607 static int video_expand_toggle(void)
4609 switch (video_supported) {
4610 case TPACPI_VIDEO_570:
4611 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4612 0 : -EIO;
4613 case TPACPI_VIDEO_770:
4614 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4615 0 : -EIO;
4616 case TPACPI_VIDEO_NEW:
4617 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4618 0 : -EIO;
4619 default:
4620 return -ENOSYS;
4622 /* not reached */
4625 static int video_read(struct seq_file *m)
4627 int status, autosw;
4629 if (video_supported == TPACPI_VIDEO_NONE) {
4630 seq_printf(m, "status:\t\tnot supported\n");
4631 return 0;
4634 /* Even reads can crash X.org, so... */
4635 if (!capable(CAP_SYS_ADMIN))
4636 return -EPERM;
4638 status = video_outputsw_get();
4639 if (status < 0)
4640 return status;
4642 autosw = video_autosw_get();
4643 if (autosw < 0)
4644 return autosw;
4646 seq_printf(m, "status:\t\tsupported\n");
4647 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4648 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4649 if (video_supported == TPACPI_VIDEO_NEW)
4650 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4651 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4652 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4653 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4654 if (video_supported == TPACPI_VIDEO_NEW)
4655 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4656 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4657 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4659 return 0;
4662 static int video_write(char *buf)
4664 char *cmd;
4665 int enable, disable, status;
4666 int res;
4668 if (video_supported == TPACPI_VIDEO_NONE)
4669 return -ENODEV;
4671 /* Even reads can crash X.org, let alone writes... */
4672 if (!capable(CAP_SYS_ADMIN))
4673 return -EPERM;
4675 enable = 0;
4676 disable = 0;
4678 while ((cmd = next_cmd(&buf))) {
4679 if (strlencmp(cmd, "lcd_enable") == 0) {
4680 enable |= TP_ACPI_VIDEO_S_LCD;
4681 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4682 disable |= TP_ACPI_VIDEO_S_LCD;
4683 } else if (strlencmp(cmd, "crt_enable") == 0) {
4684 enable |= TP_ACPI_VIDEO_S_CRT;
4685 } else if (strlencmp(cmd, "crt_disable") == 0) {
4686 disable |= TP_ACPI_VIDEO_S_CRT;
4687 } else if (video_supported == TPACPI_VIDEO_NEW &&
4688 strlencmp(cmd, "dvi_enable") == 0) {
4689 enable |= TP_ACPI_VIDEO_S_DVI;
4690 } else if (video_supported == TPACPI_VIDEO_NEW &&
4691 strlencmp(cmd, "dvi_disable") == 0) {
4692 disable |= TP_ACPI_VIDEO_S_DVI;
4693 } else if (strlencmp(cmd, "auto_enable") == 0) {
4694 res = video_autosw_set(1);
4695 if (res)
4696 return res;
4697 } else if (strlencmp(cmd, "auto_disable") == 0) {
4698 res = video_autosw_set(0);
4699 if (res)
4700 return res;
4701 } else if (strlencmp(cmd, "video_switch") == 0) {
4702 res = video_outputsw_cycle();
4703 if (res)
4704 return res;
4705 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4706 res = video_expand_toggle();
4707 if (res)
4708 return res;
4709 } else
4710 return -EINVAL;
4713 if (enable || disable) {
4714 status = video_outputsw_get();
4715 if (status < 0)
4716 return status;
4717 res = video_outputsw_set((status & ~disable) | enable);
4718 if (res)
4719 return res;
4722 return 0;
4725 static struct ibm_struct video_driver_data = {
4726 .name = "video",
4727 .read = video_read,
4728 .write = video_write,
4729 .exit = video_exit,
4732 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4734 /*************************************************************************
4735 * Light (thinklight) subdriver
4738 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4739 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4741 static int light_get_status(void)
4743 int status = 0;
4745 if (tp_features.light_status) {
4746 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4747 return -EIO;
4748 return (!!status);
4751 return -ENXIO;
4754 static int light_set_status(int status)
4756 int rc;
4758 if (tp_features.light) {
4759 if (cmos_handle) {
4760 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4761 (status)?
4762 TP_CMOS_THINKLIGHT_ON :
4763 TP_CMOS_THINKLIGHT_OFF);
4764 } else {
4765 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4766 (status)? 1 : 0);
4768 return (rc)? 0 : -EIO;
4771 return -ENXIO;
4774 static void light_set_status_worker(struct work_struct *work)
4776 struct tpacpi_led_classdev *data =
4777 container_of(work, struct tpacpi_led_classdev, work);
4779 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4780 light_set_status((data->new_state != TPACPI_LED_OFF));
4783 static void light_sysfs_set(struct led_classdev *led_cdev,
4784 enum led_brightness brightness)
4786 struct tpacpi_led_classdev *data =
4787 container_of(led_cdev,
4788 struct tpacpi_led_classdev,
4789 led_classdev);
4790 data->new_state = (brightness != LED_OFF) ?
4791 TPACPI_LED_ON : TPACPI_LED_OFF;
4792 queue_work(tpacpi_wq, &data->work);
4795 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4797 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4800 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4801 .led_classdev = {
4802 .name = "tpacpi::thinklight",
4803 .brightness_set = &light_sysfs_set,
4804 .brightness_get = &light_sysfs_get,
4808 static int __init light_init(struct ibm_init_struct *iibm)
4810 int rc;
4812 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4814 if (tpacpi_is_ibm()) {
4815 TPACPI_ACPIHANDLE_INIT(ledb);
4816 TPACPI_ACPIHANDLE_INIT(lght);
4818 TPACPI_ACPIHANDLE_INIT(cmos);
4819 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4821 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4822 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4824 if (tp_features.light)
4825 /* light status not supported on
4826 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4827 tp_features.light_status =
4828 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4830 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4831 str_supported(tp_features.light),
4832 str_supported(tp_features.light_status));
4834 if (!tp_features.light)
4835 return 1;
4837 rc = led_classdev_register(&tpacpi_pdev->dev,
4838 &tpacpi_led_thinklight.led_classdev);
4840 if (rc < 0) {
4841 tp_features.light = 0;
4842 tp_features.light_status = 0;
4843 } else {
4844 rc = 0;
4847 return rc;
4850 static void light_exit(void)
4852 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4853 if (work_pending(&tpacpi_led_thinklight.work))
4854 flush_workqueue(tpacpi_wq);
4857 static int light_read(struct seq_file *m)
4859 int status;
4861 if (!tp_features.light) {
4862 seq_printf(m, "status:\t\tnot supported\n");
4863 } else if (!tp_features.light_status) {
4864 seq_printf(m, "status:\t\tunknown\n");
4865 seq_printf(m, "commands:\ton, off\n");
4866 } else {
4867 status = light_get_status();
4868 if (status < 0)
4869 return status;
4870 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4871 seq_printf(m, "commands:\ton, off\n");
4874 return 0;
4877 static int light_write(char *buf)
4879 char *cmd;
4880 int newstatus = 0;
4882 if (!tp_features.light)
4883 return -ENODEV;
4885 while ((cmd = next_cmd(&buf))) {
4886 if (strlencmp(cmd, "on") == 0) {
4887 newstatus = 1;
4888 } else if (strlencmp(cmd, "off") == 0) {
4889 newstatus = 0;
4890 } else
4891 return -EINVAL;
4894 return light_set_status(newstatus);
4897 static struct ibm_struct light_driver_data = {
4898 .name = "light",
4899 .read = light_read,
4900 .write = light_write,
4901 .exit = light_exit,
4904 /*************************************************************************
4905 * CMOS subdriver
4908 /* sysfs cmos_command -------------------------------------------------- */
4909 static ssize_t cmos_command_store(struct device *dev,
4910 struct device_attribute *attr,
4911 const char *buf, size_t count)
4913 unsigned long cmos_cmd;
4914 int res;
4916 if (parse_strtoul(buf, 21, &cmos_cmd))
4917 return -EINVAL;
4919 res = issue_thinkpad_cmos_command(cmos_cmd);
4920 return (res)? res : count;
4923 static struct device_attribute dev_attr_cmos_command =
4924 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4926 /* --------------------------------------------------------------------- */
4928 static int __init cmos_init(struct ibm_init_struct *iibm)
4930 int res;
4932 vdbg_printk(TPACPI_DBG_INIT,
4933 "initializing cmos commands subdriver\n");
4935 TPACPI_ACPIHANDLE_INIT(cmos);
4937 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4938 str_supported(cmos_handle != NULL));
4940 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4941 if (res)
4942 return res;
4944 return (cmos_handle)? 0 : 1;
4947 static void cmos_exit(void)
4949 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4952 static int cmos_read(struct seq_file *m)
4954 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4955 R30, R31, T20-22, X20-21 */
4956 if (!cmos_handle)
4957 seq_printf(m, "status:\t\tnot supported\n");
4958 else {
4959 seq_printf(m, "status:\t\tsupported\n");
4960 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4963 return 0;
4966 static int cmos_write(char *buf)
4968 char *cmd;
4969 int cmos_cmd, res;
4971 while ((cmd = next_cmd(&buf))) {
4972 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4973 cmos_cmd >= 0 && cmos_cmd <= 21) {
4974 /* cmos_cmd set */
4975 } else
4976 return -EINVAL;
4978 res = issue_thinkpad_cmos_command(cmos_cmd);
4979 if (res)
4980 return res;
4983 return 0;
4986 static struct ibm_struct cmos_driver_data = {
4987 .name = "cmos",
4988 .read = cmos_read,
4989 .write = cmos_write,
4990 .exit = cmos_exit,
4993 /*************************************************************************
4994 * LED subdriver
4997 enum led_access_mode {
4998 TPACPI_LED_NONE = 0,
4999 TPACPI_LED_570, /* 570 */
5000 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5001 TPACPI_LED_NEW, /* all others */
5004 enum { /* For TPACPI_LED_OLD */
5005 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5006 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5007 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5010 static enum led_access_mode led_supported;
5012 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
5013 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
5014 /* T20-22, X20-21 */
5015 "LED", /* all others */
5016 ); /* R30, R31 */
5018 #define TPACPI_LED_NUMLEDS 16
5019 static struct tpacpi_led_classdev *tpacpi_leds;
5020 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5021 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5022 /* there's a limit of 19 chars + NULL before 2.6.26 */
5023 "tpacpi::power",
5024 "tpacpi:orange:batt",
5025 "tpacpi:green:batt",
5026 "tpacpi::dock_active",
5027 "tpacpi::bay_active",
5028 "tpacpi::dock_batt",
5029 "tpacpi::unknown_led",
5030 "tpacpi::standby",
5031 "tpacpi::dock_status1",
5032 "tpacpi::dock_status2",
5033 "tpacpi::unknown_led2",
5034 "tpacpi::unknown_led3",
5035 "tpacpi::thinkvantage",
5037 #define TPACPI_SAFE_LEDS 0x1081U
5039 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5041 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5042 return false;
5043 #else
5044 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5045 #endif
5048 static int led_get_status(const unsigned int led)
5050 int status;
5051 enum led_status_t led_s;
5053 switch (led_supported) {
5054 case TPACPI_LED_570:
5055 if (!acpi_evalf(ec_handle,
5056 &status, "GLED", "dd", 1 << led))
5057 return -EIO;
5058 led_s = (status == 0)?
5059 TPACPI_LED_OFF :
5060 ((status == 1)?
5061 TPACPI_LED_ON :
5062 TPACPI_LED_BLINK);
5063 tpacpi_led_state_cache[led] = led_s;
5064 return led_s;
5065 default:
5066 return -ENXIO;
5069 /* not reached */
5072 static int led_set_status(const unsigned int led,
5073 const enum led_status_t ledstatus)
5075 /* off, on, blink. Index is led_status_t */
5076 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5077 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5079 int rc = 0;
5081 switch (led_supported) {
5082 case TPACPI_LED_570:
5083 /* 570 */
5084 if (unlikely(led > 7))
5085 return -EINVAL;
5086 if (unlikely(tpacpi_is_led_restricted(led)))
5087 return -EPERM;
5088 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5089 (1 << led), led_sled_arg1[ledstatus]))
5090 rc = -EIO;
5091 break;
5092 case TPACPI_LED_OLD:
5093 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5094 if (unlikely(led > 7))
5095 return -EINVAL;
5096 if (unlikely(tpacpi_is_led_restricted(led)))
5097 return -EPERM;
5098 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5099 if (rc >= 0)
5100 rc = ec_write(TPACPI_LED_EC_HLBL,
5101 (ledstatus == TPACPI_LED_BLINK) << led);
5102 if (rc >= 0)
5103 rc = ec_write(TPACPI_LED_EC_HLCL,
5104 (ledstatus != TPACPI_LED_OFF) << led);
5105 break;
5106 case TPACPI_LED_NEW:
5107 /* all others */
5108 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5109 return -EINVAL;
5110 if (unlikely(tpacpi_is_led_restricted(led)))
5111 return -EPERM;
5112 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5113 led, led_led_arg1[ledstatus]))
5114 rc = -EIO;
5115 break;
5116 default:
5117 rc = -ENXIO;
5120 if (!rc)
5121 tpacpi_led_state_cache[led] = ledstatus;
5123 return rc;
5126 static void led_set_status_worker(struct work_struct *work)
5128 struct tpacpi_led_classdev *data =
5129 container_of(work, struct tpacpi_led_classdev, work);
5131 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5132 led_set_status(data->led, data->new_state);
5135 static void led_sysfs_set(struct led_classdev *led_cdev,
5136 enum led_brightness brightness)
5138 struct tpacpi_led_classdev *data = container_of(led_cdev,
5139 struct tpacpi_led_classdev, led_classdev);
5141 if (brightness == LED_OFF)
5142 data->new_state = TPACPI_LED_OFF;
5143 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5144 data->new_state = TPACPI_LED_ON;
5145 else
5146 data->new_state = TPACPI_LED_BLINK;
5148 queue_work(tpacpi_wq, &data->work);
5151 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5152 unsigned long *delay_on, unsigned long *delay_off)
5154 struct tpacpi_led_classdev *data = container_of(led_cdev,
5155 struct tpacpi_led_classdev, led_classdev);
5157 /* Can we choose the flash rate? */
5158 if (*delay_on == 0 && *delay_off == 0) {
5159 /* yes. set them to the hardware blink rate (1 Hz) */
5160 *delay_on = 500; /* ms */
5161 *delay_off = 500; /* ms */
5162 } else if ((*delay_on != 500) || (*delay_off != 500))
5163 return -EINVAL;
5165 data->new_state = TPACPI_LED_BLINK;
5166 queue_work(tpacpi_wq, &data->work);
5168 return 0;
5171 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5173 int rc;
5175 struct tpacpi_led_classdev *data = container_of(led_cdev,
5176 struct tpacpi_led_classdev, led_classdev);
5178 rc = led_get_status(data->led);
5180 if (rc == TPACPI_LED_OFF || rc < 0)
5181 rc = LED_OFF; /* no error handling in led class :( */
5182 else
5183 rc = LED_FULL;
5185 return rc;
5188 static void led_exit(void)
5190 unsigned int i;
5192 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5193 if (tpacpi_leds[i].led_classdev.name)
5194 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5197 kfree(tpacpi_leds);
5200 static int __init tpacpi_init_led(unsigned int led)
5202 int rc;
5204 tpacpi_leds[led].led = led;
5206 /* LEDs with no name don't get registered */
5207 if (!tpacpi_led_names[led])
5208 return 0;
5210 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5211 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5212 if (led_supported == TPACPI_LED_570)
5213 tpacpi_leds[led].led_classdev.brightness_get =
5214 &led_sysfs_get;
5216 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5218 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5220 rc = led_classdev_register(&tpacpi_pdev->dev,
5221 &tpacpi_leds[led].led_classdev);
5222 if (rc < 0)
5223 tpacpi_leds[led].led_classdev.name = NULL;
5225 return rc;
5228 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5229 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5230 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5231 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5233 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5234 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5235 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5236 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5237 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5238 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5239 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5240 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5242 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5243 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5244 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5245 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5246 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5248 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5249 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5250 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5251 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5253 /* (1) - may have excess leds enabled on MSB */
5255 /* Defaults (order matters, keep last, don't reorder!) */
5256 { /* Lenovo */
5257 .vendor = PCI_VENDOR_ID_LENOVO,
5258 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5259 .quirks = 0x1fffU,
5261 { /* IBM ThinkPads with no EC version string */
5262 .vendor = PCI_VENDOR_ID_IBM,
5263 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5264 .quirks = 0x00ffU,
5266 { /* IBM ThinkPads with EC version string */
5267 .vendor = PCI_VENDOR_ID_IBM,
5268 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5269 .quirks = 0x00bfU,
5273 #undef TPACPI_LEDQ_IBM
5274 #undef TPACPI_LEDQ_LNV
5276 static int __init led_init(struct ibm_init_struct *iibm)
5278 unsigned int i;
5279 int rc;
5280 unsigned long useful_leds;
5282 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5284 TPACPI_ACPIHANDLE_INIT(led);
5286 if (!led_handle)
5287 /* led not supported on R30, R31 */
5288 led_supported = TPACPI_LED_NONE;
5289 else if (tpacpi_is_ibm() && strlencmp(led_path, "SLED") == 0)
5290 /* 570 */
5291 led_supported = TPACPI_LED_570;
5292 else if (tpacpi_is_ibm() && strlencmp(led_path, "SYSL") == 0)
5293 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5294 led_supported = TPACPI_LED_OLD;
5295 else
5296 /* all others */
5297 led_supported = TPACPI_LED_NEW;
5299 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5300 str_supported(led_supported), led_supported);
5302 if (led_supported == TPACPI_LED_NONE)
5303 return 1;
5305 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5306 GFP_KERNEL);
5307 if (!tpacpi_leds) {
5308 printk(TPACPI_ERR "Out of memory for LED data\n");
5309 return -ENOMEM;
5312 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5313 ARRAY_SIZE(led_useful_qtable));
5315 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5316 if (!tpacpi_is_led_restricted(i) &&
5317 test_bit(i, &useful_leds)) {
5318 rc = tpacpi_init_led(i);
5319 if (rc < 0) {
5320 led_exit();
5321 return rc;
5326 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5327 printk(TPACPI_NOTICE
5328 "warning: userspace override of important "
5329 "firmware LEDs is enabled\n");
5330 #endif
5331 return 0;
5334 #define str_led_status(s) \
5335 ((s) == TPACPI_LED_OFF ? "off" : \
5336 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5338 static int led_read(struct seq_file *m)
5340 if (!led_supported) {
5341 seq_printf(m, "status:\t\tnot supported\n");
5342 return 0;
5344 seq_printf(m, "status:\t\tsupported\n");
5346 if (led_supported == TPACPI_LED_570) {
5347 /* 570 */
5348 int i, status;
5349 for (i = 0; i < 8; i++) {
5350 status = led_get_status(i);
5351 if (status < 0)
5352 return -EIO;
5353 seq_printf(m, "%d:\t\t%s\n",
5354 i, str_led_status(status));
5358 seq_printf(m, "commands:\t"
5359 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5361 return 0;
5364 static int led_write(char *buf)
5366 char *cmd;
5367 int led, rc;
5368 enum led_status_t s;
5370 if (!led_supported)
5371 return -ENODEV;
5373 while ((cmd = next_cmd(&buf))) {
5374 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5375 return -EINVAL;
5377 if (strstr(cmd, "off")) {
5378 s = TPACPI_LED_OFF;
5379 } else if (strstr(cmd, "on")) {
5380 s = TPACPI_LED_ON;
5381 } else if (strstr(cmd, "blink")) {
5382 s = TPACPI_LED_BLINK;
5383 } else {
5384 return -EINVAL;
5387 rc = led_set_status(led, s);
5388 if (rc < 0)
5389 return rc;
5392 return 0;
5395 static struct ibm_struct led_driver_data = {
5396 .name = "led",
5397 .read = led_read,
5398 .write = led_write,
5399 .exit = led_exit,
5402 /*************************************************************************
5403 * Beep subdriver
5406 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5408 #define TPACPI_BEEP_Q1 0x0001
5410 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5411 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5412 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5415 static int __init beep_init(struct ibm_init_struct *iibm)
5417 unsigned long quirks;
5419 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5421 TPACPI_ACPIHANDLE_INIT(beep);
5423 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5424 str_supported(beep_handle != NULL));
5426 quirks = tpacpi_check_quirks(beep_quirk_table,
5427 ARRAY_SIZE(beep_quirk_table));
5429 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5431 return (beep_handle)? 0 : 1;
5434 static int beep_read(struct seq_file *m)
5436 if (!beep_handle)
5437 seq_printf(m, "status:\t\tnot supported\n");
5438 else {
5439 seq_printf(m, "status:\t\tsupported\n");
5440 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5443 return 0;
5446 static int beep_write(char *buf)
5448 char *cmd;
5449 int beep_cmd;
5451 if (!beep_handle)
5452 return -ENODEV;
5454 while ((cmd = next_cmd(&buf))) {
5455 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5456 beep_cmd >= 0 && beep_cmd <= 17) {
5457 /* beep_cmd set */
5458 } else
5459 return -EINVAL;
5460 if (tp_features.beep_needs_two_args) {
5461 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5462 beep_cmd, 0))
5463 return -EIO;
5464 } else {
5465 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5466 beep_cmd))
5467 return -EIO;
5471 return 0;
5474 static struct ibm_struct beep_driver_data = {
5475 .name = "beep",
5476 .read = beep_read,
5477 .write = beep_write,
5480 /*************************************************************************
5481 * Thermal subdriver
5484 enum thermal_access_mode {
5485 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5486 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5487 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5488 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5489 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5492 enum { /* TPACPI_THERMAL_TPEC_* */
5493 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5494 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5495 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5497 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5501 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5502 struct ibm_thermal_sensors_struct {
5503 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5506 static enum thermal_access_mode thermal_read_mode;
5508 /* idx is zero-based */
5509 static int thermal_get_sensor(int idx, s32 *value)
5511 int t;
5512 s8 tmp;
5513 char tmpi[5];
5515 t = TP_EC_THERMAL_TMP0;
5517 switch (thermal_read_mode) {
5518 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5519 case TPACPI_THERMAL_TPEC_16:
5520 if (idx >= 8 && idx <= 15) {
5521 t = TP_EC_THERMAL_TMP8;
5522 idx -= 8;
5524 /* fallthrough */
5525 #endif
5526 case TPACPI_THERMAL_TPEC_8:
5527 if (idx <= 7) {
5528 if (!acpi_ec_read(t + idx, &tmp))
5529 return -EIO;
5530 *value = tmp * 1000;
5531 return 0;
5533 break;
5535 case TPACPI_THERMAL_ACPI_UPDT:
5536 if (idx <= 7) {
5537 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5538 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5539 return -EIO;
5540 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5541 return -EIO;
5542 *value = (t - 2732) * 100;
5543 return 0;
5545 break;
5547 case TPACPI_THERMAL_ACPI_TMP07:
5548 if (idx <= 7) {
5549 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5550 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5551 return -EIO;
5552 if (t > 127 || t < -127)
5553 t = TP_EC_THERMAL_TMP_NA;
5554 *value = t * 1000;
5555 return 0;
5557 break;
5559 case TPACPI_THERMAL_NONE:
5560 default:
5561 return -ENOSYS;
5564 return -EINVAL;
5567 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5569 int res, i;
5570 int n;
5572 n = 8;
5573 i = 0;
5575 if (!s)
5576 return -EINVAL;
5578 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5579 n = 16;
5581 for (i = 0 ; i < n; i++) {
5582 res = thermal_get_sensor(i, &s->temp[i]);
5583 if (res)
5584 return res;
5587 return n;
5590 static void thermal_dump_all_sensors(void)
5592 int n, i;
5593 struct ibm_thermal_sensors_struct t;
5595 n = thermal_get_sensors(&t);
5596 if (n <= 0)
5597 return;
5599 printk(TPACPI_NOTICE
5600 "temperatures (Celsius):");
5602 for (i = 0; i < n; i++) {
5603 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5604 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5605 else
5606 printk(KERN_CONT " N/A");
5609 printk(KERN_CONT "\n");
5612 /* sysfs temp##_input -------------------------------------------------- */
5614 static ssize_t thermal_temp_input_show(struct device *dev,
5615 struct device_attribute *attr,
5616 char *buf)
5618 struct sensor_device_attribute *sensor_attr =
5619 to_sensor_dev_attr(attr);
5620 int idx = sensor_attr->index;
5621 s32 value;
5622 int res;
5624 res = thermal_get_sensor(idx, &value);
5625 if (res)
5626 return res;
5627 if (value == TPACPI_THERMAL_SENSOR_NA)
5628 return -ENXIO;
5630 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5633 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5634 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5635 thermal_temp_input_show, NULL, _idxB)
5637 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5638 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5639 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5640 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5641 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5642 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5643 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5644 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5645 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5646 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5647 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5648 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5649 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5650 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5651 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5652 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5653 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5656 #define THERMAL_ATTRS(X) \
5657 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5659 static struct attribute *thermal_temp_input_attr[] = {
5660 THERMAL_ATTRS(8),
5661 THERMAL_ATTRS(9),
5662 THERMAL_ATTRS(10),
5663 THERMAL_ATTRS(11),
5664 THERMAL_ATTRS(12),
5665 THERMAL_ATTRS(13),
5666 THERMAL_ATTRS(14),
5667 THERMAL_ATTRS(15),
5668 THERMAL_ATTRS(0),
5669 THERMAL_ATTRS(1),
5670 THERMAL_ATTRS(2),
5671 THERMAL_ATTRS(3),
5672 THERMAL_ATTRS(4),
5673 THERMAL_ATTRS(5),
5674 THERMAL_ATTRS(6),
5675 THERMAL_ATTRS(7),
5676 NULL
5679 static const struct attribute_group thermal_temp_input16_group = {
5680 .attrs = thermal_temp_input_attr
5683 static const struct attribute_group thermal_temp_input8_group = {
5684 .attrs = &thermal_temp_input_attr[8]
5687 #undef THERMAL_SENSOR_ATTR_TEMP
5688 #undef THERMAL_ATTRS
5690 /* --------------------------------------------------------------------- */
5692 static int __init thermal_init(struct ibm_init_struct *iibm)
5694 u8 t, ta1, ta2;
5695 int i;
5696 int acpi_tmp7;
5697 int res;
5699 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5701 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5703 if (thinkpad_id.ec_model) {
5705 * Direct EC access mode: sensors at registers
5706 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5707 * non-implemented, thermal sensors return 0x80 when
5708 * not available
5711 ta1 = ta2 = 0;
5712 for (i = 0; i < 8; i++) {
5713 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5714 ta1 |= t;
5715 } else {
5716 ta1 = 0;
5717 break;
5719 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5720 ta2 |= t;
5721 } else {
5722 ta1 = 0;
5723 break;
5726 if (ta1 == 0) {
5727 /* This is sheer paranoia, but we handle it anyway */
5728 if (acpi_tmp7) {
5729 printk(TPACPI_ERR
5730 "ThinkPad ACPI EC access misbehaving, "
5731 "falling back to ACPI TMPx access "
5732 "mode\n");
5733 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5734 } else {
5735 printk(TPACPI_ERR
5736 "ThinkPad ACPI EC access misbehaving, "
5737 "disabling thermal sensors access\n");
5738 thermal_read_mode = TPACPI_THERMAL_NONE;
5740 } else {
5741 thermal_read_mode =
5742 (ta2 != 0) ?
5743 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5745 } else if (acpi_tmp7) {
5746 if (tpacpi_is_ibm() &&
5747 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5748 /* 600e/x, 770e, 770x */
5749 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5750 } else {
5751 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
5752 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5754 } else {
5755 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5756 thermal_read_mode = TPACPI_THERMAL_NONE;
5759 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5760 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5761 thermal_read_mode);
5763 switch (thermal_read_mode) {
5764 case TPACPI_THERMAL_TPEC_16:
5765 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5766 &thermal_temp_input16_group);
5767 if (res)
5768 return res;
5769 break;
5770 case TPACPI_THERMAL_TPEC_8:
5771 case TPACPI_THERMAL_ACPI_TMP07:
5772 case TPACPI_THERMAL_ACPI_UPDT:
5773 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5774 &thermal_temp_input8_group);
5775 if (res)
5776 return res;
5777 break;
5778 case TPACPI_THERMAL_NONE:
5779 default:
5780 return 1;
5783 return 0;
5786 static void thermal_exit(void)
5788 switch (thermal_read_mode) {
5789 case TPACPI_THERMAL_TPEC_16:
5790 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5791 &thermal_temp_input16_group);
5792 break;
5793 case TPACPI_THERMAL_TPEC_8:
5794 case TPACPI_THERMAL_ACPI_TMP07:
5795 case TPACPI_THERMAL_ACPI_UPDT:
5796 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5797 &thermal_temp_input8_group);
5798 break;
5799 case TPACPI_THERMAL_NONE:
5800 default:
5801 break;
5805 static int thermal_read(struct seq_file *m)
5807 int n, i;
5808 struct ibm_thermal_sensors_struct t;
5810 n = thermal_get_sensors(&t);
5811 if (unlikely(n < 0))
5812 return n;
5814 seq_printf(m, "temperatures:\t");
5816 if (n > 0) {
5817 for (i = 0; i < (n - 1); i++)
5818 seq_printf(m, "%d ", t.temp[i] / 1000);
5819 seq_printf(m, "%d\n", t.temp[i] / 1000);
5820 } else
5821 seq_printf(m, "not supported\n");
5823 return 0;
5826 static struct ibm_struct thermal_driver_data = {
5827 .name = "thermal",
5828 .read = thermal_read,
5829 .exit = thermal_exit,
5832 /*************************************************************************
5833 * EC Dump subdriver
5836 static u8 ecdump_regs[256];
5838 static int ecdump_read(struct seq_file *m)
5840 int i, j;
5841 u8 v;
5843 seq_printf(m, "EC "
5844 " +00 +01 +02 +03 +04 +05 +06 +07"
5845 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5846 for (i = 0; i < 256; i += 16) {
5847 seq_printf(m, "EC 0x%02x:", i);
5848 for (j = 0; j < 16; j++) {
5849 if (!acpi_ec_read(i + j, &v))
5850 break;
5851 if (v != ecdump_regs[i + j])
5852 seq_printf(m, " *%02x", v);
5853 else
5854 seq_printf(m, " %02x", v);
5855 ecdump_regs[i + j] = v;
5857 seq_putc(m, '\n');
5858 if (j != 16)
5859 break;
5862 /* These are way too dangerous to advertise openly... */
5863 #if 0
5864 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5865 " (<offset> is 00-ff, <value> is 00-ff)\n");
5866 seq_printf(m, "commands:\t0x<offset> <value> "
5867 " (<offset> is 00-ff, <value> is 0-255)\n");
5868 #endif
5869 return 0;
5872 static int ecdump_write(char *buf)
5874 char *cmd;
5875 int i, v;
5877 while ((cmd = next_cmd(&buf))) {
5878 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5879 /* i and v set */
5880 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5881 /* i and v set */
5882 } else
5883 return -EINVAL;
5884 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5885 if (!acpi_ec_write(i, v))
5886 return -EIO;
5887 } else
5888 return -EINVAL;
5891 return 0;
5894 static struct ibm_struct ecdump_driver_data = {
5895 .name = "ecdump",
5896 .read = ecdump_read,
5897 .write = ecdump_write,
5898 .flags.experimental = 1,
5901 /*************************************************************************
5902 * Backlight/brightness subdriver
5905 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5908 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5909 * CMOS NVRAM byte 0x5E, bits 0-3.
5911 * EC HBRV (0x31) has the following layout
5912 * Bit 7: unknown function
5913 * Bit 6: unknown function
5914 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5915 * Bit 4: must be set to zero to avoid problems
5916 * Bit 3-0: backlight brightness level
5918 * brightness_get_raw returns status data in the HBRV layout
5920 * WARNING: The X61 has been verified to use HBRV for something else, so
5921 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5922 * testing on the very early *60 Lenovo models...
5925 enum {
5926 TP_EC_BACKLIGHT = 0x31,
5928 /* TP_EC_BACKLIGHT bitmasks */
5929 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5930 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5931 TP_EC_BACKLIGHT_MAPSW = 0x20,
5934 enum tpacpi_brightness_access_mode {
5935 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5936 TPACPI_BRGHT_MODE_EC, /* EC control */
5937 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5938 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5939 TPACPI_BRGHT_MODE_MAX
5942 static struct backlight_device *ibm_backlight_device;
5944 static enum tpacpi_brightness_access_mode brightness_mode =
5945 TPACPI_BRGHT_MODE_MAX;
5947 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5949 static struct mutex brightness_mutex;
5951 /* NVRAM brightness access,
5952 * call with brightness_mutex held! */
5953 static unsigned int tpacpi_brightness_nvram_get(void)
5955 u8 lnvram;
5957 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5958 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5959 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5960 lnvram &= bright_maxlvl;
5962 return lnvram;
5965 static void tpacpi_brightness_checkpoint_nvram(void)
5967 u8 lec = 0;
5968 u8 b_nvram;
5970 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5971 return;
5973 vdbg_printk(TPACPI_DBG_BRGHT,
5974 "trying to checkpoint backlight level to NVRAM...\n");
5976 if (mutex_lock_killable(&brightness_mutex) < 0)
5977 return;
5979 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5980 goto unlock;
5981 lec &= TP_EC_BACKLIGHT_LVLMSK;
5982 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5984 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5985 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5986 /* NVRAM needs update */
5987 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5988 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5989 b_nvram |= lec;
5990 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5991 dbg_printk(TPACPI_DBG_BRGHT,
5992 "updated NVRAM backlight level to %u (0x%02x)\n",
5993 (unsigned int) lec, (unsigned int) b_nvram);
5994 } else
5995 vdbg_printk(TPACPI_DBG_BRGHT,
5996 "NVRAM backlight level already is %u (0x%02x)\n",
5997 (unsigned int) lec, (unsigned int) b_nvram);
5999 unlock:
6000 mutex_unlock(&brightness_mutex);
6004 /* call with brightness_mutex held! */
6005 static int tpacpi_brightness_get_raw(int *status)
6007 u8 lec = 0;
6009 switch (brightness_mode) {
6010 case TPACPI_BRGHT_MODE_UCMS_STEP:
6011 *status = tpacpi_brightness_nvram_get();
6012 return 0;
6013 case TPACPI_BRGHT_MODE_EC:
6014 case TPACPI_BRGHT_MODE_ECNVRAM:
6015 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6016 return -EIO;
6017 *status = lec;
6018 return 0;
6019 default:
6020 return -ENXIO;
6024 /* call with brightness_mutex held! */
6025 /* do NOT call with illegal backlight level value */
6026 static int tpacpi_brightness_set_ec(unsigned int value)
6028 u8 lec = 0;
6030 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6031 return -EIO;
6033 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6034 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6035 (value & TP_EC_BACKLIGHT_LVLMSK))))
6036 return -EIO;
6038 return 0;
6041 /* call with brightness_mutex held! */
6042 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6044 int cmos_cmd, inc;
6045 unsigned int current_value, i;
6047 current_value = tpacpi_brightness_nvram_get();
6049 if (value == current_value)
6050 return 0;
6052 cmos_cmd = (value > current_value) ?
6053 TP_CMOS_BRIGHTNESS_UP :
6054 TP_CMOS_BRIGHTNESS_DOWN;
6055 inc = (value > current_value) ? 1 : -1;
6057 for (i = current_value; i != value; i += inc)
6058 if (issue_thinkpad_cmos_command(cmos_cmd))
6059 return -EIO;
6061 return 0;
6064 /* May return EINTR which can always be mapped to ERESTARTSYS */
6065 static int brightness_set(unsigned int value)
6067 int res;
6069 if (value > bright_maxlvl || value < 0)
6070 return -EINVAL;
6072 vdbg_printk(TPACPI_DBG_BRGHT,
6073 "set backlight level to %d\n", value);
6075 res = mutex_lock_killable(&brightness_mutex);
6076 if (res < 0)
6077 return res;
6079 switch (brightness_mode) {
6080 case TPACPI_BRGHT_MODE_EC:
6081 case TPACPI_BRGHT_MODE_ECNVRAM:
6082 res = tpacpi_brightness_set_ec(value);
6083 break;
6084 case TPACPI_BRGHT_MODE_UCMS_STEP:
6085 res = tpacpi_brightness_set_ucmsstep(value);
6086 break;
6087 default:
6088 res = -ENXIO;
6091 mutex_unlock(&brightness_mutex);
6092 return res;
6095 /* sysfs backlight class ----------------------------------------------- */
6097 static int brightness_update_status(struct backlight_device *bd)
6099 unsigned int level =
6100 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6101 bd->props.power == FB_BLANK_UNBLANK) ?
6102 bd->props.brightness : 0;
6104 dbg_printk(TPACPI_DBG_BRGHT,
6105 "backlight: attempt to set level to %d\n",
6106 level);
6108 /* it is the backlight class's job (caller) to handle
6109 * EINTR and other errors properly */
6110 return brightness_set(level);
6113 static int brightness_get(struct backlight_device *bd)
6115 int status, res;
6117 res = mutex_lock_killable(&brightness_mutex);
6118 if (res < 0)
6119 return 0;
6121 res = tpacpi_brightness_get_raw(&status);
6123 mutex_unlock(&brightness_mutex);
6125 if (res < 0)
6126 return 0;
6128 return status & TP_EC_BACKLIGHT_LVLMSK;
6131 static void tpacpi_brightness_notify_change(void)
6133 backlight_force_update(ibm_backlight_device,
6134 BACKLIGHT_UPDATE_HOTKEY);
6137 static struct backlight_ops ibm_backlight_data = {
6138 .get_brightness = brightness_get,
6139 .update_status = brightness_update_status,
6142 /* --------------------------------------------------------------------- */
6144 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6146 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6147 union acpi_object *obj;
6148 int rc;
6150 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
6151 obj = (union acpi_object *)buffer.pointer;
6152 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6153 printk(TPACPI_ERR "Unknown _BCL data, "
6154 "please report this to %s\n", TPACPI_MAIL);
6155 rc = 0;
6156 } else {
6157 rc = obj->package.count;
6159 } else {
6160 return 0;
6163 kfree(buffer.pointer);
6164 return rc;
6167 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
6168 u32 lvl, void *context, void **rv)
6170 char name[ACPI_PATH_SEGMENT_LENGTH];
6171 struct acpi_buffer buffer = { sizeof(name), &name };
6173 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
6174 !strncmp("_BCL", name, sizeof(name) - 1)) {
6175 BUG_ON(!rv || !*rv);
6176 **(int **)rv = tpacpi_query_bcl_levels(handle);
6177 return AE_CTRL_TERMINATE;
6178 } else {
6179 return AE_OK;
6184 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6186 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6188 int status;
6189 int bcl_levels = 0;
6190 void *bcl_ptr = &bcl_levels;
6192 if (!vid_handle)
6193 TPACPI_ACPIHANDLE_INIT(vid);
6195 if (!vid_handle)
6196 return 0;
6199 * Search for a _BCL method, and execute it. This is safe on all
6200 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
6201 * BIOS in ACPI backlight control mode. We do NOT have to care
6202 * about calling the _BCL method in an enabled video device, any
6203 * will do for our purposes.
6206 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
6207 tpacpi_acpi_walk_find_bcl, NULL, NULL,
6208 &bcl_ptr);
6210 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
6211 tp_features.bright_acpimode = 1;
6212 return bcl_levels - 2;
6215 return 0;
6219 * These are only useful for models that have only one possibility
6220 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6221 * these quirks.
6223 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6224 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6225 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6227 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6228 /* Models with ATI GPUs known to require ECNVRAM mode */
6229 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6231 /* Models with ATI GPUs that can use ECNVRAM */
6232 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6233 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6234 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6235 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6237 /* Models with Intel Extreme Graphics 2 */
6238 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6239 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6240 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6242 /* Models with Intel GMA900 */
6243 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6244 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6245 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6249 * Returns < 0 for error, otherwise sets tp_features.bright_*
6250 * and bright_maxlvl.
6252 static void __init tpacpi_detect_brightness_capabilities(void)
6254 unsigned int b;
6256 vdbg_printk(TPACPI_DBG_INIT,
6257 "detecting firmware brightness interface capabilities\n");
6259 /* we could run a quirks check here (same table used by
6260 * brightness_init) if needed */
6263 * We always attempt to detect acpi support, so as to switch
6264 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6265 * going to publish a backlight interface
6267 b = tpacpi_check_std_acpi_brightness_support();
6268 switch (b) {
6269 case 16:
6270 bright_maxlvl = 15;
6271 printk(TPACPI_INFO
6272 "detected a 16-level brightness capable ThinkPad\n");
6273 break;
6274 case 8:
6275 case 0:
6276 bright_maxlvl = 7;
6277 printk(TPACPI_INFO
6278 "detected a 8-level brightness capable ThinkPad\n");
6279 break;
6280 default:
6281 printk(TPACPI_ERR
6282 "Unsupported brightness interface, "
6283 "please contact %s\n", TPACPI_MAIL);
6284 tp_features.bright_unkfw = 1;
6285 bright_maxlvl = b - 1;
6289 static int __init brightness_init(struct ibm_init_struct *iibm)
6291 struct backlight_properties props;
6292 int b;
6293 unsigned long quirks;
6295 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6297 mutex_init(&brightness_mutex);
6299 quirks = tpacpi_check_quirks(brightness_quirk_table,
6300 ARRAY_SIZE(brightness_quirk_table));
6302 /* tpacpi_detect_brightness_capabilities() must have run already */
6304 /* if it is unknown, we don't handle it: it wouldn't be safe */
6305 if (tp_features.bright_unkfw)
6306 return 1;
6308 if (tp_features.bright_acpimode) {
6309 if (acpi_video_backlight_support()) {
6310 if (brightness_enable > 1) {
6311 printk(TPACPI_NOTICE
6312 "Standard ACPI backlight interface "
6313 "available, not loading native one.\n");
6314 return 1;
6315 } else if (brightness_enable == 1) {
6316 printk(TPACPI_NOTICE
6317 "Backlight control force enabled, even if standard "
6318 "ACPI backlight interface is available\n");
6320 } else {
6321 if (brightness_enable > 1) {
6322 printk(TPACPI_NOTICE
6323 "Standard ACPI backlight interface not "
6324 "available, thinkpad_acpi native "
6325 "brightness control enabled\n");
6330 if (!brightness_enable) {
6331 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6332 "brightness support disabled by "
6333 "module parameter\n");
6334 return 1;
6338 * Check for module parameter bogosity, note that we
6339 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6340 * able to detect "unspecified"
6342 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6343 return -EINVAL;
6345 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6346 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6347 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6348 if (quirks & TPACPI_BRGHT_Q_EC)
6349 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6350 else
6351 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6353 dbg_printk(TPACPI_DBG_BRGHT,
6354 "driver auto-selected brightness_mode=%d\n",
6355 brightness_mode);
6358 /* Safety */
6359 if (!tpacpi_is_ibm() &&
6360 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6361 brightness_mode == TPACPI_BRGHT_MODE_EC))
6362 return -EINVAL;
6364 if (tpacpi_brightness_get_raw(&b) < 0)
6365 return 1;
6367 memset(&props, 0, sizeof(struct backlight_properties));
6368 props.max_brightness = bright_maxlvl;
6369 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6370 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6371 NULL, NULL,
6372 &ibm_backlight_data,
6373 &props);
6374 if (IS_ERR(ibm_backlight_device)) {
6375 int rc = PTR_ERR(ibm_backlight_device);
6376 ibm_backlight_device = NULL;
6377 printk(TPACPI_ERR "Could not register backlight device\n");
6378 return rc;
6380 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6381 "brightness is supported\n");
6383 if (quirks & TPACPI_BRGHT_Q_ASK) {
6384 printk(TPACPI_NOTICE
6385 "brightness: will use unverified default: "
6386 "brightness_mode=%d\n", brightness_mode);
6387 printk(TPACPI_NOTICE
6388 "brightness: please report to %s whether it works well "
6389 "or not on your ThinkPad\n", TPACPI_MAIL);
6392 backlight_update_status(ibm_backlight_device);
6394 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6395 "brightness: registering brightness hotkeys "
6396 "as change notification\n");
6397 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6398 | TP_ACPI_HKEY_BRGHTUP_MASK
6399 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6400 return 0;
6403 static void brightness_suspend(pm_message_t state)
6405 tpacpi_brightness_checkpoint_nvram();
6408 static void brightness_shutdown(void)
6410 tpacpi_brightness_checkpoint_nvram();
6413 static void brightness_exit(void)
6415 if (ibm_backlight_device) {
6416 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6417 "calling backlight_device_unregister()\n");
6418 backlight_device_unregister(ibm_backlight_device);
6421 tpacpi_brightness_checkpoint_nvram();
6424 static int brightness_read(struct seq_file *m)
6426 int level;
6428 level = brightness_get(NULL);
6429 if (level < 0) {
6430 seq_printf(m, "level:\t\tunreadable\n");
6431 } else {
6432 seq_printf(m, "level:\t\t%d\n", level);
6433 seq_printf(m, "commands:\tup, down\n");
6434 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6435 bright_maxlvl);
6438 return 0;
6441 static int brightness_write(char *buf)
6443 int level;
6444 int rc;
6445 char *cmd;
6447 level = brightness_get(NULL);
6448 if (level < 0)
6449 return level;
6451 while ((cmd = next_cmd(&buf))) {
6452 if (strlencmp(cmd, "up") == 0) {
6453 if (level < bright_maxlvl)
6454 level++;
6455 } else if (strlencmp(cmd, "down") == 0) {
6456 if (level > 0)
6457 level--;
6458 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6459 level >= 0 && level <= bright_maxlvl) {
6460 /* new level set */
6461 } else
6462 return -EINVAL;
6465 tpacpi_disclose_usertask("procfs brightness",
6466 "set level to %d\n", level);
6469 * Now we know what the final level should be, so we try to set it.
6470 * Doing it this way makes the syscall restartable in case of EINTR
6472 rc = brightness_set(level);
6473 if (!rc && ibm_backlight_device)
6474 backlight_force_update(ibm_backlight_device,
6475 BACKLIGHT_UPDATE_SYSFS);
6476 return (rc == -EINTR)? -ERESTARTSYS : rc;
6479 static struct ibm_struct brightness_driver_data = {
6480 .name = "brightness",
6481 .read = brightness_read,
6482 .write = brightness_write,
6483 .exit = brightness_exit,
6484 .suspend = brightness_suspend,
6485 .shutdown = brightness_shutdown,
6488 /*************************************************************************
6489 * Volume subdriver
6493 * IBM ThinkPads have a simple volume controller with MUTE gating.
6494 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6496 * Since the *61 series (and probably also the later *60 series), Lenovo
6497 * ThinkPads only implement the MUTE gate.
6499 * EC register 0x30
6500 * Bit 6: MUTE (1 mutes sound)
6501 * Bit 3-0: Volume
6502 * Other bits should be zero as far as we know.
6504 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6505 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6506 * such as bit 7 which is used to detect repeated presses of MUTE,
6507 * and we leave them unchanged.
6510 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6512 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6513 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6514 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6516 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6517 static char *alsa_id = "ThinkPadEC";
6518 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6520 struct tpacpi_alsa_data {
6521 struct snd_card *card;
6522 struct snd_ctl_elem_id *ctl_mute_id;
6523 struct snd_ctl_elem_id *ctl_vol_id;
6526 static struct snd_card *alsa_card;
6528 enum {
6529 TP_EC_AUDIO = 0x30,
6531 /* TP_EC_AUDIO bits */
6532 TP_EC_AUDIO_MUTESW = 6,
6534 /* TP_EC_AUDIO bitmasks */
6535 TP_EC_AUDIO_LVL_MSK = 0x0F,
6536 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6538 /* Maximum volume */
6539 TP_EC_VOLUME_MAX = 14,
6542 enum tpacpi_volume_access_mode {
6543 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6544 TPACPI_VOL_MODE_EC, /* Pure EC control */
6545 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6546 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6547 TPACPI_VOL_MODE_MAX
6550 enum tpacpi_volume_capabilities {
6551 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6552 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6553 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6554 TPACPI_VOL_CAP_MAX
6557 static enum tpacpi_volume_access_mode volume_mode =
6558 TPACPI_VOL_MODE_MAX;
6560 static enum tpacpi_volume_capabilities volume_capabilities;
6561 static int volume_control_allowed;
6564 * Used to syncronize writers to TP_EC_AUDIO and
6565 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6567 static struct mutex volume_mutex;
6569 static void tpacpi_volume_checkpoint_nvram(void)
6571 u8 lec = 0;
6572 u8 b_nvram;
6573 u8 ec_mask;
6575 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6576 return;
6577 if (!volume_control_allowed)
6578 return;
6580 vdbg_printk(TPACPI_DBG_MIXER,
6581 "trying to checkpoint mixer state to NVRAM...\n");
6583 if (tp_features.mixer_no_level_control)
6584 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6585 else
6586 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6588 if (mutex_lock_killable(&volume_mutex) < 0)
6589 return;
6591 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6592 goto unlock;
6593 lec &= ec_mask;
6594 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6596 if (lec != (b_nvram & ec_mask)) {
6597 /* NVRAM needs update */
6598 b_nvram &= ~ec_mask;
6599 b_nvram |= lec;
6600 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6601 dbg_printk(TPACPI_DBG_MIXER,
6602 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6603 (unsigned int) lec, (unsigned int) b_nvram);
6604 } else {
6605 vdbg_printk(TPACPI_DBG_MIXER,
6606 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6607 (unsigned int) lec, (unsigned int) b_nvram);
6610 unlock:
6611 mutex_unlock(&volume_mutex);
6614 static int volume_get_status_ec(u8 *status)
6616 u8 s;
6618 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6619 return -EIO;
6621 *status = s;
6623 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6625 return 0;
6628 static int volume_get_status(u8 *status)
6630 return volume_get_status_ec(status);
6633 static int volume_set_status_ec(const u8 status)
6635 if (!acpi_ec_write(TP_EC_AUDIO, status))
6636 return -EIO;
6638 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6640 return 0;
6643 static int volume_set_status(const u8 status)
6645 return volume_set_status_ec(status);
6648 /* returns < 0 on error, 0 on no change, 1 on change */
6649 static int __volume_set_mute_ec(const bool mute)
6651 int rc;
6652 u8 s, n;
6654 if (mutex_lock_killable(&volume_mutex) < 0)
6655 return -EINTR;
6657 rc = volume_get_status_ec(&s);
6658 if (rc)
6659 goto unlock;
6661 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6662 s & ~TP_EC_AUDIO_MUTESW_MSK;
6664 if (n != s) {
6665 rc = volume_set_status_ec(n);
6666 if (!rc)
6667 rc = 1;
6670 unlock:
6671 mutex_unlock(&volume_mutex);
6672 return rc;
6675 static int volume_alsa_set_mute(const bool mute)
6677 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6678 (mute) ? "" : "un");
6679 return __volume_set_mute_ec(mute);
6682 static int volume_set_mute(const bool mute)
6684 int rc;
6686 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6687 (mute) ? "" : "un");
6689 rc = __volume_set_mute_ec(mute);
6690 return (rc < 0) ? rc : 0;
6693 /* returns < 0 on error, 0 on no change, 1 on change */
6694 static int __volume_set_volume_ec(const u8 vol)
6696 int rc;
6697 u8 s, n;
6699 if (vol > TP_EC_VOLUME_MAX)
6700 return -EINVAL;
6702 if (mutex_lock_killable(&volume_mutex) < 0)
6703 return -EINTR;
6705 rc = volume_get_status_ec(&s);
6706 if (rc)
6707 goto unlock;
6709 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6711 if (n != s) {
6712 rc = volume_set_status_ec(n);
6713 if (!rc)
6714 rc = 1;
6717 unlock:
6718 mutex_unlock(&volume_mutex);
6719 return rc;
6722 static int volume_alsa_set_volume(const u8 vol)
6724 dbg_printk(TPACPI_DBG_MIXER,
6725 "ALSA: trying to set volume level to %hu\n", vol);
6726 return __volume_set_volume_ec(vol);
6729 static void volume_alsa_notify_change(void)
6731 struct tpacpi_alsa_data *d;
6733 if (alsa_card && alsa_card->private_data) {
6734 d = alsa_card->private_data;
6735 if (d->ctl_mute_id)
6736 snd_ctl_notify(alsa_card,
6737 SNDRV_CTL_EVENT_MASK_VALUE,
6738 d->ctl_mute_id);
6739 if (d->ctl_vol_id)
6740 snd_ctl_notify(alsa_card,
6741 SNDRV_CTL_EVENT_MASK_VALUE,
6742 d->ctl_vol_id);
6746 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6747 struct snd_ctl_elem_info *uinfo)
6749 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6750 uinfo->count = 1;
6751 uinfo->value.integer.min = 0;
6752 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6753 return 0;
6756 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6757 struct snd_ctl_elem_value *ucontrol)
6759 u8 s;
6760 int rc;
6762 rc = volume_get_status(&s);
6763 if (rc < 0)
6764 return rc;
6766 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6767 return 0;
6770 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6771 struct snd_ctl_elem_value *ucontrol)
6773 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6774 ucontrol->value.integer.value[0]);
6775 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6778 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6780 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6781 struct snd_ctl_elem_value *ucontrol)
6783 u8 s;
6784 int rc;
6786 rc = volume_get_status(&s);
6787 if (rc < 0)
6788 return rc;
6790 ucontrol->value.integer.value[0] =
6791 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6792 return 0;
6795 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6796 struct snd_ctl_elem_value *ucontrol)
6798 tpacpi_disclose_usertask("ALSA", "%smute\n",
6799 ucontrol->value.integer.value[0] ?
6800 "un" : "");
6801 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6804 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6805 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6806 .name = "Console Playback Volume",
6807 .index = 0,
6808 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6809 .info = volume_alsa_vol_info,
6810 .get = volume_alsa_vol_get,
6813 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6814 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6815 .name = "Console Playback Switch",
6816 .index = 0,
6817 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6818 .info = volume_alsa_mute_info,
6819 .get = volume_alsa_mute_get,
6822 static void volume_suspend(pm_message_t state)
6824 tpacpi_volume_checkpoint_nvram();
6827 static void volume_resume(void)
6829 volume_alsa_notify_change();
6832 static void volume_shutdown(void)
6834 tpacpi_volume_checkpoint_nvram();
6837 static void volume_exit(void)
6839 if (alsa_card) {
6840 snd_card_free(alsa_card);
6841 alsa_card = NULL;
6844 tpacpi_volume_checkpoint_nvram();
6847 static int __init volume_create_alsa_mixer(void)
6849 struct snd_card *card;
6850 struct tpacpi_alsa_data *data;
6851 struct snd_kcontrol *ctl_vol;
6852 struct snd_kcontrol *ctl_mute;
6853 int rc;
6855 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6856 sizeof(struct tpacpi_alsa_data), &card);
6857 if (rc < 0 || !card) {
6858 printk(TPACPI_ERR
6859 "Failed to create ALSA card structures: %d\n", rc);
6860 return 1;
6863 BUG_ON(!card->private_data);
6864 data = card->private_data;
6865 data->card = card;
6867 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6868 sizeof(card->driver));
6869 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6870 sizeof(card->shortname));
6871 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6872 (thinkpad_id.ec_version_str) ?
6873 thinkpad_id.ec_version_str : "(unknown)");
6874 snprintf(card->longname, sizeof(card->longname),
6875 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6876 (thinkpad_id.ec_version_str) ?
6877 thinkpad_id.ec_version_str : "unknown");
6879 if (volume_control_allowed) {
6880 volume_alsa_control_vol.put = volume_alsa_vol_put;
6881 volume_alsa_control_vol.access =
6882 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6884 volume_alsa_control_mute.put = volume_alsa_mute_put;
6885 volume_alsa_control_mute.access =
6886 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6889 if (!tp_features.mixer_no_level_control) {
6890 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6891 rc = snd_ctl_add(card, ctl_vol);
6892 if (rc < 0) {
6893 printk(TPACPI_ERR
6894 "Failed to create ALSA volume control: %d\n",
6895 rc);
6896 goto err_exit;
6898 data->ctl_vol_id = &ctl_vol->id;
6901 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6902 rc = snd_ctl_add(card, ctl_mute);
6903 if (rc < 0) {
6904 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6905 rc);
6906 goto err_exit;
6908 data->ctl_mute_id = &ctl_mute->id;
6910 snd_card_set_dev(card, &tpacpi_pdev->dev);
6911 rc = snd_card_register(card);
6912 if (rc < 0) {
6913 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6914 goto err_exit;
6917 alsa_card = card;
6918 return 0;
6920 err_exit:
6921 snd_card_free(card);
6922 return 1;
6925 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6926 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6928 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6929 /* Whitelist volume level on all IBM by default */
6930 { .vendor = PCI_VENDOR_ID_IBM,
6931 .bios = TPACPI_MATCH_ANY,
6932 .ec = TPACPI_MATCH_ANY,
6933 .quirks = TPACPI_VOL_Q_LEVEL },
6935 /* Lenovo models with volume control (needs confirmation) */
6936 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6937 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6938 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6939 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6940 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6941 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6942 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6944 /* Whitelist mute-only on all Lenovo by default */
6945 { .vendor = PCI_VENDOR_ID_LENOVO,
6946 .bios = TPACPI_MATCH_ANY,
6947 .ec = TPACPI_MATCH_ANY,
6948 .quirks = TPACPI_VOL_Q_MUTEONLY }
6951 static int __init volume_init(struct ibm_init_struct *iibm)
6953 unsigned long quirks;
6954 int rc;
6956 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6958 mutex_init(&volume_mutex);
6961 * Check for module parameter bogosity, note that we
6962 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6963 * able to detect "unspecified"
6965 if (volume_mode > TPACPI_VOL_MODE_MAX)
6966 return -EINVAL;
6968 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6969 printk(TPACPI_ERR
6970 "UCMS step volume mode not implemented, "
6971 "please contact %s\n", TPACPI_MAIL);
6972 return 1;
6975 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6976 return -EINVAL;
6979 * The ALSA mixer is our primary interface.
6980 * When disabled, don't install the subdriver at all
6982 if (!alsa_enable) {
6983 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6984 "ALSA mixer disabled by parameter, "
6985 "not loading volume subdriver...\n");
6986 return 1;
6989 quirks = tpacpi_check_quirks(volume_quirk_table,
6990 ARRAY_SIZE(volume_quirk_table));
6992 switch (volume_capabilities) {
6993 case TPACPI_VOL_CAP_AUTO:
6994 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6995 tp_features.mixer_no_level_control = 1;
6996 else if (quirks & TPACPI_VOL_Q_LEVEL)
6997 tp_features.mixer_no_level_control = 0;
6998 else
6999 return 1; /* no mixer */
7000 break;
7001 case TPACPI_VOL_CAP_VOLMUTE:
7002 tp_features.mixer_no_level_control = 0;
7003 break;
7004 case TPACPI_VOL_CAP_MUTEONLY:
7005 tp_features.mixer_no_level_control = 1;
7006 break;
7007 default:
7008 return 1;
7011 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7012 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7013 "using user-supplied volume_capabilities=%d\n",
7014 volume_capabilities);
7016 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7017 volume_mode == TPACPI_VOL_MODE_MAX) {
7018 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7020 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7021 "driver auto-selected volume_mode=%d\n",
7022 volume_mode);
7023 } else {
7024 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7025 "using user-supplied volume_mode=%d\n",
7026 volume_mode);
7029 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7030 "mute is supported, volume control is %s\n",
7031 str_supported(!tp_features.mixer_no_level_control));
7033 rc = volume_create_alsa_mixer();
7034 if (rc) {
7035 printk(TPACPI_ERR
7036 "Could not create the ALSA mixer interface\n");
7037 return rc;
7040 printk(TPACPI_INFO
7041 "Console audio control enabled, mode: %s\n",
7042 (volume_control_allowed) ?
7043 "override (read/write)" :
7044 "monitor (read only)");
7046 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7047 "registering volume hotkeys as change notification\n");
7048 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7049 | TP_ACPI_HKEY_VOLUP_MASK
7050 | TP_ACPI_HKEY_VOLDWN_MASK
7051 | TP_ACPI_HKEY_MUTE_MASK);
7053 return 0;
7056 static int volume_read(struct seq_file *m)
7058 u8 status;
7060 if (volume_get_status(&status) < 0) {
7061 seq_printf(m, "level:\t\tunreadable\n");
7062 } else {
7063 if (tp_features.mixer_no_level_control)
7064 seq_printf(m, "level:\t\tunsupported\n");
7065 else
7066 seq_printf(m, "level:\t\t%d\n",
7067 status & TP_EC_AUDIO_LVL_MSK);
7069 seq_printf(m, "mute:\t\t%s\n",
7070 onoff(status, TP_EC_AUDIO_MUTESW));
7072 if (volume_control_allowed) {
7073 seq_printf(m, "commands:\tunmute, mute\n");
7074 if (!tp_features.mixer_no_level_control) {
7075 seq_printf(m,
7076 "commands:\tup, down\n");
7077 seq_printf(m,
7078 "commands:\tlevel <level>"
7079 " (<level> is 0-%d)\n",
7080 TP_EC_VOLUME_MAX);
7085 return 0;
7088 static int volume_write(char *buf)
7090 u8 s;
7091 u8 new_level, new_mute;
7092 int l;
7093 char *cmd;
7094 int rc;
7097 * We do allow volume control at driver startup, so that the
7098 * user can set initial state through the volume=... parameter hack.
7100 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7101 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7102 tp_warned.volume_ctrl_forbidden = 1;
7103 printk(TPACPI_NOTICE
7104 "Console audio control in monitor mode, "
7105 "changes are not allowed.\n");
7106 printk(TPACPI_NOTICE
7107 "Use the volume_control=1 module parameter "
7108 "to enable volume control\n");
7110 return -EPERM;
7113 rc = volume_get_status(&s);
7114 if (rc < 0)
7115 return rc;
7117 new_level = s & TP_EC_AUDIO_LVL_MSK;
7118 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7120 while ((cmd = next_cmd(&buf))) {
7121 if (!tp_features.mixer_no_level_control) {
7122 if (strlencmp(cmd, "up") == 0) {
7123 if (new_mute)
7124 new_mute = 0;
7125 else if (new_level < TP_EC_VOLUME_MAX)
7126 new_level++;
7127 continue;
7128 } else if (strlencmp(cmd, "down") == 0) {
7129 if (new_mute)
7130 new_mute = 0;
7131 else if (new_level > 0)
7132 new_level--;
7133 continue;
7134 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7135 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7136 new_level = l;
7137 continue;
7140 if (strlencmp(cmd, "mute") == 0)
7141 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7142 else if (strlencmp(cmd, "unmute") == 0)
7143 new_mute = 0;
7144 else
7145 return -EINVAL;
7148 if (tp_features.mixer_no_level_control) {
7149 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7150 new_mute ? "" : "un");
7151 rc = volume_set_mute(!!new_mute);
7152 } else {
7153 tpacpi_disclose_usertask("procfs volume",
7154 "%smute and set level to %d\n",
7155 new_mute ? "" : "un", new_level);
7156 rc = volume_set_status(new_mute | new_level);
7158 volume_alsa_notify_change();
7160 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7163 static struct ibm_struct volume_driver_data = {
7164 .name = "volume",
7165 .read = volume_read,
7166 .write = volume_write,
7167 .exit = volume_exit,
7168 .suspend = volume_suspend,
7169 .resume = volume_resume,
7170 .shutdown = volume_shutdown,
7173 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7175 #define alsa_card NULL
7177 static void inline volume_alsa_notify_change(void)
7181 static int __init volume_init(struct ibm_init_struct *iibm)
7183 printk(TPACPI_INFO
7184 "volume: disabled as there is no ALSA support in this kernel\n");
7186 return 1;
7189 static struct ibm_struct volume_driver_data = {
7190 .name = "volume",
7193 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7195 /*************************************************************************
7196 * Fan subdriver
7200 * FAN ACCESS MODES
7202 * TPACPI_FAN_RD_ACPI_GFAN:
7203 * ACPI GFAN method: returns fan level
7205 * see TPACPI_FAN_WR_ACPI_SFAN
7206 * EC 0x2f (HFSP) not available if GFAN exists
7208 * TPACPI_FAN_WR_ACPI_SFAN:
7209 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7211 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7212 * it for writing.
7214 * TPACPI_FAN_WR_TPEC:
7215 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7216 * Supported on almost all ThinkPads
7218 * Fan speed changes of any sort (including those caused by the
7219 * disengaged mode) are usually done slowly by the firmware as the
7220 * maximum amount of fan duty cycle change per second seems to be
7221 * limited.
7223 * Reading is not available if GFAN exists.
7224 * Writing is not available if SFAN exists.
7226 * Bits
7227 * 7 automatic mode engaged;
7228 * (default operation mode of the ThinkPad)
7229 * fan level is ignored in this mode.
7230 * 6 full speed mode (takes precedence over bit 7);
7231 * not available on all thinkpads. May disable
7232 * the tachometer while the fan controller ramps up
7233 * the speed (which can take up to a few *minutes*).
7234 * Speeds up fan to 100% duty-cycle, which is far above
7235 * the standard RPM levels. It is not impossible that
7236 * it could cause hardware damage.
7237 * 5-3 unused in some models. Extra bits for fan level
7238 * in others, but still useless as all values above
7239 * 7 map to the same speed as level 7 in these models.
7240 * 2-0 fan level (0..7 usually)
7241 * 0x00 = stop
7242 * 0x07 = max (set when temperatures critical)
7243 * Some ThinkPads may have other levels, see
7244 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7246 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7247 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7248 * does so, its initial value is meaningless (0x07).
7250 * For firmware bugs, refer to:
7251 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7253 * ----
7255 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7256 * Main fan tachometer reading (in RPM)
7258 * This register is present on all ThinkPads with a new-style EC, and
7259 * it is known not to be present on the A21m/e, and T22, as there is
7260 * something else in offset 0x84 according to the ACPI DSDT. Other
7261 * ThinkPads from this same time period (and earlier) probably lack the
7262 * tachometer as well.
7264 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7265 * was never fixed by IBM to report the EC firmware version string
7266 * probably support the tachometer (like the early X models), so
7267 * detecting it is quite hard. We need more data to know for sure.
7269 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7270 * might result.
7272 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7273 * mode.
7275 * For firmware bugs, refer to:
7276 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7278 * ----
7280 * ThinkPad EC register 0x31 bit 0 (only on select models)
7282 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7283 * show the speed of the main fan. When bit 0 of EC register 0x31
7284 * is one, the tachometer registers show the speed of the auxiliary
7285 * fan.
7287 * Fan control seems to affect both fans, regardless of the state
7288 * of this bit.
7290 * So far, only the firmware for the X60/X61 non-tablet versions
7291 * seem to support this (firmware TP-7M).
7293 * TPACPI_FAN_WR_ACPI_FANS:
7294 * ThinkPad X31, X40, X41. Not available in the X60.
7296 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7297 * high speed. ACPI DSDT seems to map these three speeds to levels
7298 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7299 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7301 * The speeds are stored on handles
7302 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7304 * There are three default speed sets, accessible as handles:
7305 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7307 * ACPI DSDT switches which set is in use depending on various
7308 * factors.
7310 * TPACPI_FAN_WR_TPEC is also available and should be used to
7311 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7312 * but the ACPI tables just mention level 7.
7315 enum { /* Fan control constants */
7316 fan_status_offset = 0x2f, /* EC register 0x2f */
7317 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7318 * 0x84 must be read before 0x85 */
7319 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7320 bit 0 selects which fan is active */
7322 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7323 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7325 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7328 enum fan_status_access_mode {
7329 TPACPI_FAN_NONE = 0, /* No fan status or control */
7330 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7331 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7334 enum fan_control_access_mode {
7335 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7336 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7337 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7338 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7341 enum fan_control_commands {
7342 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7343 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7344 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7345 * and also watchdog cmd */
7348 static int fan_control_allowed;
7350 static enum fan_status_access_mode fan_status_access_mode;
7351 static enum fan_control_access_mode fan_control_access_mode;
7352 static enum fan_control_commands fan_control_commands;
7354 static u8 fan_control_initial_status;
7355 static u8 fan_control_desired_level;
7356 static u8 fan_control_resume_level;
7357 static int fan_watchdog_maxinterval;
7359 static struct mutex fan_mutex;
7361 static void fan_watchdog_fire(struct work_struct *ignored);
7362 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7364 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7365 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7366 "\\FSPD", /* 600e/x, 770e, 770x */
7367 ); /* all others */
7368 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7369 "JFNS", /* 770x-JL */
7370 ); /* all others */
7373 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7374 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7375 * be in auto mode (0x80).
7377 * This is corrected by any write to HFSP either by the driver, or
7378 * by the firmware.
7380 * We assume 0x07 really means auto mode while this quirk is active,
7381 * as this is far more likely than the ThinkPad being in level 7,
7382 * which is only used by the firmware during thermal emergencies.
7384 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7385 * TP-70 (T43, R52), which are known to be buggy.
7388 static void fan_quirk1_setup(void)
7390 if (fan_control_initial_status == 0x07) {
7391 printk(TPACPI_NOTICE
7392 "fan_init: initial fan status is unknown, "
7393 "assuming it is in auto mode\n");
7394 tp_features.fan_ctrl_status_undef = 1;
7398 static void fan_quirk1_handle(u8 *fan_status)
7400 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7401 if (*fan_status != fan_control_initial_status) {
7402 /* something changed the HFSP regisnter since
7403 * driver init time, so it is not undefined
7404 * anymore */
7405 tp_features.fan_ctrl_status_undef = 0;
7406 } else {
7407 /* Return most likely status. In fact, it
7408 * might be the only possible status */
7409 *fan_status = TP_EC_FAN_AUTO;
7414 /* Select main fan on X60/X61, NOOP on others */
7415 static bool fan_select_fan1(void)
7417 if (tp_features.second_fan) {
7418 u8 val;
7420 if (ec_read(fan_select_offset, &val) < 0)
7421 return false;
7422 val &= 0xFEU;
7423 if (ec_write(fan_select_offset, val) < 0)
7424 return false;
7426 return true;
7429 /* Select secondary fan on X60/X61 */
7430 static bool fan_select_fan2(void)
7432 u8 val;
7434 if (!tp_features.second_fan)
7435 return false;
7437 if (ec_read(fan_select_offset, &val) < 0)
7438 return false;
7439 val |= 0x01U;
7440 if (ec_write(fan_select_offset, val) < 0)
7441 return false;
7443 return true;
7447 * Call with fan_mutex held
7449 static void fan_update_desired_level(u8 status)
7451 if ((status &
7452 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7453 if (status > 7)
7454 fan_control_desired_level = 7;
7455 else
7456 fan_control_desired_level = status;
7460 static int fan_get_status(u8 *status)
7462 u8 s;
7464 /* TODO:
7465 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7467 switch (fan_status_access_mode) {
7468 case TPACPI_FAN_RD_ACPI_GFAN:
7469 /* 570, 600e/x, 770e, 770x */
7471 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7472 return -EIO;
7474 if (likely(status))
7475 *status = s & 0x07;
7477 break;
7479 case TPACPI_FAN_RD_TPEC:
7480 /* all except 570, 600e/x, 770e, 770x */
7481 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7482 return -EIO;
7484 if (likely(status)) {
7485 *status = s;
7486 fan_quirk1_handle(status);
7489 break;
7491 default:
7492 return -ENXIO;
7495 return 0;
7498 static int fan_get_status_safe(u8 *status)
7500 int rc;
7501 u8 s;
7503 if (mutex_lock_killable(&fan_mutex))
7504 return -ERESTARTSYS;
7505 rc = fan_get_status(&s);
7506 if (!rc)
7507 fan_update_desired_level(s);
7508 mutex_unlock(&fan_mutex);
7510 if (status)
7511 *status = s;
7513 return rc;
7516 static int fan_get_speed(unsigned int *speed)
7518 u8 hi, lo;
7520 switch (fan_status_access_mode) {
7521 case TPACPI_FAN_RD_TPEC:
7522 /* all except 570, 600e/x, 770e, 770x */
7523 if (unlikely(!fan_select_fan1()))
7524 return -EIO;
7525 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7526 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7527 return -EIO;
7529 if (likely(speed))
7530 *speed = (hi << 8) | lo;
7532 break;
7534 default:
7535 return -ENXIO;
7538 return 0;
7541 static int fan2_get_speed(unsigned int *speed)
7543 u8 hi, lo;
7544 bool rc;
7546 switch (fan_status_access_mode) {
7547 case TPACPI_FAN_RD_TPEC:
7548 /* all except 570, 600e/x, 770e, 770x */
7549 if (unlikely(!fan_select_fan2()))
7550 return -EIO;
7551 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7552 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7553 fan_select_fan1(); /* play it safe */
7554 if (rc)
7555 return -EIO;
7557 if (likely(speed))
7558 *speed = (hi << 8) | lo;
7560 break;
7562 default:
7563 return -ENXIO;
7566 return 0;
7569 static int fan_set_level(int level)
7571 if (!fan_control_allowed)
7572 return -EPERM;
7574 switch (fan_control_access_mode) {
7575 case TPACPI_FAN_WR_ACPI_SFAN:
7576 if (level >= 0 && level <= 7) {
7577 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7578 return -EIO;
7579 } else
7580 return -EINVAL;
7581 break;
7583 case TPACPI_FAN_WR_ACPI_FANS:
7584 case TPACPI_FAN_WR_TPEC:
7585 if (!(level & TP_EC_FAN_AUTO) &&
7586 !(level & TP_EC_FAN_FULLSPEED) &&
7587 ((level < 0) || (level > 7)))
7588 return -EINVAL;
7590 /* safety net should the EC not support AUTO
7591 * or FULLSPEED mode bits and just ignore them */
7592 if (level & TP_EC_FAN_FULLSPEED)
7593 level |= 7; /* safety min speed 7 */
7594 else if (level & TP_EC_FAN_AUTO)
7595 level |= 4; /* safety min speed 4 */
7597 if (!acpi_ec_write(fan_status_offset, level))
7598 return -EIO;
7599 else
7600 tp_features.fan_ctrl_status_undef = 0;
7601 break;
7603 default:
7604 return -ENXIO;
7607 vdbg_printk(TPACPI_DBG_FAN,
7608 "fan control: set fan control register to 0x%02x\n", level);
7609 return 0;
7612 static int fan_set_level_safe(int level)
7614 int rc;
7616 if (!fan_control_allowed)
7617 return -EPERM;
7619 if (mutex_lock_killable(&fan_mutex))
7620 return -ERESTARTSYS;
7622 if (level == TPACPI_FAN_LAST_LEVEL)
7623 level = fan_control_desired_level;
7625 rc = fan_set_level(level);
7626 if (!rc)
7627 fan_update_desired_level(level);
7629 mutex_unlock(&fan_mutex);
7630 return rc;
7633 static int fan_set_enable(void)
7635 u8 s;
7636 int rc;
7638 if (!fan_control_allowed)
7639 return -EPERM;
7641 if (mutex_lock_killable(&fan_mutex))
7642 return -ERESTARTSYS;
7644 switch (fan_control_access_mode) {
7645 case TPACPI_FAN_WR_ACPI_FANS:
7646 case TPACPI_FAN_WR_TPEC:
7647 rc = fan_get_status(&s);
7648 if (rc < 0)
7649 break;
7651 /* Don't go out of emergency fan mode */
7652 if (s != 7) {
7653 s &= 0x07;
7654 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7657 if (!acpi_ec_write(fan_status_offset, s))
7658 rc = -EIO;
7659 else {
7660 tp_features.fan_ctrl_status_undef = 0;
7661 rc = 0;
7663 break;
7665 case TPACPI_FAN_WR_ACPI_SFAN:
7666 rc = fan_get_status(&s);
7667 if (rc < 0)
7668 break;
7670 s &= 0x07;
7672 /* Set fan to at least level 4 */
7673 s |= 4;
7675 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7676 rc = -EIO;
7677 else
7678 rc = 0;
7679 break;
7681 default:
7682 rc = -ENXIO;
7685 mutex_unlock(&fan_mutex);
7687 if (!rc)
7688 vdbg_printk(TPACPI_DBG_FAN,
7689 "fan control: set fan control register to 0x%02x\n",
7691 return rc;
7694 static int fan_set_disable(void)
7696 int rc;
7698 if (!fan_control_allowed)
7699 return -EPERM;
7701 if (mutex_lock_killable(&fan_mutex))
7702 return -ERESTARTSYS;
7704 rc = 0;
7705 switch (fan_control_access_mode) {
7706 case TPACPI_FAN_WR_ACPI_FANS:
7707 case TPACPI_FAN_WR_TPEC:
7708 if (!acpi_ec_write(fan_status_offset, 0x00))
7709 rc = -EIO;
7710 else {
7711 fan_control_desired_level = 0;
7712 tp_features.fan_ctrl_status_undef = 0;
7714 break;
7716 case TPACPI_FAN_WR_ACPI_SFAN:
7717 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7718 rc = -EIO;
7719 else
7720 fan_control_desired_level = 0;
7721 break;
7723 default:
7724 rc = -ENXIO;
7727 if (!rc)
7728 vdbg_printk(TPACPI_DBG_FAN,
7729 "fan control: set fan control register to 0\n");
7731 mutex_unlock(&fan_mutex);
7732 return rc;
7735 static int fan_set_speed(int speed)
7737 int rc;
7739 if (!fan_control_allowed)
7740 return -EPERM;
7742 if (mutex_lock_killable(&fan_mutex))
7743 return -ERESTARTSYS;
7745 rc = 0;
7746 switch (fan_control_access_mode) {
7747 case TPACPI_FAN_WR_ACPI_FANS:
7748 if (speed >= 0 && speed <= 65535) {
7749 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7750 speed, speed, speed))
7751 rc = -EIO;
7752 } else
7753 rc = -EINVAL;
7754 break;
7756 default:
7757 rc = -ENXIO;
7760 mutex_unlock(&fan_mutex);
7761 return rc;
7764 static void fan_watchdog_reset(void)
7766 static int fan_watchdog_active;
7768 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7769 return;
7771 if (fan_watchdog_active)
7772 cancel_delayed_work(&fan_watchdog_task);
7774 if (fan_watchdog_maxinterval > 0 &&
7775 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7776 fan_watchdog_active = 1;
7777 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7778 msecs_to_jiffies(fan_watchdog_maxinterval
7779 * 1000))) {
7780 printk(TPACPI_ERR
7781 "failed to queue the fan watchdog, "
7782 "watchdog will not trigger\n");
7784 } else
7785 fan_watchdog_active = 0;
7788 static void fan_watchdog_fire(struct work_struct *ignored)
7790 int rc;
7792 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7793 return;
7795 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7796 rc = fan_set_enable();
7797 if (rc < 0) {
7798 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7799 "will try again later...\n", -rc);
7800 /* reschedule for later */
7801 fan_watchdog_reset();
7806 * SYSFS fan layout: hwmon compatible (device)
7808 * pwm*_enable:
7809 * 0: "disengaged" mode
7810 * 1: manual mode
7811 * 2: native EC "auto" mode (recommended, hardware default)
7813 * pwm*: set speed in manual mode, ignored otherwise.
7814 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7815 * interpolation.
7817 * fan*_input: tachometer reading, RPM
7820 * SYSFS fan layout: extensions
7822 * fan_watchdog (driver):
7823 * fan watchdog interval in seconds, 0 disables (default), max 120
7826 /* sysfs fan pwm1_enable ----------------------------------------------- */
7827 static ssize_t fan_pwm1_enable_show(struct device *dev,
7828 struct device_attribute *attr,
7829 char *buf)
7831 int res, mode;
7832 u8 status;
7834 res = fan_get_status_safe(&status);
7835 if (res)
7836 return res;
7838 if (status & TP_EC_FAN_FULLSPEED) {
7839 mode = 0;
7840 } else if (status & TP_EC_FAN_AUTO) {
7841 mode = 2;
7842 } else
7843 mode = 1;
7845 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7848 static ssize_t fan_pwm1_enable_store(struct device *dev,
7849 struct device_attribute *attr,
7850 const char *buf, size_t count)
7852 unsigned long t;
7853 int res, level;
7855 if (parse_strtoul(buf, 2, &t))
7856 return -EINVAL;
7858 tpacpi_disclose_usertask("hwmon pwm1_enable",
7859 "set fan mode to %lu\n", t);
7861 switch (t) {
7862 case 0:
7863 level = TP_EC_FAN_FULLSPEED;
7864 break;
7865 case 1:
7866 level = TPACPI_FAN_LAST_LEVEL;
7867 break;
7868 case 2:
7869 level = TP_EC_FAN_AUTO;
7870 break;
7871 case 3:
7872 /* reserved for software-controlled auto mode */
7873 return -ENOSYS;
7874 default:
7875 return -EINVAL;
7878 res = fan_set_level_safe(level);
7879 if (res == -ENXIO)
7880 return -EINVAL;
7881 else if (res < 0)
7882 return res;
7884 fan_watchdog_reset();
7886 return count;
7889 static struct device_attribute dev_attr_fan_pwm1_enable =
7890 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7891 fan_pwm1_enable_show, fan_pwm1_enable_store);
7893 /* sysfs fan pwm1 ------------------------------------------------------ */
7894 static ssize_t fan_pwm1_show(struct device *dev,
7895 struct device_attribute *attr,
7896 char *buf)
7898 int res;
7899 u8 status;
7901 res = fan_get_status_safe(&status);
7902 if (res)
7903 return res;
7905 if ((status &
7906 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7907 status = fan_control_desired_level;
7909 if (status > 7)
7910 status = 7;
7912 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7915 static ssize_t fan_pwm1_store(struct device *dev,
7916 struct device_attribute *attr,
7917 const char *buf, size_t count)
7919 unsigned long s;
7920 int rc;
7921 u8 status, newlevel;
7923 if (parse_strtoul(buf, 255, &s))
7924 return -EINVAL;
7926 tpacpi_disclose_usertask("hwmon pwm1",
7927 "set fan speed to %lu\n", s);
7929 /* scale down from 0-255 to 0-7 */
7930 newlevel = (s >> 5) & 0x07;
7932 if (mutex_lock_killable(&fan_mutex))
7933 return -ERESTARTSYS;
7935 rc = fan_get_status(&status);
7936 if (!rc && (status &
7937 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7938 rc = fan_set_level(newlevel);
7939 if (rc == -ENXIO)
7940 rc = -EINVAL;
7941 else if (!rc) {
7942 fan_update_desired_level(newlevel);
7943 fan_watchdog_reset();
7947 mutex_unlock(&fan_mutex);
7948 return (rc)? rc : count;
7951 static struct device_attribute dev_attr_fan_pwm1 =
7952 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7953 fan_pwm1_show, fan_pwm1_store);
7955 /* sysfs fan fan1_input ------------------------------------------------ */
7956 static ssize_t fan_fan1_input_show(struct device *dev,
7957 struct device_attribute *attr,
7958 char *buf)
7960 int res;
7961 unsigned int speed;
7963 res = fan_get_speed(&speed);
7964 if (res < 0)
7965 return res;
7967 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7970 static struct device_attribute dev_attr_fan_fan1_input =
7971 __ATTR(fan1_input, S_IRUGO,
7972 fan_fan1_input_show, NULL);
7974 /* sysfs fan fan2_input ------------------------------------------------ */
7975 static ssize_t fan_fan2_input_show(struct device *dev,
7976 struct device_attribute *attr,
7977 char *buf)
7979 int res;
7980 unsigned int speed;
7982 res = fan2_get_speed(&speed);
7983 if (res < 0)
7984 return res;
7986 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7989 static struct device_attribute dev_attr_fan_fan2_input =
7990 __ATTR(fan2_input, S_IRUGO,
7991 fan_fan2_input_show, NULL);
7993 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7994 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7995 char *buf)
7997 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8000 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
8001 const char *buf, size_t count)
8003 unsigned long t;
8005 if (parse_strtoul(buf, 120, &t))
8006 return -EINVAL;
8008 if (!fan_control_allowed)
8009 return -EPERM;
8011 fan_watchdog_maxinterval = t;
8012 fan_watchdog_reset();
8014 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8016 return count;
8019 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8020 fan_fan_watchdog_show, fan_fan_watchdog_store);
8022 /* --------------------------------------------------------------------- */
8023 static struct attribute *fan_attributes[] = {
8024 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
8025 &dev_attr_fan_fan1_input.attr,
8026 NULL, /* for fan2_input */
8027 NULL
8030 static const struct attribute_group fan_attr_group = {
8031 .attrs = fan_attributes,
8034 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8035 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8037 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8038 { .vendor = PCI_VENDOR_ID_IBM, \
8039 .bios = TPACPI_MATCH_ANY, \
8040 .ec = TPID(__id1, __id2), \
8041 .quirks = __quirks }
8043 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8044 { .vendor = PCI_VENDOR_ID_LENOVO, \
8045 .bios = TPACPI_MATCH_ANY, \
8046 .ec = TPID(__id1, __id2), \
8047 .quirks = __quirks }
8049 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8050 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8051 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8052 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8053 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8054 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8057 #undef TPACPI_FAN_QL
8058 #undef TPACPI_FAN_QI
8060 static int __init fan_init(struct ibm_init_struct *iibm)
8062 int rc;
8063 unsigned long quirks;
8065 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8066 "initializing fan subdriver\n");
8068 mutex_init(&fan_mutex);
8069 fan_status_access_mode = TPACPI_FAN_NONE;
8070 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8071 fan_control_commands = 0;
8072 fan_watchdog_maxinterval = 0;
8073 tp_features.fan_ctrl_status_undef = 0;
8074 tp_features.second_fan = 0;
8075 fan_control_desired_level = 7;
8077 if (tpacpi_is_ibm()) {
8078 TPACPI_ACPIHANDLE_INIT(fans);
8079 TPACPI_ACPIHANDLE_INIT(gfan);
8080 TPACPI_ACPIHANDLE_INIT(sfan);
8083 quirks = tpacpi_check_quirks(fan_quirk_table,
8084 ARRAY_SIZE(fan_quirk_table));
8086 if (gfan_handle) {
8087 /* 570, 600e/x, 770e, 770x */
8088 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8089 } else {
8090 /* all other ThinkPads: note that even old-style
8091 * ThinkPad ECs supports the fan control register */
8092 if (likely(acpi_ec_read(fan_status_offset,
8093 &fan_control_initial_status))) {
8094 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8095 if (quirks & TPACPI_FAN_Q1)
8096 fan_quirk1_setup();
8097 if (quirks & TPACPI_FAN_2FAN) {
8098 tp_features.second_fan = 1;
8099 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8100 "secondary fan support enabled\n");
8102 } else {
8103 printk(TPACPI_ERR
8104 "ThinkPad ACPI EC access misbehaving, "
8105 "fan status and control unavailable\n");
8106 return 1;
8110 if (sfan_handle) {
8111 /* 570, 770x-JL */
8112 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8113 fan_control_commands |=
8114 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8115 } else {
8116 if (!gfan_handle) {
8117 /* gfan without sfan means no fan control */
8118 /* all other models implement TP EC 0x2f control */
8120 if (fans_handle) {
8121 /* X31, X40, X41 */
8122 fan_control_access_mode =
8123 TPACPI_FAN_WR_ACPI_FANS;
8124 fan_control_commands |=
8125 TPACPI_FAN_CMD_SPEED |
8126 TPACPI_FAN_CMD_LEVEL |
8127 TPACPI_FAN_CMD_ENABLE;
8128 } else {
8129 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8130 fan_control_commands |=
8131 TPACPI_FAN_CMD_LEVEL |
8132 TPACPI_FAN_CMD_ENABLE;
8137 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8138 "fan is %s, modes %d, %d\n",
8139 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8140 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8141 fan_status_access_mode, fan_control_access_mode);
8143 /* fan control master switch */
8144 if (!fan_control_allowed) {
8145 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8146 fan_control_commands = 0;
8147 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8148 "fan control features disabled by parameter\n");
8151 /* update fan_control_desired_level */
8152 if (fan_status_access_mode != TPACPI_FAN_NONE)
8153 fan_get_status_safe(NULL);
8155 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8156 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8157 if (tp_features.second_fan) {
8158 /* attach second fan tachometer */
8159 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8160 &dev_attr_fan_fan2_input.attr;
8162 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8163 &fan_attr_group);
8164 if (rc < 0)
8165 return rc;
8167 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8168 &driver_attr_fan_watchdog);
8169 if (rc < 0) {
8170 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8171 &fan_attr_group);
8172 return rc;
8174 return 0;
8175 } else
8176 return 1;
8179 static void fan_exit(void)
8181 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8182 "cancelling any pending fan watchdog tasks\n");
8184 /* FIXME: can we really do this unconditionally? */
8185 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8186 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8187 &driver_attr_fan_watchdog);
8189 cancel_delayed_work(&fan_watchdog_task);
8190 flush_workqueue(tpacpi_wq);
8193 static void fan_suspend(pm_message_t state)
8195 int rc;
8197 if (!fan_control_allowed)
8198 return;
8200 /* Store fan status in cache */
8201 fan_control_resume_level = 0;
8202 rc = fan_get_status_safe(&fan_control_resume_level);
8203 if (rc < 0)
8204 printk(TPACPI_NOTICE
8205 "failed to read fan level for later "
8206 "restore during resume: %d\n", rc);
8208 /* if it is undefined, don't attempt to restore it.
8209 * KEEP THIS LAST */
8210 if (tp_features.fan_ctrl_status_undef)
8211 fan_control_resume_level = 0;
8214 static void fan_resume(void)
8216 u8 current_level = 7;
8217 bool do_set = false;
8218 int rc;
8220 /* DSDT *always* updates status on resume */
8221 tp_features.fan_ctrl_status_undef = 0;
8223 if (!fan_control_allowed ||
8224 !fan_control_resume_level ||
8225 (fan_get_status_safe(&current_level) < 0))
8226 return;
8228 switch (fan_control_access_mode) {
8229 case TPACPI_FAN_WR_ACPI_SFAN:
8230 /* never decrease fan level */
8231 do_set = (fan_control_resume_level > current_level);
8232 break;
8233 case TPACPI_FAN_WR_ACPI_FANS:
8234 case TPACPI_FAN_WR_TPEC:
8235 /* never decrease fan level, scale is:
8236 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8238 * We expect the firmware to set either 7 or AUTO, but we
8239 * handle FULLSPEED out of paranoia.
8241 * So, we can safely only restore FULLSPEED or 7, anything
8242 * else could slow the fan. Restoring AUTO is useless, at
8243 * best that's exactly what the DSDT already set (it is the
8244 * slower it uses).
8246 * Always keep in mind that the DSDT *will* have set the
8247 * fans to what the vendor supposes is the best level. We
8248 * muck with it only to speed the fan up.
8250 if (fan_control_resume_level != 7 &&
8251 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8252 return;
8253 else
8254 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8255 (current_level != fan_control_resume_level);
8256 break;
8257 default:
8258 return;
8260 if (do_set) {
8261 printk(TPACPI_NOTICE
8262 "restoring fan level to 0x%02x\n",
8263 fan_control_resume_level);
8264 rc = fan_set_level_safe(fan_control_resume_level);
8265 if (rc < 0)
8266 printk(TPACPI_NOTICE
8267 "failed to restore fan level: %d\n", rc);
8271 static int fan_read(struct seq_file *m)
8273 int rc;
8274 u8 status;
8275 unsigned int speed = 0;
8277 switch (fan_status_access_mode) {
8278 case TPACPI_FAN_RD_ACPI_GFAN:
8279 /* 570, 600e/x, 770e, 770x */
8280 rc = fan_get_status_safe(&status);
8281 if (rc < 0)
8282 return rc;
8284 seq_printf(m, "status:\t\t%s\n"
8285 "level:\t\t%d\n",
8286 (status != 0) ? "enabled" : "disabled", status);
8287 break;
8289 case TPACPI_FAN_RD_TPEC:
8290 /* all except 570, 600e/x, 770e, 770x */
8291 rc = fan_get_status_safe(&status);
8292 if (rc < 0)
8293 return rc;
8295 seq_printf(m, "status:\t\t%s\n",
8296 (status != 0) ? "enabled" : "disabled");
8298 rc = fan_get_speed(&speed);
8299 if (rc < 0)
8300 return rc;
8302 seq_printf(m, "speed:\t\t%d\n", speed);
8304 if (status & TP_EC_FAN_FULLSPEED)
8305 /* Disengaged mode takes precedence */
8306 seq_printf(m, "level:\t\tdisengaged\n");
8307 else if (status & TP_EC_FAN_AUTO)
8308 seq_printf(m, "level:\t\tauto\n");
8309 else
8310 seq_printf(m, "level:\t\t%d\n", status);
8311 break;
8313 case TPACPI_FAN_NONE:
8314 default:
8315 seq_printf(m, "status:\t\tnot supported\n");
8318 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8319 seq_printf(m, "commands:\tlevel <level>");
8321 switch (fan_control_access_mode) {
8322 case TPACPI_FAN_WR_ACPI_SFAN:
8323 seq_printf(m, " (<level> is 0-7)\n");
8324 break;
8326 default:
8327 seq_printf(m, " (<level> is 0-7, "
8328 "auto, disengaged, full-speed)\n");
8329 break;
8333 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8334 seq_printf(m, "commands:\tenable, disable\n"
8335 "commands:\twatchdog <timeout> (<timeout> "
8336 "is 0 (off), 1-120 (seconds))\n");
8338 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8339 seq_printf(m, "commands:\tspeed <speed>"
8340 " (<speed> is 0-65535)\n");
8342 return 0;
8345 static int fan_write_cmd_level(const char *cmd, int *rc)
8347 int level;
8349 if (strlencmp(cmd, "level auto") == 0)
8350 level = TP_EC_FAN_AUTO;
8351 else if ((strlencmp(cmd, "level disengaged") == 0) |
8352 (strlencmp(cmd, "level full-speed") == 0))
8353 level = TP_EC_FAN_FULLSPEED;
8354 else if (sscanf(cmd, "level %d", &level) != 1)
8355 return 0;
8357 *rc = fan_set_level_safe(level);
8358 if (*rc == -ENXIO)
8359 printk(TPACPI_ERR "level command accepted for unsupported "
8360 "access mode %d", fan_control_access_mode);
8361 else if (!*rc)
8362 tpacpi_disclose_usertask("procfs fan",
8363 "set level to %d\n", level);
8365 return 1;
8368 static int fan_write_cmd_enable(const char *cmd, int *rc)
8370 if (strlencmp(cmd, "enable") != 0)
8371 return 0;
8373 *rc = fan_set_enable();
8374 if (*rc == -ENXIO)
8375 printk(TPACPI_ERR "enable command accepted for unsupported "
8376 "access mode %d", fan_control_access_mode);
8377 else if (!*rc)
8378 tpacpi_disclose_usertask("procfs fan", "enable\n");
8380 return 1;
8383 static int fan_write_cmd_disable(const char *cmd, int *rc)
8385 if (strlencmp(cmd, "disable") != 0)
8386 return 0;
8388 *rc = fan_set_disable();
8389 if (*rc == -ENXIO)
8390 printk(TPACPI_ERR "disable command accepted for unsupported "
8391 "access mode %d", fan_control_access_mode);
8392 else if (!*rc)
8393 tpacpi_disclose_usertask("procfs fan", "disable\n");
8395 return 1;
8398 static int fan_write_cmd_speed(const char *cmd, int *rc)
8400 int speed;
8402 /* TODO:
8403 * Support speed <low> <medium> <high> ? */
8405 if (sscanf(cmd, "speed %d", &speed) != 1)
8406 return 0;
8408 *rc = fan_set_speed(speed);
8409 if (*rc == -ENXIO)
8410 printk(TPACPI_ERR "speed command accepted for unsupported "
8411 "access mode %d", fan_control_access_mode);
8412 else if (!*rc)
8413 tpacpi_disclose_usertask("procfs fan",
8414 "set speed to %d\n", speed);
8416 return 1;
8419 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8421 int interval;
8423 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8424 return 0;
8426 if (interval < 0 || interval > 120)
8427 *rc = -EINVAL;
8428 else {
8429 fan_watchdog_maxinterval = interval;
8430 tpacpi_disclose_usertask("procfs fan",
8431 "set watchdog timer to %d\n",
8432 interval);
8435 return 1;
8438 static int fan_write(char *buf)
8440 char *cmd;
8441 int rc = 0;
8443 while (!rc && (cmd = next_cmd(&buf))) {
8444 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8445 fan_write_cmd_level(cmd, &rc)) &&
8446 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8447 (fan_write_cmd_enable(cmd, &rc) ||
8448 fan_write_cmd_disable(cmd, &rc) ||
8449 fan_write_cmd_watchdog(cmd, &rc))) &&
8450 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8451 fan_write_cmd_speed(cmd, &rc))
8453 rc = -EINVAL;
8454 else if (!rc)
8455 fan_watchdog_reset();
8458 return rc;
8461 static struct ibm_struct fan_driver_data = {
8462 .name = "fan",
8463 .read = fan_read,
8464 .write = fan_write,
8465 .exit = fan_exit,
8466 .suspend = fan_suspend,
8467 .resume = fan_resume,
8470 /****************************************************************************
8471 ****************************************************************************
8473 * Infrastructure
8475 ****************************************************************************
8476 ****************************************************************************/
8479 * HKEY event callout for other subdrivers go here
8480 * (yes, it is ugly, but it is quick, safe, and gets the job done
8482 static void tpacpi_driver_event(const unsigned int hkey_event)
8484 if (ibm_backlight_device) {
8485 switch (hkey_event) {
8486 case TP_HKEY_EV_BRGHT_UP:
8487 case TP_HKEY_EV_BRGHT_DOWN:
8488 tpacpi_brightness_notify_change();
8491 if (alsa_card) {
8492 switch (hkey_event) {
8493 case TP_HKEY_EV_VOL_UP:
8494 case TP_HKEY_EV_VOL_DOWN:
8495 case TP_HKEY_EV_VOL_MUTE:
8496 volume_alsa_notify_change();
8501 static void hotkey_driver_event(const unsigned int scancode)
8503 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8506 /* sysfs name ---------------------------------------------------------- */
8507 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8508 struct device_attribute *attr,
8509 char *buf)
8511 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8514 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8515 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8517 /* --------------------------------------------------------------------- */
8519 /* /proc support */
8520 static struct proc_dir_entry *proc_dir;
8523 * Module and infrastructure proble, init and exit handling
8526 static int force_load;
8528 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8529 static const char * __init str_supported(int is_supported)
8531 static char text_unsupported[] __initdata = "not supported";
8533 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8535 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8537 static void ibm_exit(struct ibm_struct *ibm)
8539 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8541 list_del_init(&ibm->all_drivers);
8543 if (ibm->flags.acpi_notify_installed) {
8544 dbg_printk(TPACPI_DBG_EXIT,
8545 "%s: acpi_remove_notify_handler\n", ibm->name);
8546 BUG_ON(!ibm->acpi);
8547 acpi_remove_notify_handler(*ibm->acpi->handle,
8548 ibm->acpi->type,
8549 dispatch_acpi_notify);
8550 ibm->flags.acpi_notify_installed = 0;
8551 ibm->flags.acpi_notify_installed = 0;
8554 if (ibm->flags.proc_created) {
8555 dbg_printk(TPACPI_DBG_EXIT,
8556 "%s: remove_proc_entry\n", ibm->name);
8557 remove_proc_entry(ibm->name, proc_dir);
8558 ibm->flags.proc_created = 0;
8561 if (ibm->flags.acpi_driver_registered) {
8562 dbg_printk(TPACPI_DBG_EXIT,
8563 "%s: acpi_bus_unregister_driver\n", ibm->name);
8564 BUG_ON(!ibm->acpi);
8565 acpi_bus_unregister_driver(ibm->acpi->driver);
8566 kfree(ibm->acpi->driver);
8567 ibm->acpi->driver = NULL;
8568 ibm->flags.acpi_driver_registered = 0;
8571 if (ibm->flags.init_called && ibm->exit) {
8572 ibm->exit();
8573 ibm->flags.init_called = 0;
8576 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8579 static int __init ibm_init(struct ibm_init_struct *iibm)
8581 int ret;
8582 struct ibm_struct *ibm = iibm->data;
8583 struct proc_dir_entry *entry;
8585 BUG_ON(ibm == NULL);
8587 INIT_LIST_HEAD(&ibm->all_drivers);
8589 if (ibm->flags.experimental && !experimental)
8590 return 0;
8592 dbg_printk(TPACPI_DBG_INIT,
8593 "probing for %s\n", ibm->name);
8595 if (iibm->init) {
8596 ret = iibm->init(iibm);
8597 if (ret > 0)
8598 return 0; /* probe failed */
8599 if (ret)
8600 return ret;
8602 ibm->flags.init_called = 1;
8605 if (ibm->acpi) {
8606 if (ibm->acpi->hid) {
8607 ret = register_tpacpi_subdriver(ibm);
8608 if (ret)
8609 goto err_out;
8612 if (ibm->acpi->notify) {
8613 ret = setup_acpi_notify(ibm);
8614 if (ret == -ENODEV) {
8615 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8616 ibm->name);
8617 ret = 0;
8618 goto err_out;
8620 if (ret < 0)
8621 goto err_out;
8625 dbg_printk(TPACPI_DBG_INIT,
8626 "%s installed\n", ibm->name);
8628 if (ibm->read) {
8629 mode_t mode = iibm->base_procfs_mode;
8631 if (!mode)
8632 mode = S_IRUGO;
8633 if (ibm->write)
8634 mode |= S_IWUSR;
8635 entry = proc_create_data(ibm->name, mode, proc_dir,
8636 &dispatch_proc_fops, ibm);
8637 if (!entry) {
8638 printk(TPACPI_ERR "unable to create proc entry %s\n",
8639 ibm->name);
8640 ret = -ENODEV;
8641 goto err_out;
8643 ibm->flags.proc_created = 1;
8646 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8648 return 0;
8650 err_out:
8651 dbg_printk(TPACPI_DBG_INIT,
8652 "%s: at error exit path with result %d\n",
8653 ibm->name, ret);
8655 ibm_exit(ibm);
8656 return (ret < 0)? ret : 0;
8659 /* Probing */
8661 static bool __pure __init tpacpi_is_fw_digit(const char c)
8663 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8666 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8667 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8668 const char t)
8670 return s && strlen(s) >= 8 &&
8671 tpacpi_is_fw_digit(s[0]) &&
8672 tpacpi_is_fw_digit(s[1]) &&
8673 s[2] == t && s[3] == 'T' &&
8674 tpacpi_is_fw_digit(s[4]) &&
8675 tpacpi_is_fw_digit(s[5]) &&
8676 s[6] == 'W' && s[7] == 'W';
8679 /* returns 0 - probe ok, or < 0 - probe error.
8680 * Probe ok doesn't mean thinkpad found.
8681 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8682 static int __must_check __init get_thinkpad_model_data(
8683 struct thinkpad_id_data *tp)
8685 const struct dmi_device *dev = NULL;
8686 char ec_fw_string[18];
8687 char const *s;
8689 if (!tp)
8690 return -EINVAL;
8692 memset(tp, 0, sizeof(*tp));
8694 if (dmi_name_in_vendors("IBM"))
8695 tp->vendor = PCI_VENDOR_ID_IBM;
8696 else if (dmi_name_in_vendors("LENOVO"))
8697 tp->vendor = PCI_VENDOR_ID_LENOVO;
8698 else
8699 return 0;
8701 s = dmi_get_system_info(DMI_BIOS_VERSION);
8702 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8703 if (s && !tp->bios_version_str)
8704 return -ENOMEM;
8706 /* Really ancient ThinkPad 240X will fail this, which is fine */
8707 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8708 return 0;
8710 tp->bios_model = tp->bios_version_str[0]
8711 | (tp->bios_version_str[1] << 8);
8712 tp->bios_release = (tp->bios_version_str[4] << 8)
8713 | tp->bios_version_str[5];
8716 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8717 * X32 or newer, all Z series; Some models must have an
8718 * up-to-date BIOS or they will not be detected.
8720 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8722 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8723 if (sscanf(dev->name,
8724 "IBM ThinkPad Embedded Controller -[%17c",
8725 ec_fw_string) == 1) {
8726 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8727 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8729 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8730 if (!tp->ec_version_str)
8731 return -ENOMEM;
8733 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8734 tp->ec_model = ec_fw_string[0]
8735 | (ec_fw_string[1] << 8);
8736 tp->ec_release = (ec_fw_string[4] << 8)
8737 | ec_fw_string[5];
8738 } else {
8739 printk(TPACPI_NOTICE
8740 "ThinkPad firmware release %s "
8741 "doesn't match the known patterns\n",
8742 ec_fw_string);
8743 printk(TPACPI_NOTICE
8744 "please report this to %s\n",
8745 TPACPI_MAIL);
8747 break;
8751 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8752 if (s && !strnicmp(s, "ThinkPad", 8)) {
8753 tp->model_str = kstrdup(s, GFP_KERNEL);
8754 if (!tp->model_str)
8755 return -ENOMEM;
8758 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8759 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8760 if (s && !tp->nummodel_str)
8761 return -ENOMEM;
8763 return 0;
8766 static int __init probe_for_thinkpad(void)
8768 int is_thinkpad;
8770 if (acpi_disabled)
8771 return -ENODEV;
8773 /* It would be dangerous to run the driver in this case */
8774 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
8775 return -ENODEV;
8778 * Non-ancient models have better DMI tagging, but very old models
8779 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8781 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8782 (thinkpad_id.ec_model != 0) ||
8783 tpacpi_is_fw_known();
8785 /* The EC handler is required */
8786 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
8787 if (!ec_handle) {
8788 if (is_thinkpad)
8789 printk(TPACPI_ERR
8790 "Not yet supported ThinkPad detected!\n");
8791 return -ENODEV;
8794 if (!is_thinkpad && !force_load)
8795 return -ENODEV;
8797 return 0;
8800 static void __init thinkpad_acpi_init_banner(void)
8802 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8803 printk(TPACPI_INFO "%s\n", TPACPI_URL);
8805 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
8806 (thinkpad_id.bios_version_str) ?
8807 thinkpad_id.bios_version_str : "unknown",
8808 (thinkpad_id.ec_version_str) ?
8809 thinkpad_id.ec_version_str : "unknown");
8811 BUG_ON(!thinkpad_id.vendor);
8813 if (thinkpad_id.model_str)
8814 printk(TPACPI_INFO "%s %s, model %s\n",
8815 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8816 "IBM" : ((thinkpad_id.vendor ==
8817 PCI_VENDOR_ID_LENOVO) ?
8818 "Lenovo" : "Unknown vendor"),
8819 thinkpad_id.model_str,
8820 (thinkpad_id.nummodel_str) ?
8821 thinkpad_id.nummodel_str : "unknown");
8824 /* Module init, exit, parameters */
8826 static struct ibm_init_struct ibms_init[] __initdata = {
8828 .data = &thinkpad_acpi_driver_data,
8831 .init = hotkey_init,
8832 .data = &hotkey_driver_data,
8835 .init = bluetooth_init,
8836 .data = &bluetooth_driver_data,
8839 .init = wan_init,
8840 .data = &wan_driver_data,
8843 .init = uwb_init,
8844 .data = &uwb_driver_data,
8846 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8848 .init = video_init,
8849 .base_procfs_mode = S_IRUSR,
8850 .data = &video_driver_data,
8852 #endif
8854 .init = light_init,
8855 .data = &light_driver_data,
8858 .init = cmos_init,
8859 .data = &cmos_driver_data,
8862 .init = led_init,
8863 .data = &led_driver_data,
8866 .init = beep_init,
8867 .data = &beep_driver_data,
8870 .init = thermal_init,
8871 .data = &thermal_driver_data,
8874 .data = &ecdump_driver_data,
8877 .init = brightness_init,
8878 .data = &brightness_driver_data,
8881 .init = volume_init,
8882 .data = &volume_driver_data,
8885 .init = fan_init,
8886 .data = &fan_driver_data,
8890 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8892 unsigned int i;
8893 struct ibm_struct *ibm;
8895 if (!kp || !kp->name || !val)
8896 return -EINVAL;
8898 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8899 ibm = ibms_init[i].data;
8900 WARN_ON(ibm == NULL);
8902 if (!ibm || !ibm->name)
8903 continue;
8905 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8906 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8907 return -ENOSPC;
8908 strcpy(ibms_init[i].param, val);
8909 strcat(ibms_init[i].param, ",");
8910 return 0;
8914 return -EINVAL;
8917 module_param(experimental, int, 0444);
8918 MODULE_PARM_DESC(experimental,
8919 "Enables experimental features when non-zero");
8921 module_param_named(debug, dbg_level, uint, 0);
8922 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8924 module_param(force_load, bool, 0444);
8925 MODULE_PARM_DESC(force_load,
8926 "Attempts to load the driver even on a "
8927 "mis-identified ThinkPad when true");
8929 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8930 MODULE_PARM_DESC(fan_control,
8931 "Enables setting fan parameters features when true");
8933 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8934 MODULE_PARM_DESC(brightness_mode,
8935 "Selects brightness control strategy: "
8936 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8938 module_param(brightness_enable, uint, 0444);
8939 MODULE_PARM_DESC(brightness_enable,
8940 "Enables backlight control when 1, disables when 0");
8942 module_param(hotkey_report_mode, uint, 0444);
8943 MODULE_PARM_DESC(hotkey_report_mode,
8944 "used for backwards compatibility with userspace, "
8945 "see documentation");
8947 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8948 module_param_named(volume_mode, volume_mode, uint, 0444);
8949 MODULE_PARM_DESC(volume_mode,
8950 "Selects volume control strategy: "
8951 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8953 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8954 MODULE_PARM_DESC(volume_capabilities,
8955 "Selects the mixer capabilites: "
8956 "0=auto, 1=volume and mute, 2=mute only");
8958 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8959 MODULE_PARM_DESC(volume_control,
8960 "Enables software override for the console audio "
8961 "control when true");
8963 /* ALSA module API parameters */
8964 module_param_named(index, alsa_index, int, 0444);
8965 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8966 module_param_named(id, alsa_id, charp, 0444);
8967 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8968 module_param_named(enable, alsa_enable, bool, 0444);
8969 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8970 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8972 #define TPACPI_PARAM(feature) \
8973 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8974 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8975 "at module load, see documentation")
8977 TPACPI_PARAM(hotkey);
8978 TPACPI_PARAM(bluetooth);
8979 TPACPI_PARAM(video);
8980 TPACPI_PARAM(light);
8981 TPACPI_PARAM(cmos);
8982 TPACPI_PARAM(led);
8983 TPACPI_PARAM(beep);
8984 TPACPI_PARAM(ecdump);
8985 TPACPI_PARAM(brightness);
8986 TPACPI_PARAM(volume);
8987 TPACPI_PARAM(fan);
8989 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8990 module_param(dbg_wlswemul, uint, 0444);
8991 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8992 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8993 MODULE_PARM_DESC(wlsw_state,
8994 "Initial state of the emulated WLSW switch");
8996 module_param(dbg_bluetoothemul, uint, 0444);
8997 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8998 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8999 MODULE_PARM_DESC(bluetooth_state,
9000 "Initial state of the emulated bluetooth switch");
9002 module_param(dbg_wwanemul, uint, 0444);
9003 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9004 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9005 MODULE_PARM_DESC(wwan_state,
9006 "Initial state of the emulated WWAN switch");
9008 module_param(dbg_uwbemul, uint, 0444);
9009 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9010 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9011 MODULE_PARM_DESC(uwb_state,
9012 "Initial state of the emulated UWB switch");
9013 #endif
9015 static void thinkpad_acpi_module_exit(void)
9017 struct ibm_struct *ibm, *itmp;
9019 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9021 list_for_each_entry_safe_reverse(ibm, itmp,
9022 &tpacpi_all_drivers,
9023 all_drivers) {
9024 ibm_exit(ibm);
9027 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9029 if (tpacpi_inputdev) {
9030 if (tp_features.input_device_registered)
9031 input_unregister_device(tpacpi_inputdev);
9032 else
9033 input_free_device(tpacpi_inputdev);
9036 if (tpacpi_hwmon)
9037 hwmon_device_unregister(tpacpi_hwmon);
9039 if (tp_features.sensors_pdev_attrs_registered)
9040 device_remove_file(&tpacpi_sensors_pdev->dev,
9041 &dev_attr_thinkpad_acpi_pdev_name);
9042 if (tpacpi_sensors_pdev)
9043 platform_device_unregister(tpacpi_sensors_pdev);
9044 if (tpacpi_pdev)
9045 platform_device_unregister(tpacpi_pdev);
9047 if (tp_features.sensors_pdrv_attrs_registered)
9048 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9049 if (tp_features.platform_drv_attrs_registered)
9050 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9052 if (tp_features.sensors_pdrv_registered)
9053 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9055 if (tp_features.platform_drv_registered)
9056 platform_driver_unregister(&tpacpi_pdriver);
9058 if (proc_dir)
9059 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9061 if (tpacpi_wq)
9062 destroy_workqueue(tpacpi_wq);
9064 kfree(thinkpad_id.bios_version_str);
9065 kfree(thinkpad_id.ec_version_str);
9066 kfree(thinkpad_id.model_str);
9070 static int __init thinkpad_acpi_module_init(void)
9072 int ret, i;
9074 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9076 /* Parameter checking */
9077 if (hotkey_report_mode > 2)
9078 return -EINVAL;
9080 /* Driver-level probe */
9082 ret = get_thinkpad_model_data(&thinkpad_id);
9083 if (ret) {
9084 printk(TPACPI_ERR
9085 "unable to get DMI data: %d\n", ret);
9086 thinkpad_acpi_module_exit();
9087 return ret;
9089 ret = probe_for_thinkpad();
9090 if (ret) {
9091 thinkpad_acpi_module_exit();
9092 return ret;
9095 /* Driver initialization */
9097 thinkpad_acpi_init_banner();
9098 tpacpi_check_outdated_fw();
9100 TPACPI_ACPIHANDLE_INIT(ecrd);
9101 TPACPI_ACPIHANDLE_INIT(ecwr);
9103 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9104 if (!tpacpi_wq) {
9105 thinkpad_acpi_module_exit();
9106 return -ENOMEM;
9109 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9110 if (!proc_dir) {
9111 printk(TPACPI_ERR
9112 "unable to create proc dir " TPACPI_PROC_DIR);
9113 thinkpad_acpi_module_exit();
9114 return -ENODEV;
9117 ret = platform_driver_register(&tpacpi_pdriver);
9118 if (ret) {
9119 printk(TPACPI_ERR
9120 "unable to register main platform driver\n");
9121 thinkpad_acpi_module_exit();
9122 return ret;
9124 tp_features.platform_drv_registered = 1;
9126 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9127 if (ret) {
9128 printk(TPACPI_ERR
9129 "unable to register hwmon platform driver\n");
9130 thinkpad_acpi_module_exit();
9131 return ret;
9133 tp_features.sensors_pdrv_registered = 1;
9135 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9136 if (!ret) {
9137 tp_features.platform_drv_attrs_registered = 1;
9138 ret = tpacpi_create_driver_attributes(
9139 &tpacpi_hwmon_pdriver.driver);
9141 if (ret) {
9142 printk(TPACPI_ERR
9143 "unable to create sysfs driver attributes\n");
9144 thinkpad_acpi_module_exit();
9145 return ret;
9147 tp_features.sensors_pdrv_attrs_registered = 1;
9150 /* Device initialization */
9151 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9152 NULL, 0);
9153 if (IS_ERR(tpacpi_pdev)) {
9154 ret = PTR_ERR(tpacpi_pdev);
9155 tpacpi_pdev = NULL;
9156 printk(TPACPI_ERR "unable to register platform device\n");
9157 thinkpad_acpi_module_exit();
9158 return ret;
9160 tpacpi_sensors_pdev = platform_device_register_simple(
9161 TPACPI_HWMON_DRVR_NAME,
9162 -1, NULL, 0);
9163 if (IS_ERR(tpacpi_sensors_pdev)) {
9164 ret = PTR_ERR(tpacpi_sensors_pdev);
9165 tpacpi_sensors_pdev = NULL;
9166 printk(TPACPI_ERR
9167 "unable to register hwmon platform device\n");
9168 thinkpad_acpi_module_exit();
9169 return ret;
9171 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9172 &dev_attr_thinkpad_acpi_pdev_name);
9173 if (ret) {
9174 printk(TPACPI_ERR
9175 "unable to create sysfs hwmon device attributes\n");
9176 thinkpad_acpi_module_exit();
9177 return ret;
9179 tp_features.sensors_pdev_attrs_registered = 1;
9180 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9181 if (IS_ERR(tpacpi_hwmon)) {
9182 ret = PTR_ERR(tpacpi_hwmon);
9183 tpacpi_hwmon = NULL;
9184 printk(TPACPI_ERR "unable to register hwmon device\n");
9185 thinkpad_acpi_module_exit();
9186 return ret;
9188 mutex_init(&tpacpi_inputdev_send_mutex);
9189 tpacpi_inputdev = input_allocate_device();
9190 if (!tpacpi_inputdev) {
9191 printk(TPACPI_ERR "unable to allocate input device\n");
9192 thinkpad_acpi_module_exit();
9193 return -ENOMEM;
9194 } else {
9195 /* Prepare input device, but don't register */
9196 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9197 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9198 tpacpi_inputdev->id.bustype = BUS_HOST;
9199 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9200 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9201 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9202 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9205 /* Init subdriver dependencies */
9206 tpacpi_detect_brightness_capabilities();
9208 /* Init subdrivers */
9209 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9210 ret = ibm_init(&ibms_init[i]);
9211 if (ret >= 0 && *ibms_init[i].param)
9212 ret = ibms_init[i].data->write(ibms_init[i].param);
9213 if (ret < 0) {
9214 thinkpad_acpi_module_exit();
9215 return ret;
9219 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9221 ret = input_register_device(tpacpi_inputdev);
9222 if (ret < 0) {
9223 printk(TPACPI_ERR "unable to register input device\n");
9224 thinkpad_acpi_module_exit();
9225 return ret;
9226 } else {
9227 tp_features.input_device_registered = 1;
9230 return 0;
9233 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9236 * This will autoload the driver in almost every ThinkPad
9237 * in widespread use.
9239 * Only _VERY_ old models, like the 240, 240x and 570 lack
9240 * the HKEY event interface.
9242 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9245 * DMI matching for module autoloading
9247 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9248 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9250 * Only models listed in thinkwiki will be supported, so add yours
9251 * if it is not there yet.
9253 #define IBM_BIOS_MODULE_ALIAS(__type) \
9254 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9256 /* Ancient thinkpad BIOSes have to be identified by
9257 * BIOS type or model number, and there are far less
9258 * BIOS types than model numbers... */
9259 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9261 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9262 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9263 MODULE_DESCRIPTION(TPACPI_DESC);
9264 MODULE_VERSION(TPACPI_VERSION);
9265 MODULE_LICENSE("GPL");
9267 module_init(thinkpad_acpi_module_init);
9268 module_exit(thinkpad_acpi_module_exit);