ACPI: thinkpad-acpi: add development version tag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob902b1e9c7e5f650568e37ec1beadc0e4531038da
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.24-20100227"
25 #define TPACPI_SYSFS_VERSION 0x020700
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/seq_file.h>
65 #include <linux/sysfs.h>
66 #include <linux/backlight.h>
67 #include <linux/fb.h>
68 #include <linux/platform_device.h>
69 #include <linux/hwmon.h>
70 #include <linux/hwmon-sysfs.h>
71 #include <linux/input.h>
72 #include <linux/leds.h>
73 #include <linux/rfkill.h>
74 #include <asm/uaccess.h>
76 #include <linux/dmi.h>
77 #include <linux/jiffies.h>
78 #include <linux/workqueue.h>
80 #include <sound/core.h>
81 #include <sound/control.h>
82 #include <sound/initval.h>
84 #include <acpi/acpi_drivers.h>
86 #include <linux/pci_ids.h>
89 /* ThinkPad CMOS commands */
90 #define TP_CMOS_VOLUME_DOWN 0
91 #define TP_CMOS_VOLUME_UP 1
92 #define TP_CMOS_VOLUME_MUTE 2
93 #define TP_CMOS_BRIGHTNESS_UP 4
94 #define TP_CMOS_BRIGHTNESS_DOWN 5
95 #define TP_CMOS_THINKLIGHT_ON 12
96 #define TP_CMOS_THINKLIGHT_OFF 13
98 /* NVRAM Addresses */
99 enum tp_nvram_addr {
100 TP_NVRAM_ADDR_HK2 = 0x57,
101 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
102 TP_NVRAM_ADDR_VIDEO = 0x59,
103 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
104 TP_NVRAM_ADDR_MIXER = 0x60,
107 /* NVRAM bit masks */
108 enum {
109 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
110 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
111 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
112 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
113 TP_NVRAM_MASK_THINKLIGHT = 0x10,
114 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
115 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
116 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
117 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
118 TP_NVRAM_MASK_MUTE = 0x40,
119 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
120 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
121 TP_NVRAM_POS_LEVEL_VOLUME = 0,
124 /* ACPI HIDs */
125 #define TPACPI_ACPI_HKEY_HID "IBM0068"
127 /* Input IDs */
128 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
129 #define TPACPI_HKEY_INPUT_VERSION 0x4101
131 /* ACPI \WGSV commands */
132 enum {
133 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
134 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
135 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
136 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
139 /* TP_ACPI_WGSV_GET_STATE bits */
140 enum {
141 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
142 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
143 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
144 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
145 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
146 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
147 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
148 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
149 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
150 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
153 /* HKEY events */
154 enum tpacpi_hkey_event_t {
155 /* Hotkey-related */
156 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
157 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
158 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
159 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
160 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
161 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
163 /* Reasons for waking up from S3/S4 */
164 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
165 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
166 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
167 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
168 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
169 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
171 /* Auto-sleep after eject request */
172 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
173 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
175 /* Misc bay events */
176 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
178 /* User-interface events */
179 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
180 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
181 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
182 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
183 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
184 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
185 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
187 /* Thermal events */
188 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
189 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
190 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
191 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
192 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
194 /* Misc */
195 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
198 /****************************************************************************
199 * Main driver
202 #define TPACPI_NAME "thinkpad"
203 #define TPACPI_DESC "ThinkPad ACPI Extras"
204 #define TPACPI_FILE TPACPI_NAME "_acpi"
205 #define TPACPI_URL "http://ibm-acpi.sf.net/"
206 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
208 #define TPACPI_PROC_DIR "ibm"
209 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
210 #define TPACPI_DRVR_NAME TPACPI_FILE
211 #define TPACPI_DRVR_SHORTNAME "tpacpi"
212 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
214 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
215 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
217 #define TPACPI_MAX_ACPI_ARGS 3
219 /* printk headers */
220 #define TPACPI_LOG TPACPI_FILE ": "
221 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
222 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
223 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
224 #define TPACPI_ERR KERN_ERR TPACPI_LOG
225 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
226 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
227 #define TPACPI_INFO KERN_INFO TPACPI_LOG
228 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
230 /* Debugging printk groups */
231 #define TPACPI_DBG_ALL 0xffff
232 #define TPACPI_DBG_DISCLOSETASK 0x8000
233 #define TPACPI_DBG_INIT 0x0001
234 #define TPACPI_DBG_EXIT 0x0002
235 #define TPACPI_DBG_RFKILL 0x0004
236 #define TPACPI_DBG_HKEY 0x0008
237 #define TPACPI_DBG_FAN 0x0010
238 #define TPACPI_DBG_BRGHT 0x0020
239 #define TPACPI_DBG_MIXER 0x0040
241 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
242 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
243 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
246 /****************************************************************************
247 * Driver-wide structs and misc. variables
250 struct ibm_struct;
252 struct tp_acpi_drv_struct {
253 const struct acpi_device_id *hid;
254 struct acpi_driver *driver;
256 void (*notify) (struct ibm_struct *, u32);
257 acpi_handle *handle;
258 u32 type;
259 struct acpi_device *device;
262 struct ibm_struct {
263 char *name;
265 int (*read) (struct seq_file *);
266 int (*write) (char *);
267 void (*exit) (void);
268 void (*resume) (void);
269 void (*suspend) (pm_message_t state);
270 void (*shutdown) (void);
272 struct list_head all_drivers;
274 struct tp_acpi_drv_struct *acpi;
276 struct {
277 u8 acpi_driver_registered:1;
278 u8 acpi_notify_installed:1;
279 u8 proc_created:1;
280 u8 init_called:1;
281 u8 experimental:1;
282 } flags;
285 struct ibm_init_struct {
286 char param[32];
288 int (*init) (struct ibm_init_struct *);
289 mode_t base_procfs_mode;
290 struct ibm_struct *data;
293 static struct {
294 u32 bluetooth:1;
295 u32 hotkey:1;
296 u32 hotkey_mask:1;
297 u32 hotkey_wlsw:1;
298 u32 hotkey_tablet:1;
299 u32 light:1;
300 u32 light_status:1;
301 u32 bright_16levels:1;
302 u32 bright_acpimode:1;
303 u32 wan:1;
304 u32 uwb:1;
305 u32 fan_ctrl_status_undef:1;
306 u32 second_fan:1;
307 u32 beep_needs_two_args:1;
308 u32 mixer_no_level_control:1;
309 u32 input_device_registered:1;
310 u32 platform_drv_registered:1;
311 u32 platform_drv_attrs_registered:1;
312 u32 sensors_pdrv_registered:1;
313 u32 sensors_pdrv_attrs_registered:1;
314 u32 sensors_pdev_attrs_registered:1;
315 u32 hotkey_poll_active:1;
316 } tp_features;
318 static struct {
319 u16 hotkey_mask_ff:1;
320 u16 volume_ctrl_forbidden:1;
321 } tp_warned;
323 struct thinkpad_id_data {
324 unsigned int vendor; /* ThinkPad vendor:
325 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
327 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
328 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
330 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
331 u16 ec_model;
332 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
333 u16 ec_release;
335 char *model_str; /* ThinkPad T43 */
336 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
338 static struct thinkpad_id_data thinkpad_id;
340 static enum {
341 TPACPI_LIFE_INIT = 0,
342 TPACPI_LIFE_RUNNING,
343 TPACPI_LIFE_EXITING,
344 } tpacpi_lifecycle;
346 static int experimental;
347 static u32 dbg_level;
349 static struct workqueue_struct *tpacpi_wq;
351 enum led_status_t {
352 TPACPI_LED_OFF = 0,
353 TPACPI_LED_ON,
354 TPACPI_LED_BLINK,
357 /* Special LED class that can defer work */
358 struct tpacpi_led_classdev {
359 struct led_classdev led_classdev;
360 struct work_struct work;
361 enum led_status_t new_state;
362 unsigned int led;
365 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
366 static int dbg_wlswemul;
367 static int tpacpi_wlsw_emulstate;
368 static int dbg_bluetoothemul;
369 static int tpacpi_bluetooth_emulstate;
370 static int dbg_wwanemul;
371 static int tpacpi_wwan_emulstate;
372 static int dbg_uwbemul;
373 static int tpacpi_uwb_emulstate;
374 #endif
377 /*************************************************************************
378 * Debugging helpers
381 #define dbg_printk(a_dbg_level, format, arg...) \
382 do { if (dbg_level & (a_dbg_level)) \
383 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
384 } while (0)
386 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
387 #define vdbg_printk dbg_printk
388 static const char *str_supported(int is_supported);
389 #else
390 #define vdbg_printk(a_dbg_level, format, arg...) \
391 do { } while (0)
392 #endif
394 static void tpacpi_log_usertask(const char * const what)
396 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
397 what, task_tgid_vnr(current));
400 #define tpacpi_disclose_usertask(what, format, arg...) \
401 do { \
402 if (unlikely( \
403 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
404 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
405 printk(TPACPI_DEBUG "%s: PID %d: " format, \
406 what, task_tgid_vnr(current), ## arg); \
408 } while (0)
411 * Quirk handling helpers
413 * ThinkPad IDs and versions seen in the field so far
414 * are two-characters from the set [0-9A-Z], i.e. base 36.
416 * We use values well outside that range as specials.
419 #define TPACPI_MATCH_ANY 0xffffU
420 #define TPACPI_MATCH_UNKNOWN 0U
422 /* TPID('1', 'Y') == 0x5931 */
423 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
425 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
426 { .vendor = PCI_VENDOR_ID_IBM, \
427 .bios = TPID(__id1, __id2), \
428 .ec = TPACPI_MATCH_ANY, \
429 .quirks = (__quirk) }
431 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
432 { .vendor = PCI_VENDOR_ID_LENOVO, \
433 .bios = TPID(__id1, __id2), \
434 .ec = TPACPI_MATCH_ANY, \
435 .quirks = (__quirk) }
437 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
438 { .vendor = PCI_VENDOR_ID_LENOVO, \
439 .bios = TPACPI_MATCH_ANY, \
440 .ec = TPID(__id1, __id2), \
441 .quirks = (__quirk) }
443 struct tpacpi_quirk {
444 unsigned int vendor;
445 u16 bios;
446 u16 ec;
447 unsigned long quirks;
451 * tpacpi_check_quirks() - search BIOS/EC version on a list
452 * @qlist: array of &struct tpacpi_quirk
453 * @qlist_size: number of elements in @qlist
455 * Iterates over a quirks list until one is found that matches the
456 * ThinkPad's vendor, BIOS and EC model.
458 * Returns 0 if nothing matches, otherwise returns the quirks field of
459 * the matching &struct tpacpi_quirk entry.
461 * The match criteria is: vendor, ec and bios much match.
463 static unsigned long __init tpacpi_check_quirks(
464 const struct tpacpi_quirk *qlist,
465 unsigned int qlist_size)
467 while (qlist_size) {
468 if ((qlist->vendor == thinkpad_id.vendor ||
469 qlist->vendor == TPACPI_MATCH_ANY) &&
470 (qlist->bios == thinkpad_id.bios_model ||
471 qlist->bios == TPACPI_MATCH_ANY) &&
472 (qlist->ec == thinkpad_id.ec_model ||
473 qlist->ec == TPACPI_MATCH_ANY))
474 return qlist->quirks;
476 qlist_size--;
477 qlist++;
479 return 0;
483 /****************************************************************************
484 ****************************************************************************
486 * ACPI Helpers and device model
488 ****************************************************************************
489 ****************************************************************************/
491 /*************************************************************************
492 * ACPI basic handles
495 static acpi_handle root_handle;
497 #define TPACPI_HANDLE(object, parent, paths...) \
498 static acpi_handle object##_handle; \
499 static acpi_handle *object##_parent = &parent##_handle; \
500 static char *object##_path; \
501 static char *object##_paths[] = { paths }
503 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
504 "\\_SB.PCI.ISA.EC", /* 570 */
505 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
506 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
507 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
508 "\\_SB.PCI0.ICH3.EC0", /* R31 */
509 "\\_SB.PCI0.LPC.EC", /* all others */
512 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
513 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
515 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
516 /* T4x, X31, X40 */
517 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
518 "\\CMS", /* R40, R40e */
519 ); /* all others */
521 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
522 "^HKEY", /* R30, R31 */
523 "HKEY", /* all others */
524 ); /* 570 */
526 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
527 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
528 "\\_SB.PCI0.VID0", /* 770e */
529 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
530 "\\_SB.PCI0.AGP.VID", /* all others */
531 ); /* R30, R31 */
534 /*************************************************************************
535 * ACPI helpers
538 static int acpi_evalf(acpi_handle handle,
539 void *res, char *method, char *fmt, ...)
541 char *fmt0 = fmt;
542 struct acpi_object_list params;
543 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
544 struct acpi_buffer result, *resultp;
545 union acpi_object out_obj;
546 acpi_status status;
547 va_list ap;
548 char res_type;
549 int success;
550 int quiet;
552 if (!*fmt) {
553 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
554 return 0;
557 if (*fmt == 'q') {
558 quiet = 1;
559 fmt++;
560 } else
561 quiet = 0;
563 res_type = *(fmt++);
565 params.count = 0;
566 params.pointer = &in_objs[0];
568 va_start(ap, fmt);
569 while (*fmt) {
570 char c = *(fmt++);
571 switch (c) {
572 case 'd': /* int */
573 in_objs[params.count].integer.value = va_arg(ap, int);
574 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
575 break;
576 /* add more types as needed */
577 default:
578 printk(TPACPI_ERR "acpi_evalf() called "
579 "with invalid format character '%c'\n", c);
580 return 0;
583 va_end(ap);
585 if (res_type != 'v') {
586 result.length = sizeof(out_obj);
587 result.pointer = &out_obj;
588 resultp = &result;
589 } else
590 resultp = NULL;
592 status = acpi_evaluate_object(handle, method, &params, resultp);
594 switch (res_type) {
595 case 'd': /* int */
596 if (res)
597 *(int *)res = out_obj.integer.value;
598 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
599 break;
600 case 'v': /* void */
601 success = status == AE_OK;
602 break;
603 /* add more types as needed */
604 default:
605 printk(TPACPI_ERR "acpi_evalf() called "
606 "with invalid format character '%c'\n", res_type);
607 return 0;
610 if (!success && !quiet)
611 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
612 method, fmt0, status);
614 return success;
617 static int acpi_ec_read(int i, u8 *p)
619 int v;
621 if (ecrd_handle) {
622 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
623 return 0;
624 *p = v;
625 } else {
626 if (ec_read(i, p) < 0)
627 return 0;
630 return 1;
633 static int acpi_ec_write(int i, u8 v)
635 if (ecwr_handle) {
636 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
637 return 0;
638 } else {
639 if (ec_write(i, v) < 0)
640 return 0;
643 return 1;
646 static int issue_thinkpad_cmos_command(int cmos_cmd)
648 if (!cmos_handle)
649 return -ENXIO;
651 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
652 return -EIO;
654 return 0;
657 /*************************************************************************
658 * ACPI device model
661 #define TPACPI_ACPIHANDLE_INIT(object) \
662 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
663 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
665 static void drv_acpi_handle_init(char *name,
666 acpi_handle *handle, acpi_handle parent,
667 char **paths, int num_paths, char **path)
669 int i;
670 acpi_status status;
672 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
673 name);
675 for (i = 0; i < num_paths; i++) {
676 status = acpi_get_handle(parent, paths[i], handle);
677 if (ACPI_SUCCESS(status)) {
678 *path = paths[i];
679 dbg_printk(TPACPI_DBG_INIT,
680 "Found ACPI handle %s for %s\n",
681 *path, name);
682 return;
686 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
687 name);
688 *handle = NULL;
691 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
693 struct ibm_struct *ibm = data;
695 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
696 return;
698 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
699 return;
701 ibm->acpi->notify(ibm, event);
704 static int __init setup_acpi_notify(struct ibm_struct *ibm)
706 acpi_status status;
707 int rc;
709 BUG_ON(!ibm->acpi);
711 if (!*ibm->acpi->handle)
712 return 0;
714 vdbg_printk(TPACPI_DBG_INIT,
715 "setting up ACPI notify for %s\n", ibm->name);
717 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
718 if (rc < 0) {
719 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
720 ibm->name, rc);
721 return -ENODEV;
724 ibm->acpi->device->driver_data = ibm;
725 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
726 TPACPI_ACPI_EVENT_PREFIX,
727 ibm->name);
729 status = acpi_install_notify_handler(*ibm->acpi->handle,
730 ibm->acpi->type, dispatch_acpi_notify, ibm);
731 if (ACPI_FAILURE(status)) {
732 if (status == AE_ALREADY_EXISTS) {
733 printk(TPACPI_NOTICE
734 "another device driver is already "
735 "handling %s events\n", ibm->name);
736 } else {
737 printk(TPACPI_ERR
738 "acpi_install_notify_handler(%s) failed: %d\n",
739 ibm->name, status);
741 return -ENODEV;
743 ibm->flags.acpi_notify_installed = 1;
744 return 0;
747 static int __init tpacpi_device_add(struct acpi_device *device)
749 return 0;
752 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
754 int rc;
756 dbg_printk(TPACPI_DBG_INIT,
757 "registering %s as an ACPI driver\n", ibm->name);
759 BUG_ON(!ibm->acpi);
761 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
762 if (!ibm->acpi->driver) {
763 printk(TPACPI_ERR
764 "failed to allocate memory for ibm->acpi->driver\n");
765 return -ENOMEM;
768 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
769 ibm->acpi->driver->ids = ibm->acpi->hid;
771 ibm->acpi->driver->ops.add = &tpacpi_device_add;
773 rc = acpi_bus_register_driver(ibm->acpi->driver);
774 if (rc < 0) {
775 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
776 ibm->name, rc);
777 kfree(ibm->acpi->driver);
778 ibm->acpi->driver = NULL;
779 } else if (!rc)
780 ibm->flags.acpi_driver_registered = 1;
782 return rc;
786 /****************************************************************************
787 ****************************************************************************
789 * Procfs Helpers
791 ****************************************************************************
792 ****************************************************************************/
794 static int dispatch_proc_show(struct seq_file *m, void *v)
796 struct ibm_struct *ibm = m->private;
798 if (!ibm || !ibm->read)
799 return -EINVAL;
800 return ibm->read(m);
803 static int dispatch_proc_open(struct inode *inode, struct file *file)
805 return single_open(file, dispatch_proc_show, PDE(inode)->data);
808 static ssize_t dispatch_proc_write(struct file *file,
809 const char __user *userbuf,
810 size_t count, loff_t *pos)
812 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
813 char *kernbuf;
814 int ret;
816 if (!ibm || !ibm->write)
817 return -EINVAL;
818 if (count > PAGE_SIZE - 2)
819 return -EINVAL;
821 kernbuf = kmalloc(count + 2, GFP_KERNEL);
822 if (!kernbuf)
823 return -ENOMEM;
825 if (copy_from_user(kernbuf, userbuf, count)) {
826 kfree(kernbuf);
827 return -EFAULT;
830 kernbuf[count] = 0;
831 strcat(kernbuf, ",");
832 ret = ibm->write(kernbuf);
833 if (ret == 0)
834 ret = count;
836 kfree(kernbuf);
838 return ret;
841 static const struct file_operations dispatch_proc_fops = {
842 .owner = THIS_MODULE,
843 .open = dispatch_proc_open,
844 .read = seq_read,
845 .llseek = seq_lseek,
846 .release = single_release,
847 .write = dispatch_proc_write,
850 static char *next_cmd(char **cmds)
852 char *start = *cmds;
853 char *end;
855 while ((end = strchr(start, ',')) && end == start)
856 start = end + 1;
858 if (!end)
859 return NULL;
861 *end = 0;
862 *cmds = end + 1;
863 return start;
867 /****************************************************************************
868 ****************************************************************************
870 * Device model: input, hwmon and platform
872 ****************************************************************************
873 ****************************************************************************/
875 static struct platform_device *tpacpi_pdev;
876 static struct platform_device *tpacpi_sensors_pdev;
877 static struct device *tpacpi_hwmon;
878 static struct input_dev *tpacpi_inputdev;
879 static struct mutex tpacpi_inputdev_send_mutex;
880 static LIST_HEAD(tpacpi_all_drivers);
882 static int tpacpi_suspend_handler(struct platform_device *pdev,
883 pm_message_t state)
885 struct ibm_struct *ibm, *itmp;
887 list_for_each_entry_safe(ibm, itmp,
888 &tpacpi_all_drivers,
889 all_drivers) {
890 if (ibm->suspend)
891 (ibm->suspend)(state);
894 return 0;
897 static int tpacpi_resume_handler(struct platform_device *pdev)
899 struct ibm_struct *ibm, *itmp;
901 list_for_each_entry_safe(ibm, itmp,
902 &tpacpi_all_drivers,
903 all_drivers) {
904 if (ibm->resume)
905 (ibm->resume)();
908 return 0;
911 static void tpacpi_shutdown_handler(struct platform_device *pdev)
913 struct ibm_struct *ibm, *itmp;
915 list_for_each_entry_safe(ibm, itmp,
916 &tpacpi_all_drivers,
917 all_drivers) {
918 if (ibm->shutdown)
919 (ibm->shutdown)();
923 static struct platform_driver tpacpi_pdriver = {
924 .driver = {
925 .name = TPACPI_DRVR_NAME,
926 .owner = THIS_MODULE,
928 .suspend = tpacpi_suspend_handler,
929 .resume = tpacpi_resume_handler,
930 .shutdown = tpacpi_shutdown_handler,
933 static struct platform_driver tpacpi_hwmon_pdriver = {
934 .driver = {
935 .name = TPACPI_HWMON_DRVR_NAME,
936 .owner = THIS_MODULE,
940 /*************************************************************************
941 * sysfs support helpers
944 struct attribute_set {
945 unsigned int members, max_members;
946 struct attribute_group group;
949 struct attribute_set_obj {
950 struct attribute_set s;
951 struct attribute *a;
952 } __attribute__((packed));
954 static struct attribute_set *create_attr_set(unsigned int max_members,
955 const char *name)
957 struct attribute_set_obj *sobj;
959 if (max_members == 0)
960 return NULL;
962 /* Allocates space for implicit NULL at the end too */
963 sobj = kzalloc(sizeof(struct attribute_set_obj) +
964 max_members * sizeof(struct attribute *),
965 GFP_KERNEL);
966 if (!sobj)
967 return NULL;
968 sobj->s.max_members = max_members;
969 sobj->s.group.attrs = &sobj->a;
970 sobj->s.group.name = name;
972 return &sobj->s;
975 #define destroy_attr_set(_set) \
976 kfree(_set);
978 /* not multi-threaded safe, use it in a single thread per set */
979 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
981 if (!s || !attr)
982 return -EINVAL;
984 if (s->members >= s->max_members)
985 return -ENOMEM;
987 s->group.attrs[s->members] = attr;
988 s->members++;
990 return 0;
993 static int add_many_to_attr_set(struct attribute_set *s,
994 struct attribute **attr,
995 unsigned int count)
997 int i, res;
999 for (i = 0; i < count; i++) {
1000 res = add_to_attr_set(s, attr[i]);
1001 if (res)
1002 return res;
1005 return 0;
1008 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1010 sysfs_remove_group(kobj, &s->group);
1011 destroy_attr_set(s);
1014 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1015 sysfs_create_group(_kobj, &_attr_set->group)
1017 static int parse_strtoul(const char *buf,
1018 unsigned long max, unsigned long *value)
1020 char *endp;
1022 while (*buf && isspace(*buf))
1023 buf++;
1024 *value = simple_strtoul(buf, &endp, 0);
1025 while (*endp && isspace(*endp))
1026 endp++;
1027 if (*endp || *value > max)
1028 return -EINVAL;
1030 return 0;
1033 static void tpacpi_disable_brightness_delay(void)
1035 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1036 printk(TPACPI_NOTICE
1037 "ACPI backlight control delay disabled\n");
1040 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1042 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1043 union acpi_object *obj;
1044 int rc;
1046 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1047 obj = (union acpi_object *)buffer.pointer;
1048 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1049 printk(TPACPI_ERR "Unknown _BCL data, "
1050 "please report this to %s\n", TPACPI_MAIL);
1051 rc = 0;
1052 } else {
1053 rc = obj->package.count;
1055 } else {
1056 return 0;
1059 kfree(buffer.pointer);
1060 return rc;
1063 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1064 u32 lvl, void *context, void **rv)
1066 char name[ACPI_PATH_SEGMENT_LENGTH];
1067 struct acpi_buffer buffer = { sizeof(name), &name };
1069 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1070 !strncmp("_BCL", name, sizeof(name) - 1)) {
1071 BUG_ON(!rv || !*rv);
1072 **(int **)rv = tpacpi_query_bcl_levels(handle);
1073 return AE_CTRL_TERMINATE;
1074 } else {
1075 return AE_OK;
1080 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1082 static int __init tpacpi_check_std_acpi_brightness_support(void)
1084 int status;
1085 int bcl_levels = 0;
1086 void *bcl_ptr = &bcl_levels;
1088 if (!vid_handle) {
1089 TPACPI_ACPIHANDLE_INIT(vid);
1091 if (!vid_handle)
1092 return 0;
1095 * Search for a _BCL method, and execute it. This is safe on all
1096 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1097 * BIOS in ACPI backlight control mode. We do NOT have to care
1098 * about calling the _BCL method in an enabled video device, any
1099 * will do for our purposes.
1102 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1103 tpacpi_acpi_walk_find_bcl, NULL,
1104 &bcl_ptr);
1106 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1107 tp_features.bright_acpimode = 1;
1108 return (bcl_levels - 2);
1111 return 0;
1114 static void printk_deprecated_attribute(const char * const what,
1115 const char * const details)
1117 tpacpi_log_usertask("deprecated sysfs attribute");
1118 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1119 "will be removed. %s\n",
1120 what, details);
1123 /*************************************************************************
1124 * rfkill and radio control support helpers
1128 * ThinkPad-ACPI firmware handling model:
1130 * WLSW (master wireless switch) is event-driven, and is common to all
1131 * firmware-controlled radios. It cannot be controlled, just monitored,
1132 * as expected. It overrides all radio state in firmware
1134 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1135 * (TODO: verify how WLSW interacts with the returned radio state).
1137 * The only time there are shadow radio state changes, is when
1138 * masked-off hotkeys are used.
1142 * Internal driver API for radio state:
1144 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1145 * bool: true means radio blocked (off)
1147 enum tpacpi_rfkill_state {
1148 TPACPI_RFK_RADIO_OFF = 0,
1149 TPACPI_RFK_RADIO_ON
1152 /* rfkill switches */
1153 enum tpacpi_rfk_id {
1154 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1155 TPACPI_RFK_WWAN_SW_ID,
1156 TPACPI_RFK_UWB_SW_ID,
1157 TPACPI_RFK_SW_MAX
1160 static const char *tpacpi_rfkill_names[] = {
1161 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1162 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1163 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1164 [TPACPI_RFK_SW_MAX] = NULL
1167 /* ThinkPad-ACPI rfkill subdriver */
1168 struct tpacpi_rfk {
1169 struct rfkill *rfkill;
1170 enum tpacpi_rfk_id id;
1171 const struct tpacpi_rfk_ops *ops;
1174 struct tpacpi_rfk_ops {
1175 /* firmware interface */
1176 int (*get_status)(void);
1177 int (*set_status)(const enum tpacpi_rfkill_state);
1180 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1182 /* Query FW and update rfkill sw state for a given rfkill switch */
1183 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1185 int status;
1187 if (!tp_rfk)
1188 return -ENODEV;
1190 status = (tp_rfk->ops->get_status)();
1191 if (status < 0)
1192 return status;
1194 rfkill_set_sw_state(tp_rfk->rfkill,
1195 (status == TPACPI_RFK_RADIO_OFF));
1197 return status;
1200 /* Query FW and update rfkill sw state for all rfkill switches */
1201 static void tpacpi_rfk_update_swstate_all(void)
1203 unsigned int i;
1205 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1206 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1210 * Sync the HW-blocking state of all rfkill switches,
1211 * do notice it causes the rfkill core to schedule uevents
1213 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1215 unsigned int i;
1216 struct tpacpi_rfk *tp_rfk;
1218 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1219 tp_rfk = tpacpi_rfkill_switches[i];
1220 if (tp_rfk) {
1221 if (rfkill_set_hw_state(tp_rfk->rfkill,
1222 blocked)) {
1223 /* ignore -- we track sw block */
1229 /* Call to get the WLSW state from the firmware */
1230 static int hotkey_get_wlsw(void);
1232 /* Call to query WLSW state and update all rfkill switches */
1233 static bool tpacpi_rfk_check_hwblock_state(void)
1235 int res = hotkey_get_wlsw();
1236 int hw_blocked;
1238 /* When unknown or unsupported, we have to assume it is unblocked */
1239 if (res < 0)
1240 return false;
1242 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1243 tpacpi_rfk_update_hwblock_state(hw_blocked);
1245 return hw_blocked;
1248 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1250 struct tpacpi_rfk *tp_rfk = data;
1251 int res;
1253 dbg_printk(TPACPI_DBG_RFKILL,
1254 "request to change radio state to %s\n",
1255 blocked ? "blocked" : "unblocked");
1257 /* try to set radio state */
1258 res = (tp_rfk->ops->set_status)(blocked ?
1259 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1261 /* and update the rfkill core with whatever the FW really did */
1262 tpacpi_rfk_update_swstate(tp_rfk);
1264 return (res < 0) ? res : 0;
1267 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1268 .set_block = tpacpi_rfk_hook_set_block,
1271 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1272 const struct tpacpi_rfk_ops *tp_rfkops,
1273 const enum rfkill_type rfktype,
1274 const char *name,
1275 const bool set_default)
1277 struct tpacpi_rfk *atp_rfk;
1278 int res;
1279 bool sw_state = false;
1280 bool hw_state;
1281 int sw_status;
1283 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1285 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1286 if (atp_rfk)
1287 atp_rfk->rfkill = rfkill_alloc(name,
1288 &tpacpi_pdev->dev,
1289 rfktype,
1290 &tpacpi_rfk_rfkill_ops,
1291 atp_rfk);
1292 if (!atp_rfk || !atp_rfk->rfkill) {
1293 printk(TPACPI_ERR
1294 "failed to allocate memory for rfkill class\n");
1295 kfree(atp_rfk);
1296 return -ENOMEM;
1299 atp_rfk->id = id;
1300 atp_rfk->ops = tp_rfkops;
1302 sw_status = (tp_rfkops->get_status)();
1303 if (sw_status < 0) {
1304 printk(TPACPI_ERR
1305 "failed to read initial state for %s, error %d\n",
1306 name, sw_status);
1307 } else {
1308 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1309 if (set_default) {
1310 /* try to keep the initial state, since we ask the
1311 * firmware to preserve it across S5 in NVRAM */
1312 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1315 hw_state = tpacpi_rfk_check_hwblock_state();
1316 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1318 res = rfkill_register(atp_rfk->rfkill);
1319 if (res < 0) {
1320 printk(TPACPI_ERR
1321 "failed to register %s rfkill switch: %d\n",
1322 name, res);
1323 rfkill_destroy(atp_rfk->rfkill);
1324 kfree(atp_rfk);
1325 return res;
1328 tpacpi_rfkill_switches[id] = atp_rfk;
1330 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1331 name, (sw_state || hw_state) ? "" : "un");
1332 return 0;
1335 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1337 struct tpacpi_rfk *tp_rfk;
1339 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1341 tp_rfk = tpacpi_rfkill_switches[id];
1342 if (tp_rfk) {
1343 rfkill_unregister(tp_rfk->rfkill);
1344 rfkill_destroy(tp_rfk->rfkill);
1345 tpacpi_rfkill_switches[id] = NULL;
1346 kfree(tp_rfk);
1350 static void printk_deprecated_rfkill_attribute(const char * const what)
1352 printk_deprecated_attribute(what,
1353 "Please switch to generic rfkill before year 2010");
1356 /* sysfs <radio> enable ------------------------------------------------ */
1357 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1358 struct device_attribute *attr,
1359 char *buf)
1361 int status;
1363 printk_deprecated_rfkill_attribute(attr->attr.name);
1365 /* This is in the ABI... */
1366 if (tpacpi_rfk_check_hwblock_state()) {
1367 status = TPACPI_RFK_RADIO_OFF;
1368 } else {
1369 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1370 if (status < 0)
1371 return status;
1374 return snprintf(buf, PAGE_SIZE, "%d\n",
1375 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1378 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1379 struct device_attribute *attr,
1380 const char *buf, size_t count)
1382 unsigned long t;
1383 int res;
1385 printk_deprecated_rfkill_attribute(attr->attr.name);
1387 if (parse_strtoul(buf, 1, &t))
1388 return -EINVAL;
1390 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1392 /* This is in the ABI... */
1393 if (tpacpi_rfk_check_hwblock_state() && !!t)
1394 return -EPERM;
1396 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1397 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1398 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1400 return (res < 0) ? res : count;
1403 /* procfs -------------------------------------------------------------- */
1404 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1406 if (id >= TPACPI_RFK_SW_MAX)
1407 seq_printf(m, "status:\t\tnot supported\n");
1408 else {
1409 int status;
1411 /* This is in the ABI... */
1412 if (tpacpi_rfk_check_hwblock_state()) {
1413 status = TPACPI_RFK_RADIO_OFF;
1414 } else {
1415 status = tpacpi_rfk_update_swstate(
1416 tpacpi_rfkill_switches[id]);
1417 if (status < 0)
1418 return status;
1421 seq_printf(m, "status:\t\t%s\n",
1422 (status == TPACPI_RFK_RADIO_ON) ?
1423 "enabled" : "disabled");
1424 seq_printf(m, "commands:\tenable, disable\n");
1427 return 0;
1430 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1432 char *cmd;
1433 int status = -1;
1434 int res = 0;
1436 if (id >= TPACPI_RFK_SW_MAX)
1437 return -ENODEV;
1439 while ((cmd = next_cmd(&buf))) {
1440 if (strlencmp(cmd, "enable") == 0)
1441 status = TPACPI_RFK_RADIO_ON;
1442 else if (strlencmp(cmd, "disable") == 0)
1443 status = TPACPI_RFK_RADIO_OFF;
1444 else
1445 return -EINVAL;
1448 if (status != -1) {
1449 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1450 (status == TPACPI_RFK_RADIO_ON) ?
1451 "enable" : "disable",
1452 tpacpi_rfkill_names[id]);
1453 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1454 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1457 return res;
1460 /*************************************************************************
1461 * thinkpad-acpi driver attributes
1464 /* interface_version --------------------------------------------------- */
1465 static ssize_t tpacpi_driver_interface_version_show(
1466 struct device_driver *drv,
1467 char *buf)
1469 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1472 static DRIVER_ATTR(interface_version, S_IRUGO,
1473 tpacpi_driver_interface_version_show, NULL);
1475 /* debug_level --------------------------------------------------------- */
1476 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1477 char *buf)
1479 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1482 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1483 const char *buf, size_t count)
1485 unsigned long t;
1487 if (parse_strtoul(buf, 0xffff, &t))
1488 return -EINVAL;
1490 dbg_level = t;
1492 return count;
1495 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1496 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1498 /* version ------------------------------------------------------------- */
1499 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1500 char *buf)
1502 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1503 TPACPI_DESC, TPACPI_VERSION);
1506 static DRIVER_ATTR(version, S_IRUGO,
1507 tpacpi_driver_version_show, NULL);
1509 /* --------------------------------------------------------------------- */
1511 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1513 /* wlsw_emulstate ------------------------------------------------------ */
1514 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1515 char *buf)
1517 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1520 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1521 const char *buf, size_t count)
1523 unsigned long t;
1525 if (parse_strtoul(buf, 1, &t))
1526 return -EINVAL;
1528 if (tpacpi_wlsw_emulstate != !!t) {
1529 tpacpi_wlsw_emulstate = !!t;
1530 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1533 return count;
1536 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1537 tpacpi_driver_wlsw_emulstate_show,
1538 tpacpi_driver_wlsw_emulstate_store);
1540 /* bluetooth_emulstate ------------------------------------------------- */
1541 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1542 struct device_driver *drv,
1543 char *buf)
1545 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1548 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1549 struct device_driver *drv,
1550 const char *buf, size_t count)
1552 unsigned long t;
1554 if (parse_strtoul(buf, 1, &t))
1555 return -EINVAL;
1557 tpacpi_bluetooth_emulstate = !!t;
1559 return count;
1562 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1563 tpacpi_driver_bluetooth_emulstate_show,
1564 tpacpi_driver_bluetooth_emulstate_store);
1566 /* wwan_emulstate ------------------------------------------------- */
1567 static ssize_t tpacpi_driver_wwan_emulstate_show(
1568 struct device_driver *drv,
1569 char *buf)
1571 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1574 static ssize_t tpacpi_driver_wwan_emulstate_store(
1575 struct device_driver *drv,
1576 const char *buf, size_t count)
1578 unsigned long t;
1580 if (parse_strtoul(buf, 1, &t))
1581 return -EINVAL;
1583 tpacpi_wwan_emulstate = !!t;
1585 return count;
1588 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1589 tpacpi_driver_wwan_emulstate_show,
1590 tpacpi_driver_wwan_emulstate_store);
1592 /* uwb_emulstate ------------------------------------------------- */
1593 static ssize_t tpacpi_driver_uwb_emulstate_show(
1594 struct device_driver *drv,
1595 char *buf)
1597 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1600 static ssize_t tpacpi_driver_uwb_emulstate_store(
1601 struct device_driver *drv,
1602 const char *buf, size_t count)
1604 unsigned long t;
1606 if (parse_strtoul(buf, 1, &t))
1607 return -EINVAL;
1609 tpacpi_uwb_emulstate = !!t;
1611 return count;
1614 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1615 tpacpi_driver_uwb_emulstate_show,
1616 tpacpi_driver_uwb_emulstate_store);
1617 #endif
1619 /* --------------------------------------------------------------------- */
1621 static struct driver_attribute *tpacpi_driver_attributes[] = {
1622 &driver_attr_debug_level, &driver_attr_version,
1623 &driver_attr_interface_version,
1626 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1628 int i, res;
1630 i = 0;
1631 res = 0;
1632 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1633 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1634 i++;
1637 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1638 if (!res && dbg_wlswemul)
1639 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1640 if (!res && dbg_bluetoothemul)
1641 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1642 if (!res && dbg_wwanemul)
1643 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1644 if (!res && dbg_uwbemul)
1645 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1646 #endif
1648 return res;
1651 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1653 int i;
1655 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1656 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1658 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1659 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1660 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1661 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1662 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1663 #endif
1666 /*************************************************************************
1667 * Firmware Data
1671 * Table of recommended minimum BIOS versions
1673 * Reasons for listing:
1674 * 1. Stable BIOS, listed because the unknown ammount of
1675 * bugs and bad ACPI behaviour on older versions
1677 * 2. BIOS or EC fw with known bugs that trigger on Linux
1679 * 3. BIOS with known reduced functionality in older versions
1681 * We recommend the latest BIOS and EC version.
1682 * We only support the latest BIOS and EC fw version as a rule.
1684 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1685 * Information from users in ThinkWiki
1687 * WARNING: we use this table also to detect that the machine is
1688 * a ThinkPad in some cases, so don't remove entries lightly.
1691 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1692 { .vendor = (__v), \
1693 .bios = TPID(__id1, __id2), \
1694 .ec = TPACPI_MATCH_ANY, \
1695 .quirks = TPACPI_MATCH_ANY << 16 \
1696 | (__bv1) << 8 | (__bv2) }
1698 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1699 __eid, __ev1, __ev2) \
1700 { .vendor = (__v), \
1701 .bios = TPID(__bid1, __bid2), \
1702 .ec = __eid, \
1703 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1704 | (__bv1) << 8 | (__bv2) }
1706 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1707 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1709 /* Outdated IBM BIOSes often lack the EC id string */
1710 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1711 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1712 __bv1, __bv2, TPID(__id1, __id2), \
1713 __ev1, __ev2), \
1714 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1715 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1716 __ev1, __ev2)
1718 /* Outdated IBM BIOSes often lack the EC id string */
1719 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1720 __eid1, __eid2, __ev1, __ev2) \
1721 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1722 __bv1, __bv2, TPID(__eid1, __eid2), \
1723 __ev1, __ev2), \
1724 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1725 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1726 __ev1, __ev2)
1728 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1729 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1731 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1732 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1733 __bv1, __bv2, TPID(__id1, __id2), \
1734 __ev1, __ev2)
1736 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1737 __eid1, __eid2, __ev1, __ev2) \
1738 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1739 __bv1, __bv2, TPID(__eid1, __eid2), \
1740 __ev1, __ev2)
1742 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1743 /* Numeric models ------------------ */
1744 /* FW MODEL BIOS VERS */
1745 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1746 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1747 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1748 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1749 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1750 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1751 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1752 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1753 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1755 /* A-series ------------------------- */
1756 /* FW MODEL BIOS VERS EC VERS */
1757 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1758 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1759 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1760 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1761 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1762 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1763 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1764 TPV_QI0('1', '3', '2', '0'), /* A22m */
1765 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1766 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1767 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1769 /* G-series ------------------------- */
1770 /* FW MODEL BIOS VERS */
1771 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1772 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1774 /* R-series, T-series --------------- */
1775 /* FW MODEL BIOS VERS EC VERS */
1776 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1777 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1778 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1779 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1780 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1781 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1782 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1783 T40/p, T41/p, T42/p (1) */
1784 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1785 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1786 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1787 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1789 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1790 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1791 TPV_QI0('1', '6', '3', '2'), /* T22 */
1792 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1793 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1794 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1796 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1797 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1798 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1800 /* BIOS FW BIOS VERS EC FW EC VERS */
1801 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1802 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1804 /* X-series ------------------------- */
1805 /* FW MODEL BIOS VERS EC VERS */
1806 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1807 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1808 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1809 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1810 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1811 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1812 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1814 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1815 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1817 /* (0) - older versions lack DMI EC fw string and functionality */
1818 /* (1) - older versions known to lack functionality */
1821 #undef TPV_QL1
1822 #undef TPV_QL0
1823 #undef TPV_QI2
1824 #undef TPV_QI1
1825 #undef TPV_QI0
1826 #undef TPV_Q_X
1827 #undef TPV_Q
1829 static void __init tpacpi_check_outdated_fw(void)
1831 unsigned long fwvers;
1832 u16 ec_version, bios_version;
1834 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1835 ARRAY_SIZE(tpacpi_bios_version_qtable));
1837 if (!fwvers)
1838 return;
1840 bios_version = fwvers & 0xffffU;
1841 ec_version = (fwvers >> 16) & 0xffffU;
1843 /* note that unknown versions are set to 0x0000 and we use that */
1844 if ((bios_version > thinkpad_id.bios_release) ||
1845 (ec_version > thinkpad_id.ec_release &&
1846 ec_version != TPACPI_MATCH_ANY)) {
1848 * The changelogs would let us track down the exact
1849 * reason, but it is just too much of a pain to track
1850 * it. We only list BIOSes that are either really
1851 * broken, or really stable to begin with, so it is
1852 * best if the user upgrades the firmware anyway.
1854 printk(TPACPI_WARN
1855 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1856 printk(TPACPI_WARN
1857 "WARNING: This firmware may be missing critical bug "
1858 "fixes and/or important features\n");
1862 static bool __init tpacpi_is_fw_known(void)
1864 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1865 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1868 /****************************************************************************
1869 ****************************************************************************
1871 * Subdrivers
1873 ****************************************************************************
1874 ****************************************************************************/
1876 /*************************************************************************
1877 * thinkpad-acpi init subdriver
1880 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1882 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1883 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1885 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1886 (thinkpad_id.bios_version_str) ?
1887 thinkpad_id.bios_version_str : "unknown",
1888 (thinkpad_id.ec_version_str) ?
1889 thinkpad_id.ec_version_str : "unknown");
1891 if (thinkpad_id.vendor && thinkpad_id.model_str)
1892 printk(TPACPI_INFO "%s %s, model %s\n",
1893 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1894 "IBM" : ((thinkpad_id.vendor ==
1895 PCI_VENDOR_ID_LENOVO) ?
1896 "Lenovo" : "Unknown vendor"),
1897 thinkpad_id.model_str,
1898 (thinkpad_id.nummodel_str) ?
1899 thinkpad_id.nummodel_str : "unknown");
1901 tpacpi_check_outdated_fw();
1902 return 0;
1905 static int thinkpad_acpi_driver_read(struct seq_file *m)
1907 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1908 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1909 return 0;
1912 static struct ibm_struct thinkpad_acpi_driver_data = {
1913 .name = "driver",
1914 .read = thinkpad_acpi_driver_read,
1917 /*************************************************************************
1918 * Hotkey subdriver
1922 * ThinkPad firmware event model
1924 * The ThinkPad firmware has two main event interfaces: normal ACPI
1925 * notifications (which follow the ACPI standard), and a private event
1926 * interface.
1928 * The private event interface also issues events for the hotkeys. As
1929 * the driver gained features, the event handling code ended up being
1930 * built around the hotkey subdriver. This will need to be refactored
1931 * to a more formal event API eventually.
1933 * Some "hotkeys" are actually supposed to be used as event reports,
1934 * such as "brightness has changed", "volume has changed", depending on
1935 * the ThinkPad model and how the firmware is operating.
1937 * Unlike other classes, hotkey-class events have mask/unmask control on
1938 * non-ancient firmware. However, how it behaves changes a lot with the
1939 * firmware model and version.
1942 enum { /* hot key scan codes (derived from ACPI DSDT) */
1943 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1944 TP_ACPI_HOTKEYSCAN_FNF2,
1945 TP_ACPI_HOTKEYSCAN_FNF3,
1946 TP_ACPI_HOTKEYSCAN_FNF4,
1947 TP_ACPI_HOTKEYSCAN_FNF5,
1948 TP_ACPI_HOTKEYSCAN_FNF6,
1949 TP_ACPI_HOTKEYSCAN_FNF7,
1950 TP_ACPI_HOTKEYSCAN_FNF8,
1951 TP_ACPI_HOTKEYSCAN_FNF9,
1952 TP_ACPI_HOTKEYSCAN_FNF10,
1953 TP_ACPI_HOTKEYSCAN_FNF11,
1954 TP_ACPI_HOTKEYSCAN_FNF12,
1955 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1956 TP_ACPI_HOTKEYSCAN_FNINSERT,
1957 TP_ACPI_HOTKEYSCAN_FNDELETE,
1958 TP_ACPI_HOTKEYSCAN_FNHOME,
1959 TP_ACPI_HOTKEYSCAN_FNEND,
1960 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1961 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1962 TP_ACPI_HOTKEYSCAN_FNSPACE,
1963 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1964 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1965 TP_ACPI_HOTKEYSCAN_MUTE,
1966 TP_ACPI_HOTKEYSCAN_THINKPAD,
1969 enum { /* Keys/events available through NVRAM polling */
1970 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1971 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1974 enum { /* Positions of some of the keys in hotkey masks */
1975 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1976 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1977 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1978 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1979 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1980 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1981 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1982 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1983 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1984 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1985 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1988 enum { /* NVRAM to ACPI HKEY group map */
1989 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1990 TP_ACPI_HKEY_ZOOM_MASK |
1991 TP_ACPI_HKEY_DISPSWTCH_MASK |
1992 TP_ACPI_HKEY_HIBERNATE_MASK,
1993 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1994 TP_ACPI_HKEY_BRGHTDWN_MASK,
1995 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1996 TP_ACPI_HKEY_VOLDWN_MASK |
1997 TP_ACPI_HKEY_MUTE_MASK,
2000 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2001 struct tp_nvram_state {
2002 u16 thinkpad_toggle:1;
2003 u16 zoom_toggle:1;
2004 u16 display_toggle:1;
2005 u16 thinklight_toggle:1;
2006 u16 hibernate_toggle:1;
2007 u16 displayexp_toggle:1;
2008 u16 display_state:1;
2009 u16 brightness_toggle:1;
2010 u16 volume_toggle:1;
2011 u16 mute:1;
2013 u8 brightness_level;
2014 u8 volume_level;
2017 /* kthread for the hotkey poller */
2018 static struct task_struct *tpacpi_hotkey_task;
2020 /* Acquired while the poller kthread is running, use to sync start/stop */
2021 static struct mutex hotkey_thread_mutex;
2024 * Acquire mutex to write poller control variables as an
2025 * atomic block.
2027 * Increment hotkey_config_change when changing them if you
2028 * want the kthread to forget old state.
2030 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2032 static struct mutex hotkey_thread_data_mutex;
2033 static unsigned int hotkey_config_change;
2036 * hotkey poller control variables
2038 * Must be atomic or readers will also need to acquire mutex
2040 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2041 * should be used only when the changes need to be taken as
2042 * a block, OR when one needs to force the kthread to forget
2043 * old state.
2045 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2046 static unsigned int hotkey_poll_freq = 10; /* Hz */
2048 #define HOTKEY_CONFIG_CRITICAL_START \
2049 do { \
2050 mutex_lock(&hotkey_thread_data_mutex); \
2051 hotkey_config_change++; \
2052 } while (0);
2053 #define HOTKEY_CONFIG_CRITICAL_END \
2054 mutex_unlock(&hotkey_thread_data_mutex);
2056 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2058 #define hotkey_source_mask 0U
2059 #define HOTKEY_CONFIG_CRITICAL_START
2060 #define HOTKEY_CONFIG_CRITICAL_END
2062 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2064 static struct mutex hotkey_mutex;
2066 static enum { /* Reasons for waking up */
2067 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2068 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2069 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2070 } hotkey_wakeup_reason;
2072 static int hotkey_autosleep_ack;
2074 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2075 static u32 hotkey_all_mask; /* all events supported in fw */
2076 static u32 hotkey_reserved_mask; /* events better left disabled */
2077 static u32 hotkey_driver_mask; /* events needed by the driver */
2078 static u32 hotkey_user_mask; /* events visible to userspace */
2079 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2081 static unsigned int hotkey_report_mode;
2083 static u16 *hotkey_keycode_map;
2085 static struct attribute_set *hotkey_dev_attributes;
2087 static void tpacpi_driver_event(const unsigned int hkey_event);
2088 static void hotkey_driver_event(const unsigned int scancode);
2089 static void hotkey_poll_setup(const bool may_warn);
2091 /* HKEY.MHKG() return bits */
2092 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2094 static int hotkey_get_wlsw(void)
2096 int status;
2098 if (!tp_features.hotkey_wlsw)
2099 return -ENODEV;
2101 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2102 if (dbg_wlswemul)
2103 return (tpacpi_wlsw_emulstate) ?
2104 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2105 #endif
2107 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2108 return -EIO;
2110 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2113 static int hotkey_get_tablet_mode(int *status)
2115 int s;
2117 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2118 return -EIO;
2120 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2121 return 0;
2125 * Reads current event mask from firmware, and updates
2126 * hotkey_acpi_mask accordingly. Also resets any bits
2127 * from hotkey_user_mask that are unavailable to be
2128 * delivered (shadow requirement of the userspace ABI).
2130 * Call with hotkey_mutex held
2132 static int hotkey_mask_get(void)
2134 if (tp_features.hotkey_mask) {
2135 u32 m = 0;
2137 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2138 return -EIO;
2140 hotkey_acpi_mask = m;
2141 } else {
2142 /* no mask support doesn't mean no event support... */
2143 hotkey_acpi_mask = hotkey_all_mask;
2146 /* sync userspace-visible mask */
2147 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2149 return 0;
2152 void static hotkey_mask_warn_incomplete_mask(void)
2154 /* log only what the user can fix... */
2155 const u32 wantedmask = hotkey_driver_mask &
2156 ~(hotkey_acpi_mask | hotkey_source_mask) &
2157 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2159 if (wantedmask)
2160 printk(TPACPI_NOTICE
2161 "required events 0x%08x not enabled!\n",
2162 wantedmask);
2166 * Set the firmware mask when supported
2168 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2170 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2172 * Call with hotkey_mutex held
2174 static int hotkey_mask_set(u32 mask)
2176 int i;
2177 int rc = 0;
2179 const u32 fwmask = mask & ~hotkey_source_mask;
2181 if (tp_features.hotkey_mask) {
2182 for (i = 0; i < 32; i++) {
2183 if (!acpi_evalf(hkey_handle,
2184 NULL, "MHKM", "vdd", i + 1,
2185 !!(mask & (1 << i)))) {
2186 rc = -EIO;
2187 break;
2193 * We *must* make an inconditional call to hotkey_mask_get to
2194 * refresh hotkey_acpi_mask and update hotkey_user_mask
2196 * Take the opportunity to also log when we cannot _enable_
2197 * a given event.
2199 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2200 printk(TPACPI_NOTICE
2201 "asked for hotkey mask 0x%08x, but "
2202 "firmware forced it to 0x%08x\n",
2203 fwmask, hotkey_acpi_mask);
2206 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2207 hotkey_mask_warn_incomplete_mask();
2209 return rc;
2213 * Sets hotkey_user_mask and tries to set the firmware mask
2215 * Call with hotkey_mutex held
2217 static int hotkey_user_mask_set(const u32 mask)
2219 int rc;
2221 /* Give people a chance to notice they are doing something that
2222 * is bound to go boom on their users sooner or later */
2223 if (!tp_warned.hotkey_mask_ff &&
2224 (mask == 0xffff || mask == 0xffffff ||
2225 mask == 0xffffffff)) {
2226 tp_warned.hotkey_mask_ff = 1;
2227 printk(TPACPI_NOTICE
2228 "setting the hotkey mask to 0x%08x is likely "
2229 "not the best way to go about it\n", mask);
2230 printk(TPACPI_NOTICE
2231 "please consider using the driver defaults, "
2232 "and refer to up-to-date thinkpad-acpi "
2233 "documentation\n");
2236 /* Try to enable what the user asked for, plus whatever we need.
2237 * this syncs everything but won't enable bits in hotkey_user_mask */
2238 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2240 /* Enable the available bits in hotkey_user_mask */
2241 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2243 return rc;
2247 * Sets the driver hotkey mask.
2249 * Can be called even if the hotkey subdriver is inactive
2251 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2253 int rc;
2255 /* Do the right thing if hotkey_init has not been called yet */
2256 if (!tp_features.hotkey) {
2257 hotkey_driver_mask = mask;
2258 return 0;
2261 mutex_lock(&hotkey_mutex);
2263 HOTKEY_CONFIG_CRITICAL_START
2264 hotkey_driver_mask = mask;
2265 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2266 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2267 #endif
2268 HOTKEY_CONFIG_CRITICAL_END
2270 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2271 ~hotkey_source_mask);
2272 hotkey_poll_setup(true);
2274 mutex_unlock(&hotkey_mutex);
2276 return rc;
2279 static int hotkey_status_get(int *status)
2281 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2282 return -EIO;
2284 return 0;
2287 static int hotkey_status_set(bool enable)
2289 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2290 return -EIO;
2292 return 0;
2295 static void tpacpi_input_send_tabletsw(void)
2297 int state;
2299 if (tp_features.hotkey_tablet &&
2300 !hotkey_get_tablet_mode(&state)) {
2301 mutex_lock(&tpacpi_inputdev_send_mutex);
2303 input_report_switch(tpacpi_inputdev,
2304 SW_TABLET_MODE, !!state);
2305 input_sync(tpacpi_inputdev);
2307 mutex_unlock(&tpacpi_inputdev_send_mutex);
2311 /* Do NOT call without validating scancode first */
2312 static void tpacpi_input_send_key(const unsigned int scancode)
2314 const unsigned int keycode = hotkey_keycode_map[scancode];
2316 if (keycode != KEY_RESERVED) {
2317 mutex_lock(&tpacpi_inputdev_send_mutex);
2319 input_report_key(tpacpi_inputdev, keycode, 1);
2320 if (keycode == KEY_UNKNOWN)
2321 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2322 scancode);
2323 input_sync(tpacpi_inputdev);
2325 input_report_key(tpacpi_inputdev, keycode, 0);
2326 if (keycode == KEY_UNKNOWN)
2327 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2328 scancode);
2329 input_sync(tpacpi_inputdev);
2331 mutex_unlock(&tpacpi_inputdev_send_mutex);
2335 /* Do NOT call without validating scancode first */
2336 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2338 hotkey_driver_event(scancode);
2339 if (hotkey_user_mask & (1 << scancode))
2340 tpacpi_input_send_key(scancode);
2343 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2344 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2346 /* Do NOT call without validating scancode first */
2347 static void tpacpi_hotkey_send_key(unsigned int scancode)
2349 tpacpi_input_send_key_masked(scancode);
2350 if (hotkey_report_mode < 2) {
2351 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2352 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2356 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2358 u8 d;
2360 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2361 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2362 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2363 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2364 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2365 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2367 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2368 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2369 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2371 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2372 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2373 n->displayexp_toggle =
2374 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2376 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2377 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2378 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2379 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2380 n->brightness_toggle =
2381 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2383 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2384 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2385 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2386 >> TP_NVRAM_POS_LEVEL_VOLUME;
2387 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2388 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2392 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2393 struct tp_nvram_state *newn,
2394 const u32 event_mask)
2397 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2398 do { \
2399 if ((event_mask & (1 << __scancode)) && \
2400 oldn->__member != newn->__member) \
2401 tpacpi_hotkey_send_key(__scancode); \
2402 } while (0)
2404 #define TPACPI_MAY_SEND_KEY(__scancode) \
2405 do { \
2406 if (event_mask & (1 << __scancode)) \
2407 tpacpi_hotkey_send_key(__scancode); \
2408 } while (0)
2410 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2411 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2412 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2413 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2415 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2417 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2419 /* handle volume */
2420 if (oldn->volume_toggle != newn->volume_toggle) {
2421 if (oldn->mute != newn->mute) {
2422 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2424 if (oldn->volume_level > newn->volume_level) {
2425 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2426 } else if (oldn->volume_level < newn->volume_level) {
2427 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2428 } else if (oldn->mute == newn->mute) {
2429 /* repeated key presses that didn't change state */
2430 if (newn->mute) {
2431 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2432 } else if (newn->volume_level != 0) {
2433 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2434 } else {
2435 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2440 /* handle brightness */
2441 if (oldn->brightness_toggle != newn->brightness_toggle) {
2442 if (oldn->brightness_level < newn->brightness_level) {
2443 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2444 } else if (oldn->brightness_level > newn->brightness_level) {
2445 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2446 } else {
2447 /* repeated key presses that didn't change state */
2448 if (newn->brightness_level != 0) {
2449 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2450 } else {
2451 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2456 #undef TPACPI_COMPARE_KEY
2457 #undef TPACPI_MAY_SEND_KEY
2461 * Polling driver
2463 * We track all events in hotkey_source_mask all the time, since
2464 * most of them are edge-based. We only issue those requested by
2465 * hotkey_user_mask or hotkey_driver_mask, though.
2467 static int hotkey_kthread(void *data)
2469 struct tp_nvram_state s[2];
2470 u32 poll_mask, event_mask;
2471 unsigned int si, so;
2472 unsigned long t;
2473 unsigned int change_detector, must_reset;
2474 unsigned int poll_freq;
2476 mutex_lock(&hotkey_thread_mutex);
2478 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2479 goto exit;
2481 set_freezable();
2483 so = 0;
2484 si = 1;
2485 t = 0;
2487 /* Initial state for compares */
2488 mutex_lock(&hotkey_thread_data_mutex);
2489 change_detector = hotkey_config_change;
2490 poll_mask = hotkey_source_mask;
2491 event_mask = hotkey_source_mask &
2492 (hotkey_driver_mask | hotkey_user_mask);
2493 poll_freq = hotkey_poll_freq;
2494 mutex_unlock(&hotkey_thread_data_mutex);
2495 hotkey_read_nvram(&s[so], poll_mask);
2497 while (!kthread_should_stop()) {
2498 if (t == 0) {
2499 if (likely(poll_freq))
2500 t = 1000/poll_freq;
2501 else
2502 t = 100; /* should never happen... */
2504 t = msleep_interruptible(t);
2505 if (unlikely(kthread_should_stop()))
2506 break;
2507 must_reset = try_to_freeze();
2508 if (t > 0 && !must_reset)
2509 continue;
2511 mutex_lock(&hotkey_thread_data_mutex);
2512 if (must_reset || hotkey_config_change != change_detector) {
2513 /* forget old state on thaw or config change */
2514 si = so;
2515 t = 0;
2516 change_detector = hotkey_config_change;
2518 poll_mask = hotkey_source_mask;
2519 event_mask = hotkey_source_mask &
2520 (hotkey_driver_mask | hotkey_user_mask);
2521 poll_freq = hotkey_poll_freq;
2522 mutex_unlock(&hotkey_thread_data_mutex);
2524 if (likely(poll_mask)) {
2525 hotkey_read_nvram(&s[si], poll_mask);
2526 if (likely(si != so)) {
2527 hotkey_compare_and_issue_event(&s[so], &s[si],
2528 event_mask);
2532 so = si;
2533 si ^= 1;
2536 exit:
2537 mutex_unlock(&hotkey_thread_mutex);
2538 return 0;
2541 /* call with hotkey_mutex held */
2542 static void hotkey_poll_stop_sync(void)
2544 if (tpacpi_hotkey_task) {
2545 if (frozen(tpacpi_hotkey_task) ||
2546 freezing(tpacpi_hotkey_task))
2547 thaw_process(tpacpi_hotkey_task);
2549 kthread_stop(tpacpi_hotkey_task);
2550 tpacpi_hotkey_task = NULL;
2551 mutex_lock(&hotkey_thread_mutex);
2552 /* at this point, the thread did exit */
2553 mutex_unlock(&hotkey_thread_mutex);
2557 /* call with hotkey_mutex held */
2558 static void hotkey_poll_setup(const bool may_warn)
2560 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2561 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2563 if (hotkey_poll_freq > 0 &&
2564 (poll_driver_mask ||
2565 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2566 if (!tpacpi_hotkey_task) {
2567 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2568 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2569 if (IS_ERR(tpacpi_hotkey_task)) {
2570 tpacpi_hotkey_task = NULL;
2571 printk(TPACPI_ERR
2572 "could not create kernel thread "
2573 "for hotkey polling\n");
2576 } else {
2577 hotkey_poll_stop_sync();
2578 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2579 hotkey_poll_freq == 0) {
2580 printk(TPACPI_NOTICE
2581 "hot keys 0x%08x and/or events 0x%08x "
2582 "require polling, which is currently "
2583 "disabled\n",
2584 poll_user_mask, poll_driver_mask);
2589 static void hotkey_poll_setup_safe(const bool may_warn)
2591 mutex_lock(&hotkey_mutex);
2592 hotkey_poll_setup(may_warn);
2593 mutex_unlock(&hotkey_mutex);
2596 /* call with hotkey_mutex held */
2597 static void hotkey_poll_set_freq(unsigned int freq)
2599 if (!freq)
2600 hotkey_poll_stop_sync();
2602 hotkey_poll_freq = freq;
2605 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2607 static void hotkey_poll_setup(const bool __unused)
2611 static void hotkey_poll_setup_safe(const bool __unused)
2615 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2617 static int hotkey_inputdev_open(struct input_dev *dev)
2619 switch (tpacpi_lifecycle) {
2620 case TPACPI_LIFE_INIT:
2621 case TPACPI_LIFE_RUNNING:
2622 hotkey_poll_setup_safe(false);
2623 return 0;
2624 case TPACPI_LIFE_EXITING:
2625 return -EBUSY;
2628 /* Should only happen if tpacpi_lifecycle is corrupt */
2629 BUG();
2630 return -EBUSY;
2633 static void hotkey_inputdev_close(struct input_dev *dev)
2635 /* disable hotkey polling when possible */
2636 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2637 !(hotkey_source_mask & hotkey_driver_mask))
2638 hotkey_poll_setup_safe(false);
2641 /* sysfs hotkey enable ------------------------------------------------- */
2642 static ssize_t hotkey_enable_show(struct device *dev,
2643 struct device_attribute *attr,
2644 char *buf)
2646 int res, status;
2648 printk_deprecated_attribute("hotkey_enable",
2649 "Hotkey reporting is always enabled");
2651 res = hotkey_status_get(&status);
2652 if (res)
2653 return res;
2655 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2658 static ssize_t hotkey_enable_store(struct device *dev,
2659 struct device_attribute *attr,
2660 const char *buf, size_t count)
2662 unsigned long t;
2664 printk_deprecated_attribute("hotkey_enable",
2665 "Hotkeys can be disabled through hotkey_mask");
2667 if (parse_strtoul(buf, 1, &t))
2668 return -EINVAL;
2670 if (t == 0)
2671 return -EPERM;
2673 return count;
2676 static struct device_attribute dev_attr_hotkey_enable =
2677 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2678 hotkey_enable_show, hotkey_enable_store);
2680 /* sysfs hotkey mask --------------------------------------------------- */
2681 static ssize_t hotkey_mask_show(struct device *dev,
2682 struct device_attribute *attr,
2683 char *buf)
2685 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2688 static ssize_t hotkey_mask_store(struct device *dev,
2689 struct device_attribute *attr,
2690 const char *buf, size_t count)
2692 unsigned long t;
2693 int res;
2695 if (parse_strtoul(buf, 0xffffffffUL, &t))
2696 return -EINVAL;
2698 if (mutex_lock_killable(&hotkey_mutex))
2699 return -ERESTARTSYS;
2701 res = hotkey_user_mask_set(t);
2703 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2704 hotkey_poll_setup(true);
2705 #endif
2707 mutex_unlock(&hotkey_mutex);
2709 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2711 return (res) ? res : count;
2714 static struct device_attribute dev_attr_hotkey_mask =
2715 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2716 hotkey_mask_show, hotkey_mask_store);
2718 /* sysfs hotkey bios_enabled ------------------------------------------- */
2719 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2720 struct device_attribute *attr,
2721 char *buf)
2723 return sprintf(buf, "0\n");
2726 static struct device_attribute dev_attr_hotkey_bios_enabled =
2727 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2729 /* sysfs hotkey bios_mask ---------------------------------------------- */
2730 static ssize_t hotkey_bios_mask_show(struct device *dev,
2731 struct device_attribute *attr,
2732 char *buf)
2734 printk_deprecated_attribute("hotkey_bios_mask",
2735 "This attribute is useless.");
2736 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2739 static struct device_attribute dev_attr_hotkey_bios_mask =
2740 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2742 /* sysfs hotkey all_mask ----------------------------------------------- */
2743 static ssize_t hotkey_all_mask_show(struct device *dev,
2744 struct device_attribute *attr,
2745 char *buf)
2747 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2748 hotkey_all_mask | hotkey_source_mask);
2751 static struct device_attribute dev_attr_hotkey_all_mask =
2752 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2754 /* sysfs hotkey recommended_mask --------------------------------------- */
2755 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2756 struct device_attribute *attr,
2757 char *buf)
2759 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2760 (hotkey_all_mask | hotkey_source_mask)
2761 & ~hotkey_reserved_mask);
2764 static struct device_attribute dev_attr_hotkey_recommended_mask =
2765 __ATTR(hotkey_recommended_mask, S_IRUGO,
2766 hotkey_recommended_mask_show, NULL);
2768 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2770 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2771 static ssize_t hotkey_source_mask_show(struct device *dev,
2772 struct device_attribute *attr,
2773 char *buf)
2775 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2778 static ssize_t hotkey_source_mask_store(struct device *dev,
2779 struct device_attribute *attr,
2780 const char *buf, size_t count)
2782 unsigned long t;
2783 u32 r_ev;
2784 int rc;
2786 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2787 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2788 return -EINVAL;
2790 if (mutex_lock_killable(&hotkey_mutex))
2791 return -ERESTARTSYS;
2793 HOTKEY_CONFIG_CRITICAL_START
2794 hotkey_source_mask = t;
2795 HOTKEY_CONFIG_CRITICAL_END
2797 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2798 ~hotkey_source_mask);
2799 hotkey_poll_setup(true);
2801 /* check if events needed by the driver got disabled */
2802 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2803 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2805 mutex_unlock(&hotkey_mutex);
2807 if (rc < 0)
2808 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2809 "firmware event mask!\n");
2811 if (r_ev)
2812 printk(TPACPI_NOTICE "hotkey_source_mask: "
2813 "some important events were disabled: "
2814 "0x%04x\n", r_ev);
2816 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2818 return (rc < 0) ? rc : count;
2821 static struct device_attribute dev_attr_hotkey_source_mask =
2822 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2823 hotkey_source_mask_show, hotkey_source_mask_store);
2825 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2826 static ssize_t hotkey_poll_freq_show(struct device *dev,
2827 struct device_attribute *attr,
2828 char *buf)
2830 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2833 static ssize_t hotkey_poll_freq_store(struct device *dev,
2834 struct device_attribute *attr,
2835 const char *buf, size_t count)
2837 unsigned long t;
2839 if (parse_strtoul(buf, 25, &t))
2840 return -EINVAL;
2842 if (mutex_lock_killable(&hotkey_mutex))
2843 return -ERESTARTSYS;
2845 hotkey_poll_set_freq(t);
2846 hotkey_poll_setup(true);
2848 mutex_unlock(&hotkey_mutex);
2850 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2852 return count;
2855 static struct device_attribute dev_attr_hotkey_poll_freq =
2856 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2857 hotkey_poll_freq_show, hotkey_poll_freq_store);
2859 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2861 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2862 static ssize_t hotkey_radio_sw_show(struct device *dev,
2863 struct device_attribute *attr,
2864 char *buf)
2866 int res;
2867 res = hotkey_get_wlsw();
2868 if (res < 0)
2869 return res;
2871 /* Opportunistic update */
2872 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2874 return snprintf(buf, PAGE_SIZE, "%d\n",
2875 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2878 static struct device_attribute dev_attr_hotkey_radio_sw =
2879 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2881 static void hotkey_radio_sw_notify_change(void)
2883 if (tp_features.hotkey_wlsw)
2884 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2885 "hotkey_radio_sw");
2888 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2889 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2890 struct device_attribute *attr,
2891 char *buf)
2893 int res, s;
2894 res = hotkey_get_tablet_mode(&s);
2895 if (res < 0)
2896 return res;
2898 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2901 static struct device_attribute dev_attr_hotkey_tablet_mode =
2902 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2904 static void hotkey_tablet_mode_notify_change(void)
2906 if (tp_features.hotkey_tablet)
2907 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2908 "hotkey_tablet_mode");
2911 /* sysfs hotkey report_mode -------------------------------------------- */
2912 static ssize_t hotkey_report_mode_show(struct device *dev,
2913 struct device_attribute *attr,
2914 char *buf)
2916 return snprintf(buf, PAGE_SIZE, "%d\n",
2917 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2920 static struct device_attribute dev_attr_hotkey_report_mode =
2921 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2923 /* sysfs wakeup reason (pollable) -------------------------------------- */
2924 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2925 struct device_attribute *attr,
2926 char *buf)
2928 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2931 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2932 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2934 static void hotkey_wakeup_reason_notify_change(void)
2936 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2937 "wakeup_reason");
2940 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2941 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2942 struct device_attribute *attr,
2943 char *buf)
2945 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2948 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2949 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2950 hotkey_wakeup_hotunplug_complete_show, NULL);
2952 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2954 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2955 "wakeup_hotunplug_complete");
2958 /* --------------------------------------------------------------------- */
2960 static struct attribute *hotkey_attributes[] __initdata = {
2961 &dev_attr_hotkey_enable.attr,
2962 &dev_attr_hotkey_bios_enabled.attr,
2963 &dev_attr_hotkey_bios_mask.attr,
2964 &dev_attr_hotkey_report_mode.attr,
2965 &dev_attr_hotkey_wakeup_reason.attr,
2966 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2967 &dev_attr_hotkey_mask.attr,
2968 &dev_attr_hotkey_all_mask.attr,
2969 &dev_attr_hotkey_recommended_mask.attr,
2970 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2971 &dev_attr_hotkey_source_mask.attr,
2972 &dev_attr_hotkey_poll_freq.attr,
2973 #endif
2977 * Sync both the hw and sw blocking state of all switches
2979 static void tpacpi_send_radiosw_update(void)
2981 int wlsw;
2984 * We must sync all rfkill controllers *before* issuing any
2985 * rfkill input events, or we will race the rfkill core input
2986 * handler.
2988 * tpacpi_inputdev_send_mutex works as a syncronization point
2989 * for the above.
2991 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2994 wlsw = hotkey_get_wlsw();
2996 /* Sync hw blocking state first if it is hw-blocked */
2997 if (wlsw == TPACPI_RFK_RADIO_OFF)
2998 tpacpi_rfk_update_hwblock_state(true);
3000 /* Sync sw blocking state */
3001 tpacpi_rfk_update_swstate_all();
3003 /* Sync hw blocking state last if it is hw-unblocked */
3004 if (wlsw == TPACPI_RFK_RADIO_ON)
3005 tpacpi_rfk_update_hwblock_state(false);
3007 /* Issue rfkill input event for WLSW switch */
3008 if (!(wlsw < 0)) {
3009 mutex_lock(&tpacpi_inputdev_send_mutex);
3011 input_report_switch(tpacpi_inputdev,
3012 SW_RFKILL_ALL, (wlsw > 0));
3013 input_sync(tpacpi_inputdev);
3015 mutex_unlock(&tpacpi_inputdev_send_mutex);
3019 * this can be unconditional, as we will poll state again
3020 * if userspace uses the notify to read data
3022 hotkey_radio_sw_notify_change();
3025 static void hotkey_exit(void)
3027 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3028 mutex_lock(&hotkey_mutex);
3029 hotkey_poll_stop_sync();
3030 mutex_unlock(&hotkey_mutex);
3031 #endif
3033 if (hotkey_dev_attributes)
3034 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3036 kfree(hotkey_keycode_map);
3038 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3039 "restoring original HKEY status and mask\n");
3040 /* yes, there is a bitwise or below, we want the
3041 * functions to be called even if one of them fail */
3042 if (((tp_features.hotkey_mask &&
3043 hotkey_mask_set(hotkey_orig_mask)) |
3044 hotkey_status_set(false)) != 0)
3045 printk(TPACPI_ERR
3046 "failed to restore hot key mask "
3047 "to BIOS defaults\n");
3050 static void __init hotkey_unmap(const unsigned int scancode)
3052 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3053 clear_bit(hotkey_keycode_map[scancode],
3054 tpacpi_inputdev->keybit);
3055 hotkey_keycode_map[scancode] = KEY_RESERVED;
3060 * HKEY quirks:
3061 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3064 #define TPACPI_HK_Q_INIMASK 0x0001
3066 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3067 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3068 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3069 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3070 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3071 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3072 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3073 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3074 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3075 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3076 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3077 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3078 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3079 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3080 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3081 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3082 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3083 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3084 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3085 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3088 static int __init hotkey_init(struct ibm_init_struct *iibm)
3090 /* Requirements for changing the default keymaps:
3092 * 1. Many of the keys are mapped to KEY_RESERVED for very
3093 * good reasons. Do not change them unless you have deep
3094 * knowledge on the IBM and Lenovo ThinkPad firmware for
3095 * the various ThinkPad models. The driver behaves
3096 * differently for KEY_RESERVED: such keys have their
3097 * hot key mask *unset* in mask_recommended, and also
3098 * in the initial hot key mask programmed into the
3099 * firmware at driver load time, which means the firm-
3100 * ware may react very differently if you change them to
3101 * something else;
3103 * 2. You must be subscribed to the linux-thinkpad and
3104 * ibm-acpi-devel mailing lists, and you should read the
3105 * list archives since 2007 if you want to change the
3106 * keymaps. This requirement exists so that you will
3107 * know the past history of problems with the thinkpad-
3108 * acpi driver keymaps, and also that you will be
3109 * listening to any bug reports;
3111 * 3. Do not send thinkpad-acpi specific patches directly to
3112 * for merging, *ever*. Send them to the linux-acpi
3113 * mailinglist for comments. Merging is to be done only
3114 * through acpi-test and the ACPI maintainer.
3116 * If the above is too much to ask, don't change the keymap.
3117 * Ask the thinkpad-acpi maintainer to do it, instead.
3119 static u16 ibm_keycode_map[] __initdata = {
3120 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3121 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3122 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3123 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3125 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3126 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3127 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3128 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3130 /* brightness: firmware always reacts to them */
3131 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3132 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3134 /* Thinklight: firmware always react to it */
3135 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3137 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3138 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3140 /* Volume: firmware always react to it and reprograms
3141 * the built-in *extra* mixer. Never map it to control
3142 * another mixer by default. */
3143 KEY_RESERVED, /* 0x14: VOLUME UP */
3144 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3145 KEY_RESERVED, /* 0x16: MUTE */
3147 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3149 /* (assignments unknown, please report if found) */
3150 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3151 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3153 static u16 lenovo_keycode_map[] __initdata = {
3154 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3155 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3156 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3157 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3159 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3160 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3161 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3162 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3164 /* These should be enabled --only-- when ACPI video
3165 * is disabled (i.e. in "vendor" mode), and are handled
3166 * in a special way by the init code */
3167 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3168 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3170 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3172 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3173 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3175 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3176 * react to it and reprograms the built-in *extra* mixer.
3177 * Never map it to control another mixer by default.
3179 * T60?, T61, R60?, R61: firmware and EC tries to send
3180 * these over the regular keyboard, so these are no-ops,
3181 * but there are still weird bugs re. MUTE, so do not
3182 * change unless you get test reports from all Lenovo
3183 * models. May cause the BIOS to interfere with the
3184 * HDA mixer.
3186 KEY_RESERVED, /* 0x14: VOLUME UP */
3187 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3188 KEY_RESERVED, /* 0x16: MUTE */
3190 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3192 /* (assignments unknown, please report if found) */
3193 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3194 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3197 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3198 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3199 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3201 int res, i;
3202 int status;
3203 int hkeyv;
3204 bool radiosw_state = false;
3205 bool tabletsw_state = false;
3207 unsigned long quirks;
3209 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3210 "initializing hotkey subdriver\n");
3212 BUG_ON(!tpacpi_inputdev);
3213 BUG_ON(tpacpi_inputdev->open != NULL ||
3214 tpacpi_inputdev->close != NULL);
3216 TPACPI_ACPIHANDLE_INIT(hkey);
3217 mutex_init(&hotkey_mutex);
3219 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3220 mutex_init(&hotkey_thread_mutex);
3221 mutex_init(&hotkey_thread_data_mutex);
3222 #endif
3224 /* hotkey not supported on 570 */
3225 tp_features.hotkey = hkey_handle != NULL;
3227 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3228 "hotkeys are %s\n",
3229 str_supported(tp_features.hotkey));
3231 if (!tp_features.hotkey)
3232 return 1;
3234 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3235 ARRAY_SIZE(tpacpi_hotkey_qtable));
3237 tpacpi_disable_brightness_delay();
3239 /* MUST have enough space for all attributes to be added to
3240 * hotkey_dev_attributes */
3241 hotkey_dev_attributes = create_attr_set(
3242 ARRAY_SIZE(hotkey_attributes) + 2,
3243 NULL);
3244 if (!hotkey_dev_attributes)
3245 return -ENOMEM;
3246 res = add_many_to_attr_set(hotkey_dev_attributes,
3247 hotkey_attributes,
3248 ARRAY_SIZE(hotkey_attributes));
3249 if (res)
3250 goto err_exit;
3252 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3253 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3254 for HKEY interface version 0x100 */
3255 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3256 if ((hkeyv >> 8) != 1) {
3257 printk(TPACPI_ERR "unknown version of the "
3258 "HKEY interface: 0x%x\n", hkeyv);
3259 printk(TPACPI_ERR "please report this to %s\n",
3260 TPACPI_MAIL);
3261 } else {
3263 * MHKV 0x100 in A31, R40, R40e,
3264 * T4x, X31, and later
3266 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3267 "firmware HKEY interface version: 0x%x\n",
3268 hkeyv);
3270 /* Paranoia check AND init hotkey_all_mask */
3271 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3272 "MHKA", "qd")) {
3273 printk(TPACPI_ERR
3274 "missing MHKA handler, "
3275 "please report this to %s\n",
3276 TPACPI_MAIL);
3277 /* Fallback: pre-init for FN+F3,F4,F12 */
3278 hotkey_all_mask = 0x080cU;
3279 } else {
3280 tp_features.hotkey_mask = 1;
3285 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3286 "hotkey masks are %s\n",
3287 str_supported(tp_features.hotkey_mask));
3289 /* Init hotkey_all_mask if not initialized yet */
3290 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3291 (quirks & TPACPI_HK_Q_INIMASK))
3292 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3294 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3295 if (tp_features.hotkey_mask) {
3296 /* hotkey_source_mask *must* be zero for
3297 * the first hotkey_mask_get to return hotkey_orig_mask */
3298 res = hotkey_mask_get();
3299 if (res)
3300 goto err_exit;
3302 hotkey_orig_mask = hotkey_acpi_mask;
3303 } else {
3304 hotkey_orig_mask = hotkey_all_mask;
3305 hotkey_acpi_mask = hotkey_all_mask;
3308 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3309 if (dbg_wlswemul) {
3310 tp_features.hotkey_wlsw = 1;
3311 radiosw_state = !!tpacpi_wlsw_emulstate;
3312 printk(TPACPI_INFO
3313 "radio switch emulation enabled\n");
3314 } else
3315 #endif
3316 /* Not all thinkpads have a hardware radio switch */
3317 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3318 tp_features.hotkey_wlsw = 1;
3319 radiosw_state = !!status;
3320 printk(TPACPI_INFO
3321 "radio switch found; radios are %s\n",
3322 enabled(status, 0));
3324 if (tp_features.hotkey_wlsw)
3325 res = add_to_attr_set(hotkey_dev_attributes,
3326 &dev_attr_hotkey_radio_sw.attr);
3328 /* For X41t, X60t, X61t Tablets... */
3329 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3330 tp_features.hotkey_tablet = 1;
3331 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3332 printk(TPACPI_INFO
3333 "possible tablet mode switch found; "
3334 "ThinkPad in %s mode\n",
3335 (tabletsw_state) ? "tablet" : "laptop");
3336 res = add_to_attr_set(hotkey_dev_attributes,
3337 &dev_attr_hotkey_tablet_mode.attr);
3340 if (!res)
3341 res = register_attr_set_with_sysfs(
3342 hotkey_dev_attributes,
3343 &tpacpi_pdev->dev.kobj);
3344 if (res)
3345 goto err_exit;
3347 /* Set up key map */
3349 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3350 GFP_KERNEL);
3351 if (!hotkey_keycode_map) {
3352 printk(TPACPI_ERR
3353 "failed to allocate memory for key map\n");
3354 res = -ENOMEM;
3355 goto err_exit;
3358 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
3359 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3360 "using Lenovo default hot key map\n");
3361 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3362 TPACPI_HOTKEY_MAP_SIZE);
3363 } else {
3364 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3365 "using IBM default hot key map\n");
3366 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3367 TPACPI_HOTKEY_MAP_SIZE);
3370 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3371 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3372 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3373 tpacpi_inputdev->keycode = hotkey_keycode_map;
3374 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3375 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3376 input_set_capability(tpacpi_inputdev, EV_KEY,
3377 hotkey_keycode_map[i]);
3378 } else {
3379 if (i < sizeof(hotkey_reserved_mask)*8)
3380 hotkey_reserved_mask |= 1 << i;
3384 if (tp_features.hotkey_wlsw) {
3385 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3386 input_report_switch(tpacpi_inputdev,
3387 SW_RFKILL_ALL, radiosw_state);
3389 if (tp_features.hotkey_tablet) {
3390 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3391 input_report_switch(tpacpi_inputdev,
3392 SW_TABLET_MODE, tabletsw_state);
3395 /* Do not issue duplicate brightness change events to
3396 * userspace */
3397 if (!tp_features.bright_acpimode)
3398 /* update bright_acpimode... */
3399 tpacpi_check_std_acpi_brightness_support();
3401 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3402 printk(TPACPI_INFO
3403 "This ThinkPad has standard ACPI backlight "
3404 "brightness control, supported by the ACPI "
3405 "video driver\n");
3406 printk(TPACPI_NOTICE
3407 "Disabling thinkpad-acpi brightness events "
3408 "by default...\n");
3410 /* Disable brightness up/down on Lenovo thinkpads when
3411 * ACPI is handling them, otherwise it is plain impossible
3412 * for userspace to do something even remotely sane */
3413 hotkey_reserved_mask |=
3414 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3415 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3416 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3417 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3420 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3421 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3422 & ~hotkey_all_mask
3423 & ~hotkey_reserved_mask;
3425 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3426 "hotkey source mask 0x%08x, polling freq %u\n",
3427 hotkey_source_mask, hotkey_poll_freq);
3428 #endif
3430 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3431 "enabling firmware HKEY event interface...\n");
3432 res = hotkey_status_set(true);
3433 if (res) {
3434 hotkey_exit();
3435 return res;
3437 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3438 | hotkey_driver_mask)
3439 & ~hotkey_source_mask);
3440 if (res < 0 && res != -ENXIO) {
3441 hotkey_exit();
3442 return res;
3444 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3445 & ~hotkey_reserved_mask;
3446 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3447 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3448 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3450 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3451 "legacy ibm/hotkey event reporting over procfs %s\n",
3452 (hotkey_report_mode < 2) ?
3453 "enabled" : "disabled");
3455 tpacpi_inputdev->open = &hotkey_inputdev_open;
3456 tpacpi_inputdev->close = &hotkey_inputdev_close;
3458 hotkey_poll_setup_safe(true);
3460 return 0;
3462 err_exit:
3463 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3464 hotkey_dev_attributes = NULL;
3466 return (res < 0)? res : 1;
3469 static bool hotkey_notify_hotkey(const u32 hkey,
3470 bool *send_acpi_ev,
3471 bool *ignore_acpi_ev)
3473 /* 0x1000-0x1FFF: key presses */
3474 unsigned int scancode = hkey & 0xfff;
3475 *send_acpi_ev = true;
3476 *ignore_acpi_ev = false;
3478 if (scancode > 0 && scancode < 0x21) {
3479 scancode--;
3480 if (!(hotkey_source_mask & (1 << scancode))) {
3481 tpacpi_input_send_key_masked(scancode);
3482 *send_acpi_ev = false;
3483 } else {
3484 *ignore_acpi_ev = true;
3486 return true;
3488 return false;
3491 static bool hotkey_notify_wakeup(const u32 hkey,
3492 bool *send_acpi_ev,
3493 bool *ignore_acpi_ev)
3495 /* 0x2000-0x2FFF: Wakeup reason */
3496 *send_acpi_ev = true;
3497 *ignore_acpi_ev = false;
3499 switch (hkey) {
3500 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3501 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3502 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3503 *ignore_acpi_ev = true;
3504 break;
3506 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3507 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3508 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3509 *ignore_acpi_ev = true;
3510 break;
3512 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3513 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3514 printk(TPACPI_ALERT
3515 "EMERGENCY WAKEUP: battery almost empty\n");
3516 /* how to auto-heal: */
3517 /* 2313: woke up from S3, go to S4/S5 */
3518 /* 2413: woke up from S4, go to S5 */
3519 break;
3521 default:
3522 return false;
3525 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3526 printk(TPACPI_INFO
3527 "woke up due to a hot-unplug "
3528 "request...\n");
3529 hotkey_wakeup_reason_notify_change();
3531 return true;
3534 static bool hotkey_notify_usrevent(const u32 hkey,
3535 bool *send_acpi_ev,
3536 bool *ignore_acpi_ev)
3538 /* 0x5000-0x5FFF: human interface helpers */
3539 *send_acpi_ev = true;
3540 *ignore_acpi_ev = false;
3542 switch (hkey) {
3543 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3544 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3545 return true;
3547 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3548 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3549 tpacpi_input_send_tabletsw();
3550 hotkey_tablet_mode_notify_change();
3551 *send_acpi_ev = false;
3552 return true;
3554 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3555 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3556 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3557 /* do not propagate these events */
3558 *ignore_acpi_ev = true;
3559 return true;
3561 default:
3562 return false;
3566 static void thermal_dump_all_sensors(void);
3568 static bool hotkey_notify_thermal(const u32 hkey,
3569 bool *send_acpi_ev,
3570 bool *ignore_acpi_ev)
3572 bool known = true;
3574 /* 0x6000-0x6FFF: thermal alarms */
3575 *send_acpi_ev = true;
3576 *ignore_acpi_ev = false;
3578 switch (hkey) {
3579 case TP_HKEY_EV_THM_TABLE_CHANGED:
3580 printk(TPACPI_INFO
3581 "EC reports that Thermal Table has changed\n");
3582 /* recommended action: do nothing, we don't have
3583 * Lenovo ATM information */
3584 return true;
3585 case TP_HKEY_EV_ALARM_BAT_HOT:
3586 printk(TPACPI_CRIT
3587 "THERMAL ALARM: battery is too hot!\n");
3588 /* recommended action: warn user through gui */
3589 break;
3590 case TP_HKEY_EV_ALARM_BAT_XHOT:
3591 printk(TPACPI_ALERT
3592 "THERMAL EMERGENCY: battery is extremely hot!\n");
3593 /* recommended action: immediate sleep/hibernate */
3594 break;
3595 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3596 printk(TPACPI_CRIT
3597 "THERMAL ALARM: "
3598 "a sensor reports something is too hot!\n");
3599 /* recommended action: warn user through gui, that */
3600 /* some internal component is too hot */
3601 break;
3602 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3603 printk(TPACPI_ALERT
3604 "THERMAL EMERGENCY: "
3605 "a sensor reports something is extremely hot!\n");
3606 /* recommended action: immediate sleep/hibernate */
3607 break;
3608 default:
3609 printk(TPACPI_ALERT
3610 "THERMAL ALERT: unknown thermal alarm received\n");
3611 known = false;
3614 thermal_dump_all_sensors();
3616 return known;
3619 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3621 u32 hkey;
3622 bool send_acpi_ev;
3623 bool ignore_acpi_ev;
3624 bool known_ev;
3626 if (event != 0x80) {
3627 printk(TPACPI_ERR
3628 "unknown HKEY notification event %d\n", event);
3629 /* forward it to userspace, maybe it knows how to handle it */
3630 acpi_bus_generate_netlink_event(
3631 ibm->acpi->device->pnp.device_class,
3632 dev_name(&ibm->acpi->device->dev),
3633 event, 0);
3634 return;
3637 while (1) {
3638 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3639 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3640 return;
3643 if (hkey == 0) {
3644 /* queue empty */
3645 return;
3648 send_acpi_ev = true;
3649 ignore_acpi_ev = false;
3651 switch (hkey >> 12) {
3652 case 1:
3653 /* 0x1000-0x1FFF: key presses */
3654 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3655 &ignore_acpi_ev);
3656 break;
3657 case 2:
3658 /* 0x2000-0x2FFF: Wakeup reason */
3659 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3660 &ignore_acpi_ev);
3661 break;
3662 case 3:
3663 /* 0x3000-0x3FFF: bay-related wakeups */
3664 switch (hkey) {
3665 case TP_HKEY_EV_BAYEJ_ACK:
3666 hotkey_autosleep_ack = 1;
3667 printk(TPACPI_INFO
3668 "bay ejected\n");
3669 hotkey_wakeup_hotunplug_complete_notify_change();
3670 known_ev = true;
3671 break;
3672 case TP_HKEY_EV_OPTDRV_EJ:
3673 /* FIXME: kick libata if SATA link offline */
3674 known_ev = true;
3675 break;
3676 default:
3677 known_ev = false;
3679 break;
3680 case 4:
3681 /* 0x4000-0x4FFF: dock-related wakeups */
3682 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3683 hotkey_autosleep_ack = 1;
3684 printk(TPACPI_INFO
3685 "undocked\n");
3686 hotkey_wakeup_hotunplug_complete_notify_change();
3687 known_ev = true;
3688 } else {
3689 known_ev = false;
3691 break;
3692 case 5:
3693 /* 0x5000-0x5FFF: human interface helpers */
3694 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3695 &ignore_acpi_ev);
3696 break;
3697 case 6:
3698 /* 0x6000-0x6FFF: thermal alarms */
3699 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3700 &ignore_acpi_ev);
3701 break;
3702 case 7:
3703 /* 0x7000-0x7FFF: misc */
3704 if (tp_features.hotkey_wlsw &&
3705 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3706 tpacpi_send_radiosw_update();
3707 send_acpi_ev = 0;
3708 known_ev = true;
3709 break;
3711 /* fallthrough to default */
3712 default:
3713 known_ev = false;
3715 if (!known_ev) {
3716 printk(TPACPI_NOTICE
3717 "unhandled HKEY event 0x%04x\n", hkey);
3718 printk(TPACPI_NOTICE
3719 "please report the conditions when this "
3720 "event happened to %s\n", TPACPI_MAIL);
3723 /* Legacy events */
3724 if (!ignore_acpi_ev &&
3725 (send_acpi_ev || hotkey_report_mode < 2)) {
3726 acpi_bus_generate_proc_event(ibm->acpi->device,
3727 event, hkey);
3730 /* netlink events */
3731 if (!ignore_acpi_ev && send_acpi_ev) {
3732 acpi_bus_generate_netlink_event(
3733 ibm->acpi->device->pnp.device_class,
3734 dev_name(&ibm->acpi->device->dev),
3735 event, hkey);
3740 static void hotkey_suspend(pm_message_t state)
3742 /* Do these on suspend, we get the events on early resume! */
3743 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3744 hotkey_autosleep_ack = 0;
3747 static void hotkey_resume(void)
3749 tpacpi_disable_brightness_delay();
3751 if (hotkey_status_set(true) < 0 ||
3752 hotkey_mask_set(hotkey_acpi_mask) < 0)
3753 printk(TPACPI_ERR
3754 "error while attempting to reset the event "
3755 "firmware interface\n");
3757 tpacpi_send_radiosw_update();
3758 hotkey_tablet_mode_notify_change();
3759 hotkey_wakeup_reason_notify_change();
3760 hotkey_wakeup_hotunplug_complete_notify_change();
3761 hotkey_poll_setup_safe(false);
3764 /* procfs -------------------------------------------------------------- */
3765 static int hotkey_read(struct seq_file *m)
3767 int res, status;
3769 if (!tp_features.hotkey) {
3770 seq_printf(m, "status:\t\tnot supported\n");
3771 return 0;
3774 if (mutex_lock_killable(&hotkey_mutex))
3775 return -ERESTARTSYS;
3776 res = hotkey_status_get(&status);
3777 if (!res)
3778 res = hotkey_mask_get();
3779 mutex_unlock(&hotkey_mutex);
3780 if (res)
3781 return res;
3783 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3784 if (hotkey_all_mask) {
3785 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3786 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3787 } else {
3788 seq_printf(m, "mask:\t\tnot supported\n");
3789 seq_printf(m, "commands:\tenable, disable, reset\n");
3792 return 0;
3795 static void hotkey_enabledisable_warn(bool enable)
3797 tpacpi_log_usertask("procfs hotkey enable/disable");
3798 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3799 TPACPI_WARN
3800 "hotkey enable/disable functionality has been "
3801 "removed from the driver. Hotkeys are always "
3802 "enabled\n"))
3803 printk(TPACPI_ERR
3804 "Please remove the hotkey=enable module "
3805 "parameter, it is deprecated. Hotkeys are always "
3806 "enabled\n");
3809 static int hotkey_write(char *buf)
3811 int res;
3812 u32 mask;
3813 char *cmd;
3815 if (!tp_features.hotkey)
3816 return -ENODEV;
3818 if (mutex_lock_killable(&hotkey_mutex))
3819 return -ERESTARTSYS;
3821 mask = hotkey_user_mask;
3823 res = 0;
3824 while ((cmd = next_cmd(&buf))) {
3825 if (strlencmp(cmd, "enable") == 0) {
3826 hotkey_enabledisable_warn(1);
3827 } else if (strlencmp(cmd, "disable") == 0) {
3828 hotkey_enabledisable_warn(0);
3829 res = -EPERM;
3830 } else if (strlencmp(cmd, "reset") == 0) {
3831 mask = (hotkey_all_mask | hotkey_source_mask)
3832 & ~hotkey_reserved_mask;
3833 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3834 /* mask set */
3835 } else if (sscanf(cmd, "%x", &mask) == 1) {
3836 /* mask set */
3837 } else {
3838 res = -EINVAL;
3839 goto errexit;
3843 if (!res) {
3844 tpacpi_disclose_usertask("procfs hotkey",
3845 "set mask to 0x%08x\n", mask);
3846 res = hotkey_user_mask_set(mask);
3849 errexit:
3850 mutex_unlock(&hotkey_mutex);
3851 return res;
3854 static const struct acpi_device_id ibm_htk_device_ids[] = {
3855 {TPACPI_ACPI_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 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6411 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6412 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6413 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6415 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6416 static char *alsa_id = "ThinkPadEC";
6417 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6419 struct tpacpi_alsa_data {
6420 struct snd_card *card;
6421 struct snd_ctl_elem_id *ctl_mute_id;
6422 struct snd_ctl_elem_id *ctl_vol_id;
6425 static struct snd_card *alsa_card;
6427 enum {
6428 TP_EC_AUDIO = 0x30,
6430 /* TP_EC_AUDIO bits */
6431 TP_EC_AUDIO_MUTESW = 6,
6433 /* TP_EC_AUDIO bitmasks */
6434 TP_EC_AUDIO_LVL_MSK = 0x0F,
6435 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6437 /* Maximum volume */
6438 TP_EC_VOLUME_MAX = 14,
6441 enum tpacpi_volume_access_mode {
6442 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6443 TPACPI_VOL_MODE_EC, /* Pure EC control */
6444 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6445 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6446 TPACPI_VOL_MODE_MAX
6449 enum tpacpi_volume_capabilities {
6450 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6451 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6452 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6453 TPACPI_VOL_CAP_MAX
6456 static enum tpacpi_volume_access_mode volume_mode =
6457 TPACPI_VOL_MODE_MAX;
6459 static enum tpacpi_volume_capabilities volume_capabilities;
6460 static int volume_control_allowed;
6463 * Used to syncronize writers to TP_EC_AUDIO and
6464 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6466 static struct mutex volume_mutex;
6468 static void tpacpi_volume_checkpoint_nvram(void)
6470 u8 lec = 0;
6471 u8 b_nvram;
6472 u8 ec_mask;
6474 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6475 return;
6476 if (!volume_control_allowed)
6477 return;
6479 vdbg_printk(TPACPI_DBG_MIXER,
6480 "trying to checkpoint mixer state to NVRAM...\n");
6482 if (tp_features.mixer_no_level_control)
6483 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6484 else
6485 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6487 if (mutex_lock_killable(&volume_mutex) < 0)
6488 return;
6490 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6491 goto unlock;
6492 lec &= ec_mask;
6493 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6495 if (lec != (b_nvram & ec_mask)) {
6496 /* NVRAM needs update */
6497 b_nvram &= ~ec_mask;
6498 b_nvram |= lec;
6499 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6500 dbg_printk(TPACPI_DBG_MIXER,
6501 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6502 (unsigned int) lec, (unsigned int) b_nvram);
6503 } else {
6504 vdbg_printk(TPACPI_DBG_MIXER,
6505 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6506 (unsigned int) lec, (unsigned int) b_nvram);
6509 unlock:
6510 mutex_unlock(&volume_mutex);
6513 static int volume_get_status_ec(u8 *status)
6515 u8 s;
6517 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6518 return -EIO;
6520 *status = s;
6522 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6524 return 0;
6527 static int volume_get_status(u8 *status)
6529 return volume_get_status_ec(status);
6532 static int volume_set_status_ec(const u8 status)
6534 if (!acpi_ec_write(TP_EC_AUDIO, status))
6535 return -EIO;
6537 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6539 return 0;
6542 static int volume_set_status(const u8 status)
6544 return volume_set_status_ec(status);
6547 /* returns < 0 on error, 0 on no change, 1 on change */
6548 static int __volume_set_mute_ec(const bool mute)
6550 int rc;
6551 u8 s, n;
6553 if (mutex_lock_killable(&volume_mutex) < 0)
6554 return -EINTR;
6556 rc = volume_get_status_ec(&s);
6557 if (rc)
6558 goto unlock;
6560 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6561 s & ~TP_EC_AUDIO_MUTESW_MSK;
6563 if (n != s) {
6564 rc = volume_set_status_ec(n);
6565 if (!rc)
6566 rc = 1;
6569 unlock:
6570 mutex_unlock(&volume_mutex);
6571 return rc;
6574 static int volume_alsa_set_mute(const bool mute)
6576 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6577 (mute) ? "" : "un");
6578 return __volume_set_mute_ec(mute);
6581 static int volume_set_mute(const bool mute)
6583 int rc;
6585 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6586 (mute) ? "" : "un");
6588 rc = __volume_set_mute_ec(mute);
6589 return (rc < 0) ? rc : 0;
6592 /* returns < 0 on error, 0 on no change, 1 on change */
6593 static int __volume_set_volume_ec(const u8 vol)
6595 int rc;
6596 u8 s, n;
6598 if (vol > TP_EC_VOLUME_MAX)
6599 return -EINVAL;
6601 if (mutex_lock_killable(&volume_mutex) < 0)
6602 return -EINTR;
6604 rc = volume_get_status_ec(&s);
6605 if (rc)
6606 goto unlock;
6608 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6610 if (n != s) {
6611 rc = volume_set_status_ec(n);
6612 if (!rc)
6613 rc = 1;
6616 unlock:
6617 mutex_unlock(&volume_mutex);
6618 return rc;
6621 static int volume_alsa_set_volume(const u8 vol)
6623 dbg_printk(TPACPI_DBG_MIXER,
6624 "ALSA: trying to set volume level to %hu\n", vol);
6625 return __volume_set_volume_ec(vol);
6628 static void volume_alsa_notify_change(void)
6630 struct tpacpi_alsa_data *d;
6632 if (alsa_card && alsa_card->private_data) {
6633 d = alsa_card->private_data;
6634 if (d->ctl_mute_id)
6635 snd_ctl_notify(alsa_card,
6636 SNDRV_CTL_EVENT_MASK_VALUE,
6637 d->ctl_mute_id);
6638 if (d->ctl_vol_id)
6639 snd_ctl_notify(alsa_card,
6640 SNDRV_CTL_EVENT_MASK_VALUE,
6641 d->ctl_vol_id);
6645 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6646 struct snd_ctl_elem_info *uinfo)
6648 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6649 uinfo->count = 1;
6650 uinfo->value.integer.min = 0;
6651 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6652 return 0;
6655 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6656 struct snd_ctl_elem_value *ucontrol)
6658 u8 s;
6659 int rc;
6661 rc = volume_get_status(&s);
6662 if (rc < 0)
6663 return rc;
6665 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6666 return 0;
6669 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6670 struct snd_ctl_elem_value *ucontrol)
6672 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6675 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6677 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6678 struct snd_ctl_elem_value *ucontrol)
6680 u8 s;
6681 int rc;
6683 rc = volume_get_status(&s);
6684 if (rc < 0)
6685 return rc;
6687 ucontrol->value.integer.value[0] =
6688 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6689 return 0;
6692 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6693 struct snd_ctl_elem_value *ucontrol)
6695 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6698 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6699 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6700 .name = "Console Playback Volume",
6701 .index = 0,
6702 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6703 .info = volume_alsa_vol_info,
6704 .get = volume_alsa_vol_get,
6707 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6708 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6709 .name = "Console Playback Switch",
6710 .index = 0,
6711 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6712 .info = volume_alsa_mute_info,
6713 .get = volume_alsa_mute_get,
6716 static void volume_suspend(pm_message_t state)
6718 tpacpi_volume_checkpoint_nvram();
6721 static void volume_resume(void)
6723 volume_alsa_notify_change();
6726 static void volume_shutdown(void)
6728 tpacpi_volume_checkpoint_nvram();
6731 static void volume_exit(void)
6733 if (alsa_card) {
6734 snd_card_free(alsa_card);
6735 alsa_card = NULL;
6738 tpacpi_volume_checkpoint_nvram();
6741 static int __init volume_create_alsa_mixer(void)
6743 struct snd_card *card;
6744 struct tpacpi_alsa_data *data;
6745 struct snd_kcontrol *ctl_vol;
6746 struct snd_kcontrol *ctl_mute;
6747 int rc;
6749 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6750 sizeof(struct tpacpi_alsa_data), &card);
6751 if (rc < 0 || !card) {
6752 printk(TPACPI_ERR
6753 "Failed to create ALSA card structures: %d\n", rc);
6754 return 1;
6757 BUG_ON(!card->private_data);
6758 data = card->private_data;
6759 data->card = card;
6761 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6762 sizeof(card->driver));
6763 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6764 sizeof(card->shortname));
6765 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6766 (thinkpad_id.ec_version_str) ?
6767 thinkpad_id.ec_version_str : "(unknown)");
6768 snprintf(card->longname, sizeof(card->longname),
6769 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6770 (thinkpad_id.ec_version_str) ?
6771 thinkpad_id.ec_version_str : "unknown");
6773 if (volume_control_allowed) {
6774 volume_alsa_control_vol.put = volume_alsa_vol_put;
6775 volume_alsa_control_vol.access =
6776 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6778 volume_alsa_control_mute.put = volume_alsa_mute_put;
6779 volume_alsa_control_mute.access =
6780 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6783 if (!tp_features.mixer_no_level_control) {
6784 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6785 rc = snd_ctl_add(card, ctl_vol);
6786 if (rc < 0) {
6787 printk(TPACPI_ERR
6788 "Failed to create ALSA volume control: %d\n",
6789 rc);
6790 goto err_exit;
6792 data->ctl_vol_id = &ctl_vol->id;
6795 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6796 rc = snd_ctl_add(card, ctl_mute);
6797 if (rc < 0) {
6798 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6799 rc);
6800 goto err_exit;
6802 data->ctl_mute_id = &ctl_mute->id;
6804 snd_card_set_dev(card, &tpacpi_pdev->dev);
6805 rc = snd_card_register(card);
6806 if (rc < 0) {
6807 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6808 goto err_exit;
6811 alsa_card = card;
6812 return 0;
6814 err_exit:
6815 snd_card_free(card);
6816 return 1;
6819 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6820 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6822 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6823 /* Whitelist volume level on all IBM by default */
6824 { .vendor = PCI_VENDOR_ID_IBM,
6825 .bios = TPACPI_MATCH_ANY,
6826 .ec = TPACPI_MATCH_ANY,
6827 .quirks = TPACPI_VOL_Q_LEVEL },
6829 /* Lenovo models with volume control (needs confirmation) */
6830 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6831 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6832 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6833 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6834 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6835 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6836 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6838 /* Whitelist mute-only on all Lenovo by default */
6839 { .vendor = PCI_VENDOR_ID_LENOVO,
6840 .bios = TPACPI_MATCH_ANY,
6841 .ec = TPACPI_MATCH_ANY,
6842 .quirks = TPACPI_VOL_Q_MUTEONLY }
6845 static int __init volume_init(struct ibm_init_struct *iibm)
6847 unsigned long quirks;
6848 int rc;
6850 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6852 mutex_init(&volume_mutex);
6855 * Check for module parameter bogosity, note that we
6856 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6857 * able to detect "unspecified"
6859 if (volume_mode > TPACPI_VOL_MODE_MAX)
6860 return -EINVAL;
6862 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6863 printk(TPACPI_ERR
6864 "UCMS step volume mode not implemented, "
6865 "please contact %s\n", TPACPI_MAIL);
6866 return 1;
6869 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6870 return -EINVAL;
6873 * The ALSA mixer is our primary interface.
6874 * When disabled, don't install the subdriver at all
6876 if (!alsa_enable) {
6877 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6878 "ALSA mixer disabled by parameter, "
6879 "not loading volume subdriver...\n");
6880 return 1;
6883 quirks = tpacpi_check_quirks(volume_quirk_table,
6884 ARRAY_SIZE(volume_quirk_table));
6886 switch (volume_capabilities) {
6887 case TPACPI_VOL_CAP_AUTO:
6888 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6889 tp_features.mixer_no_level_control = 1;
6890 else if (quirks & TPACPI_VOL_Q_LEVEL)
6891 tp_features.mixer_no_level_control = 0;
6892 else
6893 return 1; /* no mixer */
6894 break;
6895 case TPACPI_VOL_CAP_VOLMUTE:
6896 tp_features.mixer_no_level_control = 0;
6897 break;
6898 case TPACPI_VOL_CAP_MUTEONLY:
6899 tp_features.mixer_no_level_control = 1;
6900 break;
6901 default:
6902 return 1;
6905 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6906 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6907 "using user-supplied volume_capabilities=%d\n",
6908 volume_capabilities);
6910 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6911 volume_mode == TPACPI_VOL_MODE_MAX) {
6912 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6914 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6915 "driver auto-selected volume_mode=%d\n",
6916 volume_mode);
6917 } else {
6918 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6919 "using user-supplied volume_mode=%d\n",
6920 volume_mode);
6923 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6924 "mute is supported, volume control is %s\n",
6925 str_supported(!tp_features.mixer_no_level_control));
6927 rc = volume_create_alsa_mixer();
6928 if (rc) {
6929 printk(TPACPI_ERR
6930 "Could not create the ALSA mixer interface\n");
6931 return rc;
6934 printk(TPACPI_INFO
6935 "Console audio control enabled, mode: %s\n",
6936 (volume_control_allowed) ?
6937 "override (read/write)" :
6938 "monitor (read only)");
6940 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6941 "registering volume hotkeys as change notification\n");
6942 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6943 | TP_ACPI_HKEY_VOLUP_MASK
6944 | TP_ACPI_HKEY_VOLDWN_MASK
6945 | TP_ACPI_HKEY_MUTE_MASK);
6947 return 0;
6950 static int volume_read(struct seq_file *m)
6952 u8 status;
6954 if (volume_get_status(&status) < 0) {
6955 seq_printf(m, "level:\t\tunreadable\n");
6956 } else {
6957 if (tp_features.mixer_no_level_control)
6958 seq_printf(m, "level:\t\tunsupported\n");
6959 else
6960 seq_printf(m, "level:\t\t%d\n",
6961 status & TP_EC_AUDIO_LVL_MSK);
6963 seq_printf(m, "mute:\t\t%s\n",
6964 onoff(status, TP_EC_AUDIO_MUTESW));
6966 if (volume_control_allowed) {
6967 seq_printf(m, "commands:\tunmute, mute\n");
6968 if (!tp_features.mixer_no_level_control) {
6969 seq_printf(m,
6970 "commands:\tup, down\n");
6971 seq_printf(m,
6972 "commands:\tlevel <level>"
6973 " (<level> is 0-%d)\n",
6974 TP_EC_VOLUME_MAX);
6979 return 0;
6982 static int volume_write(char *buf)
6984 u8 s;
6985 u8 new_level, new_mute;
6986 int l;
6987 char *cmd;
6988 int rc;
6991 * We do allow volume control at driver startup, so that the
6992 * user can set initial state through the volume=... parameter hack.
6994 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
6995 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
6996 tp_warned.volume_ctrl_forbidden = 1;
6997 printk(TPACPI_NOTICE
6998 "Console audio control in monitor mode, "
6999 "changes are not allowed.\n");
7000 printk(TPACPI_NOTICE
7001 "Use the volume_control=1 module parameter "
7002 "to enable volume control\n");
7004 return -EPERM;
7007 rc = volume_get_status(&s);
7008 if (rc < 0)
7009 return rc;
7011 new_level = s & TP_EC_AUDIO_LVL_MSK;
7012 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7014 while ((cmd = next_cmd(&buf))) {
7015 if (!tp_features.mixer_no_level_control) {
7016 if (strlencmp(cmd, "up") == 0) {
7017 if (new_mute)
7018 new_mute = 0;
7019 else if (new_level < TP_EC_VOLUME_MAX)
7020 new_level++;
7021 continue;
7022 } else if (strlencmp(cmd, "down") == 0) {
7023 if (new_mute)
7024 new_mute = 0;
7025 else if (new_level > 0)
7026 new_level--;
7027 continue;
7028 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7029 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7030 new_level = l;
7031 continue;
7034 if (strlencmp(cmd, "mute") == 0)
7035 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7036 else if (strlencmp(cmd, "unmute") == 0)
7037 new_mute = 0;
7038 else
7039 return -EINVAL;
7042 if (tp_features.mixer_no_level_control) {
7043 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7044 new_mute ? "" : "un");
7045 rc = volume_set_mute(!!new_mute);
7046 } else {
7047 tpacpi_disclose_usertask("procfs volume",
7048 "%smute and set level to %d\n",
7049 new_mute ? "" : "un", new_level);
7050 rc = volume_set_status(new_mute | new_level);
7052 volume_alsa_notify_change();
7054 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7057 static struct ibm_struct volume_driver_data = {
7058 .name = "volume",
7059 .read = volume_read,
7060 .write = volume_write,
7061 .exit = volume_exit,
7062 .suspend = volume_suspend,
7063 .resume = volume_resume,
7064 .shutdown = volume_shutdown,
7067 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7069 #define alsa_card NULL
7071 static void inline volume_alsa_notify_change(void)
7075 static int __init volume_init(struct ibm_init_struct *iibm)
7077 printk(TPACPI_INFO
7078 "volume: disabled as there is no ALSA support in this kernel\n");
7080 return 1;
7083 static struct ibm_struct volume_driver_data = {
7084 .name = "volume",
7087 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7089 /*************************************************************************
7090 * Fan subdriver
7094 * FAN ACCESS MODES
7096 * TPACPI_FAN_RD_ACPI_GFAN:
7097 * ACPI GFAN method: returns fan level
7099 * see TPACPI_FAN_WR_ACPI_SFAN
7100 * EC 0x2f (HFSP) not available if GFAN exists
7102 * TPACPI_FAN_WR_ACPI_SFAN:
7103 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7105 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7106 * it for writing.
7108 * TPACPI_FAN_WR_TPEC:
7109 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7110 * Supported on almost all ThinkPads
7112 * Fan speed changes of any sort (including those caused by the
7113 * disengaged mode) are usually done slowly by the firmware as the
7114 * maximum ammount of fan duty cycle change per second seems to be
7115 * limited.
7117 * Reading is not available if GFAN exists.
7118 * Writing is not available if SFAN exists.
7120 * Bits
7121 * 7 automatic mode engaged;
7122 * (default operation mode of the ThinkPad)
7123 * fan level is ignored in this mode.
7124 * 6 full speed mode (takes precedence over bit 7);
7125 * not available on all thinkpads. May disable
7126 * the tachometer while the fan controller ramps up
7127 * the speed (which can take up to a few *minutes*).
7128 * Speeds up fan to 100% duty-cycle, which is far above
7129 * the standard RPM levels. It is not impossible that
7130 * it could cause hardware damage.
7131 * 5-3 unused in some models. Extra bits for fan level
7132 * in others, but still useless as all values above
7133 * 7 map to the same speed as level 7 in these models.
7134 * 2-0 fan level (0..7 usually)
7135 * 0x00 = stop
7136 * 0x07 = max (set when temperatures critical)
7137 * Some ThinkPads may have other levels, see
7138 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7140 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7141 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7142 * does so, its initial value is meaningless (0x07).
7144 * For firmware bugs, refer to:
7145 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7147 * ----
7149 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7150 * Main fan tachometer reading (in RPM)
7152 * This register is present on all ThinkPads with a new-style EC, and
7153 * it is known not to be present on the A21m/e, and T22, as there is
7154 * something else in offset 0x84 according to the ACPI DSDT. Other
7155 * ThinkPads from this same time period (and earlier) probably lack the
7156 * tachometer as well.
7158 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7159 * was never fixed by IBM to report the EC firmware version string
7160 * probably support the tachometer (like the early X models), so
7161 * detecting it is quite hard. We need more data to know for sure.
7163 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7164 * might result.
7166 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7167 * mode.
7169 * For firmware bugs, refer to:
7170 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7172 * ----
7174 * ThinkPad EC register 0x31 bit 0 (only on select models)
7176 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7177 * show the speed of the main fan. When bit 0 of EC register 0x31
7178 * is one, the tachometer registers show the speed of the auxiliary
7179 * fan.
7181 * Fan control seems to affect both fans, regardless of the state
7182 * of this bit.
7184 * So far, only the firmware for the X60/X61 non-tablet versions
7185 * seem to support this (firmware TP-7M).
7187 * TPACPI_FAN_WR_ACPI_FANS:
7188 * ThinkPad X31, X40, X41. Not available in the X60.
7190 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7191 * high speed. ACPI DSDT seems to map these three speeds to levels
7192 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7193 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7195 * The speeds are stored on handles
7196 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7198 * There are three default speed sets, acessible as handles:
7199 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7201 * ACPI DSDT switches which set is in use depending on various
7202 * factors.
7204 * TPACPI_FAN_WR_TPEC is also available and should be used to
7205 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7206 * but the ACPI tables just mention level 7.
7209 enum { /* Fan control constants */
7210 fan_status_offset = 0x2f, /* EC register 0x2f */
7211 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7212 * 0x84 must be read before 0x85 */
7213 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7214 bit 0 selects which fan is active */
7216 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7217 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7219 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7222 enum fan_status_access_mode {
7223 TPACPI_FAN_NONE = 0, /* No fan status or control */
7224 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7225 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7228 enum fan_control_access_mode {
7229 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7230 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7231 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7232 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7235 enum fan_control_commands {
7236 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7237 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7238 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7239 * and also watchdog cmd */
7242 static int fan_control_allowed;
7244 static enum fan_status_access_mode fan_status_access_mode;
7245 static enum fan_control_access_mode fan_control_access_mode;
7246 static enum fan_control_commands fan_control_commands;
7248 static u8 fan_control_initial_status;
7249 static u8 fan_control_desired_level;
7250 static u8 fan_control_resume_level;
7251 static int fan_watchdog_maxinterval;
7253 static struct mutex fan_mutex;
7255 static void fan_watchdog_fire(struct work_struct *ignored);
7256 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7258 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7259 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7260 "\\FSPD", /* 600e/x, 770e, 770x */
7261 ); /* all others */
7262 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7263 "JFNS", /* 770x-JL */
7264 ); /* all others */
7267 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7268 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7269 * be in auto mode (0x80).
7271 * This is corrected by any write to HFSP either by the driver, or
7272 * by the firmware.
7274 * We assume 0x07 really means auto mode while this quirk is active,
7275 * as this is far more likely than the ThinkPad being in level 7,
7276 * which is only used by the firmware during thermal emergencies.
7278 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7279 * TP-70 (T43, R52), which are known to be buggy.
7282 static void fan_quirk1_setup(void)
7284 if (fan_control_initial_status == 0x07) {
7285 printk(TPACPI_NOTICE
7286 "fan_init: initial fan status is unknown, "
7287 "assuming it is in auto mode\n");
7288 tp_features.fan_ctrl_status_undef = 1;
7292 static void fan_quirk1_handle(u8 *fan_status)
7294 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7295 if (*fan_status != fan_control_initial_status) {
7296 /* something changed the HFSP regisnter since
7297 * driver init time, so it is not undefined
7298 * anymore */
7299 tp_features.fan_ctrl_status_undef = 0;
7300 } else {
7301 /* Return most likely status. In fact, it
7302 * might be the only possible status */
7303 *fan_status = TP_EC_FAN_AUTO;
7308 /* Select main fan on X60/X61, NOOP on others */
7309 static bool fan_select_fan1(void)
7311 if (tp_features.second_fan) {
7312 u8 val;
7314 if (ec_read(fan_select_offset, &val) < 0)
7315 return false;
7316 val &= 0xFEU;
7317 if (ec_write(fan_select_offset, val) < 0)
7318 return false;
7320 return true;
7323 /* Select secondary fan on X60/X61 */
7324 static bool fan_select_fan2(void)
7326 u8 val;
7328 if (!tp_features.second_fan)
7329 return false;
7331 if (ec_read(fan_select_offset, &val) < 0)
7332 return false;
7333 val |= 0x01U;
7334 if (ec_write(fan_select_offset, val) < 0)
7335 return false;
7337 return true;
7341 * Call with fan_mutex held
7343 static void fan_update_desired_level(u8 status)
7345 if ((status &
7346 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7347 if (status > 7)
7348 fan_control_desired_level = 7;
7349 else
7350 fan_control_desired_level = status;
7354 static int fan_get_status(u8 *status)
7356 u8 s;
7358 /* TODO:
7359 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7361 switch (fan_status_access_mode) {
7362 case TPACPI_FAN_RD_ACPI_GFAN:
7363 /* 570, 600e/x, 770e, 770x */
7365 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7366 return -EIO;
7368 if (likely(status))
7369 *status = s & 0x07;
7371 break;
7373 case TPACPI_FAN_RD_TPEC:
7374 /* all except 570, 600e/x, 770e, 770x */
7375 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7376 return -EIO;
7378 if (likely(status)) {
7379 *status = s;
7380 fan_quirk1_handle(status);
7383 break;
7385 default:
7386 return -ENXIO;
7389 return 0;
7392 static int fan_get_status_safe(u8 *status)
7394 int rc;
7395 u8 s;
7397 if (mutex_lock_killable(&fan_mutex))
7398 return -ERESTARTSYS;
7399 rc = fan_get_status(&s);
7400 if (!rc)
7401 fan_update_desired_level(s);
7402 mutex_unlock(&fan_mutex);
7404 if (status)
7405 *status = s;
7407 return rc;
7410 static int fan_get_speed(unsigned int *speed)
7412 u8 hi, lo;
7414 switch (fan_status_access_mode) {
7415 case TPACPI_FAN_RD_TPEC:
7416 /* all except 570, 600e/x, 770e, 770x */
7417 if (unlikely(!fan_select_fan1()))
7418 return -EIO;
7419 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7420 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7421 return -EIO;
7423 if (likely(speed))
7424 *speed = (hi << 8) | lo;
7426 break;
7428 default:
7429 return -ENXIO;
7432 return 0;
7435 static int fan2_get_speed(unsigned int *speed)
7437 u8 hi, lo;
7438 bool rc;
7440 switch (fan_status_access_mode) {
7441 case TPACPI_FAN_RD_TPEC:
7442 /* all except 570, 600e/x, 770e, 770x */
7443 if (unlikely(!fan_select_fan2()))
7444 return -EIO;
7445 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7446 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7447 fan_select_fan1(); /* play it safe */
7448 if (rc)
7449 return -EIO;
7451 if (likely(speed))
7452 *speed = (hi << 8) | lo;
7454 break;
7456 default:
7457 return -ENXIO;
7460 return 0;
7463 static int fan_set_level(int level)
7465 if (!fan_control_allowed)
7466 return -EPERM;
7468 switch (fan_control_access_mode) {
7469 case TPACPI_FAN_WR_ACPI_SFAN:
7470 if (level >= 0 && level <= 7) {
7471 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7472 return -EIO;
7473 } else
7474 return -EINVAL;
7475 break;
7477 case TPACPI_FAN_WR_ACPI_FANS:
7478 case TPACPI_FAN_WR_TPEC:
7479 if (!(level & TP_EC_FAN_AUTO) &&
7480 !(level & TP_EC_FAN_FULLSPEED) &&
7481 ((level < 0) || (level > 7)))
7482 return -EINVAL;
7484 /* safety net should the EC not support AUTO
7485 * or FULLSPEED mode bits and just ignore them */
7486 if (level & TP_EC_FAN_FULLSPEED)
7487 level |= 7; /* safety min speed 7 */
7488 else if (level & TP_EC_FAN_AUTO)
7489 level |= 4; /* safety min speed 4 */
7491 if (!acpi_ec_write(fan_status_offset, level))
7492 return -EIO;
7493 else
7494 tp_features.fan_ctrl_status_undef = 0;
7495 break;
7497 default:
7498 return -ENXIO;
7501 vdbg_printk(TPACPI_DBG_FAN,
7502 "fan control: set fan control register to 0x%02x\n", level);
7503 return 0;
7506 static int fan_set_level_safe(int level)
7508 int rc;
7510 if (!fan_control_allowed)
7511 return -EPERM;
7513 if (mutex_lock_killable(&fan_mutex))
7514 return -ERESTARTSYS;
7516 if (level == TPACPI_FAN_LAST_LEVEL)
7517 level = fan_control_desired_level;
7519 rc = fan_set_level(level);
7520 if (!rc)
7521 fan_update_desired_level(level);
7523 mutex_unlock(&fan_mutex);
7524 return rc;
7527 static int fan_set_enable(void)
7529 u8 s;
7530 int rc;
7532 if (!fan_control_allowed)
7533 return -EPERM;
7535 if (mutex_lock_killable(&fan_mutex))
7536 return -ERESTARTSYS;
7538 switch (fan_control_access_mode) {
7539 case TPACPI_FAN_WR_ACPI_FANS:
7540 case TPACPI_FAN_WR_TPEC:
7541 rc = fan_get_status(&s);
7542 if (rc < 0)
7543 break;
7545 /* Don't go out of emergency fan mode */
7546 if (s != 7) {
7547 s &= 0x07;
7548 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7551 if (!acpi_ec_write(fan_status_offset, s))
7552 rc = -EIO;
7553 else {
7554 tp_features.fan_ctrl_status_undef = 0;
7555 rc = 0;
7557 break;
7559 case TPACPI_FAN_WR_ACPI_SFAN:
7560 rc = fan_get_status(&s);
7561 if (rc < 0)
7562 break;
7564 s &= 0x07;
7566 /* Set fan to at least level 4 */
7567 s |= 4;
7569 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7570 rc = -EIO;
7571 else
7572 rc = 0;
7573 break;
7575 default:
7576 rc = -ENXIO;
7579 mutex_unlock(&fan_mutex);
7581 if (!rc)
7582 vdbg_printk(TPACPI_DBG_FAN,
7583 "fan control: set fan control register to 0x%02x\n",
7585 return rc;
7588 static int fan_set_disable(void)
7590 int rc;
7592 if (!fan_control_allowed)
7593 return -EPERM;
7595 if (mutex_lock_killable(&fan_mutex))
7596 return -ERESTARTSYS;
7598 rc = 0;
7599 switch (fan_control_access_mode) {
7600 case TPACPI_FAN_WR_ACPI_FANS:
7601 case TPACPI_FAN_WR_TPEC:
7602 if (!acpi_ec_write(fan_status_offset, 0x00))
7603 rc = -EIO;
7604 else {
7605 fan_control_desired_level = 0;
7606 tp_features.fan_ctrl_status_undef = 0;
7608 break;
7610 case TPACPI_FAN_WR_ACPI_SFAN:
7611 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7612 rc = -EIO;
7613 else
7614 fan_control_desired_level = 0;
7615 break;
7617 default:
7618 rc = -ENXIO;
7621 if (!rc)
7622 vdbg_printk(TPACPI_DBG_FAN,
7623 "fan control: set fan control register to 0\n");
7625 mutex_unlock(&fan_mutex);
7626 return rc;
7629 static int fan_set_speed(int speed)
7631 int rc;
7633 if (!fan_control_allowed)
7634 return -EPERM;
7636 if (mutex_lock_killable(&fan_mutex))
7637 return -ERESTARTSYS;
7639 rc = 0;
7640 switch (fan_control_access_mode) {
7641 case TPACPI_FAN_WR_ACPI_FANS:
7642 if (speed >= 0 && speed <= 65535) {
7643 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7644 speed, speed, speed))
7645 rc = -EIO;
7646 } else
7647 rc = -EINVAL;
7648 break;
7650 default:
7651 rc = -ENXIO;
7654 mutex_unlock(&fan_mutex);
7655 return rc;
7658 static void fan_watchdog_reset(void)
7660 static int fan_watchdog_active;
7662 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7663 return;
7665 if (fan_watchdog_active)
7666 cancel_delayed_work(&fan_watchdog_task);
7668 if (fan_watchdog_maxinterval > 0 &&
7669 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7670 fan_watchdog_active = 1;
7671 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7672 msecs_to_jiffies(fan_watchdog_maxinterval
7673 * 1000))) {
7674 printk(TPACPI_ERR
7675 "failed to queue the fan watchdog, "
7676 "watchdog will not trigger\n");
7678 } else
7679 fan_watchdog_active = 0;
7682 static void fan_watchdog_fire(struct work_struct *ignored)
7684 int rc;
7686 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7687 return;
7689 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7690 rc = fan_set_enable();
7691 if (rc < 0) {
7692 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7693 "will try again later...\n", -rc);
7694 /* reschedule for later */
7695 fan_watchdog_reset();
7700 * SYSFS fan layout: hwmon compatible (device)
7702 * pwm*_enable:
7703 * 0: "disengaged" mode
7704 * 1: manual mode
7705 * 2: native EC "auto" mode (recommended, hardware default)
7707 * pwm*: set speed in manual mode, ignored otherwise.
7708 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7709 * interpolation.
7711 * fan*_input: tachometer reading, RPM
7714 * SYSFS fan layout: extensions
7716 * fan_watchdog (driver):
7717 * fan watchdog interval in seconds, 0 disables (default), max 120
7720 /* sysfs fan pwm1_enable ----------------------------------------------- */
7721 static ssize_t fan_pwm1_enable_show(struct device *dev,
7722 struct device_attribute *attr,
7723 char *buf)
7725 int res, mode;
7726 u8 status;
7728 res = fan_get_status_safe(&status);
7729 if (res)
7730 return res;
7732 if (status & TP_EC_FAN_FULLSPEED) {
7733 mode = 0;
7734 } else if (status & TP_EC_FAN_AUTO) {
7735 mode = 2;
7736 } else
7737 mode = 1;
7739 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7742 static ssize_t fan_pwm1_enable_store(struct device *dev,
7743 struct device_attribute *attr,
7744 const char *buf, size_t count)
7746 unsigned long t;
7747 int res, level;
7749 if (parse_strtoul(buf, 2, &t))
7750 return -EINVAL;
7752 tpacpi_disclose_usertask("hwmon pwm1_enable",
7753 "set fan mode to %lu\n", t);
7755 switch (t) {
7756 case 0:
7757 level = TP_EC_FAN_FULLSPEED;
7758 break;
7759 case 1:
7760 level = TPACPI_FAN_LAST_LEVEL;
7761 break;
7762 case 2:
7763 level = TP_EC_FAN_AUTO;
7764 break;
7765 case 3:
7766 /* reserved for software-controlled auto mode */
7767 return -ENOSYS;
7768 default:
7769 return -EINVAL;
7772 res = fan_set_level_safe(level);
7773 if (res == -ENXIO)
7774 return -EINVAL;
7775 else if (res < 0)
7776 return res;
7778 fan_watchdog_reset();
7780 return count;
7783 static struct device_attribute dev_attr_fan_pwm1_enable =
7784 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7785 fan_pwm1_enable_show, fan_pwm1_enable_store);
7787 /* sysfs fan pwm1 ------------------------------------------------------ */
7788 static ssize_t fan_pwm1_show(struct device *dev,
7789 struct device_attribute *attr,
7790 char *buf)
7792 int res;
7793 u8 status;
7795 res = fan_get_status_safe(&status);
7796 if (res)
7797 return res;
7799 if ((status &
7800 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7801 status = fan_control_desired_level;
7803 if (status > 7)
7804 status = 7;
7806 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7809 static ssize_t fan_pwm1_store(struct device *dev,
7810 struct device_attribute *attr,
7811 const char *buf, size_t count)
7813 unsigned long s;
7814 int rc;
7815 u8 status, newlevel;
7817 if (parse_strtoul(buf, 255, &s))
7818 return -EINVAL;
7820 tpacpi_disclose_usertask("hwmon pwm1",
7821 "set fan speed to %lu\n", s);
7823 /* scale down from 0-255 to 0-7 */
7824 newlevel = (s >> 5) & 0x07;
7826 if (mutex_lock_killable(&fan_mutex))
7827 return -ERESTARTSYS;
7829 rc = fan_get_status(&status);
7830 if (!rc && (status &
7831 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7832 rc = fan_set_level(newlevel);
7833 if (rc == -ENXIO)
7834 rc = -EINVAL;
7835 else if (!rc) {
7836 fan_update_desired_level(newlevel);
7837 fan_watchdog_reset();
7841 mutex_unlock(&fan_mutex);
7842 return (rc)? rc : count;
7845 static struct device_attribute dev_attr_fan_pwm1 =
7846 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7847 fan_pwm1_show, fan_pwm1_store);
7849 /* sysfs fan fan1_input ------------------------------------------------ */
7850 static ssize_t fan_fan1_input_show(struct device *dev,
7851 struct device_attribute *attr,
7852 char *buf)
7854 int res;
7855 unsigned int speed;
7857 res = fan_get_speed(&speed);
7858 if (res < 0)
7859 return res;
7861 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7864 static struct device_attribute dev_attr_fan_fan1_input =
7865 __ATTR(fan1_input, S_IRUGO,
7866 fan_fan1_input_show, NULL);
7868 /* sysfs fan fan2_input ------------------------------------------------ */
7869 static ssize_t fan_fan2_input_show(struct device *dev,
7870 struct device_attribute *attr,
7871 char *buf)
7873 int res;
7874 unsigned int speed;
7876 res = fan2_get_speed(&speed);
7877 if (res < 0)
7878 return res;
7880 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7883 static struct device_attribute dev_attr_fan_fan2_input =
7884 __ATTR(fan2_input, S_IRUGO,
7885 fan_fan2_input_show, NULL);
7887 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7888 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7889 char *buf)
7891 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7894 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7895 const char *buf, size_t count)
7897 unsigned long t;
7899 if (parse_strtoul(buf, 120, &t))
7900 return -EINVAL;
7902 if (!fan_control_allowed)
7903 return -EPERM;
7905 fan_watchdog_maxinterval = t;
7906 fan_watchdog_reset();
7908 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7910 return count;
7913 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7914 fan_fan_watchdog_show, fan_fan_watchdog_store);
7916 /* --------------------------------------------------------------------- */
7917 static struct attribute *fan_attributes[] = {
7918 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7919 &dev_attr_fan_fan1_input.attr,
7920 NULL, /* for fan2_input */
7921 NULL
7924 static const struct attribute_group fan_attr_group = {
7925 .attrs = fan_attributes,
7928 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7929 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7931 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7932 { .vendor = PCI_VENDOR_ID_IBM, \
7933 .bios = TPACPI_MATCH_ANY, \
7934 .ec = TPID(__id1, __id2), \
7935 .quirks = __quirks }
7937 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7938 { .vendor = PCI_VENDOR_ID_LENOVO, \
7939 .bios = TPACPI_MATCH_ANY, \
7940 .ec = TPID(__id1, __id2), \
7941 .quirks = __quirks }
7943 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7944 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7945 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7946 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7947 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7948 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7951 #undef TPACPI_FAN_QL
7952 #undef TPACPI_FAN_QI
7954 static int __init fan_init(struct ibm_init_struct *iibm)
7956 int rc;
7957 unsigned long quirks;
7959 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7960 "initializing fan subdriver\n");
7962 mutex_init(&fan_mutex);
7963 fan_status_access_mode = TPACPI_FAN_NONE;
7964 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7965 fan_control_commands = 0;
7966 fan_watchdog_maxinterval = 0;
7967 tp_features.fan_ctrl_status_undef = 0;
7968 tp_features.second_fan = 0;
7969 fan_control_desired_level = 7;
7971 TPACPI_ACPIHANDLE_INIT(fans);
7972 TPACPI_ACPIHANDLE_INIT(gfan);
7973 TPACPI_ACPIHANDLE_INIT(sfan);
7975 quirks = tpacpi_check_quirks(fan_quirk_table,
7976 ARRAY_SIZE(fan_quirk_table));
7978 if (gfan_handle) {
7979 /* 570, 600e/x, 770e, 770x */
7980 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7981 } else {
7982 /* all other ThinkPads: note that even old-style
7983 * ThinkPad ECs supports the fan control register */
7984 if (likely(acpi_ec_read(fan_status_offset,
7985 &fan_control_initial_status))) {
7986 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7987 if (quirks & TPACPI_FAN_Q1)
7988 fan_quirk1_setup();
7989 if (quirks & TPACPI_FAN_2FAN) {
7990 tp_features.second_fan = 1;
7991 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7992 "secondary fan support enabled\n");
7994 } else {
7995 printk(TPACPI_ERR
7996 "ThinkPad ACPI EC access misbehaving, "
7997 "fan status and control unavailable\n");
7998 return 1;
8002 if (sfan_handle) {
8003 /* 570, 770x-JL */
8004 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8005 fan_control_commands |=
8006 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8007 } else {
8008 if (!gfan_handle) {
8009 /* gfan without sfan means no fan control */
8010 /* all other models implement TP EC 0x2f control */
8012 if (fans_handle) {
8013 /* X31, X40, X41 */
8014 fan_control_access_mode =
8015 TPACPI_FAN_WR_ACPI_FANS;
8016 fan_control_commands |=
8017 TPACPI_FAN_CMD_SPEED |
8018 TPACPI_FAN_CMD_LEVEL |
8019 TPACPI_FAN_CMD_ENABLE;
8020 } else {
8021 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8022 fan_control_commands |=
8023 TPACPI_FAN_CMD_LEVEL |
8024 TPACPI_FAN_CMD_ENABLE;
8029 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8030 "fan is %s, modes %d, %d\n",
8031 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8032 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8033 fan_status_access_mode, fan_control_access_mode);
8035 /* fan control master switch */
8036 if (!fan_control_allowed) {
8037 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8038 fan_control_commands = 0;
8039 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8040 "fan control features disabled by parameter\n");
8043 /* update fan_control_desired_level */
8044 if (fan_status_access_mode != TPACPI_FAN_NONE)
8045 fan_get_status_safe(NULL);
8047 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8048 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8049 if (tp_features.second_fan) {
8050 /* attach second fan tachometer */
8051 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8052 &dev_attr_fan_fan2_input.attr;
8054 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8055 &fan_attr_group);
8056 if (rc < 0)
8057 return rc;
8059 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8060 &driver_attr_fan_watchdog);
8061 if (rc < 0) {
8062 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8063 &fan_attr_group);
8064 return rc;
8066 return 0;
8067 } else
8068 return 1;
8071 static void fan_exit(void)
8073 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8074 "cancelling any pending fan watchdog tasks\n");
8076 /* FIXME: can we really do this unconditionally? */
8077 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8078 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8079 &driver_attr_fan_watchdog);
8081 cancel_delayed_work(&fan_watchdog_task);
8082 flush_workqueue(tpacpi_wq);
8085 static void fan_suspend(pm_message_t state)
8087 int rc;
8089 if (!fan_control_allowed)
8090 return;
8092 /* Store fan status in cache */
8093 fan_control_resume_level = 0;
8094 rc = fan_get_status_safe(&fan_control_resume_level);
8095 if (rc < 0)
8096 printk(TPACPI_NOTICE
8097 "failed to read fan level for later "
8098 "restore during resume: %d\n", rc);
8100 /* if it is undefined, don't attempt to restore it.
8101 * KEEP THIS LAST */
8102 if (tp_features.fan_ctrl_status_undef)
8103 fan_control_resume_level = 0;
8106 static void fan_resume(void)
8108 u8 current_level = 7;
8109 bool do_set = false;
8110 int rc;
8112 /* DSDT *always* updates status on resume */
8113 tp_features.fan_ctrl_status_undef = 0;
8115 if (!fan_control_allowed ||
8116 !fan_control_resume_level ||
8117 (fan_get_status_safe(&current_level) < 0))
8118 return;
8120 switch (fan_control_access_mode) {
8121 case TPACPI_FAN_WR_ACPI_SFAN:
8122 /* never decrease fan level */
8123 do_set = (fan_control_resume_level > current_level);
8124 break;
8125 case TPACPI_FAN_WR_ACPI_FANS:
8126 case TPACPI_FAN_WR_TPEC:
8127 /* never decrease fan level, scale is:
8128 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8130 * We expect the firmware to set either 7 or AUTO, but we
8131 * handle FULLSPEED out of paranoia.
8133 * So, we can safely only restore FULLSPEED or 7, anything
8134 * else could slow the fan. Restoring AUTO is useless, at
8135 * best that's exactly what the DSDT already set (it is the
8136 * slower it uses).
8138 * Always keep in mind that the DSDT *will* have set the
8139 * fans to what the vendor supposes is the best level. We
8140 * muck with it only to speed the fan up.
8142 if (fan_control_resume_level != 7 &&
8143 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8144 return;
8145 else
8146 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8147 (current_level != fan_control_resume_level);
8148 break;
8149 default:
8150 return;
8152 if (do_set) {
8153 printk(TPACPI_NOTICE
8154 "restoring fan level to 0x%02x\n",
8155 fan_control_resume_level);
8156 rc = fan_set_level_safe(fan_control_resume_level);
8157 if (rc < 0)
8158 printk(TPACPI_NOTICE
8159 "failed to restore fan level: %d\n", rc);
8163 static int fan_read(struct seq_file *m)
8165 int rc;
8166 u8 status;
8167 unsigned int speed = 0;
8169 switch (fan_status_access_mode) {
8170 case TPACPI_FAN_RD_ACPI_GFAN:
8171 /* 570, 600e/x, 770e, 770x */
8172 rc = fan_get_status_safe(&status);
8173 if (rc < 0)
8174 return rc;
8176 seq_printf(m, "status:\t\t%s\n"
8177 "level:\t\t%d\n",
8178 (status != 0) ? "enabled" : "disabled", status);
8179 break;
8181 case TPACPI_FAN_RD_TPEC:
8182 /* all except 570, 600e/x, 770e, 770x */
8183 rc = fan_get_status_safe(&status);
8184 if (rc < 0)
8185 return rc;
8187 seq_printf(m, "status:\t\t%s\n",
8188 (status != 0) ? "enabled" : "disabled");
8190 rc = fan_get_speed(&speed);
8191 if (rc < 0)
8192 return rc;
8194 seq_printf(m, "speed:\t\t%d\n", speed);
8196 if (status & TP_EC_FAN_FULLSPEED)
8197 /* Disengaged mode takes precedence */
8198 seq_printf(m, "level:\t\tdisengaged\n");
8199 else if (status & TP_EC_FAN_AUTO)
8200 seq_printf(m, "level:\t\tauto\n");
8201 else
8202 seq_printf(m, "level:\t\t%d\n", status);
8203 break;
8205 case TPACPI_FAN_NONE:
8206 default:
8207 seq_printf(m, "status:\t\tnot supported\n");
8210 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8211 seq_printf(m, "commands:\tlevel <level>");
8213 switch (fan_control_access_mode) {
8214 case TPACPI_FAN_WR_ACPI_SFAN:
8215 seq_printf(m, " (<level> is 0-7)\n");
8216 break;
8218 default:
8219 seq_printf(m, " (<level> is 0-7, "
8220 "auto, disengaged, full-speed)\n");
8221 break;
8225 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8226 seq_printf(m, "commands:\tenable, disable\n"
8227 "commands:\twatchdog <timeout> (<timeout> "
8228 "is 0 (off), 1-120 (seconds))\n");
8230 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8231 seq_printf(m, "commands:\tspeed <speed>"
8232 " (<speed> is 0-65535)\n");
8234 return 0;
8237 static int fan_write_cmd_level(const char *cmd, int *rc)
8239 int level;
8241 if (strlencmp(cmd, "level auto") == 0)
8242 level = TP_EC_FAN_AUTO;
8243 else if ((strlencmp(cmd, "level disengaged") == 0) |
8244 (strlencmp(cmd, "level full-speed") == 0))
8245 level = TP_EC_FAN_FULLSPEED;
8246 else if (sscanf(cmd, "level %d", &level) != 1)
8247 return 0;
8249 *rc = fan_set_level_safe(level);
8250 if (*rc == -ENXIO)
8251 printk(TPACPI_ERR "level command accepted for unsupported "
8252 "access mode %d", fan_control_access_mode);
8253 else if (!*rc)
8254 tpacpi_disclose_usertask("procfs fan",
8255 "set level to %d\n", level);
8257 return 1;
8260 static int fan_write_cmd_enable(const char *cmd, int *rc)
8262 if (strlencmp(cmd, "enable") != 0)
8263 return 0;
8265 *rc = fan_set_enable();
8266 if (*rc == -ENXIO)
8267 printk(TPACPI_ERR "enable command accepted for unsupported "
8268 "access mode %d", fan_control_access_mode);
8269 else if (!*rc)
8270 tpacpi_disclose_usertask("procfs fan", "enable\n");
8272 return 1;
8275 static int fan_write_cmd_disable(const char *cmd, int *rc)
8277 if (strlencmp(cmd, "disable") != 0)
8278 return 0;
8280 *rc = fan_set_disable();
8281 if (*rc == -ENXIO)
8282 printk(TPACPI_ERR "disable command accepted for unsupported "
8283 "access mode %d", fan_control_access_mode);
8284 else if (!*rc)
8285 tpacpi_disclose_usertask("procfs fan", "disable\n");
8287 return 1;
8290 static int fan_write_cmd_speed(const char *cmd, int *rc)
8292 int speed;
8294 /* TODO:
8295 * Support speed <low> <medium> <high> ? */
8297 if (sscanf(cmd, "speed %d", &speed) != 1)
8298 return 0;
8300 *rc = fan_set_speed(speed);
8301 if (*rc == -ENXIO)
8302 printk(TPACPI_ERR "speed command accepted for unsupported "
8303 "access mode %d", fan_control_access_mode);
8304 else if (!*rc)
8305 tpacpi_disclose_usertask("procfs fan",
8306 "set speed to %d\n", speed);
8308 return 1;
8311 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8313 int interval;
8315 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8316 return 0;
8318 if (interval < 0 || interval > 120)
8319 *rc = -EINVAL;
8320 else {
8321 fan_watchdog_maxinterval = interval;
8322 tpacpi_disclose_usertask("procfs fan",
8323 "set watchdog timer to %d\n",
8324 interval);
8327 return 1;
8330 static int fan_write(char *buf)
8332 char *cmd;
8333 int rc = 0;
8335 while (!rc && (cmd = next_cmd(&buf))) {
8336 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8337 fan_write_cmd_level(cmd, &rc)) &&
8338 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8339 (fan_write_cmd_enable(cmd, &rc) ||
8340 fan_write_cmd_disable(cmd, &rc) ||
8341 fan_write_cmd_watchdog(cmd, &rc))) &&
8342 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8343 fan_write_cmd_speed(cmd, &rc))
8345 rc = -EINVAL;
8346 else if (!rc)
8347 fan_watchdog_reset();
8350 return rc;
8353 static struct ibm_struct fan_driver_data = {
8354 .name = "fan",
8355 .read = fan_read,
8356 .write = fan_write,
8357 .exit = fan_exit,
8358 .suspend = fan_suspend,
8359 .resume = fan_resume,
8362 /****************************************************************************
8363 ****************************************************************************
8365 * Infrastructure
8367 ****************************************************************************
8368 ****************************************************************************/
8371 * HKEY event callout for other subdrivers go here
8372 * (yes, it is ugly, but it is quick, safe, and gets the job done
8374 static void tpacpi_driver_event(const unsigned int hkey_event)
8376 if (ibm_backlight_device) {
8377 switch (hkey_event) {
8378 case TP_HKEY_EV_BRGHT_UP:
8379 case TP_HKEY_EV_BRGHT_DOWN:
8380 tpacpi_brightness_notify_change();
8383 if (alsa_card) {
8384 switch (hkey_event) {
8385 case TP_HKEY_EV_VOL_UP:
8386 case TP_HKEY_EV_VOL_DOWN:
8387 case TP_HKEY_EV_VOL_MUTE:
8388 volume_alsa_notify_change();
8393 static void hotkey_driver_event(const unsigned int scancode)
8395 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8398 /* sysfs name ---------------------------------------------------------- */
8399 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8400 struct device_attribute *attr,
8401 char *buf)
8403 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8406 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8407 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8409 /* --------------------------------------------------------------------- */
8411 /* /proc support */
8412 static struct proc_dir_entry *proc_dir;
8415 * Module and infrastructure proble, init and exit handling
8418 static int force_load;
8420 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8421 static const char * __init str_supported(int is_supported)
8423 static char text_unsupported[] __initdata = "not supported";
8425 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8427 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8429 static void ibm_exit(struct ibm_struct *ibm)
8431 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8433 list_del_init(&ibm->all_drivers);
8435 if (ibm->flags.acpi_notify_installed) {
8436 dbg_printk(TPACPI_DBG_EXIT,
8437 "%s: acpi_remove_notify_handler\n", ibm->name);
8438 BUG_ON(!ibm->acpi);
8439 acpi_remove_notify_handler(*ibm->acpi->handle,
8440 ibm->acpi->type,
8441 dispatch_acpi_notify);
8442 ibm->flags.acpi_notify_installed = 0;
8443 ibm->flags.acpi_notify_installed = 0;
8446 if (ibm->flags.proc_created) {
8447 dbg_printk(TPACPI_DBG_EXIT,
8448 "%s: remove_proc_entry\n", ibm->name);
8449 remove_proc_entry(ibm->name, proc_dir);
8450 ibm->flags.proc_created = 0;
8453 if (ibm->flags.acpi_driver_registered) {
8454 dbg_printk(TPACPI_DBG_EXIT,
8455 "%s: acpi_bus_unregister_driver\n", ibm->name);
8456 BUG_ON(!ibm->acpi);
8457 acpi_bus_unregister_driver(ibm->acpi->driver);
8458 kfree(ibm->acpi->driver);
8459 ibm->acpi->driver = NULL;
8460 ibm->flags.acpi_driver_registered = 0;
8463 if (ibm->flags.init_called && ibm->exit) {
8464 ibm->exit();
8465 ibm->flags.init_called = 0;
8468 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8471 static int __init ibm_init(struct ibm_init_struct *iibm)
8473 int ret;
8474 struct ibm_struct *ibm = iibm->data;
8475 struct proc_dir_entry *entry;
8477 BUG_ON(ibm == NULL);
8479 INIT_LIST_HEAD(&ibm->all_drivers);
8481 if (ibm->flags.experimental && !experimental)
8482 return 0;
8484 dbg_printk(TPACPI_DBG_INIT,
8485 "probing for %s\n", ibm->name);
8487 if (iibm->init) {
8488 ret = iibm->init(iibm);
8489 if (ret > 0)
8490 return 0; /* probe failed */
8491 if (ret)
8492 return ret;
8494 ibm->flags.init_called = 1;
8497 if (ibm->acpi) {
8498 if (ibm->acpi->hid) {
8499 ret = register_tpacpi_subdriver(ibm);
8500 if (ret)
8501 goto err_out;
8504 if (ibm->acpi->notify) {
8505 ret = setup_acpi_notify(ibm);
8506 if (ret == -ENODEV) {
8507 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8508 ibm->name);
8509 ret = 0;
8510 goto err_out;
8512 if (ret < 0)
8513 goto err_out;
8517 dbg_printk(TPACPI_DBG_INIT,
8518 "%s installed\n", ibm->name);
8520 if (ibm->read) {
8521 mode_t mode = iibm->base_procfs_mode;
8523 if (!mode)
8524 mode = S_IRUGO;
8525 if (ibm->write)
8526 mode |= S_IWUSR;
8527 entry = proc_create_data(ibm->name, mode, proc_dir,
8528 &dispatch_proc_fops, ibm);
8529 if (!entry) {
8530 printk(TPACPI_ERR "unable to create proc entry %s\n",
8531 ibm->name);
8532 ret = -ENODEV;
8533 goto err_out;
8535 ibm->flags.proc_created = 1;
8538 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8540 return 0;
8542 err_out:
8543 dbg_printk(TPACPI_DBG_INIT,
8544 "%s: at error exit path with result %d\n",
8545 ibm->name, ret);
8547 ibm_exit(ibm);
8548 return (ret < 0)? ret : 0;
8551 /* Probing */
8553 static bool __pure __init tpacpi_is_fw_digit(const char c)
8555 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8558 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8559 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8560 const char t)
8562 return s && strlen(s) >= 8 &&
8563 tpacpi_is_fw_digit(s[0]) &&
8564 tpacpi_is_fw_digit(s[1]) &&
8565 s[2] == t && s[3] == 'T' &&
8566 tpacpi_is_fw_digit(s[4]) &&
8567 tpacpi_is_fw_digit(s[5]) &&
8568 s[6] == 'W' && s[7] == 'W';
8571 /* returns 0 - probe ok, or < 0 - probe error.
8572 * Probe ok doesn't mean thinkpad found.
8573 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8574 static int __must_check __init get_thinkpad_model_data(
8575 struct thinkpad_id_data *tp)
8577 const struct dmi_device *dev = NULL;
8578 char ec_fw_string[18];
8579 char const *s;
8581 if (!tp)
8582 return -EINVAL;
8584 memset(tp, 0, sizeof(*tp));
8586 if (dmi_name_in_vendors("IBM"))
8587 tp->vendor = PCI_VENDOR_ID_IBM;
8588 else if (dmi_name_in_vendors("LENOVO"))
8589 tp->vendor = PCI_VENDOR_ID_LENOVO;
8590 else
8591 return 0;
8593 s = dmi_get_system_info(DMI_BIOS_VERSION);
8594 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8595 if (s && !tp->bios_version_str)
8596 return -ENOMEM;
8598 /* Really ancient ThinkPad 240X will fail this, which is fine */
8599 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8600 return 0;
8602 tp->bios_model = tp->bios_version_str[0]
8603 | (tp->bios_version_str[1] << 8);
8604 tp->bios_release = (tp->bios_version_str[4] << 8)
8605 | tp->bios_version_str[5];
8608 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8609 * X32 or newer, all Z series; Some models must have an
8610 * up-to-date BIOS or they will not be detected.
8612 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8614 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8615 if (sscanf(dev->name,
8616 "IBM ThinkPad Embedded Controller -[%17c",
8617 ec_fw_string) == 1) {
8618 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8619 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8621 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8622 if (!tp->ec_version_str)
8623 return -ENOMEM;
8625 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8626 tp->ec_model = ec_fw_string[0]
8627 | (ec_fw_string[1] << 8);
8628 tp->ec_release = (ec_fw_string[4] << 8)
8629 | ec_fw_string[5];
8630 } else {
8631 printk(TPACPI_NOTICE
8632 "ThinkPad firmware release %s "
8633 "doesn't match the known patterns\n",
8634 ec_fw_string);
8635 printk(TPACPI_NOTICE
8636 "please report this to %s\n",
8637 TPACPI_MAIL);
8639 break;
8643 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8644 if (s && !strnicmp(s, "ThinkPad", 8)) {
8645 tp->model_str = kstrdup(s, GFP_KERNEL);
8646 if (!tp->model_str)
8647 return -ENOMEM;
8650 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8651 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8652 if (s && !tp->nummodel_str)
8653 return -ENOMEM;
8655 return 0;
8658 static int __init probe_for_thinkpad(void)
8660 int is_thinkpad;
8662 if (acpi_disabled)
8663 return -ENODEV;
8666 * Non-ancient models have better DMI tagging, but very old models
8667 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8669 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8670 (thinkpad_id.ec_model != 0) ||
8671 tpacpi_is_fw_known();
8673 /* ec is required because many other handles are relative to it */
8674 TPACPI_ACPIHANDLE_INIT(ec);
8675 if (!ec_handle) {
8676 if (is_thinkpad)
8677 printk(TPACPI_ERR
8678 "Not yet supported ThinkPad detected!\n");
8679 return -ENODEV;
8682 if (!is_thinkpad && !force_load)
8683 return -ENODEV;
8685 return 0;
8689 /* Module init, exit, parameters */
8691 static struct ibm_init_struct ibms_init[] __initdata = {
8693 .init = thinkpad_acpi_driver_init,
8694 .data = &thinkpad_acpi_driver_data,
8697 .init = hotkey_init,
8698 .data = &hotkey_driver_data,
8701 .init = bluetooth_init,
8702 .data = &bluetooth_driver_data,
8705 .init = wan_init,
8706 .data = &wan_driver_data,
8709 .init = uwb_init,
8710 .data = &uwb_driver_data,
8712 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8714 .init = video_init,
8715 .base_procfs_mode = S_IRUSR,
8716 .data = &video_driver_data,
8718 #endif
8720 .init = light_init,
8721 .data = &light_driver_data,
8724 .init = cmos_init,
8725 .data = &cmos_driver_data,
8728 .init = led_init,
8729 .data = &led_driver_data,
8732 .init = beep_init,
8733 .data = &beep_driver_data,
8736 .init = thermal_init,
8737 .data = &thermal_driver_data,
8740 .data = &ecdump_driver_data,
8743 .init = brightness_init,
8744 .data = &brightness_driver_data,
8747 .init = volume_init,
8748 .data = &volume_driver_data,
8751 .init = fan_init,
8752 .data = &fan_driver_data,
8756 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8758 unsigned int i;
8759 struct ibm_struct *ibm;
8761 if (!kp || !kp->name || !val)
8762 return -EINVAL;
8764 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8765 ibm = ibms_init[i].data;
8766 WARN_ON(ibm == NULL);
8768 if (!ibm || !ibm->name)
8769 continue;
8771 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8772 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8773 return -ENOSPC;
8774 strcpy(ibms_init[i].param, val);
8775 strcat(ibms_init[i].param, ",");
8776 return 0;
8780 return -EINVAL;
8783 module_param(experimental, int, 0444);
8784 MODULE_PARM_DESC(experimental,
8785 "Enables experimental features when non-zero");
8787 module_param_named(debug, dbg_level, uint, 0);
8788 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8790 module_param(force_load, bool, 0444);
8791 MODULE_PARM_DESC(force_load,
8792 "Attempts to load the driver even on a "
8793 "mis-identified ThinkPad when true");
8795 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8796 MODULE_PARM_DESC(fan_control,
8797 "Enables setting fan parameters features when true");
8799 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8800 MODULE_PARM_DESC(brightness_mode,
8801 "Selects brightness control strategy: "
8802 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8804 module_param(brightness_enable, uint, 0444);
8805 MODULE_PARM_DESC(brightness_enable,
8806 "Enables backlight control when 1, disables when 0");
8808 module_param(hotkey_report_mode, uint, 0444);
8809 MODULE_PARM_DESC(hotkey_report_mode,
8810 "used for backwards compatibility with userspace, "
8811 "see documentation");
8813 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8814 module_param_named(volume_mode, volume_mode, uint, 0444);
8815 MODULE_PARM_DESC(volume_mode,
8816 "Selects volume control strategy: "
8817 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8819 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8820 MODULE_PARM_DESC(volume_capabilities,
8821 "Selects the mixer capabilites: "
8822 "0=auto, 1=volume and mute, 2=mute only");
8824 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8825 MODULE_PARM_DESC(volume_control,
8826 "Enables software override for the console audio "
8827 "control when true");
8829 /* ALSA module API parameters */
8830 module_param_named(index, alsa_index, int, 0444);
8831 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8832 module_param_named(id, alsa_id, charp, 0444);
8833 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8834 module_param_named(enable, alsa_enable, bool, 0444);
8835 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8836 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8838 #define TPACPI_PARAM(feature) \
8839 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8840 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8841 "at module load, see documentation")
8843 TPACPI_PARAM(hotkey);
8844 TPACPI_PARAM(bluetooth);
8845 TPACPI_PARAM(video);
8846 TPACPI_PARAM(light);
8847 TPACPI_PARAM(cmos);
8848 TPACPI_PARAM(led);
8849 TPACPI_PARAM(beep);
8850 TPACPI_PARAM(ecdump);
8851 TPACPI_PARAM(brightness);
8852 TPACPI_PARAM(volume);
8853 TPACPI_PARAM(fan);
8855 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8856 module_param(dbg_wlswemul, uint, 0444);
8857 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8858 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8859 MODULE_PARM_DESC(wlsw_state,
8860 "Initial state of the emulated WLSW switch");
8862 module_param(dbg_bluetoothemul, uint, 0444);
8863 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8864 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8865 MODULE_PARM_DESC(bluetooth_state,
8866 "Initial state of the emulated bluetooth switch");
8868 module_param(dbg_wwanemul, uint, 0444);
8869 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8870 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8871 MODULE_PARM_DESC(wwan_state,
8872 "Initial state of the emulated WWAN switch");
8874 module_param(dbg_uwbemul, uint, 0444);
8875 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8876 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8877 MODULE_PARM_DESC(uwb_state,
8878 "Initial state of the emulated UWB switch");
8879 #endif
8881 static void thinkpad_acpi_module_exit(void)
8883 struct ibm_struct *ibm, *itmp;
8885 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8887 list_for_each_entry_safe_reverse(ibm, itmp,
8888 &tpacpi_all_drivers,
8889 all_drivers) {
8890 ibm_exit(ibm);
8893 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8895 if (tpacpi_inputdev) {
8896 if (tp_features.input_device_registered)
8897 input_unregister_device(tpacpi_inputdev);
8898 else
8899 input_free_device(tpacpi_inputdev);
8902 if (tpacpi_hwmon)
8903 hwmon_device_unregister(tpacpi_hwmon);
8905 if (tp_features.sensors_pdev_attrs_registered)
8906 device_remove_file(&tpacpi_sensors_pdev->dev,
8907 &dev_attr_thinkpad_acpi_pdev_name);
8908 if (tpacpi_sensors_pdev)
8909 platform_device_unregister(tpacpi_sensors_pdev);
8910 if (tpacpi_pdev)
8911 platform_device_unregister(tpacpi_pdev);
8913 if (tp_features.sensors_pdrv_attrs_registered)
8914 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8915 if (tp_features.platform_drv_attrs_registered)
8916 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8918 if (tp_features.sensors_pdrv_registered)
8919 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8921 if (tp_features.platform_drv_registered)
8922 platform_driver_unregister(&tpacpi_pdriver);
8924 if (proc_dir)
8925 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8927 if (tpacpi_wq)
8928 destroy_workqueue(tpacpi_wq);
8930 kfree(thinkpad_id.bios_version_str);
8931 kfree(thinkpad_id.ec_version_str);
8932 kfree(thinkpad_id.model_str);
8936 static int __init thinkpad_acpi_module_init(void)
8938 int ret, i;
8940 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8942 /* Parameter checking */
8943 if (hotkey_report_mode > 2)
8944 return -EINVAL;
8946 /* Driver-level probe */
8948 ret = get_thinkpad_model_data(&thinkpad_id);
8949 if (ret) {
8950 printk(TPACPI_ERR
8951 "unable to get DMI data: %d\n", ret);
8952 thinkpad_acpi_module_exit();
8953 return ret;
8955 ret = probe_for_thinkpad();
8956 if (ret) {
8957 thinkpad_acpi_module_exit();
8958 return ret;
8961 /* Driver initialization */
8963 TPACPI_ACPIHANDLE_INIT(ecrd);
8964 TPACPI_ACPIHANDLE_INIT(ecwr);
8966 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8967 if (!tpacpi_wq) {
8968 thinkpad_acpi_module_exit();
8969 return -ENOMEM;
8972 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8973 if (!proc_dir) {
8974 printk(TPACPI_ERR
8975 "unable to create proc dir " TPACPI_PROC_DIR);
8976 thinkpad_acpi_module_exit();
8977 return -ENODEV;
8980 ret = platform_driver_register(&tpacpi_pdriver);
8981 if (ret) {
8982 printk(TPACPI_ERR
8983 "unable to register main platform driver\n");
8984 thinkpad_acpi_module_exit();
8985 return ret;
8987 tp_features.platform_drv_registered = 1;
8989 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8990 if (ret) {
8991 printk(TPACPI_ERR
8992 "unable to register hwmon platform driver\n");
8993 thinkpad_acpi_module_exit();
8994 return ret;
8996 tp_features.sensors_pdrv_registered = 1;
8998 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8999 if (!ret) {
9000 tp_features.platform_drv_attrs_registered = 1;
9001 ret = tpacpi_create_driver_attributes(
9002 &tpacpi_hwmon_pdriver.driver);
9004 if (ret) {
9005 printk(TPACPI_ERR
9006 "unable to create sysfs driver attributes\n");
9007 thinkpad_acpi_module_exit();
9008 return ret;
9010 tp_features.sensors_pdrv_attrs_registered = 1;
9013 /* Device initialization */
9014 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9015 NULL, 0);
9016 if (IS_ERR(tpacpi_pdev)) {
9017 ret = PTR_ERR(tpacpi_pdev);
9018 tpacpi_pdev = NULL;
9019 printk(TPACPI_ERR "unable to register platform device\n");
9020 thinkpad_acpi_module_exit();
9021 return ret;
9023 tpacpi_sensors_pdev = platform_device_register_simple(
9024 TPACPI_HWMON_DRVR_NAME,
9025 -1, NULL, 0);
9026 if (IS_ERR(tpacpi_sensors_pdev)) {
9027 ret = PTR_ERR(tpacpi_sensors_pdev);
9028 tpacpi_sensors_pdev = NULL;
9029 printk(TPACPI_ERR
9030 "unable to register hwmon platform device\n");
9031 thinkpad_acpi_module_exit();
9032 return ret;
9034 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9035 &dev_attr_thinkpad_acpi_pdev_name);
9036 if (ret) {
9037 printk(TPACPI_ERR
9038 "unable to create sysfs hwmon device attributes\n");
9039 thinkpad_acpi_module_exit();
9040 return ret;
9042 tp_features.sensors_pdev_attrs_registered = 1;
9043 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9044 if (IS_ERR(tpacpi_hwmon)) {
9045 ret = PTR_ERR(tpacpi_hwmon);
9046 tpacpi_hwmon = NULL;
9047 printk(TPACPI_ERR "unable to register hwmon device\n");
9048 thinkpad_acpi_module_exit();
9049 return ret;
9051 mutex_init(&tpacpi_inputdev_send_mutex);
9052 tpacpi_inputdev = input_allocate_device();
9053 if (!tpacpi_inputdev) {
9054 printk(TPACPI_ERR "unable to allocate input device\n");
9055 thinkpad_acpi_module_exit();
9056 return -ENOMEM;
9057 } else {
9058 /* Prepare input device, but don't register */
9059 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9060 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9061 tpacpi_inputdev->id.bustype = BUS_HOST;
9062 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
9063 thinkpad_id.vendor :
9064 PCI_VENDOR_ID_IBM;
9065 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9066 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9067 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9069 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9070 ret = ibm_init(&ibms_init[i]);
9071 if (ret >= 0 && *ibms_init[i].param)
9072 ret = ibms_init[i].data->write(ibms_init[i].param);
9073 if (ret < 0) {
9074 thinkpad_acpi_module_exit();
9075 return ret;
9079 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9081 ret = input_register_device(tpacpi_inputdev);
9082 if (ret < 0) {
9083 printk(TPACPI_ERR "unable to register input device\n");
9084 thinkpad_acpi_module_exit();
9085 return ret;
9086 } else {
9087 tp_features.input_device_registered = 1;
9090 return 0;
9093 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9096 * This will autoload the driver in almost every ThinkPad
9097 * in widespread use.
9099 * Only _VERY_ old models, like the 240, 240x and 570 lack
9100 * the HKEY event interface.
9102 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9105 * DMI matching for module autoloading
9107 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9108 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9110 * Only models listed in thinkwiki will be supported, so add yours
9111 * if it is not there yet.
9113 #define IBM_BIOS_MODULE_ALIAS(__type) \
9114 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9116 /* Ancient thinkpad BIOSes have to be identified by
9117 * BIOS type or model number, and there are far less
9118 * BIOS types than model numbers... */
9119 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9121 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9122 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9123 MODULE_DESCRIPTION(TPACPI_DESC);
9124 MODULE_VERSION(TPACPI_VERSION);
9125 MODULE_LICENSE("GPL");
9127 module_init(thinkpad_acpi_module_init);
9128 module_exit(thinkpad_acpi_module_exit);