ACPI: thinkpad-acpi: add development version tag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobd5d362e6f7e85bffc84b5a47508f3d47b9ef8b8e
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-20100411"
25 #define TPACPI_SYSFS_VERSION 0x020700
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/seq_file.h>
65 #include <linux/sysfs.h>
66 #include <linux/backlight.h>
67 #include <linux/fb.h>
68 #include <linux/platform_device.h>
69 #include <linux/hwmon.h>
70 #include <linux/hwmon-sysfs.h>
71 #include <linux/input.h>
72 #include <linux/leds.h>
73 #include <linux/rfkill.h>
74 #include <asm/uaccess.h>
76 #include <linux/dmi.h>
77 #include <linux/jiffies.h>
78 #include <linux/workqueue.h>
80 #include <sound/core.h>
81 #include <sound/control.h>
82 #include <sound/initval.h>
84 #include <acpi/acpi_drivers.h>
86 #include <linux/pci_ids.h>
89 /* ThinkPad CMOS commands */
90 #define TP_CMOS_VOLUME_DOWN 0
91 #define TP_CMOS_VOLUME_UP 1
92 #define TP_CMOS_VOLUME_MUTE 2
93 #define TP_CMOS_BRIGHTNESS_UP 4
94 #define TP_CMOS_BRIGHTNESS_DOWN 5
95 #define TP_CMOS_THINKLIGHT_ON 12
96 #define TP_CMOS_THINKLIGHT_OFF 13
98 /* NVRAM Addresses */
99 enum tp_nvram_addr {
100 TP_NVRAM_ADDR_HK2 = 0x57,
101 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
102 TP_NVRAM_ADDR_VIDEO = 0x59,
103 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
104 TP_NVRAM_ADDR_MIXER = 0x60,
107 /* NVRAM bit masks */
108 enum {
109 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
110 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
111 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
112 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
113 TP_NVRAM_MASK_THINKLIGHT = 0x10,
114 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
115 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
116 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
117 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
118 TP_NVRAM_MASK_MUTE = 0x40,
119 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
120 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
121 TP_NVRAM_POS_LEVEL_VOLUME = 0,
124 /* Misc NVRAM-related */
125 enum {
126 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
129 /* ACPI HIDs */
130 #define TPACPI_ACPI_HKEY_HID "IBM0068"
131 #define TPACPI_ACPI_EC_HID "PNP0C09"
133 /* Input IDs */
134 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
135 #define TPACPI_HKEY_INPUT_VERSION 0x4101
137 /* ACPI \WGSV commands */
138 enum {
139 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
140 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
141 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
142 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
145 /* TP_ACPI_WGSV_GET_STATE bits */
146 enum {
147 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
148 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
149 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
150 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
151 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
152 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
153 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
154 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
155 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
156 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
159 /* HKEY events */
160 enum tpacpi_hkey_event_t {
161 /* Hotkey-related */
162 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
163 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
164 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
165 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
166 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
167 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
169 /* Reasons for waking up from S3/S4 */
170 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
171 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
172 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
173 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
174 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
175 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
177 /* Auto-sleep after eject request */
178 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
179 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
181 /* Misc bay events */
182 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
184 /* User-interface events */
185 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
186 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
187 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
188 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
189 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
190 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
191 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
193 /* Thermal events */
194 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
195 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
196 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
197 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
198 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
200 /* Misc */
201 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
204 /****************************************************************************
205 * Main driver
208 #define TPACPI_NAME "thinkpad"
209 #define TPACPI_DESC "ThinkPad ACPI Extras"
210 #define TPACPI_FILE TPACPI_NAME "_acpi"
211 #define TPACPI_URL "http://ibm-acpi.sf.net/"
212 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
214 #define TPACPI_PROC_DIR "ibm"
215 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
216 #define TPACPI_DRVR_NAME TPACPI_FILE
217 #define TPACPI_DRVR_SHORTNAME "tpacpi"
218 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
220 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
221 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
223 #define TPACPI_MAX_ACPI_ARGS 3
225 /* printk headers */
226 #define TPACPI_LOG TPACPI_FILE ": "
227 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
228 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
229 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
230 #define TPACPI_ERR KERN_ERR TPACPI_LOG
231 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
232 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
233 #define TPACPI_INFO KERN_INFO TPACPI_LOG
234 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
236 /* Debugging printk groups */
237 #define TPACPI_DBG_ALL 0xffff
238 #define TPACPI_DBG_DISCLOSETASK 0x8000
239 #define TPACPI_DBG_INIT 0x0001
240 #define TPACPI_DBG_EXIT 0x0002
241 #define TPACPI_DBG_RFKILL 0x0004
242 #define TPACPI_DBG_HKEY 0x0008
243 #define TPACPI_DBG_FAN 0x0010
244 #define TPACPI_DBG_BRGHT 0x0020
245 #define TPACPI_DBG_MIXER 0x0040
247 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
248 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
249 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
252 /****************************************************************************
253 * Driver-wide structs and misc. variables
256 struct ibm_struct;
258 struct tp_acpi_drv_struct {
259 const struct acpi_device_id *hid;
260 struct acpi_driver *driver;
262 void (*notify) (struct ibm_struct *, u32);
263 acpi_handle *handle;
264 u32 type;
265 struct acpi_device *device;
268 struct ibm_struct {
269 char *name;
271 int (*read) (struct seq_file *);
272 int (*write) (char *);
273 void (*exit) (void);
274 void (*resume) (void);
275 void (*suspend) (pm_message_t state);
276 void (*shutdown) (void);
278 struct list_head all_drivers;
280 struct tp_acpi_drv_struct *acpi;
282 struct {
283 u8 acpi_driver_registered:1;
284 u8 acpi_notify_installed:1;
285 u8 proc_created:1;
286 u8 init_called:1;
287 u8 experimental:1;
288 } flags;
291 struct ibm_init_struct {
292 char param[32];
294 int (*init) (struct ibm_init_struct *);
295 mode_t base_procfs_mode;
296 struct ibm_struct *data;
299 static struct {
300 u32 bluetooth:1;
301 u32 hotkey:1;
302 u32 hotkey_mask:1;
303 u32 hotkey_wlsw:1;
304 u32 hotkey_tablet:1;
305 u32 light:1;
306 u32 light_status:1;
307 u32 bright_acpimode:1;
308 u32 bright_unkfw:1;
309 u32 wan:1;
310 u32 uwb:1;
311 u32 fan_ctrl_status_undef:1;
312 u32 second_fan:1;
313 u32 beep_needs_two_args:1;
314 u32 mixer_no_level_control:1;
315 u32 input_device_registered:1;
316 u32 platform_drv_registered:1;
317 u32 platform_drv_attrs_registered:1;
318 u32 sensors_pdrv_registered:1;
319 u32 sensors_pdrv_attrs_registered:1;
320 u32 sensors_pdev_attrs_registered:1;
321 u32 hotkey_poll_active:1;
322 } tp_features;
324 static struct {
325 u16 hotkey_mask_ff:1;
326 u16 volume_ctrl_forbidden:1;
327 } tp_warned;
329 struct thinkpad_id_data {
330 unsigned int vendor; /* ThinkPad vendor:
331 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
333 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
334 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
336 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
337 u16 ec_model;
338 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
339 u16 ec_release;
341 char *model_str; /* ThinkPad T43 */
342 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
344 static struct thinkpad_id_data thinkpad_id;
346 static enum {
347 TPACPI_LIFE_INIT = 0,
348 TPACPI_LIFE_RUNNING,
349 TPACPI_LIFE_EXITING,
350 } tpacpi_lifecycle;
352 static int experimental;
353 static u32 dbg_level;
355 static struct workqueue_struct *tpacpi_wq;
357 enum led_status_t {
358 TPACPI_LED_OFF = 0,
359 TPACPI_LED_ON,
360 TPACPI_LED_BLINK,
363 /* Special LED class that can defer work */
364 struct tpacpi_led_classdev {
365 struct led_classdev led_classdev;
366 struct work_struct work;
367 enum led_status_t new_state;
368 unsigned int led;
371 /* brightness level capabilities */
372 static unsigned int bright_maxlvl; /* 0 = unknown */
374 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
375 static int dbg_wlswemul;
376 static int tpacpi_wlsw_emulstate;
377 static int dbg_bluetoothemul;
378 static int tpacpi_bluetooth_emulstate;
379 static int dbg_wwanemul;
380 static int tpacpi_wwan_emulstate;
381 static int dbg_uwbemul;
382 static int tpacpi_uwb_emulstate;
383 #endif
386 /*************************************************************************
387 * Debugging helpers
390 #define dbg_printk(a_dbg_level, format, arg...) \
391 do { if (dbg_level & (a_dbg_level)) \
392 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
393 } while (0)
395 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
396 #define vdbg_printk dbg_printk
397 static const char *str_supported(int is_supported);
398 #else
399 #define vdbg_printk(a_dbg_level, format, arg...) \
400 do { } while (0)
401 #endif
403 static void tpacpi_log_usertask(const char * const what)
405 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
406 what, task_tgid_vnr(current));
409 #define tpacpi_disclose_usertask(what, format, arg...) \
410 do { \
411 if (unlikely( \
412 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
413 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
414 printk(TPACPI_DEBUG "%s: PID %d: " format, \
415 what, task_tgid_vnr(current), ## arg); \
417 } while (0)
420 * Quirk handling helpers
422 * ThinkPad IDs and versions seen in the field so far
423 * are two-characters from the set [0-9A-Z], i.e. base 36.
425 * We use values well outside that range as specials.
428 #define TPACPI_MATCH_ANY 0xffffU
429 #define TPACPI_MATCH_UNKNOWN 0U
431 /* TPID('1', 'Y') == 0x5931 */
432 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
434 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
435 { .vendor = PCI_VENDOR_ID_IBM, \
436 .bios = TPID(__id1, __id2), \
437 .ec = TPACPI_MATCH_ANY, \
438 .quirks = (__quirk) }
440 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
441 { .vendor = PCI_VENDOR_ID_LENOVO, \
442 .bios = TPID(__id1, __id2), \
443 .ec = TPACPI_MATCH_ANY, \
444 .quirks = (__quirk) }
446 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
447 { .vendor = PCI_VENDOR_ID_LENOVO, \
448 .bios = TPACPI_MATCH_ANY, \
449 .ec = TPID(__id1, __id2), \
450 .quirks = (__quirk) }
452 struct tpacpi_quirk {
453 unsigned int vendor;
454 u16 bios;
455 u16 ec;
456 unsigned long quirks;
460 * tpacpi_check_quirks() - search BIOS/EC version on a list
461 * @qlist: array of &struct tpacpi_quirk
462 * @qlist_size: number of elements in @qlist
464 * Iterates over a quirks list until one is found that matches the
465 * ThinkPad's vendor, BIOS and EC model.
467 * Returns 0 if nothing matches, otherwise returns the quirks field of
468 * the matching &struct tpacpi_quirk entry.
470 * The match criteria is: vendor, ec and bios much match.
472 static unsigned long __init tpacpi_check_quirks(
473 const struct tpacpi_quirk *qlist,
474 unsigned int qlist_size)
476 while (qlist_size) {
477 if ((qlist->vendor == thinkpad_id.vendor ||
478 qlist->vendor == TPACPI_MATCH_ANY) &&
479 (qlist->bios == thinkpad_id.bios_model ||
480 qlist->bios == TPACPI_MATCH_ANY) &&
481 (qlist->ec == thinkpad_id.ec_model ||
482 qlist->ec == TPACPI_MATCH_ANY))
483 return qlist->quirks;
485 qlist_size--;
486 qlist++;
488 return 0;
492 /****************************************************************************
493 ****************************************************************************
495 * ACPI Helpers and device model
497 ****************************************************************************
498 ****************************************************************************/
500 /*************************************************************************
501 * ACPI basic handles
504 static acpi_handle root_handle;
505 static acpi_handle ec_handle;
507 #define TPACPI_HANDLE(object, parent, paths...) \
508 static acpi_handle object##_handle; \
509 static const acpi_handle *object##_parent __initdata = \
510 &parent##_handle; \
511 static char *object##_paths[] __initdata = { paths }
513 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
514 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
516 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
517 /* T4x, X31, X40 */
518 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
519 "\\CMS", /* R40, R40e */
520 ); /* all others */
522 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
523 "^HKEY", /* R30, R31 */
524 "HKEY", /* all others */
525 ); /* 570 */
527 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
528 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
529 "\\_SB.PCI0.VID0", /* 770e */
530 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
531 "\\_SB.PCI0.AGP.VID", /* all others */
532 ); /* R30, R31 */
535 /*************************************************************************
536 * ACPI helpers
539 static int acpi_evalf(acpi_handle handle,
540 void *res, char *method, char *fmt, ...)
542 char *fmt0 = fmt;
543 struct acpi_object_list params;
544 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
545 struct acpi_buffer result, *resultp;
546 union acpi_object out_obj;
547 acpi_status status;
548 va_list ap;
549 char res_type;
550 int success;
551 int quiet;
553 if (!*fmt) {
554 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
555 return 0;
558 if (*fmt == 'q') {
559 quiet = 1;
560 fmt++;
561 } else
562 quiet = 0;
564 res_type = *(fmt++);
566 params.count = 0;
567 params.pointer = &in_objs[0];
569 va_start(ap, fmt);
570 while (*fmt) {
571 char c = *(fmt++);
572 switch (c) {
573 case 'd': /* int */
574 in_objs[params.count].integer.value = va_arg(ap, int);
575 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
576 break;
577 /* add more types as needed */
578 default:
579 printk(TPACPI_ERR "acpi_evalf() called "
580 "with invalid format character '%c'\n", c);
581 return 0;
584 va_end(ap);
586 if (res_type != 'v') {
587 result.length = sizeof(out_obj);
588 result.pointer = &out_obj;
589 resultp = &result;
590 } else
591 resultp = NULL;
593 status = acpi_evaluate_object(handle, method, &params, resultp);
595 switch (res_type) {
596 case 'd': /* int */
597 success = (status == AE_OK &&
598 out_obj.type == ACPI_TYPE_INTEGER);
599 if (success && res)
600 *(int *)res = out_obj.integer.value;
601 break;
602 case 'v': /* void */
603 success = status == AE_OK;
604 break;
605 /* add more types as needed */
606 default:
607 printk(TPACPI_ERR "acpi_evalf() called "
608 "with invalid format character '%c'\n", res_type);
609 return 0;
612 if (!success && !quiet)
613 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %s\n",
614 method, fmt0, acpi_format_exception(status));
616 return success;
619 static int acpi_ec_read(int i, u8 *p)
621 int v;
623 if (ecrd_handle) {
624 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
625 return 0;
626 *p = v;
627 } else {
628 if (ec_read(i, p) < 0)
629 return 0;
632 return 1;
635 static int acpi_ec_write(int i, u8 v)
637 if (ecwr_handle) {
638 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
639 return 0;
640 } else {
641 if (ec_write(i, v) < 0)
642 return 0;
645 return 1;
648 static int issue_thinkpad_cmos_command(int cmos_cmd)
650 if (!cmos_handle)
651 return -ENXIO;
653 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
654 return -EIO;
656 return 0;
659 /*************************************************************************
660 * ACPI device model
663 #define TPACPI_ACPIHANDLE_INIT(object) \
664 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
665 object##_paths, ARRAY_SIZE(object##_paths))
667 static void __init drv_acpi_handle_init(const char *name,
668 acpi_handle *handle, const acpi_handle parent,
669 char **paths, const int num_paths)
671 int i;
672 acpi_status status;
674 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
675 name);
677 for (i = 0; i < num_paths; i++) {
678 status = acpi_get_handle(parent, paths[i], handle);
679 if (ACPI_SUCCESS(status)) {
680 dbg_printk(TPACPI_DBG_INIT,
681 "Found ACPI handle %s for %s\n",
682 paths[i], name);
683 return;
687 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
688 name);
689 *handle = NULL;
692 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
693 u32 level, void *context, void **return_value)
695 *(acpi_handle *)return_value = handle;
697 return AE_CTRL_TERMINATE;
700 static void __init tpacpi_acpi_handle_locate(const char *name,
701 const char *hid,
702 acpi_handle *handle)
704 acpi_status status;
705 acpi_handle device_found;
707 BUG_ON(!name || !hid || !handle);
708 vdbg_printk(TPACPI_DBG_INIT,
709 "trying to locate ACPI handle for %s, using HID %s\n",
710 name, hid);
712 memset(&device_found, 0, sizeof(device_found));
713 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
714 (void *)name, &device_found);
716 *handle = NULL;
718 if (ACPI_SUCCESS(status)) {
719 *handle = device_found;
720 dbg_printk(TPACPI_DBG_INIT,
721 "Found ACPI handle for %s\n", name);
722 } else {
723 vdbg_printk(TPACPI_DBG_INIT,
724 "Could not locate an ACPI handle for %s: %s\n",
725 name, acpi_format_exception(status));
729 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
731 struct ibm_struct *ibm = data;
733 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
734 return;
736 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
737 return;
739 ibm->acpi->notify(ibm, event);
742 static int __init setup_acpi_notify(struct ibm_struct *ibm)
744 acpi_status status;
745 int rc;
747 BUG_ON(!ibm->acpi);
749 if (!*ibm->acpi->handle)
750 return 0;
752 vdbg_printk(TPACPI_DBG_INIT,
753 "setting up ACPI notify for %s\n", ibm->name);
755 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
756 if (rc < 0) {
757 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
758 ibm->name, rc);
759 return -ENODEV;
762 ibm->acpi->device->driver_data = ibm;
763 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
764 TPACPI_ACPI_EVENT_PREFIX,
765 ibm->name);
767 status = acpi_install_notify_handler(*ibm->acpi->handle,
768 ibm->acpi->type, dispatch_acpi_notify, ibm);
769 if (ACPI_FAILURE(status)) {
770 if (status == AE_ALREADY_EXISTS) {
771 printk(TPACPI_NOTICE
772 "another device driver is already "
773 "handling %s events\n", ibm->name);
774 } else {
775 printk(TPACPI_ERR
776 "acpi_install_notify_handler(%s) failed: %s\n",
777 ibm->name, acpi_format_exception(status));
779 return -ENODEV;
781 ibm->flags.acpi_notify_installed = 1;
782 return 0;
785 static int __init tpacpi_device_add(struct acpi_device *device)
787 return 0;
790 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
792 int rc;
794 dbg_printk(TPACPI_DBG_INIT,
795 "registering %s as an ACPI driver\n", ibm->name);
797 BUG_ON(!ibm->acpi);
799 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
800 if (!ibm->acpi->driver) {
801 printk(TPACPI_ERR
802 "failed to allocate memory for ibm->acpi->driver\n");
803 return -ENOMEM;
806 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
807 ibm->acpi->driver->ids = ibm->acpi->hid;
809 ibm->acpi->driver->ops.add = &tpacpi_device_add;
811 rc = acpi_bus_register_driver(ibm->acpi->driver);
812 if (rc < 0) {
813 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
814 ibm->name, rc);
815 kfree(ibm->acpi->driver);
816 ibm->acpi->driver = NULL;
817 } else if (!rc)
818 ibm->flags.acpi_driver_registered = 1;
820 return rc;
824 /****************************************************************************
825 ****************************************************************************
827 * Procfs Helpers
829 ****************************************************************************
830 ****************************************************************************/
832 static int dispatch_proc_show(struct seq_file *m, void *v)
834 struct ibm_struct *ibm = m->private;
836 if (!ibm || !ibm->read)
837 return -EINVAL;
838 return ibm->read(m);
841 static int dispatch_proc_open(struct inode *inode, struct file *file)
843 return single_open(file, dispatch_proc_show, PDE(inode)->data);
846 static ssize_t dispatch_proc_write(struct file *file,
847 const char __user *userbuf,
848 size_t count, loff_t *pos)
850 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
851 char *kernbuf;
852 int ret;
854 if (!ibm || !ibm->write)
855 return -EINVAL;
856 if (count > PAGE_SIZE - 2)
857 return -EINVAL;
859 kernbuf = kmalloc(count + 2, GFP_KERNEL);
860 if (!kernbuf)
861 return -ENOMEM;
863 if (copy_from_user(kernbuf, userbuf, count)) {
864 kfree(kernbuf);
865 return -EFAULT;
868 kernbuf[count] = 0;
869 strcat(kernbuf, ",");
870 ret = ibm->write(kernbuf);
871 if (ret == 0)
872 ret = count;
874 kfree(kernbuf);
876 return ret;
879 static const struct file_operations dispatch_proc_fops = {
880 .owner = THIS_MODULE,
881 .open = dispatch_proc_open,
882 .read = seq_read,
883 .llseek = seq_lseek,
884 .release = single_release,
885 .write = dispatch_proc_write,
888 static char *next_cmd(char **cmds)
890 char *start = *cmds;
891 char *end;
893 while ((end = strchr(start, ',')) && end == start)
894 start = end + 1;
896 if (!end)
897 return NULL;
899 *end = 0;
900 *cmds = end + 1;
901 return start;
905 /****************************************************************************
906 ****************************************************************************
908 * Device model: input, hwmon and platform
910 ****************************************************************************
911 ****************************************************************************/
913 static struct platform_device *tpacpi_pdev;
914 static struct platform_device *tpacpi_sensors_pdev;
915 static struct device *tpacpi_hwmon;
916 static struct input_dev *tpacpi_inputdev;
917 static struct mutex tpacpi_inputdev_send_mutex;
918 static LIST_HEAD(tpacpi_all_drivers);
920 static int tpacpi_suspend_handler(struct platform_device *pdev,
921 pm_message_t state)
923 struct ibm_struct *ibm, *itmp;
925 list_for_each_entry_safe(ibm, itmp,
926 &tpacpi_all_drivers,
927 all_drivers) {
928 if (ibm->suspend)
929 (ibm->suspend)(state);
932 return 0;
935 static int tpacpi_resume_handler(struct platform_device *pdev)
937 struct ibm_struct *ibm, *itmp;
939 list_for_each_entry_safe(ibm, itmp,
940 &tpacpi_all_drivers,
941 all_drivers) {
942 if (ibm->resume)
943 (ibm->resume)();
946 return 0;
949 static void tpacpi_shutdown_handler(struct platform_device *pdev)
951 struct ibm_struct *ibm, *itmp;
953 list_for_each_entry_safe(ibm, itmp,
954 &tpacpi_all_drivers,
955 all_drivers) {
956 if (ibm->shutdown)
957 (ibm->shutdown)();
961 static struct platform_driver tpacpi_pdriver = {
962 .driver = {
963 .name = TPACPI_DRVR_NAME,
964 .owner = THIS_MODULE,
966 .suspend = tpacpi_suspend_handler,
967 .resume = tpacpi_resume_handler,
968 .shutdown = tpacpi_shutdown_handler,
971 static struct platform_driver tpacpi_hwmon_pdriver = {
972 .driver = {
973 .name = TPACPI_HWMON_DRVR_NAME,
974 .owner = THIS_MODULE,
978 /*************************************************************************
979 * sysfs support helpers
982 struct attribute_set {
983 unsigned int members, max_members;
984 struct attribute_group group;
987 struct attribute_set_obj {
988 struct attribute_set s;
989 struct attribute *a;
990 } __attribute__((packed));
992 static struct attribute_set *create_attr_set(unsigned int max_members,
993 const char *name)
995 struct attribute_set_obj *sobj;
997 if (max_members == 0)
998 return NULL;
1000 /* Allocates space for implicit NULL at the end too */
1001 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1002 max_members * sizeof(struct attribute *),
1003 GFP_KERNEL);
1004 if (!sobj)
1005 return NULL;
1006 sobj->s.max_members = max_members;
1007 sobj->s.group.attrs = &sobj->a;
1008 sobj->s.group.name = name;
1010 return &sobj->s;
1013 #define destroy_attr_set(_set) \
1014 kfree(_set);
1016 /* not multi-threaded safe, use it in a single thread per set */
1017 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1019 if (!s || !attr)
1020 return -EINVAL;
1022 if (s->members >= s->max_members)
1023 return -ENOMEM;
1025 s->group.attrs[s->members] = attr;
1026 s->members++;
1028 return 0;
1031 static int add_many_to_attr_set(struct attribute_set *s,
1032 struct attribute **attr,
1033 unsigned int count)
1035 int i, res;
1037 for (i = 0; i < count; i++) {
1038 res = add_to_attr_set(s, attr[i]);
1039 if (res)
1040 return res;
1043 return 0;
1046 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1048 sysfs_remove_group(kobj, &s->group);
1049 destroy_attr_set(s);
1052 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1053 sysfs_create_group(_kobj, &_attr_set->group)
1055 static int parse_strtoul(const char *buf,
1056 unsigned long max, unsigned long *value)
1058 char *endp;
1060 while (*buf && isspace(*buf))
1061 buf++;
1062 *value = simple_strtoul(buf, &endp, 0);
1063 while (*endp && isspace(*endp))
1064 endp++;
1065 if (*endp || *value > max)
1066 return -EINVAL;
1068 return 0;
1071 static void tpacpi_disable_brightness_delay(void)
1073 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1074 printk(TPACPI_NOTICE
1075 "ACPI backlight control delay disabled\n");
1078 static void printk_deprecated_attribute(const char * const what,
1079 const char * const details)
1081 tpacpi_log_usertask("deprecated sysfs attribute");
1082 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1083 "will be removed. %s\n",
1084 what, details);
1087 /*************************************************************************
1088 * rfkill and radio control support helpers
1092 * ThinkPad-ACPI firmware handling model:
1094 * WLSW (master wireless switch) is event-driven, and is common to all
1095 * firmware-controlled radios. It cannot be controlled, just monitored,
1096 * as expected. It overrides all radio state in firmware
1098 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1099 * (TODO: verify how WLSW interacts with the returned radio state).
1101 * The only time there are shadow radio state changes, is when
1102 * masked-off hotkeys are used.
1106 * Internal driver API for radio state:
1108 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1109 * bool: true means radio blocked (off)
1111 enum tpacpi_rfkill_state {
1112 TPACPI_RFK_RADIO_OFF = 0,
1113 TPACPI_RFK_RADIO_ON
1116 /* rfkill switches */
1117 enum tpacpi_rfk_id {
1118 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1119 TPACPI_RFK_WWAN_SW_ID,
1120 TPACPI_RFK_UWB_SW_ID,
1121 TPACPI_RFK_SW_MAX
1124 static const char *tpacpi_rfkill_names[] = {
1125 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1126 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1127 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1128 [TPACPI_RFK_SW_MAX] = NULL
1131 /* ThinkPad-ACPI rfkill subdriver */
1132 struct tpacpi_rfk {
1133 struct rfkill *rfkill;
1134 enum tpacpi_rfk_id id;
1135 const struct tpacpi_rfk_ops *ops;
1138 struct tpacpi_rfk_ops {
1139 /* firmware interface */
1140 int (*get_status)(void);
1141 int (*set_status)(const enum tpacpi_rfkill_state);
1144 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1146 /* Query FW and update rfkill sw state for a given rfkill switch */
1147 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1149 int status;
1151 if (!tp_rfk)
1152 return -ENODEV;
1154 status = (tp_rfk->ops->get_status)();
1155 if (status < 0)
1156 return status;
1158 rfkill_set_sw_state(tp_rfk->rfkill,
1159 (status == TPACPI_RFK_RADIO_OFF));
1161 return status;
1164 /* Query FW and update rfkill sw state for all rfkill switches */
1165 static void tpacpi_rfk_update_swstate_all(void)
1167 unsigned int i;
1169 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1170 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1174 * Sync the HW-blocking state of all rfkill switches,
1175 * do notice it causes the rfkill core to schedule uevents
1177 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1179 unsigned int i;
1180 struct tpacpi_rfk *tp_rfk;
1182 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1183 tp_rfk = tpacpi_rfkill_switches[i];
1184 if (tp_rfk) {
1185 if (rfkill_set_hw_state(tp_rfk->rfkill,
1186 blocked)) {
1187 /* ignore -- we track sw block */
1193 /* Call to get the WLSW state from the firmware */
1194 static int hotkey_get_wlsw(void);
1196 /* Call to query WLSW state and update all rfkill switches */
1197 static bool tpacpi_rfk_check_hwblock_state(void)
1199 int res = hotkey_get_wlsw();
1200 int hw_blocked;
1202 /* When unknown or unsupported, we have to assume it is unblocked */
1203 if (res < 0)
1204 return false;
1206 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1207 tpacpi_rfk_update_hwblock_state(hw_blocked);
1209 return hw_blocked;
1212 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1214 struct tpacpi_rfk *tp_rfk = data;
1215 int res;
1217 dbg_printk(TPACPI_DBG_RFKILL,
1218 "request to change radio state to %s\n",
1219 blocked ? "blocked" : "unblocked");
1221 /* try to set radio state */
1222 res = (tp_rfk->ops->set_status)(blocked ?
1223 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1225 /* and update the rfkill core with whatever the FW really did */
1226 tpacpi_rfk_update_swstate(tp_rfk);
1228 return (res < 0) ? res : 0;
1231 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1232 .set_block = tpacpi_rfk_hook_set_block,
1235 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1236 const struct tpacpi_rfk_ops *tp_rfkops,
1237 const enum rfkill_type rfktype,
1238 const char *name,
1239 const bool set_default)
1241 struct tpacpi_rfk *atp_rfk;
1242 int res;
1243 bool sw_state = false;
1244 bool hw_state;
1245 int sw_status;
1247 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1249 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1250 if (atp_rfk)
1251 atp_rfk->rfkill = rfkill_alloc(name,
1252 &tpacpi_pdev->dev,
1253 rfktype,
1254 &tpacpi_rfk_rfkill_ops,
1255 atp_rfk);
1256 if (!atp_rfk || !atp_rfk->rfkill) {
1257 printk(TPACPI_ERR
1258 "failed to allocate memory for rfkill class\n");
1259 kfree(atp_rfk);
1260 return -ENOMEM;
1263 atp_rfk->id = id;
1264 atp_rfk->ops = tp_rfkops;
1266 sw_status = (tp_rfkops->get_status)();
1267 if (sw_status < 0) {
1268 printk(TPACPI_ERR
1269 "failed to read initial state for %s, error %d\n",
1270 name, sw_status);
1271 } else {
1272 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1273 if (set_default) {
1274 /* try to keep the initial state, since we ask the
1275 * firmware to preserve it across S5 in NVRAM */
1276 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1279 hw_state = tpacpi_rfk_check_hwblock_state();
1280 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1282 res = rfkill_register(atp_rfk->rfkill);
1283 if (res < 0) {
1284 printk(TPACPI_ERR
1285 "failed to register %s rfkill switch: %d\n",
1286 name, res);
1287 rfkill_destroy(atp_rfk->rfkill);
1288 kfree(atp_rfk);
1289 return res;
1292 tpacpi_rfkill_switches[id] = atp_rfk;
1294 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1295 name, (sw_state || hw_state) ? "" : "un");
1296 return 0;
1299 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1301 struct tpacpi_rfk *tp_rfk;
1303 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1305 tp_rfk = tpacpi_rfkill_switches[id];
1306 if (tp_rfk) {
1307 rfkill_unregister(tp_rfk->rfkill);
1308 rfkill_destroy(tp_rfk->rfkill);
1309 tpacpi_rfkill_switches[id] = NULL;
1310 kfree(tp_rfk);
1314 static void printk_deprecated_rfkill_attribute(const char * const what)
1316 printk_deprecated_attribute(what,
1317 "Please switch to generic rfkill before year 2010");
1320 /* sysfs <radio> enable ------------------------------------------------ */
1321 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1322 struct device_attribute *attr,
1323 char *buf)
1325 int status;
1327 printk_deprecated_rfkill_attribute(attr->attr.name);
1329 /* This is in the ABI... */
1330 if (tpacpi_rfk_check_hwblock_state()) {
1331 status = TPACPI_RFK_RADIO_OFF;
1332 } else {
1333 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1334 if (status < 0)
1335 return status;
1338 return snprintf(buf, PAGE_SIZE, "%d\n",
1339 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1342 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1343 struct device_attribute *attr,
1344 const char *buf, size_t count)
1346 unsigned long t;
1347 int res;
1349 printk_deprecated_rfkill_attribute(attr->attr.name);
1351 if (parse_strtoul(buf, 1, &t))
1352 return -EINVAL;
1354 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1356 /* This is in the ABI... */
1357 if (tpacpi_rfk_check_hwblock_state() && !!t)
1358 return -EPERM;
1360 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1361 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1362 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1364 return (res < 0) ? res : count;
1367 /* procfs -------------------------------------------------------------- */
1368 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id,
1369 struct seq_file *m)
1371 if (id >= TPACPI_RFK_SW_MAX)
1372 seq_printf(m, "status:\t\tnot supported\n");
1373 else {
1374 int status;
1376 /* This is in the ABI... */
1377 if (tpacpi_rfk_check_hwblock_state()) {
1378 status = TPACPI_RFK_RADIO_OFF;
1379 } else {
1380 status = tpacpi_rfk_update_swstate(
1381 tpacpi_rfkill_switches[id]);
1382 if (status < 0)
1383 return status;
1386 seq_printf(m, "status:\t\t%s\n",
1387 (status == TPACPI_RFK_RADIO_ON) ?
1388 "enabled" : "disabled");
1389 seq_printf(m, "commands:\tenable, disable\n");
1392 return 0;
1395 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1397 char *cmd;
1398 int status = -1;
1399 int res = 0;
1401 if (id >= TPACPI_RFK_SW_MAX)
1402 return -ENODEV;
1404 while ((cmd = next_cmd(&buf))) {
1405 if (strlencmp(cmd, "enable") == 0)
1406 status = TPACPI_RFK_RADIO_ON;
1407 else if (strlencmp(cmd, "disable") == 0)
1408 status = TPACPI_RFK_RADIO_OFF;
1409 else
1410 return -EINVAL;
1413 if (status != -1) {
1414 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1415 (status == TPACPI_RFK_RADIO_ON) ?
1416 "enable" : "disable",
1417 tpacpi_rfkill_names[id]);
1418 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1419 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1422 return res;
1425 /*************************************************************************
1426 * thinkpad-acpi driver attributes
1429 /* interface_version --------------------------------------------------- */
1430 static ssize_t tpacpi_driver_interface_version_show(
1431 struct device_driver *drv,
1432 char *buf)
1434 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1437 static DRIVER_ATTR(interface_version, S_IRUGO,
1438 tpacpi_driver_interface_version_show, NULL);
1440 /* debug_level --------------------------------------------------------- */
1441 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1442 char *buf)
1444 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1447 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1448 const char *buf, size_t count)
1450 unsigned long t;
1452 if (parse_strtoul(buf, 0xffff, &t))
1453 return -EINVAL;
1455 dbg_level = t;
1457 return count;
1460 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1461 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1463 /* version ------------------------------------------------------------- */
1464 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1465 char *buf)
1467 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1468 TPACPI_DESC, TPACPI_VERSION);
1471 static DRIVER_ATTR(version, S_IRUGO,
1472 tpacpi_driver_version_show, NULL);
1474 /* --------------------------------------------------------------------- */
1476 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1478 /* wlsw_emulstate ------------------------------------------------------ */
1479 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1480 char *buf)
1482 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1485 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1486 const char *buf, size_t count)
1488 unsigned long t;
1490 if (parse_strtoul(buf, 1, &t))
1491 return -EINVAL;
1493 if (tpacpi_wlsw_emulstate != !!t) {
1494 tpacpi_wlsw_emulstate = !!t;
1495 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1498 return count;
1501 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1502 tpacpi_driver_wlsw_emulstate_show,
1503 tpacpi_driver_wlsw_emulstate_store);
1505 /* bluetooth_emulstate ------------------------------------------------- */
1506 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1507 struct device_driver *drv,
1508 char *buf)
1510 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1513 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1514 struct device_driver *drv,
1515 const char *buf, size_t count)
1517 unsigned long t;
1519 if (parse_strtoul(buf, 1, &t))
1520 return -EINVAL;
1522 tpacpi_bluetooth_emulstate = !!t;
1524 return count;
1527 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1528 tpacpi_driver_bluetooth_emulstate_show,
1529 tpacpi_driver_bluetooth_emulstate_store);
1531 /* wwan_emulstate ------------------------------------------------- */
1532 static ssize_t tpacpi_driver_wwan_emulstate_show(
1533 struct device_driver *drv,
1534 char *buf)
1536 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1539 static ssize_t tpacpi_driver_wwan_emulstate_store(
1540 struct device_driver *drv,
1541 const char *buf, size_t count)
1543 unsigned long t;
1545 if (parse_strtoul(buf, 1, &t))
1546 return -EINVAL;
1548 tpacpi_wwan_emulstate = !!t;
1550 return count;
1553 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1554 tpacpi_driver_wwan_emulstate_show,
1555 tpacpi_driver_wwan_emulstate_store);
1557 /* uwb_emulstate ------------------------------------------------- */
1558 static ssize_t tpacpi_driver_uwb_emulstate_show(
1559 struct device_driver *drv,
1560 char *buf)
1562 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1565 static ssize_t tpacpi_driver_uwb_emulstate_store(
1566 struct device_driver *drv,
1567 const char *buf, size_t count)
1569 unsigned long t;
1571 if (parse_strtoul(buf, 1, &t))
1572 return -EINVAL;
1574 tpacpi_uwb_emulstate = !!t;
1576 return count;
1579 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1580 tpacpi_driver_uwb_emulstate_show,
1581 tpacpi_driver_uwb_emulstate_store);
1582 #endif
1584 /* --------------------------------------------------------------------- */
1586 static struct driver_attribute *tpacpi_driver_attributes[] = {
1587 &driver_attr_debug_level, &driver_attr_version,
1588 &driver_attr_interface_version,
1591 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1593 int i, res;
1595 i = 0;
1596 res = 0;
1597 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1598 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1599 i++;
1602 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1603 if (!res && dbg_wlswemul)
1604 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1605 if (!res && dbg_bluetoothemul)
1606 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1607 if (!res && dbg_wwanemul)
1608 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1609 if (!res && dbg_uwbemul)
1610 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1611 #endif
1613 return res;
1616 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1618 int i;
1620 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1621 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1623 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1624 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1625 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1626 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1627 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1628 #endif
1631 /*************************************************************************
1632 * Firmware Data
1636 * Table of recommended minimum BIOS versions
1638 * Reasons for listing:
1639 * 1. Stable BIOS, listed because the unknown amount of
1640 * bugs and bad ACPI behaviour on older versions
1642 * 2. BIOS or EC fw with known bugs that trigger on Linux
1644 * 3. BIOS with known reduced functionality in older versions
1646 * We recommend the latest BIOS and EC version.
1647 * We only support the latest BIOS and EC fw version as a rule.
1649 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1650 * Information from users in ThinkWiki
1652 * WARNING: we use this table also to detect that the machine is
1653 * a ThinkPad in some cases, so don't remove entries lightly.
1656 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1657 { .vendor = (__v), \
1658 .bios = TPID(__id1, __id2), \
1659 .ec = TPACPI_MATCH_ANY, \
1660 .quirks = TPACPI_MATCH_ANY << 16 \
1661 | (__bv1) << 8 | (__bv2) }
1663 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1664 __eid, __ev1, __ev2) \
1665 { .vendor = (__v), \
1666 .bios = TPID(__bid1, __bid2), \
1667 .ec = __eid, \
1668 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1669 | (__bv1) << 8 | (__bv2) }
1671 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1672 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1674 /* Outdated IBM BIOSes often lack the EC id string */
1675 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1676 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1677 __bv1, __bv2, TPID(__id1, __id2), \
1678 __ev1, __ev2), \
1679 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1680 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1681 __ev1, __ev2)
1683 /* Outdated IBM BIOSes often lack the EC id string */
1684 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1685 __eid1, __eid2, __ev1, __ev2) \
1686 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1687 __bv1, __bv2, TPID(__eid1, __eid2), \
1688 __ev1, __ev2), \
1689 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1690 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1691 __ev1, __ev2)
1693 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1694 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1696 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1697 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1698 __bv1, __bv2, TPID(__id1, __id2), \
1699 __ev1, __ev2)
1701 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1702 __eid1, __eid2, __ev1, __ev2) \
1703 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1704 __bv1, __bv2, TPID(__eid1, __eid2), \
1705 __ev1, __ev2)
1707 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1708 /* Numeric models ------------------ */
1709 /* FW MODEL BIOS VERS */
1710 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1711 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1712 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1713 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1714 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1715 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1716 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1717 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1718 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1720 /* A-series ------------------------- */
1721 /* FW MODEL BIOS VERS EC VERS */
1722 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1723 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1724 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1725 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1726 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1727 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1728 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1729 TPV_QI0('1', '3', '2', '0'), /* A22m */
1730 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1731 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1732 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1734 /* G-series ------------------------- */
1735 /* FW MODEL BIOS VERS */
1736 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1737 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1739 /* R-series, T-series --------------- */
1740 /* FW MODEL BIOS VERS EC VERS */
1741 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1742 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1743 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1744 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1745 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1746 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1747 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1748 T40/p, T41/p, T42/p (1) */
1749 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1750 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1751 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1752 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1754 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1755 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1756 TPV_QI0('1', '6', '3', '2'), /* T22 */
1757 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1758 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1759 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1761 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1762 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1763 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1765 /* BIOS FW BIOS VERS EC FW EC VERS */
1766 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1767 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1769 /* X-series ------------------------- */
1770 /* FW MODEL BIOS VERS EC VERS */
1771 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1772 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1773 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1774 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1775 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1776 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1777 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1779 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1780 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1782 /* (0) - older versions lack DMI EC fw string and functionality */
1783 /* (1) - older versions known to lack functionality */
1786 #undef TPV_QL1
1787 #undef TPV_QL0
1788 #undef TPV_QI2
1789 #undef TPV_QI1
1790 #undef TPV_QI0
1791 #undef TPV_Q_X
1792 #undef TPV_Q
1794 static void __init tpacpi_check_outdated_fw(void)
1796 unsigned long fwvers;
1797 u16 ec_version, bios_version;
1799 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1800 ARRAY_SIZE(tpacpi_bios_version_qtable));
1802 if (!fwvers)
1803 return;
1805 bios_version = fwvers & 0xffffU;
1806 ec_version = (fwvers >> 16) & 0xffffU;
1808 /* note that unknown versions are set to 0x0000 and we use that */
1809 if ((bios_version > thinkpad_id.bios_release) ||
1810 (ec_version > thinkpad_id.ec_release &&
1811 ec_version != TPACPI_MATCH_ANY)) {
1813 * The changelogs would let us track down the exact
1814 * reason, but it is just too much of a pain to track
1815 * it. We only list BIOSes that are either really
1816 * broken, or really stable to begin with, so it is
1817 * best if the user upgrades the firmware anyway.
1819 printk(TPACPI_WARN
1820 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1821 printk(TPACPI_WARN
1822 "WARNING: This firmware may be missing critical bug "
1823 "fixes and/or important features\n");
1827 static bool __init tpacpi_is_fw_known(void)
1829 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1830 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1833 /****************************************************************************
1834 ****************************************************************************
1836 * Subdrivers
1838 ****************************************************************************
1839 ****************************************************************************/
1841 /*************************************************************************
1842 * thinkpad-acpi metadata subdriver
1845 static int thinkpad_acpi_driver_read(struct seq_file *m)
1847 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1848 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1849 return 0;
1852 static struct ibm_struct thinkpad_acpi_driver_data = {
1853 .name = "driver",
1854 .read = thinkpad_acpi_driver_read,
1857 /*************************************************************************
1858 * Hotkey subdriver
1862 * ThinkPad firmware event model
1864 * The ThinkPad firmware has two main event interfaces: normal ACPI
1865 * notifications (which follow the ACPI standard), and a private event
1866 * interface.
1868 * The private event interface also issues events for the hotkeys. As
1869 * the driver gained features, the event handling code ended up being
1870 * built around the hotkey subdriver. This will need to be refactored
1871 * to a more formal event API eventually.
1873 * Some "hotkeys" are actually supposed to be used as event reports,
1874 * such as "brightness has changed", "volume has changed", depending on
1875 * the ThinkPad model and how the firmware is operating.
1877 * Unlike other classes, hotkey-class events have mask/unmask control on
1878 * non-ancient firmware. However, how it behaves changes a lot with the
1879 * firmware model and version.
1882 enum { /* hot key scan codes (derived from ACPI DSDT) */
1883 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1884 TP_ACPI_HOTKEYSCAN_FNF2,
1885 TP_ACPI_HOTKEYSCAN_FNF3,
1886 TP_ACPI_HOTKEYSCAN_FNF4,
1887 TP_ACPI_HOTKEYSCAN_FNF5,
1888 TP_ACPI_HOTKEYSCAN_FNF6,
1889 TP_ACPI_HOTKEYSCAN_FNF7,
1890 TP_ACPI_HOTKEYSCAN_FNF8,
1891 TP_ACPI_HOTKEYSCAN_FNF9,
1892 TP_ACPI_HOTKEYSCAN_FNF10,
1893 TP_ACPI_HOTKEYSCAN_FNF11,
1894 TP_ACPI_HOTKEYSCAN_FNF12,
1895 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1896 TP_ACPI_HOTKEYSCAN_FNINSERT,
1897 TP_ACPI_HOTKEYSCAN_FNDELETE,
1898 TP_ACPI_HOTKEYSCAN_FNHOME,
1899 TP_ACPI_HOTKEYSCAN_FNEND,
1900 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1901 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1902 TP_ACPI_HOTKEYSCAN_FNSPACE,
1903 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1904 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1905 TP_ACPI_HOTKEYSCAN_MUTE,
1906 TP_ACPI_HOTKEYSCAN_THINKPAD,
1909 enum { /* Keys/events available through NVRAM polling */
1910 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1911 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1914 enum { /* Positions of some of the keys in hotkey masks */
1915 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1916 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1917 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1918 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1919 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1920 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1921 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1922 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1923 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1924 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1925 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1928 enum { /* NVRAM to ACPI HKEY group map */
1929 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1930 TP_ACPI_HKEY_ZOOM_MASK |
1931 TP_ACPI_HKEY_DISPSWTCH_MASK |
1932 TP_ACPI_HKEY_HIBERNATE_MASK,
1933 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1934 TP_ACPI_HKEY_BRGHTDWN_MASK,
1935 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1936 TP_ACPI_HKEY_VOLDWN_MASK |
1937 TP_ACPI_HKEY_MUTE_MASK,
1940 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1941 struct tp_nvram_state {
1942 u16 thinkpad_toggle:1;
1943 u16 zoom_toggle:1;
1944 u16 display_toggle:1;
1945 u16 thinklight_toggle:1;
1946 u16 hibernate_toggle:1;
1947 u16 displayexp_toggle:1;
1948 u16 display_state:1;
1949 u16 brightness_toggle:1;
1950 u16 volume_toggle:1;
1951 u16 mute:1;
1953 u8 brightness_level;
1954 u8 volume_level;
1957 /* kthread for the hotkey poller */
1958 static struct task_struct *tpacpi_hotkey_task;
1960 /* Acquired while the poller kthread is running, use to sync start/stop */
1961 static struct mutex hotkey_thread_mutex;
1964 * Acquire mutex to write poller control variables as an
1965 * atomic block.
1967 * Increment hotkey_config_change when changing them if you
1968 * want the kthread to forget old state.
1970 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1972 static struct mutex hotkey_thread_data_mutex;
1973 static unsigned int hotkey_config_change;
1976 * hotkey poller control variables
1978 * Must be atomic or readers will also need to acquire mutex
1980 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
1981 * should be used only when the changes need to be taken as
1982 * a block, OR when one needs to force the kthread to forget
1983 * old state.
1985 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1986 static unsigned int hotkey_poll_freq = 10; /* Hz */
1988 #define HOTKEY_CONFIG_CRITICAL_START \
1989 do { \
1990 mutex_lock(&hotkey_thread_data_mutex); \
1991 hotkey_config_change++; \
1992 } while (0);
1993 #define HOTKEY_CONFIG_CRITICAL_END \
1994 mutex_unlock(&hotkey_thread_data_mutex);
1996 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1998 #define hotkey_source_mask 0U
1999 #define HOTKEY_CONFIG_CRITICAL_START
2000 #define HOTKEY_CONFIG_CRITICAL_END
2002 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2004 static struct mutex hotkey_mutex;
2006 static enum { /* Reasons for waking up */
2007 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2008 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2009 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2010 } hotkey_wakeup_reason;
2012 static int hotkey_autosleep_ack;
2014 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2015 static u32 hotkey_all_mask; /* all events supported in fw */
2016 static u32 hotkey_reserved_mask; /* events better left disabled */
2017 static u32 hotkey_driver_mask; /* events needed by the driver */
2018 static u32 hotkey_user_mask; /* events visible to userspace */
2019 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2021 static unsigned int hotkey_report_mode;
2023 static u16 *hotkey_keycode_map;
2025 static struct attribute_set *hotkey_dev_attributes;
2027 static void tpacpi_driver_event(const unsigned int hkey_event);
2028 static void hotkey_driver_event(const unsigned int scancode);
2029 static void hotkey_poll_setup(const bool may_warn);
2031 /* HKEY.MHKG() return bits */
2032 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2034 static int hotkey_get_wlsw(void)
2036 int status;
2038 if (!tp_features.hotkey_wlsw)
2039 return -ENODEV;
2041 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2042 if (dbg_wlswemul)
2043 return (tpacpi_wlsw_emulstate) ?
2044 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2045 #endif
2047 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2048 return -EIO;
2050 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2053 static int hotkey_get_tablet_mode(int *status)
2055 int s;
2057 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2058 return -EIO;
2060 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2061 return 0;
2065 * Reads current event mask from firmware, and updates
2066 * hotkey_acpi_mask accordingly. Also resets any bits
2067 * from hotkey_user_mask that are unavailable to be
2068 * delivered (shadow requirement of the userspace ABI).
2070 * Call with hotkey_mutex held
2072 static int hotkey_mask_get(void)
2074 if (tp_features.hotkey_mask) {
2075 u32 m = 0;
2077 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2078 return -EIO;
2080 hotkey_acpi_mask = m;
2081 } else {
2082 /* no mask support doesn't mean no event support... */
2083 hotkey_acpi_mask = hotkey_all_mask;
2086 /* sync userspace-visible mask */
2087 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2089 return 0;
2092 void static hotkey_mask_warn_incomplete_mask(void)
2094 /* log only what the user can fix... */
2095 const u32 wantedmask = hotkey_driver_mask &
2096 ~(hotkey_acpi_mask | hotkey_source_mask) &
2097 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2099 if (wantedmask)
2100 printk(TPACPI_NOTICE
2101 "required events 0x%08x not enabled!\n",
2102 wantedmask);
2106 * Set the firmware mask when supported
2108 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2110 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2112 * Call with hotkey_mutex held
2114 static int hotkey_mask_set(u32 mask)
2116 int i;
2117 int rc = 0;
2119 const u32 fwmask = mask & ~hotkey_source_mask;
2121 if (tp_features.hotkey_mask) {
2122 for (i = 0; i < 32; i++) {
2123 if (!acpi_evalf(hkey_handle,
2124 NULL, "MHKM", "vdd", i + 1,
2125 !!(mask & (1 << i)))) {
2126 rc = -EIO;
2127 break;
2133 * We *must* make an inconditional call to hotkey_mask_get to
2134 * refresh hotkey_acpi_mask and update hotkey_user_mask
2136 * Take the opportunity to also log when we cannot _enable_
2137 * a given event.
2139 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2140 printk(TPACPI_NOTICE
2141 "asked for hotkey mask 0x%08x, but "
2142 "firmware forced it to 0x%08x\n",
2143 fwmask, hotkey_acpi_mask);
2146 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2147 hotkey_mask_warn_incomplete_mask();
2149 return rc;
2153 * Sets hotkey_user_mask and tries to set the firmware mask
2155 * Call with hotkey_mutex held
2157 static int hotkey_user_mask_set(const u32 mask)
2159 int rc;
2161 /* Give people a chance to notice they are doing something that
2162 * is bound to go boom on their users sooner or later */
2163 if (!tp_warned.hotkey_mask_ff &&
2164 (mask == 0xffff || mask == 0xffffff ||
2165 mask == 0xffffffff)) {
2166 tp_warned.hotkey_mask_ff = 1;
2167 printk(TPACPI_NOTICE
2168 "setting the hotkey mask to 0x%08x is likely "
2169 "not the best way to go about it\n", mask);
2170 printk(TPACPI_NOTICE
2171 "please consider using the driver defaults, "
2172 "and refer to up-to-date thinkpad-acpi "
2173 "documentation\n");
2176 /* Try to enable what the user asked for, plus whatever we need.
2177 * this syncs everything but won't enable bits in hotkey_user_mask */
2178 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2180 /* Enable the available bits in hotkey_user_mask */
2181 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2183 return rc;
2187 * Sets the driver hotkey mask.
2189 * Can be called even if the hotkey subdriver is inactive
2191 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2193 int rc;
2195 /* Do the right thing if hotkey_init has not been called yet */
2196 if (!tp_features.hotkey) {
2197 hotkey_driver_mask = mask;
2198 return 0;
2201 mutex_lock(&hotkey_mutex);
2203 HOTKEY_CONFIG_CRITICAL_START
2204 hotkey_driver_mask = mask;
2205 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2206 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2207 #endif
2208 HOTKEY_CONFIG_CRITICAL_END
2210 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2211 ~hotkey_source_mask);
2212 hotkey_poll_setup(true);
2214 mutex_unlock(&hotkey_mutex);
2216 return rc;
2219 static int hotkey_status_get(int *status)
2221 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2222 return -EIO;
2224 return 0;
2227 static int hotkey_status_set(bool enable)
2229 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2230 return -EIO;
2232 return 0;
2235 static void tpacpi_input_send_tabletsw(void)
2237 int state;
2239 if (tp_features.hotkey_tablet &&
2240 !hotkey_get_tablet_mode(&state)) {
2241 mutex_lock(&tpacpi_inputdev_send_mutex);
2243 input_report_switch(tpacpi_inputdev,
2244 SW_TABLET_MODE, !!state);
2245 input_sync(tpacpi_inputdev);
2247 mutex_unlock(&tpacpi_inputdev_send_mutex);
2251 /* Do NOT call without validating scancode first */
2252 static void tpacpi_input_send_key(const unsigned int scancode)
2254 const unsigned int keycode = hotkey_keycode_map[scancode];
2256 if (keycode != KEY_RESERVED) {
2257 mutex_lock(&tpacpi_inputdev_send_mutex);
2259 input_report_key(tpacpi_inputdev, keycode, 1);
2260 if (keycode == KEY_UNKNOWN)
2261 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2262 scancode);
2263 input_sync(tpacpi_inputdev);
2265 input_report_key(tpacpi_inputdev, keycode, 0);
2266 if (keycode == KEY_UNKNOWN)
2267 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2268 scancode);
2269 input_sync(tpacpi_inputdev);
2271 mutex_unlock(&tpacpi_inputdev_send_mutex);
2275 /* Do NOT call without validating scancode first */
2276 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2278 hotkey_driver_event(scancode);
2279 if (hotkey_user_mask & (1 << scancode))
2280 tpacpi_input_send_key(scancode);
2283 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2284 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2286 /* Do NOT call without validating scancode first */
2287 static void tpacpi_hotkey_send_key(unsigned int scancode)
2289 tpacpi_input_send_key_masked(scancode);
2290 if (hotkey_report_mode < 2) {
2291 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2292 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2296 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2298 u8 d;
2300 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2301 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2302 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2303 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2304 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2305 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2307 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2308 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2309 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2311 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2312 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2313 n->displayexp_toggle =
2314 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2316 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2317 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2318 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2319 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2320 n->brightness_toggle =
2321 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2323 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2324 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2325 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2326 >> TP_NVRAM_POS_LEVEL_VOLUME;
2327 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2328 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2332 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2333 struct tp_nvram_state *newn,
2334 const u32 event_mask)
2337 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2338 do { \
2339 if ((event_mask & (1 << __scancode)) && \
2340 oldn->__member != newn->__member) \
2341 tpacpi_hotkey_send_key(__scancode); \
2342 } while (0)
2344 #define TPACPI_MAY_SEND_KEY(__scancode) \
2345 do { \
2346 if (event_mask & (1 << __scancode)) \
2347 tpacpi_hotkey_send_key(__scancode); \
2348 } while (0)
2350 void issue_volchange(const unsigned int oldvol,
2351 const unsigned int newvol)
2353 unsigned int i = oldvol;
2355 while (i > newvol) {
2356 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2357 i--;
2359 while (i < newvol) {
2360 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2361 i++;
2365 void issue_brightnesschange(const unsigned int oldbrt,
2366 const unsigned int newbrt)
2368 unsigned int i = oldbrt;
2370 while (i > newbrt) {
2371 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2372 i--;
2374 while (i < newbrt) {
2375 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2376 i++;
2380 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2381 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2382 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2383 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2385 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2387 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2390 * Handle volume
2392 * This code is supposed to duplicate the IBM firmware behaviour:
2393 * - Pressing MUTE issues mute hotkey message, even when already mute
2394 * - Pressing Volume up/down issues volume up/down hotkey messages,
2395 * even when already at maximum or minumum volume
2396 * - The act of unmuting issues volume up/down notification,
2397 * depending which key was used to unmute
2399 * We are constrained to what the NVRAM can tell us, which is not much
2400 * and certainly not enough if more than one volume hotkey was pressed
2401 * since the last poll cycle.
2403 * Just to make our life interesting, some newer Lenovo ThinkPads have
2404 * bugs in the BIOS and may fail to update volume_toggle properly.
2406 if (newn->mute) {
2407 /* muted */
2408 if (!oldn->mute ||
2409 oldn->volume_toggle != newn->volume_toggle ||
2410 oldn->volume_level != newn->volume_level) {
2411 /* recently muted, or repeated mute keypress, or
2412 * multiple presses ending in mute */
2413 issue_volchange(oldn->volume_level, newn->volume_level);
2414 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2416 } else {
2417 /* unmute */
2418 if (oldn->mute) {
2419 /* recently unmuted, issue 'unmute' keypress */
2420 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2422 if (oldn->volume_level != newn->volume_level) {
2423 issue_volchange(oldn->volume_level, newn->volume_level);
2424 } else if (oldn->volume_toggle != newn->volume_toggle) {
2425 /* repeated vol up/down keypress at end of scale ? */
2426 if (newn->volume_level == 0)
2427 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2428 else if (newn->volume_level >= TP_NVRAM_LEVEL_VOLUME_MAX)
2429 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2433 /* handle brightness */
2434 if (oldn->brightness_level != newn->brightness_level) {
2435 issue_brightnesschange(oldn->brightness_level,
2436 newn->brightness_level);
2437 } else if (oldn->brightness_toggle != newn->brightness_toggle) {
2438 /* repeated key presses that didn't change state */
2439 if (newn->brightness_level == 0)
2440 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2441 else if (newn->brightness_level >= bright_maxlvl
2442 && !tp_features.bright_unkfw)
2443 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2446 #undef TPACPI_COMPARE_KEY
2447 #undef TPACPI_MAY_SEND_KEY
2451 * Polling driver
2453 * We track all events in hotkey_source_mask all the time, since
2454 * most of them are edge-based. We only issue those requested by
2455 * hotkey_user_mask or hotkey_driver_mask, though.
2457 static int hotkey_kthread(void *data)
2459 struct tp_nvram_state s[2];
2460 u32 poll_mask, event_mask;
2461 unsigned int si, so;
2462 unsigned long t;
2463 unsigned int change_detector, must_reset;
2464 unsigned int poll_freq;
2466 mutex_lock(&hotkey_thread_mutex);
2468 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2469 goto exit;
2471 set_freezable();
2473 so = 0;
2474 si = 1;
2475 t = 0;
2477 /* Initial state for compares */
2478 mutex_lock(&hotkey_thread_data_mutex);
2479 change_detector = hotkey_config_change;
2480 poll_mask = hotkey_source_mask;
2481 event_mask = hotkey_source_mask &
2482 (hotkey_driver_mask | hotkey_user_mask);
2483 poll_freq = hotkey_poll_freq;
2484 mutex_unlock(&hotkey_thread_data_mutex);
2485 hotkey_read_nvram(&s[so], poll_mask);
2487 while (!kthread_should_stop()) {
2488 if (t == 0) {
2489 if (likely(poll_freq))
2490 t = 1000/poll_freq;
2491 else
2492 t = 100; /* should never happen... */
2494 t = msleep_interruptible(t);
2495 if (unlikely(kthread_should_stop()))
2496 break;
2497 must_reset = try_to_freeze();
2498 if (t > 0 && !must_reset)
2499 continue;
2501 mutex_lock(&hotkey_thread_data_mutex);
2502 if (must_reset || hotkey_config_change != change_detector) {
2503 /* forget old state on thaw or config change */
2504 si = so;
2505 t = 0;
2506 change_detector = hotkey_config_change;
2508 poll_mask = hotkey_source_mask;
2509 event_mask = hotkey_source_mask &
2510 (hotkey_driver_mask | hotkey_user_mask);
2511 poll_freq = hotkey_poll_freq;
2512 mutex_unlock(&hotkey_thread_data_mutex);
2514 if (likely(poll_mask)) {
2515 hotkey_read_nvram(&s[si], poll_mask);
2516 if (likely(si != so)) {
2517 hotkey_compare_and_issue_event(&s[so], &s[si],
2518 event_mask);
2522 so = si;
2523 si ^= 1;
2526 exit:
2527 mutex_unlock(&hotkey_thread_mutex);
2528 return 0;
2531 /* call with hotkey_mutex held */
2532 static void hotkey_poll_stop_sync(void)
2534 if (tpacpi_hotkey_task) {
2535 if (frozen(tpacpi_hotkey_task) ||
2536 freezing(tpacpi_hotkey_task))
2537 thaw_process(tpacpi_hotkey_task);
2539 kthread_stop(tpacpi_hotkey_task);
2540 tpacpi_hotkey_task = NULL;
2541 mutex_lock(&hotkey_thread_mutex);
2542 /* at this point, the thread did exit */
2543 mutex_unlock(&hotkey_thread_mutex);
2547 /* call with hotkey_mutex held */
2548 static void hotkey_poll_setup(const bool may_warn)
2550 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2551 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2553 if (hotkey_poll_freq > 0 &&
2554 (poll_driver_mask ||
2555 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2556 if (!tpacpi_hotkey_task) {
2557 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2558 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2559 if (IS_ERR(tpacpi_hotkey_task)) {
2560 tpacpi_hotkey_task = NULL;
2561 printk(TPACPI_ERR
2562 "could not create kernel thread "
2563 "for hotkey polling\n");
2566 } else {
2567 hotkey_poll_stop_sync();
2568 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2569 hotkey_poll_freq == 0) {
2570 printk(TPACPI_NOTICE
2571 "hot keys 0x%08x and/or events 0x%08x "
2572 "require polling, which is currently "
2573 "disabled\n",
2574 poll_user_mask, poll_driver_mask);
2579 static void hotkey_poll_setup_safe(const bool may_warn)
2581 mutex_lock(&hotkey_mutex);
2582 hotkey_poll_setup(may_warn);
2583 mutex_unlock(&hotkey_mutex);
2586 /* call with hotkey_mutex held */
2587 static void hotkey_poll_set_freq(unsigned int freq)
2589 if (!freq)
2590 hotkey_poll_stop_sync();
2592 hotkey_poll_freq = freq;
2595 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2597 static void hotkey_poll_setup(const bool __unused)
2601 static void hotkey_poll_setup_safe(const bool __unused)
2605 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2607 static int hotkey_inputdev_open(struct input_dev *dev)
2609 switch (tpacpi_lifecycle) {
2610 case TPACPI_LIFE_INIT:
2611 case TPACPI_LIFE_RUNNING:
2612 hotkey_poll_setup_safe(false);
2613 return 0;
2614 case TPACPI_LIFE_EXITING:
2615 return -EBUSY;
2618 /* Should only happen if tpacpi_lifecycle is corrupt */
2619 BUG();
2620 return -EBUSY;
2623 static void hotkey_inputdev_close(struct input_dev *dev)
2625 /* disable hotkey polling when possible */
2626 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2627 !(hotkey_source_mask & hotkey_driver_mask))
2628 hotkey_poll_setup_safe(false);
2631 /* sysfs hotkey enable ------------------------------------------------- */
2632 static ssize_t hotkey_enable_show(struct device *dev,
2633 struct device_attribute *attr,
2634 char *buf)
2636 int res, status;
2638 printk_deprecated_attribute("hotkey_enable",
2639 "Hotkey reporting is always enabled");
2641 res = hotkey_status_get(&status);
2642 if (res)
2643 return res;
2645 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2648 static ssize_t hotkey_enable_store(struct device *dev,
2649 struct device_attribute *attr,
2650 const char *buf, size_t count)
2652 unsigned long t;
2654 printk_deprecated_attribute("hotkey_enable",
2655 "Hotkeys can be disabled through hotkey_mask");
2657 if (parse_strtoul(buf, 1, &t))
2658 return -EINVAL;
2660 if (t == 0)
2661 return -EPERM;
2663 return count;
2666 static struct device_attribute dev_attr_hotkey_enable =
2667 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2668 hotkey_enable_show, hotkey_enable_store);
2670 /* sysfs hotkey mask --------------------------------------------------- */
2671 static ssize_t hotkey_mask_show(struct device *dev,
2672 struct device_attribute *attr,
2673 char *buf)
2675 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2678 static ssize_t hotkey_mask_store(struct device *dev,
2679 struct device_attribute *attr,
2680 const char *buf, size_t count)
2682 unsigned long t;
2683 int res;
2685 if (parse_strtoul(buf, 0xffffffffUL, &t))
2686 return -EINVAL;
2688 if (mutex_lock_killable(&hotkey_mutex))
2689 return -ERESTARTSYS;
2691 res = hotkey_user_mask_set(t);
2693 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2694 hotkey_poll_setup(true);
2695 #endif
2697 mutex_unlock(&hotkey_mutex);
2699 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2701 return (res) ? res : count;
2704 static struct device_attribute dev_attr_hotkey_mask =
2705 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2706 hotkey_mask_show, hotkey_mask_store);
2708 /* sysfs hotkey bios_enabled ------------------------------------------- */
2709 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2710 struct device_attribute *attr,
2711 char *buf)
2713 return sprintf(buf, "0\n");
2716 static struct device_attribute dev_attr_hotkey_bios_enabled =
2717 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2719 /* sysfs hotkey bios_mask ---------------------------------------------- */
2720 static ssize_t hotkey_bios_mask_show(struct device *dev,
2721 struct device_attribute *attr,
2722 char *buf)
2724 printk_deprecated_attribute("hotkey_bios_mask",
2725 "This attribute is useless.");
2726 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2729 static struct device_attribute dev_attr_hotkey_bios_mask =
2730 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2732 /* sysfs hotkey all_mask ----------------------------------------------- */
2733 static ssize_t hotkey_all_mask_show(struct device *dev,
2734 struct device_attribute *attr,
2735 char *buf)
2737 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2738 hotkey_all_mask | hotkey_source_mask);
2741 static struct device_attribute dev_attr_hotkey_all_mask =
2742 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2744 /* sysfs hotkey recommended_mask --------------------------------------- */
2745 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2746 struct device_attribute *attr,
2747 char *buf)
2749 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2750 (hotkey_all_mask | hotkey_source_mask)
2751 & ~hotkey_reserved_mask);
2754 static struct device_attribute dev_attr_hotkey_recommended_mask =
2755 __ATTR(hotkey_recommended_mask, S_IRUGO,
2756 hotkey_recommended_mask_show, NULL);
2758 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2760 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2761 static ssize_t hotkey_source_mask_show(struct device *dev,
2762 struct device_attribute *attr,
2763 char *buf)
2765 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2768 static ssize_t hotkey_source_mask_store(struct device *dev,
2769 struct device_attribute *attr,
2770 const char *buf, size_t count)
2772 unsigned long t;
2773 u32 r_ev;
2774 int rc;
2776 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2777 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2778 return -EINVAL;
2780 if (mutex_lock_killable(&hotkey_mutex))
2781 return -ERESTARTSYS;
2783 HOTKEY_CONFIG_CRITICAL_START
2784 hotkey_source_mask = t;
2785 HOTKEY_CONFIG_CRITICAL_END
2787 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2788 ~hotkey_source_mask);
2789 hotkey_poll_setup(true);
2791 /* check if events needed by the driver got disabled */
2792 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2793 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2795 mutex_unlock(&hotkey_mutex);
2797 if (rc < 0)
2798 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2799 "firmware event mask!\n");
2801 if (r_ev)
2802 printk(TPACPI_NOTICE "hotkey_source_mask: "
2803 "some important events were disabled: "
2804 "0x%04x\n", r_ev);
2806 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2808 return (rc < 0) ? rc : count;
2811 static struct device_attribute dev_attr_hotkey_source_mask =
2812 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2813 hotkey_source_mask_show, hotkey_source_mask_store);
2815 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2816 static ssize_t hotkey_poll_freq_show(struct device *dev,
2817 struct device_attribute *attr,
2818 char *buf)
2820 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2823 static ssize_t hotkey_poll_freq_store(struct device *dev,
2824 struct device_attribute *attr,
2825 const char *buf, size_t count)
2827 unsigned long t;
2829 if (parse_strtoul(buf, 25, &t))
2830 return -EINVAL;
2832 if (mutex_lock_killable(&hotkey_mutex))
2833 return -ERESTARTSYS;
2835 hotkey_poll_set_freq(t);
2836 hotkey_poll_setup(true);
2838 mutex_unlock(&hotkey_mutex);
2840 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2842 return count;
2845 static struct device_attribute dev_attr_hotkey_poll_freq =
2846 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2847 hotkey_poll_freq_show, hotkey_poll_freq_store);
2849 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2851 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2852 static ssize_t hotkey_radio_sw_show(struct device *dev,
2853 struct device_attribute *attr,
2854 char *buf)
2856 int res;
2857 res = hotkey_get_wlsw();
2858 if (res < 0)
2859 return res;
2861 /* Opportunistic update */
2862 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2864 return snprintf(buf, PAGE_SIZE, "%d\n",
2865 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2868 static struct device_attribute dev_attr_hotkey_radio_sw =
2869 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2871 static void hotkey_radio_sw_notify_change(void)
2873 if (tp_features.hotkey_wlsw)
2874 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2875 "hotkey_radio_sw");
2878 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2879 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2880 struct device_attribute *attr,
2881 char *buf)
2883 int res, s;
2884 res = hotkey_get_tablet_mode(&s);
2885 if (res < 0)
2886 return res;
2888 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2891 static struct device_attribute dev_attr_hotkey_tablet_mode =
2892 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2894 static void hotkey_tablet_mode_notify_change(void)
2896 if (tp_features.hotkey_tablet)
2897 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2898 "hotkey_tablet_mode");
2901 /* sysfs hotkey report_mode -------------------------------------------- */
2902 static ssize_t hotkey_report_mode_show(struct device *dev,
2903 struct device_attribute *attr,
2904 char *buf)
2906 return snprintf(buf, PAGE_SIZE, "%d\n",
2907 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2910 static struct device_attribute dev_attr_hotkey_report_mode =
2911 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2913 /* sysfs wakeup reason (pollable) -------------------------------------- */
2914 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2915 struct device_attribute *attr,
2916 char *buf)
2918 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2921 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2922 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2924 static void hotkey_wakeup_reason_notify_change(void)
2926 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2927 "wakeup_reason");
2930 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2931 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2932 struct device_attribute *attr,
2933 char *buf)
2935 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2938 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2939 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2940 hotkey_wakeup_hotunplug_complete_show, NULL);
2942 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2944 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2945 "wakeup_hotunplug_complete");
2948 /* --------------------------------------------------------------------- */
2950 static struct attribute *hotkey_attributes[] __initdata = {
2951 &dev_attr_hotkey_enable.attr,
2952 &dev_attr_hotkey_bios_enabled.attr,
2953 &dev_attr_hotkey_bios_mask.attr,
2954 &dev_attr_hotkey_report_mode.attr,
2955 &dev_attr_hotkey_wakeup_reason.attr,
2956 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2957 &dev_attr_hotkey_mask.attr,
2958 &dev_attr_hotkey_all_mask.attr,
2959 &dev_attr_hotkey_recommended_mask.attr,
2960 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2961 &dev_attr_hotkey_source_mask.attr,
2962 &dev_attr_hotkey_poll_freq.attr,
2963 #endif
2967 * Sync both the hw and sw blocking state of all switches
2969 static void tpacpi_send_radiosw_update(void)
2971 int wlsw;
2974 * We must sync all rfkill controllers *before* issuing any
2975 * rfkill input events, or we will race the rfkill core input
2976 * handler.
2978 * tpacpi_inputdev_send_mutex works as a syncronization point
2979 * for the above.
2981 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2984 wlsw = hotkey_get_wlsw();
2986 /* Sync hw blocking state first if it is hw-blocked */
2987 if (wlsw == TPACPI_RFK_RADIO_OFF)
2988 tpacpi_rfk_update_hwblock_state(true);
2990 /* Sync sw blocking state */
2991 tpacpi_rfk_update_swstate_all();
2993 /* Sync hw blocking state last if it is hw-unblocked */
2994 if (wlsw == TPACPI_RFK_RADIO_ON)
2995 tpacpi_rfk_update_hwblock_state(false);
2997 /* Issue rfkill input event for WLSW switch */
2998 if (!(wlsw < 0)) {
2999 mutex_lock(&tpacpi_inputdev_send_mutex);
3001 input_report_switch(tpacpi_inputdev,
3002 SW_RFKILL_ALL, (wlsw > 0));
3003 input_sync(tpacpi_inputdev);
3005 mutex_unlock(&tpacpi_inputdev_send_mutex);
3009 * this can be unconditional, as we will poll state again
3010 * if userspace uses the notify to read data
3012 hotkey_radio_sw_notify_change();
3015 static void hotkey_exit(void)
3017 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3018 mutex_lock(&hotkey_mutex);
3019 hotkey_poll_stop_sync();
3020 mutex_unlock(&hotkey_mutex);
3021 #endif
3023 if (hotkey_dev_attributes)
3024 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3026 kfree(hotkey_keycode_map);
3028 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3029 "restoring original HKEY status and mask\n");
3030 /* yes, there is a bitwise or below, we want the
3031 * functions to be called even if one of them fail */
3032 if (((tp_features.hotkey_mask &&
3033 hotkey_mask_set(hotkey_orig_mask)) |
3034 hotkey_status_set(false)) != 0)
3035 printk(TPACPI_ERR
3036 "failed to restore hot key mask "
3037 "to BIOS defaults\n");
3040 static void __init hotkey_unmap(const unsigned int scancode)
3042 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3043 clear_bit(hotkey_keycode_map[scancode],
3044 tpacpi_inputdev->keybit);
3045 hotkey_keycode_map[scancode] = KEY_RESERVED;
3050 * HKEY quirks:
3051 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3054 #define TPACPI_HK_Q_INIMASK 0x0001
3056 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3057 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3058 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3059 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3060 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3061 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3062 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3063 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3064 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3065 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3066 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3067 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3068 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3069 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3070 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3071 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3072 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3073 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3074 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3075 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3078 static int __init hotkey_init(struct ibm_init_struct *iibm)
3080 /* Requirements for changing the default keymaps:
3082 * 1. Many of the keys are mapped to KEY_RESERVED for very
3083 * good reasons. Do not change them unless you have deep
3084 * knowledge on the IBM and Lenovo ThinkPad firmware for
3085 * the various ThinkPad models. The driver behaves
3086 * differently for KEY_RESERVED: such keys have their
3087 * hot key mask *unset* in mask_recommended, and also
3088 * in the initial hot key mask programmed into the
3089 * firmware at driver load time, which means the firm-
3090 * ware may react very differently if you change them to
3091 * something else;
3093 * 2. You must be subscribed to the linux-thinkpad and
3094 * ibm-acpi-devel mailing lists, and you should read the
3095 * list archives since 2007 if you want to change the
3096 * keymaps. This requirement exists so that you will
3097 * know the past history of problems with the thinkpad-
3098 * acpi driver keymaps, and also that you will be
3099 * listening to any bug reports;
3101 * 3. Do not send thinkpad-acpi specific patches directly to
3102 * for merging, *ever*. Send them to the linux-acpi
3103 * mailinglist for comments. Merging is to be done only
3104 * through acpi-test and the ACPI maintainer.
3106 * If the above is too much to ask, don't change the keymap.
3107 * Ask the thinkpad-acpi maintainer to do it, instead.
3109 static u16 ibm_keycode_map[] __initdata = {
3110 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3111 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3112 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3113 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3115 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3116 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3117 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3118 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3120 /* brightness: firmware always reacts to them */
3121 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3122 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3124 /* Thinklight: firmware always react to it */
3125 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3127 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3128 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3130 /* Volume: firmware always react to it and reprograms
3131 * the built-in *extra* mixer. Never map it to control
3132 * another mixer by default. */
3133 KEY_RESERVED, /* 0x14: VOLUME UP */
3134 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3135 KEY_RESERVED, /* 0x16: MUTE */
3137 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3139 /* (assignments unknown, please report if found) */
3140 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3141 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3143 static u16 lenovo_keycode_map[] __initdata = {
3144 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3145 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3146 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3147 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3149 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3150 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3151 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3152 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3154 /* These should be enabled --only-- when ACPI video
3155 * is disabled (i.e. in "vendor" mode), and are handled
3156 * in a special way by the init code */
3157 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3158 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3160 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3162 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3163 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3165 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3166 * react to it and reprograms the built-in *extra* mixer.
3167 * Never map it to control another mixer by default.
3169 * T60?, T61, R60?, R61: firmware and EC tries to send
3170 * these over the regular keyboard, so these are no-ops,
3171 * but there are still weird bugs re. MUTE, so do not
3172 * change unless you get test reports from all Lenovo
3173 * models. May cause the BIOS to interfere with the
3174 * HDA mixer.
3176 KEY_RESERVED, /* 0x14: VOLUME UP */
3177 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3178 KEY_RESERVED, /* 0x16: MUTE */
3180 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3182 /* (assignments unknown, please report if found) */
3183 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3184 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3187 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3188 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3189 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3191 int res, i;
3192 int status;
3193 int hkeyv;
3194 bool radiosw_state = false;
3195 bool tabletsw_state = false;
3197 unsigned long quirks;
3199 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3200 "initializing hotkey subdriver\n");
3202 BUG_ON(!tpacpi_inputdev);
3203 BUG_ON(tpacpi_inputdev->open != NULL ||
3204 tpacpi_inputdev->close != NULL);
3206 TPACPI_ACPIHANDLE_INIT(hkey);
3207 mutex_init(&hotkey_mutex);
3209 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3210 mutex_init(&hotkey_thread_mutex);
3211 mutex_init(&hotkey_thread_data_mutex);
3212 #endif
3214 /* hotkey not supported on 570 */
3215 tp_features.hotkey = hkey_handle != NULL;
3217 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3218 "hotkeys are %s\n",
3219 str_supported(tp_features.hotkey));
3221 if (!tp_features.hotkey)
3222 return 1;
3224 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3225 ARRAY_SIZE(tpacpi_hotkey_qtable));
3227 tpacpi_disable_brightness_delay();
3229 /* MUST have enough space for all attributes to be added to
3230 * hotkey_dev_attributes */
3231 hotkey_dev_attributes = create_attr_set(
3232 ARRAY_SIZE(hotkey_attributes) + 2,
3233 NULL);
3234 if (!hotkey_dev_attributes)
3235 return -ENOMEM;
3236 res = add_many_to_attr_set(hotkey_dev_attributes,
3237 hotkey_attributes,
3238 ARRAY_SIZE(hotkey_attributes));
3239 if (res)
3240 goto err_exit;
3242 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3243 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3244 for HKEY interface version 0x100 */
3245 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3246 if ((hkeyv >> 8) != 1) {
3247 printk(TPACPI_ERR "unknown version of the "
3248 "HKEY interface: 0x%x\n", hkeyv);
3249 printk(TPACPI_ERR "please report this to %s\n",
3250 TPACPI_MAIL);
3251 } else {
3253 * MHKV 0x100 in A31, R40, R40e,
3254 * T4x, X31, and later
3256 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3257 "firmware HKEY interface version: 0x%x\n",
3258 hkeyv);
3260 /* Paranoia check AND init hotkey_all_mask */
3261 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3262 "MHKA", "qd")) {
3263 printk(TPACPI_ERR
3264 "missing MHKA handler, "
3265 "please report this to %s\n",
3266 TPACPI_MAIL);
3267 /* Fallback: pre-init for FN+F3,F4,F12 */
3268 hotkey_all_mask = 0x080cU;
3269 } else {
3270 tp_features.hotkey_mask = 1;
3275 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3276 "hotkey masks are %s\n",
3277 str_supported(tp_features.hotkey_mask));
3279 /* Init hotkey_all_mask if not initialized yet */
3280 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3281 (quirks & TPACPI_HK_Q_INIMASK))
3282 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3284 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3285 if (tp_features.hotkey_mask) {
3286 /* hotkey_source_mask *must* be zero for
3287 * the first hotkey_mask_get to return hotkey_orig_mask */
3288 res = hotkey_mask_get();
3289 if (res)
3290 goto err_exit;
3292 hotkey_orig_mask = hotkey_acpi_mask;
3293 } else {
3294 hotkey_orig_mask = hotkey_all_mask;
3295 hotkey_acpi_mask = hotkey_all_mask;
3298 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3299 if (dbg_wlswemul) {
3300 tp_features.hotkey_wlsw = 1;
3301 radiosw_state = !!tpacpi_wlsw_emulstate;
3302 printk(TPACPI_INFO
3303 "radio switch emulation enabled\n");
3304 } else
3305 #endif
3306 /* Not all thinkpads have a hardware radio switch */
3307 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3308 tp_features.hotkey_wlsw = 1;
3309 radiosw_state = !!status;
3310 printk(TPACPI_INFO
3311 "radio switch found; radios are %s\n",
3312 enabled(status, 0));
3314 if (tp_features.hotkey_wlsw)
3315 res = add_to_attr_set(hotkey_dev_attributes,
3316 &dev_attr_hotkey_radio_sw.attr);
3318 /* For X41t, X60t, X61t Tablets... */
3319 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3320 tp_features.hotkey_tablet = 1;
3321 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3322 printk(TPACPI_INFO
3323 "possible tablet mode switch found; "
3324 "ThinkPad in %s mode\n",
3325 (tabletsw_state) ? "tablet" : "laptop");
3326 res = add_to_attr_set(hotkey_dev_attributes,
3327 &dev_attr_hotkey_tablet_mode.attr);
3330 if (!res)
3331 res = register_attr_set_with_sysfs(
3332 hotkey_dev_attributes,
3333 &tpacpi_pdev->dev.kobj);
3334 if (res)
3335 goto err_exit;
3337 /* Set up key map */
3339 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3340 GFP_KERNEL);
3341 if (!hotkey_keycode_map) {
3342 printk(TPACPI_ERR
3343 "failed to allocate memory for key map\n");
3344 res = -ENOMEM;
3345 goto err_exit;
3348 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
3349 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3350 "using Lenovo default hot key map\n");
3351 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3352 TPACPI_HOTKEY_MAP_SIZE);
3353 } else {
3354 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3355 "using IBM default hot key map\n");
3356 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3357 TPACPI_HOTKEY_MAP_SIZE);
3360 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3361 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3362 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3363 tpacpi_inputdev->keycode = hotkey_keycode_map;
3364 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3365 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3366 input_set_capability(tpacpi_inputdev, EV_KEY,
3367 hotkey_keycode_map[i]);
3368 } else {
3369 if (i < sizeof(hotkey_reserved_mask)*8)
3370 hotkey_reserved_mask |= 1 << i;
3374 if (tp_features.hotkey_wlsw) {
3375 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3376 input_report_switch(tpacpi_inputdev,
3377 SW_RFKILL_ALL, radiosw_state);
3379 if (tp_features.hotkey_tablet) {
3380 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3381 input_report_switch(tpacpi_inputdev,
3382 SW_TABLET_MODE, tabletsw_state);
3385 /* Do not issue duplicate brightness change events to
3386 * userspace. tpacpi_detect_brightness_capabilities() must have
3387 * been called before this point */
3388 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3389 printk(TPACPI_INFO
3390 "This ThinkPad has standard ACPI backlight "
3391 "brightness control, supported by the ACPI "
3392 "video driver\n");
3393 printk(TPACPI_NOTICE
3394 "Disabling thinkpad-acpi brightness events "
3395 "by default...\n");
3397 /* Disable brightness up/down on Lenovo thinkpads when
3398 * ACPI is handling them, otherwise it is plain impossible
3399 * for userspace to do something even remotely sane */
3400 hotkey_reserved_mask |=
3401 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3402 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3403 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3404 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3407 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3408 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3409 & ~hotkey_all_mask
3410 & ~hotkey_reserved_mask;
3412 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3413 "hotkey source mask 0x%08x, polling freq %u\n",
3414 hotkey_source_mask, hotkey_poll_freq);
3415 #endif
3417 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3418 "enabling firmware HKEY event interface...\n");
3419 res = hotkey_status_set(true);
3420 if (res) {
3421 hotkey_exit();
3422 return res;
3424 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3425 | hotkey_driver_mask)
3426 & ~hotkey_source_mask);
3427 if (res < 0 && res != -ENXIO) {
3428 hotkey_exit();
3429 return res;
3431 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3432 & ~hotkey_reserved_mask;
3433 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3434 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3435 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3437 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3438 "legacy ibm/hotkey event reporting over procfs %s\n",
3439 (hotkey_report_mode < 2) ?
3440 "enabled" : "disabled");
3442 tpacpi_inputdev->open = &hotkey_inputdev_open;
3443 tpacpi_inputdev->close = &hotkey_inputdev_close;
3445 hotkey_poll_setup_safe(true);
3447 return 0;
3449 err_exit:
3450 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3451 hotkey_dev_attributes = NULL;
3453 return (res < 0)? res : 1;
3456 static bool hotkey_notify_hotkey(const u32 hkey,
3457 bool *send_acpi_ev,
3458 bool *ignore_acpi_ev)
3460 /* 0x1000-0x1FFF: key presses */
3461 unsigned int scancode = hkey & 0xfff;
3462 *send_acpi_ev = true;
3463 *ignore_acpi_ev = false;
3465 if (scancode > 0 && scancode < 0x21) {
3466 scancode--;
3467 if (!(hotkey_source_mask & (1 << scancode))) {
3468 tpacpi_input_send_key_masked(scancode);
3469 *send_acpi_ev = false;
3470 } else {
3471 *ignore_acpi_ev = true;
3473 return true;
3475 return false;
3478 static bool hotkey_notify_wakeup(const u32 hkey,
3479 bool *send_acpi_ev,
3480 bool *ignore_acpi_ev)
3482 /* 0x2000-0x2FFF: Wakeup reason */
3483 *send_acpi_ev = true;
3484 *ignore_acpi_ev = false;
3486 switch (hkey) {
3487 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3488 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3489 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3490 *ignore_acpi_ev = true;
3491 break;
3493 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3494 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3495 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3496 *ignore_acpi_ev = true;
3497 break;
3499 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3500 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3501 printk(TPACPI_ALERT
3502 "EMERGENCY WAKEUP: battery almost empty\n");
3503 /* how to auto-heal: */
3504 /* 2313: woke up from S3, go to S4/S5 */
3505 /* 2413: woke up from S4, go to S5 */
3506 break;
3508 default:
3509 return false;
3512 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3513 printk(TPACPI_INFO
3514 "woke up due to a hot-unplug "
3515 "request...\n");
3516 hotkey_wakeup_reason_notify_change();
3518 return true;
3521 static bool hotkey_notify_usrevent(const u32 hkey,
3522 bool *send_acpi_ev,
3523 bool *ignore_acpi_ev)
3525 /* 0x5000-0x5FFF: human interface helpers */
3526 *send_acpi_ev = true;
3527 *ignore_acpi_ev = false;
3529 switch (hkey) {
3530 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3531 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3532 return true;
3534 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3535 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3536 tpacpi_input_send_tabletsw();
3537 hotkey_tablet_mode_notify_change();
3538 *send_acpi_ev = false;
3539 return true;
3541 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3542 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3543 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3544 /* do not propagate these events */
3545 *ignore_acpi_ev = true;
3546 return true;
3548 default:
3549 return false;
3553 static void thermal_dump_all_sensors(void);
3555 static bool hotkey_notify_thermal(const u32 hkey,
3556 bool *send_acpi_ev,
3557 bool *ignore_acpi_ev)
3559 bool known = true;
3561 /* 0x6000-0x6FFF: thermal alarms */
3562 *send_acpi_ev = true;
3563 *ignore_acpi_ev = false;
3565 switch (hkey) {
3566 case TP_HKEY_EV_THM_TABLE_CHANGED:
3567 printk(TPACPI_INFO
3568 "EC reports that Thermal Table has changed\n");
3569 /* recommended action: do nothing, we don't have
3570 * Lenovo ATM information */
3571 return true;
3572 case TP_HKEY_EV_ALARM_BAT_HOT:
3573 printk(TPACPI_CRIT
3574 "THERMAL ALARM: battery is too hot!\n");
3575 /* recommended action: warn user through gui */
3576 break;
3577 case TP_HKEY_EV_ALARM_BAT_XHOT:
3578 printk(TPACPI_ALERT
3579 "THERMAL EMERGENCY: battery is extremely hot!\n");
3580 /* recommended action: immediate sleep/hibernate */
3581 break;
3582 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3583 printk(TPACPI_CRIT
3584 "THERMAL ALARM: "
3585 "a sensor reports something is too hot!\n");
3586 /* recommended action: warn user through gui, that */
3587 /* some internal component is too hot */
3588 break;
3589 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3590 printk(TPACPI_ALERT
3591 "THERMAL EMERGENCY: "
3592 "a sensor reports something is extremely hot!\n");
3593 /* recommended action: immediate sleep/hibernate */
3594 break;
3595 default:
3596 printk(TPACPI_ALERT
3597 "THERMAL ALERT: unknown thermal alarm received\n");
3598 known = false;
3601 thermal_dump_all_sensors();
3603 return known;
3606 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3608 u32 hkey;
3609 bool send_acpi_ev;
3610 bool ignore_acpi_ev;
3611 bool known_ev;
3613 if (event != 0x80) {
3614 printk(TPACPI_ERR
3615 "unknown HKEY notification event %d\n", event);
3616 /* forward it to userspace, maybe it knows how to handle it */
3617 acpi_bus_generate_netlink_event(
3618 ibm->acpi->device->pnp.device_class,
3619 dev_name(&ibm->acpi->device->dev),
3620 event, 0);
3621 return;
3624 while (1) {
3625 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3626 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3627 return;
3630 if (hkey == 0) {
3631 /* queue empty */
3632 return;
3635 send_acpi_ev = true;
3636 ignore_acpi_ev = false;
3638 switch (hkey >> 12) {
3639 case 1:
3640 /* 0x1000-0x1FFF: key presses */
3641 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3642 &ignore_acpi_ev);
3643 break;
3644 case 2:
3645 /* 0x2000-0x2FFF: Wakeup reason */
3646 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3647 &ignore_acpi_ev);
3648 break;
3649 case 3:
3650 /* 0x3000-0x3FFF: bay-related wakeups */
3651 switch (hkey) {
3652 case TP_HKEY_EV_BAYEJ_ACK:
3653 hotkey_autosleep_ack = 1;
3654 printk(TPACPI_INFO
3655 "bay ejected\n");
3656 hotkey_wakeup_hotunplug_complete_notify_change();
3657 known_ev = true;
3658 break;
3659 case TP_HKEY_EV_OPTDRV_EJ:
3660 /* FIXME: kick libata if SATA link offline */
3661 known_ev = true;
3662 break;
3663 default:
3664 known_ev = false;
3666 break;
3667 case 4:
3668 /* 0x4000-0x4FFF: dock-related wakeups */
3669 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3670 hotkey_autosleep_ack = 1;
3671 printk(TPACPI_INFO
3672 "undocked\n");
3673 hotkey_wakeup_hotunplug_complete_notify_change();
3674 known_ev = true;
3675 } else {
3676 known_ev = false;
3678 break;
3679 case 5:
3680 /* 0x5000-0x5FFF: human interface helpers */
3681 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3682 &ignore_acpi_ev);
3683 break;
3684 case 6:
3685 /* 0x6000-0x6FFF: thermal alarms */
3686 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3687 &ignore_acpi_ev);
3688 break;
3689 case 7:
3690 /* 0x7000-0x7FFF: misc */
3691 if (tp_features.hotkey_wlsw &&
3692 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3693 tpacpi_send_radiosw_update();
3694 send_acpi_ev = 0;
3695 known_ev = true;
3696 break;
3698 /* fallthrough to default */
3699 default:
3700 known_ev = false;
3702 if (!known_ev) {
3703 printk(TPACPI_NOTICE
3704 "unhandled HKEY event 0x%04x\n", hkey);
3705 printk(TPACPI_NOTICE
3706 "please report the conditions when this "
3707 "event happened to %s\n", TPACPI_MAIL);
3710 /* Legacy events */
3711 if (!ignore_acpi_ev &&
3712 (send_acpi_ev || hotkey_report_mode < 2)) {
3713 acpi_bus_generate_proc_event(ibm->acpi->device,
3714 event, hkey);
3717 /* netlink events */
3718 if (!ignore_acpi_ev && send_acpi_ev) {
3719 acpi_bus_generate_netlink_event(
3720 ibm->acpi->device->pnp.device_class,
3721 dev_name(&ibm->acpi->device->dev),
3722 event, hkey);
3727 static void hotkey_suspend(pm_message_t state)
3729 /* Do these on suspend, we get the events on early resume! */
3730 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3731 hotkey_autosleep_ack = 0;
3734 static void hotkey_resume(void)
3736 tpacpi_disable_brightness_delay();
3738 if (hotkey_status_set(true) < 0 ||
3739 hotkey_mask_set(hotkey_acpi_mask) < 0)
3740 printk(TPACPI_ERR
3741 "error while attempting to reset the event "
3742 "firmware interface\n");
3744 tpacpi_send_radiosw_update();
3745 hotkey_tablet_mode_notify_change();
3746 hotkey_wakeup_reason_notify_change();
3747 hotkey_wakeup_hotunplug_complete_notify_change();
3748 hotkey_poll_setup_safe(false);
3751 /* procfs -------------------------------------------------------------- */
3752 static int hotkey_read(struct seq_file *m)
3754 int res, status;
3756 if (!tp_features.hotkey) {
3757 seq_printf(m, "status:\t\tnot supported\n");
3758 return 0;
3761 if (mutex_lock_killable(&hotkey_mutex))
3762 return -ERESTARTSYS;
3763 res = hotkey_status_get(&status);
3764 if (!res)
3765 res = hotkey_mask_get();
3766 mutex_unlock(&hotkey_mutex);
3767 if (res)
3768 return res;
3770 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3771 if (hotkey_all_mask) {
3772 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3773 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3774 } else {
3775 seq_printf(m, "mask:\t\tnot supported\n");
3776 seq_printf(m, "commands:\tenable, disable, reset\n");
3779 return 0;
3782 static void hotkey_enabledisable_warn(bool enable)
3784 tpacpi_log_usertask("procfs hotkey enable/disable");
3785 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3786 TPACPI_WARN
3787 "hotkey enable/disable functionality has been "
3788 "removed from the driver. Hotkeys are always "
3789 "enabled\n"))
3790 printk(TPACPI_ERR
3791 "Please remove the hotkey=enable module "
3792 "parameter, it is deprecated. Hotkeys are always "
3793 "enabled\n");
3796 static int hotkey_write(char *buf)
3798 int res;
3799 u32 mask;
3800 char *cmd;
3802 if (!tp_features.hotkey)
3803 return -ENODEV;
3805 if (mutex_lock_killable(&hotkey_mutex))
3806 return -ERESTARTSYS;
3808 mask = hotkey_user_mask;
3810 res = 0;
3811 while ((cmd = next_cmd(&buf))) {
3812 if (strlencmp(cmd, "enable") == 0) {
3813 hotkey_enabledisable_warn(1);
3814 } else if (strlencmp(cmd, "disable") == 0) {
3815 hotkey_enabledisable_warn(0);
3816 res = -EPERM;
3817 } else if (strlencmp(cmd, "reset") == 0) {
3818 mask = (hotkey_all_mask | hotkey_source_mask)
3819 & ~hotkey_reserved_mask;
3820 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3821 /* mask set */
3822 } else if (sscanf(cmd, "%x", &mask) == 1) {
3823 /* mask set */
3824 } else {
3825 res = -EINVAL;
3826 goto errexit;
3830 if (!res) {
3831 tpacpi_disclose_usertask("procfs hotkey",
3832 "set mask to 0x%08x\n", mask);
3833 res = hotkey_user_mask_set(mask);
3836 errexit:
3837 mutex_unlock(&hotkey_mutex);
3838 return res;
3841 static const struct acpi_device_id ibm_htk_device_ids[] = {
3842 {TPACPI_ACPI_HKEY_HID, 0},
3843 {"", 0},
3846 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3847 .hid = ibm_htk_device_ids,
3848 .notify = hotkey_notify,
3849 .handle = &hkey_handle,
3850 .type = ACPI_DEVICE_NOTIFY,
3853 static struct ibm_struct hotkey_driver_data = {
3854 .name = "hotkey",
3855 .read = hotkey_read,
3856 .write = hotkey_write,
3857 .exit = hotkey_exit,
3858 .resume = hotkey_resume,
3859 .suspend = hotkey_suspend,
3860 .acpi = &ibm_hotkey_acpidriver,
3863 /*************************************************************************
3864 * Bluetooth subdriver
3867 enum {
3868 /* ACPI GBDC/SBDC bits */
3869 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3870 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3871 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3872 0 = disable, 1 = enable */
3875 enum {
3876 /* ACPI \BLTH commands */
3877 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3878 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3879 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3880 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3881 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3884 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3886 static int bluetooth_get_status(void)
3888 int status;
3890 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3891 if (dbg_bluetoothemul)
3892 return (tpacpi_bluetooth_emulstate) ?
3893 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3894 #endif
3896 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3897 return -EIO;
3899 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3900 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3903 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3905 int status;
3907 vdbg_printk(TPACPI_DBG_RFKILL,
3908 "will attempt to %s bluetooth\n",
3909 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3911 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3912 if (dbg_bluetoothemul) {
3913 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3914 return 0;
3916 #endif
3918 if (state == TPACPI_RFK_RADIO_ON)
3919 status = TP_ACPI_BLUETOOTH_RADIOSSW
3920 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3921 else
3922 status = 0;
3924 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3925 return -EIO;
3927 return 0;
3930 /* sysfs bluetooth enable ---------------------------------------------- */
3931 static ssize_t bluetooth_enable_show(struct device *dev,
3932 struct device_attribute *attr,
3933 char *buf)
3935 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3936 attr, buf);
3939 static ssize_t bluetooth_enable_store(struct device *dev,
3940 struct device_attribute *attr,
3941 const char *buf, size_t count)
3943 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3944 attr, buf, count);
3947 static struct device_attribute dev_attr_bluetooth_enable =
3948 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3949 bluetooth_enable_show, bluetooth_enable_store);
3951 /* --------------------------------------------------------------------- */
3953 static struct attribute *bluetooth_attributes[] = {
3954 &dev_attr_bluetooth_enable.attr,
3955 NULL
3958 static const struct attribute_group bluetooth_attr_group = {
3959 .attrs = bluetooth_attributes,
3962 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3963 .get_status = bluetooth_get_status,
3964 .set_status = bluetooth_set_status,
3967 static void bluetooth_shutdown(void)
3969 /* Order firmware to save current state to NVRAM */
3970 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3971 TP_ACPI_BLTH_SAVE_STATE))
3972 printk(TPACPI_NOTICE
3973 "failed to save bluetooth state to NVRAM\n");
3974 else
3975 vdbg_printk(TPACPI_DBG_RFKILL,
3976 "bluestooth state saved to NVRAM\n");
3979 static void bluetooth_exit(void)
3981 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3982 &bluetooth_attr_group);
3984 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3986 bluetooth_shutdown();
3989 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3991 int res;
3992 int status = 0;
3994 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3995 "initializing bluetooth subdriver\n");
3997 TPACPI_ACPIHANDLE_INIT(hkey);
3999 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4000 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4001 tp_features.bluetooth = hkey_handle &&
4002 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4004 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4005 "bluetooth is %s, status 0x%02x\n",
4006 str_supported(tp_features.bluetooth),
4007 status);
4009 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4010 if (dbg_bluetoothemul) {
4011 tp_features.bluetooth = 1;
4012 printk(TPACPI_INFO
4013 "bluetooth switch emulation enabled\n");
4014 } else
4015 #endif
4016 if (tp_features.bluetooth &&
4017 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4018 /* no bluetooth hardware present in system */
4019 tp_features.bluetooth = 0;
4020 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4021 "bluetooth hardware not installed\n");
4024 if (!tp_features.bluetooth)
4025 return 1;
4027 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4028 &bluetooth_tprfk_ops,
4029 RFKILL_TYPE_BLUETOOTH,
4030 TPACPI_RFK_BLUETOOTH_SW_NAME,
4031 true);
4032 if (res)
4033 return res;
4035 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4036 &bluetooth_attr_group);
4037 if (res) {
4038 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4039 return res;
4042 return 0;
4045 /* procfs -------------------------------------------------------------- */
4046 static int bluetooth_read(struct seq_file *m)
4048 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4051 static int bluetooth_write(char *buf)
4053 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4056 static struct ibm_struct bluetooth_driver_data = {
4057 .name = "bluetooth",
4058 .read = bluetooth_read,
4059 .write = bluetooth_write,
4060 .exit = bluetooth_exit,
4061 .shutdown = bluetooth_shutdown,
4064 /*************************************************************************
4065 * Wan subdriver
4068 enum {
4069 /* ACPI GWAN/SWAN bits */
4070 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4071 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4072 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4073 0 = disable, 1 = enable */
4076 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4078 static int wan_get_status(void)
4080 int status;
4082 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4083 if (dbg_wwanemul)
4084 return (tpacpi_wwan_emulstate) ?
4085 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4086 #endif
4088 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4089 return -EIO;
4091 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4092 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4095 static int wan_set_status(enum tpacpi_rfkill_state state)
4097 int status;
4099 vdbg_printk(TPACPI_DBG_RFKILL,
4100 "will attempt to %s wwan\n",
4101 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4103 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4104 if (dbg_wwanemul) {
4105 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4106 return 0;
4108 #endif
4110 if (state == TPACPI_RFK_RADIO_ON)
4111 status = TP_ACPI_WANCARD_RADIOSSW
4112 | TP_ACPI_WANCARD_RESUMECTRL;
4113 else
4114 status = 0;
4116 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4117 return -EIO;
4119 return 0;
4122 /* sysfs wan enable ---------------------------------------------------- */
4123 static ssize_t wan_enable_show(struct device *dev,
4124 struct device_attribute *attr,
4125 char *buf)
4127 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4128 attr, buf);
4131 static ssize_t wan_enable_store(struct device *dev,
4132 struct device_attribute *attr,
4133 const char *buf, size_t count)
4135 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4136 attr, buf, count);
4139 static struct device_attribute dev_attr_wan_enable =
4140 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4141 wan_enable_show, wan_enable_store);
4143 /* --------------------------------------------------------------------- */
4145 static struct attribute *wan_attributes[] = {
4146 &dev_attr_wan_enable.attr,
4147 NULL
4150 static const struct attribute_group wan_attr_group = {
4151 .attrs = wan_attributes,
4154 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4155 .get_status = wan_get_status,
4156 .set_status = wan_set_status,
4159 static void wan_shutdown(void)
4161 /* Order firmware to save current state to NVRAM */
4162 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4163 TP_ACPI_WGSV_SAVE_STATE))
4164 printk(TPACPI_NOTICE
4165 "failed to save WWAN state to NVRAM\n");
4166 else
4167 vdbg_printk(TPACPI_DBG_RFKILL,
4168 "WWAN state saved to NVRAM\n");
4171 static void wan_exit(void)
4173 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4174 &wan_attr_group);
4176 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4178 wan_shutdown();
4181 static int __init wan_init(struct ibm_init_struct *iibm)
4183 int res;
4184 int status = 0;
4186 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4187 "initializing wan subdriver\n");
4189 TPACPI_ACPIHANDLE_INIT(hkey);
4191 tp_features.wan = hkey_handle &&
4192 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4194 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4195 "wan is %s, status 0x%02x\n",
4196 str_supported(tp_features.wan),
4197 status);
4199 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4200 if (dbg_wwanemul) {
4201 tp_features.wan = 1;
4202 printk(TPACPI_INFO
4203 "wwan switch emulation enabled\n");
4204 } else
4205 #endif
4206 if (tp_features.wan &&
4207 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4208 /* no wan hardware present in system */
4209 tp_features.wan = 0;
4210 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4211 "wan hardware not installed\n");
4214 if (!tp_features.wan)
4215 return 1;
4217 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4218 &wan_tprfk_ops,
4219 RFKILL_TYPE_WWAN,
4220 TPACPI_RFK_WWAN_SW_NAME,
4221 true);
4222 if (res)
4223 return res;
4225 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4226 &wan_attr_group);
4228 if (res) {
4229 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4230 return res;
4233 return 0;
4236 /* procfs -------------------------------------------------------------- */
4237 static int wan_read(struct seq_file *m)
4239 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4242 static int wan_write(char *buf)
4244 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4247 static struct ibm_struct wan_driver_data = {
4248 .name = "wan",
4249 .read = wan_read,
4250 .write = wan_write,
4251 .exit = wan_exit,
4252 .shutdown = wan_shutdown,
4255 /*************************************************************************
4256 * UWB subdriver
4259 enum {
4260 /* ACPI GUWB/SUWB bits */
4261 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4262 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4265 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4267 static int uwb_get_status(void)
4269 int status;
4271 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4272 if (dbg_uwbemul)
4273 return (tpacpi_uwb_emulstate) ?
4274 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4275 #endif
4277 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4278 return -EIO;
4280 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4281 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4284 static int uwb_set_status(enum tpacpi_rfkill_state state)
4286 int status;
4288 vdbg_printk(TPACPI_DBG_RFKILL,
4289 "will attempt to %s UWB\n",
4290 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4292 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4293 if (dbg_uwbemul) {
4294 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4295 return 0;
4297 #endif
4299 if (state == TPACPI_RFK_RADIO_ON)
4300 status = TP_ACPI_UWB_RADIOSSW;
4301 else
4302 status = 0;
4304 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4305 return -EIO;
4307 return 0;
4310 /* --------------------------------------------------------------------- */
4312 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4313 .get_status = uwb_get_status,
4314 .set_status = uwb_set_status,
4317 static void uwb_exit(void)
4319 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4322 static int __init uwb_init(struct ibm_init_struct *iibm)
4324 int res;
4325 int status = 0;
4327 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4328 "initializing uwb subdriver\n");
4330 TPACPI_ACPIHANDLE_INIT(hkey);
4332 tp_features.uwb = hkey_handle &&
4333 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4335 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4336 "uwb is %s, status 0x%02x\n",
4337 str_supported(tp_features.uwb),
4338 status);
4340 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4341 if (dbg_uwbemul) {
4342 tp_features.uwb = 1;
4343 printk(TPACPI_INFO
4344 "uwb switch emulation enabled\n");
4345 } else
4346 #endif
4347 if (tp_features.uwb &&
4348 !(status & TP_ACPI_UWB_HWPRESENT)) {
4349 /* no uwb hardware present in system */
4350 tp_features.uwb = 0;
4351 dbg_printk(TPACPI_DBG_INIT,
4352 "uwb hardware not installed\n");
4355 if (!tp_features.uwb)
4356 return 1;
4358 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4359 &uwb_tprfk_ops,
4360 RFKILL_TYPE_UWB,
4361 TPACPI_RFK_UWB_SW_NAME,
4362 false);
4363 return res;
4366 static struct ibm_struct uwb_driver_data = {
4367 .name = "uwb",
4368 .exit = uwb_exit,
4369 .flags.experimental = 1,
4372 /*************************************************************************
4373 * Video subdriver
4376 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4378 enum video_access_mode {
4379 TPACPI_VIDEO_NONE = 0,
4380 TPACPI_VIDEO_570, /* 570 */
4381 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4382 TPACPI_VIDEO_NEW, /* all others */
4385 enum { /* video status flags, based on VIDEO_570 */
4386 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4387 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4388 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4391 enum { /* TPACPI_VIDEO_570 constants */
4392 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4393 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4394 * video_status_flags */
4395 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4396 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4399 static enum video_access_mode video_supported;
4400 static int video_orig_autosw;
4402 static int video_autosw_get(void);
4403 static int video_autosw_set(int enable);
4405 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4407 static int __init video_init(struct ibm_init_struct *iibm)
4409 int ivga;
4411 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4413 TPACPI_ACPIHANDLE_INIT(vid);
4414 TPACPI_ACPIHANDLE_INIT(vid2);
4416 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4417 /* G41, assume IVGA doesn't change */
4418 vid_handle = vid2_handle;
4420 if (!vid_handle)
4421 /* video switching not supported on R30, R31 */
4422 video_supported = TPACPI_VIDEO_NONE;
4423 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4424 /* 570 */
4425 video_supported = TPACPI_VIDEO_570;
4426 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4427 /* 600e/x, 770e, 770x */
4428 video_supported = TPACPI_VIDEO_770;
4429 else
4430 /* all others */
4431 video_supported = TPACPI_VIDEO_NEW;
4433 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4434 str_supported(video_supported != TPACPI_VIDEO_NONE),
4435 video_supported);
4437 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4440 static void video_exit(void)
4442 dbg_printk(TPACPI_DBG_EXIT,
4443 "restoring original video autoswitch mode\n");
4444 if (video_autosw_set(video_orig_autosw))
4445 printk(TPACPI_ERR "error while trying to restore original "
4446 "video autoswitch mode\n");
4449 static int video_outputsw_get(void)
4451 int status = 0;
4452 int i;
4454 switch (video_supported) {
4455 case TPACPI_VIDEO_570:
4456 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4457 TP_ACPI_VIDEO_570_PHSCMD))
4458 return -EIO;
4459 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4460 break;
4461 case TPACPI_VIDEO_770:
4462 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4463 return -EIO;
4464 if (i)
4465 status |= TP_ACPI_VIDEO_S_LCD;
4466 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4467 return -EIO;
4468 if (i)
4469 status |= TP_ACPI_VIDEO_S_CRT;
4470 break;
4471 case TPACPI_VIDEO_NEW:
4472 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4473 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4474 return -EIO;
4475 if (i)
4476 status |= TP_ACPI_VIDEO_S_CRT;
4478 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4479 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4480 return -EIO;
4481 if (i)
4482 status |= TP_ACPI_VIDEO_S_LCD;
4483 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4484 return -EIO;
4485 if (i)
4486 status |= TP_ACPI_VIDEO_S_DVI;
4487 break;
4488 default:
4489 return -ENOSYS;
4492 return status;
4495 static int video_outputsw_set(int status)
4497 int autosw;
4498 int res = 0;
4500 switch (video_supported) {
4501 case TPACPI_VIDEO_570:
4502 res = acpi_evalf(NULL, NULL,
4503 "\\_SB.PHS2", "vdd",
4504 TP_ACPI_VIDEO_570_PHS2CMD,
4505 status | TP_ACPI_VIDEO_570_PHS2SET);
4506 break;
4507 case TPACPI_VIDEO_770:
4508 autosw = video_autosw_get();
4509 if (autosw < 0)
4510 return autosw;
4512 res = video_autosw_set(1);
4513 if (res)
4514 return res;
4515 res = acpi_evalf(vid_handle, NULL,
4516 "ASWT", "vdd", status * 0x100, 0);
4517 if (!autosw && video_autosw_set(autosw)) {
4518 printk(TPACPI_ERR
4519 "video auto-switch left enabled due to error\n");
4520 return -EIO;
4522 break;
4523 case TPACPI_VIDEO_NEW:
4524 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4525 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4526 break;
4527 default:
4528 return -ENOSYS;
4531 return (res)? 0 : -EIO;
4534 static int video_autosw_get(void)
4536 int autosw = 0;
4538 switch (video_supported) {
4539 case TPACPI_VIDEO_570:
4540 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4541 return -EIO;
4542 break;
4543 case TPACPI_VIDEO_770:
4544 case TPACPI_VIDEO_NEW:
4545 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4546 return -EIO;
4547 break;
4548 default:
4549 return -ENOSYS;
4552 return autosw & 1;
4555 static int video_autosw_set(int enable)
4557 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4558 return -EIO;
4559 return 0;
4562 static int video_outputsw_cycle(void)
4564 int autosw = video_autosw_get();
4565 int res;
4567 if (autosw < 0)
4568 return autosw;
4570 switch (video_supported) {
4571 case TPACPI_VIDEO_570:
4572 res = video_autosw_set(1);
4573 if (res)
4574 return res;
4575 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4576 break;
4577 case TPACPI_VIDEO_770:
4578 case TPACPI_VIDEO_NEW:
4579 res = video_autosw_set(1);
4580 if (res)
4581 return res;
4582 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4583 break;
4584 default:
4585 return -ENOSYS;
4587 if (!autosw && video_autosw_set(autosw)) {
4588 printk(TPACPI_ERR
4589 "video auto-switch left enabled due to error\n");
4590 return -EIO;
4593 return (res)? 0 : -EIO;
4596 static int video_expand_toggle(void)
4598 switch (video_supported) {
4599 case TPACPI_VIDEO_570:
4600 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4601 0 : -EIO;
4602 case TPACPI_VIDEO_770:
4603 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4604 0 : -EIO;
4605 case TPACPI_VIDEO_NEW:
4606 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4607 0 : -EIO;
4608 default:
4609 return -ENOSYS;
4611 /* not reached */
4614 static int video_read(struct seq_file *m)
4616 int status, autosw;
4618 if (video_supported == TPACPI_VIDEO_NONE) {
4619 seq_printf(m, "status:\t\tnot supported\n");
4620 return 0;
4623 /* Even reads can crash X.org, so... */
4624 if (!capable(CAP_SYS_ADMIN))
4625 return -EPERM;
4627 status = video_outputsw_get();
4628 if (status < 0)
4629 return status;
4631 autosw = video_autosw_get();
4632 if (autosw < 0)
4633 return autosw;
4635 seq_printf(m, "status:\t\tsupported\n");
4636 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4637 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4638 if (video_supported == TPACPI_VIDEO_NEW)
4639 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4640 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4641 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4642 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4643 if (video_supported == TPACPI_VIDEO_NEW)
4644 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4645 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4646 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4648 return 0;
4651 static int video_write(char *buf)
4653 char *cmd;
4654 int enable, disable, status;
4655 int res;
4657 if (video_supported == TPACPI_VIDEO_NONE)
4658 return -ENODEV;
4660 /* Even reads can crash X.org, let alone writes... */
4661 if (!capable(CAP_SYS_ADMIN))
4662 return -EPERM;
4664 enable = 0;
4665 disable = 0;
4667 while ((cmd = next_cmd(&buf))) {
4668 if (strlencmp(cmd, "lcd_enable") == 0) {
4669 enable |= TP_ACPI_VIDEO_S_LCD;
4670 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4671 disable |= TP_ACPI_VIDEO_S_LCD;
4672 } else if (strlencmp(cmd, "crt_enable") == 0) {
4673 enable |= TP_ACPI_VIDEO_S_CRT;
4674 } else if (strlencmp(cmd, "crt_disable") == 0) {
4675 disable |= TP_ACPI_VIDEO_S_CRT;
4676 } else if (video_supported == TPACPI_VIDEO_NEW &&
4677 strlencmp(cmd, "dvi_enable") == 0) {
4678 enable |= TP_ACPI_VIDEO_S_DVI;
4679 } else if (video_supported == TPACPI_VIDEO_NEW &&
4680 strlencmp(cmd, "dvi_disable") == 0) {
4681 disable |= TP_ACPI_VIDEO_S_DVI;
4682 } else if (strlencmp(cmd, "auto_enable") == 0) {
4683 res = video_autosw_set(1);
4684 if (res)
4685 return res;
4686 } else if (strlencmp(cmd, "auto_disable") == 0) {
4687 res = video_autosw_set(0);
4688 if (res)
4689 return res;
4690 } else if (strlencmp(cmd, "video_switch") == 0) {
4691 res = video_outputsw_cycle();
4692 if (res)
4693 return res;
4694 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4695 res = video_expand_toggle();
4696 if (res)
4697 return res;
4698 } else
4699 return -EINVAL;
4702 if (enable || disable) {
4703 status = video_outputsw_get();
4704 if (status < 0)
4705 return status;
4706 res = video_outputsw_set((status & ~disable) | enable);
4707 if (res)
4708 return res;
4711 return 0;
4714 static struct ibm_struct video_driver_data = {
4715 .name = "video",
4716 .read = video_read,
4717 .write = video_write,
4718 .exit = video_exit,
4721 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4723 /*************************************************************************
4724 * Light (thinklight) subdriver
4727 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4728 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4730 static int light_get_status(void)
4732 int status = 0;
4734 if (tp_features.light_status) {
4735 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4736 return -EIO;
4737 return (!!status);
4740 return -ENXIO;
4743 static int light_set_status(int status)
4745 int rc;
4747 if (tp_features.light) {
4748 if (cmos_handle) {
4749 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4750 (status)?
4751 TP_CMOS_THINKLIGHT_ON :
4752 TP_CMOS_THINKLIGHT_OFF);
4753 } else {
4754 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4755 (status)? 1 : 0);
4757 return (rc)? 0 : -EIO;
4760 return -ENXIO;
4763 static void light_set_status_worker(struct work_struct *work)
4765 struct tpacpi_led_classdev *data =
4766 container_of(work, struct tpacpi_led_classdev, work);
4768 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4769 light_set_status((data->new_state != TPACPI_LED_OFF));
4772 static void light_sysfs_set(struct led_classdev *led_cdev,
4773 enum led_brightness brightness)
4775 struct tpacpi_led_classdev *data =
4776 container_of(led_cdev,
4777 struct tpacpi_led_classdev,
4778 led_classdev);
4779 data->new_state = (brightness != LED_OFF) ?
4780 TPACPI_LED_ON : TPACPI_LED_OFF;
4781 queue_work(tpacpi_wq, &data->work);
4784 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4786 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4789 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4790 .led_classdev = {
4791 .name = "tpacpi::thinklight",
4792 .brightness_set = &light_sysfs_set,
4793 .brightness_get = &light_sysfs_get,
4797 static int __init light_init(struct ibm_init_struct *iibm)
4799 int rc;
4801 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4803 TPACPI_ACPIHANDLE_INIT(ledb);
4804 TPACPI_ACPIHANDLE_INIT(lght);
4805 TPACPI_ACPIHANDLE_INIT(cmos);
4806 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4808 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4809 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4811 if (tp_features.light)
4812 /* light status not supported on
4813 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4814 tp_features.light_status =
4815 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4817 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4818 str_supported(tp_features.light),
4819 str_supported(tp_features.light_status));
4821 if (!tp_features.light)
4822 return 1;
4824 rc = led_classdev_register(&tpacpi_pdev->dev,
4825 &tpacpi_led_thinklight.led_classdev);
4827 if (rc < 0) {
4828 tp_features.light = 0;
4829 tp_features.light_status = 0;
4830 } else {
4831 rc = 0;
4834 return rc;
4837 static void light_exit(void)
4839 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4840 if (work_pending(&tpacpi_led_thinklight.work))
4841 flush_workqueue(tpacpi_wq);
4844 static int light_read(struct seq_file *m)
4846 int status;
4848 if (!tp_features.light) {
4849 seq_printf(m, "status:\t\tnot supported\n");
4850 } else if (!tp_features.light_status) {
4851 seq_printf(m, "status:\t\tunknown\n");
4852 seq_printf(m, "commands:\ton, off\n");
4853 } else {
4854 status = light_get_status();
4855 if (status < 0)
4856 return status;
4857 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4858 seq_printf(m, "commands:\ton, off\n");
4861 return 0;
4864 static int light_write(char *buf)
4866 char *cmd;
4867 int newstatus = 0;
4869 if (!tp_features.light)
4870 return -ENODEV;
4872 while ((cmd = next_cmd(&buf))) {
4873 if (strlencmp(cmd, "on") == 0) {
4874 newstatus = 1;
4875 } else if (strlencmp(cmd, "off") == 0) {
4876 newstatus = 0;
4877 } else
4878 return -EINVAL;
4881 return light_set_status(newstatus);
4884 static struct ibm_struct light_driver_data = {
4885 .name = "light",
4886 .read = light_read,
4887 .write = light_write,
4888 .exit = light_exit,
4891 /*************************************************************************
4892 * CMOS subdriver
4895 /* sysfs cmos_command -------------------------------------------------- */
4896 static ssize_t cmos_command_store(struct device *dev,
4897 struct device_attribute *attr,
4898 const char *buf, size_t count)
4900 unsigned long cmos_cmd;
4901 int res;
4903 if (parse_strtoul(buf, 21, &cmos_cmd))
4904 return -EINVAL;
4906 res = issue_thinkpad_cmos_command(cmos_cmd);
4907 return (res)? res : count;
4910 static struct device_attribute dev_attr_cmos_command =
4911 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4913 /* --------------------------------------------------------------------- */
4915 static int __init cmos_init(struct ibm_init_struct *iibm)
4917 int res;
4919 vdbg_printk(TPACPI_DBG_INIT,
4920 "initializing cmos commands subdriver\n");
4922 TPACPI_ACPIHANDLE_INIT(cmos);
4924 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4925 str_supported(cmos_handle != NULL));
4927 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4928 if (res)
4929 return res;
4931 return (cmos_handle)? 0 : 1;
4934 static void cmos_exit(void)
4936 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4939 static int cmos_read(struct seq_file *m)
4941 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4942 R30, R31, T20-22, X20-21 */
4943 if (!cmos_handle)
4944 seq_printf(m, "status:\t\tnot supported\n");
4945 else {
4946 seq_printf(m, "status:\t\tsupported\n");
4947 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4950 return 0;
4953 static int cmos_write(char *buf)
4955 char *cmd;
4956 int cmos_cmd, res;
4958 while ((cmd = next_cmd(&buf))) {
4959 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4960 cmos_cmd >= 0 && cmos_cmd <= 21) {
4961 /* cmos_cmd set */
4962 } else
4963 return -EINVAL;
4965 res = issue_thinkpad_cmos_command(cmos_cmd);
4966 if (res)
4967 return res;
4970 return 0;
4973 static struct ibm_struct cmos_driver_data = {
4974 .name = "cmos",
4975 .read = cmos_read,
4976 .write = cmos_write,
4977 .exit = cmos_exit,
4980 /*************************************************************************
4981 * LED subdriver
4984 enum led_access_mode {
4985 TPACPI_LED_NONE = 0,
4986 TPACPI_LED_570, /* 570 */
4987 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4988 TPACPI_LED_NEW, /* all others */
4991 enum { /* For TPACPI_LED_OLD */
4992 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4993 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4994 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4997 static enum led_access_mode led_supported;
4999 static acpi_handle led_handle;
5001 #define TPACPI_LED_NUMLEDS 16
5002 static struct tpacpi_led_classdev *tpacpi_leds;
5003 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5004 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5005 /* there's a limit of 19 chars + NULL before 2.6.26 */
5006 "tpacpi::power",
5007 "tpacpi:orange:batt",
5008 "tpacpi:green:batt",
5009 "tpacpi::dock_active",
5010 "tpacpi::bay_active",
5011 "tpacpi::dock_batt",
5012 "tpacpi::unknown_led",
5013 "tpacpi::standby",
5014 "tpacpi::dock_status1",
5015 "tpacpi::dock_status2",
5016 "tpacpi::unknown_led2",
5017 "tpacpi::unknown_led3",
5018 "tpacpi::thinkvantage",
5020 #define TPACPI_SAFE_LEDS 0x1081U
5022 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5024 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5025 return false;
5026 #else
5027 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5028 #endif
5031 static int led_get_status(const unsigned int led)
5033 int status;
5034 enum led_status_t led_s;
5036 switch (led_supported) {
5037 case TPACPI_LED_570:
5038 if (!acpi_evalf(ec_handle,
5039 &status, "GLED", "dd", 1 << led))
5040 return -EIO;
5041 led_s = (status == 0)?
5042 TPACPI_LED_OFF :
5043 ((status == 1)?
5044 TPACPI_LED_ON :
5045 TPACPI_LED_BLINK);
5046 tpacpi_led_state_cache[led] = led_s;
5047 return led_s;
5048 default:
5049 return -ENXIO;
5052 /* not reached */
5055 static int led_set_status(const unsigned int led,
5056 const enum led_status_t ledstatus)
5058 /* off, on, blink. Index is led_status_t */
5059 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5060 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5062 int rc = 0;
5064 switch (led_supported) {
5065 case TPACPI_LED_570:
5066 /* 570 */
5067 if (unlikely(led > 7))
5068 return -EINVAL;
5069 if (unlikely(tpacpi_is_led_restricted(led)))
5070 return -EPERM;
5071 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5072 (1 << led), led_sled_arg1[ledstatus]))
5073 rc = -EIO;
5074 break;
5075 case TPACPI_LED_OLD:
5076 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5077 if (unlikely(led > 7))
5078 return -EINVAL;
5079 if (unlikely(tpacpi_is_led_restricted(led)))
5080 return -EPERM;
5081 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5082 if (rc >= 0)
5083 rc = ec_write(TPACPI_LED_EC_HLBL,
5084 (ledstatus == TPACPI_LED_BLINK) << led);
5085 if (rc >= 0)
5086 rc = ec_write(TPACPI_LED_EC_HLCL,
5087 (ledstatus != TPACPI_LED_OFF) << led);
5088 break;
5089 case TPACPI_LED_NEW:
5090 /* all others */
5091 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5092 return -EINVAL;
5093 if (unlikely(tpacpi_is_led_restricted(led)))
5094 return -EPERM;
5095 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5096 led, led_led_arg1[ledstatus]))
5097 rc = -EIO;
5098 break;
5099 default:
5100 rc = -ENXIO;
5103 if (!rc)
5104 tpacpi_led_state_cache[led] = ledstatus;
5106 return rc;
5109 static void led_set_status_worker(struct work_struct *work)
5111 struct tpacpi_led_classdev *data =
5112 container_of(work, struct tpacpi_led_classdev, work);
5114 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5115 led_set_status(data->led, data->new_state);
5118 static void led_sysfs_set(struct led_classdev *led_cdev,
5119 enum led_brightness brightness)
5121 struct tpacpi_led_classdev *data = container_of(led_cdev,
5122 struct tpacpi_led_classdev, led_classdev);
5124 if (brightness == LED_OFF)
5125 data->new_state = TPACPI_LED_OFF;
5126 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5127 data->new_state = TPACPI_LED_ON;
5128 else
5129 data->new_state = TPACPI_LED_BLINK;
5131 queue_work(tpacpi_wq, &data->work);
5134 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5135 unsigned long *delay_on, unsigned long *delay_off)
5137 struct tpacpi_led_classdev *data = container_of(led_cdev,
5138 struct tpacpi_led_classdev, led_classdev);
5140 /* Can we choose the flash rate? */
5141 if (*delay_on == 0 && *delay_off == 0) {
5142 /* yes. set them to the hardware blink rate (1 Hz) */
5143 *delay_on = 500; /* ms */
5144 *delay_off = 500; /* ms */
5145 } else if ((*delay_on != 500) || (*delay_off != 500))
5146 return -EINVAL;
5148 data->new_state = TPACPI_LED_BLINK;
5149 queue_work(tpacpi_wq, &data->work);
5151 return 0;
5154 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5156 int rc;
5158 struct tpacpi_led_classdev *data = container_of(led_cdev,
5159 struct tpacpi_led_classdev, led_classdev);
5161 rc = led_get_status(data->led);
5163 if (rc == TPACPI_LED_OFF || rc < 0)
5164 rc = LED_OFF; /* no error handling in led class :( */
5165 else
5166 rc = LED_FULL;
5168 return rc;
5171 static void led_exit(void)
5173 unsigned int i;
5175 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5176 if (tpacpi_leds[i].led_classdev.name)
5177 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5180 kfree(tpacpi_leds);
5183 static int __init tpacpi_init_led(unsigned int led)
5185 int rc;
5187 tpacpi_leds[led].led = led;
5189 /* LEDs with no name don't get registered */
5190 if (!tpacpi_led_names[led])
5191 return 0;
5193 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5194 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5195 if (led_supported == TPACPI_LED_570)
5196 tpacpi_leds[led].led_classdev.brightness_get =
5197 &led_sysfs_get;
5199 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5201 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5203 rc = led_classdev_register(&tpacpi_pdev->dev,
5204 &tpacpi_leds[led].led_classdev);
5205 if (rc < 0)
5206 tpacpi_leds[led].led_classdev.name = NULL;
5208 return rc;
5211 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5212 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5213 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5214 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5216 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5217 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5218 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5219 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5220 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5221 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5222 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5223 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5225 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5226 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5227 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5228 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5229 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5231 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5232 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5233 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5234 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5236 /* (1) - may have excess leds enabled on MSB */
5238 /* Defaults (order matters, keep last, don't reorder!) */
5239 { /* Lenovo */
5240 .vendor = PCI_VENDOR_ID_LENOVO,
5241 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5242 .quirks = 0x1fffU,
5244 { /* IBM ThinkPads with no EC version string */
5245 .vendor = PCI_VENDOR_ID_IBM,
5246 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5247 .quirks = 0x00ffU,
5249 { /* IBM ThinkPads with EC version string */
5250 .vendor = PCI_VENDOR_ID_IBM,
5251 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5252 .quirks = 0x00bfU,
5256 #undef TPACPI_LEDQ_IBM
5257 #undef TPACPI_LEDQ_LNV
5259 static enum led_access_mode __init led_init_detect_mode(void)
5261 acpi_status status;
5263 /* 570 */
5264 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5265 if (ACPI_SUCCESS(status))
5266 return TPACPI_LED_570;
5268 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5269 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5270 if (ACPI_SUCCESS(status))
5271 return TPACPI_LED_OLD;
5273 /* most others */
5274 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5275 if (ACPI_SUCCESS(status))
5276 return TPACPI_LED_NEW;
5278 /* R30, R31, and unknown firmwares */
5279 led_handle = NULL;
5280 return TPACPI_LED_NONE;
5283 static int __init led_init(struct ibm_init_struct *iibm)
5285 unsigned int i;
5286 int rc;
5287 unsigned long useful_leds;
5289 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5291 led_supported = led_init_detect_mode();
5292 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5293 str_supported(led_supported), led_supported);
5295 if (led_supported == TPACPI_LED_NONE)
5296 return 1;
5298 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5299 GFP_KERNEL);
5300 if (!tpacpi_leds) {
5301 printk(TPACPI_ERR "Out of memory for LED data\n");
5302 return -ENOMEM;
5305 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5306 ARRAY_SIZE(led_useful_qtable));
5308 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5309 if (!tpacpi_is_led_restricted(i) &&
5310 test_bit(i, &useful_leds)) {
5311 rc = tpacpi_init_led(i);
5312 if (rc < 0) {
5313 led_exit();
5314 return rc;
5319 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5320 printk(TPACPI_NOTICE
5321 "warning: userspace override of important "
5322 "firmware LEDs is enabled\n");
5323 #endif
5324 return 0;
5327 #define str_led_status(s) \
5328 ((s) == TPACPI_LED_OFF ? "off" : \
5329 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5331 static int led_read(struct seq_file *m)
5333 if (!led_supported) {
5334 seq_printf(m, "status:\t\tnot supported\n");
5335 return 0;
5337 seq_printf(m, "status:\t\tsupported\n");
5339 if (led_supported == TPACPI_LED_570) {
5340 /* 570 */
5341 int i, status;
5342 for (i = 0; i < 8; i++) {
5343 status = led_get_status(i);
5344 if (status < 0)
5345 return -EIO;
5346 seq_printf(m, "%d:\t\t%s\n",
5347 i, str_led_status(status));
5351 seq_printf(m, "commands:\t"
5352 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5354 return 0;
5357 static int led_write(char *buf)
5359 char *cmd;
5360 int led, rc;
5361 enum led_status_t s;
5363 if (!led_supported)
5364 return -ENODEV;
5366 while ((cmd = next_cmd(&buf))) {
5367 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5368 return -EINVAL;
5370 if (strstr(cmd, "off")) {
5371 s = TPACPI_LED_OFF;
5372 } else if (strstr(cmd, "on")) {
5373 s = TPACPI_LED_ON;
5374 } else if (strstr(cmd, "blink")) {
5375 s = TPACPI_LED_BLINK;
5376 } else {
5377 return -EINVAL;
5380 rc = led_set_status(led, s);
5381 if (rc < 0)
5382 return rc;
5385 return 0;
5388 static struct ibm_struct led_driver_data = {
5389 .name = "led",
5390 .read = led_read,
5391 .write = led_write,
5392 .exit = led_exit,
5395 /*************************************************************************
5396 * Beep subdriver
5399 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5401 #define TPACPI_BEEP_Q1 0x0001
5403 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5404 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5405 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5408 static int __init beep_init(struct ibm_init_struct *iibm)
5410 unsigned long quirks;
5412 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5414 TPACPI_ACPIHANDLE_INIT(beep);
5416 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5417 str_supported(beep_handle != NULL));
5419 quirks = tpacpi_check_quirks(beep_quirk_table,
5420 ARRAY_SIZE(beep_quirk_table));
5422 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5424 return (beep_handle)? 0 : 1;
5427 static int beep_read(struct seq_file *m)
5429 if (!beep_handle)
5430 seq_printf(m, "status:\t\tnot supported\n");
5431 else {
5432 seq_printf(m, "status:\t\tsupported\n");
5433 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5436 return 0;
5439 static int beep_write(char *buf)
5441 char *cmd;
5442 int beep_cmd;
5444 if (!beep_handle)
5445 return -ENODEV;
5447 while ((cmd = next_cmd(&buf))) {
5448 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5449 beep_cmd >= 0 && beep_cmd <= 17) {
5450 /* beep_cmd set */
5451 } else
5452 return -EINVAL;
5453 if (tp_features.beep_needs_two_args) {
5454 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5455 beep_cmd, 0))
5456 return -EIO;
5457 } else {
5458 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5459 beep_cmd))
5460 return -EIO;
5464 return 0;
5467 static struct ibm_struct beep_driver_data = {
5468 .name = "beep",
5469 .read = beep_read,
5470 .write = beep_write,
5473 /*************************************************************************
5474 * Thermal subdriver
5477 enum thermal_access_mode {
5478 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5479 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5480 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5481 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5482 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5485 enum { /* TPACPI_THERMAL_TPEC_* */
5486 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5487 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5488 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5490 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5494 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5495 struct ibm_thermal_sensors_struct {
5496 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5499 static enum thermal_access_mode thermal_read_mode;
5501 /* idx is zero-based */
5502 static int thermal_get_sensor(int idx, s32 *value)
5504 int t;
5505 s8 tmp;
5506 char tmpi[5];
5508 t = TP_EC_THERMAL_TMP0;
5510 switch (thermal_read_mode) {
5511 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5512 case TPACPI_THERMAL_TPEC_16:
5513 if (idx >= 8 && idx <= 15) {
5514 t = TP_EC_THERMAL_TMP8;
5515 idx -= 8;
5517 /* fallthrough */
5518 #endif
5519 case TPACPI_THERMAL_TPEC_8:
5520 if (idx <= 7) {
5521 if (!acpi_ec_read(t + idx, &tmp))
5522 return -EIO;
5523 *value = tmp * 1000;
5524 return 0;
5526 break;
5528 case TPACPI_THERMAL_ACPI_UPDT:
5529 if (idx <= 7) {
5530 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5531 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5532 return -EIO;
5533 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5534 return -EIO;
5535 *value = (t - 2732) * 100;
5536 return 0;
5538 break;
5540 case TPACPI_THERMAL_ACPI_TMP07:
5541 if (idx <= 7) {
5542 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5543 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5544 return -EIO;
5545 if (t > 127 || t < -127)
5546 t = TP_EC_THERMAL_TMP_NA;
5547 *value = t * 1000;
5548 return 0;
5550 break;
5552 case TPACPI_THERMAL_NONE:
5553 default:
5554 return -ENOSYS;
5557 return -EINVAL;
5560 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5562 int res, i;
5563 int n;
5565 n = 8;
5566 i = 0;
5568 if (!s)
5569 return -EINVAL;
5571 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5572 n = 16;
5574 for (i = 0 ; i < n; i++) {
5575 res = thermal_get_sensor(i, &s->temp[i]);
5576 if (res)
5577 return res;
5580 return n;
5583 static void thermal_dump_all_sensors(void)
5585 int n, i;
5586 struct ibm_thermal_sensors_struct t;
5588 n = thermal_get_sensors(&t);
5589 if (n <= 0)
5590 return;
5592 printk(TPACPI_NOTICE
5593 "temperatures (Celsius):");
5595 for (i = 0; i < n; i++) {
5596 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5597 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5598 else
5599 printk(KERN_CONT " N/A");
5602 printk(KERN_CONT "\n");
5605 /* sysfs temp##_input -------------------------------------------------- */
5607 static ssize_t thermal_temp_input_show(struct device *dev,
5608 struct device_attribute *attr,
5609 char *buf)
5611 struct sensor_device_attribute *sensor_attr =
5612 to_sensor_dev_attr(attr);
5613 int idx = sensor_attr->index;
5614 s32 value;
5615 int res;
5617 res = thermal_get_sensor(idx, &value);
5618 if (res)
5619 return res;
5620 if (value == TPACPI_THERMAL_SENSOR_NA)
5621 return -ENXIO;
5623 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5626 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5627 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5628 thermal_temp_input_show, NULL, _idxB)
5630 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5631 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5632 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5633 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5634 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5635 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5636 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5637 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5638 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5639 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5640 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5641 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5642 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5643 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5644 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5645 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5646 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5649 #define THERMAL_ATTRS(X) \
5650 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5652 static struct attribute *thermal_temp_input_attr[] = {
5653 THERMAL_ATTRS(8),
5654 THERMAL_ATTRS(9),
5655 THERMAL_ATTRS(10),
5656 THERMAL_ATTRS(11),
5657 THERMAL_ATTRS(12),
5658 THERMAL_ATTRS(13),
5659 THERMAL_ATTRS(14),
5660 THERMAL_ATTRS(15),
5661 THERMAL_ATTRS(0),
5662 THERMAL_ATTRS(1),
5663 THERMAL_ATTRS(2),
5664 THERMAL_ATTRS(3),
5665 THERMAL_ATTRS(4),
5666 THERMAL_ATTRS(5),
5667 THERMAL_ATTRS(6),
5668 THERMAL_ATTRS(7),
5669 NULL
5672 static const struct attribute_group thermal_temp_input16_group = {
5673 .attrs = thermal_temp_input_attr
5676 static const struct attribute_group thermal_temp_input8_group = {
5677 .attrs = &thermal_temp_input_attr[8]
5680 #undef THERMAL_SENSOR_ATTR_TEMP
5681 #undef THERMAL_ATTRS
5683 /* --------------------------------------------------------------------- */
5685 static int __init thermal_init(struct ibm_init_struct *iibm)
5687 u8 t, ta1, ta2;
5688 int i;
5689 int acpi_tmp7;
5690 int res;
5692 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5694 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5696 if (thinkpad_id.ec_model) {
5698 * Direct EC access mode: sensors at registers
5699 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5700 * non-implemented, thermal sensors return 0x80 when
5701 * not available
5704 ta1 = ta2 = 0;
5705 for (i = 0; i < 8; i++) {
5706 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5707 ta1 |= t;
5708 } else {
5709 ta1 = 0;
5710 break;
5712 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5713 ta2 |= t;
5714 } else {
5715 ta1 = 0;
5716 break;
5719 if (ta1 == 0) {
5720 /* This is sheer paranoia, but we handle it anyway */
5721 if (acpi_tmp7) {
5722 printk(TPACPI_ERR
5723 "ThinkPad ACPI EC access misbehaving, "
5724 "falling back to ACPI TMPx access "
5725 "mode\n");
5726 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5727 } else {
5728 printk(TPACPI_ERR
5729 "ThinkPad ACPI EC access misbehaving, "
5730 "disabling thermal sensors access\n");
5731 thermal_read_mode = TPACPI_THERMAL_NONE;
5733 } else {
5734 thermal_read_mode =
5735 (ta2 != 0) ?
5736 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5738 } else if (acpi_tmp7) {
5739 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5740 /* 600e/x, 770e, 770x */
5741 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5742 } else {
5743 /* Standard ACPI TMPx access, max 8 sensors */
5744 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5746 } else {
5747 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5748 thermal_read_mode = TPACPI_THERMAL_NONE;
5751 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5752 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5753 thermal_read_mode);
5755 switch (thermal_read_mode) {
5756 case TPACPI_THERMAL_TPEC_16:
5757 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5758 &thermal_temp_input16_group);
5759 if (res)
5760 return res;
5761 break;
5762 case TPACPI_THERMAL_TPEC_8:
5763 case TPACPI_THERMAL_ACPI_TMP07:
5764 case TPACPI_THERMAL_ACPI_UPDT:
5765 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5766 &thermal_temp_input8_group);
5767 if (res)
5768 return res;
5769 break;
5770 case TPACPI_THERMAL_NONE:
5771 default:
5772 return 1;
5775 return 0;
5778 static void thermal_exit(void)
5780 switch (thermal_read_mode) {
5781 case TPACPI_THERMAL_TPEC_16:
5782 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5783 &thermal_temp_input16_group);
5784 break;
5785 case TPACPI_THERMAL_TPEC_8:
5786 case TPACPI_THERMAL_ACPI_TMP07:
5787 case TPACPI_THERMAL_ACPI_UPDT:
5788 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5789 &thermal_temp_input8_group);
5790 break;
5791 case TPACPI_THERMAL_NONE:
5792 default:
5793 break;
5797 static int thermal_read(struct seq_file *m)
5799 int n, i;
5800 struct ibm_thermal_sensors_struct t;
5802 n = thermal_get_sensors(&t);
5803 if (unlikely(n < 0))
5804 return n;
5806 seq_printf(m, "temperatures:\t");
5808 if (n > 0) {
5809 for (i = 0; i < (n - 1); i++)
5810 seq_printf(m, "%d ", t.temp[i] / 1000);
5811 seq_printf(m, "%d\n", t.temp[i] / 1000);
5812 } else
5813 seq_printf(m, "not supported\n");
5815 return 0;
5818 static struct ibm_struct thermal_driver_data = {
5819 .name = "thermal",
5820 .read = thermal_read,
5821 .exit = thermal_exit,
5824 /*************************************************************************
5825 * EC Dump subdriver
5828 static u8 ecdump_regs[256];
5830 static int ecdump_read(struct seq_file *m)
5832 int i, j;
5833 u8 v;
5835 seq_printf(m, "EC "
5836 " +00 +01 +02 +03 +04 +05 +06 +07"
5837 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5838 for (i = 0; i < 256; i += 16) {
5839 seq_printf(m, "EC 0x%02x:", i);
5840 for (j = 0; j < 16; j++) {
5841 if (!acpi_ec_read(i + j, &v))
5842 break;
5843 if (v != ecdump_regs[i + j])
5844 seq_printf(m, " *%02x", v);
5845 else
5846 seq_printf(m, " %02x", v);
5847 ecdump_regs[i + j] = v;
5849 seq_putc(m, '\n');
5850 if (j != 16)
5851 break;
5854 /* These are way too dangerous to advertise openly... */
5855 #if 0
5856 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5857 " (<offset> is 00-ff, <value> is 00-ff)\n");
5858 seq_printf(m, "commands:\t0x<offset> <value> "
5859 " (<offset> is 00-ff, <value> is 0-255)\n");
5860 #endif
5861 return 0;
5864 static int ecdump_write(char *buf)
5866 char *cmd;
5867 int i, v;
5869 while ((cmd = next_cmd(&buf))) {
5870 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5871 /* i and v set */
5872 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5873 /* i and v set */
5874 } else
5875 return -EINVAL;
5876 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5877 if (!acpi_ec_write(i, v))
5878 return -EIO;
5879 } else
5880 return -EINVAL;
5883 return 0;
5886 static struct ibm_struct ecdump_driver_data = {
5887 .name = "ecdump",
5888 .read = ecdump_read,
5889 .write = ecdump_write,
5890 .flags.experimental = 1,
5893 /*************************************************************************
5894 * Backlight/brightness subdriver
5897 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5900 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5901 * CMOS NVRAM byte 0x5E, bits 0-3.
5903 * EC HBRV (0x31) has the following layout
5904 * Bit 7: unknown function
5905 * Bit 6: unknown function
5906 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5907 * Bit 4: must be set to zero to avoid problems
5908 * Bit 3-0: backlight brightness level
5910 * brightness_get_raw returns status data in the HBRV layout
5912 * WARNING: The X61 has been verified to use HBRV for something else, so
5913 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5914 * testing on the very early *60 Lenovo models...
5917 enum {
5918 TP_EC_BACKLIGHT = 0x31,
5920 /* TP_EC_BACKLIGHT bitmasks */
5921 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5922 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5923 TP_EC_BACKLIGHT_MAPSW = 0x20,
5926 enum tpacpi_brightness_access_mode {
5927 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5928 TPACPI_BRGHT_MODE_EC, /* EC control */
5929 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5930 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5931 TPACPI_BRGHT_MODE_MAX
5934 static struct backlight_device *ibm_backlight_device;
5936 static enum tpacpi_brightness_access_mode brightness_mode =
5937 TPACPI_BRGHT_MODE_MAX;
5939 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5941 static struct mutex brightness_mutex;
5943 /* NVRAM brightness access,
5944 * call with brightness_mutex held! */
5945 static unsigned int tpacpi_brightness_nvram_get(void)
5947 u8 lnvram;
5949 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5950 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5951 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5952 lnvram &= bright_maxlvl;
5954 return lnvram;
5957 static void tpacpi_brightness_checkpoint_nvram(void)
5959 u8 lec = 0;
5960 u8 b_nvram;
5962 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5963 return;
5965 vdbg_printk(TPACPI_DBG_BRGHT,
5966 "trying to checkpoint backlight level to NVRAM...\n");
5968 if (mutex_lock_killable(&brightness_mutex) < 0)
5969 return;
5971 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5972 goto unlock;
5973 lec &= TP_EC_BACKLIGHT_LVLMSK;
5974 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5976 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5977 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5978 /* NVRAM needs update */
5979 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5980 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5981 b_nvram |= lec;
5982 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5983 dbg_printk(TPACPI_DBG_BRGHT,
5984 "updated NVRAM backlight level to %u (0x%02x)\n",
5985 (unsigned int) lec, (unsigned int) b_nvram);
5986 } else
5987 vdbg_printk(TPACPI_DBG_BRGHT,
5988 "NVRAM backlight level already is %u (0x%02x)\n",
5989 (unsigned int) lec, (unsigned int) b_nvram);
5991 unlock:
5992 mutex_unlock(&brightness_mutex);
5996 /* call with brightness_mutex held! */
5997 static int tpacpi_brightness_get_raw(int *status)
5999 u8 lec = 0;
6001 switch (brightness_mode) {
6002 case TPACPI_BRGHT_MODE_UCMS_STEP:
6003 *status = tpacpi_brightness_nvram_get();
6004 return 0;
6005 case TPACPI_BRGHT_MODE_EC:
6006 case TPACPI_BRGHT_MODE_ECNVRAM:
6007 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6008 return -EIO;
6009 *status = lec;
6010 return 0;
6011 default:
6012 return -ENXIO;
6016 /* call with brightness_mutex held! */
6017 /* do NOT call with illegal backlight level value */
6018 static int tpacpi_brightness_set_ec(unsigned int value)
6020 u8 lec = 0;
6022 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6023 return -EIO;
6025 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6026 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6027 (value & TP_EC_BACKLIGHT_LVLMSK))))
6028 return -EIO;
6030 return 0;
6033 /* call with brightness_mutex held! */
6034 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6036 int cmos_cmd, inc;
6037 unsigned int current_value, i;
6039 current_value = tpacpi_brightness_nvram_get();
6041 if (value == current_value)
6042 return 0;
6044 cmos_cmd = (value > current_value) ?
6045 TP_CMOS_BRIGHTNESS_UP :
6046 TP_CMOS_BRIGHTNESS_DOWN;
6047 inc = (value > current_value) ? 1 : -1;
6049 for (i = current_value; i != value; i += inc)
6050 if (issue_thinkpad_cmos_command(cmos_cmd))
6051 return -EIO;
6053 return 0;
6056 /* May return EINTR which can always be mapped to ERESTARTSYS */
6057 static int brightness_set(unsigned int value)
6059 int res;
6061 if (value > bright_maxlvl || value < 0)
6062 return -EINVAL;
6064 vdbg_printk(TPACPI_DBG_BRGHT,
6065 "set backlight level to %d\n", value);
6067 res = mutex_lock_killable(&brightness_mutex);
6068 if (res < 0)
6069 return res;
6071 switch (brightness_mode) {
6072 case TPACPI_BRGHT_MODE_EC:
6073 case TPACPI_BRGHT_MODE_ECNVRAM:
6074 res = tpacpi_brightness_set_ec(value);
6075 break;
6076 case TPACPI_BRGHT_MODE_UCMS_STEP:
6077 res = tpacpi_brightness_set_ucmsstep(value);
6078 break;
6079 default:
6080 res = -ENXIO;
6083 mutex_unlock(&brightness_mutex);
6084 return res;
6087 /* sysfs backlight class ----------------------------------------------- */
6089 static int brightness_update_status(struct backlight_device *bd)
6091 unsigned int level =
6092 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6093 bd->props.power == FB_BLANK_UNBLANK) ?
6094 bd->props.brightness : 0;
6096 dbg_printk(TPACPI_DBG_BRGHT,
6097 "backlight: attempt to set level to %d\n",
6098 level);
6100 /* it is the backlight class's job (caller) to handle
6101 * EINTR and other errors properly */
6102 return brightness_set(level);
6105 static int brightness_get(struct backlight_device *bd)
6107 int status, res;
6109 res = mutex_lock_killable(&brightness_mutex);
6110 if (res < 0)
6111 return 0;
6113 res = tpacpi_brightness_get_raw(&status);
6115 mutex_unlock(&brightness_mutex);
6117 if (res < 0)
6118 return 0;
6120 return status & TP_EC_BACKLIGHT_LVLMSK;
6123 static void tpacpi_brightness_notify_change(void)
6125 backlight_force_update(ibm_backlight_device,
6126 BACKLIGHT_UPDATE_HOTKEY);
6129 static struct backlight_ops ibm_backlight_data = {
6130 .get_brightness = brightness_get,
6131 .update_status = brightness_update_status,
6134 /* --------------------------------------------------------------------- */
6136 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6138 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6139 union acpi_object *obj;
6140 int rc;
6142 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
6143 obj = (union acpi_object *)buffer.pointer;
6144 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6145 printk(TPACPI_ERR "Unknown _BCL data, "
6146 "please report this to %s\n", TPACPI_MAIL);
6147 rc = 0;
6148 } else {
6149 rc = obj->package.count;
6151 } else {
6152 return 0;
6155 kfree(buffer.pointer);
6156 return rc;
6159 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
6160 u32 lvl, void *context, void **rv)
6162 char name[ACPI_PATH_SEGMENT_LENGTH];
6163 struct acpi_buffer buffer = { sizeof(name), &name };
6165 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
6166 !strncmp("_BCL", name, sizeof(name) - 1)) {
6167 BUG_ON(!rv || !*rv);
6168 **(int **)rv = tpacpi_query_bcl_levels(handle);
6169 return AE_CTRL_TERMINATE;
6170 } else {
6171 return AE_OK;
6176 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6178 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6180 int status;
6181 int bcl_levels = 0;
6182 void *bcl_ptr = &bcl_levels;
6184 if (!vid_handle)
6185 TPACPI_ACPIHANDLE_INIT(vid);
6187 if (!vid_handle)
6188 return 0;
6191 * Search for a _BCL method, and execute it. This is safe on all
6192 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
6193 * BIOS in ACPI backlight control mode. We do NOT have to care
6194 * about calling the _BCL method in an enabled video device, any
6195 * will do for our purposes.
6198 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
6199 tpacpi_acpi_walk_find_bcl, NULL,
6200 &bcl_ptr);
6202 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
6203 tp_features.bright_acpimode = 1;
6204 return bcl_levels - 2;
6207 return 0;
6211 * These are only useful for models that have only one possibility
6212 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6213 * these quirks.
6215 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6216 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6217 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6219 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6220 /* Models with ATI GPUs known to require ECNVRAM mode */
6221 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6223 /* Models with ATI GPUs that can use ECNVRAM */
6224 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6225 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6226 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6227 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6229 /* Models with Intel Extreme Graphics 2 */
6230 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6231 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6232 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6234 /* Models with Intel GMA900 */
6235 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6236 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6237 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6241 * Returns < 0 for error, otherwise sets tp_features.bright_*
6242 * and bright_maxlvl.
6244 static void __init tpacpi_detect_brightness_capabilities(void)
6246 unsigned int b;
6248 vdbg_printk(TPACPI_DBG_INIT,
6249 "detecting firmware brightness interface capabilities\n");
6251 /* we could run a quirks check here (same table used by
6252 * brightness_init) if needed */
6255 * We always attempt to detect acpi support, so as to switch
6256 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6257 * going to publish a backlight interface
6259 b = tpacpi_check_std_acpi_brightness_support();
6260 switch (b) {
6261 case 16:
6262 bright_maxlvl = 15;
6263 printk(TPACPI_INFO
6264 "detected a 16-level brightness capable ThinkPad\n");
6265 break;
6266 case 8:
6267 case 0:
6268 bright_maxlvl = 7;
6269 printk(TPACPI_INFO
6270 "detected a 8-level brightness capable ThinkPad\n");
6271 break;
6272 default:
6273 printk(TPACPI_ERR
6274 "Unsupported brightness interface, "
6275 "please contact %s\n", TPACPI_MAIL);
6276 tp_features.bright_unkfw = 1;
6277 bright_maxlvl = b - 1;
6281 static int __init brightness_init(struct ibm_init_struct *iibm)
6283 int b;
6284 unsigned long quirks;
6286 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6288 mutex_init(&brightness_mutex);
6290 quirks = tpacpi_check_quirks(brightness_quirk_table,
6291 ARRAY_SIZE(brightness_quirk_table));
6293 /* tpacpi_detect_brightness_capabilities() must have run already */
6295 /* if it is unknown, we don't handle it: it wouldn't be safe */
6296 if (tp_features.bright_unkfw)
6297 return 1;
6299 if (tp_features.bright_acpimode) {
6300 if (acpi_video_backlight_support()) {
6301 if (brightness_enable > 1) {
6302 printk(TPACPI_NOTICE
6303 "Standard ACPI backlight interface "
6304 "available, not loading native one.\n");
6305 return 1;
6306 } else if (brightness_enable == 1) {
6307 printk(TPACPI_NOTICE
6308 "Backlight control force enabled, even if standard "
6309 "ACPI backlight interface is available\n");
6311 } else {
6312 if (brightness_enable > 1) {
6313 printk(TPACPI_NOTICE
6314 "Standard ACPI backlight interface not "
6315 "available, thinkpad_acpi native "
6316 "brightness control enabled\n");
6321 if (!brightness_enable) {
6322 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6323 "brightness support disabled by "
6324 "module parameter\n");
6325 return 1;
6329 * Check for module parameter bogosity, note that we
6330 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6331 * able to detect "unspecified"
6333 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6334 return -EINVAL;
6336 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6337 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6338 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6339 if (quirks & TPACPI_BRGHT_Q_EC)
6340 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6341 else
6342 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6344 dbg_printk(TPACPI_DBG_BRGHT,
6345 "driver auto-selected brightness_mode=%d\n",
6346 brightness_mode);
6349 /* Safety */
6350 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6351 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6352 brightness_mode == TPACPI_BRGHT_MODE_EC))
6353 return -EINVAL;
6355 if (tpacpi_brightness_get_raw(&b) < 0)
6356 return 1;
6358 ibm_backlight_device = backlight_device_register(
6359 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6360 &ibm_backlight_data);
6361 if (IS_ERR(ibm_backlight_device)) {
6362 int rc = PTR_ERR(ibm_backlight_device);
6363 ibm_backlight_device = NULL;
6364 printk(TPACPI_ERR "Could not register backlight device\n");
6365 return rc;
6367 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6368 "brightness is supported\n");
6370 if (quirks & TPACPI_BRGHT_Q_ASK) {
6371 printk(TPACPI_NOTICE
6372 "brightness: will use unverified default: "
6373 "brightness_mode=%d\n", brightness_mode);
6374 printk(TPACPI_NOTICE
6375 "brightness: please report to %s whether it works well "
6376 "or not on your ThinkPad\n", TPACPI_MAIL);
6379 ibm_backlight_device->props.max_brightness = bright_maxlvl;
6380 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6382 /* Added by mistake in early 2007. Probably useless, but it could
6383 * be working around some unknown firmware problem where the value
6384 * read at startup doesn't match the real hardware state... so leave
6385 * it in place just in case */
6386 backlight_update_status(ibm_backlight_device);
6388 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6389 "brightness: registering brightness hotkeys "
6390 "as change notification\n");
6391 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6392 | TP_ACPI_HKEY_BRGHTUP_MASK
6393 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6394 return 0;
6397 static void brightness_suspend(pm_message_t state)
6399 tpacpi_brightness_checkpoint_nvram();
6402 static void brightness_shutdown(void)
6404 tpacpi_brightness_checkpoint_nvram();
6407 static void brightness_exit(void)
6409 if (ibm_backlight_device) {
6410 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6411 "calling backlight_device_unregister()\n");
6412 backlight_device_unregister(ibm_backlight_device);
6415 tpacpi_brightness_checkpoint_nvram();
6418 static int brightness_read(struct seq_file *m)
6420 int level;
6422 level = brightness_get(NULL);
6423 if (level < 0) {
6424 seq_printf(m, "level:\t\tunreadable\n");
6425 } else {
6426 seq_printf(m, "level:\t\t%d\n", level);
6427 seq_printf(m, "commands:\tup, down\n");
6428 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6429 bright_maxlvl);
6432 return 0;
6435 static int brightness_write(char *buf)
6437 int level;
6438 int rc;
6439 char *cmd;
6441 level = brightness_get(NULL);
6442 if (level < 0)
6443 return level;
6445 while ((cmd = next_cmd(&buf))) {
6446 if (strlencmp(cmd, "up") == 0) {
6447 if (level < bright_maxlvl)
6448 level++;
6449 } else if (strlencmp(cmd, "down") == 0) {
6450 if (level > 0)
6451 level--;
6452 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6453 level >= 0 && level <= bright_maxlvl) {
6454 /* new level set */
6455 } else
6456 return -EINVAL;
6459 tpacpi_disclose_usertask("procfs brightness",
6460 "set level to %d\n", level);
6463 * Now we know what the final level should be, so we try to set it.
6464 * Doing it this way makes the syscall restartable in case of EINTR
6466 rc = brightness_set(level);
6467 if (!rc && ibm_backlight_device)
6468 backlight_force_update(ibm_backlight_device,
6469 BACKLIGHT_UPDATE_SYSFS);
6470 return (rc == -EINTR)? -ERESTARTSYS : rc;
6473 static struct ibm_struct brightness_driver_data = {
6474 .name = "brightness",
6475 .read = brightness_read,
6476 .write = brightness_write,
6477 .exit = brightness_exit,
6478 .suspend = brightness_suspend,
6479 .shutdown = brightness_shutdown,
6482 /*************************************************************************
6483 * Volume subdriver
6487 * IBM ThinkPads have a simple volume controller with MUTE gating.
6488 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6490 * Since the *61 series (and probably also the later *60 series), Lenovo
6491 * ThinkPads only implement the MUTE gate.
6493 * EC register 0x30
6494 * Bit 6: MUTE (1 mutes sound)
6495 * Bit 3-0: Volume
6496 * Other bits should be zero as far as we know.
6498 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6499 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6500 * such as bit 7 which is used to detect repeated presses of MUTE,
6501 * and we leave them unchanged.
6504 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6506 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6507 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6508 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6510 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6511 static char *alsa_id = "ThinkPadEC";
6512 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6514 struct tpacpi_alsa_data {
6515 struct snd_card *card;
6516 struct snd_ctl_elem_id *ctl_mute_id;
6517 struct snd_ctl_elem_id *ctl_vol_id;
6520 static struct snd_card *alsa_card;
6522 enum {
6523 TP_EC_AUDIO = 0x30,
6525 /* TP_EC_AUDIO bits */
6526 TP_EC_AUDIO_MUTESW = 6,
6528 /* TP_EC_AUDIO bitmasks */
6529 TP_EC_AUDIO_LVL_MSK = 0x0F,
6530 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6532 /* Maximum volume */
6533 TP_EC_VOLUME_MAX = 14,
6536 enum tpacpi_volume_access_mode {
6537 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6538 TPACPI_VOL_MODE_EC, /* Pure EC control */
6539 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6540 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6541 TPACPI_VOL_MODE_MAX
6544 enum tpacpi_volume_capabilities {
6545 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6546 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6547 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6548 TPACPI_VOL_CAP_MAX
6551 static enum tpacpi_volume_access_mode volume_mode =
6552 TPACPI_VOL_MODE_MAX;
6554 static enum tpacpi_volume_capabilities volume_capabilities;
6555 static int volume_control_allowed;
6558 * Used to syncronize writers to TP_EC_AUDIO and
6559 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6561 static struct mutex volume_mutex;
6563 static void tpacpi_volume_checkpoint_nvram(void)
6565 u8 lec = 0;
6566 u8 b_nvram;
6567 u8 ec_mask;
6569 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6570 return;
6571 if (!volume_control_allowed)
6572 return;
6574 vdbg_printk(TPACPI_DBG_MIXER,
6575 "trying to checkpoint mixer state to NVRAM...\n");
6577 if (tp_features.mixer_no_level_control)
6578 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6579 else
6580 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6582 if (mutex_lock_killable(&volume_mutex) < 0)
6583 return;
6585 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6586 goto unlock;
6587 lec &= ec_mask;
6588 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6590 if (lec != (b_nvram & ec_mask)) {
6591 /* NVRAM needs update */
6592 b_nvram &= ~ec_mask;
6593 b_nvram |= lec;
6594 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6595 dbg_printk(TPACPI_DBG_MIXER,
6596 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6597 (unsigned int) lec, (unsigned int) b_nvram);
6598 } else {
6599 vdbg_printk(TPACPI_DBG_MIXER,
6600 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6601 (unsigned int) lec, (unsigned int) b_nvram);
6604 unlock:
6605 mutex_unlock(&volume_mutex);
6608 static int volume_get_status_ec(u8 *status)
6610 u8 s;
6612 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6613 return -EIO;
6615 *status = s;
6617 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6619 return 0;
6622 static int volume_get_status(u8 *status)
6624 return volume_get_status_ec(status);
6627 static int volume_set_status_ec(const u8 status)
6629 if (!acpi_ec_write(TP_EC_AUDIO, status))
6630 return -EIO;
6632 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6634 return 0;
6637 static int volume_set_status(const u8 status)
6639 return volume_set_status_ec(status);
6642 /* returns < 0 on error, 0 on no change, 1 on change */
6643 static int __volume_set_mute_ec(const bool mute)
6645 int rc;
6646 u8 s, n;
6648 if (mutex_lock_killable(&volume_mutex) < 0)
6649 return -EINTR;
6651 rc = volume_get_status_ec(&s);
6652 if (rc)
6653 goto unlock;
6655 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6656 s & ~TP_EC_AUDIO_MUTESW_MSK;
6658 if (n != s) {
6659 rc = volume_set_status_ec(n);
6660 if (!rc)
6661 rc = 1;
6664 unlock:
6665 mutex_unlock(&volume_mutex);
6666 return rc;
6669 static int volume_alsa_set_mute(const bool mute)
6671 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6672 (mute) ? "" : "un");
6673 return __volume_set_mute_ec(mute);
6676 static int volume_set_mute(const bool mute)
6678 int rc;
6680 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6681 (mute) ? "" : "un");
6683 rc = __volume_set_mute_ec(mute);
6684 return (rc < 0) ? rc : 0;
6687 /* returns < 0 on error, 0 on no change, 1 on change */
6688 static int __volume_set_volume_ec(const u8 vol)
6690 int rc;
6691 u8 s, n;
6693 if (vol > TP_EC_VOLUME_MAX)
6694 return -EINVAL;
6696 if (mutex_lock_killable(&volume_mutex) < 0)
6697 return -EINTR;
6699 rc = volume_get_status_ec(&s);
6700 if (rc)
6701 goto unlock;
6703 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6705 if (n != s) {
6706 rc = volume_set_status_ec(n);
6707 if (!rc)
6708 rc = 1;
6711 unlock:
6712 mutex_unlock(&volume_mutex);
6713 return rc;
6716 static int volume_alsa_set_volume(const u8 vol)
6718 dbg_printk(TPACPI_DBG_MIXER,
6719 "ALSA: trying to set volume level to %hu\n", vol);
6720 return __volume_set_volume_ec(vol);
6723 static void volume_alsa_notify_change(void)
6725 struct tpacpi_alsa_data *d;
6727 if (alsa_card && alsa_card->private_data) {
6728 d = alsa_card->private_data;
6729 if (d->ctl_mute_id)
6730 snd_ctl_notify(alsa_card,
6731 SNDRV_CTL_EVENT_MASK_VALUE,
6732 d->ctl_mute_id);
6733 if (d->ctl_vol_id)
6734 snd_ctl_notify(alsa_card,
6735 SNDRV_CTL_EVENT_MASK_VALUE,
6736 d->ctl_vol_id);
6740 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6741 struct snd_ctl_elem_info *uinfo)
6743 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6744 uinfo->count = 1;
6745 uinfo->value.integer.min = 0;
6746 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6747 return 0;
6750 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6751 struct snd_ctl_elem_value *ucontrol)
6753 u8 s;
6754 int rc;
6756 rc = volume_get_status(&s);
6757 if (rc < 0)
6758 return rc;
6760 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6761 return 0;
6764 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6765 struct snd_ctl_elem_value *ucontrol)
6767 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
6768 ucontrol->value.integer.value[0]);
6769 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6772 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6774 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6775 struct snd_ctl_elem_value *ucontrol)
6777 u8 s;
6778 int rc;
6780 rc = volume_get_status(&s);
6781 if (rc < 0)
6782 return rc;
6784 ucontrol->value.integer.value[0] =
6785 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6786 return 0;
6789 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6790 struct snd_ctl_elem_value *ucontrol)
6792 tpacpi_disclose_usertask("ALSA", "%smute\n",
6793 ucontrol->value.integer.value[0] ?
6794 "un" : "");
6795 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6798 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6799 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6800 .name = "Console Playback Volume",
6801 .index = 0,
6802 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6803 .info = volume_alsa_vol_info,
6804 .get = volume_alsa_vol_get,
6807 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6808 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6809 .name = "Console Playback Switch",
6810 .index = 0,
6811 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6812 .info = volume_alsa_mute_info,
6813 .get = volume_alsa_mute_get,
6816 static void volume_suspend(pm_message_t state)
6818 tpacpi_volume_checkpoint_nvram();
6821 static void volume_resume(void)
6823 volume_alsa_notify_change();
6826 static void volume_shutdown(void)
6828 tpacpi_volume_checkpoint_nvram();
6831 static void volume_exit(void)
6833 if (alsa_card) {
6834 snd_card_free(alsa_card);
6835 alsa_card = NULL;
6838 tpacpi_volume_checkpoint_nvram();
6841 static int __init volume_create_alsa_mixer(void)
6843 struct snd_card *card;
6844 struct tpacpi_alsa_data *data;
6845 struct snd_kcontrol *ctl_vol;
6846 struct snd_kcontrol *ctl_mute;
6847 int rc;
6849 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6850 sizeof(struct tpacpi_alsa_data), &card);
6851 if (rc < 0 || !card) {
6852 printk(TPACPI_ERR
6853 "Failed to create ALSA card structures: %d\n", rc);
6854 return 1;
6857 BUG_ON(!card->private_data);
6858 data = card->private_data;
6859 data->card = card;
6861 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6862 sizeof(card->driver));
6863 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6864 sizeof(card->shortname));
6865 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6866 (thinkpad_id.ec_version_str) ?
6867 thinkpad_id.ec_version_str : "(unknown)");
6868 snprintf(card->longname, sizeof(card->longname),
6869 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6870 (thinkpad_id.ec_version_str) ?
6871 thinkpad_id.ec_version_str : "unknown");
6873 if (volume_control_allowed) {
6874 volume_alsa_control_vol.put = volume_alsa_vol_put;
6875 volume_alsa_control_vol.access =
6876 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6878 volume_alsa_control_mute.put = volume_alsa_mute_put;
6879 volume_alsa_control_mute.access =
6880 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6883 if (!tp_features.mixer_no_level_control) {
6884 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6885 rc = snd_ctl_add(card, ctl_vol);
6886 if (rc < 0) {
6887 printk(TPACPI_ERR
6888 "Failed to create ALSA volume control: %d\n",
6889 rc);
6890 goto err_exit;
6892 data->ctl_vol_id = &ctl_vol->id;
6895 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6896 rc = snd_ctl_add(card, ctl_mute);
6897 if (rc < 0) {
6898 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6899 rc);
6900 goto err_exit;
6902 data->ctl_mute_id = &ctl_mute->id;
6904 snd_card_set_dev(card, &tpacpi_pdev->dev);
6905 rc = snd_card_register(card);
6906 if (rc < 0) {
6907 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6908 goto err_exit;
6911 alsa_card = card;
6912 return 0;
6914 err_exit:
6915 snd_card_free(card);
6916 return 1;
6919 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6920 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6922 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6923 /* Whitelist volume level on all IBM by default */
6924 { .vendor = PCI_VENDOR_ID_IBM,
6925 .bios = TPACPI_MATCH_ANY,
6926 .ec = TPACPI_MATCH_ANY,
6927 .quirks = TPACPI_VOL_Q_LEVEL },
6929 /* Lenovo models with volume control (needs confirmation) */
6930 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6931 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6932 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6933 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6934 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6935 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6936 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6938 /* Whitelist mute-only on all Lenovo by default */
6939 { .vendor = PCI_VENDOR_ID_LENOVO,
6940 .bios = TPACPI_MATCH_ANY,
6941 .ec = TPACPI_MATCH_ANY,
6942 .quirks = TPACPI_VOL_Q_MUTEONLY }
6945 static int __init volume_init(struct ibm_init_struct *iibm)
6947 unsigned long quirks;
6948 int rc;
6950 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6952 mutex_init(&volume_mutex);
6955 * Check for module parameter bogosity, note that we
6956 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6957 * able to detect "unspecified"
6959 if (volume_mode > TPACPI_VOL_MODE_MAX)
6960 return -EINVAL;
6962 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6963 printk(TPACPI_ERR
6964 "UCMS step volume mode not implemented, "
6965 "please contact %s\n", TPACPI_MAIL);
6966 return 1;
6969 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6970 return -EINVAL;
6973 * The ALSA mixer is our primary interface.
6974 * When disabled, don't install the subdriver at all
6976 if (!alsa_enable) {
6977 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6978 "ALSA mixer disabled by parameter, "
6979 "not loading volume subdriver...\n");
6980 return 1;
6983 quirks = tpacpi_check_quirks(volume_quirk_table,
6984 ARRAY_SIZE(volume_quirk_table));
6986 switch (volume_capabilities) {
6987 case TPACPI_VOL_CAP_AUTO:
6988 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6989 tp_features.mixer_no_level_control = 1;
6990 else if (quirks & TPACPI_VOL_Q_LEVEL)
6991 tp_features.mixer_no_level_control = 0;
6992 else
6993 return 1; /* no mixer */
6994 break;
6995 case TPACPI_VOL_CAP_VOLMUTE:
6996 tp_features.mixer_no_level_control = 0;
6997 break;
6998 case TPACPI_VOL_CAP_MUTEONLY:
6999 tp_features.mixer_no_level_control = 1;
7000 break;
7001 default:
7002 return 1;
7005 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7006 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7007 "using user-supplied volume_capabilities=%d\n",
7008 volume_capabilities);
7010 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7011 volume_mode == TPACPI_VOL_MODE_MAX) {
7012 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7014 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7015 "driver auto-selected volume_mode=%d\n",
7016 volume_mode);
7017 } else {
7018 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7019 "using user-supplied volume_mode=%d\n",
7020 volume_mode);
7023 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7024 "mute is supported, volume control is %s\n",
7025 str_supported(!tp_features.mixer_no_level_control));
7027 rc = volume_create_alsa_mixer();
7028 if (rc) {
7029 printk(TPACPI_ERR
7030 "Could not create the ALSA mixer interface\n");
7031 return rc;
7034 printk(TPACPI_INFO
7035 "Console audio control enabled, mode: %s\n",
7036 (volume_control_allowed) ?
7037 "override (read/write)" :
7038 "monitor (read only)");
7040 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7041 "registering volume hotkeys as change notification\n");
7042 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7043 | TP_ACPI_HKEY_VOLUP_MASK
7044 | TP_ACPI_HKEY_VOLDWN_MASK
7045 | TP_ACPI_HKEY_MUTE_MASK);
7047 return 0;
7050 static int volume_read(struct seq_file *m)
7052 u8 status;
7054 if (volume_get_status(&status) < 0) {
7055 seq_printf(m, "level:\t\tunreadable\n");
7056 } else {
7057 if (tp_features.mixer_no_level_control)
7058 seq_printf(m, "level:\t\tunsupported\n");
7059 else
7060 seq_printf(m, "level:\t\t%d\n",
7061 status & TP_EC_AUDIO_LVL_MSK);
7063 seq_printf(m, "mute:\t\t%s\n",
7064 onoff(status, TP_EC_AUDIO_MUTESW));
7066 if (volume_control_allowed) {
7067 seq_printf(m, "commands:\tunmute, mute\n");
7068 if (!tp_features.mixer_no_level_control) {
7069 seq_printf(m,
7070 "commands:\tup, down\n");
7071 seq_printf(m,
7072 "commands:\tlevel <level>"
7073 " (<level> is 0-%d)\n",
7074 TP_EC_VOLUME_MAX);
7079 return 0;
7082 static int volume_write(char *buf)
7084 u8 s;
7085 u8 new_level, new_mute;
7086 int l;
7087 char *cmd;
7088 int rc;
7091 * We do allow volume control at driver startup, so that the
7092 * user can set initial state through the volume=... parameter hack.
7094 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7095 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7096 tp_warned.volume_ctrl_forbidden = 1;
7097 printk(TPACPI_NOTICE
7098 "Console audio control in monitor mode, "
7099 "changes are not allowed.\n");
7100 printk(TPACPI_NOTICE
7101 "Use the volume_control=1 module parameter "
7102 "to enable volume control\n");
7104 return -EPERM;
7107 rc = volume_get_status(&s);
7108 if (rc < 0)
7109 return rc;
7111 new_level = s & TP_EC_AUDIO_LVL_MSK;
7112 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7114 while ((cmd = next_cmd(&buf))) {
7115 if (!tp_features.mixer_no_level_control) {
7116 if (strlencmp(cmd, "up") == 0) {
7117 if (new_mute)
7118 new_mute = 0;
7119 else if (new_level < TP_EC_VOLUME_MAX)
7120 new_level++;
7121 continue;
7122 } else if (strlencmp(cmd, "down") == 0) {
7123 if (new_mute)
7124 new_mute = 0;
7125 else if (new_level > 0)
7126 new_level--;
7127 continue;
7128 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7129 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7130 new_level = l;
7131 continue;
7134 if (strlencmp(cmd, "mute") == 0)
7135 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7136 else if (strlencmp(cmd, "unmute") == 0)
7137 new_mute = 0;
7138 else
7139 return -EINVAL;
7142 if (tp_features.mixer_no_level_control) {
7143 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7144 new_mute ? "" : "un");
7145 rc = volume_set_mute(!!new_mute);
7146 } else {
7147 tpacpi_disclose_usertask("procfs volume",
7148 "%smute and set level to %d\n",
7149 new_mute ? "" : "un", new_level);
7150 rc = volume_set_status(new_mute | new_level);
7152 volume_alsa_notify_change();
7154 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7157 static struct ibm_struct volume_driver_data = {
7158 .name = "volume",
7159 .read = volume_read,
7160 .write = volume_write,
7161 .exit = volume_exit,
7162 .suspend = volume_suspend,
7163 .resume = volume_resume,
7164 .shutdown = volume_shutdown,
7167 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7169 #define alsa_card NULL
7171 static void inline volume_alsa_notify_change(void)
7175 static int __init volume_init(struct ibm_init_struct *iibm)
7177 printk(TPACPI_INFO
7178 "volume: disabled as there is no ALSA support in this kernel\n");
7180 return 1;
7183 static struct ibm_struct volume_driver_data = {
7184 .name = "volume",
7187 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7189 /*************************************************************************
7190 * Fan subdriver
7194 * FAN ACCESS MODES
7196 * TPACPI_FAN_RD_ACPI_GFAN:
7197 * ACPI GFAN method: returns fan level
7199 * see TPACPI_FAN_WR_ACPI_SFAN
7200 * EC 0x2f (HFSP) not available if GFAN exists
7202 * TPACPI_FAN_WR_ACPI_SFAN:
7203 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7205 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7206 * it for writing.
7208 * TPACPI_FAN_WR_TPEC:
7209 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7210 * Supported on almost all ThinkPads
7212 * Fan speed changes of any sort (including those caused by the
7213 * disengaged mode) are usually done slowly by the firmware as the
7214 * maximum amount of fan duty cycle change per second seems to be
7215 * limited.
7217 * Reading is not available if GFAN exists.
7218 * Writing is not available if SFAN exists.
7220 * Bits
7221 * 7 automatic mode engaged;
7222 * (default operation mode of the ThinkPad)
7223 * fan level is ignored in this mode.
7224 * 6 full speed mode (takes precedence over bit 7);
7225 * not available on all thinkpads. May disable
7226 * the tachometer while the fan controller ramps up
7227 * the speed (which can take up to a few *minutes*).
7228 * Speeds up fan to 100% duty-cycle, which is far above
7229 * the standard RPM levels. It is not impossible that
7230 * it could cause hardware damage.
7231 * 5-3 unused in some models. Extra bits for fan level
7232 * in others, but still useless as all values above
7233 * 7 map to the same speed as level 7 in these models.
7234 * 2-0 fan level (0..7 usually)
7235 * 0x00 = stop
7236 * 0x07 = max (set when temperatures critical)
7237 * Some ThinkPads may have other levels, see
7238 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7240 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7241 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7242 * does so, its initial value is meaningless (0x07).
7244 * For firmware bugs, refer to:
7245 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7247 * ----
7249 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7250 * Main fan tachometer reading (in RPM)
7252 * This register is present on all ThinkPads with a new-style EC, and
7253 * it is known not to be present on the A21m/e, and T22, as there is
7254 * something else in offset 0x84 according to the ACPI DSDT. Other
7255 * ThinkPads from this same time period (and earlier) probably lack the
7256 * tachometer as well.
7258 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7259 * was never fixed by IBM to report the EC firmware version string
7260 * probably support the tachometer (like the early X models), so
7261 * detecting it is quite hard. We need more data to know for sure.
7263 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7264 * might result.
7266 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7267 * mode.
7269 * For firmware bugs, refer to:
7270 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7272 * ----
7274 * ThinkPad EC register 0x31 bit 0 (only on select models)
7276 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7277 * show the speed of the main fan. When bit 0 of EC register 0x31
7278 * is one, the tachometer registers show the speed of the auxiliary
7279 * fan.
7281 * Fan control seems to affect both fans, regardless of the state
7282 * of this bit.
7284 * So far, only the firmware for the X60/X61 non-tablet versions
7285 * seem to support this (firmware TP-7M).
7287 * TPACPI_FAN_WR_ACPI_FANS:
7288 * ThinkPad X31, X40, X41. Not available in the X60.
7290 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7291 * high speed. ACPI DSDT seems to map these three speeds to levels
7292 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7293 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7295 * The speeds are stored on handles
7296 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7298 * There are three default speed sets, accessible as handles:
7299 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7301 * ACPI DSDT switches which set is in use depending on various
7302 * factors.
7304 * TPACPI_FAN_WR_TPEC is also available and should be used to
7305 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7306 * but the ACPI tables just mention level 7.
7309 enum { /* Fan control constants */
7310 fan_status_offset = 0x2f, /* EC register 0x2f */
7311 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7312 * 0x84 must be read before 0x85 */
7313 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7314 bit 0 selects which fan is active */
7316 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7317 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7319 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7322 enum fan_status_access_mode {
7323 TPACPI_FAN_NONE = 0, /* No fan status or control */
7324 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7325 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7328 enum fan_control_access_mode {
7329 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7330 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7331 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7332 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7335 enum fan_control_commands {
7336 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7337 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7338 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7339 * and also watchdog cmd */
7342 static int fan_control_allowed;
7344 static enum fan_status_access_mode fan_status_access_mode;
7345 static enum fan_control_access_mode fan_control_access_mode;
7346 static enum fan_control_commands fan_control_commands;
7348 static u8 fan_control_initial_status;
7349 static u8 fan_control_desired_level;
7350 static u8 fan_control_resume_level;
7351 static int fan_watchdog_maxinterval;
7353 static struct mutex fan_mutex;
7355 static void fan_watchdog_fire(struct work_struct *ignored);
7356 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7358 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7359 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7360 "\\FSPD", /* 600e/x, 770e, 770x */
7361 ); /* all others */
7362 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7363 "JFNS", /* 770x-JL */
7364 ); /* all others */
7367 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7368 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7369 * be in auto mode (0x80).
7371 * This is corrected by any write to HFSP either by the driver, or
7372 * by the firmware.
7374 * We assume 0x07 really means auto mode while this quirk is active,
7375 * as this is far more likely than the ThinkPad being in level 7,
7376 * which is only used by the firmware during thermal emergencies.
7378 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7379 * TP-70 (T43, R52), which are known to be buggy.
7382 static void fan_quirk1_setup(void)
7384 if (fan_control_initial_status == 0x07) {
7385 printk(TPACPI_NOTICE
7386 "fan_init: initial fan status is unknown, "
7387 "assuming it is in auto mode\n");
7388 tp_features.fan_ctrl_status_undef = 1;
7392 static void fan_quirk1_handle(u8 *fan_status)
7394 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7395 if (*fan_status != fan_control_initial_status) {
7396 /* something changed the HFSP regisnter since
7397 * driver init time, so it is not undefined
7398 * anymore */
7399 tp_features.fan_ctrl_status_undef = 0;
7400 } else {
7401 /* Return most likely status. In fact, it
7402 * might be the only possible status */
7403 *fan_status = TP_EC_FAN_AUTO;
7408 /* Select main fan on X60/X61, NOOP on others */
7409 static bool fan_select_fan1(void)
7411 if (tp_features.second_fan) {
7412 u8 val;
7414 if (ec_read(fan_select_offset, &val) < 0)
7415 return false;
7416 val &= 0xFEU;
7417 if (ec_write(fan_select_offset, val) < 0)
7418 return false;
7420 return true;
7423 /* Select secondary fan on X60/X61 */
7424 static bool fan_select_fan2(void)
7426 u8 val;
7428 if (!tp_features.second_fan)
7429 return false;
7431 if (ec_read(fan_select_offset, &val) < 0)
7432 return false;
7433 val |= 0x01U;
7434 if (ec_write(fan_select_offset, val) < 0)
7435 return false;
7437 return true;
7441 * Call with fan_mutex held
7443 static void fan_update_desired_level(u8 status)
7445 if ((status &
7446 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7447 if (status > 7)
7448 fan_control_desired_level = 7;
7449 else
7450 fan_control_desired_level = status;
7454 static int fan_get_status(u8 *status)
7456 u8 s;
7458 /* TODO:
7459 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7461 switch (fan_status_access_mode) {
7462 case TPACPI_FAN_RD_ACPI_GFAN:
7463 /* 570, 600e/x, 770e, 770x */
7465 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7466 return -EIO;
7468 if (likely(status))
7469 *status = s & 0x07;
7471 break;
7473 case TPACPI_FAN_RD_TPEC:
7474 /* all except 570, 600e/x, 770e, 770x */
7475 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7476 return -EIO;
7478 if (likely(status)) {
7479 *status = s;
7480 fan_quirk1_handle(status);
7483 break;
7485 default:
7486 return -ENXIO;
7489 return 0;
7492 static int fan_get_status_safe(u8 *status)
7494 int rc;
7495 u8 s;
7497 if (mutex_lock_killable(&fan_mutex))
7498 return -ERESTARTSYS;
7499 rc = fan_get_status(&s);
7500 if (!rc)
7501 fan_update_desired_level(s);
7502 mutex_unlock(&fan_mutex);
7504 if (status)
7505 *status = s;
7507 return rc;
7510 static int fan_get_speed(unsigned int *speed)
7512 u8 hi, lo;
7514 switch (fan_status_access_mode) {
7515 case TPACPI_FAN_RD_TPEC:
7516 /* all except 570, 600e/x, 770e, 770x */
7517 if (unlikely(!fan_select_fan1()))
7518 return -EIO;
7519 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7520 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7521 return -EIO;
7523 if (likely(speed))
7524 *speed = (hi << 8) | lo;
7526 break;
7528 default:
7529 return -ENXIO;
7532 return 0;
7535 static int fan2_get_speed(unsigned int *speed)
7537 u8 hi, lo;
7538 bool rc;
7540 switch (fan_status_access_mode) {
7541 case TPACPI_FAN_RD_TPEC:
7542 /* all except 570, 600e/x, 770e, 770x */
7543 if (unlikely(!fan_select_fan2()))
7544 return -EIO;
7545 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7546 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7547 fan_select_fan1(); /* play it safe */
7548 if (rc)
7549 return -EIO;
7551 if (likely(speed))
7552 *speed = (hi << 8) | lo;
7554 break;
7556 default:
7557 return -ENXIO;
7560 return 0;
7563 static int fan_set_level(int level)
7565 if (!fan_control_allowed)
7566 return -EPERM;
7568 switch (fan_control_access_mode) {
7569 case TPACPI_FAN_WR_ACPI_SFAN:
7570 if (level >= 0 && level <= 7) {
7571 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7572 return -EIO;
7573 } else
7574 return -EINVAL;
7575 break;
7577 case TPACPI_FAN_WR_ACPI_FANS:
7578 case TPACPI_FAN_WR_TPEC:
7579 if (!(level & TP_EC_FAN_AUTO) &&
7580 !(level & TP_EC_FAN_FULLSPEED) &&
7581 ((level < 0) || (level > 7)))
7582 return -EINVAL;
7584 /* safety net should the EC not support AUTO
7585 * or FULLSPEED mode bits and just ignore them */
7586 if (level & TP_EC_FAN_FULLSPEED)
7587 level |= 7; /* safety min speed 7 */
7588 else if (level & TP_EC_FAN_AUTO)
7589 level |= 4; /* safety min speed 4 */
7591 if (!acpi_ec_write(fan_status_offset, level))
7592 return -EIO;
7593 else
7594 tp_features.fan_ctrl_status_undef = 0;
7595 break;
7597 default:
7598 return -ENXIO;
7601 vdbg_printk(TPACPI_DBG_FAN,
7602 "fan control: set fan control register to 0x%02x\n", level);
7603 return 0;
7606 static int fan_set_level_safe(int level)
7608 int rc;
7610 if (!fan_control_allowed)
7611 return -EPERM;
7613 if (mutex_lock_killable(&fan_mutex))
7614 return -ERESTARTSYS;
7616 if (level == TPACPI_FAN_LAST_LEVEL)
7617 level = fan_control_desired_level;
7619 rc = fan_set_level(level);
7620 if (!rc)
7621 fan_update_desired_level(level);
7623 mutex_unlock(&fan_mutex);
7624 return rc;
7627 static int fan_set_enable(void)
7629 u8 s;
7630 int rc;
7632 if (!fan_control_allowed)
7633 return -EPERM;
7635 if (mutex_lock_killable(&fan_mutex))
7636 return -ERESTARTSYS;
7638 switch (fan_control_access_mode) {
7639 case TPACPI_FAN_WR_ACPI_FANS:
7640 case TPACPI_FAN_WR_TPEC:
7641 rc = fan_get_status(&s);
7642 if (rc < 0)
7643 break;
7645 /* Don't go out of emergency fan mode */
7646 if (s != 7) {
7647 s &= 0x07;
7648 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7651 if (!acpi_ec_write(fan_status_offset, s))
7652 rc = -EIO;
7653 else {
7654 tp_features.fan_ctrl_status_undef = 0;
7655 rc = 0;
7657 break;
7659 case TPACPI_FAN_WR_ACPI_SFAN:
7660 rc = fan_get_status(&s);
7661 if (rc < 0)
7662 break;
7664 s &= 0x07;
7666 /* Set fan to at least level 4 */
7667 s |= 4;
7669 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7670 rc = -EIO;
7671 else
7672 rc = 0;
7673 break;
7675 default:
7676 rc = -ENXIO;
7679 mutex_unlock(&fan_mutex);
7681 if (!rc)
7682 vdbg_printk(TPACPI_DBG_FAN,
7683 "fan control: set fan control register to 0x%02x\n",
7685 return rc;
7688 static int fan_set_disable(void)
7690 int rc;
7692 if (!fan_control_allowed)
7693 return -EPERM;
7695 if (mutex_lock_killable(&fan_mutex))
7696 return -ERESTARTSYS;
7698 rc = 0;
7699 switch (fan_control_access_mode) {
7700 case TPACPI_FAN_WR_ACPI_FANS:
7701 case TPACPI_FAN_WR_TPEC:
7702 if (!acpi_ec_write(fan_status_offset, 0x00))
7703 rc = -EIO;
7704 else {
7705 fan_control_desired_level = 0;
7706 tp_features.fan_ctrl_status_undef = 0;
7708 break;
7710 case TPACPI_FAN_WR_ACPI_SFAN:
7711 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7712 rc = -EIO;
7713 else
7714 fan_control_desired_level = 0;
7715 break;
7717 default:
7718 rc = -ENXIO;
7721 if (!rc)
7722 vdbg_printk(TPACPI_DBG_FAN,
7723 "fan control: set fan control register to 0\n");
7725 mutex_unlock(&fan_mutex);
7726 return rc;
7729 static int fan_set_speed(int speed)
7731 int rc;
7733 if (!fan_control_allowed)
7734 return -EPERM;
7736 if (mutex_lock_killable(&fan_mutex))
7737 return -ERESTARTSYS;
7739 rc = 0;
7740 switch (fan_control_access_mode) {
7741 case TPACPI_FAN_WR_ACPI_FANS:
7742 if (speed >= 0 && speed <= 65535) {
7743 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7744 speed, speed, speed))
7745 rc = -EIO;
7746 } else
7747 rc = -EINVAL;
7748 break;
7750 default:
7751 rc = -ENXIO;
7754 mutex_unlock(&fan_mutex);
7755 return rc;
7758 static void fan_watchdog_reset(void)
7760 static int fan_watchdog_active;
7762 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7763 return;
7765 if (fan_watchdog_active)
7766 cancel_delayed_work(&fan_watchdog_task);
7768 if (fan_watchdog_maxinterval > 0 &&
7769 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7770 fan_watchdog_active = 1;
7771 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7772 msecs_to_jiffies(fan_watchdog_maxinterval
7773 * 1000))) {
7774 printk(TPACPI_ERR
7775 "failed to queue the fan watchdog, "
7776 "watchdog will not trigger\n");
7778 } else
7779 fan_watchdog_active = 0;
7782 static void fan_watchdog_fire(struct work_struct *ignored)
7784 int rc;
7786 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7787 return;
7789 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7790 rc = fan_set_enable();
7791 if (rc < 0) {
7792 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7793 "will try again later...\n", -rc);
7794 /* reschedule for later */
7795 fan_watchdog_reset();
7800 * SYSFS fan layout: hwmon compatible (device)
7802 * pwm*_enable:
7803 * 0: "disengaged" mode
7804 * 1: manual mode
7805 * 2: native EC "auto" mode (recommended, hardware default)
7807 * pwm*: set speed in manual mode, ignored otherwise.
7808 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7809 * interpolation.
7811 * fan*_input: tachometer reading, RPM
7814 * SYSFS fan layout: extensions
7816 * fan_watchdog (driver):
7817 * fan watchdog interval in seconds, 0 disables (default), max 120
7820 /* sysfs fan pwm1_enable ----------------------------------------------- */
7821 static ssize_t fan_pwm1_enable_show(struct device *dev,
7822 struct device_attribute *attr,
7823 char *buf)
7825 int res, mode;
7826 u8 status;
7828 res = fan_get_status_safe(&status);
7829 if (res)
7830 return res;
7832 if (status & TP_EC_FAN_FULLSPEED) {
7833 mode = 0;
7834 } else if (status & TP_EC_FAN_AUTO) {
7835 mode = 2;
7836 } else
7837 mode = 1;
7839 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7842 static ssize_t fan_pwm1_enable_store(struct device *dev,
7843 struct device_attribute *attr,
7844 const char *buf, size_t count)
7846 unsigned long t;
7847 int res, level;
7849 if (parse_strtoul(buf, 2, &t))
7850 return -EINVAL;
7852 tpacpi_disclose_usertask("hwmon pwm1_enable",
7853 "set fan mode to %lu\n", t);
7855 switch (t) {
7856 case 0:
7857 level = TP_EC_FAN_FULLSPEED;
7858 break;
7859 case 1:
7860 level = TPACPI_FAN_LAST_LEVEL;
7861 break;
7862 case 2:
7863 level = TP_EC_FAN_AUTO;
7864 break;
7865 case 3:
7866 /* reserved for software-controlled auto mode */
7867 return -ENOSYS;
7868 default:
7869 return -EINVAL;
7872 res = fan_set_level_safe(level);
7873 if (res == -ENXIO)
7874 return -EINVAL;
7875 else if (res < 0)
7876 return res;
7878 fan_watchdog_reset();
7880 return count;
7883 static struct device_attribute dev_attr_fan_pwm1_enable =
7884 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7885 fan_pwm1_enable_show, fan_pwm1_enable_store);
7887 /* sysfs fan pwm1 ------------------------------------------------------ */
7888 static ssize_t fan_pwm1_show(struct device *dev,
7889 struct device_attribute *attr,
7890 char *buf)
7892 int res;
7893 u8 status;
7895 res = fan_get_status_safe(&status);
7896 if (res)
7897 return res;
7899 if ((status &
7900 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7901 status = fan_control_desired_level;
7903 if (status > 7)
7904 status = 7;
7906 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7909 static ssize_t fan_pwm1_store(struct device *dev,
7910 struct device_attribute *attr,
7911 const char *buf, size_t count)
7913 unsigned long s;
7914 int rc;
7915 u8 status, newlevel;
7917 if (parse_strtoul(buf, 255, &s))
7918 return -EINVAL;
7920 tpacpi_disclose_usertask("hwmon pwm1",
7921 "set fan speed to %lu\n", s);
7923 /* scale down from 0-255 to 0-7 */
7924 newlevel = (s >> 5) & 0x07;
7926 if (mutex_lock_killable(&fan_mutex))
7927 return -ERESTARTSYS;
7929 rc = fan_get_status(&status);
7930 if (!rc && (status &
7931 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7932 rc = fan_set_level(newlevel);
7933 if (rc == -ENXIO)
7934 rc = -EINVAL;
7935 else if (!rc) {
7936 fan_update_desired_level(newlevel);
7937 fan_watchdog_reset();
7941 mutex_unlock(&fan_mutex);
7942 return (rc)? rc : count;
7945 static struct device_attribute dev_attr_fan_pwm1 =
7946 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7947 fan_pwm1_show, fan_pwm1_store);
7949 /* sysfs fan fan1_input ------------------------------------------------ */
7950 static ssize_t fan_fan1_input_show(struct device *dev,
7951 struct device_attribute *attr,
7952 char *buf)
7954 int res;
7955 unsigned int speed;
7957 res = fan_get_speed(&speed);
7958 if (res < 0)
7959 return res;
7961 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7964 static struct device_attribute dev_attr_fan_fan1_input =
7965 __ATTR(fan1_input, S_IRUGO,
7966 fan_fan1_input_show, NULL);
7968 /* sysfs fan fan2_input ------------------------------------------------ */
7969 static ssize_t fan_fan2_input_show(struct device *dev,
7970 struct device_attribute *attr,
7971 char *buf)
7973 int res;
7974 unsigned int speed;
7976 res = fan2_get_speed(&speed);
7977 if (res < 0)
7978 return res;
7980 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7983 static struct device_attribute dev_attr_fan_fan2_input =
7984 __ATTR(fan2_input, S_IRUGO,
7985 fan_fan2_input_show, NULL);
7987 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7988 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7989 char *buf)
7991 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7994 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7995 const char *buf, size_t count)
7997 unsigned long t;
7999 if (parse_strtoul(buf, 120, &t))
8000 return -EINVAL;
8002 if (!fan_control_allowed)
8003 return -EPERM;
8005 fan_watchdog_maxinterval = t;
8006 fan_watchdog_reset();
8008 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8010 return count;
8013 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8014 fan_fan_watchdog_show, fan_fan_watchdog_store);
8016 /* --------------------------------------------------------------------- */
8017 static struct attribute *fan_attributes[] = {
8018 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
8019 &dev_attr_fan_fan1_input.attr,
8020 NULL, /* for fan2_input */
8021 NULL
8024 static const struct attribute_group fan_attr_group = {
8025 .attrs = fan_attributes,
8028 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8029 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8031 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8032 { .vendor = PCI_VENDOR_ID_IBM, \
8033 .bios = TPACPI_MATCH_ANY, \
8034 .ec = TPID(__id1, __id2), \
8035 .quirks = __quirks }
8037 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8038 { .vendor = PCI_VENDOR_ID_LENOVO, \
8039 .bios = TPACPI_MATCH_ANY, \
8040 .ec = TPID(__id1, __id2), \
8041 .quirks = __quirks }
8043 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8044 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8045 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8046 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8047 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8048 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8051 #undef TPACPI_FAN_QL
8052 #undef TPACPI_FAN_QI
8054 static int __init fan_init(struct ibm_init_struct *iibm)
8056 int rc;
8057 unsigned long quirks;
8059 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8060 "initializing fan subdriver\n");
8062 mutex_init(&fan_mutex);
8063 fan_status_access_mode = TPACPI_FAN_NONE;
8064 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8065 fan_control_commands = 0;
8066 fan_watchdog_maxinterval = 0;
8067 tp_features.fan_ctrl_status_undef = 0;
8068 tp_features.second_fan = 0;
8069 fan_control_desired_level = 7;
8071 TPACPI_ACPIHANDLE_INIT(fans);
8072 TPACPI_ACPIHANDLE_INIT(gfan);
8073 TPACPI_ACPIHANDLE_INIT(sfan);
8075 quirks = tpacpi_check_quirks(fan_quirk_table,
8076 ARRAY_SIZE(fan_quirk_table));
8078 if (gfan_handle) {
8079 /* 570, 600e/x, 770e, 770x */
8080 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8081 } else {
8082 /* all other ThinkPads: note that even old-style
8083 * ThinkPad ECs supports the fan control register */
8084 if (likely(acpi_ec_read(fan_status_offset,
8085 &fan_control_initial_status))) {
8086 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8087 if (quirks & TPACPI_FAN_Q1)
8088 fan_quirk1_setup();
8089 if (quirks & TPACPI_FAN_2FAN) {
8090 tp_features.second_fan = 1;
8091 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8092 "secondary fan support enabled\n");
8094 } else {
8095 printk(TPACPI_ERR
8096 "ThinkPad ACPI EC access misbehaving, "
8097 "fan status and control unavailable\n");
8098 return 1;
8102 if (sfan_handle) {
8103 /* 570, 770x-JL */
8104 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8105 fan_control_commands |=
8106 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8107 } else {
8108 if (!gfan_handle) {
8109 /* gfan without sfan means no fan control */
8110 /* all other models implement TP EC 0x2f control */
8112 if (fans_handle) {
8113 /* X31, X40, X41 */
8114 fan_control_access_mode =
8115 TPACPI_FAN_WR_ACPI_FANS;
8116 fan_control_commands |=
8117 TPACPI_FAN_CMD_SPEED |
8118 TPACPI_FAN_CMD_LEVEL |
8119 TPACPI_FAN_CMD_ENABLE;
8120 } else {
8121 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8122 fan_control_commands |=
8123 TPACPI_FAN_CMD_LEVEL |
8124 TPACPI_FAN_CMD_ENABLE;
8129 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8130 "fan is %s, modes %d, %d\n",
8131 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8132 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8133 fan_status_access_mode, fan_control_access_mode);
8135 /* fan control master switch */
8136 if (!fan_control_allowed) {
8137 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8138 fan_control_commands = 0;
8139 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8140 "fan control features disabled by parameter\n");
8143 /* update fan_control_desired_level */
8144 if (fan_status_access_mode != TPACPI_FAN_NONE)
8145 fan_get_status_safe(NULL);
8147 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8148 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8149 if (tp_features.second_fan) {
8150 /* attach second fan tachometer */
8151 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8152 &dev_attr_fan_fan2_input.attr;
8154 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8155 &fan_attr_group);
8156 if (rc < 0)
8157 return rc;
8159 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8160 &driver_attr_fan_watchdog);
8161 if (rc < 0) {
8162 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8163 &fan_attr_group);
8164 return rc;
8166 return 0;
8167 } else
8168 return 1;
8171 static void fan_exit(void)
8173 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8174 "cancelling any pending fan watchdog tasks\n");
8176 /* FIXME: can we really do this unconditionally? */
8177 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8178 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8179 &driver_attr_fan_watchdog);
8181 cancel_delayed_work(&fan_watchdog_task);
8182 flush_workqueue(tpacpi_wq);
8185 static void fan_suspend(pm_message_t state)
8187 int rc;
8189 if (!fan_control_allowed)
8190 return;
8192 /* Store fan status in cache */
8193 fan_control_resume_level = 0;
8194 rc = fan_get_status_safe(&fan_control_resume_level);
8195 if (rc < 0)
8196 printk(TPACPI_NOTICE
8197 "failed to read fan level for later "
8198 "restore during resume: %d\n", rc);
8200 /* if it is undefined, don't attempt to restore it.
8201 * KEEP THIS LAST */
8202 if (tp_features.fan_ctrl_status_undef)
8203 fan_control_resume_level = 0;
8206 static void fan_resume(void)
8208 u8 current_level = 7;
8209 bool do_set = false;
8210 int rc;
8212 /* DSDT *always* updates status on resume */
8213 tp_features.fan_ctrl_status_undef = 0;
8215 if (!fan_control_allowed ||
8216 !fan_control_resume_level ||
8217 (fan_get_status_safe(&current_level) < 0))
8218 return;
8220 switch (fan_control_access_mode) {
8221 case TPACPI_FAN_WR_ACPI_SFAN:
8222 /* never decrease fan level */
8223 do_set = (fan_control_resume_level > current_level);
8224 break;
8225 case TPACPI_FAN_WR_ACPI_FANS:
8226 case TPACPI_FAN_WR_TPEC:
8227 /* never decrease fan level, scale is:
8228 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8230 * We expect the firmware to set either 7 or AUTO, but we
8231 * handle FULLSPEED out of paranoia.
8233 * So, we can safely only restore FULLSPEED or 7, anything
8234 * else could slow the fan. Restoring AUTO is useless, at
8235 * best that's exactly what the DSDT already set (it is the
8236 * slower it uses).
8238 * Always keep in mind that the DSDT *will* have set the
8239 * fans to what the vendor supposes is the best level. We
8240 * muck with it only to speed the fan up.
8242 if (fan_control_resume_level != 7 &&
8243 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8244 return;
8245 else
8246 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8247 (current_level != fan_control_resume_level);
8248 break;
8249 default:
8250 return;
8252 if (do_set) {
8253 printk(TPACPI_NOTICE
8254 "restoring fan level to 0x%02x\n",
8255 fan_control_resume_level);
8256 rc = fan_set_level_safe(fan_control_resume_level);
8257 if (rc < 0)
8258 printk(TPACPI_NOTICE
8259 "failed to restore fan level: %d\n", rc);
8263 static int fan_read(struct seq_file *m)
8265 int rc;
8266 u8 status;
8267 unsigned int speed = 0;
8269 switch (fan_status_access_mode) {
8270 case TPACPI_FAN_RD_ACPI_GFAN:
8271 /* 570, 600e/x, 770e, 770x */
8272 rc = fan_get_status_safe(&status);
8273 if (rc < 0)
8274 return rc;
8276 seq_printf(m, "status:\t\t%s\n"
8277 "level:\t\t%d\n",
8278 (status != 0) ? "enabled" : "disabled", status);
8279 break;
8281 case TPACPI_FAN_RD_TPEC:
8282 /* all except 570, 600e/x, 770e, 770x */
8283 rc = fan_get_status_safe(&status);
8284 if (rc < 0)
8285 return rc;
8287 seq_printf(m, "status:\t\t%s\n",
8288 (status != 0) ? "enabled" : "disabled");
8290 rc = fan_get_speed(&speed);
8291 if (rc < 0)
8292 return rc;
8294 seq_printf(m, "speed:\t\t%d\n", speed);
8296 if (status & TP_EC_FAN_FULLSPEED)
8297 /* Disengaged mode takes precedence */
8298 seq_printf(m, "level:\t\tdisengaged\n");
8299 else if (status & TP_EC_FAN_AUTO)
8300 seq_printf(m, "level:\t\tauto\n");
8301 else
8302 seq_printf(m, "level:\t\t%d\n", status);
8303 break;
8305 case TPACPI_FAN_NONE:
8306 default:
8307 seq_printf(m, "status:\t\tnot supported\n");
8310 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8311 seq_printf(m, "commands:\tlevel <level>");
8313 switch (fan_control_access_mode) {
8314 case TPACPI_FAN_WR_ACPI_SFAN:
8315 seq_printf(m, " (<level> is 0-7)\n");
8316 break;
8318 default:
8319 seq_printf(m, " (<level> is 0-7, "
8320 "auto, disengaged, full-speed)\n");
8321 break;
8325 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8326 seq_printf(m, "commands:\tenable, disable\n"
8327 "commands:\twatchdog <timeout> (<timeout> "
8328 "is 0 (off), 1-120 (seconds))\n");
8330 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8331 seq_printf(m, "commands:\tspeed <speed>"
8332 " (<speed> is 0-65535)\n");
8334 return 0;
8337 static int fan_write_cmd_level(const char *cmd, int *rc)
8339 int level;
8341 if (strlencmp(cmd, "level auto") == 0)
8342 level = TP_EC_FAN_AUTO;
8343 else if ((strlencmp(cmd, "level disengaged") == 0) |
8344 (strlencmp(cmd, "level full-speed") == 0))
8345 level = TP_EC_FAN_FULLSPEED;
8346 else if (sscanf(cmd, "level %d", &level) != 1)
8347 return 0;
8349 *rc = fan_set_level_safe(level);
8350 if (*rc == -ENXIO)
8351 printk(TPACPI_ERR "level command accepted for unsupported "
8352 "access mode %d", fan_control_access_mode);
8353 else if (!*rc)
8354 tpacpi_disclose_usertask("procfs fan",
8355 "set level to %d\n", level);
8357 return 1;
8360 static int fan_write_cmd_enable(const char *cmd, int *rc)
8362 if (strlencmp(cmd, "enable") != 0)
8363 return 0;
8365 *rc = fan_set_enable();
8366 if (*rc == -ENXIO)
8367 printk(TPACPI_ERR "enable command accepted for unsupported "
8368 "access mode %d", fan_control_access_mode);
8369 else if (!*rc)
8370 tpacpi_disclose_usertask("procfs fan", "enable\n");
8372 return 1;
8375 static int fan_write_cmd_disable(const char *cmd, int *rc)
8377 if (strlencmp(cmd, "disable") != 0)
8378 return 0;
8380 *rc = fan_set_disable();
8381 if (*rc == -ENXIO)
8382 printk(TPACPI_ERR "disable command accepted for unsupported "
8383 "access mode %d", fan_control_access_mode);
8384 else if (!*rc)
8385 tpacpi_disclose_usertask("procfs fan", "disable\n");
8387 return 1;
8390 static int fan_write_cmd_speed(const char *cmd, int *rc)
8392 int speed;
8394 /* TODO:
8395 * Support speed <low> <medium> <high> ? */
8397 if (sscanf(cmd, "speed %d", &speed) != 1)
8398 return 0;
8400 *rc = fan_set_speed(speed);
8401 if (*rc == -ENXIO)
8402 printk(TPACPI_ERR "speed command accepted for unsupported "
8403 "access mode %d", fan_control_access_mode);
8404 else if (!*rc)
8405 tpacpi_disclose_usertask("procfs fan",
8406 "set speed to %d\n", speed);
8408 return 1;
8411 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8413 int interval;
8415 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8416 return 0;
8418 if (interval < 0 || interval > 120)
8419 *rc = -EINVAL;
8420 else {
8421 fan_watchdog_maxinterval = interval;
8422 tpacpi_disclose_usertask("procfs fan",
8423 "set watchdog timer to %d\n",
8424 interval);
8427 return 1;
8430 static int fan_write(char *buf)
8432 char *cmd;
8433 int rc = 0;
8435 while (!rc && (cmd = next_cmd(&buf))) {
8436 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8437 fan_write_cmd_level(cmd, &rc)) &&
8438 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8439 (fan_write_cmd_enable(cmd, &rc) ||
8440 fan_write_cmd_disable(cmd, &rc) ||
8441 fan_write_cmd_watchdog(cmd, &rc))) &&
8442 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8443 fan_write_cmd_speed(cmd, &rc))
8445 rc = -EINVAL;
8446 else if (!rc)
8447 fan_watchdog_reset();
8450 return rc;
8453 static struct ibm_struct fan_driver_data = {
8454 .name = "fan",
8455 .read = fan_read,
8456 .write = fan_write,
8457 .exit = fan_exit,
8458 .suspend = fan_suspend,
8459 .resume = fan_resume,
8462 /****************************************************************************
8463 ****************************************************************************
8465 * Infrastructure
8467 ****************************************************************************
8468 ****************************************************************************/
8471 * HKEY event callout for other subdrivers go here
8472 * (yes, it is ugly, but it is quick, safe, and gets the job done
8474 static void tpacpi_driver_event(const unsigned int hkey_event)
8476 if (ibm_backlight_device) {
8477 switch (hkey_event) {
8478 case TP_HKEY_EV_BRGHT_UP:
8479 case TP_HKEY_EV_BRGHT_DOWN:
8480 tpacpi_brightness_notify_change();
8483 if (alsa_card) {
8484 switch (hkey_event) {
8485 case TP_HKEY_EV_VOL_UP:
8486 case TP_HKEY_EV_VOL_DOWN:
8487 case TP_HKEY_EV_VOL_MUTE:
8488 volume_alsa_notify_change();
8493 static void hotkey_driver_event(const unsigned int scancode)
8495 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8498 /* sysfs name ---------------------------------------------------------- */
8499 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8500 struct device_attribute *attr,
8501 char *buf)
8503 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8506 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8507 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8509 /* --------------------------------------------------------------------- */
8511 /* /proc support */
8512 static struct proc_dir_entry *proc_dir;
8515 * Module and infrastructure proble, init and exit handling
8518 static int force_load;
8520 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8521 static const char * __init str_supported(int is_supported)
8523 static char text_unsupported[] __initdata = "not supported";
8525 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8527 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8529 static void ibm_exit(struct ibm_struct *ibm)
8531 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8533 list_del_init(&ibm->all_drivers);
8535 if (ibm->flags.acpi_notify_installed) {
8536 dbg_printk(TPACPI_DBG_EXIT,
8537 "%s: acpi_remove_notify_handler\n", ibm->name);
8538 BUG_ON(!ibm->acpi);
8539 acpi_remove_notify_handler(*ibm->acpi->handle,
8540 ibm->acpi->type,
8541 dispatch_acpi_notify);
8542 ibm->flags.acpi_notify_installed = 0;
8543 ibm->flags.acpi_notify_installed = 0;
8546 if (ibm->flags.proc_created) {
8547 dbg_printk(TPACPI_DBG_EXIT,
8548 "%s: remove_proc_entry\n", ibm->name);
8549 remove_proc_entry(ibm->name, proc_dir);
8550 ibm->flags.proc_created = 0;
8553 if (ibm->flags.acpi_driver_registered) {
8554 dbg_printk(TPACPI_DBG_EXIT,
8555 "%s: acpi_bus_unregister_driver\n", ibm->name);
8556 BUG_ON(!ibm->acpi);
8557 acpi_bus_unregister_driver(ibm->acpi->driver);
8558 kfree(ibm->acpi->driver);
8559 ibm->acpi->driver = NULL;
8560 ibm->flags.acpi_driver_registered = 0;
8563 if (ibm->flags.init_called && ibm->exit) {
8564 ibm->exit();
8565 ibm->flags.init_called = 0;
8568 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8571 static int __init ibm_init(struct ibm_init_struct *iibm)
8573 int ret;
8574 struct ibm_struct *ibm = iibm->data;
8575 struct proc_dir_entry *entry;
8577 BUG_ON(ibm == NULL);
8579 INIT_LIST_HEAD(&ibm->all_drivers);
8581 if (ibm->flags.experimental && !experimental)
8582 return 0;
8584 dbg_printk(TPACPI_DBG_INIT,
8585 "probing for %s\n", ibm->name);
8587 if (iibm->init) {
8588 ret = iibm->init(iibm);
8589 if (ret > 0)
8590 return 0; /* probe failed */
8591 if (ret)
8592 return ret;
8594 ibm->flags.init_called = 1;
8597 if (ibm->acpi) {
8598 if (ibm->acpi->hid) {
8599 ret = register_tpacpi_subdriver(ibm);
8600 if (ret)
8601 goto err_out;
8604 if (ibm->acpi->notify) {
8605 ret = setup_acpi_notify(ibm);
8606 if (ret == -ENODEV) {
8607 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8608 ibm->name);
8609 ret = 0;
8610 goto err_out;
8612 if (ret < 0)
8613 goto err_out;
8617 dbg_printk(TPACPI_DBG_INIT,
8618 "%s installed\n", ibm->name);
8620 if (ibm->read) {
8621 mode_t mode = iibm->base_procfs_mode;
8623 if (!mode)
8624 mode = S_IRUGO;
8625 if (ibm->write)
8626 mode |= S_IWUSR;
8627 entry = proc_create_data(ibm->name, mode, proc_dir,
8628 &dispatch_proc_fops, ibm);
8629 if (!entry) {
8630 printk(TPACPI_ERR "unable to create proc entry %s\n",
8631 ibm->name);
8632 ret = -ENODEV;
8633 goto err_out;
8635 ibm->flags.proc_created = 1;
8638 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8640 return 0;
8642 err_out:
8643 dbg_printk(TPACPI_DBG_INIT,
8644 "%s: at error exit path with result %d\n",
8645 ibm->name, ret);
8647 ibm_exit(ibm);
8648 return (ret < 0)? ret : 0;
8651 /* Probing */
8653 static bool __pure __init tpacpi_is_fw_digit(const char c)
8655 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8658 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8659 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8660 const char t)
8662 return s && strlen(s) >= 8 &&
8663 tpacpi_is_fw_digit(s[0]) &&
8664 tpacpi_is_fw_digit(s[1]) &&
8665 s[2] == t && s[3] == 'T' &&
8666 tpacpi_is_fw_digit(s[4]) &&
8667 tpacpi_is_fw_digit(s[5]) &&
8668 s[6] == 'W' && s[7] == 'W';
8671 /* returns 0 - probe ok, or < 0 - probe error.
8672 * Probe ok doesn't mean thinkpad found.
8673 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8674 static int __must_check __init get_thinkpad_model_data(
8675 struct thinkpad_id_data *tp)
8677 const struct dmi_device *dev = NULL;
8678 char ec_fw_string[18];
8679 char const *s;
8681 if (!tp)
8682 return -EINVAL;
8684 memset(tp, 0, sizeof(*tp));
8686 if (dmi_name_in_vendors("IBM"))
8687 tp->vendor = PCI_VENDOR_ID_IBM;
8688 else if (dmi_name_in_vendors("LENOVO"))
8689 tp->vendor = PCI_VENDOR_ID_LENOVO;
8690 else
8691 return 0;
8693 s = dmi_get_system_info(DMI_BIOS_VERSION);
8694 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8695 if (s && !tp->bios_version_str)
8696 return -ENOMEM;
8698 /* Really ancient ThinkPad 240X will fail this, which is fine */
8699 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8700 return 0;
8702 tp->bios_model = tp->bios_version_str[0]
8703 | (tp->bios_version_str[1] << 8);
8704 tp->bios_release = (tp->bios_version_str[4] << 8)
8705 | tp->bios_version_str[5];
8708 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8709 * X32 or newer, all Z series; Some models must have an
8710 * up-to-date BIOS or they will not be detected.
8712 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8714 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8715 if (sscanf(dev->name,
8716 "IBM ThinkPad Embedded Controller -[%17c",
8717 ec_fw_string) == 1) {
8718 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8719 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8721 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8722 if (!tp->ec_version_str)
8723 return -ENOMEM;
8725 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8726 tp->ec_model = ec_fw_string[0]
8727 | (ec_fw_string[1] << 8);
8728 tp->ec_release = (ec_fw_string[4] << 8)
8729 | ec_fw_string[5];
8730 } else {
8731 printk(TPACPI_NOTICE
8732 "ThinkPad firmware release %s "
8733 "doesn't match the known patterns\n",
8734 ec_fw_string);
8735 printk(TPACPI_NOTICE
8736 "please report this to %s\n",
8737 TPACPI_MAIL);
8739 break;
8743 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8744 if (s && !strnicmp(s, "ThinkPad", 8)) {
8745 tp->model_str = kstrdup(s, GFP_KERNEL);
8746 if (!tp->model_str)
8747 return -ENOMEM;
8750 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8751 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8752 if (s && !tp->nummodel_str)
8753 return -ENOMEM;
8755 return 0;
8758 static int __init probe_for_thinkpad(void)
8760 int is_thinkpad;
8762 if (acpi_disabled)
8763 return -ENODEV;
8766 * Non-ancient models have better DMI tagging, but very old models
8767 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8769 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8770 (thinkpad_id.ec_model != 0) ||
8771 tpacpi_is_fw_known();
8773 /* The EC handler is required */
8774 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
8775 if (!ec_handle) {
8776 if (is_thinkpad)
8777 printk(TPACPI_ERR
8778 "Not yet supported ThinkPad detected!\n");
8779 return -ENODEV;
8782 if (!is_thinkpad && !force_load)
8783 return -ENODEV;
8785 return 0;
8788 static void __init thinkpad_acpi_init_banner(void)
8790 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
8791 printk(TPACPI_INFO "%s\n", TPACPI_URL);
8793 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
8794 (thinkpad_id.bios_version_str) ?
8795 thinkpad_id.bios_version_str : "unknown",
8796 (thinkpad_id.ec_version_str) ?
8797 thinkpad_id.ec_version_str : "unknown");
8799 if (thinkpad_id.vendor && thinkpad_id.model_str)
8800 printk(TPACPI_INFO "%s %s, model %s\n",
8801 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
8802 "IBM" : ((thinkpad_id.vendor ==
8803 PCI_VENDOR_ID_LENOVO) ?
8804 "Lenovo" : "Unknown vendor"),
8805 thinkpad_id.model_str,
8806 (thinkpad_id.nummodel_str) ?
8807 thinkpad_id.nummodel_str : "unknown");
8810 /* Module init, exit, parameters */
8812 static struct ibm_init_struct ibms_init[] __initdata = {
8814 .data = &thinkpad_acpi_driver_data,
8817 .init = hotkey_init,
8818 .data = &hotkey_driver_data,
8821 .init = bluetooth_init,
8822 .data = &bluetooth_driver_data,
8825 .init = wan_init,
8826 .data = &wan_driver_data,
8829 .init = uwb_init,
8830 .data = &uwb_driver_data,
8832 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8834 .init = video_init,
8835 .base_procfs_mode = S_IRUSR,
8836 .data = &video_driver_data,
8838 #endif
8840 .init = light_init,
8841 .data = &light_driver_data,
8844 .init = cmos_init,
8845 .data = &cmos_driver_data,
8848 .init = led_init,
8849 .data = &led_driver_data,
8852 .init = beep_init,
8853 .data = &beep_driver_data,
8856 .init = thermal_init,
8857 .data = &thermal_driver_data,
8860 .data = &ecdump_driver_data,
8863 .init = brightness_init,
8864 .data = &brightness_driver_data,
8867 .init = volume_init,
8868 .data = &volume_driver_data,
8871 .init = fan_init,
8872 .data = &fan_driver_data,
8876 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8878 unsigned int i;
8879 struct ibm_struct *ibm;
8881 if (!kp || !kp->name || !val)
8882 return -EINVAL;
8884 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8885 ibm = ibms_init[i].data;
8886 WARN_ON(ibm == NULL);
8888 if (!ibm || !ibm->name)
8889 continue;
8891 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8892 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8893 return -ENOSPC;
8894 strcpy(ibms_init[i].param, val);
8895 strcat(ibms_init[i].param, ",");
8896 return 0;
8900 return -EINVAL;
8903 module_param(experimental, int, 0444);
8904 MODULE_PARM_DESC(experimental,
8905 "Enables experimental features when non-zero");
8907 module_param_named(debug, dbg_level, uint, 0);
8908 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8910 module_param(force_load, bool, 0444);
8911 MODULE_PARM_DESC(force_load,
8912 "Attempts to load the driver even on a "
8913 "mis-identified ThinkPad when true");
8915 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8916 MODULE_PARM_DESC(fan_control,
8917 "Enables setting fan parameters features when true");
8919 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8920 MODULE_PARM_DESC(brightness_mode,
8921 "Selects brightness control strategy: "
8922 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8924 module_param(brightness_enable, uint, 0444);
8925 MODULE_PARM_DESC(brightness_enable,
8926 "Enables backlight control when 1, disables when 0");
8928 module_param(hotkey_report_mode, uint, 0444);
8929 MODULE_PARM_DESC(hotkey_report_mode,
8930 "used for backwards compatibility with userspace, "
8931 "see documentation");
8933 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8934 module_param_named(volume_mode, volume_mode, uint, 0444);
8935 MODULE_PARM_DESC(volume_mode,
8936 "Selects volume control strategy: "
8937 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8939 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8940 MODULE_PARM_DESC(volume_capabilities,
8941 "Selects the mixer capabilites: "
8942 "0=auto, 1=volume and mute, 2=mute only");
8944 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8945 MODULE_PARM_DESC(volume_control,
8946 "Enables software override for the console audio "
8947 "control when true");
8949 /* ALSA module API parameters */
8950 module_param_named(index, alsa_index, int, 0444);
8951 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8952 module_param_named(id, alsa_id, charp, 0444);
8953 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8954 module_param_named(enable, alsa_enable, bool, 0444);
8955 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8956 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8958 #define TPACPI_PARAM(feature) \
8959 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8960 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8961 "at module load, see documentation")
8963 TPACPI_PARAM(hotkey);
8964 TPACPI_PARAM(bluetooth);
8965 TPACPI_PARAM(video);
8966 TPACPI_PARAM(light);
8967 TPACPI_PARAM(cmos);
8968 TPACPI_PARAM(led);
8969 TPACPI_PARAM(beep);
8970 TPACPI_PARAM(ecdump);
8971 TPACPI_PARAM(brightness);
8972 TPACPI_PARAM(volume);
8973 TPACPI_PARAM(fan);
8975 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8976 module_param(dbg_wlswemul, uint, 0444);
8977 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8978 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8979 MODULE_PARM_DESC(wlsw_state,
8980 "Initial state of the emulated WLSW switch");
8982 module_param(dbg_bluetoothemul, uint, 0444);
8983 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8984 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8985 MODULE_PARM_DESC(bluetooth_state,
8986 "Initial state of the emulated bluetooth switch");
8988 module_param(dbg_wwanemul, uint, 0444);
8989 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8990 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8991 MODULE_PARM_DESC(wwan_state,
8992 "Initial state of the emulated WWAN switch");
8994 module_param(dbg_uwbemul, uint, 0444);
8995 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8996 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8997 MODULE_PARM_DESC(uwb_state,
8998 "Initial state of the emulated UWB switch");
8999 #endif
9001 static void thinkpad_acpi_module_exit(void)
9003 struct ibm_struct *ibm, *itmp;
9005 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9007 list_for_each_entry_safe_reverse(ibm, itmp,
9008 &tpacpi_all_drivers,
9009 all_drivers) {
9010 ibm_exit(ibm);
9013 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9015 if (tpacpi_inputdev) {
9016 if (tp_features.input_device_registered)
9017 input_unregister_device(tpacpi_inputdev);
9018 else
9019 input_free_device(tpacpi_inputdev);
9022 if (tpacpi_hwmon)
9023 hwmon_device_unregister(tpacpi_hwmon);
9025 if (tp_features.sensors_pdev_attrs_registered)
9026 device_remove_file(&tpacpi_sensors_pdev->dev,
9027 &dev_attr_thinkpad_acpi_pdev_name);
9028 if (tpacpi_sensors_pdev)
9029 platform_device_unregister(tpacpi_sensors_pdev);
9030 if (tpacpi_pdev)
9031 platform_device_unregister(tpacpi_pdev);
9033 if (tp_features.sensors_pdrv_attrs_registered)
9034 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9035 if (tp_features.platform_drv_attrs_registered)
9036 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9038 if (tp_features.sensors_pdrv_registered)
9039 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9041 if (tp_features.platform_drv_registered)
9042 platform_driver_unregister(&tpacpi_pdriver);
9044 if (proc_dir)
9045 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9047 if (tpacpi_wq)
9048 destroy_workqueue(tpacpi_wq);
9050 kfree(thinkpad_id.bios_version_str);
9051 kfree(thinkpad_id.ec_version_str);
9052 kfree(thinkpad_id.model_str);
9056 static int __init thinkpad_acpi_module_init(void)
9058 int ret, i;
9060 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9062 /* Parameter checking */
9063 if (hotkey_report_mode > 2)
9064 return -EINVAL;
9066 /* Driver-level probe */
9068 ret = get_thinkpad_model_data(&thinkpad_id);
9069 if (ret) {
9070 printk(TPACPI_ERR
9071 "unable to get DMI data: %d\n", ret);
9072 thinkpad_acpi_module_exit();
9073 return ret;
9075 ret = probe_for_thinkpad();
9076 if (ret) {
9077 thinkpad_acpi_module_exit();
9078 return ret;
9081 /* Driver initialization */
9083 thinkpad_acpi_init_banner();
9084 tpacpi_check_outdated_fw();
9086 TPACPI_ACPIHANDLE_INIT(ecrd);
9087 TPACPI_ACPIHANDLE_INIT(ecwr);
9089 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9090 if (!tpacpi_wq) {
9091 thinkpad_acpi_module_exit();
9092 return -ENOMEM;
9095 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9096 if (!proc_dir) {
9097 printk(TPACPI_ERR
9098 "unable to create proc dir " TPACPI_PROC_DIR);
9099 thinkpad_acpi_module_exit();
9100 return -ENODEV;
9103 ret = platform_driver_register(&tpacpi_pdriver);
9104 if (ret) {
9105 printk(TPACPI_ERR
9106 "unable to register main platform driver\n");
9107 thinkpad_acpi_module_exit();
9108 return ret;
9110 tp_features.platform_drv_registered = 1;
9112 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9113 if (ret) {
9114 printk(TPACPI_ERR
9115 "unable to register hwmon platform driver\n");
9116 thinkpad_acpi_module_exit();
9117 return ret;
9119 tp_features.sensors_pdrv_registered = 1;
9121 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9122 if (!ret) {
9123 tp_features.platform_drv_attrs_registered = 1;
9124 ret = tpacpi_create_driver_attributes(
9125 &tpacpi_hwmon_pdriver.driver);
9127 if (ret) {
9128 printk(TPACPI_ERR
9129 "unable to create sysfs driver attributes\n");
9130 thinkpad_acpi_module_exit();
9131 return ret;
9133 tp_features.sensors_pdrv_attrs_registered = 1;
9136 /* Device initialization */
9137 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9138 NULL, 0);
9139 if (IS_ERR(tpacpi_pdev)) {
9140 ret = PTR_ERR(tpacpi_pdev);
9141 tpacpi_pdev = NULL;
9142 printk(TPACPI_ERR "unable to register platform device\n");
9143 thinkpad_acpi_module_exit();
9144 return ret;
9146 tpacpi_sensors_pdev = platform_device_register_simple(
9147 TPACPI_HWMON_DRVR_NAME,
9148 -1, NULL, 0);
9149 if (IS_ERR(tpacpi_sensors_pdev)) {
9150 ret = PTR_ERR(tpacpi_sensors_pdev);
9151 tpacpi_sensors_pdev = NULL;
9152 printk(TPACPI_ERR
9153 "unable to register hwmon platform device\n");
9154 thinkpad_acpi_module_exit();
9155 return ret;
9157 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9158 &dev_attr_thinkpad_acpi_pdev_name);
9159 if (ret) {
9160 printk(TPACPI_ERR
9161 "unable to create sysfs hwmon device attributes\n");
9162 thinkpad_acpi_module_exit();
9163 return ret;
9165 tp_features.sensors_pdev_attrs_registered = 1;
9166 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9167 if (IS_ERR(tpacpi_hwmon)) {
9168 ret = PTR_ERR(tpacpi_hwmon);
9169 tpacpi_hwmon = NULL;
9170 printk(TPACPI_ERR "unable to register hwmon device\n");
9171 thinkpad_acpi_module_exit();
9172 return ret;
9174 mutex_init(&tpacpi_inputdev_send_mutex);
9175 tpacpi_inputdev = input_allocate_device();
9176 if (!tpacpi_inputdev) {
9177 printk(TPACPI_ERR "unable to allocate input device\n");
9178 thinkpad_acpi_module_exit();
9179 return -ENOMEM;
9180 } else {
9181 /* Prepare input device, but don't register */
9182 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9183 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9184 tpacpi_inputdev->id.bustype = BUS_HOST;
9185 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
9186 thinkpad_id.vendor :
9187 PCI_VENDOR_ID_IBM;
9188 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9189 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9190 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9193 /* Init subdriver dependencies */
9194 tpacpi_detect_brightness_capabilities();
9196 /* Init subdrivers */
9197 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9198 ret = ibm_init(&ibms_init[i]);
9199 if (ret >= 0 && *ibms_init[i].param)
9200 ret = ibms_init[i].data->write(ibms_init[i].param);
9201 if (ret < 0) {
9202 thinkpad_acpi_module_exit();
9203 return ret;
9207 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9209 ret = input_register_device(tpacpi_inputdev);
9210 if (ret < 0) {
9211 printk(TPACPI_ERR "unable to register input device\n");
9212 thinkpad_acpi_module_exit();
9213 return ret;
9214 } else {
9215 tp_features.input_device_registered = 1;
9218 return 0;
9221 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9224 * This will autoload the driver in almost every ThinkPad
9225 * in widespread use.
9227 * Only _VERY_ old models, like the 240, 240x and 570 lack
9228 * the HKEY event interface.
9230 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9233 * DMI matching for module autoloading
9235 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9236 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9238 * Only models listed in thinkwiki will be supported, so add yours
9239 * if it is not there yet.
9241 #define IBM_BIOS_MODULE_ALIAS(__type) \
9242 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9244 /* Ancient thinkpad BIOSes have to be identified by
9245 * BIOS type or model number, and there are far less
9246 * BIOS types than model numbers... */
9247 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9249 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9250 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9251 MODULE_DESCRIPTION(TPACPI_DESC);
9252 MODULE_VERSION(TPACPI_VERSION);
9253 MODULE_LICENSE("GPL");
9255 module_init(thinkpad_acpi_module_init);
9256 module_exit(thinkpad_acpi_module_exit);