thinkpad-acpi: disable volume control
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobb5c0ac4590a9894517c0587813b0787020d9cea6
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.23"
25 #define TPACPI_SYSFS_VERSION 0x020700
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/seq_file.h>
65 #include <linux/sysfs.h>
66 #include <linux/backlight.h>
67 #include <linux/fb.h>
68 #include <linux/platform_device.h>
69 #include <linux/hwmon.h>
70 #include <linux/hwmon-sysfs.h>
71 #include <linux/input.h>
72 #include <linux/leds.h>
73 #include <linux/rfkill.h>
74 #include <asm/uaccess.h>
76 #include <linux/dmi.h>
77 #include <linux/jiffies.h>
78 #include <linux/workqueue.h>
80 #include <acpi/acpi_drivers.h>
82 #include <linux/pci_ids.h>
85 /* ThinkPad CMOS commands */
86 #define TP_CMOS_VOLUME_DOWN 0
87 #define TP_CMOS_VOLUME_UP 1
88 #define TP_CMOS_VOLUME_MUTE 2
89 #define TP_CMOS_BRIGHTNESS_UP 4
90 #define TP_CMOS_BRIGHTNESS_DOWN 5
91 #define TP_CMOS_THINKLIGHT_ON 12
92 #define TP_CMOS_THINKLIGHT_OFF 13
94 /* NVRAM Addresses */
95 enum tp_nvram_addr {
96 TP_NVRAM_ADDR_HK2 = 0x57,
97 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
98 TP_NVRAM_ADDR_VIDEO = 0x59,
99 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
100 TP_NVRAM_ADDR_MIXER = 0x60,
103 /* NVRAM bit masks */
104 enum {
105 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
106 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
107 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
108 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
109 TP_NVRAM_MASK_THINKLIGHT = 0x10,
110 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
111 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
112 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
113 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
114 TP_NVRAM_MASK_MUTE = 0x40,
115 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
116 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
117 TP_NVRAM_POS_LEVEL_VOLUME = 0,
120 /* ACPI HIDs */
121 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
122 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
123 #define TPACPI_ACPI_EC_HID "PNP0C09"
125 /* Input IDs */
126 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
127 #define TPACPI_HKEY_INPUT_VERSION 0x4101
129 /* ACPI \WGSV commands */
130 enum {
131 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
132 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
133 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
134 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
137 /* TP_ACPI_WGSV_GET_STATE bits */
138 enum {
139 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
140 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
141 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
142 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
143 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
144 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
145 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
146 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
147 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
148 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
151 /* HKEY events */
152 enum tpacpi_hkey_event_t {
153 /* Hotkey-related */
154 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
155 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
156 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
157 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
158 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
159 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
161 /* Reasons for waking up from S3/S4 */
162 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
163 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
164 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
165 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
166 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
167 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
169 /* Auto-sleep after eject request */
170 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
171 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
173 /* Misc bay events */
174 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
176 /* User-interface events */
177 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
178 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
179 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
180 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
181 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
182 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
183 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
185 /* Thermal events */
186 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
187 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
188 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
189 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
190 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
192 /* Misc */
193 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
196 /****************************************************************************
197 * Main driver
200 #define TPACPI_NAME "thinkpad"
201 #define TPACPI_DESC "ThinkPad ACPI Extras"
202 #define TPACPI_FILE TPACPI_NAME "_acpi"
203 #define TPACPI_URL "http://ibm-acpi.sf.net/"
204 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
206 #define TPACPI_PROC_DIR "ibm"
207 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
208 #define TPACPI_DRVR_NAME TPACPI_FILE
209 #define TPACPI_DRVR_SHORTNAME "tpacpi"
210 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
212 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
213 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
215 #define TPACPI_MAX_ACPI_ARGS 3
217 /* printk headers */
218 #define TPACPI_LOG TPACPI_FILE ": "
219 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
220 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
221 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
222 #define TPACPI_ERR KERN_ERR TPACPI_LOG
223 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
224 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
225 #define TPACPI_INFO KERN_INFO TPACPI_LOG
226 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
228 /* Debugging printk groups */
229 #define TPACPI_DBG_ALL 0xffff
230 #define TPACPI_DBG_DISCLOSETASK 0x8000
231 #define TPACPI_DBG_INIT 0x0001
232 #define TPACPI_DBG_EXIT 0x0002
233 #define TPACPI_DBG_RFKILL 0x0004
234 #define TPACPI_DBG_HKEY 0x0008
235 #define TPACPI_DBG_FAN 0x0010
236 #define TPACPI_DBG_BRGHT 0x0020
237 #define TPACPI_DBG_MIXER 0x0040
239 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
240 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
241 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
244 /****************************************************************************
245 * Driver-wide structs and misc. variables
248 struct ibm_struct;
250 struct tp_acpi_drv_struct {
251 const struct acpi_device_id *hid;
252 struct acpi_driver *driver;
254 void (*notify) (struct ibm_struct *, u32);
255 acpi_handle *handle;
256 u32 type;
257 struct acpi_device *device;
260 struct ibm_struct {
261 char *name;
263 int (*read) (struct seq_file *);
264 int (*write) (char *);
265 void (*exit) (void);
266 void (*resume) (void);
267 void (*suspend) (pm_message_t state);
268 void (*shutdown) (void);
270 struct list_head all_drivers;
272 struct tp_acpi_drv_struct *acpi;
274 struct {
275 u8 acpi_driver_registered:1;
276 u8 acpi_notify_installed:1;
277 u8 proc_created:1;
278 u8 init_called:1;
279 u8 experimental:1;
280 } flags;
283 struct ibm_init_struct {
284 char param[32];
286 int (*init) (struct ibm_init_struct *);
287 mode_t base_procfs_mode;
288 struct ibm_struct *data;
291 static struct {
292 u32 bluetooth:1;
293 u32 hotkey:1;
294 u32 hotkey_mask:1;
295 u32 hotkey_wlsw:1;
296 u32 hotkey_tablet:1;
297 u32 light:1;
298 u32 light_status:1;
299 u32 bright_16levels:1;
300 u32 bright_acpimode:1;
301 u32 wan:1;
302 u32 uwb:1;
303 u32 fan_ctrl_status_undef:1;
304 u32 second_fan:1;
305 u32 beep_needs_two_args:1;
306 u32 mixer_no_level_control:1;
307 u32 input_device_registered:1;
308 u32 platform_drv_registered:1;
309 u32 platform_drv_attrs_registered:1;
310 u32 sensors_pdrv_registered:1;
311 u32 sensors_pdrv_attrs_registered:1;
312 u32 sensors_pdev_attrs_registered:1;
313 u32 hotkey_poll_active:1;
314 } tp_features;
316 static struct {
317 u16 hotkey_mask_ff:1;
318 u16 volume_ctrl_forbidden:1;
319 } tp_warned;
321 struct thinkpad_id_data {
322 unsigned int vendor; /* ThinkPad vendor:
323 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
325 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
326 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
328 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
329 u16 ec_model;
330 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
331 u16 ec_release;
333 char *model_str; /* ThinkPad T43 */
334 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
336 static struct thinkpad_id_data thinkpad_id;
338 static enum {
339 TPACPI_LIFE_INIT = 0,
340 TPACPI_LIFE_RUNNING,
341 TPACPI_LIFE_EXITING,
342 } tpacpi_lifecycle;
344 static int experimental;
345 static u32 dbg_level;
347 static struct workqueue_struct *tpacpi_wq;
349 enum led_status_t {
350 TPACPI_LED_OFF = 0,
351 TPACPI_LED_ON,
352 TPACPI_LED_BLINK,
355 /* Special LED class that can defer work */
356 struct tpacpi_led_classdev {
357 struct led_classdev led_classdev;
358 struct work_struct work;
359 enum led_status_t new_state;
360 unsigned int led;
363 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
364 static int dbg_wlswemul;
365 static int tpacpi_wlsw_emulstate;
366 static int dbg_bluetoothemul;
367 static int tpacpi_bluetooth_emulstate;
368 static int dbg_wwanemul;
369 static int tpacpi_wwan_emulstate;
370 static int dbg_uwbemul;
371 static int tpacpi_uwb_emulstate;
372 #endif
375 /*************************************************************************
376 * Debugging helpers
379 #define dbg_printk(a_dbg_level, format, arg...) \
380 do { if (dbg_level & (a_dbg_level)) \
381 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
382 } while (0)
384 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
385 #define vdbg_printk dbg_printk
386 static const char *str_supported(int is_supported);
387 #else
388 #define vdbg_printk(a_dbg_level, format, arg...) \
389 do { } while (0)
390 #endif
392 static void tpacpi_log_usertask(const char * const what)
394 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
395 what, task_tgid_vnr(current));
398 #define tpacpi_disclose_usertask(what, format, arg...) \
399 do { \
400 if (unlikely( \
401 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
402 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
403 printk(TPACPI_DEBUG "%s: PID %d: " format, \
404 what, task_tgid_vnr(current), ## arg); \
406 } while (0)
409 * Quirk handling helpers
411 * ThinkPad IDs and versions seen in the field so far
412 * are two-characters from the set [0-9A-Z], i.e. base 36.
414 * We use values well outside that range as specials.
417 #define TPACPI_MATCH_ANY 0xffffU
418 #define TPACPI_MATCH_UNKNOWN 0U
420 /* TPID('1', 'Y') == 0x5931 */
421 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
423 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
424 { .vendor = PCI_VENDOR_ID_IBM, \
425 .bios = TPID(__id1, __id2), \
426 .ec = TPACPI_MATCH_ANY, \
427 .quirks = (__quirk) }
429 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
430 { .vendor = PCI_VENDOR_ID_LENOVO, \
431 .bios = TPID(__id1, __id2), \
432 .ec = TPACPI_MATCH_ANY, \
433 .quirks = (__quirk) }
435 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
436 { .vendor = PCI_VENDOR_ID_LENOVO, \
437 .bios = TPACPI_MATCH_ANY, \
438 .ec = TPID(__id1, __id2), \
439 .quirks = (__quirk) }
441 struct tpacpi_quirk {
442 unsigned int vendor;
443 u16 bios;
444 u16 ec;
445 unsigned long quirks;
449 * tpacpi_check_quirks() - search BIOS/EC version on a list
450 * @qlist: array of &struct tpacpi_quirk
451 * @qlist_size: number of elements in @qlist
453 * Iterates over a quirks list until one is found that matches the
454 * ThinkPad's vendor, BIOS and EC model.
456 * Returns 0 if nothing matches, otherwise returns the quirks field of
457 * the matching &struct tpacpi_quirk entry.
459 * The match criteria is: vendor, ec and bios much match.
461 static unsigned long __init tpacpi_check_quirks(
462 const struct tpacpi_quirk *qlist,
463 unsigned int qlist_size)
465 while (qlist_size) {
466 if ((qlist->vendor == thinkpad_id.vendor ||
467 qlist->vendor == TPACPI_MATCH_ANY) &&
468 (qlist->bios == thinkpad_id.bios_model ||
469 qlist->bios == TPACPI_MATCH_ANY) &&
470 (qlist->ec == thinkpad_id.ec_model ||
471 qlist->ec == TPACPI_MATCH_ANY))
472 return qlist->quirks;
474 qlist_size--;
475 qlist++;
477 return 0;
481 /****************************************************************************
482 ****************************************************************************
484 * ACPI Helpers and device model
486 ****************************************************************************
487 ****************************************************************************/
489 /*************************************************************************
490 * ACPI basic handles
493 static acpi_handle root_handle;
495 #define TPACPI_HANDLE(object, parent, paths...) \
496 static acpi_handle object##_handle; \
497 static acpi_handle *object##_parent = &parent##_handle; \
498 static char *object##_path; \
499 static char *object##_paths[] = { paths }
501 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
502 "\\_SB.PCI.ISA.EC", /* 570 */
503 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
504 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
505 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
506 "\\_SB.PCI0.ICH3.EC0", /* R31 */
507 "\\_SB.PCI0.LPC.EC", /* all others */
510 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
511 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
513 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
514 /* T4x, X31, X40 */
515 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
516 "\\CMS", /* R40, R40e */
517 ); /* all others */
519 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
520 "^HKEY", /* R30, R31 */
521 "HKEY", /* all others */
522 ); /* 570 */
524 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
525 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
526 "\\_SB.PCI0.VID0", /* 770e */
527 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
528 "\\_SB.PCI0.AGP.VID", /* all others */
529 ); /* R30, R31 */
532 /*************************************************************************
533 * ACPI helpers
536 static int acpi_evalf(acpi_handle handle,
537 void *res, char *method, char *fmt, ...)
539 char *fmt0 = fmt;
540 struct acpi_object_list params;
541 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
542 struct acpi_buffer result, *resultp;
543 union acpi_object out_obj;
544 acpi_status status;
545 va_list ap;
546 char res_type;
547 int success;
548 int quiet;
550 if (!*fmt) {
551 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
552 return 0;
555 if (*fmt == 'q') {
556 quiet = 1;
557 fmt++;
558 } else
559 quiet = 0;
561 res_type = *(fmt++);
563 params.count = 0;
564 params.pointer = &in_objs[0];
566 va_start(ap, fmt);
567 while (*fmt) {
568 char c = *(fmt++);
569 switch (c) {
570 case 'd': /* int */
571 in_objs[params.count].integer.value = va_arg(ap, int);
572 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
573 break;
574 /* add more types as needed */
575 default:
576 printk(TPACPI_ERR "acpi_evalf() called "
577 "with invalid format character '%c'\n", c);
578 return 0;
581 va_end(ap);
583 if (res_type != 'v') {
584 result.length = sizeof(out_obj);
585 result.pointer = &out_obj;
586 resultp = &result;
587 } else
588 resultp = NULL;
590 status = acpi_evaluate_object(handle, method, &params, resultp);
592 switch (res_type) {
593 case 'd': /* int */
594 if (res)
595 *(int *)res = out_obj.integer.value;
596 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
597 break;
598 case 'v': /* void */
599 success = status == AE_OK;
600 break;
601 /* add more types as needed */
602 default:
603 printk(TPACPI_ERR "acpi_evalf() called "
604 "with invalid format character '%c'\n", res_type);
605 return 0;
608 if (!success && !quiet)
609 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
610 method, fmt0, status);
612 return success;
615 static int acpi_ec_read(int i, u8 *p)
617 int v;
619 if (ecrd_handle) {
620 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
621 return 0;
622 *p = v;
623 } else {
624 if (ec_read(i, p) < 0)
625 return 0;
628 return 1;
631 static int acpi_ec_write(int i, u8 v)
633 if (ecwr_handle) {
634 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
635 return 0;
636 } else {
637 if (ec_write(i, v) < 0)
638 return 0;
641 return 1;
644 static int issue_thinkpad_cmos_command(int cmos_cmd)
646 if (!cmos_handle)
647 return -ENXIO;
649 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
650 return -EIO;
652 return 0;
655 /*************************************************************************
656 * ACPI device model
659 #define TPACPI_ACPIHANDLE_INIT(object) \
660 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
661 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
663 static void drv_acpi_handle_init(char *name,
664 acpi_handle *handle, acpi_handle parent,
665 char **paths, int num_paths, char **path)
667 int i;
668 acpi_status status;
670 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
671 name);
673 for (i = 0; i < num_paths; i++) {
674 status = acpi_get_handle(parent, paths[i], handle);
675 if (ACPI_SUCCESS(status)) {
676 *path = paths[i];
677 dbg_printk(TPACPI_DBG_INIT,
678 "Found ACPI handle %s for %s\n",
679 *path, name);
680 return;
684 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
685 name);
686 *handle = NULL;
689 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
691 struct ibm_struct *ibm = data;
693 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
694 return;
696 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
697 return;
699 ibm->acpi->notify(ibm, event);
702 static int __init setup_acpi_notify(struct ibm_struct *ibm)
704 acpi_status status;
705 int rc;
707 BUG_ON(!ibm->acpi);
709 if (!*ibm->acpi->handle)
710 return 0;
712 vdbg_printk(TPACPI_DBG_INIT,
713 "setting up ACPI notify for %s\n", ibm->name);
715 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
716 if (rc < 0) {
717 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
718 ibm->name, rc);
719 return -ENODEV;
722 ibm->acpi->device->driver_data = ibm;
723 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
724 TPACPI_ACPI_EVENT_PREFIX,
725 ibm->name);
727 status = acpi_install_notify_handler(*ibm->acpi->handle,
728 ibm->acpi->type, dispatch_acpi_notify, ibm);
729 if (ACPI_FAILURE(status)) {
730 if (status == AE_ALREADY_EXISTS) {
731 printk(TPACPI_NOTICE
732 "another device driver is already "
733 "handling %s events\n", ibm->name);
734 } else {
735 printk(TPACPI_ERR
736 "acpi_install_notify_handler(%s) failed: %d\n",
737 ibm->name, status);
739 return -ENODEV;
741 ibm->flags.acpi_notify_installed = 1;
742 return 0;
745 static int __init tpacpi_device_add(struct acpi_device *device)
747 return 0;
750 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
752 int rc;
754 dbg_printk(TPACPI_DBG_INIT,
755 "registering %s as an ACPI driver\n", ibm->name);
757 BUG_ON(!ibm->acpi);
759 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
760 if (!ibm->acpi->driver) {
761 printk(TPACPI_ERR
762 "failed to allocate memory for ibm->acpi->driver\n");
763 return -ENOMEM;
766 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
767 ibm->acpi->driver->ids = ibm->acpi->hid;
769 ibm->acpi->driver->ops.add = &tpacpi_device_add;
771 rc = acpi_bus_register_driver(ibm->acpi->driver);
772 if (rc < 0) {
773 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
774 ibm->name, rc);
775 kfree(ibm->acpi->driver);
776 ibm->acpi->driver = NULL;
777 } else if (!rc)
778 ibm->flags.acpi_driver_registered = 1;
780 return rc;
784 /****************************************************************************
785 ****************************************************************************
787 * Procfs Helpers
789 ****************************************************************************
790 ****************************************************************************/
792 static int dispatch_proc_show(struct seq_file *m, void *v)
794 struct ibm_struct *ibm = m->private;
796 if (!ibm || !ibm->read)
797 return -EINVAL;
798 return ibm->read(m);
801 static int dispatch_proc_open(struct inode *inode, struct file *file)
803 return single_open(file, dispatch_proc_show, PDE(inode)->data);
806 static ssize_t dispatch_proc_write(struct file *file,
807 const char __user *userbuf,
808 size_t count, loff_t *pos)
810 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
811 char *kernbuf;
812 int ret;
814 if (!ibm || !ibm->write)
815 return -EINVAL;
816 if (count > PAGE_SIZE - 2)
817 return -EINVAL;
819 kernbuf = kmalloc(count + 2, GFP_KERNEL);
820 if (!kernbuf)
821 return -ENOMEM;
823 if (copy_from_user(kernbuf, userbuf, count)) {
824 kfree(kernbuf);
825 return -EFAULT;
828 kernbuf[count] = 0;
829 strcat(kernbuf, ",");
830 ret = ibm->write(kernbuf);
831 if (ret == 0)
832 ret = count;
834 kfree(kernbuf);
836 return ret;
839 static const struct file_operations dispatch_proc_fops = {
840 .owner = THIS_MODULE,
841 .open = dispatch_proc_open,
842 .read = seq_read,
843 .llseek = seq_lseek,
844 .release = single_release,
845 .write = dispatch_proc_write,
848 static char *next_cmd(char **cmds)
850 char *start = *cmds;
851 char *end;
853 while ((end = strchr(start, ',')) && end == start)
854 start = end + 1;
856 if (!end)
857 return NULL;
859 *end = 0;
860 *cmds = end + 1;
861 return start;
865 /****************************************************************************
866 ****************************************************************************
868 * Device model: input, hwmon and platform
870 ****************************************************************************
871 ****************************************************************************/
873 static struct platform_device *tpacpi_pdev;
874 static struct platform_device *tpacpi_sensors_pdev;
875 static struct device *tpacpi_hwmon;
876 static struct input_dev *tpacpi_inputdev;
877 static struct mutex tpacpi_inputdev_send_mutex;
878 static LIST_HEAD(tpacpi_all_drivers);
880 static int tpacpi_suspend_handler(struct platform_device *pdev,
881 pm_message_t state)
883 struct ibm_struct *ibm, *itmp;
885 list_for_each_entry_safe(ibm, itmp,
886 &tpacpi_all_drivers,
887 all_drivers) {
888 if (ibm->suspend)
889 (ibm->suspend)(state);
892 return 0;
895 static int tpacpi_resume_handler(struct platform_device *pdev)
897 struct ibm_struct *ibm, *itmp;
899 list_for_each_entry_safe(ibm, itmp,
900 &tpacpi_all_drivers,
901 all_drivers) {
902 if (ibm->resume)
903 (ibm->resume)();
906 return 0;
909 static void tpacpi_shutdown_handler(struct platform_device *pdev)
911 struct ibm_struct *ibm, *itmp;
913 list_for_each_entry_safe(ibm, itmp,
914 &tpacpi_all_drivers,
915 all_drivers) {
916 if (ibm->shutdown)
917 (ibm->shutdown)();
921 static struct platform_driver tpacpi_pdriver = {
922 .driver = {
923 .name = TPACPI_DRVR_NAME,
924 .owner = THIS_MODULE,
926 .suspend = tpacpi_suspend_handler,
927 .resume = tpacpi_resume_handler,
928 .shutdown = tpacpi_shutdown_handler,
931 static struct platform_driver tpacpi_hwmon_pdriver = {
932 .driver = {
933 .name = TPACPI_HWMON_DRVR_NAME,
934 .owner = THIS_MODULE,
938 /*************************************************************************
939 * sysfs support helpers
942 struct attribute_set {
943 unsigned int members, max_members;
944 struct attribute_group group;
947 struct attribute_set_obj {
948 struct attribute_set s;
949 struct attribute *a;
950 } __attribute__((packed));
952 static struct attribute_set *create_attr_set(unsigned int max_members,
953 const char *name)
955 struct attribute_set_obj *sobj;
957 if (max_members == 0)
958 return NULL;
960 /* Allocates space for implicit NULL at the end too */
961 sobj = kzalloc(sizeof(struct attribute_set_obj) +
962 max_members * sizeof(struct attribute *),
963 GFP_KERNEL);
964 if (!sobj)
965 return NULL;
966 sobj->s.max_members = max_members;
967 sobj->s.group.attrs = &sobj->a;
968 sobj->s.group.name = name;
970 return &sobj->s;
973 #define destroy_attr_set(_set) \
974 kfree(_set);
976 /* not multi-threaded safe, use it in a single thread per set */
977 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
979 if (!s || !attr)
980 return -EINVAL;
982 if (s->members >= s->max_members)
983 return -ENOMEM;
985 s->group.attrs[s->members] = attr;
986 s->members++;
988 return 0;
991 static int add_many_to_attr_set(struct attribute_set *s,
992 struct attribute **attr,
993 unsigned int count)
995 int i, res;
997 for (i = 0; i < count; i++) {
998 res = add_to_attr_set(s, attr[i]);
999 if (res)
1000 return res;
1003 return 0;
1006 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1008 sysfs_remove_group(kobj, &s->group);
1009 destroy_attr_set(s);
1012 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1013 sysfs_create_group(_kobj, &_attr_set->group)
1015 static int parse_strtoul(const char *buf,
1016 unsigned long max, unsigned long *value)
1018 char *endp;
1020 while (*buf && isspace(*buf))
1021 buf++;
1022 *value = simple_strtoul(buf, &endp, 0);
1023 while (*endp && isspace(*endp))
1024 endp++;
1025 if (*endp || *value > max)
1026 return -EINVAL;
1028 return 0;
1031 static void tpacpi_disable_brightness_delay(void)
1033 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1034 printk(TPACPI_NOTICE
1035 "ACPI backlight control delay disabled\n");
1038 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1040 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1041 union acpi_object *obj;
1042 int rc;
1044 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1045 obj = (union acpi_object *)buffer.pointer;
1046 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1047 printk(TPACPI_ERR "Unknown _BCL data, "
1048 "please report this to %s\n", TPACPI_MAIL);
1049 rc = 0;
1050 } else {
1051 rc = obj->package.count;
1053 } else {
1054 return 0;
1057 kfree(buffer.pointer);
1058 return rc;
1061 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1062 u32 lvl, void *context, void **rv)
1064 char name[ACPI_PATH_SEGMENT_LENGTH];
1065 struct acpi_buffer buffer = { sizeof(name), &name };
1067 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1068 !strncmp("_BCL", name, sizeof(name) - 1)) {
1069 BUG_ON(!rv || !*rv);
1070 **(int **)rv = tpacpi_query_bcl_levels(handle);
1071 return AE_CTRL_TERMINATE;
1072 } else {
1073 return AE_OK;
1078 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1080 static int __init tpacpi_check_std_acpi_brightness_support(void)
1082 int status;
1083 int bcl_levels = 0;
1084 void *bcl_ptr = &bcl_levels;
1086 if (!vid_handle) {
1087 TPACPI_ACPIHANDLE_INIT(vid);
1089 if (!vid_handle)
1090 return 0;
1093 * Search for a _BCL method, and execute it. This is safe on all
1094 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1095 * BIOS in ACPI backlight control mode. We do NOT have to care
1096 * about calling the _BCL method in an enabled video device, any
1097 * will do for our purposes.
1100 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1101 tpacpi_acpi_walk_find_bcl, NULL,
1102 &bcl_ptr);
1104 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1105 tp_features.bright_acpimode = 1;
1106 return (bcl_levels - 2);
1109 return 0;
1112 static void printk_deprecated_attribute(const char * const what,
1113 const char * const details)
1115 tpacpi_log_usertask("deprecated sysfs attribute");
1116 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1117 "will be removed. %s\n",
1118 what, details);
1121 /*************************************************************************
1122 * rfkill and radio control support helpers
1126 * ThinkPad-ACPI firmware handling model:
1128 * WLSW (master wireless switch) is event-driven, and is common to all
1129 * firmware-controlled radios. It cannot be controlled, just monitored,
1130 * as expected. It overrides all radio state in firmware
1132 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1133 * (TODO: verify how WLSW interacts with the returned radio state).
1135 * The only time there are shadow radio state changes, is when
1136 * masked-off hotkeys are used.
1140 * Internal driver API for radio state:
1142 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1143 * bool: true means radio blocked (off)
1145 enum tpacpi_rfkill_state {
1146 TPACPI_RFK_RADIO_OFF = 0,
1147 TPACPI_RFK_RADIO_ON
1150 /* rfkill switches */
1151 enum tpacpi_rfk_id {
1152 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1153 TPACPI_RFK_WWAN_SW_ID,
1154 TPACPI_RFK_UWB_SW_ID,
1155 TPACPI_RFK_SW_MAX
1158 static const char *tpacpi_rfkill_names[] = {
1159 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1160 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1161 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1162 [TPACPI_RFK_SW_MAX] = NULL
1165 /* ThinkPad-ACPI rfkill subdriver */
1166 struct tpacpi_rfk {
1167 struct rfkill *rfkill;
1168 enum tpacpi_rfk_id id;
1169 const struct tpacpi_rfk_ops *ops;
1172 struct tpacpi_rfk_ops {
1173 /* firmware interface */
1174 int (*get_status)(void);
1175 int (*set_status)(const enum tpacpi_rfkill_state);
1178 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1180 /* Query FW and update rfkill sw state for a given rfkill switch */
1181 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1183 int status;
1185 if (!tp_rfk)
1186 return -ENODEV;
1188 status = (tp_rfk->ops->get_status)();
1189 if (status < 0)
1190 return status;
1192 rfkill_set_sw_state(tp_rfk->rfkill,
1193 (status == TPACPI_RFK_RADIO_OFF));
1195 return status;
1198 /* Query FW and update rfkill sw state for all rfkill switches */
1199 static void tpacpi_rfk_update_swstate_all(void)
1201 unsigned int i;
1203 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1204 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1208 * Sync the HW-blocking state of all rfkill switches,
1209 * do notice it causes the rfkill core to schedule uevents
1211 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1213 unsigned int i;
1214 struct tpacpi_rfk *tp_rfk;
1216 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1217 tp_rfk = tpacpi_rfkill_switches[i];
1218 if (tp_rfk) {
1219 if (rfkill_set_hw_state(tp_rfk->rfkill,
1220 blocked)) {
1221 /* ignore -- we track sw block */
1227 /* Call to get the WLSW state from the firmware */
1228 static int hotkey_get_wlsw(void);
1230 /* Call to query WLSW state and update all rfkill switches */
1231 static bool tpacpi_rfk_check_hwblock_state(void)
1233 int res = hotkey_get_wlsw();
1234 int hw_blocked;
1236 /* When unknown or unsupported, we have to assume it is unblocked */
1237 if (res < 0)
1238 return false;
1240 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1241 tpacpi_rfk_update_hwblock_state(hw_blocked);
1243 return hw_blocked;
1246 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1248 struct tpacpi_rfk *tp_rfk = data;
1249 int res;
1251 dbg_printk(TPACPI_DBG_RFKILL,
1252 "request to change radio state to %s\n",
1253 blocked ? "blocked" : "unblocked");
1255 /* try to set radio state */
1256 res = (tp_rfk->ops->set_status)(blocked ?
1257 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1259 /* and update the rfkill core with whatever the FW really did */
1260 tpacpi_rfk_update_swstate(tp_rfk);
1262 return (res < 0) ? res : 0;
1265 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1266 .set_block = tpacpi_rfk_hook_set_block,
1269 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1270 const struct tpacpi_rfk_ops *tp_rfkops,
1271 const enum rfkill_type rfktype,
1272 const char *name,
1273 const bool set_default)
1275 struct tpacpi_rfk *atp_rfk;
1276 int res;
1277 bool sw_state = false;
1278 bool hw_state;
1279 int sw_status;
1281 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1283 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1284 if (atp_rfk)
1285 atp_rfk->rfkill = rfkill_alloc(name,
1286 &tpacpi_pdev->dev,
1287 rfktype,
1288 &tpacpi_rfk_rfkill_ops,
1289 atp_rfk);
1290 if (!atp_rfk || !atp_rfk->rfkill) {
1291 printk(TPACPI_ERR
1292 "failed to allocate memory for rfkill class\n");
1293 kfree(atp_rfk);
1294 return -ENOMEM;
1297 atp_rfk->id = id;
1298 atp_rfk->ops = tp_rfkops;
1300 sw_status = (tp_rfkops->get_status)();
1301 if (sw_status < 0) {
1302 printk(TPACPI_ERR
1303 "failed to read initial state for %s, error %d\n",
1304 name, sw_status);
1305 } else {
1306 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1307 if (set_default) {
1308 /* try to keep the initial state, since we ask the
1309 * firmware to preserve it across S5 in NVRAM */
1310 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1313 hw_state = tpacpi_rfk_check_hwblock_state();
1314 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1316 res = rfkill_register(atp_rfk->rfkill);
1317 if (res < 0) {
1318 printk(TPACPI_ERR
1319 "failed to register %s rfkill switch: %d\n",
1320 name, res);
1321 rfkill_destroy(atp_rfk->rfkill);
1322 kfree(atp_rfk);
1323 return res;
1326 tpacpi_rfkill_switches[id] = atp_rfk;
1328 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1329 name, (sw_state || hw_state) ? "" : "un");
1330 return 0;
1333 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1335 struct tpacpi_rfk *tp_rfk;
1337 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1339 tp_rfk = tpacpi_rfkill_switches[id];
1340 if (tp_rfk) {
1341 rfkill_unregister(tp_rfk->rfkill);
1342 rfkill_destroy(tp_rfk->rfkill);
1343 tpacpi_rfkill_switches[id] = NULL;
1344 kfree(tp_rfk);
1348 static void printk_deprecated_rfkill_attribute(const char * const what)
1350 printk_deprecated_attribute(what,
1351 "Please switch to generic rfkill before year 2010");
1354 /* sysfs <radio> enable ------------------------------------------------ */
1355 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1356 struct device_attribute *attr,
1357 char *buf)
1359 int status;
1361 printk_deprecated_rfkill_attribute(attr->attr.name);
1363 /* This is in the ABI... */
1364 if (tpacpi_rfk_check_hwblock_state()) {
1365 status = TPACPI_RFK_RADIO_OFF;
1366 } else {
1367 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1368 if (status < 0)
1369 return status;
1372 return snprintf(buf, PAGE_SIZE, "%d\n",
1373 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1376 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1377 struct device_attribute *attr,
1378 const char *buf, size_t count)
1380 unsigned long t;
1381 int res;
1383 printk_deprecated_rfkill_attribute(attr->attr.name);
1385 if (parse_strtoul(buf, 1, &t))
1386 return -EINVAL;
1388 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1390 /* This is in the ABI... */
1391 if (tpacpi_rfk_check_hwblock_state() && !!t)
1392 return -EPERM;
1394 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1395 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1396 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1398 return (res < 0) ? res : count;
1401 /* procfs -------------------------------------------------------------- */
1402 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id,
1403 struct seq_file *m)
1405 if (id >= TPACPI_RFK_SW_MAX)
1406 seq_printf(m, "status:\t\tnot supported\n");
1407 else {
1408 int status;
1410 /* This is in the ABI... */
1411 if (tpacpi_rfk_check_hwblock_state()) {
1412 status = TPACPI_RFK_RADIO_OFF;
1413 } else {
1414 status = tpacpi_rfk_update_swstate(
1415 tpacpi_rfkill_switches[id]);
1416 if (status < 0)
1417 return status;
1420 seq_printf(m, "status:\t\t%s\n",
1421 (status == TPACPI_RFK_RADIO_ON) ?
1422 "enabled" : "disabled");
1423 seq_printf(m, "commands:\tenable, disable\n");
1426 return 0;
1429 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1431 char *cmd;
1432 int status = -1;
1433 int res = 0;
1435 if (id >= TPACPI_RFK_SW_MAX)
1436 return -ENODEV;
1438 while ((cmd = next_cmd(&buf))) {
1439 if (strlencmp(cmd, "enable") == 0)
1440 status = TPACPI_RFK_RADIO_ON;
1441 else if (strlencmp(cmd, "disable") == 0)
1442 status = TPACPI_RFK_RADIO_OFF;
1443 else
1444 return -EINVAL;
1447 if (status != -1) {
1448 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1449 (status == TPACPI_RFK_RADIO_ON) ?
1450 "enable" : "disable",
1451 tpacpi_rfkill_names[id]);
1452 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1453 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1456 return res;
1459 /*************************************************************************
1460 * thinkpad-acpi driver attributes
1463 /* interface_version --------------------------------------------------- */
1464 static ssize_t tpacpi_driver_interface_version_show(
1465 struct device_driver *drv,
1466 char *buf)
1468 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1471 static DRIVER_ATTR(interface_version, S_IRUGO,
1472 tpacpi_driver_interface_version_show, NULL);
1474 /* debug_level --------------------------------------------------------- */
1475 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1476 char *buf)
1478 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1481 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1482 const char *buf, size_t count)
1484 unsigned long t;
1486 if (parse_strtoul(buf, 0xffff, &t))
1487 return -EINVAL;
1489 dbg_level = t;
1491 return count;
1494 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1495 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1497 /* version ------------------------------------------------------------- */
1498 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1499 char *buf)
1501 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1502 TPACPI_DESC, TPACPI_VERSION);
1505 static DRIVER_ATTR(version, S_IRUGO,
1506 tpacpi_driver_version_show, NULL);
1508 /* --------------------------------------------------------------------- */
1510 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1512 /* wlsw_emulstate ------------------------------------------------------ */
1513 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1514 char *buf)
1516 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1519 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1520 const char *buf, size_t count)
1522 unsigned long t;
1524 if (parse_strtoul(buf, 1, &t))
1525 return -EINVAL;
1527 if (tpacpi_wlsw_emulstate != !!t) {
1528 tpacpi_wlsw_emulstate = !!t;
1529 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1532 return count;
1535 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1536 tpacpi_driver_wlsw_emulstate_show,
1537 tpacpi_driver_wlsw_emulstate_store);
1539 /* bluetooth_emulstate ------------------------------------------------- */
1540 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1541 struct device_driver *drv,
1542 char *buf)
1544 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1547 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1548 struct device_driver *drv,
1549 const char *buf, size_t count)
1551 unsigned long t;
1553 if (parse_strtoul(buf, 1, &t))
1554 return -EINVAL;
1556 tpacpi_bluetooth_emulstate = !!t;
1558 return count;
1561 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1562 tpacpi_driver_bluetooth_emulstate_show,
1563 tpacpi_driver_bluetooth_emulstate_store);
1565 /* wwan_emulstate ------------------------------------------------- */
1566 static ssize_t tpacpi_driver_wwan_emulstate_show(
1567 struct device_driver *drv,
1568 char *buf)
1570 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1573 static ssize_t tpacpi_driver_wwan_emulstate_store(
1574 struct device_driver *drv,
1575 const char *buf, size_t count)
1577 unsigned long t;
1579 if (parse_strtoul(buf, 1, &t))
1580 return -EINVAL;
1582 tpacpi_wwan_emulstate = !!t;
1584 return count;
1587 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1588 tpacpi_driver_wwan_emulstate_show,
1589 tpacpi_driver_wwan_emulstate_store);
1591 /* uwb_emulstate ------------------------------------------------- */
1592 static ssize_t tpacpi_driver_uwb_emulstate_show(
1593 struct device_driver *drv,
1594 char *buf)
1596 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1599 static ssize_t tpacpi_driver_uwb_emulstate_store(
1600 struct device_driver *drv,
1601 const char *buf, size_t count)
1603 unsigned long t;
1605 if (parse_strtoul(buf, 1, &t))
1606 return -EINVAL;
1608 tpacpi_uwb_emulstate = !!t;
1610 return count;
1613 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1614 tpacpi_driver_uwb_emulstate_show,
1615 tpacpi_driver_uwb_emulstate_store);
1616 #endif
1618 /* --------------------------------------------------------------------- */
1620 static struct driver_attribute *tpacpi_driver_attributes[] = {
1621 &driver_attr_debug_level, &driver_attr_version,
1622 &driver_attr_interface_version,
1625 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1627 int i, res;
1629 i = 0;
1630 res = 0;
1631 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1632 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1633 i++;
1636 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1637 if (!res && dbg_wlswemul)
1638 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1639 if (!res && dbg_bluetoothemul)
1640 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1641 if (!res && dbg_wwanemul)
1642 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1643 if (!res && dbg_uwbemul)
1644 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1645 #endif
1647 return res;
1650 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1652 int i;
1654 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1655 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1657 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1658 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1659 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1660 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1661 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1662 #endif
1665 /*************************************************************************
1666 * Firmware Data
1670 * Table of recommended minimum BIOS versions
1672 * Reasons for listing:
1673 * 1. Stable BIOS, listed because the unknown ammount of
1674 * bugs and bad ACPI behaviour on older versions
1676 * 2. BIOS or EC fw with known bugs that trigger on Linux
1678 * 3. BIOS with known reduced functionality in older versions
1680 * We recommend the latest BIOS and EC version.
1681 * We only support the latest BIOS and EC fw version as a rule.
1683 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1684 * Information from users in ThinkWiki
1686 * WARNING: we use this table also to detect that the machine is
1687 * a ThinkPad in some cases, so don't remove entries lightly.
1690 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1691 { .vendor = (__v), \
1692 .bios = TPID(__id1, __id2), \
1693 .ec = TPACPI_MATCH_ANY, \
1694 .quirks = TPACPI_MATCH_ANY << 16 \
1695 | (__bv1) << 8 | (__bv2) }
1697 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1698 __eid, __ev1, __ev2) \
1699 { .vendor = (__v), \
1700 .bios = TPID(__bid1, __bid2), \
1701 .ec = __eid, \
1702 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1703 | (__bv1) << 8 | (__bv2) }
1705 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1706 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1708 /* Outdated IBM BIOSes often lack the EC id string */
1709 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1710 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1711 __bv1, __bv2, TPID(__id1, __id2), \
1712 __ev1, __ev2), \
1713 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1714 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1715 __ev1, __ev2)
1717 /* Outdated IBM BIOSes often lack the EC id string */
1718 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1719 __eid1, __eid2, __ev1, __ev2) \
1720 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1721 __bv1, __bv2, TPID(__eid1, __eid2), \
1722 __ev1, __ev2), \
1723 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1724 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1725 __ev1, __ev2)
1727 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1728 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1730 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1731 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1732 __bv1, __bv2, TPID(__id1, __id2), \
1733 __ev1, __ev2)
1735 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1736 __eid1, __eid2, __ev1, __ev2) \
1737 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1738 __bv1, __bv2, TPID(__eid1, __eid2), \
1739 __ev1, __ev2)
1741 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1742 /* Numeric models ------------------ */
1743 /* FW MODEL BIOS VERS */
1744 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1745 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1746 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1747 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1748 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1749 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1750 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1751 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1752 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1754 /* A-series ------------------------- */
1755 /* FW MODEL BIOS VERS EC VERS */
1756 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1757 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1758 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1759 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1760 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1761 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1762 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1763 TPV_QI0('1', '3', '2', '0'), /* A22m */
1764 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1765 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1766 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1768 /* G-series ------------------------- */
1769 /* FW MODEL BIOS VERS */
1770 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1771 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1773 /* R-series, T-series --------------- */
1774 /* FW MODEL BIOS VERS EC VERS */
1775 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1776 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1777 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1778 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1779 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1780 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1781 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1782 T40/p, T41/p, T42/p (1) */
1783 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1784 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1785 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1786 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1788 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1789 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1790 TPV_QI0('1', '6', '3', '2'), /* T22 */
1791 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1792 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1793 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1795 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1796 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1797 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1799 /* BIOS FW BIOS VERS EC FW EC VERS */
1800 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1801 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1803 /* X-series ------------------------- */
1804 /* FW MODEL BIOS VERS EC VERS */
1805 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1806 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1807 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1808 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1809 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1810 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1811 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1813 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1814 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1816 /* (0) - older versions lack DMI EC fw string and functionality */
1817 /* (1) - older versions known to lack functionality */
1820 #undef TPV_QL1
1821 #undef TPV_QL0
1822 #undef TPV_QI2
1823 #undef TPV_QI1
1824 #undef TPV_QI0
1825 #undef TPV_Q_X
1826 #undef TPV_Q
1828 static void __init tpacpi_check_outdated_fw(void)
1830 unsigned long fwvers;
1831 u16 ec_version, bios_version;
1833 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1834 ARRAY_SIZE(tpacpi_bios_version_qtable));
1836 if (!fwvers)
1837 return;
1839 bios_version = fwvers & 0xffffU;
1840 ec_version = (fwvers >> 16) & 0xffffU;
1842 /* note that unknown versions are set to 0x0000 and we use that */
1843 if ((bios_version > thinkpad_id.bios_release) ||
1844 (ec_version > thinkpad_id.ec_release &&
1845 ec_version != TPACPI_MATCH_ANY)) {
1847 * The changelogs would let us track down the exact
1848 * reason, but it is just too much of a pain to track
1849 * it. We only list BIOSes that are either really
1850 * broken, or really stable to begin with, so it is
1851 * best if the user upgrades the firmware anyway.
1853 printk(TPACPI_WARN
1854 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1855 printk(TPACPI_WARN
1856 "WARNING: This firmware may be missing critical bug "
1857 "fixes and/or important features\n");
1861 static bool __init tpacpi_is_fw_known(void)
1863 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1864 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1867 /****************************************************************************
1868 ****************************************************************************
1870 * Subdrivers
1872 ****************************************************************************
1873 ****************************************************************************/
1875 /*************************************************************************
1876 * thinkpad-acpi init subdriver
1879 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1881 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1882 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1884 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1885 (thinkpad_id.bios_version_str) ?
1886 thinkpad_id.bios_version_str : "unknown",
1887 (thinkpad_id.ec_version_str) ?
1888 thinkpad_id.ec_version_str : "unknown");
1890 if (thinkpad_id.vendor && thinkpad_id.model_str)
1891 printk(TPACPI_INFO "%s %s, model %s\n",
1892 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1893 "IBM" : ((thinkpad_id.vendor ==
1894 PCI_VENDOR_ID_LENOVO) ?
1895 "Lenovo" : "Unknown vendor"),
1896 thinkpad_id.model_str,
1897 (thinkpad_id.nummodel_str) ?
1898 thinkpad_id.nummodel_str : "unknown");
1900 tpacpi_check_outdated_fw();
1901 return 0;
1904 static int thinkpad_acpi_driver_read(struct seq_file *m)
1906 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1907 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1908 return 0;
1911 static struct ibm_struct thinkpad_acpi_driver_data = {
1912 .name = "driver",
1913 .read = thinkpad_acpi_driver_read,
1916 /*************************************************************************
1917 * Hotkey subdriver
1921 * ThinkPad firmware event model
1923 * The ThinkPad firmware has two main event interfaces: normal ACPI
1924 * notifications (which follow the ACPI standard), and a private event
1925 * interface.
1927 * The private event interface also issues events for the hotkeys. As
1928 * the driver gained features, the event handling code ended up being
1929 * built around the hotkey subdriver. This will need to be refactored
1930 * to a more formal event API eventually.
1932 * Some "hotkeys" are actually supposed to be used as event reports,
1933 * such as "brightness has changed", "volume has changed", depending on
1934 * the ThinkPad model and how the firmware is operating.
1936 * Unlike other classes, hotkey-class events have mask/unmask control on
1937 * non-ancient firmware. However, how it behaves changes a lot with the
1938 * firmware model and version.
1941 enum { /* hot key scan codes (derived from ACPI DSDT) */
1942 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1943 TP_ACPI_HOTKEYSCAN_FNF2,
1944 TP_ACPI_HOTKEYSCAN_FNF3,
1945 TP_ACPI_HOTKEYSCAN_FNF4,
1946 TP_ACPI_HOTKEYSCAN_FNF5,
1947 TP_ACPI_HOTKEYSCAN_FNF6,
1948 TP_ACPI_HOTKEYSCAN_FNF7,
1949 TP_ACPI_HOTKEYSCAN_FNF8,
1950 TP_ACPI_HOTKEYSCAN_FNF9,
1951 TP_ACPI_HOTKEYSCAN_FNF10,
1952 TP_ACPI_HOTKEYSCAN_FNF11,
1953 TP_ACPI_HOTKEYSCAN_FNF12,
1954 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1955 TP_ACPI_HOTKEYSCAN_FNINSERT,
1956 TP_ACPI_HOTKEYSCAN_FNDELETE,
1957 TP_ACPI_HOTKEYSCAN_FNHOME,
1958 TP_ACPI_HOTKEYSCAN_FNEND,
1959 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1960 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1961 TP_ACPI_HOTKEYSCAN_FNSPACE,
1962 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1963 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1964 TP_ACPI_HOTKEYSCAN_MUTE,
1965 TP_ACPI_HOTKEYSCAN_THINKPAD,
1968 enum { /* Keys/events available through NVRAM polling */
1969 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1970 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1973 enum { /* Positions of some of the keys in hotkey masks */
1974 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1975 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1976 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1977 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1978 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1979 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1980 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1981 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1982 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1983 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1984 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1987 enum { /* NVRAM to ACPI HKEY group map */
1988 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1989 TP_ACPI_HKEY_ZOOM_MASK |
1990 TP_ACPI_HKEY_DISPSWTCH_MASK |
1991 TP_ACPI_HKEY_HIBERNATE_MASK,
1992 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1993 TP_ACPI_HKEY_BRGHTDWN_MASK,
1994 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1995 TP_ACPI_HKEY_VOLDWN_MASK |
1996 TP_ACPI_HKEY_MUTE_MASK,
1999 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2000 struct tp_nvram_state {
2001 u16 thinkpad_toggle:1;
2002 u16 zoom_toggle:1;
2003 u16 display_toggle:1;
2004 u16 thinklight_toggle:1;
2005 u16 hibernate_toggle:1;
2006 u16 displayexp_toggle:1;
2007 u16 display_state:1;
2008 u16 brightness_toggle:1;
2009 u16 volume_toggle:1;
2010 u16 mute:1;
2012 u8 brightness_level;
2013 u8 volume_level;
2016 /* kthread for the hotkey poller */
2017 static struct task_struct *tpacpi_hotkey_task;
2019 /* Acquired while the poller kthread is running, use to sync start/stop */
2020 static struct mutex hotkey_thread_mutex;
2023 * Acquire mutex to write poller control variables as an
2024 * atomic block.
2026 * Increment hotkey_config_change when changing them if you
2027 * want the kthread to forget old state.
2029 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2031 static struct mutex hotkey_thread_data_mutex;
2032 static unsigned int hotkey_config_change;
2035 * hotkey poller control variables
2037 * Must be atomic or readers will also need to acquire mutex
2039 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2040 * should be used only when the changes need to be taken as
2041 * a block, OR when one needs to force the kthread to forget
2042 * old state.
2044 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2045 static unsigned int hotkey_poll_freq = 10; /* Hz */
2047 #define HOTKEY_CONFIG_CRITICAL_START \
2048 do { \
2049 mutex_lock(&hotkey_thread_data_mutex); \
2050 hotkey_config_change++; \
2051 } while (0);
2052 #define HOTKEY_CONFIG_CRITICAL_END \
2053 mutex_unlock(&hotkey_thread_data_mutex);
2055 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2057 #define hotkey_source_mask 0U
2058 #define HOTKEY_CONFIG_CRITICAL_START
2059 #define HOTKEY_CONFIG_CRITICAL_END
2061 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2063 static struct mutex hotkey_mutex;
2065 static enum { /* Reasons for waking up */
2066 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2067 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2068 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2069 } hotkey_wakeup_reason;
2071 static int hotkey_autosleep_ack;
2073 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2074 static u32 hotkey_all_mask; /* all events supported in fw */
2075 static u32 hotkey_reserved_mask; /* events better left disabled */
2076 static u32 hotkey_driver_mask; /* events needed by the driver */
2077 static u32 hotkey_user_mask; /* events visible to userspace */
2078 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2080 static unsigned int hotkey_report_mode;
2082 static u16 *hotkey_keycode_map;
2084 static struct attribute_set *hotkey_dev_attributes;
2086 static void tpacpi_driver_event(const unsigned int hkey_event);
2087 static void hotkey_driver_event(const unsigned int scancode);
2088 static void hotkey_poll_setup(const bool may_warn);
2090 /* HKEY.MHKG() return bits */
2091 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2093 static int hotkey_get_wlsw(void)
2095 int status;
2097 if (!tp_features.hotkey_wlsw)
2098 return -ENODEV;
2100 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2101 if (dbg_wlswemul)
2102 return (tpacpi_wlsw_emulstate) ?
2103 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2104 #endif
2106 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2107 return -EIO;
2109 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2112 static int hotkey_get_tablet_mode(int *status)
2114 int s;
2116 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2117 return -EIO;
2119 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2120 return 0;
2124 * Reads current event mask from firmware, and updates
2125 * hotkey_acpi_mask accordingly. Also resets any bits
2126 * from hotkey_user_mask that are unavailable to be
2127 * delivered (shadow requirement of the userspace ABI).
2129 * Call with hotkey_mutex held
2131 static int hotkey_mask_get(void)
2133 if (tp_features.hotkey_mask) {
2134 u32 m = 0;
2136 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2137 return -EIO;
2139 hotkey_acpi_mask = m;
2140 } else {
2141 /* no mask support doesn't mean no event support... */
2142 hotkey_acpi_mask = hotkey_all_mask;
2145 /* sync userspace-visible mask */
2146 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2148 return 0;
2151 void static hotkey_mask_warn_incomplete_mask(void)
2153 /* log only what the user can fix... */
2154 const u32 wantedmask = hotkey_driver_mask &
2155 ~(hotkey_acpi_mask | hotkey_source_mask) &
2156 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2158 if (wantedmask)
2159 printk(TPACPI_NOTICE
2160 "required events 0x%08x not enabled!\n",
2161 wantedmask);
2165 * Set the firmware mask when supported
2167 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2169 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2171 * Call with hotkey_mutex held
2173 static int hotkey_mask_set(u32 mask)
2175 int i;
2176 int rc = 0;
2178 const u32 fwmask = mask & ~hotkey_source_mask;
2180 if (tp_features.hotkey_mask) {
2181 for (i = 0; i < 32; i++) {
2182 if (!acpi_evalf(hkey_handle,
2183 NULL, "MHKM", "vdd", i + 1,
2184 !!(mask & (1 << i)))) {
2185 rc = -EIO;
2186 break;
2192 * We *must* make an inconditional call to hotkey_mask_get to
2193 * refresh hotkey_acpi_mask and update hotkey_user_mask
2195 * Take the opportunity to also log when we cannot _enable_
2196 * a given event.
2198 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2199 printk(TPACPI_NOTICE
2200 "asked for hotkey mask 0x%08x, but "
2201 "firmware forced it to 0x%08x\n",
2202 fwmask, hotkey_acpi_mask);
2205 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2206 hotkey_mask_warn_incomplete_mask();
2208 return rc;
2212 * Sets hotkey_user_mask and tries to set the firmware mask
2214 * Call with hotkey_mutex held
2216 static int hotkey_user_mask_set(const u32 mask)
2218 int rc;
2220 /* Give people a chance to notice they are doing something that
2221 * is bound to go boom on their users sooner or later */
2222 if (!tp_warned.hotkey_mask_ff &&
2223 (mask == 0xffff || mask == 0xffffff ||
2224 mask == 0xffffffff)) {
2225 tp_warned.hotkey_mask_ff = 1;
2226 printk(TPACPI_NOTICE
2227 "setting the hotkey mask to 0x%08x is likely "
2228 "not the best way to go about it\n", mask);
2229 printk(TPACPI_NOTICE
2230 "please consider using the driver defaults, "
2231 "and refer to up-to-date thinkpad-acpi "
2232 "documentation\n");
2235 /* Try to enable what the user asked for, plus whatever we need.
2236 * this syncs everything but won't enable bits in hotkey_user_mask */
2237 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2239 /* Enable the available bits in hotkey_user_mask */
2240 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2242 return rc;
2246 * Sets the driver hotkey mask.
2248 * Can be called even if the hotkey subdriver is inactive
2250 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2252 int rc;
2254 /* Do the right thing if hotkey_init has not been called yet */
2255 if (!tp_features.hotkey) {
2256 hotkey_driver_mask = mask;
2257 return 0;
2260 mutex_lock(&hotkey_mutex);
2262 HOTKEY_CONFIG_CRITICAL_START
2263 hotkey_driver_mask = mask;
2264 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2265 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2266 #endif
2267 HOTKEY_CONFIG_CRITICAL_END
2269 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2270 ~hotkey_source_mask);
2271 hotkey_poll_setup(true);
2273 mutex_unlock(&hotkey_mutex);
2275 return rc;
2278 static int hotkey_status_get(int *status)
2280 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2281 return -EIO;
2283 return 0;
2286 static int hotkey_status_set(bool enable)
2288 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2289 return -EIO;
2291 return 0;
2294 static void tpacpi_input_send_tabletsw(void)
2296 int state;
2298 if (tp_features.hotkey_tablet &&
2299 !hotkey_get_tablet_mode(&state)) {
2300 mutex_lock(&tpacpi_inputdev_send_mutex);
2302 input_report_switch(tpacpi_inputdev,
2303 SW_TABLET_MODE, !!state);
2304 input_sync(tpacpi_inputdev);
2306 mutex_unlock(&tpacpi_inputdev_send_mutex);
2310 /* Do NOT call without validating scancode first */
2311 static void tpacpi_input_send_key(const unsigned int scancode)
2313 const unsigned int keycode = hotkey_keycode_map[scancode];
2315 if (keycode != KEY_RESERVED) {
2316 mutex_lock(&tpacpi_inputdev_send_mutex);
2318 input_report_key(tpacpi_inputdev, keycode, 1);
2319 if (keycode == KEY_UNKNOWN)
2320 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2321 scancode);
2322 input_sync(tpacpi_inputdev);
2324 input_report_key(tpacpi_inputdev, keycode, 0);
2325 if (keycode == KEY_UNKNOWN)
2326 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2327 scancode);
2328 input_sync(tpacpi_inputdev);
2330 mutex_unlock(&tpacpi_inputdev_send_mutex);
2334 /* Do NOT call without validating scancode first */
2335 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2337 hotkey_driver_event(scancode);
2338 if (hotkey_user_mask & (1 << scancode))
2339 tpacpi_input_send_key(scancode);
2342 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2343 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2345 /* Do NOT call without validating scancode first */
2346 static void tpacpi_hotkey_send_key(unsigned int scancode)
2348 tpacpi_input_send_key_masked(scancode);
2349 if (hotkey_report_mode < 2) {
2350 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2351 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2355 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2357 u8 d;
2359 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2360 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2361 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2362 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2363 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2364 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2366 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2367 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2368 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2370 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2371 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2372 n->displayexp_toggle =
2373 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2375 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2376 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2377 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2378 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2379 n->brightness_toggle =
2380 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2382 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2383 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2384 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2385 >> TP_NVRAM_POS_LEVEL_VOLUME;
2386 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2387 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2391 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2392 struct tp_nvram_state *newn,
2393 const u32 event_mask)
2396 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2397 do { \
2398 if ((event_mask & (1 << __scancode)) && \
2399 oldn->__member != newn->__member) \
2400 tpacpi_hotkey_send_key(__scancode); \
2401 } while (0)
2403 #define TPACPI_MAY_SEND_KEY(__scancode) \
2404 do { \
2405 if (event_mask & (1 << __scancode)) \
2406 tpacpi_hotkey_send_key(__scancode); \
2407 } while (0)
2409 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2410 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2411 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2412 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2414 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2416 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2418 /* handle volume */
2419 if (oldn->volume_toggle != newn->volume_toggle) {
2420 if (oldn->mute != newn->mute) {
2421 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2423 if (oldn->volume_level > newn->volume_level) {
2424 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2425 } else if (oldn->volume_level < newn->volume_level) {
2426 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2427 } else if (oldn->mute == newn->mute) {
2428 /* repeated key presses that didn't change state */
2429 if (newn->mute) {
2430 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2431 } else if (newn->volume_level != 0) {
2432 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2433 } else {
2434 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2439 /* handle brightness */
2440 if (oldn->brightness_toggle != newn->brightness_toggle) {
2441 if (oldn->brightness_level < newn->brightness_level) {
2442 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2443 } else if (oldn->brightness_level > newn->brightness_level) {
2444 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2445 } else {
2446 /* repeated key presses that didn't change state */
2447 if (newn->brightness_level != 0) {
2448 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2449 } else {
2450 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2455 #undef TPACPI_COMPARE_KEY
2456 #undef TPACPI_MAY_SEND_KEY
2460 * Polling driver
2462 * We track all events in hotkey_source_mask all the time, since
2463 * most of them are edge-based. We only issue those requested by
2464 * hotkey_user_mask or hotkey_driver_mask, though.
2466 static int hotkey_kthread(void *data)
2468 struct tp_nvram_state s[2];
2469 u32 poll_mask, event_mask;
2470 unsigned int si, so;
2471 unsigned long t;
2472 unsigned int change_detector, must_reset;
2473 unsigned int poll_freq;
2475 mutex_lock(&hotkey_thread_mutex);
2477 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2478 goto exit;
2480 set_freezable();
2482 so = 0;
2483 si = 1;
2484 t = 0;
2486 /* Initial state for compares */
2487 mutex_lock(&hotkey_thread_data_mutex);
2488 change_detector = hotkey_config_change;
2489 poll_mask = hotkey_source_mask;
2490 event_mask = hotkey_source_mask &
2491 (hotkey_driver_mask | hotkey_user_mask);
2492 poll_freq = hotkey_poll_freq;
2493 mutex_unlock(&hotkey_thread_data_mutex);
2494 hotkey_read_nvram(&s[so], poll_mask);
2496 while (!kthread_should_stop()) {
2497 if (t == 0) {
2498 if (likely(poll_freq))
2499 t = 1000/poll_freq;
2500 else
2501 t = 100; /* should never happen... */
2503 t = msleep_interruptible(t);
2504 if (unlikely(kthread_should_stop()))
2505 break;
2506 must_reset = try_to_freeze();
2507 if (t > 0 && !must_reset)
2508 continue;
2510 mutex_lock(&hotkey_thread_data_mutex);
2511 if (must_reset || hotkey_config_change != change_detector) {
2512 /* forget old state on thaw or config change */
2513 si = so;
2514 t = 0;
2515 change_detector = hotkey_config_change;
2517 poll_mask = hotkey_source_mask;
2518 event_mask = hotkey_source_mask &
2519 (hotkey_driver_mask | hotkey_user_mask);
2520 poll_freq = hotkey_poll_freq;
2521 mutex_unlock(&hotkey_thread_data_mutex);
2523 if (likely(poll_mask)) {
2524 hotkey_read_nvram(&s[si], poll_mask);
2525 if (likely(si != so)) {
2526 hotkey_compare_and_issue_event(&s[so], &s[si],
2527 event_mask);
2531 so = si;
2532 si ^= 1;
2535 exit:
2536 mutex_unlock(&hotkey_thread_mutex);
2537 return 0;
2540 /* call with hotkey_mutex held */
2541 static void hotkey_poll_stop_sync(void)
2543 if (tpacpi_hotkey_task) {
2544 if (frozen(tpacpi_hotkey_task) ||
2545 freezing(tpacpi_hotkey_task))
2546 thaw_process(tpacpi_hotkey_task);
2548 kthread_stop(tpacpi_hotkey_task);
2549 tpacpi_hotkey_task = NULL;
2550 mutex_lock(&hotkey_thread_mutex);
2551 /* at this point, the thread did exit */
2552 mutex_unlock(&hotkey_thread_mutex);
2556 /* call with hotkey_mutex held */
2557 static void hotkey_poll_setup(const bool may_warn)
2559 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2560 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2562 if (hotkey_poll_freq > 0 &&
2563 (poll_driver_mask ||
2564 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2565 if (!tpacpi_hotkey_task) {
2566 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2567 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2568 if (IS_ERR(tpacpi_hotkey_task)) {
2569 tpacpi_hotkey_task = NULL;
2570 printk(TPACPI_ERR
2571 "could not create kernel thread "
2572 "for hotkey polling\n");
2575 } else {
2576 hotkey_poll_stop_sync();
2577 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2578 hotkey_poll_freq == 0) {
2579 printk(TPACPI_NOTICE
2580 "hot keys 0x%08x and/or events 0x%08x "
2581 "require polling, which is currently "
2582 "disabled\n",
2583 poll_user_mask, poll_driver_mask);
2588 static void hotkey_poll_setup_safe(const bool may_warn)
2590 mutex_lock(&hotkey_mutex);
2591 hotkey_poll_setup(may_warn);
2592 mutex_unlock(&hotkey_mutex);
2595 /* call with hotkey_mutex held */
2596 static void hotkey_poll_set_freq(unsigned int freq)
2598 if (!freq)
2599 hotkey_poll_stop_sync();
2601 hotkey_poll_freq = freq;
2604 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2606 static void hotkey_poll_setup(const bool __unused)
2610 static void hotkey_poll_setup_safe(const bool __unused)
2614 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2616 static int hotkey_inputdev_open(struct input_dev *dev)
2618 switch (tpacpi_lifecycle) {
2619 case TPACPI_LIFE_INIT:
2620 case TPACPI_LIFE_RUNNING:
2621 hotkey_poll_setup_safe(false);
2622 return 0;
2623 case TPACPI_LIFE_EXITING:
2624 return -EBUSY;
2627 /* Should only happen if tpacpi_lifecycle is corrupt */
2628 BUG();
2629 return -EBUSY;
2632 static void hotkey_inputdev_close(struct input_dev *dev)
2634 /* disable hotkey polling when possible */
2635 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2636 !(hotkey_source_mask & hotkey_driver_mask))
2637 hotkey_poll_setup_safe(false);
2640 /* sysfs hotkey enable ------------------------------------------------- */
2641 static ssize_t hotkey_enable_show(struct device *dev,
2642 struct device_attribute *attr,
2643 char *buf)
2645 int res, status;
2647 printk_deprecated_attribute("hotkey_enable",
2648 "Hotkey reporting is always enabled");
2650 res = hotkey_status_get(&status);
2651 if (res)
2652 return res;
2654 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2657 static ssize_t hotkey_enable_store(struct device *dev,
2658 struct device_attribute *attr,
2659 const char *buf, size_t count)
2661 unsigned long t;
2663 printk_deprecated_attribute("hotkey_enable",
2664 "Hotkeys can be disabled through hotkey_mask");
2666 if (parse_strtoul(buf, 1, &t))
2667 return -EINVAL;
2669 if (t == 0)
2670 return -EPERM;
2672 return count;
2675 static struct device_attribute dev_attr_hotkey_enable =
2676 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2677 hotkey_enable_show, hotkey_enable_store);
2679 /* sysfs hotkey mask --------------------------------------------------- */
2680 static ssize_t hotkey_mask_show(struct device *dev,
2681 struct device_attribute *attr,
2682 char *buf)
2684 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2687 static ssize_t hotkey_mask_store(struct device *dev,
2688 struct device_attribute *attr,
2689 const char *buf, size_t count)
2691 unsigned long t;
2692 int res;
2694 if (parse_strtoul(buf, 0xffffffffUL, &t))
2695 return -EINVAL;
2697 if (mutex_lock_killable(&hotkey_mutex))
2698 return -ERESTARTSYS;
2700 res = hotkey_user_mask_set(t);
2702 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2703 hotkey_poll_setup(true);
2704 #endif
2706 mutex_unlock(&hotkey_mutex);
2708 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2710 return (res) ? res : count;
2713 static struct device_attribute dev_attr_hotkey_mask =
2714 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2715 hotkey_mask_show, hotkey_mask_store);
2717 /* sysfs hotkey bios_enabled ------------------------------------------- */
2718 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2719 struct device_attribute *attr,
2720 char *buf)
2722 return sprintf(buf, "0\n");
2725 static struct device_attribute dev_attr_hotkey_bios_enabled =
2726 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2728 /* sysfs hotkey bios_mask ---------------------------------------------- */
2729 static ssize_t hotkey_bios_mask_show(struct device *dev,
2730 struct device_attribute *attr,
2731 char *buf)
2733 printk_deprecated_attribute("hotkey_bios_mask",
2734 "This attribute is useless.");
2735 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2738 static struct device_attribute dev_attr_hotkey_bios_mask =
2739 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2741 /* sysfs hotkey all_mask ----------------------------------------------- */
2742 static ssize_t hotkey_all_mask_show(struct device *dev,
2743 struct device_attribute *attr,
2744 char *buf)
2746 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2747 hotkey_all_mask | hotkey_source_mask);
2750 static struct device_attribute dev_attr_hotkey_all_mask =
2751 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2753 /* sysfs hotkey recommended_mask --------------------------------------- */
2754 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2755 struct device_attribute *attr,
2756 char *buf)
2758 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2759 (hotkey_all_mask | hotkey_source_mask)
2760 & ~hotkey_reserved_mask);
2763 static struct device_attribute dev_attr_hotkey_recommended_mask =
2764 __ATTR(hotkey_recommended_mask, S_IRUGO,
2765 hotkey_recommended_mask_show, NULL);
2767 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2769 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2770 static ssize_t hotkey_source_mask_show(struct device *dev,
2771 struct device_attribute *attr,
2772 char *buf)
2774 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2777 static ssize_t hotkey_source_mask_store(struct device *dev,
2778 struct device_attribute *attr,
2779 const char *buf, size_t count)
2781 unsigned long t;
2782 u32 r_ev;
2783 int rc;
2785 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2786 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2787 return -EINVAL;
2789 if (mutex_lock_killable(&hotkey_mutex))
2790 return -ERESTARTSYS;
2792 HOTKEY_CONFIG_CRITICAL_START
2793 hotkey_source_mask = t;
2794 HOTKEY_CONFIG_CRITICAL_END
2796 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2797 ~hotkey_source_mask);
2798 hotkey_poll_setup(true);
2800 /* check if events needed by the driver got disabled */
2801 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2802 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2804 mutex_unlock(&hotkey_mutex);
2806 if (rc < 0)
2807 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2808 "firmware event mask!\n");
2810 if (r_ev)
2811 printk(TPACPI_NOTICE "hotkey_source_mask: "
2812 "some important events were disabled: "
2813 "0x%04x\n", r_ev);
2815 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2817 return (rc < 0) ? rc : count;
2820 static struct device_attribute dev_attr_hotkey_source_mask =
2821 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2822 hotkey_source_mask_show, hotkey_source_mask_store);
2824 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2825 static ssize_t hotkey_poll_freq_show(struct device *dev,
2826 struct device_attribute *attr,
2827 char *buf)
2829 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2832 static ssize_t hotkey_poll_freq_store(struct device *dev,
2833 struct device_attribute *attr,
2834 const char *buf, size_t count)
2836 unsigned long t;
2838 if (parse_strtoul(buf, 25, &t))
2839 return -EINVAL;
2841 if (mutex_lock_killable(&hotkey_mutex))
2842 return -ERESTARTSYS;
2844 hotkey_poll_set_freq(t);
2845 hotkey_poll_setup(true);
2847 mutex_unlock(&hotkey_mutex);
2849 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2851 return count;
2854 static struct device_attribute dev_attr_hotkey_poll_freq =
2855 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2856 hotkey_poll_freq_show, hotkey_poll_freq_store);
2858 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2860 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2861 static ssize_t hotkey_radio_sw_show(struct device *dev,
2862 struct device_attribute *attr,
2863 char *buf)
2865 int res;
2866 res = hotkey_get_wlsw();
2867 if (res < 0)
2868 return res;
2870 /* Opportunistic update */
2871 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2873 return snprintf(buf, PAGE_SIZE, "%d\n",
2874 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2877 static struct device_attribute dev_attr_hotkey_radio_sw =
2878 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2880 static void hotkey_radio_sw_notify_change(void)
2882 if (tp_features.hotkey_wlsw)
2883 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2884 "hotkey_radio_sw");
2887 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2888 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2889 struct device_attribute *attr,
2890 char *buf)
2892 int res, s;
2893 res = hotkey_get_tablet_mode(&s);
2894 if (res < 0)
2895 return res;
2897 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2900 static struct device_attribute dev_attr_hotkey_tablet_mode =
2901 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2903 static void hotkey_tablet_mode_notify_change(void)
2905 if (tp_features.hotkey_tablet)
2906 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2907 "hotkey_tablet_mode");
2910 /* sysfs hotkey report_mode -------------------------------------------- */
2911 static ssize_t hotkey_report_mode_show(struct device *dev,
2912 struct device_attribute *attr,
2913 char *buf)
2915 return snprintf(buf, PAGE_SIZE, "%d\n",
2916 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2919 static struct device_attribute dev_attr_hotkey_report_mode =
2920 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2922 /* sysfs wakeup reason (pollable) -------------------------------------- */
2923 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2924 struct device_attribute *attr,
2925 char *buf)
2927 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2930 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2931 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2933 static void hotkey_wakeup_reason_notify_change(void)
2935 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2936 "wakeup_reason");
2939 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2940 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2941 struct device_attribute *attr,
2942 char *buf)
2944 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2947 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2948 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2949 hotkey_wakeup_hotunplug_complete_show, NULL);
2951 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2953 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2954 "wakeup_hotunplug_complete");
2957 /* --------------------------------------------------------------------- */
2959 static struct attribute *hotkey_attributes[] __initdata = {
2960 &dev_attr_hotkey_enable.attr,
2961 &dev_attr_hotkey_bios_enabled.attr,
2962 &dev_attr_hotkey_bios_mask.attr,
2963 &dev_attr_hotkey_report_mode.attr,
2964 &dev_attr_hotkey_wakeup_reason.attr,
2965 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2966 &dev_attr_hotkey_mask.attr,
2967 &dev_attr_hotkey_all_mask.attr,
2968 &dev_attr_hotkey_recommended_mask.attr,
2969 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2970 &dev_attr_hotkey_source_mask.attr,
2971 &dev_attr_hotkey_poll_freq.attr,
2972 #endif
2976 * Sync both the hw and sw blocking state of all switches
2978 static void tpacpi_send_radiosw_update(void)
2980 int wlsw;
2983 * We must sync all rfkill controllers *before* issuing any
2984 * rfkill input events, or we will race the rfkill core input
2985 * handler.
2987 * tpacpi_inputdev_send_mutex works as a syncronization point
2988 * for the above.
2990 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2993 wlsw = hotkey_get_wlsw();
2995 /* Sync hw blocking state first if it is hw-blocked */
2996 if (wlsw == TPACPI_RFK_RADIO_OFF)
2997 tpacpi_rfk_update_hwblock_state(true);
2999 /* Sync sw blocking state */
3000 tpacpi_rfk_update_swstate_all();
3002 /* Sync hw blocking state last if it is hw-unblocked */
3003 if (wlsw == TPACPI_RFK_RADIO_ON)
3004 tpacpi_rfk_update_hwblock_state(false);
3006 /* Issue rfkill input event for WLSW switch */
3007 if (!(wlsw < 0)) {
3008 mutex_lock(&tpacpi_inputdev_send_mutex);
3010 input_report_switch(tpacpi_inputdev,
3011 SW_RFKILL_ALL, (wlsw > 0));
3012 input_sync(tpacpi_inputdev);
3014 mutex_unlock(&tpacpi_inputdev_send_mutex);
3018 * this can be unconditional, as we will poll state again
3019 * if userspace uses the notify to read data
3021 hotkey_radio_sw_notify_change();
3024 static void hotkey_exit(void)
3026 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3027 mutex_lock(&hotkey_mutex);
3028 hotkey_poll_stop_sync();
3029 mutex_unlock(&hotkey_mutex);
3030 #endif
3032 if (hotkey_dev_attributes)
3033 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3035 kfree(hotkey_keycode_map);
3037 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3038 "restoring original HKEY status and mask\n");
3039 /* yes, there is a bitwise or below, we want the
3040 * functions to be called even if one of them fail */
3041 if (((tp_features.hotkey_mask &&
3042 hotkey_mask_set(hotkey_orig_mask)) |
3043 hotkey_status_set(false)) != 0)
3044 printk(TPACPI_ERR
3045 "failed to restore hot key mask "
3046 "to BIOS defaults\n");
3049 static void __init hotkey_unmap(const unsigned int scancode)
3051 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3052 clear_bit(hotkey_keycode_map[scancode],
3053 tpacpi_inputdev->keybit);
3054 hotkey_keycode_map[scancode] = KEY_RESERVED;
3059 * HKEY quirks:
3060 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3063 #define TPACPI_HK_Q_INIMASK 0x0001
3065 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3066 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3067 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3068 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3069 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3070 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3071 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3072 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3073 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3074 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3075 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3076 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3077 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3078 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3079 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3080 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3081 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3082 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3083 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3084 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3087 static int __init hotkey_init(struct ibm_init_struct *iibm)
3089 /* Requirements for changing the default keymaps:
3091 * 1. Many of the keys are mapped to KEY_RESERVED for very
3092 * good reasons. Do not change them unless you have deep
3093 * knowledge on the IBM and Lenovo ThinkPad firmware for
3094 * the various ThinkPad models. The driver behaves
3095 * differently for KEY_RESERVED: such keys have their
3096 * hot key mask *unset* in mask_recommended, and also
3097 * in the initial hot key mask programmed into the
3098 * firmware at driver load time, which means the firm-
3099 * ware may react very differently if you change them to
3100 * something else;
3102 * 2. You must be subscribed to the linux-thinkpad and
3103 * ibm-acpi-devel mailing lists, and you should read the
3104 * list archives since 2007 if you want to change the
3105 * keymaps. This requirement exists so that you will
3106 * know the past history of problems with the thinkpad-
3107 * acpi driver keymaps, and also that you will be
3108 * listening to any bug reports;
3110 * 3. Do not send thinkpad-acpi specific patches directly to
3111 * for merging, *ever*. Send them to the linux-acpi
3112 * mailinglist for comments. Merging is to be done only
3113 * through acpi-test and the ACPI maintainer.
3115 * If the above is too much to ask, don't change the keymap.
3116 * Ask the thinkpad-acpi maintainer to do it, instead.
3118 static u16 ibm_keycode_map[] __initdata = {
3119 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3120 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3121 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3122 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3124 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3125 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3126 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3127 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3129 /* brightness: firmware always reacts to them */
3130 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3131 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3133 /* Thinklight: firmware always react to it */
3134 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3136 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3137 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3139 /* Volume: firmware always react to it and reprograms
3140 * the built-in *extra* mixer. Never map it to control
3141 * another mixer by default. */
3142 KEY_RESERVED, /* 0x14: VOLUME UP */
3143 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3144 KEY_RESERVED, /* 0x16: MUTE */
3146 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3148 /* (assignments unknown, please report if found) */
3149 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3150 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3152 static u16 lenovo_keycode_map[] __initdata = {
3153 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3154 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3155 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3156 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3158 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3159 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3160 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3161 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3163 /* These should be enabled --only-- when ACPI video
3164 * is disabled (i.e. in "vendor" mode), and are handled
3165 * in a special way by the init code */
3166 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3167 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3169 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3171 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3172 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3174 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3175 * react to it and reprograms the built-in *extra* mixer.
3176 * Never map it to control another mixer by default.
3178 * T60?, T61, R60?, R61: firmware and EC tries to send
3179 * these over the regular keyboard, so these are no-ops,
3180 * but there are still weird bugs re. MUTE, so do not
3181 * change unless you get test reports from all Lenovo
3182 * models. May cause the BIOS to interfere with the
3183 * HDA mixer.
3185 KEY_RESERVED, /* 0x14: VOLUME UP */
3186 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3187 KEY_RESERVED, /* 0x16: MUTE */
3189 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3191 /* (assignments unknown, please report if found) */
3192 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3193 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3196 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3197 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3198 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3200 int res, i;
3201 int status;
3202 int hkeyv;
3203 bool radiosw_state = false;
3204 bool tabletsw_state = false;
3206 unsigned long quirks;
3208 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3209 "initializing hotkey subdriver\n");
3211 BUG_ON(!tpacpi_inputdev);
3212 BUG_ON(tpacpi_inputdev->open != NULL ||
3213 tpacpi_inputdev->close != NULL);
3215 TPACPI_ACPIHANDLE_INIT(hkey);
3216 mutex_init(&hotkey_mutex);
3218 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3219 mutex_init(&hotkey_thread_mutex);
3220 mutex_init(&hotkey_thread_data_mutex);
3221 #endif
3223 /* hotkey not supported on 570 */
3224 tp_features.hotkey = hkey_handle != NULL;
3226 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3227 "hotkeys are %s\n",
3228 str_supported(tp_features.hotkey));
3230 if (!tp_features.hotkey)
3231 return 1;
3233 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3234 ARRAY_SIZE(tpacpi_hotkey_qtable));
3236 tpacpi_disable_brightness_delay();
3238 /* MUST have enough space for all attributes to be added to
3239 * hotkey_dev_attributes */
3240 hotkey_dev_attributes = create_attr_set(
3241 ARRAY_SIZE(hotkey_attributes) + 2,
3242 NULL);
3243 if (!hotkey_dev_attributes)
3244 return -ENOMEM;
3245 res = add_many_to_attr_set(hotkey_dev_attributes,
3246 hotkey_attributes,
3247 ARRAY_SIZE(hotkey_attributes));
3248 if (res)
3249 goto err_exit;
3251 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3252 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3253 for HKEY interface version 0x100 */
3254 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3255 if ((hkeyv >> 8) != 1) {
3256 printk(TPACPI_ERR "unknown version of the "
3257 "HKEY interface: 0x%x\n", hkeyv);
3258 printk(TPACPI_ERR "please report this to %s\n",
3259 TPACPI_MAIL);
3260 } else {
3262 * MHKV 0x100 in A31, R40, R40e,
3263 * T4x, X31, and later
3265 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3266 "firmware HKEY interface version: 0x%x\n",
3267 hkeyv);
3269 /* Paranoia check AND init hotkey_all_mask */
3270 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3271 "MHKA", "qd")) {
3272 printk(TPACPI_ERR
3273 "missing MHKA handler, "
3274 "please report this to %s\n",
3275 TPACPI_MAIL);
3276 /* Fallback: pre-init for FN+F3,F4,F12 */
3277 hotkey_all_mask = 0x080cU;
3278 } else {
3279 tp_features.hotkey_mask = 1;
3284 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3285 "hotkey masks are %s\n",
3286 str_supported(tp_features.hotkey_mask));
3288 /* Init hotkey_all_mask if not initialized yet */
3289 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3290 (quirks & TPACPI_HK_Q_INIMASK))
3291 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3293 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3294 if (tp_features.hotkey_mask) {
3295 /* hotkey_source_mask *must* be zero for
3296 * the first hotkey_mask_get to return hotkey_orig_mask */
3297 res = hotkey_mask_get();
3298 if (res)
3299 goto err_exit;
3301 hotkey_orig_mask = hotkey_acpi_mask;
3302 } else {
3303 hotkey_orig_mask = hotkey_all_mask;
3304 hotkey_acpi_mask = hotkey_all_mask;
3307 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3308 if (dbg_wlswemul) {
3309 tp_features.hotkey_wlsw = 1;
3310 radiosw_state = !!tpacpi_wlsw_emulstate;
3311 printk(TPACPI_INFO
3312 "radio switch emulation enabled\n");
3313 } else
3314 #endif
3315 /* Not all thinkpads have a hardware radio switch */
3316 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3317 tp_features.hotkey_wlsw = 1;
3318 radiosw_state = !!status;
3319 printk(TPACPI_INFO
3320 "radio switch found; radios are %s\n",
3321 enabled(status, 0));
3323 if (tp_features.hotkey_wlsw)
3324 res = add_to_attr_set(hotkey_dev_attributes,
3325 &dev_attr_hotkey_radio_sw.attr);
3327 /* For X41t, X60t, X61t Tablets... */
3328 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3329 tp_features.hotkey_tablet = 1;
3330 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3331 printk(TPACPI_INFO
3332 "possible tablet mode switch found; "
3333 "ThinkPad in %s mode\n",
3334 (tabletsw_state) ? "tablet" : "laptop");
3335 res = add_to_attr_set(hotkey_dev_attributes,
3336 &dev_attr_hotkey_tablet_mode.attr);
3339 if (!res)
3340 res = register_attr_set_with_sysfs(
3341 hotkey_dev_attributes,
3342 &tpacpi_pdev->dev.kobj);
3343 if (res)
3344 goto err_exit;
3346 /* Set up key map */
3348 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3349 GFP_KERNEL);
3350 if (!hotkey_keycode_map) {
3351 printk(TPACPI_ERR
3352 "failed to allocate memory for key map\n");
3353 res = -ENOMEM;
3354 goto err_exit;
3357 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
3358 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3359 "using Lenovo default hot key map\n");
3360 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3361 TPACPI_HOTKEY_MAP_SIZE);
3362 } else {
3363 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3364 "using IBM default hot key map\n");
3365 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3366 TPACPI_HOTKEY_MAP_SIZE);
3369 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3370 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3371 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3372 tpacpi_inputdev->keycode = hotkey_keycode_map;
3373 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3374 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3375 input_set_capability(tpacpi_inputdev, EV_KEY,
3376 hotkey_keycode_map[i]);
3377 } else {
3378 if (i < sizeof(hotkey_reserved_mask)*8)
3379 hotkey_reserved_mask |= 1 << i;
3383 if (tp_features.hotkey_wlsw) {
3384 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3385 input_report_switch(tpacpi_inputdev,
3386 SW_RFKILL_ALL, radiosw_state);
3388 if (tp_features.hotkey_tablet) {
3389 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3390 input_report_switch(tpacpi_inputdev,
3391 SW_TABLET_MODE, tabletsw_state);
3394 /* Do not issue duplicate brightness change events to
3395 * userspace */
3396 if (!tp_features.bright_acpimode)
3397 /* update bright_acpimode... */
3398 tpacpi_check_std_acpi_brightness_support();
3400 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3401 printk(TPACPI_INFO
3402 "This ThinkPad has standard ACPI backlight "
3403 "brightness control, supported by the ACPI "
3404 "video driver\n");
3405 printk(TPACPI_NOTICE
3406 "Disabling thinkpad-acpi brightness events "
3407 "by default...\n");
3409 /* Disable brightness up/down on Lenovo thinkpads when
3410 * ACPI is handling them, otherwise it is plain impossible
3411 * for userspace to do something even remotely sane */
3412 hotkey_reserved_mask |=
3413 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3414 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3415 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3416 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3419 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3420 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3421 & ~hotkey_all_mask
3422 & ~hotkey_reserved_mask;
3424 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3425 "hotkey source mask 0x%08x, polling freq %u\n",
3426 hotkey_source_mask, hotkey_poll_freq);
3427 #endif
3429 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3430 "enabling firmware HKEY event interface...\n");
3431 res = hotkey_status_set(true);
3432 if (res) {
3433 hotkey_exit();
3434 return res;
3436 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3437 | hotkey_driver_mask)
3438 & ~hotkey_source_mask);
3439 if (res < 0 && res != -ENXIO) {
3440 hotkey_exit();
3441 return res;
3443 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3444 & ~hotkey_reserved_mask;
3445 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3446 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3447 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3449 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3450 "legacy ibm/hotkey event reporting over procfs %s\n",
3451 (hotkey_report_mode < 2) ?
3452 "enabled" : "disabled");
3454 tpacpi_inputdev->open = &hotkey_inputdev_open;
3455 tpacpi_inputdev->close = &hotkey_inputdev_close;
3457 hotkey_poll_setup_safe(true);
3459 return 0;
3461 err_exit:
3462 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3463 hotkey_dev_attributes = NULL;
3465 return (res < 0)? res : 1;
3468 static bool hotkey_notify_hotkey(const u32 hkey,
3469 bool *send_acpi_ev,
3470 bool *ignore_acpi_ev)
3472 /* 0x1000-0x1FFF: key presses */
3473 unsigned int scancode = hkey & 0xfff;
3474 *send_acpi_ev = true;
3475 *ignore_acpi_ev = false;
3477 if (scancode > 0 && scancode < 0x21) {
3478 scancode--;
3479 if (!(hotkey_source_mask & (1 << scancode))) {
3480 tpacpi_input_send_key_masked(scancode);
3481 *send_acpi_ev = false;
3482 } else {
3483 *ignore_acpi_ev = true;
3485 return true;
3487 return false;
3490 static bool hotkey_notify_wakeup(const u32 hkey,
3491 bool *send_acpi_ev,
3492 bool *ignore_acpi_ev)
3494 /* 0x2000-0x2FFF: Wakeup reason */
3495 *send_acpi_ev = true;
3496 *ignore_acpi_ev = false;
3498 switch (hkey) {
3499 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3500 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3501 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3502 *ignore_acpi_ev = true;
3503 break;
3505 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3506 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3507 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3508 *ignore_acpi_ev = true;
3509 break;
3511 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3512 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3513 printk(TPACPI_ALERT
3514 "EMERGENCY WAKEUP: battery almost empty\n");
3515 /* how to auto-heal: */
3516 /* 2313: woke up from S3, go to S4/S5 */
3517 /* 2413: woke up from S4, go to S5 */
3518 break;
3520 default:
3521 return false;
3524 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3525 printk(TPACPI_INFO
3526 "woke up due to a hot-unplug "
3527 "request...\n");
3528 hotkey_wakeup_reason_notify_change();
3530 return true;
3533 static bool hotkey_notify_usrevent(const u32 hkey,
3534 bool *send_acpi_ev,
3535 bool *ignore_acpi_ev)
3537 /* 0x5000-0x5FFF: human interface helpers */
3538 *send_acpi_ev = true;
3539 *ignore_acpi_ev = false;
3541 switch (hkey) {
3542 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3543 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3544 return true;
3546 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3547 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3548 tpacpi_input_send_tabletsw();
3549 hotkey_tablet_mode_notify_change();
3550 *send_acpi_ev = false;
3551 return true;
3553 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3554 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3555 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3556 /* do not propagate these events */
3557 *ignore_acpi_ev = true;
3558 return true;
3560 default:
3561 return false;
3565 static void thermal_dump_all_sensors(void);
3567 static bool hotkey_notify_thermal(const u32 hkey,
3568 bool *send_acpi_ev,
3569 bool *ignore_acpi_ev)
3571 bool known = true;
3573 /* 0x6000-0x6FFF: thermal alarms */
3574 *send_acpi_ev = true;
3575 *ignore_acpi_ev = false;
3577 switch (hkey) {
3578 case TP_HKEY_EV_THM_TABLE_CHANGED:
3579 printk(TPACPI_INFO
3580 "EC reports that Thermal Table has changed\n");
3581 /* recommended action: do nothing, we don't have
3582 * Lenovo ATM information */
3583 return true;
3584 case TP_HKEY_EV_ALARM_BAT_HOT:
3585 printk(TPACPI_CRIT
3586 "THERMAL ALARM: battery is too hot!\n");
3587 /* recommended action: warn user through gui */
3588 break;
3589 case TP_HKEY_EV_ALARM_BAT_XHOT:
3590 printk(TPACPI_ALERT
3591 "THERMAL EMERGENCY: battery is extremely hot!\n");
3592 /* recommended action: immediate sleep/hibernate */
3593 break;
3594 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3595 printk(TPACPI_CRIT
3596 "THERMAL ALARM: "
3597 "a sensor reports something is too hot!\n");
3598 /* recommended action: warn user through gui, that */
3599 /* some internal component is too hot */
3600 break;
3601 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3602 printk(TPACPI_ALERT
3603 "THERMAL EMERGENCY: "
3604 "a sensor reports something is extremely hot!\n");
3605 /* recommended action: immediate sleep/hibernate */
3606 break;
3607 default:
3608 printk(TPACPI_ALERT
3609 "THERMAL ALERT: unknown thermal alarm received\n");
3610 known = false;
3613 thermal_dump_all_sensors();
3615 return known;
3618 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3620 u32 hkey;
3621 bool send_acpi_ev;
3622 bool ignore_acpi_ev;
3623 bool known_ev;
3625 if (event != 0x80) {
3626 printk(TPACPI_ERR
3627 "unknown HKEY notification event %d\n", event);
3628 /* forward it to userspace, maybe it knows how to handle it */
3629 acpi_bus_generate_netlink_event(
3630 ibm->acpi->device->pnp.device_class,
3631 dev_name(&ibm->acpi->device->dev),
3632 event, 0);
3633 return;
3636 while (1) {
3637 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3638 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3639 return;
3642 if (hkey == 0) {
3643 /* queue empty */
3644 return;
3647 send_acpi_ev = true;
3648 ignore_acpi_ev = false;
3650 switch (hkey >> 12) {
3651 case 1:
3652 /* 0x1000-0x1FFF: key presses */
3653 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3654 &ignore_acpi_ev);
3655 break;
3656 case 2:
3657 /* 0x2000-0x2FFF: Wakeup reason */
3658 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3659 &ignore_acpi_ev);
3660 break;
3661 case 3:
3662 /* 0x3000-0x3FFF: bay-related wakeups */
3663 switch (hkey) {
3664 case TP_HKEY_EV_BAYEJ_ACK:
3665 hotkey_autosleep_ack = 1;
3666 printk(TPACPI_INFO
3667 "bay ejected\n");
3668 hotkey_wakeup_hotunplug_complete_notify_change();
3669 known_ev = true;
3670 break;
3671 case TP_HKEY_EV_OPTDRV_EJ:
3672 /* FIXME: kick libata if SATA link offline */
3673 known_ev = true;
3674 break;
3675 default:
3676 known_ev = false;
3678 break;
3679 case 4:
3680 /* 0x4000-0x4FFF: dock-related wakeups */
3681 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3682 hotkey_autosleep_ack = 1;
3683 printk(TPACPI_INFO
3684 "undocked\n");
3685 hotkey_wakeup_hotunplug_complete_notify_change();
3686 known_ev = true;
3687 } else {
3688 known_ev = false;
3690 break;
3691 case 5:
3692 /* 0x5000-0x5FFF: human interface helpers */
3693 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3694 &ignore_acpi_ev);
3695 break;
3696 case 6:
3697 /* 0x6000-0x6FFF: thermal alarms */
3698 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3699 &ignore_acpi_ev);
3700 break;
3701 case 7:
3702 /* 0x7000-0x7FFF: misc */
3703 if (tp_features.hotkey_wlsw &&
3704 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3705 tpacpi_send_radiosw_update();
3706 send_acpi_ev = 0;
3707 known_ev = true;
3708 break;
3710 /* fallthrough to default */
3711 default:
3712 known_ev = false;
3714 if (!known_ev) {
3715 printk(TPACPI_NOTICE
3716 "unhandled HKEY event 0x%04x\n", hkey);
3717 printk(TPACPI_NOTICE
3718 "please report the conditions when this "
3719 "event happened to %s\n", TPACPI_MAIL);
3722 /* Legacy events */
3723 if (!ignore_acpi_ev &&
3724 (send_acpi_ev || hotkey_report_mode < 2)) {
3725 acpi_bus_generate_proc_event(ibm->acpi->device,
3726 event, hkey);
3729 /* netlink events */
3730 if (!ignore_acpi_ev && send_acpi_ev) {
3731 acpi_bus_generate_netlink_event(
3732 ibm->acpi->device->pnp.device_class,
3733 dev_name(&ibm->acpi->device->dev),
3734 event, hkey);
3739 static void hotkey_suspend(pm_message_t state)
3741 /* Do these on suspend, we get the events on early resume! */
3742 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3743 hotkey_autosleep_ack = 0;
3746 static void hotkey_resume(void)
3748 tpacpi_disable_brightness_delay();
3750 if (hotkey_status_set(true) < 0 ||
3751 hotkey_mask_set(hotkey_acpi_mask) < 0)
3752 printk(TPACPI_ERR
3753 "error while attempting to reset the event "
3754 "firmware interface\n");
3756 tpacpi_send_radiosw_update();
3757 hotkey_tablet_mode_notify_change();
3758 hotkey_wakeup_reason_notify_change();
3759 hotkey_wakeup_hotunplug_complete_notify_change();
3760 hotkey_poll_setup_safe(false);
3763 /* procfs -------------------------------------------------------------- */
3764 static int hotkey_read(struct seq_file *m)
3766 int res, status;
3768 if (!tp_features.hotkey) {
3769 seq_printf(m, "status:\t\tnot supported\n");
3770 return 0;
3773 if (mutex_lock_killable(&hotkey_mutex))
3774 return -ERESTARTSYS;
3775 res = hotkey_status_get(&status);
3776 if (!res)
3777 res = hotkey_mask_get();
3778 mutex_unlock(&hotkey_mutex);
3779 if (res)
3780 return res;
3782 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3783 if (hotkey_all_mask) {
3784 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3785 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3786 } else {
3787 seq_printf(m, "mask:\t\tnot supported\n");
3788 seq_printf(m, "commands:\tenable, disable, reset\n");
3791 return 0;
3794 static void hotkey_enabledisable_warn(bool enable)
3796 tpacpi_log_usertask("procfs hotkey enable/disable");
3797 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3798 TPACPI_WARN
3799 "hotkey enable/disable functionality has been "
3800 "removed from the driver. Hotkeys are always "
3801 "enabled\n"))
3802 printk(TPACPI_ERR
3803 "Please remove the hotkey=enable module "
3804 "parameter, it is deprecated. Hotkeys are always "
3805 "enabled\n");
3808 static int hotkey_write(char *buf)
3810 int res;
3811 u32 mask;
3812 char *cmd;
3814 if (!tp_features.hotkey)
3815 return -ENODEV;
3817 if (mutex_lock_killable(&hotkey_mutex))
3818 return -ERESTARTSYS;
3820 mask = hotkey_user_mask;
3822 res = 0;
3823 while ((cmd = next_cmd(&buf))) {
3824 if (strlencmp(cmd, "enable") == 0) {
3825 hotkey_enabledisable_warn(1);
3826 } else if (strlencmp(cmd, "disable") == 0) {
3827 hotkey_enabledisable_warn(0);
3828 res = -EPERM;
3829 } else if (strlencmp(cmd, "reset") == 0) {
3830 mask = (hotkey_all_mask | hotkey_source_mask)
3831 & ~hotkey_reserved_mask;
3832 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3833 /* mask set */
3834 } else if (sscanf(cmd, "%x", &mask) == 1) {
3835 /* mask set */
3836 } else {
3837 res = -EINVAL;
3838 goto errexit;
3842 if (!res) {
3843 tpacpi_disclose_usertask("procfs hotkey",
3844 "set mask to 0x%08x\n", mask);
3845 res = hotkey_user_mask_set(mask);
3848 errexit:
3849 mutex_unlock(&hotkey_mutex);
3850 return res;
3853 static const struct acpi_device_id ibm_htk_device_ids[] = {
3854 {TPACPI_ACPI_IBM_HKEY_HID, 0},
3855 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
3856 {"", 0},
3859 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3860 .hid = ibm_htk_device_ids,
3861 .notify = hotkey_notify,
3862 .handle = &hkey_handle,
3863 .type = ACPI_DEVICE_NOTIFY,
3866 static struct ibm_struct hotkey_driver_data = {
3867 .name = "hotkey",
3868 .read = hotkey_read,
3869 .write = hotkey_write,
3870 .exit = hotkey_exit,
3871 .resume = hotkey_resume,
3872 .suspend = hotkey_suspend,
3873 .acpi = &ibm_hotkey_acpidriver,
3876 /*************************************************************************
3877 * Bluetooth subdriver
3880 enum {
3881 /* ACPI GBDC/SBDC bits */
3882 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3883 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3884 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3885 0 = disable, 1 = enable */
3888 enum {
3889 /* ACPI \BLTH commands */
3890 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3891 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3892 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3893 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3894 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3897 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3899 static int bluetooth_get_status(void)
3901 int status;
3903 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3904 if (dbg_bluetoothemul)
3905 return (tpacpi_bluetooth_emulstate) ?
3906 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3907 #endif
3909 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3910 return -EIO;
3912 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3913 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3916 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3918 int status;
3920 vdbg_printk(TPACPI_DBG_RFKILL,
3921 "will attempt to %s bluetooth\n",
3922 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3924 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3925 if (dbg_bluetoothemul) {
3926 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3927 return 0;
3929 #endif
3931 if (state == TPACPI_RFK_RADIO_ON)
3932 status = TP_ACPI_BLUETOOTH_RADIOSSW
3933 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3934 else
3935 status = 0;
3937 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3938 return -EIO;
3940 return 0;
3943 /* sysfs bluetooth enable ---------------------------------------------- */
3944 static ssize_t bluetooth_enable_show(struct device *dev,
3945 struct device_attribute *attr,
3946 char *buf)
3948 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3949 attr, buf);
3952 static ssize_t bluetooth_enable_store(struct device *dev,
3953 struct device_attribute *attr,
3954 const char *buf, size_t count)
3956 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3957 attr, buf, count);
3960 static struct device_attribute dev_attr_bluetooth_enable =
3961 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3962 bluetooth_enable_show, bluetooth_enable_store);
3964 /* --------------------------------------------------------------------- */
3966 static struct attribute *bluetooth_attributes[] = {
3967 &dev_attr_bluetooth_enable.attr,
3968 NULL
3971 static const struct attribute_group bluetooth_attr_group = {
3972 .attrs = bluetooth_attributes,
3975 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3976 .get_status = bluetooth_get_status,
3977 .set_status = bluetooth_set_status,
3980 static void bluetooth_shutdown(void)
3982 /* Order firmware to save current state to NVRAM */
3983 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3984 TP_ACPI_BLTH_SAVE_STATE))
3985 printk(TPACPI_NOTICE
3986 "failed to save bluetooth state to NVRAM\n");
3987 else
3988 vdbg_printk(TPACPI_DBG_RFKILL,
3989 "bluestooth state saved to NVRAM\n");
3992 static void bluetooth_exit(void)
3994 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3995 &bluetooth_attr_group);
3997 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3999 bluetooth_shutdown();
4002 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4004 int res;
4005 int status = 0;
4007 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4008 "initializing bluetooth subdriver\n");
4010 TPACPI_ACPIHANDLE_INIT(hkey);
4012 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4013 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4014 tp_features.bluetooth = hkey_handle &&
4015 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4017 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4018 "bluetooth is %s, status 0x%02x\n",
4019 str_supported(tp_features.bluetooth),
4020 status);
4022 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4023 if (dbg_bluetoothemul) {
4024 tp_features.bluetooth = 1;
4025 printk(TPACPI_INFO
4026 "bluetooth switch emulation enabled\n");
4027 } else
4028 #endif
4029 if (tp_features.bluetooth &&
4030 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4031 /* no bluetooth hardware present in system */
4032 tp_features.bluetooth = 0;
4033 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4034 "bluetooth hardware not installed\n");
4037 if (!tp_features.bluetooth)
4038 return 1;
4040 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4041 &bluetooth_tprfk_ops,
4042 RFKILL_TYPE_BLUETOOTH,
4043 TPACPI_RFK_BLUETOOTH_SW_NAME,
4044 true);
4045 if (res)
4046 return res;
4048 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4049 &bluetooth_attr_group);
4050 if (res) {
4051 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4052 return res;
4055 return 0;
4058 /* procfs -------------------------------------------------------------- */
4059 static int bluetooth_read(struct seq_file *m)
4061 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4064 static int bluetooth_write(char *buf)
4066 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4069 static struct ibm_struct bluetooth_driver_data = {
4070 .name = "bluetooth",
4071 .read = bluetooth_read,
4072 .write = bluetooth_write,
4073 .exit = bluetooth_exit,
4074 .shutdown = bluetooth_shutdown,
4077 /*************************************************************************
4078 * Wan subdriver
4081 enum {
4082 /* ACPI GWAN/SWAN bits */
4083 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4084 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4085 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4086 0 = disable, 1 = enable */
4089 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4091 static int wan_get_status(void)
4093 int status;
4095 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4096 if (dbg_wwanemul)
4097 return (tpacpi_wwan_emulstate) ?
4098 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4099 #endif
4101 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4102 return -EIO;
4104 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4105 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4108 static int wan_set_status(enum tpacpi_rfkill_state state)
4110 int status;
4112 vdbg_printk(TPACPI_DBG_RFKILL,
4113 "will attempt to %s wwan\n",
4114 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4116 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4117 if (dbg_wwanemul) {
4118 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4119 return 0;
4121 #endif
4123 if (state == TPACPI_RFK_RADIO_ON)
4124 status = TP_ACPI_WANCARD_RADIOSSW
4125 | TP_ACPI_WANCARD_RESUMECTRL;
4126 else
4127 status = 0;
4129 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4130 return -EIO;
4132 return 0;
4135 /* sysfs wan enable ---------------------------------------------------- */
4136 static ssize_t wan_enable_show(struct device *dev,
4137 struct device_attribute *attr,
4138 char *buf)
4140 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4141 attr, buf);
4144 static ssize_t wan_enable_store(struct device *dev,
4145 struct device_attribute *attr,
4146 const char *buf, size_t count)
4148 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4149 attr, buf, count);
4152 static struct device_attribute dev_attr_wan_enable =
4153 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4154 wan_enable_show, wan_enable_store);
4156 /* --------------------------------------------------------------------- */
4158 static struct attribute *wan_attributes[] = {
4159 &dev_attr_wan_enable.attr,
4160 NULL
4163 static const struct attribute_group wan_attr_group = {
4164 .attrs = wan_attributes,
4167 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4168 .get_status = wan_get_status,
4169 .set_status = wan_set_status,
4172 static void wan_shutdown(void)
4174 /* Order firmware to save current state to NVRAM */
4175 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4176 TP_ACPI_WGSV_SAVE_STATE))
4177 printk(TPACPI_NOTICE
4178 "failed to save WWAN state to NVRAM\n");
4179 else
4180 vdbg_printk(TPACPI_DBG_RFKILL,
4181 "WWAN state saved to NVRAM\n");
4184 static void wan_exit(void)
4186 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4187 &wan_attr_group);
4189 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4191 wan_shutdown();
4194 static int __init wan_init(struct ibm_init_struct *iibm)
4196 int res;
4197 int status = 0;
4199 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4200 "initializing wan subdriver\n");
4202 TPACPI_ACPIHANDLE_INIT(hkey);
4204 tp_features.wan = hkey_handle &&
4205 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4207 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4208 "wan is %s, status 0x%02x\n",
4209 str_supported(tp_features.wan),
4210 status);
4212 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4213 if (dbg_wwanemul) {
4214 tp_features.wan = 1;
4215 printk(TPACPI_INFO
4216 "wwan switch emulation enabled\n");
4217 } else
4218 #endif
4219 if (tp_features.wan &&
4220 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4221 /* no wan hardware present in system */
4222 tp_features.wan = 0;
4223 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4224 "wan hardware not installed\n");
4227 if (!tp_features.wan)
4228 return 1;
4230 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4231 &wan_tprfk_ops,
4232 RFKILL_TYPE_WWAN,
4233 TPACPI_RFK_WWAN_SW_NAME,
4234 true);
4235 if (res)
4236 return res;
4238 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4239 &wan_attr_group);
4241 if (res) {
4242 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4243 return res;
4246 return 0;
4249 /* procfs -------------------------------------------------------------- */
4250 static int wan_read(struct seq_file *m)
4252 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4255 static int wan_write(char *buf)
4257 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4260 static struct ibm_struct wan_driver_data = {
4261 .name = "wan",
4262 .read = wan_read,
4263 .write = wan_write,
4264 .exit = wan_exit,
4265 .shutdown = wan_shutdown,
4268 /*************************************************************************
4269 * UWB subdriver
4272 enum {
4273 /* ACPI GUWB/SUWB bits */
4274 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4275 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4278 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4280 static int uwb_get_status(void)
4282 int status;
4284 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4285 if (dbg_uwbemul)
4286 return (tpacpi_uwb_emulstate) ?
4287 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4288 #endif
4290 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4291 return -EIO;
4293 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4294 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4297 static int uwb_set_status(enum tpacpi_rfkill_state state)
4299 int status;
4301 vdbg_printk(TPACPI_DBG_RFKILL,
4302 "will attempt to %s UWB\n",
4303 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4305 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4306 if (dbg_uwbemul) {
4307 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4308 return 0;
4310 #endif
4312 if (state == TPACPI_RFK_RADIO_ON)
4313 status = TP_ACPI_UWB_RADIOSSW;
4314 else
4315 status = 0;
4317 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4318 return -EIO;
4320 return 0;
4323 /* --------------------------------------------------------------------- */
4325 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4326 .get_status = uwb_get_status,
4327 .set_status = uwb_set_status,
4330 static void uwb_exit(void)
4332 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4335 static int __init uwb_init(struct ibm_init_struct *iibm)
4337 int res;
4338 int status = 0;
4340 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4341 "initializing uwb subdriver\n");
4343 TPACPI_ACPIHANDLE_INIT(hkey);
4345 tp_features.uwb = hkey_handle &&
4346 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4348 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4349 "uwb is %s, status 0x%02x\n",
4350 str_supported(tp_features.uwb),
4351 status);
4353 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4354 if (dbg_uwbemul) {
4355 tp_features.uwb = 1;
4356 printk(TPACPI_INFO
4357 "uwb switch emulation enabled\n");
4358 } else
4359 #endif
4360 if (tp_features.uwb &&
4361 !(status & TP_ACPI_UWB_HWPRESENT)) {
4362 /* no uwb hardware present in system */
4363 tp_features.uwb = 0;
4364 dbg_printk(TPACPI_DBG_INIT,
4365 "uwb hardware not installed\n");
4368 if (!tp_features.uwb)
4369 return 1;
4371 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4372 &uwb_tprfk_ops,
4373 RFKILL_TYPE_UWB,
4374 TPACPI_RFK_UWB_SW_NAME,
4375 false);
4376 return res;
4379 static struct ibm_struct uwb_driver_data = {
4380 .name = "uwb",
4381 .exit = uwb_exit,
4382 .flags.experimental = 1,
4385 /*************************************************************************
4386 * Video subdriver
4389 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4391 enum video_access_mode {
4392 TPACPI_VIDEO_NONE = 0,
4393 TPACPI_VIDEO_570, /* 570 */
4394 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4395 TPACPI_VIDEO_NEW, /* all others */
4398 enum { /* video status flags, based on VIDEO_570 */
4399 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4400 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4401 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4404 enum { /* TPACPI_VIDEO_570 constants */
4405 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4406 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4407 * video_status_flags */
4408 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4409 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4412 static enum video_access_mode video_supported;
4413 static int video_orig_autosw;
4415 static int video_autosw_get(void);
4416 static int video_autosw_set(int enable);
4418 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4420 static int __init video_init(struct ibm_init_struct *iibm)
4422 int ivga;
4424 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4426 TPACPI_ACPIHANDLE_INIT(vid);
4427 TPACPI_ACPIHANDLE_INIT(vid2);
4429 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4430 /* G41, assume IVGA doesn't change */
4431 vid_handle = vid2_handle;
4433 if (!vid_handle)
4434 /* video switching not supported on R30, R31 */
4435 video_supported = TPACPI_VIDEO_NONE;
4436 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4437 /* 570 */
4438 video_supported = TPACPI_VIDEO_570;
4439 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4440 /* 600e/x, 770e, 770x */
4441 video_supported = TPACPI_VIDEO_770;
4442 else
4443 /* all others */
4444 video_supported = TPACPI_VIDEO_NEW;
4446 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4447 str_supported(video_supported != TPACPI_VIDEO_NONE),
4448 video_supported);
4450 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4453 static void video_exit(void)
4455 dbg_printk(TPACPI_DBG_EXIT,
4456 "restoring original video autoswitch mode\n");
4457 if (video_autosw_set(video_orig_autosw))
4458 printk(TPACPI_ERR "error while trying to restore original "
4459 "video autoswitch mode\n");
4462 static int video_outputsw_get(void)
4464 int status = 0;
4465 int i;
4467 switch (video_supported) {
4468 case TPACPI_VIDEO_570:
4469 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4470 TP_ACPI_VIDEO_570_PHSCMD))
4471 return -EIO;
4472 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4473 break;
4474 case TPACPI_VIDEO_770:
4475 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4476 return -EIO;
4477 if (i)
4478 status |= TP_ACPI_VIDEO_S_LCD;
4479 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4480 return -EIO;
4481 if (i)
4482 status |= TP_ACPI_VIDEO_S_CRT;
4483 break;
4484 case TPACPI_VIDEO_NEW:
4485 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4486 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4487 return -EIO;
4488 if (i)
4489 status |= TP_ACPI_VIDEO_S_CRT;
4491 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4492 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4493 return -EIO;
4494 if (i)
4495 status |= TP_ACPI_VIDEO_S_LCD;
4496 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4497 return -EIO;
4498 if (i)
4499 status |= TP_ACPI_VIDEO_S_DVI;
4500 break;
4501 default:
4502 return -ENOSYS;
4505 return status;
4508 static int video_outputsw_set(int status)
4510 int autosw;
4511 int res = 0;
4513 switch (video_supported) {
4514 case TPACPI_VIDEO_570:
4515 res = acpi_evalf(NULL, NULL,
4516 "\\_SB.PHS2", "vdd",
4517 TP_ACPI_VIDEO_570_PHS2CMD,
4518 status | TP_ACPI_VIDEO_570_PHS2SET);
4519 break;
4520 case TPACPI_VIDEO_770:
4521 autosw = video_autosw_get();
4522 if (autosw < 0)
4523 return autosw;
4525 res = video_autosw_set(1);
4526 if (res)
4527 return res;
4528 res = acpi_evalf(vid_handle, NULL,
4529 "ASWT", "vdd", status * 0x100, 0);
4530 if (!autosw && video_autosw_set(autosw)) {
4531 printk(TPACPI_ERR
4532 "video auto-switch left enabled due to error\n");
4533 return -EIO;
4535 break;
4536 case TPACPI_VIDEO_NEW:
4537 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4538 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4539 break;
4540 default:
4541 return -ENOSYS;
4544 return (res)? 0 : -EIO;
4547 static int video_autosw_get(void)
4549 int autosw = 0;
4551 switch (video_supported) {
4552 case TPACPI_VIDEO_570:
4553 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4554 return -EIO;
4555 break;
4556 case TPACPI_VIDEO_770:
4557 case TPACPI_VIDEO_NEW:
4558 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4559 return -EIO;
4560 break;
4561 default:
4562 return -ENOSYS;
4565 return autosw & 1;
4568 static int video_autosw_set(int enable)
4570 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4571 return -EIO;
4572 return 0;
4575 static int video_outputsw_cycle(void)
4577 int autosw = video_autosw_get();
4578 int res;
4580 if (autosw < 0)
4581 return autosw;
4583 switch (video_supported) {
4584 case TPACPI_VIDEO_570:
4585 res = video_autosw_set(1);
4586 if (res)
4587 return res;
4588 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4589 break;
4590 case TPACPI_VIDEO_770:
4591 case TPACPI_VIDEO_NEW:
4592 res = video_autosw_set(1);
4593 if (res)
4594 return res;
4595 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4596 break;
4597 default:
4598 return -ENOSYS;
4600 if (!autosw && video_autosw_set(autosw)) {
4601 printk(TPACPI_ERR
4602 "video auto-switch left enabled due to error\n");
4603 return -EIO;
4606 return (res)? 0 : -EIO;
4609 static int video_expand_toggle(void)
4611 switch (video_supported) {
4612 case TPACPI_VIDEO_570:
4613 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4614 0 : -EIO;
4615 case TPACPI_VIDEO_770:
4616 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4617 0 : -EIO;
4618 case TPACPI_VIDEO_NEW:
4619 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4620 0 : -EIO;
4621 default:
4622 return -ENOSYS;
4624 /* not reached */
4627 static int video_read(struct seq_file *m)
4629 int status, autosw;
4631 if (video_supported == TPACPI_VIDEO_NONE) {
4632 seq_printf(m, "status:\t\tnot supported\n");
4633 return 0;
4636 /* Even reads can crash X.org, so... */
4637 if (!capable(CAP_SYS_ADMIN))
4638 return -EPERM;
4640 status = video_outputsw_get();
4641 if (status < 0)
4642 return status;
4644 autosw = video_autosw_get();
4645 if (autosw < 0)
4646 return autosw;
4648 seq_printf(m, "status:\t\tsupported\n");
4649 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4650 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4651 if (video_supported == TPACPI_VIDEO_NEW)
4652 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4653 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4654 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4655 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4656 if (video_supported == TPACPI_VIDEO_NEW)
4657 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4658 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4659 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4661 return 0;
4664 static int video_write(char *buf)
4666 char *cmd;
4667 int enable, disable, status;
4668 int res;
4670 if (video_supported == TPACPI_VIDEO_NONE)
4671 return -ENODEV;
4673 /* Even reads can crash X.org, let alone writes... */
4674 if (!capable(CAP_SYS_ADMIN))
4675 return -EPERM;
4677 enable = 0;
4678 disable = 0;
4680 while ((cmd = next_cmd(&buf))) {
4681 if (strlencmp(cmd, "lcd_enable") == 0) {
4682 enable |= TP_ACPI_VIDEO_S_LCD;
4683 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4684 disable |= TP_ACPI_VIDEO_S_LCD;
4685 } else if (strlencmp(cmd, "crt_enable") == 0) {
4686 enable |= TP_ACPI_VIDEO_S_CRT;
4687 } else if (strlencmp(cmd, "crt_disable") == 0) {
4688 disable |= TP_ACPI_VIDEO_S_CRT;
4689 } else if (video_supported == TPACPI_VIDEO_NEW &&
4690 strlencmp(cmd, "dvi_enable") == 0) {
4691 enable |= TP_ACPI_VIDEO_S_DVI;
4692 } else if (video_supported == TPACPI_VIDEO_NEW &&
4693 strlencmp(cmd, "dvi_disable") == 0) {
4694 disable |= TP_ACPI_VIDEO_S_DVI;
4695 } else if (strlencmp(cmd, "auto_enable") == 0) {
4696 res = video_autosw_set(1);
4697 if (res)
4698 return res;
4699 } else if (strlencmp(cmd, "auto_disable") == 0) {
4700 res = video_autosw_set(0);
4701 if (res)
4702 return res;
4703 } else if (strlencmp(cmd, "video_switch") == 0) {
4704 res = video_outputsw_cycle();
4705 if (res)
4706 return res;
4707 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4708 res = video_expand_toggle();
4709 if (res)
4710 return res;
4711 } else
4712 return -EINVAL;
4715 if (enable || disable) {
4716 status = video_outputsw_get();
4717 if (status < 0)
4718 return status;
4719 res = video_outputsw_set((status & ~disable) | enable);
4720 if (res)
4721 return res;
4724 return 0;
4727 static struct ibm_struct video_driver_data = {
4728 .name = "video",
4729 .read = video_read,
4730 .write = video_write,
4731 .exit = video_exit,
4734 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4736 /*************************************************************************
4737 * Light (thinklight) subdriver
4740 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4741 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4743 static int light_get_status(void)
4745 int status = 0;
4747 if (tp_features.light_status) {
4748 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4749 return -EIO;
4750 return (!!status);
4753 return -ENXIO;
4756 static int light_set_status(int status)
4758 int rc;
4760 if (tp_features.light) {
4761 if (cmos_handle) {
4762 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4763 (status)?
4764 TP_CMOS_THINKLIGHT_ON :
4765 TP_CMOS_THINKLIGHT_OFF);
4766 } else {
4767 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4768 (status)? 1 : 0);
4770 return (rc)? 0 : -EIO;
4773 return -ENXIO;
4776 static void light_set_status_worker(struct work_struct *work)
4778 struct tpacpi_led_classdev *data =
4779 container_of(work, struct tpacpi_led_classdev, work);
4781 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4782 light_set_status((data->new_state != TPACPI_LED_OFF));
4785 static void light_sysfs_set(struct led_classdev *led_cdev,
4786 enum led_brightness brightness)
4788 struct tpacpi_led_classdev *data =
4789 container_of(led_cdev,
4790 struct tpacpi_led_classdev,
4791 led_classdev);
4792 data->new_state = (brightness != LED_OFF) ?
4793 TPACPI_LED_ON : TPACPI_LED_OFF;
4794 queue_work(tpacpi_wq, &data->work);
4797 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4799 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4802 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4803 .led_classdev = {
4804 .name = "tpacpi::thinklight",
4805 .brightness_set = &light_sysfs_set,
4806 .brightness_get = &light_sysfs_get,
4810 static int __init light_init(struct ibm_init_struct *iibm)
4812 int rc;
4814 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4816 TPACPI_ACPIHANDLE_INIT(ledb);
4817 TPACPI_ACPIHANDLE_INIT(lght);
4818 TPACPI_ACPIHANDLE_INIT(cmos);
4819 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4821 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4822 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4824 if (tp_features.light)
4825 /* light status not supported on
4826 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4827 tp_features.light_status =
4828 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4830 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4831 str_supported(tp_features.light),
4832 str_supported(tp_features.light_status));
4834 if (!tp_features.light)
4835 return 1;
4837 rc = led_classdev_register(&tpacpi_pdev->dev,
4838 &tpacpi_led_thinklight.led_classdev);
4840 if (rc < 0) {
4841 tp_features.light = 0;
4842 tp_features.light_status = 0;
4843 } else {
4844 rc = 0;
4847 return rc;
4850 static void light_exit(void)
4852 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4853 if (work_pending(&tpacpi_led_thinklight.work))
4854 flush_workqueue(tpacpi_wq);
4857 static int light_read(struct seq_file *m)
4859 int status;
4861 if (!tp_features.light) {
4862 seq_printf(m, "status:\t\tnot supported\n");
4863 } else if (!tp_features.light_status) {
4864 seq_printf(m, "status:\t\tunknown\n");
4865 seq_printf(m, "commands:\ton, off\n");
4866 } else {
4867 status = light_get_status();
4868 if (status < 0)
4869 return status;
4870 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4871 seq_printf(m, "commands:\ton, off\n");
4874 return 0;
4877 static int light_write(char *buf)
4879 char *cmd;
4880 int newstatus = 0;
4882 if (!tp_features.light)
4883 return -ENODEV;
4885 while ((cmd = next_cmd(&buf))) {
4886 if (strlencmp(cmd, "on") == 0) {
4887 newstatus = 1;
4888 } else if (strlencmp(cmd, "off") == 0) {
4889 newstatus = 0;
4890 } else
4891 return -EINVAL;
4894 return light_set_status(newstatus);
4897 static struct ibm_struct light_driver_data = {
4898 .name = "light",
4899 .read = light_read,
4900 .write = light_write,
4901 .exit = light_exit,
4904 /*************************************************************************
4905 * CMOS subdriver
4908 /* sysfs cmos_command -------------------------------------------------- */
4909 static ssize_t cmos_command_store(struct device *dev,
4910 struct device_attribute *attr,
4911 const char *buf, size_t count)
4913 unsigned long cmos_cmd;
4914 int res;
4916 if (parse_strtoul(buf, 21, &cmos_cmd))
4917 return -EINVAL;
4919 res = issue_thinkpad_cmos_command(cmos_cmd);
4920 return (res)? res : count;
4923 static struct device_attribute dev_attr_cmos_command =
4924 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4926 /* --------------------------------------------------------------------- */
4928 static int __init cmos_init(struct ibm_init_struct *iibm)
4930 int res;
4932 vdbg_printk(TPACPI_DBG_INIT,
4933 "initializing cmos commands subdriver\n");
4935 TPACPI_ACPIHANDLE_INIT(cmos);
4937 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4938 str_supported(cmos_handle != NULL));
4940 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4941 if (res)
4942 return res;
4944 return (cmos_handle)? 0 : 1;
4947 static void cmos_exit(void)
4949 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4952 static int cmos_read(struct seq_file *m)
4954 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4955 R30, R31, T20-22, X20-21 */
4956 if (!cmos_handle)
4957 seq_printf(m, "status:\t\tnot supported\n");
4958 else {
4959 seq_printf(m, "status:\t\tsupported\n");
4960 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4963 return 0;
4966 static int cmos_write(char *buf)
4968 char *cmd;
4969 int cmos_cmd, res;
4971 while ((cmd = next_cmd(&buf))) {
4972 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4973 cmos_cmd >= 0 && cmos_cmd <= 21) {
4974 /* cmos_cmd set */
4975 } else
4976 return -EINVAL;
4978 res = issue_thinkpad_cmos_command(cmos_cmd);
4979 if (res)
4980 return res;
4983 return 0;
4986 static struct ibm_struct cmos_driver_data = {
4987 .name = "cmos",
4988 .read = cmos_read,
4989 .write = cmos_write,
4990 .exit = cmos_exit,
4993 /*************************************************************************
4994 * LED subdriver
4997 enum led_access_mode {
4998 TPACPI_LED_NONE = 0,
4999 TPACPI_LED_570, /* 570 */
5000 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5001 TPACPI_LED_NEW, /* all others */
5004 enum { /* For TPACPI_LED_OLD */
5005 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5006 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5007 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5010 static enum led_access_mode led_supported;
5012 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
5013 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
5014 /* T20-22, X20-21 */
5015 "LED", /* all others */
5016 ); /* R30, R31 */
5018 #define TPACPI_LED_NUMLEDS 16
5019 static struct tpacpi_led_classdev *tpacpi_leds;
5020 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5021 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5022 /* there's a limit of 19 chars + NULL before 2.6.26 */
5023 "tpacpi::power",
5024 "tpacpi:orange:batt",
5025 "tpacpi:green:batt",
5026 "tpacpi::dock_active",
5027 "tpacpi::bay_active",
5028 "tpacpi::dock_batt",
5029 "tpacpi::unknown_led",
5030 "tpacpi::standby",
5031 "tpacpi::dock_status1",
5032 "tpacpi::dock_status2",
5033 "tpacpi::unknown_led2",
5034 "tpacpi::unknown_led3",
5035 "tpacpi::thinkvantage",
5037 #define TPACPI_SAFE_LEDS 0x1081U
5039 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5041 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5042 return false;
5043 #else
5044 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5045 #endif
5048 static int led_get_status(const unsigned int led)
5050 int status;
5051 enum led_status_t led_s;
5053 switch (led_supported) {
5054 case TPACPI_LED_570:
5055 if (!acpi_evalf(ec_handle,
5056 &status, "GLED", "dd", 1 << led))
5057 return -EIO;
5058 led_s = (status == 0)?
5059 TPACPI_LED_OFF :
5060 ((status == 1)?
5061 TPACPI_LED_ON :
5062 TPACPI_LED_BLINK);
5063 tpacpi_led_state_cache[led] = led_s;
5064 return led_s;
5065 default:
5066 return -ENXIO;
5069 /* not reached */
5072 static int led_set_status(const unsigned int led,
5073 const enum led_status_t ledstatus)
5075 /* off, on, blink. Index is led_status_t */
5076 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5077 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5079 int rc = 0;
5081 switch (led_supported) {
5082 case TPACPI_LED_570:
5083 /* 570 */
5084 if (unlikely(led > 7))
5085 return -EINVAL;
5086 if (unlikely(tpacpi_is_led_restricted(led)))
5087 return -EPERM;
5088 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5089 (1 << led), led_sled_arg1[ledstatus]))
5090 rc = -EIO;
5091 break;
5092 case TPACPI_LED_OLD:
5093 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5094 if (unlikely(led > 7))
5095 return -EINVAL;
5096 if (unlikely(tpacpi_is_led_restricted(led)))
5097 return -EPERM;
5098 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5099 if (rc >= 0)
5100 rc = ec_write(TPACPI_LED_EC_HLBL,
5101 (ledstatus == TPACPI_LED_BLINK) << led);
5102 if (rc >= 0)
5103 rc = ec_write(TPACPI_LED_EC_HLCL,
5104 (ledstatus != TPACPI_LED_OFF) << led);
5105 break;
5106 case TPACPI_LED_NEW:
5107 /* all others */
5108 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5109 return -EINVAL;
5110 if (unlikely(tpacpi_is_led_restricted(led)))
5111 return -EPERM;
5112 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5113 led, led_led_arg1[ledstatus]))
5114 rc = -EIO;
5115 break;
5116 default:
5117 rc = -ENXIO;
5120 if (!rc)
5121 tpacpi_led_state_cache[led] = ledstatus;
5123 return rc;
5126 static void led_set_status_worker(struct work_struct *work)
5128 struct tpacpi_led_classdev *data =
5129 container_of(work, struct tpacpi_led_classdev, work);
5131 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5132 led_set_status(data->led, data->new_state);
5135 static void led_sysfs_set(struct led_classdev *led_cdev,
5136 enum led_brightness brightness)
5138 struct tpacpi_led_classdev *data = container_of(led_cdev,
5139 struct tpacpi_led_classdev, led_classdev);
5141 if (brightness == LED_OFF)
5142 data->new_state = TPACPI_LED_OFF;
5143 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5144 data->new_state = TPACPI_LED_ON;
5145 else
5146 data->new_state = TPACPI_LED_BLINK;
5148 queue_work(tpacpi_wq, &data->work);
5151 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5152 unsigned long *delay_on, unsigned long *delay_off)
5154 struct tpacpi_led_classdev *data = container_of(led_cdev,
5155 struct tpacpi_led_classdev, led_classdev);
5157 /* Can we choose the flash rate? */
5158 if (*delay_on == 0 && *delay_off == 0) {
5159 /* yes. set them to the hardware blink rate (1 Hz) */
5160 *delay_on = 500; /* ms */
5161 *delay_off = 500; /* ms */
5162 } else if ((*delay_on != 500) || (*delay_off != 500))
5163 return -EINVAL;
5165 data->new_state = TPACPI_LED_BLINK;
5166 queue_work(tpacpi_wq, &data->work);
5168 return 0;
5171 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5173 int rc;
5175 struct tpacpi_led_classdev *data = container_of(led_cdev,
5176 struct tpacpi_led_classdev, led_classdev);
5178 rc = led_get_status(data->led);
5180 if (rc == TPACPI_LED_OFF || rc < 0)
5181 rc = LED_OFF; /* no error handling in led class :( */
5182 else
5183 rc = LED_FULL;
5185 return rc;
5188 static void led_exit(void)
5190 unsigned int i;
5192 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5193 if (tpacpi_leds[i].led_classdev.name)
5194 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5197 kfree(tpacpi_leds);
5200 static int __init tpacpi_init_led(unsigned int led)
5202 int rc;
5204 tpacpi_leds[led].led = led;
5206 /* LEDs with no name don't get registered */
5207 if (!tpacpi_led_names[led])
5208 return 0;
5210 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5211 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5212 if (led_supported == TPACPI_LED_570)
5213 tpacpi_leds[led].led_classdev.brightness_get =
5214 &led_sysfs_get;
5216 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5218 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5220 rc = led_classdev_register(&tpacpi_pdev->dev,
5221 &tpacpi_leds[led].led_classdev);
5222 if (rc < 0)
5223 tpacpi_leds[led].led_classdev.name = NULL;
5225 return rc;
5228 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5229 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5230 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5231 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5233 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5234 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5235 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5236 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5237 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5238 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5239 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5240 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5242 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5243 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5244 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5245 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5246 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5248 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5249 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5250 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5251 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5253 /* (1) - may have excess leds enabled on MSB */
5255 /* Defaults (order matters, keep last, don't reorder!) */
5256 { /* Lenovo */
5257 .vendor = PCI_VENDOR_ID_LENOVO,
5258 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5259 .quirks = 0x1fffU,
5261 { /* IBM ThinkPads with no EC version string */
5262 .vendor = PCI_VENDOR_ID_IBM,
5263 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5264 .quirks = 0x00ffU,
5266 { /* IBM ThinkPads with EC version string */
5267 .vendor = PCI_VENDOR_ID_IBM,
5268 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5269 .quirks = 0x00bfU,
5273 #undef TPACPI_LEDQ_IBM
5274 #undef TPACPI_LEDQ_LNV
5276 static int __init led_init(struct ibm_init_struct *iibm)
5278 unsigned int i;
5279 int rc;
5280 unsigned long useful_leds;
5282 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5284 TPACPI_ACPIHANDLE_INIT(led);
5286 if (!led_handle)
5287 /* led not supported on R30, R31 */
5288 led_supported = TPACPI_LED_NONE;
5289 else if (strlencmp(led_path, "SLED") == 0)
5290 /* 570 */
5291 led_supported = TPACPI_LED_570;
5292 else if (strlencmp(led_path, "SYSL") == 0)
5293 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5294 led_supported = TPACPI_LED_OLD;
5295 else
5296 /* all others */
5297 led_supported = TPACPI_LED_NEW;
5299 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5300 str_supported(led_supported), led_supported);
5302 if (led_supported == TPACPI_LED_NONE)
5303 return 1;
5305 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5306 GFP_KERNEL);
5307 if (!tpacpi_leds) {
5308 printk(TPACPI_ERR "Out of memory for LED data\n");
5309 return -ENOMEM;
5312 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5313 ARRAY_SIZE(led_useful_qtable));
5315 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5316 if (!tpacpi_is_led_restricted(i) &&
5317 test_bit(i, &useful_leds)) {
5318 rc = tpacpi_init_led(i);
5319 if (rc < 0) {
5320 led_exit();
5321 return rc;
5326 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5327 printk(TPACPI_NOTICE
5328 "warning: userspace override of important "
5329 "firmware LEDs is enabled\n");
5330 #endif
5331 return 0;
5334 #define str_led_status(s) \
5335 ((s) == TPACPI_LED_OFF ? "off" : \
5336 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5338 static int led_read(struct seq_file *m)
5340 if (!led_supported) {
5341 seq_printf(m, "status:\t\tnot supported\n");
5342 return 0;
5344 seq_printf(m, "status:\t\tsupported\n");
5346 if (led_supported == TPACPI_LED_570) {
5347 /* 570 */
5348 int i, status;
5349 for (i = 0; i < 8; i++) {
5350 status = led_get_status(i);
5351 if (status < 0)
5352 return -EIO;
5353 seq_printf(m, "%d:\t\t%s\n",
5354 i, str_led_status(status));
5358 seq_printf(m, "commands:\t"
5359 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5361 return 0;
5364 static int led_write(char *buf)
5366 char *cmd;
5367 int led, rc;
5368 enum led_status_t s;
5370 if (!led_supported)
5371 return -ENODEV;
5373 while ((cmd = next_cmd(&buf))) {
5374 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5375 return -EINVAL;
5377 if (strstr(cmd, "off")) {
5378 s = TPACPI_LED_OFF;
5379 } else if (strstr(cmd, "on")) {
5380 s = TPACPI_LED_ON;
5381 } else if (strstr(cmd, "blink")) {
5382 s = TPACPI_LED_BLINK;
5383 } else {
5384 return -EINVAL;
5387 rc = led_set_status(led, s);
5388 if (rc < 0)
5389 return rc;
5392 return 0;
5395 static struct ibm_struct led_driver_data = {
5396 .name = "led",
5397 .read = led_read,
5398 .write = led_write,
5399 .exit = led_exit,
5402 /*************************************************************************
5403 * Beep subdriver
5406 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5408 #define TPACPI_BEEP_Q1 0x0001
5410 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5411 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5412 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5415 static int __init beep_init(struct ibm_init_struct *iibm)
5417 unsigned long quirks;
5419 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5421 TPACPI_ACPIHANDLE_INIT(beep);
5423 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5424 str_supported(beep_handle != NULL));
5426 quirks = tpacpi_check_quirks(beep_quirk_table,
5427 ARRAY_SIZE(beep_quirk_table));
5429 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5431 return (beep_handle)? 0 : 1;
5434 static int beep_read(struct seq_file *m)
5436 if (!beep_handle)
5437 seq_printf(m, "status:\t\tnot supported\n");
5438 else {
5439 seq_printf(m, "status:\t\tsupported\n");
5440 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5443 return 0;
5446 static int beep_write(char *buf)
5448 char *cmd;
5449 int beep_cmd;
5451 if (!beep_handle)
5452 return -ENODEV;
5454 while ((cmd = next_cmd(&buf))) {
5455 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5456 beep_cmd >= 0 && beep_cmd <= 17) {
5457 /* beep_cmd set */
5458 } else
5459 return -EINVAL;
5460 if (tp_features.beep_needs_two_args) {
5461 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5462 beep_cmd, 0))
5463 return -EIO;
5464 } else {
5465 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5466 beep_cmd))
5467 return -EIO;
5471 return 0;
5474 static struct ibm_struct beep_driver_data = {
5475 .name = "beep",
5476 .read = beep_read,
5477 .write = beep_write,
5480 /*************************************************************************
5481 * Thermal subdriver
5484 enum thermal_access_mode {
5485 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5486 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5487 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5488 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5489 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5492 enum { /* TPACPI_THERMAL_TPEC_* */
5493 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5494 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5495 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5497 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5501 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5502 struct ibm_thermal_sensors_struct {
5503 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5506 static enum thermal_access_mode thermal_read_mode;
5508 /* idx is zero-based */
5509 static int thermal_get_sensor(int idx, s32 *value)
5511 int t;
5512 s8 tmp;
5513 char tmpi[5];
5515 t = TP_EC_THERMAL_TMP0;
5517 switch (thermal_read_mode) {
5518 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5519 case TPACPI_THERMAL_TPEC_16:
5520 if (idx >= 8 && idx <= 15) {
5521 t = TP_EC_THERMAL_TMP8;
5522 idx -= 8;
5524 /* fallthrough */
5525 #endif
5526 case TPACPI_THERMAL_TPEC_8:
5527 if (idx <= 7) {
5528 if (!acpi_ec_read(t + idx, &tmp))
5529 return -EIO;
5530 *value = tmp * 1000;
5531 return 0;
5533 break;
5535 case TPACPI_THERMAL_ACPI_UPDT:
5536 if (idx <= 7) {
5537 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5538 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5539 return -EIO;
5540 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5541 return -EIO;
5542 *value = (t - 2732) * 100;
5543 return 0;
5545 break;
5547 case TPACPI_THERMAL_ACPI_TMP07:
5548 if (idx <= 7) {
5549 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5550 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5551 return -EIO;
5552 if (t > 127 || t < -127)
5553 t = TP_EC_THERMAL_TMP_NA;
5554 *value = t * 1000;
5555 return 0;
5557 break;
5559 case TPACPI_THERMAL_NONE:
5560 default:
5561 return -ENOSYS;
5564 return -EINVAL;
5567 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5569 int res, i;
5570 int n;
5572 n = 8;
5573 i = 0;
5575 if (!s)
5576 return -EINVAL;
5578 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5579 n = 16;
5581 for (i = 0 ; i < n; i++) {
5582 res = thermal_get_sensor(i, &s->temp[i]);
5583 if (res)
5584 return res;
5587 return n;
5590 static void thermal_dump_all_sensors(void)
5592 int n, i;
5593 struct ibm_thermal_sensors_struct t;
5595 n = thermal_get_sensors(&t);
5596 if (n <= 0)
5597 return;
5599 printk(TPACPI_NOTICE
5600 "temperatures (Celsius):");
5602 for (i = 0; i < n; i++) {
5603 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5604 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5605 else
5606 printk(KERN_CONT " N/A");
5609 printk(KERN_CONT "\n");
5612 /* sysfs temp##_input -------------------------------------------------- */
5614 static ssize_t thermal_temp_input_show(struct device *dev,
5615 struct device_attribute *attr,
5616 char *buf)
5618 struct sensor_device_attribute *sensor_attr =
5619 to_sensor_dev_attr(attr);
5620 int idx = sensor_attr->index;
5621 s32 value;
5622 int res;
5624 res = thermal_get_sensor(idx, &value);
5625 if (res)
5626 return res;
5627 if (value == TPACPI_THERMAL_SENSOR_NA)
5628 return -ENXIO;
5630 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5633 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5634 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5635 thermal_temp_input_show, NULL, _idxB)
5637 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5638 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5639 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5640 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5641 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5642 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5643 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5644 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5645 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5646 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5647 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5648 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5649 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5650 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5651 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5652 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5653 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5656 #define THERMAL_ATTRS(X) \
5657 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5659 static struct attribute *thermal_temp_input_attr[] = {
5660 THERMAL_ATTRS(8),
5661 THERMAL_ATTRS(9),
5662 THERMAL_ATTRS(10),
5663 THERMAL_ATTRS(11),
5664 THERMAL_ATTRS(12),
5665 THERMAL_ATTRS(13),
5666 THERMAL_ATTRS(14),
5667 THERMAL_ATTRS(15),
5668 THERMAL_ATTRS(0),
5669 THERMAL_ATTRS(1),
5670 THERMAL_ATTRS(2),
5671 THERMAL_ATTRS(3),
5672 THERMAL_ATTRS(4),
5673 THERMAL_ATTRS(5),
5674 THERMAL_ATTRS(6),
5675 THERMAL_ATTRS(7),
5676 NULL
5679 static const struct attribute_group thermal_temp_input16_group = {
5680 .attrs = thermal_temp_input_attr
5683 static const struct attribute_group thermal_temp_input8_group = {
5684 .attrs = &thermal_temp_input_attr[8]
5687 #undef THERMAL_SENSOR_ATTR_TEMP
5688 #undef THERMAL_ATTRS
5690 /* --------------------------------------------------------------------- */
5692 static int __init thermal_init(struct ibm_init_struct *iibm)
5694 u8 t, ta1, ta2;
5695 int i;
5696 int acpi_tmp7;
5697 int res;
5699 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5701 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5703 if (thinkpad_id.ec_model) {
5705 * Direct EC access mode: sensors at registers
5706 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5707 * non-implemented, thermal sensors return 0x80 when
5708 * not available
5711 ta1 = ta2 = 0;
5712 for (i = 0; i < 8; i++) {
5713 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5714 ta1 |= t;
5715 } else {
5716 ta1 = 0;
5717 break;
5719 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5720 ta2 |= t;
5721 } else {
5722 ta1 = 0;
5723 break;
5726 if (ta1 == 0) {
5727 /* This is sheer paranoia, but we handle it anyway */
5728 if (acpi_tmp7) {
5729 printk(TPACPI_ERR
5730 "ThinkPad ACPI EC access misbehaving, "
5731 "falling back to ACPI TMPx access "
5732 "mode\n");
5733 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5734 } else {
5735 printk(TPACPI_ERR
5736 "ThinkPad ACPI EC access misbehaving, "
5737 "disabling thermal sensors access\n");
5738 thermal_read_mode = TPACPI_THERMAL_NONE;
5740 } else {
5741 thermal_read_mode =
5742 (ta2 != 0) ?
5743 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5745 } else if (acpi_tmp7) {
5746 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5747 /* 600e/x, 770e, 770x */
5748 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5749 } else {
5750 /* Standard ACPI TMPx access, max 8 sensors */
5751 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5753 } else {
5754 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5755 thermal_read_mode = TPACPI_THERMAL_NONE;
5758 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5759 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5760 thermal_read_mode);
5762 switch (thermal_read_mode) {
5763 case TPACPI_THERMAL_TPEC_16:
5764 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5765 &thermal_temp_input16_group);
5766 if (res)
5767 return res;
5768 break;
5769 case TPACPI_THERMAL_TPEC_8:
5770 case TPACPI_THERMAL_ACPI_TMP07:
5771 case TPACPI_THERMAL_ACPI_UPDT:
5772 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5773 &thermal_temp_input8_group);
5774 if (res)
5775 return res;
5776 break;
5777 case TPACPI_THERMAL_NONE:
5778 default:
5779 return 1;
5782 return 0;
5785 static void thermal_exit(void)
5787 switch (thermal_read_mode) {
5788 case TPACPI_THERMAL_TPEC_16:
5789 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5790 &thermal_temp_input16_group);
5791 break;
5792 case TPACPI_THERMAL_TPEC_8:
5793 case TPACPI_THERMAL_ACPI_TMP07:
5794 case TPACPI_THERMAL_ACPI_UPDT:
5795 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5796 &thermal_temp_input8_group);
5797 break;
5798 case TPACPI_THERMAL_NONE:
5799 default:
5800 break;
5804 static int thermal_read(struct seq_file *m)
5806 int n, i;
5807 struct ibm_thermal_sensors_struct t;
5809 n = thermal_get_sensors(&t);
5810 if (unlikely(n < 0))
5811 return n;
5813 seq_printf(m, "temperatures:\t");
5815 if (n > 0) {
5816 for (i = 0; i < (n - 1); i++)
5817 seq_printf(m, "%d ", t.temp[i] / 1000);
5818 seq_printf(m, "%d\n", t.temp[i] / 1000);
5819 } else
5820 seq_printf(m, "not supported\n");
5822 return 0;
5825 static struct ibm_struct thermal_driver_data = {
5826 .name = "thermal",
5827 .read = thermal_read,
5828 .exit = thermal_exit,
5831 /*************************************************************************
5832 * EC Dump subdriver
5835 static u8 ecdump_regs[256];
5837 static int ecdump_read(struct seq_file *m)
5839 int i, j;
5840 u8 v;
5842 seq_printf(m, "EC "
5843 " +00 +01 +02 +03 +04 +05 +06 +07"
5844 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5845 for (i = 0; i < 256; i += 16) {
5846 seq_printf(m, "EC 0x%02x:", i);
5847 for (j = 0; j < 16; j++) {
5848 if (!acpi_ec_read(i + j, &v))
5849 break;
5850 if (v != ecdump_regs[i + j])
5851 seq_printf(m, " *%02x", v);
5852 else
5853 seq_printf(m, " %02x", v);
5854 ecdump_regs[i + j] = v;
5856 seq_putc(m, '\n');
5857 if (j != 16)
5858 break;
5861 /* These are way too dangerous to advertise openly... */
5862 #if 0
5863 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5864 " (<offset> is 00-ff, <value> is 00-ff)\n");
5865 seq_printf(m, "commands:\t0x<offset> <value> "
5866 " (<offset> is 00-ff, <value> is 0-255)\n");
5867 #endif
5868 return 0;
5871 static int ecdump_write(char *buf)
5873 char *cmd;
5874 int i, v;
5876 while ((cmd = next_cmd(&buf))) {
5877 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5878 /* i and v set */
5879 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5880 /* i and v set */
5881 } else
5882 return -EINVAL;
5883 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5884 if (!acpi_ec_write(i, v))
5885 return -EIO;
5886 } else
5887 return -EINVAL;
5890 return 0;
5893 static struct ibm_struct ecdump_driver_data = {
5894 .name = "ecdump",
5895 .read = ecdump_read,
5896 .write = ecdump_write,
5897 .flags.experimental = 1,
5900 /*************************************************************************
5901 * Backlight/brightness subdriver
5904 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5907 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5908 * CMOS NVRAM byte 0x5E, bits 0-3.
5910 * EC HBRV (0x31) has the following layout
5911 * Bit 7: unknown function
5912 * Bit 6: unknown function
5913 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5914 * Bit 4: must be set to zero to avoid problems
5915 * Bit 3-0: backlight brightness level
5917 * brightness_get_raw returns status data in the HBRV layout
5919 * WARNING: The X61 has been verified to use HBRV for something else, so
5920 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5921 * testing on the very early *60 Lenovo models...
5924 enum {
5925 TP_EC_BACKLIGHT = 0x31,
5927 /* TP_EC_BACKLIGHT bitmasks */
5928 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5929 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5930 TP_EC_BACKLIGHT_MAPSW = 0x20,
5933 enum tpacpi_brightness_access_mode {
5934 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5935 TPACPI_BRGHT_MODE_EC, /* EC control */
5936 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5937 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5938 TPACPI_BRGHT_MODE_MAX
5941 static struct backlight_device *ibm_backlight_device;
5943 static enum tpacpi_brightness_access_mode brightness_mode =
5944 TPACPI_BRGHT_MODE_MAX;
5946 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5948 static struct mutex brightness_mutex;
5950 /* NVRAM brightness access,
5951 * call with brightness_mutex held! */
5952 static unsigned int tpacpi_brightness_nvram_get(void)
5954 u8 lnvram;
5956 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5957 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5958 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5959 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5961 return lnvram;
5964 static void tpacpi_brightness_checkpoint_nvram(void)
5966 u8 lec = 0;
5967 u8 b_nvram;
5969 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5970 return;
5972 vdbg_printk(TPACPI_DBG_BRGHT,
5973 "trying to checkpoint backlight level to NVRAM...\n");
5975 if (mutex_lock_killable(&brightness_mutex) < 0)
5976 return;
5978 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5979 goto unlock;
5980 lec &= TP_EC_BACKLIGHT_LVLMSK;
5981 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5983 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5984 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5985 /* NVRAM needs update */
5986 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5987 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5988 b_nvram |= lec;
5989 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5990 dbg_printk(TPACPI_DBG_BRGHT,
5991 "updated NVRAM backlight level to %u (0x%02x)\n",
5992 (unsigned int) lec, (unsigned int) b_nvram);
5993 } else
5994 vdbg_printk(TPACPI_DBG_BRGHT,
5995 "NVRAM backlight level already is %u (0x%02x)\n",
5996 (unsigned int) lec, (unsigned int) b_nvram);
5998 unlock:
5999 mutex_unlock(&brightness_mutex);
6003 /* call with brightness_mutex held! */
6004 static int tpacpi_brightness_get_raw(int *status)
6006 u8 lec = 0;
6008 switch (brightness_mode) {
6009 case TPACPI_BRGHT_MODE_UCMS_STEP:
6010 *status = tpacpi_brightness_nvram_get();
6011 return 0;
6012 case TPACPI_BRGHT_MODE_EC:
6013 case TPACPI_BRGHT_MODE_ECNVRAM:
6014 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6015 return -EIO;
6016 *status = lec;
6017 return 0;
6018 default:
6019 return -ENXIO;
6023 /* call with brightness_mutex held! */
6024 /* do NOT call with illegal backlight level value */
6025 static int tpacpi_brightness_set_ec(unsigned int value)
6027 u8 lec = 0;
6029 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6030 return -EIO;
6032 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6033 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6034 (value & TP_EC_BACKLIGHT_LVLMSK))))
6035 return -EIO;
6037 return 0;
6040 /* call with brightness_mutex held! */
6041 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6043 int cmos_cmd, inc;
6044 unsigned int current_value, i;
6046 current_value = tpacpi_brightness_nvram_get();
6048 if (value == current_value)
6049 return 0;
6051 cmos_cmd = (value > current_value) ?
6052 TP_CMOS_BRIGHTNESS_UP :
6053 TP_CMOS_BRIGHTNESS_DOWN;
6054 inc = (value > current_value) ? 1 : -1;
6056 for (i = current_value; i != value; i += inc)
6057 if (issue_thinkpad_cmos_command(cmos_cmd))
6058 return -EIO;
6060 return 0;
6063 /* May return EINTR which can always be mapped to ERESTARTSYS */
6064 static int brightness_set(unsigned int value)
6066 int res;
6068 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6069 value < 0)
6070 return -EINVAL;
6072 vdbg_printk(TPACPI_DBG_BRGHT,
6073 "set backlight level to %d\n", value);
6075 res = mutex_lock_killable(&brightness_mutex);
6076 if (res < 0)
6077 return res;
6079 switch (brightness_mode) {
6080 case TPACPI_BRGHT_MODE_EC:
6081 case TPACPI_BRGHT_MODE_ECNVRAM:
6082 res = tpacpi_brightness_set_ec(value);
6083 break;
6084 case TPACPI_BRGHT_MODE_UCMS_STEP:
6085 res = tpacpi_brightness_set_ucmsstep(value);
6086 break;
6087 default:
6088 res = -ENXIO;
6091 mutex_unlock(&brightness_mutex);
6092 return res;
6095 /* sysfs backlight class ----------------------------------------------- */
6097 static int brightness_update_status(struct backlight_device *bd)
6099 unsigned int level =
6100 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6101 bd->props.power == FB_BLANK_UNBLANK) ?
6102 bd->props.brightness : 0;
6104 dbg_printk(TPACPI_DBG_BRGHT,
6105 "backlight: attempt to set level to %d\n",
6106 level);
6108 /* it is the backlight class's job (caller) to handle
6109 * EINTR and other errors properly */
6110 return brightness_set(level);
6113 static int brightness_get(struct backlight_device *bd)
6115 int status, res;
6117 res = mutex_lock_killable(&brightness_mutex);
6118 if (res < 0)
6119 return 0;
6121 res = tpacpi_brightness_get_raw(&status);
6123 mutex_unlock(&brightness_mutex);
6125 if (res < 0)
6126 return 0;
6128 return status & TP_EC_BACKLIGHT_LVLMSK;
6131 static void tpacpi_brightness_notify_change(void)
6133 backlight_force_update(ibm_backlight_device,
6134 BACKLIGHT_UPDATE_HOTKEY);
6137 static struct backlight_ops ibm_backlight_data = {
6138 .get_brightness = brightness_get,
6139 .update_status = brightness_update_status,
6142 /* --------------------------------------------------------------------- */
6145 * These are only useful for models that have only one possibility
6146 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6147 * these quirks.
6149 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6150 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6151 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6153 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6154 /* Models with ATI GPUs known to require ECNVRAM mode */
6155 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6157 /* Models with ATI GPUs that can use ECNVRAM */
6158 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6159 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6160 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6161 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6163 /* Models with Intel Extreme Graphics 2 */
6164 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6165 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6166 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6168 /* Models with Intel GMA900 */
6169 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6170 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6171 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6174 static int __init brightness_init(struct ibm_init_struct *iibm)
6176 int b;
6177 unsigned long quirks;
6179 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6181 mutex_init(&brightness_mutex);
6183 quirks = tpacpi_check_quirks(brightness_quirk_table,
6184 ARRAY_SIZE(brightness_quirk_table));
6187 * We always attempt to detect acpi support, so as to switch
6188 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6189 * going to publish a backlight interface
6191 b = tpacpi_check_std_acpi_brightness_support();
6192 if (b > 0) {
6194 if (acpi_video_backlight_support()) {
6195 if (brightness_enable > 1) {
6196 printk(TPACPI_NOTICE
6197 "Standard ACPI backlight interface "
6198 "available, not loading native one.\n");
6199 return 1;
6200 } else if (brightness_enable == 1) {
6201 printk(TPACPI_NOTICE
6202 "Backlight control force enabled, even if standard "
6203 "ACPI backlight interface is available\n");
6205 } else {
6206 if (brightness_enable > 1) {
6207 printk(TPACPI_NOTICE
6208 "Standard ACPI backlight interface not "
6209 "available, thinkpad_acpi native "
6210 "brightness control enabled\n");
6215 if (!brightness_enable) {
6216 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6217 "brightness support disabled by "
6218 "module parameter\n");
6219 return 1;
6222 if (b > 16) {
6223 printk(TPACPI_ERR
6224 "Unsupported brightness interface, "
6225 "please contact %s\n", TPACPI_MAIL);
6226 return 1;
6228 if (b == 16)
6229 tp_features.bright_16levels = 1;
6232 * Check for module parameter bogosity, note that we
6233 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6234 * able to detect "unspecified"
6236 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6237 return -EINVAL;
6239 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6240 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6241 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6242 if (quirks & TPACPI_BRGHT_Q_EC)
6243 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6244 else
6245 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6247 dbg_printk(TPACPI_DBG_BRGHT,
6248 "driver auto-selected brightness_mode=%d\n",
6249 brightness_mode);
6252 /* Safety */
6253 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6254 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6255 brightness_mode == TPACPI_BRGHT_MODE_EC))
6256 return -EINVAL;
6258 if (tpacpi_brightness_get_raw(&b) < 0)
6259 return 1;
6261 if (tp_features.bright_16levels)
6262 printk(TPACPI_INFO
6263 "detected a 16-level brightness capable ThinkPad\n");
6265 ibm_backlight_device = backlight_device_register(
6266 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6267 &ibm_backlight_data);
6268 if (IS_ERR(ibm_backlight_device)) {
6269 int rc = PTR_ERR(ibm_backlight_device);
6270 ibm_backlight_device = NULL;
6271 printk(TPACPI_ERR "Could not register backlight device\n");
6272 return rc;
6274 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6275 "brightness is supported\n");
6277 if (quirks & TPACPI_BRGHT_Q_ASK) {
6278 printk(TPACPI_NOTICE
6279 "brightness: will use unverified default: "
6280 "brightness_mode=%d\n", brightness_mode);
6281 printk(TPACPI_NOTICE
6282 "brightness: please report to %s whether it works well "
6283 "or not on your ThinkPad\n", TPACPI_MAIL);
6286 ibm_backlight_device->props.max_brightness =
6287 (tp_features.bright_16levels)? 15 : 7;
6288 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6289 backlight_update_status(ibm_backlight_device);
6291 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6292 "brightness: registering brightness hotkeys "
6293 "as change notification\n");
6294 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6295 | TP_ACPI_HKEY_BRGHTUP_MASK
6296 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6297 return 0;
6300 static void brightness_suspend(pm_message_t state)
6302 tpacpi_brightness_checkpoint_nvram();
6305 static void brightness_shutdown(void)
6307 tpacpi_brightness_checkpoint_nvram();
6310 static void brightness_exit(void)
6312 if (ibm_backlight_device) {
6313 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6314 "calling backlight_device_unregister()\n");
6315 backlight_device_unregister(ibm_backlight_device);
6318 tpacpi_brightness_checkpoint_nvram();
6321 static int brightness_read(struct seq_file *m)
6323 int level;
6325 level = brightness_get(NULL);
6326 if (level < 0) {
6327 seq_printf(m, "level:\t\tunreadable\n");
6328 } else {
6329 seq_printf(m, "level:\t\t%d\n", level);
6330 seq_printf(m, "commands:\tup, down\n");
6331 seq_printf(m, "commands:\tlevel <level>"
6332 " (<level> is 0-%d)\n",
6333 (tp_features.bright_16levels) ? 15 : 7);
6336 return 0;
6339 static int brightness_write(char *buf)
6341 int level;
6342 int rc;
6343 char *cmd;
6344 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6346 level = brightness_get(NULL);
6347 if (level < 0)
6348 return level;
6350 while ((cmd = next_cmd(&buf))) {
6351 if (strlencmp(cmd, "up") == 0) {
6352 if (level < max_level)
6353 level++;
6354 } else if (strlencmp(cmd, "down") == 0) {
6355 if (level > 0)
6356 level--;
6357 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6358 level >= 0 && level <= max_level) {
6359 /* new level set */
6360 } else
6361 return -EINVAL;
6364 tpacpi_disclose_usertask("procfs brightness",
6365 "set level to %d\n", level);
6368 * Now we know what the final level should be, so we try to set it.
6369 * Doing it this way makes the syscall restartable in case of EINTR
6371 rc = brightness_set(level);
6372 if (!rc && ibm_backlight_device)
6373 backlight_force_update(ibm_backlight_device,
6374 BACKLIGHT_UPDATE_SYSFS);
6375 return (rc == -EINTR)? -ERESTARTSYS : rc;
6378 static struct ibm_struct brightness_driver_data = {
6379 .name = "brightness",
6380 .read = brightness_read,
6381 .write = brightness_write,
6382 .exit = brightness_exit,
6383 .suspend = brightness_suspend,
6384 .shutdown = brightness_shutdown,
6387 /*************************************************************************
6388 * Volume subdriver
6392 * IBM ThinkPads have a simple volume controller with MUTE gating.
6393 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6395 * Since the *61 series (and probably also the later *60 series), Lenovo
6396 * ThinkPads only implement the MUTE gate.
6398 * EC register 0x30
6399 * Bit 6: MUTE (1 mutes sound)
6400 * Bit 3-0: Volume
6401 * Other bits should be zero as far as we know.
6403 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6404 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6405 * such as bit 7 which is used to detect repeated presses of MUTE,
6406 * and we leave them unchanged.
6409 enum {
6410 TP_EC_AUDIO = 0x30,
6412 /* TP_EC_AUDIO bits */
6413 TP_EC_AUDIO_MUTESW = 6,
6415 /* TP_EC_AUDIO bitmasks */
6416 TP_EC_AUDIO_LVL_MSK = 0x0F,
6417 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6419 /* Maximum volume */
6420 TP_EC_VOLUME_MAX = 14,
6423 enum tpacpi_volume_access_mode {
6424 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6425 TPACPI_VOL_MODE_EC, /* Pure EC control */
6426 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6427 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6428 TPACPI_VOL_MODE_MAX
6431 enum tpacpi_volume_capabilities {
6432 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6433 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6434 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6435 TPACPI_VOL_CAP_MAX
6438 static enum tpacpi_volume_access_mode volume_mode =
6439 TPACPI_VOL_MODE_MAX;
6441 static enum tpacpi_volume_capabilities volume_capabilities;
6442 static int volume_control_allowed;
6445 * Used to syncronize writers to TP_EC_AUDIO and
6446 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6448 static struct mutex volume_mutex;
6450 static void tpacpi_volume_checkpoint_nvram(void)
6452 u8 lec = 0;
6453 u8 b_nvram;
6454 u8 ec_mask;
6456 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6457 return;
6458 if (!volume_control_allowed)
6459 return;
6461 vdbg_printk(TPACPI_DBG_MIXER,
6462 "trying to checkpoint mixer state to NVRAM...\n");
6464 if (tp_features.mixer_no_level_control)
6465 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6466 else
6467 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6469 if (mutex_lock_killable(&volume_mutex) < 0)
6470 return;
6472 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6473 goto unlock;
6474 lec &= ec_mask;
6475 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6477 if (lec != (b_nvram & ec_mask)) {
6478 /* NVRAM needs update */
6479 b_nvram &= ~ec_mask;
6480 b_nvram |= lec;
6481 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6482 dbg_printk(TPACPI_DBG_MIXER,
6483 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6484 (unsigned int) lec, (unsigned int) b_nvram);
6485 } else {
6486 vdbg_printk(TPACPI_DBG_MIXER,
6487 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6488 (unsigned int) lec, (unsigned int) b_nvram);
6491 unlock:
6492 mutex_unlock(&volume_mutex);
6495 static int volume_get_status_ec(u8 *status)
6497 u8 s;
6499 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6500 return -EIO;
6502 *status = s;
6504 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6506 return 0;
6509 static int volume_get_status(u8 *status)
6511 return volume_get_status_ec(status);
6514 static int volume_set_status_ec(const u8 status)
6516 if (!acpi_ec_write(TP_EC_AUDIO, status))
6517 return -EIO;
6519 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6521 return 0;
6524 static int volume_set_status(const u8 status)
6526 return volume_set_status_ec(status);
6529 static int volume_set_mute_ec(const bool mute)
6531 int rc;
6532 u8 s, n;
6534 if (mutex_lock_killable(&volume_mutex) < 0)
6535 return -EINTR;
6537 rc = volume_get_status_ec(&s);
6538 if (rc)
6539 goto unlock;
6541 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6542 s & ~TP_EC_AUDIO_MUTESW_MSK;
6544 if (n != s)
6545 rc = volume_set_status_ec(n);
6547 unlock:
6548 mutex_unlock(&volume_mutex);
6549 return rc;
6552 static int volume_set_mute(const bool mute)
6554 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6555 (mute) ? "" : "un");
6556 return volume_set_mute_ec(mute);
6559 static int volume_set_volume_ec(const u8 vol)
6561 int rc;
6562 u8 s, n;
6564 if (vol > TP_EC_VOLUME_MAX)
6565 return -EINVAL;
6567 if (mutex_lock_killable(&volume_mutex) < 0)
6568 return -EINTR;
6570 rc = volume_get_status_ec(&s);
6571 if (rc)
6572 goto unlock;
6574 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6576 if (n != s)
6577 rc = volume_set_status_ec(n);
6579 unlock:
6580 mutex_unlock(&volume_mutex);
6581 return rc;
6584 static int volume_set_volume(const u8 vol)
6586 dbg_printk(TPACPI_DBG_MIXER,
6587 "trying to set volume level to %hu\n", vol);
6588 return volume_set_volume_ec(vol);
6591 static void volume_suspend(pm_message_t state)
6593 tpacpi_volume_checkpoint_nvram();
6596 static void volume_shutdown(void)
6598 tpacpi_volume_checkpoint_nvram();
6601 static void volume_exit(void)
6603 tpacpi_volume_checkpoint_nvram();
6606 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6607 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6609 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6610 /* Whitelist volume level on all IBM by default */
6611 { .vendor = PCI_VENDOR_ID_IBM,
6612 .bios = TPACPI_MATCH_ANY,
6613 .ec = TPACPI_MATCH_ANY,
6614 .quirks = TPACPI_VOL_Q_LEVEL },
6616 /* Lenovo models with volume control (needs confirmation) */
6617 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6618 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6619 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6620 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6621 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6622 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6623 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6625 /* Whitelist mute-only on all Lenovo by default */
6626 { .vendor = PCI_VENDOR_ID_LENOVO,
6627 .bios = TPACPI_MATCH_ANY,
6628 .ec = TPACPI_MATCH_ANY,
6629 .quirks = TPACPI_VOL_Q_MUTEONLY }
6632 static int __init volume_init(struct ibm_init_struct *iibm)
6634 unsigned long quirks;
6636 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6638 mutex_init(&volume_mutex);
6641 * Check for module parameter bogosity, note that we
6642 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6643 * able to detect "unspecified"
6645 if (volume_mode > TPACPI_VOL_MODE_MAX)
6646 return -EINVAL;
6648 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6649 printk(TPACPI_ERR
6650 "UCMS step volume mode not implemented, "
6651 "please contact %s\n", TPACPI_MAIL);
6652 return 1;
6655 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6656 return -EINVAL;
6658 quirks = tpacpi_check_quirks(volume_quirk_table,
6659 ARRAY_SIZE(volume_quirk_table));
6661 switch (volume_capabilities) {
6662 case TPACPI_VOL_CAP_AUTO:
6663 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6664 tp_features.mixer_no_level_control = 1;
6665 else if (quirks & TPACPI_VOL_Q_LEVEL)
6666 tp_features.mixer_no_level_control = 0;
6667 else
6668 return 1; /* no mixer */
6669 break;
6670 case TPACPI_VOL_CAP_VOLMUTE:
6671 tp_features.mixer_no_level_control = 0;
6672 break;
6673 case TPACPI_VOL_CAP_MUTEONLY:
6674 tp_features.mixer_no_level_control = 1;
6675 break;
6676 default:
6677 return 1;
6680 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6681 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6682 "using user-supplied volume_capabilities=%d\n",
6683 volume_capabilities);
6685 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6686 volume_mode == TPACPI_VOL_MODE_MAX) {
6687 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6689 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6690 "driver auto-selected volume_mode=%d\n",
6691 volume_mode);
6692 } else {
6693 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6694 "using user-supplied volume_mode=%d\n",
6695 volume_mode);
6698 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6699 "mute is supported, volume control is %s\n",
6700 str_supported(!tp_features.mixer_no_level_control));
6702 printk(TPACPI_INFO
6703 "Console audio control enabled, mode: %s\n",
6704 (volume_control_allowed) ?
6705 "override (read/write)" :
6706 "monitor (read only)");
6708 return 0;
6711 static int volume_read(struct seq_file *m)
6713 u8 status;
6715 if (volume_get_status(&status) < 0) {
6716 seq_printf(m, "level:\t\tunreadable\n");
6717 } else {
6718 if (tp_features.mixer_no_level_control)
6719 seq_printf(m, "level:\t\tunsupported\n");
6720 else
6721 seq_printf(m, "level:\t\t%d\n",
6722 status & TP_EC_AUDIO_LVL_MSK);
6724 seq_printf(m, "mute:\t\t%s\n",
6725 onoff(status, TP_EC_AUDIO_MUTESW));
6727 if (volume_control_allowed) {
6728 seq_printf(m, "commands:\tunmute, mute\n");
6729 if (!tp_features.mixer_no_level_control) {
6730 seq_printf(m,
6731 "commands:\tup, down\n");
6732 seq_printf(m,
6733 "commands:\tlevel <level>"
6734 " (<level> is 0-%d)\n",
6735 TP_EC_VOLUME_MAX);
6740 return 0;
6743 static int volume_write(char *buf)
6745 u8 s;
6746 u8 new_level, new_mute;
6747 int l;
6748 char *cmd;
6749 int rc;
6752 * We do allow volume control at driver startup, so that the
6753 * user can set initial state through the volume=... parameter hack.
6755 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
6756 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
6757 tp_warned.volume_ctrl_forbidden = 1;
6758 printk(TPACPI_NOTICE
6759 "Console audio control in monitor mode, "
6760 "changes are not allowed.\n");
6761 printk(TPACPI_NOTICE
6762 "Use the volume_control=1 module parameter "
6763 "to enable volume control\n");
6765 return -EPERM;
6768 rc = volume_get_status(&s);
6769 if (rc < 0)
6770 return rc;
6772 new_level = s & TP_EC_AUDIO_LVL_MSK;
6773 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
6775 while ((cmd = next_cmd(&buf))) {
6776 if (!tp_features.mixer_no_level_control) {
6777 if (strlencmp(cmd, "up") == 0) {
6778 if (new_mute)
6779 new_mute = 0;
6780 else if (new_level < TP_EC_VOLUME_MAX)
6781 new_level++;
6782 continue;
6783 } else if (strlencmp(cmd, "down") == 0) {
6784 if (new_mute)
6785 new_mute = 0;
6786 else if (new_level > 0)
6787 new_level--;
6788 continue;
6789 } else if (sscanf(cmd, "level %u", &l) == 1 &&
6790 l >= 0 && l <= TP_EC_VOLUME_MAX) {
6791 new_level = l;
6792 continue;
6795 if (strlencmp(cmd, "mute") == 0)
6796 new_mute = TP_EC_AUDIO_MUTESW_MSK;
6797 else if (strlencmp(cmd, "unmute") == 0)
6798 new_mute = 0;
6799 else
6800 return -EINVAL;
6803 if (tp_features.mixer_no_level_control) {
6804 tpacpi_disclose_usertask("procfs volume", "%smute\n",
6805 new_mute ? "" : "un");
6806 rc = volume_set_mute(!!new_mute);
6807 } else {
6808 tpacpi_disclose_usertask("procfs volume",
6809 "%smute and set level to %d\n",
6810 new_mute ? "" : "un", new_level);
6811 rc = volume_set_status(new_mute | new_level);
6814 return (rc == -EINTR) ? -ERESTARTSYS : rc;
6817 static struct ibm_struct volume_driver_data = {
6818 .name = "volume",
6819 .read = volume_read,
6820 .write = volume_write,
6821 .exit = volume_exit,
6822 .suspend = volume_suspend,
6823 .shutdown = volume_shutdown,
6826 /*************************************************************************
6827 * Fan subdriver
6831 * FAN ACCESS MODES
6833 * TPACPI_FAN_RD_ACPI_GFAN:
6834 * ACPI GFAN method: returns fan level
6836 * see TPACPI_FAN_WR_ACPI_SFAN
6837 * EC 0x2f (HFSP) not available if GFAN exists
6839 * TPACPI_FAN_WR_ACPI_SFAN:
6840 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
6842 * EC 0x2f (HFSP) might be available *for reading*, but do not use
6843 * it for writing.
6845 * TPACPI_FAN_WR_TPEC:
6846 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
6847 * Supported on almost all ThinkPads
6849 * Fan speed changes of any sort (including those caused by the
6850 * disengaged mode) are usually done slowly by the firmware as the
6851 * maximum ammount of fan duty cycle change per second seems to be
6852 * limited.
6854 * Reading is not available if GFAN exists.
6855 * Writing is not available if SFAN exists.
6857 * Bits
6858 * 7 automatic mode engaged;
6859 * (default operation mode of the ThinkPad)
6860 * fan level is ignored in this mode.
6861 * 6 full speed mode (takes precedence over bit 7);
6862 * not available on all thinkpads. May disable
6863 * the tachometer while the fan controller ramps up
6864 * the speed (which can take up to a few *minutes*).
6865 * Speeds up fan to 100% duty-cycle, which is far above
6866 * the standard RPM levels. It is not impossible that
6867 * it could cause hardware damage.
6868 * 5-3 unused in some models. Extra bits for fan level
6869 * in others, but still useless as all values above
6870 * 7 map to the same speed as level 7 in these models.
6871 * 2-0 fan level (0..7 usually)
6872 * 0x00 = stop
6873 * 0x07 = max (set when temperatures critical)
6874 * Some ThinkPads may have other levels, see
6875 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6877 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6878 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6879 * does so, its initial value is meaningless (0x07).
6881 * For firmware bugs, refer to:
6882 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6884 * ----
6886 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6887 * Main fan tachometer reading (in RPM)
6889 * This register is present on all ThinkPads with a new-style EC, and
6890 * it is known not to be present on the A21m/e, and T22, as there is
6891 * something else in offset 0x84 according to the ACPI DSDT. Other
6892 * ThinkPads from this same time period (and earlier) probably lack the
6893 * tachometer as well.
6895 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6896 * was never fixed by IBM to report the EC firmware version string
6897 * probably support the tachometer (like the early X models), so
6898 * detecting it is quite hard. We need more data to know for sure.
6900 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6901 * might result.
6903 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6904 * mode.
6906 * For firmware bugs, refer to:
6907 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6909 * ----
6911 * ThinkPad EC register 0x31 bit 0 (only on select models)
6913 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6914 * show the speed of the main fan. When bit 0 of EC register 0x31
6915 * is one, the tachometer registers show the speed of the auxiliary
6916 * fan.
6918 * Fan control seems to affect both fans, regardless of the state
6919 * of this bit.
6921 * So far, only the firmware for the X60/X61 non-tablet versions
6922 * seem to support this (firmware TP-7M).
6924 * TPACPI_FAN_WR_ACPI_FANS:
6925 * ThinkPad X31, X40, X41. Not available in the X60.
6927 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6928 * high speed. ACPI DSDT seems to map these three speeds to levels
6929 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6930 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6932 * The speeds are stored on handles
6933 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6935 * There are three default speed sets, acessible as handles:
6936 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6938 * ACPI DSDT switches which set is in use depending on various
6939 * factors.
6941 * TPACPI_FAN_WR_TPEC is also available and should be used to
6942 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6943 * but the ACPI tables just mention level 7.
6946 enum { /* Fan control constants */
6947 fan_status_offset = 0x2f, /* EC register 0x2f */
6948 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6949 * 0x84 must be read before 0x85 */
6950 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6951 bit 0 selects which fan is active */
6953 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6954 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6956 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6959 enum fan_status_access_mode {
6960 TPACPI_FAN_NONE = 0, /* No fan status or control */
6961 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6962 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6965 enum fan_control_access_mode {
6966 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6967 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6968 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6969 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6972 enum fan_control_commands {
6973 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6974 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6975 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6976 * and also watchdog cmd */
6979 static int fan_control_allowed;
6981 static enum fan_status_access_mode fan_status_access_mode;
6982 static enum fan_control_access_mode fan_control_access_mode;
6983 static enum fan_control_commands fan_control_commands;
6985 static u8 fan_control_initial_status;
6986 static u8 fan_control_desired_level;
6987 static u8 fan_control_resume_level;
6988 static int fan_watchdog_maxinterval;
6990 static struct mutex fan_mutex;
6992 static void fan_watchdog_fire(struct work_struct *ignored);
6993 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6995 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6996 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6997 "\\FSPD", /* 600e/x, 770e, 770x */
6998 ); /* all others */
6999 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7000 "JFNS", /* 770x-JL */
7001 ); /* all others */
7004 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7005 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7006 * be in auto mode (0x80).
7008 * This is corrected by any write to HFSP either by the driver, or
7009 * by the firmware.
7011 * We assume 0x07 really means auto mode while this quirk is active,
7012 * as this is far more likely than the ThinkPad being in level 7,
7013 * which is only used by the firmware during thermal emergencies.
7015 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7016 * TP-70 (T43, R52), which are known to be buggy.
7019 static void fan_quirk1_setup(void)
7021 if (fan_control_initial_status == 0x07) {
7022 printk(TPACPI_NOTICE
7023 "fan_init: initial fan status is unknown, "
7024 "assuming it is in auto mode\n");
7025 tp_features.fan_ctrl_status_undef = 1;
7029 static void fan_quirk1_handle(u8 *fan_status)
7031 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7032 if (*fan_status != fan_control_initial_status) {
7033 /* something changed the HFSP regisnter since
7034 * driver init time, so it is not undefined
7035 * anymore */
7036 tp_features.fan_ctrl_status_undef = 0;
7037 } else {
7038 /* Return most likely status. In fact, it
7039 * might be the only possible status */
7040 *fan_status = TP_EC_FAN_AUTO;
7045 /* Select main fan on X60/X61, NOOP on others */
7046 static bool fan_select_fan1(void)
7048 if (tp_features.second_fan) {
7049 u8 val;
7051 if (ec_read(fan_select_offset, &val) < 0)
7052 return false;
7053 val &= 0xFEU;
7054 if (ec_write(fan_select_offset, val) < 0)
7055 return false;
7057 return true;
7060 /* Select secondary fan on X60/X61 */
7061 static bool fan_select_fan2(void)
7063 u8 val;
7065 if (!tp_features.second_fan)
7066 return false;
7068 if (ec_read(fan_select_offset, &val) < 0)
7069 return false;
7070 val |= 0x01U;
7071 if (ec_write(fan_select_offset, val) < 0)
7072 return false;
7074 return true;
7078 * Call with fan_mutex held
7080 static void fan_update_desired_level(u8 status)
7082 if ((status &
7083 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7084 if (status > 7)
7085 fan_control_desired_level = 7;
7086 else
7087 fan_control_desired_level = status;
7091 static int fan_get_status(u8 *status)
7093 u8 s;
7095 /* TODO:
7096 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7098 switch (fan_status_access_mode) {
7099 case TPACPI_FAN_RD_ACPI_GFAN:
7100 /* 570, 600e/x, 770e, 770x */
7102 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7103 return -EIO;
7105 if (likely(status))
7106 *status = s & 0x07;
7108 break;
7110 case TPACPI_FAN_RD_TPEC:
7111 /* all except 570, 600e/x, 770e, 770x */
7112 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7113 return -EIO;
7115 if (likely(status)) {
7116 *status = s;
7117 fan_quirk1_handle(status);
7120 break;
7122 default:
7123 return -ENXIO;
7126 return 0;
7129 static int fan_get_status_safe(u8 *status)
7131 int rc;
7132 u8 s;
7134 if (mutex_lock_killable(&fan_mutex))
7135 return -ERESTARTSYS;
7136 rc = fan_get_status(&s);
7137 if (!rc)
7138 fan_update_desired_level(s);
7139 mutex_unlock(&fan_mutex);
7141 if (status)
7142 *status = s;
7144 return rc;
7147 static int fan_get_speed(unsigned int *speed)
7149 u8 hi, lo;
7151 switch (fan_status_access_mode) {
7152 case TPACPI_FAN_RD_TPEC:
7153 /* all except 570, 600e/x, 770e, 770x */
7154 if (unlikely(!fan_select_fan1()))
7155 return -EIO;
7156 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7157 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7158 return -EIO;
7160 if (likely(speed))
7161 *speed = (hi << 8) | lo;
7163 break;
7165 default:
7166 return -ENXIO;
7169 return 0;
7172 static int fan2_get_speed(unsigned int *speed)
7174 u8 hi, lo;
7175 bool rc;
7177 switch (fan_status_access_mode) {
7178 case TPACPI_FAN_RD_TPEC:
7179 /* all except 570, 600e/x, 770e, 770x */
7180 if (unlikely(!fan_select_fan2()))
7181 return -EIO;
7182 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7183 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7184 fan_select_fan1(); /* play it safe */
7185 if (rc)
7186 return -EIO;
7188 if (likely(speed))
7189 *speed = (hi << 8) | lo;
7191 break;
7193 default:
7194 return -ENXIO;
7197 return 0;
7200 static int fan_set_level(int level)
7202 if (!fan_control_allowed)
7203 return -EPERM;
7205 switch (fan_control_access_mode) {
7206 case TPACPI_FAN_WR_ACPI_SFAN:
7207 if (level >= 0 && level <= 7) {
7208 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7209 return -EIO;
7210 } else
7211 return -EINVAL;
7212 break;
7214 case TPACPI_FAN_WR_ACPI_FANS:
7215 case TPACPI_FAN_WR_TPEC:
7216 if (!(level & TP_EC_FAN_AUTO) &&
7217 !(level & TP_EC_FAN_FULLSPEED) &&
7218 ((level < 0) || (level > 7)))
7219 return -EINVAL;
7221 /* safety net should the EC not support AUTO
7222 * or FULLSPEED mode bits and just ignore them */
7223 if (level & TP_EC_FAN_FULLSPEED)
7224 level |= 7; /* safety min speed 7 */
7225 else if (level & TP_EC_FAN_AUTO)
7226 level |= 4; /* safety min speed 4 */
7228 if (!acpi_ec_write(fan_status_offset, level))
7229 return -EIO;
7230 else
7231 tp_features.fan_ctrl_status_undef = 0;
7232 break;
7234 default:
7235 return -ENXIO;
7238 vdbg_printk(TPACPI_DBG_FAN,
7239 "fan control: set fan control register to 0x%02x\n", level);
7240 return 0;
7243 static int fan_set_level_safe(int level)
7245 int rc;
7247 if (!fan_control_allowed)
7248 return -EPERM;
7250 if (mutex_lock_killable(&fan_mutex))
7251 return -ERESTARTSYS;
7253 if (level == TPACPI_FAN_LAST_LEVEL)
7254 level = fan_control_desired_level;
7256 rc = fan_set_level(level);
7257 if (!rc)
7258 fan_update_desired_level(level);
7260 mutex_unlock(&fan_mutex);
7261 return rc;
7264 static int fan_set_enable(void)
7266 u8 s;
7267 int rc;
7269 if (!fan_control_allowed)
7270 return -EPERM;
7272 if (mutex_lock_killable(&fan_mutex))
7273 return -ERESTARTSYS;
7275 switch (fan_control_access_mode) {
7276 case TPACPI_FAN_WR_ACPI_FANS:
7277 case TPACPI_FAN_WR_TPEC:
7278 rc = fan_get_status(&s);
7279 if (rc < 0)
7280 break;
7282 /* Don't go out of emergency fan mode */
7283 if (s != 7) {
7284 s &= 0x07;
7285 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7288 if (!acpi_ec_write(fan_status_offset, s))
7289 rc = -EIO;
7290 else {
7291 tp_features.fan_ctrl_status_undef = 0;
7292 rc = 0;
7294 break;
7296 case TPACPI_FAN_WR_ACPI_SFAN:
7297 rc = fan_get_status(&s);
7298 if (rc < 0)
7299 break;
7301 s &= 0x07;
7303 /* Set fan to at least level 4 */
7304 s |= 4;
7306 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7307 rc = -EIO;
7308 else
7309 rc = 0;
7310 break;
7312 default:
7313 rc = -ENXIO;
7316 mutex_unlock(&fan_mutex);
7318 if (!rc)
7319 vdbg_printk(TPACPI_DBG_FAN,
7320 "fan control: set fan control register to 0x%02x\n",
7322 return rc;
7325 static int fan_set_disable(void)
7327 int rc;
7329 if (!fan_control_allowed)
7330 return -EPERM;
7332 if (mutex_lock_killable(&fan_mutex))
7333 return -ERESTARTSYS;
7335 rc = 0;
7336 switch (fan_control_access_mode) {
7337 case TPACPI_FAN_WR_ACPI_FANS:
7338 case TPACPI_FAN_WR_TPEC:
7339 if (!acpi_ec_write(fan_status_offset, 0x00))
7340 rc = -EIO;
7341 else {
7342 fan_control_desired_level = 0;
7343 tp_features.fan_ctrl_status_undef = 0;
7345 break;
7347 case TPACPI_FAN_WR_ACPI_SFAN:
7348 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7349 rc = -EIO;
7350 else
7351 fan_control_desired_level = 0;
7352 break;
7354 default:
7355 rc = -ENXIO;
7358 if (!rc)
7359 vdbg_printk(TPACPI_DBG_FAN,
7360 "fan control: set fan control register to 0\n");
7362 mutex_unlock(&fan_mutex);
7363 return rc;
7366 static int fan_set_speed(int speed)
7368 int rc;
7370 if (!fan_control_allowed)
7371 return -EPERM;
7373 if (mutex_lock_killable(&fan_mutex))
7374 return -ERESTARTSYS;
7376 rc = 0;
7377 switch (fan_control_access_mode) {
7378 case TPACPI_FAN_WR_ACPI_FANS:
7379 if (speed >= 0 && speed <= 65535) {
7380 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7381 speed, speed, speed))
7382 rc = -EIO;
7383 } else
7384 rc = -EINVAL;
7385 break;
7387 default:
7388 rc = -ENXIO;
7391 mutex_unlock(&fan_mutex);
7392 return rc;
7395 static void fan_watchdog_reset(void)
7397 static int fan_watchdog_active;
7399 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7400 return;
7402 if (fan_watchdog_active)
7403 cancel_delayed_work(&fan_watchdog_task);
7405 if (fan_watchdog_maxinterval > 0 &&
7406 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7407 fan_watchdog_active = 1;
7408 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7409 msecs_to_jiffies(fan_watchdog_maxinterval
7410 * 1000))) {
7411 printk(TPACPI_ERR
7412 "failed to queue the fan watchdog, "
7413 "watchdog will not trigger\n");
7415 } else
7416 fan_watchdog_active = 0;
7419 static void fan_watchdog_fire(struct work_struct *ignored)
7421 int rc;
7423 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7424 return;
7426 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7427 rc = fan_set_enable();
7428 if (rc < 0) {
7429 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7430 "will try again later...\n", -rc);
7431 /* reschedule for later */
7432 fan_watchdog_reset();
7437 * SYSFS fan layout: hwmon compatible (device)
7439 * pwm*_enable:
7440 * 0: "disengaged" mode
7441 * 1: manual mode
7442 * 2: native EC "auto" mode (recommended, hardware default)
7444 * pwm*: set speed in manual mode, ignored otherwise.
7445 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7446 * interpolation.
7448 * fan*_input: tachometer reading, RPM
7451 * SYSFS fan layout: extensions
7453 * fan_watchdog (driver):
7454 * fan watchdog interval in seconds, 0 disables (default), max 120
7457 /* sysfs fan pwm1_enable ----------------------------------------------- */
7458 static ssize_t fan_pwm1_enable_show(struct device *dev,
7459 struct device_attribute *attr,
7460 char *buf)
7462 int res, mode;
7463 u8 status;
7465 res = fan_get_status_safe(&status);
7466 if (res)
7467 return res;
7469 if (status & TP_EC_FAN_FULLSPEED) {
7470 mode = 0;
7471 } else if (status & TP_EC_FAN_AUTO) {
7472 mode = 2;
7473 } else
7474 mode = 1;
7476 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7479 static ssize_t fan_pwm1_enable_store(struct device *dev,
7480 struct device_attribute *attr,
7481 const char *buf, size_t count)
7483 unsigned long t;
7484 int res, level;
7486 if (parse_strtoul(buf, 2, &t))
7487 return -EINVAL;
7489 tpacpi_disclose_usertask("hwmon pwm1_enable",
7490 "set fan mode to %lu\n", t);
7492 switch (t) {
7493 case 0:
7494 level = TP_EC_FAN_FULLSPEED;
7495 break;
7496 case 1:
7497 level = TPACPI_FAN_LAST_LEVEL;
7498 break;
7499 case 2:
7500 level = TP_EC_FAN_AUTO;
7501 break;
7502 case 3:
7503 /* reserved for software-controlled auto mode */
7504 return -ENOSYS;
7505 default:
7506 return -EINVAL;
7509 res = fan_set_level_safe(level);
7510 if (res == -ENXIO)
7511 return -EINVAL;
7512 else if (res < 0)
7513 return res;
7515 fan_watchdog_reset();
7517 return count;
7520 static struct device_attribute dev_attr_fan_pwm1_enable =
7521 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7522 fan_pwm1_enable_show, fan_pwm1_enable_store);
7524 /* sysfs fan pwm1 ------------------------------------------------------ */
7525 static ssize_t fan_pwm1_show(struct device *dev,
7526 struct device_attribute *attr,
7527 char *buf)
7529 int res;
7530 u8 status;
7532 res = fan_get_status_safe(&status);
7533 if (res)
7534 return res;
7536 if ((status &
7537 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7538 status = fan_control_desired_level;
7540 if (status > 7)
7541 status = 7;
7543 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7546 static ssize_t fan_pwm1_store(struct device *dev,
7547 struct device_attribute *attr,
7548 const char *buf, size_t count)
7550 unsigned long s;
7551 int rc;
7552 u8 status, newlevel;
7554 if (parse_strtoul(buf, 255, &s))
7555 return -EINVAL;
7557 tpacpi_disclose_usertask("hwmon pwm1",
7558 "set fan speed to %lu\n", s);
7560 /* scale down from 0-255 to 0-7 */
7561 newlevel = (s >> 5) & 0x07;
7563 if (mutex_lock_killable(&fan_mutex))
7564 return -ERESTARTSYS;
7566 rc = fan_get_status(&status);
7567 if (!rc && (status &
7568 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7569 rc = fan_set_level(newlevel);
7570 if (rc == -ENXIO)
7571 rc = -EINVAL;
7572 else if (!rc) {
7573 fan_update_desired_level(newlevel);
7574 fan_watchdog_reset();
7578 mutex_unlock(&fan_mutex);
7579 return (rc)? rc : count;
7582 static struct device_attribute dev_attr_fan_pwm1 =
7583 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7584 fan_pwm1_show, fan_pwm1_store);
7586 /* sysfs fan fan1_input ------------------------------------------------ */
7587 static ssize_t fan_fan1_input_show(struct device *dev,
7588 struct device_attribute *attr,
7589 char *buf)
7591 int res;
7592 unsigned int speed;
7594 res = fan_get_speed(&speed);
7595 if (res < 0)
7596 return res;
7598 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7601 static struct device_attribute dev_attr_fan_fan1_input =
7602 __ATTR(fan1_input, S_IRUGO,
7603 fan_fan1_input_show, NULL);
7605 /* sysfs fan fan2_input ------------------------------------------------ */
7606 static ssize_t fan_fan2_input_show(struct device *dev,
7607 struct device_attribute *attr,
7608 char *buf)
7610 int res;
7611 unsigned int speed;
7613 res = fan2_get_speed(&speed);
7614 if (res < 0)
7615 return res;
7617 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7620 static struct device_attribute dev_attr_fan_fan2_input =
7621 __ATTR(fan2_input, S_IRUGO,
7622 fan_fan2_input_show, NULL);
7624 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7625 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7626 char *buf)
7628 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7631 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7632 const char *buf, size_t count)
7634 unsigned long t;
7636 if (parse_strtoul(buf, 120, &t))
7637 return -EINVAL;
7639 if (!fan_control_allowed)
7640 return -EPERM;
7642 fan_watchdog_maxinterval = t;
7643 fan_watchdog_reset();
7645 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7647 return count;
7650 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7651 fan_fan_watchdog_show, fan_fan_watchdog_store);
7653 /* --------------------------------------------------------------------- */
7654 static struct attribute *fan_attributes[] = {
7655 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7656 &dev_attr_fan_fan1_input.attr,
7657 NULL, /* for fan2_input */
7658 NULL
7661 static const struct attribute_group fan_attr_group = {
7662 .attrs = fan_attributes,
7665 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7666 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7668 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7669 { .vendor = PCI_VENDOR_ID_IBM, \
7670 .bios = TPACPI_MATCH_ANY, \
7671 .ec = TPID(__id1, __id2), \
7672 .quirks = __quirks }
7674 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7675 { .vendor = PCI_VENDOR_ID_LENOVO, \
7676 .bios = TPACPI_MATCH_ANY, \
7677 .ec = TPID(__id1, __id2), \
7678 .quirks = __quirks }
7680 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7681 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7682 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7683 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7684 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7685 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7688 #undef TPACPI_FAN_QL
7689 #undef TPACPI_FAN_QI
7691 static int __init fan_init(struct ibm_init_struct *iibm)
7693 int rc;
7694 unsigned long quirks;
7696 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7697 "initializing fan subdriver\n");
7699 mutex_init(&fan_mutex);
7700 fan_status_access_mode = TPACPI_FAN_NONE;
7701 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7702 fan_control_commands = 0;
7703 fan_watchdog_maxinterval = 0;
7704 tp_features.fan_ctrl_status_undef = 0;
7705 tp_features.second_fan = 0;
7706 fan_control_desired_level = 7;
7708 TPACPI_ACPIHANDLE_INIT(fans);
7709 TPACPI_ACPIHANDLE_INIT(gfan);
7710 TPACPI_ACPIHANDLE_INIT(sfan);
7712 quirks = tpacpi_check_quirks(fan_quirk_table,
7713 ARRAY_SIZE(fan_quirk_table));
7715 if (gfan_handle) {
7716 /* 570, 600e/x, 770e, 770x */
7717 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7718 } else {
7719 /* all other ThinkPads: note that even old-style
7720 * ThinkPad ECs supports the fan control register */
7721 if (likely(acpi_ec_read(fan_status_offset,
7722 &fan_control_initial_status))) {
7723 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7724 if (quirks & TPACPI_FAN_Q1)
7725 fan_quirk1_setup();
7726 if (quirks & TPACPI_FAN_2FAN) {
7727 tp_features.second_fan = 1;
7728 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7729 "secondary fan support enabled\n");
7731 } else {
7732 printk(TPACPI_ERR
7733 "ThinkPad ACPI EC access misbehaving, "
7734 "fan status and control unavailable\n");
7735 return 1;
7739 if (sfan_handle) {
7740 /* 570, 770x-JL */
7741 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7742 fan_control_commands |=
7743 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7744 } else {
7745 if (!gfan_handle) {
7746 /* gfan without sfan means no fan control */
7747 /* all other models implement TP EC 0x2f control */
7749 if (fans_handle) {
7750 /* X31, X40, X41 */
7751 fan_control_access_mode =
7752 TPACPI_FAN_WR_ACPI_FANS;
7753 fan_control_commands |=
7754 TPACPI_FAN_CMD_SPEED |
7755 TPACPI_FAN_CMD_LEVEL |
7756 TPACPI_FAN_CMD_ENABLE;
7757 } else {
7758 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
7759 fan_control_commands |=
7760 TPACPI_FAN_CMD_LEVEL |
7761 TPACPI_FAN_CMD_ENABLE;
7766 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7767 "fan is %s, modes %d, %d\n",
7768 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
7769 fan_control_access_mode != TPACPI_FAN_WR_NONE),
7770 fan_status_access_mode, fan_control_access_mode);
7772 /* fan control master switch */
7773 if (!fan_control_allowed) {
7774 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7775 fan_control_commands = 0;
7776 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7777 "fan control features disabled by parameter\n");
7780 /* update fan_control_desired_level */
7781 if (fan_status_access_mode != TPACPI_FAN_NONE)
7782 fan_get_status_safe(NULL);
7784 if (fan_status_access_mode != TPACPI_FAN_NONE ||
7785 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
7786 if (tp_features.second_fan) {
7787 /* attach second fan tachometer */
7788 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
7789 &dev_attr_fan_fan2_input.attr;
7791 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
7792 &fan_attr_group);
7793 if (rc < 0)
7794 return rc;
7796 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
7797 &driver_attr_fan_watchdog);
7798 if (rc < 0) {
7799 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
7800 &fan_attr_group);
7801 return rc;
7803 return 0;
7804 } else
7805 return 1;
7808 static void fan_exit(void)
7810 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
7811 "cancelling any pending fan watchdog tasks\n");
7813 /* FIXME: can we really do this unconditionally? */
7814 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
7815 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
7816 &driver_attr_fan_watchdog);
7818 cancel_delayed_work(&fan_watchdog_task);
7819 flush_workqueue(tpacpi_wq);
7822 static void fan_suspend(pm_message_t state)
7824 int rc;
7826 if (!fan_control_allowed)
7827 return;
7829 /* Store fan status in cache */
7830 fan_control_resume_level = 0;
7831 rc = fan_get_status_safe(&fan_control_resume_level);
7832 if (rc < 0)
7833 printk(TPACPI_NOTICE
7834 "failed to read fan level for later "
7835 "restore during resume: %d\n", rc);
7837 /* if it is undefined, don't attempt to restore it.
7838 * KEEP THIS LAST */
7839 if (tp_features.fan_ctrl_status_undef)
7840 fan_control_resume_level = 0;
7843 static void fan_resume(void)
7845 u8 current_level = 7;
7846 bool do_set = false;
7847 int rc;
7849 /* DSDT *always* updates status on resume */
7850 tp_features.fan_ctrl_status_undef = 0;
7852 if (!fan_control_allowed ||
7853 !fan_control_resume_level ||
7854 (fan_get_status_safe(&current_level) < 0))
7855 return;
7857 switch (fan_control_access_mode) {
7858 case TPACPI_FAN_WR_ACPI_SFAN:
7859 /* never decrease fan level */
7860 do_set = (fan_control_resume_level > current_level);
7861 break;
7862 case TPACPI_FAN_WR_ACPI_FANS:
7863 case TPACPI_FAN_WR_TPEC:
7864 /* never decrease fan level, scale is:
7865 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7867 * We expect the firmware to set either 7 or AUTO, but we
7868 * handle FULLSPEED out of paranoia.
7870 * So, we can safely only restore FULLSPEED or 7, anything
7871 * else could slow the fan. Restoring AUTO is useless, at
7872 * best that's exactly what the DSDT already set (it is the
7873 * slower it uses).
7875 * Always keep in mind that the DSDT *will* have set the
7876 * fans to what the vendor supposes is the best level. We
7877 * muck with it only to speed the fan up.
7879 if (fan_control_resume_level != 7 &&
7880 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7881 return;
7882 else
7883 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7884 (current_level != fan_control_resume_level);
7885 break;
7886 default:
7887 return;
7889 if (do_set) {
7890 printk(TPACPI_NOTICE
7891 "restoring fan level to 0x%02x\n",
7892 fan_control_resume_level);
7893 rc = fan_set_level_safe(fan_control_resume_level);
7894 if (rc < 0)
7895 printk(TPACPI_NOTICE
7896 "failed to restore fan level: %d\n", rc);
7900 static int fan_read(struct seq_file *m)
7902 int rc;
7903 u8 status;
7904 unsigned int speed = 0;
7906 switch (fan_status_access_mode) {
7907 case TPACPI_FAN_RD_ACPI_GFAN:
7908 /* 570, 600e/x, 770e, 770x */
7909 rc = fan_get_status_safe(&status);
7910 if (rc < 0)
7911 return rc;
7913 seq_printf(m, "status:\t\t%s\n"
7914 "level:\t\t%d\n",
7915 (status != 0) ? "enabled" : "disabled", status);
7916 break;
7918 case TPACPI_FAN_RD_TPEC:
7919 /* all except 570, 600e/x, 770e, 770x */
7920 rc = fan_get_status_safe(&status);
7921 if (rc < 0)
7922 return rc;
7924 seq_printf(m, "status:\t\t%s\n",
7925 (status != 0) ? "enabled" : "disabled");
7927 rc = fan_get_speed(&speed);
7928 if (rc < 0)
7929 return rc;
7931 seq_printf(m, "speed:\t\t%d\n", speed);
7933 if (status & TP_EC_FAN_FULLSPEED)
7934 /* Disengaged mode takes precedence */
7935 seq_printf(m, "level:\t\tdisengaged\n");
7936 else if (status & TP_EC_FAN_AUTO)
7937 seq_printf(m, "level:\t\tauto\n");
7938 else
7939 seq_printf(m, "level:\t\t%d\n", status);
7940 break;
7942 case TPACPI_FAN_NONE:
7943 default:
7944 seq_printf(m, "status:\t\tnot supported\n");
7947 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7948 seq_printf(m, "commands:\tlevel <level>");
7950 switch (fan_control_access_mode) {
7951 case TPACPI_FAN_WR_ACPI_SFAN:
7952 seq_printf(m, " (<level> is 0-7)\n");
7953 break;
7955 default:
7956 seq_printf(m, " (<level> is 0-7, "
7957 "auto, disengaged, full-speed)\n");
7958 break;
7962 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7963 seq_printf(m, "commands:\tenable, disable\n"
7964 "commands:\twatchdog <timeout> (<timeout> "
7965 "is 0 (off), 1-120 (seconds))\n");
7967 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7968 seq_printf(m, "commands:\tspeed <speed>"
7969 " (<speed> is 0-65535)\n");
7971 return 0;
7974 static int fan_write_cmd_level(const char *cmd, int *rc)
7976 int level;
7978 if (strlencmp(cmd, "level auto") == 0)
7979 level = TP_EC_FAN_AUTO;
7980 else if ((strlencmp(cmd, "level disengaged") == 0) |
7981 (strlencmp(cmd, "level full-speed") == 0))
7982 level = TP_EC_FAN_FULLSPEED;
7983 else if (sscanf(cmd, "level %d", &level) != 1)
7984 return 0;
7986 *rc = fan_set_level_safe(level);
7987 if (*rc == -ENXIO)
7988 printk(TPACPI_ERR "level command accepted for unsupported "
7989 "access mode %d", fan_control_access_mode);
7990 else if (!*rc)
7991 tpacpi_disclose_usertask("procfs fan",
7992 "set level to %d\n", level);
7994 return 1;
7997 static int fan_write_cmd_enable(const char *cmd, int *rc)
7999 if (strlencmp(cmd, "enable") != 0)
8000 return 0;
8002 *rc = fan_set_enable();
8003 if (*rc == -ENXIO)
8004 printk(TPACPI_ERR "enable command accepted for unsupported "
8005 "access mode %d", fan_control_access_mode);
8006 else if (!*rc)
8007 tpacpi_disclose_usertask("procfs fan", "enable\n");
8009 return 1;
8012 static int fan_write_cmd_disable(const char *cmd, int *rc)
8014 if (strlencmp(cmd, "disable") != 0)
8015 return 0;
8017 *rc = fan_set_disable();
8018 if (*rc == -ENXIO)
8019 printk(TPACPI_ERR "disable command accepted for unsupported "
8020 "access mode %d", fan_control_access_mode);
8021 else if (!*rc)
8022 tpacpi_disclose_usertask("procfs fan", "disable\n");
8024 return 1;
8027 static int fan_write_cmd_speed(const char *cmd, int *rc)
8029 int speed;
8031 /* TODO:
8032 * Support speed <low> <medium> <high> ? */
8034 if (sscanf(cmd, "speed %d", &speed) != 1)
8035 return 0;
8037 *rc = fan_set_speed(speed);
8038 if (*rc == -ENXIO)
8039 printk(TPACPI_ERR "speed command accepted for unsupported "
8040 "access mode %d", fan_control_access_mode);
8041 else if (!*rc)
8042 tpacpi_disclose_usertask("procfs fan",
8043 "set speed to %d\n", speed);
8045 return 1;
8048 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8050 int interval;
8052 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8053 return 0;
8055 if (interval < 0 || interval > 120)
8056 *rc = -EINVAL;
8057 else {
8058 fan_watchdog_maxinterval = interval;
8059 tpacpi_disclose_usertask("procfs fan",
8060 "set watchdog timer to %d\n",
8061 interval);
8064 return 1;
8067 static int fan_write(char *buf)
8069 char *cmd;
8070 int rc = 0;
8072 while (!rc && (cmd = next_cmd(&buf))) {
8073 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8074 fan_write_cmd_level(cmd, &rc)) &&
8075 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8076 (fan_write_cmd_enable(cmd, &rc) ||
8077 fan_write_cmd_disable(cmd, &rc) ||
8078 fan_write_cmd_watchdog(cmd, &rc))) &&
8079 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8080 fan_write_cmd_speed(cmd, &rc))
8082 rc = -EINVAL;
8083 else if (!rc)
8084 fan_watchdog_reset();
8087 return rc;
8090 static struct ibm_struct fan_driver_data = {
8091 .name = "fan",
8092 .read = fan_read,
8093 .write = fan_write,
8094 .exit = fan_exit,
8095 .suspend = fan_suspend,
8096 .resume = fan_resume,
8099 /****************************************************************************
8100 ****************************************************************************
8102 * Infrastructure
8104 ****************************************************************************
8105 ****************************************************************************/
8108 * HKEY event callout for other subdrivers go here
8109 * (yes, it is ugly, but it is quick, safe, and gets the job done
8111 static void tpacpi_driver_event(const unsigned int hkey_event)
8113 if (ibm_backlight_device) {
8114 switch (hkey_event) {
8115 case TP_HKEY_EV_BRGHT_UP:
8116 case TP_HKEY_EV_BRGHT_DOWN:
8117 tpacpi_brightness_notify_change();
8124 static void hotkey_driver_event(const unsigned int scancode)
8126 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8129 /* sysfs name ---------------------------------------------------------- */
8130 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8131 struct device_attribute *attr,
8132 char *buf)
8134 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8137 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8138 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8140 /* --------------------------------------------------------------------- */
8142 /* /proc support */
8143 static struct proc_dir_entry *proc_dir;
8146 * Module and infrastructure proble, init and exit handling
8149 static int force_load;
8151 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8152 static const char * __init str_supported(int is_supported)
8154 static char text_unsupported[] __initdata = "not supported";
8156 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8158 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8160 static void ibm_exit(struct ibm_struct *ibm)
8162 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8164 list_del_init(&ibm->all_drivers);
8166 if (ibm->flags.acpi_notify_installed) {
8167 dbg_printk(TPACPI_DBG_EXIT,
8168 "%s: acpi_remove_notify_handler\n", ibm->name);
8169 BUG_ON(!ibm->acpi);
8170 acpi_remove_notify_handler(*ibm->acpi->handle,
8171 ibm->acpi->type,
8172 dispatch_acpi_notify);
8173 ibm->flags.acpi_notify_installed = 0;
8174 ibm->flags.acpi_notify_installed = 0;
8177 if (ibm->flags.proc_created) {
8178 dbg_printk(TPACPI_DBG_EXIT,
8179 "%s: remove_proc_entry\n", ibm->name);
8180 remove_proc_entry(ibm->name, proc_dir);
8181 ibm->flags.proc_created = 0;
8184 if (ibm->flags.acpi_driver_registered) {
8185 dbg_printk(TPACPI_DBG_EXIT,
8186 "%s: acpi_bus_unregister_driver\n", ibm->name);
8187 BUG_ON(!ibm->acpi);
8188 acpi_bus_unregister_driver(ibm->acpi->driver);
8189 kfree(ibm->acpi->driver);
8190 ibm->acpi->driver = NULL;
8191 ibm->flags.acpi_driver_registered = 0;
8194 if (ibm->flags.init_called && ibm->exit) {
8195 ibm->exit();
8196 ibm->flags.init_called = 0;
8199 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8202 static int __init ibm_init(struct ibm_init_struct *iibm)
8204 int ret;
8205 struct ibm_struct *ibm = iibm->data;
8206 struct proc_dir_entry *entry;
8208 BUG_ON(ibm == NULL);
8210 INIT_LIST_HEAD(&ibm->all_drivers);
8212 if (ibm->flags.experimental && !experimental)
8213 return 0;
8215 dbg_printk(TPACPI_DBG_INIT,
8216 "probing for %s\n", ibm->name);
8218 if (iibm->init) {
8219 ret = iibm->init(iibm);
8220 if (ret > 0)
8221 return 0; /* probe failed */
8222 if (ret)
8223 return ret;
8225 ibm->flags.init_called = 1;
8228 if (ibm->acpi) {
8229 if (ibm->acpi->hid) {
8230 ret = register_tpacpi_subdriver(ibm);
8231 if (ret)
8232 goto err_out;
8235 if (ibm->acpi->notify) {
8236 ret = setup_acpi_notify(ibm);
8237 if (ret == -ENODEV) {
8238 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8239 ibm->name);
8240 ret = 0;
8241 goto err_out;
8243 if (ret < 0)
8244 goto err_out;
8248 dbg_printk(TPACPI_DBG_INIT,
8249 "%s installed\n", ibm->name);
8251 if (ibm->read) {
8252 mode_t mode = iibm->base_procfs_mode;
8254 if (!mode)
8255 mode = S_IRUGO;
8256 if (ibm->write)
8257 mode |= S_IWUSR;
8258 entry = proc_create_data(ibm->name, mode, proc_dir,
8259 &dispatch_proc_fops, ibm);
8260 if (!entry) {
8261 printk(TPACPI_ERR "unable to create proc entry %s\n",
8262 ibm->name);
8263 ret = -ENODEV;
8264 goto err_out;
8266 ibm->flags.proc_created = 1;
8269 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8271 return 0;
8273 err_out:
8274 dbg_printk(TPACPI_DBG_INIT,
8275 "%s: at error exit path with result %d\n",
8276 ibm->name, ret);
8278 ibm_exit(ibm);
8279 return (ret < 0)? ret : 0;
8282 /* Probing */
8284 static bool __pure __init tpacpi_is_fw_digit(const char c)
8286 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8289 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8290 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8291 const char t)
8293 return s && strlen(s) >= 8 &&
8294 tpacpi_is_fw_digit(s[0]) &&
8295 tpacpi_is_fw_digit(s[1]) &&
8296 s[2] == t && s[3] == 'T' &&
8297 tpacpi_is_fw_digit(s[4]) &&
8298 tpacpi_is_fw_digit(s[5]) &&
8299 s[6] == 'W' && s[7] == 'W';
8302 /* returns 0 - probe ok, or < 0 - probe error.
8303 * Probe ok doesn't mean thinkpad found.
8304 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8305 static int __must_check __init get_thinkpad_model_data(
8306 struct thinkpad_id_data *tp)
8308 const struct dmi_device *dev = NULL;
8309 char ec_fw_string[18];
8310 char const *s;
8312 if (!tp)
8313 return -EINVAL;
8315 memset(tp, 0, sizeof(*tp));
8317 if (dmi_name_in_vendors("IBM"))
8318 tp->vendor = PCI_VENDOR_ID_IBM;
8319 else if (dmi_name_in_vendors("LENOVO"))
8320 tp->vendor = PCI_VENDOR_ID_LENOVO;
8321 else
8322 return 0;
8324 s = dmi_get_system_info(DMI_BIOS_VERSION);
8325 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8326 if (s && !tp->bios_version_str)
8327 return -ENOMEM;
8329 /* Really ancient ThinkPad 240X will fail this, which is fine */
8330 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8331 return 0;
8333 tp->bios_model = tp->bios_version_str[0]
8334 | (tp->bios_version_str[1] << 8);
8335 tp->bios_release = (tp->bios_version_str[4] << 8)
8336 | tp->bios_version_str[5];
8339 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8340 * X32 or newer, all Z series; Some models must have an
8341 * up-to-date BIOS or they will not be detected.
8343 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8345 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8346 if (sscanf(dev->name,
8347 "IBM ThinkPad Embedded Controller -[%17c",
8348 ec_fw_string) == 1) {
8349 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8350 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8352 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8353 if (!tp->ec_version_str)
8354 return -ENOMEM;
8356 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8357 tp->ec_model = ec_fw_string[0]
8358 | (ec_fw_string[1] << 8);
8359 tp->ec_release = (ec_fw_string[4] << 8)
8360 | ec_fw_string[5];
8361 } else {
8362 printk(TPACPI_NOTICE
8363 "ThinkPad firmware release %s "
8364 "doesn't match the known patterns\n",
8365 ec_fw_string);
8366 printk(TPACPI_NOTICE
8367 "please report this to %s\n",
8368 TPACPI_MAIL);
8370 break;
8374 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8375 if (s && !strnicmp(s, "ThinkPad", 8)) {
8376 tp->model_str = kstrdup(s, GFP_KERNEL);
8377 if (!tp->model_str)
8378 return -ENOMEM;
8381 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8382 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8383 if (s && !tp->nummodel_str)
8384 return -ENOMEM;
8386 return 0;
8389 static int __init probe_for_thinkpad(void)
8391 int is_thinkpad;
8393 if (acpi_disabled)
8394 return -ENODEV;
8397 * Non-ancient models have better DMI tagging, but very old models
8398 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8400 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8401 (thinkpad_id.ec_model != 0) ||
8402 tpacpi_is_fw_known();
8404 /* ec is required because many other handles are relative to it */
8405 TPACPI_ACPIHANDLE_INIT(ec);
8406 if (!ec_handle) {
8407 if (is_thinkpad)
8408 printk(TPACPI_ERR
8409 "Not yet supported ThinkPad detected!\n");
8410 return -ENODEV;
8413 if (!is_thinkpad && !force_load)
8414 return -ENODEV;
8416 return 0;
8420 /* Module init, exit, parameters */
8422 static struct ibm_init_struct ibms_init[] __initdata = {
8424 .init = thinkpad_acpi_driver_init,
8425 .data = &thinkpad_acpi_driver_data,
8428 .init = hotkey_init,
8429 .data = &hotkey_driver_data,
8432 .init = bluetooth_init,
8433 .data = &bluetooth_driver_data,
8436 .init = wan_init,
8437 .data = &wan_driver_data,
8440 .init = uwb_init,
8441 .data = &uwb_driver_data,
8443 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8445 .init = video_init,
8446 .base_procfs_mode = S_IRUSR,
8447 .data = &video_driver_data,
8449 #endif
8451 .init = light_init,
8452 .data = &light_driver_data,
8455 .init = cmos_init,
8456 .data = &cmos_driver_data,
8459 .init = led_init,
8460 .data = &led_driver_data,
8463 .init = beep_init,
8464 .data = &beep_driver_data,
8467 .init = thermal_init,
8468 .data = &thermal_driver_data,
8471 .data = &ecdump_driver_data,
8474 .init = brightness_init,
8475 .data = &brightness_driver_data,
8478 .init = volume_init,
8479 .data = &volume_driver_data,
8482 .init = fan_init,
8483 .data = &fan_driver_data,
8487 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8489 unsigned int i;
8490 struct ibm_struct *ibm;
8492 if (!kp || !kp->name || !val)
8493 return -EINVAL;
8495 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8496 ibm = ibms_init[i].data;
8497 WARN_ON(ibm == NULL);
8499 if (!ibm || !ibm->name)
8500 continue;
8502 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8503 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8504 return -ENOSPC;
8505 strcpy(ibms_init[i].param, val);
8506 strcat(ibms_init[i].param, ",");
8507 return 0;
8511 return -EINVAL;
8514 module_param(experimental, int, 0444);
8515 MODULE_PARM_DESC(experimental,
8516 "Enables experimental features when non-zero");
8518 module_param_named(debug, dbg_level, uint, 0);
8519 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8521 module_param(force_load, bool, 0444);
8522 MODULE_PARM_DESC(force_load,
8523 "Attempts to load the driver even on a "
8524 "mis-identified ThinkPad when true");
8526 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8527 MODULE_PARM_DESC(fan_control,
8528 "Enables setting fan parameters features when true");
8530 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8531 MODULE_PARM_DESC(brightness_mode,
8532 "Selects brightness control strategy: "
8533 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8535 module_param(brightness_enable, uint, 0444);
8536 MODULE_PARM_DESC(brightness_enable,
8537 "Enables backlight control when 1, disables when 0");
8539 module_param(hotkey_report_mode, uint, 0444);
8540 MODULE_PARM_DESC(hotkey_report_mode,
8541 "used for backwards compatibility with userspace, "
8542 "see documentation");
8544 module_param_named(volume_mode, volume_mode, uint, 0444);
8545 MODULE_PARM_DESC(volume_mode,
8546 "Selects volume control strategy: "
8547 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8549 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8550 MODULE_PARM_DESC(volume_capabilities,
8551 "Selects the mixer capabilites: "
8552 "0=auto, 1=volume and mute, 2=mute only");
8554 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8555 MODULE_PARM_DESC(volume_control,
8556 "Enables software override for the console audio "
8557 "control when true");
8559 #define TPACPI_PARAM(feature) \
8560 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8561 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8562 "at module load, see documentation")
8564 TPACPI_PARAM(hotkey);
8565 TPACPI_PARAM(bluetooth);
8566 TPACPI_PARAM(video);
8567 TPACPI_PARAM(light);
8568 TPACPI_PARAM(cmos);
8569 TPACPI_PARAM(led);
8570 TPACPI_PARAM(beep);
8571 TPACPI_PARAM(ecdump);
8572 TPACPI_PARAM(brightness);
8573 TPACPI_PARAM(volume);
8574 TPACPI_PARAM(fan);
8576 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8577 module_param(dbg_wlswemul, uint, 0444);
8578 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8579 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8580 MODULE_PARM_DESC(wlsw_state,
8581 "Initial state of the emulated WLSW switch");
8583 module_param(dbg_bluetoothemul, uint, 0444);
8584 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8585 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8586 MODULE_PARM_DESC(bluetooth_state,
8587 "Initial state of the emulated bluetooth switch");
8589 module_param(dbg_wwanemul, uint, 0444);
8590 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8591 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8592 MODULE_PARM_DESC(wwan_state,
8593 "Initial state of the emulated WWAN switch");
8595 module_param(dbg_uwbemul, uint, 0444);
8596 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8597 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8598 MODULE_PARM_DESC(uwb_state,
8599 "Initial state of the emulated UWB switch");
8600 #endif
8602 static void thinkpad_acpi_module_exit(void)
8604 struct ibm_struct *ibm, *itmp;
8606 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8608 list_for_each_entry_safe_reverse(ibm, itmp,
8609 &tpacpi_all_drivers,
8610 all_drivers) {
8611 ibm_exit(ibm);
8614 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8616 if (tpacpi_inputdev) {
8617 if (tp_features.input_device_registered)
8618 input_unregister_device(tpacpi_inputdev);
8619 else
8620 input_free_device(tpacpi_inputdev);
8623 if (tpacpi_hwmon)
8624 hwmon_device_unregister(tpacpi_hwmon);
8626 if (tp_features.sensors_pdev_attrs_registered)
8627 device_remove_file(&tpacpi_sensors_pdev->dev,
8628 &dev_attr_thinkpad_acpi_pdev_name);
8629 if (tpacpi_sensors_pdev)
8630 platform_device_unregister(tpacpi_sensors_pdev);
8631 if (tpacpi_pdev)
8632 platform_device_unregister(tpacpi_pdev);
8634 if (tp_features.sensors_pdrv_attrs_registered)
8635 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8636 if (tp_features.platform_drv_attrs_registered)
8637 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8639 if (tp_features.sensors_pdrv_registered)
8640 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8642 if (tp_features.platform_drv_registered)
8643 platform_driver_unregister(&tpacpi_pdriver);
8645 if (proc_dir)
8646 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8648 if (tpacpi_wq)
8649 destroy_workqueue(tpacpi_wq);
8651 kfree(thinkpad_id.bios_version_str);
8652 kfree(thinkpad_id.ec_version_str);
8653 kfree(thinkpad_id.model_str);
8657 static int __init thinkpad_acpi_module_init(void)
8659 int ret, i;
8661 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8663 /* Parameter checking */
8664 if (hotkey_report_mode > 2)
8665 return -EINVAL;
8667 /* Driver-level probe */
8669 ret = get_thinkpad_model_data(&thinkpad_id);
8670 if (ret) {
8671 printk(TPACPI_ERR
8672 "unable to get DMI data: %d\n", ret);
8673 thinkpad_acpi_module_exit();
8674 return ret;
8676 ret = probe_for_thinkpad();
8677 if (ret) {
8678 thinkpad_acpi_module_exit();
8679 return ret;
8682 /* Driver initialization */
8684 TPACPI_ACPIHANDLE_INIT(ecrd);
8685 TPACPI_ACPIHANDLE_INIT(ecwr);
8687 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8688 if (!tpacpi_wq) {
8689 thinkpad_acpi_module_exit();
8690 return -ENOMEM;
8693 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8694 if (!proc_dir) {
8695 printk(TPACPI_ERR
8696 "unable to create proc dir " TPACPI_PROC_DIR);
8697 thinkpad_acpi_module_exit();
8698 return -ENODEV;
8701 ret = platform_driver_register(&tpacpi_pdriver);
8702 if (ret) {
8703 printk(TPACPI_ERR
8704 "unable to register main platform driver\n");
8705 thinkpad_acpi_module_exit();
8706 return ret;
8708 tp_features.platform_drv_registered = 1;
8710 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8711 if (ret) {
8712 printk(TPACPI_ERR
8713 "unable to register hwmon platform driver\n");
8714 thinkpad_acpi_module_exit();
8715 return ret;
8717 tp_features.sensors_pdrv_registered = 1;
8719 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8720 if (!ret) {
8721 tp_features.platform_drv_attrs_registered = 1;
8722 ret = tpacpi_create_driver_attributes(
8723 &tpacpi_hwmon_pdriver.driver);
8725 if (ret) {
8726 printk(TPACPI_ERR
8727 "unable to create sysfs driver attributes\n");
8728 thinkpad_acpi_module_exit();
8729 return ret;
8731 tp_features.sensors_pdrv_attrs_registered = 1;
8734 /* Device initialization */
8735 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
8736 NULL, 0);
8737 if (IS_ERR(tpacpi_pdev)) {
8738 ret = PTR_ERR(tpacpi_pdev);
8739 tpacpi_pdev = NULL;
8740 printk(TPACPI_ERR "unable to register platform device\n");
8741 thinkpad_acpi_module_exit();
8742 return ret;
8744 tpacpi_sensors_pdev = platform_device_register_simple(
8745 TPACPI_HWMON_DRVR_NAME,
8746 -1, NULL, 0);
8747 if (IS_ERR(tpacpi_sensors_pdev)) {
8748 ret = PTR_ERR(tpacpi_sensors_pdev);
8749 tpacpi_sensors_pdev = NULL;
8750 printk(TPACPI_ERR
8751 "unable to register hwmon platform device\n");
8752 thinkpad_acpi_module_exit();
8753 return ret;
8755 ret = device_create_file(&tpacpi_sensors_pdev->dev,
8756 &dev_attr_thinkpad_acpi_pdev_name);
8757 if (ret) {
8758 printk(TPACPI_ERR
8759 "unable to create sysfs hwmon device attributes\n");
8760 thinkpad_acpi_module_exit();
8761 return ret;
8763 tp_features.sensors_pdev_attrs_registered = 1;
8764 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
8765 if (IS_ERR(tpacpi_hwmon)) {
8766 ret = PTR_ERR(tpacpi_hwmon);
8767 tpacpi_hwmon = NULL;
8768 printk(TPACPI_ERR "unable to register hwmon device\n");
8769 thinkpad_acpi_module_exit();
8770 return ret;
8772 mutex_init(&tpacpi_inputdev_send_mutex);
8773 tpacpi_inputdev = input_allocate_device();
8774 if (!tpacpi_inputdev) {
8775 printk(TPACPI_ERR "unable to allocate input device\n");
8776 thinkpad_acpi_module_exit();
8777 return -ENOMEM;
8778 } else {
8779 /* Prepare input device, but don't register */
8780 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
8781 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
8782 tpacpi_inputdev->id.bustype = BUS_HOST;
8783 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
8784 thinkpad_id.vendor :
8785 PCI_VENDOR_ID_IBM;
8786 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
8787 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
8788 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
8790 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8791 ret = ibm_init(&ibms_init[i]);
8792 if (ret >= 0 && *ibms_init[i].param)
8793 ret = ibms_init[i].data->write(ibms_init[i].param);
8794 if (ret < 0) {
8795 thinkpad_acpi_module_exit();
8796 return ret;
8800 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
8802 ret = input_register_device(tpacpi_inputdev);
8803 if (ret < 0) {
8804 printk(TPACPI_ERR "unable to register input device\n");
8805 thinkpad_acpi_module_exit();
8806 return ret;
8807 } else {
8808 tp_features.input_device_registered = 1;
8811 return 0;
8814 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
8817 * This will autoload the driver in almost every ThinkPad
8818 * in widespread use.
8820 * Only _VERY_ old models, like the 240, 240x and 570 lack
8821 * the HKEY event interface.
8823 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
8826 * DMI matching for module autoloading
8828 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8829 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
8831 * Only models listed in thinkwiki will be supported, so add yours
8832 * if it is not there yet.
8834 #define IBM_BIOS_MODULE_ALIAS(__type) \
8835 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
8837 /* Ancient thinkpad BIOSes have to be identified by
8838 * BIOS type or model number, and there are far less
8839 * BIOS types than model numbers... */
8840 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
8842 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
8843 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
8844 MODULE_DESCRIPTION(TPACPI_DESC);
8845 MODULE_VERSION(TPACPI_VERSION);
8846 MODULE_LICENSE("GPL");
8848 module_init(thinkpad_acpi_module_init);
8849 module_exit(thinkpad_acpi_module_exit);