thinkpad-acpi: fix bluetooth/wwan resume
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob4ab5382ca4edfca575da24bd9bb43177cb63740c
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.24"
25 #define TPACPI_SYSFS_VERSION 0x020700
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/seq_file.h>
65 #include <linux/sysfs.h>
66 #include <linux/backlight.h>
67 #include <linux/fb.h>
68 #include <linux/platform_device.h>
69 #include <linux/hwmon.h>
70 #include <linux/hwmon-sysfs.h>
71 #include <linux/input.h>
72 #include <linux/leds.h>
73 #include <linux/rfkill.h>
74 #include <asm/uaccess.h>
76 #include <linux/dmi.h>
77 #include <linux/jiffies.h>
78 #include <linux/workqueue.h>
80 #include <sound/core.h>
81 #include <sound/control.h>
82 #include <sound/initval.h>
84 #include <acpi/acpi_drivers.h>
86 #include <linux/pci_ids.h>
89 /* ThinkPad CMOS commands */
90 #define TP_CMOS_VOLUME_DOWN 0
91 #define TP_CMOS_VOLUME_UP 1
92 #define TP_CMOS_VOLUME_MUTE 2
93 #define TP_CMOS_BRIGHTNESS_UP 4
94 #define TP_CMOS_BRIGHTNESS_DOWN 5
95 #define TP_CMOS_THINKLIGHT_ON 12
96 #define TP_CMOS_THINKLIGHT_OFF 13
98 /* NVRAM Addresses */
99 enum tp_nvram_addr {
100 TP_NVRAM_ADDR_HK2 = 0x57,
101 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
102 TP_NVRAM_ADDR_VIDEO = 0x59,
103 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
104 TP_NVRAM_ADDR_MIXER = 0x60,
107 /* NVRAM bit masks */
108 enum {
109 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
110 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
111 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
112 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
113 TP_NVRAM_MASK_THINKLIGHT = 0x10,
114 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
115 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
116 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
117 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
118 TP_NVRAM_MASK_MUTE = 0x40,
119 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
120 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
121 TP_NVRAM_POS_LEVEL_VOLUME = 0,
124 /* 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 struct ibm_struct *data;
292 static struct {
293 u32 bluetooth:1;
294 u32 hotkey:1;
295 u32 hotkey_mask:1;
296 u32 hotkey_wlsw:1;
297 u32 hotkey_tablet:1;
298 u32 light:1;
299 u32 light_status:1;
300 u32 bright_16levels:1;
301 u32 bright_acpimode:1;
302 u32 wan:1;
303 u32 uwb:1;
304 u32 fan_ctrl_status_undef:1;
305 u32 second_fan:1;
306 u32 beep_needs_two_args:1;
307 u32 mixer_no_level_control:1;
308 u32 input_device_registered:1;
309 u32 platform_drv_registered:1;
310 u32 platform_drv_attrs_registered:1;
311 u32 sensors_pdrv_registered:1;
312 u32 sensors_pdrv_attrs_registered:1;
313 u32 sensors_pdev_attrs_registered:1;
314 u32 hotkey_poll_active:1;
315 } tp_features;
317 static struct {
318 u16 hotkey_mask_ff:1;
319 u16 volume_ctrl_forbidden:1;
320 } tp_warned;
322 struct thinkpad_id_data {
323 unsigned int vendor; /* ThinkPad vendor:
324 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
326 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
327 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
329 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
330 u16 ec_model;
331 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
332 u16 ec_release;
334 char *model_str; /* ThinkPad T43 */
335 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
337 static struct thinkpad_id_data thinkpad_id;
339 static enum {
340 TPACPI_LIFE_INIT = 0,
341 TPACPI_LIFE_RUNNING,
342 TPACPI_LIFE_EXITING,
343 } tpacpi_lifecycle;
345 static int experimental;
346 static u32 dbg_level;
348 static struct workqueue_struct *tpacpi_wq;
350 enum led_status_t {
351 TPACPI_LED_OFF = 0,
352 TPACPI_LED_ON,
353 TPACPI_LED_BLINK,
356 /* Special LED class that can defer work */
357 struct tpacpi_led_classdev {
358 struct led_classdev led_classdev;
359 struct work_struct work;
360 enum led_status_t new_state;
361 unsigned int led;
364 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
365 static int dbg_wlswemul;
366 static int tpacpi_wlsw_emulstate;
367 static int dbg_bluetoothemul;
368 static int tpacpi_bluetooth_emulstate;
369 static int dbg_wwanemul;
370 static int tpacpi_wwan_emulstate;
371 static int dbg_uwbemul;
372 static int tpacpi_uwb_emulstate;
373 #endif
376 /*************************************************************************
377 * Debugging helpers
380 #define dbg_printk(a_dbg_level, format, arg...) \
381 do { if (dbg_level & (a_dbg_level)) \
382 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
383 } while (0)
385 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
386 #define vdbg_printk dbg_printk
387 static const char *str_supported(int is_supported);
388 #else
389 #define vdbg_printk(a_dbg_level, format, arg...) \
390 do { } while (0)
391 #endif
393 static void tpacpi_log_usertask(const char * const what)
395 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
396 what, task_tgid_vnr(current));
399 #define tpacpi_disclose_usertask(what, format, arg...) \
400 do { \
401 if (unlikely( \
402 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
403 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
404 printk(TPACPI_DEBUG "%s: PID %d: " format, \
405 what, task_tgid_vnr(current), ## arg); \
407 } while (0)
410 * Quirk handling helpers
412 * ThinkPad IDs and versions seen in the field so far
413 * are two-characters from the set [0-9A-Z], i.e. base 36.
415 * We use values well outside that range as specials.
418 #define TPACPI_MATCH_ANY 0xffffU
419 #define TPACPI_MATCH_UNKNOWN 0U
421 /* TPID('1', 'Y') == 0x5931 */
422 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
424 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
425 { .vendor = PCI_VENDOR_ID_IBM, \
426 .bios = TPID(__id1, __id2), \
427 .ec = TPACPI_MATCH_ANY, \
428 .quirks = (__quirk) }
430 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
431 { .vendor = PCI_VENDOR_ID_LENOVO, \
432 .bios = TPID(__id1, __id2), \
433 .ec = TPACPI_MATCH_ANY, \
434 .quirks = (__quirk) }
436 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
437 { .vendor = PCI_VENDOR_ID_LENOVO, \
438 .bios = TPACPI_MATCH_ANY, \
439 .ec = TPID(__id1, __id2), \
440 .quirks = (__quirk) }
442 struct tpacpi_quirk {
443 unsigned int vendor;
444 u16 bios;
445 u16 ec;
446 unsigned long quirks;
450 * tpacpi_check_quirks() - search BIOS/EC version on a list
451 * @qlist: array of &struct tpacpi_quirk
452 * @qlist_size: number of elements in @qlist
454 * Iterates over a quirks list until one is found that matches the
455 * ThinkPad's vendor, BIOS and EC model.
457 * Returns 0 if nothing matches, otherwise returns the quirks field of
458 * the matching &struct tpacpi_quirk entry.
460 * The match criteria is: vendor, ec and bios much match.
462 static unsigned long __init tpacpi_check_quirks(
463 const struct tpacpi_quirk *qlist,
464 unsigned int qlist_size)
466 while (qlist_size) {
467 if ((qlist->vendor == thinkpad_id.vendor ||
468 qlist->vendor == TPACPI_MATCH_ANY) &&
469 (qlist->bios == thinkpad_id.bios_model ||
470 qlist->bios == TPACPI_MATCH_ANY) &&
471 (qlist->ec == thinkpad_id.ec_model ||
472 qlist->ec == TPACPI_MATCH_ANY))
473 return qlist->quirks;
475 qlist_size--;
476 qlist++;
478 return 0;
482 /****************************************************************************
483 ****************************************************************************
485 * ACPI Helpers and device model
487 ****************************************************************************
488 ****************************************************************************/
490 /*************************************************************************
491 * ACPI basic handles
494 static acpi_handle root_handle;
496 #define TPACPI_HANDLE(object, parent, paths...) \
497 static acpi_handle object##_handle; \
498 static acpi_handle *object##_parent = &parent##_handle; \
499 static char *object##_path; \
500 static char *object##_paths[] = { paths }
502 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
503 "\\_SB.PCI.ISA.EC", /* 570 */
504 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
505 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
506 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
507 "\\_SB.PCI0.ICH3.EC0", /* R31 */
508 "\\_SB.PCI0.LPC.EC", /* all others */
511 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
512 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
514 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
515 /* T4x, X31, X40 */
516 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
517 "\\CMS", /* R40, R40e */
518 ); /* all others */
520 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
521 "^HKEY", /* R30, R31 */
522 "HKEY", /* all others */
523 ); /* 570 */
525 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
526 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
527 "\\_SB.PCI0.VID0", /* 770e */
528 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
529 "\\_SB.PCI0.AGP.VID", /* all others */
530 ); /* R30, R31 */
533 /*************************************************************************
534 * ACPI helpers
537 static int acpi_evalf(acpi_handle handle,
538 void *res, char *method, char *fmt, ...)
540 char *fmt0 = fmt;
541 struct acpi_object_list params;
542 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
543 struct acpi_buffer result, *resultp;
544 union acpi_object out_obj;
545 acpi_status status;
546 va_list ap;
547 char res_type;
548 int success;
549 int quiet;
551 if (!*fmt) {
552 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
553 return 0;
556 if (*fmt == 'q') {
557 quiet = 1;
558 fmt++;
559 } else
560 quiet = 0;
562 res_type = *(fmt++);
564 params.count = 0;
565 params.pointer = &in_objs[0];
567 va_start(ap, fmt);
568 while (*fmt) {
569 char c = *(fmt++);
570 switch (c) {
571 case 'd': /* int */
572 in_objs[params.count].integer.value = va_arg(ap, int);
573 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
574 break;
575 /* add more types as needed */
576 default:
577 printk(TPACPI_ERR "acpi_evalf() called "
578 "with invalid format character '%c'\n", c);
579 return 0;
582 va_end(ap);
584 if (res_type != 'v') {
585 result.length = sizeof(out_obj);
586 result.pointer = &out_obj;
587 resultp = &result;
588 } else
589 resultp = NULL;
591 status = acpi_evaluate_object(handle, method, &params, resultp);
593 switch (res_type) {
594 case 'd': /* int */
595 if (res)
596 *(int *)res = out_obj.integer.value;
597 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
598 break;
599 case 'v': /* void */
600 success = status == AE_OK;
601 break;
602 /* add more types as needed */
603 default:
604 printk(TPACPI_ERR "acpi_evalf() called "
605 "with invalid format character '%c'\n", res_type);
606 return 0;
609 if (!success && !quiet)
610 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
611 method, fmt0, status);
613 return success;
616 static int acpi_ec_read(int i, u8 *p)
618 int v;
620 if (ecrd_handle) {
621 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
622 return 0;
623 *p = v;
624 } else {
625 if (ec_read(i, p) < 0)
626 return 0;
629 return 1;
632 static int acpi_ec_write(int i, u8 v)
634 if (ecwr_handle) {
635 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
636 return 0;
637 } else {
638 if (ec_write(i, v) < 0)
639 return 0;
642 return 1;
645 static int issue_thinkpad_cmos_command(int cmos_cmd)
647 if (!cmos_handle)
648 return -ENXIO;
650 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
651 return -EIO;
653 return 0;
656 /*************************************************************************
657 * ACPI device model
660 #define TPACPI_ACPIHANDLE_INIT(object) \
661 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
662 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
664 static void drv_acpi_handle_init(char *name,
665 acpi_handle *handle, acpi_handle parent,
666 char **paths, int num_paths, char **path)
668 int i;
669 acpi_status status;
671 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
672 name);
674 for (i = 0; i < num_paths; i++) {
675 status = acpi_get_handle(parent, paths[i], handle);
676 if (ACPI_SUCCESS(status)) {
677 *path = paths[i];
678 dbg_printk(TPACPI_DBG_INIT,
679 "Found ACPI handle %s for %s\n",
680 *path, name);
681 return;
685 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
686 name);
687 *handle = NULL;
690 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
692 struct ibm_struct *ibm = data;
694 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
695 return;
697 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
698 return;
700 ibm->acpi->notify(ibm, event);
703 static int __init setup_acpi_notify(struct ibm_struct *ibm)
705 acpi_status status;
706 int rc;
708 BUG_ON(!ibm->acpi);
710 if (!*ibm->acpi->handle)
711 return 0;
713 vdbg_printk(TPACPI_DBG_INIT,
714 "setting up ACPI notify for %s\n", ibm->name);
716 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
717 if (rc < 0) {
718 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
719 ibm->name, rc);
720 return -ENODEV;
723 ibm->acpi->device->driver_data = ibm;
724 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
725 TPACPI_ACPI_EVENT_PREFIX,
726 ibm->name);
728 status = acpi_install_notify_handler(*ibm->acpi->handle,
729 ibm->acpi->type, dispatch_acpi_notify, ibm);
730 if (ACPI_FAILURE(status)) {
731 if (status == AE_ALREADY_EXISTS) {
732 printk(TPACPI_NOTICE
733 "another device driver is already "
734 "handling %s events\n", ibm->name);
735 } else {
736 printk(TPACPI_ERR
737 "acpi_install_notify_handler(%s) failed: %d\n",
738 ibm->name, status);
740 return -ENODEV;
742 ibm->flags.acpi_notify_installed = 1;
743 return 0;
746 static int __init tpacpi_device_add(struct acpi_device *device)
748 return 0;
751 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
753 int rc;
755 dbg_printk(TPACPI_DBG_INIT,
756 "registering %s as an ACPI driver\n", ibm->name);
758 BUG_ON(!ibm->acpi);
760 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
761 if (!ibm->acpi->driver) {
762 printk(TPACPI_ERR
763 "failed to allocate memory for ibm->acpi->driver\n");
764 return -ENOMEM;
767 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
768 ibm->acpi->driver->ids = ibm->acpi->hid;
770 ibm->acpi->driver->ops.add = &tpacpi_device_add;
772 rc = acpi_bus_register_driver(ibm->acpi->driver);
773 if (rc < 0) {
774 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
775 ibm->name, rc);
776 kfree(ibm->acpi->driver);
777 ibm->acpi->driver = NULL;
778 } else if (!rc)
779 ibm->flags.acpi_driver_registered = 1;
781 return rc;
785 /****************************************************************************
786 ****************************************************************************
788 * Procfs Helpers
790 ****************************************************************************
791 ****************************************************************************/
793 static int dispatch_proc_show(struct seq_file *m, void *v)
795 struct ibm_struct *ibm = m->private;
797 if (!ibm || !ibm->read)
798 return -EINVAL;
799 return ibm->read(m);
802 static int dispatch_proc_open(struct inode *inode, struct file *file)
804 return single_open(file, dispatch_proc_show, PDE(inode)->data);
807 static ssize_t dispatch_proc_write(struct file *file,
808 const char __user *userbuf,
809 size_t count, loff_t *pos)
811 struct ibm_struct *ibm = PDE(file->f_path.dentry->d_inode)->data;
812 char *kernbuf;
813 int ret;
815 if (!ibm || !ibm->write)
816 return -EINVAL;
817 if (count > PAGE_SIZE - 2)
818 return -EINVAL;
820 kernbuf = kmalloc(count + 2, GFP_KERNEL);
821 if (!kernbuf)
822 return -ENOMEM;
824 if (copy_from_user(kernbuf, userbuf, count)) {
825 kfree(kernbuf);
826 return -EFAULT;
829 kernbuf[count] = 0;
830 strcat(kernbuf, ",");
831 ret = ibm->write(kernbuf);
832 if (ret == 0)
833 ret = count;
835 kfree(kernbuf);
837 return ret;
840 static const struct file_operations dispatch_proc_fops = {
841 .owner = THIS_MODULE,
842 .open = dispatch_proc_open,
843 .read = seq_read,
844 .llseek = seq_lseek,
845 .release = single_release,
846 .write = dispatch_proc_write,
849 static char *next_cmd(char **cmds)
851 char *start = *cmds;
852 char *end;
854 while ((end = strchr(start, ',')) && end == start)
855 start = end + 1;
857 if (!end)
858 return NULL;
860 *end = 0;
861 *cmds = end + 1;
862 return start;
866 /****************************************************************************
867 ****************************************************************************
869 * Device model: input, hwmon and platform
871 ****************************************************************************
872 ****************************************************************************/
874 static struct platform_device *tpacpi_pdev;
875 static struct platform_device *tpacpi_sensors_pdev;
876 static struct device *tpacpi_hwmon;
877 static struct input_dev *tpacpi_inputdev;
878 static struct mutex tpacpi_inputdev_send_mutex;
879 static LIST_HEAD(tpacpi_all_drivers);
881 static int tpacpi_suspend_handler(struct platform_device *pdev,
882 pm_message_t state)
884 struct ibm_struct *ibm, *itmp;
886 list_for_each_entry_safe(ibm, itmp,
887 &tpacpi_all_drivers,
888 all_drivers) {
889 if (ibm->suspend)
890 (ibm->suspend)(state);
893 return 0;
896 static int tpacpi_resume_handler(struct platform_device *pdev)
898 struct ibm_struct *ibm, *itmp;
900 list_for_each_entry_safe(ibm, itmp,
901 &tpacpi_all_drivers,
902 all_drivers) {
903 if (ibm->resume)
904 (ibm->resume)();
907 return 0;
910 static void tpacpi_shutdown_handler(struct platform_device *pdev)
912 struct ibm_struct *ibm, *itmp;
914 list_for_each_entry_safe(ibm, itmp,
915 &tpacpi_all_drivers,
916 all_drivers) {
917 if (ibm->shutdown)
918 (ibm->shutdown)();
922 static struct platform_driver tpacpi_pdriver = {
923 .driver = {
924 .name = TPACPI_DRVR_NAME,
925 .owner = THIS_MODULE,
927 .suspend = tpacpi_suspend_handler,
928 .resume = tpacpi_resume_handler,
929 .shutdown = tpacpi_shutdown_handler,
932 static struct platform_driver tpacpi_hwmon_pdriver = {
933 .driver = {
934 .name = TPACPI_HWMON_DRVR_NAME,
935 .owner = THIS_MODULE,
939 /*************************************************************************
940 * sysfs support helpers
943 struct attribute_set {
944 unsigned int members, max_members;
945 struct attribute_group group;
948 struct attribute_set_obj {
949 struct attribute_set s;
950 struct attribute *a;
951 } __attribute__((packed));
953 static struct attribute_set *create_attr_set(unsigned int max_members,
954 const char *name)
956 struct attribute_set_obj *sobj;
958 if (max_members == 0)
959 return NULL;
961 /* Allocates space for implicit NULL at the end too */
962 sobj = kzalloc(sizeof(struct attribute_set_obj) +
963 max_members * sizeof(struct attribute *),
964 GFP_KERNEL);
965 if (!sobj)
966 return NULL;
967 sobj->s.max_members = max_members;
968 sobj->s.group.attrs = &sobj->a;
969 sobj->s.group.name = name;
971 return &sobj->s;
974 #define destroy_attr_set(_set) \
975 kfree(_set);
977 /* not multi-threaded safe, use it in a single thread per set */
978 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
980 if (!s || !attr)
981 return -EINVAL;
983 if (s->members >= s->max_members)
984 return -ENOMEM;
986 s->group.attrs[s->members] = attr;
987 s->members++;
989 return 0;
992 static int add_many_to_attr_set(struct attribute_set *s,
993 struct attribute **attr,
994 unsigned int count)
996 int i, res;
998 for (i = 0; i < count; i++) {
999 res = add_to_attr_set(s, attr[i]);
1000 if (res)
1001 return res;
1004 return 0;
1007 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1009 sysfs_remove_group(kobj, &s->group);
1010 destroy_attr_set(s);
1013 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1014 sysfs_create_group(_kobj, &_attr_set->group)
1016 static int parse_strtoul(const char *buf,
1017 unsigned long max, unsigned long *value)
1019 char *endp;
1021 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1022 endp = skip_spaces(endp);
1023 if (*endp || *value > max)
1024 return -EINVAL;
1026 return 0;
1029 static void tpacpi_disable_brightness_delay(void)
1031 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1032 printk(TPACPI_NOTICE
1033 "ACPI backlight control delay disabled\n");
1036 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
1038 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1039 union acpi_object *obj;
1040 int rc;
1042 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
1043 obj = (union acpi_object *)buffer.pointer;
1044 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
1045 printk(TPACPI_ERR "Unknown _BCL data, "
1046 "please report this to %s\n", TPACPI_MAIL);
1047 rc = 0;
1048 } else {
1049 rc = obj->package.count;
1051 } else {
1052 return 0;
1055 kfree(buffer.pointer);
1056 return rc;
1059 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1060 u32 lvl, void *context, void **rv)
1062 char name[ACPI_PATH_SEGMENT_LENGTH];
1063 struct acpi_buffer buffer = { sizeof(name), &name };
1065 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1066 !strncmp("_BCL", name, sizeof(name) - 1)) {
1067 BUG_ON(!rv || !*rv);
1068 **(int **)rv = tpacpi_query_bcl_levels(handle);
1069 return AE_CTRL_TERMINATE;
1070 } else {
1071 return AE_OK;
1076 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1078 static int __init tpacpi_check_std_acpi_brightness_support(void)
1080 int status;
1081 int bcl_levels = 0;
1082 void *bcl_ptr = &bcl_levels;
1084 if (!vid_handle) {
1085 TPACPI_ACPIHANDLE_INIT(vid);
1087 if (!vid_handle)
1088 return 0;
1091 * Search for a _BCL method, and execute it. This is safe on all
1092 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1093 * BIOS in ACPI backlight control mode. We do NOT have to care
1094 * about calling the _BCL method in an enabled video device, any
1095 * will do for our purposes.
1098 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1099 tpacpi_acpi_walk_find_bcl, NULL, NULL,
1100 &bcl_ptr);
1102 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1103 tp_features.bright_acpimode = 1;
1104 return (bcl_levels - 2);
1107 return 0;
1110 static void printk_deprecated_attribute(const char * const what,
1111 const char * const details)
1113 tpacpi_log_usertask("deprecated sysfs attribute");
1114 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1115 "will be removed. %s\n",
1116 what, details);
1119 /*************************************************************************
1120 * rfkill and radio control support helpers
1124 * ThinkPad-ACPI firmware handling model:
1126 * WLSW (master wireless switch) is event-driven, and is common to all
1127 * firmware-controlled radios. It cannot be controlled, just monitored,
1128 * as expected. It overrides all radio state in firmware
1130 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1131 * (TODO: verify how WLSW interacts with the returned radio state).
1133 * The only time there are shadow radio state changes, is when
1134 * masked-off hotkeys are used.
1138 * Internal driver API for radio state:
1140 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1141 * bool: true means radio blocked (off)
1143 enum tpacpi_rfkill_state {
1144 TPACPI_RFK_RADIO_OFF = 0,
1145 TPACPI_RFK_RADIO_ON
1148 /* rfkill switches */
1149 enum tpacpi_rfk_id {
1150 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1151 TPACPI_RFK_WWAN_SW_ID,
1152 TPACPI_RFK_UWB_SW_ID,
1153 TPACPI_RFK_SW_MAX
1156 static const char *tpacpi_rfkill_names[] = {
1157 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1158 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1159 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1160 [TPACPI_RFK_SW_MAX] = NULL
1163 /* ThinkPad-ACPI rfkill subdriver */
1164 struct tpacpi_rfk {
1165 struct rfkill *rfkill;
1166 enum tpacpi_rfk_id id;
1167 const struct tpacpi_rfk_ops *ops;
1170 struct tpacpi_rfk_ops {
1171 /* firmware interface */
1172 int (*get_status)(void);
1173 int (*set_status)(const enum tpacpi_rfkill_state);
1176 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1178 /* Query FW and update rfkill sw state for a given rfkill switch */
1179 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1181 int status;
1183 if (!tp_rfk)
1184 return -ENODEV;
1186 status = (tp_rfk->ops->get_status)();
1187 if (status < 0)
1188 return status;
1190 rfkill_set_sw_state(tp_rfk->rfkill,
1191 (status == TPACPI_RFK_RADIO_OFF));
1193 return status;
1196 /* Query FW and update rfkill sw state for all rfkill switches */
1197 static void tpacpi_rfk_update_swstate_all(void)
1199 unsigned int i;
1201 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1202 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1206 * Sync the HW-blocking state of all rfkill switches,
1207 * do notice it causes the rfkill core to schedule uevents
1209 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1211 unsigned int i;
1212 struct tpacpi_rfk *tp_rfk;
1214 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1215 tp_rfk = tpacpi_rfkill_switches[i];
1216 if (tp_rfk) {
1217 if (rfkill_set_hw_state(tp_rfk->rfkill,
1218 blocked)) {
1219 /* ignore -- we track sw block */
1225 /* Call to get the WLSW state from the firmware */
1226 static int hotkey_get_wlsw(void);
1228 /* Call to query WLSW state and update all rfkill switches */
1229 static bool tpacpi_rfk_check_hwblock_state(void)
1231 int res = hotkey_get_wlsw();
1232 int hw_blocked;
1234 /* When unknown or unsupported, we have to assume it is unblocked */
1235 if (res < 0)
1236 return false;
1238 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1239 tpacpi_rfk_update_hwblock_state(hw_blocked);
1241 return hw_blocked;
1244 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1246 struct tpacpi_rfk *tp_rfk = data;
1247 int res;
1249 dbg_printk(TPACPI_DBG_RFKILL,
1250 "request to change radio state to %s\n",
1251 blocked ? "blocked" : "unblocked");
1253 /* try to set radio state */
1254 res = (tp_rfk->ops->set_status)(blocked ?
1255 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1257 /* and update the rfkill core with whatever the FW really did */
1258 tpacpi_rfk_update_swstate(tp_rfk);
1260 return (res < 0) ? res : 0;
1263 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1264 .set_block = tpacpi_rfk_hook_set_block,
1267 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1268 const struct tpacpi_rfk_ops *tp_rfkops,
1269 const enum rfkill_type rfktype,
1270 const char *name,
1271 const bool set_default)
1273 struct tpacpi_rfk *atp_rfk;
1274 int res;
1275 bool sw_state = false;
1276 bool hw_state;
1277 int sw_status;
1279 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1281 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1282 if (atp_rfk)
1283 atp_rfk->rfkill = rfkill_alloc(name,
1284 &tpacpi_pdev->dev,
1285 rfktype,
1286 &tpacpi_rfk_rfkill_ops,
1287 atp_rfk);
1288 if (!atp_rfk || !atp_rfk->rfkill) {
1289 printk(TPACPI_ERR
1290 "failed to allocate memory for rfkill class\n");
1291 kfree(atp_rfk);
1292 return -ENOMEM;
1295 atp_rfk->id = id;
1296 atp_rfk->ops = tp_rfkops;
1298 sw_status = (tp_rfkops->get_status)();
1299 if (sw_status < 0) {
1300 printk(TPACPI_ERR
1301 "failed to read initial state for %s, error %d\n",
1302 name, sw_status);
1303 } else {
1304 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1305 if (set_default) {
1306 /* try to keep the initial state, since we ask the
1307 * firmware to preserve it across S5 in NVRAM */
1308 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1311 hw_state = tpacpi_rfk_check_hwblock_state();
1312 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1314 res = rfkill_register(atp_rfk->rfkill);
1315 if (res < 0) {
1316 printk(TPACPI_ERR
1317 "failed to register %s rfkill switch: %d\n",
1318 name, res);
1319 rfkill_destroy(atp_rfk->rfkill);
1320 kfree(atp_rfk);
1321 return res;
1324 tpacpi_rfkill_switches[id] = atp_rfk;
1326 printk(TPACPI_INFO "rfkill switch %s: radio is %sblocked\n",
1327 name, (sw_state || hw_state) ? "" : "un");
1328 return 0;
1331 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1333 struct tpacpi_rfk *tp_rfk;
1335 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1337 tp_rfk = tpacpi_rfkill_switches[id];
1338 if (tp_rfk) {
1339 rfkill_unregister(tp_rfk->rfkill);
1340 rfkill_destroy(tp_rfk->rfkill);
1341 tpacpi_rfkill_switches[id] = NULL;
1342 kfree(tp_rfk);
1346 static void printk_deprecated_rfkill_attribute(const char * const what)
1348 printk_deprecated_attribute(what,
1349 "Please switch to generic rfkill before year 2010");
1352 /* sysfs <radio> enable ------------------------------------------------ */
1353 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1354 struct device_attribute *attr,
1355 char *buf)
1357 int status;
1359 printk_deprecated_rfkill_attribute(attr->attr.name);
1361 /* This is in the ABI... */
1362 if (tpacpi_rfk_check_hwblock_state()) {
1363 status = TPACPI_RFK_RADIO_OFF;
1364 } else {
1365 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1366 if (status < 0)
1367 return status;
1370 return snprintf(buf, PAGE_SIZE, "%d\n",
1371 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1374 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1375 struct device_attribute *attr,
1376 const char *buf, size_t count)
1378 unsigned long t;
1379 int res;
1381 printk_deprecated_rfkill_attribute(attr->attr.name);
1383 if (parse_strtoul(buf, 1, &t))
1384 return -EINVAL;
1386 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1388 /* This is in the ABI... */
1389 if (tpacpi_rfk_check_hwblock_state() && !!t)
1390 return -EPERM;
1392 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1393 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1394 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1396 return (res < 0) ? res : count;
1399 /* procfs -------------------------------------------------------------- */
1400 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1402 if (id >= TPACPI_RFK_SW_MAX)
1403 seq_printf(m, "status:\t\tnot supported\n");
1404 else {
1405 int status;
1407 /* This is in the ABI... */
1408 if (tpacpi_rfk_check_hwblock_state()) {
1409 status = TPACPI_RFK_RADIO_OFF;
1410 } else {
1411 status = tpacpi_rfk_update_swstate(
1412 tpacpi_rfkill_switches[id]);
1413 if (status < 0)
1414 return status;
1417 seq_printf(m, "status:\t\t%s\n",
1418 (status == TPACPI_RFK_RADIO_ON) ?
1419 "enabled" : "disabled");
1420 seq_printf(m, "commands:\tenable, disable\n");
1423 return 0;
1426 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1428 char *cmd;
1429 int status = -1;
1430 int res = 0;
1432 if (id >= TPACPI_RFK_SW_MAX)
1433 return -ENODEV;
1435 while ((cmd = next_cmd(&buf))) {
1436 if (strlencmp(cmd, "enable") == 0)
1437 status = TPACPI_RFK_RADIO_ON;
1438 else if (strlencmp(cmd, "disable") == 0)
1439 status = TPACPI_RFK_RADIO_OFF;
1440 else
1441 return -EINVAL;
1444 if (status != -1) {
1445 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1446 (status == TPACPI_RFK_RADIO_ON) ?
1447 "enable" : "disable",
1448 tpacpi_rfkill_names[id]);
1449 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1450 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1453 return res;
1456 /*************************************************************************
1457 * thinkpad-acpi driver attributes
1460 /* interface_version --------------------------------------------------- */
1461 static ssize_t tpacpi_driver_interface_version_show(
1462 struct device_driver *drv,
1463 char *buf)
1465 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1468 static DRIVER_ATTR(interface_version, S_IRUGO,
1469 tpacpi_driver_interface_version_show, NULL);
1471 /* debug_level --------------------------------------------------------- */
1472 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1473 char *buf)
1475 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1478 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1479 const char *buf, size_t count)
1481 unsigned long t;
1483 if (parse_strtoul(buf, 0xffff, &t))
1484 return -EINVAL;
1486 dbg_level = t;
1488 return count;
1491 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1492 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1494 /* version ------------------------------------------------------------- */
1495 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1496 char *buf)
1498 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1499 TPACPI_DESC, TPACPI_VERSION);
1502 static DRIVER_ATTR(version, S_IRUGO,
1503 tpacpi_driver_version_show, NULL);
1505 /* --------------------------------------------------------------------- */
1507 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1509 /* wlsw_emulstate ------------------------------------------------------ */
1510 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1511 char *buf)
1513 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1516 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1517 const char *buf, size_t count)
1519 unsigned long t;
1521 if (parse_strtoul(buf, 1, &t))
1522 return -EINVAL;
1524 if (tpacpi_wlsw_emulstate != !!t) {
1525 tpacpi_wlsw_emulstate = !!t;
1526 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1529 return count;
1532 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1533 tpacpi_driver_wlsw_emulstate_show,
1534 tpacpi_driver_wlsw_emulstate_store);
1536 /* bluetooth_emulstate ------------------------------------------------- */
1537 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1538 struct device_driver *drv,
1539 char *buf)
1541 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1544 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1545 struct device_driver *drv,
1546 const char *buf, size_t count)
1548 unsigned long t;
1550 if (parse_strtoul(buf, 1, &t))
1551 return -EINVAL;
1553 tpacpi_bluetooth_emulstate = !!t;
1555 return count;
1558 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1559 tpacpi_driver_bluetooth_emulstate_show,
1560 tpacpi_driver_bluetooth_emulstate_store);
1562 /* wwan_emulstate ------------------------------------------------- */
1563 static ssize_t tpacpi_driver_wwan_emulstate_show(
1564 struct device_driver *drv,
1565 char *buf)
1567 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1570 static ssize_t tpacpi_driver_wwan_emulstate_store(
1571 struct device_driver *drv,
1572 const char *buf, size_t count)
1574 unsigned long t;
1576 if (parse_strtoul(buf, 1, &t))
1577 return -EINVAL;
1579 tpacpi_wwan_emulstate = !!t;
1581 return count;
1584 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1585 tpacpi_driver_wwan_emulstate_show,
1586 tpacpi_driver_wwan_emulstate_store);
1588 /* uwb_emulstate ------------------------------------------------- */
1589 static ssize_t tpacpi_driver_uwb_emulstate_show(
1590 struct device_driver *drv,
1591 char *buf)
1593 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1596 static ssize_t tpacpi_driver_uwb_emulstate_store(
1597 struct device_driver *drv,
1598 const char *buf, size_t count)
1600 unsigned long t;
1602 if (parse_strtoul(buf, 1, &t))
1603 return -EINVAL;
1605 tpacpi_uwb_emulstate = !!t;
1607 return count;
1610 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1611 tpacpi_driver_uwb_emulstate_show,
1612 tpacpi_driver_uwb_emulstate_store);
1613 #endif
1615 /* --------------------------------------------------------------------- */
1617 static struct driver_attribute *tpacpi_driver_attributes[] = {
1618 &driver_attr_debug_level, &driver_attr_version,
1619 &driver_attr_interface_version,
1622 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1624 int i, res;
1626 i = 0;
1627 res = 0;
1628 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1629 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1630 i++;
1633 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1634 if (!res && dbg_wlswemul)
1635 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1636 if (!res && dbg_bluetoothemul)
1637 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1638 if (!res && dbg_wwanemul)
1639 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1640 if (!res && dbg_uwbemul)
1641 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1642 #endif
1644 return res;
1647 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1649 int i;
1651 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1652 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1654 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1655 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1656 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1657 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1658 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1659 #endif
1662 /*************************************************************************
1663 * Firmware Data
1667 * Table of recommended minimum BIOS versions
1669 * Reasons for listing:
1670 * 1. Stable BIOS, listed because the unknown ammount of
1671 * bugs and bad ACPI behaviour on older versions
1673 * 2. BIOS or EC fw with known bugs that trigger on Linux
1675 * 3. BIOS with known reduced functionality in older versions
1677 * We recommend the latest BIOS and EC version.
1678 * We only support the latest BIOS and EC fw version as a rule.
1680 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1681 * Information from users in ThinkWiki
1683 * WARNING: we use this table also to detect that the machine is
1684 * a ThinkPad in some cases, so don't remove entries lightly.
1687 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1688 { .vendor = (__v), \
1689 .bios = TPID(__id1, __id2), \
1690 .ec = TPACPI_MATCH_ANY, \
1691 .quirks = TPACPI_MATCH_ANY << 16 \
1692 | (__bv1) << 8 | (__bv2) }
1694 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1695 __eid, __ev1, __ev2) \
1696 { .vendor = (__v), \
1697 .bios = TPID(__bid1, __bid2), \
1698 .ec = __eid, \
1699 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1700 | (__bv1) << 8 | (__bv2) }
1702 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1703 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1705 /* Outdated IBM BIOSes often lack the EC id string */
1706 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1707 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1708 __bv1, __bv2, TPID(__id1, __id2), \
1709 __ev1, __ev2), \
1710 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1711 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1712 __ev1, __ev2)
1714 /* Outdated IBM BIOSes often lack the EC id string */
1715 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1716 __eid1, __eid2, __ev1, __ev2) \
1717 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1718 __bv1, __bv2, TPID(__eid1, __eid2), \
1719 __ev1, __ev2), \
1720 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1721 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1722 __ev1, __ev2)
1724 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1725 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1727 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1728 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1729 __bv1, __bv2, TPID(__id1, __id2), \
1730 __ev1, __ev2)
1732 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1733 __eid1, __eid2, __ev1, __ev2) \
1734 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1735 __bv1, __bv2, TPID(__eid1, __eid2), \
1736 __ev1, __ev2)
1738 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1739 /* Numeric models ------------------ */
1740 /* FW MODEL BIOS VERS */
1741 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1742 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1743 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1744 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1745 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1746 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1747 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1748 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1749 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1751 /* A-series ------------------------- */
1752 /* FW MODEL BIOS VERS EC VERS */
1753 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1754 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1755 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1756 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1757 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1758 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1759 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1760 TPV_QI0('1', '3', '2', '0'), /* A22m */
1761 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1762 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1763 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1765 /* G-series ------------------------- */
1766 /* FW MODEL BIOS VERS */
1767 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1768 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1770 /* R-series, T-series --------------- */
1771 /* FW MODEL BIOS VERS EC VERS */
1772 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1773 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1774 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1775 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1776 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1777 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1778 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1779 T40/p, T41/p, T42/p (1) */
1780 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1781 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1782 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1783 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1785 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1786 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1787 TPV_QI0('1', '6', '3', '2'), /* T22 */
1788 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1789 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1790 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1792 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1793 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1794 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1796 /* BIOS FW BIOS VERS EC FW EC VERS */
1797 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1798 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1800 /* X-series ------------------------- */
1801 /* FW MODEL BIOS VERS EC VERS */
1802 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1803 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1804 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1805 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1806 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1807 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1808 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1810 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1811 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1813 /* (0) - older versions lack DMI EC fw string and functionality */
1814 /* (1) - older versions known to lack functionality */
1817 #undef TPV_QL1
1818 #undef TPV_QL0
1819 #undef TPV_QI2
1820 #undef TPV_QI1
1821 #undef TPV_QI0
1822 #undef TPV_Q_X
1823 #undef TPV_Q
1825 static void __init tpacpi_check_outdated_fw(void)
1827 unsigned long fwvers;
1828 u16 ec_version, bios_version;
1830 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1831 ARRAY_SIZE(tpacpi_bios_version_qtable));
1833 if (!fwvers)
1834 return;
1836 bios_version = fwvers & 0xffffU;
1837 ec_version = (fwvers >> 16) & 0xffffU;
1839 /* note that unknown versions are set to 0x0000 and we use that */
1840 if ((bios_version > thinkpad_id.bios_release) ||
1841 (ec_version > thinkpad_id.ec_release &&
1842 ec_version != TPACPI_MATCH_ANY)) {
1844 * The changelogs would let us track down the exact
1845 * reason, but it is just too much of a pain to track
1846 * it. We only list BIOSes that are either really
1847 * broken, or really stable to begin with, so it is
1848 * best if the user upgrades the firmware anyway.
1850 printk(TPACPI_WARN
1851 "WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1852 printk(TPACPI_WARN
1853 "WARNING: This firmware may be missing critical bug "
1854 "fixes and/or important features\n");
1858 static bool __init tpacpi_is_fw_known(void)
1860 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1861 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1864 /****************************************************************************
1865 ****************************************************************************
1867 * Subdrivers
1869 ****************************************************************************
1870 ****************************************************************************/
1872 /*************************************************************************
1873 * thinkpad-acpi init subdriver
1876 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1878 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1879 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1881 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1882 (thinkpad_id.bios_version_str) ?
1883 thinkpad_id.bios_version_str : "unknown",
1884 (thinkpad_id.ec_version_str) ?
1885 thinkpad_id.ec_version_str : "unknown");
1887 if (thinkpad_id.vendor && thinkpad_id.model_str)
1888 printk(TPACPI_INFO "%s %s, model %s\n",
1889 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1890 "IBM" : ((thinkpad_id.vendor ==
1891 PCI_VENDOR_ID_LENOVO) ?
1892 "Lenovo" : "Unknown vendor"),
1893 thinkpad_id.model_str,
1894 (thinkpad_id.nummodel_str) ?
1895 thinkpad_id.nummodel_str : "unknown");
1897 tpacpi_check_outdated_fw();
1898 return 0;
1901 static int thinkpad_acpi_driver_read(struct seq_file *m)
1903 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1904 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1905 return 0;
1908 static struct ibm_struct thinkpad_acpi_driver_data = {
1909 .name = "driver",
1910 .read = thinkpad_acpi_driver_read,
1913 /*************************************************************************
1914 * Hotkey subdriver
1918 * ThinkPad firmware event model
1920 * The ThinkPad firmware has two main event interfaces: normal ACPI
1921 * notifications (which follow the ACPI standard), and a private event
1922 * interface.
1924 * The private event interface also issues events for the hotkeys. As
1925 * the driver gained features, the event handling code ended up being
1926 * built around the hotkey subdriver. This will need to be refactored
1927 * to a more formal event API eventually.
1929 * Some "hotkeys" are actually supposed to be used as event reports,
1930 * such as "brightness has changed", "volume has changed", depending on
1931 * the ThinkPad model and how the firmware is operating.
1933 * Unlike other classes, hotkey-class events have mask/unmask control on
1934 * non-ancient firmware. However, how it behaves changes a lot with the
1935 * firmware model and version.
1938 enum { /* hot key scan codes (derived from ACPI DSDT) */
1939 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1940 TP_ACPI_HOTKEYSCAN_FNF2,
1941 TP_ACPI_HOTKEYSCAN_FNF3,
1942 TP_ACPI_HOTKEYSCAN_FNF4,
1943 TP_ACPI_HOTKEYSCAN_FNF5,
1944 TP_ACPI_HOTKEYSCAN_FNF6,
1945 TP_ACPI_HOTKEYSCAN_FNF7,
1946 TP_ACPI_HOTKEYSCAN_FNF8,
1947 TP_ACPI_HOTKEYSCAN_FNF9,
1948 TP_ACPI_HOTKEYSCAN_FNF10,
1949 TP_ACPI_HOTKEYSCAN_FNF11,
1950 TP_ACPI_HOTKEYSCAN_FNF12,
1951 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1952 TP_ACPI_HOTKEYSCAN_FNINSERT,
1953 TP_ACPI_HOTKEYSCAN_FNDELETE,
1954 TP_ACPI_HOTKEYSCAN_FNHOME,
1955 TP_ACPI_HOTKEYSCAN_FNEND,
1956 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1957 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1958 TP_ACPI_HOTKEYSCAN_FNSPACE,
1959 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1960 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1961 TP_ACPI_HOTKEYSCAN_MUTE,
1962 TP_ACPI_HOTKEYSCAN_THINKPAD,
1965 enum { /* Keys/events available through NVRAM polling */
1966 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1967 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1970 enum { /* Positions of some of the keys in hotkey masks */
1971 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1972 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1973 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1974 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1975 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1976 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1977 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1978 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1979 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1980 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1981 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1984 enum { /* NVRAM to ACPI HKEY group map */
1985 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1986 TP_ACPI_HKEY_ZOOM_MASK |
1987 TP_ACPI_HKEY_DISPSWTCH_MASK |
1988 TP_ACPI_HKEY_HIBERNATE_MASK,
1989 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1990 TP_ACPI_HKEY_BRGHTDWN_MASK,
1991 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1992 TP_ACPI_HKEY_VOLDWN_MASK |
1993 TP_ACPI_HKEY_MUTE_MASK,
1996 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1997 struct tp_nvram_state {
1998 u16 thinkpad_toggle:1;
1999 u16 zoom_toggle:1;
2000 u16 display_toggle:1;
2001 u16 thinklight_toggle:1;
2002 u16 hibernate_toggle:1;
2003 u16 displayexp_toggle:1;
2004 u16 display_state:1;
2005 u16 brightness_toggle:1;
2006 u16 volume_toggle:1;
2007 u16 mute:1;
2009 u8 brightness_level;
2010 u8 volume_level;
2013 /* kthread for the hotkey poller */
2014 static struct task_struct *tpacpi_hotkey_task;
2016 /* Acquired while the poller kthread is running, use to sync start/stop */
2017 static struct mutex hotkey_thread_mutex;
2020 * Acquire mutex to write poller control variables as an
2021 * atomic block.
2023 * Increment hotkey_config_change when changing them if you
2024 * want the kthread to forget old state.
2026 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2028 static struct mutex hotkey_thread_data_mutex;
2029 static unsigned int hotkey_config_change;
2032 * hotkey poller control variables
2034 * Must be atomic or readers will also need to acquire mutex
2036 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2037 * should be used only when the changes need to be taken as
2038 * a block, OR when one needs to force the kthread to forget
2039 * old state.
2041 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2042 static unsigned int hotkey_poll_freq = 10; /* Hz */
2044 #define HOTKEY_CONFIG_CRITICAL_START \
2045 do { \
2046 mutex_lock(&hotkey_thread_data_mutex); \
2047 hotkey_config_change++; \
2048 } while (0);
2049 #define HOTKEY_CONFIG_CRITICAL_END \
2050 mutex_unlock(&hotkey_thread_data_mutex);
2052 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2054 #define hotkey_source_mask 0U
2055 #define HOTKEY_CONFIG_CRITICAL_START
2056 #define HOTKEY_CONFIG_CRITICAL_END
2058 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2060 static struct mutex hotkey_mutex;
2062 static enum { /* Reasons for waking up */
2063 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2064 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2065 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2066 } hotkey_wakeup_reason;
2068 static int hotkey_autosleep_ack;
2070 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2071 static u32 hotkey_all_mask; /* all events supported in fw */
2072 static u32 hotkey_reserved_mask; /* events better left disabled */
2073 static u32 hotkey_driver_mask; /* events needed by the driver */
2074 static u32 hotkey_user_mask; /* events visible to userspace */
2075 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2077 static unsigned int hotkey_report_mode;
2079 static u16 *hotkey_keycode_map;
2081 static struct attribute_set *hotkey_dev_attributes;
2083 static void tpacpi_driver_event(const unsigned int hkey_event);
2084 static void hotkey_driver_event(const unsigned int scancode);
2085 static void hotkey_poll_setup(const bool may_warn);
2087 /* HKEY.MHKG() return bits */
2088 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2090 static int hotkey_get_wlsw(void)
2092 int status;
2094 if (!tp_features.hotkey_wlsw)
2095 return -ENODEV;
2097 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2098 if (dbg_wlswemul)
2099 return (tpacpi_wlsw_emulstate) ?
2100 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2101 #endif
2103 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2104 return -EIO;
2106 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2109 static int hotkey_get_tablet_mode(int *status)
2111 int s;
2113 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2114 return -EIO;
2116 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2117 return 0;
2121 * Reads current event mask from firmware, and updates
2122 * hotkey_acpi_mask accordingly. Also resets any bits
2123 * from hotkey_user_mask that are unavailable to be
2124 * delivered (shadow requirement of the userspace ABI).
2126 * Call with hotkey_mutex held
2128 static int hotkey_mask_get(void)
2130 if (tp_features.hotkey_mask) {
2131 u32 m = 0;
2133 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2134 return -EIO;
2136 hotkey_acpi_mask = m;
2137 } else {
2138 /* no mask support doesn't mean no event support... */
2139 hotkey_acpi_mask = hotkey_all_mask;
2142 /* sync userspace-visible mask */
2143 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2145 return 0;
2148 void static hotkey_mask_warn_incomplete_mask(void)
2150 /* log only what the user can fix... */
2151 const u32 wantedmask = hotkey_driver_mask &
2152 ~(hotkey_acpi_mask | hotkey_source_mask) &
2153 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2155 if (wantedmask)
2156 printk(TPACPI_NOTICE
2157 "required events 0x%08x not enabled!\n",
2158 wantedmask);
2162 * Set the firmware mask when supported
2164 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2166 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2168 * Call with hotkey_mutex held
2170 static int hotkey_mask_set(u32 mask)
2172 int i;
2173 int rc = 0;
2175 const u32 fwmask = mask & ~hotkey_source_mask;
2177 if (tp_features.hotkey_mask) {
2178 for (i = 0; i < 32; i++) {
2179 if (!acpi_evalf(hkey_handle,
2180 NULL, "MHKM", "vdd", i + 1,
2181 !!(mask & (1 << i)))) {
2182 rc = -EIO;
2183 break;
2189 * We *must* make an inconditional call to hotkey_mask_get to
2190 * refresh hotkey_acpi_mask and update hotkey_user_mask
2192 * Take the opportunity to also log when we cannot _enable_
2193 * a given event.
2195 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2196 printk(TPACPI_NOTICE
2197 "asked for hotkey mask 0x%08x, but "
2198 "firmware forced it to 0x%08x\n",
2199 fwmask, hotkey_acpi_mask);
2202 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2203 hotkey_mask_warn_incomplete_mask();
2205 return rc;
2209 * Sets hotkey_user_mask and tries to set the firmware mask
2211 * Call with hotkey_mutex held
2213 static int hotkey_user_mask_set(const u32 mask)
2215 int rc;
2217 /* Give people a chance to notice they are doing something that
2218 * is bound to go boom on their users sooner or later */
2219 if (!tp_warned.hotkey_mask_ff &&
2220 (mask == 0xffff || mask == 0xffffff ||
2221 mask == 0xffffffff)) {
2222 tp_warned.hotkey_mask_ff = 1;
2223 printk(TPACPI_NOTICE
2224 "setting the hotkey mask to 0x%08x is likely "
2225 "not the best way to go about it\n", mask);
2226 printk(TPACPI_NOTICE
2227 "please consider using the driver defaults, "
2228 "and refer to up-to-date thinkpad-acpi "
2229 "documentation\n");
2232 /* Try to enable what the user asked for, plus whatever we need.
2233 * this syncs everything but won't enable bits in hotkey_user_mask */
2234 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2236 /* Enable the available bits in hotkey_user_mask */
2237 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2239 return rc;
2243 * Sets the driver hotkey mask.
2245 * Can be called even if the hotkey subdriver is inactive
2247 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2249 int rc;
2251 /* Do the right thing if hotkey_init has not been called yet */
2252 if (!tp_features.hotkey) {
2253 hotkey_driver_mask = mask;
2254 return 0;
2257 mutex_lock(&hotkey_mutex);
2259 HOTKEY_CONFIG_CRITICAL_START
2260 hotkey_driver_mask = mask;
2261 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2262 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2263 #endif
2264 HOTKEY_CONFIG_CRITICAL_END
2266 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2267 ~hotkey_source_mask);
2268 hotkey_poll_setup(true);
2270 mutex_unlock(&hotkey_mutex);
2272 return rc;
2275 static int hotkey_status_get(int *status)
2277 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2278 return -EIO;
2280 return 0;
2283 static int hotkey_status_set(bool enable)
2285 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2286 return -EIO;
2288 return 0;
2291 static void tpacpi_input_send_tabletsw(void)
2293 int state;
2295 if (tp_features.hotkey_tablet &&
2296 !hotkey_get_tablet_mode(&state)) {
2297 mutex_lock(&tpacpi_inputdev_send_mutex);
2299 input_report_switch(tpacpi_inputdev,
2300 SW_TABLET_MODE, !!state);
2301 input_sync(tpacpi_inputdev);
2303 mutex_unlock(&tpacpi_inputdev_send_mutex);
2307 /* Do NOT call without validating scancode first */
2308 static void tpacpi_input_send_key(const unsigned int scancode)
2310 const unsigned int keycode = hotkey_keycode_map[scancode];
2312 if (keycode != KEY_RESERVED) {
2313 mutex_lock(&tpacpi_inputdev_send_mutex);
2315 input_report_key(tpacpi_inputdev, keycode, 1);
2316 if (keycode == KEY_UNKNOWN)
2317 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2318 scancode);
2319 input_sync(tpacpi_inputdev);
2321 input_report_key(tpacpi_inputdev, keycode, 0);
2322 if (keycode == KEY_UNKNOWN)
2323 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
2324 scancode);
2325 input_sync(tpacpi_inputdev);
2327 mutex_unlock(&tpacpi_inputdev_send_mutex);
2331 /* Do NOT call without validating scancode first */
2332 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2334 hotkey_driver_event(scancode);
2335 if (hotkey_user_mask & (1 << scancode))
2336 tpacpi_input_send_key(scancode);
2339 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2340 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
2342 /* Do NOT call without validating scancode first */
2343 static void tpacpi_hotkey_send_key(unsigned int scancode)
2345 tpacpi_input_send_key_masked(scancode);
2346 if (hotkey_report_mode < 2) {
2347 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
2348 0x80, TP_HKEY_EV_HOTKEY_BASE + scancode);
2352 static void hotkey_read_nvram(struct tp_nvram_state *n, const u32 m)
2354 u8 d;
2356 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
2357 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
2358 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
2359 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
2360 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
2361 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
2363 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
2364 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
2365 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
2367 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
2368 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
2369 n->displayexp_toggle =
2370 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
2372 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
2373 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
2374 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
2375 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
2376 n->brightness_toggle =
2377 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
2379 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
2380 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
2381 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2382 >> TP_NVRAM_POS_LEVEL_VOLUME;
2383 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2384 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2388 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2389 struct tp_nvram_state *newn,
2390 const u32 event_mask)
2393 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2394 do { \
2395 if ((event_mask & (1 << __scancode)) && \
2396 oldn->__member != newn->__member) \
2397 tpacpi_hotkey_send_key(__scancode); \
2398 } while (0)
2400 #define TPACPI_MAY_SEND_KEY(__scancode) \
2401 do { \
2402 if (event_mask & (1 << __scancode)) \
2403 tpacpi_hotkey_send_key(__scancode); \
2404 } while (0)
2406 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2407 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2408 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2409 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2411 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2413 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2415 /* handle volume */
2416 if (oldn->volume_toggle != newn->volume_toggle) {
2417 if (oldn->mute != newn->mute) {
2418 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2420 if (oldn->volume_level > newn->volume_level) {
2421 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2422 } else if (oldn->volume_level < newn->volume_level) {
2423 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2424 } else if (oldn->mute == newn->mute) {
2425 /* repeated key presses that didn't change state */
2426 if (newn->mute) {
2427 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2428 } else if (newn->volume_level != 0) {
2429 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2430 } else {
2431 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2436 /* handle brightness */
2437 if (oldn->brightness_toggle != newn->brightness_toggle) {
2438 if (oldn->brightness_level < newn->brightness_level) {
2439 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2440 } else if (oldn->brightness_level > newn->brightness_level) {
2441 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2442 } else {
2443 /* repeated key presses that didn't change state */
2444 if (newn->brightness_level != 0) {
2445 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2446 } else {
2447 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2452 #undef TPACPI_COMPARE_KEY
2453 #undef TPACPI_MAY_SEND_KEY
2457 * Polling driver
2459 * We track all events in hotkey_source_mask all the time, since
2460 * most of them are edge-based. We only issue those requested by
2461 * hotkey_user_mask or hotkey_driver_mask, though.
2463 static int hotkey_kthread(void *data)
2465 struct tp_nvram_state s[2];
2466 u32 poll_mask, event_mask;
2467 unsigned int si, so;
2468 unsigned long t;
2469 unsigned int change_detector, must_reset;
2470 unsigned int poll_freq;
2472 mutex_lock(&hotkey_thread_mutex);
2474 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2475 goto exit;
2477 set_freezable();
2479 so = 0;
2480 si = 1;
2481 t = 0;
2483 /* Initial state for compares */
2484 mutex_lock(&hotkey_thread_data_mutex);
2485 change_detector = hotkey_config_change;
2486 poll_mask = hotkey_source_mask;
2487 event_mask = hotkey_source_mask &
2488 (hotkey_driver_mask | hotkey_user_mask);
2489 poll_freq = hotkey_poll_freq;
2490 mutex_unlock(&hotkey_thread_data_mutex);
2491 hotkey_read_nvram(&s[so], poll_mask);
2493 while (!kthread_should_stop()) {
2494 if (t == 0) {
2495 if (likely(poll_freq))
2496 t = 1000/poll_freq;
2497 else
2498 t = 100; /* should never happen... */
2500 t = msleep_interruptible(t);
2501 if (unlikely(kthread_should_stop()))
2502 break;
2503 must_reset = try_to_freeze();
2504 if (t > 0 && !must_reset)
2505 continue;
2507 mutex_lock(&hotkey_thread_data_mutex);
2508 if (must_reset || hotkey_config_change != change_detector) {
2509 /* forget old state on thaw or config change */
2510 si = so;
2511 t = 0;
2512 change_detector = hotkey_config_change;
2514 poll_mask = hotkey_source_mask;
2515 event_mask = hotkey_source_mask &
2516 (hotkey_driver_mask | hotkey_user_mask);
2517 poll_freq = hotkey_poll_freq;
2518 mutex_unlock(&hotkey_thread_data_mutex);
2520 if (likely(poll_mask)) {
2521 hotkey_read_nvram(&s[si], poll_mask);
2522 if (likely(si != so)) {
2523 hotkey_compare_and_issue_event(&s[so], &s[si],
2524 event_mask);
2528 so = si;
2529 si ^= 1;
2532 exit:
2533 mutex_unlock(&hotkey_thread_mutex);
2534 return 0;
2537 /* call with hotkey_mutex held */
2538 static void hotkey_poll_stop_sync(void)
2540 if (tpacpi_hotkey_task) {
2541 if (frozen(tpacpi_hotkey_task) ||
2542 freezing(tpacpi_hotkey_task))
2543 thaw_process(tpacpi_hotkey_task);
2545 kthread_stop(tpacpi_hotkey_task);
2546 tpacpi_hotkey_task = NULL;
2547 mutex_lock(&hotkey_thread_mutex);
2548 /* at this point, the thread did exit */
2549 mutex_unlock(&hotkey_thread_mutex);
2553 /* call with hotkey_mutex held */
2554 static void hotkey_poll_setup(const bool may_warn)
2556 const u32 poll_driver_mask = hotkey_driver_mask & hotkey_source_mask;
2557 const u32 poll_user_mask = hotkey_user_mask & hotkey_source_mask;
2559 if (hotkey_poll_freq > 0 &&
2560 (poll_driver_mask ||
2561 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2562 if (!tpacpi_hotkey_task) {
2563 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2564 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2565 if (IS_ERR(tpacpi_hotkey_task)) {
2566 tpacpi_hotkey_task = NULL;
2567 printk(TPACPI_ERR
2568 "could not create kernel thread "
2569 "for hotkey polling\n");
2572 } else {
2573 hotkey_poll_stop_sync();
2574 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2575 hotkey_poll_freq == 0) {
2576 printk(TPACPI_NOTICE
2577 "hot keys 0x%08x and/or events 0x%08x "
2578 "require polling, which is currently "
2579 "disabled\n",
2580 poll_user_mask, poll_driver_mask);
2585 static void hotkey_poll_setup_safe(const bool may_warn)
2587 mutex_lock(&hotkey_mutex);
2588 hotkey_poll_setup(may_warn);
2589 mutex_unlock(&hotkey_mutex);
2592 /* call with hotkey_mutex held */
2593 static void hotkey_poll_set_freq(unsigned int freq)
2595 if (!freq)
2596 hotkey_poll_stop_sync();
2598 hotkey_poll_freq = freq;
2601 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2603 static void hotkey_poll_setup(const bool __unused)
2607 static void hotkey_poll_setup_safe(const bool __unused)
2611 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2613 static int hotkey_inputdev_open(struct input_dev *dev)
2615 switch (tpacpi_lifecycle) {
2616 case TPACPI_LIFE_INIT:
2617 case TPACPI_LIFE_RUNNING:
2618 hotkey_poll_setup_safe(false);
2619 return 0;
2620 case TPACPI_LIFE_EXITING:
2621 return -EBUSY;
2624 /* Should only happen if tpacpi_lifecycle is corrupt */
2625 BUG();
2626 return -EBUSY;
2629 static void hotkey_inputdev_close(struct input_dev *dev)
2631 /* disable hotkey polling when possible */
2632 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2633 !(hotkey_source_mask & hotkey_driver_mask))
2634 hotkey_poll_setup_safe(false);
2637 /* sysfs hotkey enable ------------------------------------------------- */
2638 static ssize_t hotkey_enable_show(struct device *dev,
2639 struct device_attribute *attr,
2640 char *buf)
2642 int res, status;
2644 printk_deprecated_attribute("hotkey_enable",
2645 "Hotkey reporting is always enabled");
2647 res = hotkey_status_get(&status);
2648 if (res)
2649 return res;
2651 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2654 static ssize_t hotkey_enable_store(struct device *dev,
2655 struct device_attribute *attr,
2656 const char *buf, size_t count)
2658 unsigned long t;
2660 printk_deprecated_attribute("hotkey_enable",
2661 "Hotkeys can be disabled through hotkey_mask");
2663 if (parse_strtoul(buf, 1, &t))
2664 return -EINVAL;
2666 if (t == 0)
2667 return -EPERM;
2669 return count;
2672 static struct device_attribute dev_attr_hotkey_enable =
2673 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2674 hotkey_enable_show, hotkey_enable_store);
2676 /* sysfs hotkey mask --------------------------------------------------- */
2677 static ssize_t hotkey_mask_show(struct device *dev,
2678 struct device_attribute *attr,
2679 char *buf)
2681 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2684 static ssize_t hotkey_mask_store(struct device *dev,
2685 struct device_attribute *attr,
2686 const char *buf, size_t count)
2688 unsigned long t;
2689 int res;
2691 if (parse_strtoul(buf, 0xffffffffUL, &t))
2692 return -EINVAL;
2694 if (mutex_lock_killable(&hotkey_mutex))
2695 return -ERESTARTSYS;
2697 res = hotkey_user_mask_set(t);
2699 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2700 hotkey_poll_setup(true);
2701 #endif
2703 mutex_unlock(&hotkey_mutex);
2705 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2707 return (res) ? res : count;
2710 static struct device_attribute dev_attr_hotkey_mask =
2711 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2712 hotkey_mask_show, hotkey_mask_store);
2714 /* sysfs hotkey bios_enabled ------------------------------------------- */
2715 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2716 struct device_attribute *attr,
2717 char *buf)
2719 return sprintf(buf, "0\n");
2722 static struct device_attribute dev_attr_hotkey_bios_enabled =
2723 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2725 /* sysfs hotkey bios_mask ---------------------------------------------- */
2726 static ssize_t hotkey_bios_mask_show(struct device *dev,
2727 struct device_attribute *attr,
2728 char *buf)
2730 printk_deprecated_attribute("hotkey_bios_mask",
2731 "This attribute is useless.");
2732 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2735 static struct device_attribute dev_attr_hotkey_bios_mask =
2736 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2738 /* sysfs hotkey all_mask ----------------------------------------------- */
2739 static ssize_t hotkey_all_mask_show(struct device *dev,
2740 struct device_attribute *attr,
2741 char *buf)
2743 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2744 hotkey_all_mask | hotkey_source_mask);
2747 static struct device_attribute dev_attr_hotkey_all_mask =
2748 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2750 /* sysfs hotkey recommended_mask --------------------------------------- */
2751 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2752 struct device_attribute *attr,
2753 char *buf)
2755 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2756 (hotkey_all_mask | hotkey_source_mask)
2757 & ~hotkey_reserved_mask);
2760 static struct device_attribute dev_attr_hotkey_recommended_mask =
2761 __ATTR(hotkey_recommended_mask, S_IRUGO,
2762 hotkey_recommended_mask_show, NULL);
2764 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2766 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2767 static ssize_t hotkey_source_mask_show(struct device *dev,
2768 struct device_attribute *attr,
2769 char *buf)
2771 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2774 static ssize_t hotkey_source_mask_store(struct device *dev,
2775 struct device_attribute *attr,
2776 const char *buf, size_t count)
2778 unsigned long t;
2779 u32 r_ev;
2780 int rc;
2782 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2783 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2784 return -EINVAL;
2786 if (mutex_lock_killable(&hotkey_mutex))
2787 return -ERESTARTSYS;
2789 HOTKEY_CONFIG_CRITICAL_START
2790 hotkey_source_mask = t;
2791 HOTKEY_CONFIG_CRITICAL_END
2793 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2794 ~hotkey_source_mask);
2795 hotkey_poll_setup(true);
2797 /* check if events needed by the driver got disabled */
2798 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2799 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2801 mutex_unlock(&hotkey_mutex);
2803 if (rc < 0)
2804 printk(TPACPI_ERR "hotkey_source_mask: failed to update the"
2805 "firmware event mask!\n");
2807 if (r_ev)
2808 printk(TPACPI_NOTICE "hotkey_source_mask: "
2809 "some important events were disabled: "
2810 "0x%04x\n", r_ev);
2812 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2814 return (rc < 0) ? rc : count;
2817 static struct device_attribute dev_attr_hotkey_source_mask =
2818 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2819 hotkey_source_mask_show, hotkey_source_mask_store);
2821 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2822 static ssize_t hotkey_poll_freq_show(struct device *dev,
2823 struct device_attribute *attr,
2824 char *buf)
2826 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2829 static ssize_t hotkey_poll_freq_store(struct device *dev,
2830 struct device_attribute *attr,
2831 const char *buf, size_t count)
2833 unsigned long t;
2835 if (parse_strtoul(buf, 25, &t))
2836 return -EINVAL;
2838 if (mutex_lock_killable(&hotkey_mutex))
2839 return -ERESTARTSYS;
2841 hotkey_poll_set_freq(t);
2842 hotkey_poll_setup(true);
2844 mutex_unlock(&hotkey_mutex);
2846 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2848 return count;
2851 static struct device_attribute dev_attr_hotkey_poll_freq =
2852 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2853 hotkey_poll_freq_show, hotkey_poll_freq_store);
2855 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2857 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2858 static ssize_t hotkey_radio_sw_show(struct device *dev,
2859 struct device_attribute *attr,
2860 char *buf)
2862 int res;
2863 res = hotkey_get_wlsw();
2864 if (res < 0)
2865 return res;
2867 /* Opportunistic update */
2868 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2870 return snprintf(buf, PAGE_SIZE, "%d\n",
2871 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2874 static struct device_attribute dev_attr_hotkey_radio_sw =
2875 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2877 static void hotkey_radio_sw_notify_change(void)
2879 if (tp_features.hotkey_wlsw)
2880 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2881 "hotkey_radio_sw");
2884 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2885 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2886 struct device_attribute *attr,
2887 char *buf)
2889 int res, s;
2890 res = hotkey_get_tablet_mode(&s);
2891 if (res < 0)
2892 return res;
2894 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2897 static struct device_attribute dev_attr_hotkey_tablet_mode =
2898 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2900 static void hotkey_tablet_mode_notify_change(void)
2902 if (tp_features.hotkey_tablet)
2903 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2904 "hotkey_tablet_mode");
2907 /* sysfs hotkey report_mode -------------------------------------------- */
2908 static ssize_t hotkey_report_mode_show(struct device *dev,
2909 struct device_attribute *attr,
2910 char *buf)
2912 return snprintf(buf, PAGE_SIZE, "%d\n",
2913 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2916 static struct device_attribute dev_attr_hotkey_report_mode =
2917 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2919 /* sysfs wakeup reason (pollable) -------------------------------------- */
2920 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2921 struct device_attribute *attr,
2922 char *buf)
2924 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2927 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2928 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2930 static void hotkey_wakeup_reason_notify_change(void)
2932 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2933 "wakeup_reason");
2936 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2937 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2938 struct device_attribute *attr,
2939 char *buf)
2941 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2944 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2945 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2946 hotkey_wakeup_hotunplug_complete_show, NULL);
2948 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2950 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2951 "wakeup_hotunplug_complete");
2954 /* --------------------------------------------------------------------- */
2956 static struct attribute *hotkey_attributes[] __initdata = {
2957 &dev_attr_hotkey_enable.attr,
2958 &dev_attr_hotkey_bios_enabled.attr,
2959 &dev_attr_hotkey_bios_mask.attr,
2960 &dev_attr_hotkey_report_mode.attr,
2961 &dev_attr_hotkey_wakeup_reason.attr,
2962 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2963 &dev_attr_hotkey_mask.attr,
2964 &dev_attr_hotkey_all_mask.attr,
2965 &dev_attr_hotkey_recommended_mask.attr,
2966 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2967 &dev_attr_hotkey_source_mask.attr,
2968 &dev_attr_hotkey_poll_freq.attr,
2969 #endif
2973 * Sync both the hw and sw blocking state of all switches
2975 static void tpacpi_send_radiosw_update(void)
2977 int wlsw;
2980 * We must sync all rfkill controllers *before* issuing any
2981 * rfkill input events, or we will race the rfkill core input
2982 * handler.
2984 * tpacpi_inputdev_send_mutex works as a syncronization point
2985 * for the above.
2987 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2990 wlsw = hotkey_get_wlsw();
2992 /* Sync hw blocking state first if it is hw-blocked */
2993 if (wlsw == TPACPI_RFK_RADIO_OFF)
2994 tpacpi_rfk_update_hwblock_state(true);
2996 /* Sync sw blocking state */
2997 tpacpi_rfk_update_swstate_all();
2999 /* Sync hw blocking state last if it is hw-unblocked */
3000 if (wlsw == TPACPI_RFK_RADIO_ON)
3001 tpacpi_rfk_update_hwblock_state(false);
3003 /* Issue rfkill input event for WLSW switch */
3004 if (!(wlsw < 0)) {
3005 mutex_lock(&tpacpi_inputdev_send_mutex);
3007 input_report_switch(tpacpi_inputdev,
3008 SW_RFKILL_ALL, (wlsw > 0));
3009 input_sync(tpacpi_inputdev);
3011 mutex_unlock(&tpacpi_inputdev_send_mutex);
3015 * this can be unconditional, as we will poll state again
3016 * if userspace uses the notify to read data
3018 hotkey_radio_sw_notify_change();
3021 static void hotkey_exit(void)
3023 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3024 mutex_lock(&hotkey_mutex);
3025 hotkey_poll_stop_sync();
3026 mutex_unlock(&hotkey_mutex);
3027 #endif
3029 if (hotkey_dev_attributes)
3030 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3032 kfree(hotkey_keycode_map);
3034 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3035 "restoring original HKEY status and mask\n");
3036 /* yes, there is a bitwise or below, we want the
3037 * functions to be called even if one of them fail */
3038 if (((tp_features.hotkey_mask &&
3039 hotkey_mask_set(hotkey_orig_mask)) |
3040 hotkey_status_set(false)) != 0)
3041 printk(TPACPI_ERR
3042 "failed to restore hot key mask "
3043 "to BIOS defaults\n");
3046 static void __init hotkey_unmap(const unsigned int scancode)
3048 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3049 clear_bit(hotkey_keycode_map[scancode],
3050 tpacpi_inputdev->keybit);
3051 hotkey_keycode_map[scancode] = KEY_RESERVED;
3056 * HKEY quirks:
3057 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3060 #define TPACPI_HK_Q_INIMASK 0x0001
3062 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3063 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3064 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3065 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3066 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3067 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3068 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3069 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3070 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3071 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3072 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3073 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3074 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3075 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3076 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3077 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3078 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3079 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3080 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3081 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3084 static int __init hotkey_init(struct ibm_init_struct *iibm)
3086 /* Requirements for changing the default keymaps:
3088 * 1. Many of the keys are mapped to KEY_RESERVED for very
3089 * good reasons. Do not change them unless you have deep
3090 * knowledge on the IBM and Lenovo ThinkPad firmware for
3091 * the various ThinkPad models. The driver behaves
3092 * differently for KEY_RESERVED: such keys have their
3093 * hot key mask *unset* in mask_recommended, and also
3094 * in the initial hot key mask programmed into the
3095 * firmware at driver load time, which means the firm-
3096 * ware may react very differently if you change them to
3097 * something else;
3099 * 2. You must be subscribed to the linux-thinkpad and
3100 * ibm-acpi-devel mailing lists, and you should read the
3101 * list archives since 2007 if you want to change the
3102 * keymaps. This requirement exists so that you will
3103 * know the past history of problems with the thinkpad-
3104 * acpi driver keymaps, and also that you will be
3105 * listening to any bug reports;
3107 * 3. Do not send thinkpad-acpi specific patches directly to
3108 * for merging, *ever*. Send them to the linux-acpi
3109 * mailinglist for comments. Merging is to be done only
3110 * through acpi-test and the ACPI maintainer.
3112 * If the above is too much to ask, don't change the keymap.
3113 * Ask the thinkpad-acpi maintainer to do it, instead.
3115 static u16 ibm_keycode_map[] __initdata = {
3116 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3117 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
3118 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3119 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3121 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3122 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3123 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3124 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3126 /* brightness: firmware always reacts to them */
3127 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3128 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3130 /* Thinklight: firmware always react to it */
3131 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3133 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3134 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3136 /* Volume: firmware always react to it and reprograms
3137 * the built-in *extra* mixer. Never map it to control
3138 * another mixer by default. */
3139 KEY_RESERVED, /* 0x14: VOLUME UP */
3140 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3141 KEY_RESERVED, /* 0x16: MUTE */
3143 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3145 /* (assignments unknown, please report if found) */
3146 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3147 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3149 static u16 lenovo_keycode_map[] __initdata = {
3150 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3151 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3152 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3153 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3155 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3156 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3157 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3158 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3160 /* These should be enabled --only-- when ACPI video
3161 * is disabled (i.e. in "vendor" mode), and are handled
3162 * in a special way by the init code */
3163 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3164 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3166 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3168 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3169 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3171 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3172 * react to it and reprograms the built-in *extra* mixer.
3173 * Never map it to control another mixer by default.
3175 * T60?, T61, R60?, R61: firmware and EC tries to send
3176 * these over the regular keyboard, so these are no-ops,
3177 * but there are still weird bugs re. MUTE, so do not
3178 * change unless you get test reports from all Lenovo
3179 * models. May cause the BIOS to interfere with the
3180 * HDA mixer.
3182 KEY_RESERVED, /* 0x14: VOLUME UP */
3183 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3184 KEY_RESERVED, /* 0x16: MUTE */
3186 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3188 /* (assignments unknown, please report if found) */
3189 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3190 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3193 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
3194 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
3195 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
3197 int res, i;
3198 int status;
3199 int hkeyv;
3200 bool radiosw_state = false;
3201 bool tabletsw_state = false;
3203 unsigned long quirks;
3205 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3206 "initializing hotkey subdriver\n");
3208 BUG_ON(!tpacpi_inputdev);
3209 BUG_ON(tpacpi_inputdev->open != NULL ||
3210 tpacpi_inputdev->close != NULL);
3212 TPACPI_ACPIHANDLE_INIT(hkey);
3213 mutex_init(&hotkey_mutex);
3215 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3216 mutex_init(&hotkey_thread_mutex);
3217 mutex_init(&hotkey_thread_data_mutex);
3218 #endif
3220 /* hotkey not supported on 570 */
3221 tp_features.hotkey = hkey_handle != NULL;
3223 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3224 "hotkeys are %s\n",
3225 str_supported(tp_features.hotkey));
3227 if (!tp_features.hotkey)
3228 return 1;
3230 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3231 ARRAY_SIZE(tpacpi_hotkey_qtable));
3233 tpacpi_disable_brightness_delay();
3235 /* MUST have enough space for all attributes to be added to
3236 * hotkey_dev_attributes */
3237 hotkey_dev_attributes = create_attr_set(
3238 ARRAY_SIZE(hotkey_attributes) + 2,
3239 NULL);
3240 if (!hotkey_dev_attributes)
3241 return -ENOMEM;
3242 res = add_many_to_attr_set(hotkey_dev_attributes,
3243 hotkey_attributes,
3244 ARRAY_SIZE(hotkey_attributes));
3245 if (res)
3246 goto err_exit;
3248 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3249 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3250 for HKEY interface version 0x100 */
3251 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3252 if ((hkeyv >> 8) != 1) {
3253 printk(TPACPI_ERR "unknown version of the "
3254 "HKEY interface: 0x%x\n", hkeyv);
3255 printk(TPACPI_ERR "please report this to %s\n",
3256 TPACPI_MAIL);
3257 } else {
3259 * MHKV 0x100 in A31, R40, R40e,
3260 * T4x, X31, and later
3262 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3263 "firmware HKEY interface version: 0x%x\n",
3264 hkeyv);
3266 /* Paranoia check AND init hotkey_all_mask */
3267 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3268 "MHKA", "qd")) {
3269 printk(TPACPI_ERR
3270 "missing MHKA handler, "
3271 "please report this to %s\n",
3272 TPACPI_MAIL);
3273 /* Fallback: pre-init for FN+F3,F4,F12 */
3274 hotkey_all_mask = 0x080cU;
3275 } else {
3276 tp_features.hotkey_mask = 1;
3281 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3282 "hotkey masks are %s\n",
3283 str_supported(tp_features.hotkey_mask));
3285 /* Init hotkey_all_mask if not initialized yet */
3286 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3287 (quirks & TPACPI_HK_Q_INIMASK))
3288 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3290 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3291 if (tp_features.hotkey_mask) {
3292 /* hotkey_source_mask *must* be zero for
3293 * the first hotkey_mask_get to return hotkey_orig_mask */
3294 res = hotkey_mask_get();
3295 if (res)
3296 goto err_exit;
3298 hotkey_orig_mask = hotkey_acpi_mask;
3299 } else {
3300 hotkey_orig_mask = hotkey_all_mask;
3301 hotkey_acpi_mask = hotkey_all_mask;
3304 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3305 if (dbg_wlswemul) {
3306 tp_features.hotkey_wlsw = 1;
3307 radiosw_state = !!tpacpi_wlsw_emulstate;
3308 printk(TPACPI_INFO
3309 "radio switch emulation enabled\n");
3310 } else
3311 #endif
3312 /* Not all thinkpads have a hardware radio switch */
3313 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3314 tp_features.hotkey_wlsw = 1;
3315 radiosw_state = !!status;
3316 printk(TPACPI_INFO
3317 "radio switch found; radios are %s\n",
3318 enabled(status, 0));
3320 if (tp_features.hotkey_wlsw)
3321 res = add_to_attr_set(hotkey_dev_attributes,
3322 &dev_attr_hotkey_radio_sw.attr);
3324 /* For X41t, X60t, X61t Tablets... */
3325 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
3326 tp_features.hotkey_tablet = 1;
3327 tabletsw_state = !!(status & TP_HOTKEY_TABLET_MASK);
3328 printk(TPACPI_INFO
3329 "possible tablet mode switch found; "
3330 "ThinkPad in %s mode\n",
3331 (tabletsw_state) ? "tablet" : "laptop");
3332 res = add_to_attr_set(hotkey_dev_attributes,
3333 &dev_attr_hotkey_tablet_mode.attr);
3336 if (!res)
3337 res = register_attr_set_with_sysfs(
3338 hotkey_dev_attributes,
3339 &tpacpi_pdev->dev.kobj);
3340 if (res)
3341 goto err_exit;
3343 /* Set up key map */
3345 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3346 GFP_KERNEL);
3347 if (!hotkey_keycode_map) {
3348 printk(TPACPI_ERR
3349 "failed to allocate memory for key map\n");
3350 res = -ENOMEM;
3351 goto err_exit;
3354 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
3355 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3356 "using Lenovo default hot key map\n");
3357 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
3358 TPACPI_HOTKEY_MAP_SIZE);
3359 } else {
3360 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3361 "using IBM default hot key map\n");
3362 memcpy(hotkey_keycode_map, &ibm_keycode_map,
3363 TPACPI_HOTKEY_MAP_SIZE);
3366 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3367 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3368 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3369 tpacpi_inputdev->keycode = hotkey_keycode_map;
3370 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3371 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3372 input_set_capability(tpacpi_inputdev, EV_KEY,
3373 hotkey_keycode_map[i]);
3374 } else {
3375 if (i < sizeof(hotkey_reserved_mask)*8)
3376 hotkey_reserved_mask |= 1 << i;
3380 if (tp_features.hotkey_wlsw) {
3381 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3382 input_report_switch(tpacpi_inputdev,
3383 SW_RFKILL_ALL, radiosw_state);
3385 if (tp_features.hotkey_tablet) {
3386 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3387 input_report_switch(tpacpi_inputdev,
3388 SW_TABLET_MODE, tabletsw_state);
3391 /* Do not issue duplicate brightness change events to
3392 * userspace */
3393 if (!tp_features.bright_acpimode)
3394 /* update bright_acpimode... */
3395 tpacpi_check_std_acpi_brightness_support();
3397 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
3398 printk(TPACPI_INFO
3399 "This ThinkPad has standard ACPI backlight "
3400 "brightness control, supported by the ACPI "
3401 "video driver\n");
3402 printk(TPACPI_NOTICE
3403 "Disabling thinkpad-acpi brightness events "
3404 "by default...\n");
3406 /* Disable brightness up/down on Lenovo thinkpads when
3407 * ACPI is handling them, otherwise it is plain impossible
3408 * for userspace to do something even remotely sane */
3409 hotkey_reserved_mask |=
3410 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3411 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3412 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3413 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3416 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3417 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3418 & ~hotkey_all_mask
3419 & ~hotkey_reserved_mask;
3421 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3422 "hotkey source mask 0x%08x, polling freq %u\n",
3423 hotkey_source_mask, hotkey_poll_freq);
3424 #endif
3426 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3427 "enabling firmware HKEY event interface...\n");
3428 res = hotkey_status_set(true);
3429 if (res) {
3430 hotkey_exit();
3431 return res;
3433 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3434 | hotkey_driver_mask)
3435 & ~hotkey_source_mask);
3436 if (res < 0 && res != -ENXIO) {
3437 hotkey_exit();
3438 return res;
3440 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3441 & ~hotkey_reserved_mask;
3442 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3443 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3444 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3446 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3447 "legacy ibm/hotkey event reporting over procfs %s\n",
3448 (hotkey_report_mode < 2) ?
3449 "enabled" : "disabled");
3451 tpacpi_inputdev->open = &hotkey_inputdev_open;
3452 tpacpi_inputdev->close = &hotkey_inputdev_close;
3454 hotkey_poll_setup_safe(true);
3456 return 0;
3458 err_exit:
3459 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3460 hotkey_dev_attributes = NULL;
3462 return (res < 0)? res : 1;
3465 static bool hotkey_notify_hotkey(const u32 hkey,
3466 bool *send_acpi_ev,
3467 bool *ignore_acpi_ev)
3469 /* 0x1000-0x1FFF: key presses */
3470 unsigned int scancode = hkey & 0xfff;
3471 *send_acpi_ev = true;
3472 *ignore_acpi_ev = false;
3474 if (scancode > 0 && scancode < 0x21) {
3475 scancode--;
3476 if (!(hotkey_source_mask & (1 << scancode))) {
3477 tpacpi_input_send_key_masked(scancode);
3478 *send_acpi_ev = false;
3479 } else {
3480 *ignore_acpi_ev = true;
3482 return true;
3484 return false;
3487 static bool hotkey_notify_wakeup(const u32 hkey,
3488 bool *send_acpi_ev,
3489 bool *ignore_acpi_ev)
3491 /* 0x2000-0x2FFF: Wakeup reason */
3492 *send_acpi_ev = true;
3493 *ignore_acpi_ev = false;
3495 switch (hkey) {
3496 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3497 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3498 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3499 *ignore_acpi_ev = true;
3500 break;
3502 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3503 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3504 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3505 *ignore_acpi_ev = true;
3506 break;
3508 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3509 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3510 printk(TPACPI_ALERT
3511 "EMERGENCY WAKEUP: battery almost empty\n");
3512 /* how to auto-heal: */
3513 /* 2313: woke up from S3, go to S4/S5 */
3514 /* 2413: woke up from S4, go to S5 */
3515 break;
3517 default:
3518 return false;
3521 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3522 printk(TPACPI_INFO
3523 "woke up due to a hot-unplug "
3524 "request...\n");
3525 hotkey_wakeup_reason_notify_change();
3527 return true;
3530 static bool hotkey_notify_usrevent(const u32 hkey,
3531 bool *send_acpi_ev,
3532 bool *ignore_acpi_ev)
3534 /* 0x5000-0x5FFF: human interface helpers */
3535 *send_acpi_ev = true;
3536 *ignore_acpi_ev = false;
3538 switch (hkey) {
3539 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3540 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3541 return true;
3543 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3544 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3545 tpacpi_input_send_tabletsw();
3546 hotkey_tablet_mode_notify_change();
3547 *send_acpi_ev = false;
3548 return true;
3550 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3551 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3552 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3553 /* do not propagate these events */
3554 *ignore_acpi_ev = true;
3555 return true;
3557 default:
3558 return false;
3562 static void thermal_dump_all_sensors(void);
3564 static bool hotkey_notify_thermal(const u32 hkey,
3565 bool *send_acpi_ev,
3566 bool *ignore_acpi_ev)
3568 bool known = true;
3570 /* 0x6000-0x6FFF: thermal alarms */
3571 *send_acpi_ev = true;
3572 *ignore_acpi_ev = false;
3574 switch (hkey) {
3575 case TP_HKEY_EV_THM_TABLE_CHANGED:
3576 printk(TPACPI_INFO
3577 "EC reports that Thermal Table has changed\n");
3578 /* recommended action: do nothing, we don't have
3579 * Lenovo ATM information */
3580 return true;
3581 case TP_HKEY_EV_ALARM_BAT_HOT:
3582 printk(TPACPI_CRIT
3583 "THERMAL ALARM: battery is too hot!\n");
3584 /* recommended action: warn user through gui */
3585 break;
3586 case TP_HKEY_EV_ALARM_BAT_XHOT:
3587 printk(TPACPI_ALERT
3588 "THERMAL EMERGENCY: battery is extremely hot!\n");
3589 /* recommended action: immediate sleep/hibernate */
3590 break;
3591 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3592 printk(TPACPI_CRIT
3593 "THERMAL ALARM: "
3594 "a sensor reports something is too hot!\n");
3595 /* recommended action: warn user through gui, that */
3596 /* some internal component is too hot */
3597 break;
3598 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3599 printk(TPACPI_ALERT
3600 "THERMAL EMERGENCY: "
3601 "a sensor reports something is extremely hot!\n");
3602 /* recommended action: immediate sleep/hibernate */
3603 break;
3604 default:
3605 printk(TPACPI_ALERT
3606 "THERMAL ALERT: unknown thermal alarm received\n");
3607 known = false;
3610 thermal_dump_all_sensors();
3612 return known;
3615 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3617 u32 hkey;
3618 bool send_acpi_ev;
3619 bool ignore_acpi_ev;
3620 bool known_ev;
3622 if (event != 0x80) {
3623 printk(TPACPI_ERR
3624 "unknown HKEY notification event %d\n", event);
3625 /* forward it to userspace, maybe it knows how to handle it */
3626 acpi_bus_generate_netlink_event(
3627 ibm->acpi->device->pnp.device_class,
3628 dev_name(&ibm->acpi->device->dev),
3629 event, 0);
3630 return;
3633 while (1) {
3634 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3635 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3636 return;
3639 if (hkey == 0) {
3640 /* queue empty */
3641 return;
3644 send_acpi_ev = true;
3645 ignore_acpi_ev = false;
3647 switch (hkey >> 12) {
3648 case 1:
3649 /* 0x1000-0x1FFF: key presses */
3650 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3651 &ignore_acpi_ev);
3652 break;
3653 case 2:
3654 /* 0x2000-0x2FFF: Wakeup reason */
3655 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3656 &ignore_acpi_ev);
3657 break;
3658 case 3:
3659 /* 0x3000-0x3FFF: bay-related wakeups */
3660 switch (hkey) {
3661 case TP_HKEY_EV_BAYEJ_ACK:
3662 hotkey_autosleep_ack = 1;
3663 printk(TPACPI_INFO
3664 "bay ejected\n");
3665 hotkey_wakeup_hotunplug_complete_notify_change();
3666 known_ev = true;
3667 break;
3668 case TP_HKEY_EV_OPTDRV_EJ:
3669 /* FIXME: kick libata if SATA link offline */
3670 known_ev = true;
3671 break;
3672 default:
3673 known_ev = false;
3675 break;
3676 case 4:
3677 /* 0x4000-0x4FFF: dock-related wakeups */
3678 if (hkey == TP_HKEY_EV_UNDOCK_ACK) {
3679 hotkey_autosleep_ack = 1;
3680 printk(TPACPI_INFO
3681 "undocked\n");
3682 hotkey_wakeup_hotunplug_complete_notify_change();
3683 known_ev = true;
3684 } else {
3685 known_ev = false;
3687 break;
3688 case 5:
3689 /* 0x5000-0x5FFF: human interface helpers */
3690 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3691 &ignore_acpi_ev);
3692 break;
3693 case 6:
3694 /* 0x6000-0x6FFF: thermal alarms */
3695 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3696 &ignore_acpi_ev);
3697 break;
3698 case 7:
3699 /* 0x7000-0x7FFF: misc */
3700 if (tp_features.hotkey_wlsw &&
3701 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
3702 tpacpi_send_radiosw_update();
3703 send_acpi_ev = 0;
3704 known_ev = true;
3705 break;
3707 /* fallthrough to default */
3708 default:
3709 known_ev = false;
3711 if (!known_ev) {
3712 printk(TPACPI_NOTICE
3713 "unhandled HKEY event 0x%04x\n", hkey);
3714 printk(TPACPI_NOTICE
3715 "please report the conditions when this "
3716 "event happened to %s\n", TPACPI_MAIL);
3719 /* Legacy events */
3720 if (!ignore_acpi_ev &&
3721 (send_acpi_ev || hotkey_report_mode < 2)) {
3722 acpi_bus_generate_proc_event(ibm->acpi->device,
3723 event, hkey);
3726 /* netlink events */
3727 if (!ignore_acpi_ev && send_acpi_ev) {
3728 acpi_bus_generate_netlink_event(
3729 ibm->acpi->device->pnp.device_class,
3730 dev_name(&ibm->acpi->device->dev),
3731 event, hkey);
3736 static void hotkey_suspend(pm_message_t state)
3738 /* Do these on suspend, we get the events on early resume! */
3739 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3740 hotkey_autosleep_ack = 0;
3743 static void hotkey_resume(void)
3745 tpacpi_disable_brightness_delay();
3747 if (hotkey_status_set(true) < 0 ||
3748 hotkey_mask_set(hotkey_acpi_mask) < 0)
3749 printk(TPACPI_ERR
3750 "error while attempting to reset the event "
3751 "firmware interface\n");
3753 tpacpi_send_radiosw_update();
3754 hotkey_tablet_mode_notify_change();
3755 hotkey_wakeup_reason_notify_change();
3756 hotkey_wakeup_hotunplug_complete_notify_change();
3757 hotkey_poll_setup_safe(false);
3760 /* procfs -------------------------------------------------------------- */
3761 static int hotkey_read(struct seq_file *m)
3763 int res, status;
3765 if (!tp_features.hotkey) {
3766 seq_printf(m, "status:\t\tnot supported\n");
3767 return 0;
3770 if (mutex_lock_killable(&hotkey_mutex))
3771 return -ERESTARTSYS;
3772 res = hotkey_status_get(&status);
3773 if (!res)
3774 res = hotkey_mask_get();
3775 mutex_unlock(&hotkey_mutex);
3776 if (res)
3777 return res;
3779 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
3780 if (hotkey_all_mask) {
3781 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
3782 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
3783 } else {
3784 seq_printf(m, "mask:\t\tnot supported\n");
3785 seq_printf(m, "commands:\tenable, disable, reset\n");
3788 return 0;
3791 static void hotkey_enabledisable_warn(bool enable)
3793 tpacpi_log_usertask("procfs hotkey enable/disable");
3794 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3795 TPACPI_WARN
3796 "hotkey enable/disable functionality has been "
3797 "removed from the driver. Hotkeys are always "
3798 "enabled\n"))
3799 printk(TPACPI_ERR
3800 "Please remove the hotkey=enable module "
3801 "parameter, it is deprecated. Hotkeys are always "
3802 "enabled\n");
3805 static int hotkey_write(char *buf)
3807 int res;
3808 u32 mask;
3809 char *cmd;
3811 if (!tp_features.hotkey)
3812 return -ENODEV;
3814 if (mutex_lock_killable(&hotkey_mutex))
3815 return -ERESTARTSYS;
3817 mask = hotkey_user_mask;
3819 res = 0;
3820 while ((cmd = next_cmd(&buf))) {
3821 if (strlencmp(cmd, "enable") == 0) {
3822 hotkey_enabledisable_warn(1);
3823 } else if (strlencmp(cmd, "disable") == 0) {
3824 hotkey_enabledisable_warn(0);
3825 res = -EPERM;
3826 } else if (strlencmp(cmd, "reset") == 0) {
3827 mask = (hotkey_all_mask | hotkey_source_mask)
3828 & ~hotkey_reserved_mask;
3829 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3830 /* mask set */
3831 } else if (sscanf(cmd, "%x", &mask) == 1) {
3832 /* mask set */
3833 } else {
3834 res = -EINVAL;
3835 goto errexit;
3839 if (!res) {
3840 tpacpi_disclose_usertask("procfs hotkey",
3841 "set mask to 0x%08x\n", mask);
3842 res = hotkey_user_mask_set(mask);
3845 errexit:
3846 mutex_unlock(&hotkey_mutex);
3847 return res;
3850 static const struct acpi_device_id ibm_htk_device_ids[] = {
3851 {TPACPI_ACPI_HKEY_HID, 0},
3852 {"", 0},
3855 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3856 .hid = ibm_htk_device_ids,
3857 .notify = hotkey_notify,
3858 .handle = &hkey_handle,
3859 .type = ACPI_DEVICE_NOTIFY,
3862 static struct ibm_struct hotkey_driver_data = {
3863 .name = "hotkey",
3864 .read = hotkey_read,
3865 .write = hotkey_write,
3866 .exit = hotkey_exit,
3867 .resume = hotkey_resume,
3868 .suspend = hotkey_suspend,
3869 .acpi = &ibm_hotkey_acpidriver,
3872 /*************************************************************************
3873 * Bluetooth subdriver
3876 enum {
3877 /* ACPI GBDC/SBDC bits */
3878 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3879 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3880 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3881 0 = disable, 1 = enable */
3884 enum {
3885 /* ACPI \BLTH commands */
3886 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3887 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3888 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3889 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3890 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3893 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3895 static int bluetooth_get_status(void)
3897 int status;
3899 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3900 if (dbg_bluetoothemul)
3901 return (tpacpi_bluetooth_emulstate) ?
3902 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3903 #endif
3905 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3906 return -EIO;
3908 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3909 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3912 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3914 int status;
3916 vdbg_printk(TPACPI_DBG_RFKILL,
3917 "will attempt to %s bluetooth\n",
3918 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3920 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3921 if (dbg_bluetoothemul) {
3922 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3923 return 0;
3925 #endif
3927 if (state == TPACPI_RFK_RADIO_ON)
3928 status = TP_ACPI_BLUETOOTH_RADIOSSW
3929 | TP_ACPI_BLUETOOTH_RESUMECTRL;
3930 else
3931 status = 0;
3933 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3934 return -EIO;
3936 return 0;
3939 /* sysfs bluetooth enable ---------------------------------------------- */
3940 static ssize_t bluetooth_enable_show(struct device *dev,
3941 struct device_attribute *attr,
3942 char *buf)
3944 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3945 attr, buf);
3948 static ssize_t bluetooth_enable_store(struct device *dev,
3949 struct device_attribute *attr,
3950 const char *buf, size_t count)
3952 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3953 attr, buf, count);
3956 static struct device_attribute dev_attr_bluetooth_enable =
3957 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3958 bluetooth_enable_show, bluetooth_enable_store);
3960 /* --------------------------------------------------------------------- */
3962 static struct attribute *bluetooth_attributes[] = {
3963 &dev_attr_bluetooth_enable.attr,
3964 NULL
3967 static const struct attribute_group bluetooth_attr_group = {
3968 .attrs = bluetooth_attributes,
3971 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3972 .get_status = bluetooth_get_status,
3973 .set_status = bluetooth_set_status,
3976 static void bluetooth_shutdown(void)
3978 /* Order firmware to save current state to NVRAM */
3979 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3980 TP_ACPI_BLTH_SAVE_STATE))
3981 printk(TPACPI_NOTICE
3982 "failed to save bluetooth state to NVRAM\n");
3983 else
3984 vdbg_printk(TPACPI_DBG_RFKILL,
3985 "bluestooth state saved to NVRAM\n");
3988 static void bluetooth_exit(void)
3990 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3991 &bluetooth_attr_group);
3993 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3995 bluetooth_shutdown();
3998 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4000 int res;
4001 int status = 0;
4003 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4004 "initializing bluetooth subdriver\n");
4006 TPACPI_ACPIHANDLE_INIT(hkey);
4008 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4009 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4010 tp_features.bluetooth = hkey_handle &&
4011 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4013 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4014 "bluetooth is %s, status 0x%02x\n",
4015 str_supported(tp_features.bluetooth),
4016 status);
4018 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4019 if (dbg_bluetoothemul) {
4020 tp_features.bluetooth = 1;
4021 printk(TPACPI_INFO
4022 "bluetooth switch emulation enabled\n");
4023 } else
4024 #endif
4025 if (tp_features.bluetooth &&
4026 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4027 /* no bluetooth hardware present in system */
4028 tp_features.bluetooth = 0;
4029 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4030 "bluetooth hardware not installed\n");
4033 if (!tp_features.bluetooth)
4034 return 1;
4036 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4037 &bluetooth_tprfk_ops,
4038 RFKILL_TYPE_BLUETOOTH,
4039 TPACPI_RFK_BLUETOOTH_SW_NAME,
4040 true);
4041 if (res)
4042 return res;
4044 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4045 &bluetooth_attr_group);
4046 if (res) {
4047 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4048 return res;
4051 return 0;
4054 /* procfs -------------------------------------------------------------- */
4055 static int bluetooth_read(struct seq_file *m)
4057 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4060 static int bluetooth_write(char *buf)
4062 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4065 static struct ibm_struct bluetooth_driver_data = {
4066 .name = "bluetooth",
4067 .read = bluetooth_read,
4068 .write = bluetooth_write,
4069 .exit = bluetooth_exit,
4070 .shutdown = bluetooth_shutdown,
4073 /*************************************************************************
4074 * Wan subdriver
4077 enum {
4078 /* ACPI GWAN/SWAN bits */
4079 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4080 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4081 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4082 0 = disable, 1 = enable */
4085 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4087 static int wan_get_status(void)
4089 int status;
4091 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4092 if (dbg_wwanemul)
4093 return (tpacpi_wwan_emulstate) ?
4094 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4095 #endif
4097 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4098 return -EIO;
4100 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4101 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4104 static int wan_set_status(enum tpacpi_rfkill_state state)
4106 int status;
4108 vdbg_printk(TPACPI_DBG_RFKILL,
4109 "will attempt to %s wwan\n",
4110 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4112 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4113 if (dbg_wwanemul) {
4114 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4115 return 0;
4117 #endif
4119 if (state == TPACPI_RFK_RADIO_ON)
4120 status = TP_ACPI_WANCARD_RADIOSSW
4121 | TP_ACPI_WANCARD_RESUMECTRL;
4122 else
4123 status = 0;
4125 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4126 return -EIO;
4128 return 0;
4131 /* sysfs wan enable ---------------------------------------------------- */
4132 static ssize_t wan_enable_show(struct device *dev,
4133 struct device_attribute *attr,
4134 char *buf)
4136 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4137 attr, buf);
4140 static ssize_t wan_enable_store(struct device *dev,
4141 struct device_attribute *attr,
4142 const char *buf, size_t count)
4144 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4145 attr, buf, count);
4148 static struct device_attribute dev_attr_wan_enable =
4149 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4150 wan_enable_show, wan_enable_store);
4152 /* --------------------------------------------------------------------- */
4154 static struct attribute *wan_attributes[] = {
4155 &dev_attr_wan_enable.attr,
4156 NULL
4159 static const struct attribute_group wan_attr_group = {
4160 .attrs = wan_attributes,
4163 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4164 .get_status = wan_get_status,
4165 .set_status = wan_set_status,
4168 static void wan_shutdown(void)
4170 /* Order firmware to save current state to NVRAM */
4171 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4172 TP_ACPI_WGSV_SAVE_STATE))
4173 printk(TPACPI_NOTICE
4174 "failed to save WWAN state to NVRAM\n");
4175 else
4176 vdbg_printk(TPACPI_DBG_RFKILL,
4177 "WWAN state saved to NVRAM\n");
4180 static void wan_exit(void)
4182 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4183 &wan_attr_group);
4185 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4187 wan_shutdown();
4190 static int __init wan_init(struct ibm_init_struct *iibm)
4192 int res;
4193 int status = 0;
4195 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4196 "initializing wan subdriver\n");
4198 TPACPI_ACPIHANDLE_INIT(hkey);
4200 tp_features.wan = hkey_handle &&
4201 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4203 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4204 "wan is %s, status 0x%02x\n",
4205 str_supported(tp_features.wan),
4206 status);
4208 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4209 if (dbg_wwanemul) {
4210 tp_features.wan = 1;
4211 printk(TPACPI_INFO
4212 "wwan switch emulation enabled\n");
4213 } else
4214 #endif
4215 if (tp_features.wan &&
4216 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4217 /* no wan hardware present in system */
4218 tp_features.wan = 0;
4219 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4220 "wan hardware not installed\n");
4223 if (!tp_features.wan)
4224 return 1;
4226 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4227 &wan_tprfk_ops,
4228 RFKILL_TYPE_WWAN,
4229 TPACPI_RFK_WWAN_SW_NAME,
4230 true);
4231 if (res)
4232 return res;
4234 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4235 &wan_attr_group);
4237 if (res) {
4238 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4239 return res;
4242 return 0;
4245 /* procfs -------------------------------------------------------------- */
4246 static int wan_read(struct seq_file *m)
4248 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4251 static int wan_write(char *buf)
4253 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4256 static struct ibm_struct wan_driver_data = {
4257 .name = "wan",
4258 .read = wan_read,
4259 .write = wan_write,
4260 .exit = wan_exit,
4261 .shutdown = wan_shutdown,
4264 /*************************************************************************
4265 * UWB subdriver
4268 enum {
4269 /* ACPI GUWB/SUWB bits */
4270 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4271 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4274 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4276 static int uwb_get_status(void)
4278 int status;
4280 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4281 if (dbg_uwbemul)
4282 return (tpacpi_uwb_emulstate) ?
4283 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4284 #endif
4286 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4287 return -EIO;
4289 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4290 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4293 static int uwb_set_status(enum tpacpi_rfkill_state state)
4295 int status;
4297 vdbg_printk(TPACPI_DBG_RFKILL,
4298 "will attempt to %s UWB\n",
4299 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4301 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4302 if (dbg_uwbemul) {
4303 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4304 return 0;
4306 #endif
4308 if (state == TPACPI_RFK_RADIO_ON)
4309 status = TP_ACPI_UWB_RADIOSSW;
4310 else
4311 status = 0;
4313 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4314 return -EIO;
4316 return 0;
4319 /* --------------------------------------------------------------------- */
4321 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4322 .get_status = uwb_get_status,
4323 .set_status = uwb_set_status,
4326 static void uwb_exit(void)
4328 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4331 static int __init uwb_init(struct ibm_init_struct *iibm)
4333 int res;
4334 int status = 0;
4336 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4337 "initializing uwb subdriver\n");
4339 TPACPI_ACPIHANDLE_INIT(hkey);
4341 tp_features.uwb = hkey_handle &&
4342 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4344 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4345 "uwb is %s, status 0x%02x\n",
4346 str_supported(tp_features.uwb),
4347 status);
4349 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4350 if (dbg_uwbemul) {
4351 tp_features.uwb = 1;
4352 printk(TPACPI_INFO
4353 "uwb switch emulation enabled\n");
4354 } else
4355 #endif
4356 if (tp_features.uwb &&
4357 !(status & TP_ACPI_UWB_HWPRESENT)) {
4358 /* no uwb hardware present in system */
4359 tp_features.uwb = 0;
4360 dbg_printk(TPACPI_DBG_INIT,
4361 "uwb hardware not installed\n");
4364 if (!tp_features.uwb)
4365 return 1;
4367 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4368 &uwb_tprfk_ops,
4369 RFKILL_TYPE_UWB,
4370 TPACPI_RFK_UWB_SW_NAME,
4371 false);
4372 return res;
4375 static struct ibm_struct uwb_driver_data = {
4376 .name = "uwb",
4377 .exit = uwb_exit,
4378 .flags.experimental = 1,
4381 /*************************************************************************
4382 * Video subdriver
4385 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4387 enum video_access_mode {
4388 TPACPI_VIDEO_NONE = 0,
4389 TPACPI_VIDEO_570, /* 570 */
4390 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4391 TPACPI_VIDEO_NEW, /* all others */
4394 enum { /* video status flags, based on VIDEO_570 */
4395 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4396 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4397 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4400 enum { /* TPACPI_VIDEO_570 constants */
4401 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4402 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4403 * video_status_flags */
4404 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4405 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4408 static enum video_access_mode video_supported;
4409 static int video_orig_autosw;
4411 static int video_autosw_get(void);
4412 static int video_autosw_set(int enable);
4414 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4416 static int __init video_init(struct ibm_init_struct *iibm)
4418 int ivga;
4420 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4422 TPACPI_ACPIHANDLE_INIT(vid);
4423 TPACPI_ACPIHANDLE_INIT(vid2);
4425 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4426 /* G41, assume IVGA doesn't change */
4427 vid_handle = vid2_handle;
4429 if (!vid_handle)
4430 /* video switching not supported on R30, R31 */
4431 video_supported = TPACPI_VIDEO_NONE;
4432 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4433 /* 570 */
4434 video_supported = TPACPI_VIDEO_570;
4435 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4436 /* 600e/x, 770e, 770x */
4437 video_supported = TPACPI_VIDEO_770;
4438 else
4439 /* all others */
4440 video_supported = TPACPI_VIDEO_NEW;
4442 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4443 str_supported(video_supported != TPACPI_VIDEO_NONE),
4444 video_supported);
4446 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
4449 static void video_exit(void)
4451 dbg_printk(TPACPI_DBG_EXIT,
4452 "restoring original video autoswitch mode\n");
4453 if (video_autosw_set(video_orig_autosw))
4454 printk(TPACPI_ERR "error while trying to restore original "
4455 "video autoswitch mode\n");
4458 static int video_outputsw_get(void)
4460 int status = 0;
4461 int i;
4463 switch (video_supported) {
4464 case TPACPI_VIDEO_570:
4465 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4466 TP_ACPI_VIDEO_570_PHSCMD))
4467 return -EIO;
4468 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4469 break;
4470 case TPACPI_VIDEO_770:
4471 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4472 return -EIO;
4473 if (i)
4474 status |= TP_ACPI_VIDEO_S_LCD;
4475 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4476 return -EIO;
4477 if (i)
4478 status |= TP_ACPI_VIDEO_S_CRT;
4479 break;
4480 case TPACPI_VIDEO_NEW:
4481 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4482 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4483 return -EIO;
4484 if (i)
4485 status |= TP_ACPI_VIDEO_S_CRT;
4487 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4488 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4489 return -EIO;
4490 if (i)
4491 status |= TP_ACPI_VIDEO_S_LCD;
4492 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4493 return -EIO;
4494 if (i)
4495 status |= TP_ACPI_VIDEO_S_DVI;
4496 break;
4497 default:
4498 return -ENOSYS;
4501 return status;
4504 static int video_outputsw_set(int status)
4506 int autosw;
4507 int res = 0;
4509 switch (video_supported) {
4510 case TPACPI_VIDEO_570:
4511 res = acpi_evalf(NULL, NULL,
4512 "\\_SB.PHS2", "vdd",
4513 TP_ACPI_VIDEO_570_PHS2CMD,
4514 status | TP_ACPI_VIDEO_570_PHS2SET);
4515 break;
4516 case TPACPI_VIDEO_770:
4517 autosw = video_autosw_get();
4518 if (autosw < 0)
4519 return autosw;
4521 res = video_autosw_set(1);
4522 if (res)
4523 return res;
4524 res = acpi_evalf(vid_handle, NULL,
4525 "ASWT", "vdd", status * 0x100, 0);
4526 if (!autosw && video_autosw_set(autosw)) {
4527 printk(TPACPI_ERR
4528 "video auto-switch left enabled due to error\n");
4529 return -EIO;
4531 break;
4532 case TPACPI_VIDEO_NEW:
4533 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4534 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4535 break;
4536 default:
4537 return -ENOSYS;
4540 return (res)? 0 : -EIO;
4543 static int video_autosw_get(void)
4545 int autosw = 0;
4547 switch (video_supported) {
4548 case TPACPI_VIDEO_570:
4549 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4550 return -EIO;
4551 break;
4552 case TPACPI_VIDEO_770:
4553 case TPACPI_VIDEO_NEW:
4554 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4555 return -EIO;
4556 break;
4557 default:
4558 return -ENOSYS;
4561 return autosw & 1;
4564 static int video_autosw_set(int enable)
4566 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4567 return -EIO;
4568 return 0;
4571 static int video_outputsw_cycle(void)
4573 int autosw = video_autosw_get();
4574 int res;
4576 if (autosw < 0)
4577 return autosw;
4579 switch (video_supported) {
4580 case TPACPI_VIDEO_570:
4581 res = video_autosw_set(1);
4582 if (res)
4583 return res;
4584 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4585 break;
4586 case TPACPI_VIDEO_770:
4587 case TPACPI_VIDEO_NEW:
4588 res = video_autosw_set(1);
4589 if (res)
4590 return res;
4591 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4592 break;
4593 default:
4594 return -ENOSYS;
4596 if (!autosw && video_autosw_set(autosw)) {
4597 printk(TPACPI_ERR
4598 "video auto-switch left enabled due to error\n");
4599 return -EIO;
4602 return (res)? 0 : -EIO;
4605 static int video_expand_toggle(void)
4607 switch (video_supported) {
4608 case TPACPI_VIDEO_570:
4609 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4610 0 : -EIO;
4611 case TPACPI_VIDEO_770:
4612 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4613 0 : -EIO;
4614 case TPACPI_VIDEO_NEW:
4615 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4616 0 : -EIO;
4617 default:
4618 return -ENOSYS;
4620 /* not reached */
4623 static int video_read(struct seq_file *m)
4625 int status, autosw;
4627 if (video_supported == TPACPI_VIDEO_NONE) {
4628 seq_printf(m, "status:\t\tnot supported\n");
4629 return 0;
4632 status = video_outputsw_get();
4633 if (status < 0)
4634 return status;
4636 autosw = video_autosw_get();
4637 if (autosw < 0)
4638 return autosw;
4640 seq_printf(m, "status:\t\tsupported\n");
4641 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4642 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4643 if (video_supported == TPACPI_VIDEO_NEW)
4644 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4645 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
4646 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
4647 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
4648 if (video_supported == TPACPI_VIDEO_NEW)
4649 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
4650 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
4651 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
4653 return 0;
4656 static int video_write(char *buf)
4658 char *cmd;
4659 int enable, disable, status;
4660 int res;
4662 if (video_supported == TPACPI_VIDEO_NONE)
4663 return -ENODEV;
4665 enable = 0;
4666 disable = 0;
4668 while ((cmd = next_cmd(&buf))) {
4669 if (strlencmp(cmd, "lcd_enable") == 0) {
4670 enable |= TP_ACPI_VIDEO_S_LCD;
4671 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4672 disable |= TP_ACPI_VIDEO_S_LCD;
4673 } else if (strlencmp(cmd, "crt_enable") == 0) {
4674 enable |= TP_ACPI_VIDEO_S_CRT;
4675 } else if (strlencmp(cmd, "crt_disable") == 0) {
4676 disable |= TP_ACPI_VIDEO_S_CRT;
4677 } else if (video_supported == TPACPI_VIDEO_NEW &&
4678 strlencmp(cmd, "dvi_enable") == 0) {
4679 enable |= TP_ACPI_VIDEO_S_DVI;
4680 } else if (video_supported == TPACPI_VIDEO_NEW &&
4681 strlencmp(cmd, "dvi_disable") == 0) {
4682 disable |= TP_ACPI_VIDEO_S_DVI;
4683 } else if (strlencmp(cmd, "auto_enable") == 0) {
4684 res = video_autosw_set(1);
4685 if (res)
4686 return res;
4687 } else if (strlencmp(cmd, "auto_disable") == 0) {
4688 res = video_autosw_set(0);
4689 if (res)
4690 return res;
4691 } else if (strlencmp(cmd, "video_switch") == 0) {
4692 res = video_outputsw_cycle();
4693 if (res)
4694 return res;
4695 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4696 res = video_expand_toggle();
4697 if (res)
4698 return res;
4699 } else
4700 return -EINVAL;
4703 if (enable || disable) {
4704 status = video_outputsw_get();
4705 if (status < 0)
4706 return status;
4707 res = video_outputsw_set((status & ~disable) | enable);
4708 if (res)
4709 return res;
4712 return 0;
4715 static struct ibm_struct video_driver_data = {
4716 .name = "video",
4717 .read = video_read,
4718 .write = video_write,
4719 .exit = video_exit,
4722 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4724 /*************************************************************************
4725 * Light (thinklight) subdriver
4728 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4729 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4731 static int light_get_status(void)
4733 int status = 0;
4735 if (tp_features.light_status) {
4736 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4737 return -EIO;
4738 return (!!status);
4741 return -ENXIO;
4744 static int light_set_status(int status)
4746 int rc;
4748 if (tp_features.light) {
4749 if (cmos_handle) {
4750 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4751 (status)?
4752 TP_CMOS_THINKLIGHT_ON :
4753 TP_CMOS_THINKLIGHT_OFF);
4754 } else {
4755 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4756 (status)? 1 : 0);
4758 return (rc)? 0 : -EIO;
4761 return -ENXIO;
4764 static void light_set_status_worker(struct work_struct *work)
4766 struct tpacpi_led_classdev *data =
4767 container_of(work, struct tpacpi_led_classdev, work);
4769 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4770 light_set_status((data->new_state != TPACPI_LED_OFF));
4773 static void light_sysfs_set(struct led_classdev *led_cdev,
4774 enum led_brightness brightness)
4776 struct tpacpi_led_classdev *data =
4777 container_of(led_cdev,
4778 struct tpacpi_led_classdev,
4779 led_classdev);
4780 data->new_state = (brightness != LED_OFF) ?
4781 TPACPI_LED_ON : TPACPI_LED_OFF;
4782 queue_work(tpacpi_wq, &data->work);
4785 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4787 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4790 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4791 .led_classdev = {
4792 .name = "tpacpi::thinklight",
4793 .brightness_set = &light_sysfs_set,
4794 .brightness_get = &light_sysfs_get,
4798 static int __init light_init(struct ibm_init_struct *iibm)
4800 int rc;
4802 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4804 TPACPI_ACPIHANDLE_INIT(ledb);
4805 TPACPI_ACPIHANDLE_INIT(lght);
4806 TPACPI_ACPIHANDLE_INIT(cmos);
4807 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4809 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4810 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4812 if (tp_features.light)
4813 /* light status not supported on
4814 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4815 tp_features.light_status =
4816 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4818 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4819 str_supported(tp_features.light),
4820 str_supported(tp_features.light_status));
4822 if (!tp_features.light)
4823 return 1;
4825 rc = led_classdev_register(&tpacpi_pdev->dev,
4826 &tpacpi_led_thinklight.led_classdev);
4828 if (rc < 0) {
4829 tp_features.light = 0;
4830 tp_features.light_status = 0;
4831 } else {
4832 rc = 0;
4835 return rc;
4838 static void light_exit(void)
4840 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4841 if (work_pending(&tpacpi_led_thinklight.work))
4842 flush_workqueue(tpacpi_wq);
4845 static int light_read(struct seq_file *m)
4847 int status;
4849 if (!tp_features.light) {
4850 seq_printf(m, "status:\t\tnot supported\n");
4851 } else if (!tp_features.light_status) {
4852 seq_printf(m, "status:\t\tunknown\n");
4853 seq_printf(m, "commands:\ton, off\n");
4854 } else {
4855 status = light_get_status();
4856 if (status < 0)
4857 return status;
4858 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
4859 seq_printf(m, "commands:\ton, off\n");
4862 return 0;
4865 static int light_write(char *buf)
4867 char *cmd;
4868 int newstatus = 0;
4870 if (!tp_features.light)
4871 return -ENODEV;
4873 while ((cmd = next_cmd(&buf))) {
4874 if (strlencmp(cmd, "on") == 0) {
4875 newstatus = 1;
4876 } else if (strlencmp(cmd, "off") == 0) {
4877 newstatus = 0;
4878 } else
4879 return -EINVAL;
4882 return light_set_status(newstatus);
4885 static struct ibm_struct light_driver_data = {
4886 .name = "light",
4887 .read = light_read,
4888 .write = light_write,
4889 .exit = light_exit,
4892 /*************************************************************************
4893 * CMOS subdriver
4896 /* sysfs cmos_command -------------------------------------------------- */
4897 static ssize_t cmos_command_store(struct device *dev,
4898 struct device_attribute *attr,
4899 const char *buf, size_t count)
4901 unsigned long cmos_cmd;
4902 int res;
4904 if (parse_strtoul(buf, 21, &cmos_cmd))
4905 return -EINVAL;
4907 res = issue_thinkpad_cmos_command(cmos_cmd);
4908 return (res)? res : count;
4911 static struct device_attribute dev_attr_cmos_command =
4912 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4914 /* --------------------------------------------------------------------- */
4916 static int __init cmos_init(struct ibm_init_struct *iibm)
4918 int res;
4920 vdbg_printk(TPACPI_DBG_INIT,
4921 "initializing cmos commands subdriver\n");
4923 TPACPI_ACPIHANDLE_INIT(cmos);
4925 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4926 str_supported(cmos_handle != NULL));
4928 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4929 if (res)
4930 return res;
4932 return (cmos_handle)? 0 : 1;
4935 static void cmos_exit(void)
4937 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4940 static int cmos_read(struct seq_file *m)
4942 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4943 R30, R31, T20-22, X20-21 */
4944 if (!cmos_handle)
4945 seq_printf(m, "status:\t\tnot supported\n");
4946 else {
4947 seq_printf(m, "status:\t\tsupported\n");
4948 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
4951 return 0;
4954 static int cmos_write(char *buf)
4956 char *cmd;
4957 int cmos_cmd, res;
4959 while ((cmd = next_cmd(&buf))) {
4960 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4961 cmos_cmd >= 0 && cmos_cmd <= 21) {
4962 /* cmos_cmd set */
4963 } else
4964 return -EINVAL;
4966 res = issue_thinkpad_cmos_command(cmos_cmd);
4967 if (res)
4968 return res;
4971 return 0;
4974 static struct ibm_struct cmos_driver_data = {
4975 .name = "cmos",
4976 .read = cmos_read,
4977 .write = cmos_write,
4978 .exit = cmos_exit,
4981 /*************************************************************************
4982 * LED subdriver
4985 enum led_access_mode {
4986 TPACPI_LED_NONE = 0,
4987 TPACPI_LED_570, /* 570 */
4988 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4989 TPACPI_LED_NEW, /* all others */
4992 enum { /* For TPACPI_LED_OLD */
4993 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4994 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4995 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4998 static enum led_access_mode led_supported;
5000 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
5001 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
5002 /* T20-22, X20-21 */
5003 "LED", /* all others */
5004 ); /* R30, R31 */
5006 #define TPACPI_LED_NUMLEDS 16
5007 static struct tpacpi_led_classdev *tpacpi_leds;
5008 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5009 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5010 /* there's a limit of 19 chars + NULL before 2.6.26 */
5011 "tpacpi::power",
5012 "tpacpi:orange:batt",
5013 "tpacpi:green:batt",
5014 "tpacpi::dock_active",
5015 "tpacpi::bay_active",
5016 "tpacpi::dock_batt",
5017 "tpacpi::unknown_led",
5018 "tpacpi::standby",
5019 "tpacpi::dock_status1",
5020 "tpacpi::dock_status2",
5021 "tpacpi::unknown_led2",
5022 "tpacpi::unknown_led3",
5023 "tpacpi::thinkvantage",
5025 #define TPACPI_SAFE_LEDS 0x1081U
5027 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5029 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5030 return false;
5031 #else
5032 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5033 #endif
5036 static int led_get_status(const unsigned int led)
5038 int status;
5039 enum led_status_t led_s;
5041 switch (led_supported) {
5042 case TPACPI_LED_570:
5043 if (!acpi_evalf(ec_handle,
5044 &status, "GLED", "dd", 1 << led))
5045 return -EIO;
5046 led_s = (status == 0)?
5047 TPACPI_LED_OFF :
5048 ((status == 1)?
5049 TPACPI_LED_ON :
5050 TPACPI_LED_BLINK);
5051 tpacpi_led_state_cache[led] = led_s;
5052 return led_s;
5053 default:
5054 return -ENXIO;
5057 /* not reached */
5060 static int led_set_status(const unsigned int led,
5061 const enum led_status_t ledstatus)
5063 /* off, on, blink. Index is led_status_t */
5064 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5065 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5067 int rc = 0;
5069 switch (led_supported) {
5070 case TPACPI_LED_570:
5071 /* 570 */
5072 if (unlikely(led > 7))
5073 return -EINVAL;
5074 if (unlikely(tpacpi_is_led_restricted(led)))
5075 return -EPERM;
5076 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5077 (1 << led), led_sled_arg1[ledstatus]))
5078 rc = -EIO;
5079 break;
5080 case TPACPI_LED_OLD:
5081 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5082 if (unlikely(led > 7))
5083 return -EINVAL;
5084 if (unlikely(tpacpi_is_led_restricted(led)))
5085 return -EPERM;
5086 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5087 if (rc >= 0)
5088 rc = ec_write(TPACPI_LED_EC_HLBL,
5089 (ledstatus == TPACPI_LED_BLINK) << led);
5090 if (rc >= 0)
5091 rc = ec_write(TPACPI_LED_EC_HLCL,
5092 (ledstatus != TPACPI_LED_OFF) << led);
5093 break;
5094 case TPACPI_LED_NEW:
5095 /* all others */
5096 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5097 return -EINVAL;
5098 if (unlikely(tpacpi_is_led_restricted(led)))
5099 return -EPERM;
5100 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5101 led, led_led_arg1[ledstatus]))
5102 rc = -EIO;
5103 break;
5104 default:
5105 rc = -ENXIO;
5108 if (!rc)
5109 tpacpi_led_state_cache[led] = ledstatus;
5111 return rc;
5114 static void led_set_status_worker(struct work_struct *work)
5116 struct tpacpi_led_classdev *data =
5117 container_of(work, struct tpacpi_led_classdev, work);
5119 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
5120 led_set_status(data->led, data->new_state);
5123 static void led_sysfs_set(struct led_classdev *led_cdev,
5124 enum led_brightness brightness)
5126 struct tpacpi_led_classdev *data = container_of(led_cdev,
5127 struct tpacpi_led_classdev, led_classdev);
5129 if (brightness == LED_OFF)
5130 data->new_state = TPACPI_LED_OFF;
5131 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5132 data->new_state = TPACPI_LED_ON;
5133 else
5134 data->new_state = TPACPI_LED_BLINK;
5136 queue_work(tpacpi_wq, &data->work);
5139 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5140 unsigned long *delay_on, unsigned long *delay_off)
5142 struct tpacpi_led_classdev *data = container_of(led_cdev,
5143 struct tpacpi_led_classdev, led_classdev);
5145 /* Can we choose the flash rate? */
5146 if (*delay_on == 0 && *delay_off == 0) {
5147 /* yes. set them to the hardware blink rate (1 Hz) */
5148 *delay_on = 500; /* ms */
5149 *delay_off = 500; /* ms */
5150 } else if ((*delay_on != 500) || (*delay_off != 500))
5151 return -EINVAL;
5153 data->new_state = TPACPI_LED_BLINK;
5154 queue_work(tpacpi_wq, &data->work);
5156 return 0;
5159 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5161 int rc;
5163 struct tpacpi_led_classdev *data = container_of(led_cdev,
5164 struct tpacpi_led_classdev, led_classdev);
5166 rc = led_get_status(data->led);
5168 if (rc == TPACPI_LED_OFF || rc < 0)
5169 rc = LED_OFF; /* no error handling in led class :( */
5170 else
5171 rc = LED_FULL;
5173 return rc;
5176 static void led_exit(void)
5178 unsigned int i;
5180 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5181 if (tpacpi_leds[i].led_classdev.name)
5182 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5185 kfree(tpacpi_leds);
5188 static int __init tpacpi_init_led(unsigned int led)
5190 int rc;
5192 tpacpi_leds[led].led = led;
5194 /* LEDs with no name don't get registered */
5195 if (!tpacpi_led_names[led])
5196 return 0;
5198 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
5199 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5200 if (led_supported == TPACPI_LED_570)
5201 tpacpi_leds[led].led_classdev.brightness_get =
5202 &led_sysfs_get;
5204 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5206 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
5208 rc = led_classdev_register(&tpacpi_pdev->dev,
5209 &tpacpi_leds[led].led_classdev);
5210 if (rc < 0)
5211 tpacpi_leds[led].led_classdev.name = NULL;
5213 return rc;
5216 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5217 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5218 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5219 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5221 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5222 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5223 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5224 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5225 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5226 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5227 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5228 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5230 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5231 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5232 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5233 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5234 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5236 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5237 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5238 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5239 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5241 /* (1) - may have excess leds enabled on MSB */
5243 /* Defaults (order matters, keep last, don't reorder!) */
5244 { /* Lenovo */
5245 .vendor = PCI_VENDOR_ID_LENOVO,
5246 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5247 .quirks = 0x1fffU,
5249 { /* IBM ThinkPads with no EC version string */
5250 .vendor = PCI_VENDOR_ID_IBM,
5251 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5252 .quirks = 0x00ffU,
5254 { /* IBM ThinkPads with EC version string */
5255 .vendor = PCI_VENDOR_ID_IBM,
5256 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5257 .quirks = 0x00bfU,
5261 #undef TPACPI_LEDQ_IBM
5262 #undef TPACPI_LEDQ_LNV
5264 static int __init led_init(struct ibm_init_struct *iibm)
5266 unsigned int i;
5267 int rc;
5268 unsigned long useful_leds;
5270 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5272 TPACPI_ACPIHANDLE_INIT(led);
5274 if (!led_handle)
5275 /* led not supported on R30, R31 */
5276 led_supported = TPACPI_LED_NONE;
5277 else if (strlencmp(led_path, "SLED") == 0)
5278 /* 570 */
5279 led_supported = TPACPI_LED_570;
5280 else if (strlencmp(led_path, "SYSL") == 0)
5281 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5282 led_supported = TPACPI_LED_OLD;
5283 else
5284 /* all others */
5285 led_supported = TPACPI_LED_NEW;
5287 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5288 str_supported(led_supported), led_supported);
5290 if (led_supported == TPACPI_LED_NONE)
5291 return 1;
5293 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5294 GFP_KERNEL);
5295 if (!tpacpi_leds) {
5296 printk(TPACPI_ERR "Out of memory for LED data\n");
5297 return -ENOMEM;
5300 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5301 ARRAY_SIZE(led_useful_qtable));
5303 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5304 if (!tpacpi_is_led_restricted(i) &&
5305 test_bit(i, &useful_leds)) {
5306 rc = tpacpi_init_led(i);
5307 if (rc < 0) {
5308 led_exit();
5309 return rc;
5314 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5315 printk(TPACPI_NOTICE
5316 "warning: userspace override of important "
5317 "firmware LEDs is enabled\n");
5318 #endif
5319 return 0;
5322 #define str_led_status(s) \
5323 ((s) == TPACPI_LED_OFF ? "off" : \
5324 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5326 static int led_read(struct seq_file *m)
5328 if (!led_supported) {
5329 seq_printf(m, "status:\t\tnot supported\n");
5330 return 0;
5332 seq_printf(m, "status:\t\tsupported\n");
5334 if (led_supported == TPACPI_LED_570) {
5335 /* 570 */
5336 int i, status;
5337 for (i = 0; i < 8; i++) {
5338 status = led_get_status(i);
5339 if (status < 0)
5340 return -EIO;
5341 seq_printf(m, "%d:\t\t%s\n",
5342 i, str_led_status(status));
5346 seq_printf(m, "commands:\t"
5347 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5349 return 0;
5352 static int led_write(char *buf)
5354 char *cmd;
5355 int led, rc;
5356 enum led_status_t s;
5358 if (!led_supported)
5359 return -ENODEV;
5361 while ((cmd = next_cmd(&buf))) {
5362 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
5363 return -EINVAL;
5365 if (strstr(cmd, "off")) {
5366 s = TPACPI_LED_OFF;
5367 } else if (strstr(cmd, "on")) {
5368 s = TPACPI_LED_ON;
5369 } else if (strstr(cmd, "blink")) {
5370 s = TPACPI_LED_BLINK;
5371 } else {
5372 return -EINVAL;
5375 rc = led_set_status(led, s);
5376 if (rc < 0)
5377 return rc;
5380 return 0;
5383 static struct ibm_struct led_driver_data = {
5384 .name = "led",
5385 .read = led_read,
5386 .write = led_write,
5387 .exit = led_exit,
5390 /*************************************************************************
5391 * Beep subdriver
5394 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5396 #define TPACPI_BEEP_Q1 0x0001
5398 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5399 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5400 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5403 static int __init beep_init(struct ibm_init_struct *iibm)
5405 unsigned long quirks;
5407 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5409 TPACPI_ACPIHANDLE_INIT(beep);
5411 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5412 str_supported(beep_handle != NULL));
5414 quirks = tpacpi_check_quirks(beep_quirk_table,
5415 ARRAY_SIZE(beep_quirk_table));
5417 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
5419 return (beep_handle)? 0 : 1;
5422 static int beep_read(struct seq_file *m)
5424 if (!beep_handle)
5425 seq_printf(m, "status:\t\tnot supported\n");
5426 else {
5427 seq_printf(m, "status:\t\tsupported\n");
5428 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
5431 return 0;
5434 static int beep_write(char *buf)
5436 char *cmd;
5437 int beep_cmd;
5439 if (!beep_handle)
5440 return -ENODEV;
5442 while ((cmd = next_cmd(&buf))) {
5443 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5444 beep_cmd >= 0 && beep_cmd <= 17) {
5445 /* beep_cmd set */
5446 } else
5447 return -EINVAL;
5448 if (tp_features.beep_needs_two_args) {
5449 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
5450 beep_cmd, 0))
5451 return -EIO;
5452 } else {
5453 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5454 beep_cmd))
5455 return -EIO;
5459 return 0;
5462 static struct ibm_struct beep_driver_data = {
5463 .name = "beep",
5464 .read = beep_read,
5465 .write = beep_write,
5468 /*************************************************************************
5469 * Thermal subdriver
5472 enum thermal_access_mode {
5473 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5474 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5475 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5476 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5477 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5480 enum { /* TPACPI_THERMAL_TPEC_* */
5481 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5482 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5483 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5485 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
5489 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5490 struct ibm_thermal_sensors_struct {
5491 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5494 static enum thermal_access_mode thermal_read_mode;
5496 /* idx is zero-based */
5497 static int thermal_get_sensor(int idx, s32 *value)
5499 int t;
5500 s8 tmp;
5501 char tmpi[5];
5503 t = TP_EC_THERMAL_TMP0;
5505 switch (thermal_read_mode) {
5506 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5507 case TPACPI_THERMAL_TPEC_16:
5508 if (idx >= 8 && idx <= 15) {
5509 t = TP_EC_THERMAL_TMP8;
5510 idx -= 8;
5512 /* fallthrough */
5513 #endif
5514 case TPACPI_THERMAL_TPEC_8:
5515 if (idx <= 7) {
5516 if (!acpi_ec_read(t + idx, &tmp))
5517 return -EIO;
5518 *value = tmp * 1000;
5519 return 0;
5521 break;
5523 case TPACPI_THERMAL_ACPI_UPDT:
5524 if (idx <= 7) {
5525 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5526 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5527 return -EIO;
5528 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5529 return -EIO;
5530 *value = (t - 2732) * 100;
5531 return 0;
5533 break;
5535 case TPACPI_THERMAL_ACPI_TMP07:
5536 if (idx <= 7) {
5537 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5538 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5539 return -EIO;
5540 if (t > 127 || t < -127)
5541 t = TP_EC_THERMAL_TMP_NA;
5542 *value = t * 1000;
5543 return 0;
5545 break;
5547 case TPACPI_THERMAL_NONE:
5548 default:
5549 return -ENOSYS;
5552 return -EINVAL;
5555 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5557 int res, i;
5558 int n;
5560 n = 8;
5561 i = 0;
5563 if (!s)
5564 return -EINVAL;
5566 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5567 n = 16;
5569 for (i = 0 ; i < n; i++) {
5570 res = thermal_get_sensor(i, &s->temp[i]);
5571 if (res)
5572 return res;
5575 return n;
5578 static void thermal_dump_all_sensors(void)
5580 int n, i;
5581 struct ibm_thermal_sensors_struct t;
5583 n = thermal_get_sensors(&t);
5584 if (n <= 0)
5585 return;
5587 printk(TPACPI_NOTICE
5588 "temperatures (Celsius):");
5590 for (i = 0; i < n; i++) {
5591 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
5592 printk(KERN_CONT " %d", (int)(t.temp[i] / 1000));
5593 else
5594 printk(KERN_CONT " N/A");
5597 printk(KERN_CONT "\n");
5600 /* sysfs temp##_input -------------------------------------------------- */
5602 static ssize_t thermal_temp_input_show(struct device *dev,
5603 struct device_attribute *attr,
5604 char *buf)
5606 struct sensor_device_attribute *sensor_attr =
5607 to_sensor_dev_attr(attr);
5608 int idx = sensor_attr->index;
5609 s32 value;
5610 int res;
5612 res = thermal_get_sensor(idx, &value);
5613 if (res)
5614 return res;
5615 if (value == TPACPI_THERMAL_SENSOR_NA)
5616 return -ENXIO;
5618 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5621 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5622 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5623 thermal_temp_input_show, NULL, _idxB)
5625 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5626 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5627 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5628 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5629 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5630 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5631 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5632 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5633 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5634 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5635 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5636 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5637 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5638 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5639 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5640 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5641 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5644 #define THERMAL_ATTRS(X) \
5645 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5647 static struct attribute *thermal_temp_input_attr[] = {
5648 THERMAL_ATTRS(8),
5649 THERMAL_ATTRS(9),
5650 THERMAL_ATTRS(10),
5651 THERMAL_ATTRS(11),
5652 THERMAL_ATTRS(12),
5653 THERMAL_ATTRS(13),
5654 THERMAL_ATTRS(14),
5655 THERMAL_ATTRS(15),
5656 THERMAL_ATTRS(0),
5657 THERMAL_ATTRS(1),
5658 THERMAL_ATTRS(2),
5659 THERMAL_ATTRS(3),
5660 THERMAL_ATTRS(4),
5661 THERMAL_ATTRS(5),
5662 THERMAL_ATTRS(6),
5663 THERMAL_ATTRS(7),
5664 NULL
5667 static const struct attribute_group thermal_temp_input16_group = {
5668 .attrs = thermal_temp_input_attr
5671 static const struct attribute_group thermal_temp_input8_group = {
5672 .attrs = &thermal_temp_input_attr[8]
5675 #undef THERMAL_SENSOR_ATTR_TEMP
5676 #undef THERMAL_ATTRS
5678 /* --------------------------------------------------------------------- */
5680 static int __init thermal_init(struct ibm_init_struct *iibm)
5682 u8 t, ta1, ta2;
5683 int i;
5684 int acpi_tmp7;
5685 int res;
5687 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5689 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5691 if (thinkpad_id.ec_model) {
5693 * Direct EC access mode: sensors at registers
5694 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5695 * non-implemented, thermal sensors return 0x80 when
5696 * not available
5699 ta1 = ta2 = 0;
5700 for (i = 0; i < 8; i++) {
5701 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5702 ta1 |= t;
5703 } else {
5704 ta1 = 0;
5705 break;
5707 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5708 ta2 |= t;
5709 } else {
5710 ta1 = 0;
5711 break;
5714 if (ta1 == 0) {
5715 /* This is sheer paranoia, but we handle it anyway */
5716 if (acpi_tmp7) {
5717 printk(TPACPI_ERR
5718 "ThinkPad ACPI EC access misbehaving, "
5719 "falling back to ACPI TMPx access "
5720 "mode\n");
5721 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5722 } else {
5723 printk(TPACPI_ERR
5724 "ThinkPad ACPI EC access misbehaving, "
5725 "disabling thermal sensors access\n");
5726 thermal_read_mode = TPACPI_THERMAL_NONE;
5728 } else {
5729 thermal_read_mode =
5730 (ta2 != 0) ?
5731 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5733 } else if (acpi_tmp7) {
5734 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5735 /* 600e/x, 770e, 770x */
5736 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5737 } else {
5738 /* Standard ACPI TMPx access, max 8 sensors */
5739 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5741 } else {
5742 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5743 thermal_read_mode = TPACPI_THERMAL_NONE;
5746 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5747 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5748 thermal_read_mode);
5750 switch (thermal_read_mode) {
5751 case TPACPI_THERMAL_TPEC_16:
5752 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5753 &thermal_temp_input16_group);
5754 if (res)
5755 return res;
5756 break;
5757 case TPACPI_THERMAL_TPEC_8:
5758 case TPACPI_THERMAL_ACPI_TMP07:
5759 case TPACPI_THERMAL_ACPI_UPDT:
5760 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5761 &thermal_temp_input8_group);
5762 if (res)
5763 return res;
5764 break;
5765 case TPACPI_THERMAL_NONE:
5766 default:
5767 return 1;
5770 return 0;
5773 static void thermal_exit(void)
5775 switch (thermal_read_mode) {
5776 case TPACPI_THERMAL_TPEC_16:
5777 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5778 &thermal_temp_input16_group);
5779 break;
5780 case TPACPI_THERMAL_TPEC_8:
5781 case TPACPI_THERMAL_ACPI_TMP07:
5782 case TPACPI_THERMAL_ACPI_UPDT:
5783 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5784 &thermal_temp_input8_group);
5785 break;
5786 case TPACPI_THERMAL_NONE:
5787 default:
5788 break;
5792 static int thermal_read(struct seq_file *m)
5794 int n, i;
5795 struct ibm_thermal_sensors_struct t;
5797 n = thermal_get_sensors(&t);
5798 if (unlikely(n < 0))
5799 return n;
5801 seq_printf(m, "temperatures:\t");
5803 if (n > 0) {
5804 for (i = 0; i < (n - 1); i++)
5805 seq_printf(m, "%d ", t.temp[i] / 1000);
5806 seq_printf(m, "%d\n", t.temp[i] / 1000);
5807 } else
5808 seq_printf(m, "not supported\n");
5810 return 0;
5813 static struct ibm_struct thermal_driver_data = {
5814 .name = "thermal",
5815 .read = thermal_read,
5816 .exit = thermal_exit,
5819 /*************************************************************************
5820 * EC Dump subdriver
5823 static u8 ecdump_regs[256];
5825 static int ecdump_read(struct seq_file *m)
5827 int i, j;
5828 u8 v;
5830 seq_printf(m, "EC "
5831 " +00 +01 +02 +03 +04 +05 +06 +07"
5832 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5833 for (i = 0; i < 256; i += 16) {
5834 seq_printf(m, "EC 0x%02x:", i);
5835 for (j = 0; j < 16; j++) {
5836 if (!acpi_ec_read(i + j, &v))
5837 break;
5838 if (v != ecdump_regs[i + j])
5839 seq_printf(m, " *%02x", v);
5840 else
5841 seq_printf(m, " %02x", v);
5842 ecdump_regs[i + j] = v;
5844 seq_putc(m, '\n');
5845 if (j != 16)
5846 break;
5849 /* These are way too dangerous to advertise openly... */
5850 #if 0
5851 seq_printf(m, "commands:\t0x<offset> 0x<value>"
5852 " (<offset> is 00-ff, <value> is 00-ff)\n");
5853 seq_printf(m, "commands:\t0x<offset> <value> "
5854 " (<offset> is 00-ff, <value> is 0-255)\n");
5855 #endif
5856 return 0;
5859 static int ecdump_write(char *buf)
5861 char *cmd;
5862 int i, v;
5864 while ((cmd = next_cmd(&buf))) {
5865 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5866 /* i and v set */
5867 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5868 /* i and v set */
5869 } else
5870 return -EINVAL;
5871 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5872 if (!acpi_ec_write(i, v))
5873 return -EIO;
5874 } else
5875 return -EINVAL;
5878 return 0;
5881 static struct ibm_struct ecdump_driver_data = {
5882 .name = "ecdump",
5883 .read = ecdump_read,
5884 .write = ecdump_write,
5885 .flags.experimental = 1,
5888 /*************************************************************************
5889 * Backlight/brightness subdriver
5892 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5895 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5896 * CMOS NVRAM byte 0x5E, bits 0-3.
5898 * EC HBRV (0x31) has the following layout
5899 * Bit 7: unknown function
5900 * Bit 6: unknown function
5901 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5902 * Bit 4: must be set to zero to avoid problems
5903 * Bit 3-0: backlight brightness level
5905 * brightness_get_raw returns status data in the HBRV layout
5907 * WARNING: The X61 has been verified to use HBRV for something else, so
5908 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5909 * testing on the very early *60 Lenovo models...
5912 enum {
5913 TP_EC_BACKLIGHT = 0x31,
5915 /* TP_EC_BACKLIGHT bitmasks */
5916 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5917 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5918 TP_EC_BACKLIGHT_MAPSW = 0x20,
5921 enum tpacpi_brightness_access_mode {
5922 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5923 TPACPI_BRGHT_MODE_EC, /* EC control */
5924 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5925 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5926 TPACPI_BRGHT_MODE_MAX
5929 static struct backlight_device *ibm_backlight_device;
5931 static enum tpacpi_brightness_access_mode brightness_mode =
5932 TPACPI_BRGHT_MODE_MAX;
5934 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5936 static struct mutex brightness_mutex;
5938 /* NVRAM brightness access,
5939 * call with brightness_mutex held! */
5940 static unsigned int tpacpi_brightness_nvram_get(void)
5942 u8 lnvram;
5944 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5945 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5946 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5947 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5949 return lnvram;
5952 static void tpacpi_brightness_checkpoint_nvram(void)
5954 u8 lec = 0;
5955 u8 b_nvram;
5957 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5958 return;
5960 vdbg_printk(TPACPI_DBG_BRGHT,
5961 "trying to checkpoint backlight level to NVRAM...\n");
5963 if (mutex_lock_killable(&brightness_mutex) < 0)
5964 return;
5966 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5967 goto unlock;
5968 lec &= TP_EC_BACKLIGHT_LVLMSK;
5969 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5971 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5972 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5973 /* NVRAM needs update */
5974 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5975 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5976 b_nvram |= lec;
5977 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5978 dbg_printk(TPACPI_DBG_BRGHT,
5979 "updated NVRAM backlight level to %u (0x%02x)\n",
5980 (unsigned int) lec, (unsigned int) b_nvram);
5981 } else
5982 vdbg_printk(TPACPI_DBG_BRGHT,
5983 "NVRAM backlight level already is %u (0x%02x)\n",
5984 (unsigned int) lec, (unsigned int) b_nvram);
5986 unlock:
5987 mutex_unlock(&brightness_mutex);
5991 /* call with brightness_mutex held! */
5992 static int tpacpi_brightness_get_raw(int *status)
5994 u8 lec = 0;
5996 switch (brightness_mode) {
5997 case TPACPI_BRGHT_MODE_UCMS_STEP:
5998 *status = tpacpi_brightness_nvram_get();
5999 return 0;
6000 case TPACPI_BRGHT_MODE_EC:
6001 case TPACPI_BRGHT_MODE_ECNVRAM:
6002 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6003 return -EIO;
6004 *status = lec;
6005 return 0;
6006 default:
6007 return -ENXIO;
6011 /* call with brightness_mutex held! */
6012 /* do NOT call with illegal backlight level value */
6013 static int tpacpi_brightness_set_ec(unsigned int value)
6015 u8 lec = 0;
6017 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6018 return -EIO;
6020 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6021 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6022 (value & TP_EC_BACKLIGHT_LVLMSK))))
6023 return -EIO;
6025 return 0;
6028 /* call with brightness_mutex held! */
6029 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6031 int cmos_cmd, inc;
6032 unsigned int current_value, i;
6034 current_value = tpacpi_brightness_nvram_get();
6036 if (value == current_value)
6037 return 0;
6039 cmos_cmd = (value > current_value) ?
6040 TP_CMOS_BRIGHTNESS_UP :
6041 TP_CMOS_BRIGHTNESS_DOWN;
6042 inc = (value > current_value) ? 1 : -1;
6044 for (i = current_value; i != value; i += inc)
6045 if (issue_thinkpad_cmos_command(cmos_cmd))
6046 return -EIO;
6048 return 0;
6051 /* May return EINTR which can always be mapped to ERESTARTSYS */
6052 static int brightness_set(unsigned int value)
6054 int res;
6056 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
6057 value < 0)
6058 return -EINVAL;
6060 vdbg_printk(TPACPI_DBG_BRGHT,
6061 "set backlight level to %d\n", value);
6063 res = mutex_lock_killable(&brightness_mutex);
6064 if (res < 0)
6065 return res;
6067 switch (brightness_mode) {
6068 case TPACPI_BRGHT_MODE_EC:
6069 case TPACPI_BRGHT_MODE_ECNVRAM:
6070 res = tpacpi_brightness_set_ec(value);
6071 break;
6072 case TPACPI_BRGHT_MODE_UCMS_STEP:
6073 res = tpacpi_brightness_set_ucmsstep(value);
6074 break;
6075 default:
6076 res = -ENXIO;
6079 mutex_unlock(&brightness_mutex);
6080 return res;
6083 /* sysfs backlight class ----------------------------------------------- */
6085 static int brightness_update_status(struct backlight_device *bd)
6087 unsigned int level =
6088 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6089 bd->props.power == FB_BLANK_UNBLANK) ?
6090 bd->props.brightness : 0;
6092 dbg_printk(TPACPI_DBG_BRGHT,
6093 "backlight: attempt to set level to %d\n",
6094 level);
6096 /* it is the backlight class's job (caller) to handle
6097 * EINTR and other errors properly */
6098 return brightness_set(level);
6101 static int brightness_get(struct backlight_device *bd)
6103 int status, res;
6105 res = mutex_lock_killable(&brightness_mutex);
6106 if (res < 0)
6107 return 0;
6109 res = tpacpi_brightness_get_raw(&status);
6111 mutex_unlock(&brightness_mutex);
6113 if (res < 0)
6114 return 0;
6116 return status & TP_EC_BACKLIGHT_LVLMSK;
6119 static void tpacpi_brightness_notify_change(void)
6121 backlight_force_update(ibm_backlight_device,
6122 BACKLIGHT_UPDATE_HOTKEY);
6125 static struct backlight_ops ibm_backlight_data = {
6126 .get_brightness = brightness_get,
6127 .update_status = brightness_update_status,
6130 /* --------------------------------------------------------------------- */
6133 * These are only useful for models that have only one possibility
6134 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6135 * these quirks.
6137 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6138 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6139 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6141 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6142 /* Models with ATI GPUs known to require ECNVRAM mode */
6143 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6145 /* Models with ATI GPUs that can use ECNVRAM */
6146 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6147 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6148 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6149 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6151 /* Models with Intel Extreme Graphics 2 */
6152 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6153 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6154 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6156 /* Models with Intel GMA900 */
6157 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6158 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6159 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6162 static int __init brightness_init(struct ibm_init_struct *iibm)
6164 int b;
6165 unsigned long quirks;
6167 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6169 mutex_init(&brightness_mutex);
6171 quirks = tpacpi_check_quirks(brightness_quirk_table,
6172 ARRAY_SIZE(brightness_quirk_table));
6175 * We always attempt to detect acpi support, so as to switch
6176 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6177 * going to publish a backlight interface
6179 b = tpacpi_check_std_acpi_brightness_support();
6180 if (b > 0) {
6182 if (acpi_video_backlight_support()) {
6183 if (brightness_enable > 1) {
6184 printk(TPACPI_NOTICE
6185 "Standard ACPI backlight interface "
6186 "available, not loading native one.\n");
6187 return 1;
6188 } else if (brightness_enable == 1) {
6189 printk(TPACPI_NOTICE
6190 "Backlight control force enabled, even if standard "
6191 "ACPI backlight interface is available\n");
6193 } else {
6194 if (brightness_enable > 1) {
6195 printk(TPACPI_NOTICE
6196 "Standard ACPI backlight interface not "
6197 "available, thinkpad_acpi native "
6198 "brightness control enabled\n");
6203 if (!brightness_enable) {
6204 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6205 "brightness support disabled by "
6206 "module parameter\n");
6207 return 1;
6210 if (b > 16) {
6211 printk(TPACPI_ERR
6212 "Unsupported brightness interface, "
6213 "please contact %s\n", TPACPI_MAIL);
6214 return 1;
6216 if (b == 16)
6217 tp_features.bright_16levels = 1;
6220 * Check for module parameter bogosity, note that we
6221 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6222 * able to detect "unspecified"
6224 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6225 return -EINVAL;
6227 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6228 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6229 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6230 if (quirks & TPACPI_BRGHT_Q_EC)
6231 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6232 else
6233 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6235 dbg_printk(TPACPI_DBG_BRGHT,
6236 "driver auto-selected brightness_mode=%d\n",
6237 brightness_mode);
6240 /* Safety */
6241 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
6242 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6243 brightness_mode == TPACPI_BRGHT_MODE_EC))
6244 return -EINVAL;
6246 if (tpacpi_brightness_get_raw(&b) < 0)
6247 return 1;
6249 if (tp_features.bright_16levels)
6250 printk(TPACPI_INFO
6251 "detected a 16-level brightness capable ThinkPad\n");
6253 ibm_backlight_device = backlight_device_register(
6254 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
6255 &ibm_backlight_data);
6256 if (IS_ERR(ibm_backlight_device)) {
6257 int rc = PTR_ERR(ibm_backlight_device);
6258 ibm_backlight_device = NULL;
6259 printk(TPACPI_ERR "Could not register backlight device\n");
6260 return rc;
6262 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6263 "brightness is supported\n");
6265 if (quirks & TPACPI_BRGHT_Q_ASK) {
6266 printk(TPACPI_NOTICE
6267 "brightness: will use unverified default: "
6268 "brightness_mode=%d\n", brightness_mode);
6269 printk(TPACPI_NOTICE
6270 "brightness: please report to %s whether it works well "
6271 "or not on your ThinkPad\n", TPACPI_MAIL);
6274 ibm_backlight_device->props.max_brightness =
6275 (tp_features.bright_16levels)? 15 : 7;
6276 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6277 backlight_update_status(ibm_backlight_device);
6279 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6280 "brightness: registering brightness hotkeys "
6281 "as change notification\n");
6282 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6283 | TP_ACPI_HKEY_BRGHTUP_MASK
6284 | TP_ACPI_HKEY_BRGHTDWN_MASK);;
6285 return 0;
6288 static void brightness_suspend(pm_message_t state)
6290 tpacpi_brightness_checkpoint_nvram();
6293 static void brightness_shutdown(void)
6295 tpacpi_brightness_checkpoint_nvram();
6298 static void brightness_exit(void)
6300 if (ibm_backlight_device) {
6301 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6302 "calling backlight_device_unregister()\n");
6303 backlight_device_unregister(ibm_backlight_device);
6306 tpacpi_brightness_checkpoint_nvram();
6309 static int brightness_read(struct seq_file *m)
6311 int level;
6313 level = brightness_get(NULL);
6314 if (level < 0) {
6315 seq_printf(m, "level:\t\tunreadable\n");
6316 } else {
6317 seq_printf(m, "level:\t\t%d\n", level);
6318 seq_printf(m, "commands:\tup, down\n");
6319 seq_printf(m, "commands:\tlevel <level>"
6320 " (<level> is 0-%d)\n",
6321 (tp_features.bright_16levels) ? 15 : 7);
6324 return 0;
6327 static int brightness_write(char *buf)
6329 int level;
6330 int rc;
6331 char *cmd;
6332 int max_level = (tp_features.bright_16levels) ? 15 : 7;
6334 level = brightness_get(NULL);
6335 if (level < 0)
6336 return level;
6338 while ((cmd = next_cmd(&buf))) {
6339 if (strlencmp(cmd, "up") == 0) {
6340 if (level < max_level)
6341 level++;
6342 } else if (strlencmp(cmd, "down") == 0) {
6343 if (level > 0)
6344 level--;
6345 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6346 level >= 0 && level <= max_level) {
6347 /* new level set */
6348 } else
6349 return -EINVAL;
6352 tpacpi_disclose_usertask("procfs brightness",
6353 "set level to %d\n", level);
6356 * Now we know what the final level should be, so we try to set it.
6357 * Doing it this way makes the syscall restartable in case of EINTR
6359 rc = brightness_set(level);
6360 if (!rc && ibm_backlight_device)
6361 backlight_force_update(ibm_backlight_device,
6362 BACKLIGHT_UPDATE_SYSFS);
6363 return (rc == -EINTR)? -ERESTARTSYS : rc;
6366 static struct ibm_struct brightness_driver_data = {
6367 .name = "brightness",
6368 .read = brightness_read,
6369 .write = brightness_write,
6370 .exit = brightness_exit,
6371 .suspend = brightness_suspend,
6372 .shutdown = brightness_shutdown,
6375 /*************************************************************************
6376 * Volume subdriver
6380 * IBM ThinkPads have a simple volume controller with MUTE gating.
6381 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6383 * Since the *61 series (and probably also the later *60 series), Lenovo
6384 * ThinkPads only implement the MUTE gate.
6386 * EC register 0x30
6387 * Bit 6: MUTE (1 mutes sound)
6388 * Bit 3-0: Volume
6389 * Other bits should be zero as far as we know.
6391 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6392 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6393 * such as bit 7 which is used to detect repeated presses of MUTE,
6394 * and we leave them unchanged.
6397 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6399 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6400 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6401 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
6403 static int alsa_index = ~((1 << (SNDRV_CARDS - 3)) - 1); /* last three slots */
6404 static char *alsa_id = "ThinkPadEC";
6405 static int alsa_enable = SNDRV_DEFAULT_ENABLE1;
6407 struct tpacpi_alsa_data {
6408 struct snd_card *card;
6409 struct snd_ctl_elem_id *ctl_mute_id;
6410 struct snd_ctl_elem_id *ctl_vol_id;
6413 static struct snd_card *alsa_card;
6415 enum {
6416 TP_EC_AUDIO = 0x30,
6418 /* TP_EC_AUDIO bits */
6419 TP_EC_AUDIO_MUTESW = 6,
6421 /* TP_EC_AUDIO bitmasks */
6422 TP_EC_AUDIO_LVL_MSK = 0x0F,
6423 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
6425 /* Maximum volume */
6426 TP_EC_VOLUME_MAX = 14,
6429 enum tpacpi_volume_access_mode {
6430 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
6431 TPACPI_VOL_MODE_EC, /* Pure EC control */
6432 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
6433 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6434 TPACPI_VOL_MODE_MAX
6437 enum tpacpi_volume_capabilities {
6438 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
6439 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
6440 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
6441 TPACPI_VOL_CAP_MAX
6444 static enum tpacpi_volume_access_mode volume_mode =
6445 TPACPI_VOL_MODE_MAX;
6447 static enum tpacpi_volume_capabilities volume_capabilities;
6448 static int volume_control_allowed;
6451 * Used to syncronize writers to TP_EC_AUDIO and
6452 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
6454 static struct mutex volume_mutex;
6456 static void tpacpi_volume_checkpoint_nvram(void)
6458 u8 lec = 0;
6459 u8 b_nvram;
6460 u8 ec_mask;
6462 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
6463 return;
6464 if (!volume_control_allowed)
6465 return;
6467 vdbg_printk(TPACPI_DBG_MIXER,
6468 "trying to checkpoint mixer state to NVRAM...\n");
6470 if (tp_features.mixer_no_level_control)
6471 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
6472 else
6473 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
6475 if (mutex_lock_killable(&volume_mutex) < 0)
6476 return;
6478 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
6479 goto unlock;
6480 lec &= ec_mask;
6481 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
6483 if (lec != (b_nvram & ec_mask)) {
6484 /* NVRAM needs update */
6485 b_nvram &= ~ec_mask;
6486 b_nvram |= lec;
6487 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
6488 dbg_printk(TPACPI_DBG_MIXER,
6489 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
6490 (unsigned int) lec, (unsigned int) b_nvram);
6491 } else {
6492 vdbg_printk(TPACPI_DBG_MIXER,
6493 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
6494 (unsigned int) lec, (unsigned int) b_nvram);
6497 unlock:
6498 mutex_unlock(&volume_mutex);
6501 static int volume_get_status_ec(u8 *status)
6503 u8 s;
6505 if (!acpi_ec_read(TP_EC_AUDIO, &s))
6506 return -EIO;
6508 *status = s;
6510 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
6512 return 0;
6515 static int volume_get_status(u8 *status)
6517 return volume_get_status_ec(status);
6520 static int volume_set_status_ec(const u8 status)
6522 if (!acpi_ec_write(TP_EC_AUDIO, status))
6523 return -EIO;
6525 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
6527 return 0;
6530 static int volume_set_status(const u8 status)
6532 return volume_set_status_ec(status);
6535 /* returns < 0 on error, 0 on no change, 1 on change */
6536 static int __volume_set_mute_ec(const bool mute)
6538 int rc;
6539 u8 s, n;
6541 if (mutex_lock_killable(&volume_mutex) < 0)
6542 return -EINTR;
6544 rc = volume_get_status_ec(&s);
6545 if (rc)
6546 goto unlock;
6548 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
6549 s & ~TP_EC_AUDIO_MUTESW_MSK;
6551 if (n != s) {
6552 rc = volume_set_status_ec(n);
6553 if (!rc)
6554 rc = 1;
6557 unlock:
6558 mutex_unlock(&volume_mutex);
6559 return rc;
6562 static int volume_alsa_set_mute(const bool mute)
6564 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
6565 (mute) ? "" : "un");
6566 return __volume_set_mute_ec(mute);
6569 static int volume_set_mute(const bool mute)
6571 int rc;
6573 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
6574 (mute) ? "" : "un");
6576 rc = __volume_set_mute_ec(mute);
6577 return (rc < 0) ? rc : 0;
6580 /* returns < 0 on error, 0 on no change, 1 on change */
6581 static int __volume_set_volume_ec(const u8 vol)
6583 int rc;
6584 u8 s, n;
6586 if (vol > TP_EC_VOLUME_MAX)
6587 return -EINVAL;
6589 if (mutex_lock_killable(&volume_mutex) < 0)
6590 return -EINTR;
6592 rc = volume_get_status_ec(&s);
6593 if (rc)
6594 goto unlock;
6596 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
6598 if (n != s) {
6599 rc = volume_set_status_ec(n);
6600 if (!rc)
6601 rc = 1;
6604 unlock:
6605 mutex_unlock(&volume_mutex);
6606 return rc;
6609 static int volume_alsa_set_volume(const u8 vol)
6611 dbg_printk(TPACPI_DBG_MIXER,
6612 "ALSA: trying to set volume level to %hu\n", vol);
6613 return __volume_set_volume_ec(vol);
6616 static void volume_alsa_notify_change(void)
6618 struct tpacpi_alsa_data *d;
6620 if (alsa_card && alsa_card->private_data) {
6621 d = alsa_card->private_data;
6622 if (d->ctl_mute_id)
6623 snd_ctl_notify(alsa_card,
6624 SNDRV_CTL_EVENT_MASK_VALUE,
6625 d->ctl_mute_id);
6626 if (d->ctl_vol_id)
6627 snd_ctl_notify(alsa_card,
6628 SNDRV_CTL_EVENT_MASK_VALUE,
6629 d->ctl_vol_id);
6633 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
6634 struct snd_ctl_elem_info *uinfo)
6636 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6637 uinfo->count = 1;
6638 uinfo->value.integer.min = 0;
6639 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
6640 return 0;
6643 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
6644 struct snd_ctl_elem_value *ucontrol)
6646 u8 s;
6647 int rc;
6649 rc = volume_get_status(&s);
6650 if (rc < 0)
6651 return rc;
6653 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
6654 return 0;
6657 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
6658 struct snd_ctl_elem_value *ucontrol)
6660 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
6663 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
6665 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
6666 struct snd_ctl_elem_value *ucontrol)
6668 u8 s;
6669 int rc;
6671 rc = volume_get_status(&s);
6672 if (rc < 0)
6673 return rc;
6675 ucontrol->value.integer.value[0] =
6676 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
6677 return 0;
6680 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
6681 struct snd_ctl_elem_value *ucontrol)
6683 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
6686 static struct snd_kcontrol_new volume_alsa_control_vol __devinitdata = {
6687 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6688 .name = "Console Playback Volume",
6689 .index = 0,
6690 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6691 .info = volume_alsa_vol_info,
6692 .get = volume_alsa_vol_get,
6695 static struct snd_kcontrol_new volume_alsa_control_mute __devinitdata = {
6696 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6697 .name = "Console Playback Switch",
6698 .index = 0,
6699 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6700 .info = volume_alsa_mute_info,
6701 .get = volume_alsa_mute_get,
6704 static void volume_suspend(pm_message_t state)
6706 tpacpi_volume_checkpoint_nvram();
6709 static void volume_resume(void)
6711 volume_alsa_notify_change();
6714 static void volume_shutdown(void)
6716 tpacpi_volume_checkpoint_nvram();
6719 static void volume_exit(void)
6721 if (alsa_card) {
6722 snd_card_free(alsa_card);
6723 alsa_card = NULL;
6726 tpacpi_volume_checkpoint_nvram();
6729 static int __init volume_create_alsa_mixer(void)
6731 struct snd_card *card;
6732 struct tpacpi_alsa_data *data;
6733 struct snd_kcontrol *ctl_vol;
6734 struct snd_kcontrol *ctl_mute;
6735 int rc;
6737 rc = snd_card_create(alsa_index, alsa_id, THIS_MODULE,
6738 sizeof(struct tpacpi_alsa_data), &card);
6739 if (rc < 0 || !card) {
6740 printk(TPACPI_ERR
6741 "Failed to create ALSA card structures: %d\n", rc);
6742 return 1;
6745 BUG_ON(!card->private_data);
6746 data = card->private_data;
6747 data->card = card;
6749 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
6750 sizeof(card->driver));
6751 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
6752 sizeof(card->shortname));
6753 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
6754 (thinkpad_id.ec_version_str) ?
6755 thinkpad_id.ec_version_str : "(unknown)");
6756 snprintf(card->longname, sizeof(card->longname),
6757 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
6758 (thinkpad_id.ec_version_str) ?
6759 thinkpad_id.ec_version_str : "unknown");
6761 if (volume_control_allowed) {
6762 volume_alsa_control_vol.put = volume_alsa_vol_put;
6763 volume_alsa_control_vol.access =
6764 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6766 volume_alsa_control_mute.put = volume_alsa_mute_put;
6767 volume_alsa_control_mute.access =
6768 SNDRV_CTL_ELEM_ACCESS_READWRITE;
6771 if (!tp_features.mixer_no_level_control) {
6772 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
6773 rc = snd_ctl_add(card, ctl_vol);
6774 if (rc < 0) {
6775 printk(TPACPI_ERR
6776 "Failed to create ALSA volume control: %d\n",
6777 rc);
6778 goto err_exit;
6780 data->ctl_vol_id = &ctl_vol->id;
6783 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
6784 rc = snd_ctl_add(card, ctl_mute);
6785 if (rc < 0) {
6786 printk(TPACPI_ERR "Failed to create ALSA mute control: %d\n",
6787 rc);
6788 goto err_exit;
6790 data->ctl_mute_id = &ctl_mute->id;
6792 snd_card_set_dev(card, &tpacpi_pdev->dev);
6793 rc = snd_card_register(card);
6794 if (rc < 0) {
6795 printk(TPACPI_ERR "Failed to register ALSA card: %d\n", rc);
6796 goto err_exit;
6799 alsa_card = card;
6800 return 0;
6802 err_exit:
6803 snd_card_free(card);
6804 return 1;
6807 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
6808 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
6810 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
6811 /* Whitelist volume level on all IBM by default */
6812 { .vendor = PCI_VENDOR_ID_IBM,
6813 .bios = TPACPI_MATCH_ANY,
6814 .ec = TPACPI_MATCH_ANY,
6815 .quirks = TPACPI_VOL_Q_LEVEL },
6817 /* Lenovo models with volume control (needs confirmation) */
6818 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
6819 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
6820 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
6821 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
6822 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
6823 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
6824 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
6826 /* Whitelist mute-only on all Lenovo by default */
6827 { .vendor = PCI_VENDOR_ID_LENOVO,
6828 .bios = TPACPI_MATCH_ANY,
6829 .ec = TPACPI_MATCH_ANY,
6830 .quirks = TPACPI_VOL_Q_MUTEONLY }
6833 static int __init volume_init(struct ibm_init_struct *iibm)
6835 unsigned long quirks;
6836 int rc;
6838 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
6840 mutex_init(&volume_mutex);
6843 * Check for module parameter bogosity, note that we
6844 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
6845 * able to detect "unspecified"
6847 if (volume_mode > TPACPI_VOL_MODE_MAX)
6848 return -EINVAL;
6850 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
6851 printk(TPACPI_ERR
6852 "UCMS step volume mode not implemented, "
6853 "please contact %s\n", TPACPI_MAIL);
6854 return 1;
6857 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
6858 return -EINVAL;
6861 * The ALSA mixer is our primary interface.
6862 * When disabled, don't install the subdriver at all
6864 if (!alsa_enable) {
6865 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6866 "ALSA mixer disabled by parameter, "
6867 "not loading volume subdriver...\n");
6868 return 1;
6871 quirks = tpacpi_check_quirks(volume_quirk_table,
6872 ARRAY_SIZE(volume_quirk_table));
6874 switch (volume_capabilities) {
6875 case TPACPI_VOL_CAP_AUTO:
6876 if (quirks & TPACPI_VOL_Q_MUTEONLY)
6877 tp_features.mixer_no_level_control = 1;
6878 else if (quirks & TPACPI_VOL_Q_LEVEL)
6879 tp_features.mixer_no_level_control = 0;
6880 else
6881 return 1; /* no mixer */
6882 break;
6883 case TPACPI_VOL_CAP_VOLMUTE:
6884 tp_features.mixer_no_level_control = 0;
6885 break;
6886 case TPACPI_VOL_CAP_MUTEONLY:
6887 tp_features.mixer_no_level_control = 1;
6888 break;
6889 default:
6890 return 1;
6893 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
6894 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6895 "using user-supplied volume_capabilities=%d\n",
6896 volume_capabilities);
6898 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
6899 volume_mode == TPACPI_VOL_MODE_MAX) {
6900 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
6902 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6903 "driver auto-selected volume_mode=%d\n",
6904 volume_mode);
6905 } else {
6906 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6907 "using user-supplied volume_mode=%d\n",
6908 volume_mode);
6911 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6912 "mute is supported, volume control is %s\n",
6913 str_supported(!tp_features.mixer_no_level_control));
6915 rc = volume_create_alsa_mixer();
6916 if (rc) {
6917 printk(TPACPI_ERR
6918 "Could not create the ALSA mixer interface\n");
6919 return rc;
6922 printk(TPACPI_INFO
6923 "Console audio control enabled, mode: %s\n",
6924 (volume_control_allowed) ?
6925 "override (read/write)" :
6926 "monitor (read only)");
6928 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
6929 "registering volume hotkeys as change notification\n");
6930 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6931 | TP_ACPI_HKEY_VOLUP_MASK
6932 | TP_ACPI_HKEY_VOLDWN_MASK
6933 | TP_ACPI_HKEY_MUTE_MASK);
6935 return 0;
6938 static int volume_read(struct seq_file *m)
6940 u8 status;
6942 if (volume_get_status(&status) < 0) {
6943 seq_printf(m, "level:\t\tunreadable\n");
6944 } else {
6945 if (tp_features.mixer_no_level_control)
6946 seq_printf(m, "level:\t\tunsupported\n");
6947 else
6948 seq_printf(m, "level:\t\t%d\n",
6949 status & TP_EC_AUDIO_LVL_MSK);
6951 seq_printf(m, "mute:\t\t%s\n",
6952 onoff(status, TP_EC_AUDIO_MUTESW));
6954 if (volume_control_allowed) {
6955 seq_printf(m, "commands:\tunmute, mute\n");
6956 if (!tp_features.mixer_no_level_control) {
6957 seq_printf(m,
6958 "commands:\tup, down\n");
6959 seq_printf(m,
6960 "commands:\tlevel <level>"
6961 " (<level> is 0-%d)\n",
6962 TP_EC_VOLUME_MAX);
6967 return 0;
6970 static int volume_write(char *buf)
6972 u8 s;
6973 u8 new_level, new_mute;
6974 int l;
6975 char *cmd;
6976 int rc;
6979 * We do allow volume control at driver startup, so that the
6980 * user can set initial state through the volume=... parameter hack.
6982 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
6983 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
6984 tp_warned.volume_ctrl_forbidden = 1;
6985 printk(TPACPI_NOTICE
6986 "Console audio control in monitor mode, "
6987 "changes are not allowed.\n");
6988 printk(TPACPI_NOTICE
6989 "Use the volume_control=1 module parameter "
6990 "to enable volume control\n");
6992 return -EPERM;
6995 rc = volume_get_status(&s);
6996 if (rc < 0)
6997 return rc;
6999 new_level = s & TP_EC_AUDIO_LVL_MSK;
7000 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7002 while ((cmd = next_cmd(&buf))) {
7003 if (!tp_features.mixer_no_level_control) {
7004 if (strlencmp(cmd, "up") == 0) {
7005 if (new_mute)
7006 new_mute = 0;
7007 else if (new_level < TP_EC_VOLUME_MAX)
7008 new_level++;
7009 continue;
7010 } else if (strlencmp(cmd, "down") == 0) {
7011 if (new_mute)
7012 new_mute = 0;
7013 else if (new_level > 0)
7014 new_level--;
7015 continue;
7016 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7017 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7018 new_level = l;
7019 continue;
7022 if (strlencmp(cmd, "mute") == 0)
7023 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7024 else if (strlencmp(cmd, "unmute") == 0)
7025 new_mute = 0;
7026 else
7027 return -EINVAL;
7030 if (tp_features.mixer_no_level_control) {
7031 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7032 new_mute ? "" : "un");
7033 rc = volume_set_mute(!!new_mute);
7034 } else {
7035 tpacpi_disclose_usertask("procfs volume",
7036 "%smute and set level to %d\n",
7037 new_mute ? "" : "un", new_level);
7038 rc = volume_set_status(new_mute | new_level);
7040 volume_alsa_notify_change();
7042 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7045 static struct ibm_struct volume_driver_data = {
7046 .name = "volume",
7047 .read = volume_read,
7048 .write = volume_write,
7049 .exit = volume_exit,
7050 .suspend = volume_suspend,
7051 .resume = volume_resume,
7052 .shutdown = volume_shutdown,
7055 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7057 #define alsa_card NULL
7059 static void inline volume_alsa_notify_change(void)
7063 static int __init volume_init(struct ibm_init_struct *iibm)
7065 printk(TPACPI_INFO
7066 "volume: disabled as there is no ALSA support in this kernel\n");
7068 return 1;
7071 static struct ibm_struct volume_driver_data = {
7072 .name = "volume",
7075 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7077 /*************************************************************************
7078 * Fan subdriver
7082 * FAN ACCESS MODES
7084 * TPACPI_FAN_RD_ACPI_GFAN:
7085 * ACPI GFAN method: returns fan level
7087 * see TPACPI_FAN_WR_ACPI_SFAN
7088 * EC 0x2f (HFSP) not available if GFAN exists
7090 * TPACPI_FAN_WR_ACPI_SFAN:
7091 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7093 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7094 * it for writing.
7096 * TPACPI_FAN_WR_TPEC:
7097 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7098 * Supported on almost all ThinkPads
7100 * Fan speed changes of any sort (including those caused by the
7101 * disengaged mode) are usually done slowly by the firmware as the
7102 * maximum ammount of fan duty cycle change per second seems to be
7103 * limited.
7105 * Reading is not available if GFAN exists.
7106 * Writing is not available if SFAN exists.
7108 * Bits
7109 * 7 automatic mode engaged;
7110 * (default operation mode of the ThinkPad)
7111 * fan level is ignored in this mode.
7112 * 6 full speed mode (takes precedence over bit 7);
7113 * not available on all thinkpads. May disable
7114 * the tachometer while the fan controller ramps up
7115 * the speed (which can take up to a few *minutes*).
7116 * Speeds up fan to 100% duty-cycle, which is far above
7117 * the standard RPM levels. It is not impossible that
7118 * it could cause hardware damage.
7119 * 5-3 unused in some models. Extra bits for fan level
7120 * in others, but still useless as all values above
7121 * 7 map to the same speed as level 7 in these models.
7122 * 2-0 fan level (0..7 usually)
7123 * 0x00 = stop
7124 * 0x07 = max (set when temperatures critical)
7125 * Some ThinkPads may have other levels, see
7126 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7128 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7129 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
7130 * does so, its initial value is meaningless (0x07).
7132 * For firmware bugs, refer to:
7133 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7135 * ----
7137 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7138 * Main fan tachometer reading (in RPM)
7140 * This register is present on all ThinkPads with a new-style EC, and
7141 * it is known not to be present on the A21m/e, and T22, as there is
7142 * something else in offset 0x84 according to the ACPI DSDT. Other
7143 * ThinkPads from this same time period (and earlier) probably lack the
7144 * tachometer as well.
7146 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7147 * was never fixed by IBM to report the EC firmware version string
7148 * probably support the tachometer (like the early X models), so
7149 * detecting it is quite hard. We need more data to know for sure.
7151 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7152 * might result.
7154 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7155 * mode.
7157 * For firmware bugs, refer to:
7158 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7160 * ----
7162 * ThinkPad EC register 0x31 bit 0 (only on select models)
7164 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7165 * show the speed of the main fan. When bit 0 of EC register 0x31
7166 * is one, the tachometer registers show the speed of the auxiliary
7167 * fan.
7169 * Fan control seems to affect both fans, regardless of the state
7170 * of this bit.
7172 * So far, only the firmware for the X60/X61 non-tablet versions
7173 * seem to support this (firmware TP-7M).
7175 * TPACPI_FAN_WR_ACPI_FANS:
7176 * ThinkPad X31, X40, X41. Not available in the X60.
7178 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7179 * high speed. ACPI DSDT seems to map these three speeds to levels
7180 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7181 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7183 * The speeds are stored on handles
7184 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7186 * There are three default speed sets, accessible as handles:
7187 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7189 * ACPI DSDT switches which set is in use depending on various
7190 * factors.
7192 * TPACPI_FAN_WR_TPEC is also available and should be used to
7193 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7194 * but the ACPI tables just mention level 7.
7197 enum { /* Fan control constants */
7198 fan_status_offset = 0x2f, /* EC register 0x2f */
7199 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7200 * 0x84 must be read before 0x85 */
7201 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7202 bit 0 selects which fan is active */
7204 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7205 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7207 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7210 enum fan_status_access_mode {
7211 TPACPI_FAN_NONE = 0, /* No fan status or control */
7212 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7213 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7216 enum fan_control_access_mode {
7217 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7218 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7219 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7220 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7223 enum fan_control_commands {
7224 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7225 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7226 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7227 * and also watchdog cmd */
7230 static int fan_control_allowed;
7232 static enum fan_status_access_mode fan_status_access_mode;
7233 static enum fan_control_access_mode fan_control_access_mode;
7234 static enum fan_control_commands fan_control_commands;
7236 static u8 fan_control_initial_status;
7237 static u8 fan_control_desired_level;
7238 static u8 fan_control_resume_level;
7239 static int fan_watchdog_maxinterval;
7241 static struct mutex fan_mutex;
7243 static void fan_watchdog_fire(struct work_struct *ignored);
7244 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7246 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7247 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7248 "\\FSPD", /* 600e/x, 770e, 770x */
7249 ); /* all others */
7250 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7251 "JFNS", /* 770x-JL */
7252 ); /* all others */
7255 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7256 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7257 * be in auto mode (0x80).
7259 * This is corrected by any write to HFSP either by the driver, or
7260 * by the firmware.
7262 * We assume 0x07 really means auto mode while this quirk is active,
7263 * as this is far more likely than the ThinkPad being in level 7,
7264 * which is only used by the firmware during thermal emergencies.
7266 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7267 * TP-70 (T43, R52), which are known to be buggy.
7270 static void fan_quirk1_setup(void)
7272 if (fan_control_initial_status == 0x07) {
7273 printk(TPACPI_NOTICE
7274 "fan_init: initial fan status is unknown, "
7275 "assuming it is in auto mode\n");
7276 tp_features.fan_ctrl_status_undef = 1;
7280 static void fan_quirk1_handle(u8 *fan_status)
7282 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7283 if (*fan_status != fan_control_initial_status) {
7284 /* something changed the HFSP regisnter since
7285 * driver init time, so it is not undefined
7286 * anymore */
7287 tp_features.fan_ctrl_status_undef = 0;
7288 } else {
7289 /* Return most likely status. In fact, it
7290 * might be the only possible status */
7291 *fan_status = TP_EC_FAN_AUTO;
7296 /* Select main fan on X60/X61, NOOP on others */
7297 static bool fan_select_fan1(void)
7299 if (tp_features.second_fan) {
7300 u8 val;
7302 if (ec_read(fan_select_offset, &val) < 0)
7303 return false;
7304 val &= 0xFEU;
7305 if (ec_write(fan_select_offset, val) < 0)
7306 return false;
7308 return true;
7311 /* Select secondary fan on X60/X61 */
7312 static bool fan_select_fan2(void)
7314 u8 val;
7316 if (!tp_features.second_fan)
7317 return false;
7319 if (ec_read(fan_select_offset, &val) < 0)
7320 return false;
7321 val |= 0x01U;
7322 if (ec_write(fan_select_offset, val) < 0)
7323 return false;
7325 return true;
7329 * Call with fan_mutex held
7331 static void fan_update_desired_level(u8 status)
7333 if ((status &
7334 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7335 if (status > 7)
7336 fan_control_desired_level = 7;
7337 else
7338 fan_control_desired_level = status;
7342 static int fan_get_status(u8 *status)
7344 u8 s;
7346 /* TODO:
7347 * Add TPACPI_FAN_RD_ACPI_FANS ? */
7349 switch (fan_status_access_mode) {
7350 case TPACPI_FAN_RD_ACPI_GFAN:
7351 /* 570, 600e/x, 770e, 770x */
7353 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
7354 return -EIO;
7356 if (likely(status))
7357 *status = s & 0x07;
7359 break;
7361 case TPACPI_FAN_RD_TPEC:
7362 /* all except 570, 600e/x, 770e, 770x */
7363 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
7364 return -EIO;
7366 if (likely(status)) {
7367 *status = s;
7368 fan_quirk1_handle(status);
7371 break;
7373 default:
7374 return -ENXIO;
7377 return 0;
7380 static int fan_get_status_safe(u8 *status)
7382 int rc;
7383 u8 s;
7385 if (mutex_lock_killable(&fan_mutex))
7386 return -ERESTARTSYS;
7387 rc = fan_get_status(&s);
7388 if (!rc)
7389 fan_update_desired_level(s);
7390 mutex_unlock(&fan_mutex);
7392 if (status)
7393 *status = s;
7395 return rc;
7398 static int fan_get_speed(unsigned int *speed)
7400 u8 hi, lo;
7402 switch (fan_status_access_mode) {
7403 case TPACPI_FAN_RD_TPEC:
7404 /* all except 570, 600e/x, 770e, 770x */
7405 if (unlikely(!fan_select_fan1()))
7406 return -EIO;
7407 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
7408 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
7409 return -EIO;
7411 if (likely(speed))
7412 *speed = (hi << 8) | lo;
7414 break;
7416 default:
7417 return -ENXIO;
7420 return 0;
7423 static int fan2_get_speed(unsigned int *speed)
7425 u8 hi, lo;
7426 bool rc;
7428 switch (fan_status_access_mode) {
7429 case TPACPI_FAN_RD_TPEC:
7430 /* all except 570, 600e/x, 770e, 770x */
7431 if (unlikely(!fan_select_fan2()))
7432 return -EIO;
7433 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
7434 !acpi_ec_read(fan_rpm_offset + 1, &hi);
7435 fan_select_fan1(); /* play it safe */
7436 if (rc)
7437 return -EIO;
7439 if (likely(speed))
7440 *speed = (hi << 8) | lo;
7442 break;
7444 default:
7445 return -ENXIO;
7448 return 0;
7451 static int fan_set_level(int level)
7453 if (!fan_control_allowed)
7454 return -EPERM;
7456 switch (fan_control_access_mode) {
7457 case TPACPI_FAN_WR_ACPI_SFAN:
7458 if (level >= 0 && level <= 7) {
7459 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
7460 return -EIO;
7461 } else
7462 return -EINVAL;
7463 break;
7465 case TPACPI_FAN_WR_ACPI_FANS:
7466 case TPACPI_FAN_WR_TPEC:
7467 if (!(level & TP_EC_FAN_AUTO) &&
7468 !(level & TP_EC_FAN_FULLSPEED) &&
7469 ((level < 0) || (level > 7)))
7470 return -EINVAL;
7472 /* safety net should the EC not support AUTO
7473 * or FULLSPEED mode bits and just ignore them */
7474 if (level & TP_EC_FAN_FULLSPEED)
7475 level |= 7; /* safety min speed 7 */
7476 else if (level & TP_EC_FAN_AUTO)
7477 level |= 4; /* safety min speed 4 */
7479 if (!acpi_ec_write(fan_status_offset, level))
7480 return -EIO;
7481 else
7482 tp_features.fan_ctrl_status_undef = 0;
7483 break;
7485 default:
7486 return -ENXIO;
7489 vdbg_printk(TPACPI_DBG_FAN,
7490 "fan control: set fan control register to 0x%02x\n", level);
7491 return 0;
7494 static int fan_set_level_safe(int level)
7496 int rc;
7498 if (!fan_control_allowed)
7499 return -EPERM;
7501 if (mutex_lock_killable(&fan_mutex))
7502 return -ERESTARTSYS;
7504 if (level == TPACPI_FAN_LAST_LEVEL)
7505 level = fan_control_desired_level;
7507 rc = fan_set_level(level);
7508 if (!rc)
7509 fan_update_desired_level(level);
7511 mutex_unlock(&fan_mutex);
7512 return rc;
7515 static int fan_set_enable(void)
7517 u8 s;
7518 int rc;
7520 if (!fan_control_allowed)
7521 return -EPERM;
7523 if (mutex_lock_killable(&fan_mutex))
7524 return -ERESTARTSYS;
7526 switch (fan_control_access_mode) {
7527 case TPACPI_FAN_WR_ACPI_FANS:
7528 case TPACPI_FAN_WR_TPEC:
7529 rc = fan_get_status(&s);
7530 if (rc < 0)
7531 break;
7533 /* Don't go out of emergency fan mode */
7534 if (s != 7) {
7535 s &= 0x07;
7536 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
7539 if (!acpi_ec_write(fan_status_offset, s))
7540 rc = -EIO;
7541 else {
7542 tp_features.fan_ctrl_status_undef = 0;
7543 rc = 0;
7545 break;
7547 case TPACPI_FAN_WR_ACPI_SFAN:
7548 rc = fan_get_status(&s);
7549 if (rc < 0)
7550 break;
7552 s &= 0x07;
7554 /* Set fan to at least level 4 */
7555 s |= 4;
7557 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
7558 rc = -EIO;
7559 else
7560 rc = 0;
7561 break;
7563 default:
7564 rc = -ENXIO;
7567 mutex_unlock(&fan_mutex);
7569 if (!rc)
7570 vdbg_printk(TPACPI_DBG_FAN,
7571 "fan control: set fan control register to 0x%02x\n",
7573 return rc;
7576 static int fan_set_disable(void)
7578 int rc;
7580 if (!fan_control_allowed)
7581 return -EPERM;
7583 if (mutex_lock_killable(&fan_mutex))
7584 return -ERESTARTSYS;
7586 rc = 0;
7587 switch (fan_control_access_mode) {
7588 case TPACPI_FAN_WR_ACPI_FANS:
7589 case TPACPI_FAN_WR_TPEC:
7590 if (!acpi_ec_write(fan_status_offset, 0x00))
7591 rc = -EIO;
7592 else {
7593 fan_control_desired_level = 0;
7594 tp_features.fan_ctrl_status_undef = 0;
7596 break;
7598 case TPACPI_FAN_WR_ACPI_SFAN:
7599 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
7600 rc = -EIO;
7601 else
7602 fan_control_desired_level = 0;
7603 break;
7605 default:
7606 rc = -ENXIO;
7609 if (!rc)
7610 vdbg_printk(TPACPI_DBG_FAN,
7611 "fan control: set fan control register to 0\n");
7613 mutex_unlock(&fan_mutex);
7614 return rc;
7617 static int fan_set_speed(int speed)
7619 int rc;
7621 if (!fan_control_allowed)
7622 return -EPERM;
7624 if (mutex_lock_killable(&fan_mutex))
7625 return -ERESTARTSYS;
7627 rc = 0;
7628 switch (fan_control_access_mode) {
7629 case TPACPI_FAN_WR_ACPI_FANS:
7630 if (speed >= 0 && speed <= 65535) {
7631 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
7632 speed, speed, speed))
7633 rc = -EIO;
7634 } else
7635 rc = -EINVAL;
7636 break;
7638 default:
7639 rc = -ENXIO;
7642 mutex_unlock(&fan_mutex);
7643 return rc;
7646 static void fan_watchdog_reset(void)
7648 static int fan_watchdog_active;
7650 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
7651 return;
7653 if (fan_watchdog_active)
7654 cancel_delayed_work(&fan_watchdog_task);
7656 if (fan_watchdog_maxinterval > 0 &&
7657 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
7658 fan_watchdog_active = 1;
7659 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
7660 msecs_to_jiffies(fan_watchdog_maxinterval
7661 * 1000))) {
7662 printk(TPACPI_ERR
7663 "failed to queue the fan watchdog, "
7664 "watchdog will not trigger\n");
7666 } else
7667 fan_watchdog_active = 0;
7670 static void fan_watchdog_fire(struct work_struct *ignored)
7672 int rc;
7674 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
7675 return;
7677 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
7678 rc = fan_set_enable();
7679 if (rc < 0) {
7680 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
7681 "will try again later...\n", -rc);
7682 /* reschedule for later */
7683 fan_watchdog_reset();
7688 * SYSFS fan layout: hwmon compatible (device)
7690 * pwm*_enable:
7691 * 0: "disengaged" mode
7692 * 1: manual mode
7693 * 2: native EC "auto" mode (recommended, hardware default)
7695 * pwm*: set speed in manual mode, ignored otherwise.
7696 * 0 is level 0; 255 is level 7. Intermediate points done with linear
7697 * interpolation.
7699 * fan*_input: tachometer reading, RPM
7702 * SYSFS fan layout: extensions
7704 * fan_watchdog (driver):
7705 * fan watchdog interval in seconds, 0 disables (default), max 120
7708 /* sysfs fan pwm1_enable ----------------------------------------------- */
7709 static ssize_t fan_pwm1_enable_show(struct device *dev,
7710 struct device_attribute *attr,
7711 char *buf)
7713 int res, mode;
7714 u8 status;
7716 res = fan_get_status_safe(&status);
7717 if (res)
7718 return res;
7720 if (status & TP_EC_FAN_FULLSPEED) {
7721 mode = 0;
7722 } else if (status & TP_EC_FAN_AUTO) {
7723 mode = 2;
7724 } else
7725 mode = 1;
7727 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
7730 static ssize_t fan_pwm1_enable_store(struct device *dev,
7731 struct device_attribute *attr,
7732 const char *buf, size_t count)
7734 unsigned long t;
7735 int res, level;
7737 if (parse_strtoul(buf, 2, &t))
7738 return -EINVAL;
7740 tpacpi_disclose_usertask("hwmon pwm1_enable",
7741 "set fan mode to %lu\n", t);
7743 switch (t) {
7744 case 0:
7745 level = TP_EC_FAN_FULLSPEED;
7746 break;
7747 case 1:
7748 level = TPACPI_FAN_LAST_LEVEL;
7749 break;
7750 case 2:
7751 level = TP_EC_FAN_AUTO;
7752 break;
7753 case 3:
7754 /* reserved for software-controlled auto mode */
7755 return -ENOSYS;
7756 default:
7757 return -EINVAL;
7760 res = fan_set_level_safe(level);
7761 if (res == -ENXIO)
7762 return -EINVAL;
7763 else if (res < 0)
7764 return res;
7766 fan_watchdog_reset();
7768 return count;
7771 static struct device_attribute dev_attr_fan_pwm1_enable =
7772 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
7773 fan_pwm1_enable_show, fan_pwm1_enable_store);
7775 /* sysfs fan pwm1 ------------------------------------------------------ */
7776 static ssize_t fan_pwm1_show(struct device *dev,
7777 struct device_attribute *attr,
7778 char *buf)
7780 int res;
7781 u8 status;
7783 res = fan_get_status_safe(&status);
7784 if (res)
7785 return res;
7787 if ((status &
7788 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
7789 status = fan_control_desired_level;
7791 if (status > 7)
7792 status = 7;
7794 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
7797 static ssize_t fan_pwm1_store(struct device *dev,
7798 struct device_attribute *attr,
7799 const char *buf, size_t count)
7801 unsigned long s;
7802 int rc;
7803 u8 status, newlevel;
7805 if (parse_strtoul(buf, 255, &s))
7806 return -EINVAL;
7808 tpacpi_disclose_usertask("hwmon pwm1",
7809 "set fan speed to %lu\n", s);
7811 /* scale down from 0-255 to 0-7 */
7812 newlevel = (s >> 5) & 0x07;
7814 if (mutex_lock_killable(&fan_mutex))
7815 return -ERESTARTSYS;
7817 rc = fan_get_status(&status);
7818 if (!rc && (status &
7819 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
7820 rc = fan_set_level(newlevel);
7821 if (rc == -ENXIO)
7822 rc = -EINVAL;
7823 else if (!rc) {
7824 fan_update_desired_level(newlevel);
7825 fan_watchdog_reset();
7829 mutex_unlock(&fan_mutex);
7830 return (rc)? rc : count;
7833 static struct device_attribute dev_attr_fan_pwm1 =
7834 __ATTR(pwm1, S_IWUSR | S_IRUGO,
7835 fan_pwm1_show, fan_pwm1_store);
7837 /* sysfs fan fan1_input ------------------------------------------------ */
7838 static ssize_t fan_fan1_input_show(struct device *dev,
7839 struct device_attribute *attr,
7840 char *buf)
7842 int res;
7843 unsigned int speed;
7845 res = fan_get_speed(&speed);
7846 if (res < 0)
7847 return res;
7849 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7852 static struct device_attribute dev_attr_fan_fan1_input =
7853 __ATTR(fan1_input, S_IRUGO,
7854 fan_fan1_input_show, NULL);
7856 /* sysfs fan fan2_input ------------------------------------------------ */
7857 static ssize_t fan_fan2_input_show(struct device *dev,
7858 struct device_attribute *attr,
7859 char *buf)
7861 int res;
7862 unsigned int speed;
7864 res = fan2_get_speed(&speed);
7865 if (res < 0)
7866 return res;
7868 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
7871 static struct device_attribute dev_attr_fan_fan2_input =
7872 __ATTR(fan2_input, S_IRUGO,
7873 fan_fan2_input_show, NULL);
7875 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
7876 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
7877 char *buf)
7879 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
7882 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
7883 const char *buf, size_t count)
7885 unsigned long t;
7887 if (parse_strtoul(buf, 120, &t))
7888 return -EINVAL;
7890 if (!fan_control_allowed)
7891 return -EPERM;
7893 fan_watchdog_maxinterval = t;
7894 fan_watchdog_reset();
7896 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
7898 return count;
7901 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
7902 fan_fan_watchdog_show, fan_fan_watchdog_store);
7904 /* --------------------------------------------------------------------- */
7905 static struct attribute *fan_attributes[] = {
7906 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
7907 &dev_attr_fan_fan1_input.attr,
7908 NULL, /* for fan2_input */
7909 NULL
7912 static const struct attribute_group fan_attr_group = {
7913 .attrs = fan_attributes,
7916 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
7917 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
7919 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
7920 { .vendor = PCI_VENDOR_ID_IBM, \
7921 .bios = TPACPI_MATCH_ANY, \
7922 .ec = TPID(__id1, __id2), \
7923 .quirks = __quirks }
7925 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
7926 { .vendor = PCI_VENDOR_ID_LENOVO, \
7927 .bios = TPACPI_MATCH_ANY, \
7928 .ec = TPID(__id1, __id2), \
7929 .quirks = __quirks }
7931 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
7932 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
7933 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
7934 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
7935 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
7936 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
7939 #undef TPACPI_FAN_QL
7940 #undef TPACPI_FAN_QI
7942 static int __init fan_init(struct ibm_init_struct *iibm)
7944 int rc;
7945 unsigned long quirks;
7947 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7948 "initializing fan subdriver\n");
7950 mutex_init(&fan_mutex);
7951 fan_status_access_mode = TPACPI_FAN_NONE;
7952 fan_control_access_mode = TPACPI_FAN_WR_NONE;
7953 fan_control_commands = 0;
7954 fan_watchdog_maxinterval = 0;
7955 tp_features.fan_ctrl_status_undef = 0;
7956 tp_features.second_fan = 0;
7957 fan_control_desired_level = 7;
7959 TPACPI_ACPIHANDLE_INIT(fans);
7960 TPACPI_ACPIHANDLE_INIT(gfan);
7961 TPACPI_ACPIHANDLE_INIT(sfan);
7963 quirks = tpacpi_check_quirks(fan_quirk_table,
7964 ARRAY_SIZE(fan_quirk_table));
7966 if (gfan_handle) {
7967 /* 570, 600e/x, 770e, 770x */
7968 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
7969 } else {
7970 /* all other ThinkPads: note that even old-style
7971 * ThinkPad ECs supports the fan control register */
7972 if (likely(acpi_ec_read(fan_status_offset,
7973 &fan_control_initial_status))) {
7974 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
7975 if (quirks & TPACPI_FAN_Q1)
7976 fan_quirk1_setup();
7977 if (quirks & TPACPI_FAN_2FAN) {
7978 tp_features.second_fan = 1;
7979 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
7980 "secondary fan support enabled\n");
7982 } else {
7983 printk(TPACPI_ERR
7984 "ThinkPad ACPI EC access misbehaving, "
7985 "fan status and control unavailable\n");
7986 return 1;
7990 if (sfan_handle) {
7991 /* 570, 770x-JL */
7992 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
7993 fan_control_commands |=
7994 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
7995 } else {
7996 if (!gfan_handle) {
7997 /* gfan without sfan means no fan control */
7998 /* all other models implement TP EC 0x2f control */
8000 if (fans_handle) {
8001 /* X31, X40, X41 */
8002 fan_control_access_mode =
8003 TPACPI_FAN_WR_ACPI_FANS;
8004 fan_control_commands |=
8005 TPACPI_FAN_CMD_SPEED |
8006 TPACPI_FAN_CMD_LEVEL |
8007 TPACPI_FAN_CMD_ENABLE;
8008 } else {
8009 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8010 fan_control_commands |=
8011 TPACPI_FAN_CMD_LEVEL |
8012 TPACPI_FAN_CMD_ENABLE;
8017 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8018 "fan is %s, modes %d, %d\n",
8019 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8020 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8021 fan_status_access_mode, fan_control_access_mode);
8023 /* fan control master switch */
8024 if (!fan_control_allowed) {
8025 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8026 fan_control_commands = 0;
8027 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8028 "fan control features disabled by parameter\n");
8031 /* update fan_control_desired_level */
8032 if (fan_status_access_mode != TPACPI_FAN_NONE)
8033 fan_get_status_safe(NULL);
8035 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8036 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8037 if (tp_features.second_fan) {
8038 /* attach second fan tachometer */
8039 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8040 &dev_attr_fan_fan2_input.attr;
8042 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8043 &fan_attr_group);
8044 if (rc < 0)
8045 return rc;
8047 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8048 &driver_attr_fan_watchdog);
8049 if (rc < 0) {
8050 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8051 &fan_attr_group);
8052 return rc;
8054 return 0;
8055 } else
8056 return 1;
8059 static void fan_exit(void)
8061 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8062 "cancelling any pending fan watchdog tasks\n");
8064 /* FIXME: can we really do this unconditionally? */
8065 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8066 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8067 &driver_attr_fan_watchdog);
8069 cancel_delayed_work(&fan_watchdog_task);
8070 flush_workqueue(tpacpi_wq);
8073 static void fan_suspend(pm_message_t state)
8075 int rc;
8077 if (!fan_control_allowed)
8078 return;
8080 /* Store fan status in cache */
8081 fan_control_resume_level = 0;
8082 rc = fan_get_status_safe(&fan_control_resume_level);
8083 if (rc < 0)
8084 printk(TPACPI_NOTICE
8085 "failed to read fan level for later "
8086 "restore during resume: %d\n", rc);
8088 /* if it is undefined, don't attempt to restore it.
8089 * KEEP THIS LAST */
8090 if (tp_features.fan_ctrl_status_undef)
8091 fan_control_resume_level = 0;
8094 static void fan_resume(void)
8096 u8 current_level = 7;
8097 bool do_set = false;
8098 int rc;
8100 /* DSDT *always* updates status on resume */
8101 tp_features.fan_ctrl_status_undef = 0;
8103 if (!fan_control_allowed ||
8104 !fan_control_resume_level ||
8105 (fan_get_status_safe(&current_level) < 0))
8106 return;
8108 switch (fan_control_access_mode) {
8109 case TPACPI_FAN_WR_ACPI_SFAN:
8110 /* never decrease fan level */
8111 do_set = (fan_control_resume_level > current_level);
8112 break;
8113 case TPACPI_FAN_WR_ACPI_FANS:
8114 case TPACPI_FAN_WR_TPEC:
8115 /* never decrease fan level, scale is:
8116 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8118 * We expect the firmware to set either 7 or AUTO, but we
8119 * handle FULLSPEED out of paranoia.
8121 * So, we can safely only restore FULLSPEED or 7, anything
8122 * else could slow the fan. Restoring AUTO is useless, at
8123 * best that's exactly what the DSDT already set (it is the
8124 * slower it uses).
8126 * Always keep in mind that the DSDT *will* have set the
8127 * fans to what the vendor supposes is the best level. We
8128 * muck with it only to speed the fan up.
8130 if (fan_control_resume_level != 7 &&
8131 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8132 return;
8133 else
8134 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8135 (current_level != fan_control_resume_level);
8136 break;
8137 default:
8138 return;
8140 if (do_set) {
8141 printk(TPACPI_NOTICE
8142 "restoring fan level to 0x%02x\n",
8143 fan_control_resume_level);
8144 rc = fan_set_level_safe(fan_control_resume_level);
8145 if (rc < 0)
8146 printk(TPACPI_NOTICE
8147 "failed to restore fan level: %d\n", rc);
8151 static int fan_read(struct seq_file *m)
8153 int rc;
8154 u8 status;
8155 unsigned int speed = 0;
8157 switch (fan_status_access_mode) {
8158 case TPACPI_FAN_RD_ACPI_GFAN:
8159 /* 570, 600e/x, 770e, 770x */
8160 rc = fan_get_status_safe(&status);
8161 if (rc < 0)
8162 return rc;
8164 seq_printf(m, "status:\t\t%s\n"
8165 "level:\t\t%d\n",
8166 (status != 0) ? "enabled" : "disabled", status);
8167 break;
8169 case TPACPI_FAN_RD_TPEC:
8170 /* all except 570, 600e/x, 770e, 770x */
8171 rc = fan_get_status_safe(&status);
8172 if (rc < 0)
8173 return rc;
8175 seq_printf(m, "status:\t\t%s\n",
8176 (status != 0) ? "enabled" : "disabled");
8178 rc = fan_get_speed(&speed);
8179 if (rc < 0)
8180 return rc;
8182 seq_printf(m, "speed:\t\t%d\n", speed);
8184 if (status & TP_EC_FAN_FULLSPEED)
8185 /* Disengaged mode takes precedence */
8186 seq_printf(m, "level:\t\tdisengaged\n");
8187 else if (status & TP_EC_FAN_AUTO)
8188 seq_printf(m, "level:\t\tauto\n");
8189 else
8190 seq_printf(m, "level:\t\t%d\n", status);
8191 break;
8193 case TPACPI_FAN_NONE:
8194 default:
8195 seq_printf(m, "status:\t\tnot supported\n");
8198 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8199 seq_printf(m, "commands:\tlevel <level>");
8201 switch (fan_control_access_mode) {
8202 case TPACPI_FAN_WR_ACPI_SFAN:
8203 seq_printf(m, " (<level> is 0-7)\n");
8204 break;
8206 default:
8207 seq_printf(m, " (<level> is 0-7, "
8208 "auto, disengaged, full-speed)\n");
8209 break;
8213 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8214 seq_printf(m, "commands:\tenable, disable\n"
8215 "commands:\twatchdog <timeout> (<timeout> "
8216 "is 0 (off), 1-120 (seconds))\n");
8218 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8219 seq_printf(m, "commands:\tspeed <speed>"
8220 " (<speed> is 0-65535)\n");
8222 return 0;
8225 static int fan_write_cmd_level(const char *cmd, int *rc)
8227 int level;
8229 if (strlencmp(cmd, "level auto") == 0)
8230 level = TP_EC_FAN_AUTO;
8231 else if ((strlencmp(cmd, "level disengaged") == 0) |
8232 (strlencmp(cmd, "level full-speed") == 0))
8233 level = TP_EC_FAN_FULLSPEED;
8234 else if (sscanf(cmd, "level %d", &level) != 1)
8235 return 0;
8237 *rc = fan_set_level_safe(level);
8238 if (*rc == -ENXIO)
8239 printk(TPACPI_ERR "level command accepted for unsupported "
8240 "access mode %d", fan_control_access_mode);
8241 else if (!*rc)
8242 tpacpi_disclose_usertask("procfs fan",
8243 "set level to %d\n", level);
8245 return 1;
8248 static int fan_write_cmd_enable(const char *cmd, int *rc)
8250 if (strlencmp(cmd, "enable") != 0)
8251 return 0;
8253 *rc = fan_set_enable();
8254 if (*rc == -ENXIO)
8255 printk(TPACPI_ERR "enable command accepted for unsupported "
8256 "access mode %d", fan_control_access_mode);
8257 else if (!*rc)
8258 tpacpi_disclose_usertask("procfs fan", "enable\n");
8260 return 1;
8263 static int fan_write_cmd_disable(const char *cmd, int *rc)
8265 if (strlencmp(cmd, "disable") != 0)
8266 return 0;
8268 *rc = fan_set_disable();
8269 if (*rc == -ENXIO)
8270 printk(TPACPI_ERR "disable command accepted for unsupported "
8271 "access mode %d", fan_control_access_mode);
8272 else if (!*rc)
8273 tpacpi_disclose_usertask("procfs fan", "disable\n");
8275 return 1;
8278 static int fan_write_cmd_speed(const char *cmd, int *rc)
8280 int speed;
8282 /* TODO:
8283 * Support speed <low> <medium> <high> ? */
8285 if (sscanf(cmd, "speed %d", &speed) != 1)
8286 return 0;
8288 *rc = fan_set_speed(speed);
8289 if (*rc == -ENXIO)
8290 printk(TPACPI_ERR "speed command accepted for unsupported "
8291 "access mode %d", fan_control_access_mode);
8292 else if (!*rc)
8293 tpacpi_disclose_usertask("procfs fan",
8294 "set speed to %d\n", speed);
8296 return 1;
8299 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8301 int interval;
8303 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8304 return 0;
8306 if (interval < 0 || interval > 120)
8307 *rc = -EINVAL;
8308 else {
8309 fan_watchdog_maxinterval = interval;
8310 tpacpi_disclose_usertask("procfs fan",
8311 "set watchdog timer to %d\n",
8312 interval);
8315 return 1;
8318 static int fan_write(char *buf)
8320 char *cmd;
8321 int rc = 0;
8323 while (!rc && (cmd = next_cmd(&buf))) {
8324 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8325 fan_write_cmd_level(cmd, &rc)) &&
8326 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8327 (fan_write_cmd_enable(cmd, &rc) ||
8328 fan_write_cmd_disable(cmd, &rc) ||
8329 fan_write_cmd_watchdog(cmd, &rc))) &&
8330 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8331 fan_write_cmd_speed(cmd, &rc))
8333 rc = -EINVAL;
8334 else if (!rc)
8335 fan_watchdog_reset();
8338 return rc;
8341 static struct ibm_struct fan_driver_data = {
8342 .name = "fan",
8343 .read = fan_read,
8344 .write = fan_write,
8345 .exit = fan_exit,
8346 .suspend = fan_suspend,
8347 .resume = fan_resume,
8350 /****************************************************************************
8351 ****************************************************************************
8353 * Infrastructure
8355 ****************************************************************************
8356 ****************************************************************************/
8359 * HKEY event callout for other subdrivers go here
8360 * (yes, it is ugly, but it is quick, safe, and gets the job done
8362 static void tpacpi_driver_event(const unsigned int hkey_event)
8364 if (ibm_backlight_device) {
8365 switch (hkey_event) {
8366 case TP_HKEY_EV_BRGHT_UP:
8367 case TP_HKEY_EV_BRGHT_DOWN:
8368 tpacpi_brightness_notify_change();
8371 if (alsa_card) {
8372 switch (hkey_event) {
8373 case TP_HKEY_EV_VOL_UP:
8374 case TP_HKEY_EV_VOL_DOWN:
8375 case TP_HKEY_EV_VOL_MUTE:
8376 volume_alsa_notify_change();
8381 static void hotkey_driver_event(const unsigned int scancode)
8383 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
8386 /* sysfs name ---------------------------------------------------------- */
8387 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
8388 struct device_attribute *attr,
8389 char *buf)
8391 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
8394 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
8395 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
8397 /* --------------------------------------------------------------------- */
8399 /* /proc support */
8400 static struct proc_dir_entry *proc_dir;
8403 * Module and infrastructure proble, init and exit handling
8406 static int force_load;
8408 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
8409 static const char * __init str_supported(int is_supported)
8411 static char text_unsupported[] __initdata = "not supported";
8413 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
8415 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
8417 static void ibm_exit(struct ibm_struct *ibm)
8419 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
8421 list_del_init(&ibm->all_drivers);
8423 if (ibm->flags.acpi_notify_installed) {
8424 dbg_printk(TPACPI_DBG_EXIT,
8425 "%s: acpi_remove_notify_handler\n", ibm->name);
8426 BUG_ON(!ibm->acpi);
8427 acpi_remove_notify_handler(*ibm->acpi->handle,
8428 ibm->acpi->type,
8429 dispatch_acpi_notify);
8430 ibm->flags.acpi_notify_installed = 0;
8431 ibm->flags.acpi_notify_installed = 0;
8434 if (ibm->flags.proc_created) {
8435 dbg_printk(TPACPI_DBG_EXIT,
8436 "%s: remove_proc_entry\n", ibm->name);
8437 remove_proc_entry(ibm->name, proc_dir);
8438 ibm->flags.proc_created = 0;
8441 if (ibm->flags.acpi_driver_registered) {
8442 dbg_printk(TPACPI_DBG_EXIT,
8443 "%s: acpi_bus_unregister_driver\n", ibm->name);
8444 BUG_ON(!ibm->acpi);
8445 acpi_bus_unregister_driver(ibm->acpi->driver);
8446 kfree(ibm->acpi->driver);
8447 ibm->acpi->driver = NULL;
8448 ibm->flags.acpi_driver_registered = 0;
8451 if (ibm->flags.init_called && ibm->exit) {
8452 ibm->exit();
8453 ibm->flags.init_called = 0;
8456 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
8459 static int __init ibm_init(struct ibm_init_struct *iibm)
8461 int ret;
8462 struct ibm_struct *ibm = iibm->data;
8463 struct proc_dir_entry *entry;
8465 BUG_ON(ibm == NULL);
8467 INIT_LIST_HEAD(&ibm->all_drivers);
8469 if (ibm->flags.experimental && !experimental)
8470 return 0;
8472 dbg_printk(TPACPI_DBG_INIT,
8473 "probing for %s\n", ibm->name);
8475 if (iibm->init) {
8476 ret = iibm->init(iibm);
8477 if (ret > 0)
8478 return 0; /* probe failed */
8479 if (ret)
8480 return ret;
8482 ibm->flags.init_called = 1;
8485 if (ibm->acpi) {
8486 if (ibm->acpi->hid) {
8487 ret = register_tpacpi_subdriver(ibm);
8488 if (ret)
8489 goto err_out;
8492 if (ibm->acpi->notify) {
8493 ret = setup_acpi_notify(ibm);
8494 if (ret == -ENODEV) {
8495 printk(TPACPI_NOTICE "disabling subdriver %s\n",
8496 ibm->name);
8497 ret = 0;
8498 goto err_out;
8500 if (ret < 0)
8501 goto err_out;
8505 dbg_printk(TPACPI_DBG_INIT,
8506 "%s installed\n", ibm->name);
8508 if (ibm->read) {
8509 mode_t mode;
8511 mode = S_IRUGO;
8512 if (ibm->write)
8513 mode |= S_IWUSR;
8514 entry = proc_create_data(ibm->name, mode, proc_dir,
8515 &dispatch_proc_fops, ibm);
8516 if (!entry) {
8517 printk(TPACPI_ERR "unable to create proc entry %s\n",
8518 ibm->name);
8519 ret = -ENODEV;
8520 goto err_out;
8522 ibm->flags.proc_created = 1;
8525 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
8527 return 0;
8529 err_out:
8530 dbg_printk(TPACPI_DBG_INIT,
8531 "%s: at error exit path with result %d\n",
8532 ibm->name, ret);
8534 ibm_exit(ibm);
8535 return (ret < 0)? ret : 0;
8538 /* Probing */
8540 static bool __pure __init tpacpi_is_fw_digit(const char c)
8542 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
8545 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
8546 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
8547 const char t)
8549 return s && strlen(s) >= 8 &&
8550 tpacpi_is_fw_digit(s[0]) &&
8551 tpacpi_is_fw_digit(s[1]) &&
8552 s[2] == t && s[3] == 'T' &&
8553 tpacpi_is_fw_digit(s[4]) &&
8554 tpacpi_is_fw_digit(s[5]) &&
8555 s[6] == 'W' && s[7] == 'W';
8558 /* returns 0 - probe ok, or < 0 - probe error.
8559 * Probe ok doesn't mean thinkpad found.
8560 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
8561 static int __must_check __init get_thinkpad_model_data(
8562 struct thinkpad_id_data *tp)
8564 const struct dmi_device *dev = NULL;
8565 char ec_fw_string[18];
8566 char const *s;
8568 if (!tp)
8569 return -EINVAL;
8571 memset(tp, 0, sizeof(*tp));
8573 if (dmi_name_in_vendors("IBM"))
8574 tp->vendor = PCI_VENDOR_ID_IBM;
8575 else if (dmi_name_in_vendors("LENOVO"))
8576 tp->vendor = PCI_VENDOR_ID_LENOVO;
8577 else
8578 return 0;
8580 s = dmi_get_system_info(DMI_BIOS_VERSION);
8581 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
8582 if (s && !tp->bios_version_str)
8583 return -ENOMEM;
8585 /* Really ancient ThinkPad 240X will fail this, which is fine */
8586 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
8587 return 0;
8589 tp->bios_model = tp->bios_version_str[0]
8590 | (tp->bios_version_str[1] << 8);
8591 tp->bios_release = (tp->bios_version_str[4] << 8)
8592 | tp->bios_version_str[5];
8595 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
8596 * X32 or newer, all Z series; Some models must have an
8597 * up-to-date BIOS or they will not be detected.
8599 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
8601 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
8602 if (sscanf(dev->name,
8603 "IBM ThinkPad Embedded Controller -[%17c",
8604 ec_fw_string) == 1) {
8605 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
8606 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
8608 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
8609 if (!tp->ec_version_str)
8610 return -ENOMEM;
8612 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
8613 tp->ec_model = ec_fw_string[0]
8614 | (ec_fw_string[1] << 8);
8615 tp->ec_release = (ec_fw_string[4] << 8)
8616 | ec_fw_string[5];
8617 } else {
8618 printk(TPACPI_NOTICE
8619 "ThinkPad firmware release %s "
8620 "doesn't match the known patterns\n",
8621 ec_fw_string);
8622 printk(TPACPI_NOTICE
8623 "please report this to %s\n",
8624 TPACPI_MAIL);
8626 break;
8630 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
8631 if (s && !strnicmp(s, "ThinkPad", 8)) {
8632 tp->model_str = kstrdup(s, GFP_KERNEL);
8633 if (!tp->model_str)
8634 return -ENOMEM;
8637 s = dmi_get_system_info(DMI_PRODUCT_NAME);
8638 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
8639 if (s && !tp->nummodel_str)
8640 return -ENOMEM;
8642 return 0;
8645 static int __init probe_for_thinkpad(void)
8647 int is_thinkpad;
8649 if (acpi_disabled)
8650 return -ENODEV;
8653 * Non-ancient models have better DMI tagging, but very old models
8654 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
8656 is_thinkpad = (thinkpad_id.model_str != NULL) ||
8657 (thinkpad_id.ec_model != 0) ||
8658 tpacpi_is_fw_known();
8660 /* ec is required because many other handles are relative to it */
8661 TPACPI_ACPIHANDLE_INIT(ec);
8662 if (!ec_handle) {
8663 if (is_thinkpad)
8664 printk(TPACPI_ERR
8665 "Not yet supported ThinkPad detected!\n");
8666 return -ENODEV;
8669 if (!is_thinkpad && !force_load)
8670 return -ENODEV;
8672 return 0;
8676 /* Module init, exit, parameters */
8678 static struct ibm_init_struct ibms_init[] __initdata = {
8680 .init = thinkpad_acpi_driver_init,
8681 .data = &thinkpad_acpi_driver_data,
8684 .init = hotkey_init,
8685 .data = &hotkey_driver_data,
8688 .init = bluetooth_init,
8689 .data = &bluetooth_driver_data,
8692 .init = wan_init,
8693 .data = &wan_driver_data,
8696 .init = uwb_init,
8697 .data = &uwb_driver_data,
8699 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
8701 .init = video_init,
8702 .data = &video_driver_data,
8704 #endif
8706 .init = light_init,
8707 .data = &light_driver_data,
8710 .init = cmos_init,
8711 .data = &cmos_driver_data,
8714 .init = led_init,
8715 .data = &led_driver_data,
8718 .init = beep_init,
8719 .data = &beep_driver_data,
8722 .init = thermal_init,
8723 .data = &thermal_driver_data,
8726 .data = &ecdump_driver_data,
8729 .init = brightness_init,
8730 .data = &brightness_driver_data,
8733 .init = volume_init,
8734 .data = &volume_driver_data,
8737 .init = fan_init,
8738 .data = &fan_driver_data,
8742 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
8744 unsigned int i;
8745 struct ibm_struct *ibm;
8747 if (!kp || !kp->name || !val)
8748 return -EINVAL;
8750 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
8751 ibm = ibms_init[i].data;
8752 WARN_ON(ibm == NULL);
8754 if (!ibm || !ibm->name)
8755 continue;
8757 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
8758 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
8759 return -ENOSPC;
8760 strcpy(ibms_init[i].param, val);
8761 strcat(ibms_init[i].param, ",");
8762 return 0;
8766 return -EINVAL;
8769 module_param(experimental, int, 0444);
8770 MODULE_PARM_DESC(experimental,
8771 "Enables experimental features when non-zero");
8773 module_param_named(debug, dbg_level, uint, 0);
8774 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
8776 module_param(force_load, bool, 0444);
8777 MODULE_PARM_DESC(force_load,
8778 "Attempts to load the driver even on a "
8779 "mis-identified ThinkPad when true");
8781 module_param_named(fan_control, fan_control_allowed, bool, 0444);
8782 MODULE_PARM_DESC(fan_control,
8783 "Enables setting fan parameters features when true");
8785 module_param_named(brightness_mode, brightness_mode, uint, 0444);
8786 MODULE_PARM_DESC(brightness_mode,
8787 "Selects brightness control strategy: "
8788 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
8790 module_param(brightness_enable, uint, 0444);
8791 MODULE_PARM_DESC(brightness_enable,
8792 "Enables backlight control when 1, disables when 0");
8794 module_param(hotkey_report_mode, uint, 0444);
8795 MODULE_PARM_DESC(hotkey_report_mode,
8796 "used for backwards compatibility with userspace, "
8797 "see documentation");
8799 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
8800 module_param_named(volume_mode, volume_mode, uint, 0444);
8801 MODULE_PARM_DESC(volume_mode,
8802 "Selects volume control strategy: "
8803 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
8805 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
8806 MODULE_PARM_DESC(volume_capabilities,
8807 "Selects the mixer capabilites: "
8808 "0=auto, 1=volume and mute, 2=mute only");
8810 module_param_named(volume_control, volume_control_allowed, bool, 0444);
8811 MODULE_PARM_DESC(volume_control,
8812 "Enables software override for the console audio "
8813 "control when true");
8815 /* ALSA module API parameters */
8816 module_param_named(index, alsa_index, int, 0444);
8817 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
8818 module_param_named(id, alsa_id, charp, 0444);
8819 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
8820 module_param_named(enable, alsa_enable, bool, 0444);
8821 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
8822 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
8824 #define TPACPI_PARAM(feature) \
8825 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
8826 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
8827 "at module load, see documentation")
8829 TPACPI_PARAM(hotkey);
8830 TPACPI_PARAM(bluetooth);
8831 TPACPI_PARAM(video);
8832 TPACPI_PARAM(light);
8833 TPACPI_PARAM(cmos);
8834 TPACPI_PARAM(led);
8835 TPACPI_PARAM(beep);
8836 TPACPI_PARAM(ecdump);
8837 TPACPI_PARAM(brightness);
8838 TPACPI_PARAM(volume);
8839 TPACPI_PARAM(fan);
8841 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
8842 module_param(dbg_wlswemul, uint, 0444);
8843 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
8844 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
8845 MODULE_PARM_DESC(wlsw_state,
8846 "Initial state of the emulated WLSW switch");
8848 module_param(dbg_bluetoothemul, uint, 0444);
8849 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
8850 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
8851 MODULE_PARM_DESC(bluetooth_state,
8852 "Initial state of the emulated bluetooth switch");
8854 module_param(dbg_wwanemul, uint, 0444);
8855 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
8856 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
8857 MODULE_PARM_DESC(wwan_state,
8858 "Initial state of the emulated WWAN switch");
8860 module_param(dbg_uwbemul, uint, 0444);
8861 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
8862 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
8863 MODULE_PARM_DESC(uwb_state,
8864 "Initial state of the emulated UWB switch");
8865 #endif
8867 static void thinkpad_acpi_module_exit(void)
8869 struct ibm_struct *ibm, *itmp;
8871 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
8873 list_for_each_entry_safe_reverse(ibm, itmp,
8874 &tpacpi_all_drivers,
8875 all_drivers) {
8876 ibm_exit(ibm);
8879 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
8881 if (tpacpi_inputdev) {
8882 if (tp_features.input_device_registered)
8883 input_unregister_device(tpacpi_inputdev);
8884 else
8885 input_free_device(tpacpi_inputdev);
8888 if (tpacpi_hwmon)
8889 hwmon_device_unregister(tpacpi_hwmon);
8891 if (tp_features.sensors_pdev_attrs_registered)
8892 device_remove_file(&tpacpi_sensors_pdev->dev,
8893 &dev_attr_thinkpad_acpi_pdev_name);
8894 if (tpacpi_sensors_pdev)
8895 platform_device_unregister(tpacpi_sensors_pdev);
8896 if (tpacpi_pdev)
8897 platform_device_unregister(tpacpi_pdev);
8899 if (tp_features.sensors_pdrv_attrs_registered)
8900 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
8901 if (tp_features.platform_drv_attrs_registered)
8902 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
8904 if (tp_features.sensors_pdrv_registered)
8905 platform_driver_unregister(&tpacpi_hwmon_pdriver);
8907 if (tp_features.platform_drv_registered)
8908 platform_driver_unregister(&tpacpi_pdriver);
8910 if (proc_dir)
8911 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
8913 if (tpacpi_wq)
8914 destroy_workqueue(tpacpi_wq);
8916 kfree(thinkpad_id.bios_version_str);
8917 kfree(thinkpad_id.ec_version_str);
8918 kfree(thinkpad_id.model_str);
8922 static int __init thinkpad_acpi_module_init(void)
8924 int ret, i;
8926 tpacpi_lifecycle = TPACPI_LIFE_INIT;
8928 /* Parameter checking */
8929 if (hotkey_report_mode > 2)
8930 return -EINVAL;
8932 /* Driver-level probe */
8934 ret = get_thinkpad_model_data(&thinkpad_id);
8935 if (ret) {
8936 printk(TPACPI_ERR
8937 "unable to get DMI data: %d\n", ret);
8938 thinkpad_acpi_module_exit();
8939 return ret;
8941 ret = probe_for_thinkpad();
8942 if (ret) {
8943 thinkpad_acpi_module_exit();
8944 return ret;
8947 /* Driver initialization */
8949 TPACPI_ACPIHANDLE_INIT(ecrd);
8950 TPACPI_ACPIHANDLE_INIT(ecwr);
8952 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
8953 if (!tpacpi_wq) {
8954 thinkpad_acpi_module_exit();
8955 return -ENOMEM;
8958 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
8959 if (!proc_dir) {
8960 printk(TPACPI_ERR
8961 "unable to create proc dir " TPACPI_PROC_DIR);
8962 thinkpad_acpi_module_exit();
8963 return -ENODEV;
8966 ret = platform_driver_register(&tpacpi_pdriver);
8967 if (ret) {
8968 printk(TPACPI_ERR
8969 "unable to register main platform driver\n");
8970 thinkpad_acpi_module_exit();
8971 return ret;
8973 tp_features.platform_drv_registered = 1;
8975 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
8976 if (ret) {
8977 printk(TPACPI_ERR
8978 "unable to register hwmon platform driver\n");
8979 thinkpad_acpi_module_exit();
8980 return ret;
8982 tp_features.sensors_pdrv_registered = 1;
8984 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
8985 if (!ret) {
8986 tp_features.platform_drv_attrs_registered = 1;
8987 ret = tpacpi_create_driver_attributes(
8988 &tpacpi_hwmon_pdriver.driver);
8990 if (ret) {
8991 printk(TPACPI_ERR
8992 "unable to create sysfs driver attributes\n");
8993 thinkpad_acpi_module_exit();
8994 return ret;
8996 tp_features.sensors_pdrv_attrs_registered = 1;
8999 /* Device initialization */
9000 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9001 NULL, 0);
9002 if (IS_ERR(tpacpi_pdev)) {
9003 ret = PTR_ERR(tpacpi_pdev);
9004 tpacpi_pdev = NULL;
9005 printk(TPACPI_ERR "unable to register platform device\n");
9006 thinkpad_acpi_module_exit();
9007 return ret;
9009 tpacpi_sensors_pdev = platform_device_register_simple(
9010 TPACPI_HWMON_DRVR_NAME,
9011 -1, NULL, 0);
9012 if (IS_ERR(tpacpi_sensors_pdev)) {
9013 ret = PTR_ERR(tpacpi_sensors_pdev);
9014 tpacpi_sensors_pdev = NULL;
9015 printk(TPACPI_ERR
9016 "unable to register hwmon platform device\n");
9017 thinkpad_acpi_module_exit();
9018 return ret;
9020 ret = device_create_file(&tpacpi_sensors_pdev->dev,
9021 &dev_attr_thinkpad_acpi_pdev_name);
9022 if (ret) {
9023 printk(TPACPI_ERR
9024 "unable to create sysfs hwmon device attributes\n");
9025 thinkpad_acpi_module_exit();
9026 return ret;
9028 tp_features.sensors_pdev_attrs_registered = 1;
9029 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9030 if (IS_ERR(tpacpi_hwmon)) {
9031 ret = PTR_ERR(tpacpi_hwmon);
9032 tpacpi_hwmon = NULL;
9033 printk(TPACPI_ERR "unable to register hwmon device\n");
9034 thinkpad_acpi_module_exit();
9035 return ret;
9037 mutex_init(&tpacpi_inputdev_send_mutex);
9038 tpacpi_inputdev = input_allocate_device();
9039 if (!tpacpi_inputdev) {
9040 printk(TPACPI_ERR "unable to allocate input device\n");
9041 thinkpad_acpi_module_exit();
9042 return -ENOMEM;
9043 } else {
9044 /* Prepare input device, but don't register */
9045 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9046 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9047 tpacpi_inputdev->id.bustype = BUS_HOST;
9048 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
9049 thinkpad_id.vendor :
9050 PCI_VENDOR_ID_IBM;
9051 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9052 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9053 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9055 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9056 ret = ibm_init(&ibms_init[i]);
9057 if (ret >= 0 && *ibms_init[i].param)
9058 ret = ibms_init[i].data->write(ibms_init[i].param);
9059 if (ret < 0) {
9060 thinkpad_acpi_module_exit();
9061 return ret;
9065 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9067 ret = input_register_device(tpacpi_inputdev);
9068 if (ret < 0) {
9069 printk(TPACPI_ERR "unable to register input device\n");
9070 thinkpad_acpi_module_exit();
9071 return ret;
9072 } else {
9073 tp_features.input_device_registered = 1;
9076 return 0;
9079 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9082 * This will autoload the driver in almost every ThinkPad
9083 * in widespread use.
9085 * Only _VERY_ old models, like the 240, 240x and 570 lack
9086 * the HKEY event interface.
9088 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9091 * DMI matching for module autoloading
9093 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9094 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9096 * Only models listed in thinkwiki will be supported, so add yours
9097 * if it is not there yet.
9099 #define IBM_BIOS_MODULE_ALIAS(__type) \
9100 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9102 /* Ancient thinkpad BIOSes have to be identified by
9103 * BIOS type or model number, and there are far less
9104 * BIOS types than model numbers... */
9105 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9107 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9108 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9109 MODULE_DESCRIPTION(TPACPI_DESC);
9110 MODULE_VERSION(TPACPI_VERSION);
9111 MODULE_LICENSE("GPL");
9113 module_init(thinkpad_acpi_module_init);
9114 module_exit(thinkpad_acpi_module_exit);